blob: 27b61639cdc7e7073d7bf3cd5247ac371a75c37c [file] [log] [blame]
Mark Brown3cc72982012-06-19 16:31:53 +01001/*
2 * Arizona core driver
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
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +010013#include <linux/clk.h>
Mark Brown3cc72982012-06-19 16:31:53 +010014#include <linux/delay.h>
Mark Brown59db9692012-07-09 00:31:36 +020015#include <linux/err.h>
Charles Keepaxc1860462018-03-12 15:52:00 +000016#include <linux/gpio/consumer.h>
Mark Brown3cc72982012-06-19 16:31:53 +010017#include <linux/interrupt.h>
18#include <linux/mfd/core.h>
19#include <linux/module.h>
Mark Brownd7810092013-03-25 00:11:27 +000020#include <linux/of.h>
21#include <linux/of_device.h>
Mark Brown3cc72982012-06-19 16:31:53 +010022#include <linux/pm_runtime.h>
23#include <linux/regmap.h>
24#include <linux/regulator/consumer.h>
Mark Brown59274672013-04-23 19:44:16 +010025#include <linux/regulator/machine.h>
Mark Brown3cc72982012-06-19 16:31:53 +010026#include <linux/slab.h>
Charles Keepaxf99fea92018-06-28 16:49:35 +010027#include <linux/ktime.h>
Richard Fitzgeraldae05ea362015-10-02 13:29:13 +010028#include <linux/platform_device.h>
Mark Brown3cc72982012-06-19 16:31:53 +010029
30#include <linux/mfd/arizona/core.h>
31#include <linux/mfd/arizona/registers.h>
32
33#include "arizona.h"
34
Charles Keepax3762aed2015-08-11 09:34:31 +010035static const char * const wm5102_core_supplies[] = {
Mark Brown3cc72982012-06-19 16:31:53 +010036 "AVDD",
37 "DBVDD1",
Mark Brown3cc72982012-06-19 16:31:53 +010038};
39
40int arizona_clk32k_enable(struct arizona *arizona)
41{
42 int ret = 0;
43
44 mutex_lock(&arizona->clk_lock);
45
46 arizona->clk32k_ref++;
47
Mark Brown247fa192013-03-19 14:47:47 +010048 if (arizona->clk32k_ref == 1) {
49 switch (arizona->pdata.clk32k_src) {
50 case ARIZONA_32KZ_MCLK1:
51 ret = pm_runtime_get_sync(arizona->dev);
52 if (ret != 0)
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +010053 goto err_ref;
54 ret = clk_prepare_enable(arizona->mclk[ARIZONA_MCLK1]);
Sapthagiri Baratam6b269a42018-08-21 19:52:44 +053055 if (ret != 0) {
56 pm_runtime_put_sync(arizona->dev);
57 goto err_ref;
58 }
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +010059 break;
60 case ARIZONA_32KZ_MCLK2:
61 ret = clk_prepare_enable(arizona->mclk[ARIZONA_MCLK2]);
62 if (ret != 0)
63 goto err_ref;
Mark Brown247fa192013-03-19 14:47:47 +010064 break;
65 }
66
Mark Brown3cc72982012-06-19 16:31:53 +010067 ret = regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
68 ARIZONA_CLK_32K_ENA,
69 ARIZONA_CLK_32K_ENA);
Mark Brown247fa192013-03-19 14:47:47 +010070 }
Mark Brown3cc72982012-06-19 16:31:53 +010071
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +010072err_ref:
Mark Brown3cc72982012-06-19 16:31:53 +010073 if (ret != 0)
74 arizona->clk32k_ref--;
75
76 mutex_unlock(&arizona->clk_lock);
77
78 return ret;
79}
80EXPORT_SYMBOL_GPL(arizona_clk32k_enable);
81
82int arizona_clk32k_disable(struct arizona *arizona)
83{
Mark Brown3cc72982012-06-19 16:31:53 +010084 mutex_lock(&arizona->clk_lock);
85
86 BUG_ON(arizona->clk32k_ref <= 0);
87
88 arizona->clk32k_ref--;
89
Mark Brown247fa192013-03-19 14:47:47 +010090 if (arizona->clk32k_ref == 0) {
Mark Brown3cc72982012-06-19 16:31:53 +010091 regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
92 ARIZONA_CLK_32K_ENA, 0);
93
Mark Brown247fa192013-03-19 14:47:47 +010094 switch (arizona->pdata.clk32k_src) {
95 case ARIZONA_32KZ_MCLK1:
96 pm_runtime_put_sync(arizona->dev);
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +010097 clk_disable_unprepare(arizona->mclk[ARIZONA_MCLK1]);
98 break;
99 case ARIZONA_32KZ_MCLK2:
100 clk_disable_unprepare(arizona->mclk[ARIZONA_MCLK2]);
Mark Brown247fa192013-03-19 14:47:47 +0100101 break;
102 }
103 }
104
Mark Brown3cc72982012-06-19 16:31:53 +0100105 mutex_unlock(&arizona->clk_lock);
106
Javier Martinez Canillasa260fba2015-09-29 13:26:02 +0200107 return 0;
Mark Brown3cc72982012-06-19 16:31:53 +0100108}
109EXPORT_SYMBOL_GPL(arizona_clk32k_disable);
110
111static irqreturn_t arizona_clkgen_err(int irq, void *data)
112{
113 struct arizona *arizona = data;
114
115 dev_err(arizona->dev, "CLKGEN error\n");
116
117 return IRQ_HANDLED;
118}
119
120static irqreturn_t arizona_underclocked(int irq, void *data)
121{
122 struct arizona *arizona = data;
123 unsigned int val;
124 int ret;
125
126 ret = regmap_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_8,
127 &val);
128 if (ret != 0) {
129 dev_err(arizona->dev, "Failed to read underclock status: %d\n",
130 ret);
131 return IRQ_NONE;
132 }
133
134 if (val & ARIZONA_AIF3_UNDERCLOCKED_STS)
135 dev_err(arizona->dev, "AIF3 underclocked\n");
Mark Brown3cc72982012-06-19 16:31:53 +0100136 if (val & ARIZONA_AIF2_UNDERCLOCKED_STS)
Charles Keepax3ebef342012-11-20 13:46:20 +0900137 dev_err(arizona->dev, "AIF2 underclocked\n");
138 if (val & ARIZONA_AIF1_UNDERCLOCKED_STS)
Mark Brown3cc72982012-06-19 16:31:53 +0100139 dev_err(arizona->dev, "AIF1 underclocked\n");
Charles Keepax6e440d22014-07-15 11:21:49 +0100140 if (val & ARIZONA_ISRC3_UNDERCLOCKED_STS)
141 dev_err(arizona->dev, "ISRC3 underclocked\n");
Mark Brown3cc72982012-06-19 16:31:53 +0100142 if (val & ARIZONA_ISRC2_UNDERCLOCKED_STS)
143 dev_err(arizona->dev, "ISRC2 underclocked\n");
144 if (val & ARIZONA_ISRC1_UNDERCLOCKED_STS)
145 dev_err(arizona->dev, "ISRC1 underclocked\n");
146 if (val & ARIZONA_FX_UNDERCLOCKED_STS)
147 dev_err(arizona->dev, "FX underclocked\n");
148 if (val & ARIZONA_ASRC_UNDERCLOCKED_STS)
149 dev_err(arizona->dev, "ASRC underclocked\n");
150 if (val & ARIZONA_DAC_UNDERCLOCKED_STS)
151 dev_err(arizona->dev, "DAC underclocked\n");
152 if (val & ARIZONA_ADC_UNDERCLOCKED_STS)
153 dev_err(arizona->dev, "ADC underclocked\n");
154 if (val & ARIZONA_MIXER_UNDERCLOCKED_STS)
Mark Brown648a9882013-01-28 00:32:53 +0800155 dev_err(arizona->dev, "Mixer dropped sample\n");
Mark Brown3cc72982012-06-19 16:31:53 +0100156
157 return IRQ_HANDLED;
158}
159
160static irqreturn_t arizona_overclocked(int irq, void *data)
161{
162 struct arizona *arizona = data;
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100163 unsigned int val[3];
Mark Brown3cc72982012-06-19 16:31:53 +0100164 int ret;
Charles Keepax3762aed2015-08-11 09:34:31 +0100165
Mark Brown3cc72982012-06-19 16:31:53 +0100166 ret = regmap_bulk_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_6,
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100167 &val[0], 3);
Mark Brown3cc72982012-06-19 16:31:53 +0100168 if (ret != 0) {
169 dev_err(arizona->dev, "Failed to read overclock status: %d\n",
170 ret);
171 return IRQ_NONE;
172 }
173
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100174 switch (arizona->type) {
175 case WM8998:
176 case WM1814:
177 /* Some bits are shifted on WM8998,
178 * rearrange to match the standard bit layout
179 */
180 val[0] = ((val[0] & 0x60e0) >> 1) |
181 ((val[0] & 0x1e00) >> 2) |
182 (val[0] & 0x000f);
183 break;
184 default:
185 break;
186 }
187
Mark Brown3cc72982012-06-19 16:31:53 +0100188 if (val[0] & ARIZONA_PWM_OVERCLOCKED_STS)
189 dev_err(arizona->dev, "PWM overclocked\n");
190 if (val[0] & ARIZONA_FX_CORE_OVERCLOCKED_STS)
191 dev_err(arizona->dev, "FX core overclocked\n");
192 if (val[0] & ARIZONA_DAC_SYS_OVERCLOCKED_STS)
193 dev_err(arizona->dev, "DAC SYS overclocked\n");
194 if (val[0] & ARIZONA_DAC_WARP_OVERCLOCKED_STS)
195 dev_err(arizona->dev, "DAC WARP overclocked\n");
196 if (val[0] & ARIZONA_ADC_OVERCLOCKED_STS)
197 dev_err(arizona->dev, "ADC overclocked\n");
198 if (val[0] & ARIZONA_MIXER_OVERCLOCKED_STS)
199 dev_err(arizona->dev, "Mixer overclocked\n");
200 if (val[0] & ARIZONA_AIF3_SYNC_OVERCLOCKED_STS)
201 dev_err(arizona->dev, "AIF3 overclocked\n");
202 if (val[0] & ARIZONA_AIF2_SYNC_OVERCLOCKED_STS)
203 dev_err(arizona->dev, "AIF2 overclocked\n");
204 if (val[0] & ARIZONA_AIF1_SYNC_OVERCLOCKED_STS)
205 dev_err(arizona->dev, "AIF1 overclocked\n");
206 if (val[0] & ARIZONA_PAD_CTRL_OVERCLOCKED_STS)
207 dev_err(arizona->dev, "Pad control overclocked\n");
208
209 if (val[1] & ARIZONA_SLIMBUS_SUBSYS_OVERCLOCKED_STS)
210 dev_err(arizona->dev, "Slimbus subsystem overclocked\n");
211 if (val[1] & ARIZONA_SLIMBUS_ASYNC_OVERCLOCKED_STS)
212 dev_err(arizona->dev, "Slimbus async overclocked\n");
213 if (val[1] & ARIZONA_SLIMBUS_SYNC_OVERCLOCKED_STS)
214 dev_err(arizona->dev, "Slimbus sync overclocked\n");
215 if (val[1] & ARIZONA_ASRC_ASYNC_SYS_OVERCLOCKED_STS)
216 dev_err(arizona->dev, "ASRC async system overclocked\n");
217 if (val[1] & ARIZONA_ASRC_ASYNC_WARP_OVERCLOCKED_STS)
218 dev_err(arizona->dev, "ASRC async WARP overclocked\n");
219 if (val[1] & ARIZONA_ASRC_SYNC_SYS_OVERCLOCKED_STS)
220 dev_err(arizona->dev, "ASRC sync system overclocked\n");
221 if (val[1] & ARIZONA_ASRC_SYNC_WARP_OVERCLOCKED_STS)
222 dev_err(arizona->dev, "ASRC sync WARP overclocked\n");
223 if (val[1] & ARIZONA_ADSP2_1_OVERCLOCKED_STS)
224 dev_err(arizona->dev, "DSP1 overclocked\n");
Charles Keepax6e440d22014-07-15 11:21:49 +0100225 if (val[1] & ARIZONA_ISRC3_OVERCLOCKED_STS)
226 dev_err(arizona->dev, "ISRC3 overclocked\n");
Mark Brown3cc72982012-06-19 16:31:53 +0100227 if (val[1] & ARIZONA_ISRC2_OVERCLOCKED_STS)
228 dev_err(arizona->dev, "ISRC2 overclocked\n");
229 if (val[1] & ARIZONA_ISRC1_OVERCLOCKED_STS)
230 dev_err(arizona->dev, "ISRC1 overclocked\n");
231
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100232 if (val[2] & ARIZONA_SPDIF_OVERCLOCKED_STS)
233 dev_err(arizona->dev, "SPDIF overclocked\n");
234
Mark Brown3cc72982012-06-19 16:31:53 +0100235 return IRQ_HANDLED;
236}
237
Charles Keepaxef84f882017-03-15 14:58:38 +0000238#define ARIZONA_REG_POLL_DELAY_US 7500
239
Charles Keepaxf99fea92018-06-28 16:49:35 +0100240static inline bool arizona_poll_reg_delay(ktime_t timeout)
241{
242 if (ktime_compare(ktime_get(), timeout) > 0)
243 return false;
244
245 usleep_range(ARIZONA_REG_POLL_DELAY_US / 2, ARIZONA_REG_POLL_DELAY_US);
246
247 return true;
248}
249
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000250static int arizona_poll_reg(struct arizona *arizona,
Charles Keepaxef84f882017-03-15 14:58:38 +0000251 int timeout_ms, unsigned int reg,
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000252 unsigned int mask, unsigned int target)
253{
Charles Keepaxf99fea92018-06-28 16:49:35 +0100254 ktime_t timeout = ktime_add_us(ktime_get(), timeout_ms * USEC_PER_MSEC);
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000255 unsigned int val = 0;
Charles Keepaxef84f882017-03-15 14:58:38 +0000256 int ret;
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000257
Charles Keepaxf99fea92018-06-28 16:49:35 +0100258 do {
259 ret = regmap_read(arizona->regmap, reg, &val);
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000260
Charles Keepaxf99fea92018-06-28 16:49:35 +0100261 if ((val & mask) == target)
262 return 0;
263 } while (arizona_poll_reg_delay(timeout));
264
265 if (ret) {
266 dev_err(arizona->dev, "Failed polling reg 0x%x: %d\n",
267 reg, ret);
268 return ret;
269 }
270
271 dev_err(arizona->dev, "Polling reg 0x%x timed out: %x\n", reg, val);
272 return -ETIMEDOUT;
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000273}
274
Mark Brown3cc72982012-06-19 16:31:53 +0100275static int arizona_wait_for_boot(struct arizona *arizona)
276{
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000277 int ret;
Mark Brown3cc72982012-06-19 16:31:53 +0100278
279 /*
280 * We can't use an interrupt as we need to runtime resume to do so,
281 * we won't race with the interrupt handler as it'll be blocked on
282 * runtime resume.
283 */
Charles Keepaxef84f882017-03-15 14:58:38 +0000284 ret = arizona_poll_reg(arizona, 30, ARIZONA_INTERRUPT_RAW_STATUS_5,
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000285 ARIZONA_BOOT_DONE_STS, ARIZONA_BOOT_DONE_STS);
Mark Brown3cc72982012-06-19 16:31:53 +0100286
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000287 if (!ret)
Mark Brown3cc72982012-06-19 16:31:53 +0100288 regmap_write(arizona->regmap, ARIZONA_INTERRUPT_STATUS_5,
289 ARIZONA_BOOT_DONE_STS);
Mark Brown3cc72982012-06-19 16:31:53 +0100290
291 pm_runtime_mark_last_busy(arizona->dev);
292
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000293 return ret;
Mark Brown3cc72982012-06-19 16:31:53 +0100294}
295
Charles Keepax22298752015-05-11 13:58:02 +0100296static inline void arizona_enable_reset(struct arizona *arizona)
297{
298 if (arizona->pdata.reset)
Charles Keepaxc1860462018-03-12 15:52:00 +0000299 gpiod_set_raw_value_cansleep(arizona->pdata.reset, 0);
Charles Keepax22298752015-05-11 13:58:02 +0100300}
301
302static void arizona_disable_reset(struct arizona *arizona)
303{
304 if (arizona->pdata.reset) {
Charles Keepax121c0752015-05-11 13:58:10 +0100305 switch (arizona->type) {
306 case WM5110:
307 case WM8280:
308 /* Meet requirements for minimum reset duration */
Lee Jonesb79a9802015-10-28 12:42:30 +0000309 usleep_range(5000, 10000);
Charles Keepax121c0752015-05-11 13:58:10 +0100310 break;
311 default:
312 break;
313 }
314
Charles Keepaxc1860462018-03-12 15:52:00 +0000315 gpiod_set_raw_value_cansleep(arizona->pdata.reset, 1);
Lee Jonesb79a9802015-10-28 12:42:30 +0000316 usleep_range(1000, 5000);
Charles Keepax22298752015-05-11 13:58:02 +0100317 }
318}
319
Charles Keepax3850e3e2015-05-11 13:58:05 +0100320struct arizona_sysclk_state {
321 unsigned int fll;
322 unsigned int sysclk;
323};
324
325static int arizona_enable_freerun_sysclk(struct arizona *arizona,
326 struct arizona_sysclk_state *state)
Charles Keepaxe80436b2013-03-26 18:46:15 +0000327{
Charles Keepaxe80436b2013-03-26 18:46:15 +0000328 int ret, err;
329
Charles Keepaxe80436b2013-03-26 18:46:15 +0000330 /* Cache existing FLL and SYSCLK settings */
Charles Keepax3850e3e2015-05-11 13:58:05 +0100331 ret = regmap_read(arizona->regmap, ARIZONA_FLL1_CONTROL_1, &state->fll);
Charles Keepax0be068a2015-05-11 13:58:04 +0100332 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000333 dev_err(arizona->dev, "Failed to cache FLL settings: %d\n",
334 ret);
335 return ret;
336 }
Charles Keepax3850e3e2015-05-11 13:58:05 +0100337 ret = regmap_read(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1,
338 &state->sysclk);
Charles Keepax0be068a2015-05-11 13:58:04 +0100339 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000340 dev_err(arizona->dev, "Failed to cache SYSCLK settings: %d\n",
341 ret);
342 return ret;
343 }
344
345 /* Start up SYSCLK using the FLL in free running mode */
346 ret = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1,
347 ARIZONA_FLL1_ENA | ARIZONA_FLL1_FREERUN);
Charles Keepax0be068a2015-05-11 13:58:04 +0100348 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000349 dev_err(arizona->dev,
350 "Failed to start FLL in freerunning mode: %d\n",
351 ret);
352 return ret;
353 }
Charles Keepaxef84f882017-03-15 14:58:38 +0000354 ret = arizona_poll_reg(arizona, 180, ARIZONA_INTERRUPT_RAW_STATUS_5,
Charles Keepaxe80436b2013-03-26 18:46:15 +0000355 ARIZONA_FLL1_CLOCK_OK_STS,
356 ARIZONA_FLL1_CLOCK_OK_STS);
Charles Keepaxde4ea102017-03-15 14:58:36 +0000357 if (ret)
Charles Keepaxe80436b2013-03-26 18:46:15 +0000358 goto err_fll;
Charles Keepaxe80436b2013-03-26 18:46:15 +0000359
360 ret = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1, 0x0144);
Charles Keepax0be068a2015-05-11 13:58:04 +0100361 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000362 dev_err(arizona->dev, "Failed to start SYSCLK: %d\n", ret);
363 goto err_fll;
364 }
365
Charles Keepax3850e3e2015-05-11 13:58:05 +0100366 return 0;
367
368err_fll:
369 err = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, state->fll);
370 if (err)
371 dev_err(arizona->dev,
372 "Failed to re-apply old FLL settings: %d\n", err);
373
374 return ret;
375}
376
377static int arizona_disable_freerun_sysclk(struct arizona *arizona,
378 struct arizona_sysclk_state *state)
379{
380 int ret;
381
382 ret = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1,
383 state->sysclk);
384 if (ret) {
385 dev_err(arizona->dev,
386 "Failed to re-apply old SYSCLK settings: %d\n", ret);
387 return ret;
388 }
389
390 ret = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, state->fll);
391 if (ret) {
392 dev_err(arizona->dev,
393 "Failed to re-apply old FLL settings: %d\n", ret);
394 return ret;
395 }
396
397 return 0;
398}
399
400static int wm5102_apply_hardware_patch(struct arizona *arizona)
401{
402 struct arizona_sysclk_state state;
403 int err, ret;
404
405 ret = arizona_enable_freerun_sysclk(arizona, &state);
406 if (ret)
407 return ret;
408
Charles Keepaxe80436b2013-03-26 18:46:15 +0000409 /* Start the write sequencer and wait for it to finish */
410 ret = regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0,
Charles Keepax0be068a2015-05-11 13:58:04 +0100411 ARIZONA_WSEQ_ENA | ARIZONA_WSEQ_START | 160);
412 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000413 dev_err(arizona->dev, "Failed to start write sequencer: %d\n",
414 ret);
Charles Keepax3850e3e2015-05-11 13:58:05 +0100415 goto err;
Charles Keepaxe80436b2013-03-26 18:46:15 +0000416 }
Charles Keepax3850e3e2015-05-11 13:58:05 +0100417
Charles Keepaxef84f882017-03-15 14:58:38 +0000418 ret = arizona_poll_reg(arizona, 30, ARIZONA_WRITE_SEQUENCER_CTRL_1,
Charles Keepaxe80436b2013-03-26 18:46:15 +0000419 ARIZONA_WSEQ_BUSY, 0);
Charles Keepaxde4ea102017-03-15 14:58:36 +0000420 if (ret)
Charles Keepaxe80436b2013-03-26 18:46:15 +0000421 regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0,
Charles Keepax0be068a2015-05-11 13:58:04 +0100422 ARIZONA_WSEQ_ABORT);
Charles Keepaxe80436b2013-03-26 18:46:15 +0000423
Charles Keepax3850e3e2015-05-11 13:58:05 +0100424err:
425 err = arizona_disable_freerun_sysclk(arizona, &state);
Charles Keepaxe80436b2013-03-26 18:46:15 +0000426
Charles Keepax0be068a2015-05-11 13:58:04 +0100427 return ret ?: err;
Charles Keepaxe80436b2013-03-26 18:46:15 +0000428}
429
Charles Keepax882bc462015-05-11 13:58:06 +0100430/*
431 * Register patch to some of the CODECs internal write sequences
432 * to ensure a clean exit from the low power sleep state.
433 */
Nariman Poushin8019ff62015-07-16 16:36:21 +0100434static const struct reg_sequence wm5110_sleep_patch[] = {
Charles Keepax882bc462015-05-11 13:58:06 +0100435 { 0x337A, 0xC100 },
436 { 0x337B, 0x0041 },
437 { 0x3300, 0xA210 },
438 { 0x3301, 0x050C },
439};
440
441static int wm5110_apply_sleep_patch(struct arizona *arizona)
442{
443 struct arizona_sysclk_state state;
444 int err, ret;
445
446 ret = arizona_enable_freerun_sysclk(arizona, &state);
447 if (ret)
448 return ret;
449
450 ret = regmap_multi_reg_write_bypassed(arizona->regmap,
451 wm5110_sleep_patch,
452 ARRAY_SIZE(wm5110_sleep_patch));
453
454 err = arizona_disable_freerun_sysclk(arizona, &state);
455
456 return ret ?: err;
457}
458
Charles Keepax1c1c6bb2015-05-11 13:58:03 +0100459static int wm5102_clear_write_sequencer(struct arizona *arizona)
460{
461 int ret;
462
463 ret = regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_3,
464 0x0);
465 if (ret) {
466 dev_err(arizona->dev,
467 "Failed to clear write sequencer state: %d\n", ret);
468 return ret;
469 }
470
471 arizona_enable_reset(arizona);
472 regulator_disable(arizona->dcvdd);
473
474 msleep(20);
475
476 ret = regulator_enable(arizona->dcvdd);
477 if (ret) {
478 dev_err(arizona->dev, "Failed to re-enable DCVDD: %d\n", ret);
479 return ret;
480 }
481 arizona_disable_reset(arizona);
482
483 return 0;
484}
485
Rafael J. Wysocki48bb9fe2014-12-05 03:04:12 +0100486#ifdef CONFIG_PM
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100487static int arizona_isolate_dcvdd(struct arizona *arizona)
488{
489 int ret;
490
491 ret = regmap_update_bits(arizona->regmap,
492 ARIZONA_ISOLATION_CONTROL,
493 ARIZONA_ISOLATE_DCVDD1,
494 ARIZONA_ISOLATE_DCVDD1);
495 if (ret != 0)
496 dev_err(arizona->dev, "Failed to isolate DCVDD: %d\n", ret);
497
498 return ret;
499}
500
501static int arizona_connect_dcvdd(struct arizona *arizona)
502{
503 int ret;
504
505 ret = regmap_update_bits(arizona->regmap,
506 ARIZONA_ISOLATION_CONTROL,
507 ARIZONA_ISOLATE_DCVDD1, 0);
508 if (ret != 0)
509 dev_err(arizona->dev, "Failed to connect DCVDD: %d\n", ret);
510
511 return ret;
512}
513
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100514static int arizona_is_jack_det_active(struct arizona *arizona)
515{
516 unsigned int val;
517 int ret;
518
519 ret = regmap_read(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE, &val);
520 if (ret) {
521 dev_err(arizona->dev,
522 "Failed to check jack det status: %d\n", ret);
523 return ret;
524 } else if (val & ARIZONA_JD1_ENA) {
525 return 1;
526 } else {
527 return 0;
528 }
529}
530
Mark Brown3cc72982012-06-19 16:31:53 +0100531static int arizona_runtime_resume(struct device *dev)
532{
533 struct arizona *arizona = dev_get_drvdata(dev);
534 int ret;
535
Mark Brown508c8292012-07-20 17:09:12 +0100536 dev_dbg(arizona->dev, "Leaving AoD mode\n");
537
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100538 if (arizona->has_fully_powered_off) {
539 dev_dbg(arizona->dev, "Re-enabling core supplies\n");
540
541 ret = regulator_bulk_enable(arizona->num_core_supplies,
542 arizona->core_supplies);
543 if (ret) {
544 dev_err(dev, "Failed to enable core supplies: %d\n",
545 ret);
546 return ret;
547 }
548 }
549
Mark Brown59db9692012-07-09 00:31:36 +0200550 ret = regulator_enable(arizona->dcvdd);
551 if (ret != 0) {
552 dev_err(arizona->dev, "Failed to enable DCVDD: %d\n", ret);
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100553 if (arizona->has_fully_powered_off)
554 regulator_bulk_disable(arizona->num_core_supplies,
555 arizona->core_supplies);
Mark Brown59db9692012-07-09 00:31:36 +0200556 return ret;
557 }
Mark Brown3cc72982012-06-19 16:31:53 +0100558
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100559 if (arizona->has_fully_powered_off) {
560 arizona_disable_reset(arizona);
561 enable_irq(arizona->irq);
562 arizona->has_fully_powered_off = false;
563 }
564
Mark Brown3cc72982012-06-19 16:31:53 +0100565 regcache_cache_only(arizona->regmap, false);
566
Charles Keepax4c9bb8b2013-03-26 18:01:49 +0000567 switch (arizona->type) {
568 case WM5102:
Mark Brown59274672013-04-23 19:44:16 +0100569 if (arizona->external_dcvdd) {
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100570 ret = arizona_connect_dcvdd(arizona);
571 if (ret != 0)
Mark Brown59274672013-04-23 19:44:16 +0100572 goto err;
Mark Brown59274672013-04-23 19:44:16 +0100573 }
574
Charles Keepax4c9bb8b2013-03-26 18:01:49 +0000575 ret = wm5102_patch(arizona);
576 if (ret != 0) {
577 dev_err(arizona->dev, "Failed to apply patch: %d\n",
578 ret);
579 goto err;
580 }
Charles Keepaxe80436b2013-03-26 18:46:15 +0000581
Charles Keepax0be068a2015-05-11 13:58:04 +0100582 ret = wm5102_apply_hardware_patch(arizona);
583 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000584 dev_err(arizona->dev,
585 "Failed to apply hardware patch: %d\n",
586 ret);
587 goto err;
588 }
589 break;
Charles Keepax96129a02015-05-11 13:58:08 +0100590 case WM5110:
591 case WM8280:
592 ret = arizona_wait_for_boot(arizona);
593 if (ret)
594 goto err;
595
596 if (arizona->external_dcvdd) {
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100597 ret = arizona_connect_dcvdd(arizona);
598 if (ret != 0)
Charles Keepax96129a02015-05-11 13:58:08 +0100599 goto err;
Charles Keepax96129a02015-05-11 13:58:08 +0100600 } else {
601 /*
602 * As this is only called for the internal regulator
603 * (where we know voltage ranges available) it is ok
604 * to request an exact range.
605 */
606 ret = regulator_set_voltage(arizona->dcvdd,
607 1200000, 1200000);
608 if (ret < 0) {
609 dev_err(arizona->dev,
610 "Failed to set resume voltage: %d\n",
611 ret);
612 goto err;
613 }
614 }
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100615
616 ret = wm5110_apply_sleep_patch(arizona);
617 if (ret) {
618 dev_err(arizona->dev,
619 "Failed to re-apply sleep patch: %d\n",
620 ret);
621 goto err;
622 }
Charles Keepax96129a02015-05-11 13:58:08 +0100623 break;
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000624 case WM1831:
625 case CS47L24:
626 ret = arizona_wait_for_boot(arizona);
627 if (ret != 0)
628 goto err;
629 break;
Charles Keepaxe80436b2013-03-26 18:46:15 +0000630 default:
Charles Keepax12bb68e2013-03-27 09:49:40 +0000631 ret = arizona_wait_for_boot(arizona);
Charles Keepax3762aed2015-08-11 09:34:31 +0100632 if (ret != 0)
Charles Keepax12bb68e2013-03-27 09:49:40 +0000633 goto err;
Charles Keepax12bb68e2013-03-27 09:49:40 +0000634
Mark Brown59274672013-04-23 19:44:16 +0100635 if (arizona->external_dcvdd) {
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100636 ret = arizona_connect_dcvdd(arizona);
637 if (ret != 0)
Mark Brown59274672013-04-23 19:44:16 +0100638 goto err;
Mark Brown59274672013-04-23 19:44:16 +0100639 }
Charles Keepaxe80436b2013-03-26 18:46:15 +0000640 break;
Charles Keepax4c9bb8b2013-03-26 18:01:49 +0000641 }
642
Mark Brown9270bdf2013-01-04 17:16:12 +0000643 ret = regcache_sync(arizona->regmap);
644 if (ret != 0) {
645 dev_err(arizona->dev, "Failed to restore register cache\n");
Mark Brown4816bd12013-01-14 15:50:38 +0900646 goto err;
Mark Brown9270bdf2013-01-04 17:16:12 +0000647 }
Mark Brown3cc72982012-06-19 16:31:53 +0100648
649 return 0;
Mark Brown4816bd12013-01-14 15:50:38 +0900650
651err:
652 regcache_cache_only(arizona->regmap, true);
653 regulator_disable(arizona->dcvdd);
654 return ret;
Mark Brown3cc72982012-06-19 16:31:53 +0100655}
656
657static int arizona_runtime_suspend(struct device *dev)
658{
659 struct arizona *arizona = dev_get_drvdata(dev);
Dan Carpentera05950a2015-10-19 13:18:05 +0300660 int jd_active = 0;
Mark Brown59274672013-04-23 19:44:16 +0100661 int ret;
Mark Brown3cc72982012-06-19 16:31:53 +0100662
Mark Brown508c8292012-07-20 17:09:12 +0100663 dev_dbg(arizona->dev, "Entering AoD mode\n");
664
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100665 switch (arizona->type) {
666 case WM5110:
667 case WM8280:
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100668 jd_active = arizona_is_jack_det_active(arizona);
669 if (jd_active < 0)
670 return jd_active;
671
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100672 if (arizona->external_dcvdd) {
673 ret = arizona_isolate_dcvdd(arizona);
674 if (ret != 0)
675 return ret;
676 } else {
677 /*
678 * As this is only called for the internal regulator
679 * (where we know voltage ranges available) it is ok
680 * to request an exact range.
681 */
682 ret = regulator_set_voltage(arizona->dcvdd,
683 1175000, 1175000);
684 if (ret < 0) {
685 dev_err(arizona->dev,
686 "Failed to set suspend voltage: %d\n",
687 ret);
688 return ret;
689 }
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100690 }
691 break;
692 case WM5102:
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100693 jd_active = arizona_is_jack_det_active(arizona);
694 if (jd_active < 0)
695 return jd_active;
696
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100697 if (arizona->external_dcvdd) {
698 ret = arizona_isolate_dcvdd(arizona);
699 if (ret != 0)
700 return ret;
701 }
702
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100703 if (!jd_active) {
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100704 ret = regmap_write(arizona->regmap,
705 ARIZONA_WRITE_SEQUENCER_CTRL_3, 0x0);
706 if (ret) {
Charles Keepax96129a02015-05-11 13:58:08 +0100707 dev_err(arizona->dev,
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100708 "Failed to clear write sequencer: %d\n",
Charles Keepax96129a02015-05-11 13:58:08 +0100709 ret);
710 return ret;
711 }
Charles Keepax96129a02015-05-11 13:58:08 +0100712 }
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100713 break;
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000714 case WM1831:
715 case CS47L24:
716 break;
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100717 default:
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100718 jd_active = arizona_is_jack_det_active(arizona);
719 if (jd_active < 0)
720 return jd_active;
721
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100722 if (arizona->external_dcvdd) {
723 ret = arizona_isolate_dcvdd(arizona);
724 if (ret != 0)
725 return ret;
726 }
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100727 break;
Mark Brown59274672013-04-23 19:44:16 +0100728 }
729
Mark Brown59db9692012-07-09 00:31:36 +0200730 regcache_cache_only(arizona->regmap, true);
731 regcache_mark_dirty(arizona->regmap);
Charles Keepaxe293e84722013-08-06 17:18:35 +0100732 regulator_disable(arizona->dcvdd);
Mark Brown3cc72982012-06-19 16:31:53 +0100733
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100734 /* Allow us to completely power down if no jack detection */
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100735 if (!jd_active) {
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100736 dev_dbg(arizona->dev, "Fully powering off\n");
737
738 arizona->has_fully_powered_off = true;
739
Charles Keepax11150922015-06-14 15:41:49 +0100740 disable_irq_nosync(arizona->irq);
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100741 arizona_enable_reset(arizona);
742 regulator_bulk_disable(arizona->num_core_supplies,
743 arizona->core_supplies);
744 }
745
Mark Brown3cc72982012-06-19 16:31:53 +0100746 return 0;
747}
748#endif
749
Mark Browndc781d02013-01-27 12:07:32 +0800750#ifdef CONFIG_PM_SLEEP
Mark Brown67c99292013-04-10 12:40:26 +0100751static int arizona_suspend(struct device *dev)
752{
753 struct arizona *arizona = dev_get_drvdata(dev);
754
755 dev_dbg(arizona->dev, "Suspend, disabling IRQ\n");
756 disable_irq(arizona->irq);
757
758 return 0;
759}
760
Charles Keepax3612b272016-08-30 10:33:10 +0100761static int arizona_suspend_noirq(struct device *dev)
Mark Brown67c99292013-04-10 12:40:26 +0100762{
763 struct arizona *arizona = dev_get_drvdata(dev);
764
765 dev_dbg(arizona->dev, "Late suspend, reenabling IRQ\n");
766 enable_irq(arizona->irq);
767
768 return 0;
769}
770
Mark Browndc781d02013-01-27 12:07:32 +0800771static int arizona_resume_noirq(struct device *dev)
772{
773 struct arizona *arizona = dev_get_drvdata(dev);
774
775 dev_dbg(arizona->dev, "Early resume, disabling IRQ\n");
776 disable_irq(arizona->irq);
777
778 return 0;
779}
780
781static int arizona_resume(struct device *dev)
782{
783 struct arizona *arizona = dev_get_drvdata(dev);
784
Charles Keepax3612b272016-08-30 10:33:10 +0100785 dev_dbg(arizona->dev, "Resume, reenabling IRQ\n");
Mark Browndc781d02013-01-27 12:07:32 +0800786 enable_irq(arizona->irq);
787
788 return 0;
789}
790#endif
791
Mark Brown3cc72982012-06-19 16:31:53 +0100792const struct dev_pm_ops arizona_pm_ops = {
793 SET_RUNTIME_PM_OPS(arizona_runtime_suspend,
794 arizona_runtime_resume,
795 NULL)
Mark Brown67c99292013-04-10 12:40:26 +0100796 SET_SYSTEM_SLEEP_PM_OPS(arizona_suspend, arizona_resume)
Charles Keepax3612b272016-08-30 10:33:10 +0100797 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(arizona_suspend_noirq,
798 arizona_resume_noirq)
Mark Brown3cc72982012-06-19 16:31:53 +0100799};
800EXPORT_SYMBOL_GPL(arizona_pm_ops);
801
Mark Brownd7810092013-03-25 00:11:27 +0000802#ifdef CONFIG_OF
Lee Jones942786e2014-07-02 14:28:46 +0100803unsigned long arizona_of_get_type(struct device *dev)
Mark Brownd7810092013-03-25 00:11:27 +0000804{
805 const struct of_device_id *id = of_match_device(arizona_of_match, dev);
806
807 if (id)
Lee Jones942786e2014-07-02 14:28:46 +0100808 return (unsigned long)id->data;
Mark Brownd7810092013-03-25 00:11:27 +0000809 else
810 return 0;
811}
812EXPORT_SYMBOL_GPL(arizona_of_get_type);
813
814static int arizona_of_get_core_pdata(struct arizona *arizona)
815{
Charles Keepaxe4fcb1d2014-04-16 10:01:37 +0100816 struct arizona_pdata *pdata = &arizona->pdata;
Mark Brownd7810092013-03-25 00:11:27 +0000817 int ret, i;
818
Charles Keepaxc1860462018-03-12 15:52:00 +0000819 /* Handle old non-standard DT binding */
820 pdata->reset = devm_gpiod_get_from_of_node(arizona->dev,
821 arizona->dev->of_node,
822 "wlf,reset", 0,
823 GPIOD_OUT_LOW,
824 "arizona /RESET");
825 if (IS_ERR(pdata->reset)) {
826 ret = PTR_ERR(pdata->reset);
Charles Keepax1961531d2016-09-20 16:30:13 +0100827
Charles Keepaxc1860462018-03-12 15:52:00 +0000828 /*
829 * Reset missing will be caught when other binding is read
830 * but all other errors imply this binding is in use but has
831 * encountered a problem so should be handled.
832 */
833 if (ret == -EPROBE_DEFER)
834 return ret;
835 else if (ret != -ENOENT && ret != -ENOSYS)
836 dev_err(arizona->dev, "Reset GPIO malformed: %d\n",
837 ret);
838
839 pdata->reset = NULL;
Charles Keepax1961531d2016-09-20 16:30:13 +0100840 }
Mark Brownd7810092013-03-25 00:11:27 +0000841
842 ret = of_property_read_u32_array(arizona->dev->of_node,
843 "wlf,gpio-defaults",
Charles Keepax3762aed2015-08-11 09:34:31 +0100844 pdata->gpio_defaults,
845 ARRAY_SIZE(pdata->gpio_defaults));
Mark Brownd7810092013-03-25 00:11:27 +0000846 if (ret >= 0) {
847 /*
848 * All values are literal except out of range values
849 * which are chip default, translate into platform
850 * data which uses 0 as chip default and out of range
851 * as zero.
852 */
Charles Keepax3762aed2015-08-11 09:34:31 +0100853 for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
854 if (pdata->gpio_defaults[i] > 0xffff)
855 pdata->gpio_defaults[i] = 0;
856 else if (pdata->gpio_defaults[i] == 0)
857 pdata->gpio_defaults[i] = 0x10000;
Mark Brownd7810092013-03-25 00:11:27 +0000858 }
859 } else {
860 dev_err(arizona->dev, "Failed to parse GPIO defaults: %d\n",
861 ret);
862 }
863
864 return 0;
865}
866
867const struct of_device_id arizona_of_match[] = {
868 { .compatible = "wlf,wm5102", .data = (void *)WM5102 },
869 { .compatible = "wlf,wm5110", .data = (void *)WM5110 },
Richard Fitzgeralde5d4ef02015-01-17 15:21:22 +0000870 { .compatible = "wlf,wm8280", .data = (void *)WM8280 },
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100871 { .compatible = "wlf,wm8997", .data = (void *)WM8997 },
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100872 { .compatible = "wlf,wm8998", .data = (void *)WM8998 },
873 { .compatible = "wlf,wm1814", .data = (void *)WM1814 },
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000874 { .compatible = "wlf,wm1831", .data = (void *)WM1831 },
875 { .compatible = "cirrus,cs47l24", .data = (void *)CS47L24 },
Mark Brownd7810092013-03-25 00:11:27 +0000876 {},
877};
878EXPORT_SYMBOL_GPL(arizona_of_match);
879#else
880static inline int arizona_of_get_core_pdata(struct arizona *arizona)
881{
882 return 0;
883}
884#endif
885
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +0100886static const struct mfd_cell early_devs[] = {
Mark Brown3cc72982012-06-19 16:31:53 +0100887 { .name = "arizona-ldo1" },
888};
889
Charles Keepax3762aed2015-08-11 09:34:31 +0100890static const char * const wm5102_supplies[] = {
Charles Keepax5fc6c392014-07-25 16:24:44 +0100891 "MICVDD",
Charles Keepax32dadef2013-10-15 20:14:22 +0100892 "DBVDD2",
893 "DBVDD3",
894 "CPVDD",
895 "SPKVDDL",
896 "SPKVDDR",
897};
898
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +0100899static const struct mfd_cell wm5102_devs[] = {
Mark Brownd7768112012-12-20 15:38:03 +0000900 { .name = "arizona-micsupp" },
Charles Keepaxf83c2182016-04-15 13:18:47 +0100901 { .name = "arizona-gpio" },
Charles Keepax5fc6c392014-07-25 16:24:44 +0100902 {
903 .name = "arizona-extcon",
904 .parent_supplies = wm5102_supplies,
905 .num_parent_supplies = 1, /* We only need MICVDD */
906 },
Mark Brown503b1ca2012-11-27 17:36:38 +0000907 { .name = "arizona-haptics" },
Mark Brown3cc72982012-06-19 16:31:53 +0100908 { .name = "arizona-pwm" },
Charles Keepax32dadef2013-10-15 20:14:22 +0100909 {
910 .name = "wm5102-codec",
911 .parent_supplies = wm5102_supplies,
912 .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
913 },
Mark Brown3cc72982012-06-19 16:31:53 +0100914};
915
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +0100916static const struct mfd_cell wm5110_devs[] = {
Mark Brownd7768112012-12-20 15:38:03 +0000917 { .name = "arizona-micsupp" },
Charles Keepaxf83c2182016-04-15 13:18:47 +0100918 { .name = "arizona-gpio" },
Charles Keepax5fc6c392014-07-25 16:24:44 +0100919 {
920 .name = "arizona-extcon",
921 .parent_supplies = wm5102_supplies,
922 .num_parent_supplies = 1, /* We only need MICVDD */
923 },
Mark Brown503b1ca2012-11-27 17:36:38 +0000924 { .name = "arizona-haptics" },
Mark Browne102bef2012-07-10 12:37:58 +0100925 { .name = "arizona-pwm" },
Charles Keepax32dadef2013-10-15 20:14:22 +0100926 {
927 .name = "wm5110-codec",
928 .parent_supplies = wm5102_supplies,
929 .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
930 },
931};
932
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000933static const char * const cs47l24_supplies[] = {
934 "MICVDD",
935 "CPVDD",
936 "SPKVDD",
937};
938
939static const struct mfd_cell cs47l24_devs[] = {
940 { .name = "arizona-gpio" },
941 { .name = "arizona-haptics" },
942 { .name = "arizona-pwm" },
943 {
944 .name = "cs47l24-codec",
945 .parent_supplies = cs47l24_supplies,
946 .num_parent_supplies = ARRAY_SIZE(cs47l24_supplies),
947 },
948};
949
Charles Keepax3762aed2015-08-11 09:34:31 +0100950static const char * const wm8997_supplies[] = {
Charles Keepax996c2d42014-07-25 16:24:43 +0100951 "MICVDD",
Charles Keepax32dadef2013-10-15 20:14:22 +0100952 "DBVDD2",
953 "CPVDD",
954 "SPKVDD",
Mark Browne102bef2012-07-10 12:37:58 +0100955};
956
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +0100957static const struct mfd_cell wm8997_devs[] = {
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100958 { .name = "arizona-micsupp" },
Charles Keepaxf83c2182016-04-15 13:18:47 +0100959 { .name = "arizona-gpio" },
Charles Keepax5fc6c392014-07-25 16:24:44 +0100960 {
961 .name = "arizona-extcon",
962 .parent_supplies = wm8997_supplies,
963 .num_parent_supplies = 1, /* We only need MICVDD */
964 },
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100965 { .name = "arizona-haptics" },
966 { .name = "arizona-pwm" },
Charles Keepax32dadef2013-10-15 20:14:22 +0100967 {
968 .name = "wm8997-codec",
969 .parent_supplies = wm8997_supplies,
970 .num_parent_supplies = ARRAY_SIZE(wm8997_supplies),
971 },
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100972};
973
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100974static const struct mfd_cell wm8998_devs[] = {
Charles Keepaxf83c2182016-04-15 13:18:47 +0100975 { .name = "arizona-micsupp" },
976 { .name = "arizona-gpio" },
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100977 {
978 .name = "arizona-extcon",
979 .parent_supplies = wm5102_supplies,
980 .num_parent_supplies = 1, /* We only need MICVDD */
981 },
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100982 { .name = "arizona-haptics" },
983 { .name = "arizona-pwm" },
984 {
985 .name = "wm8998-codec",
986 .parent_supplies = wm5102_supplies,
987 .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
988 },
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100989};
990
Bill Pembertonf791be42012-11-19 13:23:04 -0500991int arizona_dev_init(struct arizona *arizona)
Mark Brown3cc72982012-06-19 16:31:53 +0100992{
Colin Ian King8e27a562018-09-04 15:15:53 +0100993 static const char * const mclk_name[] = { "mclk1", "mclk2" };
Mark Brown3cc72982012-06-19 16:31:53 +0100994 struct device *dev = arizona->dev;
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000995 const char *type_name = NULL;
Charles Keepax6000c992017-09-04 16:41:51 +0100996 unsigned int reg, val;
Mark Brown62d62b52012-12-02 11:41:46 +0900997 int (*apply_patch)(struct arizona *) = NULL;
Richard Fitzgeraldae05ea362015-10-02 13:29:13 +0100998 const struct mfd_cell *subdevs = NULL;
999 int n_subdevs, ret, i;
Mark Brown3cc72982012-06-19 16:31:53 +01001000
1001 dev_set_drvdata(arizona->dev, arizona);
1002 mutex_init(&arizona->clk_lock);
1003
Charles Keepaxb8d336e2016-09-20 16:30:14 +01001004 if (dev_get_platdata(arizona->dev)) {
Mark Brown3cc72982012-06-19 16:31:53 +01001005 memcpy(&arizona->pdata, dev_get_platdata(arizona->dev),
1006 sizeof(arizona->pdata));
Charles Keepaxb8d336e2016-09-20 16:30:14 +01001007 } else {
1008 ret = arizona_of_get_core_pdata(arizona);
1009 if (ret < 0)
1010 return ret;
1011 }
Mark Brown3cc72982012-06-19 16:31:53 +01001012
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +01001013 BUILD_BUG_ON(ARRAY_SIZE(arizona->mclk) != ARRAY_SIZE(mclk_name));
1014 for (i = 0; i < ARRAY_SIZE(arizona->mclk); i++) {
1015 arizona->mclk[i] = devm_clk_get(arizona->dev, mclk_name[i]);
1016 if (IS_ERR(arizona->mclk[i])) {
1017 dev_info(arizona->dev, "Failed to get %s: %ld\n",
1018 mclk_name[i], PTR_ERR(arizona->mclk[i]));
1019 arizona->mclk[i] = NULL;
1020 }
1021 }
1022
Mark Brown3cc72982012-06-19 16:31:53 +01001023 regcache_cache_only(arizona->regmap, true);
1024
1025 switch (arizona->type) {
1026 case WM5102:
Mark Browne102bef2012-07-10 12:37:58 +01001027 case WM5110:
Richard Fitzgeralde5d4ef02015-01-17 15:21:22 +00001028 case WM8280:
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001029 case WM8997:
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001030 case WM8998:
1031 case WM1814:
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +00001032 case WM1831:
1033 case CS47L24:
Mark Brown3cc72982012-06-19 16:31:53 +01001034 for (i = 0; i < ARRAY_SIZE(wm5102_core_supplies); i++)
1035 arizona->core_supplies[i].supply
1036 = wm5102_core_supplies[i];
1037 arizona->num_core_supplies = ARRAY_SIZE(wm5102_core_supplies);
1038 break;
1039 default:
1040 dev_err(arizona->dev, "Unknown device type %d\n",
1041 arizona->type);
Charles Keepax75d8a2b2016-08-31 10:41:30 +01001042 return -ENODEV;
Mark Brown3cc72982012-06-19 16:31:53 +01001043 }
1044
Charles Keepax4a8c4752014-04-16 10:01:38 +01001045 /* Mark DCVDD as external, LDO1 driver will clear if internal */
1046 arizona->external_dcvdd = true;
1047
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +00001048 switch (arizona->type) {
1049 case WM1831:
1050 case CS47L24:
1051 break; /* No LDO1 regulator */
1052 default:
1053 ret = mfd_add_devices(arizona->dev, -1, early_devs,
1054 ARRAY_SIZE(early_devs), NULL, 0, NULL);
1055 if (ret != 0) {
1056 dev_err(dev, "Failed to add early children: %d\n", ret);
1057 return ret;
1058 }
1059 break;
Mark Brown3cc72982012-06-19 16:31:53 +01001060 }
1061
1062 ret = devm_regulator_bulk_get(dev, arizona->num_core_supplies,
1063 arizona->core_supplies);
1064 if (ret != 0) {
1065 dev_err(dev, "Failed to request core supplies: %d\n",
1066 ret);
1067 goto err_early;
1068 }
1069
Charles Keepax0c2d0ff2014-06-19 16:04:23 +01001070 /**
1071 * Don't use devres here because the only device we have to get
1072 * against is the MFD device and DCVDD will likely be supplied by
1073 * one of its children. Meaning that the regulator will be
1074 * destroyed by the time devres calls regulator put.
1075 */
Charles Keepaxe6021512014-06-02 09:50:41 +01001076 arizona->dcvdd = regulator_get(arizona->dev, "DCVDD");
Mark Brown59db9692012-07-09 00:31:36 +02001077 if (IS_ERR(arizona->dcvdd)) {
1078 ret = PTR_ERR(arizona->dcvdd);
1079 dev_err(dev, "Failed to request DCVDD: %d\n", ret);
1080 goto err_early;
1081 }
1082
Charles Keepaxc1860462018-03-12 15:52:00 +00001083 if (!arizona->pdata.reset) {
Mark Brown87d3af42013-03-26 12:15:26 +00001084 /* Start out with /RESET low to put the chip into reset */
Charles Keepaxc1860462018-03-12 15:52:00 +00001085 arizona->pdata.reset = devm_gpiod_get(arizona->dev, "reset",
1086 GPIOD_OUT_LOW);
1087 if (IS_ERR(arizona->pdata.reset)) {
1088 ret = PTR_ERR(arizona->pdata.reset);
1089 if (ret == -EPROBE_DEFER)
1090 goto err_dcvdd;
1091
1092 dev_err(arizona->dev,
1093 "Reset GPIO missing/malformed: %d\n", ret);
1094
1095 arizona->pdata.reset = NULL;
Mark Brown87d3af42013-03-26 12:15:26 +00001096 }
1097 }
1098
Mark Brown3cc72982012-06-19 16:31:53 +01001099 ret = regulator_bulk_enable(arizona->num_core_supplies,
1100 arizona->core_supplies);
1101 if (ret != 0) {
1102 dev_err(dev, "Failed to enable core supplies: %d\n",
1103 ret);
Charles Keepaxe6021512014-06-02 09:50:41 +01001104 goto err_dcvdd;
Mark Brown3cc72982012-06-19 16:31:53 +01001105 }
1106
Mark Brown59db9692012-07-09 00:31:36 +02001107 ret = regulator_enable(arizona->dcvdd);
1108 if (ret != 0) {
1109 dev_err(dev, "Failed to enable DCVDD: %d\n", ret);
1110 goto err_enable;
1111 }
1112
Charles Keepax22298752015-05-11 13:58:02 +01001113 arizona_disable_reset(arizona);
Mark Brown3cc72982012-06-19 16:31:53 +01001114
Mark Brown3cc72982012-06-19 16:31:53 +01001115 regcache_cache_only(arizona->regmap, false);
1116
Mark Brownca76ceb2013-04-09 16:04:35 +01001117 /* Verify that this is a chip we know about */
1118 ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, &reg);
1119 if (ret != 0) {
1120 dev_err(dev, "Failed to read ID register: %d\n", ret);
1121 goto err_reset;
1122 }
1123
1124 switch (reg) {
1125 case 0x5102:
1126 case 0x5110:
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001127 case 0x6349:
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +00001128 case 0x6363:
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001129 case 0x8997:
Mark Brownca76ceb2013-04-09 16:04:35 +01001130 break;
1131 default:
1132 dev_err(arizona->dev, "Unknown device ID: %x\n", reg);
Charles Keepax75d8a2b2016-08-31 10:41:30 +01001133 ret = -ENODEV;
Mark Brownca76ceb2013-04-09 16:04:35 +01001134 goto err_reset;
1135 }
1136
1137 /* If we have a /RESET GPIO we'll already be reset */
1138 if (!arizona->pdata.reset) {
Mark Brownca76ceb2013-04-09 16:04:35 +01001139 ret = regmap_write(arizona->regmap, ARIZONA_SOFTWARE_RESET, 0);
1140 if (ret != 0) {
1141 dev_err(dev, "Failed to reset device: %d\n", ret);
1142 goto err_reset;
1143 }
1144
Lee Jonesb79a9802015-10-28 12:42:30 +00001145 usleep_range(1000, 5000);
Mark Brownca76ceb2013-04-09 16:04:35 +01001146 }
1147
1148 /* Ensure device startup is complete */
1149 switch (arizona->type) {
1150 case WM5102:
Mark Brown48018942014-08-13 11:42:46 +01001151 ret = regmap_read(arizona->regmap,
1152 ARIZONA_WRITE_SEQUENCER_CTRL_3, &val);
Charles Keepax1c1c6bb2015-05-11 13:58:03 +01001153 if (ret) {
Mark Brownca76ceb2013-04-09 16:04:35 +01001154 dev_err(dev,
1155 "Failed to check write sequencer state: %d\n",
1156 ret);
Charles Keepax1c1c6bb2015-05-11 13:58:03 +01001157 } else if (val & 0x01) {
1158 ret = wm5102_clear_write_sequencer(arizona);
1159 if (ret)
1160 return ret;
Mark Brownca76ceb2013-04-09 16:04:35 +01001161 }
1162 break;
Charles Keepax1c1c6bb2015-05-11 13:58:03 +01001163 default:
1164 break;
1165 }
1166
1167 ret = arizona_wait_for_boot(arizona);
1168 if (ret) {
1169 dev_err(arizona->dev, "Device failed initial boot: %d\n", ret);
1170 goto err_reset;
Mark Brownca76ceb2013-04-09 16:04:35 +01001171 }
1172
1173 /* Read the device ID information & do device specific stuff */
Mark Brown3cc72982012-06-19 16:31:53 +01001174 ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, &reg);
1175 if (ret != 0) {
1176 dev_err(dev, "Failed to read ID register: %d\n", ret);
Mark Brown59db9692012-07-09 00:31:36 +02001177 goto err_reset;
Mark Brown3cc72982012-06-19 16:31:53 +01001178 }
1179
1180 ret = regmap_read(arizona->regmap, ARIZONA_DEVICE_REVISION,
1181 &arizona->rev);
1182 if (ret != 0) {
1183 dev_err(dev, "Failed to read revision register: %d\n", ret);
Mark Brown59db9692012-07-09 00:31:36 +02001184 goto err_reset;
Mark Brown3cc72982012-06-19 16:31:53 +01001185 }
1186 arizona->rev &= ARIZONA_DEVICE_REVISION_MASK;
1187
1188 switch (reg) {
1189 case 0x5102:
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001190 if (IS_ENABLED(CONFIG_MFD_WM5102)) {
1191 type_name = "WM5102";
1192 if (arizona->type != WM5102) {
1193 dev_warn(arizona->dev,
1194 "WM5102 registered as %d\n",
1195 arizona->type);
1196 arizona->type = WM5102;
1197 }
1198
1199 apply_patch = wm5102_patch;
1200 arizona->rev &= 0x7;
1201 subdevs = wm5102_devs;
1202 n_subdevs = ARRAY_SIZE(wm5102_devs);
Mark Brown3cc72982012-06-19 16:31:53 +01001203 }
Mark Brown3cc72982012-06-19 16:31:53 +01001204 break;
Mark Browne102bef2012-07-10 12:37:58 +01001205 case 0x5110:
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001206 if (IS_ENABLED(CONFIG_MFD_WM5110)) {
1207 switch (arizona->type) {
1208 case WM5110:
1209 type_name = "WM5110";
1210 break;
1211 case WM8280:
1212 type_name = "WM8280";
1213 break;
1214 default:
1215 type_name = "WM5110";
1216 dev_warn(arizona->dev,
1217 "WM5110 registered as %d\n",
1218 arizona->type);
1219 arizona->type = WM5110;
1220 break;
1221 }
1222
1223 apply_patch = wm5110_patch;
1224 subdevs = wm5110_devs;
1225 n_subdevs = ARRAY_SIZE(wm5110_devs);
Mark Browne102bef2012-07-10 12:37:58 +01001226 }
Mark Browne102bef2012-07-10 12:37:58 +01001227 break;
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +00001228 case 0x6363:
1229 if (IS_ENABLED(CONFIG_MFD_CS47L24)) {
1230 switch (arizona->type) {
1231 case CS47L24:
1232 type_name = "CS47L24";
1233 break;
1234
1235 case WM1831:
1236 type_name = "WM1831";
1237 break;
1238
1239 default:
1240 dev_warn(arizona->dev,
1241 "CS47L24 registered as %d\n",
1242 arizona->type);
1243 arizona->type = CS47L24;
1244 break;
1245 }
1246
1247 apply_patch = cs47l24_patch;
1248 subdevs = cs47l24_devs;
1249 n_subdevs = ARRAY_SIZE(cs47l24_devs);
1250 }
1251 break;
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001252 case 0x8997:
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001253 if (IS_ENABLED(CONFIG_MFD_WM8997)) {
1254 type_name = "WM8997";
1255 if (arizona->type != WM8997) {
1256 dev_warn(arizona->dev,
1257 "WM8997 registered as %d\n",
1258 arizona->type);
1259 arizona->type = WM8997;
1260 }
1261
1262 apply_patch = wm8997_patch;
1263 subdevs = wm8997_devs;
1264 n_subdevs = ARRAY_SIZE(wm8997_devs);
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001265 }
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001266 break;
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001267 case 0x6349:
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001268 if (IS_ENABLED(CONFIG_MFD_WM8998)) {
1269 switch (arizona->type) {
1270 case WM8998:
1271 type_name = "WM8998";
1272 break;
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001273
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001274 case WM1814:
1275 type_name = "WM1814";
1276 break;
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001277
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001278 default:
1279 type_name = "WM8998";
1280 dev_warn(arizona->dev,
1281 "WM8998 registered as %d\n",
1282 arizona->type);
1283 arizona->type = WM8998;
1284 }
1285
1286 apply_patch = wm8998_patch;
1287 subdevs = wm8998_devs;
1288 n_subdevs = ARRAY_SIZE(wm8998_devs);
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001289 }
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001290 break;
Mark Brown3cc72982012-06-19 16:31:53 +01001291 default:
1292 dev_err(arizona->dev, "Unknown device ID %x\n", reg);
Charles Keepax75d8a2b2016-08-31 10:41:30 +01001293 ret = -ENODEV;
Mark Brown59db9692012-07-09 00:31:36 +02001294 goto err_reset;
Mark Brown3cc72982012-06-19 16:31:53 +01001295 }
1296
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001297 if (!subdevs) {
1298 dev_err(arizona->dev,
1299 "No kernel support for device ID %x\n", reg);
Charles Keepax75d8a2b2016-08-31 10:41:30 +01001300 ret = -ENODEV;
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001301 goto err_reset;
1302 }
1303
Mark Brown3cc72982012-06-19 16:31:53 +01001304 dev_info(dev, "%s revision %c\n", type_name, arizona->rev + 'A');
1305
Mark Brown62d62b52012-12-02 11:41:46 +09001306 if (apply_patch) {
1307 ret = apply_patch(arizona);
1308 if (ret != 0) {
1309 dev_err(arizona->dev, "Failed to apply patch: %d\n",
1310 ret);
1311 goto err_reset;
1312 }
Charles Keepaxe80436b2013-03-26 18:46:15 +00001313
1314 switch (arizona->type) {
1315 case WM5102:
Charles Keepax0be068a2015-05-11 13:58:04 +01001316 ret = wm5102_apply_hardware_patch(arizona);
1317 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +00001318 dev_err(arizona->dev,
1319 "Failed to apply hardware patch: %d\n",
1320 ret);
1321 goto err_reset;
1322 }
1323 break;
Charles Keepax882bc462015-05-11 13:58:06 +01001324 case WM5110:
1325 case WM8280:
1326 ret = wm5110_apply_sleep_patch(arizona);
1327 if (ret) {
1328 dev_err(arizona->dev,
1329 "Failed to apply sleep patch: %d\n",
1330 ret);
1331 goto err_reset;
1332 }
1333 break;
Charles Keepaxe80436b2013-03-26 18:46:15 +00001334 default:
1335 break;
1336 }
Mark Brown62d62b52012-12-02 11:41:46 +09001337 }
1338
Mark Brown3cc72982012-06-19 16:31:53 +01001339 for (i = 0; i < ARRAY_SIZE(arizona->pdata.gpio_defaults); i++) {
1340 if (!arizona->pdata.gpio_defaults[i])
1341 continue;
1342
1343 regmap_write(arizona->regmap, ARIZONA_GPIO1_CTRL + i,
1344 arizona->pdata.gpio_defaults[i]);
1345 }
1346
Mark Brown3cc72982012-06-19 16:31:53 +01001347 /* Chip default */
1348 if (!arizona->pdata.clk32k_src)
1349 arizona->pdata.clk32k_src = ARIZONA_32KZ_MCLK2;
1350
1351 switch (arizona->pdata.clk32k_src) {
1352 case ARIZONA_32KZ_MCLK1:
1353 case ARIZONA_32KZ_MCLK2:
1354 regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
1355 ARIZONA_CLK_32K_SRC_MASK,
1356 arizona->pdata.clk32k_src - 1);
Mark Brown767c6dc2013-03-19 19:04:46 +01001357 arizona_clk32k_enable(arizona);
Mark Brown3cc72982012-06-19 16:31:53 +01001358 break;
1359 case ARIZONA_32KZ_NONE:
1360 regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
1361 ARIZONA_CLK_32K_SRC_MASK, 2);
1362 break;
1363 default:
1364 dev_err(arizona->dev, "Invalid 32kHz clock source: %d\n",
1365 arizona->pdata.clk32k_src);
1366 ret = -EINVAL;
Mark Brown59db9692012-07-09 00:31:36 +02001367 goto err_reset;
Mark Brown3cc72982012-06-19 16:31:53 +01001368 }
1369
Mark Brown3d91f822013-01-29 00:47:37 +08001370 for (i = 0; i < ARIZONA_MAX_MICBIAS; i++) {
Mark Brown544c7aa2013-01-29 18:44:41 +08001371 if (!arizona->pdata.micbias[i].mV &&
1372 !arizona->pdata.micbias[i].bypass)
Mark Brown3d91f822013-01-29 00:47:37 +08001373 continue;
1374
Mark Brown544c7aa2013-01-29 18:44:41 +08001375 /* Apply default for bypass mode */
1376 if (!arizona->pdata.micbias[i].mV)
1377 arizona->pdata.micbias[i].mV = 2800;
1378
Mark Brown3d91f822013-01-29 00:47:37 +08001379 val = (arizona->pdata.micbias[i].mV - 1500) / 100;
Mark Brown544c7aa2013-01-29 18:44:41 +08001380
Mark Brown3d91f822013-01-29 00:47:37 +08001381 val <<= ARIZONA_MICB1_LVL_SHIFT;
1382
1383 if (arizona->pdata.micbias[i].ext_cap)
1384 val |= ARIZONA_MICB1_EXT_CAP;
1385
1386 if (arizona->pdata.micbias[i].discharge)
1387 val |= ARIZONA_MICB1_DISCH;
1388
Charles Keepaxf773fc62013-05-21 14:56:58 +01001389 if (arizona->pdata.micbias[i].soft_start)
Mark Brown3d91f822013-01-29 00:47:37 +08001390 val |= ARIZONA_MICB1_RATE;
1391
Mark Brown544c7aa2013-01-29 18:44:41 +08001392 if (arizona->pdata.micbias[i].bypass)
1393 val |= ARIZONA_MICB1_BYPASS;
1394
Mark Brown3d91f822013-01-29 00:47:37 +08001395 regmap_update_bits(arizona->regmap,
1396 ARIZONA_MIC_BIAS_CTRL_1 + i,
1397 ARIZONA_MICB1_LVL_MASK |
Charles Keepax71d134b2014-09-24 10:37:11 +01001398 ARIZONA_MICB1_EXT_CAP |
Mark Brown3d91f822013-01-29 00:47:37 +08001399 ARIZONA_MICB1_DISCH |
Mark Brown544c7aa2013-01-29 18:44:41 +08001400 ARIZONA_MICB1_BYPASS |
Mark Brown3d91f822013-01-29 00:47:37 +08001401 ARIZONA_MICB1_RATE, val);
1402 }
1403
Charles Keepax72e43162015-06-14 15:41:50 +01001404 pm_runtime_set_active(arizona->dev);
1405 pm_runtime_enable(arizona->dev);
1406
Mark Brown3cc72982012-06-19 16:31:53 +01001407 /* Set up for interrupts */
1408 ret = arizona_irq_init(arizona);
1409 if (ret != 0)
Charles Keepaxd3477922016-06-27 15:19:11 +01001410 goto err_pm;
Mark Brown3cc72982012-06-19 16:31:53 +01001411
Charles Keepax72e43162015-06-14 15:41:50 +01001412 pm_runtime_set_autosuspend_delay(arizona->dev, 100);
1413 pm_runtime_use_autosuspend(arizona->dev);
1414
Mark Brown3cc72982012-06-19 16:31:53 +01001415 arizona_request_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, "CLKGEN error",
1416 arizona_clkgen_err, arizona);
1417 arizona_request_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, "Overclocked",
1418 arizona_overclocked, arizona);
1419 arizona_request_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, "Underclocked",
1420 arizona_underclocked, arizona);
1421
Richard Fitzgeraldae05ea362015-10-02 13:29:13 +01001422 ret = mfd_add_devices(arizona->dev, PLATFORM_DEVID_NONE,
1423 subdevs, n_subdevs, NULL, 0, NULL);
Mark Brown3cc72982012-06-19 16:31:53 +01001424
Richard Fitzgeraldae05ea362015-10-02 13:29:13 +01001425 if (ret) {
Mark Brown3cc72982012-06-19 16:31:53 +01001426 dev_err(arizona->dev, "Failed to add subdevices: %d\n", ret);
1427 goto err_irq;
1428 }
1429
1430 return 0;
1431
1432err_irq:
1433 arizona_irq_exit(arizona);
Charles Keepaxd3477922016-06-27 15:19:11 +01001434err_pm:
1435 pm_runtime_disable(arizona->dev);
Mark Brown3cc72982012-06-19 16:31:53 +01001436err_reset:
Charles Keepax22298752015-05-11 13:58:02 +01001437 arizona_enable_reset(arizona);
Mark Brown59db9692012-07-09 00:31:36 +02001438 regulator_disable(arizona->dcvdd);
Mark Brown3cc72982012-06-19 16:31:53 +01001439err_enable:
Mark Brown3a36a0db2012-07-09 00:45:53 +02001440 regulator_bulk_disable(arizona->num_core_supplies,
Mark Brown3cc72982012-06-19 16:31:53 +01001441 arizona->core_supplies);
Charles Keepaxe6021512014-06-02 09:50:41 +01001442err_dcvdd:
1443 regulator_put(arizona->dcvdd);
Mark Brown3cc72982012-06-19 16:31:53 +01001444err_early:
1445 mfd_remove_devices(dev);
1446 return ret;
1447}
1448EXPORT_SYMBOL_GPL(arizona_dev_init);
1449
Bill Pemberton4740f732012-11-19 13:26:01 -05001450int arizona_dev_exit(struct arizona *arizona)
Mark Brown3cc72982012-06-19 16:31:53 +01001451{
Charles Keepaxfb36f772016-11-14 17:15:56 +00001452 disable_irq(arizona->irq);
Charles Keepaxb8040202014-06-02 09:50:39 +01001453 pm_runtime_disable(arizona->dev);
1454
Charles Keepaxdf6b3352014-06-02 09:50:40 +01001455 regulator_disable(arizona->dcvdd);
Charles Keepaxe6021512014-06-02 09:50:41 +01001456 regulator_put(arizona->dcvdd);
Charles Keepaxdf6b3352014-06-02 09:50:40 +01001457
Mark Brown3cc72982012-06-19 16:31:53 +01001458 mfd_remove_devices(arizona->dev);
1459 arizona_free_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, arizona);
1460 arizona_free_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, arizona);
1461 arizona_free_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, arizona);
Mark Brown3cc72982012-06-19 16:31:53 +01001462 arizona_irq_exit(arizona);
Charles Keepax22298752015-05-11 13:58:02 +01001463 arizona_enable_reset(arizona);
Charles Keepaxdf6b3352014-06-02 09:50:40 +01001464
Charles Keepax44202862014-06-02 09:50:42 +01001465 regulator_bulk_disable(arizona->num_core_supplies,
Mark Brown1d017b62013-03-26 12:16:26 +00001466 arizona->core_supplies);
Mark Brown3cc72982012-06-19 16:31:53 +01001467 return 0;
1468}
1469EXPORT_SYMBOL_GPL(arizona_dev_exit);