1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright 2012 Wolfson Microelectronics plc
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/err.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/interrupt.h>
15 #include <linux/mfd/core.h>
16 #include <linux/module.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regmap.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/slab.h>
23 #include <linux/ktime.h>
24 #include <linux/platform_device.h>
26 #include <linux/mfd/arizona/core.h>
27 #include <linux/mfd/arizona/registers.h>
31 static const char * const wm5102_core_supplies
[] = {
36 int arizona_clk32k_enable(struct arizona
*arizona
)
40 mutex_lock(&arizona
->clk_lock
);
42 arizona
->clk32k_ref
++;
44 if (arizona
->clk32k_ref
== 1) {
45 switch (arizona
->pdata
.clk32k_src
) {
46 case ARIZONA_32KZ_MCLK1
:
47 ret
= pm_runtime_resume_and_get(arizona
->dev
);
50 ret
= clk_prepare_enable(arizona
->mclk
[ARIZONA_MCLK1
]);
52 pm_runtime_put_sync(arizona
->dev
);
56 case ARIZONA_32KZ_MCLK2
:
57 ret
= clk_prepare_enable(arizona
->mclk
[ARIZONA_MCLK2
]);
63 ret
= regmap_update_bits(arizona
->regmap
, ARIZONA_CLOCK_32K_1
,
70 arizona
->clk32k_ref
--;
72 mutex_unlock(&arizona
->clk_lock
);
76 EXPORT_SYMBOL_GPL(arizona_clk32k_enable
);
78 int arizona_clk32k_disable(struct arizona
*arizona
)
80 mutex_lock(&arizona
->clk_lock
);
82 WARN_ON(arizona
->clk32k_ref
<= 0);
84 arizona
->clk32k_ref
--;
86 if (arizona
->clk32k_ref
== 0) {
87 regmap_update_bits(arizona
->regmap
, ARIZONA_CLOCK_32K_1
,
88 ARIZONA_CLK_32K_ENA
, 0);
90 switch (arizona
->pdata
.clk32k_src
) {
91 case ARIZONA_32KZ_MCLK1
:
92 pm_runtime_put_sync(arizona
->dev
);
93 clk_disable_unprepare(arizona
->mclk
[ARIZONA_MCLK1
]);
95 case ARIZONA_32KZ_MCLK2
:
96 clk_disable_unprepare(arizona
->mclk
[ARIZONA_MCLK2
]);
101 mutex_unlock(&arizona
->clk_lock
);
105 EXPORT_SYMBOL_GPL(arizona_clk32k_disable
);
107 static irqreturn_t
arizona_clkgen_err(int irq
, void *data
)
109 struct arizona
*arizona
= data
;
111 dev_err(arizona
->dev
, "CLKGEN error\n");
116 static irqreturn_t
arizona_underclocked(int irq
, void *data
)
118 struct arizona
*arizona
= data
;
122 ret
= regmap_read(arizona
->regmap
, ARIZONA_INTERRUPT_RAW_STATUS_8
,
125 dev_err(arizona
->dev
, "Failed to read underclock status: %d\n",
130 if (val
& ARIZONA_AIF3_UNDERCLOCKED_STS
)
131 dev_err(arizona
->dev
, "AIF3 underclocked\n");
132 if (val
& ARIZONA_AIF2_UNDERCLOCKED_STS
)
133 dev_err(arizona
->dev
, "AIF2 underclocked\n");
134 if (val
& ARIZONA_AIF1_UNDERCLOCKED_STS
)
135 dev_err(arizona
->dev
, "AIF1 underclocked\n");
136 if (val
& ARIZONA_ISRC3_UNDERCLOCKED_STS
)
137 dev_err(arizona
->dev
, "ISRC3 underclocked\n");
138 if (val
& ARIZONA_ISRC2_UNDERCLOCKED_STS
)
139 dev_err(arizona
->dev
, "ISRC2 underclocked\n");
140 if (val
& ARIZONA_ISRC1_UNDERCLOCKED_STS
)
141 dev_err(arizona
->dev
, "ISRC1 underclocked\n");
142 if (val
& ARIZONA_FX_UNDERCLOCKED_STS
)
143 dev_err(arizona
->dev
, "FX underclocked\n");
144 if (val
& ARIZONA_ASRC_UNDERCLOCKED_STS
)
145 dev_err(arizona
->dev
, "ASRC underclocked\n");
146 if (val
& ARIZONA_DAC_UNDERCLOCKED_STS
)
147 dev_err(arizona
->dev
, "DAC underclocked\n");
148 if (val
& ARIZONA_ADC_UNDERCLOCKED_STS
)
149 dev_err(arizona
->dev
, "ADC underclocked\n");
150 if (val
& ARIZONA_MIXER_UNDERCLOCKED_STS
)
151 dev_err(arizona
->dev
, "Mixer dropped sample\n");
156 static irqreturn_t
arizona_overclocked(int irq
, void *data
)
158 struct arizona
*arizona
= data
;
162 ret
= regmap_bulk_read(arizona
->regmap
, ARIZONA_INTERRUPT_RAW_STATUS_6
,
165 dev_err(arizona
->dev
, "Failed to read overclock status: %d\n",
170 switch (arizona
->type
) {
173 /* Some bits are shifted on WM8998,
174 * rearrange to match the standard bit layout
176 val
[0] = ((val
[0] & 0x60e0) >> 1) |
177 ((val
[0] & 0x1e00) >> 2) |
184 if (val
[0] & ARIZONA_PWM_OVERCLOCKED_STS
)
185 dev_err(arizona
->dev
, "PWM overclocked\n");
186 if (val
[0] & ARIZONA_FX_CORE_OVERCLOCKED_STS
)
187 dev_err(arizona
->dev
, "FX core overclocked\n");
188 if (val
[0] & ARIZONA_DAC_SYS_OVERCLOCKED_STS
)
189 dev_err(arizona
->dev
, "DAC SYS overclocked\n");
190 if (val
[0] & ARIZONA_DAC_WARP_OVERCLOCKED_STS
)
191 dev_err(arizona
->dev
, "DAC WARP overclocked\n");
192 if (val
[0] & ARIZONA_ADC_OVERCLOCKED_STS
)
193 dev_err(arizona
->dev
, "ADC overclocked\n");
194 if (val
[0] & ARIZONA_MIXER_OVERCLOCKED_STS
)
195 dev_err(arizona
->dev
, "Mixer overclocked\n");
196 if (val
[0] & ARIZONA_AIF3_SYNC_OVERCLOCKED_STS
)
197 dev_err(arizona
->dev
, "AIF3 overclocked\n");
198 if (val
[0] & ARIZONA_AIF2_SYNC_OVERCLOCKED_STS
)
199 dev_err(arizona
->dev
, "AIF2 overclocked\n");
200 if (val
[0] & ARIZONA_AIF1_SYNC_OVERCLOCKED_STS
)
201 dev_err(arizona
->dev
, "AIF1 overclocked\n");
202 if (val
[0] & ARIZONA_PAD_CTRL_OVERCLOCKED_STS
)
203 dev_err(arizona
->dev
, "Pad control overclocked\n");
205 if (val
[1] & ARIZONA_SLIMBUS_SUBSYS_OVERCLOCKED_STS
)
206 dev_err(arizona
->dev
, "Slimbus subsystem overclocked\n");
207 if (val
[1] & ARIZONA_SLIMBUS_ASYNC_OVERCLOCKED_STS
)
208 dev_err(arizona
->dev
, "Slimbus async overclocked\n");
209 if (val
[1] & ARIZONA_SLIMBUS_SYNC_OVERCLOCKED_STS
)
210 dev_err(arizona
->dev
, "Slimbus sync overclocked\n");
211 if (val
[1] & ARIZONA_ASRC_ASYNC_SYS_OVERCLOCKED_STS
)
212 dev_err(arizona
->dev
, "ASRC async system overclocked\n");
213 if (val
[1] & ARIZONA_ASRC_ASYNC_WARP_OVERCLOCKED_STS
)
214 dev_err(arizona
->dev
, "ASRC async WARP overclocked\n");
215 if (val
[1] & ARIZONA_ASRC_SYNC_SYS_OVERCLOCKED_STS
)
216 dev_err(arizona
->dev
, "ASRC sync system overclocked\n");
217 if (val
[1] & ARIZONA_ASRC_SYNC_WARP_OVERCLOCKED_STS
)
218 dev_err(arizona
->dev
, "ASRC sync WARP overclocked\n");
219 if (val
[1] & ARIZONA_ADSP2_1_OVERCLOCKED_STS
)
220 dev_err(arizona
->dev
, "DSP1 overclocked\n");
221 if (val
[1] & ARIZONA_ISRC3_OVERCLOCKED_STS
)
222 dev_err(arizona
->dev
, "ISRC3 overclocked\n");
223 if (val
[1] & ARIZONA_ISRC2_OVERCLOCKED_STS
)
224 dev_err(arizona
->dev
, "ISRC2 overclocked\n");
225 if (val
[1] & ARIZONA_ISRC1_OVERCLOCKED_STS
)
226 dev_err(arizona
->dev
, "ISRC1 overclocked\n");
228 if (val
[2] & ARIZONA_SPDIF_OVERCLOCKED_STS
)
229 dev_err(arizona
->dev
, "SPDIF overclocked\n");
234 #define ARIZONA_REG_POLL_DELAY_US 7500
236 static inline bool arizona_poll_reg_delay(ktime_t timeout
)
238 if (ktime_compare(ktime_get(), timeout
) > 0)
241 usleep_range(ARIZONA_REG_POLL_DELAY_US
/ 2, ARIZONA_REG_POLL_DELAY_US
);
246 static int arizona_poll_reg(struct arizona
*arizona
,
247 int timeout_ms
, unsigned int reg
,
248 unsigned int mask
, unsigned int target
)
250 ktime_t timeout
= ktime_add_us(ktime_get(), timeout_ms
* USEC_PER_MSEC
);
251 unsigned int val
= 0;
255 ret
= regmap_read(arizona
->regmap
, reg
, &val
);
257 if ((val
& mask
) == target
)
259 } while (arizona_poll_reg_delay(timeout
));
262 dev_err(arizona
->dev
, "Failed polling reg 0x%x: %d\n",
267 dev_err(arizona
->dev
, "Polling reg 0x%x timed out: %x\n", reg
, val
);
271 static int arizona_wait_for_boot(struct arizona
*arizona
)
276 * We can't use an interrupt as we need to runtime resume to do so,
277 * we won't race with the interrupt handler as it'll be blocked on
280 ret
= arizona_poll_reg(arizona
, 30, ARIZONA_INTERRUPT_RAW_STATUS_5
,
281 ARIZONA_BOOT_DONE_STS
, ARIZONA_BOOT_DONE_STS
);
284 regmap_write(arizona
->regmap
, ARIZONA_INTERRUPT_STATUS_5
,
285 ARIZONA_BOOT_DONE_STS
);
287 pm_runtime_mark_last_busy(arizona
->dev
);
292 static inline void arizona_enable_reset(struct arizona
*arizona
)
294 if (arizona
->pdata
.reset
)
295 gpiod_set_raw_value_cansleep(arizona
->pdata
.reset
, 0);
298 static void arizona_disable_reset(struct arizona
*arizona
)
300 if (arizona
->pdata
.reset
) {
301 switch (arizona
->type
) {
304 /* Meet requirements for minimum reset duration */
305 usleep_range(5000, 10000);
311 gpiod_set_raw_value_cansleep(arizona
->pdata
.reset
, 1);
312 usleep_range(1000, 5000);
316 struct arizona_sysclk_state
{
321 static int arizona_enable_freerun_sysclk(struct arizona
*arizona
,
322 struct arizona_sysclk_state
*state
)
326 /* Cache existing FLL and SYSCLK settings */
327 ret
= regmap_read(arizona
->regmap
, ARIZONA_FLL1_CONTROL_1
, &state
->fll
);
329 dev_err(arizona
->dev
, "Failed to cache FLL settings: %d\n",
333 ret
= regmap_read(arizona
->regmap
, ARIZONA_SYSTEM_CLOCK_1
,
336 dev_err(arizona
->dev
, "Failed to cache SYSCLK settings: %d\n",
341 /* Start up SYSCLK using the FLL in free running mode */
342 ret
= regmap_write(arizona
->regmap
, ARIZONA_FLL1_CONTROL_1
,
343 ARIZONA_FLL1_ENA
| ARIZONA_FLL1_FREERUN
);
345 dev_err(arizona
->dev
,
346 "Failed to start FLL in freerunning mode: %d\n",
350 ret
= arizona_poll_reg(arizona
, 180, ARIZONA_INTERRUPT_RAW_STATUS_5
,
351 ARIZONA_FLL1_CLOCK_OK_STS
,
352 ARIZONA_FLL1_CLOCK_OK_STS
);
356 ret
= regmap_write(arizona
->regmap
, ARIZONA_SYSTEM_CLOCK_1
, 0x0144);
358 dev_err(arizona
->dev
, "Failed to start SYSCLK: %d\n", ret
);
365 err
= regmap_write(arizona
->regmap
, ARIZONA_FLL1_CONTROL_1
, state
->fll
);
367 dev_err(arizona
->dev
,
368 "Failed to re-apply old FLL settings: %d\n", err
);
373 static int arizona_disable_freerun_sysclk(struct arizona
*arizona
,
374 struct arizona_sysclk_state
*state
)
378 ret
= regmap_write(arizona
->regmap
, ARIZONA_SYSTEM_CLOCK_1
,
381 dev_err(arizona
->dev
,
382 "Failed to re-apply old SYSCLK settings: %d\n", ret
);
386 ret
= regmap_write(arizona
->regmap
, ARIZONA_FLL1_CONTROL_1
, state
->fll
);
388 dev_err(arizona
->dev
,
389 "Failed to re-apply old FLL settings: %d\n", ret
);
396 static int wm5102_apply_hardware_patch(struct arizona
*arizona
)
398 struct arizona_sysclk_state state
;
401 ret
= arizona_enable_freerun_sysclk(arizona
, &state
);
405 /* Start the write sequencer and wait for it to finish */
406 ret
= regmap_write(arizona
->regmap
, ARIZONA_WRITE_SEQUENCER_CTRL_0
,
407 ARIZONA_WSEQ_ENA
| ARIZONA_WSEQ_START
| 160);
409 dev_err(arizona
->dev
, "Failed to start write sequencer: %d\n",
414 ret
= arizona_poll_reg(arizona
, 30, ARIZONA_WRITE_SEQUENCER_CTRL_1
,
415 ARIZONA_WSEQ_BUSY
, 0);
417 regmap_write(arizona
->regmap
, ARIZONA_WRITE_SEQUENCER_CTRL_0
,
421 err
= arizona_disable_freerun_sysclk(arizona
, &state
);
427 * Register patch to some of the CODECs internal write sequences
428 * to ensure a clean exit from the low power sleep state.
430 static const struct reg_sequence wm5110_sleep_patch
[] = {
437 static int wm5110_apply_sleep_patch(struct arizona
*arizona
)
439 struct arizona_sysclk_state state
;
442 ret
= arizona_enable_freerun_sysclk(arizona
, &state
);
446 ret
= regmap_multi_reg_write_bypassed(arizona
->regmap
,
448 ARRAY_SIZE(wm5110_sleep_patch
));
450 err
= arizona_disable_freerun_sysclk(arizona
, &state
);
455 static int wm5102_clear_write_sequencer(struct arizona
*arizona
)
459 ret
= regmap_write(arizona
->regmap
, ARIZONA_WRITE_SEQUENCER_CTRL_3
,
462 dev_err(arizona
->dev
,
463 "Failed to clear write sequencer state: %d\n", ret
);
467 arizona_enable_reset(arizona
);
468 regulator_disable(arizona
->dcvdd
);
472 ret
= regulator_enable(arizona
->dcvdd
);
474 dev_err(arizona
->dev
, "Failed to re-enable DCVDD: %d\n", ret
);
477 arizona_disable_reset(arizona
);
482 static int arizona_isolate_dcvdd(struct arizona
*arizona
)
486 ret
= regmap_update_bits(arizona
->regmap
,
487 ARIZONA_ISOLATION_CONTROL
,
488 ARIZONA_ISOLATE_DCVDD1
,
489 ARIZONA_ISOLATE_DCVDD1
);
491 dev_err(arizona
->dev
, "Failed to isolate DCVDD: %d\n", ret
);
496 static int arizona_connect_dcvdd(struct arizona
*arizona
)
500 ret
= regmap_update_bits(arizona
->regmap
,
501 ARIZONA_ISOLATION_CONTROL
,
502 ARIZONA_ISOLATE_DCVDD1
, 0);
504 dev_err(arizona
->dev
, "Failed to connect DCVDD: %d\n", ret
);
509 static int arizona_is_jack_det_active(struct arizona
*arizona
)
514 ret
= regmap_read(arizona
->regmap
, ARIZONA_JACK_DETECT_ANALOGUE
, &val
);
516 dev_err(arizona
->dev
,
517 "Failed to check jack det status: %d\n", ret
);
519 } else if (val
& ARIZONA_JD1_ENA
) {
526 static int arizona_runtime_resume(struct device
*dev
)
528 struct arizona
*arizona
= dev_get_drvdata(dev
);
531 dev_dbg(arizona
->dev
, "Leaving AoD mode\n");
533 if (arizona
->has_fully_powered_off
) {
534 dev_dbg(arizona
->dev
, "Re-enabling core supplies\n");
536 ret
= regulator_bulk_enable(arizona
->num_core_supplies
,
537 arizona
->core_supplies
);
539 dev_err(dev
, "Failed to enable core supplies: %d\n",
545 ret
= regulator_enable(arizona
->dcvdd
);
547 dev_err(arizona
->dev
, "Failed to enable DCVDD: %d\n", ret
);
548 if (arizona
->has_fully_powered_off
)
549 regulator_bulk_disable(arizona
->num_core_supplies
,
550 arizona
->core_supplies
);
554 if (arizona
->has_fully_powered_off
) {
555 arizona_disable_reset(arizona
);
556 enable_irq(arizona
->irq
);
557 arizona
->has_fully_powered_off
= false;
560 regcache_cache_only(arizona
->regmap
, false);
562 switch (arizona
->type
) {
564 if (arizona
->external_dcvdd
) {
565 ret
= arizona_connect_dcvdd(arizona
);
570 ret
= wm5102_patch(arizona
);
572 dev_err(arizona
->dev
, "Failed to apply patch: %d\n",
577 ret
= wm5102_apply_hardware_patch(arizona
);
579 dev_err(arizona
->dev
,
580 "Failed to apply hardware patch: %d\n",
587 ret
= arizona_wait_for_boot(arizona
);
591 if (arizona
->external_dcvdd
) {
592 ret
= arizona_connect_dcvdd(arizona
);
597 * As this is only called for the internal regulator
598 * (where we know voltage ranges available) it is ok
599 * to request an exact range.
601 ret
= regulator_set_voltage(arizona
->dcvdd
,
604 dev_err(arizona
->dev
,
605 "Failed to set resume voltage: %d\n",
611 ret
= wm5110_apply_sleep_patch(arizona
);
613 dev_err(arizona
->dev
,
614 "Failed to re-apply sleep patch: %d\n",
621 ret
= arizona_wait_for_boot(arizona
);
626 ret
= arizona_wait_for_boot(arizona
);
630 if (arizona
->external_dcvdd
) {
631 ret
= arizona_connect_dcvdd(arizona
);
638 ret
= regcache_sync(arizona
->regmap
);
640 dev_err(arizona
->dev
, "Failed to restore register cache\n");
647 regcache_cache_only(arizona
->regmap
, true);
648 regulator_disable(arizona
->dcvdd
);
652 static int arizona_runtime_suspend(struct device
*dev
)
654 struct arizona
*arizona
= dev_get_drvdata(dev
);
658 dev_dbg(arizona
->dev
, "Entering AoD mode\n");
660 switch (arizona
->type
) {
663 jd_active
= arizona_is_jack_det_active(arizona
);
667 if (arizona
->external_dcvdd
) {
668 ret
= arizona_isolate_dcvdd(arizona
);
673 * As this is only called for the internal regulator
674 * (where we know voltage ranges available) it is ok
675 * to request an exact range.
677 ret
= regulator_set_voltage(arizona
->dcvdd
,
680 dev_err(arizona
->dev
,
681 "Failed to set suspend voltage: %d\n",
688 jd_active
= arizona_is_jack_det_active(arizona
);
692 if (arizona
->external_dcvdd
) {
693 ret
= arizona_isolate_dcvdd(arizona
);
699 ret
= regmap_write(arizona
->regmap
,
700 ARIZONA_WRITE_SEQUENCER_CTRL_3
, 0x0);
702 dev_err(arizona
->dev
,
703 "Failed to clear write sequencer: %d\n",
713 jd_active
= arizona_is_jack_det_active(arizona
);
717 if (arizona
->external_dcvdd
) {
718 ret
= arizona_isolate_dcvdd(arizona
);
725 regcache_cache_only(arizona
->regmap
, true);
726 regcache_mark_dirty(arizona
->regmap
);
727 regulator_disable(arizona
->dcvdd
);
729 /* Allow us to completely power down if no jack detection */
731 dev_dbg(arizona
->dev
, "Fully powering off\n");
733 arizona
->has_fully_powered_off
= true;
735 disable_irq_nosync(arizona
->irq
);
736 arizona_enable_reset(arizona
);
737 regulator_bulk_disable(arizona
->num_core_supplies
,
738 arizona
->core_supplies
);
744 static int arizona_suspend(struct device
*dev
)
746 struct arizona
*arizona
= dev_get_drvdata(dev
);
748 dev_dbg(arizona
->dev
, "Suspend, disabling IRQ\n");
749 disable_irq(arizona
->irq
);
754 static int arizona_suspend_noirq(struct device
*dev
)
756 struct arizona
*arizona
= dev_get_drvdata(dev
);
758 dev_dbg(arizona
->dev
, "Late suspend, reenabling IRQ\n");
759 enable_irq(arizona
->irq
);
764 static int arizona_resume_noirq(struct device
*dev
)
766 struct arizona
*arizona
= dev_get_drvdata(dev
);
768 dev_dbg(arizona
->dev
, "Early resume, disabling IRQ\n");
769 disable_irq(arizona
->irq
);
774 static int arizona_resume(struct device
*dev
)
776 struct arizona
*arizona
= dev_get_drvdata(dev
);
778 dev_dbg(arizona
->dev
, "Resume, reenabling IRQ\n");
779 enable_irq(arizona
->irq
);
784 EXPORT_GPL_DEV_PM_OPS(arizona_pm_ops
) = {
785 RUNTIME_PM_OPS(arizona_runtime_suspend
,
786 arizona_runtime_resume
,
788 SYSTEM_SLEEP_PM_OPS(arizona_suspend
, arizona_resume
)
789 NOIRQ_SYSTEM_SLEEP_PM_OPS(arizona_suspend_noirq
,
790 arizona_resume_noirq
)
794 static int arizona_of_get_core_pdata(struct arizona
*arizona
)
796 struct arizona_pdata
*pdata
= &arizona
->pdata
;
799 /* Handle old non-standard DT binding */
800 pdata
->reset
= devm_gpiod_get(arizona
->dev
, "wlf,reset", GPIOD_OUT_LOW
);
801 if (IS_ERR(pdata
->reset
)) {
802 ret
= PTR_ERR(pdata
->reset
);
805 * Reset missing will be caught when other binding is read
806 * but all other errors imply this binding is in use but has
807 * encountered a problem so should be handled.
809 if (ret
== -EPROBE_DEFER
)
811 else if (ret
!= -ENOENT
&& ret
!= -ENOSYS
)
812 dev_err(arizona
->dev
, "Reset GPIO malformed: %d\n",
818 ret
= of_property_read_u32_array(arizona
->dev
->of_node
,
820 pdata
->gpio_defaults
,
821 ARRAY_SIZE(pdata
->gpio_defaults
));
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
829 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;
836 dev_err(arizona
->dev
, "Failed to parse GPIO defaults: %d\n",
843 static inline int arizona_of_get_core_pdata(struct arizona
*arizona
)
849 static const struct mfd_cell early_devs
[] = {
850 { .name
= "arizona-ldo1" },
853 static const char * const wm5102_supplies
[] = {
862 static const struct mfd_cell wm5102_devs
[] = {
863 { .name
= "arizona-micsupp" },
864 { .name
= "arizona-gpio" },
865 { .name
= "arizona-haptics" },
866 { .name
= "arizona-pwm" },
868 .name
= "wm5102-codec",
869 .parent_supplies
= wm5102_supplies
,
870 .num_parent_supplies
= ARRAY_SIZE(wm5102_supplies
),
874 static const struct mfd_cell wm5110_devs
[] = {
875 { .name
= "arizona-micsupp" },
876 { .name
= "arizona-gpio" },
877 { .name
= "arizona-haptics" },
878 { .name
= "arizona-pwm" },
880 .name
= "wm5110-codec",
881 .parent_supplies
= wm5102_supplies
,
882 .num_parent_supplies
= ARRAY_SIZE(wm5102_supplies
),
886 static const char * const cs47l24_supplies
[] = {
892 static const struct mfd_cell cs47l24_devs
[] = {
893 { .name
= "arizona-gpio" },
894 { .name
= "arizona-haptics" },
895 { .name
= "arizona-pwm" },
897 .name
= "cs47l24-codec",
898 .parent_supplies
= cs47l24_supplies
,
899 .num_parent_supplies
= ARRAY_SIZE(cs47l24_supplies
),
903 static const char * const wm8997_supplies
[] = {
910 static const struct mfd_cell wm8997_devs
[] = {
911 { .name
= "arizona-micsupp" },
912 { .name
= "arizona-gpio" },
913 { .name
= "arizona-haptics" },
914 { .name
= "arizona-pwm" },
916 .name
= "wm8997-codec",
917 .parent_supplies
= wm8997_supplies
,
918 .num_parent_supplies
= ARRAY_SIZE(wm8997_supplies
),
922 static const struct mfd_cell wm8998_devs
[] = {
923 { .name
= "arizona-micsupp" },
924 { .name
= "arizona-gpio" },
925 { .name
= "arizona-haptics" },
926 { .name
= "arizona-pwm" },
928 .name
= "wm8998-codec",
929 .parent_supplies
= wm5102_supplies
,
930 .num_parent_supplies
= ARRAY_SIZE(wm5102_supplies
),
934 int arizona_dev_init(struct arizona
*arizona
)
936 static const char * const mclk_name
[] = { "mclk1", "mclk2" };
937 struct device
*dev
= arizona
->dev
;
938 const char *type_name
= NULL
;
939 unsigned int reg
, val
;
940 int (*apply_patch
)(struct arizona
*) = NULL
;
941 const struct mfd_cell
*subdevs
= NULL
;
942 int n_subdevs
= 0, ret
, i
;
944 dev_set_drvdata(arizona
->dev
, arizona
);
945 mutex_init(&arizona
->clk_lock
);
947 if (dev_get_platdata(arizona
->dev
)) {
948 memcpy(&arizona
->pdata
, dev_get_platdata(arizona
->dev
),
949 sizeof(arizona
->pdata
));
951 ret
= arizona_of_get_core_pdata(arizona
);
956 BUILD_BUG_ON(ARRAY_SIZE(arizona
->mclk
) != ARRAY_SIZE(mclk_name
));
957 for (i
= 0; i
< ARRAY_SIZE(arizona
->mclk
); i
++) {
958 arizona
->mclk
[i
] = devm_clk_get(arizona
->dev
, mclk_name
[i
]);
959 if (IS_ERR(arizona
->mclk
[i
])) {
960 dev_info(arizona
->dev
, "Failed to get %s: %ld\n",
961 mclk_name
[i
], PTR_ERR(arizona
->mclk
[i
]));
962 arizona
->mclk
[i
] = NULL
;
966 regcache_cache_only(arizona
->regmap
, true);
968 switch (arizona
->type
) {
977 for (i
= 0; i
< ARRAY_SIZE(wm5102_core_supplies
); i
++)
978 arizona
->core_supplies
[i
].supply
979 = wm5102_core_supplies
[i
];
980 arizona
->num_core_supplies
= ARRAY_SIZE(wm5102_core_supplies
);
983 dev_err(arizona
->dev
, "Unknown device type %d\n",
988 /* Mark DCVDD as external, LDO1 driver will clear if internal */
989 arizona
->external_dcvdd
= true;
991 switch (arizona
->type
) {
994 break; /* No LDO1 regulator */
996 ret
= mfd_add_devices(arizona
->dev
, -1, early_devs
,
997 ARRAY_SIZE(early_devs
), NULL
, 0, NULL
);
999 dev_err(dev
, "Failed to add early children: %d\n", ret
);
1005 ret
= devm_regulator_bulk_get(dev
, arizona
->num_core_supplies
,
1006 arizona
->core_supplies
);
1008 dev_err(dev
, "Failed to request core supplies: %d\n",
1014 * Don't use devres here because the only device we have to get
1015 * against is the MFD device and DCVDD will likely be supplied by
1016 * one of its children. Meaning that the regulator will be
1017 * destroyed by the time devres calls regulator put.
1019 arizona
->dcvdd
= regulator_get(arizona
->dev
, "DCVDD");
1020 if (IS_ERR(arizona
->dcvdd
)) {
1021 ret
= PTR_ERR(arizona
->dcvdd
);
1022 dev_err(dev
, "Failed to request DCVDD: %d\n", ret
);
1026 if (!arizona
->pdata
.reset
) {
1027 /* Start out with /RESET low to put the chip into reset */
1028 arizona
->pdata
.reset
= devm_gpiod_get(arizona
->dev
, "reset",
1030 if (IS_ERR(arizona
->pdata
.reset
)) {
1031 ret
= PTR_ERR(arizona
->pdata
.reset
);
1032 if (ret
== -EPROBE_DEFER
)
1035 dev_err(arizona
->dev
,
1036 "Reset GPIO missing/malformed: %d\n", ret
);
1038 arizona
->pdata
.reset
= NULL
;
1042 ret
= regulator_bulk_enable(arizona
->num_core_supplies
,
1043 arizona
->core_supplies
);
1045 dev_err(dev
, "Failed to enable core supplies: %d\n",
1050 ret
= regulator_enable(arizona
->dcvdd
);
1052 dev_err(dev
, "Failed to enable DCVDD: %d\n", ret
);
1056 arizona_disable_reset(arizona
);
1058 regcache_cache_only(arizona
->regmap
, false);
1060 /* Verify that this is a chip we know about */
1061 ret
= regmap_read(arizona
->regmap
, ARIZONA_SOFTWARE_RESET
, ®
);
1063 dev_err(dev
, "Failed to read ID register: %d\n", ret
);
1075 dev_err(arizona
->dev
, "Unknown device ID: %x\n", reg
);
1080 /* If we have a /RESET GPIO we'll already be reset */
1081 if (!arizona
->pdata
.reset
) {
1082 ret
= regmap_write(arizona
->regmap
, ARIZONA_SOFTWARE_RESET
, 0);
1084 dev_err(dev
, "Failed to reset device: %d\n", ret
);
1088 usleep_range(1000, 5000);
1091 /* Ensure device startup is complete */
1092 switch (arizona
->type
) {
1094 ret
= regmap_read(arizona
->regmap
,
1095 ARIZONA_WRITE_SEQUENCER_CTRL_3
, &val
);
1098 "Failed to check write sequencer state: %d\n",
1100 } else if (val
& 0x01) {
1101 ret
= wm5102_clear_write_sequencer(arizona
);
1110 ret
= arizona_wait_for_boot(arizona
);
1112 dev_err(arizona
->dev
, "Device failed initial boot: %d\n", ret
);
1116 /* Read the device ID information & do device specific stuff */
1117 ret
= regmap_read(arizona
->regmap
, ARIZONA_SOFTWARE_RESET
, ®
);
1119 dev_err(dev
, "Failed to read ID register: %d\n", ret
);
1123 ret
= regmap_read(arizona
->regmap
, ARIZONA_DEVICE_REVISION
,
1126 dev_err(dev
, "Failed to read revision register: %d\n", ret
);
1129 arizona
->rev
&= ARIZONA_DEVICE_REVISION_MASK
;
1133 if (IS_ENABLED(CONFIG_MFD_WM5102
)) {
1134 type_name
= "WM5102";
1135 if (arizona
->type
!= WM5102
) {
1136 dev_warn(arizona
->dev
,
1137 "WM5102 registered as %d\n",
1139 arizona
->type
= WM5102
;
1142 apply_patch
= wm5102_patch
;
1143 arizona
->rev
&= 0x7;
1144 subdevs
= wm5102_devs
;
1145 n_subdevs
= ARRAY_SIZE(wm5102_devs
);
1149 if (IS_ENABLED(CONFIG_MFD_WM5110
)) {
1150 switch (arizona
->type
) {
1152 type_name
= "WM5110";
1155 type_name
= "WM8280";
1158 type_name
= "WM5110";
1159 dev_warn(arizona
->dev
,
1160 "WM5110 registered as %d\n",
1162 arizona
->type
= WM5110
;
1166 apply_patch
= wm5110_patch
;
1167 subdevs
= wm5110_devs
;
1168 n_subdevs
= ARRAY_SIZE(wm5110_devs
);
1172 if (IS_ENABLED(CONFIG_MFD_CS47L24
)) {
1173 switch (arizona
->type
) {
1175 type_name
= "CS47L24";
1179 type_name
= "WM1831";
1183 dev_warn(arizona
->dev
,
1184 "CS47L24 registered as %d\n",
1186 arizona
->type
= CS47L24
;
1190 apply_patch
= cs47l24_patch
;
1191 subdevs
= cs47l24_devs
;
1192 n_subdevs
= ARRAY_SIZE(cs47l24_devs
);
1196 if (IS_ENABLED(CONFIG_MFD_WM8997
)) {
1197 type_name
= "WM8997";
1198 if (arizona
->type
!= WM8997
) {
1199 dev_warn(arizona
->dev
,
1200 "WM8997 registered as %d\n",
1202 arizona
->type
= WM8997
;
1205 apply_patch
= wm8997_patch
;
1206 subdevs
= wm8997_devs
;
1207 n_subdevs
= ARRAY_SIZE(wm8997_devs
);
1211 if (IS_ENABLED(CONFIG_MFD_WM8998
)) {
1212 switch (arizona
->type
) {
1214 type_name
= "WM8998";
1218 type_name
= "WM1814";
1222 type_name
= "WM8998";
1223 dev_warn(arizona
->dev
,
1224 "WM8998 registered as %d\n",
1226 arizona
->type
= WM8998
;
1229 apply_patch
= wm8998_patch
;
1230 subdevs
= wm8998_devs
;
1231 n_subdevs
= ARRAY_SIZE(wm8998_devs
);
1235 dev_err(arizona
->dev
, "Unknown device ID %x\n", reg
);
1241 dev_err(arizona
->dev
,
1242 "No kernel support for device ID %x\n", reg
);
1247 dev_info(dev
, "%s revision %c\n", type_name
, arizona
->rev
+ 'A');
1250 ret
= apply_patch(arizona
);
1252 dev_err(arizona
->dev
, "Failed to apply patch: %d\n",
1257 switch (arizona
->type
) {
1259 ret
= wm5102_apply_hardware_patch(arizona
);
1261 dev_err(arizona
->dev
,
1262 "Failed to apply hardware patch: %d\n",
1269 ret
= wm5110_apply_sleep_patch(arizona
);
1271 dev_err(arizona
->dev
,
1272 "Failed to apply sleep patch: %d\n",
1282 for (i
= 0; i
< ARRAY_SIZE(arizona
->pdata
.gpio_defaults
); i
++) {
1283 if (!arizona
->pdata
.gpio_defaults
[i
])
1286 regmap_write(arizona
->regmap
, ARIZONA_GPIO1_CTRL
+ i
,
1287 arizona
->pdata
.gpio_defaults
[i
]);
1291 if (!arizona
->pdata
.clk32k_src
)
1292 arizona
->pdata
.clk32k_src
= ARIZONA_32KZ_MCLK2
;
1294 switch (arizona
->pdata
.clk32k_src
) {
1295 case ARIZONA_32KZ_MCLK1
:
1296 case ARIZONA_32KZ_MCLK2
:
1297 regmap_update_bits(arizona
->regmap
, ARIZONA_CLOCK_32K_1
,
1298 ARIZONA_CLK_32K_SRC_MASK
,
1299 arizona
->pdata
.clk32k_src
- 1);
1300 arizona_clk32k_enable(arizona
);
1302 case ARIZONA_32KZ_NONE
:
1303 regmap_update_bits(arizona
->regmap
, ARIZONA_CLOCK_32K_1
,
1304 ARIZONA_CLK_32K_SRC_MASK
, 2);
1307 dev_err(arizona
->dev
, "Invalid 32kHz clock source: %d\n",
1308 arizona
->pdata
.clk32k_src
);
1313 for (i
= 0; i
< ARIZONA_MAX_MICBIAS
; i
++) {
1314 if (!arizona
->pdata
.micbias
[i
].mV
&&
1315 !arizona
->pdata
.micbias
[i
].bypass
)
1318 /* Apply default for bypass mode */
1319 if (!arizona
->pdata
.micbias
[i
].mV
)
1320 arizona
->pdata
.micbias
[i
].mV
= 2800;
1322 val
= (arizona
->pdata
.micbias
[i
].mV
- 1500) / 100;
1324 val
<<= ARIZONA_MICB1_LVL_SHIFT
;
1326 if (arizona
->pdata
.micbias
[i
].ext_cap
)
1327 val
|= ARIZONA_MICB1_EXT_CAP
;
1329 if (arizona
->pdata
.micbias
[i
].discharge
)
1330 val
|= ARIZONA_MICB1_DISCH
;
1332 if (arizona
->pdata
.micbias
[i
].soft_start
)
1333 val
|= ARIZONA_MICB1_RATE
;
1335 if (arizona
->pdata
.micbias
[i
].bypass
)
1336 val
|= ARIZONA_MICB1_BYPASS
;
1338 regmap_update_bits(arizona
->regmap
,
1339 ARIZONA_MIC_BIAS_CTRL_1
+ i
,
1340 ARIZONA_MICB1_LVL_MASK
|
1341 ARIZONA_MICB1_EXT_CAP
|
1342 ARIZONA_MICB1_DISCH
|
1343 ARIZONA_MICB1_BYPASS
|
1344 ARIZONA_MICB1_RATE
, val
);
1347 pm_runtime_set_active(arizona
->dev
);
1348 pm_runtime_enable(arizona
->dev
);
1350 /* Set up for interrupts */
1351 ret
= arizona_irq_init(arizona
);
1355 pm_runtime_set_autosuspend_delay(arizona
->dev
, 100);
1356 pm_runtime_use_autosuspend(arizona
->dev
);
1358 arizona_request_irq(arizona
, ARIZONA_IRQ_CLKGEN_ERR
, "CLKGEN error",
1359 arizona_clkgen_err
, arizona
);
1360 arizona_request_irq(arizona
, ARIZONA_IRQ_OVERCLOCKED
, "Overclocked",
1361 arizona_overclocked
, arizona
);
1362 arizona_request_irq(arizona
, ARIZONA_IRQ_UNDERCLOCKED
, "Underclocked",
1363 arizona_underclocked
, arizona
);
1365 ret
= mfd_add_devices(arizona
->dev
, PLATFORM_DEVID_NONE
,
1366 subdevs
, n_subdevs
, NULL
, 0, NULL
);
1369 dev_err(arizona
->dev
, "Failed to add subdevices: %d\n", ret
);
1376 arizona_irq_exit(arizona
);
1378 pm_runtime_disable(arizona
->dev
);
1380 switch (arizona
->pdata
.clk32k_src
) {
1381 case ARIZONA_32KZ_MCLK1
:
1382 case ARIZONA_32KZ_MCLK2
:
1383 arizona_clk32k_disable(arizona
);
1389 arizona_enable_reset(arizona
);
1390 regulator_disable(arizona
->dcvdd
);
1392 regulator_bulk_disable(arizona
->num_core_supplies
,
1393 arizona
->core_supplies
);
1395 regulator_put(arizona
->dcvdd
);
1397 mfd_remove_devices(dev
);
1400 EXPORT_SYMBOL_GPL(arizona_dev_init
);
1402 int arizona_dev_exit(struct arizona
*arizona
)
1404 disable_irq(arizona
->irq
);
1405 pm_runtime_disable(arizona
->dev
);
1407 regulator_disable(arizona
->dcvdd
);
1408 regulator_put(arizona
->dcvdd
);
1410 switch (arizona
->pdata
.clk32k_src
) {
1411 case ARIZONA_32KZ_MCLK1
:
1412 case ARIZONA_32KZ_MCLK2
:
1413 arizona_clk32k_disable(arizona
);
1419 mfd_remove_devices(arizona
->dev
);
1420 arizona_free_irq(arizona
, ARIZONA_IRQ_UNDERCLOCKED
, arizona
);
1421 arizona_free_irq(arizona
, ARIZONA_IRQ_OVERCLOCKED
, arizona
);
1422 arizona_free_irq(arizona
, ARIZONA_IRQ_CLKGEN_ERR
, arizona
);
1423 arizona_irq_exit(arizona
);
1424 arizona_enable_reset(arizona
);
1426 regulator_bulk_disable(arizona
->num_core_supplies
,
1427 arizona
->core_supplies
);
1430 EXPORT_SYMBOL_GPL(arizona_dev_exit
);
1432 MODULE_DESCRIPTION("Wolfson Arizona core driver");
1433 MODULE_LICENSE("GPL v2");