tc358775.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * TC358775 DSI to LVDS bridge driver
  4. *
  5. * Copyright (C) 2020 SMART Wireless Computing
  6. * Author: Vinay Simha BN <simhavcs@gmail.com>
  7. *
  8. */
  9. /* #define DEBUG */
  10. #include <linux/bitfield.h>
  11. #include <linux/clk.h>
  12. #include <linux/device.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/i2c.h>
  15. #include <linux/kernel.h>
  16. #include <linux/media-bus-format.h>
  17. #include <linux/module.h>
  18. #include <linux/of_device.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/slab.h>
  21. #include <linux/unaligned.h>
  22. #include <drm/display/drm_dp_helper.h>
  23. #include <drm/drm_atomic_helper.h>
  24. #include <drm/drm_bridge.h>
  25. #include <drm/drm_mipi_dsi.h>
  26. #include <drm/drm_of.h>
  27. #include <drm/drm_panel.h>
  28. #include <drm/drm_probe_helper.h>
  29. #define FLD_VAL(val, start, end) FIELD_PREP(GENMASK(start, end), val)
  30. /* Registers */
  31. /* DSI D-PHY Layer Registers */
  32. #define D0W_DPHYCONTTX 0x0004 /* Data Lane 0 DPHY Tx Control */
  33. #define CLW_DPHYCONTRX 0x0020 /* Clock Lane DPHY Rx Control */
  34. #define D0W_DPHYCONTRX 0x0024 /* Data Lane 0 DPHY Rx Control */
  35. #define D1W_DPHYCONTRX 0x0028 /* Data Lane 1 DPHY Rx Control */
  36. #define D2W_DPHYCONTRX 0x002C /* Data Lane 2 DPHY Rx Control */
  37. #define D3W_DPHYCONTRX 0x0030 /* Data Lane 3 DPHY Rx Control */
  38. #define COM_DPHYCONTRX 0x0038 /* DPHY Rx Common Control */
  39. #define CLW_CNTRL 0x0040 /* Clock Lane Control */
  40. #define D0W_CNTRL 0x0044 /* Data Lane 0 Control */
  41. #define D1W_CNTRL 0x0048 /* Data Lane 1 Control */
  42. #define D2W_CNTRL 0x004C /* Data Lane 2 Control */
  43. #define D3W_CNTRL 0x0050 /* Data Lane 3 Control */
  44. #define DFTMODE_CNTRL 0x0054 /* DFT Mode Control */
  45. /* DSI PPI Layer Registers */
  46. #define PPI_STARTPPI 0x0104 /* START control bit of PPI-TX function. */
  47. #define PPI_START_FUNCTION 1
  48. #define PPI_BUSYPPI 0x0108
  49. #define PPI_LINEINITCNT 0x0110 /* Line Initialization Wait Counter */
  50. #define PPI_LPTXTIMECNT 0x0114
  51. #define PPI_LANEENABLE 0x0134 /* Enables each lane at the PPI layer. */
  52. #define PPI_TX_RX_TA 0x013C /* DSI Bus Turn Around timing parameters */
  53. /* Analog timer function enable */
  54. #define PPI_CLS_ATMR 0x0140 /* Delay for Clock Lane in LPRX */
  55. #define PPI_D0S_ATMR 0x0144 /* Delay for Data Lane 0 in LPRX */
  56. #define PPI_D1S_ATMR 0x0148 /* Delay for Data Lane 1 in LPRX */
  57. #define PPI_D2S_ATMR 0x014C /* Delay for Data Lane 2 in LPRX */
  58. #define PPI_D3S_ATMR 0x0150 /* Delay for Data Lane 3 in LPRX */
  59. #define PPI_D0S_CLRSIPOCOUNT 0x0164 /* For lane 0 */
  60. #define PPI_D1S_CLRSIPOCOUNT 0x0168 /* For lane 1 */
  61. #define PPI_D2S_CLRSIPOCOUNT 0x016C /* For lane 2 */
  62. #define PPI_D3S_CLRSIPOCOUNT 0x0170 /* For lane 3 */
  63. #define CLS_PRE 0x0180 /* Digital Counter inside of PHY IO */
  64. #define D0S_PRE 0x0184 /* Digital Counter inside of PHY IO */
  65. #define D1S_PRE 0x0188 /* Digital Counter inside of PHY IO */
  66. #define D2S_PRE 0x018C /* Digital Counter inside of PHY IO */
  67. #define D3S_PRE 0x0190 /* Digital Counter inside of PHY IO */
  68. #define CLS_PREP 0x01A0 /* Digital Counter inside of PHY IO */
  69. #define D0S_PREP 0x01A4 /* Digital Counter inside of PHY IO */
  70. #define D1S_PREP 0x01A8 /* Digital Counter inside of PHY IO */
  71. #define D2S_PREP 0x01AC /* Digital Counter inside of PHY IO */
  72. #define D3S_PREP 0x01B0 /* Digital Counter inside of PHY IO */
  73. #define CLS_ZERO 0x01C0 /* Digital Counter inside of PHY IO */
  74. #define D0S_ZERO 0x01C4 /* Digital Counter inside of PHY IO */
  75. #define D1S_ZERO 0x01C8 /* Digital Counter inside of PHY IO */
  76. #define D2S_ZERO 0x01CC /* Digital Counter inside of PHY IO */
  77. #define D3S_ZERO 0x01D0 /* Digital Counter inside of PHY IO */
  78. #define PPI_CLRFLG 0x01E0 /* PRE Counters has reached set values */
  79. #define PPI_CLRSIPO 0x01E4 /* Clear SIPO values, Slave mode use only. */
  80. #define HSTIMEOUT 0x01F0 /* HS Rx Time Out Counter */
  81. #define HSTIMEOUTENABLE 0x01F4 /* Enable HS Rx Time Out Counter */
  82. #define DSI_STARTDSI 0x0204 /* START control bit of DSI-TX function */
  83. #define DSI_RX_START 1
  84. #define DSI_BUSYDSI 0x0208
  85. #define DSI_LANEENABLE 0x0210 /* Enables each lane at the Protocol layer. */
  86. #define DSI_LANESTATUS0 0x0214 /* Displays lane is in HS RX mode. */
  87. #define DSI_LANESTATUS1 0x0218 /* Displays lane is in ULPS or STOP state */
  88. #define DSI_INTSTATUS 0x0220 /* Interrupt Status */
  89. #define DSI_INTMASK 0x0224 /* Interrupt Mask */
  90. #define DSI_INTCLR 0x0228 /* Interrupt Clear */
  91. #define DSI_LPTXTO 0x0230 /* Low Power Tx Time Out Counter */
  92. #define DSIERRCNT 0x0300 /* DSI Error Count */
  93. #define APLCTRL 0x0400 /* Application Layer Control */
  94. #define RDPKTLN 0x0404 /* Command Read Packet Length */
  95. #define VPCTRL 0x0450 /* Video Path Control */
  96. #define EVTMODE BIT(5) /* Video event mode enable, tc35876x only */
  97. #define HTIM1 0x0454 /* Horizontal Timing Control 1 */
  98. #define HTIM2 0x0458 /* Horizontal Timing Control 2 */
  99. #define VTIM1 0x045C /* Vertical Timing Control 1 */
  100. #define VTIM2 0x0460 /* Vertical Timing Control 2 */
  101. #define VFUEN 0x0464 /* Video Frame Timing Update Enable */
  102. #define VFUEN_EN BIT(0) /* Upload Enable */
  103. /* Mux Input Select for LVDS LINK Input */
  104. #define LV_MX0003 0x0480 /* Bit 0 to 3 */
  105. #define LV_MX0407 0x0484 /* Bit 4 to 7 */
  106. #define LV_MX0811 0x0488 /* Bit 8 to 11 */
  107. #define LV_MX1215 0x048C /* Bit 12 to 15 */
  108. #define LV_MX1619 0x0490 /* Bit 16 to 19 */
  109. #define LV_MX2023 0x0494 /* Bit 20 to 23 */
  110. #define LV_MX2427 0x0498 /* Bit 24 to 27 */
  111. #define LV_MX(b0, b1, b2, b3) (FLD_VAL(b0, 4, 0) | FLD_VAL(b1, 12, 8) | \
  112. FLD_VAL(b2, 20, 16) | FLD_VAL(b3, 28, 24))
  113. /* Input bit numbers used in mux registers */
  114. enum {
  115. LVI_R0,
  116. LVI_R1,
  117. LVI_R2,
  118. LVI_R3,
  119. LVI_R4,
  120. LVI_R5,
  121. LVI_R6,
  122. LVI_R7,
  123. LVI_G0,
  124. LVI_G1,
  125. LVI_G2,
  126. LVI_G3,
  127. LVI_G4,
  128. LVI_G5,
  129. LVI_G6,
  130. LVI_G7,
  131. LVI_B0,
  132. LVI_B1,
  133. LVI_B2,
  134. LVI_B3,
  135. LVI_B4,
  136. LVI_B5,
  137. LVI_B6,
  138. LVI_B7,
  139. LVI_HS,
  140. LVI_VS,
  141. LVI_DE,
  142. LVI_L0
  143. };
  144. #define LVCFG 0x049C /* LVDS Configuration */
  145. #define LVPHY0 0x04A0 /* LVDS PHY 0 */
  146. #define LV_PHY0_RST(v) FLD_VAL(v, 22, 22) /* PHY reset */
  147. #define LV_PHY0_IS(v) FLD_VAL(v, 15, 14)
  148. #define LV_PHY0_ND(v) FLD_VAL(v, 4, 0) /* Frequency range select */
  149. #define LV_PHY0_PRBS_ON(v) FLD_VAL(v, 20, 16) /* Clock/Data Flag pins */
  150. #define LVPHY1 0x04A4 /* LVDS PHY 1 */
  151. #define SYSSTAT 0x0500 /* System Status */
  152. #define SYSRST 0x0504 /* System Reset */
  153. #define SYS_RST_I2CS BIT(0) /* Reset I2C-Slave controller */
  154. #define SYS_RST_I2CM BIT(1) /* Reset I2C-Master controller */
  155. #define SYS_RST_LCD BIT(2) /* Reset LCD controller */
  156. #define SYS_RST_BM BIT(3) /* Reset Bus Management controller */
  157. #define SYS_RST_DSIRX BIT(4) /* Reset DSI-RX and App controller */
  158. #define SYS_RST_REG BIT(5) /* Reset Register module */
  159. /* GPIO Registers */
  160. #define GPIOC 0x0520 /* GPIO Control */
  161. #define GPIOO 0x0524 /* GPIO Output */
  162. #define GPIOI 0x0528 /* GPIO Input */
  163. /* I2C Registers */
  164. #define I2CTIMCTRL 0x0540 /* I2C IF Timing and Enable Control */
  165. #define I2CMADDR 0x0544 /* I2C Master Addressing */
  166. #define WDATAQ 0x0548 /* Write Data Queue */
  167. #define RDATAQ 0x054C /* Read Data Queue */
  168. /* Chip ID and Revision ID Register */
  169. #define IDREG 0x0580
  170. #define LPX_PERIOD 4
  171. #define TTA_GET 0x40000
  172. #define TTA_SURE 6
  173. #define SINGLE_LINK 1
  174. #define DUAL_LINK 2
  175. #define TC358775XBG_ID 0x00007500
  176. /* Debug Registers */
  177. #define DEBUG00 0x05A0 /* Debug */
  178. #define DEBUG01 0x05A4 /* LVDS Data */
  179. #define DSI_CLEN_BIT BIT(0)
  180. #define DIVIDE_BY_3 3 /* PCLK=DCLK/3 */
  181. #define DIVIDE_BY_6 6 /* PCLK=DCLK/6 */
  182. #define LVCFG_LVEN_BIT BIT(0)
  183. #define L0EN BIT(1)
  184. #define TC358775_VPCTRL_VSDELAY__MASK 0x3FF00000
  185. #define TC358775_VPCTRL_VSDELAY__SHIFT 20
  186. static inline u32 TC358775_VPCTRL_VSDELAY(uint32_t val)
  187. {
  188. return ((val) << TC358775_VPCTRL_VSDELAY__SHIFT) &
  189. TC358775_VPCTRL_VSDELAY__MASK;
  190. }
  191. #define TC358775_VPCTRL_OPXLFMT__MASK 0x00000100
  192. #define TC358775_VPCTRL_OPXLFMT__SHIFT 8
  193. static inline u32 TC358775_VPCTRL_OPXLFMT(uint32_t val)
  194. {
  195. return ((val) << TC358775_VPCTRL_OPXLFMT__SHIFT) &
  196. TC358775_VPCTRL_OPXLFMT__MASK;
  197. }
  198. #define TC358775_VPCTRL_MSF__MASK 0x00000001
  199. #define TC358775_VPCTRL_MSF__SHIFT 0
  200. static inline u32 TC358775_VPCTRL_MSF(uint32_t val)
  201. {
  202. return ((val) << TC358775_VPCTRL_MSF__SHIFT) &
  203. TC358775_VPCTRL_MSF__MASK;
  204. }
  205. #define TC358775_LVCFG_PCLKDIV__MASK 0x000000f0
  206. #define TC358775_LVCFG_PCLKDIV__SHIFT 4
  207. static inline u32 TC358775_LVCFG_PCLKDIV(uint32_t val)
  208. {
  209. return ((val) << TC358775_LVCFG_PCLKDIV__SHIFT) &
  210. TC358775_LVCFG_PCLKDIV__MASK;
  211. }
  212. #define TC358775_LVCFG_LVDLINK__MASK 0x00000002
  213. #define TC358775_LVCFG_LVDLINK__SHIFT 1
  214. static inline u32 TC358775_LVCFG_LVDLINK(uint32_t val)
  215. {
  216. return ((val) << TC358775_LVCFG_LVDLINK__SHIFT) &
  217. TC358775_LVCFG_LVDLINK__MASK;
  218. }
  219. enum tc358775_ports {
  220. TC358775_DSI_IN,
  221. TC358775_LVDS_OUT0,
  222. TC358775_LVDS_OUT1,
  223. };
  224. enum tc3587x5_type {
  225. TC358765 = 0x65,
  226. TC358775 = 0x75,
  227. };
  228. struct tc_data {
  229. struct i2c_client *i2c;
  230. struct device *dev;
  231. struct drm_bridge bridge;
  232. struct drm_bridge *panel_bridge;
  233. struct device_node *host_node;
  234. struct mipi_dsi_device *dsi;
  235. u8 num_dsi_lanes;
  236. struct regulator *vdd;
  237. struct regulator *vddio;
  238. struct gpio_desc *reset_gpio;
  239. struct gpio_desc *stby_gpio;
  240. u8 lvds_link; /* single-link or dual-link */
  241. u8 bpc;
  242. enum tc3587x5_type type;
  243. };
  244. static inline struct tc_data *bridge_to_tc(struct drm_bridge *b)
  245. {
  246. return container_of(b, struct tc_data, bridge);
  247. }
  248. static void tc_bridge_pre_enable(struct drm_bridge *bridge)
  249. {
  250. struct tc_data *tc = bridge_to_tc(bridge);
  251. struct device *dev = &tc->dsi->dev;
  252. int ret;
  253. ret = regulator_enable(tc->vddio);
  254. if (ret < 0)
  255. dev_err(dev, "regulator vddio enable failed, %d\n", ret);
  256. usleep_range(10000, 11000);
  257. ret = regulator_enable(tc->vdd);
  258. if (ret < 0)
  259. dev_err(dev, "regulator vdd enable failed, %d\n", ret);
  260. usleep_range(10000, 11000);
  261. gpiod_set_value(tc->stby_gpio, 0);
  262. usleep_range(10000, 11000);
  263. gpiod_set_value(tc->reset_gpio, 0);
  264. usleep_range(10, 20);
  265. }
  266. static void tc_bridge_post_disable(struct drm_bridge *bridge)
  267. {
  268. struct tc_data *tc = bridge_to_tc(bridge);
  269. struct device *dev = &tc->dsi->dev;
  270. int ret;
  271. gpiod_set_value(tc->reset_gpio, 1);
  272. usleep_range(10, 20);
  273. gpiod_set_value(tc->stby_gpio, 1);
  274. usleep_range(10000, 11000);
  275. ret = regulator_disable(tc->vdd);
  276. if (ret < 0)
  277. dev_err(dev, "regulator vdd disable failed, %d\n", ret);
  278. usleep_range(10000, 11000);
  279. ret = regulator_disable(tc->vddio);
  280. if (ret < 0)
  281. dev_err(dev, "regulator vddio disable failed, %d\n", ret);
  282. usleep_range(10000, 11000);
  283. }
  284. static void d2l_read(struct i2c_client *i2c, u16 addr, u32 *val)
  285. {
  286. int ret;
  287. u8 buf_addr[2];
  288. put_unaligned_be16(addr, buf_addr);
  289. ret = i2c_master_send(i2c, buf_addr, sizeof(buf_addr));
  290. if (ret < 0)
  291. goto fail;
  292. ret = i2c_master_recv(i2c, (u8 *)val, sizeof(*val));
  293. if (ret < 0)
  294. goto fail;
  295. pr_debug("d2l: I2C : addr:%04x value:%08x\n", addr, *val);
  296. return;
  297. fail:
  298. dev_err(&i2c->dev, "Error %d reading from subaddress 0x%x\n",
  299. ret, addr);
  300. }
  301. static void d2l_write(struct i2c_client *i2c, u16 addr, u32 val)
  302. {
  303. u8 data[6];
  304. int ret;
  305. put_unaligned_be16(addr, data);
  306. put_unaligned_le32(val, data + 2);
  307. ret = i2c_master_send(i2c, data, ARRAY_SIZE(data));
  308. if (ret < 0)
  309. dev_err(&i2c->dev, "Error %d writing to subaddress 0x%x\n",
  310. ret, addr);
  311. }
  312. /* helper function to access bus_formats */
  313. static struct drm_connector *get_connector(struct drm_encoder *encoder)
  314. {
  315. struct drm_device *dev = encoder->dev;
  316. struct drm_connector *connector;
  317. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  318. if (connector->encoder == encoder)
  319. return connector;
  320. return NULL;
  321. }
  322. static void tc_bridge_enable(struct drm_bridge *bridge)
  323. {
  324. struct tc_data *tc = bridge_to_tc(bridge);
  325. u32 hback_porch, hsync_len, hfront_porch, hactive, htime1, htime2;
  326. u32 vback_porch, vsync_len, vfront_porch, vactive, vtime1, vtime2;
  327. u32 val = 0;
  328. u16 dsiclk, clkdiv, byteclk, t1, t2, t3, vsdelay;
  329. struct drm_display_mode *mode;
  330. struct drm_connector *connector = get_connector(bridge->encoder);
  331. mode = &bridge->encoder->crtc->state->adjusted_mode;
  332. hback_porch = mode->htotal - mode->hsync_end;
  333. hsync_len = mode->hsync_end - mode->hsync_start;
  334. vback_porch = mode->vtotal - mode->vsync_end;
  335. vsync_len = mode->vsync_end - mode->vsync_start;
  336. htime1 = (hback_porch << 16) + hsync_len;
  337. vtime1 = (vback_porch << 16) + vsync_len;
  338. hfront_porch = mode->hsync_start - mode->hdisplay;
  339. hactive = mode->hdisplay;
  340. vfront_porch = mode->vsync_start - mode->vdisplay;
  341. vactive = mode->vdisplay;
  342. htime2 = (hfront_porch << 16) + hactive;
  343. vtime2 = (vfront_porch << 16) + vactive;
  344. d2l_read(tc->i2c, IDREG, &val);
  345. dev_info(tc->dev, "DSI2LVDS Chip ID.%02x Revision ID. %02x **\n",
  346. (val >> 8) & 0xFF, val & 0xFF);
  347. d2l_write(tc->i2c, SYSRST, SYS_RST_REG | SYS_RST_DSIRX | SYS_RST_BM |
  348. SYS_RST_LCD | SYS_RST_I2CM);
  349. usleep_range(30000, 40000);
  350. d2l_write(tc->i2c, PPI_TX_RX_TA, TTA_GET | TTA_SURE);
  351. d2l_write(tc->i2c, PPI_LPTXTIMECNT, LPX_PERIOD);
  352. d2l_write(tc->i2c, PPI_D0S_CLRSIPOCOUNT, 3);
  353. d2l_write(tc->i2c, PPI_D1S_CLRSIPOCOUNT, 3);
  354. d2l_write(tc->i2c, PPI_D2S_CLRSIPOCOUNT, 3);
  355. d2l_write(tc->i2c, PPI_D3S_CLRSIPOCOUNT, 3);
  356. val = ((L0EN << tc->num_dsi_lanes) - L0EN) | DSI_CLEN_BIT;
  357. d2l_write(tc->i2c, PPI_LANEENABLE, val);
  358. d2l_write(tc->i2c, DSI_LANEENABLE, val);
  359. d2l_write(tc->i2c, PPI_STARTPPI, PPI_START_FUNCTION);
  360. d2l_write(tc->i2c, DSI_STARTDSI, DSI_RX_START);
  361. /* Video event mode vs pulse mode bit, does not exist for tc358775 */
  362. if (tc->type == TC358765)
  363. val = EVTMODE;
  364. else
  365. val = 0;
  366. if (tc->bpc == 8)
  367. val |= TC358775_VPCTRL_OPXLFMT(1);
  368. else /* bpc = 6; */
  369. val |= TC358775_VPCTRL_MSF(1);
  370. dsiclk = mode->crtc_clock * 3 * tc->bpc / tc->num_dsi_lanes / 1000;
  371. clkdiv = dsiclk / (tc->lvds_link == DUAL_LINK ? DIVIDE_BY_6 : DIVIDE_BY_3);
  372. byteclk = dsiclk / 4;
  373. t1 = hactive * (tc->bpc * 3 / 8) / tc->num_dsi_lanes;
  374. t2 = ((100000 / clkdiv)) * (hactive + hback_porch + hsync_len + hfront_porch) / 1000;
  375. t3 = ((t2 * byteclk) / 100) - (hactive * (tc->bpc * 3 / 8) /
  376. tc->num_dsi_lanes);
  377. vsdelay = (clkdiv * (t1 + t3) / byteclk) - hback_porch - hsync_len - hactive;
  378. val |= TC358775_VPCTRL_VSDELAY(vsdelay);
  379. d2l_write(tc->i2c, VPCTRL, val);
  380. d2l_write(tc->i2c, HTIM1, htime1);
  381. d2l_write(tc->i2c, VTIM1, vtime1);
  382. d2l_write(tc->i2c, HTIM2, htime2);
  383. d2l_write(tc->i2c, VTIM2, vtime2);
  384. d2l_write(tc->i2c, VFUEN, VFUEN_EN);
  385. d2l_write(tc->i2c, SYSRST, SYS_RST_LCD);
  386. d2l_write(tc->i2c, LVPHY0, LV_PHY0_PRBS_ON(4) | LV_PHY0_ND(6));
  387. dev_dbg(tc->dev, "bus_formats %04x bpc %d\n",
  388. connector->display_info.bus_formats[0],
  389. tc->bpc);
  390. if (connector->display_info.bus_formats[0] ==
  391. MEDIA_BUS_FMT_RGB888_1X7X4_SPWG) {
  392. /* VESA-24 */
  393. d2l_write(tc->i2c, LV_MX0003, LV_MX(LVI_R0, LVI_R1, LVI_R2, LVI_R3));
  394. d2l_write(tc->i2c, LV_MX0407, LV_MX(LVI_R4, LVI_R7, LVI_R5, LVI_G0));
  395. d2l_write(tc->i2c, LV_MX0811, LV_MX(LVI_G1, LVI_G2, LVI_G6, LVI_G7));
  396. d2l_write(tc->i2c, LV_MX1215, LV_MX(LVI_G3, LVI_G4, LVI_G5, LVI_B0));
  397. d2l_write(tc->i2c, LV_MX1619, LV_MX(LVI_B6, LVI_B7, LVI_B1, LVI_B2));
  398. d2l_write(tc->i2c, LV_MX2023, LV_MX(LVI_B3, LVI_B4, LVI_B5, LVI_L0));
  399. d2l_write(tc->i2c, LV_MX2427, LV_MX(LVI_HS, LVI_VS, LVI_DE, LVI_R6));
  400. } else {
  401. /* JEIDA-18 and JEIDA-24 */
  402. d2l_write(tc->i2c, LV_MX0003, LV_MX(LVI_R2, LVI_R3, LVI_R4, LVI_R5));
  403. d2l_write(tc->i2c, LV_MX0407, LV_MX(LVI_R6, LVI_R1, LVI_R7, LVI_G2));
  404. d2l_write(tc->i2c, LV_MX0811, LV_MX(LVI_G3, LVI_G4, LVI_G0, LVI_G1));
  405. d2l_write(tc->i2c, LV_MX1215, LV_MX(LVI_G5, LVI_G6, LVI_G7, LVI_B2));
  406. d2l_write(tc->i2c, LV_MX1619, LV_MX(LVI_B0, LVI_B1, LVI_B3, LVI_B4));
  407. d2l_write(tc->i2c, LV_MX2023, LV_MX(LVI_B5, LVI_B6, LVI_B7, LVI_L0));
  408. d2l_write(tc->i2c, LV_MX2427, LV_MX(LVI_HS, LVI_VS, LVI_DE, LVI_R0));
  409. }
  410. d2l_write(tc->i2c, VFUEN, VFUEN_EN);
  411. val = LVCFG_LVEN_BIT;
  412. if (tc->lvds_link == DUAL_LINK) {
  413. val |= TC358775_LVCFG_LVDLINK(1);
  414. val |= TC358775_LVCFG_PCLKDIV(DIVIDE_BY_6);
  415. } else {
  416. val |= TC358775_LVCFG_PCLKDIV(DIVIDE_BY_3);
  417. }
  418. d2l_write(tc->i2c, LVCFG, val);
  419. }
  420. static enum drm_mode_status
  421. tc_mode_valid(struct drm_bridge *bridge,
  422. const struct drm_display_info *info,
  423. const struct drm_display_mode *mode)
  424. {
  425. struct tc_data *tc = bridge_to_tc(bridge);
  426. /*
  427. * Maximum pixel clock speed 135MHz for single-link
  428. * 270MHz for dual-link
  429. */
  430. if ((mode->clock > 135000 && tc->lvds_link == SINGLE_LINK) ||
  431. (mode->clock > 270000 && tc->lvds_link == DUAL_LINK))
  432. return MODE_CLOCK_HIGH;
  433. switch (info->bus_formats[0]) {
  434. case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
  435. case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
  436. /* RGB888 */
  437. tc->bpc = 8;
  438. break;
  439. case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
  440. /* RGB666 */
  441. tc->bpc = 6;
  442. break;
  443. default:
  444. dev_warn(tc->dev,
  445. "unsupported LVDS bus format 0x%04x\n",
  446. info->bus_formats[0]);
  447. return MODE_NOMODE;
  448. }
  449. return MODE_OK;
  450. }
  451. static int tc358775_parse_dt(struct device_node *np, struct tc_data *tc)
  452. {
  453. struct device_node *endpoint;
  454. struct device_node *remote;
  455. int dsi_lanes = -1;
  456. endpoint = of_graph_get_endpoint_by_regs(tc->dev->of_node,
  457. TC358775_DSI_IN, -1);
  458. dsi_lanes = drm_of_get_data_lanes_count(endpoint, 1, 4);
  459. /* Quirk old dtb: Use data lanes from the DSI host side instead of bridge */
  460. if (dsi_lanes == -EINVAL || dsi_lanes == -ENODEV) {
  461. remote = of_graph_get_remote_endpoint(endpoint);
  462. dsi_lanes = drm_of_get_data_lanes_count(remote, 1, 4);
  463. of_node_put(remote);
  464. if (dsi_lanes >= 1)
  465. dev_warn(tc->dev, "no dsi-lanes for the bridge, using host lanes\n");
  466. }
  467. of_node_put(endpoint);
  468. if (dsi_lanes < 0)
  469. return dsi_lanes;
  470. tc->num_dsi_lanes = dsi_lanes;
  471. tc->host_node = of_graph_get_remote_node(np, 0, 0);
  472. if (!tc->host_node)
  473. return -ENODEV;
  474. of_node_put(tc->host_node);
  475. tc->lvds_link = SINGLE_LINK;
  476. endpoint = of_graph_get_endpoint_by_regs(tc->dev->of_node,
  477. TC358775_LVDS_OUT1, -1);
  478. if (endpoint) {
  479. remote = of_graph_get_remote_port_parent(endpoint);
  480. of_node_put(endpoint);
  481. if (remote) {
  482. if (of_device_is_available(remote))
  483. tc->lvds_link = DUAL_LINK;
  484. of_node_put(remote);
  485. }
  486. }
  487. dev_dbg(tc->dev, "no.of dsi lanes: %d\n", tc->num_dsi_lanes);
  488. dev_dbg(tc->dev, "operating in %d-link mode\n", tc->lvds_link);
  489. return 0;
  490. }
  491. static int tc_bridge_attach(struct drm_bridge *bridge,
  492. enum drm_bridge_attach_flags flags)
  493. {
  494. struct tc_data *tc = bridge_to_tc(bridge);
  495. /* Attach the panel-bridge to the dsi bridge */
  496. return drm_bridge_attach(bridge->encoder, tc->panel_bridge,
  497. &tc->bridge, flags);
  498. }
  499. static const struct drm_bridge_funcs tc_bridge_funcs = {
  500. .attach = tc_bridge_attach,
  501. .pre_enable = tc_bridge_pre_enable,
  502. .enable = tc_bridge_enable,
  503. .mode_valid = tc_mode_valid,
  504. .post_disable = tc_bridge_post_disable,
  505. };
  506. static int tc_attach_host(struct tc_data *tc)
  507. {
  508. struct device *dev = &tc->i2c->dev;
  509. struct mipi_dsi_host *host;
  510. struct mipi_dsi_device *dsi;
  511. int ret;
  512. const struct mipi_dsi_device_info info = { .type = "tc358775",
  513. .channel = 0,
  514. .node = NULL,
  515. };
  516. host = of_find_mipi_dsi_host_by_node(tc->host_node);
  517. if (!host)
  518. return dev_err_probe(dev, -EPROBE_DEFER, "failed to find dsi host\n");
  519. dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
  520. if (IS_ERR(dsi)) {
  521. dev_err(dev, "failed to create dsi device\n");
  522. return PTR_ERR(dsi);
  523. }
  524. tc->dsi = dsi;
  525. dsi->lanes = tc->num_dsi_lanes;
  526. dsi->format = MIPI_DSI_FMT_RGB888;
  527. dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
  528. MIPI_DSI_MODE_LPM;
  529. /*
  530. * The hs_rate and lp_rate are data rate values. The HS mode is
  531. * differential, while the LP mode is single ended. As the HS mode
  532. * uses DDR, the DSI clock frequency is half the hs_rate. The 10 Mbs
  533. * data rate for LP mode is not specified in the bridge data sheet,
  534. * but seems to be part of the MIPI DSI spec.
  535. */
  536. if (tc->type == TC358765)
  537. dsi->hs_rate = 800000000;
  538. else
  539. dsi->hs_rate = 1000000000;
  540. dsi->lp_rate = 10000000;
  541. ret = devm_mipi_dsi_attach(dev, dsi);
  542. if (ret < 0) {
  543. dev_err(dev, "failed to attach dsi to host\n");
  544. return ret;
  545. }
  546. return 0;
  547. }
  548. static int tc_probe(struct i2c_client *client)
  549. {
  550. struct device *dev = &client->dev;
  551. struct tc_data *tc;
  552. int ret;
  553. tc = devm_kzalloc(dev, sizeof(*tc), GFP_KERNEL);
  554. if (!tc)
  555. return -ENOMEM;
  556. tc->dev = dev;
  557. tc->i2c = client;
  558. tc->type = (enum tc3587x5_type)(unsigned long)of_device_get_match_data(dev);
  559. tc->panel_bridge = devm_drm_of_get_bridge(dev, dev->of_node,
  560. TC358775_LVDS_OUT0, 0);
  561. if (IS_ERR(tc->panel_bridge))
  562. return PTR_ERR(tc->panel_bridge);
  563. ret = tc358775_parse_dt(dev->of_node, tc);
  564. if (ret)
  565. return ret;
  566. tc->vddio = devm_regulator_get(dev, "vddio-supply");
  567. if (IS_ERR(tc->vddio)) {
  568. ret = PTR_ERR(tc->vddio);
  569. dev_err(dev, "vddio-supply not found\n");
  570. return ret;
  571. }
  572. tc->vdd = devm_regulator_get(dev, "vdd-supply");
  573. if (IS_ERR(tc->vdd)) {
  574. ret = PTR_ERR(tc->vdd);
  575. dev_err(dev, "vdd-supply not found\n");
  576. return ret;
  577. }
  578. tc->stby_gpio = devm_gpiod_get_optional(dev, "stby", GPIOD_OUT_HIGH);
  579. if (IS_ERR(tc->stby_gpio))
  580. return PTR_ERR(tc->stby_gpio);
  581. tc->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  582. if (IS_ERR(tc->reset_gpio)) {
  583. ret = PTR_ERR(tc->reset_gpio);
  584. dev_err(dev, "cannot get reset-gpios %d\n", ret);
  585. return ret;
  586. }
  587. tc->bridge.funcs = &tc_bridge_funcs;
  588. tc->bridge.of_node = dev->of_node;
  589. tc->bridge.pre_enable_prev_first = true;
  590. drm_bridge_add(&tc->bridge);
  591. i2c_set_clientdata(client, tc);
  592. ret = tc_attach_host(tc);
  593. if (ret)
  594. goto err_bridge_remove;
  595. return 0;
  596. err_bridge_remove:
  597. drm_bridge_remove(&tc->bridge);
  598. return ret;
  599. }
  600. static void tc_remove(struct i2c_client *client)
  601. {
  602. struct tc_data *tc = i2c_get_clientdata(client);
  603. drm_bridge_remove(&tc->bridge);
  604. }
  605. static const struct i2c_device_id tc358775_i2c_ids[] = {
  606. { "tc358765", TC358765, },
  607. { "tc358775", TC358775, },
  608. { }
  609. };
  610. MODULE_DEVICE_TABLE(i2c, tc358775_i2c_ids);
  611. static const struct of_device_id tc358775_of_ids[] = {
  612. { .compatible = "toshiba,tc358765", .data = (void *)TC358765, },
  613. { .compatible = "toshiba,tc358775", .data = (void *)TC358775, },
  614. { }
  615. };
  616. MODULE_DEVICE_TABLE(of, tc358775_of_ids);
  617. static struct i2c_driver tc358775_driver = {
  618. .driver = {
  619. .name = "tc358775",
  620. .of_match_table = tc358775_of_ids,
  621. },
  622. .id_table = tc358775_i2c_ids,
  623. .probe = tc_probe,
  624. .remove = tc_remove,
  625. };
  626. module_i2c_driver(tc358775_driver);
  627. MODULE_AUTHOR("Vinay Simha BN <simhavcs@gmail.com>");
  628. MODULE_DESCRIPTION("TC358775 DSI/LVDS bridge driver");
  629. MODULE_LICENSE("GPL v2");