1 // SPDX-License-Identifier: GPL-2.0+
3 * Pericom PI3USB30532 Type-C cross switch / mux driver
5 * Copyright (c) 2017-2018 Hans de Goede <hdegoede@redhat.com>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/usb/typec_dp.h>
13 #include <linux/usb/typec_mux.h>
15 #define PI3USB30532_CONF 0x00
17 #define PI3USB30532_CONF_OPEN 0x00
18 #define PI3USB30532_CONF_SWAP 0x01
19 #define PI3USB30532_CONF_4LANE_DP 0x02
20 #define PI3USB30532_CONF_USB3 0x04
21 #define PI3USB30532_CONF_USB3_AND_2LANE_DP 0x06
24 struct i2c_client *client;
25 struct mutex lock; /* protects the cached conf register */
26 struct typec_switch sw;
31 static int pi3usb30532_set_conf(struct pi3usb30532 *pi, u8 new_conf)
35 if (pi->conf == new_conf)
38 ret = i2c_smbus_write_byte_data(pi->client, PI3USB30532_CONF, new_conf);
40 dev_err(&pi->client->dev, "Error writing conf: %d\n", ret);
48 static int pi3usb30532_sw_set(struct typec_switch *sw,
49 enum typec_orientation orientation)
51 struct pi3usb30532 *pi = container_of(sw, struct pi3usb30532, sw);
55 mutex_lock(&pi->lock);
58 switch (orientation) {
59 case TYPEC_ORIENTATION_NONE:
60 new_conf = PI3USB30532_CONF_OPEN;
62 case TYPEC_ORIENTATION_NORMAL:
63 new_conf &= ~PI3USB30532_CONF_SWAP;
65 case TYPEC_ORIENTATION_REVERSE:
66 new_conf |= PI3USB30532_CONF_SWAP;
70 ret = pi3usb30532_set_conf(pi, new_conf);
71 mutex_unlock(&pi->lock);
76 static int pi3usb30532_mux_set(struct typec_mux *mux, int state)
78 struct pi3usb30532 *pi = container_of(mux, struct pi3usb30532, mux);
82 mutex_lock(&pi->lock);
86 case TYPEC_STATE_SAFE:
87 new_conf = PI3USB30532_CONF_OPEN;
90 new_conf = (new_conf & PI3USB30532_CONF_SWAP) |
91 PI3USB30532_CONF_USB3;
93 case TYPEC_DP_STATE_C:
94 case TYPEC_DP_STATE_E:
95 new_conf = (new_conf & PI3USB30532_CONF_SWAP) |
96 PI3USB30532_CONF_4LANE_DP;
98 case TYPEC_DP_STATE_D:
99 new_conf = (new_conf & PI3USB30532_CONF_SWAP) |
100 PI3USB30532_CONF_USB3_AND_2LANE_DP;
106 ret = pi3usb30532_set_conf(pi, new_conf);
107 mutex_unlock(&pi->lock);
112 static int pi3usb30532_probe(struct i2c_client *client)
114 struct device *dev = &client->dev;
115 struct pi3usb30532 *pi;
118 pi = devm_kzalloc(dev, sizeof(*pi), GFP_KERNEL);
124 pi->sw.set = pi3usb30532_sw_set;
126 pi->mux.set = pi3usb30532_mux_set;
127 mutex_init(&pi->lock);
129 ret = i2c_smbus_read_byte_data(client, PI3USB30532_CONF);
131 dev_err(dev, "Error reading config register %d\n", ret);
136 ret = typec_switch_register(&pi->sw);
138 dev_err(dev, "Error registering typec switch: %d\n", ret);
142 ret = typec_mux_register(&pi->mux);
144 typec_switch_unregister(&pi->sw);
145 dev_err(dev, "Error registering typec mux: %d\n", ret);
149 i2c_set_clientdata(client, pi);
153 static int pi3usb30532_remove(struct i2c_client *client)
155 struct pi3usb30532 *pi = i2c_get_clientdata(client);
157 typec_mux_unregister(&pi->mux);
158 typec_switch_unregister(&pi->sw);
162 static const struct i2c_device_id pi3usb30532_table[] = {
166 MODULE_DEVICE_TABLE(i2c, pi3usb30532_table);
168 static struct i2c_driver pi3usb30532_driver = {
170 .name = "pi3usb30532",
172 .probe_new = pi3usb30532_probe,
173 .remove = pi3usb30532_remove,
174 .id_table = pi3usb30532_table,
177 module_i2c_driver(pi3usb30532_driver);
179 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
180 MODULE_DESCRIPTION("Pericom PI3USB30532 Type-C mux driver");
181 MODULE_LICENSE("GPL");