img-ascii-lcd.c 6.8 KB

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