img-ascii-lcd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright (C) 2016 Imagination Technologies
  3. * Author: Paul Burton <paul.burton@imgtec.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;
  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;
  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. /**
  191. * img_ascii_lcd_scroll() - scroll the display by a character
  192. * @arg: really a pointer to the private data structure
  193. *
  194. * Scroll the current message along the LCD by one character, rearming the
  195. * timer if required.
  196. */
  197. static void img_ascii_lcd_scroll(unsigned long arg)
  198. {
  199. struct img_ascii_lcd_ctx *ctx = (struct img_ascii_lcd_ctx *)arg;
  200. unsigned int i, ch = ctx->scroll_pos;
  201. unsigned int num_chars = ctx->cfg->num_chars;
  202. /* update the current message string */
  203. for (i = 0; i < num_chars;) {
  204. /* copy as many characters from the string as possible */
  205. for (; i < num_chars && ch < ctx->message_len; i++, ch++)
  206. ctx->curr[i] = ctx->message[ch];
  207. /* wrap around to the start of the string */
  208. ch = 0;
  209. }
  210. /* update the LCD */
  211. ctx->cfg->update(ctx);
  212. /* move on to the next character */
  213. ctx->scroll_pos++;
  214. ctx->scroll_pos %= ctx->message_len;
  215. /* rearm the timer */
  216. if (ctx->message_len > ctx->cfg->num_chars)
  217. mod_timer(&ctx->timer, jiffies + ctx->scroll_rate);
  218. }
  219. /**
  220. * img_ascii_lcd_display() - set the message to be displayed
  221. * @ctx: pointer to the private data structure
  222. * @msg: the message to display
  223. * @count: length of msg, or -1
  224. *
  225. * Display a new message @msg on the LCD. @msg can be longer than the number of
  226. * characters the LCD can display, in which case it will begin scrolling across
  227. * the LCD display.
  228. *
  229. * Return: 0 on success, -ENOMEM on memory allocation failure
  230. */
  231. static int img_ascii_lcd_display(struct img_ascii_lcd_ctx *ctx,
  232. const char *msg, ssize_t count)
  233. {
  234. char *new_msg;
  235. /* stop the scroll timer */
  236. del_timer_sync(&ctx->timer);
  237. if (count == -1)
  238. count = strlen(msg);
  239. /* if the string ends with a newline, trim it */
  240. if (msg[count - 1] == '\n')
  241. count--;
  242. new_msg = devm_kmalloc(&ctx->pdev->dev, count + 1, GFP_KERNEL);
  243. if (!new_msg)
  244. return -ENOMEM;
  245. memcpy(new_msg, msg, count);
  246. new_msg[count] = 0;
  247. if (ctx->message)
  248. devm_kfree(&ctx->pdev->dev, ctx->message);
  249. ctx->message = new_msg;
  250. ctx->message_len = count;
  251. ctx->scroll_pos = 0;
  252. /* update the LCD */
  253. img_ascii_lcd_scroll((unsigned long)ctx);
  254. return 0;
  255. }
  256. /**
  257. * message_show() - read message via sysfs
  258. * @dev: the LCD device
  259. * @attr: the LCD message attribute
  260. * @buf: the buffer to read the message into
  261. *
  262. * Read the current message being displayed or scrolled across the LCD display
  263. * into @buf, for reads from sysfs.
  264. *
  265. * Return: the number of characters written to @buf
  266. */
  267. static ssize_t message_show(struct device *dev, struct device_attribute *attr,
  268. char *buf)
  269. {
  270. struct img_ascii_lcd_ctx *ctx = dev_get_drvdata(dev);
  271. return sprintf(buf, "%s\n", ctx->message);
  272. }
  273. /**
  274. * message_store() - write a new message via sysfs
  275. * @dev: the LCD device
  276. * @attr: the LCD message attribute
  277. * @buf: the buffer containing the new message
  278. * @count: the size of the message in @buf
  279. *
  280. * Write a new message to display or scroll across the LCD display from sysfs.
  281. *
  282. * Return: the size of the message on success, else -ERRNO
  283. */
  284. static ssize_t message_store(struct device *dev, struct device_attribute *attr,
  285. const char *buf, size_t count)
  286. {
  287. struct img_ascii_lcd_ctx *ctx = dev_get_drvdata(dev);
  288. int err;
  289. err = img_ascii_lcd_display(ctx, buf, count);
  290. return err ?: count;
  291. }
  292. static DEVICE_ATTR_RW(message);
  293. /**
  294. * img_ascii_lcd_probe() - probe an LCD display device
  295. * @pdev: the LCD platform device
  296. *
  297. * Probe an LCD display device, ensuring that we have the required resources in
  298. * order to access the LCD & setting up private data as well as sysfs files.
  299. *
  300. * Return: 0 on success, else -ERRNO
  301. */
  302. static int img_ascii_lcd_probe(struct platform_device *pdev)
  303. {
  304. const struct of_device_id *match;
  305. const struct img_ascii_lcd_config *cfg;
  306. struct img_ascii_lcd_ctx *ctx;
  307. struct resource *res;
  308. int err;
  309. match = of_match_device(img_ascii_lcd_matches, &pdev->dev);
  310. if (!match)
  311. return -ENODEV;
  312. cfg = match->data;
  313. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx) + cfg->num_chars,
  314. GFP_KERNEL);
  315. if (!ctx)
  316. return -ENOMEM;
  317. if (cfg->external_regmap) {
  318. ctx->regmap = syscon_node_to_regmap(pdev->dev.parent->of_node);
  319. if (IS_ERR(ctx->regmap))
  320. return PTR_ERR(ctx->regmap);
  321. if (of_property_read_u32(pdev->dev.of_node, "offset",
  322. &ctx->offset))
  323. return -EINVAL;
  324. } else {
  325. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  326. ctx->base = devm_ioremap_resource(&pdev->dev, res);
  327. if (IS_ERR(ctx->base))
  328. return PTR_ERR(ctx->base);
  329. }
  330. ctx->pdev = pdev;
  331. ctx->cfg = cfg;
  332. ctx->message = NULL;
  333. ctx->scroll_pos = 0;
  334. ctx->scroll_rate = HZ / 2;
  335. /* initialise a timer for scrolling the message */
  336. init_timer(&ctx->timer);
  337. ctx->timer.function = img_ascii_lcd_scroll;
  338. ctx->timer.data = (unsigned long)ctx;
  339. platform_set_drvdata(pdev, ctx);
  340. /* display a default message */
  341. err = img_ascii_lcd_display(ctx, "Linux " UTS_RELEASE " ", -1);
  342. if (err)
  343. goto out_del_timer;
  344. err = device_create_file(&pdev->dev, &dev_attr_message);
  345. if (err)
  346. goto out_del_timer;
  347. return 0;
  348. out_del_timer:
  349. del_timer_sync(&ctx->timer);
  350. return err;
  351. }
  352. /**
  353. * img_ascii_lcd_remove() - remove an LCD display device
  354. * @pdev: the LCD platform device
  355. *
  356. * Remove an LCD display device, freeing private resources & ensuring that the
  357. * driver stops using the LCD display registers.
  358. *
  359. * Return: 0
  360. */
  361. static int img_ascii_lcd_remove(struct platform_device *pdev)
  362. {
  363. struct img_ascii_lcd_ctx *ctx = platform_get_drvdata(pdev);
  364. device_remove_file(&pdev->dev, &dev_attr_message);
  365. del_timer_sync(&ctx->timer);
  366. return 0;
  367. }
  368. static struct platform_driver img_ascii_lcd_driver = {
  369. .driver = {
  370. .name = "img-ascii-lcd",
  371. .of_match_table = img_ascii_lcd_matches,
  372. },
  373. .probe = img_ascii_lcd_probe,
  374. .remove = img_ascii_lcd_remove,
  375. };
  376. module_platform_driver(img_ascii_lcd_driver);
  377. MODULE_DESCRIPTION("Imagination Technologies ASCII LCD Display");
  378. MODULE_AUTHOR("Paul Burton <paul.burton@mips.com>");
  379. MODULE_LICENSE("GPL");