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