tilcdc_tfp410.c 10 KB

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