Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Regulator driver for DA9055 PMIC |
| 3 | * |
| 4 | * Copyright(c) 2012 Dialog Semiconductor Ltd. |
| 5 | * |
| 6 | * Author: David Dajun Chen <dchen@diasemi.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/gpio.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/regulator/driver.h> |
| 21 | #include <linux/regulator/machine.h> |
| 22 | |
| 23 | #include <linux/mfd/da9055/core.h> |
| 24 | #include <linux/mfd/da9055/reg.h> |
| 25 | #include <linux/mfd/da9055/pdata.h> |
| 26 | |
| 27 | #define DA9055_MIN_UA 0 |
| 28 | #define DA9055_MAX_UA 3 |
| 29 | |
| 30 | #define DA9055_LDO_MODE_SYNC 0 |
| 31 | #define DA9055_LDO_MODE_SLEEP 1 |
| 32 | |
| 33 | #define DA9055_BUCK_MODE_SLEEP 1 |
| 34 | #define DA9055_BUCK_MODE_SYNC 2 |
| 35 | #define DA9055_BUCK_MODE_AUTO 3 |
| 36 | |
| 37 | /* DA9055 REGULATOR IDs */ |
| 38 | #define DA9055_ID_BUCK1 0 |
| 39 | #define DA9055_ID_BUCK2 1 |
| 40 | #define DA9055_ID_LDO1 2 |
| 41 | #define DA9055_ID_LDO2 3 |
| 42 | #define DA9055_ID_LDO3 4 |
| 43 | #define DA9055_ID_LDO4 5 |
| 44 | #define DA9055_ID_LDO5 6 |
| 45 | #define DA9055_ID_LDO6 7 |
| 46 | |
| 47 | /* DA9055 BUCK current limit */ |
| 48 | static const int da9055_current_limits[] = { 500000, 600000, 700000, 800000 }; |
| 49 | |
| 50 | struct da9055_conf_reg { |
| 51 | int reg; |
| 52 | int sel_mask; |
| 53 | int en_mask; |
| 54 | }; |
| 55 | |
| 56 | struct da9055_volt_reg { |
| 57 | int reg_a; |
| 58 | int reg_b; |
| 59 | int sl_shift; |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 60 | int v_mask; |
| 61 | int v_shift; |
| 62 | }; |
| 63 | |
| 64 | struct da9055_mode_reg { |
| 65 | int reg; |
| 66 | int mask; |
| 67 | int shift; |
| 68 | }; |
| 69 | |
| 70 | struct da9055_regulator_info { |
| 71 | struct regulator_desc reg_desc; |
| 72 | struct da9055_conf_reg conf; |
| 73 | struct da9055_volt_reg volt; |
| 74 | struct da9055_mode_reg mode; |
| 75 | }; |
| 76 | |
| 77 | struct da9055_regulator { |
| 78 | struct da9055 *da9055; |
| 79 | struct da9055_regulator_info *info; |
| 80 | struct regulator_dev *rdev; |
| 81 | enum gpio_select reg_rselect; |
| 82 | }; |
| 83 | |
| 84 | static unsigned int da9055_buck_get_mode(struct regulator_dev *rdev) |
| 85 | { |
| 86 | struct da9055_regulator *regulator = rdev_get_drvdata(rdev); |
| 87 | struct da9055_regulator_info *info = regulator->info; |
| 88 | int ret, mode = 0; |
| 89 | |
| 90 | ret = da9055_reg_read(regulator->da9055, info->mode.reg); |
| 91 | if (ret < 0) |
| 92 | return ret; |
| 93 | |
| 94 | switch ((ret & info->mode.mask) >> info->mode.shift) { |
| 95 | case DA9055_BUCK_MODE_SYNC: |
| 96 | mode = REGULATOR_MODE_FAST; |
| 97 | break; |
| 98 | case DA9055_BUCK_MODE_AUTO: |
| 99 | mode = REGULATOR_MODE_NORMAL; |
| 100 | break; |
| 101 | case DA9055_BUCK_MODE_SLEEP: |
| 102 | mode = REGULATOR_MODE_STANDBY; |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | return mode; |
| 107 | } |
| 108 | |
| 109 | static int da9055_buck_set_mode(struct regulator_dev *rdev, |
| 110 | unsigned int mode) |
| 111 | { |
| 112 | struct da9055_regulator *regulator = rdev_get_drvdata(rdev); |
| 113 | struct da9055_regulator_info *info = regulator->info; |
| 114 | int val = 0; |
| 115 | |
| 116 | switch (mode) { |
| 117 | case REGULATOR_MODE_FAST: |
| 118 | val = DA9055_BUCK_MODE_SYNC << info->mode.shift; |
| 119 | break; |
| 120 | case REGULATOR_MODE_NORMAL: |
| 121 | val = DA9055_BUCK_MODE_AUTO << info->mode.shift; |
| 122 | break; |
| 123 | case REGULATOR_MODE_STANDBY: |
| 124 | val = DA9055_BUCK_MODE_SLEEP << info->mode.shift; |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | return da9055_reg_update(regulator->da9055, info->mode.reg, |
| 129 | info->mode.mask, val); |
| 130 | } |
| 131 | |
| 132 | static unsigned int da9055_ldo_get_mode(struct regulator_dev *rdev) |
| 133 | { |
| 134 | struct da9055_regulator *regulator = rdev_get_drvdata(rdev); |
| 135 | struct da9055_regulator_info *info = regulator->info; |
| 136 | int ret; |
| 137 | |
| 138 | ret = da9055_reg_read(regulator->da9055, info->volt.reg_b); |
| 139 | if (ret < 0) |
| 140 | return ret; |
| 141 | |
| 142 | if (ret >> info->volt.sl_shift) |
| 143 | return REGULATOR_MODE_STANDBY; |
| 144 | else |
| 145 | return REGULATOR_MODE_NORMAL; |
| 146 | } |
| 147 | |
| 148 | static int da9055_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode) |
| 149 | { |
| 150 | struct da9055_regulator *regulator = rdev_get_drvdata(rdev); |
| 151 | struct da9055_regulator_info *info = regulator->info; |
| 152 | struct da9055_volt_reg volt = info->volt; |
| 153 | int val = 0; |
| 154 | |
| 155 | switch (mode) { |
| 156 | case REGULATOR_MODE_NORMAL: |
| 157 | case REGULATOR_MODE_FAST: |
| 158 | val = DA9055_LDO_MODE_SYNC; |
| 159 | break; |
| 160 | case REGULATOR_MODE_STANDBY: |
| 161 | val = DA9055_LDO_MODE_SLEEP; |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | return da9055_reg_update(regulator->da9055, volt.reg_b, |
| 166 | 1 << volt.sl_shift, |
| 167 | val << volt.sl_shift); |
| 168 | } |
| 169 | |
| 170 | static int da9055_buck_get_current_limit(struct regulator_dev *rdev) |
| 171 | { |
| 172 | struct da9055_regulator *regulator = rdev_get_drvdata(rdev); |
| 173 | struct da9055_regulator_info *info = regulator->info; |
| 174 | int ret; |
| 175 | |
| 176 | ret = da9055_reg_read(regulator->da9055, DA9055_REG_BUCK_LIM); |
| 177 | if (ret < 0) |
| 178 | return ret; |
| 179 | |
| 180 | ret &= info->mode.mask; |
| 181 | return da9055_current_limits[ret >> info->mode.shift]; |
| 182 | } |
| 183 | |
| 184 | static int da9055_buck_set_current_limit(struct regulator_dev *rdev, int min_uA, |
| 185 | int max_uA) |
| 186 | { |
| 187 | struct da9055_regulator *regulator = rdev_get_drvdata(rdev); |
| 188 | struct da9055_regulator_info *info = regulator->info; |
| 189 | int i, val = 0; |
| 190 | |
| 191 | if (min_uA > da9055_current_limits[DA9055_MAX_UA] || |
| 192 | max_uA < da9055_current_limits[DA9055_MIN_UA]) |
| 193 | return -EINVAL; |
| 194 | |
| 195 | for (i = 0; i < ARRAY_SIZE(da9055_current_limits); i++) { |
| 196 | if (min_uA <= da9055_current_limits[i]) { |
| 197 | val = i; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return da9055_reg_update(regulator->da9055, DA9055_REG_BUCK_LIM, |
| 203 | info->mode.mask, val << info->mode.shift); |
| 204 | } |
| 205 | |
| 206 | static int da9055_regulator_get_voltage_sel(struct regulator_dev *rdev) |
| 207 | { |
| 208 | struct da9055_regulator *regulator = rdev_get_drvdata(rdev); |
| 209 | struct da9055_regulator_info *info = regulator->info; |
| 210 | struct da9055_volt_reg volt = info->volt; |
| 211 | int ret, sel; |
| 212 | |
| 213 | /* |
| 214 | * There are two voltage register set A & B for voltage ramping but |
| 215 | * either one of then can be active therefore we first determine |
| 216 | * the active register set. |
| 217 | */ |
| 218 | ret = da9055_reg_read(regulator->da9055, info->conf.reg); |
| 219 | if (ret < 0) |
| 220 | return ret; |
| 221 | |
| 222 | ret &= info->conf.sel_mask; |
| 223 | |
| 224 | /* Get the voltage for the active register set A/B */ |
| 225 | if (ret == DA9055_REGUALTOR_SET_A) |
| 226 | ret = da9055_reg_read(regulator->da9055, volt.reg_a); |
| 227 | else |
| 228 | ret = da9055_reg_read(regulator->da9055, volt.reg_b); |
| 229 | |
| 230 | if (ret < 0) |
| 231 | return ret; |
| 232 | |
| 233 | sel = (ret & volt.v_mask); |
Axel Lin | 6b1f8a4 | 2012-11-27 10:26:14 +0800 | [diff] [blame^] | 234 | return sel; |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 235 | } |
| 236 | |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 237 | static int da9055_regulator_set_voltage_sel(struct regulator_dev *rdev, |
| 238 | unsigned int selector) |
| 239 | { |
| 240 | struct da9055_regulator *regulator = rdev_get_drvdata(rdev); |
| 241 | struct da9055_regulator_info *info = regulator->info; |
| 242 | int ret; |
| 243 | |
| 244 | /* |
| 245 | * Regulator register set A/B is not selected through GPIO therefore |
| 246 | * we use default register set A for voltage ramping. |
| 247 | */ |
| 248 | if (regulator->reg_rselect == NO_GPIO) { |
| 249 | /* Select register set A */ |
| 250 | ret = da9055_reg_update(regulator->da9055, info->conf.reg, |
| 251 | info->conf.sel_mask, DA9055_SEL_REG_A); |
| 252 | if (ret < 0) |
| 253 | return ret; |
| 254 | |
| 255 | /* Set the voltage */ |
Axel Lin | 369cf60 | 2012-11-20 15:36:46 +0800 | [diff] [blame] | 256 | return da9055_reg_update(regulator->da9055, info->volt.reg_a, |
| 257 | info->volt.v_mask, selector); |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Here regulator register set A/B is selected through GPIO. |
| 262 | * Therefore we first determine the selected register set A/B and |
| 263 | * then set the desired voltage for that register set A/B. |
| 264 | */ |
| 265 | ret = da9055_reg_read(regulator->da9055, info->conf.reg); |
| 266 | if (ret < 0) |
| 267 | return ret; |
| 268 | |
| 269 | ret &= info->conf.sel_mask; |
| 270 | |
| 271 | /* Set the voltage */ |
| 272 | if (ret == DA9055_REGUALTOR_SET_A) |
Axel Lin | 369cf60 | 2012-11-20 15:36:46 +0800 | [diff] [blame] | 273 | return da9055_reg_update(regulator->da9055, info->volt.reg_a, |
| 274 | info->volt.v_mask, selector); |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 275 | else |
Axel Lin | 369cf60 | 2012-11-20 15:36:46 +0800 | [diff] [blame] | 276 | return da9055_reg_update(regulator->da9055, info->volt.reg_b, |
| 277 | info->volt.v_mask, selector); |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static int da9055_regulator_set_suspend_voltage(struct regulator_dev *rdev, |
| 281 | int uV) |
| 282 | { |
| 283 | struct da9055_regulator *regulator = rdev_get_drvdata(rdev); |
| 284 | struct da9055_regulator_info *info = regulator->info; |
| 285 | int ret; |
| 286 | |
| 287 | /* Select register set B for suspend voltage ramping. */ |
| 288 | if (regulator->reg_rselect == NO_GPIO) { |
| 289 | ret = da9055_reg_update(regulator->da9055, info->conf.reg, |
| 290 | info->conf.sel_mask, DA9055_SEL_REG_B); |
| 291 | if (ret < 0) |
| 292 | return ret; |
| 293 | } |
| 294 | |
Axel Lin | 6b1f8a4 | 2012-11-27 10:26:14 +0800 | [diff] [blame^] | 295 | ret = regulator_map_voltage_linear(rdev, uV, uV); |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 296 | if (ret < 0) |
| 297 | return ret; |
| 298 | |
Axel Lin | 369cf60 | 2012-11-20 15:36:46 +0800 | [diff] [blame] | 299 | return da9055_reg_update(regulator->da9055, info->volt.reg_b, |
| 300 | info->volt.v_mask, ret); |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static int da9055_suspend_enable(struct regulator_dev *rdev) |
| 304 | { |
| 305 | struct da9055_regulator *regulator = rdev_get_drvdata(rdev); |
| 306 | struct da9055_regulator_info *info = regulator->info; |
| 307 | |
| 308 | /* Select register set B for voltage ramping. */ |
| 309 | if (regulator->reg_rselect == NO_GPIO) |
| 310 | return da9055_reg_update(regulator->da9055, info->conf.reg, |
| 311 | info->conf.sel_mask, DA9055_SEL_REG_B); |
| 312 | else |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static int da9055_suspend_disable(struct regulator_dev *rdev) |
| 317 | { |
| 318 | struct da9055_regulator *regulator = rdev_get_drvdata(rdev); |
| 319 | struct da9055_regulator_info *info = regulator->info; |
| 320 | |
| 321 | /* Diselect register set B. */ |
| 322 | if (regulator->reg_rselect == NO_GPIO) |
| 323 | return da9055_reg_update(regulator->da9055, info->conf.reg, |
| 324 | info->conf.sel_mask, DA9055_SEL_REG_A); |
| 325 | else |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static struct regulator_ops da9055_buck_ops = { |
| 330 | .get_mode = da9055_buck_get_mode, |
| 331 | .set_mode = da9055_buck_set_mode, |
| 332 | |
| 333 | .get_current_limit = da9055_buck_get_current_limit, |
| 334 | .set_current_limit = da9055_buck_set_current_limit, |
| 335 | |
| 336 | .get_voltage_sel = da9055_regulator_get_voltage_sel, |
| 337 | .set_voltage_sel = da9055_regulator_set_voltage_sel, |
Axel Lin | 6b1f8a4 | 2012-11-27 10:26:14 +0800 | [diff] [blame^] | 338 | .list_voltage = regulator_list_voltage_linear, |
| 339 | .map_voltage = regulator_map_voltage_linear, |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 340 | .is_enabled = regulator_is_enabled_regmap, |
| 341 | .enable = regulator_enable_regmap, |
| 342 | .disable = regulator_disable_regmap, |
| 343 | |
| 344 | .set_suspend_voltage = da9055_regulator_set_suspend_voltage, |
| 345 | .set_suspend_enable = da9055_suspend_enable, |
| 346 | .set_suspend_disable = da9055_suspend_disable, |
| 347 | .set_suspend_mode = da9055_buck_set_mode, |
| 348 | }; |
| 349 | |
| 350 | static struct regulator_ops da9055_ldo_ops = { |
| 351 | .get_mode = da9055_ldo_get_mode, |
| 352 | .set_mode = da9055_ldo_set_mode, |
| 353 | |
| 354 | .get_voltage_sel = da9055_regulator_get_voltage_sel, |
| 355 | .set_voltage_sel = da9055_regulator_set_voltage_sel, |
Axel Lin | 6b1f8a4 | 2012-11-27 10:26:14 +0800 | [diff] [blame^] | 356 | .list_voltage = regulator_list_voltage_linear, |
| 357 | .map_voltage = regulator_map_voltage_linear, |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 358 | .is_enabled = regulator_is_enabled_regmap, |
| 359 | .enable = regulator_enable_regmap, |
| 360 | .disable = regulator_disable_regmap, |
| 361 | |
| 362 | .set_suspend_voltage = da9055_regulator_set_suspend_voltage, |
| 363 | .set_suspend_enable = da9055_suspend_enable, |
| 364 | .set_suspend_disable = da9055_suspend_disable, |
| 365 | .set_suspend_mode = da9055_ldo_set_mode, |
| 366 | |
| 367 | }; |
| 368 | |
| 369 | #define DA9055_LDO(_id, step, min, max, vbits, voffset) \ |
| 370 | {\ |
| 371 | .reg_desc = {\ |
| 372 | .name = #_id,\ |
| 373 | .ops = &da9055_ldo_ops,\ |
| 374 | .type = REGULATOR_VOLTAGE,\ |
| 375 | .id = DA9055_ID_##_id,\ |
Axel Lin | 369cf60 | 2012-11-20 15:36:46 +0800 | [diff] [blame] | 376 | .n_voltages = (max - min) / step + 1 + (voffset), \ |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 377 | .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \ |
| 378 | .enable_mask = 1, \ |
| 379 | .min_uV = (min) * 1000,\ |
| 380 | .uV_step = (step) * 1000,\ |
Axel Lin | 6b1f8a4 | 2012-11-27 10:26:14 +0800 | [diff] [blame^] | 381 | .linear_min_sel = (voffset),\ |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 382 | .owner = THIS_MODULE,\ |
| 383 | },\ |
| 384 | .conf = {\ |
| 385 | .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \ |
| 386 | .sel_mask = (1 << 4),\ |
| 387 | .en_mask = 1,\ |
| 388 | },\ |
| 389 | .volt = {\ |
| 390 | .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \ |
| 391 | .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \ |
| 392 | .sl_shift = 7,\ |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 393 | .v_mask = (1 << (vbits)) - 1,\ |
| 394 | .v_shift = (vbits),\ |
| 395 | },\ |
| 396 | } |
| 397 | |
| 398 | #define DA9055_BUCK(_id, step, min, max, vbits, voffset, mbits, sbits) \ |
| 399 | {\ |
| 400 | .reg_desc = {\ |
| 401 | .name = #_id,\ |
| 402 | .ops = &da9055_buck_ops,\ |
| 403 | .type = REGULATOR_VOLTAGE,\ |
| 404 | .id = DA9055_ID_##_id,\ |
Axel Lin | 369cf60 | 2012-11-20 15:36:46 +0800 | [diff] [blame] | 405 | .n_voltages = (max - min) / step + 1 + (voffset), \ |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 406 | .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \ |
| 407 | .enable_mask = 1,\ |
| 408 | .min_uV = (min) * 1000,\ |
| 409 | .uV_step = (step) * 1000,\ |
Axel Lin | 6b1f8a4 | 2012-11-27 10:26:14 +0800 | [diff] [blame^] | 410 | .linear_min_sel = (voffset),\ |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 411 | .owner = THIS_MODULE,\ |
| 412 | },\ |
| 413 | .conf = {\ |
| 414 | .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \ |
| 415 | .sel_mask = (1 << 4),\ |
| 416 | .en_mask = 1,\ |
| 417 | },\ |
| 418 | .volt = {\ |
| 419 | .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \ |
| 420 | .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \ |
| 421 | .sl_shift = 7,\ |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 422 | .v_mask = (1 << (vbits)) - 1,\ |
| 423 | .v_shift = (vbits),\ |
| 424 | },\ |
| 425 | .mode = {\ |
| 426 | .reg = DA9055_REG_BCORE_MODE,\ |
| 427 | .mask = (mbits),\ |
| 428 | .shift = (sbits),\ |
| 429 | },\ |
| 430 | } |
| 431 | |
| 432 | static struct da9055_regulator_info da9055_regulator_info[] = { |
| 433 | DA9055_BUCK(BUCK1, 25, 725, 2075, 6, 9, 0xc, 2), |
| 434 | DA9055_BUCK(BUCK2, 25, 925, 2500, 6, 0, 3, 0), |
| 435 | DA9055_LDO(LDO1, 50, 900, 3300, 6, 2), |
| 436 | DA9055_LDO(LDO2, 50, 900, 3300, 6, 3), |
| 437 | DA9055_LDO(LDO3, 50, 900, 3300, 6, 2), |
| 438 | DA9055_LDO(LDO4, 50, 900, 3300, 6, 2), |
| 439 | DA9055_LDO(LDO5, 50, 900, 2750, 6, 2), |
| 440 | DA9055_LDO(LDO6, 20, 900, 3300, 7, 0), |
| 441 | }; |
| 442 | |
| 443 | /* |
| 444 | * Configures regulator to be controlled either through GPIO 1 or 2. |
| 445 | * GPIO can control regulator state and/or select the regulator register |
| 446 | * set A/B for voltage ramping. |
| 447 | */ |
| 448 | static __devinit int da9055_gpio_init(struct da9055_regulator *regulator, |
| 449 | struct regulator_config *config, |
| 450 | struct da9055_pdata *pdata, int id) |
| 451 | { |
| 452 | struct da9055_regulator_info *info = regulator->info; |
| 453 | int ret = 0; |
| 454 | |
| 455 | if (pdata->gpio_ren && pdata->gpio_ren[id]) { |
| 456 | char name[18]; |
| 457 | int gpio_mux = pdata->gpio_ren[id]; |
| 458 | |
| 459 | config->ena_gpio = pdata->ena_gpio[id]; |
| 460 | config->ena_gpio_flags = GPIOF_OUT_INIT_HIGH; |
| 461 | config->ena_gpio_invert = 1; |
| 462 | |
| 463 | /* |
| 464 | * GPI pin is muxed with regulator to control the |
| 465 | * regulator state. |
| 466 | */ |
| 467 | sprintf(name, "DA9055 GPI %d", gpio_mux); |
| 468 | ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN, |
| 469 | name); |
| 470 | if (ret < 0) |
| 471 | goto err; |
| 472 | |
| 473 | /* |
| 474 | * Let the regulator know that its state is controlled |
| 475 | * through GPI. |
| 476 | */ |
| 477 | ret = da9055_reg_update(regulator->da9055, info->conf.reg, |
| 478 | DA9055_E_GPI_MASK, |
| 479 | pdata->reg_ren[id] |
| 480 | << DA9055_E_GPI_SHIFT); |
| 481 | if (ret < 0) |
| 482 | goto err; |
| 483 | } |
| 484 | |
Axel Lin | 26cbd30 | 2012-11-16 00:35:14 +0800 | [diff] [blame] | 485 | if (pdata->gpio_rsel && pdata->gpio_rsel[id]) { |
Ashish Jangam | f6130be | 2012-11-01 13:57:56 +0530 | [diff] [blame] | 486 | char name[18]; |
| 487 | int gpio_mux = pdata->gpio_rsel[id]; |
| 488 | |
| 489 | regulator->reg_rselect = pdata->reg_rsel[id]; |
| 490 | |
| 491 | /* |
| 492 | * GPI pin is muxed with regulator to select the |
| 493 | * regulator register set A/B for voltage ramping. |
| 494 | */ |
| 495 | sprintf(name, "DA9055 GPI %d", gpio_mux); |
| 496 | ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN, |
| 497 | name); |
| 498 | if (ret < 0) |
| 499 | goto err; |
| 500 | |
| 501 | /* |
| 502 | * Let the regulator know that its register set A/B |
| 503 | * will be selected through GPI for voltage ramping. |
| 504 | */ |
| 505 | ret = da9055_reg_update(regulator->da9055, info->conf.reg, |
| 506 | DA9055_V_GPI_MASK, |
| 507 | pdata->reg_rsel[id] |
| 508 | << DA9055_V_GPI_SHIFT); |
| 509 | } |
| 510 | |
| 511 | err: |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | static irqreturn_t da9055_ldo5_6_oc_irq(int irq, void *data) |
| 516 | { |
| 517 | struct da9055_regulator *regulator = data; |
| 518 | |
| 519 | regulator_notifier_call_chain(regulator->rdev, |
| 520 | REGULATOR_EVENT_OVER_CURRENT, NULL); |
| 521 | |
| 522 | return IRQ_HANDLED; |
| 523 | } |
| 524 | |
| 525 | static inline struct da9055_regulator_info *find_regulator_info(int id) |
| 526 | { |
| 527 | struct da9055_regulator_info *info; |
| 528 | int i; |
| 529 | |
| 530 | for (i = 0; i < ARRAY_SIZE(da9055_regulator_info); i++) { |
| 531 | info = &da9055_regulator_info[i]; |
| 532 | if (info->reg_desc.id == id) |
| 533 | return info; |
| 534 | } |
| 535 | |
| 536 | return NULL; |
| 537 | } |
| 538 | |
| 539 | static int __devinit da9055_regulator_probe(struct platform_device *pdev) |
| 540 | { |
| 541 | struct regulator_config config = { }; |
| 542 | struct da9055_regulator *regulator; |
| 543 | struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent); |
| 544 | struct da9055_pdata *pdata = da9055->dev->platform_data; |
| 545 | int ret, irq; |
| 546 | |
| 547 | if (pdata == NULL || pdata->regulators[pdev->id] == NULL) |
| 548 | return -ENODEV; |
| 549 | |
| 550 | regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9055_regulator), |
| 551 | GFP_KERNEL); |
| 552 | if (!regulator) |
| 553 | return -ENOMEM; |
| 554 | |
| 555 | regulator->info = find_regulator_info(pdev->id); |
| 556 | if (regulator->info == NULL) { |
| 557 | dev_err(&pdev->dev, "invalid regulator ID specified\n"); |
| 558 | return -EINVAL; |
| 559 | } |
| 560 | |
| 561 | regulator->da9055 = da9055; |
| 562 | config.dev = &pdev->dev; |
| 563 | config.driver_data = regulator; |
| 564 | config.regmap = da9055->regmap; |
| 565 | |
| 566 | if (pdata && pdata->regulators) |
| 567 | config.init_data = pdata->regulators[pdev->id]; |
| 568 | |
| 569 | ret = da9055_gpio_init(regulator, &config, pdata, pdev->id); |
| 570 | if (ret < 0) |
| 571 | return ret; |
| 572 | |
| 573 | regulator->rdev = regulator_register(®ulator->info->reg_desc, |
| 574 | &config); |
| 575 | if (IS_ERR(regulator->rdev)) { |
| 576 | dev_err(&pdev->dev, "Failed to register regulator %s\n", |
| 577 | regulator->info->reg_desc.name); |
| 578 | ret = PTR_ERR(regulator->rdev); |
| 579 | return ret; |
| 580 | } |
| 581 | |
| 582 | /* Only LDO 5 and 6 has got the over current interrupt */ |
| 583 | if (pdev->id == DA9055_ID_LDO5 || pdev->id == DA9055_ID_LDO6) { |
| 584 | irq = platform_get_irq_byname(pdev, "REGULATOR"); |
| 585 | irq = regmap_irq_get_virq(da9055->irq_data, irq); |
| 586 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
| 587 | da9055_ldo5_6_oc_irq, |
| 588 | IRQF_TRIGGER_HIGH | |
| 589 | IRQF_ONESHOT | |
| 590 | IRQF_PROBE_SHARED, |
| 591 | pdev->name, regulator); |
| 592 | if (ret != 0) { |
| 593 | if (ret != -EBUSY) { |
| 594 | dev_err(&pdev->dev, |
| 595 | "Failed to request Regulator IRQ %d: %d\n", |
| 596 | irq, ret); |
| 597 | goto err_regulator; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | platform_set_drvdata(pdev, regulator); |
| 603 | |
| 604 | return 0; |
| 605 | |
| 606 | err_regulator: |
| 607 | regulator_unregister(regulator->rdev); |
| 608 | return ret; |
| 609 | } |
| 610 | |
| 611 | static int __devexit da9055_regulator_remove(struct platform_device *pdev) |
| 612 | { |
| 613 | struct da9055_regulator *regulator = platform_get_drvdata(pdev); |
| 614 | |
| 615 | regulator_unregister(regulator->rdev); |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static struct platform_driver da9055_regulator_driver = { |
| 621 | .probe = da9055_regulator_probe, |
| 622 | .remove = __devexit_p(da9055_regulator_remove), |
| 623 | .driver = { |
| 624 | .name = "da9055-regulator", |
| 625 | .owner = THIS_MODULE, |
| 626 | }, |
| 627 | }; |
| 628 | |
| 629 | static int __init da9055_regulator_init(void) |
| 630 | { |
| 631 | return platform_driver_register(&da9055_regulator_driver); |
| 632 | } |
| 633 | subsys_initcall(da9055_regulator_init); |
| 634 | |
| 635 | static void __exit da9055_regulator_exit(void) |
| 636 | { |
| 637 | platform_driver_unregister(&da9055_regulator_driver); |
| 638 | } |
| 639 | module_exit(da9055_regulator_exit); |
| 640 | |
| 641 | MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>"); |
| 642 | MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9055 PMIC"); |
| 643 | MODULE_LICENSE("GPL"); |
| 644 | MODULE_ALIAS("platform:da9055-regulator"); |