panel-tfp410.c 7.8 KB

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