Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * arizona-micsupp.c -- Microphone 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> |
Charles Keepax | 6f1c9c5 | 2014-05-08 17:17:38 +0100 | [diff] [blame] | 19 | #include <linux/of.h> |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/regulator/driver.h> |
| 22 | #include <linux/regulator/machine.h> |
Charles Keepax | 36bcdf1 | 2014-04-16 10:01:41 +0100 | [diff] [blame] | 23 | #include <linux/regulator/of_regulator.h> |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 24 | #include <linux/gpio.h> |
| 25 | #include <linux/slab.h> |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 26 | #include <linux/workqueue.h> |
| 27 | #include <sound/soc.h> |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 28 | |
| 29 | #include <linux/mfd/arizona/core.h> |
| 30 | #include <linux/mfd/arizona/pdata.h> |
| 31 | #include <linux/mfd/arizona/registers.h> |
| 32 | |
Richard Fitzgerald | 22161f3 | 2017-04-18 11:43:49 +0100 | [diff] [blame] | 33 | #include <linux/regulator/arizona-micsupp.h> |
| 34 | |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 35 | struct arizona_micsupp { |
| 36 | struct regulator_dev *regulator; |
Richard Fitzgerald | e165983 | 2017-04-18 11:43:50 +0100 | [diff] [blame] | 37 | struct regmap *regmap; |
| 38 | struct snd_soc_dapm_context **dapm; |
| 39 | unsigned int enable_reg; |
| 40 | struct device *dev; |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 41 | |
| 42 | struct regulator_consumer_supply supply; |
| 43 | struct regulator_init_data init_data; |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 44 | |
| 45 | struct work_struct check_cp_work; |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 48 | static void arizona_micsupp_check_cp(struct work_struct *work) |
| 49 | { |
| 50 | struct arizona_micsupp *micsupp = |
| 51 | container_of(work, struct arizona_micsupp, check_cp_work); |
Richard Fitzgerald | e165983 | 2017-04-18 11:43:50 +0100 | [diff] [blame] | 52 | struct snd_soc_dapm_context *dapm = *micsupp->dapm; |
| 53 | struct snd_soc_component *component; |
| 54 | unsigned int val; |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 55 | int ret; |
| 56 | |
Richard Fitzgerald | e165983 | 2017-04-18 11:43:50 +0100 | [diff] [blame] | 57 | ret = regmap_read(micsupp->regmap, micsupp->enable_reg, &val); |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 58 | if (ret != 0) { |
Richard Fitzgerald | e165983 | 2017-04-18 11:43:50 +0100 | [diff] [blame] | 59 | dev_err(micsupp->dev, |
| 60 | "Failed to read CP state: %d\n", ret); |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
| 64 | if (dapm) { |
Richard Fitzgerald | e165983 | 2017-04-18 11:43:50 +0100 | [diff] [blame] | 65 | component = snd_soc_dapm_to_component(dapm); |
| 66 | |
| 67 | if ((val & (ARIZONA_CPMIC_ENA | ARIZONA_CPMIC_BYPASS)) == |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 68 | ARIZONA_CPMIC_ENA) |
Richard Fitzgerald | 98cf996 | 2016-12-15 14:43:49 +0000 | [diff] [blame] | 69 | snd_soc_component_force_enable_pin(component, |
| 70 | "MICSUPP"); |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 71 | else |
Richard Fitzgerald | 98cf996 | 2016-12-15 14:43:49 +0000 | [diff] [blame] | 72 | snd_soc_component_disable_pin(component, "MICSUPP"); |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 73 | |
| 74 | snd_soc_dapm_sync(dapm); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static int arizona_micsupp_enable(struct regulator_dev *rdev) |
| 79 | { |
| 80 | struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev); |
| 81 | int ret; |
| 82 | |
| 83 | ret = regulator_enable_regmap(rdev); |
| 84 | |
| 85 | if (ret == 0) |
| 86 | schedule_work(&micsupp->check_cp_work); |
| 87 | |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | static int arizona_micsupp_disable(struct regulator_dev *rdev) |
| 92 | { |
| 93 | struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev); |
| 94 | int ret; |
| 95 | |
| 96 | ret = regulator_disable_regmap(rdev); |
| 97 | if (ret == 0) |
| 98 | schedule_work(&micsupp->check_cp_work); |
| 99 | |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | static int arizona_micsupp_set_bypass(struct regulator_dev *rdev, bool ena) |
| 104 | { |
| 105 | struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev); |
| 106 | int ret; |
| 107 | |
| 108 | ret = regulator_set_bypass_regmap(rdev, ena); |
| 109 | if (ret == 0) |
| 110 | schedule_work(&micsupp->check_cp_work); |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
Bhumika Goyal | 2773ead | 2017-01-28 19:10:00 +0530 | [diff] [blame] | 115 | static const struct regulator_ops arizona_micsupp_ops = { |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 116 | .enable = arizona_micsupp_enable, |
| 117 | .disable = arizona_micsupp_disable, |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 118 | .is_enabled = regulator_is_enabled_regmap, |
| 119 | |
Charles Keepax | 71979aa | 2013-11-15 13:13:32 +0000 | [diff] [blame] | 120 | .list_voltage = regulator_list_voltage_linear_range, |
| 121 | .map_voltage = regulator_map_voltage_linear_range, |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 122 | |
| 123 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 124 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Mark Brown | e477ce0 | 2012-08-27 16:04:47 -0700 | [diff] [blame] | 125 | |
| 126 | .get_bypass = regulator_get_bypass_regmap, |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 127 | .set_bypass = arizona_micsupp_set_bypass, |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 128 | }; |
| 129 | |
Charles Keepax | 71979aa | 2013-11-15 13:13:32 +0000 | [diff] [blame] | 130 | static const struct regulator_linear_range arizona_micsupp_ranges[] = { |
| 131 | REGULATOR_LINEAR_RANGE(1700000, 0, 0x1e, 50000), |
| 132 | REGULATOR_LINEAR_RANGE(3300000, 0x1f, 0x1f, 0), |
| 133 | }; |
| 134 | |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 135 | static const struct regulator_desc arizona_micsupp = { |
| 136 | .name = "MICVDD", |
| 137 | .supply_name = "CPVDD", |
| 138 | .type = REGULATOR_VOLTAGE, |
Charles Keepax | 71979aa | 2013-11-15 13:13:32 +0000 | [diff] [blame] | 139 | .n_voltages = 32, |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 140 | .ops = &arizona_micsupp_ops, |
| 141 | |
| 142 | .vsel_reg = ARIZONA_LDO2_CONTROL_1, |
| 143 | .vsel_mask = ARIZONA_LDO2_VSEL_MASK, |
| 144 | .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1, |
| 145 | .enable_mask = ARIZONA_CPMIC_ENA, |
Mark Brown | e477ce0 | 2012-08-27 16:04:47 -0700 | [diff] [blame] | 146 | .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1, |
| 147 | .bypass_mask = ARIZONA_CPMIC_BYPASS, |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 148 | |
Charles Keepax | 71979aa | 2013-11-15 13:13:32 +0000 | [diff] [blame] | 149 | .linear_ranges = arizona_micsupp_ranges, |
| 150 | .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ranges), |
| 151 | |
Mark Brown | 9507281 | 2012-11-27 14:55:49 +0000 | [diff] [blame] | 152 | .enable_time = 3000, |
| 153 | |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 154 | .owner = THIS_MODULE, |
| 155 | }; |
| 156 | |
Charles Keepax | d2e7491 | 2013-11-15 13:48:23 +0000 | [diff] [blame] | 157 | static const struct regulator_linear_range arizona_micsupp_ext_ranges[] = { |
| 158 | REGULATOR_LINEAR_RANGE(900000, 0, 0x14, 25000), |
| 159 | REGULATOR_LINEAR_RANGE(1500000, 0x15, 0x27, 100000), |
| 160 | }; |
| 161 | |
| 162 | static const struct regulator_desc arizona_micsupp_ext = { |
| 163 | .name = "MICVDD", |
| 164 | .supply_name = "CPVDD", |
| 165 | .type = REGULATOR_VOLTAGE, |
| 166 | .n_voltages = 40, |
| 167 | .ops = &arizona_micsupp_ops, |
| 168 | |
| 169 | .vsel_reg = ARIZONA_LDO2_CONTROL_1, |
| 170 | .vsel_mask = ARIZONA_LDO2_VSEL_MASK, |
| 171 | .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1, |
| 172 | .enable_mask = ARIZONA_CPMIC_ENA, |
| 173 | .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1, |
| 174 | .bypass_mask = ARIZONA_CPMIC_BYPASS, |
| 175 | |
| 176 | .linear_ranges = arizona_micsupp_ext_ranges, |
| 177 | .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ext_ranges), |
| 178 | |
| 179 | .enable_time = 3000, |
| 180 | |
| 181 | .owner = THIS_MODULE, |
| 182 | }; |
| 183 | |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 184 | static const struct regulator_init_data arizona_micsupp_default = { |
| 185 | .constraints = { |
| 186 | .valid_ops_mask = REGULATOR_CHANGE_STATUS | |
Mark Brown | 9fc50a2 | 2013-01-10 19:31:47 +0000 | [diff] [blame] | 187 | REGULATOR_CHANGE_VOLTAGE | |
| 188 | REGULATOR_CHANGE_BYPASS, |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 189 | .min_uV = 1700000, |
| 190 | .max_uV = 3300000, |
| 191 | }, |
| 192 | |
| 193 | .num_consumer_supplies = 1, |
| 194 | }; |
| 195 | |
Charles Keepax | d2e7491 | 2013-11-15 13:48:23 +0000 | [diff] [blame] | 196 | static const struct regulator_init_data arizona_micsupp_ext_default = { |
| 197 | .constraints = { |
| 198 | .valid_ops_mask = REGULATOR_CHANGE_STATUS | |
| 199 | REGULATOR_CHANGE_VOLTAGE | |
| 200 | REGULATOR_CHANGE_BYPASS, |
| 201 | .min_uV = 900000, |
| 202 | .max_uV = 3300000, |
| 203 | }, |
| 204 | |
| 205 | .num_consumer_supplies = 1, |
| 206 | }; |
| 207 | |
Richard Fitzgerald | 22161f3 | 2017-04-18 11:43:49 +0100 | [diff] [blame] | 208 | static int arizona_micsupp_of_get_pdata(struct arizona_micsupp_pdata *pdata, |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 209 | struct regulator_config *config, |
| 210 | const struct regulator_desc *desc) |
Charles Keepax | 36bcdf1 | 2014-04-16 10:01:41 +0100 | [diff] [blame] | 211 | { |
Charles Keepax | 36bcdf1 | 2014-04-16 10:01:41 +0100 | [diff] [blame] | 212 | struct arizona_micsupp *micsupp = config->driver_data; |
| 213 | struct device_node *np; |
| 214 | struct regulator_init_data *init_data; |
| 215 | |
Richard Fitzgerald | 22161f3 | 2017-04-18 11:43:49 +0100 | [diff] [blame] | 216 | np = of_get_child_by_name(config->dev->of_node, "micvdd"); |
Charles Keepax | 36bcdf1 | 2014-04-16 10:01:41 +0100 | [diff] [blame] | 217 | |
| 218 | if (np) { |
| 219 | config->of_node = np; |
| 220 | |
Richard Fitzgerald | 22161f3 | 2017-04-18 11:43:49 +0100 | [diff] [blame] | 221 | init_data = of_get_regulator_init_data(config->dev, np, desc); |
Charles Keepax | 36bcdf1 | 2014-04-16 10:01:41 +0100 | [diff] [blame] | 222 | |
| 223 | if (init_data) { |
| 224 | init_data->consumer_supplies = &micsupp->supply; |
| 225 | init_data->num_consumer_supplies = 1; |
| 226 | |
Richard Fitzgerald | 22161f3 | 2017-04-18 11:43:49 +0100 | [diff] [blame] | 227 | pdata->init_data = init_data; |
Charles Keepax | 36bcdf1 | 2014-04-16 10:01:41 +0100 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Richard Fitzgerald | 7d8d14b | 2017-04-18 11:43:51 +0100 | [diff] [blame] | 234 | static int arizona_micsupp_common_init(struct platform_device *pdev, |
| 235 | struct arizona_micsupp *micsupp, |
| 236 | const struct regulator_desc *desc, |
| 237 | struct arizona_micsupp_pdata *pdata) |
| 238 | { |
| 239 | struct regulator_config config = { }; |
| 240 | int ret; |
| 241 | |
| 242 | INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp); |
| 243 | |
| 244 | micsupp->init_data.consumer_supplies = &micsupp->supply; |
| 245 | micsupp->supply.supply = "MICVDD"; |
| 246 | micsupp->supply.dev_name = dev_name(micsupp->dev); |
| 247 | micsupp->enable_reg = desc->enable_reg; |
| 248 | |
| 249 | config.dev = micsupp->dev; |
| 250 | config.driver_data = micsupp; |
| 251 | config.regmap = micsupp->regmap; |
| 252 | |
| 253 | if (IS_ENABLED(CONFIG_OF)) { |
| 254 | if (!dev_get_platdata(micsupp->dev)) { |
| 255 | ret = arizona_micsupp_of_get_pdata(pdata, &config, |
| 256 | desc); |
| 257 | if (ret < 0) |
| 258 | return ret; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (pdata->init_data) |
| 263 | config.init_data = pdata->init_data; |
| 264 | else |
| 265 | config.init_data = &micsupp->init_data; |
| 266 | |
| 267 | /* Default to regulated mode */ |
| 268 | regmap_update_bits(micsupp->regmap, micsupp->enable_reg, |
| 269 | ARIZONA_CPMIC_BYPASS, 0); |
| 270 | |
| 271 | micsupp->regulator = devm_regulator_register(&pdev->dev, |
| 272 | desc, |
| 273 | &config); |
| 274 | |
| 275 | of_node_put(config.of_node); |
| 276 | |
| 277 | if (IS_ERR(micsupp->regulator)) { |
| 278 | ret = PTR_ERR(micsupp->regulator); |
| 279 | dev_err(micsupp->dev, "Failed to register mic supply: %d\n", |
| 280 | ret); |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | platform_set_drvdata(pdev, micsupp); |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 289 | static int arizona_micsupp_probe(struct platform_device *pdev) |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 290 | { |
| 291 | struct arizona *arizona = dev_get_drvdata(pdev->dev.parent); |
Charles Keepax | d2e7491 | 2013-11-15 13:48:23 +0000 | [diff] [blame] | 292 | const struct regulator_desc *desc; |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 293 | struct arizona_micsupp *micsupp; |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 294 | |
| 295 | micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL); |
Sachin Kamat | 820cd31 | 2014-02-18 16:11:10 +0530 | [diff] [blame] | 296 | if (!micsupp) |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 297 | return -ENOMEM; |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 298 | |
Richard Fitzgerald | e165983 | 2017-04-18 11:43:50 +0100 | [diff] [blame] | 299 | micsupp->regmap = arizona->regmap; |
| 300 | micsupp->dapm = &arizona->dapm; |
| 301 | micsupp->dev = arizona->dev; |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 302 | |
| 303 | /* |
| 304 | * Since the chip usually supplies itself we provide some |
| 305 | * default init_data for it. This will be overridden with |
| 306 | * platform data if provided. |
| 307 | */ |
Charles Keepax | d2e7491 | 2013-11-15 13:48:23 +0000 | [diff] [blame] | 308 | switch (arizona->type) { |
| 309 | case WM5110: |
Richard Fitzgerald | f729311 | 2015-01-17 15:21:24 +0000 | [diff] [blame] | 310 | case WM8280: |
Charles Keepax | d2e7491 | 2013-11-15 13:48:23 +0000 | [diff] [blame] | 311 | desc = &arizona_micsupp_ext; |
| 312 | micsupp->init_data = arizona_micsupp_ext_default; |
| 313 | break; |
| 314 | default: |
| 315 | desc = &arizona_micsupp; |
| 316 | micsupp->init_data = arizona_micsupp_default; |
| 317 | break; |
| 318 | } |
| 319 | |
Richard Fitzgerald | 7d8d14b | 2017-04-18 11:43:51 +0100 | [diff] [blame] | 320 | return arizona_micsupp_common_init(pdev, micsupp, desc, |
| 321 | &arizona->pdata.micvdd); |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 324 | static struct platform_driver arizona_micsupp_driver = { |
| 325 | .probe = arizona_micsupp_probe, |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 326 | .driver = { |
| 327 | .name = "arizona-micsupp", |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 328 | }, |
| 329 | }; |
| 330 | |
| 331 | module_platform_driver(arizona_micsupp_driver); |
| 332 | |
| 333 | /* Module information */ |
| 334 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 335 | MODULE_DESCRIPTION("Arizona microphone supply driver"); |
| 336 | MODULE_LICENSE("GPL"); |
| 337 | MODULE_ALIAS("platform:arizona-micsupp"); |