img-ascii-lcd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright (C) 2016 Imagination Technologies
  3. * Author: Paul Burton <paul.burton@mips.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. */
  10. #include <generated/utsrelease.h>
  11. #include <linux/kernel.h>
  12. #include <linux/io.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. #include <linux/sysfs.h>
  21. struct img_ascii_lcd_ctx;
  22. /**
  23. * struct img_ascii_lcd_config - Configuration information about an LCD model
  24. * @num_chars: the number of characters the LCD can display
  25. * @external_regmap: true if registers are in a system controller, else false
  26. * @update: function called to update the LCD
  27. */
  28. struct img_ascii_lcd_config {
  29. unsigned int num_chars;
  30. bool external_regmap;
  31. void (*update)(struct img_ascii_lcd_ctx *ctx);
  32. };
  33. /**
  34. * struct img_ascii_lcd_ctx - Private data structure
  35. * @pdev: the ASCII LCD platform device
  36. * @base: the base address of the LCD registers
  37. * @regmap: the regmap through which LCD registers are accessed
  38. * @offset: the offset within regmap to the start of the LCD registers
  39. * @cfg: pointer to the LCD model configuration
  40. * @message: the full message to display or scroll on the LCD
  41. * @message_len: the length of the @message string
  42. * @scroll_pos: index of the first character of @message currently displayed
  43. * @scroll_rate: scroll interval in jiffies
  44. * @timer: timer used to implement scrolling
  45. * @curr: the string currently displayed on the LCD
  46. */
  47. struct img_ascii_lcd_ctx {
  48. struct platform_device *pdev;
  49. union {
  50. void __iomem *base;
  51. struct regmap *regmap;
  52. };
  53. u32 offset;
  54. const struct img_ascii_lcd_config *cfg;
  55. char *message;
  56. unsigned int message_len;
  57. unsigned int scroll_pos;
  58. unsigned int scroll_rate;
  59. struct timer_list timer;
  60. char curr[] __aligned(8);
  61. };
  62. /*
  63. * MIPS Boston development board
  64. */
  65. static void boston_update(struct img_ascii_lcd_ctx *ctx)
  66. {
  67. ulong val;
  68. #if BITS_PER_LONG == 64
  69. val = *((u64 *)&ctx->curr[0]);
  70. __raw_writeq(val, ctx->base);
  71. #elif BITS_PER_LONG == 32
  72. val = *((u32 *)&ctx->curr[0]);
  73. __raw_writel(val, ctx->base);
  74. val = *((u32 *)&ctx->curr[4]);
  75. __raw_writel(val, ctx->base + 4);
  76. #else
  77. # error Not 32 or 64 bit
  78. #endif
  79. }
  80. static struct img_ascii_lcd_config boston_config = {
  81. .num_chars = 8,
  82. .update = boston_update,
  83. };
  84. /*
  85. * MIPS Malta development board
  86. */
  87. static void malta_update(struct img_ascii_lcd_ctx *ctx)
  88. {
  89. unsigned int i;
  90. int err = 0;
  91. for (i = 0; i < ctx->cfg->num_chars; i++) {
  92. err = regmap_write(ctx->regmap,
  93. ctx->offset + (i * 8), ctx->curr[i]);
  94. if (err)
  95. break;
  96. }
  97. if (unlikely(err))
  98. pr_err_ratelimited("Failed to update LCD display: %d\n", err);
  99. }
  100. static struct img_ascii_lcd_config malta_config = {
  101. .num_chars = 8,
  102. .external_regmap = true,
  103. .update = malta_update,
  104. };
  105. /*
  106. * MIPS SEAD3 development board
  107. */
  108. enum {
  109. SEAD3_REG_LCD_CTRL = 0x00,
  110. #define SEAD3_REG_LCD_CTRL_SETDRAM BIT(7)
  111. SEAD3_REG_LCD_DATA = 0x08,
  112. SEAD3_REG_CPLD_STATUS = 0x10,
  113. #define SEAD3_REG_CPLD_STATUS_BUSY BIT(0)
  114. SEAD3_REG_CPLD_DATA = 0x18,
  115. #define SEAD3_REG_CPLD_DATA_BUSY BIT(7)
  116. };
  117. static int sead3_wait_sm_idle(struct img_ascii_lcd_ctx *ctx)
  118. {
  119. unsigned int status;
  120. int err;
  121. do {
  122. err = regmap_read(ctx->regmap,
  123. ctx->offset + SEAD3_REG_CPLD_STATUS,
  124. &status);
  125. if (err)
  126. return err;
  127. } while (status & SEAD3_REG_CPLD_STATUS_BUSY);
  128. return 0;
  129. }
  130. static int sead3_wait_lcd_idle(struct img_ascii_lcd_ctx *ctx)
  131. {
  132. unsigned int cpld_data;
  133. int err;
  134. err = sead3_wait_sm_idle(ctx);
  135. if (err)
  136. return err;
  137. do {
  138. err = regmap_read(ctx->regmap,
  139. ctx->offset + SEAD3_REG_LCD_CTRL,
  140. &cpld_data);
  141. if (err)
  142. return err;
  143. err = sead3_wait_sm_idle(ctx);
  144. if (err)
  145. return err;
  146. err = regmap_read(ctx->regmap,
  147. ctx->offset + SEAD3_REG_CPLD_DATA,
  148. &cpld_data);
  149. if (err)
  150. return err;
  151. } while (cpld_data & SEAD3_REG_CPLD_DATA_BUSY);
  152. return 0;
  153. }
  154. static void sead3_update(struct img_ascii_lcd_ctx *ctx)
  155. {
  156. unsigned int i;
  157. int err = 0;
  158. for (i = 0; i < ctx->cfg->num_chars; i++) {
  159. err = sead3_wait_lcd_idle(ctx);
  160. if (err)
  161. break;
  162. err = regmap_write(ctx->regmap,
  163. ctx->offset + SEAD3_REG_LCD_CTRL,
  164. SEAD3_REG_LCD_CTRL_SETDRAM | i);
  165. if (err)
  166. break;
  167. err = sead3_wait_lcd_idle(ctx);
  168. if (err)
  169. break;
  170. err = regmap_write(ctx->regmap,
  171. ctx->offset + SEAD3_REG_LCD_DATA,
  172. ctx->curr[i]);
  173. if (err)
  174. break;
  175. }
  176. if (unlikely(err))
  177. pr_err_ratelimited("Failed to update LCD display: %d\n", err);
  178. }
  179. static struct img_ascii_lcd_config sead3_config = {
  180. .num_chars = 16,
  181. .external_regmap = true,
  182. .update = sead3_update,
  183. };
  184. static const struct of_device_id img_ascii_lcd_matches[] = {
  185. { .compatible = "img,boston-lcd", .data = &boston_config },
  186. { .compatible = "mti,malta-lcd", .data = &malta_config },
  187. { .compatible = "mti,sead3-lcd", .data = &sead3_config },
  188. { /* sentinel */ }
  189. };
  190. MODULE_DEVICE_TABLE(of, img_ascii_lcd_matches);
  191. /**
  192. * img_ascii_lcd_scroll() - scroll the display by a character
  193. * @t: really a pointer to the private data structure
  194. *
  195. * Scroll the current message along the LCD by one character, rearming the
  196. * timer if required.
  197. */
  198. static void img_ascii_lcd_scroll(struct timer_list *t)
  199. {
  200. struct img_ascii_lcd_ctx *ctx = from_timer(ctx, t, timer);
  201. unsigned int i, ch = ctx->scroll_pos;
  202. unsigned int num_chars = ctx->cfg->num_chars;
  203. /* update the current message string */
  204. for (i = 0; i < num_chars;) {
  205. /* copy as many characters from the string as possible */
  206. for (; i < num_chars && ch < ctx->message_len; i++, ch++)
  207. ctx->curr[i] = ctx->message[ch];
  208. /* wrap around to the start of the string */
  209. ch = 0;
  210. }
  211. /* update the LCD */
  212. ctx->cfg->update(ctx);
  213. /* move on to the next character */
  214. ctx->scroll_pos++;
  215. ctx->scroll_pos %= ctx->message_len;
  216. /* rearm the timer */
  217. if (ctx->message_len > ctx->cfg->num_chars)
  218. mod_timer(&ctx->timer, jiffies + ctx->scroll_rate);
  219. }
  220. /**
  221. * img_ascii_lcd_display() - set the message to be displayed
  222. * @ctx: pointer to the private data structure
  223. * @msg: the message to display
  224. * @count: length of msg, or -1
  225. *
  226. * Display a new message @msg on the LCD. @msg can be longer than the number of
  227. * characters the LCD can display, in which case it will begin scrolling across
  228. * the LCD display.
  229. *
  230. * Return: 0 on success, -ENOMEM on memory allocation failure
  231. */
  232. static int img_ascii_lcd_display(struct img_ascii_lcd_ctx *ctx,
  233. const char *msg, ssize_t count)
  234. {
  235. char *new_msg;
  236. /* stop the scroll timer */
  237. del_timer_sync(&ctx->timer);
  238. if (count == -1)
  239. count = strlen(msg);
  240. /* if the string ends with a newline, trim it */
  241. if (msg[count - 1] == '\n')
  242. count--;
  243. new_msg = devm_kmalloc(&ctx->pdev->dev, count + 1, GFP_KERNEL);
  244. if (!new_msg)
  245. return -ENOMEM;
  246. memcpy(new_msg, msg, count);
  247. new_msg[count] = 0;
  248. if (ctx->message)
  249. devm_kfree(&ctx->pdev->dev, ctx->message);
  250. ctx->message = new_msg;
  251. ctx->message_len = count;
  252. ctx->scroll_pos = 0;
  253. /* update the LCD */
  254. img_ascii_lcd_scroll(&ctx->timer);
  255. return 0;
  256. }
  257. /**
  258. * message_show() - read message via sysfs
  259. * @dev: the LCD device
  260. * @attr: the LCD message attribute
  261. * @buf: the buffer to read the message into
  262. *
  263. * Read the current message being displayed or scrolled across the LCD display
  264. * into @buf, for reads from sysfs.
  265. *
  266. * Return: the number of characters written to @buf
  267. */
  268. static ssize_t message_show(struct device *dev, struct device_attribute *attr,
  269. char *buf)
  270. {
  271. struct img_ascii_lcd_ctx *ctx = dev_get_drvdata(dev);
  272. return sprintf(buf, "%s\n", ctx->message);
  273. }
  274. /**
  275. * message_store() - write a new message via sysfs
  276. * @dev: the LCD device
  277. * @attr: the LCD message attribute
  278. * @buf: the buffer containing the new message
  279. * @count: the size of the message in @buf
  280. *
  281. * Write a new message to display or scroll across the LCD display from sysfs.
  282. *
  283. * Return: the size of the message on success, else -ERRNO
  284. */
  285. static ssize_t message_store(struct device *dev, struct device_attribute *attr,
  286. const char *buf, size_t count)
  287. {
  288. struct img_ascii_lcd_ctx *ctx = dev_get_drvdata(dev);
  289. int err;
  290. err = img_ascii_lcd_display(ctx, buf, count);
  291. return err ?: count;
  292. }
  293. static DEVICE_ATTR_RW(message);
  294. /**
  295. * img_ascii_lcd_probe() - probe an LCD display device
  296. * @pdev: the LCD platform device
  297. *
  298. * Probe an LCD display device, ensuring that we have the required resources in
  299. * order to access the LCD & setting up private data as well as sysfs files.
  300. *
  301. * Return: 0 on success, else -ERRNO
  302. */
  303. static int img_ascii_lcd_probe(struct platform_device *pdev)
  304. {
  305. const struct of_device_id *match;
  306. const struct img_ascii_lcd_config *cfg;
  307. struct img_ascii_lcd_ctx *ctx;
  308. struct resource *res;
  309. int err;
  310. match = of_match_device(img_ascii_lcd_matches, &pdev->dev);
  311. if (!match)
  312. return -ENODEV;
  313. cfg = match->data;
  314. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx) + cfg->num_chars,
  315. GFP_KERNEL);
  316. if (!ctx)
  317. return -ENOMEM;
  318. if (cfg->external_regmap) {
  319. ctx->regmap = syscon_node_to_regmap(pdev->dev.parent->of_node);
  320. if (IS_ERR(ctx->regmap))
  321. return PTR_ERR(ctx->regmap);
  322. if (of_property_read_u32(pdev->dev.of_node, "offset",
  323. &ctx->offset))
  324. return -EINVAL;
  325. } else {
  326. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  327. ctx->base = devm_ioremap_resource(&pdev->dev, res);
  328. if (IS_ERR(ctx->base))
  329. return PTR_ERR(ctx->base);
  330. }
  331. ctx->pdev = pdev;
  332. ctx->cfg = cfg;
  333. ctx->message = NULL;
  334. ctx->scroll_pos = 0;
  335. ctx->scroll_rate = HZ / 2;
  336. /* initialise a timer for scrolling the message */
  337. timer_setup(&ctx->timer, img_ascii_lcd_scroll, 0);
  338. platform_set_drvdata(pdev, ctx);
  339. /* display a default message */
  340. err = img_ascii_lcd_display(ctx, "Linux " UTS_RELEASE " ", -1);
  341. if (err)
  342. goto out_del_timer;
  343. err = device_create_file(&pdev->dev, &dev_attr_message);
  344. if (err)
  345. goto out_del_timer;
  346. return 0;
  347. out_del_timer:
  348. del_timer_sync(&ctx->timer);
  349. return err;
  350. }
  351. /**
  352. * img_ascii_lcd_remove() - remove an LCD display device
  353. * @pdev: the LCD platform device
  354. *
  355. * Remove an LCD display device, freeing private resources & ensuring that the
  356. * driver stops using the LCD display registers.
  357. *
  358. * Return: 0
  359. */
  360. static int img_ascii_lcd_remove(struct platform_device *pdev)
  361. {
  362. struct img_ascii_lcd_ctx *ctx = platform_get_drvdata(pdev);
  363. device_remove_file(&pdev->dev, &dev_attr_message);
  364. del_timer_sync(&ctx->timer);
  365. return 0;
  366. }
  367. static struct platform_driver img_ascii_lcd_driver = {
  368. .driver = {
  369. .name = "img-ascii-lcd",
  370. .of_match_table = img_ascii_lcd_matches,
  371. },
  372. .probe = img_ascii_lcd_probe,
  373. .remove = img_ascii_lcd_remove,
  374. };
  375. module_platform_driver(img_ascii_lcd_driver);
  376. MODULE_DESCRIPTION("Imagination Technologies ASCII LCD Display");
  377. MODULE_AUTHOR("Paul Burton <paul.burton@mips.com>");
  378. MODULE_LICENSE("GPL");