Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 1 | /* |
| 2 | * tps65912.c -- TI tps65912 |
| 3 | * |
| 4 | * Copyright 2011 Texas Instruments Inc. |
| 5 | * |
| 6 | * Author: Margarita Olaya Cabrera <magi@slimlogic.co.uk> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | * This driver is based on wm8350 implementation. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/regulator/driver.h> |
| 22 | #include <linux/regulator/machine.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/gpio.h> |
| 26 | #include <linux/mfd/tps65912.h> |
| 27 | |
| 28 | /* DCDC's */ |
| 29 | #define TPS65912_REG_DCDC1 0 |
| 30 | #define TPS65912_REG_DCDC2 1 |
| 31 | #define TPS65912_REG_DCDC3 2 |
| 32 | #define TPS65912_REG_DCDC4 3 |
| 33 | |
| 34 | /* LDOs */ |
| 35 | #define TPS65912_REG_LDO1 4 |
| 36 | #define TPS65912_REG_LDO2 5 |
| 37 | #define TPS65912_REG_LDO3 6 |
| 38 | #define TPS65912_REG_LDO4 7 |
| 39 | #define TPS65912_REG_LDO5 8 |
| 40 | #define TPS65912_REG_LDO6 9 |
| 41 | #define TPS65912_REG_LDO7 10 |
| 42 | #define TPS65912_REG_LDO8 11 |
| 43 | #define TPS65912_REG_LDO9 12 |
| 44 | #define TPS65912_REG_LDO10 13 |
| 45 | |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 46 | /* Number of step-down converters available */ |
| 47 | #define TPS65912_NUM_DCDC 4 |
| 48 | |
| 49 | /* Number of LDO voltage regulators available */ |
| 50 | #define TPS65912_NUM_LDO 10 |
| 51 | |
| 52 | /* Number of total regulators available */ |
| 53 | #define TPS65912_NUM_REGULATOR (TPS65912_NUM_DCDC + TPS65912_NUM_LDO) |
| 54 | |
| 55 | #define TPS65912_REG_ENABLED 0x80 |
| 56 | #define OP_SELREG_MASK 0x40 |
| 57 | #define OP_SELREG_SHIFT 6 |
| 58 | |
| 59 | struct tps_info { |
| 60 | const char *name; |
| 61 | }; |
| 62 | |
| 63 | static struct tps_info tps65912_regs[] = { |
| 64 | { |
| 65 | .name = "DCDC1", |
| 66 | }, |
| 67 | { |
| 68 | .name = "DCDC2", |
| 69 | }, |
| 70 | { |
| 71 | .name = "DCDC3", |
| 72 | }, |
| 73 | { |
| 74 | .name = "DCDC4", |
| 75 | }, |
| 76 | { |
| 77 | .name = "LDO1", |
| 78 | }, |
| 79 | { |
| 80 | .name = "LDO2", |
| 81 | }, |
| 82 | { |
| 83 | .name = "LDO3", |
| 84 | }, |
| 85 | { |
| 86 | .name = "LDO4", |
| 87 | }, |
| 88 | { |
| 89 | .name = "LDO5", |
| 90 | }, |
| 91 | { |
| 92 | .name = "LDO6", |
| 93 | }, |
| 94 | { |
| 95 | .name = "LDO7", |
| 96 | }, |
| 97 | { |
| 98 | .name = "LDO8", |
| 99 | }, |
| 100 | { |
| 101 | .name = "LDO9", |
| 102 | }, |
| 103 | { |
| 104 | .name = "LDO10", |
| 105 | }, |
| 106 | }; |
| 107 | |
| 108 | struct tps65912_reg { |
| 109 | struct regulator_desc desc[TPS65912_NUM_REGULATOR]; |
| 110 | struct tps65912 *mfd; |
| 111 | struct regulator_dev *rdev[TPS65912_NUM_REGULATOR]; |
| 112 | struct tps_info *info[TPS65912_NUM_REGULATOR]; |
| 113 | /* for read/write access */ |
| 114 | struct mutex io_lock; |
| 115 | int mode; |
| 116 | int (*get_ctrl_reg)(int); |
Axel Lin | 85c5d86 | 2012-03-09 07:05:41 +0800 | [diff] [blame] | 117 | int dcdc_range[TPS65912_NUM_DCDC]; |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 118 | int pwm_mode_reg; |
| 119 | int eco_reg; |
| 120 | }; |
| 121 | |
| 122 | static int tps65912_get_range(struct tps65912_reg *pmic, int id) |
| 123 | { |
| 124 | struct tps65912 *mfd = pmic->mfd; |
Axel Lin | 85c5d86 | 2012-03-09 07:05:41 +0800 | [diff] [blame] | 125 | int range; |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 126 | |
| 127 | switch (id) { |
| 128 | case TPS65912_REG_DCDC1: |
Axel Lin | 85c5d86 | 2012-03-09 07:05:41 +0800 | [diff] [blame] | 129 | range = tps65912_reg_read(mfd, TPS65912_DCDC1_LIMIT); |
| 130 | break; |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 131 | case TPS65912_REG_DCDC2: |
Axel Lin | 85c5d86 | 2012-03-09 07:05:41 +0800 | [diff] [blame] | 132 | range = tps65912_reg_read(mfd, TPS65912_DCDC2_LIMIT); |
| 133 | break; |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 134 | case TPS65912_REG_DCDC3: |
Axel Lin | 85c5d86 | 2012-03-09 07:05:41 +0800 | [diff] [blame] | 135 | range = tps65912_reg_read(mfd, TPS65912_DCDC3_LIMIT); |
| 136 | break; |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 137 | case TPS65912_REG_DCDC4: |
Axel Lin | 85c5d86 | 2012-03-09 07:05:41 +0800 | [diff] [blame] | 138 | range = tps65912_reg_read(mfd, TPS65912_DCDC4_LIMIT); |
| 139 | break; |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 140 | default: |
| 141 | return 0; |
| 142 | } |
Axel Lin | 85c5d86 | 2012-03-09 07:05:41 +0800 | [diff] [blame] | 143 | |
| 144 | if (range >= 0) |
| 145 | range = (range & DCDC_LIMIT_RANGE_MASK) |
| 146 | >> DCDC_LIMIT_RANGE_SHIFT; |
| 147 | |
| 148 | pmic->dcdc_range[id] = range; |
| 149 | return range; |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static unsigned long tps65912_vsel_to_uv_range0(u8 vsel) |
| 153 | { |
| 154 | unsigned long uv; |
| 155 | |
| 156 | uv = ((vsel * 12500) + 500000); |
| 157 | return uv; |
| 158 | } |
| 159 | |
| 160 | static unsigned long tps65912_vsel_to_uv_range1(u8 vsel) |
| 161 | { |
| 162 | unsigned long uv; |
| 163 | |
| 164 | uv = ((vsel * 12500) + 700000); |
| 165 | return uv; |
| 166 | } |
| 167 | |
| 168 | static unsigned long tps65912_vsel_to_uv_range2(u8 vsel) |
| 169 | { |
| 170 | unsigned long uv; |
| 171 | |
| 172 | uv = ((vsel * 25000) + 500000); |
| 173 | return uv; |
| 174 | } |
| 175 | |
| 176 | static unsigned long tps65912_vsel_to_uv_range3(u8 vsel) |
| 177 | { |
| 178 | unsigned long uv; |
| 179 | |
| 180 | if (vsel == 0x3f) |
| 181 | uv = 3800000; |
| 182 | else |
| 183 | uv = ((vsel * 50000) + 500000); |
| 184 | |
| 185 | return uv; |
| 186 | } |
| 187 | |
| 188 | static unsigned long tps65912_vsel_to_uv_ldo(u8 vsel) |
| 189 | { |
| 190 | unsigned long uv = 0; |
| 191 | |
| 192 | if (vsel <= 32) |
| 193 | uv = ((vsel * 25000) + 800000); |
| 194 | else if (vsel > 32 && vsel <= 60) |
| 195 | uv = (((vsel - 32) * 50000) + 1600000); |
| 196 | else if (vsel > 60) |
| 197 | uv = (((vsel - 60) * 100000) + 3000000); |
| 198 | |
| 199 | return uv; |
| 200 | } |
| 201 | |
| 202 | static int tps65912_get_ctrl_register(int id) |
| 203 | { |
Axel Lin | 42b5efe | 2012-03-13 06:40:48 +0800 | [diff] [blame^] | 204 | if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4) |
| 205 | return id * 3 + TPS65912_DCDC1_AVS; |
| 206 | else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10) |
| 207 | return id - TPS65912_REG_LDO5 + TPS65912_LDO5; |
| 208 | else |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 209 | return -EINVAL; |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 210 | } |
| 211 | |
Axel Lin | 42b5efe | 2012-03-13 06:40:48 +0800 | [diff] [blame^] | 212 | static int tps65912_get_sel_register(struct tps65912_reg *pmic, int id) |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 213 | { |
| 214 | struct tps65912 *mfd = pmic->mfd; |
Axel Lin | 42b5efe | 2012-03-13 06:40:48 +0800 | [diff] [blame^] | 215 | int opvsel; |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 216 | u8 reg = 0; |
| 217 | |
Axel Lin | 42b5efe | 2012-03-13 06:40:48 +0800 | [diff] [blame^] | 218 | if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4) { |
| 219 | opvsel = tps65912_reg_read(mfd, id * 3 + TPS65912_DCDC1_OP); |
| 220 | if (opvsel & OP_SELREG_MASK) |
| 221 | reg = id * 3 + TPS65912_DCDC1_AVS; |
| 222 | else |
| 223 | reg = id * 3 + TPS65912_DCDC1_OP; |
| 224 | } else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10) { |
| 225 | reg = id - TPS65912_REG_LDO5 + TPS65912_LDO5; |
| 226 | } else { |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 227 | return -EINVAL; |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return reg; |
| 231 | } |
| 232 | |
| 233 | static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id) |
| 234 | { |
| 235 | switch (id) { |
| 236 | case TPS65912_REG_DCDC1: |
| 237 | pmic->pwm_mode_reg = TPS65912_DCDC1_CTRL; |
| 238 | pmic->eco_reg = TPS65912_DCDC1_AVS; |
| 239 | break; |
| 240 | case TPS65912_REG_DCDC2: |
| 241 | pmic->pwm_mode_reg = TPS65912_DCDC2_CTRL; |
| 242 | pmic->eco_reg = TPS65912_DCDC2_AVS; |
| 243 | break; |
| 244 | case TPS65912_REG_DCDC3: |
| 245 | pmic->pwm_mode_reg = TPS65912_DCDC3_CTRL; |
| 246 | pmic->eco_reg = TPS65912_DCDC3_AVS; |
| 247 | break; |
| 248 | case TPS65912_REG_DCDC4: |
| 249 | pmic->pwm_mode_reg = TPS65912_DCDC4_CTRL; |
| 250 | pmic->eco_reg = TPS65912_DCDC4_AVS; |
| 251 | break; |
| 252 | default: |
| 253 | return -EINVAL; |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int tps65912_reg_is_enabled(struct regulator_dev *dev) |
| 260 | { |
| 261 | struct tps65912_reg *pmic = rdev_get_drvdata(dev); |
| 262 | struct tps65912 *mfd = pmic->mfd; |
| 263 | int reg, value, id = rdev_get_id(dev); |
| 264 | |
| 265 | if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10) |
| 266 | return -EINVAL; |
| 267 | |
| 268 | reg = pmic->get_ctrl_reg(id); |
| 269 | if (reg < 0) |
| 270 | return reg; |
| 271 | |
| 272 | value = tps65912_reg_read(mfd, reg); |
| 273 | if (value < 0) |
| 274 | return value; |
| 275 | |
| 276 | return value & TPS65912_REG_ENABLED; |
| 277 | } |
| 278 | |
| 279 | static int tps65912_reg_enable(struct regulator_dev *dev) |
| 280 | { |
| 281 | struct tps65912_reg *pmic = rdev_get_drvdata(dev); |
| 282 | struct tps65912 *mfd = pmic->mfd; |
| 283 | int id = rdev_get_id(dev); |
Dan Carpenter | 6daa663 | 2011-07-06 21:57:18 +0300 | [diff] [blame] | 284 | int reg; |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 285 | |
| 286 | if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10) |
| 287 | return -EINVAL; |
| 288 | |
| 289 | reg = pmic->get_ctrl_reg(id); |
| 290 | if (reg < 0) |
| 291 | return reg; |
| 292 | |
| 293 | return tps65912_set_bits(mfd, reg, TPS65912_REG_ENABLED); |
| 294 | } |
| 295 | |
| 296 | static int tps65912_reg_disable(struct regulator_dev *dev) |
| 297 | { |
| 298 | struct tps65912_reg *pmic = rdev_get_drvdata(dev); |
| 299 | struct tps65912 *mfd = pmic->mfd; |
| 300 | int id = rdev_get_id(dev), reg; |
| 301 | |
| 302 | reg = pmic->get_ctrl_reg(id); |
| 303 | if (reg < 0) |
| 304 | return reg; |
| 305 | |
| 306 | return tps65912_clear_bits(mfd, reg, TPS65912_REG_ENABLED); |
| 307 | } |
| 308 | |
| 309 | static int tps65912_set_mode(struct regulator_dev *dev, unsigned int mode) |
| 310 | { |
| 311 | struct tps65912_reg *pmic = rdev_get_drvdata(dev); |
| 312 | struct tps65912 *mfd = pmic->mfd; |
| 313 | int pwm_mode, eco, id = rdev_get_id(dev); |
| 314 | |
| 315 | tps65912_get_mode_regiters(pmic, id); |
| 316 | |
| 317 | pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg); |
| 318 | eco = tps65912_reg_read(mfd, pmic->eco_reg); |
| 319 | |
| 320 | pwm_mode &= DCDCCTRL_DCDC_MODE_MASK; |
| 321 | eco &= DCDC_AVS_ECO_MASK; |
| 322 | |
| 323 | switch (mode) { |
| 324 | case REGULATOR_MODE_FAST: |
| 325 | /* Verify if mode alredy set */ |
| 326 | if (pwm_mode && !eco) |
| 327 | break; |
| 328 | tps65912_set_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK); |
| 329 | tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK); |
| 330 | break; |
| 331 | case REGULATOR_MODE_NORMAL: |
| 332 | case REGULATOR_MODE_IDLE: |
| 333 | if (!pwm_mode && !eco) |
| 334 | break; |
| 335 | tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK); |
| 336 | tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK); |
| 337 | break; |
| 338 | case REGULATOR_MODE_STANDBY: |
| 339 | if (!pwm_mode && eco) |
| 340 | break; |
| 341 | tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK); |
| 342 | tps65912_set_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK); |
| 343 | break; |
| 344 | default: |
| 345 | return -EINVAL; |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static unsigned int tps65912_get_mode(struct regulator_dev *dev) |
| 352 | { |
| 353 | struct tps65912_reg *pmic = rdev_get_drvdata(dev); |
| 354 | struct tps65912 *mfd = pmic->mfd; |
| 355 | int pwm_mode, eco, mode = 0, id = rdev_get_id(dev); |
| 356 | |
| 357 | tps65912_get_mode_regiters(pmic, id); |
| 358 | |
| 359 | pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg); |
| 360 | eco = tps65912_reg_read(mfd, pmic->eco_reg); |
| 361 | |
| 362 | pwm_mode &= DCDCCTRL_DCDC_MODE_MASK; |
| 363 | eco &= DCDC_AVS_ECO_MASK; |
| 364 | |
| 365 | if (pwm_mode && !eco) |
| 366 | mode = REGULATOR_MODE_FAST; |
| 367 | else if (!pwm_mode && !eco) |
| 368 | mode = REGULATOR_MODE_NORMAL; |
| 369 | else if (!pwm_mode && eco) |
| 370 | mode = REGULATOR_MODE_STANDBY; |
| 371 | |
| 372 | return mode; |
| 373 | } |
| 374 | |
Axel Lin | 844775e | 2012-03-08 12:07:37 +0800 | [diff] [blame] | 375 | static int tps65912_list_voltage_dcdc(struct regulator_dev *dev, |
| 376 | unsigned selector) |
| 377 | { |
| 378 | struct tps65912_reg *pmic = rdev_get_drvdata(dev); |
| 379 | int range, voltage = 0, id = rdev_get_id(dev); |
| 380 | |
Axel Lin | 85c5d86 | 2012-03-09 07:05:41 +0800 | [diff] [blame] | 381 | if (id > TPS65912_REG_DCDC4) |
Axel Lin | 844775e | 2012-03-08 12:07:37 +0800 | [diff] [blame] | 382 | return -EINVAL; |
Axel Lin | 85c5d86 | 2012-03-09 07:05:41 +0800 | [diff] [blame] | 383 | |
| 384 | range = pmic->dcdc_range[id]; |
Axel Lin | 844775e | 2012-03-08 12:07:37 +0800 | [diff] [blame] | 385 | |
| 386 | switch (range) { |
| 387 | case 0: |
| 388 | /* 0.5 - 1.2875V in 12.5mV steps */ |
| 389 | voltage = tps65912_vsel_to_uv_range0(selector); |
| 390 | break; |
| 391 | case 1: |
| 392 | /* 0.7 - 1.4875V in 12.5mV steps */ |
| 393 | voltage = tps65912_vsel_to_uv_range1(selector); |
| 394 | break; |
| 395 | case 2: |
| 396 | /* 0.5 - 2.075V in 25mV steps */ |
| 397 | voltage = tps65912_vsel_to_uv_range2(selector); |
| 398 | break; |
| 399 | case 3: |
| 400 | /* 0.5 - 3.8V in 50mV steps */ |
| 401 | voltage = tps65912_vsel_to_uv_range3(selector); |
| 402 | break; |
| 403 | } |
| 404 | return voltage; |
| 405 | } |
| 406 | |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 407 | static int tps65912_get_voltage_dcdc(struct regulator_dev *dev) |
| 408 | { |
| 409 | struct tps65912_reg *pmic = rdev_get_drvdata(dev); |
| 410 | struct tps65912 *mfd = pmic->mfd; |
Axel Lin | 844775e | 2012-03-08 12:07:37 +0800 | [diff] [blame] | 411 | int id = rdev_get_id(dev); |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 412 | int opvsel = 0, avsel = 0, sr, vsel; |
| 413 | |
| 414 | switch (id) { |
| 415 | case TPS65912_REG_DCDC1: |
| 416 | opvsel = tps65912_reg_read(mfd, TPS65912_DCDC1_OP); |
| 417 | avsel = tps65912_reg_read(mfd, TPS65912_DCDC1_AVS); |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 418 | break; |
| 419 | case TPS65912_REG_DCDC2: |
| 420 | opvsel = tps65912_reg_read(mfd, TPS65912_DCDC2_OP); |
| 421 | avsel = tps65912_reg_read(mfd, TPS65912_DCDC2_AVS); |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 422 | break; |
| 423 | case TPS65912_REG_DCDC3: |
| 424 | opvsel = tps65912_reg_read(mfd, TPS65912_DCDC3_OP); |
| 425 | avsel = tps65912_reg_read(mfd, TPS65912_DCDC3_AVS); |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 426 | break; |
| 427 | case TPS65912_REG_DCDC4: |
| 428 | opvsel = tps65912_reg_read(mfd, TPS65912_DCDC4_OP); |
| 429 | avsel = tps65912_reg_read(mfd, TPS65912_DCDC4_AVS); |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 430 | break; |
| 431 | default: |
| 432 | return -EINVAL; |
| 433 | } |
| 434 | |
| 435 | sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT; |
| 436 | if (sr) |
| 437 | vsel = avsel; |
| 438 | else |
| 439 | vsel = opvsel; |
| 440 | vsel &= 0x3F; |
| 441 | |
Axel Lin | 844775e | 2012-03-08 12:07:37 +0800 | [diff] [blame] | 442 | return tps65912_list_voltage_dcdc(dev, vsel); |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 443 | } |
| 444 | |
Axel Lin | 94732b9 | 2012-03-09 10:22:20 +0800 | [diff] [blame] | 445 | static int tps65912_set_voltage_dcdc_sel(struct regulator_dev *dev, |
| 446 | unsigned selector) |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 447 | { |
| 448 | struct tps65912_reg *pmic = rdev_get_drvdata(dev); |
| 449 | struct tps65912 *mfd = pmic->mfd; |
| 450 | int id = rdev_get_id(dev); |
| 451 | int value; |
| 452 | u8 reg; |
| 453 | |
Axel Lin | 42b5efe | 2012-03-13 06:40:48 +0800 | [diff] [blame^] | 454 | reg = tps65912_get_sel_register(pmic, id); |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 455 | value = tps65912_reg_read(mfd, reg); |
| 456 | value &= 0xC0; |
| 457 | return tps65912_reg_write(mfd, reg, selector | value); |
| 458 | } |
| 459 | |
| 460 | static int tps65912_get_voltage_ldo(struct regulator_dev *dev) |
| 461 | { |
| 462 | struct tps65912_reg *pmic = rdev_get_drvdata(dev); |
| 463 | struct tps65912 *mfd = pmic->mfd; |
| 464 | int id = rdev_get_id(dev); |
| 465 | int vsel = 0; |
| 466 | u8 reg; |
| 467 | |
Axel Lin | 42b5efe | 2012-03-13 06:40:48 +0800 | [diff] [blame^] | 468 | reg = tps65912_get_sel_register(pmic, id); |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 469 | vsel = tps65912_reg_read(mfd, reg); |
| 470 | vsel &= 0x3F; |
| 471 | |
| 472 | return tps65912_vsel_to_uv_ldo(vsel); |
| 473 | } |
| 474 | |
Axel Lin | 94732b9 | 2012-03-09 10:22:20 +0800 | [diff] [blame] | 475 | static int tps65912_set_voltage_ldo_sel(struct regulator_dev *dev, |
| 476 | unsigned selector) |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 477 | { |
| 478 | struct tps65912_reg *pmic = rdev_get_drvdata(dev); |
| 479 | struct tps65912 *mfd = pmic->mfd; |
| 480 | int id = rdev_get_id(dev), reg, value; |
| 481 | |
Axel Lin | 42b5efe | 2012-03-13 06:40:48 +0800 | [diff] [blame^] | 482 | reg = tps65912_get_sel_register(pmic, id); |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 483 | value = tps65912_reg_read(mfd, reg); |
| 484 | value &= 0xC0; |
| 485 | return tps65912_reg_write(mfd, reg, selector | value); |
| 486 | } |
| 487 | |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 488 | static int tps65912_list_voltage_ldo(struct regulator_dev *dev, |
| 489 | unsigned selector) |
| 490 | { |
| 491 | int ldo = rdev_get_id(dev); |
| 492 | |
| 493 | if (ldo < TPS65912_REG_LDO1 || ldo > TPS65912_REG_LDO10) |
| 494 | return -EINVAL; |
| 495 | |
| 496 | return tps65912_vsel_to_uv_ldo(selector); |
| 497 | } |
| 498 | |
| 499 | /* Operations permitted on DCDCx */ |
| 500 | static struct regulator_ops tps65912_ops_dcdc = { |
| 501 | .is_enabled = tps65912_reg_is_enabled, |
| 502 | .enable = tps65912_reg_enable, |
| 503 | .disable = tps65912_reg_disable, |
| 504 | .set_mode = tps65912_set_mode, |
| 505 | .get_mode = tps65912_get_mode, |
| 506 | .get_voltage = tps65912_get_voltage_dcdc, |
Axel Lin | 94732b9 | 2012-03-09 10:22:20 +0800 | [diff] [blame] | 507 | .set_voltage_sel = tps65912_set_voltage_dcdc_sel, |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 508 | .list_voltage = tps65912_list_voltage_dcdc, |
| 509 | }; |
| 510 | |
| 511 | /* Operations permitted on LDOx */ |
| 512 | static struct regulator_ops tps65912_ops_ldo = { |
| 513 | .is_enabled = tps65912_reg_is_enabled, |
| 514 | .enable = tps65912_reg_enable, |
| 515 | .disable = tps65912_reg_disable, |
| 516 | .get_voltage = tps65912_get_voltage_ldo, |
Axel Lin | 94732b9 | 2012-03-09 10:22:20 +0800 | [diff] [blame] | 517 | .set_voltage_sel = tps65912_set_voltage_ldo_sel, |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 518 | .list_voltage = tps65912_list_voltage_ldo, |
| 519 | }; |
| 520 | |
| 521 | static __devinit int tps65912_probe(struct platform_device *pdev) |
| 522 | { |
| 523 | struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent); |
| 524 | struct tps_info *info; |
| 525 | struct regulator_init_data *reg_data; |
| 526 | struct regulator_dev *rdev; |
| 527 | struct tps65912_reg *pmic; |
| 528 | struct tps65912_board *pmic_plat_data; |
| 529 | int i, err; |
| 530 | |
| 531 | pmic_plat_data = dev_get_platdata(tps65912->dev); |
| 532 | if (!pmic_plat_data) |
| 533 | return -EINVAL; |
| 534 | |
| 535 | reg_data = pmic_plat_data->tps65912_pmic_init_data; |
| 536 | |
| 537 | pmic = kzalloc(sizeof(*pmic), GFP_KERNEL); |
| 538 | if (!pmic) |
| 539 | return -ENOMEM; |
| 540 | |
| 541 | mutex_init(&pmic->io_lock); |
| 542 | pmic->mfd = tps65912; |
| 543 | platform_set_drvdata(pdev, pmic); |
| 544 | |
| 545 | pmic->get_ctrl_reg = &tps65912_get_ctrl_register; |
| 546 | info = tps65912_regs; |
| 547 | |
| 548 | for (i = 0; i < TPS65912_NUM_REGULATOR; i++, info++, reg_data++) { |
| 549 | int range = 0; |
| 550 | /* Register the regulators */ |
| 551 | pmic->info[i] = info; |
| 552 | |
| 553 | pmic->desc[i].name = info->name; |
| 554 | pmic->desc[i].id = i; |
| 555 | pmic->desc[i].n_voltages = 64; |
| 556 | pmic->desc[i].ops = (i > TPS65912_REG_DCDC4 ? |
| 557 | &tps65912_ops_ldo : &tps65912_ops_dcdc); |
| 558 | pmic->desc[i].type = REGULATOR_VOLTAGE; |
| 559 | pmic->desc[i].owner = THIS_MODULE; |
| 560 | range = tps65912_get_range(pmic, i); |
| 561 | rdev = regulator_register(&pmic->desc[i], |
Rajendra Nayak | 2c043bc | 2011-11-18 16:47:19 +0530 | [diff] [blame] | 562 | tps65912->dev, reg_data, pmic, NULL); |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 563 | if (IS_ERR(rdev)) { |
| 564 | dev_err(tps65912->dev, |
| 565 | "failed to register %s regulator\n", |
| 566 | pdev->name); |
| 567 | err = PTR_ERR(rdev); |
| 568 | goto err; |
| 569 | } |
| 570 | |
| 571 | /* Save regulator for cleanup */ |
| 572 | pmic->rdev[i] = rdev; |
| 573 | } |
| 574 | return 0; |
| 575 | |
| 576 | err: |
| 577 | while (--i >= 0) |
| 578 | regulator_unregister(pmic->rdev[i]); |
| 579 | |
| 580 | kfree(pmic); |
| 581 | return err; |
| 582 | } |
| 583 | |
| 584 | static int __devexit tps65912_remove(struct platform_device *pdev) |
| 585 | { |
| 586 | struct tps65912_reg *tps65912_reg = platform_get_drvdata(pdev); |
| 587 | int i; |
| 588 | |
| 589 | for (i = 0; i < TPS65912_NUM_REGULATOR; i++) |
| 590 | regulator_unregister(tps65912_reg->rdev[i]); |
| 591 | |
| 592 | kfree(tps65912_reg); |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | static struct platform_driver tps65912_driver = { |
| 597 | .driver = { |
| 598 | .name = "tps65912-pmic", |
| 599 | .owner = THIS_MODULE, |
| 600 | }, |
| 601 | .probe = tps65912_probe, |
| 602 | .remove = __devexit_p(tps65912_remove), |
| 603 | }; |
| 604 | |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 605 | static int __init tps65912_init(void) |
| 606 | { |
| 607 | return platform_driver_register(&tps65912_driver); |
| 608 | } |
| 609 | subsys_initcall(tps65912_init); |
| 610 | |
Margarita Olaya | 9260ad98 | 2011-06-09 14:50:27 -0500 | [diff] [blame] | 611 | static void __exit tps65912_cleanup(void) |
| 612 | { |
| 613 | platform_driver_unregister(&tps65912_driver); |
| 614 | } |
| 615 | module_exit(tps65912_cleanup); |
| 616 | |
| 617 | MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>"); |
| 618 | MODULE_DESCRIPTION("TPS65912 voltage regulator driver"); |
| 619 | MODULE_LICENSE("GPL v2"); |
| 620 | MODULE_ALIAS("platform:tps65912-pmic"); |