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> 2149d8c599SYunfan Zhang #include <linux/i2c.h> 2249d8c599SYunfan Zhang #include <linux/slab.h> 2349d8c599SYunfan Zhang #include <linux/regmap.h> 2449d8c599SYunfan Zhang #include <linux/regulator/fan53555.h> 2549d8c599SYunfan Zhang 2649d8c599SYunfan Zhang /* Voltage setting */ 2749d8c599SYunfan Zhang #define FAN53555_VSEL0 0x00 2849d8c599SYunfan Zhang #define FAN53555_VSEL1 0x01 2949d8c599SYunfan Zhang /* Control register */ 3049d8c599SYunfan Zhang #define FAN53555_CONTROL 0x02 3149d8c599SYunfan Zhang /* IC Type */ 3249d8c599SYunfan Zhang #define FAN53555_ID1 0x03 3349d8c599SYunfan Zhang /* IC mask version */ 3449d8c599SYunfan Zhang #define FAN53555_ID2 0x04 3549d8c599SYunfan Zhang /* Monitor register */ 3649d8c599SYunfan Zhang #define FAN53555_MONITOR 0x05 3749d8c599SYunfan Zhang 3849d8c599SYunfan Zhang /* VSEL bit definitions */ 3949d8c599SYunfan Zhang #define VSEL_BUCK_EN (1 << 7) 4049d8c599SYunfan Zhang #define VSEL_MODE (1 << 6) 4149d8c599SYunfan Zhang #define VSEL_NSEL_MASK 0x3F 4249d8c599SYunfan Zhang /* Chip ID and Verison */ 4349d8c599SYunfan Zhang #define DIE_ID 0x0F /* ID1 */ 4449d8c599SYunfan Zhang #define DIE_REV 0x0F /* ID2 */ 4549d8c599SYunfan Zhang /* Control bit definitions */ 4649d8c599SYunfan Zhang #define CTL_OUTPUT_DISCHG (1 << 7) 4749d8c599SYunfan Zhang #define CTL_SLEW_MASK (0x7 << 4) 4849d8c599SYunfan Zhang #define CTL_SLEW_SHIFT 4 4949d8c599SYunfan Zhang #define CTL_RESET (1 << 2) 5049d8c599SYunfan Zhang 5149d8c599SYunfan Zhang #define FAN53555_NVOLTAGES 64 /* Numbers of voltages */ 5249d8c599SYunfan Zhang 5349d8c599SYunfan Zhang /* IC Type */ 5449d8c599SYunfan Zhang enum { 5549d8c599SYunfan Zhang FAN53555_CHIP_ID_00 = 0, 5649d8c599SYunfan Zhang FAN53555_CHIP_ID_01, 5749d8c599SYunfan Zhang FAN53555_CHIP_ID_02, 5849d8c599SYunfan Zhang FAN53555_CHIP_ID_03, 5949d8c599SYunfan Zhang FAN53555_CHIP_ID_04, 6049d8c599SYunfan Zhang FAN53555_CHIP_ID_05, 6149d8c599SYunfan Zhang }; 6249d8c599SYunfan Zhang 6349d8c599SYunfan Zhang struct fan53555_device_info { 6449d8c599SYunfan Zhang struct regmap *regmap; 6549d8c599SYunfan Zhang struct device *dev; 6649d8c599SYunfan Zhang struct regulator_desc desc; 6749d8c599SYunfan Zhang struct regulator_dev *rdev; 6849d8c599SYunfan Zhang struct regulator_init_data *regulator; 6949d8c599SYunfan Zhang /* IC Type and Rev */ 7049d8c599SYunfan Zhang int chip_id; 7149d8c599SYunfan Zhang int chip_rev; 7249d8c599SYunfan Zhang /* Voltage setting register */ 7349d8c599SYunfan Zhang unsigned int vol_reg; 7449d8c599SYunfan Zhang unsigned int sleep_reg; 7549d8c599SYunfan Zhang /* Voltage range and step(linear) */ 7649d8c599SYunfan Zhang unsigned int vsel_min; 7749d8c599SYunfan Zhang unsigned int vsel_step; 7849d8c599SYunfan Zhang /* Voltage slew rate limiting */ 7949d8c599SYunfan Zhang unsigned int slew_rate; 8049d8c599SYunfan Zhang /* Sleep voltage cache */ 8149d8c599SYunfan Zhang unsigned int sleep_vol_cache; 8249d8c599SYunfan Zhang }; 8349d8c599SYunfan Zhang 8449d8c599SYunfan Zhang static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV) 8549d8c599SYunfan Zhang { 8649d8c599SYunfan Zhang struct fan53555_device_info *di = rdev_get_drvdata(rdev); 8749d8c599SYunfan Zhang int ret; 8849d8c599SYunfan Zhang 8949d8c599SYunfan Zhang if (di->sleep_vol_cache == uV) 9049d8c599SYunfan Zhang return 0; 9149d8c599SYunfan Zhang ret = regulator_map_voltage_linear(rdev, uV, uV); 9249d8c599SYunfan Zhang if (ret < 0) 93*145fe1e1SSachin Kamat return ret; 9449d8c599SYunfan Zhang ret = regmap_update_bits(di->regmap, di->sleep_reg, 9549d8c599SYunfan Zhang VSEL_NSEL_MASK, ret); 9649d8c599SYunfan Zhang if (ret < 0) 97*145fe1e1SSachin Kamat return ret; 9849d8c599SYunfan Zhang /* Cache the sleep voltage setting. 9949d8c599SYunfan Zhang * Might not be the real voltage which is rounded */ 10049d8c599SYunfan Zhang di->sleep_vol_cache = uV; 10149d8c599SYunfan Zhang 10249d8c599SYunfan Zhang return 0; 10349d8c599SYunfan Zhang } 10449d8c599SYunfan Zhang 10549d8c599SYunfan Zhang static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode) 10649d8c599SYunfan Zhang { 10749d8c599SYunfan Zhang struct fan53555_device_info *di = rdev_get_drvdata(rdev); 10849d8c599SYunfan Zhang 10949d8c599SYunfan Zhang switch (mode) { 11049d8c599SYunfan Zhang case REGULATOR_MODE_FAST: 11149d8c599SYunfan Zhang regmap_update_bits(di->regmap, di->vol_reg, 11249d8c599SYunfan Zhang VSEL_MODE, VSEL_MODE); 11349d8c599SYunfan Zhang break; 11449d8c599SYunfan Zhang case REGULATOR_MODE_NORMAL: 11549d8c599SYunfan Zhang regmap_update_bits(di->regmap, di->vol_reg, VSEL_MODE, 0); 11649d8c599SYunfan Zhang break; 11749d8c599SYunfan Zhang default: 11849d8c599SYunfan Zhang return -EINVAL; 11949d8c599SYunfan Zhang } 12049d8c599SYunfan Zhang return 0; 12149d8c599SYunfan Zhang } 12249d8c599SYunfan Zhang 12349d8c599SYunfan Zhang static unsigned int fan53555_get_mode(struct regulator_dev *rdev) 12449d8c599SYunfan Zhang { 12549d8c599SYunfan Zhang struct fan53555_device_info *di = rdev_get_drvdata(rdev); 12649d8c599SYunfan Zhang unsigned int val; 12749d8c599SYunfan Zhang int ret = 0; 12849d8c599SYunfan Zhang 12949d8c599SYunfan Zhang ret = regmap_read(di->regmap, di->vol_reg, &val); 13049d8c599SYunfan Zhang if (ret < 0) 13149d8c599SYunfan Zhang return ret; 13249d8c599SYunfan Zhang if (val & VSEL_MODE) 13349d8c599SYunfan Zhang return REGULATOR_MODE_FAST; 13449d8c599SYunfan Zhang else 13549d8c599SYunfan Zhang return REGULATOR_MODE_NORMAL; 13649d8c599SYunfan Zhang } 13749d8c599SYunfan Zhang 13849d8c599SYunfan Zhang static struct regulator_ops fan53555_regulator_ops = { 13949d8c599SYunfan Zhang .set_voltage_sel = regulator_set_voltage_sel_regmap, 14049d8c599SYunfan Zhang .get_voltage_sel = regulator_get_voltage_sel_regmap, 14149d8c599SYunfan Zhang .map_voltage = regulator_map_voltage_linear, 14249d8c599SYunfan Zhang .list_voltage = regulator_list_voltage_linear, 14349d8c599SYunfan Zhang .set_suspend_voltage = fan53555_set_suspend_voltage, 14449d8c599SYunfan Zhang .enable = regulator_enable_regmap, 14549d8c599SYunfan Zhang .disable = regulator_disable_regmap, 14649d8c599SYunfan Zhang .is_enabled = regulator_is_enabled_regmap, 14749d8c599SYunfan Zhang .set_mode = fan53555_set_mode, 14849d8c599SYunfan Zhang .get_mode = fan53555_get_mode, 14949d8c599SYunfan Zhang }; 15049d8c599SYunfan Zhang 15149d8c599SYunfan Zhang /* For 00,01,03,05 options: 15249d8c599SYunfan Zhang * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V. 15349d8c599SYunfan Zhang * For 04 option: 15449d8c599SYunfan Zhang * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V. 15549d8c599SYunfan Zhang * */ 15649d8c599SYunfan Zhang static int fan53555_device_setup(struct fan53555_device_info *di, 15749d8c599SYunfan Zhang struct fan53555_platform_data *pdata) 15849d8c599SYunfan Zhang { 15949d8c599SYunfan Zhang unsigned int reg, data, mask; 16049d8c599SYunfan Zhang 16149d8c599SYunfan Zhang /* Setup voltage control register */ 16249d8c599SYunfan Zhang switch (pdata->sleep_vsel_id) { 16349d8c599SYunfan Zhang case FAN53555_VSEL_ID_0: 16449d8c599SYunfan Zhang di->sleep_reg = FAN53555_VSEL0; 16549d8c599SYunfan Zhang di->vol_reg = FAN53555_VSEL1; 16649d8c599SYunfan Zhang break; 16749d8c599SYunfan Zhang case FAN53555_VSEL_ID_1: 16849d8c599SYunfan Zhang di->sleep_reg = FAN53555_VSEL1; 16949d8c599SYunfan Zhang di->vol_reg = FAN53555_VSEL0; 17049d8c599SYunfan Zhang break; 17149d8c599SYunfan Zhang default: 17249d8c599SYunfan Zhang dev_err(di->dev, "Invalid VSEL ID!\n"); 17349d8c599SYunfan Zhang return -EINVAL; 17449d8c599SYunfan Zhang } 17549d8c599SYunfan Zhang /* Init voltage range and step */ 17649d8c599SYunfan Zhang switch (di->chip_id) { 17749d8c599SYunfan Zhang case FAN53555_CHIP_ID_00: 17849d8c599SYunfan Zhang case FAN53555_CHIP_ID_01: 17949d8c599SYunfan Zhang case FAN53555_CHIP_ID_03: 18049d8c599SYunfan Zhang case FAN53555_CHIP_ID_05: 18149d8c599SYunfan Zhang di->vsel_min = 600000; 18249d8c599SYunfan Zhang di->vsel_step = 10000; 18349d8c599SYunfan Zhang break; 18449d8c599SYunfan Zhang case FAN53555_CHIP_ID_04: 18549d8c599SYunfan Zhang di->vsel_min = 603000; 18649d8c599SYunfan Zhang di->vsel_step = 12826; 18749d8c599SYunfan Zhang break; 18849d8c599SYunfan Zhang default: 18949d8c599SYunfan Zhang dev_err(di->dev, 19049d8c599SYunfan Zhang "Chip ID[%d]\n not supported!\n", di->chip_id); 19149d8c599SYunfan Zhang return -EINVAL; 19249d8c599SYunfan Zhang } 19349d8c599SYunfan Zhang /* Init slew rate */ 19449d8c599SYunfan Zhang if (pdata->slew_rate & 0x7) 19549d8c599SYunfan Zhang di->slew_rate = pdata->slew_rate; 19649d8c599SYunfan Zhang else 19749d8c599SYunfan Zhang di->slew_rate = FAN53555_SLEW_RATE_64MV; 19849d8c599SYunfan Zhang reg = FAN53555_CONTROL; 19949d8c599SYunfan Zhang data = di->slew_rate << CTL_SLEW_SHIFT; 20049d8c599SYunfan Zhang mask = CTL_SLEW_MASK; 20149d8c599SYunfan Zhang return regmap_update_bits(di->regmap, reg, mask, data); 20249d8c599SYunfan Zhang } 20349d8c599SYunfan Zhang 20449d8c599SYunfan Zhang static int fan53555_regulator_register(struct fan53555_device_info *di, 20549d8c599SYunfan Zhang struct regulator_config *config) 20649d8c599SYunfan Zhang { 20749d8c599SYunfan Zhang struct regulator_desc *rdesc = &di->desc; 20849d8c599SYunfan Zhang 20949d8c599SYunfan Zhang rdesc->name = "fan53555-reg"; 21049d8c599SYunfan Zhang rdesc->ops = &fan53555_regulator_ops; 21149d8c599SYunfan Zhang rdesc->type = REGULATOR_VOLTAGE; 21249d8c599SYunfan Zhang rdesc->n_voltages = FAN53555_NVOLTAGES; 21349d8c599SYunfan Zhang rdesc->enable_reg = di->vol_reg; 21449d8c599SYunfan Zhang rdesc->enable_mask = VSEL_BUCK_EN; 21549d8c599SYunfan Zhang rdesc->min_uV = di->vsel_min; 21649d8c599SYunfan Zhang rdesc->uV_step = di->vsel_step; 21749d8c599SYunfan Zhang rdesc->vsel_reg = di->vol_reg; 21849d8c599SYunfan Zhang rdesc->vsel_mask = VSEL_NSEL_MASK; 21949d8c599SYunfan Zhang rdesc->owner = THIS_MODULE; 22049d8c599SYunfan Zhang 221b15f5f76SAxel Lin di->rdev = devm_regulator_register(di->dev, &di->desc, config); 2228c6ffba0SRusty Russell return PTR_ERR_OR_ZERO(di->rdev); 22349d8c599SYunfan Zhang } 22449d8c599SYunfan Zhang 22549d8c599SYunfan Zhang static struct regmap_config fan53555_regmap_config = { 22649d8c599SYunfan Zhang .reg_bits = 8, 22749d8c599SYunfan Zhang .val_bits = 8, 22849d8c599SYunfan Zhang }; 22949d8c599SYunfan Zhang 230a5023574SBill Pemberton static int fan53555_regulator_probe(struct i2c_client *client, 23149d8c599SYunfan Zhang const struct i2c_device_id *id) 23249d8c599SYunfan Zhang { 23349d8c599SYunfan Zhang struct fan53555_device_info *di; 23449d8c599SYunfan Zhang struct fan53555_platform_data *pdata; 23549d8c599SYunfan Zhang struct regulator_config config = { }; 23649d8c599SYunfan Zhang unsigned int val; 23749d8c599SYunfan Zhang int ret; 23849d8c599SYunfan Zhang 239dff91d0bSJingoo Han pdata = dev_get_platdata(&client->dev); 24049d8c599SYunfan Zhang if (!pdata || !pdata->regulator) { 24149d8c599SYunfan Zhang dev_err(&client->dev, "Platform data not found!\n"); 24249d8c599SYunfan Zhang return -ENODEV; 24349d8c599SYunfan Zhang } 24449d8c599SYunfan Zhang 24549d8c599SYunfan Zhang di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info), 24649d8c599SYunfan Zhang GFP_KERNEL); 24749d8c599SYunfan Zhang if (!di) { 24849d8c599SYunfan Zhang dev_err(&client->dev, "Failed to allocate device info data!\n"); 24949d8c599SYunfan Zhang return -ENOMEM; 25049d8c599SYunfan Zhang } 25149d8c599SYunfan Zhang di->regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config); 25249d8c599SYunfan Zhang if (IS_ERR(di->regmap)) { 25349d8c599SYunfan Zhang dev_err(&client->dev, "Failed to allocate regmap!\n"); 25449d8c599SYunfan Zhang return PTR_ERR(di->regmap); 25549d8c599SYunfan Zhang } 25649d8c599SYunfan Zhang di->dev = &client->dev; 25749d8c599SYunfan Zhang di->regulator = pdata->regulator; 25849d8c599SYunfan Zhang i2c_set_clientdata(client, di); 25949d8c599SYunfan Zhang /* Get chip ID */ 26049d8c599SYunfan Zhang ret = regmap_read(di->regmap, FAN53555_ID1, &val); 26149d8c599SYunfan Zhang if (ret < 0) { 26249d8c599SYunfan Zhang dev_err(&client->dev, "Failed to get chip ID!\n"); 263*145fe1e1SSachin Kamat return ret; 26449d8c599SYunfan Zhang } 26549d8c599SYunfan Zhang di->chip_id = val & DIE_ID; 26649d8c599SYunfan Zhang /* Get chip revision */ 26749d8c599SYunfan Zhang ret = regmap_read(di->regmap, FAN53555_ID2, &val); 26849d8c599SYunfan Zhang if (ret < 0) { 26949d8c599SYunfan Zhang dev_err(&client->dev, "Failed to get chip Rev!\n"); 270*145fe1e1SSachin Kamat return ret; 27149d8c599SYunfan Zhang } 27249d8c599SYunfan Zhang di->chip_rev = val & DIE_REV; 27349d8c599SYunfan Zhang dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n", 27449d8c599SYunfan Zhang di->chip_id, di->chip_rev); 27549d8c599SYunfan Zhang /* Device init */ 27649d8c599SYunfan Zhang ret = fan53555_device_setup(di, pdata); 27749d8c599SYunfan Zhang if (ret < 0) { 27849d8c599SYunfan Zhang dev_err(&client->dev, "Failed to setup device!\n"); 27949d8c599SYunfan Zhang return ret; 28049d8c599SYunfan Zhang } 28149d8c599SYunfan Zhang /* Register regulator */ 28249d8c599SYunfan Zhang config.dev = di->dev; 28349d8c599SYunfan Zhang config.init_data = di->regulator; 28449d8c599SYunfan Zhang config.regmap = di->regmap; 28549d8c599SYunfan Zhang config.driver_data = di; 28649d8c599SYunfan Zhang ret = fan53555_regulator_register(di, &config); 28749d8c599SYunfan Zhang if (ret < 0) 28849d8c599SYunfan Zhang dev_err(&client->dev, "Failed to register regulator!\n"); 28949d8c599SYunfan Zhang return ret; 29049d8c599SYunfan Zhang 29149d8c599SYunfan Zhang } 29249d8c599SYunfan Zhang 29349d8c599SYunfan Zhang static const struct i2c_device_id fan53555_id[] = { 29449d8c599SYunfan Zhang {"fan53555", -1}, 29549d8c599SYunfan Zhang { }, 29649d8c599SYunfan Zhang }; 29749d8c599SYunfan Zhang 29849d8c599SYunfan Zhang static struct i2c_driver fan53555_regulator_driver = { 29949d8c599SYunfan Zhang .driver = { 30049d8c599SYunfan Zhang .name = "fan53555-regulator", 30149d8c599SYunfan Zhang }, 30249d8c599SYunfan Zhang .probe = fan53555_regulator_probe, 30349d8c599SYunfan Zhang .id_table = fan53555_id, 30449d8c599SYunfan Zhang }; 30549d8c599SYunfan Zhang 30649d8c599SYunfan Zhang module_i2c_driver(fan53555_regulator_driver); 30749d8c599SYunfan Zhang 30849d8c599SYunfan Zhang MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>"); 30949d8c599SYunfan Zhang MODULE_DESCRIPTION("FAN53555 regulator driver"); 31049d8c599SYunfan Zhang MODULE_LICENSE("GPL v2"); 311