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