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>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/regmap.h>
15 #include <linux/slab.h>
16 #include <linux/extcon-provider.h>
17 #include <linux/gpio/consumer.h>
19 /* PTN5150 registers */
21 PTN5150_REG_DEVICE_ID
= 0x01,
23 PTN5150_REG_INT_STATUS
,
24 PTN5150_REG_CC_STATUS
,
25 PTN5150_REG_CON_DET
= 0x09,
26 PTN5150_REG_VCONN_STATUS
,
28 PTN5150_REG_INT_MASK
= 0x18,
29 PTN5150_REG_INT_REG_STATUS
,
33 #define PTN5150_DFP_ATTACHED 0x1
34 #define PTN5150_UFP_ATTACHED 0x2
36 /* Define PTN5150 MASK/SHIFT constant */
37 #define PTN5150_REG_DEVICE_ID_VENDOR_SHIFT 0
38 #define PTN5150_REG_DEVICE_ID_VENDOR_MASK \
39 (0x3 << PTN5150_REG_DEVICE_ID_VENDOR_SHIFT)
41 #define PTN5150_REG_DEVICE_ID_VERSION_SHIFT 3
42 #define PTN5150_REG_DEVICE_ID_VERSION_MASK \
43 (0x1f << PTN5150_REG_DEVICE_ID_VERSION_SHIFT)
45 #define PTN5150_REG_CC_PORT_ATTACHMENT_SHIFT 2
46 #define PTN5150_REG_CC_PORT_ATTACHMENT_MASK \
47 (0x7 << PTN5150_REG_CC_PORT_ATTACHMENT_SHIFT)
49 #define PTN5150_REG_CC_VBUS_DETECTION_SHIFT 7
50 #define PTN5150_REG_CC_VBUS_DETECTION_MASK \
51 (0x1 << PTN5150_REG_CC_VBUS_DETECTION_SHIFT)
53 #define PTN5150_REG_INT_CABLE_ATTACH_SHIFT 0
54 #define PTN5150_REG_INT_CABLE_ATTACH_MASK \
55 (0x1 << PTN5150_REG_INT_CABLE_ATTACH_SHIFT)
57 #define PTN5150_REG_INT_CABLE_DETACH_SHIFT 1
58 #define PTN5150_REG_INT_CABLE_DETACH_MASK \
59 (0x1 << PTN5150_REG_CC_CABLE_DETACH_SHIFT)
63 struct extcon_dev
*edev
;
64 struct i2c_client
*i2c
;
65 struct regmap
*regmap
;
66 struct gpio_desc
*int_gpiod
;
67 struct gpio_desc
*vbus_gpiod
;
69 struct work_struct irq_work
;
73 /* List of detectable cables */
74 static const unsigned int ptn5150_extcon_cable
[] = {
80 static const struct regmap_config ptn5150_regmap_config
= {
83 .max_register
= PTN5150_REG_END
,
86 static void ptn5150_irq_work(struct work_struct
*work
)
88 struct ptn5150_info
*info
= container_of(work
,
89 struct ptn5150_info
, irq_work
);
91 unsigned int reg_data
;
92 unsigned int int_status
;
97 mutex_lock(&info
->mutex
);
99 ret
= regmap_read(info
->regmap
, PTN5150_REG_CC_STATUS
, ®_data
);
101 dev_err(info
->dev
, "failed to read CC STATUS %d\n", ret
);
102 mutex_unlock(&info
->mutex
);
106 /* Clear interrupt. Read would clear the register */
107 ret
= regmap_read(info
->regmap
, PTN5150_REG_INT_STATUS
, &int_status
);
109 dev_err(info
->dev
, "failed to read INT STATUS %d\n", ret
);
110 mutex_unlock(&info
->mutex
);
115 unsigned int cable_attach
;
117 cable_attach
= int_status
& PTN5150_REG_INT_CABLE_ATTACH_MASK
;
119 unsigned int port_status
;
122 port_status
= ((reg_data
&
123 PTN5150_REG_CC_PORT_ATTACHMENT_MASK
) >>
124 PTN5150_REG_CC_PORT_ATTACHMENT_SHIFT
);
126 switch (port_status
) {
127 case PTN5150_DFP_ATTACHED
:
128 extcon_set_state_sync(info
->edev
,
129 EXTCON_USB_HOST
, false);
130 gpiod_set_value(info
->vbus_gpiod
, 0);
131 extcon_set_state_sync(info
->edev
, EXTCON_USB
,
134 case PTN5150_UFP_ATTACHED
:
135 extcon_set_state_sync(info
->edev
, EXTCON_USB
,
138 PTN5150_REG_CC_VBUS_DETECTION_MASK
) >>
139 PTN5150_REG_CC_VBUS_DETECTION_SHIFT
);
141 gpiod_set_value(info
->vbus_gpiod
, 0);
143 gpiod_set_value(info
->vbus_gpiod
, 1);
145 extcon_set_state_sync(info
->edev
,
146 EXTCON_USB_HOST
, true);
150 "Unknown Port status : %x\n",
155 extcon_set_state_sync(info
->edev
,
156 EXTCON_USB_HOST
, false);
157 extcon_set_state_sync(info
->edev
,
159 gpiod_set_value(info
->vbus_gpiod
, 0);
163 /* Clear interrupt. Read would clear the register */
164 ret
= regmap_read(info
->regmap
, PTN5150_REG_INT_REG_STATUS
,
168 "failed to read INT REG STATUS %d\n", ret
);
169 mutex_unlock(&info
->mutex
);
173 mutex_unlock(&info
->mutex
);
177 static irqreturn_t
ptn5150_irq_handler(int irq
, void *data
)
179 struct ptn5150_info
*info
= data
;
181 schedule_work(&info
->irq_work
);
186 static int ptn5150_init_dev_type(struct ptn5150_info
*info
)
188 unsigned int reg_data
, vendor_id
, version_id
;
191 ret
= regmap_read(info
->regmap
, PTN5150_REG_DEVICE_ID
, ®_data
);
193 dev_err(info
->dev
, "failed to read DEVICE_ID %d\n", ret
);
197 vendor_id
= ((reg_data
& PTN5150_REG_DEVICE_ID_VENDOR_MASK
) >>
198 PTN5150_REG_DEVICE_ID_VENDOR_SHIFT
);
199 version_id
= ((reg_data
& PTN5150_REG_DEVICE_ID_VERSION_MASK
) >>
200 PTN5150_REG_DEVICE_ID_VERSION_SHIFT
);
202 dev_info(info
->dev
, "Device type: version: 0x%x, vendor: 0x%x\n",
203 version_id
, vendor_id
);
205 /* Clear any existing interrupts */
206 ret
= regmap_read(info
->regmap
, PTN5150_REG_INT_STATUS
, ®_data
);
209 "failed to read PTN5150_REG_INT_STATUS %d\n",
214 ret
= regmap_read(info
->regmap
, PTN5150_REG_INT_REG_STATUS
, ®_data
);
217 "failed to read PTN5150_REG_INT_REG_STATUS %d\n", ret
);
224 static int ptn5150_i2c_probe(struct i2c_client
*i2c
,
225 const struct i2c_device_id
*id
)
227 struct device
*dev
= &i2c
->dev
;
228 struct device_node
*np
= i2c
->dev
.of_node
;
229 struct ptn5150_info
*info
;
235 info
= devm_kzalloc(&i2c
->dev
, sizeof(*info
), GFP_KERNEL
);
238 i2c_set_clientdata(i2c
, info
);
240 info
->dev
= &i2c
->dev
;
242 info
->int_gpiod
= devm_gpiod_get(&i2c
->dev
, "int", GPIOD_IN
);
243 if (IS_ERR(info
->int_gpiod
)) {
244 dev_err(dev
, "failed to get INT GPIO\n");
245 return PTR_ERR(info
->int_gpiod
);
247 info
->vbus_gpiod
= devm_gpiod_get(&i2c
->dev
, "vbus", GPIOD_IN
);
248 if (IS_ERR(info
->vbus_gpiod
)) {
249 dev_err(dev
, "failed to get VBUS GPIO\n");
250 return PTR_ERR(info
->vbus_gpiod
);
252 ret
= gpiod_direction_output(info
->vbus_gpiod
, 0);
254 dev_err(dev
, "failed to set VBUS GPIO direction\n");
258 mutex_init(&info
->mutex
);
260 INIT_WORK(&info
->irq_work
, ptn5150_irq_work
);
262 info
->regmap
= devm_regmap_init_i2c(i2c
, &ptn5150_regmap_config
);
263 if (IS_ERR(info
->regmap
)) {
264 ret
= PTR_ERR(info
->regmap
);
265 dev_err(info
->dev
, "failed to allocate register map: %d\n",
270 if (info
->int_gpiod
) {
271 info
->irq
= gpiod_to_irq(info
->int_gpiod
);
273 dev_err(dev
, "failed to get INTB IRQ\n");
277 ret
= devm_request_threaded_irq(dev
, info
->irq
, NULL
,
279 IRQF_TRIGGER_FALLING
|
283 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 /* Initialize PTN5150 device and print vendor id and version id */
303 ret
= ptn5150_init_dev_type(info
);
310 static const struct of_device_id ptn5150_dt_match
[] = {
311 { .compatible
= "nxp,ptn5150" },
314 MODULE_DEVICE_TABLE(of
, ptn5150_dt_match
);
316 static const struct i2c_device_id ptn5150_i2c_id
[] = {
320 MODULE_DEVICE_TABLE(i2c
, ptn5150_i2c_id
);
322 static struct i2c_driver ptn5150_i2c_driver
= {
325 .of_match_table
= ptn5150_dt_match
,
327 .probe
= ptn5150_i2c_probe
,
328 .id_table
= ptn5150_i2c_id
,
331 static int __init
ptn5150_i2c_init(void)
333 return i2c_add_driver(&ptn5150_i2c_driver
);
335 subsys_initcall(ptn5150_i2c_init
);
337 MODULE_DESCRIPTION("NXP PTN5150 CC logic Extcon driver");
338 MODULE_AUTHOR("Vijai Kumar K <vijaikumar.kanagarajan@gmail.com>");
339 MODULE_LICENSE("GPL v2");