ti-tfp410.c 8.2 KB

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