Skip to content

Commit 7a8e0ad

Browse files
committed
power: introduce the charger uclass
chargers have set current, get current, and get status operations. They control the charging state of a battery. Typically a charger, extcon, and battery driver would work in tandem: * extcon detects the cable's maximum current (if a cable is attached). * battery detects state of charge (i.e. does the battery need charging?). * charger controls current in order to allow the battery to charge.
1 parent 0b1f24d commit 7a8e0ad

File tree

7 files changed

+111
-0
lines changed

7 files changed

+111
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ libs-y += drivers/net/
666666
libs-y += drivers/net/phy/
667667
libs-y += drivers/pci/
668668
libs-y += drivers/power/ \
669+
drivers/power/charger/ \
669670
drivers/power/domain/ \
670671
drivers/power/fuel_gauge/ \
671672
drivers/power/mfd/ \

drivers/power/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ menu "Power"
22

33
source "drivers/power/battery/Kconfig"
44

5+
source "drivers/power/charger/Kconfig"
6+
57
source "drivers/power/domain/Kconfig"
68

79
source "drivers/power/pmic/Kconfig"

drivers/power/charger/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
config DM_CHARGER
2+
bool "Enable Driver Model for charger drivers (UCLASS_CHARGER)"
3+
depends on DM
4+
---help---
5+
This config enables driver model charger support.

drivers/power/charger/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# Copyright (C) 2018 Simon Shields <simon@lineageos.org>
3+
#
4+
# SPDX-License-Identifier: GPL-2.0+
5+
#
6+
7+
obj-$(CONFIG_DM_CHARGER) += charger-uclass.o
8+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (C) 2018 Simon Shields <simon@lineageos.org>
3+
*
4+
* SPDX-License-Identifier: GPL-2.0+
5+
*/
6+
7+
#include <common.h>
8+
#include <errno.h>
9+
#include <dm.h>
10+
#include <dm/uclass-internal.h>
11+
#include <power/charger.h>
12+
13+
DECLARE_GLOBAL_DATA_PTR;
14+
15+
int charger_get(const char *devname, struct udevice **devp)
16+
{
17+
return uclass_get_device_by_name(UCLASS_CHARGER, devname, devp);
18+
}
19+
20+
int charger_set_current(struct udevice *dev, unsigned int microamps)
21+
{
22+
const struct dm_charger_ops *ops = dev_get_driver_ops(dev);
23+
24+
if (!ops || !ops->set_current)
25+
return -ENOSYS;
26+
27+
return ops->set_current(dev, microamps);
28+
}
29+
30+
int charger_get_current(struct udevice *dev)
31+
{
32+
const struct dm_charger_ops *ops = dev_get_driver_ops(dev);
33+
34+
if (!ops || !ops->get_current)
35+
return -ENOSYS;
36+
37+
return ops->get_current(dev);
38+
}
39+
40+
int charger_get_status(struct udevice *dev)
41+
{
42+
const struct dm_charger_ops *ops = dev_get_driver_ops(dev);
43+
44+
if (!ops || !ops->get_status)
45+
return -ENOSYS;
46+
47+
return ops->get_status(dev);
48+
}
49+
50+
51+
UCLASS_DRIVER(charger) = {
52+
.id = UCLASS_CHARGER,
53+
.name = "charger",
54+
};

include/dm/uclass-id.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ enum uclass_id {
3030
UCLASS_AHCI, /* SATA disk controller */
3131
UCLASS_BATTERY, /* Battery */
3232
UCLASS_BLK, /* Block device */
33+
UCLASS_CHARGER, /* Charger */
3334
UCLASS_CLK, /* Clock source, e.g. used by peripherals */
3435
UCLASS_CPU, /* CPU, typically part of an SoC */
3536
UCLASS_CROS_EC, /* Chrome OS EC */

include/power/charger.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2018 Simon Shields <simon@lineageos.org>
3+
*
4+
* SPDX-License-Identifier: GPL-2.0+
5+
*/
6+
7+
enum charger_state {
8+
CHARGE_STATE_UNKNOWN = 0,
9+
CHARGE_STATE_CHARGING = 1, /* charging normally */
10+
CHARGE_STATE_FULL = 2, /* not charging - battery full */
11+
CHARGE_STATE_NOT_CHARGING = 3, /* not charging - some other reason */
12+
CHARGE_STATE_DISCHARGING = 4, /* discharging */
13+
};
14+
15+
struct dm_charger_ops {
16+
/**
17+
* Get the charge current of the charger.
18+
* Some devices may return the maximum charge current rather than the current charge current.
19+
* @dev - charger device.
20+
* @return -errno on error, charge current in uA.
21+
*/
22+
int (*get_current)(struct udevice *dev);
23+
/**
24+
* Set the maximum charge current for the charger. A current of zero will disable charging.
25+
* @dev - charger device
26+
* @return -errno on error, 0 otherwise.
27+
*/
28+
int (*set_current)(struct udevice *dev, unsigned int microamps);
29+
/**
30+
* Get current charging state.
31+
* @dev - charger device
32+
* @return -errno on error, enum charger_state otherwise.
33+
*/
34+
int (*get_status)(struct udevice *dev);
35+
};
36+
37+
int charger_get(const char *devname, struct udevice **devp);
38+
int charger_get_current(struct udevice *dev);
39+
int charger_set_current(struct udevice *dev, unsigned int microamps);
40+
int charger_get_status(struct udevice *dev);

0 commit comments

Comments
 (0)