Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010-2011, 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> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/regulator/driver.h> |
| 24 | #include <linux/regulator/machine.h> |
| 25 | #include <linux/mfd/pmic8901.h> |
| 26 | |
| 27 | #include "spm.h" |
| 28 | |
| 29 | #define FTSMPS_VCTRL_BAND_MASK 0xC0 |
| 30 | #define FTSMPS_VCTRL_BAND_1 0x40 |
| 31 | #define FTSMPS_VCTRL_BAND_2 0x80 |
| 32 | #define FTSMPS_VCTRL_BAND_3 0xC0 |
| 33 | #define FTSMPS_VCTRL_VPROG_MASK 0x3F |
| 34 | |
| 35 | #define FTSMPS_BAND1_UV_MIN 350000 |
| 36 | #define FTSMPS_BAND1_UV_MAX 650000 |
| 37 | /* 3 LSB's of program voltage must be 0 in band 1. */ |
| 38 | /* Logical step size */ |
| 39 | #define FTSMPS_BAND1_UV_LOG_STEP 50000 |
| 40 | /* Physical step size */ |
| 41 | #define FTSMPS_BAND1_UV_PHYS_STEP 6250 |
| 42 | |
| 43 | #define FTSMPS_BAND2_UV_MIN 700000 |
| 44 | #define FTSMPS_BAND2_UV_MAX 1400000 |
| 45 | #define FTSMPS_BAND2_UV_STEP 12500 |
| 46 | |
| 47 | #define FTSMPS_BAND3_UV_MIN 1400000 |
| 48 | #define FTSMPS_BAND3_UV_SET_POINT_MIN 1500000 |
| 49 | #define FTSMPS_BAND3_UV_MAX 3300000 |
| 50 | #define FTSMPS_BAND3_UV_STEP 50000 |
| 51 | |
| 52 | struct saw_vreg { |
| 53 | struct regulator_desc desc; |
| 54 | struct regulator_dev *rdev; |
| 55 | char *name; |
| 56 | int uV; |
| 57 | }; |
| 58 | |
| 59 | /* Minimum core operating voltage */ |
| 60 | #define MIN_CORE_VOLTAGE 950000 |
| 61 | |
| 62 | /* Specifies the PMIC internal slew rate in uV/us. */ |
| 63 | #define REGULATOR_SLEW_RATE 1250 |
| 64 | |
| 65 | static int saw_get_voltage(struct regulator_dev *rdev) |
| 66 | { |
| 67 | struct saw_vreg *vreg = rdev_get_drvdata(rdev); |
| 68 | |
| 69 | return vreg->uV; |
| 70 | } |
| 71 | |
| 72 | static int saw_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV, |
| 73 | unsigned *selector) |
| 74 | { |
| 75 | struct saw_vreg *vreg = rdev_get_drvdata(rdev); |
| 76 | int uV = min_uV; |
| 77 | int rc; |
| 78 | u8 vprog, band; |
| 79 | |
| 80 | if (uV < FTSMPS_BAND1_UV_MIN && max_uV >= FTSMPS_BAND1_UV_MIN) |
| 81 | uV = FTSMPS_BAND1_UV_MIN; |
| 82 | |
| 83 | if (uV < FTSMPS_BAND1_UV_MIN || uV > FTSMPS_BAND3_UV_MAX) { |
| 84 | pr_err("%s: request v=[%d, %d] is outside possible " |
| 85 | "v=[%d, %d]\n", vreg->name, min_uV, max_uV, |
| 86 | FTSMPS_BAND1_UV_MIN, FTSMPS_BAND3_UV_MAX); |
| 87 | return -EINVAL; |
| 88 | } |
| 89 | |
| 90 | /* Round up for set points in the gaps between bands. */ |
| 91 | if (uV > FTSMPS_BAND1_UV_MAX && uV < FTSMPS_BAND2_UV_MIN) |
| 92 | uV = FTSMPS_BAND2_UV_MIN; |
| 93 | else if (uV > FTSMPS_BAND2_UV_MAX |
| 94 | && uV < FTSMPS_BAND3_UV_SET_POINT_MIN) |
| 95 | uV = FTSMPS_BAND3_UV_SET_POINT_MIN; |
| 96 | |
| 97 | if (uV > FTSMPS_BAND2_UV_MAX) { |
| 98 | vprog = (uV - FTSMPS_BAND3_UV_MIN + FTSMPS_BAND3_UV_STEP - 1) |
| 99 | / FTSMPS_BAND3_UV_STEP; |
| 100 | band = FTSMPS_VCTRL_BAND_3; |
| 101 | uV = FTSMPS_BAND3_UV_MIN + vprog * FTSMPS_BAND3_UV_STEP; |
| 102 | } else if (uV > FTSMPS_BAND1_UV_MAX) { |
| 103 | vprog = (uV - FTSMPS_BAND2_UV_MIN + FTSMPS_BAND2_UV_STEP - 1) |
| 104 | / FTSMPS_BAND2_UV_STEP; |
| 105 | band = FTSMPS_VCTRL_BAND_2; |
| 106 | uV = FTSMPS_BAND2_UV_MIN + vprog * FTSMPS_BAND2_UV_STEP; |
| 107 | } else { |
| 108 | vprog = (uV - FTSMPS_BAND1_UV_MIN |
| 109 | + FTSMPS_BAND1_UV_LOG_STEP - 1) |
| 110 | / FTSMPS_BAND1_UV_LOG_STEP; |
| 111 | uV = FTSMPS_BAND1_UV_MIN + vprog * FTSMPS_BAND1_UV_LOG_STEP; |
| 112 | vprog *= FTSMPS_BAND1_UV_LOG_STEP / FTSMPS_BAND1_UV_PHYS_STEP; |
| 113 | band = FTSMPS_VCTRL_BAND_1; |
| 114 | } |
| 115 | |
| 116 | if (uV > max_uV) { |
| 117 | pr_err("%s: request v=[%d, %d] cannot be met by any setpoint\n", |
| 118 | vreg->name, min_uV, max_uV); |
| 119 | return -EINVAL; |
| 120 | } |
| 121 | |
| 122 | rc = msm_spm_set_vdd(rdev_get_id(rdev), band | vprog); |
| 123 | if (!rc) { |
| 124 | if (uV > vreg->uV) { |
| 125 | /* Wait for voltage to stabalize. */ |
| 126 | udelay((uV - vreg->uV) / REGULATOR_SLEW_RATE); |
| 127 | } |
| 128 | vreg->uV = uV; |
| 129 | } else { |
| 130 | pr_err("%s: msm_spm_set_vdd failed %d\n", vreg->name, rc); |
| 131 | } |
| 132 | |
| 133 | return rc; |
| 134 | } |
| 135 | |
| 136 | static struct regulator_ops saw_ops = { |
| 137 | .get_voltage = saw_get_voltage, |
| 138 | .set_voltage = saw_set_voltage, |
| 139 | }; |
| 140 | |
| 141 | static int __devinit saw_probe(struct platform_device *pdev) |
| 142 | { |
| 143 | struct regulator_init_data *init_data; |
| 144 | struct saw_vreg *vreg; |
| 145 | int rc = 0; |
| 146 | |
| 147 | if (!pdev->dev.platform_data) { |
| 148 | pr_err("platform data required.\n"); |
| 149 | return -EINVAL; |
| 150 | } |
| 151 | |
| 152 | init_data = pdev->dev.platform_data; |
| 153 | if (!init_data->constraints.name) { |
| 154 | pr_err("regulator name must be specified in constraints.\n"); |
| 155 | return -EINVAL; |
| 156 | } |
| 157 | |
| 158 | vreg = kzalloc(sizeof(struct saw_vreg), GFP_KERNEL); |
| 159 | if (!vreg) { |
| 160 | pr_err("kzalloc failed.\n"); |
| 161 | return -ENOMEM; |
| 162 | } |
| 163 | |
| 164 | vreg->name = kstrdup(init_data->constraints.name, GFP_KERNEL); |
| 165 | if (!vreg->name) { |
| 166 | pr_err("kzalloc failed.\n"); |
| 167 | rc = -ENOMEM; |
| 168 | goto free_vreg; |
| 169 | } |
| 170 | |
| 171 | vreg->desc.name = vreg->name; |
| 172 | vreg->desc.id = pdev->id; |
| 173 | vreg->desc.ops = &saw_ops; |
| 174 | vreg->desc.type = REGULATOR_VOLTAGE; |
| 175 | vreg->desc.owner = THIS_MODULE; |
| 176 | vreg->uV = MIN_CORE_VOLTAGE; |
| 177 | |
Rajendra Nayak | 11eafc6 | 2011-11-18 16:47:19 +0530 | [diff] [blame] | 178 | vreg->rdev = regulator_register(&vreg->desc, &pdev->dev, |
| 179 | init_data, vreg, NULL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 180 | if (IS_ERR(vreg->rdev)) { |
| 181 | rc = PTR_ERR(vreg->rdev); |
| 182 | pr_err("regulator_register failed, rc=%d.\n", rc); |
| 183 | goto free_name; |
| 184 | } |
| 185 | |
| 186 | platform_set_drvdata(pdev, vreg); |
| 187 | |
| 188 | pr_info("id=%d, name=%s\n", pdev->id, vreg->name); |
| 189 | |
| 190 | return rc; |
| 191 | |
| 192 | free_name: |
| 193 | kfree(vreg->name); |
| 194 | free_vreg: |
| 195 | kfree(vreg); |
| 196 | |
| 197 | return rc; |
| 198 | } |
| 199 | |
| 200 | static int __devexit saw_remove(struct platform_device *pdev) |
| 201 | { |
| 202 | struct saw_vreg *vreg = platform_get_drvdata(pdev); |
| 203 | |
| 204 | regulator_unregister(vreg->rdev); |
| 205 | kfree(vreg->name); |
| 206 | kfree(vreg); |
| 207 | platform_set_drvdata(pdev, NULL); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static struct platform_driver saw_driver = { |
| 213 | .probe = saw_probe, |
| 214 | .remove = __devexit_p(saw_remove), |
| 215 | .driver = { |
| 216 | .name = "saw-regulator", |
| 217 | .owner = THIS_MODULE, |
| 218 | }, |
| 219 | }; |
| 220 | |
| 221 | static int __init saw_init(void) |
| 222 | { |
| 223 | return platform_driver_register(&saw_driver); |
| 224 | } |
| 225 | |
| 226 | static void __exit saw_exit(void) |
| 227 | { |
| 228 | platform_driver_unregister(&saw_driver); |
| 229 | } |
| 230 | |
| 231 | postcore_initcall(saw_init); |
| 232 | module_exit(saw_exit); |
| 233 | |
| 234 | MODULE_LICENSE("GPL v2"); |
| 235 | MODULE_DESCRIPTION("SAW regulator driver"); |
| 236 | MODULE_VERSION("1.0"); |
| 237 | MODULE_ALIAS("platform:saw-regulator"); |