Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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