panel-tpo-td043mtea1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/module.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/slab.h>
  18. #include <linux/spi/spi.h>
  19. #include "../dss/omapdss.h"
  20. #define TPO_R02_MODE(x) ((x) & 7)
  21. #define TPO_R02_MODE_800x480 7
  22. #define TPO_R02_NCLK_RISING BIT(3)
  23. #define TPO_R02_HSYNC_HIGH BIT(4)
  24. #define TPO_R02_VSYNC_HIGH BIT(5)
  25. #define TPO_R03_NSTANDBY BIT(0)
  26. #define TPO_R03_EN_CP_CLK BIT(1)
  27. #define TPO_R03_EN_VGL_PUMP BIT(2)
  28. #define TPO_R03_EN_PWM BIT(3)
  29. #define TPO_R03_DRIVING_CAP_100 BIT(4)
  30. #define TPO_R03_EN_PRE_CHARGE BIT(6)
  31. #define TPO_R03_SOFTWARE_CTL BIT(7)
  32. #define TPO_R04_NFLIP_H BIT(0)
  33. #define TPO_R04_NFLIP_V BIT(1)
  34. #define TPO_R04_CP_CLK_FREQ_1H BIT(2)
  35. #define TPO_R04_VGL_FREQ_1H BIT(4)
  36. #define TPO_R03_VAL_NORMAL (TPO_R03_NSTANDBY | TPO_R03_EN_CP_CLK | \
  37. TPO_R03_EN_VGL_PUMP | TPO_R03_EN_PWM | \
  38. TPO_R03_DRIVING_CAP_100 | TPO_R03_EN_PRE_CHARGE | \
  39. TPO_R03_SOFTWARE_CTL)
  40. #define TPO_R03_VAL_STANDBY (TPO_R03_DRIVING_CAP_100 | \
  41. TPO_R03_EN_PRE_CHARGE | TPO_R03_SOFTWARE_CTL)
  42. static const u16 tpo_td043_def_gamma[12] = {
  43. 105, 315, 381, 431, 490, 537, 579, 686, 780, 837, 880, 1023
  44. };
  45. struct panel_drv_data {
  46. struct omap_dss_device dssdev;
  47. struct videomode vm;
  48. struct spi_device *spi;
  49. struct regulator *vcc_reg;
  50. struct gpio_desc *reset_gpio;
  51. u16 gamma[12];
  52. u32 mode;
  53. u32 vmirror:1;
  54. u32 powered_on:1;
  55. u32 spi_suspended:1;
  56. u32 power_on_resume:1;
  57. };
  58. static const struct videomode tpo_td043_vm = {
  59. .hactive = 800,
  60. .vactive = 480,
  61. .pixelclock = 36000000,
  62. .hsync_len = 1,
  63. .hfront_porch = 68,
  64. .hback_porch = 214,
  65. .vsync_len = 1,
  66. .vfront_porch = 39,
  67. .vback_porch = 34,
  68. .flags = DISPLAY_FLAGS_HSYNC_LOW | DISPLAY_FLAGS_VSYNC_LOW,
  69. };
  70. #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
  71. static int tpo_td043_write(struct spi_device *spi, u8 addr, u8 data)
  72. {
  73. struct spi_message m;
  74. struct spi_transfer xfer;
  75. u16 w;
  76. int r;
  77. spi_message_init(&m);
  78. memset(&xfer, 0, sizeof(xfer));
  79. w = ((u16)addr << 10) | (1 << 8) | data;
  80. xfer.tx_buf = &w;
  81. xfer.bits_per_word = 16;
  82. xfer.len = 2;
  83. spi_message_add_tail(&xfer, &m);
  84. r = spi_sync(spi, &m);
  85. if (r < 0)
  86. dev_warn(&spi->dev, "failed to write to LCD reg (%d)\n", r);
  87. return r;
  88. }
  89. static void tpo_td043_write_gamma(struct spi_device *spi, u16 gamma[12])
  90. {
  91. u8 i, val;
  92. /* gamma bits [9:8] */
  93. for (val = i = 0; i < 4; i++)
  94. val |= (gamma[i] & 0x300) >> ((i + 1) * 2);
  95. tpo_td043_write(spi, 0x11, val);
  96. for (val = i = 0; i < 4; i++)
  97. val |= (gamma[i+4] & 0x300) >> ((i + 1) * 2);
  98. tpo_td043_write(spi, 0x12, val);
  99. for (val = i = 0; i < 4; i++)
  100. val |= (gamma[i+8] & 0x300) >> ((i + 1) * 2);
  101. tpo_td043_write(spi, 0x13, val);
  102. /* gamma bits [7:0] */
  103. for (val = i = 0; i < 12; i++)
  104. tpo_td043_write(spi, 0x14 + i, gamma[i] & 0xff);
  105. }
  106. static int tpo_td043_write_mirror(struct spi_device *spi, bool h, bool v)
  107. {
  108. u8 reg4 = TPO_R04_NFLIP_H | TPO_R04_NFLIP_V |
  109. TPO_R04_CP_CLK_FREQ_1H | TPO_R04_VGL_FREQ_1H;
  110. if (h)
  111. reg4 &= ~TPO_R04_NFLIP_H;
  112. if (v)
  113. reg4 &= ~TPO_R04_NFLIP_V;
  114. return tpo_td043_write(spi, 4, reg4);
  115. }
  116. static ssize_t tpo_td043_vmirror_show(struct device *dev,
  117. struct device_attribute *attr, char *buf)
  118. {
  119. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  120. return snprintf(buf, PAGE_SIZE, "%d\n", ddata->vmirror);
  121. }
  122. static ssize_t tpo_td043_vmirror_store(struct device *dev,
  123. struct device_attribute *attr, const char *buf, size_t count)
  124. {
  125. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  126. int val;
  127. int ret;
  128. ret = kstrtoint(buf, 0, &val);
  129. if (ret < 0)
  130. return ret;
  131. val = !!val;
  132. ret = tpo_td043_write_mirror(ddata->spi, false, val);
  133. if (ret < 0)
  134. return ret;
  135. ddata->vmirror = val;
  136. return count;
  137. }
  138. static ssize_t tpo_td043_mode_show(struct device *dev,
  139. struct device_attribute *attr, char *buf)
  140. {
  141. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  142. return snprintf(buf, PAGE_SIZE, "%d\n", ddata->mode);
  143. }
  144. static ssize_t tpo_td043_mode_store(struct device *dev,
  145. struct device_attribute *attr, const char *buf, size_t count)
  146. {
  147. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  148. long val;
  149. int ret;
  150. ret = kstrtol(buf, 0, &val);
  151. if (ret != 0 || val & ~7)
  152. return -EINVAL;
  153. ddata->mode = val;
  154. val |= TPO_R02_NCLK_RISING;
  155. tpo_td043_write(ddata->spi, 2, val);
  156. return count;
  157. }
  158. static ssize_t tpo_td043_gamma_show(struct device *dev,
  159. struct device_attribute *attr, char *buf)
  160. {
  161. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  162. ssize_t len = 0;
  163. int ret;
  164. int i;
  165. for (i = 0; i < ARRAY_SIZE(ddata->gamma); i++) {
  166. ret = snprintf(buf + len, PAGE_SIZE - len, "%u ",
  167. ddata->gamma[i]);
  168. if (ret < 0)
  169. return ret;
  170. len += ret;
  171. }
  172. buf[len - 1] = '\n';
  173. return len;
  174. }
  175. static ssize_t tpo_td043_gamma_store(struct device *dev,
  176. struct device_attribute *attr, const char *buf, size_t count)
  177. {
  178. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  179. unsigned int g[12];
  180. int ret;
  181. int i;
  182. ret = sscanf(buf, "%u %u %u %u %u %u %u %u %u %u %u %u",
  183. &g[0], &g[1], &g[2], &g[3], &g[4], &g[5],
  184. &g[6], &g[7], &g[8], &g[9], &g[10], &g[11]);
  185. if (ret != 12)
  186. return -EINVAL;
  187. for (i = 0; i < 12; i++)
  188. ddata->gamma[i] = g[i];
  189. tpo_td043_write_gamma(ddata->spi, ddata->gamma);
  190. return count;
  191. }
  192. static DEVICE_ATTR(vmirror, S_IRUGO | S_IWUSR,
  193. tpo_td043_vmirror_show, tpo_td043_vmirror_store);
  194. static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
  195. tpo_td043_mode_show, tpo_td043_mode_store);
  196. static DEVICE_ATTR(gamma, S_IRUGO | S_IWUSR,
  197. tpo_td043_gamma_show, tpo_td043_gamma_store);
  198. static struct attribute *tpo_td043_attrs[] = {
  199. &dev_attr_vmirror.attr,
  200. &dev_attr_mode.attr,
  201. &dev_attr_gamma.attr,
  202. NULL,
  203. };
  204. static const struct attribute_group tpo_td043_attr_group = {
  205. .attrs = tpo_td043_attrs,
  206. };
  207. static int tpo_td043_power_on(struct panel_drv_data *ddata)
  208. {
  209. int r;
  210. if (ddata->powered_on)
  211. return 0;
  212. r = regulator_enable(ddata->vcc_reg);
  213. if (r != 0)
  214. return r;
  215. /* wait for panel to stabilize */
  216. msleep(160);
  217. gpiod_set_value(ddata->reset_gpio, 0);
  218. tpo_td043_write(ddata->spi, 2,
  219. TPO_R02_MODE(ddata->mode) | TPO_R02_NCLK_RISING);
  220. tpo_td043_write(ddata->spi, 3, TPO_R03_VAL_NORMAL);
  221. tpo_td043_write(ddata->spi, 0x20, 0xf0);
  222. tpo_td043_write(ddata->spi, 0x21, 0xf0);
  223. tpo_td043_write_mirror(ddata->spi, false, ddata->vmirror);
  224. tpo_td043_write_gamma(ddata->spi, ddata->gamma);
  225. ddata->powered_on = 1;
  226. return 0;
  227. }
  228. static void tpo_td043_power_off(struct panel_drv_data *ddata)
  229. {
  230. if (!ddata->powered_on)
  231. return;
  232. tpo_td043_write(ddata->spi, 3,
  233. TPO_R03_VAL_STANDBY | TPO_R03_EN_PWM);
  234. gpiod_set_value(ddata->reset_gpio, 1);
  235. /* wait for at least 2 vsyncs before cutting off power */
  236. msleep(50);
  237. tpo_td043_write(ddata->spi, 3, TPO_R03_VAL_STANDBY);
  238. regulator_disable(ddata->vcc_reg);
  239. ddata->powered_on = 0;
  240. }
  241. static int tpo_td043_connect(struct omap_dss_device *src,
  242. struct omap_dss_device *dst)
  243. {
  244. return 0;
  245. }
  246. static void tpo_td043_disconnect(struct omap_dss_device *src,
  247. struct omap_dss_device *dst)
  248. {
  249. }
  250. static int tpo_td043_enable(struct omap_dss_device *dssdev)
  251. {
  252. struct panel_drv_data *ddata = to_panel_data(dssdev);
  253. struct omap_dss_device *src = dssdev->src;
  254. int r;
  255. if (!omapdss_device_is_connected(dssdev))
  256. return -ENODEV;
  257. if (omapdss_device_is_enabled(dssdev))
  258. return 0;
  259. r = src->ops->enable(src);
  260. if (r)
  261. return r;
  262. /*
  263. * If we are resuming from system suspend, SPI clocks might not be
  264. * enabled yet, so we'll program the LCD from SPI PM resume callback.
  265. */
  266. if (!ddata->spi_suspended) {
  267. r = tpo_td043_power_on(ddata);
  268. if (r) {
  269. src->ops->disable(src);
  270. return r;
  271. }
  272. }
  273. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  274. return 0;
  275. }
  276. static void tpo_td043_disable(struct omap_dss_device *dssdev)
  277. {
  278. struct panel_drv_data *ddata = to_panel_data(dssdev);
  279. struct omap_dss_device *src = dssdev->src;
  280. if (!omapdss_device_is_enabled(dssdev))
  281. return;
  282. src->ops->disable(src);
  283. if (!ddata->spi_suspended)
  284. tpo_td043_power_off(ddata);
  285. dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
  286. }
  287. static void tpo_td043_get_timings(struct omap_dss_device *dssdev,
  288. struct videomode *vm)
  289. {
  290. struct panel_drv_data *ddata = to_panel_data(dssdev);
  291. *vm = ddata->vm;
  292. }
  293. static const struct omap_dss_device_ops tpo_td043_ops = {
  294. .connect = tpo_td043_connect,
  295. .disconnect = tpo_td043_disconnect,
  296. .enable = tpo_td043_enable,
  297. .disable = tpo_td043_disable,
  298. .get_timings = tpo_td043_get_timings,
  299. };
  300. static int tpo_td043_probe(struct spi_device *spi)
  301. {
  302. struct panel_drv_data *ddata;
  303. struct omap_dss_device *dssdev;
  304. struct gpio_desc *gpio;
  305. int r;
  306. dev_dbg(&spi->dev, "%s\n", __func__);
  307. spi->bits_per_word = 16;
  308. spi->mode = SPI_MODE_0;
  309. r = spi_setup(spi);
  310. if (r < 0) {
  311. dev_err(&spi->dev, "spi_setup failed: %d\n", r);
  312. return r;
  313. }
  314. ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
  315. if (ddata == NULL)
  316. return -ENOMEM;
  317. dev_set_drvdata(&spi->dev, ddata);
  318. ddata->spi = spi;
  319. ddata->mode = TPO_R02_MODE_800x480;
  320. memcpy(ddata->gamma, tpo_td043_def_gamma, sizeof(ddata->gamma));
  321. ddata->vcc_reg = devm_regulator_get(&spi->dev, "vcc");
  322. if (IS_ERR(ddata->vcc_reg)) {
  323. dev_err(&spi->dev, "failed to get LCD VCC regulator\n");
  324. return PTR_ERR(ddata->vcc_reg);
  325. }
  326. gpio = devm_gpiod_get(&spi->dev, "reset", GPIOD_OUT_HIGH);
  327. if (IS_ERR(gpio)) {
  328. dev_err(&spi->dev, "failed to get reset gpio\n");
  329. return PTR_ERR(gpio);
  330. }
  331. ddata->reset_gpio = gpio;
  332. r = sysfs_create_group(&spi->dev.kobj, &tpo_td043_attr_group);
  333. if (r) {
  334. dev_err(&spi->dev, "failed to create sysfs files\n");
  335. return r;
  336. }
  337. ddata->vm = tpo_td043_vm;
  338. dssdev = &ddata->dssdev;
  339. dssdev->dev = &spi->dev;
  340. dssdev->ops = &tpo_td043_ops;
  341. dssdev->type = OMAP_DISPLAY_TYPE_DPI;
  342. dssdev->owner = THIS_MODULE;
  343. dssdev->of_ports = BIT(0);
  344. /*
  345. * Note: According to the panel documentation:
  346. * SYNC needs to be driven on the FALLING edge
  347. */
  348. dssdev->bus_flags = DRM_BUS_FLAG_DE_HIGH | DRM_BUS_FLAG_SYNC_POSEDGE
  349. | DRM_BUS_FLAG_PIXDATA_NEGEDGE;
  350. omapdss_display_init(dssdev);
  351. omapdss_device_register(dssdev);
  352. return 0;
  353. }
  354. static int tpo_td043_remove(struct spi_device *spi)
  355. {
  356. struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
  357. struct omap_dss_device *dssdev = &ddata->dssdev;
  358. dev_dbg(&ddata->spi->dev, "%s\n", __func__);
  359. omapdss_device_unregister(dssdev);
  360. tpo_td043_disable(dssdev);
  361. sysfs_remove_group(&spi->dev.kobj, &tpo_td043_attr_group);
  362. return 0;
  363. }
  364. #ifdef CONFIG_PM_SLEEP
  365. static int tpo_td043_spi_suspend(struct device *dev)
  366. {
  367. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  368. dev_dbg(dev, "tpo_td043_spi_suspend, tpo %p\n", ddata);
  369. ddata->power_on_resume = ddata->powered_on;
  370. tpo_td043_power_off(ddata);
  371. ddata->spi_suspended = 1;
  372. return 0;
  373. }
  374. static int tpo_td043_spi_resume(struct device *dev)
  375. {
  376. struct panel_drv_data *ddata = dev_get_drvdata(dev);
  377. int ret;
  378. dev_dbg(dev, "tpo_td043_spi_resume\n");
  379. if (ddata->power_on_resume) {
  380. ret = tpo_td043_power_on(ddata);
  381. if (ret)
  382. return ret;
  383. }
  384. ddata->spi_suspended = 0;
  385. return 0;
  386. }
  387. #endif
  388. static SIMPLE_DEV_PM_OPS(tpo_td043_spi_pm,
  389. tpo_td043_spi_suspend, tpo_td043_spi_resume);
  390. static const struct of_device_id tpo_td043_of_match[] = {
  391. { .compatible = "omapdss,tpo,td043mtea1", },
  392. {},
  393. };
  394. MODULE_DEVICE_TABLE(of, tpo_td043_of_match);
  395. static struct spi_driver tpo_td043_spi_driver = {
  396. .driver = {
  397. .name = "panel-tpo-td043mtea1",
  398. .pm = &tpo_td043_spi_pm,
  399. .of_match_table = tpo_td043_of_match,
  400. .suppress_bind_attrs = true,
  401. },
  402. .probe = tpo_td043_probe,
  403. .remove = tpo_td043_remove,
  404. };
  405. module_spi_driver(tpo_td043_spi_driver);
  406. MODULE_ALIAS("spi:tpo,td043mtea1");
  407. MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
  408. MODULE_DESCRIPTION("TPO TD043MTEA1 LCD Driver");
  409. MODULE_LICENSE("GPL");