blob: 8a8d022b76fc27eb434e2d9470bc5c4acfe9ee8d [file] [log] [blame]
/*
* driver/muic/s2mu106.c - S2MU106 micro USB switch device driver
*
* Copyright (C) 2018 Samsung Electronics
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#define pr_fmt(fmt) "[MUIC] " fmt
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/muic/muic.h>
#include <linux/mfd/samsung/s2mu106.h>
#include <linux/muic/s2mu106-muic.h>
#if IS_ENABLED(CONFIG_MUIC_SYSFS)
#include <linux/muic/muic_sysfs.h>
#endif
#if IS_ENABLED(CONFIG_HV_MUIC_S2MU106_AFC)
#include <linux/muic/s2mu106-muic-hv.h>
#endif
#if IS_ENABLED(CONFIG_MUIC_NOTIFIER)
#include <linux/muic/muic_notifier.h>
#endif
#if IS_ENABLED(CONFIG_USB_HOST_NOTIFY)
#include <linux/usb_notify.h>
#endif
#include <linux/muic/muic_interface.h>
#if IS_ENABLED(CONFIG_CCIC_NOTIFIER)
#include <linux/ccic/ccic_notifier.h>
#endif
#if IS_ENABLED(CONFIG_VBUS_NOTIFIER)
#include <linux/vbus_notifier.h>
#endif
#if IS_ENABLED(CONFIG_MUIC_UART_SWITCH)
#include <mach/pinctrl-samsung.h>
#endif
#if IS_ENABLED(CONFIG_CP_UART_NOTI)
#include <soc/samsung/exynos-modem-ctrl.h>
#endif
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
#include <linux/fb.h>
#if IS_ENABLED(CONFIG_HICCUP_CHARGER)
#include <linux/sec_batt.h>
#endif
#endif
/* Prototypes of the Static symbols of s2mu106-muic */
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
static void s2mu106_muic_detect_dev_ccic(struct s2mu106_muic_data *muic_data,
muic_attached_dev_t new_dev);
int s2mu106_muic_bcd_rescan(struct s2mu106_muic_data *muic_data);
#endif
static int s2mu106_muic_detect_dev_bc1p2(struct s2mu106_muic_data *muic_data);
static void s2mu106_muic_handle_attached_dev(struct s2mu106_muic_data *muic_data);
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
static void s2mu106_muic_set_water_state(struct s2mu106_muic_data *muic_data, bool en);
static void s2mu106_muic_set_rid_for_water(struct s2mu106_muic_data *muic_data, bool en);
static void s2mu106_muic_set_rid_int_mask_en(struct s2mu106_muic_data *muic_data, bool en);
#endif
void s2mu106_muic_get_detect_info(struct s2mu106_muic_data *muic_data);
static bool s2mu106_muic_is_opmode_typeC(struct s2mu106_muic_data *muic_data);
/*
* Debuging functions
*/
static const char *dev_to_str(muic_attached_dev_t n)
{
char *ret;
switch (n) {
ENUM_STR(ATTACHED_DEV_NONE_MUIC, ret);
ENUM_STR(ATTACHED_DEV_USB_MUIC, ret);
ENUM_STR(ATTACHED_DEV_CDP_MUIC, ret);
ENUM_STR(ATTACHED_DEV_OTG_MUIC, ret);
ENUM_STR(ATTACHED_DEV_TA_MUIC, ret);
ENUM_STR(ATTACHED_DEV_UNOFFICIAL_MUIC, ret);
ENUM_STR(ATTACHED_DEV_UNOFFICIAL_TA_MUIC, ret);
ENUM_STR(ATTACHED_DEV_UNOFFICIAL_ID_MUIC, ret);
ENUM_STR(ATTACHED_DEV_UNOFFICIAL_ID_TA_MUIC, ret);
ENUM_STR(ATTACHED_DEV_UNOFFICIAL_ID_ANY_MUIC, ret);
ENUM_STR(ATTACHED_DEV_UNOFFICIAL_ID_USB_MUIC, ret);
ENUM_STR(ATTACHED_DEV_UNOFFICIAL_ID_CDP_MUIC, ret);
ENUM_STR(ATTACHED_DEV_UNDEFINED_CHARGING_MUIC, ret);
ENUM_STR(ATTACHED_DEV_DESKDOCK_MUIC, ret);
ENUM_STR(ATTACHED_DEV_UNKNOWN_VB_MUIC, ret);
ENUM_STR(ATTACHED_DEV_DESKDOCK_VB_MUIC, ret);
ENUM_STR(ATTACHED_DEV_CARDOCK_MUIC, ret);
ENUM_STR(ATTACHED_DEV_JIG_UART_OFF_MUIC, ret);
ENUM_STR(ATTACHED_DEV_JIG_UART_OFF_VB_MUIC, ret);
ENUM_STR(ATTACHED_DEV_JIG_UART_OFF_VB_OTG_MUIC, ret);
ENUM_STR(ATTACHED_DEV_JIG_UART_OFF_VB_FG_MUIC, ret);
ENUM_STR(ATTACHED_DEV_JIG_UART_ON_MUIC, ret);
ENUM_STR(ATTACHED_DEV_JIG_UART_ON_VB_MUIC, ret);
ENUM_STR(ATTACHED_DEV_JIG_USB_OFF_MUIC, ret);
ENUM_STR(ATTACHED_DEV_JIG_USB_ON_MUIC, ret);
ENUM_STR(ATTACHED_DEV_SMARTDOCK_MUIC, ret);
ENUM_STR(ATTACHED_DEV_SMARTDOCK_VB_MUIC, ret);
ENUM_STR(ATTACHED_DEV_SMARTDOCK_TA_MUIC, ret);
ENUM_STR(ATTACHED_DEV_SMARTDOCK_USB_MUIC, ret);
ENUM_STR(ATTACHED_DEV_UNIVERSAL_MMDOCK_MUIC, ret);
ENUM_STR(ATTACHED_DEV_AUDIODOCK_MUIC, ret);
ENUM_STR(ATTACHED_DEV_MHL_MUIC, ret);
ENUM_STR(ATTACHED_DEV_CHARGING_CABLE_MUIC, ret);
ENUM_STR(ATTACHED_DEV_AFC_CHARGER_PREPARE_MUIC, ret);
ENUM_STR(ATTACHED_DEV_AFC_CHARGER_PREPARE_DUPLI_MUIC, ret);
ENUM_STR(ATTACHED_DEV_AFC_CHARGER_5V_MUIC, ret);
ENUM_STR(ATTACHED_DEV_AFC_CHARGER_5V_DUPLI_MUIC, ret);
ENUM_STR(ATTACHED_DEV_AFC_CHARGER_9V_MUIC, ret);
ENUM_STR(ATTACHED_DEV_AFC_CHARGER_ERR_V_MUIC, ret);
ENUM_STR(ATTACHED_DEV_AFC_CHARGER_ERR_V_DUPLI_MUIC, ret);
ENUM_STR(ATTACHED_DEV_AFC_CHARGER_DISABLED_MUIC, ret);
ENUM_STR(ATTACHED_DEV_QC_CHARGER_PREPARE_MUIC, ret);
ENUM_STR(ATTACHED_DEV_QC_CHARGER_5V_MUIC, ret);
ENUM_STR(ATTACHED_DEV_QC_CHARGER_ERR_V_MUIC, ret);
ENUM_STR(ATTACHED_DEV_QC_CHARGER_9V_MUIC, ret);
ENUM_STR(ATTACHED_DEV_HV_ID_ERR_UNDEFINED_MUIC, ret);
ENUM_STR(ATTACHED_DEV_HV_ID_ERR_UNSUPPORTED_MUIC, ret);
ENUM_STR(ATTACHED_DEV_HV_ID_ERR_SUPPORTED_MUIC, ret);
ENUM_STR(ATTACHED_DEV_HMT_MUIC, ret);
ENUM_STR(ATTACHED_DEV_VZW_ACC_MUIC, ret);
ENUM_STR(ATTACHED_DEV_VZW_INCOMPATIBLE_MUIC, ret);
ENUM_STR(ATTACHED_DEV_USB_LANHUB_MUIC, ret);
ENUM_STR(ATTACHED_DEV_TYPE2_CHG_MUIC, ret);
ENUM_STR(ATTACHED_DEV_TYPE3_MUIC, ret);
ENUM_STR(ATTACHED_DEV_TYPE3_MUIC_TA, ret);
ENUM_STR(ATTACHED_DEV_TYPE3_ADAPTER_MUIC, ret);
ENUM_STR(ATTACHED_DEV_TYPE3_CHARGER_MUIC, ret);
ENUM_STR(ATTACHED_DEV_NONE_TYPE3_MUIC, ret);
ENUM_STR(ATTACHED_DEV_UNSUPPORTED_ID_MUIC, ret);
ENUM_STR(ATTACHED_DEV_UNSUPPORTED_ID_VB_MUIC, ret);
ENUM_STR(ATTACHED_DEV_TIMEOUT_OPEN_MUIC, ret);
ENUM_STR(ATTACHED_DEV_WIRELESS_PAD_MUIC, ret);
ENUM_STR(ATTACHED_DEV_POWERPACK_MUIC, ret);
ENUM_STR(ATTACHED_DEV_UNDEFINED_RANGE_MUIC, ret);
ENUM_STR(ATTACHED_DEV_CHK_WATER_REQ, ret);
ENUM_STR(ATTACHED_DEV_CHK_WATER_DRY_REQ, ret);
ENUM_STR(ATTACHED_DEV_GAMEPAD_MUIC, ret);
ENUM_STR(ATTACHED_DEV_CHECK_OCP, ret);
ENUM_STR(ATTACHED_DEV_RDU_TA_MUIC, ret);
ENUM_STR(ATTACHED_DEV_AFC_CHARGER_9V_DUPLI_MUIC, ret);
ENUM_STR(ATTACHED_DEV_UNKNOWN_MUIC, ret);
ENUM_STR(ATTACHED_DEV_NUM, ret);
default:
return "invalid";
}
return ret;
}
#if IS_ENABLED(CONFIG_MUIC_DEBUG)
#define MAX_LOG 25
#define READ 0
#define WRITE 1
static u8 s2mu106_log_cnt;
static u8 s2mu106_log[MAX_LOG][3];
static void s2mu106_reg_log(u8 reg, u8 value, u8 rw)
{
s2mu106_log[s2mu106_log_cnt][0] = reg;
s2mu106_log[s2mu106_log_cnt][1] = value;
s2mu106_log[s2mu106_log_cnt][2] = rw;
s2mu106_log_cnt++;
if (s2mu106_log_cnt >= MAX_LOG)
s2mu106_log_cnt = 0;
}
static void s2mu106_print_reg_log(void)
{
int i;
u8 reg, value, rw;
char mesg[256] = "";
for (i = 0; i < MAX_LOG; i++) {
reg = s2mu106_log[s2mu106_log_cnt][0];
value = s2mu106_log[s2mu106_log_cnt][1];
rw = s2mu106_log[s2mu106_log_cnt][2];
s2mu106_log_cnt++;
if (s2mu106_log_cnt >= MAX_LOG)
s2mu106_log_cnt = 0;
sprintf(mesg + strlen(mesg), "%x(%x)%x ", reg, value, rw);
}
pr_info("%s %s\n", __func__, mesg);
}
void s2mu106_read_reg_dump(struct s2mu106_muic_data *muic, char *mesg)
{
u8 val;
mutex_lock(&muic->muic_mutex);
s2mu106_read_reg(muic->i2c, S2MU106_REG_MUIC_CTRL1, &val);
sprintf(mesg + strlen(mesg), "CTRL1:%x ", val);
s2mu106_read_reg(muic->i2c, S2MU106_REG_MANUAL_SW_CTRL, &val);
sprintf(mesg + strlen(mesg), "SW_CTRL:%x ", val);
s2mu106_read_reg(muic->i2c, S2MU106_REG_MUIC_INT1_MASK, &val);
sprintf(mesg + strlen(mesg), "IM1:%x ", val);
s2mu106_read_reg(muic->i2c, S2MU106_REG_MUIC_INT2_MASK, &val);
sprintf(mesg + strlen(mesg), "IM2:%x ", val);
s2mu106_read_reg(muic->i2c, S2MU106_REG_CHG_TYP, &val);
sprintf(mesg + strlen(mesg), "CHG_T:%x ", val);
s2mu106_read_reg(muic->i2c, S2MU106_REG_DEVICE_APPLE, &val);
sprintf(mesg + strlen(mesg), "APPLE_DT:%x ", val);
s2mu106_read_reg(muic->i2c, S2MU106_REG_ADC_VALUE, &val);
sprintf(mesg + strlen(mesg), "ADC:%x ", val);
s2mu106_read_reg(muic->i2c, S2MU106_REG_DEVICE_TYP1, &val);
sprintf(mesg + strlen(mesg), "DT1:%x ", val);
s2mu106_read_reg(muic->i2c, S2MU106_REG_DEVICE_TYP2, &val);
sprintf(mesg + strlen(mesg), "DT2:%x ", val);
s2mu106_read_reg(muic->i2c, S2MU106_REG_DEVICE_TYP3, &val);
sprintf(mesg + strlen(mesg), "DT3:%x ", val);
s2mu106_read_reg(muic->i2c, S2MU106_REG_MANUAL_SW_CTRL, &val);
sprintf(mesg + strlen(mesg), "SW_CTRL:%x ", val);
s2mu106_read_reg(muic->i2c, S2MU106_REG_MUIC_INT1, &val);
sprintf(mesg + strlen(mesg), "INT1:%x ", val);
s2mu106_read_reg(muic->i2c, S2MU106_REG_MUIC_INT2, &val);
sprintf(mesg + strlen(mesg), "INT2:%x ", val);
mutex_unlock(&muic->muic_mutex);
}
void s2mu106_print_reg_dump(struct s2mu106_muic_data *muic_data)
{
char mesg[256] = "";
s2mu106_read_reg_dump(muic_data, mesg);
pr_info("%s %s\n", __func__, mesg);
}
void s2mu106_read_reg_dump_otp(struct s2mu106_muic_data *muic_data)
{
int i;
u8 val;
pr_info("%s=======================START\n", __func__);
for (i = 0x6D; i <= 0xD9; i++) {
s2mu106_read_reg(muic_data->i2c, i, &val);
pr_info("0x%X : 0x%X\n", i, val);
}
pr_info("%s=======================END\n", __func__);
}
#endif
/*
* Unit Functions
*/
int s2mu106_i2c_read_byte(struct i2c_client *client, u8 command)
{
int ret = 0;
int retry = 0;
u8 data = 0;
ret = s2mu106_read_reg(client, command, &data);
while (ret < 0) {
pr_info("failed to read reg(0x%x) retry(%d)\n", command, retry);
if (retry > 10) {
pr_err("%s retry failed!!\n", __func__);
break;
}
msleep(100);
ret = s2mu106_read_reg(client, command, &data);
retry++;
}
#if IS_ENABLED(DEBUG_MUIC)
s2mu106_reg_log(command, ret, retry << 1 | READ);
#endif
return data;
}
int s2mu106_i2c_write_byte(struct i2c_client *client, u8 command, u8 value)
{
int ret_r = 0;
int ret_w = 0;
int retry = 0;
u8 written = 0;
ret_w = s2mu106_write_reg(client, command, value);
while (ret_w < 0) {
pr_info("failed to write reg(0x%x) retry(%d)\n", command, retry);
ret_r = s2mu106_read_reg(client, command, &written);
if (ret_r < 0)
pr_err("%s reg(0x%x)\n", __func__, command);
msleep(100);
ret_w = s2mu106_write_reg(client, command, value);
retry++;
}
#if IS_ENABLED(DEBUG_MUIC)
s2mu106_reg_log(command, value, retry << 1 | WRITE);
#endif
return ret_w;
}
static int _s2mu106_i2c_guaranteed_wbyte(struct i2c_client *client,
u8 command, u8 value)
{
int ret;
int retry = 0;
int written;
ret = s2mu106_i2c_write_byte(client, command, value);
written = s2mu106_i2c_read_byte(client, command);
while (written != value) {
pr_info("reg(0x%x): written(0x%x) != value(0x%x)\n",
command, written, value);
if (retry > 10) {
pr_err("%s retry failed!!\n", __func__);
break;
}
msleep(100);
retry++;
ret = s2mu106_i2c_write_byte(client, command, value);
written = s2mu106_i2c_read_byte(client, command);
}
return ret;
}
static int _s2mu106_i2c_update_bit(struct i2c_client *i2c,
u8 reg, u8 mask, u8 shift, u8 value)
{
int ret;
u8 reg_val = 0;
reg_val = s2mu106_i2c_read_byte(i2c, reg);
reg_val &= ~mask;
reg_val |= value << shift;
ret = s2mu106_i2c_write_byte(i2c, reg, reg_val);
pr_info("%s reg(0x%x) value(0x%x)\n", __func__, reg, reg_val);
if (ret < 0)
pr_err("%s Reg = 0x%X, mask = 0x%X, val = 0x%X write err : %d\n",
__func__, reg, mask, value, ret);
return ret;
}
#if defined(CONFIG_MUIC_SUPPORT_PRSWAP)
static void _s2mu106_muic_set_chg_det(struct s2mu106_muic_data *muic_data,
bool enable)
{
struct i2c_client *i2c = muic_data->i2c;
u8 r_val = 0, w_val = 0;
r_val = s2mu106_i2c_read_byte(i2c, S2MU106_REG_RID_DISCHARGE);
if (enable) {
w_val = r_val & ~(RID_DISCHARGE_CHG_DET_OFF_MASK);
} else {
w_val = r_val | RID_DISCHARGE_CHG_DET_OFF_MASK;
}
if(w_val != r_val) {
pr_info("%s en(%d)\n", __func__, enable);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_RID_DISCHARGE, w_val);
}
}
#endif
static int _s2mu106_muic_sel_path(struct s2mu106_muic_data *muic_data,
t_path_data path_data)
{
int ret = 0;
u8 reg_val1, reg_val2;
reg_val1 = s2mu106_i2c_read_byte(muic_data->i2c, S2MU106_REG_MANUAL_SW_CTRL);
reg_val2 = reg_val1 & ~(MANUAL_SW_CTRL_DM_SWITCHING_MASK | MANUAL_SW_CTRL_DP_SWITCHING_MASK);
#if IS_ENABLED(CONFIG_HICCUP_CHARGER)
if (muic_data->is_hiccup_mode)
reg_val2 |= MANUAL_SW_CTRL_UART2_MASK;
else {
#endif
switch (path_data) {
case S2MU106_PATH_USB:
reg_val2 |= MANUAL_SW_CTRL_USB_MASK;
break;
case S2MU106_PATH_UART_AP:
reg_val2 |= MANUAL_SW_CTRL_UART1_MASK;
break;
case S2MU106_PATH_UART_CP:
#if IS_ENABLED(CONFIG_PMU_UART_SWITCH)
reg_val2 |= MANUAL_SW_CTRL_UART1_MASK;
#else
reg_val2 |= MANUAL_SW_CTRL_UART2_MASK;
#endif
break;
case S2MU106_PATH_OPEN:
default:
reg_val2 |= MANUAL_SW_CTRL_OPEN_MASK;
break;
}
#if IS_ENABLED(CONFIG_HICCUP_CHARGER)
}
#endif
if (reg_val1 != reg_val2) {
ret = s2mu106_i2c_write_byte(muic_data->i2c, S2MU106_REG_MANUAL_SW_CTRL, reg_val2);
reg_val2 = s2mu106_i2c_read_byte(muic_data->i2c, S2MU106_REG_MANUAL_SW_CTRL);
pr_info("%s manual_sw_ctrl(%#x->%#x)\n", __func__, reg_val1, reg_val2);
} else
pr_info("%s Skip to set same path val(%#x)\n", __func__, reg_val1);
return ret;
}
static int _s2mu106_muic_set_path_mode(struct s2mu106_muic_data *muic_data,
t_mode_data mode_data)
{
int ret = 0;
u8 reg_val;
pr_info("%s new mode(%d)\n", __func__, mode_data);
reg_val = s2mu106_i2c_read_byte(muic_data->i2c, S2MU106_REG_MUIC_CTRL1);
switch (mode_data) {
case S2MU106_MODE_MANUAL:
reg_val &= ~MUIC_CTRL1_MANUAL_SW_MASK;
break;
case S2MU106_MODE_AUTO:
default:
reg_val |= MUIC_CTRL1_MANUAL_SW_MASK;
break;
}
ret = s2mu106_i2c_write_byte(muic_data->i2c, S2MU106_REG_MUIC_CTRL1, reg_val);
return ret;
}
static int _s2mu106_muic_control_rid_adc(struct s2mu106_muic_data *muic_data, bool enable)
{
int ret = 0;
u8 reg_val = 0;
pr_info("%s (%s)\n", __func__, enable ? "Enable" : "Disable");
reg_val = s2mu106_i2c_read_byte(muic_data->i2c, S2MU106_REG_MUIC_CTRL2);
if (enable)
reg_val &= ~MUIC_CTRL2_ADC_OFF_MASK;
else
reg_val |= MUIC_CTRL2_ADC_OFF_MASK;
ret = s2mu106_i2c_write_byte(muic_data->i2c, S2MU106_REG_MUIC_CTRL2, reg_val);
return 0;
}
static int _s2mu106_muic_set_bcd_rescan_reg(struct s2mu106_muic_data *muic_data)
{
u8 reg_val = 0;
reg_val = s2mu106_i2c_read_byte(muic_data->i2c, S2MU106_REG_BCD_RESCAN);
reg_val |= BCD_RESCAN_BCD_RESCAN_MASK;
s2mu106_i2c_write_byte(muic_data->i2c, S2MU106_REG_BCD_RESCAN, reg_val);
return 0;
}
static inline int _s2mu106_muic_get_rid_adc(struct s2mu106_muic_data *muic_data)
{
return s2mu106_i2c_read_byte(muic_data->i2c, S2MU106_REG_ADC_VALUE) & ADC_VALUE_ADCVAL_MASK;
}
static int _s2mu106_muic_com_to_uart(struct s2mu106_muic_data *muic_data)
{
struct muic_platform_data *muic_pdata = muic_data->pdata;
int ret = 0;
if (muic_data->pdata->is_rustproof) {
pr_info("%s rustproof mode\n", __func__);
return ret;
}
if (muic_pdata->uart_path == MUIC_PATH_UART_AP) {
ret = _s2mu106_muic_sel_path(muic_data, S2MU106_PATH_UART_AP);
#if IS_ENABLED(CONFIG_CP_UART_NOTI)
send_uart_noti_to_modem(MODEM_CTRL_UART_AP);
#endif
} else {
ret = _s2mu106_muic_sel_path(muic_data, S2MU106_PATH_UART_CP);
#if IS_ENABLED(CONFIG_CP_UART_NOTI)
send_uart_noti_to_modem(MODEM_CTRL_UART_CP);
#endif
}
return ret;
}
int _s2mu106_muic_set_jig_on(struct s2mu106_muic_data *muic_data)
{
#if IS_ENABLED(CONFIG_SEC_FACTORY)
struct muic_platform_data *muic_pdata = muic_data->pdata;
bool en = muic_pdata->is_jig_on;
int reg = 0, ret = 0;
pr_err("%s: %s\n", __func__, en ? "on" : "off");
reg = s2mu106_i2c_read_byte(muic_data->i2c,
S2MU106_REG_MANUAL_SW_CTRL);
if (en)
reg |= MANUAL_SW_CTRL_JIG_MASK;
else
reg &= ~(MANUAL_SW_CTRL_JIG_MASK);
ret = s2mu106_i2c_write_byte(muic_data->i2c,
S2MU106_REG_MANUAL_SW_CTRL, (u8)reg);
return ret;
#else
pr_err("%s: Skip the jig control, Not Factory Mode.\n", __func__);
return 0;
#endif
}
#if IS_ENABLED(CONFIG_MUIC_S2MU106_RID)
int _s2mu106_muic_recheck_adc(struct s2mu106_muic_data *muic_data)
{
_s2mu106_muic_control_rid_adc(muic_data, MUIC_DISABLE);
usleep_range(10000, 12000);
_s2mu106_muic_control_rid_adc(muic_data, MUIC_ENABLE);
msleep(200);
return _s2mu106_muic_get_rid_adc(muic_data);
}
int s2mu106_muic_refresh_adc(struct s2mu106_muic_data *muic_data)
{
struct i2c_client *i2c = muic_data->i2c;
int adc = 0;
u8 reg_data, b_Rid_en = 0;
reg_data = s2mu106_i2c_read_byte(muic_data->i2c, S2MU106_REG_MUIC_CTRL2);
if (!(reg_data & MUIC_CTRL2_ADC_OFF_MASK)) {
b_Rid_en = 1;
} else {
pr_info("%s, enable the RID\n", __func__);
reg_data &= ~MUIC_CTRL2_ADC_OFF_MASK;
s2mu106_i2c_write_byte(i2c, S2MU106_REG_MUIC_CTRL2, reg_data);
msleep(35);
}
adc = (s2mu106_i2c_read_byte(muic_data->i2c, S2MU106_REG_ADC_VALUE)) & ADC_VALUE_ADCVAL_MASK;
pr_info("%s, adc : 0x%X\n", __func__, adc);
if (!b_Rid_en) {
pr_info("%s, disable the RID\n", __func__);
reg_data |= MUIC_CTRL2_ADC_OFF_MASK;
s2mu106_i2c_write_byte(i2c, S2MU106_REG_MUIC_CTRL2, reg_data);
}
return adc;
}
#endif
static inline int _s2mu106_muic_get_vbus_state(struct s2mu106_muic_data *muic_data)
{
return (s2mu106_i2c_read_byte(muic_data->i2c, S2MU106_REG_DEVICE_APPLE)
& DEVICE_APPLE_VBUS_WAKEUP_MASK) >> DEVICE_APPLE_VBUS_WAKEUP_SHIFT;
}
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
static void _s2mu106_muic_set_int_mask(struct s2mu106_muic_data *muic_data,
u8 mask1, u8 mask2, u8 val1, u8 val2)
{
/* @ mask1,2 : selected bits to write values
* @ val1,2 : values to write on the masks
* @ ex) unmask DETACH -> (0x02, 0x00, 0x00, 0x00)
*
* - INT1_MASK
* WAKE_UP, USB_KILLER, RID_CHG, LKR, LKP, KP, DETACH, ATTACH
*
* - INT2_MASK
* VBUS_OFF, AV_CHARGE, MHDL, STUCKRCV, STUCK, ADCCHANGE, RSVD_ATTACH, VBUS_ON
*/
struct i2c_client *i2c = muic_data->i2c;
u8 int1_mask_r = 0, int1_mask_w = 0, int2_mask_r = 0, int2_mask_w = 0;
int1_mask_w = int1_mask_r = s2mu106_i2c_read_byte(i2c, S2MU106_REG_MUIC_INT1_MASK);
int2_mask_w = int2_mask_r = s2mu106_i2c_read_byte(i2c, S2MU106_REG_MUIC_INT2_MASK);
if (mask1 || mask2) {
int1_mask_w &= ~(mask1);
int2_mask_w &= ~(mask2);
int1_mask_w |= val1;
int2_mask_w |= val2;
if ((int1_mask_r != int1_mask_w) || (int2_mask_r != int2_mask_w)) {
s2mu106_i2c_write_byte(i2c, S2MU106_REG_MUIC_INT1_MASK, int1_mask_w);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_MUIC_INT2_MASK, int2_mask_w);
pr_info("%s INT1_MASK(%#x->%#x), INT2_MASK(%#x->%#x)\n", __func__,
int1_mask_r, int1_mask_w, int2_mask_r, int2_mask_w);
}
}
}
#endif
#if IS_ENABLED(CONFIG_S2MU106_IFCONN_HOUSE_NOT_GND)
static void _s2mu106_muic_set_rescan_status(struct s2mu106_muic_data *muic_data,
bool en)
{
if (muic_data->is_rescanning != en) {
pr_info("%s is_rescanning(%d->%d)\n", __func__, muic_data->is_rescanning, en);
muic_data->is_rescanning = en;
}
}
static inline bool _s2mu106_muic_get_rescan_status(struct s2mu106_muic_data *muic_data)
{
return muic_data->is_rescanning;
}
#endif
void s2mu106_muic_get_detect_info(struct s2mu106_muic_data *muic_data)
{
struct muic_platform_data *muic_pdata = muic_data->pdata;
struct i2c_client *i2c = muic_data->i2c;
muic_data->reg[DEVICE_TYPE1] = s2mu106_i2c_read_byte(i2c, S2MU106_REG_DEVICE_TYP1);
muic_data->reg[DEVICE_TYPE2] = s2mu106_i2c_read_byte(i2c, S2MU106_REG_DEVICE_TYP2);
muic_data->reg[DEVICE_TYPE3] = s2mu106_i2c_read_byte(i2c, S2MU106_REG_DEVICE_TYP3);
muic_data->reg[DEVICE_APPLE] = s2mu106_i2c_read_byte(i2c, S2MU106_REG_DEVICE_APPLE);
muic_data->reg[CHG_TYPE] = s2mu106_i2c_read_byte(i2c, S2MU106_REG_CHG_TYP);
muic_data->reg[ADC] = _s2mu106_muic_get_rid_adc(muic_data);
muic_data->vbvolt = muic_pdata->vbvolt = _s2mu106_muic_get_vbus_state(muic_data);
muic_data->adc = muic_pdata->adc = muic_data->reg[ADC];
pr_info("dev[1:0x%02x, 2:0x%02x, 3:0x%02x]\n", muic_data->reg[DEVICE_TYPE1],
muic_data->reg[DEVICE_TYPE2], muic_data->reg[DEVICE_TYPE3]);
pr_info("adc:0x%02x, vbvolt:0x%02x, apple:0x%02x\n",
muic_data->adc, muic_data->vbvolt, muic_data->reg[DEVICE_APPLE]);
pr_info("chg_type:0x%02x\n", muic_data->reg[CHG_TYPE]);
}
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
int s2mu106_muic_bcd_rescan(struct s2mu106_muic_data *muic_data)
{
int ret = 0;
pr_info("%s call\n", __func__);
muic_data->rescan_cnt++;
/* muic mux switch open */
ret = _s2mu106_muic_sel_path(muic_data, S2MU106_PATH_OPEN);
if (ret < 0)
pr_err("%s, fail to open mansw\n", __func__);
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
#if IS_ENABLED(CONFIG_S2MU106_IFCONN_HOUSE_NOT_GND)
_s2mu106_muic_set_rescan_status(muic_data, true);
#endif
_s2mu106_muic_control_rid_adc(muic_data, MUIC_DISABLE);
msleep(150);
cancel_delayed_work(&muic_data->rescan_validity_checker);
schedule_delayed_work(&muic_data->rescan_validity_checker,
msecs_to_jiffies(1200));
#endif /* CONFIG_MUIC_S2MU106_RID */
_s2mu106_muic_set_bcd_rescan_reg(muic_data);
return 0;
}
#endif
static void s2mu106_muic_dcd_recheck(struct work_struct *work)
{
struct s2mu106_muic_data *muic_data =
container_of(work, struct s2mu106_muic_data, dcd_recheck.work);
struct muic_platform_data *muic_pdata = muic_data->pdata;
/* Driver re-detects the rescan type. */
int det_ret = S2MU106_DETECT_NONE;
if (muic_core_get_ccic_cable_state(muic_pdata)) {
pr_info("%s Tried to rescan, but cc type detected.\n", __func__);
return;
}
mutex_lock(&muic_data->bcd_rescan_mutex);
if (!muic_data->vbvolt) {
goto skip_dcd_recheck;
}
s2mu106_muic_bcd_rescan(muic_data);
msleep(650);
s2mu106_muic_get_detect_info(muic_data);
if (!muic_data->vbvolt || muic_core_get_ccic_cable_state(muic_data->pdata)) {
goto skip_dcd_recheck;
}
#if IS_ENABLED(CONFIG_S2MU106_IFCONN_HOUSE_NOT_GND)
_s2mu106_muic_set_rescan_status(muic_data, false);
#endif
/* Detect Type & Handle the result */
det_ret = s2mu106_muic_detect_dev_bc1p2(muic_data);
if (det_ret == S2MU106_DETECT_NONE) {
pr_err("%s abnormal detection : set general TA\n", __func__);
muic_data->new_dev = ATTACHED_DEV_TA_MUIC;
}
s2mu106_muic_handle_attached_dev(muic_data);
skip_dcd_recheck:
mutex_unlock(&muic_data->bcd_rescan_mutex);
}
static void s2mu106_muic_rescan_validity_checker(struct work_struct *work)
{
struct s2mu106_muic_data *muic_data =
container_of(work, struct s2mu106_muic_data, rescan_validity_checker.work);
struct muic_platform_data *muic_pdata = muic_data->pdata;
struct muic_interface_t *muic_if = muic_data->if_data;
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
u8 reg_val = 0;
#endif
pr_info("%s entered\n", __func__);
if (!_s2mu106_muic_get_vbus_state(muic_data)
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
|| !muic_data->is_cable_inserted
#endif
) {
return;
} else if (muic_if->is_bypass) {
pr_info("%s is_bypass(%d)\n", __func__, muic_if->is_bypass);
return;
}
if (!MUIC_IS_ATTACHED(muic_pdata->attached_dev)) {
pr_info("%s detected dev(%s)\n", __func__, dev_to_str(muic_pdata->attached_dev));
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
reg_val = s2mu106_i2c_read_byte(muic_data->i2c, S2MU106_REG_MUIC_CTRL2);
if (reg_val & MUIC_CTRL2_ADC_OFF_MASK) {
muic_data->invalid_rescanned = true;
}
#if IS_ENABLED(CONFIG_S2MU106_IFCONN_HOUSE_NOT_GND)
_s2mu106_muic_set_rescan_status(muic_data, false);
#endif
#else
#if !IS_ENABLED(CONFIG_SEC_FACTORY)
pr_info("TIMEOUT DETECTED\n");
muic_data->new_dev = ATTACHED_DEV_TIMEOUT_OPEN_MUIC;
muic_data->is_timeout_attached = true;
s2mu106_muic_handle_attached_dev(muic_data);
#endif
#endif
}
}
/*
* Interface Functions
*/
#if IS_ENABLED(CONFIG_MUIC_S2MU106_RID)
static int s2mu106_if_get_adc(void *mdata)
{
struct s2mu106_muic_data *muic_data = (struct s2mu106_muic_data *)mdata;
int ret = 0;
mutex_lock(&muic_data->switch_mutex);
ret = _s2mu106_muic_get_rid_adc(muic_data);
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
if (ret == ADC_OPEN && muic_data->is_cable_inserted)
ret = ADC_GND;
#endif
mutex_unlock(&muic_data->switch_mutex);
return ret;
}
#endif
#if defined(CONFIG_MUIC_SUPPORT_PRSWAP)
static void s2mu106_if_prswap_work(void *mdata, int mode)
{
struct s2mu106_muic_data *muic_data = (struct s2mu106_muic_data *)mdata;
struct muic_platform_data *muic_pdata = muic_data->pdata;
int adc = 0, vbvolt = 0;
pr_info("%s+ dev(%s)\n", __func__, dev_to_str(muic_pdata->attached_dev));
if (muic_pdata->attached_dev == ATTACHED_DEV_USB_MUIC
|| muic_pdata->attached_dev == ATTACHED_DEV_OTG_MUIC
|| muic_pdata->attached_dev == ATTACHED_DEV_TIMEOUT_OPEN_MUIC) {
pr_err("%s(%d) invalid status\n", __func__, __LINE__);
goto work_done;
}
mutex_lock(&muic_data->muic_mutex);
_s2mu106_muic_set_chg_det(muic_data, MUIC_DISABLE);
_s2mu106_muic_sel_path(muic_data, S2MU106_PATH_USB);
switch (mode) {
case MUIC_PRSWAP_TO_SINK:
adc = _s2mu106_muic_get_rid_adc(muic_data);
vbvolt = _s2mu106_muic_get_vbus_state(muic_data);
muic_core_handle_attach(muic_data->pdata, ATTACHED_DEV_USB_MUIC,
adc, !!vbvolt);
break;
case MUIC_PRSWAP_TO_SRC:
muic_pdata->attached_dev = ATTACHED_DEV_OTG_MUIC;
break;
default:
pr_err("%s(%d) invalid value\n", __func__, __LINE__);
goto work_done;
break;
}
mutex_unlock(&muic_data->muic_mutex);
work_done:
pr_info("%s- dev(%s)\n", __func__, dev_to_str(muic_pdata->attached_dev));
}
#endif
static int s2mu106_if_com_to_open(void *mdata)
{
struct s2mu106_muic_data *muic_data = (struct s2mu106_muic_data *)mdata;
int ret = 0;
mutex_lock(&muic_data->switch_mutex);
ret = _s2mu106_muic_sel_path(muic_data, S2MU106_PATH_OPEN);
if (ret)
pr_err("%s set_com_open err\n", __func__);
mutex_unlock(&muic_data->switch_mutex);
return ret;
}
static int s2mu106_if_com_to_usb(void *mdata)
{
struct s2mu106_muic_data *muic_data = (struct s2mu106_muic_data *)mdata;
int ret = 0;
mutex_lock(&muic_data->switch_mutex);
ret = _s2mu106_muic_sel_path(muic_data, S2MU106_PATH_USB);
if (ret)
pr_err("%s set_com_usb err\n", __func__);
mutex_unlock(&muic_data->switch_mutex);
return ret;
}
static int s2mu106_if_com_to_uart(void *mdata)
{
struct s2mu106_muic_data *muic_data = (struct s2mu106_muic_data *)mdata;
int ret = 0;
mutex_lock(&muic_data->switch_mutex);
ret = _s2mu106_muic_com_to_uart(muic_data);
if (ret)
pr_err("%s set_com_uart err\n", __func__);
mutex_unlock(&muic_data->switch_mutex);
return ret;
}
static int s2mu106_if_get_vbus(void *mdata)
{
struct s2mu106_muic_data *muic_data = (struct s2mu106_muic_data *)mdata;
return _s2mu106_muic_get_vbus_state(muic_data);
}
#if defined(CONFIG_MUIC_SUPPORT_PRSWAP)
static void s2mu106_if_set_chg_det(void *mdata, bool enable)
{
struct s2mu106_muic_data *muic_data = (struct s2mu106_muic_data *)mdata;
_s2mu106_muic_set_chg_det(muic_data, enable);
}
#endif
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
static void s2mu106_if_set_cable_state(void *mdata, muic_attached_dev_t new_dev)
{
struct s2mu106_muic_data *muic_data = (struct s2mu106_muic_data *)mdata;
mutex_lock(&muic_data->muic_mutex);
wake_lock(&muic_data->wake_lock);
s2mu106_muic_detect_dev_ccic(muic_data, new_dev);
wake_unlock(&muic_data->wake_lock);
mutex_unlock(&muic_data->muic_mutex);
}
static void s2mu106_if_cable_recheck(void *mdata)
{
struct s2mu106_muic_data *muic_data = (struct s2mu106_muic_data *)mdata;
mutex_lock(&muic_data->muic_mutex);
wake_lock(&muic_data->wake_lock);
mutex_lock(&muic_data->switch_mutex);
s2mu106_muic_bcd_rescan(muic_data);
mutex_unlock(&muic_data->switch_mutex);
wake_unlock(&muic_data->wake_lock);
mutex_unlock(&muic_data->muic_mutex);
}
#if IS_ENABLED(CONFIG_HV_MUIC_S2MU106_AFC)
static int s2mu106_muic_get_vdnmon(struct s2mu106_muic_data* muic_data)
{
u8 vdnmon = s2mu106_i2c_read_byte(muic_data->i2c, S2MU106_REG_AFC_STATUS);
pr_info("%s(%d) vdnmon(%#x)\n", __func__, __LINE__, vdnmon);
return (vdnmon >> STATUS_VDNMON_SHIFT) & 0x1;
}
static void s2mu106_muic_set_dn_ready_for_killer(struct s2mu106_muic_data *muic_data)
{
struct i2c_client *i2c = muic_data->i2c;
int i;
u8 vdnmon, reg_val;
vdnmon = s2mu106_muic_get_vdnmon(muic_data);
if (!vdnmon)
return;
reg_val = s2mu106_i2c_read_byte(i2c, S2MU106_REG_AFC_CTRL1);
for (i=0; i < 10; i++) {
reg_val = (AFCCTRL1_AFCEN_MASK | AFCCTRL1_DPDNVDEN_MASK |
DP_GND_MASK | DN_GND_MASK);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_CTRL1, reg_val);
usleep_range(20000, 21000);
reg_val = (AFCCTRL1_AFCEN_MASK | AFCCTRL1_DPDNVDEN_MASK |
DP_HIZ_MASK | DN_HIZ_MASK);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_CTRL1, reg_val);
usleep_range(10000, 11000);
vdnmon = s2mu106_muic_get_vdnmon(muic_data);
if (!vdnmon) {
break;
}
}
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_CTRL1,
(AFCCTRL1_AFCEN_MASK | AFCCTRL1_DPDNVDEN_MASK));
}
static int s2mu106_muic_detect_usb_killer(struct s2mu106_muic_data *muic_data)
{
struct i2c_client *i2c = muic_data->i2c;
u8 reg_val = 0;
u8 afc_otp6, dnres, vdnmon, afc_otp3;
int ret = MUIC_NORMAL_OTG;
pr_info("%s entered\n", __func__);
/* Set Data Path to Open. */
reg_val = s2mu106_i2c_read_byte(i2c, S2MU106_REG_MANUAL_SW_CTRL);
if (reg_val & MANUAL_SW_CTRL_DM_SWITCHING_MASK) {
ret = _s2mu106_muic_sel_path(muic_data, S2MU106_PATH_OPEN);
msleep(50);
}
/* AFC Block Enable & INT Masking */
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_INT_MASK, 0xff);
reg_val = (AFCCTRL1_AFCEN_MASK | AFCCTRL1_DPDNVDEN_MASK);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_CTRL1, reg_val);
s2mu106_muic_set_dn_ready_for_killer(muic_data);
afc_otp3 = s2mu106_i2c_read_byte(i2c, S2MU106_REG_AFC_OTP3);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_OTP3,
S2MU106_COMP_REF_SEL_0p4V_MASK | S2MU106_HCOMP_REF_SEL_1p2V_MASK);
usleep_range(10000, 11000);
/* 1st check */
reg_val = (AFCCTRL2_DP06EN_MASK);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_CTRL2, reg_val);
msleep(80);
vdnmon = s2mu106_muic_get_vdnmon(muic_data);
if (!vdnmon) {
pr_info("%s, 1st chk: Normal OTG.", __func__);
ret = MUIC_NORMAL_OTG;
goto exit_chk;
}
/* 2nd check */
afc_otp6 = s2mu106_i2c_read_byte(i2c, S2MU106_REG_AFC_OTP6);
reg_val = (afc_otp6 | AFCOTP6_CTRL_IDM_ON_REG_SEL_MASK);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_OTP6, reg_val);
reg_val = (AFCCTRL2_DNRESEN_MASK);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_CTRL2, reg_val);
reg_val = (AFCCTRL1_CTRLIDMON_MASK | AFCCTRL1_AFCEN_MASK |
AFCCTRL1_DPDNVDEN_MASK);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_CTRL1, reg_val);
usleep_range(20000, 21000);
reg_val = s2mu106_i2c_read_byte(i2c, S2MU106_REG_AFC_STATUS);
pr_info("%s 2nd chk: AFC_STATUS(%#x)\n", __func__, reg_val);
dnres = reg_val & STATUS_DNRES_MASK;
if (!dnres) {
pr_info("%s, USB Killer Condition.", __func__);
ret = MUIC_ABNORMAL_OTG;
}
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_OTP6, afc_otp6);
exit_chk:
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_OTP3, afc_otp3);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_INT_MASK, 0x00);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_CTRL1, 0x0);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_CTRL2, 0x0);
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
if (muic_data->water_status != S2MU106_WATER_MUIC_IDLE)
return MUIC_ABNORMAL_OTG;
#endif
return ret;
}
static int s2mu106_if_check_usb_killer(void *mdata)
{
struct s2mu106_muic_data *muic_data = (struct s2mu106_muic_data *)mdata;
int ret = MUIC_NORMAL_OTG, i;
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
#if !IS_ENABLED(CONFIG_S2MU106_IFCONN_HOUSE_NOT_GND)
int wait_ret = 0;
#endif
#endif
/* vbus discharing off when cc attach event */
if (muic_data->discharging_en) {
if (gpio_is_valid(muic_data->vbus_discharging)) {
if (muic_data->discharging) {
pr_info("%s, discharging forced finished, flush wq\n", __func__);
muic_data->discharging = 0;
gpio_direction_output(muic_data->vbus_discharging, 0);
flush_workqueue(muic_data->discharging_wq);
flush_workqueue(muic_data->discharging_start_wq);
}
}
}
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
#if !IS_ENABLED(CONFIG_S2MU106_IFCONN_HOUSE_NOT_GND)
if (muic_data->water_status != S2MU106_WATER_MUIC_IDLE)
return MUIC_ABNORMAL_OTG;
if (!muic_data->is_cable_inserted) {
wait_ret = wait_event_interruptible_timeout(muic_data->cable_wait,
muic_data->is_cable_inserted == true,
msecs_to_jiffies(300));
if ((wait_ret < 0) || (!wait_ret)) {
pr_err("%s not cable state.\n", __func__);
return MUIC_ABNORMAL_OTG;
}
pr_info("%s, cable detected after while.", __func__);
}
#endif
#endif
for (i = 0; i < 3; i++) {
ret = s2mu106_muic_detect_usb_killer(muic_data);
if (ret == MUIC_NORMAL_OTG)
return ret;
msleep(150);
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
#if !IS_ENABLED(CONFIG_S2MU106_IFCONN_HOUSE_NOT_GND)
if (muic_data->water_status != S2MU106_WATER_MUIC_IDLE)
return MUIC_ABNORMAL_OTG;
#endif
#endif
}
pr_info("%s, USB Killer is detected.", __func__);
return ret;
}
#endif
#endif
static int s2mu106_if_set_bcd_rescan_reg(void *mdata)
{
struct s2mu106_muic_data *muic_data = (struct s2mu106_muic_data *)mdata;
return _s2mu106_muic_set_bcd_rescan_reg(muic_data);
}
static int s2mu106_if_control_rid_adc(void *mdata, bool enable)
{
struct s2mu106_muic_data *muic_data = (struct s2mu106_muic_data *)mdata;
return _s2mu106_muic_control_rid_adc(muic_data, enable);
}
static int s2mu106_if_set_gpio_uart_sel(void *mdata, int uart_path)
{
struct s2mu106_muic_data *muic_data = (struct s2mu106_muic_data *)mdata;
return s2mu106_set_gpio_uart_sel(muic_data, uart_path);
}
static int s2mu106_if_set_jig_ctrl_on(void *mdata)
{
struct s2mu106_muic_data *muic_data =
(struct s2mu106_muic_data *)mdata;
return _s2mu106_muic_set_jig_on(muic_data);
}
#if IS_ENABLED(CONFIG_MUIC_SYSFS)
static int s2mu106_if_show_register(void *mdata, char *mesg)
{
#if IS_ENABLED(CONFIG_MUIC_DEBUG)
struct s2mu106_muic_data *muic_data =
(struct s2mu106_muic_data *)mdata;
if (mesg != NULL)
s2mu106_read_reg_dump(muic_data, mesg);
pr_info("%s %s\n", __func__, mesg);
#endif
return 0;
}
#endif
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
static void s2mu106_if_set_water_det(void *mdata, bool val)
{
struct s2mu106_muic_data *muic_data =
(struct s2mu106_muic_data *)mdata;
mutex_lock(&muic_data->muic_mutex);
pr_info("%s water_status : (%d)\n",
__func__, muic_data->water_status);
if (muic_data->water_dry_status == S2MU106_WATER_DRY_MUIC_DET) {
if (val)
muic_data->water_dry_status = S2MU106_WATER_DRY_MUIC_CCIC_INVALID;
else
muic_data->water_dry_status = S2MU106_WATER_DRY_MUIC_CCIC_DET;
wake_up_interruptible(&muic_data->wait);
} else {
if (muic_data->water_status >= S2MU106_WATER_MUIC_DET) {
if (val)
muic_data->water_status = S2MU106_WATER_MUIC_CCIC_DET;
else
muic_data->water_status = S2MU106_WATER_MUIC_CCIC_INVALID;
pr_info("%s water_status : (%d)\n",
__func__, muic_data->water_status);
wake_up_interruptible(&muic_data->wait);
} else {
pr_err("%s wrong status\n", __func__);
}
}
mutex_unlock(&muic_data->muic_mutex);
}
#if !IS_ENABLED(CONFIG_SEC_FACTORY)
static void s2mu106_if_set_water_det_from_boot(void *mdata, bool val)
{
struct s2mu106_muic_data *muic_data =
(struct s2mu106_muic_data *)mdata;
if (val)
s2mu106_muic_set_water_state(muic_data, MUIC_ENABLE);
}
#endif
#endif
#if IS_ENABLED(CONFIG_HICCUP_CHARGER)
static int s2mu106_if_set_hiccup_mode(void *mdata, bool val)
{
struct s2mu106_muic_data *muic_data =
(struct s2mu106_muic_data *)mdata;
muic_data->is_hiccup_mode = val;
return 0;
}
static int s2mu106_if_get_hiccup_mode(void *mdata)
{
struct s2mu106_muic_data *muic_data =
(struct s2mu106_muic_data *)mdata;
return muic_data->is_hiccup_mode;
}
#endif
int s2mu106_set_gpio_uart_sel(struct s2mu106_muic_data *muic_data, int uart_sel)
{
const char *mode;
struct muic_platform_data *muic_pdata = muic_data->pdata;
#if !IS_ENABLED(CONFIG_MUIC_UART_SWITCH)
int uart_sel_gpio = muic_pdata->gpio_uart_sel;
int uart_sel_val;
int ret;
ret = gpio_request(uart_sel_gpio, "GPIO_UART_SEL");
if (ret) {
pr_err("failed to gpio_request GPIO_UART_SEL\n");
return ret;
}
uart_sel_val = gpio_get_value(uart_sel_gpio);
switch (uart_sel) {
case MUIC_PATH_UART_AP:
mode = "AP_UART";
if (gpio_is_valid(uart_sel_gpio))
gpio_direction_output(uart_sel_gpio, 1);
break;
case MUIC_PATH_UART_CP:
mode = "CP_UART";
if (gpio_is_valid(uart_sel_gpio))
gpio_direction_output(uart_sel_gpio, 0);
break;
default:
mode = "Error";
break;
}
uart_sel_val = gpio_get_value(uart_sel_gpio);
gpio_free(uart_sel_gpio);
pr_info("%s, GPIO_UART_SEL(%d)=%c\n",
mode, uart_sel_gpio, (uart_sel_val == 0 ? 'L' : 'H'));
#else
switch (uart_sel) {
case MUIC_PATH_UART_AP:
mode = "AP_UART";
pin_config_set(muic_pdata.uart_addr, muic_pdata.uart_rxd,
PINCFG_PACK(PINCFG_TYPE_FUNC, 0x2));
pin_config_set(muic_pdata.uart_addr, muic_pdata.uart_txd,
PINCFG_PACK(PINCFG_TYPE_FUNC, 0x2));
break;
case MUIC_PATH_UART_CP:
mode = "CP_UART";
pin_config_set(muic_pdata.uart_addr, muic_pdata.uart_rxd,
PINCFG_PACK(PINCFG_TYPE_FUNC, 0x3));
pin_config_set(muic_pdata.uart_addr, muic_pdata.uart_txd,
PINCFG_PACK(PINCFG_TYPE_FUNC, 0x3));
break;
default:
mode = "Error";
break;
}
pr_info("%s %s\n", __func__, mode);
#endif /* CONFIG_MUIC_UART_SWITCH */
return 0;
}
static int s2mu106_muic_reg_init(struct s2mu106_muic_data *muic_data)
{
struct i2c_client *i2c = muic_data->i2c;
int ret = 0, data = 0;
u8 reg_val = 0;
u8 r_val = 0, w_val = 0;
pr_info("%s\n", __func__);
s2mu106_muic_get_detect_info(muic_data);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_MUIC_INT1_MASK, INT_MUIC_MASK1);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_MUIC_INT2_MASK, INT_MUIC_MASK2);
reg_val = s2mu106_i2c_read_byte(i2c, S2MU106_REG_TIMER_SET3);
reg_val &= ~TIMER_SET3_DCDTMRSET_MASK;
reg_val |= TIMER_SET3_DCDTMRSET_600MS_MASK;
s2mu106_i2c_write_byte(i2c, S2MU106_REG_TIMER_SET3, reg_val);
#if !IS_ENABLED(CONFIG_MUIC_S2MU106_RID)
/* Masking ADC, RID interrupt */
reg_val = s2mu106_i2c_read_byte(i2c, S2MU106_REG_MUIC_INT1_MASK);
reg_val |= MUIC_INT1_MASK_RID_CHG_Im_MASK;
s2mu106_i2c_write_byte(i2c, S2MU106_REG_MUIC_INT1_MASK, reg_val);
reg_val = s2mu106_i2c_read_byte(i2c, S2MU106_REG_MUIC_INT2_MASK);
reg_val |= MUIC_INT2_MASK_ADCCHANGE_Im_MASK;
s2mu106_i2c_write_byte(i2c, S2MU106_REG_MUIC_INT2_MASK, reg_val);
#endif
/* Set VDAT_REF 0.3V */
r_val = s2mu106_i2c_read_byte(i2c, S2MU106_REG_AFC_OTP3);
w_val = r_val & ~S2MU106_COMP_REF_SEL_MASK;
w_val |= S2MU106_COMP_REF_SEL_0p3V_MASK;
s2mu106_i2c_write_byte(i2c, S2MU106_REG_AFC_OTP3, w_val);
pr_info("%s AFC_OTP3 %#x->%#x\n", __func__, r_val, w_val);
/* for usb killer */
_s2mu106_i2c_update_bit(muic_data->i2c, S2MU106_REG_RID_DISCHARGE,
RID_DISCHARGE_USBKILL_OTG_OPTION_MASK,
RID_DISCHARGE_USBKILL_OTG_OPTION_SHIFT,
0x1);
#if IS_ENABLED(CONFIG_MUIC_S2MU106_ENABLE_AUTOSW)
reg_val = MUIC_CTRL1_SWITCH_OPEN_MASK | MUIC_CTRL1_WAIT_MASK
| MUIC_CTRL1_MANUAL_SW_MASK;
#else
reg_val = MUIC_CTRL1_SWITCH_OPEN_MASK | MUIC_CTRL1_WAIT_MASK;
#endif
ret = _s2mu106_i2c_guaranteed_wbyte(i2c, S2MU106_REG_MUIC_CTRL1, reg_val);
if (ret < 0)
pr_err("failed to write ctrl(%d)\n", ret);
data = s2mu106_i2c_read_byte(i2c, S2MU106_REG_MANUAL_SW_CTRL);
pr_info("%s init path(0x%x)\n", __func__, data);
if (data)
_s2mu106_muic_sel_path(muic_data, S2MU106_PATH_OPEN);
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
/*
* These registers represents the RID ADC LDO voltage control.
* Low / High LDO initialized to 3V, 2.7V each.
*/
_s2mu106_muic_control_rid_adc(muic_data, MUIC_ENABLE);
_s2mu106_i2c_update_bit(muic_data->i2c, S2MU106_REG_RID_DISCHARGE,
RID_DISCHARGE_RID_DISCHARGE_ON_MASK,
RID_DISCHARGE_RID_DISCHARGE_ON_SHIFT,
0x1);
_s2mu106_i2c_update_bit(muic_data->i2c, S2MU106_REG_LDOPCP_VSET_OTP,
LDOPCP_VSET_OTP_DETACH_TIME_SET_MASK,
LDOPCP_VSET_OTP_DETACH_TIME_SET_SHIFT,
0x3);
s2mu106_muic_set_rid_for_water(muic_data, MUIC_ENABLE);
#else
if (s2mu106_muic_is_opmode_typeC(muic_data)) {
_s2mu106_muic_control_rid_adc(muic_data, MUIC_DISABLE);
}
#endif
#if IS_ENABLED(CONFIG_VBUS_NOTIFIER)
vbus_notifier_handle((!!muic_data->vbvolt) ? STATUS_VBUS_HIGH : STATUS_VBUS_LOW);
#endif /* CONFIG_VBUS_NOTIFIER */
return ret;
}
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
static void s2mu106_muic_handle_legacy_detach(struct s2mu106_muic_data *muic_data)
{
muic_data->rescan_cnt = 0;
_s2mu106_muic_control_rid_adc(muic_data, MUIC_ENABLE);
if (muic_data->is_cable_inserted) {
muic_data->is_cable_inserted = false;
#if IS_ENABLED(CONFIG_S2MU106_IFCONN_HOUSE_NOT_GND)
MUIC_SEND_NOTI_TO_CCIC_DETACH(muic_data->pdata->attached_dev);
#endif
}
s2mu106_muic_set_rid_int_mask_en(muic_data, MUIC_DISABLE);
return;
}
#endif
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
static void s2mu106_muic_detect_dev_ccic(struct s2mu106_muic_data *muic_data,
muic_attached_dev_t new_dev)
{
struct muic_platform_data *muic_pdata = muic_data->pdata;
int adc = 0, vbvolt = 0;
pr_info("%s new dev(%s)\n", __func__, dev_to_str(new_dev));
if (muic_pdata->attached_dev == new_dev) {
pr_err("%s: Skip to handle duplicated type\n", __func__);
return;
}
if (new_dev == ATTACHED_DEV_NONE_MUIC) {
/* Detach from CCIC */
if (muic_core_get_ccic_cable_state(muic_data->pdata) == false) {
pr_err("%s: Skip to detach legacy type\n", __func__);
return;
}
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
if (!muic_data->is_cable_inserted) {
s2mu106_muic_set_rid_int_mask_en(muic_data, MUIC_DISABLE);
}
#if defined(CONFIG_MUIC_SUPPORT_PRSWAP)
if (muic_pdata->attached_dev == ATTACHED_DEV_USB_MUIC) {
s2mu106_muic_handle_legacy_detach(muic_data);
}
#endif
#endif
muic_pdata->attached_dev = ATTACHED_DEV_NONE_MUIC;
} else {
/* Attach from CCIC */
pr_info("%s DETECTED\n", dev_to_str(new_dev));
switch (new_dev) {
case ATTACHED_DEV_OTG_MUIC:
_s2mu106_muic_sel_path(muic_data, S2MU106_PATH_USB);
break;
case ATTACHED_DEV_TYPE3_CHARGER_MUIC: /* PD Charger */
_s2mu106_muic_sel_path(muic_data, S2MU106_PATH_OPEN);
return;
default:
break;
}
}
if (new_dev != ATTACHED_DEV_NONE_MUIC) {
adc = _s2mu106_muic_get_rid_adc(muic_data);
vbvolt = _s2mu106_muic_get_vbus_state(muic_data);
muic_core_handle_attach(muic_data->pdata, new_dev, adc, !!vbvolt);
muic_pdata->attached_dev = new_dev;
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
_s2mu106_muic_control_rid_adc(muic_data, MUIC_ENABLE);
#endif
} else if (muic_pdata->attached_dev == ATTACHED_DEV_NONE_MUIC) {
muic_core_handle_detach(muic_data->pdata);
}
}
#endif
static int s2mu106_muic_detect_dev_bc1p2(struct s2mu106_muic_data *muic_data)
{
struct muic_interface_t *muic_if = (struct muic_interface_t *)muic_data->if_data;
muic_data->new_dev = ATTACHED_DEV_UNKNOWN_MUIC;
/* Attached */
switch (muic_data->reg[DEVICE_TYPE1]) {
case DEVICE_TYP1_CDPCHG_MASK:
if (muic_data->vbvolt) {
muic_data->new_dev = ATTACHED_DEV_CDP_MUIC;
pr_info("USB_CDP DETECTED\n");
}
break;
case DEVICE_TYP1_USB_MASK:
if (muic_data->vbvolt) {
pr_info("USB DETECTED\n");
muic_data->new_dev = ATTACHED_DEV_USB_MUIC;
#if defined(CONFIG_MUIC_SUPPORT_PRSWAP)
_s2mu106_muic_set_chg_det(muic_data, MUIC_DISABLE);
#endif
}
break;
case DEVICE_TYP1_DCPCHG_MASK:
case DEVICE_TYP1_DCPCHG_MASK | DEVICE_TYP1_USB_MASK:
case DEVICE_TYP1_DCPCHG_MASK | DEVICE_TYP1_CDPCHG_MASK:
if (muic_data->vbvolt) {
muic_if->is_dcp_charger = true;
muic_data->new_dev = ATTACHED_DEV_TA_MUIC;
muic_data->afc_check = true;
pr_info("DEDICATED CHARGER DETECTED\n");
}
break;
#if IS_ENABLED(CONFIG_MUIC_SUPPORT_TYPEB) && IS_ENABLED(CONFIG_MUIC_S2MU106_RID)
case DEVICE_TYP1_USBOTG_MASK:
muic_data->new_dev = ATTACHED_DEV_OTG_MUIC;
pr_info("USB_OTG DETECTED\n");
break;
#endif
case DEVICE_TYP1_CARKIT_MASK:
if (muic_data->vbvolt) {
/* 200K, 442K should be checkef */
if (muic_data->adc == ADC_CEA936ATYPE2_CHG) {
muic_data->new_dev = ATTACHED_DEV_JIG_UART_OFF_VB_MUIC;
pr_info("CEA936ATYPE2_CHG DETECTED\n");
muic_data->afc_check = false;
} else {
muic_data->new_dev = ATTACHED_DEV_USB_MUIC;
pr_info("T1_T2_CHG DETECTED\n");
}
}
break;
default:
break;
}
if (muic_data->new_dev != ATTACHED_DEV_UNKNOWN_MUIC &&
muic_data->new_dev != ATTACHED_DEV_NONE_MUIC)
goto detect_done;
switch (muic_data->reg[DEVICE_TYPE2]) {
case DEVICE_TYP2_SDP_1P8S_MASK:
if (muic_data->vbvolt) {
#if IS_ENABLED(CONFIG_SEC_FACTORY)
pr_info("SDP_1P8S=>USB DETECTED\n");
muic_data->new_dev = ATTACHED_DEV_USB_MUIC;
#else
pr_info("SDP_1P8S DETECTED\n");
#if defined(CONFIG_MUIC_SUPPORT_PRSWAP)
_s2mu106_muic_set_chg_det(muic_data, MUIC_DISABLE);
#endif
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
if (muic_data->rescan_cnt > 1) {
muic_data->new_dev = ATTACHED_DEV_TIMEOUT_OPEN_MUIC;
} else {
schedule_delayed_work(&muic_data->dcd_recheck, msecs_to_jiffies(600));
return S2MU106_DETECT_SKIP;
}
#else
muic_data->new_dev = ATTACHED_DEV_TIMEOUT_OPEN_MUIC;
#if defined(CONFIG_MUIC_SUPPORT_PRSWAP)
_s2mu106_muic_set_chg_det(muic_data, MUIC_DISABLE);
#endif
#endif
}
break;
default:
break;
}
switch (muic_data->reg[CHG_TYPE]) {
case CHG_TYP_DP_3V_SDP_MASK:
if (muic_data->vbvolt) {
pr_info("DP_3V_SDP DETECTED\n");
muic_data->new_dev = ATTACHED_DEV_TIMEOUT_OPEN_MUIC;
#if defined(CONFIG_MUIC_SUPPORT_PRSWAP)
_s2mu106_muic_set_chg_det(muic_data, MUIC_DISABLE);
#endif
#endif
}
break;
default:
break;
}
if (muic_data->new_dev != ATTACHED_DEV_UNKNOWN_MUIC &&
muic_data->new_dev != ATTACHED_DEV_NONE_MUIC)
goto detect_done;
if (muic_data->vbvolt &&
((muic_data->reg[DEVICE_APPLE] & DEVICE_APPLE_APPLE2P4A_CHG_MASK)
|| (muic_data->reg[DEVICE_APPLE] & DEVICE_APPLE_APPLE2A_CHG_MASK)
|| (muic_data->reg[DEVICE_APPLE] & DEVICE_APPLE_APPLE1A_CHG_MASK)
|| (muic_data->reg[DEVICE_APPLE] & DEVICE_APPLE_APPLE0P5A_CHG_MASK))) {
pr_info("APPLE_CHG DETECTED\n");
muic_data->new_dev = ATTACHED_DEV_TA_MUIC;
muic_data->afc_check = false;
}
if ((muic_data->reg[CHG_TYPE] & DEV_TYPE_CHG_TYPE) &&
(muic_data->new_dev == ATTACHED_DEV_UNKNOWN_MUIC)) {
muic_data->new_dev = ATTACHED_DEV_TA_MUIC;
muic_data->afc_check = false;
pr_info("CHG_TYPE DETECTED\n");
if (muic_data->rescan_cnt > 1) {
muic_data->new_dev = ATTACHED_DEV_TA_MUIC;
muic_data->afc_check = false;
} else {
schedule_delayed_work(&muic_data->dcd_recheck, msecs_to_jiffies(100));
return S2MU106_DETECT_SKIP;
}
}
detect_done:
if (muic_data->new_dev != ATTACHED_DEV_UNKNOWN_MUIC)
return S2MU106_DETECT_DONE;
else
return S2MU106_DETECT_NONE;
}
#if IS_ENABLED(CONFIG_MUIC_S2MU106_RID)
static int s2mu106_muic_detect_dev_rid_array(struct s2mu106_muic_data *muic_data)
{
muic_data->new_dev = ATTACHED_DEV_UNKNOWN_MUIC;
switch (muic_data->adc) {
case ADC_JIG_UART_OFF:
pr_info("ADC_JIG_UART_OFF\n");
if (muic_data->vbvolt)
muic_data->new_dev = ATTACHED_DEV_JIG_UART_OFF_VB_MUIC;
else
muic_data->new_dev = ATTACHED_DEV_JIG_UART_OFF_MUIC;
break;
case ADC_JIG_USB_ON:
case ADC_JIG_USB_OFF:
case ADC_DESKDOCK:
muic_data->new_dev = ATTACHED_DEV_JIG_USB_ON_MUIC;
pr_info("ADC JIG_USB_ON DETECTED\n");
break;
case ADC_JIG_UART_ON:
muic_data->new_dev = ATTACHED_DEV_JIG_UART_ON_MUIC;
pr_info("ADC JIG_UART_ON DETECTED\n");
break;
default:
pr_info("%s unsupported ADC(0x%02x)\n", __func__, muic_data->adc);
}
return muic_data->new_dev;
}
#if IS_ENABLED(CONFIG_MUIC_SUPPORT_TYPEB)
static int s2mu106_muic_detect_dev_jig_type(struct s2mu106_muic_data *muic_data)
{
switch (muic_data->reg[DEVICE_TYPE2]) {
case DEVICE_TYP2_JIGUARTOFF_MASK:
if (muic_data->vbvolt)
muic_data->new_dev = ATTACHED_DEV_JIG_UART_OFF_VB_MUIC;
else
muic_data->new_dev = ATTACHED_DEV_JIG_UART_OFF_MUIC;
pr_info("JIG_UART_OFF DETECTED\n");
break;
case DEVICE_TYP2_JIGUSBOFF_MASK:
if (!muic_data->vbvolt)
break;
muic_data->new_dev = ATTACHED_DEV_JIG_USB_OFF_MUIC;
pr_info("JIG_USB_OFF DETECTED\n");
break;
case DEVICE_TYP2_JIGUSBON_MASK:
if (!muic_data->vbvolt)
break;
muic_data->new_dev = ATTACHED_DEV_JIG_USB_ON_MUIC;
pr_info("JIG_USB_ON DETECTED\n");
break;
case DEVICE_TYP2_JIGUARTON_MASK:
if (muic_data->new_dev != ATTACHED_DEV_JIG_UART_ON_MUIC) {
if (!muic_data->vbvolt) {
muic_data->new_dev = ATTACHED_DEV_JIG_UART_ON_MUIC;
pr_info("ADC JIG_UART_ON DETECTED\n");
} else {
muic_data->new_dev = ATTACHED_DEV_JIG_UART_ON_VB_MUIC;
pr_info("ADC JIG_UART_ON_VB DETECTED\n");
}
}
break;
default:
break;
}
return muic_data->new_dev;
}
static int s2mu106_muic_detect_dev_rid_device(struct s2mu106_muic_data *muic_data)
{
struct muic_platform_data *muic_pdata = muic_data->pdata;
muic_data->new_dev = ATTACHED_DEV_UNKNOWN_MUIC;
switch (muic_data->adc) {
case ADC_CEA936ATYPE1_CHG: /*200k ohm */
if (muic_data->vbvolt) {
/* This is workaournd for LG USB cable which has 219k ohm ID */
muic_data->new_dev = ATTACHED_DEV_USB_MUIC;
pr_info("TYPE1 CHARGER DETECTED(USB)\n");
}
break;
case ADC_CEA936ATYPE2_CHG:
if (muic_data->vbvolt) {
muic_data->new_dev = ATTACHED_DEV_UNDEFINED_RANGE_MUIC;
muic_data->afc_check = false;
pr_info("%s unsupported ADC(0x%02x)\n", __func__, muic_data->adc);
}
break;
case ADC_JIG_USB_OFF: /* 255k */
if (!muic_data->vbvolt)
break;
if (muic_data->new_dev != ATTACHED_DEV_JIG_USB_OFF_MUIC) {
muic_data->new_dev = ATTACHED_DEV_JIG_USB_OFF_MUIC;
pr_info("ADC JIG_USB_OFF DETECTED\n");
}
break;
case ADC_JIG_USB_ON:
if (!muic_data->vbvolt)
break;
if (muic_data->new_dev != ATTACHED_DEV_JIG_USB_ON_MUIC) {
if (!muic_data->vbvolt) {
muic_data->new_dev = ATTACHED_DEV_JIG_UART_ON_MUIC;
pr_info("ADC JIG_UART_ON DETECTED\n");
} else {
muic_data->new_dev = ATTACHED_DEV_JIG_UART_ON_VB_MUIC;
pr_info("ADC JIG_UART_ON_VB DETECTED\n");
}
}
break;
case ADC_JIG_UART_OFF:
if (muic_pdata->is_otg_test) {
mdelay(100);
if (muic_data->vmid == 0x4) {
pr_info("OTG_TEST DETECTED, vmid = %d\n", muic_data->vmid);
muic_data->vbvolt = 1;
muic_data->new_dev = ATTACHED_DEV_JIG_UART_OFF_VB_OTG_MUIC;
} else
muic_data->new_dev = ATTACHED_DEV_JIG_UART_OFF_MUIC;
} else if (muic_data->vbvolt)
muic_data->new_dev = ATTACHED_DEV_JIG_UART_OFF_VB_MUIC;
else
muic_data->new_dev = ATTACHED_DEV_JIG_UART_OFF_MUIC;
pr_info("ADC JIG_UART_OFF DETECTED\n");
break;
case ADC_JIG_UART_ON:
if (muic_data->new_dev != ATTACHED_DEV_JIG_UART_ON_MUIC) {
muic_data->new_dev = ATTACHED_DEV_JIG_UART_ON_MUIC;
pr_info("ADC JIG_UART_ON DETECTED\n");
}
break;
case ADC_SMARTDOCK: /* 0x10000 40.2K ohm */
/* SMARTDOCK is not supported */
/* force not to charge the device with SMARTDOCK */
muic_data->new_dev = ATTACHED_DEV_UNSUPPORTED_ID_VB_MUIC;
pr_info("%s unsupported ADC(0x%02x) but charging\n",
__func__, muic_data->adc);
break;
case ADC_HMT: /* 0x10001 49.9K ohm */
muic_data->new_dev = ATTACHED_DEV_UNKNOWN_MUIC;
pr_info("%s unsupported ADC(0x%02x) not charging\n",
__func__, muic_data->adc);
break;
case ADC_AUDIODOCK:
#if IS_ENABLED(CONFIG_MUIC_S2MU106_SUPPORT_AUDIODOCK)
muic_data->new_dev = ATTACHED_DEV_AUDIODOCK_MUIC;
#else
muic_data->new_dev = ATTACHED_DEV_UNDEFINED_RANGE_MUIC;
#endif
pr_info("ADC AUDIODOCK DETECTED\n");
break;
case ADC_UNIVERSAL_MMDOCK:
muic_data->new_dev = ATTACHED_DEV_UNKNOWN_MUIC;
pr_info("%s unsupported ADC(0x%02x) not charging\n",
__func__, muic_data->adc);
break;
case ADC_OPEN:
/* sometimes muic fails to catch JIG_UART_OFF detaching */
/* double check with ADC */
if (muic_data->new_dev == ATTACHED_DEV_JIG_UART_OFF_MUIC) {
muic_data->new_dev = ATTACHED_DEV_UNKNOWN_MUIC;
pr_info("ADC OPEN DETECTED\n");
}
break;
default:
muic_data->new_dev = ATTACHED_DEV_UNDEFINED_RANGE_MUIC;
pr_info("%s unsupported ADC(0x%02x)\n", __func__, muic_data->adc);
}
return muic_data->new_dev;
}
static int s2mu106_muic_detect_dev_mrid_adc(struct s2mu106_muic_data *muic_data)
{
muic_data->new_dev = ATTACHED_DEV_UNKNOWN_MUIC;
if (muic_data->adc & ADC_CONVERSION_ERR_MASK) {
pr_info("%s ADC conversion error! (adc=%#x)\n", __func__, muic_data->adc);
return S2MU106_DETECT_NONE;
}
muic_data->new_dev = s2mu106_muic_detect_dev_jig_type(muic_data);
if (muic_data->new_dev != ATTACHED_DEV_UNKNOWN_MUIC)
return S2MU106_DETECT_DONE;
return S2MU106_DETECT_NONE;
}
#endif /* CONFIG_MUIC_SUPPORT_TYPEB */
#endif /* CONFIG_MUIC_S2MU106_RID */
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
#if IS_ENABLED(CONFIG_HICCUP_CHARGER)
static void s2mu106_muic_set_hiccup_mode(struct s2mu106_muic_data *muic_data, bool en)
{
pr_info("%s en : %d\n", __func__, (int)en);
muic_data->is_hiccup_mode = en;
_s2mu106_muic_sel_path(muic_data, S2MU106_PATH_OPEN);
}
#endif
static void s2mu106_muic_set_rid_int_mask_en(struct s2mu106_muic_data *muic_data, bool en)
{
struct i2c_client *i2c = muic_data->i2c;
pr_info("%s en : %d\n", __func__, (int)en);
if (en) {
s2mu106_i2c_write_byte(i2c, S2MU106_REG_MUIC_INT1_MASK, INT_WATER_MASK1);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_MUIC_INT2_MASK, INT_WATER_MASK2);
} else {
s2mu106_i2c_write_byte(i2c, S2MU106_REG_MUIC_INT1_MASK, INT_MUIC_MASK1);
s2mu106_i2c_write_byte(i2c, S2MU106_REG_MUIC_INT2_MASK, INT_MUIC_MASK2);
}
}
static void s2mu106_muic_set_rid_for_water(struct s2mu106_muic_data *muic_data, bool en)
{
struct i2c_client *i2c;
u8 reg_rid_ldo;
u8 reg_std_bias;
if (muic_data == NULL) {
pr_err("%s no data\n", __func__);
return;
}
i2c = muic_data->i2c;
reg_rid_ldo = s2mu106_i2c_read_byte(i2c, S2MU106_REG_ADCBIAS_OTP4);
reg_std_bias = s2mu106_i2c_read_byte(i2c, S2MU106_REG_RID_WATER_PROOF);
pr_info("%s en : %d, std_bias : 0x%x\n", __func__, (int)en, reg_std_bias & 0xF0);
#if IS_ENABLED(CONFIG_SEC_FACTORY)
reg_rid_ldo &= ~ADCBIAS_OTP4_ADC_STDBY_BIAS_SEL_MASK;
#else
if (en) {
reg_rid_ldo |= ADCBIAS_OTP4_ADC_STDBY_BIAS_SEL_MASK;
reg_std_bias |= 0xF0;
s2mu106_i2c_write_byte(i2c, S2MU106_REG_RID_WATER_PROOF, reg_std_bias);
} else {
reg_rid_ldo &= ~ADCBIAS_OTP4_ADC_STDBY_BIAS_SEL_MASK;
}
#endif
s2mu106_i2c_write_byte(i2c, S2MU106_REG_ADCBIAS_OTP4, reg_rid_ldo);
}
static void s2mu106_muic_put_dry_chk_time(struct s2mu106_muic_data *muic_data)
{
struct timeval time;
do_gettimeofday(&time);
pr_info("%s Dry check time : %ld\n", __func__, (long)time.tv_sec);
muic_data->dry_chk_time = (long)time.tv_sec;
}
static int s2mu106_muic_water_judge(struct s2mu106_muic_data *muic_data)
{
int i, adc = 0;
pr_info("%s : enter\n", __func__);
for (i = 0; i < WATER_DET_RETRY_CNT; i++) {
adc = _s2mu106_muic_recheck_adc(muic_data);
if (adc == ADC_GND) {
pr_info("%s : NOT WATER\n", __func__);
return adc;
}
if (_s2mu106_muic_get_vbus_state(muic_data)) {
pr_info("%s : vbus while detecting\n", __func__);
return ADC_GND;
}
pr_info("%s : %d st try : adc(0x%x)\n", __func__, i, adc);
}
return adc;
}
static void s2mu106_muic_set_water_state(struct s2mu106_muic_data *muic_data, bool en)
{
#if IS_ENABLED(CONFIG_VBUS_NOTIFIER)
int vbvolt = 0;
#endif
pr_info("%s en : %d\n", __func__, (int)en);
if (en) {
pr_info("%s: WATER DETECT!!!\n", __func__);
muic_data->dry_cnt = 0;
muic_data->dry_duration_sec = WATER_DRY_RETRY_INTERVAL_SEC;
MUIC_SEND_NOTI_ATTACH(ATTACHED_DEV_UNDEFINED_RANGE_MUIC);
muic_data->water_status = S2MU106_WATER_MUIC_CCIC_STABLE;
muic_data->water_dry_status = S2MU106_WATER_DRY_MUIC_IDLE;
#if IS_ENABLED(CONFIG_VBUS_NOTIFIER)
vbvolt = _s2mu106_muic_get_vbus_state(muic_data);
vbus_notifier_handle(vbvolt ? STATUS_VBUS_HIGH : STATUS_VBUS_LOW);
#endif /* CONFIG_VBUS_NOTIFIER */
s2mu106_muic_put_dry_chk_time(muic_data);
cancel_delayed_work(&muic_data->water_dry_handler);
schedule_delayed_work(&muic_data->water_dry_handler,
msecs_to_jiffies(1800000));
_s2mu106_muic_control_rid_adc(muic_data, MUIC_DISABLE);
pr_info("%s %d WATER DETECT stabled adc : 0x%X\n", __func__, __LINE__, muic_data->adc);
} else {
pr_info("%s WATER DRIED!!!\n", __func__);
muic_data->water_status = S2MU106_WATER_MUIC_IDLE;
muic_data->water_dry_status = S2MU106_WATER_DRY_MUIC_IDLE;
#if IS_ENABLED(CONFIG_HICCUP_CHARGER)
s2mu106_muic_set_hiccup_mode(muic_data, MUIC_DISABLE);
#endif
muic_data->dry_duration_sec = WATER_DRY_RETRY_INTERVAL_SEC;
muic_data->dry_cnt = 0;
s2mu106_muic_set_rid_for_water(muic_data, MUIC_ENABLE);
MUIC_SEND_NOTI_DETACH(ATTACHED_DEV_UNDEFINED_RANGE_MUIC);
MUIC_SEND_NOTI_TO_CCIC_DETACH(ATTACHED_DEV_UNDEFINED_RANGE_MUIC);
}
}
static void s2mu106_muic_water_detect_handler(struct work_struct *work)
{
struct s2mu106_muic_data *muic_data =
container_of(work, struct s2mu106_muic_data, water_detect_handler.work);
int wait_ret = 0;
mutex_lock(&muic_data->water_det_mutex);
wake_lock(&muic_data->water_wake_lock);
if (muic_data->water_status != S2MU106_WATER_MUIC_VERIFY) {
pr_info("%s %d exit detect, due to status mismatch\n", __func__, __LINE__);
goto EXIT_DETECT;
}
pr_info("%s\n", __func__);
#if IS_ENABLED(CONFIG_HICCUP_CHARGER)
s2mu106_muic_set_hiccup_mode(muic_data, MUIC_ENABLE);
#endif
muic_data->water_status = S2MU106_WATER_MUIC_DET;
_s2mu106_muic_control_rid_adc(muic_data, MUIC_DISABLE);
MUIC_SEND_NOTI_TO_CCIC_ATTACH(ATTACHED_DEV_CHK_WATER_REQ);
wait_ret = wait_event_interruptible_timeout(muic_data->wait,
muic_data->water_status >= S2MU106_WATER_MUIC_CCIC_DET,
msecs_to_jiffies(WATER_CCIC_WAIT_DURATION_MS));
if ((wait_ret < 0) || (!wait_ret)) {
pr_err("%s wait_q abnormal, status : %d\n", __func__, muic_data->water_status);
muic_data->water_status = S2MU106_WATER_MUIC_IDLE;
_s2mu106_muic_control_rid_adc(muic_data, MUIC_ENABLE);
} else {
if (muic_data->water_status == S2MU106_WATER_MUIC_CCIC_DET) {
s2mu106_muic_set_water_state(muic_data, MUIC_ENABLE);
} else if (muic_data->water_status == S2MU106_WATER_MUIC_CCIC_INVALID) {
pr_info("%s Not Water From CCIC.\n", __func__);
muic_data->water_status = S2MU106_WATER_MUIC_IDLE;
_s2mu106_muic_control_rid_adc(muic_data, MUIC_ENABLE);
#if IS_ENABLED(CONFIG_HICCUP_CHARGER)
s2mu106_muic_set_hiccup_mode(muic_data, MUIC_DISABLE);
#endif
}
}
EXIT_DETECT:
wake_unlock(&muic_data->water_wake_lock);
mutex_unlock(&muic_data->water_det_mutex);
}
static void s2mu106_muic_water_dry_handler(struct work_struct *work)
{
struct s2mu106_muic_data *muic_data =
container_of(work, struct s2mu106_muic_data, water_dry_handler.work);
int adc, i = 0;
int wait_ret = 0;
mutex_lock(&muic_data->water_dry_mutex);
wake_lock(&muic_data->water_dry_wake_lock);
if (muic_data->water_status != S2MU106_WATER_MUIC_CCIC_STABLE) {
pr_info("%s Invalid status for Dry check\n", __func__);
goto EXIT_DRY_STATE;
}
pr_info("%s Dry check start\n", __func__);
s2mu106_muic_put_dry_chk_time(muic_data);
s2mu106_muic_set_rid_int_mask_en(muic_data, true);
if (muic_data->dry_cnt++ > 5) {
muic_data->dry_duration_sec = 1800;
pr_info("%s Dry check cnt : %d\n", __func__, muic_data->dry_cnt);
}
for (i = 0; i < WATER_DET_RETRY_CNT; i++) {
adc = _s2mu106_muic_recheck_adc(muic_data);
pr_info("%s, %d th try, adc : 0x%X\n", __func__, i, (char)adc);
if (adc < ADC_OPEN) {
pr_info("%s WATER IS NOT DRIED YET!!!\n", __func__);
_s2mu106_muic_control_rid_adc(muic_data, MUIC_DISABLE);
cancel_delayed_work(&muic_data->water_dry_handler);
schedule_delayed_work(&muic_data->water_dry_handler,
msecs_to_jiffies(1800000));
msleep(1000);
goto EXIT_DRY;
}
}
s2mu106_muic_set_rid_int_mask_en(muic_data, false);
muic_data->water_dry_status = S2MU106_WATER_DRY_MUIC_DET;
MUIC_SEND_NOTI_TO_CCIC_ATTACH(ATTACHED_DEV_CHK_WATER_DRY_REQ);
wait_ret = wait_event_interruptible_timeout(muic_data->wait,
muic_data->water_dry_status >= S2MU106_WATER_DRY_MUIC_CCIC_DET,
msecs_to_jiffies(WATER_CCIC_WAIT_DURATION_MS));
if ((wait_ret < 0) || (!wait_ret)
|| muic_data->water_dry_status == S2MU106_WATER_DRY_MUIC_CCIC_INVALID) {
pr_err("%s wait_q abnormal, status : %d\n",
__func__, muic_data->water_dry_status);
_s2mu106_muic_control_rid_adc(muic_data, MUIC_DISABLE);
muic_data->water_dry_status = S2MU106_WATER_DRY_MUIC_IDLE;
msleep(1000);
muic_data->water_status = S2MU106_WATER_MUIC_CCIC_STABLE;
cancel_delayed_work(&muic_data->water_dry_handler);
schedule_delayed_work(&muic_data->water_dry_handler,
msecs_to_jiffies(WATER_DRY_RETRY_INTERVAL_MS));
} else if (muic_data->water_dry_status == S2MU106_WATER_DRY_MUIC_CCIC_DET) {
msleep(500);
s2mu106_muic_set_water_state(muic_data, MUIC_DISABLE);
}
EXIT_DRY:
pr_info("%s %d Exit DRY handler!!!\n", __func__, __LINE__);
s2mu106_muic_set_rid_int_mask_en(muic_data, false);
EXIT_DRY_STATE:
wake_unlock(&muic_data->water_dry_wake_lock);
mutex_unlock(&muic_data->water_dry_mutex);
}
static void s2mu106_muic_sleep_dry_checker(struct work_struct *work)
{
struct s2mu106_muic_data *muic_data =
container_of(work, struct s2mu106_muic_data, sleep_dry_checker.work);
struct timeval time;
long duration;
pr_info("%s entered\n", __func__);
if (muic_data->water_status == S2MU106_WATER_MUIC_CCIC_STABLE && muic_data->lcd_on) {
if (!_s2mu106_muic_get_vbus_state(muic_data)) {
do_gettimeofday(&time);
duration = (long)time.tv_sec - muic_data->dry_chk_time;
pr_info("%s dry check duration : (%ld), compare : (%ld)\n",
__func__, duration, muic_data->dry_duration_sec);
if ((duration > muic_data->dry_duration_sec) || (duration < 0)) {
cancel_delayed_work(&muic_data->water_dry_handler);
schedule_delayed_work(&muic_data->water_dry_handler, 0);
}
}
}
}
static int muic_fb_notifier_event(struct notifier_block *this,
unsigned long val, void *v)
{
struct fb_event *evdata = v;
struct s2mu106_muic_data *muic_data =
container_of(this, struct s2mu106_muic_data, fb_notifier);
int fb_blank = 0;
if (evdata->info->node)
return NOTIFY_DONE;
switch (val) {
case FB_EVENT_BLANK:
break;
default:
return NOTIFY_DONE;
}
fb_blank = *(int *)evdata->data;
if (fb_blank == FB_BLANK_UNBLANK) {
pr_info("%s: lcd on\n", __func__);
muic_data->lcd_on = true;
pr_info("%s entered, w_state : %d\n", __func__, muic_data->water_status);
if (muic_data->water_status == S2MU106_WATER_MUIC_CCIC_STABLE) {
cancel_delayed_work(&muic_data->sleep_dry_checker);
schedule_delayed_work(&muic_data->sleep_dry_checker,
msecs_to_jiffies(WATER_WAKEUP_WAIT_DURATION_MS));
}
} else
muic_data->lcd_on = false;
return NOTIFY_DONE;
}
#endif
static void s2mu106_muic_handle_attached_dev(struct s2mu106_muic_data *muic_data)
{
struct muic_platform_data *muic_pdata = muic_data->pdata;
if (muic_data->new_dev != ATTACHED_DEV_UNKNOWN_MUIC &&
muic_data->new_dev != muic_pdata->attached_dev) {
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
muic_manager_set_legacy_dev(muic_pdata->muic_if, muic_data->new_dev);
#endif
muic_core_handle_attach(muic_pdata, muic_data->new_dev,
muic_pdata->adc, muic_pdata->vbvolt);
}
}
static void _s2mu106_muic_resend_jig_type(struct s2mu106_muic_data *muic_data)
{
struct muic_platform_data *muic_pdata = muic_data->pdata;
if (muic_data->vbvolt
&& (muic_pdata->attached_dev == ATTACHED_DEV_JIG_UART_ON_MUIC)) {
muic_data->new_dev = ATTACHED_DEV_JIG_UART_ON_VB_MUIC;
s2mu106_muic_handle_attached_dev(muic_data);
} else if (!muic_data->vbvolt
&& (muic_pdata->attached_dev == ATTACHED_DEV_JIG_UART_ON_VB_MUIC)) {
muic_data->new_dev = ATTACHED_DEV_JIG_UART_ON_MUIC;
s2mu106_muic_handle_attached_dev(muic_data);
}
}
#if IS_ENABLED(CONFIG_MUIC_SUPPORT_CCIC)
static bool s2mu106_muic_is_opmode_typeC(struct s2mu106_muic_data *muic_data)
{
struct muic_interface_t *muic_if;
if (muic_data->if_data == NULL) {
pr_err("%s data NULL\n", __func__);
return false;
}
if (muic_data->if_data)
muic_if = muic_data->if_data;
if (muic_if->opmode & OPMODE_CCIC)
return true;
else
return false;
}
#endif
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
static void s2mu106_muic_handle_cable_inserted(struct s2mu106_muic_data *muic_data)
{
pr_info("%s enter\n", __func__);
muic_data->is_cable_inserted = true;
#if IS_ENABLED(CONFIG_S2MU106_IFCONN_HOUSE_NOT_GND)
MUIC_SEND_NOTI_TO_CCIC_ATTACH(ATTACHED_DEV_TYPE3_MUIC);
#endif
}
#endif
static irqreturn_t s2mu106_muic_attach_isr(int irq, void *data)
{
struct s2mu106_muic_data *muic_data = data;
struct muic_platform_data *muic_pdata;
int det_ret = S2MU106_DETECT_NONE;
if (muic_data == NULL) {
pr_err("%s data NULL\n", __func__);
return IRQ_NONE;
}
muic_pdata = muic_data->pdata;
if (muic_pdata == NULL) {
pr_err("%s data NULL\n", __func__);
return IRQ_NONE;
}
mutex_lock(&muic_data->muic_mutex);
wake_lock(&muic_data->wake_lock);
s2mu106_muic_get_detect_info(muic_data);
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
#if IS_ENABLED(CONFIG_S2MU106_IFCONN_HOUSE_NOT_GND)
_s2mu106_muic_set_rescan_status(muic_data, false);
#endif
if (s2mu106_muic_is_opmode_typeC(muic_data)) {
if (!muic_data->is_cable_inserted
&& muic_data->adc == ADC_GND) {
s2mu106_muic_handle_cable_inserted(muic_data);
wake_up_interruptible(&muic_data->cable_wait);
}
}
#endif
if (MUIC_IS_ATTACHED(muic_pdata->attached_dev)) {
pr_err("%s Cable type already was attached\n", __func__);
goto attach_skip;
}
pr_info("%s start(%s)\n", __func__, dev_to_str(muic_pdata->attached_dev));
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
pr_info("%s rescan cnt : %d\n", __func__, muic_data->rescan_cnt);
if (s2mu106_muic_is_opmode_typeC(muic_data)) {
if (IS_WATER_ADC(muic_data->adc)
|| IS_WATER_STATUS(muic_data->water_status)) {
goto attach_skip;
} else if (muic_data->rescan_cnt == 0
&& muic_data->adc == ADC_GND
&& muic_data->vbvolt
&& !muic_core_get_ccic_cable_state(muic_pdata)
&& !IS_WATER_STATUS(muic_data->water_status)) {
s2mu106_muic_bcd_rescan(muic_data);
goto attach_skip;
}
}
#endif
det_ret = s2mu106_muic_detect_dev_bc1p2(muic_data);
if (det_ret == S2MU106_DETECT_DONE)
goto attach_done;
else if (det_ret == S2MU106_DETECT_SKIP)
goto attach_skip;
#if IS_ENABLED(CONFIG_MUIC_S2MU106_RID) && IS_ENABLED(CONFIG_MUIC_SUPPORT_TYPEB)
det_ret = s2mu106_muic_detect_dev_mrid_adc(muic_data);
#endif
attach_done:
s2mu106_muic_handle_attached_dev(muic_data);
attach_skip:
pr_info("%s done(%s)\n", __func__, dev_to_str(muic_pdata->attached_dev));
wake_unlock(&muic_data->wake_lock);
mutex_unlock(&muic_data->muic_mutex);
return IRQ_HANDLED;
}
static irqreturn_t s2mu106_muic_detach_isr(int irq, void *data)
{
struct s2mu106_muic_data *muic_data = data;
struct muic_platform_data *muic_pdata;
if (muic_data == NULL) {
pr_err("%s data NULL\n", __func__);
return IRQ_NONE;
}
muic_pdata = muic_data->pdata;
if (muic_pdata == NULL) {
pr_err("%s data NULL\n", __func__);
return IRQ_NONE;
}
pr_info("%s start(%s)\n", __func__, dev_to_str(muic_pdata->attached_dev));
mutex_lock(&muic_data->muic_mutex);
wake_lock(&muic_data->wake_lock);
s2mu106_muic_get_detect_info(muic_data);
#if IS_ENABLED(CONFIG_S2MU106_IFCONN_HOUSE_NOT_GND)
if (_s2mu106_muic_get_rescan_status(muic_data)) {
pr_err("%s rid disable in rescan\n", __func__);
goto detach_skip;
}
#endif
if (MUIC_IS_ATTACHED(muic_pdata->attached_dev) == false) {
pr_err("%s Cable type already was detached\n", __func__);
#if IS_ENABLED(CONFIG_S2MU106_IFCONN_HOUSE_NOT_GND)
if (muic_data->adc == ADC_OPEN && muic_data->is_cable_inserted) {
muic_data->is_cable_inserted = false;
MUIC_SEND_NOTI_TO_CCIC_DETACH(muic_data->pdata->attached_dev);
}
#endif
goto detach_skip;
}
#if IS_ENABLED(CONFIG_MUIC_SUPPORT_CCIC)
if (s2mu106_muic_is_opmode_typeC(muic_data)) {
if (!muic_core_get_ccic_cable_state(muic_data->pdata)) {
muic_core_handle_detach(muic_data->pdata);
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
s2mu106_muic_handle_legacy_detach(muic_data);
#endif
}
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
else if (muic_data->adc == ADC_OPEN && muic_data->is_cable_inserted) {
muic_data->is_cable_inserted = false;
}
#endif
} else
muic_core_handle_detach(muic_data->pdata);
#else
muic_core_handle_detach(muic_data->pdata);
#endif
detach_skip:
pr_info("%s done(%s)\n", __func__, dev_to_str(muic_pdata->attached_dev));
wake_unlock(&muic_data->wake_lock);
mutex_unlock(&muic_data->muic_mutex);
return IRQ_HANDLED;
}
static irqreturn_t s2mu106_muic_vbus_on_isr(int irq, void *data)
{
struct s2mu106_muic_data *muic_data = data;
struct muic_platform_data *muic_pdata;
if (muic_data == NULL) {
pr_err("%s data NULL\n", __func__);
return IRQ_NONE;
}
muic_pdata = muic_data->pdata;
if (muic_pdata == NULL) {
pr_err("%s data NULL\n", __func__);
return IRQ_NONE;
}
mutex_lock(&muic_data->muic_mutex);
wake_lock(&muic_data->wake_lock);
pr_info("%s start(%s)\n", __func__, dev_to_str(muic_pdata->attached_dev));
if (muic_data->discharging_en) {
if (gpio_is_valid(muic_data->vbus_discharging)) {
if (muic_data->discharging) {
pr_info("%s, discharging forced finished, flush wq\n", __func__);
muic_data->discharging = 0;
gpio_direction_output(muic_data->vbus_discharging, 0);
flush_workqueue(muic_data->discharging_wq);
flush_workqueue(muic_data->discharging_start_wq);
}
}
}
s2mu106_muic_get_detect_info(muic_data);
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
if (s2mu106_muic_is_opmode_typeC(muic_data)) {
muic_data->invalid_rescanned = false;
cancel_delayed_work(&muic_data->rescan_validity_checker);
schedule_delayed_work(&muic_data->rescan_validity_checker,
msecs_to_jiffies(1200));
if (!muic_data->is_cable_inserted
&& muic_data->adc == ADC_GND) {
s2mu106_muic_handle_cable_inserted(muic_data);
wake_up_interruptible(&muic_data->cable_wait);
}
#if !IS_ENABLED(CONFIG_MUIC_S2MU106_FAST_DETECTION)
if (muic_data->rescan_cnt == 0
&&muic_data->adc == ADC_GND
&& !muic_core_get_ccic_cable_state(muic_pdata)
&& !IS_WATER_STATUS(muic_data->water_status)) {
s2mu106_muic_bcd_rescan(muic_data);
}
#endif
}
#else
muic_pdata->vbvolt = muic_data->vbvolt = _s2mu106_muic_get_vbus_state(muic_data);
if (s2mu106_muic_is_opmode_typeC(muic_data)) {
cancel_delayed_work(&muic_data->rescan_validity_checker);
schedule_delayed_work(&muic_data->rescan_validity_checker,
msecs_to_jiffies(1200));
}
#endif
#if IS_ENABLED(CONFIG_HICCUP_CHARGER) && IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
if (!lpcharge &&
IS_WATER_STATUS(muic_data->water_status)) {
s2mu106_muic_set_hiccup_mode(muic_data, MUIC_ENABLE);
}
#endif
#if IS_ENABLED(CONFIG_VBUS_NOTIFIER)
vbus_notifier_handle(STATUS_VBUS_HIGH);
#endif /* CONFIG_VBUS_NOTIFIER */
_s2mu106_muic_resend_jig_type(muic_data);
pr_info("%s done(%s)\n", __func__, dev_to_str(muic_pdata->attached_dev));
wake_unlock(&muic_data->wake_lock);
mutex_unlock(&muic_data->muic_mutex);
return IRQ_HANDLED;
}
static void s2mu106_muic_discharging_handler(struct work_struct *work)
{
struct s2mu106_muic_data *muic_data =
container_of(work, struct s2mu106_muic_data, discharging_handler.work);
if (!muic_data->discharging)
return;
muic_data->discharging = 0;
pr_info("%s, discharging finished\n", __func__);
gpio_direction_output(muic_data->vbus_discharging, 0);
return;
}
static void s2mu106_muic_discharging_start_handler(struct work_struct *work)
{
struct s2mu106_muic_data *muic_data =
container_of(work, struct s2mu106_muic_data, discharging_start_handler.work);
if (!muic_data->discharging)
return;
pr_info("%s, discharging start\n", __func__);
gpio_direction_output(muic_data->vbus_discharging, 1);
return;
}
static irqreturn_t s2mu106_muic_vbus_off_isr(int irq, void *data)
{
struct s2mu106_muic_data *muic_data = data;
struct muic_platform_data *muic_pdata;
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
struct muic_interface_t *muic_if;
#endif
if (muic_data == NULL) {
pr_err("%s data NULL\n", __func__);
return IRQ_NONE;
}
muic_pdata = muic_data->pdata;
if (muic_pdata == NULL) {
pr_err("%s data NULL\n", __func__);
return IRQ_NONE;
}
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
muic_if = muic_data->if_data;
if (muic_if == NULL) {
pr_err("%s data NULL\n", __func__);
return IRQ_NONE;
}
#endif
mutex_lock(&muic_data->muic_mutex);
wake_lock(&muic_data->wake_lock);
pr_info("%s start(%s)\n", __func__, dev_to_str(muic_pdata->attached_dev));
if (muic_data->discharging_en) {
if (gpio_is_valid(muic_data->vbus_discharging)) {
pr_info("%s, discharging start after 300ms\n", __func__);
muic_data->discharging = 1;
queue_delayed_work(muic_data->discharging_start_wq, &muic_data->discharging_start_handler, 300);
queue_delayed_work(muic_data->discharging_wq, &muic_data->discharging_handler, 500);
}
}
muic_pdata->vbvolt = muic_data->vbvolt = _s2mu106_muic_get_vbus_state(muic_data);
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
muic_data->rescan_cnt = 0;
#endif
#if IS_ENABLED(CONFIG_VBUS_NOTIFIER)
vbus_notifier_handle(STATUS_VBUS_LOW);
#endif /* CONFIG_VBUS_NOTIFIER */
_s2mu106_muic_resend_jig_type(muic_data);
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
if (muic_data->invalid_rescanned
&& !MUIC_IS_ATTACHED(muic_pdata->attached_dev)) {
#if IS_ENABLED(CONFIG_S2MU106_IFCONN_HOUSE_NOT_GND)
_s2mu106_muic_set_rescan_status(muic_data, false);
#endif
_s2mu106_muic_control_rid_adc(muic_data, MUIC_ENABLE);
muic_data->invalid_rescanned = false;
}
#endif
if (muic_data->is_timeout_attached) {
muic_data->is_timeout_attached = false;
if (muic_pdata->attached_dev == ATTACHED_DEV_TIMEOUT_OPEN_MUIC)
muic_core_handle_detach(muic_data->pdata);
}
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
if (s2mu106_muic_is_opmode_typeC(muic_data)) {
if (muic_core_get_ccic_cable_state(muic_pdata)
&& muic_if->is_ccic_attached == false) {
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
_s2mu106_muic_control_rid_adc(muic_data, MUIC_ENABLE);
_s2mu106_muic_set_int_mask(muic_data, 0x3, 0x0, 0x0, 0x0);
#endif
muic_core_handle_detach(muic_data->pdata);
}
}
#endif
pr_info("%s done(%s)\n", __func__, dev_to_str(muic_pdata->attached_dev));
wake_unlock(&muic_data->wake_lock);
mutex_unlock(&muic_data->muic_mutex);
return IRQ_HANDLED;
}
#if IS_ENABLED(CONFIG_MUIC_S2MU106_RID)
static irqreturn_t s2mu106_muic_rid_chg_isr(int irq, void *data)
{
struct s2mu106_muic_data *muic_data = data;
struct muic_platform_data *muic_pdata;
int det_ret = S2MU106_DETECT_NONE;
if (muic_data == NULL) {
pr_err("%s data NULL\n", __func__);
return IRQ_NONE;
}
muic_pdata = muic_data->pdata;
if (muic_pdata == NULL) {
pr_err("%s data NULL\n", __func__);
return IRQ_NONE;
}
mutex_lock(&muic_data->muic_mutex);
wake_lock(&muic_data->wake_lock);
#if IS_ENABLED(CONFIG_MUIC_S2MU106_FAST_DETECTION)
s2mu106_muic_get_detect_info(muic_data);
if (MUIC_IS_ATTACHED(muic_pdata->attached_dev)) {
pr_err("%s Cable type already was attached\n", __func__);
goto attach_skip;
}
pr_info("%s start(%s)\n", __func__, dev_to_str(muic_pdata->attached_dev));
if (s2mu106_muic_is_opmode_typeC(muic_data)) {
if (!muic_data->is_cable_inserted
&& muic_data->adc == ADC_GND) {
s2mu106_muic_handle_cable_inserted(muic_data);
}
if (muic_data->adc == ADC_GND
&& muic_data->vbvolt
&& !muic_core_get_ccic_cable_state(muic_pdata)
&& !IS_WATER_STATUS(muic_data->water_status)) {
s2mu106_muic_set_rid_int_mask_en(muic_data, MUIC_ENABLE);
_s2mu106_muic_control_rid_adc(muic_data, MUIC_DISABLE);
s2mu106_muic_set_rid_int_mask_en(muic_data, MUIC_DISABLE);
det_ret = s2mu106_muic_detect_dev_bc1p2(muic_data);
if (det_ret == S2MU106_DETECT_DONE)
goto attach_done;
else if (det_ret == S2MU106_DETECT_SKIP)
goto attach_skip;
} else if (IS_WATER_ADC(muic_data->adc)
|| IS_WATER_STATUS(muic_data->water_status)) {
goto attach_skip;
}
}
attach_done:
s2mu106_muic_handle_attached_dev(muic_data);
attach_skip:
pr_info("%s done(%s)\n", __func__, dev_to_str(muic_pdata->attached_dev));
#endif
pr_info("%s Vbus(%s), rid_adc(%#x), Type(%s) det_ret : %d\n", __func__,
(_s2mu106_muic_get_vbus_state(muic_data) ? "High" : "Low"),
muic_data->adc, dev_to_str(muic_pdata->attached_dev),
det_ret);
wake_unlock(&muic_data->wake_lock);
mutex_unlock(&muic_data->muic_mutex);
return IRQ_HANDLED;
}
static irqreturn_t s2mu106_muic_adc_change_isr(int irq, void *data)
{
struct s2mu106_muic_data *muic_data = data;
struct muic_platform_data *muic_pdata;
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
struct muic_interface_t *muic_if;
#endif
if (muic_data == NULL) {
pr_err("%s data NULL\n", __func__);
return IRQ_NONE;
}
muic_pdata = muic_data->pdata;
if (muic_pdata == NULL) {
pr_err("%s data NULL\n", __func__);
return IRQ_NONE;
}
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
muic_if = muic_data->if_data;
if (muic_if == NULL) {
pr_err("%s data NULL\n", __func__);
return IRQ_NONE;
}
#endif
mutex_lock(&muic_data->muic_mutex);
wake_lock(&muic_data->wake_lock);
muic_data->adc = muic_pdata->adc = _s2mu106_muic_get_rid_adc(muic_data);
muic_data->vbvolt = _s2mu106_muic_get_vbus_state(muic_data);
pr_info("%s Vbus(%s), rid_adc(%#x), Type(%s), opmode : %s\n", __func__,
(muic_data->vbvolt ? "High" : "Low"),
muic_data->adc, dev_to_str(muic_pdata->attached_dev),
(muic_if->opmode ? "CCIC" : "MUIC"));
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
if (muic_if->opmode & OPMODE_CCIC) {
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
if ((IS_WATER_ADC(muic_data->adc)
|| (muic_data->adc & ADC_CONVERSION_ERR_MASK))
&& (!muic_data->vbvolt)
&& (muic_data->water_status == S2MU106_WATER_MUIC_IDLE)) {
muic_data->water_status = S2MU106_WATER_MUIC_VERIFY;
msleep(100);
muic_data->adc = s2mu106_muic_water_judge(muic_data);
muic_data->vbvolt = _s2mu106_muic_get_vbus_state(muic_data);
if (IS_WATER_ADC(muic_data->adc) && (!muic_data->vbvolt)) {
cancel_delayed_work(&muic_data->water_detect_handler);
schedule_delayed_work(&muic_data->water_detect_handler,
msecs_to_jiffies(0));
goto exit_adc_chg;
} else {
muic_data->water_status = S2MU106_WATER_MUIC_IDLE;
}
}
#endif
} else {
s2mu106_muic_detect_dev_rid_array(muic_data);
s2mu106_muic_handle_attached_dev(muic_data);
}
#endif
#if IS_ENABLED(CONFIG_MUIC_SUPPORT_TYPEB)
if (!MUIC_IS_ATTACHED(muic_pdata->attached_dev)) {
muic_data->new_dev = s2mu106_muic_detect_dev_rid_device(muic_data);
if (MUIC_IS_ATTACHED(muic_data->new_dev) &&
muic_data->new_dev != muic_pdata->attached_dev) {
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
muic_manager_set_legacy_dev(muic_pdata->muic_if, muic_data->new_dev);
#endif
muic_core_handle_attach(muic_pdata, muic_data->new_dev,
muic_pdata->adc, muic_pdata->vbvolt);
}
} else {
pr_info("%s, but (%s) is attached.\n",
__func__, dev_to_str(muic_pdata->attached_dev));
}
#endif
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
exit_adc_chg:
#endif
wake_unlock(&muic_data->wake_lock);
mutex_unlock(&muic_data->muic_mutex);
return IRQ_HANDLED;
}
#endif
static irqreturn_t s2mu106_muic_reserved_isr(int irq, void *data)
{
pr_info("%s irq_num(%d)\n", __func__, irq);
return IRQ_HANDLED;
}
static int s2mu106_muic_irq_init(struct s2mu106_muic_data *muic_data)
{
int ret = 0;
if (muic_data->mfd_pdata && (muic_data->mfd_pdata->irq_base > 0)) {
int irq_base = muic_data->mfd_pdata->irq_base;
/* request MUIC IRQ */
muic_data->irq_attach = irq_base + S2MU106_MUIC_IRQ1_ATTATCH;
REQUEST_IRQ(muic_data->irq_attach, muic_data,
"muic-attach", &s2mu106_muic_attach_isr);
muic_data->irq_detach = irq_base + S2MU106_MUIC_IRQ1_DETACH;
REQUEST_IRQ(muic_data->irq_detach, muic_data,
"muic-detach", &s2mu106_muic_detach_isr);
#if IS_ENABLED(CONFIG_MUIC_S2MU106_RID)
muic_data->irq_rid_chg = irq_base + S2MU106_MUIC_IRQ1_RID_CHG;
REQUEST_IRQ(muic_data->irq_rid_chg, muic_data,
"muic-rid_chg", &s2mu106_muic_rid_chg_isr);
#endif
muic_data->irq_vbus_on = irq_base + S2MU106_MUIC_IRQ2_VBUS_ON;
REQUEST_IRQ(muic_data->irq_vbus_on, muic_data,
"muic-vbus_on", &s2mu106_muic_vbus_on_isr);
muic_data->irq_rsvd_attach = irq_base + S2MU106_MUIC_IRQ2_RSVD_ATTACH;
REQUEST_IRQ(muic_data->irq_rsvd_attach, muic_data,
"muic-rsvd_attach", &s2mu106_muic_reserved_isr);
#if IS_ENABLED(CONFIG_MUIC_S2MU106_RID)
muic_data->irq_adc_change = irq_base + S2MU106_MUIC_IRQ2_ADC_CHANGE;
REQUEST_IRQ(muic_data->irq_adc_change, muic_data,
"muic-adc_change", &s2mu106_muic_adc_change_isr);
#endif
muic_data->irq_av_charge = irq_base + S2MU106_MUIC_IRQ2_AV_CHARGE;
REQUEST_IRQ(muic_data->irq_av_charge, muic_data,
"muic-av_charge", &s2mu106_muic_reserved_isr);
muic_data->irq_vbus_off = irq_base + S2MU106_MUIC_IRQ2_VBUS_OFF;
REQUEST_IRQ(muic_data->irq_vbus_off, muic_data,
"muic-vbus_off", &s2mu106_muic_vbus_off_isr);
}
pr_info("%s muic-attach(%d), muic-detach(%d), muic-rid_chg(%d), muic-vbus_on(%d)",
__func__, muic_data->irq_attach, muic_data->irq_detach, muic_data->irq_rid_chg,
muic_data->irq_vbus_on);
pr_info("muic-rsvd_attach(%d), muic-adc_change(%d), muic-av_charge(%d), muic-vbus_off(%d)\n",
muic_data->irq_rsvd_attach, muic_data->irq_adc_change,
muic_data->irq_av_charge, muic_data->irq_vbus_off);
return ret;
}
static void s2mu106_muic_free_irqs(struct s2mu106_muic_data *muic_data)
{
pr_info("%s\n", __func__);
/* free MUIC IRQ */
FREE_IRQ(muic_data->irq_attach, muic_data, "muic-attach");
FREE_IRQ(muic_data->irq_detach, muic_data, "muic-detach");
#if IS_ENABLED(CONFIG_MUIC_S2MU106_RID)
FREE_IRQ(muic_data->irq_rid_chg, muic_data, "muic-rid_chg");
#endif
FREE_IRQ(muic_data->irq_vbus_on, muic_data, "muic-vbus_on");
FREE_IRQ(muic_data->irq_rsvd_attach, muic_data, "muic-rsvd_attach");
#if IS_ENABLED(CONFIG_MUIC_S2MU106_RID)
FREE_IRQ(muic_data->irq_adc_change, muic_data, "muic-adc_change");
#endif
FREE_IRQ(muic_data->irq_av_charge, muic_data, "muic-av_charge");
FREE_IRQ(muic_data->irq_vbus_off, muic_data, "muic-vbus_off");
}
#if IS_ENABLED(CONFIG_OF)
static int of_s2mu106_muic_dt(struct device *dev,
struct s2mu106_muic_data *muic_data)
{
struct device_node *np, *np_muic;
int ret = 0;
np = dev->parent->of_node;
if (!np) {
pr_err("%s : could not find np\n", __func__);
return -ENODEV;
}
np_muic = of_find_node_by_name(np, "muic");
if (!np_muic) {
pr_err("%s : could not find muic sub-node np_muic\n", __func__);
return -EINVAL;
} else {
muic_data->vbus_discharging = of_get_named_gpio(np_muic,
"muic,discharging", 0);
if (muic_data->vbus_discharging < 0) {
dev_err(dev, "error reading vbus discharging gpio = %d\n",
muic_data->vbus_discharging);
muic_data->discharging_en = 0;
} else
muic_data->discharging_en = 1;
}
/* FIXME */
#if !IS_ENABLED(CONFIG_MUIC_UART_SWITCH)
if (of_gpio_count(np_muic) < 1) {
pr_err("%s : could not find muic gpio\n", __func__);
muic_data->pdata->gpio_uart_sel = 0;
} else
muic_data->pdata->gpio_uart_sel = of_get_gpio(np_muic, 0);
#else
muic_data->pdata->uart_addr =
(const char *)of_get_property(np_muic, "muic,uart_addr", NULL);
muic_data->pdata->uart_txd =
(const char *)of_get_property(np_muic, "muic,uart_txd", NULL);
muic_data->pdata->uart_rxd =
(const char *)of_get_property(np_muic, "muic,uart_rxd", NULL);
#endif
return ret;
}
#endif /* CONFIG_OF */
static void s2mu106_muic_init_drvdata(struct s2mu106_muic_data *muic_data,
struct s2mu106_dev *s2mu106,
struct platform_device *pdev,
struct s2mu106_platform_data *mfd_pdata)
{
/* save platfom data for gpio control functions */
muic_data->s2mu106_dev = s2mu106;
muic_data->dev = &pdev->dev;
muic_data->i2c = s2mu106->muic;
muic_data->mfd_pdata = mfd_pdata;
muic_data->afc_check = false;
muic_data->rescan_cnt = 0;
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
muic_data->water_status = S2MU106_WATER_MUIC_IDLE;
muic_data->water_dry_status = S2MU106_WATER_DRY_MUIC_IDLE;
muic_data->dry_chk_time = 0;
muic_data->dry_cnt = 0;
muic_data->dry_duration_sec = WATER_DRY_RETRY_INTERVAL_SEC;
muic_data->invalid_rescanned = false;
#if IS_ENABLED(CONFIG_HICCUP_CHARGER)
muic_data->is_hiccup_mode = false;
#endif
muic_data->is_cable_inserted = false;
#endif
muic_data->is_timeout_attached = false;
#if IS_ENABLED(CONFIG_S2MU106_IFCONN_HOUSE_NOT_GND)
muic_data->is_rescanning = false;
#endif
}
static void s2mu106_muic_init_interface(struct s2mu106_muic_data *muic_data,
struct muic_interface_t *muic_if)
{
struct muic_platform_data *muic_pdata = muic_data->pdata;
pr_info("%s, muic_if : 0x%p, muic_data : 0x%p\n",
__func__, muic_if, muic_data);
muic_if->muic_data = (void *)muic_data;
muic_if->set_com_to_open = s2mu106_if_com_to_open;
muic_if->set_switch_to_usb = s2mu106_if_com_to_usb;
muic_if->set_com_to_otg = s2mu106_if_com_to_usb;
muic_if->set_switch_to_uart = s2mu106_if_com_to_uart;
muic_if->get_vbus = s2mu106_if_get_vbus;
muic_if->set_gpio_uart_sel = s2mu106_if_set_gpio_uart_sel;
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
muic_if->set_cable_state = s2mu106_if_set_cable_state;
muic_if->set_dcd_rescan = s2mu106_if_cable_recheck;
#if IS_ENABLED(CONFIG_HV_MUIC_S2MU106_AFC)
muic_if->check_usb_killer = s2mu106_if_check_usb_killer;
#endif
#endif
muic_if->bcd_rescan = s2mu106_if_set_bcd_rescan_reg;
muic_if->control_rid_adc = s2mu106_if_control_rid_adc;
#if IS_ENABLED(CONFIG_MUIC_S2MU106_RID)
muic_if->get_adc = s2mu106_if_get_adc;
#endif
muic_if->set_jig_ctrl_on = s2mu106_if_set_jig_ctrl_on;
#if IS_ENABLED(CONFIG_MUIC_SYSFS)
muic_if->show_register = s2mu106_if_show_register;
#endif
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
muic_if->set_water_detect = s2mu106_if_set_water_det;
#if !IS_ENABLED(CONFIG_SEC_FACTORY)
muic_if->set_water_detect_from_boot = s2mu106_if_set_water_det_from_boot;
#endif
#endif
#if IS_ENABLED(CONFIG_HICCUP_CHARGER)
muic_if->set_hiccup_mode = s2mu106_if_set_hiccup_mode;
muic_if->get_hiccup_mode = s2mu106_if_get_hiccup_mode;
#endif
#if defined(CONFIG_MUIC_SUPPORT_PRSWAP)
muic_if->set_chg_det = s2mu106_if_set_chg_det;
muic_if->prswap_work = s2mu106_if_prswap_work;
#endif
muic_data->if_data = muic_if;
muic_pdata->muic_if = muic_if;
}
static int s2mu106_muic_probe(struct platform_device *pdev)
{
struct s2mu106_dev *s2mu106 = dev_get_drvdata(pdev->dev.parent);
struct s2mu106_platform_data *mfd_pdata = dev_get_platdata(s2mu106->dev);
struct s2mu106_muic_data *muic_data;
struct muic_platform_data *muic_pdata;
struct muic_interface_t *muic_if;
int ret = 0;
pr_info("%s start\n", __func__);
muic_data = devm_kzalloc(&pdev->dev, sizeof(*muic_data), GFP_KERNEL);
if (unlikely(!muic_data)) {
pr_err("%s out of memory\n", __func__);
ret = -ENOMEM;
goto err_return;
}
if (unlikely(!mfd_pdata)) {
pr_err("%s failed to get s2mu106 mfd platform data\n",
__func__);
ret = -ENOMEM;
goto err_kfree1;
}
muic_pdata = muic_core_init(muic_data);
if (unlikely(!muic_pdata))
goto err_kfree1;
muic_data->pdata = muic_pdata;
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
muic_if = muic_manager_init(muic_pdata, muic_data);
#else
muic_if = kzalloc(sizeof(*muic_if), GFP_KERNEL);
#endif
if (!muic_if) {
pr_err("%s failed to init muic manager, ret : 0x%X\n",
__func__, ret);
goto err_init_if;
}
s2mu106_muic_init_interface(muic_data, muic_if);
s2mu106_muic_init_drvdata(muic_data, s2mu106, pdev, mfd_pdata);
#if IS_ENABLED(CONFIG_OF)
ret = of_s2mu106_muic_dt(&pdev->dev, muic_data);
if (ret < 0)
pr_err("no muic dt! ret[%d]\n", ret);
#endif /* CONFIG_OF */
if (muic_data->discharging_en)
gpio_request(muic_data->vbus_discharging, "s2mu106-discharging");
muic_data->discharging = 0;
mutex_init(&muic_data->muic_mutex);
mutex_init(&muic_data->switch_mutex);
mutex_init(&muic_data->bcd_rescan_mutex);
wake_lock_init(&muic_data->wake_lock, WAKE_LOCK_SUSPEND, "muic_wake");
platform_set_drvdata(pdev, muic_data);
if (muic_data->pdata->init_gpio_cb)
ret = muic_data->pdata->init_gpio_cb(muic_data->pdata, get_switch_sel());
if (ret) {
pr_err("%s failed to init gpio(%d)\n", __func__, ret);
goto fail_init_gpio;
}
pr_info("%s: usb_path(%d), uart_path(%d)\n", __func__,
muic_pdata->usb_path, muic_pdata->uart_path);
#if IS_ENABLED(CONFIG_MUIC_SYSFS)
ret = muic_sysfs_init(muic_pdata);
if (ret) {
pr_err("failed to create sysfs\n");
goto fail_init_sysfs;
}
#endif
#if IS_ENABLED(CONFIG_HV_MUIC_S2MU106_AFC)
ret = s2mu106_hv_muic_init(muic_data);
if (ret) {
pr_err("failed to init hv-muic(%d)\n", ret);
goto fail;
}
#endif /* CONFIG_HV_MUIC_S2MU106_AFC */
ret = s2mu106_muic_reg_init(muic_data);
if (ret) {
pr_err("failed to init muic(%d)\n", ret);
goto fail;
}
if (muic_pdata->is_rustproof) {
pr_err("%s rustproof is enabled\n", __func__);
ret = _s2mu106_muic_sel_path(muic_data, S2MU106_PATH_OPEN);
}
#if IS_ENABLED(CONFIG_HV_MUIC_S2MU106_AFC)
if (get_afc_mode() == CH_MODE_AFC_DISABLE_VAL) {
pr_info("AFC mode disabled\n");
muic_data->pdata->afc_disable = true;
} else {
pr_info("AFC mode enabled\n");
muic_data->pdata->afc_disable = false;
}
#endif /* CONFIG_HV_MUIC_S2MU106_AFC */
pr_info("%s muic_if->opmode(%d)\n", __func__, muic_if->opmode);
muic_data->discharging_wq = alloc_workqueue("s2mu106_muic", WQ_MEM_RECLAIM, 1);
if (!muic_data->discharging_wq) {
pr_err("%s, fail to create workqueue\n", __func__);
goto fail;
}
muic_data->discharging_start_wq = alloc_workqueue("s2mu106_muic", WQ_MEM_RECLAIM, 1);
if (!muic_data->discharging_start_wq) {
pr_err("%s, fail to create workqueue\n", __func__);
goto fail;
}
INIT_DELAYED_WORK(&muic_data->discharging_handler, s2mu106_muic_discharging_handler);
INIT_DELAYED_WORK(&muic_data->discharging_start_handler, s2mu106_muic_discharging_start_handler);
INIT_DELAYED_WORK(&muic_data->dcd_recheck, s2mu106_muic_dcd_recheck);
INIT_DELAYED_WORK(&muic_data->rescan_validity_checker,
s2mu106_muic_rescan_validity_checker);
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
init_waitqueue_head(&muic_data->wait);
init_waitqueue_head(&muic_data->cable_wait);
INIT_DELAYED_WORK(&muic_data->water_dry_handler,
s2mu106_muic_water_dry_handler);
INIT_DELAYED_WORK(&muic_data->water_detect_handler,
s2mu106_muic_water_detect_handler);
INIT_DELAYED_WORK(&muic_data->sleep_dry_checker,
s2mu106_muic_sleep_dry_checker);
mutex_init(&muic_data->water_det_mutex);
mutex_init(&muic_data->water_dry_mutex);
wake_lock_init(&muic_data->water_wake_lock,
WAKE_LOCK_SUSPEND, "muic_water_wake");
wake_lock_init(&muic_data->water_dry_wake_lock,
WAKE_LOCK_SUSPEND, "muic_water_dry_wake");
muic_data->fb_notifier.priority = -1;
muic_data->fb_notifier.notifier_call = muic_fb_notifier_event;
fb_register_client(&muic_data->fb_notifier);
#endif
#if defined(CONFIG_MUIC_SUPPORT_POWERMETER)
ret = muic_manager_psy_init(muic_if, &pdev->dev);
if (ret) {
pr_err("%s failed to init psy(%d)\n", __func__, ret);
}
#endif
ret = s2mu106_muic_irq_init(muic_data);
if (ret) {
pr_err("%s failed to init irq(%d)\n", __func__, ret);
goto fail_init_irq;
}
if (muic_if->opmode == OPMODE_MUIC) {
s2mu106_muic_adc_change_isr(-1, muic_data);
} else {
s2mu106_muic_get_detect_info(muic_data);
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
/* Initial Check */
if (muic_data->rescan_cnt == 0
&& muic_data->vbvolt
&& muic_data->adc == ADC_GND
&& !muic_core_get_ccic_cable_state(muic_pdata)
&& !IS_WATER_STATUS(muic_data->water_status)) {
/* In case of normal charger cable */
s2mu106_muic_handle_cable_inserted(muic_data);
s2mu106_muic_bcd_rescan(muic_data);
} else if (!muic_data->is_cable_inserted
&& muic_data->adc == ADC_GND) {
/* In case of OTG */
s2mu106_muic_handle_cable_inserted(muic_data);
wake_up_interruptible(&muic_data->cable_wait);
} else if (muic_data->adc != ADC_GND
&& !_s2mu106_muic_get_vbus_state(muic_data)) {
/* In case of None Cable
* Need to check the water.
*/
muic_data->adc = _s2mu106_muic_recheck_adc(muic_data);
}
#else
s2mu106_muic_bcd_rescan(muic_data);
s2mu106_muic_attach_isr(-1, muic_data);
cancel_delayed_work(&muic_data->rescan_validity_checker);
schedule_delayed_work(&muic_data->rescan_validity_checker,
msecs_to_jiffies(1200));
#endif
}
return 0;
fail_init_irq:
fail:
#if IS_ENABLED(CONFIG_MUIC_SYSFS)
muic_sysfs_deinit(muic_pdata);
fail_init_sysfs:
#endif
fail_init_gpio:
mutex_destroy(&muic_data->muic_mutex);
err_init_if:
err_kfree1:
err_return:
return ret;
}
/* if need to set s2mu106 pdata */
static const struct of_device_id s2mu106_muic_match_table[] = {
{.compatible = "samsung,s2mu106-muic",},
{},
};
static int s2mu106_muic_remove(struct platform_device *pdev)
{
struct s2mu106_muic_data *muic_data = platform_get_drvdata(pdev);
if (muic_data) {
pr_info("%s\n", __func__);
#if IS_ENABLED(CONFIG_MUIC_SYSFS)
if (muic_data->pdata != NULL)
muic_sysfs_deinit(muic_data->pdata);
#endif
#if IS_ENABLED(CONFIG_MUIC_MANAGER)
if (muic_data->if_data != NULL)
muic_manager_exit(muic_data->if_data);
#else
kfree(muic_data->if_data);
#endif
muic_core_exit(muic_data->pdata);
disable_irq_wake(muic_data->i2c->irq);
s2mu106_muic_free_irqs(muic_data);
mutex_destroy(&muic_data->muic_mutex);
mutex_destroy(&muic_data->switch_mutex);
i2c_set_clientdata(muic_data->i2c, NULL);
}
return 0;
}
static void s2mu106_muic_shutdown(struct platform_device *pdev)
{
struct s2mu106_muic_data *muic_data = platform_get_drvdata(pdev);
int ret;
pr_info("%s\n", __func__);
if (!muic_data->i2c) {
pr_err("%s no muic i2c client\n", __func__);
return;
}
#if IS_ENABLED(CONFIG_HV_MUIC_S2MU106_AFC)
s2mu106_hv_muic_remove(muic_data);
#endif /* CONFIG_HV_MUIC_S2MU106_AFC */
ret = _s2mu106_muic_sel_path(muic_data, S2MU106_PATH_OPEN);
if (ret < 0)
pr_err("fail to open mansw\n");
/* set auto sw mode before shutdown to make sure device goes into */
/* LPM charging when TA or USB is connected during off state */
ret = _s2mu106_muic_set_path_mode(muic_data, S2MU106_MODE_AUTO);
if (ret < 0) {
pr_err("%s fail to update reg\n", __func__);
return;
}
#if defined(CONFIG_MUIC_SUPPORT_PRSWAP)
_s2mu106_muic_set_chg_det(muic_data, MUIC_ENABLE);
#endif
}
#if IS_ENABLED(CONFIG_PM)
static int s2mu106_muic_suspend(struct device *dev)
{
struct s2mu106_muic_data *muic_data = dev_get_drvdata(dev);
struct muic_platform_data *muic_pdata = muic_data->pdata;
muic_pdata->suspended = true;
#if IS_ENABLED(CONFIG_S2MU106_TYPEC_WATER)
if (muic_data->water_status == S2MU106_WATER_MUIC_CCIC_STABLE) {
cancel_delayed_work(&muic_data->sleep_dry_checker);
}
#endif
return 0;
}
static int s2mu106_muic_resume(struct device *dev)
{
struct s2mu106_muic_data *muic_data = dev_get_drvdata(dev);
struct muic_platform_data *muic_pdata = muic_data->pdata;
muic_pdata->suspended = false;
if (muic_pdata->need_to_noti) {
if (muic_pdata->attached_dev) {
MUIC_SEND_NOTI_ATTACH(muic_pdata->attached_dev);
} else {
MUIC_SEND_NOTI_DETACH(muic_pdata->attached_dev);
}
muic_pdata->need_to_noti = false;
}
return 0;
}
#else
#define s2mu106_muic_suspend NULL
#define s2mu106_muic_resume NULL
#endif
static SIMPLE_DEV_PM_OPS(s2mu106_muic_pm_ops, s2mu106_muic_suspend,
s2mu106_muic_resume);
static struct platform_driver s2mu106_muic_driver = {
.probe = s2mu106_muic_probe,
.remove = s2mu106_muic_remove,
.shutdown = s2mu106_muic_shutdown,
.driver = {
.name = "s2mu106-muic",
.owner = THIS_MODULE,
.of_match_table = s2mu106_muic_match_table,
#if IS_ENABLED(CONFIG_PM)
.pm = &s2mu106_muic_pm_ops,
#endif
},
};
static int __init s2mu106_muic_init(void)
{
return platform_driver_register(&s2mu106_muic_driver);
}
module_init(s2mu106_muic_init);
static void __exit s2mu106_muic_exit(void)
{
platform_driver_unregister(&s2mu106_muic_driver);
}
module_exit(s2mu106_muic_exit);
MODULE_DESCRIPTION("Samsung S2MU106 Micro USB IC driver");
MODULE_LICENSE("GPL");