blob: 9323b1e3a69efe874cfbbbd9419d440e6a70afec [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Mark Brown3cc72982012-06-19 16:31:53 +01002/*
3 * Arizona core driver
4 *
5 * Copyright 2012 Wolfson Microelectronics plc
6 *
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
Mark Brown3cc72982012-06-19 16:31:53 +01008 */
9
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +010010#include <linux/clk.h>
Mark Brown3cc72982012-06-19 16:31:53 +010011#include <linux/delay.h>
Mark Brown59db9692012-07-09 00:31:36 +020012#include <linux/err.h>
Charles Keepaxc1860462018-03-12 15:52:00 +000013#include <linux/gpio/consumer.h>
Mark Brown3cc72982012-06-19 16:31:53 +010014#include <linux/interrupt.h>
15#include <linux/mfd/core.h>
16#include <linux/module.h>
Mark Brownd7810092013-03-25 00:11:27 +000017#include <linux/of.h>
18#include <linux/of_device.h>
Mark Brown3cc72982012-06-19 16:31:53 +010019#include <linux/pm_runtime.h>
20#include <linux/regmap.h>
21#include <linux/regulator/consumer.h>
Mark Brown59274672013-04-23 19:44:16 +010022#include <linux/regulator/machine.h>
Mark Brown3cc72982012-06-19 16:31:53 +010023#include <linux/slab.h>
Charles Keepaxf99fea92018-06-28 16:49:35 +010024#include <linux/ktime.h>
Richard Fitzgeraldae05ea362015-10-02 13:29:13 +010025#include <linux/platform_device.h>
Mark Brown3cc72982012-06-19 16:31:53 +010026
27#include <linux/mfd/arizona/core.h>
28#include <linux/mfd/arizona/registers.h>
29
30#include "arizona.h"
31
Charles Keepax3762aed2015-08-11 09:34:31 +010032static const char * const wm5102_core_supplies[] = {
Mark Brown3cc72982012-06-19 16:31:53 +010033 "AVDD",
34 "DBVDD1",
Mark Brown3cc72982012-06-19 16:31:53 +010035};
36
37int arizona_clk32k_enable(struct arizona *arizona)
38{
39 int ret = 0;
40
41 mutex_lock(&arizona->clk_lock);
42
43 arizona->clk32k_ref++;
44
Mark Brown247fa192013-03-19 14:47:47 +010045 if (arizona->clk32k_ref == 1) {
46 switch (arizona->pdata.clk32k_src) {
47 case ARIZONA_32KZ_MCLK1:
48 ret = pm_runtime_get_sync(arizona->dev);
49 if (ret != 0)
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +010050 goto err_ref;
51 ret = clk_prepare_enable(arizona->mclk[ARIZONA_MCLK1]);
Sapthagiri Baratam6b269a42018-08-21 19:52:44 +053052 if (ret != 0) {
53 pm_runtime_put_sync(arizona->dev);
54 goto err_ref;
55 }
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +010056 break;
57 case ARIZONA_32KZ_MCLK2:
58 ret = clk_prepare_enable(arizona->mclk[ARIZONA_MCLK2]);
59 if (ret != 0)
60 goto err_ref;
Mark Brown247fa192013-03-19 14:47:47 +010061 break;
62 }
63
Mark Brown3cc72982012-06-19 16:31:53 +010064 ret = regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
65 ARIZONA_CLK_32K_ENA,
66 ARIZONA_CLK_32K_ENA);
Mark Brown247fa192013-03-19 14:47:47 +010067 }
Mark Brown3cc72982012-06-19 16:31:53 +010068
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +010069err_ref:
Mark Brown3cc72982012-06-19 16:31:53 +010070 if (ret != 0)
71 arizona->clk32k_ref--;
72
73 mutex_unlock(&arizona->clk_lock);
74
75 return ret;
76}
77EXPORT_SYMBOL_GPL(arizona_clk32k_enable);
78
79int arizona_clk32k_disable(struct arizona *arizona)
80{
Mark Brown3cc72982012-06-19 16:31:53 +010081 mutex_lock(&arizona->clk_lock);
82
Charles Keepax14024cc2020-06-15 14:53:20 +010083 WARN_ON(arizona->clk32k_ref <= 0);
Mark Brown3cc72982012-06-19 16:31:53 +010084
85 arizona->clk32k_ref--;
86
Mark Brown247fa192013-03-19 14:47:47 +010087 if (arizona->clk32k_ref == 0) {
Mark Brown3cc72982012-06-19 16:31:53 +010088 regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
89 ARIZONA_CLK_32K_ENA, 0);
90
Mark Brown247fa192013-03-19 14:47:47 +010091 switch (arizona->pdata.clk32k_src) {
92 case ARIZONA_32KZ_MCLK1:
93 pm_runtime_put_sync(arizona->dev);
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +010094 clk_disable_unprepare(arizona->mclk[ARIZONA_MCLK1]);
95 break;
96 case ARIZONA_32KZ_MCLK2:
97 clk_disable_unprepare(arizona->mclk[ARIZONA_MCLK2]);
Mark Brown247fa192013-03-19 14:47:47 +010098 break;
99 }
100 }
101
Mark Brown3cc72982012-06-19 16:31:53 +0100102 mutex_unlock(&arizona->clk_lock);
103
Javier Martinez Canillasa260fba2015-09-29 13:26:02 +0200104 return 0;
Mark Brown3cc72982012-06-19 16:31:53 +0100105}
106EXPORT_SYMBOL_GPL(arizona_clk32k_disable);
107
108static irqreturn_t arizona_clkgen_err(int irq, void *data)
109{
110 struct arizona *arizona = data;
111
112 dev_err(arizona->dev, "CLKGEN error\n");
113
114 return IRQ_HANDLED;
115}
116
117static irqreturn_t arizona_underclocked(int irq, void *data)
118{
119 struct arizona *arizona = data;
120 unsigned int val;
121 int ret;
122
123 ret = regmap_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_8,
124 &val);
125 if (ret != 0) {
126 dev_err(arizona->dev, "Failed to read underclock status: %d\n",
127 ret);
128 return IRQ_NONE;
129 }
130
131 if (val & ARIZONA_AIF3_UNDERCLOCKED_STS)
132 dev_err(arizona->dev, "AIF3 underclocked\n");
Mark Brown3cc72982012-06-19 16:31:53 +0100133 if (val & ARIZONA_AIF2_UNDERCLOCKED_STS)
Charles Keepax3ebef342012-11-20 13:46:20 +0900134 dev_err(arizona->dev, "AIF2 underclocked\n");
135 if (val & ARIZONA_AIF1_UNDERCLOCKED_STS)
Mark Brown3cc72982012-06-19 16:31:53 +0100136 dev_err(arizona->dev, "AIF1 underclocked\n");
Charles Keepax6e440d22014-07-15 11:21:49 +0100137 if (val & ARIZONA_ISRC3_UNDERCLOCKED_STS)
138 dev_err(arizona->dev, "ISRC3 underclocked\n");
Mark Brown3cc72982012-06-19 16:31:53 +0100139 if (val & ARIZONA_ISRC2_UNDERCLOCKED_STS)
140 dev_err(arizona->dev, "ISRC2 underclocked\n");
141 if (val & ARIZONA_ISRC1_UNDERCLOCKED_STS)
142 dev_err(arizona->dev, "ISRC1 underclocked\n");
143 if (val & ARIZONA_FX_UNDERCLOCKED_STS)
144 dev_err(arizona->dev, "FX underclocked\n");
145 if (val & ARIZONA_ASRC_UNDERCLOCKED_STS)
146 dev_err(arizona->dev, "ASRC underclocked\n");
147 if (val & ARIZONA_DAC_UNDERCLOCKED_STS)
148 dev_err(arizona->dev, "DAC underclocked\n");
149 if (val & ARIZONA_ADC_UNDERCLOCKED_STS)
150 dev_err(arizona->dev, "ADC underclocked\n");
151 if (val & ARIZONA_MIXER_UNDERCLOCKED_STS)
Mark Brown648a9882013-01-28 00:32:53 +0800152 dev_err(arizona->dev, "Mixer dropped sample\n");
Mark Brown3cc72982012-06-19 16:31:53 +0100153
154 return IRQ_HANDLED;
155}
156
157static irqreturn_t arizona_overclocked(int irq, void *data)
158{
159 struct arizona *arizona = data;
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100160 unsigned int val[3];
Mark Brown3cc72982012-06-19 16:31:53 +0100161 int ret;
Charles Keepax3762aed2015-08-11 09:34:31 +0100162
Mark Brown3cc72982012-06-19 16:31:53 +0100163 ret = regmap_bulk_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_6,
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100164 &val[0], 3);
Mark Brown3cc72982012-06-19 16:31:53 +0100165 if (ret != 0) {
166 dev_err(arizona->dev, "Failed to read overclock status: %d\n",
167 ret);
168 return IRQ_NONE;
169 }
170
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100171 switch (arizona->type) {
172 case WM8998:
173 case WM1814:
174 /* Some bits are shifted on WM8998,
175 * rearrange to match the standard bit layout
176 */
177 val[0] = ((val[0] & 0x60e0) >> 1) |
178 ((val[0] & 0x1e00) >> 2) |
179 (val[0] & 0x000f);
180 break;
181 default:
182 break;
183 }
184
Mark Brown3cc72982012-06-19 16:31:53 +0100185 if (val[0] & ARIZONA_PWM_OVERCLOCKED_STS)
186 dev_err(arizona->dev, "PWM overclocked\n");
187 if (val[0] & ARIZONA_FX_CORE_OVERCLOCKED_STS)
188 dev_err(arizona->dev, "FX core overclocked\n");
189 if (val[0] & ARIZONA_DAC_SYS_OVERCLOCKED_STS)
190 dev_err(arizona->dev, "DAC SYS overclocked\n");
191 if (val[0] & ARIZONA_DAC_WARP_OVERCLOCKED_STS)
192 dev_err(arizona->dev, "DAC WARP overclocked\n");
193 if (val[0] & ARIZONA_ADC_OVERCLOCKED_STS)
194 dev_err(arizona->dev, "ADC overclocked\n");
195 if (val[0] & ARIZONA_MIXER_OVERCLOCKED_STS)
196 dev_err(arizona->dev, "Mixer overclocked\n");
197 if (val[0] & ARIZONA_AIF3_SYNC_OVERCLOCKED_STS)
198 dev_err(arizona->dev, "AIF3 overclocked\n");
199 if (val[0] & ARIZONA_AIF2_SYNC_OVERCLOCKED_STS)
200 dev_err(arizona->dev, "AIF2 overclocked\n");
201 if (val[0] & ARIZONA_AIF1_SYNC_OVERCLOCKED_STS)
202 dev_err(arizona->dev, "AIF1 overclocked\n");
203 if (val[0] & ARIZONA_PAD_CTRL_OVERCLOCKED_STS)
204 dev_err(arizona->dev, "Pad control overclocked\n");
205
206 if (val[1] & ARIZONA_SLIMBUS_SUBSYS_OVERCLOCKED_STS)
207 dev_err(arizona->dev, "Slimbus subsystem overclocked\n");
208 if (val[1] & ARIZONA_SLIMBUS_ASYNC_OVERCLOCKED_STS)
209 dev_err(arizona->dev, "Slimbus async overclocked\n");
210 if (val[1] & ARIZONA_SLIMBUS_SYNC_OVERCLOCKED_STS)
211 dev_err(arizona->dev, "Slimbus sync overclocked\n");
212 if (val[1] & ARIZONA_ASRC_ASYNC_SYS_OVERCLOCKED_STS)
213 dev_err(arizona->dev, "ASRC async system overclocked\n");
214 if (val[1] & ARIZONA_ASRC_ASYNC_WARP_OVERCLOCKED_STS)
215 dev_err(arizona->dev, "ASRC async WARP overclocked\n");
216 if (val[1] & ARIZONA_ASRC_SYNC_SYS_OVERCLOCKED_STS)
217 dev_err(arizona->dev, "ASRC sync system overclocked\n");
218 if (val[1] & ARIZONA_ASRC_SYNC_WARP_OVERCLOCKED_STS)
219 dev_err(arizona->dev, "ASRC sync WARP overclocked\n");
220 if (val[1] & ARIZONA_ADSP2_1_OVERCLOCKED_STS)
221 dev_err(arizona->dev, "DSP1 overclocked\n");
Charles Keepax6e440d22014-07-15 11:21:49 +0100222 if (val[1] & ARIZONA_ISRC3_OVERCLOCKED_STS)
223 dev_err(arizona->dev, "ISRC3 overclocked\n");
Mark Brown3cc72982012-06-19 16:31:53 +0100224 if (val[1] & ARIZONA_ISRC2_OVERCLOCKED_STS)
225 dev_err(arizona->dev, "ISRC2 overclocked\n");
226 if (val[1] & ARIZONA_ISRC1_OVERCLOCKED_STS)
227 dev_err(arizona->dev, "ISRC1 overclocked\n");
228
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100229 if (val[2] & ARIZONA_SPDIF_OVERCLOCKED_STS)
230 dev_err(arizona->dev, "SPDIF overclocked\n");
231
Mark Brown3cc72982012-06-19 16:31:53 +0100232 return IRQ_HANDLED;
233}
234
Charles Keepaxef84f882017-03-15 14:58:38 +0000235#define ARIZONA_REG_POLL_DELAY_US 7500
236
Charles Keepaxf99fea92018-06-28 16:49:35 +0100237static inline bool arizona_poll_reg_delay(ktime_t timeout)
238{
239 if (ktime_compare(ktime_get(), timeout) > 0)
240 return false;
241
242 usleep_range(ARIZONA_REG_POLL_DELAY_US / 2, ARIZONA_REG_POLL_DELAY_US);
243
244 return true;
245}
246
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000247static int arizona_poll_reg(struct arizona *arizona,
Charles Keepaxef84f882017-03-15 14:58:38 +0000248 int timeout_ms, unsigned int reg,
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000249 unsigned int mask, unsigned int target)
250{
Charles Keepaxf99fea92018-06-28 16:49:35 +0100251 ktime_t timeout = ktime_add_us(ktime_get(), timeout_ms * USEC_PER_MSEC);
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000252 unsigned int val = 0;
Charles Keepaxef84f882017-03-15 14:58:38 +0000253 int ret;
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000254
Charles Keepaxf99fea92018-06-28 16:49:35 +0100255 do {
256 ret = regmap_read(arizona->regmap, reg, &val);
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000257
Charles Keepaxf99fea92018-06-28 16:49:35 +0100258 if ((val & mask) == target)
259 return 0;
260 } while (arizona_poll_reg_delay(timeout));
261
262 if (ret) {
263 dev_err(arizona->dev, "Failed polling reg 0x%x: %d\n",
264 reg, ret);
265 return ret;
266 }
267
268 dev_err(arizona->dev, "Polling reg 0x%x timed out: %x\n", reg, val);
269 return -ETIMEDOUT;
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000270}
271
Mark Brown3cc72982012-06-19 16:31:53 +0100272static int arizona_wait_for_boot(struct arizona *arizona)
273{
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000274 int ret;
Mark Brown3cc72982012-06-19 16:31:53 +0100275
276 /*
277 * We can't use an interrupt as we need to runtime resume to do so,
278 * we won't race with the interrupt handler as it'll be blocked on
279 * runtime resume.
280 */
Charles Keepaxef84f882017-03-15 14:58:38 +0000281 ret = arizona_poll_reg(arizona, 30, ARIZONA_INTERRUPT_RAW_STATUS_5,
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000282 ARIZONA_BOOT_DONE_STS, ARIZONA_BOOT_DONE_STS);
Mark Brown3cc72982012-06-19 16:31:53 +0100283
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000284 if (!ret)
Mark Brown3cc72982012-06-19 16:31:53 +0100285 regmap_write(arizona->regmap, ARIZONA_INTERRUPT_STATUS_5,
286 ARIZONA_BOOT_DONE_STS);
Mark Brown3cc72982012-06-19 16:31:53 +0100287
288 pm_runtime_mark_last_busy(arizona->dev);
289
Charles Keepax9d53dfd2013-03-26 17:38:45 +0000290 return ret;
Mark Brown3cc72982012-06-19 16:31:53 +0100291}
292
Charles Keepax22298752015-05-11 13:58:02 +0100293static inline void arizona_enable_reset(struct arizona *arizona)
294{
295 if (arizona->pdata.reset)
Charles Keepaxc1860462018-03-12 15:52:00 +0000296 gpiod_set_raw_value_cansleep(arizona->pdata.reset, 0);
Charles Keepax22298752015-05-11 13:58:02 +0100297}
298
299static void arizona_disable_reset(struct arizona *arizona)
300{
301 if (arizona->pdata.reset) {
Charles Keepax121c0752015-05-11 13:58:10 +0100302 switch (arizona->type) {
303 case WM5110:
304 case WM8280:
305 /* Meet requirements for minimum reset duration */
Lee Jonesb79a9802015-10-28 12:42:30 +0000306 usleep_range(5000, 10000);
Charles Keepax121c0752015-05-11 13:58:10 +0100307 break;
308 default:
309 break;
310 }
311
Charles Keepaxc1860462018-03-12 15:52:00 +0000312 gpiod_set_raw_value_cansleep(arizona->pdata.reset, 1);
Lee Jonesb79a9802015-10-28 12:42:30 +0000313 usleep_range(1000, 5000);
Charles Keepax22298752015-05-11 13:58:02 +0100314 }
315}
316
Charles Keepax3850e3e2015-05-11 13:58:05 +0100317struct arizona_sysclk_state {
318 unsigned int fll;
319 unsigned int sysclk;
320};
321
322static int arizona_enable_freerun_sysclk(struct arizona *arizona,
323 struct arizona_sysclk_state *state)
Charles Keepaxe80436b2013-03-26 18:46:15 +0000324{
Charles Keepaxe80436b2013-03-26 18:46:15 +0000325 int ret, err;
326
Charles Keepaxe80436b2013-03-26 18:46:15 +0000327 /* Cache existing FLL and SYSCLK settings */
Charles Keepax3850e3e2015-05-11 13:58:05 +0100328 ret = regmap_read(arizona->regmap, ARIZONA_FLL1_CONTROL_1, &state->fll);
Charles Keepax0be068a2015-05-11 13:58:04 +0100329 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000330 dev_err(arizona->dev, "Failed to cache FLL settings: %d\n",
331 ret);
332 return ret;
333 }
Charles Keepax3850e3e2015-05-11 13:58:05 +0100334 ret = regmap_read(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1,
335 &state->sysclk);
Charles Keepax0be068a2015-05-11 13:58:04 +0100336 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000337 dev_err(arizona->dev, "Failed to cache SYSCLK settings: %d\n",
338 ret);
339 return ret;
340 }
341
342 /* Start up SYSCLK using the FLL in free running mode */
343 ret = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1,
344 ARIZONA_FLL1_ENA | ARIZONA_FLL1_FREERUN);
Charles Keepax0be068a2015-05-11 13:58:04 +0100345 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000346 dev_err(arizona->dev,
347 "Failed to start FLL in freerunning mode: %d\n",
348 ret);
349 return ret;
350 }
Charles Keepaxef84f882017-03-15 14:58:38 +0000351 ret = arizona_poll_reg(arizona, 180, ARIZONA_INTERRUPT_RAW_STATUS_5,
Charles Keepaxe80436b2013-03-26 18:46:15 +0000352 ARIZONA_FLL1_CLOCK_OK_STS,
353 ARIZONA_FLL1_CLOCK_OK_STS);
Charles Keepaxde4ea102017-03-15 14:58:36 +0000354 if (ret)
Charles Keepaxe80436b2013-03-26 18:46:15 +0000355 goto err_fll;
Charles Keepaxe80436b2013-03-26 18:46:15 +0000356
357 ret = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1, 0x0144);
Charles Keepax0be068a2015-05-11 13:58:04 +0100358 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000359 dev_err(arizona->dev, "Failed to start SYSCLK: %d\n", ret);
360 goto err_fll;
361 }
362
Charles Keepax3850e3e2015-05-11 13:58:05 +0100363 return 0;
364
365err_fll:
366 err = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, state->fll);
367 if (err)
368 dev_err(arizona->dev,
369 "Failed to re-apply old FLL settings: %d\n", err);
370
371 return ret;
372}
373
374static int arizona_disable_freerun_sysclk(struct arizona *arizona,
375 struct arizona_sysclk_state *state)
376{
377 int ret;
378
379 ret = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1,
380 state->sysclk);
381 if (ret) {
382 dev_err(arizona->dev,
383 "Failed to re-apply old SYSCLK settings: %d\n", ret);
384 return ret;
385 }
386
387 ret = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, state->fll);
388 if (ret) {
389 dev_err(arizona->dev,
390 "Failed to re-apply old FLL settings: %d\n", ret);
391 return ret;
392 }
393
394 return 0;
395}
396
397static int wm5102_apply_hardware_patch(struct arizona *arizona)
398{
399 struct arizona_sysclk_state state;
400 int err, ret;
401
402 ret = arizona_enable_freerun_sysclk(arizona, &state);
403 if (ret)
404 return ret;
405
Charles Keepaxe80436b2013-03-26 18:46:15 +0000406 /* Start the write sequencer and wait for it to finish */
407 ret = regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0,
Charles Keepax0be068a2015-05-11 13:58:04 +0100408 ARIZONA_WSEQ_ENA | ARIZONA_WSEQ_START | 160);
409 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000410 dev_err(arizona->dev, "Failed to start write sequencer: %d\n",
411 ret);
Charles Keepax3850e3e2015-05-11 13:58:05 +0100412 goto err;
Charles Keepaxe80436b2013-03-26 18:46:15 +0000413 }
Charles Keepax3850e3e2015-05-11 13:58:05 +0100414
Charles Keepaxef84f882017-03-15 14:58:38 +0000415 ret = arizona_poll_reg(arizona, 30, ARIZONA_WRITE_SEQUENCER_CTRL_1,
Charles Keepaxe80436b2013-03-26 18:46:15 +0000416 ARIZONA_WSEQ_BUSY, 0);
Charles Keepaxde4ea102017-03-15 14:58:36 +0000417 if (ret)
Charles Keepaxe80436b2013-03-26 18:46:15 +0000418 regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0,
Charles Keepax0be068a2015-05-11 13:58:04 +0100419 ARIZONA_WSEQ_ABORT);
Charles Keepaxe80436b2013-03-26 18:46:15 +0000420
Charles Keepax3850e3e2015-05-11 13:58:05 +0100421err:
422 err = arizona_disable_freerun_sysclk(arizona, &state);
Charles Keepaxe80436b2013-03-26 18:46:15 +0000423
Charles Keepax0be068a2015-05-11 13:58:04 +0100424 return ret ?: err;
Charles Keepaxe80436b2013-03-26 18:46:15 +0000425}
426
Charles Keepax882bc462015-05-11 13:58:06 +0100427/*
428 * Register patch to some of the CODECs internal write sequences
429 * to ensure a clean exit from the low power sleep state.
430 */
Nariman Poushin8019ff62015-07-16 16:36:21 +0100431static const struct reg_sequence wm5110_sleep_patch[] = {
Charles Keepax882bc462015-05-11 13:58:06 +0100432 { 0x337A, 0xC100 },
433 { 0x337B, 0x0041 },
434 { 0x3300, 0xA210 },
435 { 0x3301, 0x050C },
436};
437
438static int wm5110_apply_sleep_patch(struct arizona *arizona)
439{
440 struct arizona_sysclk_state state;
441 int err, ret;
442
443 ret = arizona_enable_freerun_sysclk(arizona, &state);
444 if (ret)
445 return ret;
446
447 ret = regmap_multi_reg_write_bypassed(arizona->regmap,
448 wm5110_sleep_patch,
449 ARRAY_SIZE(wm5110_sleep_patch));
450
451 err = arizona_disable_freerun_sysclk(arizona, &state);
452
453 return ret ?: err;
454}
455
Charles Keepax1c1c6bb2015-05-11 13:58:03 +0100456static int wm5102_clear_write_sequencer(struct arizona *arizona)
457{
458 int ret;
459
460 ret = regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_3,
461 0x0);
462 if (ret) {
463 dev_err(arizona->dev,
464 "Failed to clear write sequencer state: %d\n", ret);
465 return ret;
466 }
467
468 arizona_enable_reset(arizona);
469 regulator_disable(arizona->dcvdd);
470
471 msleep(20);
472
473 ret = regulator_enable(arizona->dcvdd);
474 if (ret) {
475 dev_err(arizona->dev, "Failed to re-enable DCVDD: %d\n", ret);
476 return ret;
477 }
478 arizona_disable_reset(arizona);
479
480 return 0;
481}
482
Rafael J. Wysocki48bb9fe2014-12-05 03:04:12 +0100483#ifdef CONFIG_PM
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100484static int arizona_isolate_dcvdd(struct arizona *arizona)
485{
486 int ret;
487
488 ret = regmap_update_bits(arizona->regmap,
489 ARIZONA_ISOLATION_CONTROL,
490 ARIZONA_ISOLATE_DCVDD1,
491 ARIZONA_ISOLATE_DCVDD1);
492 if (ret != 0)
493 dev_err(arizona->dev, "Failed to isolate DCVDD: %d\n", ret);
494
495 return ret;
496}
497
498static int arizona_connect_dcvdd(struct arizona *arizona)
499{
500 int ret;
501
502 ret = regmap_update_bits(arizona->regmap,
503 ARIZONA_ISOLATION_CONTROL,
504 ARIZONA_ISOLATE_DCVDD1, 0);
505 if (ret != 0)
506 dev_err(arizona->dev, "Failed to connect DCVDD: %d\n", ret);
507
508 return ret;
509}
510
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100511static int arizona_is_jack_det_active(struct arizona *arizona)
512{
513 unsigned int val;
514 int ret;
515
516 ret = regmap_read(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE, &val);
517 if (ret) {
518 dev_err(arizona->dev,
519 "Failed to check jack det status: %d\n", ret);
520 return ret;
521 } else if (val & ARIZONA_JD1_ENA) {
522 return 1;
523 } else {
524 return 0;
525 }
526}
527
Mark Brown3cc72982012-06-19 16:31:53 +0100528static int arizona_runtime_resume(struct device *dev)
529{
530 struct arizona *arizona = dev_get_drvdata(dev);
531 int ret;
532
Mark Brown508c8292012-07-20 17:09:12 +0100533 dev_dbg(arizona->dev, "Leaving AoD mode\n");
534
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100535 if (arizona->has_fully_powered_off) {
536 dev_dbg(arizona->dev, "Re-enabling core supplies\n");
537
538 ret = regulator_bulk_enable(arizona->num_core_supplies,
539 arizona->core_supplies);
540 if (ret) {
541 dev_err(dev, "Failed to enable core supplies: %d\n",
542 ret);
543 return ret;
544 }
545 }
546
Mark Brown59db9692012-07-09 00:31:36 +0200547 ret = regulator_enable(arizona->dcvdd);
548 if (ret != 0) {
549 dev_err(arizona->dev, "Failed to enable DCVDD: %d\n", ret);
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100550 if (arizona->has_fully_powered_off)
551 regulator_bulk_disable(arizona->num_core_supplies,
552 arizona->core_supplies);
Mark Brown59db9692012-07-09 00:31:36 +0200553 return ret;
554 }
Mark Brown3cc72982012-06-19 16:31:53 +0100555
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100556 if (arizona->has_fully_powered_off) {
557 arizona_disable_reset(arizona);
558 enable_irq(arizona->irq);
559 arizona->has_fully_powered_off = false;
560 }
561
Mark Brown3cc72982012-06-19 16:31:53 +0100562 regcache_cache_only(arizona->regmap, false);
563
Charles Keepax4c9bb8b2013-03-26 18:01:49 +0000564 switch (arizona->type) {
565 case WM5102:
Mark Brown59274672013-04-23 19:44:16 +0100566 if (arizona->external_dcvdd) {
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100567 ret = arizona_connect_dcvdd(arizona);
568 if (ret != 0)
Mark Brown59274672013-04-23 19:44:16 +0100569 goto err;
Mark Brown59274672013-04-23 19:44:16 +0100570 }
571
Charles Keepax4c9bb8b2013-03-26 18:01:49 +0000572 ret = wm5102_patch(arizona);
573 if (ret != 0) {
574 dev_err(arizona->dev, "Failed to apply patch: %d\n",
575 ret);
576 goto err;
577 }
Charles Keepaxe80436b2013-03-26 18:46:15 +0000578
Charles Keepax0be068a2015-05-11 13:58:04 +0100579 ret = wm5102_apply_hardware_patch(arizona);
580 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +0000581 dev_err(arizona->dev,
582 "Failed to apply hardware patch: %d\n",
583 ret);
584 goto err;
585 }
586 break;
Charles Keepax96129a02015-05-11 13:58:08 +0100587 case WM5110:
588 case WM8280:
589 ret = arizona_wait_for_boot(arizona);
590 if (ret)
591 goto err;
592
593 if (arizona->external_dcvdd) {
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100594 ret = arizona_connect_dcvdd(arizona);
595 if (ret != 0)
Charles Keepax96129a02015-05-11 13:58:08 +0100596 goto err;
Charles Keepax96129a02015-05-11 13:58:08 +0100597 } else {
598 /*
599 * As this is only called for the internal regulator
600 * (where we know voltage ranges available) it is ok
601 * to request an exact range.
602 */
603 ret = regulator_set_voltage(arizona->dcvdd,
604 1200000, 1200000);
605 if (ret < 0) {
606 dev_err(arizona->dev,
607 "Failed to set resume voltage: %d\n",
608 ret);
609 goto err;
610 }
611 }
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100612
613 ret = wm5110_apply_sleep_patch(arizona);
614 if (ret) {
615 dev_err(arizona->dev,
616 "Failed to re-apply sleep patch: %d\n",
617 ret);
618 goto err;
619 }
Charles Keepax96129a02015-05-11 13:58:08 +0100620 break;
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000621 case WM1831:
622 case CS47L24:
623 ret = arizona_wait_for_boot(arizona);
624 if (ret != 0)
625 goto err;
626 break;
Charles Keepaxe80436b2013-03-26 18:46:15 +0000627 default:
Charles Keepax12bb68e2013-03-27 09:49:40 +0000628 ret = arizona_wait_for_boot(arizona);
Charles Keepax3762aed2015-08-11 09:34:31 +0100629 if (ret != 0)
Charles Keepax12bb68e2013-03-27 09:49:40 +0000630 goto err;
Charles Keepax12bb68e2013-03-27 09:49:40 +0000631
Mark Brown59274672013-04-23 19:44:16 +0100632 if (arizona->external_dcvdd) {
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100633 ret = arizona_connect_dcvdd(arizona);
634 if (ret != 0)
Mark Brown59274672013-04-23 19:44:16 +0100635 goto err;
Mark Brown59274672013-04-23 19:44:16 +0100636 }
Charles Keepaxe80436b2013-03-26 18:46:15 +0000637 break;
Charles Keepax4c9bb8b2013-03-26 18:01:49 +0000638 }
639
Mark Brown9270bdf2013-01-04 17:16:12 +0000640 ret = regcache_sync(arizona->regmap);
641 if (ret != 0) {
642 dev_err(arizona->dev, "Failed to restore register cache\n");
Mark Brown4816bd12013-01-14 15:50:38 +0900643 goto err;
Mark Brown9270bdf2013-01-04 17:16:12 +0000644 }
Mark Brown3cc72982012-06-19 16:31:53 +0100645
646 return 0;
Mark Brown4816bd12013-01-14 15:50:38 +0900647
648err:
649 regcache_cache_only(arizona->regmap, true);
650 regulator_disable(arizona->dcvdd);
651 return ret;
Mark Brown3cc72982012-06-19 16:31:53 +0100652}
653
654static int arizona_runtime_suspend(struct device *dev)
655{
656 struct arizona *arizona = dev_get_drvdata(dev);
Dan Carpentera05950a2015-10-19 13:18:05 +0300657 int jd_active = 0;
Mark Brown59274672013-04-23 19:44:16 +0100658 int ret;
Mark Brown3cc72982012-06-19 16:31:53 +0100659
Mark Brown508c8292012-07-20 17:09:12 +0100660 dev_dbg(arizona->dev, "Entering AoD mode\n");
661
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100662 switch (arizona->type) {
663 case WM5110:
664 case WM8280:
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100665 jd_active = arizona_is_jack_det_active(arizona);
666 if (jd_active < 0)
667 return jd_active;
668
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100669 if (arizona->external_dcvdd) {
670 ret = arizona_isolate_dcvdd(arizona);
671 if (ret != 0)
672 return ret;
673 } else {
674 /*
675 * As this is only called for the internal regulator
676 * (where we know voltage ranges available) it is ok
677 * to request an exact range.
678 */
679 ret = regulator_set_voltage(arizona->dcvdd,
680 1175000, 1175000);
681 if (ret < 0) {
682 dev_err(arizona->dev,
683 "Failed to set suspend voltage: %d\n",
684 ret);
685 return ret;
686 }
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100687 }
688 break;
689 case WM5102:
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100690 jd_active = arizona_is_jack_det_active(arizona);
691 if (jd_active < 0)
692 return jd_active;
693
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100694 if (arizona->external_dcvdd) {
695 ret = arizona_isolate_dcvdd(arizona);
696 if (ret != 0)
697 return ret;
698 }
699
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100700 if (!jd_active) {
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100701 ret = regmap_write(arizona->regmap,
702 ARIZONA_WRITE_SEQUENCER_CTRL_3, 0x0);
703 if (ret) {
Charles Keepax96129a02015-05-11 13:58:08 +0100704 dev_err(arizona->dev,
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100705 "Failed to clear write sequencer: %d\n",
Charles Keepax96129a02015-05-11 13:58:08 +0100706 ret);
707 return ret;
708 }
Charles Keepax96129a02015-05-11 13:58:08 +0100709 }
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100710 break;
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000711 case WM1831:
712 case CS47L24:
713 break;
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100714 default:
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100715 jd_active = arizona_is_jack_det_active(arizona);
716 if (jd_active < 0)
717 return jd_active;
718
Richard Fitzgeralde7811142015-10-02 13:29:10 +0100719 if (arizona->external_dcvdd) {
720 ret = arizona_isolate_dcvdd(arizona);
721 if (ret != 0)
722 return ret;
723 }
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100724 break;
Mark Brown59274672013-04-23 19:44:16 +0100725 }
726
Mark Brown59db9692012-07-09 00:31:36 +0200727 regcache_cache_only(arizona->regmap, true);
728 regcache_mark_dirty(arizona->regmap);
Charles Keepaxe293e84722013-08-06 17:18:35 +0100729 regulator_disable(arizona->dcvdd);
Mark Brown3cc72982012-06-19 16:31:53 +0100730
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100731 /* Allow us to completely power down if no jack detection */
Richard Fitzgeralde3424272015-10-02 13:29:11 +0100732 if (!jd_active) {
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100733 dev_dbg(arizona->dev, "Fully powering off\n");
734
735 arizona->has_fully_powered_off = true;
736
Charles Keepax11150922015-06-14 15:41:49 +0100737 disable_irq_nosync(arizona->irq);
Charles Keepaxe6cb7342015-05-11 13:58:09 +0100738 arizona_enable_reset(arizona);
739 regulator_bulk_disable(arizona->num_core_supplies,
740 arizona->core_supplies);
741 }
742
Mark Brown3cc72982012-06-19 16:31:53 +0100743 return 0;
744}
745#endif
746
Mark Browndc781d02013-01-27 12:07:32 +0800747#ifdef CONFIG_PM_SLEEP
Mark Brown67c99292013-04-10 12:40:26 +0100748static int arizona_suspend(struct device *dev)
749{
750 struct arizona *arizona = dev_get_drvdata(dev);
751
752 dev_dbg(arizona->dev, "Suspend, disabling IRQ\n");
753 disable_irq(arizona->irq);
754
755 return 0;
756}
757
Charles Keepax3612b272016-08-30 10:33:10 +0100758static int arizona_suspend_noirq(struct device *dev)
Mark Brown67c99292013-04-10 12:40:26 +0100759{
760 struct arizona *arizona = dev_get_drvdata(dev);
761
762 dev_dbg(arizona->dev, "Late suspend, reenabling IRQ\n");
763 enable_irq(arizona->irq);
764
765 return 0;
766}
767
Mark Browndc781d02013-01-27 12:07:32 +0800768static int arizona_resume_noirq(struct device *dev)
769{
770 struct arizona *arizona = dev_get_drvdata(dev);
771
772 dev_dbg(arizona->dev, "Early resume, disabling IRQ\n");
773 disable_irq(arizona->irq);
774
775 return 0;
776}
777
778static int arizona_resume(struct device *dev)
779{
780 struct arizona *arizona = dev_get_drvdata(dev);
781
Charles Keepax3612b272016-08-30 10:33:10 +0100782 dev_dbg(arizona->dev, "Resume, reenabling IRQ\n");
Mark Browndc781d02013-01-27 12:07:32 +0800783 enable_irq(arizona->irq);
784
785 return 0;
786}
787#endif
788
Mark Brown3cc72982012-06-19 16:31:53 +0100789const struct dev_pm_ops arizona_pm_ops = {
790 SET_RUNTIME_PM_OPS(arizona_runtime_suspend,
791 arizona_runtime_resume,
792 NULL)
Mark Brown67c99292013-04-10 12:40:26 +0100793 SET_SYSTEM_SLEEP_PM_OPS(arizona_suspend, arizona_resume)
Charles Keepax3612b272016-08-30 10:33:10 +0100794 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(arizona_suspend_noirq,
795 arizona_resume_noirq)
Mark Brown3cc72982012-06-19 16:31:53 +0100796};
797EXPORT_SYMBOL_GPL(arizona_pm_ops);
798
Mark Brownd7810092013-03-25 00:11:27 +0000799#ifdef CONFIG_OF
Mark Brownd7810092013-03-25 00:11:27 +0000800static int arizona_of_get_core_pdata(struct arizona *arizona)
801{
Charles Keepaxe4fcb1d2014-04-16 10:01:37 +0100802 struct arizona_pdata *pdata = &arizona->pdata;
Mark Brownd7810092013-03-25 00:11:27 +0000803 int ret, i;
804
Charles Keepaxc1860462018-03-12 15:52:00 +0000805 /* Handle old non-standard DT binding */
Dmitry Torokhov5da3f762019-10-02 17:28:32 -0700806 pdata->reset = devm_gpiod_get(arizona->dev, "wlf,reset", GPIOD_OUT_LOW);
Charles Keepaxc1860462018-03-12 15:52:00 +0000807 if (IS_ERR(pdata->reset)) {
808 ret = PTR_ERR(pdata->reset);
Charles Keepax1961531d2016-09-20 16:30:13 +0100809
Charles Keepaxc1860462018-03-12 15:52:00 +0000810 /*
811 * Reset missing will be caught when other binding is read
812 * but all other errors imply this binding is in use but has
813 * encountered a problem so should be handled.
814 */
815 if (ret == -EPROBE_DEFER)
816 return ret;
817 else if (ret != -ENOENT && ret != -ENOSYS)
818 dev_err(arizona->dev, "Reset GPIO malformed: %d\n",
819 ret);
820
821 pdata->reset = NULL;
Charles Keepax1961531d2016-09-20 16:30:13 +0100822 }
Mark Brownd7810092013-03-25 00:11:27 +0000823
824 ret = of_property_read_u32_array(arizona->dev->of_node,
825 "wlf,gpio-defaults",
Charles Keepax3762aed2015-08-11 09:34:31 +0100826 pdata->gpio_defaults,
827 ARRAY_SIZE(pdata->gpio_defaults));
Mark Brownd7810092013-03-25 00:11:27 +0000828 if (ret >= 0) {
829 /*
830 * All values are literal except out of range values
831 * which are chip default, translate into platform
832 * data which uses 0 as chip default and out of range
833 * as zero.
834 */
Charles Keepax3762aed2015-08-11 09:34:31 +0100835 for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
836 if (pdata->gpio_defaults[i] > 0xffff)
837 pdata->gpio_defaults[i] = 0;
838 else if (pdata->gpio_defaults[i] == 0)
839 pdata->gpio_defaults[i] = 0x10000;
Mark Brownd7810092013-03-25 00:11:27 +0000840 }
841 } else {
842 dev_err(arizona->dev, "Failed to parse GPIO defaults: %d\n",
843 ret);
844 }
845
846 return 0;
847}
848
849const struct of_device_id arizona_of_match[] = {
850 { .compatible = "wlf,wm5102", .data = (void *)WM5102 },
851 { .compatible = "wlf,wm5110", .data = (void *)WM5110 },
Richard Fitzgeralde5d4ef02015-01-17 15:21:22 +0000852 { .compatible = "wlf,wm8280", .data = (void *)WM8280 },
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100853 { .compatible = "wlf,wm8997", .data = (void *)WM8997 },
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100854 { .compatible = "wlf,wm8998", .data = (void *)WM8998 },
855 { .compatible = "wlf,wm1814", .data = (void *)WM1814 },
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000856 { .compatible = "wlf,wm1831", .data = (void *)WM1831 },
857 { .compatible = "cirrus,cs47l24", .data = (void *)CS47L24 },
Mark Brownd7810092013-03-25 00:11:27 +0000858 {},
859};
860EXPORT_SYMBOL_GPL(arizona_of_match);
861#else
862static inline int arizona_of_get_core_pdata(struct arizona *arizona)
863{
864 return 0;
865}
866#endif
867
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +0100868static const struct mfd_cell early_devs[] = {
Mark Brown3cc72982012-06-19 16:31:53 +0100869 { .name = "arizona-ldo1" },
870};
871
Charles Keepax3762aed2015-08-11 09:34:31 +0100872static const char * const wm5102_supplies[] = {
Charles Keepax5fc6c392014-07-25 16:24:44 +0100873 "MICVDD",
Charles Keepax32dadef2013-10-15 20:14:22 +0100874 "DBVDD2",
875 "DBVDD3",
876 "CPVDD",
877 "SPKVDDL",
878 "SPKVDDR",
879};
880
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +0100881static const struct mfd_cell wm5102_devs[] = {
Mark Brownd7768112012-12-20 15:38:03 +0000882 { .name = "arizona-micsupp" },
Charles Keepaxf83c2182016-04-15 13:18:47 +0100883 { .name = "arizona-gpio" },
Mark Brown503b1ca2012-11-27 17:36:38 +0000884 { .name = "arizona-haptics" },
Mark Brown3cc72982012-06-19 16:31:53 +0100885 { .name = "arizona-pwm" },
Charles Keepax32dadef2013-10-15 20:14:22 +0100886 {
887 .name = "wm5102-codec",
888 .parent_supplies = wm5102_supplies,
889 .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
890 },
Mark Brown3cc72982012-06-19 16:31:53 +0100891};
892
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +0100893static const struct mfd_cell wm5110_devs[] = {
Mark Brownd7768112012-12-20 15:38:03 +0000894 { .name = "arizona-micsupp" },
Charles Keepaxf83c2182016-04-15 13:18:47 +0100895 { .name = "arizona-gpio" },
Mark Brown503b1ca2012-11-27 17:36:38 +0000896 { .name = "arizona-haptics" },
Mark Browne102bef2012-07-10 12:37:58 +0100897 { .name = "arizona-pwm" },
Charles Keepax32dadef2013-10-15 20:14:22 +0100898 {
899 .name = "wm5110-codec",
900 .parent_supplies = wm5102_supplies,
901 .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
902 },
903};
904
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000905static const char * const cs47l24_supplies[] = {
906 "MICVDD",
907 "CPVDD",
908 "SPKVDD",
909};
910
911static const struct mfd_cell cs47l24_devs[] = {
912 { .name = "arizona-gpio" },
913 { .name = "arizona-haptics" },
914 { .name = "arizona-pwm" },
915 {
916 .name = "cs47l24-codec",
917 .parent_supplies = cs47l24_supplies,
918 .num_parent_supplies = ARRAY_SIZE(cs47l24_supplies),
919 },
920};
921
Charles Keepax3762aed2015-08-11 09:34:31 +0100922static const char * const wm8997_supplies[] = {
Charles Keepax996c2d42014-07-25 16:24:43 +0100923 "MICVDD",
Charles Keepax32dadef2013-10-15 20:14:22 +0100924 "DBVDD2",
925 "CPVDD",
926 "SPKVDD",
Mark Browne102bef2012-07-10 12:37:58 +0100927};
928
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +0100929static const struct mfd_cell wm8997_devs[] = {
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100930 { .name = "arizona-micsupp" },
Charles Keepaxf83c2182016-04-15 13:18:47 +0100931 { .name = "arizona-gpio" },
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100932 { .name = "arizona-haptics" },
933 { .name = "arizona-pwm" },
Charles Keepax32dadef2013-10-15 20:14:22 +0100934 {
935 .name = "wm8997-codec",
936 .parent_supplies = wm8997_supplies,
937 .num_parent_supplies = ARRAY_SIZE(wm8997_supplies),
938 },
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100939};
940
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100941static const struct mfd_cell wm8998_devs[] = {
Charles Keepaxf83c2182016-04-15 13:18:47 +0100942 { .name = "arizona-micsupp" },
943 { .name = "arizona-gpio" },
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100944 { .name = "arizona-haptics" },
945 { .name = "arizona-pwm" },
946 {
947 .name = "wm8998-codec",
948 .parent_supplies = wm5102_supplies,
949 .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
950 },
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100951};
952
Bill Pembertonf791be42012-11-19 13:23:04 -0500953int arizona_dev_init(struct arizona *arizona)
Mark Brown3cc72982012-06-19 16:31:53 +0100954{
Colin Ian King8e27a562018-09-04 15:15:53 +0100955 static const char * const mclk_name[] = { "mclk1", "mclk2" };
Mark Brown3cc72982012-06-19 16:31:53 +0100956 struct device *dev = arizona->dev;
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000957 const char *type_name = NULL;
Charles Keepax6000c992017-09-04 16:41:51 +0100958 unsigned int reg, val;
Mark Brown62d62b52012-12-02 11:41:46 +0900959 int (*apply_patch)(struct arizona *) = NULL;
Richard Fitzgeraldae05ea362015-10-02 13:29:13 +0100960 const struct mfd_cell *subdevs = NULL;
Arnd Bergmann5da6cbc2019-05-20 10:06:25 +0100961 int n_subdevs = 0, ret, i;
Mark Brown3cc72982012-06-19 16:31:53 +0100962
963 dev_set_drvdata(arizona->dev, arizona);
964 mutex_init(&arizona->clk_lock);
965
Charles Keepaxb8d336e2016-09-20 16:30:14 +0100966 if (dev_get_platdata(arizona->dev)) {
Mark Brown3cc72982012-06-19 16:31:53 +0100967 memcpy(&arizona->pdata, dev_get_platdata(arizona->dev),
968 sizeof(arizona->pdata));
Charles Keepaxb8d336e2016-09-20 16:30:14 +0100969 } else {
970 ret = arizona_of_get_core_pdata(arizona);
971 if (ret < 0)
972 return ret;
973 }
Mark Brown3cc72982012-06-19 16:31:53 +0100974
Sylwester Nawrockicdd8da82016-09-02 16:52:46 +0100975 BUILD_BUG_ON(ARRAY_SIZE(arizona->mclk) != ARRAY_SIZE(mclk_name));
976 for (i = 0; i < ARRAY_SIZE(arizona->mclk); i++) {
977 arizona->mclk[i] = devm_clk_get(arizona->dev, mclk_name[i]);
978 if (IS_ERR(arizona->mclk[i])) {
979 dev_info(arizona->dev, "Failed to get %s: %ld\n",
980 mclk_name[i], PTR_ERR(arizona->mclk[i]));
981 arizona->mclk[i] = NULL;
982 }
983 }
984
Mark Brown3cc72982012-06-19 16:31:53 +0100985 regcache_cache_only(arizona->regmap, true);
986
987 switch (arizona->type) {
988 case WM5102:
Mark Browne102bef2012-07-10 12:37:58 +0100989 case WM5110:
Richard Fitzgeralde5d4ef02015-01-17 15:21:22 +0000990 case WM8280:
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100991 case WM8997:
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100992 case WM8998:
993 case WM1814:
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000994 case WM1831:
995 case CS47L24:
Mark Brown3cc72982012-06-19 16:31:53 +0100996 for (i = 0; i < ARRAY_SIZE(wm5102_core_supplies); i++)
997 arizona->core_supplies[i].supply
998 = wm5102_core_supplies[i];
999 arizona->num_core_supplies = ARRAY_SIZE(wm5102_core_supplies);
1000 break;
1001 default:
1002 dev_err(arizona->dev, "Unknown device type %d\n",
1003 arizona->type);
Charles Keepax75d8a2b2016-08-31 10:41:30 +01001004 return -ENODEV;
Mark Brown3cc72982012-06-19 16:31:53 +01001005 }
1006
Charles Keepax4a8c4752014-04-16 10:01:38 +01001007 /* Mark DCVDD as external, LDO1 driver will clear if internal */
1008 arizona->external_dcvdd = true;
1009
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +00001010 switch (arizona->type) {
1011 case WM1831:
1012 case CS47L24:
1013 break; /* No LDO1 regulator */
1014 default:
1015 ret = mfd_add_devices(arizona->dev, -1, early_devs,
1016 ARRAY_SIZE(early_devs), NULL, 0, NULL);
1017 if (ret != 0) {
1018 dev_err(dev, "Failed to add early children: %d\n", ret);
1019 return ret;
1020 }
1021 break;
Mark Brown3cc72982012-06-19 16:31:53 +01001022 }
1023
1024 ret = devm_regulator_bulk_get(dev, arizona->num_core_supplies,
1025 arizona->core_supplies);
1026 if (ret != 0) {
1027 dev_err(dev, "Failed to request core supplies: %d\n",
1028 ret);
1029 goto err_early;
1030 }
1031
Charles Keepax0c2d0ff2014-06-19 16:04:23 +01001032 /**
1033 * Don't use devres here because the only device we have to get
1034 * against is the MFD device and DCVDD will likely be supplied by
1035 * one of its children. Meaning that the regulator will be
1036 * destroyed by the time devres calls regulator put.
1037 */
Charles Keepaxe6021512014-06-02 09:50:41 +01001038 arizona->dcvdd = regulator_get(arizona->dev, "DCVDD");
Mark Brown59db9692012-07-09 00:31:36 +02001039 if (IS_ERR(arizona->dcvdd)) {
1040 ret = PTR_ERR(arizona->dcvdd);
1041 dev_err(dev, "Failed to request DCVDD: %d\n", ret);
1042 goto err_early;
1043 }
1044
Charles Keepaxc1860462018-03-12 15:52:00 +00001045 if (!arizona->pdata.reset) {
Mark Brown87d3af42013-03-26 12:15:26 +00001046 /* Start out with /RESET low to put the chip into reset */
Charles Keepaxc1860462018-03-12 15:52:00 +00001047 arizona->pdata.reset = devm_gpiod_get(arizona->dev, "reset",
1048 GPIOD_OUT_LOW);
1049 if (IS_ERR(arizona->pdata.reset)) {
1050 ret = PTR_ERR(arizona->pdata.reset);
1051 if (ret == -EPROBE_DEFER)
1052 goto err_dcvdd;
1053
1054 dev_err(arizona->dev,
1055 "Reset GPIO missing/malformed: %d\n", ret);
1056
1057 arizona->pdata.reset = NULL;
Mark Brown87d3af42013-03-26 12:15:26 +00001058 }
1059 }
1060
Mark Brown3cc72982012-06-19 16:31:53 +01001061 ret = regulator_bulk_enable(arizona->num_core_supplies,
1062 arizona->core_supplies);
1063 if (ret != 0) {
1064 dev_err(dev, "Failed to enable core supplies: %d\n",
1065 ret);
Charles Keepaxe6021512014-06-02 09:50:41 +01001066 goto err_dcvdd;
Mark Brown3cc72982012-06-19 16:31:53 +01001067 }
1068
Mark Brown59db9692012-07-09 00:31:36 +02001069 ret = regulator_enable(arizona->dcvdd);
1070 if (ret != 0) {
1071 dev_err(dev, "Failed to enable DCVDD: %d\n", ret);
1072 goto err_enable;
1073 }
1074
Charles Keepax22298752015-05-11 13:58:02 +01001075 arizona_disable_reset(arizona);
Mark Brown3cc72982012-06-19 16:31:53 +01001076
Mark Brown3cc72982012-06-19 16:31:53 +01001077 regcache_cache_only(arizona->regmap, false);
1078
Mark Brownca76ceb2013-04-09 16:04:35 +01001079 /* Verify that this is a chip we know about */
1080 ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, &reg);
1081 if (ret != 0) {
1082 dev_err(dev, "Failed to read ID register: %d\n", ret);
1083 goto err_reset;
1084 }
1085
1086 switch (reg) {
1087 case 0x5102:
1088 case 0x5110:
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001089 case 0x6349:
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +00001090 case 0x6363:
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001091 case 0x8997:
Mark Brownca76ceb2013-04-09 16:04:35 +01001092 break;
1093 default:
1094 dev_err(arizona->dev, "Unknown device ID: %x\n", reg);
Charles Keepax75d8a2b2016-08-31 10:41:30 +01001095 ret = -ENODEV;
Mark Brownca76ceb2013-04-09 16:04:35 +01001096 goto err_reset;
1097 }
1098
1099 /* If we have a /RESET GPIO we'll already be reset */
1100 if (!arizona->pdata.reset) {
Mark Brownca76ceb2013-04-09 16:04:35 +01001101 ret = regmap_write(arizona->regmap, ARIZONA_SOFTWARE_RESET, 0);
1102 if (ret != 0) {
1103 dev_err(dev, "Failed to reset device: %d\n", ret);
1104 goto err_reset;
1105 }
1106
Lee Jonesb79a9802015-10-28 12:42:30 +00001107 usleep_range(1000, 5000);
Mark Brownca76ceb2013-04-09 16:04:35 +01001108 }
1109
1110 /* Ensure device startup is complete */
1111 switch (arizona->type) {
1112 case WM5102:
Mark Brown48018942014-08-13 11:42:46 +01001113 ret = regmap_read(arizona->regmap,
1114 ARIZONA_WRITE_SEQUENCER_CTRL_3, &val);
Charles Keepax1c1c6bb2015-05-11 13:58:03 +01001115 if (ret) {
Mark Brownca76ceb2013-04-09 16:04:35 +01001116 dev_err(dev,
1117 "Failed to check write sequencer state: %d\n",
1118 ret);
Charles Keepax1c1c6bb2015-05-11 13:58:03 +01001119 } else if (val & 0x01) {
1120 ret = wm5102_clear_write_sequencer(arizona);
1121 if (ret)
1122 return ret;
Mark Brownca76ceb2013-04-09 16:04:35 +01001123 }
1124 break;
Charles Keepax1c1c6bb2015-05-11 13:58:03 +01001125 default:
1126 break;
1127 }
1128
1129 ret = arizona_wait_for_boot(arizona);
1130 if (ret) {
1131 dev_err(arizona->dev, "Device failed initial boot: %d\n", ret);
1132 goto err_reset;
Mark Brownca76ceb2013-04-09 16:04:35 +01001133 }
1134
1135 /* Read the device ID information & do device specific stuff */
Mark Brown3cc72982012-06-19 16:31:53 +01001136 ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, &reg);
1137 if (ret != 0) {
1138 dev_err(dev, "Failed to read ID register: %d\n", ret);
Mark Brown59db9692012-07-09 00:31:36 +02001139 goto err_reset;
Mark Brown3cc72982012-06-19 16:31:53 +01001140 }
1141
1142 ret = regmap_read(arizona->regmap, ARIZONA_DEVICE_REVISION,
1143 &arizona->rev);
1144 if (ret != 0) {
1145 dev_err(dev, "Failed to read revision register: %d\n", ret);
Mark Brown59db9692012-07-09 00:31:36 +02001146 goto err_reset;
Mark Brown3cc72982012-06-19 16:31:53 +01001147 }
1148 arizona->rev &= ARIZONA_DEVICE_REVISION_MASK;
1149
1150 switch (reg) {
1151 case 0x5102:
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001152 if (IS_ENABLED(CONFIG_MFD_WM5102)) {
1153 type_name = "WM5102";
1154 if (arizona->type != WM5102) {
1155 dev_warn(arizona->dev,
1156 "WM5102 registered as %d\n",
1157 arizona->type);
1158 arizona->type = WM5102;
1159 }
1160
1161 apply_patch = wm5102_patch;
1162 arizona->rev &= 0x7;
1163 subdevs = wm5102_devs;
1164 n_subdevs = ARRAY_SIZE(wm5102_devs);
Mark Brown3cc72982012-06-19 16:31:53 +01001165 }
Mark Brown3cc72982012-06-19 16:31:53 +01001166 break;
Mark Browne102bef2012-07-10 12:37:58 +01001167 case 0x5110:
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001168 if (IS_ENABLED(CONFIG_MFD_WM5110)) {
1169 switch (arizona->type) {
1170 case WM5110:
1171 type_name = "WM5110";
1172 break;
1173 case WM8280:
1174 type_name = "WM8280";
1175 break;
1176 default:
1177 type_name = "WM5110";
1178 dev_warn(arizona->dev,
1179 "WM5110 registered as %d\n",
1180 arizona->type);
1181 arizona->type = WM5110;
1182 break;
1183 }
1184
1185 apply_patch = wm5110_patch;
1186 subdevs = wm5110_devs;
1187 n_subdevs = ARRAY_SIZE(wm5110_devs);
Mark Browne102bef2012-07-10 12:37:58 +01001188 }
Mark Browne102bef2012-07-10 12:37:58 +01001189 break;
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +00001190 case 0x6363:
1191 if (IS_ENABLED(CONFIG_MFD_CS47L24)) {
1192 switch (arizona->type) {
1193 case CS47L24:
1194 type_name = "CS47L24";
1195 break;
1196
1197 case WM1831:
1198 type_name = "WM1831";
1199 break;
1200
1201 default:
1202 dev_warn(arizona->dev,
1203 "CS47L24 registered as %d\n",
1204 arizona->type);
1205 arizona->type = CS47L24;
1206 break;
1207 }
1208
1209 apply_patch = cs47l24_patch;
1210 subdevs = cs47l24_devs;
1211 n_subdevs = ARRAY_SIZE(cs47l24_devs);
1212 }
1213 break;
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001214 case 0x8997:
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001215 if (IS_ENABLED(CONFIG_MFD_WM8997)) {
1216 type_name = "WM8997";
1217 if (arizona->type != WM8997) {
1218 dev_warn(arizona->dev,
1219 "WM8997 registered as %d\n",
1220 arizona->type);
1221 arizona->type = WM8997;
1222 }
1223
1224 apply_patch = wm8997_patch;
1225 subdevs = wm8997_devs;
1226 n_subdevs = ARRAY_SIZE(wm8997_devs);
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001227 }
Charles Keepaxdc7d4862013-06-13 09:43:29 +01001228 break;
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001229 case 0x6349:
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001230 if (IS_ENABLED(CONFIG_MFD_WM8998)) {
1231 switch (arizona->type) {
1232 case WM8998:
1233 type_name = "WM8998";
1234 break;
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001235
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001236 case WM1814:
1237 type_name = "WM1814";
1238 break;
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001239
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001240 default:
1241 type_name = "WM8998";
1242 dev_warn(arizona->dev,
1243 "WM8998 registered as %d\n",
1244 arizona->type);
1245 arizona->type = WM8998;
1246 }
1247
1248 apply_patch = wm8998_patch;
1249 subdevs = wm8998_devs;
1250 n_subdevs = ARRAY_SIZE(wm8998_devs);
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001251 }
Richard Fitzgerald6887b042015-07-03 16:16:35 +01001252 break;
Mark Brown3cc72982012-06-19 16:31:53 +01001253 default:
1254 dev_err(arizona->dev, "Unknown device ID %x\n", reg);
Charles Keepax75d8a2b2016-08-31 10:41:30 +01001255 ret = -ENODEV;
Mark Brown59db9692012-07-09 00:31:36 +02001256 goto err_reset;
Mark Brown3cc72982012-06-19 16:31:53 +01001257 }
1258
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001259 if (!subdevs) {
1260 dev_err(arizona->dev,
1261 "No kernel support for device ID %x\n", reg);
Charles Keepax75d8a2b2016-08-31 10:41:30 +01001262 ret = -ENODEV;
Richard Fitzgeraldb61c1ec2015-10-02 13:29:14 +01001263 goto err_reset;
1264 }
1265
Mark Brown3cc72982012-06-19 16:31:53 +01001266 dev_info(dev, "%s revision %c\n", type_name, arizona->rev + 'A');
1267
Mark Brown62d62b52012-12-02 11:41:46 +09001268 if (apply_patch) {
1269 ret = apply_patch(arizona);
1270 if (ret != 0) {
1271 dev_err(arizona->dev, "Failed to apply patch: %d\n",
1272 ret);
1273 goto err_reset;
1274 }
Charles Keepaxe80436b2013-03-26 18:46:15 +00001275
1276 switch (arizona->type) {
1277 case WM5102:
Charles Keepax0be068a2015-05-11 13:58:04 +01001278 ret = wm5102_apply_hardware_patch(arizona);
1279 if (ret) {
Charles Keepaxe80436b2013-03-26 18:46:15 +00001280 dev_err(arizona->dev,
1281 "Failed to apply hardware patch: %d\n",
1282 ret);
1283 goto err_reset;
1284 }
1285 break;
Charles Keepax882bc462015-05-11 13:58:06 +01001286 case WM5110:
1287 case WM8280:
1288 ret = wm5110_apply_sleep_patch(arizona);
1289 if (ret) {
1290 dev_err(arizona->dev,
1291 "Failed to apply sleep patch: %d\n",
1292 ret);
1293 goto err_reset;
1294 }
1295 break;
Charles Keepaxe80436b2013-03-26 18:46:15 +00001296 default:
1297 break;
1298 }
Mark Brown62d62b52012-12-02 11:41:46 +09001299 }
1300
Mark Brown3cc72982012-06-19 16:31:53 +01001301 for (i = 0; i < ARRAY_SIZE(arizona->pdata.gpio_defaults); i++) {
1302 if (!arizona->pdata.gpio_defaults[i])
1303 continue;
1304
1305 regmap_write(arizona->regmap, ARIZONA_GPIO1_CTRL + i,
1306 arizona->pdata.gpio_defaults[i]);
1307 }
1308
Mark Brown3cc72982012-06-19 16:31:53 +01001309 /* Chip default */
1310 if (!arizona->pdata.clk32k_src)
1311 arizona->pdata.clk32k_src = ARIZONA_32KZ_MCLK2;
1312
1313 switch (arizona->pdata.clk32k_src) {
1314 case ARIZONA_32KZ_MCLK1:
1315 case ARIZONA_32KZ_MCLK2:
1316 regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
1317 ARIZONA_CLK_32K_SRC_MASK,
1318 arizona->pdata.clk32k_src - 1);
Mark Brown767c6dc2013-03-19 19:04:46 +01001319 arizona_clk32k_enable(arizona);
Mark Brown3cc72982012-06-19 16:31:53 +01001320 break;
1321 case ARIZONA_32KZ_NONE:
1322 regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
1323 ARIZONA_CLK_32K_SRC_MASK, 2);
1324 break;
1325 default:
1326 dev_err(arizona->dev, "Invalid 32kHz clock source: %d\n",
1327 arizona->pdata.clk32k_src);
1328 ret = -EINVAL;
Mark Brown59db9692012-07-09 00:31:36 +02001329 goto err_reset;
Mark Brown3cc72982012-06-19 16:31:53 +01001330 }
1331
Mark Brown3d91f822013-01-29 00:47:37 +08001332 for (i = 0; i < ARIZONA_MAX_MICBIAS; i++) {
Mark Brown544c7aa2013-01-29 18:44:41 +08001333 if (!arizona->pdata.micbias[i].mV &&
1334 !arizona->pdata.micbias[i].bypass)
Mark Brown3d91f822013-01-29 00:47:37 +08001335 continue;
1336
Mark Brown544c7aa2013-01-29 18:44:41 +08001337 /* Apply default for bypass mode */
1338 if (!arizona->pdata.micbias[i].mV)
1339 arizona->pdata.micbias[i].mV = 2800;
1340
Mark Brown3d91f822013-01-29 00:47:37 +08001341 val = (arizona->pdata.micbias[i].mV - 1500) / 100;
Mark Brown544c7aa2013-01-29 18:44:41 +08001342
Mark Brown3d91f822013-01-29 00:47:37 +08001343 val <<= ARIZONA_MICB1_LVL_SHIFT;
1344
1345 if (arizona->pdata.micbias[i].ext_cap)
1346 val |= ARIZONA_MICB1_EXT_CAP;
1347
1348 if (arizona->pdata.micbias[i].discharge)
1349 val |= ARIZONA_MICB1_DISCH;
1350
Charles Keepaxf773fc62013-05-21 14:56:58 +01001351 if (arizona->pdata.micbias[i].soft_start)
Mark Brown3d91f822013-01-29 00:47:37 +08001352 val |= ARIZONA_MICB1_RATE;
1353
Mark Brown544c7aa2013-01-29 18:44:41 +08001354 if (arizona->pdata.micbias[i].bypass)
1355 val |= ARIZONA_MICB1_BYPASS;
1356
Mark Brown3d91f822013-01-29 00:47:37 +08001357 regmap_update_bits(arizona->regmap,
1358 ARIZONA_MIC_BIAS_CTRL_1 + i,
1359 ARIZONA_MICB1_LVL_MASK |
Charles Keepax71d134b2014-09-24 10:37:11 +01001360 ARIZONA_MICB1_EXT_CAP |
Mark Brown3d91f822013-01-29 00:47:37 +08001361 ARIZONA_MICB1_DISCH |
Mark Brown544c7aa2013-01-29 18:44:41 +08001362 ARIZONA_MICB1_BYPASS |
Mark Brown3d91f822013-01-29 00:47:37 +08001363 ARIZONA_MICB1_RATE, val);
1364 }
1365
Charles Keepax72e43162015-06-14 15:41:50 +01001366 pm_runtime_set_active(arizona->dev);
1367 pm_runtime_enable(arizona->dev);
1368
Mark Brown3cc72982012-06-19 16:31:53 +01001369 /* Set up for interrupts */
1370 ret = arizona_irq_init(arizona);
1371 if (ret != 0)
Charles Keepaxd3477922016-06-27 15:19:11 +01001372 goto err_pm;
Mark Brown3cc72982012-06-19 16:31:53 +01001373
Charles Keepax72e43162015-06-14 15:41:50 +01001374 pm_runtime_set_autosuspend_delay(arizona->dev, 100);
1375 pm_runtime_use_autosuspend(arizona->dev);
1376
Mark Brown3cc72982012-06-19 16:31:53 +01001377 arizona_request_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, "CLKGEN error",
1378 arizona_clkgen_err, arizona);
1379 arizona_request_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, "Overclocked",
1380 arizona_overclocked, arizona);
1381 arizona_request_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, "Underclocked",
1382 arizona_underclocked, arizona);
1383
Richard Fitzgeraldae05ea362015-10-02 13:29:13 +01001384 ret = mfd_add_devices(arizona->dev, PLATFORM_DEVID_NONE,
1385 subdevs, n_subdevs, NULL, 0, NULL);
Mark Brown3cc72982012-06-19 16:31:53 +01001386
Richard Fitzgeraldae05ea362015-10-02 13:29:13 +01001387 if (ret) {
Mark Brown3cc72982012-06-19 16:31:53 +01001388 dev_err(arizona->dev, "Failed to add subdevices: %d\n", ret);
1389 goto err_irq;
1390 }
1391
1392 return 0;
1393
1394err_irq:
1395 arizona_irq_exit(arizona);
Charles Keepaxd3477922016-06-27 15:19:11 +01001396err_pm:
1397 pm_runtime_disable(arizona->dev);
Charles Keepaxddff6c42020-06-15 14:53:21 +01001398
1399 switch (arizona->pdata.clk32k_src) {
1400 case ARIZONA_32KZ_MCLK1:
1401 case ARIZONA_32KZ_MCLK2:
1402 arizona_clk32k_disable(arizona);
1403 break;
1404 default:
1405 break;
1406 }
Mark Brown3cc72982012-06-19 16:31:53 +01001407err_reset:
Charles Keepax22298752015-05-11 13:58:02 +01001408 arizona_enable_reset(arizona);
Mark Brown59db9692012-07-09 00:31:36 +02001409 regulator_disable(arizona->dcvdd);
Mark Brown3cc72982012-06-19 16:31:53 +01001410err_enable:
Mark Brown3a36a0db2012-07-09 00:45:53 +02001411 regulator_bulk_disable(arizona->num_core_supplies,
Mark Brown3cc72982012-06-19 16:31:53 +01001412 arizona->core_supplies);
Charles Keepaxe6021512014-06-02 09:50:41 +01001413err_dcvdd:
1414 regulator_put(arizona->dcvdd);
Mark Brown3cc72982012-06-19 16:31:53 +01001415err_early:
1416 mfd_remove_devices(dev);
1417 return ret;
1418}
1419EXPORT_SYMBOL_GPL(arizona_dev_init);
1420
Bill Pemberton4740f732012-11-19 13:26:01 -05001421int arizona_dev_exit(struct arizona *arizona)
Mark Brown3cc72982012-06-19 16:31:53 +01001422{
Charles Keepaxfb36f772016-11-14 17:15:56 +00001423 disable_irq(arizona->irq);
Charles Keepaxb8040202014-06-02 09:50:39 +01001424 pm_runtime_disable(arizona->dev);
1425
Charles Keepaxdf6b3352014-06-02 09:50:40 +01001426 regulator_disable(arizona->dcvdd);
Charles Keepaxe6021512014-06-02 09:50:41 +01001427 regulator_put(arizona->dcvdd);
Charles Keepaxdf6b3352014-06-02 09:50:40 +01001428
Charles Keepaxddff6c42020-06-15 14:53:21 +01001429 switch (arizona->pdata.clk32k_src) {
1430 case ARIZONA_32KZ_MCLK1:
1431 case ARIZONA_32KZ_MCLK2:
1432 arizona_clk32k_disable(arizona);
1433 break;
1434 default:
1435 break;
1436 }
1437
Mark Brown3cc72982012-06-19 16:31:53 +01001438 mfd_remove_devices(arizona->dev);
1439 arizona_free_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, arizona);
1440 arizona_free_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, arizona);
1441 arizona_free_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, arizona);
Mark Brown3cc72982012-06-19 16:31:53 +01001442 arizona_irq_exit(arizona);
Charles Keepax22298752015-05-11 13:58:02 +01001443 arizona_enable_reset(arizona);
Charles Keepaxdf6b3352014-06-02 09:50:40 +01001444
Charles Keepax44202862014-06-02 09:50:42 +01001445 regulator_bulk_disable(arizona->num_core_supplies,
Mark Brown1d017b62013-03-26 12:16:26 +00001446 arizona->core_supplies);
Mark Brown3cc72982012-06-19 16:31:53 +01001447 return 0;
1448}
1449EXPORT_SYMBOL_GPL(arizona_dev_exit);
Hans de Goede33d55072021-05-21 15:50:23 +02001450
1451MODULE_LICENSE("GPL v2");