Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame^] | 1 | /* Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 14 | |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/of.h> |
| 24 | #include <linux/of_device.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/regulator/driver.h> |
| 27 | #include <linux/regulator/machine.h> |
| 28 | #include <linux/regulator/of_regulator.h> |
| 29 | #include <linux/regulator/krait-regulator.h> |
| 30 | |
| 31 | #include "spm.h" |
| 32 | |
| 33 | #define PMIC_VOLTAGE_MIN 350000 |
| 34 | #define PMIC_VOLTAGE_MAX 1355000 |
| 35 | #define LV_RANGE_STEP 5000 |
| 36 | #define LV_RANGE_MIN 80000 |
| 37 | |
| 38 | #define LOAD_PER_PHASE 3200000 |
| 39 | |
| 40 | #define CORE_VOLTAGE_MIN 500000 |
| 41 | |
| 42 | /** |
| 43 | * struct pmic_gang_vreg - |
| 44 | * @name: the string used to represent the gang |
| 45 | * @pmic_vmax_uV: the current pmic gang voltage |
| 46 | * @pmic_phase_count: the number of phases turned on in the gang |
| 47 | * @krait_power_vregs: a list of krait consumers this gang supplies to |
| 48 | * @krait_power_vregs_lock: lock to prevent simultaneous access to the list |
| 49 | * and its nodes. This needs to be taken by each |
| 50 | * regulator's callback functions to prevent |
| 51 | * simultaneous updates to the pmic's phase |
| 52 | * voltage. |
| 53 | */ |
| 54 | struct pmic_gang_vreg { |
| 55 | const char *name; |
| 56 | int pmic_vmax_uV; |
| 57 | int pmic_phase_count; |
| 58 | struct list_head krait_power_vregs; |
| 59 | struct mutex krait_power_vregs_lock; |
| 60 | }; |
| 61 | |
| 62 | static struct pmic_gang_vreg *the_gang; |
| 63 | |
| 64 | enum krait_supply_mode { |
| 65 | HS_MODE = REGULATOR_MODE_NORMAL, |
| 66 | LDO_MODE = REGULATOR_MODE_IDLE, |
| 67 | }; |
| 68 | |
| 69 | struct krait_power_vreg { |
| 70 | struct list_head link; |
| 71 | struct regulator_desc desc; |
| 72 | struct regulator_dev *rdev; |
| 73 | const char *name; |
| 74 | struct pmic_gang_vreg *pvreg; |
| 75 | int uV; |
| 76 | int load_uA; |
| 77 | enum krait_supply_mode mode; |
| 78 | void __iomem *reg_base; |
| 79 | }; |
| 80 | |
| 81 | static int set_pmic_gang_phases(int phase_count) |
| 82 | { |
| 83 | return msm_spm_apcs_set_phase(phase_count); |
| 84 | } |
| 85 | |
| 86 | static int set_pmic_gang_voltage(int uV) |
| 87 | { |
| 88 | int setpoint; |
| 89 | |
| 90 | if (uV < PMIC_VOLTAGE_MIN) { |
| 91 | pr_err("requested %d < %d, restricting it to %d\n", |
| 92 | uV, PMIC_VOLTAGE_MIN, PMIC_VOLTAGE_MIN); |
| 93 | uV = PMIC_VOLTAGE_MIN; |
| 94 | } |
| 95 | if (uV > PMIC_VOLTAGE_MAX) { |
| 96 | pr_err("requested %d > %d, restricting it to %d\n", |
| 97 | uV, PMIC_VOLTAGE_MAX, PMIC_VOLTAGE_MAX); |
| 98 | uV = PMIC_VOLTAGE_MAX; |
| 99 | } |
| 100 | |
| 101 | setpoint = DIV_ROUND_UP(uV - LV_RANGE_MIN, LV_RANGE_STEP); |
| 102 | return msm_spm_apcs_set_vdd(setpoint); |
| 103 | } |
| 104 | |
| 105 | #define SLEW_RATE 2994 |
| 106 | static int pmic_gang_set_voltage(struct krait_power_vreg *from, |
| 107 | int vmax) |
| 108 | { |
| 109 | int rc; |
| 110 | struct pmic_gang_vreg *pvreg = from->pvreg; |
| 111 | int settling_us; |
| 112 | |
| 113 | if (pvreg->pmic_vmax_uV == vmax) |
| 114 | return 0; |
| 115 | |
| 116 | rc = set_pmic_gang_voltage(vmax); |
| 117 | if (rc < 0) { |
| 118 | dev_err(&from->rdev->dev, "%s failed set voltage %d rc = %d\n", |
| 119 | pvreg->name, vmax, rc); |
| 120 | return rc; |
| 121 | } |
| 122 | |
| 123 | /* delay until the voltage is settled when it is raised */ |
| 124 | if (vmax > pvreg->pmic_vmax_uV) { |
| 125 | settling_us = DIV_ROUND_UP(vmax - pvreg->pmic_vmax_uV, |
| 126 | SLEW_RATE); |
| 127 | udelay(settling_us); |
| 128 | } |
| 129 | |
| 130 | pvreg->pmic_vmax_uV = vmax; |
| 131 | |
| 132 | return rc; |
| 133 | } |
| 134 | |
| 135 | #define PHASE_SETTLING_TIME_US 10 |
| 136 | static unsigned int pmic_gang_set_phases(struct krait_power_vreg *from, |
| 137 | int load_uA) |
| 138 | { |
| 139 | struct pmic_gang_vreg *pvreg = from->pvreg; |
| 140 | int phase_count = DIV_ROUND_UP(load_uA, LOAD_PER_PHASE) - 1; |
| 141 | int rc = 0; |
| 142 | |
| 143 | if (phase_count < 0) |
| 144 | phase_count = 0; |
| 145 | |
| 146 | if (phase_count != pvreg->pmic_phase_count) { |
| 147 | rc = set_pmic_gang_phases(phase_count); |
| 148 | if (rc < 0) { |
| 149 | dev_err(&from->rdev->dev, |
| 150 | "%s failed set phase %d rc = %d\n", |
| 151 | pvreg->name, phase_count, rc); |
| 152 | return rc; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * delay until the phases are settled when |
| 157 | * the count is raised |
| 158 | */ |
| 159 | if (phase_count > pvreg->pmic_phase_count) |
| 160 | udelay(PHASE_SETTLING_TIME_US); |
| 161 | |
| 162 | pvreg->pmic_phase_count = phase_count; |
| 163 | } |
| 164 | return rc; |
| 165 | } |
| 166 | |
| 167 | static int __init pvreg_init(struct platform_device *pdev) |
| 168 | { |
| 169 | struct pmic_gang_vreg *pvreg; |
| 170 | |
| 171 | pvreg = devm_kzalloc(&pdev->dev, |
| 172 | sizeof(struct pmic_gang_vreg), GFP_KERNEL); |
| 173 | if (!pvreg) { |
| 174 | pr_err("kzalloc failed.\n"); |
| 175 | return -ENOMEM; |
| 176 | } |
| 177 | |
| 178 | pvreg->name = "pmic_gang"; |
| 179 | pvreg->pmic_vmax_uV = PMIC_VOLTAGE_MIN; |
| 180 | pvreg->pmic_phase_count = 1; |
| 181 | |
| 182 | mutex_init(&pvreg->krait_power_vregs_lock); |
| 183 | INIT_LIST_HEAD(&pvreg->krait_power_vregs); |
| 184 | the_gang = pvreg; |
| 185 | |
| 186 | pr_debug("name=%s inited\n", pvreg->name); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static int krait_power_get_voltage(struct regulator_dev *rdev) |
| 192 | { |
| 193 | struct krait_power_vreg *kvreg = rdev_get_drvdata(rdev); |
| 194 | |
| 195 | return kvreg->uV; |
| 196 | } |
| 197 | |
| 198 | static int get_vmax(struct krait_power_vreg *from, int min_uV) |
| 199 | { |
| 200 | int vmax = 0; |
| 201 | int v; |
| 202 | struct krait_power_vreg *kvreg; |
| 203 | struct pmic_gang_vreg *pvreg = from->pvreg; |
| 204 | |
| 205 | list_for_each_entry(kvreg, &pvreg->krait_power_vregs, link) { |
| 206 | v = kvreg->uV; |
| 207 | |
| 208 | if (kvreg == from) |
| 209 | v = min_uV; |
| 210 | |
| 211 | if (vmax < v) |
| 212 | vmax = v; |
| 213 | } |
| 214 | return vmax; |
| 215 | } |
| 216 | |
| 217 | static int get_total_load(struct krait_power_vreg *from) |
| 218 | { |
| 219 | int load_total = 0; |
| 220 | struct krait_power_vreg *kvreg; |
| 221 | struct pmic_gang_vreg *pvreg = from->pvreg; |
| 222 | |
| 223 | list_for_each_entry(kvreg, &pvreg->krait_power_vregs, link) |
| 224 | load_total += kvreg->load_uA; |
| 225 | |
| 226 | return load_total; |
| 227 | } |
| 228 | |
| 229 | static int krait_power_set_voltage(struct regulator_dev *rdev, |
| 230 | int min_uV, int max_uV, unsigned *selector) |
| 231 | { |
| 232 | struct krait_power_vreg *kvreg = rdev_get_drvdata(rdev); |
| 233 | struct pmic_gang_vreg *pvreg = kvreg->pvreg; |
| 234 | int rc; |
| 235 | int vmax; |
| 236 | |
| 237 | mutex_lock(&pvreg->krait_power_vregs_lock); |
| 238 | |
| 239 | vmax = get_vmax(kvreg, min_uV); |
| 240 | |
| 241 | rc = pmic_gang_set_voltage(kvreg, vmax); |
| 242 | if (rc < 0) { |
| 243 | dev_err(&rdev->dev, "%s failed set voltage (%d, %d) rc = %d\n", |
| 244 | kvreg->name, min_uV, max_uV, rc); |
| 245 | goto out; |
| 246 | } |
| 247 | kvreg->uV = min_uV; |
| 248 | |
| 249 | out: |
| 250 | mutex_unlock(&pvreg->krait_power_vregs_lock); |
| 251 | return rc; |
| 252 | } |
| 253 | |
| 254 | static unsigned int krait_power_get_optimum_mode(struct regulator_dev *rdev, |
| 255 | int input_uV, int output_uV, int load_uA) |
| 256 | { |
| 257 | struct krait_power_vreg *kvreg = rdev_get_drvdata(rdev); |
| 258 | struct pmic_gang_vreg *pvreg = kvreg->pvreg; |
| 259 | int rc; |
| 260 | int load_total_uA; |
| 261 | int reg_mode = -EINVAL; |
| 262 | |
| 263 | mutex_lock(&pvreg->krait_power_vregs_lock); |
| 264 | |
| 265 | kvreg->load_uA = load_uA; |
| 266 | |
| 267 | load_total_uA = get_total_load(kvreg); |
| 268 | |
| 269 | rc = pmic_gang_set_phases(kvreg, load_total_uA); |
| 270 | if (rc < 0) { |
| 271 | dev_err(&rdev->dev, "%s failed set mode %d rc = %d\n", |
| 272 | kvreg->name, load_total_uA, rc); |
| 273 | goto out; |
| 274 | } |
| 275 | |
| 276 | reg_mode = kvreg->mode; |
| 277 | out: |
| 278 | mutex_unlock(&pvreg->krait_power_vregs_lock); |
| 279 | return reg_mode; |
| 280 | } |
| 281 | |
| 282 | static int krait_power_set_mode(struct regulator_dev *rdev, unsigned int mode) |
| 283 | { |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static unsigned int krait_power_get_mode(struct regulator_dev *rdev) |
| 288 | { |
| 289 | struct krait_power_vreg *kvreg = rdev_get_drvdata(rdev); |
| 290 | |
| 291 | return kvreg->mode; |
| 292 | } |
| 293 | |
| 294 | static struct regulator_ops krait_power_ops = { |
| 295 | .get_voltage = krait_power_get_voltage, |
| 296 | .set_voltage = krait_power_set_voltage, |
| 297 | .get_optimum_mode = krait_power_get_optimum_mode, |
| 298 | .set_mode = krait_power_set_mode, |
| 299 | .get_mode = krait_power_get_mode, |
| 300 | }; |
| 301 | |
| 302 | static int __devinit krait_power_probe(struct platform_device *pdev) |
| 303 | { |
| 304 | struct krait_power_vreg *kvreg; |
| 305 | struct resource *res; |
| 306 | struct regulator_init_data *init_data = pdev->dev.platform_data; |
| 307 | int rc = 0; |
| 308 | |
| 309 | /* Initialize the pmic gang if it hasn't been initialized already */ |
| 310 | if (the_gang == NULL) { |
| 311 | rc = pvreg_init(pdev); |
| 312 | if (rc < 0) { |
| 313 | dev_err(&pdev->dev, |
| 314 | "failed to init pmic gang rc = %d\n", rc); |
| 315 | return rc; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | if (pdev->dev.of_node) { |
| 320 | /* Get init_data from device tree. */ |
| 321 | init_data = of_get_regulator_init_data(&pdev->dev, |
| 322 | pdev->dev.of_node); |
| 323 | init_data->constraints.valid_ops_mask |
| 324 | |= REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_DRMS |
| 325 | | REGULATOR_CHANGE_MODE; |
| 326 | init_data->constraints.valid_modes_mask |
| 327 | |= REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE |
| 328 | | REGULATOR_MODE_FAST; |
| 329 | init_data->constraints.input_uV = init_data->constraints.max_uV; |
| 330 | } |
| 331 | |
| 332 | if (!init_data) { |
| 333 | dev_err(&pdev->dev, "init data required.\n"); |
| 334 | return -EINVAL; |
| 335 | } |
| 336 | |
| 337 | if (!init_data->constraints.name) { |
| 338 | dev_err(&pdev->dev, |
| 339 | "regulator name must be specified in constraints.\n"); |
| 340 | return -EINVAL; |
| 341 | } |
| 342 | |
| 343 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 344 | if (!res) { |
| 345 | dev_err(&pdev->dev, "missing physical register addresses\n"); |
| 346 | return -EINVAL; |
| 347 | } |
| 348 | |
| 349 | kvreg = devm_kzalloc(&pdev->dev, |
| 350 | sizeof(struct krait_power_vreg), GFP_KERNEL); |
| 351 | if (!kvreg) { |
| 352 | dev_err(&pdev->dev, "kzalloc failed.\n"); |
| 353 | return -ENOMEM; |
| 354 | } |
| 355 | |
| 356 | kvreg->reg_base = devm_ioremap(&pdev->dev, |
| 357 | res->start, resource_size(res)); |
| 358 | |
| 359 | kvreg->pvreg = the_gang; |
| 360 | kvreg->name = init_data->constraints.name; |
| 361 | kvreg->desc.name = kvreg->name; |
| 362 | kvreg->desc.ops = &krait_power_ops; |
| 363 | kvreg->desc.type = REGULATOR_VOLTAGE; |
| 364 | kvreg->desc.owner = THIS_MODULE; |
| 365 | kvreg->uV = CORE_VOLTAGE_MIN; |
| 366 | kvreg->mode = HS_MODE; |
| 367 | kvreg->desc.ops = &krait_power_ops; |
| 368 | |
| 369 | platform_set_drvdata(pdev, kvreg); |
| 370 | |
| 371 | mutex_lock(&the_gang->krait_power_vregs_lock); |
| 372 | list_add_tail(&kvreg->link, &the_gang->krait_power_vregs); |
| 373 | mutex_unlock(&the_gang->krait_power_vregs_lock); |
| 374 | |
| 375 | kvreg->rdev = regulator_register(&kvreg->desc, &pdev->dev, init_data, |
| 376 | kvreg, pdev->dev.of_node); |
| 377 | if (IS_ERR(kvreg->rdev)) { |
| 378 | rc = PTR_ERR(kvreg->rdev); |
| 379 | pr_err("regulator_register failed, rc=%d.\n", rc); |
| 380 | goto out; |
| 381 | } |
| 382 | |
| 383 | dev_dbg(&pdev->dev, "id=%d, name=%s\n", pdev->id, kvreg->name); |
| 384 | |
| 385 | return 0; |
| 386 | out: |
| 387 | mutex_lock(&the_gang->krait_power_vregs_lock); |
| 388 | list_del(&kvreg->link); |
| 389 | mutex_unlock(&the_gang->krait_power_vregs_lock); |
| 390 | |
| 391 | platform_set_drvdata(pdev, NULL); |
| 392 | return rc; |
| 393 | } |
| 394 | |
| 395 | static int __devexit krait_power_remove(struct platform_device *pdev) |
| 396 | { |
| 397 | struct krait_power_vreg *kvreg = platform_get_drvdata(pdev); |
| 398 | struct pmic_gang_vreg *pvreg = kvreg->pvreg; |
| 399 | |
| 400 | mutex_lock(&pvreg->krait_power_vregs_lock); |
| 401 | list_del(&kvreg->link); |
| 402 | mutex_unlock(&pvreg->krait_power_vregs_lock); |
| 403 | |
| 404 | regulator_unregister(kvreg->rdev); |
| 405 | platform_set_drvdata(pdev, NULL); |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | static struct of_device_id krait_power_match_table[] = { |
| 410 | { .compatible = "qcom,krait-regulator", }, |
| 411 | {} |
| 412 | }; |
| 413 | |
| 414 | static struct platform_driver krait_power_driver = { |
| 415 | .probe = krait_power_probe, |
| 416 | .remove = __devexit_p(krait_power_remove), |
| 417 | .driver = { |
| 418 | .name = KRAIT_REGULATOR_DRIVER_NAME, |
| 419 | .of_match_table = krait_power_match_table, |
| 420 | .owner = THIS_MODULE, |
| 421 | }, |
| 422 | }; |
| 423 | |
| 424 | int __init krait_power_init(void) |
| 425 | { |
| 426 | return platform_driver_register(&krait_power_driver); |
| 427 | } |
| 428 | |
| 429 | static void __exit krait_power_exit(void) |
| 430 | { |
| 431 | platform_driver_unregister(&krait_power_driver); |
| 432 | } |
| 433 | |
| 434 | module_exit(krait_power_exit); |
| 435 | |
| 436 | MODULE_LICENSE("GPL v2"); |
| 437 | MODULE_DESCRIPTION("KRAIT POWER regulator driver"); |
| 438 | MODULE_VERSION("1.0"); |
| 439 | MODULE_ALIAS("platform:"KRAIT_REGULATOR_DRIVER_NAME); |