1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // max20086-regulator.c - MAX20086-MAX20089 camera power protector driver
5 // Copyright (C) 2022 Laurent Pinchart <laurent.pinchart@idesonboard.com>
6 // Copyright (C) 2018 Avnet, Inc.
9 #include <linux/gpio/consumer.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/regulator/machine.h>
15 #include <linux/regulator/of_regulator.h>
16 #include <linux/slab.h>
19 #define MAX20086_REG_MASK 0x00
20 #define MAX20086_REG_CONFIG 0x01
21 #define MAX20086_REG_ID 0x02
22 #define MAX20086_REG_STAT1 0x03
23 #define MAX20086_REG_STAT2_L 0x04
24 #define MAX20086_REG_STAT2_H 0x05
25 #define MAX20086_REG_ADC1 0x06
26 #define MAX20086_REG_ADC2 0x07
27 #define MAX20086_REG_ADC3 0x08
28 #define MAX20086_REG_ADC4 0x09
31 #define MAX20086_DEVICE_ID_MAX20086 0x40
32 #define MAX20086_DEVICE_ID_MAX20087 0x20
33 #define MAX20086_DEVICE_ID_MAX20088 0x10
34 #define MAX20086_DEVICE_ID_MAX20089 0x00
35 #define DEVICE_ID_MASK 0xf0
38 #define MAX20086_EN_MASK 0x0f
39 #define MAX20086_EN_OUT1 0x01
40 #define MAX20086_EN_OUT2 0x02
41 #define MAX20086_EN_OUT3 0x04
42 #define MAX20086_EN_OUT4 0x08
43 #define MAX20086_INT_DISABLE_ALL 0x3f
45 #define MAX20086_MAX_REGULATORS 4
47 struct max20086_chip_info {
49 unsigned int num_outputs;
52 struct max20086_regulator {
53 struct device_node *of_node;
54 struct regulator_init_data *init_data;
55 const struct regulator_desc *desc;
56 struct regulator_dev *rdev;
61 struct regmap *regmap;
62 struct gpio_desc *ena_gpiod;
64 const struct max20086_chip_info *info;
66 struct max20086_regulator regulators[MAX20086_MAX_REGULATORS];
69 static const struct regulator_ops max20086_buck_ops = {
70 .enable = regulator_enable_regmap,
71 .disable = regulator_disable_regmap,
72 .is_enabled = regulator_is_enabled_regmap,
75 #define MAX20086_REGULATOR_DESC(n) \
78 .supply_name = "in", \
80 .ops = &max20086_buck_ops, \
81 .type = REGULATOR_VOLTAGE, \
82 .owner = THIS_MODULE, \
83 .enable_reg = MAX20086_REG_CONFIG, \
84 .enable_mask = 1 << ((n) - 1), \
85 .enable_val = 1 << ((n) - 1), \
89 static const char * const max20086_output_names[] = {
96 static const struct regulator_desc max20086_regulators[] = {
97 MAX20086_REGULATOR_DESC(1),
98 MAX20086_REGULATOR_DESC(2),
99 MAX20086_REGULATOR_DESC(3),
100 MAX20086_REGULATOR_DESC(4),
103 static int max20086_regulators_register(struct max20086 *chip)
107 for (i = 0; i < chip->info->num_outputs; i++) {
108 struct max20086_regulator *reg = &chip->regulators[i];
109 struct regulator_config config = { };
110 struct regulator_dev *rdev;
112 config.dev = chip->dev;
113 config.init_data = reg->init_data;
114 config.driver_data = chip;
115 config.of_node = reg->of_node;
116 config.regmap = chip->regmap;
117 config.ena_gpiod = chip->ena_gpiod;
119 rdev = devm_regulator_register(chip->dev, reg->desc, &config);
122 "Failed to register regulator output %s\n",
124 return PTR_ERR(rdev);
133 static int max20086_parse_regulators_dt(struct max20086 *chip, bool *boot_on)
135 struct of_regulator_match matches[MAX20086_MAX_REGULATORS] = { };
136 struct device_node *node;
140 node = of_get_child_by_name(chip->dev->of_node, "regulators");
142 dev_err(chip->dev, "regulators node not found\n");
146 for (i = 0; i < chip->info->num_outputs; ++i)
147 matches[i].name = max20086_output_names[i];
149 ret = of_regulator_match(chip->dev, node, matches,
150 chip->info->num_outputs);
153 dev_err(chip->dev, "Failed to match regulators\n");
159 for (i = 0; i < chip->info->num_outputs; i++) {
160 struct max20086_regulator *reg = &chip->regulators[i];
162 reg->init_data = matches[i].init_data;
163 reg->of_node = matches[i].of_node;
164 reg->desc = &max20086_regulators[i];
166 if (reg->init_data) {
167 if (reg->init_data->constraints.always_on ||
168 reg->init_data->constraints.boot_on)
176 static int max20086_detect(struct max20086 *chip)
181 ret = regmap_read(chip->regmap, MAX20086_REG_ID, &data);
183 dev_err(chip->dev, "Failed to read DEVICE_ID reg: %d\n", ret);
187 if ((data & DEVICE_ID_MASK) != chip->info->id) {
188 dev_err(chip->dev, "Invalid device ID 0x%02x\n", data);
195 static bool max20086_gen_is_writeable_reg(struct device *dev, unsigned int reg)
198 case MAX20086_REG_MASK:
199 case MAX20086_REG_CONFIG:
206 static const struct regmap_config max20086_regmap_config = {
209 .writeable_reg = max20086_gen_is_writeable_reg,
211 .cache_type = REGCACHE_NONE,
214 static int max20086_i2c_probe(struct i2c_client *i2c)
216 struct max20086 *chip;
217 enum gpiod_flags flags;
221 chip = devm_kzalloc(&i2c->dev, sizeof(*chip), GFP_KERNEL);
225 chip->dev = &i2c->dev;
226 chip->info = device_get_match_data(chip->dev);
228 i2c_set_clientdata(i2c, chip);
230 chip->regmap = devm_regmap_init_i2c(i2c, &max20086_regmap_config);
231 if (IS_ERR(chip->regmap)) {
232 ret = PTR_ERR(chip->regmap);
233 dev_err(chip->dev, "Failed to allocate register map: %d\n", ret);
237 ret = max20086_parse_regulators_dt(chip, &boot_on);
241 ret = max20086_detect(chip);
245 /* Until IRQ support is added, just disable all interrupts. */
246 ret = regmap_update_bits(chip->regmap, MAX20086_REG_MASK,
247 MAX20086_INT_DISABLE_ALL,
248 MAX20086_INT_DISABLE_ALL);
250 dev_err(chip->dev, "Failed to disable interrupts: %d\n", ret);
255 * Get the enable GPIO. If any of the outputs is marked as being
256 * enabled at boot, request the GPIO with an initial high state to
257 * avoid disabling outputs that may have been turned on by the boot
258 * loader. Otherwise, request it with a low state to enter lower-power
261 flags = boot_on ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
262 chip->ena_gpiod = devm_gpiod_get(chip->dev, "enable", flags);
263 if (IS_ERR(chip->ena_gpiod)) {
264 ret = PTR_ERR(chip->ena_gpiod);
265 dev_err(chip->dev, "Failed to get enable GPIO: %d\n", ret);
269 ret = max20086_regulators_register(chip);
271 dev_err(chip->dev, "Failed to register regulators: %d\n", ret);
278 static const struct i2c_device_id max20086_i2c_id[] = {
286 MODULE_DEVICE_TABLE(i2c, max20086_i2c_id);
288 static const struct of_device_id max20086_dt_ids[] __maybe_unused = {
290 .compatible = "maxim,max20086",
291 .data = &(const struct max20086_chip_info) {
292 .id = MAX20086_DEVICE_ID_MAX20086,
296 .compatible = "maxim,max20087",
297 .data = &(const struct max20086_chip_info) {
298 .id = MAX20086_DEVICE_ID_MAX20087,
302 .compatible = "maxim,max20088",
303 .data = &(const struct max20086_chip_info) {
304 .id = MAX20086_DEVICE_ID_MAX20088,
308 .compatible = "maxim,max20089",
309 .data = &(const struct max20086_chip_info) {
310 .id = MAX20086_DEVICE_ID_MAX20089,
317 MODULE_DEVICE_TABLE(of, max20086_dt_ids);
319 static struct i2c_driver max20086_regulator_driver = {
322 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
323 .of_match_table = of_match_ptr(max20086_dt_ids),
325 .probe = max20086_i2c_probe,
326 .id_table = max20086_i2c_id,
329 module_i2c_driver(max20086_regulator_driver);
331 MODULE_AUTHOR("Watson Chow <watson.chow@avnet.com>");
332 MODULE_DESCRIPTION("MAX20086-MAX20089 Camera Power Protector Driver");
333 MODULE_LICENSE("GPL");