Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * wm8974.c -- WM8974 ALSA Soc Audio driver |
| 3 | * |
Mark Brown | 8b83a19 | 2009-06-30 19:37:02 +0100 | [diff] [blame] | 4 | * Copyright 2006-2009 Wolfson Microelectronics PLC. |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 5 | * |
Mark Brown | 9a185b9 | 2011-10-06 11:10:01 +0100 | [diff] [blame] | 6 | * Author: Liam Girdwood <Liam.Girdwood@wolfsonmicro.com> |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 7 | * |
| 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. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/moduleparam.h> |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/pm.h> |
| 19 | #include <linux/i2c.h> |
Mark Brown | e40e0b5 | 2013-11-08 14:01:39 +0000 | [diff] [blame] | 20 | #include <linux/regmap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 22 | #include <sound/core.h> |
| 23 | #include <sound/pcm.h> |
| 24 | #include <sound/pcm_params.h> |
| 25 | #include <sound/soc.h> |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 26 | #include <sound/initval.h> |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 27 | #include <sound/tlv.h> |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 28 | |
| 29 | #include "wm8974.h" |
| 30 | |
Mans Rullgard | 51b2bb3 | 2016-01-25 12:36:43 +0000 | [diff] [blame] | 31 | struct wm8974_priv { |
| 32 | unsigned int mclk; |
| 33 | unsigned int fs; |
| 34 | }; |
| 35 | |
Mark Brown | e40e0b5 | 2013-11-08 14:01:39 +0000 | [diff] [blame] | 36 | static const struct reg_default wm8974_reg_defaults[] = { |
| 37 | { 0, 0x0000 }, { 1, 0x0000 }, { 2, 0x0000 }, { 3, 0x0000 }, |
| 38 | { 4, 0x0050 }, { 5, 0x0000 }, { 6, 0x0140 }, { 7, 0x0000 }, |
| 39 | { 8, 0x0000 }, { 9, 0x0000 }, { 10, 0x0000 }, { 11, 0x00ff }, |
| 40 | { 12, 0x0000 }, { 13, 0x0000 }, { 14, 0x0100 }, { 15, 0x00ff }, |
| 41 | { 16, 0x0000 }, { 17, 0x0000 }, { 18, 0x012c }, { 19, 0x002c }, |
| 42 | { 20, 0x002c }, { 21, 0x002c }, { 22, 0x002c }, { 23, 0x0000 }, |
| 43 | { 24, 0x0032 }, { 25, 0x0000 }, { 26, 0x0000 }, { 27, 0x0000 }, |
| 44 | { 28, 0x0000 }, { 29, 0x0000 }, { 30, 0x0000 }, { 31, 0x0000 }, |
| 45 | { 32, 0x0038 }, { 33, 0x000b }, { 34, 0x0032 }, { 35, 0x0000 }, |
| 46 | { 36, 0x0008 }, { 37, 0x000c }, { 38, 0x0093 }, { 39, 0x00e9 }, |
| 47 | { 40, 0x0000 }, { 41, 0x0000 }, { 42, 0x0000 }, { 43, 0x0000 }, |
| 48 | { 44, 0x0003 }, { 45, 0x0010 }, { 46, 0x0000 }, { 47, 0x0000 }, |
| 49 | { 48, 0x0000 }, { 49, 0x0002 }, { 50, 0x0000 }, { 51, 0x0000 }, |
| 50 | { 52, 0x0000 }, { 53, 0x0000 }, { 54, 0x0039 }, { 55, 0x0000 }, |
| 51 | { 56, 0x0000 }, |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 54 | #define WM8974_POWER1_BIASEN 0x08 |
Guennadi Liakhovetski | 48c03ce | 2009-12-17 14:51:35 +0100 | [diff] [blame] | 55 | #define WM8974_POWER1_BUFIOEN 0x04 |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 56 | |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 57 | #define wm8974_reset(c) snd_soc_write(c, WM8974_RESET, 0) |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 58 | |
| 59 | static const char *wm8974_companding[] = {"Off", "NC", "u-law", "A-law" }; |
| 60 | static const char *wm8974_deemp[] = {"None", "32kHz", "44.1kHz", "48kHz" }; |
| 61 | static const char *wm8974_eqmode[] = {"Capture", "Playback" }; |
| 62 | static const char *wm8974_bw[] = {"Narrow", "Wide" }; |
| 63 | static const char *wm8974_eq1[] = {"80Hz", "105Hz", "135Hz", "175Hz" }; |
| 64 | static const char *wm8974_eq2[] = {"230Hz", "300Hz", "385Hz", "500Hz" }; |
| 65 | static const char *wm8974_eq3[] = {"650Hz", "850Hz", "1.1kHz", "1.4kHz" }; |
| 66 | static const char *wm8974_eq4[] = {"1.8kHz", "2.4kHz", "3.2kHz", "4.1kHz" }; |
| 67 | static const char *wm8974_eq5[] = {"5.3kHz", "6.9kHz", "9kHz", "11.7kHz" }; |
| 68 | static const char *wm8974_alc[] = {"ALC", "Limiter" }; |
| 69 | |
| 70 | static const struct soc_enum wm8974_enum[] = { |
| 71 | SOC_ENUM_SINGLE(WM8974_COMP, 1, 4, wm8974_companding), /* adc */ |
| 72 | SOC_ENUM_SINGLE(WM8974_COMP, 3, 4, wm8974_companding), /* dac */ |
| 73 | SOC_ENUM_SINGLE(WM8974_DAC, 4, 4, wm8974_deemp), |
| 74 | SOC_ENUM_SINGLE(WM8974_EQ1, 8, 2, wm8974_eqmode), |
| 75 | |
| 76 | SOC_ENUM_SINGLE(WM8974_EQ1, 5, 4, wm8974_eq1), |
| 77 | SOC_ENUM_SINGLE(WM8974_EQ2, 8, 2, wm8974_bw), |
| 78 | SOC_ENUM_SINGLE(WM8974_EQ2, 5, 4, wm8974_eq2), |
| 79 | SOC_ENUM_SINGLE(WM8974_EQ3, 8, 2, wm8974_bw), |
| 80 | |
| 81 | SOC_ENUM_SINGLE(WM8974_EQ3, 5, 4, wm8974_eq3), |
| 82 | SOC_ENUM_SINGLE(WM8974_EQ4, 8, 2, wm8974_bw), |
| 83 | SOC_ENUM_SINGLE(WM8974_EQ4, 5, 4, wm8974_eq4), |
| 84 | SOC_ENUM_SINGLE(WM8974_EQ5, 8, 2, wm8974_bw), |
| 85 | |
| 86 | SOC_ENUM_SINGLE(WM8974_EQ5, 5, 4, wm8974_eq5), |
| 87 | SOC_ENUM_SINGLE(WM8974_ALC3, 8, 2, wm8974_alc), |
| 88 | }; |
| 89 | |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 90 | static const char *wm8974_auxmode_text[] = { "Buffer", "Mixer" }; |
| 91 | |
Takashi Iwai | de461bd | 2014-02-18 10:43:31 +0100 | [diff] [blame] | 92 | static SOC_ENUM_SINGLE_DECL(wm8974_auxmode, |
| 93 | WM8974_INPUT, 3, wm8974_auxmode_text); |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 94 | |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 95 | static const DECLARE_TLV_DB_SCALE(digital_tlv, -12750, 50, 1); |
| 96 | static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0); |
| 97 | static const DECLARE_TLV_DB_SCALE(inpga_tlv, -1200, 75, 0); |
| 98 | static const DECLARE_TLV_DB_SCALE(spk_tlv, -5700, 100, 0); |
| 99 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 100 | static const struct snd_kcontrol_new wm8974_snd_controls[] = { |
| 101 | |
| 102 | SOC_SINGLE("Digital Loopback Switch", WM8974_COMP, 0, 1, 0), |
| 103 | |
| 104 | SOC_ENUM("DAC Companding", wm8974_enum[1]), |
| 105 | SOC_ENUM("ADC Companding", wm8974_enum[0]), |
| 106 | |
| 107 | SOC_ENUM("Playback De-emphasis", wm8974_enum[2]), |
| 108 | SOC_SINGLE("DAC Inversion Switch", WM8974_DAC, 0, 1, 0), |
| 109 | |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 110 | SOC_SINGLE_TLV("PCM Volume", WM8974_DACVOL, 0, 255, 0, digital_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 111 | |
| 112 | SOC_SINGLE("High Pass Filter Switch", WM8974_ADC, 8, 1, 0), |
| 113 | SOC_SINGLE("High Pass Cut Off", WM8974_ADC, 4, 7, 0), |
javier Martin | 25cbf46 | 2009-07-21 11:15:06 +0200 | [diff] [blame] | 114 | SOC_SINGLE("ADC Inversion Switch", WM8974_ADC, 0, 1, 0), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 115 | |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 116 | SOC_SINGLE_TLV("Capture Volume", WM8974_ADCVOL, 0, 255, 0, digital_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 117 | |
| 118 | SOC_ENUM("Equaliser Function", wm8974_enum[3]), |
| 119 | SOC_ENUM("EQ1 Cut Off", wm8974_enum[4]), |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 120 | SOC_SINGLE_TLV("EQ1 Volume", WM8974_EQ1, 0, 24, 1, eq_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 121 | |
Masanari Iida | c46d5c0 | 2012-11-02 23:25:30 +0900 | [diff] [blame] | 122 | SOC_ENUM("Equaliser EQ2 Bandwidth", wm8974_enum[5]), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 123 | SOC_ENUM("EQ2 Cut Off", wm8974_enum[6]), |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 124 | SOC_SINGLE_TLV("EQ2 Volume", WM8974_EQ2, 0, 24, 1, eq_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 125 | |
Masanari Iida | c46d5c0 | 2012-11-02 23:25:30 +0900 | [diff] [blame] | 126 | SOC_ENUM("Equaliser EQ3 Bandwidth", wm8974_enum[7]), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 127 | SOC_ENUM("EQ3 Cut Off", wm8974_enum[8]), |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 128 | SOC_SINGLE_TLV("EQ3 Volume", WM8974_EQ3, 0, 24, 1, eq_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 129 | |
Masanari Iida | c46d5c0 | 2012-11-02 23:25:30 +0900 | [diff] [blame] | 130 | SOC_ENUM("Equaliser EQ4 Bandwidth", wm8974_enum[9]), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 131 | SOC_ENUM("EQ4 Cut Off", wm8974_enum[10]), |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 132 | SOC_SINGLE_TLV("EQ4 Volume", WM8974_EQ4, 0, 24, 1, eq_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 133 | |
Masanari Iida | a895d57 | 2013-04-09 02:06:50 +0900 | [diff] [blame] | 134 | SOC_ENUM("Equaliser EQ5 Bandwidth", wm8974_enum[11]), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 135 | SOC_ENUM("EQ5 Cut Off", wm8974_enum[12]), |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 136 | SOC_SINGLE_TLV("EQ5 Volume", WM8974_EQ5, 0, 24, 1, eq_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 137 | |
| 138 | SOC_SINGLE("DAC Playback Limiter Switch", WM8974_DACLIM1, 8, 1, 0), |
| 139 | SOC_SINGLE("DAC Playback Limiter Decay", WM8974_DACLIM1, 4, 15, 0), |
| 140 | SOC_SINGLE("DAC Playback Limiter Attack", WM8974_DACLIM1, 0, 15, 0), |
| 141 | |
| 142 | SOC_SINGLE("DAC Playback Limiter Threshold", WM8974_DACLIM2, 4, 7, 0), |
| 143 | SOC_SINGLE("DAC Playback Limiter Boost", WM8974_DACLIM2, 0, 15, 0), |
| 144 | |
| 145 | SOC_SINGLE("ALC Enable Switch", WM8974_ALC1, 8, 1, 0), |
| 146 | SOC_SINGLE("ALC Capture Max Gain", WM8974_ALC1, 3, 7, 0), |
| 147 | SOC_SINGLE("ALC Capture Min Gain", WM8974_ALC1, 0, 7, 0), |
| 148 | |
| 149 | SOC_SINGLE("ALC Capture ZC Switch", WM8974_ALC2, 8, 1, 0), |
| 150 | SOC_SINGLE("ALC Capture Hold", WM8974_ALC2, 4, 7, 0), |
| 151 | SOC_SINGLE("ALC Capture Target", WM8974_ALC2, 0, 15, 0), |
| 152 | |
| 153 | SOC_ENUM("ALC Capture Mode", wm8974_enum[13]), |
| 154 | SOC_SINGLE("ALC Capture Decay", WM8974_ALC3, 4, 15, 0), |
| 155 | SOC_SINGLE("ALC Capture Attack", WM8974_ALC3, 0, 15, 0), |
| 156 | |
| 157 | SOC_SINGLE("ALC Capture Noise Gate Switch", WM8974_NGATE, 3, 1, 0), |
| 158 | SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8974_NGATE, 0, 7, 0), |
| 159 | |
| 160 | SOC_SINGLE("Capture PGA ZC Switch", WM8974_INPPGA, 7, 1, 0), |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 161 | SOC_SINGLE_TLV("Capture PGA Volume", WM8974_INPPGA, 0, 63, 0, inpga_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 162 | |
| 163 | SOC_SINGLE("Speaker Playback ZC Switch", WM8974_SPKVOL, 7, 1, 0), |
| 164 | SOC_SINGLE("Speaker Playback Switch", WM8974_SPKVOL, 6, 1, 1), |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 165 | SOC_SINGLE_TLV("Speaker Playback Volume", WM8974_SPKVOL, 0, 63, 0, spk_tlv), |
| 166 | |
| 167 | SOC_ENUM("Aux Mode", wm8974_auxmode), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 168 | |
| 169 | SOC_SINGLE("Capture Boost(+20dB)", WM8974_ADCBOOST, 8, 1, 0), |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 170 | SOC_SINGLE("Mono Playback Switch", WM8974_MONOMIX, 6, 1, 1), |
Guennadi Liakhovetski | b2c3e92 | 2010-01-29 15:31:06 +0100 | [diff] [blame] | 171 | |
| 172 | /* DAC / ADC oversampling */ |
| 173 | SOC_SINGLE("DAC 128x Oversampling Switch", WM8974_DAC, 8, 1, 0), |
| 174 | SOC_SINGLE("ADC 128x Oversampling Switch", WM8974_ADC, 8, 1, 0), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 175 | }; |
| 176 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 177 | /* Speaker Output Mixer */ |
| 178 | static const struct snd_kcontrol_new wm8974_speaker_mixer_controls[] = { |
| 179 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8974_SPKMIX, 1, 1, 0), |
| 180 | SOC_DAPM_SINGLE("Aux Playback Switch", WM8974_SPKMIX, 5, 1, 0), |
Mark Brown | 759512f | 2010-04-23 17:39:23 +0100 | [diff] [blame] | 181 | SOC_DAPM_SINGLE("PCM Playback Switch", WM8974_SPKMIX, 0, 1, 0), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | /* Mono Output Mixer */ |
| 185 | static const struct snd_kcontrol_new wm8974_mono_mixer_controls[] = { |
| 186 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8974_MONOMIX, 1, 1, 0), |
| 187 | SOC_DAPM_SINGLE("Aux Playback Switch", WM8974_MONOMIX, 2, 1, 0), |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 188 | SOC_DAPM_SINGLE("PCM Playback Switch", WM8974_MONOMIX, 0, 1, 0), |
| 189 | }; |
| 190 | |
| 191 | /* Boost mixer */ |
| 192 | static const struct snd_kcontrol_new wm8974_boost_mixer[] = { |
| 193 | SOC_DAPM_SINGLE("Aux Switch", WM8974_INPPGA, 6, 1, 0), |
| 194 | }; |
| 195 | |
| 196 | /* Input PGA */ |
| 197 | static const struct snd_kcontrol_new wm8974_inpga[] = { |
| 198 | SOC_DAPM_SINGLE("Aux Switch", WM8974_INPUT, 2, 1, 0), |
| 199 | SOC_DAPM_SINGLE("MicN Switch", WM8974_INPUT, 1, 1, 0), |
| 200 | SOC_DAPM_SINGLE("MicP Switch", WM8974_INPUT, 0, 1, 0), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | /* AUX Input boost vol */ |
| 204 | static const struct snd_kcontrol_new wm8974_aux_boost_controls = |
| 205 | SOC_DAPM_SINGLE("Aux Volume", WM8974_ADCBOOST, 0, 7, 0); |
| 206 | |
| 207 | /* Mic Input boost vol */ |
| 208 | static const struct snd_kcontrol_new wm8974_mic_boost_controls = |
| 209 | SOC_DAPM_SINGLE("Mic Volume", WM8974_ADCBOOST, 4, 7, 0); |
| 210 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 211 | static const struct snd_soc_dapm_widget wm8974_dapm_widgets[] = { |
| 212 | SND_SOC_DAPM_MIXER("Speaker Mixer", WM8974_POWER3, 2, 0, |
| 213 | &wm8974_speaker_mixer_controls[0], |
| 214 | ARRAY_SIZE(wm8974_speaker_mixer_controls)), |
| 215 | SND_SOC_DAPM_MIXER("Mono Mixer", WM8974_POWER3, 3, 0, |
| 216 | &wm8974_mono_mixer_controls[0], |
| 217 | ARRAY_SIZE(wm8974_mono_mixer_controls)), |
| 218 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8974_POWER3, 0, 0), |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 219 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8974_POWER2, 0, 0), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 220 | SND_SOC_DAPM_PGA("Aux Input", WM8974_POWER1, 6, 0, NULL, 0), |
| 221 | SND_SOC_DAPM_PGA("SpkN Out", WM8974_POWER3, 5, 0, NULL, 0), |
| 222 | SND_SOC_DAPM_PGA("SpkP Out", WM8974_POWER3, 6, 0, NULL, 0), |
| 223 | SND_SOC_DAPM_PGA("Mono Out", WM8974_POWER3, 7, 0, NULL, 0), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 224 | |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 225 | SND_SOC_DAPM_MIXER("Input PGA", WM8974_POWER2, 2, 0, wm8974_inpga, |
| 226 | ARRAY_SIZE(wm8974_inpga)), |
| 227 | SND_SOC_DAPM_MIXER("Boost Mixer", WM8974_POWER2, 4, 0, |
| 228 | wm8974_boost_mixer, ARRAY_SIZE(wm8974_boost_mixer)), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 229 | |
Mark Brown | 48dd231 | 2011-10-27 09:47:09 +0200 | [diff] [blame] | 230 | SND_SOC_DAPM_SUPPLY("Mic Bias", WM8974_POWER1, 4, 0, NULL, 0), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 231 | |
| 232 | SND_SOC_DAPM_INPUT("MICN"), |
| 233 | SND_SOC_DAPM_INPUT("MICP"), |
| 234 | SND_SOC_DAPM_INPUT("AUX"), |
| 235 | SND_SOC_DAPM_OUTPUT("MONOOUT"), |
| 236 | SND_SOC_DAPM_OUTPUT("SPKOUTP"), |
| 237 | SND_SOC_DAPM_OUTPUT("SPKOUTN"), |
| 238 | }; |
| 239 | |
Mark Brown | a2bd691 | 2011-12-29 11:10:27 +0000 | [diff] [blame] | 240 | static const struct snd_soc_dapm_route wm8974_dapm_routes[] = { |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 241 | /* Mono output mixer */ |
| 242 | {"Mono Mixer", "PCM Playback Switch", "DAC"}, |
| 243 | {"Mono Mixer", "Aux Playback Switch", "Aux Input"}, |
| 244 | {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"}, |
| 245 | |
| 246 | /* Speaker output mixer */ |
| 247 | {"Speaker Mixer", "PCM Playback Switch", "DAC"}, |
| 248 | {"Speaker Mixer", "Aux Playback Switch", "Aux Input"}, |
| 249 | {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"}, |
| 250 | |
| 251 | /* Outputs */ |
| 252 | {"Mono Out", NULL, "Mono Mixer"}, |
| 253 | {"MONOOUT", NULL, "Mono Out"}, |
| 254 | {"SpkN Out", NULL, "Speaker Mixer"}, |
| 255 | {"SpkP Out", NULL, "Speaker Mixer"}, |
| 256 | {"SPKOUTN", NULL, "SpkN Out"}, |
| 257 | {"SPKOUTP", NULL, "SpkP Out"}, |
| 258 | |
| 259 | /* Boost Mixer */ |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 260 | {"ADC", NULL, "Boost Mixer"}, |
| 261 | {"Boost Mixer", "Aux Switch", "Aux Input"}, |
| 262 | {"Boost Mixer", NULL, "Input PGA"}, |
| 263 | {"Boost Mixer", NULL, "MICP"}, |
| 264 | |
| 265 | /* Input PGA */ |
| 266 | {"Input PGA", "Aux Switch", "Aux Input"}, |
| 267 | {"Input PGA", "MicN Switch", "MICN"}, |
| 268 | {"Input PGA", "MicP Switch", "MICP"}, |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 269 | |
| 270 | /* Inputs */ |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 271 | {"Aux Input", NULL, "AUX"}, |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 272 | }; |
| 273 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 274 | struct pll_ { |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 275 | unsigned int pre_div:1; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 276 | unsigned int n:4; |
| 277 | unsigned int k; |
| 278 | }; |
| 279 | |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 280 | /* The size in bits of the pll divide multiplied by 10 |
| 281 | * to allow rounding later */ |
| 282 | #define FIXED_PLL_SIZE ((1 << 24) * 10) |
| 283 | |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 284 | static void pll_factors(struct pll_ *pll_div, |
| 285 | unsigned int target, unsigned int source) |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 286 | { |
| 287 | unsigned long long Kpart; |
| 288 | unsigned int K, Ndiv, Nmod; |
| 289 | |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 290 | /* There is a fixed divide by 4 in the output path */ |
| 291 | target *= 4; |
| 292 | |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 293 | Ndiv = target / source; |
| 294 | if (Ndiv < 6) { |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 295 | source /= 2; |
| 296 | pll_div->pre_div = 1; |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 297 | Ndiv = target / source; |
| 298 | } else |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 299 | pll_div->pre_div = 0; |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 300 | |
| 301 | if ((Ndiv < 6) || (Ndiv > 12)) |
| 302 | printk(KERN_WARNING |
Mark Brown | 8b83a19 | 2009-06-30 19:37:02 +0100 | [diff] [blame] | 303 | "WM8974 N value %u outwith recommended range!\n", |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 304 | Ndiv); |
| 305 | |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 306 | pll_div->n = Ndiv; |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 307 | Nmod = target % source; |
| 308 | Kpart = FIXED_PLL_SIZE * (long long)Nmod; |
| 309 | |
| 310 | do_div(Kpart, source); |
| 311 | |
| 312 | K = Kpart & 0xFFFFFFFF; |
| 313 | |
| 314 | /* Check if we need to round */ |
| 315 | if ((K % 10) >= 5) |
| 316 | K += 5; |
| 317 | |
| 318 | /* Move down to proper range now rounding is done */ |
| 319 | K /= 10; |
| 320 | |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 321 | pll_div->k = K; |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 322 | } |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 323 | |
Mark Brown | 8548803 | 2009-09-05 18:52:16 +0100 | [diff] [blame] | 324 | static int wm8974_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id, |
| 325 | int source, unsigned int freq_in, unsigned int freq_out) |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 326 | { |
| 327 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 328 | struct pll_ pll_div; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 329 | u16 reg; |
| 330 | |
Mark Brown | 1a55b3f | 2009-05-23 11:31:40 +0100 | [diff] [blame] | 331 | if (freq_in == 0 || freq_out == 0) { |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 332 | /* Clock CODEC directly from MCLK */ |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 333 | reg = snd_soc_read(codec, WM8974_CLOCK); |
| 334 | snd_soc_write(codec, WM8974_CLOCK, reg & 0x0ff); |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 335 | |
| 336 | /* Turn off PLL */ |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 337 | reg = snd_soc_read(codec, WM8974_POWER1); |
| 338 | snd_soc_write(codec, WM8974_POWER1, reg & 0x1df); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 342 | pll_factors(&pll_div, freq_out, freq_in); |
Mark Brown | 1a55b3f | 2009-05-23 11:31:40 +0100 | [diff] [blame] | 343 | |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 344 | snd_soc_write(codec, WM8974_PLLN, (pll_div.pre_div << 4) | pll_div.n); |
| 345 | snd_soc_write(codec, WM8974_PLLK1, pll_div.k >> 18); |
| 346 | snd_soc_write(codec, WM8974_PLLK2, (pll_div.k >> 9) & 0x1ff); |
| 347 | snd_soc_write(codec, WM8974_PLLK3, pll_div.k & 0x1ff); |
| 348 | reg = snd_soc_read(codec, WM8974_POWER1); |
| 349 | snd_soc_write(codec, WM8974_POWER1, reg | 0x020); |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 350 | |
| 351 | /* Run CODEC from PLL instead of MCLK */ |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 352 | reg = snd_soc_read(codec, WM8974_CLOCK); |
| 353 | snd_soc_write(codec, WM8974_CLOCK, reg | 0x100); |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 354 | |
| 355 | return 0; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Configure WM8974 clock dividers. |
| 360 | */ |
| 361 | static int wm8974_set_dai_clkdiv(struct snd_soc_dai *codec_dai, |
| 362 | int div_id, int div) |
| 363 | { |
| 364 | struct snd_soc_codec *codec = codec_dai->codec; |
| 365 | u16 reg; |
| 366 | |
| 367 | switch (div_id) { |
| 368 | case WM8974_OPCLKDIV: |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 369 | reg = snd_soc_read(codec, WM8974_GPIO) & 0x1cf; |
| 370 | snd_soc_write(codec, WM8974_GPIO, reg | div); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 371 | break; |
| 372 | case WM8974_MCLKDIV: |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 373 | reg = snd_soc_read(codec, WM8974_CLOCK) & 0x11f; |
| 374 | snd_soc_write(codec, WM8974_CLOCK, reg | div); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 375 | break; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 376 | case WM8974_BCLKDIV: |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 377 | reg = snd_soc_read(codec, WM8974_CLOCK) & 0x1e3; |
| 378 | snd_soc_write(codec, WM8974_CLOCK, reg | div); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 379 | break; |
| 380 | default: |
| 381 | return -EINVAL; |
| 382 | } |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
Mans Rullgard | 51b2bb3 | 2016-01-25 12:36:43 +0000 | [diff] [blame] | 387 | static unsigned int wm8974_get_mclkdiv(unsigned int f_in, unsigned int f_out, |
| 388 | int *mclkdiv) |
| 389 | { |
| 390 | unsigned int ratio = 2 * f_in / f_out; |
| 391 | |
| 392 | if (ratio <= 2) { |
| 393 | *mclkdiv = WM8974_MCLKDIV_1; |
| 394 | ratio = 2; |
| 395 | } else if (ratio == 3) { |
| 396 | *mclkdiv = WM8974_MCLKDIV_1_5; |
| 397 | } else if (ratio == 4) { |
| 398 | *mclkdiv = WM8974_MCLKDIV_2; |
| 399 | } else if (ratio <= 6) { |
| 400 | *mclkdiv = WM8974_MCLKDIV_3; |
| 401 | ratio = 6; |
| 402 | } else if (ratio <= 8) { |
| 403 | *mclkdiv = WM8974_MCLKDIV_4; |
| 404 | ratio = 8; |
| 405 | } else if (ratio <= 12) { |
| 406 | *mclkdiv = WM8974_MCLKDIV_6; |
| 407 | ratio = 12; |
| 408 | } else if (ratio <= 16) { |
| 409 | *mclkdiv = WM8974_MCLKDIV_8; |
| 410 | ratio = 16; |
| 411 | } else { |
| 412 | *mclkdiv = WM8974_MCLKDIV_12; |
| 413 | ratio = 24; |
| 414 | } |
| 415 | |
| 416 | return f_out * ratio / 2; |
| 417 | } |
| 418 | |
| 419 | static int wm8974_update_clocks(struct snd_soc_dai *dai) |
| 420 | { |
| 421 | struct snd_soc_codec *codec = dai->codec; |
| 422 | struct wm8974_priv *priv = snd_soc_codec_get_drvdata(codec); |
| 423 | unsigned int fs256; |
| 424 | unsigned int fpll = 0; |
| 425 | unsigned int f; |
| 426 | int mclkdiv; |
| 427 | |
| 428 | if (!priv->mclk || !priv->fs) |
| 429 | return 0; |
| 430 | |
| 431 | fs256 = 256 * priv->fs; |
| 432 | |
| 433 | f = wm8974_get_mclkdiv(priv->mclk, fs256, &mclkdiv); |
| 434 | |
| 435 | if (f != priv->mclk) { |
| 436 | /* The PLL performs best around 90MHz */ |
| 437 | fpll = wm8974_get_mclkdiv(22500000, fs256, &mclkdiv); |
| 438 | } |
| 439 | |
| 440 | wm8974_set_dai_pll(dai, 0, 0, priv->mclk, fpll); |
| 441 | wm8974_set_dai_clkdiv(dai, WM8974_MCLKDIV, mclkdiv); |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static int wm8974_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id, |
| 447 | unsigned int freq, int dir) |
| 448 | { |
| 449 | struct snd_soc_codec *codec = dai->codec; |
| 450 | struct wm8974_priv *priv = snd_soc_codec_get_drvdata(codec); |
| 451 | |
| 452 | if (dir != SND_SOC_CLOCK_IN) |
| 453 | return -EINVAL; |
| 454 | |
| 455 | priv->mclk = freq; |
| 456 | |
| 457 | return wm8974_update_clocks(dai); |
| 458 | } |
| 459 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 460 | static int wm8974_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 461 | unsigned int fmt) |
| 462 | { |
| 463 | struct snd_soc_codec *codec = codec_dai->codec; |
| 464 | u16 iface = 0; |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 465 | u16 clk = snd_soc_read(codec, WM8974_CLOCK) & 0x1fe; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 466 | |
| 467 | /* set master/slave audio interface */ |
| 468 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 469 | case SND_SOC_DAIFMT_CBM_CFM: |
| 470 | clk |= 0x0001; |
| 471 | break; |
| 472 | case SND_SOC_DAIFMT_CBS_CFS: |
| 473 | break; |
| 474 | default: |
| 475 | return -EINVAL; |
| 476 | } |
| 477 | |
| 478 | /* interface format */ |
| 479 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 480 | case SND_SOC_DAIFMT_I2S: |
| 481 | iface |= 0x0010; |
| 482 | break; |
| 483 | case SND_SOC_DAIFMT_RIGHT_J: |
| 484 | break; |
| 485 | case SND_SOC_DAIFMT_LEFT_J: |
| 486 | iface |= 0x0008; |
| 487 | break; |
| 488 | case SND_SOC_DAIFMT_DSP_A: |
| 489 | iface |= 0x00018; |
| 490 | break; |
| 491 | default: |
| 492 | return -EINVAL; |
| 493 | } |
| 494 | |
| 495 | /* clock inversion */ |
| 496 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 497 | case SND_SOC_DAIFMT_NB_NF: |
| 498 | break; |
| 499 | case SND_SOC_DAIFMT_IB_IF: |
| 500 | iface |= 0x0180; |
| 501 | break; |
| 502 | case SND_SOC_DAIFMT_IB_NF: |
| 503 | iface |= 0x0100; |
| 504 | break; |
| 505 | case SND_SOC_DAIFMT_NB_IF: |
| 506 | iface |= 0x0080; |
| 507 | break; |
| 508 | default: |
| 509 | return -EINVAL; |
| 510 | } |
| 511 | |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 512 | snd_soc_write(codec, WM8974_IFACE, iface); |
| 513 | snd_soc_write(codec, WM8974_CLOCK, clk); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static int wm8974_pcm_hw_params(struct snd_pcm_substream *substream, |
| 518 | struct snd_pcm_hw_params *params, |
| 519 | struct snd_soc_dai *dai) |
| 520 | { |
| 521 | struct snd_soc_codec *codec = dai->codec; |
Mans Rullgard | 51b2bb3 | 2016-01-25 12:36:43 +0000 | [diff] [blame] | 522 | struct wm8974_priv *priv = snd_soc_codec_get_drvdata(codec); |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 523 | u16 iface = snd_soc_read(codec, WM8974_IFACE) & 0x19f; |
| 524 | u16 adn = snd_soc_read(codec, WM8974_ADD) & 0x1f1; |
Mans Rullgard | 51b2bb3 | 2016-01-25 12:36:43 +0000 | [diff] [blame] | 525 | int err; |
| 526 | |
| 527 | priv->fs = params_rate(params); |
| 528 | err = wm8974_update_clocks(dai); |
| 529 | if (err) |
| 530 | return err; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 531 | |
| 532 | /* bit size */ |
Mark Brown | 6afdc9a | 2014-07-31 12:53:50 +0100 | [diff] [blame] | 533 | switch (params_width(params)) { |
| 534 | case 16: |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 535 | break; |
Mark Brown | 6afdc9a | 2014-07-31 12:53:50 +0100 | [diff] [blame] | 536 | case 20: |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 537 | iface |= 0x0020; |
| 538 | break; |
Mark Brown | 6afdc9a | 2014-07-31 12:53:50 +0100 | [diff] [blame] | 539 | case 24: |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 540 | iface |= 0x0040; |
| 541 | break; |
Mark Brown | 6afdc9a | 2014-07-31 12:53:50 +0100 | [diff] [blame] | 542 | case 32: |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 543 | iface |= 0x0060; |
| 544 | break; |
| 545 | } |
| 546 | |
| 547 | /* filter coefficient */ |
| 548 | switch (params_rate(params)) { |
Guennadi Liakhovetski | b3172f2 | 2009-12-24 01:13:51 +0100 | [diff] [blame] | 549 | case 8000: |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 550 | adn |= 0x5 << 1; |
| 551 | break; |
Guennadi Liakhovetski | b3172f2 | 2009-12-24 01:13:51 +0100 | [diff] [blame] | 552 | case 11025: |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 553 | adn |= 0x4 << 1; |
| 554 | break; |
Guennadi Liakhovetski | b3172f2 | 2009-12-24 01:13:51 +0100 | [diff] [blame] | 555 | case 16000: |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 556 | adn |= 0x3 << 1; |
| 557 | break; |
Guennadi Liakhovetski | b3172f2 | 2009-12-24 01:13:51 +0100 | [diff] [blame] | 558 | case 22050: |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 559 | adn |= 0x2 << 1; |
| 560 | break; |
Guennadi Liakhovetski | b3172f2 | 2009-12-24 01:13:51 +0100 | [diff] [blame] | 561 | case 32000: |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 562 | adn |= 0x1 << 1; |
| 563 | break; |
Guennadi Liakhovetski | b3172f2 | 2009-12-24 01:13:51 +0100 | [diff] [blame] | 564 | case 44100: |
| 565 | case 48000: |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 566 | break; |
| 567 | } |
| 568 | |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 569 | snd_soc_write(codec, WM8974_IFACE, iface); |
| 570 | snd_soc_write(codec, WM8974_ADD, adn); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | static int wm8974_mute(struct snd_soc_dai *dai, int mute) |
| 575 | { |
| 576 | struct snd_soc_codec *codec = dai->codec; |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 577 | u16 mute_reg = snd_soc_read(codec, WM8974_DAC) & 0xffbf; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 578 | |
Mark Brown | 1a55b3f | 2009-05-23 11:31:40 +0100 | [diff] [blame] | 579 | if (mute) |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 580 | snd_soc_write(codec, WM8974_DAC, mute_reg | 0x40); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 581 | else |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 582 | snd_soc_write(codec, WM8974_DAC, mute_reg); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | /* liam need to make this lower power with dapm */ |
| 587 | static int wm8974_set_bias_level(struct snd_soc_codec *codec, |
| 588 | enum snd_soc_bias_level level) |
| 589 | { |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 590 | u16 power1 = snd_soc_read(codec, WM8974_POWER1) & ~0x3; |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 591 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 592 | switch (level) { |
| 593 | case SND_SOC_BIAS_ON: |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 594 | case SND_SOC_BIAS_PREPARE: |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 595 | power1 |= 0x1; /* VMID 50k */ |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 596 | snd_soc_write(codec, WM8974_POWER1, power1); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 597 | break; |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 598 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 599 | case SND_SOC_BIAS_STANDBY: |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 600 | power1 |= WM8974_POWER1_BIASEN | WM8974_POWER1_BUFIOEN; |
| 601 | |
Lars-Peter Clausen | 5c6415d | 2015-06-01 10:10:52 +0200 | [diff] [blame] | 602 | if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) { |
Mark Brown | e40e0b5 | 2013-11-08 14:01:39 +0000 | [diff] [blame] | 603 | regcache_sync(dev_get_regmap(codec->dev, NULL)); |
Axel Lin | 0bad3d8 | 2011-10-07 21:52:42 +0800 | [diff] [blame] | 604 | |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 605 | /* Initial cap charge at VMID 5k */ |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 606 | snd_soc_write(codec, WM8974_POWER1, power1 | 0x3); |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 607 | mdelay(100); |
| 608 | } |
| 609 | |
| 610 | power1 |= 0x2; /* VMID 500k */ |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 611 | snd_soc_write(codec, WM8974_POWER1, power1); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 612 | break; |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 613 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 614 | case SND_SOC_BIAS_OFF: |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 615 | snd_soc_write(codec, WM8974_POWER1, 0); |
| 616 | snd_soc_write(codec, WM8974_POWER2, 0); |
| 617 | snd_soc_write(codec, WM8974_POWER3, 0); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 618 | break; |
| 619 | } |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 620 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
Mark Brown | 1a55b3f | 2009-05-23 11:31:40 +0100 | [diff] [blame] | 624 | #define WM8974_RATES (SNDRV_PCM_RATE_8000_48000) |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 625 | |
| 626 | #define WM8974_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 627 | SNDRV_PCM_FMTBIT_S24_LE) |
| 628 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 629 | static const struct snd_soc_dai_ops wm8974_ops = { |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 630 | .hw_params = wm8974_pcm_hw_params, |
| 631 | .digital_mute = wm8974_mute, |
| 632 | .set_fmt = wm8974_set_dai_fmt, |
| 633 | .set_clkdiv = wm8974_set_dai_clkdiv, |
| 634 | .set_pll = wm8974_set_dai_pll, |
Mans Rullgard | 51b2bb3 | 2016-01-25 12:36:43 +0000 | [diff] [blame] | 635 | .set_sysclk = wm8974_set_dai_sysclk, |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 636 | }; |
| 637 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 638 | static struct snd_soc_dai_driver wm8974_dai = { |
| 639 | .name = "wm8974-hifi", |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 640 | .playback = { |
| 641 | .stream_name = "Playback", |
| 642 | .channels_min = 1, |
Mark Brown | 33d81af | 2009-06-30 19:01:52 +0100 | [diff] [blame] | 643 | .channels_max = 2, /* Only 1 channel of data */ |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 644 | .rates = WM8974_RATES, |
| 645 | .formats = WM8974_FORMATS,}, |
| 646 | .capture = { |
| 647 | .stream_name = "Capture", |
| 648 | .channels_min = 1, |
Mark Brown | 33d81af | 2009-06-30 19:01:52 +0100 | [diff] [blame] | 649 | .channels_max = 2, /* Only 1 channel of data */ |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 650 | .rates = WM8974_RATES, |
| 651 | .formats = WM8974_FORMATS,}, |
| 652 | .ops = &wm8974_ops, |
Mark Brown | cb11d39 | 2009-06-30 19:36:39 +0100 | [diff] [blame] | 653 | .symmetric_rates = 1, |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 654 | }; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 655 | |
Mark Brown | e40e0b5 | 2013-11-08 14:01:39 +0000 | [diff] [blame] | 656 | static const struct regmap_config wm8974_regmap = { |
| 657 | .reg_bits = 7, |
| 658 | .val_bits = 9, |
| 659 | |
| 660 | .max_register = WM8974_MONOMIX, |
| 661 | .reg_defaults = wm8974_reg_defaults, |
| 662 | .num_reg_defaults = ARRAY_SIZE(wm8974_reg_defaults), |
Mans Rullgard | 1ea5998a | 2015-12-11 11:27:08 +0000 | [diff] [blame] | 663 | .cache_type = REGCACHE_FLAT, |
Mark Brown | e40e0b5 | 2013-11-08 14:01:39 +0000 | [diff] [blame] | 664 | }; |
| 665 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 666 | static int wm8974_probe(struct snd_soc_codec *codec) |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 667 | { |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 668 | int ret = 0; |
| 669 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 670 | ret = wm8974_reset(codec); |
| 671 | if (ret < 0) { |
| 672 | dev_err(codec->dev, "Failed to issue reset\n"); |
| 673 | return ret; |
| 674 | } |
| 675 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 676 | return 0; |
| 677 | } |
| 678 | |
Julia Lawall | f802d6c | 2016-08-31 23:52:27 +0200 | [diff] [blame] | 679 | static const struct snd_soc_codec_driver soc_codec_dev_wm8974 = { |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 680 | .probe = wm8974_probe, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 681 | .set_bias_level = wm8974_set_bias_level, |
Lars-Peter Clausen | ed1358f | 2014-11-23 13:37:35 +0100 | [diff] [blame] | 682 | .suspend_bias_off = true, |
Mark Brown | a2bd691 | 2011-12-29 11:10:27 +0000 | [diff] [blame] | 683 | |
Kuninori Morimoto | 48b0423 | 2016-08-08 08:48:05 +0000 | [diff] [blame] | 684 | .component_driver = { |
| 685 | .controls = wm8974_snd_controls, |
| 686 | .num_controls = ARRAY_SIZE(wm8974_snd_controls), |
| 687 | .dapm_widgets = wm8974_dapm_widgets, |
| 688 | .num_dapm_widgets = ARRAY_SIZE(wm8974_dapm_widgets), |
| 689 | .dapm_routes = wm8974_dapm_routes, |
| 690 | .num_dapm_routes = ARRAY_SIZE(wm8974_dapm_routes), |
| 691 | }, |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 692 | }; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 693 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 694 | static int wm8974_i2c_probe(struct i2c_client *i2c, |
| 695 | const struct i2c_device_id *id) |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 696 | { |
Mans Rullgard | 51b2bb3 | 2016-01-25 12:36:43 +0000 | [diff] [blame] | 697 | struct wm8974_priv *priv; |
Mark Brown | e40e0b5 | 2013-11-08 14:01:39 +0000 | [diff] [blame] | 698 | struct regmap *regmap; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 699 | int ret; |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 700 | |
Mans Rullgard | 51b2bb3 | 2016-01-25 12:36:43 +0000 | [diff] [blame] | 701 | priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL); |
| 702 | if (!priv) |
| 703 | return -ENOMEM; |
| 704 | |
| 705 | i2c_set_clientdata(i2c, priv); |
| 706 | |
Mark Brown | e40e0b5 | 2013-11-08 14:01:39 +0000 | [diff] [blame] | 707 | regmap = devm_regmap_init_i2c(i2c, &wm8974_regmap); |
| 708 | if (IS_ERR(regmap)) |
| 709 | return PTR_ERR(regmap); |
| 710 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 711 | ret = snd_soc_register_codec(&i2c->dev, |
| 712 | &soc_codec_dev_wm8974, &wm8974_dai, 1); |
Mark Brown | c2562a8 | 2011-12-29 11:11:25 +0000 | [diff] [blame] | 713 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 714 | return ret; |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 715 | } |
| 716 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 717 | static int wm8974_i2c_remove(struct i2c_client *client) |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 718 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 719 | snd_soc_unregister_codec(&client->dev); |
Mark Brown | c2562a8 | 2011-12-29 11:11:25 +0000 | [diff] [blame] | 720 | |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static const struct i2c_device_id wm8974_i2c_id[] = { |
| 725 | { "wm8974", 0 }, |
| 726 | { } |
| 727 | }; |
| 728 | MODULE_DEVICE_TABLE(i2c, wm8974_i2c_id); |
| 729 | |
Mans Rullgard | 2005bd8 | 2015-12-16 13:02:55 +0000 | [diff] [blame] | 730 | static const struct of_device_id wm8974_of_match[] = { |
| 731 | { .compatible = "wlf,wm8974", }, |
| 732 | { } |
| 733 | }; |
| 734 | MODULE_DEVICE_TABLE(of, wm8974_of_match); |
| 735 | |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 736 | static struct i2c_driver wm8974_i2c_driver = { |
| 737 | .driver = { |
Mark Brown | 091edcc | 2011-12-02 22:08:49 +0000 | [diff] [blame] | 738 | .name = "wm8974", |
Mans Rullgard | 2005bd8 | 2015-12-16 13:02:55 +0000 | [diff] [blame] | 739 | .of_match_table = wm8974_of_match, |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 740 | }, |
| 741 | .probe = wm8974_i2c_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 742 | .remove = wm8974_i2c_remove, |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 743 | .id_table = wm8974_i2c_id, |
| 744 | }; |
| 745 | |
Sachin Kamat | 2be5941 | 2012-08-06 17:25:59 +0530 | [diff] [blame] | 746 | module_i2c_driver(wm8974_i2c_driver); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 747 | |
| 748 | MODULE_DESCRIPTION("ASoC WM8974 driver"); |
| 749 | MODULE_AUTHOR("Liam Girdwood"); |
| 750 | MODULE_LICENSE("GPL"); |