panel-dsi-cm.c 29 KB

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