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