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