tilcdc_tfp410.c 9.9 KB

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