mirror of
https://github.com/AetherDroid/android_kernel_samsung_on5xelte.git
synced 2025-09-08 17:18:05 -04:00
Fixed MTP to work with TWRP
This commit is contained in:
commit
f6dfaef42e
50820 changed files with 20846062 additions and 0 deletions
759
drivers/power/s2mu003_fuelgauge.c
Normal file
759
drivers/power/s2mu003_fuelgauge.c
Normal file
|
@ -0,0 +1,759 @@
|
|||
/*
|
||||
* Samsung S2MU003 Fuel Gauge Driver
|
||||
*
|
||||
* Copyright (c) 2015 Samsung Electronics Co., Ltd.
|
||||
* http://www.samsung.com
|
||||
* Author: Junhan Bae <junhan84.bae@samsung.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include <linux/mfd/samsung/s2mu003.h>
|
||||
#include <linux/power/s2mu003_fuelgauge.h>
|
||||
#include <linux/of_gpio.h>
|
||||
|
||||
static enum power_supply_property s2mu003_fuelgauge_props[] = {
|
||||
POWER_SUPPLY_PROP_STATUS,
|
||||
POWER_SUPPLY_PROP_VOLTAGE_NOW,
|
||||
POWER_SUPPLY_PROP_VOLTAGE_AVG,
|
||||
POWER_SUPPLY_PROP_CURRENT_NOW,
|
||||
POWER_SUPPLY_PROP_CURRENT_AVG,
|
||||
POWER_SUPPLY_PROP_CHARGE_FULL,
|
||||
POWER_SUPPLY_PROP_ENERGY_NOW,
|
||||
POWER_SUPPLY_PROP_CAPACITY,
|
||||
POWER_SUPPLY_PROP_TEMP,
|
||||
POWER_SUPPLY_PROP_TEMP_AMBIENT,
|
||||
};
|
||||
|
||||
static int s2mu003_init_regs(struct s2mu003_fuelgauge_data *fuelgauge)
|
||||
{
|
||||
int ret = 0;
|
||||
u8 data;
|
||||
|
||||
pr_info("%s: s2mu003 fuelgauge initialize\n", __func__);
|
||||
|
||||
data = i2c_smbus_read_byte_data(fuelgauge->i2c, 0x2e);
|
||||
data &= ~(0x01 << 3);
|
||||
ret = i2c_smbus_write_byte_data(fuelgauge->i2c, 0x2e, data);
|
||||
|
||||
data = i2c_smbus_read_byte_data(fuelgauge->i2c, 0x1A);
|
||||
data |= 0x03;
|
||||
ret = i2c_smbus_write_byte_data(fuelgauge->i2c, 0x1A, data);
|
||||
|
||||
if (ret < 0)
|
||||
dev_err(&fuelgauge->i2c->dev, "%s: Error(%d)\n", __func__, ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void s2mu003_alert_init(struct s2mu003_fuelgauge_data *fuelgauge)
|
||||
{
|
||||
u8 data[2];
|
||||
|
||||
/* VBAT Threshold setting */
|
||||
data[0] = 0x00 & 0x0f;
|
||||
|
||||
/* SOC Threshold setting */
|
||||
data[0] = data[0] | (fuelgauge->pdata->fuel_alert_soc << 4);
|
||||
|
||||
data[1] = 0x00;
|
||||
i2c_smbus_write_i2c_block_data(fuelgauge->i2c,
|
||||
S2MU003_REG_IRQ_LVL, 2, data);
|
||||
}
|
||||
|
||||
static bool s2mu003_check_status(struct i2c_client *client)
|
||||
{
|
||||
u8 data[2];
|
||||
bool ret = false;
|
||||
|
||||
/* check if Smn was generated */
|
||||
if (i2c_smbus_read_i2c_block_data(client, S2MU003_REG_STATUS, 2, data) < 0)
|
||||
return ret;
|
||||
|
||||
dev_dbg(&client->dev, "%s: status to (%02x%02x)\n",
|
||||
__func__, data[1], data[0]);
|
||||
|
||||
if (data[1] & (0x1 << 1))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
static int s2mu003_set_temperature(struct s2mu003_fuelgauge_data *fuelgauge,
|
||||
int temperature)
|
||||
{
|
||||
u8 data[2];
|
||||
char val;
|
||||
|
||||
val = temperature / 10;
|
||||
|
||||
if (val < -5)
|
||||
val = 0xf6;
|
||||
else if (val >= -5 && val < 5)
|
||||
val = 0x00;
|
||||
else if (val >= 5 && val < 15)
|
||||
val = 0x0a;
|
||||
else if (val >= 15 && val < 35)
|
||||
val = 0x19;
|
||||
else if (val >= 35)
|
||||
val = 0x28;
|
||||
data[0] = val;
|
||||
data[1] = 0x00;
|
||||
#if 0
|
||||
s2mu003_block_write_device(fuelgauge->i2c, S2MU003_REG_RTEMP, 2, data);
|
||||
#endif
|
||||
dev_dbg(&fuelgauge->i2c->dev, "%s: temperature to (%d)\n",
|
||||
__func__, temperature);
|
||||
|
||||
return temperature;
|
||||
}
|
||||
|
||||
static int s2mu003_get_temperature(struct s2mu003_fuelgauge_data *fuelgauge)
|
||||
{
|
||||
u8 data[2];
|
||||
s32 temperature = 0;
|
||||
|
||||
if (i2c_smbus_read_i2c_block_data(fuelgauge->i2c,
|
||||
S2MU003_REG_RTEMP, 2, data) < 0)
|
||||
return -ERANGE;
|
||||
|
||||
/* data[] store 2's compliment format number */
|
||||
if (data[0] & (0x1 << 7)) {
|
||||
/* Negative */
|
||||
temperature = ((~(data[0])) & 0xFF) + 1;
|
||||
temperature *= -10;
|
||||
} else {
|
||||
temperature = data[0] & 0x7F;
|
||||
temperature *= 10;
|
||||
}
|
||||
|
||||
dev_dbg(&fuelgauge->i2c->dev, "%s: temperature (%d)\n",
|
||||
__func__, temperature);
|
||||
|
||||
return temperature;
|
||||
}
|
||||
|
||||
/* soc should be 0.01% unit */
|
||||
static int s2mu003_get_soc(struct s2mu003_fuelgauge_data *fuelgauge)
|
||||
{
|
||||
u8 data[2], check_data[2];
|
||||
u16 compliment;
|
||||
int rsoc, i;
|
||||
|
||||
for (i = 0; i < 50; i++) {
|
||||
if (i2c_smbus_read_i2c_block_data(fuelgauge->i2c,
|
||||
S2MU003_REG_RSOC, 2, data) < 0)
|
||||
return -EINVAL;
|
||||
if (i2c_smbus_read_i2c_block_data(fuelgauge->i2c,
|
||||
S2MU003_REG_RSOC, 2, check_data) < 0)
|
||||
return -EINVAL;
|
||||
if ((data[0] == check_data[0]) && (data[1] == check_data[1]))
|
||||
break;
|
||||
}
|
||||
|
||||
compliment = (data[1] << 8) | (data[0]);
|
||||
|
||||
/* data[] store 2's compliment format number */
|
||||
if (compliment & (0x1 << 15)) {
|
||||
/* Negative */
|
||||
rsoc = ((~compliment) & 0xFFFF) + 1;
|
||||
rsoc = (rsoc * (-10000)) / (0x1 << 12);
|
||||
} else {
|
||||
rsoc = compliment & 0x7FFF;
|
||||
rsoc = ((rsoc * 10000) / (0x1 << 12));
|
||||
}
|
||||
|
||||
dev_info(&fuelgauge->i2c->dev, "%s: raw capacity (0x%x:%d)\n", __func__,
|
||||
compliment, rsoc);
|
||||
|
||||
return min(rsoc, 10000) / 10;
|
||||
}
|
||||
|
||||
static int s2mu003_get_rawsoc(struct s2mu003_fuelgauge_data *fuelgauge)
|
||||
{
|
||||
u8 data[2], check_data[2];
|
||||
u16 compliment;
|
||||
int rsoc, i;
|
||||
|
||||
for (i = 0; i < 50; i++) {
|
||||
if (i2c_smbus_read_i2c_block_data(fuelgauge->i2c,
|
||||
S2MU003_REG_RSOC, 2, data) < 0)
|
||||
return -EINVAL;
|
||||
if (i2c_smbus_read_i2c_block_data(fuelgauge->i2c,
|
||||
S2MU003_REG_RSOC, 2, check_data) < 0)
|
||||
return -EINVAL;
|
||||
if ((data[0] == check_data[0]) && (data[1] == check_data[1]))
|
||||
break;
|
||||
}
|
||||
|
||||
compliment = (data[1] << 8) | (data[0]);
|
||||
|
||||
/* data[] store 2's compliment format number */
|
||||
if (compliment & (0x1 << 15)) {
|
||||
/* Negative */
|
||||
rsoc = ((~compliment) & 0xFFFF) + 1;
|
||||
rsoc = (rsoc * (-10000)) / (0x1 << 12);
|
||||
} else {
|
||||
rsoc = compliment & 0x7FFF;
|
||||
rsoc = ((rsoc * 10000) / (0x1 << 12));
|
||||
}
|
||||
|
||||
dev_info(&fuelgauge->i2c->dev, "%s: raw capacity (0x%x:%d)\n", __func__,
|
||||
compliment, rsoc);
|
||||
|
||||
return min(rsoc, 10000);
|
||||
}
|
||||
|
||||
static int s2mu003_get_ocv(struct s2mu003_fuelgauge_data *fuelgauge)
|
||||
{
|
||||
u8 data[2];
|
||||
u32 rocv = 0;
|
||||
|
||||
if (i2c_smbus_read_i2c_block_data(fuelgauge->i2c,
|
||||
S2MU003_REG_ROCV, 2, data) < 0)
|
||||
return -EINVAL;
|
||||
|
||||
rocv = ((data[0] + (data[1] << 8)) * 1000) >> 13;
|
||||
|
||||
dev_dbg(&fuelgauge->i2c->dev, "%s: rocv (%d)\n", __func__, rocv);
|
||||
|
||||
return rocv;
|
||||
}
|
||||
|
||||
static int s2mu003_get_vbat(struct s2mu003_fuelgauge_data *fuelgauge)
|
||||
{
|
||||
u8 data[2];
|
||||
u32 vbat = 0;
|
||||
|
||||
if (i2c_smbus_read_i2c_block_data(fuelgauge->i2c,
|
||||
S2MU003_REG_RVBAT, 2, data) < 0)
|
||||
return -EINVAL;
|
||||
vbat = ((data[0] + (data[1] << 8)) * 1000) >> 13;
|
||||
|
||||
dev_dbg(&fuelgauge->i2c->dev, "%s: vbat (%d)\n", __func__, vbat);
|
||||
|
||||
return vbat;
|
||||
}
|
||||
|
||||
static int s2mu003_get_avgvbat(struct s2mu003_fuelgauge_data *fuelgauge)
|
||||
{
|
||||
u8 data[2];
|
||||
u32 new_vbat, old_vbat = 0;
|
||||
int cnt;
|
||||
|
||||
for (cnt = 0; cnt < 5; cnt++) {
|
||||
if (i2c_smbus_read_i2c_block_data(fuelgauge->i2c,
|
||||
S2MU003_REG_RVBAT, 2, data) < 0)
|
||||
return -EINVAL;
|
||||
|
||||
new_vbat = ((data[0] + (data[1] << 8)) * 1000) >> 13;
|
||||
|
||||
if (cnt == 0)
|
||||
old_vbat = new_vbat;
|
||||
else
|
||||
old_vbat = new_vbat / 2 + old_vbat / 2;
|
||||
}
|
||||
|
||||
dev_dbg(&fuelgauge->i2c->dev, "%s: avgvbat (%d)\n", __func__, old_vbat);
|
||||
|
||||
return old_vbat;
|
||||
}
|
||||
|
||||
/* capacity is 0.1% unit */
|
||||
static void s2mu003_fg_get_scaled_capacity(
|
||||
struct s2mu003_fuelgauge_data *fuelgauge,
|
||||
union power_supply_propval *val)
|
||||
{
|
||||
val->intval = (val->intval < fuelgauge->pdata->capacity_min) ?
|
||||
0 : ((val->intval - fuelgauge->pdata->capacity_min) * 1000 /
|
||||
(fuelgauge->capacity_max - fuelgauge->pdata->capacity_min));
|
||||
|
||||
dev_dbg(&fuelgauge->i2c->dev,
|
||||
"%s: scaled capacity (%d.%d)\n",
|
||||
__func__, val->intval/10, val->intval%10);
|
||||
}
|
||||
|
||||
/* capacity is integer */
|
||||
static void s2mu003_fg_get_atomic_capacity(
|
||||
struct s2mu003_fuelgauge_data *fuelgauge,
|
||||
union power_supply_propval *val)
|
||||
{
|
||||
if (fuelgauge->pdata->capacity_calculation_type &
|
||||
SEC_FUELGAUGE_CAPACITY_TYPE_ATOMIC) {
|
||||
if (fuelgauge->capacity_old < val->intval)
|
||||
val->intval = fuelgauge->capacity_old + 1;
|
||||
else if (fuelgauge->capacity_old > val->intval)
|
||||
val->intval = fuelgauge->capacity_old - 1;
|
||||
}
|
||||
|
||||
/* updated old capacity */
|
||||
fuelgauge->capacity_old = val->intval;
|
||||
}
|
||||
|
||||
static int s2mu003_fg_calculate_dynamic_scale(
|
||||
struct s2mu003_fuelgauge_data *fuelgauge, int capacity)
|
||||
{
|
||||
union power_supply_propval raw_soc_val;
|
||||
raw_soc_val.intval = s2mu003_get_rawsoc(fuelgauge) / 10;
|
||||
|
||||
if (raw_soc_val.intval <
|
||||
fuelgauge->pdata->capacity_max -
|
||||
fuelgauge->pdata->capacity_max_margin) {
|
||||
fuelgauge->capacity_max =
|
||||
fuelgauge->pdata->capacity_max -
|
||||
fuelgauge->pdata->capacity_max_margin;
|
||||
pr_debug("%s: capacity_max (%d)", __func__,
|
||||
fuelgauge->capacity_max);
|
||||
} else {
|
||||
fuelgauge->capacity_max =
|
||||
(raw_soc_val.intval >
|
||||
fuelgauge->pdata->capacity_max +
|
||||
fuelgauge->pdata->capacity_max_margin) ?
|
||||
(fuelgauge->pdata->capacity_max +
|
||||
fuelgauge->pdata->capacity_max_margin) :
|
||||
raw_soc_val.intval;
|
||||
pr_debug("%s: raw soc (%d)", __func__,
|
||||
fuelgauge->capacity_max);
|
||||
}
|
||||
|
||||
if (capacity != 100) {
|
||||
fuelgauge->capacity_max =
|
||||
(fuelgauge->capacity_max * 100 / capacity);
|
||||
fuelgauge->capacity_old = capacity;
|
||||
} else {
|
||||
fuelgauge->capacity_max =
|
||||
(fuelgauge->capacity_max * 99 / 100);
|
||||
fuelgauge->capacity_old = 100;
|
||||
}
|
||||
|
||||
pr_info("%s: %d is used for capacity_max, capacity(%d)\n",
|
||||
__func__, fuelgauge->capacity_max, capacity);
|
||||
|
||||
return fuelgauge->capacity_max;
|
||||
}
|
||||
|
||||
bool s2mu003_fuelgauge_fuelalert_init(struct i2c_client *client, int soc)
|
||||
{
|
||||
struct s2mu003_fuelgauge_data *fuelgauge = i2c_get_clientdata(client);
|
||||
u8 data[2];
|
||||
|
||||
/* 1. Set s2mu003 alert configuration. */
|
||||
s2mu003_alert_init(fuelgauge);
|
||||
|
||||
if (i2c_smbus_read_i2c_block_data(client, S2MU003_REG_IRQ, 2, data) < 0)
|
||||
return -1;
|
||||
|
||||
/*Enable VBAT, SOC */
|
||||
data[1] &= 0xfc;
|
||||
|
||||
/*Disable IDLE_ST, INIT)ST */
|
||||
data[1] |= 0x0c;
|
||||
|
||||
i2c_smbus_write_i2c_block_data(client, S2MU003_REG_IRQ, 2, data);
|
||||
|
||||
dev_dbg(&client->dev, "%s: irq_reg(%02x%02x) irq(%d)\n",
|
||||
__func__, data[1], data[0], fuelgauge->pdata->fg_irq);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool s2mu003_fuelgauge_is_fuelalerted(struct s2mu003_fuelgauge_data *fuelgauge)
|
||||
{
|
||||
return s2mu003_check_status(fuelgauge->i2c);
|
||||
}
|
||||
|
||||
bool s2mu003_hal_fg_fuelalert_process(void *irq_data, bool is_fuel_alerted)
|
||||
{
|
||||
struct s2mu003_fuelgauge_data *fuelgauge = irq_data;
|
||||
int ret;
|
||||
|
||||
ret = i2c_smbus_write_byte_data(fuelgauge->i2c, S2MU003_REG_IRQ, 0x00);
|
||||
if (ret < 0)
|
||||
dev_err(&fuelgauge->i2c->dev, "%s: Error(%d)\n", __func__, ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool s2mu003_hal_fg_full_charged(struct i2c_client *client)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static int s2mu003_fg_get_property(struct power_supply *psy,
|
||||
enum power_supply_property psp,
|
||||
union power_supply_propval *val)
|
||||
{
|
||||
struct s2mu003_fuelgauge_data *fuelgauge =
|
||||
container_of(psy, struct s2mu003_fuelgauge_data, psy_fg);
|
||||
|
||||
switch (psp) {
|
||||
/* Cell voltage (VCELL, mV) */
|
||||
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
||||
val->intval = s2mu003_get_vbat(fuelgauge);
|
||||
break;
|
||||
/* Additional Voltage Information (mV) */
|
||||
case POWER_SUPPLY_PROP_VOLTAGE_AVG:
|
||||
switch (val->intval) {
|
||||
case SEC_BATTERY_VOLTAGE_AVERAGE:
|
||||
val->intval = s2mu003_get_avgvbat(fuelgauge);
|
||||
break;
|
||||
case SEC_BATTERY_VOLTAGE_OCV:
|
||||
val->intval = s2mu003_get_ocv(fuelgauge);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
/* Current (mA) */
|
||||
case POWER_SUPPLY_PROP_CURRENT_NOW:
|
||||
val->intval = 0;
|
||||
break;
|
||||
/* Average Current (mA) */
|
||||
case POWER_SUPPLY_PROP_CURRENT_AVG:
|
||||
val->intval = 0;
|
||||
break;
|
||||
case POWER_SUPPLY_PROP_CAPACITY:
|
||||
if (val->intval == SEC_FUELGAUGE_CAPACITY_TYPE_RAW) {
|
||||
val->intval = s2mu003_get_rawsoc(fuelgauge);
|
||||
} else {
|
||||
val->intval = s2mu003_get_soc(fuelgauge);
|
||||
|
||||
if (fuelgauge->pdata->capacity_calculation_type &
|
||||
(SEC_FUELGAUGE_CAPACITY_TYPE_SCALE |
|
||||
SEC_FUELGAUGE_CAPACITY_TYPE_DYNAMIC_SCALE))
|
||||
s2mu003_fg_get_scaled_capacity(fuelgauge, val);
|
||||
|
||||
/* capacity should be between 0% and 100%
|
||||
* (0.1% degree)
|
||||
*/
|
||||
if (val->intval > 1000)
|
||||
val->intval = 1000;
|
||||
if (val->intval < 0)
|
||||
val->intval = 0;
|
||||
|
||||
/* get only integer part */
|
||||
val->intval /= 10;
|
||||
|
||||
/* check whether doing the wake_unlock */
|
||||
if ((val->intval > fuelgauge->pdata->fuel_alert_soc) &&
|
||||
fuelgauge->is_fuel_alerted) {
|
||||
wake_unlock(&fuelgauge->fuel_alert_wake_lock);
|
||||
s2mu003_fuelgauge_fuelalert_init(fuelgauge
|
||||
->i2c, fuelgauge->pdata->fuel_alert_soc);
|
||||
}
|
||||
|
||||
/* (Only for atomic capacity)
|
||||
* In initial time, capacity_old is 0.
|
||||
* and in resume from sleep,
|
||||
* capacity_old is too different from actual soc.
|
||||
* should update capacity_old
|
||||
* by val->intval in booting or resume.
|
||||
*/
|
||||
if (fuelgauge->initial_update_of_soc) {
|
||||
/* updated old capacity */
|
||||
fuelgauge->capacity_old = val->intval;
|
||||
fuelgauge->initial_update_of_soc = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (fuelgauge->pdata->capacity_calculation_type &
|
||||
(SEC_FUELGAUGE_CAPACITY_TYPE_ATOMIC |
|
||||
SEC_FUELGAUGE_CAPACITY_TYPE_SKIP_ABNORMAL))
|
||||
s2mu003_fg_get_atomic_capacity(fuelgauge, val);
|
||||
}
|
||||
|
||||
break;
|
||||
/* Battery Temperature */
|
||||
case POWER_SUPPLY_PROP_TEMP:
|
||||
/* Target Temperature */
|
||||
case POWER_SUPPLY_PROP_TEMP_AMBIENT:
|
||||
val->intval = s2mu003_get_temperature(fuelgauge);
|
||||
break;
|
||||
case POWER_SUPPLY_PROP_STATUS:
|
||||
val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static int s2mu003_fg_set_property(struct power_supply *psy,
|
||||
enum power_supply_property psp,
|
||||
const union power_supply_propval *val)
|
||||
{
|
||||
struct s2mu003_fuelgauge_data *fuelgauge =
|
||||
container_of(psy, struct s2mu003_fuelgauge_data, psy_fg);
|
||||
|
||||
switch (psp) {
|
||||
case POWER_SUPPLY_PROP_TEMP:
|
||||
/* Target Temperature */
|
||||
case POWER_SUPPLY_PROP_TEMP_AMBIENT:
|
||||
s2mu003_set_temperature(fuelgauge, val->intval);
|
||||
break;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_OF
|
||||
static int s2mu003_fuelgauge_parse_dt(struct s2mu003_fuelgauge_data *fuelgauge)
|
||||
{
|
||||
|
||||
struct device_node *np =
|
||||
of_find_node_by_name(NULL, "s2mu003-fuelgauge");
|
||||
int ret;
|
||||
int i, len;
|
||||
const u32 *p;
|
||||
|
||||
/* reset, irq gpio info */
|
||||
if (np == NULL) {
|
||||
pr_err("%s np NULL1\n", __func__);
|
||||
} else {
|
||||
ret = of_property_read_u32(np, "fuelgauge,capacity_max",
|
||||
&fuelgauge->pdata->capacity_max);
|
||||
if (ret < 0)
|
||||
pr_err("%s error reading capacity_max %d\n",
|
||||
__func__, ret);
|
||||
|
||||
ret = of_property_read_u32(np, "fuelgauge,capacity_max_margin",
|
||||
&fuelgauge->pdata->capacity_max_margin);
|
||||
if (ret < 0)
|
||||
pr_err("%s error reading capacity_max_margin %d\n",
|
||||
__func__, ret);
|
||||
|
||||
ret = of_property_read_u32(np, "fuelgauge,capacity_min",
|
||||
&fuelgauge->pdata->capacity_min);
|
||||
if (ret < 0)
|
||||
pr_err("%s error reading capacity_min %d\n",
|
||||
__func__, ret);
|
||||
|
||||
ret = of_property_read_u32(np,
|
||||
"fuelgauge,capacity_calculation_type",
|
||||
&fuelgauge->pdata->capacity_calculation_type);
|
||||
if (ret < 0)
|
||||
pr_err("%s error reading capacity_calculation_type %d\n",
|
||||
__func__, ret);
|
||||
|
||||
ret = of_property_read_u32(np, "fuelgauge,fuel_alert_soc",
|
||||
&fuelgauge->pdata->fuel_alert_soc);
|
||||
if (ret < 0)
|
||||
pr_err("%s error reading pdata->fuel_alert_soc %d\n",
|
||||
__func__, ret);
|
||||
fuelgauge->pdata->repeated_fuelalert = of_property_read_bool(np,
|
||||
"fuelgauge,repeated_fuelalert");
|
||||
|
||||
np = of_find_node_by_name(NULL, "battery");
|
||||
if (!np) {
|
||||
pr_err("%s np NULL2\n", __func__);
|
||||
return 1;
|
||||
} else {
|
||||
ret = of_property_read_string(np,
|
||||
"battery,fuelgauge_name",
|
||||
(char const **)&fuelgauge->
|
||||
pdata->fuelgauge_name);
|
||||
p = of_get_property(np,
|
||||
"battery,input_current_limit", &len);
|
||||
if (!p)
|
||||
return 1;
|
||||
|
||||
len = len / sizeof(u32);
|
||||
fuelgauge->pdata->charging_current =
|
||||
kzalloc(sizeof(struct sec_charging_current)
|
||||
* len, GFP_KERNEL);
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
ret = of_property_read_u32_index(np,
|
||||
"battery,input_current_limit", i,
|
||||
&fuelgauge->pdata->charging_current[i].
|
||||
input_current_limit);
|
||||
ret = of_property_read_u32_index(np,
|
||||
"battery,fast_charging_current", i,
|
||||
&fuelgauge->pdata->charging_current[i].
|
||||
fast_charging_current);
|
||||
ret = of_property_read_u32_index(np,
|
||||
"battery,full_check_current_1st", i,
|
||||
&fuelgauge->pdata->charging_current[i].
|
||||
full_check_current_1st);
|
||||
ret = of_property_read_u32_index(np,
|
||||
"battery,full_check_current_2nd", i,
|
||||
&fuelgauge->pdata->charging_current[i].
|
||||
full_check_current_2nd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct of_device_id s2mu003_fuelgauge_match_table[] = {
|
||||
{ .compatible = "samsung,s2mu003-fuelgauge",},
|
||||
{},
|
||||
};
|
||||
#else
|
||||
static int s2mu003_fuelgauge_parse_dt(struct s2mu003_fuelgauge_data *fuelgauge)
|
||||
{
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
#define s2mu003_fuelgauge_match_table NULL
|
||||
#endif /* CONFIG_OF */
|
||||
|
||||
static int s2mu003_fuelgauge_probe(struct i2c_client *client,
|
||||
const struct i2c_device_id *id)
|
||||
{
|
||||
struct s2mu003_fuelgauge_data *fuelgauge;
|
||||
union power_supply_propval raw_soc_val;
|
||||
int ret = 0;
|
||||
|
||||
pr_info("%s: S2MU003 Fuelgauge Driver Loading\n", __func__);
|
||||
|
||||
fuelgauge = kzalloc(sizeof(*fuelgauge), GFP_KERNEL);
|
||||
if (!fuelgauge)
|
||||
return -ENOMEM;
|
||||
|
||||
mutex_init(&fuelgauge->fg_lock);
|
||||
|
||||
fuelgauge->i2c = client;
|
||||
|
||||
#ifdef CONFIG_OF
|
||||
fuelgauge->pdata = devm_kzalloc(&client->dev,
|
||||
sizeof(*(fuelgauge->pdata)), GFP_KERNEL);
|
||||
if (!fuelgauge->pdata) {
|
||||
dev_err(&client->dev, "Failed to allocate memory\n");
|
||||
ret = -ENOMEM;
|
||||
goto err_parse_dt_nomem;
|
||||
}
|
||||
ret = s2mu003_fuelgauge_parse_dt(fuelgauge);
|
||||
if (ret < 0)
|
||||
goto err_parse_dt;
|
||||
#else
|
||||
fuelgauge->pdata = mfd_pdata->charger_platform_data;
|
||||
#endif
|
||||
|
||||
i2c_set_clientdata(client, fuelgauge);
|
||||
|
||||
if (fuelgauge->pdata->fuelgauge_name == NULL)
|
||||
fuelgauge->pdata->fuelgauge_name = "sec-fuelgauge";
|
||||
|
||||
fuelgauge->psy_fg.name = fuelgauge->pdata->fuelgauge_name;
|
||||
fuelgauge->psy_fg.type = POWER_SUPPLY_TYPE_UNKNOWN;
|
||||
fuelgauge->psy_fg.get_property = s2mu003_fg_get_property;
|
||||
fuelgauge->psy_fg.set_property = s2mu003_fg_set_property;
|
||||
fuelgauge->psy_fg.properties = s2mu003_fuelgauge_props;
|
||||
fuelgauge->psy_fg.num_properties =
|
||||
ARRAY_SIZE(s2mu003_fuelgauge_props);
|
||||
|
||||
fuelgauge->capacity_max = fuelgauge->pdata->capacity_max;
|
||||
raw_soc_val.intval = s2mu003_get_rawsoc(fuelgauge) / 10;
|
||||
|
||||
if (raw_soc_val.intval > fuelgauge->capacity_max)
|
||||
s2mu003_fg_calculate_dynamic_scale(fuelgauge, 100);
|
||||
|
||||
ret = s2mu003_init_regs(fuelgauge);
|
||||
if (ret < 0) {
|
||||
pr_err("%s: Failed to Initialize Fuelgauge\n", __func__);
|
||||
/* goto err_data_free; */
|
||||
}
|
||||
|
||||
ret = power_supply_register(&client->dev, &fuelgauge->psy_fg);
|
||||
if (ret) {
|
||||
pr_err("%s: Failed to Register psy_fg\n", __func__);
|
||||
goto err_data_free;
|
||||
}
|
||||
|
||||
fuelgauge->initial_update_of_soc = true;
|
||||
|
||||
pr_info("%s: S2MU003 Fuelgauge Driver Loaded\n", __func__);
|
||||
return 0;
|
||||
|
||||
err_data_free:
|
||||
if (client->dev.of_node)
|
||||
kfree(fuelgauge->pdata);
|
||||
|
||||
err_parse_dt:
|
||||
err_parse_dt_nomem:
|
||||
mutex_destroy(&fuelgauge->fg_lock);
|
||||
kfree(fuelgauge);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void s2mu003_fuelgauge_shutdown(struct i2c_client *i2c)
|
||||
{
|
||||
}
|
||||
|
||||
static int s2mu003_fuelgauge_remove(struct i2c_client *i2c)
|
||||
{
|
||||
struct s2mu003_fuelgauge_data *fuelgauge =
|
||||
i2c_get_clientdata(i2c);
|
||||
|
||||
if (fuelgauge->pdata->fuel_alert_soc >= 0)
|
||||
wake_lock_destroy(&fuelgauge->fuel_alert_wake_lock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined CONFIG_PM
|
||||
static int s2mu003_fuelgauge_suspend(struct device *dev)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int s2mu003_fuelgauge_resume(struct device *dev)
|
||||
{
|
||||
struct i2c_client *i2c =
|
||||
container_of(dev, struct i2c_client, dev);
|
||||
struct s2mu003_fuelgauge_data *fuelgauge =
|
||||
i2c_get_clientdata(i2c);
|
||||
fuelgauge->initial_update_of_soc = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
#define s2mu003_fuelgauge_suspend NULL
|
||||
#define s2mu003_fuelgauge_resume NULL
|
||||
#endif
|
||||
|
||||
static const struct i2c_device_id s2mu003_fuelgauge_id[] = {
|
||||
{"s2mu003-fuelgauge", 0},
|
||||
{}
|
||||
};
|
||||
MODULE_DEVICE_TABLE(i2c, s2mu003_fuelgauge_id);
|
||||
|
||||
static SIMPLE_DEV_PM_OPS(s2mu003_fuelgauge_pm_ops, s2mu003_fuelgauge_suspend,
|
||||
s2mu003_fuelgauge_resume);
|
||||
|
||||
static struct i2c_driver s2mu003_fuelgauge_driver = {
|
||||
.driver = {
|
||||
.name = "s2mu003-fuelgauge",
|
||||
.owner = THIS_MODULE,
|
||||
.pm = &s2mu003_fuelgauge_pm_ops,
|
||||
.of_match_table = s2mu003_fuelgauge_match_table,
|
||||
},
|
||||
.probe = s2mu003_fuelgauge_probe,
|
||||
.remove = s2mu003_fuelgauge_remove,
|
||||
.shutdown = s2mu003_fuelgauge_shutdown,
|
||||
.id_table = s2mu003_fuelgauge_id,
|
||||
};
|
||||
|
||||
static int __init s2mu003_fuelgauge_init(void)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
ret = i2c_add_driver(&s2mu003_fuelgauge_driver);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void __exit s2mu003_fuelgauge_exit(void)
|
||||
{
|
||||
i2c_del_driver(&s2mu003_fuelgauge_driver);
|
||||
}
|
||||
module_init(s2mu003_fuelgauge_init);
|
||||
module_exit(s2mu003_fuelgauge_exit);
|
||||
|
||||
MODULE_DESCRIPTION("Samsung S2MU003 Fuel Gauge Driver");
|
||||
MODULE_AUTHOR("Samsung Electronics");
|
||||
MODULE_LICENSE("GPL");
|
Loading…
Add table
Add a link
Reference in a new issue