panel-orisetech-ota5601a.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Orisetech OTA5601A TFT LCD panel driver
  4. *
  5. * Copyright (C) 2021, Christophe Branchereau <cbranchereau@gmail.com>
  6. */
  7. #include <linux/bits.h>
  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/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. #define OTA5601A_CTL 0x01
  20. #define OTA5601A_CTL_OFF 0x00
  21. #define OTA5601A_CTL_ON BIT(0)
  22. struct ota5601a_panel_info {
  23. const struct drm_display_mode *display_modes;
  24. unsigned int num_modes;
  25. u16 width_mm, height_mm;
  26. u32 bus_format, bus_flags;
  27. };
  28. struct ota5601a {
  29. struct drm_panel drm_panel;
  30. struct regmap *map;
  31. struct regulator *supply;
  32. const struct ota5601a_panel_info *panel_info;
  33. struct gpio_desc *reset_gpio;
  34. };
  35. static inline struct ota5601a *to_ota5601a(struct drm_panel *panel)
  36. {
  37. return container_of(panel, struct ota5601a, drm_panel);
  38. }
  39. static const struct reg_sequence ota5601a_panel_regs[] = {
  40. { 0xfd, 0x00 }, /* Page Shift */
  41. { 0x02, 0x00 }, /* Reset */
  42. { 0x18, 0x00 }, /* Interface Sel: RGB 24 Bits */
  43. { 0x34, 0x20 }, /* Undocumented */
  44. { 0x0c, 0x01 }, /* Contrast set by CMD1 == within page 0x00 */
  45. { 0x0d, 0x48 }, /* R Brightness */
  46. { 0x0e, 0x48 }, /* G Brightness */
  47. { 0x0f, 0x48 }, /* B Brightness */
  48. { 0x07, 0x40 }, /* R Contrast */
  49. { 0x08, 0x33 }, /* G Contrast */
  50. { 0x09, 0x3a }, /* B Contrast */
  51. { 0x16, 0x01 }, /* NTSC Sel */
  52. { 0x19, 0x8d }, /* VBLK */
  53. { 0x1a, 0x28 }, /* HBLK */
  54. { 0x1c, 0x00 }, /* Scan Shift Dir. */
  55. { 0xfd, 0xc5 }, /* Page Shift */
  56. { 0x82, 0x0c }, /* PWR_CTRL Pump */
  57. { 0xa2, 0xb4 }, /* PWR_CTRL VGH/VGL */
  58. { 0xfd, 0xc4 }, /* Page Shift - What follows is listed as "RGB 24bit Timing Set" */
  59. { 0x82, 0x45 },
  60. { 0xfd, 0xc1 },
  61. { 0x91, 0x02 },
  62. { 0xfd, 0xc0 },
  63. { 0xa1, 0x01 },
  64. { 0xa2, 0x1f },
  65. { 0xa3, 0x0b },
  66. { 0xa4, 0x38 },
  67. { 0xa5, 0x00 },
  68. { 0xa6, 0x0a },
  69. { 0xa7, 0x38 },
  70. { 0xa8, 0x00 },
  71. { 0xa9, 0x0a },
  72. { 0xaa, 0x37 },
  73. { 0xfd, 0xce },
  74. { 0x81, 0x18 },
  75. { 0x82, 0x43 },
  76. { 0x83, 0x43 },
  77. { 0x91, 0x06 },
  78. { 0x93, 0x38 },
  79. { 0x94, 0x02 },
  80. { 0x95, 0x06 },
  81. { 0x97, 0x38 },
  82. { 0x98, 0x02 },
  83. { 0x99, 0x06 },
  84. { 0x9b, 0x38 },
  85. { 0x9c, 0x02 },
  86. { 0xfd, 0x00 }, /* Page Shift */
  87. };
  88. static const struct regmap_config ota5601a_regmap_config = {
  89. .reg_bits = 8,
  90. .val_bits = 8,
  91. };
  92. static int ota5601a_prepare(struct drm_panel *drm_panel)
  93. {
  94. struct ota5601a *panel = to_ota5601a(drm_panel);
  95. int err;
  96. err = regulator_enable(panel->supply);
  97. if (err) {
  98. dev_err(drm_panel->dev, "Failed to enable power supply: %d\n", err);
  99. return err;
  100. }
  101. /* Reset to be held low for 10us min according to the doc, 10ms before sending commands */
  102. gpiod_set_value_cansleep(panel->reset_gpio, 1);
  103. usleep_range(10, 30);
  104. gpiod_set_value_cansleep(panel->reset_gpio, 0);
  105. usleep_range(10000, 20000);
  106. /* Init all registers. */
  107. err = regmap_multi_reg_write(panel->map, ota5601a_panel_regs,
  108. ARRAY_SIZE(ota5601a_panel_regs));
  109. if (err) {
  110. dev_err(drm_panel->dev, "Failed to init registers: %d\n", err);
  111. goto err_disable_regulator;
  112. }
  113. msleep(120);
  114. return 0;
  115. err_disable_regulator:
  116. regulator_disable(panel->supply);
  117. return err;
  118. }
  119. static int ota5601a_unprepare(struct drm_panel *drm_panel)
  120. {
  121. struct ota5601a *panel = to_ota5601a(drm_panel);
  122. gpiod_set_value_cansleep(panel->reset_gpio, 1);
  123. regulator_disable(panel->supply);
  124. return 0;
  125. }
  126. static int ota5601a_enable(struct drm_panel *drm_panel)
  127. {
  128. struct ota5601a *panel = to_ota5601a(drm_panel);
  129. int err;
  130. err = regmap_write(panel->map, OTA5601A_CTL, OTA5601A_CTL_ON);
  131. if (err) {
  132. dev_err(drm_panel->dev, "Unable to enable panel: %d\n", err);
  133. return err;
  134. }
  135. if (drm_panel->backlight) {
  136. /* Wait for the picture to be ready before enabling backlight */
  137. msleep(120);
  138. }
  139. return 0;
  140. }
  141. static int ota5601a_disable(struct drm_panel *drm_panel)
  142. {
  143. struct ota5601a *panel = to_ota5601a(drm_panel);
  144. int err;
  145. err = regmap_write(panel->map, OTA5601A_CTL, OTA5601A_CTL_OFF);
  146. if (err) {
  147. dev_err(drm_panel->dev, "Unable to disable panel: %d\n", err);
  148. return err;
  149. }
  150. return 0;
  151. }
  152. static int ota5601a_get_modes(struct drm_panel *drm_panel,
  153. struct drm_connector *connector)
  154. {
  155. struct ota5601a *panel = to_ota5601a(drm_panel);
  156. const struct ota5601a_panel_info *panel_info = panel->panel_info;
  157. struct drm_display_mode *mode;
  158. unsigned int i;
  159. for (i = 0; i < panel_info->num_modes; i++) {
  160. mode = drm_mode_duplicate(connector->dev,
  161. &panel_info->display_modes[i]);
  162. if (!mode)
  163. return -ENOMEM;
  164. drm_mode_set_name(mode);
  165. mode->type = DRM_MODE_TYPE_DRIVER;
  166. if (panel_info->num_modes == 1)
  167. mode->type |= DRM_MODE_TYPE_PREFERRED;
  168. drm_mode_probed_add(connector, mode);
  169. }
  170. connector->display_info.bpc = 8;
  171. connector->display_info.width_mm = panel_info->width_mm;
  172. connector->display_info.height_mm = panel_info->height_mm;
  173. drm_display_info_set_bus_formats(&connector->display_info,
  174. &panel_info->bus_format, 1);
  175. connector->display_info.bus_flags = panel_info->bus_flags;
  176. return panel_info->num_modes;
  177. }
  178. static const struct drm_panel_funcs ota5601a_funcs = {
  179. .prepare = ota5601a_prepare,
  180. .unprepare = ota5601a_unprepare,
  181. .enable = ota5601a_enable,
  182. .disable = ota5601a_disable,
  183. .get_modes = ota5601a_get_modes,
  184. };
  185. static int ota5601a_probe(struct spi_device *spi)
  186. {
  187. const struct spi_device_id *id = spi_get_device_id(spi);
  188. struct device *dev = &spi->dev;
  189. struct ota5601a *panel;
  190. int err;
  191. panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
  192. if (!panel)
  193. return -ENOMEM;
  194. spi_set_drvdata(spi, panel);
  195. panel->panel_info = (const struct ota5601a_panel_info *)id->driver_data;
  196. if (!panel->panel_info)
  197. return -EINVAL;
  198. panel->supply = devm_regulator_get(dev, "power");
  199. if (IS_ERR(panel->supply)) {
  200. dev_err(dev, "Failed to get power supply\n");
  201. return PTR_ERR(panel->supply);
  202. }
  203. panel->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  204. if (IS_ERR(panel->reset_gpio)) {
  205. dev_err(dev, "Failed to get reset GPIO\n");
  206. return PTR_ERR(panel->reset_gpio);
  207. }
  208. spi->bits_per_word = 8;
  209. spi->mode = SPI_MODE_3 | SPI_3WIRE;
  210. err = spi_setup(spi);
  211. if (err) {
  212. dev_err(dev, "Failed to setup SPI\n");
  213. return err;
  214. }
  215. panel->map = devm_regmap_init_spi(spi, &ota5601a_regmap_config);
  216. if (IS_ERR(panel->map)) {
  217. dev_err(dev, "Failed to init regmap\n");
  218. return PTR_ERR(panel->map);
  219. }
  220. drm_panel_init(&panel->drm_panel, dev, &ota5601a_funcs,
  221. DRM_MODE_CONNECTOR_DPI);
  222. err = drm_panel_of_backlight(&panel->drm_panel);
  223. if (err) {
  224. if (err != -EPROBE_DEFER)
  225. dev_err(dev, "Failed to get backlight handle\n");
  226. return err;
  227. }
  228. drm_panel_add(&panel->drm_panel);
  229. return 0;
  230. }
  231. static void ota5601a_remove(struct spi_device *spi)
  232. {
  233. struct ota5601a *panel = spi_get_drvdata(spi);
  234. drm_panel_remove(&panel->drm_panel);
  235. ota5601a_disable(&panel->drm_panel);
  236. ota5601a_unprepare(&panel->drm_panel);
  237. }
  238. static const struct drm_display_mode gpt3_display_modes[] = {
  239. { /* 60 Hz */
  240. .clock = 27000,
  241. .hdisplay = 640,
  242. .hsync_start = 640 + 220,
  243. .hsync_end = 640 + 220 + 20,
  244. .htotal = 640 + 220 + 20 + 20,
  245. .vdisplay = 480,
  246. .vsync_start = 480 + 7,
  247. .vsync_end = 480 + 7 + 6,
  248. .vtotal = 480 + 7 + 6 + 7,
  249. .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
  250. },
  251. { /* 50 Hz */
  252. .clock = 24000,
  253. .hdisplay = 640,
  254. .hsync_start = 640 + 280,
  255. .hsync_end = 640 + 280 + 20,
  256. .htotal = 640 + 280 + 20 + 20,
  257. .vdisplay = 480,
  258. .vsync_start = 480 + 7,
  259. .vsync_end = 480 + 7 + 6,
  260. .vtotal = 480 + 7 + 6 + 7,
  261. .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
  262. },
  263. };
  264. static const struct ota5601a_panel_info gpt3_info = {
  265. .display_modes = gpt3_display_modes,
  266. .num_modes = ARRAY_SIZE(gpt3_display_modes),
  267. .width_mm = 71,
  268. .height_mm = 51,
  269. .bus_format = MEDIA_BUS_FMT_RGB888_1X24,
  270. .bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE,
  271. };
  272. static const struct spi_device_id gpt3_id[] = {
  273. { "gpt3", (kernel_ulong_t)&gpt3_info },
  274. { /* sentinel */ }
  275. };
  276. MODULE_DEVICE_TABLE(spi, gpt3_id);
  277. static const struct of_device_id ota5601a_of_match[] = {
  278. { .compatible = "focaltech,gpt3" },
  279. { /* sentinel */ }
  280. };
  281. MODULE_DEVICE_TABLE(of, ota5601a_of_match);
  282. static struct spi_driver ota5601a_driver = {
  283. .driver = {
  284. .name = "ota5601a",
  285. .of_match_table = ota5601a_of_match,
  286. },
  287. .id_table = gpt3_id,
  288. .probe = ota5601a_probe,
  289. .remove = ota5601a_remove,
  290. };
  291. module_spi_driver(ota5601a_driver);
  292. MODULE_AUTHOR("Christophe Branchereau <cbranchereau@gmail.com>");
  293. MODULE_DESCRIPTION("Orisetech OTA5601A TFT LCD panel driver");
  294. MODULE_LICENSE("GPL");