panel-dsi-cm.c 27 KB

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