panel-tfp410.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. };
  37. struct panel_drv_data {
  38. struct omap_dss_device *dssdev;
  39. struct mutex lock;
  40. int pd_gpio;
  41. };
  42. static inline struct tfp410_platform_data
  43. *get_pdata(const struct omap_dss_device *dssdev)
  44. {
  45. return dssdev->data;
  46. }
  47. static int tfp410_power_on(struct omap_dss_device *dssdev)
  48. {
  49. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  50. int r;
  51. if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
  52. return 0;
  53. r = omapdss_dpi_display_enable(dssdev);
  54. if (r)
  55. goto err0;
  56. if (gpio_is_valid(ddata->pd_gpio))
  57. gpio_set_value(ddata->pd_gpio, 1);
  58. return 0;
  59. err0:
  60. return r;
  61. }
  62. static void tfp410_power_off(struct omap_dss_device *dssdev)
  63. {
  64. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  65. if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
  66. return;
  67. if (gpio_is_valid(ddata->pd_gpio))
  68. gpio_set_value(ddata->pd_gpio, 0);
  69. omapdss_dpi_display_disable(dssdev);
  70. }
  71. static int tfp410_probe(struct omap_dss_device *dssdev)
  72. {
  73. struct tfp410_platform_data *pdata = get_pdata(dssdev);
  74. struct panel_drv_data *ddata;
  75. int r;
  76. ddata = kzalloc(sizeof(*ddata), GFP_KERNEL);
  77. if (!ddata)
  78. return -ENOMEM;
  79. dssdev->panel.timings = tfp410_default_timings;
  80. dssdev->panel.config = OMAP_DSS_LCD_TFT;
  81. ddata->dssdev = dssdev;
  82. mutex_init(&ddata->lock);
  83. if (pdata)
  84. ddata->pd_gpio = pdata->power_down_gpio;
  85. else
  86. ddata->pd_gpio = -1;
  87. if (gpio_is_valid(ddata->pd_gpio)) {
  88. r = gpio_request_one(ddata->pd_gpio, GPIOF_OUT_INIT_LOW,
  89. "tfp410 pd");
  90. if (r) {
  91. dev_err(&dssdev->dev, "Failed to request PD GPIO %d\n",
  92. ddata->pd_gpio);
  93. ddata->pd_gpio = -1;
  94. }
  95. }
  96. dev_set_drvdata(&dssdev->dev, ddata);
  97. return 0;
  98. }
  99. static void __exit tfp410_remove(struct omap_dss_device *dssdev)
  100. {
  101. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  102. mutex_lock(&ddata->lock);
  103. if (gpio_is_valid(ddata->pd_gpio))
  104. gpio_free(ddata->pd_gpio);
  105. dev_set_drvdata(&dssdev->dev, NULL);
  106. mutex_unlock(&ddata->lock);
  107. kfree(ddata);
  108. }
  109. static int tfp410_enable(struct omap_dss_device *dssdev)
  110. {
  111. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  112. int r;
  113. mutex_lock(&ddata->lock);
  114. r = tfp410_power_on(dssdev);
  115. if (r == 0)
  116. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  117. mutex_unlock(&ddata->lock);
  118. return r;
  119. }
  120. static void tfp410_disable(struct omap_dss_device *dssdev)
  121. {
  122. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  123. mutex_lock(&ddata->lock);
  124. tfp410_power_off(dssdev);
  125. dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
  126. mutex_unlock(&ddata->lock);
  127. }
  128. static int tfp410_suspend(struct omap_dss_device *dssdev)
  129. {
  130. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  131. mutex_lock(&ddata->lock);
  132. tfp410_power_off(dssdev);
  133. dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
  134. mutex_unlock(&ddata->lock);
  135. return 0;
  136. }
  137. static int tfp410_resume(struct omap_dss_device *dssdev)
  138. {
  139. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  140. int r;
  141. mutex_lock(&ddata->lock);
  142. r = tfp410_power_on(dssdev);
  143. if (r == 0)
  144. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  145. mutex_unlock(&ddata->lock);
  146. return r;
  147. }
  148. static void tfp410_set_timings(struct omap_dss_device *dssdev,
  149. struct omap_video_timings *timings)
  150. {
  151. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  152. mutex_lock(&ddata->lock);
  153. dpi_set_timings(dssdev, timings);
  154. mutex_unlock(&ddata->lock);
  155. }
  156. static void tfp410_get_timings(struct omap_dss_device *dssdev,
  157. struct omap_video_timings *timings)
  158. {
  159. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  160. mutex_lock(&ddata->lock);
  161. *timings = dssdev->panel.timings;
  162. mutex_unlock(&ddata->lock);
  163. }
  164. static int tfp410_check_timings(struct omap_dss_device *dssdev,
  165. struct omap_video_timings *timings)
  166. {
  167. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  168. int r;
  169. mutex_lock(&ddata->lock);
  170. r = dpi_check_timings(dssdev, timings);
  171. mutex_unlock(&ddata->lock);
  172. return r;
  173. }
  174. static int tfp410_ddc_read(struct i2c_adapter *adapter,
  175. unsigned char *buf, u16 count, u8 offset)
  176. {
  177. int r, retries;
  178. for (retries = 3; retries > 0; retries--) {
  179. struct i2c_msg msgs[] = {
  180. {
  181. .addr = DDC_ADDR,
  182. .flags = 0,
  183. .len = 1,
  184. .buf = &offset,
  185. }, {
  186. .addr = DDC_ADDR,
  187. .flags = I2C_M_RD,
  188. .len = count,
  189. .buf = buf,
  190. }
  191. };
  192. r = i2c_transfer(adapter, msgs, 2);
  193. if (r == 2)
  194. return 0;
  195. if (r != -EAGAIN)
  196. break;
  197. }
  198. return r < 0 ? r : -EIO;
  199. }
  200. static int tfp410_read_edid(struct omap_dss_device *dssdev,
  201. u8 *edid, int len)
  202. {
  203. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  204. struct tfp410_platform_data *pdata = get_pdata(dssdev);
  205. struct i2c_adapter *adapter;
  206. int r, l, bytes_read;
  207. mutex_lock(&ddata->lock);
  208. if (pdata->i2c_bus_num == 0) {
  209. r = -ENODEV;
  210. goto err;
  211. }
  212. adapter = i2c_get_adapter(pdata->i2c_bus_num);
  213. if (!adapter) {
  214. dev_err(&dssdev->dev, "Failed to get I2C adapter, bus %d\n",
  215. pdata->i2c_bus_num);
  216. r = -EINVAL;
  217. goto err;
  218. }
  219. l = min(EDID_LENGTH, len);
  220. r = tfp410_ddc_read(adapter, edid, l, 0);
  221. if (r)
  222. goto err;
  223. bytes_read = l;
  224. /* if there are extensions, read second block */
  225. if (len > EDID_LENGTH && edid[0x7e] > 0) {
  226. l = min(EDID_LENGTH, len - EDID_LENGTH);
  227. r = tfp410_ddc_read(adapter, edid + EDID_LENGTH,
  228. l, EDID_LENGTH);
  229. if (r)
  230. goto err;
  231. bytes_read += l;
  232. }
  233. mutex_unlock(&ddata->lock);
  234. return bytes_read;
  235. err:
  236. mutex_unlock(&ddata->lock);
  237. return r;
  238. }
  239. static bool tfp410_detect(struct omap_dss_device *dssdev)
  240. {
  241. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  242. struct tfp410_platform_data *pdata = get_pdata(dssdev);
  243. struct i2c_adapter *adapter;
  244. unsigned char out;
  245. int r;
  246. mutex_lock(&ddata->lock);
  247. if (pdata->i2c_bus_num == 0)
  248. goto out;
  249. adapter = i2c_get_adapter(pdata->i2c_bus_num);
  250. if (!adapter)
  251. goto out;
  252. r = tfp410_ddc_read(adapter, &out, 1, 0);
  253. mutex_unlock(&ddata->lock);
  254. return r == 0;
  255. out:
  256. mutex_unlock(&ddata->lock);
  257. return true;
  258. }
  259. static struct omap_dss_driver tfp410_driver = {
  260. .probe = tfp410_probe,
  261. .remove = __exit_p(tfp410_remove),
  262. .enable = tfp410_enable,
  263. .disable = tfp410_disable,
  264. .suspend = tfp410_suspend,
  265. .resume = tfp410_resume,
  266. .set_timings = tfp410_set_timings,
  267. .get_timings = tfp410_get_timings,
  268. .check_timings = tfp410_check_timings,
  269. .read_edid = tfp410_read_edid,
  270. .detect = tfp410_detect,
  271. .driver = {
  272. .name = "tfp410",
  273. .owner = THIS_MODULE,
  274. },
  275. };
  276. static int __init tfp410_init(void)
  277. {
  278. return omap_dss_register_driver(&tfp410_driver);
  279. }
  280. static void __exit tfp410_exit(void)
  281. {
  282. omap_dss_unregister_driver(&tfp410_driver);
  283. }
  284. module_init(tfp410_init);
  285. module_exit(tfp410_exit);
  286. MODULE_LICENSE("GPL");