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> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/regulator/driver.h> |
| 21 | #include <linux/regulator/machine.h> |
| 22 | #include <linux/gpio.h> |
| 23 | #include <linux/slab.h> |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 24 | #include <linux/workqueue.h> |
| 25 | #include <sound/soc.h> |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 26 | |
| 27 | #include <linux/mfd/arizona/core.h> |
| 28 | #include <linux/mfd/arizona/pdata.h> |
| 29 | #include <linux/mfd/arizona/registers.h> |
| 30 | |
| 31 | #define ARIZONA_MICSUPP_MAX_SELECTOR 0x1f |
| 32 | |
| 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 | |
| 43 | static int arizona_micsupp_list_voltage(struct regulator_dev *rdev, |
| 44 | unsigned int selector) |
| 45 | { |
| 46 | if (selector > ARIZONA_MICSUPP_MAX_SELECTOR) |
| 47 | return -EINVAL; |
| 48 | |
| 49 | if (selector == ARIZONA_MICSUPP_MAX_SELECTOR) |
| 50 | return 3300000; |
| 51 | else |
| 52 | return (selector * 50000) + 1700000; |
| 53 | } |
| 54 | |
| 55 | static int arizona_micsupp_map_voltage(struct regulator_dev *rdev, |
| 56 | int min_uV, int max_uV) |
| 57 | { |
| 58 | unsigned int voltage; |
| 59 | int selector; |
| 60 | |
| 61 | if (min_uV < 1700000) |
| 62 | min_uV = 1700000; |
| 63 | |
Axel Lin | 7f217d3 | 2012-06-26 18:37:58 +0800 | [diff] [blame] | 64 | if (min_uV > 3200000) |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 65 | selector = ARIZONA_MICSUPP_MAX_SELECTOR; |
| 66 | else |
| 67 | selector = DIV_ROUND_UP(min_uV - 1700000, 50000); |
| 68 | |
| 69 | if (selector < 0) |
| 70 | return -EINVAL; |
| 71 | |
| 72 | voltage = arizona_micsupp_list_voltage(rdev, selector); |
| 73 | if (voltage < min_uV || voltage > max_uV) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | return selector; |
| 77 | } |
| 78 | |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 79 | static void arizona_micsupp_check_cp(struct work_struct *work) |
| 80 | { |
| 81 | struct arizona_micsupp *micsupp = |
| 82 | container_of(work, struct arizona_micsupp, check_cp_work); |
| 83 | struct snd_soc_dapm_context *dapm = micsupp->arizona->dapm; |
| 84 | struct arizona *arizona = micsupp->arizona; |
| 85 | struct regmap *regmap = arizona->regmap; |
| 86 | unsigned int reg; |
| 87 | int ret; |
| 88 | |
| 89 | ret = regmap_read(regmap, ARIZONA_MIC_CHARGE_PUMP_1, ®); |
| 90 | if (ret != 0) { |
| 91 | dev_err(arizona->dev, "Failed to read CP state: %d\n", ret); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | if (dapm) { |
| 96 | if ((reg & (ARIZONA_CPMIC_ENA | ARIZONA_CPMIC_BYPASS)) == |
| 97 | ARIZONA_CPMIC_ENA) |
| 98 | snd_soc_dapm_force_enable_pin(dapm, "MICSUPP"); |
| 99 | else |
| 100 | snd_soc_dapm_disable_pin(dapm, "MICSUPP"); |
| 101 | |
| 102 | snd_soc_dapm_sync(dapm); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static int arizona_micsupp_enable(struct regulator_dev *rdev) |
| 107 | { |
| 108 | struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev); |
| 109 | int ret; |
| 110 | |
| 111 | ret = regulator_enable_regmap(rdev); |
| 112 | |
| 113 | if (ret == 0) |
| 114 | schedule_work(&micsupp->check_cp_work); |
| 115 | |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | static int arizona_micsupp_disable(struct regulator_dev *rdev) |
| 120 | { |
| 121 | struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev); |
| 122 | int ret; |
| 123 | |
| 124 | ret = regulator_disable_regmap(rdev); |
| 125 | if (ret == 0) |
| 126 | schedule_work(&micsupp->check_cp_work); |
| 127 | |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | static int arizona_micsupp_set_bypass(struct regulator_dev *rdev, bool ena) |
| 132 | { |
| 133 | struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev); |
| 134 | int ret; |
| 135 | |
| 136 | ret = regulator_set_bypass_regmap(rdev, ena); |
| 137 | if (ret == 0) |
| 138 | schedule_work(&micsupp->check_cp_work); |
| 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 143 | static struct regulator_ops arizona_micsupp_ops = { |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 144 | .enable = arizona_micsupp_enable, |
| 145 | .disable = arizona_micsupp_disable, |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 146 | .is_enabled = regulator_is_enabled_regmap, |
| 147 | |
| 148 | .list_voltage = arizona_micsupp_list_voltage, |
| 149 | .map_voltage = arizona_micsupp_map_voltage, |
| 150 | |
| 151 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 152 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Mark Brown | e477ce0 | 2012-08-27 16:04:47 -0700 | [diff] [blame] | 153 | |
| 154 | .get_bypass = regulator_get_bypass_regmap, |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 155 | .set_bypass = arizona_micsupp_set_bypass, |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | static const struct regulator_desc arizona_micsupp = { |
| 159 | .name = "MICVDD", |
| 160 | .supply_name = "CPVDD", |
| 161 | .type = REGULATOR_VOLTAGE, |
| 162 | .n_voltages = ARIZONA_MICSUPP_MAX_SELECTOR + 1, |
| 163 | .ops = &arizona_micsupp_ops, |
| 164 | |
| 165 | .vsel_reg = ARIZONA_LDO2_CONTROL_1, |
| 166 | .vsel_mask = ARIZONA_LDO2_VSEL_MASK, |
| 167 | .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1, |
| 168 | .enable_mask = ARIZONA_CPMIC_ENA, |
Mark Brown | e477ce0 | 2012-08-27 16:04:47 -0700 | [diff] [blame] | 169 | .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1, |
| 170 | .bypass_mask = ARIZONA_CPMIC_BYPASS, |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 171 | |
Mark Brown | 9507281 | 2012-11-27 14:55:49 +0000 | [diff] [blame] | 172 | .enable_time = 3000, |
| 173 | |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 174 | .owner = THIS_MODULE, |
| 175 | }; |
| 176 | |
| 177 | static const struct regulator_init_data arizona_micsupp_default = { |
| 178 | .constraints = { |
| 179 | .valid_ops_mask = REGULATOR_CHANGE_STATUS | |
Mark Brown | 9fc50a2 | 2013-01-10 19:31:47 +0000 | [diff] [blame] | 180 | REGULATOR_CHANGE_VOLTAGE | |
| 181 | REGULATOR_CHANGE_BYPASS, |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 182 | .min_uV = 1700000, |
| 183 | .max_uV = 3300000, |
| 184 | }, |
| 185 | |
| 186 | .num_consumer_supplies = 1, |
| 187 | }; |
| 188 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 189 | static int arizona_micsupp_probe(struct platform_device *pdev) |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 190 | { |
| 191 | struct arizona *arizona = dev_get_drvdata(pdev->dev.parent); |
| 192 | struct regulator_config config = { }; |
| 193 | struct arizona_micsupp *micsupp; |
| 194 | int ret; |
| 195 | |
| 196 | micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL); |
| 197 | if (micsupp == NULL) { |
| 198 | dev_err(&pdev->dev, "Unable to allocate private data\n"); |
| 199 | return -ENOMEM; |
| 200 | } |
| 201 | |
| 202 | micsupp->arizona = arizona; |
Mark Brown | e6ed905 | 2013-01-10 19:14:11 +0000 | [diff] [blame] | 203 | INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp); |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * Since the chip usually supplies itself we provide some |
| 207 | * default init_data for it. This will be overridden with |
| 208 | * platform data if provided. |
| 209 | */ |
| 210 | micsupp->init_data = arizona_micsupp_default; |
| 211 | micsupp->init_data.consumer_supplies = &micsupp->supply; |
| 212 | micsupp->supply.supply = "MICVDD"; |
| 213 | micsupp->supply.dev_name = dev_name(arizona->dev); |
| 214 | |
| 215 | config.dev = arizona->dev; |
| 216 | config.driver_data = micsupp; |
| 217 | config.regmap = arizona->regmap; |
| 218 | |
| 219 | if (arizona->pdata.micvdd) |
| 220 | config.init_data = arizona->pdata.micvdd; |
| 221 | else |
| 222 | config.init_data = &micsupp->init_data; |
| 223 | |
Mark Brown | 6dc027c | 2012-07-04 12:50:02 +0100 | [diff] [blame] | 224 | /* Default to regulated mode until the API supports bypass */ |
| 225 | regmap_update_bits(arizona->regmap, ARIZONA_MIC_CHARGE_PUMP_1, |
| 226 | ARIZONA_CPMIC_BYPASS, 0); |
| 227 | |
Mark Brown | b6b7709 | 2013-08-31 11:57:00 +0100 | [diff] [blame] | 228 | micsupp->regulator = devm_regulator_register(&pdev->dev, |
| 229 | &arizona_micsupp, |
| 230 | &config); |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 231 | if (IS_ERR(micsupp->regulator)) { |
| 232 | ret = PTR_ERR(micsupp->regulator); |
| 233 | dev_err(arizona->dev, "Failed to register mic supply: %d\n", |
| 234 | ret); |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | platform_set_drvdata(pdev, micsupp); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 243 | static struct platform_driver arizona_micsupp_driver = { |
| 244 | .probe = arizona_micsupp_probe, |
Mark Brown | b667a45 | 2012-06-14 18:14:00 +0100 | [diff] [blame] | 245 | .driver = { |
| 246 | .name = "arizona-micsupp", |
| 247 | .owner = THIS_MODULE, |
| 248 | }, |
| 249 | }; |
| 250 | |
| 251 | module_platform_driver(arizona_micsupp_driver); |
| 252 | |
| 253 | /* Module information */ |
| 254 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 255 | MODULE_DESCRIPTION("Arizona microphone supply driver"); |
| 256 | MODULE_LICENSE("GPL"); |
| 257 | MODULE_ALIAS("platform:arizona-micsupp"); |