panel-innolux-ej030na.c 7.1 KB

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