Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * arizona-ldo1.c -- LDO1 supply for Arizona devices |
| 3 | * |
| 4 | * Copyright 2012 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 modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/moduleparam.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/bitops.h> |
| 18 | #include <linux/err.h> |
Arnd Bergmann | 7b22b9a | 2014-05-27 17:27:34 +0100 | [diff] [blame] | 19 | #include <linux/of.h> |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/regulator/driver.h> |
| 22 | #include <linux/regulator/machine.h> |
Charles Keepax | 2cce4be | 2014-04-16 10:01:39 +0100 | [diff] [blame] | 23 | #include <linux/regulator/of_regulator.h> |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 24 | #include <linux/gpio.h> |
| 25 | #include <linux/slab.h> |
| 26 | |
| 27 | #include <linux/mfd/arizona/core.h> |
| 28 | #include <linux/mfd/arizona/pdata.h> |
| 29 | #include <linux/mfd/arizona/registers.h> |
| 30 | |
| 31 | struct arizona_ldo1 { |
| 32 | struct regulator_dev *regulator; |
| 33 | struct arizona *arizona; |
| 34 | |
| 35 | struct regulator_consumer_supply supply; |
| 36 | struct regulator_init_data init_data; |
| 37 | }; |
| 38 | |
Mark Brown | 73ee294 | 2012-11-27 18:48:33 +0000 | [diff] [blame] | 39 | static int arizona_ldo1_hc_list_voltage(struct regulator_dev *rdev, |
| 40 | unsigned int selector) |
| 41 | { |
| 42 | if (selector >= rdev->desc->n_voltages) |
| 43 | return -EINVAL; |
| 44 | |
| 45 | if (selector == rdev->desc->n_voltages - 1) |
| 46 | return 1800000; |
| 47 | else |
| 48 | return rdev->desc->min_uV + (rdev->desc->uV_step * selector); |
| 49 | } |
| 50 | |
| 51 | static int arizona_ldo1_hc_map_voltage(struct regulator_dev *rdev, |
| 52 | int min_uV, int max_uV) |
| 53 | { |
| 54 | int sel; |
| 55 | |
| 56 | sel = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step); |
| 57 | if (sel >= rdev->desc->n_voltages) |
| 58 | sel = rdev->desc->n_voltages - 1; |
| 59 | |
| 60 | return sel; |
| 61 | } |
| 62 | |
| 63 | static int arizona_ldo1_hc_set_voltage_sel(struct regulator_dev *rdev, |
| 64 | unsigned sel) |
| 65 | { |
| 66 | struct arizona_ldo1 *ldo = rdev_get_drvdata(rdev); |
| 67 | struct regmap *regmap = ldo->arizona->regmap; |
| 68 | unsigned int val; |
| 69 | int ret; |
| 70 | |
| 71 | if (sel == rdev->desc->n_voltages - 1) |
| 72 | val = ARIZONA_LDO1_HI_PWR; |
| 73 | else |
| 74 | val = 0; |
| 75 | |
| 76 | ret = regmap_update_bits(regmap, ARIZONA_LDO1_CONTROL_2, |
| 77 | ARIZONA_LDO1_HI_PWR, val); |
| 78 | if (ret != 0) |
| 79 | return ret; |
| 80 | |
Mark Brown | 73ee294 | 2012-11-27 18:48:33 +0000 | [diff] [blame] | 81 | if (val) |
| 82 | return 0; |
| 83 | |
| 84 | val = sel << ARIZONA_LDO1_VSEL_SHIFT; |
| 85 | |
| 86 | return regmap_update_bits(regmap, ARIZONA_LDO1_CONTROL_1, |
| 87 | ARIZONA_LDO1_VSEL_MASK, val); |
| 88 | } |
| 89 | |
| 90 | static int arizona_ldo1_hc_get_voltage_sel(struct regulator_dev *rdev) |
| 91 | { |
| 92 | struct arizona_ldo1 *ldo = rdev_get_drvdata(rdev); |
| 93 | struct regmap *regmap = ldo->arizona->regmap; |
| 94 | unsigned int val; |
| 95 | int ret; |
| 96 | |
| 97 | ret = regmap_read(regmap, ARIZONA_LDO1_CONTROL_2, &val); |
| 98 | if (ret != 0) |
| 99 | return ret; |
| 100 | |
| 101 | if (val & ARIZONA_LDO1_HI_PWR) |
| 102 | return rdev->desc->n_voltages - 1; |
| 103 | |
| 104 | ret = regmap_read(regmap, ARIZONA_LDO1_CONTROL_1, &val); |
| 105 | if (ret != 0) |
| 106 | return ret; |
| 107 | |
| 108 | return (val & ARIZONA_LDO1_VSEL_MASK) >> ARIZONA_LDO1_VSEL_SHIFT; |
| 109 | } |
| 110 | |
| 111 | static struct regulator_ops arizona_ldo1_hc_ops = { |
| 112 | .list_voltage = arizona_ldo1_hc_list_voltage, |
| 113 | .map_voltage = arizona_ldo1_hc_map_voltage, |
| 114 | .get_voltage_sel = arizona_ldo1_hc_get_voltage_sel, |
| 115 | .set_voltage_sel = arizona_ldo1_hc_set_voltage_sel, |
| 116 | .get_bypass = regulator_get_bypass_regmap, |
| 117 | .set_bypass = regulator_set_bypass_regmap, |
| 118 | }; |
| 119 | |
| 120 | static const struct regulator_desc arizona_ldo1_hc = { |
| 121 | .name = "LDO1", |
| 122 | .supply_name = "LDOVDD", |
| 123 | .type = REGULATOR_VOLTAGE, |
| 124 | .ops = &arizona_ldo1_hc_ops, |
| 125 | |
| 126 | .bypass_reg = ARIZONA_LDO1_CONTROL_1, |
| 127 | .bypass_mask = ARIZONA_LDO1_BYPASS, |
| 128 | .min_uV = 900000, |
| 129 | .uV_step = 50000, |
| 130 | .n_voltages = 8, |
Mark Brown | ce1bcb7 | 2013-03-26 12:02:32 +0000 | [diff] [blame] | 131 | .enable_time = 1500, |
Mark Brown | 73ee294 | 2012-11-27 18:48:33 +0000 | [diff] [blame] | 132 | |
| 133 | .owner = THIS_MODULE, |
| 134 | }; |
| 135 | |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 136 | static struct regulator_ops arizona_ldo1_ops = { |
| 137 | .list_voltage = regulator_list_voltage_linear, |
| 138 | .map_voltage = regulator_map_voltage_linear, |
| 139 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 140 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 141 | }; |
| 142 | |
| 143 | static const struct regulator_desc arizona_ldo1 = { |
| 144 | .name = "LDO1", |
| 145 | .supply_name = "LDOVDD", |
| 146 | .type = REGULATOR_VOLTAGE, |
| 147 | .ops = &arizona_ldo1_ops, |
| 148 | |
| 149 | .vsel_reg = ARIZONA_LDO1_CONTROL_1, |
| 150 | .vsel_mask = ARIZONA_LDO1_VSEL_MASK, |
| 151 | .min_uV = 900000, |
Charles Keepax | a35ff28 | 2014-03-18 10:49:17 +0000 | [diff] [blame] | 152 | .uV_step = 25000, |
| 153 | .n_voltages = 13, |
Mark Brown | 86a1450 | 2012-11-27 17:02:29 +0000 | [diff] [blame] | 154 | .enable_time = 500, |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 155 | |
| 156 | .owner = THIS_MODULE, |
| 157 | }; |
| 158 | |
Mark Brown | 55a18ae | 2012-11-27 17:51:46 +0000 | [diff] [blame] | 159 | static const struct regulator_init_data arizona_ldo1_dvfs = { |
| 160 | .constraints = { |
| 161 | .min_uV = 1200000, |
| 162 | .max_uV = 1800000, |
| 163 | .valid_ops_mask = REGULATOR_CHANGE_STATUS | |
| 164 | REGULATOR_CHANGE_VOLTAGE, |
| 165 | }, |
| 166 | .num_consumer_supplies = 1, |
| 167 | }; |
| 168 | |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 169 | static const struct regulator_init_data arizona_ldo1_default = { |
Mark Brown | a9905b1 | 2012-06-27 14:32:06 +0100 | [diff] [blame] | 170 | .constraints = { |
| 171 | .valid_ops_mask = REGULATOR_CHANGE_STATUS, |
| 172 | }, |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 173 | .num_consumer_supplies = 1, |
| 174 | }; |
| 175 | |
Charles Keepax | 9a6c2fe | 2015-05-11 13:58:07 +0100 | [diff] [blame] | 176 | static const struct regulator_init_data arizona_ldo1_wm5110 = { |
| 177 | .constraints = { |
| 178 | .min_uV = 1175000, |
| 179 | .max_uV = 1200000, |
| 180 | .valid_ops_mask = REGULATOR_CHANGE_STATUS | |
| 181 | REGULATOR_CHANGE_VOLTAGE, |
| 182 | }, |
| 183 | .num_consumer_supplies = 1, |
| 184 | }; |
| 185 | |
Charles Keepax | 2cce4be | 2014-04-16 10:01:39 +0100 | [diff] [blame] | 186 | static int arizona_ldo1_of_get_pdata(struct arizona *arizona, |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 187 | struct regulator_config *config, |
| 188 | const struct regulator_desc *desc) |
Charles Keepax | 4a8c475 | 2014-04-16 10:01:38 +0100 | [diff] [blame] | 189 | { |
| 190 | struct arizona_pdata *pdata = &arizona->pdata; |
Charles Keepax | 2cce4be | 2014-04-16 10:01:39 +0100 | [diff] [blame] | 191 | struct arizona_ldo1 *ldo1 = config->driver_data; |
| 192 | struct device_node *init_node, *dcvdd_node; |
| 193 | struct regulator_init_data *init_data; |
Charles Keepax | 4a8c475 | 2014-04-16 10:01:38 +0100 | [diff] [blame] | 194 | |
| 195 | pdata->ldoena = arizona_of_get_named_gpio(arizona, "wlf,ldoena", true); |
| 196 | |
Charles Keepax | 2cce4be | 2014-04-16 10:01:39 +0100 | [diff] [blame] | 197 | init_node = of_get_child_by_name(arizona->dev->of_node, "ldo1"); |
| 198 | dcvdd_node = of_parse_phandle(arizona->dev->of_node, "DCVDD-supply", 0); |
| 199 | |
| 200 | if (init_node) { |
| 201 | config->of_node = init_node; |
| 202 | |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 203 | init_data = of_get_regulator_init_data(arizona->dev, init_node, |
| 204 | desc); |
Charles Keepax | 2cce4be | 2014-04-16 10:01:39 +0100 | [diff] [blame] | 205 | |
| 206 | if (init_data) { |
| 207 | init_data->consumer_supplies = &ldo1->supply; |
| 208 | init_data->num_consumer_supplies = 1; |
| 209 | |
| 210 | if (dcvdd_node && dcvdd_node != init_node) |
| 211 | arizona->external_dcvdd = true; |
| 212 | |
| 213 | pdata->ldo1 = init_data; |
| 214 | } |
| 215 | } else if (dcvdd_node) { |
| 216 | arizona->external_dcvdd = true; |
| 217 | } |
| 218 | |
| 219 | of_node_put(dcvdd_node); |
| 220 | |
Charles Keepax | 4a8c475 | 2014-04-16 10:01:38 +0100 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 224 | static int arizona_ldo1_probe(struct platform_device *pdev) |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 225 | { |
| 226 | struct arizona *arizona = dev_get_drvdata(pdev->dev.parent); |
Mark Brown | 73ee294 | 2012-11-27 18:48:33 +0000 | [diff] [blame] | 227 | const struct regulator_desc *desc; |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 228 | struct regulator_config config = { }; |
| 229 | struct arizona_ldo1 *ldo1; |
| 230 | int ret; |
| 231 | |
Charles Keepax | 4a8c475 | 2014-04-16 10:01:38 +0100 | [diff] [blame] | 232 | arizona->external_dcvdd = false; |
| 233 | |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 234 | ldo1 = devm_kzalloc(&pdev->dev, sizeof(*ldo1), GFP_KERNEL); |
Sachin Kamat | f4a6c5b | 2014-02-18 16:11:09 +0530 | [diff] [blame] | 235 | if (!ldo1) |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 236 | return -ENOMEM; |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 237 | |
| 238 | ldo1->arizona = arizona; |
| 239 | |
| 240 | /* |
| 241 | * Since the chip usually supplies itself we provide some |
| 242 | * default init_data for it. This will be overridden with |
| 243 | * platform data if provided. |
| 244 | */ |
Mark Brown | 55a18ae | 2012-11-27 17:51:46 +0000 | [diff] [blame] | 245 | switch (arizona->type) { |
| 246 | case WM5102: |
Charles Keepax | a35ff28 | 2014-03-18 10:49:17 +0000 | [diff] [blame] | 247 | case WM8997: |
Richard Fitzgerald | 33aa380 | 2015-09-28 14:01:07 +0100 | [diff] [blame^] | 248 | case WM8998: |
| 249 | case WM1814: |
Mark Brown | 73ee294 | 2012-11-27 18:48:33 +0000 | [diff] [blame] | 250 | desc = &arizona_ldo1_hc; |
Mark Brown | 55a18ae | 2012-11-27 17:51:46 +0000 | [diff] [blame] | 251 | ldo1->init_data = arizona_ldo1_dvfs; |
| 252 | break; |
Charles Keepax | 9a6c2fe | 2015-05-11 13:58:07 +0100 | [diff] [blame] | 253 | case WM5110: |
| 254 | case WM8280: |
| 255 | desc = &arizona_ldo1; |
| 256 | ldo1->init_data = arizona_ldo1_wm5110; |
| 257 | break; |
Mark Brown | 55a18ae | 2012-11-27 17:51:46 +0000 | [diff] [blame] | 258 | default: |
Mark Brown | 73ee294 | 2012-11-27 18:48:33 +0000 | [diff] [blame] | 259 | desc = &arizona_ldo1; |
Mark Brown | 55a18ae | 2012-11-27 17:51:46 +0000 | [diff] [blame] | 260 | ldo1->init_data = arizona_ldo1_default; |
| 261 | break; |
| 262 | } |
| 263 | |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 264 | ldo1->init_data.consumer_supplies = &ldo1->supply; |
| 265 | ldo1->supply.supply = "DCVDD"; |
| 266 | ldo1->supply.dev_name = dev_name(arizona->dev); |
| 267 | |
| 268 | config.dev = arizona->dev; |
| 269 | config.driver_data = ldo1; |
| 270 | config.regmap = arizona->regmap; |
Charles Keepax | 4a8c475 | 2014-04-16 10:01:38 +0100 | [diff] [blame] | 271 | |
| 272 | if (IS_ENABLED(CONFIG_OF)) { |
| 273 | if (!dev_get_platdata(arizona->dev)) { |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 274 | ret = arizona_ldo1_of_get_pdata(arizona, &config, desc); |
Charles Keepax | 4a8c475 | 2014-04-16 10:01:38 +0100 | [diff] [blame] | 275 | if (ret < 0) |
| 276 | return ret; |
Markus Pargmann | 1de3821 | 2014-11-03 19:12:04 +0100 | [diff] [blame] | 277 | |
| 278 | config.ena_gpio_initialized = true; |
Charles Keepax | 4a8c475 | 2014-04-16 10:01:38 +0100 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Mark Brown | a9905b1 | 2012-06-27 14:32:06 +0100 | [diff] [blame] | 282 | config.ena_gpio = arizona->pdata.ldoena; |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 283 | |
| 284 | if (arizona->pdata.ldo1) |
| 285 | config.init_data = arizona->pdata.ldo1; |
| 286 | else |
| 287 | config.init_data = &ldo1->init_data; |
| 288 | |
Charles Keepax | 4a8c475 | 2014-04-16 10:01:38 +0100 | [diff] [blame] | 289 | /* |
| 290 | * LDO1 can only be used to supply DCVDD so if it has no |
| 291 | * consumers then DCVDD is supplied externally. |
| 292 | */ |
| 293 | if (config.init_data->num_consumer_supplies == 0) |
| 294 | arizona->external_dcvdd = true; |
| 295 | |
Mark Brown | 14ffa88 | 2013-08-31 11:56:47 +0100 | [diff] [blame] | 296 | ldo1->regulator = devm_regulator_register(&pdev->dev, desc, &config); |
Charles Keepax | ec454d7 | 2015-02-20 16:08:43 +0000 | [diff] [blame] | 297 | |
| 298 | of_node_put(config.of_node); |
| 299 | |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 300 | if (IS_ERR(ldo1->regulator)) { |
| 301 | ret = PTR_ERR(ldo1->regulator); |
| 302 | dev_err(arizona->dev, "Failed to register LDO1 supply: %d\n", |
| 303 | ret); |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | platform_set_drvdata(pdev, ldo1); |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 312 | static struct platform_driver arizona_ldo1_driver = { |
| 313 | .probe = arizona_ldo1_probe, |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 314 | .driver = { |
| 315 | .name = "arizona-ldo1", |
Mark Brown | 1910efa | 2012-06-23 13:07:57 +0100 | [diff] [blame] | 316 | }, |
| 317 | }; |
| 318 | |
| 319 | module_platform_driver(arizona_ldo1_driver); |
| 320 | |
| 321 | /* Module information */ |
| 322 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 323 | MODULE_DESCRIPTION("Arizona LDO1 driver"); |
| 324 | MODULE_LICENSE("GPL"); |
| 325 | MODULE_ALIAS("platform:arizona-ldo1"); |