panel-tfp410.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. struct i2c_adapter *i2c_adapter;
  42. };
  43. static int tfp410_power_on(struct omap_dss_device *dssdev)
  44. {
  45. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  46. int r;
  47. if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
  48. return 0;
  49. r = omapdss_dpi_display_enable(dssdev);
  50. if (r)
  51. goto err0;
  52. if (gpio_is_valid(ddata->pd_gpio))
  53. gpio_set_value_cansleep(ddata->pd_gpio, 1);
  54. return 0;
  55. err0:
  56. return r;
  57. }
  58. static void tfp410_power_off(struct omap_dss_device *dssdev)
  59. {
  60. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  61. if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
  62. return;
  63. if (gpio_is_valid(ddata->pd_gpio))
  64. gpio_set_value_cansleep(ddata->pd_gpio, 0);
  65. omapdss_dpi_display_disable(dssdev);
  66. }
  67. static int tfp410_probe(struct omap_dss_device *dssdev)
  68. {
  69. struct panel_drv_data *ddata;
  70. int r;
  71. int i2c_bus_num;
  72. ddata = devm_kzalloc(&dssdev->dev, sizeof(*ddata), GFP_KERNEL);
  73. if (!ddata)
  74. return -ENOMEM;
  75. dssdev->panel.timings = tfp410_default_timings;
  76. dssdev->panel.config = OMAP_DSS_LCD_TFT;
  77. ddata->dssdev = dssdev;
  78. mutex_init(&ddata->lock);
  79. if (dssdev->data) {
  80. struct tfp410_platform_data *pdata = dssdev->data;
  81. ddata->pd_gpio = pdata->power_down_gpio;
  82. i2c_bus_num = pdata->i2c_bus_num;
  83. } else {
  84. ddata->pd_gpio = -1;
  85. i2c_bus_num = -1;
  86. }
  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. return r;
  94. }
  95. }
  96. if (i2c_bus_num != -1) {
  97. struct i2c_adapter *adapter;
  98. adapter = i2c_get_adapter(i2c_bus_num);
  99. if (!adapter) {
  100. dev_err(&dssdev->dev, "Failed to get I2C adapter, bus %d\n",
  101. i2c_bus_num);
  102. r = -EINVAL;
  103. goto err_i2c;
  104. }
  105. ddata->i2c_adapter = adapter;
  106. }
  107. dev_set_drvdata(&dssdev->dev, ddata);
  108. return 0;
  109. err_i2c:
  110. if (gpio_is_valid(ddata->pd_gpio))
  111. gpio_free(ddata->pd_gpio);
  112. return r;
  113. }
  114. static void __exit tfp410_remove(struct omap_dss_device *dssdev)
  115. {
  116. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  117. mutex_lock(&ddata->lock);
  118. if (ddata->i2c_adapter)
  119. i2c_put_adapter(ddata->i2c_adapter);
  120. if (gpio_is_valid(ddata->pd_gpio))
  121. gpio_free(ddata->pd_gpio);
  122. dev_set_drvdata(&dssdev->dev, NULL);
  123. mutex_unlock(&ddata->lock);
  124. }
  125. static int tfp410_enable(struct omap_dss_device *dssdev)
  126. {
  127. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  128. int r;
  129. mutex_lock(&ddata->lock);
  130. r = tfp410_power_on(dssdev);
  131. if (r == 0)
  132. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  133. mutex_unlock(&ddata->lock);
  134. return r;
  135. }
  136. static void tfp410_disable(struct omap_dss_device *dssdev)
  137. {
  138. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  139. mutex_lock(&ddata->lock);
  140. tfp410_power_off(dssdev);
  141. dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
  142. mutex_unlock(&ddata->lock);
  143. }
  144. static int tfp410_suspend(struct omap_dss_device *dssdev)
  145. {
  146. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  147. mutex_lock(&ddata->lock);
  148. tfp410_power_off(dssdev);
  149. dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
  150. mutex_unlock(&ddata->lock);
  151. return 0;
  152. }
  153. static int tfp410_resume(struct omap_dss_device *dssdev)
  154. {
  155. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  156. int r;
  157. mutex_lock(&ddata->lock);
  158. r = tfp410_power_on(dssdev);
  159. if (r == 0)
  160. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  161. mutex_unlock(&ddata->lock);
  162. return r;
  163. }
  164. static void tfp410_set_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. mutex_lock(&ddata->lock);
  169. dpi_set_timings(dssdev, 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");