1 // SPDX-License-Identifier: GPL-2.0-or-later 2 // 3 // max20086-regulator.c - MAX20086-MAX20089 camera power protector driver 4 // 5 // Copyright (C) 2022 Laurent Pinchart <laurent.pinchart@idesonboard.com> 6 // Copyright (C) 2018 Avnet, Inc. 7 8 #include <linux/cleanup.h> 9 #include <linux/err.h> 10 #include <linux/gpio/consumer.h> 11 #include <linux/i2c.h> 12 #include <linux/module.h> 13 #include <linux/regmap.h> 14 #include <linux/regulator/driver.h> 15 #include <linux/regulator/machine.h> 16 #include <linux/regulator/of_regulator.h> 17 #include <linux/slab.h> 18 19 /* Register Offset */ 20 #define MAX20086_REG_MASK 0x00 21 #define MAX20086_REG_CONFIG 0x01 22 #define MAX20086_REG_ID 0x02 23 #define MAX20086_REG_STAT1 0x03 24 #define MAX20086_REG_STAT2_L 0x04 25 #define MAX20086_REG_STAT2_H 0x05 26 #define MAX20086_REG_ADC1 0x06 27 #define MAX20086_REG_ADC2 0x07 28 #define MAX20086_REG_ADC3 0x08 29 #define MAX20086_REG_ADC4 0x09 30 31 /* DEVICE IDs */ 32 #define MAX20086_DEVICE_ID_MAX20086 0x30 33 #define MAX20086_DEVICE_ID_MAX20087 0x20 34 #define MAX20086_DEVICE_ID_MAX20088 0x10 35 #define MAX20086_DEVICE_ID_MAX20089 0x00 36 #define DEVICE_ID_MASK 0xf0 37 38 /* Register bits */ 39 #define MAX20086_EN_MASK 0x0f 40 #define MAX20086_EN_OUT1 0x01 41 #define MAX20086_EN_OUT2 0x02 42 #define MAX20086_EN_OUT3 0x04 43 #define MAX20086_EN_OUT4 0x08 44 #define MAX20086_INT_DISABLE_ALL 0x3f 45 46 #define MAX20086_MAX_REGULATORS 4 47 48 struct max20086_chip_info { 49 u8 id; 50 unsigned int num_outputs; 51 }; 52 53 struct max20086_regulator { 54 struct device_node *of_node; 55 struct regulator_init_data *init_data; 56 const struct regulator_desc *desc; 57 struct regulator_dev *rdev; 58 }; 59 60 struct max20086 { 61 struct device *dev; 62 struct regmap *regmap; 63 struct gpio_desc *ena_gpiod; 64 65 const struct max20086_chip_info *info; 66 67 struct max20086_regulator regulators[MAX20086_MAX_REGULATORS]; 68 }; 69 70 static const struct regulator_ops max20086_buck_ops = { 71 .enable = regulator_enable_regmap, 72 .disable = regulator_disable_regmap, 73 .is_enabled = regulator_is_enabled_regmap, 74 }; 75 76 #define MAX20086_REGULATOR_DESC(n) \ 77 { \ 78 .name = "OUT"#n, \ 79 .supply_name = "in", \ 80 .id = (n) - 1, \ 81 .ops = &max20086_buck_ops, \ 82 .type = REGULATOR_VOLTAGE, \ 83 .owner = THIS_MODULE, \ 84 .enable_reg = MAX20086_REG_CONFIG, \ 85 .enable_mask = 1 << ((n) - 1), \ 86 .enable_val = 1 << ((n) - 1), \ 87 .disable_val = 0, \ 88 } 89 90 static const char * const max20086_output_names[] = { 91 "OUT1", 92 "OUT2", 93 "OUT3", 94 "OUT4", 95 }; 96 97 static const struct regulator_desc max20086_regulators[] = { 98 MAX20086_REGULATOR_DESC(1), 99 MAX20086_REGULATOR_DESC(2), 100 MAX20086_REGULATOR_DESC(3), 101 MAX20086_REGULATOR_DESC(4), 102 }; 103 104 static int max20086_regulators_register(struct max20086 *chip) 105 { 106 unsigned int i; 107 108 for (i = 0; i < chip->info->num_outputs; i++) { 109 struct max20086_regulator *reg = &chip->regulators[i]; 110 struct regulator_config config = { }; 111 struct regulator_dev *rdev; 112 113 config.dev = chip->dev; 114 config.init_data = reg->init_data; 115 config.driver_data = chip; 116 config.of_node = reg->of_node; 117 config.regmap = chip->regmap; 118 config.ena_gpiod = chip->ena_gpiod; 119 120 rdev = devm_regulator_register(chip->dev, reg->desc, &config); 121 if (IS_ERR(rdev)) { 122 dev_err(chip->dev, 123 "Failed to register regulator output %s\n", 124 reg->desc->name); 125 return PTR_ERR(rdev); 126 } 127 128 reg->rdev = rdev; 129 } 130 131 return 0; 132 } 133 134 static int max20086_parse_regulators_dt(struct max20086 *chip, bool *boot_on) 135 { 136 struct of_regulator_match *matches; 137 unsigned int i; 138 int ret; 139 140 struct device_node *node __free(device_node) = 141 of_get_child_by_name(chip->dev->of_node, "regulators"); 142 if (!node) { 143 dev_err(chip->dev, "regulators node not found\n"); 144 return -ENODEV; 145 } 146 147 matches = devm_kcalloc(chip->dev, chip->info->num_outputs, 148 sizeof(*matches), GFP_KERNEL); 149 if (!matches) 150 return -ENOMEM; 151 152 for (i = 0; i < chip->info->num_outputs; ++i) 153 matches[i].name = max20086_output_names[i]; 154 155 ret = of_regulator_match(chip->dev, node, matches, 156 chip->info->num_outputs); 157 if (ret < 0) { 158 dev_err(chip->dev, "Failed to match regulators\n"); 159 return -EINVAL; 160 } 161 162 *boot_on = false; 163 164 for (i = 0; i < chip->info->num_outputs; i++) { 165 struct max20086_regulator *reg = &chip->regulators[i]; 166 167 reg->init_data = matches[i].init_data; 168 reg->of_node = matches[i].of_node; 169 reg->desc = &max20086_regulators[i]; 170 171 if (reg->init_data) { 172 if (reg->init_data->constraints.always_on || 173 reg->init_data->constraints.boot_on) 174 *boot_on = true; 175 } 176 } 177 178 return 0; 179 } 180 181 static int max20086_detect(struct max20086 *chip) 182 { 183 unsigned int data; 184 int ret; 185 186 ret = regmap_read(chip->regmap, MAX20086_REG_ID, &data); 187 if (ret < 0) { 188 dev_err(chip->dev, "Failed to read DEVICE_ID reg: %d\n", ret); 189 return ret; 190 } 191 192 if ((data & DEVICE_ID_MASK) != chip->info->id) { 193 dev_err(chip->dev, "Invalid device ID 0x%02x\n", data); 194 return -ENXIO; 195 } 196 197 return 0; 198 } 199 200 static bool max20086_gen_is_writeable_reg(struct device *dev, unsigned int reg) 201 { 202 switch (reg) { 203 case MAX20086_REG_MASK: 204 case MAX20086_REG_CONFIG: 205 return true; 206 default: 207 return false; 208 } 209 } 210 211 static const struct regmap_config max20086_regmap_config = { 212 .reg_bits = 8, 213 .val_bits = 8, 214 .writeable_reg = max20086_gen_is_writeable_reg, 215 .max_register = 0x9, 216 .cache_type = REGCACHE_NONE, 217 }; 218 219 static int max20086_i2c_probe(struct i2c_client *i2c) 220 { 221 struct max20086 *chip; 222 enum gpiod_flags flags; 223 bool boot_on; 224 int ret; 225 226 chip = devm_kzalloc(&i2c->dev, sizeof(*chip), GFP_KERNEL); 227 if (!chip) 228 return -ENOMEM; 229 230 chip->dev = &i2c->dev; 231 chip->info = i2c_get_match_data(i2c); 232 233 i2c_set_clientdata(i2c, chip); 234 235 chip->regmap = devm_regmap_init_i2c(i2c, &max20086_regmap_config); 236 if (IS_ERR(chip->regmap)) { 237 ret = PTR_ERR(chip->regmap); 238 dev_err(chip->dev, "Failed to allocate register map: %d\n", ret); 239 return ret; 240 } 241 242 ret = max20086_parse_regulators_dt(chip, &boot_on); 243 if (ret < 0) 244 return ret; 245 246 ret = max20086_detect(chip); 247 if (ret < 0) 248 return ret; 249 250 /* Until IRQ support is added, just disable all interrupts. */ 251 ret = regmap_update_bits(chip->regmap, MAX20086_REG_MASK, 252 MAX20086_INT_DISABLE_ALL, 253 MAX20086_INT_DISABLE_ALL); 254 if (ret < 0) { 255 dev_err(chip->dev, "Failed to disable interrupts: %d\n", ret); 256 return ret; 257 } 258 259 /* 260 * Get the enable GPIO. If any of the outputs is marked as being 261 * enabled at boot, request the GPIO with an initial high state to 262 * avoid disabling outputs that may have been turned on by the boot 263 * loader. Otherwise, request it with a low state to enter lower-power 264 * shutdown. 265 */ 266 flags = boot_on ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; 267 chip->ena_gpiod = devm_gpiod_get_optional(chip->dev, "enable", flags); 268 if (IS_ERR(chip->ena_gpiod)) { 269 ret = PTR_ERR(chip->ena_gpiod); 270 dev_err(chip->dev, "Failed to get enable GPIO: %d\n", ret); 271 return ret; 272 } 273 274 ret = max20086_regulators_register(chip); 275 if (ret < 0) { 276 dev_err(chip->dev, "Failed to register regulators: %d\n", ret); 277 return ret; 278 } 279 280 return 0; 281 } 282 283 static const struct max20086_chip_info max20086_chip_info = { 284 .id = MAX20086_DEVICE_ID_MAX20086, 285 .num_outputs = 4, 286 }; 287 288 static const struct max20086_chip_info max20087_chip_info = { 289 .id = MAX20086_DEVICE_ID_MAX20087, 290 .num_outputs = 4, 291 }; 292 293 static const struct max20086_chip_info max20088_chip_info = { 294 .id = MAX20086_DEVICE_ID_MAX20088, 295 .num_outputs = 2, 296 }; 297 298 static const struct max20086_chip_info max20089_chip_info = { 299 .id = MAX20086_DEVICE_ID_MAX20089, 300 .num_outputs = 2, 301 }; 302 303 static const struct i2c_device_id max20086_i2c_id[] = { 304 { "max20086", (kernel_ulong_t)&max20086_chip_info }, 305 { "max20087", (kernel_ulong_t)&max20087_chip_info }, 306 { "max20088", (kernel_ulong_t)&max20088_chip_info }, 307 { "max20089", (kernel_ulong_t)&max20089_chip_info }, 308 { /* Sentinel */ } 309 }; 310 MODULE_DEVICE_TABLE(i2c, max20086_i2c_id); 311 312 static const struct of_device_id max20086_dt_ids[] __maybe_unused = { 313 { .compatible = "maxim,max20086", .data = &max20086_chip_info }, 314 { .compatible = "maxim,max20087", .data = &max20087_chip_info }, 315 { .compatible = "maxim,max20088", .data = &max20088_chip_info }, 316 { .compatible = "maxim,max20089", .data = &max20089_chip_info }, 317 { /* Sentinel */ } 318 }; 319 MODULE_DEVICE_TABLE(of, max20086_dt_ids); 320 321 static struct i2c_driver max20086_regulator_driver = { 322 .driver = { 323 .name = "max20086", 324 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 325 .of_match_table = of_match_ptr(max20086_dt_ids), 326 }, 327 .probe = max20086_i2c_probe, 328 .id_table = max20086_i2c_id, 329 }; 330 331 module_i2c_driver(max20086_regulator_driver); 332 333 MODULE_AUTHOR("Watson Chow <watson.chow@avnet.com>"); 334 MODULE_DESCRIPTION("MAX20086-MAX20089 Camera Power Protector Driver"); 335 MODULE_LICENSE("GPL"); 336