panel-dsi-cm.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. /*
  2. * Generic DSI Command Mode panel driver
  3. *
  4. * Copyright (C) 2013 Texas Instruments
  5. * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. /* #define DEBUG */
  12. #include <linux/backlight.h>
  13. #include <linux/delay.h>
  14. #include <linux/fb.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_gpio.h>
  25. #include <video/mipi_display.h>
  26. #include "../dss/omapdss.h"
  27. /* DSI Virtual channel. Hardcoded for now. */
  28. #define TCH 0
  29. #define DCS_READ_NUM_ERRORS 0x05
  30. #define DCS_BRIGHTNESS 0x51
  31. #define DCS_CTRL_DISPLAY 0x53
  32. #define DCS_GET_ID1 0xda
  33. #define DCS_GET_ID2 0xdb
  34. #define DCS_GET_ID3 0xdc
  35. struct panel_drv_data {
  36. struct omap_dss_device dssdev;
  37. struct omap_dss_device *in;
  38. struct omap_video_timings timings;
  39. struct platform_device *pdev;
  40. struct mutex lock;
  41. struct backlight_device *bldev;
  42. unsigned long hw_guard_end; /* next value of jiffies when we can
  43. * issue the next sleep in/out command
  44. */
  45. unsigned long hw_guard_wait; /* max guard time in jiffies */
  46. /* panel HW configuration from DT or platform data */
  47. int reset_gpio;
  48. int ext_te_gpio;
  49. bool use_dsi_backlight;
  50. struct omap_dsi_pin_config pin_config;
  51. /* runtime variables */
  52. bool enabled;
  53. bool te_enabled;
  54. atomic_t do_update;
  55. int channel;
  56. struct delayed_work te_timeout_work;
  57. bool intro_printed;
  58. struct workqueue_struct *workqueue;
  59. bool ulps_enabled;
  60. unsigned ulps_timeout;
  61. struct delayed_work ulps_work;
  62. };
  63. #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
  64. static irqreturn_t dsicm_te_isr(int irq, void *data);
  65. static void dsicm_te_timeout_work_callback(struct work_struct *work);
  66. static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable);
  67. static int dsicm_panel_reset(struct panel_drv_data *ddata);
  68. static void dsicm_ulps_work(struct work_struct *work);
  69. static void hw_guard_start(struct panel_drv_data *ddata, int guard_msec)
  70. {
  71. ddata->hw_guard_wait = msecs_to_jiffies(guard_msec);
  72. ddata->hw_guard_end = jiffies + ddata->hw_guard_wait;
  73. }
  74. static void hw_guard_wait(struct panel_drv_data *ddata)
  75. {
  76. unsigned long wait = ddata->hw_guard_end - jiffies;
  77. if ((long)wait > 0 && wait <= ddata->hw_guard_wait) {
  78. set_current_state(TASK_UNINTERRUPTIBLE);
  79. schedule_timeout(wait);
  80. }
  81. }
  82. static int dsicm_dcs_read_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 *data)
  83. {
  84. struct omap_dss_device *in = ddata->in;
  85. int r;
  86. u8 buf[1];
  87. r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd, buf, 1);
  88. if (r < 0)
  89. return r;
  90. *data = buf[0];
  91. return 0;
  92. }
  93. static int dsicm_dcs_write_0(struct panel_drv_data *ddata, u8 dcs_cmd)
  94. {
  95. struct omap_dss_device *in = ddata->in;
  96. return in->ops.dsi->dcs_write(in, ddata->channel, &dcs_cmd, 1);
  97. }
  98. static int dsicm_dcs_write_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 param)
  99. {
  100. struct omap_dss_device *in = ddata->in;
  101. u8 buf[2] = { dcs_cmd, param };
  102. return in->ops.dsi->dcs_write(in, ddata->channel, buf, 2);
  103. }
  104. static int dsicm_sleep_in(struct panel_drv_data *ddata)
  105. {
  106. struct omap_dss_device *in = ddata->in;
  107. u8 cmd;
  108. int r;
  109. hw_guard_wait(ddata);
  110. cmd = MIPI_DCS_ENTER_SLEEP_MODE;
  111. r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, &cmd, 1);
  112. if (r)
  113. return r;
  114. hw_guard_start(ddata, 120);
  115. usleep_range(5000, 10000);
  116. return 0;
  117. }
  118. static int dsicm_sleep_out(struct panel_drv_data *ddata)
  119. {
  120. int r;
  121. hw_guard_wait(ddata);
  122. r = dsicm_dcs_write_0(ddata, MIPI_DCS_EXIT_SLEEP_MODE);
  123. if (r)
  124. return r;
  125. hw_guard_start(ddata, 120);
  126. usleep_range(5000, 10000);
  127. return 0;
  128. }
  129. static int dsicm_get_id(struct panel_drv_data *ddata, u8 *id1, u8 *id2, u8 *id3)
  130. {
  131. int r;
  132. r = dsicm_dcs_read_1(ddata, DCS_GET_ID1, id1);
  133. if (r)
  134. return r;
  135. r = dsicm_dcs_read_1(ddata, DCS_GET_ID2, id2);
  136. if (r)
  137. return r;
  138. r = dsicm_dcs_read_1(ddata, DCS_GET_ID3, id3);
  139. if (r)
  140. return r;
  141. return 0;
  142. }
  143. static int dsicm_set_update_window(struct panel_drv_data *ddata,
  144. u16 x, u16 y, u16 w, u16 h)
  145. {
  146. struct omap_dss_device *in = ddata->in;
  147. int r;
  148. u16 x1 = x;
  149. u16 x2 = x + w - 1;
  150. u16 y1 = y;
  151. u16 y2 = y + h - 1;
  152. u8 buf[5];
  153. buf[0] = MIPI_DCS_SET_COLUMN_ADDRESS;
  154. buf[1] = (x1 >> 8) & 0xff;
  155. buf[2] = (x1 >> 0) & 0xff;
  156. buf[3] = (x2 >> 8) & 0xff;
  157. buf[4] = (x2 >> 0) & 0xff;
  158. r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf));
  159. if (r)
  160. return r;
  161. buf[0] = MIPI_DCS_SET_PAGE_ADDRESS;
  162. buf[1] = (y1 >> 8) & 0xff;
  163. buf[2] = (y1 >> 0) & 0xff;
  164. buf[3] = (y2 >> 8) & 0xff;
  165. buf[4] = (y2 >> 0) & 0xff;
  166. r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf));
  167. if (r)
  168. return r;
  169. in->ops.dsi->bta_sync(in, ddata->channel);
  170. return r;
  171. }
  172. static void dsicm_queue_ulps_work(struct panel_drv_data *ddata)
  173. {
  174. if (ddata->ulps_timeout > 0)
  175. queue_delayed_work(ddata->workqueue, &ddata->ulps_work,
  176. msecs_to_jiffies(ddata->ulps_timeout));
  177. }
  178. static void dsicm_cancel_ulps_work(struct panel_drv_data *ddata)
  179. {
  180. cancel_delayed_work(&ddata->ulps_work);
  181. }
  182. static int dsicm_enter_ulps(struct panel_drv_data *ddata)
  183. {
  184. struct omap_dss_device *in = ddata->in;
  185. int r;
  186. if (ddata->ulps_enabled)
  187. return 0;
  188. dsicm_cancel_ulps_work(ddata);
  189. r = _dsicm_enable_te(ddata, false);
  190. if (r)
  191. goto err;
  192. if (gpio_is_valid(ddata->ext_te_gpio))
  193. disable_irq(gpio_to_irq(ddata->ext_te_gpio));
  194. in->ops.dsi->disable(in, false, true);
  195. ddata->ulps_enabled = true;
  196. return 0;
  197. err:
  198. dev_err(&ddata->pdev->dev, "enter ULPS failed");
  199. dsicm_panel_reset(ddata);
  200. ddata->ulps_enabled = false;
  201. dsicm_queue_ulps_work(ddata);
  202. return r;
  203. }
  204. static int dsicm_exit_ulps(struct panel_drv_data *ddata)
  205. {
  206. struct omap_dss_device *in = ddata->in;
  207. int r;
  208. if (!ddata->ulps_enabled)
  209. return 0;
  210. r = in->ops.dsi->enable(in);
  211. if (r) {
  212. dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
  213. goto err1;
  214. }
  215. in->ops.dsi->enable_hs(in, ddata->channel, true);
  216. r = _dsicm_enable_te(ddata, true);
  217. if (r) {
  218. dev_err(&ddata->pdev->dev, "failed to re-enable TE");
  219. goto err2;
  220. }
  221. if (gpio_is_valid(ddata->ext_te_gpio))
  222. enable_irq(gpio_to_irq(ddata->ext_te_gpio));
  223. dsicm_queue_ulps_work(ddata);
  224. ddata->ulps_enabled = false;
  225. return 0;
  226. err2:
  227. dev_err(&ddata->pdev->dev, "failed to exit ULPS");
  228. r = dsicm_panel_reset(ddata);
  229. if (!r) {
  230. if (gpio_is_valid(ddata->ext_te_gpio))
  231. enable_irq(gpio_to_irq(ddata->ext_te_gpio));
  232. ddata->ulps_enabled = false;
  233. }
  234. err1:
  235. dsicm_queue_ulps_work(ddata);
  236. return r;
  237. }
  238. static int dsicm_wake_up(struct panel_drv_data *ddata)
  239. {
  240. if (ddata->ulps_enabled)
  241. return dsicm_exit_ulps(ddata);
  242. dsicm_cancel_ulps_work(ddata);
  243. dsicm_queue_ulps_work(ddata);
  244. return 0;
  245. }
  246. static int dsicm_bl_update_status(struct backlight_device *dev)
  247. {
  248. struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
  249. struct omap_dss_device *in = ddata->in;
  250. int r;
  251. int level;
  252. if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
  253. dev->props.power == FB_BLANK_UNBLANK)
  254. level = dev->props.brightness;
  255. else
  256. level = 0;
  257. dev_dbg(&ddata->pdev->dev, "update brightness to %d\n", level);
  258. mutex_lock(&ddata->lock);
  259. if (ddata->enabled) {
  260. in->ops.dsi->bus_lock(in);
  261. r = dsicm_wake_up(ddata);
  262. if (!r)
  263. r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, level);
  264. in->ops.dsi->bus_unlock(in);
  265. } else {
  266. r = 0;
  267. }
  268. mutex_unlock(&ddata->lock);
  269. return r;
  270. }
  271. static int dsicm_bl_get_intensity(struct backlight_device *dev)
  272. {
  273. if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
  274. dev->props.power == FB_BLANK_UNBLANK)
  275. return dev->props.brightness;
  276. return 0;
  277. }
  278. static const struct backlight_ops dsicm_bl_ops = {
  279. .get_brightness = dsicm_bl_get_intensity,
  280. .update_status = dsicm_bl_update_status,
  281. };
  282. static void dsicm_get_resolution(struct omap_dss_device *dssdev,
  283. u16 *xres, u16 *yres)
  284. {
  285. *xres = dssdev->panel.timings.x_res;
  286. *yres = dssdev->panel.timings.y_res;
  287. }
  288. static ssize_t dsicm_num_errors_show(struct device *dev,
  289. struct device_attribute *attr, char *buf)
  290. {
  291. struct platform_device *pdev = to_platform_device(dev);
  292. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  293. struct omap_dss_device *in = ddata->in;
  294. u8 errors = 0;
  295. int r;
  296. mutex_lock(&ddata->lock);
  297. if (ddata->enabled) {
  298. in->ops.dsi->bus_lock(in);
  299. r = dsicm_wake_up(ddata);
  300. if (!r)
  301. r = dsicm_dcs_read_1(ddata, DCS_READ_NUM_ERRORS,
  302. &errors);
  303. in->ops.dsi->bus_unlock(in);
  304. } else {
  305. r = -ENODEV;
  306. }
  307. mutex_unlock(&ddata->lock);
  308. if (r)
  309. return r;
  310. return snprintf(buf, PAGE_SIZE, "%d\n", errors);
  311. }
  312. static ssize_t dsicm_hw_revision_show(struct device *dev,
  313. struct device_attribute *attr, char *buf)
  314. {
  315. struct platform_device *pdev = to_platform_device(dev);
  316. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  317. struct omap_dss_device *in = ddata->in;
  318. u8 id1, id2, id3;
  319. int r;
  320. mutex_lock(&ddata->lock);
  321. if (ddata->enabled) {
  322. in->ops.dsi->bus_lock(in);
  323. r = dsicm_wake_up(ddata);
  324. if (!r)
  325. r = dsicm_get_id(ddata, &id1, &id2, &id3);
  326. in->ops.dsi->bus_unlock(in);
  327. } else {
  328. r = -ENODEV;
  329. }
  330. mutex_unlock(&ddata->lock);
  331. if (r)
  332. return r;
  333. return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x\n", id1, id2, id3);
  334. }
  335. static ssize_t dsicm_store_ulps(struct device *dev,
  336. struct device_attribute *attr,
  337. const char *buf, size_t count)
  338. {
  339. struct platform_device *pdev = to_platform_device(dev);
  340. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  341. struct omap_dss_device *in = ddata->in;
  342. unsigned long t;
  343. int r;
  344. r = kstrtoul(buf, 0, &t);
  345. if (r)
  346. return r;
  347. mutex_lock(&ddata->lock);
  348. if (ddata->enabled) {
  349. in->ops.dsi->bus_lock(in);
  350. if (t)
  351. r = dsicm_enter_ulps(ddata);
  352. else
  353. r = dsicm_wake_up(ddata);
  354. in->ops.dsi->bus_unlock(in);
  355. }
  356. mutex_unlock(&ddata->lock);
  357. if (r)
  358. return r;
  359. return count;
  360. }
  361. static ssize_t dsicm_show_ulps(struct device *dev,
  362. struct device_attribute *attr,
  363. char *buf)
  364. {
  365. struct platform_device *pdev = to_platform_device(dev);
  366. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  367. unsigned t;
  368. mutex_lock(&ddata->lock);
  369. t = ddata->ulps_enabled;
  370. mutex_unlock(&ddata->lock);
  371. return snprintf(buf, PAGE_SIZE, "%u\n", t);
  372. }
  373. static ssize_t dsicm_store_ulps_timeout(struct device *dev,
  374. struct device_attribute *attr,
  375. const char *buf, size_t count)
  376. {
  377. struct platform_device *pdev = to_platform_device(dev);
  378. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  379. struct omap_dss_device *in = ddata->in;
  380. unsigned long t;
  381. int r;
  382. r = kstrtoul(buf, 0, &t);
  383. if (r)
  384. return r;
  385. mutex_lock(&ddata->lock);
  386. ddata->ulps_timeout = t;
  387. if (ddata->enabled) {
  388. /* dsicm_wake_up will restart the timer */
  389. in->ops.dsi->bus_lock(in);
  390. r = dsicm_wake_up(ddata);
  391. in->ops.dsi->bus_unlock(in);
  392. }
  393. mutex_unlock(&ddata->lock);
  394. if (r)
  395. return r;
  396. return count;
  397. }
  398. static ssize_t dsicm_show_ulps_timeout(struct device *dev,
  399. struct device_attribute *attr,
  400. char *buf)
  401. {
  402. struct platform_device *pdev = to_platform_device(dev);
  403. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  404. unsigned t;
  405. mutex_lock(&ddata->lock);
  406. t = ddata->ulps_timeout;
  407. mutex_unlock(&ddata->lock);
  408. return snprintf(buf, PAGE_SIZE, "%u\n", t);
  409. }
  410. static DEVICE_ATTR(num_dsi_errors, S_IRUGO, dsicm_num_errors_show, NULL);
  411. static DEVICE_ATTR(hw_revision, S_IRUGO, dsicm_hw_revision_show, NULL);
  412. static DEVICE_ATTR(ulps, S_IRUGO | S_IWUSR,
  413. dsicm_show_ulps, dsicm_store_ulps);
  414. static DEVICE_ATTR(ulps_timeout, S_IRUGO | S_IWUSR,
  415. dsicm_show_ulps_timeout, dsicm_store_ulps_timeout);
  416. static struct attribute *dsicm_attrs[] = {
  417. &dev_attr_num_dsi_errors.attr,
  418. &dev_attr_hw_revision.attr,
  419. &dev_attr_ulps.attr,
  420. &dev_attr_ulps_timeout.attr,
  421. NULL,
  422. };
  423. static struct attribute_group dsicm_attr_group = {
  424. .attrs = dsicm_attrs,
  425. };
  426. static void dsicm_hw_reset(struct panel_drv_data *ddata)
  427. {
  428. if (!gpio_is_valid(ddata->reset_gpio))
  429. return;
  430. gpio_set_value(ddata->reset_gpio, 1);
  431. udelay(10);
  432. /* reset the panel */
  433. gpio_set_value(ddata->reset_gpio, 0);
  434. /* assert reset */
  435. udelay(10);
  436. gpio_set_value(ddata->reset_gpio, 1);
  437. /* wait after releasing reset */
  438. usleep_range(5000, 10000);
  439. }
  440. static int dsicm_power_on(struct panel_drv_data *ddata)
  441. {
  442. struct omap_dss_device *in = ddata->in;
  443. u8 id1, id2, id3;
  444. int r;
  445. struct omap_dss_dsi_config dsi_config = {
  446. .mode = OMAP_DSS_DSI_CMD_MODE,
  447. .pixel_format = OMAP_DSS_DSI_FMT_RGB888,
  448. .timings = &ddata->timings,
  449. .hs_clk_min = 150000000,
  450. .hs_clk_max = 300000000,
  451. .lp_clk_min = 7000000,
  452. .lp_clk_max = 10000000,
  453. };
  454. if (ddata->pin_config.num_pins > 0) {
  455. r = in->ops.dsi->configure_pins(in, &ddata->pin_config);
  456. if (r) {
  457. dev_err(&ddata->pdev->dev,
  458. "failed to configure DSI pins\n");
  459. goto err0;
  460. }
  461. }
  462. r = in->ops.dsi->set_config(in, &dsi_config);
  463. if (r) {
  464. dev_err(&ddata->pdev->dev, "failed to configure DSI\n");
  465. goto err0;
  466. }
  467. r = in->ops.dsi->enable(in);
  468. if (r) {
  469. dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
  470. goto err0;
  471. }
  472. dsicm_hw_reset(ddata);
  473. in->ops.dsi->enable_hs(in, ddata->channel, false);
  474. r = dsicm_sleep_out(ddata);
  475. if (r)
  476. goto err;
  477. r = dsicm_get_id(ddata, &id1, &id2, &id3);
  478. if (r)
  479. goto err;
  480. r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, 0xff);
  481. if (r)
  482. goto err;
  483. r = dsicm_dcs_write_1(ddata, DCS_CTRL_DISPLAY,
  484. (1<<2) | (1<<5)); /* BL | BCTRL */
  485. if (r)
  486. goto err;
  487. r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_PIXEL_FORMAT,
  488. MIPI_DCS_PIXEL_FMT_24BIT);
  489. if (r)
  490. goto err;
  491. r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_ON);
  492. if (r)
  493. goto err;
  494. r = _dsicm_enable_te(ddata, ddata->te_enabled);
  495. if (r)
  496. goto err;
  497. r = in->ops.dsi->enable_video_output(in, ddata->channel);
  498. if (r)
  499. goto err;
  500. ddata->enabled = 1;
  501. if (!ddata->intro_printed) {
  502. dev_info(&ddata->pdev->dev, "panel revision %02x.%02x.%02x\n",
  503. id1, id2, id3);
  504. ddata->intro_printed = true;
  505. }
  506. in->ops.dsi->enable_hs(in, ddata->channel, true);
  507. return 0;
  508. err:
  509. dev_err(&ddata->pdev->dev, "error while enabling panel, issuing HW reset\n");
  510. dsicm_hw_reset(ddata);
  511. in->ops.dsi->disable(in, true, false);
  512. err0:
  513. return r;
  514. }
  515. static void dsicm_power_off(struct panel_drv_data *ddata)
  516. {
  517. struct omap_dss_device *in = ddata->in;
  518. int r;
  519. in->ops.dsi->disable_video_output(in, ddata->channel);
  520. r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_OFF);
  521. if (!r)
  522. r = dsicm_sleep_in(ddata);
  523. if (r) {
  524. dev_err(&ddata->pdev->dev,
  525. "error disabling panel, issuing HW reset\n");
  526. dsicm_hw_reset(ddata);
  527. }
  528. in->ops.dsi->disable(in, true, false);
  529. ddata->enabled = 0;
  530. }
  531. static int dsicm_panel_reset(struct panel_drv_data *ddata)
  532. {
  533. dev_err(&ddata->pdev->dev, "performing LCD reset\n");
  534. dsicm_power_off(ddata);
  535. dsicm_hw_reset(ddata);
  536. return dsicm_power_on(ddata);
  537. }
  538. static int dsicm_connect(struct omap_dss_device *dssdev)
  539. {
  540. struct panel_drv_data *ddata = to_panel_data(dssdev);
  541. struct omap_dss_device *in = ddata->in;
  542. struct device *dev = &ddata->pdev->dev;
  543. int r;
  544. if (omapdss_device_is_connected(dssdev))
  545. return 0;
  546. r = in->ops.dsi->connect(in, dssdev);
  547. if (r) {
  548. dev_err(dev, "Failed to connect to video source\n");
  549. return r;
  550. }
  551. r = in->ops.dsi->request_vc(ddata->in, &ddata->channel);
  552. if (r) {
  553. dev_err(dev, "failed to get virtual channel\n");
  554. goto err_req_vc;
  555. }
  556. r = in->ops.dsi->set_vc_id(ddata->in, ddata->channel, TCH);
  557. if (r) {
  558. dev_err(dev, "failed to set VC_ID\n");
  559. goto err_vc_id;
  560. }
  561. return 0;
  562. err_vc_id:
  563. in->ops.dsi->release_vc(ddata->in, ddata->channel);
  564. err_req_vc:
  565. in->ops.dsi->disconnect(in, dssdev);
  566. return r;
  567. }
  568. static void dsicm_disconnect(struct omap_dss_device *dssdev)
  569. {
  570. struct panel_drv_data *ddata = to_panel_data(dssdev);
  571. struct omap_dss_device *in = ddata->in;
  572. if (!omapdss_device_is_connected(dssdev))
  573. return;
  574. in->ops.dsi->release_vc(in, ddata->channel);
  575. in->ops.dsi->disconnect(in, dssdev);
  576. }
  577. static int dsicm_enable(struct omap_dss_device *dssdev)
  578. {
  579. struct panel_drv_data *ddata = to_panel_data(dssdev);
  580. struct omap_dss_device *in = ddata->in;
  581. int r;
  582. dev_dbg(&ddata->pdev->dev, "enable\n");
  583. mutex_lock(&ddata->lock);
  584. if (!omapdss_device_is_connected(dssdev)) {
  585. r = -ENODEV;
  586. goto err;
  587. }
  588. if (omapdss_device_is_enabled(dssdev)) {
  589. r = 0;
  590. goto err;
  591. }
  592. in->ops.dsi->bus_lock(in);
  593. r = dsicm_power_on(ddata);
  594. in->ops.dsi->bus_unlock(in);
  595. if (r)
  596. goto err;
  597. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  598. mutex_unlock(&ddata->lock);
  599. return 0;
  600. err:
  601. dev_dbg(&ddata->pdev->dev, "enable failed\n");
  602. mutex_unlock(&ddata->lock);
  603. return r;
  604. }
  605. static void dsicm_disable(struct omap_dss_device *dssdev)
  606. {
  607. struct panel_drv_data *ddata = to_panel_data(dssdev);
  608. struct omap_dss_device *in = ddata->in;
  609. int r;
  610. dev_dbg(&ddata->pdev->dev, "disable\n");
  611. mutex_lock(&ddata->lock);
  612. dsicm_cancel_ulps_work(ddata);
  613. in->ops.dsi->bus_lock(in);
  614. if (omapdss_device_is_enabled(dssdev)) {
  615. r = dsicm_wake_up(ddata);
  616. if (!r)
  617. dsicm_power_off(ddata);
  618. }
  619. in->ops.dsi->bus_unlock(in);
  620. dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
  621. mutex_unlock(&ddata->lock);
  622. }
  623. static void dsicm_framedone_cb(int err, void *data)
  624. {
  625. struct panel_drv_data *ddata = data;
  626. struct omap_dss_device *in = ddata->in;
  627. dev_dbg(&ddata->pdev->dev, "framedone, err %d\n", err);
  628. in->ops.dsi->bus_unlock(ddata->in);
  629. }
  630. static irqreturn_t dsicm_te_isr(int irq, void *data)
  631. {
  632. struct panel_drv_data *ddata = data;
  633. struct omap_dss_device *in = ddata->in;
  634. int old;
  635. int r;
  636. old = atomic_cmpxchg(&ddata->do_update, 1, 0);
  637. if (old) {
  638. cancel_delayed_work(&ddata->te_timeout_work);
  639. r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
  640. ddata);
  641. if (r)
  642. goto err;
  643. }
  644. return IRQ_HANDLED;
  645. err:
  646. dev_err(&ddata->pdev->dev, "start update failed\n");
  647. in->ops.dsi->bus_unlock(in);
  648. return IRQ_HANDLED;
  649. }
  650. static void dsicm_te_timeout_work_callback(struct work_struct *work)
  651. {
  652. struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
  653. te_timeout_work.work);
  654. struct omap_dss_device *in = ddata->in;
  655. dev_err(&ddata->pdev->dev, "TE not received for 250ms!\n");
  656. atomic_set(&ddata->do_update, 0);
  657. in->ops.dsi->bus_unlock(in);
  658. }
  659. static int dsicm_update(struct omap_dss_device *dssdev,
  660. u16 x, u16 y, u16 w, u16 h)
  661. {
  662. struct panel_drv_data *ddata = to_panel_data(dssdev);
  663. struct omap_dss_device *in = ddata->in;
  664. int r;
  665. dev_dbg(&ddata->pdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);
  666. mutex_lock(&ddata->lock);
  667. in->ops.dsi->bus_lock(in);
  668. r = dsicm_wake_up(ddata);
  669. if (r)
  670. goto err;
  671. if (!ddata->enabled) {
  672. r = 0;
  673. goto err;
  674. }
  675. /* XXX no need to send this every frame, but dsi break if not done */
  676. r = dsicm_set_update_window(ddata, 0, 0,
  677. dssdev->panel.timings.x_res,
  678. dssdev->panel.timings.y_res);
  679. if (r)
  680. goto err;
  681. if (ddata->te_enabled && gpio_is_valid(ddata->ext_te_gpio)) {
  682. schedule_delayed_work(&ddata->te_timeout_work,
  683. msecs_to_jiffies(250));
  684. atomic_set(&ddata->do_update, 1);
  685. } else {
  686. r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
  687. ddata);
  688. if (r)
  689. goto err;
  690. }
  691. /* note: no bus_unlock here. unlock is in framedone_cb */
  692. mutex_unlock(&ddata->lock);
  693. return 0;
  694. err:
  695. in->ops.dsi->bus_unlock(in);
  696. mutex_unlock(&ddata->lock);
  697. return r;
  698. }
  699. static int dsicm_sync(struct omap_dss_device *dssdev)
  700. {
  701. struct panel_drv_data *ddata = to_panel_data(dssdev);
  702. struct omap_dss_device *in = ddata->in;
  703. dev_dbg(&ddata->pdev->dev, "sync\n");
  704. mutex_lock(&ddata->lock);
  705. in->ops.dsi->bus_lock(in);
  706. in->ops.dsi->bus_unlock(in);
  707. mutex_unlock(&ddata->lock);
  708. dev_dbg(&ddata->pdev->dev, "sync done\n");
  709. return 0;
  710. }
  711. static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable)
  712. {
  713. struct omap_dss_device *in = ddata->in;
  714. int r;
  715. if (enable)
  716. r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_TEAR_ON, 0);
  717. else
  718. r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_TEAR_OFF);
  719. if (!gpio_is_valid(ddata->ext_te_gpio))
  720. in->ops.dsi->enable_te(in, enable);
  721. /* possible panel bug */
  722. msleep(100);
  723. return r;
  724. }
  725. static int dsicm_enable_te(struct omap_dss_device *dssdev, bool enable)
  726. {
  727. struct panel_drv_data *ddata = to_panel_data(dssdev);
  728. struct omap_dss_device *in = ddata->in;
  729. int r;
  730. mutex_lock(&ddata->lock);
  731. if (ddata->te_enabled == enable)
  732. goto end;
  733. in->ops.dsi->bus_lock(in);
  734. if (ddata->enabled) {
  735. r = dsicm_wake_up(ddata);
  736. if (r)
  737. goto err;
  738. r = _dsicm_enable_te(ddata, enable);
  739. if (r)
  740. goto err;
  741. }
  742. ddata->te_enabled = enable;
  743. in->ops.dsi->bus_unlock(in);
  744. end:
  745. mutex_unlock(&ddata->lock);
  746. return 0;
  747. err:
  748. in->ops.dsi->bus_unlock(in);
  749. mutex_unlock(&ddata->lock);
  750. return r;
  751. }
  752. static int dsicm_get_te(struct omap_dss_device *dssdev)
  753. {
  754. struct panel_drv_data *ddata = to_panel_data(dssdev);
  755. int r;
  756. mutex_lock(&ddata->lock);
  757. r = ddata->te_enabled;
  758. mutex_unlock(&ddata->lock);
  759. return r;
  760. }
  761. static int dsicm_memory_read(struct omap_dss_device *dssdev,
  762. void *buf, size_t size,
  763. u16 x, u16 y, u16 w, u16 h)
  764. {
  765. struct panel_drv_data *ddata = to_panel_data(dssdev);
  766. struct omap_dss_device *in = ddata->in;
  767. int r;
  768. int first = 1;
  769. int plen;
  770. unsigned buf_used = 0;
  771. if (size < w * h * 3)
  772. return -ENOMEM;
  773. mutex_lock(&ddata->lock);
  774. if (!ddata->enabled) {
  775. r = -ENODEV;
  776. goto err1;
  777. }
  778. size = min(w * h * 3,
  779. dssdev->panel.timings.x_res *
  780. dssdev->panel.timings.y_res * 3);
  781. in->ops.dsi->bus_lock(in);
  782. r = dsicm_wake_up(ddata);
  783. if (r)
  784. goto err2;
  785. /* plen 1 or 2 goes into short packet. until checksum error is fixed,
  786. * use short packets. plen 32 works, but bigger packets seem to cause
  787. * an error. */
  788. if (size % 2)
  789. plen = 1;
  790. else
  791. plen = 2;
  792. dsicm_set_update_window(ddata, x, y, w, h);
  793. r = in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, plen);
  794. if (r)
  795. goto err2;
  796. while (buf_used < size) {
  797. u8 dcs_cmd = first ? 0x2e : 0x3e;
  798. first = 0;
  799. r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd,
  800. buf + buf_used, size - buf_used);
  801. if (r < 0) {
  802. dev_err(dssdev->dev, "read error\n");
  803. goto err3;
  804. }
  805. buf_used += r;
  806. if (r < plen) {
  807. dev_err(&ddata->pdev->dev, "short read\n");
  808. break;
  809. }
  810. if (signal_pending(current)) {
  811. dev_err(&ddata->pdev->dev, "signal pending, "
  812. "aborting memory read\n");
  813. r = -ERESTARTSYS;
  814. goto err3;
  815. }
  816. }
  817. r = buf_used;
  818. err3:
  819. in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, 1);
  820. err2:
  821. in->ops.dsi->bus_unlock(in);
  822. err1:
  823. mutex_unlock(&ddata->lock);
  824. return r;
  825. }
  826. static void dsicm_ulps_work(struct work_struct *work)
  827. {
  828. struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
  829. ulps_work.work);
  830. struct omap_dss_device *dssdev = &ddata->dssdev;
  831. struct omap_dss_device *in = ddata->in;
  832. mutex_lock(&ddata->lock);
  833. if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !ddata->enabled) {
  834. mutex_unlock(&ddata->lock);
  835. return;
  836. }
  837. in->ops.dsi->bus_lock(in);
  838. dsicm_enter_ulps(ddata);
  839. in->ops.dsi->bus_unlock(in);
  840. mutex_unlock(&ddata->lock);
  841. }
  842. static struct omap_dss_driver dsicm_ops = {
  843. .connect = dsicm_connect,
  844. .disconnect = dsicm_disconnect,
  845. .enable = dsicm_enable,
  846. .disable = dsicm_disable,
  847. .update = dsicm_update,
  848. .sync = dsicm_sync,
  849. .get_resolution = dsicm_get_resolution,
  850. .get_recommended_bpp = omapdss_default_get_recommended_bpp,
  851. .enable_te = dsicm_enable_te,
  852. .get_te = dsicm_get_te,
  853. .memory_read = dsicm_memory_read,
  854. };
  855. static int dsicm_probe_of(struct platform_device *pdev)
  856. {
  857. struct device_node *node = pdev->dev.of_node;
  858. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  859. struct omap_dss_device *in;
  860. int gpio;
  861. gpio = of_get_named_gpio(node, "reset-gpios", 0);
  862. if (!gpio_is_valid(gpio)) {
  863. dev_err(&pdev->dev, "failed to parse reset gpio\n");
  864. return gpio;
  865. }
  866. ddata->reset_gpio = gpio;
  867. gpio = of_get_named_gpio(node, "te-gpios", 0);
  868. if (gpio_is_valid(gpio) || gpio == -ENOENT) {
  869. ddata->ext_te_gpio = gpio;
  870. } else {
  871. dev_err(&pdev->dev, "failed to parse TE gpio\n");
  872. return gpio;
  873. }
  874. in = omapdss_of_find_source_for_first_ep(node);
  875. if (IS_ERR(in)) {
  876. dev_err(&pdev->dev, "failed to find video source\n");
  877. return PTR_ERR(in);
  878. }
  879. ddata->in = in;
  880. /* TODO: ulps, backlight */
  881. return 0;
  882. }
  883. static int dsicm_probe(struct platform_device *pdev)
  884. {
  885. struct backlight_properties props;
  886. struct panel_drv_data *ddata;
  887. struct backlight_device *bldev = NULL;
  888. struct device *dev = &pdev->dev;
  889. struct omap_dss_device *dssdev;
  890. int r;
  891. dev_dbg(dev, "probe\n");
  892. ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
  893. if (!ddata)
  894. return -ENOMEM;
  895. platform_set_drvdata(pdev, ddata);
  896. ddata->pdev = pdev;
  897. if (!pdev->dev.of_node)
  898. return -ENODEV;
  899. r = dsicm_probe_of(pdev);
  900. if (r)
  901. return r;
  902. ddata->timings.x_res = 864;
  903. ddata->timings.y_res = 480;
  904. ddata->timings.pixelclock = 864 * 480 * 60;
  905. dssdev = &ddata->dssdev;
  906. dssdev->dev = dev;
  907. dssdev->driver = &dsicm_ops;
  908. dssdev->panel.timings = ddata->timings;
  909. dssdev->type = OMAP_DISPLAY_TYPE_DSI;
  910. dssdev->owner = THIS_MODULE;
  911. dssdev->panel.dsi_pix_fmt = OMAP_DSS_DSI_FMT_RGB888;
  912. dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE |
  913. OMAP_DSS_DISPLAY_CAP_TEAR_ELIM;
  914. r = omapdss_register_display(dssdev);
  915. if (r) {
  916. dev_err(dev, "Failed to register panel\n");
  917. goto err_reg;
  918. }
  919. mutex_init(&ddata->lock);
  920. atomic_set(&ddata->do_update, 0);
  921. if (gpio_is_valid(ddata->reset_gpio)) {
  922. r = devm_gpio_request_one(dev, ddata->reset_gpio,
  923. GPIOF_OUT_INIT_LOW, "taal rst");
  924. if (r) {
  925. dev_err(dev, "failed to request reset gpio\n");
  926. return r;
  927. }
  928. }
  929. if (gpio_is_valid(ddata->ext_te_gpio)) {
  930. r = devm_gpio_request_one(dev, ddata->ext_te_gpio,
  931. GPIOF_IN, "taal irq");
  932. if (r) {
  933. dev_err(dev, "GPIO request failed\n");
  934. return r;
  935. }
  936. r = devm_request_irq(dev, gpio_to_irq(ddata->ext_te_gpio),
  937. dsicm_te_isr,
  938. IRQF_TRIGGER_RISING,
  939. "taal vsync", ddata);
  940. if (r) {
  941. dev_err(dev, "IRQ request failed\n");
  942. return r;
  943. }
  944. INIT_DEFERRABLE_WORK(&ddata->te_timeout_work,
  945. dsicm_te_timeout_work_callback);
  946. dev_dbg(dev, "Using GPIO TE\n");
  947. }
  948. ddata->workqueue = create_singlethread_workqueue("dsicm_wq");
  949. if (ddata->workqueue == NULL) {
  950. dev_err(dev, "can't create workqueue\n");
  951. return -ENOMEM;
  952. }
  953. INIT_DELAYED_WORK(&ddata->ulps_work, dsicm_ulps_work);
  954. dsicm_hw_reset(ddata);
  955. if (ddata->use_dsi_backlight) {
  956. memset(&props, 0, sizeof(struct backlight_properties));
  957. props.max_brightness = 255;
  958. props.type = BACKLIGHT_RAW;
  959. bldev = backlight_device_register(dev_name(dev),
  960. dev, ddata, &dsicm_bl_ops, &props);
  961. if (IS_ERR(bldev)) {
  962. r = PTR_ERR(bldev);
  963. goto err_bl;
  964. }
  965. ddata->bldev = bldev;
  966. bldev->props.fb_blank = FB_BLANK_UNBLANK;
  967. bldev->props.power = FB_BLANK_UNBLANK;
  968. bldev->props.brightness = 255;
  969. dsicm_bl_update_status(bldev);
  970. }
  971. r = sysfs_create_group(&dev->kobj, &dsicm_attr_group);
  972. if (r) {
  973. dev_err(dev, "failed to create sysfs files\n");
  974. goto err_sysfs_create;
  975. }
  976. return 0;
  977. err_sysfs_create:
  978. backlight_device_unregister(bldev);
  979. err_bl:
  980. destroy_workqueue(ddata->workqueue);
  981. err_reg:
  982. return r;
  983. }
  984. static int __exit dsicm_remove(struct platform_device *pdev)
  985. {
  986. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  987. struct omap_dss_device *dssdev = &ddata->dssdev;
  988. struct backlight_device *bldev;
  989. dev_dbg(&pdev->dev, "remove\n");
  990. omapdss_unregister_display(dssdev);
  991. dsicm_disable(dssdev);
  992. dsicm_disconnect(dssdev);
  993. sysfs_remove_group(&pdev->dev.kobj, &dsicm_attr_group);
  994. bldev = ddata->bldev;
  995. if (bldev != NULL) {
  996. bldev->props.power = FB_BLANK_POWERDOWN;
  997. dsicm_bl_update_status(bldev);
  998. backlight_device_unregister(bldev);
  999. }
  1000. omap_dss_put_device(ddata->in);
  1001. dsicm_cancel_ulps_work(ddata);
  1002. destroy_workqueue(ddata->workqueue);
  1003. /* reset, to be sure that the panel is in a valid state */
  1004. dsicm_hw_reset(ddata);
  1005. return 0;
  1006. }
  1007. static const struct of_device_id dsicm_of_match[] = {
  1008. { .compatible = "omapdss,panel-dsi-cm", },
  1009. {},
  1010. };
  1011. MODULE_DEVICE_TABLE(of, dsicm_of_match);
  1012. static struct platform_driver dsicm_driver = {
  1013. .probe = dsicm_probe,
  1014. .remove = __exit_p(dsicm_remove),
  1015. .driver = {
  1016. .name = "panel-dsi-cm",
  1017. .of_match_table = dsicm_of_match,
  1018. .suppress_bind_attrs = true,
  1019. },
  1020. };
  1021. module_platform_driver(dsicm_driver);
  1022. MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
  1023. MODULE_DESCRIPTION("Generic DSI Command Mode Panel Driver");
  1024. MODULE_LICENSE("GPL");