tilcdc_tfp410.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (C) 2012 Texas Instruments
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/i2c.h>
  18. #include <linux/gpio.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/pinctrl/pinmux.h>
  21. #include <linux/pinctrl/consumer.h>
  22. #include <drm/drm_atomic_helper.h>
  23. #include "tilcdc_drv.h"
  24. #include "tilcdc_tfp410.h"
  25. struct tfp410_module {
  26. struct tilcdc_module base;
  27. struct i2c_adapter *i2c;
  28. int gpio;
  29. };
  30. #define to_tfp410_module(x) container_of(x, struct tfp410_module, base)
  31. static const struct tilcdc_panel_info dvi_info = {
  32. .ac_bias = 255,
  33. .ac_bias_intrpt = 0,
  34. .dma_burst_sz = 16,
  35. .bpp = 16,
  36. .fdd = 0x80,
  37. .tft_alt_mode = 0,
  38. .sync_edge = 0,
  39. .sync_ctrl = 1,
  40. .raster_order = 0,
  41. };
  42. /*
  43. * Encoder:
  44. */
  45. struct tfp410_encoder {
  46. struct drm_encoder base;
  47. struct tfp410_module *mod;
  48. int dpms;
  49. };
  50. #define to_tfp410_encoder(x) container_of(x, struct tfp410_encoder, base)
  51. static void tfp410_encoder_dpms(struct drm_encoder *encoder, int mode)
  52. {
  53. struct tfp410_encoder *tfp410_encoder = to_tfp410_encoder(encoder);
  54. if (tfp410_encoder->dpms == mode)
  55. return;
  56. if (mode == DRM_MODE_DPMS_ON) {
  57. DBG("Power on");
  58. gpio_direction_output(tfp410_encoder->mod->gpio, 1);
  59. } else {
  60. DBG("Power off");
  61. gpio_direction_output(tfp410_encoder->mod->gpio, 0);
  62. }
  63. tfp410_encoder->dpms = mode;
  64. }
  65. static void tfp410_encoder_prepare(struct drm_encoder *encoder)
  66. {
  67. tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  68. }
  69. static void tfp410_encoder_commit(struct drm_encoder *encoder)
  70. {
  71. tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
  72. }
  73. static void tfp410_encoder_mode_set(struct drm_encoder *encoder,
  74. struct drm_display_mode *mode,
  75. struct drm_display_mode *adjusted_mode)
  76. {
  77. /* nothing needed */
  78. }
  79. static const struct drm_encoder_funcs tfp410_encoder_funcs = {
  80. .destroy = drm_encoder_cleanup,
  81. };
  82. static const struct drm_encoder_helper_funcs tfp410_encoder_helper_funcs = {
  83. .dpms = tfp410_encoder_dpms,
  84. .prepare = tfp410_encoder_prepare,
  85. .commit = tfp410_encoder_commit,
  86. .mode_set = tfp410_encoder_mode_set,
  87. };
  88. static struct drm_encoder *tfp410_encoder_create(struct drm_device *dev,
  89. struct tfp410_module *mod)
  90. {
  91. struct tfp410_encoder *tfp410_encoder;
  92. struct drm_encoder *encoder;
  93. int ret;
  94. tfp410_encoder = devm_kzalloc(dev->dev, sizeof(*tfp410_encoder),
  95. GFP_KERNEL);
  96. if (!tfp410_encoder)
  97. return NULL;
  98. tfp410_encoder->dpms = DRM_MODE_DPMS_OFF;
  99. tfp410_encoder->mod = mod;
  100. encoder = &tfp410_encoder->base;
  101. encoder->possible_crtcs = 1;
  102. ret = drm_encoder_init(dev, encoder, &tfp410_encoder_funcs,
  103. DRM_MODE_ENCODER_TMDS, NULL);
  104. if (ret < 0)
  105. goto fail;
  106. drm_encoder_helper_add(encoder, &tfp410_encoder_helper_funcs);
  107. return encoder;
  108. fail:
  109. drm_encoder_cleanup(encoder);
  110. return NULL;
  111. }
  112. /*
  113. * Connector:
  114. */
  115. struct tfp410_connector {
  116. struct drm_connector base;
  117. struct drm_encoder *encoder; /* our connected encoder */
  118. struct tfp410_module *mod;
  119. };
  120. #define to_tfp410_connector(x) container_of(x, struct tfp410_connector, base)
  121. static void tfp410_connector_destroy(struct drm_connector *connector)
  122. {
  123. drm_connector_unregister(connector);
  124. drm_connector_cleanup(connector);
  125. }
  126. static enum drm_connector_status tfp410_connector_detect(
  127. struct drm_connector *connector,
  128. bool force)
  129. {
  130. struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
  131. if (drm_probe_ddc(tfp410_connector->mod->i2c))
  132. return connector_status_connected;
  133. return connector_status_unknown;
  134. }
  135. static int tfp410_connector_get_modes(struct drm_connector *connector)
  136. {
  137. struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
  138. struct edid *edid;
  139. int ret = 0;
  140. edid = drm_get_edid(connector, tfp410_connector->mod->i2c);
  141. drm_mode_connector_update_edid_property(connector, edid);
  142. if (edid) {
  143. ret = drm_add_edid_modes(connector, edid);
  144. kfree(edid);
  145. }
  146. return ret;
  147. }
  148. static int tfp410_connector_mode_valid(struct drm_connector *connector,
  149. struct drm_display_mode *mode)
  150. {
  151. struct tilcdc_drm_private *priv = connector->dev->dev_private;
  152. /* our only constraints are what the crtc can generate: */
  153. return tilcdc_crtc_mode_valid(priv->crtc, mode);
  154. }
  155. static struct drm_encoder *tfp410_connector_best_encoder(
  156. struct drm_connector *connector)
  157. {
  158. struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
  159. return tfp410_connector->encoder;
  160. }
  161. static const struct drm_connector_funcs tfp410_connector_funcs = {
  162. .destroy = tfp410_connector_destroy,
  163. .detect = tfp410_connector_detect,
  164. .fill_modes = drm_helper_probe_single_connector_modes,
  165. .reset = drm_atomic_helper_connector_reset,
  166. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  167. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  168. };
  169. static const struct drm_connector_helper_funcs tfp410_connector_helper_funcs = {
  170. .get_modes = tfp410_connector_get_modes,
  171. .mode_valid = tfp410_connector_mode_valid,
  172. .best_encoder = tfp410_connector_best_encoder,
  173. };
  174. static struct drm_connector *tfp410_connector_create(struct drm_device *dev,
  175. struct tfp410_module *mod, struct drm_encoder *encoder)
  176. {
  177. struct tfp410_connector *tfp410_connector;
  178. struct drm_connector *connector;
  179. int ret;
  180. tfp410_connector = devm_kzalloc(dev->dev, sizeof(*tfp410_connector),
  181. GFP_KERNEL);
  182. if (!tfp410_connector)
  183. return NULL;
  184. tfp410_connector->encoder = encoder;
  185. tfp410_connector->mod = mod;
  186. connector = &tfp410_connector->base;
  187. drm_connector_init(dev, connector, &tfp410_connector_funcs,
  188. DRM_MODE_CONNECTOR_DVID);
  189. drm_connector_helper_add(connector, &tfp410_connector_helper_funcs);
  190. connector->polled = DRM_CONNECTOR_POLL_CONNECT |
  191. DRM_CONNECTOR_POLL_DISCONNECT;
  192. connector->interlace_allowed = 0;
  193. connector->doublescan_allowed = 0;
  194. ret = drm_mode_connector_attach_encoder(connector, encoder);
  195. if (ret)
  196. goto fail;
  197. return connector;
  198. fail:
  199. tfp410_connector_destroy(connector);
  200. return NULL;
  201. }
  202. /*
  203. * Module:
  204. */
  205. static int tfp410_modeset_init(struct tilcdc_module *mod, struct drm_device *dev)
  206. {
  207. struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
  208. struct tilcdc_drm_private *priv = dev->dev_private;
  209. struct drm_encoder *encoder;
  210. struct drm_connector *connector;
  211. encoder = tfp410_encoder_create(dev, tfp410_mod);
  212. if (!encoder)
  213. return -ENOMEM;
  214. connector = tfp410_connector_create(dev, tfp410_mod, encoder);
  215. if (!connector)
  216. return -ENOMEM;
  217. priv->encoders[priv->num_encoders++] = encoder;
  218. priv->connectors[priv->num_connectors++] = connector;
  219. tilcdc_crtc_set_panel_info(priv->crtc, &dvi_info);
  220. return 0;
  221. }
  222. static const struct tilcdc_module_ops tfp410_module_ops = {
  223. .modeset_init = tfp410_modeset_init,
  224. };
  225. /*
  226. * Device:
  227. */
  228. static int tfp410_probe(struct platform_device *pdev)
  229. {
  230. struct device_node *node = pdev->dev.of_node;
  231. struct device_node *i2c_node;
  232. struct tfp410_module *tfp410_mod;
  233. struct tilcdc_module *mod;
  234. struct pinctrl *pinctrl;
  235. uint32_t i2c_phandle;
  236. int ret = -EINVAL;
  237. /* bail out early if no DT data: */
  238. if (!node) {
  239. dev_err(&pdev->dev, "device-tree data is missing\n");
  240. return -ENXIO;
  241. }
  242. tfp410_mod = devm_kzalloc(&pdev->dev, sizeof(*tfp410_mod), GFP_KERNEL);
  243. if (!tfp410_mod)
  244. return -ENOMEM;
  245. mod = &tfp410_mod->base;
  246. pdev->dev.platform_data = mod;
  247. tilcdc_module_init(mod, "tfp410", &tfp410_module_ops);
  248. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  249. if (IS_ERR(pinctrl))
  250. dev_warn(&pdev->dev, "pins are not configured\n");
  251. if (of_property_read_u32(node, "i2c", &i2c_phandle)) {
  252. dev_err(&pdev->dev, "could not get i2c bus phandle\n");
  253. goto fail;
  254. }
  255. i2c_node = of_find_node_by_phandle(i2c_phandle);
  256. if (!i2c_node) {
  257. dev_err(&pdev->dev, "could not get i2c bus node\n");
  258. goto fail;
  259. }
  260. tfp410_mod->i2c = of_find_i2c_adapter_by_node(i2c_node);
  261. if (!tfp410_mod->i2c) {
  262. dev_err(&pdev->dev, "could not get i2c\n");
  263. of_node_put(i2c_node);
  264. goto fail;
  265. }
  266. of_node_put(i2c_node);
  267. tfp410_mod->gpio = of_get_named_gpio_flags(node, "powerdn-gpio",
  268. 0, NULL);
  269. if (tfp410_mod->gpio < 0) {
  270. dev_warn(&pdev->dev, "No power down GPIO\n");
  271. } else {
  272. ret = gpio_request(tfp410_mod->gpio, "DVI_PDn");
  273. if (ret) {
  274. dev_err(&pdev->dev, "could not get DVI_PDn gpio\n");
  275. goto fail_adapter;
  276. }
  277. }
  278. return 0;
  279. fail_adapter:
  280. i2c_put_adapter(tfp410_mod->i2c);
  281. fail:
  282. tilcdc_module_cleanup(mod);
  283. return ret;
  284. }
  285. static int tfp410_remove(struct platform_device *pdev)
  286. {
  287. struct tilcdc_module *mod = dev_get_platdata(&pdev->dev);
  288. struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
  289. i2c_put_adapter(tfp410_mod->i2c);
  290. gpio_free(tfp410_mod->gpio);
  291. tilcdc_module_cleanup(mod);
  292. return 0;
  293. }
  294. static const struct of_device_id tfp410_of_match[] = {
  295. { .compatible = "ti,tilcdc,tfp410", },
  296. { },
  297. };
  298. struct platform_driver tfp410_driver = {
  299. .probe = tfp410_probe,
  300. .remove = tfp410_remove,
  301. .driver = {
  302. .owner = THIS_MODULE,
  303. .name = "tfp410",
  304. .of_match_table = tfp410_of_match,
  305. },
  306. };
  307. int __init tilcdc_tfp410_init(void)
  308. {
  309. return platform_driver_register(&tfp410_driver);
  310. }
  311. void __exit tilcdc_tfp410_fini(void)
  312. {
  313. platform_driver_unregister(&tfp410_driver);
  314. }