display.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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;
  43. bool enabled;
  44. r = strtobool(buf, &enabled);
  45. if (r)
  46. return r;
  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 r;
  71. bool te;
  72. if (!dssdev->driver->enable_te || !dssdev->driver->get_te)
  73. return -ENOENT;
  74. r = strtobool(buf, &te);
  75. if (r)
  76. return r;
  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 = dssdev->panel.timings;
  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->disable(dssdev);
  122. dssdev->driver->set_timings(dssdev, &t);
  123. r = dssdev->driver->enable(dssdev);
  124. if (r)
  125. return r;
  126. return size;
  127. }
  128. static ssize_t display_rotate_show(struct device *dev,
  129. struct device_attribute *attr, char *buf)
  130. {
  131. struct omap_dss_device *dssdev = to_dss_device(dev);
  132. int rotate;
  133. if (!dssdev->driver->get_rotate)
  134. return -ENOENT;
  135. rotate = dssdev->driver->get_rotate(dssdev);
  136. return snprintf(buf, PAGE_SIZE, "%u\n", rotate);
  137. }
  138. static ssize_t display_rotate_store(struct device *dev,
  139. struct device_attribute *attr, const char *buf, size_t size)
  140. {
  141. struct omap_dss_device *dssdev = to_dss_device(dev);
  142. int rot, r;
  143. if (!dssdev->driver->set_rotate || !dssdev->driver->get_rotate)
  144. return -ENOENT;
  145. r = kstrtoint(buf, 0, &rot);
  146. if (r)
  147. return r;
  148. r = dssdev->driver->set_rotate(dssdev, rot);
  149. if (r)
  150. return r;
  151. return size;
  152. }
  153. static ssize_t display_mirror_show(struct device *dev,
  154. struct device_attribute *attr, char *buf)
  155. {
  156. struct omap_dss_device *dssdev = to_dss_device(dev);
  157. int mirror;
  158. if (!dssdev->driver->get_mirror)
  159. return -ENOENT;
  160. mirror = dssdev->driver->get_mirror(dssdev);
  161. return snprintf(buf, PAGE_SIZE, "%u\n", mirror);
  162. }
  163. static ssize_t display_mirror_store(struct device *dev,
  164. struct device_attribute *attr, const char *buf, size_t size)
  165. {
  166. struct omap_dss_device *dssdev = to_dss_device(dev);
  167. int r;
  168. bool mirror;
  169. if (!dssdev->driver->set_mirror || !dssdev->driver->get_mirror)
  170. return -ENOENT;
  171. r = strtobool(buf, &mirror);
  172. if (r)
  173. return r;
  174. r = dssdev->driver->set_mirror(dssdev, mirror);
  175. if (r)
  176. return r;
  177. return size;
  178. }
  179. static ssize_t display_wss_show(struct device *dev,
  180. struct device_attribute *attr, char *buf)
  181. {
  182. struct omap_dss_device *dssdev = to_dss_device(dev);
  183. unsigned int wss;
  184. if (!dssdev->driver->get_wss)
  185. return -ENOENT;
  186. wss = dssdev->driver->get_wss(dssdev);
  187. return snprintf(buf, PAGE_SIZE, "0x%05x\n", wss);
  188. }
  189. static ssize_t display_wss_store(struct device *dev,
  190. struct device_attribute *attr, const char *buf, size_t size)
  191. {
  192. struct omap_dss_device *dssdev = to_dss_device(dev);
  193. u32 wss;
  194. int r;
  195. if (!dssdev->driver->get_wss || !dssdev->driver->set_wss)
  196. return -ENOENT;
  197. r = kstrtou32(buf, 0, &wss);
  198. if (r)
  199. return r;
  200. if (wss > 0xfffff)
  201. return -EINVAL;
  202. r = dssdev->driver->set_wss(dssdev, wss);
  203. if (r)
  204. return r;
  205. return size;
  206. }
  207. static DEVICE_ATTR(enabled, S_IRUGO|S_IWUSR,
  208. display_enabled_show, display_enabled_store);
  209. static DEVICE_ATTR(tear_elim, S_IRUGO|S_IWUSR,
  210. display_tear_show, display_tear_store);
  211. static DEVICE_ATTR(timings, S_IRUGO|S_IWUSR,
  212. display_timings_show, display_timings_store);
  213. static DEVICE_ATTR(rotate, S_IRUGO|S_IWUSR,
  214. display_rotate_show, display_rotate_store);
  215. static DEVICE_ATTR(mirror, S_IRUGO|S_IWUSR,
  216. display_mirror_show, display_mirror_store);
  217. static DEVICE_ATTR(wss, S_IRUGO|S_IWUSR,
  218. display_wss_show, display_wss_store);
  219. static struct device_attribute *display_sysfs_attrs[] = {
  220. &dev_attr_enabled,
  221. &dev_attr_tear_elim,
  222. &dev_attr_timings,
  223. &dev_attr_rotate,
  224. &dev_attr_mirror,
  225. &dev_attr_wss,
  226. NULL
  227. };
  228. void omapdss_default_get_resolution(struct omap_dss_device *dssdev,
  229. u16 *xres, u16 *yres)
  230. {
  231. *xres = dssdev->panel.timings.x_res;
  232. *yres = dssdev->panel.timings.y_res;
  233. }
  234. EXPORT_SYMBOL(omapdss_default_get_resolution);
  235. int omapdss_default_get_recommended_bpp(struct omap_dss_device *dssdev)
  236. {
  237. switch (dssdev->type) {
  238. case OMAP_DISPLAY_TYPE_DPI:
  239. if (dssdev->phy.dpi.data_lines == 24)
  240. return 24;
  241. else
  242. return 16;
  243. case OMAP_DISPLAY_TYPE_DBI:
  244. if (dssdev->ctrl.pixel_size == 24)
  245. return 24;
  246. else
  247. return 16;
  248. case OMAP_DISPLAY_TYPE_DSI:
  249. if (dsi_get_pixel_size(dssdev->panel.dsi_pix_fmt) > 16)
  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. return 0;
  260. }
  261. }
  262. EXPORT_SYMBOL(omapdss_default_get_recommended_bpp);
  263. void omapdss_default_get_timings(struct omap_dss_device *dssdev,
  264. struct omap_video_timings *timings)
  265. {
  266. *timings = dssdev->panel.timings;
  267. }
  268. EXPORT_SYMBOL(omapdss_default_get_timings);
  269. /*
  270. * Connect dssdev to a manager if the manager is free or if force is specified.
  271. * Connect all overlays to that manager if they are free or if force is
  272. * specified.
  273. */
  274. static int dss_init_connections(struct omap_dss_device *dssdev, bool force)
  275. {
  276. struct omap_dss_output *out;
  277. struct omap_overlay_manager *mgr;
  278. int i, r;
  279. out = omapdss_get_output_from_dssdev(dssdev);
  280. WARN_ON(dssdev->output);
  281. WARN_ON(out->device);
  282. r = omapdss_output_set_device(out, dssdev);
  283. if (r) {
  284. DSSERR("failed to connect output to new device\n");
  285. return r;
  286. }
  287. mgr = omap_dss_get_overlay_manager(dssdev->channel);
  288. if (mgr->output && !force)
  289. return 0;
  290. if (mgr->output)
  291. mgr->unset_output(mgr);
  292. r = mgr->set_output(mgr, out);
  293. if (r) {
  294. DSSERR("failed to connect manager to output of new device\n");
  295. /* remove the output-device connection we just made */
  296. omapdss_output_unset_device(out);
  297. return r;
  298. }
  299. for (i = 0; i < omap_dss_get_num_overlays(); ++i) {
  300. struct omap_overlay *ovl = omap_dss_get_overlay(i);
  301. if (!ovl->manager || force) {
  302. if (ovl->manager)
  303. ovl->unset_manager(ovl);
  304. r = ovl->set_manager(ovl, mgr);
  305. if (r) {
  306. DSSERR("failed to set initial overlay\n");
  307. return r;
  308. }
  309. }
  310. }
  311. return 0;
  312. }
  313. static void dss_uninit_connections(struct omap_dss_device *dssdev)
  314. {
  315. if (dssdev->output) {
  316. struct omap_overlay_manager *mgr = dssdev->output->manager;
  317. if (mgr)
  318. mgr->unset_output(mgr);
  319. omapdss_output_unset_device(dssdev->output);
  320. }
  321. }
  322. int dss_init_device(struct platform_device *pdev,
  323. struct omap_dss_device *dssdev)
  324. {
  325. struct device_attribute *attr;
  326. int i, r;
  327. const char *def_disp_name = dss_get_default_display_name();
  328. bool force;
  329. force = def_disp_name && strcmp(def_disp_name, dssdev->name) == 0;
  330. dss_init_connections(dssdev, force);
  331. /* create device sysfs files */
  332. i = 0;
  333. while ((attr = display_sysfs_attrs[i++]) != NULL) {
  334. r = device_create_file(&dssdev->dev, attr);
  335. if (r) {
  336. for (i = i - 2; i >= 0; i--) {
  337. attr = display_sysfs_attrs[i];
  338. device_remove_file(&dssdev->dev, attr);
  339. }
  340. dss_uninit_connections(dssdev);
  341. DSSERR("failed to create sysfs file\n");
  342. return r;
  343. }
  344. }
  345. /* create display? sysfs links */
  346. r = sysfs_create_link(&pdev->dev.kobj, &dssdev->dev.kobj,
  347. dev_name(&dssdev->dev));
  348. if (r) {
  349. while ((attr = display_sysfs_attrs[i++]) != NULL)
  350. device_remove_file(&dssdev->dev, attr);
  351. dss_uninit_connections(dssdev);
  352. DSSERR("failed to create sysfs display link\n");
  353. return r;
  354. }
  355. return 0;
  356. }
  357. void dss_uninit_device(struct platform_device *pdev,
  358. struct omap_dss_device *dssdev)
  359. {
  360. struct device_attribute *attr;
  361. int i = 0;
  362. sysfs_remove_link(&pdev->dev.kobj, dev_name(&dssdev->dev));
  363. while ((attr = display_sysfs_attrs[i++]) != NULL)
  364. device_remove_file(&dssdev->dev, attr);
  365. dss_uninit_connections(dssdev);
  366. }
  367. static int dss_suspend_device(struct device *dev, void *data)
  368. {
  369. struct omap_dss_device *dssdev = to_dss_device(dev);
  370. if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE) {
  371. dssdev->activate_after_resume = false;
  372. return 0;
  373. }
  374. dssdev->driver->disable(dssdev);
  375. dssdev->activate_after_resume = true;
  376. return 0;
  377. }
  378. int dss_suspend_all_devices(void)
  379. {
  380. int r;
  381. struct bus_type *bus = dss_get_bus();
  382. r = bus_for_each_dev(bus, NULL, NULL, dss_suspend_device);
  383. if (r) {
  384. /* resume all displays that were suspended */
  385. dss_resume_all_devices();
  386. return r;
  387. }
  388. return 0;
  389. }
  390. static int dss_resume_device(struct device *dev, void *data)
  391. {
  392. int r;
  393. struct omap_dss_device *dssdev = to_dss_device(dev);
  394. if (dssdev->activate_after_resume) {
  395. r = dssdev->driver->enable(dssdev);
  396. if (r)
  397. return r;
  398. }
  399. dssdev->activate_after_resume = false;
  400. return 0;
  401. }
  402. int dss_resume_all_devices(void)
  403. {
  404. struct bus_type *bus = dss_get_bus();
  405. return bus_for_each_dev(bus, NULL, NULL, dss_resume_device);
  406. }
  407. static int dss_disable_device(struct device *dev, void *data)
  408. {
  409. struct omap_dss_device *dssdev = to_dss_device(dev);
  410. if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED)
  411. dssdev->driver->disable(dssdev);
  412. return 0;
  413. }
  414. void dss_disable_all_devices(void)
  415. {
  416. struct bus_type *bus = dss_get_bus();
  417. bus_for_each_dev(bus, NULL, NULL, dss_disable_device);
  418. }
  419. void omap_dss_get_device(struct omap_dss_device *dssdev)
  420. {
  421. get_device(&dssdev->dev);
  422. }
  423. EXPORT_SYMBOL(omap_dss_get_device);
  424. void omap_dss_put_device(struct omap_dss_device *dssdev)
  425. {
  426. put_device(&dssdev->dev);
  427. }
  428. EXPORT_SYMBOL(omap_dss_put_device);
  429. /* ref count of the found device is incremented. ref count
  430. * of from-device is decremented. */
  431. struct omap_dss_device *omap_dss_get_next_device(struct omap_dss_device *from)
  432. {
  433. struct device *dev;
  434. struct device *dev_start = NULL;
  435. struct omap_dss_device *dssdev = NULL;
  436. int match(struct device *dev, void *data)
  437. {
  438. return 1;
  439. }
  440. if (from)
  441. dev_start = &from->dev;
  442. dev = bus_find_device(dss_get_bus(), dev_start, NULL, match);
  443. if (dev)
  444. dssdev = to_dss_device(dev);
  445. if (from)
  446. put_device(&from->dev);
  447. return dssdev;
  448. }
  449. EXPORT_SYMBOL(omap_dss_get_next_device);
  450. struct omap_dss_device *omap_dss_find_device(void *data,
  451. int (*match)(struct omap_dss_device *dssdev, void *data))
  452. {
  453. struct omap_dss_device *dssdev = NULL;
  454. while ((dssdev = omap_dss_get_next_device(dssdev)) != NULL) {
  455. if (match(dssdev, data))
  456. return dssdev;
  457. }
  458. return NULL;
  459. }
  460. EXPORT_SYMBOL(omap_dss_find_device);
  461. int omap_dss_start_device(struct omap_dss_device *dssdev)
  462. {
  463. if (!dssdev->driver) {
  464. DSSDBG("no driver\n");
  465. return -ENODEV;
  466. }
  467. if (!try_module_get(dssdev->dev.driver->owner)) {
  468. return -ENODEV;
  469. }
  470. return 0;
  471. }
  472. EXPORT_SYMBOL(omap_dss_start_device);
  473. void omap_dss_stop_device(struct omap_dss_device *dssdev)
  474. {
  475. module_put(dssdev->dev.driver->owner);
  476. }
  477. EXPORT_SYMBOL(omap_dss_stop_device);