nxp-ptn3460.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * NXP PTN3460 DP/LVDS bridge driver
  3. *
  4. * Copyright (C) 2013 Google, Inc.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/gpio.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/i2c.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/of_graph.h>
  23. #include <drm/drm_panel.h>
  24. #include "drm_crtc.h"
  25. #include "drm_crtc_helper.h"
  26. #include "drm_atomic_helper.h"
  27. #include "drm_edid.h"
  28. #include "drmP.h"
  29. #define PTN3460_EDID_ADDR 0x0
  30. #define PTN3460_EDID_EMULATION_ADDR 0x84
  31. #define PTN3460_EDID_ENABLE_EMULATION 0
  32. #define PTN3460_EDID_EMULATION_SELECTION 1
  33. #define PTN3460_EDID_SRAM_LOAD_ADDR 0x85
  34. struct ptn3460_bridge {
  35. struct drm_connector connector;
  36. struct i2c_client *client;
  37. struct drm_bridge bridge;
  38. struct edid *edid;
  39. struct drm_panel *panel;
  40. struct gpio_desc *gpio_pd_n;
  41. struct gpio_desc *gpio_rst_n;
  42. u32 edid_emulation;
  43. bool enabled;
  44. };
  45. static inline struct ptn3460_bridge *
  46. bridge_to_ptn3460(struct drm_bridge *bridge)
  47. {
  48. return container_of(bridge, struct ptn3460_bridge, bridge);
  49. }
  50. static inline struct ptn3460_bridge *
  51. connector_to_ptn3460(struct drm_connector *connector)
  52. {
  53. return container_of(connector, struct ptn3460_bridge, connector);
  54. }
  55. static int ptn3460_read_bytes(struct ptn3460_bridge *ptn_bridge, char addr,
  56. u8 *buf, int len)
  57. {
  58. int ret;
  59. ret = i2c_master_send(ptn_bridge->client, &addr, 1);
  60. if (ret <= 0) {
  61. DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
  62. return ret;
  63. }
  64. ret = i2c_master_recv(ptn_bridge->client, buf, len);
  65. if (ret <= 0) {
  66. DRM_ERROR("Failed to recv i2c data, ret=%d\n", ret);
  67. return ret;
  68. }
  69. return 0;
  70. }
  71. static int ptn3460_write_byte(struct ptn3460_bridge *ptn_bridge, char addr,
  72. char val)
  73. {
  74. int ret;
  75. char buf[2];
  76. buf[0] = addr;
  77. buf[1] = val;
  78. ret = i2c_master_send(ptn_bridge->client, buf, ARRAY_SIZE(buf));
  79. if (ret <= 0) {
  80. DRM_ERROR("Failed to send i2c command, ret=%d\n", ret);
  81. return ret;
  82. }
  83. return 0;
  84. }
  85. static int ptn3460_select_edid(struct ptn3460_bridge *ptn_bridge)
  86. {
  87. int ret;
  88. char val;
  89. /* Load the selected edid into SRAM (accessed at PTN3460_EDID_ADDR) */
  90. ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_SRAM_LOAD_ADDR,
  91. ptn_bridge->edid_emulation);
  92. if (ret) {
  93. DRM_ERROR("Failed to transfer EDID to sram, ret=%d\n", ret);
  94. return ret;
  95. }
  96. /* Enable EDID emulation and select the desired EDID */
  97. val = 1 << PTN3460_EDID_ENABLE_EMULATION |
  98. ptn_bridge->edid_emulation << PTN3460_EDID_EMULATION_SELECTION;
  99. ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_EMULATION_ADDR, val);
  100. if (ret) {
  101. DRM_ERROR("Failed to write EDID value, ret=%d\n", ret);
  102. return ret;
  103. }
  104. return 0;
  105. }
  106. static void ptn3460_pre_enable(struct drm_bridge *bridge)
  107. {
  108. struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
  109. int ret;
  110. if (ptn_bridge->enabled)
  111. return;
  112. gpiod_set_value(ptn_bridge->gpio_pd_n, 1);
  113. gpiod_set_value(ptn_bridge->gpio_rst_n, 0);
  114. usleep_range(10, 20);
  115. gpiod_set_value(ptn_bridge->gpio_rst_n, 1);
  116. if (drm_panel_prepare(ptn_bridge->panel)) {
  117. DRM_ERROR("failed to prepare panel\n");
  118. return;
  119. }
  120. /*
  121. * There's a bug in the PTN chip where it falsely asserts hotplug before
  122. * it is fully functional. We're forced to wait for the maximum start up
  123. * time specified in the chip's datasheet to make sure we're really up.
  124. */
  125. msleep(90);
  126. ret = ptn3460_select_edid(ptn_bridge);
  127. if (ret)
  128. DRM_ERROR("Select EDID failed ret=%d\n", ret);
  129. ptn_bridge->enabled = true;
  130. }
  131. static void ptn3460_enable(struct drm_bridge *bridge)
  132. {
  133. struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
  134. if (drm_panel_enable(ptn_bridge->panel)) {
  135. DRM_ERROR("failed to enable panel\n");
  136. return;
  137. }
  138. }
  139. static void ptn3460_disable(struct drm_bridge *bridge)
  140. {
  141. struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
  142. if (!ptn_bridge->enabled)
  143. return;
  144. ptn_bridge->enabled = false;
  145. if (drm_panel_disable(ptn_bridge->panel)) {
  146. DRM_ERROR("failed to disable panel\n");
  147. return;
  148. }
  149. gpiod_set_value(ptn_bridge->gpio_rst_n, 1);
  150. gpiod_set_value(ptn_bridge->gpio_pd_n, 0);
  151. }
  152. static void ptn3460_post_disable(struct drm_bridge *bridge)
  153. {
  154. struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
  155. if (drm_panel_unprepare(ptn_bridge->panel)) {
  156. DRM_ERROR("failed to unprepare panel\n");
  157. return;
  158. }
  159. }
  160. static int ptn3460_get_modes(struct drm_connector *connector)
  161. {
  162. struct ptn3460_bridge *ptn_bridge;
  163. u8 *edid;
  164. int ret, num_modes = 0;
  165. bool power_off;
  166. ptn_bridge = connector_to_ptn3460(connector);
  167. if (ptn_bridge->edid)
  168. return drm_add_edid_modes(connector, ptn_bridge->edid);
  169. power_off = !ptn_bridge->enabled;
  170. ptn3460_pre_enable(&ptn_bridge->bridge);
  171. edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
  172. if (!edid) {
  173. DRM_ERROR("Failed to allocate EDID\n");
  174. return 0;
  175. }
  176. ret = ptn3460_read_bytes(ptn_bridge, PTN3460_EDID_ADDR, edid,
  177. EDID_LENGTH);
  178. if (ret) {
  179. kfree(edid);
  180. goto out;
  181. }
  182. ptn_bridge->edid = (struct edid *)edid;
  183. drm_mode_connector_update_edid_property(connector, ptn_bridge->edid);
  184. num_modes = drm_add_edid_modes(connector, ptn_bridge->edid);
  185. out:
  186. if (power_off)
  187. ptn3460_disable(&ptn_bridge->bridge);
  188. return num_modes;
  189. }
  190. static struct drm_encoder *ptn3460_best_encoder(struct drm_connector *connector)
  191. {
  192. struct ptn3460_bridge *ptn_bridge = connector_to_ptn3460(connector);
  193. return ptn_bridge->bridge.encoder;
  194. }
  195. static struct drm_connector_helper_funcs ptn3460_connector_helper_funcs = {
  196. .get_modes = ptn3460_get_modes,
  197. .best_encoder = ptn3460_best_encoder,
  198. };
  199. static enum drm_connector_status ptn3460_detect(struct drm_connector *connector,
  200. bool force)
  201. {
  202. return connector_status_connected;
  203. }
  204. static void ptn3460_connector_destroy(struct drm_connector *connector)
  205. {
  206. drm_connector_cleanup(connector);
  207. }
  208. static struct drm_connector_funcs ptn3460_connector_funcs = {
  209. .dpms = drm_atomic_helper_connector_dpms,
  210. .fill_modes = drm_helper_probe_single_connector_modes,
  211. .detect = ptn3460_detect,
  212. .destroy = ptn3460_connector_destroy,
  213. .reset = drm_atomic_helper_connector_reset,
  214. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  215. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  216. };
  217. static int ptn3460_bridge_attach(struct drm_bridge *bridge)
  218. {
  219. struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge);
  220. int ret;
  221. if (!bridge->encoder) {
  222. DRM_ERROR("Parent encoder object not found");
  223. return -ENODEV;
  224. }
  225. ptn_bridge->connector.polled = DRM_CONNECTOR_POLL_HPD;
  226. ret = drm_connector_init(bridge->dev, &ptn_bridge->connector,
  227. &ptn3460_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
  228. if (ret) {
  229. DRM_ERROR("Failed to initialize connector with drm\n");
  230. return ret;
  231. }
  232. drm_connector_helper_add(&ptn_bridge->connector,
  233. &ptn3460_connector_helper_funcs);
  234. drm_connector_register(&ptn_bridge->connector);
  235. drm_mode_connector_attach_encoder(&ptn_bridge->connector,
  236. bridge->encoder);
  237. if (ptn_bridge->panel)
  238. drm_panel_attach(ptn_bridge->panel, &ptn_bridge->connector);
  239. drm_helper_hpd_irq_event(ptn_bridge->connector.dev);
  240. return ret;
  241. }
  242. static struct drm_bridge_funcs ptn3460_bridge_funcs = {
  243. .pre_enable = ptn3460_pre_enable,
  244. .enable = ptn3460_enable,
  245. .disable = ptn3460_disable,
  246. .post_disable = ptn3460_post_disable,
  247. .attach = ptn3460_bridge_attach,
  248. };
  249. static int ptn3460_probe(struct i2c_client *client,
  250. const struct i2c_device_id *id)
  251. {
  252. struct device *dev = &client->dev;
  253. struct ptn3460_bridge *ptn_bridge;
  254. struct device_node *endpoint, *panel_node;
  255. int ret;
  256. ptn_bridge = devm_kzalloc(dev, sizeof(*ptn_bridge), GFP_KERNEL);
  257. if (!ptn_bridge) {
  258. return -ENOMEM;
  259. }
  260. endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
  261. if (endpoint) {
  262. panel_node = of_graph_get_remote_port_parent(endpoint);
  263. if (panel_node) {
  264. ptn_bridge->panel = of_drm_find_panel(panel_node);
  265. of_node_put(panel_node);
  266. if (!ptn_bridge->panel)
  267. return -EPROBE_DEFER;
  268. }
  269. }
  270. ptn_bridge->client = client;
  271. ptn_bridge->gpio_pd_n = devm_gpiod_get(&client->dev, "powerdown",
  272. GPIOD_OUT_HIGH);
  273. if (IS_ERR(ptn_bridge->gpio_pd_n)) {
  274. ret = PTR_ERR(ptn_bridge->gpio_pd_n);
  275. dev_err(dev, "cannot get gpio_pd_n %d\n", ret);
  276. return ret;
  277. }
  278. /*
  279. * Request the reset pin low to avoid the bridge being
  280. * initialized prematurely
  281. */
  282. ptn_bridge->gpio_rst_n = devm_gpiod_get(&client->dev, "reset",
  283. GPIOD_OUT_LOW);
  284. if (IS_ERR(ptn_bridge->gpio_rst_n)) {
  285. ret = PTR_ERR(ptn_bridge->gpio_rst_n);
  286. DRM_ERROR("cannot get gpio_rst_n %d\n", ret);
  287. return ret;
  288. }
  289. ret = of_property_read_u32(dev->of_node, "edid-emulation",
  290. &ptn_bridge->edid_emulation);
  291. if (ret) {
  292. dev_err(dev, "Can't read EDID emulation value\n");
  293. return ret;
  294. }
  295. ptn_bridge->bridge.funcs = &ptn3460_bridge_funcs;
  296. ptn_bridge->bridge.of_node = dev->of_node;
  297. ret = drm_bridge_add(&ptn_bridge->bridge);
  298. if (ret) {
  299. DRM_ERROR("Failed to add bridge\n");
  300. return ret;
  301. }
  302. i2c_set_clientdata(client, ptn_bridge);
  303. return 0;
  304. }
  305. static int ptn3460_remove(struct i2c_client *client)
  306. {
  307. struct ptn3460_bridge *ptn_bridge = i2c_get_clientdata(client);
  308. drm_bridge_remove(&ptn_bridge->bridge);
  309. return 0;
  310. }
  311. static const struct i2c_device_id ptn3460_i2c_table[] = {
  312. {"ptn3460", 0},
  313. {},
  314. };
  315. MODULE_DEVICE_TABLE(i2c, ptn3460_i2c_table);
  316. static const struct of_device_id ptn3460_match[] = {
  317. { .compatible = "nxp,ptn3460" },
  318. {},
  319. };
  320. MODULE_DEVICE_TABLE(of, ptn3460_match);
  321. static struct i2c_driver ptn3460_driver = {
  322. .id_table = ptn3460_i2c_table,
  323. .probe = ptn3460_probe,
  324. .remove = ptn3460_remove,
  325. .driver = {
  326. .name = "nxp,ptn3460",
  327. .of_match_table = ptn3460_match,
  328. },
  329. };
  330. module_i2c_driver(ptn3460_driver);
  331. MODULE_AUTHOR("Sean Paul <seanpaul@chromium.org>");
  332. MODULE_DESCRIPTION("NXP ptn3460 eDP-LVDS converter driver");
  333. MODULE_LICENSE("GPL v2");