drm/sysfs: rename connector modes' name
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / drm_sysfs.c
1
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.
6  *
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.
10  *
11  * This file is released under the GPLv2
12  *
13  */
14
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>
20
21 #include <drm/drm_sysfs.h>
22 #include <drm/drm_core.h>
23 #include <drm/drmP.h>
24 #include "drm_internal.h"
25
26 #define to_drm_minor(d) dev_get_drvdata(d)
27 #define to_drm_connector(d) dev_get_drvdata(d)
28
29 static struct device_type drm_sysfs_device_minor = {
30         .name = "drm_minor"
31 };
32
33 struct class *drm_class;
34
35 /**
36  * __drm_class_suspend - internal DRM class suspend routine
37  * @dev: Linux device to suspend
38  * @state: power state to enter
39  *
40  * Just figures out what the actual struct drm_device associated with
41  * @dev is and calls its suspend hook, if present.
42  */
43 static int __drm_class_suspend(struct device *dev, pm_message_t state)
44 {
45         if (dev->type == &drm_sysfs_device_minor) {
46                 struct drm_minor *drm_minor = to_drm_minor(dev);
47                 struct drm_device *drm_dev = drm_minor->dev;
48
49                 if (drm_minor->type == DRM_MINOR_LEGACY &&
50                     !drm_core_check_feature(drm_dev, DRIVER_MODESET) &&
51                     drm_dev->driver->suspend)
52                         return drm_dev->driver->suspend(drm_dev, state);
53         }
54         return 0;
55 }
56
57 /**
58  * drm_class_suspend - internal DRM class suspend hook. Simply calls
59  * __drm_class_suspend() with the correct pm state.
60  * @dev: Linux device to suspend
61  */
62 static int drm_class_suspend(struct device *dev)
63 {
64         return __drm_class_suspend(dev, PMSG_SUSPEND);
65 }
66
67 /**
68  * drm_class_freeze - internal DRM class freeze hook. Simply calls
69  * __drm_class_suspend() with the correct pm state.
70  * @dev: Linux device to freeze
71  */
72 static int drm_class_freeze(struct device *dev)
73 {
74         return __drm_class_suspend(dev, PMSG_FREEZE);
75 }
76
77 /**
78  * drm_class_resume - DRM class resume hook
79  * @dev: Linux device to resume
80  *
81  * Just figures out what the actual struct drm_device associated with
82  * @dev is and calls its resume hook, if present.
83  */
84 static int drm_class_resume(struct device *dev)
85 {
86         if (dev->type == &drm_sysfs_device_minor) {
87                 struct drm_minor *drm_minor = to_drm_minor(dev);
88                 struct drm_device *drm_dev = drm_minor->dev;
89
90                 if (drm_minor->type == DRM_MINOR_LEGACY &&
91                     !drm_core_check_feature(drm_dev, DRIVER_MODESET) &&
92                     drm_dev->driver->resume)
93                         return drm_dev->driver->resume(drm_dev);
94         }
95         return 0;
96 }
97
98 static const struct dev_pm_ops drm_class_dev_pm_ops = {
99         .suspend        = drm_class_suspend,
100         .resume         = drm_class_resume,
101         .freeze         = drm_class_freeze,
102 };
103
104 static char *drm_devnode(struct device *dev, umode_t *mode)
105 {
106         return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
107 }
108
109 static CLASS_ATTR_STRING(version, S_IRUGO,
110                 CORE_NAME " "
111                 __stringify(CORE_MAJOR) "."
112                 __stringify(CORE_MINOR) "."
113                 __stringify(CORE_PATCHLEVEL) " "
114                 CORE_DATE);
115
116 /**
117  * drm_sysfs_init - initialize sysfs helpers
118  *
119  * This is used to create the DRM class, which is the implicit parent of any
120  * other top-level DRM sysfs objects.
121  *
122  * You must call drm_sysfs_destroy() to release the allocated resources.
123  *
124  * Return: 0 on success, negative error code on failure.
125  */
126 int drm_sysfs_init(void)
127 {
128         int err;
129
130         drm_class = class_create(THIS_MODULE, "drm");
131         if (IS_ERR(drm_class))
132                 return PTR_ERR(drm_class);
133
134         drm_class->pm = &drm_class_dev_pm_ops;
135
136         err = class_create_file(drm_class, &class_attr_version.attr);
137         if (err) {
138                 class_destroy(drm_class);
139                 drm_class = NULL;
140                 return err;
141         }
142
143         drm_class->devnode = drm_devnode;
144         return 0;
145 }
146
147 /**
148  * drm_sysfs_destroy - destroys DRM class
149  *
150  * Destroy the DRM device class.
151  */
152 void drm_sysfs_destroy(void)
153 {
154         if (IS_ERR_OR_NULL(drm_class))
155                 return;
156         class_remove_file(drm_class, &class_attr_version.attr);
157         class_destroy(drm_class);
158         drm_class = NULL;
159 }
160
161 /*
162  * Connector properties
163  */
164 static ssize_t status_store(struct device *device,
165                            struct device_attribute *attr,
166                            const char *buf, size_t count)
167 {
168         struct drm_connector *connector = to_drm_connector(device);
169         struct drm_device *dev = connector->dev;
170         enum drm_connector_status old_status;
171         int ret;
172
173         ret = mutex_lock_interruptible(&dev->mode_config.mutex);
174         if (ret)
175                 return ret;
176
177         old_status = connector->status;
178
179         if (sysfs_streq(buf, "detect")) {
180                 connector->force = 0;
181                 connector->status = connector->funcs->detect(connector, true);
182         } else if (sysfs_streq(buf, "on")) {
183                 connector->force = DRM_FORCE_ON;
184         } else if (sysfs_streq(buf, "on-digital")) {
185                 connector->force = DRM_FORCE_ON_DIGITAL;
186         } else if (sysfs_streq(buf, "off")) {
187                 connector->force = DRM_FORCE_OFF;
188         } else
189                 ret = -EINVAL;
190
191         if (ret == 0 && connector->force) {
192                 if (connector->force == DRM_FORCE_ON ||
193                     connector->force == DRM_FORCE_ON_DIGITAL)
194                         connector->status = connector_status_connected;
195                 else
196                         connector->status = connector_status_disconnected;
197                 if (connector->funcs->force)
198                         connector->funcs->force(connector);
199         }
200
201         if (old_status != connector->status) {
202                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
203                               connector->base.id,
204                               connector->name,
205                               old_status, connector->status);
206
207                 dev->mode_config.delayed_event = true;
208                 if (dev->mode_config.poll_enabled)
209                         schedule_delayed_work(&dev->mode_config.output_poll_work,
210                                               0);
211         }
212
213         mutex_unlock(&dev->mode_config.mutex);
214
215         return ret ? ret : count;
216 }
217
218 static ssize_t status_show(struct device *device,
219                            struct device_attribute *attr,
220                            char *buf)
221 {
222         struct drm_connector *connector = to_drm_connector(device);
223
224         return snprintf(buf, PAGE_SIZE, "%s\n",
225                         drm_get_connector_status_name(connector->status));
226 }
227
228 static ssize_t dpms_show(struct device *device,
229                            struct device_attribute *attr,
230                            char *buf)
231 {
232         struct drm_connector *connector = to_drm_connector(device);
233         int dpms;
234
235         dpms = READ_ONCE(connector->dpms);
236
237         return snprintf(buf, PAGE_SIZE, "%s\n",
238                         drm_get_dpms_name(dpms));
239 }
240
241 static ssize_t enabled_show(struct device *device,
242                             struct device_attribute *attr,
243                            char *buf)
244 {
245         struct drm_connector *connector = to_drm_connector(device);
246
247         return snprintf(buf, PAGE_SIZE, "%s\n", connector->encoder ? "enabled" :
248                         "disabled");
249 }
250
251 static ssize_t edid_show(struct file *filp, struct kobject *kobj,
252                          struct bin_attribute *attr, char *buf, loff_t off,
253                          size_t count)
254 {
255         struct device *connector_dev = container_of(kobj, struct device, kobj);
256         struct drm_connector *connector = to_drm_connector(connector_dev);
257         unsigned char *edid;
258         size_t size;
259
260         if (!connector->edid_blob_ptr)
261                 return 0;
262
263         edid = connector->edid_blob_ptr->data;
264         size = connector->edid_blob_ptr->length;
265         if (!edid)
266                 return 0;
267
268         if (off >= size)
269                 return 0;
270
271         if (off + count > size)
272                 count = size - off;
273         memcpy(buf, edid + off, count);
274
275         return count;
276 }
277
278 static ssize_t modes_show(struct device *device,
279                            struct device_attribute *attr,
280                            char *buf)
281 {
282         struct drm_connector *connector = to_drm_connector(device);
283         struct drm_display_mode *mode;
284         int written = 0;
285
286         list_for_each_entry(mode, &connector->modes, head) {
287                 bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
288
289                 written += snprintf(buf + written, PAGE_SIZE - written,
290                                     "%dx%d%s%d\n",
291                                     mode->hdisplay, mode->vdisplay,
292                                     interlaced ? "i" : "p",
293                                     drm_mode_vrefresh(mode));
294         }
295
296         return written;
297 }
298
299 static ssize_t tv_subconnector_show(struct device *device,
300                                     struct device_attribute *attr,
301                                     char *buf)
302 {
303         struct drm_connector *connector = to_drm_connector(device);
304         struct drm_device *dev = connector->dev;
305         struct drm_property *prop;
306         uint64_t subconnector;
307         int ret;
308
309         prop = dev->mode_config.tv_subconnector_property;
310         if (!prop) {
311                 DRM_ERROR("Unable to find subconnector property\n");
312                 return 0;
313         }
314
315         ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
316         if (ret)
317                 return 0;
318
319         return snprintf(buf, PAGE_SIZE, "%s",
320                         drm_get_tv_subconnector_name((int)subconnector));
321 }
322
323 static ssize_t tv_select_subconnector_show(struct device *device,
324                                            struct device_attribute *attr,
325                                            char *buf)
326 {
327         struct drm_connector *connector = to_drm_connector(device);
328         struct drm_device *dev = connector->dev;
329         struct drm_property *prop;
330         uint64_t subconnector;
331         int ret;
332
333         prop = dev->mode_config.tv_select_subconnector_property;
334         if (!prop) {
335                 DRM_ERROR("Unable to find select subconnector property\n");
336                 return 0;
337         }
338
339         ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
340         if (ret)
341                 return 0;
342
343         return snprintf(buf, PAGE_SIZE, "%s",
344                         drm_get_tv_select_name((int)subconnector));
345 }
346
347 static ssize_t dvii_subconnector_show(struct device *device,
348                                       struct device_attribute *attr,
349                                       char *buf)
350 {
351         struct drm_connector *connector = to_drm_connector(device);
352         struct drm_device *dev = connector->dev;
353         struct drm_property *prop;
354         uint64_t subconnector;
355         int ret;
356
357         prop = dev->mode_config.dvi_i_subconnector_property;
358         if (!prop) {
359                 DRM_ERROR("Unable to find subconnector property\n");
360                 return 0;
361         }
362
363         ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
364         if (ret)
365                 return 0;
366
367         return snprintf(buf, PAGE_SIZE, "%s",
368                         drm_get_dvi_i_subconnector_name((int)subconnector));
369 }
370
371 static ssize_t dvii_select_subconnector_show(struct device *device,
372                                              struct device_attribute *attr,
373                                              char *buf)
374 {
375         struct drm_connector *connector = to_drm_connector(device);
376         struct drm_device *dev = connector->dev;
377         struct drm_property *prop;
378         uint64_t subconnector;
379         int ret;
380
381         prop = dev->mode_config.dvi_i_select_subconnector_property;
382         if (!prop) {
383                 DRM_ERROR("Unable to find select subconnector property\n");
384                 return 0;
385         }
386
387         ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
388         if (ret)
389                 return 0;
390
391         return snprintf(buf, PAGE_SIZE, "%s",
392                         drm_get_dvi_i_select_name((int)subconnector));
393 }
394
395 static DEVICE_ATTR_RW(status);
396 static DEVICE_ATTR_RO(enabled);
397 static DEVICE_ATTR_RO(dpms);
398 static DEVICE_ATTR_RO(modes);
399
400 static struct attribute *connector_dev_attrs[] = {
401         &dev_attr_status.attr,
402         &dev_attr_enabled.attr,
403         &dev_attr_dpms.attr,
404         &dev_attr_modes.attr,
405         NULL
406 };
407
408 static DEVICE_ATTR_RO(tv_subconnector);
409 static DEVICE_ATTR_RO(tv_select_subconnector);
410
411 static struct attribute *connector_tv_dev_attrs[] = {
412         &dev_attr_tv_subconnector.attr,
413         &dev_attr_tv_select_subconnector.attr,
414         NULL
415 };
416
417 static DEVICE_ATTR_RO(dvii_subconnector);
418 static DEVICE_ATTR_RO(dvii_select_subconnector);
419
420 static struct attribute *connector_dvii_dev_attrs[] = {
421         &dev_attr_dvii_subconnector.attr,
422         &dev_attr_dvii_select_subconnector.attr,
423         NULL
424 };
425
426 /* Connector type related helpers */
427 static int kobj_connector_type(struct kobject *kobj)
428 {
429         struct device *dev = kobj_to_dev(kobj);
430         struct drm_connector *connector = to_drm_connector(dev);
431
432         return connector->connector_type;
433 }
434
435 static umode_t connector_is_dvii(struct kobject *kobj,
436                                  struct attribute *attr, int idx)
437 {
438         return kobj_connector_type(kobj) == DRM_MODE_CONNECTOR_DVII ?
439                 attr->mode : 0;
440 }
441
442 static umode_t connector_is_tv(struct kobject *kobj,
443                                struct attribute *attr, int idx)
444 {
445         switch (kobj_connector_type(kobj)) {
446         case DRM_MODE_CONNECTOR_Composite:
447         case DRM_MODE_CONNECTOR_SVIDEO:
448         case DRM_MODE_CONNECTOR_Component:
449         case DRM_MODE_CONNECTOR_TV:
450                 return attr->mode;
451         }
452
453         return 0;
454 }
455
456 static struct bin_attribute edid_attr = {
457         .attr.name = "edid",
458         .attr.mode = 0444,
459         .size = 0,
460         .read = edid_show,
461 };
462
463 static struct bin_attribute *connector_bin_attrs[] = {
464         &edid_attr,
465         NULL
466 };
467
468 static const struct attribute_group connector_dev_group = {
469         .attrs = connector_dev_attrs,
470         .bin_attrs = connector_bin_attrs,
471 };
472
473 static const struct attribute_group connector_tv_dev_group = {
474         .attrs = connector_tv_dev_attrs,
475         .is_visible = connector_is_tv,
476 };
477
478 static const struct attribute_group connector_dvii_dev_group = {
479         .attrs = connector_dvii_dev_attrs,
480         .is_visible = connector_is_dvii,
481 };
482
483 static const struct attribute_group *connector_dev_groups[] = {
484         &connector_dev_group,
485         &connector_tv_dev_group,
486         &connector_dvii_dev_group,
487         NULL
488 };
489
490 /**
491  * drm_sysfs_connector_add - add a connector to sysfs
492  * @connector: connector to add
493  *
494  * Create a connector device in sysfs, along with its associated connector
495  * properties (so far, connection status, dpms, mode list & edid) and
496  * generate a hotplug event so userspace knows there's a new connector
497  * available.
498  */
499 int drm_sysfs_connector_add(struct drm_connector *connector)
500 {
501         struct drm_device *dev = connector->dev;
502
503         if (connector->kdev)
504                 return 0;
505
506         connector->kdev =
507                 device_create_with_groups(drm_class, dev->primary->kdev, 0,
508                                           connector, connector_dev_groups,
509                                           "card%d-%s", dev->primary->index,
510                                           connector->name);
511         DRM_DEBUG("adding \"%s\" to sysfs\n",
512                   connector->name);
513
514         if (IS_ERR(connector->kdev)) {
515                 DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev));
516                 return PTR_ERR(connector->kdev);
517         }
518
519         /* Let userspace know we have a new connector */
520         drm_sysfs_hotplug_event(dev);
521
522         return 0;
523 }
524
525 /**
526  * drm_sysfs_connector_remove - remove an connector device from sysfs
527  * @connector: connector to remove
528  *
529  * Remove @connector and its associated attributes from sysfs.  Note that
530  * the device model core will take care of sending the "remove" uevent
531  * at this time, so we don't need to do it.
532  *
533  * Note:
534  * This routine should only be called if the connector was previously
535  * successfully registered.  If @connector hasn't been registered yet,
536  * you'll likely see a panic somewhere deep in sysfs code when called.
537  */
538 void drm_sysfs_connector_remove(struct drm_connector *connector)
539 {
540         if (!connector->kdev)
541                 return;
542         DRM_DEBUG("removing \"%s\" from sysfs\n",
543                   connector->name);
544
545         device_unregister(connector->kdev);
546         connector->kdev = NULL;
547 }
548
549 /**
550  * drm_sysfs_hotplug_event - generate a DRM uevent
551  * @dev: DRM device
552  *
553  * Send a uevent for the DRM device specified by @dev.  Currently we only
554  * set HOTPLUG=1 in the uevent environment, but this could be expanded to
555  * deal with other types of events.
556  */
557 void drm_sysfs_hotplug_event(struct drm_device *dev)
558 {
559         char *event_string = "HOTPLUG=1";
560         char *envp[] = { event_string, NULL };
561
562         DRM_DEBUG("generating hotplug event\n");
563
564         kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
565 }
566 EXPORT_SYMBOL(drm_sysfs_hotplug_event);
567
568 static void drm_sysfs_release(struct device *dev)
569 {
570         kfree(dev);
571 }
572
573 /**
574  * drm_sysfs_minor_alloc() - Allocate sysfs device for given minor
575  * @minor: minor to allocate sysfs device for
576  *
577  * This allocates a new sysfs device for @minor and returns it. The device is
578  * not registered nor linked. The caller has to use device_add() and
579  * device_del() to register and unregister it.
580  *
581  * Note that dev_get_drvdata() on the new device will return the minor.
582  * However, the device does not hold a ref-count to the minor nor to the
583  * underlying drm_device. This is unproblematic as long as you access the
584  * private data only in sysfs callbacks. device_del() disables those
585  * synchronously, so they cannot be called after you cleanup a minor.
586  */
587 struct device *drm_sysfs_minor_alloc(struct drm_minor *minor)
588 {
589         const char *minor_str;
590         struct device *kdev;
591         int r;
592
593         if (minor->type == DRM_MINOR_CONTROL)
594                 minor_str = "controlD%d";
595         else if (minor->type == DRM_MINOR_RENDER)
596                 minor_str = "renderD%d";
597         else
598                 minor_str = "card%d";
599
600         kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
601         if (!kdev)
602                 return ERR_PTR(-ENOMEM);
603
604         device_initialize(kdev);
605         kdev->devt = MKDEV(DRM_MAJOR, minor->index);
606         kdev->class = drm_class;
607         kdev->type = &drm_sysfs_device_minor;
608         kdev->parent = minor->dev->dev;
609         kdev->release = drm_sysfs_release;
610         dev_set_drvdata(kdev, minor);
611
612         r = dev_set_name(kdev, minor_str, minor->index);
613         if (r < 0)
614                 goto err_free;
615
616         return kdev;
617
618 err_free:
619         put_device(kdev);
620         return ERR_PTR(r);
621 }
622
623 /**
624  * drm_class_device_register - Register a struct device in the drm class.
625  *
626  * @dev: pointer to struct device to register.
627  *
628  * @dev should have all relevant members pre-filled with the exception
629  * of the class member. In particular, the device_type member must
630  * be set.
631  */
632
633 int drm_class_device_register(struct device *dev)
634 {
635         if (!drm_class || IS_ERR(drm_class))
636                 return -ENOENT;
637
638         dev->class = drm_class;
639         return device_register(dev);
640 }
641 EXPORT_SYMBOL_GPL(drm_class_device_register);
642
643 void drm_class_device_unregister(struct device *dev)
644 {
645         return device_unregister(dev);
646 }
647 EXPORT_SYMBOL_GPL(drm_class_device_unregister);