display.c 13 KB

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