0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/err.h>
0009 #include <linux/gpio.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/i2c.h>
0012 #include <linux/module.h>
0013 #include <linux/regmap.h>
0014 #include <linux/regulator/driver.h>
0015 #include <linux/regulator/machine.h>
0016 #include <linux/regulator/of_regulator.h>
0017 #include <linux/slab.h>
0018
0019
0020 #define MAX20086_REG_MASK 0x00
0021 #define MAX20086_REG_CONFIG 0x01
0022 #define MAX20086_REG_ID 0x02
0023 #define MAX20086_REG_STAT1 0x03
0024 #define MAX20086_REG_STAT2_L 0x04
0025 #define MAX20086_REG_STAT2_H 0x05
0026 #define MAX20086_REG_ADC1 0x06
0027 #define MAX20086_REG_ADC2 0x07
0028 #define MAX20086_REG_ADC3 0x08
0029 #define MAX20086_REG_ADC4 0x09
0030
0031
0032 #define MAX20086_DEVICE_ID_MAX20086 0x40
0033 #define MAX20086_DEVICE_ID_MAX20087 0x20
0034 #define MAX20086_DEVICE_ID_MAX20088 0x10
0035 #define MAX20086_DEVICE_ID_MAX20089 0x00
0036 #define DEVICE_ID_MASK 0xf0
0037
0038
0039 #define MAX20086_EN_MASK 0x0f
0040 #define MAX20086_EN_OUT1 0x01
0041 #define MAX20086_EN_OUT2 0x02
0042 #define MAX20086_EN_OUT3 0x04
0043 #define MAX20086_EN_OUT4 0x08
0044 #define MAX20086_INT_DISABLE_ALL 0x3f
0045
0046 #define MAX20086_MAX_REGULATORS 4
0047
0048 struct max20086_chip_info {
0049 u8 id;
0050 unsigned int num_outputs;
0051 };
0052
0053 struct max20086_regulator {
0054 struct device_node *of_node;
0055 struct regulator_init_data *init_data;
0056 const struct regulator_desc *desc;
0057 struct regulator_dev *rdev;
0058 };
0059
0060 struct max20086 {
0061 struct device *dev;
0062 struct regmap *regmap;
0063 struct gpio_desc *ena_gpiod;
0064
0065 const struct max20086_chip_info *info;
0066
0067 struct max20086_regulator regulators[MAX20086_MAX_REGULATORS];
0068 };
0069
0070 static const struct regulator_ops max20086_buck_ops = {
0071 .enable = regulator_enable_regmap,
0072 .disable = regulator_disable_regmap,
0073 .is_enabled = regulator_is_enabled_regmap,
0074 };
0075
0076 #define MAX20086_REGULATOR_DESC(n) \
0077 { \
0078 .name = "OUT"#n, \
0079 .supply_name = "in", \
0080 .id = (n) - 1, \
0081 .ops = &max20086_buck_ops, \
0082 .type = REGULATOR_VOLTAGE, \
0083 .owner = THIS_MODULE, \
0084 .enable_reg = MAX20086_REG_CONFIG, \
0085 .enable_mask = 1 << ((n) - 1), \
0086 .enable_val = 1 << ((n) - 1), \
0087 .disable_val = 0, \
0088 }
0089
0090 static const char * const max20086_output_names[] = {
0091 "OUT1",
0092 "OUT2",
0093 "OUT3",
0094 "OUT4",
0095 };
0096
0097 static const struct regulator_desc max20086_regulators[] = {
0098 MAX20086_REGULATOR_DESC(1),
0099 MAX20086_REGULATOR_DESC(2),
0100 MAX20086_REGULATOR_DESC(3),
0101 MAX20086_REGULATOR_DESC(4),
0102 };
0103
0104 static int max20086_regulators_register(struct max20086 *chip)
0105 {
0106 unsigned int i;
0107
0108 for (i = 0; i < chip->info->num_outputs; i++) {
0109 struct max20086_regulator *reg = &chip->regulators[i];
0110 struct regulator_config config = { };
0111 struct regulator_dev *rdev;
0112
0113 config.dev = chip->dev;
0114 config.init_data = reg->init_data;
0115 config.driver_data = chip;
0116 config.of_node = reg->of_node;
0117 config.regmap = chip->regmap;
0118 config.ena_gpiod = chip->ena_gpiod;
0119
0120 rdev = devm_regulator_register(chip->dev, reg->desc, &config);
0121 if (IS_ERR(rdev)) {
0122 dev_err(chip->dev,
0123 "Failed to register regulator output %s\n",
0124 reg->desc->name);
0125 return PTR_ERR(rdev);
0126 }
0127
0128 reg->rdev = rdev;
0129 }
0130
0131 return 0;
0132 }
0133
0134 static int max20086_parse_regulators_dt(struct max20086 *chip, bool *boot_on)
0135 {
0136 struct of_regulator_match matches[MAX20086_MAX_REGULATORS] = { };
0137 struct device_node *node;
0138 unsigned int i;
0139 int ret;
0140
0141 node = of_get_child_by_name(chip->dev->of_node, "regulators");
0142 if (!node) {
0143 dev_err(chip->dev, "regulators node not found\n");
0144 return -ENODEV;
0145 }
0146
0147 for (i = 0; i < chip->info->num_outputs; ++i)
0148 matches[i].name = max20086_output_names[i];
0149
0150 ret = of_regulator_match(chip->dev, node, matches,
0151 chip->info->num_outputs);
0152 of_node_put(node);
0153 if (ret < 0) {
0154 dev_err(chip->dev, "Failed to match regulators\n");
0155 return -EINVAL;
0156 }
0157
0158 *boot_on = false;
0159
0160 for (i = 0; i < chip->info->num_outputs; i++) {
0161 struct max20086_regulator *reg = &chip->regulators[i];
0162
0163 reg->init_data = matches[i].init_data;
0164 reg->of_node = matches[i].of_node;
0165 reg->desc = &max20086_regulators[i];
0166
0167 if (reg->init_data) {
0168 if (reg->init_data->constraints.always_on ||
0169 reg->init_data->constraints.boot_on)
0170 *boot_on = true;
0171 }
0172 }
0173
0174 return 0;
0175 }
0176
0177 static int max20086_detect(struct max20086 *chip)
0178 {
0179 unsigned int data;
0180 int ret;
0181
0182 ret = regmap_read(chip->regmap, MAX20086_REG_ID, &data);
0183 if (ret < 0) {
0184 dev_err(chip->dev, "Failed to read DEVICE_ID reg: %d\n", ret);
0185 return ret;
0186 }
0187
0188 if ((data & DEVICE_ID_MASK) != chip->info->id) {
0189 dev_err(chip->dev, "Invalid device ID 0x%02x\n", data);
0190 return -ENXIO;
0191 }
0192
0193 return 0;
0194 }
0195
0196 static bool max20086_gen_is_writeable_reg(struct device *dev, unsigned int reg)
0197 {
0198 switch (reg) {
0199 case MAX20086_REG_MASK:
0200 case MAX20086_REG_CONFIG:
0201 return true;
0202 default:
0203 return false;
0204 }
0205 }
0206
0207 static const struct regmap_config max20086_regmap_config = {
0208 .reg_bits = 8,
0209 .val_bits = 8,
0210 .writeable_reg = max20086_gen_is_writeable_reg,
0211 .max_register = 0x9,
0212 .cache_type = REGCACHE_NONE,
0213 };
0214
0215 static int max20086_i2c_probe(struct i2c_client *i2c)
0216 {
0217 struct max20086 *chip;
0218 enum gpiod_flags flags;
0219 bool boot_on;
0220 int ret;
0221
0222 chip = devm_kzalloc(&i2c->dev, sizeof(*chip), GFP_KERNEL);
0223 if (!chip)
0224 return -ENOMEM;
0225
0226 chip->dev = &i2c->dev;
0227 chip->info = device_get_match_data(chip->dev);
0228
0229 i2c_set_clientdata(i2c, chip);
0230
0231 chip->regmap = devm_regmap_init_i2c(i2c, &max20086_regmap_config);
0232 if (IS_ERR(chip->regmap)) {
0233 ret = PTR_ERR(chip->regmap);
0234 dev_err(chip->dev, "Failed to allocate register map: %d\n", ret);
0235 return ret;
0236 }
0237
0238 ret = max20086_parse_regulators_dt(chip, &boot_on);
0239 if (ret < 0)
0240 return ret;
0241
0242 ret = max20086_detect(chip);
0243 if (ret < 0)
0244 return ret;
0245
0246
0247 ret = regmap_update_bits(chip->regmap, MAX20086_REG_MASK,
0248 MAX20086_INT_DISABLE_ALL,
0249 MAX20086_INT_DISABLE_ALL);
0250 if (ret < 0) {
0251 dev_err(chip->dev, "Failed to disable interrupts: %d\n", ret);
0252 return ret;
0253 }
0254
0255
0256
0257
0258
0259
0260
0261
0262 flags = boot_on ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
0263 chip->ena_gpiod = devm_gpiod_get(chip->dev, "enable", flags);
0264 if (IS_ERR(chip->ena_gpiod)) {
0265 ret = PTR_ERR(chip->ena_gpiod);
0266 dev_err(chip->dev, "Failed to get enable GPIO: %d\n", ret);
0267 return ret;
0268 }
0269
0270 ret = max20086_regulators_register(chip);
0271 if (ret < 0) {
0272 dev_err(chip->dev, "Failed to register regulators: %d\n", ret);
0273 return ret;
0274 }
0275
0276 return 0;
0277 }
0278
0279 static const struct i2c_device_id max20086_i2c_id[] = {
0280 { "max20086" },
0281 { "max20087" },
0282 { "max20088" },
0283 { "max20089" },
0284 { },
0285 };
0286
0287 MODULE_DEVICE_TABLE(i2c, max20086_i2c_id);
0288
0289 static const struct of_device_id max20086_dt_ids[] = {
0290 {
0291 .compatible = "maxim,max20086",
0292 .data = &(const struct max20086_chip_info) {
0293 .id = MAX20086_DEVICE_ID_MAX20086,
0294 .num_outputs = 4,
0295 }
0296 }, {
0297 .compatible = "maxim,max20087",
0298 .data = &(const struct max20086_chip_info) {
0299 .id = MAX20086_DEVICE_ID_MAX20087,
0300 .num_outputs = 4,
0301 }
0302 }, {
0303 .compatible = "maxim,max20088",
0304 .data = &(const struct max20086_chip_info) {
0305 .id = MAX20086_DEVICE_ID_MAX20088,
0306 .num_outputs = 2,
0307 }
0308 }, {
0309 .compatible = "maxim,max20089",
0310 .data = &(const struct max20086_chip_info) {
0311 .id = MAX20086_DEVICE_ID_MAX20089,
0312 .num_outputs = 2,
0313 }
0314 },
0315 { },
0316 };
0317
0318 MODULE_DEVICE_TABLE(of, max20086_dt_ids);
0319
0320 static struct i2c_driver max20086_regulator_driver = {
0321 .driver = {
0322 .name = "max20086",
0323 .of_match_table = of_match_ptr(max20086_dt_ids),
0324 },
0325 .probe_new = max20086_i2c_probe,
0326 .id_table = max20086_i2c_id,
0327 };
0328
0329 module_i2c_driver(max20086_regulator_driver);
0330
0331 MODULE_AUTHOR("Watson Chow <watson.chow@avnet.com>");
0332 MODULE_DESCRIPTION("MAX20086-MAX20089 Camera Power Protector Driver");
0333 MODULE_LICENSE("GPL");