1*ed2f4c74SNaresh Solanki // SPDX-License-Identifier: GPL-2.0 2*ed2f4c74SNaresh Solanki /* 3*ed2f4c74SNaresh Solanki * Device driver for regulators in MAX5970 and MAX5978 IC 4*ed2f4c74SNaresh Solanki * 5*ed2f4c74SNaresh Solanki * Copyright (c) 2022 9elements GmbH 6*ed2f4c74SNaresh Solanki * 7*ed2f4c74SNaresh Solanki * Author: Patrick Rudolph <patrick.rudolph@9elements.com> 8*ed2f4c74SNaresh Solanki */ 9*ed2f4c74SNaresh Solanki 10*ed2f4c74SNaresh Solanki #include <linux/bitops.h> 11*ed2f4c74SNaresh Solanki #include <linux/device.h> 12*ed2f4c74SNaresh Solanki #include <linux/err.h> 13*ed2f4c74SNaresh Solanki #include <linux/module.h> 14*ed2f4c74SNaresh Solanki #include <linux/io.h> 15*ed2f4c74SNaresh Solanki #include <linux/of.h> 16*ed2f4c74SNaresh Solanki #include <linux/i2c.h> 17*ed2f4c74SNaresh Solanki #include <linux/regmap.h> 18*ed2f4c74SNaresh Solanki #include <linux/regulator/driver.h> 19*ed2f4c74SNaresh Solanki #include <linux/regulator/machine.h> 20*ed2f4c74SNaresh Solanki #include <linux/regulator/of_regulator.h> 21*ed2f4c74SNaresh Solanki #include <linux/platform_device.h> 22*ed2f4c74SNaresh Solanki 23*ed2f4c74SNaresh Solanki #include <linux/mfd/max5970.h> 24*ed2f4c74SNaresh Solanki 25*ed2f4c74SNaresh Solanki struct max5970_regulator { 26*ed2f4c74SNaresh Solanki int num_switches, mon_rng, irng, shunt_micro_ohms, lim_uA; 27*ed2f4c74SNaresh Solanki struct regmap *regmap; 28*ed2f4c74SNaresh Solanki }; 29*ed2f4c74SNaresh Solanki 30*ed2f4c74SNaresh Solanki enum max597x_regulator_id { 31*ed2f4c74SNaresh Solanki MAX597X_SW0, 32*ed2f4c74SNaresh Solanki MAX597X_SW1, 33*ed2f4c74SNaresh Solanki }; 34*ed2f4c74SNaresh Solanki 35*ed2f4c74SNaresh Solanki static int max597x_uvp_ovp_check_mode(struct regulator_dev *rdev, int severity) 36*ed2f4c74SNaresh Solanki { 37*ed2f4c74SNaresh Solanki int ret, reg; 38*ed2f4c74SNaresh Solanki 39*ed2f4c74SNaresh Solanki /* Status1 register contains the soft strap values sampled at POR */ 40*ed2f4c74SNaresh Solanki ret = regmap_read(rdev->regmap, MAX5970_REG_STATUS1, ®); 41*ed2f4c74SNaresh Solanki if (ret) 42*ed2f4c74SNaresh Solanki return ret; 43*ed2f4c74SNaresh Solanki 44*ed2f4c74SNaresh Solanki /* Check soft straps match requested mode */ 45*ed2f4c74SNaresh Solanki if (severity == REGULATOR_SEVERITY_PROT) { 46*ed2f4c74SNaresh Solanki if (STATUS1_PROT(reg) != STATUS1_PROT_SHUTDOWN) 47*ed2f4c74SNaresh Solanki return -EOPNOTSUPP; 48*ed2f4c74SNaresh Solanki 49*ed2f4c74SNaresh Solanki return 0; 50*ed2f4c74SNaresh Solanki } 51*ed2f4c74SNaresh Solanki if (STATUS1_PROT(reg) == STATUS1_PROT_SHUTDOWN) 52*ed2f4c74SNaresh Solanki return -EOPNOTSUPP; 53*ed2f4c74SNaresh Solanki 54*ed2f4c74SNaresh Solanki return 0; 55*ed2f4c74SNaresh Solanki } 56*ed2f4c74SNaresh Solanki 57*ed2f4c74SNaresh Solanki static int max597x_set_vp(struct regulator_dev *rdev, int lim_uV, int severity, 58*ed2f4c74SNaresh Solanki bool enable, bool overvoltage) 59*ed2f4c74SNaresh Solanki { 60*ed2f4c74SNaresh Solanki int off_h, off_l, reg, ret; 61*ed2f4c74SNaresh Solanki struct max5970_regulator *data = rdev_get_drvdata(rdev); 62*ed2f4c74SNaresh Solanki int channel = rdev_get_id(rdev); 63*ed2f4c74SNaresh Solanki 64*ed2f4c74SNaresh Solanki if (overvoltage) { 65*ed2f4c74SNaresh Solanki if (severity == REGULATOR_SEVERITY_WARN) { 66*ed2f4c74SNaresh Solanki off_h = MAX5970_REG_CH_OV_WARN_H(channel); 67*ed2f4c74SNaresh Solanki off_l = MAX5970_REG_CH_OV_WARN_L(channel); 68*ed2f4c74SNaresh Solanki } else { 69*ed2f4c74SNaresh Solanki off_h = MAX5970_REG_CH_OV_CRIT_H(channel); 70*ed2f4c74SNaresh Solanki off_l = MAX5970_REG_CH_OV_CRIT_L(channel); 71*ed2f4c74SNaresh Solanki } 72*ed2f4c74SNaresh Solanki } else { 73*ed2f4c74SNaresh Solanki if (severity == REGULATOR_SEVERITY_WARN) { 74*ed2f4c74SNaresh Solanki off_h = MAX5970_REG_CH_UV_WARN_H(channel); 75*ed2f4c74SNaresh Solanki off_l = MAX5970_REG_CH_UV_WARN_L(channel); 76*ed2f4c74SNaresh Solanki } else { 77*ed2f4c74SNaresh Solanki off_h = MAX5970_REG_CH_UV_CRIT_H(channel); 78*ed2f4c74SNaresh Solanki off_l = MAX5970_REG_CH_UV_CRIT_L(channel); 79*ed2f4c74SNaresh Solanki } 80*ed2f4c74SNaresh Solanki } 81*ed2f4c74SNaresh Solanki 82*ed2f4c74SNaresh Solanki if (enable) 83*ed2f4c74SNaresh Solanki /* reg = ADC_MASK * (lim_uV / 1000000) / (data->mon_rng / 1000000) */ 84*ed2f4c74SNaresh Solanki reg = ADC_MASK * lim_uV / data->mon_rng; 85*ed2f4c74SNaresh Solanki else 86*ed2f4c74SNaresh Solanki reg = 0; 87*ed2f4c74SNaresh Solanki 88*ed2f4c74SNaresh Solanki ret = regmap_write(rdev->regmap, off_h, MAX5970_VAL2REG_H(reg)); 89*ed2f4c74SNaresh Solanki if (ret) 90*ed2f4c74SNaresh Solanki return ret; 91*ed2f4c74SNaresh Solanki 92*ed2f4c74SNaresh Solanki ret = regmap_write(rdev->regmap, off_l, MAX5970_VAL2REG_L(reg)); 93*ed2f4c74SNaresh Solanki if (ret) 94*ed2f4c74SNaresh Solanki return ret; 95*ed2f4c74SNaresh Solanki 96*ed2f4c74SNaresh Solanki return 0; 97*ed2f4c74SNaresh Solanki } 98*ed2f4c74SNaresh Solanki 99*ed2f4c74SNaresh Solanki static int max597x_set_uvp(struct regulator_dev *rdev, int lim_uV, int severity, 100*ed2f4c74SNaresh Solanki bool enable) 101*ed2f4c74SNaresh Solanki { 102*ed2f4c74SNaresh Solanki int ret; 103*ed2f4c74SNaresh Solanki 104*ed2f4c74SNaresh Solanki /* 105*ed2f4c74SNaresh Solanki * MAX5970 has enable control as a special value in limit reg. Can't 106*ed2f4c74SNaresh Solanki * set limit but keep feature disabled or enable W/O given limit. 107*ed2f4c74SNaresh Solanki */ 108*ed2f4c74SNaresh Solanki if ((lim_uV && !enable) || (!lim_uV && enable)) 109*ed2f4c74SNaresh Solanki return -EINVAL; 110*ed2f4c74SNaresh Solanki 111*ed2f4c74SNaresh Solanki ret = max597x_uvp_ovp_check_mode(rdev, severity); 112*ed2f4c74SNaresh Solanki if (ret) 113*ed2f4c74SNaresh Solanki return ret; 114*ed2f4c74SNaresh Solanki 115*ed2f4c74SNaresh Solanki return max597x_set_vp(rdev, lim_uV, severity, enable, false); 116*ed2f4c74SNaresh Solanki } 117*ed2f4c74SNaresh Solanki 118*ed2f4c74SNaresh Solanki static int max597x_set_ovp(struct regulator_dev *rdev, int lim_uV, int severity, 119*ed2f4c74SNaresh Solanki bool enable) 120*ed2f4c74SNaresh Solanki { 121*ed2f4c74SNaresh Solanki int ret; 122*ed2f4c74SNaresh Solanki 123*ed2f4c74SNaresh Solanki /* 124*ed2f4c74SNaresh Solanki * MAX5970 has enable control as a special value in limit reg. Can't 125*ed2f4c74SNaresh Solanki * set limit but keep feature disabled or enable W/O given limit. 126*ed2f4c74SNaresh Solanki */ 127*ed2f4c74SNaresh Solanki if ((lim_uV && !enable) || (!lim_uV && enable)) 128*ed2f4c74SNaresh Solanki return -EINVAL; 129*ed2f4c74SNaresh Solanki 130*ed2f4c74SNaresh Solanki ret = max597x_uvp_ovp_check_mode(rdev, severity); 131*ed2f4c74SNaresh Solanki if (ret) 132*ed2f4c74SNaresh Solanki return ret; 133*ed2f4c74SNaresh Solanki 134*ed2f4c74SNaresh Solanki return max597x_set_vp(rdev, lim_uV, severity, enable, true); 135*ed2f4c74SNaresh Solanki } 136*ed2f4c74SNaresh Solanki 137*ed2f4c74SNaresh Solanki static int max597x_set_ocp(struct regulator_dev *rdev, int lim_uA, 138*ed2f4c74SNaresh Solanki int severity, bool enable) 139*ed2f4c74SNaresh Solanki { 140*ed2f4c74SNaresh Solanki int val, reg; 141*ed2f4c74SNaresh Solanki unsigned int vthst, vthfst; 142*ed2f4c74SNaresh Solanki 143*ed2f4c74SNaresh Solanki struct max5970_regulator *data = rdev_get_drvdata(rdev); 144*ed2f4c74SNaresh Solanki int rdev_id = rdev_get_id(rdev); 145*ed2f4c74SNaresh Solanki /* 146*ed2f4c74SNaresh Solanki * MAX5970 doesn't has enable control for ocp. 147*ed2f4c74SNaresh Solanki * If limit is specified but enable is not set then hold the value in 148*ed2f4c74SNaresh Solanki * variable & later use it when ocp needs to be enabled. 149*ed2f4c74SNaresh Solanki */ 150*ed2f4c74SNaresh Solanki if (lim_uA != 0 && lim_uA != data->lim_uA) 151*ed2f4c74SNaresh Solanki data->lim_uA = lim_uA; 152*ed2f4c74SNaresh Solanki 153*ed2f4c74SNaresh Solanki if (severity != REGULATOR_SEVERITY_PROT) 154*ed2f4c74SNaresh Solanki return -EINVAL; 155*ed2f4c74SNaresh Solanki 156*ed2f4c74SNaresh Solanki if (enable) { 157*ed2f4c74SNaresh Solanki 158*ed2f4c74SNaresh Solanki /* Calc Vtrip threshold in uV. */ 159*ed2f4c74SNaresh Solanki vthst = 160*ed2f4c74SNaresh Solanki div_u64(mul_u32_u32(data->shunt_micro_ohms, data->lim_uA), 161*ed2f4c74SNaresh Solanki 1000000); 162*ed2f4c74SNaresh Solanki 163*ed2f4c74SNaresh Solanki /* 164*ed2f4c74SNaresh Solanki * As recommended in datasheed, add 20% margin to avoid 165*ed2f4c74SNaresh Solanki * spurious event & passive component tolerance. 166*ed2f4c74SNaresh Solanki */ 167*ed2f4c74SNaresh Solanki vthst = div_u64(mul_u32_u32(vthst, 120), 100); 168*ed2f4c74SNaresh Solanki 169*ed2f4c74SNaresh Solanki /* Calc fast Vtrip threshold in uV */ 170*ed2f4c74SNaresh Solanki vthfst = vthst * (MAX5970_FAST2SLOW_RATIO / 100); 171*ed2f4c74SNaresh Solanki 172*ed2f4c74SNaresh Solanki if (vthfst > data->irng) { 173*ed2f4c74SNaresh Solanki dev_err(&rdev->dev, "Current limit out of range\n"); 174*ed2f4c74SNaresh Solanki return -EINVAL; 175*ed2f4c74SNaresh Solanki } 176*ed2f4c74SNaresh Solanki /* Fast trip threshold to be programmed */ 177*ed2f4c74SNaresh Solanki val = div_u64(mul_u32_u32(0xFF, vthfst), data->irng); 178*ed2f4c74SNaresh Solanki } else 179*ed2f4c74SNaresh Solanki /* 180*ed2f4c74SNaresh Solanki * Since there is no option to disable ocp, set limit to max 181*ed2f4c74SNaresh Solanki * value 182*ed2f4c74SNaresh Solanki */ 183*ed2f4c74SNaresh Solanki val = 0xFF; 184*ed2f4c74SNaresh Solanki 185*ed2f4c74SNaresh Solanki reg = MAX5970_REG_DAC_FAST(rdev_id); 186*ed2f4c74SNaresh Solanki 187*ed2f4c74SNaresh Solanki return regmap_write(rdev->regmap, reg, val); 188*ed2f4c74SNaresh Solanki } 189*ed2f4c74SNaresh Solanki 190*ed2f4c74SNaresh Solanki static int max597x_get_status(struct regulator_dev *rdev) 191*ed2f4c74SNaresh Solanki { 192*ed2f4c74SNaresh Solanki int val, ret; 193*ed2f4c74SNaresh Solanki 194*ed2f4c74SNaresh Solanki ret = regmap_read(rdev->regmap, MAX5970_REG_STATUS3, &val); 195*ed2f4c74SNaresh Solanki if (ret) 196*ed2f4c74SNaresh Solanki return ret; 197*ed2f4c74SNaresh Solanki 198*ed2f4c74SNaresh Solanki if (val & MAX5970_STATUS3_ALERT) 199*ed2f4c74SNaresh Solanki return REGULATOR_STATUS_ERROR; 200*ed2f4c74SNaresh Solanki 201*ed2f4c74SNaresh Solanki ret = regulator_is_enabled_regmap(rdev); 202*ed2f4c74SNaresh Solanki if (ret < 0) 203*ed2f4c74SNaresh Solanki return ret; 204*ed2f4c74SNaresh Solanki 205*ed2f4c74SNaresh Solanki if (ret) 206*ed2f4c74SNaresh Solanki return REGULATOR_STATUS_ON; 207*ed2f4c74SNaresh Solanki 208*ed2f4c74SNaresh Solanki return REGULATOR_STATUS_OFF; 209*ed2f4c74SNaresh Solanki } 210*ed2f4c74SNaresh Solanki 211*ed2f4c74SNaresh Solanki static const struct regulator_ops max597x_switch_ops = { 212*ed2f4c74SNaresh Solanki .enable = regulator_enable_regmap, 213*ed2f4c74SNaresh Solanki .disable = regulator_disable_regmap, 214*ed2f4c74SNaresh Solanki .is_enabled = regulator_is_enabled_regmap, 215*ed2f4c74SNaresh Solanki .get_status = max597x_get_status, 216*ed2f4c74SNaresh Solanki .set_over_voltage_protection = max597x_set_ovp, 217*ed2f4c74SNaresh Solanki .set_under_voltage_protection = max597x_set_uvp, 218*ed2f4c74SNaresh Solanki .set_over_current_protection = max597x_set_ocp, 219*ed2f4c74SNaresh Solanki }; 220*ed2f4c74SNaresh Solanki 221*ed2f4c74SNaresh Solanki static int max597x_dt_parse(struct device_node *np, 222*ed2f4c74SNaresh Solanki const struct regulator_desc *desc, 223*ed2f4c74SNaresh Solanki struct regulator_config *cfg) 224*ed2f4c74SNaresh Solanki { 225*ed2f4c74SNaresh Solanki struct max5970_regulator *data = cfg->driver_data; 226*ed2f4c74SNaresh Solanki int ret = 0; 227*ed2f4c74SNaresh Solanki 228*ed2f4c74SNaresh Solanki ret = 229*ed2f4c74SNaresh Solanki of_property_read_u32(np, "shunt-resistor-micro-ohms", 230*ed2f4c74SNaresh Solanki &data->shunt_micro_ohms); 231*ed2f4c74SNaresh Solanki if (ret < 0) 232*ed2f4c74SNaresh Solanki dev_err(cfg->dev, 233*ed2f4c74SNaresh Solanki "property 'shunt-resistor-micro-ohms' not found, err %d\n", 234*ed2f4c74SNaresh Solanki ret); 235*ed2f4c74SNaresh Solanki return ret; 236*ed2f4c74SNaresh Solanki 237*ed2f4c74SNaresh Solanki } 238*ed2f4c74SNaresh Solanki 239*ed2f4c74SNaresh Solanki #define MAX597X_SWITCH(_ID, _ereg, _chan, _supply) { \ 240*ed2f4c74SNaresh Solanki .name = #_ID, \ 241*ed2f4c74SNaresh Solanki .of_match = of_match_ptr(#_ID), \ 242*ed2f4c74SNaresh Solanki .ops = &max597x_switch_ops, \ 243*ed2f4c74SNaresh Solanki .regulators_node = of_match_ptr("regulators"), \ 244*ed2f4c74SNaresh Solanki .type = REGULATOR_VOLTAGE, \ 245*ed2f4c74SNaresh Solanki .id = MAX597X_##_ID, \ 246*ed2f4c74SNaresh Solanki .owner = THIS_MODULE, \ 247*ed2f4c74SNaresh Solanki .supply_name = _supply, \ 248*ed2f4c74SNaresh Solanki .enable_reg = _ereg, \ 249*ed2f4c74SNaresh Solanki .enable_mask = CHXEN((_chan)), \ 250*ed2f4c74SNaresh Solanki .of_parse_cb = max597x_dt_parse, \ 251*ed2f4c74SNaresh Solanki } 252*ed2f4c74SNaresh Solanki 253*ed2f4c74SNaresh Solanki static const struct regulator_desc regulators[] = { 254*ed2f4c74SNaresh Solanki MAX597X_SWITCH(SW0, MAX5970_REG_CHXEN, 0, "vss1"), 255*ed2f4c74SNaresh Solanki MAX597X_SWITCH(SW1, MAX5970_REG_CHXEN, 1, "vss2"), 256*ed2f4c74SNaresh Solanki }; 257*ed2f4c74SNaresh Solanki 258*ed2f4c74SNaresh Solanki static int max597x_regmap_read_clear(struct regmap *map, unsigned int reg, 259*ed2f4c74SNaresh Solanki unsigned int *val) 260*ed2f4c74SNaresh Solanki { 261*ed2f4c74SNaresh Solanki int ret; 262*ed2f4c74SNaresh Solanki 263*ed2f4c74SNaresh Solanki ret = regmap_read(map, reg, val); 264*ed2f4c74SNaresh Solanki if (ret) 265*ed2f4c74SNaresh Solanki return ret; 266*ed2f4c74SNaresh Solanki 267*ed2f4c74SNaresh Solanki if (*val) 268*ed2f4c74SNaresh Solanki return regmap_write(map, reg, *val); 269*ed2f4c74SNaresh Solanki 270*ed2f4c74SNaresh Solanki return 0; 271*ed2f4c74SNaresh Solanki } 272*ed2f4c74SNaresh Solanki 273*ed2f4c74SNaresh Solanki static int max597x_irq_handler(int irq, struct regulator_irq_data *rid, 274*ed2f4c74SNaresh Solanki unsigned long *dev_mask) 275*ed2f4c74SNaresh Solanki { 276*ed2f4c74SNaresh Solanki struct regulator_err_state *stat; 277*ed2f4c74SNaresh Solanki struct max5970_regulator *d = (struct max5970_regulator *)rid->data; 278*ed2f4c74SNaresh Solanki int val, ret, i; 279*ed2f4c74SNaresh Solanki 280*ed2f4c74SNaresh Solanki ret = max597x_regmap_read_clear(d->regmap, MAX5970_REG_FAULT0, &val); 281*ed2f4c74SNaresh Solanki if (ret) 282*ed2f4c74SNaresh Solanki return REGULATOR_FAILED_RETRY; 283*ed2f4c74SNaresh Solanki 284*ed2f4c74SNaresh Solanki *dev_mask = 0; 285*ed2f4c74SNaresh Solanki for (i = 0; i < d->num_switches; i++) { 286*ed2f4c74SNaresh Solanki stat = &rid->states[i]; 287*ed2f4c74SNaresh Solanki stat->notifs = 0; 288*ed2f4c74SNaresh Solanki stat->errors = 0; 289*ed2f4c74SNaresh Solanki } 290*ed2f4c74SNaresh Solanki 291*ed2f4c74SNaresh Solanki for (i = 0; i < d->num_switches; i++) { 292*ed2f4c74SNaresh Solanki stat = &rid->states[i]; 293*ed2f4c74SNaresh Solanki 294*ed2f4c74SNaresh Solanki if (val & UV_STATUS_CRIT(i)) { 295*ed2f4c74SNaresh Solanki *dev_mask |= 1 << i; 296*ed2f4c74SNaresh Solanki stat->notifs |= REGULATOR_EVENT_UNDER_VOLTAGE; 297*ed2f4c74SNaresh Solanki stat->errors |= REGULATOR_ERROR_UNDER_VOLTAGE; 298*ed2f4c74SNaresh Solanki } else if (val & UV_STATUS_WARN(i)) { 299*ed2f4c74SNaresh Solanki *dev_mask |= 1 << i; 300*ed2f4c74SNaresh Solanki stat->notifs |= REGULATOR_EVENT_UNDER_VOLTAGE_WARN; 301*ed2f4c74SNaresh Solanki stat->errors |= REGULATOR_ERROR_UNDER_VOLTAGE_WARN; 302*ed2f4c74SNaresh Solanki } 303*ed2f4c74SNaresh Solanki } 304*ed2f4c74SNaresh Solanki 305*ed2f4c74SNaresh Solanki ret = max597x_regmap_read_clear(d->regmap, MAX5970_REG_FAULT1, &val); 306*ed2f4c74SNaresh Solanki if (ret) 307*ed2f4c74SNaresh Solanki return REGULATOR_FAILED_RETRY; 308*ed2f4c74SNaresh Solanki 309*ed2f4c74SNaresh Solanki for (i = 0; i < d->num_switches; i++) { 310*ed2f4c74SNaresh Solanki stat = &rid->states[i]; 311*ed2f4c74SNaresh Solanki 312*ed2f4c74SNaresh Solanki if (val & OV_STATUS_CRIT(i)) { 313*ed2f4c74SNaresh Solanki *dev_mask |= 1 << i; 314*ed2f4c74SNaresh Solanki stat->notifs |= REGULATOR_EVENT_REGULATION_OUT; 315*ed2f4c74SNaresh Solanki stat->errors |= REGULATOR_ERROR_REGULATION_OUT; 316*ed2f4c74SNaresh Solanki } else if (val & OV_STATUS_WARN(i)) { 317*ed2f4c74SNaresh Solanki *dev_mask |= 1 << i; 318*ed2f4c74SNaresh Solanki stat->notifs |= REGULATOR_EVENT_OVER_VOLTAGE_WARN; 319*ed2f4c74SNaresh Solanki stat->errors |= REGULATOR_ERROR_OVER_VOLTAGE_WARN; 320*ed2f4c74SNaresh Solanki } 321*ed2f4c74SNaresh Solanki } 322*ed2f4c74SNaresh Solanki 323*ed2f4c74SNaresh Solanki ret = max597x_regmap_read_clear(d->regmap, MAX5970_REG_FAULT2, &val); 324*ed2f4c74SNaresh Solanki if (ret) 325*ed2f4c74SNaresh Solanki return REGULATOR_FAILED_RETRY; 326*ed2f4c74SNaresh Solanki 327*ed2f4c74SNaresh Solanki for (i = 0; i < d->num_switches; i++) { 328*ed2f4c74SNaresh Solanki stat = &rid->states[i]; 329*ed2f4c74SNaresh Solanki 330*ed2f4c74SNaresh Solanki if (val & OC_STATUS_WARN(i)) { 331*ed2f4c74SNaresh Solanki *dev_mask |= 1 << i; 332*ed2f4c74SNaresh Solanki stat->notifs |= REGULATOR_EVENT_OVER_CURRENT_WARN; 333*ed2f4c74SNaresh Solanki stat->errors |= REGULATOR_ERROR_OVER_CURRENT_WARN; 334*ed2f4c74SNaresh Solanki } 335*ed2f4c74SNaresh Solanki } 336*ed2f4c74SNaresh Solanki 337*ed2f4c74SNaresh Solanki ret = regmap_read(d->regmap, MAX5970_REG_STATUS0, &val); 338*ed2f4c74SNaresh Solanki if (ret) 339*ed2f4c74SNaresh Solanki return REGULATOR_FAILED_RETRY; 340*ed2f4c74SNaresh Solanki 341*ed2f4c74SNaresh Solanki for (i = 0; i < d->num_switches; i++) { 342*ed2f4c74SNaresh Solanki stat = &rid->states[i]; 343*ed2f4c74SNaresh Solanki 344*ed2f4c74SNaresh Solanki if ((val & MAX5970_CB_IFAULTF(i)) 345*ed2f4c74SNaresh Solanki || (val & MAX5970_CB_IFAULTS(i))) { 346*ed2f4c74SNaresh Solanki *dev_mask |= 1 << i; 347*ed2f4c74SNaresh Solanki stat->notifs |= 348*ed2f4c74SNaresh Solanki REGULATOR_EVENT_OVER_CURRENT | 349*ed2f4c74SNaresh Solanki REGULATOR_EVENT_DISABLE; 350*ed2f4c74SNaresh Solanki stat->errors |= 351*ed2f4c74SNaresh Solanki REGULATOR_ERROR_OVER_CURRENT | REGULATOR_ERROR_FAIL; 352*ed2f4c74SNaresh Solanki 353*ed2f4c74SNaresh Solanki /* Clear the sub-IRQ status */ 354*ed2f4c74SNaresh Solanki regulator_disable_regmap(stat->rdev); 355*ed2f4c74SNaresh Solanki } 356*ed2f4c74SNaresh Solanki } 357*ed2f4c74SNaresh Solanki return 0; 358*ed2f4c74SNaresh Solanki } 359*ed2f4c74SNaresh Solanki 360*ed2f4c74SNaresh Solanki static int max597x_adc_range(struct regmap *regmap, const int ch, 361*ed2f4c74SNaresh Solanki u32 *irng, u32 *mon_rng) 362*ed2f4c74SNaresh Solanki { 363*ed2f4c74SNaresh Solanki unsigned int reg; 364*ed2f4c74SNaresh Solanki int ret; 365*ed2f4c74SNaresh Solanki 366*ed2f4c74SNaresh Solanki /* Decode current ADC range */ 367*ed2f4c74SNaresh Solanki ret = regmap_read(regmap, MAX5970_REG_STATUS2, ®); 368*ed2f4c74SNaresh Solanki if (ret) 369*ed2f4c74SNaresh Solanki return ret; 370*ed2f4c74SNaresh Solanki switch (MAX5970_IRNG(reg, ch)) { 371*ed2f4c74SNaresh Solanki case 0: 372*ed2f4c74SNaresh Solanki *irng = 100000; /* 100 mV */ 373*ed2f4c74SNaresh Solanki break; 374*ed2f4c74SNaresh Solanki case 1: 375*ed2f4c74SNaresh Solanki *irng = 50000; /* 50 mV */ 376*ed2f4c74SNaresh Solanki break; 377*ed2f4c74SNaresh Solanki case 2: 378*ed2f4c74SNaresh Solanki *irng = 25000; /* 25 mV */ 379*ed2f4c74SNaresh Solanki break; 380*ed2f4c74SNaresh Solanki default: 381*ed2f4c74SNaresh Solanki return -EINVAL; 382*ed2f4c74SNaresh Solanki } 383*ed2f4c74SNaresh Solanki 384*ed2f4c74SNaresh Solanki /* Decode current voltage monitor range */ 385*ed2f4c74SNaresh Solanki ret = regmap_read(regmap, MAX5970_REG_MON_RANGE, ®); 386*ed2f4c74SNaresh Solanki if (ret) 387*ed2f4c74SNaresh Solanki return ret; 388*ed2f4c74SNaresh Solanki 389*ed2f4c74SNaresh Solanki *mon_rng = MAX5970_MON_MAX_RANGE_UV >> MAX5970_MON(reg, ch); 390*ed2f4c74SNaresh Solanki 391*ed2f4c74SNaresh Solanki return 0; 392*ed2f4c74SNaresh Solanki } 393*ed2f4c74SNaresh Solanki 394*ed2f4c74SNaresh Solanki static int max597x_setup_irq(struct device *dev, 395*ed2f4c74SNaresh Solanki int irq, 396*ed2f4c74SNaresh Solanki struct regulator_dev *rdevs[MAX5970_NUM_SWITCHES], 397*ed2f4c74SNaresh Solanki int num_switches, struct max5970_regulator *data) 398*ed2f4c74SNaresh Solanki { 399*ed2f4c74SNaresh Solanki struct regulator_irq_desc max597x_notif = { 400*ed2f4c74SNaresh Solanki .name = "max597x-irq", 401*ed2f4c74SNaresh Solanki .map_event = max597x_irq_handler, 402*ed2f4c74SNaresh Solanki .data = data, 403*ed2f4c74SNaresh Solanki }; 404*ed2f4c74SNaresh Solanki int errs = REGULATOR_ERROR_UNDER_VOLTAGE | 405*ed2f4c74SNaresh Solanki REGULATOR_ERROR_UNDER_VOLTAGE_WARN | 406*ed2f4c74SNaresh Solanki REGULATOR_ERROR_OVER_VOLTAGE_WARN | 407*ed2f4c74SNaresh Solanki REGULATOR_ERROR_REGULATION_OUT | 408*ed2f4c74SNaresh Solanki REGULATOR_ERROR_OVER_CURRENT | 409*ed2f4c74SNaresh Solanki REGULATOR_ERROR_OVER_CURRENT_WARN | REGULATOR_ERROR_FAIL; 410*ed2f4c74SNaresh Solanki void *irq_helper; 411*ed2f4c74SNaresh Solanki 412*ed2f4c74SNaresh Solanki /* Register notifiers - can fail if IRQ is not given */ 413*ed2f4c74SNaresh Solanki irq_helper = devm_regulator_irq_helper(dev, &max597x_notif, 414*ed2f4c74SNaresh Solanki irq, 0, errs, NULL, 415*ed2f4c74SNaresh Solanki &rdevs[0], num_switches); 416*ed2f4c74SNaresh Solanki if (IS_ERR(irq_helper)) { 417*ed2f4c74SNaresh Solanki if (PTR_ERR(irq_helper) == -EPROBE_DEFER) 418*ed2f4c74SNaresh Solanki return -EPROBE_DEFER; 419*ed2f4c74SNaresh Solanki 420*ed2f4c74SNaresh Solanki dev_warn(dev, "IRQ disabled %pe\n", irq_helper); 421*ed2f4c74SNaresh Solanki } 422*ed2f4c74SNaresh Solanki 423*ed2f4c74SNaresh Solanki return 0; 424*ed2f4c74SNaresh Solanki } 425*ed2f4c74SNaresh Solanki 426*ed2f4c74SNaresh Solanki static int max597x_regulator_probe(struct platform_device *pdev) 427*ed2f4c74SNaresh Solanki { 428*ed2f4c74SNaresh Solanki struct max5970_data *max597x; 429*ed2f4c74SNaresh Solanki struct regmap *regmap = dev_get_regmap(pdev->dev.parent, NULL); 430*ed2f4c74SNaresh Solanki struct max5970_regulator *data; 431*ed2f4c74SNaresh Solanki struct i2c_client *i2c = to_i2c_client(pdev->dev.parent); 432*ed2f4c74SNaresh Solanki struct regulator_config config = { }; 433*ed2f4c74SNaresh Solanki struct regulator_dev *rdev; 434*ed2f4c74SNaresh Solanki struct regulator_dev *rdevs[MAX5970_NUM_SWITCHES]; 435*ed2f4c74SNaresh Solanki int num_switches; 436*ed2f4c74SNaresh Solanki int ret, i; 437*ed2f4c74SNaresh Solanki 438*ed2f4c74SNaresh Solanki if (!regmap) 439*ed2f4c74SNaresh Solanki return -EPROBE_DEFER; 440*ed2f4c74SNaresh Solanki 441*ed2f4c74SNaresh Solanki max597x = devm_kzalloc(&i2c->dev, sizeof(struct max5970_data), GFP_KERNEL); 442*ed2f4c74SNaresh Solanki if (!max597x) 443*ed2f4c74SNaresh Solanki return -ENOMEM; 444*ed2f4c74SNaresh Solanki 445*ed2f4c74SNaresh Solanki i2c_set_clientdata(i2c, max597x); 446*ed2f4c74SNaresh Solanki 447*ed2f4c74SNaresh Solanki if (of_device_is_compatible(i2c->dev.of_node, "maxim,max5978")) 448*ed2f4c74SNaresh Solanki max597x->num_switches = MAX5978_NUM_SWITCHES; 449*ed2f4c74SNaresh Solanki else if (of_device_is_compatible(i2c->dev.of_node, "maxim,max5970")) 450*ed2f4c74SNaresh Solanki max597x->num_switches = MAX5970_NUM_SWITCHES; 451*ed2f4c74SNaresh Solanki else 452*ed2f4c74SNaresh Solanki return -ENODEV; 453*ed2f4c74SNaresh Solanki 454*ed2f4c74SNaresh Solanki i2c_set_clientdata(i2c, max597x); 455*ed2f4c74SNaresh Solanki num_switches = max597x->num_switches; 456*ed2f4c74SNaresh Solanki 457*ed2f4c74SNaresh Solanki for (i = 0; i < num_switches; i++) { 458*ed2f4c74SNaresh Solanki data = 459*ed2f4c74SNaresh Solanki devm_kzalloc(&i2c->dev, sizeof(struct max5970_regulator), 460*ed2f4c74SNaresh Solanki GFP_KERNEL); 461*ed2f4c74SNaresh Solanki if (!data) 462*ed2f4c74SNaresh Solanki return -ENOMEM; 463*ed2f4c74SNaresh Solanki 464*ed2f4c74SNaresh Solanki data->num_switches = num_switches; 465*ed2f4c74SNaresh Solanki data->regmap = regmap; 466*ed2f4c74SNaresh Solanki 467*ed2f4c74SNaresh Solanki ret = max597x_adc_range(regmap, i, &max597x->irng[i], &max597x->mon_rng[i]); 468*ed2f4c74SNaresh Solanki if (ret < 0) 469*ed2f4c74SNaresh Solanki return ret; 470*ed2f4c74SNaresh Solanki 471*ed2f4c74SNaresh Solanki data->irng = max597x->irng[i]; 472*ed2f4c74SNaresh Solanki data->mon_rng = max597x->mon_rng[i]; 473*ed2f4c74SNaresh Solanki 474*ed2f4c74SNaresh Solanki config.dev = &i2c->dev; 475*ed2f4c74SNaresh Solanki config.driver_data = (void *)data; 476*ed2f4c74SNaresh Solanki config.regmap = data->regmap; 477*ed2f4c74SNaresh Solanki rdev = devm_regulator_register(&i2c->dev, 478*ed2f4c74SNaresh Solanki ®ulators[i], &config); 479*ed2f4c74SNaresh Solanki if (IS_ERR(rdev)) { 480*ed2f4c74SNaresh Solanki dev_err(&i2c->dev, "failed to register regulator %s\n", 481*ed2f4c74SNaresh Solanki regulators[i].name); 482*ed2f4c74SNaresh Solanki return PTR_ERR(rdev); 483*ed2f4c74SNaresh Solanki } 484*ed2f4c74SNaresh Solanki rdevs[i] = rdev; 485*ed2f4c74SNaresh Solanki max597x->shunt_micro_ohms[i] = data->shunt_micro_ohms; 486*ed2f4c74SNaresh Solanki } 487*ed2f4c74SNaresh Solanki 488*ed2f4c74SNaresh Solanki if (i2c->irq) { 489*ed2f4c74SNaresh Solanki ret = 490*ed2f4c74SNaresh Solanki max597x_setup_irq(&i2c->dev, i2c->irq, rdevs, num_switches, 491*ed2f4c74SNaresh Solanki data); 492*ed2f4c74SNaresh Solanki if (ret) { 493*ed2f4c74SNaresh Solanki dev_err(&i2c->dev, "IRQ setup failed"); 494*ed2f4c74SNaresh Solanki return ret; 495*ed2f4c74SNaresh Solanki } 496*ed2f4c74SNaresh Solanki } 497*ed2f4c74SNaresh Solanki 498*ed2f4c74SNaresh Solanki return ret; 499*ed2f4c74SNaresh Solanki } 500*ed2f4c74SNaresh Solanki 501*ed2f4c74SNaresh Solanki static struct platform_driver max597x_regulator_driver = { 502*ed2f4c74SNaresh Solanki .driver = { 503*ed2f4c74SNaresh Solanki .name = "max5970-regulator", 504*ed2f4c74SNaresh Solanki .probe_type = PROBE_PREFER_ASYNCHRONOUS, 505*ed2f4c74SNaresh Solanki }, 506*ed2f4c74SNaresh Solanki .probe = max597x_regulator_probe, 507*ed2f4c74SNaresh Solanki }; 508*ed2f4c74SNaresh Solanki 509*ed2f4c74SNaresh Solanki module_platform_driver(max597x_regulator_driver); 510*ed2f4c74SNaresh Solanki 511*ed2f4c74SNaresh Solanki 512*ed2f4c74SNaresh Solanki MODULE_AUTHOR("Patrick Rudolph <patrick.rudolph@9elements.com>"); 513*ed2f4c74SNaresh Solanki MODULE_DESCRIPTION("MAX5970_hot-swap controller driver"); 514*ed2f4c74SNaresh Solanki MODULE_LICENSE("GPL v2"); 515