display-sysfs.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (C) 2009 Nokia Corporation
  3. * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
  4. *
  5. * Some code and ideas taken from drivers/video/omap/ driver
  6. * by Imre Deak.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published by
  10. * the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #define DSS_SUBSYS_NAME "DISPLAY"
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/sysfs.h>
  25. #include <video/omapdss.h>
  26. #include "dss.h"
  27. static struct omap_dss_device *to_dss_device_sysfs(struct device *dev)
  28. {
  29. struct omap_dss_device *dssdev = NULL;
  30. for_each_dss_dev(dssdev) {
  31. if (dssdev->dev == dev) {
  32. omap_dss_put_device(dssdev);
  33. return dssdev;
  34. }
  35. }
  36. return NULL;
  37. }
  38. static ssize_t display_name_show(struct device *dev,
  39. struct device_attribute *attr, char *buf)
  40. {
  41. struct omap_dss_device *dssdev = to_dss_device_sysfs(dev);
  42. return snprintf(buf, PAGE_SIZE, "%s\n",
  43. dssdev->name ?
  44. dssdev->name : "");
  45. }
  46. static ssize_t display_enabled_show(struct device *dev,
  47. struct device_attribute *attr, char *buf)
  48. {
  49. struct omap_dss_device *dssdev = to_dss_device_sysfs(dev);
  50. return snprintf(buf, PAGE_SIZE, "%d\n",
  51. omapdss_device_is_enabled(dssdev));
  52. }
  53. static ssize_t display_enabled_store(struct device *dev,
  54. struct device_attribute *attr,
  55. const char *buf, size_t size)
  56. {
  57. struct omap_dss_device *dssdev = to_dss_device_sysfs(dev);
  58. int r;
  59. bool enable;
  60. r = strtobool(buf, &enable);
  61. if (r)
  62. return r;
  63. if (enable == omapdss_device_is_enabled(dssdev))
  64. return size;
  65. if (omapdss_device_is_connected(dssdev) == false)
  66. return -ENODEV;
  67. if (enable) {
  68. r = dssdev->driver->enable(dssdev);
  69. if (r)
  70. return r;
  71. } else {
  72. dssdev->driver->disable(dssdev);
  73. }
  74. return size;
  75. }
  76. static ssize_t display_tear_show(struct device *dev,
  77. struct device_attribute *attr, char *buf)
  78. {
  79. struct omap_dss_device *dssdev = to_dss_device_sysfs(dev);
  80. return snprintf(buf, PAGE_SIZE, "%d\n",
  81. dssdev->driver->get_te ?
  82. dssdev->driver->get_te(dssdev) : 0);
  83. }
  84. static ssize_t display_tear_store(struct device *dev,
  85. struct device_attribute *attr, const char *buf, size_t size)
  86. {
  87. struct omap_dss_device *dssdev = to_dss_device_sysfs(dev);
  88. int r;
  89. bool te;
  90. if (!dssdev->driver->enable_te || !dssdev->driver->get_te)
  91. return -ENOENT;
  92. r = strtobool(buf, &te);
  93. if (r)
  94. return r;
  95. r = dssdev->driver->enable_te(dssdev, te);
  96. if (r)
  97. return r;
  98. return size;
  99. }
  100. static ssize_t display_timings_show(struct device *dev,
  101. struct device_attribute *attr, char *buf)
  102. {
  103. struct omap_dss_device *dssdev = to_dss_device_sysfs(dev);
  104. struct omap_video_timings t;
  105. if (!dssdev->driver->get_timings)
  106. return -ENOENT;
  107. dssdev->driver->get_timings(dssdev, &t);
  108. return snprintf(buf, PAGE_SIZE, "%u,%u/%u/%u/%u,%u/%u/%u/%u\n",
  109. t.pixel_clock,
  110. t.x_res, t.hfp, t.hbp, t.hsw,
  111. t.y_res, t.vfp, t.vbp, t.vsw);
  112. }
  113. static ssize_t display_timings_store(struct device *dev,
  114. struct device_attribute *attr, const char *buf, size_t size)
  115. {
  116. struct omap_dss_device *dssdev = to_dss_device_sysfs(dev);
  117. struct omap_video_timings t = dssdev->panel.timings;
  118. int r, found;
  119. if (!dssdev->driver->set_timings || !dssdev->driver->check_timings)
  120. return -ENOENT;
  121. found = 0;
  122. #ifdef CONFIG_OMAP2_DSS_VENC
  123. if (strncmp("pal", buf, 3) == 0) {
  124. t = omap_dss_pal_timings;
  125. found = 1;
  126. } else if (strncmp("ntsc", buf, 4) == 0) {
  127. t = omap_dss_ntsc_timings;
  128. found = 1;
  129. }
  130. #endif
  131. if (!found && sscanf(buf, "%u,%hu/%hu/%hu/%hu,%hu/%hu/%hu/%hu",
  132. &t.pixel_clock,
  133. &t.x_res, &t.hfp, &t.hbp, &t.hsw,
  134. &t.y_res, &t.vfp, &t.vbp, &t.vsw) != 9)
  135. return -EINVAL;
  136. r = dssdev->driver->check_timings(dssdev, &t);
  137. if (r)
  138. return r;
  139. dssdev->driver->disable(dssdev);
  140. dssdev->driver->set_timings(dssdev, &t);
  141. r = dssdev->driver->enable(dssdev);
  142. if (r)
  143. return r;
  144. return size;
  145. }
  146. static ssize_t display_rotate_show(struct device *dev,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. struct omap_dss_device *dssdev = to_dss_device_sysfs(dev);
  150. int rotate;
  151. if (!dssdev->driver->get_rotate)
  152. return -ENOENT;
  153. rotate = dssdev->driver->get_rotate(dssdev);
  154. return snprintf(buf, PAGE_SIZE, "%u\n", rotate);
  155. }
  156. static ssize_t display_rotate_store(struct device *dev,
  157. struct device_attribute *attr, const char *buf, size_t size)
  158. {
  159. struct omap_dss_device *dssdev = to_dss_device_sysfs(dev);
  160. int rot, r;
  161. if (!dssdev->driver->set_rotate || !dssdev->driver->get_rotate)
  162. return -ENOENT;
  163. r = kstrtoint(buf, 0, &rot);
  164. if (r)
  165. return r;
  166. r = dssdev->driver->set_rotate(dssdev, rot);
  167. if (r)
  168. return r;
  169. return size;
  170. }
  171. static ssize_t display_mirror_show(struct device *dev,
  172. struct device_attribute *attr, char *buf)
  173. {
  174. struct omap_dss_device *dssdev = to_dss_device_sysfs(dev);
  175. int mirror;
  176. if (!dssdev->driver->get_mirror)
  177. return -ENOENT;
  178. mirror = dssdev->driver->get_mirror(dssdev);
  179. return snprintf(buf, PAGE_SIZE, "%u\n", mirror);
  180. }
  181. static ssize_t display_mirror_store(struct device *dev,
  182. struct device_attribute *attr, const char *buf, size_t size)
  183. {
  184. struct omap_dss_device *dssdev = to_dss_device_sysfs(dev);
  185. int r;
  186. bool mirror;
  187. if (!dssdev->driver->set_mirror || !dssdev->driver->get_mirror)
  188. return -ENOENT;
  189. r = strtobool(buf, &mirror);
  190. if (r)
  191. return r;
  192. r = dssdev->driver->set_mirror(dssdev, mirror);
  193. if (r)
  194. return r;
  195. return size;
  196. }
  197. static ssize_t display_wss_show(struct device *dev,
  198. struct device_attribute *attr, char *buf)
  199. {
  200. struct omap_dss_device *dssdev = to_dss_device_sysfs(dev);
  201. unsigned int wss;
  202. if (!dssdev->driver->get_wss)
  203. return -ENOENT;
  204. wss = dssdev->driver->get_wss(dssdev);
  205. return snprintf(buf, PAGE_SIZE, "0x%05x\n", wss);
  206. }
  207. static ssize_t display_wss_store(struct device *dev,
  208. struct device_attribute *attr, const char *buf, size_t size)
  209. {
  210. struct omap_dss_device *dssdev = to_dss_device_sysfs(dev);
  211. u32 wss;
  212. int r;
  213. if (!dssdev->driver->get_wss || !dssdev->driver->set_wss)
  214. return -ENOENT;
  215. r = kstrtou32(buf, 0, &wss);
  216. if (r)
  217. return r;
  218. if (wss > 0xfffff)
  219. return -EINVAL;
  220. r = dssdev->driver->set_wss(dssdev, wss);
  221. if (r)
  222. return r;
  223. return size;
  224. }
  225. static DEVICE_ATTR(name, S_IRUGO, display_name_show, NULL);
  226. static DEVICE_ATTR(enabled, S_IRUGO|S_IWUSR,
  227. display_enabled_show, display_enabled_store);
  228. static DEVICE_ATTR(tear_elim, S_IRUGO|S_IWUSR,
  229. display_tear_show, display_tear_store);
  230. static DEVICE_ATTR(timings, S_IRUGO|S_IWUSR,
  231. display_timings_show, display_timings_store);
  232. static DEVICE_ATTR(rotate, S_IRUGO|S_IWUSR,
  233. display_rotate_show, display_rotate_store);
  234. static DEVICE_ATTR(mirror, S_IRUGO|S_IWUSR,
  235. display_mirror_show, display_mirror_store);
  236. static DEVICE_ATTR(wss, S_IRUGO|S_IWUSR,
  237. display_wss_show, display_wss_store);
  238. static const struct attribute *display_sysfs_attrs[] = {
  239. &dev_attr_name.attr,
  240. &dev_attr_enabled.attr,
  241. &dev_attr_tear_elim.attr,
  242. &dev_attr_timings.attr,
  243. &dev_attr_rotate.attr,
  244. &dev_attr_mirror.attr,
  245. &dev_attr_wss.attr,
  246. NULL
  247. };
  248. int display_init_sysfs(struct platform_device *pdev)
  249. {
  250. struct omap_dss_device *dssdev = NULL;
  251. int r;
  252. for_each_dss_dev(dssdev) {
  253. struct kobject *kobj = &dssdev->dev->kobj;
  254. r = sysfs_create_files(kobj, display_sysfs_attrs);
  255. if (r) {
  256. DSSERR("failed to create sysfs files\n");
  257. goto err;
  258. }
  259. r = sysfs_create_link(&pdev->dev.kobj, kobj, dssdev->alias);
  260. if (r) {
  261. sysfs_remove_files(kobj, display_sysfs_attrs);
  262. DSSERR("failed to create sysfs display link\n");
  263. goto err;
  264. }
  265. }
  266. return 0;
  267. err:
  268. display_uninit_sysfs(pdev);
  269. return r;
  270. }
  271. void display_uninit_sysfs(struct platform_device *pdev)
  272. {
  273. struct omap_dss_device *dssdev = NULL;
  274. for_each_dss_dev(dssdev) {
  275. sysfs_remove_link(&pdev->dev.kobj, dssdev->alias);
  276. sysfs_remove_files(&dssdev->dev->kobj,
  277. display_sysfs_attrs);
  278. }
  279. }