omapfb-sysfs.c 11 KB

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