arm-charlcd.c 9.3 KB

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