dumb-vga-dac.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2015-2016 Free Electrons
  3. * Copyright (C) 2015-2016 NextThing Co
  4. *
  5. * Maxime Ripard <maxime.ripard@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/of_graph.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 dumb_vga {
  19. struct drm_bridge bridge;
  20. struct drm_connector connector;
  21. struct i2c_adapter *ddc;
  22. };
  23. static inline struct dumb_vga *
  24. drm_bridge_to_dumb_vga(struct drm_bridge *bridge)
  25. {
  26. return container_of(bridge, struct dumb_vga, bridge);
  27. }
  28. static inline struct dumb_vga *
  29. drm_connector_to_dumb_vga(struct drm_connector *connector)
  30. {
  31. return container_of(connector, struct dumb_vga, connector);
  32. }
  33. static int dumb_vga_get_modes(struct drm_connector *connector)
  34. {
  35. struct dumb_vga *vga = drm_connector_to_dumb_vga(connector);
  36. struct edid *edid;
  37. int ret;
  38. if (IS_ERR(vga->ddc))
  39. goto fallback;
  40. edid = drm_get_edid(connector, vga->ddc);
  41. if (!edid) {
  42. DRM_INFO("EDID readout failed, falling back to standard modes\n");
  43. goto fallback;
  44. }
  45. drm_mode_connector_update_edid_property(connector, edid);
  46. return drm_add_edid_modes(connector, edid);
  47. fallback:
  48. /*
  49. * In case we cannot retrieve the EDIDs (broken or missing i2c
  50. * bus), fallback on the XGA standards
  51. */
  52. ret = drm_add_modes_noedid(connector, 1920, 1200);
  53. /* And prefer a mode pretty much anyone can handle */
  54. drm_set_preferred_mode(connector, 1024, 768);
  55. return ret;
  56. }
  57. static const struct drm_connector_helper_funcs dumb_vga_con_helper_funcs = {
  58. .get_modes = dumb_vga_get_modes,
  59. };
  60. static enum drm_connector_status
  61. dumb_vga_connector_detect(struct drm_connector *connector, bool force)
  62. {
  63. struct dumb_vga *vga = drm_connector_to_dumb_vga(connector);
  64. /*
  65. * Even if we have an I2C bus, we can't assume that the cable
  66. * is disconnected if drm_probe_ddc fails. Some cables don't
  67. * wire the DDC pins, or the I2C bus might not be working at
  68. * all.
  69. */
  70. if (!IS_ERR(vga->ddc) && drm_probe_ddc(vga->ddc))
  71. return connector_status_connected;
  72. return connector_status_unknown;
  73. }
  74. static const struct drm_connector_funcs dumb_vga_con_funcs = {
  75. .dpms = drm_atomic_helper_connector_dpms,
  76. .detect = dumb_vga_connector_detect,
  77. .fill_modes = drm_helper_probe_single_connector_modes,
  78. .destroy = drm_connector_cleanup,
  79. .reset = drm_atomic_helper_connector_reset,
  80. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  81. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  82. };
  83. static int dumb_vga_attach(struct drm_bridge *bridge)
  84. {
  85. struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge);
  86. int ret;
  87. if (!bridge->encoder) {
  88. DRM_ERROR("Missing encoder\n");
  89. return -ENODEV;
  90. }
  91. drm_connector_helper_add(&vga->connector,
  92. &dumb_vga_con_helper_funcs);
  93. ret = drm_connector_init(bridge->dev, &vga->connector,
  94. &dumb_vga_con_funcs, DRM_MODE_CONNECTOR_VGA);
  95. if (ret) {
  96. DRM_ERROR("Failed to initialize connector\n");
  97. return ret;
  98. }
  99. drm_mode_connector_attach_encoder(&vga->connector,
  100. bridge->encoder);
  101. return 0;
  102. }
  103. static const struct drm_bridge_funcs dumb_vga_bridge_funcs = {
  104. .attach = dumb_vga_attach,
  105. };
  106. static struct i2c_adapter *dumb_vga_retrieve_ddc(struct device *dev)
  107. {
  108. struct device_node *end_node, *phandle, *remote;
  109. struct i2c_adapter *ddc;
  110. end_node = of_graph_get_endpoint_by_regs(dev->of_node, 1, -1);
  111. if (!end_node) {
  112. dev_err(dev, "Missing connector endpoint\n");
  113. return ERR_PTR(-ENODEV);
  114. }
  115. remote = of_graph_get_remote_port_parent(end_node);
  116. of_node_put(end_node);
  117. if (!remote) {
  118. dev_err(dev, "Enable to parse remote node\n");
  119. return ERR_PTR(-EINVAL);
  120. }
  121. phandle = of_parse_phandle(remote, "ddc-i2c-bus", 0);
  122. of_node_put(remote);
  123. if (!phandle)
  124. return ERR_PTR(-ENODEV);
  125. ddc = of_get_i2c_adapter_by_node(phandle);
  126. of_node_put(phandle);
  127. if (!ddc)
  128. return ERR_PTR(-EPROBE_DEFER);
  129. return ddc;
  130. }
  131. static int dumb_vga_probe(struct platform_device *pdev)
  132. {
  133. struct dumb_vga *vga;
  134. int ret;
  135. vga = devm_kzalloc(&pdev->dev, sizeof(*vga), GFP_KERNEL);
  136. if (!vga)
  137. return -ENOMEM;
  138. platform_set_drvdata(pdev, vga);
  139. vga->ddc = dumb_vga_retrieve_ddc(&pdev->dev);
  140. if (IS_ERR(vga->ddc)) {
  141. if (PTR_ERR(vga->ddc) == -ENODEV) {
  142. dev_dbg(&pdev->dev,
  143. "No i2c bus specified. Disabling EDID readout\n");
  144. } else {
  145. dev_err(&pdev->dev, "Couldn't retrieve i2c bus\n");
  146. return PTR_ERR(vga->ddc);
  147. }
  148. }
  149. vga->bridge.funcs = &dumb_vga_bridge_funcs;
  150. vga->bridge.of_node = pdev->dev.of_node;
  151. ret = drm_bridge_add(&vga->bridge);
  152. if (ret && !IS_ERR(vga->ddc))
  153. i2c_put_adapter(vga->ddc);
  154. return ret;
  155. }
  156. static int dumb_vga_remove(struct platform_device *pdev)
  157. {
  158. struct dumb_vga *vga = platform_get_drvdata(pdev);
  159. drm_bridge_remove(&vga->bridge);
  160. if (!IS_ERR(vga->ddc))
  161. i2c_put_adapter(vga->ddc);
  162. return 0;
  163. }
  164. static const struct of_device_id dumb_vga_match[] = {
  165. { .compatible = "dumb-vga-dac" },
  166. { .compatible = "ti,ths8135" },
  167. {},
  168. };
  169. MODULE_DEVICE_TABLE(of, dumb_vga_match);
  170. static struct platform_driver dumb_vga_driver = {
  171. .probe = dumb_vga_probe,
  172. .remove = dumb_vga_remove,
  173. .driver = {
  174. .name = "dumb-vga-dac",
  175. .of_match_table = dumb_vga_match,
  176. },
  177. };
  178. module_platform_driver(dumb_vga_driver);
  179. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  180. MODULE_DESCRIPTION("Dumb VGA DAC bridge driver");
  181. MODULE_LICENSE("GPL");