Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Regulator support for WM8400 |
| 3 | * |
| 4 | * Copyright 2008 Wolfson Microelectronics PLC. |
| 5 | * |
| 6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of the |
| 11 | * License, or (at your option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/bug.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/kernel.h> |
Paul Gortmaker | 65602c3 | 2011-07-17 16:28:23 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 19 | #include <linux/regulator/driver.h> |
| 20 | #include <linux/mfd/wm8400-private.h> |
| 21 | |
Mark Brown | 216765d | 2009-03-10 16:28:35 +0000 | [diff] [blame] | 22 | static int wm8400_ldo_list_voltage(struct regulator_dev *dev, |
| 23 | unsigned selector) |
| 24 | { |
| 25 | if (selector > WM8400_LDO1_VSEL_MASK) |
| 26 | return -EINVAL; |
| 27 | |
| 28 | if (selector < 15) |
| 29 | return 900000 + (selector * 50000); |
| 30 | else |
| 31 | return 1600000 + ((selector - 14) * 100000); |
| 32 | } |
| 33 | |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 34 | static int wm8400_ldo_map_voltage(struct regulator_dev *dev, |
| 35 | int min_uV, int max_uV) |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 36 | { |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 37 | u16 val; |
Axel Lin | 38c20eb | 2012-06-12 16:34:55 +0800 | [diff] [blame^] | 38 | int volt; |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 39 | |
| 40 | if (min_uV < 900000 || min_uV > 3300000) |
| 41 | return -EINVAL; |
| 42 | |
Axel Lin | 38c20eb | 2012-06-12 16:34:55 +0800 | [diff] [blame^] | 43 | if (min_uV < 1700000) /* Steps of 50mV from 900mV; */ |
Axel Lin | e9a15c8 | 2012-03-06 09:56:05 +0800 | [diff] [blame] | 44 | val = DIV_ROUND_UP(min_uV - 900000, 50000); |
Axel Lin | 38c20eb | 2012-06-12 16:34:55 +0800 | [diff] [blame^] | 45 | else /* Steps of 100mV from 1700mV */ |
| 46 | val = DIV_ROUND_UP(min_uV - 1700000, 100000) + 15; |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 47 | |
Axel Lin | 38c20eb | 2012-06-12 16:34:55 +0800 | [diff] [blame^] | 48 | volt = wm8400_ldo_list_voltage(dev, val); |
| 49 | if (volt < min_uV || volt > max_uV) |
| 50 | return -EINVAL; |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 51 | |
Mark Brown | 7ce8a2a | 2011-05-08 22:19:09 +0100 | [diff] [blame] | 52 | return val; |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 55 | static struct regulator_ops wm8400_ldo_ops = { |
| 56 | .is_enabled = regulator_is_enabled_regmap, |
| 57 | .enable = regulator_enable_regmap, |
| 58 | .disable = regulator_disable_regmap, |
| 59 | .list_voltage = wm8400_ldo_list_voltage, |
| 60 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 61 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 62 | .map_voltage = wm8400_ldo_map_voltage, |
| 63 | }; |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 64 | |
| 65 | static unsigned int wm8400_dcdc_get_mode(struct regulator_dev *dev) |
| 66 | { |
| 67 | struct wm8400 *wm8400 = rdev_get_drvdata(dev); |
| 68 | int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2; |
| 69 | u16 data[2]; |
| 70 | int ret; |
| 71 | |
| 72 | ret = wm8400_block_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 2, |
| 73 | data); |
| 74 | if (ret != 0) |
| 75 | return 0; |
| 76 | |
| 77 | /* Datasheet: hibernate */ |
| 78 | if (data[0] & WM8400_DC1_SLEEP) |
| 79 | return REGULATOR_MODE_STANDBY; |
| 80 | |
| 81 | /* Datasheet: standby */ |
| 82 | if (!(data[0] & WM8400_DC1_ACTIVE)) |
| 83 | return REGULATOR_MODE_IDLE; |
| 84 | |
| 85 | /* Datasheet: active with or without force PWM */ |
| 86 | if (data[1] & WM8400_DC1_FRC_PWM) |
| 87 | return REGULATOR_MODE_FAST; |
| 88 | else |
| 89 | return REGULATOR_MODE_NORMAL; |
| 90 | } |
| 91 | |
| 92 | static int wm8400_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode) |
| 93 | { |
| 94 | struct wm8400 *wm8400 = rdev_get_drvdata(dev); |
| 95 | int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2; |
| 96 | int ret; |
| 97 | |
| 98 | switch (mode) { |
| 99 | case REGULATOR_MODE_FAST: |
| 100 | /* Datasheet: active with force PWM */ |
| 101 | ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset, |
| 102 | WM8400_DC1_FRC_PWM, WM8400_DC1_FRC_PWM); |
| 103 | if (ret != 0) |
| 104 | return ret; |
| 105 | |
| 106 | return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset, |
| 107 | WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, |
| 108 | WM8400_DC1_ACTIVE); |
| 109 | |
| 110 | case REGULATOR_MODE_NORMAL: |
| 111 | /* Datasheet: active */ |
| 112 | ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset, |
| 113 | WM8400_DC1_FRC_PWM, 0); |
| 114 | if (ret != 0) |
| 115 | return ret; |
| 116 | |
| 117 | return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset, |
| 118 | WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, |
| 119 | WM8400_DC1_ACTIVE); |
| 120 | |
| 121 | case REGULATOR_MODE_IDLE: |
| 122 | /* Datasheet: standby */ |
| 123 | ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset, |
| 124 | WM8400_DC1_ACTIVE, 0); |
| 125 | if (ret != 0) |
| 126 | return ret; |
| 127 | return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset, |
| 128 | WM8400_DC1_SLEEP, 0); |
| 129 | |
| 130 | default: |
| 131 | return -EINVAL; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev *dev, |
| 136 | int input_uV, int output_uV, |
| 137 | int load_uA) |
| 138 | { |
| 139 | return REGULATOR_MODE_NORMAL; |
| 140 | } |
| 141 | |
| 142 | static struct regulator_ops wm8400_dcdc_ops = { |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 143 | .is_enabled = regulator_is_enabled_regmap, |
| 144 | .enable = regulator_enable_regmap, |
| 145 | .disable = regulator_disable_regmap, |
| 146 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | 27eeabb | 2012-05-31 17:40:22 +0800 | [diff] [blame] | 147 | .map_voltage = regulator_map_voltage_linear, |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 148 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 149 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 150 | .get_mode = wm8400_dcdc_get_mode, |
| 151 | .set_mode = wm8400_dcdc_set_mode, |
| 152 | .get_optimum_mode = wm8400_dcdc_get_optimum_mode, |
| 153 | }; |
| 154 | |
| 155 | static struct regulator_desc regulators[] = { |
| 156 | { |
| 157 | .name = "LDO1", |
| 158 | .id = WM8400_LDO1, |
| 159 | .ops = &wm8400_ldo_ops, |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 160 | .enable_reg = WM8400_LDO1_CONTROL, |
| 161 | .enable_mask = WM8400_LDO1_ENA, |
Mark Brown | 216765d | 2009-03-10 16:28:35 +0000 | [diff] [blame] | 162 | .n_voltages = WM8400_LDO1_VSEL_MASK + 1, |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 163 | .vsel_reg = WM8400_LDO1_CONTROL, |
| 164 | .vsel_mask = WM8400_LDO1_VSEL_MASK, |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 165 | .type = REGULATOR_VOLTAGE, |
| 166 | .owner = THIS_MODULE, |
| 167 | }, |
| 168 | { |
| 169 | .name = "LDO2", |
| 170 | .id = WM8400_LDO2, |
| 171 | .ops = &wm8400_ldo_ops, |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 172 | .enable_reg = WM8400_LDO2_CONTROL, |
| 173 | .enable_mask = WM8400_LDO2_ENA, |
Mark Brown | 216765d | 2009-03-10 16:28:35 +0000 | [diff] [blame] | 174 | .n_voltages = WM8400_LDO2_VSEL_MASK + 1, |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 175 | .type = REGULATOR_VOLTAGE, |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 176 | .vsel_reg = WM8400_LDO2_CONTROL, |
| 177 | .vsel_mask = WM8400_LDO2_VSEL_MASK, |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 178 | .owner = THIS_MODULE, |
| 179 | }, |
| 180 | { |
| 181 | .name = "LDO3", |
| 182 | .id = WM8400_LDO3, |
| 183 | .ops = &wm8400_ldo_ops, |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 184 | .enable_reg = WM8400_LDO3_CONTROL, |
| 185 | .enable_mask = WM8400_LDO3_ENA, |
Mark Brown | 216765d | 2009-03-10 16:28:35 +0000 | [diff] [blame] | 186 | .n_voltages = WM8400_LDO3_VSEL_MASK + 1, |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 187 | .vsel_reg = WM8400_LDO3_CONTROL, |
| 188 | .vsel_mask = WM8400_LDO3_VSEL_MASK, |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 189 | .type = REGULATOR_VOLTAGE, |
| 190 | .owner = THIS_MODULE, |
| 191 | }, |
| 192 | { |
| 193 | .name = "LDO4", |
| 194 | .id = WM8400_LDO4, |
| 195 | .ops = &wm8400_ldo_ops, |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 196 | .enable_reg = WM8400_LDO4_CONTROL, |
| 197 | .enable_mask = WM8400_LDO4_ENA, |
Mark Brown | 216765d | 2009-03-10 16:28:35 +0000 | [diff] [blame] | 198 | .n_voltages = WM8400_LDO4_VSEL_MASK + 1, |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 199 | .vsel_reg = WM8400_LDO4_CONTROL, |
| 200 | .vsel_mask = WM8400_LDO4_VSEL_MASK, |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 201 | .type = REGULATOR_VOLTAGE, |
| 202 | .owner = THIS_MODULE, |
| 203 | }, |
| 204 | { |
| 205 | .name = "DCDC1", |
| 206 | .id = WM8400_DCDC1, |
| 207 | .ops = &wm8400_dcdc_ops, |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 208 | .enable_reg = WM8400_DCDC1_CONTROL_1, |
| 209 | .enable_mask = WM8400_DC1_ENA_MASK, |
Mark Brown | 216765d | 2009-03-10 16:28:35 +0000 | [diff] [blame] | 210 | .n_voltages = WM8400_DC1_VSEL_MASK + 1, |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 211 | .vsel_reg = WM8400_DCDC1_CONTROL_1, |
| 212 | .vsel_mask = WM8400_DC1_VSEL_MASK, |
| 213 | .min_uV = 850000, |
| 214 | .uV_step = 25000, |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 215 | .type = REGULATOR_VOLTAGE, |
| 216 | .owner = THIS_MODULE, |
| 217 | }, |
| 218 | { |
| 219 | .name = "DCDC2", |
| 220 | .id = WM8400_DCDC2, |
| 221 | .ops = &wm8400_dcdc_ops, |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 222 | .enable_reg = WM8400_DCDC2_CONTROL_1, |
| 223 | .enable_mask = WM8400_DC1_ENA_MASK, |
Mark Brown | 216765d | 2009-03-10 16:28:35 +0000 | [diff] [blame] | 224 | .n_voltages = WM8400_DC2_VSEL_MASK + 1, |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 225 | .vsel_reg = WM8400_DCDC2_CONTROL_1, |
| 226 | .vsel_mask = WM8400_DC2_VSEL_MASK, |
| 227 | .min_uV = 850000, |
| 228 | .uV_step = 25000, |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 229 | .type = REGULATOR_VOLTAGE, |
| 230 | .owner = THIS_MODULE, |
| 231 | }, |
| 232 | }; |
| 233 | |
Uwe Kleine-König | 5dbdf73 | 2009-01-12 23:25:05 +0100 | [diff] [blame] | 234 | static int __devinit wm8400_regulator_probe(struct platform_device *pdev) |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 235 | { |
Dmitry Torokhov | 1ad02bb | 2010-02-25 01:55:37 -0800 | [diff] [blame] | 236 | struct wm8400 *wm8400 = container_of(pdev, struct wm8400, regulators[pdev->id]); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 237 | struct regulator_config config = { }; |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 238 | struct regulator_dev *rdev; |
| 239 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 240 | config.dev = &pdev->dev; |
| 241 | config.init_data = pdev->dev.platform_data; |
| 242 | config.driver_data = wm8400; |
Mark Brown | c54a155 | 2012-05-10 00:12:09 +0100 | [diff] [blame] | 243 | config.regmap = wm8400->regmap; |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 244 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 245 | rdev = regulator_register(®ulators[pdev->id], &config); |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 246 | if (IS_ERR(rdev)) |
| 247 | return PTR_ERR(rdev); |
| 248 | |
Dmitry Torokhov | 1ad02bb | 2010-02-25 01:55:37 -0800 | [diff] [blame] | 249 | platform_set_drvdata(pdev, rdev); |
| 250 | |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static int __devexit wm8400_regulator_remove(struct platform_device *pdev) |
| 255 | { |
| 256 | struct regulator_dev *rdev = platform_get_drvdata(pdev); |
| 257 | |
Dmitry Torokhov | 1ad02bb | 2010-02-25 01:55:37 -0800 | [diff] [blame] | 258 | platform_set_drvdata(pdev, NULL); |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 259 | regulator_unregister(rdev); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static struct platform_driver wm8400_regulator_driver = { |
| 265 | .driver = { |
| 266 | .name = "wm8400-regulator", |
| 267 | }, |
| 268 | .probe = wm8400_regulator_probe, |
| 269 | .remove = __devexit_p(wm8400_regulator_remove), |
| 270 | }; |
| 271 | |
| 272 | /** |
| 273 | * wm8400_register_regulator - enable software control of a WM8400 regulator |
| 274 | * |
| 275 | * This function enables software control of a WM8400 regulator via |
| 276 | * the regulator API. It is intended to be called from the |
| 277 | * platform_init() callback of the WM8400 MFD driver. |
| 278 | * |
| 279 | * @param dev The WM8400 device to operate on. |
| 280 | * @param reg The regulator to control. |
| 281 | * @param initdata Regulator initdata for the regulator. |
| 282 | */ |
| 283 | int wm8400_register_regulator(struct device *dev, int reg, |
| 284 | struct regulator_init_data *initdata) |
| 285 | { |
Greg Kroah-Hartman | 1909e2f | 2009-04-30 15:21:37 -0700 | [diff] [blame] | 286 | struct wm8400 *wm8400 = dev_get_drvdata(dev); |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 287 | |
| 288 | if (wm8400->regulators[reg].name) |
| 289 | return -EBUSY; |
| 290 | |
| 291 | initdata->driver_data = wm8400; |
| 292 | |
| 293 | wm8400->regulators[reg].name = "wm8400-regulator"; |
| 294 | wm8400->regulators[reg].id = reg; |
| 295 | wm8400->regulators[reg].dev.parent = dev; |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 296 | wm8400->regulators[reg].dev.platform_data = initdata; |
| 297 | |
| 298 | return platform_device_register(&wm8400->regulators[reg]); |
| 299 | } |
| 300 | EXPORT_SYMBOL_GPL(wm8400_register_regulator); |
| 301 | |
| 302 | static int __init wm8400_regulator_init(void) |
| 303 | { |
| 304 | return platform_driver_register(&wm8400_regulator_driver); |
| 305 | } |
Mark Brown | 5a1b22b | 2009-04-27 18:21:18 +0100 | [diff] [blame] | 306 | subsys_initcall(wm8400_regulator_init); |
Mark Brown | 42fad57 | 2008-09-11 11:12:01 +0100 | [diff] [blame] | 307 | |
| 308 | static void __exit wm8400_regulator_exit(void) |
| 309 | { |
| 310 | platform_driver_unregister(&wm8400_regulator_driver); |
| 311 | } |
| 312 | module_exit(wm8400_regulator_exit); |
| 313 | |
| 314 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 315 | MODULE_DESCRIPTION("WM8400 regulator driver"); |
| 316 | MODULE_LICENSE("GPL"); |
| 317 | MODULE_ALIAS("platform:wm8400-regulator"); |