1 // SPDX-License-Identifier: GPL-2.0+
3 // extcon-ptn5150.c - PTN5150 CC logic extcon driver to support USB detection
5 // Based on extcon-sm5502.c driver
6 // Copyright (c) 2018-2019 by Vijai Kumar K
7 // Author: Vijai Kumar K <vijaikumar.kanagarajan@gmail.com>
8 // Copyright (c) 2020 Krzysztof Kozlowski <krzk@kernel.org>
10 #include <linux/bitfield.h>
11 #include <linux/err.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/regmap.h>
17 #include <linux/slab.h>
18 #include <linux/extcon-provider.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/usb/role.h>
22 /* PTN5150 registers */
23 #define PTN5150_REG_DEVICE_ID 0x01
24 #define PTN5150_REG_CONTROL 0x02
25 #define PTN5150_REG_INT_STATUS 0x03
26 #define PTN5150_REG_CC_STATUS 0x04
27 #define PTN5150_REG_CON_DET 0x09
28 #define PTN5150_REG_VCONN_STATUS 0x0a
29 #define PTN5150_REG_RESET 0x0b
30 #define PTN5150_REG_INT_MASK 0x18
31 #define PTN5150_REG_INT_REG_STATUS 0x19
32 #define PTN5150_REG_END PTN5150_REG_INT_REG_STATUS
34 #define PTN5150_DFP_ATTACHED 0x1
35 #define PTN5150_UFP_ATTACHED 0x2
37 /* Define PTN5150 MASK/SHIFT constant */
38 #define PTN5150_REG_DEVICE_ID_VERSION GENMASK(7, 3)
39 #define PTN5150_REG_DEVICE_ID_VENDOR GENMASK(2, 0)
41 #define PTN5150_REG_CC_PORT_ATTACHMENT GENMASK(4, 2)
42 #define PTN5150_REG_CC_VBUS_DETECTION BIT(7)
43 #define PTN5150_REG_INT_CABLE_ATTACH_MASK BIT(0)
44 #define PTN5150_REG_INT_CABLE_DETACH_MASK BIT(1)
48 struct extcon_dev
*edev
;
49 struct i2c_client
*i2c
;
50 struct regmap
*regmap
;
51 struct gpio_desc
*int_gpiod
;
52 struct gpio_desc
*vbus_gpiod
;
54 struct work_struct irq_work
;
56 struct usb_role_switch
*role_sw
;
59 /* List of detectable cables */
60 static const unsigned int ptn5150_extcon_cable
[] = {
66 static const struct regmap_config ptn5150_regmap_config
= {
69 .max_register
= PTN5150_REG_END
,
72 static void ptn5150_check_state(struct ptn5150_info
*info
)
74 unsigned int port_status
, reg_data
, vbus
;
75 enum usb_role usb_role
= USB_ROLE_NONE
;
78 ret
= regmap_read(info
->regmap
, PTN5150_REG_CC_STATUS
, ®_data
);
80 dev_err(info
->dev
, "failed to read CC STATUS %d\n", ret
);
84 port_status
= FIELD_GET(PTN5150_REG_CC_PORT_ATTACHMENT
, reg_data
);
86 switch (port_status
) {
87 case PTN5150_DFP_ATTACHED
:
88 extcon_set_state_sync(info
->edev
, EXTCON_USB_HOST
, false);
89 gpiod_set_value_cansleep(info
->vbus_gpiod
, 0);
90 extcon_set_state_sync(info
->edev
, EXTCON_USB
, true);
91 usb_role
= USB_ROLE_DEVICE
;
93 case PTN5150_UFP_ATTACHED
:
94 extcon_set_state_sync(info
->edev
, EXTCON_USB
, false);
95 vbus
= FIELD_GET(PTN5150_REG_CC_VBUS_DETECTION
, reg_data
);
97 gpiod_set_value_cansleep(info
->vbus_gpiod
, 0);
99 gpiod_set_value_cansleep(info
->vbus_gpiod
, 1);
101 extcon_set_state_sync(info
->edev
, EXTCON_USB_HOST
, true);
102 usb_role
= USB_ROLE_HOST
;
109 ret
= usb_role_switch_set_role(info
->role_sw
, usb_role
);
111 dev_err(info
->dev
, "failed to set %s role: %d\n",
112 usb_role_string(usb_role
), ret
);
116 static void ptn5150_irq_work(struct work_struct
*work
)
118 struct ptn5150_info
*info
= container_of(work
,
119 struct ptn5150_info
, irq_work
);
121 unsigned int int_status
;
126 mutex_lock(&info
->mutex
);
128 /* Clear interrupt. Read would clear the register */
129 ret
= regmap_read(info
->regmap
, PTN5150_REG_INT_STATUS
, &int_status
);
131 dev_err(info
->dev
, "failed to read INT STATUS %d\n", ret
);
132 mutex_unlock(&info
->mutex
);
137 unsigned int cable_attach
;
139 cable_attach
= int_status
& PTN5150_REG_INT_CABLE_ATTACH_MASK
;
141 ptn5150_check_state(info
);
143 extcon_set_state_sync(info
->edev
,
144 EXTCON_USB_HOST
, false);
145 extcon_set_state_sync(info
->edev
,
147 gpiod_set_value_cansleep(info
->vbus_gpiod
, 0);
149 ret
= usb_role_switch_set_role(info
->role_sw
,
153 "failed to set none role: %d\n",
158 /* Clear interrupt. Read would clear the register */
159 ret
= regmap_read(info
->regmap
, PTN5150_REG_INT_REG_STATUS
,
163 "failed to read INT REG STATUS %d\n", ret
);
164 mutex_unlock(&info
->mutex
);
168 mutex_unlock(&info
->mutex
);
172 static irqreturn_t
ptn5150_irq_handler(int irq
, void *data
)
174 struct ptn5150_info
*info
= data
;
176 schedule_work(&info
->irq_work
);
181 static int ptn5150_init_dev_type(struct ptn5150_info
*info
)
183 unsigned int reg_data
, vendor_id
, version_id
;
186 ret
= regmap_read(info
->regmap
, PTN5150_REG_DEVICE_ID
, ®_data
);
188 dev_err(info
->dev
, "failed to read DEVICE_ID %d\n", ret
);
192 vendor_id
= FIELD_GET(PTN5150_REG_DEVICE_ID_VENDOR
, reg_data
);
193 version_id
= FIELD_GET(PTN5150_REG_DEVICE_ID_VERSION
, reg_data
);
194 dev_dbg(info
->dev
, "Device type: version: 0x%x, vendor: 0x%x\n",
195 version_id
, vendor_id
);
197 /* Clear any existing interrupts */
198 ret
= regmap_read(info
->regmap
, PTN5150_REG_INT_STATUS
, ®_data
);
201 "failed to read PTN5150_REG_INT_STATUS %d\n",
206 ret
= regmap_read(info
->regmap
, PTN5150_REG_INT_REG_STATUS
, ®_data
);
209 "failed to read PTN5150_REG_INT_REG_STATUS %d\n", ret
);
216 static void ptn5150_work_sync_and_put(void *data
)
218 struct ptn5150_info
*info
= data
;
220 cancel_work_sync(&info
->irq_work
);
221 usb_role_switch_put(info
->role_sw
);
224 static int ptn5150_i2c_probe(struct i2c_client
*i2c
)
226 struct device
*dev
= &i2c
->dev
;
227 struct device_node
*np
= i2c
->dev
.of_node
;
228 struct ptn5150_info
*info
;
234 info
= devm_kzalloc(&i2c
->dev
, sizeof(*info
), GFP_KERNEL
);
237 i2c_set_clientdata(i2c
, info
);
239 info
->dev
= &i2c
->dev
;
241 info
->vbus_gpiod
= devm_gpiod_get(&i2c
->dev
, "vbus", GPIOD_OUT_LOW
);
242 if (IS_ERR(info
->vbus_gpiod
)) {
243 ret
= PTR_ERR(info
->vbus_gpiod
);
244 if (ret
== -ENOENT
) {
245 dev_info(dev
, "No VBUS GPIO, ignoring VBUS control\n");
246 info
->vbus_gpiod
= NULL
;
248 return dev_err_probe(dev
, ret
, "failed to get VBUS GPIO\n");
252 mutex_init(&info
->mutex
);
254 INIT_WORK(&info
->irq_work
, ptn5150_irq_work
);
256 info
->regmap
= devm_regmap_init_i2c(i2c
, &ptn5150_regmap_config
);
257 if (IS_ERR(info
->regmap
)) {
258 return dev_err_probe(info
->dev
, PTR_ERR(info
->regmap
),
259 "failed to allocate register map\n");
263 info
->irq
= i2c
->irq
;
265 info
->int_gpiod
= devm_gpiod_get(&i2c
->dev
, "int", GPIOD_IN
);
266 if (IS_ERR(info
->int_gpiod
)) {
267 return dev_err_probe(dev
, PTR_ERR(info
->int_gpiod
),
268 "failed to get INT GPIO\n");
271 info
->irq
= gpiod_to_irq(info
->int_gpiod
);
273 dev_err(dev
, "failed to get INTB IRQ\n");
278 ret
= devm_request_threaded_irq(dev
, info
->irq
, NULL
,
280 IRQF_TRIGGER_FALLING
|
284 dev_err(dev
, "failed to request handler for INTB IRQ\n");
288 /* Allocate extcon device */
289 info
->edev
= devm_extcon_dev_allocate(info
->dev
, ptn5150_extcon_cable
);
290 if (IS_ERR(info
->edev
)) {
291 dev_err(info
->dev
, "failed to allocate memory for extcon\n");
295 /* Register extcon device */
296 ret
= devm_extcon_dev_register(info
->dev
, info
->edev
);
298 dev_err(info
->dev
, "failed to register extcon device\n");
302 extcon_set_property_capability(info
->edev
, EXTCON_USB
,
303 EXTCON_PROP_USB_VBUS
);
304 extcon_set_property_capability(info
->edev
, EXTCON_USB_HOST
,
305 EXTCON_PROP_USB_VBUS
);
306 extcon_set_property_capability(info
->edev
, EXTCON_USB_HOST
,
307 EXTCON_PROP_USB_TYPEC_POLARITY
);
309 /* Initialize PTN5150 device and print vendor id and version id */
310 ret
= ptn5150_init_dev_type(info
);
314 info
->role_sw
= usb_role_switch_get(info
->dev
);
315 if (IS_ERR(info
->role_sw
))
316 return dev_err_probe(info
->dev
, PTR_ERR(info
->role_sw
),
317 "failed to get role switch\n");
319 ret
= devm_add_action_or_reset(dev
, ptn5150_work_sync_and_put
, info
);
324 * Update current extcon state if for example OTG connection was there
327 mutex_lock(&info
->mutex
);
328 ptn5150_check_state(info
);
329 mutex_unlock(&info
->mutex
);
334 static const struct of_device_id ptn5150_dt_match
[] = {
335 { .compatible
= "nxp,ptn5150" },
338 MODULE_DEVICE_TABLE(of
, ptn5150_dt_match
);
340 static const struct i2c_device_id ptn5150_i2c_id
[] = {
344 MODULE_DEVICE_TABLE(i2c
, ptn5150_i2c_id
);
346 static struct i2c_driver ptn5150_i2c_driver
= {
349 .of_match_table
= ptn5150_dt_match
,
351 .probe
= ptn5150_i2c_probe
,
352 .id_table
= ptn5150_i2c_id
,
354 module_i2c_driver(ptn5150_i2c_driver
);
356 MODULE_DESCRIPTION("NXP PTN5150 CC logic Extcon driver");
357 MODULE_AUTHOR("Vijai Kumar K <vijaikumar.kanagarajan@gmail.com>");
358 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
359 MODULE_LICENSE("GPL v2");