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/err.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
17 #include <linux/extcon-provider.h>
18 #include <linux/gpio/consumer.h>
20 /* PTN5150 registers */
22 PTN5150_REG_DEVICE_ID = 0x01,
24 PTN5150_REG_INT_STATUS,
25 PTN5150_REG_CC_STATUS,
26 PTN5150_REG_CON_DET = 0x09,
27 PTN5150_REG_VCONN_STATUS,
29 PTN5150_REG_INT_MASK = 0x18,
30 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_VENDOR_SHIFT 0
39 #define PTN5150_REG_DEVICE_ID_VENDOR_MASK \
40 (0x3 << PTN5150_REG_DEVICE_ID_VENDOR_SHIFT)
42 #define PTN5150_REG_DEVICE_ID_VERSION_SHIFT 3
43 #define PTN5150_REG_DEVICE_ID_VERSION_MASK \
44 (0x1f << PTN5150_REG_DEVICE_ID_VERSION_SHIFT)
46 #define PTN5150_REG_CC_PORT_ATTACHMENT_SHIFT 2
47 #define PTN5150_REG_CC_PORT_ATTACHMENT_MASK \
48 (0x7 << PTN5150_REG_CC_PORT_ATTACHMENT_SHIFT)
50 #define PTN5150_REG_CC_VBUS_DETECTION_SHIFT 7
51 #define PTN5150_REG_CC_VBUS_DETECTION_MASK \
52 (0x1 << PTN5150_REG_CC_VBUS_DETECTION_SHIFT)
54 #define PTN5150_REG_INT_CABLE_ATTACH_SHIFT 0
55 #define PTN5150_REG_INT_CABLE_ATTACH_MASK \
56 (0x1 << PTN5150_REG_INT_CABLE_ATTACH_SHIFT)
58 #define PTN5150_REG_INT_CABLE_DETACH_SHIFT 1
59 #define PTN5150_REG_INT_CABLE_DETACH_MASK \
60 (0x1 << PTN5150_REG_CC_CABLE_DETACH_SHIFT)
64 struct extcon_dev *edev;
65 struct i2c_client *i2c;
66 struct regmap *regmap;
67 struct gpio_desc *int_gpiod;
68 struct gpio_desc *vbus_gpiod;
70 struct work_struct irq_work;
74 /* List of detectable cables */
75 static const unsigned int ptn5150_extcon_cable[] = {
81 static const struct regmap_config ptn5150_regmap_config = {
84 .max_register = PTN5150_REG_END,
87 static void ptn5150_check_state(struct ptn5150_info *info)
89 unsigned int port_status, reg_data, vbus;
92 ret = regmap_read(info->regmap, PTN5150_REG_CC_STATUS, ®_data);
94 dev_err(info->dev, "failed to read CC STATUS %d\n", ret);
98 port_status = ((reg_data &
99 PTN5150_REG_CC_PORT_ATTACHMENT_MASK) >>
100 PTN5150_REG_CC_PORT_ATTACHMENT_SHIFT);
102 switch (port_status) {
103 case PTN5150_DFP_ATTACHED:
104 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
105 gpiod_set_value_cansleep(info->vbus_gpiod, 0);
106 extcon_set_state_sync(info->edev, EXTCON_USB, true);
108 case PTN5150_UFP_ATTACHED:
109 extcon_set_state_sync(info->edev, EXTCON_USB, false);
110 vbus = ((reg_data & PTN5150_REG_CC_VBUS_DETECTION_MASK) >>
111 PTN5150_REG_CC_VBUS_DETECTION_SHIFT);
113 gpiod_set_value_cansleep(info->vbus_gpiod, 0);
115 gpiod_set_value_cansleep(info->vbus_gpiod, 1);
117 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
120 dev_err(info->dev, "Unknown Port status : %x\n", port_status);
125 static void ptn5150_irq_work(struct work_struct *work)
127 struct ptn5150_info *info = container_of(work,
128 struct ptn5150_info, irq_work);
130 unsigned int int_status;
135 mutex_lock(&info->mutex);
137 /* Clear interrupt. Read would clear the register */
138 ret = regmap_read(info->regmap, PTN5150_REG_INT_STATUS, &int_status);
140 dev_err(info->dev, "failed to read INT STATUS %d\n", ret);
141 mutex_unlock(&info->mutex);
146 unsigned int cable_attach;
148 cable_attach = int_status & PTN5150_REG_INT_CABLE_ATTACH_MASK;
150 ptn5150_check_state(info);
152 extcon_set_state_sync(info->edev,
153 EXTCON_USB_HOST, false);
154 extcon_set_state_sync(info->edev,
156 gpiod_set_value_cansleep(info->vbus_gpiod, 0);
160 /* Clear interrupt. Read would clear the register */
161 ret = regmap_read(info->regmap, PTN5150_REG_INT_REG_STATUS,
165 "failed to read INT REG STATUS %d\n", ret);
166 mutex_unlock(&info->mutex);
170 mutex_unlock(&info->mutex);
174 static irqreturn_t ptn5150_irq_handler(int irq, void *data)
176 struct ptn5150_info *info = data;
178 schedule_work(&info->irq_work);
183 static int ptn5150_init_dev_type(struct ptn5150_info *info)
185 unsigned int reg_data, vendor_id, version_id;
188 ret = regmap_read(info->regmap, PTN5150_REG_DEVICE_ID, ®_data);
190 dev_err(info->dev, "failed to read DEVICE_ID %d\n", ret);
194 vendor_id = ((reg_data & PTN5150_REG_DEVICE_ID_VENDOR_MASK) >>
195 PTN5150_REG_DEVICE_ID_VENDOR_SHIFT);
196 version_id = ((reg_data & PTN5150_REG_DEVICE_ID_VERSION_MASK) >>
197 PTN5150_REG_DEVICE_ID_VERSION_SHIFT);
199 dev_dbg(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
200 version_id, vendor_id);
202 /* Clear any existing interrupts */
203 ret = regmap_read(info->regmap, PTN5150_REG_INT_STATUS, ®_data);
206 "failed to read PTN5150_REG_INT_STATUS %d\n",
211 ret = regmap_read(info->regmap, PTN5150_REG_INT_REG_STATUS, ®_data);
214 "failed to read PTN5150_REG_INT_REG_STATUS %d\n", ret);
221 static int ptn5150_i2c_probe(struct i2c_client *i2c,
222 const struct i2c_device_id *id)
224 struct device *dev = &i2c->dev;
225 struct device_node *np = i2c->dev.of_node;
226 struct ptn5150_info *info;
232 info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
235 i2c_set_clientdata(i2c, info);
237 info->dev = &i2c->dev;
239 info->vbus_gpiod = devm_gpiod_get(&i2c->dev, "vbus", GPIOD_OUT_LOW);
240 if (IS_ERR(info->vbus_gpiod)) {
241 ret = PTR_ERR(info->vbus_gpiod);
242 if (ret == -ENOENT) {
243 dev_info(dev, "No VBUS GPIO, ignoring VBUS control\n");
244 info->vbus_gpiod = NULL;
246 dev_err_probe(dev, ret, "failed to get VBUS GPIO\n");
251 mutex_init(&info->mutex);
253 INIT_WORK(&info->irq_work, ptn5150_irq_work);
255 info->regmap = devm_regmap_init_i2c(i2c, &ptn5150_regmap_config);
256 if (IS_ERR(info->regmap)) {
257 ret = PTR_ERR(info->regmap);
258 dev_err_probe(info->dev, ret, "failed to allocate register map: %d\n",
264 info->irq = i2c->irq;
266 info->int_gpiod = devm_gpiod_get(&i2c->dev, "int", GPIOD_IN);
267 if (IS_ERR(info->int_gpiod)) {
268 ret = PTR_ERR(info->int_gpiod);
269 dev_err_probe(dev, ret, "failed to get INT GPIO\n");
273 info->irq = gpiod_to_irq(info->int_gpiod);
275 dev_err(dev, "failed to get INTB IRQ\n");
280 ret = devm_request_threaded_irq(dev, info->irq, NULL,
282 IRQF_TRIGGER_FALLING |
286 dev_err(dev, "failed to request handler for INTB IRQ\n");
290 /* Allocate extcon device */
291 info->edev = devm_extcon_dev_allocate(info->dev, ptn5150_extcon_cable);
292 if (IS_ERR(info->edev)) {
293 dev_err(info->dev, "failed to allocate memory for extcon\n");
297 /* Register extcon device */
298 ret = devm_extcon_dev_register(info->dev, info->edev);
300 dev_err(info->dev, "failed to register extcon device\n");
304 /* Initialize PTN5150 device and print vendor id and version id */
305 ret = ptn5150_init_dev_type(info);
310 * Update current extcon state if for example OTG connection was there
313 mutex_lock(&info->mutex);
314 ptn5150_check_state(info);
315 mutex_unlock(&info->mutex);
320 static const struct of_device_id ptn5150_dt_match[] = {
321 { .compatible = "nxp,ptn5150" },
324 MODULE_DEVICE_TABLE(of, ptn5150_dt_match);
326 static const struct i2c_device_id ptn5150_i2c_id[] = {
330 MODULE_DEVICE_TABLE(i2c, ptn5150_i2c_id);
332 static struct i2c_driver ptn5150_i2c_driver = {
335 .of_match_table = ptn5150_dt_match,
337 .probe = ptn5150_i2c_probe,
338 .id_table = ptn5150_i2c_id,
341 static int __init ptn5150_i2c_init(void)
343 return i2c_add_driver(&ptn5150_i2c_driver);
345 subsys_initcall(ptn5150_i2c_init);
347 MODULE_DESCRIPTION("NXP PTN5150 CC logic Extcon driver");
348 MODULE_AUTHOR("Vijai Kumar K <vijaikumar.kanagarajan@gmail.com>");
349 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
350 MODULE_LICENSE("GPL v2");