drm/msm/hdmi: Enable HPD after HDMI IRQ is set up
[linux/fpc-iii.git] / drivers / gpu / drm / drm_sysfs.c
blobecb7b33002bb27de0af599702a354e7c241cd6ed
2 /*
3 * drm_sysfs.c - Modifications to drm_sysfs_class.c to support
4 * extra sysfs attribute from DRM. Normal drm_sysfs_class
5 * does not allow adding attributes.
7 * Copyright (c) 2004 Jon Smirl <jonsmirl@gmail.com>
8 * Copyright (c) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
9 * Copyright (c) 2003-2004 IBM Corp.
11 * This file is released under the GPLv2
15 #include <linux/device.h>
16 #include <linux/kdev_t.h>
17 #include <linux/gfp.h>
18 #include <linux/err.h>
19 #include <linux/export.h>
21 #include <drm/drm_sysfs.h>
22 #include <drm/drmP.h>
23 #include "drm_internal.h"
25 #define to_drm_minor(d) dev_get_drvdata(d)
26 #define to_drm_connector(d) dev_get_drvdata(d)
28 /**
29 * DOC: overview
31 * DRM provides very little additional support to drivers for sysfs
32 * interactions, beyond just all the standard stuff. Drivers who want to expose
33 * additional sysfs properties and property groups can attach them at either
34 * &drm_device.dev or &drm_connector.kdev.
36 * Registration is automatically handled when calling drm_dev_register(), or
37 * drm_connector_register() in case of hot-plugged connectors. Unregistration is
38 * also automatically handled by drm_dev_unregister() and
39 * drm_connector_unregister().
42 static struct device_type drm_sysfs_device_minor = {
43 .name = "drm_minor"
46 struct class *drm_class;
48 static char *drm_devnode(struct device *dev, umode_t *mode)
50 return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
53 static CLASS_ATTR_STRING(version, S_IRUGO, "drm 1.1.0 20060810");
55 /**
56 * drm_sysfs_init - initialize sysfs helpers
58 * This is used to create the DRM class, which is the implicit parent of any
59 * other top-level DRM sysfs objects.
61 * You must call drm_sysfs_destroy() to release the allocated resources.
63 * Return: 0 on success, negative error code on failure.
65 int drm_sysfs_init(void)
67 int err;
69 drm_class = class_create(THIS_MODULE, "drm");
70 if (IS_ERR(drm_class))
71 return PTR_ERR(drm_class);
73 err = class_create_file(drm_class, &class_attr_version.attr);
74 if (err) {
75 class_destroy(drm_class);
76 drm_class = NULL;
77 return err;
80 drm_class->devnode = drm_devnode;
81 return 0;
84 /**
85 * drm_sysfs_destroy - destroys DRM class
87 * Destroy the DRM device class.
89 void drm_sysfs_destroy(void)
91 if (IS_ERR_OR_NULL(drm_class))
92 return;
93 class_remove_file(drm_class, &class_attr_version.attr);
94 class_destroy(drm_class);
95 drm_class = NULL;
99 * Connector properties
101 static ssize_t status_store(struct device *device,
102 struct device_attribute *attr,
103 const char *buf, size_t count)
105 struct drm_connector *connector = to_drm_connector(device);
106 struct drm_device *dev = connector->dev;
107 enum drm_connector_force old_force;
108 int ret;
110 ret = mutex_lock_interruptible(&dev->mode_config.mutex);
111 if (ret)
112 return ret;
114 old_force = connector->force;
116 if (sysfs_streq(buf, "detect"))
117 connector->force = 0;
118 else if (sysfs_streq(buf, "on"))
119 connector->force = DRM_FORCE_ON;
120 else if (sysfs_streq(buf, "on-digital"))
121 connector->force = DRM_FORCE_ON_DIGITAL;
122 else if (sysfs_streq(buf, "off"))
123 connector->force = DRM_FORCE_OFF;
124 else
125 ret = -EINVAL;
127 if (old_force != connector->force || !connector->force) {
128 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force updated from %d to %d or reprobing\n",
129 connector->base.id,
130 connector->name,
131 old_force, connector->force);
133 connector->funcs->fill_modes(connector,
134 dev->mode_config.max_width,
135 dev->mode_config.max_height);
138 mutex_unlock(&dev->mode_config.mutex);
140 return ret ? ret : count;
143 static ssize_t status_show(struct device *device,
144 struct device_attribute *attr,
145 char *buf)
147 struct drm_connector *connector = to_drm_connector(device);
148 enum drm_connector_status status;
150 status = READ_ONCE(connector->status);
152 return snprintf(buf, PAGE_SIZE, "%s\n",
153 drm_get_connector_status_name(status));
156 static ssize_t dpms_show(struct device *device,
157 struct device_attribute *attr,
158 char *buf)
160 struct drm_connector *connector = to_drm_connector(device);
161 int dpms;
163 dpms = READ_ONCE(connector->dpms);
165 return snprintf(buf, PAGE_SIZE, "%s\n",
166 drm_get_dpms_name(dpms));
169 static ssize_t enabled_show(struct device *device,
170 struct device_attribute *attr,
171 char *buf)
173 struct drm_connector *connector = to_drm_connector(device);
174 bool enabled;
176 enabled = READ_ONCE(connector->encoder);
178 return snprintf(buf, PAGE_SIZE, enabled ? "enabled\n" : "disabled\n");
181 static ssize_t edid_show(struct file *filp, struct kobject *kobj,
182 struct bin_attribute *attr, char *buf, loff_t off,
183 size_t count)
185 struct device *connector_dev = kobj_to_dev(kobj);
186 struct drm_connector *connector = to_drm_connector(connector_dev);
187 unsigned char *edid;
188 size_t size;
189 ssize_t ret = 0;
191 mutex_lock(&connector->dev->mode_config.mutex);
192 if (!connector->edid_blob_ptr)
193 goto unlock;
195 edid = connector->edid_blob_ptr->data;
196 size = connector->edid_blob_ptr->length;
197 if (!edid)
198 goto unlock;
200 if (off >= size)
201 goto unlock;
203 if (off + count > size)
204 count = size - off;
205 memcpy(buf, edid + off, count);
207 ret = count;
208 unlock:
209 mutex_unlock(&connector->dev->mode_config.mutex);
211 return ret;
214 static ssize_t modes_show(struct device *device,
215 struct device_attribute *attr,
216 char *buf)
218 struct drm_connector *connector = to_drm_connector(device);
219 struct drm_display_mode *mode;
220 int written = 0;
222 mutex_lock(&connector->dev->mode_config.mutex);
223 list_for_each_entry(mode, &connector->modes, head) {
224 written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
225 mode->name);
227 mutex_unlock(&connector->dev->mode_config.mutex);
229 return written;
232 static DEVICE_ATTR_RW(status);
233 static DEVICE_ATTR_RO(enabled);
234 static DEVICE_ATTR_RO(dpms);
235 static DEVICE_ATTR_RO(modes);
237 static struct attribute *connector_dev_attrs[] = {
238 &dev_attr_status.attr,
239 &dev_attr_enabled.attr,
240 &dev_attr_dpms.attr,
241 &dev_attr_modes.attr,
242 NULL
245 static struct bin_attribute edid_attr = {
246 .attr.name = "edid",
247 .attr.mode = 0444,
248 .size = 0,
249 .read = edid_show,
252 static struct bin_attribute *connector_bin_attrs[] = {
253 &edid_attr,
254 NULL
257 static const struct attribute_group connector_dev_group = {
258 .attrs = connector_dev_attrs,
259 .bin_attrs = connector_bin_attrs,
262 static const struct attribute_group *connector_dev_groups[] = {
263 &connector_dev_group,
264 NULL
267 int drm_sysfs_connector_add(struct drm_connector *connector)
269 struct drm_device *dev = connector->dev;
271 if (connector->kdev)
272 return 0;
274 connector->kdev =
275 device_create_with_groups(drm_class, dev->primary->kdev, 0,
276 connector, connector_dev_groups,
277 "card%d-%s", dev->primary->index,
278 connector->name);
279 DRM_DEBUG("adding \"%s\" to sysfs\n",
280 connector->name);
282 if (IS_ERR(connector->kdev)) {
283 DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev));
284 return PTR_ERR(connector->kdev);
287 /* Let userspace know we have a new connector */
288 drm_sysfs_hotplug_event(dev);
290 return 0;
293 void drm_sysfs_connector_remove(struct drm_connector *connector)
295 if (!connector->kdev)
296 return;
297 DRM_DEBUG("removing \"%s\" from sysfs\n",
298 connector->name);
300 device_unregister(connector->kdev);
301 connector->kdev = NULL;
304 void drm_sysfs_lease_event(struct drm_device *dev)
306 char *event_string = "LEASE=1";
307 char *envp[] = { event_string, NULL };
309 DRM_DEBUG("generating lease event\n");
311 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
315 * drm_sysfs_hotplug_event - generate a DRM uevent
316 * @dev: DRM device
318 * Send a uevent for the DRM device specified by @dev. Currently we only
319 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
320 * deal with other types of events.
322 void drm_sysfs_hotplug_event(struct drm_device *dev)
324 char *event_string = "HOTPLUG=1";
325 char *envp[] = { event_string, NULL };
327 DRM_DEBUG("generating hotplug event\n");
329 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
331 EXPORT_SYMBOL(drm_sysfs_hotplug_event);
333 static void drm_sysfs_release(struct device *dev)
335 kfree(dev);
338 struct device *drm_sysfs_minor_alloc(struct drm_minor *minor)
340 const char *minor_str;
341 struct device *kdev;
342 int r;
344 if (minor->type == DRM_MINOR_RENDER)
345 minor_str = "renderD%d";
346 else
347 minor_str = "card%d";
349 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
350 if (!kdev)
351 return ERR_PTR(-ENOMEM);
353 device_initialize(kdev);
354 kdev->devt = MKDEV(DRM_MAJOR, minor->index);
355 kdev->class = drm_class;
356 kdev->type = &drm_sysfs_device_minor;
357 kdev->parent = minor->dev->dev;
358 kdev->release = drm_sysfs_release;
359 dev_set_drvdata(kdev, minor);
361 r = dev_set_name(kdev, minor_str, minor->index);
362 if (r < 0)
363 goto err_free;
365 return kdev;
367 err_free:
368 put_device(kdev);
369 return ERR_PTR(r);
373 * drm_class_device_register - register new device with the DRM sysfs class
374 * @dev: device to register
376 * Registers a new &struct device within the DRM sysfs class. Essentially only
377 * used by ttm to have a place for its global settings. Drivers should never use
378 * this.
380 int drm_class_device_register(struct device *dev)
382 if (!drm_class || IS_ERR(drm_class))
383 return -ENOENT;
385 dev->class = drm_class;
386 return device_register(dev);
388 EXPORT_SYMBOL_GPL(drm_class_device_register);
391 * drm_class_device_unregister - unregister device with the DRM sysfs class
392 * @dev: device to unregister
394 * Unregisters a &struct device from the DRM sysfs class. Essentially only used
395 * by ttm to have a place for its global settings. Drivers should never use
396 * this.
398 void drm_class_device_unregister(struct device *dev)
400 return device_unregister(dev);
402 EXPORT_SYMBOL_GPL(drm_class_device_unregister);