tc358762.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Marek Vasut <marex@denx.de>
  4. *
  5. * Based on tc358764.c by
  6. * Andrzej Hajda <a.hajda@samsung.com>
  7. * Maciej Purski <m.purski@samsung.com>
  8. *
  9. * Based on rpi_touchscreen.c by
  10. * Eric Anholt <eric@anholt.net>
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/mod_devicetable.h>
  15. #include <linux/module.h>
  16. #include <linux/of_graph.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <video/mipi_display.h>
  19. #include <drm/drm_atomic_helper.h>
  20. #include <drm/drm_crtc.h>
  21. #include <drm/drm_mipi_dsi.h>
  22. #include <drm/drm_of.h>
  23. #include <drm/drm_panel.h>
  24. #include <drm/drm_print.h>
  25. #include <drm/drm_probe_helper.h>
  26. /* PPI layer registers */
  27. #define PPI_STARTPPI 0x0104 /* START control bit */
  28. #define PPI_LPTXTIMECNT 0x0114 /* LPTX timing signal */
  29. #define PPI_D0S_ATMR 0x0144
  30. #define PPI_D1S_ATMR 0x0148
  31. #define PPI_D0S_CLRSIPOCOUNT 0x0164 /* Assertion timer for Lane 0 */
  32. #define PPI_D1S_CLRSIPOCOUNT 0x0168 /* Assertion timer for Lane 1 */
  33. #define PPI_START_FUNCTION 1
  34. /* DSI layer registers */
  35. #define DSI_STARTDSI 0x0204 /* START control bit of DSI-TX */
  36. #define DSI_LANEENABLE 0x0210 /* Enables each lane */
  37. #define DSI_RX_START 1
  38. /* LCDC/DPI Host Registers, based on guesswork that this matches TC358764 */
  39. #define LCDCTRL 0x0420 /* Video Path Control */
  40. #define LCDCTRL_MSF BIT(0) /* Magic square in RGB666 */
  41. #define LCDCTRL_VTGEN BIT(4)/* Use chip clock for timing */
  42. #define LCDCTRL_UNK6 BIT(6) /* Unknown */
  43. #define LCDCTRL_EVTMODE BIT(5) /* Event mode */
  44. #define LCDCTRL_RGB888 BIT(8) /* RGB888 mode */
  45. #define LCDCTRL_HSPOL BIT(17) /* Polarity of HSYNC signal */
  46. #define LCDCTRL_DEPOL BIT(18) /* Polarity of DE signal */
  47. #define LCDCTRL_VSPOL BIT(19) /* Polarity of VSYNC signal */
  48. #define LCDCTRL_VSDELAY(v) (((v) & 0xfff) << 20) /* VSYNC delay */
  49. /* SPI Master Registers */
  50. #define SPICMR 0x0450
  51. #define SPITCR 0x0454
  52. /* System Controller Registers */
  53. #define SYSCTRL 0x0464
  54. /* System registers */
  55. #define LPX_PERIOD 3
  56. /* Lane enable PPI and DSI register bits */
  57. #define LANEENABLE_CLEN BIT(0)
  58. #define LANEENABLE_L0EN BIT(1)
  59. #define LANEENABLE_L1EN BIT(2)
  60. struct tc358762 {
  61. struct device *dev;
  62. struct drm_bridge bridge;
  63. struct regulator *regulator;
  64. struct drm_bridge *panel_bridge;
  65. struct gpio_desc *reset_gpio;
  66. struct drm_display_mode mode;
  67. bool pre_enabled;
  68. int error;
  69. };
  70. static int tc358762_clear_error(struct tc358762 *ctx)
  71. {
  72. int ret = ctx->error;
  73. ctx->error = 0;
  74. return ret;
  75. }
  76. static void tc358762_write(struct tc358762 *ctx, u16 addr, u32 val)
  77. {
  78. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  79. ssize_t ret;
  80. u8 data[6];
  81. if (ctx->error)
  82. return;
  83. data[0] = addr;
  84. data[1] = addr >> 8;
  85. data[2] = val;
  86. data[3] = val >> 8;
  87. data[4] = val >> 16;
  88. data[5] = val >> 24;
  89. ret = mipi_dsi_generic_write(dsi, data, sizeof(data));
  90. if (ret < 0)
  91. ctx->error = ret;
  92. }
  93. static inline struct tc358762 *bridge_to_tc358762(struct drm_bridge *bridge)
  94. {
  95. return container_of(bridge, struct tc358762, bridge);
  96. }
  97. static int tc358762_init(struct tc358762 *ctx)
  98. {
  99. u32 lcdctrl;
  100. tc358762_write(ctx, DSI_LANEENABLE,
  101. LANEENABLE_L0EN | LANEENABLE_CLEN);
  102. tc358762_write(ctx, PPI_D0S_CLRSIPOCOUNT, 5);
  103. tc358762_write(ctx, PPI_D1S_CLRSIPOCOUNT, 5);
  104. tc358762_write(ctx, PPI_D0S_ATMR, 0);
  105. tc358762_write(ctx, PPI_D1S_ATMR, 0);
  106. tc358762_write(ctx, PPI_LPTXTIMECNT, LPX_PERIOD);
  107. tc358762_write(ctx, SPICMR, 0x00);
  108. lcdctrl = LCDCTRL_VSDELAY(1) | LCDCTRL_RGB888 |
  109. LCDCTRL_UNK6 | LCDCTRL_VTGEN;
  110. if (ctx->mode.flags & DRM_MODE_FLAG_NHSYNC)
  111. lcdctrl |= LCDCTRL_HSPOL;
  112. if (ctx->mode.flags & DRM_MODE_FLAG_NVSYNC)
  113. lcdctrl |= LCDCTRL_VSPOL;
  114. tc358762_write(ctx, LCDCTRL, lcdctrl);
  115. tc358762_write(ctx, SYSCTRL, 0x040f);
  116. msleep(100);
  117. tc358762_write(ctx, PPI_STARTPPI, PPI_START_FUNCTION);
  118. tc358762_write(ctx, DSI_STARTDSI, DSI_RX_START);
  119. msleep(100);
  120. return tc358762_clear_error(ctx);
  121. }
  122. static void tc358762_post_disable(struct drm_bridge *bridge, struct drm_bridge_state *state)
  123. {
  124. struct tc358762 *ctx = bridge_to_tc358762(bridge);
  125. int ret;
  126. /*
  127. * The post_disable hook might be called multiple times.
  128. * We want to avoid regulator imbalance below.
  129. */
  130. if (!ctx->pre_enabled)
  131. return;
  132. ctx->pre_enabled = false;
  133. if (ctx->reset_gpio)
  134. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  135. ret = regulator_disable(ctx->regulator);
  136. if (ret < 0)
  137. dev_err(ctx->dev, "error disabling regulators (%d)\n", ret);
  138. }
  139. static void tc358762_pre_enable(struct drm_bridge *bridge, struct drm_bridge_state *state)
  140. {
  141. struct tc358762 *ctx = bridge_to_tc358762(bridge);
  142. int ret;
  143. ret = regulator_enable(ctx->regulator);
  144. if (ret < 0)
  145. dev_err(ctx->dev, "error enabling regulators (%d)\n", ret);
  146. if (ctx->reset_gpio) {
  147. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  148. usleep_range(5000, 10000);
  149. }
  150. ctx->pre_enabled = true;
  151. }
  152. static void tc358762_enable(struct drm_bridge *bridge, struct drm_bridge_state *state)
  153. {
  154. struct tc358762 *ctx = bridge_to_tc358762(bridge);
  155. int ret;
  156. ret = tc358762_init(ctx);
  157. if (ret < 0)
  158. dev_err(ctx->dev, "error initializing bridge (%d)\n", ret);
  159. }
  160. static int tc358762_attach(struct drm_bridge *bridge,
  161. enum drm_bridge_attach_flags flags)
  162. {
  163. struct tc358762 *ctx = bridge_to_tc358762(bridge);
  164. return drm_bridge_attach(bridge->encoder, ctx->panel_bridge,
  165. bridge, flags);
  166. }
  167. static void tc358762_bridge_mode_set(struct drm_bridge *bridge,
  168. const struct drm_display_mode *mode,
  169. const struct drm_display_mode *adj)
  170. {
  171. struct tc358762 *ctx = bridge_to_tc358762(bridge);
  172. drm_mode_copy(&ctx->mode, mode);
  173. }
  174. static const struct drm_bridge_funcs tc358762_bridge_funcs = {
  175. .atomic_post_disable = tc358762_post_disable,
  176. .atomic_pre_enable = tc358762_pre_enable,
  177. .atomic_enable = tc358762_enable,
  178. .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
  179. .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
  180. .atomic_reset = drm_atomic_helper_bridge_reset,
  181. .attach = tc358762_attach,
  182. .mode_set = tc358762_bridge_mode_set,
  183. };
  184. static int tc358762_parse_dt(struct tc358762 *ctx)
  185. {
  186. struct drm_bridge *panel_bridge;
  187. struct device *dev = ctx->dev;
  188. panel_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 1, 0);
  189. if (IS_ERR(panel_bridge))
  190. return PTR_ERR(panel_bridge);
  191. ctx->panel_bridge = panel_bridge;
  192. /* Reset GPIO is optional */
  193. ctx->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  194. if (IS_ERR(ctx->reset_gpio))
  195. return PTR_ERR(ctx->reset_gpio);
  196. return 0;
  197. }
  198. static int tc358762_configure_regulators(struct tc358762 *ctx)
  199. {
  200. ctx->regulator = devm_regulator_get(ctx->dev, "vddc");
  201. if (IS_ERR(ctx->regulator))
  202. return PTR_ERR(ctx->regulator);
  203. return 0;
  204. }
  205. static int tc358762_probe(struct mipi_dsi_device *dsi)
  206. {
  207. struct device *dev = &dsi->dev;
  208. struct tc358762 *ctx;
  209. int ret;
  210. ctx = devm_kzalloc(dev, sizeof(struct tc358762), GFP_KERNEL);
  211. if (!ctx)
  212. return -ENOMEM;
  213. mipi_dsi_set_drvdata(dsi, ctx);
  214. ctx->dev = dev;
  215. ctx->pre_enabled = false;
  216. /* TODO: Find out how to get dual-lane mode working */
  217. dsi->lanes = 1;
  218. dsi->format = MIPI_DSI_FMT_RGB888;
  219. dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
  220. MIPI_DSI_MODE_LPM | MIPI_DSI_MODE_VIDEO_HSE;
  221. ret = tc358762_parse_dt(ctx);
  222. if (ret < 0)
  223. return ret;
  224. ret = tc358762_configure_regulators(ctx);
  225. if (ret < 0)
  226. return ret;
  227. ctx->bridge.funcs = &tc358762_bridge_funcs;
  228. ctx->bridge.type = DRM_MODE_CONNECTOR_DPI;
  229. ctx->bridge.of_node = dev->of_node;
  230. ctx->bridge.pre_enable_prev_first = true;
  231. drm_bridge_add(&ctx->bridge);
  232. ret = mipi_dsi_attach(dsi);
  233. if (ret < 0) {
  234. drm_bridge_remove(&ctx->bridge);
  235. dev_err(dev, "failed to attach dsi\n");
  236. }
  237. return ret;
  238. }
  239. static void tc358762_remove(struct mipi_dsi_device *dsi)
  240. {
  241. struct tc358762 *ctx = mipi_dsi_get_drvdata(dsi);
  242. mipi_dsi_detach(dsi);
  243. drm_bridge_remove(&ctx->bridge);
  244. }
  245. static const struct of_device_id tc358762_of_match[] = {
  246. { .compatible = "toshiba,tc358762" },
  247. { }
  248. };
  249. MODULE_DEVICE_TABLE(of, tc358762_of_match);
  250. static struct mipi_dsi_driver tc358762_driver = {
  251. .probe = tc358762_probe,
  252. .remove = tc358762_remove,
  253. .driver = {
  254. .name = "tc358762",
  255. .of_match_table = tc358762_of_match,
  256. },
  257. };
  258. module_mipi_dsi_driver(tc358762_driver);
  259. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  260. MODULE_DESCRIPTION("MIPI-DSI based Driver for TC358762 DSI/DPI Bridge");
  261. MODULE_LICENSE("GPL v2");