omapfb-sysfs.c 12 KB

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