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
42 # define F_DISCHARGE_ERR BIT(6)
43 # define F_RESET BIT(5)
44 # define F_MIN_KEEP_OUT BIT(4)
46 # define F_OVER_VOLT BIT(2)
47 # define F_BACK_VOLT BIT(1)
48 # define F_OVER_ILIM BIT(0)
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)
65 /* General Configuration Register */
66 #define UCS1002_REG_GENERAL_CFG 0x15
67 # define F_RATION_EN BIT(3)
69 /* Emulation Configuration Register */
70 #define UCS1002_REG_EMU_CFG 0x16
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)
88 /* Current Limit Register */
89 #define UCS1002_REG_ILIMIT 0x19
90 # define UCS1002_ILIM_SW_MASK GENMASK(3, 0)
93 #define UCS1002_REG_PRODUCT_ID 0xfd
94 # define UCS1002_PRODUCT_ID 0x4e
96 /* Manufacture name */
97 #define UCS1002_MANUFACTURER "SMSC"
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
;
107 struct delayed_work health_poll
;
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
,
123 static int ucs1002_get_online(struct ucs1002_info
*info
,
124 union power_supply_propval
*val
)
129 ret
= regmap_read(info
->regmap
, UCS1002_REG_OTHER_STATUS
, ®
);
133 val
->intval
= !!(reg
& F_CHG_ACT
);
138 static int ucs1002_get_charge(struct ucs1002_info
*info
,
139 union power_supply_propval
*val
)
142 * To fit within 32 bits some values are rounded (uA/h)
144 * For Total Accumulated Charge Middle Low Byte register, addr
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
153 * For Total Accumulated Charge Low Byte register, addr 04h,
156 * B6: 0.00271 mA/h rounded to 3 uA/h
157 * B7: 0.005422 mA/h rounded to 5 uA/h
159 static const int bit_weights_uAh
[BITS_PER_TYPE(u32
)] = {
161 * Bit corresponding to low byte (offset 0x04)
162 * B0 B1 B2 B3 B4 B5 B6 B7
164 0, 0, 0, 0, 0, 0, 3, 5,
166 * Bit corresponding to middle low byte (offset 0x03)
167 * B0 B1 B2 B3 B4 B5 B6 B7
169 11, 22, 43, 87, 173, 347, 694, 1388,
171 * Bit corresponding to middle high byte (offset 0x02)
172 * B0 B1 B2 B3 B4 B5 B6 B7
174 2776, 5552, 11105, 22210, 44420, 88840, 177700, 355400,
176 * Bit corresponding to high byte (offset 0x01)
177 * B0 B1 B2 B3 B4 B5 B6 B7
179 710700, 1421000, 2843000, 5685000, 11371000, 22742000,
182 unsigned long total_acc_charger
;
186 ret
= regmap_bulk_read(info
->regmap
, UCS1002_REG_TOTAL_ACC_CHARGE
,
191 total_acc_charger
= be32_to_cpu(reg
); /* BE as per offsets above */
194 for_each_set_bit(i
, &total_acc_charger
, ARRAY_SIZE(bit_weights_uAh
))
195 val
->intval
+= bit_weights_uAh
[i
];
200 static int ucs1002_get_current(struct ucs1002_info
*info
,
201 union power_supply_propval
*val
)
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.
208 static const int bit_weights_uA
[BITS_PER_TYPE(u8
)] = {
209 9760, 19500, 39000, 78100, 156200, 312300, 624600, 1249300,
211 unsigned long current_measurement
;
215 ret
= regmap_read(info
->regmap
, UCS1002_REG_CURRENT_MEASUREMENT
, ®
);
219 current_measurement
= reg
;
222 for_each_set_bit(i
, ¤t_measurement
, ARRAY_SIZE(bit_weights_uA
))
223 val
->intval
+= bit_weights_uA
[i
];
229 * The Current Limit register stores the maximum current used by the
230 * port switch. The range is from 500mA to 2.5 A.
232 static const u32 ucs1002_current_limit_uA
[] = {
233 500000, 900000, 1000000, 1200000, 1500000, 1800000, 2000000, 2500000,
236 static int ucs1002_get_max_current(struct ucs1002_info
*info
,
237 union power_supply_propval
*val
)
242 if (info
->output_disable
) {
247 ret
= regmap_read(info
->regmap
, UCS1002_REG_ILIMIT
, ®
);
251 val
->intval
= ucs1002_current_limit_uA
[reg
& UCS1002_ILIM_SW_MASK
];
256 static int ucs1002_set_max_current(struct ucs1002_info
*info
, u32 val
)
262 info
->output_disable
= true;
263 regulator_disable_regmap(info
->rdev
);
267 for (idx
= 0; idx
< ARRAY_SIZE(ucs1002_current_limit_uA
); idx
++) {
268 if (val
== ucs1002_current_limit_uA
[idx
])
272 if (idx
== ARRAY_SIZE(ucs1002_current_limit_uA
))
275 ret
= regmap_write(info
->regmap
, UCS1002_REG_ILIMIT
, idx
);
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.
283 ret
= regmap_read(info
->regmap
, UCS1002_REG_ILIMIT
, ®
);
290 info
->output_disable
= false;
292 if (info
->rdev
&& info
->rdev
->use_count
&&
293 !regulator_is_enabled_regmap(info
->rdev
))
294 regulator_enable_regmap(info
->rdev
);
299 static int ucs1002_set_usb_type(struct ucs1002_info
*info
, int val
)
305 * POWER_SUPPLY_USB_TYPE_UNKNOWN == 0, map this to dedicated for
306 * userspace API compatibility with older versions of this driver
307 * which mapped 0 to dedicated.
309 case POWER_SUPPLY_USB_TYPE_UNKNOWN
:
310 case POWER_SUPPLY_USB_TYPE_PD
:
311 mode
= V_SET_ACTIVE_MODE_DEDICATED
;
313 case POWER_SUPPLY_USB_TYPE_SDP
:
314 mode
= V_SET_ACTIVE_MODE_BC12_SDP
;
316 case POWER_SUPPLY_USB_TYPE_DCP
:
317 mode
= V_SET_ACTIVE_MODE_BC12_DCP
;
319 case POWER_SUPPLY_USB_TYPE_CDP
:
320 mode
= V_SET_ACTIVE_MODE_BC12_CDP
;
326 return regmap_update_bits(info
->regmap
, UCS1002_REG_SWITCH_CFG
,
327 V_SET_ACTIVE_MODE_MASK
, mode
);
330 static int ucs1002_get_usb_type(struct ucs1002_info
*info
,
331 union power_supply_propval
*val
)
333 enum power_supply_usb_type type
;
337 ret
= regmap_read(info
->regmap
, UCS1002_REG_PIN_STATUS
, ®
);
341 switch (reg
& F_ACTIVE_MODE_MASK
) {
343 type
= POWER_SUPPLY_USB_TYPE_UNKNOWN
;
345 case F_ACTIVE_MODE_DEDICATED
:
346 type
= POWER_SUPPLY_USB_TYPE_PD
;
348 case F_ACTIVE_MODE_BC12_SDP
:
349 type
= POWER_SUPPLY_USB_TYPE_SDP
;
351 case F_ACTIVE_MODE_BC12_DCP
:
352 type
= POWER_SUPPLY_USB_TYPE_DCP
;
354 case F_ACTIVE_MODE_BC12_CDP
:
355 type
= POWER_SUPPLY_USB_TYPE_CDP
;
364 static int ucs1002_get_property(struct power_supply
*psy
,
365 enum power_supply_property psp
,
366 union power_supply_propval
*val
)
368 struct ucs1002_info
*info
= power_supply_get_drvdata(psy
);
371 case POWER_SUPPLY_PROP_ONLINE
:
372 return ucs1002_get_online(info
, val
);
373 case POWER_SUPPLY_PROP_CHARGE_NOW
:
374 return ucs1002_get_charge(info
, val
);
375 case POWER_SUPPLY_PROP_CURRENT_NOW
:
376 return ucs1002_get_current(info
, val
);
377 case POWER_SUPPLY_PROP_CURRENT_MAX
:
378 return ucs1002_get_max_current(info
, val
);
379 case POWER_SUPPLY_PROP_USB_TYPE
:
380 return ucs1002_get_usb_type(info
, val
);
381 case POWER_SUPPLY_PROP_HEALTH
:
382 val
->intval
= info
->health
;
384 case POWER_SUPPLY_PROP_PRESENT
:
385 val
->intval
= info
->present
;
387 case POWER_SUPPLY_PROP_MANUFACTURER
:
388 val
->strval
= UCS1002_MANUFACTURER
;
395 static int ucs1002_set_property(struct power_supply
*psy
,
396 enum power_supply_property psp
,
397 const union power_supply_propval
*val
)
399 struct ucs1002_info
*info
= power_supply_get_drvdata(psy
);
402 case POWER_SUPPLY_PROP_CURRENT_MAX
:
403 return ucs1002_set_max_current(info
, val
->intval
);
404 case POWER_SUPPLY_PROP_USB_TYPE
:
405 return ucs1002_set_usb_type(info
, val
->intval
);
411 static int ucs1002_property_is_writeable(struct power_supply
*psy
,
412 enum power_supply_property psp
)
415 case POWER_SUPPLY_PROP_CURRENT_MAX
:
416 case POWER_SUPPLY_PROP_USB_TYPE
:
423 static const struct power_supply_desc ucs1002_charger_desc
= {
425 .type
= POWER_SUPPLY_TYPE_USB
,
426 .usb_types
= BIT(POWER_SUPPLY_USB_TYPE_SDP
) |
427 BIT(POWER_SUPPLY_USB_TYPE_CDP
) |
428 BIT(POWER_SUPPLY_USB_TYPE_DCP
) |
429 BIT(POWER_SUPPLY_USB_TYPE_PD
) |
430 BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN
),
431 .get_property
= ucs1002_get_property
,
432 .set_property
= ucs1002_set_property
,
433 .property_is_writeable
= ucs1002_property_is_writeable
,
434 .properties
= ucs1002_props
,
435 .num_properties
= ARRAY_SIZE(ucs1002_props
),
438 static void ucs1002_health_poll(struct work_struct
*work
)
440 struct ucs1002_info
*info
= container_of(work
, struct ucs1002_info
,
445 ret
= regmap_read(info
->regmap
, UCS1002_REG_INTERRUPT_STATUS
, ®
);
449 /* bad health and no status change, just schedule us again in a while */
450 if ((reg
& F_ERR
) && info
->health
!= POWER_SUPPLY_HEALTH_GOOD
) {
451 schedule_delayed_work(&info
->health_poll
,
452 msecs_to_jiffies(2000));
457 info
->health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
458 else if (reg
& (F_OVER_VOLT
| F_BACK_VOLT
))
459 info
->health
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
460 else if (reg
& F_OVER_ILIM
)
461 info
->health
= POWER_SUPPLY_HEALTH_OVERCURRENT
;
462 else if (reg
& (F_DISCHARGE_ERR
| F_MIN_KEEP_OUT
))
463 info
->health
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
465 info
->health
= POWER_SUPPLY_HEALTH_GOOD
;
467 sysfs_notify(&info
->charger
->dev
.kobj
, NULL
, "health");
470 static irqreturn_t
ucs1002_charger_irq(int irq
, void *data
)
474 struct ucs1002_info
*info
= data
;
476 present
= info
->present
;
478 ret
= regmap_read(info
->regmap
, UCS1002_REG_OTHER_STATUS
, ®val
);
482 /* update attached status */
483 info
->present
= regval
& F_ADET_PIN
;
485 /* notify the change */
486 if (present
!= info
->present
)
487 power_supply_changed(info
->charger
);
492 static irqreturn_t
ucs1002_alert_irq(int irq
, void *data
)
494 struct ucs1002_info
*info
= data
;
496 mod_delayed_work(system_wq
, &info
->health_poll
, 0);
501 static int ucs1002_regulator_enable(struct regulator_dev
*rdev
)
503 struct ucs1002_info
*info
= rdev_get_drvdata(rdev
);
506 * If the output is disabled due to 0 maximum current, just pretend the
507 * enable did work. The regulator will be enabled as soon as we get a
508 * a non-zero maximum current budget.
510 if (info
->output_disable
)
513 return regulator_enable_regmap(rdev
);
516 static const struct regulator_ops ucs1002_regulator_ops
= {
517 .is_enabled
= regulator_is_enabled_regmap
,
518 .enable
= ucs1002_regulator_enable
,
519 .disable
= regulator_disable_regmap
,
522 static const struct regulator_desc ucs1002_regulator_descriptor
= {
523 .name
= "ucs1002-vbus",
524 .ops
= &ucs1002_regulator_ops
,
525 .type
= REGULATOR_VOLTAGE
,
526 .owner
= THIS_MODULE
,
527 .enable_reg
= UCS1002_REG_SWITCH_CFG
,
528 .enable_mask
= F_PWR_EN_SET
,
529 .enable_val
= F_PWR_EN_SET
,
534 static int ucs1002_probe(struct i2c_client
*client
)
536 struct device
*dev
= &client
->dev
;
537 struct power_supply_config charger_config
= {};
538 const struct regmap_config regmap_config
= {
542 struct regulator_config regulator_config
= {};
543 int irq_a_det
, irq_alert
, ret
;
544 struct ucs1002_info
*info
;
547 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
551 info
->regmap
= devm_regmap_init_i2c(client
, ®map_config
);
552 ret
= PTR_ERR_OR_ZERO(info
->regmap
);
554 dev_err(dev
, "Regmap initialization failed: %d\n", ret
);
558 info
->client
= client
;
560 irq_a_det
= of_irq_get_byname(dev
->of_node
, "a_det");
561 irq_alert
= of_irq_get_byname(dev
->of_node
, "alert");
563 charger_config
.of_node
= dev
->of_node
;
564 charger_config
.drv_data
= info
;
566 ret
= regmap_read(info
->regmap
, UCS1002_REG_PRODUCT_ID
, ®val
);
568 dev_err(dev
, "Failed to read product ID: %d\n", ret
);
572 if (regval
!= UCS1002_PRODUCT_ID
) {
574 "Product ID does not match (0x%02x != 0x%02x)\n",
575 regval
, UCS1002_PRODUCT_ID
);
579 /* Enable charge rationing by default */
580 ret
= regmap_update_bits(info
->regmap
, UCS1002_REG_GENERAL_CFG
,
581 F_RATION_EN
, F_RATION_EN
);
583 dev_err(dev
, "Failed to read general config: %d\n", ret
);
588 * Ignore the M1, M2, PWR_EN, and EM_EN pin states. Set active
589 * mode selection to BC1.2 CDP.
591 ret
= regmap_update_bits(info
->regmap
, UCS1002_REG_SWITCH_CFG
,
592 V_SET_ACTIVE_MODE_MASK
| F_PIN_IGNORE
,
593 V_SET_ACTIVE_MODE_BC12_CDP
| F_PIN_IGNORE
);
595 dev_err(dev
, "Failed to configure default mode: %d\n", ret
);
599 * Be safe and set initial current limit to 500mA
601 ret
= ucs1002_set_max_current(info
, 500000);
603 dev_err(dev
, "Failed to set max current default: %d\n", ret
);
607 info
->charger
= devm_power_supply_register(dev
, &ucs1002_charger_desc
,
609 ret
= PTR_ERR_OR_ZERO(info
->charger
);
611 dev_err(dev
, "Failed to register power supply: %d\n", ret
);
615 ret
= regmap_read(info
->regmap
, UCS1002_REG_PIN_STATUS
, ®val
);
617 dev_err(dev
, "Failed to read pin status: %d\n", ret
);
621 info
->regulator_descriptor
=
622 devm_kmemdup(dev
, &ucs1002_regulator_descriptor
,
623 sizeof(ucs1002_regulator_descriptor
),
625 if (!info
->regulator_descriptor
)
628 info
->regulator_descriptor
->enable_is_inverted
= !(regval
& F_SEL_PIN
);
630 regulator_config
.dev
= dev
;
631 regulator_config
.of_node
= dev
->of_node
;
632 regulator_config
.regmap
= info
->regmap
;
633 regulator_config
.driver_data
= info
;
635 info
->rdev
= devm_regulator_register(dev
, info
->regulator_descriptor
,
637 ret
= PTR_ERR_OR_ZERO(info
->rdev
);
639 dev_err(dev
, "Failed to register VBUS regulator: %d\n", ret
);
643 info
->health
= POWER_SUPPLY_HEALTH_GOOD
;
644 INIT_DELAYED_WORK(&info
->health_poll
, ucs1002_health_poll
);
647 ret
= devm_request_threaded_irq(dev
, irq_a_det
, NULL
,
650 "ucs1002-a_det", info
);
652 dev_err(dev
, "Failed to request A_DET threaded irq: %d\n",
659 ret
= devm_request_irq(dev
, irq_alert
, ucs1002_alert_irq
,
660 0,"ucs1002-alert", info
);
662 dev_err(dev
, "Failed to request ALERT threaded irq: %d\n",
671 static const struct of_device_id ucs1002_of_match
[] = {
672 { .compatible
= "microchip,ucs1002", },
675 MODULE_DEVICE_TABLE(of
, ucs1002_of_match
);
677 static struct i2c_driver ucs1002_driver
= {
680 .of_match_table
= ucs1002_of_match
,
682 .probe
= ucs1002_probe
,
684 module_i2c_driver(ucs1002_driver
);
686 MODULE_DESCRIPTION("Microchip UCS1002 Programmable USB Port Power Controller");
687 MODULE_AUTHOR("Enric Balletbo Serra <enric.balletbo@collabora.com>");
688 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
689 MODULE_LICENSE("GPL");