-
Notifications
You must be signed in to change notification settings - Fork 1
/
hitachi-lcd.c
272 lines (239 loc) · 6.41 KB
/
hitachi-lcd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/gpio/consumer.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/slab.h>
/* Simple device driver
* for the Hitachi HD44780U LDC */
#define MODULE_NAME "hitachi-lcd"
#define CMD_TIME_START (40) /* 40 usec */
#define CMD_TIME_END (50)
#define DISPLAY_ON 1
#define DISPLAY_OFF 0
#define CURSOR 1
#define NO_CURSOR 0
#define BLINK 1
#define NO_BLINK 0
#define LINES_2 1
#define LINES_1 0
#define LCD_MAX_CHARS 40
#define BUF_SIZE 32
static DEFINE_MUTEX(hitachi_lcd_lock);
struct hitachi_lcd {
struct gpio_desc *data[4];
struct gpio_desc *rs;
struct gpio_desc *rw;
struct gpio_desc *e;
char *buf;
unsigned short index;
};
static struct hitachi_lcd lcd;
/* module params */
static int modeset = 1;
static int max_chars = LCD_MAX_CHARS;
/* Pins use BCM Numbering */
static int pin_d4 = 5;
static int pin_d5 = 6;
static int pin_d6 = 12;
static int pin_d7 = 13;
static int pin_rs = 16;
static int pin_rw = 26;
static int pin_e = 25;
module_param(modeset, int, 0444);
module_param(max_chars, int, 0444);
module_param(pin_d4, int, 0444);
module_param(pin_d5, int, 0444);
module_param(pin_d6, int, 0444);
module_param(pin_d7, int, 0444);
module_param(pin_rs, int, 0444);
module_param(pin_rw, int, 0444);
module_param(pin_e, int, 0444);
static struct gpio_desc *init_gpio_out(unsigned int n)
{
struct gpio_desc *gpio;
gpio = gpio_to_desc(n);
if (IS_ERR(gpio)) {
pr_err("hitachi-lcd: failed gpio %d allocation\n", n);
return NULL;
}
if (gpiod_direction_output(gpio, GPIOD_OUT_LOW)) {
pr_err("hitachi-lcd: failed set gpio direction\n");
gpiod_put(gpio);
return NULL;
}
return gpio;
}
static void put_gpios(struct hitachi_lcd *lcd)
{
int i;
for (i = 0; i < 4; i++) {
if (lcd->data[i])
gpiod_put(lcd->data[i]);
}
if (lcd->rs)
gpiod_put(lcd->rs);
if (lcd->rw)
gpiod_put(lcd->rw);
if (lcd->e)
gpiod_put(lcd->e);
}
static void clear_data_gpios(struct hitachi_lcd *lcd)
{
gpiod_set_value(lcd->data[0], 0);
gpiod_set_value(lcd->data[1], 0);
gpiod_set_value(lcd->data[2], 0);
gpiod_set_value(lcd->data[3], 0);
}
static void clear_all_gpios(struct hitachi_lcd *lcd)
{
clear_data_gpios(lcd);
gpiod_set_value(lcd->rs, 0);
gpiod_set_value(lcd->rw, 0);
gpiod_set_value(lcd->e, 0);
}
static void send_command(struct hitachi_lcd *lcd)
{
gpiod_set_value(lcd->e, 1);
usleep_range(CMD_TIME_START, CMD_TIME_END);
gpiod_set_value(lcd->e, 0);
}
static void set_display(struct hitachi_lcd *lcd, unsigned display,
unsigned cursor, unsigned blink)
{
clear_all_gpios(lcd);
send_command(lcd);
gpiod_set_value(lcd->data[3], 1);
gpiod_set_value(lcd->data[2], display);
gpiod_set_value(lcd->data[1], cursor);
gpiod_set_value(lcd->data[0], blink);
send_command(lcd);
}
static void setmode_4bit(struct hitachi_lcd *lcd, unsigned line_no)
{
clear_all_gpios(lcd);
gpiod_set_value(lcd->data[1], 1);
send_command(lcd);
send_command(lcd);
gpiod_set_value(lcd->data[3], line_no);
send_command(lcd);
}
static void clear_display(struct hitachi_lcd *lcd)
{
clear_all_gpios(lcd);
send_command(lcd);
gpiod_set_value(lcd->data[0], 1);
send_command(lcd);
}
static void entry_mode_set(struct hitachi_lcd *lcd)
{
clear_all_gpios(lcd);
send_command(lcd);
gpiod_set_value(lcd->data[1], 1);
gpiod_set_value(lcd->data[2], 1);
send_command(lcd);
}
static void lcd_putchar(struct hitachi_lcd *lcd, char c)
{
int i;
clear_all_gpios(lcd);
/* 1st nibble */
gpiod_set_value(lcd->rs, 1);
for (i = 4; i < 8; i++) {
gpiod_set_value(lcd->data[i - 4], (c & (1 << i)) >> i);
}
send_command(lcd);
/* 2nd nibble */
for (i = 0; i < 4; i++) {
gpiod_set_value(lcd->data[i], (c & (1 << i)) >> i);
}
send_command(lcd);
}
static void lcd_puts(struct hitachi_lcd *lcd, char *s, size_t size)
{
int i;
for (i = 0; i < size; i++) {
if (lcd->index > max_chars) {
lcd->index = 0;
clear_display(lcd);
msleep(100);
}
if (s[i] == '\n')
continue;
lcd_putchar(lcd, s[i]);
lcd->index++;
}
}
ssize_t lcd_write(struct file *file, const char __user *from, size_t size,
loff_t *off)
{
size_t read_size;
if (size < 1)
return size;
mutex_lock(&hitachi_lcd_lock);
read_size = simple_write_to_buffer(lcd.buf, size, off, from, BUF_SIZE);
if (read_size > 0) {
lcd_puts(&lcd, lcd.buf, read_size);
}
mutex_unlock(&hitachi_lcd_lock);
return read_size;
}
static struct file_operations lcd_ops = {
.write = lcd_write, .read = NULL,
};
static struct miscdevice lcd_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = MODULE_NAME,
.fops = &lcd_ops,
.mode = 0222,
};
static int __init hitachi_lcd_load(void)
{
int i;
/* lcd char buffer */
lcd.buf = kmalloc(BUF_SIZE, GFP_KERNEL);
if (!lcd.buf)
return -ENOMEM;
lcd.data[0] = init_gpio_out(pin_d4); /* D4 */
lcd.data[1] = init_gpio_out(pin_d5); /* D5 */
lcd.data[2] = init_gpio_out(pin_d6); /* D6 */
lcd.data[3] = init_gpio_out(pin_d7); /* D7 */
lcd.rs = init_gpio_out(pin_rs); /* RS */
lcd.rw = init_gpio_out(pin_rw); /* RW */
lcd.e = init_gpio_out(pin_e); /* E */
lcd.index = 0; /* next position in the lcd */
/* check correct init of gpios */
if (!lcd.rs || !lcd.rw || !lcd.e)
goto cleanup;
for (i = 0; i < 4; i++) {
if (!lcd.data[i])
goto cleanup;
}
/* init display */
/* set modeset to 0 if the display
* has not been HW reinit */
if (modeset)
setmode_4bit(&lcd, LINES_1);
set_display(&lcd, DISPLAY_ON, CURSOR, BLINK);
entry_mode_set(&lcd);
clear_display(&lcd);
misc_register(&lcd_device);
return 0;
cleanup:
kfree(lcd.buf);
put_gpios(&lcd);
misc_deregister(&lcd_device);
return -EIO;
}
static void __exit hitachi_lcd_unload(void)
{
kfree(lcd.buf);
set_display(&lcd, DISPLAY_OFF, NO_CURSOR, NO_BLINK);
clear_display(&lcd);
put_gpios(&lcd);
misc_deregister(&lcd_device);
}
module_init(hitachi_lcd_load);
module_exit(hitachi_lcd_unload);
MODULE_AUTHOR("Antonio Cardace <[email protected]>");
MODULE_LICENSE("GPL v2");