panel-tpo-td043mtea1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * TPO TD043MTEA1 Panel driver
  3. *
  4. * Author: Gražvydas Ignotas <notasas@gmail.com>
  5. * Converted to new DSS device model: Tomi Valkeinen <tomi.valkeinen@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/delay.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/err.h>
  18. #include <linux/slab.h>
  19. #include <linux/of_gpio.h>
  20. #include "../dss/omapdss.h"
  21. #define TPO_R02_MODE(x) ((x) & 7)
  22. #define TPO_R02_MODE_800x480 7
  23. #define TPO_R02_NCLK_RISING BIT(3)
  24. #define TPO_R02_HSYNC_HIGH BIT(4)
  25. #define TPO_R02_VSYNC_HIGH BIT(5)
  26. #define TPO_R03_NSTANDBY BIT(0)
  27. #define TPO_R03_EN_CP_CLK BIT(1)
  28. #define TPO_R03_EN_VGL_PUMP BIT(2)
  29. #define TPO_R03_EN_PWM BIT(3)
  30. #define TPO_R03_DRIVING_CAP_100 BIT(4)
  31. #define TPO_R03_EN_PRE_CHARGE BIT(6)
  32. #define TPO_R03_SOFTWARE_CTL BIT(7)
  33. #define TPO_R04_NFLIP_H BIT(0)
  34. #define TPO_R04_NFLIP_V BIT(1)
  35. #define TPO_R04_CP_CLK_FREQ_1H BIT(2)
  36. #define TPO_R04_VGL_FREQ_1H BIT(4)
  37. #define TPO_R03_VAL_NORMAL (TPO_R03_NSTANDBY | TPO_R03_EN_CP_CLK | \
  38. TPO_R03_EN_VGL_PUMP | TPO_R03_EN_PWM | \
  39. TPO_R03_DRIVING_CAP_100 | TPO_R03_EN_PRE_CHARGE | \
  40. TPO_R03_SOFTWARE_CTL)
  41. #define TPO_R03_VAL_STANDBY (TPO_R03_DRIVING_CAP_100 | \
  42. TPO_R03_EN_PRE_CHARGE | TPO_R03_SOFTWARE_CTL)
  43. static const u16 tpo_td043_def_gamma[12] = {
  44. 105, 315, 381, 431, 490, 537, 579, 686, 780, 837, 880, 1023
  45. };
  46. struct panel_drv_data {
  47. struct omap_dss_device dssdev;
  48. struct omap_dss_device *in;
  49. struct videomode vm;
  50. struct spi_device *spi;
  51. struct regulator *vcc_reg;
  52. int nreset_gpio;
  53. u16 gamma[12];
  54. u32 mode;
  55. u32 hmirror:1;
  56. u32 vmirror:1;
  57. u32 powered_on:1;
  58. u32 spi_suspended:1;
  59. u32 power_on_resume:1;
  60. };
  61. static const struct videomode tpo_td043_vm = {
  62. .hactive = 800,
  63. .vactive = 480,
  64. .pixelclock = 36000000,
  65. .hsync_len = 1,
  66. .hfront_porch = 68,
  67. .hback_porch = 214,
  68. .vsync_len = 1,
  69. .vfront_porch = 39,
  70. .vback_porch = 34,
  71. .flags = DISPLAY_FLAGS_HSYNC_LOW | DISPLAY_FLAGS_VSYNC_LOW |
  72. DISPLAY_FLAGS_DE_HIGH | DISPLAY_FLAGS_SYNC_POSEDGE |
  73. DISPLAY_FLAGS_PIXDATA_NEGEDGE,
  74. /*
  75. * Note: According to the panel documentation:
  76. * SYNC needs to be driven on the FALLING edge
  77. */
  78. };
  79. #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
  80. static int tpo_td043_write(struct spi_device *spi, u8 addr, u8 data)
  81. {
  82. struct spi_message m;
  83. struct spi_transfer xfer;
  84. u16 w;
  85. int r;
  86. spi_message_init(&m);
  87. memset(&xfer, 0, sizeof(xfer));
  88. w = ((u16)addr << 10) | (1 << 8) | data;
  89. xfer.tx_buf = &w;
  90. xfer.bits_per_word = 16;
  91. xfer.len = 2;
  92. spi_message_add_tail(&xfer, &m);
  93. r = spi_sync(spi, &m);
  94. if (r < 0)
  95. dev_warn(&spi->dev, "failed to write to LCD reg (%d)\n", r);
  96. return r;
  97. }
  98. static void tpo_td043_write_gamma(struct spi_device *spi, u16 gamma[12])
  99. {
  100. u8 i, val;
  101. /* gamma bits [9:8] */
  102. for (val = i = 0; i < 4; i++)
  103. val |= (gamma[i] & 0x300) >> ((i + 1) * 2);
  104. tpo_td043_write(spi, 0x11, val);
  105. for (val = i = 0; i < 4; i++)
  106. val |= (gamma[i+4] & 0x300) >> ((i + 1) * 2);
  107. tpo_td043_write(spi, 0x12, val);
  108. for (val = i = 0; i < 4; i++)
  109. val |= (gamma[i+8] & 0x300) >> ((i + 1) * 2);
  110. tpo_td043_write(spi, 0x13, val);
  111. /* gamma bits [7:0] */
  112. for (val = i = 0; i < 12; i++)
  113. tpo_td043_write(spi, 0x14 + i, gamma[i] & 0xff);
  114. }
  115. static int tpo_td043_write_mirror(struct spi_device *spi, bool h, bool v)
  116. {
  117. u8 reg4 = TPO_R04_NFLIP_H | TPO_R04_NFLIP_V |
  118. TPO_R04_CP_CLK_FREQ_1H | TPO_R04_VGL_FREQ_1H;
  119. if (h)
  120. reg4 &= ~TPO_R04_NFLIP_H;
  121. if (v)
  122. reg4 &= ~TPO_R04_NFLIP_V;
  123. return tpo_td043_write(spi, 4, reg4);
  124. }
  125. static int tpo_td043_set_hmirror(struct omap_dss_device *dssdev, bool enable)
  126. {
  127. struct panel_drv_data *ddata = dev_get_drvdata(dssdev->dev);
  128. ddata->hmirror = enable;
  129. return tpo_td043_write_mirror(ddata->spi, ddata->hmirror,
  130. ddata->vmirror);
  131. }
  132. static bool tpo_td043_get_hmirror(struct omap_dss_device *dssdev)
  133. {
  134. struct panel_drv_data *ddata = dev_get_drvdata(dssdev->dev);
  135. return ddata->hmirror;
  136. }
  137. static ssize_t tpo_td043_vmirror_show(struct device *dev,
  138. struct device_attribute *attr, char *buf)
  139. {
  140. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  141. return snprintf(buf, PAGE_SIZE, "%d\n", ddata->vmirror);
  142. }
  143. static ssize_t tpo_td043_vmirror_store(struct device *dev,
  144. struct device_attribute *attr, const char *buf, size_t count)
  145. {
  146. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  147. int val;
  148. int ret;
  149. ret = kstrtoint(buf, 0, &val);
  150. if (ret < 0)
  151. return ret;
  152. val = !!val;
  153. ret = tpo_td043_write_mirror(ddata->spi, ddata->hmirror, val);
  154. if (ret < 0)
  155. return ret;
  156. ddata->vmirror = val;
  157. return count;
  158. }
  159. static ssize_t tpo_td043_mode_show(struct device *dev,
  160. struct device_attribute *attr, char *buf)
  161. {
  162. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  163. return snprintf(buf, PAGE_SIZE, "%d\n", ddata->mode);
  164. }
  165. static ssize_t tpo_td043_mode_store(struct device *dev,
  166. struct device_attribute *attr, const char *buf, size_t count)
  167. {
  168. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  169. long val;
  170. int ret;
  171. ret = kstrtol(buf, 0, &val);
  172. if (ret != 0 || val & ~7)
  173. return -EINVAL;
  174. ddata->mode = val;
  175. val |= TPO_R02_NCLK_RISING;
  176. tpo_td043_write(ddata->spi, 2, val);
  177. return count;
  178. }
  179. static ssize_t tpo_td043_gamma_show(struct device *dev,
  180. struct device_attribute *attr, char *buf)
  181. {
  182. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  183. ssize_t len = 0;
  184. int ret;
  185. int i;
  186. for (i = 0; i < ARRAY_SIZE(ddata->gamma); i++) {
  187. ret = snprintf(buf + len, PAGE_SIZE - len, "%u ",
  188. ddata->gamma[i]);
  189. if (ret < 0)
  190. return ret;
  191. len += ret;
  192. }
  193. buf[len - 1] = '\n';
  194. return len;
  195. }
  196. static ssize_t tpo_td043_gamma_store(struct device *dev,
  197. struct device_attribute *attr, const char *buf, size_t count)
  198. {
  199. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  200. unsigned int g[12];
  201. int ret;
  202. int i;
  203. ret = sscanf(buf, "%u %u %u %u %u %u %u %u %u %u %u %u",
  204. &g[0], &g[1], &g[2], &g[3], &g[4], &g[5],
  205. &g[6], &g[7], &g[8], &g[9], &g[10], &g[11]);
  206. if (ret != 12)
  207. return -EINVAL;
  208. for (i = 0; i < 12; i++)
  209. ddata->gamma[i] = g[i];
  210. tpo_td043_write_gamma(ddata->spi, ddata->gamma);
  211. return count;
  212. }
  213. static DEVICE_ATTR(vmirror, S_IRUGO | S_IWUSR,
  214. tpo_td043_vmirror_show, tpo_td043_vmirror_store);
  215. static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
  216. tpo_td043_mode_show, tpo_td043_mode_store);
  217. static DEVICE_ATTR(gamma, S_IRUGO | S_IWUSR,
  218. tpo_td043_gamma_show, tpo_td043_gamma_store);
  219. static struct attribute *tpo_td043_attrs[] = {
  220. &dev_attr_vmirror.attr,
  221. &dev_attr_mode.attr,
  222. &dev_attr_gamma.attr,
  223. NULL,
  224. };
  225. static const struct attribute_group tpo_td043_attr_group = {
  226. .attrs = tpo_td043_attrs,
  227. };
  228. static int tpo_td043_power_on(struct panel_drv_data *ddata)
  229. {
  230. int r;
  231. if (ddata->powered_on)
  232. return 0;
  233. r = regulator_enable(ddata->vcc_reg);
  234. if (r != 0)
  235. return r;
  236. /* wait for panel to stabilize */
  237. msleep(160);
  238. if (gpio_is_valid(ddata->nreset_gpio))
  239. gpio_set_value(ddata->nreset_gpio, 1);
  240. tpo_td043_write(ddata->spi, 2,
  241. TPO_R02_MODE(ddata->mode) | TPO_R02_NCLK_RISING);
  242. tpo_td043_write(ddata->spi, 3, TPO_R03_VAL_NORMAL);
  243. tpo_td043_write(ddata->spi, 0x20, 0xf0);
  244. tpo_td043_write(ddata->spi, 0x21, 0xf0);
  245. tpo_td043_write_mirror(ddata->spi, ddata->hmirror,
  246. ddata->vmirror);
  247. tpo_td043_write_gamma(ddata->spi, ddata->gamma);
  248. ddata->powered_on = 1;
  249. return 0;
  250. }
  251. static void tpo_td043_power_off(struct panel_drv_data *ddata)
  252. {
  253. if (!ddata->powered_on)
  254. return;
  255. tpo_td043_write(ddata->spi, 3,
  256. TPO_R03_VAL_STANDBY | TPO_R03_EN_PWM);
  257. if (gpio_is_valid(ddata->nreset_gpio))
  258. gpio_set_value(ddata->nreset_gpio, 0);
  259. /* wait for at least 2 vsyncs before cutting off power */
  260. msleep(50);
  261. tpo_td043_write(ddata->spi, 3, TPO_R03_VAL_STANDBY);
  262. regulator_disable(ddata->vcc_reg);
  263. ddata->powered_on = 0;
  264. }
  265. static int tpo_td043_connect(struct omap_dss_device *dssdev)
  266. {
  267. struct panel_drv_data *ddata = to_panel_data(dssdev);
  268. struct omap_dss_device *in;
  269. int r;
  270. if (omapdss_device_is_connected(dssdev))
  271. return 0;
  272. in = omapdss_of_find_source_for_first_ep(dssdev->dev->of_node);
  273. if (IS_ERR(in)) {
  274. dev_err(dssdev->dev, "failed to find video source\n");
  275. return PTR_ERR(in);
  276. }
  277. r = in->ops.dpi->connect(in, dssdev);
  278. if (r) {
  279. omap_dss_put_device(in);
  280. return r;
  281. }
  282. ddata->in = in;
  283. return 0;
  284. }
  285. static void tpo_td043_disconnect(struct omap_dss_device *dssdev)
  286. {
  287. struct panel_drv_data *ddata = to_panel_data(dssdev);
  288. struct omap_dss_device *in = ddata->in;
  289. if (!omapdss_device_is_connected(dssdev))
  290. return;
  291. in->ops.dpi->disconnect(in, dssdev);
  292. omap_dss_put_device(in);
  293. ddata->in = NULL;
  294. }
  295. static int tpo_td043_enable(struct omap_dss_device *dssdev)
  296. {
  297. struct panel_drv_data *ddata = to_panel_data(dssdev);
  298. struct omap_dss_device *in = ddata->in;
  299. int r;
  300. if (!omapdss_device_is_connected(dssdev))
  301. return -ENODEV;
  302. if (omapdss_device_is_enabled(dssdev))
  303. return 0;
  304. in->ops.dpi->set_timings(in, &ddata->vm);
  305. r = in->ops.dpi->enable(in);
  306. if (r)
  307. return r;
  308. /*
  309. * If we are resuming from system suspend, SPI clocks might not be
  310. * enabled yet, so we'll program the LCD from SPI PM resume callback.
  311. */
  312. if (!ddata->spi_suspended) {
  313. r = tpo_td043_power_on(ddata);
  314. if (r) {
  315. in->ops.dpi->disable(in);
  316. return r;
  317. }
  318. }
  319. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  320. return 0;
  321. }
  322. static void tpo_td043_disable(struct omap_dss_device *dssdev)
  323. {
  324. struct panel_drv_data *ddata = to_panel_data(dssdev);
  325. struct omap_dss_device *in = ddata->in;
  326. if (!omapdss_device_is_enabled(dssdev))
  327. return;
  328. in->ops.dpi->disable(in);
  329. if (!ddata->spi_suspended)
  330. tpo_td043_power_off(ddata);
  331. dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
  332. }
  333. static void tpo_td043_set_timings(struct omap_dss_device *dssdev,
  334. struct videomode *vm)
  335. {
  336. struct panel_drv_data *ddata = to_panel_data(dssdev);
  337. struct omap_dss_device *in = ddata->in;
  338. ddata->vm = *vm;
  339. dssdev->panel.vm = *vm;
  340. in->ops.dpi->set_timings(in, vm);
  341. }
  342. static void tpo_td043_get_timings(struct omap_dss_device *dssdev,
  343. struct videomode *vm)
  344. {
  345. struct panel_drv_data *ddata = to_panel_data(dssdev);
  346. *vm = ddata->vm;
  347. }
  348. static int tpo_td043_check_timings(struct omap_dss_device *dssdev,
  349. struct videomode *vm)
  350. {
  351. struct panel_drv_data *ddata = to_panel_data(dssdev);
  352. struct omap_dss_device *in = ddata->in;
  353. return in->ops.dpi->check_timings(in, vm);
  354. }
  355. static struct omap_dss_driver tpo_td043_ops = {
  356. .connect = tpo_td043_connect,
  357. .disconnect = tpo_td043_disconnect,
  358. .enable = tpo_td043_enable,
  359. .disable = tpo_td043_disable,
  360. .set_timings = tpo_td043_set_timings,
  361. .get_timings = tpo_td043_get_timings,
  362. .check_timings = tpo_td043_check_timings,
  363. .set_mirror = tpo_td043_set_hmirror,
  364. .get_mirror = tpo_td043_get_hmirror,
  365. };
  366. static int tpo_td043_probe_of(struct spi_device *spi)
  367. {
  368. struct device_node *node = spi->dev.of_node;
  369. struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
  370. int gpio;
  371. gpio = of_get_named_gpio(node, "reset-gpios", 0);
  372. if (!gpio_is_valid(gpio)) {
  373. dev_err(&spi->dev, "failed to parse enable gpio\n");
  374. return gpio;
  375. }
  376. ddata->nreset_gpio = gpio;
  377. return 0;
  378. }
  379. static int tpo_td043_probe(struct spi_device *spi)
  380. {
  381. struct panel_drv_data *ddata;
  382. struct omap_dss_device *dssdev;
  383. int r;
  384. dev_dbg(&spi->dev, "%s\n", __func__);
  385. spi->bits_per_word = 16;
  386. spi->mode = SPI_MODE_0;
  387. r = spi_setup(spi);
  388. if (r < 0) {
  389. dev_err(&spi->dev, "spi_setup failed: %d\n", r);
  390. return r;
  391. }
  392. ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
  393. if (ddata == NULL)
  394. return -ENOMEM;
  395. dev_set_drvdata(&spi->dev, ddata);
  396. ddata->spi = spi;
  397. r = tpo_td043_probe_of(spi);
  398. if (r)
  399. return r;
  400. ddata->mode = TPO_R02_MODE_800x480;
  401. memcpy(ddata->gamma, tpo_td043_def_gamma, sizeof(ddata->gamma));
  402. ddata->vcc_reg = devm_regulator_get(&spi->dev, "vcc");
  403. if (IS_ERR(ddata->vcc_reg)) {
  404. dev_err(&spi->dev, "failed to get LCD VCC regulator\n");
  405. r = PTR_ERR(ddata->vcc_reg);
  406. goto err_regulator;
  407. }
  408. if (gpio_is_valid(ddata->nreset_gpio)) {
  409. r = devm_gpio_request_one(&spi->dev,
  410. ddata->nreset_gpio, GPIOF_OUT_INIT_LOW,
  411. "lcd reset");
  412. if (r < 0) {
  413. dev_err(&spi->dev, "couldn't request reset GPIO\n");
  414. goto err_gpio_req;
  415. }
  416. }
  417. r = sysfs_create_group(&spi->dev.kobj, &tpo_td043_attr_group);
  418. if (r) {
  419. dev_err(&spi->dev, "failed to create sysfs files\n");
  420. goto err_sysfs;
  421. }
  422. ddata->vm = tpo_td043_vm;
  423. dssdev = &ddata->dssdev;
  424. dssdev->dev = &spi->dev;
  425. dssdev->driver = &tpo_td043_ops;
  426. dssdev->type = OMAP_DISPLAY_TYPE_DPI;
  427. dssdev->owner = THIS_MODULE;
  428. dssdev->panel.vm = ddata->vm;
  429. r = omapdss_register_display(dssdev);
  430. if (r) {
  431. dev_err(&spi->dev, "Failed to register panel\n");
  432. goto err_reg;
  433. }
  434. return 0;
  435. err_reg:
  436. sysfs_remove_group(&spi->dev.kobj, &tpo_td043_attr_group);
  437. err_sysfs:
  438. err_gpio_req:
  439. err_regulator:
  440. return r;
  441. }
  442. static int tpo_td043_remove(struct spi_device *spi)
  443. {
  444. struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
  445. struct omap_dss_device *dssdev = &ddata->dssdev;
  446. dev_dbg(&ddata->spi->dev, "%s\n", __func__);
  447. omapdss_unregister_display(dssdev);
  448. tpo_td043_disable(dssdev);
  449. tpo_td043_disconnect(dssdev);
  450. sysfs_remove_group(&spi->dev.kobj, &tpo_td043_attr_group);
  451. return 0;
  452. }
  453. #ifdef CONFIG_PM_SLEEP
  454. static int tpo_td043_spi_suspend(struct device *dev)
  455. {
  456. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  457. dev_dbg(dev, "tpo_td043_spi_suspend, tpo %p\n", ddata);
  458. ddata->power_on_resume = ddata->powered_on;
  459. tpo_td043_power_off(ddata);
  460. ddata->spi_suspended = 1;
  461. return 0;
  462. }
  463. static int tpo_td043_spi_resume(struct device *dev)
  464. {
  465. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  466. int ret;
  467. dev_dbg(dev, "tpo_td043_spi_resume\n");
  468. if (ddata->power_on_resume) {
  469. ret = tpo_td043_power_on(ddata);
  470. if (ret)
  471. return ret;
  472. }
  473. ddata->spi_suspended = 0;
  474. return 0;
  475. }
  476. #endif
  477. static SIMPLE_DEV_PM_OPS(tpo_td043_spi_pm,
  478. tpo_td043_spi_suspend, tpo_td043_spi_resume);
  479. static const struct of_device_id tpo_td043_of_match[] = {
  480. { .compatible = "omapdss,tpo,td043mtea1", },
  481. {},
  482. };
  483. MODULE_DEVICE_TABLE(of, tpo_td043_of_match);
  484. static struct spi_driver tpo_td043_spi_driver = {
  485. .driver = {
  486. .name = "panel-tpo-td043mtea1",
  487. .pm = &tpo_td043_spi_pm,
  488. .of_match_table = tpo_td043_of_match,
  489. .suppress_bind_attrs = true,
  490. },
  491. .probe = tpo_td043_probe,
  492. .remove = tpo_td043_remove,
  493. };
  494. module_spi_driver(tpo_td043_spi_driver);
  495. MODULE_ALIAS("spi:tpo,td043mtea1");
  496. MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
  497. MODULE_DESCRIPTION("TPO TD043MTEA1 LCD Driver");
  498. MODULE_LICENSE("GPL");