rk818-charger: init at 0.1 (WIP)
this doesn't build yet. i'll also need to import the patched mfd driver.
This commit is contained in:
4
pkgs/additional/rk818-charger/Makefile
Normal file
4
pkgs/additional/rk818-charger/Makefile
Normal file
@@ -0,0 +1,4 @@
|
||||
obj-m := rk818_charger.o rk818_battery.o
|
||||
|
||||
all:
|
||||
$(MAKE) -C "$(KERNEL_DIR)" M="$(PWD)" modules
|
37
pkgs/additional/rk818-charger/default.nix
Normal file
37
pkgs/additional/rk818-charger/default.nix
Normal file
@@ -0,0 +1,37 @@
|
||||
# C code is taken from megi's kernel tree,
|
||||
# which he appears to have taken from the Rockchip BSP
|
||||
# i.e. https://github.com/rockchip-linux/kernel
|
||||
# Rockchip rk818 seems to have seen no development after 2021-06-09 (as of 2024-10-01)
|
||||
#
|
||||
# mainline linux contains `drivers/mfd/rk8xx-core.c` and `Documentation/devicetree/bindings/mfd/rockchip,rk818.yaml`
|
||||
# the former includes "rk817-charger", but nothing for the rk818.
|
||||
# the latter includes the PMIC, without mention of any battery/charger.
|
||||
# hmm...
|
||||
{
|
||||
kernel ? linuxPackages_6_11.kernel, linuxPackages_6_11,
|
||||
stdenv,
|
||||
}:
|
||||
|
||||
stdenv.mkDerivation {
|
||||
pname = "rk818-charger";
|
||||
version = "0-unstable-2024-10-01";
|
||||
|
||||
src = ./.;
|
||||
|
||||
hardeningDisable = [ "pic" ];
|
||||
nativeBuildInputs = kernel.moduleBuildDependencies;
|
||||
|
||||
makeFlags = [
|
||||
"KERNELRELEASE=${kernel.modDirVersion}"
|
||||
"KERNEL_DIR=${kernel.dev}/lib/modules/${kernel.modDirVersion}/build"
|
||||
"INSTALL_MOD_PATH=$(out)"
|
||||
];
|
||||
|
||||
# AFAICT the module names are really just the .o files.
|
||||
# i guess if you wanted a module with more than one file,
|
||||
# you would compile all their .c sources into one .o and add just that to `obj-m`?
|
||||
passthru.moduleNames = [
|
||||
"rk818_battery"
|
||||
"rk818_charger"
|
||||
];
|
||||
}
|
3498
pkgs/additional/rk818-charger/rk818_battery.c
Normal file
3498
pkgs/additional/rk818-charger/rk818_battery.c
Normal file
File diff suppressed because it is too large
Load Diff
168
pkgs/additional/rk818-charger/rk818_battery.h
Normal file
168
pkgs/additional/rk818-charger/rk818_battery.h
Normal file
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
* rk818_battery.h: fuel gauge driver structures
|
||||
*
|
||||
* Copyright (C) 2016 Rockchip Electronics Co., Ltd
|
||||
* Author: chenjh <chenjh@rock-chips.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*/
|
||||
|
||||
#ifndef RK818_BATTERY
|
||||
#define RK818_BATTERY
|
||||
|
||||
/* RK818_INT_STS_MSK_REG2 */
|
||||
#define PLUG_IN_MSK BIT(0)
|
||||
#define PLUG_OUT_MSK BIT(1)
|
||||
#define CHRG_CVTLMT_INT_MSK BIT(6)
|
||||
|
||||
/* RK818_TS_CTRL_REG */
|
||||
#define GG_EN BIT(7)
|
||||
#define ADC_CUR_EN BIT(6)
|
||||
#define ADC_TS1_EN BIT(5)
|
||||
#define ADC_TS2_EN BIT(4)
|
||||
#define TS1_CUR_MSK 0x03
|
||||
|
||||
/* RK818_GGCON */
|
||||
#define OCV_SAMP_MIN_MSK 0x0c
|
||||
#define OCV_SAMP_8MIN (0x00 << 2)
|
||||
|
||||
#define ADC_CAL_MIN_MSK 0x30
|
||||
#define ADC_CAL_8MIN (0x00 << 4)
|
||||
#define ADC_CUR_MODE BIT(1)
|
||||
|
||||
/* RK818_GGSTS */
|
||||
#define BAT_CON BIT(4)
|
||||
#define RELAX_VOL1_UPD BIT(3)
|
||||
#define RELAX_VOL2_UPD BIT(2)
|
||||
#define RELAX_VOL12_UPD_MSK (RELAX_VOL1_UPD | RELAX_VOL2_UPD)
|
||||
|
||||
/* RK818_SUP_STS_REG */
|
||||
#define CHRG_STATUS_MSK 0x70
|
||||
#define BAT_EXS BIT(7)
|
||||
#define CHARGE_OFF (0x0 << 4)
|
||||
#define DEAD_CHARGE (0x1 << 4)
|
||||
#define TRICKLE_CHARGE (0x2 << 4)
|
||||
#define CC_OR_CV (0x3 << 4)
|
||||
#define CHARGE_FINISH (0x4 << 4)
|
||||
#define USB_OVER_VOL (0x5 << 4)
|
||||
#define BAT_TMP_ERR (0x6 << 4)
|
||||
#define TIMER_ERR (0x7 << 4)
|
||||
#define USB_VLIMIT_EN BIT(3)
|
||||
#define USB_CLIMIT_EN BIT(2)
|
||||
#define USB_EXIST BIT(1)
|
||||
#define USB_EFF BIT(0)
|
||||
|
||||
/* RK818_USB_CTRL_REG */
|
||||
#define CHRG_CT_EN BIT(7)
|
||||
#define FINISH_CUR_MSK 0xc0
|
||||
#define TEMP_105C (0x02 << 2)
|
||||
#define FINISH_100MA (0x00 << 6)
|
||||
#define FINISH_150MA (0x01 << 6)
|
||||
#define FINISH_200MA (0x02 << 6)
|
||||
#define FINISH_250MA (0x03 << 6)
|
||||
|
||||
/* RK818_CHRG_CTRL_REG3 */
|
||||
#define CHRG_TERM_MODE_MSK BIT(5)
|
||||
#define CHRG_TERM_ANA_SIGNAL (0 << 5)
|
||||
#define CHRG_TERM_DIG_SIGNAL BIT(5)
|
||||
#define CHRG_TIMER_CCCV_EN BIT(2)
|
||||
#define CHRG_EN BIT(7)
|
||||
|
||||
/* RK818_VB_MON_REG */
|
||||
#define RK818_VBAT_LOW_3V0 0x02
|
||||
#define RK818_VBAT_LOW_3V4 0x06
|
||||
#define PLUG_IN_STS BIT(6)
|
||||
|
||||
/* RK818_THERMAL_REG */
|
||||
#define FB_TEMP_MSK 0x0c
|
||||
#define HOTDIE_STS BIT(1)
|
||||
|
||||
/* RK818_INT_STS_MSK_REG1 */
|
||||
#define VB_LOW_INT_EN BIT(1)
|
||||
|
||||
/* RK818_MISC_MARK_REG */
|
||||
#define FG_INIT BIT(5)
|
||||
#define FG_RESET_LATE BIT(4)
|
||||
#define FG_RESET_NOW BIT(3)
|
||||
#define ALGO_REST_MODE_MSK (0xc0)
|
||||
#define ALGO_REST_MODE_SHIFT 6
|
||||
|
||||
/* bit shift */
|
||||
#define FB_TEMP_SHIFT 2
|
||||
|
||||
/* parse ocv table param */
|
||||
#define TIMER_MS_COUNTS 1000
|
||||
#define MAX_PERCENTAGE 100
|
||||
#define MAX_INTERPOLATE 1000
|
||||
#define MAX_INT 0x7FFF
|
||||
|
||||
#define DRIVER_VERSION "7.1"
|
||||
|
||||
struct battery_platform_data {
|
||||
u32 *ocv_table;
|
||||
u32 *zero_table;
|
||||
u32 *ntc_table;
|
||||
u32 ocv_size;
|
||||
u32 max_chrg_voltage;
|
||||
u32 ntc_size;
|
||||
int ntc_degree_from;
|
||||
u32 pwroff_vol;
|
||||
u32 monitor_sec;
|
||||
u32 zero_algorithm_vol;
|
||||
u32 zero_reserve_dsoc;
|
||||
u32 bat_res;
|
||||
u32 design_capacity;
|
||||
u32 design_qmax;
|
||||
u32 sleep_enter_current;
|
||||
u32 sleep_exit_current;
|
||||
u32 max_soc_offset;
|
||||
u32 sample_res;
|
||||
u32 bat_mode;
|
||||
u32 fb_temp;
|
||||
u32 energy_mode;
|
||||
u32 cccv_hour;
|
||||
u32 ntc_uA;
|
||||
u32 ntc_factor;
|
||||
};
|
||||
|
||||
enum work_mode {
|
||||
MODE_ZERO = 0,
|
||||
MODE_FINISH,
|
||||
MODE_SMOOTH_CHRG,
|
||||
MODE_SMOOTH_DISCHRG,
|
||||
MODE_SMOOTH,
|
||||
};
|
||||
|
||||
enum bat_mode {
|
||||
MODE_BATTARY = 0,
|
||||
MODE_VIRTUAL,
|
||||
};
|
||||
|
||||
static const u16 feedback_temp_array[] = {
|
||||
85, 95, 105, 115
|
||||
};
|
||||
|
||||
static const u16 chrg_vol_sel_array[] = {
|
||||
4050, 4100, 4150, 4200, 4250, 4300, 4350
|
||||
};
|
||||
|
||||
static const u16 chrg_cur_sel_array[] = {
|
||||
1000, 1200, 1400, 1600, 1800, 2000, 2250, 2400, 2600, 2800, 3000
|
||||
};
|
||||
|
||||
static const u16 chrg_cur_input_array[] = {
|
||||
450, 80, 850, 1000, 1250, 1500, 1750, 2000, 2250, 2500, 2750, 3000
|
||||
};
|
||||
|
||||
void kernel_power_off(void);
|
||||
int rk818_bat_temp_notifier_register(struct notifier_block *nb);
|
||||
int rk818_bat_temp_notifier_unregister(struct notifier_block *nb);
|
||||
|
||||
#endif
|
725
pkgs/additional/rk818-charger/rk818_charger.c
Normal file
725
pkgs/additional/rk818-charger/rk818_charger.c
Normal file
@@ -0,0 +1,725 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
/*
|
||||
* rk818 usb power driver
|
||||
*
|
||||
* Copyright (c) 2021 Ondřej Jirman <megi@xff.cz>
|
||||
*/
|
||||
#include <linux/delay.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/mfd/rk808.h>
|
||||
#include <linux/mod_devicetable.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/of_device.h>
|
||||
#include <linux/platform_device.h>
|
||||
#include <linux/power_supply.h>
|
||||
#include <linux/regmap.h>
|
||||
|
||||
#define RK818_CHG_STS_MASK (7u << 4) /* charger status */
|
||||
#define RK818_CHG_STS_NONE (0u << 4)
|
||||
#define RK818_CHG_STS_WAKEUP_CUR (1u << 4)
|
||||
#define RK818_CHG_STS_TRICKLE_CUR (2u << 4)
|
||||
#define RK818_CHG_STS_CC_OR_CV (3u << 4)
|
||||
#define RK818_CHG_STS_TERMINATED (4u << 4)
|
||||
#define RK818_CHG_STS_USB_OV (5u << 4)
|
||||
#define RK818_CHG_STS_BAT_TEMP_FAULT (6u << 4)
|
||||
#define RK818_CHG_STS_TIMEOUT (7u << 4)
|
||||
|
||||
/* RK818_SUP_STS_REG */
|
||||
#define RK818_SUP_STS_USB_VLIM_EN BIT(3) /* input voltage limit enable */
|
||||
#define RK818_SUP_STS_USB_ILIM_EN BIT(2) /* input current limit enable */
|
||||
#define RK818_SUP_STS_USB_EXS BIT(1) /* USB power connected */
|
||||
#define RK818_SUP_STS_USB_EFF BIT(0) /* USB fault */
|
||||
|
||||
/* RK818_USB_CTRL_REG */
|
||||
#define RK818_USB_CTRL_USB_ILIM_MASK (0xfu)
|
||||
#define RK818_USB_CTRL_USB_CHG_SD_VSEL_OFFSET 4
|
||||
#define RK818_USB_CTRL_USB_CHG_SD_VSEL_MASK (0x7u << 4)
|
||||
|
||||
/* RK818_CHRG_CTRL_REG1 */
|
||||
#define RK818_CHRG_CTRL_REG1_CHRG_EN BIT(7)
|
||||
#define RK818_CHRG_CTRL_REG1_CHRG_VOL_SEL_OFFSET 4
|
||||
#define RK818_CHRG_CTRL_REG1_CHRG_VOL_SEL_MASK (0x7u << 4)
|
||||
#define RK818_CHRG_CTRL_REG1_CHRG_CUR_SEL_OFFSET 0
|
||||
#define RK818_CHRG_CTRL_REG1_CHRG_CUR_SEL_MASK (0xfu << 0)
|
||||
|
||||
/* RK818_CHRG_CTRL_REG3 */
|
||||
#define RK818_CHRG_CTRL_REG3_CHRG_TERM_DIGITAL BIT(5)
|
||||
|
||||
struct rk818_charger {
|
||||
struct device *dev;
|
||||
struct rk808 *rk818;
|
||||
struct regmap *regmap;
|
||||
|
||||
struct power_supply *usb_psy;
|
||||
struct power_supply *charger_psy;
|
||||
|
||||
bool apply_ilim;
|
||||
};
|
||||
|
||||
// {{{ USB supply
|
||||
|
||||
static int rk818_usb_set_input_current_max(struct rk818_charger *cg,
|
||||
int val)
|
||||
{
|
||||
int ret;
|
||||
unsigned reg;
|
||||
|
||||
if (val < 450000)
|
||||
reg = 1;
|
||||
else if (val < 850000)
|
||||
reg = 0;
|
||||
else if (val < 1000000)
|
||||
reg = 2;
|
||||
else if (val < 3000000)
|
||||
reg = 3 + (val - 1000000) / 250000;
|
||||
else
|
||||
reg = 11;
|
||||
|
||||
dev_info(cg->dev, "applying input current limit %d mA\n", val / 1000);
|
||||
|
||||
ret = regmap_update_bits(cg->regmap, RK818_USB_CTRL_REG,
|
||||
RK818_USB_CTRL_USB_ILIM_MASK, reg);
|
||||
if (ret)
|
||||
dev_err(cg->dev,
|
||||
"USB input current limit setting failed (%d)\n", ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int rk818_usb_get_input_current_max(struct rk818_charger *cg,
|
||||
int *val)
|
||||
{
|
||||
unsigned reg;
|
||||
int ret;
|
||||
|
||||
ret = regmap_read(cg->regmap, RK818_USB_CTRL_REG, ®);
|
||||
if (ret) {
|
||||
dev_err(cg->dev,
|
||||
"USB input current limit getting failed (%d)\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
reg &= RK818_USB_CTRL_USB_ILIM_MASK;
|
||||
if (reg == 0)
|
||||
*val = 450000;
|
||||
else if (reg == 1)
|
||||
*val = 80000;
|
||||
else if (reg == 2)
|
||||
*val = 850000;
|
||||
else if (reg < 11)
|
||||
*val = 1000000 + (reg - 3) * 250000;
|
||||
else
|
||||
*val = 3000000;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rk818_usb_set_input_voltage_min(struct rk818_charger *cg,
|
||||
int val)
|
||||
{
|
||||
unsigned reg;
|
||||
int ret;
|
||||
|
||||
if (val < 2780000)
|
||||
reg = 0;
|
||||
else if (val < 3270000)
|
||||
reg = (val - 2780000) / 70000;
|
||||
else
|
||||
reg = 7;
|
||||
|
||||
ret = regmap_update_bits(cg->regmap, RK818_USB_CTRL_REG,
|
||||
RK818_USB_CTRL_USB_CHG_SD_VSEL_MASK,
|
||||
reg << RK818_USB_CTRL_USB_CHG_SD_VSEL_OFFSET);
|
||||
if (ret)
|
||||
dev_err(cg->dev,
|
||||
"USB input voltage limit setting failed (%d)\n", ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int rk818_usb_get_input_voltage_min(struct rk818_charger *cg,
|
||||
int *val)
|
||||
{
|
||||
unsigned reg;
|
||||
int ret;
|
||||
|
||||
ret = regmap_read(cg->regmap, RK818_USB_CTRL_REG, ®);
|
||||
if (ret) {
|
||||
dev_err(cg->dev,
|
||||
"USB input voltage limit getting failed (%d)\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
reg &= RK818_USB_CTRL_USB_CHG_SD_VSEL_MASK;
|
||||
reg >>= RK818_USB_CTRL_USB_CHG_SD_VSEL_OFFSET;
|
||||
|
||||
*val = 2780000 + (reg * 70000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rk818_usb_power_get_property(struct power_supply *psy,
|
||||
enum power_supply_property psp,
|
||||
union power_supply_propval *val)
|
||||
{
|
||||
struct rk818_charger *cg = power_supply_get_drvdata(psy);
|
||||
unsigned reg;
|
||||
int ret;
|
||||
|
||||
switch (psp) {
|
||||
case POWER_SUPPLY_PROP_PRESENT:
|
||||
ret = regmap_read(cg->regmap, RK818_SUP_STS_REG, ®);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
val->intval = !!(reg & RK818_SUP_STS_USB_EXS);
|
||||
break;
|
||||
|
||||
case POWER_SUPPLY_PROP_HEALTH:
|
||||
ret = regmap_read(cg->regmap, RK818_SUP_STS_REG, ®);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
if (!(reg & RK818_SUP_STS_USB_EXS)) {
|
||||
val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
|
||||
} else if (reg & RK818_SUP_STS_USB_EFF) {
|
||||
val->intval = POWER_SUPPLY_HEALTH_GOOD;
|
||||
} else {
|
||||
val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
|
||||
return rk818_usb_get_input_voltage_min(cg, &val->intval);
|
||||
|
||||
case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
|
||||
return rk818_usb_get_input_current_max(cg, &val->intval);
|
||||
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rk818_usb_power_set_property(struct power_supply *psy,
|
||||
enum power_supply_property psp,
|
||||
const union power_supply_propval *val)
|
||||
{
|
||||
struct rk818_charger *cg = power_supply_get_drvdata(psy);
|
||||
|
||||
switch (psp) {
|
||||
case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
|
||||
return rk818_usb_set_input_voltage_min(cg, val->intval);
|
||||
|
||||
case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
|
||||
return rk818_usb_set_input_current_max(cg, val->intval);
|
||||
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
static int rk818_usb_power_prop_writeable(struct power_supply *psy,
|
||||
enum power_supply_property psp)
|
||||
{
|
||||
switch (psp) {
|
||||
case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
|
||||
case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Sync the input-current-limit with our parent supply (if we have one) */
|
||||
static void rk818_usb_power_external_power_changed(struct power_supply *psy)
|
||||
{
|
||||
struct rk818_charger *cg = power_supply_get_drvdata(psy);
|
||||
union power_supply_propval val;
|
||||
int ret;
|
||||
|
||||
ret = power_supply_get_property_from_supplier(cg->usb_psy,
|
||||
POWER_SUPPLY_PROP_CURRENT_MAX,
|
||||
&val);
|
||||
if (ret)
|
||||
return;
|
||||
|
||||
/*
|
||||
* We only want to start applying input current limit after we get first
|
||||
* non-0 value from the supplier. Until then, we keep the limit applied
|
||||
* by the bootloader. If we lower the limit before the charger is properly
|
||||
* detected, we risk boot failure due to insufficient power.
|
||||
*/
|
||||
if (!cg->apply_ilim) {
|
||||
if (!val.intval)
|
||||
return;
|
||||
|
||||
cg->apply_ilim = true;
|
||||
}
|
||||
|
||||
if (val.intval < 500000)
|
||||
val.intval = 500000;
|
||||
|
||||
rk818_usb_power_set_property(cg->usb_psy,
|
||||
POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
|
||||
&val);
|
||||
}
|
||||
|
||||
static enum power_supply_property rk818_usb_power_props[] = {
|
||||
POWER_SUPPLY_PROP_PRESENT,
|
||||
POWER_SUPPLY_PROP_HEALTH,
|
||||
POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
|
||||
POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT,
|
||||
};
|
||||
|
||||
static const struct power_supply_desc rk818_usb_desc = {
|
||||
.name = "rk818-usb",
|
||||
.type = POWER_SUPPLY_TYPE_USB,
|
||||
.properties = rk818_usb_power_props,
|
||||
.num_properties = ARRAY_SIZE(rk818_usb_power_props),
|
||||
.property_is_writeable = rk818_usb_power_prop_writeable,
|
||||
.get_property = rk818_usb_power_get_property,
|
||||
.set_property = rk818_usb_power_set_property,
|
||||
.external_power_changed = rk818_usb_power_external_power_changed,
|
||||
};
|
||||
|
||||
// }}}
|
||||
// {{{ Charger supply
|
||||
|
||||
static int rk818_charger_set_current_max(struct rk818_charger *cg, int val)
|
||||
{
|
||||
unsigned reg;
|
||||
int ret;
|
||||
|
||||
if (val < 1000000)
|
||||
reg = 0;
|
||||
else if (val < 3000000)
|
||||
reg = (val - 1000000) / 200000;
|
||||
else
|
||||
reg = 10;
|
||||
|
||||
ret = regmap_update_bits(cg->regmap, RK818_CHRG_CTRL_REG1,
|
||||
RK818_CHRG_CTRL_REG1_CHRG_CUR_SEL_MASK,
|
||||
reg << RK818_CHRG_CTRL_REG1_CHRG_CUR_SEL_OFFSET);
|
||||
if (ret)
|
||||
dev_err(cg->dev,
|
||||
"Charging max current setting failed (%d)\n", ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int rk818_charger_get_current_max(struct rk818_charger *cg, int *val)
|
||||
{
|
||||
unsigned reg;
|
||||
int ret;
|
||||
|
||||
ret = regmap_read(cg->regmap, RK818_CHRG_CTRL_REG1, ®);
|
||||
if (ret) {
|
||||
dev_err(cg->dev,
|
||||
"Charging max current getting failed (%d)\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
reg &= RK818_CHRG_CTRL_REG1_CHRG_CUR_SEL_MASK;
|
||||
reg >>= RK818_CHRG_CTRL_REG1_CHRG_CUR_SEL_OFFSET;
|
||||
|
||||
*val = 1000000 + reg * 200000;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rk818_charger_set_voltage_max(struct rk818_charger *cg, int val)
|
||||
{
|
||||
unsigned reg;
|
||||
int ret;
|
||||
|
||||
if (val < 4050000)
|
||||
reg = 0;
|
||||
else if (val < 4350000)
|
||||
reg = (val - 4050000) / 50000;
|
||||
else
|
||||
reg = 6;
|
||||
|
||||
ret = regmap_update_bits(cg->regmap, RK818_CHRG_CTRL_REG1,
|
||||
RK818_CHRG_CTRL_REG1_CHRG_VOL_SEL_MASK,
|
||||
reg << RK818_CHRG_CTRL_REG1_CHRG_VOL_SEL_OFFSET);
|
||||
if (ret)
|
||||
dev_err(cg->dev,
|
||||
"Charging end voltage setting failed (%d)\n", ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int rk818_charger_get_voltage_max(struct rk818_charger *cg, int *val)
|
||||
{
|
||||
unsigned reg;
|
||||
int ret;
|
||||
|
||||
ret = regmap_read(cg->regmap, RK818_CHRG_CTRL_REG1, ®);
|
||||
if (ret) {
|
||||
dev_err(cg->dev,
|
||||
"Charging end voltage getting failed (%d)\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
reg &= RK818_CHRG_CTRL_REG1_CHRG_VOL_SEL_MASK;
|
||||
reg >>= RK818_CHRG_CTRL_REG1_CHRG_VOL_SEL_OFFSET;
|
||||
|
||||
*val = 4050000 + reg * 50000;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct rk818_battery;
|
||||
struct rk818_battery* rk818_battery_get(void);
|
||||
int rk818_battery_get_property(struct rk818_battery *di,
|
||||
enum power_supply_property psp,
|
||||
union power_supply_propval *val);
|
||||
|
||||
static int rk818_charger_get_property(struct power_supply *psy,
|
||||
enum power_supply_property psp,
|
||||
union power_supply_propval *val)
|
||||
{
|
||||
struct rk818_charger *cg = power_supply_get_drvdata(psy);
|
||||
struct rk818_battery* di = rk818_battery_get();
|
||||
unsigned reg;
|
||||
int ret;
|
||||
|
||||
switch (psp) {
|
||||
case POWER_SUPPLY_PROP_CURRENT_NOW:
|
||||
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
||||
case POWER_SUPPLY_PROP_PRESENT:
|
||||
case POWER_SUPPLY_PROP_CAPACITY:
|
||||
case POWER_SUPPLY_PROP_TEMP:
|
||||
case POWER_SUPPLY_PROP_STATUS:
|
||||
case POWER_SUPPLY_PROP_CHARGE_COUNTER:
|
||||
case POWER_SUPPLY_PROP_CHARGE_FULL:
|
||||
if (!di)
|
||||
return -ENODEV;
|
||||
return rk818_battery_get_property(di, psp, val);
|
||||
default:;
|
||||
}
|
||||
|
||||
switch (psp) {
|
||||
case POWER_SUPPLY_PROP_ONLINE:
|
||||
ret = regmap_read(cg->regmap, RK818_CHRG_CTRL_REG1, ®);
|
||||
if (ret) {
|
||||
dev_err(cg->dev, "failed to read the charger state (%d)\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
val->intval = !!(reg & RK818_CHRG_CTRL_REG1_CHRG_EN);
|
||||
break;
|
||||
|
||||
case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR:
|
||||
ret = regmap_read(cg->regmap, RK818_CHRG_CTRL_REG1, ®);
|
||||
if (ret) {
|
||||
dev_err(cg->dev, "failed to read the charger state (%d)\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (reg & RK818_CHRG_CTRL_REG1_CHRG_EN)
|
||||
val->intval = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
|
||||
else
|
||||
val->intval = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE;
|
||||
|
||||
return 0;
|
||||
|
||||
case POWER_SUPPLY_PROP_STATUS:
|
||||
ret = regmap_read(cg->regmap, RK818_SUP_STS_REG, ®);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
switch (reg & RK818_CHG_STS_MASK) {
|
||||
case RK818_CHG_STS_WAKEUP_CUR:
|
||||
case RK818_CHG_STS_TRICKLE_CUR:
|
||||
case RK818_CHG_STS_CC_OR_CV:
|
||||
val->intval = POWER_SUPPLY_STATUS_CHARGING;
|
||||
break;
|
||||
case RK818_CHG_STS_TERMINATED:
|
||||
default:
|
||||
val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case POWER_SUPPLY_PROP_CHARGE_TYPE:
|
||||
ret = regmap_read(cg->regmap, RK818_SUP_STS_REG, ®);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
switch (reg & RK818_CHG_STS_MASK) {
|
||||
case RK818_CHG_STS_WAKEUP_CUR:
|
||||
case RK818_CHG_STS_TRICKLE_CUR:
|
||||
val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
|
||||
break;
|
||||
case RK818_CHG_STS_CC_OR_CV:
|
||||
val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
|
||||
break;
|
||||
case RK818_CHG_STS_TERMINATED:
|
||||
val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
|
||||
break;
|
||||
default:
|
||||
val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
|
||||
ret = regmap_read(cg->regmap, RK818_CHRG_CTRL_REG2, ®);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
val->intval = 100000 + ((reg >> 6) & 3) * 50000;
|
||||
break;
|
||||
|
||||
case POWER_SUPPLY_PROP_HEALTH:
|
||||
ret = regmap_read(cg->regmap, RK818_SUP_STS_REG, ®);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
switch (reg & RK818_CHG_STS_MASK) {
|
||||
case RK818_CHG_STS_USB_OV:
|
||||
val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
|
||||
break;
|
||||
case RK818_CHG_STS_BAT_TEMP_FAULT:
|
||||
val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
|
||||
break;
|
||||
case RK818_CHG_STS_TIMEOUT:
|
||||
val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
|
||||
break;
|
||||
default:
|
||||
val->intval = POWER_SUPPLY_HEALTH_GOOD;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
|
||||
return rk818_charger_get_voltage_max(cg, &val->intval);
|
||||
|
||||
case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
|
||||
ret = rk818_charger_get_current_max(cg, &val->intval);
|
||||
val->intval /= 10;
|
||||
return ret;
|
||||
|
||||
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
|
||||
return rk818_charger_get_current_max(cg, &val->intval);
|
||||
|
||||
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
|
||||
val->intval = 4350000;
|
||||
break;
|
||||
|
||||
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
|
||||
val->intval = 3000000;
|
||||
break;
|
||||
|
||||
case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
|
||||
val->intval = 11400000;
|
||||
return 0;
|
||||
|
||||
case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
|
||||
val->intval = 0;
|
||||
return 0;
|
||||
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rk818_charger_set_property(struct power_supply *psy,
|
||||
enum power_supply_property psp,
|
||||
const union power_supply_propval *val)
|
||||
{
|
||||
struct rk818_charger *cg = power_supply_get_drvdata(psy);
|
||||
|
||||
switch (psp) {
|
||||
case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR:
|
||||
switch (val->intval) {
|
||||
case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO:
|
||||
return regmap_update_bits(cg->regmap, RK818_CHRG_CTRL_REG1,
|
||||
RK818_CHRG_CTRL_REG1_CHRG_EN,
|
||||
RK818_CHRG_CTRL_REG1_CHRG_EN);
|
||||
case POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE:
|
||||
return regmap_update_bits(cg->regmap, RK818_CHRG_CTRL_REG1,
|
||||
RK818_CHRG_CTRL_REG1_CHRG_EN, 0);
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
|
||||
return rk818_charger_set_voltage_max(cg, val->intval);
|
||||
|
||||
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
|
||||
return rk818_charger_set_current_max(cg, val->intval);
|
||||
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
static int rk818_charger_prop_writeable(struct power_supply *psy,
|
||||
enum power_supply_property psp)
|
||||
{
|
||||
switch (psp) {
|
||||
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
|
||||
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
|
||||
case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR:
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static enum power_supply_property rk818_charger_props[] = {
|
||||
POWER_SUPPLY_PROP_ONLINE,
|
||||
POWER_SUPPLY_PROP_HEALTH,
|
||||
POWER_SUPPLY_PROP_STATUS,
|
||||
POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR,
|
||||
POWER_SUPPLY_PROP_CHARGE_TYPE,
|
||||
POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
|
||||
POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
|
||||
POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
|
||||
POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
|
||||
POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
|
||||
POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
|
||||
POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
|
||||
POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN,
|
||||
|
||||
// inherited from BSP battery driver
|
||||
POWER_SUPPLY_PROP_CURRENT_NOW,
|
||||
POWER_SUPPLY_PROP_VOLTAGE_NOW,
|
||||
POWER_SUPPLY_PROP_PRESENT,
|
||||
POWER_SUPPLY_PROP_CAPACITY,
|
||||
POWER_SUPPLY_PROP_TEMP,
|
||||
POWER_SUPPLY_PROP_CHARGE_COUNTER,
|
||||
POWER_SUPPLY_PROP_CHARGE_FULL,
|
||||
};
|
||||
|
||||
/*
|
||||
* We import some capacity tracking functionality from the BSP battery driver.
|
||||
* Some poor soul will have to understand and clean up the BSP battery driver,
|
||||
* but not me, not now. :)
|
||||
*/
|
||||
static const struct power_supply_desc rk818_charger_desc = {
|
||||
.name = "rk818-battery",
|
||||
.type = POWER_SUPPLY_TYPE_BATTERY,
|
||||
.properties = rk818_charger_props,
|
||||
.num_properties = ARRAY_SIZE(rk818_charger_props),
|
||||
.property_is_writeable = rk818_charger_prop_writeable,
|
||||
.get_property = rk818_charger_get_property,
|
||||
.set_property = rk818_charger_set_property,
|
||||
.charge_behaviours = BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO) | BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE),
|
||||
};
|
||||
|
||||
// }}}
|
||||
|
||||
static int rk818_charger_probe(struct platform_device *pdev)
|
||||
{
|
||||
struct rk808 *rk818 = dev_get_drvdata(pdev->dev.parent);
|
||||
struct power_supply_config psy_cfg = { };
|
||||
struct device *dev = &pdev->dev;
|
||||
struct rk818_charger *cg;
|
||||
int ret;
|
||||
|
||||
cg = devm_kzalloc(dev, sizeof(*cg), GFP_KERNEL);
|
||||
if (!cg)
|
||||
return -ENOMEM;
|
||||
|
||||
cg->rk818 = rk818;
|
||||
cg->dev = dev;
|
||||
cg->regmap = rk818->regmap;
|
||||
platform_set_drvdata(pdev, cg);
|
||||
|
||||
psy_cfg.drv_data = cg;
|
||||
psy_cfg.of_node = dev->of_node;
|
||||
|
||||
cg->usb_psy = devm_power_supply_register(dev, &rk818_usb_desc,
|
||||
&psy_cfg);
|
||||
if (IS_ERR(cg->usb_psy))
|
||||
return dev_err_probe(dev, PTR_ERR(cg->usb_psy),
|
||||
"register usb power supply fail\n");
|
||||
|
||||
cg->charger_psy = devm_power_supply_register(dev, &rk818_charger_desc,
|
||||
&psy_cfg);
|
||||
if (IS_ERR(cg->charger_psy))
|
||||
return dev_err_probe(dev, PTR_ERR(cg->charger_psy),
|
||||
"register charger power supply fail\n");
|
||||
|
||||
/* disable voltage limit and enable input current limit */
|
||||
ret = regmap_update_bits(cg->regmap, RK818_SUP_STS_REG,
|
||||
RK818_SUP_STS_USB_ILIM_EN | RK818_SUP_STS_USB_VLIM_EN,
|
||||
RK818_SUP_STS_USB_ILIM_EN);
|
||||
if (ret)
|
||||
dev_warn(cg->dev, "failed to enable input current limit (%d)\n", ret);
|
||||
|
||||
/* make sure analog control loop is enabled */
|
||||
ret = regmap_update_bits(cg->regmap, RK818_CHRG_CTRL_REG3,
|
||||
RK818_CHRG_CTRL_REG3_CHRG_TERM_DIGITAL,
|
||||
0);
|
||||
if (ret)
|
||||
dev_warn(cg->dev, "failed to enable analog control loop (%d)\n", ret);
|
||||
|
||||
/* enable charger and set some reasonable limits on each boot */
|
||||
ret = regmap_write(cg->regmap, RK818_CHRG_CTRL_REG1,
|
||||
RK818_CHRG_CTRL_REG1_CHRG_EN
|
||||
| (1) /* 1.2A */
|
||||
| (5 << 4) /* 4.3V */);
|
||||
if (ret)
|
||||
dev_warn(cg->dev, "failed to enable charger (%d)\n", ret);
|
||||
|
||||
rk818_usb_power_external_power_changed(cg->usb_psy);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void rk818_charger_remove(struct platform_device *pdev)
|
||||
{
|
||||
//struct rk818_charger *cg = platform_get_drvdata(pdev);
|
||||
}
|
||||
|
||||
static void rk818_charger_shutdown(struct platform_device *pdev)
|
||||
{
|
||||
}
|
||||
|
||||
static int rk818_charger_suspend(struct platform_device *pdev,
|
||||
pm_message_t state)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rk818_charger_resume(struct platform_device *pdev)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct of_device_id rk818_charger_of_match[] = {
|
||||
{ .compatible = "rockchip,rk818-charger", },
|
||||
{ },
|
||||
};
|
||||
|
||||
static struct platform_driver rk818_charger_driver = {
|
||||
.probe = rk818_charger_probe,
|
||||
.remove = rk818_charger_remove,
|
||||
.suspend = rk818_charger_suspend,
|
||||
.resume = rk818_charger_resume,
|
||||
.shutdown = rk818_charger_shutdown,
|
||||
.driver = {
|
||||
.name = "rk818-charger",
|
||||
.of_match_table = rk818_charger_of_match,
|
||||
},
|
||||
};
|
||||
|
||||
module_platform_driver(rk818_charger_driver);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_ALIAS("platform:rk818-charger");
|
||||
MODULE_AUTHOR("Ondřej Jirman <megi@xff.cz>");
|
@@ -93,6 +93,7 @@ let
|
||||
pyln-client = callPackage ./additional/pyln-client { };
|
||||
pyln-proto = callPackage ./additional/pyln-proto { };
|
||||
qmkPackages = recurseIntoAttrs (callPackage ./additional/qmk-packages { });
|
||||
rk818-charger = callPackage ./additional/rk818-charger { };
|
||||
rtl8723cs-firmware = callPackage ./additional/rtl8723cs-firmware { };
|
||||
rtl8723cs-wowlan = callPackage ./additional/rtl8723cs-wowlan { };
|
||||
sane-backgrounds = callPackage ./additional/sane-backgrounds { };
|
||||
|
Reference in New Issue
Block a user