panel-dsi-cm.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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_of(struct platform_device *pdev)
  857. {
  858. struct device_node *node = pdev->dev.of_node;
  859. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  860. struct omap_dss_device *in;
  861. int gpio;
  862. gpio = of_get_named_gpio(node, "reset-gpios", 0);
  863. if (!gpio_is_valid(gpio)) {
  864. dev_err(&pdev->dev, "failed to parse reset gpio\n");
  865. return gpio;
  866. }
  867. ddata->reset_gpio = gpio;
  868. gpio = of_get_named_gpio(node, "te-gpios", 0);
  869. if (gpio_is_valid(gpio) || gpio == -ENOENT) {
  870. ddata->ext_te_gpio = gpio;
  871. } else {
  872. dev_err(&pdev->dev, "failed to parse TE gpio\n");
  873. return gpio;
  874. }
  875. in = omapdss_of_find_source_for_first_ep(node);
  876. if (IS_ERR(in)) {
  877. dev_err(&pdev->dev, "failed to find video source\n");
  878. return PTR_ERR(in);
  879. }
  880. ddata->in = in;
  881. /* TODO: ulps, backlight */
  882. return 0;
  883. }
  884. static int dsicm_probe(struct platform_device *pdev)
  885. {
  886. struct backlight_properties props;
  887. struct panel_drv_data *ddata;
  888. struct backlight_device *bldev = NULL;
  889. struct device *dev = &pdev->dev;
  890. struct omap_dss_device *dssdev;
  891. int r;
  892. dev_dbg(dev, "probe\n");
  893. ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
  894. if (!ddata)
  895. return -ENOMEM;
  896. platform_set_drvdata(pdev, ddata);
  897. ddata->pdev = pdev;
  898. if (!pdev->dev.of_node)
  899. return -ENODEV;
  900. r = dsicm_probe_of(pdev);
  901. if (r)
  902. return r;
  903. ddata->timings.x_res = 864;
  904. ddata->timings.y_res = 480;
  905. ddata->timings.pixelclock = 864 * 480 * 60;
  906. dssdev = &ddata->dssdev;
  907. dssdev->dev = dev;
  908. dssdev->driver = &dsicm_ops;
  909. dssdev->panel.timings = ddata->timings;
  910. dssdev->type = OMAP_DISPLAY_TYPE_DSI;
  911. dssdev->owner = THIS_MODULE;
  912. dssdev->panel.dsi_pix_fmt = OMAP_DSS_DSI_FMT_RGB888;
  913. dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE |
  914. OMAP_DSS_DISPLAY_CAP_TEAR_ELIM;
  915. r = omapdss_register_display(dssdev);
  916. if (r) {
  917. dev_err(dev, "Failed to register panel\n");
  918. goto err_reg;
  919. }
  920. mutex_init(&ddata->lock);
  921. atomic_set(&ddata->do_update, 0);
  922. if (gpio_is_valid(ddata->reset_gpio)) {
  923. r = devm_gpio_request_one(dev, ddata->reset_gpio,
  924. GPIOF_OUT_INIT_LOW, "taal rst");
  925. if (r) {
  926. dev_err(dev, "failed to request reset gpio\n");
  927. return r;
  928. }
  929. }
  930. if (gpio_is_valid(ddata->ext_te_gpio)) {
  931. r = devm_gpio_request_one(dev, ddata->ext_te_gpio,
  932. GPIOF_IN, "taal irq");
  933. if (r) {
  934. dev_err(dev, "GPIO request failed\n");
  935. return r;
  936. }
  937. r = devm_request_irq(dev, gpio_to_irq(ddata->ext_te_gpio),
  938. dsicm_te_isr,
  939. IRQF_TRIGGER_RISING,
  940. "taal vsync", ddata);
  941. if (r) {
  942. dev_err(dev, "IRQ request failed\n");
  943. return r;
  944. }
  945. INIT_DEFERRABLE_WORK(&ddata->te_timeout_work,
  946. dsicm_te_timeout_work_callback);
  947. dev_dbg(dev, "Using GPIO TE\n");
  948. }
  949. ddata->workqueue = create_singlethread_workqueue("dsicm_wq");
  950. if (ddata->workqueue == NULL) {
  951. dev_err(dev, "can't create workqueue\n");
  952. return -ENOMEM;
  953. }
  954. INIT_DELAYED_WORK(&ddata->ulps_work, dsicm_ulps_work);
  955. dsicm_hw_reset(ddata);
  956. if (ddata->use_dsi_backlight) {
  957. memset(&props, 0, sizeof(struct backlight_properties));
  958. props.max_brightness = 255;
  959. props.type = BACKLIGHT_RAW;
  960. bldev = backlight_device_register(dev_name(dev),
  961. dev, ddata, &dsicm_bl_ops, &props);
  962. if (IS_ERR(bldev)) {
  963. r = PTR_ERR(bldev);
  964. goto err_bl;
  965. }
  966. ddata->bldev = bldev;
  967. bldev->props.fb_blank = FB_BLANK_UNBLANK;
  968. bldev->props.power = FB_BLANK_UNBLANK;
  969. bldev->props.brightness = 255;
  970. dsicm_bl_update_status(bldev);
  971. }
  972. r = sysfs_create_group(&dev->kobj, &dsicm_attr_group);
  973. if (r) {
  974. dev_err(dev, "failed to create sysfs files\n");
  975. goto err_sysfs_create;
  976. }
  977. return 0;
  978. err_sysfs_create:
  979. if (bldev != NULL)
  980. backlight_device_unregister(bldev);
  981. err_bl:
  982. destroy_workqueue(ddata->workqueue);
  983. err_reg:
  984. return r;
  985. }
  986. static int __exit dsicm_remove(struct platform_device *pdev)
  987. {
  988. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  989. struct omap_dss_device *dssdev = &ddata->dssdev;
  990. struct backlight_device *bldev;
  991. dev_dbg(&pdev->dev, "remove\n");
  992. omapdss_unregister_display(dssdev);
  993. dsicm_disable(dssdev);
  994. dsicm_disconnect(dssdev);
  995. sysfs_remove_group(&pdev->dev.kobj, &dsicm_attr_group);
  996. bldev = ddata->bldev;
  997. if (bldev != NULL) {
  998. bldev->props.power = FB_BLANK_POWERDOWN;
  999. dsicm_bl_update_status(bldev);
  1000. backlight_device_unregister(bldev);
  1001. }
  1002. omap_dss_put_device(ddata->in);
  1003. dsicm_cancel_ulps_work(ddata);
  1004. destroy_workqueue(ddata->workqueue);
  1005. /* reset, to be sure that the panel is in a valid state */
  1006. dsicm_hw_reset(ddata);
  1007. return 0;
  1008. }
  1009. static const struct of_device_id dsicm_of_match[] = {
  1010. { .compatible = "omapdss,panel-dsi-cm", },
  1011. {},
  1012. };
  1013. MODULE_DEVICE_TABLE(of, dsicm_of_match);
  1014. static struct platform_driver dsicm_driver = {
  1015. .probe = dsicm_probe,
  1016. .remove = __exit_p(dsicm_remove),
  1017. .driver = {
  1018. .name = "panel-dsi-cm",
  1019. .of_match_table = dsicm_of_match,
  1020. .suppress_bind_attrs = true,
  1021. },
  1022. };
  1023. module_platform_driver(dsicm_driver);
  1024. MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
  1025. MODULE_DESCRIPTION("Generic DSI Command Mode Panel Driver");
  1026. MODULE_LICENSE("GPL");