summaryrefslogtreecommitdiff
path: root/drivers/usb/typec/mux/gpio-sbu-mux.c
blob: f62516dafe8fad5c9bbfe202734e66a811b8a750 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2022 Linaro Ltd.
 */

#include <linux/bits.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/usb/typec_dp.h>
#include <linux/usb/typec_mux.h>

struct gpio_sbu_mux {
	struct gpio_desc *enable_gpio;
	struct gpio_desc *select_gpio;

	struct typec_switch_dev *sw;
	struct typec_mux_dev *mux;

	struct mutex lock; /* protect enabled and swapped */
	bool enabled;
	bool swapped;
};

static int gpio_sbu_switch_set(struct typec_switch_dev *sw,
			       enum typec_orientation orientation)
{
	struct gpio_sbu_mux *sbu_mux = typec_switch_get_drvdata(sw);
	bool enabled;
	bool swapped;

	mutex_lock(&sbu_mux->lock);

	enabled = sbu_mux->enabled;
	swapped = sbu_mux->swapped;

	switch (orientation) {
	case TYPEC_ORIENTATION_NONE:
		enabled = false;
		break;
	case TYPEC_ORIENTATION_NORMAL:
		swapped = false;
		break;
	case TYPEC_ORIENTATION_REVERSE:
		swapped = true;
		break;
	}

	if (enabled != sbu_mux->enabled)
		gpiod_set_value(sbu_mux->enable_gpio, enabled);

	if (swapped != sbu_mux->swapped)
		gpiod_set_value(sbu_mux->select_gpio, swapped);

	sbu_mux->enabled = enabled;
	sbu_mux->swapped = swapped;

	mutex_unlock(&sbu_mux->lock);

	return 0;
}

static int gpio_sbu_mux_set(struct typec_mux_dev *mux,
			    struct typec_mux_state *state)
{
	struct gpio_sbu_mux *sbu_mux = typec_mux_get_drvdata(mux);

	mutex_lock(&sbu_mux->lock);

	switch (state->mode) {
	case TYPEC_STATE_SAFE:
	case TYPEC_STATE_USB:
		sbu_mux->enabled = false;
		break;
	case TYPEC_DP_STATE_C:
	case TYPEC_DP_STATE_D:
	case TYPEC_DP_STATE_E:
		sbu_mux->enabled = true;
		break;
	default:
		break;
	}

	gpiod_set_value(sbu_mux->enable_gpio, sbu_mux->enabled);

	mutex_unlock(&sbu_mux->lock);

	return 0;
}

static int gpio_sbu_mux_probe(struct platform_device *pdev)
{
	struct typec_switch_desc sw_desc = { };
	struct typec_mux_desc mux_desc = { };
	struct device *dev = &pdev->dev;
	struct gpio_sbu_mux *sbu_mux;

	sbu_mux = devm_kzalloc(dev, sizeof(*sbu_mux), GFP_KERNEL);
	if (!sbu_mux)
		return -ENOMEM;

	mutex_init(&sbu_mux->lock);

	sbu_mux->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
	if (IS_ERR(sbu_mux->enable_gpio))
		return dev_err_probe(dev, PTR_ERR(sbu_mux->enable_gpio),
				     "unable to acquire enable gpio\n");

	sbu_mux->select_gpio = devm_gpiod_get(dev, "select", GPIOD_OUT_LOW);
	if (IS_ERR(sbu_mux->select_gpio))
		return dev_err_probe(dev, PTR_ERR(sbu_mux->select_gpio),
				     "unable to acquire select gpio\n");

	sw_desc.drvdata = sbu_mux;
	sw_desc.fwnode = dev_fwnode(dev);
	sw_desc.set = gpio_sbu_switch_set;

	sbu_mux->sw = typec_switch_register(dev, &sw_desc);
	if (IS_ERR(sbu_mux->sw))
		return dev_err_probe(dev, PTR_ERR(sbu_mux->sw),
				     "failed to register typec switch\n");

	mux_desc.drvdata = sbu_mux;
	mux_desc.fwnode = dev_fwnode(dev);
	mux_desc.set = gpio_sbu_mux_set;

	sbu_mux->mux = typec_mux_register(dev, &mux_desc);
	if (IS_ERR(sbu_mux->mux)) {
		typec_switch_unregister(sbu_mux->sw);
		return dev_err_probe(dev, PTR_ERR(sbu_mux->mux),
				     "failed to register typec mux\n");
	}

	platform_set_drvdata(pdev, sbu_mux);

	return 0;
}

static int gpio_sbu_mux_remove(struct platform_device *pdev)
{
	struct gpio_sbu_mux *sbu_mux = platform_get_drvdata(pdev);

	gpiod_set_value(sbu_mux->enable_gpio, 0);

	typec_mux_unregister(sbu_mux->mux);
	typec_switch_unregister(sbu_mux->sw);

	return 0;
}

static const struct of_device_id gpio_sbu_mux_match[] = {
	{ .compatible = "gpio-sbu-mux", },
	{}
};
MODULE_DEVICE_TABLE(of, gpio_sbu_mux_match);

static struct platform_driver gpio_sbu_mux_driver = {
	.probe = gpio_sbu_mux_probe,
	.remove = gpio_sbu_mux_remove,
	.driver = {
		.name = "gpio_sbu_mux",
		.of_match_table = gpio_sbu_mux_match,
	},
};
module_platform_driver(gpio_sbu_mux_driver);

MODULE_DESCRIPTION("GPIO based SBU mux driver");
MODULE_LICENSE("GPL");