1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for UCS1002 Programmable USB Port Power Controller
4  *
5  * Copyright (C) 2019 Zodiac Inflight Innovations
6  */
7 #include <linux/bits.h>
8 #include <linux/freezer.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/kthread.h>
14 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/of_irq.h>
18 #include <linux/power_supply.h>
19 #include <linux/regmap.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/of_regulator.h>
22 
23 /* UCS1002 Registers */
24 #define UCS1002_REG_CURRENT_MEASUREMENT	0x00
25 
26 /*
27  * The Total Accumulated Charge registers store the total accumulated
28  * charge delivered from the VS source to a portable device. The total
29  * value is calculated using four registers, from 01h to 04h. The bit
30  * weighting of the registers is given in mA/hrs.
31  */
32 #define UCS1002_REG_TOTAL_ACC_CHARGE	0x01
33 
34 /* Other Status Register */
35 #define UCS1002_REG_OTHER_STATUS	0x0f
36 #  define F_ADET_PIN			BIT(4)
37 #  define F_CHG_ACT			BIT(3)
38 
39 /* Interrupt Status */
40 #define UCS1002_REG_INTERRUPT_STATUS	0x10
41 #  define F_DISCHARGE_ERR		BIT(6)
42 #  define F_RESET			BIT(5)
43 #  define F_MIN_KEEP_OUT		BIT(4)
44 #  define F_TSD				BIT(3)
45 #  define F_OVER_VOLT			BIT(2)
46 #  define F_BACK_VOLT			BIT(1)
47 #  define F_OVER_ILIM			BIT(0)
48 
49 /* Pin Status Register */
50 #define UCS1002_REG_PIN_STATUS		0x14
51 #  define UCS1002_PWR_STATE_MASK	0x03
52 #  define F_PWR_EN_PIN			BIT(6)
53 #  define F_M2_PIN			BIT(5)
54 #  define F_M1_PIN			BIT(4)
55 #  define F_EM_EN_PIN			BIT(3)
56 #  define F_SEL_PIN			BIT(2)
57 #  define F_ACTIVE_MODE_MASK		GENMASK(5, 3)
58 #  define F_ACTIVE_MODE_PASSTHROUGH	F_M2_PIN
59 #  define F_ACTIVE_MODE_DEDICATED	F_EM_EN_PIN
60 #  define F_ACTIVE_MODE_BC12_DCP	(F_M2_PIN | F_EM_EN_PIN)
61 #  define F_ACTIVE_MODE_BC12_SDP	F_M1_PIN
62 #  define F_ACTIVE_MODE_BC12_CDP	(F_M1_PIN | F_M2_PIN | F_EM_EN_PIN)
63 
64 /* General Configuration Register */
65 #define UCS1002_REG_GENERAL_CFG		0x15
66 #  define F_RATION_EN			BIT(3)
67 
68 /* Emulation Configuration Register */
69 #define UCS1002_REG_EMU_CFG		0x16
70 
71 /* Switch Configuration Register */
72 #define UCS1002_REG_SWITCH_CFG		0x17
73 #  define F_PIN_IGNORE			BIT(7)
74 #  define F_EM_EN_SET			BIT(5)
75 #  define F_M2_SET			BIT(4)
76 #  define F_M1_SET			BIT(3)
77 #  define F_S0_SET			BIT(2)
78 #  define F_PWR_EN_SET			BIT(1)
79 #  define F_LATCH_SET			BIT(0)
80 #  define V_SET_ACTIVE_MODE_MASK	GENMASK(5, 3)
81 #  define V_SET_ACTIVE_MODE_PASSTHROUGH	F_M2_SET
82 #  define V_SET_ACTIVE_MODE_DEDICATED	F_EM_EN_SET
83 #  define V_SET_ACTIVE_MODE_BC12_DCP	(F_M2_SET | F_EM_EN_SET)
84 #  define V_SET_ACTIVE_MODE_BC12_SDP	F_M1_SET
85 #  define V_SET_ACTIVE_MODE_BC12_CDP	(F_M1_SET | F_M2_SET | F_EM_EN_SET)
86 
87 /* Current Limit Register */
88 #define UCS1002_REG_ILIMIT		0x19
89 #  define UCS1002_ILIM_SW_MASK		GENMASK(3, 0)
90 
91 /* Product ID */
92 #define UCS1002_REG_PRODUCT_ID		0xfd
93 #  define UCS1002_PRODUCT_ID		0x4e
94 
95 /* Manufacture name */
96 #define UCS1002_MANUFACTURER		"SMSC"
97 
98 struct ucs1002_info {
99 	struct power_supply *charger;
100 	struct i2c_client *client;
101 	struct regmap *regmap;
102 	struct regulator_desc *regulator_descriptor;
103 	struct regulator_dev *rdev;
104 	bool present;
105 	bool output_disable;
106 };
107 
108 static enum power_supply_property ucs1002_props[] = {
109 	POWER_SUPPLY_PROP_ONLINE,
110 	POWER_SUPPLY_PROP_CHARGE_NOW,
111 	POWER_SUPPLY_PROP_CURRENT_NOW,
112 	POWER_SUPPLY_PROP_CURRENT_MAX,
113 	POWER_SUPPLY_PROP_PRESENT, /* the presence of PED */
114 	POWER_SUPPLY_PROP_MANUFACTURER,
115 	POWER_SUPPLY_PROP_USB_TYPE,
116 	POWER_SUPPLY_PROP_HEALTH,
117 };
118 
119 static int ucs1002_get_online(struct ucs1002_info *info,
120 			      union power_supply_propval *val)
121 {
122 	unsigned int reg;
123 	int ret;
124 
125 	ret = regmap_read(info->regmap, UCS1002_REG_OTHER_STATUS, &reg);
126 	if (ret)
127 		return ret;
128 
129 	val->intval = !!(reg & F_CHG_ACT);
130 
131 	return 0;
132 }
133 
134 static int ucs1002_get_charge(struct ucs1002_info *info,
135 			      union power_supply_propval *val)
136 {
137 	/*
138 	 * To fit within 32 bits some values are rounded (uA/h)
139 	 *
140 	 * For Total Accumulated Charge Middle Low Byte register, addr
141 	 * 03h, byte 2
142 	 *
143 	 *   B0: 0.01084 mA/h rounded to 11 uA/h
144 	 *   B1: 0.02169 mA/h rounded to 22 uA/h
145 	 *   B2: 0.04340 mA/h rounded to 43 uA/h
146 	 *   B3: 0.08676 mA/h rounded to 87 uA/h
147 	 *   B4: 0.17350 mA/h rounded to 173 uÁ/h
148 	 *
149 	 * For Total Accumulated Charge Low Byte register, addr 04h,
150 	 * byte 3
151 	 *
152 	 *   B6: 0.00271 mA/h rounded to 3 uA/h
153 	 *   B7: 0.005422 mA/h rounded to 5 uA/h
154 	 */
155 	static const int bit_weights_uAh[BITS_PER_TYPE(u32)] = {
156 		/*
157 		 * Bit corresponding to low byte (offset 0x04)
158 		 * B0 B1 B2 B3 B4 B5 B6 B7
159 		 */
160 		0, 0, 0, 0, 0, 0, 3, 5,
161 		/*
162 		 * Bit corresponding to middle low byte (offset 0x03)
163 		 * B0 B1 B2 B3 B4 B5 B6 B7
164 		 */
165 		11, 22, 43, 87, 173, 347, 694, 1388,
166 		/*
167 		 * Bit corresponding to middle high byte (offset 0x02)
168 		 * B0 B1 B2 B3 B4 B5 B6 B7
169 		 */
170 		2776, 5552, 11105, 22210, 44420, 88840, 177700, 355400,
171 		/*
172 		 * Bit corresponding to high byte (offset 0x01)
173 		 * B0 B1 B2 B3 B4 B5 B6 B7
174 		 */
175 		710700, 1421000, 2843000, 5685000, 11371000, 22742000,
176 		45484000, 90968000,
177 	};
178 	unsigned long total_acc_charger;
179 	unsigned int reg;
180 	int i, ret;
181 
182 	ret = regmap_bulk_read(info->regmap, UCS1002_REG_TOTAL_ACC_CHARGE,
183 			       &reg, sizeof(u32));
184 	if (ret)
185 		return ret;
186 
187 	total_acc_charger = be32_to_cpu(reg); /* BE as per offsets above */
188 	val->intval = 0;
189 
190 	for_each_set_bit(i, &total_acc_charger, ARRAY_SIZE(bit_weights_uAh))
191 		val->intval += bit_weights_uAh[i];
192 
193 	return 0;
194 }
195 
196 static int ucs1002_get_current(struct ucs1002_info *info,
197 			       union power_supply_propval *val)
198 {
199 	/*
200 	 * The Current Measurement register stores the measured
201 	 * current value delivered to the portable device. The range
202 	 * is from 9.76 mA to 2.5 A.
203 	 */
204 	static const int bit_weights_uA[BITS_PER_TYPE(u8)] = {
205 		9760, 19500, 39000, 78100, 156200, 312300, 624600, 1249300,
206 	};
207 	unsigned long current_measurement;
208 	unsigned int reg;
209 	int i, ret;
210 
211 	ret = regmap_read(info->regmap, UCS1002_REG_CURRENT_MEASUREMENT, &reg);
212 	if (ret)
213 		return ret;
214 
215 	current_measurement = reg;
216 	val->intval = 0;
217 
218 	for_each_set_bit(i, &current_measurement, ARRAY_SIZE(bit_weights_uA))
219 		val->intval += bit_weights_uA[i];
220 
221 	return 0;
222 }
223 
224 /*
225  * The Current Limit register stores the maximum current used by the
226  * port switch. The range is from 500mA to 2.5 A.
227  */
228 static const u32 ucs1002_current_limit_uA[] = {
229 	500000, 900000, 1000000, 1200000, 1500000, 1800000, 2000000, 2500000,
230 };
231 
232 static int ucs1002_get_max_current(struct ucs1002_info *info,
233 				   union power_supply_propval *val)
234 {
235 	unsigned int reg;
236 	int ret;
237 
238 	if (info->output_disable) {
239 		val->intval = 0;
240 		return 0;
241 	}
242 
243 	ret = regmap_read(info->regmap, UCS1002_REG_ILIMIT, &reg);
244 	if (ret)
245 		return ret;
246 
247 	val->intval = ucs1002_current_limit_uA[reg & UCS1002_ILIM_SW_MASK];
248 
249 	return 0;
250 }
251 
252 static int ucs1002_set_max_current(struct ucs1002_info *info, u32 val)
253 {
254 	unsigned int reg;
255 	int ret, idx;
256 
257 	if (val == 0) {
258 		info->output_disable = true;
259 		regulator_disable_regmap(info->rdev);
260 		return 0;
261 	}
262 
263 	for (idx = 0; idx < ARRAY_SIZE(ucs1002_current_limit_uA); idx++) {
264 		if (val == ucs1002_current_limit_uA[idx])
265 			break;
266 	}
267 
268 	if (idx == ARRAY_SIZE(ucs1002_current_limit_uA))
269 		return -EINVAL;
270 
271 	ret = regmap_write(info->regmap, UCS1002_REG_ILIMIT, idx);
272 	if (ret)
273 		return ret;
274 	/*
275 	 * Any current limit setting exceeding the one set via ILIM
276 	 * pin will be rejected, so we read out freshly changed limit
277 	 * to make sure that it took effect.
278 	 */
279 	ret = regmap_read(info->regmap, UCS1002_REG_ILIMIT, &reg);
280 	if (ret)
281 		return ret;
282 
283 	if (reg != idx)
284 		return -EINVAL;
285 
286 	info->output_disable = false;
287 
288 	if (info->rdev && info->rdev->use_count &&
289 	    !regulator_is_enabled_regmap(info->rdev))
290 		regulator_enable_regmap(info->rdev);
291 
292 	return 0;
293 }
294 
295 static enum power_supply_usb_type ucs1002_usb_types[] = {
296 	POWER_SUPPLY_USB_TYPE_PD,
297 	POWER_SUPPLY_USB_TYPE_SDP,
298 	POWER_SUPPLY_USB_TYPE_DCP,
299 	POWER_SUPPLY_USB_TYPE_CDP,
300 	POWER_SUPPLY_USB_TYPE_UNKNOWN,
301 };
302 
303 static int ucs1002_set_usb_type(struct ucs1002_info *info, int val)
304 {
305 	unsigned int mode;
306 
307 	if (val < 0 || val >= ARRAY_SIZE(ucs1002_usb_types))
308 		return -EINVAL;
309 
310 	switch (ucs1002_usb_types[val]) {
311 	case POWER_SUPPLY_USB_TYPE_PD:
312 		mode = V_SET_ACTIVE_MODE_DEDICATED;
313 		break;
314 	case POWER_SUPPLY_USB_TYPE_SDP:
315 		mode = V_SET_ACTIVE_MODE_BC12_SDP;
316 		break;
317 	case POWER_SUPPLY_USB_TYPE_DCP:
318 		mode = V_SET_ACTIVE_MODE_BC12_DCP;
319 		break;
320 	case POWER_SUPPLY_USB_TYPE_CDP:
321 		mode = V_SET_ACTIVE_MODE_BC12_CDP;
322 		break;
323 	default:
324 		return -EINVAL;
325 	}
326 
327 	return regmap_update_bits(info->regmap, UCS1002_REG_SWITCH_CFG,
328 				  V_SET_ACTIVE_MODE_MASK, mode);
329 }
330 
331 static int ucs1002_get_usb_type(struct ucs1002_info *info,
332 				union power_supply_propval *val)
333 {
334 	enum power_supply_usb_type type;
335 	unsigned int reg;
336 	int ret;
337 
338 	ret = regmap_read(info->regmap, UCS1002_REG_PIN_STATUS, &reg);
339 	if (ret)
340 		return ret;
341 
342 	switch (reg & F_ACTIVE_MODE_MASK) {
343 	default:
344 		type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
345 		break;
346 	case F_ACTIVE_MODE_DEDICATED:
347 		type = POWER_SUPPLY_USB_TYPE_PD;
348 		break;
349 	case F_ACTIVE_MODE_BC12_SDP:
350 		type = POWER_SUPPLY_USB_TYPE_SDP;
351 		break;
352 	case F_ACTIVE_MODE_BC12_DCP:
353 		type = POWER_SUPPLY_USB_TYPE_DCP;
354 		break;
355 	case F_ACTIVE_MODE_BC12_CDP:
356 		type = POWER_SUPPLY_USB_TYPE_CDP;
357 		break;
358 	}
359 
360 	val->intval = type;
361 
362 	return 0;
363 }
364 
365 static int ucs1002_get_health(struct ucs1002_info *info,
366 			      union power_supply_propval *val)
367 {
368 	unsigned int reg;
369 	int ret, health;
370 
371 	ret = regmap_read(info->regmap, UCS1002_REG_INTERRUPT_STATUS, &reg);
372 	if (ret)
373 		return ret;
374 
375 	if (reg & F_TSD)
376 		health = POWER_SUPPLY_HEALTH_OVERHEAT;
377 	else if (reg & (F_OVER_VOLT | F_BACK_VOLT))
378 		health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
379 	else if (reg & F_OVER_ILIM)
380 		health = POWER_SUPPLY_HEALTH_OVERCURRENT;
381 	else if (reg & (F_DISCHARGE_ERR | F_MIN_KEEP_OUT))
382 		health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
383 	else
384 		health = POWER_SUPPLY_HEALTH_GOOD;
385 
386 	val->intval = health;
387 
388 	return 0;
389 }
390 
391 static int ucs1002_get_property(struct power_supply *psy,
392 				enum power_supply_property psp,
393 				union power_supply_propval *val)
394 {
395 	struct ucs1002_info *info = power_supply_get_drvdata(psy);
396 
397 	switch (psp) {
398 	case POWER_SUPPLY_PROP_ONLINE:
399 		return ucs1002_get_online(info, val);
400 	case POWER_SUPPLY_PROP_CHARGE_NOW:
401 		return ucs1002_get_charge(info, val);
402 	case POWER_SUPPLY_PROP_CURRENT_NOW:
403 		return ucs1002_get_current(info, val);
404 	case POWER_SUPPLY_PROP_CURRENT_MAX:
405 		return ucs1002_get_max_current(info, val);
406 	case POWER_SUPPLY_PROP_USB_TYPE:
407 		return ucs1002_get_usb_type(info, val);
408 	case POWER_SUPPLY_PROP_HEALTH:
409 		return ucs1002_get_health(info, val);
410 	case POWER_SUPPLY_PROP_PRESENT:
411 		val->intval = info->present;
412 		return 0;
413 	case POWER_SUPPLY_PROP_MANUFACTURER:
414 		val->strval = UCS1002_MANUFACTURER;
415 		return 0;
416 	default:
417 		return -EINVAL;
418 	}
419 }
420 
421 static int ucs1002_set_property(struct power_supply *psy,
422 				enum power_supply_property psp,
423 				const union power_supply_propval *val)
424 {
425 	struct ucs1002_info *info = power_supply_get_drvdata(psy);
426 
427 	switch (psp) {
428 	case POWER_SUPPLY_PROP_CURRENT_MAX:
429 		return ucs1002_set_max_current(info, val->intval);
430 	case POWER_SUPPLY_PROP_USB_TYPE:
431 		return ucs1002_set_usb_type(info, val->intval);
432 	default:
433 		return -EINVAL;
434 	}
435 }
436 
437 static int ucs1002_property_is_writeable(struct power_supply *psy,
438 					 enum power_supply_property psp)
439 {
440 	switch (psp) {
441 	case POWER_SUPPLY_PROP_CURRENT_MAX:
442 	case POWER_SUPPLY_PROP_USB_TYPE:
443 		return true;
444 	default:
445 		return false;
446 	}
447 }
448 
449 static const struct power_supply_desc ucs1002_charger_desc = {
450 	.name			= "ucs1002",
451 	.type			= POWER_SUPPLY_TYPE_USB,
452 	.usb_types		= ucs1002_usb_types,
453 	.num_usb_types		= ARRAY_SIZE(ucs1002_usb_types),
454 	.get_property		= ucs1002_get_property,
455 	.set_property		= ucs1002_set_property,
456 	.property_is_writeable	= ucs1002_property_is_writeable,
457 	.properties		= ucs1002_props,
458 	.num_properties		= ARRAY_SIZE(ucs1002_props),
459 };
460 
461 static irqreturn_t ucs1002_charger_irq(int irq, void *data)
462 {
463 	int ret, regval;
464 	bool present;
465 	struct ucs1002_info *info = data;
466 
467 	present = info->present;
468 
469 	ret = regmap_read(info->regmap, UCS1002_REG_OTHER_STATUS, &regval);
470 	if (ret)
471 		return IRQ_HANDLED;
472 
473 	/* update attached status */
474 	info->present = regval & F_ADET_PIN;
475 
476 	/* notify the change */
477 	if (present != info->present)
478 		power_supply_changed(info->charger);
479 
480 	return IRQ_HANDLED;
481 }
482 
483 static irqreturn_t ucs1002_alert_irq(int irq, void *data)
484 {
485 	struct ucs1002_info *info = data;
486 
487 	power_supply_changed(info->charger);
488 
489 	return IRQ_HANDLED;
490 }
491 
492 static int ucs1002_regulator_enable(struct regulator_dev *rdev)
493 {
494 	struct ucs1002_info *info = rdev_get_drvdata(rdev);
495 
496 	/*
497 	 * If the output is disabled due to 0 maximum current, just pretend the
498 	 * enable did work. The regulator will be enabled as soon as we get a
499 	 * a non-zero maximum current budget.
500 	 */
501 	if (info->output_disable)
502 		return 0;
503 
504 	return regulator_enable_regmap(rdev);
505 }
506 
507 static const struct regulator_ops ucs1002_regulator_ops = {
508 	.is_enabled	= regulator_is_enabled_regmap,
509 	.enable		= ucs1002_regulator_enable,
510 	.disable	= regulator_disable_regmap,
511 };
512 
513 static const struct regulator_desc ucs1002_regulator_descriptor = {
514 	.name		= "ucs1002-vbus",
515 	.ops		= &ucs1002_regulator_ops,
516 	.type		= REGULATOR_VOLTAGE,
517 	.owner		= THIS_MODULE,
518 	.enable_reg	= UCS1002_REG_SWITCH_CFG,
519 	.enable_mask	= F_PWR_EN_SET,
520 	.enable_val	= F_PWR_EN_SET,
521 	.fixed_uV	= 5000000,
522 	.n_voltages	= 1,
523 };
524 
525 static int ucs1002_probe(struct i2c_client *client,
526 			 const struct i2c_device_id *dev_id)
527 {
528 	struct device *dev = &client->dev;
529 	struct power_supply_config charger_config = {};
530 	const struct regmap_config regmap_config = {
531 		.reg_bits = 8,
532 		.val_bits = 8,
533 	};
534 	struct regulator_config regulator_config = {};
535 	int irq_a_det, irq_alert, ret;
536 	struct ucs1002_info *info;
537 	unsigned int regval;
538 
539 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
540 	if (!info)
541 		return -ENOMEM;
542 
543 	info->regmap = devm_regmap_init_i2c(client, &regmap_config);
544 	ret = PTR_ERR_OR_ZERO(info->regmap);
545 	if (ret) {
546 		dev_err(dev, "Regmap initialization failed: %d\n", ret);
547 		return ret;
548 	}
549 
550 	info->client = client;
551 
552 	irq_a_det = of_irq_get_byname(dev->of_node, "a_det");
553 	irq_alert = of_irq_get_byname(dev->of_node, "alert");
554 
555 	charger_config.of_node = dev->of_node;
556 	charger_config.drv_data = info;
557 
558 	ret = regmap_read(info->regmap, UCS1002_REG_PRODUCT_ID, &regval);
559 	if (ret) {
560 		dev_err(dev, "Failed to read product ID: %d\n", ret);
561 		return ret;
562 	}
563 
564 	if (regval != UCS1002_PRODUCT_ID) {
565 		dev_err(dev,
566 			"Product ID does not match (0x%02x != 0x%02x)\n",
567 			regval, UCS1002_PRODUCT_ID);
568 		return -ENODEV;
569 	}
570 
571 	/* Enable charge rationing by default */
572 	ret = regmap_update_bits(info->regmap, UCS1002_REG_GENERAL_CFG,
573 				 F_RATION_EN, F_RATION_EN);
574 	if (ret) {
575 		dev_err(dev, "Failed to read general config: %d\n", ret);
576 		return ret;
577 	}
578 
579 	/*
580 	 * Ignore the M1, M2, PWR_EN, and EM_EN pin states. Set active
581 	 * mode selection to BC1.2 CDP.
582 	 */
583 	ret = regmap_update_bits(info->regmap, UCS1002_REG_SWITCH_CFG,
584 				 V_SET_ACTIVE_MODE_MASK | F_PIN_IGNORE,
585 				 V_SET_ACTIVE_MODE_BC12_CDP | F_PIN_IGNORE);
586 	if (ret) {
587 		dev_err(dev, "Failed to configure default mode: %d\n", ret);
588 		return ret;
589 	}
590 	/*
591 	 * Be safe and set initial current limit to 500mA
592 	 */
593 	ret = ucs1002_set_max_current(info, 500000);
594 	if (ret) {
595 		dev_err(dev, "Failed to set max current default: %d\n", ret);
596 		return ret;
597 	}
598 
599 	info->charger = devm_power_supply_register(dev, &ucs1002_charger_desc,
600 						   &charger_config);
601 	ret = PTR_ERR_OR_ZERO(info->charger);
602 	if (ret) {
603 		dev_err(dev, "Failed to register power supply: %d\n", ret);
604 		return ret;
605 	}
606 
607 	ret = regmap_read(info->regmap, UCS1002_REG_PIN_STATUS, &regval);
608 	if (ret) {
609 		dev_err(dev, "Failed to read pin status: %d\n", ret);
610 		return ret;
611 	}
612 
613 	info->regulator_descriptor =
614 		devm_kmemdup(dev, &ucs1002_regulator_descriptor,
615 			     sizeof(ucs1002_regulator_descriptor),
616 			     GFP_KERNEL);
617 	if (!info->regulator_descriptor)
618 		return -ENOMEM;
619 
620 	info->regulator_descriptor->enable_is_inverted = !(regval & F_SEL_PIN);
621 
622 	regulator_config.dev = dev;
623 	regulator_config.of_node = dev->of_node;
624 	regulator_config.regmap = info->regmap;
625 	regulator_config.driver_data = info;
626 
627 	info->rdev = devm_regulator_register(dev, info->regulator_descriptor,
628 				       &regulator_config);
629 	ret = PTR_ERR_OR_ZERO(info->rdev);
630 	if (ret) {
631 		dev_err(dev, "Failed to register VBUS regulator: %d\n", ret);
632 		return ret;
633 	}
634 
635 	if (irq_a_det > 0) {
636 		ret = devm_request_threaded_irq(dev, irq_a_det, NULL,
637 						ucs1002_charger_irq,
638 						IRQF_ONESHOT,
639 						"ucs1002-a_det", info);
640 		if (ret) {
641 			dev_err(dev, "Failed to request A_DET threaded irq: %d\n",
642 				ret);
643 			return ret;
644 		}
645 	}
646 
647 	if (irq_alert > 0) {
648 		ret = devm_request_threaded_irq(dev, irq_alert, NULL,
649 						ucs1002_alert_irq,
650 						IRQF_ONESHOT,
651 						"ucs1002-alert", info);
652 		if (ret) {
653 			dev_err(dev, "Failed to request ALERT threaded irq: %d\n",
654 				ret);
655 			return ret;
656 		}
657 	}
658 
659 	return 0;
660 }
661 
662 static const struct of_device_id ucs1002_of_match[] = {
663 	{ .compatible = "microchip,ucs1002", },
664 	{ /* sentinel */ },
665 };
666 MODULE_DEVICE_TABLE(of, ucs1002_of_match);
667 
668 static struct i2c_driver ucs1002_driver = {
669 	.driver = {
670 		   .name = "ucs1002",
671 		   .of_match_table = ucs1002_of_match,
672 	},
673 	.probe = ucs1002_probe,
674 };
675 module_i2c_driver(ucs1002_driver);
676 
677 MODULE_DESCRIPTION("Microchip UCS1002 Programmable USB Port Power Controller");
678 MODULE_AUTHOR("Enric Balletbo Serra <enric.balletbo@collabora.com>");
679 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
680 MODULE_LICENSE("GPL");
681