blob: 3c75bf82eeb56734ecbf2d12889c54aea87760cb [file] [log] [blame]
Mark Brownb667a452012-06-14 18:14:00 +01001/*
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 Browne6ed9052013-01-10 19:14:11 +000024#include <linux/workqueue.h>
25#include <sound/soc.h>
Mark Brownb667a452012-06-14 18:14:00 +010026
27#include <linux/mfd/arizona/core.h>
28#include <linux/mfd/arizona/pdata.h>
29#include <linux/mfd/arizona/registers.h>
30
Mark Brownb667a452012-06-14 18:14:00 +010031struct arizona_micsupp {
32 struct regulator_dev *regulator;
33 struct arizona *arizona;
34
35 struct regulator_consumer_supply supply;
36 struct regulator_init_data init_data;
Mark Browne6ed9052013-01-10 19:14:11 +000037
38 struct work_struct check_cp_work;
Mark Brownb667a452012-06-14 18:14:00 +010039};
40
Mark Browne6ed9052013-01-10 19:14:11 +000041static void arizona_micsupp_check_cp(struct work_struct *work)
42{
43 struct arizona_micsupp *micsupp =
44 container_of(work, struct arizona_micsupp, check_cp_work);
45 struct snd_soc_dapm_context *dapm = micsupp->arizona->dapm;
46 struct arizona *arizona = micsupp->arizona;
47 struct regmap *regmap = arizona->regmap;
48 unsigned int reg;
49 int ret;
50
51 ret = regmap_read(regmap, ARIZONA_MIC_CHARGE_PUMP_1, &reg);
52 if (ret != 0) {
53 dev_err(arizona->dev, "Failed to read CP state: %d\n", ret);
54 return;
55 }
56
57 if (dapm) {
58 if ((reg & (ARIZONA_CPMIC_ENA | ARIZONA_CPMIC_BYPASS)) ==
59 ARIZONA_CPMIC_ENA)
60 snd_soc_dapm_force_enable_pin(dapm, "MICSUPP");
61 else
62 snd_soc_dapm_disable_pin(dapm, "MICSUPP");
63
64 snd_soc_dapm_sync(dapm);
65 }
66}
67
68static int arizona_micsupp_enable(struct regulator_dev *rdev)
69{
70 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
71 int ret;
72
73 ret = regulator_enable_regmap(rdev);
74
75 if (ret == 0)
76 schedule_work(&micsupp->check_cp_work);
77
78 return ret;
79}
80
81static int arizona_micsupp_disable(struct regulator_dev *rdev)
82{
83 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
84 int ret;
85
86 ret = regulator_disable_regmap(rdev);
87 if (ret == 0)
88 schedule_work(&micsupp->check_cp_work);
89
90 return ret;
91}
92
93static int arizona_micsupp_set_bypass(struct regulator_dev *rdev, bool ena)
94{
95 struct arizona_micsupp *micsupp = rdev_get_drvdata(rdev);
96 int ret;
97
98 ret = regulator_set_bypass_regmap(rdev, ena);
99 if (ret == 0)
100 schedule_work(&micsupp->check_cp_work);
101
102 return ret;
103}
104
Mark Brownb667a452012-06-14 18:14:00 +0100105static struct regulator_ops arizona_micsupp_ops = {
Mark Browne6ed9052013-01-10 19:14:11 +0000106 .enable = arizona_micsupp_enable,
107 .disable = arizona_micsupp_disable,
Mark Brownb667a452012-06-14 18:14:00 +0100108 .is_enabled = regulator_is_enabled_regmap,
109
Charles Keepax71979aa2013-11-15 13:13:32 +0000110 .list_voltage = regulator_list_voltage_linear_range,
111 .map_voltage = regulator_map_voltage_linear_range,
Mark Brownb667a452012-06-14 18:14:00 +0100112
113 .get_voltage_sel = regulator_get_voltage_sel_regmap,
114 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Browne477ce02012-08-27 16:04:47 -0700115
116 .get_bypass = regulator_get_bypass_regmap,
Mark Browne6ed9052013-01-10 19:14:11 +0000117 .set_bypass = arizona_micsupp_set_bypass,
Mark Brownb667a452012-06-14 18:14:00 +0100118};
119
Charles Keepax71979aa2013-11-15 13:13:32 +0000120static const struct regulator_linear_range arizona_micsupp_ranges[] = {
121 REGULATOR_LINEAR_RANGE(1700000, 0, 0x1e, 50000),
122 REGULATOR_LINEAR_RANGE(3300000, 0x1f, 0x1f, 0),
123};
124
Mark Brownb667a452012-06-14 18:14:00 +0100125static const struct regulator_desc arizona_micsupp = {
126 .name = "MICVDD",
127 .supply_name = "CPVDD",
128 .type = REGULATOR_VOLTAGE,
Charles Keepax71979aa2013-11-15 13:13:32 +0000129 .n_voltages = 32,
Mark Brownb667a452012-06-14 18:14:00 +0100130 .ops = &arizona_micsupp_ops,
131
132 .vsel_reg = ARIZONA_LDO2_CONTROL_1,
133 .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
134 .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
135 .enable_mask = ARIZONA_CPMIC_ENA,
Mark Browne477ce02012-08-27 16:04:47 -0700136 .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
137 .bypass_mask = ARIZONA_CPMIC_BYPASS,
Mark Brownb667a452012-06-14 18:14:00 +0100138
Charles Keepax71979aa2013-11-15 13:13:32 +0000139 .linear_ranges = arizona_micsupp_ranges,
140 .n_linear_ranges = ARRAY_SIZE(arizona_micsupp_ranges),
141
Mark Brown95072812012-11-27 14:55:49 +0000142 .enable_time = 3000,
143
Mark Brownb667a452012-06-14 18:14:00 +0100144 .owner = THIS_MODULE,
145};
146
147static const struct regulator_init_data arizona_micsupp_default = {
148 .constraints = {
149 .valid_ops_mask = REGULATOR_CHANGE_STATUS |
Mark Brown9fc50a22013-01-10 19:31:47 +0000150 REGULATOR_CHANGE_VOLTAGE |
151 REGULATOR_CHANGE_BYPASS,
Mark Brownb667a452012-06-14 18:14:00 +0100152 .min_uV = 1700000,
153 .max_uV = 3300000,
154 },
155
156 .num_consumer_supplies = 1,
157};
158
Bill Pembertona5023572012-11-19 13:22:22 -0500159static int arizona_micsupp_probe(struct platform_device *pdev)
Mark Brownb667a452012-06-14 18:14:00 +0100160{
161 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
162 struct regulator_config config = { };
163 struct arizona_micsupp *micsupp;
164 int ret;
165
166 micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
167 if (micsupp == NULL) {
168 dev_err(&pdev->dev, "Unable to allocate private data\n");
169 return -ENOMEM;
170 }
171
172 micsupp->arizona = arizona;
Mark Browne6ed9052013-01-10 19:14:11 +0000173 INIT_WORK(&micsupp->check_cp_work, arizona_micsupp_check_cp);
Mark Brownb667a452012-06-14 18:14:00 +0100174
175 /*
176 * Since the chip usually supplies itself we provide some
177 * default init_data for it. This will be overridden with
178 * platform data if provided.
179 */
180 micsupp->init_data = arizona_micsupp_default;
181 micsupp->init_data.consumer_supplies = &micsupp->supply;
182 micsupp->supply.supply = "MICVDD";
183 micsupp->supply.dev_name = dev_name(arizona->dev);
184
185 config.dev = arizona->dev;
186 config.driver_data = micsupp;
187 config.regmap = arizona->regmap;
188
189 if (arizona->pdata.micvdd)
190 config.init_data = arizona->pdata.micvdd;
191 else
192 config.init_data = &micsupp->init_data;
193
Mark Brown6dc027c2012-07-04 12:50:02 +0100194 /* Default to regulated mode until the API supports bypass */
195 regmap_update_bits(arizona->regmap, ARIZONA_MIC_CHARGE_PUMP_1,
196 ARIZONA_CPMIC_BYPASS, 0);
197
Mark Brownb6b77092013-08-31 11:57:00 +0100198 micsupp->regulator = devm_regulator_register(&pdev->dev,
199 &arizona_micsupp,
200 &config);
Mark Brownb667a452012-06-14 18:14:00 +0100201 if (IS_ERR(micsupp->regulator)) {
202 ret = PTR_ERR(micsupp->regulator);
203 dev_err(arizona->dev, "Failed to register mic supply: %d\n",
204 ret);
205 return ret;
206 }
207
208 platform_set_drvdata(pdev, micsupp);
209
210 return 0;
211}
212
Mark Brownb667a452012-06-14 18:14:00 +0100213static struct platform_driver arizona_micsupp_driver = {
214 .probe = arizona_micsupp_probe,
Mark Brownb667a452012-06-14 18:14:00 +0100215 .driver = {
216 .name = "arizona-micsupp",
217 .owner = THIS_MODULE,
218 },
219};
220
221module_platform_driver(arizona_micsupp_driver);
222
223/* Module information */
224MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
225MODULE_DESCRIPTION("Arizona microphone supply driver");
226MODULE_LICENSE("GPL");
227MODULE_ALIAS("platform:arizona-micsupp");