Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 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 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 13 | #include <drm/drmP.h> |
| 14 | #include <drm/drm_crtc.h> |
| 15 | #include <drm/drm_edid.h> |
| 16 | #include <drm/drm_crtc_helper.h> |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 17 | #include "udl_drv.h" |
| 18 | |
| 19 | /* dummy connector to just get EDID, |
| 20 | all UDL appear to have a DVI-D */ |
| 21 | |
| 22 | static u8 *udl_get_edid(struct udl_device *udl) |
| 23 | { |
| 24 | u8 *block; |
Hans de Goede | 242187b | 2013-01-11 12:08:57 +0100 | [diff] [blame] | 25 | char *rbuf; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 26 | int ret, i; |
| 27 | |
| 28 | block = kmalloc(EDID_LENGTH, GFP_KERNEL); |
| 29 | if (block == NULL) |
| 30 | return NULL; |
| 31 | |
Hans de Goede | 242187b | 2013-01-11 12:08:57 +0100 | [diff] [blame] | 32 | rbuf = kmalloc(2, GFP_KERNEL); |
| 33 | if (rbuf == NULL) |
| 34 | goto error; |
| 35 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 36 | for (i = 0; i < EDID_LENGTH; i++) { |
| 37 | ret = usb_control_msg(udl->ddev->usbdev, |
| 38 | usb_rcvctrlpipe(udl->ddev->usbdev, 0), (0x02), |
| 39 | (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2, |
| 40 | HZ); |
| 41 | if (ret < 1) { |
| 42 | DRM_ERROR("Read EDID byte %d failed err %x\n", i, ret); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 43 | goto error; |
| 44 | } |
| 45 | block[i] = rbuf[1]; |
| 46 | } |
| 47 | |
Hans de Goede | 242187b | 2013-01-11 12:08:57 +0100 | [diff] [blame] | 48 | kfree(rbuf); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 49 | return block; |
| 50 | |
| 51 | error: |
| 52 | kfree(block); |
Hans de Goede | 242187b | 2013-01-11 12:08:57 +0100 | [diff] [blame] | 53 | kfree(rbuf); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | static int udl_get_modes(struct drm_connector *connector) |
| 58 | { |
| 59 | struct udl_device *udl = connector->dev->dev_private; |
| 60 | struct edid *edid; |
| 61 | int ret; |
| 62 | |
| 63 | edid = (struct edid *)udl_get_edid(udl); |
| 64 | |
Hans de Goede | c930812 | 2013-01-11 12:08:56 +0100 | [diff] [blame] | 65 | /* |
| 66 | * We only read the main block, but if the monitor reports extension |
| 67 | * blocks then the drm edid code expects them to be present, so patch |
| 68 | * the extension count to 0. |
| 69 | */ |
| 70 | edid->checksum += edid->extensions; |
| 71 | edid->extensions = 0; |
| 72 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 73 | drm_mode_connector_update_edid_property(connector, edid); |
| 74 | ret = drm_add_edid_modes(connector, edid); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 75 | kfree(edid); |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | static int udl_mode_valid(struct drm_connector *connector, |
| 80 | struct drm_display_mode *mode) |
| 81 | { |
Dave Airlie | 3a75885 | 2012-09-25 16:17:43 +1000 | [diff] [blame] | 82 | struct udl_device *udl = connector->dev->dev_private; |
| 83 | if (!udl->sku_pixel_limit) |
| 84 | return 0; |
| 85 | |
| 86 | if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit) |
| 87 | return MODE_VIRTUAL_Y; |
| 88 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static enum drm_connector_status |
| 93 | udl_detect(struct drm_connector *connector, bool force) |
| 94 | { |
| 95 | if (drm_device_is_unplugged(connector->dev)) |
| 96 | return connector_status_disconnected; |
| 97 | return connector_status_connected; |
| 98 | } |
| 99 | |
Sachin Kamat | b27b6d3 | 2012-11-19 05:27:43 +0000 | [diff] [blame] | 100 | static struct drm_encoder* |
| 101 | udl_best_single_encoder(struct drm_connector *connector) |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 102 | { |
| 103 | int enc_id = connector->encoder_ids[0]; |
| 104 | struct drm_mode_object *obj; |
| 105 | struct drm_encoder *encoder; |
| 106 | |
| 107 | obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER); |
| 108 | if (!obj) |
| 109 | return NULL; |
| 110 | encoder = obj_to_encoder(obj); |
| 111 | return encoder; |
| 112 | } |
| 113 | |
Sachin Kamat | b27b6d3 | 2012-11-19 05:27:43 +0000 | [diff] [blame] | 114 | static int udl_connector_set_property(struct drm_connector *connector, |
| 115 | struct drm_property *property, |
| 116 | uint64_t val) |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 117 | { |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static void udl_connector_destroy(struct drm_connector *connector) |
| 122 | { |
| 123 | drm_sysfs_connector_remove(connector); |
| 124 | drm_connector_cleanup(connector); |
| 125 | kfree(connector); |
| 126 | } |
| 127 | |
Sachin Kamat | b27b6d3 | 2012-11-19 05:27:43 +0000 | [diff] [blame] | 128 | static struct drm_connector_helper_funcs udl_connector_helper_funcs = { |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 129 | .get_modes = udl_get_modes, |
| 130 | .mode_valid = udl_mode_valid, |
| 131 | .best_encoder = udl_best_single_encoder, |
| 132 | }; |
| 133 | |
Sachin Kamat | b27b6d3 | 2012-11-19 05:27:43 +0000 | [diff] [blame] | 134 | static struct drm_connector_funcs udl_connector_funcs = { |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 135 | .dpms = drm_helper_connector_dpms, |
| 136 | .detect = udl_detect, |
| 137 | .fill_modes = drm_helper_probe_single_connector_modes, |
| 138 | .destroy = udl_connector_destroy, |
| 139 | .set_property = udl_connector_set_property, |
| 140 | }; |
| 141 | |
| 142 | int udl_connector_init(struct drm_device *dev, struct drm_encoder *encoder) |
| 143 | { |
| 144 | struct drm_connector *connector; |
| 145 | |
| 146 | connector = kzalloc(sizeof(struct drm_connector), GFP_KERNEL); |
| 147 | if (!connector) |
| 148 | return -ENOMEM; |
| 149 | |
| 150 | drm_connector_init(dev, connector, &udl_connector_funcs, DRM_MODE_CONNECTOR_DVII); |
| 151 | drm_connector_helper_add(connector, &udl_connector_helper_funcs); |
| 152 | |
| 153 | drm_sysfs_connector_add(connector); |
| 154 | drm_mode_connector_attach_encoder(connector, encoder); |
| 155 | |
Rob Clark | 99d1dba | 2012-10-11 20:46:48 -0500 | [diff] [blame] | 156 | drm_object_attach_property(&connector->base, |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 157 | dev->mode_config.dirty_info_property, |
| 158 | 1); |
| 159 | return 0; |
| 160 | } |