ti-tfp410.c 11 KB

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