149d8c599SYunfan Zhang /* 249d8c599SYunfan Zhang * FAN53555 Fairchild Digitally Programmable TinyBuck Regulator Driver. 349d8c599SYunfan Zhang * 449d8c599SYunfan Zhang * Supported Part Numbers: 549d8c599SYunfan Zhang * FAN53555UC00X/01X/03X/04X/05X 649d8c599SYunfan Zhang * 749d8c599SYunfan Zhang * Copyright (c) 2012 Marvell Technology Ltd. 849d8c599SYunfan Zhang * Yunfan Zhang <yfzhang@marvell.com> 949d8c599SYunfan Zhang * 1049d8c599SYunfan Zhang * This package is free software; you can redistribute it and/or modify 1149d8c599SYunfan Zhang * it under the terms of the GNU General Public License version 2 as 1249d8c599SYunfan Zhang * published by the Free Software Foundation. 1349d8c599SYunfan Zhang * 1449d8c599SYunfan Zhang */ 1549d8c599SYunfan Zhang #include <linux/module.h> 1649d8c599SYunfan Zhang #include <linux/param.h> 1749d8c599SYunfan Zhang #include <linux/err.h> 1849d8c599SYunfan Zhang #include <linux/platform_device.h> 1949d8c599SYunfan Zhang #include <linux/regulator/driver.h> 2049d8c599SYunfan Zhang #include <linux/regulator/machine.h> 2191f23d8fSHeiko Stuebner #include <linux/regulator/of_regulator.h> 2291f23d8fSHeiko Stuebner #include <linux/of_device.h> 2349d8c599SYunfan Zhang #include <linux/i2c.h> 2449d8c599SYunfan Zhang #include <linux/slab.h> 2549d8c599SYunfan Zhang #include <linux/regmap.h> 2649d8c599SYunfan Zhang #include <linux/regulator/fan53555.h> 2749d8c599SYunfan Zhang 2849d8c599SYunfan Zhang /* Voltage setting */ 2949d8c599SYunfan Zhang #define FAN53555_VSEL0 0x00 3049d8c599SYunfan Zhang #define FAN53555_VSEL1 0x01 3149d8c599SYunfan Zhang /* Control register */ 3249d8c599SYunfan Zhang #define FAN53555_CONTROL 0x02 3349d8c599SYunfan Zhang /* IC Type */ 3449d8c599SYunfan Zhang #define FAN53555_ID1 0x03 3549d8c599SYunfan Zhang /* IC mask version */ 3649d8c599SYunfan Zhang #define FAN53555_ID2 0x04 3749d8c599SYunfan Zhang /* Monitor register */ 3849d8c599SYunfan Zhang #define FAN53555_MONITOR 0x05 3949d8c599SYunfan Zhang 4049d8c599SYunfan Zhang /* VSEL bit definitions */ 4149d8c599SYunfan Zhang #define VSEL_BUCK_EN (1 << 7) 4249d8c599SYunfan Zhang #define VSEL_MODE (1 << 6) 4349d8c599SYunfan Zhang #define VSEL_NSEL_MASK 0x3F 4449d8c599SYunfan Zhang /* Chip ID and Verison */ 4549d8c599SYunfan Zhang #define DIE_ID 0x0F /* ID1 */ 4649d8c599SYunfan Zhang #define DIE_REV 0x0F /* ID2 */ 4749d8c599SYunfan Zhang /* Control bit definitions */ 4849d8c599SYunfan Zhang #define CTL_OUTPUT_DISCHG (1 << 7) 4949d8c599SYunfan Zhang #define CTL_SLEW_MASK (0x7 << 4) 5049d8c599SYunfan Zhang #define CTL_SLEW_SHIFT 4 5149d8c599SYunfan Zhang #define CTL_RESET (1 << 2) 5249d8c599SYunfan Zhang 5349d8c599SYunfan Zhang #define FAN53555_NVOLTAGES 64 /* Numbers of voltages */ 5449d8c599SYunfan Zhang 55*ee30928aSHeiko Stuebner enum fan53555_vendor { 56*ee30928aSHeiko Stuebner FAN53555_VENDOR_FAIRCHILD = 0, 57*ee30928aSHeiko Stuebner FAN53555_VENDOR_SILERGY, 58*ee30928aSHeiko Stuebner }; 59*ee30928aSHeiko Stuebner 6049d8c599SYunfan Zhang /* IC Type */ 6149d8c599SYunfan Zhang enum { 6249d8c599SYunfan Zhang FAN53555_CHIP_ID_00 = 0, 6349d8c599SYunfan Zhang FAN53555_CHIP_ID_01, 6449d8c599SYunfan Zhang FAN53555_CHIP_ID_02, 6549d8c599SYunfan Zhang FAN53555_CHIP_ID_03, 6649d8c599SYunfan Zhang FAN53555_CHIP_ID_04, 6749d8c599SYunfan Zhang FAN53555_CHIP_ID_05, 6849d8c599SYunfan Zhang }; 6949d8c599SYunfan Zhang 70*ee30928aSHeiko Stuebner enum { 71*ee30928aSHeiko Stuebner SILERGY_SYR82X = 8, 72*ee30928aSHeiko Stuebner }; 73*ee30928aSHeiko Stuebner 7449d8c599SYunfan Zhang struct fan53555_device_info { 75*ee30928aSHeiko Stuebner enum fan53555_vendor vendor; 7649d8c599SYunfan Zhang struct regmap *regmap; 7749d8c599SYunfan Zhang struct device *dev; 7849d8c599SYunfan Zhang struct regulator_desc desc; 7949d8c599SYunfan Zhang struct regulator_dev *rdev; 8049d8c599SYunfan Zhang struct regulator_init_data *regulator; 8149d8c599SYunfan Zhang /* IC Type and Rev */ 8249d8c599SYunfan Zhang int chip_id; 8349d8c599SYunfan Zhang int chip_rev; 8449d8c599SYunfan Zhang /* Voltage setting register */ 8549d8c599SYunfan Zhang unsigned int vol_reg; 8649d8c599SYunfan Zhang unsigned int sleep_reg; 8749d8c599SYunfan Zhang /* Voltage range and step(linear) */ 8849d8c599SYunfan Zhang unsigned int vsel_min; 8949d8c599SYunfan Zhang unsigned int vsel_step; 9049d8c599SYunfan Zhang /* Voltage slew rate limiting */ 9149d8c599SYunfan Zhang unsigned int slew_rate; 9249d8c599SYunfan Zhang /* Sleep voltage cache */ 9349d8c599SYunfan Zhang unsigned int sleep_vol_cache; 9449d8c599SYunfan Zhang }; 9549d8c599SYunfan Zhang 9649d8c599SYunfan Zhang static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV) 9749d8c599SYunfan Zhang { 9849d8c599SYunfan Zhang struct fan53555_device_info *di = rdev_get_drvdata(rdev); 9949d8c599SYunfan Zhang int ret; 10049d8c599SYunfan Zhang 10149d8c599SYunfan Zhang if (di->sleep_vol_cache == uV) 10249d8c599SYunfan Zhang return 0; 10349d8c599SYunfan Zhang ret = regulator_map_voltage_linear(rdev, uV, uV); 10449d8c599SYunfan Zhang if (ret < 0) 105145fe1e1SSachin Kamat return ret; 10649d8c599SYunfan Zhang ret = regmap_update_bits(di->regmap, di->sleep_reg, 10749d8c599SYunfan Zhang VSEL_NSEL_MASK, ret); 10849d8c599SYunfan Zhang if (ret < 0) 109145fe1e1SSachin Kamat return ret; 11049d8c599SYunfan Zhang /* Cache the sleep voltage setting. 11149d8c599SYunfan Zhang * Might not be the real voltage which is rounded */ 11249d8c599SYunfan Zhang di->sleep_vol_cache = uV; 11349d8c599SYunfan Zhang 11449d8c599SYunfan Zhang return 0; 11549d8c599SYunfan Zhang } 11649d8c599SYunfan Zhang 11749d8c599SYunfan Zhang static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode) 11849d8c599SYunfan Zhang { 11949d8c599SYunfan Zhang struct fan53555_device_info *di = rdev_get_drvdata(rdev); 12049d8c599SYunfan Zhang 12149d8c599SYunfan Zhang switch (mode) { 12249d8c599SYunfan Zhang case REGULATOR_MODE_FAST: 12349d8c599SYunfan Zhang regmap_update_bits(di->regmap, di->vol_reg, 12449d8c599SYunfan Zhang VSEL_MODE, VSEL_MODE); 12549d8c599SYunfan Zhang break; 12649d8c599SYunfan Zhang case REGULATOR_MODE_NORMAL: 12749d8c599SYunfan Zhang regmap_update_bits(di->regmap, di->vol_reg, VSEL_MODE, 0); 12849d8c599SYunfan Zhang break; 12949d8c599SYunfan Zhang default: 13049d8c599SYunfan Zhang return -EINVAL; 13149d8c599SYunfan Zhang } 13249d8c599SYunfan Zhang return 0; 13349d8c599SYunfan Zhang } 13449d8c599SYunfan Zhang 13549d8c599SYunfan Zhang static unsigned int fan53555_get_mode(struct regulator_dev *rdev) 13649d8c599SYunfan Zhang { 13749d8c599SYunfan Zhang struct fan53555_device_info *di = rdev_get_drvdata(rdev); 13849d8c599SYunfan Zhang unsigned int val; 13949d8c599SYunfan Zhang int ret = 0; 14049d8c599SYunfan Zhang 14149d8c599SYunfan Zhang ret = regmap_read(di->regmap, di->vol_reg, &val); 14249d8c599SYunfan Zhang if (ret < 0) 14349d8c599SYunfan Zhang return ret; 14449d8c599SYunfan Zhang if (val & VSEL_MODE) 14549d8c599SYunfan Zhang return REGULATOR_MODE_FAST; 14649d8c599SYunfan Zhang else 14749d8c599SYunfan Zhang return REGULATOR_MODE_NORMAL; 14849d8c599SYunfan Zhang } 14949d8c599SYunfan Zhang 150dd7e71fbSHeiko Stuebner static int slew_rates[] = { 151dd7e71fbSHeiko Stuebner 64000, 152dd7e71fbSHeiko Stuebner 32000, 153dd7e71fbSHeiko Stuebner 16000, 154dd7e71fbSHeiko Stuebner 8000, 155dd7e71fbSHeiko Stuebner 4000, 156dd7e71fbSHeiko Stuebner 2000, 157dd7e71fbSHeiko Stuebner 1000, 158dd7e71fbSHeiko Stuebner 500, 159dd7e71fbSHeiko Stuebner }; 160dd7e71fbSHeiko Stuebner 161dd7e71fbSHeiko Stuebner static int fan53555_set_ramp(struct regulator_dev *rdev, int ramp) 162dd7e71fbSHeiko Stuebner { 163dd7e71fbSHeiko Stuebner struct fan53555_device_info *di = rdev_get_drvdata(rdev); 164dd7e71fbSHeiko Stuebner int regval = -1, i; 165dd7e71fbSHeiko Stuebner 166dd7e71fbSHeiko Stuebner for (i = 0; i < ARRAY_SIZE(slew_rates); i++) { 167dd7e71fbSHeiko Stuebner if (ramp <= slew_rates[i]) 168dd7e71fbSHeiko Stuebner regval = i; 169dd7e71fbSHeiko Stuebner else 170dd7e71fbSHeiko Stuebner break; 171dd7e71fbSHeiko Stuebner } 172dd7e71fbSHeiko Stuebner 173dd7e71fbSHeiko Stuebner if (regval < 0) { 174dd7e71fbSHeiko Stuebner dev_err(di->dev, "unsupported ramp value %d\n", ramp); 175dd7e71fbSHeiko Stuebner return -EINVAL; 176dd7e71fbSHeiko Stuebner } 177dd7e71fbSHeiko Stuebner 178dd7e71fbSHeiko Stuebner return regmap_update_bits(di->regmap, FAN53555_CONTROL, 179dd7e71fbSHeiko Stuebner CTL_SLEW_MASK, regval << CTL_SLEW_SHIFT); 180dd7e71fbSHeiko Stuebner } 181dd7e71fbSHeiko Stuebner 18249d8c599SYunfan Zhang static struct regulator_ops fan53555_regulator_ops = { 18349d8c599SYunfan Zhang .set_voltage_sel = regulator_set_voltage_sel_regmap, 18449d8c599SYunfan Zhang .get_voltage_sel = regulator_get_voltage_sel_regmap, 18549d8c599SYunfan Zhang .map_voltage = regulator_map_voltage_linear, 18649d8c599SYunfan Zhang .list_voltage = regulator_list_voltage_linear, 18749d8c599SYunfan Zhang .set_suspend_voltage = fan53555_set_suspend_voltage, 18849d8c599SYunfan Zhang .enable = regulator_enable_regmap, 18949d8c599SYunfan Zhang .disable = regulator_disable_regmap, 19049d8c599SYunfan Zhang .is_enabled = regulator_is_enabled_regmap, 19149d8c599SYunfan Zhang .set_mode = fan53555_set_mode, 19249d8c599SYunfan Zhang .get_mode = fan53555_get_mode, 193dd7e71fbSHeiko Stuebner .set_ramp_delay = fan53555_set_ramp, 19449d8c599SYunfan Zhang }; 19549d8c599SYunfan Zhang 196*ee30928aSHeiko Stuebner static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di) 19749d8c599SYunfan Zhang { 19849d8c599SYunfan Zhang /* Init voltage range and step */ 19949d8c599SYunfan Zhang switch (di->chip_id) { 20049d8c599SYunfan Zhang case FAN53555_CHIP_ID_00: 20149d8c599SYunfan Zhang case FAN53555_CHIP_ID_01: 20249d8c599SYunfan Zhang case FAN53555_CHIP_ID_03: 20349d8c599SYunfan Zhang case FAN53555_CHIP_ID_05: 20449d8c599SYunfan Zhang di->vsel_min = 600000; 20549d8c599SYunfan Zhang di->vsel_step = 10000; 20649d8c599SYunfan Zhang break; 20749d8c599SYunfan Zhang case FAN53555_CHIP_ID_04: 20849d8c599SYunfan Zhang di->vsel_min = 603000; 20949d8c599SYunfan Zhang di->vsel_step = 12826; 21049d8c599SYunfan Zhang break; 21149d8c599SYunfan Zhang default: 21249d8c599SYunfan Zhang dev_err(di->dev, 213*ee30928aSHeiko Stuebner "Chip ID %d not supported!\n", di->chip_id); 21449d8c599SYunfan Zhang return -EINVAL; 21549d8c599SYunfan Zhang } 216dd7e71fbSHeiko Stuebner 217dd7e71fbSHeiko Stuebner return 0; 21849d8c599SYunfan Zhang } 21949d8c599SYunfan Zhang 220*ee30928aSHeiko Stuebner static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di) 221*ee30928aSHeiko Stuebner { 222*ee30928aSHeiko Stuebner /* Init voltage range and step */ 223*ee30928aSHeiko Stuebner switch (di->chip_id) { 224*ee30928aSHeiko Stuebner case SILERGY_SYR82X: 225*ee30928aSHeiko Stuebner di->vsel_min = 712500; 226*ee30928aSHeiko Stuebner di->vsel_step = 12500; 227*ee30928aSHeiko Stuebner break; 228*ee30928aSHeiko Stuebner default: 229*ee30928aSHeiko Stuebner dev_err(di->dev, 230*ee30928aSHeiko Stuebner "Chip ID %d not supported!\n", di->chip_id); 231*ee30928aSHeiko Stuebner return -EINVAL; 232*ee30928aSHeiko Stuebner } 233*ee30928aSHeiko Stuebner 234*ee30928aSHeiko Stuebner return 0; 235*ee30928aSHeiko Stuebner } 236*ee30928aSHeiko Stuebner 237*ee30928aSHeiko Stuebner /* For 00,01,03,05 options: 238*ee30928aSHeiko Stuebner * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V. 239*ee30928aSHeiko Stuebner * For 04 option: 240*ee30928aSHeiko Stuebner * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V. 241*ee30928aSHeiko Stuebner * */ 242*ee30928aSHeiko Stuebner static int fan53555_device_setup(struct fan53555_device_info *di, 243*ee30928aSHeiko Stuebner struct fan53555_platform_data *pdata) 244*ee30928aSHeiko Stuebner { 245*ee30928aSHeiko Stuebner int ret = 0; 246*ee30928aSHeiko Stuebner 247*ee30928aSHeiko Stuebner /* Setup voltage control register */ 248*ee30928aSHeiko Stuebner switch (pdata->sleep_vsel_id) { 249*ee30928aSHeiko Stuebner case FAN53555_VSEL_ID_0: 250*ee30928aSHeiko Stuebner di->sleep_reg = FAN53555_VSEL0; 251*ee30928aSHeiko Stuebner di->vol_reg = FAN53555_VSEL1; 252*ee30928aSHeiko Stuebner break; 253*ee30928aSHeiko Stuebner case FAN53555_VSEL_ID_1: 254*ee30928aSHeiko Stuebner di->sleep_reg = FAN53555_VSEL1; 255*ee30928aSHeiko Stuebner di->vol_reg = FAN53555_VSEL0; 256*ee30928aSHeiko Stuebner break; 257*ee30928aSHeiko Stuebner default: 258*ee30928aSHeiko Stuebner dev_err(di->dev, "Invalid VSEL ID!\n"); 259*ee30928aSHeiko Stuebner return -EINVAL; 260*ee30928aSHeiko Stuebner } 261*ee30928aSHeiko Stuebner 262*ee30928aSHeiko Stuebner switch (di->vendor) { 263*ee30928aSHeiko Stuebner case FAN53555_VENDOR_FAIRCHILD: 264*ee30928aSHeiko Stuebner ret = fan53555_voltages_setup_fairchild(di); 265*ee30928aSHeiko Stuebner break; 266*ee30928aSHeiko Stuebner case FAN53555_VENDOR_SILERGY: 267*ee30928aSHeiko Stuebner ret = fan53555_voltages_setup_silergy(di); 268*ee30928aSHeiko Stuebner break; 269*ee30928aSHeiko Stuebner default: 270*ee30928aSHeiko Stuebner dev_err(di->dev, 271*ee30928aSHeiko Stuebner "vendor %d not supported!\n", di->chip_id); 272*ee30928aSHeiko Stuebner return -EINVAL; 273*ee30928aSHeiko Stuebner } 274*ee30928aSHeiko Stuebner 275*ee30928aSHeiko Stuebner return ret; 276*ee30928aSHeiko Stuebner } 277*ee30928aSHeiko Stuebner 27849d8c599SYunfan Zhang static int fan53555_regulator_register(struct fan53555_device_info *di, 27949d8c599SYunfan Zhang struct regulator_config *config) 28049d8c599SYunfan Zhang { 28149d8c599SYunfan Zhang struct regulator_desc *rdesc = &di->desc; 28249d8c599SYunfan Zhang 28349d8c599SYunfan Zhang rdesc->name = "fan53555-reg"; 2843415d601SHeiko Stuebner rdesc->supply_name = "vin"; 28549d8c599SYunfan Zhang rdesc->ops = &fan53555_regulator_ops; 28649d8c599SYunfan Zhang rdesc->type = REGULATOR_VOLTAGE; 28749d8c599SYunfan Zhang rdesc->n_voltages = FAN53555_NVOLTAGES; 28849d8c599SYunfan Zhang rdesc->enable_reg = di->vol_reg; 28949d8c599SYunfan Zhang rdesc->enable_mask = VSEL_BUCK_EN; 29049d8c599SYunfan Zhang rdesc->min_uV = di->vsel_min; 29149d8c599SYunfan Zhang rdesc->uV_step = di->vsel_step; 29249d8c599SYunfan Zhang rdesc->vsel_reg = di->vol_reg; 29349d8c599SYunfan Zhang rdesc->vsel_mask = VSEL_NSEL_MASK; 29449d8c599SYunfan Zhang rdesc->owner = THIS_MODULE; 29549d8c599SYunfan Zhang 296b15f5f76SAxel Lin di->rdev = devm_regulator_register(di->dev, &di->desc, config); 2978c6ffba0SRusty Russell return PTR_ERR_OR_ZERO(di->rdev); 29849d8c599SYunfan Zhang } 29949d8c599SYunfan Zhang 30049d8c599SYunfan Zhang static struct regmap_config fan53555_regmap_config = { 30149d8c599SYunfan Zhang .reg_bits = 8, 30249d8c599SYunfan Zhang .val_bits = 8, 30349d8c599SYunfan Zhang }; 30449d8c599SYunfan Zhang 30591f23d8fSHeiko Stuebner static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev, 30691f23d8fSHeiko Stuebner struct device_node *np) 30791f23d8fSHeiko Stuebner { 30891f23d8fSHeiko Stuebner struct fan53555_platform_data *pdata; 30991f23d8fSHeiko Stuebner int ret; 31091f23d8fSHeiko Stuebner u32 tmp; 31191f23d8fSHeiko Stuebner 31291f23d8fSHeiko Stuebner pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 31391f23d8fSHeiko Stuebner if (!pdata) 31491f23d8fSHeiko Stuebner return NULL; 31591f23d8fSHeiko Stuebner 31691f23d8fSHeiko Stuebner pdata->regulator = of_get_regulator_init_data(dev, np); 31791f23d8fSHeiko Stuebner 31891f23d8fSHeiko Stuebner ret = of_property_read_u32(np, "fcs,suspend-voltage-selector", 31991f23d8fSHeiko Stuebner &tmp); 32091f23d8fSHeiko Stuebner if (!ret) 32191f23d8fSHeiko Stuebner pdata->sleep_vsel_id = tmp; 32291f23d8fSHeiko Stuebner 32391f23d8fSHeiko Stuebner return pdata; 32491f23d8fSHeiko Stuebner } 32591f23d8fSHeiko Stuebner 32691f23d8fSHeiko Stuebner static const struct of_device_id fan53555_dt_ids[] = { 32791f23d8fSHeiko Stuebner { 32891f23d8fSHeiko Stuebner .compatible = "fcs,fan53555", 329*ee30928aSHeiko Stuebner .data = (void *)FAN53555_VENDOR_FAIRCHILD 330*ee30928aSHeiko Stuebner }, { 331*ee30928aSHeiko Stuebner .compatible = "silergy,syr827", 332*ee30928aSHeiko Stuebner .data = (void *)FAN53555_VENDOR_SILERGY, 333*ee30928aSHeiko Stuebner }, { 334*ee30928aSHeiko Stuebner .compatible = "silergy,syr828", 335*ee30928aSHeiko Stuebner .data = (void *)FAN53555_VENDOR_SILERGY, 33691f23d8fSHeiko Stuebner }, 33791f23d8fSHeiko Stuebner { } 33891f23d8fSHeiko Stuebner }; 33991f23d8fSHeiko Stuebner MODULE_DEVICE_TABLE(of, fan53555_dt_ids); 34091f23d8fSHeiko Stuebner 341a5023574SBill Pemberton static int fan53555_regulator_probe(struct i2c_client *client, 34249d8c599SYunfan Zhang const struct i2c_device_id *id) 34349d8c599SYunfan Zhang { 34491f23d8fSHeiko Stuebner struct device_node *np = client->dev.of_node; 34549d8c599SYunfan Zhang struct fan53555_device_info *di; 34649d8c599SYunfan Zhang struct fan53555_platform_data *pdata; 34749d8c599SYunfan Zhang struct regulator_config config = { }; 34849d8c599SYunfan Zhang unsigned int val; 34949d8c599SYunfan Zhang int ret; 35049d8c599SYunfan Zhang 351dff91d0bSJingoo Han pdata = dev_get_platdata(&client->dev); 35291f23d8fSHeiko Stuebner if (!pdata) 35391f23d8fSHeiko Stuebner pdata = fan53555_parse_dt(&client->dev, np); 35491f23d8fSHeiko Stuebner 35549d8c599SYunfan Zhang if (!pdata || !pdata->regulator) { 35649d8c599SYunfan Zhang dev_err(&client->dev, "Platform data not found!\n"); 35749d8c599SYunfan Zhang return -ENODEV; 35849d8c599SYunfan Zhang } 35949d8c599SYunfan Zhang 36049d8c599SYunfan Zhang di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info), 36149d8c599SYunfan Zhang GFP_KERNEL); 362df948bbfSSachin Kamat if (!di) 36349d8c599SYunfan Zhang return -ENOMEM; 364df948bbfSSachin Kamat 365*ee30928aSHeiko Stuebner if (client->dev.of_node) { 366*ee30928aSHeiko Stuebner const struct of_device_id *match; 367*ee30928aSHeiko Stuebner 368*ee30928aSHeiko Stuebner match = of_match_device(of_match_ptr(fan53555_dt_ids), 369*ee30928aSHeiko Stuebner &client->dev); 370*ee30928aSHeiko Stuebner if (!match) 371*ee30928aSHeiko Stuebner return -ENODEV; 372*ee30928aSHeiko Stuebner 373*ee30928aSHeiko Stuebner di->vendor = (int) match->data; 374*ee30928aSHeiko Stuebner } else { 375dd7e71fbSHeiko Stuebner /* if no ramp constraint set, get the pdata ramp_delay */ 376dd7e71fbSHeiko Stuebner if (!di->regulator->constraints.ramp_delay) { 37791f23d8fSHeiko Stuebner int slew_idx = (pdata->slew_rate & 0x7) 37891f23d8fSHeiko Stuebner ? pdata->slew_rate : 0; 379dd7e71fbSHeiko Stuebner 38091f23d8fSHeiko Stuebner di->regulator->constraints.ramp_delay 38191f23d8fSHeiko Stuebner = slew_rates[slew_idx]; 38291f23d8fSHeiko Stuebner } 383*ee30928aSHeiko Stuebner 384*ee30928aSHeiko Stuebner di->vendor = id->driver_data; 385dd7e71fbSHeiko Stuebner } 386dd7e71fbSHeiko Stuebner 38749d8c599SYunfan Zhang di->regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config); 38849d8c599SYunfan Zhang if (IS_ERR(di->regmap)) { 38949d8c599SYunfan Zhang dev_err(&client->dev, "Failed to allocate regmap!\n"); 39049d8c599SYunfan Zhang return PTR_ERR(di->regmap); 39149d8c599SYunfan Zhang } 39249d8c599SYunfan Zhang di->dev = &client->dev; 39349d8c599SYunfan Zhang di->regulator = pdata->regulator; 39449d8c599SYunfan Zhang i2c_set_clientdata(client, di); 39549d8c599SYunfan Zhang /* Get chip ID */ 39649d8c599SYunfan Zhang ret = regmap_read(di->regmap, FAN53555_ID1, &val); 39749d8c599SYunfan Zhang if (ret < 0) { 39849d8c599SYunfan Zhang dev_err(&client->dev, "Failed to get chip ID!\n"); 399145fe1e1SSachin Kamat return ret; 40049d8c599SYunfan Zhang } 40149d8c599SYunfan Zhang di->chip_id = val & DIE_ID; 40249d8c599SYunfan Zhang /* Get chip revision */ 40349d8c599SYunfan Zhang ret = regmap_read(di->regmap, FAN53555_ID2, &val); 40449d8c599SYunfan Zhang if (ret < 0) { 40549d8c599SYunfan Zhang dev_err(&client->dev, "Failed to get chip Rev!\n"); 406145fe1e1SSachin Kamat return ret; 40749d8c599SYunfan Zhang } 40849d8c599SYunfan Zhang di->chip_rev = val & DIE_REV; 40949d8c599SYunfan Zhang dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n", 41049d8c599SYunfan Zhang di->chip_id, di->chip_rev); 41149d8c599SYunfan Zhang /* Device init */ 41249d8c599SYunfan Zhang ret = fan53555_device_setup(di, pdata); 41349d8c599SYunfan Zhang if (ret < 0) { 41449d8c599SYunfan Zhang dev_err(&client->dev, "Failed to setup device!\n"); 41549d8c599SYunfan Zhang return ret; 41649d8c599SYunfan Zhang } 41749d8c599SYunfan Zhang /* Register regulator */ 41849d8c599SYunfan Zhang config.dev = di->dev; 41949d8c599SYunfan Zhang config.init_data = di->regulator; 42049d8c599SYunfan Zhang config.regmap = di->regmap; 42149d8c599SYunfan Zhang config.driver_data = di; 42291f23d8fSHeiko Stuebner config.of_node = np; 42391f23d8fSHeiko Stuebner 42449d8c599SYunfan Zhang ret = fan53555_regulator_register(di, &config); 42549d8c599SYunfan Zhang if (ret < 0) 42649d8c599SYunfan Zhang dev_err(&client->dev, "Failed to register regulator!\n"); 42749d8c599SYunfan Zhang return ret; 42849d8c599SYunfan Zhang 42949d8c599SYunfan Zhang } 43049d8c599SYunfan Zhang 43149d8c599SYunfan Zhang static const struct i2c_device_id fan53555_id[] = { 432*ee30928aSHeiko Stuebner { 433*ee30928aSHeiko Stuebner .name = "fan53555", 434*ee30928aSHeiko Stuebner .driver_data = FAN53555_VENDOR_FAIRCHILD 435*ee30928aSHeiko Stuebner }, { 436*ee30928aSHeiko Stuebner .name = "syr82x", 437*ee30928aSHeiko Stuebner .driver_data = FAN53555_VENDOR_SILERGY 438*ee30928aSHeiko Stuebner }, 43949d8c599SYunfan Zhang { }, 44049d8c599SYunfan Zhang }; 44149d8c599SYunfan Zhang 44249d8c599SYunfan Zhang static struct i2c_driver fan53555_regulator_driver = { 44349d8c599SYunfan Zhang .driver = { 44449d8c599SYunfan Zhang .name = "fan53555-regulator", 44591f23d8fSHeiko Stuebner .of_match_table = of_match_ptr(fan53555_dt_ids), 44649d8c599SYunfan Zhang }, 44749d8c599SYunfan Zhang .probe = fan53555_regulator_probe, 44849d8c599SYunfan Zhang .id_table = fan53555_id, 44949d8c599SYunfan Zhang }; 45049d8c599SYunfan Zhang 45149d8c599SYunfan Zhang module_i2c_driver(fan53555_regulator_driver); 45249d8c599SYunfan Zhang 45349d8c599SYunfan Zhang MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>"); 45449d8c599SYunfan Zhang MODULE_DESCRIPTION("FAN53555 regulator driver"); 45549d8c599SYunfan Zhang MODULE_LICENSE("GPL v2"); 456