parade-ps8640.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016 MediaTek Inc.
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/err.h>
  7. #include <linux/gpio/consumer.h>
  8. #include <linux/i2c.h>
  9. #include <linux/module.h>
  10. #include <linux/of_graph.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <drm/drm_bridge.h>
  13. #include <drm/drm_mipi_dsi.h>
  14. #include <drm/drm_of.h>
  15. #include <drm/drm_panel.h>
  16. #include <drm/drm_print.h>
  17. #define PAGE2_GPIO_H 0xa7
  18. #define PS_GPIO9 BIT(1)
  19. #define PAGE2_I2C_BYPASS 0xea
  20. #define I2C_BYPASS_EN 0xd0
  21. #define PAGE2_MCS_EN 0xf3
  22. #define MCS_EN BIT(0)
  23. #define PAGE3_SET_ADD 0xfe
  24. #define VDO_CTL_ADD 0x13
  25. #define VDO_DIS 0x18
  26. #define VDO_EN 0x1c
  27. #define DP_NUM_LANES 4
  28. /*
  29. * PS8640 uses multiple addresses:
  30. * page[0]: for DP control
  31. * page[1]: for VIDEO Bridge
  32. * page[2]: for control top
  33. * page[3]: for DSI Link Control1
  34. * page[4]: for MIPI Phy
  35. * page[5]: for VPLL
  36. * page[6]: for DSI Link Control2
  37. * page[7]: for SPI ROM mapping
  38. */
  39. enum page_addr_offset {
  40. PAGE0_DP_CNTL = 0,
  41. PAGE1_VDO_BDG,
  42. PAGE2_TOP_CNTL,
  43. PAGE3_DSI_CNTL1,
  44. PAGE4_MIPI_PHY,
  45. PAGE5_VPLL,
  46. PAGE6_DSI_CNTL2,
  47. PAGE7_SPI_CNTL,
  48. MAX_DEVS
  49. };
  50. enum ps8640_vdo_control {
  51. DISABLE = VDO_DIS,
  52. ENABLE = VDO_EN,
  53. };
  54. struct ps8640 {
  55. struct drm_bridge bridge;
  56. struct drm_bridge *panel_bridge;
  57. struct mipi_dsi_device *dsi;
  58. struct i2c_client *page[MAX_DEVS];
  59. struct regulator_bulk_data supplies[2];
  60. struct gpio_desc *gpio_reset;
  61. struct gpio_desc *gpio_powerdown;
  62. bool powered;
  63. };
  64. static inline struct ps8640 *bridge_to_ps8640(struct drm_bridge *e)
  65. {
  66. return container_of(e, struct ps8640, bridge);
  67. }
  68. static int ps8640_bridge_vdo_control(struct ps8640 *ps_bridge,
  69. const enum ps8640_vdo_control ctrl)
  70. {
  71. struct i2c_client *client = ps_bridge->page[PAGE3_DSI_CNTL1];
  72. u8 vdo_ctrl_buf[] = { VDO_CTL_ADD, ctrl };
  73. int ret;
  74. ret = i2c_smbus_write_i2c_block_data(client, PAGE3_SET_ADD,
  75. sizeof(vdo_ctrl_buf),
  76. vdo_ctrl_buf);
  77. if (ret < 0) {
  78. DRM_ERROR("failed to %sable VDO: %d\n",
  79. ctrl == ENABLE ? "en" : "dis", ret);
  80. return ret;
  81. }
  82. return 0;
  83. }
  84. static void ps8640_bridge_poweron(struct ps8640 *ps_bridge)
  85. {
  86. struct i2c_client *client = ps_bridge->page[PAGE2_TOP_CNTL];
  87. unsigned long timeout;
  88. int ret, status;
  89. if (ps_bridge->powered)
  90. return;
  91. ret = regulator_bulk_enable(ARRAY_SIZE(ps_bridge->supplies),
  92. ps_bridge->supplies);
  93. if (ret < 0) {
  94. DRM_ERROR("cannot enable regulators %d\n", ret);
  95. return;
  96. }
  97. gpiod_set_value(ps_bridge->gpio_powerdown, 0);
  98. gpiod_set_value(ps_bridge->gpio_reset, 1);
  99. usleep_range(2000, 2500);
  100. gpiod_set_value(ps_bridge->gpio_reset, 0);
  101. /*
  102. * Wait for the ps8640 embedded MCU to be ready
  103. * First wait 200ms and then check the MCU ready flag every 20ms
  104. */
  105. msleep(200);
  106. timeout = jiffies + msecs_to_jiffies(200) + 1;
  107. while (time_is_after_jiffies(timeout)) {
  108. status = i2c_smbus_read_byte_data(client, PAGE2_GPIO_H);
  109. if (status < 0) {
  110. DRM_ERROR("failed read PAGE2_GPIO_H: %d\n", status);
  111. goto err_regulators_disable;
  112. }
  113. if ((status & PS_GPIO9) == PS_GPIO9)
  114. break;
  115. msleep(20);
  116. }
  117. msleep(50);
  118. /*
  119. * The Manufacturer Command Set (MCS) is a device dependent interface
  120. * intended for factory programming of the display module default
  121. * parameters. Once the display module is configured, the MCS shall be
  122. * disabled by the manufacturer. Once disabled, all MCS commands are
  123. * ignored by the display interface.
  124. */
  125. status = i2c_smbus_read_byte_data(client, PAGE2_MCS_EN);
  126. if (status < 0) {
  127. DRM_ERROR("failed read PAGE2_MCS_EN: %d\n", status);
  128. goto err_regulators_disable;
  129. }
  130. ret = i2c_smbus_write_byte_data(client, PAGE2_MCS_EN,
  131. status & ~MCS_EN);
  132. if (ret < 0) {
  133. DRM_ERROR("failed write PAGE2_MCS_EN: %d\n", ret);
  134. goto err_regulators_disable;
  135. }
  136. /* Switch access edp panel's edid through i2c */
  137. ret = i2c_smbus_write_byte_data(client, PAGE2_I2C_BYPASS,
  138. I2C_BYPASS_EN);
  139. if (ret < 0) {
  140. DRM_ERROR("failed write PAGE2_I2C_BYPASS: %d\n", ret);
  141. goto err_regulators_disable;
  142. }
  143. ps_bridge->powered = true;
  144. return;
  145. err_regulators_disable:
  146. regulator_bulk_disable(ARRAY_SIZE(ps_bridge->supplies),
  147. ps_bridge->supplies);
  148. }
  149. static void ps8640_bridge_poweroff(struct ps8640 *ps_bridge)
  150. {
  151. int ret;
  152. if (!ps_bridge->powered)
  153. return;
  154. gpiod_set_value(ps_bridge->gpio_reset, 1);
  155. gpiod_set_value(ps_bridge->gpio_powerdown, 1);
  156. ret = regulator_bulk_disable(ARRAY_SIZE(ps_bridge->supplies),
  157. ps_bridge->supplies);
  158. if (ret < 0)
  159. DRM_ERROR("cannot disable regulators %d\n", ret);
  160. ps_bridge->powered = false;
  161. }
  162. static void ps8640_pre_enable(struct drm_bridge *bridge)
  163. {
  164. struct ps8640 *ps_bridge = bridge_to_ps8640(bridge);
  165. int ret;
  166. ps8640_bridge_poweron(ps_bridge);
  167. ret = ps8640_bridge_vdo_control(ps_bridge, ENABLE);
  168. if (ret < 0)
  169. ps8640_bridge_poweroff(ps_bridge);
  170. }
  171. static void ps8640_post_disable(struct drm_bridge *bridge)
  172. {
  173. struct ps8640 *ps_bridge = bridge_to_ps8640(bridge);
  174. ps8640_bridge_vdo_control(ps_bridge, DISABLE);
  175. ps8640_bridge_poweroff(ps_bridge);
  176. }
  177. static int ps8640_bridge_attach(struct drm_bridge *bridge,
  178. enum drm_bridge_attach_flags flags)
  179. {
  180. struct ps8640 *ps_bridge = bridge_to_ps8640(bridge);
  181. struct device *dev = &ps_bridge->page[0]->dev;
  182. struct device_node *in_ep, *dsi_node;
  183. struct mipi_dsi_device *dsi;
  184. struct mipi_dsi_host *host;
  185. int ret;
  186. const struct mipi_dsi_device_info info = { .type = "ps8640",
  187. .channel = 0,
  188. .node = NULL,
  189. };
  190. if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
  191. return -EINVAL;
  192. /* port@0 is ps8640 dsi input port */
  193. in_ep = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);
  194. if (!in_ep)
  195. return -ENODEV;
  196. dsi_node = of_graph_get_remote_port_parent(in_ep);
  197. of_node_put(in_ep);
  198. if (!dsi_node)
  199. return -ENODEV;
  200. host = of_find_mipi_dsi_host_by_node(dsi_node);
  201. of_node_put(dsi_node);
  202. if (!host)
  203. return -ENODEV;
  204. dsi = mipi_dsi_device_register_full(host, &info);
  205. if (IS_ERR(dsi)) {
  206. dev_err(dev, "failed to create dsi device\n");
  207. ret = PTR_ERR(dsi);
  208. return ret;
  209. }
  210. ps_bridge->dsi = dsi;
  211. dsi->host = host;
  212. dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
  213. MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
  214. dsi->format = MIPI_DSI_FMT_RGB888;
  215. dsi->lanes = DP_NUM_LANES;
  216. ret = mipi_dsi_attach(dsi);
  217. if (ret)
  218. goto err_dsi_attach;
  219. /* Attach the panel-bridge to the dsi bridge */
  220. return drm_bridge_attach(bridge->encoder, ps_bridge->panel_bridge,
  221. &ps_bridge->bridge, flags);
  222. err_dsi_attach:
  223. mipi_dsi_device_unregister(dsi);
  224. return ret;
  225. }
  226. static struct edid *ps8640_bridge_get_edid(struct drm_bridge *bridge,
  227. struct drm_connector *connector)
  228. {
  229. struct ps8640 *ps_bridge = bridge_to_ps8640(bridge);
  230. bool poweroff = !ps_bridge->powered;
  231. struct edid *edid;
  232. /*
  233. * When we end calling get_edid() triggered by an ioctl, i.e
  234. *
  235. * drm_mode_getconnector (ioctl)
  236. * -> drm_helper_probe_single_connector_modes
  237. * -> drm_bridge_connector_get_modes
  238. * -> ps8640_bridge_get_edid
  239. *
  240. * We need to make sure that what we need is enabled before reading
  241. * EDID, for this chip, we need to do a full poweron, otherwise it will
  242. * fail.
  243. */
  244. drm_bridge_chain_pre_enable(bridge);
  245. edid = drm_get_edid(connector,
  246. ps_bridge->page[PAGE0_DP_CNTL]->adapter);
  247. /*
  248. * If we call the get_edid() function without having enabled the chip
  249. * before, return the chip to its original power state.
  250. */
  251. if (poweroff)
  252. drm_bridge_chain_post_disable(bridge);
  253. return edid;
  254. }
  255. static const struct drm_bridge_funcs ps8640_bridge_funcs = {
  256. .attach = ps8640_bridge_attach,
  257. .get_edid = ps8640_bridge_get_edid,
  258. .post_disable = ps8640_post_disable,
  259. .pre_enable = ps8640_pre_enable,
  260. };
  261. static int ps8640_probe(struct i2c_client *client)
  262. {
  263. struct device *dev = &client->dev;
  264. struct device_node *np = dev->of_node;
  265. struct ps8640 *ps_bridge;
  266. struct drm_panel *panel;
  267. int ret;
  268. u32 i;
  269. ps_bridge = devm_kzalloc(dev, sizeof(*ps_bridge), GFP_KERNEL);
  270. if (!ps_bridge)
  271. return -ENOMEM;
  272. /* port@1 is ps8640 output port */
  273. ret = drm_of_find_panel_or_bridge(np, 1, 0, &panel, NULL);
  274. if (ret < 0)
  275. return ret;
  276. if (!panel)
  277. return -ENODEV;
  278. ps_bridge->panel_bridge = devm_drm_panel_bridge_add(dev, panel);
  279. if (IS_ERR(ps_bridge->panel_bridge))
  280. return PTR_ERR(ps_bridge->panel_bridge);
  281. ps_bridge->supplies[0].supply = "vdd33";
  282. ps_bridge->supplies[1].supply = "vdd12";
  283. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ps_bridge->supplies),
  284. ps_bridge->supplies);
  285. if (ret)
  286. return ret;
  287. ps_bridge->gpio_powerdown = devm_gpiod_get(&client->dev, "powerdown",
  288. GPIOD_OUT_HIGH);
  289. if (IS_ERR(ps_bridge->gpio_powerdown))
  290. return PTR_ERR(ps_bridge->gpio_powerdown);
  291. /*
  292. * Assert the reset to avoid the bridge being initialized prematurely
  293. */
  294. ps_bridge->gpio_reset = devm_gpiod_get(&client->dev, "reset",
  295. GPIOD_OUT_HIGH);
  296. if (IS_ERR(ps_bridge->gpio_reset))
  297. return PTR_ERR(ps_bridge->gpio_reset);
  298. ps_bridge->bridge.funcs = &ps8640_bridge_funcs;
  299. ps_bridge->bridge.of_node = dev->of_node;
  300. ps_bridge->bridge.ops = DRM_BRIDGE_OP_EDID;
  301. ps_bridge->bridge.type = DRM_MODE_CONNECTOR_eDP;
  302. ps_bridge->page[PAGE0_DP_CNTL] = client;
  303. for (i = 1; i < ARRAY_SIZE(ps_bridge->page); i++) {
  304. ps_bridge->page[i] = devm_i2c_new_dummy_device(&client->dev,
  305. client->adapter,
  306. client->addr + i);
  307. if (IS_ERR(ps_bridge->page[i])) {
  308. dev_err(dev, "failed i2c dummy device, address %02x\n",
  309. client->addr + i);
  310. return PTR_ERR(ps_bridge->page[i]);
  311. }
  312. }
  313. i2c_set_clientdata(client, ps_bridge);
  314. drm_bridge_add(&ps_bridge->bridge);
  315. return 0;
  316. }
  317. static int ps8640_remove(struct i2c_client *client)
  318. {
  319. struct ps8640 *ps_bridge = i2c_get_clientdata(client);
  320. drm_bridge_remove(&ps_bridge->bridge);
  321. return 0;
  322. }
  323. static const struct of_device_id ps8640_match[] = {
  324. { .compatible = "parade,ps8640" },
  325. { }
  326. };
  327. MODULE_DEVICE_TABLE(of, ps8640_match);
  328. static struct i2c_driver ps8640_driver = {
  329. .probe_new = ps8640_probe,
  330. .remove = ps8640_remove,
  331. .driver = {
  332. .name = "ps8640",
  333. .of_match_table = ps8640_match,
  334. },
  335. };
  336. module_i2c_driver(ps8640_driver);
  337. MODULE_AUTHOR("Jitao Shi <jitao.shi@mediatek.com>");
  338. MODULE_AUTHOR("CK Hu <ck.hu@mediatek.com>");
  339. MODULE_AUTHOR("Enric Balletbo i Serra <enric.balletbo@collabora.com>");
  340. MODULE_DESCRIPTION("PARADE ps8640 DSI-eDP converter driver");
  341. MODULE_LICENSE("GPL v2");