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/ktime.h>
28 #include <linux/platform_device.h>
30 #include <linux/mfd/arizona/core.h>
31 #include <linux/mfd/arizona/registers.h>
35 static const char * const wm5102_core_supplies
[] = {
40 int arizona_clk32k_enable(struct arizona
*arizona
)
44 mutex_lock(&arizona
->clk_lock
);
46 arizona
->clk32k_ref
++;
48 if (arizona
->clk32k_ref
== 1) {
49 switch (arizona
->pdata
.clk32k_src
) {
50 case ARIZONA_32KZ_MCLK1
:
51 ret
= pm_runtime_get_sync(arizona
->dev
);
54 ret
= clk_prepare_enable(arizona
->mclk
[ARIZONA_MCLK1
]);
56 pm_runtime_put_sync(arizona
->dev
);
60 case ARIZONA_32KZ_MCLK2
:
61 ret
= clk_prepare_enable(arizona
->mclk
[ARIZONA_MCLK2
]);
67 ret
= regmap_update_bits(arizona
->regmap
, ARIZONA_CLOCK_32K_1
,
74 arizona
->clk32k_ref
--;
76 mutex_unlock(&arizona
->clk_lock
);
80 EXPORT_SYMBOL_GPL(arizona_clk32k_enable
);
82 int arizona_clk32k_disable(struct arizona
*arizona
)
84 mutex_lock(&arizona
->clk_lock
);
86 BUG_ON(arizona
->clk32k_ref
<= 0);
88 arizona
->clk32k_ref
--;
90 if (arizona
->clk32k_ref
== 0) {
91 regmap_update_bits(arizona
->regmap
, ARIZONA_CLOCK_32K_1
,
92 ARIZONA_CLK_32K_ENA
, 0);
94 switch (arizona
->pdata
.clk32k_src
) {
95 case ARIZONA_32KZ_MCLK1
:
96 pm_runtime_put_sync(arizona
->dev
);
97 clk_disable_unprepare(arizona
->mclk
[ARIZONA_MCLK1
]);
99 case ARIZONA_32KZ_MCLK2
:
100 clk_disable_unprepare(arizona
->mclk
[ARIZONA_MCLK2
]);
105 mutex_unlock(&arizona
->clk_lock
);
109 EXPORT_SYMBOL_GPL(arizona_clk32k_disable
);
111 static irqreturn_t
arizona_clkgen_err(int irq
, void *data
)
113 struct arizona
*arizona
= data
;
115 dev_err(arizona
->dev
, "CLKGEN error\n");
120 static irqreturn_t
arizona_underclocked(int irq
, void *data
)
122 struct arizona
*arizona
= data
;
126 ret
= regmap_read(arizona
->regmap
, ARIZONA_INTERRUPT_RAW_STATUS_8
,
129 dev_err(arizona
->dev
, "Failed to read underclock status: %d\n",
134 if (val
& ARIZONA_AIF3_UNDERCLOCKED_STS
)
135 dev_err(arizona
->dev
, "AIF3 underclocked\n");
136 if (val
& ARIZONA_AIF2_UNDERCLOCKED_STS
)
137 dev_err(arizona
->dev
, "AIF2 underclocked\n");
138 if (val
& ARIZONA_AIF1_UNDERCLOCKED_STS
)
139 dev_err(arizona
->dev
, "AIF1 underclocked\n");
140 if (val
& ARIZONA_ISRC3_UNDERCLOCKED_STS
)
141 dev_err(arizona
->dev
, "ISRC3 underclocked\n");
142 if (val
& ARIZONA_ISRC2_UNDERCLOCKED_STS
)
143 dev_err(arizona
->dev
, "ISRC2 underclocked\n");
144 if (val
& ARIZONA_ISRC1_UNDERCLOCKED_STS
)
145 dev_err(arizona
->dev
, "ISRC1 underclocked\n");
146 if (val
& ARIZONA_FX_UNDERCLOCKED_STS
)
147 dev_err(arizona
->dev
, "FX underclocked\n");
148 if (val
& ARIZONA_ASRC_UNDERCLOCKED_STS
)
149 dev_err(arizona
->dev
, "ASRC underclocked\n");
150 if (val
& ARIZONA_DAC_UNDERCLOCKED_STS
)
151 dev_err(arizona
->dev
, "DAC underclocked\n");
152 if (val
& ARIZONA_ADC_UNDERCLOCKED_STS
)
153 dev_err(arizona
->dev
, "ADC underclocked\n");
154 if (val
& ARIZONA_MIXER_UNDERCLOCKED_STS
)
155 dev_err(arizona
->dev
, "Mixer dropped sample\n");
160 static irqreturn_t
arizona_overclocked(int irq
, void *data
)
162 struct arizona
*arizona
= data
;
166 ret
= regmap_bulk_read(arizona
->regmap
, ARIZONA_INTERRUPT_RAW_STATUS_6
,
169 dev_err(arizona
->dev
, "Failed to read overclock status: %d\n",
174 switch (arizona
->type
) {
177 /* Some bits are shifted on WM8998,
178 * rearrange to match the standard bit layout
180 val
[0] = ((val
[0] & 0x60e0) >> 1) |
181 ((val
[0] & 0x1e00) >> 2) |
188 if (val
[0] & ARIZONA_PWM_OVERCLOCKED_STS
)
189 dev_err(arizona
->dev
, "PWM overclocked\n");
190 if (val
[0] & ARIZONA_FX_CORE_OVERCLOCKED_STS
)
191 dev_err(arizona
->dev
, "FX core overclocked\n");
192 if (val
[0] & ARIZONA_DAC_SYS_OVERCLOCKED_STS
)
193 dev_err(arizona
->dev
, "DAC SYS overclocked\n");
194 if (val
[0] & ARIZONA_DAC_WARP_OVERCLOCKED_STS
)
195 dev_err(arizona
->dev
, "DAC WARP overclocked\n");
196 if (val
[0] & ARIZONA_ADC_OVERCLOCKED_STS
)
197 dev_err(arizona
->dev
, "ADC overclocked\n");
198 if (val
[0] & ARIZONA_MIXER_OVERCLOCKED_STS
)
199 dev_err(arizona
->dev
, "Mixer overclocked\n");
200 if (val
[0] & ARIZONA_AIF3_SYNC_OVERCLOCKED_STS
)
201 dev_err(arizona
->dev
, "AIF3 overclocked\n");
202 if (val
[0] & ARIZONA_AIF2_SYNC_OVERCLOCKED_STS
)
203 dev_err(arizona
->dev
, "AIF2 overclocked\n");
204 if (val
[0] & ARIZONA_AIF1_SYNC_OVERCLOCKED_STS
)
205 dev_err(arizona
->dev
, "AIF1 overclocked\n");
206 if (val
[0] & ARIZONA_PAD_CTRL_OVERCLOCKED_STS
)
207 dev_err(arizona
->dev
, "Pad control overclocked\n");
209 if (val
[1] & ARIZONA_SLIMBUS_SUBSYS_OVERCLOCKED_STS
)
210 dev_err(arizona
->dev
, "Slimbus subsystem overclocked\n");
211 if (val
[1] & ARIZONA_SLIMBUS_ASYNC_OVERCLOCKED_STS
)
212 dev_err(arizona
->dev
, "Slimbus async overclocked\n");
213 if (val
[1] & ARIZONA_SLIMBUS_SYNC_OVERCLOCKED_STS
)
214 dev_err(arizona
->dev
, "Slimbus sync overclocked\n");
215 if (val
[1] & ARIZONA_ASRC_ASYNC_SYS_OVERCLOCKED_STS
)
216 dev_err(arizona
->dev
, "ASRC async system overclocked\n");
217 if (val
[1] & ARIZONA_ASRC_ASYNC_WARP_OVERCLOCKED_STS
)
218 dev_err(arizona
->dev
, "ASRC async WARP overclocked\n");
219 if (val
[1] & ARIZONA_ASRC_SYNC_SYS_OVERCLOCKED_STS
)
220 dev_err(arizona
->dev
, "ASRC sync system overclocked\n");
221 if (val
[1] & ARIZONA_ASRC_SYNC_WARP_OVERCLOCKED_STS
)
222 dev_err(arizona
->dev
, "ASRC sync WARP overclocked\n");
223 if (val
[1] & ARIZONA_ADSP2_1_OVERCLOCKED_STS
)
224 dev_err(arizona
->dev
, "DSP1 overclocked\n");
225 if (val
[1] & ARIZONA_ISRC3_OVERCLOCKED_STS
)
226 dev_err(arizona
->dev
, "ISRC3 overclocked\n");
227 if (val
[1] & ARIZONA_ISRC2_OVERCLOCKED_STS
)
228 dev_err(arizona
->dev
, "ISRC2 overclocked\n");
229 if (val
[1] & ARIZONA_ISRC1_OVERCLOCKED_STS
)
230 dev_err(arizona
->dev
, "ISRC1 overclocked\n");
232 if (val
[2] & ARIZONA_SPDIF_OVERCLOCKED_STS
)
233 dev_err(arizona
->dev
, "SPDIF overclocked\n");
238 #define ARIZONA_REG_POLL_DELAY_US 7500
240 static inline bool arizona_poll_reg_delay(ktime_t timeout
)
242 if (ktime_compare(ktime_get(), timeout
) > 0)
245 usleep_range(ARIZONA_REG_POLL_DELAY_US
/ 2, ARIZONA_REG_POLL_DELAY_US
);
250 static int arizona_poll_reg(struct arizona
*arizona
,
251 int timeout_ms
, unsigned int reg
,
252 unsigned int mask
, unsigned int target
)
254 ktime_t timeout
= ktime_add_us(ktime_get(), timeout_ms
* USEC_PER_MSEC
);
255 unsigned int val
= 0;
259 ret
= regmap_read(arizona
->regmap
, reg
, &val
);
261 if ((val
& mask
) == target
)
263 } while (arizona_poll_reg_delay(timeout
));
266 dev_err(arizona
->dev
, "Failed polling reg 0x%x: %d\n",
271 dev_err(arizona
->dev
, "Polling reg 0x%x timed out: %x\n", reg
, val
);
275 static int arizona_wait_for_boot(struct arizona
*arizona
)
280 * We can't use an interrupt as we need to runtime resume to do so,
281 * we won't race with the interrupt handler as it'll be blocked on
284 ret
= arizona_poll_reg(arizona
, 30, ARIZONA_INTERRUPT_RAW_STATUS_5
,
285 ARIZONA_BOOT_DONE_STS
, ARIZONA_BOOT_DONE_STS
);
288 regmap_write(arizona
->regmap
, ARIZONA_INTERRUPT_STATUS_5
,
289 ARIZONA_BOOT_DONE_STS
);
291 pm_runtime_mark_last_busy(arizona
->dev
);
296 static inline void arizona_enable_reset(struct arizona
*arizona
)
298 if (arizona
->pdata
.reset
)
299 gpiod_set_raw_value_cansleep(arizona
->pdata
.reset
, 0);
302 static void arizona_disable_reset(struct arizona
*arizona
)
304 if (arizona
->pdata
.reset
) {
305 switch (arizona
->type
) {
308 /* Meet requirements for minimum reset duration */
309 usleep_range(5000, 10000);
315 gpiod_set_raw_value_cansleep(arizona
->pdata
.reset
, 1);
316 usleep_range(1000, 5000);
320 struct arizona_sysclk_state
{
325 static int arizona_enable_freerun_sysclk(struct arizona
*arizona
,
326 struct arizona_sysclk_state
*state
)
330 /* Cache existing FLL and SYSCLK settings */
331 ret
= regmap_read(arizona
->regmap
, ARIZONA_FLL1_CONTROL_1
, &state
->fll
);
333 dev_err(arizona
->dev
, "Failed to cache FLL settings: %d\n",
337 ret
= regmap_read(arizona
->regmap
, ARIZONA_SYSTEM_CLOCK_1
,
340 dev_err(arizona
->dev
, "Failed to cache SYSCLK settings: %d\n",
345 /* Start up SYSCLK using the FLL in free running mode */
346 ret
= regmap_write(arizona
->regmap
, ARIZONA_FLL1_CONTROL_1
,
347 ARIZONA_FLL1_ENA
| ARIZONA_FLL1_FREERUN
);
349 dev_err(arizona
->dev
,
350 "Failed to start FLL in freerunning mode: %d\n",
354 ret
= arizona_poll_reg(arizona
, 180, ARIZONA_INTERRUPT_RAW_STATUS_5
,
355 ARIZONA_FLL1_CLOCK_OK_STS
,
356 ARIZONA_FLL1_CLOCK_OK_STS
);
360 ret
= regmap_write(arizona
->regmap
, ARIZONA_SYSTEM_CLOCK_1
, 0x0144);
362 dev_err(arizona
->dev
, "Failed to start SYSCLK: %d\n", ret
);
369 err
= regmap_write(arizona
->regmap
, ARIZONA_FLL1_CONTROL_1
, state
->fll
);
371 dev_err(arizona
->dev
,
372 "Failed to re-apply old FLL settings: %d\n", err
);
377 static int arizona_disable_freerun_sysclk(struct arizona
*arizona
,
378 struct arizona_sysclk_state
*state
)
382 ret
= regmap_write(arizona
->regmap
, ARIZONA_SYSTEM_CLOCK_1
,
385 dev_err(arizona
->dev
,
386 "Failed to re-apply old SYSCLK settings: %d\n", ret
);
390 ret
= regmap_write(arizona
->regmap
, ARIZONA_FLL1_CONTROL_1
, state
->fll
);
392 dev_err(arizona
->dev
,
393 "Failed to re-apply old FLL settings: %d\n", ret
);
400 static int wm5102_apply_hardware_patch(struct arizona
*arizona
)
402 struct arizona_sysclk_state state
;
405 ret
= arizona_enable_freerun_sysclk(arizona
, &state
);
409 /* Start the write sequencer and wait for it to finish */
410 ret
= regmap_write(arizona
->regmap
, ARIZONA_WRITE_SEQUENCER_CTRL_0
,
411 ARIZONA_WSEQ_ENA
| ARIZONA_WSEQ_START
| 160);
413 dev_err(arizona
->dev
, "Failed to start write sequencer: %d\n",
418 ret
= arizona_poll_reg(arizona
, 30, ARIZONA_WRITE_SEQUENCER_CTRL_1
,
419 ARIZONA_WSEQ_BUSY
, 0);
421 regmap_write(arizona
->regmap
, ARIZONA_WRITE_SEQUENCER_CTRL_0
,
425 err
= arizona_disable_freerun_sysclk(arizona
, &state
);
431 * Register patch to some of the CODECs internal write sequences
432 * to ensure a clean exit from the low power sleep state.
434 static const struct reg_sequence wm5110_sleep_patch
[] = {
441 static int wm5110_apply_sleep_patch(struct arizona
*arizona
)
443 struct arizona_sysclk_state state
;
446 ret
= arizona_enable_freerun_sysclk(arizona
, &state
);
450 ret
= regmap_multi_reg_write_bypassed(arizona
->regmap
,
452 ARRAY_SIZE(wm5110_sleep_patch
));
454 err
= arizona_disable_freerun_sysclk(arizona
, &state
);
459 static int wm5102_clear_write_sequencer(struct arizona
*arizona
)
463 ret
= regmap_write(arizona
->regmap
, ARIZONA_WRITE_SEQUENCER_CTRL_3
,
466 dev_err(arizona
->dev
,
467 "Failed to clear write sequencer state: %d\n", ret
);
471 arizona_enable_reset(arizona
);
472 regulator_disable(arizona
->dcvdd
);
476 ret
= regulator_enable(arizona
->dcvdd
);
478 dev_err(arizona
->dev
, "Failed to re-enable DCVDD: %d\n", ret
);
481 arizona_disable_reset(arizona
);
487 static int arizona_isolate_dcvdd(struct arizona
*arizona
)
491 ret
= regmap_update_bits(arizona
->regmap
,
492 ARIZONA_ISOLATION_CONTROL
,
493 ARIZONA_ISOLATE_DCVDD1
,
494 ARIZONA_ISOLATE_DCVDD1
);
496 dev_err(arizona
->dev
, "Failed to isolate DCVDD: %d\n", ret
);
501 static int arizona_connect_dcvdd(struct arizona
*arizona
)
505 ret
= regmap_update_bits(arizona
->regmap
,
506 ARIZONA_ISOLATION_CONTROL
,
507 ARIZONA_ISOLATE_DCVDD1
, 0);
509 dev_err(arizona
->dev
, "Failed to connect DCVDD: %d\n", ret
);
514 static int arizona_is_jack_det_active(struct arizona
*arizona
)
519 ret
= regmap_read(arizona
->regmap
, ARIZONA_JACK_DETECT_ANALOGUE
, &val
);
521 dev_err(arizona
->dev
,
522 "Failed to check jack det status: %d\n", ret
);
524 } else if (val
& ARIZONA_JD1_ENA
) {
531 static int arizona_runtime_resume(struct device
*dev
)
533 struct arizona
*arizona
= dev_get_drvdata(dev
);
536 dev_dbg(arizona
->dev
, "Leaving AoD mode\n");
538 if (arizona
->has_fully_powered_off
) {
539 dev_dbg(arizona
->dev
, "Re-enabling core supplies\n");
541 ret
= regulator_bulk_enable(arizona
->num_core_supplies
,
542 arizona
->core_supplies
);
544 dev_err(dev
, "Failed to enable core supplies: %d\n",
550 ret
= regulator_enable(arizona
->dcvdd
);
552 dev_err(arizona
->dev
, "Failed to enable DCVDD: %d\n", ret
);
553 if (arizona
->has_fully_powered_off
)
554 regulator_bulk_disable(arizona
->num_core_supplies
,
555 arizona
->core_supplies
);
559 if (arizona
->has_fully_powered_off
) {
560 arizona_disable_reset(arizona
);
561 enable_irq(arizona
->irq
);
562 arizona
->has_fully_powered_off
= false;
565 regcache_cache_only(arizona
->regmap
, false);
567 switch (arizona
->type
) {
569 if (arizona
->external_dcvdd
) {
570 ret
= arizona_connect_dcvdd(arizona
);
575 ret
= wm5102_patch(arizona
);
577 dev_err(arizona
->dev
, "Failed to apply patch: %d\n",
582 ret
= wm5102_apply_hardware_patch(arizona
);
584 dev_err(arizona
->dev
,
585 "Failed to apply hardware patch: %d\n",
592 ret
= arizona_wait_for_boot(arizona
);
596 if (arizona
->external_dcvdd
) {
597 ret
= arizona_connect_dcvdd(arizona
);
602 * As this is only called for the internal regulator
603 * (where we know voltage ranges available) it is ok
604 * to request an exact range.
606 ret
= regulator_set_voltage(arizona
->dcvdd
,
609 dev_err(arizona
->dev
,
610 "Failed to set resume voltage: %d\n",
616 ret
= wm5110_apply_sleep_patch(arizona
);
618 dev_err(arizona
->dev
,
619 "Failed to re-apply sleep patch: %d\n",
626 ret
= arizona_wait_for_boot(arizona
);
631 ret
= arizona_wait_for_boot(arizona
);
635 if (arizona
->external_dcvdd
) {
636 ret
= arizona_connect_dcvdd(arizona
);
643 ret
= regcache_sync(arizona
->regmap
);
645 dev_err(arizona
->dev
, "Failed to restore register cache\n");
652 regcache_cache_only(arizona
->regmap
, true);
653 regulator_disable(arizona
->dcvdd
);
657 static int arizona_runtime_suspend(struct device
*dev
)
659 struct arizona
*arizona
= dev_get_drvdata(dev
);
663 dev_dbg(arizona
->dev
, "Entering AoD mode\n");
665 switch (arizona
->type
) {
668 jd_active
= arizona_is_jack_det_active(arizona
);
672 if (arizona
->external_dcvdd
) {
673 ret
= arizona_isolate_dcvdd(arizona
);
678 * As this is only called for the internal regulator
679 * (where we know voltage ranges available) it is ok
680 * to request an exact range.
682 ret
= regulator_set_voltage(arizona
->dcvdd
,
685 dev_err(arizona
->dev
,
686 "Failed to set suspend voltage: %d\n",
693 jd_active
= arizona_is_jack_det_active(arizona
);
697 if (arizona
->external_dcvdd
) {
698 ret
= arizona_isolate_dcvdd(arizona
);
704 ret
= regmap_write(arizona
->regmap
,
705 ARIZONA_WRITE_SEQUENCER_CTRL_3
, 0x0);
707 dev_err(arizona
->dev
,
708 "Failed to clear write sequencer: %d\n",
718 jd_active
= arizona_is_jack_det_active(arizona
);
722 if (arizona
->external_dcvdd
) {
723 ret
= arizona_isolate_dcvdd(arizona
);
730 regcache_cache_only(arizona
->regmap
, true);
731 regcache_mark_dirty(arizona
->regmap
);
732 regulator_disable(arizona
->dcvdd
);
734 /* Allow us to completely power down if no jack detection */
736 dev_dbg(arizona
->dev
, "Fully powering off\n");
738 arizona
->has_fully_powered_off
= true;
740 disable_irq_nosync(arizona
->irq
);
741 arizona_enable_reset(arizona
);
742 regulator_bulk_disable(arizona
->num_core_supplies
,
743 arizona
->core_supplies
);
750 #ifdef CONFIG_PM_SLEEP
751 static int arizona_suspend(struct device
*dev
)
753 struct arizona
*arizona
= dev_get_drvdata(dev
);
755 dev_dbg(arizona
->dev
, "Suspend, disabling IRQ\n");
756 disable_irq(arizona
->irq
);
761 static int arizona_suspend_noirq(struct device
*dev
)
763 struct arizona
*arizona
= dev_get_drvdata(dev
);
765 dev_dbg(arizona
->dev
, "Late suspend, reenabling IRQ\n");
766 enable_irq(arizona
->irq
);
771 static int arizona_resume_noirq(struct device
*dev
)
773 struct arizona
*arizona
= dev_get_drvdata(dev
);
775 dev_dbg(arizona
->dev
, "Early resume, disabling IRQ\n");
776 disable_irq(arizona
->irq
);
781 static int arizona_resume(struct device
*dev
)
783 struct arizona
*arizona
= dev_get_drvdata(dev
);
785 dev_dbg(arizona
->dev
, "Resume, reenabling IRQ\n");
786 enable_irq(arizona
->irq
);
792 const struct dev_pm_ops arizona_pm_ops
= {
793 SET_RUNTIME_PM_OPS(arizona_runtime_suspend
,
794 arizona_runtime_resume
,
796 SET_SYSTEM_SLEEP_PM_OPS(arizona_suspend
, arizona_resume
)
797 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(arizona_suspend_noirq
,
798 arizona_resume_noirq
)
800 EXPORT_SYMBOL_GPL(arizona_pm_ops
);
803 unsigned long arizona_of_get_type(struct device
*dev
)
805 const struct of_device_id
*id
= of_match_device(arizona_of_match
, dev
);
808 return (unsigned long)id
->data
;
812 EXPORT_SYMBOL_GPL(arizona_of_get_type
);
814 static int arizona_of_get_core_pdata(struct arizona
*arizona
)
816 struct arizona_pdata
*pdata
= &arizona
->pdata
;
819 /* Handle old non-standard DT binding */
820 pdata
->reset
= devm_gpiod_get_from_of_node(arizona
->dev
,
821 arizona
->dev
->of_node
,
825 if (IS_ERR(pdata
->reset
)) {
826 ret
= PTR_ERR(pdata
->reset
);
829 * Reset missing will be caught when other binding is read
830 * but all other errors imply this binding is in use but has
831 * encountered a problem so should be handled.
833 if (ret
== -EPROBE_DEFER
)
835 else if (ret
!= -ENOENT
&& ret
!= -ENOSYS
)
836 dev_err(arizona
->dev
, "Reset GPIO malformed: %d\n",
842 ret
= of_property_read_u32_array(arizona
->dev
->of_node
,
844 pdata
->gpio_defaults
,
845 ARRAY_SIZE(pdata
->gpio_defaults
));
848 * All values are literal except out of range values
849 * which are chip default, translate into platform
850 * data which uses 0 as chip default and out of range
853 for (i
= 0; i
< ARRAY_SIZE(pdata
->gpio_defaults
); i
++) {
854 if (pdata
->gpio_defaults
[i
] > 0xffff)
855 pdata
->gpio_defaults
[i
] = 0;
856 else if (pdata
->gpio_defaults
[i
] == 0)
857 pdata
->gpio_defaults
[i
] = 0x10000;
860 dev_err(arizona
->dev
, "Failed to parse GPIO defaults: %d\n",
867 const struct of_device_id arizona_of_match
[] = {
868 { .compatible
= "wlf,wm5102", .data
= (void *)WM5102
},
869 { .compatible
= "wlf,wm5110", .data
= (void *)WM5110
},
870 { .compatible
= "wlf,wm8280", .data
= (void *)WM8280
},
871 { .compatible
= "wlf,wm8997", .data
= (void *)WM8997
},
872 { .compatible
= "wlf,wm8998", .data
= (void *)WM8998
},
873 { .compatible
= "wlf,wm1814", .data
= (void *)WM1814
},
874 { .compatible
= "wlf,wm1831", .data
= (void *)WM1831
},
875 { .compatible
= "cirrus,cs47l24", .data
= (void *)CS47L24
},
878 EXPORT_SYMBOL_GPL(arizona_of_match
);
880 static inline int arizona_of_get_core_pdata(struct arizona
*arizona
)
886 static const struct mfd_cell early_devs
[] = {
887 { .name
= "arizona-ldo1" },
890 static const char * const wm5102_supplies
[] = {
899 static const struct mfd_cell wm5102_devs
[] = {
900 { .name
= "arizona-micsupp" },
901 { .name
= "arizona-gpio" },
903 .name
= "arizona-extcon",
904 .parent_supplies
= wm5102_supplies
,
905 .num_parent_supplies
= 1, /* We only need MICVDD */
907 { .name
= "arizona-haptics" },
908 { .name
= "arizona-pwm" },
910 .name
= "wm5102-codec",
911 .parent_supplies
= wm5102_supplies
,
912 .num_parent_supplies
= ARRAY_SIZE(wm5102_supplies
),
916 static const struct mfd_cell wm5110_devs
[] = {
917 { .name
= "arizona-micsupp" },
918 { .name
= "arizona-gpio" },
920 .name
= "arizona-extcon",
921 .parent_supplies
= wm5102_supplies
,
922 .num_parent_supplies
= 1, /* We only need MICVDD */
924 { .name
= "arizona-haptics" },
925 { .name
= "arizona-pwm" },
927 .name
= "wm5110-codec",
928 .parent_supplies
= wm5102_supplies
,
929 .num_parent_supplies
= ARRAY_SIZE(wm5102_supplies
),
933 static const char * const cs47l24_supplies
[] = {
939 static const struct mfd_cell cs47l24_devs
[] = {
940 { .name
= "arizona-gpio" },
941 { .name
= "arizona-haptics" },
942 { .name
= "arizona-pwm" },
944 .name
= "cs47l24-codec",
945 .parent_supplies
= cs47l24_supplies
,
946 .num_parent_supplies
= ARRAY_SIZE(cs47l24_supplies
),
950 static const char * const wm8997_supplies
[] = {
957 static const struct mfd_cell wm8997_devs
[] = {
958 { .name
= "arizona-micsupp" },
959 { .name
= "arizona-gpio" },
961 .name
= "arizona-extcon",
962 .parent_supplies
= wm8997_supplies
,
963 .num_parent_supplies
= 1, /* We only need MICVDD */
965 { .name
= "arizona-haptics" },
966 { .name
= "arizona-pwm" },
968 .name
= "wm8997-codec",
969 .parent_supplies
= wm8997_supplies
,
970 .num_parent_supplies
= ARRAY_SIZE(wm8997_supplies
),
974 static const struct mfd_cell wm8998_devs
[] = {
975 { .name
= "arizona-micsupp" },
976 { .name
= "arizona-gpio" },
978 .name
= "arizona-extcon",
979 .parent_supplies
= wm5102_supplies
,
980 .num_parent_supplies
= 1, /* We only need MICVDD */
982 { .name
= "arizona-haptics" },
983 { .name
= "arizona-pwm" },
985 .name
= "wm8998-codec",
986 .parent_supplies
= wm5102_supplies
,
987 .num_parent_supplies
= ARRAY_SIZE(wm5102_supplies
),
991 int arizona_dev_init(struct arizona
*arizona
)
993 const char * const mclk_name
[] = { "mclk1", "mclk2" };
994 struct device
*dev
= arizona
->dev
;
995 const char *type_name
= NULL
;
996 unsigned int reg
, val
;
997 int (*apply_patch
)(struct arizona
*) = NULL
;
998 const struct mfd_cell
*subdevs
= NULL
;
999 int n_subdevs
= 0, ret
, i
;
1001 dev_set_drvdata(arizona
->dev
, arizona
);
1002 mutex_init(&arizona
->clk_lock
);
1004 if (dev_get_platdata(arizona
->dev
)) {
1005 memcpy(&arizona
->pdata
, dev_get_platdata(arizona
->dev
),
1006 sizeof(arizona
->pdata
));
1008 ret
= arizona_of_get_core_pdata(arizona
);
1013 BUILD_BUG_ON(ARRAY_SIZE(arizona
->mclk
) != ARRAY_SIZE(mclk_name
));
1014 for (i
= 0; i
< ARRAY_SIZE(arizona
->mclk
); i
++) {
1015 arizona
->mclk
[i
] = devm_clk_get(arizona
->dev
, mclk_name
[i
]);
1016 if (IS_ERR(arizona
->mclk
[i
])) {
1017 dev_info(arizona
->dev
, "Failed to get %s: %ld\n",
1018 mclk_name
[i
], PTR_ERR(arizona
->mclk
[i
]));
1019 arizona
->mclk
[i
] = NULL
;
1023 regcache_cache_only(arizona
->regmap
, true);
1025 switch (arizona
->type
) {
1034 for (i
= 0; i
< ARRAY_SIZE(wm5102_core_supplies
); i
++)
1035 arizona
->core_supplies
[i
].supply
1036 = wm5102_core_supplies
[i
];
1037 arizona
->num_core_supplies
= ARRAY_SIZE(wm5102_core_supplies
);
1040 dev_err(arizona
->dev
, "Unknown device type %d\n",
1045 /* Mark DCVDD as external, LDO1 driver will clear if internal */
1046 arizona
->external_dcvdd
= true;
1048 switch (arizona
->type
) {
1051 break; /* No LDO1 regulator */
1053 ret
= mfd_add_devices(arizona
->dev
, -1, early_devs
,
1054 ARRAY_SIZE(early_devs
), NULL
, 0, NULL
);
1056 dev_err(dev
, "Failed to add early children: %d\n", ret
);
1062 ret
= devm_regulator_bulk_get(dev
, arizona
->num_core_supplies
,
1063 arizona
->core_supplies
);
1065 dev_err(dev
, "Failed to request core supplies: %d\n",
1071 * Don't use devres here because the only device we have to get
1072 * against is the MFD device and DCVDD will likely be supplied by
1073 * one of its children. Meaning that the regulator will be
1074 * destroyed by the time devres calls regulator put.
1076 arizona
->dcvdd
= regulator_get(arizona
->dev
, "DCVDD");
1077 if (IS_ERR(arizona
->dcvdd
)) {
1078 ret
= PTR_ERR(arizona
->dcvdd
);
1079 dev_err(dev
, "Failed to request DCVDD: %d\n", ret
);
1083 if (!arizona
->pdata
.reset
) {
1084 /* Start out with /RESET low to put the chip into reset */
1085 arizona
->pdata
.reset
= devm_gpiod_get(arizona
->dev
, "reset",
1087 if (IS_ERR(arizona
->pdata
.reset
)) {
1088 ret
= PTR_ERR(arizona
->pdata
.reset
);
1089 if (ret
== -EPROBE_DEFER
)
1092 dev_err(arizona
->dev
,
1093 "Reset GPIO missing/malformed: %d\n", ret
);
1095 arizona
->pdata
.reset
= NULL
;
1099 ret
= regulator_bulk_enable(arizona
->num_core_supplies
,
1100 arizona
->core_supplies
);
1102 dev_err(dev
, "Failed to enable core supplies: %d\n",
1107 ret
= regulator_enable(arizona
->dcvdd
);
1109 dev_err(dev
, "Failed to enable DCVDD: %d\n", ret
);
1113 arizona_disable_reset(arizona
);
1115 regcache_cache_only(arizona
->regmap
, false);
1117 /* Verify that this is a chip we know about */
1118 ret
= regmap_read(arizona
->regmap
, ARIZONA_SOFTWARE_RESET
, ®
);
1120 dev_err(dev
, "Failed to read ID register: %d\n", ret
);
1132 dev_err(arizona
->dev
, "Unknown device ID: %x\n", reg
);
1137 /* If we have a /RESET GPIO we'll already be reset */
1138 if (!arizona
->pdata
.reset
) {
1139 ret
= regmap_write(arizona
->regmap
, ARIZONA_SOFTWARE_RESET
, 0);
1141 dev_err(dev
, "Failed to reset device: %d\n", ret
);
1145 usleep_range(1000, 5000);
1148 /* Ensure device startup is complete */
1149 switch (arizona
->type
) {
1151 ret
= regmap_read(arizona
->regmap
,
1152 ARIZONA_WRITE_SEQUENCER_CTRL_3
, &val
);
1155 "Failed to check write sequencer state: %d\n",
1157 } else if (val
& 0x01) {
1158 ret
= wm5102_clear_write_sequencer(arizona
);
1167 ret
= arizona_wait_for_boot(arizona
);
1169 dev_err(arizona
->dev
, "Device failed initial boot: %d\n", ret
);
1173 /* Read the device ID information & do device specific stuff */
1174 ret
= regmap_read(arizona
->regmap
, ARIZONA_SOFTWARE_RESET
, ®
);
1176 dev_err(dev
, "Failed to read ID register: %d\n", ret
);
1180 ret
= regmap_read(arizona
->regmap
, ARIZONA_DEVICE_REVISION
,
1183 dev_err(dev
, "Failed to read revision register: %d\n", ret
);
1186 arizona
->rev
&= ARIZONA_DEVICE_REVISION_MASK
;
1190 if (IS_ENABLED(CONFIG_MFD_WM5102
)) {
1191 type_name
= "WM5102";
1192 if (arizona
->type
!= WM5102
) {
1193 dev_warn(arizona
->dev
,
1194 "WM5102 registered as %d\n",
1196 arizona
->type
= WM5102
;
1199 apply_patch
= wm5102_patch
;
1200 arizona
->rev
&= 0x7;
1201 subdevs
= wm5102_devs
;
1202 n_subdevs
= ARRAY_SIZE(wm5102_devs
);
1206 if (IS_ENABLED(CONFIG_MFD_WM5110
)) {
1207 switch (arizona
->type
) {
1209 type_name
= "WM5110";
1212 type_name
= "WM8280";
1215 type_name
= "WM5110";
1216 dev_warn(arizona
->dev
,
1217 "WM5110 registered as %d\n",
1219 arizona
->type
= WM5110
;
1223 apply_patch
= wm5110_patch
;
1224 subdevs
= wm5110_devs
;
1225 n_subdevs
= ARRAY_SIZE(wm5110_devs
);
1229 if (IS_ENABLED(CONFIG_MFD_CS47L24
)) {
1230 switch (arizona
->type
) {
1232 type_name
= "CS47L24";
1236 type_name
= "WM1831";
1240 dev_warn(arizona
->dev
,
1241 "CS47L24 registered as %d\n",
1243 arizona
->type
= CS47L24
;
1247 apply_patch
= cs47l24_patch
;
1248 subdevs
= cs47l24_devs
;
1249 n_subdevs
= ARRAY_SIZE(cs47l24_devs
);
1253 if (IS_ENABLED(CONFIG_MFD_WM8997
)) {
1254 type_name
= "WM8997";
1255 if (arizona
->type
!= WM8997
) {
1256 dev_warn(arizona
->dev
,
1257 "WM8997 registered as %d\n",
1259 arizona
->type
= WM8997
;
1262 apply_patch
= wm8997_patch
;
1263 subdevs
= wm8997_devs
;
1264 n_subdevs
= ARRAY_SIZE(wm8997_devs
);
1268 if (IS_ENABLED(CONFIG_MFD_WM8998
)) {
1269 switch (arizona
->type
) {
1271 type_name
= "WM8998";
1275 type_name
= "WM1814";
1279 type_name
= "WM8998";
1280 dev_warn(arizona
->dev
,
1281 "WM8998 registered as %d\n",
1283 arizona
->type
= WM8998
;
1286 apply_patch
= wm8998_patch
;
1287 subdevs
= wm8998_devs
;
1288 n_subdevs
= ARRAY_SIZE(wm8998_devs
);
1292 dev_err(arizona
->dev
, "Unknown device ID %x\n", reg
);
1298 dev_err(arizona
->dev
,
1299 "No kernel support for device ID %x\n", reg
);
1304 dev_info(dev
, "%s revision %c\n", type_name
, arizona
->rev
+ 'A');
1307 ret
= apply_patch(arizona
);
1309 dev_err(arizona
->dev
, "Failed to apply patch: %d\n",
1314 switch (arizona
->type
) {
1316 ret
= wm5102_apply_hardware_patch(arizona
);
1318 dev_err(arizona
->dev
,
1319 "Failed to apply hardware patch: %d\n",
1326 ret
= wm5110_apply_sleep_patch(arizona
);
1328 dev_err(arizona
->dev
,
1329 "Failed to apply sleep patch: %d\n",
1339 for (i
= 0; i
< ARRAY_SIZE(arizona
->pdata
.gpio_defaults
); i
++) {
1340 if (!arizona
->pdata
.gpio_defaults
[i
])
1343 regmap_write(arizona
->regmap
, ARIZONA_GPIO1_CTRL
+ i
,
1344 arizona
->pdata
.gpio_defaults
[i
]);
1348 if (!arizona
->pdata
.clk32k_src
)
1349 arizona
->pdata
.clk32k_src
= ARIZONA_32KZ_MCLK2
;
1351 switch (arizona
->pdata
.clk32k_src
) {
1352 case ARIZONA_32KZ_MCLK1
:
1353 case ARIZONA_32KZ_MCLK2
:
1354 regmap_update_bits(arizona
->regmap
, ARIZONA_CLOCK_32K_1
,
1355 ARIZONA_CLK_32K_SRC_MASK
,
1356 arizona
->pdata
.clk32k_src
- 1);
1357 arizona_clk32k_enable(arizona
);
1359 case ARIZONA_32KZ_NONE
:
1360 regmap_update_bits(arizona
->regmap
, ARIZONA_CLOCK_32K_1
,
1361 ARIZONA_CLK_32K_SRC_MASK
, 2);
1364 dev_err(arizona
->dev
, "Invalid 32kHz clock source: %d\n",
1365 arizona
->pdata
.clk32k_src
);
1370 for (i
= 0; i
< ARIZONA_MAX_MICBIAS
; i
++) {
1371 if (!arizona
->pdata
.micbias
[i
].mV
&&
1372 !arizona
->pdata
.micbias
[i
].bypass
)
1375 /* Apply default for bypass mode */
1376 if (!arizona
->pdata
.micbias
[i
].mV
)
1377 arizona
->pdata
.micbias
[i
].mV
= 2800;
1379 val
= (arizona
->pdata
.micbias
[i
].mV
- 1500) / 100;
1381 val
<<= ARIZONA_MICB1_LVL_SHIFT
;
1383 if (arizona
->pdata
.micbias
[i
].ext_cap
)
1384 val
|= ARIZONA_MICB1_EXT_CAP
;
1386 if (arizona
->pdata
.micbias
[i
].discharge
)
1387 val
|= ARIZONA_MICB1_DISCH
;
1389 if (arizona
->pdata
.micbias
[i
].soft_start
)
1390 val
|= ARIZONA_MICB1_RATE
;
1392 if (arizona
->pdata
.micbias
[i
].bypass
)
1393 val
|= ARIZONA_MICB1_BYPASS
;
1395 regmap_update_bits(arizona
->regmap
,
1396 ARIZONA_MIC_BIAS_CTRL_1
+ i
,
1397 ARIZONA_MICB1_LVL_MASK
|
1398 ARIZONA_MICB1_EXT_CAP
|
1399 ARIZONA_MICB1_DISCH
|
1400 ARIZONA_MICB1_BYPASS
|
1401 ARIZONA_MICB1_RATE
, val
);
1404 pm_runtime_set_active(arizona
->dev
);
1405 pm_runtime_enable(arizona
->dev
);
1407 /* Set up for interrupts */
1408 ret
= arizona_irq_init(arizona
);
1412 pm_runtime_set_autosuspend_delay(arizona
->dev
, 100);
1413 pm_runtime_use_autosuspend(arizona
->dev
);
1415 arizona_request_irq(arizona
, ARIZONA_IRQ_CLKGEN_ERR
, "CLKGEN error",
1416 arizona_clkgen_err
, arizona
);
1417 arizona_request_irq(arizona
, ARIZONA_IRQ_OVERCLOCKED
, "Overclocked",
1418 arizona_overclocked
, arizona
);
1419 arizona_request_irq(arizona
, ARIZONA_IRQ_UNDERCLOCKED
, "Underclocked",
1420 arizona_underclocked
, arizona
);
1422 ret
= mfd_add_devices(arizona
->dev
, PLATFORM_DEVID_NONE
,
1423 subdevs
, n_subdevs
, NULL
, 0, NULL
);
1426 dev_err(arizona
->dev
, "Failed to add subdevices: %d\n", ret
);
1433 arizona_irq_exit(arizona
);
1435 pm_runtime_disable(arizona
->dev
);
1437 arizona_enable_reset(arizona
);
1438 regulator_disable(arizona
->dcvdd
);
1440 regulator_bulk_disable(arizona
->num_core_supplies
,
1441 arizona
->core_supplies
);
1443 regulator_put(arizona
->dcvdd
);
1445 mfd_remove_devices(dev
);
1448 EXPORT_SYMBOL_GPL(arizona_dev_init
);
1450 int arizona_dev_exit(struct arizona
*arizona
)
1452 disable_irq(arizona
->irq
);
1453 pm_runtime_disable(arizona
->dev
);
1455 regulator_disable(arizona
->dcvdd
);
1456 regulator_put(arizona
->dcvdd
);
1458 mfd_remove_devices(arizona
->dev
);
1459 arizona_free_irq(arizona
, ARIZONA_IRQ_UNDERCLOCKED
, arizona
);
1460 arizona_free_irq(arizona
, ARIZONA_IRQ_OVERCLOCKED
, arizona
);
1461 arizona_free_irq(arizona
, ARIZONA_IRQ_CLKGEN_ERR
, arizona
);
1462 arizona_irq_exit(arizona
);
1463 arizona_enable_reset(arizona
);
1465 regulator_bulk_disable(arizona
->num_core_supplies
,
1466 arizona
->core_supplies
);
1469 EXPORT_SYMBOL_GPL(arizona_dev_exit
);