ti-tfp410.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (C) 2016 Texas Instruments
  3. * Author: Jyri Sarha <jsarha@ti.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/fwnode.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/irq.h>
  14. #include <linux/module.h>
  15. #include <linux/of_graph.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/i2c.h>
  18. #include <drm/drmP.h>
  19. #include <drm/drm_atomic_helper.h>
  20. #include <drm/drm_crtc.h>
  21. #include <drm/drm_crtc_helper.h>
  22. #define HOTPLUG_DEBOUNCE_MS 1100
  23. struct tfp410 {
  24. struct drm_bridge bridge;
  25. struct drm_connector connector;
  26. struct i2c_adapter *ddc;
  27. struct gpio_desc *hpd;
  28. struct delayed_work hpd_work;
  29. struct device *dev;
  30. };
  31. static inline struct tfp410 *
  32. drm_bridge_to_tfp410(struct drm_bridge *bridge)
  33. {
  34. return container_of(bridge, struct tfp410, bridge);
  35. }
  36. static inline struct tfp410 *
  37. drm_connector_to_tfp410(struct drm_connector *connector)
  38. {
  39. return container_of(connector, struct tfp410, connector);
  40. }
  41. static int tfp410_get_modes(struct drm_connector *connector)
  42. {
  43. struct tfp410 *dvi = drm_connector_to_tfp410(connector);
  44. struct edid *edid;
  45. int ret;
  46. if (!dvi->ddc)
  47. goto fallback;
  48. edid = drm_get_edid(connector, dvi->ddc);
  49. if (!edid) {
  50. DRM_INFO("EDID read failed. Fallback to standard modes\n");
  51. goto fallback;
  52. }
  53. drm_connector_update_edid_property(connector, edid);
  54. ret = drm_add_edid_modes(connector, edid);
  55. kfree(edid);
  56. return ret;
  57. fallback:
  58. /* No EDID, fallback on the XGA standard modes */
  59. ret = drm_add_modes_noedid(connector, 1920, 1200);
  60. /* And prefer a mode pretty much anything can handle */
  61. drm_set_preferred_mode(connector, 1024, 768);
  62. return ret;
  63. }
  64. static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
  65. .get_modes = tfp410_get_modes,
  66. };
  67. static enum drm_connector_status
  68. tfp410_connector_detect(struct drm_connector *connector, bool force)
  69. {
  70. struct tfp410 *dvi = drm_connector_to_tfp410(connector);
  71. if (dvi->hpd) {
  72. if (gpiod_get_value_cansleep(dvi->hpd))
  73. return connector_status_connected;
  74. else
  75. return connector_status_disconnected;
  76. }
  77. if (dvi->ddc) {
  78. if (drm_probe_ddc(dvi->ddc))
  79. return connector_status_connected;
  80. else
  81. return connector_status_disconnected;
  82. }
  83. return connector_status_unknown;
  84. }
  85. static const struct drm_connector_funcs tfp410_con_funcs = {
  86. .detect = tfp410_connector_detect,
  87. .fill_modes = drm_helper_probe_single_connector_modes,
  88. .destroy = drm_connector_cleanup,
  89. .reset = drm_atomic_helper_connector_reset,
  90. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  91. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  92. };
  93. static int tfp410_attach(struct drm_bridge *bridge)
  94. {
  95. struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
  96. int ret;
  97. if (!bridge->encoder) {
  98. dev_err(dvi->dev, "Missing encoder\n");
  99. return -ENODEV;
  100. }
  101. if (dvi->hpd)
  102. dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
  103. drm_connector_helper_add(&dvi->connector,
  104. &tfp410_con_helper_funcs);
  105. ret = drm_connector_init(bridge->dev, &dvi->connector,
  106. &tfp410_con_funcs, DRM_MODE_CONNECTOR_HDMIA);
  107. if (ret) {
  108. dev_err(dvi->dev, "drm_connector_init() failed: %d\n", ret);
  109. return ret;
  110. }
  111. drm_connector_attach_encoder(&dvi->connector,
  112. bridge->encoder);
  113. return 0;
  114. }
  115. static const struct drm_bridge_funcs tfp410_bridge_funcs = {
  116. .attach = tfp410_attach,
  117. };
  118. static void tfp410_hpd_work_func(struct work_struct *work)
  119. {
  120. struct tfp410 *dvi;
  121. dvi = container_of(work, struct tfp410, hpd_work.work);
  122. if (dvi->bridge.dev)
  123. drm_helper_hpd_irq_event(dvi->bridge.dev);
  124. }
  125. static irqreturn_t tfp410_hpd_irq_thread(int irq, void *arg)
  126. {
  127. struct tfp410 *dvi = arg;
  128. mod_delayed_work(system_wq, &dvi->hpd_work,
  129. msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
  130. return IRQ_HANDLED;
  131. }
  132. static int tfp410_get_connector_properties(struct tfp410 *dvi)
  133. {
  134. struct device_node *connector_node, *ddc_phandle;
  135. int ret = 0;
  136. /* port@1 is the connector node */
  137. connector_node = of_graph_get_remote_node(dvi->dev->of_node, 1, -1);
  138. if (!connector_node)
  139. return -ENODEV;
  140. dvi->hpd = fwnode_get_named_gpiod(&connector_node->fwnode,
  141. "hpd-gpios", 0, GPIOD_IN, "hpd");
  142. if (IS_ERR(dvi->hpd)) {
  143. ret = PTR_ERR(dvi->hpd);
  144. dvi->hpd = NULL;
  145. if (ret == -ENOENT)
  146. ret = 0;
  147. else
  148. goto fail;
  149. }
  150. ddc_phandle = of_parse_phandle(connector_node, "ddc-i2c-bus", 0);
  151. if (!ddc_phandle)
  152. goto fail;
  153. dvi->ddc = of_get_i2c_adapter_by_node(ddc_phandle);
  154. if (dvi->ddc)
  155. dev_info(dvi->dev, "Connector's ddc i2c bus found\n");
  156. else
  157. ret = -EPROBE_DEFER;
  158. of_node_put(ddc_phandle);
  159. fail:
  160. of_node_put(connector_node);
  161. return ret;
  162. }
  163. static int tfp410_init(struct device *dev)
  164. {
  165. struct tfp410 *dvi;
  166. int ret;
  167. if (!dev->of_node) {
  168. dev_err(dev, "device-tree data is missing\n");
  169. return -ENXIO;
  170. }
  171. dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
  172. if (!dvi)
  173. return -ENOMEM;
  174. dev_set_drvdata(dev, dvi);
  175. dvi->bridge.funcs = &tfp410_bridge_funcs;
  176. dvi->bridge.of_node = dev->of_node;
  177. dvi->dev = dev;
  178. ret = tfp410_get_connector_properties(dvi);
  179. if (ret)
  180. goto fail;
  181. if (dvi->hpd) {
  182. INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
  183. ret = devm_request_threaded_irq(dev, gpiod_to_irq(dvi->hpd),
  184. NULL, tfp410_hpd_irq_thread, IRQF_TRIGGER_RISING |
  185. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  186. "hdmi-hpd", dvi);
  187. if (ret) {
  188. DRM_ERROR("failed to register hpd interrupt\n");
  189. goto fail;
  190. }
  191. }
  192. drm_bridge_add(&dvi->bridge);
  193. return 0;
  194. fail:
  195. i2c_put_adapter(dvi->ddc);
  196. if (dvi->hpd)
  197. gpiod_put(dvi->hpd);
  198. return ret;
  199. }
  200. static int tfp410_fini(struct device *dev)
  201. {
  202. struct tfp410 *dvi = dev_get_drvdata(dev);
  203. cancel_delayed_work_sync(&dvi->hpd_work);
  204. drm_bridge_remove(&dvi->bridge);
  205. if (dvi->ddc)
  206. i2c_put_adapter(dvi->ddc);
  207. if (dvi->hpd)
  208. gpiod_put(dvi->hpd);
  209. return 0;
  210. }
  211. static int tfp410_probe(struct platform_device *pdev)
  212. {
  213. return tfp410_init(&pdev->dev);
  214. }
  215. static int tfp410_remove(struct platform_device *pdev)
  216. {
  217. return tfp410_fini(&pdev->dev);
  218. }
  219. static const struct of_device_id tfp410_match[] = {
  220. { .compatible = "ti,tfp410" },
  221. {},
  222. };
  223. MODULE_DEVICE_TABLE(of, tfp410_match);
  224. static struct platform_driver tfp410_platform_driver = {
  225. .probe = tfp410_probe,
  226. .remove = tfp410_remove,
  227. .driver = {
  228. .name = "tfp410-bridge",
  229. .of_match_table = tfp410_match,
  230. },
  231. };
  232. #if IS_ENABLED(CONFIG_I2C)
  233. /* There is currently no i2c functionality. */
  234. static int tfp410_i2c_probe(struct i2c_client *client,
  235. const struct i2c_device_id *id)
  236. {
  237. int reg;
  238. if (!client->dev.of_node ||
  239. of_property_read_u32(client->dev.of_node, "reg", &reg)) {
  240. dev_err(&client->dev,
  241. "Can't get i2c reg property from device-tree\n");
  242. return -ENXIO;
  243. }
  244. return tfp410_init(&client->dev);
  245. }
  246. static int tfp410_i2c_remove(struct i2c_client *client)
  247. {
  248. return tfp410_fini(&client->dev);
  249. }
  250. static const struct i2c_device_id tfp410_i2c_ids[] = {
  251. { "tfp410", 0 },
  252. { }
  253. };
  254. MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
  255. static struct i2c_driver tfp410_i2c_driver = {
  256. .driver = {
  257. .name = "tfp410",
  258. .of_match_table = of_match_ptr(tfp410_match),
  259. },
  260. .id_table = tfp410_i2c_ids,
  261. .probe = tfp410_i2c_probe,
  262. .remove = tfp410_i2c_remove,
  263. };
  264. #endif /* IS_ENABLED(CONFIG_I2C) */
  265. static struct {
  266. uint i2c:1;
  267. uint platform:1;
  268. } tfp410_registered_driver;
  269. static int __init tfp410_module_init(void)
  270. {
  271. int ret;
  272. #if IS_ENABLED(CONFIG_I2C)
  273. ret = i2c_add_driver(&tfp410_i2c_driver);
  274. if (ret)
  275. pr_err("%s: registering i2c driver failed: %d",
  276. __func__, ret);
  277. else
  278. tfp410_registered_driver.i2c = 1;
  279. #endif
  280. ret = platform_driver_register(&tfp410_platform_driver);
  281. if (ret)
  282. pr_err("%s: registering platform driver failed: %d",
  283. __func__, ret);
  284. else
  285. tfp410_registered_driver.platform = 1;
  286. if (tfp410_registered_driver.i2c ||
  287. tfp410_registered_driver.platform)
  288. return 0;
  289. return ret;
  290. }
  291. module_init(tfp410_module_init);
  292. static void __exit tfp410_module_exit(void)
  293. {
  294. #if IS_ENABLED(CONFIG_I2C)
  295. if (tfp410_registered_driver.i2c)
  296. i2c_del_driver(&tfp410_i2c_driver);
  297. #endif
  298. if (tfp410_registered_driver.platform)
  299. platform_driver_unregister(&tfp410_platform_driver);
  300. }
  301. module_exit(tfp410_module_exit);
  302. MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
  303. MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
  304. MODULE_LICENSE("GPL");