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/of_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/slab.h>
24 #include <linux/ktime.h>
25 #include <linux/platform_device.h>
27 #include <linux/mfd/arizona/core.h>
28 #include <linux/mfd/arizona/registers.h>
32 static const char * const wm5102_core_supplies[] = {
37 int arizona_clk32k_enable(struct arizona *arizona)
41 mutex_lock(&arizona->clk_lock);
43 arizona->clk32k_ref++;
45 if (arizona->clk32k_ref == 1) {
46 switch (arizona->pdata.clk32k_src) {
47 case ARIZONA_32KZ_MCLK1:
48 ret = pm_runtime_resume_and_get(arizona->dev);
51 ret = clk_prepare_enable(arizona->mclk[ARIZONA_MCLK1]);
53 pm_runtime_put_sync(arizona->dev);
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,
71 arizona->clk32k_ref--;
73 mutex_unlock(&arizona->clk_lock);
77 EXPORT_SYMBOL_GPL(arizona_clk32k_enable);
79 int arizona_clk32k_disable(struct arizona *arizona)
81 mutex_lock(&arizona->clk_lock);
83 WARN_ON(arizona->clk32k_ref <= 0);
85 arizona->clk32k_ref--;
87 if (arizona->clk32k_ref == 0) {
88 regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
89 ARIZONA_CLK_32K_ENA, 0);
91 switch (arizona->pdata.clk32k_src) {
92 case ARIZONA_32KZ_MCLK1:
93 pm_runtime_put_sync(arizona->dev);
94 clk_disable_unprepare(arizona->mclk[ARIZONA_MCLK1]);
96 case ARIZONA_32KZ_MCLK2:
97 clk_disable_unprepare(arizona->mclk[ARIZONA_MCLK2]);
102 mutex_unlock(&arizona->clk_lock);
106 EXPORT_SYMBOL_GPL(arizona_clk32k_disable);
108 static irqreturn_t arizona_clkgen_err(int irq, void *data)
110 struct arizona *arizona = data;
112 dev_err(arizona->dev, "CLKGEN error\n");
117 static irqreturn_t arizona_underclocked(int irq, void *data)
119 struct arizona *arizona = data;
123 ret = regmap_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_8,
126 dev_err(arizona->dev, "Failed to read underclock status: %d\n",
131 if (val & ARIZONA_AIF3_UNDERCLOCKED_STS)
132 dev_err(arizona->dev, "AIF3 underclocked\n");
133 if (val & ARIZONA_AIF2_UNDERCLOCKED_STS)
134 dev_err(arizona->dev, "AIF2 underclocked\n");
135 if (val & ARIZONA_AIF1_UNDERCLOCKED_STS)
136 dev_err(arizona->dev, "AIF1 underclocked\n");
137 if (val & ARIZONA_ISRC3_UNDERCLOCKED_STS)
138 dev_err(arizona->dev, "ISRC3 underclocked\n");
139 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)
152 dev_err(arizona->dev, "Mixer dropped sample\n");
157 static irqreturn_t arizona_overclocked(int irq, void *data)
159 struct arizona *arizona = data;
163 ret = regmap_bulk_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_6,
166 dev_err(arizona->dev, "Failed to read overclock status: %d\n",
171 switch (arizona->type) {
174 /* Some bits are shifted on WM8998,
175 * rearrange to match the standard bit layout
177 val[0] = ((val[0] & 0x60e0) >> 1) |
178 ((val[0] & 0x1e00) >> 2) |
185 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");
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");
222 if (val[1] & ARIZONA_ISRC3_OVERCLOCKED_STS)
223 dev_err(arizona->dev, "ISRC3 overclocked\n");
224 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");
229 if (val[2] & ARIZONA_SPDIF_OVERCLOCKED_STS)
230 dev_err(arizona->dev, "SPDIF overclocked\n");
235 #define ARIZONA_REG_POLL_DELAY_US 7500
237 static inline bool arizona_poll_reg_delay(ktime_t timeout)
239 if (ktime_compare(ktime_get(), timeout) > 0)
242 usleep_range(ARIZONA_REG_POLL_DELAY_US / 2, ARIZONA_REG_POLL_DELAY_US);
247 static int arizona_poll_reg(struct arizona *arizona,
248 int timeout_ms, unsigned int reg,
249 unsigned int mask, unsigned int target)
251 ktime_t timeout = ktime_add_us(ktime_get(), timeout_ms * USEC_PER_MSEC);
252 unsigned int val = 0;
256 ret = regmap_read(arizona->regmap, reg, &val);
258 if ((val & mask) == target)
260 } while (arizona_poll_reg_delay(timeout));
263 dev_err(arizona->dev, "Failed polling reg 0x%x: %d\n",
268 dev_err(arizona->dev, "Polling reg 0x%x timed out: %x\n", reg, val);
272 static int arizona_wait_for_boot(struct arizona *arizona)
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
281 ret = arizona_poll_reg(arizona, 30, ARIZONA_INTERRUPT_RAW_STATUS_5,
282 ARIZONA_BOOT_DONE_STS, ARIZONA_BOOT_DONE_STS);
285 regmap_write(arizona->regmap, ARIZONA_INTERRUPT_STATUS_5,
286 ARIZONA_BOOT_DONE_STS);
288 pm_runtime_mark_last_busy(arizona->dev);
293 static inline void arizona_enable_reset(struct arizona *arizona)
295 if (arizona->pdata.reset)
296 gpiod_set_raw_value_cansleep(arizona->pdata.reset, 0);
299 static void arizona_disable_reset(struct arizona *arizona)
301 if (arizona->pdata.reset) {
302 switch (arizona->type) {
305 /* Meet requirements for minimum reset duration */
306 usleep_range(5000, 10000);
312 gpiod_set_raw_value_cansleep(arizona->pdata.reset, 1);
313 usleep_range(1000, 5000);
317 struct arizona_sysclk_state {
322 static int arizona_enable_freerun_sysclk(struct arizona *arizona,
323 struct arizona_sysclk_state *state)
327 /* Cache existing FLL and SYSCLK settings */
328 ret = regmap_read(arizona->regmap, ARIZONA_FLL1_CONTROL_1, &state->fll);
330 dev_err(arizona->dev, "Failed to cache FLL settings: %d\n",
334 ret = regmap_read(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1,
337 dev_err(arizona->dev, "Failed to cache SYSCLK settings: %d\n",
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);
346 dev_err(arizona->dev,
347 "Failed to start FLL in freerunning mode: %d\n",
351 ret = arizona_poll_reg(arizona, 180, ARIZONA_INTERRUPT_RAW_STATUS_5,
352 ARIZONA_FLL1_CLOCK_OK_STS,
353 ARIZONA_FLL1_CLOCK_OK_STS);
357 ret = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1, 0x0144);
359 dev_err(arizona->dev, "Failed to start SYSCLK: %d\n", ret);
366 err = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, state->fll);
368 dev_err(arizona->dev,
369 "Failed to re-apply old FLL settings: %d\n", err);
374 static int arizona_disable_freerun_sysclk(struct arizona *arizona,
375 struct arizona_sysclk_state *state)
379 ret = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1,
382 dev_err(arizona->dev,
383 "Failed to re-apply old SYSCLK settings: %d\n", ret);
387 ret = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, state->fll);
389 dev_err(arizona->dev,
390 "Failed to re-apply old FLL settings: %d\n", ret);
397 static int wm5102_apply_hardware_patch(struct arizona *arizona)
399 struct arizona_sysclk_state state;
402 ret = arizona_enable_freerun_sysclk(arizona, &state);
406 /* Start the write sequencer and wait for it to finish */
407 ret = regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0,
408 ARIZONA_WSEQ_ENA | ARIZONA_WSEQ_START | 160);
410 dev_err(arizona->dev, "Failed to start write sequencer: %d\n",
415 ret = arizona_poll_reg(arizona, 30, ARIZONA_WRITE_SEQUENCER_CTRL_1,
416 ARIZONA_WSEQ_BUSY, 0);
418 regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0,
422 err = arizona_disable_freerun_sysclk(arizona, &state);
428 * Register patch to some of the CODECs internal write sequences
429 * to ensure a clean exit from the low power sleep state.
431 static const struct reg_sequence wm5110_sleep_patch[] = {
438 static int wm5110_apply_sleep_patch(struct arizona *arizona)
440 struct arizona_sysclk_state state;
443 ret = arizona_enable_freerun_sysclk(arizona, &state);
447 ret = regmap_multi_reg_write_bypassed(arizona->regmap,
449 ARRAY_SIZE(wm5110_sleep_patch));
451 err = arizona_disable_freerun_sysclk(arizona, &state);
456 static int wm5102_clear_write_sequencer(struct arizona *arizona)
460 ret = regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_3,
463 dev_err(arizona->dev,
464 "Failed to clear write sequencer state: %d\n", ret);
468 arizona_enable_reset(arizona);
469 regulator_disable(arizona->dcvdd);
473 ret = regulator_enable(arizona->dcvdd);
475 dev_err(arizona->dev, "Failed to re-enable DCVDD: %d\n", ret);
478 arizona_disable_reset(arizona);
484 static int arizona_isolate_dcvdd(struct arizona *arizona)
488 ret = regmap_update_bits(arizona->regmap,
489 ARIZONA_ISOLATION_CONTROL,
490 ARIZONA_ISOLATE_DCVDD1,
491 ARIZONA_ISOLATE_DCVDD1);
493 dev_err(arizona->dev, "Failed to isolate DCVDD: %d\n", ret);
498 static int arizona_connect_dcvdd(struct arizona *arizona)
502 ret = regmap_update_bits(arizona->regmap,
503 ARIZONA_ISOLATION_CONTROL,
504 ARIZONA_ISOLATE_DCVDD1, 0);
506 dev_err(arizona->dev, "Failed to connect DCVDD: %d\n", ret);
511 static int arizona_is_jack_det_active(struct arizona *arizona)
516 ret = regmap_read(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE, &val);
518 dev_err(arizona->dev,
519 "Failed to check jack det status: %d\n", ret);
521 } else if (val & ARIZONA_JD1_ENA) {
528 static int arizona_runtime_resume(struct device *dev)
530 struct arizona *arizona = dev_get_drvdata(dev);
533 dev_dbg(arizona->dev, "Leaving AoD mode\n");
535 if (arizona->has_fully_powered_off) {
536 dev_dbg(arizona->dev, "Re-enabling core supplies\n");
538 ret = regulator_bulk_enable(arizona->num_core_supplies,
539 arizona->core_supplies);
541 dev_err(dev, "Failed to enable core supplies: %d\n",
547 ret = regulator_enable(arizona->dcvdd);
549 dev_err(arizona->dev, "Failed to enable DCVDD: %d\n", ret);
550 if (arizona->has_fully_powered_off)
551 regulator_bulk_disable(arizona->num_core_supplies,
552 arizona->core_supplies);
556 if (arizona->has_fully_powered_off) {
557 arizona_disable_reset(arizona);
558 enable_irq(arizona->irq);
559 arizona->has_fully_powered_off = false;
562 regcache_cache_only(arizona->regmap, false);
564 switch (arizona->type) {
566 if (arizona->external_dcvdd) {
567 ret = arizona_connect_dcvdd(arizona);
572 ret = wm5102_patch(arizona);
574 dev_err(arizona->dev, "Failed to apply patch: %d\n",
579 ret = wm5102_apply_hardware_patch(arizona);
581 dev_err(arizona->dev,
582 "Failed to apply hardware patch: %d\n",
589 ret = arizona_wait_for_boot(arizona);
593 if (arizona->external_dcvdd) {
594 ret = arizona_connect_dcvdd(arizona);
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.
603 ret = regulator_set_voltage(arizona->dcvdd,
606 dev_err(arizona->dev,
607 "Failed to set resume voltage: %d\n",
613 ret = wm5110_apply_sleep_patch(arizona);
615 dev_err(arizona->dev,
616 "Failed to re-apply sleep patch: %d\n",
623 ret = arizona_wait_for_boot(arizona);
628 ret = arizona_wait_for_boot(arizona);
632 if (arizona->external_dcvdd) {
633 ret = arizona_connect_dcvdd(arizona);
640 ret = regcache_sync(arizona->regmap);
642 dev_err(arizona->dev, "Failed to restore register cache\n");
649 regcache_cache_only(arizona->regmap, true);
650 regulator_disable(arizona->dcvdd);
654 static int arizona_runtime_suspend(struct device *dev)
656 struct arizona *arizona = dev_get_drvdata(dev);
660 dev_dbg(arizona->dev, "Entering AoD mode\n");
662 switch (arizona->type) {
665 jd_active = arizona_is_jack_det_active(arizona);
669 if (arizona->external_dcvdd) {
670 ret = arizona_isolate_dcvdd(arizona);
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.
679 ret = regulator_set_voltage(arizona->dcvdd,
682 dev_err(arizona->dev,
683 "Failed to set suspend voltage: %d\n",
690 jd_active = arizona_is_jack_det_active(arizona);
694 if (arizona->external_dcvdd) {
695 ret = arizona_isolate_dcvdd(arizona);
701 ret = regmap_write(arizona->regmap,
702 ARIZONA_WRITE_SEQUENCER_CTRL_3, 0x0);
704 dev_err(arizona->dev,
705 "Failed to clear write sequencer: %d\n",
715 jd_active = arizona_is_jack_det_active(arizona);
719 if (arizona->external_dcvdd) {
720 ret = arizona_isolate_dcvdd(arizona);
727 regcache_cache_only(arizona->regmap, true);
728 regcache_mark_dirty(arizona->regmap);
729 regulator_disable(arizona->dcvdd);
731 /* Allow us to completely power down if no jack detection */
733 dev_dbg(arizona->dev, "Fully powering off\n");
735 arizona->has_fully_powered_off = true;
737 disable_irq_nosync(arizona->irq);
738 arizona_enable_reset(arizona);
739 regulator_bulk_disable(arizona->num_core_supplies,
740 arizona->core_supplies);
747 #ifdef CONFIG_PM_SLEEP
748 static int arizona_suspend(struct device *dev)
750 struct arizona *arizona = dev_get_drvdata(dev);
752 dev_dbg(arizona->dev, "Suspend, disabling IRQ\n");
753 disable_irq(arizona->irq);
758 static int arizona_suspend_noirq(struct device *dev)
760 struct arizona *arizona = dev_get_drvdata(dev);
762 dev_dbg(arizona->dev, "Late suspend, reenabling IRQ\n");
763 enable_irq(arizona->irq);
768 static int arizona_resume_noirq(struct device *dev)
770 struct arizona *arizona = dev_get_drvdata(dev);
772 dev_dbg(arizona->dev, "Early resume, disabling IRQ\n");
773 disable_irq(arizona->irq);
778 static int arizona_resume(struct device *dev)
780 struct arizona *arizona = dev_get_drvdata(dev);
782 dev_dbg(arizona->dev, "Resume, reenabling IRQ\n");
783 enable_irq(arizona->irq);
789 const struct dev_pm_ops arizona_pm_ops = {
790 SET_RUNTIME_PM_OPS(arizona_runtime_suspend,
791 arizona_runtime_resume,
793 SET_SYSTEM_SLEEP_PM_OPS(arizona_suspend, arizona_resume)
794 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(arizona_suspend_noirq,
795 arizona_resume_noirq)
797 EXPORT_SYMBOL_GPL(arizona_pm_ops);
800 unsigned long arizona_of_get_type(struct device *dev)
802 const struct of_device_id *id = of_match_device(arizona_of_match, dev);
805 return (unsigned long)id->data;
809 EXPORT_SYMBOL_GPL(arizona_of_get_type);
811 static int arizona_of_get_core_pdata(struct arizona *arizona)
813 struct arizona_pdata *pdata = &arizona->pdata;
816 /* Handle old non-standard DT binding */
817 pdata->reset = devm_gpiod_get(arizona->dev, "wlf,reset", GPIOD_OUT_LOW);
818 if (IS_ERR(pdata->reset)) {
819 ret = PTR_ERR(pdata->reset);
822 * Reset missing will be caught when other binding is read
823 * but all other errors imply this binding is in use but has
824 * encountered a problem so should be handled.
826 if (ret == -EPROBE_DEFER)
828 else if (ret != -ENOENT && ret != -ENOSYS)
829 dev_err(arizona->dev, "Reset GPIO malformed: %d\n",
835 ret = of_property_read_u32_array(arizona->dev->of_node,
837 pdata->gpio_defaults,
838 ARRAY_SIZE(pdata->gpio_defaults));
841 * All values are literal except out of range values
842 * which are chip default, translate into platform
843 * data which uses 0 as chip default and out of range
846 for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
847 if (pdata->gpio_defaults[i] > 0xffff)
848 pdata->gpio_defaults[i] = 0;
849 else if (pdata->gpio_defaults[i] == 0)
850 pdata->gpio_defaults[i] = 0x10000;
853 dev_err(arizona->dev, "Failed to parse GPIO defaults: %d\n",
860 const struct of_device_id arizona_of_match[] = {
861 { .compatible = "wlf,wm5102", .data = (void *)WM5102 },
862 { .compatible = "wlf,wm5110", .data = (void *)WM5110 },
863 { .compatible = "wlf,wm8280", .data = (void *)WM8280 },
864 { .compatible = "wlf,wm8997", .data = (void *)WM8997 },
865 { .compatible = "wlf,wm8998", .data = (void *)WM8998 },
866 { .compatible = "wlf,wm1814", .data = (void *)WM1814 },
867 { .compatible = "wlf,wm1831", .data = (void *)WM1831 },
868 { .compatible = "cirrus,cs47l24", .data = (void *)CS47L24 },
871 EXPORT_SYMBOL_GPL(arizona_of_match);
873 static inline int arizona_of_get_core_pdata(struct arizona *arizona)
879 static const struct mfd_cell early_devs[] = {
880 { .name = "arizona-ldo1" },
883 static const char * const wm5102_supplies[] = {
892 static const struct mfd_cell wm5102_devs[] = {
893 { .name = "arizona-micsupp" },
894 { .name = "arizona-gpio" },
896 .name = "arizona-extcon",
897 .parent_supplies = wm5102_supplies,
898 .num_parent_supplies = 1, /* We only need MICVDD */
900 { .name = "arizona-haptics" },
901 { .name = "arizona-pwm" },
903 .name = "wm5102-codec",
904 .parent_supplies = wm5102_supplies,
905 .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
909 static const struct mfd_cell wm5110_devs[] = {
910 { .name = "arizona-micsupp" },
911 { .name = "arizona-gpio" },
913 .name = "arizona-extcon",
914 .parent_supplies = wm5102_supplies,
915 .num_parent_supplies = 1, /* We only need MICVDD */
917 { .name = "arizona-haptics" },
918 { .name = "arizona-pwm" },
920 .name = "wm5110-codec",
921 .parent_supplies = wm5102_supplies,
922 .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
926 static const char * const cs47l24_supplies[] = {
932 static const struct mfd_cell cs47l24_devs[] = {
933 { .name = "arizona-gpio" },
934 { .name = "arizona-haptics" },
935 { .name = "arizona-pwm" },
937 .name = "cs47l24-codec",
938 .parent_supplies = cs47l24_supplies,
939 .num_parent_supplies = ARRAY_SIZE(cs47l24_supplies),
943 static const char * const wm8997_supplies[] = {
950 static const struct mfd_cell wm8997_devs[] = {
951 { .name = "arizona-micsupp" },
952 { .name = "arizona-gpio" },
954 .name = "arizona-extcon",
955 .parent_supplies = wm8997_supplies,
956 .num_parent_supplies = 1, /* We only need MICVDD */
958 { .name = "arizona-haptics" },
959 { .name = "arizona-pwm" },
961 .name = "wm8997-codec",
962 .parent_supplies = wm8997_supplies,
963 .num_parent_supplies = ARRAY_SIZE(wm8997_supplies),
967 static const struct mfd_cell wm8998_devs[] = {
968 { .name = "arizona-micsupp" },
969 { .name = "arizona-gpio" },
971 .name = "arizona-extcon",
972 .parent_supplies = wm5102_supplies,
973 .num_parent_supplies = 1, /* We only need MICVDD */
975 { .name = "arizona-haptics" },
976 { .name = "arizona-pwm" },
978 .name = "wm8998-codec",
979 .parent_supplies = wm5102_supplies,
980 .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
984 int arizona_dev_init(struct arizona *arizona)
986 static const char * const mclk_name[] = { "mclk1", "mclk2" };
987 struct device *dev = arizona->dev;
988 const char *type_name = NULL;
989 unsigned int reg, val;
990 int (*apply_patch)(struct arizona *) = NULL;
991 const struct mfd_cell *subdevs = NULL;
992 int n_subdevs = 0, ret, i;
994 dev_set_drvdata(arizona->dev, arizona);
995 mutex_init(&arizona->clk_lock);
997 if (dev_get_platdata(arizona->dev)) {
998 memcpy(&arizona->pdata, dev_get_platdata(arizona->dev),
999 sizeof(arizona->pdata));
1001 ret = arizona_of_get_core_pdata(arizona);
1006 BUILD_BUG_ON(ARRAY_SIZE(arizona->mclk) != ARRAY_SIZE(mclk_name));
1007 for (i = 0; i < ARRAY_SIZE(arizona->mclk); i++) {
1008 arizona->mclk[i] = devm_clk_get(arizona->dev, mclk_name[i]);
1009 if (IS_ERR(arizona->mclk[i])) {
1010 dev_info(arizona->dev, "Failed to get %s: %ld\n",
1011 mclk_name[i], PTR_ERR(arizona->mclk[i]));
1012 arizona->mclk[i] = NULL;
1016 regcache_cache_only(arizona->regmap, true);
1018 switch (arizona->type) {
1027 for (i = 0; i < ARRAY_SIZE(wm5102_core_supplies); i++)
1028 arizona->core_supplies[i].supply
1029 = wm5102_core_supplies[i];
1030 arizona->num_core_supplies = ARRAY_SIZE(wm5102_core_supplies);
1033 dev_err(arizona->dev, "Unknown device type %d\n",
1038 /* Mark DCVDD as external, LDO1 driver will clear if internal */
1039 arizona->external_dcvdd = true;
1041 switch (arizona->type) {
1044 break; /* No LDO1 regulator */
1046 ret = mfd_add_devices(arizona->dev, -1, early_devs,
1047 ARRAY_SIZE(early_devs), NULL, 0, NULL);
1049 dev_err(dev, "Failed to add early children: %d\n", ret);
1055 ret = devm_regulator_bulk_get(dev, arizona->num_core_supplies,
1056 arizona->core_supplies);
1058 dev_err(dev, "Failed to request core supplies: %d\n",
1064 * Don't use devres here because the only device we have to get
1065 * against is the MFD device and DCVDD will likely be supplied by
1066 * one of its children. Meaning that the regulator will be
1067 * destroyed by the time devres calls regulator put.
1069 arizona->dcvdd = regulator_get(arizona->dev, "DCVDD");
1070 if (IS_ERR(arizona->dcvdd)) {
1071 ret = PTR_ERR(arizona->dcvdd);
1072 dev_err(dev, "Failed to request DCVDD: %d\n", ret);
1076 if (!arizona->pdata.reset) {
1077 /* Start out with /RESET low to put the chip into reset */
1078 arizona->pdata.reset = devm_gpiod_get(arizona->dev, "reset",
1080 if (IS_ERR(arizona->pdata.reset)) {
1081 ret = PTR_ERR(arizona->pdata.reset);
1082 if (ret == -EPROBE_DEFER)
1085 dev_err(arizona->dev,
1086 "Reset GPIO missing/malformed: %d\n", ret);
1088 arizona->pdata.reset = NULL;
1092 ret = regulator_bulk_enable(arizona->num_core_supplies,
1093 arizona->core_supplies);
1095 dev_err(dev, "Failed to enable core supplies: %d\n",
1100 ret = regulator_enable(arizona->dcvdd);
1102 dev_err(dev, "Failed to enable DCVDD: %d\n", ret);
1106 arizona_disable_reset(arizona);
1108 regcache_cache_only(arizona->regmap, false);
1110 /* Verify that this is a chip we know about */
1111 ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, ®);
1113 dev_err(dev, "Failed to read ID register: %d\n", ret);
1125 dev_err(arizona->dev, "Unknown device ID: %x\n", reg);
1130 /* If we have a /RESET GPIO we'll already be reset */
1131 if (!arizona->pdata.reset) {
1132 ret = regmap_write(arizona->regmap, ARIZONA_SOFTWARE_RESET, 0);
1134 dev_err(dev, "Failed to reset device: %d\n", ret);
1138 usleep_range(1000, 5000);
1141 /* Ensure device startup is complete */
1142 switch (arizona->type) {
1144 ret = regmap_read(arizona->regmap,
1145 ARIZONA_WRITE_SEQUENCER_CTRL_3, &val);
1148 "Failed to check write sequencer state: %d\n",
1150 } else if (val & 0x01) {
1151 ret = wm5102_clear_write_sequencer(arizona);
1160 ret = arizona_wait_for_boot(arizona);
1162 dev_err(arizona->dev, "Device failed initial boot: %d\n", ret);
1166 /* Read the device ID information & do device specific stuff */
1167 ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, ®);
1169 dev_err(dev, "Failed to read ID register: %d\n", ret);
1173 ret = regmap_read(arizona->regmap, ARIZONA_DEVICE_REVISION,
1176 dev_err(dev, "Failed to read revision register: %d\n", ret);
1179 arizona->rev &= ARIZONA_DEVICE_REVISION_MASK;
1183 if (IS_ENABLED(CONFIG_MFD_WM5102)) {
1184 type_name = "WM5102";
1185 if (arizona->type != WM5102) {
1186 dev_warn(arizona->dev,
1187 "WM5102 registered as %d\n",
1189 arizona->type = WM5102;
1192 apply_patch = wm5102_patch;
1193 arizona->rev &= 0x7;
1194 subdevs = wm5102_devs;
1195 n_subdevs = ARRAY_SIZE(wm5102_devs);
1199 if (IS_ENABLED(CONFIG_MFD_WM5110)) {
1200 switch (arizona->type) {
1202 type_name = "WM5110";
1205 type_name = "WM8280";
1208 type_name = "WM5110";
1209 dev_warn(arizona->dev,
1210 "WM5110 registered as %d\n",
1212 arizona->type = WM5110;
1216 apply_patch = wm5110_patch;
1217 subdevs = wm5110_devs;
1218 n_subdevs = ARRAY_SIZE(wm5110_devs);
1222 if (IS_ENABLED(CONFIG_MFD_CS47L24)) {
1223 switch (arizona->type) {
1225 type_name = "CS47L24";
1229 type_name = "WM1831";
1233 dev_warn(arizona->dev,
1234 "CS47L24 registered as %d\n",
1236 arizona->type = CS47L24;
1240 apply_patch = cs47l24_patch;
1241 subdevs = cs47l24_devs;
1242 n_subdevs = ARRAY_SIZE(cs47l24_devs);
1246 if (IS_ENABLED(CONFIG_MFD_WM8997)) {
1247 type_name = "WM8997";
1248 if (arizona->type != WM8997) {
1249 dev_warn(arizona->dev,
1250 "WM8997 registered as %d\n",
1252 arizona->type = WM8997;
1255 apply_patch = wm8997_patch;
1256 subdevs = wm8997_devs;
1257 n_subdevs = ARRAY_SIZE(wm8997_devs);
1261 if (IS_ENABLED(CONFIG_MFD_WM8998)) {
1262 switch (arizona->type) {
1264 type_name = "WM8998";
1268 type_name = "WM1814";
1272 type_name = "WM8998";
1273 dev_warn(arizona->dev,
1274 "WM8998 registered as %d\n",
1276 arizona->type = WM8998;
1279 apply_patch = wm8998_patch;
1280 subdevs = wm8998_devs;
1281 n_subdevs = ARRAY_SIZE(wm8998_devs);
1285 dev_err(arizona->dev, "Unknown device ID %x\n", reg);
1291 dev_err(arizona->dev,
1292 "No kernel support for device ID %x\n", reg);
1297 dev_info(dev, "%s revision %c\n", type_name, arizona->rev + 'A');
1300 ret = apply_patch(arizona);
1302 dev_err(arizona->dev, "Failed to apply patch: %d\n",
1307 switch (arizona->type) {
1309 ret = wm5102_apply_hardware_patch(arizona);
1311 dev_err(arizona->dev,
1312 "Failed to apply hardware patch: %d\n",
1319 ret = wm5110_apply_sleep_patch(arizona);
1321 dev_err(arizona->dev,
1322 "Failed to apply sleep patch: %d\n",
1332 for (i = 0; i < ARRAY_SIZE(arizona->pdata.gpio_defaults); i++) {
1333 if (!arizona->pdata.gpio_defaults[i])
1336 regmap_write(arizona->regmap, ARIZONA_GPIO1_CTRL + i,
1337 arizona->pdata.gpio_defaults[i]);
1341 if (!arizona->pdata.clk32k_src)
1342 arizona->pdata.clk32k_src = ARIZONA_32KZ_MCLK2;
1344 switch (arizona->pdata.clk32k_src) {
1345 case ARIZONA_32KZ_MCLK1:
1346 case ARIZONA_32KZ_MCLK2:
1347 regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
1348 ARIZONA_CLK_32K_SRC_MASK,
1349 arizona->pdata.clk32k_src - 1);
1350 arizona_clk32k_enable(arizona);
1352 case ARIZONA_32KZ_NONE:
1353 regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
1354 ARIZONA_CLK_32K_SRC_MASK, 2);
1357 dev_err(arizona->dev, "Invalid 32kHz clock source: %d\n",
1358 arizona->pdata.clk32k_src);
1363 for (i = 0; i < ARIZONA_MAX_MICBIAS; i++) {
1364 if (!arizona->pdata.micbias[i].mV &&
1365 !arizona->pdata.micbias[i].bypass)
1368 /* Apply default for bypass mode */
1369 if (!arizona->pdata.micbias[i].mV)
1370 arizona->pdata.micbias[i].mV = 2800;
1372 val = (arizona->pdata.micbias[i].mV - 1500) / 100;
1374 val <<= ARIZONA_MICB1_LVL_SHIFT;
1376 if (arizona->pdata.micbias[i].ext_cap)
1377 val |= ARIZONA_MICB1_EXT_CAP;
1379 if (arizona->pdata.micbias[i].discharge)
1380 val |= ARIZONA_MICB1_DISCH;
1382 if (arizona->pdata.micbias[i].soft_start)
1383 val |= ARIZONA_MICB1_RATE;
1385 if (arizona->pdata.micbias[i].bypass)
1386 val |= ARIZONA_MICB1_BYPASS;
1388 regmap_update_bits(arizona->regmap,
1389 ARIZONA_MIC_BIAS_CTRL_1 + i,
1390 ARIZONA_MICB1_LVL_MASK |
1391 ARIZONA_MICB1_EXT_CAP |
1392 ARIZONA_MICB1_DISCH |
1393 ARIZONA_MICB1_BYPASS |
1394 ARIZONA_MICB1_RATE, val);
1397 pm_runtime_set_active(arizona->dev);
1398 pm_runtime_enable(arizona->dev);
1400 /* Set up for interrupts */
1401 ret = arizona_irq_init(arizona);
1405 pm_runtime_set_autosuspend_delay(arizona->dev, 100);
1406 pm_runtime_use_autosuspend(arizona->dev);
1408 arizona_request_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, "CLKGEN error",
1409 arizona_clkgen_err, arizona);
1410 arizona_request_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, "Overclocked",
1411 arizona_overclocked, arizona);
1412 arizona_request_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, "Underclocked",
1413 arizona_underclocked, arizona);
1415 ret = mfd_add_devices(arizona->dev, PLATFORM_DEVID_NONE,
1416 subdevs, n_subdevs, NULL, 0, NULL);
1419 dev_err(arizona->dev, "Failed to add subdevices: %d\n", ret);
1426 arizona_irq_exit(arizona);
1428 pm_runtime_disable(arizona->dev);
1430 switch (arizona->pdata.clk32k_src) {
1431 case ARIZONA_32KZ_MCLK1:
1432 case ARIZONA_32KZ_MCLK2:
1433 arizona_clk32k_disable(arizona);
1439 arizona_enable_reset(arizona);
1440 regulator_disable(arizona->dcvdd);
1442 regulator_bulk_disable(arizona->num_core_supplies,
1443 arizona->core_supplies);
1445 regulator_put(arizona->dcvdd);
1447 mfd_remove_devices(dev);
1450 EXPORT_SYMBOL_GPL(arizona_dev_init);
1452 int arizona_dev_exit(struct arizona *arizona)
1454 disable_irq(arizona->irq);
1455 pm_runtime_disable(arizona->dev);
1457 regulator_disable(arizona->dcvdd);
1458 regulator_put(arizona->dcvdd);
1460 switch (arizona->pdata.clk32k_src) {
1461 case ARIZONA_32KZ_MCLK1:
1462 case ARIZONA_32KZ_MCLK2:
1463 arizona_clk32k_disable(arizona);
1469 mfd_remove_devices(arizona->dev);
1470 arizona_free_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, arizona);
1471 arizona_free_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, arizona);
1472 arizona_free_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, arizona);
1473 arizona_irq_exit(arizona);
1474 arizona_enable_reset(arizona);
1476 regulator_bulk_disable(arizona->num_core_supplies,
1477 arizona->core_supplies);
1480 EXPORT_SYMBOL_GPL(arizona_dev_exit);