Skip to content

Commit

Permalink
power: introduce the charger uclass
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fourkbomb committed May 22, 2018
1 parent 0b1f24d commit 7a8e0ad
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ libs-y += drivers/net/
libs-y += drivers/net/phy/
libs-y += drivers/pci/
libs-y += drivers/power/ \
drivers/power/charger/ \
drivers/power/domain/ \
drivers/power/fuel_gauge/ \
drivers/power/mfd/ \
Expand Down
2 changes: 2 additions & 0 deletions drivers/power/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ menu "Power"

source "drivers/power/battery/Kconfig"

source "drivers/power/charger/Kconfig"

source "drivers/power/domain/Kconfig"

source "drivers/power/pmic/Kconfig"
Expand Down
5 changes: 5 additions & 0 deletions drivers/power/charger/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
config DM_CHARGER
bool "Enable Driver Model for charger drivers (UCLASS_CHARGER)"
depends on DM
---help---
This config enables driver model charger support.
8 changes: 8 additions & 0 deletions drivers/power/charger/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#
# Copyright (C) 2018 Simon Shields <simon@lineageos.org>
#
# SPDX-License-Identifier: GPL-2.0+
#

obj-$(CONFIG_DM_CHARGER) += charger-uclass.o

54 changes: 54 additions & 0 deletions drivers/power/charger/charger-uclass.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2018 Simon Shields <simon@lineageos.org>
*
* SPDX-License-Identifier: GPL-2.0+
*/

#include <common.h>
#include <errno.h>
#include <dm.h>
#include <dm/uclass-internal.h>
#include <power/charger.h>

DECLARE_GLOBAL_DATA_PTR;

int charger_get(const char *devname, struct udevice **devp)
{
return uclass_get_device_by_name(UCLASS_CHARGER, devname, devp);
}

int charger_set_current(struct udevice *dev, unsigned int microamps)
{
const struct dm_charger_ops *ops = dev_get_driver_ops(dev);

if (!ops || !ops->set_current)
return -ENOSYS;

return ops->set_current(dev, microamps);
}

int charger_get_current(struct udevice *dev)
{
const struct dm_charger_ops *ops = dev_get_driver_ops(dev);

if (!ops || !ops->get_current)
return -ENOSYS;

return ops->get_current(dev);
}

int charger_get_status(struct udevice *dev)
{
const struct dm_charger_ops *ops = dev_get_driver_ops(dev);

if (!ops || !ops->get_status)
return -ENOSYS;

return ops->get_status(dev);
}


UCLASS_DRIVER(charger) = {
.id = UCLASS_CHARGER,
.name = "charger",
};
1 change: 1 addition & 0 deletions include/dm/uclass-id.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum uclass_id {
UCLASS_AHCI, /* SATA disk controller */
UCLASS_BATTERY, /* Battery */
UCLASS_BLK, /* Block device */
UCLASS_CHARGER, /* Charger */
UCLASS_CLK, /* Clock source, e.g. used by peripherals */
UCLASS_CPU, /* CPU, typically part of an SoC */
UCLASS_CROS_EC, /* Chrome OS EC */
Expand Down
40 changes: 40 additions & 0 deletions include/power/charger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2018 Simon Shields <simon@lineageos.org>
*
* SPDX-License-Identifier: GPL-2.0+
*/

enum charger_state {
CHARGE_STATE_UNKNOWN = 0,
CHARGE_STATE_CHARGING = 1, /* charging normally */
CHARGE_STATE_FULL = 2, /* not charging - battery full */
CHARGE_STATE_NOT_CHARGING = 3, /* not charging - some other reason */
CHARGE_STATE_DISCHARGING = 4, /* discharging */
};

struct dm_charger_ops {
/**
* Get the charge current of the charger.
* Some devices may return the maximum charge current rather than the current charge current.
* @dev - charger device.
* @return -errno on error, charge current in uA.
*/
int (*get_current)(struct udevice *dev);
/**
* Set the maximum charge current for the charger. A current of zero will disable charging.
* @dev - charger device
* @return -errno on error, 0 otherwise.
*/
int (*set_current)(struct udevice *dev, unsigned int microamps);
/**
* Get current charging state.
* @dev - charger device
* @return -errno on error, enum charger_state otherwise.
*/
int (*get_status)(struct udevice *dev);
};

int charger_get(const char *devname, struct udevice **devp);
int charger_get_current(struct udevice *dev);
int charger_set_current(struct udevice *dev, unsigned int microamps);
int charger_get_status(struct udevice *dev);

0 comments on commit 7a8e0ad

Please sign in to comment.