overlay-sysfs.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 "OVERLAY"
  21. #include <linux/module.h>
  22. #include <linux/err.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/kobject.h>
  25. #include <linux/platform_device.h>
  26. #include <video/omapdss.h>
  27. #include "dss.h"
  28. #include "dss_features.h"
  29. static ssize_t overlay_name_show(struct omap_overlay *ovl, char *buf)
  30. {
  31. return snprintf(buf, PAGE_SIZE, "%s\n", ovl->name);
  32. }
  33. static ssize_t overlay_manager_show(struct omap_overlay *ovl, char *buf)
  34. {
  35. return snprintf(buf, PAGE_SIZE, "%s\n",
  36. ovl->manager ? ovl->manager->name : "<none>");
  37. }
  38. static ssize_t overlay_manager_store(struct omap_overlay *ovl, const char *buf,
  39. size_t size)
  40. {
  41. int i, r;
  42. struct omap_overlay_manager *mgr = NULL;
  43. struct omap_overlay_manager *old_mgr;
  44. int len = size;
  45. if (buf[size-1] == '\n')
  46. --len;
  47. if (len > 0) {
  48. for (i = 0; i < omap_dss_get_num_overlay_managers(); ++i) {
  49. mgr = omap_dss_get_overlay_manager(i);
  50. if (sysfs_streq(buf, mgr->name))
  51. break;
  52. mgr = NULL;
  53. }
  54. }
  55. if (len > 0 && mgr == NULL)
  56. return -EINVAL;
  57. if (mgr)
  58. DSSDBG("manager %s found\n", mgr->name);
  59. if (mgr == ovl->manager)
  60. return size;
  61. old_mgr = ovl->manager;
  62. r = dispc_runtime_get();
  63. if (r)
  64. return r;
  65. /* detach old manager */
  66. if (old_mgr) {
  67. r = ovl->unset_manager(ovl);
  68. if (r) {
  69. DSSERR("detach failed\n");
  70. goto err;
  71. }
  72. r = old_mgr->apply(old_mgr);
  73. if (r)
  74. goto err;
  75. }
  76. if (mgr) {
  77. r = ovl->set_manager(ovl, mgr);
  78. if (r) {
  79. DSSERR("Failed to attach overlay\n");
  80. goto err;
  81. }
  82. r = mgr->apply(mgr);
  83. if (r)
  84. goto err;
  85. }
  86. dispc_runtime_put();
  87. return size;
  88. err:
  89. dispc_runtime_put();
  90. return r;
  91. }
  92. static ssize_t overlay_input_size_show(struct omap_overlay *ovl, char *buf)
  93. {
  94. struct omap_overlay_info info;
  95. ovl->get_overlay_info(ovl, &info);
  96. return snprintf(buf, PAGE_SIZE, "%d,%d\n",
  97. info.width, info.height);
  98. }
  99. static ssize_t overlay_screen_width_show(struct omap_overlay *ovl, char *buf)
  100. {
  101. struct omap_overlay_info info;
  102. ovl->get_overlay_info(ovl, &info);
  103. return snprintf(buf, PAGE_SIZE, "%d\n", info.screen_width);
  104. }
  105. static ssize_t overlay_position_show(struct omap_overlay *ovl, char *buf)
  106. {
  107. struct omap_overlay_info info;
  108. ovl->get_overlay_info(ovl, &info);
  109. return snprintf(buf, PAGE_SIZE, "%d,%d\n",
  110. info.pos_x, info.pos_y);
  111. }
  112. static ssize_t overlay_position_store(struct omap_overlay *ovl,
  113. const char *buf, size_t size)
  114. {
  115. int r;
  116. char *last;
  117. struct omap_overlay_info info;
  118. ovl->get_overlay_info(ovl, &info);
  119. info.pos_x = simple_strtoul(buf, &last, 10);
  120. ++last;
  121. if (last - buf >= size)
  122. return -EINVAL;
  123. info.pos_y = simple_strtoul(last, &last, 10);
  124. r = ovl->set_overlay_info(ovl, &info);
  125. if (r)
  126. return r;
  127. if (ovl->manager) {
  128. r = ovl->manager->apply(ovl->manager);
  129. if (r)
  130. return r;
  131. }
  132. return size;
  133. }
  134. static ssize_t overlay_output_size_show(struct omap_overlay *ovl, char *buf)
  135. {
  136. struct omap_overlay_info info;
  137. ovl->get_overlay_info(ovl, &info);
  138. return snprintf(buf, PAGE_SIZE, "%d,%d\n",
  139. info.out_width, info.out_height);
  140. }
  141. static ssize_t overlay_output_size_store(struct omap_overlay *ovl,
  142. const char *buf, size_t size)
  143. {
  144. int r;
  145. char *last;
  146. struct omap_overlay_info info;
  147. ovl->get_overlay_info(ovl, &info);
  148. info.out_width = simple_strtoul(buf, &last, 10);
  149. ++last;
  150. if (last - buf >= size)
  151. return -EINVAL;
  152. info.out_height = simple_strtoul(last, &last, 10);
  153. r = ovl->set_overlay_info(ovl, &info);
  154. if (r)
  155. return r;
  156. if (ovl->manager) {
  157. r = ovl->manager->apply(ovl->manager);
  158. if (r)
  159. return r;
  160. }
  161. return size;
  162. }
  163. static ssize_t overlay_enabled_show(struct omap_overlay *ovl, char *buf)
  164. {
  165. return snprintf(buf, PAGE_SIZE, "%d\n", ovl->is_enabled(ovl));
  166. }
  167. static ssize_t overlay_enabled_store(struct omap_overlay *ovl, const char *buf,
  168. size_t size)
  169. {
  170. int r;
  171. bool enable;
  172. r = strtobool(buf, &enable);
  173. if (r)
  174. return r;
  175. if (enable)
  176. r = ovl->enable(ovl);
  177. else
  178. r = ovl->disable(ovl);
  179. if (r)
  180. return r;
  181. return size;
  182. }
  183. static ssize_t overlay_global_alpha_show(struct omap_overlay *ovl, char *buf)
  184. {
  185. struct omap_overlay_info info;
  186. ovl->get_overlay_info(ovl, &info);
  187. return snprintf(buf, PAGE_SIZE, "%d\n",
  188. info.global_alpha);
  189. }
  190. static ssize_t overlay_global_alpha_store(struct omap_overlay *ovl,
  191. const char *buf, size_t size)
  192. {
  193. int r;
  194. u8 alpha;
  195. struct omap_overlay_info info;
  196. if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
  197. return -ENODEV;
  198. r = kstrtou8(buf, 0, &alpha);
  199. if (r)
  200. return r;
  201. ovl->get_overlay_info(ovl, &info);
  202. info.global_alpha = alpha;
  203. r = ovl->set_overlay_info(ovl, &info);
  204. if (r)
  205. return r;
  206. if (ovl->manager) {
  207. r = ovl->manager->apply(ovl->manager);
  208. if (r)
  209. return r;
  210. }
  211. return size;
  212. }
  213. static ssize_t overlay_pre_mult_alpha_show(struct omap_overlay *ovl,
  214. char *buf)
  215. {
  216. struct omap_overlay_info info;
  217. ovl->get_overlay_info(ovl, &info);
  218. return snprintf(buf, PAGE_SIZE, "%d\n",
  219. info.pre_mult_alpha);
  220. }
  221. static ssize_t overlay_pre_mult_alpha_store(struct omap_overlay *ovl,
  222. const char *buf, size_t size)
  223. {
  224. int r;
  225. u8 alpha;
  226. struct omap_overlay_info info;
  227. if ((ovl->caps & OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA) == 0)
  228. return -ENODEV;
  229. r = kstrtou8(buf, 0, &alpha);
  230. if (r)
  231. return r;
  232. ovl->get_overlay_info(ovl, &info);
  233. info.pre_mult_alpha = alpha;
  234. r = ovl->set_overlay_info(ovl, &info);
  235. if (r)
  236. return r;
  237. if (ovl->manager) {
  238. r = ovl->manager->apply(ovl->manager);
  239. if (r)
  240. return r;
  241. }
  242. return size;
  243. }
  244. static ssize_t overlay_zorder_show(struct omap_overlay *ovl, char *buf)
  245. {
  246. struct omap_overlay_info info;
  247. ovl->get_overlay_info(ovl, &info);
  248. return snprintf(buf, PAGE_SIZE, "%d\n", info.zorder);
  249. }
  250. static ssize_t overlay_zorder_store(struct omap_overlay *ovl,
  251. const char *buf, size_t size)
  252. {
  253. int r;
  254. u8 zorder;
  255. struct omap_overlay_info info;
  256. if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
  257. return -ENODEV;
  258. r = kstrtou8(buf, 0, &zorder);
  259. if (r)
  260. return r;
  261. ovl->get_overlay_info(ovl, &info);
  262. info.zorder = zorder;
  263. r = ovl->set_overlay_info(ovl, &info);
  264. if (r)
  265. return r;
  266. if (ovl->manager) {
  267. r = ovl->manager->apply(ovl->manager);
  268. if (r)
  269. return r;
  270. }
  271. return size;
  272. }
  273. struct overlay_attribute {
  274. struct attribute attr;
  275. ssize_t (*show)(struct omap_overlay *, char *);
  276. ssize_t (*store)(struct omap_overlay *, const char *, size_t);
  277. };
  278. #define OVERLAY_ATTR(_name, _mode, _show, _store) \
  279. struct overlay_attribute overlay_attr_##_name = \
  280. __ATTR(_name, _mode, _show, _store)
  281. static OVERLAY_ATTR(name, S_IRUGO, overlay_name_show, NULL);
  282. static OVERLAY_ATTR(manager, S_IRUGO|S_IWUSR,
  283. overlay_manager_show, overlay_manager_store);
  284. static OVERLAY_ATTR(input_size, S_IRUGO, overlay_input_size_show, NULL);
  285. static OVERLAY_ATTR(screen_width, S_IRUGO, overlay_screen_width_show, NULL);
  286. static OVERLAY_ATTR(position, S_IRUGO|S_IWUSR,
  287. overlay_position_show, overlay_position_store);
  288. static OVERLAY_ATTR(output_size, S_IRUGO|S_IWUSR,
  289. overlay_output_size_show, overlay_output_size_store);
  290. static OVERLAY_ATTR(enabled, S_IRUGO|S_IWUSR,
  291. overlay_enabled_show, overlay_enabled_store);
  292. static OVERLAY_ATTR(global_alpha, S_IRUGO|S_IWUSR,
  293. overlay_global_alpha_show, overlay_global_alpha_store);
  294. static OVERLAY_ATTR(pre_mult_alpha, S_IRUGO|S_IWUSR,
  295. overlay_pre_mult_alpha_show,
  296. overlay_pre_mult_alpha_store);
  297. static OVERLAY_ATTR(zorder, S_IRUGO|S_IWUSR,
  298. overlay_zorder_show, overlay_zorder_store);
  299. static struct attribute *overlay_sysfs_attrs[] = {
  300. &overlay_attr_name.attr,
  301. &overlay_attr_manager.attr,
  302. &overlay_attr_input_size.attr,
  303. &overlay_attr_screen_width.attr,
  304. &overlay_attr_position.attr,
  305. &overlay_attr_output_size.attr,
  306. &overlay_attr_enabled.attr,
  307. &overlay_attr_global_alpha.attr,
  308. &overlay_attr_pre_mult_alpha.attr,
  309. &overlay_attr_zorder.attr,
  310. NULL
  311. };
  312. static ssize_t overlay_attr_show(struct kobject *kobj, struct attribute *attr,
  313. char *buf)
  314. {
  315. struct omap_overlay *overlay;
  316. struct overlay_attribute *overlay_attr;
  317. overlay = container_of(kobj, struct omap_overlay, kobj);
  318. overlay_attr = container_of(attr, struct overlay_attribute, attr);
  319. if (!overlay_attr->show)
  320. return -ENOENT;
  321. return overlay_attr->show(overlay, buf);
  322. }
  323. static ssize_t overlay_attr_store(struct kobject *kobj, struct attribute *attr,
  324. const char *buf, size_t size)
  325. {
  326. struct omap_overlay *overlay;
  327. struct overlay_attribute *overlay_attr;
  328. overlay = container_of(kobj, struct omap_overlay, kobj);
  329. overlay_attr = container_of(attr, struct overlay_attribute, attr);
  330. if (!overlay_attr->store)
  331. return -ENOENT;
  332. return overlay_attr->store(overlay, buf, size);
  333. }
  334. static const struct sysfs_ops overlay_sysfs_ops = {
  335. .show = overlay_attr_show,
  336. .store = overlay_attr_store,
  337. };
  338. static struct kobj_type overlay_ktype = {
  339. .sysfs_ops = &overlay_sysfs_ops,
  340. .default_attrs = overlay_sysfs_attrs,
  341. };
  342. int dss_overlay_kobj_init(struct omap_overlay *ovl,
  343. struct platform_device *pdev)
  344. {
  345. return kobject_init_and_add(&ovl->kobj, &overlay_ktype,
  346. &pdev->dev.kobj, "overlay%d", ovl->id);
  347. }
  348. void dss_overlay_kobj_uninit(struct omap_overlay *ovl)
  349. {
  350. kobject_del(&ovl->kobj);
  351. kobject_put(&ovl->kobj);
  352. }