1
2
3
4
5
6
7
8
9
10#include <linux/init.h>
11#include <linux/interrupt.h>
12#include <linux/platform_device.h>
13#include <linux/of.h>
14#include <linux/completion.h>
15#include <linux/delay.h>
16#include <linux/io.h>
17#include <linux/slab.h>
18#include <linux/workqueue.h>
19#include <generated/utsrelease.h>
20
21#define DRIVERNAME "arm-charlcd"
22#define CHARLCD_TIMEOUT (msecs_to_jiffies(1000))
23
24
25#define CHAR_COM 0x00U
26#define CHAR_DAT 0x04U
27#define CHAR_RD 0x08U
28#define CHAR_RAW 0x0CU
29#define CHAR_MASK 0x10U
30#define CHAR_STAT 0x14U
31
32#define CHAR_RAW_CLEAR 0x00000000U
33#define CHAR_RAW_VALID 0x00000100U
34
35
36#define HD_CLEAR 0x01U
37#define HD_HOME 0x02U
38#define HD_ENTRYMODE 0x04U
39#define HD_ENTRYMODE_INCREMENT 0x02U
40#define HD_ENTRYMODE_SHIFT 0x01U
41#define HD_DISPCTRL 0x08U
42#define HD_DISPCTRL_ON 0x04U
43#define HD_DISPCTRL_CURSOR_ON 0x02U
44#define HD_DISPCTRL_CURSOR_BLINK 0x01U
45#define HD_CRSR_SHIFT 0x10U
46#define HD_CRSR_SHIFT_DISPLAY 0x08U
47#define HD_CRSR_SHIFT_DISPLAY_RIGHT 0x04U
48#define HD_FUNCSET 0x20U
49#define HD_FUNCSET_8BIT 0x10U
50#define HD_FUNCSET_2_LINES 0x08U
51#define HD_FUNCSET_FONT_5X10 0x04U
52#define HD_SET_CGRAM 0x40U
53#define HD_SET_DDRAM 0x80U
54#define HD_BUSY_FLAG 0x80U
55
56
57
58
59
60
61
62
63
64
65
66struct charlcd {
67 struct device *dev;
68 u32 phybase;
69 u32 physize;
70 void __iomem *virtbase;
71 int irq;
72 struct completion complete;
73 struct delayed_work init_work;
74};
75
76static irqreturn_t charlcd_interrupt(int irq, void *data)
77{
78 struct charlcd *lcd = data;
79 u8 status;
80
81 status = readl(lcd->virtbase + CHAR_STAT) & 0x01;
82
83 writel(CHAR_RAW_CLEAR, lcd->virtbase + CHAR_RAW);
84 if (status)
85 complete(&lcd->complete);
86 else
87 dev_info(lcd->dev, "Spurious IRQ (%02x)\n", status);
88 return IRQ_HANDLED;
89}
90
91
92static void charlcd_wait_complete_irq(struct charlcd *lcd)
93{
94 int ret;
95
96 ret = wait_for_completion_interruptible_timeout(&lcd->complete,
97 CHARLCD_TIMEOUT);
98
99 writel(0x00, lcd->virtbase + CHAR_MASK);
100
101 if (ret < 0) {
102 dev_err(lcd->dev,
103 "wait_for_completion_interruptible_timeout() "
104 "returned %d waiting for ready\n", ret);
105 return;
106 }
107
108 if (ret == 0) {
109 dev_err(lcd->dev, "charlcd controller timed out "
110 "waiting for ready\n");
111 return;
112 }
113}
114
115static u8 charlcd_4bit_read_char(struct charlcd *lcd)
116{
117 u8 data;
118 u32 val;
119 int i;
120
121
122 if (lcd->irq >= 0)
123 charlcd_wait_complete_irq(lcd);
124 else {
125 i = 0;
126 val = 0;
127 while (!(val & CHAR_RAW_VALID) && i < 10) {
128 udelay(100);
129 val = readl(lcd->virtbase + CHAR_RAW);
130 i++;
131 }
132
133 writel(CHAR_RAW_CLEAR, lcd->virtbase + CHAR_RAW);
134 }
135 msleep(1);
136
137
138 data = readl(lcd->virtbase + CHAR_RD) & 0xf0;
139
140
141
142
143
144 i = 0;
145 val = 0;
146 while (!(val & CHAR_RAW_VALID) && i < 10) {
147 udelay(100);
148 val = readl(lcd->virtbase + CHAR_RAW);
149 i++;
150 }
151 writel(CHAR_RAW_CLEAR, lcd->virtbase + CHAR_RAW);
152 msleep(1);
153
154
155 data |= (readl(lcd->virtbase + CHAR_RD) >> 4) & 0x0f;
156
157 return data;
158}
159
160static bool charlcd_4bit_read_bf(struct charlcd *lcd)
161{
162 if (lcd->irq >= 0) {
163
164
165
166
167 writel(CHAR_RAW_CLEAR, lcd->virtbase + CHAR_RAW);
168 init_completion(&lcd->complete);
169 writel(0x01, lcd->virtbase + CHAR_MASK);
170 }
171 readl(lcd->virtbase + CHAR_COM);
172 return charlcd_4bit_read_char(lcd) & HD_BUSY_FLAG ? true : false;
173}
174
175static void charlcd_4bit_wait_busy(struct charlcd *lcd)
176{
177 int retries = 50;
178
179 udelay(100);
180 while (charlcd_4bit_read_bf(lcd) && retries)
181 retries--;
182 if (!retries)
183 dev_err(lcd->dev, "timeout waiting for busyflag\n");
184}
185
186static void charlcd_4bit_command(struct charlcd *lcd, u8 cmd)
187{
188 u32 cmdlo = (cmd << 4) & 0xf0;
189 u32 cmdhi = (cmd & 0xf0);
190
191 writel(cmdhi, lcd->virtbase + CHAR_COM);
192 udelay(10);
193 writel(cmdlo, lcd->virtbase + CHAR_COM);
194 charlcd_4bit_wait_busy(lcd);
195}
196
197static void charlcd_4bit_char(struct charlcd *lcd, u8 ch)
198{
199 u32 chlo = (ch << 4) & 0xf0;
200 u32 chhi = (ch & 0xf0);
201
202 writel(chhi, lcd->virtbase + CHAR_DAT);
203 udelay(10);
204 writel(chlo, lcd->virtbase + CHAR_DAT);
205 charlcd_4bit_wait_busy(lcd);
206}
207
208static void charlcd_4bit_print(struct charlcd *lcd, int line, const char *str)
209{
210 u8 offset;
211 int i;
212
213
214
215
216
217
218 if (line == 0)
219 offset = 0;
220 else if (line == 1)
221 offset = 0x28;
222 else
223 return;
224
225
226 charlcd_4bit_command(lcd, HD_SET_DDRAM | offset);
227
228
229 for (i = 0; i < strlen(str) && i < 0x28; i++)
230 charlcd_4bit_char(lcd, str[i]);
231}
232
233static void charlcd_4bit_init(struct charlcd *lcd)
234{
235
236 writel(HD_FUNCSET | HD_FUNCSET_8BIT, lcd->virtbase + CHAR_COM);
237 msleep(5);
238 writel(HD_FUNCSET | HD_FUNCSET_8BIT, lcd->virtbase + CHAR_COM);
239 udelay(100);
240 writel(HD_FUNCSET | HD_FUNCSET_8BIT, lcd->virtbase + CHAR_COM);
241 udelay(100);
242
243 writel(HD_FUNCSET, lcd->virtbase + CHAR_COM);
244 udelay(100);
245
246
247
248
249 charlcd_4bit_command(lcd, HD_FUNCSET | HD_FUNCSET_2_LINES);
250 charlcd_4bit_command(lcd, HD_DISPCTRL | HD_DISPCTRL_ON);
251 charlcd_4bit_command(lcd, HD_ENTRYMODE | HD_ENTRYMODE_INCREMENT);
252 charlcd_4bit_command(lcd, HD_CLEAR);
253 charlcd_4bit_command(lcd, HD_HOME);
254
255 charlcd_4bit_print(lcd, 0, "ARM Linux");
256 charlcd_4bit_print(lcd, 1, UTS_RELEASE);
257}
258
259static void charlcd_init_work(struct work_struct *work)
260{
261 struct charlcd *lcd =
262 container_of(work, struct charlcd, init_work.work);
263
264 charlcd_4bit_init(lcd);
265}
266
267static int __init charlcd_probe(struct platform_device *pdev)
268{
269 int ret;
270 struct charlcd *lcd;
271 struct resource *res;
272
273 lcd = kzalloc(sizeof(struct charlcd), GFP_KERNEL);
274 if (!lcd)
275 return -ENOMEM;
276
277 lcd->dev = &pdev->dev;
278
279 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
280 if (!res) {
281 ret = -ENOENT;
282 goto out_no_resource;
283 }
284 lcd->phybase = res->start;
285 lcd->physize = resource_size(res);
286
287 if (request_mem_region(lcd->phybase, lcd->physize,
288 DRIVERNAME) == NULL) {
289 ret = -EBUSY;
290 goto out_no_memregion;
291 }
292
293 lcd->virtbase = ioremap(lcd->phybase, lcd->physize);
294 if (!lcd->virtbase) {
295 ret = -ENOMEM;
296 goto out_no_memregion;
297 }
298
299 lcd->irq = platform_get_irq(pdev, 0);
300
301 if (lcd->irq >= 0) {
302 if (request_irq(lcd->irq, charlcd_interrupt, 0,
303 DRIVERNAME, lcd)) {
304 ret = -EIO;
305 goto out_no_irq;
306 }
307 }
308
309 platform_set_drvdata(pdev, lcd);
310
311
312
313
314
315 INIT_DELAYED_WORK(&lcd->init_work, charlcd_init_work);
316 schedule_delayed_work(&lcd->init_work, 0);
317
318 dev_info(&pdev->dev, "initialized ARM character LCD at %08x\n",
319 lcd->phybase);
320
321 return 0;
322
323out_no_irq:
324 iounmap(lcd->virtbase);
325out_no_memregion:
326 release_mem_region(lcd->phybase, SZ_4K);
327out_no_resource:
328 kfree(lcd);
329 return ret;
330}
331
332static int charlcd_suspend(struct device *dev)
333{
334 struct charlcd *lcd = dev_get_drvdata(dev);
335
336
337 charlcd_4bit_command(lcd, HD_DISPCTRL);
338 return 0;
339}
340
341static int charlcd_resume(struct device *dev)
342{
343 struct charlcd *lcd = dev_get_drvdata(dev);
344
345
346 charlcd_4bit_command(lcd, HD_DISPCTRL | HD_DISPCTRL_ON);
347 return 0;
348}
349
350static const struct dev_pm_ops charlcd_pm_ops = {
351 .suspend = charlcd_suspend,
352 .resume = charlcd_resume,
353};
354
355static const struct of_device_id charlcd_match[] = {
356 { .compatible = "arm,versatile-lcd", },
357 {}
358};
359
360static struct platform_driver charlcd_driver = {
361 .driver = {
362 .name = DRIVERNAME,
363 .pm = &charlcd_pm_ops,
364 .suppress_bind_attrs = true,
365 .of_match_table = of_match_ptr(charlcd_match),
366 },
367};
368builtin_platform_driver_probe(charlcd_driver, charlcd_probe);
369