162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 262306a36Sopenharmony_ci// 362306a36Sopenharmony_ci// max14577.c - mfd core driver for the Maxim 14577/77836 462306a36Sopenharmony_ci// 562306a36Sopenharmony_ci// Copyright (C) 2014 Samsung Electronics 662306a36Sopenharmony_ci// Chanwoo Choi <cw00.choi@samsung.com> 762306a36Sopenharmony_ci// Krzysztof Kozlowski <krzk@kernel.org> 862306a36Sopenharmony_ci// 962306a36Sopenharmony_ci// This driver is based on max8997.c 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#include <linux/err.h> 1262306a36Sopenharmony_ci#include <linux/module.h> 1362306a36Sopenharmony_ci#include <linux/interrupt.h> 1462306a36Sopenharmony_ci#include <linux/of_device.h> 1562306a36Sopenharmony_ci#include <linux/mfd/core.h> 1662306a36Sopenharmony_ci#include <linux/mfd/max14577.h> 1762306a36Sopenharmony_ci#include <linux/mfd/max14577-private.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci/* 2062306a36Sopenharmony_ci * Table of valid charger currents for different Maxim chipsets. 2162306a36Sopenharmony_ci * It is placed here because it is used by both charger and regulator driver. 2262306a36Sopenharmony_ci */ 2362306a36Sopenharmony_ciconst struct maxim_charger_current maxim_charger_currents[] = { 2462306a36Sopenharmony_ci [MAXIM_DEVICE_TYPE_UNKNOWN] = { 0, 0, 0, 0 }, 2562306a36Sopenharmony_ci [MAXIM_DEVICE_TYPE_MAX14577] = { 2662306a36Sopenharmony_ci .min = MAX14577_CHARGER_CURRENT_LIMIT_MIN, 2762306a36Sopenharmony_ci .high_start = MAX14577_CHARGER_CURRENT_LIMIT_HIGH_START, 2862306a36Sopenharmony_ci .high_step = MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP, 2962306a36Sopenharmony_ci .max = MAX14577_CHARGER_CURRENT_LIMIT_MAX, 3062306a36Sopenharmony_ci }, 3162306a36Sopenharmony_ci [MAXIM_DEVICE_TYPE_MAX77836] = { 3262306a36Sopenharmony_ci .min = MAX77836_CHARGER_CURRENT_LIMIT_MIN, 3362306a36Sopenharmony_ci .high_start = MAX77836_CHARGER_CURRENT_LIMIT_HIGH_START, 3462306a36Sopenharmony_ci .high_step = MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP, 3562306a36Sopenharmony_ci .max = MAX77836_CHARGER_CURRENT_LIMIT_MAX, 3662306a36Sopenharmony_ci }, 3762306a36Sopenharmony_ci}; 3862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(maxim_charger_currents); 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci/* 4162306a36Sopenharmony_ci * maxim_charger_calc_reg_current - Calculate register value for current 4262306a36Sopenharmony_ci * @limits: constraints for charger, matching the MBCICHWRC register 4362306a36Sopenharmony_ci * @min_ua: minimal requested current, micro Amps 4462306a36Sopenharmony_ci * @max_ua: maximum requested current, micro Amps 4562306a36Sopenharmony_ci * @dst: destination to store calculated register value 4662306a36Sopenharmony_ci * 4762306a36Sopenharmony_ci * Calculates the value of MBCICHWRC (Fast Battery Charge Current) register 4862306a36Sopenharmony_ci * for given current and stores it under pointed 'dst'. The stored value 4962306a36Sopenharmony_ci * combines low bit (MBCICHWRCL) and high bits (MBCICHWRCH). It is also 5062306a36Sopenharmony_ci * properly shifted. 5162306a36Sopenharmony_ci * 5262306a36Sopenharmony_ci * The calculated register value matches the current which: 5362306a36Sopenharmony_ci * - is always between <limits.min, limits.max>; 5462306a36Sopenharmony_ci * - is always less or equal to max_ua; 5562306a36Sopenharmony_ci * - is the highest possible value; 5662306a36Sopenharmony_ci * - may be lower than min_ua. 5762306a36Sopenharmony_ci * 5862306a36Sopenharmony_ci * On success returns 0. On error returns -EINVAL (requested min/max current 5962306a36Sopenharmony_ci * is outside of given charger limits) and 'dst' is not set. 6062306a36Sopenharmony_ci */ 6162306a36Sopenharmony_ciint maxim_charger_calc_reg_current(const struct maxim_charger_current *limits, 6262306a36Sopenharmony_ci unsigned int min_ua, unsigned int max_ua, u8 *dst) 6362306a36Sopenharmony_ci{ 6462306a36Sopenharmony_ci unsigned int current_bits; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci if (min_ua > max_ua) 6762306a36Sopenharmony_ci return -EINVAL; 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci if (min_ua > limits->max || max_ua < limits->min) 7062306a36Sopenharmony_ci return -EINVAL; 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci if (max_ua < limits->high_start) { 7362306a36Sopenharmony_ci /* 7462306a36Sopenharmony_ci * Less than high_start, so set the minimal current 7562306a36Sopenharmony_ci * (turn Low Bit off, 0 as high bits). 7662306a36Sopenharmony_ci */ 7762306a36Sopenharmony_ci *dst = 0x0; 7862306a36Sopenharmony_ci return 0; 7962306a36Sopenharmony_ci } 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci /* max_ua is in range: <high_start, infinite>, cut it to limits.max */ 8262306a36Sopenharmony_ci max_ua = min(limits->max, max_ua); 8362306a36Sopenharmony_ci max_ua -= limits->high_start; 8462306a36Sopenharmony_ci /* 8562306a36Sopenharmony_ci * There is no risk of overflow 'max_ua' here because: 8662306a36Sopenharmony_ci * - max_ua >= limits.high_start 8762306a36Sopenharmony_ci * - BUILD_BUG checks that 'limits' are: max >= high_start + high_step 8862306a36Sopenharmony_ci */ 8962306a36Sopenharmony_ci current_bits = max_ua / limits->high_step; 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci /* Turn Low Bit on (use range <limits.high_start, limits.max>) ... */ 9262306a36Sopenharmony_ci *dst = 0x1 << CHGCTRL4_MBCICHWRCL_SHIFT; 9362306a36Sopenharmony_ci /* and set proper High Bits */ 9462306a36Sopenharmony_ci *dst |= current_bits << CHGCTRL4_MBCICHWRCH_SHIFT; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci return 0; 9762306a36Sopenharmony_ci} 9862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(maxim_charger_calc_reg_current); 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_cistatic const struct mfd_cell max14577_devs[] = { 10162306a36Sopenharmony_ci { 10262306a36Sopenharmony_ci .name = "max14577-muic", 10362306a36Sopenharmony_ci .of_compatible = "maxim,max14577-muic", 10462306a36Sopenharmony_ci }, 10562306a36Sopenharmony_ci { 10662306a36Sopenharmony_ci .name = "max14577-regulator", 10762306a36Sopenharmony_ci .of_compatible = "maxim,max14577-regulator", 10862306a36Sopenharmony_ci }, 10962306a36Sopenharmony_ci { 11062306a36Sopenharmony_ci .name = "max14577-charger", 11162306a36Sopenharmony_ci .of_compatible = "maxim,max14577-charger", 11262306a36Sopenharmony_ci }, 11362306a36Sopenharmony_ci}; 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_cistatic const struct mfd_cell max77836_devs[] = { 11662306a36Sopenharmony_ci { 11762306a36Sopenharmony_ci .name = "max77836-muic", 11862306a36Sopenharmony_ci .of_compatible = "maxim,max77836-muic", 11962306a36Sopenharmony_ci }, 12062306a36Sopenharmony_ci { 12162306a36Sopenharmony_ci .name = "max77836-regulator", 12262306a36Sopenharmony_ci .of_compatible = "maxim,max77836-regulator", 12362306a36Sopenharmony_ci }, 12462306a36Sopenharmony_ci { 12562306a36Sopenharmony_ci .name = "max77836-charger", 12662306a36Sopenharmony_ci .of_compatible = "maxim,max77836-charger", 12762306a36Sopenharmony_ci }, 12862306a36Sopenharmony_ci { 12962306a36Sopenharmony_ci .name = "max77836-battery", 13062306a36Sopenharmony_ci .of_compatible = "maxim,max77836-battery", 13162306a36Sopenharmony_ci }, 13262306a36Sopenharmony_ci}; 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_cistatic const struct of_device_id max14577_dt_match[] = { 13562306a36Sopenharmony_ci { 13662306a36Sopenharmony_ci .compatible = "maxim,max14577", 13762306a36Sopenharmony_ci .data = (void *)MAXIM_DEVICE_TYPE_MAX14577, 13862306a36Sopenharmony_ci }, 13962306a36Sopenharmony_ci { 14062306a36Sopenharmony_ci .compatible = "maxim,max77836", 14162306a36Sopenharmony_ci .data = (void *)MAXIM_DEVICE_TYPE_MAX77836, 14262306a36Sopenharmony_ci }, 14362306a36Sopenharmony_ci {}, 14462306a36Sopenharmony_ci}; 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_cistatic bool max14577_muic_volatile_reg(struct device *dev, unsigned int reg) 14762306a36Sopenharmony_ci{ 14862306a36Sopenharmony_ci switch (reg) { 14962306a36Sopenharmony_ci case MAX14577_REG_INT1 ... MAX14577_REG_STATUS3: 15062306a36Sopenharmony_ci return true; 15162306a36Sopenharmony_ci default: 15262306a36Sopenharmony_ci break; 15362306a36Sopenharmony_ci } 15462306a36Sopenharmony_ci return false; 15562306a36Sopenharmony_ci} 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_cistatic bool max77836_muic_volatile_reg(struct device *dev, unsigned int reg) 15862306a36Sopenharmony_ci{ 15962306a36Sopenharmony_ci /* Any max14577 volatile registers are also max77836 volatile. */ 16062306a36Sopenharmony_ci if (max14577_muic_volatile_reg(dev, reg)) 16162306a36Sopenharmony_ci return true; 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci switch (reg) { 16462306a36Sopenharmony_ci case MAX77836_FG_REG_VCELL_MSB ... MAX77836_FG_REG_SOC_LSB: 16562306a36Sopenharmony_ci case MAX77836_FG_REG_CRATE_MSB ... MAX77836_FG_REG_CRATE_LSB: 16662306a36Sopenharmony_ci case MAX77836_FG_REG_STATUS_H ... MAX77836_FG_REG_STATUS_L: 16762306a36Sopenharmony_ci case MAX77836_PMIC_REG_INTSRC: 16862306a36Sopenharmony_ci case MAX77836_PMIC_REG_TOPSYS_INT: 16962306a36Sopenharmony_ci case MAX77836_PMIC_REG_TOPSYS_STAT: 17062306a36Sopenharmony_ci return true; 17162306a36Sopenharmony_ci default: 17262306a36Sopenharmony_ci break; 17362306a36Sopenharmony_ci } 17462306a36Sopenharmony_ci return false; 17562306a36Sopenharmony_ci} 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_cistatic const struct regmap_config max14577_muic_regmap_config = { 17862306a36Sopenharmony_ci .reg_bits = 8, 17962306a36Sopenharmony_ci .val_bits = 8, 18062306a36Sopenharmony_ci .volatile_reg = max14577_muic_volatile_reg, 18162306a36Sopenharmony_ci .max_register = MAX14577_REG_END, 18262306a36Sopenharmony_ci}; 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_cistatic const struct regmap_config max77836_pmic_regmap_config = { 18562306a36Sopenharmony_ci .reg_bits = 8, 18662306a36Sopenharmony_ci .val_bits = 8, 18762306a36Sopenharmony_ci .volatile_reg = max77836_muic_volatile_reg, 18862306a36Sopenharmony_ci .max_register = MAX77836_PMIC_REG_END, 18962306a36Sopenharmony_ci}; 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_cistatic const struct regmap_irq max14577_irqs[] = { 19262306a36Sopenharmony_ci /* INT1 interrupts */ 19362306a36Sopenharmony_ci { .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, }, 19462306a36Sopenharmony_ci { .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, }, 19562306a36Sopenharmony_ci { .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, }, 19662306a36Sopenharmony_ci /* INT2 interrupts */ 19762306a36Sopenharmony_ci { .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, }, 19862306a36Sopenharmony_ci { .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, }, 19962306a36Sopenharmony_ci { .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, }, 20062306a36Sopenharmony_ci { .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, }, 20162306a36Sopenharmony_ci { .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, }, 20262306a36Sopenharmony_ci /* INT3 interrupts */ 20362306a36Sopenharmony_ci { .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, }, 20462306a36Sopenharmony_ci { .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, }, 20562306a36Sopenharmony_ci { .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, }, 20662306a36Sopenharmony_ci { .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, }, 20762306a36Sopenharmony_ci}; 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_cistatic const struct regmap_irq_chip max14577_irq_chip = { 21062306a36Sopenharmony_ci .name = "max14577", 21162306a36Sopenharmony_ci .status_base = MAX14577_REG_INT1, 21262306a36Sopenharmony_ci .unmask_base = MAX14577_REG_INTMASK1, 21362306a36Sopenharmony_ci .num_regs = 3, 21462306a36Sopenharmony_ci .irqs = max14577_irqs, 21562306a36Sopenharmony_ci .num_irqs = ARRAY_SIZE(max14577_irqs), 21662306a36Sopenharmony_ci}; 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_cistatic const struct regmap_irq max77836_muic_irqs[] = { 21962306a36Sopenharmony_ci /* INT1 interrupts */ 22062306a36Sopenharmony_ci { .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, }, 22162306a36Sopenharmony_ci { .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, }, 22262306a36Sopenharmony_ci { .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, }, 22362306a36Sopenharmony_ci { .reg_offset = 0, .mask = MAX77836_INT1_ADC1K_MASK, }, 22462306a36Sopenharmony_ci /* INT2 interrupts */ 22562306a36Sopenharmony_ci { .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, }, 22662306a36Sopenharmony_ci { .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, }, 22762306a36Sopenharmony_ci { .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, }, 22862306a36Sopenharmony_ci { .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, }, 22962306a36Sopenharmony_ci { .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, }, 23062306a36Sopenharmony_ci { .reg_offset = 1, .mask = MAX77836_INT2_VIDRM_MASK, }, 23162306a36Sopenharmony_ci /* INT3 interrupts */ 23262306a36Sopenharmony_ci { .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, }, 23362306a36Sopenharmony_ci { .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, }, 23462306a36Sopenharmony_ci { .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, }, 23562306a36Sopenharmony_ci { .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, }, 23662306a36Sopenharmony_ci}; 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_cistatic const struct regmap_irq_chip max77836_muic_irq_chip = { 23962306a36Sopenharmony_ci .name = "max77836-muic", 24062306a36Sopenharmony_ci .status_base = MAX14577_REG_INT1, 24162306a36Sopenharmony_ci .unmask_base = MAX14577_REG_INTMASK1, 24262306a36Sopenharmony_ci .num_regs = 3, 24362306a36Sopenharmony_ci .irqs = max77836_muic_irqs, 24462306a36Sopenharmony_ci .num_irqs = ARRAY_SIZE(max77836_muic_irqs), 24562306a36Sopenharmony_ci}; 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_cistatic const struct regmap_irq max77836_pmic_irqs[] = { 24862306a36Sopenharmony_ci { .reg_offset = 0, .mask = MAX77836_TOPSYS_INT_T120C_MASK, }, 24962306a36Sopenharmony_ci { .reg_offset = 0, .mask = MAX77836_TOPSYS_INT_T140C_MASK, }, 25062306a36Sopenharmony_ci}; 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_cistatic const struct regmap_irq_chip max77836_pmic_irq_chip = { 25362306a36Sopenharmony_ci .name = "max77836-pmic", 25462306a36Sopenharmony_ci .status_base = MAX77836_PMIC_REG_TOPSYS_INT, 25562306a36Sopenharmony_ci .mask_base = MAX77836_PMIC_REG_TOPSYS_INT_MASK, 25662306a36Sopenharmony_ci .num_regs = 1, 25762306a36Sopenharmony_ci .irqs = max77836_pmic_irqs, 25862306a36Sopenharmony_ci .num_irqs = ARRAY_SIZE(max77836_pmic_irqs), 25962306a36Sopenharmony_ci}; 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_cistatic void max14577_print_dev_type(struct max14577 *max14577) 26262306a36Sopenharmony_ci{ 26362306a36Sopenharmony_ci u8 reg_data, vendor_id, device_id; 26462306a36Sopenharmony_ci int ret; 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ci ret = max14577_read_reg(max14577->regmap, MAX14577_REG_DEVICEID, 26762306a36Sopenharmony_ci ®_data); 26862306a36Sopenharmony_ci if (ret) { 26962306a36Sopenharmony_ci dev_err(max14577->dev, 27062306a36Sopenharmony_ci "Failed to read DEVICEID register: %d\n", ret); 27162306a36Sopenharmony_ci return; 27262306a36Sopenharmony_ci } 27362306a36Sopenharmony_ci 27462306a36Sopenharmony_ci vendor_id = ((reg_data & DEVID_VENDORID_MASK) >> 27562306a36Sopenharmony_ci DEVID_VENDORID_SHIFT); 27662306a36Sopenharmony_ci device_id = ((reg_data & DEVID_DEVICEID_MASK) >> 27762306a36Sopenharmony_ci DEVID_DEVICEID_SHIFT); 27862306a36Sopenharmony_ci 27962306a36Sopenharmony_ci dev_info(max14577->dev, "Device type: %u (ID: 0x%x, vendor: 0x%x)\n", 28062306a36Sopenharmony_ci max14577->dev_type, device_id, vendor_id); 28162306a36Sopenharmony_ci} 28262306a36Sopenharmony_ci 28362306a36Sopenharmony_ci/* 28462306a36Sopenharmony_ci * Max77836 specific initialization code for driver probe. 28562306a36Sopenharmony_ci * Adds new I2C dummy device, regmap and regmap IRQ chip. 28662306a36Sopenharmony_ci * Unmasks Interrupt Source register. 28762306a36Sopenharmony_ci * 28862306a36Sopenharmony_ci * On success returns 0. 28962306a36Sopenharmony_ci * On failure returns errno and reverts any changes done so far (e.g. remove 29062306a36Sopenharmony_ci * I2C dummy device), except masking the INT SRC register. 29162306a36Sopenharmony_ci */ 29262306a36Sopenharmony_cistatic int max77836_init(struct max14577 *max14577) 29362306a36Sopenharmony_ci{ 29462306a36Sopenharmony_ci int ret; 29562306a36Sopenharmony_ci u8 intsrc_mask; 29662306a36Sopenharmony_ci 29762306a36Sopenharmony_ci max14577->i2c_pmic = i2c_new_dummy_device(max14577->i2c->adapter, 29862306a36Sopenharmony_ci I2C_ADDR_PMIC); 29962306a36Sopenharmony_ci if (IS_ERR(max14577->i2c_pmic)) { 30062306a36Sopenharmony_ci dev_err(max14577->dev, "Failed to register PMIC I2C device\n"); 30162306a36Sopenharmony_ci return PTR_ERR(max14577->i2c_pmic); 30262306a36Sopenharmony_ci } 30362306a36Sopenharmony_ci i2c_set_clientdata(max14577->i2c_pmic, max14577); 30462306a36Sopenharmony_ci 30562306a36Sopenharmony_ci max14577->regmap_pmic = devm_regmap_init_i2c(max14577->i2c_pmic, 30662306a36Sopenharmony_ci &max77836_pmic_regmap_config); 30762306a36Sopenharmony_ci if (IS_ERR(max14577->regmap_pmic)) { 30862306a36Sopenharmony_ci ret = PTR_ERR(max14577->regmap_pmic); 30962306a36Sopenharmony_ci dev_err(max14577->dev, "Failed to allocate PMIC register map: %d\n", 31062306a36Sopenharmony_ci ret); 31162306a36Sopenharmony_ci goto err; 31262306a36Sopenharmony_ci } 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_ci /* Un-mask MAX77836 Interrupt Source register */ 31562306a36Sopenharmony_ci ret = max14577_read_reg(max14577->regmap_pmic, 31662306a36Sopenharmony_ci MAX77836_PMIC_REG_INTSRC_MASK, &intsrc_mask); 31762306a36Sopenharmony_ci if (ret < 0) { 31862306a36Sopenharmony_ci dev_err(max14577->dev, "Failed to read PMIC register\n"); 31962306a36Sopenharmony_ci goto err; 32062306a36Sopenharmony_ci } 32162306a36Sopenharmony_ci 32262306a36Sopenharmony_ci intsrc_mask &= ~(MAX77836_INTSRC_MASK_TOP_INT_MASK); 32362306a36Sopenharmony_ci intsrc_mask &= ~(MAX77836_INTSRC_MASK_MUIC_CHG_INT_MASK); 32462306a36Sopenharmony_ci ret = max14577_write_reg(max14577->regmap_pmic, 32562306a36Sopenharmony_ci MAX77836_PMIC_REG_INTSRC_MASK, intsrc_mask); 32662306a36Sopenharmony_ci if (ret < 0) { 32762306a36Sopenharmony_ci dev_err(max14577->dev, "Failed to write PMIC register\n"); 32862306a36Sopenharmony_ci goto err; 32962306a36Sopenharmony_ci } 33062306a36Sopenharmony_ci 33162306a36Sopenharmony_ci ret = regmap_add_irq_chip(max14577->regmap_pmic, max14577->irq, 33262306a36Sopenharmony_ci IRQF_ONESHOT | IRQF_SHARED, 33362306a36Sopenharmony_ci 0, &max77836_pmic_irq_chip, 33462306a36Sopenharmony_ci &max14577->irq_data_pmic); 33562306a36Sopenharmony_ci if (ret != 0) { 33662306a36Sopenharmony_ci dev_err(max14577->dev, "Failed to request PMIC IRQ %d: %d\n", 33762306a36Sopenharmony_ci max14577->irq, ret); 33862306a36Sopenharmony_ci goto err; 33962306a36Sopenharmony_ci } 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ci return 0; 34262306a36Sopenharmony_ci 34362306a36Sopenharmony_cierr: 34462306a36Sopenharmony_ci i2c_unregister_device(max14577->i2c_pmic); 34562306a36Sopenharmony_ci 34662306a36Sopenharmony_ci return ret; 34762306a36Sopenharmony_ci} 34862306a36Sopenharmony_ci 34962306a36Sopenharmony_ci/* 35062306a36Sopenharmony_ci * Max77836 specific de-initialization code for driver remove. 35162306a36Sopenharmony_ci */ 35262306a36Sopenharmony_cistatic void max77836_remove(struct max14577 *max14577) 35362306a36Sopenharmony_ci{ 35462306a36Sopenharmony_ci regmap_del_irq_chip(max14577->irq, max14577->irq_data_pmic); 35562306a36Sopenharmony_ci i2c_unregister_device(max14577->i2c_pmic); 35662306a36Sopenharmony_ci} 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_cistatic int max14577_i2c_probe(struct i2c_client *i2c) 35962306a36Sopenharmony_ci{ 36062306a36Sopenharmony_ci const struct i2c_device_id *id = i2c_client_get_device_id(i2c); 36162306a36Sopenharmony_ci struct max14577 *max14577; 36262306a36Sopenharmony_ci struct max14577_platform_data *pdata = dev_get_platdata(&i2c->dev); 36362306a36Sopenharmony_ci struct device_node *np = i2c->dev.of_node; 36462306a36Sopenharmony_ci int ret = 0; 36562306a36Sopenharmony_ci const struct regmap_irq_chip *irq_chip; 36662306a36Sopenharmony_ci const struct mfd_cell *mfd_devs; 36762306a36Sopenharmony_ci unsigned int mfd_devs_size; 36862306a36Sopenharmony_ci int irq_flags; 36962306a36Sopenharmony_ci 37062306a36Sopenharmony_ci if (np) { 37162306a36Sopenharmony_ci pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL); 37262306a36Sopenharmony_ci if (!pdata) 37362306a36Sopenharmony_ci return -ENOMEM; 37462306a36Sopenharmony_ci i2c->dev.platform_data = pdata; 37562306a36Sopenharmony_ci } 37662306a36Sopenharmony_ci 37762306a36Sopenharmony_ci if (!pdata) { 37862306a36Sopenharmony_ci dev_err(&i2c->dev, "No platform data found.\n"); 37962306a36Sopenharmony_ci return -EINVAL; 38062306a36Sopenharmony_ci } 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_ci max14577 = devm_kzalloc(&i2c->dev, sizeof(*max14577), GFP_KERNEL); 38362306a36Sopenharmony_ci if (!max14577) 38462306a36Sopenharmony_ci return -ENOMEM; 38562306a36Sopenharmony_ci 38662306a36Sopenharmony_ci i2c_set_clientdata(i2c, max14577); 38762306a36Sopenharmony_ci max14577->dev = &i2c->dev; 38862306a36Sopenharmony_ci max14577->i2c = i2c; 38962306a36Sopenharmony_ci max14577->irq = i2c->irq; 39062306a36Sopenharmony_ci 39162306a36Sopenharmony_ci max14577->regmap = devm_regmap_init_i2c(i2c, 39262306a36Sopenharmony_ci &max14577_muic_regmap_config); 39362306a36Sopenharmony_ci if (IS_ERR(max14577->regmap)) { 39462306a36Sopenharmony_ci ret = PTR_ERR(max14577->regmap); 39562306a36Sopenharmony_ci dev_err(max14577->dev, "Failed to allocate register map: %d\n", 39662306a36Sopenharmony_ci ret); 39762306a36Sopenharmony_ci return ret; 39862306a36Sopenharmony_ci } 39962306a36Sopenharmony_ci 40062306a36Sopenharmony_ci if (np) { 40162306a36Sopenharmony_ci const struct of_device_id *of_id; 40262306a36Sopenharmony_ci 40362306a36Sopenharmony_ci of_id = of_match_device(max14577_dt_match, &i2c->dev); 40462306a36Sopenharmony_ci if (of_id) 40562306a36Sopenharmony_ci max14577->dev_type = (uintptr_t)of_id->data; 40662306a36Sopenharmony_ci } else { 40762306a36Sopenharmony_ci max14577->dev_type = id->driver_data; 40862306a36Sopenharmony_ci } 40962306a36Sopenharmony_ci 41062306a36Sopenharmony_ci max14577_print_dev_type(max14577); 41162306a36Sopenharmony_ci 41262306a36Sopenharmony_ci switch (max14577->dev_type) { 41362306a36Sopenharmony_ci case MAXIM_DEVICE_TYPE_MAX77836: 41462306a36Sopenharmony_ci irq_chip = &max77836_muic_irq_chip; 41562306a36Sopenharmony_ci mfd_devs = max77836_devs; 41662306a36Sopenharmony_ci mfd_devs_size = ARRAY_SIZE(max77836_devs); 41762306a36Sopenharmony_ci irq_flags = IRQF_ONESHOT | IRQF_SHARED; 41862306a36Sopenharmony_ci break; 41962306a36Sopenharmony_ci case MAXIM_DEVICE_TYPE_MAX14577: 42062306a36Sopenharmony_ci default: 42162306a36Sopenharmony_ci irq_chip = &max14577_irq_chip; 42262306a36Sopenharmony_ci mfd_devs = max14577_devs; 42362306a36Sopenharmony_ci mfd_devs_size = ARRAY_SIZE(max14577_devs); 42462306a36Sopenharmony_ci irq_flags = IRQF_ONESHOT; 42562306a36Sopenharmony_ci break; 42662306a36Sopenharmony_ci } 42762306a36Sopenharmony_ci 42862306a36Sopenharmony_ci ret = regmap_add_irq_chip(max14577->regmap, max14577->irq, 42962306a36Sopenharmony_ci irq_flags, 0, irq_chip, 43062306a36Sopenharmony_ci &max14577->irq_data); 43162306a36Sopenharmony_ci if (ret != 0) { 43262306a36Sopenharmony_ci dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n", 43362306a36Sopenharmony_ci max14577->irq, ret); 43462306a36Sopenharmony_ci return ret; 43562306a36Sopenharmony_ci } 43662306a36Sopenharmony_ci 43762306a36Sopenharmony_ci /* Max77836 specific initialization code (additional regmap) */ 43862306a36Sopenharmony_ci if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836) { 43962306a36Sopenharmony_ci ret = max77836_init(max14577); 44062306a36Sopenharmony_ci if (ret < 0) 44162306a36Sopenharmony_ci goto err_max77836; 44262306a36Sopenharmony_ci } 44362306a36Sopenharmony_ci 44462306a36Sopenharmony_ci ret = mfd_add_devices(max14577->dev, -1, mfd_devs, 44562306a36Sopenharmony_ci mfd_devs_size, NULL, 0, NULL); 44662306a36Sopenharmony_ci if (ret < 0) 44762306a36Sopenharmony_ci goto err_mfd; 44862306a36Sopenharmony_ci 44962306a36Sopenharmony_ci device_init_wakeup(max14577->dev, 1); 45062306a36Sopenharmony_ci 45162306a36Sopenharmony_ci return 0; 45262306a36Sopenharmony_ci 45362306a36Sopenharmony_cierr_mfd: 45462306a36Sopenharmony_ci if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836) 45562306a36Sopenharmony_ci max77836_remove(max14577); 45662306a36Sopenharmony_cierr_max77836: 45762306a36Sopenharmony_ci regmap_del_irq_chip(max14577->irq, max14577->irq_data); 45862306a36Sopenharmony_ci 45962306a36Sopenharmony_ci return ret; 46062306a36Sopenharmony_ci} 46162306a36Sopenharmony_ci 46262306a36Sopenharmony_cistatic void max14577_i2c_remove(struct i2c_client *i2c) 46362306a36Sopenharmony_ci{ 46462306a36Sopenharmony_ci struct max14577 *max14577 = i2c_get_clientdata(i2c); 46562306a36Sopenharmony_ci 46662306a36Sopenharmony_ci mfd_remove_devices(max14577->dev); 46762306a36Sopenharmony_ci regmap_del_irq_chip(max14577->irq, max14577->irq_data); 46862306a36Sopenharmony_ci if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836) 46962306a36Sopenharmony_ci max77836_remove(max14577); 47062306a36Sopenharmony_ci} 47162306a36Sopenharmony_ci 47262306a36Sopenharmony_cistatic const struct i2c_device_id max14577_i2c_id[] = { 47362306a36Sopenharmony_ci { "max14577", MAXIM_DEVICE_TYPE_MAX14577, }, 47462306a36Sopenharmony_ci { "max77836", MAXIM_DEVICE_TYPE_MAX77836, }, 47562306a36Sopenharmony_ci { } 47662306a36Sopenharmony_ci}; 47762306a36Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, max14577_i2c_id); 47862306a36Sopenharmony_ci 47962306a36Sopenharmony_cistatic int max14577_suspend(struct device *dev) 48062306a36Sopenharmony_ci{ 48162306a36Sopenharmony_ci struct i2c_client *i2c = to_i2c_client(dev); 48262306a36Sopenharmony_ci struct max14577 *max14577 = i2c_get_clientdata(i2c); 48362306a36Sopenharmony_ci 48462306a36Sopenharmony_ci if (device_may_wakeup(dev)) 48562306a36Sopenharmony_ci enable_irq_wake(max14577->irq); 48662306a36Sopenharmony_ci /* 48762306a36Sopenharmony_ci * MUIC IRQ must be disabled during suspend because if it happens 48862306a36Sopenharmony_ci * while suspended it will be handled before resuming I2C. 48962306a36Sopenharmony_ci * 49062306a36Sopenharmony_ci * When device is woken up from suspend (e.g. by ADC change), 49162306a36Sopenharmony_ci * an interrupt occurs before resuming I2C bus controller. 49262306a36Sopenharmony_ci * Interrupt handler tries to read registers but this read 49362306a36Sopenharmony_ci * will fail because I2C is still suspended. 49462306a36Sopenharmony_ci */ 49562306a36Sopenharmony_ci disable_irq(max14577->irq); 49662306a36Sopenharmony_ci 49762306a36Sopenharmony_ci return 0; 49862306a36Sopenharmony_ci} 49962306a36Sopenharmony_ci 50062306a36Sopenharmony_cistatic int max14577_resume(struct device *dev) 50162306a36Sopenharmony_ci{ 50262306a36Sopenharmony_ci struct i2c_client *i2c = to_i2c_client(dev); 50362306a36Sopenharmony_ci struct max14577 *max14577 = i2c_get_clientdata(i2c); 50462306a36Sopenharmony_ci 50562306a36Sopenharmony_ci if (device_may_wakeup(dev)) 50662306a36Sopenharmony_ci disable_irq_wake(max14577->irq); 50762306a36Sopenharmony_ci enable_irq(max14577->irq); 50862306a36Sopenharmony_ci 50962306a36Sopenharmony_ci return 0; 51062306a36Sopenharmony_ci} 51162306a36Sopenharmony_ci 51262306a36Sopenharmony_cistatic DEFINE_SIMPLE_DEV_PM_OPS(max14577_pm, max14577_suspend, max14577_resume); 51362306a36Sopenharmony_ci 51462306a36Sopenharmony_cistatic struct i2c_driver max14577_i2c_driver = { 51562306a36Sopenharmony_ci .driver = { 51662306a36Sopenharmony_ci .name = "max14577", 51762306a36Sopenharmony_ci .pm = pm_sleep_ptr(&max14577_pm), 51862306a36Sopenharmony_ci .of_match_table = max14577_dt_match, 51962306a36Sopenharmony_ci }, 52062306a36Sopenharmony_ci .probe = max14577_i2c_probe, 52162306a36Sopenharmony_ci .remove = max14577_i2c_remove, 52262306a36Sopenharmony_ci .id_table = max14577_i2c_id, 52362306a36Sopenharmony_ci}; 52462306a36Sopenharmony_ci 52562306a36Sopenharmony_cistatic int __init max14577_i2c_init(void) 52662306a36Sopenharmony_ci{ 52762306a36Sopenharmony_ci BUILD_BUG_ON(ARRAY_SIZE(max14577_i2c_id) != MAXIM_DEVICE_TYPE_NUM); 52862306a36Sopenharmony_ci BUILD_BUG_ON(ARRAY_SIZE(max14577_dt_match) != MAXIM_DEVICE_TYPE_NUM); 52962306a36Sopenharmony_ci 53062306a36Sopenharmony_ci /* Valid charger current values must be provided for each chipset */ 53162306a36Sopenharmony_ci BUILD_BUG_ON(ARRAY_SIZE(maxim_charger_currents) != MAXIM_DEVICE_TYPE_NUM); 53262306a36Sopenharmony_ci 53362306a36Sopenharmony_ci /* Check for valid values for charger */ 53462306a36Sopenharmony_ci BUILD_BUG_ON(MAX14577_CHARGER_CURRENT_LIMIT_HIGH_START + 53562306a36Sopenharmony_ci MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP * 0xf != 53662306a36Sopenharmony_ci MAX14577_CHARGER_CURRENT_LIMIT_MAX); 53762306a36Sopenharmony_ci BUILD_BUG_ON(MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP == 0); 53862306a36Sopenharmony_ci 53962306a36Sopenharmony_ci BUILD_BUG_ON(MAX77836_CHARGER_CURRENT_LIMIT_HIGH_START + 54062306a36Sopenharmony_ci MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP * 0xf != 54162306a36Sopenharmony_ci MAX77836_CHARGER_CURRENT_LIMIT_MAX); 54262306a36Sopenharmony_ci BUILD_BUG_ON(MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP == 0); 54362306a36Sopenharmony_ci 54462306a36Sopenharmony_ci return i2c_add_driver(&max14577_i2c_driver); 54562306a36Sopenharmony_ci} 54662306a36Sopenharmony_cimodule_init(max14577_i2c_init); 54762306a36Sopenharmony_ci 54862306a36Sopenharmony_cistatic void __exit max14577_i2c_exit(void) 54962306a36Sopenharmony_ci{ 55062306a36Sopenharmony_ci i2c_del_driver(&max14577_i2c_driver); 55162306a36Sopenharmony_ci} 55262306a36Sopenharmony_cimodule_exit(max14577_i2c_exit); 55362306a36Sopenharmony_ci 55462306a36Sopenharmony_ciMODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <krzk@kernel.org>"); 55562306a36Sopenharmony_ciMODULE_DESCRIPTION("Maxim 14577/77836 multi-function core driver"); 55662306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 557