Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 1 | /* NXP PCF50633 PMIC Driver |
| 2 | * |
| 3 | * (C) 2006-2008 by Openmoko, Inc. |
| 4 | * Author: Balaji Rao <balajirrao@openmoko.org> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Broken down from monstrous PCF50633 driver mainly by |
| 8 | * Harald Welte and Andy Green and Werner Almesberger |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the |
| 12 | * Free Software Foundation; either version 2 of the License, or (at your |
| 13 | * option) any later version. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/device.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | |
| 24 | #include <linux/mfd/pcf50633/core.h> |
| 25 | #include <linux/mfd/pcf50633/pmic.h> |
| 26 | |
Lars-Peter Clausen | a6576cf | 2009-08-04 02:03:52 +0200 | [diff] [blame] | 27 | #define PCF50633_REGULATOR(_name, _id, _n) \ |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 28 | { \ |
| 29 | .name = _name, \ |
| 30 | .id = _id, \ |
| 31 | .ops = &pcf50633_regulator_ops, \ |
Lars-Peter Clausen | a6576cf | 2009-08-04 02:03:52 +0200 | [diff] [blame] | 32 | .n_voltages = _n, \ |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 33 | .type = REGULATOR_VOLTAGE, \ |
| 34 | .owner = THIS_MODULE, \ |
| 35 | } |
| 36 | |
| 37 | static const u8 pcf50633_regulator_registers[PCF50633_NUM_REGULATORS] = { |
| 38 | [PCF50633_REGULATOR_AUTO] = PCF50633_REG_AUTOOUT, |
| 39 | [PCF50633_REGULATOR_DOWN1] = PCF50633_REG_DOWN1OUT, |
| 40 | [PCF50633_REGULATOR_DOWN2] = PCF50633_REG_DOWN2OUT, |
| 41 | [PCF50633_REGULATOR_MEMLDO] = PCF50633_REG_MEMLDOOUT, |
| 42 | [PCF50633_REGULATOR_LDO1] = PCF50633_REG_LDO1OUT, |
| 43 | [PCF50633_REGULATOR_LDO2] = PCF50633_REG_LDO2OUT, |
| 44 | [PCF50633_REGULATOR_LDO3] = PCF50633_REG_LDO3OUT, |
| 45 | [PCF50633_REGULATOR_LDO4] = PCF50633_REG_LDO4OUT, |
| 46 | [PCF50633_REGULATOR_LDO5] = PCF50633_REG_LDO5OUT, |
| 47 | [PCF50633_REGULATOR_LDO6] = PCF50633_REG_LDO6OUT, |
| 48 | [PCF50633_REGULATOR_HCLDO] = PCF50633_REG_HCLDOOUT, |
| 49 | }; |
| 50 | |
| 51 | /* Bits from voltage value */ |
| 52 | static u8 auto_voltage_bits(unsigned int millivolts) |
| 53 | { |
| 54 | if (millivolts < 1800) |
| 55 | return 0; |
| 56 | if (millivolts > 3800) |
| 57 | return 0xff; |
| 58 | |
| 59 | millivolts -= 625; |
| 60 | |
| 61 | return millivolts / 25; |
| 62 | } |
| 63 | |
| 64 | static u8 down_voltage_bits(unsigned int millivolts) |
| 65 | { |
| 66 | if (millivolts < 625) |
| 67 | return 0; |
| 68 | else if (millivolts > 3000) |
| 69 | return 0xff; |
| 70 | |
| 71 | millivolts -= 625; |
| 72 | |
| 73 | return millivolts / 25; |
| 74 | } |
| 75 | |
| 76 | static u8 ldo_voltage_bits(unsigned int millivolts) |
| 77 | { |
| 78 | if (millivolts < 900) |
| 79 | return 0; |
| 80 | else if (millivolts > 3600) |
| 81 | return 0x1f; |
| 82 | |
| 83 | millivolts -= 900; |
| 84 | return millivolts / 100; |
| 85 | } |
| 86 | |
| 87 | /* Obtain voltage value from bits */ |
| 88 | static unsigned int auto_voltage_value(u8 bits) |
| 89 | { |
| 90 | if (bits < 0x2f) |
| 91 | return 0; |
| 92 | |
| 93 | return 625 + (bits * 25); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static unsigned int down_voltage_value(u8 bits) |
| 98 | { |
| 99 | return 625 + (bits * 25); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | static unsigned int ldo_voltage_value(u8 bits) |
| 104 | { |
| 105 | bits &= 0x1f; |
| 106 | |
| 107 | return 900 + (bits * 100); |
| 108 | } |
| 109 | |
| 110 | static int pcf50633_regulator_set_voltage(struct regulator_dev *rdev, |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 111 | int min_uV, int max_uV, |
| 112 | unsigned *selector) |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 113 | { |
| 114 | struct pcf50633 *pcf; |
| 115 | int regulator_id, millivolts; |
| 116 | u8 volt_bits, regnr; |
| 117 | |
| 118 | pcf = rdev_get_drvdata(rdev); |
| 119 | |
| 120 | regulator_id = rdev_get_id(rdev); |
| 121 | if (regulator_id >= PCF50633_NUM_REGULATORS) |
| 122 | return -EINVAL; |
| 123 | |
| 124 | millivolts = min_uV / 1000; |
| 125 | |
| 126 | regnr = pcf50633_regulator_registers[regulator_id]; |
| 127 | |
| 128 | switch (regulator_id) { |
| 129 | case PCF50633_REGULATOR_AUTO: |
| 130 | volt_bits = auto_voltage_bits(millivolts); |
| 131 | break; |
| 132 | case PCF50633_REGULATOR_DOWN1: |
| 133 | volt_bits = down_voltage_bits(millivolts); |
| 134 | break; |
| 135 | case PCF50633_REGULATOR_DOWN2: |
| 136 | volt_bits = down_voltage_bits(millivolts); |
| 137 | break; |
| 138 | case PCF50633_REGULATOR_LDO1: |
| 139 | case PCF50633_REGULATOR_LDO2: |
| 140 | case PCF50633_REGULATOR_LDO3: |
| 141 | case PCF50633_REGULATOR_LDO4: |
| 142 | case PCF50633_REGULATOR_LDO5: |
| 143 | case PCF50633_REGULATOR_LDO6: |
| 144 | case PCF50633_REGULATOR_HCLDO: |
Axel Lin | ba51c6c | 2012-02-29 12:45:35 +0800 | [diff] [blame] | 145 | case PCF50633_REGULATOR_MEMLDO: |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 146 | volt_bits = ldo_voltage_bits(millivolts); |
| 147 | break; |
| 148 | default: |
| 149 | return -EINVAL; |
| 150 | } |
| 151 | |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 152 | *selector = volt_bits; |
| 153 | |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 154 | return pcf50633_reg_write(pcf, regnr, volt_bits); |
| 155 | } |
| 156 | |
Lars-Peter Clausen | a6576cf | 2009-08-04 02:03:52 +0200 | [diff] [blame] | 157 | static int pcf50633_regulator_voltage_value(enum pcf50633_regulator_id id, |
| 158 | u8 bits) |
| 159 | { |
| 160 | int millivolts; |
| 161 | |
| 162 | switch (id) { |
| 163 | case PCF50633_REGULATOR_AUTO: |
| 164 | millivolts = auto_voltage_value(bits); |
| 165 | break; |
| 166 | case PCF50633_REGULATOR_DOWN1: |
| 167 | millivolts = down_voltage_value(bits); |
| 168 | break; |
| 169 | case PCF50633_REGULATOR_DOWN2: |
| 170 | millivolts = down_voltage_value(bits); |
| 171 | break; |
| 172 | case PCF50633_REGULATOR_LDO1: |
| 173 | case PCF50633_REGULATOR_LDO2: |
| 174 | case PCF50633_REGULATOR_LDO3: |
| 175 | case PCF50633_REGULATOR_LDO4: |
| 176 | case PCF50633_REGULATOR_LDO5: |
| 177 | case PCF50633_REGULATOR_LDO6: |
| 178 | case PCF50633_REGULATOR_HCLDO: |
Axel Lin | ba51c6c | 2012-02-29 12:45:35 +0800 | [diff] [blame] | 179 | case PCF50633_REGULATOR_MEMLDO: |
Lars-Peter Clausen | a6576cf | 2009-08-04 02:03:52 +0200 | [diff] [blame] | 180 | millivolts = ldo_voltage_value(bits); |
| 181 | break; |
| 182 | default: |
| 183 | return -EINVAL; |
| 184 | } |
| 185 | |
| 186 | return millivolts * 1000; |
| 187 | } |
| 188 | |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 189 | static int pcf50633_regulator_get_voltage(struct regulator_dev *rdev) |
| 190 | { |
| 191 | struct pcf50633 *pcf; |
Lars-Peter Clausen | a6576cf | 2009-08-04 02:03:52 +0200 | [diff] [blame] | 192 | int regulator_id; |
| 193 | u8 volt_bits, regnr; |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 194 | |
Joe Perches | 561864e8 | 2009-06-28 09:26:42 -0700 | [diff] [blame] | 195 | pcf = rdev_get_drvdata(rdev); |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 196 | |
| 197 | regulator_id = rdev_get_id(rdev); |
| 198 | if (regulator_id >= PCF50633_NUM_REGULATORS) |
| 199 | return -EINVAL; |
| 200 | |
| 201 | regnr = pcf50633_regulator_registers[regulator_id]; |
| 202 | |
| 203 | volt_bits = pcf50633_reg_read(pcf, regnr); |
Lars-Peter Clausen | a6576cf | 2009-08-04 02:03:52 +0200 | [diff] [blame] | 204 | |
| 205 | return pcf50633_regulator_voltage_value(regulator_id, volt_bits); |
| 206 | } |
| 207 | |
| 208 | static int pcf50633_regulator_list_voltage(struct regulator_dev *rdev, |
| 209 | unsigned int index) |
| 210 | { |
| 211 | struct pcf50633 *pcf; |
| 212 | int regulator_id; |
| 213 | |
| 214 | pcf = rdev_get_drvdata(rdev); |
| 215 | |
| 216 | regulator_id = rdev_get_id(rdev); |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 217 | |
| 218 | switch (regulator_id) { |
| 219 | case PCF50633_REGULATOR_AUTO: |
Lars-Peter Clausen | a6576cf | 2009-08-04 02:03:52 +0200 | [diff] [blame] | 220 | index += 0x2f; |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 221 | break; |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 222 | default: |
Lars-Peter Clausen | a6576cf | 2009-08-04 02:03:52 +0200 | [diff] [blame] | 223 | break; |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Lars-Peter Clausen | a6576cf | 2009-08-04 02:03:52 +0200 | [diff] [blame] | 226 | return pcf50633_regulator_voltage_value(regulator_id, index); |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static int pcf50633_regulator_enable(struct regulator_dev *rdev) |
| 230 | { |
| 231 | struct pcf50633 *pcf = rdev_get_drvdata(rdev); |
| 232 | int regulator_id; |
| 233 | u8 regnr; |
| 234 | |
| 235 | regulator_id = rdev_get_id(rdev); |
| 236 | if (regulator_id >= PCF50633_NUM_REGULATORS) |
| 237 | return -EINVAL; |
| 238 | |
| 239 | /* The *ENA register is always one after the *OUT register */ |
| 240 | regnr = pcf50633_regulator_registers[regulator_id] + 1; |
| 241 | |
| 242 | return pcf50633_reg_set_bit_mask(pcf, regnr, PCF50633_REGULATOR_ON, |
| 243 | PCF50633_REGULATOR_ON); |
| 244 | } |
| 245 | |
| 246 | static int pcf50633_regulator_disable(struct regulator_dev *rdev) |
| 247 | { |
| 248 | struct pcf50633 *pcf = rdev_get_drvdata(rdev); |
| 249 | int regulator_id; |
| 250 | u8 regnr; |
| 251 | |
| 252 | regulator_id = rdev_get_id(rdev); |
| 253 | if (regulator_id >= PCF50633_NUM_REGULATORS) |
| 254 | return -EINVAL; |
| 255 | |
| 256 | /* the *ENA register is always one after the *OUT register */ |
| 257 | regnr = pcf50633_regulator_registers[regulator_id] + 1; |
| 258 | |
| 259 | return pcf50633_reg_set_bit_mask(pcf, regnr, |
| 260 | PCF50633_REGULATOR_ON, 0); |
| 261 | } |
| 262 | |
| 263 | static int pcf50633_regulator_is_enabled(struct regulator_dev *rdev) |
| 264 | { |
| 265 | struct pcf50633 *pcf = rdev_get_drvdata(rdev); |
| 266 | int regulator_id = rdev_get_id(rdev); |
| 267 | u8 regnr; |
| 268 | |
| 269 | regulator_id = rdev_get_id(rdev); |
| 270 | if (regulator_id >= PCF50633_NUM_REGULATORS) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | /* the *ENA register is always one after the *OUT register */ |
| 274 | regnr = pcf50633_regulator_registers[regulator_id] + 1; |
| 275 | |
| 276 | return pcf50633_reg_read(pcf, regnr) & PCF50633_REGULATOR_ON; |
| 277 | } |
| 278 | |
| 279 | static struct regulator_ops pcf50633_regulator_ops = { |
| 280 | .set_voltage = pcf50633_regulator_set_voltage, |
| 281 | .get_voltage = pcf50633_regulator_get_voltage, |
Lars-Peter Clausen | a6576cf | 2009-08-04 02:03:52 +0200 | [diff] [blame] | 282 | .list_voltage = pcf50633_regulator_list_voltage, |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 283 | .enable = pcf50633_regulator_enable, |
| 284 | .disable = pcf50633_regulator_disable, |
| 285 | .is_enabled = pcf50633_regulator_is_enabled, |
| 286 | }; |
| 287 | |
| 288 | static struct regulator_desc regulators[] = { |
| 289 | [PCF50633_REGULATOR_AUTO] = |
Axel Lin | ba51c6c | 2012-02-29 12:45:35 +0800 | [diff] [blame] | 290 | PCF50633_REGULATOR("auto", PCF50633_REGULATOR_AUTO, 81), |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 291 | [PCF50633_REGULATOR_DOWN1] = |
Axel Lin | ba51c6c | 2012-02-29 12:45:35 +0800 | [diff] [blame] | 292 | PCF50633_REGULATOR("down1", PCF50633_REGULATOR_DOWN1, 96), |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 293 | [PCF50633_REGULATOR_DOWN2] = |
Axel Lin | ba51c6c | 2012-02-29 12:45:35 +0800 | [diff] [blame] | 294 | PCF50633_REGULATOR("down2", PCF50633_REGULATOR_DOWN2, 96), |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 295 | [PCF50633_REGULATOR_LDO1] = |
Axel Lin | ba51c6c | 2012-02-29 12:45:35 +0800 | [diff] [blame] | 296 | PCF50633_REGULATOR("ldo1", PCF50633_REGULATOR_LDO1, 28), |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 297 | [PCF50633_REGULATOR_LDO2] = |
Axel Lin | ba51c6c | 2012-02-29 12:45:35 +0800 | [diff] [blame] | 298 | PCF50633_REGULATOR("ldo2", PCF50633_REGULATOR_LDO2, 28), |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 299 | [PCF50633_REGULATOR_LDO3] = |
Axel Lin | ba51c6c | 2012-02-29 12:45:35 +0800 | [diff] [blame] | 300 | PCF50633_REGULATOR("ldo3", PCF50633_REGULATOR_LDO3, 28), |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 301 | [PCF50633_REGULATOR_LDO4] = |
Axel Lin | ba51c6c | 2012-02-29 12:45:35 +0800 | [diff] [blame] | 302 | PCF50633_REGULATOR("ldo4", PCF50633_REGULATOR_LDO4, 28), |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 303 | [PCF50633_REGULATOR_LDO5] = |
Axel Lin | ba51c6c | 2012-02-29 12:45:35 +0800 | [diff] [blame] | 304 | PCF50633_REGULATOR("ldo5", PCF50633_REGULATOR_LDO5, 28), |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 305 | [PCF50633_REGULATOR_LDO6] = |
Axel Lin | ba51c6c | 2012-02-29 12:45:35 +0800 | [diff] [blame] | 306 | PCF50633_REGULATOR("ldo6", PCF50633_REGULATOR_LDO6, 28), |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 307 | [PCF50633_REGULATOR_HCLDO] = |
Axel Lin | ba51c6c | 2012-02-29 12:45:35 +0800 | [diff] [blame] | 308 | PCF50633_REGULATOR("hcldo", PCF50633_REGULATOR_HCLDO, 28), |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 309 | [PCF50633_REGULATOR_MEMLDO] = |
Axel Lin | ba51c6c | 2012-02-29 12:45:35 +0800 | [diff] [blame] | 310 | PCF50633_REGULATOR("memldo", PCF50633_REGULATOR_MEMLDO, 28), |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | static int __devinit pcf50633_regulator_probe(struct platform_device *pdev) |
| 314 | { |
| 315 | struct regulator_dev *rdev; |
| 316 | struct pcf50633 *pcf; |
| 317 | |
| 318 | /* Already set by core driver */ |
Lars-Peter Clausen | 98c2e49 | 2009-10-14 02:12:36 +0400 | [diff] [blame] | 319 | pcf = dev_to_pcf50633(pdev->dev.parent); |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 320 | |
Mark Brown | 0527100 | 2009-01-19 13:37:02 +0000 | [diff] [blame] | 321 | rdev = regulator_register(®ulators[pdev->id], &pdev->dev, |
Rajendra Nayak | 2c043bc | 2011-11-18 16:47:19 +0530 | [diff] [blame] | 322 | pdev->dev.platform_data, pcf, NULL); |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 323 | if (IS_ERR(rdev)) |
| 324 | return PTR_ERR(rdev); |
| 325 | |
Lars-Peter Clausen | 98c2e49 | 2009-10-14 02:12:36 +0400 | [diff] [blame] | 326 | platform_set_drvdata(pdev, rdev); |
| 327 | |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 328 | if (pcf->pdata->regulator_registered) |
| 329 | pcf->pdata->regulator_registered(pcf, pdev->id); |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static int __devexit pcf50633_regulator_remove(struct platform_device *pdev) |
| 335 | { |
| 336 | struct regulator_dev *rdev = platform_get_drvdata(pdev); |
| 337 | |
Lars-Peter Clausen | 98c2e49 | 2009-10-14 02:12:36 +0400 | [diff] [blame] | 338 | platform_set_drvdata(pdev, NULL); |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 339 | regulator_unregister(rdev); |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static struct platform_driver pcf50633_regulator_driver = { |
| 345 | .driver = { |
| 346 | .name = "pcf50633-regltr", |
| 347 | }, |
| 348 | .probe = pcf50633_regulator_probe, |
| 349 | .remove = __devexit_p(pcf50633_regulator_remove), |
| 350 | }; |
| 351 | |
| 352 | static int __init pcf50633_regulator_init(void) |
| 353 | { |
| 354 | return platform_driver_register(&pcf50633_regulator_driver); |
| 355 | } |
Mark Brown | 5a1b22b | 2009-04-27 18:21:18 +0100 | [diff] [blame] | 356 | subsys_initcall(pcf50633_regulator_init); |
Balaji Rao | 5ec271e7 | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 357 | |
| 358 | static void __exit pcf50633_regulator_exit(void) |
| 359 | { |
| 360 | platform_driver_unregister(&pcf50633_regulator_driver); |
| 361 | } |
| 362 | module_exit(pcf50633_regulator_exit); |
| 363 | |
| 364 | MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>"); |
| 365 | MODULE_DESCRIPTION("PCF50633 regulator driver"); |
| 366 | MODULE_LICENSE("GPL"); |
| 367 | MODULE_ALIAS("platform:pcf50633-regulator"); |