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