blob: 75488e65cd96cf6484e300d47280df77f1eec77a [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>
Mark Brown3cc72982012-06-19 16:31:53 +010016#include <linux/gpio.h>
17#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>
22#include <linux/of_gpio.h>
Mark Brown3cc72982012-06-19 16:31:53 +010023#include <linux/pm_runtime.h>
24#include <linux/regmap.h>
25#include <linux/regulator/consumer.h>
Mark Brown59274672013-04-23 19:44:16 +010026#include <linux/regulator/machine.h>
Mark Brown3cc72982012-06-19 16:31:53 +010027#include <linux/slab.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]);
55 if (ret != 0)
56 goto err_pm;
57 break;
58 case ARIZONA_32KZ_MCLK2:
59 ret = clk_prepare_enable(arizona->mclk[ARIZONA_MCLK2]);
60 if (ret != 0)
61 goto err_ref;
Mark Brown247fa192013-03-19 14:47:47 +010062 break;
63 }
64
Mark Brown3cc72982012-06-19 16:31:53 +010065 ret = regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
66 ARIZONA_CLK_32K_ENA,
67 ARIZONA_CLK_32K_ENA);
Mark Brown247fa192013-03-19 14:47:47 +010068 }
Mark Brown3cc72982012-06-19 16:31:53 +010069
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +010070err_pm:
71 pm_runtime_put_sync(arizona->dev);
72err_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 Keepax9d53dfd2013-03-26 17:38:45 +0000240static int arizona_poll_reg(struct arizona *arizona,
Charles Keepaxef84f882017-03-15 14:58:38 +0000241 int timeout_ms, unsigned int reg,
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000242 unsigned int mask, unsigned int target)
243{
244 unsigned int val = 0;
Charles Keepaxef84f882017-03-15 14:58:38 +0000245 int ret;
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000246
Charles Keepaxef84f882017-03-15 14:58:38 +0000247 ret = regmap_read_poll_timeout(arizona->regmap,
248 ARIZONA_INTERRUPT_RAW_STATUS_5, val,
249 ((val & mask) == target),
250 ARIZONA_REG_POLL_DELAY_US,
251 timeout_ms * 1000);
252 if (ret)
253 dev_err(arizona->dev, "Polling reg 0x%x timed out: %x\n",
254 reg, val);
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000255
Charles Keepaxef84f882017-03-15 14:58:38 +0000256 return ret;
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000257}
258
Mark Brown3cc72982012-06-19 16:31:53 +0100259static int arizona_wait_for_boot(struct arizona *arizona)
260{
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000261 int ret;
Mark Brown3cc72982012-06-19 16:31:53 +0100262
263 /*
264 * We can't use an interrupt as we need to runtime resume to do so,
265 * we won't race with the interrupt handler as it'll be blocked on
266 * runtime resume.
267 */
Charles Keepaxef84f882017-03-15 14:58:38 +0000268 ret = arizona_poll_reg(arizona, 30, ARIZONA_INTERRUPT_RAW_STATUS_5,
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000269 ARIZONA_BOOT_DONE_STS, ARIZONA_BOOT_DONE_STS);
Mark Brown3cc72982012-06-19 16:31:53 +0100270
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000271 if (!ret)
Mark Brown3cc72982012-06-19 16:31:53 +0100272 regmap_write(arizona->regmap, ARIZONA_INTERRUPT_STATUS_5,
273 ARIZONA_BOOT_DONE_STS);
Mark Brown3cc72982012-06-19 16:31:53 +0100274
275 pm_runtime_mark_last_busy(arizona->dev);
276
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000277 return ret;
Mark Brown3cc72982012-06-19 16:31:53 +0100278}
279
Charles Keepax22298752015-05-11 13:58:02 +0100280static inline void arizona_enable_reset(struct arizona *arizona)
281{
282 if (arizona->pdata.reset)
283 gpio_set_value_cansleep(arizona->pdata.reset, 0);
284}
285
286static void arizona_disable_reset(struct arizona *arizona)
287{
288 if (arizona->pdata.reset) {
Charles Keepax121c0752015-05-11 13:58:10 +0100289 switch (arizona->type) {
290 case WM5110:
291 case WM8280:
292 /* Meet requirements for minimum reset duration */
Lee Jonesb79a9802015-10-28 12:42:30 +0000293 usleep_range(5000, 10000);
Charles Keepax121c0752015-05-11 13:58:10 +0100294 break;
295 default:
296 break;
297 }
298
Charles Keepax22298752015-05-11 13:58:02 +0100299 gpio_set_value_cansleep(arizona->pdata.reset, 1);
Lee Jonesb79a9802015-10-28 12:42:30 +0000300 usleep_range(1000, 5000);
Charles Keepax22298752015-05-11 13:58:02 +0100301 }
302}
303
Charles Keepax3850e3e2015-05-11 13:58:05 +0100304struct arizona_sysclk_state {
305 unsigned int fll;
306 unsigned int sysclk;
307};
308
309static int arizona_enable_freerun_sysclk(struct arizona *arizona,
310 struct arizona_sysclk_state *state)
Charles Keepaxe80436b2013-03-26 18:46:15 +0000311{
Charles Keepaxe80436b2013-03-26 18:46:15 +0000312 int ret, err;
313
Charles Keepaxe80436b2013-03-26 18:46:15 +0000314 /* Cache existing FLL and SYSCLK settings */
Charles Keepax3850e3e2015-05-11 13:58:05 +0100315 ret = regmap_read(arizona->regmap, ARIZONA_FLL1_CONTROL_1, &state->fll);
Charles Keepax0be068a2015-05-11 13:58:04 +0100316 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000317 dev_err(arizona->dev, "Failed to cache FLL settings: %d\n",
318 ret);
319 return ret;
320 }
Charles Keepax3850e3e2015-05-11 13:58:05 +0100321 ret = regmap_read(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1,
322 &state->sysclk);
Charles Keepax0be068a2015-05-11 13:58:04 +0100323 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000324 dev_err(arizona->dev, "Failed to cache SYSCLK settings: %d\n",
325 ret);
326 return ret;
327 }
328
329 /* Start up SYSCLK using the FLL in free running mode */
330 ret = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1,
331 ARIZONA_FLL1_ENA | ARIZONA_FLL1_FREERUN);
Charles Keepax0be068a2015-05-11 13:58:04 +0100332 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000333 dev_err(arizona->dev,
334 "Failed to start FLL in freerunning mode: %d\n",
335 ret);
336 return ret;
337 }
Charles Keepaxef84f882017-03-15 14:58:38 +0000338 ret = arizona_poll_reg(arizona, 180, ARIZONA_INTERRUPT_RAW_STATUS_5,
Charles Keepaxe80436b2013-03-26 18:46:15 +0000339 ARIZONA_FLL1_CLOCK_OK_STS,
340 ARIZONA_FLL1_CLOCK_OK_STS);
Charles Keepaxde4ea102017-03-15 14:58:36 +0000341 if (ret)
Charles Keepaxe80436b2013-03-26 18:46:15 +0000342 goto err_fll;
Charles Keepaxe80436b2013-03-26 18:46:15 +0000343
344 ret = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1, 0x0144);
Charles Keepax0be068a2015-05-11 13:58:04 +0100345 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000346 dev_err(arizona->dev, "Failed to start SYSCLK: %d\n", ret);
347 goto err_fll;
348 }
349
Charles Keepax3850e3e2015-05-11 13:58:05 +0100350 return 0;
351
352err_fll:
353 err = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, state->fll);
354 if (err)
355 dev_err(arizona->dev,
356 "Failed to re-apply old FLL settings: %d\n", err);
357
358 return ret;
359}
360
361static int arizona_disable_freerun_sysclk(struct arizona *arizona,
362 struct arizona_sysclk_state *state)
363{
364 int ret;
365
366 ret = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1,
367 state->sysclk);
368 if (ret) {
369 dev_err(arizona->dev,
370 "Failed to re-apply old SYSCLK settings: %d\n", ret);
371 return ret;
372 }
373
374 ret = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, state->fll);
375 if (ret) {
376 dev_err(arizona->dev,
377 "Failed to re-apply old FLL settings: %d\n", ret);
378 return ret;
379 }
380
381 return 0;
382}
383
384static int wm5102_apply_hardware_patch(struct arizona *arizona)
385{
386 struct arizona_sysclk_state state;
387 int err, ret;
388
389 ret = arizona_enable_freerun_sysclk(arizona, &state);
390 if (ret)
391 return ret;
392
Charles Keepaxe80436b2013-03-26 18:46:15 +0000393 /* Start the write sequencer and wait for it to finish */
394 ret = regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0,
Charles Keepax0be068a2015-05-11 13:58:04 +0100395 ARIZONA_WSEQ_ENA | ARIZONA_WSEQ_START | 160);
396 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000397 dev_err(arizona->dev, "Failed to start write sequencer: %d\n",
398 ret);
Charles Keepax3850e3e2015-05-11 13:58:05 +0100399 goto err;
Charles Keepaxe80436b2013-03-26 18:46:15 +0000400 }
Charles Keepax3850e3e2015-05-11 13:58:05 +0100401
Charles Keepaxef84f882017-03-15 14:58:38 +0000402 ret = arizona_poll_reg(arizona, 30, ARIZONA_WRITE_SEQUENCER_CTRL_1,
Charles Keepaxe80436b2013-03-26 18:46:15 +0000403 ARIZONA_WSEQ_BUSY, 0);
Charles Keepaxde4ea102017-03-15 14:58:36 +0000404 if (ret)
Charles Keepaxe80436b2013-03-26 18:46:15 +0000405 regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0,
Charles Keepax0be068a2015-05-11 13:58:04 +0100406 ARIZONA_WSEQ_ABORT);
Charles Keepaxe80436b2013-03-26 18:46:15 +0000407
Charles Keepax3850e3e2015-05-11 13:58:05 +0100408err:
409 err = arizona_disable_freerun_sysclk(arizona, &state);
Charles Keepaxe80436b2013-03-26 18:46:15 +0000410
Charles Keepax0be068a2015-05-11 13:58:04 +0100411 return ret ?: err;
Charles Keepaxe80436b2013-03-26 18:46:15 +0000412}
413
Charles Keepax882bc462015-05-11 13:58:06 +0100414/*
415 * Register patch to some of the CODECs internal write sequences
416 * to ensure a clean exit from the low power sleep state.
417 */
Nariman Poushin8019ff62015-07-16 16:36:21 +0100418static const struct reg_sequence wm5110_sleep_patch[] = {
Charles Keepax882bc462015-05-11 13:58:06 +0100419 { 0x337A, 0xC100 },
420 { 0x337B, 0x0041 },
421 { 0x3300, 0xA210 },
422 { 0x3301, 0x050C },
423};
424
425static int wm5110_apply_sleep_patch(struct arizona *arizona)
426{
427 struct arizona_sysclk_state state;
428 int err, ret;
429
430 ret = arizona_enable_freerun_sysclk(arizona, &state);
431 if (ret)
432 return ret;
433
434 ret = regmap_multi_reg_write_bypassed(arizona->regmap,
435 wm5110_sleep_patch,
436 ARRAY_SIZE(wm5110_sleep_patch));
437
438 err = arizona_disable_freerun_sysclk(arizona, &state);
439
440 return ret ?: err;
441}
442
Charles Keepax1c1c6bb2015-05-11 13:58:03 +0100443static int wm5102_clear_write_sequencer(struct arizona *arizona)
444{
445 int ret;
446
447 ret = regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_3,
448 0x0);
449 if (ret) {
450 dev_err(arizona->dev,
451 "Failed to clear write sequencer state: %d\n", ret);
452 return ret;
453 }
454
455 arizona_enable_reset(arizona);
456 regulator_disable(arizona->dcvdd);
457
458 msleep(20);
459
460 ret = regulator_enable(arizona->dcvdd);
461 if (ret) {
462 dev_err(arizona->dev, "Failed to re-enable DCVDD: %d\n", ret);
463 return ret;
464 }
465 arizona_disable_reset(arizona);
466
467 return 0;
468}
469
Rafael J. Wysocki48bb9fe2014-12-05 03:04:12 +0100470#ifdef CONFIG_PM
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100471static int arizona_isolate_dcvdd(struct arizona *arizona)
472{
473 int ret;
474
475 ret = regmap_update_bits(arizona->regmap,
476 ARIZONA_ISOLATION_CONTROL,
477 ARIZONA_ISOLATE_DCVDD1,
478 ARIZONA_ISOLATE_DCVDD1);
479 if (ret != 0)
480 dev_err(arizona->dev, "Failed to isolate DCVDD: %d\n", ret);
481
482 return ret;
483}
484
485static int arizona_connect_dcvdd(struct arizona *arizona)
486{
487 int ret;
488
489 ret = regmap_update_bits(arizona->regmap,
490 ARIZONA_ISOLATION_CONTROL,
491 ARIZONA_ISOLATE_DCVDD1, 0);
492 if (ret != 0)
493 dev_err(arizona->dev, "Failed to connect DCVDD: %d\n", ret);
494
495 return ret;
496}
497
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100498static int arizona_is_jack_det_active(struct arizona *arizona)
499{
500 unsigned int val;
501 int ret;
502
503 ret = regmap_read(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE, &val);
504 if (ret) {
505 dev_err(arizona->dev,
506 "Failed to check jack det status: %d\n", ret);
507 return ret;
508 } else if (val & ARIZONA_JD1_ENA) {
509 return 1;
510 } else {
511 return 0;
512 }
513}
514
Mark Brown3cc72982012-06-19 16:31:53 +0100515static int arizona_runtime_resume(struct device *dev)
516{
517 struct arizona *arizona = dev_get_drvdata(dev);
518 int ret;
519
Mark Brown508c8292012-07-20 17:09:12 +0100520 dev_dbg(arizona->dev, "Leaving AoD mode\n");
521
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100522 if (arizona->has_fully_powered_off) {
523 dev_dbg(arizona->dev, "Re-enabling core supplies\n");
524
525 ret = regulator_bulk_enable(arizona->num_core_supplies,
526 arizona->core_supplies);
527 if (ret) {
528 dev_err(dev, "Failed to enable core supplies: %d\n",
529 ret);
530 return ret;
531 }
532 }
533
Mark Brown59db9692012-07-09 00:31:36 +0200534 ret = regulator_enable(arizona->dcvdd);
535 if (ret != 0) {
536 dev_err(arizona->dev, "Failed to enable DCVDD: %d\n", ret);
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100537 if (arizona->has_fully_powered_off)
538 regulator_bulk_disable(arizona->num_core_supplies,
539 arizona->core_supplies);
Mark Brown59db9692012-07-09 00:31:36 +0200540 return ret;
541 }
Mark Brown3cc72982012-06-19 16:31:53 +0100542
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100543 if (arizona->has_fully_powered_off) {
544 arizona_disable_reset(arizona);
545 enable_irq(arizona->irq);
546 arizona->has_fully_powered_off = false;
547 }
548
Mark Brown3cc72982012-06-19 16:31:53 +0100549 regcache_cache_only(arizona->regmap, false);
550
Charles Keepax4c9bb8b2013-03-26 18:01:49 +0000551 switch (arizona->type) {
552 case WM5102:
Mark Brown59274672013-04-23 19:44:16 +0100553 if (arizona->external_dcvdd) {
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100554 ret = arizona_connect_dcvdd(arizona);
555 if (ret != 0)
Mark Brown59274672013-04-23 19:44:16 +0100556 goto err;
Mark Brown59274672013-04-23 19:44:16 +0100557 }
558
Charles Keepax4c9bb8b2013-03-26 18:01:49 +0000559 ret = wm5102_patch(arizona);
560 if (ret != 0) {
561 dev_err(arizona->dev, "Failed to apply patch: %d\n",
562 ret);
563 goto err;
564 }
Charles Keepaxe80436b2013-03-26 18:46:15 +0000565
Charles Keepax0be068a2015-05-11 13:58:04 +0100566 ret = wm5102_apply_hardware_patch(arizona);
567 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000568 dev_err(arizona->dev,
569 "Failed to apply hardware patch: %d\n",
570 ret);
571 goto err;
572 }
573 break;
Charles Keepax96129a02015-05-11 13:58:08 +0100574 case WM5110:
575 case WM8280:
576 ret = arizona_wait_for_boot(arizona);
577 if (ret)
578 goto err;
579
580 if (arizona->external_dcvdd) {
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100581 ret = arizona_connect_dcvdd(arizona);
582 if (ret != 0)
Charles Keepax96129a02015-05-11 13:58:08 +0100583 goto err;
Charles Keepax96129a02015-05-11 13:58:08 +0100584 } else {
585 /*
586 * As this is only called for the internal regulator
587 * (where we know voltage ranges available) it is ok
588 * to request an exact range.
589 */
590 ret = regulator_set_voltage(arizona->dcvdd,
591 1200000, 1200000);
592 if (ret < 0) {
593 dev_err(arizona->dev,
594 "Failed to set resume voltage: %d\n",
595 ret);
596 goto err;
597 }
598 }
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100599
600 ret = wm5110_apply_sleep_patch(arizona);
601 if (ret) {
602 dev_err(arizona->dev,
603 "Failed to re-apply sleep patch: %d\n",
604 ret);
605 goto err;
606 }
Charles Keepax96129a02015-05-11 13:58:08 +0100607 break;
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000608 case WM1831:
609 case CS47L24:
610 ret = arizona_wait_for_boot(arizona);
611 if (ret != 0)
612 goto err;
613 break;
Charles Keepaxe80436b2013-03-26 18:46:15 +0000614 default:
Charles Keepax12bb68e2013-03-27 09:49:40 +0000615 ret = arizona_wait_for_boot(arizona);
Charles Keepax3762aed2015-08-11 09:34:31 +0100616 if (ret != 0)
Charles Keepax12bb68e2013-03-27 09:49:40 +0000617 goto err;
Charles Keepax12bb68e2013-03-27 09:49:40 +0000618
Mark Brown59274672013-04-23 19:44:16 +0100619 if (arizona->external_dcvdd) {
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100620 ret = arizona_connect_dcvdd(arizona);
621 if (ret != 0)
Mark Brown59274672013-04-23 19:44:16 +0100622 goto err;
Mark Brown59274672013-04-23 19:44:16 +0100623 }
Charles Keepaxe80436b2013-03-26 18:46:15 +0000624 break;
Charles Keepax4c9bb8b2013-03-26 18:01:49 +0000625 }
626
Mark Brown9270bdf2013-01-04 17:16:12 +0000627 ret = regcache_sync(arizona->regmap);
628 if (ret != 0) {
629 dev_err(arizona->dev, "Failed to restore register cache\n");
Mark Brown4816bd12013-01-14 15:50:38 +0900630 goto err;
Mark Brown9270bdf2013-01-04 17:16:12 +0000631 }
Mark Brown3cc72982012-06-19 16:31:53 +0100632
633 return 0;
Mark Brown4816bd12013-01-14 15:50:38 +0900634
635err:
636 regcache_cache_only(arizona->regmap, true);
637 regulator_disable(arizona->dcvdd);
638 return ret;
Mark Brown3cc72982012-06-19 16:31:53 +0100639}
640
641static int arizona_runtime_suspend(struct device *dev)
642{
643 struct arizona *arizona = dev_get_drvdata(dev);
Dan Carpentera05950a2015-10-19 13:18:05 +0300644 int jd_active = 0;
Mark Brown59274672013-04-23 19:44:16 +0100645 int ret;
Mark Brown3cc72982012-06-19 16:31:53 +0100646
Mark Brown508c8292012-07-20 17:09:12 +0100647 dev_dbg(arizona->dev, "Entering AoD mode\n");
648
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100649 switch (arizona->type) {
650 case WM5110:
651 case WM8280:
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100652 jd_active = arizona_is_jack_det_active(arizona);
653 if (jd_active < 0)
654 return jd_active;
655
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100656 if (arizona->external_dcvdd) {
657 ret = arizona_isolate_dcvdd(arizona);
658 if (ret != 0)
659 return ret;
660 } else {
661 /*
662 * As this is only called for the internal regulator
663 * (where we know voltage ranges available) it is ok
664 * to request an exact range.
665 */
666 ret = regulator_set_voltage(arizona->dcvdd,
667 1175000, 1175000);
668 if (ret < 0) {
669 dev_err(arizona->dev,
670 "Failed to set suspend voltage: %d\n",
671 ret);
672 return ret;
673 }
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100674 }
675 break;
676 case WM5102:
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100677 jd_active = arizona_is_jack_det_active(arizona);
678 if (jd_active < 0)
679 return jd_active;
680
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100681 if (arizona->external_dcvdd) {
682 ret = arizona_isolate_dcvdd(arizona);
683 if (ret != 0)
684 return ret;
685 }
686
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100687 if (!jd_active) {
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100688 ret = regmap_write(arizona->regmap,
689 ARIZONA_WRITE_SEQUENCER_CTRL_3, 0x0);
690 if (ret) {
Charles Keepax96129a02015-05-11 13:58:08 +0100691 dev_err(arizona->dev,
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100692 "Failed to clear write sequencer: %d\n",
Charles Keepax96129a02015-05-11 13:58:08 +0100693 ret);
694 return ret;
695 }
Charles Keepax96129a02015-05-11 13:58:08 +0100696 }
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100697 break;
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000698 case WM1831:
699 case CS47L24:
700 break;
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100701 default:
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100702 jd_active = arizona_is_jack_det_active(arizona);
703 if (jd_active < 0)
704 return jd_active;
705
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100706 if (arizona->external_dcvdd) {
707 ret = arizona_isolate_dcvdd(arizona);
708 if (ret != 0)
709 return ret;
710 }
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100711 break;
Mark Brown59274672013-04-23 19:44:16 +0100712 }
713
Mark Brown59db9692012-07-09 00:31:36 +0200714 regcache_cache_only(arizona->regmap, true);
715 regcache_mark_dirty(arizona->regmap);
Charles Keepaxe293e84722013-08-06 17:18:35 +0100716 regulator_disable(arizona->dcvdd);
Mark Brown3cc72982012-06-19 16:31:53 +0100717
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100718 /* Allow us to completely power down if no jack detection */
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100719 if (!jd_active) {
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100720 dev_dbg(arizona->dev, "Fully powering off\n");
721
722 arizona->has_fully_powered_off = true;
723
Charles Keepax11150922015-06-14 15:41:49 +0100724 disable_irq_nosync(arizona->irq);
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100725 arizona_enable_reset(arizona);
726 regulator_bulk_disable(arizona->num_core_supplies,
727 arizona->core_supplies);
728 }
729
Mark Brown3cc72982012-06-19 16:31:53 +0100730 return 0;
731}
732#endif
733
Mark Browndc781d02013-01-27 12:07:32 +0800734#ifdef CONFIG_PM_SLEEP
Mark Brown67c99292013-04-10 12:40:26 +0100735static int arizona_suspend(struct device *dev)
736{
737 struct arizona *arizona = dev_get_drvdata(dev);
738
739 dev_dbg(arizona->dev, "Suspend, disabling IRQ\n");
740 disable_irq(arizona->irq);
741
742 return 0;
743}
744
Charles Keepax3612b272016-08-30 10:33:10 +0100745static int arizona_suspend_noirq(struct device *dev)
Mark Brown67c99292013-04-10 12:40:26 +0100746{
747 struct arizona *arizona = dev_get_drvdata(dev);
748
749 dev_dbg(arizona->dev, "Late suspend, reenabling IRQ\n");
750 enable_irq(arizona->irq);
751
752 return 0;
753}
754
Mark Browndc781d02013-01-27 12:07:32 +0800755static int arizona_resume_noirq(struct device *dev)
756{
757 struct arizona *arizona = dev_get_drvdata(dev);
758
759 dev_dbg(arizona->dev, "Early resume, disabling IRQ\n");
760 disable_irq(arizona->irq);
761
762 return 0;
763}
764
765static int arizona_resume(struct device *dev)
766{
767 struct arizona *arizona = dev_get_drvdata(dev);
768
Charles Keepax3612b272016-08-30 10:33:10 +0100769 dev_dbg(arizona->dev, "Resume, reenabling IRQ\n");
Mark Browndc781d02013-01-27 12:07:32 +0800770 enable_irq(arizona->irq);
771
772 return 0;
773}
774#endif
775
Mark Brown3cc72982012-06-19 16:31:53 +0100776const struct dev_pm_ops arizona_pm_ops = {
777 SET_RUNTIME_PM_OPS(arizona_runtime_suspend,
778 arizona_runtime_resume,
779 NULL)
Mark Brown67c99292013-04-10 12:40:26 +0100780 SET_SYSTEM_SLEEP_PM_OPS(arizona_suspend, arizona_resume)
Charles Keepax3612b272016-08-30 10:33:10 +0100781 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(arizona_suspend_noirq,
782 arizona_resume_noirq)
Mark Brown3cc72982012-06-19 16:31:53 +0100783};
784EXPORT_SYMBOL_GPL(arizona_pm_ops);
785
Mark Brownd7810092013-03-25 00:11:27 +0000786#ifdef CONFIG_OF
Lee Jones942786e2014-07-02 14:28:46 +0100787unsigned long arizona_of_get_type(struct device *dev)
Mark Brownd7810092013-03-25 00:11:27 +0000788{
789 const struct of_device_id *id = of_match_device(arizona_of_match, dev);
790
791 if (id)
Lee Jones942786e2014-07-02 14:28:46 +0100792 return (unsigned long)id->data;
Mark Brownd7810092013-03-25 00:11:27 +0000793 else
794 return 0;
795}
796EXPORT_SYMBOL_GPL(arizona_of_get_type);
797
798static int arizona_of_get_core_pdata(struct arizona *arizona)
799{
Charles Keepaxe4fcb1d2014-04-16 10:01:37 +0100800 struct arizona_pdata *pdata = &arizona->pdata;
Inha Songcc47aed2014-08-30 11:27:18 +0900801 struct property *prop;
802 const __be32 *cur;
803 u32 val;
Richard Fitzgeraldf4c05262016-09-13 10:05:44 +0100804 u32 pdm_val[ARIZONA_MAX_PDM_SPK];
Mark Brownd7810092013-03-25 00:11:27 +0000805 int ret, i;
Inha Songcc47aed2014-08-30 11:27:18 +0900806 int count = 0;
Mark Brownd7810092013-03-25 00:11:27 +0000807
Charles Keepax1961531d2016-09-20 16:30:13 +0100808 pdata->reset = of_get_named_gpio(arizona->dev->of_node, "wlf,reset", 0);
Charles Keepaxb8d336e2016-09-20 16:30:14 +0100809 if (pdata->reset == -EPROBE_DEFER) {
810 return pdata->reset;
811 } else if (pdata->reset < 0) {
Charles Keepax1961531d2016-09-20 16:30:13 +0100812 dev_err(arizona->dev, "Reset GPIO missing/malformed: %d\n",
813 pdata->reset);
814
815 pdata->reset = 0;
816 }
Mark Brownd7810092013-03-25 00:11:27 +0000817
818 ret = of_property_read_u32_array(arizona->dev->of_node,
819 "wlf,gpio-defaults",
Charles Keepax3762aed2015-08-11 09:34:31 +0100820 pdata->gpio_defaults,
821 ARRAY_SIZE(pdata->gpio_defaults));
Mark Brownd7810092013-03-25 00:11:27 +0000822 if (ret >= 0) {
823 /*
824 * All values are literal except out of range values
825 * which are chip default, translate into platform
826 * data which uses 0 as chip default and out of range
827 * as zero.
828 */
Charles Keepax3762aed2015-08-11 09:34:31 +0100829 for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
830 if (pdata->gpio_defaults[i] > 0xffff)
831 pdata->gpio_defaults[i] = 0;
832 else if (pdata->gpio_defaults[i] == 0)
833 pdata->gpio_defaults[i] = 0x10000;
Mark Brownd7810092013-03-25 00:11:27 +0000834 }
835 } else {
836 dev_err(arizona->dev, "Failed to parse GPIO defaults: %d\n",
837 ret);
838 }
839
Inha Songcc47aed2014-08-30 11:27:18 +0900840 of_property_for_each_u32(arizona->dev->of_node, "wlf,inmode", prop,
841 cur, val) {
Charles Keepax3762aed2015-08-11 09:34:31 +0100842 if (count == ARRAY_SIZE(pdata->inmode))
Inha Songcc47aed2014-08-30 11:27:18 +0900843 break;
844
Charles Keepax3762aed2015-08-11 09:34:31 +0100845 pdata->inmode[count] = val;
Inha Songcc47aed2014-08-30 11:27:18 +0900846 count++;
847 }
848
Charles Keepaxe7ad27c2015-03-03 15:04:53 +0000849 count = 0;
850 of_property_for_each_u32(arizona->dev->of_node, "wlf,dmic-ref", prop,
851 cur, val) {
Charles Keepax3762aed2015-08-11 09:34:31 +0100852 if (count == ARRAY_SIZE(pdata->dmic_ref))
Charles Keepaxe7ad27c2015-03-03 15:04:53 +0000853 break;
854
Charles Keepax3762aed2015-08-11 09:34:31 +0100855 pdata->dmic_ref[count] = val;
Charles Keepaxe7ad27c2015-03-03 15:04:53 +0000856 count++;
857 }
858
Charles Keepaxf199d392015-12-14 10:19:11 +0000859 count = 0;
860 of_property_for_each_u32(arizona->dev->of_node, "wlf,out-mono", prop,
861 cur, val) {
862 if (count == ARRAY_SIZE(pdata->out_mono))
863 break;
864
865 pdata->out_mono[count] = !!val;
866 count++;
867 }
868
Richard Fitzgeraldf4c05262016-09-13 10:05:44 +0100869 count = 0;
870 of_property_for_each_u32(arizona->dev->of_node,
871 "wlf,max-channels-clocked",
872 prop, cur, val) {
873 if (count == ARRAY_SIZE(pdata->max_channels_clocked))
874 break;
875
876 pdata->max_channels_clocked[count] = val;
877 count++;
878 }
879
880 ret = of_property_read_u32_array(arizona->dev->of_node,
881 "wlf,spk-fmt",
882 pdm_val,
883 ARRAY_SIZE(pdm_val));
884
885 if (ret >= 0)
886 for (count = 0; count < ARRAY_SIZE(pdata->spk_fmt); ++count)
887 pdata->spk_fmt[count] = pdm_val[count];
888
889 ret = of_property_read_u32_array(arizona->dev->of_node,
890 "wlf,spk-mute",
891 pdm_val,
892 ARRAY_SIZE(pdm_val));
893
894 if (ret >= 0)
895 for (count = 0; count < ARRAY_SIZE(pdata->spk_mute); ++count)
896 pdata->spk_mute[count] = pdm_val[count];
897
Mark Brownd7810092013-03-25 00:11:27 +0000898 return 0;
899}
900
901const struct of_device_id arizona_of_match[] = {
902 { .compatible = "wlf,wm5102", .data = (void *)WM5102 },
903 { .compatible = "wlf,wm5110", .data = (void *)WM5110 },
Richard Fitzgeralde5d4ef02015-01-17 15:21:22 +0000904 { .compatible = "wlf,wm8280", .data = (void *)WM8280 },
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100905 { .compatible = "wlf,wm8997", .data = (void *)WM8997 },
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100906 { .compatible = "wlf,wm8998", .data = (void *)WM8998 },
907 { .compatible = "wlf,wm1814", .data = (void *)WM1814 },
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000908 { .compatible = "wlf,wm1831", .data = (void *)WM1831 },
909 { .compatible = "cirrus,cs47l24", .data = (void *)CS47L24 },
Mark Brownd7810092013-03-25 00:11:27 +0000910 {},
911};
912EXPORT_SYMBOL_GPL(arizona_of_match);
913#else
914static inline int arizona_of_get_core_pdata(struct arizona *arizona)
915{
916 return 0;
917}
918#endif
919
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +0100920static const struct mfd_cell early_devs[] = {
Mark Brown3cc72982012-06-19 16:31:53 +0100921 { .name = "arizona-ldo1" },
922};
923
Charles Keepax3762aed2015-08-11 09:34:31 +0100924static const char * const wm5102_supplies[] = {
Charles Keepax5fc6c392014-07-25 16:24:44 +0100925 "MICVDD",
Charles Keepax32dadef2013-10-15 20:14:22 +0100926 "DBVDD2",
927 "DBVDD3",
928 "CPVDD",
929 "SPKVDDL",
930 "SPKVDDR",
931};
932
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +0100933static const struct mfd_cell wm5102_devs[] = {
Mark Brownd7768112012-12-20 15:38:03 +0000934 { .name = "arizona-micsupp" },
Charles Keepaxf83c2182016-04-15 13:18:47 +0100935 { .name = "arizona-gpio" },
Charles Keepax5fc6c392014-07-25 16:24:44 +0100936 {
937 .name = "arizona-extcon",
938 .parent_supplies = wm5102_supplies,
939 .num_parent_supplies = 1, /* We only need MICVDD */
940 },
Mark Brown503b1ca2012-11-27 17:36:38 +0000941 { .name = "arizona-haptics" },
Mark Brown3cc72982012-06-19 16:31:53 +0100942 { .name = "arizona-pwm" },
Charles Keepax32dadef2013-10-15 20:14:22 +0100943 {
944 .name = "wm5102-codec",
945 .parent_supplies = wm5102_supplies,
946 .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
947 },
Mark Brown3cc72982012-06-19 16:31:53 +0100948};
949
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +0100950static const struct mfd_cell wm5110_devs[] = {
Mark Brownd7768112012-12-20 15:38:03 +0000951 { .name = "arizona-micsupp" },
Charles Keepaxf83c2182016-04-15 13:18:47 +0100952 { .name = "arizona-gpio" },
Charles Keepax5fc6c392014-07-25 16:24:44 +0100953 {
954 .name = "arizona-extcon",
955 .parent_supplies = wm5102_supplies,
956 .num_parent_supplies = 1, /* We only need MICVDD */
957 },
Mark Brown503b1ca2012-11-27 17:36:38 +0000958 { .name = "arizona-haptics" },
Mark Browne102bef2012-07-10 12:37:58 +0100959 { .name = "arizona-pwm" },
Charles Keepax32dadef2013-10-15 20:14:22 +0100960 {
961 .name = "wm5110-codec",
962 .parent_supplies = wm5102_supplies,
963 .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
964 },
965};
966
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000967static const char * const cs47l24_supplies[] = {
968 "MICVDD",
969 "CPVDD",
970 "SPKVDD",
971};
972
973static const struct mfd_cell cs47l24_devs[] = {
974 { .name = "arizona-gpio" },
975 { .name = "arizona-haptics" },
976 { .name = "arizona-pwm" },
977 {
978 .name = "cs47l24-codec",
979 .parent_supplies = cs47l24_supplies,
980 .num_parent_supplies = ARRAY_SIZE(cs47l24_supplies),
981 },
982};
983
Charles Keepax3762aed2015-08-11 09:34:31 +0100984static const char * const wm8997_supplies[] = {
Charles Keepax996c2d42014-07-25 16:24:43 +0100985 "MICVDD",
Charles Keepax32dadef2013-10-15 20:14:22 +0100986 "DBVDD2",
987 "CPVDD",
988 "SPKVDD",
Mark Browne102bef2012-07-10 12:37:58 +0100989};
990
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +0100991static const struct mfd_cell wm8997_devs[] = {
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100992 { .name = "arizona-micsupp" },
Charles Keepaxf83c2182016-04-15 13:18:47 +0100993 { .name = "arizona-gpio" },
Charles Keepax5fc6c392014-07-25 16:24:44 +0100994 {
995 .name = "arizona-extcon",
996 .parent_supplies = wm8997_supplies,
997 .num_parent_supplies = 1, /* We only need MICVDD */
998 },
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100999 { .name = "arizona-haptics" },
1000 { .name = "arizona-pwm" },
Charles Keepax32dadef2013-10-15 20:14:22 +01001001 {
1002 .name = "wm8997-codec",
1003 .parent_supplies = wm8997_supplies,
1004 .num_parent_supplies = ARRAY_SIZE(wm8997_supplies),
1005 },
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001006};
1007
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001008static const struct mfd_cell wm8998_devs[] = {
Charles Keepaxf83c2182016-04-15 13:18:47 +01001009 { .name = "arizona-micsupp" },
1010 { .name = "arizona-gpio" },
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001011 {
1012 .name = "arizona-extcon",
1013 .parent_supplies = wm5102_supplies,
1014 .num_parent_supplies = 1, /* We only need MICVDD */
1015 },
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001016 { .name = "arizona-haptics" },
1017 { .name = "arizona-pwm" },
1018 {
1019 .name = "wm8998-codec",
1020 .parent_supplies = wm5102_supplies,
1021 .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
1022 },
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001023};
1024
Bill Pembertonf791be42012-11-19 13:23:04 -05001025int arizona_dev_init(struct arizona *arizona)
Mark Brown3cc72982012-06-19 16:31:53 +01001026{
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +01001027 const char * const mclk_name[] = { "mclk1", "mclk2" };
Mark Brown3cc72982012-06-19 16:31:53 +01001028 struct device *dev = arizona->dev;
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +00001029 const char *type_name = NULL;
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001030 unsigned int reg, val, mask;
Mark Brown62d62b52012-12-02 11:41:46 +09001031 int (*apply_patch)(struct arizona *) = NULL;
Richard Fitzgeraldae05ea362015-10-02 13:29:13 +01001032 const struct mfd_cell *subdevs = NULL;
1033 int n_subdevs, ret, i;
Mark Brown3cc72982012-06-19 16:31:53 +01001034
1035 dev_set_drvdata(arizona->dev, arizona);
1036 mutex_init(&arizona->clk_lock);
1037
Charles Keepaxb8d336e2016-09-20 16:30:14 +01001038 if (dev_get_platdata(arizona->dev)) {
Mark Brown3cc72982012-06-19 16:31:53 +01001039 memcpy(&arizona->pdata, dev_get_platdata(arizona->dev),
1040 sizeof(arizona->pdata));
Charles Keepaxb8d336e2016-09-20 16:30:14 +01001041 } else {
1042 ret = arizona_of_get_core_pdata(arizona);
1043 if (ret < 0)
1044 return ret;
1045 }
Mark Brown3cc72982012-06-19 16:31:53 +01001046
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +01001047 BUILD_BUG_ON(ARRAY_SIZE(arizona->mclk) != ARRAY_SIZE(mclk_name));
1048 for (i = 0; i < ARRAY_SIZE(arizona->mclk); i++) {
1049 arizona->mclk[i] = devm_clk_get(arizona->dev, mclk_name[i]);
1050 if (IS_ERR(arizona->mclk[i])) {
1051 dev_info(arizona->dev, "Failed to get %s: %ld\n",
1052 mclk_name[i], PTR_ERR(arizona->mclk[i]));
1053 arizona->mclk[i] = NULL;
1054 }
1055 }
1056
Mark Brown3cc72982012-06-19 16:31:53 +01001057 regcache_cache_only(arizona->regmap, true);
1058
1059 switch (arizona->type) {
1060 case WM5102:
Mark Browne102bef2012-07-10 12:37:58 +01001061 case WM5110:
Richard Fitzgeralde5d4ef02015-01-17 15:21:22 +00001062 case WM8280:
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001063 case WM8997:
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001064 case WM8998:
1065 case WM1814:
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +00001066 case WM1831:
1067 case CS47L24:
Mark Brown3cc72982012-06-19 16:31:53 +01001068 for (i = 0; i < ARRAY_SIZE(wm5102_core_supplies); i++)
1069 arizona->core_supplies[i].supply
1070 = wm5102_core_supplies[i];
1071 arizona->num_core_supplies = ARRAY_SIZE(wm5102_core_supplies);
1072 break;
1073 default:
1074 dev_err(arizona->dev, "Unknown device type %d\n",
1075 arizona->type);
Charles Keepax75d8a2b2016-08-31 10:41:30 +01001076 return -ENODEV;
Mark Brown3cc72982012-06-19 16:31:53 +01001077 }
1078
Charles Keepax4a8c4752014-04-16 10:01:38 +01001079 /* Mark DCVDD as external, LDO1 driver will clear if internal */
1080 arizona->external_dcvdd = true;
1081
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +00001082 switch (arizona->type) {
1083 case WM1831:
1084 case CS47L24:
1085 break; /* No LDO1 regulator */
1086 default:
1087 ret = mfd_add_devices(arizona->dev, -1, early_devs,
1088 ARRAY_SIZE(early_devs), NULL, 0, NULL);
1089 if (ret != 0) {
1090 dev_err(dev, "Failed to add early children: %d\n", ret);
1091 return ret;
1092 }
1093 break;
Mark Brown3cc72982012-06-19 16:31:53 +01001094 }
1095
1096 ret = devm_regulator_bulk_get(dev, arizona->num_core_supplies,
1097 arizona->core_supplies);
1098 if (ret != 0) {
1099 dev_err(dev, "Failed to request core supplies: %d\n",
1100 ret);
1101 goto err_early;
1102 }
1103
Charles Keepax0c2d0ff2014-06-19 16:04:23 +01001104 /**
1105 * Don't use devres here because the only device we have to get
1106 * against is the MFD device and DCVDD will likely be supplied by
1107 * one of its children. Meaning that the regulator will be
1108 * destroyed by the time devres calls regulator put.
1109 */
Charles Keepaxe6021512014-06-02 09:50:41 +01001110 arizona->dcvdd = regulator_get(arizona->dev, "DCVDD");
Mark Brown59db9692012-07-09 00:31:36 +02001111 if (IS_ERR(arizona->dcvdd)) {
1112 ret = PTR_ERR(arizona->dcvdd);
1113 dev_err(dev, "Failed to request DCVDD: %d\n", ret);
1114 goto err_early;
1115 }
1116
Mark Brown87d3af42013-03-26 12:15:26 +00001117 if (arizona->pdata.reset) {
1118 /* Start out with /RESET low to put the chip into reset */
Charles Keepax5f056bf2015-05-11 13:58:01 +01001119 ret = devm_gpio_request_one(arizona->dev, arizona->pdata.reset,
1120 GPIOF_DIR_OUT | GPIOF_INIT_LOW,
1121 "arizona /RESET");
Mark Brown87d3af42013-03-26 12:15:26 +00001122 if (ret != 0) {
1123 dev_err(dev, "Failed to request /RESET: %d\n", ret);
Charles Keepaxe6021512014-06-02 09:50:41 +01001124 goto err_dcvdd;
Mark Brown87d3af42013-03-26 12:15:26 +00001125 }
1126 }
1127
Mark Brown3cc72982012-06-19 16:31:53 +01001128 ret = regulator_bulk_enable(arizona->num_core_supplies,
1129 arizona->core_supplies);
1130 if (ret != 0) {
1131 dev_err(dev, "Failed to enable core supplies: %d\n",
1132 ret);
Charles Keepaxe6021512014-06-02 09:50:41 +01001133 goto err_dcvdd;
Mark Brown3cc72982012-06-19 16:31:53 +01001134 }
1135
Mark Brown59db9692012-07-09 00:31:36 +02001136 ret = regulator_enable(arizona->dcvdd);
1137 if (ret != 0) {
1138 dev_err(dev, "Failed to enable DCVDD: %d\n", ret);
1139 goto err_enable;
1140 }
1141
Charles Keepax22298752015-05-11 13:58:02 +01001142 arizona_disable_reset(arizona);
Mark Brown3cc72982012-06-19 16:31:53 +01001143
Mark Brown3cc72982012-06-19 16:31:53 +01001144 regcache_cache_only(arizona->regmap, false);
1145
Mark Brownca76ceb2013-04-09 16:04:35 +01001146 /* Verify that this is a chip we know about */
1147 ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, &reg);
1148 if (ret != 0) {
1149 dev_err(dev, "Failed to read ID register: %d\n", ret);
1150 goto err_reset;
1151 }
1152
1153 switch (reg) {
1154 case 0x5102:
1155 case 0x5110:
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001156 case 0x6349:
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +00001157 case 0x6363:
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001158 case 0x8997:
Mark Brownca76ceb2013-04-09 16:04:35 +01001159 break;
1160 default:
1161 dev_err(arizona->dev, "Unknown device ID: %x\n", reg);
Charles Keepax75d8a2b2016-08-31 10:41:30 +01001162 ret = -ENODEV;
Mark Brownca76ceb2013-04-09 16:04:35 +01001163 goto err_reset;
1164 }
1165
1166 /* If we have a /RESET GPIO we'll already be reset */
1167 if (!arizona->pdata.reset) {
Mark Brownca76ceb2013-04-09 16:04:35 +01001168 ret = regmap_write(arizona->regmap, ARIZONA_SOFTWARE_RESET, 0);
1169 if (ret != 0) {
1170 dev_err(dev, "Failed to reset device: %d\n", ret);
1171 goto err_reset;
1172 }
1173
Lee Jonesb79a9802015-10-28 12:42:30 +00001174 usleep_range(1000, 5000);
Mark Brownca76ceb2013-04-09 16:04:35 +01001175 }
1176
1177 /* Ensure device startup is complete */
1178 switch (arizona->type) {
1179 case WM5102:
Mark Brown48018942014-08-13 11:42:46 +01001180 ret = regmap_read(arizona->regmap,
1181 ARIZONA_WRITE_SEQUENCER_CTRL_3, &val);
Charles Keepax1c1c6bb2015-05-11 13:58:03 +01001182 if (ret) {
Mark Brownca76ceb2013-04-09 16:04:35 +01001183 dev_err(dev,
1184 "Failed to check write sequencer state: %d\n",
1185 ret);
Charles Keepax1c1c6bb2015-05-11 13:58:03 +01001186 } else if (val & 0x01) {
1187 ret = wm5102_clear_write_sequencer(arizona);
1188 if (ret)
1189 return ret;
Mark Brownca76ceb2013-04-09 16:04:35 +01001190 }
1191 break;
Charles Keepax1c1c6bb2015-05-11 13:58:03 +01001192 default:
1193 break;
1194 }
1195
1196 ret = arizona_wait_for_boot(arizona);
1197 if (ret) {
1198 dev_err(arizona->dev, "Device failed initial boot: %d\n", ret);
1199 goto err_reset;
Mark Brownca76ceb2013-04-09 16:04:35 +01001200 }
1201
1202 /* Read the device ID information & do device specific stuff */
Mark Brown3cc72982012-06-19 16:31:53 +01001203 ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, &reg);
1204 if (ret != 0) {
1205 dev_err(dev, "Failed to read ID register: %d\n", ret);
Mark Brown59db9692012-07-09 00:31:36 +02001206 goto err_reset;
Mark Brown3cc72982012-06-19 16:31:53 +01001207 }
1208
1209 ret = regmap_read(arizona->regmap, ARIZONA_DEVICE_REVISION,
1210 &arizona->rev);
1211 if (ret != 0) {
1212 dev_err(dev, "Failed to read revision register: %d\n", ret);
Mark Brown59db9692012-07-09 00:31:36 +02001213 goto err_reset;
Mark Brown3cc72982012-06-19 16:31:53 +01001214 }
1215 arizona->rev &= ARIZONA_DEVICE_REVISION_MASK;
1216
1217 switch (reg) {
1218 case 0x5102:
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001219 if (IS_ENABLED(CONFIG_MFD_WM5102)) {
1220 type_name = "WM5102";
1221 if (arizona->type != WM5102) {
1222 dev_warn(arizona->dev,
1223 "WM5102 registered as %d\n",
1224 arizona->type);
1225 arizona->type = WM5102;
1226 }
1227
1228 apply_patch = wm5102_patch;
1229 arizona->rev &= 0x7;
1230 subdevs = wm5102_devs;
1231 n_subdevs = ARRAY_SIZE(wm5102_devs);
Mark Brown3cc72982012-06-19 16:31:53 +01001232 }
Mark Brown3cc72982012-06-19 16:31:53 +01001233 break;
Mark Browne102bef2012-07-10 12:37:58 +01001234 case 0x5110:
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001235 if (IS_ENABLED(CONFIG_MFD_WM5110)) {
1236 switch (arizona->type) {
1237 case WM5110:
1238 type_name = "WM5110";
1239 break;
1240 case WM8280:
1241 type_name = "WM8280";
1242 break;
1243 default:
1244 type_name = "WM5110";
1245 dev_warn(arizona->dev,
1246 "WM5110 registered as %d\n",
1247 arizona->type);
1248 arizona->type = WM5110;
1249 break;
1250 }
1251
1252 apply_patch = wm5110_patch;
1253 subdevs = wm5110_devs;
1254 n_subdevs = ARRAY_SIZE(wm5110_devs);
Mark Browne102bef2012-07-10 12:37:58 +01001255 }
Mark Browne102bef2012-07-10 12:37:58 +01001256 break;
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +00001257 case 0x6363:
1258 if (IS_ENABLED(CONFIG_MFD_CS47L24)) {
1259 switch (arizona->type) {
1260 case CS47L24:
1261 type_name = "CS47L24";
1262 break;
1263
1264 case WM1831:
1265 type_name = "WM1831";
1266 break;
1267
1268 default:
1269 dev_warn(arizona->dev,
1270 "CS47L24 registered as %d\n",
1271 arizona->type);
1272 arizona->type = CS47L24;
1273 break;
1274 }
1275
1276 apply_patch = cs47l24_patch;
1277 subdevs = cs47l24_devs;
1278 n_subdevs = ARRAY_SIZE(cs47l24_devs);
1279 }
1280 break;
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001281 case 0x8997:
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001282 if (IS_ENABLED(CONFIG_MFD_WM8997)) {
1283 type_name = "WM8997";
1284 if (arizona->type != WM8997) {
1285 dev_warn(arizona->dev,
1286 "WM8997 registered as %d\n",
1287 arizona->type);
1288 arizona->type = WM8997;
1289 }
1290
1291 apply_patch = wm8997_patch;
1292 subdevs = wm8997_devs;
1293 n_subdevs = ARRAY_SIZE(wm8997_devs);
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001294 }
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001295 break;
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001296 case 0x6349:
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001297 if (IS_ENABLED(CONFIG_MFD_WM8998)) {
1298 switch (arizona->type) {
1299 case WM8998:
1300 type_name = "WM8998";
1301 break;
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001302
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001303 case WM1814:
1304 type_name = "WM1814";
1305 break;
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001306
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001307 default:
1308 type_name = "WM8998";
1309 dev_warn(arizona->dev,
1310 "WM8998 registered as %d\n",
1311 arizona->type);
1312 arizona->type = WM8998;
1313 }
1314
1315 apply_patch = wm8998_patch;
1316 subdevs = wm8998_devs;
1317 n_subdevs = ARRAY_SIZE(wm8998_devs);
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001318 }
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001319 break;
Mark Brown3cc72982012-06-19 16:31:53 +01001320 default:
1321 dev_err(arizona->dev, "Unknown device ID %x\n", reg);
Charles Keepax75d8a2b2016-08-31 10:41:30 +01001322 ret = -ENODEV;
Mark Brown59db9692012-07-09 00:31:36 +02001323 goto err_reset;
Mark Brown3cc72982012-06-19 16:31:53 +01001324 }
1325
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001326 if (!subdevs) {
1327 dev_err(arizona->dev,
1328 "No kernel support for device ID %x\n", reg);
Charles Keepax75d8a2b2016-08-31 10:41:30 +01001329 ret = -ENODEV;
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001330 goto err_reset;
1331 }
1332
Mark Brown3cc72982012-06-19 16:31:53 +01001333 dev_info(dev, "%s revision %c\n", type_name, arizona->rev + 'A');
1334
Mark Brown62d62b52012-12-02 11:41:46 +09001335 if (apply_patch) {
1336 ret = apply_patch(arizona);
1337 if (ret != 0) {
1338 dev_err(arizona->dev, "Failed to apply patch: %d\n",
1339 ret);
1340 goto err_reset;
1341 }
Charles Keepaxe80436b2013-03-26 18:46:15 +00001342
1343 switch (arizona->type) {
1344 case WM5102:
Charles Keepax0be068a2015-05-11 13:58:04 +01001345 ret = wm5102_apply_hardware_patch(arizona);
1346 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +00001347 dev_err(arizona->dev,
1348 "Failed to apply hardware patch: %d\n",
1349 ret);
1350 goto err_reset;
1351 }
1352 break;
Charles Keepax882bc462015-05-11 13:58:06 +01001353 case WM5110:
1354 case WM8280:
1355 ret = wm5110_apply_sleep_patch(arizona);
1356 if (ret) {
1357 dev_err(arizona->dev,
1358 "Failed to apply sleep patch: %d\n",
1359 ret);
1360 goto err_reset;
1361 }
1362 break;
Charles Keepaxe80436b2013-03-26 18:46:15 +00001363 default:
1364 break;
1365 }
Mark Brown62d62b52012-12-02 11:41:46 +09001366 }
1367
Mark Brown3cc72982012-06-19 16:31:53 +01001368 for (i = 0; i < ARRAY_SIZE(arizona->pdata.gpio_defaults); i++) {
1369 if (!arizona->pdata.gpio_defaults[i])
1370 continue;
1371
1372 regmap_write(arizona->regmap, ARIZONA_GPIO1_CTRL + i,
1373 arizona->pdata.gpio_defaults[i]);
1374 }
1375
Mark Brown3cc72982012-06-19 16:31:53 +01001376 /* Chip default */
1377 if (!arizona->pdata.clk32k_src)
1378 arizona->pdata.clk32k_src = ARIZONA_32KZ_MCLK2;
1379
1380 switch (arizona->pdata.clk32k_src) {
1381 case ARIZONA_32KZ_MCLK1:
1382 case ARIZONA_32KZ_MCLK2:
1383 regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
1384 ARIZONA_CLK_32K_SRC_MASK,
1385 arizona->pdata.clk32k_src - 1);
Mark Brown767c6dc2013-03-19 19:04:46 +01001386 arizona_clk32k_enable(arizona);
Mark Brown3cc72982012-06-19 16:31:53 +01001387 break;
1388 case ARIZONA_32KZ_NONE:
1389 regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
1390 ARIZONA_CLK_32K_SRC_MASK, 2);
1391 break;
1392 default:
1393 dev_err(arizona->dev, "Invalid 32kHz clock source: %d\n",
1394 arizona->pdata.clk32k_src);
1395 ret = -EINVAL;
Mark Brown59db9692012-07-09 00:31:36 +02001396 goto err_reset;
Mark Brown3cc72982012-06-19 16:31:53 +01001397 }
1398
Mark Brown3d91f822013-01-29 00:47:37 +08001399 for (i = 0; i < ARIZONA_MAX_MICBIAS; i++) {
Mark Brown544c7aa2013-01-29 18:44:41 +08001400 if (!arizona->pdata.micbias[i].mV &&
1401 !arizona->pdata.micbias[i].bypass)
Mark Brown3d91f822013-01-29 00:47:37 +08001402 continue;
1403
Mark Brown544c7aa2013-01-29 18:44:41 +08001404 /* Apply default for bypass mode */
1405 if (!arizona->pdata.micbias[i].mV)
1406 arizona->pdata.micbias[i].mV = 2800;
1407
Mark Brown3d91f822013-01-29 00:47:37 +08001408 val = (arizona->pdata.micbias[i].mV - 1500) / 100;
Mark Brown544c7aa2013-01-29 18:44:41 +08001409
Mark Brown3d91f822013-01-29 00:47:37 +08001410 val <<= ARIZONA_MICB1_LVL_SHIFT;
1411
1412 if (arizona->pdata.micbias[i].ext_cap)
1413 val |= ARIZONA_MICB1_EXT_CAP;
1414
1415 if (arizona->pdata.micbias[i].discharge)
1416 val |= ARIZONA_MICB1_DISCH;
1417
Charles Keepaxf773fc62013-05-21 14:56:58 +01001418 if (arizona->pdata.micbias[i].soft_start)
Mark Brown3d91f822013-01-29 00:47:37 +08001419 val |= ARIZONA_MICB1_RATE;
1420
Mark Brown544c7aa2013-01-29 18:44:41 +08001421 if (arizona->pdata.micbias[i].bypass)
1422 val |= ARIZONA_MICB1_BYPASS;
1423
Mark Brown3d91f822013-01-29 00:47:37 +08001424 regmap_update_bits(arizona->regmap,
1425 ARIZONA_MIC_BIAS_CTRL_1 + i,
1426 ARIZONA_MICB1_LVL_MASK |
Charles Keepax71d134b2014-09-24 10:37:11 +01001427 ARIZONA_MICB1_EXT_CAP |
Mark Brown3d91f822013-01-29 00:47:37 +08001428 ARIZONA_MICB1_DISCH |
Mark Brown544c7aa2013-01-29 18:44:41 +08001429 ARIZONA_MICB1_BYPASS |
Mark Brown3d91f822013-01-29 00:47:37 +08001430 ARIZONA_MICB1_RATE, val);
1431 }
1432
Mark Brown3cc72982012-06-19 16:31:53 +01001433 for (i = 0; i < ARIZONA_MAX_INPUT; i++) {
1434 /* Default for both is 0 so noop with defaults */
1435 val = arizona->pdata.dmic_ref[i]
1436 << ARIZONA_IN1_DMIC_SUP_SHIFT;
Richard Fitzgeraldfc027d12015-05-01 16:15:12 +01001437 if (arizona->pdata.inmode[i] & ARIZONA_INMODE_DMIC)
1438 val |= 1 << ARIZONA_IN1_MODE_SHIFT;
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001439
1440 switch (arizona->type) {
1441 case WM8998:
1442 case WM1814:
1443 regmap_update_bits(arizona->regmap,
1444 ARIZONA_ADC_DIGITAL_VOLUME_1L + (i * 8),
1445 ARIZONA_IN1L_SRC_SE_MASK,
1446 (arizona->pdata.inmode[i] & ARIZONA_INMODE_SE)
1447 << ARIZONA_IN1L_SRC_SE_SHIFT);
1448
1449 regmap_update_bits(arizona->regmap,
1450 ARIZONA_ADC_DIGITAL_VOLUME_1R + (i * 8),
1451 ARIZONA_IN1R_SRC_SE_MASK,
1452 (arizona->pdata.inmode[i] & ARIZONA_INMODE_SE)
1453 << ARIZONA_IN1R_SRC_SE_SHIFT);
1454
1455 mask = ARIZONA_IN1_DMIC_SUP_MASK |
1456 ARIZONA_IN1_MODE_MASK;
1457 break;
1458 default:
1459 if (arizona->pdata.inmode[i] & ARIZONA_INMODE_SE)
1460 val |= 1 << ARIZONA_IN1_SINGLE_ENDED_SHIFT;
1461
1462 mask = ARIZONA_IN1_DMIC_SUP_MASK |
1463 ARIZONA_IN1_MODE_MASK |
1464 ARIZONA_IN1_SINGLE_ENDED_MASK;
1465 break;
1466 }
Mark Brown3cc72982012-06-19 16:31:53 +01001467
1468 regmap_update_bits(arizona->regmap,
1469 ARIZONA_IN1L_CONTROL + (i * 8),
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001470 mask, val);
Mark Brown3cc72982012-06-19 16:31:53 +01001471 }
1472
1473 for (i = 0; i < ARIZONA_MAX_OUTPUT; i++) {
1474 /* Default is 0 so noop with defaults */
1475 if (arizona->pdata.out_mono[i])
1476 val = ARIZONA_OUT1_MONO;
1477 else
1478 val = 0;
1479
1480 regmap_update_bits(arizona->regmap,
1481 ARIZONA_OUTPUT_PATH_CONFIG_1L + (i * 8),
1482 ARIZONA_OUT1_MONO, val);
1483 }
1484
Mark Brown3cc72982012-06-19 16:31:53 +01001485 for (i = 0; i < ARIZONA_MAX_PDM_SPK; i++) {
1486 if (arizona->pdata.spk_mute[i])
1487 regmap_update_bits(arizona->regmap,
Mark Brown2a51da02012-07-09 19:33:14 +01001488 ARIZONA_PDM_SPK1_CTRL_1 + (i * 2),
Mark Brown3cc72982012-06-19 16:31:53 +01001489 ARIZONA_SPK1_MUTE_ENDIAN_MASK |
1490 ARIZONA_SPK1_MUTE_SEQ1_MASK,
1491 arizona->pdata.spk_mute[i]);
1492
1493 if (arizona->pdata.spk_fmt[i])
1494 regmap_update_bits(arizona->regmap,
Mark Brown2a51da02012-07-09 19:33:14 +01001495 ARIZONA_PDM_SPK1_CTRL_2 + (i * 2),
Mark Brown3cc72982012-06-19 16:31:53 +01001496 ARIZONA_SPK1_FMT_MASK,
1497 arizona->pdata.spk_fmt[i]);
1498 }
1499
Charles Keepax72e43162015-06-14 15:41:50 +01001500 pm_runtime_set_active(arizona->dev);
1501 pm_runtime_enable(arizona->dev);
1502
Mark Brown3cc72982012-06-19 16:31:53 +01001503 /* Set up for interrupts */
1504 ret = arizona_irq_init(arizona);
1505 if (ret != 0)
Charles Keepaxd3477922016-06-27 15:19:11 +01001506 goto err_pm;
Mark Brown3cc72982012-06-19 16:31:53 +01001507
Charles Keepax72e43162015-06-14 15:41:50 +01001508 pm_runtime_set_autosuspend_delay(arizona->dev, 100);
1509 pm_runtime_use_autosuspend(arizona->dev);
1510
Mark Brown3cc72982012-06-19 16:31:53 +01001511 arizona_request_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, "CLKGEN error",
1512 arizona_clkgen_err, arizona);
1513 arizona_request_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, "Overclocked",
1514 arizona_overclocked, arizona);
1515 arizona_request_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, "Underclocked",
1516 arizona_underclocked, arizona);
1517
Richard Fitzgeraldae05ea362015-10-02 13:29:13 +01001518 ret = mfd_add_devices(arizona->dev, PLATFORM_DEVID_NONE,
1519 subdevs, n_subdevs, NULL, 0, NULL);
Mark Brown3cc72982012-06-19 16:31:53 +01001520
Richard Fitzgeraldae05ea362015-10-02 13:29:13 +01001521 if (ret) {
Mark Brown3cc72982012-06-19 16:31:53 +01001522 dev_err(arizona->dev, "Failed to add subdevices: %d\n", ret);
1523 goto err_irq;
1524 }
1525
1526 return 0;
1527
1528err_irq:
1529 arizona_irq_exit(arizona);
Charles Keepaxd3477922016-06-27 15:19:11 +01001530err_pm:
1531 pm_runtime_disable(arizona->dev);
Mark Brown3cc72982012-06-19 16:31:53 +01001532err_reset:
Charles Keepax22298752015-05-11 13:58:02 +01001533 arizona_enable_reset(arizona);
Mark Brown59db9692012-07-09 00:31:36 +02001534 regulator_disable(arizona->dcvdd);
Mark Brown3cc72982012-06-19 16:31:53 +01001535err_enable:
Mark Brown3a36a0db2012-07-09 00:45:53 +02001536 regulator_bulk_disable(arizona->num_core_supplies,
Mark Brown3cc72982012-06-19 16:31:53 +01001537 arizona->core_supplies);
Charles Keepaxe6021512014-06-02 09:50:41 +01001538err_dcvdd:
1539 regulator_put(arizona->dcvdd);
Mark Brown3cc72982012-06-19 16:31:53 +01001540err_early:
1541 mfd_remove_devices(dev);
1542 return ret;
1543}
1544EXPORT_SYMBOL_GPL(arizona_dev_init);
1545
Bill Pemberton4740f732012-11-19 13:26:01 -05001546int arizona_dev_exit(struct arizona *arizona)
Mark Brown3cc72982012-06-19 16:31:53 +01001547{
Charles Keepaxfb36f772016-11-14 17:15:56 +00001548 disable_irq(arizona->irq);
Charles Keepaxb8040202014-06-02 09:50:39 +01001549 pm_runtime_disable(arizona->dev);
1550
Charles Keepaxdf6b3352014-06-02 09:50:40 +01001551 regulator_disable(arizona->dcvdd);
Charles Keepaxe6021512014-06-02 09:50:41 +01001552 regulator_put(arizona->dcvdd);
Charles Keepaxdf6b3352014-06-02 09:50:40 +01001553
Mark Brown3cc72982012-06-19 16:31:53 +01001554 mfd_remove_devices(arizona->dev);
1555 arizona_free_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, arizona);
1556 arizona_free_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, arizona);
1557 arizona_free_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, arizona);
Mark Brown3cc72982012-06-19 16:31:53 +01001558 arizona_irq_exit(arizona);
Charles Keepax22298752015-05-11 13:58:02 +01001559 arizona_enable_reset(arizona);
Charles Keepaxdf6b3352014-06-02 09:50:40 +01001560
Charles Keepax44202862014-06-02 09:50:42 +01001561 regulator_bulk_disable(arizona->num_core_supplies,
Mark Brown1d017b62013-03-26 12:16:26 +00001562 arizona->core_supplies);
Mark Brown3cc72982012-06-19 16:31:53 +01001563 return 0;
1564}
1565EXPORT_SYMBOL_GPL(arizona_dev_exit);