blob: 4d08c2123e5980e27cfe262607ee8320fb004242 [file] [log] [blame]
Michael Auchter06bc4ca2020-10-15 09:07:34 -05001// SPDX-License-Identifier: GPL-2.0
Rong Chen08099ec2022-09-06 12:04:29 +08002/*
Michael Auchter06bc4ca2020-10-15 09:07:34 -05003 * drivers/extcon/extcon-tusb320.c - TUSB320 extcon driver
4 *
5 * Copyright (C) 2020 National Instruments Corporation
6 * Author: Michael Auchter <michael.auchter@ni.com>
7 */
8
Marek Vasutbf7571c2022-07-30 20:05:00 +02009#include <linux/bitfield.h>
Michael Auchter06bc4ca2020-10-15 09:07:34 -050010#include <linux/extcon-provider.h>
11#include <linux/i2c.h>
12#include <linux/init.h>
13#include <linux/interrupt.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/regmap.h>
Marek Vasutbf7571c2022-07-30 20:05:00 +020017#include <linux/usb/typec.h>
Alvin Šipraga18eb81d2023-03-18 16:05:53 +010018#include <linux/usb/typec_altmode.h>
Alvin Šipragadf101442023-03-18 16:05:54 +010019#include <linux/usb/role.h>
Marek Vasutbf7571c2022-07-30 20:05:00 +020020
21#define TUSB320_REG8 0x8
22#define TUSB320_REG8_CURRENT_MODE_ADVERTISE GENMASK(7, 6)
23#define TUSB320_REG8_CURRENT_MODE_ADVERTISE_USB 0x0
24#define TUSB320_REG8_CURRENT_MODE_ADVERTISE_15A 0x1
25#define TUSB320_REG8_CURRENT_MODE_ADVERTISE_30A 0x2
26#define TUSB320_REG8_CURRENT_MODE_DETECT GENMASK(5, 4)
27#define TUSB320_REG8_CURRENT_MODE_DETECT_DEF 0x0
28#define TUSB320_REG8_CURRENT_MODE_DETECT_MED 0x1
29#define TUSB320_REG8_CURRENT_MODE_DETECT_ACC 0x2
30#define TUSB320_REG8_CURRENT_MODE_DETECT_HI 0x3
Alvin Šipraga18eb81d2023-03-18 16:05:53 +010031#define TUSB320_REG8_ACCESSORY_CONNECTED GENMASK(3, 1)
Marek Vasutbf7571c2022-07-30 20:05:00 +020032#define TUSB320_REG8_ACCESSORY_CONNECTED_NONE 0x0
33#define TUSB320_REG8_ACCESSORY_CONNECTED_AUDIO 0x4
Alvin Šipraga18eb81d2023-03-18 16:05:53 +010034#define TUSB320_REG8_ACCESSORY_CONNECTED_ACHRG 0x5
35#define TUSB320_REG8_ACCESSORY_CONNECTED_DBGDFP 0x6
36#define TUSB320_REG8_ACCESSORY_CONNECTED_DBGUFP 0x7
Marek Vasutbf7571c2022-07-30 20:05:00 +020037#define TUSB320_REG8_ACTIVE_CABLE_DETECTION BIT(0)
Michael Auchter06bc4ca2020-10-15 09:07:34 -050038
39#define TUSB320_REG9 0x9
Alvin Šipraga18eb81d2023-03-18 16:05:53 +010040#define TUSB320_REG9_ATTACHED_STATE GENMASK(7, 6)
Michael Auchter06bc4ca2020-10-15 09:07:34 -050041#define TUSB320_REG9_CABLE_DIRECTION BIT(5)
42#define TUSB320_REG9_INTERRUPT_STATUS BIT(4)
Yassine Oudjana70c55d62021-09-25 05:45:39 +000043
44#define TUSB320_REGA 0xa
Yassine Oudjanace0320b2021-09-25 05:45:54 +000045#define TUSB320L_REGA_DISABLE_TERM BIT(0)
Yassine Oudjana70c55d62021-09-25 05:45:39 +000046#define TUSB320_REGA_I2C_SOFT_RESET BIT(3)
47#define TUSB320_REGA_MODE_SELECT_SHIFT 4
48#define TUSB320_REGA_MODE_SELECT_MASK 0x3
49
Yassine Oudjanace0320b2021-09-25 05:45:54 +000050#define TUSB320L_REGA0_REVISION 0xa0
51
Yassine Oudjana70c55d62021-09-25 05:45:39 +000052enum tusb320_attached_state {
53 TUSB320_ATTACHED_STATE_NONE,
54 TUSB320_ATTACHED_STATE_DFP,
55 TUSB320_ATTACHED_STATE_UFP,
56 TUSB320_ATTACHED_STATE_ACC,
57};
58
59enum tusb320_mode {
60 TUSB320_MODE_PORT,
61 TUSB320_MODE_UFP,
62 TUSB320_MODE_DFP,
63 TUSB320_MODE_DRP,
64};
Michael Auchter06bc4ca2020-10-15 09:07:34 -050065
Yassine Oudjanace0320b2021-09-25 05:45:54 +000066struct tusb320_priv;
67
68struct tusb320_ops {
69 int (*set_mode)(struct tusb320_priv *priv, enum tusb320_mode mode);
70 int (*get_revision)(struct tusb320_priv *priv, unsigned int *revision);
71};
72
Michael Auchter06bc4ca2020-10-15 09:07:34 -050073struct tusb320_priv {
74 struct device *dev;
75 struct regmap *regmap;
76 struct extcon_dev *edev;
Yassine Oudjanace0320b2021-09-25 05:45:54 +000077 struct tusb320_ops *ops;
Yassine Oudjana70c55d62021-09-25 05:45:39 +000078 enum tusb320_attached_state state;
Marek Vasutbf7571c2022-07-30 20:05:00 +020079 struct typec_port *port;
80 struct typec_capability cap;
81 enum typec_port_type port_type;
82 enum typec_pwr_opmode pwr_opmode;
Alvin Šipraga3adbaa32023-03-15 15:15:47 +010083 struct fwnode_handle *connector_fwnode;
Alvin Šipragadf101442023-03-18 16:05:54 +010084 struct usb_role_switch *role_sw;
Michael Auchter06bc4ca2020-10-15 09:07:34 -050085};
86
87static const char * const tusb_attached_states[] = {
88 [TUSB320_ATTACHED_STATE_NONE] = "not attached",
89 [TUSB320_ATTACHED_STATE_DFP] = "downstream facing port",
90 [TUSB320_ATTACHED_STATE_UFP] = "upstream facing port",
91 [TUSB320_ATTACHED_STATE_ACC] = "accessory",
92};
93
94static const unsigned int tusb320_extcon_cable[] = {
95 EXTCON_USB,
96 EXTCON_USB_HOST,
97 EXTCON_NONE,
98};
99
100static int tusb320_check_signature(struct tusb320_priv *priv)
101{
102 static const char sig[] = { '\0', 'T', 'U', 'S', 'B', '3', '2', '0' };
103 unsigned val;
104 int i, ret;
105
106 for (i = 0; i < sizeof(sig); i++) {
107 ret = regmap_read(priv->regmap, sizeof(sig) - 1 - i, &val);
108 if (ret < 0)
109 return ret;
110 if (val != sig[i]) {
111 dev_err(priv->dev, "signature mismatch!\n");
112 return -ENODEV;
113 }
114 }
115
116 return 0;
117}
118
Yassine Oudjana70c55d62021-09-25 05:45:39 +0000119static int tusb320_set_mode(struct tusb320_priv *priv, enum tusb320_mode mode)
120{
121 int ret;
122
123 /* Mode cannot be changed while cable is attached */
124 if (priv->state != TUSB320_ATTACHED_STATE_NONE)
125 return -EBUSY;
126
127 /* Write mode */
128 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
129 TUSB320_REGA_MODE_SELECT_MASK << TUSB320_REGA_MODE_SELECT_SHIFT,
130 mode << TUSB320_REGA_MODE_SELECT_SHIFT);
131 if (ret) {
132 dev_err(priv->dev, "failed to write mode: %d\n", ret);
133 return ret;
134 }
135
136 return 0;
137}
138
Yassine Oudjanace0320b2021-09-25 05:45:54 +0000139static int tusb320l_set_mode(struct tusb320_priv *priv, enum tusb320_mode mode)
140{
141 int ret;
142
143 /* Disable CC state machine */
144 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
145 TUSB320L_REGA_DISABLE_TERM, 1);
146 if (ret) {
147 dev_err(priv->dev,
148 "failed to disable CC state machine: %d\n", ret);
149 return ret;
150 }
151
152 /* Write mode */
153 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
154 TUSB320_REGA_MODE_SELECT_MASK << TUSB320_REGA_MODE_SELECT_SHIFT,
155 mode << TUSB320_REGA_MODE_SELECT_SHIFT);
156 if (ret) {
157 dev_err(priv->dev, "failed to write mode: %d\n", ret);
158 goto err;
159 }
160
161 msleep(5);
162err:
163 /* Re-enable CC state machine */
164 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
165 TUSB320L_REGA_DISABLE_TERM, 0);
166 if (ret)
167 dev_err(priv->dev,
168 "failed to re-enable CC state machine: %d\n", ret);
169
170 return ret;
171}
172
Yassine Oudjana70c55d62021-09-25 05:45:39 +0000173static int tusb320_reset(struct tusb320_priv *priv)
174{
175 int ret;
176
177 /* Set mode to default (follow PORT pin) */
Yassine Oudjanace0320b2021-09-25 05:45:54 +0000178 ret = priv->ops->set_mode(priv, TUSB320_MODE_PORT);
Yassine Oudjana70c55d62021-09-25 05:45:39 +0000179 if (ret && ret != -EBUSY) {
180 dev_err(priv->dev,
181 "failed to set mode to PORT: %d\n", ret);
182 return ret;
183 }
184
185 /* Perform soft reset */
186 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
187 TUSB320_REGA_I2C_SOFT_RESET, 1);
188 if (ret) {
189 dev_err(priv->dev,
190 "failed to write soft reset bit: %d\n", ret);
191 return ret;
192 }
193
194 /* Wait for chip to go through reset */
195 msleep(95);
196
197 return 0;
198}
199
Yassine Oudjanace0320b2021-09-25 05:45:54 +0000200static int tusb320l_get_revision(struct tusb320_priv *priv, unsigned int *revision)
201{
202 return regmap_read(priv->regmap, TUSB320L_REGA0_REVISION, revision);
203}
204
205static struct tusb320_ops tusb320_ops = {
206 .set_mode = tusb320_set_mode,
207};
208
209static struct tusb320_ops tusb320l_ops = {
210 .set_mode = tusb320l_set_mode,
211 .get_revision = tusb320l_get_revision,
212};
213
Marek Vasutbf7571c2022-07-30 20:05:00 +0200214static int tusb320_set_adv_pwr_mode(struct tusb320_priv *priv)
215{
216 u8 mode;
217
218 if (priv->pwr_opmode == TYPEC_PWR_MODE_USB)
219 mode = TUSB320_REG8_CURRENT_MODE_ADVERTISE_USB;
220 else if (priv->pwr_opmode == TYPEC_PWR_MODE_1_5A)
221 mode = TUSB320_REG8_CURRENT_MODE_ADVERTISE_15A;
222 else if (priv->pwr_opmode == TYPEC_PWR_MODE_3_0A)
223 mode = TUSB320_REG8_CURRENT_MODE_ADVERTISE_30A;
224 else /* No other mode is supported. */
225 return -EINVAL;
226
227 return regmap_write_bits(priv->regmap, TUSB320_REG8,
228 TUSB320_REG8_CURRENT_MODE_ADVERTISE,
229 FIELD_PREP(TUSB320_REG8_CURRENT_MODE_ADVERTISE,
230 mode));
231}
232
233static int tusb320_port_type_set(struct typec_port *port,
234 enum typec_port_type type)
235{
236 struct tusb320_priv *priv = typec_get_drvdata(port);
237
238 if (type == TYPEC_PORT_SRC)
239 return priv->ops->set_mode(priv, TUSB320_MODE_DFP);
240 else if (type == TYPEC_PORT_SNK)
241 return priv->ops->set_mode(priv, TUSB320_MODE_UFP);
242 else if (type == TYPEC_PORT_DRP)
243 return priv->ops->set_mode(priv, TUSB320_MODE_DRP);
244 else
245 return priv->ops->set_mode(priv, TUSB320_MODE_PORT);
246}
247
248static const struct typec_operations tusb320_typec_ops = {
249 .port_type_set = tusb320_port_type_set,
250};
251
Marek Vasut94838112022-07-30 20:04:59 +0200252static void tusb320_extcon_irq_handler(struct tusb320_priv *priv, u8 reg)
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500253{
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500254 int state, polarity;
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500255
Alvin Šipraga18eb81d2023-03-18 16:05:53 +0100256 state = FIELD_GET(TUSB320_REG9_ATTACHED_STATE, reg);
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500257 polarity = !!(reg & TUSB320_REG9_CABLE_DIRECTION);
258
259 dev_dbg(priv->dev, "attached state: %s, polarity: %d\n",
260 tusb_attached_states[state], polarity);
261
262 extcon_set_state(priv->edev, EXTCON_USB,
263 state == TUSB320_ATTACHED_STATE_UFP);
264 extcon_set_state(priv->edev, EXTCON_USB_HOST,
265 state == TUSB320_ATTACHED_STATE_DFP);
266 extcon_set_property(priv->edev, EXTCON_USB,
267 EXTCON_PROP_USB_TYPEC_POLARITY,
268 (union extcon_property_value)polarity);
269 extcon_set_property(priv->edev, EXTCON_USB_HOST,
270 EXTCON_PROP_USB_TYPEC_POLARITY,
271 (union extcon_property_value)polarity);
272 extcon_sync(priv->edev, EXTCON_USB);
273 extcon_sync(priv->edev, EXTCON_USB_HOST);
274
Yassine Oudjana70c55d62021-09-25 05:45:39 +0000275 priv->state = state;
Marek Vasut94838112022-07-30 20:04:59 +0200276}
277
Marek Vasutbf7571c2022-07-30 20:05:00 +0200278static void tusb320_typec_irq_handler(struct tusb320_priv *priv, u8 reg9)
279{
280 struct typec_port *port = priv->port;
281 struct device *dev = priv->dev;
Alvin Šipraga18eb81d2023-03-18 16:05:53 +0100282 int typec_mode;
Alvin Šipragadf101442023-03-18 16:05:54 +0100283 enum usb_role usb_role;
Alvin Šipraga18eb81d2023-03-18 16:05:53 +0100284 enum typec_role pwr_role;
285 enum typec_data_role data_role;
286 u8 state, mode, accessory;
Marek Vasutbf7571c2022-07-30 20:05:00 +0200287 int ret, reg8;
288 bool ori;
289
Marek Vasutbf7571c2022-07-30 20:05:00 +0200290 ret = regmap_read(priv->regmap, TUSB320_REG8, &reg8);
291 if (ret) {
292 dev_err(dev, "error during reg8 i2c read, ret=%d!\n", ret);
293 return;
294 }
295
Alvin Šipraga18eb81d2023-03-18 16:05:53 +0100296 ori = reg9 & TUSB320_REG9_CABLE_DIRECTION;
297 typec_set_orientation(port, ori ? TYPEC_ORIENTATION_REVERSE :
298 TYPEC_ORIENTATION_NORMAL);
299
300 state = FIELD_GET(TUSB320_REG9_ATTACHED_STATE, reg9);
301 accessory = FIELD_GET(TUSB320_REG8_ACCESSORY_CONNECTED, reg8);
302
303 switch (state) {
304 case TUSB320_ATTACHED_STATE_DFP:
305 typec_mode = TYPEC_MODE_USB2;
Alvin Šipragadf101442023-03-18 16:05:54 +0100306 usb_role = USB_ROLE_HOST;
Alvin Šipraga18eb81d2023-03-18 16:05:53 +0100307 pwr_role = TYPEC_SOURCE;
308 data_role = TYPEC_HOST;
309 break;
310 case TUSB320_ATTACHED_STATE_UFP:
311 typec_mode = TYPEC_MODE_USB2;
Alvin Šipragadf101442023-03-18 16:05:54 +0100312 usb_role = USB_ROLE_DEVICE;
Alvin Šipraga18eb81d2023-03-18 16:05:53 +0100313 pwr_role = TYPEC_SINK;
314 data_role = TYPEC_DEVICE;
315 break;
316 case TUSB320_ATTACHED_STATE_ACC:
317 /*
318 * Accessory detected. For debug accessories, just make some
319 * qualified guesses as to the role for lack of a better option.
320 */
321 if (accessory == TUSB320_REG8_ACCESSORY_CONNECTED_AUDIO ||
322 accessory == TUSB320_REG8_ACCESSORY_CONNECTED_ACHRG) {
323 typec_mode = TYPEC_MODE_AUDIO;
Alvin Šipragadf101442023-03-18 16:05:54 +0100324 usb_role = USB_ROLE_NONE;
Alvin Šipraga18eb81d2023-03-18 16:05:53 +0100325 pwr_role = TYPEC_SINK;
326 data_role = TYPEC_DEVICE;
327 break;
328 } else if (accessory ==
329 TUSB320_REG8_ACCESSORY_CONNECTED_DBGDFP) {
330 typec_mode = TYPEC_MODE_DEBUG;
331 pwr_role = TYPEC_SOURCE;
Alvin Šipragadf101442023-03-18 16:05:54 +0100332 usb_role = USB_ROLE_HOST;
Alvin Šipraga18eb81d2023-03-18 16:05:53 +0100333 data_role = TYPEC_HOST;
334 break;
335 } else if (accessory ==
336 TUSB320_REG8_ACCESSORY_CONNECTED_DBGUFP) {
337 typec_mode = TYPEC_MODE_DEBUG;
338 pwr_role = TYPEC_SINK;
Alvin Šipragadf101442023-03-18 16:05:54 +0100339 usb_role = USB_ROLE_DEVICE;
Alvin Šipraga18eb81d2023-03-18 16:05:53 +0100340 data_role = TYPEC_DEVICE;
341 break;
342 }
343
344 dev_warn(priv->dev, "unexpected ACCESSORY_CONNECTED state %d\n",
345 accessory);
346
347 fallthrough;
348 default:
349 typec_mode = TYPEC_MODE_USB2;
Alvin Šipragadf101442023-03-18 16:05:54 +0100350 usb_role = USB_ROLE_NONE;
Alvin Šipraga18eb81d2023-03-18 16:05:53 +0100351 pwr_role = TYPEC_SINK;
352 data_role = TYPEC_DEVICE;
353 break;
354 }
355
356 typec_set_vconn_role(port, pwr_role);
357 typec_set_pwr_role(port, pwr_role);
358 typec_set_data_role(port, data_role);
359 typec_set_mode(port, typec_mode);
Alvin Šipragadf101442023-03-18 16:05:54 +0100360 usb_role_switch_set_role(priv->role_sw, usb_role);
Alvin Šipraga18eb81d2023-03-18 16:05:53 +0100361
Marek Vasutbf7571c2022-07-30 20:05:00 +0200362 mode = FIELD_GET(TUSB320_REG8_CURRENT_MODE_DETECT, reg8);
363 if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_DEF)
364 typec_set_pwr_opmode(port, TYPEC_PWR_MODE_USB);
365 else if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_MED)
366 typec_set_pwr_opmode(port, TYPEC_PWR_MODE_1_5A);
367 else if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_HI)
368 typec_set_pwr_opmode(port, TYPEC_PWR_MODE_3_0A);
369 else /* Charge through accessory */
370 typec_set_pwr_opmode(port, TYPEC_PWR_MODE_USB);
371}
372
Marek Vasut581c8482022-11-20 15:15:09 +0100373static irqreturn_t tusb320_state_update_handler(struct tusb320_priv *priv,
374 bool force_update)
Marek Vasut94838112022-07-30 20:04:59 +0200375{
Marek Vasut94838112022-07-30 20:04:59 +0200376 unsigned int reg;
377
378 if (regmap_read(priv->regmap, TUSB320_REG9, &reg)) {
379 dev_err(priv->dev, "error during i2c read!\n");
380 return IRQ_NONE;
381 }
382
Marek Vasut581c8482022-11-20 15:15:09 +0100383 if (!force_update && !(reg & TUSB320_REG9_INTERRUPT_STATUS))
Marek Vasut94838112022-07-30 20:04:59 +0200384 return IRQ_NONE;
385
386 tusb320_extcon_irq_handler(priv, reg);
Yassine Oudjana341fd152022-11-07 18:33:17 +0300387
388 /*
389 * Type-C support is optional. Only call the Type-C handler if a
390 * port had been registered previously.
391 */
392 if (priv->port)
393 tusb320_typec_irq_handler(priv, reg);
Yassine Oudjana70c55d62021-09-25 05:45:39 +0000394
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500395 regmap_write(priv->regmap, TUSB320_REG9, reg);
396
397 return IRQ_HANDLED;
398}
399
Marek Vasut581c8482022-11-20 15:15:09 +0100400static irqreturn_t tusb320_irq_handler(int irq, void *dev_id)
401{
402 struct tusb320_priv *priv = dev_id;
403
404 return tusb320_state_update_handler(priv, false);
405}
406
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500407static const struct regmap_config tusb320_regmap_config = {
408 .reg_bits = 8,
409 .val_bits = 8,
410};
411
Marek Vasut94838112022-07-30 20:04:59 +0200412static int tusb320_extcon_probe(struct tusb320_priv *priv)
413{
414 int ret;
415
416 priv->edev = devm_extcon_dev_allocate(priv->dev, tusb320_extcon_cable);
417 if (IS_ERR(priv->edev)) {
418 dev_err(priv->dev, "failed to allocate extcon device\n");
419 return PTR_ERR(priv->edev);
420 }
421
422 ret = devm_extcon_dev_register(priv->dev, priv->edev);
423 if (ret < 0) {
424 dev_err(priv->dev, "failed to register extcon device\n");
425 return ret;
426 }
427
428 extcon_set_property_capability(priv->edev, EXTCON_USB,
429 EXTCON_PROP_USB_TYPEC_POLARITY);
430 extcon_set_property_capability(priv->edev, EXTCON_USB_HOST,
431 EXTCON_PROP_USB_TYPEC_POLARITY);
432
433 return 0;
434}
435
Marek Vasutbf7571c2022-07-30 20:05:00 +0200436static int tusb320_typec_probe(struct i2c_client *client,
437 struct tusb320_priv *priv)
438{
439 struct fwnode_handle *connector;
440 const char *cap_str;
441 int ret;
442
443 /* The Type-C connector is optional, for backward compatibility. */
444 connector = device_get_named_child_node(&client->dev, "connector");
445 if (!connector)
446 return 0;
447
448 /* Type-C connector found. */
449 ret = typec_get_fw_cap(&priv->cap, connector);
450 if (ret)
Alvin Šipraga3adbaa32023-03-15 15:15:47 +0100451 goto err_put;
Marek Vasutbf7571c2022-07-30 20:05:00 +0200452
453 priv->port_type = priv->cap.type;
454
455 /* This goes into register 0x8 field CURRENT_MODE_ADVERTISE */
456 ret = fwnode_property_read_string(connector, "typec-power-opmode", &cap_str);
457 if (ret)
Alvin Šipraga3adbaa32023-03-15 15:15:47 +0100458 goto err_put;
Marek Vasutbf7571c2022-07-30 20:05:00 +0200459
460 ret = typec_find_pwr_opmode(cap_str);
461 if (ret < 0)
Alvin Šipraga3adbaa32023-03-15 15:15:47 +0100462 goto err_put;
Marek Vasutbf7571c2022-07-30 20:05:00 +0200463
464 priv->pwr_opmode = ret;
465
466 /* Initialize the hardware with the devicetree settings. */
467 ret = tusb320_set_adv_pwr_mode(priv);
468 if (ret)
Alvin Šipraga3adbaa32023-03-15 15:15:47 +0100469 goto err_put;
Marek Vasutbf7571c2022-07-30 20:05:00 +0200470
471 priv->cap.revision = USB_TYPEC_REV_1_1;
472 priv->cap.accessory[0] = TYPEC_ACCESSORY_AUDIO;
473 priv->cap.accessory[1] = TYPEC_ACCESSORY_DEBUG;
474 priv->cap.orientation_aware = true;
475 priv->cap.driver_data = priv;
476 priv->cap.ops = &tusb320_typec_ops;
477 priv->cap.fwnode = connector;
478
479 priv->port = typec_register_port(&client->dev, &priv->cap);
Alvin Šipraga3adbaa32023-03-15 15:15:47 +0100480 if (IS_ERR(priv->port)) {
481 ret = PTR_ERR(priv->port);
482 goto err_put;
483 }
484
Alvin Šipragadf101442023-03-18 16:05:54 +0100485 /* Find any optional USB role switch that needs reporting to */
486 priv->role_sw = fwnode_usb_role_switch_get(connector);
487 if (IS_ERR(priv->role_sw)) {
488 ret = PTR_ERR(priv->role_sw);
489 goto err_unreg;
490 }
491
Alvin Šipraga3adbaa32023-03-15 15:15:47 +0100492 priv->connector_fwnode = connector;
Marek Vasutbf7571c2022-07-30 20:05:00 +0200493
494 return 0;
Alvin Šipraga3adbaa32023-03-15 15:15:47 +0100495
Alvin Šipragadf101442023-03-18 16:05:54 +0100496err_unreg:
497 typec_unregister_port(priv->port);
498
Alvin Šipraga3adbaa32023-03-15 15:15:47 +0100499err_put:
500 fwnode_handle_put(connector);
501
502 return ret;
503}
504
505static void tusb320_typec_remove(struct tusb320_priv *priv)
506{
Alvin Šipragadf101442023-03-18 16:05:54 +0100507 usb_role_switch_put(priv->role_sw);
Alvin Šipraga3adbaa32023-03-15 15:15:47 +0100508 typec_unregister_port(priv->port);
509 fwnode_handle_put(priv->connector_fwnode);
Marek Vasutbf7571c2022-07-30 20:05:00 +0200510}
511
Uwe Kleine-König53131212022-11-18 23:35:44 +0100512static int tusb320_probe(struct i2c_client *client)
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500513{
514 struct tusb320_priv *priv;
Yassine Oudjanace0320b2021-09-25 05:45:54 +0000515 const void *match_data;
516 unsigned int revision;
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500517 int ret;
518
519 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
520 if (!priv)
521 return -ENOMEM;
Alvin Šipraga3adbaa32023-03-15 15:15:47 +0100522
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500523 priv->dev = &client->dev;
Alvin Šipraga3adbaa32023-03-15 15:15:47 +0100524 i2c_set_clientdata(client, priv);
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500525
526 priv->regmap = devm_regmap_init_i2c(client, &tusb320_regmap_config);
527 if (IS_ERR(priv->regmap))
528 return PTR_ERR(priv->regmap);
529
530 ret = tusb320_check_signature(priv);
531 if (ret)
532 return ret;
533
Yassine Oudjanace0320b2021-09-25 05:45:54 +0000534 match_data = device_get_match_data(&client->dev);
535 if (!match_data)
536 return -EINVAL;
537
538 priv->ops = (struct tusb320_ops*)match_data;
539
Yassine Oudjanace0320b2021-09-25 05:45:54 +0000540 if (priv->ops->get_revision) {
541 ret = priv->ops->get_revision(priv, &revision);
542 if (ret)
543 dev_warn(priv->dev,
544 "failed to read revision register: %d\n", ret);
545 else
546 dev_info(priv->dev, "chip revision %d\n", revision);
547 }
548
Marek Vasut94838112022-07-30 20:04:59 +0200549 ret = tusb320_extcon_probe(priv);
550 if (ret)
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500551 return ret;
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500552
Marek Vasutbf7571c2022-07-30 20:05:00 +0200553 ret = tusb320_typec_probe(client, priv);
554 if (ret)
555 return ret;
556
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500557 /* update initial state */
Marek Vasut581c8482022-11-20 15:15:09 +0100558 tusb320_state_update_handler(priv, true);
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500559
Yassine Oudjana70c55d62021-09-25 05:45:39 +0000560 /* Reset chip to its default state */
561 ret = tusb320_reset(priv);
562 if (ret)
563 dev_warn(priv->dev, "failed to reset chip: %d\n", ret);
564 else
565 /*
566 * State and polarity might change after a reset, so update
567 * them again and make sure the interrupt status bit is cleared.
568 */
Marek Vasut581c8482022-11-20 15:15:09 +0100569 tusb320_state_update_handler(priv, true);
Yassine Oudjana70c55d62021-09-25 05:45:39 +0000570
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500571 ret = devm_request_threaded_irq(priv->dev, client->irq, NULL,
572 tusb320_irq_handler,
573 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
574 client->name, priv);
Alvin Šipraga3adbaa32023-03-15 15:15:47 +0100575 if (ret)
576 tusb320_typec_remove(priv);
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500577
578 return ret;
579}
580
Alvin Šipraga3adbaa32023-03-15 15:15:47 +0100581static void tusb320_remove(struct i2c_client *client)
582{
583 struct tusb320_priv *priv = i2c_get_clientdata(client);
584
585 tusb320_typec_remove(priv);
586}
587
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500588static const struct of_device_id tusb320_extcon_dt_match[] = {
Yassine Oudjanace0320b2021-09-25 05:45:54 +0000589 { .compatible = "ti,tusb320", .data = &tusb320_ops, },
590 { .compatible = "ti,tusb320l", .data = &tusb320l_ops, },
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500591 { }
592};
593MODULE_DEVICE_TABLE(of, tusb320_extcon_dt_match);
594
595static struct i2c_driver tusb320_extcon_driver = {
Uwe Kleine-Königbcfa8e32023-05-30 07:17:26 +0200596 .probe = tusb320_probe,
Alvin Šipraga3adbaa32023-03-15 15:15:47 +0100597 .remove = tusb320_remove,
Michael Auchter06bc4ca2020-10-15 09:07:34 -0500598 .driver = {
599 .name = "extcon-tusb320",
600 .of_match_table = tusb320_extcon_dt_match,
601 },
602};
603
604static int __init tusb320_init(void)
605{
606 return i2c_add_driver(&tusb320_extcon_driver);
607}
608subsys_initcall(tusb320_init);
609
610static void __exit tusb320_exit(void)
611{
612 i2c_del_driver(&tusb320_extcon_driver);
613}
614module_exit(tusb320_exit);
615
616MODULE_AUTHOR("Michael Auchter <michael.auchter@ni.com>");
617MODULE_DESCRIPTION("TI TUSB320 extcon driver");
618MODULE_LICENSE("GPL v2");