panel-tfp410.c 7.8 KB

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