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 vsel_active_low; 46de20b747SChiYuan Huang bool enable_state; 47de20b747SChiYuan Huang }; 48de20b747SChiYuan Huang 49*b113ec2dSAxel Lin static const unsigned int rt6160_ramp_tables[] = { 50*b113ec2dSAxel Lin 1000, 2500, 5000, 10000 51*b113ec2dSAxel Lin }; 52*b113ec2dSAxel Lin 53de20b747SChiYuan Huang static int rt6160_enable(struct regulator_dev *rdev) 54de20b747SChiYuan Huang { 55de20b747SChiYuan Huang struct rt6160_priv *priv = rdev_get_drvdata(rdev); 56de20b747SChiYuan Huang 57de20b747SChiYuan Huang if (!priv->enable_gpio) 58de20b747SChiYuan Huang return 0; 59de20b747SChiYuan Huang 60de20b747SChiYuan Huang gpiod_set_value_cansleep(priv->enable_gpio, 1); 61de20b747SChiYuan Huang priv->enable_state = true; 62de20b747SChiYuan Huang 63de20b747SChiYuan Huang usleep_range(RT6160_I2CRDY_TIMEUS, RT6160_I2CRDY_TIMEUS + 100); 64de20b747SChiYuan Huang 65de20b747SChiYuan Huang regcache_cache_only(priv->regmap, false); 66de20b747SChiYuan Huang return regcache_sync(priv->regmap); 67de20b747SChiYuan Huang } 68de20b747SChiYuan Huang 69de20b747SChiYuan Huang static int rt6160_disable(struct regulator_dev *rdev) 70de20b747SChiYuan Huang { 71de20b747SChiYuan Huang struct rt6160_priv *priv = rdev_get_drvdata(rdev); 72de20b747SChiYuan Huang 73de20b747SChiYuan Huang if (!priv->enable_gpio) 74de20b747SChiYuan Huang return -EINVAL; 75de20b747SChiYuan Huang 76de20b747SChiYuan Huang /* Mark regcache as dirty and cache only before HW disabled */ 77de20b747SChiYuan Huang regcache_cache_only(priv->regmap, true); 78de20b747SChiYuan Huang regcache_mark_dirty(priv->regmap); 79de20b747SChiYuan Huang 80de20b747SChiYuan Huang priv->enable_state = false; 81de20b747SChiYuan Huang gpiod_set_value_cansleep(priv->enable_gpio, 0); 82de20b747SChiYuan Huang 83de20b747SChiYuan Huang return 0; 84de20b747SChiYuan Huang 85de20b747SChiYuan Huang } 86de20b747SChiYuan Huang 87de20b747SChiYuan Huang static int rt6160_is_enabled(struct regulator_dev *rdev) 88de20b747SChiYuan Huang { 89de20b747SChiYuan Huang struct rt6160_priv *priv = rdev_get_drvdata(rdev); 90de20b747SChiYuan Huang 91de20b747SChiYuan Huang return priv->enable_state ? 1 : 0; 92de20b747SChiYuan Huang } 93de20b747SChiYuan Huang 94de20b747SChiYuan Huang static int rt6160_set_mode(struct regulator_dev *rdev, unsigned int mode) 95de20b747SChiYuan Huang { 96de20b747SChiYuan Huang struct regmap *regmap = rdev_get_regmap(rdev); 97de20b747SChiYuan Huang unsigned int mode_val; 98de20b747SChiYuan Huang 99de20b747SChiYuan Huang switch (mode) { 100de20b747SChiYuan Huang case REGULATOR_MODE_FAST: 101de20b747SChiYuan Huang mode_val = RT6160_FPWM_MASK; 102de20b747SChiYuan Huang break; 103de20b747SChiYuan Huang case REGULATOR_MODE_NORMAL: 104de20b747SChiYuan Huang mode_val = 0; 105de20b747SChiYuan Huang break; 106de20b747SChiYuan Huang default: 107de20b747SChiYuan Huang dev_err(&rdev->dev, "mode not supported\n"); 108de20b747SChiYuan Huang return -EINVAL; 109de20b747SChiYuan Huang } 110de20b747SChiYuan Huang 111de20b747SChiYuan Huang return regmap_update_bits(regmap, RT6160_REG_CNTL, RT6160_FPWM_MASK, mode_val); 112de20b747SChiYuan Huang } 113de20b747SChiYuan Huang 114de20b747SChiYuan Huang static unsigned int rt6160_get_mode(struct regulator_dev *rdev) 115de20b747SChiYuan Huang { 116de20b747SChiYuan Huang struct regmap *regmap = rdev_get_regmap(rdev); 117de20b747SChiYuan Huang unsigned int val; 118de20b747SChiYuan Huang int ret; 119de20b747SChiYuan Huang 120de20b747SChiYuan Huang ret = regmap_read(regmap, RT6160_REG_CNTL, &val); 121de20b747SChiYuan Huang if (ret) 122de20b747SChiYuan Huang return ret; 123de20b747SChiYuan Huang 124de20b747SChiYuan Huang if (val & RT6160_FPWM_MASK) 125de20b747SChiYuan Huang return REGULATOR_MODE_FAST; 126de20b747SChiYuan Huang 127de20b747SChiYuan Huang return REGULATOR_MODE_NORMAL; 128de20b747SChiYuan Huang } 129de20b747SChiYuan Huang 130de20b747SChiYuan Huang static int rt6160_set_suspend_voltage(struct regulator_dev *rdev, int uV) 131de20b747SChiYuan Huang { 132de20b747SChiYuan Huang struct rt6160_priv *priv = rdev_get_drvdata(rdev); 133de20b747SChiYuan Huang struct regmap *regmap = rdev_get_regmap(rdev); 134de20b747SChiYuan Huang unsigned int reg = RT6160_REG_VSELH; 135de20b747SChiYuan Huang int vsel; 136de20b747SChiYuan Huang 137de20b747SChiYuan Huang vsel = regulator_map_voltage_linear(rdev, uV, uV); 138de20b747SChiYuan Huang if (vsel < 0) 139de20b747SChiYuan Huang return vsel; 140de20b747SChiYuan Huang 141de20b747SChiYuan Huang if (priv->vsel_active_low) 142de20b747SChiYuan Huang reg = RT6160_REG_VSELL; 143de20b747SChiYuan Huang 144de20b747SChiYuan Huang return regmap_update_bits(regmap, reg, 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, 185*b113ec2dSAxel 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; 232de20b747SChiYuan Huang unsigned int devid; 233de20b747SChiYuan Huang int ret; 234de20b747SChiYuan Huang 235de20b747SChiYuan Huang priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL); 236de20b747SChiYuan Huang if (!priv) 237de20b747SChiYuan Huang return -ENOMEM; 238de20b747SChiYuan Huang 239de20b747SChiYuan Huang priv->vsel_active_low = device_property_present(&i2c->dev, "richtek,vsel-active-low"); 240de20b747SChiYuan Huang 241de20b747SChiYuan Huang priv->enable_gpio = devm_gpiod_get_optional(&i2c->dev, "enable", GPIOD_OUT_HIGH); 242de20b747SChiYuan Huang if (IS_ERR(priv->enable_gpio)) { 243de20b747SChiYuan Huang dev_err(&i2c->dev, "Failed to get 'enable' gpio\n"); 244de20b747SChiYuan Huang return PTR_ERR(priv->enable_gpio); 245de20b747SChiYuan Huang } 246de20b747SChiYuan Huang priv->enable_state = true; 247de20b747SChiYuan Huang 248de20b747SChiYuan Huang usleep_range(RT6160_I2CRDY_TIMEUS, RT6160_I2CRDY_TIMEUS + 100); 249de20b747SChiYuan Huang 250de20b747SChiYuan Huang priv->regmap = devm_regmap_init_i2c(i2c, &rt6160_regmap_config); 251de20b747SChiYuan Huang if (IS_ERR(priv->regmap)) { 252de20b747SChiYuan Huang ret = PTR_ERR(priv->regmap); 253de20b747SChiYuan Huang dev_err(&i2c->dev, "Failed to init regmap (%d)\n", ret); 254de20b747SChiYuan Huang return ret; 255de20b747SChiYuan Huang } 256de20b747SChiYuan Huang 257de20b747SChiYuan Huang ret = regmap_read(priv->regmap, RT6160_REG_DEVID, &devid); 258de20b747SChiYuan Huang if (ret) 259de20b747SChiYuan Huang return ret; 260de20b747SChiYuan Huang 261de20b747SChiYuan Huang if ((devid & RT6160_VID_MASK) != RT6160_VENDOR_ID) { 262de20b747SChiYuan Huang dev_err(&i2c->dev, "VID not correct [0x%02x]\n", devid); 263de20b747SChiYuan Huang return -ENODEV; 264de20b747SChiYuan Huang } 265de20b747SChiYuan Huang 266de20b747SChiYuan Huang priv->desc.name = "rt6160-buckboost"; 267de20b747SChiYuan Huang priv->desc.type = REGULATOR_VOLTAGE; 268de20b747SChiYuan Huang priv->desc.owner = THIS_MODULE; 269de20b747SChiYuan Huang priv->desc.min_uV = RT6160_VOUT_MINUV; 270de20b747SChiYuan Huang priv->desc.uV_step = RT6160_VOUT_STPUV; 271de20b747SChiYuan Huang priv->desc.vsel_reg = RT6160_REG_VSELH; 272de20b747SChiYuan Huang priv->desc.vsel_mask = RT6160_VSEL_MASK; 273de20b747SChiYuan Huang priv->desc.n_voltages = RT6160_N_VOUTS; 274*b113ec2dSAxel Lin priv->desc.ramp_reg = RT6160_REG_CNTL; 275*b113ec2dSAxel Lin priv->desc.ramp_mask = RT6160_RAMPRATE_MASK; 276*b113ec2dSAxel Lin priv->desc.ramp_delay_table = rt6160_ramp_tables; 277*b113ec2dSAxel Lin priv->desc.n_ramp_values = ARRAY_SIZE(rt6160_ramp_tables); 278de20b747SChiYuan Huang priv->desc.of_map_mode = rt6160_of_map_mode; 279de20b747SChiYuan Huang priv->desc.ops = &rt6160_regulator_ops; 280de20b747SChiYuan Huang if (priv->vsel_active_low) 281de20b747SChiYuan Huang priv->desc.vsel_reg = RT6160_REG_VSELL; 282de20b747SChiYuan Huang 283de20b747SChiYuan Huang regulator_cfg.dev = &i2c->dev; 284de20b747SChiYuan Huang regulator_cfg.of_node = i2c->dev.of_node; 285de20b747SChiYuan Huang regulator_cfg.regmap = priv->regmap; 286de20b747SChiYuan Huang regulator_cfg.driver_data = priv; 287de20b747SChiYuan Huang regulator_cfg.init_data = of_get_regulator_init_data(&i2c->dev, i2c->dev.of_node, 288de20b747SChiYuan Huang &priv->desc); 289de20b747SChiYuan Huang 290de20b747SChiYuan Huang rdev = devm_regulator_register(&i2c->dev, &priv->desc, ®ulator_cfg); 291de20b747SChiYuan Huang if (IS_ERR(rdev)) { 292de20b747SChiYuan Huang dev_err(&i2c->dev, "Failed to register regulator\n"); 293de20b747SChiYuan Huang return PTR_ERR(rdev); 294de20b747SChiYuan Huang } 295de20b747SChiYuan Huang 296de20b747SChiYuan Huang return 0; 297de20b747SChiYuan Huang } 298de20b747SChiYuan Huang 299de20b747SChiYuan Huang static const struct of_device_id __maybe_unused rt6160_of_match_table[] = { 300de20b747SChiYuan Huang { .compatible = "richtek,rt6160", }, 301de20b747SChiYuan Huang {} 302de20b747SChiYuan Huang }; 303de20b747SChiYuan Huang MODULE_DEVICE_TABLE(of, rt6160_of_match_table); 304de20b747SChiYuan Huang 305de20b747SChiYuan Huang static struct i2c_driver rt6160_driver = { 306de20b747SChiYuan Huang .driver = { 307de20b747SChiYuan Huang .name = "rt6160", 308de20b747SChiYuan Huang .of_match_table = rt6160_of_match_table, 309de20b747SChiYuan Huang }, 310de20b747SChiYuan Huang .probe_new = rt6160_probe, 311de20b747SChiYuan Huang }; 312de20b747SChiYuan Huang module_i2c_driver(rt6160_driver); 313de20b747SChiYuan Huang 314de20b747SChiYuan Huang MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>"); 315de20b747SChiYuan Huang MODULE_LICENSE("GPL v2"); 316