arm-charlcd.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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
  6. *
  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>
  21. #define DRIVERNAME "arm-charlcd"
  22. #define CHARLCD_TIMEOUT (msecs_to_jiffies(1000))
  23. /* Offsets to registers */
  24. #define CHAR_COM 0x00U
  25. #define CHAR_DAT 0x04U
  26. #define CHAR_RD 0x08U
  27. #define CHAR_RAW 0x0CU
  28. #define CHAR_MASK 0x10U
  29. #define CHAR_STAT 0x14U
  30. #define CHAR_RAW_CLEAR 0x00000000U
  31. #define CHAR_RAW_VALID 0x00000100U
  32. /* Hitachi HD44780 display commands */
  33. #define HD_CLEAR 0x01U
  34. #define HD_HOME 0x02U
  35. #define HD_ENTRYMODE 0x04U
  36. #define HD_ENTRYMODE_INCREMENT 0x02U
  37. #define HD_ENTRYMODE_SHIFT 0x01U
  38. #define HD_DISPCTRL 0x08U
  39. #define HD_DISPCTRL_ON 0x04U
  40. #define HD_DISPCTRL_CURSOR_ON 0x02U
  41. #define HD_DISPCTRL_CURSOR_BLINK 0x01U
  42. #define HD_CRSR_SHIFT 0x10U
  43. #define HD_CRSR_SHIFT_DISPLAY 0x08U
  44. #define HD_CRSR_SHIFT_DISPLAY_RIGHT 0x04U
  45. #define HD_FUNCSET 0x20U
  46. #define HD_FUNCSET_8BIT 0x10U
  47. #define HD_FUNCSET_2_LINES 0x08U
  48. #define HD_FUNCSET_FONT_5X10 0x04U
  49. #define HD_SET_CGRAM 0x40U
  50. #define HD_SET_DDRAM 0x80U
  51. #define HD_BUSY_FLAG 0x80U
  52. /**
  53. * @dev: a pointer back to containing device
  54. * @phybase: the offset to the controller in physical memory
  55. * @physize: the size of the physical page
  56. * @virtbase: the offset to the controller in virtual memory
  57. * @irq: reserved interrupt number
  58. * @complete: completion structure for the last LCD command
  59. */
  60. struct charlcd {
  61. struct device *dev;
  62. u32 phybase;
  63. u32 physize;
  64. void __iomem *virtbase;
  65. int irq;
  66. struct completion complete;
  67. struct delayed_work init_work;
  68. };
  69. static irqreturn_t charlcd_interrupt(int irq, void *data)
  70. {
  71. struct charlcd *lcd = data;
  72. u8 status;
  73. status = readl(lcd->virtbase + CHAR_STAT) & 0x01;
  74. /* Clear IRQ */
  75. writel(CHAR_RAW_CLEAR, lcd->virtbase + CHAR_RAW);
  76. if (status)
  77. complete(&lcd->complete);
  78. else
  79. dev_info(lcd->dev, "Spurious IRQ (%02x)\n", status);
  80. return IRQ_HANDLED;
  81. }
  82. static void charlcd_wait_complete_irq(struct charlcd *lcd)
  83. {
  84. int ret;
  85. ret = wait_for_completion_interruptible_timeout(&lcd->complete,
  86. CHARLCD_TIMEOUT);
  87. /* Disable IRQ after completion */
  88. writel(0x00, lcd->virtbase + CHAR_MASK);
  89. if (ret < 0) {
  90. dev_err(lcd->dev,
  91. "wait_for_completion_interruptible_timeout() "
  92. "returned %d waiting for ready\n", ret);
  93. return;
  94. }
  95. if (ret == 0) {
  96. dev_err(lcd->dev, "charlcd controller timed out "
  97. "waiting for ready\n");
  98. return;
  99. }
  100. }
  101. static u8 charlcd_4bit_read_char(struct charlcd *lcd)
  102. {
  103. u8 data;
  104. u32 val;
  105. int i;
  106. /* If we can, use an IRQ to wait for the data, else poll */
  107. if (lcd->irq >= 0)
  108. charlcd_wait_complete_irq(lcd);
  109. else {
  110. i = 0;
  111. val = 0;
  112. while (!(val & CHAR_RAW_VALID) && i < 10) {
  113. udelay(100);
  114. val = readl(lcd->virtbase + CHAR_RAW);
  115. i++;
  116. }
  117. writel(CHAR_RAW_CLEAR, lcd->virtbase + CHAR_RAW);
  118. }
  119. msleep(1);
  120. /* Read the 4 high bits of the data */
  121. data = readl(lcd->virtbase + CHAR_RD) & 0xf0;
  122. /*
  123. * The second read for the low bits does not trigger an IRQ
  124. * so in this case we have to poll for the 4 lower bits
  125. */
  126. i = 0;
  127. val = 0;
  128. while (!(val & CHAR_RAW_VALID) && i < 10) {
  129. udelay(100);
  130. val = readl(lcd->virtbase + CHAR_RAW);
  131. i++;
  132. }
  133. writel(CHAR_RAW_CLEAR, lcd->virtbase + CHAR_RAW);
  134. msleep(1);
  135. /* Read the 4 low bits of the data */
  136. data |= (readl(lcd->virtbase + CHAR_RD) >> 4) & 0x0f;
  137. return data;
  138. }
  139. static bool charlcd_4bit_read_bf(struct charlcd *lcd)
  140. {
  141. if (lcd->irq >= 0) {
  142. /*
  143. * If we'll use IRQs to wait for the busyflag, clear any
  144. * pending flag and enable IRQ
  145. */
  146. writel(CHAR_RAW_CLEAR, lcd->virtbase + CHAR_RAW);
  147. init_completion(&lcd->complete);
  148. writel(0x01, lcd->virtbase + CHAR_MASK);
  149. }
  150. readl(lcd->virtbase + CHAR_COM);
  151. return charlcd_4bit_read_char(lcd) & HD_BUSY_FLAG ? true : false;
  152. }
  153. static void charlcd_4bit_wait_busy(struct charlcd *lcd)
  154. {
  155. int retries = 50;
  156. udelay(100);
  157. while (charlcd_4bit_read_bf(lcd) && retries)
  158. retries--;
  159. if (!retries)
  160. dev_err(lcd->dev, "timeout waiting for busyflag\n");
  161. }
  162. static void charlcd_4bit_command(struct charlcd *lcd, u8 cmd)
  163. {
  164. u32 cmdlo = (cmd << 4) & 0xf0;
  165. u32 cmdhi = (cmd & 0xf0);
  166. writel(cmdhi, lcd->virtbase + CHAR_COM);
  167. udelay(10);
  168. writel(cmdlo, lcd->virtbase + CHAR_COM);
  169. charlcd_4bit_wait_busy(lcd);
  170. }
  171. static void charlcd_4bit_char(struct charlcd *lcd, u8 ch)
  172. {
  173. u32 chlo = (ch << 4) & 0xf0;
  174. u32 chhi = (ch & 0xf0);
  175. writel(chhi, lcd->virtbase + CHAR_DAT);
  176. udelay(10);
  177. writel(chlo, lcd->virtbase + CHAR_DAT);
  178. charlcd_4bit_wait_busy(lcd);
  179. }
  180. static void charlcd_4bit_print(struct charlcd *lcd, int line, const char *str)
  181. {
  182. u8 offset;
  183. int i;
  184. /*
  185. * We support line 0, 1
  186. * Line 1 runs from 0x00..0x27
  187. * Line 2 runs from 0x28..0x4f
  188. */
  189. if (line == 0)
  190. offset = 0;
  191. else if (line == 1)
  192. offset = 0x28;
  193. else
  194. return;
  195. /* Set offset */
  196. charlcd_4bit_command(lcd, HD_SET_DDRAM | offset);
  197. /* Send string */
  198. for (i = 0; i < strlen(str) && i < 0x28; i++)
  199. charlcd_4bit_char(lcd, str[i]);
  200. }
  201. static void charlcd_4bit_init(struct charlcd *lcd)
  202. {
  203. /* These commands cannot be checked with the busy flag */
  204. writel(HD_FUNCSET | HD_FUNCSET_8BIT, lcd->virtbase + CHAR_COM);
  205. msleep(5);
  206. writel(HD_FUNCSET | HD_FUNCSET_8BIT, lcd->virtbase + CHAR_COM);
  207. udelay(100);
  208. writel(HD_FUNCSET | HD_FUNCSET_8BIT, lcd->virtbase + CHAR_COM);
  209. udelay(100);
  210. /* Go to 4bit mode */
  211. writel(HD_FUNCSET, lcd->virtbase + CHAR_COM);
  212. udelay(100);
  213. /*
  214. * 4bit mode, 2 lines, 5x8 font, after this the number of lines
  215. * and the font cannot be changed until the next initialization sequence
  216. */
  217. charlcd_4bit_command(lcd, HD_FUNCSET | HD_FUNCSET_2_LINES);
  218. charlcd_4bit_command(lcd, HD_DISPCTRL | HD_DISPCTRL_ON);
  219. charlcd_4bit_command(lcd, HD_ENTRYMODE | HD_ENTRYMODE_INCREMENT);
  220. charlcd_4bit_command(lcd, HD_CLEAR);
  221. charlcd_4bit_command(lcd, HD_HOME);
  222. /* Put something useful in the display */
  223. charlcd_4bit_print(lcd, 0, "ARM Linux");
  224. charlcd_4bit_print(lcd, 1, UTS_RELEASE);
  225. }
  226. static void charlcd_init_work(struct work_struct *work)
  227. {
  228. struct charlcd *lcd =
  229. container_of(work, struct charlcd, init_work.work);
  230. charlcd_4bit_init(lcd);
  231. }
  232. static int __init charlcd_probe(struct platform_device *pdev)
  233. {
  234. int ret;
  235. struct charlcd *lcd;
  236. struct resource *res;
  237. lcd = kzalloc(sizeof(struct charlcd), GFP_KERNEL);
  238. if (!lcd)
  239. return -ENOMEM;
  240. lcd->dev = &pdev->dev;
  241. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  242. if (!res) {
  243. ret = -ENOENT;
  244. goto out_no_resource;
  245. }
  246. lcd->phybase = res->start;
  247. lcd->physize = resource_size(res);
  248. if (request_mem_region(lcd->phybase, lcd->physize,
  249. DRIVERNAME) == NULL) {
  250. ret = -EBUSY;
  251. goto out_no_memregion;
  252. }
  253. lcd->virtbase = ioremap(lcd->phybase, lcd->physize);
  254. if (!lcd->virtbase) {
  255. ret = -ENOMEM;
  256. goto out_no_memregion;
  257. }
  258. lcd->irq = platform_get_irq(pdev, 0);
  259. /* If no IRQ is supplied, we'll survive without it */
  260. if (lcd->irq >= 0) {
  261. if (request_irq(lcd->irq, charlcd_interrupt, 0,
  262. DRIVERNAME, lcd)) {
  263. ret = -EIO;
  264. goto out_no_irq;
  265. }
  266. }
  267. platform_set_drvdata(pdev, lcd);
  268. /*
  269. * Initialize the display in a delayed work, because
  270. * it is VERY slow and would slow down the boot of the system.
  271. */
  272. INIT_DELAYED_WORK(&lcd->init_work, charlcd_init_work);
  273. schedule_delayed_work(&lcd->init_work, 0);
  274. dev_info(&pdev->dev, "initialized ARM character LCD at %08x\n",
  275. lcd->phybase);
  276. return 0;
  277. out_no_irq:
  278. iounmap(lcd->virtbase);
  279. out_no_memregion:
  280. release_mem_region(lcd->phybase, SZ_4K);
  281. out_no_resource:
  282. kfree(lcd);
  283. return ret;
  284. }
  285. static int __exit charlcd_remove(struct platform_device *pdev)
  286. {
  287. struct charlcd *lcd = platform_get_drvdata(pdev);
  288. if (lcd) {
  289. free_irq(lcd->irq, lcd);
  290. iounmap(lcd->virtbase);
  291. release_mem_region(lcd->phybase, lcd->physize);
  292. kfree(lcd);
  293. }
  294. return 0;
  295. }
  296. static int charlcd_suspend(struct device *dev)
  297. {
  298. struct platform_device *pdev = to_platform_device(dev);
  299. struct charlcd *lcd = platform_get_drvdata(pdev);
  300. /* Power the display off */
  301. charlcd_4bit_command(lcd, HD_DISPCTRL);
  302. return 0;
  303. }
  304. static int charlcd_resume(struct device *dev)
  305. {
  306. struct platform_device *pdev = to_platform_device(dev);
  307. struct charlcd *lcd = platform_get_drvdata(pdev);
  308. /* Turn the display back on */
  309. charlcd_4bit_command(lcd, HD_DISPCTRL | HD_DISPCTRL_ON);
  310. return 0;
  311. }
  312. static const struct dev_pm_ops charlcd_pm_ops = {
  313. .suspend = charlcd_suspend,
  314. .resume = charlcd_resume,
  315. };
  316. static const struct of_device_id charlcd_match[] = {
  317. { .compatible = "arm,versatile-lcd", },
  318. {}
  319. };
  320. static struct platform_driver charlcd_driver = {
  321. .driver = {
  322. .name = DRIVERNAME,
  323. .pm = &charlcd_pm_ops,
  324. .of_match_table = of_match_ptr(charlcd_match),
  325. },
  326. .remove = __exit_p(charlcd_remove),
  327. };
  328. module_platform_driver_probe(charlcd_driver, charlcd_probe);
  329. MODULE_AUTHOR("Linus Walleij <triad@df.lth.se>");
  330. MODULE_DESCRIPTION("ARM Character LCD Driver");
  331. MODULE_LICENSE("GPL v2");