2 * Driver for the on-board character LCD found on some ARM reference boards
3 * This is basically an Hitachi HD44780 LCD with a custom IP block to drive it
4 * http://en.wikipedia.org/wiki/HD44780_Character_LCD
5 * Currently it will just display the text "ARM Linux" and the linux version
7 * License terms: GNU General Public License (GPL) version 2
8 * Author: Linus Walleij <triad@df.lth.se>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
20 #include <generated/utsrelease.h>
22 #define DRIVERNAME "arm-charlcd"
23 #define CHARLCD_TIMEOUT (msecs_to_jiffies(1000))
25 /* Offsets to registers */
26 #define CHAR_COM 0x00U
27 #define CHAR_DAT 0x04U
29 #define CHAR_RAW 0x0CU
30 #define CHAR_MASK 0x10U
31 #define CHAR_STAT 0x14U
33 #define CHAR_RAW_CLEAR 0x00000000U
34 #define CHAR_RAW_VALID 0x00000100U
36 /* Hitachi HD44780 display commands */
37 #define HD_CLEAR 0x01U
39 #define HD_ENTRYMODE 0x04U
40 #define HD_ENTRYMODE_INCREMENT 0x02U
41 #define HD_ENTRYMODE_SHIFT 0x01U
42 #define HD_DISPCTRL 0x08U
43 #define HD_DISPCTRL_ON 0x04U
44 #define HD_DISPCTRL_CURSOR_ON 0x02U
45 #define HD_DISPCTRL_CURSOR_BLINK 0x01U
46 #define HD_CRSR_SHIFT 0x10U
47 #define HD_CRSR_SHIFT_DISPLAY 0x08U
48 #define HD_CRSR_SHIFT_DISPLAY_RIGHT 0x04U
49 #define HD_FUNCSET 0x20U
50 #define HD_FUNCSET_8BIT 0x10U
51 #define HD_FUNCSET_2_LINES 0x08U
52 #define HD_FUNCSET_FONT_5X10 0x04U
53 #define HD_SET_CGRAM 0x40U
54 #define HD_SET_DDRAM 0x80U
55 #define HD_BUSY_FLAG 0x80U
58 * @dev: a pointer back to containing device
59 * @phybase: the offset to the controller in physical memory
60 * @physize: the size of the physical page
61 * @virtbase: the offset to the controller in virtual memory
62 * @irq: reserved interrupt number
63 * @complete: completion structure for the last LCD command
69 void __iomem
*virtbase
;
71 struct completion complete
;
72 struct delayed_work init_work
;
75 static irqreturn_t
charlcd_interrupt(int irq
, void *data
)
77 struct charlcd
*lcd
= data
;
80 status
= readl(lcd
->virtbase
+ CHAR_STAT
) & 0x01;
82 writel(CHAR_RAW_CLEAR
, lcd
->virtbase
+ CHAR_RAW
);
84 complete(&lcd
->complete
);
86 dev_info(lcd
->dev
, "Spurious IRQ (%02x)\n", status
);
91 static void charlcd_wait_complete_irq(struct charlcd
*lcd
)
95 ret
= wait_for_completion_interruptible_timeout(&lcd
->complete
,
97 /* Disable IRQ after completion */
98 writel(0x00, lcd
->virtbase
+ CHAR_MASK
);
102 "wait_for_completion_interruptible_timeout() "
103 "returned %d waiting for ready\n", ret
);
108 dev_err(lcd
->dev
, "charlcd controller timed out "
109 "waiting for ready\n");
114 static u8
charlcd_4bit_read_char(struct charlcd
*lcd
)
120 /* If we can, use an IRQ to wait for the data, else poll */
122 charlcd_wait_complete_irq(lcd
);
126 while (!(val
& CHAR_RAW_VALID
) && i
< 10) {
128 val
= readl(lcd
->virtbase
+ CHAR_RAW
);
132 writel(CHAR_RAW_CLEAR
, lcd
->virtbase
+ CHAR_RAW
);
136 /* Read the 4 high bits of the data */
137 data
= readl(lcd
->virtbase
+ CHAR_RD
) & 0xf0;
140 * The second read for the low bits does not trigger an IRQ
141 * so in this case we have to poll for the 4 lower bits
145 while (!(val
& CHAR_RAW_VALID
) && i
< 10) {
147 val
= readl(lcd
->virtbase
+ CHAR_RAW
);
150 writel(CHAR_RAW_CLEAR
, lcd
->virtbase
+ CHAR_RAW
);
153 /* Read the 4 low bits of the data */
154 data
|= (readl(lcd
->virtbase
+ CHAR_RD
) >> 4) & 0x0f;
159 static bool charlcd_4bit_read_bf(struct charlcd
*lcd
)
163 * If we'll use IRQs to wait for the busyflag, clear any
164 * pending flag and enable IRQ
166 writel(CHAR_RAW_CLEAR
, lcd
->virtbase
+ CHAR_RAW
);
167 init_completion(&lcd
->complete
);
168 writel(0x01, lcd
->virtbase
+ CHAR_MASK
);
170 readl(lcd
->virtbase
+ CHAR_COM
);
171 return charlcd_4bit_read_char(lcd
) & HD_BUSY_FLAG
? true : false;
174 static void charlcd_4bit_wait_busy(struct charlcd
*lcd
)
179 while (charlcd_4bit_read_bf(lcd
) && retries
)
182 dev_err(lcd
->dev
, "timeout waiting for busyflag\n");
185 static void charlcd_4bit_command(struct charlcd
*lcd
, u8 cmd
)
187 u32 cmdlo
= (cmd
<< 4) & 0xf0;
188 u32 cmdhi
= (cmd
& 0xf0);
190 writel(cmdhi
, lcd
->virtbase
+ CHAR_COM
);
192 writel(cmdlo
, lcd
->virtbase
+ CHAR_COM
);
193 charlcd_4bit_wait_busy(lcd
);
196 static void charlcd_4bit_char(struct charlcd
*lcd
, u8 ch
)
198 u32 chlo
= (ch
<< 4) & 0xf0;
199 u32 chhi
= (ch
& 0xf0);
201 writel(chhi
, lcd
->virtbase
+ CHAR_DAT
);
203 writel(chlo
, lcd
->virtbase
+ CHAR_DAT
);
204 charlcd_4bit_wait_busy(lcd
);
207 static void charlcd_4bit_print(struct charlcd
*lcd
, int line
, const char *str
)
213 * We support line 0, 1
214 * Line 1 runs from 0x00..0x27
215 * Line 2 runs from 0x28..0x4f
225 charlcd_4bit_command(lcd
, HD_SET_DDRAM
| offset
);
228 for (i
= 0; i
< strlen(str
) && i
< 0x28; i
++)
229 charlcd_4bit_char(lcd
, str
[i
]);
232 static void charlcd_4bit_init(struct charlcd
*lcd
)
234 /* These commands cannot be checked with the busy flag */
235 writel(HD_FUNCSET
| HD_FUNCSET_8BIT
, lcd
->virtbase
+ CHAR_COM
);
237 writel(HD_FUNCSET
| HD_FUNCSET_8BIT
, lcd
->virtbase
+ CHAR_COM
);
239 writel(HD_FUNCSET
| HD_FUNCSET_8BIT
, lcd
->virtbase
+ CHAR_COM
);
241 /* Go to 4bit mode */
242 writel(HD_FUNCSET
, lcd
->virtbase
+ CHAR_COM
);
245 * 4bit mode, 2 lines, 5x8 font, after this the number of lines
246 * and the font cannot be changed until the next initialization sequence
248 charlcd_4bit_command(lcd
, HD_FUNCSET
| HD_FUNCSET_2_LINES
);
249 charlcd_4bit_command(lcd
, HD_DISPCTRL
| HD_DISPCTRL_ON
);
250 charlcd_4bit_command(lcd
, HD_ENTRYMODE
| HD_ENTRYMODE_INCREMENT
);
251 charlcd_4bit_command(lcd
, HD_CLEAR
);
252 charlcd_4bit_command(lcd
, HD_HOME
);
253 /* Put something useful in the display */
254 charlcd_4bit_print(lcd
, 0, "ARM Linux");
255 charlcd_4bit_print(lcd
, 1, UTS_RELEASE
);
258 static void charlcd_init_work(struct work_struct
*work
)
260 struct charlcd
*lcd
=
261 container_of(work
, struct charlcd
, init_work
.work
);
263 charlcd_4bit_init(lcd
);
266 static int __init
charlcd_probe(struct platform_device
*pdev
)
270 struct resource
*res
;
272 lcd
= kzalloc(sizeof(struct charlcd
), GFP_KERNEL
);
276 lcd
->dev
= &pdev
->dev
;
278 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
281 goto out_no_resource
;
283 lcd
->phybase
= res
->start
;
284 lcd
->physize
= resource_size(res
);
286 if (request_mem_region(lcd
->phybase
, lcd
->physize
,
287 DRIVERNAME
) == NULL
) {
289 goto out_no_memregion
;
292 lcd
->virtbase
= ioremap(lcd
->phybase
, lcd
->physize
);
293 if (!lcd
->virtbase
) {
295 goto out_no_memregion
;
298 lcd
->irq
= platform_get_irq(pdev
, 0);
299 /* If no IRQ is supplied, we'll survive without it */
301 if (request_irq(lcd
->irq
, charlcd_interrupt
, 0,
308 platform_set_drvdata(pdev
, lcd
);
311 * Initialize the display in a delayed work, because
312 * it is VERY slow and would slow down the boot of the system.
314 INIT_DELAYED_WORK(&lcd
->init_work
, charlcd_init_work
);
315 schedule_delayed_work(&lcd
->init_work
, 0);
317 dev_info(&pdev
->dev
, "initialized ARM character LCD at %08x\n",
323 iounmap(lcd
->virtbase
);
325 release_mem_region(lcd
->phybase
, SZ_4K
);
331 static int __exit
charlcd_remove(struct platform_device
*pdev
)
333 struct charlcd
*lcd
= platform_get_drvdata(pdev
);
336 free_irq(lcd
->irq
, lcd
);
337 iounmap(lcd
->virtbase
);
338 release_mem_region(lcd
->phybase
, lcd
->physize
);
345 static int charlcd_suspend(struct device
*dev
)
347 struct platform_device
*pdev
= to_platform_device(dev
);
348 struct charlcd
*lcd
= platform_get_drvdata(pdev
);
350 /* Power the display off */
351 charlcd_4bit_command(lcd
, HD_DISPCTRL
);
355 static int charlcd_resume(struct device
*dev
)
357 struct platform_device
*pdev
= to_platform_device(dev
);
358 struct charlcd
*lcd
= platform_get_drvdata(pdev
);
360 /* Turn the display back on */
361 charlcd_4bit_command(lcd
, HD_DISPCTRL
| HD_DISPCTRL_ON
);
365 static const struct dev_pm_ops charlcd_pm_ops
= {
366 .suspend
= charlcd_suspend
,
367 .resume
= charlcd_resume
,
370 static const struct of_device_id charlcd_match
[] = {
371 { .compatible
= "arm,versatile-lcd", },
375 static struct platform_driver charlcd_driver
= {
378 .pm
= &charlcd_pm_ops
,
379 .of_match_table
= of_match_ptr(charlcd_match
),
381 .remove
= __exit_p(charlcd_remove
),
384 module_platform_driver_probe(charlcd_driver
, charlcd_probe
);
386 MODULE_AUTHOR("Linus Walleij <triad@df.lth.se>");
387 MODULE_DESCRIPTION("ARM Character LCD Driver");
388 MODULE_LICENSE("GPL v2");