Balaji Rao | 5ec271e | 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 | |
Axel Lin | 11bb88e | 2012-04-18 10:50:37 +0800 | [diff] [blame^] | 27 | #define PCF50633_REGULATOR(_name, _id, _n) \ |
| 28 | { \ |
| 29 | .name = _name, \ |
| 30 | .id = PCF50633_REGULATOR_##_id, \ |
| 31 | .ops = &pcf50633_regulator_ops, \ |
| 32 | .n_voltages = _n, \ |
| 33 | .type = REGULATOR_VOLTAGE, \ |
| 34 | .owner = THIS_MODULE, \ |
| 35 | .enable_reg = PCF50633_REG_##_id##OUT + 1, \ |
| 36 | .enable_mask = PCF50633_REGULATOR_ON, \ |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static const u8 pcf50633_regulator_registers[PCF50633_NUM_REGULATORS] = { |
| 40 | [PCF50633_REGULATOR_AUTO] = PCF50633_REG_AUTOOUT, |
| 41 | [PCF50633_REGULATOR_DOWN1] = PCF50633_REG_DOWN1OUT, |
| 42 | [PCF50633_REGULATOR_DOWN2] = PCF50633_REG_DOWN2OUT, |
| 43 | [PCF50633_REGULATOR_MEMLDO] = PCF50633_REG_MEMLDOOUT, |
| 44 | [PCF50633_REGULATOR_LDO1] = PCF50633_REG_LDO1OUT, |
| 45 | [PCF50633_REGULATOR_LDO2] = PCF50633_REG_LDO2OUT, |
| 46 | [PCF50633_REGULATOR_LDO3] = PCF50633_REG_LDO3OUT, |
| 47 | [PCF50633_REGULATOR_LDO4] = PCF50633_REG_LDO4OUT, |
| 48 | [PCF50633_REGULATOR_LDO5] = PCF50633_REG_LDO5OUT, |
| 49 | [PCF50633_REGULATOR_LDO6] = PCF50633_REG_LDO6OUT, |
| 50 | [PCF50633_REGULATOR_HCLDO] = PCF50633_REG_HCLDOOUT, |
| 51 | }; |
| 52 | |
| 53 | /* Bits from voltage value */ |
| 54 | static u8 auto_voltage_bits(unsigned int millivolts) |
| 55 | { |
| 56 | if (millivolts < 1800) |
Axel Lin | 022dcdf | 2012-03-19 10:55:24 +0800 | [diff] [blame] | 57 | return 0x2f; |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 58 | if (millivolts > 3800) |
| 59 | return 0xff; |
| 60 | |
| 61 | millivolts -= 625; |
| 62 | |
| 63 | return millivolts / 25; |
| 64 | } |
| 65 | |
| 66 | static u8 down_voltage_bits(unsigned int millivolts) |
| 67 | { |
| 68 | if (millivolts < 625) |
| 69 | return 0; |
| 70 | else if (millivolts > 3000) |
| 71 | return 0xff; |
| 72 | |
| 73 | millivolts -= 625; |
| 74 | |
| 75 | return millivolts / 25; |
| 76 | } |
| 77 | |
| 78 | static u8 ldo_voltage_bits(unsigned int millivolts) |
| 79 | { |
| 80 | if (millivolts < 900) |
| 81 | return 0; |
| 82 | else if (millivolts > 3600) |
| 83 | return 0x1f; |
| 84 | |
| 85 | millivolts -= 900; |
| 86 | return millivolts / 100; |
| 87 | } |
| 88 | |
| 89 | /* Obtain voltage value from bits */ |
| 90 | static unsigned int auto_voltage_value(u8 bits) |
| 91 | { |
Axel Lin | 022dcdf | 2012-03-19 10:55:24 +0800 | [diff] [blame] | 92 | /* AUTOOUT: 00000000 to 00101110 are reserved. |
| 93 | * Return 0 for bits in reserved range, which means this selector code |
| 94 | * can't be used on this system */ |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 95 | if (bits < 0x2f) |
| 96 | return 0; |
| 97 | |
| 98 | return 625 + (bits * 25); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | static unsigned int down_voltage_value(u8 bits) |
| 103 | { |
| 104 | return 625 + (bits * 25); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static unsigned int ldo_voltage_value(u8 bits) |
| 109 | { |
| 110 | bits &= 0x1f; |
| 111 | |
| 112 | return 900 + (bits * 100); |
| 113 | } |
| 114 | |
| 115 | static int pcf50633_regulator_set_voltage(struct regulator_dev *rdev, |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 116 | int min_uV, int max_uV, |
| 117 | unsigned *selector) |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 118 | { |
| 119 | struct pcf50633 *pcf; |
| 120 | int regulator_id, millivolts; |
| 121 | u8 volt_bits, regnr; |
| 122 | |
| 123 | pcf = rdev_get_drvdata(rdev); |
| 124 | |
| 125 | regulator_id = rdev_get_id(rdev); |
| 126 | if (regulator_id >= PCF50633_NUM_REGULATORS) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | millivolts = min_uV / 1000; |
| 130 | |
| 131 | regnr = pcf50633_regulator_registers[regulator_id]; |
| 132 | |
| 133 | switch (regulator_id) { |
| 134 | case PCF50633_REGULATOR_AUTO: |
| 135 | volt_bits = auto_voltage_bits(millivolts); |
| 136 | break; |
| 137 | case PCF50633_REGULATOR_DOWN1: |
| 138 | volt_bits = down_voltage_bits(millivolts); |
| 139 | break; |
| 140 | case PCF50633_REGULATOR_DOWN2: |
| 141 | volt_bits = down_voltage_bits(millivolts); |
| 142 | break; |
| 143 | case PCF50633_REGULATOR_LDO1: |
| 144 | case PCF50633_REGULATOR_LDO2: |
| 145 | case PCF50633_REGULATOR_LDO3: |
| 146 | case PCF50633_REGULATOR_LDO4: |
| 147 | case PCF50633_REGULATOR_LDO5: |
| 148 | case PCF50633_REGULATOR_LDO6: |
| 149 | case PCF50633_REGULATOR_HCLDO: |
Axel Lin | ba51c6c | 2012-02-29 12:45:35 +0800 | [diff] [blame] | 150 | case PCF50633_REGULATOR_MEMLDO: |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 151 | volt_bits = ldo_voltage_bits(millivolts); |
| 152 | break; |
| 153 | default: |
| 154 | return -EINVAL; |
| 155 | } |
| 156 | |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 157 | *selector = volt_bits; |
| 158 | |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 159 | return pcf50633_reg_write(pcf, regnr, volt_bits); |
| 160 | } |
| 161 | |
Axel Lin | 8300d2f | 2012-03-19 10:57:06 +0800 | [diff] [blame] | 162 | static int pcf50633_regulator_get_voltage_sel(struct regulator_dev *rdev) |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 163 | { |
| 164 | struct pcf50633 *pcf; |
Lars-Peter Clausen | a6576cf | 2009-08-04 02:03:52 +0200 | [diff] [blame] | 165 | int regulator_id; |
Axel Lin | 8300d2f | 2012-03-19 10:57:06 +0800 | [diff] [blame] | 166 | u8 regnr; |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 167 | |
Joe Perches | 561864e8 | 2009-06-28 09:26:42 -0700 | [diff] [blame] | 168 | pcf = rdev_get_drvdata(rdev); |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 169 | |
| 170 | regulator_id = rdev_get_id(rdev); |
| 171 | if (regulator_id >= PCF50633_NUM_REGULATORS) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | regnr = pcf50633_regulator_registers[regulator_id]; |
| 175 | |
Axel Lin | 8300d2f | 2012-03-19 10:57:06 +0800 | [diff] [blame] | 176 | return pcf50633_reg_read(pcf, regnr); |
Lars-Peter Clausen | a6576cf | 2009-08-04 02:03:52 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static int pcf50633_regulator_list_voltage(struct regulator_dev *rdev, |
| 180 | unsigned int index) |
| 181 | { |
Axel Lin | 022dcdf | 2012-03-19 10:55:24 +0800 | [diff] [blame] | 182 | int regulator_id = rdev_get_id(rdev); |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 183 | |
Axel Lin | 8300d2f | 2012-03-19 10:57:06 +0800 | [diff] [blame] | 184 | int millivolts; |
| 185 | |
| 186 | switch (regulator_id) { |
| 187 | case PCF50633_REGULATOR_AUTO: |
| 188 | millivolts = auto_voltage_value(index); |
| 189 | break; |
| 190 | case PCF50633_REGULATOR_DOWN1: |
| 191 | millivolts = down_voltage_value(index); |
| 192 | break; |
| 193 | case PCF50633_REGULATOR_DOWN2: |
| 194 | millivolts = down_voltage_value(index); |
| 195 | break; |
| 196 | case PCF50633_REGULATOR_LDO1: |
| 197 | case PCF50633_REGULATOR_LDO2: |
| 198 | case PCF50633_REGULATOR_LDO3: |
| 199 | case PCF50633_REGULATOR_LDO4: |
| 200 | case PCF50633_REGULATOR_LDO5: |
| 201 | case PCF50633_REGULATOR_LDO6: |
| 202 | case PCF50633_REGULATOR_HCLDO: |
| 203 | case PCF50633_REGULATOR_MEMLDO: |
| 204 | millivolts = ldo_voltage_value(index); |
| 205 | break; |
| 206 | default: |
| 207 | return -EINVAL; |
| 208 | } |
| 209 | |
| 210 | return millivolts * 1000; |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 213 | static struct regulator_ops pcf50633_regulator_ops = { |
| 214 | .set_voltage = pcf50633_regulator_set_voltage, |
Axel Lin | 8300d2f | 2012-03-19 10:57:06 +0800 | [diff] [blame] | 215 | .get_voltage_sel = pcf50633_regulator_get_voltage_sel, |
Lars-Peter Clausen | a6576cf | 2009-08-04 02:03:52 +0200 | [diff] [blame] | 216 | .list_voltage = pcf50633_regulator_list_voltage, |
Axel Lin | 11bb88e | 2012-04-18 10:50:37 +0800 | [diff] [blame^] | 217 | .enable = regulator_enable_regmap, |
| 218 | .disable = regulator_disable_regmap, |
| 219 | .is_enabled = regulator_is_enabled_regmap, |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 220 | }; |
| 221 | |
Axel Lin | 2ac2d7d | 2012-04-05 12:02:36 +0800 | [diff] [blame] | 222 | static const struct regulator_desc regulators[] = { |
Axel Lin | 11bb88e | 2012-04-18 10:50:37 +0800 | [diff] [blame^] | 223 | [PCF50633_REGULATOR_AUTO] = PCF50633_REGULATOR("auto", AUTO, 128), |
| 224 | [PCF50633_REGULATOR_DOWN1] = PCF50633_REGULATOR("down1", DOWN1, 96), |
| 225 | [PCF50633_REGULATOR_DOWN2] = PCF50633_REGULATOR("down2", DOWN2, 96), |
| 226 | [PCF50633_REGULATOR_LDO1] = PCF50633_REGULATOR("ldo1", LDO1, 28), |
| 227 | [PCF50633_REGULATOR_LDO2] = PCF50633_REGULATOR("ldo2", LDO2, 28), |
| 228 | [PCF50633_REGULATOR_LDO3] = PCF50633_REGULATOR("ldo3", LDO3, 28), |
| 229 | [PCF50633_REGULATOR_LDO4] = PCF50633_REGULATOR("ldo4", LDO4, 28), |
| 230 | [PCF50633_REGULATOR_LDO5] = PCF50633_REGULATOR("ldo5", LDO5, 28), |
| 231 | [PCF50633_REGULATOR_LDO6] = PCF50633_REGULATOR("ldo6", LDO6, 28), |
| 232 | [PCF50633_REGULATOR_HCLDO] = PCF50633_REGULATOR("hcldo", HCLDO, 28), |
| 233 | [PCF50633_REGULATOR_MEMLDO] = PCF50633_REGULATOR("memldo", MEMLDO, 28), |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | static int __devinit pcf50633_regulator_probe(struct platform_device *pdev) |
| 237 | { |
| 238 | struct regulator_dev *rdev; |
| 239 | struct pcf50633 *pcf; |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 240 | struct regulator_config config = { }; |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 241 | |
| 242 | /* Already set by core driver */ |
Lars-Peter Clausen | 98c2e49 | 2009-10-14 02:12:36 +0400 | [diff] [blame] | 243 | pcf = dev_to_pcf50633(pdev->dev.parent); |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 244 | |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 245 | config.dev = &pdev->dev; |
| 246 | config.init_data = pdev->dev.platform_data; |
| 247 | config.driver_data = pcf; |
Axel Lin | 11bb88e | 2012-04-18 10:50:37 +0800 | [diff] [blame^] | 248 | config.regmap = pcf->regmap; |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 249 | |
| 250 | rdev = regulator_register(®ulators[pdev->id], &config); |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 251 | if (IS_ERR(rdev)) |
| 252 | return PTR_ERR(rdev); |
| 253 | |
Lars-Peter Clausen | 98c2e49 | 2009-10-14 02:12:36 +0400 | [diff] [blame] | 254 | platform_set_drvdata(pdev, rdev); |
| 255 | |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 256 | if (pcf->pdata->regulator_registered) |
| 257 | pcf->pdata->regulator_registered(pcf, pdev->id); |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static int __devexit pcf50633_regulator_remove(struct platform_device *pdev) |
| 263 | { |
| 264 | struct regulator_dev *rdev = platform_get_drvdata(pdev); |
| 265 | |
Lars-Peter Clausen | 98c2e49 | 2009-10-14 02:12:36 +0400 | [diff] [blame] | 266 | platform_set_drvdata(pdev, NULL); |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 267 | regulator_unregister(rdev); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static struct platform_driver pcf50633_regulator_driver = { |
| 273 | .driver = { |
| 274 | .name = "pcf50633-regltr", |
| 275 | }, |
| 276 | .probe = pcf50633_regulator_probe, |
| 277 | .remove = __devexit_p(pcf50633_regulator_remove), |
| 278 | }; |
| 279 | |
| 280 | static int __init pcf50633_regulator_init(void) |
| 281 | { |
| 282 | return platform_driver_register(&pcf50633_regulator_driver); |
| 283 | } |
Mark Brown | 5a1b22b | 2009-04-27 18:21:18 +0100 | [diff] [blame] | 284 | subsys_initcall(pcf50633_regulator_init); |
Balaji Rao | 5ec271e | 2009-01-09 01:51:01 +0100 | [diff] [blame] | 285 | |
| 286 | static void __exit pcf50633_regulator_exit(void) |
| 287 | { |
| 288 | platform_driver_unregister(&pcf50633_regulator_driver); |
| 289 | } |
| 290 | module_exit(pcf50633_regulator_exit); |
| 291 | |
| 292 | MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>"); |
| 293 | MODULE_DESCRIPTION("PCF50633 regulator driver"); |
| 294 | MODULE_LICENSE("GPL"); |
| 295 | MODULE_ALIAS("platform:pcf50633-regulator"); |