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