dumb-vga-dac.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. ret = drm_add_edid_modes(connector, edid);
  47. kfree(edid);
  48. return ret;
  49. fallback:
  50. /*
  51. * In case we cannot retrieve the EDIDs (broken or missing i2c
  52. * bus), fallback on the XGA standards
  53. */
  54. ret = drm_add_modes_noedid(connector, 1920, 1200);
  55. /* And prefer a mode pretty much anyone can handle */
  56. drm_set_preferred_mode(connector, 1024, 768);
  57. return ret;
  58. }
  59. static const struct drm_connector_helper_funcs dumb_vga_con_helper_funcs = {
  60. .get_modes = dumb_vga_get_modes,
  61. };
  62. static enum drm_connector_status
  63. dumb_vga_connector_detect(struct drm_connector *connector, bool force)
  64. {
  65. struct dumb_vga *vga = drm_connector_to_dumb_vga(connector);
  66. /*
  67. * Even if we have an I2C bus, we can't assume that the cable
  68. * is disconnected if drm_probe_ddc fails. Some cables don't
  69. * wire the DDC pins, or the I2C bus might not be working at
  70. * all.
  71. */
  72. if (!IS_ERR(vga->ddc) && drm_probe_ddc(vga->ddc))
  73. return connector_status_connected;
  74. return connector_status_unknown;
  75. }
  76. static const struct drm_connector_funcs dumb_vga_con_funcs = {
  77. .dpms = drm_atomic_helper_connector_dpms,
  78. .detect = dumb_vga_connector_detect,
  79. .fill_modes = drm_helper_probe_single_connector_modes,
  80. .destroy = drm_connector_cleanup,
  81. .reset = drm_atomic_helper_connector_reset,
  82. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  83. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  84. };
  85. static int dumb_vga_attach(struct drm_bridge *bridge)
  86. {
  87. struct dumb_vga *vga = drm_bridge_to_dumb_vga(bridge);
  88. int ret;
  89. if (!bridge->encoder) {
  90. DRM_ERROR("Missing encoder\n");
  91. return -ENODEV;
  92. }
  93. drm_connector_helper_add(&vga->connector,
  94. &dumb_vga_con_helper_funcs);
  95. ret = drm_connector_init(bridge->dev, &vga->connector,
  96. &dumb_vga_con_funcs, DRM_MODE_CONNECTOR_VGA);
  97. if (ret) {
  98. DRM_ERROR("Failed to initialize connector\n");
  99. return ret;
  100. }
  101. drm_mode_connector_attach_encoder(&vga->connector,
  102. bridge->encoder);
  103. return 0;
  104. }
  105. static const struct drm_bridge_funcs dumb_vga_bridge_funcs = {
  106. .attach = dumb_vga_attach,
  107. };
  108. static struct i2c_adapter *dumb_vga_retrieve_ddc(struct device *dev)
  109. {
  110. struct device_node *end_node, *phandle, *remote;
  111. struct i2c_adapter *ddc;
  112. end_node = of_graph_get_endpoint_by_regs(dev->of_node, 1, -1);
  113. if (!end_node) {
  114. dev_err(dev, "Missing connector endpoint\n");
  115. return ERR_PTR(-ENODEV);
  116. }
  117. remote = of_graph_get_remote_port_parent(end_node);
  118. of_node_put(end_node);
  119. if (!remote) {
  120. dev_err(dev, "Enable to parse remote node\n");
  121. return ERR_PTR(-EINVAL);
  122. }
  123. phandle = of_parse_phandle(remote, "ddc-i2c-bus", 0);
  124. of_node_put(remote);
  125. if (!phandle)
  126. return ERR_PTR(-ENODEV);
  127. ddc = of_get_i2c_adapter_by_node(phandle);
  128. of_node_put(phandle);
  129. if (!ddc)
  130. return ERR_PTR(-EPROBE_DEFER);
  131. return ddc;
  132. }
  133. static int dumb_vga_probe(struct platform_device *pdev)
  134. {
  135. struct dumb_vga *vga;
  136. int ret;
  137. vga = devm_kzalloc(&pdev->dev, sizeof(*vga), GFP_KERNEL);
  138. if (!vga)
  139. return -ENOMEM;
  140. platform_set_drvdata(pdev, vga);
  141. vga->ddc = dumb_vga_retrieve_ddc(&pdev->dev);
  142. if (IS_ERR(vga->ddc)) {
  143. if (PTR_ERR(vga->ddc) == -ENODEV) {
  144. dev_dbg(&pdev->dev,
  145. "No i2c bus specified. Disabling EDID readout\n");
  146. } else {
  147. dev_err(&pdev->dev, "Couldn't retrieve i2c bus\n");
  148. return PTR_ERR(vga->ddc);
  149. }
  150. }
  151. vga->bridge.funcs = &dumb_vga_bridge_funcs;
  152. vga->bridge.of_node = pdev->dev.of_node;
  153. ret = drm_bridge_add(&vga->bridge);
  154. if (ret && !IS_ERR(vga->ddc))
  155. i2c_put_adapter(vga->ddc);
  156. return ret;
  157. }
  158. static int dumb_vga_remove(struct platform_device *pdev)
  159. {
  160. struct dumb_vga *vga = platform_get_drvdata(pdev);
  161. drm_bridge_remove(&vga->bridge);
  162. if (!IS_ERR(vga->ddc))
  163. i2c_put_adapter(vga->ddc);
  164. return 0;
  165. }
  166. static const struct of_device_id dumb_vga_match[] = {
  167. { .compatible = "dumb-vga-dac" },
  168. {},
  169. };
  170. MODULE_DEVICE_TABLE(of, dumb_vga_match);
  171. static struct platform_driver dumb_vga_driver = {
  172. .probe = dumb_vga_probe,
  173. .remove = dumb_vga_remove,
  174. .driver = {
  175. .name = "dumb-vga-dac",
  176. .of_match_table = dumb_vga_match,
  177. },
  178. };
  179. module_platform_driver(dumb_vga_driver);
  180. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  181. MODULE_DESCRIPTION("Dumb VGA DAC bridge driver");
  182. MODULE_LICENSE("GPL");