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