panel-dsi-cm.c 28 KB

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