tc358764.c 14 KB

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