ptn3460.c 10.0 KB

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