omapfb-sysfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * linux/drivers/video/omap2/omapfb-sysfs.c
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
  6. *
  7. * Some code and ideas taken from drivers/video/omap/ driver
  8. * by Imre Deak.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published by
  12. * the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <linux/fb.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/device.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/kernel.h>
  28. #include <linux/mm.h>
  29. #include <linux/omapfb.h>
  30. #include <plat/display.h>
  31. #include <plat/vrfb.h>
  32. #include "omapfb.h"
  33. static ssize_t show_rotate_type(struct device *dev,
  34. struct device_attribute *attr, char *buf)
  35. {
  36. struct fb_info *fbi = dev_get_drvdata(dev);
  37. struct omapfb_info *ofbi = FB2OFB(fbi);
  38. return snprintf(buf, PAGE_SIZE, "%d\n", ofbi->rotation_type);
  39. }
  40. static ssize_t store_rotate_type(struct device *dev,
  41. struct device_attribute *attr,
  42. const char *buf, size_t count)
  43. {
  44. struct fb_info *fbi = dev_get_drvdata(dev);
  45. struct omapfb_info *ofbi = FB2OFB(fbi);
  46. enum omap_dss_rotation_type rot_type;
  47. int r;
  48. rot_type = simple_strtoul(buf, NULL, 0);
  49. if (rot_type != OMAP_DSS_ROT_DMA && rot_type != OMAP_DSS_ROT_VRFB)
  50. return -EINVAL;
  51. if (!lock_fb_info(fbi))
  52. return -ENODEV;
  53. r = 0;
  54. if (rot_type == ofbi->rotation_type)
  55. goto out;
  56. if (ofbi->region.size) {
  57. r = -EBUSY;
  58. goto out;
  59. }
  60. ofbi->rotation_type = rot_type;
  61. /*
  62. * Since the VRAM for this FB is not allocated at the moment we don't
  63. * need to do any further parameter checking at this point.
  64. */
  65. out:
  66. unlock_fb_info(fbi);
  67. return r ? r : count;
  68. }
  69. static ssize_t show_mirror(struct device *dev,
  70. struct device_attribute *attr, char *buf)
  71. {
  72. struct fb_info *fbi = dev_get_drvdata(dev);
  73. struct omapfb_info *ofbi = FB2OFB(fbi);
  74. return snprintf(buf, PAGE_SIZE, "%d\n", ofbi->mirror);
  75. }
  76. static ssize_t store_mirror(struct device *dev,
  77. struct device_attribute *attr,
  78. const char *buf, size_t count)
  79. {
  80. struct fb_info *fbi = dev_get_drvdata(dev);
  81. struct omapfb_info *ofbi = FB2OFB(fbi);
  82. bool mirror;
  83. int r;
  84. struct fb_var_screeninfo new_var;
  85. mirror = simple_strtoul(buf, NULL, 0);
  86. if (mirror != 0 && mirror != 1)
  87. return -EINVAL;
  88. if (!lock_fb_info(fbi))
  89. return -ENODEV;
  90. ofbi->mirror = mirror;
  91. memcpy(&new_var, &fbi->var, sizeof(new_var));
  92. r = check_fb_var(fbi, &new_var);
  93. if (r)
  94. goto out;
  95. memcpy(&fbi->var, &new_var, sizeof(fbi->var));
  96. set_fb_fix(fbi);
  97. r = omapfb_apply_changes(fbi, 0);
  98. if (r)
  99. goto out;
  100. r = count;
  101. out:
  102. unlock_fb_info(fbi);
  103. return r;
  104. }
  105. static ssize_t show_overlays(struct device *dev,
  106. struct device_attribute *attr, char *buf)
  107. {
  108. struct fb_info *fbi = dev_get_drvdata(dev);
  109. struct omapfb_info *ofbi = FB2OFB(fbi);
  110. struct omapfb2_device *fbdev = ofbi->fbdev;
  111. ssize_t l = 0;
  112. int t;
  113. if (!lock_fb_info(fbi))
  114. return -ENODEV;
  115. omapfb_lock(fbdev);
  116. for (t = 0; t < ofbi->num_overlays; t++) {
  117. struct omap_overlay *ovl = ofbi->overlays[t];
  118. int ovlnum;
  119. for (ovlnum = 0; ovlnum < fbdev->num_overlays; ++ovlnum)
  120. if (ovl == fbdev->overlays[ovlnum])
  121. break;
  122. l += snprintf(buf + l, PAGE_SIZE - l, "%s%d",
  123. t == 0 ? "" : ",", ovlnum);
  124. }
  125. l += snprintf(buf + l, PAGE_SIZE - l, "\n");
  126. omapfb_unlock(fbdev);
  127. unlock_fb_info(fbi);
  128. return l;
  129. }
  130. static struct omapfb_info *get_overlay_fb(struct omapfb2_device *fbdev,
  131. struct omap_overlay *ovl)
  132. {
  133. int i, t;
  134. for (i = 0; i < fbdev->num_fbs; i++) {
  135. struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
  136. for (t = 0; t < ofbi->num_overlays; t++) {
  137. if (ofbi->overlays[t] == ovl)
  138. return ofbi;
  139. }
  140. }
  141. return NULL;
  142. }
  143. static ssize_t store_overlays(struct device *dev, struct device_attribute *attr,
  144. const char *buf, size_t count)
  145. {
  146. struct fb_info *fbi = dev_get_drvdata(dev);
  147. struct omapfb_info *ofbi = FB2OFB(fbi);
  148. struct omapfb2_device *fbdev = ofbi->fbdev;
  149. struct omap_overlay *ovls[OMAPFB_MAX_OVL_PER_FB];
  150. struct omap_overlay *ovl;
  151. int num_ovls, r, i;
  152. int len;
  153. bool added = false;
  154. num_ovls = 0;
  155. len = strlen(buf);
  156. if (buf[len - 1] == '\n')
  157. len = len - 1;
  158. if (!lock_fb_info(fbi))
  159. return -ENODEV;
  160. omapfb_lock(fbdev);
  161. if (len > 0) {
  162. char *p = (char *)buf;
  163. int ovlnum;
  164. while (p < buf + len) {
  165. int found;
  166. if (num_ovls == OMAPFB_MAX_OVL_PER_FB) {
  167. r = -EINVAL;
  168. goto out;
  169. }
  170. ovlnum = simple_strtoul(p, &p, 0);
  171. if (ovlnum > fbdev->num_overlays) {
  172. r = -EINVAL;
  173. goto out;
  174. }
  175. found = 0;
  176. for (i = 0; i < num_ovls; ++i) {
  177. if (ovls[i] == fbdev->overlays[ovlnum]) {
  178. found = 1;
  179. break;
  180. }
  181. }
  182. if (!found)
  183. ovls[num_ovls++] = fbdev->overlays[ovlnum];
  184. p++;
  185. }
  186. }
  187. for (i = 0; i < num_ovls; ++i) {
  188. struct omapfb_info *ofbi2 = get_overlay_fb(fbdev, ovls[i]);
  189. if (ofbi2 && ofbi2 != ofbi) {
  190. dev_err(fbdev->dev, "overlay already in use\n");
  191. r = -EINVAL;
  192. goto out;
  193. }
  194. }
  195. /* detach unused overlays */
  196. for (i = 0; i < ofbi->num_overlays; ++i) {
  197. int t, found;
  198. ovl = ofbi->overlays[i];
  199. found = 0;
  200. for (t = 0; t < num_ovls; ++t) {
  201. if (ovl == ovls[t]) {
  202. found = 1;
  203. break;
  204. }
  205. }
  206. if (found)
  207. continue;
  208. DBG("detaching %d\n", ofbi->overlays[i]->id);
  209. omapfb_overlay_enable(ovl, 0);
  210. if (ovl->manager)
  211. ovl->manager->apply(ovl->manager);
  212. for (t = i + 1; t < ofbi->num_overlays; t++) {
  213. ofbi->rotation[t-1] = ofbi->rotation[t];
  214. ofbi->overlays[t-1] = ofbi->overlays[t];
  215. }
  216. ofbi->num_overlays--;
  217. i--;
  218. }
  219. for (i = 0; i < num_ovls; ++i) {
  220. int t, found;
  221. ovl = ovls[i];
  222. found = 0;
  223. for (t = 0; t < ofbi->num_overlays; ++t) {
  224. if (ovl == ofbi->overlays[t]) {
  225. found = 1;
  226. break;
  227. }
  228. }
  229. if (found)
  230. continue;
  231. ofbi->rotation[ofbi->num_overlays] = 0;
  232. ofbi->overlays[ofbi->num_overlays++] = ovl;
  233. added = true;
  234. }
  235. if (added) {
  236. r = omapfb_apply_changes(fbi, 0);
  237. if (r)
  238. goto out;
  239. }
  240. r = count;
  241. out:
  242. omapfb_unlock(fbdev);
  243. unlock_fb_info(fbi);
  244. return r;
  245. }
  246. static ssize_t show_overlays_rotate(struct device *dev,
  247. struct device_attribute *attr, char *buf)
  248. {
  249. struct fb_info *fbi = dev_get_drvdata(dev);
  250. struct omapfb_info *ofbi = FB2OFB(fbi);
  251. ssize_t l = 0;
  252. int t;
  253. if (!lock_fb_info(fbi))
  254. return -ENODEV;
  255. for (t = 0; t < ofbi->num_overlays; t++) {
  256. l += snprintf(buf + l, PAGE_SIZE - l, "%s%d",
  257. t == 0 ? "" : ",", ofbi->rotation[t]);
  258. }
  259. l += snprintf(buf + l, PAGE_SIZE - l, "\n");
  260. unlock_fb_info(fbi);
  261. return l;
  262. }
  263. static ssize_t store_overlays_rotate(struct device *dev,
  264. struct device_attribute *attr, const char *buf, size_t count)
  265. {
  266. struct fb_info *fbi = dev_get_drvdata(dev);
  267. struct omapfb_info *ofbi = FB2OFB(fbi);
  268. int num_ovls = 0, r, i;
  269. int len;
  270. bool changed = false;
  271. u8 rotation[OMAPFB_MAX_OVL_PER_FB];
  272. len = strlen(buf);
  273. if (buf[len - 1] == '\n')
  274. len = len - 1;
  275. if (!lock_fb_info(fbi))
  276. return -ENODEV;
  277. if (len > 0) {
  278. char *p = (char *)buf;
  279. while (p < buf + len) {
  280. int rot;
  281. if (num_ovls == ofbi->num_overlays) {
  282. r = -EINVAL;
  283. goto out;
  284. }
  285. rot = simple_strtoul(p, &p, 0);
  286. if (rot < 0 || rot > 3) {
  287. r = -EINVAL;
  288. goto out;
  289. }
  290. if (ofbi->rotation[num_ovls] != rot)
  291. changed = true;
  292. rotation[num_ovls++] = rot;
  293. p++;
  294. }
  295. }
  296. if (num_ovls != ofbi->num_overlays) {
  297. r = -EINVAL;
  298. goto out;
  299. }
  300. if (changed) {
  301. for (i = 0; i < num_ovls; ++i)
  302. ofbi->rotation[i] = rotation[i];
  303. r = omapfb_apply_changes(fbi, 0);
  304. if (r)
  305. goto out;
  306. /* FIXME error handling? */
  307. }
  308. r = count;
  309. out:
  310. unlock_fb_info(fbi);
  311. return r;
  312. }
  313. static ssize_t show_size(struct device *dev,
  314. struct device_attribute *attr, char *buf)
  315. {
  316. struct fb_info *fbi = dev_get_drvdata(dev);
  317. struct omapfb_info *ofbi = FB2OFB(fbi);
  318. return snprintf(buf, PAGE_SIZE, "%lu\n", ofbi->region.size);
  319. }
  320. static ssize_t store_size(struct device *dev, struct device_attribute *attr,
  321. const char *buf, size_t count)
  322. {
  323. struct fb_info *fbi = dev_get_drvdata(dev);
  324. struct omapfb_info *ofbi = FB2OFB(fbi);
  325. unsigned long size;
  326. int r;
  327. int i;
  328. size = PAGE_ALIGN(simple_strtoul(buf, NULL, 0));
  329. if (!lock_fb_info(fbi))
  330. return -ENODEV;
  331. for (i = 0; i < ofbi->num_overlays; i++) {
  332. if (ofbi->overlays[i]->info.enabled) {
  333. r = -EBUSY;
  334. goto out;
  335. }
  336. }
  337. if (size != ofbi->region.size) {
  338. r = omapfb_realloc_fbmem(fbi, size, ofbi->region.type);
  339. if (r) {
  340. dev_err(dev, "realloc fbmem failed\n");
  341. goto out;
  342. }
  343. }
  344. r = count;
  345. out:
  346. unlock_fb_info(fbi);
  347. return r;
  348. }
  349. static ssize_t show_phys(struct device *dev,
  350. struct device_attribute *attr, char *buf)
  351. {
  352. struct fb_info *fbi = dev_get_drvdata(dev);
  353. struct omapfb_info *ofbi = FB2OFB(fbi);
  354. return snprintf(buf, PAGE_SIZE, "%0x\n", ofbi->region.paddr);
  355. }
  356. static ssize_t show_virt(struct device *dev,
  357. struct device_attribute *attr, char *buf)
  358. {
  359. struct fb_info *fbi = dev_get_drvdata(dev);
  360. struct omapfb_info *ofbi = FB2OFB(fbi);
  361. return snprintf(buf, PAGE_SIZE, "%p\n", ofbi->region.vaddr);
  362. }
  363. static struct device_attribute omapfb_attrs[] = {
  364. __ATTR(rotate_type, S_IRUGO | S_IWUSR, show_rotate_type,
  365. store_rotate_type),
  366. __ATTR(mirror, S_IRUGO | S_IWUSR, show_mirror, store_mirror),
  367. __ATTR(size, S_IRUGO | S_IWUSR, show_size, store_size),
  368. __ATTR(overlays, S_IRUGO | S_IWUSR, show_overlays, store_overlays),
  369. __ATTR(overlays_rotate, S_IRUGO | S_IWUSR, show_overlays_rotate,
  370. store_overlays_rotate),
  371. __ATTR(phys_addr, S_IRUGO, show_phys, NULL),
  372. __ATTR(virt_addr, S_IRUGO, show_virt, NULL),
  373. };
  374. int omapfb_create_sysfs(struct omapfb2_device *fbdev)
  375. {
  376. int i;
  377. int r;
  378. DBG("create sysfs for fbs\n");
  379. for (i = 0; i < fbdev->num_fbs; i++) {
  380. int t;
  381. for (t = 0; t < ARRAY_SIZE(omapfb_attrs); t++) {
  382. r = device_create_file(fbdev->fbs[i]->dev,
  383. &omapfb_attrs[t]);
  384. if (r) {
  385. dev_err(fbdev->dev, "failed to create sysfs "
  386. "file\n");
  387. return r;
  388. }
  389. }
  390. }
  391. return 0;
  392. }
  393. void omapfb_remove_sysfs(struct omapfb2_device *fbdev)
  394. {
  395. int i, t;
  396. DBG("remove sysfs for fbs\n");
  397. for (i = 0; i < fbdev->num_fbs; i++) {
  398. for (t = 0; t < ARRAY_SIZE(omapfb_attrs); t++)
  399. device_remove_file(fbdev->fbs[i]->dev,
  400. &omapfb_attrs[t]);
  401. }
  402. }