1*49d8c599SYunfan Zhang /* 2*49d8c599SYunfan Zhang * FAN53555 Fairchild Digitally Programmable TinyBuck Regulator Driver. 3*49d8c599SYunfan Zhang * 4*49d8c599SYunfan Zhang * Supported Part Numbers: 5*49d8c599SYunfan Zhang * FAN53555UC00X/01X/03X/04X/05X 6*49d8c599SYunfan Zhang * 7*49d8c599SYunfan Zhang * Copyright (c) 2012 Marvell Technology Ltd. 8*49d8c599SYunfan Zhang * Yunfan Zhang <yfzhang@marvell.com> 9*49d8c599SYunfan Zhang * 10*49d8c599SYunfan Zhang * This package is free software; you can redistribute it and/or modify 11*49d8c599SYunfan Zhang * it under the terms of the GNU General Public License version 2 as 12*49d8c599SYunfan Zhang * published by the Free Software Foundation. 13*49d8c599SYunfan Zhang * 14*49d8c599SYunfan Zhang */ 15*49d8c599SYunfan Zhang #include <linux/module.h> 16*49d8c599SYunfan Zhang #include <linux/param.h> 17*49d8c599SYunfan Zhang #include <linux/err.h> 18*49d8c599SYunfan Zhang #include <linux/platform_device.h> 19*49d8c599SYunfan Zhang #include <linux/regulator/driver.h> 20*49d8c599SYunfan Zhang #include <linux/regulator/machine.h> 21*49d8c599SYunfan Zhang #include <linux/i2c.h> 22*49d8c599SYunfan Zhang #include <linux/slab.h> 23*49d8c599SYunfan Zhang #include <linux/regmap.h> 24*49d8c599SYunfan Zhang #include <linux/regulator/fan53555.h> 25*49d8c599SYunfan Zhang 26*49d8c599SYunfan Zhang /* Voltage setting */ 27*49d8c599SYunfan Zhang #define FAN53555_VSEL0 0x00 28*49d8c599SYunfan Zhang #define FAN53555_VSEL1 0x01 29*49d8c599SYunfan Zhang /* Control register */ 30*49d8c599SYunfan Zhang #define FAN53555_CONTROL 0x02 31*49d8c599SYunfan Zhang /* IC Type */ 32*49d8c599SYunfan Zhang #define FAN53555_ID1 0x03 33*49d8c599SYunfan Zhang /* IC mask version */ 34*49d8c599SYunfan Zhang #define FAN53555_ID2 0x04 35*49d8c599SYunfan Zhang /* Monitor register */ 36*49d8c599SYunfan Zhang #define FAN53555_MONITOR 0x05 37*49d8c599SYunfan Zhang 38*49d8c599SYunfan Zhang /* VSEL bit definitions */ 39*49d8c599SYunfan Zhang #define VSEL_BUCK_EN (1 << 7) 40*49d8c599SYunfan Zhang #define VSEL_MODE (1 << 6) 41*49d8c599SYunfan Zhang #define VSEL_NSEL_MASK 0x3F 42*49d8c599SYunfan Zhang /* Chip ID and Verison */ 43*49d8c599SYunfan Zhang #define DIE_ID 0x0F /* ID1 */ 44*49d8c599SYunfan Zhang #define DIE_REV 0x0F /* ID2 */ 45*49d8c599SYunfan Zhang /* Control bit definitions */ 46*49d8c599SYunfan Zhang #define CTL_OUTPUT_DISCHG (1 << 7) 47*49d8c599SYunfan Zhang #define CTL_SLEW_MASK (0x7 << 4) 48*49d8c599SYunfan Zhang #define CTL_SLEW_SHIFT 4 49*49d8c599SYunfan Zhang #define CTL_RESET (1 << 2) 50*49d8c599SYunfan Zhang 51*49d8c599SYunfan Zhang #define FAN53555_NVOLTAGES 64 /* Numbers of voltages */ 52*49d8c599SYunfan Zhang 53*49d8c599SYunfan Zhang /* IC Type */ 54*49d8c599SYunfan Zhang enum { 55*49d8c599SYunfan Zhang FAN53555_CHIP_ID_00 = 0, 56*49d8c599SYunfan Zhang FAN53555_CHIP_ID_01, 57*49d8c599SYunfan Zhang FAN53555_CHIP_ID_02, 58*49d8c599SYunfan Zhang FAN53555_CHIP_ID_03, 59*49d8c599SYunfan Zhang FAN53555_CHIP_ID_04, 60*49d8c599SYunfan Zhang FAN53555_CHIP_ID_05, 61*49d8c599SYunfan Zhang }; 62*49d8c599SYunfan Zhang 63*49d8c599SYunfan Zhang struct fan53555_device_info { 64*49d8c599SYunfan Zhang struct regmap *regmap; 65*49d8c599SYunfan Zhang struct device *dev; 66*49d8c599SYunfan Zhang struct regulator_desc desc; 67*49d8c599SYunfan Zhang struct regulator_dev *rdev; 68*49d8c599SYunfan Zhang struct regulator_init_data *regulator; 69*49d8c599SYunfan Zhang /* IC Type and Rev */ 70*49d8c599SYunfan Zhang int chip_id; 71*49d8c599SYunfan Zhang int chip_rev; 72*49d8c599SYunfan Zhang /* Voltage setting register */ 73*49d8c599SYunfan Zhang unsigned int vol_reg; 74*49d8c599SYunfan Zhang unsigned int sleep_reg; 75*49d8c599SYunfan Zhang /* Voltage range and step(linear) */ 76*49d8c599SYunfan Zhang unsigned int vsel_min; 77*49d8c599SYunfan Zhang unsigned int vsel_max; 78*49d8c599SYunfan Zhang unsigned int vsel_step; 79*49d8c599SYunfan Zhang /* Voltage slew rate limiting */ 80*49d8c599SYunfan Zhang unsigned int slew_rate; 81*49d8c599SYunfan Zhang /* Sleep voltage cache */ 82*49d8c599SYunfan Zhang unsigned int sleep_vol_cache; 83*49d8c599SYunfan Zhang }; 84*49d8c599SYunfan Zhang 85*49d8c599SYunfan Zhang static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV) 86*49d8c599SYunfan Zhang { 87*49d8c599SYunfan Zhang struct fan53555_device_info *di = rdev_get_drvdata(rdev); 88*49d8c599SYunfan Zhang int ret; 89*49d8c599SYunfan Zhang 90*49d8c599SYunfan Zhang if (di->sleep_vol_cache == uV) 91*49d8c599SYunfan Zhang return 0; 92*49d8c599SYunfan Zhang ret = regulator_map_voltage_linear(rdev, uV, uV); 93*49d8c599SYunfan Zhang if (ret < 0) 94*49d8c599SYunfan Zhang return -EINVAL; 95*49d8c599SYunfan Zhang ret = regmap_update_bits(di->regmap, di->sleep_reg, 96*49d8c599SYunfan Zhang VSEL_NSEL_MASK, ret); 97*49d8c599SYunfan Zhang if (ret < 0) 98*49d8c599SYunfan Zhang return -EINVAL; 99*49d8c599SYunfan Zhang /* Cache the sleep voltage setting. 100*49d8c599SYunfan Zhang * Might not be the real voltage which is rounded */ 101*49d8c599SYunfan Zhang di->sleep_vol_cache = uV; 102*49d8c599SYunfan Zhang 103*49d8c599SYunfan Zhang return 0; 104*49d8c599SYunfan Zhang } 105*49d8c599SYunfan Zhang 106*49d8c599SYunfan Zhang static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode) 107*49d8c599SYunfan Zhang { 108*49d8c599SYunfan Zhang struct fan53555_device_info *di = rdev_get_drvdata(rdev); 109*49d8c599SYunfan Zhang 110*49d8c599SYunfan Zhang switch (mode) { 111*49d8c599SYunfan Zhang case REGULATOR_MODE_FAST: 112*49d8c599SYunfan Zhang regmap_update_bits(di->regmap, di->vol_reg, 113*49d8c599SYunfan Zhang VSEL_MODE, VSEL_MODE); 114*49d8c599SYunfan Zhang break; 115*49d8c599SYunfan Zhang case REGULATOR_MODE_NORMAL: 116*49d8c599SYunfan Zhang regmap_update_bits(di->regmap, di->vol_reg, VSEL_MODE, 0); 117*49d8c599SYunfan Zhang break; 118*49d8c599SYunfan Zhang default: 119*49d8c599SYunfan Zhang return -EINVAL; 120*49d8c599SYunfan Zhang } 121*49d8c599SYunfan Zhang return 0; 122*49d8c599SYunfan Zhang } 123*49d8c599SYunfan Zhang 124*49d8c599SYunfan Zhang static unsigned int fan53555_get_mode(struct regulator_dev *rdev) 125*49d8c599SYunfan Zhang { 126*49d8c599SYunfan Zhang struct fan53555_device_info *di = rdev_get_drvdata(rdev); 127*49d8c599SYunfan Zhang unsigned int val; 128*49d8c599SYunfan Zhang int ret = 0; 129*49d8c599SYunfan Zhang 130*49d8c599SYunfan Zhang ret = regmap_read(di->regmap, di->vol_reg, &val); 131*49d8c599SYunfan Zhang if (ret < 0) 132*49d8c599SYunfan Zhang return ret; 133*49d8c599SYunfan Zhang if (val & VSEL_MODE) 134*49d8c599SYunfan Zhang return REGULATOR_MODE_FAST; 135*49d8c599SYunfan Zhang else 136*49d8c599SYunfan Zhang return REGULATOR_MODE_NORMAL; 137*49d8c599SYunfan Zhang } 138*49d8c599SYunfan Zhang 139*49d8c599SYunfan Zhang static struct regulator_ops fan53555_regulator_ops = { 140*49d8c599SYunfan Zhang .set_voltage_sel = regulator_set_voltage_sel_regmap, 141*49d8c599SYunfan Zhang .get_voltage_sel = regulator_get_voltage_sel_regmap, 142*49d8c599SYunfan Zhang .map_voltage = regulator_map_voltage_linear, 143*49d8c599SYunfan Zhang .list_voltage = regulator_list_voltage_linear, 144*49d8c599SYunfan Zhang .set_suspend_voltage = fan53555_set_suspend_voltage, 145*49d8c599SYunfan Zhang .enable = regulator_enable_regmap, 146*49d8c599SYunfan Zhang .disable = regulator_disable_regmap, 147*49d8c599SYunfan Zhang .is_enabled = regulator_is_enabled_regmap, 148*49d8c599SYunfan Zhang .set_mode = fan53555_set_mode, 149*49d8c599SYunfan Zhang .get_mode = fan53555_get_mode, 150*49d8c599SYunfan Zhang }; 151*49d8c599SYunfan Zhang 152*49d8c599SYunfan Zhang /* For 00,01,03,05 options: 153*49d8c599SYunfan Zhang * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V. 154*49d8c599SYunfan Zhang * For 04 option: 155*49d8c599SYunfan Zhang * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V. 156*49d8c599SYunfan Zhang * */ 157*49d8c599SYunfan Zhang static int fan53555_device_setup(struct fan53555_device_info *di, 158*49d8c599SYunfan Zhang struct fan53555_platform_data *pdata) 159*49d8c599SYunfan Zhang { 160*49d8c599SYunfan Zhang unsigned int reg, data, mask; 161*49d8c599SYunfan Zhang 162*49d8c599SYunfan Zhang /* Setup voltage control register */ 163*49d8c599SYunfan Zhang switch (pdata->sleep_vsel_id) { 164*49d8c599SYunfan Zhang case FAN53555_VSEL_ID_0: 165*49d8c599SYunfan Zhang di->sleep_reg = FAN53555_VSEL0; 166*49d8c599SYunfan Zhang di->vol_reg = FAN53555_VSEL1; 167*49d8c599SYunfan Zhang break; 168*49d8c599SYunfan Zhang case FAN53555_VSEL_ID_1: 169*49d8c599SYunfan Zhang di->sleep_reg = FAN53555_VSEL1; 170*49d8c599SYunfan Zhang di->vol_reg = FAN53555_VSEL0; 171*49d8c599SYunfan Zhang break; 172*49d8c599SYunfan Zhang default: 173*49d8c599SYunfan Zhang dev_err(di->dev, "Invalid VSEL ID!\n"); 174*49d8c599SYunfan Zhang return -EINVAL; 175*49d8c599SYunfan Zhang } 176*49d8c599SYunfan Zhang /* Init voltage range and step */ 177*49d8c599SYunfan Zhang switch (di->chip_id) { 178*49d8c599SYunfan Zhang case FAN53555_CHIP_ID_00: 179*49d8c599SYunfan Zhang case FAN53555_CHIP_ID_01: 180*49d8c599SYunfan Zhang case FAN53555_CHIP_ID_03: 181*49d8c599SYunfan Zhang case FAN53555_CHIP_ID_05: 182*49d8c599SYunfan Zhang di->vsel_min = 600000; 183*49d8c599SYunfan Zhang di->vsel_max = 1230000; 184*49d8c599SYunfan Zhang di->vsel_step = 10000; 185*49d8c599SYunfan Zhang break; 186*49d8c599SYunfan Zhang case FAN53555_CHIP_ID_04: 187*49d8c599SYunfan Zhang di->vsel_min = 603000; 188*49d8c599SYunfan Zhang di->vsel_max = 1411000; 189*49d8c599SYunfan Zhang di->vsel_step = 12826; 190*49d8c599SYunfan Zhang break; 191*49d8c599SYunfan Zhang default: 192*49d8c599SYunfan Zhang dev_err(di->dev, 193*49d8c599SYunfan Zhang "Chip ID[%d]\n not supported!\n", di->chip_id); 194*49d8c599SYunfan Zhang return -EINVAL; 195*49d8c599SYunfan Zhang } 196*49d8c599SYunfan Zhang /* Init slew rate */ 197*49d8c599SYunfan Zhang if (pdata->slew_rate & 0x7) 198*49d8c599SYunfan Zhang di->slew_rate = pdata->slew_rate; 199*49d8c599SYunfan Zhang else 200*49d8c599SYunfan Zhang di->slew_rate = FAN53555_SLEW_RATE_64MV; 201*49d8c599SYunfan Zhang reg = FAN53555_CONTROL; 202*49d8c599SYunfan Zhang data = di->slew_rate << CTL_SLEW_SHIFT; 203*49d8c599SYunfan Zhang mask = CTL_SLEW_MASK; 204*49d8c599SYunfan Zhang return regmap_update_bits(di->regmap, reg, mask, data); 205*49d8c599SYunfan Zhang } 206*49d8c599SYunfan Zhang 207*49d8c599SYunfan Zhang static int fan53555_regulator_register(struct fan53555_device_info *di, 208*49d8c599SYunfan Zhang struct regulator_config *config) 209*49d8c599SYunfan Zhang { 210*49d8c599SYunfan Zhang struct regulator_desc *rdesc = &di->desc; 211*49d8c599SYunfan Zhang 212*49d8c599SYunfan Zhang rdesc->name = "fan53555-reg"; 213*49d8c599SYunfan Zhang rdesc->ops = &fan53555_regulator_ops; 214*49d8c599SYunfan Zhang rdesc->type = REGULATOR_VOLTAGE; 215*49d8c599SYunfan Zhang rdesc->n_voltages = FAN53555_NVOLTAGES; 216*49d8c599SYunfan Zhang rdesc->enable_reg = di->vol_reg; 217*49d8c599SYunfan Zhang rdesc->enable_mask = VSEL_BUCK_EN; 218*49d8c599SYunfan Zhang rdesc->min_uV = di->vsel_min; 219*49d8c599SYunfan Zhang rdesc->uV_step = di->vsel_step; 220*49d8c599SYunfan Zhang rdesc->vsel_reg = di->vol_reg; 221*49d8c599SYunfan Zhang rdesc->vsel_mask = VSEL_NSEL_MASK; 222*49d8c599SYunfan Zhang rdesc->owner = THIS_MODULE; 223*49d8c599SYunfan Zhang 224*49d8c599SYunfan Zhang di->rdev = regulator_register(&di->desc, config); 225*49d8c599SYunfan Zhang if (IS_ERR(di->rdev)) 226*49d8c599SYunfan Zhang return PTR_ERR(di->rdev); 227*49d8c599SYunfan Zhang return 0; 228*49d8c599SYunfan Zhang 229*49d8c599SYunfan Zhang } 230*49d8c599SYunfan Zhang 231*49d8c599SYunfan Zhang static struct regmap_config fan53555_regmap_config = { 232*49d8c599SYunfan Zhang .reg_bits = 8, 233*49d8c599SYunfan Zhang .val_bits = 8, 234*49d8c599SYunfan Zhang }; 235*49d8c599SYunfan Zhang 236*49d8c599SYunfan Zhang static int __devinit fan53555_regulator_probe(struct i2c_client *client, 237*49d8c599SYunfan Zhang const struct i2c_device_id *id) 238*49d8c599SYunfan Zhang { 239*49d8c599SYunfan Zhang struct fan53555_device_info *di; 240*49d8c599SYunfan Zhang struct fan53555_platform_data *pdata; 241*49d8c599SYunfan Zhang struct regulator_config config = { }; 242*49d8c599SYunfan Zhang unsigned int val; 243*49d8c599SYunfan Zhang int ret; 244*49d8c599SYunfan Zhang 245*49d8c599SYunfan Zhang pdata = client->dev.platform_data; 246*49d8c599SYunfan Zhang if (!pdata || !pdata->regulator) { 247*49d8c599SYunfan Zhang dev_err(&client->dev, "Platform data not found!\n"); 248*49d8c599SYunfan Zhang return -ENODEV; 249*49d8c599SYunfan Zhang } 250*49d8c599SYunfan Zhang 251*49d8c599SYunfan Zhang di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info), 252*49d8c599SYunfan Zhang GFP_KERNEL); 253*49d8c599SYunfan Zhang if (!di) { 254*49d8c599SYunfan Zhang dev_err(&client->dev, "Failed to allocate device info data!\n"); 255*49d8c599SYunfan Zhang return -ENOMEM; 256*49d8c599SYunfan Zhang } 257*49d8c599SYunfan Zhang di->regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config); 258*49d8c599SYunfan Zhang if (IS_ERR(di->regmap)) { 259*49d8c599SYunfan Zhang dev_err(&client->dev, "Failed to allocate regmap!\n"); 260*49d8c599SYunfan Zhang return PTR_ERR(di->regmap); 261*49d8c599SYunfan Zhang } 262*49d8c599SYunfan Zhang di->dev = &client->dev; 263*49d8c599SYunfan Zhang di->regulator = pdata->regulator; 264*49d8c599SYunfan Zhang i2c_set_clientdata(client, di); 265*49d8c599SYunfan Zhang /* Get chip ID */ 266*49d8c599SYunfan Zhang ret = regmap_read(di->regmap, FAN53555_ID1, &val); 267*49d8c599SYunfan Zhang if (ret < 0) { 268*49d8c599SYunfan Zhang dev_err(&client->dev, "Failed to get chip ID!\n"); 269*49d8c599SYunfan Zhang return -ENODEV; 270*49d8c599SYunfan Zhang } 271*49d8c599SYunfan Zhang di->chip_id = val & DIE_ID; 272*49d8c599SYunfan Zhang /* Get chip revision */ 273*49d8c599SYunfan Zhang ret = regmap_read(di->regmap, FAN53555_ID2, &val); 274*49d8c599SYunfan Zhang if (ret < 0) { 275*49d8c599SYunfan Zhang dev_err(&client->dev, "Failed to get chip Rev!\n"); 276*49d8c599SYunfan Zhang return -ENODEV; 277*49d8c599SYunfan Zhang } 278*49d8c599SYunfan Zhang di->chip_rev = val & DIE_REV; 279*49d8c599SYunfan Zhang dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n", 280*49d8c599SYunfan Zhang di->chip_id, di->chip_rev); 281*49d8c599SYunfan Zhang /* Device init */ 282*49d8c599SYunfan Zhang ret = fan53555_device_setup(di, pdata); 283*49d8c599SYunfan Zhang if (ret < 0) { 284*49d8c599SYunfan Zhang dev_err(&client->dev, "Failed to setup device!\n"); 285*49d8c599SYunfan Zhang return ret; 286*49d8c599SYunfan Zhang } 287*49d8c599SYunfan Zhang /* Register regulator */ 288*49d8c599SYunfan Zhang config.dev = di->dev; 289*49d8c599SYunfan Zhang config.init_data = di->regulator; 290*49d8c599SYunfan Zhang config.regmap = di->regmap; 291*49d8c599SYunfan Zhang config.driver_data = di; 292*49d8c599SYunfan Zhang ret = fan53555_regulator_register(di, &config); 293*49d8c599SYunfan Zhang if (ret < 0) 294*49d8c599SYunfan Zhang dev_err(&client->dev, "Failed to register regulator!\n"); 295*49d8c599SYunfan Zhang return ret; 296*49d8c599SYunfan Zhang 297*49d8c599SYunfan Zhang } 298*49d8c599SYunfan Zhang 299*49d8c599SYunfan Zhang static int __devexit fan53555_regulator_remove(struct i2c_client *client) 300*49d8c599SYunfan Zhang { 301*49d8c599SYunfan Zhang struct fan53555_device_info *di = i2c_get_clientdata(client); 302*49d8c599SYunfan Zhang 303*49d8c599SYunfan Zhang regulator_unregister(di->rdev); 304*49d8c599SYunfan Zhang return 0; 305*49d8c599SYunfan Zhang } 306*49d8c599SYunfan Zhang 307*49d8c599SYunfan Zhang static const struct i2c_device_id fan53555_id[] = { 308*49d8c599SYunfan Zhang {"fan53555", -1}, 309*49d8c599SYunfan Zhang { }, 310*49d8c599SYunfan Zhang }; 311*49d8c599SYunfan Zhang 312*49d8c599SYunfan Zhang static struct i2c_driver fan53555_regulator_driver = { 313*49d8c599SYunfan Zhang .driver = { 314*49d8c599SYunfan Zhang .name = "fan53555-regulator", 315*49d8c599SYunfan Zhang }, 316*49d8c599SYunfan Zhang .probe = fan53555_regulator_probe, 317*49d8c599SYunfan Zhang .remove = __devexit_p(fan53555_regulator_remove), 318*49d8c599SYunfan Zhang .id_table = fan53555_id, 319*49d8c599SYunfan Zhang }; 320*49d8c599SYunfan Zhang 321*49d8c599SYunfan Zhang module_i2c_driver(fan53555_regulator_driver); 322*49d8c599SYunfan Zhang 323*49d8c599SYunfan Zhang MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>"); 324*49d8c599SYunfan Zhang MODULE_DESCRIPTION("FAN53555 regulator driver"); 325*49d8c599SYunfan Zhang MODULE_LICENSE("GPL v2"); 326