img-ascii-lcd.c 11 KB

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