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