lontium-lt8912b.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/delay.h>
  7. #include <linux/gpio/consumer.h>
  8. #include <linux/i2c.h>
  9. #include <linux/media-bus-format.h>
  10. #include <linux/regmap.h>
  11. #include <drm/drm_probe_helper.h>
  12. #include <drm/drm_atomic_helper.h>
  13. #include <drm/drm_edid.h>
  14. #include <drm/drm_mipi_dsi.h>
  15. #include <drm/drm_of.h>
  16. #include <video/videomode.h>
  17. #define I2C_MAIN 0
  18. #define I2C_ADDR_MAIN 0x48
  19. #define I2C_CEC_DSI 1
  20. #define I2C_ADDR_CEC_DSI 0x49
  21. #define I2C_MAX_IDX 2
  22. struct lt8912 {
  23. struct device *dev;
  24. struct drm_bridge bridge;
  25. struct drm_connector connector;
  26. struct i2c_client *i2c_client[I2C_MAX_IDX];
  27. struct regmap *regmap[I2C_MAX_IDX];
  28. struct device_node *host_node;
  29. struct drm_bridge *hdmi_port;
  30. struct mipi_dsi_device *dsi;
  31. struct gpio_desc *gp_reset;
  32. struct videomode mode;
  33. u8 data_lanes;
  34. bool is_power_on;
  35. };
  36. static int lt8912_write_init_config(struct lt8912 *lt)
  37. {
  38. const struct reg_sequence seq[] = {
  39. /* Digital clock en*/
  40. {0x08, 0xff},
  41. {0x09, 0xff},
  42. {0x0a, 0xff},
  43. {0x0b, 0x7c},
  44. {0x0c, 0xff},
  45. {0x42, 0x04},
  46. /*Tx Analog*/
  47. {0x31, 0xb1},
  48. {0x32, 0xb1},
  49. {0x33, 0x0e},
  50. {0x37, 0x00},
  51. {0x38, 0x22},
  52. {0x60, 0x82},
  53. /*Cbus Analog*/
  54. {0x39, 0x45},
  55. {0x3a, 0x00},
  56. {0x3b, 0x00},
  57. /*HDMI Pll Analog*/
  58. {0x44, 0x31},
  59. {0x55, 0x44},
  60. {0x57, 0x01},
  61. {0x5a, 0x02},
  62. /*MIPI Analog*/
  63. {0x3e, 0xd6},
  64. {0x3f, 0xd4},
  65. {0x41, 0x3c},
  66. {0xB2, 0x00},
  67. };
  68. return regmap_multi_reg_write(lt->regmap[I2C_MAIN], seq, ARRAY_SIZE(seq));
  69. }
  70. static int lt8912_write_mipi_basic_config(struct lt8912 *lt)
  71. {
  72. const struct reg_sequence seq[] = {
  73. {0x12, 0x04},
  74. {0x14, 0x00},
  75. {0x15, 0x00},
  76. {0x1a, 0x03},
  77. {0x1b, 0x03},
  78. };
  79. return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
  80. };
  81. static int lt8912_write_dds_config(struct lt8912 *lt)
  82. {
  83. const struct reg_sequence seq[] = {
  84. {0x4e, 0xff},
  85. {0x4f, 0x56},
  86. {0x50, 0x69},
  87. {0x51, 0x80},
  88. {0x1f, 0x5e},
  89. {0x20, 0x01},
  90. {0x21, 0x2c},
  91. {0x22, 0x01},
  92. {0x23, 0xfa},
  93. {0x24, 0x00},
  94. {0x25, 0xc8},
  95. {0x26, 0x00},
  96. {0x27, 0x5e},
  97. {0x28, 0x01},
  98. {0x29, 0x2c},
  99. {0x2a, 0x01},
  100. {0x2b, 0xfa},
  101. {0x2c, 0x00},
  102. {0x2d, 0xc8},
  103. {0x2e, 0x00},
  104. {0x42, 0x64},
  105. {0x43, 0x00},
  106. {0x44, 0x04},
  107. {0x45, 0x00},
  108. {0x46, 0x59},
  109. {0x47, 0x00},
  110. {0x48, 0xf2},
  111. {0x49, 0x06},
  112. {0x4a, 0x00},
  113. {0x4b, 0x72},
  114. {0x4c, 0x45},
  115. {0x4d, 0x00},
  116. {0x52, 0x08},
  117. {0x53, 0x00},
  118. {0x54, 0xb2},
  119. {0x55, 0x00},
  120. {0x56, 0xe4},
  121. {0x57, 0x0d},
  122. {0x58, 0x00},
  123. {0x59, 0xe4},
  124. {0x5a, 0x8a},
  125. {0x5b, 0x00},
  126. {0x5c, 0x34},
  127. {0x1e, 0x4f},
  128. {0x51, 0x00},
  129. };
  130. return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
  131. }
  132. static int lt8912_write_rxlogicres_config(struct lt8912 *lt)
  133. {
  134. int ret;
  135. ret = regmap_write(lt->regmap[I2C_MAIN], 0x03, 0x7f);
  136. usleep_range(10000, 20000);
  137. ret |= regmap_write(lt->regmap[I2C_MAIN], 0x03, 0xff);
  138. return ret;
  139. };
  140. /* enable LVDS output with some hardcoded configuration, not required for the HDMI output */
  141. static int lt8912_write_lvds_config(struct lt8912 *lt)
  142. {
  143. const struct reg_sequence seq[] = {
  144. // lvds power up
  145. {0x44, 0x30},
  146. {0x51, 0x05},
  147. // core pll bypass
  148. {0x50, 0x24}, // cp=50uA
  149. {0x51, 0x2d}, // Pix_clk as reference, second order passive LPF PLL
  150. {0x52, 0x04}, // loopdiv=0, use second-order PLL
  151. {0x69, 0x0e}, // CP_PRESET_DIV_RATIO
  152. {0x69, 0x8e},
  153. {0x6a, 0x00},
  154. {0x6c, 0xb8}, // RGD_CP_SOFT_K_EN,RGD_CP_SOFT_K[13:8]
  155. {0x6b, 0x51},
  156. {0x04, 0xfb}, // core pll reset
  157. {0x04, 0xff},
  158. // scaler bypass
  159. {0x7f, 0x00}, // disable scaler
  160. {0xa8, 0x13}, // 0x13: JEIDA, 0x33: VESA
  161. {0x02, 0xf7}, // lvds pll reset
  162. {0x02, 0xff},
  163. {0x03, 0xcf},
  164. {0x03, 0xff},
  165. };
  166. return regmap_multi_reg_write(lt->regmap[I2C_MAIN], seq, ARRAY_SIZE(seq));
  167. };
  168. static inline struct lt8912 *bridge_to_lt8912(struct drm_bridge *b)
  169. {
  170. return container_of(b, struct lt8912, bridge);
  171. }
  172. static inline struct lt8912 *connector_to_lt8912(struct drm_connector *c)
  173. {
  174. return container_of(c, struct lt8912, connector);
  175. }
  176. static const struct regmap_config lt8912_regmap_config = {
  177. .reg_bits = 8,
  178. .val_bits = 8,
  179. .max_register = 0xff,
  180. };
  181. static int lt8912_init_i2c(struct lt8912 *lt, struct i2c_client *client)
  182. {
  183. unsigned int i;
  184. /*
  185. * At this time we only initialize 2 chips, but the lt8912 provides
  186. * a third interface for the audio over HDMI configuration.
  187. */
  188. struct i2c_board_info info[] = {
  189. { I2C_BOARD_INFO("lt8912p0", I2C_ADDR_MAIN), },
  190. { I2C_BOARD_INFO("lt8912p1", I2C_ADDR_CEC_DSI), },
  191. };
  192. if (!lt)
  193. return -ENODEV;
  194. for (i = 0; i < ARRAY_SIZE(info); i++) {
  195. if (i > 0) {
  196. lt->i2c_client[i] = i2c_new_dummy_device(client->adapter,
  197. info[i].addr);
  198. if (IS_ERR(lt->i2c_client[i]))
  199. return PTR_ERR(lt->i2c_client[i]);
  200. }
  201. lt->regmap[i] = devm_regmap_init_i2c(lt->i2c_client[i],
  202. &lt8912_regmap_config);
  203. if (IS_ERR(lt->regmap[i]))
  204. return PTR_ERR(lt->regmap[i]);
  205. }
  206. return 0;
  207. }
  208. static int lt8912_free_i2c(struct lt8912 *lt)
  209. {
  210. unsigned int i;
  211. for (i = 1; i < I2C_MAX_IDX; i++)
  212. i2c_unregister_device(lt->i2c_client[i]);
  213. return 0;
  214. }
  215. static int lt8912_hard_power_on(struct lt8912 *lt)
  216. {
  217. gpiod_set_value_cansleep(lt->gp_reset, 0);
  218. msleep(20);
  219. return 0;
  220. }
  221. static void lt8912_hard_power_off(struct lt8912 *lt)
  222. {
  223. gpiod_set_value_cansleep(lt->gp_reset, 1);
  224. msleep(20);
  225. lt->is_power_on = false;
  226. }
  227. static int lt8912_video_setup(struct lt8912 *lt)
  228. {
  229. u32 hactive, h_total, hpw, hfp, hbp;
  230. u32 vactive, v_total, vpw, vfp, vbp;
  231. u8 settle = 0x08;
  232. int ret, hsync_activehigh, vsync_activehigh;
  233. if (!lt)
  234. return -EINVAL;
  235. hactive = lt->mode.hactive;
  236. hfp = lt->mode.hfront_porch;
  237. hpw = lt->mode.hsync_len;
  238. hbp = lt->mode.hback_porch;
  239. h_total = hactive + hfp + hpw + hbp;
  240. hsync_activehigh = lt->mode.flags & DISPLAY_FLAGS_HSYNC_HIGH;
  241. vactive = lt->mode.vactive;
  242. vfp = lt->mode.vfront_porch;
  243. vpw = lt->mode.vsync_len;
  244. vbp = lt->mode.vback_porch;
  245. v_total = vactive + vfp + vpw + vbp;
  246. vsync_activehigh = lt->mode.flags & DISPLAY_FLAGS_VSYNC_HIGH;
  247. if (vactive <= 600)
  248. settle = 0x04;
  249. else if (vactive == 1080)
  250. settle = 0x0a;
  251. ret = regmap_write(lt->regmap[I2C_CEC_DSI], 0x10, 0x01);
  252. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x11, settle);
  253. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x18, hpw);
  254. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x19, vpw);
  255. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1c, hactive & 0xff);
  256. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1d, hactive >> 8);
  257. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x2f, 0x0c);
  258. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x34, h_total & 0xff);
  259. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x35, h_total >> 8);
  260. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x36, v_total & 0xff);
  261. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x37, v_total >> 8);
  262. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x38, vbp & 0xff);
  263. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x39, vbp >> 8);
  264. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3a, vfp & 0xff);
  265. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3b, vfp >> 8);
  266. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3c, hbp & 0xff);
  267. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3d, hbp >> 8);
  268. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3e, hfp & 0xff);
  269. ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3f, hfp >> 8);
  270. ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xab, BIT(0),
  271. vsync_activehigh ? BIT(0) : 0);
  272. ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xab, BIT(1),
  273. hsync_activehigh ? BIT(1) : 0);
  274. ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xb2, BIT(0),
  275. lt->connector.display_info.is_hdmi ? BIT(0) : 0);
  276. return ret;
  277. }
  278. static int lt8912_soft_power_on(struct lt8912 *lt)
  279. {
  280. if (!lt->is_power_on) {
  281. u32 lanes = lt->data_lanes;
  282. lt8912_write_init_config(lt);
  283. regmap_write(lt->regmap[I2C_CEC_DSI], 0x13, lanes & 3);
  284. lt8912_write_mipi_basic_config(lt);
  285. lt->is_power_on = true;
  286. }
  287. return 0;
  288. }
  289. static int lt8912_video_on(struct lt8912 *lt)
  290. {
  291. int ret;
  292. ret = lt8912_video_setup(lt);
  293. if (ret < 0)
  294. goto end;
  295. ret = lt8912_write_dds_config(lt);
  296. if (ret < 0)
  297. goto end;
  298. ret = lt8912_write_rxlogicres_config(lt);
  299. if (ret < 0)
  300. goto end;
  301. ret = lt8912_write_lvds_config(lt);
  302. if (ret < 0)
  303. goto end;
  304. end:
  305. return ret;
  306. }
  307. static enum drm_connector_status lt8912_check_cable_status(struct lt8912 *lt)
  308. {
  309. int ret;
  310. unsigned int reg_val;
  311. ret = regmap_read(lt->regmap[I2C_MAIN], 0xC1, &reg_val);
  312. if (ret)
  313. return connector_status_unknown;
  314. if (reg_val & BIT(7))
  315. return connector_status_connected;
  316. return connector_status_disconnected;
  317. }
  318. static enum drm_connector_status
  319. lt8912_connector_detect(struct drm_connector *connector, bool force)
  320. {
  321. struct lt8912 *lt = connector_to_lt8912(connector);
  322. if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
  323. return drm_bridge_detect(lt->hdmi_port);
  324. return lt8912_check_cable_status(lt);
  325. }
  326. static const struct drm_connector_funcs lt8912_connector_funcs = {
  327. .detect = lt8912_connector_detect,
  328. .fill_modes = drm_helper_probe_single_connector_modes,
  329. .destroy = drm_connector_cleanup,
  330. .reset = drm_atomic_helper_connector_reset,
  331. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  332. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  333. };
  334. static enum drm_mode_status
  335. lt8912_connector_mode_valid(struct drm_connector *connector,
  336. struct drm_display_mode *mode)
  337. {
  338. if (mode->clock > 150000)
  339. return MODE_CLOCK_HIGH;
  340. if (mode->hdisplay > 1920)
  341. return MODE_BAD_HVALUE;
  342. if (mode->vdisplay > 1080)
  343. return MODE_BAD_VVALUE;
  344. return MODE_OK;
  345. }
  346. static int lt8912_connector_get_modes(struct drm_connector *connector)
  347. {
  348. struct edid *edid;
  349. int ret = -1;
  350. int num = 0;
  351. struct lt8912 *lt = connector_to_lt8912(connector);
  352. u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  353. edid = drm_bridge_get_edid(lt->hdmi_port, connector);
  354. if (edid) {
  355. drm_connector_update_edid_property(connector, edid);
  356. num = drm_add_edid_modes(connector, edid);
  357. } else {
  358. return ret;
  359. }
  360. ret = drm_display_info_set_bus_formats(&connector->display_info,
  361. &bus_format, 1);
  362. if (ret)
  363. num = ret;
  364. kfree(edid);
  365. return num;
  366. }
  367. static const struct drm_connector_helper_funcs lt8912_connector_helper_funcs = {
  368. .get_modes = lt8912_connector_get_modes,
  369. .mode_valid = lt8912_connector_mode_valid,
  370. };
  371. static void lt8912_bridge_mode_set(struct drm_bridge *bridge,
  372. const struct drm_display_mode *mode,
  373. const struct drm_display_mode *adj)
  374. {
  375. struct lt8912 *lt = bridge_to_lt8912(bridge);
  376. drm_display_mode_to_videomode(adj, &lt->mode);
  377. }
  378. static void lt8912_bridge_enable(struct drm_bridge *bridge)
  379. {
  380. struct lt8912 *lt = bridge_to_lt8912(bridge);
  381. lt8912_video_on(lt);
  382. }
  383. static int lt8912_attach_dsi(struct lt8912 *lt)
  384. {
  385. struct device *dev = lt->dev;
  386. struct mipi_dsi_host *host;
  387. struct mipi_dsi_device *dsi;
  388. int ret = -1;
  389. const struct mipi_dsi_device_info info = { .type = "lt8912",
  390. .channel = 0,
  391. .node = NULL,
  392. };
  393. host = of_find_mipi_dsi_host_by_node(lt->host_node);
  394. if (!host) {
  395. dev_err(dev, "failed to find dsi host\n");
  396. return -EPROBE_DEFER;
  397. }
  398. dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
  399. if (IS_ERR(dsi)) {
  400. ret = PTR_ERR(dsi);
  401. dev_err(dev, "failed to create dsi device (%d)\n", ret);
  402. return ret;
  403. }
  404. lt->dsi = dsi;
  405. dsi->lanes = lt->data_lanes;
  406. dsi->format = MIPI_DSI_FMT_RGB888;
  407. dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
  408. MIPI_DSI_MODE_LPM |
  409. MIPI_DSI_MODE_NO_EOT_PACKET;
  410. ret = devm_mipi_dsi_attach(dev, dsi);
  411. if (ret < 0) {
  412. dev_err(dev, "failed to attach dsi to host\n");
  413. return ret;
  414. }
  415. return 0;
  416. }
  417. static void lt8912_bridge_hpd_cb(void *data, enum drm_connector_status status)
  418. {
  419. struct lt8912 *lt = data;
  420. if (lt->bridge.dev)
  421. drm_helper_hpd_irq_event(lt->bridge.dev);
  422. }
  423. static int lt8912_bridge_connector_init(struct drm_bridge *bridge)
  424. {
  425. int ret;
  426. struct lt8912 *lt = bridge_to_lt8912(bridge);
  427. struct drm_connector *connector = &lt->connector;
  428. if (lt->hdmi_port->ops & DRM_BRIDGE_OP_HPD) {
  429. drm_bridge_hpd_enable(lt->hdmi_port, lt8912_bridge_hpd_cb, lt);
  430. connector->polled = DRM_CONNECTOR_POLL_HPD;
  431. } else {
  432. connector->polled = DRM_CONNECTOR_POLL_CONNECT |
  433. DRM_CONNECTOR_POLL_DISCONNECT;
  434. }
  435. ret = drm_connector_init(bridge->dev, connector,
  436. &lt8912_connector_funcs,
  437. lt->hdmi_port->type);
  438. if (ret)
  439. goto exit;
  440. drm_connector_helper_add(connector, &lt8912_connector_helper_funcs);
  441. connector->dpms = DRM_MODE_DPMS_OFF;
  442. drm_connector_attach_encoder(connector, bridge->encoder);
  443. exit:
  444. return ret;
  445. }
  446. static int lt8912_bridge_attach(struct drm_bridge *bridge,
  447. enum drm_bridge_attach_flags flags)
  448. {
  449. struct lt8912 *lt = bridge_to_lt8912(bridge);
  450. int ret;
  451. ret = drm_bridge_attach(bridge->encoder, lt->hdmi_port, bridge,
  452. DRM_BRIDGE_ATTACH_NO_CONNECTOR);
  453. if (ret < 0) {
  454. dev_err(lt->dev, "Failed to attach next bridge (%d)\n", ret);
  455. return ret;
  456. }
  457. if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
  458. ret = lt8912_bridge_connector_init(bridge);
  459. if (ret) {
  460. dev_err(lt->dev, "Failed to init bridge ! (%d)\n", ret);
  461. return ret;
  462. }
  463. }
  464. ret = lt8912_hard_power_on(lt);
  465. if (ret)
  466. return ret;
  467. ret = lt8912_soft_power_on(lt);
  468. if (ret)
  469. goto error;
  470. return 0;
  471. error:
  472. lt8912_hard_power_off(lt);
  473. return ret;
  474. }
  475. static void lt8912_bridge_detach(struct drm_bridge *bridge)
  476. {
  477. struct lt8912 *lt = bridge_to_lt8912(bridge);
  478. lt8912_hard_power_off(lt);
  479. if (lt->connector.dev && lt->hdmi_port->ops & DRM_BRIDGE_OP_HPD)
  480. drm_bridge_hpd_disable(lt->hdmi_port);
  481. }
  482. static enum drm_connector_status
  483. lt8912_bridge_detect(struct drm_bridge *bridge)
  484. {
  485. struct lt8912 *lt = bridge_to_lt8912(bridge);
  486. if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
  487. return drm_bridge_detect(lt->hdmi_port);
  488. return lt8912_check_cable_status(lt);
  489. }
  490. static struct edid *lt8912_bridge_get_edid(struct drm_bridge *bridge,
  491. struct drm_connector *connector)
  492. {
  493. struct lt8912 *lt = bridge_to_lt8912(bridge);
  494. /*
  495. * edid must be read through the ddc bus but it must be
  496. * given to the hdmi connector node.
  497. */
  498. if (lt->hdmi_port->ops & DRM_BRIDGE_OP_EDID)
  499. return drm_bridge_get_edid(lt->hdmi_port, connector);
  500. dev_warn(lt->dev, "The connected bridge does not supports DRM_BRIDGE_OP_EDID\n");
  501. return NULL;
  502. }
  503. static const struct drm_bridge_funcs lt8912_bridge_funcs = {
  504. .attach = lt8912_bridge_attach,
  505. .detach = lt8912_bridge_detach,
  506. .mode_set = lt8912_bridge_mode_set,
  507. .enable = lt8912_bridge_enable,
  508. .detect = lt8912_bridge_detect,
  509. .get_edid = lt8912_bridge_get_edid,
  510. };
  511. static int lt8912_parse_dt(struct lt8912 *lt)
  512. {
  513. struct gpio_desc *gp_reset;
  514. struct device *dev = lt->dev;
  515. int ret;
  516. int data_lanes;
  517. struct device_node *port_node;
  518. gp_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  519. if (IS_ERR(gp_reset)) {
  520. ret = PTR_ERR(gp_reset);
  521. if (ret != -EPROBE_DEFER)
  522. dev_err(dev, "Failed to get reset gpio: %d\n", ret);
  523. return ret;
  524. }
  525. lt->gp_reset = gp_reset;
  526. data_lanes = drm_of_get_data_lanes_count_ep(dev->of_node, 0, -1, 1, 4);
  527. if (data_lanes < 0) {
  528. dev_err(lt->dev, "%s: Bad data-lanes property\n", __func__);
  529. return data_lanes;
  530. }
  531. lt->data_lanes = data_lanes;
  532. lt->host_node = of_graph_get_remote_node(dev->of_node, 0, -1);
  533. if (!lt->host_node) {
  534. dev_err(lt->dev, "%s: Failed to get remote port\n", __func__);
  535. return -ENODEV;
  536. }
  537. port_node = of_graph_get_remote_node(dev->of_node, 1, -1);
  538. if (!port_node) {
  539. dev_err(lt->dev, "%s: Failed to get connector port\n", __func__);
  540. ret = -ENODEV;
  541. goto err_free_host_node;
  542. }
  543. lt->hdmi_port = of_drm_find_bridge(port_node);
  544. if (!lt->hdmi_port) {
  545. ret = -EPROBE_DEFER;
  546. dev_err_probe(lt->dev, ret, "%s: Failed to get hdmi port\n", __func__);
  547. goto err_free_host_node;
  548. }
  549. if (!of_device_is_compatible(port_node, "hdmi-connector")) {
  550. dev_err(lt->dev, "%s: Failed to get hdmi port\n", __func__);
  551. ret = -EINVAL;
  552. goto err_free_host_node;
  553. }
  554. of_node_put(port_node);
  555. return 0;
  556. err_free_host_node:
  557. of_node_put(port_node);
  558. of_node_put(lt->host_node);
  559. return ret;
  560. }
  561. static int lt8912_put_dt(struct lt8912 *lt)
  562. {
  563. of_node_put(lt->host_node);
  564. return 0;
  565. }
  566. static int lt8912_probe(struct i2c_client *client,
  567. const struct i2c_device_id *id)
  568. {
  569. static struct lt8912 *lt;
  570. int ret = 0;
  571. struct device *dev = &client->dev;
  572. lt = devm_kzalloc(dev, sizeof(struct lt8912), GFP_KERNEL);
  573. if (!lt)
  574. return -ENOMEM;
  575. lt->dev = dev;
  576. lt->i2c_client[0] = client;
  577. ret = lt8912_parse_dt(lt);
  578. if (ret)
  579. goto err_dt_parse;
  580. ret = lt8912_init_i2c(lt, client);
  581. if (ret)
  582. goto err_i2c;
  583. i2c_set_clientdata(client, lt);
  584. lt->bridge.funcs = &lt8912_bridge_funcs;
  585. lt->bridge.of_node = dev->of_node;
  586. lt->bridge.ops = (DRM_BRIDGE_OP_EDID |
  587. DRM_BRIDGE_OP_DETECT);
  588. drm_bridge_add(&lt->bridge);
  589. ret = lt8912_attach_dsi(lt);
  590. if (ret)
  591. goto err_attach;
  592. return 0;
  593. err_attach:
  594. drm_bridge_remove(&lt->bridge);
  595. lt8912_free_i2c(lt);
  596. err_i2c:
  597. lt8912_put_dt(lt);
  598. err_dt_parse:
  599. return ret;
  600. }
  601. static void lt8912_remove(struct i2c_client *client)
  602. {
  603. struct lt8912 *lt = i2c_get_clientdata(client);
  604. drm_bridge_remove(&lt->bridge);
  605. lt8912_free_i2c(lt);
  606. lt8912_put_dt(lt);
  607. }
  608. static const struct of_device_id lt8912_dt_match[] = {
  609. {.compatible = "lontium,lt8912b"},
  610. {}
  611. };
  612. MODULE_DEVICE_TABLE(of, lt8912_dt_match);
  613. static const struct i2c_device_id lt8912_id[] = {
  614. {"lt8912", 0},
  615. {},
  616. };
  617. MODULE_DEVICE_TABLE(i2c, lt8912_id);
  618. static struct i2c_driver lt8912_i2c_driver = {
  619. .driver = {
  620. .name = "lt8912",
  621. .of_match_table = lt8912_dt_match,
  622. },
  623. .probe = lt8912_probe,
  624. .remove = lt8912_remove,
  625. .id_table = lt8912_id,
  626. };
  627. module_i2c_driver(lt8912_i2c_driver);
  628. MODULE_AUTHOR("Adrien Grassein <[email protected]>");
  629. MODULE_DESCRIPTION("lt8912 drm driver");
  630. MODULE_LICENSE("GPL v2");