1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * TPD12S015 HDMI ESD protection & level shifter chip driver 4 * 5 * Copyright (C) 2019 Texas Instruments Incorporated 6 * 7 * Based on the omapdrm-specific encoder-opa362 driver 8 * 9 * Copyright (C) 2013 Texas Instruments Incorporated 10 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com> 11 */ 12 13 #include <linux/delay.h> 14 #include <linux/gpio/consumer.h> 15 #include <linux/interrupt.h> 16 #include <linux/module.h> 17 #include <linux/mutex.h> 18 #include <linux/of.h> 19 #include <linux/of_graph.h> 20 #include <linux/platform_device.h> 21 22 #include <drm/drm_atomic_state_helper.h> 23 #include <drm/drm_bridge.h> 24 25 struct tpd12s015_device { 26 struct drm_bridge bridge; 27 28 struct gpio_desc *ct_cp_hpd_gpio; 29 struct gpio_desc *ls_oe_gpio; 30 struct gpio_desc *hpd_gpio; 31 int hpd_irq; 32 }; 33 34 static inline struct tpd12s015_device *to_tpd12s015(struct drm_bridge *bridge) 35 { 36 return container_of(bridge, struct tpd12s015_device, bridge); 37 } 38 39 static int tpd12s015_attach(struct drm_bridge *bridge, 40 struct drm_encoder *encoder, 41 enum drm_bridge_attach_flags flags) 42 { 43 struct tpd12s015_device *tpd = to_tpd12s015(bridge); 44 int ret; 45 46 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) 47 return -EINVAL; 48 49 ret = drm_bridge_attach(encoder, tpd->bridge.next_bridge, 50 bridge, flags); 51 if (ret < 0) 52 return ret; 53 54 gpiod_set_value_cansleep(tpd->ls_oe_gpio, 1); 55 56 /* DC-DC converter needs at max 300us to get to 90% of 5V. */ 57 usleep_range(300, 1000); 58 59 return 0; 60 } 61 62 static void tpd12s015_detach(struct drm_bridge *bridge) 63 { 64 struct tpd12s015_device *tpd = to_tpd12s015(bridge); 65 66 gpiod_set_value_cansleep(tpd->ls_oe_gpio, 0); 67 } 68 69 static enum drm_connector_status tpd12s015_detect(struct drm_bridge *bridge) 70 { 71 struct tpd12s015_device *tpd = to_tpd12s015(bridge); 72 73 if (gpiod_get_value_cansleep(tpd->hpd_gpio)) 74 return connector_status_connected; 75 else 76 return connector_status_disconnected; 77 } 78 79 static enum drm_connector_status 80 tpd12s015_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector) 81 { 82 return tpd12s015_detect(bridge); 83 } 84 85 static void tpd12s015_hpd_enable(struct drm_bridge *bridge) 86 { 87 struct tpd12s015_device *tpd = to_tpd12s015(bridge); 88 89 gpiod_set_value_cansleep(tpd->ct_cp_hpd_gpio, 1); 90 } 91 92 static void tpd12s015_hpd_disable(struct drm_bridge *bridge) 93 { 94 struct tpd12s015_device *tpd = to_tpd12s015(bridge); 95 96 gpiod_set_value_cansleep(tpd->ct_cp_hpd_gpio, 0); 97 } 98 99 static const struct drm_bridge_funcs tpd12s015_bridge_funcs = { 100 .atomic_create_state = drm_atomic_helper_bridge_create_state, 101 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state, 102 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state, 103 .attach = tpd12s015_attach, 104 .detach = tpd12s015_detach, 105 .detect = tpd12s015_bridge_detect, 106 .hpd_enable = tpd12s015_hpd_enable, 107 .hpd_disable = tpd12s015_hpd_disable, 108 }; 109 110 static irqreturn_t tpd12s015_hpd_isr(int irq, void *data) 111 { 112 struct tpd12s015_device *tpd = data; 113 struct drm_bridge *bridge = &tpd->bridge; 114 115 drm_bridge_hpd_notify(bridge, tpd12s015_detect(bridge)); 116 117 return IRQ_HANDLED; 118 } 119 120 static int tpd12s015_probe(struct platform_device *pdev) 121 { 122 struct tpd12s015_device *tpd; 123 struct device_node *node; 124 struct gpio_desc *gpio; 125 int ret; 126 127 tpd = devm_drm_bridge_alloc(&pdev->dev, struct tpd12s015_device, 128 bridge, &tpd12s015_bridge_funcs); 129 if (IS_ERR(tpd)) 130 return PTR_ERR(tpd); 131 132 platform_set_drvdata(pdev, tpd); 133 134 tpd->bridge.of_node = pdev->dev.of_node; 135 tpd->bridge.type = DRM_MODE_CONNECTOR_HDMIA; 136 tpd->bridge.ops = DRM_BRIDGE_OP_DETECT; 137 138 /* Get the next bridge, connected to port@1. */ 139 node = of_graph_get_remote_node(pdev->dev.of_node, 1, -1); 140 if (!node) 141 return -ENODEV; 142 143 tpd->bridge.next_bridge = of_drm_find_and_get_bridge(node); 144 of_node_put(node); 145 146 if (!tpd->bridge.next_bridge) 147 return -EPROBE_DEFER; 148 149 /* Get the control and HPD GPIOs. */ 150 gpio = devm_gpiod_get_index_optional(&pdev->dev, NULL, 0, 151 GPIOD_OUT_LOW); 152 if (IS_ERR(gpio)) 153 return PTR_ERR(gpio); 154 155 tpd->ct_cp_hpd_gpio = gpio; 156 157 gpio = devm_gpiod_get_index_optional(&pdev->dev, NULL, 1, 158 GPIOD_OUT_LOW); 159 if (IS_ERR(gpio)) 160 return PTR_ERR(gpio); 161 162 tpd->ls_oe_gpio = gpio; 163 164 gpio = devm_gpiod_get_index(&pdev->dev, NULL, 2, GPIOD_IN); 165 if (IS_ERR(gpio)) 166 return PTR_ERR(gpio); 167 168 tpd->hpd_gpio = gpio; 169 170 /* Register the IRQ if the HPD GPIO is IRQ-capable. */ 171 tpd->hpd_irq = gpiod_to_irq(tpd->hpd_gpio); 172 if (tpd->hpd_irq >= 0) { 173 ret = devm_request_threaded_irq(&pdev->dev, tpd->hpd_irq, NULL, 174 tpd12s015_hpd_isr, 175 IRQF_TRIGGER_RISING | 176 IRQF_TRIGGER_FALLING | 177 IRQF_ONESHOT, 178 "tpd12s015 hpd", tpd); 179 if (ret) 180 return ret; 181 182 tpd->bridge.ops |= DRM_BRIDGE_OP_HPD; 183 } 184 185 /* Register the DRM bridge. */ 186 drm_bridge_add(&tpd->bridge); 187 188 return 0; 189 } 190 191 static void tpd12s015_remove(struct platform_device *pdev) 192 { 193 struct tpd12s015_device *tpd = platform_get_drvdata(pdev); 194 195 drm_bridge_remove(&tpd->bridge); 196 } 197 198 static const struct of_device_id tpd12s015_of_match[] = { 199 { .compatible = "ti,tpd12s015", }, 200 {}, 201 }; 202 203 MODULE_DEVICE_TABLE(of, tpd12s015_of_match); 204 205 static struct platform_driver tpd12s015_driver = { 206 .probe = tpd12s015_probe, 207 .remove = tpd12s015_remove, 208 .driver = { 209 .name = "tpd12s015", 210 .of_match_table = tpd12s015_of_match, 211 }, 212 }; 213 214 module_platform_driver(tpd12s015_driver); 215 216 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>"); 217 MODULE_DESCRIPTION("TPD12S015 HDMI level shifter and ESD protection driver"); 218 MODULE_LICENSE("GPL"); 219