1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // extcon-ptn5150.c - PTN5150 CC logic extcon driver to support USB detection
4 //
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>
9 
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 
21 /* PTN5150 registers */
22 enum ptn5150_reg {
23 	PTN5150_REG_DEVICE_ID = 0x01,
24 	PTN5150_REG_CONTROL,
25 	PTN5150_REG_INT_STATUS,
26 	PTN5150_REG_CC_STATUS,
27 	PTN5150_REG_CON_DET = 0x09,
28 	PTN5150_REG_VCONN_STATUS,
29 	PTN5150_REG_RESET,
30 	PTN5150_REG_INT_MASK = 0x18,
31 	PTN5150_REG_INT_REG_STATUS,
32 	PTN5150_REG_END,
33 };
34 
35 #define PTN5150_DFP_ATTACHED			0x1
36 #define PTN5150_UFP_ATTACHED			0x2
37 
38 /* Define PTN5150 MASK/SHIFT constant */
39 #define PTN5150_REG_DEVICE_ID_VERSION		GENMASK(7, 3)
40 #define PTN5150_REG_DEVICE_ID_VENDOR		GENMASK(2, 0)
41 
42 #define PTN5150_REG_CC_PORT_ATTACHMENT		GENMASK(4, 2)
43 #define PTN5150_REG_CC_VBUS_DETECTION		BIT(7)
44 #define PTN5150_REG_INT_CABLE_ATTACH_MASK	BIT(0)
45 #define PTN5150_REG_INT_CABLE_DETACH_MASK	BIT(1)
46 
47 struct ptn5150_info {
48 	struct device *dev;
49 	struct extcon_dev *edev;
50 	struct i2c_client *i2c;
51 	struct regmap *regmap;
52 	struct gpio_desc *int_gpiod;
53 	struct gpio_desc *vbus_gpiod;
54 	int irq;
55 	struct work_struct irq_work;
56 	struct mutex mutex;
57 };
58 
59 /* List of detectable cables */
60 static const unsigned int ptn5150_extcon_cable[] = {
61 	EXTCON_USB,
62 	EXTCON_USB_HOST,
63 	EXTCON_NONE,
64 };
65 
66 static const struct regmap_config ptn5150_regmap_config = {
67 	.reg_bits	= 8,
68 	.val_bits	= 8,
69 	.max_register	= PTN5150_REG_END,
70 };
71 
72 static void ptn5150_check_state(struct ptn5150_info *info)
73 {
74 	unsigned int port_status, reg_data, vbus;
75 	int ret;
76 
77 	ret = regmap_read(info->regmap, PTN5150_REG_CC_STATUS, &reg_data);
78 	if (ret) {
79 		dev_err(info->dev, "failed to read CC STATUS %d\n", ret);
80 		return;
81 	}
82 
83 	port_status = FIELD_GET(PTN5150_REG_CC_PORT_ATTACHMENT, reg_data);
84 
85 	switch (port_status) {
86 	case PTN5150_DFP_ATTACHED:
87 		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
88 		gpiod_set_value_cansleep(info->vbus_gpiod, 0);
89 		extcon_set_state_sync(info->edev, EXTCON_USB, true);
90 		break;
91 	case PTN5150_UFP_ATTACHED:
92 		extcon_set_state_sync(info->edev, EXTCON_USB, false);
93 		vbus = FIELD_GET(PTN5150_REG_CC_VBUS_DETECTION, reg_data);
94 		if (vbus)
95 			gpiod_set_value_cansleep(info->vbus_gpiod, 0);
96 		else
97 			gpiod_set_value_cansleep(info->vbus_gpiod, 1);
98 
99 		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
100 		break;
101 	default:
102 		dev_err(info->dev, "Unknown Port status : %x\n", port_status);
103 		break;
104 	}
105 }
106 
107 static void ptn5150_irq_work(struct work_struct *work)
108 {
109 	struct ptn5150_info *info = container_of(work,
110 			struct ptn5150_info, irq_work);
111 	int ret = 0;
112 	unsigned int int_status;
113 
114 	if (!info->edev)
115 		return;
116 
117 	mutex_lock(&info->mutex);
118 
119 	/* Clear interrupt. Read would clear the register */
120 	ret = regmap_read(info->regmap, PTN5150_REG_INT_STATUS, &int_status);
121 	if (ret) {
122 		dev_err(info->dev, "failed to read INT STATUS %d\n", ret);
123 		mutex_unlock(&info->mutex);
124 		return;
125 	}
126 
127 	if (int_status) {
128 		unsigned int cable_attach;
129 
130 		cable_attach = int_status & PTN5150_REG_INT_CABLE_ATTACH_MASK;
131 		if (cable_attach) {
132 			ptn5150_check_state(info);
133 		} else {
134 			extcon_set_state_sync(info->edev,
135 					EXTCON_USB_HOST, false);
136 			extcon_set_state_sync(info->edev,
137 					EXTCON_USB, false);
138 			gpiod_set_value_cansleep(info->vbus_gpiod, 0);
139 		}
140 	}
141 
142 	/* Clear interrupt. Read would clear the register */
143 	ret = regmap_read(info->regmap, PTN5150_REG_INT_REG_STATUS,
144 			&int_status);
145 	if (ret) {
146 		dev_err(info->dev,
147 			"failed to read INT REG STATUS %d\n", ret);
148 		mutex_unlock(&info->mutex);
149 		return;
150 	}
151 
152 	mutex_unlock(&info->mutex);
153 }
154 
155 
156 static irqreturn_t ptn5150_irq_handler(int irq, void *data)
157 {
158 	struct ptn5150_info *info = data;
159 
160 	schedule_work(&info->irq_work);
161 
162 	return IRQ_HANDLED;
163 }
164 
165 static int ptn5150_init_dev_type(struct ptn5150_info *info)
166 {
167 	unsigned int reg_data, vendor_id, version_id;
168 	int ret;
169 
170 	ret = regmap_read(info->regmap, PTN5150_REG_DEVICE_ID, &reg_data);
171 	if (ret) {
172 		dev_err(info->dev, "failed to read DEVICE_ID %d\n", ret);
173 		return -EINVAL;
174 	}
175 
176 	vendor_id = FIELD_GET(PTN5150_REG_DEVICE_ID_VENDOR, reg_data);
177 	version_id = FIELD_GET(PTN5150_REG_DEVICE_ID_VERSION, reg_data);
178 	dev_dbg(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
179 		version_id, vendor_id);
180 
181 	/* Clear any existing interrupts */
182 	ret = regmap_read(info->regmap, PTN5150_REG_INT_STATUS, &reg_data);
183 	if (ret) {
184 		dev_err(info->dev,
185 			"failed to read PTN5150_REG_INT_STATUS %d\n",
186 			ret);
187 		return -EINVAL;
188 	}
189 
190 	ret = regmap_read(info->regmap, PTN5150_REG_INT_REG_STATUS, &reg_data);
191 	if (ret) {
192 		dev_err(info->dev,
193 			"failed to read PTN5150_REG_INT_REG_STATUS %d\n", ret);
194 		return -EINVAL;
195 	}
196 
197 	return 0;
198 }
199 
200 static int ptn5150_i2c_probe(struct i2c_client *i2c)
201 {
202 	struct device *dev = &i2c->dev;
203 	struct device_node *np = i2c->dev.of_node;
204 	struct ptn5150_info *info;
205 	int ret;
206 
207 	if (!np)
208 		return -EINVAL;
209 
210 	info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
211 	if (!info)
212 		return -ENOMEM;
213 	i2c_set_clientdata(i2c, info);
214 
215 	info->dev = &i2c->dev;
216 	info->i2c = i2c;
217 	info->vbus_gpiod = devm_gpiod_get(&i2c->dev, "vbus", GPIOD_OUT_LOW);
218 	if (IS_ERR(info->vbus_gpiod)) {
219 		ret = PTR_ERR(info->vbus_gpiod);
220 		if (ret == -ENOENT) {
221 			dev_info(dev, "No VBUS GPIO, ignoring VBUS control\n");
222 			info->vbus_gpiod = NULL;
223 		} else {
224 			return dev_err_probe(dev, ret, "failed to get VBUS GPIO\n");
225 		}
226 	}
227 
228 	mutex_init(&info->mutex);
229 
230 	INIT_WORK(&info->irq_work, ptn5150_irq_work);
231 
232 	info->regmap = devm_regmap_init_i2c(i2c, &ptn5150_regmap_config);
233 	if (IS_ERR(info->regmap)) {
234 		return dev_err_probe(info->dev, PTR_ERR(info->regmap),
235 				     "failed to allocate register map\n");
236 	}
237 
238 	if (i2c->irq > 0) {
239 		info->irq = i2c->irq;
240 	} else {
241 		info->int_gpiod = devm_gpiod_get(&i2c->dev, "int", GPIOD_IN);
242 		if (IS_ERR(info->int_gpiod)) {
243 			return dev_err_probe(dev, PTR_ERR(info->int_gpiod),
244 					     "failed to get INT GPIO\n");
245 		}
246 
247 		info->irq = gpiod_to_irq(info->int_gpiod);
248 		if (info->irq < 0) {
249 			dev_err(dev, "failed to get INTB IRQ\n");
250 			return info->irq;
251 		}
252 	}
253 
254 	ret = devm_request_threaded_irq(dev, info->irq, NULL,
255 					ptn5150_irq_handler,
256 					IRQF_TRIGGER_FALLING |
257 					IRQF_ONESHOT,
258 					i2c->name, info);
259 	if (ret < 0) {
260 		dev_err(dev, "failed to request handler for INTB IRQ\n");
261 		return ret;
262 	}
263 
264 	/* Allocate extcon device */
265 	info->edev = devm_extcon_dev_allocate(info->dev, ptn5150_extcon_cable);
266 	if (IS_ERR(info->edev)) {
267 		dev_err(info->dev, "failed to allocate memory for extcon\n");
268 		return -ENOMEM;
269 	}
270 
271 	/* Register extcon device */
272 	ret = devm_extcon_dev_register(info->dev, info->edev);
273 	if (ret) {
274 		dev_err(info->dev, "failed to register extcon device\n");
275 		return ret;
276 	}
277 
278 	/* Initialize PTN5150 device and print vendor id and version id */
279 	ret = ptn5150_init_dev_type(info);
280 	if (ret)
281 		return -EINVAL;
282 
283 	/*
284 	 * Update current extcon state if for example OTG connection was there
285 	 * before the probe
286 	 */
287 	mutex_lock(&info->mutex);
288 	ptn5150_check_state(info);
289 	mutex_unlock(&info->mutex);
290 
291 	return 0;
292 }
293 
294 static const struct of_device_id ptn5150_dt_match[] = {
295 	{ .compatible = "nxp,ptn5150" },
296 	{ },
297 };
298 MODULE_DEVICE_TABLE(of, ptn5150_dt_match);
299 
300 static const struct i2c_device_id ptn5150_i2c_id[] = {
301 	{ "ptn5150", 0 },
302 	{ }
303 };
304 MODULE_DEVICE_TABLE(i2c, ptn5150_i2c_id);
305 
306 static struct i2c_driver ptn5150_i2c_driver = {
307 	.driver		= {
308 		.name	= "ptn5150",
309 		.of_match_table = ptn5150_dt_match,
310 	},
311 	.probe_new	= ptn5150_i2c_probe,
312 	.id_table = ptn5150_i2c_id,
313 };
314 module_i2c_driver(ptn5150_i2c_driver);
315 
316 MODULE_DESCRIPTION("NXP PTN5150 CC logic Extcon driver");
317 MODULE_AUTHOR("Vijai Kumar K <vijaikumar.kanagarajan@gmail.com>");
318 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
319 MODULE_LICENSE("GPL v2");
320