IB/hfi1: Fix issues with qp_stats print
[linux/fpc-iii.git] / drivers / misc / arm-charlcd.c
blobc65b5ea5d5ef44c90713bae61e9a0c461e3be93e
1 /*
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>
9 */
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/of.h>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
17 #include <linux/io.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
28 #define CHAR_RD 0x08U
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
38 #define HD_HOME 0x02U
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
57 /**
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
65 struct charlcd {
66 struct device *dev;
67 u32 phybase;
68 u32 physize;
69 void __iomem *virtbase;
70 int irq;
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;
78 u8 status;
80 status = readl(lcd->virtbase + CHAR_STAT) & 0x01;
81 /* Clear IRQ */
82 writel(CHAR_RAW_CLEAR, lcd->virtbase + CHAR_RAW);
83 if (status)
84 complete(&lcd->complete);
85 else
86 dev_info(lcd->dev, "Spurious IRQ (%02x)\n", status);
87 return IRQ_HANDLED;
91 static void charlcd_wait_complete_irq(struct charlcd *lcd)
93 int ret;
95 ret = wait_for_completion_interruptible_timeout(&lcd->complete,
96 CHARLCD_TIMEOUT);
97 /* Disable IRQ after completion */
98 writel(0x00, lcd->virtbase + CHAR_MASK);
100 if (ret < 0) {
101 dev_err(lcd->dev,
102 "wait_for_completion_interruptible_timeout() "
103 "returned %d waiting for ready\n", ret);
104 return;
107 if (ret == 0) {
108 dev_err(lcd->dev, "charlcd controller timed out "
109 "waiting for ready\n");
110 return;
114 static u8 charlcd_4bit_read_char(struct charlcd *lcd)
116 u8 data;
117 u32 val;
118 int i;
120 /* If we can, use an IRQ to wait for the data, else poll */
121 if (lcd->irq >= 0)
122 charlcd_wait_complete_irq(lcd);
123 else {
124 i = 0;
125 val = 0;
126 while (!(val & CHAR_RAW_VALID) && i < 10) {
127 udelay(100);
128 val = readl(lcd->virtbase + CHAR_RAW);
129 i++;
132 writel(CHAR_RAW_CLEAR, lcd->virtbase + CHAR_RAW);
134 msleep(1);
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
143 i = 0;
144 val = 0;
145 while (!(val & CHAR_RAW_VALID) && i < 10) {
146 udelay(100);
147 val = readl(lcd->virtbase + CHAR_RAW);
148 i++;
150 writel(CHAR_RAW_CLEAR, lcd->virtbase + CHAR_RAW);
151 msleep(1);
153 /* Read the 4 low bits of the data */
154 data |= (readl(lcd->virtbase + CHAR_RD) >> 4) & 0x0f;
156 return data;
159 static bool charlcd_4bit_read_bf(struct charlcd *lcd)
161 if (lcd->irq >= 0) {
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)
176 int retries = 50;
178 udelay(100);
179 while (charlcd_4bit_read_bf(lcd) && retries)
180 retries--;
181 if (!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);
191 udelay(10);
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);
202 udelay(10);
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)
209 u8 offset;
210 int i;
213 * We support line 0, 1
214 * Line 1 runs from 0x00..0x27
215 * Line 2 runs from 0x28..0x4f
217 if (line == 0)
218 offset = 0;
219 else if (line == 1)
220 offset = 0x28;
221 else
222 return;
224 /* Set offset */
225 charlcd_4bit_command(lcd, HD_SET_DDRAM | offset);
227 /* Send string */
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);
236 msleep(5);
237 writel(HD_FUNCSET | HD_FUNCSET_8BIT, lcd->virtbase + CHAR_COM);
238 udelay(100);
239 writel(HD_FUNCSET | HD_FUNCSET_8BIT, lcd->virtbase + CHAR_COM);
240 udelay(100);
241 /* Go to 4bit mode */
242 writel(HD_FUNCSET, lcd->virtbase + CHAR_COM);
243 udelay(100);
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)
268 int ret;
269 struct charlcd *lcd;
270 struct resource *res;
272 lcd = kzalloc(sizeof(struct charlcd), GFP_KERNEL);
273 if (!lcd)
274 return -ENOMEM;
276 lcd->dev = &pdev->dev;
278 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
279 if (!res) {
280 ret = -ENOENT;
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) {
288 ret = -EBUSY;
289 goto out_no_memregion;
292 lcd->virtbase = ioremap(lcd->phybase, lcd->physize);
293 if (!lcd->virtbase) {
294 ret = -ENOMEM;
295 goto out_no_memregion;
298 lcd->irq = platform_get_irq(pdev, 0);
299 /* If no IRQ is supplied, we'll survive without it */
300 if (lcd->irq >= 0) {
301 if (request_irq(lcd->irq, charlcd_interrupt, 0,
302 DRIVERNAME, lcd)) {
303 ret = -EIO;
304 goto out_no_irq;
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",
318 lcd->phybase);
320 return 0;
322 out_no_irq:
323 iounmap(lcd->virtbase);
324 out_no_memregion:
325 release_mem_region(lcd->phybase, SZ_4K);
326 out_no_resource:
327 kfree(lcd);
328 return ret;
331 static int __exit charlcd_remove(struct platform_device *pdev)
333 struct charlcd *lcd = platform_get_drvdata(pdev);
335 if (lcd) {
336 free_irq(lcd->irq, lcd);
337 iounmap(lcd->virtbase);
338 release_mem_region(lcd->phybase, lcd->physize);
339 kfree(lcd);
342 return 0;
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);
352 return 0;
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);
362 return 0;
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 = {
376 .driver = {
377 .name = DRIVERNAME,
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");