0001
0002
0003
0004 #include <linux/err.h>
0005 #include <linux/i2c.h>
0006 #include <linux/module.h>
0007 #include <linux/of_device.h>
0008 #include <linux/regmap.h>
0009 #include <linux/regulator/of_regulator.h>
0010 #include <linux/regulator/machine.h>
0011 #include <linux/regulator/driver.h>
0012
0013 #include <dt-bindings/regulator/ti,tps62864.h>
0014
0015 #define TPS6286X_VOUT1 0x01
0016 #define TPS6286X_VOUT1_VO1_SET GENMASK(7, 0)
0017
0018 #define TPS6286X_CONTROL 0x03
0019 #define TPS6286X_CONTROL_FPWM BIT(4)
0020 #define TPS6286X_CONTROL_SWEN BIT(5)
0021
0022 #define TPS6286X_MIN_MV 400
0023 #define TPS6286X_MAX_MV 1675
0024 #define TPS6286X_STEP_MV 5
0025
0026 static const struct regmap_config tps6286x_regmap_config = {
0027 .reg_bits = 8,
0028 .val_bits = 8,
0029 };
0030
0031 static int tps6286x_set_mode(struct regulator_dev *rdev, unsigned int mode)
0032 {
0033 unsigned int val;
0034
0035 switch (mode) {
0036 case REGULATOR_MODE_NORMAL:
0037 val = 0;
0038 break;
0039 case REGULATOR_MODE_FAST:
0040 val = TPS6286X_CONTROL_FPWM;
0041 break;
0042 default:
0043 return -EINVAL;
0044 }
0045
0046 return regmap_update_bits(rdev->regmap, TPS6286X_CONTROL,
0047 TPS6286X_CONTROL_FPWM, val);
0048 }
0049
0050 static unsigned int tps6286x_get_mode(struct regulator_dev *rdev)
0051 {
0052 unsigned int val;
0053 int ret;
0054
0055 ret = regmap_read(rdev->regmap, TPS6286X_CONTROL, &val);
0056 if (ret < 0)
0057 return 0;
0058
0059 return (val & TPS6286X_CONTROL_FPWM) ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
0060 }
0061
0062 static const struct regulator_ops tps6286x_regulator_ops = {
0063 .enable = regulator_enable_regmap,
0064 .disable = regulator_disable_regmap,
0065 .set_mode = tps6286x_set_mode,
0066 .get_mode = tps6286x_get_mode,
0067 .is_enabled = regulator_is_enabled_regmap,
0068 .set_voltage_sel = regulator_set_voltage_sel_regmap,
0069 .get_voltage_sel = regulator_get_voltage_sel_regmap,
0070 .list_voltage = regulator_list_voltage_linear,
0071 };
0072
0073 static unsigned int tps6286x_of_map_mode(unsigned int mode)
0074 {
0075 switch (mode) {
0076 case TPS62864_MODE_NORMAL:
0077 return REGULATOR_MODE_NORMAL;
0078 case TPS62864_MODE_FPWM:
0079 return REGULATOR_MODE_FAST;
0080 default:
0081 return REGULATOR_MODE_INVALID;
0082 }
0083 }
0084
0085 static const struct regulator_desc tps6286x_reg = {
0086 .name = "tps6286x",
0087 .of_match = of_match_ptr("SW"),
0088 .owner = THIS_MODULE,
0089 .ops = &tps6286x_regulator_ops,
0090 .of_map_mode = tps6286x_of_map_mode,
0091 .regulators_node = of_match_ptr("regulators"),
0092 .type = REGULATOR_VOLTAGE,
0093 .n_voltages = ((TPS6286X_MAX_MV - TPS6286X_MIN_MV) / TPS6286X_STEP_MV) + 1,
0094 .min_uV = TPS6286X_MIN_MV * 1000,
0095 .uV_step = TPS6286X_STEP_MV * 1000,
0096 .vsel_reg = TPS6286X_VOUT1,
0097 .vsel_mask = TPS6286X_VOUT1_VO1_SET,
0098 .enable_reg = TPS6286X_CONTROL,
0099 .enable_mask = TPS6286X_CONTROL_SWEN,
0100 .ramp_delay = 1000,
0101
0102 .enable_time = 3000,
0103 };
0104
0105 static const struct of_device_id tps6286x_dt_ids[] = {
0106 { .compatible = "ti,tps62864", },
0107 { .compatible = "ti,tps62866", },
0108 { .compatible = "ti,tps62868", },
0109 { .compatible = "ti,tps62869", },
0110 { }
0111 };
0112 MODULE_DEVICE_TABLE(of, tps6286x_dt_ids);
0113
0114 static int tps6286x_i2c_probe(struct i2c_client *i2c,
0115 const struct i2c_device_id *id)
0116 {
0117 struct device *dev = &i2c->dev;
0118 struct regulator_config config = {};
0119 struct regulator_dev *rdev;
0120 struct regmap *regmap;
0121
0122 regmap = devm_regmap_init_i2c(i2c, &tps6286x_regmap_config);
0123 if (IS_ERR(regmap))
0124 return PTR_ERR(regmap);
0125
0126 config.dev = &i2c->dev;
0127 config.of_node = dev->of_node;
0128 config.regmap = regmap;
0129
0130 rdev = devm_regulator_register(&i2c->dev, &tps6286x_reg, &config);
0131 if (IS_ERR(rdev)) {
0132 dev_err(&i2c->dev, "Failed to register tps6286x regulator\n");
0133 return PTR_ERR(rdev);
0134 }
0135
0136 return 0;
0137 }
0138
0139 static const struct i2c_device_id tps6286x_i2c_id[] = {
0140 { "tps62864", 0 },
0141 { "tps62866", 0 },
0142 { "tps62868", 0 },
0143 { "tps62869", 0 },
0144 {},
0145 };
0146 MODULE_DEVICE_TABLE(i2c, tps6286x_i2c_id);
0147
0148 static struct i2c_driver tps6286x_regulator_driver = {
0149 .driver = {
0150 .name = "tps6286x",
0151 .of_match_table = of_match_ptr(tps6286x_dt_ids),
0152 },
0153 .probe = tps6286x_i2c_probe,
0154 .id_table = tps6286x_i2c_id,
0155 };
0156
0157 module_i2c_driver(tps6286x_regulator_driver);
0158
0159 MODULE_LICENSE("GPL v2");