encoder-opa362.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * OPA362 analog video amplifier with output/power control
  3. *
  4. * Copyright (C) 2014 Golden Delicious Computers
  5. * Author: H. Nikolaus Schaller <hns@goldelico.com>
  6. *
  7. * based on encoder-tfp410
  8. *
  9. * Copyright (C) 2013 Texas Instruments
  10. * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License version 2 as published by
  14. * the Free Software Foundation.
  15. */
  16. #include <linux/gpio.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/of_gpio.h>
  21. #include <video/omapdss.h>
  22. struct panel_drv_data {
  23. struct omap_dss_device dssdev;
  24. struct omap_dss_device *in;
  25. struct gpio_desc *enable_gpio;
  26. struct omap_video_timings timings;
  27. };
  28. #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
  29. static int opa362_connect(struct omap_dss_device *dssdev,
  30. struct omap_dss_device *dst)
  31. {
  32. struct panel_drv_data *ddata = to_panel_data(dssdev);
  33. struct omap_dss_device *in = ddata->in;
  34. int r;
  35. dev_dbg(dssdev->dev, "connect\n");
  36. if (omapdss_device_is_connected(dssdev))
  37. return -EBUSY;
  38. r = in->ops.atv->connect(in, dssdev);
  39. if (r)
  40. return r;
  41. dst->src = dssdev;
  42. dssdev->dst = dst;
  43. return 0;
  44. }
  45. static void opa362_disconnect(struct omap_dss_device *dssdev,
  46. struct omap_dss_device *dst)
  47. {
  48. struct panel_drv_data *ddata = to_panel_data(dssdev);
  49. struct omap_dss_device *in = ddata->in;
  50. dev_dbg(dssdev->dev, "disconnect\n");
  51. WARN_ON(!omapdss_device_is_connected(dssdev));
  52. if (!omapdss_device_is_connected(dssdev))
  53. return;
  54. WARN_ON(dst != dssdev->dst);
  55. if (dst != dssdev->dst)
  56. return;
  57. dst->src = NULL;
  58. dssdev->dst = NULL;
  59. in->ops.atv->disconnect(in, &ddata->dssdev);
  60. }
  61. static int opa362_enable(struct omap_dss_device *dssdev)
  62. {
  63. struct panel_drv_data *ddata = to_panel_data(dssdev);
  64. struct omap_dss_device *in = ddata->in;
  65. int r;
  66. dev_dbg(dssdev->dev, "enable\n");
  67. if (!omapdss_device_is_connected(dssdev))
  68. return -ENODEV;
  69. if (omapdss_device_is_enabled(dssdev))
  70. return 0;
  71. in->ops.atv->set_timings(in, &ddata->timings);
  72. r = in->ops.atv->enable(in);
  73. if (r)
  74. return r;
  75. if (ddata->enable_gpio)
  76. gpiod_set_value_cansleep(ddata->enable_gpio, 1);
  77. dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
  78. return 0;
  79. }
  80. static void opa362_disable(struct omap_dss_device *dssdev)
  81. {
  82. struct panel_drv_data *ddata = to_panel_data(dssdev);
  83. struct omap_dss_device *in = ddata->in;
  84. dev_dbg(dssdev->dev, "disable\n");
  85. if (!omapdss_device_is_enabled(dssdev))
  86. return;
  87. if (ddata->enable_gpio)
  88. gpiod_set_value_cansleep(ddata->enable_gpio, 0);
  89. in->ops.atv->disable(in);
  90. dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
  91. }
  92. static void opa362_set_timings(struct omap_dss_device *dssdev,
  93. struct omap_video_timings *timings)
  94. {
  95. struct panel_drv_data *ddata = to_panel_data(dssdev);
  96. struct omap_dss_device *in = ddata->in;
  97. dev_dbg(dssdev->dev, "set_timings\n");
  98. ddata->timings = *timings;
  99. dssdev->panel.timings = *timings;
  100. in->ops.atv->set_timings(in, timings);
  101. }
  102. static void opa362_get_timings(struct omap_dss_device *dssdev,
  103. struct omap_video_timings *timings)
  104. {
  105. struct panel_drv_data *ddata = to_panel_data(dssdev);
  106. dev_dbg(dssdev->dev, "get_timings\n");
  107. *timings = ddata->timings;
  108. }
  109. static int opa362_check_timings(struct omap_dss_device *dssdev,
  110. struct omap_video_timings *timings)
  111. {
  112. struct panel_drv_data *ddata = to_panel_data(dssdev);
  113. struct omap_dss_device *in = ddata->in;
  114. dev_dbg(dssdev->dev, "check_timings\n");
  115. return in->ops.atv->check_timings(in, timings);
  116. }
  117. static void opa362_set_type(struct omap_dss_device *dssdev,
  118. enum omap_dss_venc_type type)
  119. {
  120. /* we can only drive a COMPOSITE output */
  121. WARN_ON(type != OMAP_DSS_VENC_TYPE_COMPOSITE);
  122. }
  123. static const struct omapdss_atv_ops opa362_atv_ops = {
  124. .connect = opa362_connect,
  125. .disconnect = opa362_disconnect,
  126. .enable = opa362_enable,
  127. .disable = opa362_disable,
  128. .check_timings = opa362_check_timings,
  129. .set_timings = opa362_set_timings,
  130. .get_timings = opa362_get_timings,
  131. .set_type = opa362_set_type,
  132. };
  133. static int opa362_probe(struct platform_device *pdev)
  134. {
  135. struct device_node *node = pdev->dev.of_node;
  136. struct panel_drv_data *ddata;
  137. struct omap_dss_device *dssdev, *in;
  138. struct gpio_desc *gpio;
  139. int r;
  140. dev_dbg(&pdev->dev, "probe\n");
  141. if (node == NULL) {
  142. dev_err(&pdev->dev, "Unable to find device tree\n");
  143. return -EINVAL;
  144. }
  145. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  146. if (!ddata)
  147. return -ENOMEM;
  148. platform_set_drvdata(pdev, ddata);
  149. gpio = devm_gpiod_get_optional(&pdev->dev, "enable", GPIOD_OUT_LOW);
  150. if (IS_ERR(gpio))
  151. return PTR_ERR(gpio);
  152. ddata->enable_gpio = gpio;
  153. in = omapdss_of_find_source_for_first_ep(node);
  154. if (IS_ERR(in)) {
  155. dev_err(&pdev->dev, "failed to find video source\n");
  156. return PTR_ERR(in);
  157. }
  158. ddata->in = in;
  159. dssdev = &ddata->dssdev;
  160. dssdev->ops.atv = &opa362_atv_ops;
  161. dssdev->dev = &pdev->dev;
  162. dssdev->type = OMAP_DISPLAY_TYPE_VENC;
  163. dssdev->output_type = OMAP_DISPLAY_TYPE_VENC;
  164. dssdev->owner = THIS_MODULE;
  165. r = omapdss_register_output(dssdev);
  166. if (r) {
  167. dev_err(&pdev->dev, "Failed to register output\n");
  168. goto err_reg;
  169. }
  170. return 0;
  171. err_reg:
  172. omap_dss_put_device(ddata->in);
  173. return r;
  174. }
  175. static int __exit opa362_remove(struct platform_device *pdev)
  176. {
  177. struct panel_drv_data *ddata = platform_get_drvdata(pdev);
  178. struct omap_dss_device *dssdev = &ddata->dssdev;
  179. struct omap_dss_device *in = ddata->in;
  180. omapdss_unregister_output(&ddata->dssdev);
  181. WARN_ON(omapdss_device_is_enabled(dssdev));
  182. if (omapdss_device_is_enabled(dssdev))
  183. opa362_disable(dssdev);
  184. WARN_ON(omapdss_device_is_connected(dssdev));
  185. if (omapdss_device_is_connected(dssdev))
  186. opa362_disconnect(dssdev, dssdev->dst);
  187. omap_dss_put_device(in);
  188. return 0;
  189. }
  190. static const struct of_device_id opa362_of_match[] = {
  191. { .compatible = "omapdss,ti,opa362", },
  192. {},
  193. };
  194. MODULE_DEVICE_TABLE(of, opa362_of_match);
  195. static struct platform_driver opa362_driver = {
  196. .probe = opa362_probe,
  197. .remove = __exit_p(opa362_remove),
  198. .driver = {
  199. .name = "amplifier-opa362",
  200. .of_match_table = opa362_of_match,
  201. .suppress_bind_attrs = true,
  202. },
  203. };
  204. module_platform_driver(opa362_driver);
  205. MODULE_AUTHOR("H. Nikolaus Schaller <hns@goldelico.com>");
  206. MODULE_DESCRIPTION("OPA362 analog video amplifier with output/power control");
  207. MODULE_LICENSE("GPL v2");