omapfb-sysfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. lock_fb_info(fbi);
  52. r = 0;
  53. if (rot_type == ofbi->rotation_type)
  54. goto out;
  55. if (ofbi->region.size) {
  56. r = -EBUSY;
  57. goto out;
  58. }
  59. ofbi->rotation_type = rot_type;
  60. /*
  61. * Since the VRAM for this FB is not allocated at the moment we don't
  62. * need to do any further parameter checking at this point.
  63. */
  64. out:
  65. unlock_fb_info(fbi);
  66. return r ? r : count;
  67. }
  68. static ssize_t show_mirror(struct device *dev,
  69. struct device_attribute *attr, char *buf)
  70. {
  71. struct fb_info *fbi = dev_get_drvdata(dev);
  72. struct omapfb_info *ofbi = FB2OFB(fbi);
  73. return snprintf(buf, PAGE_SIZE, "%d\n", ofbi->mirror);
  74. }
  75. static ssize_t store_mirror(struct device *dev,
  76. struct device_attribute *attr,
  77. const char *buf, size_t count)
  78. {
  79. struct fb_info *fbi = dev_get_drvdata(dev);
  80. struct omapfb_info *ofbi = FB2OFB(fbi);
  81. bool mirror;
  82. int r;
  83. struct fb_var_screeninfo new_var;
  84. mirror = simple_strtoul(buf, NULL, 0);
  85. if (mirror != 0 && mirror != 1)
  86. return -EINVAL;
  87. lock_fb_info(fbi);
  88. ofbi->mirror = mirror;
  89. memcpy(&new_var, &fbi->var, sizeof(new_var));
  90. r = check_fb_var(fbi, &new_var);
  91. if (r)
  92. goto out;
  93. memcpy(&fbi->var, &new_var, sizeof(fbi->var));
  94. set_fb_fix(fbi);
  95. r = omapfb_apply_changes(fbi, 0);
  96. if (r)
  97. goto out;
  98. r = count;
  99. out:
  100. unlock_fb_info(fbi);
  101. return r;
  102. }
  103. static ssize_t show_overlays(struct device *dev,
  104. struct device_attribute *attr, char *buf)
  105. {
  106. struct fb_info *fbi = dev_get_drvdata(dev);
  107. struct omapfb_info *ofbi = FB2OFB(fbi);
  108. struct omapfb2_device *fbdev = ofbi->fbdev;
  109. ssize_t l = 0;
  110. int t;
  111. omapfb_lock(fbdev);
  112. lock_fb_info(fbi);
  113. for (t = 0; t < ofbi->num_overlays; t++) {
  114. struct omap_overlay *ovl = ofbi->overlays[t];
  115. int ovlnum;
  116. for (ovlnum = 0; ovlnum < fbdev->num_overlays; ++ovlnum)
  117. if (ovl == fbdev->overlays[ovlnum])
  118. break;
  119. l += snprintf(buf + l, PAGE_SIZE - l, "%s%d",
  120. t == 0 ? "" : ",", ovlnum);
  121. }
  122. l += snprintf(buf + l, PAGE_SIZE - l, "\n");
  123. unlock_fb_info(fbi);
  124. omapfb_unlock(fbdev);
  125. return l;
  126. }
  127. static struct omapfb_info *get_overlay_fb(struct omapfb2_device *fbdev,
  128. struct omap_overlay *ovl)
  129. {
  130. int i, t;
  131. for (i = 0; i < fbdev->num_fbs; i++) {
  132. struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
  133. for (t = 0; t < ofbi->num_overlays; t++) {
  134. if (ofbi->overlays[t] == ovl)
  135. return ofbi;
  136. }
  137. }
  138. return NULL;
  139. }
  140. static ssize_t store_overlays(struct device *dev, struct device_attribute *attr,
  141. const char *buf, size_t count)
  142. {
  143. struct fb_info *fbi = dev_get_drvdata(dev);
  144. struct omapfb_info *ofbi = FB2OFB(fbi);
  145. struct omapfb2_device *fbdev = ofbi->fbdev;
  146. struct omap_overlay *ovls[OMAPFB_MAX_OVL_PER_FB];
  147. struct omap_overlay *ovl;
  148. int num_ovls, r, i;
  149. int len;
  150. bool added = false;
  151. num_ovls = 0;
  152. len = strlen(buf);
  153. if (buf[len - 1] == '\n')
  154. len = len - 1;
  155. omapfb_lock(fbdev);
  156. lock_fb_info(fbi);
  157. if (len > 0) {
  158. char *p = (char *)buf;
  159. int ovlnum;
  160. while (p < buf + len) {
  161. int found;
  162. if (num_ovls == OMAPFB_MAX_OVL_PER_FB) {
  163. r = -EINVAL;
  164. goto out;
  165. }
  166. ovlnum = simple_strtoul(p, &p, 0);
  167. if (ovlnum > fbdev->num_overlays) {
  168. r = -EINVAL;
  169. goto out;
  170. }
  171. found = 0;
  172. for (i = 0; i < num_ovls; ++i) {
  173. if (ovls[i] == fbdev->overlays[ovlnum]) {
  174. found = 1;
  175. break;
  176. }
  177. }
  178. if (!found)
  179. ovls[num_ovls++] = fbdev->overlays[ovlnum];
  180. p++;
  181. }
  182. }
  183. for (i = 0; i < num_ovls; ++i) {
  184. struct omapfb_info *ofbi2 = get_overlay_fb(fbdev, ovls[i]);
  185. if (ofbi2 && ofbi2 != ofbi) {
  186. dev_err(fbdev->dev, "overlay already in use\n");
  187. r = -EINVAL;
  188. goto out;
  189. }
  190. }
  191. /* detach unused overlays */
  192. for (i = 0; i < ofbi->num_overlays; ++i) {
  193. int t, found;
  194. ovl = ofbi->overlays[i];
  195. found = 0;
  196. for (t = 0; t < num_ovls; ++t) {
  197. if (ovl == ovls[t]) {
  198. found = 1;
  199. break;
  200. }
  201. }
  202. if (found)
  203. continue;
  204. DBG("detaching %d\n", ofbi->overlays[i]->id);
  205. omapfb_overlay_enable(ovl, 0);
  206. if (ovl->manager)
  207. ovl->manager->apply(ovl->manager);
  208. for (t = i + 1; t < ofbi->num_overlays; t++) {
  209. ofbi->rotation[t-1] = ofbi->rotation[t];
  210. ofbi->overlays[t-1] = ofbi->overlays[t];
  211. }
  212. ofbi->num_overlays--;
  213. i--;
  214. }
  215. for (i = 0; i < num_ovls; ++i) {
  216. int t, found;
  217. ovl = ovls[i];
  218. found = 0;
  219. for (t = 0; t < ofbi->num_overlays; ++t) {
  220. if (ovl == ofbi->overlays[t]) {
  221. found = 1;
  222. break;
  223. }
  224. }
  225. if (found)
  226. continue;
  227. ofbi->rotation[ofbi->num_overlays] = 0;
  228. ofbi->overlays[ofbi->num_overlays++] = ovl;
  229. added = true;
  230. }
  231. if (added) {
  232. r = omapfb_apply_changes(fbi, 0);
  233. if (r)
  234. goto out;
  235. }
  236. r = count;
  237. out:
  238. unlock_fb_info(fbi);
  239. omapfb_unlock(fbdev);
  240. return r;
  241. }
  242. static ssize_t show_overlays_rotate(struct device *dev,
  243. struct device_attribute *attr, char *buf)
  244. {
  245. struct fb_info *fbi = dev_get_drvdata(dev);
  246. struct omapfb_info *ofbi = FB2OFB(fbi);
  247. ssize_t l = 0;
  248. int t;
  249. lock_fb_info(fbi);
  250. for (t = 0; t < ofbi->num_overlays; t++) {
  251. l += snprintf(buf + l, PAGE_SIZE - l, "%s%d",
  252. t == 0 ? "" : ",", ofbi->rotation[t]);
  253. }
  254. l += snprintf(buf + l, PAGE_SIZE - l, "\n");
  255. unlock_fb_info(fbi);
  256. return l;
  257. }
  258. static ssize_t store_overlays_rotate(struct device *dev,
  259. struct device_attribute *attr, const char *buf, size_t count)
  260. {
  261. struct fb_info *fbi = dev_get_drvdata(dev);
  262. struct omapfb_info *ofbi = FB2OFB(fbi);
  263. int num_ovls = 0, r, i;
  264. int len;
  265. bool changed = false;
  266. u8 rotation[OMAPFB_MAX_OVL_PER_FB];
  267. len = strlen(buf);
  268. if (buf[len - 1] == '\n')
  269. len = len - 1;
  270. lock_fb_info(fbi);
  271. if (len > 0) {
  272. char *p = (char *)buf;
  273. while (p < buf + len) {
  274. int rot;
  275. if (num_ovls == ofbi->num_overlays) {
  276. r = -EINVAL;
  277. goto out;
  278. }
  279. rot = simple_strtoul(p, &p, 0);
  280. if (rot < 0 || rot > 3) {
  281. r = -EINVAL;
  282. goto out;
  283. }
  284. if (ofbi->rotation[num_ovls] != rot)
  285. changed = true;
  286. rotation[num_ovls++] = rot;
  287. p++;
  288. }
  289. }
  290. if (num_ovls != ofbi->num_overlays) {
  291. r = -EINVAL;
  292. goto out;
  293. }
  294. if (changed) {
  295. for (i = 0; i < num_ovls; ++i)
  296. ofbi->rotation[i] = rotation[i];
  297. r = omapfb_apply_changes(fbi, 0);
  298. if (r)
  299. goto out;
  300. /* FIXME error handling? */
  301. }
  302. r = count;
  303. out:
  304. unlock_fb_info(fbi);
  305. return r;
  306. }
  307. static ssize_t show_size(struct device *dev,
  308. struct device_attribute *attr, char *buf)
  309. {
  310. struct fb_info *fbi = dev_get_drvdata(dev);
  311. struct omapfb_info *ofbi = FB2OFB(fbi);
  312. return snprintf(buf, PAGE_SIZE, "%lu\n", ofbi->region.size);
  313. }
  314. static ssize_t store_size(struct device *dev, struct device_attribute *attr,
  315. const char *buf, size_t count)
  316. {
  317. struct fb_info *fbi = dev_get_drvdata(dev);
  318. struct omapfb_info *ofbi = FB2OFB(fbi);
  319. unsigned long size;
  320. int r;
  321. int i;
  322. size = PAGE_ALIGN(simple_strtoul(buf, NULL, 0));
  323. lock_fb_info(fbi);
  324. for (i = 0; i < ofbi->num_overlays; i++) {
  325. if (ofbi->overlays[i]->info.enabled) {
  326. r = -EBUSY;
  327. goto out;
  328. }
  329. }
  330. if (size != ofbi->region.size) {
  331. r = omapfb_realloc_fbmem(fbi, size, ofbi->region.type);
  332. if (r) {
  333. dev_err(dev, "realloc fbmem failed\n");
  334. goto out;
  335. }
  336. }
  337. r = count;
  338. out:
  339. unlock_fb_info(fbi);
  340. return r;
  341. }
  342. static ssize_t show_phys(struct device *dev,
  343. struct device_attribute *attr, char *buf)
  344. {
  345. struct fb_info *fbi = dev_get_drvdata(dev);
  346. struct omapfb_info *ofbi = FB2OFB(fbi);
  347. return snprintf(buf, PAGE_SIZE, "%0x\n", ofbi->region.paddr);
  348. }
  349. static ssize_t show_virt(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, "%p\n", ofbi->region.vaddr);
  355. }
  356. static struct device_attribute omapfb_attrs[] = {
  357. __ATTR(rotate_type, S_IRUGO | S_IWUSR, show_rotate_type,
  358. store_rotate_type),
  359. __ATTR(mirror, S_IRUGO | S_IWUSR, show_mirror, store_mirror),
  360. __ATTR(size, S_IRUGO | S_IWUSR, show_size, store_size),
  361. __ATTR(overlays, S_IRUGO | S_IWUSR, show_overlays, store_overlays),
  362. __ATTR(overlays_rotate, S_IRUGO | S_IWUSR, show_overlays_rotate,
  363. store_overlays_rotate),
  364. __ATTR(phys_addr, S_IRUGO, show_phys, NULL),
  365. __ATTR(virt_addr, S_IRUGO, show_virt, NULL),
  366. };
  367. int omapfb_create_sysfs(struct omapfb2_device *fbdev)
  368. {
  369. int i;
  370. int r;
  371. DBG("create sysfs for fbs\n");
  372. for (i = 0; i < fbdev->num_fbs; i++) {
  373. int t;
  374. for (t = 0; t < ARRAY_SIZE(omapfb_attrs); t++) {
  375. r = device_create_file(fbdev->fbs[i]->dev,
  376. &omapfb_attrs[t]);
  377. if (r) {
  378. dev_err(fbdev->dev, "failed to create sysfs "
  379. "file\n");
  380. return r;
  381. }
  382. }
  383. }
  384. return 0;
  385. }
  386. void omapfb_remove_sysfs(struct omapfb2_device *fbdev)
  387. {
  388. int i, t;
  389. DBG("remove sysfs for fbs\n");
  390. for (i = 0; i < fbdev->num_fbs; i++) {
  391. for (t = 0; t < ARRAY_SIZE(omapfb_attrs); t++)
  392. device_remove_file(fbdev->fbs[i]->dev,
  393. &omapfb_attrs[t]);
  394. }
  395. }