panel-innolux-ej030na.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Innolux/Chimei EJ030NA TFT LCD panel driver
  4. *
  5. * Copyright (C) 2020, Paul Cercueil <[email protected]>
  6. * Copyright (C) 2020, Christophe Branchereau <[email protected]>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/media-bus-format.h>
  12. #include <linux/module.h>
  13. #include <linux/of_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/spi/spi.h>
  17. #include <drm/drm_modes.h>
  18. #include <drm/drm_panel.h>
  19. struct ej030na_info {
  20. const struct drm_display_mode *display_modes;
  21. unsigned int num_modes;
  22. u16 width_mm, height_mm;
  23. u32 bus_format, bus_flags;
  24. };
  25. struct ej030na {
  26. struct drm_panel panel;
  27. struct spi_device *spi;
  28. struct regmap *map;
  29. const struct ej030na_info *panel_info;
  30. struct regulator *supply;
  31. struct gpio_desc *reset_gpio;
  32. };
  33. static inline struct ej030na *to_ej030na(struct drm_panel *panel)
  34. {
  35. return container_of(panel, struct ej030na, panel);
  36. }
  37. static const struct reg_sequence ej030na_init_sequence[] = {
  38. { 0x05, 0x1e },
  39. { 0x05, 0x5c },
  40. { 0x02, 0x14 },
  41. { 0x03, 0x40 },
  42. { 0x04, 0x07 },
  43. { 0x06, 0x12 },
  44. { 0x07, 0xd2 },
  45. { 0x0c, 0x06 },
  46. { 0x0d, 0x40 },
  47. { 0x0e, 0x40 },
  48. { 0x0f, 0x40 },
  49. { 0x10, 0x40 },
  50. { 0x11, 0x40 },
  51. { 0x2f, 0x40 },
  52. { 0x5a, 0x02 },
  53. { 0x30, 0x07 },
  54. { 0x31, 0x57 },
  55. { 0x32, 0x53 },
  56. { 0x33, 0x77 },
  57. { 0x34, 0xb8 },
  58. { 0x35, 0xbd },
  59. { 0x36, 0xb8 },
  60. { 0x37, 0xe7 },
  61. { 0x38, 0x04 },
  62. { 0x39, 0xff },
  63. { 0x40, 0x0b },
  64. { 0x41, 0xb8 },
  65. { 0x42, 0xab },
  66. { 0x43, 0xb9 },
  67. { 0x44, 0x6a },
  68. { 0x45, 0x56 },
  69. { 0x46, 0x61 },
  70. { 0x47, 0x08 },
  71. { 0x48, 0x0f },
  72. { 0x49, 0x0f },
  73. };
  74. static int ej030na_prepare(struct drm_panel *panel)
  75. {
  76. struct ej030na *priv = to_ej030na(panel);
  77. struct device *dev = &priv->spi->dev;
  78. int err;
  79. err = regulator_enable(priv->supply);
  80. if (err) {
  81. dev_err(dev, "Failed to enable power supply: %d\n", err);
  82. return err;
  83. }
  84. /* Reset the chip */
  85. gpiod_set_value_cansleep(priv->reset_gpio, 1);
  86. usleep_range(50, 150);
  87. gpiod_set_value_cansleep(priv->reset_gpio, 0);
  88. usleep_range(50, 150);
  89. err = regmap_multi_reg_write(priv->map, ej030na_init_sequence,
  90. ARRAY_SIZE(ej030na_init_sequence));
  91. if (err) {
  92. dev_err(dev, "Failed to init registers: %d\n", err);
  93. goto err_disable_regulator;
  94. }
  95. return 0;
  96. err_disable_regulator:
  97. regulator_disable(priv->supply);
  98. return err;
  99. }
  100. static int ej030na_unprepare(struct drm_panel *panel)
  101. {
  102. struct ej030na *priv = to_ej030na(panel);
  103. gpiod_set_value_cansleep(priv->reset_gpio, 1);
  104. regulator_disable(priv->supply);
  105. return 0;
  106. }
  107. static int ej030na_enable(struct drm_panel *panel)
  108. {
  109. struct ej030na *priv = to_ej030na(panel);
  110. /* standby off */
  111. regmap_write(priv->map, 0x2b, 0x01);
  112. if (panel->backlight) {
  113. /* Wait for the picture to be ready before enabling backlight */
  114. msleep(120);
  115. }
  116. return 0;
  117. }
  118. static int ej030na_disable(struct drm_panel *panel)
  119. {
  120. struct ej030na *priv = to_ej030na(panel);
  121. /* standby on */
  122. regmap_write(priv->map, 0x2b, 0x00);
  123. return 0;
  124. }
  125. static int ej030na_get_modes(struct drm_panel *panel,
  126. struct drm_connector *connector)
  127. {
  128. struct ej030na *priv = to_ej030na(panel);
  129. const struct ej030na_info *panel_info = priv->panel_info;
  130. struct drm_display_mode *mode;
  131. unsigned int i;
  132. for (i = 0; i < panel_info->num_modes; i++) {
  133. mode = drm_mode_duplicate(connector->dev,
  134. &panel_info->display_modes[i]);
  135. if (!mode)
  136. return -ENOMEM;
  137. drm_mode_set_name(mode);
  138. mode->type = DRM_MODE_TYPE_DRIVER;
  139. if (panel_info->num_modes == 1)
  140. mode->type |= DRM_MODE_TYPE_PREFERRED;
  141. drm_mode_probed_add(connector, mode);
  142. }
  143. connector->display_info.bpc = 8;
  144. connector->display_info.width_mm = panel_info->width_mm;
  145. connector->display_info.height_mm = panel_info->height_mm;
  146. drm_display_info_set_bus_formats(&connector->display_info,
  147. &panel_info->bus_format, 1);
  148. connector->display_info.bus_flags = panel_info->bus_flags;
  149. return panel_info->num_modes;
  150. }
  151. static const struct drm_panel_funcs ej030na_funcs = {
  152. .prepare = ej030na_prepare,
  153. .unprepare = ej030na_unprepare,
  154. .enable = ej030na_enable,
  155. .disable = ej030na_disable,
  156. .get_modes = ej030na_get_modes,
  157. };
  158. static const struct regmap_config ej030na_regmap_config = {
  159. .reg_bits = 8,
  160. .val_bits = 8,
  161. .max_register = 0x5a,
  162. };
  163. static int ej030na_probe(struct spi_device *spi)
  164. {
  165. struct device *dev = &spi->dev;
  166. struct ej030na *priv;
  167. int err;
  168. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  169. if (!priv)
  170. return -ENOMEM;
  171. priv->spi = spi;
  172. spi_set_drvdata(spi, priv);
  173. priv->map = devm_regmap_init_spi(spi, &ej030na_regmap_config);
  174. if (IS_ERR(priv->map)) {
  175. dev_err(dev, "Unable to init regmap\n");
  176. return PTR_ERR(priv->map);
  177. }
  178. priv->panel_info = of_device_get_match_data(dev);
  179. if (!priv->panel_info)
  180. return -EINVAL;
  181. priv->supply = devm_regulator_get(dev, "power");
  182. if (IS_ERR(priv->supply))
  183. return dev_err_probe(dev, PTR_ERR(priv->supply),
  184. "Failed to get power supply\n");
  185. priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  186. if (IS_ERR(priv->reset_gpio))
  187. return dev_err_probe(dev, PTR_ERR(priv->reset_gpio),
  188. "Failed to get reset GPIO\n");
  189. drm_panel_init(&priv->panel, dev, &ej030na_funcs,
  190. DRM_MODE_CONNECTOR_DPI);
  191. err = drm_panel_of_backlight(&priv->panel);
  192. if (err)
  193. return err;
  194. drm_panel_add(&priv->panel);
  195. return 0;
  196. }
  197. static void ej030na_remove(struct spi_device *spi)
  198. {
  199. struct ej030na *priv = spi_get_drvdata(spi);
  200. drm_panel_remove(&priv->panel);
  201. drm_panel_disable(&priv->panel);
  202. drm_panel_unprepare(&priv->panel);
  203. }
  204. static const struct drm_display_mode ej030na_modes[] = {
  205. { /* 60 Hz */
  206. .clock = 14400,
  207. .hdisplay = 320,
  208. .hsync_start = 320 + 10,
  209. .hsync_end = 320 + 10 + 37,
  210. .htotal = 320 + 10 + 37 + 33,
  211. .vdisplay = 480,
  212. .vsync_start = 480 + 102,
  213. .vsync_end = 480 + 102 + 9 + 9,
  214. .vtotal = 480 + 102 + 9 + 9,
  215. .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
  216. },
  217. { /* 50 Hz */
  218. .clock = 12000,
  219. .hdisplay = 320,
  220. .hsync_start = 320 + 10,
  221. .hsync_end = 320 + 10 + 37,
  222. .htotal = 320 + 10 + 37 + 33,
  223. .vdisplay = 480,
  224. .vsync_start = 480 + 102,
  225. .vsync_end = 480 + 102 + 9,
  226. .vtotal = 480 + 102 + 9 + 9,
  227. .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
  228. },
  229. };
  230. static const struct ej030na_info ej030na_info = {
  231. .display_modes = ej030na_modes,
  232. .num_modes = ARRAY_SIZE(ej030na_modes),
  233. .width_mm = 70,
  234. .height_mm = 51,
  235. .bus_format = MEDIA_BUS_FMT_RGB888_3X8_DELTA,
  236. .bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE | DRM_BUS_FLAG_DE_LOW,
  237. };
  238. static const struct of_device_id ej030na_of_match[] = {
  239. { .compatible = "innolux,ej030na", .data = &ej030na_info },
  240. { /* sentinel */ }
  241. };
  242. MODULE_DEVICE_TABLE(of, ej030na_of_match);
  243. static struct spi_driver ej030na_driver = {
  244. .driver = {
  245. .name = "panel-innolux-ej030na",
  246. .of_match_table = ej030na_of_match,
  247. },
  248. .probe = ej030na_probe,
  249. .remove = ej030na_remove,
  250. };
  251. module_spi_driver(ej030na_driver);
  252. MODULE_AUTHOR("Paul Cercueil <[email protected]>");
  253. MODULE_AUTHOR("Christophe Branchereau <[email protected]>");
  254. MODULE_LICENSE("GPL v2");