1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* |
3 | * 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 | |
9 | #include <linux/bitfield.h> |
10 | #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> |
17 | #include <linux/usb/typec.h> |
18 | #include <linux/usb/typec_altmode.h> |
19 | #include <linux/usb/role.h> |
20 | |
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 |
31 | #define TUSB320_REG8_ACCESSORY_CONNECTED GENMASK(3, 1) |
32 | #define TUSB320_REG8_ACCESSORY_CONNECTED_NONE 0x0 |
33 | #define TUSB320_REG8_ACCESSORY_CONNECTED_AUDIO 0x4 |
34 | #define TUSB320_REG8_ACCESSORY_CONNECTED_ACHRG 0x5 |
35 | #define TUSB320_REG8_ACCESSORY_CONNECTED_DBGDFP 0x6 |
36 | #define TUSB320_REG8_ACCESSORY_CONNECTED_DBGUFP 0x7 |
37 | #define TUSB320_REG8_ACTIVE_CABLE_DETECTION BIT(0) |
38 | |
39 | #define TUSB320_REG9 0x9 |
40 | #define TUSB320_REG9_ATTACHED_STATE GENMASK(7, 6) |
41 | #define TUSB320_REG9_CABLE_DIRECTION BIT(5) |
42 | #define TUSB320_REG9_INTERRUPT_STATUS BIT(4) |
43 | |
44 | #define TUSB320_REGA 0xa |
45 | #define TUSB320L_REGA_DISABLE_TERM BIT(0) |
46 | #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 | |
50 | #define TUSB320L_REGA0_REVISION 0xa0 |
51 | |
52 | enum 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 | |
59 | enum tusb320_mode { |
60 | TUSB320_MODE_PORT, |
61 | TUSB320_MODE_UFP, |
62 | TUSB320_MODE_DFP, |
63 | TUSB320_MODE_DRP, |
64 | }; |
65 | |
66 | struct tusb320_priv; |
67 | |
68 | struct 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 | |
73 | struct tusb320_priv { |
74 | struct device *dev; |
75 | struct regmap *regmap; |
76 | struct extcon_dev *edev; |
77 | struct tusb320_ops *ops; |
78 | enum tusb320_attached_state state; |
79 | struct typec_port *port; |
80 | struct typec_capability cap; |
81 | enum typec_port_type port_type; |
82 | enum typec_pwr_opmode pwr_opmode; |
83 | struct fwnode_handle *connector_fwnode; |
84 | struct usb_role_switch *role_sw; |
85 | }; |
86 | |
87 | static 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 | |
94 | static const unsigned int tusb320_extcon_cable[] = { |
95 | EXTCON_USB, |
96 | EXTCON_USB_HOST, |
97 | EXTCON_NONE, |
98 | }; |
99 | |
100 | static 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(map: priv->regmap, reg: sizeof(sig) - 1 - i, val: &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 | |
119 | static 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(map: priv->regmap, TUSB320_REGA, |
129 | TUSB320_REGA_MODE_SELECT_MASK << TUSB320_REGA_MODE_SELECT_SHIFT, |
130 | val: 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 | |
139 | static 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(map: priv->regmap, TUSB320_REGA, |
145 | TUSB320L_REGA_DISABLE_TERM, val: 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(map: priv->regmap, TUSB320_REGA, |
154 | TUSB320_REGA_MODE_SELECT_MASK << TUSB320_REGA_MODE_SELECT_SHIFT, |
155 | val: 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(msecs: 5); |
162 | err: |
163 | /* Re-enable CC state machine */ |
164 | ret = regmap_write_bits(map: priv->regmap, TUSB320_REGA, |
165 | TUSB320L_REGA_DISABLE_TERM, val: 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 | |
173 | static int tusb320_reset(struct tusb320_priv *priv) |
174 | { |
175 | int ret; |
176 | |
177 | /* Set mode to default (follow PORT pin) */ |
178 | ret = priv->ops->set_mode(priv, TUSB320_MODE_PORT); |
179 | 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(map: priv->regmap, TUSB320_REGA, |
187 | TUSB320_REGA_I2C_SOFT_RESET, val: 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(msecs: 95); |
196 | |
197 | return 0; |
198 | } |
199 | |
200 | static int tusb320l_get_revision(struct tusb320_priv *priv, unsigned int *revision) |
201 | { |
202 | return regmap_read(map: priv->regmap, TUSB320L_REGA0_REVISION, val: revision); |
203 | } |
204 | |
205 | static struct tusb320_ops tusb320_ops = { |
206 | .set_mode = tusb320_set_mode, |
207 | }; |
208 | |
209 | static struct tusb320_ops tusb320l_ops = { |
210 | .set_mode = tusb320l_set_mode, |
211 | .get_revision = tusb320l_get_revision, |
212 | }; |
213 | |
214 | static 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(map: priv->regmap, TUSB320_REG8, |
228 | TUSB320_REG8_CURRENT_MODE_ADVERTISE, |
229 | FIELD_PREP(TUSB320_REG8_CURRENT_MODE_ADVERTISE, |
230 | mode)); |
231 | } |
232 | |
233 | static 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 | |
248 | static const struct typec_operations tusb320_typec_ops = { |
249 | .port_type_set = tusb320_port_type_set, |
250 | }; |
251 | |
252 | static void tusb320_extcon_irq_handler(struct tusb320_priv *priv, u8 reg) |
253 | { |
254 | int state, polarity; |
255 | |
256 | state = FIELD_GET(TUSB320_REG9_ATTACHED_STATE, reg); |
257 | 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(edev: priv->edev, EXTCON_USB, |
263 | state: state == TUSB320_ATTACHED_STATE_UFP); |
264 | extcon_set_state(edev: priv->edev, EXTCON_USB_HOST, |
265 | state: state == TUSB320_ATTACHED_STATE_DFP); |
266 | extcon_set_property(edev: priv->edev, EXTCON_USB, |
267 | EXTCON_PROP_USB_TYPEC_POLARITY, |
268 | prop_val: (union extcon_property_value)polarity); |
269 | extcon_set_property(edev: priv->edev, EXTCON_USB_HOST, |
270 | EXTCON_PROP_USB_TYPEC_POLARITY, |
271 | prop_val: (union extcon_property_value)polarity); |
272 | extcon_sync(edev: priv->edev, EXTCON_USB); |
273 | extcon_sync(edev: priv->edev, EXTCON_USB_HOST); |
274 | |
275 | priv->state = state; |
276 | } |
277 | |
278 | static 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; |
282 | int typec_mode; |
283 | enum usb_role usb_role; |
284 | enum typec_role pwr_role; |
285 | enum typec_data_role data_role; |
286 | u8 state, mode, accessory; |
287 | int ret, reg8; |
288 | bool ori; |
289 | |
290 | ret = regmap_read(map: priv->regmap, TUSB320_REG8, val: ®8); |
291 | if (ret) { |
292 | dev_err(dev, "error during reg8 i2c read, ret=%d!\n" , ret); |
293 | return; |
294 | } |
295 | |
296 | ori = reg9 & TUSB320_REG9_CABLE_DIRECTION; |
297 | typec_set_orientation(port, orientation: 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; |
306 | usb_role = USB_ROLE_HOST; |
307 | pwr_role = TYPEC_SOURCE; |
308 | data_role = TYPEC_HOST; |
309 | break; |
310 | case TUSB320_ATTACHED_STATE_UFP: |
311 | typec_mode = TYPEC_MODE_USB2; |
312 | usb_role = USB_ROLE_DEVICE; |
313 | 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; |
324 | usb_role = USB_ROLE_NONE; |
325 | 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; |
332 | usb_role = USB_ROLE_HOST; |
333 | 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; |
339 | usb_role = USB_ROLE_DEVICE; |
340 | 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; |
350 | usb_role = USB_ROLE_NONE; |
351 | pwr_role = TYPEC_SINK; |
352 | data_role = TYPEC_DEVICE; |
353 | break; |
354 | } |
355 | |
356 | typec_set_vconn_role(port, role: pwr_role); |
357 | typec_set_pwr_role(port, role: pwr_role); |
358 | typec_set_data_role(port, role: data_role); |
359 | typec_set_mode(port, mode: typec_mode); |
360 | usb_role_switch_set_role(sw: priv->role_sw, role: usb_role); |
361 | |
362 | mode = FIELD_GET(TUSB320_REG8_CURRENT_MODE_DETECT, reg8); |
363 | if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_DEF) |
364 | typec_set_pwr_opmode(port, mode: TYPEC_PWR_MODE_USB); |
365 | else if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_MED) |
366 | typec_set_pwr_opmode(port, mode: TYPEC_PWR_MODE_1_5A); |
367 | else if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_HI) |
368 | typec_set_pwr_opmode(port, mode: TYPEC_PWR_MODE_3_0A); |
369 | else /* Charge through accessory */ |
370 | typec_set_pwr_opmode(port, mode: TYPEC_PWR_MODE_USB); |
371 | } |
372 | |
373 | static irqreturn_t tusb320_state_update_handler(struct tusb320_priv *priv, |
374 | bool force_update) |
375 | { |
376 | unsigned int reg; |
377 | |
378 | if (regmap_read(map: priv->regmap, TUSB320_REG9, val: ®)) { |
379 | dev_err(priv->dev, "error during i2c read!\n" ); |
380 | return IRQ_NONE; |
381 | } |
382 | |
383 | if (!force_update && !(reg & TUSB320_REG9_INTERRUPT_STATUS)) |
384 | return IRQ_NONE; |
385 | |
386 | tusb320_extcon_irq_handler(priv, reg); |
387 | |
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, reg9: reg); |
394 | |
395 | regmap_write(map: priv->regmap, TUSB320_REG9, val: reg); |
396 | |
397 | return IRQ_HANDLED; |
398 | } |
399 | |
400 | static 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, force_update: false); |
405 | } |
406 | |
407 | static const struct regmap_config tusb320_regmap_config = { |
408 | .reg_bits = 8, |
409 | .val_bits = 8, |
410 | }; |
411 | |
412 | static int tusb320_extcon_probe(struct tusb320_priv *priv) |
413 | { |
414 | int ret; |
415 | |
416 | priv->edev = devm_extcon_dev_allocate(dev: priv->dev, cable: tusb320_extcon_cable); |
417 | if (IS_ERR(ptr: priv->edev)) { |
418 | dev_err(priv->dev, "failed to allocate extcon device\n" ); |
419 | return PTR_ERR(ptr: priv->edev); |
420 | } |
421 | |
422 | ret = devm_extcon_dev_register(dev: priv->dev, edev: 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(edev: priv->edev, EXTCON_USB, |
429 | EXTCON_PROP_USB_TYPEC_POLARITY); |
430 | extcon_set_property_capability(edev: priv->edev, EXTCON_USB_HOST, |
431 | EXTCON_PROP_USB_TYPEC_POLARITY); |
432 | |
433 | return 0; |
434 | } |
435 | |
436 | static 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(dev: &client->dev, childname: "connector" ); |
445 | if (!connector) |
446 | return 0; |
447 | |
448 | /* Type-C connector found. */ |
449 | ret = typec_get_fw_cap(cap: &priv->cap, fwnode: connector); |
450 | if (ret) |
451 | goto err_put; |
452 | |
453 | priv->port_type = priv->cap.type; |
454 | |
455 | /* This goes into register 0x8 field CURRENT_MODE_ADVERTISE */ |
456 | ret = fwnode_property_read_string(fwnode: connector, propname: "typec-power-opmode" , val: &cap_str); |
457 | if (ret) |
458 | goto err_put; |
459 | |
460 | ret = typec_find_pwr_opmode(name: cap_str); |
461 | if (ret < 0) |
462 | goto err_put; |
463 | |
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) |
469 | goto err_put; |
470 | |
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(parent: &client->dev, cap: &priv->cap); |
480 | if (IS_ERR(ptr: priv->port)) { |
481 | ret = PTR_ERR(ptr: priv->port); |
482 | goto err_put; |
483 | } |
484 | |
485 | /* Find any optional USB role switch that needs reporting to */ |
486 | priv->role_sw = fwnode_usb_role_switch_get(node: connector); |
487 | if (IS_ERR(ptr: priv->role_sw)) { |
488 | ret = PTR_ERR(ptr: priv->role_sw); |
489 | goto err_unreg; |
490 | } |
491 | |
492 | priv->connector_fwnode = connector; |
493 | |
494 | return 0; |
495 | |
496 | err_unreg: |
497 | typec_unregister_port(port: priv->port); |
498 | |
499 | err_put: |
500 | fwnode_handle_put(fwnode: connector); |
501 | |
502 | return ret; |
503 | } |
504 | |
505 | static void tusb320_typec_remove(struct tusb320_priv *priv) |
506 | { |
507 | usb_role_switch_put(sw: priv->role_sw); |
508 | typec_unregister_port(port: priv->port); |
509 | fwnode_handle_put(fwnode: priv->connector_fwnode); |
510 | } |
511 | |
512 | static int tusb320_probe(struct i2c_client *client) |
513 | { |
514 | struct tusb320_priv *priv; |
515 | const void *match_data; |
516 | unsigned int revision; |
517 | int ret; |
518 | |
519 | priv = devm_kzalloc(dev: &client->dev, size: sizeof(*priv), GFP_KERNEL); |
520 | if (!priv) |
521 | return -ENOMEM; |
522 | |
523 | priv->dev = &client->dev; |
524 | i2c_set_clientdata(client, data: priv); |
525 | |
526 | priv->regmap = devm_regmap_init_i2c(client, &tusb320_regmap_config); |
527 | if (IS_ERR(ptr: priv->regmap)) |
528 | return PTR_ERR(ptr: priv->regmap); |
529 | |
530 | ret = tusb320_check_signature(priv); |
531 | if (ret) |
532 | return ret; |
533 | |
534 | match_data = device_get_match_data(dev: &client->dev); |
535 | if (!match_data) |
536 | return -EINVAL; |
537 | |
538 | priv->ops = (struct tusb320_ops*)match_data; |
539 | |
540 | 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 | |
549 | ret = tusb320_extcon_probe(priv); |
550 | if (ret) |
551 | return ret; |
552 | |
553 | ret = tusb320_typec_probe(client, priv); |
554 | if (ret) |
555 | return ret; |
556 | |
557 | /* update initial state */ |
558 | tusb320_state_update_handler(priv, force_update: true); |
559 | |
560 | /* 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 | */ |
569 | tusb320_state_update_handler(priv, force_update: true); |
570 | |
571 | ret = devm_request_threaded_irq(dev: priv->dev, irq: client->irq, NULL, |
572 | thread_fn: tusb320_irq_handler, |
573 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
574 | devname: client->name, dev_id: priv); |
575 | if (ret) |
576 | tusb320_typec_remove(priv); |
577 | |
578 | return ret; |
579 | } |
580 | |
581 | static 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 | |
588 | static const struct of_device_id tusb320_extcon_dt_match[] = { |
589 | { .compatible = "ti,tusb320" , .data = &tusb320_ops, }, |
590 | { .compatible = "ti,tusb320l" , .data = &tusb320l_ops, }, |
591 | { } |
592 | }; |
593 | MODULE_DEVICE_TABLE(of, tusb320_extcon_dt_match); |
594 | |
595 | static struct i2c_driver tusb320_extcon_driver = { |
596 | .probe = tusb320_probe, |
597 | .remove = tusb320_remove, |
598 | .driver = { |
599 | .name = "extcon-tusb320" , |
600 | .of_match_table = tusb320_extcon_dt_match, |
601 | }, |
602 | }; |
603 | |
604 | static int __init tusb320_init(void) |
605 | { |
606 | return i2c_add_driver(&tusb320_extcon_driver); |
607 | } |
608 | subsys_initcall(tusb320_init); |
609 | |
610 | static void __exit tusb320_exit(void) |
611 | { |
612 | i2c_del_driver(driver: &tusb320_extcon_driver); |
613 | } |
614 | module_exit(tusb320_exit); |
615 | |
616 | MODULE_AUTHOR("Michael Auchter <michael.auchter@ni.com>" ); |
617 | MODULE_DESCRIPTION("TI TUSB320 extcon driver" ); |
618 | MODULE_LICENSE("GPL v2" ); |
619 | |