1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for UCS1002 Programmable USB Port Power Controller
5 * Copyright (C) 2019 Zodiac Inflight Innovations
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>
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>
23 /* UCS1002 Registers */
24 #define UCS1002_REG_CURRENT_MEASUREMENT 0x00
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.
32 #define UCS1002_REG_TOTAL_ACC_CHARGE 0x01
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)
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)
45 # define F_OVER_VOLT BIT(2)
46 # define F_BACK_VOLT BIT(1)
47 # define F_OVER_ILIM BIT(0)
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)
64 /* General Configuration Register */
65 #define UCS1002_REG_GENERAL_CFG 0x15
66 # define F_RATION_EN BIT(3)
68 /* Emulation Configuration Register */
69 #define UCS1002_REG_EMU_CFG 0x16
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)
87 /* Current Limit Register */
88 #define UCS1002_REG_ILIMIT 0x19
89 # define UCS1002_ILIM_SW_MASK GENMASK(3, 0)
92 #define UCS1002_REG_PRODUCT_ID 0xfd
93 # define UCS1002_PRODUCT_ID 0x4e
95 /* Manufacture name */
96 #define UCS1002_MANUFACTURER "SMSC"
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
;
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
,
119 static int ucs1002_get_online(struct ucs1002_info
*info
,
120 union power_supply_propval
*val
)
125 ret
= regmap_read(info
->regmap
, UCS1002_REG_OTHER_STATUS
, ®
);
129 val
->intval
= !!(reg
& F_CHG_ACT
);
134 static int ucs1002_get_charge(struct ucs1002_info
*info
,
135 union power_supply_propval
*val
)
138 * To fit within 32 bits some values are rounded (uA/h)
140 * For Total Accumulated Charge Middle Low Byte register, addr
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
149 * For Total Accumulated Charge Low Byte register, addr 04h,
152 * B6: 0.00271 mA/h rounded to 3 uA/h
153 * B7: 0.005422 mA/h rounded to 5 uA/h
155 static const int bit_weights_uAh
[BITS_PER_TYPE(u32
)] = {
157 * Bit corresponding to low byte (offset 0x04)
158 * B0 B1 B2 B3 B4 B5 B6 B7
160 0, 0, 0, 0, 0, 0, 3, 5,
162 * Bit corresponding to middle low byte (offset 0x03)
163 * B0 B1 B2 B3 B4 B5 B6 B7
165 11, 22, 43, 87, 173, 347, 694, 1388,
167 * Bit corresponding to middle high byte (offset 0x02)
168 * B0 B1 B2 B3 B4 B5 B6 B7
170 2776, 5552, 11105, 22210, 44420, 88840, 177700, 355400,
172 * Bit corresponding to high byte (offset 0x01)
173 * B0 B1 B2 B3 B4 B5 B6 B7
175 710700, 1421000, 2843000, 5685000, 11371000, 22742000,
178 unsigned long total_acc_charger
;
182 ret
= regmap_bulk_read(info
->regmap
, UCS1002_REG_TOTAL_ACC_CHARGE
,
187 total_acc_charger
= be32_to_cpu(reg
); /* BE as per offsets above */
190 for_each_set_bit(i
, &total_acc_charger
, ARRAY_SIZE(bit_weights_uAh
))
191 val
->intval
+= bit_weights_uAh
[i
];
196 static int ucs1002_get_current(struct ucs1002_info
*info
,
197 union power_supply_propval
*val
)
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.
204 static const int bit_weights_uA
[BITS_PER_TYPE(u8
)] = {
205 9760, 19500, 39000, 78100, 156200, 312300, 624600, 1249300,
207 unsigned long current_measurement
;
211 ret
= regmap_read(info
->regmap
, UCS1002_REG_CURRENT_MEASUREMENT
, ®
);
215 current_measurement
= reg
;
218 for_each_set_bit(i
, ¤t_measurement
, ARRAY_SIZE(bit_weights_uA
))
219 val
->intval
+= bit_weights_uA
[i
];
225 * The Current Limit register stores the maximum current used by the
226 * port switch. The range is from 500mA to 2.5 A.
228 static const u32 ucs1002_current_limit_uA
[] = {
229 500000, 900000, 1000000, 1200000, 1500000, 1800000, 2000000, 2500000,
232 static int ucs1002_get_max_current(struct ucs1002_info
*info
,
233 union power_supply_propval
*val
)
238 if (info
->output_disable
) {
243 ret
= regmap_read(info
->regmap
, UCS1002_REG_ILIMIT
, ®
);
247 val
->intval
= ucs1002_current_limit_uA
[reg
& UCS1002_ILIM_SW_MASK
];
252 static int ucs1002_set_max_current(struct ucs1002_info
*info
, u32 val
)
258 info
->output_disable
= true;
259 regulator_disable_regmap(info
->rdev
);
263 for (idx
= 0; idx
< ARRAY_SIZE(ucs1002_current_limit_uA
); idx
++) {
264 if (val
== ucs1002_current_limit_uA
[idx
])
268 if (idx
== ARRAY_SIZE(ucs1002_current_limit_uA
))
271 ret
= regmap_write(info
->regmap
, UCS1002_REG_ILIMIT
, idx
);
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.
279 ret
= regmap_read(info
->regmap
, UCS1002_REG_ILIMIT
, ®
);
286 info
->output_disable
= false;
288 if (info
->rdev
&& info
->rdev
->use_count
&&
289 !regulator_is_enabled_regmap(info
->rdev
))
290 regulator_enable_regmap(info
->rdev
);
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
,
303 static int ucs1002_set_usb_type(struct ucs1002_info
*info
, int val
)
307 if (val
< 0 || val
>= ARRAY_SIZE(ucs1002_usb_types
))
310 switch (ucs1002_usb_types
[val
]) {
311 case POWER_SUPPLY_USB_TYPE_PD
:
312 mode
= V_SET_ACTIVE_MODE_DEDICATED
;
314 case POWER_SUPPLY_USB_TYPE_SDP
:
315 mode
= V_SET_ACTIVE_MODE_BC12_SDP
;
317 case POWER_SUPPLY_USB_TYPE_DCP
:
318 mode
= V_SET_ACTIVE_MODE_BC12_DCP
;
320 case POWER_SUPPLY_USB_TYPE_CDP
:
321 mode
= V_SET_ACTIVE_MODE_BC12_CDP
;
327 return regmap_update_bits(info
->regmap
, UCS1002_REG_SWITCH_CFG
,
328 V_SET_ACTIVE_MODE_MASK
, mode
);
331 static int ucs1002_get_usb_type(struct ucs1002_info
*info
,
332 union power_supply_propval
*val
)
334 enum power_supply_usb_type type
;
338 ret
= regmap_read(info
->regmap
, UCS1002_REG_PIN_STATUS
, ®
);
342 switch (reg
& F_ACTIVE_MODE_MASK
) {
344 type
= POWER_SUPPLY_USB_TYPE_UNKNOWN
;
346 case F_ACTIVE_MODE_DEDICATED
:
347 type
= POWER_SUPPLY_USB_TYPE_PD
;
349 case F_ACTIVE_MODE_BC12_SDP
:
350 type
= POWER_SUPPLY_USB_TYPE_SDP
;
352 case F_ACTIVE_MODE_BC12_DCP
:
353 type
= POWER_SUPPLY_USB_TYPE_DCP
;
355 case F_ACTIVE_MODE_BC12_CDP
:
356 type
= POWER_SUPPLY_USB_TYPE_CDP
;
365 static int ucs1002_get_health(struct ucs1002_info
*info
,
366 union power_supply_propval
*val
)
371 ret
= regmap_read(info
->regmap
, UCS1002_REG_INTERRUPT_STATUS
, ®
);
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
;
384 health
= POWER_SUPPLY_HEALTH_GOOD
;
386 val
->intval
= health
;
391 static int ucs1002_get_property(struct power_supply
*psy
,
392 enum power_supply_property psp
,
393 union power_supply_propval
*val
)
395 struct ucs1002_info
*info
= power_supply_get_drvdata(psy
);
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
;
413 case POWER_SUPPLY_PROP_MANUFACTURER
:
414 val
->strval
= UCS1002_MANUFACTURER
;
421 static int ucs1002_set_property(struct power_supply
*psy
,
422 enum power_supply_property psp
,
423 const union power_supply_propval
*val
)
425 struct ucs1002_info
*info
= power_supply_get_drvdata(psy
);
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
);
437 static int ucs1002_property_is_writeable(struct power_supply
*psy
,
438 enum power_supply_property psp
)
441 case POWER_SUPPLY_PROP_CURRENT_MAX
:
442 case POWER_SUPPLY_PROP_USB_TYPE
:
449 static const struct power_supply_desc ucs1002_charger_desc
= {
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
),
461 static irqreturn_t
ucs1002_charger_irq(int irq
, void *data
)
465 struct ucs1002_info
*info
= data
;
467 present
= info
->present
;
469 ret
= regmap_read(info
->regmap
, UCS1002_REG_OTHER_STATUS
, ®val
);
473 /* update attached status */
474 info
->present
= regval
& F_ADET_PIN
;
476 /* notify the change */
477 if (present
!= info
->present
)
478 power_supply_changed(info
->charger
);
483 static irqreturn_t
ucs1002_alert_irq(int irq
, void *data
)
485 struct ucs1002_info
*info
= data
;
487 power_supply_changed(info
->charger
);
492 static int ucs1002_regulator_enable(struct regulator_dev
*rdev
)
494 struct ucs1002_info
*info
= rdev_get_drvdata(rdev
);
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.
501 if (info
->output_disable
)
504 return regulator_enable_regmap(rdev
);
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
,
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
,
525 static int ucs1002_probe(struct i2c_client
*client
,
526 const struct i2c_device_id
*dev_id
)
528 struct device
*dev
= &client
->dev
;
529 struct power_supply_config charger_config
= {};
530 const struct regmap_config regmap_config
= {
534 struct regulator_config regulator_config
= {};
535 int irq_a_det
, irq_alert
, ret
;
536 struct ucs1002_info
*info
;
539 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
543 info
->regmap
= devm_regmap_init_i2c(client
, ®map_config
);
544 ret
= PTR_ERR_OR_ZERO(info
->regmap
);
546 dev_err(dev
, "Regmap initialization failed: %d\n", ret
);
550 info
->client
= client
;
552 irq_a_det
= of_irq_get_byname(dev
->of_node
, "a_det");
553 irq_alert
= of_irq_get_byname(dev
->of_node
, "alert");
555 charger_config
.of_node
= dev
->of_node
;
556 charger_config
.drv_data
= info
;
558 ret
= regmap_read(info
->regmap
, UCS1002_REG_PRODUCT_ID
, ®val
);
560 dev_err(dev
, "Failed to read product ID: %d\n", ret
);
564 if (regval
!= UCS1002_PRODUCT_ID
) {
566 "Product ID does not match (0x%02x != 0x%02x)\n",
567 regval
, UCS1002_PRODUCT_ID
);
571 /* Enable charge rationing by default */
572 ret
= regmap_update_bits(info
->regmap
, UCS1002_REG_GENERAL_CFG
,
573 F_RATION_EN
, F_RATION_EN
);
575 dev_err(dev
, "Failed to read general config: %d\n", ret
);
580 * Ignore the M1, M2, PWR_EN, and EM_EN pin states. Set active
581 * mode selection to BC1.2 CDP.
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
);
587 dev_err(dev
, "Failed to configure default mode: %d\n", ret
);
591 * Be safe and set initial current limit to 500mA
593 ret
= ucs1002_set_max_current(info
, 500000);
595 dev_err(dev
, "Failed to set max current default: %d\n", ret
);
599 info
->charger
= devm_power_supply_register(dev
, &ucs1002_charger_desc
,
601 ret
= PTR_ERR_OR_ZERO(info
->charger
);
603 dev_err(dev
, "Failed to register power supply: %d\n", ret
);
607 ret
= regmap_read(info
->regmap
, UCS1002_REG_PIN_STATUS
, ®val
);
609 dev_err(dev
, "Failed to read pin status: %d\n", ret
);
613 info
->regulator_descriptor
=
614 devm_kmemdup(dev
, &ucs1002_regulator_descriptor
,
615 sizeof(ucs1002_regulator_descriptor
),
617 if (!info
->regulator_descriptor
)
620 info
->regulator_descriptor
->enable_is_inverted
= !(regval
& F_SEL_PIN
);
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
;
627 info
->rdev
= devm_regulator_register(dev
, info
->regulator_descriptor
,
629 ret
= PTR_ERR_OR_ZERO(info
->rdev
);
631 dev_err(dev
, "Failed to register VBUS regulator: %d\n", ret
);
636 ret
= devm_request_threaded_irq(dev
, irq_a_det
, NULL
,
639 "ucs1002-a_det", info
);
641 dev_err(dev
, "Failed to request A_DET threaded irq: %d\n",
648 ret
= devm_request_threaded_irq(dev
, irq_alert
, NULL
,
651 "ucs1002-alert", info
);
653 dev_err(dev
, "Failed to request ALERT threaded irq: %d\n",
662 static const struct of_device_id ucs1002_of_match
[] = {
663 { .compatible
= "microchip,ucs1002", },
666 MODULE_DEVICE_TABLE(of
, ucs1002_of_match
);
668 static struct i2c_driver ucs1002_driver
= {
671 .of_match_table
= ucs1002_of_match
,
673 .probe
= ucs1002_probe
,
675 module_i2c_driver(ucs1002_driver
);
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");