1de20b747SChiYuan Huang // SPDX-License-Identifier: GPL-2.0-only 2de20b747SChiYuan Huang 3de20b747SChiYuan Huang #include <linux/delay.h> 4de20b747SChiYuan Huang #include <linux/gpio/consumer.h> 5de20b747SChiYuan Huang #include <linux/i2c.h> 6de20b747SChiYuan Huang #include <linux/kernel.h> 7de20b747SChiYuan Huang #include <linux/module.h> 8de20b747SChiYuan Huang #include <linux/property.h> 9de20b747SChiYuan Huang #include <linux/regmap.h> 10de20b747SChiYuan Huang #include <linux/regulator/driver.h> 11de20b747SChiYuan Huang #include <linux/regulator/of_regulator.h> 12de20b747SChiYuan Huang 13de20b747SChiYuan Huang #define RT6160_MODE_AUTO 0 14de20b747SChiYuan Huang #define RT6160_MODE_FPWM 1 15de20b747SChiYuan Huang 16de20b747SChiYuan Huang #define RT6160_REG_CNTL 0x01 17de20b747SChiYuan Huang #define RT6160_REG_STATUS 0x02 18de20b747SChiYuan Huang #define RT6160_REG_DEVID 0x03 19de20b747SChiYuan Huang #define RT6160_REG_VSELL 0x04 20de20b747SChiYuan Huang #define RT6160_REG_VSELH 0x05 21de20b747SChiYuan Huang #define RT6160_NUM_REGS (RT6160_REG_VSELH + 1) 22de20b747SChiYuan Huang 23de20b747SChiYuan Huang #define RT6160_FPWM_MASK BIT(3) 24de20b747SChiYuan Huang #define RT6160_RAMPRATE_MASK GENMASK(1, 0) 25de20b747SChiYuan Huang #define RT6160_VID_MASK GENMASK(7, 4) 26de20b747SChiYuan Huang #define RT6160_VSEL_MASK GENMASK(6, 0) 27de20b747SChiYuan Huang #define RT6160_HDSTAT_MASK BIT(4) 28de20b747SChiYuan Huang #define RT6160_UVSTAT_MASK BIT(3) 29de20b747SChiYuan Huang #define RT6160_OCSTAT_MASK BIT(2) 30de20b747SChiYuan Huang #define RT6160_TSDSTAT_MASK BIT(1) 31de20b747SChiYuan Huang #define RT6160_PGSTAT_MASK BIT(0) 32de20b747SChiYuan Huang 33de20b747SChiYuan Huang #define RT6160_VENDOR_ID 0xA0 34de20b747SChiYuan Huang #define RT6160_VOUT_MINUV 2025000 35de20b747SChiYuan Huang #define RT6160_VOUT_MAXUV 5200000 36de20b747SChiYuan Huang #define RT6160_VOUT_STPUV 25000 37de20b747SChiYuan Huang #define RT6160_N_VOUTS ((RT6160_VOUT_MAXUV - RT6160_VOUT_MINUV) / RT6160_VOUT_STPUV + 1) 38de20b747SChiYuan Huang 39de20b747SChiYuan Huang #define RT6160_I2CRDY_TIMEUS 100 40de20b747SChiYuan Huang 41de20b747SChiYuan Huang struct rt6160_priv { 42de20b747SChiYuan Huang struct regulator_desc desc; 43de20b747SChiYuan Huang struct gpio_desc *enable_gpio; 44de20b747SChiYuan Huang struct regmap *regmap; 45de20b747SChiYuan Huang bool enable_state; 46de20b747SChiYuan Huang }; 47de20b747SChiYuan Huang 48b113ec2dSAxel Lin static const unsigned int rt6160_ramp_tables[] = { 49b113ec2dSAxel Lin 1000, 2500, 5000, 10000 50b113ec2dSAxel Lin }; 51b113ec2dSAxel Lin 52de20b747SChiYuan Huang static int rt6160_enable(struct regulator_dev *rdev) 53de20b747SChiYuan Huang { 54de20b747SChiYuan Huang struct rt6160_priv *priv = rdev_get_drvdata(rdev); 55de20b747SChiYuan Huang 56de20b747SChiYuan Huang if (!priv->enable_gpio) 57de20b747SChiYuan Huang return 0; 58de20b747SChiYuan Huang 59de20b747SChiYuan Huang gpiod_set_value_cansleep(priv->enable_gpio, 1); 60de20b747SChiYuan Huang priv->enable_state = true; 61de20b747SChiYuan Huang 62de20b747SChiYuan Huang usleep_range(RT6160_I2CRDY_TIMEUS, RT6160_I2CRDY_TIMEUS + 100); 63de20b747SChiYuan Huang 64de20b747SChiYuan Huang regcache_cache_only(priv->regmap, false); 65de20b747SChiYuan Huang return regcache_sync(priv->regmap); 66de20b747SChiYuan Huang } 67de20b747SChiYuan Huang 68de20b747SChiYuan Huang static int rt6160_disable(struct regulator_dev *rdev) 69de20b747SChiYuan Huang { 70de20b747SChiYuan Huang struct rt6160_priv *priv = rdev_get_drvdata(rdev); 71de20b747SChiYuan Huang 72de20b747SChiYuan Huang if (!priv->enable_gpio) 73de20b747SChiYuan Huang return -EINVAL; 74de20b747SChiYuan Huang 75de20b747SChiYuan Huang /* Mark regcache as dirty and cache only before HW disabled */ 76de20b747SChiYuan Huang regcache_cache_only(priv->regmap, true); 77de20b747SChiYuan Huang regcache_mark_dirty(priv->regmap); 78de20b747SChiYuan Huang 79de20b747SChiYuan Huang priv->enable_state = false; 80de20b747SChiYuan Huang gpiod_set_value_cansleep(priv->enable_gpio, 0); 81de20b747SChiYuan Huang 82de20b747SChiYuan Huang return 0; 83de20b747SChiYuan Huang } 84de20b747SChiYuan Huang 85de20b747SChiYuan Huang static int rt6160_is_enabled(struct regulator_dev *rdev) 86de20b747SChiYuan Huang { 87de20b747SChiYuan Huang struct rt6160_priv *priv = rdev_get_drvdata(rdev); 88de20b747SChiYuan Huang 89de20b747SChiYuan Huang return priv->enable_state ? 1 : 0; 90de20b747SChiYuan Huang } 91de20b747SChiYuan Huang 92de20b747SChiYuan Huang static int rt6160_set_mode(struct regulator_dev *rdev, unsigned int mode) 93de20b747SChiYuan Huang { 94de20b747SChiYuan Huang struct regmap *regmap = rdev_get_regmap(rdev); 95de20b747SChiYuan Huang unsigned int mode_val; 96de20b747SChiYuan Huang 97de20b747SChiYuan Huang switch (mode) { 98de20b747SChiYuan Huang case REGULATOR_MODE_FAST: 99de20b747SChiYuan Huang mode_val = RT6160_FPWM_MASK; 100de20b747SChiYuan Huang break; 101de20b747SChiYuan Huang case REGULATOR_MODE_NORMAL: 102de20b747SChiYuan Huang mode_val = 0; 103de20b747SChiYuan Huang break; 104de20b747SChiYuan Huang default: 105de20b747SChiYuan Huang dev_err(&rdev->dev, "mode not supported\n"); 106de20b747SChiYuan Huang return -EINVAL; 107de20b747SChiYuan Huang } 108de20b747SChiYuan Huang 109de20b747SChiYuan Huang return regmap_update_bits(regmap, RT6160_REG_CNTL, RT6160_FPWM_MASK, mode_val); 110de20b747SChiYuan Huang } 111de20b747SChiYuan Huang 112de20b747SChiYuan Huang static unsigned int rt6160_get_mode(struct regulator_dev *rdev) 113de20b747SChiYuan Huang { 114de20b747SChiYuan Huang struct regmap *regmap = rdev_get_regmap(rdev); 115de20b747SChiYuan Huang unsigned int val; 116de20b747SChiYuan Huang int ret; 117de20b747SChiYuan Huang 118de20b747SChiYuan Huang ret = regmap_read(regmap, RT6160_REG_CNTL, &val); 119de20b747SChiYuan Huang if (ret) 120de20b747SChiYuan Huang return ret; 121de20b747SChiYuan Huang 122de20b747SChiYuan Huang if (val & RT6160_FPWM_MASK) 123de20b747SChiYuan Huang return REGULATOR_MODE_FAST; 124de20b747SChiYuan Huang 125de20b747SChiYuan Huang return REGULATOR_MODE_NORMAL; 126de20b747SChiYuan Huang } 127de20b747SChiYuan Huang 128de20b747SChiYuan Huang static int rt6160_set_suspend_voltage(struct regulator_dev *rdev, int uV) 129de20b747SChiYuan Huang { 130de20b747SChiYuan Huang struct regmap *regmap = rdev_get_regmap(rdev); 131*512d8956SAxel Lin unsigned int suspend_vsel_reg; 132de20b747SChiYuan Huang int vsel; 133de20b747SChiYuan Huang 134de20b747SChiYuan Huang vsel = regulator_map_voltage_linear(rdev, uV, uV); 135de20b747SChiYuan Huang if (vsel < 0) 136de20b747SChiYuan Huang return vsel; 137de20b747SChiYuan Huang 138*512d8956SAxel Lin if (rdev->desc->vsel_reg == RT6160_REG_VSELL) 139*512d8956SAxel Lin suspend_vsel_reg = RT6160_REG_VSELH; 140*512d8956SAxel Lin else 141*512d8956SAxel Lin suspend_vsel_reg = RT6160_REG_VSELL; 142*512d8956SAxel Lin 143*512d8956SAxel Lin return regmap_update_bits(regmap, suspend_vsel_reg, 144f3f4f37dSAxel Lin RT6160_VSEL_MASK, vsel); 145de20b747SChiYuan Huang } 146de20b747SChiYuan Huang 147de20b747SChiYuan Huang static int rt6160_get_error_flags(struct regulator_dev *rdev, unsigned int *flags) 148de20b747SChiYuan Huang { 149de20b747SChiYuan Huang struct regmap *regmap = rdev_get_regmap(rdev); 150de20b747SChiYuan Huang unsigned int val, events = 0; 151de20b747SChiYuan Huang int ret; 152de20b747SChiYuan Huang 153de20b747SChiYuan Huang ret = regmap_read(regmap, RT6160_REG_STATUS, &val); 154de20b747SChiYuan Huang if (ret) 155de20b747SChiYuan Huang return ret; 156de20b747SChiYuan Huang 157de20b747SChiYuan Huang if (val & (RT6160_HDSTAT_MASK | RT6160_TSDSTAT_MASK)) 158de20b747SChiYuan Huang events |= REGULATOR_ERROR_OVER_TEMP; 159de20b747SChiYuan Huang 160de20b747SChiYuan Huang if (val & RT6160_UVSTAT_MASK) 161de20b747SChiYuan Huang events |= REGULATOR_ERROR_UNDER_VOLTAGE; 162de20b747SChiYuan Huang 163de20b747SChiYuan Huang if (val & RT6160_OCSTAT_MASK) 164de20b747SChiYuan Huang events |= REGULATOR_ERROR_OVER_CURRENT; 165de20b747SChiYuan Huang 166de20b747SChiYuan Huang if (val & RT6160_PGSTAT_MASK) 167de20b747SChiYuan Huang events |= REGULATOR_ERROR_FAIL; 168de20b747SChiYuan Huang 169de20b747SChiYuan Huang *flags = events; 170de20b747SChiYuan Huang return 0; 171de20b747SChiYuan Huang } 172de20b747SChiYuan Huang 173de20b747SChiYuan Huang static const struct regulator_ops rt6160_regulator_ops = { 174de20b747SChiYuan Huang .list_voltage = regulator_list_voltage_linear, 175de20b747SChiYuan Huang .set_voltage_sel = regulator_set_voltage_sel_regmap, 176de20b747SChiYuan Huang .get_voltage_sel = regulator_get_voltage_sel_regmap, 177de20b747SChiYuan Huang 178de20b747SChiYuan Huang .enable = rt6160_enable, 179de20b747SChiYuan Huang .disable = rt6160_disable, 180de20b747SChiYuan Huang .is_enabled = rt6160_is_enabled, 181de20b747SChiYuan Huang 182de20b747SChiYuan Huang .set_mode = rt6160_set_mode, 183de20b747SChiYuan Huang .get_mode = rt6160_get_mode, 184de20b747SChiYuan Huang .set_suspend_voltage = rt6160_set_suspend_voltage, 185b113ec2dSAxel Lin .set_ramp_delay = regulator_set_ramp_delay_regmap, 186de20b747SChiYuan Huang .get_error_flags = rt6160_get_error_flags, 187de20b747SChiYuan Huang }; 188de20b747SChiYuan Huang 189de20b747SChiYuan Huang static unsigned int rt6160_of_map_mode(unsigned int mode) 190de20b747SChiYuan Huang { 191de20b747SChiYuan Huang switch (mode) { 192de20b747SChiYuan Huang case RT6160_MODE_FPWM: 193de20b747SChiYuan Huang return REGULATOR_MODE_FAST; 194de20b747SChiYuan Huang case RT6160_MODE_AUTO: 195de20b747SChiYuan Huang return REGULATOR_MODE_NORMAL; 196de20b747SChiYuan Huang } 197de20b747SChiYuan Huang 198de20b747SChiYuan Huang return REGULATOR_MODE_INVALID; 199de20b747SChiYuan Huang } 200de20b747SChiYuan Huang 201de20b747SChiYuan Huang static bool rt6160_is_accessible_reg(struct device *dev, unsigned int reg) 202de20b747SChiYuan Huang { 203de20b747SChiYuan Huang if (reg >= RT6160_REG_CNTL && reg <= RT6160_REG_VSELH) 204de20b747SChiYuan Huang return true; 205de20b747SChiYuan Huang return false; 206de20b747SChiYuan Huang } 207de20b747SChiYuan Huang 208de20b747SChiYuan Huang static bool rt6160_is_volatile_reg(struct device *dev, unsigned int reg) 209de20b747SChiYuan Huang { 210de20b747SChiYuan Huang if (reg == RT6160_REG_STATUS) 211de20b747SChiYuan Huang return true; 212de20b747SChiYuan Huang return false; 213de20b747SChiYuan Huang } 214de20b747SChiYuan Huang 215de20b747SChiYuan Huang static const struct regmap_config rt6160_regmap_config = { 216de20b747SChiYuan Huang .reg_bits = 8, 217de20b747SChiYuan Huang .val_bits = 8, 218de20b747SChiYuan Huang .max_register = RT6160_REG_VSELH, 219de20b747SChiYuan Huang .num_reg_defaults_raw = RT6160_NUM_REGS, 220de20b747SChiYuan Huang .cache_type = REGCACHE_FLAT, 221de20b747SChiYuan Huang 222de20b747SChiYuan Huang .writeable_reg = rt6160_is_accessible_reg, 223de20b747SChiYuan Huang .readable_reg = rt6160_is_accessible_reg, 224de20b747SChiYuan Huang .volatile_reg = rt6160_is_volatile_reg, 225de20b747SChiYuan Huang }; 226de20b747SChiYuan Huang 227de20b747SChiYuan Huang static int rt6160_probe(struct i2c_client *i2c) 228de20b747SChiYuan Huang { 229de20b747SChiYuan Huang struct rt6160_priv *priv; 230de20b747SChiYuan Huang struct regulator_config regulator_cfg = {}; 231de20b747SChiYuan Huang struct regulator_dev *rdev; 232f3f4f37dSAxel Lin bool vsel_active_low; 233de20b747SChiYuan Huang unsigned int devid; 234de20b747SChiYuan Huang int ret; 235de20b747SChiYuan Huang 236de20b747SChiYuan Huang priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL); 237de20b747SChiYuan Huang if (!priv) 238de20b747SChiYuan Huang return -ENOMEM; 239de20b747SChiYuan Huang 240f3f4f37dSAxel Lin vsel_active_low = 241f3f4f37dSAxel Lin device_property_present(&i2c->dev, "richtek,vsel-active-low"); 242de20b747SChiYuan Huang 243de20b747SChiYuan Huang priv->enable_gpio = devm_gpiod_get_optional(&i2c->dev, "enable", GPIOD_OUT_HIGH); 244de20b747SChiYuan Huang if (IS_ERR(priv->enable_gpio)) { 245de20b747SChiYuan Huang dev_err(&i2c->dev, "Failed to get 'enable' gpio\n"); 246de20b747SChiYuan Huang return PTR_ERR(priv->enable_gpio); 247de20b747SChiYuan Huang } 248de20b747SChiYuan Huang priv->enable_state = true; 249de20b747SChiYuan Huang 250de20b747SChiYuan Huang usleep_range(RT6160_I2CRDY_TIMEUS, RT6160_I2CRDY_TIMEUS + 100); 251de20b747SChiYuan Huang 252de20b747SChiYuan Huang priv->regmap = devm_regmap_init_i2c(i2c, &rt6160_regmap_config); 253de20b747SChiYuan Huang if (IS_ERR(priv->regmap)) { 254de20b747SChiYuan Huang ret = PTR_ERR(priv->regmap); 255de20b747SChiYuan Huang dev_err(&i2c->dev, "Failed to init regmap (%d)\n", ret); 256de20b747SChiYuan Huang return ret; 257de20b747SChiYuan Huang } 258de20b747SChiYuan Huang 259de20b747SChiYuan Huang ret = regmap_read(priv->regmap, RT6160_REG_DEVID, &devid); 260de20b747SChiYuan Huang if (ret) 261de20b747SChiYuan Huang return ret; 262de20b747SChiYuan Huang 263de20b747SChiYuan Huang if ((devid & RT6160_VID_MASK) != RT6160_VENDOR_ID) { 264de20b747SChiYuan Huang dev_err(&i2c->dev, "VID not correct [0x%02x]\n", devid); 265de20b747SChiYuan Huang return -ENODEV; 266de20b747SChiYuan Huang } 267de20b747SChiYuan Huang 268de20b747SChiYuan Huang priv->desc.name = "rt6160-buckboost"; 269de20b747SChiYuan Huang priv->desc.type = REGULATOR_VOLTAGE; 270de20b747SChiYuan Huang priv->desc.owner = THIS_MODULE; 271de20b747SChiYuan Huang priv->desc.min_uV = RT6160_VOUT_MINUV; 272de20b747SChiYuan Huang priv->desc.uV_step = RT6160_VOUT_STPUV; 273f3f4f37dSAxel Lin if (vsel_active_low) 274f3f4f37dSAxel Lin priv->desc.vsel_reg = RT6160_REG_VSELL; 275f3f4f37dSAxel Lin else 276de20b747SChiYuan Huang priv->desc.vsel_reg = RT6160_REG_VSELH; 277de20b747SChiYuan Huang priv->desc.vsel_mask = RT6160_VSEL_MASK; 278de20b747SChiYuan Huang priv->desc.n_voltages = RT6160_N_VOUTS; 279b113ec2dSAxel Lin priv->desc.ramp_reg = RT6160_REG_CNTL; 280b113ec2dSAxel Lin priv->desc.ramp_mask = RT6160_RAMPRATE_MASK; 281b113ec2dSAxel Lin priv->desc.ramp_delay_table = rt6160_ramp_tables; 282b113ec2dSAxel Lin priv->desc.n_ramp_values = ARRAY_SIZE(rt6160_ramp_tables); 283de20b747SChiYuan Huang priv->desc.of_map_mode = rt6160_of_map_mode; 284de20b747SChiYuan Huang priv->desc.ops = &rt6160_regulator_ops; 285de20b747SChiYuan Huang 286de20b747SChiYuan Huang regulator_cfg.dev = &i2c->dev; 287de20b747SChiYuan Huang regulator_cfg.of_node = i2c->dev.of_node; 288de20b747SChiYuan Huang regulator_cfg.regmap = priv->regmap; 289de20b747SChiYuan Huang regulator_cfg.driver_data = priv; 290de20b747SChiYuan Huang regulator_cfg.init_data = of_get_regulator_init_data(&i2c->dev, i2c->dev.of_node, 291de20b747SChiYuan Huang &priv->desc); 292de20b747SChiYuan Huang 293de20b747SChiYuan Huang rdev = devm_regulator_register(&i2c->dev, &priv->desc, ®ulator_cfg); 294de20b747SChiYuan Huang if (IS_ERR(rdev)) { 295de20b747SChiYuan Huang dev_err(&i2c->dev, "Failed to register regulator\n"); 296de20b747SChiYuan Huang return PTR_ERR(rdev); 297de20b747SChiYuan Huang } 298de20b747SChiYuan Huang 299de20b747SChiYuan Huang return 0; 300de20b747SChiYuan Huang } 301de20b747SChiYuan Huang 302de20b747SChiYuan Huang static const struct of_device_id __maybe_unused rt6160_of_match_table[] = { 303de20b747SChiYuan Huang { .compatible = "richtek,rt6160", }, 304de20b747SChiYuan Huang {} 305de20b747SChiYuan Huang }; 306de20b747SChiYuan Huang MODULE_DEVICE_TABLE(of, rt6160_of_match_table); 307de20b747SChiYuan Huang 308de20b747SChiYuan Huang static struct i2c_driver rt6160_driver = { 309de20b747SChiYuan Huang .driver = { 310de20b747SChiYuan Huang .name = "rt6160", 311de20b747SChiYuan Huang .of_match_table = rt6160_of_match_table, 312de20b747SChiYuan Huang }, 313de20b747SChiYuan Huang .probe_new = rt6160_probe, 314de20b747SChiYuan Huang }; 315de20b747SChiYuan Huang module_i2c_driver(rt6160_driver); 316de20b747SChiYuan Huang 31723f95199SChiYuan Huang MODULE_DESCRIPTION("Richtek RT6160 voltage regulator driver"); 318de20b747SChiYuan Huang MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>"); 319de20b747SChiYuan Huang MODULE_LICENSE("GPL v2"); 320