blob: 78ccdede211308a9c56f3be2673d09f9ead17ae3 [file] [log] [blame]
/*
* max77854_charger.c
* Samsung MAX77854 Charger Driver
*
* Copyright (C) 2012 Samsung Electronics
*
*
* 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.
*/
#define DEBUG
#include <linux/mfd/max77854-private.h>
#if defined(CONFIG_BATTERY_NOTIFIER)
#include <linux/battery/battery_notifier.h>
#endif
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/power_supply.h>
#include <linux/battery/sec_charger.h>
#include <linux/mfd/max77854.h>
#include <linux/of_gpio.h>
#ifdef CONFIG_USB_HOST_NOTIFY
#include <linux/usb_notify.h>
#endif
#define ENABLE 1
#define DISABLE 0
#if defined(CONFIG_SEC_FACTORY)
#define WC_CURRENT_WORK_STEP 250
#else
#define WC_CURRENT_WORK_STEP 1000
#endif
static enum power_supply_property max77854_charger_props[] = {
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_CHARGE_TYPE,
POWER_SUPPLY_PROP_HEALTH,
POWER_SUPPLY_PROP_ONLINE,
POWER_SUPPLY_PROP_CURRENT_MAX,
POWER_SUPPLY_PROP_CURRENT_AVG,
POWER_SUPPLY_PROP_CURRENT_NOW,
POWER_SUPPLY_PROP_CHARGE_OTG_CONTROL,
POWER_SUPPLY_PROP_USB_HC,
#if defined(CONFIG_BATTERY_SWELLING)
POWER_SUPPLY_PROP_VOLTAGE_MAX,
#endif
#if defined(CONFIG_AFC_CHARGER_MODE)
POWER_SUPPLY_PROP_AFC_CHARGER_MODE,
#endif
POWER_SUPPLY_PROP_CHARGE_NOW,
};
static enum power_supply_property max77854_otg_props[] = {
POWER_SUPPLY_PROP_ONLINE,
};
static struct device_attribute max77854_charger_attrs[] = {
SEC_CHARGER_ATTR(chip_id),
SEC_CHARGER_ATTR(data),
};
static void max77854_charger_initialize(struct max77854_charger_data *charger);
static int max77854_get_vbus_state(struct max77854_charger_data *charger);
static int max77854_get_charger_state(struct max77854_charger_data *charger);
static void max77854_set_charger_state(struct max77854_charger_data *charger,
int enable);
static void max77854_enable_aicl_irq(struct max77854_charger_data *charger);
static bool max77854_charger_unlock(struct max77854_charger_data *charger)
{
u8 reg_data;
u8 chgprot;
int retry_cnt = 0;
bool need_init = false;
do {
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_CNFG_06, &reg_data);
chgprot = ((reg_data & 0x0C) >> 2);
if (chgprot != 0x03) {
pr_err("%s: unlock err, chgprot(0x%x), retry(%d)\n",
__func__, chgprot, retry_cnt);
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_06,
(0x03 << 2), (0x03 << 2));
need_init = true;
msleep(20);
} else {
pr_debug("%s: unlock success, chgprot(0x%x)\n",
__func__, chgprot);
break;
}
} while ((chgprot != 0x03) && (++retry_cnt < 10));
return need_init;
}
static void check_charger_unlock_state(struct max77854_charger_data *charger)
{
bool need_reg_init;
pr_debug("%s\n", __func__);
need_reg_init = max77854_charger_unlock(charger);
if (need_reg_init) {
pr_err("%s: charger locked state, reg init\n", __func__);
max77854_charger_initialize(charger);
}
}
static void max77854_test_read(struct max77854_charger_data *charger)
{
u8 data = 0;
u32 addr = 0;
char str[1024]={0,};
for (addr = 0xB1; addr <= 0xC3; addr++) {
max77854_read_reg(charger->i2c, addr, &data);
sprintf(str + strlen(str), "[0x%02x]0x%02x, ", addr, data);
}
pr_info("MAX77854 : %s\n", str);
}
static int max77854_get_vbus_state(struct max77854_charger_data *charger)
{
u8 reg_data;
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_DETAILS_00, &reg_data);
if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS_ETX ||
charger->cable_type == POWER_SUPPLY_TYPE_PMA_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK_TA ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_STAND ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND)
reg_data = ((reg_data & MAX77854_WCIN_DTLS) >>
MAX77854_WCIN_DTLS_SHIFT);
else
reg_data = ((reg_data & MAX77854_CHGIN_DTLS) >>
MAX77854_CHGIN_DTLS_SHIFT);
switch (reg_data) {
case 0x00:
pr_info("%s: VBUS is invalid. CHGIN < CHGIN_UVLO\n",
__func__);
break;
case 0x01:
pr_info("%s: VBUS is invalid. CHGIN < MBAT+CHGIN2SYS" \
"and CHGIN > CHGIN_UVLO\n", __func__);
break;
case 0x02:
pr_info("%s: VBUS is invalid. CHGIN > CHGIN_OVLO",
__func__);
break;
case 0x03:
pr_info("%s: VBUS is valid. CHGIN < CHGIN_OVLO", __func__);
break;
default:
break;
}
return reg_data;
}
static int max77854_get_charger_state(struct max77854_charger_data *charger)
{
int status = POWER_SUPPLY_STATUS_UNKNOWN;
u8 reg_data;
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_DETAILS_01, &reg_data);
pr_info("%s : charger status (0x%02x)\n", __func__, reg_data);
reg_data &= 0x0f;
switch (reg_data)
{
case 0x00:
case 0x01:
case 0x02:
status = POWER_SUPPLY_STATUS_CHARGING;
break;
case 0x03:
case 0x04:
status = POWER_SUPPLY_STATUS_FULL;
break;
case 0x05:
case 0x06:
case 0x07:
status = POWER_SUPPLY_STATUS_NOT_CHARGING;
break;
case 0x08:
case 0xA:
case 0xB:
status = POWER_SUPPLY_STATUS_DISCHARGING;
break;
default:
status = POWER_SUPPLY_STATUS_UNKNOWN;
break;
}
return (int)status;
}
static bool max77854_is_constant_current(struct max77854_charger_data *charger)
{
u8 reg_data;
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_DETAILS_01, &reg_data);
pr_info("%s : charger status (0x%02x)\n", __func__, reg_data);
reg_data &= 0x0f;
if (reg_data == 0x01)
return true;
return false;
}
static void max77854_set_float_voltage(struct max77854_charger_data *charger, int float_voltage)
{
u8 reg_data = 0;
reg_data = float_voltage <= 40500 ? 0x0 :
float_voltage >= 45000 ? 0x24 :
(float_voltage - 40500) / 125;
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_04,
(reg_data << CHG_CNFG_04_CHG_CV_PRM_SHIFT),
CHG_CNFG_04_CHG_CV_PRM_MASK);
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_CNFG_04, &reg_data);
pr_info("%s: battery cv voltage 0x%x\n", __func__, reg_data);
}
static u8 max77854_get_float_voltage(struct max77854_charger_data *charger)
{
u8 reg_data = 0;
int float_voltage;
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_CNFG_04, &reg_data);
reg_data &= 0x3F;
float_voltage = reg_data * 125 + 40500;
pr_debug("%s: battery cv reg : 0x%x, float voltage val : %d\n",
__func__, reg_data, float_voltage);
return reg_data;
}
static int max77854_get_charging_health(struct max77854_charger_data *charger)
{
int state;
int vbus_state;
int retry_cnt;
u8 chg_dtls, reg_data;
u8 chg_cnfg_00;
/* watchdog kick */
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_06,
MAX77854_WDTCLR, MAX77854_WDTCLR);
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_DETAILS_01, &reg_data);
reg_data = ((reg_data & MAX77854_BAT_DTLS) >> MAX77854_BAT_DTLS_SHIFT);
pr_info("%s: reg_data(0x%x)\n", __func__, reg_data);
switch (reg_data) {
case 0x00:
pr_info("%s: No battery and the charger is suspended\n",
__func__);
state = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
break;
case 0x01:
pr_info("%s: battery is okay "
"but its voltage is low(~VPQLB)\n", __func__);
state = POWER_SUPPLY_HEALTH_GOOD;
break;
case 0x02:
pr_info("%s: battery dead\n", __func__);
state = POWER_SUPPLY_HEALTH_DEAD;
break;
case 0x03:
state = POWER_SUPPLY_HEALTH_GOOD;
break;
case 0x04:
pr_info("%s: battery is okay" \
"but its voltage is low\n", __func__);
state = POWER_SUPPLY_HEALTH_GOOD;
break;
case 0x05:
pr_info("%s: battery ovp\n", __func__);
state = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
break;
default:
pr_info("%s: battery unknown : 0x%x\n", __func__, reg_data);
state = POWER_SUPPLY_HEALTH_UNKNOWN;
break;
}
if (state == POWER_SUPPLY_HEALTH_GOOD) {
union power_supply_propval value;
psy_do_property("battery", get,
POWER_SUPPLY_PROP_HEALTH, value);
/* VBUS OVP state return battery OVP state */
vbus_state = max77854_get_vbus_state(charger);
/* read CHG_DTLS and detecting battery terminal error */
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_DETAILS_01, &chg_dtls);
chg_dtls = ((chg_dtls & MAX77854_CHG_DTLS) >>
MAX77854_CHG_DTLS_SHIFT);
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_00, &chg_cnfg_00);
/* print the log at the abnormal case */
if((charger->is_charging == 1) && (chg_dtls & 0x08)) {
max77854_test_read(charger);
max77854_set_charger_state(charger, DISABLE);
max77854_set_float_voltage(charger, charger->float_voltage);
max77854_set_charger_state(charger, ENABLE);
}
pr_info("%s: vbus_state : 0x%x, chg_dtls : 0x%x\n", __func__, vbus_state, chg_dtls);
/* OVP is higher priority */
if (vbus_state == 0x02) { /* CHGIN_OVLO */
pr_info("%s: vbus ovp\n", __func__);
state = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_PMA_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK_TA ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_STAND ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) {
retry_cnt = 0;
do {
msleep(50);
vbus_state = max77854_get_vbus_state(charger);
} while((retry_cnt++ < 2) && (vbus_state == 0x02));
if (vbus_state == 0x02) {
state = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
pr_info("%s: wpc and over-voltage\n", __func__);
} else
state = POWER_SUPPLY_HEALTH_GOOD;
}
} else if (((vbus_state == 0x0) || (vbus_state == 0x01)) && (chg_dtls & 0x08) && \
(chg_cnfg_00 & MAX77854_MODE_BUCK) && \
(chg_cnfg_00 & MAX77854_MODE_CHGR) && \
(charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_HV_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_PMA_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_PACK &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_PACK_TA &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_STAND &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_HV_STAND)) {
pr_info("%s: vbus is under\n", __func__);
state = POWER_SUPPLY_HEALTH_UNDERVOLTAGE;
} else if ((value.intval == POWER_SUPPLY_HEALTH_UNDERVOLTAGE) && \
((vbus_state == 0x0) || (vbus_state == 0x01)) && \
(charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_HV_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_PMA_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_PACK &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_PACK_TA &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_STAND &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_HV_STAND)) {
pr_info("%s: keep under-voltage\n", __func__);
state = POWER_SUPPLY_HEALTH_UNDERVOLTAGE;
}
}
return (int)state;
}
static int max77854_get_charge_current(struct max77854_charger_data *charger)
{
u8 reg_data;
int get_current = 0;
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_02, &reg_data);
reg_data &= MAX77854_CHG_CC;
get_current = reg_data <= 0x2 ? 100 : reg_data * 50;
return get_current;
}
static int max77854_get_input_current_type(struct max77854_charger_data *charger,
int cable_type)
{
u8 reg_data;
int get_current = 0;
if (cable_type == POWER_SUPPLY_TYPE_WIRELESS) {
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_10, &reg_data);
/* AND operation for removing the formal 2bit */
reg_data &= 0x3F;
if (reg_data <= 0x3)
get_current = 90;
else
get_current = reg_data * 30;
} else {
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_09, &reg_data);
/* AND operation for removing the formal 1bit */
reg_data &= 0x7F;
if (reg_data <= 0x3) {
get_current = 100;
} else if (reg_data >= 0x78) {
get_current = 4000;
} else {
int quotient, remainder;
quotient = reg_data / 3;
remainder = reg_data % 3;
if (remainder == 0)
get_current = quotient * 100;
else if (remainder == 1)
get_current = quotient * 100 + 33;
else
get_current = quotient * 100 + 67;
}
}
return get_current;
}
static int max77854_get_input_current(struct max77854_charger_data *charger)
{
if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_PMA_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK_TA ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_STAND ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND)
return max77854_get_input_current_type(charger, POWER_SUPPLY_TYPE_WIRELESS);
else
return max77854_get_input_current_type(charger, POWER_SUPPLY_TYPE_MAINS);
}
static void reduce_input_current(struct max77854_charger_data *charger, int cur)
{
u8 set_reg, set_value;
unsigned int input_curr_limit_step = 0;
if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_PMA_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK_TA ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_STAND ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) {
set_reg = MAX77854_CHG_REG_CNFG_10;
input_curr_limit_step = 30;
} else {
set_reg = MAX77854_CHG_REG_CNFG_09;
input_curr_limit_step = 33;
}
if (!max77854_read_reg(charger->i2c, set_reg, &set_value)) {
if (set_value <= (MINIMUM_INPUT_CURRENT / input_curr_limit_step) ||
set_value <= (cur / input_curr_limit_step))
return;
set_value -= (cur / input_curr_limit_step);
set_value = (set_value < (MINIMUM_INPUT_CURRENT / input_curr_limit_step)) ?
(MINIMUM_INPUT_CURRENT / input_curr_limit_step) : set_value;
max77854_write_reg(charger->i2c, set_reg, set_value);
pr_info("%s: set current: reg:(0x%x), val:(0x%x)\n",
__func__, set_reg, set_value);
charger->input_current = max77854_get_input_current(charger);
}
}
static bool max77854_check_battery(struct max77854_charger_data *charger)
{
u8 reg_data;
u8 reg_data2;
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_INT_OK, &reg_data);
pr_info("%s : CHG_INT_OK(0x%x)\n", __func__, reg_data);
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_DETAILS_00, &reg_data2);
pr_info("%s : CHG_DETAILS00(0x%x)\n", __func__, reg_data2);
if ((reg_data & MAX77854_BATP_OK) ||
!(reg_data2 & MAX77854_BATP_DTLS))
return true;
else
return false;
}
static void max77854_set_buck(struct max77854_charger_data *charger,
int enable)
{
u8 reg_data;
if (enable) {
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
CHG_CNFG_00_BUCK_MASK, CHG_CNFG_00_BUCK_MASK);
} else {
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
0, CHG_CNFG_00_BUCK_MASK);
}
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00, &reg_data);
pr_info("%s : CHG_CNFG_00(0x%02x)\n", __func__, reg_data);
}
static void max77854_change_charge_path(struct max77854_charger_data *charger,
int path)
{
u8 cnfg12;
if (path == POWER_SUPPLY_TYPE_WIRELESS ||
path == POWER_SUPPLY_TYPE_HV_WIRELESS ||
path == POWER_SUPPLY_TYPE_PMA_WIRELESS ||
path == POWER_SUPPLY_TYPE_WIRELESS_PACK ||
path == POWER_SUPPLY_TYPE_WIRELESS_PACK_TA ||
path == POWER_SUPPLY_TYPE_WIRELESS_STAND ||
path == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) {
cnfg12 = (0 << CHG_CNFG_12_CHGINSEL_SHIFT);
} else {
cnfg12 = (1 << CHG_CNFG_12_CHGINSEL_SHIFT);
}
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_12,
cnfg12, CHG_CNFG_12_CHGINSEL_MASK);
pr_info("%s : CHG_CNFG_12(0x%02x)\n", __func__, cnfg12);
}
static void max77854_set_input_current(struct max77854_charger_data *charger,
int input_current)
{
int quotient, remainder;
u8 set_reg, reg_data;
mutex_lock(&charger->charger_mutex);
if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_PMA_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK_TA ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_STAND ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) {
set_reg = MAX77854_CHG_REG_CNFG_10;
max77854_read_reg(charger->i2c,
set_reg, &reg_data);
reg_data &= ~MAX77854_CHG_WCIN_LIM;
} else {
set_reg = MAX77854_CHG_REG_CNFG_09;
max77854_read_reg(charger->i2c,
set_reg, &reg_data);
reg_data &= ~MAX77854_CHG_CHGIN_LIM;
}
if (!input_current) {
max77854_write_reg(charger->i2c,
set_reg, reg_data);
} else if(charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_PMA_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK_TA ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_STAND ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) {
input_current = (input_current > 1890) ? 1890 : input_current;
quotient = input_current / 30;
reg_data |= quotient;
max77854_write_reg(charger->i2c, set_reg, reg_data);
} else {
input_current = (input_current > 4000) ? 4000 : input_current;
quotient = input_current / 100;
remainder = input_current % 100;
if (remainder >= 67)
reg_data |= (quotient * 3) + 2;
else if (remainder >= 33)
reg_data |= (quotient * 3) + 1;
else if (remainder < 33)
reg_data |= quotient * 3;
max77854_write_reg(charger->i2c,
set_reg, reg_data);
}
mutex_unlock(&charger->charger_mutex);
pr_info("[%s] REG(0x%02x) DATA(0x%02x), CURRENT(%d)\n",
__func__, set_reg, reg_data, input_current);
}
static void max77854_set_charge_current(struct max77854_charger_data *charger,
int fast_charging_current)
{
int curr_step = 50;
u8 reg_data;
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_02, &reg_data);
reg_data &= ~MAX77854_CHG_CC;
if (!fast_charging_current) {
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_02, reg_data);
} else {
fast_charging_current = (fast_charging_current > 3150) ? 3150 : fast_charging_current;
reg_data |= (fast_charging_current / curr_step);
max77854_write_reg(charger->i2c,MAX77854_CHG_REG_CNFG_02, reg_data);
}
pr_info("[%s] REG(0x%02x) DATA(0x%02x), CURRENT(%d)\n",
__func__, MAX77854_CHG_REG_CNFG_02,
reg_data, fast_charging_current);
}
static void max77854_set_wireless_input_current(struct max77854_charger_data *charger,
int input_current)
{
union power_supply_propval value;
/* Wcurr-A) In cases of wireless input current change,
configure the Vrect adj room to 270mV for safe wireless charging. */
wake_lock(&charger->wc_current_wake_lock);
value.intval = WIRELESS_VRECT_ADJ_ROOM_1; /* 270mV */
psy_do_property(charger->pdata->wireless_charger_name, set,
POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION, value);
msleep(500); /* delay 0.5sec */
charger->wc_pre_current = max77854_get_input_current(charger);
charger->wc_current = input_current;
if (charger->wc_current > charger->wc_pre_current) {
max77854_set_charge_current(charger, charger->charging_current);
}
queue_delayed_work_on(0, charger->wqueue, &charger->wc_current_work, 0);
}
static void max77854_set_topoff_current(struct max77854_charger_data *charger,
int termination_current)
{
int curr_base = 150, curr_step = 50;
u8 reg_data;
if (termination_current < curr_base)
termination_current = curr_base;
else if (termination_current > 500)
termination_current = 500;
reg_data = (termination_current - curr_base) / curr_step;
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_03,
reg_data, MAX77854_CHG_TO_ITH);
pr_info("%s: reg_data(0x%02x), topoff(%dmA)\n",
__func__, reg_data, termination_current);
}
static void max77854_set_charger_state(struct max77854_charger_data *charger,
int enable)
{
u8 cnfg_00, cnfg_12;
if (enable) {
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
CHG_CNFG_00_CHG_MASK, CHG_CNFG_00_CHG_MASK);
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_12,
0x1, CHG_CNFG_12_DISSKIP);
} else {
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
0, CHG_CNFG_00_CHG_MASK);
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_12,
0x0, CHG_CNFG_12_DISSKIP);
}
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00, &cnfg_00);
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_CNFG_12, &cnfg_12);
pr_info("%s : CHG_CNFG_00(0x%02x), CHG_CNFG_12(0x%02x)\n", __func__, cnfg_00, cnfg_12);
}
static void max77854_check_slow_charging(struct max77854_charger_data *charger,
int input_current)
{
/* under 400mA considered as slow charging concept for VZW */
if (input_current <= SLOW_CHARGING_CURRENT_STANDARD &&
charger->cable_type != POWER_SUPPLY_TYPE_BATTERY) {
union power_supply_propval value;
charger->aicl_on = true;
pr_info("%s: slow charging on : input current(%dmA), cable type(%d)\n",
__func__, input_current, charger->cable_type);
psy_do_property("battery", set,
POWER_SUPPLY_PROP_CHARGE_TYPE, value);
}
else
charger->aicl_on = false;
}
static void max77854_charger_initialize(struct max77854_charger_data *charger)
{
u8 reg_data;
int jig_gpio;
pr_info("%s\n", __func__);
/* unmasked: CHGIN_I, WCIN_I, BATP_I, BYP_I */
/*max77854_write_reg(charger->i2c, MAX77854_CHG_REG_INT_MASK, 0x9a);*/
/* unlock charger setting protect */
reg_data = (0x03 << 2);
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_06, reg_data, reg_data);
/*
* fast charge timer disable
* restart threshold disable
* pre-qual charge disable
*/
reg_data = (0x03 << 4);
reg_data |= 0x08;
max77854_write_reg(charger->i2c, MAX77854_CHG_REG_CNFG_01, reg_data);
/*
* charge current 466mA(default)
* otg current limit 900mA
*/
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_02,
(0x01 << 6), 0xC0);
/*
* top off current 150mA
* top off timer 70min
*/
reg_data = 0x38;
max77854_write_reg(charger->i2c, MAX77854_CHG_REG_CNFG_03, reg_data);
/*
* cv voltage 4.2V or 4.35V
* MINVSYS 3.6V(default)
*/
max77854_set_float_voltage(charger, charger->pdata->chg_float_voltage);
/*
* VCHGIN & WCHGIN regulation threshold set 4.3v, uvlo 4.5V
* Auto skip mode
*/
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_12,
0x0, CHG_CNFG_12_VCHGIN_REG_MASK|CHG_CNFG_12_WCIN_REG_MASK);
/* Boost mode possible in FACTORY MODE */
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_07,
MAX77854_CHG_FMBST, CHG_CNFG_07_REG_FMBST_MASK);
jig_gpio = gpio_get_value(charger->jig_gpio);
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_07,
(jig_gpio << CHG_CNFG_07_REG_FGSRC_SHIFT),
CHG_CNFG_07_REG_FGSRC_MASK);
/* Watchdog Enable */
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
MAX77854_WDTEN, MAX77854_WDTEN);
//max77854_test_read(charger);
}
static int max77854_chg_create_attrs(struct device *dev)
{
int i, rc;
for (i = 0; i < ARRAY_SIZE(max77854_charger_attrs); i++) {
rc = device_create_file(dev, &max77854_charger_attrs[i]);
if (rc)
goto create_attrs_failed;
}
return rc;
create_attrs_failed:
dev_err(dev, "%s: failed (%d)\n", __func__, rc);
while (i--)
device_remove_file(dev, &max77854_charger_attrs[i]);
return rc;
}
ssize_t sec_chg_show_attrs(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct power_supply *psy = dev_get_drvdata(dev);
struct max77854_charger_data *charger =
container_of(psy, struct max77854_charger_data, psy_chg);
const ptrdiff_t offset = attr - max77854_charger_attrs;
int i = 0;
u8 addr, data;
switch(offset) {
case CHIP_ID:
i += scnprintf(buf + i, PAGE_SIZE - i, "%s\n",
(charger->pmic_ver >= 0x1 && charger->pmic_ver <= 0x03) ?
"MAX77854" : "Unknown");
break;
case DATA:
for (addr = 0xB1; addr <= 0xC3; addr++) {
max77854_read_reg(charger->i2c, addr, &data);
i += scnprintf(buf + i, PAGE_SIZE - i, "0x%02x : 0x%02x\n", addr, data);
}
break;
default:
return -EINVAL;
}
return i;
}
ssize_t sec_chg_store_attrs(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct power_supply *psy = dev_get_drvdata(dev);
struct max77854_charger_data *charger =
container_of(psy, struct max77854_charger_data, psy_chg);
const ptrdiff_t offset = attr - max77854_charger_attrs;
int ret = 0;
int x,y;
switch(offset) {
case CHIP_ID:
ret = count;
break;
case DATA:
if (sscanf(buf, "0x%x 0x%x", &x, &y) == 2) {
if (x >= 0xB1 && x <= 0xC3) {
u8 addr = x;
u8 data = y;
if (max77854_write_reg(charger->i2c, addr, data) < 0) {
dev_info(charger->dev,
"%s: addr: 0x%x write fail\n", __func__, addr);
}
} else {
dev_info(charger->dev,
"%s: addr: 0x%x is wrong\n", __func__, x);
}
}
ret = count;
break;
default:
ret = -EINVAL;
}
return ret;
}
static int max77854_chg_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct max77854_charger_data *charger =
container_of(psy, struct max77854_charger_data, psy_chg);
u8 reg_data;
switch (psp) {
case POWER_SUPPLY_PROP_ONLINE:
val->intval = POWER_SUPPLY_TYPE_BATTERY;
if (max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_INT_OK, &reg_data) == 0) {
if (reg_data & MAX77854_WCIN_OK) {
val->intval = POWER_SUPPLY_TYPE_WIRELESS;
charger->wc_w_state = 1;
} else if (reg_data & MAX77854_CHGIN_OK) {
val->intval = POWER_SUPPLY_TYPE_MAINS;
}
}
break;
case POWER_SUPPLY_PROP_PRESENT:
val->intval = max77854_check_battery(charger);
break;
case POWER_SUPPLY_PROP_STATUS:
val->intval = max77854_get_charger_state(charger);
break;
case POWER_SUPPLY_PROP_CHARGE_TYPE:
if (!charger->is_charging)
val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
else if (charger->aicl_on)
{
val->intval = POWER_SUPPLY_CHARGE_TYPE_SLOW;
pr_info("%s: slow-charging mode\n", __func__);
}
else
val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
break;
case POWER_SUPPLY_PROP_HEALTH:
val->intval = max77854_get_charging_health(charger);
max77854_test_read(charger);
break;
case POWER_SUPPLY_PROP_CURRENT_MAX:
val->intval = charger->input_current;
break;
case POWER_SUPPLY_PROP_CURRENT_AVG:
if (val->intval == POWER_SUPPLY_TYPE_WIRELESS ||
val->intval == POWER_SUPPLY_TYPE_HV_WIRELESS ||
val->intval == POWER_SUPPLY_TYPE_PMA_WIRELESS ||
val->intval == POWER_SUPPLY_TYPE_WIRELESS_PACK ||
val->intval == POWER_SUPPLY_TYPE_WIRELESS_PACK_TA ||
val->intval == POWER_SUPPLY_TYPE_WIRELESS_STAND ||
val->intval == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND ||
val->intval == POWER_SUPPLY_TYPE_HV_WIRELESS_ETX)
val->intval = max77854_get_input_current_type(charger, POWER_SUPPLY_TYPE_WIRELESS);
else
val->intval = max77854_get_input_current_type(charger, POWER_SUPPLY_TYPE_MAINS);
break;
case POWER_SUPPLY_PROP_CURRENT_NOW:
val->intval = charger->charging_current;
break;
case POWER_SUPPLY_PROP_VOLTAGE_MAX:
val->intval = max77854_get_float_voltage(charger);
break;
#if defined(CONFIG_AFC_CHARGER_MODE)
case POWER_SUPPLY_PROP_AFC_CHARGER_MODE:
return -ENODATA;
#endif
case POWER_SUPPLY_PROP_CHARGE_NOW:
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_DETAILS_01, &reg_data);
reg_data &= 0x0F;
switch (reg_data) {
case 0x01:
val->strval = "CC Mode";
break;
case 0x02:
val->strval = "CV Mode";
break;
case 0x03:
val->strval = "EOC";
break;
case 0x04:
val->strval = "DONE";
break;
default:
val->strval = "NONE";
break;
}
break;
case POWER_SUPPLY_PROP_CHARGE_OTG_CONTROL:
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
&reg_data);
if ((reg_data & CHG_CNFG_00_OTG_CTRL) == CHG_CNFG_00_OTG_CTRL)
val->intval = 1;
else
val->intval = 0;
break;
case POWER_SUPPLY_PROP_CHARGE_UNO_CONTROL:
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
&reg_data);
if ((reg_data & CHG_CNFG_00_UNO_CTRL) == CHG_CNFG_00_BOOST_MASK)
val->intval = 1;
else
val->intval = 0;
break;
case POWER_SUPPLY_PROP_USB_HC:
return -ENODATA;
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
if (max77854_is_constant_current(charger)) {
val->intval = 0;
} else {
val->intval = 1;
}
break;
default:
return -EINVAL;
}
return 0;
}
#if defined(CONFIG_UPDATE_BATTERY_DATA)
static int max77854_charger_parse_dt(struct max77854_charger_data *charger);
#endif
static int max77854_chg_set_property(struct power_supply *psy,
enum power_supply_property psp,
const union power_supply_propval *val)
{
struct max77854_charger_data *charger =
container_of(psy, struct max77854_charger_data, psy_chg);
union power_supply_propval value;
u8 chg_cnfg_00 = 0;
static u8 chg_int_state;
int buck_state = ENABLE;
switch (psp) {
/* val->intval : type */
case POWER_SUPPLY_PROP_STATUS:
charger->status = val->intval;
break;
case POWER_SUPPLY_PROP_CHARGING_ENABLED:
charger->charge_mode = val->intval;
switch (charger->charge_mode) {
case SEC_BAT_CHG_MODE_BUCK_OFF:
buck_state = DISABLE;
case SEC_BAT_CHG_MODE_CHARGING_OFF:
charger->is_charging = false;
break;
case SEC_BAT_CHG_MODE_CHARGING:
charger->is_charging = true;
break;
}
max77854_set_buck(charger, buck_state);
max77854_set_charger_state(charger, charger->is_charging);
break;
case POWER_SUPPLY_PROP_ONLINE:
charger->cable_type = val->intval;
charger->aicl_on = false;
charger->input_current =
charger->pdata->charging_current[charger->cable_type].input_current_limit;
max77854_change_charge_path(charger, charger->cable_type);
if (charger->cable_type == POWER_SUPPLY_TYPE_BATTERY) {
charger->wc_pre_current = WC_CURRENT_START;
wake_unlock(&charger->wpc_wake_lock);
cancel_delayed_work(&charger->wpc_work);
/* Set CHGIN_LIMIT(500mA) and WCIN_LIMIT(480mA) to default */
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_09, 0x0F); // CHGIN_LIMIT
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_10, 0x10); // WCIN_LIMIT
max77854_update_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, 0, MAX77854_WCIN_IM);
}
break;
/* val->intval : input charging current */
case POWER_SUPPLY_PROP_CURRENT_MAX:
{
int input_current = val->intval;
if(charger->input_current < input_current) {
input_current = charger->input_current;
}
if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_PMA_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK_TA ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_STAND ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) {
max77854_set_wireless_input_current(charger, input_current);
} else {
max77854_set_input_current(charger, input_current);
}
}
break;
/* val->intval : charging current */
case POWER_SUPPLY_PROP_CURRENT_AVG:
charger->charging_current = val->intval;
max77854_set_charge_current(charger,
val->intval);
break;
/* val->intval : charging current */
case POWER_SUPPLY_PROP_CURRENT_NOW:
charger->charging_current = val->intval;
if (charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_HV_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_PMA_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_PACK &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_PACK_TA &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_STAND &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) {
max77854_set_charge_current(charger, charger->charging_current);
}
break;
case POWER_SUPPLY_PROP_CURRENT_FULL:
max77854_set_topoff_current(charger, val->intval);
break;
#if defined(CONFIG_AFC_CHARGER_MODE)
case POWER_SUPPLY_PROP_AFC_CHARGER_MODE:
max77854_hv_muic_charger_init();
break;
#endif
#if defined(CONFIG_BATTERY_SWELLING)
case POWER_SUPPLY_PROP_VOLTAGE_MAX:
charger->float_voltage = val->intval;
pr_info("%s: float voltage(%d)\n", __func__, val->intval);
max77854_set_float_voltage(charger, val->intval);
break;
#endif
case POWER_SUPPLY_PROP_USB_HC:
break;
case POWER_SUPPLY_PROP_ENERGY_NOW:
/* if jig attached, change the power source
from the VBATFG to the internal VSYS*/
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_07,
(val->intval << CHG_CNFG_07_REG_FGSRC_SHIFT),
CHG_CNFG_07_REG_FGSRC_MASK);
break;
case POWER_SUPPLY_PROP_CHARGE_OTG_CONTROL:
pr_info("%s: CHGIN-OTG %s\n", __func__, val->intval > 0 ? "on" : "off");
/* CHGIN-OTG */
if (val->intval) {
if (charger->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) {
pr_info("%s: OTG enabled on HV_WC, set 5V", __func__);
value.intval = WIRELESS_VOUT_5V;
psy_do_property(charger->pdata->wireless_charger_name, set,
POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION, value);
mdelay(200);
}
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_INT_MASK,
&chg_int_state);
/* disable charger interrupt: CHG_I, CHGIN_I */
/* enable charger interrupt: BYP_I */
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_INT_MASK,
MAX77854_CHG_IM | MAX77854_CHGIN_IM,
MAX77854_CHG_IM | MAX77854_CHGIN_IM | MAX77854_BYP_IM);
/* OTG on, boost on */
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
CHG_CNFG_00_OTG_CTRL, CHG_CNFG_00_OTG_CTRL);
/* Update CHG_CNFG_11 to 0x50(5V) */
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_11, 0x50);
} else {
/* OTG off(UNO on), boost off */
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
0, CHG_CNFG_00_OTG_CTRL);
/* Update CHG_CNFG_11 to 0x00(3V) */
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_11, 0x00);
mdelay(50);
/* enable charger interrupt */
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, chg_int_state);
if (charger->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) {
pr_info("%s: OTG disabled on HV_WC, set 9V", __func__);
value.intval = WIRELESS_VOUT_9V_OTG;
psy_do_property(charger->pdata->wireless_charger_name, set,
POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION, value);
}
}
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_INT_MASK,
&chg_int_state);
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
&chg_cnfg_00);
pr_info("%s: INT_MASK(0x%x), CHG_CNFG_00(0x%x)\n",
__func__, chg_int_state, chg_cnfg_00);
break;
case POWER_SUPPLY_PROP_CHARGE_UNO_CONTROL:
pr_info("%s: WCIN-UNO %s\n", __func__, val->intval > 0 ? "on" : "off");
/* WCIN-UNO */
if (val->intval) {
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_INT_MASK,
&chg_int_state);
/* disable charger interrupt: CHG_I, CHGIN_I */
/* enable charger interrupt: BYP_I */
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_INT_MASK,
MAX77854_CHG_IM | MAX77854_CHGIN_IM,
MAX77854_CHG_IM | MAX77854_CHGIN_IM | MAX77854_BYP_IM);
/* UNO on, boost on */
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
CHG_CNFG_00_BOOST_MASK, CHG_CNFG_00_UNO_CTRL);
/* Update CHG_CNFG_11 to 0x50(5V) */
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_11, 0x50);
} else {
/* boost off*/
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
0, CHG_CNFG_00_BOOST_MASK);
/* Update CHG_CNFG_11 to 0x00(3V) */
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_11, 0x00);
mdelay(50);
/* enable charger interrupt */
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, chg_int_state);
}
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_INT_MASK,
&chg_int_state);
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
&chg_cnfg_00);
pr_info("%s: INT_MASK(0x%x), CHG_CNFG_00(0x%x)\n",
__func__, chg_int_state, chg_cnfg_00);
break;
case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
max77854_enable_aicl_irq(charger);
queue_delayed_work(charger->wqueue, &charger->aicl_work, msecs_to_jiffies(50));
break;
#if defined(CONFIG_UPDATE_BATTERY_DATA)
case POWER_SUPPLY_PROP_POWER_DESIGN:
max77854_charger_parse_dt(charger);
break;
#endif
default:
return -EINVAL;
}
return 0;
}
static int max77854_otg_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct max77854_charger_data *charger =
container_of(psy, struct max77854_charger_data, psy_otg);
u8 chg_cnfg_00;
switch (psp) {
case POWER_SUPPLY_PROP_ONLINE:
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_00, &chg_cnfg_00);
val->intval = chg_cnfg_00 & CHG_CNFG_00_OTG_CTRL;
break;
default:
return -EINVAL;
}
return 0;
}
static int max77854_otg_set_property(struct power_supply *psy,
enum power_supply_property psp,
const union power_supply_propval *val)
{
struct max77854_charger_data *charger =
container_of(psy, struct max77854_charger_data, psy_otg);
union power_supply_propval value;
static u8 chg_int_state;
u8 chg_cnfg_00;
switch (psp) {
case POWER_SUPPLY_PROP_ONLINE:
pr_info("%s: CHGIN-OTG %s\n", __func__, val->intval > 0 ? "on" : "off");
/* CHGIN-OTG */
if (val->intval) {
if (charger->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) {
pr_info("%s: OTG enabled on HV_WC, set 5V", __func__);
value.intval = WIRELESS_VOUT_5V;
psy_do_property(charger->pdata->wireless_charger_name, set,
POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION, value);
mdelay(200);
}
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_INT_MASK,
&chg_int_state);
/* disable charger interrupt: CHG_I, CHGIN_I */
/* enable charger interrupt: BYP_I */
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_INT_MASK,
MAX77854_CHG_IM | MAX77854_CHGIN_IM,
MAX77854_CHG_IM | MAX77854_CHGIN_IM | MAX77854_BYP_IM);
/* OTG on, boost on */
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
CHG_CNFG_00_OTG_CTRL, CHG_CNFG_00_OTG_CTRL);
/* Update CHG_CNFG_11 to 0x50(5V) */
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_11, 0x50);
} else {
/* OTG off(UNO on), boost off */
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
0, CHG_CNFG_00_OTG_CTRL);
/* Update CHG_CNFG_11 to 0x00(3V) */
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_11, 0x00);
mdelay(50);
/* enable charger interrupt */
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, chg_int_state);
if (charger->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) {
pr_info("%s: OTG disabled on HV_WC, set 9V", __func__);
value.intval = WIRELESS_VOUT_9V_OTG;
psy_do_property(charger->pdata->wireless_charger_name, set,
POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION, value);
}
}
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_INT_MASK,
&chg_int_state);
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
&chg_cnfg_00);
pr_info("%s: INT_MASK(0x%x), CHG_CNFG_00(0x%x)\n",
__func__, chg_int_state, chg_cnfg_00);
break;
default:
return -EINVAL;
}
return 0;
}
static int max77854_debugfs_show(struct seq_file *s, void *data)
{
struct max77854_charger_data *charger = s->private;
u8 reg;
u8 reg_data;
seq_printf(s, "MAX77854 CHARGER IC :\n");
seq_printf(s, "===================\n");
for (reg = 0xB0; reg <= 0xC3; reg++) {
max77854_read_reg(charger->i2c, reg, &reg_data);
seq_printf(s, "0x%02x:\t0x%02x\n", reg, reg_data);
}
seq_printf(s, "\n");
return 0;
}
static int max77854_debugfs_open(struct inode *inode, struct file *file)
{
return single_open(file, max77854_debugfs_show, inode->i_private);
}
static const struct file_operations max77854_debugfs_fops = {
.open = max77854_debugfs_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static void max77854_chg_isr_work(struct work_struct *work)
{
struct max77854_charger_data *charger =
container_of(work, struct max77854_charger_data, isr_work.work);
union power_supply_propval val;
if (charger->pdata->full_check_type ==
SEC_BATTERY_FULLCHARGED_CHGINT) {
val.intval = max77854_get_charger_state(charger);
switch (val.intval) {
case POWER_SUPPLY_STATUS_DISCHARGING:
pr_err("%s: Interrupted but Discharging\n", __func__);
break;
case POWER_SUPPLY_STATUS_NOT_CHARGING:
pr_err("%s: Interrupted but NOT Charging\n", __func__);
break;
case POWER_SUPPLY_STATUS_FULL:
pr_info("%s: Interrupted by Full\n", __func__);
psy_do_property("battery", set,
POWER_SUPPLY_PROP_STATUS, val);
break;
case POWER_SUPPLY_STATUS_CHARGING:
pr_err("%s: Interrupted but Charging\n", __func__);
break;
case POWER_SUPPLY_STATUS_UNKNOWN:
default:
pr_err("%s: Invalid Charger Status\n", __func__);
break;
}
}
if (charger->pdata->ovp_uvlo_check_type ==
SEC_BATTERY_OVP_UVLO_CHGINT) {
val.intval = max77854_get_charging_health(charger);
switch (val.intval) {
case POWER_SUPPLY_HEALTH_OVERHEAT:
case POWER_SUPPLY_HEALTH_COLD:
pr_err("%s: Interrupted but Hot/Cold\n", __func__);
break;
case POWER_SUPPLY_HEALTH_DEAD:
pr_err("%s: Interrupted but Dead\n", __func__);
break;
case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
case POWER_SUPPLY_HEALTH_UNDERVOLTAGE:
pr_info("%s: Interrupted by OVP/UVLO\n", __func__);
psy_do_property("battery", set,
POWER_SUPPLY_PROP_HEALTH, val);
break;
case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
pr_err("%s: Interrupted but Unspec\n", __func__);
break;
case POWER_SUPPLY_HEALTH_GOOD:
pr_err("%s: Interrupted but Good\n", __func__);
break;
case POWER_SUPPLY_HEALTH_UNKNOWN:
default:
pr_err("%s: Invalid Charger Health\n", __func__);
break;
}
}
}
static irqreturn_t max77854_chg_irq_thread(int irq, void *irq_data)
{
struct max77854_charger_data *charger = irq_data;
pr_info("%s: Charger interrupt occured\n", __func__);
if ((charger->pdata->full_check_type ==
SEC_BATTERY_FULLCHARGED_CHGINT) ||
(charger->pdata->ovp_uvlo_check_type ==
SEC_BATTERY_OVP_UVLO_CHGINT))
schedule_delayed_work(&charger->isr_work, 0);
return IRQ_HANDLED;
}
/* If we have external wireless TRx, enable WCIN interrupt to detect Mis-align only */
static void wpc_detect_work(struct work_struct *work)
{
struct max77854_charger_data *charger = container_of(work,
struct max77854_charger_data,
wpc_work.work);
max77854_update_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, 0, MAX77854_WCIN_IM);
if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_PMA_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK_TA ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_STAND ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) {
u8 reg_data, wcin_state, wcin_dtls, wcin_cnt = 0;
do {
wcin_cnt++;
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_INT_OK,
&reg_data);
wcin_state = (reg_data & MAX77854_WCIN_OK) >> MAX77854_WCIN_OK_SHIFT;
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_DETAILS_00,
&reg_data);
wcin_dtls = (reg_data & MAX77854_WCIN_DTLS) >> MAX77854_WCIN_DTLS_SHIFT;
if (!wcin_state && !wcin_dtls && wcin_cnt >= 2) {
union power_supply_propval value;
pr_info("%s: invalid WCIN, Misalign occurs!\n", __func__);
value.intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
psy_do_property(charger->pdata->wireless_charger_name, set,
POWER_SUPPLY_PROP_STATUS, value);
}
mdelay(50);
} while (!wcin_state && !wcin_dtls && wcin_cnt < 2);
}
/* Do unmask again. (for frequent wcin irq problem) */
max77854_update_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, 0, MAX77854_WCIN_IM);
wake_unlock(&charger->wpc_wake_lock);
}
static irqreturn_t wpc_charger_irq(int irq, void *data)
{
struct max77854_charger_data *charger = data;
pr_info("%s: irq(%d)\n", __func__, irq);
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_INT_MASK,
MAX77854_WCIN_IM, MAX77854_WCIN_IM);
wake_lock(&charger->wpc_wake_lock);
queue_delayed_work(charger->wqueue, &charger->wpc_work, msecs_to_jiffies(10000));
return IRQ_HANDLED;
}
static irqreturn_t max77854_batp_irq(int irq, void *data)
{
struct max77854_charger_data *charger = data;
union power_supply_propval value;
u8 reg_data;
pr_info("%s : irq(%d)\n", __func__, irq);
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, &reg_data);
reg_data |= (1 << 2);
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, reg_data);
check_charger_unlock_state(charger);
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_INT_OK,
&reg_data);
if (!(reg_data & MAX77854_BATP_OK))
psy_do_property("battery", set, POWER_SUPPLY_PROP_PRESENT, value);
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, &reg_data);
reg_data &= ~(1 << 2);
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, reg_data);
return IRQ_HANDLED;
}
static irqreturn_t max77854_bypass_irq(int irq, void *data)
{
struct max77854_charger_data *charger = data;
u8 dtls_02;
u8 byp_dtls;
u8 vbus_state;
#ifdef CONFIG_USB_HOST_NOTIFY
struct otg_notify *o_notify;
o_notify = get_otg_notify();
#endif
pr_info("%s: irq(%d)\n", __func__, irq);
/* check and unlock */
check_charger_unlock_state(charger);
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_DETAILS_02,
&dtls_02);
byp_dtls = ((dtls_02 & MAX77854_BYP_DTLS) >>
MAX77854_BYP_DTLS_SHIFT);
pr_info("%s: BYP_DTLS(0x%02x)\n", __func__, byp_dtls);
vbus_state = max77854_get_vbus_state(charger);
if (byp_dtls & 0x1) {
pr_info("%s: bypass overcurrent limit\n", __func__);
#ifdef CONFIG_USB_HOST_NOTIFY
send_otg_notify(o_notify, NOTIFY_EVENT_OVERCURRENT, 0);
#endif
/* disable the register values just related to OTG and
keep the values about the charging */
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_00,
0, CHG_CNFG_00_OTG_CTRL);
}
return IRQ_HANDLED;
}
static void max77854_aicl_isr_work(struct work_struct *work)
{
struct max77854_charger_data *charger = container_of(work,
struct max77854_charger_data, aicl_work.work);
u8 aicl_state, aicl_cnt = 0;
pr_info("%s: \n", __func__);
wake_lock(&charger->aicl_wake_lock);
mutex_lock(&charger->charger_mutex);
max77854_update_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, MAX77854_AICL_IM, MAX77854_AICL_IM);
/* check and unlock */
check_charger_unlock_state(charger);
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_INT_OK, &aicl_state);
while (!(aicl_state & 0x80) && charger->cable_type != POWER_SUPPLY_TYPE_BATTERY) {
if (++aicl_cnt >= 2) {
reduce_input_current(charger, REDUCE_CURRENT_STEP);
aicl_cnt = 0;
}
mdelay(50);
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_INT_OK, &aicl_state);
if (max77854_get_input_current(charger) <= MINIMUM_INPUT_CURRENT)
break;
}
#if defined(CONFIG_BATTERY_NOTIFIER)
{
struct charger_notifier_struct charger_notifier;
/* notify the AICL to multi_charger */
charger_notifier.event = CHARGER_NOTIFY_EVENT_AICL;
charger_notifier.aicl_status.input_current = max77854_get_input_current(charger);
charger_notifier_call(&charger_notifier);
}
#endif
if (charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_HV_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_PMA_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_PACK &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_PACK_TA &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_STAND &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_HV_STAND)
max77854_check_slow_charging(charger, charger->input_current);
max77854_update_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, 0, MAX77854_AICL_IM);
mutex_unlock(&charger->charger_mutex);
wake_unlock(&charger->aicl_wake_lock);
}
static irqreturn_t max77854_aicl_irq(int irq, void *data)
{
struct max77854_charger_data *charger = data;
queue_delayed_work(charger->wqueue, &charger->aicl_work, msecs_to_jiffies(50));
pr_info("%s: irq(%d)\n", __func__, irq);
wake_unlock(&charger->wc_current_wake_lock);
cancel_delayed_work(&charger->wc_current_work);
return IRQ_HANDLED;
}
static void max77854_enable_aicl_irq(struct max77854_charger_data *charger)
{
int ret;
pr_info("%s \n", __func__);
ret = request_threaded_irq(charger->irq_aicl, NULL,
max77854_aicl_irq, 0, "aicl-irq", charger);
if (ret < 0) {
pr_err("%s: fail to request aicl IRQ: %d: %d\n",
__func__, charger->irq_aicl, ret);
} else {
max77854_update_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, 0, MAX77854_AICL_IM);
}
}
static void max77854_chgin_isr_work(struct work_struct *work)
{
struct max77854_charger_data *charger = container_of(work,
struct max77854_charger_data, chgin_work);
u8 chgin_dtls, chg_dtls, chg_cnfg_00, reg_data;
u8 prev_chgin_dtls = 0xff;
int battery_health;
union power_supply_propval value;
int stable_count = 0;
wake_lock(&charger->chgin_wake_lock);
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, &reg_data);
reg_data |= (1 << 6);
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, reg_data);
while (1) {
psy_do_property("battery", get,
POWER_SUPPLY_PROP_HEALTH, value);
battery_health = value.intval;
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_DETAILS_00,
&chgin_dtls);
chgin_dtls = ((chgin_dtls & MAX77854_CHGIN_DTLS) >>
MAX77854_CHGIN_DTLS_SHIFT);
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_DETAILS_01, &chg_dtls);
chg_dtls = ((chg_dtls & MAX77854_CHG_DTLS) >>
MAX77854_CHG_DTLS_SHIFT);
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_00, &chg_cnfg_00);
if (prev_chgin_dtls == chgin_dtls)
stable_count++;
else
stable_count = 0;
if (stable_count > 10) {
pr_info("%s: irq(%d), chgin(0x%x), chg_dtls(0x%x) prev 0x%x\n",
__func__, charger->irq_chgin,
chgin_dtls, chg_dtls, prev_chgin_dtls);
if (charger->is_charging) {
if ((chgin_dtls == 0x02) && \
(battery_health != POWER_SUPPLY_HEALTH_OVERVOLTAGE)) {
pr_info("%s: charger is over voltage\n",
__func__);
value.intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
psy_do_property("battery", set,
POWER_SUPPLY_PROP_HEALTH, value);
} else if (((chgin_dtls == 0x0) || (chgin_dtls == 0x01)) &&(chg_dtls & 0x08) && \
(chg_cnfg_00 & MAX77854_MODE_BUCK) && \
(chg_cnfg_00 & MAX77854_MODE_CHGR) && \
(battery_health != POWER_SUPPLY_HEALTH_UNDERVOLTAGE) && \
(charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_HV_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_PMA_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_PACK &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_PACK_TA &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_STAND &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_HV_STAND)) {
pr_info("%s, vbus_state : 0x%d, chg_state : 0x%d\n", __func__, chgin_dtls, chg_dtls);
pr_info("%s: vBus is undervoltage\n", __func__);
value.intval = POWER_SUPPLY_HEALTH_UNDERVOLTAGE;
psy_do_property("battery", set,
POWER_SUPPLY_PROP_HEALTH, value);
}
} else {
if ((battery_health == \
POWER_SUPPLY_HEALTH_OVERVOLTAGE) &&
(chgin_dtls != 0x02)) {
pr_info("%s: vbus_state : 0x%d, chg_state : 0x%d\n", __func__, chgin_dtls, chg_dtls);
pr_info("%s: overvoltage->normal\n", __func__);
value.intval = POWER_SUPPLY_HEALTH_GOOD;
psy_do_property("battery", set,
POWER_SUPPLY_PROP_HEALTH, value);
} else if ((battery_health == \
POWER_SUPPLY_HEALTH_UNDERVOLTAGE) &&
!((chgin_dtls == 0x0) || (chgin_dtls == 0x01))){
pr_info("%s: vbus_state : 0x%d, chg_state : 0x%d\n", __func__, chgin_dtls, chg_dtls);
pr_info("%s: undervoltage->normal\n", __func__);
value.intval = POWER_SUPPLY_HEALTH_GOOD;
psy_do_property("battery", set,
POWER_SUPPLY_PROP_HEALTH, value);
max77854_set_input_current(charger,
charger->input_current);
}
}
break;
}
prev_chgin_dtls = chgin_dtls;
msleep(100);
}
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, &reg_data);
reg_data &= ~(1 << 6);
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, reg_data);
wake_unlock(&charger->chgin_wake_lock);
}
static irqreturn_t max77854_chgin_irq(int irq, void *data)
{
struct max77854_charger_data *charger = data;
queue_work(charger->wqueue, &charger->chgin_work);
return IRQ_HANDLED;
}
/* register chgin isr after sec_battery_probe */
static void max77854_chgin_init_work(struct work_struct *work)
{
struct max77854_charger_data *charger = container_of(work,
struct max77854_charger_data,
chgin_init_work.work);
int ret;
pr_info("%s \n", __func__);
ret = request_threaded_irq(charger->irq_chgin, NULL,
max77854_chgin_irq, 0, "chgin-irq", charger);
if (ret < 0) {
pr_err("%s: fail to request chgin IRQ: %d: %d\n",
__func__, charger->irq_chgin, ret);
} else {
max77854_update_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, 0, MAX77854_CHGIN_IM);
}
}
static void max77854_wc_current_work(struct work_struct *work)
{
struct max77854_charger_data *charger = container_of(work,
struct max77854_charger_data,
wc_current_work.work);
int diff_current = 0;
if (charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_HV_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_PMA_WIRELESS &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_PACK &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_PACK_TA &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_STAND &&
charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) {
charger->wc_pre_current = WC_CURRENT_START;
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_10, 0x10);
wake_unlock(&charger->wc_current_wake_lock);
return;
}
if (charger->wc_pre_current == charger->wc_current) {
union power_supply_propval value;
max77854_set_charge_current(charger, charger->charging_current);
/* Wcurr-B) Restore Vrect adj room to previous value
after finishing wireless input current setting. Refer to Wcurr-A) step */
msleep(500);
if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_PACK_TA ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_STAND) {
psy_do_property("battery", get, POWER_SUPPLY_PROP_CAPACITY, value);
if (value.intval < charger->pdata->wireless_cc_cv)
value.intval = WIRELESS_VRECT_ADJ_ROOM_4; /* WPC 4.5W, Vrect Room 30mV */
else
value.intval = WIRELESS_VRECT_ADJ_ROOM_5; /* WPC 4.5W, Vrect Room 80mV */
} else if (charger->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS ||
charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) {
value.intval = WIRELESS_VRECT_ADJ_ROOM_5; /* WPC 9W, Vrect Room 80mV */
} else
value.intval = WIRELESS_VRECT_ADJ_OFF; /* PMA 4.5W, Vrect Room 0mV */
psy_do_property(charger->pdata->wireless_charger_name, set,
POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION, value);
wake_unlock(&charger->wc_current_wake_lock);
} else {
if (charger->wc_pre_current > charger->wc_current) {
diff_current = charger->wc_pre_current - charger->wc_current;
if (diff_current < WC_CURRENT_STEP)
charger->wc_pre_current -= diff_current;
else
charger->wc_pre_current -= WC_CURRENT_STEP;
} else {
diff_current = charger->wc_current - charger->wc_pre_current;
if (diff_current < WC_CURRENT_STEP)
charger->wc_pre_current += diff_current;
else
charger->wc_pre_current += WC_CURRENT_STEP;
}
max77854_set_input_current(charger, charger->wc_pre_current);
queue_delayed_work_on(0, charger->wqueue, &charger->wc_current_work,
msecs_to_jiffies(WC_CURRENT_WORK_STEP));
}
pr_info("%s: wc_current(%d), wc_pre_current(%d), diff(%d)\n",
__func__, charger->wc_current, charger->wc_pre_current, diff_current);
}
#ifdef CONFIG_OF
static int max77854_charger_parse_dt(struct max77854_charger_data *charger)
{
struct device_node *np;
sec_battery_platform_data_t *pdata = charger->pdata;
int ret = 0;
np = of_find_node_by_name(NULL, "battery");
if (!np) {
pr_err("%s np NULL\n", __func__);
} else {
int len;
unsigned int i;
const u32 *p;
ret = of_property_read_u32(np, "battery,chg_float_voltage",
&pdata->chg_float_voltage);
if (ret) {
pr_info("%s: battery,chg_float_voltage is Empty\n", __func__);
pdata->chg_float_voltage = 42000;
}
pr_info("%s: battery,chg_float_voltage is %d\n", __func__, pdata->chg_float_voltage);
charger->float_voltage = pdata->chg_float_voltage;
ret = of_property_read_string(np,
"battery,wireless_charger_name", (char const **)&pdata->wireless_charger_name);
if (ret)
pr_info("%s: Wireless charger name is Empty\n", __func__);
ret = of_property_read_u32(np, "battery,full_check_type_2nd",
&pdata->full_check_type_2nd);
if (ret)
pr_info("%s : Full check type 2nd is Empty\n", __func__);
ret = of_property_read_u32(np, "battery,wireless_cc_cv", &pdata->wireless_cc_cv);
if (ret)
pr_info("%s : wireless_cc_cv is Empty\n", __func__);
p = of_get_property(np, "battery,input_current_limit", &len);
if (!p)
return 1;
len = len / sizeof(u32);
pdata->charging_current =
kzalloc(sizeof(sec_charging_current_t) * len,
GFP_KERNEL);
for(i = 0; i < len; i++) {
ret = of_property_read_u32_index(np,
"battery,input_current_limit", i,
&pdata->charging_current[i].input_current_limit);
if (ret)
pr_info("%s : Input_current_limit is Empty\n",
__func__);
}
}
np = of_find_node_by_name(NULL, "max77854-fuelgauge");
if (!np) {
pr_err("%s: np NULL\n", __func__);
} else {
charger->jig_gpio = of_get_named_gpio(np, "fuelgauge,jig_gpio", 0);
if (charger->jig_gpio < 0) {
pr_err("%s error reading jig_gpio = %d\n",
__func__, charger->jig_gpio);
charger->jig_gpio = 0;
}
}
return ret;
}
#endif
static int __devinit max77854_charger_probe(struct platform_device *pdev)
{
struct max77854_dev *max77854 = dev_get_drvdata(pdev->dev.parent);
struct max77854_platform_data *pdata = dev_get_platdata(max77854->dev);
sec_battery_platform_data_t *charger_data;
struct max77854_charger_data *charger;
int ret = 0;
u8 reg_data;
pr_info("%s: Max77854 Charger Driver Loading\n", __func__);
charger = kzalloc(sizeof(*charger), GFP_KERNEL);
if (!charger)
return -ENOMEM;
charger_data = kzalloc(sizeof(sec_battery_platform_data_t), GFP_KERNEL);
if (!charger_data) {
ret = -ENOMEM;
goto err_free;
}
mutex_init(&charger->charger_mutex);
charger->dev = &pdev->dev;
charger->i2c = max77854->charger;
charger->pmic_i2c = max77854->i2c;
charger->pdata = charger_data;
charger->aicl_on = false;
charger->is_mdock = false;
charger->max77854_pdata = pdata;
charger->wc_pre_current = WC_CURRENT_START;
#if defined(CONFIG_OF)
ret = max77854_charger_parse_dt(charger);
if (ret < 0) {
pr_err("%s not found charger dt! ret[%d]\n",
__func__, ret);
}
#endif
platform_set_drvdata(pdev, charger);
charger->psy_chg.name = "max77854-charger";
charger->psy_chg.type = POWER_SUPPLY_TYPE_UNKNOWN;
charger->psy_chg.get_property = max77854_chg_get_property;
charger->psy_chg.set_property = max77854_chg_set_property;
charger->psy_chg.properties = max77854_charger_props;
charger->psy_chg.num_properties = ARRAY_SIZE(max77854_charger_props);
charger->psy_otg.name = "otg";
charger->psy_otg.type = POWER_SUPPLY_TYPE_OTG;
charger->psy_otg.get_property = max77854_otg_get_property;
charger->psy_otg.set_property = max77854_otg_set_property;
charger->psy_otg.properties = max77854_otg_props;
charger->psy_otg.num_properties = ARRAY_SIZE(max77854_otg_props);
max77854_charger_initialize(charger);
max77854_read_reg(charger->i2c, MAX77854_CHG_REG_INT_OK, &reg_data);
if (reg_data & MAX77854_WCIN_OK)
charger->cable_type = POWER_SUPPLY_TYPE_WIRELESS;
charger->input_current = max77854_get_input_current(charger);
charger->charging_current = max77854_get_charge_current(charger);
if (max77854_read_reg(max77854->i2c, MAX77854_PMIC_REG_PMICREV, &reg_data) < 0) {
pr_err("device not found on this channel (this is not an error)\n");
ret = -ENOMEM;
goto err_pdata_free;
} else {
charger->pmic_ver = (reg_data & 0x7);
pr_info("%s : device found : ver.0x%x\n", __func__, charger->pmic_ver);
}
(void) debugfs_create_file("max77854-regs",
S_IRUGO, NULL, (void *)charger, &max77854_debugfs_fops);
charger->wqueue =
create_singlethread_workqueue(dev_name(&pdev->dev));
if (!charger->wqueue) {
pr_err("%s: Fail to Create Workqueue\n", __func__);
goto err_pdata_free;
}
wake_lock_init(&charger->chgin_wake_lock, WAKE_LOCK_SUSPEND,
"charger->chgin");
INIT_WORK(&charger->chgin_work, max77854_chgin_isr_work);
wake_lock_init(&charger->aicl_wake_lock, WAKE_LOCK_SUSPEND,
"charger-aicl");
INIT_DELAYED_WORK(&charger->aicl_work, max77854_aicl_isr_work);
INIT_DELAYED_WORK(&charger->chgin_init_work, max77854_chgin_init_work);
wake_lock_init(&charger->wpc_wake_lock, WAKE_LOCK_SUSPEND,
"charger-wpc");
INIT_DELAYED_WORK(&charger->wpc_work, wpc_detect_work);
wake_lock_init(&charger->wc_current_wake_lock, WAKE_LOCK_SUSPEND,
"charger->wc-current");
INIT_DELAYED_WORK(&charger->wc_current_work, max77854_wc_current_work);
ret = power_supply_register(&pdev->dev, &charger->psy_chg);
if (ret) {
pr_err("%s: Failed to Register psy_chg\n", __func__);
goto err_power_supply_register;
}
ret = power_supply_register(&pdev->dev, &charger->psy_otg);
if (ret) {
pr_err("%s: Failed to Register otg_chg\n", __func__);
goto err_power_supply_register_otg;
}
if (charger->pdata->chg_irq) {
INIT_DELAYED_WORK(&charger->isr_work, max77854_chg_isr_work);
ret = request_threaded_irq(charger->pdata->chg_irq,
NULL, max77854_chg_irq_thread,
charger->pdata->chg_irq_attr,
"charger-irq", charger);
if (ret) {
pr_err("%s: Failed to Request IRQ\n", __func__);
goto err_irq;
}
ret = enable_irq_wake(charger->pdata->chg_irq);
if (ret < 0)
pr_err("%s: Failed to Enable Wakeup Source(%d)\n",
__func__, ret);
}
charger->wc_w_irq = pdata->irq_base + MAX77854_CHG_IRQ_WCIN_I;
ret = request_threaded_irq(charger->wc_w_irq,
NULL, wpc_charger_irq,
IRQF_TRIGGER_FALLING,
"wpc-int", charger);
if (ret) {
pr_err("%s: Failed to Request IRQ\n", __func__);
goto err_wc_irq;
}
max77854_read_reg(charger->i2c,
MAX77854_CHG_REG_INT_OK, &reg_data);
charger->wc_w_state = (reg_data & MAX77854_WCIN_OK)
>> MAX77854_WCIN_OK_SHIFT;
charger->irq_chgin = pdata->irq_base + MAX77854_CHG_IRQ_CHGIN_I;
/* enable chgin irq after sec_battery_probe */
queue_delayed_work(charger->wqueue, &charger->chgin_init_work,
msecs_to_jiffies(3000));
charger->irq_bypass = pdata->irq_base + MAX77854_CHG_IRQ_BYP_I;
ret = request_threaded_irq(charger->irq_bypass, NULL,
max77854_bypass_irq, 0, "bypass-irq", charger);
if (ret < 0) {
pr_err("%s: fail to request bypass IRQ: %d: %d\n",
__func__, charger->irq_bypass, ret);
} else {
max77854_update_reg(charger->i2c,
MAX77854_CHG_REG_INT_MASK, 0, MAX77854_BYP_IM);
}
charger->irq_aicl = pdata->irq_base + MAX77854_CHG_IRQ_AICL_I;
charger->irq_batp = pdata->irq_base + MAX77854_CHG_IRQ_BATP_I;
ret = request_threaded_irq(charger->irq_batp, NULL,
max77854_batp_irq, 0,
"batp-irq", charger);
if (ret < 0)
pr_err("%s: fail to request bypass IRQ: %d: %d\n",
__func__, charger->irq_batp, ret);
ret = max77854_chg_create_attrs(charger->psy_chg.dev);
if (ret) {
dev_err(charger->dev,
"%s : Failed to create_attrs\n", __func__);
goto err_wc_irq;
}
/* watchdog kick */
max77854_update_reg(charger->i2c, MAX77854_CHG_REG_CNFG_06,
MAX77854_WDTCLR, MAX77854_WDTCLR);
pr_info("%s: MAX77854 Charger Driver Loaded\n", __func__);
return 0;
err_wc_irq:
free_irq(charger->pdata->chg_irq, NULL);
err_irq:
power_supply_unregister(&charger->psy_otg);
err_power_supply_register_otg:
power_supply_unregister(&charger->psy_chg);
err_power_supply_register:
destroy_workqueue(charger->wqueue);
err_pdata_free:
kfree(charger_data);
err_free:
kfree(charger);
return ret;
}
static int __devexit max77854_charger_remove(struct platform_device *pdev)
{
struct max77854_charger_data *charger =
platform_get_drvdata(pdev);
destroy_workqueue(charger->wqueue);
free_irq(charger->wc_w_irq, NULL);
free_irq(charger->pdata->chg_irq, NULL);
power_supply_unregister(&charger->psy_chg);
power_supply_unregister(&charger->psy_otg);
kfree(charger);
return 0;
}
#if defined CONFIG_PM
static int max77854_charger_suspend(struct device *dev)
{
return 0;
}
static int max77854_charger_resume(struct device *dev)
{
return 0;
}
#else
#define max77854_charger_suspend NULL
#define max77854_charger_resume NULL
#endif
static void max77854_charger_shutdown(struct device *dev)
{
struct max77854_charger_data *charger =
dev_get_drvdata(dev);
u8 reg_data;
pr_info("%s: MAX77854 Charger driver shutdown\n", __func__);
if (!charger->i2c) {
pr_err("%s: no max77854 i2c client\n", __func__);
return;
}
reg_data = 0x04;
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_00, reg_data);
reg_data = 0x0F;
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_09, reg_data);
reg_data = 0x10;
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_10, reg_data);
reg_data = 0x60;
max77854_write_reg(charger->i2c,
MAX77854_CHG_REG_CNFG_12, reg_data);
pr_info("func:%s \n", __func__);
}
static SIMPLE_DEV_PM_OPS(max77854_charger_pm_ops, max77854_charger_suspend,
max77854_charger_resume);
static struct platform_driver max77854_charger_driver = {
.driver = {
.name = "max77854-charger",
.owner = THIS_MODULE,
#ifdef CONFIG_PM
.pm = &max77854_charger_pm_ops,
#endif
.shutdown = max77854_charger_shutdown,
},
.probe = max77854_charger_probe,
.remove = __devexit_p(max77854_charger_remove),
};
static int __init max77854_charger_init(void)
{
pr_info("%s : \n", __func__);
return platform_driver_register(&max77854_charger_driver);
}
static void __exit max77854_charger_exit(void)
{
platform_driver_unregister(&max77854_charger_driver);
}
module_init(max77854_charger_init);
module_exit(max77854_charger_exit);
MODULE_DESCRIPTION("Samsung MAX77854 Charger Driver");
MODULE_AUTHOR("Samsung Electronics");
MODULE_LICENSE("GPL");