img-ascii-lcd.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 Imagination Technologies
  4. * Author: Paul Burton <[email protected]>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/io.h>
  8. #include <linux/mfd/syscon.h>
  9. #include <linux/module.h>
  10. #include <linux/of_address.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <linux/slab.h>
  15. #include "line-display.h"
  16. struct img_ascii_lcd_ctx;
  17. /**
  18. * struct img_ascii_lcd_config - Configuration information about an LCD model
  19. * @num_chars: the number of characters the LCD can display
  20. * @external_regmap: true if registers are in a system controller, else false
  21. * @update: function called to update the LCD
  22. */
  23. struct img_ascii_lcd_config {
  24. unsigned int num_chars;
  25. bool external_regmap;
  26. void (*update)(struct linedisp *linedisp);
  27. };
  28. /**
  29. * struct img_ascii_lcd_ctx - Private data structure
  30. * @base: the base address of the LCD registers
  31. * @regmap: the regmap through which LCD registers are accessed
  32. * @offset: the offset within regmap to the start of the LCD registers
  33. * @cfg: pointer to the LCD model configuration
  34. * @linedisp: line display structure
  35. * @curr: the string currently displayed on the LCD
  36. */
  37. struct img_ascii_lcd_ctx {
  38. union {
  39. void __iomem *base;
  40. struct regmap *regmap;
  41. };
  42. u32 offset;
  43. const struct img_ascii_lcd_config *cfg;
  44. struct linedisp linedisp;
  45. char curr[] __aligned(8);
  46. };
  47. /*
  48. * MIPS Boston development board
  49. */
  50. static void boston_update(struct linedisp *linedisp)
  51. {
  52. struct img_ascii_lcd_ctx *ctx =
  53. container_of(linedisp, struct img_ascii_lcd_ctx, linedisp);
  54. ulong val;
  55. #if BITS_PER_LONG == 64
  56. val = *((u64 *)&ctx->curr[0]);
  57. __raw_writeq(val, ctx->base);
  58. #elif BITS_PER_LONG == 32
  59. val = *((u32 *)&ctx->curr[0]);
  60. __raw_writel(val, ctx->base);
  61. val = *((u32 *)&ctx->curr[4]);
  62. __raw_writel(val, ctx->base + 4);
  63. #else
  64. # error Not 32 or 64 bit
  65. #endif
  66. }
  67. static struct img_ascii_lcd_config boston_config = {
  68. .num_chars = 8,
  69. .update = boston_update,
  70. };
  71. /*
  72. * MIPS Malta development board
  73. */
  74. static void malta_update(struct linedisp *linedisp)
  75. {
  76. struct img_ascii_lcd_ctx *ctx =
  77. container_of(linedisp, struct img_ascii_lcd_ctx, linedisp);
  78. unsigned int i;
  79. int err = 0;
  80. for (i = 0; i < linedisp->num_chars; i++) {
  81. err = regmap_write(ctx->regmap,
  82. ctx->offset + (i * 8), ctx->curr[i]);
  83. if (err)
  84. break;
  85. }
  86. if (unlikely(err))
  87. pr_err_ratelimited("Failed to update LCD display: %d\n", err);
  88. }
  89. static struct img_ascii_lcd_config malta_config = {
  90. .num_chars = 8,
  91. .external_regmap = true,
  92. .update = malta_update,
  93. };
  94. /*
  95. * MIPS SEAD3 development board
  96. */
  97. enum {
  98. SEAD3_REG_LCD_CTRL = 0x00,
  99. #define SEAD3_REG_LCD_CTRL_SETDRAM BIT(7)
  100. SEAD3_REG_LCD_DATA = 0x08,
  101. SEAD3_REG_CPLD_STATUS = 0x10,
  102. #define SEAD3_REG_CPLD_STATUS_BUSY BIT(0)
  103. SEAD3_REG_CPLD_DATA = 0x18,
  104. #define SEAD3_REG_CPLD_DATA_BUSY BIT(7)
  105. };
  106. static int sead3_wait_sm_idle(struct img_ascii_lcd_ctx *ctx)
  107. {
  108. unsigned int status;
  109. int err;
  110. do {
  111. err = regmap_read(ctx->regmap,
  112. ctx->offset + SEAD3_REG_CPLD_STATUS,
  113. &status);
  114. if (err)
  115. return err;
  116. } while (status & SEAD3_REG_CPLD_STATUS_BUSY);
  117. return 0;
  118. }
  119. static int sead3_wait_lcd_idle(struct img_ascii_lcd_ctx *ctx)
  120. {
  121. unsigned int cpld_data;
  122. int err;
  123. err = sead3_wait_sm_idle(ctx);
  124. if (err)
  125. return err;
  126. do {
  127. err = regmap_read(ctx->regmap,
  128. ctx->offset + SEAD3_REG_LCD_CTRL,
  129. &cpld_data);
  130. if (err)
  131. return err;
  132. err = sead3_wait_sm_idle(ctx);
  133. if (err)
  134. return err;
  135. err = regmap_read(ctx->regmap,
  136. ctx->offset + SEAD3_REG_CPLD_DATA,
  137. &cpld_data);
  138. if (err)
  139. return err;
  140. } while (cpld_data & SEAD3_REG_CPLD_DATA_BUSY);
  141. return 0;
  142. }
  143. static void sead3_update(struct linedisp *linedisp)
  144. {
  145. struct img_ascii_lcd_ctx *ctx =
  146. container_of(linedisp, struct img_ascii_lcd_ctx, linedisp);
  147. unsigned int i;
  148. int err = 0;
  149. for (i = 0; i < linedisp->num_chars; i++) {
  150. err = sead3_wait_lcd_idle(ctx);
  151. if (err)
  152. break;
  153. err = regmap_write(ctx->regmap,
  154. ctx->offset + SEAD3_REG_LCD_CTRL,
  155. SEAD3_REG_LCD_CTRL_SETDRAM | i);
  156. if (err)
  157. break;
  158. err = sead3_wait_lcd_idle(ctx);
  159. if (err)
  160. break;
  161. err = regmap_write(ctx->regmap,
  162. ctx->offset + SEAD3_REG_LCD_DATA,
  163. ctx->curr[i]);
  164. if (err)
  165. break;
  166. }
  167. if (unlikely(err))
  168. pr_err_ratelimited("Failed to update LCD display: %d\n", err);
  169. }
  170. static struct img_ascii_lcd_config sead3_config = {
  171. .num_chars = 16,
  172. .external_regmap = true,
  173. .update = sead3_update,
  174. };
  175. static const struct of_device_id img_ascii_lcd_matches[] = {
  176. { .compatible = "img,boston-lcd", .data = &boston_config },
  177. { .compatible = "mti,malta-lcd", .data = &malta_config },
  178. { .compatible = "mti,sead3-lcd", .data = &sead3_config },
  179. { /* sentinel */ }
  180. };
  181. MODULE_DEVICE_TABLE(of, img_ascii_lcd_matches);
  182. /**
  183. * img_ascii_lcd_probe() - probe an LCD display device
  184. * @pdev: the LCD platform device
  185. *
  186. * Probe an LCD display device, ensuring that we have the required resources in
  187. * order to access the LCD & setting up private data as well as sysfs files.
  188. *
  189. * Return: 0 on success, else -ERRNO
  190. */
  191. static int img_ascii_lcd_probe(struct platform_device *pdev)
  192. {
  193. const struct of_device_id *match;
  194. const struct img_ascii_lcd_config *cfg;
  195. struct device *dev = &pdev->dev;
  196. struct img_ascii_lcd_ctx *ctx;
  197. int err;
  198. match = of_match_device(img_ascii_lcd_matches, dev);
  199. if (!match)
  200. return -ENODEV;
  201. cfg = match->data;
  202. ctx = devm_kzalloc(dev, sizeof(*ctx) + cfg->num_chars, GFP_KERNEL);
  203. if (!ctx)
  204. return -ENOMEM;
  205. if (cfg->external_regmap) {
  206. ctx->regmap = syscon_node_to_regmap(dev->parent->of_node);
  207. if (IS_ERR(ctx->regmap))
  208. return PTR_ERR(ctx->regmap);
  209. if (of_property_read_u32(dev->of_node, "offset", &ctx->offset))
  210. return -EINVAL;
  211. } else {
  212. ctx->base = devm_platform_ioremap_resource(pdev, 0);
  213. if (IS_ERR(ctx->base))
  214. return PTR_ERR(ctx->base);
  215. }
  216. err = linedisp_register(&ctx->linedisp, dev, cfg->num_chars, ctx->curr,
  217. cfg->update);
  218. if (err)
  219. return err;
  220. /* for backwards compatibility */
  221. err = compat_only_sysfs_link_entry_to_kobj(&dev->kobj,
  222. &ctx->linedisp.dev.kobj,
  223. "message", NULL);
  224. if (err)
  225. goto err_unregister;
  226. platform_set_drvdata(pdev, ctx);
  227. return 0;
  228. err_unregister:
  229. linedisp_unregister(&ctx->linedisp);
  230. return err;
  231. }
  232. /**
  233. * img_ascii_lcd_remove() - remove an LCD display device
  234. * @pdev: the LCD platform device
  235. *
  236. * Remove an LCD display device, freeing private resources & ensuring that the
  237. * driver stops using the LCD display registers.
  238. *
  239. * Return: 0
  240. */
  241. static int img_ascii_lcd_remove(struct platform_device *pdev)
  242. {
  243. struct img_ascii_lcd_ctx *ctx = platform_get_drvdata(pdev);
  244. sysfs_remove_link(&pdev->dev.kobj, "message");
  245. linedisp_unregister(&ctx->linedisp);
  246. return 0;
  247. }
  248. static struct platform_driver img_ascii_lcd_driver = {
  249. .driver = {
  250. .name = "img-ascii-lcd",
  251. .of_match_table = img_ascii_lcd_matches,
  252. },
  253. .probe = img_ascii_lcd_probe,
  254. .remove = img_ascii_lcd_remove,
  255. };
  256. module_platform_driver(img_ascii_lcd_driver);
  257. MODULE_DESCRIPTION("Imagination Technologies ASCII LCD Display");
  258. MODULE_AUTHOR("Paul Burton <[email protected]>");
  259. MODULE_LICENSE("GPL");