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