Krystian Garbaciak | 69ca3e5 | 2013-07-29 19:00:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Regulator driver for DA9063 PMIC series |
| 3 | * |
| 4 | * Copyright 2012 Dialog Semiconductors Ltd. |
| 5 | * Copyright 2013 Philipp Zabel, Pengutronix |
| 6 | * |
| 7 | * Author: Krystian Garbaciak <krystian.garbaciak@diasemi.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
| 13 | * |
| 14 | */ |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/regmap.h> |
| 23 | #include <linux/regulator/driver.h> |
| 24 | #include <linux/regulator/machine.h> |
| 25 | #include <linux/regulator/of_regulator.h> |
| 26 | #include <linux/mfd/da9063/core.h> |
| 27 | #include <linux/mfd/da9063/pdata.h> |
| 28 | #include <linux/mfd/da9063/registers.h> |
| 29 | |
| 30 | |
| 31 | /* Definition for registering regmap bit fields using a mask */ |
| 32 | #define BFIELD(_reg, _mask) \ |
| 33 | REG_FIELD(_reg, __builtin_ffs((int)_mask) - 1, \ |
| 34 | sizeof(unsigned int) * 8 - __builtin_clz((_mask)) - 1) |
| 35 | |
| 36 | /* Regulator capabilities and registers description */ |
| 37 | struct da9063_regulator_info { |
| 38 | struct regulator_desc desc; |
| 39 | |
| 40 | /* Current limiting */ |
| 41 | unsigned n_current_limits; |
| 42 | const int *current_limits; |
| 43 | |
| 44 | /* DA9063 main register fields */ |
| 45 | struct reg_field mode; /* buck mode of operation */ |
| 46 | struct reg_field suspend; |
| 47 | struct reg_field sleep; |
| 48 | struct reg_field suspend_sleep; |
| 49 | unsigned int suspend_vsel_reg; |
| 50 | struct reg_field ilimit; |
| 51 | |
| 52 | /* DA9063 event detection bit */ |
| 53 | struct reg_field oc_event; |
| 54 | }; |
| 55 | |
| 56 | /* Macros for LDO */ |
| 57 | #define DA9063_LDO(chip, regl_name, min_mV, step_mV, max_mV) \ |
| 58 | .desc.id = chip##_ID_##regl_name, \ |
| 59 | .desc.name = __stringify(chip##_##regl_name), \ |
| 60 | .desc.ops = &da9063_ldo_ops, \ |
| 61 | .desc.min_uV = (min_mV) * 1000, \ |
| 62 | .desc.uV_step = (step_mV) * 1000, \ |
| 63 | .desc.n_voltages = (((max_mV) - (min_mV))/(step_mV) + 1), \ |
| 64 | .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \ |
| 65 | .desc.enable_mask = DA9063_LDO_EN, \ |
| 66 | .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \ |
| 67 | .desc.vsel_mask = DA9063_V##regl_name##_MASK, \ |
| 68 | .desc.linear_min_sel = DA9063_V##regl_name##_BIAS, \ |
| 69 | .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_LDO_SL), \ |
| 70 | .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_LDO_SL), \ |
| 71 | .suspend_vsel_reg = DA9063_REG_V##regl_name##_B |
| 72 | |
| 73 | /* Macros for voltage DC/DC converters (BUCKs) */ |
| 74 | #define DA9063_BUCK(chip, regl_name, min_mV, step_mV, max_mV, limits_array) \ |
| 75 | .desc.id = chip##_ID_##regl_name, \ |
| 76 | .desc.name = __stringify(chip##_##regl_name), \ |
| 77 | .desc.ops = &da9063_buck_ops, \ |
| 78 | .desc.min_uV = (min_mV) * 1000, \ |
| 79 | .desc.uV_step = (step_mV) * 1000, \ |
| 80 | .desc.n_voltages = ((max_mV) - (min_mV))/(step_mV) + 1, \ |
| 81 | .current_limits = limits_array, \ |
| 82 | .n_current_limits = ARRAY_SIZE(limits_array) |
| 83 | |
| 84 | #define DA9063_BUCK_COMMON_FIELDS(regl_name) \ |
| 85 | .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \ |
| 86 | .desc.enable_mask = DA9063_BUCK_EN, \ |
| 87 | .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \ |
| 88 | .desc.vsel_mask = DA9063_VBUCK_MASK, \ |
| 89 | .desc.linear_min_sel = DA9063_VBUCK_BIAS, \ |
| 90 | .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_BUCK_SL), \ |
| 91 | .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_BUCK_SL), \ |
| 92 | .suspend_vsel_reg = DA9063_REG_V##regl_name##_B, \ |
| 93 | .mode = BFIELD(DA9063_REG_##regl_name##_CFG, DA9063_BUCK_MODE_MASK) |
| 94 | |
| 95 | /* Defines asignment of regulators info table to chip model */ |
| 96 | struct da9063_dev_model { |
| 97 | const struct da9063_regulator_info *regulator_info; |
| 98 | unsigned n_regulators; |
| 99 | unsigned dev_model; |
| 100 | }; |
| 101 | |
| 102 | /* Single regulator settings */ |
| 103 | struct da9063_regulator { |
| 104 | struct regulator_desc desc; |
| 105 | struct regulator_dev *rdev; |
| 106 | struct da9063 *hw; |
| 107 | const struct da9063_regulator_info *info; |
| 108 | |
| 109 | struct regmap_field *mode; |
| 110 | struct regmap_field *suspend; |
| 111 | struct regmap_field *sleep; |
| 112 | struct regmap_field *suspend_sleep; |
| 113 | struct regmap_field *ilimit; |
| 114 | }; |
| 115 | |
| 116 | /* Encapsulates all information for the regulators driver */ |
| 117 | struct da9063_regulators { |
| 118 | int irq_ldo_lim; |
| 119 | int irq_uvov; |
| 120 | |
| 121 | unsigned n_regulators; |
| 122 | /* Array size to be defined during init. Keep at end. */ |
| 123 | struct da9063_regulator regulator[0]; |
| 124 | }; |
| 125 | |
| 126 | /* BUCK modes for DA9063 */ |
| 127 | enum { |
| 128 | BUCK_MODE_MANUAL, /* 0 */ |
| 129 | BUCK_MODE_SLEEP, /* 1 */ |
| 130 | BUCK_MODE_SYNC, /* 2 */ |
| 131 | BUCK_MODE_AUTO /* 3 */ |
| 132 | }; |
| 133 | |
| 134 | /* Regulator operations */ |
| 135 | |
| 136 | /* Current limits array (in uA) for BCORE1, BCORE2, BPRO. |
| 137 | Entry indexes corresponds to register values. */ |
| 138 | static const int da9063_buck_a_limits[] = { |
| 139 | 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000, |
| 140 | 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000 |
| 141 | }; |
| 142 | |
| 143 | /* Current limits array (in uA) for BMEM, BIO, BPERI. |
| 144 | Entry indexes corresponds to register values. */ |
| 145 | static const int da9063_buck_b_limits[] = { |
| 146 | 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000, |
| 147 | 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000 |
| 148 | }; |
| 149 | |
| 150 | /* Current limits array (in uA) for merged BCORE1 and BCORE2. |
| 151 | Entry indexes corresponds to register values. */ |
| 152 | static const int da9063_bcores_merged_limits[] = { |
| 153 | 1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000, |
| 154 | 2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000 |
| 155 | }; |
| 156 | |
| 157 | /* Current limits array (in uA) for merged BMEM and BIO. |
| 158 | Entry indexes corresponds to register values. */ |
| 159 | static const int da9063_bmem_bio_merged_limits[] = { |
| 160 | 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, |
| 161 | 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000 |
| 162 | }; |
| 163 | |
| 164 | static int da9063_set_current_limit(struct regulator_dev *rdev, |
| 165 | int min_uA, int max_uA) |
| 166 | { |
| 167 | struct da9063_regulator *regl = rdev_get_drvdata(rdev); |
| 168 | const struct da9063_regulator_info *rinfo = regl->info; |
Axel Lin | 556dcf9 | 2013-08-30 20:07:38 +0800 | [diff] [blame] | 169 | int n, tval; |
Krystian Garbaciak | 69ca3e5 | 2013-07-29 19:00:45 +0200 | [diff] [blame] | 170 | |
| 171 | for (n = 0; n < rinfo->n_current_limits; n++) { |
| 172 | tval = rinfo->current_limits[n]; |
Axel Lin | 556dcf9 | 2013-08-30 20:07:38 +0800 | [diff] [blame] | 173 | if (tval >= min_uA && tval <= max_uA) |
| 174 | return regmap_field_write(regl->ilimit, n); |
Krystian Garbaciak | 69ca3e5 | 2013-07-29 19:00:45 +0200 | [diff] [blame] | 175 | } |
Krystian Garbaciak | 69ca3e5 | 2013-07-29 19:00:45 +0200 | [diff] [blame] | 176 | |
Axel Lin | 556dcf9 | 2013-08-30 20:07:38 +0800 | [diff] [blame] | 177 | return -EINVAL; |
Krystian Garbaciak | 69ca3e5 | 2013-07-29 19:00:45 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static int da9063_get_current_limit(struct regulator_dev *rdev) |
| 181 | { |
| 182 | struct da9063_regulator *regl = rdev_get_drvdata(rdev); |
| 183 | const struct da9063_regulator_info *rinfo = regl->info; |
| 184 | unsigned int sel; |
| 185 | int ret; |
| 186 | |
| 187 | ret = regmap_field_read(regl->ilimit, &sel); |
| 188 | if (ret < 0) |
| 189 | return ret; |
| 190 | |
| 191 | if (sel >= rinfo->n_current_limits) |
| 192 | sel = rinfo->n_current_limits - 1; |
| 193 | |
| 194 | return rinfo->current_limits[sel]; |
| 195 | } |
| 196 | |
| 197 | static int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned mode) |
| 198 | { |
| 199 | struct da9063_regulator *regl = rdev_get_drvdata(rdev); |
| 200 | unsigned val; |
| 201 | |
| 202 | switch (mode) { |
| 203 | case REGULATOR_MODE_FAST: |
| 204 | val = BUCK_MODE_SYNC; |
| 205 | break; |
| 206 | case REGULATOR_MODE_NORMAL: |
| 207 | val = BUCK_MODE_AUTO; |
| 208 | break; |
| 209 | case REGULATOR_MODE_STANDBY: |
| 210 | val = BUCK_MODE_SLEEP; |
| 211 | break; |
| 212 | default: |
| 213 | return -EINVAL; |
| 214 | } |
| 215 | |
| 216 | return regmap_field_write(regl->mode, val); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Bucks use single mode register field for normal operation |
| 221 | * and suspend state. |
| 222 | * There are 3 modes to map to: FAST, NORMAL, and STANDBY. |
| 223 | */ |
| 224 | |
| 225 | static unsigned da9063_buck_get_mode(struct regulator_dev *rdev) |
| 226 | { |
| 227 | struct da9063_regulator *regl = rdev_get_drvdata(rdev); |
| 228 | struct regmap_field *field; |
| 229 | unsigned int val, mode = 0; |
| 230 | int ret; |
| 231 | |
| 232 | ret = regmap_field_read(regl->mode, &val); |
| 233 | if (ret < 0) |
| 234 | return ret; |
| 235 | |
| 236 | switch (val) { |
| 237 | default: |
| 238 | case BUCK_MODE_MANUAL: |
| 239 | mode = REGULATOR_MODE_FAST | REGULATOR_MODE_STANDBY; |
| 240 | /* Sleep flag bit decides the mode */ |
| 241 | break; |
| 242 | case BUCK_MODE_SLEEP: |
| 243 | return REGULATOR_MODE_STANDBY; |
| 244 | case BUCK_MODE_SYNC: |
| 245 | return REGULATOR_MODE_FAST; |
| 246 | case BUCK_MODE_AUTO: |
| 247 | return REGULATOR_MODE_NORMAL; |
| 248 | } |
| 249 | |
| 250 | /* Detect current regulator state */ |
| 251 | ret = regmap_field_read(regl->suspend, &val); |
| 252 | if (ret < 0) |
| 253 | return 0; |
| 254 | |
| 255 | /* Read regulator mode from proper register, depending on state */ |
| 256 | if (val) |
| 257 | field = regl->suspend_sleep; |
| 258 | else |
| 259 | field = regl->sleep; |
| 260 | |
| 261 | ret = regmap_field_read(field, &val); |
| 262 | if (ret < 0) |
| 263 | return 0; |
| 264 | |
| 265 | if (val) |
| 266 | mode &= REGULATOR_MODE_STANDBY; |
| 267 | else |
| 268 | mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST; |
| 269 | |
| 270 | return mode; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * LDOs use sleep flags - one for normal and one for suspend state. |
| 275 | * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state. |
| 276 | */ |
| 277 | |
| 278 | static int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned mode) |
| 279 | { |
| 280 | struct da9063_regulator *regl = rdev_get_drvdata(rdev); |
| 281 | unsigned val; |
| 282 | |
| 283 | switch (mode) { |
| 284 | case REGULATOR_MODE_NORMAL: |
| 285 | val = 0; |
| 286 | break; |
| 287 | case REGULATOR_MODE_STANDBY: |
| 288 | val = 1; |
| 289 | break; |
| 290 | default: |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | |
| 294 | return regmap_field_write(regl->sleep, val); |
| 295 | } |
| 296 | |
| 297 | static unsigned da9063_ldo_get_mode(struct regulator_dev *rdev) |
| 298 | { |
| 299 | struct da9063_regulator *regl = rdev_get_drvdata(rdev); |
| 300 | struct regmap_field *field; |
| 301 | int ret, val; |
| 302 | |
| 303 | /* Detect current regulator state */ |
| 304 | ret = regmap_field_read(regl->suspend, &val); |
| 305 | if (ret < 0) |
| 306 | return 0; |
| 307 | |
| 308 | /* Read regulator mode from proper register, depending on state */ |
| 309 | if (val) |
| 310 | field = regl->suspend_sleep; |
| 311 | else |
| 312 | field = regl->sleep; |
| 313 | |
| 314 | ret = regmap_field_read(field, &val); |
| 315 | if (ret < 0) |
| 316 | return 0; |
| 317 | |
| 318 | if (val) |
| 319 | return REGULATOR_MODE_STANDBY; |
| 320 | else |
| 321 | return REGULATOR_MODE_NORMAL; |
| 322 | } |
| 323 | |
| 324 | static int da9063_buck_get_status(struct regulator_dev *rdev) |
| 325 | { |
| 326 | int ret = regulator_is_enabled_regmap(rdev); |
| 327 | |
| 328 | if (ret == 0) { |
| 329 | ret = REGULATOR_STATUS_OFF; |
| 330 | } else if (ret > 0) { |
| 331 | ret = da9063_buck_get_mode(rdev); |
| 332 | if (ret > 0) |
| 333 | ret = regulator_mode_to_status(ret); |
| 334 | else if (ret == 0) |
| 335 | ret = -EIO; |
| 336 | } |
| 337 | |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | static int da9063_ldo_get_status(struct regulator_dev *rdev) |
| 342 | { |
| 343 | int ret = regulator_is_enabled_regmap(rdev); |
| 344 | |
| 345 | if (ret == 0) { |
| 346 | ret = REGULATOR_STATUS_OFF; |
| 347 | } else if (ret > 0) { |
| 348 | ret = da9063_ldo_get_mode(rdev); |
| 349 | if (ret > 0) |
| 350 | ret = regulator_mode_to_status(ret); |
| 351 | else if (ret == 0) |
| 352 | ret = -EIO; |
| 353 | } |
| 354 | |
| 355 | return ret; |
| 356 | } |
| 357 | |
| 358 | static int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV) |
| 359 | { |
| 360 | struct da9063_regulator *regl = rdev_get_drvdata(rdev); |
| 361 | const struct da9063_regulator_info *rinfo = regl->info; |
| 362 | int ret, sel; |
| 363 | |
| 364 | sel = regulator_map_voltage_linear(rdev, uV, uV); |
| 365 | if (sel < 0) |
| 366 | return -EINVAL; |
| 367 | |
| 368 | sel <<= ffs(rdev->desc->vsel_mask) - 1; |
| 369 | |
| 370 | ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg, |
| 371 | rdev->desc->vsel_mask, sel); |
| 372 | |
| 373 | return ret; |
| 374 | } |
| 375 | |
| 376 | static int da9063_suspend_enable(struct regulator_dev *rdev) |
| 377 | { |
| 378 | struct da9063_regulator *regl = rdev_get_drvdata(rdev); |
| 379 | |
| 380 | return regmap_field_write(regl->suspend, 1); |
| 381 | } |
| 382 | |
| 383 | static int da9063_suspend_disable(struct regulator_dev *rdev) |
| 384 | { |
| 385 | struct da9063_regulator *regl = rdev_get_drvdata(rdev); |
| 386 | |
| 387 | return regmap_field_write(regl->suspend, 0); |
| 388 | } |
| 389 | |
| 390 | static int da9063_buck_set_suspend_mode(struct regulator_dev *rdev, unsigned mode) |
| 391 | { |
| 392 | struct da9063_regulator *regl = rdev_get_drvdata(rdev); |
| 393 | int val; |
| 394 | |
| 395 | switch (mode) { |
| 396 | case REGULATOR_MODE_FAST: |
| 397 | val = BUCK_MODE_SYNC; |
| 398 | break; |
| 399 | case REGULATOR_MODE_NORMAL: |
| 400 | val = BUCK_MODE_AUTO; |
| 401 | break; |
| 402 | case REGULATOR_MODE_STANDBY: |
| 403 | val = BUCK_MODE_SLEEP; |
| 404 | break; |
| 405 | default: |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | |
| 409 | return regmap_field_write(regl->mode, val); |
| 410 | } |
| 411 | |
| 412 | static int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev, unsigned mode) |
| 413 | { |
| 414 | struct da9063_regulator *regl = rdev_get_drvdata(rdev); |
| 415 | unsigned val; |
| 416 | |
| 417 | switch (mode) { |
| 418 | case REGULATOR_MODE_NORMAL: |
| 419 | val = 0; |
| 420 | break; |
| 421 | case REGULATOR_MODE_STANDBY: |
| 422 | val = 1; |
| 423 | break; |
| 424 | default: |
| 425 | return -EINVAL; |
| 426 | } |
| 427 | |
| 428 | return regmap_field_write(regl->suspend_sleep, val); |
| 429 | } |
| 430 | |
| 431 | static struct regulator_ops da9063_buck_ops = { |
| 432 | .enable = regulator_enable_regmap, |
| 433 | .disable = regulator_disable_regmap, |
| 434 | .is_enabled = regulator_is_enabled_regmap, |
| 435 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 436 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 437 | .list_voltage = regulator_list_voltage_linear, |
| 438 | .set_current_limit = da9063_set_current_limit, |
| 439 | .get_current_limit = da9063_get_current_limit, |
| 440 | .set_mode = da9063_buck_set_mode, |
| 441 | .get_mode = da9063_buck_get_mode, |
| 442 | .get_status = da9063_buck_get_status, |
| 443 | .set_suspend_voltage = da9063_set_suspend_voltage, |
| 444 | .set_suspend_enable = da9063_suspend_enable, |
| 445 | .set_suspend_disable = da9063_suspend_disable, |
| 446 | .set_suspend_mode = da9063_buck_set_suspend_mode, |
| 447 | }; |
| 448 | |
| 449 | static struct regulator_ops da9063_ldo_ops = { |
| 450 | .enable = regulator_enable_regmap, |
| 451 | .disable = regulator_disable_regmap, |
| 452 | .is_enabled = regulator_is_enabled_regmap, |
| 453 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 454 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 455 | .list_voltage = regulator_list_voltage_linear, |
| 456 | .set_mode = da9063_ldo_set_mode, |
| 457 | .get_mode = da9063_ldo_get_mode, |
| 458 | .get_status = da9063_ldo_get_status, |
| 459 | .set_suspend_voltage = da9063_set_suspend_voltage, |
| 460 | .set_suspend_enable = da9063_suspend_enable, |
| 461 | .set_suspend_disable = da9063_suspend_disable, |
| 462 | .set_suspend_mode = da9063_ldo_set_suspend_mode, |
| 463 | }; |
| 464 | |
| 465 | /* Info of regulators for DA9063 */ |
| 466 | static const struct da9063_regulator_info da9063_regulator_info[] = { |
| 467 | { |
| 468 | DA9063_BUCK(DA9063, BCORE1, 300, 10, 1570, |
| 469 | da9063_buck_a_limits), |
| 470 | DA9063_BUCK_COMMON_FIELDS(BCORE1), |
| 471 | .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL), |
| 472 | .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C, |
| 473 | DA9063_BCORE1_ILIM_MASK), |
| 474 | }, |
| 475 | { |
| 476 | DA9063_BUCK(DA9063, BCORE2, 300, 10, 1570, |
| 477 | da9063_buck_a_limits), |
| 478 | DA9063_BUCK_COMMON_FIELDS(BCORE2), |
| 479 | .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE2_SEL), |
| 480 | .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C, |
| 481 | DA9063_BCORE2_ILIM_MASK), |
| 482 | }, |
| 483 | { |
| 484 | DA9063_BUCK(DA9063, BPRO, 530, 10, 1800, |
| 485 | da9063_buck_a_limits), |
| 486 | DA9063_BUCK_COMMON_FIELDS(BPRO), |
| 487 | .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPRO_SEL), |
| 488 | .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_B, |
| 489 | DA9063_BPRO_ILIM_MASK), |
| 490 | }, |
| 491 | { |
| 492 | DA9063_BUCK(DA9063, BMEM, 800, 20, 3340, |
| 493 | da9063_buck_b_limits), |
| 494 | DA9063_BUCK_COMMON_FIELDS(BMEM), |
| 495 | .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL), |
| 496 | .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A, |
| 497 | DA9063_BMEM_ILIM_MASK), |
| 498 | }, |
| 499 | { |
| 500 | DA9063_BUCK(DA9063, BIO, 800, 20, 3340, |
| 501 | da9063_buck_b_limits), |
| 502 | DA9063_BUCK_COMMON_FIELDS(BIO), |
| 503 | .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VBIO_SEL), |
| 504 | .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A, |
| 505 | DA9063_BIO_ILIM_MASK), |
| 506 | }, |
| 507 | { |
| 508 | DA9063_BUCK(DA9063, BPERI, 800, 20, 3340, |
| 509 | da9063_buck_b_limits), |
| 510 | DA9063_BUCK_COMMON_FIELDS(BPERI), |
| 511 | .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPERI_SEL), |
| 512 | .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_B, |
| 513 | DA9063_BPERI_ILIM_MASK), |
| 514 | }, |
| 515 | { |
| 516 | DA9063_BUCK(DA9063, BCORES_MERGED, 300, 10, 1570, |
| 517 | da9063_bcores_merged_limits), |
| 518 | /* BCORES_MERGED uses the same register fields as BCORE1 */ |
| 519 | DA9063_BUCK_COMMON_FIELDS(BCORE1), |
| 520 | .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL), |
| 521 | .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C, |
| 522 | DA9063_BCORE1_ILIM_MASK), |
| 523 | }, |
| 524 | { |
| 525 | DA9063_BUCK(DA9063, BMEM_BIO_MERGED, 800, 20, 3340, |
| 526 | da9063_bmem_bio_merged_limits), |
| 527 | /* BMEM_BIO_MERGED uses the same register fields as BMEM */ |
| 528 | DA9063_BUCK_COMMON_FIELDS(BMEM), |
| 529 | .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL), |
| 530 | .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A, |
| 531 | DA9063_BMEM_ILIM_MASK), |
| 532 | }, |
| 533 | { |
| 534 | DA9063_LDO(DA9063, LDO1, 600, 20, 1860), |
| 535 | .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO1_SEL), |
| 536 | }, |
| 537 | { |
| 538 | DA9063_LDO(DA9063, LDO2, 600, 20, 1860), |
| 539 | .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO2_SEL), |
| 540 | }, |
| 541 | { |
| 542 | DA9063_LDO(DA9063, LDO3, 900, 20, 3440), |
| 543 | .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO3_SEL), |
| 544 | .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO3_LIM), |
| 545 | }, |
| 546 | { |
| 547 | DA9063_LDO(DA9063, LDO4, 900, 20, 3440), |
| 548 | .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VLDO4_SEL), |
| 549 | .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO4_LIM), |
| 550 | }, |
| 551 | { |
| 552 | DA9063_LDO(DA9063, LDO5, 900, 50, 3600), |
| 553 | .suspend = BFIELD(DA9063_REG_LDO5_CONT, DA9063_VLDO5_SEL), |
| 554 | }, |
| 555 | { |
| 556 | DA9063_LDO(DA9063, LDO6, 900, 50, 3600), |
| 557 | .suspend = BFIELD(DA9063_REG_LDO6_CONT, DA9063_VLDO6_SEL), |
| 558 | }, |
| 559 | { |
| 560 | DA9063_LDO(DA9063, LDO7, 900, 50, 3600), |
| 561 | .suspend = BFIELD(DA9063_REG_LDO7_CONT, DA9063_VLDO7_SEL), |
| 562 | .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO7_LIM), |
| 563 | }, |
| 564 | { |
| 565 | DA9063_LDO(DA9063, LDO8, 900, 50, 3600), |
| 566 | .suspend = BFIELD(DA9063_REG_LDO8_CONT, DA9063_VLDO8_SEL), |
| 567 | .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO8_LIM), |
| 568 | }, |
| 569 | { |
| 570 | DA9063_LDO(DA9063, LDO9, 950, 50, 3600), |
| 571 | .suspend = BFIELD(DA9063_REG_LDO9_CONT, DA9063_VLDO9_SEL), |
| 572 | }, |
| 573 | { |
| 574 | DA9063_LDO(DA9063, LDO10, 900, 50, 3600), |
| 575 | .suspend = BFIELD(DA9063_REG_LDO10_CONT, DA9063_VLDO10_SEL), |
| 576 | }, |
| 577 | { |
| 578 | DA9063_LDO(DA9063, LDO11, 900, 50, 3600), |
| 579 | .suspend = BFIELD(DA9063_REG_LDO11_CONT, DA9063_VLDO11_SEL), |
| 580 | .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO11_LIM), |
| 581 | }, |
| 582 | }; |
| 583 | |
| 584 | /* Link chip model with regulators info table */ |
| 585 | static struct da9063_dev_model regulators_models[] = { |
| 586 | { |
| 587 | .regulator_info = da9063_regulator_info, |
| 588 | .n_regulators = ARRAY_SIZE(da9063_regulator_info), |
| 589 | .dev_model = PMIC_DA9063, |
| 590 | }, |
| 591 | { } |
| 592 | }; |
| 593 | |
| 594 | /* Regulator interrupt handlers */ |
Axel Lin | 632b3d6 | 2013-09-01 12:19:51 +0800 | [diff] [blame] | 595 | static irqreturn_t da9063_ldo_lim_event(int irq, void *data) |
Krystian Garbaciak | 69ca3e5 | 2013-07-29 19:00:45 +0200 | [diff] [blame] | 596 | { |
| 597 | struct da9063_regulators *regulators = data; |
| 598 | struct da9063 *hw = regulators->regulator[0].hw; |
| 599 | struct da9063_regulator *regl; |
| 600 | int bits, i , ret; |
| 601 | |
| 602 | ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits); |
| 603 | if (ret < 0) |
| 604 | return IRQ_NONE; |
| 605 | |
| 606 | for (i = regulators->n_regulators - 1; i >= 0; i--) { |
| 607 | regl = ®ulators->regulator[i]; |
| 608 | if (regl->info->oc_event.reg != DA9063_REG_STATUS_D) |
| 609 | continue; |
| 610 | |
| 611 | if (BIT(regl->info->oc_event.lsb) & bits) |
| 612 | regulator_notifier_call_chain(regl->rdev, |
| 613 | REGULATOR_EVENT_OVER_CURRENT, NULL); |
| 614 | } |
| 615 | |
| 616 | return IRQ_HANDLED; |
| 617 | } |
| 618 | |
| 619 | /* |
| 620 | * Probing and Initialisation functions |
| 621 | */ |
| 622 | static const struct regulator_init_data *da9063_get_regulator_initdata( |
| 623 | const struct da9063_regulators_pdata *regl_pdata, int id) |
| 624 | { |
| 625 | int i; |
| 626 | |
| 627 | for (i = 0; i < regl_pdata->n_regulators; i++) { |
| 628 | if (id == regl_pdata->regulator_data[i].id) |
| 629 | return regl_pdata->regulator_data[i].initdata; |
| 630 | } |
| 631 | |
| 632 | return NULL; |
| 633 | } |
| 634 | |
| 635 | #ifdef CONFIG_OF |
| 636 | static struct of_regulator_match da9063_matches[] = { |
| 637 | [DA9063_ID_BCORE1] = { .name = "bcore1" }, |
| 638 | [DA9063_ID_BCORE2] = { .name = "bcore2" }, |
| 639 | [DA9063_ID_BPRO] = { .name = "bpro", }, |
| 640 | [DA9063_ID_BMEM] = { .name = "bmem", }, |
| 641 | [DA9063_ID_BIO] = { .name = "bio", }, |
| 642 | [DA9063_ID_BPERI] = { .name = "bperi", }, |
| 643 | [DA9063_ID_BCORES_MERGED] = { .name = "bcores-merged" }, |
| 644 | [DA9063_ID_BMEM_BIO_MERGED] = { .name = "bmem-bio-merged", }, |
| 645 | [DA9063_ID_LDO1] = { .name = "ldo1", }, |
| 646 | [DA9063_ID_LDO2] = { .name = "ldo2", }, |
| 647 | [DA9063_ID_LDO3] = { .name = "ldo3", }, |
| 648 | [DA9063_ID_LDO4] = { .name = "ldo4", }, |
| 649 | [DA9063_ID_LDO5] = { .name = "ldo5", }, |
| 650 | [DA9063_ID_LDO6] = { .name = "ldo6", }, |
| 651 | [DA9063_ID_LDO7] = { .name = "ldo7", }, |
| 652 | [DA9063_ID_LDO8] = { .name = "ldo8", }, |
| 653 | [DA9063_ID_LDO9] = { .name = "ldo9", }, |
| 654 | [DA9063_ID_LDO10] = { .name = "ldo10", }, |
| 655 | [DA9063_ID_LDO11] = { .name = "ldo11", }, |
| 656 | }; |
| 657 | |
| 658 | static struct da9063_regulators_pdata *da9063_parse_regulators_dt( |
| 659 | struct platform_device *pdev, |
| 660 | struct of_regulator_match **da9063_reg_matches) |
| 661 | { |
| 662 | struct da9063_regulators_pdata *pdata; |
| 663 | struct da9063_regulator_data *rdata; |
| 664 | struct device_node *node; |
| 665 | int i, n, num; |
| 666 | |
| 667 | node = of_find_node_by_name(pdev->dev.parent->of_node, "regulators"); |
| 668 | if (!node) { |
| 669 | dev_err(&pdev->dev, "Regulators device node not found\n"); |
| 670 | return ERR_PTR(-ENODEV); |
| 671 | } |
| 672 | |
| 673 | num = of_regulator_match(&pdev->dev, node, da9063_matches, |
| 674 | ARRAY_SIZE(da9063_matches)); |
| 675 | if (num < 0) { |
| 676 | dev_err(&pdev->dev, "Failed to match regulators\n"); |
| 677 | return ERR_PTR(-EINVAL); |
| 678 | } |
| 679 | |
| 680 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 681 | if (!pdata) |
| 682 | return ERR_PTR(-ENOMEM); |
| 683 | |
| 684 | pdata->regulator_data = devm_kzalloc(&pdev->dev, |
| 685 | num * sizeof(*pdata->regulator_data), |
| 686 | GFP_KERNEL); |
| 687 | if (!pdata->regulator_data) |
| 688 | return ERR_PTR(-ENOMEM); |
| 689 | pdata->n_regulators = num; |
| 690 | |
| 691 | n = 0; |
| 692 | for (i = 0; i < ARRAY_SIZE(da9063_matches); i++) { |
| 693 | if (!da9063_matches[i].init_data) |
| 694 | continue; |
| 695 | |
| 696 | rdata = &pdata->regulator_data[n]; |
| 697 | rdata->id = i; |
| 698 | rdata->initdata = da9063_matches[i].init_data; |
| 699 | |
| 700 | n++; |
| 701 | }; |
| 702 | |
| 703 | *da9063_reg_matches = da9063_matches; |
| 704 | return pdata; |
| 705 | } |
| 706 | #else |
| 707 | static struct da9063_regulators_pdata *da9063_parse_regulators_dt( |
| 708 | struct platform_device *pdev, |
| 709 | struct of_regulator_match **da9063_reg_matches) |
| 710 | { |
| 711 | da9063_reg_matches = NULL; |
Geert Uytterhoeven | e8cb204 | 2013-09-09 13:49:26 +0200 | [diff] [blame] | 712 | return ERR_PTR(-ENODEV); |
Krystian Garbaciak | 69ca3e5 | 2013-07-29 19:00:45 +0200 | [diff] [blame] | 713 | } |
| 714 | #endif |
| 715 | |
| 716 | static int da9063_regulator_probe(struct platform_device *pdev) |
| 717 | { |
| 718 | struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent); |
| 719 | struct da9063_pdata *da9063_pdata = dev_get_platdata(da9063->dev); |
| 720 | struct of_regulator_match *da9063_reg_matches; |
| 721 | struct da9063_regulators_pdata *regl_pdata; |
| 722 | const struct da9063_dev_model *model; |
| 723 | struct da9063_regulators *regulators; |
| 724 | struct da9063_regulator *regl; |
| 725 | struct regulator_config config; |
| 726 | bool bcores_merged, bmem_bio_merged; |
| 727 | int id, irq, n, n_regulators, ret, val; |
| 728 | size_t size; |
| 729 | |
| 730 | regl_pdata = da9063_pdata ? da9063_pdata->regulators_pdata : NULL; |
| 731 | |
| 732 | if (!regl_pdata) |
| 733 | regl_pdata = da9063_parse_regulators_dt(pdev, |
| 734 | &da9063_reg_matches); |
| 735 | |
| 736 | if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) { |
| 737 | dev_err(&pdev->dev, |
| 738 | "No regulators defined for the platform\n"); |
| 739 | return PTR_ERR(regl_pdata); |
| 740 | } |
| 741 | |
| 742 | /* Find regulators set for particular device model */ |
| 743 | for (model = regulators_models; model->regulator_info; model++) { |
| 744 | if (model->dev_model == da9063->model) |
| 745 | break; |
| 746 | } |
| 747 | if (!model->regulator_info) { |
| 748 | dev_err(&pdev->dev, "Chip model not recognised (%u)\n", |
| 749 | da9063->model); |
| 750 | return -ENODEV; |
| 751 | } |
| 752 | |
| 753 | ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val); |
| 754 | if (ret < 0) { |
| 755 | dev_err(&pdev->dev, |
| 756 | "Error while reading BUCKs configuration\n"); |
| 757 | return -EIO; |
| 758 | } |
| 759 | bcores_merged = val & DA9063_BCORE_MERGE; |
| 760 | bmem_bio_merged = val & DA9063_BUCK_MERGE; |
| 761 | |
| 762 | n_regulators = model->n_regulators; |
| 763 | if (bcores_merged) |
| 764 | n_regulators -= 2; /* remove BCORE1, BCORE2 */ |
| 765 | else |
| 766 | n_regulators--; /* remove BCORES_MERGED */ |
| 767 | if (bmem_bio_merged) |
| 768 | n_regulators -= 2; /* remove BMEM, BIO */ |
| 769 | else |
| 770 | n_regulators--; /* remove BMEM_BIO_MERGED */ |
| 771 | |
| 772 | /* Allocate memory required by usable regulators */ |
| 773 | size = sizeof(struct da9063_regulators) + |
| 774 | n_regulators * sizeof(struct da9063_regulator); |
| 775 | regulators = devm_kzalloc(&pdev->dev, size, GFP_KERNEL); |
| 776 | if (!regulators) { |
| 777 | dev_err(&pdev->dev, "No memory for regulators\n"); |
| 778 | return -ENOMEM; |
| 779 | } |
| 780 | |
| 781 | regulators->n_regulators = n_regulators; |
| 782 | platform_set_drvdata(pdev, regulators); |
| 783 | |
| 784 | /* Register all regulators declared in platform information */ |
| 785 | n = 0; |
| 786 | id = 0; |
| 787 | while (n < regulators->n_regulators) { |
| 788 | /* Skip regulator IDs depending on merge mode configuration */ |
| 789 | switch (id) { |
| 790 | case DA9063_ID_BCORE1: |
| 791 | case DA9063_ID_BCORE2: |
| 792 | if (bcores_merged) { |
| 793 | id++; |
| 794 | continue; |
| 795 | } |
| 796 | break; |
| 797 | case DA9063_ID_BMEM: |
| 798 | case DA9063_ID_BIO: |
| 799 | if (bmem_bio_merged) { |
| 800 | id++; |
| 801 | continue; |
| 802 | } |
| 803 | break; |
| 804 | case DA9063_ID_BCORES_MERGED: |
| 805 | if (!bcores_merged) { |
| 806 | id++; |
| 807 | continue; |
| 808 | } |
| 809 | break; |
| 810 | case DA9063_ID_BMEM_BIO_MERGED: |
| 811 | if (!bmem_bio_merged) { |
| 812 | id++; |
| 813 | continue; |
| 814 | } |
| 815 | break; |
| 816 | } |
| 817 | |
| 818 | /* Initialise regulator structure */ |
| 819 | regl = ®ulators->regulator[n]; |
| 820 | regl->hw = da9063; |
| 821 | regl->info = &model->regulator_info[id]; |
| 822 | regl->desc = regl->info->desc; |
| 823 | regl->desc.type = REGULATOR_VOLTAGE; |
| 824 | regl->desc.owner = THIS_MODULE; |
| 825 | |
| 826 | if (regl->info->mode.reg) |
| 827 | regl->mode = devm_regmap_field_alloc(&pdev->dev, |
| 828 | da9063->regmap, regl->info->mode); |
| 829 | if (regl->info->suspend.reg) |
| 830 | regl->suspend = devm_regmap_field_alloc(&pdev->dev, |
| 831 | da9063->regmap, regl->info->suspend); |
| 832 | if (regl->info->sleep.reg) |
| 833 | regl->sleep = devm_regmap_field_alloc(&pdev->dev, |
| 834 | da9063->regmap, regl->info->sleep); |
| 835 | if (regl->info->suspend_sleep.reg) |
| 836 | regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev, |
| 837 | da9063->regmap, regl->info->suspend_sleep); |
| 838 | if (regl->info->ilimit.reg) |
| 839 | regl->ilimit = devm_regmap_field_alloc(&pdev->dev, |
| 840 | da9063->regmap, regl->info->ilimit); |
| 841 | |
| 842 | /* Register regulator */ |
| 843 | memset(&config, 0, sizeof(config)); |
| 844 | config.dev = &pdev->dev; |
| 845 | config.init_data = da9063_get_regulator_initdata(regl_pdata, id); |
| 846 | config.driver_data = regl; |
| 847 | if (da9063_reg_matches) |
| 848 | config.of_node = da9063_reg_matches[id].of_node; |
| 849 | config.regmap = da9063->regmap; |
| 850 | regl->rdev = regulator_register(®l->desc, &config); |
Axel Lin | e515800 | 2013-08-30 20:08:42 +0800 | [diff] [blame] | 851 | if (IS_ERR(regl->rdev)) { |
Krystian Garbaciak | 69ca3e5 | 2013-07-29 19:00:45 +0200 | [diff] [blame] | 852 | dev_err(&pdev->dev, |
| 853 | "Failed to register %s regulator\n", |
| 854 | regl->desc.name); |
| 855 | ret = PTR_ERR(regl->rdev); |
| 856 | goto err; |
| 857 | } |
| 858 | id++; |
| 859 | n++; |
| 860 | } |
| 861 | |
| 862 | /* LDOs overcurrent event support */ |
| 863 | irq = platform_get_irq_byname(pdev, "LDO_LIM"); |
| 864 | if (irq < 0) { |
| 865 | ret = irq; |
| 866 | dev_err(&pdev->dev, "Failed to get IRQ.\n"); |
| 867 | goto err; |
| 868 | } |
| 869 | |
| 870 | regulators->irq_ldo_lim = regmap_irq_get_virq(da9063->regmap_irq, irq); |
| 871 | if (regulators->irq_ldo_lim >= 0) { |
| 872 | ret = request_threaded_irq(regulators->irq_ldo_lim, |
| 873 | NULL, da9063_ldo_lim_event, |
| 874 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 875 | "LDO_LIM", regulators); |
| 876 | if (ret) { |
| 877 | dev_err(&pdev->dev, |
| 878 | "Failed to request LDO_LIM IRQ.\n"); |
| 879 | regulators->irq_ldo_lim = -ENXIO; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | return 0; |
| 884 | |
| 885 | err: |
| 886 | /* Wind back regulators registeration */ |
| 887 | while (--n >= 0) |
| 888 | regulator_unregister(regulators->regulator[n].rdev); |
| 889 | |
| 890 | return ret; |
| 891 | } |
| 892 | |
| 893 | static int da9063_regulator_remove(struct platform_device *pdev) |
| 894 | { |
| 895 | struct da9063_regulators *regulators = platform_get_drvdata(pdev); |
| 896 | struct da9063_regulator *regl; |
| 897 | |
| 898 | free_irq(regulators->irq_ldo_lim, regulators); |
| 899 | free_irq(regulators->irq_uvov, regulators); |
| 900 | |
| 901 | for (regl = ®ulators->regulator[regulators->n_regulators - 1]; |
| 902 | regl >= ®ulators->regulator[0]; regl--) |
| 903 | regulator_unregister(regl->rdev); |
| 904 | |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | static struct platform_driver da9063_regulator_driver = { |
| 909 | .driver = { |
| 910 | .name = DA9063_DRVNAME_REGULATORS, |
| 911 | .owner = THIS_MODULE, |
| 912 | }, |
| 913 | .probe = da9063_regulator_probe, |
| 914 | .remove = da9063_regulator_remove, |
| 915 | }; |
| 916 | |
| 917 | static int __init da9063_regulator_init(void) |
| 918 | { |
| 919 | return platform_driver_register(&da9063_regulator_driver); |
| 920 | } |
| 921 | subsys_initcall(da9063_regulator_init); |
| 922 | |
| 923 | static void __exit da9063_regulator_cleanup(void) |
| 924 | { |
| 925 | platform_driver_unregister(&da9063_regulator_driver); |
| 926 | } |
| 927 | module_exit(da9063_regulator_cleanup); |
| 928 | |
| 929 | |
| 930 | /* Module information */ |
| 931 | MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>"); |
| 932 | MODULE_DESCRIPTION("DA9063 regulators driver"); |
| 933 | MODULE_LICENSE("GPL"); |
| 934 | MODULE_ALIAS("paltform:" DA9063_DRVNAME_REGULATORS); |