img-ascii-lcd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. new_msg = devm_kmalloc(&ctx->pdev->dev, count + 1, GFP_KERNEL);
  240. if (!new_msg)
  241. return -ENOMEM;
  242. memcpy(new_msg, msg, count);
  243. new_msg[count] = 0;
  244. if (ctx->message)
  245. devm_kfree(&ctx->pdev->dev, ctx->message);
  246. ctx->message = new_msg;
  247. ctx->message_len = count;
  248. ctx->scroll_pos = 0;
  249. /* update the LCD */
  250. img_ascii_lcd_scroll(&ctx->timer);
  251. return 0;
  252. }
  253. /**
  254. * message_show() - read message via sysfs
  255. * @dev: the LCD device
  256. * @attr: the LCD message attribute
  257. * @buf: the buffer to read the message into
  258. *
  259. * Read the current message being displayed or scrolled across the LCD display
  260. * into @buf, for reads from sysfs.
  261. *
  262. * Return: the number of characters written to @buf
  263. */
  264. static ssize_t message_show(struct device *dev, struct device_attribute *attr,
  265. char *buf)
  266. {
  267. struct img_ascii_lcd_ctx *ctx = dev_get_drvdata(dev);
  268. return sprintf(buf, "%s\n", ctx->message);
  269. }
  270. /**
  271. * message_store() - write a new message via sysfs
  272. * @dev: the LCD device
  273. * @attr: the LCD message attribute
  274. * @buf: the buffer containing the new message
  275. * @count: the size of the message in @buf
  276. *
  277. * Write a new message to display or scroll across the LCD display from sysfs.
  278. *
  279. * Return: the size of the message on success, else -ERRNO
  280. */
  281. static ssize_t message_store(struct device *dev, struct device_attribute *attr,
  282. const char *buf, size_t count)
  283. {
  284. struct img_ascii_lcd_ctx *ctx = dev_get_drvdata(dev);
  285. int err;
  286. err = img_ascii_lcd_display(ctx, buf, count);
  287. return err ?: count;
  288. }
  289. static DEVICE_ATTR_RW(message);
  290. /**
  291. * img_ascii_lcd_probe() - probe an LCD display device
  292. * @pdev: the LCD platform device
  293. *
  294. * Probe an LCD display device, ensuring that we have the required resources in
  295. * order to access the LCD & setting up private data as well as sysfs files.
  296. *
  297. * Return: 0 on success, else -ERRNO
  298. */
  299. static int img_ascii_lcd_probe(struct platform_device *pdev)
  300. {
  301. const struct of_device_id *match;
  302. const struct img_ascii_lcd_config *cfg;
  303. struct img_ascii_lcd_ctx *ctx;
  304. struct resource *res;
  305. int err;
  306. match = of_match_device(img_ascii_lcd_matches, &pdev->dev);
  307. if (!match)
  308. return -ENODEV;
  309. cfg = match->data;
  310. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx) + cfg->num_chars,
  311. GFP_KERNEL);
  312. if (!ctx)
  313. return -ENOMEM;
  314. if (cfg->external_regmap) {
  315. ctx->regmap = syscon_node_to_regmap(pdev->dev.parent->of_node);
  316. if (IS_ERR(ctx->regmap))
  317. return PTR_ERR(ctx->regmap);
  318. if (of_property_read_u32(pdev->dev.of_node, "offset",
  319. &ctx->offset))
  320. return -EINVAL;
  321. } else {
  322. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  323. ctx->base = devm_ioremap_resource(&pdev->dev, res);
  324. if (IS_ERR(ctx->base))
  325. return PTR_ERR(ctx->base);
  326. }
  327. ctx->pdev = pdev;
  328. ctx->cfg = cfg;
  329. ctx->message = NULL;
  330. ctx->scroll_pos = 0;
  331. ctx->scroll_rate = HZ / 2;
  332. /* initialise a timer for scrolling the message */
  333. timer_setup(&ctx->timer, img_ascii_lcd_scroll, 0);
  334. platform_set_drvdata(pdev, ctx);
  335. /* display a default message */
  336. err = img_ascii_lcd_display(ctx, "Linux " UTS_RELEASE " ", -1);
  337. if (err)
  338. goto out_del_timer;
  339. err = device_create_file(&pdev->dev, &dev_attr_message);
  340. if (err)
  341. goto out_del_timer;
  342. return 0;
  343. out_del_timer:
  344. del_timer_sync(&ctx->timer);
  345. return err;
  346. }
  347. /**
  348. * img_ascii_lcd_remove() - remove an LCD display device
  349. * @pdev: the LCD platform device
  350. *
  351. * Remove an LCD display device, freeing private resources & ensuring that the
  352. * driver stops using the LCD display registers.
  353. *
  354. * Return: 0
  355. */
  356. static int img_ascii_lcd_remove(struct platform_device *pdev)
  357. {
  358. struct img_ascii_lcd_ctx *ctx = platform_get_drvdata(pdev);
  359. device_remove_file(&pdev->dev, &dev_attr_message);
  360. del_timer_sync(&ctx->timer);
  361. return 0;
  362. }
  363. static struct platform_driver img_ascii_lcd_driver = {
  364. .driver = {
  365. .name = "img-ascii-lcd",
  366. .of_match_table = img_ascii_lcd_matches,
  367. },
  368. .probe = img_ascii_lcd_probe,
  369. .remove = img_ascii_lcd_remove,
  370. };
  371. module_platform_driver(img_ascii_lcd_driver);
  372. MODULE_DESCRIPTION("Imagination Technologies ASCII LCD Display");
  373. MODULE_AUTHOR("Paul Burton <paul.burton@mips.com>");
  374. MODULE_LICENSE("GPL");