udl_connector.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (C) 2012 Red Hat
  3. * based in parts on udlfb.c:
  4. * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
  5. * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
  6. * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License v2. See the file COPYING in the main directory of this archive for
  10. * more details.
  11. */
  12. #include <drm/drmP.h>
  13. #include <drm/drm_crtc.h>
  14. #include <drm/drm_edid.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include "udl_drv.h"
  17. /* dummy connector to just get EDID,
  18. all UDL appear to have a DVI-D */
  19. static u8 *udl_get_edid(struct udl_device *udl)
  20. {
  21. u8 *block;
  22. char rbuf[3];
  23. int ret, i;
  24. block = kmalloc(EDID_LENGTH, GFP_KERNEL);
  25. if (block == NULL)
  26. return NULL;
  27. for (i = 0; i < EDID_LENGTH; i++) {
  28. ret = usb_control_msg(udl->ddev->usbdev,
  29. usb_rcvctrlpipe(udl->ddev->usbdev, 0), (0x02),
  30. (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
  31. HZ);
  32. if (ret < 1) {
  33. DRM_ERROR("Read EDID byte %d failed err %x\n", i, ret);
  34. i--;
  35. goto error;
  36. }
  37. block[i] = rbuf[1];
  38. }
  39. return block;
  40. error:
  41. kfree(block);
  42. return NULL;
  43. }
  44. static int udl_get_modes(struct drm_connector *connector)
  45. {
  46. struct udl_device *udl = connector->dev->dev_private;
  47. struct edid *edid;
  48. int ret;
  49. edid = (struct edid *)udl_get_edid(udl);
  50. drm_mode_connector_update_edid_property(connector, edid);
  51. ret = drm_add_edid_modes(connector, edid);
  52. kfree(edid);
  53. return ret;
  54. }
  55. static int udl_mode_valid(struct drm_connector *connector,
  56. struct drm_display_mode *mode)
  57. {
  58. struct udl_device *udl = connector->dev->dev_private;
  59. if (!udl->sku_pixel_limit)
  60. return 0;
  61. if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit)
  62. return MODE_VIRTUAL_Y;
  63. return 0;
  64. }
  65. static enum drm_connector_status
  66. udl_detect(struct drm_connector *connector, bool force)
  67. {
  68. if (drm_device_is_unplugged(connector->dev))
  69. return connector_status_disconnected;
  70. return connector_status_connected;
  71. }
  72. static struct drm_encoder*
  73. udl_best_single_encoder(struct drm_connector *connector)
  74. {
  75. int enc_id = connector->encoder_ids[0];
  76. struct drm_mode_object *obj;
  77. struct drm_encoder *encoder;
  78. obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
  79. if (!obj)
  80. return NULL;
  81. encoder = obj_to_encoder(obj);
  82. return encoder;
  83. }
  84. static int udl_connector_set_property(struct drm_connector *connector,
  85. struct drm_property *property,
  86. uint64_t val)
  87. {
  88. return 0;
  89. }
  90. static void udl_connector_destroy(struct drm_connector *connector)
  91. {
  92. drm_sysfs_connector_remove(connector);
  93. drm_connector_cleanup(connector);
  94. kfree(connector);
  95. }
  96. static struct drm_connector_helper_funcs udl_connector_helper_funcs = {
  97. .get_modes = udl_get_modes,
  98. .mode_valid = udl_mode_valid,
  99. .best_encoder = udl_best_single_encoder,
  100. };
  101. static struct drm_connector_funcs udl_connector_funcs = {
  102. .dpms = drm_helper_connector_dpms,
  103. .detect = udl_detect,
  104. .fill_modes = drm_helper_probe_single_connector_modes,
  105. .destroy = udl_connector_destroy,
  106. .set_property = udl_connector_set_property,
  107. };
  108. int udl_connector_init(struct drm_device *dev, struct drm_encoder *encoder)
  109. {
  110. struct drm_connector *connector;
  111. connector = kzalloc(sizeof(struct drm_connector), GFP_KERNEL);
  112. if (!connector)
  113. return -ENOMEM;
  114. drm_connector_init(dev, connector, &udl_connector_funcs, DRM_MODE_CONNECTOR_DVII);
  115. drm_connector_helper_add(connector, &udl_connector_helper_funcs);
  116. drm_sysfs_connector_add(connector);
  117. drm_mode_connector_attach_encoder(connector, encoder);
  118. drm_object_attach_property(&connector->base,
  119. dev->mode_config.dirty_info_property,
  120. 1);
  121. return 0;
  122. }