/* * max77705.c - mfd core driver for the Maxim 77705 * * Copyright (C) 2016 Samsung Electronics * Insun Choi * * 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, see . * * This driver is based on max8997.c */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(CONFIG_OF) #include #include #endif /* CONFIG_OF */ #include "../battery_v2/include/sec_charging_common.h" #define I2C_ADDR_PMIC (0xCC >> 1) /* Top sys, Haptic */ #define I2C_ADDR_MUIC (0x4A >> 1) #define I2C_ADDR_CHG (0xD2 >> 1) #define I2C_ADDR_FG (0x6C >> 1) #define I2C_ADDR_DEBUG (0xC4 >> 1) static struct mfd_cell max77705_devs[] = { #if defined(CONFIG_CCIC_MAX77705) { .name = "max77705-usbc", }, #endif #if defined(CONFIG_REGULATOR_MAX77705) { .name = "max77705-safeout", }, #endif /* CONFIG_REGULATOR_MAX77705 */ #if defined(CONFIG_FUELGAUGE_MAX77705) { .name = "max77705-fuelgauge", }, #endif #if defined(CONFIG_CHARGER_MAX77705) { .name = "max77705-charger", }, #endif #if defined(CONFIG_MOTOR_DRV_MAX77705) { .name = "max77705-haptic", }, #endif /* CONFIG_MAX77705_HAPTIC */ #if defined(CONFIG_LEDS_MAX77705_RGB) { .name = "leds-max77705-rgb", }, #endif /* CONFIG_LEDS_MAX77705_RGB */ #if defined(CONFIG_LEDS_MAX77705_FLASH) { .name = "max77705-flash", }, #endif }; int max77705_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest) { struct max77705_dev *max77705 = i2c_get_clientdata(i2c); struct otg_notify *o_notify = get_otg_notify(); int ret; mutex_lock(&max77705->i2c_lock); ret = i2c_smbus_read_byte_data(i2c, reg); mutex_unlock(&max77705->i2c_lock); if (ret < 0) { pr_info("%s:%s reg(0x%x), ret(%d)\n", MFD_DEV_NAME, __func__, reg, ret); if (o_notify) inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT); return ret; } ret &= 0xff; *dest = ret; return 0; } EXPORT_SYMBOL_GPL(max77705_read_reg); int max77705_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf) { struct max77705_dev *max77705 = i2c_get_clientdata(i2c); struct otg_notify *o_notify = get_otg_notify(); int ret; mutex_lock(&max77705->i2c_lock); ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf); mutex_unlock(&max77705->i2c_lock); if (ret < 0) { if (o_notify) inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT); return ret; } return 0; } EXPORT_SYMBOL_GPL(max77705_bulk_read); int max77705_read_word(struct i2c_client *i2c, u8 reg) { struct max77705_dev *max77705 = i2c_get_clientdata(i2c); struct otg_notify *o_notify = get_otg_notify(); int ret; mutex_lock(&max77705->i2c_lock); ret = i2c_smbus_read_word_data(i2c, reg); mutex_unlock(&max77705->i2c_lock); if (ret < 0) { if (o_notify) inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT); } return ret; } EXPORT_SYMBOL_GPL(max77705_read_word); int max77705_write_reg(struct i2c_client *i2c, u8 reg, u8 value) { struct max77705_dev *max77705 = i2c_get_clientdata(i2c); struct otg_notify *o_notify = get_otg_notify(); int ret; mutex_lock(&max77705->i2c_lock); ret = i2c_smbus_write_byte_data(i2c, reg, value); mutex_unlock(&max77705->i2c_lock); if (ret < 0) { pr_info("%s:%s reg(0x%x), ret(%d)\n", MFD_DEV_NAME, __func__, reg, ret); if (o_notify) inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT); } return ret; } EXPORT_SYMBOL_GPL(max77705_write_reg); int max77705_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf) { struct max77705_dev *max77705 = i2c_get_clientdata(i2c); struct otg_notify *o_notify = get_otg_notify(); int ret; mutex_lock(&max77705->i2c_lock); ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf); mutex_unlock(&max77705->i2c_lock); if (ret < 0) { if (o_notify) inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT); return ret; } return 0; } EXPORT_SYMBOL_GPL(max77705_bulk_write); int max77705_write_word(struct i2c_client *i2c, u8 reg, u16 value) { struct max77705_dev *max77705 = i2c_get_clientdata(i2c); struct otg_notify *o_notify = get_otg_notify(); int ret; mutex_lock(&max77705->i2c_lock); ret = i2c_smbus_write_word_data(i2c, reg, value); mutex_unlock(&max77705->i2c_lock); if (ret < 0) { if (o_notify) inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT); return ret; } return 0; } EXPORT_SYMBOL_GPL(max77705_write_word); int max77705_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask) { struct max77705_dev *max77705 = i2c_get_clientdata(i2c); struct otg_notify *o_notify = get_otg_notify(); int ret; u8 old_val, new_val; mutex_lock(&max77705->i2c_lock); ret = i2c_smbus_read_byte_data(i2c, reg); if (ret < 0) { if (o_notify) inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT); goto err; } if (ret >= 0) { old_val = ret & 0xff; new_val = (val & mask) | (old_val & (~mask)); ret = i2c_smbus_write_byte_data(i2c, reg, new_val); if (ret < 0) { if (o_notify) inc_hw_param(o_notify, USB_CCIC_I2C_ERROR_COUNT); goto err; } } err: mutex_unlock(&max77705->i2c_lock); return ret; } EXPORT_SYMBOL_GPL(max77705_update_reg); #if defined(CONFIG_OF) static int of_max77705_dt(struct device *dev, struct max77705_platform_data *pdata) { struct device_node *np_max77705 = dev->of_node; struct device_node *np_battery; int ret = 0; if (!np_max77705) return -EINVAL; pdata->irq_gpio = of_get_named_gpio(np_max77705, "max77705,irq-gpio", 0); pdata->wakeup = of_property_read_bool(np_max77705, "max77705,wakeup"); pr_info("%s: irq-gpio: %u\n", __func__, pdata->irq_gpio); np_battery = of_find_node_by_name(NULL, "battery"); if (!np_battery) { pr_info("%s: np_battery NULL\n", __func__); } else { pdata->wpc_en = of_get_named_gpio(np_battery, "battery,wpc_en", 0); if (pdata->wpc_en < 0) { pr_info("%s: can't get wpc_en (%d)\n", __func__, pdata->wpc_en); pdata->wpc_en = 0; } ret = of_property_read_string(np_battery, "battery,wireless_charger_name", (char const **)&pdata->wireless_charger_name); if (ret) pr_info("%s: Wireless charger name is Empty\n", __func__); } return 0; } #endif /* CONFIG_OF */ /* samsung */ #ifdef CONFIG_CCIC_MAX77705 static void max77705_reset_ic(struct max77705_dev *max77705) { msg_maxim("Reset!!"); max77705_write_reg(max77705->muic, 0x80, 0x0F); msleep(100); } static void max77705_usbc_wait_response_q(struct work_struct *work) { struct max77705_dev *max77705; u8 read_value = 0x00; u8 dummy[2] = { 0, }; max77705 = container_of(work, struct max77705_dev, fw_work); while (max77705->fw_update_state == FW_UPDATE_WAIT_RESP_START) { switch (max77705->pmic_rev) { case MAX77705_PASS1: max77705_bulk_read(max77705->muic, REG_UIC_INT-1, 2, dummy); read_value = dummy[1]; break; default: max77705_bulk_read(max77705->muic, REG_UIC_INT, 1, dummy); read_value = dummy[0]; break; } if ((read_value & BIT_APCmdResI) == BIT_APCmdResI) break; } complete_all(&max77705->fw_completion); } static int max77705_usbc_wait_response(struct max77705_dev *max77705) { unsigned long time_remaining = 0; max77705->fw_update_state = FW_UPDATE_WAIT_RESP_START; init_completion(&max77705->fw_completion); queue_work(max77705->fw_workqueue, &max77705->fw_work); time_remaining = wait_for_completion_timeout( &max77705->fw_completion, msecs_to_jiffies(FW_WAIT_TIMEOUT)); max77705->fw_update_state = FW_UPDATE_WAIT_RESP_STOP; if (!time_remaining) { msg_maxim("Failed to update due to timeout"); cancel_work_sync(&max77705->fw_work); return FW_UPDATE_TIMEOUT_FAIL; } return 0; } static int __max77705_usbc_fw_update( struct max77705_dev *max77705, const u8 *fw_bin) { u8 fw_cmd = FW_CMD_END; u8 fw_len = 0; u8 fw_opcode = 0; u8 fw_data_len = 0; u8 fw_data[FW_CMD_WRITE_SIZE] = { 0, }; u8 verify_data[FW_VERIFY_DATA_SIZE] = { 0, }; int ret = -FW_UPDATE_CMD_FAIL; /* * fw_bin[0] = Write Command (0x01) * or * fw_bin[0] = Read Command (0x03) * or * fw_bin[0] = End Command (0x00) */ fw_cmd = fw_bin[0]; /* * Check FW Command */ if (fw_cmd == FW_CMD_END) { max77705_reset_ic(max77705); max77705->fw_update_state = FW_UPDATE_END; return FW_UPDATE_END; } /* * fw_bin[1] = Length ( OPCode + Data ) */ fw_len = fw_bin[1]; /* * Check fw data length * We support 0x22 or 0x04 only */ if (fw_len != 0x22 && fw_len != 0x04) return FW_UPDATE_MAX_LENGTH_FAIL; /* * fw_bin[2] = OPCode */ fw_opcode = fw_bin[2]; /* * In case write command, * fw_bin[35:3] = Data * * In case read command, * fw_bin[5:3] = Data */ fw_data_len = fw_len - 1; /* exclude opcode */ memcpy(fw_data, &fw_bin[3], fw_data_len); switch (fw_cmd) { case FW_CMD_WRITE: if (fw_data_len > I2C_SMBUS_BLOCK_MAX) { /* write the half data */ max77705_bulk_write(max77705->muic, fw_opcode, I2C_SMBUS_BLOCK_HALF, fw_data); max77705_bulk_write(max77705->muic, fw_opcode + I2C_SMBUS_BLOCK_HALF, fw_data_len - I2C_SMBUS_BLOCK_HALF, &fw_data[I2C_SMBUS_BLOCK_HALF]); } else max77705_bulk_write(max77705->muic, fw_opcode, fw_data_len, fw_data); ret = max77705_usbc_wait_response(max77705); if (ret) return ret; /* * Why do we need 1ms sleep in case MQ81? */ /* msleep(1); */ return FW_CMD_WRITE_SIZE; case FW_CMD_READ: max77705_bulk_read(max77705->muic, fw_opcode, fw_data_len, verify_data); /* * Check fw data sequence number * It should be increased from 1 step by step. */ if (memcmp(verify_data, &fw_data[1], 2)) { msg_maxim("[0x%02x 0x%02x], [0x%02x, 0x%02x], [0x%02x, 0x%02x]", verify_data[0], fw_data[0], verify_data[1], fw_data[1], verify_data[2], fw_data[2]); return FW_UPDATE_VERIFY_FAIL; } return FW_CMD_READ_SIZE; } msg_maxim("Command error"); return ret; } int max77705_write_jig_on(struct max77705_dev *max77705) { u8 write_values[OPCODE_MAX_LENGTH] = { 0, }; int ret = 0; int length = 0x2; int i = 0; write_values[0] = OPCODE_CTRL3_W; write_values[1] = 0x1; write_values[2] = 0x1; for (i = 0; i < length + OPCODE_SIZE; i++) msg_maxim("[%d], 0x[%x]", i, write_values[i]); /* Write opcode and data */ ret = max77705_bulk_write(max77705->muic, OPCODE_WRITE, length + OPCODE_SIZE, write_values); /* Write end of data by 0x00 */ if (length < OPCODE_DATA_LENGTH) max77705_write_reg(max77705->muic, OPCODE_WRITE_END, 0x00); return 0; } static int max77705_fuelgauge_read_vcell(struct max77705_dev *max77705) { u8 data[2]; u32 vcell; u16 w_data; u32 temp; u32 temp2; if (max77705_bulk_read(max77705->fuelgauge, VCELL_REG, 2, data) < 0) { pr_err("%s: Failed to read VCELL\n", __func__); return -1; } w_data = (data[1] << 8) | data[0]; temp = (w_data & 0xFFF) * 78125; vcell = temp / 1000000; temp = ((w_data & 0xF000) >> 4) * 78125; temp2 = temp / 1000000; vcell += (temp2 << 4); return vcell; } static void max77705_wc_control(struct max77705_dev *max77705, bool enable) { struct power_supply *psy = NULL; union power_supply_propval value = {0, }; char wpc_en_status[2]; psy = get_power_supply_by_name(max77705->pdata->wireless_charger_name); if (psy) { wpc_en_status[0] = WPC_EN_CCIC; wpc_en_status[1] = enable ? true : false; value.strval= wpc_en_status; if ((psy->desc->set_property != NULL) && (psy->desc->set_property(psy, (enum power_supply_property)POWER_SUPPLY_EXT_PROP_WPC_EN, &value) >= 0)) pr_info("%s: WC CONTROL: %s", __func__, wpc_en_status[1] ? "Enable" : "Disable"); power_supply_put(psy); } else { if (max77705->pdata->wpc_en) { if (enable) { gpio_direction_output(max77705->pdata->wpc_en, 0); pr_info("%s: WC CONTROL: ENABLE", __func__); } else { gpio_direction_output(max77705->pdata->wpc_en, 1); pr_info("%s: WC CONTROL: DISABLE", __func__); } } else { pr_info("%s : no wpc_en\n", __func__); } } pr_info("%s: wpc_en(%d)\n", __func__, gpio_get_value(max77705->pdata->wpc_en)); } int max77705_usbc_fw_update(struct max77705_dev *max77705, const u8 *fw_bin, int fw_bin_len, int enforce_do) { struct max77705_fw_header fw_header; struct otg_notify *o_notify = get_otg_notify(); int offset = 0; unsigned long duration = 0; int size = 0; int try_count = 0; int ret = 0; u8 pd_status1 = 0x0; static u8 fct_id; /* FCT cable */ u8 try_command = 0; u8 sw_boot = 0; u8 chg_cnfg_00 = 0; bool chg_mode_changed = 0; bool wpc_en_changed = 0; int vcell = 0; u8 vbvolt = 0; disable_irq(max77705->irq); max77705_write_reg(max77705->muic, REG_PD_INT_M, 0xFF); max77705_write_reg(max77705->muic, REG_CC_INT_M, 0xFF); max77705_write_reg(max77705->muic, REG_UIC_INT_M, 0xFF); max77705_write_reg(max77705->muic, REG_VDM_INT_M, 0xFF); max77705_read_reg(max77705->charger, MAX77705_CHG_REG_CNFG_00, &chg_cnfg_00); retry: offset = 0; duration = 0; size = 0; ret = 0; /* to do (unmask interrupt) */ ret = max77705_read_reg(max77705->muic, REG_UIC_FW_REV, &max77705->FW_Revision); ret = max77705_read_reg(max77705->muic, REG_UIC_FW_MINOR, &max77705->FW_Minor_Revision); if (ret < 0 && (try_count == 0 && try_command == 0)) { msg_maxim("Failed to read FW_REV"); goto out; } duration = jiffies; msg_maxim("chip : %02X.%02X, FW : %02X.%02X", max77705->FW_Revision, max77705->FW_Minor_Revision, fw_bin[4], fw_bin[5]); store_ccic_bin_version(&fw_bin[4], &sw_boot); #ifdef CONFIG_SEC_FACTORY if ((max77705->FW_Revision != fw_bin[4]) || (max77705->FW_Minor_Revision != fw_bin[5])) { #else if ((max77705->FW_Revision == 0xff) || (max77705->FW_Revision < fw_bin[4]) || ((max77705->FW_Revision == fw_bin[4]) && (max77705->FW_Minor_Revision < fw_bin[5])) || enforce_do) { #endif if (!enforce_do && max77705->pmic_rev > MAX77705_PASS1) { /* on Booting time */ max77705_read_reg(max77705->muic, REG_PD_STATUS1, &pd_status1); fct_id = (pd_status1 & BIT_FCT_ID) >> FFS(BIT_FCT_ID); msg_maxim("FCT_ID : 0x%x", fct_id); } if (try_count == 0 && try_command == 0) { /* change chg_mode during FW update */ vcell = max77705_fuelgauge_read_vcell(max77705); if (vcell >= 3300) { wpc_en_changed = true; max77705_wc_control(max77705, false); max77705_read_reg(max77705->muic, MAX77705_USBC_REG_BC_STATUS, &vbvolt); pr_info("%s: BC:0x%02x, vbvolt:0x%x\n", __func__, vbvolt, (vbvolt & 0x80) >> 7); if (!(vbvolt & 0x80)) { chg_mode_changed = true; /* Switching Frequency : 3MHz */ max77705_update_reg(max77705->charger, MAX77705_CHG_REG_CNFG_08, 0x00, 0x3); pr_info("%s: +set Switching Frequency 3Mhz\n", __func__); /* Disable skip mode */ max77705_update_reg(max77705->charger, MAX77705_CHG_REG_CNFG_12, 0x1, 0x1); pr_info("%s: +set Disable skip mode\n", __func__); max77705_update_reg(max77705->charger, MAX77705_CHG_REG_CNFG_00, 0x09, 0x0F); pr_info("%s: +change chg_mode(0x9), vcell(%dmv)\n", __func__, vcell); } else { pr_info("%s: +keep chg_mode(0x%x), vcell(%dmv)\n", __func__, chg_cnfg_00 & 0x0F, vcell); } msleep(500); } else { pr_info("%s: keep chg_mode(0x%x), vcell(%dmv)\n", __func__, chg_cnfg_00 & 0x0F, vcell); goto out; } } max77705_write_reg(max77705->muic, 0x21, 0xD0); max77705_write_reg(max77705->muic, 0x41, 0x00); msleep(500); max77705_read_reg(max77705->muic, REG_UIC_FW_REV, &max77705->FW_Revision); max77705_read_reg(max77705->muic, REG_UIC_FW_MINOR, &max77705->FW_Minor_Revision); msg_maxim("Start FW updating (%02X.%02X)", max77705->FW_Revision, max77705->FW_Minor_Revision); if (max77705->FW_Revision != 0xFF && try_command < 3) { try_command++; goto retry; } /* Copy fw header */ memcpy(&fw_header, fw_bin, FW_HEADER_SIZE); if (max77705->FW_Revision != 0xFF) { if (++try_count < FW_VERIFY_TRY_COUNT) { msg_maxim("the Fail to enter secure mode %d", try_count); goto retry; } else { msg_maxim("the Secure Update Fail!!"); if (o_notify) inc_hw_param(o_notify, USB_CCIC_FWUP_ERROR_COUNT); goto out; } } for (offset = FW_HEADER_SIZE; offset < fw_bin_len && size != FW_UPDATE_END;) { size = __max77705_usbc_fw_update(max77705, &fw_bin[offset]); switch (size) { case FW_UPDATE_VERIFY_FAIL: offset -= FW_CMD_WRITE_SIZE; /* FALLTHROUGH */ case FW_UPDATE_TIMEOUT_FAIL: /* * Retry FW updating */ if (++try_count < FW_VERIFY_TRY_COUNT) { msg_maxim("Retry fw write. ret %d, count %d, offset %d", size, try_count, offset); goto retry; } else { msg_maxim("Failed to update FW. ret %d, offset %d", size, (offset + size)); if (o_notify) inc_hw_param(o_notify, USB_CCIC_FWUP_ERROR_COUNT); goto out; } break; case FW_UPDATE_CMD_FAIL: case FW_UPDATE_MAX_LENGTH_FAIL: msg_maxim("Failed to update FW. ret %d, offset %d", size, (offset + size)); if (o_notify) inc_hw_param(o_notify, USB_CCIC_FWUP_ERROR_COUNT); goto out; case FW_UPDATE_END: /* 0x00 */ /* JIG PIN for setting HIGH. */ if (fct_id == FCT_523Kohm && !enforce_do) max77705_write_jig_on(max77705); max77705_read_reg(max77705->muic, REG_UIC_FW_REV, &max77705->FW_Revision); max77705_read_reg(max77705->muic, REG_UIC_FW_MINOR, &max77705->FW_Minor_Revision); msg_maxim("chip : %02X.%02X, FW : %02X.%02X", max77705->FW_Revision, max77705->FW_Minor_Revision, fw_header.data4, fw_header.data5); msg_maxim("Completed"); break; default: offset += size; break; } if (offset == fw_bin_len) { max77705_reset_ic(max77705); max77705_read_reg(max77705->muic, REG_UIC_FW_REV, &max77705->FW_Revision); max77705_read_reg(max77705->muic, REG_UIC_FW_MINOR, &max77705->FW_Minor_Revision); msg_maxim("chip : %02X.%02X, FW : %02X.%02X", max77705->FW_Revision, max77705->FW_Minor_Revision, fw_header.data4, fw_header.data5); msg_maxim("Completed via SYS path"); } } } else { msg_maxim("Don't need to update!"); goto out; } duration = jiffies - duration; msg_maxim("Duration : %dms", jiffies_to_msecs(duration)); out: if (chg_mode_changed) { vcell = max77705_fuelgauge_read_vcell(max77705); /* Auto skip mode */ max77705_update_reg(max77705->charger, MAX77705_CHG_REG_CNFG_12, 0x0, 0x1); pr_info("%s: -set Auto skip mode\n", __func__); max77705_update_reg(max77705->charger, MAX77705_CHG_REG_CNFG_12, 0x0, 0x20); pr_info("%s: -disable CHGINSEL\n", __func__); max77705_update_reg(max77705->charger, MAX77705_CHG_REG_CNFG_00, 0x4, 0x0F); pr_info("%s: -set chg_mode(0x4)\n", __func__); max77705_update_reg(max77705->charger, MAX77705_CHG_REG_CNFG_12, 0x20, 0x20); pr_info("%s: -enable CHGINSEL\n", __func__); max77705_update_reg(max77705->charger, MAX77705_CHG_REG_CNFG_00, chg_cnfg_00, 0x0F); pr_info("%s: -recover chg_mode(0x%x), vcell(%dmv)\n", __func__, chg_cnfg_00 & 0x0F, vcell); /* Switching Frequency : 1.5MHz */ max77705_update_reg(max77705->charger, MAX77705_CHG_REG_CNFG_08, 0x02, 0x3); pr_info("%s: -set Switching Frequency 1.5MHz\n", __func__); } if (wpc_en_changed) { max77705_wc_control(max77705, true); } enable_irq(max77705->irq); return 0; } EXPORT_SYMBOL_GPL(max77705_usbc_fw_update); #endif static int max77705_i2c_probe(struct i2c_client *i2c, const struct i2c_device_id *dev_id) { struct max77705_dev *max77705; struct max77705_platform_data *pdata = i2c->dev.platform_data; u8 reg_data; int ret = 0; pr_info("%s:%s\n", MFD_DEV_NAME, __func__); max77705 = kzalloc(sizeof(struct max77705_dev), GFP_KERNEL); if (!max77705) return -ENOMEM; if (i2c->dev.of_node) { pdata = devm_kzalloc(&i2c->dev, sizeof(struct max77705_platform_data), GFP_KERNEL); if (!pdata) { ret = -ENOMEM; goto err; } ret = of_max77705_dt(&i2c->dev, pdata); if (ret < 0) { dev_err(&i2c->dev, "Failed to get device of_node\n"); goto err; } i2c->dev.platform_data = pdata; } else pdata = i2c->dev.platform_data; max77705->dev = &i2c->dev; max77705->i2c = i2c; max77705->irq = i2c->irq; if (pdata) { max77705->pdata = pdata; pdata->irq_base = irq_alloc_descs(-1, 0, MAX77705_IRQ_NR, -1); if (pdata->irq_base < 0) { pr_err("%s:%s irq_alloc_descs Fail! ret(%d)\n", MFD_DEV_NAME, __func__, pdata->irq_base); ret = -EINVAL; goto err; } else max77705->irq_base = pdata->irq_base; max77705->irq_gpio = pdata->irq_gpio; max77705->wakeup = pdata->wakeup; } else { ret = -EINVAL; goto err; } mutex_init(&max77705->i2c_lock); i2c_set_clientdata(i2c, max77705); if (max77705_read_reg(i2c, MAX77705_PMIC_REG_PMICREV, ®_data) < 0) { dev_err(max77705->dev, "device not found on this channel (this is not an error)\n"); ret = -ENODEV; goto err_w_lock; } else { /* print rev */ max77705->pmic_rev = (reg_data & 0x7); max77705->pmic_ver = ((reg_data & 0xF8) >> 0x3); pr_info("%s:%s device found: rev.0x%x, ver.0x%x\n", MFD_DEV_NAME, __func__, max77705->pmic_rev, max77705->pmic_ver); } /* No active discharge on safeout ldo 1,2 */ /* max77705_update_reg(i2c, MAX77705_PMIC_REG_SAFEOUT_CTRL, 0x00, 0x30); */ #ifdef CONFIG_CCIC_MAX77705 init_completion(&max77705->fw_completion); max77705->fw_workqueue = create_singlethread_workqueue("fw_update"); if (max77705->fw_workqueue == NULL) return -ENOMEM; INIT_WORK(&max77705->fw_work, max77705_usbc_wait_response_q); #endif max77705->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC); i2c_set_clientdata(max77705->muic, max77705); max77705->charger = i2c_new_dummy(i2c->adapter, I2C_ADDR_CHG); i2c_set_clientdata(max77705->charger, max77705); max77705->fuelgauge = i2c_new_dummy(i2c->adapter, I2C_ADDR_FG); i2c_set_clientdata(max77705->fuelgauge, max77705); #ifdef CONFIG_CCIC_MAX77705 switch (max77705->pmic_rev) { case MAX77705_PASS1: pr_info("[MAX77705] Doesn't update the MAX77705_PASS1\n"); break; case MAX77705_PASS2: max77705_usbc_fw_update(max77705, BOOT_FLASH_FW_PASS2, ARRAY_SIZE(BOOT_FLASH_FW_PASS2), 0); break; case MAX77705_PASS3: max77705_usbc_fw_update(max77705, BOOT_FLASH_FW_PASS3, ARRAY_SIZE(BOOT_FLASH_FW_PASS3), 0); break; default: pr_info("[MAX77705] FAIL F/W ON BOOTING TIME and PMIC_REVISION isn't valid\n"); break; }; #endif max77705->debug = i2c_new_dummy(i2c->adapter, I2C_ADDR_DEBUG); i2c_set_clientdata(max77705->debug, max77705); disable_irq(max77705->irq); ret = max77705_irq_init(max77705); if (ret < 0) goto err_irq_init; ret = mfd_add_devices(max77705->dev, -1, max77705_devs, ARRAY_SIZE(max77705_devs), NULL, 0, NULL); if (ret < 0) goto err_mfd; device_init_wakeup(max77705->dev, pdata->wakeup); return ret; err_mfd: mfd_remove_devices(max77705->dev); err_irq_init: i2c_unregister_device(max77705->muic); i2c_unregister_device(max77705->charger); i2c_unregister_device(max77705->fuelgauge); i2c_unregister_device(max77705->debug); err_w_lock: mutex_destroy(&max77705->i2c_lock); err: kfree(max77705); return ret; } static int max77705_i2c_remove(struct i2c_client *i2c) { struct max77705_dev *max77705 = i2c_get_clientdata(i2c); device_init_wakeup(max77705->dev, 0); max77705_irq_exit(max77705); mfd_remove_devices(max77705->dev); i2c_unregister_device(max77705->muic); i2c_unregister_device(max77705->charger); i2c_unregister_device(max77705->fuelgauge); i2c_unregister_device(max77705->debug); kfree(max77705); return 0; } static const struct i2c_device_id max77705_i2c_id[] = { { MFD_DEV_NAME, TYPE_MAX77705 }, { } }; MODULE_DEVICE_TABLE(i2c, max77705_i2c_id); #if defined(CONFIG_OF) static const struct of_device_id max77705_i2c_dt_ids[] = { { .compatible = "maxim,max77705" }, { }, }; MODULE_DEVICE_TABLE(of, max77705_i2c_dt_ids); #endif /* CONFIG_OF */ #if defined(CONFIG_PM) static int max77705_suspend(struct device *dev) { struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); struct max77705_dev *max77705 = i2c_get_clientdata(i2c); pr_info("%s:%s\n", MFD_DEV_NAME, __func__); if (device_may_wakeup(dev)) enable_irq_wake(max77705->irq); disable_irq(max77705->irq); return 0; } static int max77705_resume(struct device *dev) { struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); struct max77705_dev *max77705 = i2c_get_clientdata(i2c); pr_info("%s:%s\n", MFD_DEV_NAME, __func__); if (device_may_wakeup(dev)) disable_irq_wake(max77705->irq); enable_irq(max77705->irq); return 0; } #else #define max77705_suspend NULL #define max77705_resume NULL #endif /* CONFIG_PM */ #ifdef CONFIG_HIBERNATION #if 0 u8 max77705_dumpaddr_pmic[] = { #if 0 MAX77705_LED_REG_IFLASH, MAX77705_LED_REG_IFLASH1, MAX77705_LED_REG_IFLASH2, MAX77705_LED_REG_ITORCH, MAX77705_LED_REG_ITORCHTORCHTIMER, MAX77705_LED_REG_FLASH_TIMER, MAX77705_LED_REG_FLASH_EN, MAX77705_LED_REG_MAX_FLASH1, MAX77705_LED_REG_MAX_FLASH2, MAX77705_LED_REG_VOUT_CNTL, MAX77705_LED_REG_VOUT_FLASH, MAX77705_LED_REG_VOUT_FLASH1, MAX77705_LED_REG_FLASH_INT_STATUS, #endif MAX77705_PMIC_REG_PMICID1, MAX77705_PMIC_REG_PMICREV, MAX77705_PMIC_REG_MAINCTRL1, MAX77705_PMIC_REG_MCONFIG, }; #endif u8 max77705_dumpaddr_muic[] = { MAX77705_MUIC_REG_INTMASK_MAIN, MAX77705_MUIC_REG_INTMASK_BC, MAX77705_MUIC_REG_INTMASK_FC, MAX77705_MUIC_REG_INTMASK_GP, MAX77705_MUIC_REG_STATUS1_BC, MAX77705_MUIC_REG_STATUS2_BC, MAX77705_MUIC_REG_STATUS_GP, MAX77705_MUIC_REG_CONTROL1_BC, MAX77705_MUIC_REG_CONTROL2_BC, MAX77705_MUIC_REG_CONTROL1, MAX77705_MUIC_REG_CONTROL2, MAX77705_MUIC_REG_CONTROL3, MAX77705_MUIC_REG_CONTROL4, MAX77705_MUIC_REG_HVCONTROL1, MAX77705_MUIC_REG_HVCONTROL2, }; #if 0 u8 max77705_dumpaddr_haptic[] = { MAX77705_HAPTIC_REG_CONFIG1, MAX77705_HAPTIC_REG_CONFIG2, MAX77705_HAPTIC_REG_CONFIG_CHNL, MAX77705_HAPTIC_REG_CONFG_CYC1, MAX77705_HAPTIC_REG_CONFG_CYC2, MAX77705_HAPTIC_REG_CONFIG_PER1, MAX77705_HAPTIC_REG_CONFIG_PER2, MAX77705_HAPTIC_REG_CONFIG_PER3, MAX77705_HAPTIC_REG_CONFIG_PER4, MAX77705_HAPTIC_REG_CONFIG_DUTY1, MAX77705_HAPTIC_REG_CONFIG_DUTY2, MAX77705_HAPTIC_REG_CONFIG_PWM1, MAX77705_HAPTIC_REG_CONFIG_PWM2, MAX77705_HAPTIC_REG_CONFIG_PWM3, MAX77705_HAPTIC_REG_CONFIG_PWM4, }; #endif u8 max77705_dumpaddr_led[] = { MAX77705_RGBLED_REG_LEDEN, MAX77705_RGBLED_REG_LED0BRT, MAX77705_RGBLED_REG_LED1BRT, MAX77705_RGBLED_REG_LED2BRT, MAX77705_RGBLED_REG_LED3BRT, MAX77705_RGBLED_REG_LEDBLNK, MAX77705_RGBLED_REG_LEDRMP, }; static int max77705_freeze(struct device *dev) { struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); struct max77705_dev *max77705 = i2c_get_clientdata(i2c); int i; for (i = 0; i < ARRAY_SIZE(max77705_dumpaddr_pmic); i++) max77705_read_reg(i2c, max77705_dumpaddr_pmic[i], &max77705->reg_pmic_dump[i]); for (i = 0; i < ARRAY_SIZE(max77705_dumpaddr_muic); i++) max77705_read_reg(i2c, max77705_dumpaddr_muic[i], &max77705->reg_muic_dump[i]); for (i = 0; i < ARRAY_SIZE(max77705_dumpaddr_led); i++) max77705_read_reg(i2c, max77705_dumpaddr_led[i], &max77705->reg_led_dump[i]); disable_irq(max77705->irq); return 0; } static int max77705_restore(struct device *dev) { struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); struct max77705_dev *max77705 = i2c_get_clientdata(i2c); int i; enable_irq(max77705->irq); for (i = 0; i < ARRAY_SIZE(max77705_dumpaddr_pmic); i++) max77705_write_reg(i2c, max77705_dumpaddr_pmic[i], max77705->reg_pmic_dump[i]); for (i = 0; i < ARRAY_SIZE(max77705_dumpaddr_muic); i++) max77705_write_reg(i2c, max77705_dumpaddr_muic[i], max77705->reg_muic_dump[i]); for (i = 0; i < ARRAY_SIZE(max77705_dumpaddr_led); i++) max77705_write_reg(i2c, max77705_dumpaddr_led[i], max77705->reg_led_dump[i]); return 0; } #endif const struct dev_pm_ops max77705_pm = { .suspend = max77705_suspend, .resume = max77705_resume, #ifdef CONFIG_HIBERNATION .freeze = max77705_freeze, .thaw = max77705_restore, .restore = max77705_restore, #endif }; static struct i2c_driver max77705_i2c_driver = { .driver = { .name = MFD_DEV_NAME, .owner = THIS_MODULE, #if defined(CONFIG_PM) .pm = &max77705_pm, #endif /* CONFIG_PM */ #if defined(CONFIG_OF) .of_match_table = max77705_i2c_dt_ids, #endif /* CONFIG_OF */ }, .probe = max77705_i2c_probe, .remove = max77705_i2c_remove, .id_table = max77705_i2c_id, }; static int __init max77705_i2c_init(void) { pr_info("%s:%s\n", MFD_DEV_NAME, __func__); return i2c_add_driver(&max77705_i2c_driver); } /* init early so consumer devices can complete system boot */ subsys_initcall(max77705_i2c_init); static void __exit max77705_i2c_exit(void) { i2c_del_driver(&max77705_i2c_driver); } module_exit(max77705_i2c_exit); MODULE_DESCRIPTION("MAXIM 77705 multi-function core driver"); MODULE_AUTHOR("Insun Choi "); MODULE_LICENSE("GPL");