tc358764.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Samsung Electronics Co., Ltd
  4. *
  5. * Authors:
  6. * Andrzej Hajda <a.hajda@samsung.com>
  7. * Maciej Purski <m.purski@samsung.com>
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/module.h>
  13. #include <linux/of_graph.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <video/mipi_display.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_mipi_dsi.h>
  18. #include <drm/drm_of.h>
  19. #include <drm/drm_print.h>
  20. #define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end))
  21. #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
  22. /* PPI layer registers */
  23. #define PPI_STARTPPI 0x0104 /* START control bit */
  24. #define PPI_LPTXTIMECNT 0x0114 /* LPTX timing signal */
  25. #define PPI_LANEENABLE 0x0134 /* Enables each lane */
  26. #define PPI_TX_RX_TA 0x013C /* BTA timing parameters */
  27. #define PPI_D0S_CLRSIPOCOUNT 0x0164 /* Assertion timer for Lane 0 */
  28. #define PPI_D1S_CLRSIPOCOUNT 0x0168 /* Assertion timer for Lane 1 */
  29. #define PPI_D2S_CLRSIPOCOUNT 0x016C /* Assertion timer for Lane 2 */
  30. #define PPI_D3S_CLRSIPOCOUNT 0x0170 /* Assertion timer for Lane 3 */
  31. #define PPI_START_FUNCTION 1
  32. /* DSI layer registers */
  33. #define DSI_STARTDSI 0x0204 /* START control bit of DSI-TX */
  34. #define DSI_LANEENABLE 0x0210 /* Enables each lane */
  35. #define DSI_RX_START 1
  36. /* Video path registers */
  37. #define VP_CTRL 0x0450 /* Video Path Control */
  38. #define VP_CTRL_MSF BIT(0) /* Magic square in RGB666 */
  39. #define VP_CTRL_VTGEN BIT(4) /* Use chip clock for timing */
  40. #define VP_CTRL_EVTMODE BIT(5) /* Event mode */
  41. #define VP_CTRL_RGB888 BIT(8) /* RGB888 mode */
  42. #define VP_CTRL_VSDELAY(v) FLD_VAL(v, 31, 20) /* VSYNC delay */
  43. #define VP_CTRL_HSPOL BIT(17) /* Polarity of HSYNC signal */
  44. #define VP_CTRL_DEPOL BIT(18) /* Polarity of DE signal */
  45. #define VP_CTRL_VSPOL BIT(19) /* Polarity of VSYNC signal */
  46. #define VP_HTIM1 0x0454 /* Horizontal Timing Control 1 */
  47. #define VP_HTIM1_HBP(v) FLD_VAL(v, 24, 16)
  48. #define VP_HTIM1_HSYNC(v) FLD_VAL(v, 8, 0)
  49. #define VP_HTIM2 0x0458 /* Horizontal Timing Control 2 */
  50. #define VP_HTIM2_HFP(v) FLD_VAL(v, 24, 16)
  51. #define VP_HTIM2_HACT(v) FLD_VAL(v, 10, 0)
  52. #define VP_VTIM1 0x045C /* Vertical Timing Control 1 */
  53. #define VP_VTIM1_VBP(v) FLD_VAL(v, 23, 16)
  54. #define VP_VTIM1_VSYNC(v) FLD_VAL(v, 7, 0)
  55. #define VP_VTIM2 0x0460 /* Vertical Timing Control 2 */
  56. #define VP_VTIM2_VFP(v) FLD_VAL(v, 23, 16)
  57. #define VP_VTIM2_VACT(v) FLD_VAL(v, 10, 0)
  58. #define VP_VFUEN 0x0464 /* Video Frame Timing Update Enable */
  59. /* LVDS registers */
  60. #define LV_MX0003 0x0480 /* Mux input bit 0 to 3 */
  61. #define LV_MX0407 0x0484 /* Mux input bit 4 to 7 */
  62. #define LV_MX0811 0x0488 /* Mux input bit 8 to 11 */
  63. #define LV_MX1215 0x048C /* Mux input bit 12 to 15 */
  64. #define LV_MX1619 0x0490 /* Mux input bit 16 to 19 */
  65. #define LV_MX2023 0x0494 /* Mux input bit 20 to 23 */
  66. #define LV_MX2427 0x0498 /* Mux input bit 24 to 27 */
  67. #define LV_MX(b0, b1, b2, b3) (FLD_VAL(b0, 4, 0) | FLD_VAL(b1, 12, 8) | \
  68. FLD_VAL(b2, 20, 16) | FLD_VAL(b3, 28, 24))
  69. /* Input bit numbers used in mux registers */
  70. enum {
  71. LVI_R0,
  72. LVI_R1,
  73. LVI_R2,
  74. LVI_R3,
  75. LVI_R4,
  76. LVI_R5,
  77. LVI_R6,
  78. LVI_R7,
  79. LVI_G0,
  80. LVI_G1,
  81. LVI_G2,
  82. LVI_G3,
  83. LVI_G4,
  84. LVI_G5,
  85. LVI_G6,
  86. LVI_G7,
  87. LVI_B0,
  88. LVI_B1,
  89. LVI_B2,
  90. LVI_B3,
  91. LVI_B4,
  92. LVI_B5,
  93. LVI_B6,
  94. LVI_B7,
  95. LVI_HS,
  96. LVI_VS,
  97. LVI_DE,
  98. LVI_L0
  99. };
  100. #define LV_CFG 0x049C /* LVDS Configuration */
  101. #define LV_PHY0 0x04A0 /* LVDS PHY 0 */
  102. #define LV_PHY0_RST(v) FLD_VAL(v, 22, 22) /* PHY reset */
  103. #define LV_PHY0_IS(v) FLD_VAL(v, 15, 14)
  104. #define LV_PHY0_ND(v) FLD_VAL(v, 4, 0) /* Frequency range select */
  105. #define LV_PHY0_PRBS_ON(v) FLD_VAL(v, 20, 16) /* Clock/Data Flag pins */
  106. /* System registers */
  107. #define SYS_RST 0x0504 /* System Reset */
  108. #define SYS_ID 0x0580 /* System ID */
  109. #define SYS_RST_I2CS BIT(0) /* Reset I2C-Slave controller */
  110. #define SYS_RST_I2CM BIT(1) /* Reset I2C-Master controller */
  111. #define SYS_RST_LCD BIT(2) /* Reset LCD controller */
  112. #define SYS_RST_BM BIT(3) /* Reset Bus Management controller */
  113. #define SYS_RST_DSIRX BIT(4) /* Reset DSI-RX and App controller */
  114. #define SYS_RST_REG BIT(5) /* Reset Register module */
  115. #define LPX_PERIOD 2
  116. #define TTA_SURE 3
  117. #define TTA_GET 0x20000
  118. /* Lane enable PPI and DSI register bits */
  119. #define LANEENABLE_CLEN BIT(0)
  120. #define LANEENABLE_L0EN BIT(1)
  121. #define LANEENABLE_L1EN BIT(2)
  122. #define LANEENABLE_L2EN BIT(3)
  123. #define LANEENABLE_L3EN BIT(4)
  124. /* LVCFG fields */
  125. #define LV_CFG_LVEN BIT(0)
  126. #define LV_CFG_LVDLINK BIT(1)
  127. #define LV_CFG_CLKPOL1 BIT(2)
  128. #define LV_CFG_CLKPOL2 BIT(3)
  129. static const char * const tc358764_supplies[] = {
  130. "vddc", "vddio", "vddlvds"
  131. };
  132. struct tc358764 {
  133. struct device *dev;
  134. struct drm_bridge bridge;
  135. struct drm_bridge *next_bridge;
  136. struct regulator_bulk_data supplies[ARRAY_SIZE(tc358764_supplies)];
  137. struct gpio_desc *gpio_reset;
  138. int error;
  139. };
  140. static int tc358764_clear_error(struct tc358764 *ctx)
  141. {
  142. int ret = ctx->error;
  143. ctx->error = 0;
  144. return ret;
  145. }
  146. static void tc358764_read(struct tc358764 *ctx, u16 addr, u32 *val)
  147. {
  148. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  149. ssize_t ret;
  150. if (ctx->error)
  151. return;
  152. cpu_to_le16s(&addr);
  153. ret = mipi_dsi_generic_read(dsi, &addr, sizeof(addr), val, sizeof(*val));
  154. if (ret >= 0)
  155. le32_to_cpus(val);
  156. dev_dbg(ctx->dev, "read: addr=0x%04x data=0x%08x\n", addr, *val);
  157. }
  158. static void tc358764_write(struct tc358764 *ctx, u16 addr, u32 val)
  159. {
  160. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  161. ssize_t ret;
  162. u8 data[6];
  163. if (ctx->error)
  164. return;
  165. data[0] = addr;
  166. data[1] = addr >> 8;
  167. data[2] = val;
  168. data[3] = val >> 8;
  169. data[4] = val >> 16;
  170. data[5] = val >> 24;
  171. ret = mipi_dsi_generic_write(dsi, data, sizeof(data));
  172. if (ret < 0)
  173. ctx->error = ret;
  174. }
  175. static inline struct tc358764 *bridge_to_tc358764(struct drm_bridge *bridge)
  176. {
  177. return container_of(bridge, struct tc358764, bridge);
  178. }
  179. static int tc358764_init(struct tc358764 *ctx)
  180. {
  181. u32 v = 0;
  182. tc358764_read(ctx, SYS_ID, &v);
  183. if (ctx->error)
  184. return tc358764_clear_error(ctx);
  185. dev_info(ctx->dev, "ID: %#x\n", v);
  186. /* configure PPI counters */
  187. tc358764_write(ctx, PPI_TX_RX_TA, TTA_GET | TTA_SURE);
  188. tc358764_write(ctx, PPI_LPTXTIMECNT, LPX_PERIOD);
  189. tc358764_write(ctx, PPI_D0S_CLRSIPOCOUNT, 5);
  190. tc358764_write(ctx, PPI_D1S_CLRSIPOCOUNT, 5);
  191. tc358764_write(ctx, PPI_D2S_CLRSIPOCOUNT, 5);
  192. tc358764_write(ctx, PPI_D3S_CLRSIPOCOUNT, 5);
  193. /* enable four data lanes and clock lane */
  194. tc358764_write(ctx, PPI_LANEENABLE, LANEENABLE_L3EN | LANEENABLE_L2EN |
  195. LANEENABLE_L1EN | LANEENABLE_L0EN | LANEENABLE_CLEN);
  196. tc358764_write(ctx, DSI_LANEENABLE, LANEENABLE_L3EN | LANEENABLE_L2EN |
  197. LANEENABLE_L1EN | LANEENABLE_L0EN | LANEENABLE_CLEN);
  198. /* start */
  199. tc358764_write(ctx, PPI_STARTPPI, PPI_START_FUNCTION);
  200. tc358764_write(ctx, DSI_STARTDSI, DSI_RX_START);
  201. /* configure video path */
  202. tc358764_write(ctx, VP_CTRL, VP_CTRL_VSDELAY(15) | VP_CTRL_RGB888 |
  203. VP_CTRL_EVTMODE | VP_CTRL_HSPOL | VP_CTRL_VSPOL);
  204. /* reset PHY */
  205. tc358764_write(ctx, LV_PHY0, LV_PHY0_RST(1) |
  206. LV_PHY0_PRBS_ON(4) | LV_PHY0_IS(2) | LV_PHY0_ND(6));
  207. tc358764_write(ctx, LV_PHY0, LV_PHY0_PRBS_ON(4) | LV_PHY0_IS(2) |
  208. LV_PHY0_ND(6));
  209. /* reset bridge */
  210. tc358764_write(ctx, SYS_RST, SYS_RST_LCD);
  211. /* set bit order */
  212. tc358764_write(ctx, LV_MX0003, LV_MX(LVI_R0, LVI_R1, LVI_R2, LVI_R3));
  213. tc358764_write(ctx, LV_MX0407, LV_MX(LVI_R4, LVI_R7, LVI_R5, LVI_G0));
  214. tc358764_write(ctx, LV_MX0811, LV_MX(LVI_G1, LVI_G2, LVI_G6, LVI_G7));
  215. tc358764_write(ctx, LV_MX1215, LV_MX(LVI_G3, LVI_G4, LVI_G5, LVI_B0));
  216. tc358764_write(ctx, LV_MX1619, LV_MX(LVI_B6, LVI_B7, LVI_B1, LVI_B2));
  217. tc358764_write(ctx, LV_MX2023, LV_MX(LVI_B3, LVI_B4, LVI_B5, LVI_L0));
  218. tc358764_write(ctx, LV_MX2427, LV_MX(LVI_HS, LVI_VS, LVI_DE, LVI_R6));
  219. tc358764_write(ctx, LV_CFG, LV_CFG_CLKPOL2 | LV_CFG_CLKPOL1 |
  220. LV_CFG_LVEN);
  221. return tc358764_clear_error(ctx);
  222. }
  223. static void tc358764_reset(struct tc358764 *ctx)
  224. {
  225. gpiod_set_value(ctx->gpio_reset, 1);
  226. usleep_range(1000, 2000);
  227. gpiod_set_value(ctx->gpio_reset, 0);
  228. usleep_range(1000, 2000);
  229. }
  230. static void tc358764_post_disable(struct drm_bridge *bridge)
  231. {
  232. struct tc358764 *ctx = bridge_to_tc358764(bridge);
  233. int ret;
  234. tc358764_reset(ctx);
  235. usleep_range(10000, 15000);
  236. ret = regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  237. if (ret < 0)
  238. dev_err(ctx->dev, "error disabling regulators (%d)\n", ret);
  239. }
  240. static void tc358764_pre_enable(struct drm_bridge *bridge)
  241. {
  242. struct tc358764 *ctx = bridge_to_tc358764(bridge);
  243. int ret;
  244. ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  245. if (ret < 0)
  246. dev_err(ctx->dev, "error enabling regulators (%d)\n", ret);
  247. usleep_range(10000, 15000);
  248. tc358764_reset(ctx);
  249. ret = tc358764_init(ctx);
  250. if (ret < 0)
  251. dev_err(ctx->dev, "error initializing bridge (%d)\n", ret);
  252. }
  253. static int tc358764_attach(struct drm_bridge *bridge,
  254. enum drm_bridge_attach_flags flags)
  255. {
  256. struct tc358764 *ctx = bridge_to_tc358764(bridge);
  257. return drm_bridge_attach(bridge->encoder, ctx->next_bridge, bridge, flags);
  258. }
  259. static const struct drm_bridge_funcs tc358764_bridge_funcs = {
  260. .post_disable = tc358764_post_disable,
  261. .pre_enable = tc358764_pre_enable,
  262. .attach = tc358764_attach,
  263. };
  264. static int tc358764_parse_dt(struct tc358764 *ctx)
  265. {
  266. struct device *dev = ctx->dev;
  267. ctx->gpio_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
  268. if (IS_ERR(ctx->gpio_reset)) {
  269. dev_err(dev, "no reset GPIO pin provided\n");
  270. return PTR_ERR(ctx->gpio_reset);
  271. }
  272. ctx->next_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 1, 0);
  273. if (IS_ERR(ctx->next_bridge))
  274. return PTR_ERR(ctx->next_bridge);
  275. return 0;
  276. }
  277. static int tc358764_configure_regulators(struct tc358764 *ctx)
  278. {
  279. int i, ret;
  280. for (i = 0; i < ARRAY_SIZE(ctx->supplies); ++i)
  281. ctx->supplies[i].supply = tc358764_supplies[i];
  282. ret = devm_regulator_bulk_get(ctx->dev, ARRAY_SIZE(ctx->supplies),
  283. ctx->supplies);
  284. if (ret < 0)
  285. dev_err(ctx->dev, "failed to get regulators: %d\n", ret);
  286. return ret;
  287. }
  288. static int tc358764_probe(struct mipi_dsi_device *dsi)
  289. {
  290. struct device *dev = &dsi->dev;
  291. struct tc358764 *ctx;
  292. int ret;
  293. ctx = devm_kzalloc(dev, sizeof(struct tc358764), GFP_KERNEL);
  294. if (!ctx)
  295. return -ENOMEM;
  296. mipi_dsi_set_drvdata(dsi, ctx);
  297. ctx->dev = dev;
  298. dsi->lanes = 4;
  299. dsi->format = MIPI_DSI_FMT_RGB888;
  300. dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST
  301. | MIPI_DSI_MODE_VIDEO_AUTO_VERT | MIPI_DSI_MODE_LPM;
  302. ret = tc358764_parse_dt(ctx);
  303. if (ret < 0)
  304. return ret;
  305. ret = tc358764_configure_regulators(ctx);
  306. if (ret < 0)
  307. return ret;
  308. ctx->bridge.funcs = &tc358764_bridge_funcs;
  309. ctx->bridge.of_node = dev->of_node;
  310. ctx->bridge.pre_enable_prev_first = true;
  311. drm_bridge_add(&ctx->bridge);
  312. ret = mipi_dsi_attach(dsi);
  313. if (ret < 0) {
  314. drm_bridge_remove(&ctx->bridge);
  315. dev_err(dev, "failed to attach dsi\n");
  316. }
  317. return ret;
  318. }
  319. static void tc358764_remove(struct mipi_dsi_device *dsi)
  320. {
  321. struct tc358764 *ctx = mipi_dsi_get_drvdata(dsi);
  322. mipi_dsi_detach(dsi);
  323. drm_bridge_remove(&ctx->bridge);
  324. }
  325. static const struct of_device_id tc358764_of_match[] = {
  326. { .compatible = "toshiba,tc358764" },
  327. { }
  328. };
  329. MODULE_DEVICE_TABLE(of, tc358764_of_match);
  330. static struct mipi_dsi_driver tc358764_driver = {
  331. .probe = tc358764_probe,
  332. .remove = tc358764_remove,
  333. .driver = {
  334. .name = "tc358764",
  335. .of_match_table = tc358764_of_match,
  336. },
  337. };
  338. module_mipi_dsi_driver(tc358764_driver);
  339. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  340. MODULE_AUTHOR("Maciej Purski <m.purski@samsung.com>");
  341. MODULE_DESCRIPTION("MIPI-DSI based Driver for TC358764 DSI/LVDS Bridge");
  342. MODULE_LICENSE("GPL v2");