display.c 13 KB

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