4 * Copyright 2012 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/interrupt.h>
18 #include <linux/mfd/core.h>
19 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/machine.h>
26 #include <linux/slab.h>
27 #include <linux/platform_device.h>
29 #include <linux/mfd/arizona/core.h>
30 #include <linux/mfd/arizona/registers.h>
34 static const char * const wm5102_core_supplies
[] = {
39 int arizona_clk32k_enable(struct arizona
*arizona
)
43 mutex_lock(&arizona
->clk_lock
);
45 arizona
->clk32k_ref
++;
47 if (arizona
->clk32k_ref
== 1) {
48 switch (arizona
->pdata
.clk32k_src
) {
49 case ARIZONA_32KZ_MCLK1
:
50 ret
= pm_runtime_get_sync(arizona
->dev
);
53 ret
= clk_prepare_enable(arizona
->mclk
[ARIZONA_MCLK1
]);
57 case ARIZONA_32KZ_MCLK2
:
58 ret
= clk_prepare_enable(arizona
->mclk
[ARIZONA_MCLK2
]);
64 ret
= regmap_update_bits(arizona
->regmap
, ARIZONA_CLOCK_32K_1
,
70 pm_runtime_put_sync(arizona
->dev
);
73 arizona
->clk32k_ref
--;
75 mutex_unlock(&arizona
->clk_lock
);
79 EXPORT_SYMBOL_GPL(arizona_clk32k_enable
);
81 int arizona_clk32k_disable(struct arizona
*arizona
)
83 mutex_lock(&arizona
->clk_lock
);
85 BUG_ON(arizona
->clk32k_ref
<= 0);
87 arizona
->clk32k_ref
--;
89 if (arizona
->clk32k_ref
== 0) {
90 regmap_update_bits(arizona
->regmap
, ARIZONA_CLOCK_32K_1
,
91 ARIZONA_CLK_32K_ENA
, 0);
93 switch (arizona
->pdata
.clk32k_src
) {
94 case ARIZONA_32KZ_MCLK1
:
95 pm_runtime_put_sync(arizona
->dev
);
96 clk_disable_unprepare(arizona
->mclk
[ARIZONA_MCLK1
]);
98 case ARIZONA_32KZ_MCLK2
:
99 clk_disable_unprepare(arizona
->mclk
[ARIZONA_MCLK2
]);
104 mutex_unlock(&arizona
->clk_lock
);
108 EXPORT_SYMBOL_GPL(arizona_clk32k_disable
);
110 static irqreturn_t
arizona_clkgen_err(int irq
, void *data
)
112 struct arizona
*arizona
= data
;
114 dev_err(arizona
->dev
, "CLKGEN error\n");
119 static irqreturn_t
arizona_underclocked(int irq
, void *data
)
121 struct arizona
*arizona
= data
;
125 ret
= regmap_read(arizona
->regmap
, ARIZONA_INTERRUPT_RAW_STATUS_8
,
128 dev_err(arizona
->dev
, "Failed to read underclock status: %d\n",
133 if (val
& ARIZONA_AIF3_UNDERCLOCKED_STS
)
134 dev_err(arizona
->dev
, "AIF3 underclocked\n");
135 if (val
& ARIZONA_AIF2_UNDERCLOCKED_STS
)
136 dev_err(arizona
->dev
, "AIF2 underclocked\n");
137 if (val
& ARIZONA_AIF1_UNDERCLOCKED_STS
)
138 dev_err(arizona
->dev
, "AIF1 underclocked\n");
139 if (val
& ARIZONA_ISRC3_UNDERCLOCKED_STS
)
140 dev_err(arizona
->dev
, "ISRC3 underclocked\n");
141 if (val
& ARIZONA_ISRC2_UNDERCLOCKED_STS
)
142 dev_err(arizona
->dev
, "ISRC2 underclocked\n");
143 if (val
& ARIZONA_ISRC1_UNDERCLOCKED_STS
)
144 dev_err(arizona
->dev
, "ISRC1 underclocked\n");
145 if (val
& ARIZONA_FX_UNDERCLOCKED_STS
)
146 dev_err(arizona
->dev
, "FX underclocked\n");
147 if (val
& ARIZONA_ASRC_UNDERCLOCKED_STS
)
148 dev_err(arizona
->dev
, "ASRC underclocked\n");
149 if (val
& ARIZONA_DAC_UNDERCLOCKED_STS
)
150 dev_err(arizona
->dev
, "DAC underclocked\n");
151 if (val
& ARIZONA_ADC_UNDERCLOCKED_STS
)
152 dev_err(arizona
->dev
, "ADC underclocked\n");
153 if (val
& ARIZONA_MIXER_UNDERCLOCKED_STS
)
154 dev_err(arizona
->dev
, "Mixer dropped sample\n");
159 static irqreturn_t
arizona_overclocked(int irq
, void *data
)
161 struct arizona
*arizona
= data
;
165 ret
= regmap_bulk_read(arizona
->regmap
, ARIZONA_INTERRUPT_RAW_STATUS_6
,
168 dev_err(arizona
->dev
, "Failed to read overclock status: %d\n",
173 switch (arizona
->type
) {
176 /* Some bits are shifted on WM8998,
177 * rearrange to match the standard bit layout
179 val
[0] = ((val
[0] & 0x60e0) >> 1) |
180 ((val
[0] & 0x1e00) >> 2) |
187 if (val
[0] & ARIZONA_PWM_OVERCLOCKED_STS
)
188 dev_err(arizona
->dev
, "PWM overclocked\n");
189 if (val
[0] & ARIZONA_FX_CORE_OVERCLOCKED_STS
)
190 dev_err(arizona
->dev
, "FX core overclocked\n");
191 if (val
[0] & ARIZONA_DAC_SYS_OVERCLOCKED_STS
)
192 dev_err(arizona
->dev
, "DAC SYS overclocked\n");
193 if (val
[0] & ARIZONA_DAC_WARP_OVERCLOCKED_STS
)
194 dev_err(arizona
->dev
, "DAC WARP overclocked\n");
195 if (val
[0] & ARIZONA_ADC_OVERCLOCKED_STS
)
196 dev_err(arizona
->dev
, "ADC overclocked\n");
197 if (val
[0] & ARIZONA_MIXER_OVERCLOCKED_STS
)
198 dev_err(arizona
->dev
, "Mixer overclocked\n");
199 if (val
[0] & ARIZONA_AIF3_SYNC_OVERCLOCKED_STS
)
200 dev_err(arizona
->dev
, "AIF3 overclocked\n");
201 if (val
[0] & ARIZONA_AIF2_SYNC_OVERCLOCKED_STS
)
202 dev_err(arizona
->dev
, "AIF2 overclocked\n");
203 if (val
[0] & ARIZONA_AIF1_SYNC_OVERCLOCKED_STS
)
204 dev_err(arizona
->dev
, "AIF1 overclocked\n");
205 if (val
[0] & ARIZONA_PAD_CTRL_OVERCLOCKED_STS
)
206 dev_err(arizona
->dev
, "Pad control overclocked\n");
208 if (val
[1] & ARIZONA_SLIMBUS_SUBSYS_OVERCLOCKED_STS
)
209 dev_err(arizona
->dev
, "Slimbus subsystem overclocked\n");
210 if (val
[1] & ARIZONA_SLIMBUS_ASYNC_OVERCLOCKED_STS
)
211 dev_err(arizona
->dev
, "Slimbus async overclocked\n");
212 if (val
[1] & ARIZONA_SLIMBUS_SYNC_OVERCLOCKED_STS
)
213 dev_err(arizona
->dev
, "Slimbus sync overclocked\n");
214 if (val
[1] & ARIZONA_ASRC_ASYNC_SYS_OVERCLOCKED_STS
)
215 dev_err(arizona
->dev
, "ASRC async system overclocked\n");
216 if (val
[1] & ARIZONA_ASRC_ASYNC_WARP_OVERCLOCKED_STS
)
217 dev_err(arizona
->dev
, "ASRC async WARP overclocked\n");
218 if (val
[1] & ARIZONA_ASRC_SYNC_SYS_OVERCLOCKED_STS
)
219 dev_err(arizona
->dev
, "ASRC sync system overclocked\n");
220 if (val
[1] & ARIZONA_ASRC_SYNC_WARP_OVERCLOCKED_STS
)
221 dev_err(arizona
->dev
, "ASRC sync WARP overclocked\n");
222 if (val
[1] & ARIZONA_ADSP2_1_OVERCLOCKED_STS
)
223 dev_err(arizona
->dev
, "DSP1 overclocked\n");
224 if (val
[1] & ARIZONA_ISRC3_OVERCLOCKED_STS
)
225 dev_err(arizona
->dev
, "ISRC3 overclocked\n");
226 if (val
[1] & ARIZONA_ISRC2_OVERCLOCKED_STS
)
227 dev_err(arizona
->dev
, "ISRC2 overclocked\n");
228 if (val
[1] & ARIZONA_ISRC1_OVERCLOCKED_STS
)
229 dev_err(arizona
->dev
, "ISRC1 overclocked\n");
231 if (val
[2] & ARIZONA_SPDIF_OVERCLOCKED_STS
)
232 dev_err(arizona
->dev
, "SPDIF overclocked\n");
237 #define ARIZONA_REG_POLL_DELAY_US 7500
239 static int arizona_poll_reg(struct arizona
*arizona
,
240 int timeout_ms
, unsigned int reg
,
241 unsigned int mask
, unsigned int target
)
243 unsigned int val
= 0;
246 ret
= regmap_read_poll_timeout(arizona
->regmap
,
247 reg
, val
, ((val
& mask
) == target
),
248 ARIZONA_REG_POLL_DELAY_US
,
251 dev_err(arizona
->dev
, "Polling reg 0x%x timed out: %x\n",
257 static int arizona_wait_for_boot(struct arizona
*arizona
)
262 * We can't use an interrupt as we need to runtime resume to do so,
263 * we won't race with the interrupt handler as it'll be blocked on
266 ret
= arizona_poll_reg(arizona
, 30, ARIZONA_INTERRUPT_RAW_STATUS_5
,
267 ARIZONA_BOOT_DONE_STS
, ARIZONA_BOOT_DONE_STS
);
270 regmap_write(arizona
->regmap
, ARIZONA_INTERRUPT_STATUS_5
,
271 ARIZONA_BOOT_DONE_STS
);
273 pm_runtime_mark_last_busy(arizona
->dev
);
278 static inline void arizona_enable_reset(struct arizona
*arizona
)
280 if (arizona
->pdata
.reset
)
281 gpiod_set_raw_value_cansleep(arizona
->pdata
.reset
, 0);
284 static void arizona_disable_reset(struct arizona
*arizona
)
286 if (arizona
->pdata
.reset
) {
287 switch (arizona
->type
) {
290 /* Meet requirements for minimum reset duration */
291 usleep_range(5000, 10000);
297 gpiod_set_raw_value_cansleep(arizona
->pdata
.reset
, 1);
298 usleep_range(1000, 5000);
302 struct arizona_sysclk_state
{
307 static int arizona_enable_freerun_sysclk(struct arizona
*arizona
,
308 struct arizona_sysclk_state
*state
)
312 /* Cache existing FLL and SYSCLK settings */
313 ret
= regmap_read(arizona
->regmap
, ARIZONA_FLL1_CONTROL_1
, &state
->fll
);
315 dev_err(arizona
->dev
, "Failed to cache FLL settings: %d\n",
319 ret
= regmap_read(arizona
->regmap
, ARIZONA_SYSTEM_CLOCK_1
,
322 dev_err(arizona
->dev
, "Failed to cache SYSCLK settings: %d\n",
327 /* Start up SYSCLK using the FLL in free running mode */
328 ret
= regmap_write(arizona
->regmap
, ARIZONA_FLL1_CONTROL_1
,
329 ARIZONA_FLL1_ENA
| ARIZONA_FLL1_FREERUN
);
331 dev_err(arizona
->dev
,
332 "Failed to start FLL in freerunning mode: %d\n",
336 ret
= arizona_poll_reg(arizona
, 180, ARIZONA_INTERRUPT_RAW_STATUS_5
,
337 ARIZONA_FLL1_CLOCK_OK_STS
,
338 ARIZONA_FLL1_CLOCK_OK_STS
);
342 ret
= regmap_write(arizona
->regmap
, ARIZONA_SYSTEM_CLOCK_1
, 0x0144);
344 dev_err(arizona
->dev
, "Failed to start SYSCLK: %d\n", ret
);
351 err
= regmap_write(arizona
->regmap
, ARIZONA_FLL1_CONTROL_1
, state
->fll
);
353 dev_err(arizona
->dev
,
354 "Failed to re-apply old FLL settings: %d\n", err
);
359 static int arizona_disable_freerun_sysclk(struct arizona
*arizona
,
360 struct arizona_sysclk_state
*state
)
364 ret
= regmap_write(arizona
->regmap
, ARIZONA_SYSTEM_CLOCK_1
,
367 dev_err(arizona
->dev
,
368 "Failed to re-apply old SYSCLK settings: %d\n", ret
);
372 ret
= regmap_write(arizona
->regmap
, ARIZONA_FLL1_CONTROL_1
, state
->fll
);
374 dev_err(arizona
->dev
,
375 "Failed to re-apply old FLL settings: %d\n", ret
);
382 static int wm5102_apply_hardware_patch(struct arizona
*arizona
)
384 struct arizona_sysclk_state state
;
387 ret
= arizona_enable_freerun_sysclk(arizona
, &state
);
391 /* Start the write sequencer and wait for it to finish */
392 ret
= regmap_write(arizona
->regmap
, ARIZONA_WRITE_SEQUENCER_CTRL_0
,
393 ARIZONA_WSEQ_ENA
| ARIZONA_WSEQ_START
| 160);
395 dev_err(arizona
->dev
, "Failed to start write sequencer: %d\n",
400 ret
= arizona_poll_reg(arizona
, 30, ARIZONA_WRITE_SEQUENCER_CTRL_1
,
401 ARIZONA_WSEQ_BUSY
, 0);
403 regmap_write(arizona
->regmap
, ARIZONA_WRITE_SEQUENCER_CTRL_0
,
407 err
= arizona_disable_freerun_sysclk(arizona
, &state
);
413 * Register patch to some of the CODECs internal write sequences
414 * to ensure a clean exit from the low power sleep state.
416 static const struct reg_sequence wm5110_sleep_patch
[] = {
423 static int wm5110_apply_sleep_patch(struct arizona
*arizona
)
425 struct arizona_sysclk_state state
;
428 ret
= arizona_enable_freerun_sysclk(arizona
, &state
);
432 ret
= regmap_multi_reg_write_bypassed(arizona
->regmap
,
434 ARRAY_SIZE(wm5110_sleep_patch
));
436 err
= arizona_disable_freerun_sysclk(arizona
, &state
);
441 static int wm5102_clear_write_sequencer(struct arizona
*arizona
)
445 ret
= regmap_write(arizona
->regmap
, ARIZONA_WRITE_SEQUENCER_CTRL_3
,
448 dev_err(arizona
->dev
,
449 "Failed to clear write sequencer state: %d\n", ret
);
453 arizona_enable_reset(arizona
);
454 regulator_disable(arizona
->dcvdd
);
458 ret
= regulator_enable(arizona
->dcvdd
);
460 dev_err(arizona
->dev
, "Failed to re-enable DCVDD: %d\n", ret
);
463 arizona_disable_reset(arizona
);
469 static int arizona_isolate_dcvdd(struct arizona
*arizona
)
473 ret
= regmap_update_bits(arizona
->regmap
,
474 ARIZONA_ISOLATION_CONTROL
,
475 ARIZONA_ISOLATE_DCVDD1
,
476 ARIZONA_ISOLATE_DCVDD1
);
478 dev_err(arizona
->dev
, "Failed to isolate DCVDD: %d\n", ret
);
483 static int arizona_connect_dcvdd(struct arizona
*arizona
)
487 ret
= regmap_update_bits(arizona
->regmap
,
488 ARIZONA_ISOLATION_CONTROL
,
489 ARIZONA_ISOLATE_DCVDD1
, 0);
491 dev_err(arizona
->dev
, "Failed to connect DCVDD: %d\n", ret
);
496 static int arizona_is_jack_det_active(struct arizona
*arizona
)
501 ret
= regmap_read(arizona
->regmap
, ARIZONA_JACK_DETECT_ANALOGUE
, &val
);
503 dev_err(arizona
->dev
,
504 "Failed to check jack det status: %d\n", ret
);
506 } else if (val
& ARIZONA_JD1_ENA
) {
513 static int arizona_runtime_resume(struct device
*dev
)
515 struct arizona
*arizona
= dev_get_drvdata(dev
);
518 dev_dbg(arizona
->dev
, "Leaving AoD mode\n");
520 if (arizona
->has_fully_powered_off
) {
521 dev_dbg(arizona
->dev
, "Re-enabling core supplies\n");
523 ret
= regulator_bulk_enable(arizona
->num_core_supplies
,
524 arizona
->core_supplies
);
526 dev_err(dev
, "Failed to enable core supplies: %d\n",
532 ret
= regulator_enable(arizona
->dcvdd
);
534 dev_err(arizona
->dev
, "Failed to enable DCVDD: %d\n", ret
);
535 if (arizona
->has_fully_powered_off
)
536 regulator_bulk_disable(arizona
->num_core_supplies
,
537 arizona
->core_supplies
);
541 if (arizona
->has_fully_powered_off
) {
542 arizona_disable_reset(arizona
);
543 enable_irq(arizona
->irq
);
544 arizona
->has_fully_powered_off
= false;
547 regcache_cache_only(arizona
->regmap
, false);
549 switch (arizona
->type
) {
551 if (arizona
->external_dcvdd
) {
552 ret
= arizona_connect_dcvdd(arizona
);
557 ret
= wm5102_patch(arizona
);
559 dev_err(arizona
->dev
, "Failed to apply patch: %d\n",
564 ret
= wm5102_apply_hardware_patch(arizona
);
566 dev_err(arizona
->dev
,
567 "Failed to apply hardware patch: %d\n",
574 ret
= arizona_wait_for_boot(arizona
);
578 if (arizona
->external_dcvdd
) {
579 ret
= arizona_connect_dcvdd(arizona
);
584 * As this is only called for the internal regulator
585 * (where we know voltage ranges available) it is ok
586 * to request an exact range.
588 ret
= regulator_set_voltage(arizona
->dcvdd
,
591 dev_err(arizona
->dev
,
592 "Failed to set resume voltage: %d\n",
598 ret
= wm5110_apply_sleep_patch(arizona
);
600 dev_err(arizona
->dev
,
601 "Failed to re-apply sleep patch: %d\n",
608 ret
= arizona_wait_for_boot(arizona
);
613 ret
= arizona_wait_for_boot(arizona
);
617 if (arizona
->external_dcvdd
) {
618 ret
= arizona_connect_dcvdd(arizona
);
625 ret
= regcache_sync(arizona
->regmap
);
627 dev_err(arizona
->dev
, "Failed to restore register cache\n");
634 regcache_cache_only(arizona
->regmap
, true);
635 regulator_disable(arizona
->dcvdd
);
639 static int arizona_runtime_suspend(struct device
*dev
)
641 struct arizona
*arizona
= dev_get_drvdata(dev
);
645 dev_dbg(arizona
->dev
, "Entering AoD mode\n");
647 switch (arizona
->type
) {
650 jd_active
= arizona_is_jack_det_active(arizona
);
654 if (arizona
->external_dcvdd
) {
655 ret
= arizona_isolate_dcvdd(arizona
);
660 * As this is only called for the internal regulator
661 * (where we know voltage ranges available) it is ok
662 * to request an exact range.
664 ret
= regulator_set_voltage(arizona
->dcvdd
,
667 dev_err(arizona
->dev
,
668 "Failed to set suspend voltage: %d\n",
675 jd_active
= arizona_is_jack_det_active(arizona
);
679 if (arizona
->external_dcvdd
) {
680 ret
= arizona_isolate_dcvdd(arizona
);
686 ret
= regmap_write(arizona
->regmap
,
687 ARIZONA_WRITE_SEQUENCER_CTRL_3
, 0x0);
689 dev_err(arizona
->dev
,
690 "Failed to clear write sequencer: %d\n",
700 jd_active
= arizona_is_jack_det_active(arizona
);
704 if (arizona
->external_dcvdd
) {
705 ret
= arizona_isolate_dcvdd(arizona
);
712 regcache_cache_only(arizona
->regmap
, true);
713 regcache_mark_dirty(arizona
->regmap
);
714 regulator_disable(arizona
->dcvdd
);
716 /* Allow us to completely power down if no jack detection */
718 dev_dbg(arizona
->dev
, "Fully powering off\n");
720 arizona
->has_fully_powered_off
= true;
722 disable_irq_nosync(arizona
->irq
);
723 arizona_enable_reset(arizona
);
724 regulator_bulk_disable(arizona
->num_core_supplies
,
725 arizona
->core_supplies
);
732 #ifdef CONFIG_PM_SLEEP
733 static int arizona_suspend(struct device
*dev
)
735 struct arizona
*arizona
= dev_get_drvdata(dev
);
737 dev_dbg(arizona
->dev
, "Suspend, disabling IRQ\n");
738 disable_irq(arizona
->irq
);
743 static int arizona_suspend_noirq(struct device
*dev
)
745 struct arizona
*arizona
= dev_get_drvdata(dev
);
747 dev_dbg(arizona
->dev
, "Late suspend, reenabling IRQ\n");
748 enable_irq(arizona
->irq
);
753 static int arizona_resume_noirq(struct device
*dev
)
755 struct arizona
*arizona
= dev_get_drvdata(dev
);
757 dev_dbg(arizona
->dev
, "Early resume, disabling IRQ\n");
758 disable_irq(arizona
->irq
);
763 static int arizona_resume(struct device
*dev
)
765 struct arizona
*arizona
= dev_get_drvdata(dev
);
767 dev_dbg(arizona
->dev
, "Resume, reenabling IRQ\n");
768 enable_irq(arizona
->irq
);
774 const struct dev_pm_ops arizona_pm_ops
= {
775 SET_RUNTIME_PM_OPS(arizona_runtime_suspend
,
776 arizona_runtime_resume
,
778 SET_SYSTEM_SLEEP_PM_OPS(arizona_suspend
, arizona_resume
)
779 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(arizona_suspend_noirq
,
780 arizona_resume_noirq
)
782 EXPORT_SYMBOL_GPL(arizona_pm_ops
);
785 unsigned long arizona_of_get_type(struct device
*dev
)
787 const struct of_device_id
*id
= of_match_device(arizona_of_match
, dev
);
790 return (unsigned long)id
->data
;
794 EXPORT_SYMBOL_GPL(arizona_of_get_type
);
796 static int arizona_of_get_core_pdata(struct arizona
*arizona
)
798 struct arizona_pdata
*pdata
= &arizona
->pdata
;
801 /* Handle old non-standard DT binding */
802 pdata
->reset
= devm_gpiod_get_from_of_node(arizona
->dev
,
803 arizona
->dev
->of_node
,
807 if (IS_ERR(pdata
->reset
)) {
808 ret
= PTR_ERR(pdata
->reset
);
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.
815 if (ret
== -EPROBE_DEFER
)
817 else if (ret
!= -ENOENT
&& ret
!= -ENOSYS
)
818 dev_err(arizona
->dev
, "Reset GPIO malformed: %d\n",
824 ret
= of_property_read_u32_array(arizona
->dev
->of_node
,
826 pdata
->gpio_defaults
,
827 ARRAY_SIZE(pdata
->gpio_defaults
));
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
835 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;
842 dev_err(arizona
->dev
, "Failed to parse GPIO defaults: %d\n",
849 const struct of_device_id arizona_of_match
[] = {
850 { .compatible
= "wlf,wm5102", .data
= (void *)WM5102
},
851 { .compatible
= "wlf,wm5110", .data
= (void *)WM5110
},
852 { .compatible
= "wlf,wm8280", .data
= (void *)WM8280
},
853 { .compatible
= "wlf,wm8997", .data
= (void *)WM8997
},
854 { .compatible
= "wlf,wm8998", .data
= (void *)WM8998
},
855 { .compatible
= "wlf,wm1814", .data
= (void *)WM1814
},
856 { .compatible
= "wlf,wm1831", .data
= (void *)WM1831
},
857 { .compatible
= "cirrus,cs47l24", .data
= (void *)CS47L24
},
860 EXPORT_SYMBOL_GPL(arizona_of_match
);
862 static inline int arizona_of_get_core_pdata(struct arizona
*arizona
)
868 static const struct mfd_cell early_devs
[] = {
869 { .name
= "arizona-ldo1" },
872 static const char * const wm5102_supplies
[] = {
881 static const struct mfd_cell wm5102_devs
[] = {
882 { .name
= "arizona-micsupp" },
883 { .name
= "arizona-gpio" },
885 .name
= "arizona-extcon",
886 .parent_supplies
= wm5102_supplies
,
887 .num_parent_supplies
= 1, /* We only need MICVDD */
889 { .name
= "arizona-haptics" },
890 { .name
= "arizona-pwm" },
892 .name
= "wm5102-codec",
893 .parent_supplies
= wm5102_supplies
,
894 .num_parent_supplies
= ARRAY_SIZE(wm5102_supplies
),
898 static const struct mfd_cell wm5110_devs
[] = {
899 { .name
= "arizona-micsupp" },
900 { .name
= "arizona-gpio" },
902 .name
= "arizona-extcon",
903 .parent_supplies
= wm5102_supplies
,
904 .num_parent_supplies
= 1, /* We only need MICVDD */
906 { .name
= "arizona-haptics" },
907 { .name
= "arizona-pwm" },
909 .name
= "wm5110-codec",
910 .parent_supplies
= wm5102_supplies
,
911 .num_parent_supplies
= ARRAY_SIZE(wm5102_supplies
),
915 static const char * const cs47l24_supplies
[] = {
921 static const struct mfd_cell cs47l24_devs
[] = {
922 { .name
= "arizona-gpio" },
923 { .name
= "arizona-haptics" },
924 { .name
= "arizona-pwm" },
926 .name
= "cs47l24-codec",
927 .parent_supplies
= cs47l24_supplies
,
928 .num_parent_supplies
= ARRAY_SIZE(cs47l24_supplies
),
932 static const char * const wm8997_supplies
[] = {
939 static const struct mfd_cell wm8997_devs
[] = {
940 { .name
= "arizona-micsupp" },
941 { .name
= "arizona-gpio" },
943 .name
= "arizona-extcon",
944 .parent_supplies
= wm8997_supplies
,
945 .num_parent_supplies
= 1, /* We only need MICVDD */
947 { .name
= "arizona-haptics" },
948 { .name
= "arizona-pwm" },
950 .name
= "wm8997-codec",
951 .parent_supplies
= wm8997_supplies
,
952 .num_parent_supplies
= ARRAY_SIZE(wm8997_supplies
),
956 static const struct mfd_cell wm8998_devs
[] = {
957 { .name
= "arizona-micsupp" },
958 { .name
= "arizona-gpio" },
960 .name
= "arizona-extcon",
961 .parent_supplies
= wm5102_supplies
,
962 .num_parent_supplies
= 1, /* We only need MICVDD */
964 { .name
= "arizona-haptics" },
965 { .name
= "arizona-pwm" },
967 .name
= "wm8998-codec",
968 .parent_supplies
= wm5102_supplies
,
969 .num_parent_supplies
= ARRAY_SIZE(wm5102_supplies
),
973 int arizona_dev_init(struct arizona
*arizona
)
975 const char * const mclk_name
[] = { "mclk1", "mclk2" };
976 struct device
*dev
= arizona
->dev
;
977 const char *type_name
= NULL
;
978 unsigned int reg
, val
;
979 int (*apply_patch
)(struct arizona
*) = NULL
;
980 const struct mfd_cell
*subdevs
= NULL
;
981 int n_subdevs
, ret
, i
;
983 dev_set_drvdata(arizona
->dev
, arizona
);
984 mutex_init(&arizona
->clk_lock
);
986 if (dev_get_platdata(arizona
->dev
)) {
987 memcpy(&arizona
->pdata
, dev_get_platdata(arizona
->dev
),
988 sizeof(arizona
->pdata
));
990 ret
= arizona_of_get_core_pdata(arizona
);
995 BUILD_BUG_ON(ARRAY_SIZE(arizona
->mclk
) != ARRAY_SIZE(mclk_name
));
996 for (i
= 0; i
< ARRAY_SIZE(arizona
->mclk
); i
++) {
997 arizona
->mclk
[i
] = devm_clk_get(arizona
->dev
, mclk_name
[i
]);
998 if (IS_ERR(arizona
->mclk
[i
])) {
999 dev_info(arizona
->dev
, "Failed to get %s: %ld\n",
1000 mclk_name
[i
], PTR_ERR(arizona
->mclk
[i
]));
1001 arizona
->mclk
[i
] = NULL
;
1005 regcache_cache_only(arizona
->regmap
, true);
1007 switch (arizona
->type
) {
1016 for (i
= 0; i
< ARRAY_SIZE(wm5102_core_supplies
); i
++)
1017 arizona
->core_supplies
[i
].supply
1018 = wm5102_core_supplies
[i
];
1019 arizona
->num_core_supplies
= ARRAY_SIZE(wm5102_core_supplies
);
1022 dev_err(arizona
->dev
, "Unknown device type %d\n",
1027 /* Mark DCVDD as external, LDO1 driver will clear if internal */
1028 arizona
->external_dcvdd
= true;
1030 switch (arizona
->type
) {
1033 break; /* No LDO1 regulator */
1035 ret
= mfd_add_devices(arizona
->dev
, -1, early_devs
,
1036 ARRAY_SIZE(early_devs
), NULL
, 0, NULL
);
1038 dev_err(dev
, "Failed to add early children: %d\n", ret
);
1044 ret
= devm_regulator_bulk_get(dev
, arizona
->num_core_supplies
,
1045 arizona
->core_supplies
);
1047 dev_err(dev
, "Failed to request core supplies: %d\n",
1053 * Don't use devres here because the only device we have to get
1054 * against is the MFD device and DCVDD will likely be supplied by
1055 * one of its children. Meaning that the regulator will be
1056 * destroyed by the time devres calls regulator put.
1058 arizona
->dcvdd
= regulator_get(arizona
->dev
, "DCVDD");
1059 if (IS_ERR(arizona
->dcvdd
)) {
1060 ret
= PTR_ERR(arizona
->dcvdd
);
1061 dev_err(dev
, "Failed to request DCVDD: %d\n", ret
);
1065 if (!arizona
->pdata
.reset
) {
1066 /* Start out with /RESET low to put the chip into reset */
1067 arizona
->pdata
.reset
= devm_gpiod_get(arizona
->dev
, "reset",
1069 if (IS_ERR(arizona
->pdata
.reset
)) {
1070 ret
= PTR_ERR(arizona
->pdata
.reset
);
1071 if (ret
== -EPROBE_DEFER
)
1074 dev_err(arizona
->dev
,
1075 "Reset GPIO missing/malformed: %d\n", ret
);
1077 arizona
->pdata
.reset
= NULL
;
1081 ret
= regulator_bulk_enable(arizona
->num_core_supplies
,
1082 arizona
->core_supplies
);
1084 dev_err(dev
, "Failed to enable core supplies: %d\n",
1089 ret
= regulator_enable(arizona
->dcvdd
);
1091 dev_err(dev
, "Failed to enable DCVDD: %d\n", ret
);
1095 arizona_disable_reset(arizona
);
1097 regcache_cache_only(arizona
->regmap
, false);
1099 /* Verify that this is a chip we know about */
1100 ret
= regmap_read(arizona
->regmap
, ARIZONA_SOFTWARE_RESET
, ®
);
1102 dev_err(dev
, "Failed to read ID register: %d\n", ret
);
1114 dev_err(arizona
->dev
, "Unknown device ID: %x\n", reg
);
1119 /* If we have a /RESET GPIO we'll already be reset */
1120 if (!arizona
->pdata
.reset
) {
1121 ret
= regmap_write(arizona
->regmap
, ARIZONA_SOFTWARE_RESET
, 0);
1123 dev_err(dev
, "Failed to reset device: %d\n", ret
);
1127 usleep_range(1000, 5000);
1130 /* Ensure device startup is complete */
1131 switch (arizona
->type
) {
1133 ret
= regmap_read(arizona
->regmap
,
1134 ARIZONA_WRITE_SEQUENCER_CTRL_3
, &val
);
1137 "Failed to check write sequencer state: %d\n",
1139 } else if (val
& 0x01) {
1140 ret
= wm5102_clear_write_sequencer(arizona
);
1149 ret
= arizona_wait_for_boot(arizona
);
1151 dev_err(arizona
->dev
, "Device failed initial boot: %d\n", ret
);
1155 /* Read the device ID information & do device specific stuff */
1156 ret
= regmap_read(arizona
->regmap
, ARIZONA_SOFTWARE_RESET
, ®
);
1158 dev_err(dev
, "Failed to read ID register: %d\n", ret
);
1162 ret
= regmap_read(arizona
->regmap
, ARIZONA_DEVICE_REVISION
,
1165 dev_err(dev
, "Failed to read revision register: %d\n", ret
);
1168 arizona
->rev
&= ARIZONA_DEVICE_REVISION_MASK
;
1172 if (IS_ENABLED(CONFIG_MFD_WM5102
)) {
1173 type_name
= "WM5102";
1174 if (arizona
->type
!= WM5102
) {
1175 dev_warn(arizona
->dev
,
1176 "WM5102 registered as %d\n",
1178 arizona
->type
= WM5102
;
1181 apply_patch
= wm5102_patch
;
1182 arizona
->rev
&= 0x7;
1183 subdevs
= wm5102_devs
;
1184 n_subdevs
= ARRAY_SIZE(wm5102_devs
);
1188 if (IS_ENABLED(CONFIG_MFD_WM5110
)) {
1189 switch (arizona
->type
) {
1191 type_name
= "WM5110";
1194 type_name
= "WM8280";
1197 type_name
= "WM5110";
1198 dev_warn(arizona
->dev
,
1199 "WM5110 registered as %d\n",
1201 arizona
->type
= WM5110
;
1205 apply_patch
= wm5110_patch
;
1206 subdevs
= wm5110_devs
;
1207 n_subdevs
= ARRAY_SIZE(wm5110_devs
);
1211 if (IS_ENABLED(CONFIG_MFD_CS47L24
)) {
1212 switch (arizona
->type
) {
1214 type_name
= "CS47L24";
1218 type_name
= "WM1831";
1222 dev_warn(arizona
->dev
,
1223 "CS47L24 registered as %d\n",
1225 arizona
->type
= CS47L24
;
1229 apply_patch
= cs47l24_patch
;
1230 subdevs
= cs47l24_devs
;
1231 n_subdevs
= ARRAY_SIZE(cs47l24_devs
);
1235 if (IS_ENABLED(CONFIG_MFD_WM8997
)) {
1236 type_name
= "WM8997";
1237 if (arizona
->type
!= WM8997
) {
1238 dev_warn(arizona
->dev
,
1239 "WM8997 registered as %d\n",
1241 arizona
->type
= WM8997
;
1244 apply_patch
= wm8997_patch
;
1245 subdevs
= wm8997_devs
;
1246 n_subdevs
= ARRAY_SIZE(wm8997_devs
);
1250 if (IS_ENABLED(CONFIG_MFD_WM8998
)) {
1251 switch (arizona
->type
) {
1253 type_name
= "WM8998";
1257 type_name
= "WM1814";
1261 type_name
= "WM8998";
1262 dev_warn(arizona
->dev
,
1263 "WM8998 registered as %d\n",
1265 arizona
->type
= WM8998
;
1268 apply_patch
= wm8998_patch
;
1269 subdevs
= wm8998_devs
;
1270 n_subdevs
= ARRAY_SIZE(wm8998_devs
);
1274 dev_err(arizona
->dev
, "Unknown device ID %x\n", reg
);
1280 dev_err(arizona
->dev
,
1281 "No kernel support for device ID %x\n", reg
);
1286 dev_info(dev
, "%s revision %c\n", type_name
, arizona
->rev
+ 'A');
1289 ret
= apply_patch(arizona
);
1291 dev_err(arizona
->dev
, "Failed to apply patch: %d\n",
1296 switch (arizona
->type
) {
1298 ret
= wm5102_apply_hardware_patch(arizona
);
1300 dev_err(arizona
->dev
,
1301 "Failed to apply hardware patch: %d\n",
1308 ret
= wm5110_apply_sleep_patch(arizona
);
1310 dev_err(arizona
->dev
,
1311 "Failed to apply sleep patch: %d\n",
1321 for (i
= 0; i
< ARRAY_SIZE(arizona
->pdata
.gpio_defaults
); i
++) {
1322 if (!arizona
->pdata
.gpio_defaults
[i
])
1325 regmap_write(arizona
->regmap
, ARIZONA_GPIO1_CTRL
+ i
,
1326 arizona
->pdata
.gpio_defaults
[i
]);
1330 if (!arizona
->pdata
.clk32k_src
)
1331 arizona
->pdata
.clk32k_src
= ARIZONA_32KZ_MCLK2
;
1333 switch (arizona
->pdata
.clk32k_src
) {
1334 case ARIZONA_32KZ_MCLK1
:
1335 case ARIZONA_32KZ_MCLK2
:
1336 regmap_update_bits(arizona
->regmap
, ARIZONA_CLOCK_32K_1
,
1337 ARIZONA_CLK_32K_SRC_MASK
,
1338 arizona
->pdata
.clk32k_src
- 1);
1339 arizona_clk32k_enable(arizona
);
1341 case ARIZONA_32KZ_NONE
:
1342 regmap_update_bits(arizona
->regmap
, ARIZONA_CLOCK_32K_1
,
1343 ARIZONA_CLK_32K_SRC_MASK
, 2);
1346 dev_err(arizona
->dev
, "Invalid 32kHz clock source: %d\n",
1347 arizona
->pdata
.clk32k_src
);
1352 for (i
= 0; i
< ARIZONA_MAX_MICBIAS
; i
++) {
1353 if (!arizona
->pdata
.micbias
[i
].mV
&&
1354 !arizona
->pdata
.micbias
[i
].bypass
)
1357 /* Apply default for bypass mode */
1358 if (!arizona
->pdata
.micbias
[i
].mV
)
1359 arizona
->pdata
.micbias
[i
].mV
= 2800;
1361 val
= (arizona
->pdata
.micbias
[i
].mV
- 1500) / 100;
1363 val
<<= ARIZONA_MICB1_LVL_SHIFT
;
1365 if (arizona
->pdata
.micbias
[i
].ext_cap
)
1366 val
|= ARIZONA_MICB1_EXT_CAP
;
1368 if (arizona
->pdata
.micbias
[i
].discharge
)
1369 val
|= ARIZONA_MICB1_DISCH
;
1371 if (arizona
->pdata
.micbias
[i
].soft_start
)
1372 val
|= ARIZONA_MICB1_RATE
;
1374 if (arizona
->pdata
.micbias
[i
].bypass
)
1375 val
|= ARIZONA_MICB1_BYPASS
;
1377 regmap_update_bits(arizona
->regmap
,
1378 ARIZONA_MIC_BIAS_CTRL_1
+ i
,
1379 ARIZONA_MICB1_LVL_MASK
|
1380 ARIZONA_MICB1_EXT_CAP
|
1381 ARIZONA_MICB1_DISCH
|
1382 ARIZONA_MICB1_BYPASS
|
1383 ARIZONA_MICB1_RATE
, val
);
1386 pm_runtime_set_active(arizona
->dev
);
1387 pm_runtime_enable(arizona
->dev
);
1389 /* Set up for interrupts */
1390 ret
= arizona_irq_init(arizona
);
1394 pm_runtime_set_autosuspend_delay(arizona
->dev
, 100);
1395 pm_runtime_use_autosuspend(arizona
->dev
);
1397 arizona_request_irq(arizona
, ARIZONA_IRQ_CLKGEN_ERR
, "CLKGEN error",
1398 arizona_clkgen_err
, arizona
);
1399 arizona_request_irq(arizona
, ARIZONA_IRQ_OVERCLOCKED
, "Overclocked",
1400 arizona_overclocked
, arizona
);
1401 arizona_request_irq(arizona
, ARIZONA_IRQ_UNDERCLOCKED
, "Underclocked",
1402 arizona_underclocked
, arizona
);
1404 ret
= mfd_add_devices(arizona
->dev
, PLATFORM_DEVID_NONE
,
1405 subdevs
, n_subdevs
, NULL
, 0, NULL
);
1408 dev_err(arizona
->dev
, "Failed to add subdevices: %d\n", ret
);
1415 arizona_irq_exit(arizona
);
1417 pm_runtime_disable(arizona
->dev
);
1419 arizona_enable_reset(arizona
);
1420 regulator_disable(arizona
->dcvdd
);
1422 regulator_bulk_disable(arizona
->num_core_supplies
,
1423 arizona
->core_supplies
);
1425 regulator_put(arizona
->dcvdd
);
1427 mfd_remove_devices(dev
);
1430 EXPORT_SYMBOL_GPL(arizona_dev_init
);
1432 int arizona_dev_exit(struct arizona
*arizona
)
1434 disable_irq(arizona
->irq
);
1435 pm_runtime_disable(arizona
->dev
);
1437 regulator_disable(arizona
->dcvdd
);
1438 regulator_put(arizona
->dcvdd
);
1440 mfd_remove_devices(arizona
->dev
);
1441 arizona_free_irq(arizona
, ARIZONA_IRQ_UNDERCLOCKED
, arizona
);
1442 arizona_free_irq(arizona
, ARIZONA_IRQ_OVERCLOCKED
, arizona
);
1443 arizona_free_irq(arizona
, ARIZONA_IRQ_CLKGEN_ERR
, arizona
);
1444 arizona_irq_exit(arizona
);
1445 arizona_enable_reset(arizona
);
1447 regulator_bulk_disable(arizona
->num_core_supplies
,
1448 arizona
->core_supplies
);
1451 EXPORT_SYMBOL_GPL(arizona_dev_exit
);