omapfb-sysfs.c 12 KB

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