blob: 9323e34623227843bf67bcf160fe238681e1ea6a [file] [log] [blame]
/*
* Copyrights (C) 2017 Samsung Electronics, Inc.
* Copyrights (C) 2017 Maxim Integrated Products, Inc.
*
* 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.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/mod_devicetable.h>
#include <linux/power_supply.h>
#include <linux/of.h>
#include <linux/irq.h>
#include <linux/gpio.h>
#include <linux/mfd/max77705-private.h>
#include <linux/platform_device.h>
#include <linux/ccic/max77705_usbc.h>
#if defined(CONFIG_BATTERY_NOTIFIER)
#include <linux/battery/battery_notifier.h>
#endif
#if defined(CONFIG_CCIC_NOTIFIER)
#include <linux/ccic/ccic_core.h>
#include <linux/ccic/ccic_notifier.h>
#include <linux/ccic/max77705_alternate.h>
#endif
#if defined(CONFIG_USB_HOST_NOTIFY)
#include <linux/usb_notify.h>
#endif
#if defined(CONFIG_DUAL_ROLE_USB_INTF)
#include <linux/usb/class-dual-role.h>
#elif defined(CONFIG_TYPEC)
#include <linux/usb/typec.h>
#endif
#if defined(CONFIG_USE_SECOND_MUIC)
#include <linux/muic/muic.h>
#endif
#include "../battery_v2/include/sec_charging_common.h"
extern struct pdic_notifier_struct pd_noti;
extern void (*fp_select_pdo)(int num);
#if defined(CONFIG_PDIC_PD30)
extern int (*fp_sec_pd_select_pps)(int num, int ppsVol, int ppsCur);
extern int (*fp_sec_pd_get_apdo_max_power)(unsigned int *pdo_pos, unsigned int *taMaxVol, unsigned int *taMaxCur, unsigned int *taMaxPwr);
#endif
#if defined(CONFIG_PDIC_PD30)
static void max77705_process_pd(struct max77705_usbc_platform_data *usbc_data)
{
if (usbc_data->pd_data->cc_sbu_short) {
pd_noti.sink_status.available_pdo_num = 1;
pd_noti.sink_status.power_list[1].max_current =
pd_noti.sink_status.power_list[1].max_current > 1800 ?
1800 : pd_noti.sink_status.power_list[1].max_current;
pd_noti.sink_status.has_apdo = false;
}
pr_info("%s : current_pdo_num(%d), available_pdo_num(%d), has_apdo(%d)\n", __func__,
pd_noti.sink_status.current_pdo_num, pd_noti.sink_status.available_pdo_num, pd_noti.sink_status.has_apdo);
max77705_ccic_event_work(usbc_data, CCIC_NOTIFY_DEV_BATTERY,
CCIC_NOTIFY_ID_POWER_STATUS, 1/*attach*/, 0, 0);
}
#else
static void max77705_process_pd(struct max77705_usbc_platform_data *usbc_data)
{
int i;
if (usbc_data->pd_data->cc_sbu_short) {
pd_noti.sink_status.available_pdo_num = 1;
pd_noti.sink_status.power_list[1].max_current =
pd_noti.sink_status.power_list[1].max_current > 1800 ?
1800 : pd_noti.sink_status.power_list[1].max_current;
}
pr_info("%s : CURRENT PDO NUM(%d), available_pdo_num[%d]",
__func__, pd_noti.sink_status.current_pdo_num,
pd_noti.sink_status.available_pdo_num);
for (i = 1; i <= pd_noti.sink_status.available_pdo_num; i++) {
pr_info("%s : PDO_Num[%d] MAX_CURR(%d) MAX_VOLT(%d)\n", __func__,
i, pd_noti.sink_status.power_list[i].max_current,
pd_noti.sink_status.power_list[i].max_voltage);
}
max77705_ccic_event_work(usbc_data, CCIC_NOTIFY_DEV_BATTERY,
CCIC_NOTIFY_ID_POWER_STATUS, 1/*attach*/, 0, 0);
}
#endif
void max77705_select_pdo(int num)
{
struct max77705_usbc_platform_data *pusbpd = pd_noti.pusbpd;
usbc_cmd_data value;
u8 temp;
init_usbc_cmd_data(&value);
pr_info("%s : NUM(%d)\n", __func__, num);
temp = num;
pd_noti.sink_status.selected_pdo_num = num;
if (pd_noti.event != PDIC_NOTIFY_EVENT_PD_SINK)
pd_noti.event = PDIC_NOTIFY_EVENT_PD_SINK;
if (pd_noti.sink_status.current_pdo_num == pd_noti.sink_status.selected_pdo_num) {
max77705_process_pd(pusbpd);
} else {
pusbpd->pn_flag = false;
value.opcode = OPCODE_SRCCAP_REQUEST;
value.write_data[0] = temp;
value.write_length = 1;
value.read_length = 1;
max77705_usbc_opcode_write(pusbpd, &value);
}
pr_info("%s : OPCODE(0x%02x) W_LENGTH(%d) R_LENGTH(%d) NUM(%d)\n",
__func__, value.opcode, value.write_length, value.read_length, num);
}
void max77705_response_pdo_request(struct max77705_usbc_platform_data *usbc_data,
unsigned char *data)
{
u8 result = data[1];
pr_info("%s: %s (0x%02X)\n", __func__, result ? "Error," : "Sent,", result);
switch (result) {
case 0x00:
pr_info("%s: Sent PDO Request Message to Port Partner(0x%02X)\n", __func__, result);
break;
case 0xFE:
pr_info("%s: Error, SinkTxNg(0x%02X)\n", __func__, result);
break;
case 0xFF:
pr_info("%s: Error, Not in SNK Ready State(0x%02X)\n", __func__, result);
break;
default:
break;
}
/* retry if the state of sink is not stable yet */
if (result == 0xFE || result == 0xFF) {
cancel_delayed_work(&usbc_data->pd_data->retry_work);
queue_delayed_work(usbc_data->pd_data->wqueue, &usbc_data->pd_data->retry_work, 0);
}
}
#if defined(CONFIG_PDIC_PD30)
void max77705_set_enable_pps(bool enable, int ppsVol, int ppsCur)
{
usbc_cmd_data value;
init_usbc_cmd_data(&value);
value.opcode = OPCODE_SET_PPS;
if (enable) {
value.write_data[0] = 0x1; //PPS_ON On
value.write_data[1] = (ppsVol / 20) & 0xFF; //Default Output Voltage (Low), 20mV
value.write_data[2] = ((ppsVol / 20) >> 8) & 0xFF; //Default Output Voltage (High), 20mV
value.write_data[3] = (ppsCur / 50) & 0x7F; //Default Operating Current, 50mA
value.write_length = 4;
value.read_length = 1;
pr_info("%s : PPS_On (Vol:%dmV, Cur:%dmA)\n", __func__, ppsVol, ppsCur);
} else {
value.write_data[0] = 0x0; //PPS_ON Off
value.write_length = 1;
value.read_length = 1;
pr_info("%s : PPS_Off\n", __func__);
}
max77705_usbc_opcode_write(pd_noti.pusbpd, &value);
}
void max77705_response_set_pps(struct max77705_usbc_platform_data *usbc_data,
unsigned char *data)
{
u8 result = data[1];
if (result & 0x01)
usbc_data->pd_data->bPPS_on = true;
else
usbc_data->pd_data->bPPS_on = false;
pr_info("%s : PPS_%s (0x%02X)\n",
__func__, usbc_data->pd_data->bPPS_on ? "On" : "Off", result);
}
void max77705_response_apdo_request(struct max77705_usbc_platform_data *usbc_data,
unsigned char *data)
{
u8 result = data[1];
pr_info("%s: %s (0x%02X)\n", __func__, result ? "Error," : "Sent,", result);
switch (result) {
case 0x00:
pr_info("%s: Sent APDO Request Message to Port Partner(0x%02X)\n", __func__, result);
break;
case 0x01:
pr_info("%s: Error, Invalid APDO position(0x%02X)\n", __func__, result);
break;
case 0x02:
pr_info("%s: Error, Invalid Output Voltage(0x%02X)\n", __func__, result);
break;
case 0x03:
pr_info("%s: Error, Invalid Operating Current(0x%02X)\n", __func__, result);
break;
case 0x04:
pr_info("%s: Error, PPS Function Off(0x%02X)\n", __func__, result);
break;
case 0x05:
pr_info("%s: Error, Not in SNK Ready State(0x%02X)\n", __func__, result);
break;
case 0x06:
pr_info("%s: Error, PD2.0 Contract(0x%02X)\n", __func__, result);
break;
case 0x07:
pr_info("%s: Error, SinkTxNg(0x%02X)\n", __func__, result);
break;
default:
break;
}
/* retry if the state of sink is not stable yet */
if (result == 0x05 || result == 0x07) {
cancel_delayed_work(&usbc_data->pd_data->retry_work);
queue_delayed_work(usbc_data->pd_data->wqueue, &usbc_data->pd_data->retry_work, 0);
}
}
int max77705_select_pps(int num, int ppsVol, int ppsCur)
{
struct max77705_usbc_platform_data *pusbpd = pd_noti.pusbpd;
usbc_cmd_data value;
/* [dchg] TODO: check more below option */
if (num > pd_noti.sink_status.available_pdo_num) {
pr_info("%s: request pdo num(%d) is higher taht available pdo.\n", __func__, num);
return -EINVAL;
}
if (!pd_noti.sink_status.power_list[num].apdo) {
pr_info("%s: request pdo num(%d) is not apdo.\n", __func__, num);
return -EINVAL;
} else
pd_noti.sink_status.selected_pdo_num = num;
if (ppsVol > pd_noti.sink_status.power_list[num].max_voltage) {
pr_info("%s: ppsVol is over(%d, max:%d)\n",
__func__, ppsVol, pd_noti.sink_status.power_list[num].max_voltage);
ppsVol = pd_noti.sink_status.power_list[num].max_voltage;
} else if (ppsVol < pd_noti.sink_status.power_list[num].min_voltage) {
pr_info("%s: ppsVol is under(%d, min:%d)\n",
__func__, ppsVol, pd_noti.sink_status.power_list[num].min_voltage);
ppsVol = pd_noti.sink_status.power_list[num].min_voltage;
}
if (ppsCur > pd_noti.sink_status.power_list[num].max_current) {
pr_info("%s: ppsCur is over(%d, max:%d)\n",
__func__, ppsCur, pd_noti.sink_status.power_list[num].max_current);
ppsCur = pd_noti.sink_status.power_list[num].max_current;
} else if (ppsCur < 0) {
pr_info("%s: ppsCur is under(%d, 0)\n",
__func__, ppsCur);
ppsCur = 0;
}
pd_noti.sink_status.pps_voltage = ppsVol;
pd_noti.sink_status.pps_current = ppsCur;
pr_info(" %s : PPS PDO(%d), voltage(%d), current(%d) is selected to change\n",
__func__, pd_noti.sink_status.selected_pdo_num, ppsVol, ppsCur);
init_usbc_cmd_data(&value);
pusbpd->pn_flag = false;
value.opcode = OPCODE_APDO_SRCCAP_REQUEST;
value.write_data[0] = (num & 0xFF); /* APDO Position */
value.write_data[1] = (ppsVol / 20) & 0xFF; /* Output Voltage(Low) */
value.write_data[2] = ((ppsVol / 20) >> 8) & 0xFF; /* Output Voltage(High) */
value.write_data[3] = (ppsCur / 50) & 0x7F; /* Operating Current */
value.write_length = 4;
value.read_length = 1; /* Result */
max77705_usbc_opcode_write(pusbpd, &value);
/* [dchg] TODO: add return value */
return 0;
}
int max77705_get_apdo_max_power(unsigned int *pdo_pos, unsigned int *taMaxVol, unsigned int *taMaxCur, unsigned int *taMaxPwr)
{
int i;
int ret = 0;
int max_current = 0, max_voltage = 0, max_power = 0;
if (!pd_noti.sink_status.has_apdo) {
pr_info("%s: pd don't have apdo\n", __func__);
return -1;
}
/* First, get TA maximum power from the fixed PDO */
for (i = 1; i <= pd_noti.sink_status.available_pdo_num; i++) {
if (!(pd_noti.sink_status.power_list[i].apdo)) {
max_voltage = pd_noti.sink_status.power_list[i].max_voltage;
max_current = pd_noti.sink_status.power_list[i].max_current;
max_power = (max_voltage * max_current > max_power) ? (max_voltage * max_current) : max_power;
*taMaxPwr = max_power; /* mW */
}
}
if (*pdo_pos == 0) {
/* Get the proper PDO */
for (i = 1; i <= pd_noti.sink_status.available_pdo_num; i++) {
if (pd_noti.sink_status.power_list[i].apdo) {
if (pd_noti.sink_status.power_list[i].max_voltage >= *taMaxVol) {
*pdo_pos = i;
*taMaxVol = pd_noti.sink_status.power_list[i].max_voltage;
*taMaxCur = pd_noti.sink_status.power_list[i].max_current;
break;
}
}
if (*pdo_pos)
break;
}
if (*pdo_pos == 0) {
pr_info("mv (%d) and ma (%d) out of range of APDO\n",
*taMaxVol, *taMaxCur);
ret = -EINVAL;
}
} else {
/* If we already have pdo object position, we don't need to search max current */
ret = -ENOTSUPP;
}
if (!ret)
max77705_set_enable_pps(true, 5000, *taMaxCur); /* request as default 5V when enable first */
else
max77705_set_enable_pps(false, 0, 0);
pr_info("%s : *pdo_pos(%d), *taMaxVol(%d), *maxCur(%d), *maxPwr(%d)\n",
__func__, *pdo_pos, *taMaxVol, *taMaxCur, *taMaxPwr);
return ret;
}
#endif
void max77705_pd_retry_work(struct work_struct *work)
{
struct max77705_usbc_platform_data *pusbpd = pd_noti.pusbpd;
usbc_cmd_data value;
u8 num;
if (pd_noti.event == PDIC_NOTIFY_EVENT_DETACH)
return;
init_usbc_cmd_data(&value);
num = pd_noti.sink_status.selected_pdo_num;
pr_info("%s : latest selected_pdo_num(%d)\n", __func__, num);
pusbpd->pn_flag = false;
#if defined(CONFIG_PDIC_PD30)
if (pd_noti.sink_status.power_list[num].apdo) {
value.opcode = OPCODE_APDO_SRCCAP_REQUEST;
value.write_data[0] = (num & 0xFF); /* APDO Position */
value.write_data[1] = (pd_noti.sink_status.pps_voltage / 20) & 0xFF; /* Output Voltage(Low) */
value.write_data[2] = ((pd_noti.sink_status.pps_voltage / 20) >> 8) & 0xFF; /* Output Voltage(High) */
value.write_data[3] = (pd_noti.sink_status.pps_current / 50) & 0x7F; /* Operating Current */
value.write_length = 4;
value.read_length = 1; /* Result */
max77705_usbc_opcode_write(pusbpd, &value);
} else {
value.opcode = OPCODE_SRCCAP_REQUEST;
value.write_data[0] = num;
value.write_length = 1;
value.read_length = 1;
max77705_usbc_opcode_write(pusbpd, &value);
}
#else
value.opcode = OPCODE_SRCCAP_REQUEST;
value.write_data[0] = num;
value.write_length = 1;
value.read_length = 1;
max77705_usbc_opcode_write(pusbpd, &value);
#endif
pr_info("%s : OPCODE(0x%02x) W_LENGTH(%d) R_LENGTH(%d) NUM(%d)\n",
__func__, value.opcode, value.write_length, value.read_length, num);
}
void max77705_usbc_icurr(u8 curr)
{
usbc_cmd_data value;
init_usbc_cmd_data(&value);
value.opcode = OPCODE_CHGIN_ILIM_W;
value.write_data[0] = curr;
value.write_length = 1;
value.read_length = 0;
max77705_usbc_opcode_write(pd_noti.pusbpd, &value);
pr_info("%s : OPCODE(0x%02x) W_LENGTH(%d) R_LENGTH(%d) USBC_ILIM(0x%x)\n",
__func__, value.opcode, value.write_length, value.read_length, curr);
}
void max77705_set_gpio5_control(int direction, int output)
{
struct max77705_usbc_platform_data *pusbpd = pd_noti.pusbpd;
usbc_cmd_data value;
u8 op_data1, op_data2;
if (direction)
op_data1 = 0xFF; // OUTPUT
else
op_data1 = 0x00; // INPUT
if (output)
op_data2 = 0xFF; // HIGH
else
op_data2 = 0x00; // LOW
init_usbc_cmd_data(&value);
value.opcode = OPCODE_SAMSUNG_GPIO5_CONTROL;
value.write_data[0] = op_data1;
value.write_data[1] = op_data2;
value.write_length = 2;
value.read_length = 0;
max77705_usbc_opcode_write(pusbpd, &value);
pr_info("%s : OPCODE(0x%02x) W_LENGTH(%d) R_LENGTH(%d) gpio5(0x%x, 0x%x)\n",
__func__, value.opcode, value.write_length, value.read_length, op_data1, op_data2);
}
void max77705_set_fw_noautoibus(int enable)
{
struct max77705_usbc_platform_data *pusbpd = pd_noti.pusbpd;
usbc_cmd_data value;
u8 op_data;
switch (enable) {
case MAX77705_AUTOIBUS_FW_AT_OFF:
op_data = 0x03; /* usbc fw off & auto off(manual on) */
break;
case MAX77705_AUTOIBUS_FW_OFF:
op_data = 0x02; /* usbc fw off & auto on(manual off) */
break;
case MAX77705_AUTOIBUS_AT_OFF:
op_data = 0x01; /* usbc fw on & auto off(manual on) */
break;
case MAX77705_AUTOIBUS_ON:
default:
op_data = 0x00; /* usbc fw on & auto on(manual off) */
break;
}
init_usbc_cmd_data(&value);
value.opcode = OPCODE_SAMSUNG_FW_AUTOIBUS;
value.write_data[0] = op_data;
value.write_length = 1;
value.read_length = 0;
max77705_usbc_opcode_write(pusbpd, &value);
pr_info("%s : OPCODE(0x%02x) W_LENGTH(%d) R_LENGTH(%d) AUTOIBUS(0x%x)\n",
__func__, value.opcode, value.write_length, value.read_length, op_data);
}
void max77705_vbus_turn_on_ctrl(struct max77705_usbc_platform_data *usbc_data, bool enable, bool swaped)
{
struct power_supply *psy_otg;
union power_supply_propval val;
int on = !!enable;
int ret = 0;
int count = 5;
#if defined(CONFIG_USB_HOST_NOTIFY)
struct otg_notify *o_notify = get_otg_notify();
bool must_block_host = is_blocked(o_notify, NOTIFY_BLOCK_TYPE_HOST);
pr_info("%s : enable=%d, auto_vbus_en=%d, must_block_host=%d, swaped=%d\n",
__func__, enable, usbc_data->auto_vbus_en, must_block_host, swaped);
// turn on
if (enable) {
// auto-mode
if (usbc_data->auto_vbus_en) {
// mpsm
if (must_block_host) {
if (swaped) {
// turn off vbus because of swaped and blocked host
enable = false;
pr_info("%s : turn off vbus because of blocked host\n",
__func__);
} else {
enable = false;
pr_info("%s : turn off vbus because of blocked host\n",
__func__);
}
} else {
// don't turn on because of auto-mode
return;
}
// non auto-mode
} else {
if (must_block_host) {
if (swaped) {
enable = false;
pr_info("%s : turn off vbus because of blocked host\n",
__func__);
} else {
enable = false;
pr_info("%s : turn off vbus because of blocked host\n",
__func__);
}
}
}
// turn off
} else {
// don't turn off because of auto-mode or blocked (already off)
if (usbc_data->auto_vbus_en || must_block_host)
return;
}
#endif
pr_info("%s : enable=%d\n", __func__, enable);
while (count) {
psy_otg = power_supply_get_by_name("otg");
if (psy_otg) {
val.intval = enable;
#if defined(CONFIG_USE_SECOND_MUIC)
muic_hv_charger_disable(enable);
#endif
ret = psy_otg->desc->set_property(psy_otg, POWER_SUPPLY_PROP_ONLINE, &val);
if (ret == -ENODEV) {
pr_err("%s: fail to set power_suppy ONLINE property %d) retry (%d)\n",__func__, ret, count);
count--;
} else {
if (ret) {
pr_err("%s: fail to set power_suppy ONLINE property(%d) \n",__func__, ret);
} else {
pr_info("otg accessory power = %d\n", on);
}
break;
}
} else {
pr_err("%s: fail to get psy battery\n", __func__);
count--;
msleep(200);
}
}
}
void max77705_pdo_list(struct max77705_usbc_platform_data *usbc_data, unsigned char *data)
{
u8 temp = 0x00;
int i;
bool do_power_nego = false;
pd_noti.event = PDIC_NOTIFY_EVENT_PD_SINK;
temp = (data[1] >> 5);
#if defined(CONFIG_BATTERY_NOTIFIER)
if (temp > MAX_PDO_NUM) {
pr_info("%s : update available_pdo_num[%d -> %d]",
__func__, temp, MAX_PDO_NUM);
temp = MAX_PDO_NUM;
}
#endif
pd_noti.sink_status.available_pdo_num = temp;
pr_info("%s : Temp[0x%02x] Data[0x%02x] available_pdo_num[%d]\n",
__func__, temp, data[1], pd_noti.sink_status.available_pdo_num);
for (i = 0; i < temp; i++) {
u32 pdo_temp;
int max_current = 0, max_voltage = 0;
pdo_temp = (data[2 + (i * 4)]
| (data[3 + (i * 4)] << 8)
| (data[4 + (i * 4)] << 16)
| (data[5 + (i * 4)] << 24));
pr_info("%s : PDO[%d] = 0x%x\n", __func__, i, pdo_temp);
max_current = (0x3FF & pdo_temp);
max_voltage = (0x3FF & (pdo_temp >> 10));
if (!(do_power_nego) &&
(pd_noti.sink_status.power_list[i + 1].max_current != max_current * UNIT_FOR_CURRENT ||
pd_noti.sink_status.power_list[i + 1].max_voltage != max_voltage * UNIT_FOR_VOLTAGE))
do_power_nego = true;
pd_noti.sink_status.power_list[i + 1].max_current = max_current * UNIT_FOR_CURRENT;
pd_noti.sink_status.power_list[i + 1].max_voltage = max_voltage * UNIT_FOR_VOLTAGE;
pr_info("%s : PDO_Num[%d] MAX_CURR(%d) MAX_VOLT(%d), AVAILABLE_PDO_Num(%d)\n", __func__,
i, pd_noti.sink_status.power_list[i + 1].max_current,
pd_noti.sink_status.power_list[i + 1].max_voltage,
pd_noti.sink_status.available_pdo_num);
}
if (usbc_data->pd_data->pdo_list && do_power_nego) {
pr_info("%s : PDO list is changed, so power negotiation is need\n",
__func__, pd_noti.sink_status.selected_pdo_num);
pd_noti.sink_status.selected_pdo_num = 0;
pd_noti.event = PDIC_NOTIFY_EVENT_PD_SINK_CAP;
}
if (pd_noti.sink_status.current_pdo_num != pd_noti.sink_status.selected_pdo_num) {
if (pd_noti.sink_status.selected_pdo_num == 0)
pr_info("%s : PDO is not selected yet by default\n", __func__);
}
usbc_data->pd_data->pdo_list = true;
max77705_process_pd(usbc_data);
}
#if defined(CONFIG_PDIC_PD30)
void max77705_current_pdo(struct max77705_usbc_platform_data *usbc_data, unsigned char *data)
{
u8 sel_pdo_pos = 0x00, num_of_pdo = 0x00;
int i, available_pdo_num = 0;
bool do_power_nego = false;
U_SEC_PDO_OBJECT pdo_obj;
POWER_LIST* pPower_list;
POWER_LIST prev_power_list;
if (!pd_noti.sink_status.available_pdo_num)
do_power_nego = true;
sel_pdo_pos = ((data[1] >> 3) & 0x07);
pd_noti.sink_status.current_pdo_num = sel_pdo_pos;
num_of_pdo = (data[1] & 0x07);
if (num_of_pdo > MAX_PDO_NUM) {
pr_info("%s : update available_pdo_num[%d -> %d]",
__func__, num_of_pdo, MAX_PDO_NUM);
num_of_pdo = MAX_PDO_NUM;
}
pd_noti.sink_status.has_apdo = false;
for (i = 0; i < num_of_pdo; ++i) {
pPower_list = &pd_noti.sink_status.power_list[available_pdo_num + 1];
pdo_obj.data = (data[2 + (i * 4)]
| (data[3 + (i * 4)] << 8)
| (data[4 + (i * 4)] << 16)
| (data[5 + (i * 4)] << 24));
if (!do_power_nego)
prev_power_list = pd_noti.sink_status.power_list[available_pdo_num + 1];
switch (pdo_obj.BITS_supply.type) {
case PDO_TYPE_FIXED:
pPower_list->apdo = false;
pPower_list->max_voltage = pdo_obj.BITS_pdo_fixed.voltage * UNIT_FOR_VOLTAGE;
pPower_list->min_voltage = 0;
pPower_list->max_current = pdo_obj.BITS_pdo_fixed.max_current * UNIT_FOR_CURRENT;
if (pPower_list->max_voltage > AVAILABLE_VOLTAGE)
pPower_list->accept = false;
else
pPower_list->accept = true;
available_pdo_num++;
break;
case PDO_TYPE_APDO:
pd_noti.sink_status.has_apdo = true;
available_pdo_num++;
pPower_list->apdo = true;
pPower_list->max_voltage = pdo_obj.BITS_pdo_programmable.max_voltage * UNIT_FOR_APDO_VOLTAGE;
pPower_list->min_voltage = pdo_obj.BITS_pdo_programmable.min_voltage * UNIT_FOR_APDO_VOLTAGE;
pPower_list->max_current = pdo_obj.BITS_pdo_programmable.max_current * UNIT_FOR_APDO_CURRENT;
pPower_list->accept = true;
break;
case PDO_TYPE_BATTERY:
case PDO_TYPE_VARIABLE:
default:
break;
}
if (!(do_power_nego) &&
(pPower_list->max_current != prev_power_list.max_current ||
pPower_list->max_voltage != prev_power_list.max_voltage ||
pPower_list->min_voltage != prev_power_list.min_voltage))
do_power_nego = true;
}
if (!do_power_nego && (pd_noti.sink_status.available_pdo_num != available_pdo_num))
do_power_nego = true;
pd_noti.sink_status.available_pdo_num = available_pdo_num;
pr_info("%s : current_pdo_num(%d), available_pdo_num(%d/%d)\n", __func__,
pd_noti.sink_status.current_pdo_num, pd_noti.sink_status.available_pdo_num, num_of_pdo);
pd_noti.event = PDIC_NOTIFY_EVENT_PD_SINK;
if (usbc_data->pd_data->pdo_list && do_power_nego) {
pr_info("%s : PDO list is changed, so power negotiation is need\n",
__func__, pd_noti.sink_status.selected_pdo_num);
pd_noti.sink_status.selected_pdo_num = 0;
pd_noti.event = PDIC_NOTIFY_EVENT_PD_SINK_CAP;
}
if (pd_noti.sink_status.current_pdo_num != pd_noti.sink_status.selected_pdo_num) {
if (pd_noti.sink_status.selected_pdo_num == 0)
pr_info("%s : PDO is not selected yet by default\n", __func__);
}
if (do_power_nego || pd_noti.sink_status.selected_pdo_num == 0) {
for (i = 0; i < num_of_pdo; ++i) {
pdo_obj.data = (data[2 + (i * 4)]
| (data[3 + (i * 4)] << 8)
| (data[4 + (i * 4)] << 16)
| (data[5 + (i * 4)] << 24));
pr_info("%s : PDO[%d] = 0x%08X\n", __func__, i + 1, pdo_obj.data);
}
for (i = 0; i < pd_noti.sink_status.available_pdo_num; ++i) {
pPower_list = &pd_noti.sink_status.power_list[i + 1];
pr_info("%s : PDO[%d,%s,%s] max_vol(%dmV),min_vol(%dmV),max_cur(%dmA)\n",
__func__, i + 1,
pPower_list->apdo ? "APDO" : "FIXED", pPower_list->accept ? "O" : "X",
pPower_list->max_voltage, pPower_list->min_voltage, pPower_list->max_current);
}
}
usbc_data->pd_data->pdo_list = true;
max77705_process_pd(usbc_data);
}
#else
void max77705_current_pdo(struct max77705_usbc_platform_data *usbc_data, unsigned char *data)
{
u8 temp = 0x00;
int i;
bool do_power_nego = false;
pd_noti.event = PDIC_NOTIFY_EVENT_PD_SINK;
temp = ((data[1] >> 3) & 0x07);
pd_noti.sink_status.current_pdo_num = temp;
pr_info("%s : CURRENT PDO NUM(%d)\n",
__func__, pd_noti.sink_status.current_pdo_num);
temp = (data[1] & 0x07);
#if defined(CONFIG_BATTERY_NOTIFIER)
if (temp > MAX_PDO_NUM) {
pr_info("%s : update available_pdo_num[%d -> %d]",
__func__, temp, MAX_PDO_NUM);
temp = MAX_PDO_NUM;
}
#endif
pd_noti.sink_status.available_pdo_num = temp;
pr_info("%s : Temp[0x%02x] Data[0x%02x] available_pdo_num[%d]\n",
__func__, temp, data[1], pd_noti.sink_status.available_pdo_num);
for (i = 0; i < temp; i++) {
u32 pdo_temp;
int max_current = 0, max_voltage = 0;
pdo_temp = (data[2 + (i * 4)]
| (data[3 + (i * 4)] << 8)
| (data[4 + (i * 4)] << 16)
| (data[5 + (i * 4)] << 24));
pr_info("%s : PDO[%d] = 0x%x\n", __func__, i, pdo_temp);
max_current = (0x3FF & pdo_temp);
max_voltage = (0x3FF & (pdo_temp >> 10));
if (!(do_power_nego) &&
(pd_noti.sink_status.power_list[i + 1].max_current != max_current * UNIT_FOR_CURRENT ||
pd_noti.sink_status.power_list[i + 1].max_voltage != max_voltage * UNIT_FOR_VOLTAGE))
do_power_nego = true;
pd_noti.sink_status.power_list[i + 1].max_current = max_current * UNIT_FOR_CURRENT;
pd_noti.sink_status.power_list[i + 1].max_voltage = max_voltage * UNIT_FOR_VOLTAGE;
pr_info("%s : PDO_Num[%d] MAX_CURR(%d) MAX_VOLT(%d), AVAILABLE_PDO_Num(%d)\n", __func__,
i, pd_noti.sink_status.power_list[i + 1].max_current,
pd_noti.sink_status.power_list[i + 1].max_voltage,
pd_noti.sink_status.available_pdo_num);
}
if (usbc_data->pd_data->pdo_list && do_power_nego) {
pr_info("%s : PDO list is changed, so power negotiation is need\n",
__func__, pd_noti.sink_status.selected_pdo_num);
pd_noti.sink_status.selected_pdo_num = 0;
pd_noti.event = PDIC_NOTIFY_EVENT_PD_SINK_CAP;
}
if (pd_noti.sink_status.current_pdo_num != pd_noti.sink_status.selected_pdo_num) {
if (pd_noti.sink_status.selected_pdo_num == 0)
pr_info("%s : PDO is not selected yet by default\n", __func__);
}
usbc_data->pd_data->pdo_list = true;
max77705_process_pd(usbc_data);
}
#endif
void max77705_detach_pd(struct max77705_usbc_platform_data *usbc_data)
{
pr_info("%s : Detach PD CHARGER\n", __func__);
if (pd_noti.event != PDIC_NOTIFY_EVENT_DETACH) {
cancel_delayed_work(&usbc_data->pd_data->retry_work);
#if defined(CONFIG_PDIC_PD30)
if (pd_noti.sink_status.available_pdo_num)
memset(pd_noti.sink_status.power_list, 0, (sizeof(POWER_LIST) * (MAX_PDO_NUM + 1)));
#endif
pd_noti.sink_status.rp_currentlvl = RP_CURRENT_LEVEL_NONE;
pd_noti.sink_status.selected_pdo_num = 0;
pd_noti.sink_status.available_pdo_num = 0;
pd_noti.sink_status.current_pdo_num = 0;
#if defined(CONFIG_PDIC_PD30)
pd_noti.sink_status.pps_voltage = 0;
pd_noti.sink_status.pps_current = 0;
pd_noti.sink_status.request_apdo = false;
pd_noti.sink_status.has_apdo = false;
max77705_set_enable_pps(false, 0, 0);
#endif
pd_noti.event = PDIC_NOTIFY_EVENT_DETACH;
usbc_data->pd_data->psrdy_received = false;
usbc_data->pd_data->pdo_list = false;
usbc_data->pd_data->cc_sbu_short = false;
max77705_ccic_event_work(usbc_data, CCIC_NOTIFY_DEV_BATTERY,
CCIC_NOTIFY_ID_POWER_STATUS, 0/*attach*/, 0, 0);
}
}
static void max77705_notify_prswap(struct max77705_usbc_platform_data *usbc_data, u8 pd_msg)
{
pr_info("%s : PR SWAP pd_msg [%x]\n", __func__, pd_msg);
switch(pd_msg) {
case PRSWAP_SNKTOSWAP:
pd_noti.event = PDIC_NOTIFY_EVENT_PD_PRSWAP_SNKTOSRC;
pd_noti.sink_status.selected_pdo_num = 0;
pd_noti.sink_status.available_pdo_num = 0;
pd_noti.sink_status.current_pdo_num = 0;
usbc_data->pd_data->psrdy_received = false;
usbc_data->pd_data->pdo_list = false;
usbc_data->pd_data->cc_sbu_short = false;
max77705_ccic_event_work(usbc_data, CCIC_NOTIFY_DEV_BATTERY,
CCIC_NOTIFY_ID_POWER_STATUS, 0/*attach*/, 0, 0);
break;
default:
break;
}
}
void max77705_check_pdo(struct max77705_usbc_platform_data *usbc_data)
{
usbc_cmd_data value;
init_usbc_cmd_data(&value);
value.opcode = OPCODE_CURRENT_SRCCAP;
value.write_length = 0x0;
value.read_length = 31;
max77705_usbc_opcode_read(usbc_data, &value);
pr_info("%s : OPCODE(0x%02x) W_LENGTH(%d) R_LENGTH(%d)\n",
__func__, value.opcode, value.write_length, value.read_length);
}
void max77705_notify_rp_current_level(struct max77705_usbc_platform_data *usbc_data)
{
unsigned int rp_currentlvl;
switch (usbc_data->cc_data->ccistat) {
case CCI_500mA:
rp_currentlvl = RP_CURRENT_LEVEL_DEFAULT;
break;
case CCI_1_5A:
rp_currentlvl = RP_CURRENT_LEVEL2;
break;
case CCI_3_0A:
rp_currentlvl = RP_CURRENT_LEVEL3;
break;
case CCI_SHORT:
rp_currentlvl = RP_CURRENT_ABNORMAL;
break;
default:
rp_currentlvl = RP_CURRENT_LEVEL_NONE;
break;
}
if (usbc_data->plug_attach_done && !usbc_data->pd_data->psrdy_received &&
usbc_data->cc_data->current_pr == SNK &&
usbc_data->pd_state == max77705_State_PE_SNK_Wait_for_Capabilities &&
rp_currentlvl != pd_noti.sink_status.rp_currentlvl &&
rp_currentlvl >= RP_CURRENT_LEVEL_DEFAULT) {
pd_noti.sink_status.rp_currentlvl = rp_currentlvl;
pd_noti.event = PDIC_NOTIFY_EVENT_CCIC_ATTACH;
pr_info("%s : rp_currentlvl(%d)\n", __func__, pd_noti.sink_status.rp_currentlvl);
max77705_ccic_event_work(usbc_data, CCIC_NOTIFY_DEV_BATTERY,
CCIC_NOTIFY_ID_POWER_STATUS, 0/*attach*/, 0, 0);
}
}
static void max77705_pd_check_pdmsg(struct max77705_usbc_platform_data *usbc_data, u8 pd_msg)
{
struct power_supply *psy_charger;
union power_supply_propval val;
usbc_cmd_data value;
/*int dr_swap, pr_swap, vcon_swap = 0; u8 value[2], rc = 0;*/
MAX77705_VDM_MSG_IRQ_STATUS_Type VDM_MSG_IRQ_State;
#ifdef CONFIG_USB_NOTIFY_PROC_LOG
int event;
#endif
VDM_MSG_IRQ_State.DATA = 0x0;
init_usbc_cmd_data(&value);
msg_maxim(" pd_msg [%x]", pd_msg);
switch (pd_msg) {
case Nothing_happened:
break;
case Sink_PD_PSRdy_received:
/* currently, do nothing
* calling max77705_check_pdo() has been moved to max77705_psrdy_irq()
* for specific PD charger issue
*/
break;
case Sink_PD_Error_Recovery:
break;
case Sink_PD_SenderResponseTimer_Timeout:
msg_maxim("Sink_PD_SenderResponseTimer_Timeout received.");
/* queue_work(usbc_data->op_send_queue, &usbc_data->op_send_work); */
break;
case Source_PD_PSRdy_Sent:
if (usbc_data->mpsm_mode && (usbc_data->pd_pr_swap == cc_SOURCE)) {
max77705_usbc_disable_auto_vbus(usbc_data);
max77705_vbus_turn_on_ctrl(usbc_data, OFF, false);
}
break;
case Source_PD_Error_Recovery:
break;
case Source_PD_SenderResponseTimer_Timeout:
max77705_vbus_turn_on_ctrl(usbc_data, OFF, false);
schedule_delayed_work(&usbc_data->vbus_hard_reset_work, msecs_to_jiffies(800));
break;
case PD_DR_Swap_Request_Received:
msg_maxim("DR_SWAP received.");
/* currently, do nothing
* calling max77705_check_pdo() has been moved to max77705_psrdy_irq()
* for specific PD charger issue
*/
break;
case PD_PR_Swap_Request_Received:
msg_maxim("PR_SWAP received.");
break;
case PD_VCONN_Swap_Request_Received:
msg_maxim("VCONN_SWAP received.");
break;
case Received_PD_Message_in_illegal_state:
break;
case Samsung_Accessory_is_attached:
break;
case VDM_Attention_message_Received:
break;
case Sink_PD_Disabled:
#if 0
/* to do */
/* AFC HV */
value[0] = 0x20;
rc = max77705_ccpd_write_command(chip, value, 1);
if (rc > 0)
pr_err("failed to send command\n");
#endif
break;
case Source_PD_Disabled:
break;
case Prswap_Snktosrc_Sent:
usbc_data->pd_pr_swap = cc_SOURCE;
break;
case Prswap_Srctosnk_Sent:
usbc_data->pd_pr_swap = cc_SINK;
break;
case HARDRESET_RECEIVED:
/*turn off the vbus both Source and Sink*/
if (usbc_data->cc_data->current_pr == SRC) {
max77705_vbus_turn_on_ctrl(usbc_data, OFF, false);
schedule_delayed_work(&usbc_data->vbus_hard_reset_work, msecs_to_jiffies(760));
}
#ifdef CONFIG_USB_NOTIFY_PROC_LOG
event = NOTIFY_EXTRA_HARDRESET_RECEIVED;
store_usblog_notify(NOTIFY_EXTRA, (void *)&event, NULL);
#endif
break;
case HARDRESET_SENT:
/*turn off the vbus both Source and Sink*/
if (usbc_data->cc_data->current_pr == SRC) {
max77705_vbus_turn_on_ctrl(usbc_data, OFF, false);
schedule_delayed_work(&usbc_data->vbus_hard_reset_work, msecs_to_jiffies(760));
}
#ifdef CONFIG_USB_NOTIFY_PROC_LOG
event = NOTIFY_EXTRA_HARDRESET_SENT;
store_usblog_notify(NOTIFY_EXTRA, (void *)&event, NULL);
#endif
break;
case Get_Vbus_turn_on:
break;
case Get_Vbus_turn_off:
max77705_vbus_turn_on_ctrl(usbc_data, OFF, false);
break;
case PRSWAP_SRCTOSWAP:
max77705_vbus_turn_on_ctrl(usbc_data, OFF, false);
msg_maxim("PRSWAP_SRCTOSWAP : [%x]", pd_msg);
break;
case PRSWAP_SWAPTOSNK:
max77705_vbus_turn_on_ctrl(usbc_data, OFF, false);
msg_maxim("PRSWAP_SWAPTOSNK : [%x]", pd_msg);
break;
case PRSWAP_SNKTOSWAP:
msg_maxim("PRSWAP_SNKTOSWAP : [%x]", pd_msg);
max77705_notify_prswap(usbc_data, PRSWAP_SNKTOSWAP);
/* CHGINSEL disable */
psy_charger = power_supply_get_by_name("max77705-charger");
if (psy_charger) {
val.intval = 0;
psy_do_property("max77705-charger", set, POWER_SUPPLY_EXT_PROP_CHGINSEL, val);
} else {
pr_err("%s: Fail to get psy charger\n", __func__);
}
break;
case PRSWAP_SWAPTOSRC:
max77705_vbus_turn_on_ctrl(usbc_data, ON, false);
msg_maxim("PRSWAP_SNKTOSRC : [%x]", pd_msg);
break;
case Current_Cable_Connected:
max77705_manual_jig_on(usbc_data, 1);
usbc_data->manual_lpm_mode = 1;
msg_maxim("Current_Cable_Connected : [%x]", pd_msg);
break;
case SRC_CAP_RECEIVED:
msg_maxim("SRC_CAP_RECEIVED : [%x]", pd_msg);
break;
case Status_Received:
value.opcode = OPCODE_SAMSUNG_READ_MESSAGE;
value.write_data[0] = 0x02;
value.write_length = 1;
value.read_length = 32;
max77705_usbc_opcode_write(usbc_data, &value);
msg_maxim("@TA_ALERT: Status Receviced : [%x]", pd_msg);
break;
case Alert_Message:
value.opcode = OPCODE_SAMSUNG_READ_MESSAGE;
value.write_data[0] = 0x01;
value.write_length = 1;
value.read_length = 32;
max77705_usbc_opcode_write(usbc_data, &value);
msg_maxim("@TA_ALERT: Alert Message : [%x]", pd_msg);
break;
default:
break;
}
}
void max77705_pd_check_pdmsg_callback(void *data, u8 pdmsg)
{
struct max77705_usbc_platform_data *usbc_data = data;
if (!usbc_data) {
msg_maxim("usbc_data is null");
return;
}
if (!usbc_data->pd_data->psrdy_received &&
(pdmsg == Sink_PD_PSRdy_received || pdmsg == SRC_CAP_RECEIVED)) {
union power_supply_propval val;
msg_maxim("pdmsg=%x", pdmsg);
val.intval = 1;
psy_do_property("battery", set, POWER_SUPPLY_EXT_PROP_SRCCAP, val);
}
}
static void max77705_pd_rid(struct max77705_usbc_platform_data *usbc_data, u8 fct_id)
{
struct max77705_pd_data *pd_data = usbc_data->pd_data;
u8 prev_rid = pd_data->device;
#if defined(CONFIG_CCIC_NOTIFIER)
static int rid = RID_OPEN;
#endif
switch (fct_id) {
case FCT_GND:
msg_maxim(" RID_000K");
pd_data->device = DEV_FCT_0;
#if defined(CONFIG_CCIC_NOTIFIER)
rid = RID_000K;
#endif
break;
case FCT_1Kohm:
msg_maxim(" RID_001K");
pd_data->device = DEV_FCT_1K;
#if defined(CONFIG_CCIC_NOTIFIER)
rid = RID_001K;
#endif
break;
case FCT_255Kohm:
msg_maxim(" RID_255K");
pd_data->device = DEV_FCT_255K;
#if defined(CONFIG_CCIC_NOTIFIER)
rid = RID_255K;
#endif
break;
case FCT_301Kohm:
msg_maxim(" RID_301K");
pd_data->device = DEV_FCT_301K;
#if defined(CONFIG_CCIC_NOTIFIER)
rid = RID_301K;
#endif
break;
case FCT_523Kohm:
msg_maxim(" RID_523K");
pd_data->device = DEV_FCT_523K;
#if defined(CONFIG_CCIC_NOTIFIER)
rid = RID_523K;
#endif
break;
case FCT_619Kohm:
msg_maxim(" RID_619K");
pd_data->device = DEV_FCT_619K;
#if defined(CONFIG_CCIC_NOTIFIER)
rid = RID_619K;
#endif
break;
case FCT_OPEN:
msg_maxim(" RID_OPEN");
pd_data->device = DEV_FCT_OPEN;
#if defined(CONFIG_CCIC_NOTIFIER)
rid = RID_OPEN;
#endif
break;
default:
msg_maxim(" RID_UNDEFINED");
pd_data->device = DEV_UNKNOWN;
#if defined(CONFIG_CCIC_NOTIFIER)
rid = RID_UNDEFINED;
#endif
break;
}
if (prev_rid != pd_data->device) {
#if defined(CONFIG_CCIC_NOTIFIER)
/* RID */
max77705_ccic_event_work(usbc_data,
CCIC_NOTIFY_DEV_MUIC, CCIC_NOTIFY_ID_RID,
rid, 0, 0);
usbc_data->cur_rid = rid;
/* turn off USB */
if (pd_data->device == DEV_FCT_OPEN || pd_data->device == DEV_UNKNOWN
|| pd_data->device == DEV_FCT_523K || pd_data->device == DEV_FCT_619K) {
#if defined(CONFIG_DUAL_ROLE_USB_INTF)
usbc_data->power_role = DUAL_ROLE_PROP_PR_NONE;
#elif defined(CONFIG_TYPEC)
usbc_data->typec_power_role = TYPEC_SINK;
#endif
/* usb or otg */
max77705_ccic_event_work(usbc_data,
CCIC_NOTIFY_DEV_USB, CCIC_NOTIFY_ID_USB,
0/*attach*/, USB_STATUS_NOTIFY_DETACH/*drp*/, 0);
}
#endif
}
}
static irqreturn_t max77705_pdmsg_irq(int irq, void *data)
{
struct max77705_usbc_platform_data *usbc_data = data;
struct max77705_pd_data *pd_data = usbc_data->pd_data;
u8 pdmsg = 0;
max77705_read_reg(usbc_data->muic, REG_PD_STATUS0, &pd_data->pd_status0);
pdmsg = pd_data->pd_status0;
msg_maxim("IRQ(%d)_IN pdmsg: %02x", irq, pdmsg);
max77705_pd_check_pdmsg(usbc_data, pdmsg);
pd_data->pdsmg = pdmsg;
msg_maxim("IRQ(%d)_OUT", irq);
return IRQ_HANDLED;
}
static irqreturn_t max77705_psrdy_irq(int irq, void *data)
{
struct max77705_usbc_platform_data *usbc_data = data;
u8 psrdy_received = 0;
#if defined(CONFIG_TYPEC)
enum typec_pwr_opmode mode = TYPEC_PWR_MODE_USB;
#endif
msg_maxim("IN");
max77705_read_reg(usbc_data->muic, REG_PD_STATUS1, &usbc_data->pd_status1);
psrdy_received = (usbc_data->pd_status1 & BIT_PD_PSRDY)
>> FFS(BIT_PD_PSRDY);
if (psrdy_received && !usbc_data->pd_support
&& usbc_data->pd_data->cc_status != CC_NO_CONN)
usbc_data->pd_support = true;
if (usbc_data->typec_try_state_change == TRY_ROLE_SWAP_PR &&
usbc_data->pd_support) {
msg_maxim("typec_reverse_completion");
usbc_data->typec_try_state_change = TRY_ROLE_SWAP_NONE;
complete(&usbc_data->typec_reverse_completion);
}
msg_maxim("psrdy_received=%d, usbc_data->pd_support=%d, cc_status=%d",
psrdy_received, usbc_data->pd_support, usbc_data->pd_data->cc_status);
#if defined(CONFIG_TYPEC)
mode = max77705_get_pd_support(usbc_data);
typec_set_pwr_opmode(usbc_data->port, mode);
#endif
if (usbc_data->pd_data->cc_status == CC_SNK && psrdy_received) {
max77705_check_pdo(usbc_data);
usbc_data->pd_data->psrdy_received = true;
}
if (psrdy_received && usbc_data->pd_data->cc_status != CC_NO_CONN) {
usbc_data->pn_flag = true;
complete(&usbc_data->psrdy_wait);
}
msg_maxim("OUT");
return IRQ_HANDLED;
}
bool max77705_sec_pps_control(int en)
{
#if defined(CONFIG_PDIC_PD30)
struct max77705_usbc_platform_data *pusbpd = pd_noti.pusbpd;
union power_supply_propval val = {0,};
msg_maxim(": %d", en);
val.intval = en; /* 0: stop pps, 1: start pps */
psy_do_property("battery", set,
POWER_SUPPLY_EXT_PROP_DIRECT_SEND_UVDM, val);
if (!en && !pusbpd->pn_flag) {
reinit_completion(&pusbpd->psrdy_wait);
if (!wait_for_completion_timeout(&pusbpd->psrdy_wait, msecs_to_jiffies(1000))) {
msg_maxim("PSRDY COMPLETION TIMEOUT");
return false;
}
}
return true;
#else
return true;
#endif
}
static void max77705_datarole_irq_handler(void *data, int irq)
{
struct max77705_usbc_platform_data *usbc_data = data;
struct max77705_pd_data *pd_data = usbc_data->pd_data;
u8 datarole = 0;
max77705_read_reg(usbc_data->muic, REG_PD_STATUS1, &pd_data->pd_status1);
datarole = (pd_data->pd_status1 & BIT_PD_DataRole)
>> FFS(BIT_PD_DataRole);
/* abnormal data role without setting power role */
if (usbc_data->cc_data->current_pr == 0xFF) {
msg_maxim("INVALID IRQ IRQ(%d)_OUT", irq);
return;
}
if (irq == CCIC_IRQ_INIT_DETECT) {
if (usbc_data->pd_data->cc_status == CC_SNK)
msg_maxim("initial time : SNK");
else
return;
}
switch (datarole) {
case UFP:
if (pd_data->current_dr != UFP) {
pd_data->previous_dr = pd_data->current_dr;
pd_data->current_dr = UFP;
if (pd_data->previous_dr != 0xFF)
msg_maxim("%s detach previous usb connection\n", __func__);
max77705_notify_dr_status(usbc_data, 1);
#if defined(CONFIG_TYPEC)
if (usbc_data->typec_try_state_change == TRY_ROLE_SWAP_DR ||
usbc_data->typec_try_state_change == TRY_ROLE_SWAP_TYPE) {
msg_maxim("typec_reverse_completion");
usbc_data->typec_try_state_change = TRY_ROLE_SWAP_NONE;
complete(&usbc_data->typec_reverse_completion);
}
#endif
}
msg_maxim(" UFP");
break;
case DFP:
if (pd_data->current_dr != DFP) {
pd_data->previous_dr = pd_data->current_dr;
pd_data->current_dr = DFP;
if (pd_data->previous_dr != 0xFF)
msg_maxim("%s detach previous usb connection\n", __func__);
max77705_notify_dr_status(usbc_data, 1);
#if defined(CONFIG_TYPEC)
if (usbc_data->typec_try_state_change == TRY_ROLE_SWAP_DR ||
usbc_data->typec_try_state_change == TRY_ROLE_SWAP_TYPE) {
msg_maxim("typec_reverse_completion");
usbc_data->typec_try_state_change = TRY_ROLE_SWAP_NONE;
complete(&usbc_data->typec_reverse_completion);
}
#endif
if (usbc_data->cc_data->current_pr == SNK && !(usbc_data->is_first_booting)) {
max77705_vdm_process_set_identity_req(usbc_data);
msg_maxim("SEND THE IDENTITY REQUEST FROM DFP HANDLER");
}
}
msg_maxim(" DFP");
break;
default:
msg_maxim(" DATAROLE(Never Call this routine)");
break;
}
}
static irqreturn_t max77705_datarole_irq(int irq, void *data)
{
pr_debug("%s: IRQ(%d)_IN\n", __func__, irq);
max77705_datarole_irq_handler(data, irq);
pr_debug("%s: IRQ(%d)_OUT\n", __func__, irq);
return IRQ_HANDLED;
}
static irqreturn_t max77705_ssacc_irq(int irq, void *data)
{
struct max77705_usbc_platform_data *usbc_data = data;
struct max77705_pd_data *pd_data = usbc_data->pd_data;
pr_debug("%s: IRQ(%d)_IN\n", __func__, irq);
msg_maxim(" SSAcc command received");
/* Read through Opcode command 0x50 */
pd_data->ssacc = 1;
pr_debug("%s: IRQ(%d)_OUT\n", __func__, irq);
return IRQ_HANDLED;
}
static void max77705_check_cc_sbu_short(void *data)
{
u8 cc_status1 = 0;
struct max77705_usbc_platform_data *usbc_data = data;
struct max77705_pd_data *pd_data = usbc_data->pd_data;
max77705_read_reg(usbc_data->muic, REG_CC_STATUS1, &cc_status1);
/* 0b01: CC-5V, 0b10: SBU-5V, 0b11: SBU-GND Short */
cc_status1 = (cc_status1 & BIT_CCSBUSHORT) >> FFS(BIT_CCSBUSHORT);
if (cc_status1)
pd_data->cc_sbu_short = true;
msg_maxim("%s cc_status1 : %x, cc_sbu_short : %d\n", __func__, cc_status1, pd_data->cc_sbu_short);
}
static u8 max77705_check_rid(void *data)
{
u8 fct_id = 0;
struct max77705_usbc_platform_data *usbc_data = data;
struct max77705_pd_data *pd_data = usbc_data->pd_data;
max77705_read_reg(usbc_data->muic, REG_PD_STATUS1, &pd_data->pd_status1);
fct_id = (pd_data->pd_status1 & BIT_FCT_ID) >> FFS(BIT_FCT_ID);
#if defined(CONFIG_SEC_FACTORY)
factory_execute_monitor(FAC_ABNORMAL_REPEAT_RID);
#endif
max77705_pd_rid(usbc_data, fct_id);
pd_data->fct_id = fct_id;
msg_maxim("%s rid : %d, fct_id : %d\n", __func__, usbc_data->cur_rid, fct_id);
return fct_id;
}
static irqreturn_t max77705_fctid_irq(int irq, void *data)
{
pr_debug("%s: IRQ(%d)_IN\n", __func__, irq);
max77705_check_rid(data);
pr_debug("%s: IRQ(%d)_OUT\n", __func__, irq);
return IRQ_HANDLED;
}
int max77705_pd_init(struct max77705_usbc_platform_data *usbc_data)
{
struct max77705_pd_data *pd_data = NULL;
int ret;
msg_maxim(" IN");
pd_data = usbc_data->pd_data;
pd_noti.pusbpd = usbc_data;
pd_noti.sink_status.rp_currentlvl = RP_CURRENT_LEVEL_NONE;
pd_noti.sink_status.available_pdo_num = 0;
pd_noti.sink_status.selected_pdo_num = 0;
pd_noti.sink_status.current_pdo_num = 0;
#if defined(CONFIG_PDIC_PD30)
pd_noti.sink_status.pps_voltage = 0;
pd_noti.sink_status.pps_current = 0;
pd_noti.sink_status.request_apdo = false;
pd_noti.sink_status.has_apdo = false;
#endif
pd_noti.event = PDIC_NOTIFY_EVENT_DETACH;
pd_data->pdo_list = false;
pd_data->psrdy_received = false;
pd_data->cc_sbu_short = false;
fp_select_pdo = max77705_select_pdo;
#if defined(CONFIG_PDIC_PD30)
fp_sec_pd_select_pps = max77705_select_pps;
fp_sec_pd_get_apdo_max_power = max77705_get_apdo_max_power;
#endif
pd_data->wqueue = create_singlethread_workqueue("max77705_pd");
if (!pd_data->wqueue) {
pr_err("%s: Fail to Create Workqueue\n", __func__);
goto err_irq;
}
INIT_DELAYED_WORK(&pd_data->retry_work, max77705_pd_retry_work);
wake_lock_init(&pd_data->pdmsg_wake_lock, WAKE_LOCK_SUSPEND,
"pd->pdmsg");
wake_lock_init(&pd_data->datarole_wake_lock, WAKE_LOCK_SUSPEND,
"pd->datarole");
wake_lock_init(&pd_data->ssacc_wake_lock, WAKE_LOCK_SUSPEND,
"pd->ssacc");
wake_lock_init(&pd_data->fct_id_wake_lock, WAKE_LOCK_SUSPEND,
"pd->fctid");
pd_data->irq_pdmsg = usbc_data->irq_base + MAX77705_PD_IRQ_PDMSG_INT;
if (pd_data->irq_pdmsg) {
ret = request_threaded_irq(pd_data->irq_pdmsg,
NULL, max77705_pdmsg_irq,
0,
"pd-pdmsg-irq", usbc_data);
if (ret) {
pr_err("%s: Failed to Request IRQ (%d)\n", __func__, ret);
goto err_irq;
}
}
pd_data->irq_psrdy = usbc_data->irq_base + MAX77705_PD_IRQ_PS_RDY_INT;
if (pd_data->irq_psrdy) {
ret = request_threaded_irq(pd_data->irq_psrdy,
NULL, max77705_psrdy_irq,
0,
"pd-psrdy-irq", usbc_data);
if (ret) {
pr_err("%s: Failed to Request IRQ (%d)\n", __func__, ret);
goto err_irq;
}
}
pd_data->irq_datarole = usbc_data->irq_base + MAX77705_PD_IRQ_DATAROLE_INT;
if (pd_data->irq_datarole) {
ret = request_threaded_irq(pd_data->irq_datarole,
NULL, max77705_datarole_irq,
0,
"pd-datarole-irq", usbc_data);
if (ret) {
pr_err("%s: Failed to Request IRQ (%d)\n", __func__, ret);
goto err_irq;
}
}
pd_data->irq_ssacc = usbc_data->irq_base + MAX77705_PD_IRQ_SSACCI_INT;
if (pd_data->irq_ssacc) {
ret = request_threaded_irq(pd_data->irq_ssacc,
NULL, max77705_ssacc_irq,
0,
"pd-ssacci-irq", usbc_data);
if (ret) {
pr_err("%s: Failed to Request IRQ (%d)\n", __func__, ret);
goto err_irq;
}
}
pd_data->irq_fct_id = usbc_data->irq_base + MAX77705_PD_IRQ_FCTIDI_INT;
if (pd_data->irq_fct_id) {
ret = request_threaded_irq(pd_data->irq_fct_id,
NULL, max77705_fctid_irq,
0,
"pd-fctid-irq", usbc_data);
if (ret) {
pr_err("%s: Failed to Request IRQ (%d)\n", __func__, ret);
goto err_irq;
}
}
/* check RID value for booting time */
max77705_check_rid(usbc_data);
max77705_set_fw_noautoibus(MAX77705_AUTOIBUS_AT_OFF);
/* check CC Pin state for cable attach booting scenario */
max77705_datarole_irq_handler(usbc_data, CCIC_IRQ_INIT_DETECT);
max77705_check_cc_sbu_short(usbc_data);
max77705_register_pdmsg_func(usbc_data->max77705,
max77705_pd_check_pdmsg_callback, (void *)usbc_data);
msg_maxim(" OUT");
return 0;
err_irq:
kfree(pd_data);
return ret;
}