2 * arizona.c - Wolfson Arizona class device shared support
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/delay.h>
14 #include <linux/gcd.h>
15 #include <linux/module.h>
16 #include <linux/pm_runtime.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/tlv.h>
21 #include <linux/mfd/arizona/core.h>
22 #include <linux/mfd/arizona/gpio.h>
23 #include <linux/mfd/arizona/registers.h>
27 #define ARIZONA_AIF_BCLK_CTRL 0x00
28 #define ARIZONA_AIF_TX_PIN_CTRL 0x01
29 #define ARIZONA_AIF_RX_PIN_CTRL 0x02
30 #define ARIZONA_AIF_RATE_CTRL 0x03
31 #define ARIZONA_AIF_FORMAT 0x04
32 #define ARIZONA_AIF_TX_BCLK_RATE 0x05
33 #define ARIZONA_AIF_RX_BCLK_RATE 0x06
34 #define ARIZONA_AIF_FRAME_CTRL_1 0x07
35 #define ARIZONA_AIF_FRAME_CTRL_2 0x08
36 #define ARIZONA_AIF_FRAME_CTRL_3 0x09
37 #define ARIZONA_AIF_FRAME_CTRL_4 0x0A
38 #define ARIZONA_AIF_FRAME_CTRL_5 0x0B
39 #define ARIZONA_AIF_FRAME_CTRL_6 0x0C
40 #define ARIZONA_AIF_FRAME_CTRL_7 0x0D
41 #define ARIZONA_AIF_FRAME_CTRL_8 0x0E
42 #define ARIZONA_AIF_FRAME_CTRL_9 0x0F
43 #define ARIZONA_AIF_FRAME_CTRL_10 0x10
44 #define ARIZONA_AIF_FRAME_CTRL_11 0x11
45 #define ARIZONA_AIF_FRAME_CTRL_12 0x12
46 #define ARIZONA_AIF_FRAME_CTRL_13 0x13
47 #define ARIZONA_AIF_FRAME_CTRL_14 0x14
48 #define ARIZONA_AIF_FRAME_CTRL_15 0x15
49 #define ARIZONA_AIF_FRAME_CTRL_16 0x16
50 #define ARIZONA_AIF_FRAME_CTRL_17 0x17
51 #define ARIZONA_AIF_FRAME_CTRL_18 0x18
52 #define ARIZONA_AIF_TX_ENABLES 0x19
53 #define ARIZONA_AIF_RX_ENABLES 0x1A
54 #define ARIZONA_AIF_FORCE_WRITE 0x1B
56 #define ARIZONA_FLL_VCO_CORNER 141900000
57 #define ARIZONA_FLL_MAX_FREF 13500000
58 #define ARIZONA_FLL_MIN_FVCO 90000000
59 #define ARIZONA_FLL_MAX_FRATIO 16
60 #define ARIZONA_FLL_MAX_REFDIV 8
61 #define ARIZONA_FLL_MIN_OUTDIV 2
62 #define ARIZONA_FLL_MAX_OUTDIV 7
64 #define ARIZONA_FMT_DSP_MODE_A 0
65 #define ARIZONA_FMT_DSP_MODE_B 1
66 #define ARIZONA_FMT_I2S_MODE 2
67 #define ARIZONA_FMT_LEFT_JUSTIFIED_MODE 3
69 #define arizona_fll_err(_fll, fmt, ...) \
70 dev_err(_fll->arizona->dev, "FLL%d: " fmt, _fll->id, ##__VA_ARGS__)
71 #define arizona_fll_warn(_fll, fmt, ...) \
72 dev_warn(_fll->arizona->dev, "FLL%d: " fmt, _fll->id, ##__VA_ARGS__)
73 #define arizona_fll_dbg(_fll, fmt, ...) \
74 dev_dbg(_fll->arizona->dev, "FLL%d: " fmt, _fll->id, ##__VA_ARGS__)
76 #define arizona_aif_err(_dai, fmt, ...) \
77 dev_err(_dai->dev, "AIF%d: " fmt, _dai->id, ##__VA_ARGS__)
78 #define arizona_aif_warn(_dai, fmt, ...) \
79 dev_warn(_dai->dev, "AIF%d: " fmt, _dai->id, ##__VA_ARGS__)
80 #define arizona_aif_dbg(_dai, fmt, ...) \
81 dev_dbg(_dai->dev, "AIF%d: " fmt, _dai->id, ##__VA_ARGS__)
83 static int arizona_spk_ev(struct snd_soc_dapm_widget *w,
84 struct snd_kcontrol *kcontrol,
87 struct snd_soc_codec *codec = w->codec;
88 struct arizona *arizona = dev_get_drvdata(codec->dev->parent);
89 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
90 bool manual_ena = false;
93 switch (arizona->type) {
95 switch (arizona->rev) {
107 case SND_SOC_DAPM_PRE_PMU:
108 if (!priv->spk_ena && manual_ena) {
109 regmap_write_async(arizona->regmap, 0x4f5, 0x25a);
110 priv->spk_ena_pending = true;
113 case SND_SOC_DAPM_POST_PMU:
114 val = snd_soc_read(codec, ARIZONA_INTERRUPT_RAW_STATUS_3);
115 if (val & ARIZONA_SPK_OVERHEAT_STS) {
116 dev_crit(arizona->dev,
117 "Speaker not enabled due to temperature\n");
121 regmap_update_bits_async(arizona->regmap,
122 ARIZONA_OUTPUT_ENABLES_1,
123 1 << w->shift, 1 << w->shift);
125 if (priv->spk_ena_pending) {
127 regmap_write_async(arizona->regmap, 0x4f5, 0xda);
128 priv->spk_ena_pending = false;
132 case SND_SOC_DAPM_PRE_PMD:
136 regmap_write_async(arizona->regmap,
140 regmap_update_bits_async(arizona->regmap,
141 ARIZONA_OUTPUT_ENABLES_1,
144 case SND_SOC_DAPM_POST_PMD:
147 regmap_write_async(arizona->regmap,
156 static irqreturn_t arizona_thermal_warn(int irq, void *data)
158 struct arizona *arizona = data;
162 ret = regmap_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_3,
165 dev_err(arizona->dev, "Failed to read thermal status: %d\n",
167 } else if (val & ARIZONA_SPK_OVERHEAT_WARN_STS) {
168 dev_crit(arizona->dev, "Thermal warning\n");
174 static irqreturn_t arizona_thermal_shutdown(int irq, void *data)
176 struct arizona *arizona = data;
180 ret = regmap_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_3,
183 dev_err(arizona->dev, "Failed to read thermal status: %d\n",
185 } else if (val & ARIZONA_SPK_OVERHEAT_STS) {
186 dev_crit(arizona->dev, "Thermal shutdown\n");
187 ret = regmap_update_bits(arizona->regmap,
188 ARIZONA_OUTPUT_ENABLES_1,
190 ARIZONA_OUT4R_ENA, 0);
192 dev_crit(arizona->dev,
193 "Failed to disable speaker outputs: %d\n",
200 static const struct snd_soc_dapm_widget arizona_spkl =
201 SND_SOC_DAPM_PGA_E("OUT4L", SND_SOC_NOPM,
202 ARIZONA_OUT4L_ENA_SHIFT, 0, NULL, 0, arizona_spk_ev,
203 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU);
205 static const struct snd_soc_dapm_widget arizona_spkr =
206 SND_SOC_DAPM_PGA_E("OUT4R", SND_SOC_NOPM,
207 ARIZONA_OUT4R_ENA_SHIFT, 0, NULL, 0, arizona_spk_ev,
208 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU);
210 int arizona_init_spk(struct snd_soc_codec *codec)
212 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
213 struct arizona *arizona = priv->arizona;
216 ret = snd_soc_dapm_new_controls(&codec->dapm, &arizona_spkl, 1);
220 switch (arizona->type) {
224 ret = snd_soc_dapm_new_controls(&codec->dapm,
231 ret = arizona_request_irq(arizona, ARIZONA_IRQ_SPK_OVERHEAT_WARN,
232 "Thermal warning", arizona_thermal_warn,
235 dev_err(arizona->dev,
236 "Failed to get thermal warning IRQ: %d\n",
239 ret = arizona_request_irq(arizona, ARIZONA_IRQ_SPK_OVERHEAT,
240 "Thermal shutdown", arizona_thermal_shutdown,
243 dev_err(arizona->dev,
244 "Failed to get thermal shutdown IRQ: %d\n",
249 EXPORT_SYMBOL_GPL(arizona_init_spk);
251 static const struct snd_soc_dapm_route arizona_mono_routes[] = {
252 { "OUT1R", NULL, "OUT1L" },
253 { "OUT2R", NULL, "OUT2L" },
254 { "OUT3R", NULL, "OUT3L" },
255 { "OUT4R", NULL, "OUT4L" },
256 { "OUT5R", NULL, "OUT5L" },
257 { "OUT6R", NULL, "OUT6L" },
260 int arizona_init_mono(struct snd_soc_codec *codec)
262 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
263 struct arizona *arizona = priv->arizona;
266 for (i = 0; i < ARIZONA_MAX_OUTPUT; ++i) {
267 if (arizona->pdata.out_mono[i])
268 snd_soc_dapm_add_routes(&codec->dapm,
269 &arizona_mono_routes[i], 1);
274 EXPORT_SYMBOL_GPL(arizona_init_mono);
276 int arizona_init_gpio(struct snd_soc_codec *codec)
278 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
279 struct arizona *arizona = priv->arizona;
282 switch (arizona->type) {
284 snd_soc_dapm_disable_pin(&codec->dapm, "DRC2 Signal Activity");
290 snd_soc_dapm_disable_pin(&codec->dapm, "DRC1 Signal Activity");
292 for (i = 0; i < ARRAY_SIZE(arizona->pdata.gpio_defaults); i++) {
293 switch (arizona->pdata.gpio_defaults[i] & ARIZONA_GPN_FN_MASK) {
294 case ARIZONA_GP_FN_DRC1_SIGNAL_DETECT:
295 snd_soc_dapm_enable_pin(&codec->dapm,
296 "DRC1 Signal Activity");
298 case ARIZONA_GP_FN_DRC2_SIGNAL_DETECT:
299 snd_soc_dapm_enable_pin(&codec->dapm,
300 "DRC2 Signal Activity");
309 EXPORT_SYMBOL_GPL(arizona_init_gpio);
311 const char *arizona_mixer_texts[ARIZONA_NUM_MIXER_INPUTS] = {
416 EXPORT_SYMBOL_GPL(arizona_mixer_texts);
418 int arizona_mixer_values[ARIZONA_NUM_MIXER_INPUTS] = {
424 0x0c, /* Noise mixer */
425 0x0d, /* Comfort noise */
498 0xa0, /* ISRC1INT1 */
502 0xa4, /* ISRC1DEC1 */
506 0xa8, /* ISRC2DEC1 */
510 0xac, /* ISRC2INT1 */
514 0xb0, /* ISRC3DEC1 */
518 0xb4, /* ISRC3INT1 */
523 EXPORT_SYMBOL_GPL(arizona_mixer_values);
525 const DECLARE_TLV_DB_SCALE(arizona_mixer_tlv, -3200, 100, 0);
526 EXPORT_SYMBOL_GPL(arizona_mixer_tlv);
528 const char *arizona_rate_text[ARIZONA_RATE_ENUM_SIZE] = {
529 "SYNCCLK rate", "8kHz", "16kHz", "ASYNCCLK rate",
531 EXPORT_SYMBOL_GPL(arizona_rate_text);
533 const int arizona_rate_val[ARIZONA_RATE_ENUM_SIZE] = {
536 EXPORT_SYMBOL_GPL(arizona_rate_val);
539 const struct soc_enum arizona_isrc_fsh[] = {
540 SOC_VALUE_ENUM_SINGLE(ARIZONA_ISRC_1_CTRL_1,
541 ARIZONA_ISRC1_FSH_SHIFT, 0xf,
542 ARIZONA_RATE_ENUM_SIZE,
543 arizona_rate_text, arizona_rate_val),
544 SOC_VALUE_ENUM_SINGLE(ARIZONA_ISRC_2_CTRL_1,
545 ARIZONA_ISRC2_FSH_SHIFT, 0xf,
546 ARIZONA_RATE_ENUM_SIZE,
547 arizona_rate_text, arizona_rate_val),
548 SOC_VALUE_ENUM_SINGLE(ARIZONA_ISRC_3_CTRL_1,
549 ARIZONA_ISRC3_FSH_SHIFT, 0xf,
550 ARIZONA_RATE_ENUM_SIZE,
551 arizona_rate_text, arizona_rate_val),
553 EXPORT_SYMBOL_GPL(arizona_isrc_fsh);
555 const struct soc_enum arizona_isrc_fsl[] = {
556 SOC_VALUE_ENUM_SINGLE(ARIZONA_ISRC_1_CTRL_2,
557 ARIZONA_ISRC1_FSL_SHIFT, 0xf,
558 ARIZONA_RATE_ENUM_SIZE,
559 arizona_rate_text, arizona_rate_val),
560 SOC_VALUE_ENUM_SINGLE(ARIZONA_ISRC_2_CTRL_2,
561 ARIZONA_ISRC2_FSL_SHIFT, 0xf,
562 ARIZONA_RATE_ENUM_SIZE,
563 arizona_rate_text, arizona_rate_val),
564 SOC_VALUE_ENUM_SINGLE(ARIZONA_ISRC_3_CTRL_2,
565 ARIZONA_ISRC3_FSL_SHIFT, 0xf,
566 ARIZONA_RATE_ENUM_SIZE,
567 arizona_rate_text, arizona_rate_val),
569 EXPORT_SYMBOL_GPL(arizona_isrc_fsl);
571 const struct soc_enum arizona_asrc_rate1 =
572 SOC_VALUE_ENUM_SINGLE(ARIZONA_ASRC_RATE1,
573 ARIZONA_ASRC_RATE1_SHIFT, 0xf,
574 ARIZONA_RATE_ENUM_SIZE - 1,
575 arizona_rate_text, arizona_rate_val);
576 EXPORT_SYMBOL_GPL(arizona_asrc_rate1);
578 static const char *arizona_vol_ramp_text[] = {
579 "0ms/6dB", "0.5ms/6dB", "1ms/6dB", "2ms/6dB", "4ms/6dB", "8ms/6dB",
580 "15ms/6dB", "30ms/6dB",
583 SOC_ENUM_SINGLE_DECL(arizona_in_vd_ramp,
584 ARIZONA_INPUT_VOLUME_RAMP,
585 ARIZONA_IN_VD_RAMP_SHIFT,
586 arizona_vol_ramp_text);
587 EXPORT_SYMBOL_GPL(arizona_in_vd_ramp);
589 SOC_ENUM_SINGLE_DECL(arizona_in_vi_ramp,
590 ARIZONA_INPUT_VOLUME_RAMP,
591 ARIZONA_IN_VI_RAMP_SHIFT,
592 arizona_vol_ramp_text);
593 EXPORT_SYMBOL_GPL(arizona_in_vi_ramp);
595 SOC_ENUM_SINGLE_DECL(arizona_out_vd_ramp,
596 ARIZONA_OUTPUT_VOLUME_RAMP,
597 ARIZONA_OUT_VD_RAMP_SHIFT,
598 arizona_vol_ramp_text);
599 EXPORT_SYMBOL_GPL(arizona_out_vd_ramp);
601 SOC_ENUM_SINGLE_DECL(arizona_out_vi_ramp,
602 ARIZONA_OUTPUT_VOLUME_RAMP,
603 ARIZONA_OUT_VI_RAMP_SHIFT,
604 arizona_vol_ramp_text);
605 EXPORT_SYMBOL_GPL(arizona_out_vi_ramp);
607 static const char *arizona_lhpf_mode_text[] = {
608 "Low-pass", "High-pass"
611 SOC_ENUM_SINGLE_DECL(arizona_lhpf1_mode,
613 ARIZONA_LHPF1_MODE_SHIFT,
614 arizona_lhpf_mode_text);
615 EXPORT_SYMBOL_GPL(arizona_lhpf1_mode);
617 SOC_ENUM_SINGLE_DECL(arizona_lhpf2_mode,
619 ARIZONA_LHPF2_MODE_SHIFT,
620 arizona_lhpf_mode_text);
621 EXPORT_SYMBOL_GPL(arizona_lhpf2_mode);
623 SOC_ENUM_SINGLE_DECL(arizona_lhpf3_mode,
625 ARIZONA_LHPF3_MODE_SHIFT,
626 arizona_lhpf_mode_text);
627 EXPORT_SYMBOL_GPL(arizona_lhpf3_mode);
629 SOC_ENUM_SINGLE_DECL(arizona_lhpf4_mode,
631 ARIZONA_LHPF4_MODE_SHIFT,
632 arizona_lhpf_mode_text);
633 EXPORT_SYMBOL_GPL(arizona_lhpf4_mode);
635 static const char *arizona_ng_hold_text[] = {
636 "30ms", "120ms", "250ms", "500ms",
639 SOC_ENUM_SINGLE_DECL(arizona_ng_hold,
640 ARIZONA_NOISE_GATE_CONTROL,
641 ARIZONA_NGATE_HOLD_SHIFT,
642 arizona_ng_hold_text);
643 EXPORT_SYMBOL_GPL(arizona_ng_hold);
645 static const char * const arizona_in_hpf_cut_text[] = {
646 "2.5Hz", "5Hz", "10Hz", "20Hz", "40Hz"
649 SOC_ENUM_SINGLE_DECL(arizona_in_hpf_cut_enum,
651 ARIZONA_IN_HPF_CUT_SHIFT,
652 arizona_in_hpf_cut_text);
653 EXPORT_SYMBOL_GPL(arizona_in_hpf_cut_enum);
655 static const char * const arizona_in_dmic_osr_text[] = {
656 "1.536MHz", "3.072MHz", "6.144MHz", "768kHz",
659 const struct soc_enum arizona_in_dmic_osr[] = {
660 SOC_ENUM_SINGLE(ARIZONA_IN1L_CONTROL, ARIZONA_IN1_OSR_SHIFT,
661 ARRAY_SIZE(arizona_in_dmic_osr_text),
662 arizona_in_dmic_osr_text),
663 SOC_ENUM_SINGLE(ARIZONA_IN2L_CONTROL, ARIZONA_IN2_OSR_SHIFT,
664 ARRAY_SIZE(arizona_in_dmic_osr_text),
665 arizona_in_dmic_osr_text),
666 SOC_ENUM_SINGLE(ARIZONA_IN3L_CONTROL, ARIZONA_IN3_OSR_SHIFT,
667 ARRAY_SIZE(arizona_in_dmic_osr_text),
668 arizona_in_dmic_osr_text),
669 SOC_ENUM_SINGLE(ARIZONA_IN4L_CONTROL, ARIZONA_IN4_OSR_SHIFT,
670 ARRAY_SIZE(arizona_in_dmic_osr_text),
671 arizona_in_dmic_osr_text),
673 EXPORT_SYMBOL_GPL(arizona_in_dmic_osr);
675 static void arizona_in_set_vu(struct snd_soc_codec *codec, int ena)
677 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
686 for (i = 0; i < priv->num_inputs; i++)
687 snd_soc_update_bits(codec,
688 ARIZONA_ADC_DIGITAL_VOLUME_1L + (i * 4),
692 int arizona_in_ev(struct snd_soc_dapm_widget *w, struct snd_kcontrol *kcontrol,
695 struct arizona_priv *priv = snd_soc_codec_get_drvdata(w->codec);
699 reg = ARIZONA_ADC_DIGITAL_VOLUME_1L + ((w->shift / 2) * 8);
701 reg = ARIZONA_ADC_DIGITAL_VOLUME_1R + ((w->shift / 2) * 8);
704 case SND_SOC_DAPM_PRE_PMU:
707 case SND_SOC_DAPM_POST_PMU:
708 snd_soc_update_bits(w->codec, reg, ARIZONA_IN1L_MUTE, 0);
710 /* If this is the last input pending then allow VU */
712 if (priv->in_pending == 0) {
714 arizona_in_set_vu(w->codec, 1);
717 case SND_SOC_DAPM_PRE_PMD:
718 snd_soc_update_bits(w->codec, reg,
719 ARIZONA_IN1L_MUTE | ARIZONA_IN_VU,
720 ARIZONA_IN1L_MUTE | ARIZONA_IN_VU);
722 case SND_SOC_DAPM_POST_PMD:
723 /* Disable volume updates if no inputs are enabled */
724 reg = snd_soc_read(w->codec, ARIZONA_INPUT_ENABLES);
726 arizona_in_set_vu(w->codec, 0);
731 EXPORT_SYMBOL_GPL(arizona_in_ev);
733 int arizona_out_ev(struct snd_soc_dapm_widget *w,
734 struct snd_kcontrol *kcontrol,
738 case SND_SOC_DAPM_POST_PMU:
740 case ARIZONA_OUT1L_ENA_SHIFT:
741 case ARIZONA_OUT1R_ENA_SHIFT:
742 case ARIZONA_OUT2L_ENA_SHIFT:
743 case ARIZONA_OUT2R_ENA_SHIFT:
744 case ARIZONA_OUT3L_ENA_SHIFT:
745 case ARIZONA_OUT3R_ENA_SHIFT:
757 EXPORT_SYMBOL_GPL(arizona_out_ev);
759 int arizona_hp_ev(struct snd_soc_dapm_widget *w,
760 struct snd_kcontrol *kcontrol,
763 struct arizona_priv *priv = snd_soc_codec_get_drvdata(w->codec);
764 struct arizona *arizona = priv->arizona;
765 unsigned int mask = 1 << w->shift;
769 case SND_SOC_DAPM_POST_PMU:
772 case SND_SOC_DAPM_PRE_PMD:
779 /* Store the desired state for the HP outputs */
780 priv->arizona->hp_ena &= ~mask;
781 priv->arizona->hp_ena |= val;
783 /* Force off if HPDET magic is active */
784 if (priv->arizona->hpdet_magic)
787 regmap_update_bits_async(arizona->regmap, ARIZONA_OUTPUT_ENABLES_1,
790 return arizona_out_ev(w, kcontrol, event);
792 EXPORT_SYMBOL_GPL(arizona_hp_ev);
794 static unsigned int arizona_sysclk_48k_rates[] = {
804 static unsigned int arizona_sysclk_44k1_rates[] = {
814 static int arizona_set_opclk(struct snd_soc_codec *codec, unsigned int clk,
817 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
820 int ref, div, refclk;
823 case ARIZONA_CLK_OPCLK:
824 reg = ARIZONA_OUTPUT_SYSTEM_CLOCK;
825 refclk = priv->sysclk;
827 case ARIZONA_CLK_ASYNC_OPCLK:
828 reg = ARIZONA_OUTPUT_ASYNC_CLOCK;
829 refclk = priv->asyncclk;
836 rates = arizona_sysclk_44k1_rates;
838 rates = arizona_sysclk_48k_rates;
840 for (ref = 0; ref < ARRAY_SIZE(arizona_sysclk_48k_rates) &&
841 rates[ref] <= refclk; ref++) {
843 while (rates[ref] / div >= freq && div < 32) {
844 if (rates[ref] / div == freq) {
845 dev_dbg(codec->dev, "Configured %dHz OPCLK\n",
847 snd_soc_update_bits(codec, reg,
848 ARIZONA_OPCLK_DIV_MASK |
849 ARIZONA_OPCLK_SEL_MASK,
851 ARIZONA_OPCLK_DIV_SHIFT) |
859 dev_err(codec->dev, "Unable to generate %dHz OPCLK\n", freq);
863 int arizona_set_sysclk(struct snd_soc_codec *codec, int clk_id,
864 int source, unsigned int freq, int dir)
866 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
867 struct arizona *arizona = priv->arizona;
870 unsigned int mask = ARIZONA_SYSCLK_FREQ_MASK | ARIZONA_SYSCLK_SRC_MASK;
871 unsigned int val = source << ARIZONA_SYSCLK_SRC_SHIFT;
875 case ARIZONA_CLK_SYSCLK:
877 reg = ARIZONA_SYSTEM_CLOCK_1;
879 mask |= ARIZONA_SYSCLK_FRAC;
881 case ARIZONA_CLK_ASYNCCLK:
883 reg = ARIZONA_ASYNC_CLOCK_1;
884 clk = &priv->asyncclk;
886 case ARIZONA_CLK_OPCLK:
887 case ARIZONA_CLK_ASYNC_OPCLK:
888 return arizona_set_opclk(codec, clk_id, freq);
899 val |= ARIZONA_CLK_12MHZ << ARIZONA_SYSCLK_FREQ_SHIFT;
903 val |= ARIZONA_CLK_24MHZ << ARIZONA_SYSCLK_FREQ_SHIFT;
907 val |= ARIZONA_CLK_49MHZ << ARIZONA_SYSCLK_FREQ_SHIFT;
911 val |= ARIZONA_CLK_73MHZ << ARIZONA_SYSCLK_FREQ_SHIFT;
915 val |= ARIZONA_CLK_98MHZ << ARIZONA_SYSCLK_FREQ_SHIFT;
919 val |= ARIZONA_CLK_147MHZ << ARIZONA_SYSCLK_FREQ_SHIFT;
922 dev_dbg(arizona->dev, "%s cleared\n", name);
932 val |= ARIZONA_SYSCLK_FRAC;
934 dev_dbg(arizona->dev, "%s set to %uHz", name, freq);
936 return regmap_update_bits(arizona->regmap, reg, mask, val);
938 EXPORT_SYMBOL_GPL(arizona_set_sysclk);
940 static int arizona_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
942 struct snd_soc_codec *codec = dai->codec;
943 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
944 struct arizona *arizona = priv->arizona;
945 int lrclk, bclk, mode, base;
947 base = dai->driver->base;
952 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
953 case SND_SOC_DAIFMT_DSP_A:
954 mode = ARIZONA_FMT_DSP_MODE_A;
956 case SND_SOC_DAIFMT_DSP_B:
957 if ((fmt & SND_SOC_DAIFMT_MASTER_MASK)
958 != SND_SOC_DAIFMT_CBM_CFM) {
959 arizona_aif_err(dai, "DSP_B not valid in slave mode\n");
962 mode = ARIZONA_FMT_DSP_MODE_B;
964 case SND_SOC_DAIFMT_I2S:
965 mode = ARIZONA_FMT_I2S_MODE;
967 case SND_SOC_DAIFMT_LEFT_J:
968 if ((fmt & SND_SOC_DAIFMT_MASTER_MASK)
969 != SND_SOC_DAIFMT_CBM_CFM) {
970 arizona_aif_err(dai, "LEFT_J not valid in slave mode\n");
973 mode = ARIZONA_FMT_LEFT_JUSTIFIED_MODE;
976 arizona_aif_err(dai, "Unsupported DAI format %d\n",
977 fmt & SND_SOC_DAIFMT_FORMAT_MASK);
981 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
982 case SND_SOC_DAIFMT_CBS_CFS:
984 case SND_SOC_DAIFMT_CBS_CFM:
985 lrclk |= ARIZONA_AIF1TX_LRCLK_MSTR;
987 case SND_SOC_DAIFMT_CBM_CFS:
988 bclk |= ARIZONA_AIF1_BCLK_MSTR;
990 case SND_SOC_DAIFMT_CBM_CFM:
991 bclk |= ARIZONA_AIF1_BCLK_MSTR;
992 lrclk |= ARIZONA_AIF1TX_LRCLK_MSTR;
995 arizona_aif_err(dai, "Unsupported master mode %d\n",
996 fmt & SND_SOC_DAIFMT_MASTER_MASK);
1000 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1001 case SND_SOC_DAIFMT_NB_NF:
1003 case SND_SOC_DAIFMT_IB_IF:
1004 bclk |= ARIZONA_AIF1_BCLK_INV;
1005 lrclk |= ARIZONA_AIF1TX_LRCLK_INV;
1007 case SND_SOC_DAIFMT_IB_NF:
1008 bclk |= ARIZONA_AIF1_BCLK_INV;
1010 case SND_SOC_DAIFMT_NB_IF:
1011 lrclk |= ARIZONA_AIF1TX_LRCLK_INV;
1017 regmap_update_bits_async(arizona->regmap, base + ARIZONA_AIF_BCLK_CTRL,
1018 ARIZONA_AIF1_BCLK_INV |
1019 ARIZONA_AIF1_BCLK_MSTR,
1021 regmap_update_bits_async(arizona->regmap, base + ARIZONA_AIF_TX_PIN_CTRL,
1022 ARIZONA_AIF1TX_LRCLK_INV |
1023 ARIZONA_AIF1TX_LRCLK_MSTR, lrclk);
1024 regmap_update_bits_async(arizona->regmap,
1025 base + ARIZONA_AIF_RX_PIN_CTRL,
1026 ARIZONA_AIF1RX_LRCLK_INV |
1027 ARIZONA_AIF1RX_LRCLK_MSTR, lrclk);
1028 regmap_update_bits(arizona->regmap, base + ARIZONA_AIF_FORMAT,
1029 ARIZONA_AIF1_FMT_MASK, mode);
1034 static const int arizona_48k_bclk_rates[] = {
1056 static const unsigned int arizona_48k_rates[] = {
1074 static const struct snd_pcm_hw_constraint_list arizona_48k_constraint = {
1075 .count = ARRAY_SIZE(arizona_48k_rates),
1076 .list = arizona_48k_rates,
1079 static const int arizona_44k1_bclk_rates[] = {
1101 static const unsigned int arizona_44k1_rates[] = {
1111 static const struct snd_pcm_hw_constraint_list arizona_44k1_constraint = {
1112 .count = ARRAY_SIZE(arizona_44k1_rates),
1113 .list = arizona_44k1_rates,
1116 static int arizona_sr_vals[] = {
1143 static int arizona_startup(struct snd_pcm_substream *substream,
1144 struct snd_soc_dai *dai)
1146 struct snd_soc_codec *codec = dai->codec;
1147 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
1148 struct arizona_dai_priv *dai_priv = &priv->dai[dai->id - 1];
1149 const struct snd_pcm_hw_constraint_list *constraint;
1150 unsigned int base_rate;
1152 switch (dai_priv->clk) {
1153 case ARIZONA_CLK_SYSCLK:
1154 base_rate = priv->sysclk;
1156 case ARIZONA_CLK_ASYNCCLK:
1157 base_rate = priv->asyncclk;
1166 if (base_rate % 8000)
1167 constraint = &arizona_44k1_constraint;
1169 constraint = &arizona_48k_constraint;
1171 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1172 SNDRV_PCM_HW_PARAM_RATE,
1176 static void arizona_wm5102_set_dac_comp(struct snd_soc_codec *codec,
1179 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
1180 struct arizona *arizona = priv->arizona;
1181 struct reg_default dac_comp[] = {
1183 { ARIZONA_DAC_COMP_1, 0 },
1184 { ARIZONA_DAC_COMP_2, 0 },
1188 mutex_lock(&arizona->dac_comp_lock);
1190 dac_comp[1].def = arizona->dac_comp_coeff;
1192 dac_comp[2].def = arizona->dac_comp_enabled;
1194 mutex_unlock(&arizona->dac_comp_lock);
1196 regmap_multi_reg_write(arizona->regmap,
1198 ARRAY_SIZE(dac_comp));
1201 static int arizona_hw_params_rate(struct snd_pcm_substream *substream,
1202 struct snd_pcm_hw_params *params,
1203 struct snd_soc_dai *dai)
1205 struct snd_soc_codec *codec = dai->codec;
1206 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
1207 struct arizona_dai_priv *dai_priv = &priv->dai[dai->id - 1];
1208 int base = dai->driver->base;
1212 * We will need to be more flexible than this in future,
1213 * currently we use a single sample rate for SYSCLK.
1215 for (i = 0; i < ARRAY_SIZE(arizona_sr_vals); i++)
1216 if (arizona_sr_vals[i] == params_rate(params))
1218 if (i == ARRAY_SIZE(arizona_sr_vals)) {
1219 arizona_aif_err(dai, "Unsupported sample rate %dHz\n",
1220 params_rate(params));
1225 switch (dai_priv->clk) {
1226 case ARIZONA_CLK_SYSCLK:
1227 switch (priv->arizona->type) {
1229 arizona_wm5102_set_dac_comp(codec,
1230 params_rate(params));
1236 snd_soc_update_bits(codec, ARIZONA_SAMPLE_RATE_1,
1237 ARIZONA_SAMPLE_RATE_1_MASK, sr_val);
1239 snd_soc_update_bits(codec, base + ARIZONA_AIF_RATE_CTRL,
1240 ARIZONA_AIF1_RATE_MASK, 0);
1242 case ARIZONA_CLK_ASYNCCLK:
1243 snd_soc_update_bits(codec, ARIZONA_ASYNC_SAMPLE_RATE_1,
1244 ARIZONA_ASYNC_SAMPLE_RATE_1_MASK, sr_val);
1246 snd_soc_update_bits(codec, base + ARIZONA_AIF_RATE_CTRL,
1247 ARIZONA_AIF1_RATE_MASK,
1248 8 << ARIZONA_AIF1_RATE_SHIFT);
1251 arizona_aif_err(dai, "Invalid clock %d\n", dai_priv->clk);
1258 static bool arizona_aif_cfg_changed(struct snd_soc_codec *codec,
1259 int base, int bclk, int lrclk, int frame)
1263 val = snd_soc_read(codec, base + ARIZONA_AIF_BCLK_CTRL);
1264 if (bclk != (val & ARIZONA_AIF1_BCLK_FREQ_MASK))
1267 val = snd_soc_read(codec, base + ARIZONA_AIF_TX_BCLK_RATE);
1268 if (lrclk != (val & ARIZONA_AIF1TX_BCPF_MASK))
1271 val = snd_soc_read(codec, base + ARIZONA_AIF_FRAME_CTRL_1);
1272 if (frame != (val & (ARIZONA_AIF1TX_WL_MASK |
1273 ARIZONA_AIF1TX_SLOT_LEN_MASK)))
1279 static int arizona_hw_params(struct snd_pcm_substream *substream,
1280 struct snd_pcm_hw_params *params,
1281 struct snd_soc_dai *dai)
1283 struct snd_soc_codec *codec = dai->codec;
1284 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
1285 struct arizona *arizona = priv->arizona;
1286 int base = dai->driver->base;
1289 int channels = params_channels(params);
1290 int chan_limit = arizona->pdata.max_channels_clocked[dai->id - 1];
1291 int tdm_width = arizona->tdm_width[dai->id - 1];
1292 int tdm_slots = arizona->tdm_slots[dai->id - 1];
1293 int bclk, lrclk, wl, frame, bclk_target;
1295 unsigned int aif_tx_state, aif_rx_state;
1297 if (params_rate(params) % 8000)
1298 rates = &arizona_44k1_bclk_rates[0];
1300 rates = &arizona_48k_bclk_rates[0];
1302 wl = snd_pcm_format_width(params_format(params));
1305 arizona_aif_dbg(dai, "Configuring for %d %d bit TDM slots\n",
1306 tdm_slots, tdm_width);
1307 bclk_target = tdm_slots * tdm_width * params_rate(params);
1308 channels = tdm_slots;
1310 bclk_target = snd_soc_params_to_bclk(params);
1314 if (chan_limit && chan_limit < channels) {
1315 arizona_aif_dbg(dai, "Limiting to %d channels\n", chan_limit);
1316 bclk_target /= channels;
1317 bclk_target *= chan_limit;
1320 /* Force multiple of 2 channels for I2S mode */
1321 val = snd_soc_read(codec, base + ARIZONA_AIF_FORMAT);
1322 val &= ARIZONA_AIF1_FMT_MASK;
1323 if ((channels & 1) && (val == ARIZONA_FMT_I2S_MODE)) {
1324 arizona_aif_dbg(dai, "Forcing stereo mode\n");
1325 bclk_target /= channels;
1326 bclk_target *= channels + 1;
1329 for (i = 0; i < ARRAY_SIZE(arizona_44k1_bclk_rates); i++) {
1330 if (rates[i] >= bclk_target &&
1331 rates[i] % params_rate(params) == 0) {
1336 if (i == ARRAY_SIZE(arizona_44k1_bclk_rates)) {
1337 arizona_aif_err(dai, "Unsupported sample rate %dHz\n",
1338 params_rate(params));
1342 lrclk = rates[bclk] / params_rate(params);
1344 arizona_aif_dbg(dai, "BCLK %dHz LRCLK %dHz\n",
1345 rates[bclk], rates[bclk] / lrclk);
1347 frame = wl << ARIZONA_AIF1TX_WL_SHIFT | tdm_width;
1349 reconfig = arizona_aif_cfg_changed(codec, base, bclk, lrclk, frame);
1352 /* Save AIF TX/RX state */
1353 aif_tx_state = snd_soc_read(codec,
1354 base + ARIZONA_AIF_TX_ENABLES);
1355 aif_rx_state = snd_soc_read(codec,
1356 base + ARIZONA_AIF_RX_ENABLES);
1357 /* Disable AIF TX/RX before reconfiguring it */
1358 regmap_update_bits_async(arizona->regmap,
1359 base + ARIZONA_AIF_TX_ENABLES, 0xff, 0x0);
1360 regmap_update_bits(arizona->regmap,
1361 base + ARIZONA_AIF_RX_ENABLES, 0xff, 0x0);
1364 ret = arizona_hw_params_rate(substream, params, dai);
1369 regmap_update_bits_async(arizona->regmap,
1370 base + ARIZONA_AIF_BCLK_CTRL,
1371 ARIZONA_AIF1_BCLK_FREQ_MASK, bclk);
1372 regmap_update_bits_async(arizona->regmap,
1373 base + ARIZONA_AIF_TX_BCLK_RATE,
1374 ARIZONA_AIF1TX_BCPF_MASK, lrclk);
1375 regmap_update_bits_async(arizona->regmap,
1376 base + ARIZONA_AIF_RX_BCLK_RATE,
1377 ARIZONA_AIF1RX_BCPF_MASK, lrclk);
1378 regmap_update_bits_async(arizona->regmap,
1379 base + ARIZONA_AIF_FRAME_CTRL_1,
1380 ARIZONA_AIF1TX_WL_MASK |
1381 ARIZONA_AIF1TX_SLOT_LEN_MASK, frame);
1382 regmap_update_bits(arizona->regmap,
1383 base + ARIZONA_AIF_FRAME_CTRL_2,
1384 ARIZONA_AIF1RX_WL_MASK |
1385 ARIZONA_AIF1RX_SLOT_LEN_MASK, frame);
1390 /* Restore AIF TX/RX state */
1391 regmap_update_bits_async(arizona->regmap,
1392 base + ARIZONA_AIF_TX_ENABLES,
1393 0xff, aif_tx_state);
1394 regmap_update_bits(arizona->regmap,
1395 base + ARIZONA_AIF_RX_ENABLES,
1396 0xff, aif_rx_state);
1401 static const char *arizona_dai_clk_str(int clk_id)
1404 case ARIZONA_CLK_SYSCLK:
1406 case ARIZONA_CLK_ASYNCCLK:
1409 return "Unknown clock";
1413 static int arizona_dai_set_sysclk(struct snd_soc_dai *dai,
1414 int clk_id, unsigned int freq, int dir)
1416 struct snd_soc_codec *codec = dai->codec;
1417 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
1418 struct arizona_dai_priv *dai_priv = &priv->dai[dai->id - 1];
1419 struct snd_soc_dapm_route routes[2];
1422 case ARIZONA_CLK_SYSCLK:
1423 case ARIZONA_CLK_ASYNCCLK:
1429 if (clk_id == dai_priv->clk)
1433 dev_err(codec->dev, "Can't change clock on active DAI %d\n",
1438 dev_dbg(codec->dev, "Setting AIF%d to %s\n", dai->id + 1,
1439 arizona_dai_clk_str(clk_id));
1441 memset(&routes, 0, sizeof(routes));
1442 routes[0].sink = dai->driver->capture.stream_name;
1443 routes[1].sink = dai->driver->playback.stream_name;
1445 routes[0].source = arizona_dai_clk_str(dai_priv->clk);
1446 routes[1].source = arizona_dai_clk_str(dai_priv->clk);
1447 snd_soc_dapm_del_routes(&codec->dapm, routes, ARRAY_SIZE(routes));
1449 routes[0].source = arizona_dai_clk_str(clk_id);
1450 routes[1].source = arizona_dai_clk_str(clk_id);
1451 snd_soc_dapm_add_routes(&codec->dapm, routes, ARRAY_SIZE(routes));
1453 dai_priv->clk = clk_id;
1455 return snd_soc_dapm_sync(&codec->dapm);
1458 static int arizona_set_tristate(struct snd_soc_dai *dai, int tristate)
1460 struct snd_soc_codec *codec = dai->codec;
1461 int base = dai->driver->base;
1465 reg = ARIZONA_AIF1_TRI;
1469 return snd_soc_update_bits(codec, base + ARIZONA_AIF_RATE_CTRL,
1470 ARIZONA_AIF1_TRI, reg);
1473 static void arizona_set_channels_to_mask(struct snd_soc_dai *dai,
1475 int channels, unsigned int mask)
1477 struct snd_soc_codec *codec = dai->codec;
1478 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
1479 struct arizona *arizona = priv->arizona;
1482 for (i = 0; i < channels; ++i) {
1483 slot = ffs(mask) - 1;
1487 regmap_write(arizona->regmap, base + i, slot);
1489 mask &= ~(1 << slot);
1493 arizona_aif_warn(dai, "Too many channels in TDM mask\n");
1496 static int arizona_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
1497 unsigned int rx_mask, int slots, int slot_width)
1499 struct snd_soc_codec *codec = dai->codec;
1500 struct arizona_priv *priv = snd_soc_codec_get_drvdata(codec);
1501 struct arizona *arizona = priv->arizona;
1502 int base = dai->driver->base;
1503 int rx_max_chan = dai->driver->playback.channels_max;
1504 int tx_max_chan = dai->driver->capture.channels_max;
1506 /* Only support TDM for the physical AIFs */
1507 if (dai->id > ARIZONA_MAX_AIF)
1511 tx_mask = (1 << tx_max_chan) - 1;
1512 rx_mask = (1 << rx_max_chan) - 1;
1515 arizona_set_channels_to_mask(dai, base + ARIZONA_AIF_FRAME_CTRL_3,
1516 tx_max_chan, tx_mask);
1517 arizona_set_channels_to_mask(dai, base + ARIZONA_AIF_FRAME_CTRL_11,
1518 rx_max_chan, rx_mask);
1520 arizona->tdm_width[dai->id - 1] = slot_width;
1521 arizona->tdm_slots[dai->id - 1] = slots;
1526 const struct snd_soc_dai_ops arizona_dai_ops = {
1527 .startup = arizona_startup,
1528 .set_fmt = arizona_set_fmt,
1529 .set_tdm_slot = arizona_set_tdm_slot,
1530 .hw_params = arizona_hw_params,
1531 .set_sysclk = arizona_dai_set_sysclk,
1532 .set_tristate = arizona_set_tristate,
1534 EXPORT_SYMBOL_GPL(arizona_dai_ops);
1536 const struct snd_soc_dai_ops arizona_simple_dai_ops = {
1537 .startup = arizona_startup,
1538 .hw_params = arizona_hw_params_rate,
1539 .set_sysclk = arizona_dai_set_sysclk,
1541 EXPORT_SYMBOL_GPL(arizona_simple_dai_ops);
1543 int arizona_init_dai(struct arizona_priv *priv, int id)
1545 struct arizona_dai_priv *dai_priv = &priv->dai[id];
1547 dai_priv->clk = ARIZONA_CLK_SYSCLK;
1551 EXPORT_SYMBOL_GPL(arizona_init_dai);
1553 static irqreturn_t arizona_fll_clock_ok(int irq, void *data)
1555 struct arizona_fll *fll = data;
1557 arizona_fll_dbg(fll, "clock OK\n");
1570 { 0, 64000, 4, 16 },
1571 { 64000, 128000, 3, 8 },
1572 { 128000, 256000, 2, 4 },
1573 { 256000, 1000000, 1, 2 },
1574 { 1000000, 13500000, 0, 1 },
1583 { 256000, 1000000, 2 },
1584 { 1000000, 13500000, 4 },
1587 struct arizona_fll_cfg {
1597 static int arizona_validate_fll(struct arizona_fll *fll,
1601 unsigned int Fvco_min;
1603 if (fll->fout && Fout != fll->fout) {
1604 arizona_fll_err(fll,
1605 "Can't change output on active FLL\n");
1609 if (Fref / ARIZONA_FLL_MAX_REFDIV > ARIZONA_FLL_MAX_FREF) {
1610 arizona_fll_err(fll,
1611 "Can't scale %dMHz in to <=13.5MHz\n",
1616 Fvco_min = ARIZONA_FLL_MIN_FVCO * fll->vco_mult;
1617 if (Fout * ARIZONA_FLL_MAX_OUTDIV < Fvco_min) {
1618 arizona_fll_err(fll, "No FLL_OUTDIV for Fout=%uHz\n",
1626 static int arizona_find_fratio(unsigned int Fref, int *fratio)
1630 /* Find an appropriate FLL_FRATIO */
1631 for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
1632 if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
1634 *fratio = fll_fratios[i].fratio;
1635 return fll_fratios[i].ratio;
1642 static int arizona_calc_fratio(struct arizona_fll *fll,
1643 struct arizona_fll_cfg *cfg,
1644 unsigned int target,
1645 unsigned int Fref, bool sync)
1647 int init_ratio, ratio;
1650 /* Fref must be <=13.5MHz, find initial refdiv */
1653 while (Fref > ARIZONA_FLL_MAX_FREF) {
1658 if (div > ARIZONA_FLL_MAX_REFDIV)
1662 /* Find an appropriate FLL_FRATIO */
1663 init_ratio = arizona_find_fratio(Fref, &cfg->fratio);
1664 if (init_ratio < 0) {
1665 arizona_fll_err(fll, "Unable to find FRATIO for Fref=%uHz\n",
1670 switch (fll->arizona->type) {
1672 if (fll->arizona->rev < 3 || sync)
1679 cfg->fratio = init_ratio - 1;
1681 /* Adjust FRATIO/refdiv to avoid integer mode if possible */
1682 refdiv = cfg->refdiv;
1684 while (div <= ARIZONA_FLL_MAX_REFDIV) {
1685 for (ratio = init_ratio; ratio <= ARIZONA_FLL_MAX_FRATIO;
1687 if ((ARIZONA_FLL_VCO_CORNER / 2) /
1688 (fll->vco_mult * ratio) < Fref)
1691 if (target % (ratio * Fref)) {
1692 cfg->refdiv = refdiv;
1693 cfg->fratio = ratio - 1;
1698 for (ratio = init_ratio - 1; ratio > 0; ratio--) {
1699 if (target % (ratio * Fref)) {
1700 cfg->refdiv = refdiv;
1701 cfg->fratio = ratio - 1;
1709 init_ratio = arizona_find_fratio(Fref, NULL);
1712 arizona_fll_warn(fll, "Falling back to integer mode operation\n");
1713 return cfg->fratio + 1;
1716 static int arizona_calc_fll(struct arizona_fll *fll,
1717 struct arizona_fll_cfg *cfg,
1718 unsigned int Fref, bool sync)
1720 unsigned int target, div, gcd_fll;
1723 arizona_fll_dbg(fll, "Fref=%u Fout=%u\n", Fref, fll->fout);
1725 /* Fvco should be over the targt; don't check the upper bound */
1726 div = ARIZONA_FLL_MIN_OUTDIV;
1727 while (fll->fout * div < ARIZONA_FLL_MIN_FVCO * fll->vco_mult) {
1729 if (div > ARIZONA_FLL_MAX_OUTDIV)
1732 target = fll->fout * div / fll->vco_mult;
1735 arizona_fll_dbg(fll, "Fvco=%dHz\n", target);
1737 /* Find an appropriate FLL_FRATIO and refdiv */
1738 ratio = arizona_calc_fratio(fll, cfg, target, Fref, sync);
1742 /* Apply the division for our remaining calculations */
1743 Fref = Fref / (1 << cfg->refdiv);
1745 cfg->n = target / (ratio * Fref);
1747 if (target % (ratio * Fref)) {
1748 gcd_fll = gcd(target, ratio * Fref);
1749 arizona_fll_dbg(fll, "GCD=%u\n", gcd_fll);
1751 cfg->theta = (target - (cfg->n * ratio * Fref))
1753 cfg->lambda = (ratio * Fref) / gcd_fll;
1759 /* Round down to 16bit range with cost of accuracy lost.
1760 * Denominator must be bigger than numerator so we only
1763 while (cfg->lambda >= (1 << 16)) {
1768 for (i = 0; i < ARRAY_SIZE(fll_gains); i++) {
1769 if (fll_gains[i].min <= Fref && Fref <= fll_gains[i].max) {
1770 cfg->gain = fll_gains[i].gain;
1774 if (i == ARRAY_SIZE(fll_gains)) {
1775 arizona_fll_err(fll, "Unable to find gain for Fref=%uHz\n",
1780 arizona_fll_dbg(fll, "N=%x THETA=%x LAMBDA=%x\n",
1781 cfg->n, cfg->theta, cfg->lambda);
1782 arizona_fll_dbg(fll, "FRATIO=%x(%d) OUTDIV=%x REFCLK_DIV=%x\n",
1783 cfg->fratio, cfg->fratio, cfg->outdiv, cfg->refdiv);
1784 arizona_fll_dbg(fll, "GAIN=%d\n", cfg->gain);
1790 static void arizona_apply_fll(struct arizona *arizona, unsigned int base,
1791 struct arizona_fll_cfg *cfg, int source,
1794 regmap_update_bits_async(arizona->regmap, base + 3,
1795 ARIZONA_FLL1_THETA_MASK, cfg->theta);
1796 regmap_update_bits_async(arizona->regmap, base + 4,
1797 ARIZONA_FLL1_LAMBDA_MASK, cfg->lambda);
1798 regmap_update_bits_async(arizona->regmap, base + 5,
1799 ARIZONA_FLL1_FRATIO_MASK,
1800 cfg->fratio << ARIZONA_FLL1_FRATIO_SHIFT);
1801 regmap_update_bits_async(arizona->regmap, base + 6,
1802 ARIZONA_FLL1_CLK_REF_DIV_MASK |
1803 ARIZONA_FLL1_CLK_REF_SRC_MASK,
1804 cfg->refdiv << ARIZONA_FLL1_CLK_REF_DIV_SHIFT |
1805 source << ARIZONA_FLL1_CLK_REF_SRC_SHIFT);
1808 regmap_update_bits(arizona->regmap, base + 0x7,
1809 ARIZONA_FLL1_GAIN_MASK,
1810 cfg->gain << ARIZONA_FLL1_GAIN_SHIFT);
1812 regmap_update_bits(arizona->regmap, base + 0x5,
1813 ARIZONA_FLL1_OUTDIV_MASK,
1814 cfg->outdiv << ARIZONA_FLL1_OUTDIV_SHIFT);
1815 regmap_update_bits(arizona->regmap, base + 0x9,
1816 ARIZONA_FLL1_GAIN_MASK,
1817 cfg->gain << ARIZONA_FLL1_GAIN_SHIFT);
1820 regmap_update_bits_async(arizona->regmap, base + 2,
1821 ARIZONA_FLL1_CTRL_UPD | ARIZONA_FLL1_N_MASK,
1822 ARIZONA_FLL1_CTRL_UPD | cfg->n);
1825 static int arizona_is_enabled_fll(struct arizona_fll *fll)
1827 struct arizona *arizona = fll->arizona;
1831 ret = regmap_read(arizona->regmap, fll->base + 1, ®);
1833 arizona_fll_err(fll, "Failed to read current state: %d\n",
1838 return reg & ARIZONA_FLL1_ENA;
1841 static int arizona_enable_fll(struct arizona_fll *fll)
1843 struct arizona *arizona = fll->arizona;
1845 bool use_sync = false;
1846 int already_enabled = arizona_is_enabled_fll(fll);
1847 struct arizona_fll_cfg cfg;
1849 if (already_enabled < 0)
1850 return already_enabled;
1852 if (already_enabled) {
1853 /* Facilitate smooth refclk across the transition */
1854 regmap_update_bits_async(fll->arizona->regmap, fll->base + 0x7,
1855 ARIZONA_FLL1_GAIN_MASK, 0);
1856 regmap_update_bits_async(fll->arizona->regmap, fll->base + 1,
1857 ARIZONA_FLL1_FREERUN,
1858 ARIZONA_FLL1_FREERUN);
1862 * If we have both REFCLK and SYNCCLK then enable both,
1863 * otherwise apply the SYNCCLK settings to REFCLK.
1865 if (fll->ref_src >= 0 && fll->ref_freq &&
1866 fll->ref_src != fll->sync_src) {
1867 arizona_calc_fll(fll, &cfg, fll->ref_freq, false);
1869 arizona_apply_fll(arizona, fll->base, &cfg, fll->ref_src,
1871 if (fll->sync_src >= 0) {
1872 arizona_calc_fll(fll, &cfg, fll->sync_freq, true);
1874 arizona_apply_fll(arizona, fll->base + 0x10, &cfg,
1875 fll->sync_src, true);
1878 } else if (fll->sync_src >= 0) {
1879 arizona_calc_fll(fll, &cfg, fll->sync_freq, false);
1881 arizona_apply_fll(arizona, fll->base, &cfg,
1882 fll->sync_src, false);
1884 regmap_update_bits_async(arizona->regmap, fll->base + 0x11,
1885 ARIZONA_FLL1_SYNC_ENA, 0);
1887 arizona_fll_err(fll, "No clocks provided\n");
1892 * Increase the bandwidth if we're not using a low frequency
1895 if (use_sync && fll->sync_freq > 100000)
1896 regmap_update_bits_async(arizona->regmap, fll->base + 0x17,
1897 ARIZONA_FLL1_SYNC_BW, 0);
1899 regmap_update_bits_async(arizona->regmap, fll->base + 0x17,
1900 ARIZONA_FLL1_SYNC_BW,
1901 ARIZONA_FLL1_SYNC_BW);
1903 if (!already_enabled)
1904 pm_runtime_get(arizona->dev);
1906 /* Clear any pending completions */
1907 try_wait_for_completion(&fll->ok);
1909 regmap_update_bits_async(arizona->regmap, fll->base + 1,
1910 ARIZONA_FLL1_ENA, ARIZONA_FLL1_ENA);
1912 regmap_update_bits_async(arizona->regmap, fll->base + 0x11,
1913 ARIZONA_FLL1_SYNC_ENA,
1914 ARIZONA_FLL1_SYNC_ENA);
1916 if (already_enabled)
1917 regmap_update_bits_async(arizona->regmap, fll->base + 1,
1918 ARIZONA_FLL1_FREERUN, 0);
1920 ret = wait_for_completion_timeout(&fll->ok,
1921 msecs_to_jiffies(250));
1923 arizona_fll_warn(fll, "Timed out waiting for lock\n");
1928 static void arizona_disable_fll(struct arizona_fll *fll)
1930 struct arizona *arizona = fll->arizona;
1933 regmap_update_bits_async(arizona->regmap, fll->base + 1,
1934 ARIZONA_FLL1_FREERUN, ARIZONA_FLL1_FREERUN);
1935 regmap_update_bits_check(arizona->regmap, fll->base + 1,
1936 ARIZONA_FLL1_ENA, 0, &change);
1937 regmap_update_bits(arizona->regmap, fll->base + 0x11,
1938 ARIZONA_FLL1_SYNC_ENA, 0);
1939 regmap_update_bits_async(arizona->regmap, fll->base + 1,
1940 ARIZONA_FLL1_FREERUN, 0);
1943 pm_runtime_put_autosuspend(arizona->dev);
1946 int arizona_set_fll_refclk(struct arizona_fll *fll, int source,
1947 unsigned int Fref, unsigned int Fout)
1951 if (fll->ref_src == source && fll->ref_freq == Fref)
1954 if (fll->fout && Fref > 0) {
1955 ret = arizona_validate_fll(fll, Fref, fll->fout);
1960 fll->ref_src = source;
1961 fll->ref_freq = Fref;
1963 if (fll->fout && Fref > 0) {
1964 ret = arizona_enable_fll(fll);
1969 EXPORT_SYMBOL_GPL(arizona_set_fll_refclk);
1971 int arizona_set_fll(struct arizona_fll *fll, int source,
1972 unsigned int Fref, unsigned int Fout)
1976 if (fll->sync_src == source &&
1977 fll->sync_freq == Fref && fll->fout == Fout)
1981 if (fll->ref_src >= 0) {
1982 ret = arizona_validate_fll(fll, fll->ref_freq, Fout);
1987 ret = arizona_validate_fll(fll, Fref, Fout);
1992 fll->sync_src = source;
1993 fll->sync_freq = Fref;
1997 ret = arizona_enable_fll(fll);
1999 arizona_disable_fll(fll);
2003 EXPORT_SYMBOL_GPL(arizona_set_fll);
2005 int arizona_init_fll(struct arizona *arizona, int id, int base, int lock_irq,
2006 int ok_irq, struct arizona_fll *fll)
2011 init_completion(&fll->ok);
2015 fll->arizona = arizona;
2016 fll->sync_src = ARIZONA_FLL_SRC_NONE;
2018 /* Configure default refclk to 32kHz if we have one */
2019 regmap_read(arizona->regmap, ARIZONA_CLOCK_32K_1, &val);
2020 switch (val & ARIZONA_CLK_32K_SRC_MASK) {
2021 case ARIZONA_CLK_SRC_MCLK1:
2022 case ARIZONA_CLK_SRC_MCLK2:
2023 fll->ref_src = val & ARIZONA_CLK_32K_SRC_MASK;
2026 fll->ref_src = ARIZONA_FLL_SRC_NONE;
2028 fll->ref_freq = 32768;
2030 snprintf(fll->lock_name, sizeof(fll->lock_name), "FLL%d lock", id);
2031 snprintf(fll->clock_ok_name, sizeof(fll->clock_ok_name),
2032 "FLL%d clock OK", id);
2034 ret = arizona_request_irq(arizona, ok_irq, fll->clock_ok_name,
2035 arizona_fll_clock_ok, fll);
2037 dev_err(arizona->dev, "Failed to get FLL%d clock OK IRQ: %d\n",
2041 regmap_update_bits(arizona->regmap, fll->base + 1,
2042 ARIZONA_FLL1_FREERUN, 0);
2046 EXPORT_SYMBOL_GPL(arizona_init_fll);
2049 * arizona_set_output_mode - Set the mode of the specified output
2051 * @codec: Device to configure
2052 * @output: Output number
2053 * @diff: True to set the output to differential mode
2055 * Some systems use external analogue switches to connect more
2056 * analogue devices to the CODEC than are supported by the device. In
2057 * some systems this requires changing the switched output from single
2058 * ended to differential mode dynamically at runtime, an operation
2059 * supported using this function.
2061 * Most systems have a single static configuration and should use
2062 * platform data instead.
2064 int arizona_set_output_mode(struct snd_soc_codec *codec, int output, bool diff)
2066 unsigned int reg, val;
2068 if (output < 1 || output > 6)
2071 reg = ARIZONA_OUTPUT_PATH_CONFIG_1L + (output - 1) * 8;
2074 val = ARIZONA_OUT1_MONO;
2078 return snd_soc_update_bits(codec, reg, ARIZONA_OUT1_MONO, val);
2080 EXPORT_SYMBOL_GPL(arizona_set_output_mode);
2082 MODULE_DESCRIPTION("ASoC Wolfson Arizona class device support");
2083 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
2084 MODULE_LICENSE("GPL");