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