display.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. * linux/drivers/video/omap2/dss/display.c
  3. *
  4. * Copyright (C) 2009 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. #define DSS_SUBSYS_NAME "DISPLAY"
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/platform_device.h>
  27. #include <video/omapdss.h>
  28. #include "dss.h"
  29. static ssize_t display_enabled_show(struct device *dev,
  30. struct device_attribute *attr, char *buf)
  31. {
  32. struct omap_dss_device *dssdev = to_dss_device(dev);
  33. bool enabled = dssdev->state != OMAP_DSS_DISPLAY_DISABLED;
  34. return snprintf(buf, PAGE_SIZE, "%d\n", enabled);
  35. }
  36. static ssize_t display_enabled_store(struct device *dev,
  37. struct device_attribute *attr,
  38. const char *buf, size_t size)
  39. {
  40. struct omap_dss_device *dssdev = to_dss_device(dev);
  41. int r, enabled;
  42. r = kstrtoint(buf, 0, &enabled);
  43. if (r)
  44. return r;
  45. enabled = !!enabled;
  46. if (enabled != (dssdev->state != OMAP_DSS_DISPLAY_DISABLED)) {
  47. if (enabled) {
  48. r = dssdev->driver->enable(dssdev);
  49. if (r)
  50. return r;
  51. } else {
  52. dssdev->driver->disable(dssdev);
  53. }
  54. }
  55. return size;
  56. }
  57. static ssize_t display_upd_mode_show(struct device *dev,
  58. struct device_attribute *attr, char *buf)
  59. {
  60. struct omap_dss_device *dssdev = to_dss_device(dev);
  61. enum omap_dss_update_mode mode = OMAP_DSS_UPDATE_AUTO;
  62. if (dssdev->driver->get_update_mode)
  63. mode = dssdev->driver->get_update_mode(dssdev);
  64. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  65. }
  66. static ssize_t display_upd_mode_store(struct device *dev,
  67. struct device_attribute *attr,
  68. const char *buf, size_t size)
  69. {
  70. struct omap_dss_device *dssdev = to_dss_device(dev);
  71. int val, r;
  72. enum omap_dss_update_mode mode;
  73. if (!dssdev->driver->set_update_mode)
  74. return -EINVAL;
  75. r = kstrtoint(buf, 0, &val);
  76. if (r)
  77. return r;
  78. switch (val) {
  79. case OMAP_DSS_UPDATE_DISABLED:
  80. case OMAP_DSS_UPDATE_AUTO:
  81. case OMAP_DSS_UPDATE_MANUAL:
  82. mode = (enum omap_dss_update_mode)val;
  83. break;
  84. default:
  85. return -EINVAL;
  86. }
  87. r = dssdev->driver->set_update_mode(dssdev, mode);
  88. if (r)
  89. return r;
  90. return size;
  91. }
  92. static ssize_t display_tear_show(struct device *dev,
  93. struct device_attribute *attr, char *buf)
  94. {
  95. struct omap_dss_device *dssdev = to_dss_device(dev);
  96. return snprintf(buf, PAGE_SIZE, "%d\n",
  97. dssdev->driver->get_te ?
  98. dssdev->driver->get_te(dssdev) : 0);
  99. }
  100. static ssize_t display_tear_store(struct device *dev,
  101. struct device_attribute *attr, const char *buf, size_t size)
  102. {
  103. struct omap_dss_device *dssdev = to_dss_device(dev);
  104. int te, r;
  105. if (!dssdev->driver->enable_te || !dssdev->driver->get_te)
  106. return -ENOENT;
  107. r = kstrtoint(buf, 0, &te);
  108. if (r)
  109. return r;
  110. te = !!te;
  111. r = dssdev->driver->enable_te(dssdev, te);
  112. if (r)
  113. return r;
  114. return size;
  115. }
  116. static ssize_t display_timings_show(struct device *dev,
  117. struct device_attribute *attr, char *buf)
  118. {
  119. struct omap_dss_device *dssdev = to_dss_device(dev);
  120. struct omap_video_timings t;
  121. if (!dssdev->driver->get_timings)
  122. return -ENOENT;
  123. dssdev->driver->get_timings(dssdev, &t);
  124. return snprintf(buf, PAGE_SIZE, "%u,%u/%u/%u/%u,%u/%u/%u/%u\n",
  125. t.pixel_clock,
  126. t.x_res, t.hfp, t.hbp, t.hsw,
  127. t.y_res, t.vfp, t.vbp, t.vsw);
  128. }
  129. static ssize_t display_timings_store(struct device *dev,
  130. struct device_attribute *attr, const char *buf, size_t size)
  131. {
  132. struct omap_dss_device *dssdev = to_dss_device(dev);
  133. struct omap_video_timings t;
  134. int r, found;
  135. if (!dssdev->driver->set_timings || !dssdev->driver->check_timings)
  136. return -ENOENT;
  137. found = 0;
  138. #ifdef CONFIG_OMAP2_DSS_VENC
  139. if (strncmp("pal", buf, 3) == 0) {
  140. t = omap_dss_pal_timings;
  141. found = 1;
  142. } else if (strncmp("ntsc", buf, 4) == 0) {
  143. t = omap_dss_ntsc_timings;
  144. found = 1;
  145. }
  146. #endif
  147. if (!found && sscanf(buf, "%u,%hu/%hu/%hu/%hu,%hu/%hu/%hu/%hu",
  148. &t.pixel_clock,
  149. &t.x_res, &t.hfp, &t.hbp, &t.hsw,
  150. &t.y_res, &t.vfp, &t.vbp, &t.vsw) != 9)
  151. return -EINVAL;
  152. r = dssdev->driver->check_timings(dssdev, &t);
  153. if (r)
  154. return r;
  155. dssdev->driver->set_timings(dssdev, &t);
  156. return size;
  157. }
  158. static ssize_t display_rotate_show(struct device *dev,
  159. struct device_attribute *attr, char *buf)
  160. {
  161. struct omap_dss_device *dssdev = to_dss_device(dev);
  162. int rotate;
  163. if (!dssdev->driver->get_rotate)
  164. return -ENOENT;
  165. rotate = dssdev->driver->get_rotate(dssdev);
  166. return snprintf(buf, PAGE_SIZE, "%u\n", rotate);
  167. }
  168. static ssize_t display_rotate_store(struct device *dev,
  169. struct device_attribute *attr, const char *buf, size_t size)
  170. {
  171. struct omap_dss_device *dssdev = to_dss_device(dev);
  172. int rot, r;
  173. if (!dssdev->driver->set_rotate || !dssdev->driver->get_rotate)
  174. return -ENOENT;
  175. r = kstrtoint(buf, 0, &rot);
  176. if (r)
  177. return r;
  178. r = dssdev->driver->set_rotate(dssdev, rot);
  179. if (r)
  180. return r;
  181. return size;
  182. }
  183. static ssize_t display_mirror_show(struct device *dev,
  184. struct device_attribute *attr, char *buf)
  185. {
  186. struct omap_dss_device *dssdev = to_dss_device(dev);
  187. int mirror;
  188. if (!dssdev->driver->get_mirror)
  189. return -ENOENT;
  190. mirror = dssdev->driver->get_mirror(dssdev);
  191. return snprintf(buf, PAGE_SIZE, "%u\n", mirror);
  192. }
  193. static ssize_t display_mirror_store(struct device *dev,
  194. struct device_attribute *attr, const char *buf, size_t size)
  195. {
  196. struct omap_dss_device *dssdev = to_dss_device(dev);
  197. int mirror, r;
  198. if (!dssdev->driver->set_mirror || !dssdev->driver->get_mirror)
  199. return -ENOENT;
  200. r = kstrtoint(buf, 0, &mirror);
  201. if (r)
  202. return r;
  203. mirror = !!mirror;
  204. r = dssdev->driver->set_mirror(dssdev, mirror);
  205. if (r)
  206. return r;
  207. return size;
  208. }
  209. static ssize_t display_wss_show(struct device *dev,
  210. struct device_attribute *attr, char *buf)
  211. {
  212. struct omap_dss_device *dssdev = to_dss_device(dev);
  213. unsigned int wss;
  214. if (!dssdev->driver->get_wss)
  215. return -ENOENT;
  216. wss = dssdev->driver->get_wss(dssdev);
  217. return snprintf(buf, PAGE_SIZE, "0x%05x\n", wss);
  218. }
  219. static ssize_t display_wss_store(struct device *dev,
  220. struct device_attribute *attr, const char *buf, size_t size)
  221. {
  222. struct omap_dss_device *dssdev = to_dss_device(dev);
  223. u32 wss;
  224. int r;
  225. if (!dssdev->driver->get_wss || !dssdev->driver->set_wss)
  226. return -ENOENT;
  227. r = kstrtou32(buf, 0, &wss);
  228. if (r)
  229. return r;
  230. if (wss > 0xfffff)
  231. return -EINVAL;
  232. r = dssdev->driver->set_wss(dssdev, wss);
  233. if (r)
  234. return r;
  235. return size;
  236. }
  237. static DEVICE_ATTR(enabled, S_IRUGO|S_IWUSR,
  238. display_enabled_show, display_enabled_store);
  239. static DEVICE_ATTR(update_mode, S_IRUGO|S_IWUSR,
  240. display_upd_mode_show, display_upd_mode_store);
  241. static DEVICE_ATTR(tear_elim, S_IRUGO|S_IWUSR,
  242. display_tear_show, display_tear_store);
  243. static DEVICE_ATTR(timings, S_IRUGO|S_IWUSR,
  244. display_timings_show, display_timings_store);
  245. static DEVICE_ATTR(rotate, S_IRUGO|S_IWUSR,
  246. display_rotate_show, display_rotate_store);
  247. static DEVICE_ATTR(mirror, S_IRUGO|S_IWUSR,
  248. display_mirror_show, display_mirror_store);
  249. static DEVICE_ATTR(wss, S_IRUGO|S_IWUSR,
  250. display_wss_show, display_wss_store);
  251. static struct device_attribute *display_sysfs_attrs[] = {
  252. &dev_attr_enabled,
  253. &dev_attr_update_mode,
  254. &dev_attr_tear_elim,
  255. &dev_attr_timings,
  256. &dev_attr_rotate,
  257. &dev_attr_mirror,
  258. &dev_attr_wss,
  259. NULL
  260. };
  261. void omapdss_default_get_resolution(struct omap_dss_device *dssdev,
  262. u16 *xres, u16 *yres)
  263. {
  264. *xres = dssdev->panel.timings.x_res;
  265. *yres = dssdev->panel.timings.y_res;
  266. }
  267. EXPORT_SYMBOL(omapdss_default_get_resolution);
  268. void default_get_overlay_fifo_thresholds(enum omap_plane plane,
  269. u32 fifo_size, enum omap_burst_size *burst_size,
  270. u32 *fifo_low, u32 *fifo_high)
  271. {
  272. unsigned burst_size_bytes;
  273. *burst_size = OMAP_DSS_BURST_16x32;
  274. burst_size_bytes = 16 * 32 / 8;
  275. *fifo_high = fifo_size - 1;
  276. *fifo_low = fifo_size - burst_size_bytes;
  277. }
  278. int omapdss_default_get_recommended_bpp(struct omap_dss_device *dssdev)
  279. {
  280. switch (dssdev->type) {
  281. case OMAP_DISPLAY_TYPE_DPI:
  282. if (dssdev->phy.dpi.data_lines == 24)
  283. return 24;
  284. else
  285. return 16;
  286. case OMAP_DISPLAY_TYPE_DBI:
  287. case OMAP_DISPLAY_TYPE_DSI:
  288. if (dssdev->ctrl.pixel_size == 24)
  289. return 24;
  290. else
  291. return 16;
  292. case OMAP_DISPLAY_TYPE_VENC:
  293. case OMAP_DISPLAY_TYPE_SDI:
  294. case OMAP_DISPLAY_TYPE_HDMI:
  295. return 24;
  296. default:
  297. BUG();
  298. }
  299. }
  300. EXPORT_SYMBOL(omapdss_default_get_recommended_bpp);
  301. /* Checks if replication logic should be used. Only use for active matrix,
  302. * when overlay is in RGB12U or RGB16 mode, and LCD interface is
  303. * 18bpp or 24bpp */
  304. bool dss_use_replication(struct omap_dss_device *dssdev,
  305. enum omap_color_mode mode)
  306. {
  307. int bpp;
  308. if (mode != OMAP_DSS_COLOR_RGB12U && mode != OMAP_DSS_COLOR_RGB16)
  309. return false;
  310. if (dssdev->type == OMAP_DISPLAY_TYPE_DPI &&
  311. (dssdev->panel.config & OMAP_DSS_LCD_TFT) == 0)
  312. return false;
  313. switch (dssdev->type) {
  314. case OMAP_DISPLAY_TYPE_DPI:
  315. bpp = dssdev->phy.dpi.data_lines;
  316. break;
  317. case OMAP_DISPLAY_TYPE_HDMI:
  318. case OMAP_DISPLAY_TYPE_VENC:
  319. case OMAP_DISPLAY_TYPE_SDI:
  320. bpp = 24;
  321. break;
  322. case OMAP_DISPLAY_TYPE_DBI:
  323. case OMAP_DISPLAY_TYPE_DSI:
  324. bpp = dssdev->ctrl.pixel_size;
  325. break;
  326. default:
  327. BUG();
  328. }
  329. return bpp > 16;
  330. }
  331. void dss_init_device(struct platform_device *pdev,
  332. struct omap_dss_device *dssdev)
  333. {
  334. struct device_attribute *attr;
  335. int i;
  336. int r;
  337. switch (dssdev->type) {
  338. #ifdef CONFIG_OMAP2_DSS_DPI
  339. case OMAP_DISPLAY_TYPE_DPI:
  340. r = dpi_init_display(dssdev);
  341. break;
  342. #endif
  343. #ifdef CONFIG_OMAP2_DSS_RFBI
  344. case OMAP_DISPLAY_TYPE_DBI:
  345. r = rfbi_init_display(dssdev);
  346. break;
  347. #endif
  348. #ifdef CONFIG_OMAP2_DSS_VENC
  349. case OMAP_DISPLAY_TYPE_VENC:
  350. r = venc_init_display(dssdev);
  351. break;
  352. #endif
  353. #ifdef CONFIG_OMAP2_DSS_SDI
  354. case OMAP_DISPLAY_TYPE_SDI:
  355. r = sdi_init_display(dssdev);
  356. break;
  357. #endif
  358. #ifdef CONFIG_OMAP2_DSS_DSI
  359. case OMAP_DISPLAY_TYPE_DSI:
  360. r = dsi_init_display(dssdev);
  361. break;
  362. #endif
  363. case OMAP_DISPLAY_TYPE_HDMI:
  364. r = hdmi_init_display(dssdev);
  365. break;
  366. default:
  367. DSSERR("Support for display '%s' not compiled in.\n",
  368. dssdev->name);
  369. return;
  370. }
  371. if (r) {
  372. DSSERR("failed to init display %s\n", dssdev->name);
  373. return;
  374. }
  375. /* create device sysfs files */
  376. i = 0;
  377. while ((attr = display_sysfs_attrs[i++]) != NULL) {
  378. r = device_create_file(&dssdev->dev, attr);
  379. if (r)
  380. DSSERR("failed to create sysfs file\n");
  381. }
  382. /* create display? sysfs links */
  383. r = sysfs_create_link(&pdev->dev.kobj, &dssdev->dev.kobj,
  384. dev_name(&dssdev->dev));
  385. if (r)
  386. DSSERR("failed to create sysfs display link\n");
  387. }
  388. void dss_uninit_device(struct platform_device *pdev,
  389. struct omap_dss_device *dssdev)
  390. {
  391. struct device_attribute *attr;
  392. int i = 0;
  393. sysfs_remove_link(&pdev->dev.kobj, dev_name(&dssdev->dev));
  394. while ((attr = display_sysfs_attrs[i++]) != NULL)
  395. device_remove_file(&dssdev->dev, attr);
  396. if (dssdev->manager)
  397. dssdev->manager->unset_device(dssdev->manager);
  398. }
  399. static int dss_suspend_device(struct device *dev, void *data)
  400. {
  401. int r;
  402. struct omap_dss_device *dssdev = to_dss_device(dev);
  403. if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE) {
  404. dssdev->activate_after_resume = false;
  405. return 0;
  406. }
  407. if (!dssdev->driver->suspend) {
  408. DSSERR("display '%s' doesn't implement suspend\n",
  409. dssdev->name);
  410. return -ENOSYS;
  411. }
  412. r = dssdev->driver->suspend(dssdev);
  413. if (r)
  414. return r;
  415. dssdev->activate_after_resume = true;
  416. return 0;
  417. }
  418. int dss_suspend_all_devices(void)
  419. {
  420. int r;
  421. struct bus_type *bus = dss_get_bus();
  422. r = bus_for_each_dev(bus, NULL, NULL, dss_suspend_device);
  423. if (r) {
  424. /* resume all displays that were suspended */
  425. dss_resume_all_devices();
  426. return r;
  427. }
  428. return 0;
  429. }
  430. static int dss_resume_device(struct device *dev, void *data)
  431. {
  432. int r;
  433. struct omap_dss_device *dssdev = to_dss_device(dev);
  434. if (dssdev->activate_after_resume && dssdev->driver->resume) {
  435. r = dssdev->driver->resume(dssdev);
  436. if (r)
  437. return r;
  438. }
  439. dssdev->activate_after_resume = false;
  440. return 0;
  441. }
  442. int dss_resume_all_devices(void)
  443. {
  444. struct bus_type *bus = dss_get_bus();
  445. return bus_for_each_dev(bus, NULL, NULL, dss_resume_device);
  446. }
  447. static int dss_disable_device(struct device *dev, void *data)
  448. {
  449. struct omap_dss_device *dssdev = to_dss_device(dev);
  450. if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED)
  451. dssdev->driver->disable(dssdev);
  452. return 0;
  453. }
  454. void dss_disable_all_devices(void)
  455. {
  456. struct bus_type *bus = dss_get_bus();
  457. bus_for_each_dev(bus, NULL, NULL, dss_disable_device);
  458. }
  459. void omap_dss_get_device(struct omap_dss_device *dssdev)
  460. {
  461. get_device(&dssdev->dev);
  462. }
  463. EXPORT_SYMBOL(omap_dss_get_device);
  464. void omap_dss_put_device(struct omap_dss_device *dssdev)
  465. {
  466. put_device(&dssdev->dev);
  467. }
  468. EXPORT_SYMBOL(omap_dss_put_device);
  469. /* ref count of the found device is incremented. ref count
  470. * of from-device is decremented. */
  471. struct omap_dss_device *omap_dss_get_next_device(struct omap_dss_device *from)
  472. {
  473. struct device *dev;
  474. struct device *dev_start = NULL;
  475. struct omap_dss_device *dssdev = NULL;
  476. int match(struct device *dev, void *data)
  477. {
  478. return 1;
  479. }
  480. if (from)
  481. dev_start = &from->dev;
  482. dev = bus_find_device(dss_get_bus(), dev_start, NULL, match);
  483. if (dev)
  484. dssdev = to_dss_device(dev);
  485. if (from)
  486. put_device(&from->dev);
  487. return dssdev;
  488. }
  489. EXPORT_SYMBOL(omap_dss_get_next_device);
  490. struct omap_dss_device *omap_dss_find_device(void *data,
  491. int (*match)(struct omap_dss_device *dssdev, void *data))
  492. {
  493. struct omap_dss_device *dssdev = NULL;
  494. while ((dssdev = omap_dss_get_next_device(dssdev)) != NULL) {
  495. if (match(dssdev, data))
  496. return dssdev;
  497. }
  498. return NULL;
  499. }
  500. EXPORT_SYMBOL(omap_dss_find_device);
  501. int omap_dss_start_device(struct omap_dss_device *dssdev)
  502. {
  503. if (!dssdev->driver) {
  504. DSSDBG("no driver\n");
  505. return -ENODEV;
  506. }
  507. if (!try_module_get(dssdev->dev.driver->owner)) {
  508. return -ENODEV;
  509. }
  510. return 0;
  511. }
  512. EXPORT_SYMBOL(omap_dss_start_device);
  513. void omap_dss_stop_device(struct omap_dss_device *dssdev)
  514. {
  515. module_put(dssdev->dev.driver->owner);
  516. }
  517. EXPORT_SYMBOL(omap_dss_stop_device);