ti-tfp410.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/module.h>
  11. #include <linux/of_graph.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/i2c.h>
  14. #include <drm/drmP.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <drm/drm_crtc.h>
  17. #include <drm/drm_crtc_helper.h>
  18. struct tfp410 {
  19. struct drm_bridge bridge;
  20. struct drm_connector connector;
  21. struct i2c_adapter *ddc;
  22. struct device *dev;
  23. };
  24. static inline struct tfp410 *
  25. drm_bridge_to_tfp410(struct drm_bridge *bridge)
  26. {
  27. return container_of(bridge, struct tfp410, bridge);
  28. }
  29. static inline struct tfp410 *
  30. drm_connector_to_tfp410(struct drm_connector *connector)
  31. {
  32. return container_of(connector, struct tfp410, connector);
  33. }
  34. static int tfp410_get_modes(struct drm_connector *connector)
  35. {
  36. struct tfp410 *dvi = drm_connector_to_tfp410(connector);
  37. struct edid *edid;
  38. int ret;
  39. if (!dvi->ddc)
  40. goto fallback;
  41. edid = drm_get_edid(connector, dvi->ddc);
  42. if (!edid) {
  43. DRM_INFO("EDID read failed. Fallback to standard modes\n");
  44. goto fallback;
  45. }
  46. drm_mode_connector_update_edid_property(connector, edid);
  47. return drm_add_edid_modes(connector, edid);
  48. fallback:
  49. /* No EDID, fallback on the XGA standard modes */
  50. ret = drm_add_modes_noedid(connector, 1920, 1200);
  51. /* And prefer a mode pretty much anything can handle */
  52. drm_set_preferred_mode(connector, 1024, 768);
  53. return ret;
  54. }
  55. static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
  56. .get_modes = tfp410_get_modes,
  57. };
  58. static enum drm_connector_status
  59. tfp410_connector_detect(struct drm_connector *connector, bool force)
  60. {
  61. struct tfp410 *dvi = drm_connector_to_tfp410(connector);
  62. if (dvi->ddc) {
  63. if (drm_probe_ddc(dvi->ddc))
  64. return connector_status_connected;
  65. else
  66. return connector_status_disconnected;
  67. }
  68. return connector_status_unknown;
  69. }
  70. static const struct drm_connector_funcs tfp410_con_funcs = {
  71. .dpms = drm_atomic_helper_connector_dpms,
  72. .detect = tfp410_connector_detect,
  73. .fill_modes = drm_helper_probe_single_connector_modes,
  74. .destroy = drm_connector_cleanup,
  75. .reset = drm_atomic_helper_connector_reset,
  76. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  77. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  78. };
  79. static int tfp410_attach(struct drm_bridge *bridge)
  80. {
  81. struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
  82. int ret;
  83. if (!bridge->encoder) {
  84. dev_err(dvi->dev, "Missing encoder\n");
  85. return -ENODEV;
  86. }
  87. drm_connector_helper_add(&dvi->connector,
  88. &tfp410_con_helper_funcs);
  89. ret = drm_connector_init(bridge->dev, &dvi->connector,
  90. &tfp410_con_funcs, DRM_MODE_CONNECTOR_HDMIA);
  91. if (ret) {
  92. dev_err(dvi->dev, "drm_connector_init() failed: %d\n", ret);
  93. return ret;
  94. }
  95. drm_mode_connector_attach_encoder(&dvi->connector,
  96. bridge->encoder);
  97. return 0;
  98. }
  99. static const struct drm_bridge_funcs tfp410_bridge_funcs = {
  100. .attach = tfp410_attach,
  101. };
  102. static int tfp410_get_connector_ddc(struct tfp410 *dvi)
  103. {
  104. struct device_node *ep = NULL, *connector_node = NULL;
  105. struct device_node *ddc_phandle = NULL;
  106. int ret = 0;
  107. /* port@1 is the connector node */
  108. ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 1, -1);
  109. if (!ep)
  110. goto fail;
  111. connector_node = of_graph_get_remote_port_parent(ep);
  112. if (!connector_node)
  113. goto fail;
  114. ddc_phandle = of_parse_phandle(connector_node, "ddc-i2c-bus", 0);
  115. if (!ddc_phandle)
  116. goto fail;
  117. dvi->ddc = of_get_i2c_adapter_by_node(ddc_phandle);
  118. if (dvi->ddc)
  119. dev_info(dvi->dev, "Connector's ddc i2c bus found\n");
  120. else
  121. ret = -EPROBE_DEFER;
  122. fail:
  123. of_node_put(ep);
  124. of_node_put(connector_node);
  125. of_node_put(ddc_phandle);
  126. return ret;
  127. }
  128. static int tfp410_init(struct device *dev)
  129. {
  130. struct tfp410 *dvi;
  131. int ret;
  132. if (!dev->of_node) {
  133. dev_err(dev, "device-tree data is missing\n");
  134. return -ENXIO;
  135. }
  136. dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
  137. if (!dvi)
  138. return -ENOMEM;
  139. dev_set_drvdata(dev, dvi);
  140. dvi->bridge.funcs = &tfp410_bridge_funcs;
  141. dvi->bridge.of_node = dev->of_node;
  142. dvi->dev = dev;
  143. ret = tfp410_get_connector_ddc(dvi);
  144. if (ret)
  145. goto fail;
  146. ret = drm_bridge_add(&dvi->bridge);
  147. if (ret) {
  148. dev_err(dev, "drm_bridge_add() failed: %d\n", ret);
  149. goto fail;
  150. }
  151. return 0;
  152. fail:
  153. i2c_put_adapter(dvi->ddc);
  154. return ret;
  155. }
  156. static int tfp410_fini(struct device *dev)
  157. {
  158. struct tfp410 *dvi = dev_get_drvdata(dev);
  159. drm_bridge_remove(&dvi->bridge);
  160. if (dvi->ddc)
  161. i2c_put_adapter(dvi->ddc);
  162. return 0;
  163. }
  164. static int tfp410_probe(struct platform_device *pdev)
  165. {
  166. return tfp410_init(&pdev->dev);
  167. }
  168. static int tfp410_remove(struct platform_device *pdev)
  169. {
  170. return tfp410_fini(&pdev->dev);
  171. }
  172. static const struct of_device_id tfp410_match[] = {
  173. { .compatible = "ti,tfp410" },
  174. {},
  175. };
  176. MODULE_DEVICE_TABLE(of, tfp410_match);
  177. struct platform_driver tfp410_platform_driver = {
  178. .probe = tfp410_probe,
  179. .remove = tfp410_remove,
  180. .driver = {
  181. .name = "tfp410-bridge",
  182. .of_match_table = tfp410_match,
  183. },
  184. };
  185. #if IS_ENABLED(CONFIG_I2C)
  186. /* There is currently no i2c functionality. */
  187. static int tfp410_i2c_probe(struct i2c_client *client,
  188. const struct i2c_device_id *id)
  189. {
  190. int reg;
  191. if (!client->dev.of_node ||
  192. of_property_read_u32(client->dev.of_node, "reg", &reg)) {
  193. dev_err(&client->dev,
  194. "Can't get i2c reg property from device-tree\n");
  195. return -ENXIO;
  196. }
  197. return tfp410_init(&client->dev);
  198. }
  199. static int tfp410_i2c_remove(struct i2c_client *client)
  200. {
  201. return tfp410_fini(&client->dev);
  202. }
  203. static const struct i2c_device_id tfp410_i2c_ids[] = {
  204. { "tfp410", 0 },
  205. { }
  206. };
  207. MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
  208. static struct i2c_driver tfp410_i2c_driver = {
  209. .driver = {
  210. .name = "tfp410",
  211. .of_match_table = of_match_ptr(tfp410_match),
  212. },
  213. .id_table = tfp410_i2c_ids,
  214. .probe = tfp410_i2c_probe,
  215. .remove = tfp410_i2c_remove,
  216. };
  217. #endif /* IS_ENABLED(CONFIG_I2C) */
  218. static struct {
  219. uint i2c:1;
  220. uint platform:1;
  221. } tfp410_registered_driver;
  222. static int __init tfp410_module_init(void)
  223. {
  224. int ret;
  225. #if IS_ENABLED(CONFIG_I2C)
  226. ret = i2c_add_driver(&tfp410_i2c_driver);
  227. if (ret)
  228. pr_err("%s: registering i2c driver failed: %d",
  229. __func__, ret);
  230. else
  231. tfp410_registered_driver.i2c = 1;
  232. #endif
  233. ret = platform_driver_register(&tfp410_platform_driver);
  234. if (ret)
  235. pr_err("%s: registering platform driver failed: %d",
  236. __func__, ret);
  237. else
  238. tfp410_registered_driver.platform = 1;
  239. if (tfp410_registered_driver.i2c ||
  240. tfp410_registered_driver.platform)
  241. return 0;
  242. return ret;
  243. }
  244. module_init(tfp410_module_init);
  245. static void __exit tfp410_module_exit(void)
  246. {
  247. #if IS_ENABLED(CONFIG_I2C)
  248. if (tfp410_registered_driver.i2c)
  249. i2c_del_driver(&tfp410_i2c_driver);
  250. #endif
  251. if (tfp410_registered_driver.platform)
  252. platform_driver_unregister(&tfp410_platform_driver);
  253. }
  254. module_exit(tfp410_module_exit);
  255. MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
  256. MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
  257. MODULE_LICENSE("GPL");