panel-tfp410.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * TFP410 DPI-to-DVI chip
  3. *
  4. * Copyright (C) 2011 Texas Instruments Inc
  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. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <video/omapdss.h>
  22. #include <linux/i2c.h>
  23. #include <linux/gpio.h>
  24. #include <drm/drm_edid.h>
  25. #include <video/omap-panel-tfp410.h>
  26. static const struct omap_video_timings tfp410_default_timings = {
  27. .x_res = 640,
  28. .y_res = 480,
  29. .pixel_clock = 23500,
  30. .hfp = 48,
  31. .hsw = 32,
  32. .hbp = 80,
  33. .vfp = 3,
  34. .vsw = 4,
  35. .vbp = 7,
  36. .vsync_level = OMAPDSS_SIG_ACTIVE_HIGH,
  37. .hsync_level = OMAPDSS_SIG_ACTIVE_HIGH,
  38. .data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE,
  39. .de_level = OMAPDSS_SIG_ACTIVE_HIGH,
  40. .sync_pclk_edge = OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES,
  41. };
  42. struct panel_drv_data {
  43. struct omap_dss_device *dssdev;
  44. struct mutex lock;
  45. int pd_gpio;
  46. struct i2c_adapter *i2c_adapter;
  47. };
  48. static int tfp410_power_on(struct omap_dss_device *dssdev)
  49. {
  50. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  51. int r;
  52. if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
  53. return 0;
  54. omapdss_dpi_set_timings(dssdev, &dssdev->panel.timings);
  55. omapdss_dpi_set_data_lines(dssdev, dssdev->phy.dpi.data_lines);
  56. r = omapdss_dpi_display_enable(dssdev);
  57. if (r)
  58. goto err0;
  59. if (gpio_is_valid(ddata->pd_gpio))
  60. gpio_set_value_cansleep(ddata->pd_gpio, 1);
  61. return 0;
  62. err0:
  63. return r;
  64. }
  65. static void tfp410_power_off(struct omap_dss_device *dssdev)
  66. {
  67. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  68. if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
  69. return;
  70. if (gpio_is_valid(ddata->pd_gpio))
  71. gpio_set_value_cansleep(ddata->pd_gpio, 0);
  72. omapdss_dpi_display_disable(dssdev);
  73. }
  74. static int tfp410_probe(struct omap_dss_device *dssdev)
  75. {
  76. struct panel_drv_data *ddata;
  77. int r;
  78. int i2c_bus_num;
  79. ddata = devm_kzalloc(&dssdev->dev, sizeof(*ddata), GFP_KERNEL);
  80. if (!ddata)
  81. return -ENOMEM;
  82. dssdev->panel.timings = tfp410_default_timings;
  83. ddata->dssdev = dssdev;
  84. mutex_init(&ddata->lock);
  85. if (dssdev->data) {
  86. struct tfp410_platform_data *pdata = dssdev->data;
  87. ddata->pd_gpio = pdata->power_down_gpio;
  88. i2c_bus_num = pdata->i2c_bus_num;
  89. } else {
  90. ddata->pd_gpio = -1;
  91. i2c_bus_num = -1;
  92. }
  93. if (gpio_is_valid(ddata->pd_gpio)) {
  94. r = devm_gpio_request_one(&dssdev->dev, ddata->pd_gpio,
  95. GPIOF_OUT_INIT_LOW, "tfp410 pd");
  96. if (r) {
  97. dev_err(&dssdev->dev, "Failed to request PD GPIO %d\n",
  98. ddata->pd_gpio);
  99. return r;
  100. }
  101. }
  102. if (i2c_bus_num != -1) {
  103. struct i2c_adapter *adapter;
  104. adapter = i2c_get_adapter(i2c_bus_num);
  105. if (!adapter) {
  106. dev_err(&dssdev->dev, "Failed to get I2C adapter, bus %d\n",
  107. i2c_bus_num);
  108. return -EINVAL;
  109. }
  110. ddata->i2c_adapter = adapter;
  111. }
  112. dev_set_drvdata(&dssdev->dev, ddata);
  113. return 0;
  114. }
  115. static void __exit tfp410_remove(struct omap_dss_device *dssdev)
  116. {
  117. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  118. mutex_lock(&ddata->lock);
  119. if (ddata->i2c_adapter)
  120. i2c_put_adapter(ddata->i2c_adapter);
  121. dev_set_drvdata(&dssdev->dev, NULL);
  122. mutex_unlock(&ddata->lock);
  123. }
  124. static int tfp410_enable(struct omap_dss_device *dssdev)
  125. {
  126. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  127. int r;
  128. mutex_lock(&ddata->lock);
  129. r = tfp410_power_on(dssdev);
  130. if (r == 0)
  131. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  132. mutex_unlock(&ddata->lock);
  133. return r;
  134. }
  135. static void tfp410_disable(struct omap_dss_device *dssdev)
  136. {
  137. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  138. mutex_lock(&ddata->lock);
  139. tfp410_power_off(dssdev);
  140. dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
  141. mutex_unlock(&ddata->lock);
  142. }
  143. static int tfp410_suspend(struct omap_dss_device *dssdev)
  144. {
  145. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  146. mutex_lock(&ddata->lock);
  147. tfp410_power_off(dssdev);
  148. dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
  149. mutex_unlock(&ddata->lock);
  150. return 0;
  151. }
  152. static int tfp410_resume(struct omap_dss_device *dssdev)
  153. {
  154. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  155. int r;
  156. mutex_lock(&ddata->lock);
  157. r = tfp410_power_on(dssdev);
  158. if (r == 0)
  159. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  160. mutex_unlock(&ddata->lock);
  161. return r;
  162. }
  163. static void tfp410_set_timings(struct omap_dss_device *dssdev,
  164. struct omap_video_timings *timings)
  165. {
  166. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  167. mutex_lock(&ddata->lock);
  168. omapdss_dpi_set_timings(dssdev, timings);
  169. dssdev->panel.timings = *timings;
  170. mutex_unlock(&ddata->lock);
  171. }
  172. static void tfp410_get_timings(struct omap_dss_device *dssdev,
  173. struct omap_video_timings *timings)
  174. {
  175. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  176. mutex_lock(&ddata->lock);
  177. *timings = dssdev->panel.timings;
  178. mutex_unlock(&ddata->lock);
  179. }
  180. static int tfp410_check_timings(struct omap_dss_device *dssdev,
  181. struct omap_video_timings *timings)
  182. {
  183. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  184. int r;
  185. mutex_lock(&ddata->lock);
  186. r = dpi_check_timings(dssdev, timings);
  187. mutex_unlock(&ddata->lock);
  188. return r;
  189. }
  190. static int tfp410_ddc_read(struct i2c_adapter *adapter,
  191. unsigned char *buf, u16 count, u8 offset)
  192. {
  193. int r, retries;
  194. for (retries = 3; retries > 0; retries--) {
  195. struct i2c_msg msgs[] = {
  196. {
  197. .addr = DDC_ADDR,
  198. .flags = 0,
  199. .len = 1,
  200. .buf = &offset,
  201. }, {
  202. .addr = DDC_ADDR,
  203. .flags = I2C_M_RD,
  204. .len = count,
  205. .buf = buf,
  206. }
  207. };
  208. r = i2c_transfer(adapter, msgs, 2);
  209. if (r == 2)
  210. return 0;
  211. if (r != -EAGAIN)
  212. break;
  213. }
  214. return r < 0 ? r : -EIO;
  215. }
  216. static int tfp410_read_edid(struct omap_dss_device *dssdev,
  217. u8 *edid, int len)
  218. {
  219. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  220. int r, l, bytes_read;
  221. mutex_lock(&ddata->lock);
  222. if (!ddata->i2c_adapter) {
  223. r = -ENODEV;
  224. goto err;
  225. }
  226. l = min(EDID_LENGTH, len);
  227. r = tfp410_ddc_read(ddata->i2c_adapter, edid, l, 0);
  228. if (r)
  229. goto err;
  230. bytes_read = l;
  231. /* if there are extensions, read second block */
  232. if (len > EDID_LENGTH && edid[0x7e] > 0) {
  233. l = min(EDID_LENGTH, len - EDID_LENGTH);
  234. r = tfp410_ddc_read(ddata->i2c_adapter, edid + EDID_LENGTH,
  235. l, EDID_LENGTH);
  236. if (r)
  237. goto err;
  238. bytes_read += l;
  239. }
  240. mutex_unlock(&ddata->lock);
  241. return bytes_read;
  242. err:
  243. mutex_unlock(&ddata->lock);
  244. return r;
  245. }
  246. static bool tfp410_detect(struct omap_dss_device *dssdev)
  247. {
  248. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  249. unsigned char out;
  250. int r;
  251. mutex_lock(&ddata->lock);
  252. if (!ddata->i2c_adapter)
  253. goto out;
  254. r = tfp410_ddc_read(ddata->i2c_adapter, &out, 1, 0);
  255. mutex_unlock(&ddata->lock);
  256. return r == 0;
  257. out:
  258. mutex_unlock(&ddata->lock);
  259. return true;
  260. }
  261. static struct omap_dss_driver tfp410_driver = {
  262. .probe = tfp410_probe,
  263. .remove = __exit_p(tfp410_remove),
  264. .enable = tfp410_enable,
  265. .disable = tfp410_disable,
  266. .suspend = tfp410_suspend,
  267. .resume = tfp410_resume,
  268. .set_timings = tfp410_set_timings,
  269. .get_timings = tfp410_get_timings,
  270. .check_timings = tfp410_check_timings,
  271. .read_edid = tfp410_read_edid,
  272. .detect = tfp410_detect,
  273. .driver = {
  274. .name = "tfp410",
  275. .owner = THIS_MODULE,
  276. },
  277. };
  278. static int __init tfp410_init(void)
  279. {
  280. return omap_dss_register_driver(&tfp410_driver);
  281. }
  282. static void __exit tfp410_exit(void)
  283. {
  284. omap_dss_unregister_driver(&tfp410_driver);
  285. }
  286. module_init(tfp410_init);
  287. module_exit(tfp410_exit);
  288. MODULE_LICENSE("GPL");