1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // pv88060-regulator.c - Regulator device driver for PV88060 4 // Copyright (C) 2015 Powerventure Semiconductor Ltd. 5 6 #include <linux/err.h> 7 #include <linux/i2c.h> 8 #include <linux/module.h> 9 #include <linux/init.h> 10 #include <linux/slab.h> 11 #include <linux/regulator/driver.h> 12 #include <linux/regulator/machine.h> 13 #include <linux/regmap.h> 14 #include <linux/irq.h> 15 #include <linux/interrupt.h> 16 #include <linux/regulator/of_regulator.h> 17 #include "pv88060-regulator.h" 18 19 #define PV88060_MAX_REGULATORS 14 20 21 /* PV88060 REGULATOR IDs */ 22 enum { 23 /* BUCKs */ 24 PV88060_ID_BUCK1, 25 26 /* LDOs */ 27 PV88060_ID_LDO1, 28 PV88060_ID_LDO2, 29 PV88060_ID_LDO3, 30 PV88060_ID_LDO4, 31 PV88060_ID_LDO5, 32 PV88060_ID_LDO6, 33 PV88060_ID_LDO7, 34 35 /* SWTs */ 36 PV88060_ID_SW1, 37 PV88060_ID_SW2, 38 PV88060_ID_SW3, 39 PV88060_ID_SW4, 40 PV88060_ID_SW5, 41 PV88060_ID_SW6, 42 }; 43 44 struct pv88060_regulator { 45 struct regulator_desc desc; 46 unsigned int conf; /* buck configuration register */ 47 }; 48 49 struct pv88060 { 50 struct device *dev; 51 struct regmap *regmap; 52 struct regulator_dev *rdev[PV88060_MAX_REGULATORS]; 53 }; 54 55 static const struct regmap_config pv88060_regmap_config = { 56 .reg_bits = 8, 57 .val_bits = 8, 58 }; 59 60 /* Current limits array (in uA) for BUCK1 61 * Entry indexes corresponds to register values. 62 */ 63 64 static const unsigned int pv88060_buck1_limits[] = { 65 1496000, 2393000, 3291000, 4189000 66 }; 67 68 static unsigned int pv88060_buck_get_mode(struct regulator_dev *rdev) 69 { 70 struct pv88060_regulator *info = rdev_get_drvdata(rdev); 71 unsigned int data; 72 int ret, mode = 0; 73 74 ret = regmap_read(rdev->regmap, info->conf, &data); 75 if (ret < 0) 76 return ret; 77 78 switch (data & PV88060_BUCK_MODE_MASK) { 79 case PV88060_BUCK_MODE_SYNC: 80 mode = REGULATOR_MODE_FAST; 81 break; 82 case PV88060_BUCK_MODE_AUTO: 83 mode = REGULATOR_MODE_NORMAL; 84 break; 85 case PV88060_BUCK_MODE_SLEEP: 86 mode = REGULATOR_MODE_STANDBY; 87 break; 88 } 89 90 return mode; 91 } 92 93 static int pv88060_buck_set_mode(struct regulator_dev *rdev, 94 unsigned int mode) 95 { 96 struct pv88060_regulator *info = rdev_get_drvdata(rdev); 97 int val = 0; 98 99 switch (mode) { 100 case REGULATOR_MODE_FAST: 101 val = PV88060_BUCK_MODE_SYNC; 102 break; 103 case REGULATOR_MODE_NORMAL: 104 val = PV88060_BUCK_MODE_AUTO; 105 break; 106 case REGULATOR_MODE_STANDBY: 107 val = PV88060_BUCK_MODE_SLEEP; 108 break; 109 default: 110 return -EINVAL; 111 } 112 113 return regmap_update_bits(rdev->regmap, info->conf, 114 PV88060_BUCK_MODE_MASK, val); 115 } 116 117 static const struct regulator_ops pv88060_buck_ops = { 118 .get_mode = pv88060_buck_get_mode, 119 .set_mode = pv88060_buck_set_mode, 120 .enable = regulator_enable_regmap, 121 .disable = regulator_disable_regmap, 122 .is_enabled = regulator_is_enabled_regmap, 123 .set_voltage_sel = regulator_set_voltage_sel_regmap, 124 .get_voltage_sel = regulator_get_voltage_sel_regmap, 125 .list_voltage = regulator_list_voltage_linear, 126 .set_current_limit = regulator_set_current_limit_regmap, 127 .get_current_limit = regulator_get_current_limit_regmap, 128 }; 129 130 static const struct regulator_ops pv88060_ldo_ops = { 131 .enable = regulator_enable_regmap, 132 .disable = regulator_disable_regmap, 133 .is_enabled = regulator_is_enabled_regmap, 134 .set_voltage_sel = regulator_set_voltage_sel_regmap, 135 .get_voltage_sel = regulator_get_voltage_sel_regmap, 136 .list_voltage = regulator_list_voltage_linear, 137 }; 138 139 static const struct regulator_ops pv88060_sw_ops = { 140 .enable = regulator_enable_regmap, 141 .disable = regulator_disable_regmap, 142 .is_enabled = regulator_is_enabled_regmap, 143 }; 144 145 #define PV88060_BUCK(chip, regl_name, min, step, max, limits_array) \ 146 {\ 147 .desc = {\ 148 .id = chip##_ID_##regl_name,\ 149 .name = __stringify(chip##_##regl_name),\ 150 .of_match = of_match_ptr(#regl_name),\ 151 .regulators_node = of_match_ptr("regulators"),\ 152 .type = REGULATOR_VOLTAGE,\ 153 .owner = THIS_MODULE,\ 154 .ops = &pv88060_buck_ops,\ 155 .min_uV = min,\ 156 .uV_step = step,\ 157 .n_voltages = ((max) - (min))/(step) + 1,\ 158 .enable_reg = PV88060_REG_##regl_name##_CONF0,\ 159 .enable_mask = PV88060_BUCK_EN, \ 160 .vsel_reg = PV88060_REG_##regl_name##_CONF0,\ 161 .vsel_mask = PV88060_VBUCK_MASK,\ 162 .curr_table = limits_array,\ 163 .n_current_limits = ARRAY_SIZE(limits_array),\ 164 .csel_reg = PV88060_REG_##regl_name##_CONF1,\ 165 .csel_mask = PV88060_BUCK_ILIM_MASK,\ 166 },\ 167 .conf = PV88060_REG_##regl_name##_CONF1,\ 168 } 169 170 #define PV88060_LDO(chip, regl_name, min, step, max) \ 171 {\ 172 .desc = {\ 173 .id = chip##_ID_##regl_name,\ 174 .name = __stringify(chip##_##regl_name),\ 175 .of_match = of_match_ptr(#regl_name),\ 176 .regulators_node = of_match_ptr("regulators"),\ 177 .type = REGULATOR_VOLTAGE,\ 178 .owner = THIS_MODULE,\ 179 .ops = &pv88060_ldo_ops,\ 180 .min_uV = min, \ 181 .uV_step = step, \ 182 .n_voltages = (step) ? ((max - min) / step + 1) : 1, \ 183 .enable_reg = PV88060_REG_##regl_name##_CONF, \ 184 .enable_mask = PV88060_LDO_EN, \ 185 .vsel_reg = PV88060_REG_##regl_name##_CONF, \ 186 .vsel_mask = PV88060_VLDO_MASK, \ 187 },\ 188 } 189 190 #define PV88060_SW(chip, regl_name, max) \ 191 {\ 192 .desc = {\ 193 .id = chip##_ID_##regl_name,\ 194 .name = __stringify(chip##_##regl_name),\ 195 .of_match = of_match_ptr(#regl_name),\ 196 .regulators_node = of_match_ptr("regulators"),\ 197 .type = REGULATOR_VOLTAGE,\ 198 .owner = THIS_MODULE,\ 199 .ops = &pv88060_sw_ops,\ 200 .fixed_uV = max,\ 201 .n_voltages = 1,\ 202 .enable_reg = PV88060_REG_##regl_name##_CONF,\ 203 .enable_mask = PV88060_SW_EN,\ 204 },\ 205 } 206 207 static const struct pv88060_regulator pv88060_regulator_info[] = { 208 PV88060_BUCK(PV88060, BUCK1, 2800000, 12500, 4387500, 209 pv88060_buck1_limits), 210 PV88060_LDO(PV88060, LDO1, 1200000, 50000, 3350000), 211 PV88060_LDO(PV88060, LDO2, 1200000, 50000, 3350000), 212 PV88060_LDO(PV88060, LDO3, 1200000, 50000, 3350000), 213 PV88060_LDO(PV88060, LDO4, 1200000, 50000, 3350000), 214 PV88060_LDO(PV88060, LDO5, 1200000, 50000, 3350000), 215 PV88060_LDO(PV88060, LDO6, 1200000, 50000, 3350000), 216 PV88060_LDO(PV88060, LDO7, 1200000, 50000, 3350000), 217 PV88060_SW(PV88060, SW1, 5000000), 218 PV88060_SW(PV88060, SW2, 5000000), 219 PV88060_SW(PV88060, SW3, 5000000), 220 PV88060_SW(PV88060, SW4, 5000000), 221 PV88060_SW(PV88060, SW5, 5000000), 222 PV88060_SW(PV88060, SW6, 5000000), 223 }; 224 225 static irqreturn_t pv88060_irq_handler(int irq, void *data) 226 { 227 struct pv88060 *chip = data; 228 int i, reg_val, err, ret = IRQ_NONE; 229 230 err = regmap_read(chip->regmap, PV88060_REG_EVENT_A, ®_val); 231 if (err < 0) 232 goto error_i2c; 233 234 if (reg_val & PV88060_E_VDD_FLT) { 235 for (i = 0; i < PV88060_MAX_REGULATORS; i++) { 236 if (chip->rdev[i] != NULL) 237 regulator_notifier_call_chain(chip->rdev[i], 238 REGULATOR_EVENT_UNDER_VOLTAGE, 239 NULL); 240 } 241 242 err = regmap_write(chip->regmap, PV88060_REG_EVENT_A, 243 PV88060_E_VDD_FLT); 244 if (err < 0) 245 goto error_i2c; 246 247 ret = IRQ_HANDLED; 248 } 249 250 if (reg_val & PV88060_E_OVER_TEMP) { 251 for (i = 0; i < PV88060_MAX_REGULATORS; i++) { 252 if (chip->rdev[i] != NULL) 253 regulator_notifier_call_chain(chip->rdev[i], 254 REGULATOR_EVENT_OVER_TEMP, 255 NULL); 256 } 257 258 err = regmap_write(chip->regmap, PV88060_REG_EVENT_A, 259 PV88060_E_OVER_TEMP); 260 if (err < 0) 261 goto error_i2c; 262 263 ret = IRQ_HANDLED; 264 } 265 266 return ret; 267 268 error_i2c: 269 dev_err(chip->dev, "I2C error : %d\n", err); 270 return IRQ_NONE; 271 } 272 273 /* 274 * I2C driver interface functions 275 */ 276 static int pv88060_i2c_probe(struct i2c_client *i2c) 277 { 278 struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev); 279 struct pv88060 *chip; 280 struct regulator_config config = { }; 281 int error, i, ret = 0; 282 283 chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88060), GFP_KERNEL); 284 if (!chip) 285 return -ENOMEM; 286 287 chip->dev = &i2c->dev; 288 chip->regmap = devm_regmap_init_i2c(i2c, &pv88060_regmap_config); 289 if (IS_ERR(chip->regmap)) { 290 error = PTR_ERR(chip->regmap); 291 dev_err(chip->dev, "Failed to allocate register map: %d\n", 292 error); 293 return error; 294 } 295 296 i2c_set_clientdata(i2c, chip); 297 298 if (i2c->irq != 0) { 299 ret = regmap_write(chip->regmap, PV88060_REG_MASK_A, 0xFF); 300 if (ret < 0) { 301 dev_err(chip->dev, 302 "Failed to mask A reg: %d\n", ret); 303 return ret; 304 } 305 306 ret = regmap_write(chip->regmap, PV88060_REG_MASK_B, 0xFF); 307 if (ret < 0) { 308 dev_err(chip->dev, 309 "Failed to mask B reg: %d\n", ret); 310 return ret; 311 } 312 313 ret = regmap_write(chip->regmap, PV88060_REG_MASK_C, 0xFF); 314 if (ret < 0) { 315 dev_err(chip->dev, 316 "Failed to mask C reg: %d\n", ret); 317 return ret; 318 } 319 320 ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, 321 pv88060_irq_handler, 322 IRQF_TRIGGER_LOW|IRQF_ONESHOT, 323 "pv88060", chip); 324 if (ret != 0) { 325 dev_err(chip->dev, "Failed to request IRQ: %d\n", 326 i2c->irq); 327 return ret; 328 } 329 330 ret = regmap_update_bits(chip->regmap, PV88060_REG_MASK_A, 331 PV88060_M_VDD_FLT | PV88060_M_OVER_TEMP, 0); 332 if (ret < 0) { 333 dev_err(chip->dev, 334 "Failed to update mask reg: %d\n", ret); 335 return ret; 336 } 337 338 } else { 339 dev_warn(chip->dev, "No IRQ configured\n"); 340 } 341 342 config.dev = chip->dev; 343 config.regmap = chip->regmap; 344 345 for (i = 0; i < PV88060_MAX_REGULATORS; i++) { 346 if (init_data) 347 config.init_data = &init_data[i]; 348 349 config.driver_data = (void *)&pv88060_regulator_info[i]; 350 chip->rdev[i] = devm_regulator_register(chip->dev, 351 &pv88060_regulator_info[i].desc, &config); 352 if (IS_ERR(chip->rdev[i])) { 353 dev_err(chip->dev, 354 "Failed to register PV88060 regulator\n"); 355 return PTR_ERR(chip->rdev[i]); 356 } 357 } 358 359 return 0; 360 } 361 362 static const struct i2c_device_id pv88060_i2c_id[] = { 363 {"pv88060", 0}, 364 {}, 365 }; 366 MODULE_DEVICE_TABLE(i2c, pv88060_i2c_id); 367 368 #ifdef CONFIG_OF 369 static const struct of_device_id pv88060_dt_ids[] = { 370 { .compatible = "pvs,pv88060", .data = &pv88060_i2c_id[0] }, 371 {}, 372 }; 373 MODULE_DEVICE_TABLE(of, pv88060_dt_ids); 374 #endif 375 376 static struct i2c_driver pv88060_regulator_driver = { 377 .driver = { 378 .name = "pv88060", 379 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 380 .of_match_table = of_match_ptr(pv88060_dt_ids), 381 }, 382 .probe = pv88060_i2c_probe, 383 .id_table = pv88060_i2c_id, 384 }; 385 386 module_i2c_driver(pv88060_regulator_driver); 387 388 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>"); 389 MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88060"); 390 MODULE_LICENSE("GPL"); 391