Permalink
Browse files

power: add battery uclass

this uclass represents battery devices - they have a "state of charge"
(capacity %ge), a voltage, and a "state" - not present, ok, or low
power.
  • Loading branch information...
fourkbomb committed May 21, 2018
1 parent 98dd51e commit 948addd66fd952c3fe33c9a0a7b7ba409465052b
@@ -1,5 +1,7 @@
menu "Power"

source "drivers/power/battery/Kconfig"

source "drivers/power/domain/Kconfig"

source "drivers/power/pmic/Kconfig"
@@ -0,0 +1,6 @@
config DM_BATTERY
bool "Enable Driver Model for BATTERY drivers (UCLASS_BATTERY)"
depends on DM
---help---
This config enables driver model battery support.

@@ -7,3 +7,5 @@

obj-$(CONFIG_POWER_BATTERY_TRATS) += bat_trats.o
obj-$(CONFIG_POWER_BATTERY_TRATS2) += bat_trats2.o

obj-$(CONFIG_DM_BATTERY) += battery-uclass.o
@@ -0,0 +1,53 @@
/*
* 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/battery.h>

DECLARE_GLOBAL_DATA_PTR;

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

int battery_get_voltage(struct udevice *dev, unsigned int *uV)
{
const struct dm_battery_ops *ops = dev_get_driver_ops(dev);

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

return ops->get_voltage(dev, uV);
}

int battery_get_status(struct udevice *dev)
{
const struct dm_battery_ops *ops = dev_get_driver_ops(dev);

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

return ops->get_status(dev);
}

int battery_get_soc(struct udevice *dev)
{
const struct dm_battery_ops *ops = dev_get_driver_ops(dev);

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

return ops->get_soc(dev);
}

UCLASS_DRIVER(battery) = {
.id = UCLASS_BATTERY,
.name = "battery",
};
@@ -28,6 +28,7 @@ enum uclass_id {
/* U-Boot uclasses start here - in alphabetical order */
UCLASS_ADC, /* Analog-to-digital converter */
UCLASS_AHCI, /* SATA disk controller */
UCLASS_BATTERY, /* Battery */
UCLASS_BLK, /* Block device */
UCLASS_CLK, /* Clock source, e.g. used by peripherals */
UCLASS_CPU, /* CPU, typically part of an SoC */
@@ -8,6 +8,8 @@
#ifndef __POWER_BATTERY_H_
#define __POWER_BATTERY_H_

#ifndef CONFIG_DM_BATTERY
/* TODO: remove this once all users have been moved over to DM */
struct battery {
unsigned int version;
unsigned int state_of_chrg;
@@ -19,4 +21,41 @@ struct battery {
};

int power_bat_init(unsigned char bus);
#else

enum dm_battery_state {
BAT_STATE_UNUSED = 0, /* never used */
BAT_STATE_NOT_PRESENT, /* battery is not present */
BAT_STATE_NEED_CHARGING, /* battery needs charging (i.e. low SOC or voltage) */
BAT_STATE_NORMAL, /* battery is OK */
};

/* Battery device operations */
struct dm_battery_ops {
/**
* Get the current voltage of the battery.
* @dev - battery device
* @uV - pointer to place to store voltage, in microvolts
* @return 0 if success, -errno otherwise.
*/
int (*get_voltage)(struct udevice *dev, unsigned int *uV);
/**
* Get the current battery status
* @dev - battery device
* @return -errno on error, enum dm_battery_state otherwise
*/
int (*get_status)(struct udevice *dev);
/**
* Get the battery's State Of Charge (SOC)
* @dev - battery device
* @return 0-100 value representing current battery charge percentage, -errno on error
*/
int (*get_soc)(struct udevice *dev);
};

int battery_get(const char *devname, struct udevice **devp);
int battery_get_voltage(struct udevice *dev, unsigned int *uV);
int battery_get_status(struct udevice *dev);
int battery_get_soc(struct udevice *dev);
#endif /* CONFIG_DM_BATTERY */
#endif /* __POWER_BATTERY_H_ */

0 comments on commit 948addd

Please sign in to comment.