panel-tfp410.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 = gpio_request_one(ddata->pd_gpio, GPIOF_OUT_INIT_LOW,
  95. "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. r = -EINVAL;
  109. goto err_i2c;
  110. }
  111. ddata->i2c_adapter = adapter;
  112. }
  113. dev_set_drvdata(&dssdev->dev, ddata);
  114. return 0;
  115. err_i2c:
  116. if (gpio_is_valid(ddata->pd_gpio))
  117. gpio_free(ddata->pd_gpio);
  118. return r;
  119. }
  120. static void __exit tfp410_remove(struct omap_dss_device *dssdev)
  121. {
  122. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  123. mutex_lock(&ddata->lock);
  124. if (ddata->i2c_adapter)
  125. i2c_put_adapter(ddata->i2c_adapter);
  126. if (gpio_is_valid(ddata->pd_gpio))
  127. gpio_free(ddata->pd_gpio);
  128. dev_set_drvdata(&dssdev->dev, NULL);
  129. mutex_unlock(&ddata->lock);
  130. }
  131. static int tfp410_enable(struct omap_dss_device *dssdev)
  132. {
  133. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  134. int r;
  135. mutex_lock(&ddata->lock);
  136. r = tfp410_power_on(dssdev);
  137. if (r == 0)
  138. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  139. mutex_unlock(&ddata->lock);
  140. return r;
  141. }
  142. static void tfp410_disable(struct omap_dss_device *dssdev)
  143. {
  144. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  145. mutex_lock(&ddata->lock);
  146. tfp410_power_off(dssdev);
  147. dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
  148. mutex_unlock(&ddata->lock);
  149. }
  150. static int tfp410_suspend(struct omap_dss_device *dssdev)
  151. {
  152. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  153. mutex_lock(&ddata->lock);
  154. tfp410_power_off(dssdev);
  155. dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
  156. mutex_unlock(&ddata->lock);
  157. return 0;
  158. }
  159. static int tfp410_resume(struct omap_dss_device *dssdev)
  160. {
  161. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  162. int r;
  163. mutex_lock(&ddata->lock);
  164. r = tfp410_power_on(dssdev);
  165. if (r == 0)
  166. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  167. mutex_unlock(&ddata->lock);
  168. return r;
  169. }
  170. static void tfp410_set_timings(struct omap_dss_device *dssdev,
  171. struct omap_video_timings *timings)
  172. {
  173. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  174. mutex_lock(&ddata->lock);
  175. omapdss_dpi_set_timings(dssdev, timings);
  176. dssdev->panel.timings = *timings;
  177. mutex_unlock(&ddata->lock);
  178. }
  179. static void tfp410_get_timings(struct omap_dss_device *dssdev,
  180. struct omap_video_timings *timings)
  181. {
  182. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  183. mutex_lock(&ddata->lock);
  184. *timings = dssdev->panel.timings;
  185. mutex_unlock(&ddata->lock);
  186. }
  187. static int tfp410_check_timings(struct omap_dss_device *dssdev,
  188. struct omap_video_timings *timings)
  189. {
  190. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  191. int r;
  192. mutex_lock(&ddata->lock);
  193. r = dpi_check_timings(dssdev, timings);
  194. mutex_unlock(&ddata->lock);
  195. return r;
  196. }
  197. static int tfp410_ddc_read(struct i2c_adapter *adapter,
  198. unsigned char *buf, u16 count, u8 offset)
  199. {
  200. int r, retries;
  201. for (retries = 3; retries > 0; retries--) {
  202. struct i2c_msg msgs[] = {
  203. {
  204. .addr = DDC_ADDR,
  205. .flags = 0,
  206. .len = 1,
  207. .buf = &offset,
  208. }, {
  209. .addr = DDC_ADDR,
  210. .flags = I2C_M_RD,
  211. .len = count,
  212. .buf = buf,
  213. }
  214. };
  215. r = i2c_transfer(adapter, msgs, 2);
  216. if (r == 2)
  217. return 0;
  218. if (r != -EAGAIN)
  219. break;
  220. }
  221. return r < 0 ? r : -EIO;
  222. }
  223. static int tfp410_read_edid(struct omap_dss_device *dssdev,
  224. u8 *edid, int len)
  225. {
  226. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  227. int r, l, bytes_read;
  228. mutex_lock(&ddata->lock);
  229. if (!ddata->i2c_adapter) {
  230. r = -ENODEV;
  231. goto err;
  232. }
  233. l = min(EDID_LENGTH, len);
  234. r = tfp410_ddc_read(ddata->i2c_adapter, edid, l, 0);
  235. if (r)
  236. goto err;
  237. bytes_read = l;
  238. /* if there are extensions, read second block */
  239. if (len > EDID_LENGTH && edid[0x7e] > 0) {
  240. l = min(EDID_LENGTH, len - EDID_LENGTH);
  241. r = tfp410_ddc_read(ddata->i2c_adapter, edid + EDID_LENGTH,
  242. l, EDID_LENGTH);
  243. if (r)
  244. goto err;
  245. bytes_read += l;
  246. }
  247. mutex_unlock(&ddata->lock);
  248. return bytes_read;
  249. err:
  250. mutex_unlock(&ddata->lock);
  251. return r;
  252. }
  253. static bool tfp410_detect(struct omap_dss_device *dssdev)
  254. {
  255. struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
  256. unsigned char out;
  257. int r;
  258. mutex_lock(&ddata->lock);
  259. if (!ddata->i2c_adapter)
  260. goto out;
  261. r = tfp410_ddc_read(ddata->i2c_adapter, &out, 1, 0);
  262. mutex_unlock(&ddata->lock);
  263. return r == 0;
  264. out:
  265. mutex_unlock(&ddata->lock);
  266. return true;
  267. }
  268. static struct omap_dss_driver tfp410_driver = {
  269. .probe = tfp410_probe,
  270. .remove = __exit_p(tfp410_remove),
  271. .enable = tfp410_enable,
  272. .disable = tfp410_disable,
  273. .suspend = tfp410_suspend,
  274. .resume = tfp410_resume,
  275. .set_timings = tfp410_set_timings,
  276. .get_timings = tfp410_get_timings,
  277. .check_timings = tfp410_check_timings,
  278. .read_edid = tfp410_read_edid,
  279. .detect = tfp410_detect,
  280. .driver = {
  281. .name = "tfp410",
  282. .owner = THIS_MODULE,
  283. },
  284. };
  285. static int __init tfp410_init(void)
  286. {
  287. return omap_dss_register_driver(&tfp410_driver);
  288. }
  289. static void __exit tfp410_exit(void)
  290. {
  291. omap_dss_unregister_driver(&tfp410_driver);
  292. }
  293. module_init(tfp410_init);
  294. module_exit(tfp410_exit);
  295. MODULE_LICENSE("GPL");