ti-tfp410.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 Texas Instruments
  4. * Author: Jyri Sarha <jsarha@ti.com>
  5. */
  6. #include <linux/gpio/consumer.h>
  7. #include <linux/i2c.h>
  8. #include <linux/module.h>
  9. #include <linux/of_graph.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/workqueue.h>
  12. #include <drm/drm_atomic_helper.h>
  13. #include <drm/drm_bridge.h>
  14. #include <drm/drm_crtc.h>
  15. #include <drm/drm_print.h>
  16. #include <drm/drm_probe_helper.h>
  17. #define HOTPLUG_DEBOUNCE_MS 1100
  18. struct tfp410 {
  19. struct drm_bridge bridge;
  20. struct drm_connector connector;
  21. u32 bus_format;
  22. struct delayed_work hpd_work;
  23. struct gpio_desc *powerdown;
  24. struct drm_bridge_timings timings;
  25. struct drm_bridge *next_bridge;
  26. struct device *dev;
  27. };
  28. static inline struct tfp410 *
  29. drm_bridge_to_tfp410(struct drm_bridge *bridge)
  30. {
  31. return container_of(bridge, struct tfp410, bridge);
  32. }
  33. static inline struct tfp410 *
  34. drm_connector_to_tfp410(struct drm_connector *connector)
  35. {
  36. return container_of(connector, struct tfp410, connector);
  37. }
  38. static int tfp410_get_modes(struct drm_connector *connector)
  39. {
  40. struct tfp410 *dvi = drm_connector_to_tfp410(connector);
  41. struct edid *edid;
  42. int ret;
  43. if (dvi->next_bridge->ops & DRM_BRIDGE_OP_EDID) {
  44. edid = drm_bridge_get_edid(dvi->next_bridge, connector);
  45. if (!edid)
  46. DRM_INFO("EDID read failed. Fallback to standard modes\n");
  47. } else {
  48. edid = NULL;
  49. }
  50. if (!edid) {
  51. /*
  52. * No EDID, fallback on the XGA standard modes and prefer a mode
  53. * pretty much anything can handle.
  54. */
  55. ret = drm_add_modes_noedid(connector, 1920, 1200);
  56. drm_set_preferred_mode(connector, 1024, 768);
  57. return ret;
  58. }
  59. drm_connector_update_edid_property(connector, edid);
  60. ret = drm_add_edid_modes(connector, edid);
  61. kfree(edid);
  62. return ret;
  63. }
  64. static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
  65. .get_modes = tfp410_get_modes,
  66. };
  67. static enum drm_connector_status
  68. tfp410_connector_detect(struct drm_connector *connector, bool force)
  69. {
  70. struct tfp410 *dvi = drm_connector_to_tfp410(connector);
  71. return drm_bridge_detect(dvi->next_bridge);
  72. }
  73. static const struct drm_connector_funcs tfp410_con_funcs = {
  74. .detect = tfp410_connector_detect,
  75. .fill_modes = drm_helper_probe_single_connector_modes,
  76. .destroy = drm_connector_cleanup,
  77. .reset = drm_atomic_helper_connector_reset,
  78. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  79. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  80. };
  81. static void tfp410_hpd_work_func(struct work_struct *work)
  82. {
  83. struct tfp410 *dvi;
  84. dvi = container_of(work, struct tfp410, hpd_work.work);
  85. if (dvi->bridge.dev)
  86. drm_helper_hpd_irq_event(dvi->bridge.dev);
  87. }
  88. static void tfp410_hpd_callback(void *arg, enum drm_connector_status status)
  89. {
  90. struct tfp410 *dvi = arg;
  91. mod_delayed_work(system_wq, &dvi->hpd_work,
  92. msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
  93. }
  94. static int tfp410_attach(struct drm_bridge *bridge,
  95. enum drm_bridge_attach_flags flags)
  96. {
  97. struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
  98. int ret;
  99. ret = drm_bridge_attach(bridge->encoder, dvi->next_bridge, bridge,
  100. DRM_BRIDGE_ATTACH_NO_CONNECTOR);
  101. if (ret < 0)
  102. return ret;
  103. if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
  104. return 0;
  105. if (!bridge->encoder) {
  106. dev_err(dvi->dev, "Missing encoder\n");
  107. return -ENODEV;
  108. }
  109. if (dvi->next_bridge->ops & DRM_BRIDGE_OP_DETECT)
  110. dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
  111. else
  112. dvi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
  113. if (dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
  114. INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
  115. drm_bridge_hpd_enable(dvi->next_bridge, tfp410_hpd_callback,
  116. dvi);
  117. }
  118. drm_connector_helper_add(&dvi->connector,
  119. &tfp410_con_helper_funcs);
  120. ret = drm_connector_init_with_ddc(bridge->dev, &dvi->connector,
  121. &tfp410_con_funcs,
  122. dvi->next_bridge->type,
  123. dvi->next_bridge->ddc);
  124. if (ret) {
  125. dev_err(dvi->dev, "drm_connector_init_with_ddc() failed: %d\n",
  126. ret);
  127. return ret;
  128. }
  129. drm_display_info_set_bus_formats(&dvi->connector.display_info,
  130. &dvi->bus_format, 1);
  131. drm_connector_attach_encoder(&dvi->connector, bridge->encoder);
  132. return 0;
  133. }
  134. static void tfp410_detach(struct drm_bridge *bridge)
  135. {
  136. struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
  137. if (dvi->connector.dev && dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
  138. drm_bridge_hpd_disable(dvi->next_bridge);
  139. cancel_delayed_work_sync(&dvi->hpd_work);
  140. }
  141. }
  142. static void tfp410_enable(struct drm_bridge *bridge)
  143. {
  144. struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
  145. gpiod_set_value_cansleep(dvi->powerdown, 0);
  146. }
  147. static void tfp410_disable(struct drm_bridge *bridge)
  148. {
  149. struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
  150. gpiod_set_value_cansleep(dvi->powerdown, 1);
  151. }
  152. static enum drm_mode_status tfp410_mode_valid(struct drm_bridge *bridge,
  153. const struct drm_display_info *info,
  154. const struct drm_display_mode *mode)
  155. {
  156. if (mode->clock < 25000)
  157. return MODE_CLOCK_LOW;
  158. if (mode->clock > 165000)
  159. return MODE_CLOCK_HIGH;
  160. return MODE_OK;
  161. }
  162. static const struct drm_bridge_funcs tfp410_bridge_funcs = {
  163. .attach = tfp410_attach,
  164. .detach = tfp410_detach,
  165. .enable = tfp410_enable,
  166. .disable = tfp410_disable,
  167. .mode_valid = tfp410_mode_valid,
  168. };
  169. static const struct drm_bridge_timings tfp410_default_timings = {
  170. .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
  171. | DRM_BUS_FLAG_DE_HIGH,
  172. .setup_time_ps = 1200,
  173. .hold_time_ps = 1300,
  174. };
  175. static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c)
  176. {
  177. struct drm_bridge_timings *timings = &dvi->timings;
  178. struct device_node *ep;
  179. u32 pclk_sample = 0;
  180. u32 bus_width = 24;
  181. u32 deskew = 0;
  182. /* Start with defaults. */
  183. *timings = tfp410_default_timings;
  184. if (i2c)
  185. /*
  186. * In I2C mode timings are configured through the I2C interface.
  187. * As the driver doesn't support I2C configuration yet, we just
  188. * go with the defaults (BSEL=1, DSEL=1, DKEN=0, EDGE=1).
  189. */
  190. return 0;
  191. /*
  192. * In non-I2C mode, timings are configured through the BSEL, DSEL, DKEN
  193. * and EDGE pins. They are specified in DT through endpoint properties
  194. * and vendor-specific properties.
  195. */
  196. ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0);
  197. if (!ep)
  198. return -EINVAL;
  199. /* Get the sampling edge from the endpoint. */
  200. of_property_read_u32(ep, "pclk-sample", &pclk_sample);
  201. of_property_read_u32(ep, "bus-width", &bus_width);
  202. of_node_put(ep);
  203. timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH;
  204. switch (pclk_sample) {
  205. case 0:
  206. timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
  207. | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE;
  208. break;
  209. case 1:
  210. timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
  211. | DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
  212. break;
  213. default:
  214. return -EINVAL;
  215. }
  216. switch (bus_width) {
  217. case 12:
  218. dvi->bus_format = MEDIA_BUS_FMT_RGB888_2X12_LE;
  219. break;
  220. case 24:
  221. dvi->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  222. break;
  223. default:
  224. return -EINVAL;
  225. }
  226. /* Get the setup and hold time from vendor-specific properties. */
  227. of_property_read_u32(dvi->dev->of_node, "ti,deskew", &deskew);
  228. if (deskew > 7)
  229. return -EINVAL;
  230. timings->setup_time_ps = 1200 - 350 * ((s32)deskew - 4);
  231. timings->hold_time_ps = max(0, 1300 + 350 * ((s32)deskew - 4));
  232. return 0;
  233. }
  234. static int tfp410_init(struct device *dev, bool i2c)
  235. {
  236. struct device_node *node;
  237. struct tfp410 *dvi;
  238. int ret;
  239. if (!dev->of_node) {
  240. dev_err(dev, "device-tree data is missing\n");
  241. return -ENXIO;
  242. }
  243. dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
  244. if (!dvi)
  245. return -ENOMEM;
  246. dvi->dev = dev;
  247. dev_set_drvdata(dev, dvi);
  248. dvi->bridge.funcs = &tfp410_bridge_funcs;
  249. dvi->bridge.of_node = dev->of_node;
  250. dvi->bridge.timings = &dvi->timings;
  251. dvi->bridge.type = DRM_MODE_CONNECTOR_DVID;
  252. ret = tfp410_parse_timings(dvi, i2c);
  253. if (ret)
  254. return ret;
  255. /* Get the next bridge, connected to port@1. */
  256. node = of_graph_get_remote_node(dev->of_node, 1, -1);
  257. if (!node)
  258. return -ENODEV;
  259. dvi->next_bridge = of_drm_find_bridge(node);
  260. of_node_put(node);
  261. if (!dvi->next_bridge)
  262. return -EPROBE_DEFER;
  263. /* Get the powerdown GPIO. */
  264. dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown",
  265. GPIOD_OUT_HIGH);
  266. if (IS_ERR(dvi->powerdown)) {
  267. dev_err(dev, "failed to parse powerdown gpio\n");
  268. return PTR_ERR(dvi->powerdown);
  269. }
  270. /* Register the DRM bridge. */
  271. drm_bridge_add(&dvi->bridge);
  272. return 0;
  273. }
  274. static int tfp410_fini(struct device *dev)
  275. {
  276. struct tfp410 *dvi = dev_get_drvdata(dev);
  277. drm_bridge_remove(&dvi->bridge);
  278. return 0;
  279. }
  280. static int tfp410_probe(struct platform_device *pdev)
  281. {
  282. return tfp410_init(&pdev->dev, false);
  283. }
  284. static int tfp410_remove(struct platform_device *pdev)
  285. {
  286. return tfp410_fini(&pdev->dev);
  287. }
  288. static const struct of_device_id tfp410_match[] = {
  289. { .compatible = "ti,tfp410" },
  290. {},
  291. };
  292. MODULE_DEVICE_TABLE(of, tfp410_match);
  293. static struct platform_driver tfp410_platform_driver = {
  294. .probe = tfp410_probe,
  295. .remove = tfp410_remove,
  296. .driver = {
  297. .name = "tfp410-bridge",
  298. .of_match_table = tfp410_match,
  299. },
  300. };
  301. #if IS_ENABLED(CONFIG_I2C)
  302. /* There is currently no i2c functionality. */
  303. static int tfp410_i2c_probe(struct i2c_client *client,
  304. const struct i2c_device_id *id)
  305. {
  306. int reg;
  307. if (!client->dev.of_node ||
  308. of_property_read_u32(client->dev.of_node, "reg", &reg)) {
  309. dev_err(&client->dev,
  310. "Can't get i2c reg property from device-tree\n");
  311. return -ENXIO;
  312. }
  313. return tfp410_init(&client->dev, true);
  314. }
  315. static int tfp410_i2c_remove(struct i2c_client *client)
  316. {
  317. return tfp410_fini(&client->dev);
  318. }
  319. static const struct i2c_device_id tfp410_i2c_ids[] = {
  320. { "tfp410", 0 },
  321. { }
  322. };
  323. MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
  324. static struct i2c_driver tfp410_i2c_driver = {
  325. .driver = {
  326. .name = "tfp410",
  327. .of_match_table = of_match_ptr(tfp410_match),
  328. },
  329. .id_table = tfp410_i2c_ids,
  330. .probe = tfp410_i2c_probe,
  331. .remove = tfp410_i2c_remove,
  332. };
  333. #endif /* IS_ENABLED(CONFIG_I2C) */
  334. static struct {
  335. uint i2c:1;
  336. uint platform:1;
  337. } tfp410_registered_driver;
  338. static int __init tfp410_module_init(void)
  339. {
  340. int ret;
  341. #if IS_ENABLED(CONFIG_I2C)
  342. ret = i2c_add_driver(&tfp410_i2c_driver);
  343. if (ret)
  344. pr_err("%s: registering i2c driver failed: %d",
  345. __func__, ret);
  346. else
  347. tfp410_registered_driver.i2c = 1;
  348. #endif
  349. ret = platform_driver_register(&tfp410_platform_driver);
  350. if (ret)
  351. pr_err("%s: registering platform driver failed: %d",
  352. __func__, ret);
  353. else
  354. tfp410_registered_driver.platform = 1;
  355. if (tfp410_registered_driver.i2c ||
  356. tfp410_registered_driver.platform)
  357. return 0;
  358. return ret;
  359. }
  360. module_init(tfp410_module_init);
  361. static void __exit tfp410_module_exit(void)
  362. {
  363. #if IS_ENABLED(CONFIG_I2C)
  364. if (tfp410_registered_driver.i2c)
  365. i2c_del_driver(&tfp410_i2c_driver);
  366. #endif
  367. if (tfp410_registered_driver.platform)
  368. platform_driver_unregister(&tfp410_platform_driver);
  369. }
  370. module_exit(tfp410_module_exit);
  371. MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
  372. MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
  373. MODULE_LICENSE("GPL");