adf_sysfs.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (C) 2013 Google, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <video/adf_client.h>
  15. #include "adf.h"
  16. #include "adf_fops.h"
  17. #include "adf_sysfs.h"
  18. static struct class *adf_class;
  19. static int adf_major;
  20. static DEFINE_IDR(adf_minors);
  21. #define dev_to_adf_interface(p) \
  22. adf_obj_to_interface(container_of(p, struct adf_obj, dev))
  23. static ssize_t dpms_state_show(struct device *dev,
  24. struct device_attribute *attr, char *buf)
  25. {
  26. struct adf_interface *intf = dev_to_adf_interface(dev);
  27. return scnprintf(buf, PAGE_SIZE, "%u\n",
  28. adf_interface_dpms_state(intf));
  29. }
  30. static ssize_t dpms_state_store(struct device *dev,
  31. struct device_attribute *attr, const char *buf, size_t count)
  32. {
  33. struct adf_interface *intf = dev_to_adf_interface(dev);
  34. u8 dpms_state;
  35. int err;
  36. err = kstrtou8(buf, 0, &dpms_state);
  37. if (err < 0)
  38. return err;
  39. err = adf_interface_blank(intf, dpms_state);
  40. if (err < 0)
  41. return err;
  42. return count;
  43. }
  44. static ssize_t current_mode_show(struct device *dev,
  45. struct device_attribute *attr, char *buf)
  46. {
  47. struct adf_interface *intf = dev_to_adf_interface(dev);
  48. struct drm_mode_modeinfo mode;
  49. adf_interface_current_mode(intf, &mode);
  50. if (mode.name[0]) {
  51. return scnprintf(buf, PAGE_SIZE, "%s\n", mode.name);
  52. } else {
  53. bool interlaced = !!(mode.flags & DRM_MODE_FLAG_INTERLACE);
  54. return scnprintf(buf, PAGE_SIZE, "%ux%u%s\n", mode.hdisplay,
  55. mode.vdisplay, interlaced ? "i" : "");
  56. }
  57. }
  58. static ssize_t type_show(struct device *dev, struct device_attribute *attr,
  59. char *buf)
  60. {
  61. struct adf_interface *intf = dev_to_adf_interface(dev);
  62. return scnprintf(buf, PAGE_SIZE, "%s\n",
  63. adf_interface_type_str(intf));
  64. }
  65. static ssize_t vsync_timestamp_show(struct device *dev,
  66. struct device_attribute *attr, char *buf)
  67. {
  68. struct adf_interface *intf = dev_to_adf_interface(dev);
  69. ktime_t timestamp;
  70. unsigned long flags;
  71. read_lock_irqsave(&intf->vsync_lock, flags);
  72. memcpy(&timestamp, &intf->vsync_timestamp, sizeof(timestamp));
  73. read_unlock_irqrestore(&intf->vsync_lock, flags);
  74. return scnprintf(buf, PAGE_SIZE, "%llu\n", ktime_to_ns(timestamp));
  75. }
  76. static ssize_t hotplug_detect_show(struct device *dev,
  77. struct device_attribute *attr, char *buf)
  78. {
  79. struct adf_interface *intf = dev_to_adf_interface(dev);
  80. return scnprintf(buf, PAGE_SIZE, "%u\n", intf->hotplug_detect);
  81. }
  82. static struct device_attribute adf_interface_attrs[] = {
  83. __ATTR(dpms_state, S_IRUGO|S_IWUSR, dpms_state_show, dpms_state_store),
  84. __ATTR_RO(current_mode),
  85. __ATTR_RO(hotplug_detect),
  86. __ATTR_RO(type),
  87. __ATTR_RO(vsync_timestamp),
  88. };
  89. int adf_obj_sysfs_init(struct adf_obj *obj, struct device *parent)
  90. {
  91. int ret = idr_alloc(&adf_minors, obj, 0, 0, GFP_KERNEL);
  92. if (ret < 0) {
  93. pr_err("%s: allocating adf minor failed: %d\n", __func__,
  94. ret);
  95. return ret;
  96. }
  97. obj->minor = ret;
  98. obj->dev.parent = parent;
  99. obj->dev.class = adf_class;
  100. obj->dev.devt = MKDEV(adf_major, obj->minor);
  101. ret = device_register(&obj->dev);
  102. if (ret < 0) {
  103. pr_err("%s: registering adf object failed: %d\n", __func__,
  104. ret);
  105. goto err_device_register;
  106. }
  107. return 0;
  108. err_device_register:
  109. idr_remove(&adf_minors, obj->minor);
  110. return ret;
  111. }
  112. static char *adf_device_devnode(struct device *dev, umode_t *mode,
  113. kuid_t *uid, kgid_t *gid)
  114. {
  115. struct adf_obj *obj = container_of(dev, struct adf_obj, dev);
  116. return kasprintf(GFP_KERNEL, "adf%d", obj->id);
  117. }
  118. static char *adf_interface_devnode(struct device *dev, umode_t *mode,
  119. kuid_t *uid, kgid_t *gid)
  120. {
  121. struct adf_obj *obj = container_of(dev, struct adf_obj, dev);
  122. struct adf_interface *intf = adf_obj_to_interface(obj);
  123. struct adf_device *parent = adf_interface_parent(intf);
  124. return kasprintf(GFP_KERNEL, "adf-interface%d.%d",
  125. parent->base.id, intf->base.id);
  126. }
  127. static char *adf_overlay_engine_devnode(struct device *dev, umode_t *mode,
  128. kuid_t *uid, kgid_t *gid)
  129. {
  130. struct adf_obj *obj = container_of(dev, struct adf_obj, dev);
  131. struct adf_overlay_engine *eng = adf_obj_to_overlay_engine(obj);
  132. struct adf_device *parent = adf_overlay_engine_parent(eng);
  133. return kasprintf(GFP_KERNEL, "adf-overlay-engine%d.%d",
  134. parent->base.id, eng->base.id);
  135. }
  136. static void adf_noop_release(struct device *dev)
  137. {
  138. }
  139. static struct device_type adf_device_type = {
  140. .name = "adf_device",
  141. .devnode = adf_device_devnode,
  142. .release = adf_noop_release,
  143. };
  144. static struct device_type adf_interface_type = {
  145. .name = "adf_interface",
  146. .devnode = adf_interface_devnode,
  147. .release = adf_noop_release,
  148. };
  149. static struct device_type adf_overlay_engine_type = {
  150. .name = "adf_overlay_engine",
  151. .devnode = adf_overlay_engine_devnode,
  152. .release = adf_noop_release,
  153. };
  154. int adf_device_sysfs_init(struct adf_device *dev)
  155. {
  156. dev->base.dev.type = &adf_device_type;
  157. dev_set_name(&dev->base.dev, "%s", dev->base.name);
  158. return adf_obj_sysfs_init(&dev->base, dev->dev);
  159. }
  160. int adf_interface_sysfs_init(struct adf_interface *intf)
  161. {
  162. struct adf_device *parent = adf_interface_parent(intf);
  163. size_t i, j;
  164. int ret;
  165. intf->base.dev.type = &adf_interface_type;
  166. dev_set_name(&intf->base.dev, "%s-interface%d", parent->base.name,
  167. intf->base.id);
  168. ret = adf_obj_sysfs_init(&intf->base, &parent->base.dev);
  169. if (ret < 0)
  170. return ret;
  171. for (i = 0; i < ARRAY_SIZE(adf_interface_attrs); i++) {
  172. ret = device_create_file(&intf->base.dev,
  173. &adf_interface_attrs[i]);
  174. if (ret < 0) {
  175. dev_err(&intf->base.dev, "creating sysfs attribute %s failed: %d\n",
  176. adf_interface_attrs[i].attr.name, ret);
  177. goto err;
  178. }
  179. }
  180. return 0;
  181. err:
  182. for (j = 0; j < i; j++)
  183. device_remove_file(&intf->base.dev, &adf_interface_attrs[j]);
  184. return ret;
  185. }
  186. int adf_overlay_engine_sysfs_init(struct adf_overlay_engine *eng)
  187. {
  188. struct adf_device *parent = adf_overlay_engine_parent(eng);
  189. eng->base.dev.type = &adf_overlay_engine_type;
  190. dev_set_name(&eng->base.dev, "%s-overlay-engine%d", parent->base.name,
  191. eng->base.id);
  192. return adf_obj_sysfs_init(&eng->base, &parent->base.dev);
  193. }
  194. struct adf_obj *adf_obj_sysfs_find(int minor)
  195. {
  196. return idr_find(&adf_minors, minor);
  197. }
  198. void adf_obj_sysfs_destroy(struct adf_obj *obj)
  199. {
  200. idr_remove(&adf_minors, obj->minor);
  201. device_unregister(&obj->dev);
  202. }
  203. void adf_device_sysfs_destroy(struct adf_device *dev)
  204. {
  205. adf_obj_sysfs_destroy(&dev->base);
  206. }
  207. void adf_interface_sysfs_destroy(struct adf_interface *intf)
  208. {
  209. size_t i;
  210. for (i = 0; i < ARRAY_SIZE(adf_interface_attrs); i++)
  211. device_remove_file(&intf->base.dev, &adf_interface_attrs[i]);
  212. adf_obj_sysfs_destroy(&intf->base);
  213. }
  214. void adf_overlay_engine_sysfs_destroy(struct adf_overlay_engine *eng)
  215. {
  216. adf_obj_sysfs_destroy(&eng->base);
  217. }
  218. int adf_sysfs_init(void)
  219. {
  220. struct class *class;
  221. int ret;
  222. class = class_create(THIS_MODULE, "adf");
  223. if (IS_ERR(class)) {
  224. ret = PTR_ERR(class);
  225. pr_err("%s: creating class failed: %d\n", __func__, ret);
  226. return ret;
  227. }
  228. ret = register_chrdev(0, "adf", &adf_fops);
  229. if (ret < 0) {
  230. pr_err("%s: registering device failed: %d\n", __func__, ret);
  231. goto err_chrdev;
  232. }
  233. adf_class = class;
  234. adf_major = ret;
  235. return 0;
  236. err_chrdev:
  237. class_destroy(adf_class);
  238. return ret;
  239. }
  240. void adf_sysfs_destroy(void)
  241. {
  242. idr_destroy(&adf_minors);
  243. class_destroy(adf_class);
  244. }