1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright Axis Communications AB 3 4 #include <linux/err.h> 5 #include <linux/i2c.h> 6 #include <linux/module.h> 7 #include <linux/of.h> 8 #include <linux/regmap.h> 9 #include <linux/regulator/of_regulator.h> 10 #include <linux/regulator/machine.h> 11 #include <linux/regulator/driver.h> 12 13 #include <dt-bindings/regulator/ti,tps62864.h> 14 15 #define TPS6286X_VOUT1 0x01 16 #define TPS6286X_VOUT1_VO1_SET GENMASK(7, 0) 17 18 #define TPS6286X_CONTROL 0x03 19 #define TPS6286X_CONTROL_FPWM BIT(4) 20 #define TPS6286X_CONTROL_SWEN BIT(5) 21 22 #define TPS6286X_STATUS 0x05 23 24 #define TPS6286X_MIN_MV 400 25 #define TPS6286X_MAX_MV 1675 26 #define TPS6286X_STEP_MV 5 27 28 static bool tps6286x_volatile_reg(struct device *dev, unsigned int reg) 29 { 30 return reg == TPS6286X_STATUS; 31 } 32 33 static const struct regmap_config tps6286x_regmap_config = { 34 .reg_bits = 8, 35 .val_bits = 8, 36 .cache_type = REGCACHE_MAPLE, 37 .volatile_reg = tps6286x_volatile_reg, 38 }; 39 40 static int tps6286x_set_mode(struct regulator_dev *rdev, unsigned int mode) 41 { 42 unsigned int val; 43 44 switch (mode) { 45 case REGULATOR_MODE_NORMAL: 46 val = 0; 47 break; 48 case REGULATOR_MODE_FAST: 49 val = TPS6286X_CONTROL_FPWM; 50 break; 51 default: 52 return -EINVAL; 53 } 54 55 return regmap_update_bits(rdev->regmap, TPS6286X_CONTROL, 56 TPS6286X_CONTROL_FPWM, val); 57 } 58 59 static unsigned int tps6286x_get_mode(struct regulator_dev *rdev) 60 { 61 unsigned int val; 62 int ret; 63 64 ret = regmap_read(rdev->regmap, TPS6286X_CONTROL, &val); 65 if (ret < 0) 66 return 0; 67 68 return (val & TPS6286X_CONTROL_FPWM) ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL; 69 } 70 71 static const struct regulator_ops tps6286x_regulator_ops = { 72 .enable = regulator_enable_regmap, 73 .disable = regulator_disable_regmap, 74 .set_mode = tps6286x_set_mode, 75 .get_mode = tps6286x_get_mode, 76 .is_enabled = regulator_is_enabled_regmap, 77 .set_voltage_sel = regulator_set_voltage_sel_regmap, 78 .get_voltage_sel = regulator_get_voltage_sel_regmap, 79 .list_voltage = regulator_list_voltage_linear, 80 }; 81 82 static unsigned int tps6286x_of_map_mode(unsigned int mode) 83 { 84 switch (mode) { 85 case TPS62864_MODE_NORMAL: 86 return REGULATOR_MODE_NORMAL; 87 case TPS62864_MODE_FPWM: 88 return REGULATOR_MODE_FAST; 89 default: 90 return REGULATOR_MODE_INVALID; 91 } 92 } 93 94 static const struct regulator_desc tps6286x_reg = { 95 .name = "tps6286x", 96 .of_match = "SW", 97 .owner = THIS_MODULE, 98 .ops = &tps6286x_regulator_ops, 99 .of_map_mode = tps6286x_of_map_mode, 100 .regulators_node = "regulators", 101 .type = REGULATOR_VOLTAGE, 102 .n_voltages = ((TPS6286X_MAX_MV - TPS6286X_MIN_MV) / TPS6286X_STEP_MV) + 1, 103 .min_uV = TPS6286X_MIN_MV * 1000, 104 .uV_step = TPS6286X_STEP_MV * 1000, 105 .vsel_reg = TPS6286X_VOUT1, 106 .vsel_mask = TPS6286X_VOUT1_VO1_SET, 107 .enable_reg = TPS6286X_CONTROL, 108 .enable_mask = TPS6286X_CONTROL_SWEN, 109 .ramp_delay = 1000, 110 /* tDelay + tRamp, rounded up */ 111 .enable_time = 3000, 112 }; 113 114 static const struct of_device_id tps6286x_dt_ids[] = { 115 { .compatible = "ti,tps62864", }, 116 { .compatible = "ti,tps62866", }, 117 { .compatible = "ti,tps62868", }, 118 { .compatible = "ti,tps62869", }, 119 { } 120 }; 121 MODULE_DEVICE_TABLE(of, tps6286x_dt_ids); 122 123 static int tps6286x_i2c_probe(struct i2c_client *i2c) 124 { 125 struct device *dev = &i2c->dev; 126 struct regulator_config config = {}; 127 struct regulator_dev *rdev; 128 struct regmap *regmap; 129 130 regmap = devm_regmap_init_i2c(i2c, &tps6286x_regmap_config); 131 if (IS_ERR(regmap)) 132 return PTR_ERR(regmap); 133 134 config.dev = &i2c->dev; 135 config.of_node = dev->of_node; 136 config.regmap = regmap; 137 138 rdev = devm_regulator_register(&i2c->dev, &tps6286x_reg, &config); 139 if (IS_ERR(rdev)) { 140 dev_err(&i2c->dev, "Failed to register tps6286x regulator\n"); 141 return PTR_ERR(rdev); 142 } 143 144 return 0; 145 } 146 147 static const struct i2c_device_id tps6286x_i2c_id[] = { 148 { "tps62864" }, 149 { "tps62866" }, 150 { "tps62868" }, 151 { "tps62869" }, 152 {} 153 }; 154 MODULE_DEVICE_TABLE(i2c, tps6286x_i2c_id); 155 156 static struct i2c_driver tps6286x_regulator_driver = { 157 .driver = { 158 .name = "tps6286x", 159 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 160 .of_match_table = tps6286x_dt_ids, 161 }, 162 .probe = tps6286x_i2c_probe, 163 .id_table = tps6286x_i2c_id, 164 }; 165 166 module_i2c_driver(tps6286x_regulator_driver); 167 168 MODULE_DESCRIPTION("TI TPS6286x Power Regulator driver"); 169 MODULE_LICENSE("GPL v2"); 170