panel-tfp410.c 7.2 KB

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