Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * wm8971.c  --  WM8971 ALSA SoC Audio driver
0004  *
0005  * Copyright 2005 Lab126, Inc.
0006  *
0007  * Author: Kenneth Kiraly <kiraly@lab126.com>
0008  *
0009  * Based on wm8753.c by Liam Girdwood
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/moduleparam.h>
0014 #include <linux/init.h>
0015 #include <linux/delay.h>
0016 #include <linux/pm.h>
0017 #include <linux/i2c.h>
0018 #include <linux/regmap.h>
0019 #include <linux/slab.h>
0020 #include <sound/core.h>
0021 #include <sound/pcm.h>
0022 #include <sound/pcm_params.h>
0023 #include <sound/soc.h>
0024 #include <sound/initval.h>
0025 
0026 #include "wm8971.h"
0027 
0028 #define WM8971_REG_COUNT        43
0029 
0030 /* codec private data */
0031 struct wm8971_priv {
0032     unsigned int sysclk;
0033     struct delayed_work charge_work;
0034     struct regmap *regmap;
0035 };
0036 
0037 /*
0038  * wm8971 register cache
0039  * We can't read the WM8971 register space when we
0040  * are using 2 wire for device control, so we cache them instead.
0041  */
0042 static const struct reg_default wm8971_reg_defaults[] = {
0043     {  0, 0x0097 },
0044     {  1, 0x0097 },
0045     {  2, 0x0079 },
0046     {  3, 0x0079 },
0047     {  4, 0x0000 },
0048     {  5, 0x0008 },
0049     {  6, 0x0000 },
0050     {  7, 0x000a },
0051     {  8, 0x0000 },
0052     {  9, 0x0000 },
0053     { 10, 0x00ff },
0054     { 11, 0x00ff },
0055     { 12, 0x000f },
0056     { 13, 0x000f },
0057     { 14, 0x0000 },
0058     { 15, 0x0000 },
0059     { 16, 0x0000 },
0060     { 17, 0x007b },
0061     { 18, 0x0000 },
0062     { 19, 0x0032 },
0063     { 20, 0x0000 },
0064     { 21, 0x00c3 },
0065     { 22, 0x00c3 },
0066     { 23, 0x00c0 },
0067     { 24, 0x0000 },
0068     { 25, 0x0000 },
0069     { 26, 0x0000 },
0070     { 27, 0x0000 },
0071     { 28, 0x0000 },
0072     { 29, 0x0000 },
0073     { 30, 0x0000 },
0074     { 31, 0x0000 },
0075     { 32, 0x0000 },
0076     { 33, 0x0000 },
0077     { 34, 0x0050 },
0078     { 35, 0x0050 },
0079     { 36, 0x0050 },
0080     { 37, 0x0050 },
0081     { 38, 0x0050 },
0082     { 39, 0x0050 },
0083     { 40, 0x0079 },
0084     { 41, 0x0079 },
0085     { 42, 0x0079 },
0086 };
0087 
0088 #define wm8971_reset(c) snd_soc_component_write(c, WM8971_RESET, 0)
0089 
0090 /* WM8971 Controls */
0091 static const char *wm8971_bass[] = { "Linear Control", "Adaptive Boost" };
0092 static const char *wm8971_bass_filter[] = { "130Hz @ 48kHz",
0093     "200Hz @ 48kHz" };
0094 static const char *wm8971_treble[] = { "8kHz", "4kHz" };
0095 static const char *wm8971_alc_func[] = { "Off", "Right", "Left", "Stereo" };
0096 static const char *wm8971_ng_type[] = { "Constant PGA Gain",
0097     "Mute ADC Output" };
0098 static const char *wm8971_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" };
0099 static const char *wm8971_mono_mux[] = {"Stereo", "Mono (Left)",
0100     "Mono (Right)", "Digital Mono"};
0101 static const char *wm8971_dac_phase[] = { "Non Inverted", "Inverted" };
0102 static const char *wm8971_lline_mux[] = {"Line", "NC", "NC", "PGA",
0103     "Differential"};
0104 static const char *wm8971_rline_mux[] = {"Line", "Mic", "NC", "PGA",
0105     "Differential"};
0106 static const char *wm8971_lpga_sel[] = {"Line", "NC", "NC", "Differential"};
0107 static const char *wm8971_rpga_sel[] = {"Line", "Mic", "NC", "Differential"};
0108 static const char *wm8971_adcpol[] = {"Normal", "L Invert", "R Invert",
0109     "L + R Invert"};
0110 
0111 static const struct soc_enum wm8971_enum[] = {
0112     SOC_ENUM_SINGLE(WM8971_BASS, 7, 2, wm8971_bass),    /* 0 */
0113     SOC_ENUM_SINGLE(WM8971_BASS, 6, 2, wm8971_bass_filter),
0114     SOC_ENUM_SINGLE(WM8971_TREBLE, 6, 2, wm8971_treble),
0115     SOC_ENUM_SINGLE(WM8971_ALC1, 7, 4, wm8971_alc_func),
0116     SOC_ENUM_SINGLE(WM8971_NGATE, 1, 2, wm8971_ng_type),    /* 4 */
0117     SOC_ENUM_SINGLE(WM8971_ADCDAC, 1, 4, wm8971_deemp),
0118     SOC_ENUM_SINGLE(WM8971_ADCTL1, 4, 4, wm8971_mono_mux),
0119     SOC_ENUM_SINGLE(WM8971_ADCTL1, 1, 2, wm8971_dac_phase),
0120     SOC_ENUM_SINGLE(WM8971_LOUTM1, 0, 5, wm8971_lline_mux), /* 8 */
0121     SOC_ENUM_SINGLE(WM8971_ROUTM1, 0, 5, wm8971_rline_mux),
0122     SOC_ENUM_SINGLE(WM8971_LADCIN, 6, 4, wm8971_lpga_sel),
0123     SOC_ENUM_SINGLE(WM8971_RADCIN, 6, 4, wm8971_rpga_sel),
0124     SOC_ENUM_SINGLE(WM8971_ADCDAC, 5, 4, wm8971_adcpol),    /* 12 */
0125     SOC_ENUM_SINGLE(WM8971_ADCIN, 6, 4, wm8971_mono_mux),
0126 };
0127 
0128 static const struct snd_kcontrol_new wm8971_snd_controls[] = {
0129     SOC_DOUBLE_R("Capture Volume", WM8971_LINVOL, WM8971_RINVOL, 0, 63, 0),
0130     SOC_DOUBLE_R("Capture ZC Switch", WM8971_LINVOL, WM8971_RINVOL,
0131              6, 1, 0),
0132     SOC_DOUBLE_R("Capture Switch", WM8971_LINVOL, WM8971_RINVOL, 7, 1, 1),
0133 
0134     SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8971_LOUT1V,
0135         WM8971_ROUT1V, 7, 1, 0),
0136     SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8971_LOUT2V,
0137         WM8971_ROUT2V, 7, 1, 0),
0138     SOC_SINGLE("Mono Playback ZC Switch", WM8971_MOUTV, 7, 1, 0),
0139 
0140     SOC_DOUBLE_R("PCM Volume", WM8971_LDAC, WM8971_RDAC, 0, 255, 0),
0141 
0142     SOC_DOUBLE_R("Bypass Left Playback Volume", WM8971_LOUTM1,
0143         WM8971_LOUTM2, 4, 7, 1),
0144     SOC_DOUBLE_R("Bypass Right Playback Volume", WM8971_ROUTM1,
0145         WM8971_ROUTM2, 4, 7, 1),
0146     SOC_DOUBLE_R("Bypass Mono Playback Volume", WM8971_MOUTM1,
0147         WM8971_MOUTM2, 4, 7, 1),
0148 
0149     SOC_DOUBLE_R("Headphone Playback Volume", WM8971_LOUT1V,
0150         WM8971_ROUT1V, 0, 127, 0),
0151     SOC_DOUBLE_R("Speaker Playback Volume", WM8971_LOUT2V,
0152         WM8971_ROUT2V, 0, 127, 0),
0153 
0154     SOC_ENUM("Bass Boost", wm8971_enum[0]),
0155     SOC_ENUM("Bass Filter", wm8971_enum[1]),
0156     SOC_SINGLE("Bass Volume", WM8971_BASS, 0, 7, 1),
0157 
0158     SOC_SINGLE("Treble Volume", WM8971_TREBLE, 0, 7, 0),
0159     SOC_ENUM("Treble Cut-off", wm8971_enum[2]),
0160 
0161     SOC_SINGLE("Capture Filter Switch", WM8971_ADCDAC, 0, 1, 1),
0162 
0163     SOC_SINGLE("ALC Target Volume", WM8971_ALC1, 0, 7, 0),
0164     SOC_SINGLE("ALC Max Volume", WM8971_ALC1, 4, 7, 0),
0165 
0166     SOC_SINGLE("ALC Capture Target Volume", WM8971_ALC1, 0, 7, 0),
0167     SOC_SINGLE("ALC Capture Max Volume", WM8971_ALC1, 4, 7, 0),
0168     SOC_ENUM("ALC Capture Function", wm8971_enum[3]),
0169     SOC_SINGLE("ALC Capture ZC Switch", WM8971_ALC2, 7, 1, 0),
0170     SOC_SINGLE("ALC Capture Hold Time", WM8971_ALC2, 0, 15, 0),
0171     SOC_SINGLE("ALC Capture Decay Time", WM8971_ALC3, 4, 15, 0),
0172     SOC_SINGLE("ALC Capture Attack Time", WM8971_ALC3, 0, 15, 0),
0173     SOC_SINGLE("ALC Capture NG Threshold", WM8971_NGATE, 3, 31, 0),
0174     SOC_ENUM("ALC Capture NG Type", wm8971_enum[4]),
0175     SOC_SINGLE("ALC Capture NG Switch", WM8971_NGATE, 0, 1, 0),
0176 
0177     SOC_SINGLE("Capture 6dB Attenuate", WM8971_ADCDAC, 8, 1, 0),
0178     SOC_SINGLE("Playback 6dB Attenuate", WM8971_ADCDAC, 7, 1, 0),
0179 
0180     SOC_ENUM("Playback De-emphasis", wm8971_enum[5]),
0181     SOC_ENUM("Playback Function", wm8971_enum[6]),
0182     SOC_ENUM("Playback Phase", wm8971_enum[7]),
0183 
0184     SOC_DOUBLE_R("Mic Boost", WM8971_LADCIN, WM8971_RADCIN, 4, 3, 0),
0185 };
0186 
0187 /*
0188  * DAPM Controls
0189  */
0190 
0191 /* Left Mixer */
0192 static const struct snd_kcontrol_new wm8971_left_mixer_controls[] = {
0193 SOC_DAPM_SINGLE("Playback Switch", WM8971_LOUTM1, 8, 1, 0),
0194 SOC_DAPM_SINGLE("Left Bypass Switch", WM8971_LOUTM1, 7, 1, 0),
0195 SOC_DAPM_SINGLE("Right Playback Switch", WM8971_LOUTM2, 8, 1, 0),
0196 SOC_DAPM_SINGLE("Right Bypass Switch", WM8971_LOUTM2, 7, 1, 0),
0197 };
0198 
0199 /* Right Mixer */
0200 static const struct snd_kcontrol_new wm8971_right_mixer_controls[] = {
0201 SOC_DAPM_SINGLE("Left Playback Switch", WM8971_ROUTM1, 8, 1, 0),
0202 SOC_DAPM_SINGLE("Left Bypass Switch", WM8971_ROUTM1, 7, 1, 0),
0203 SOC_DAPM_SINGLE("Playback Switch", WM8971_ROUTM2, 8, 1, 0),
0204 SOC_DAPM_SINGLE("Right Bypass Switch", WM8971_ROUTM2, 7, 1, 0),
0205 };
0206 
0207 /* Mono Mixer */
0208 static const struct snd_kcontrol_new wm8971_mono_mixer_controls[] = {
0209 SOC_DAPM_SINGLE("Left Playback Switch", WM8971_MOUTM1, 8, 1, 0),
0210 SOC_DAPM_SINGLE("Left Bypass Switch", WM8971_MOUTM1, 7, 1, 0),
0211 SOC_DAPM_SINGLE("Right Playback Switch", WM8971_MOUTM2, 8, 1, 0),
0212 SOC_DAPM_SINGLE("Right Bypass Switch", WM8971_MOUTM2, 7, 1, 0),
0213 };
0214 
0215 /* Left Line Mux */
0216 static const struct snd_kcontrol_new wm8971_left_line_controls =
0217 SOC_DAPM_ENUM("Route", wm8971_enum[8]);
0218 
0219 /* Right Line Mux */
0220 static const struct snd_kcontrol_new wm8971_right_line_controls =
0221 SOC_DAPM_ENUM("Route", wm8971_enum[9]);
0222 
0223 /* Left PGA Mux */
0224 static const struct snd_kcontrol_new wm8971_left_pga_controls =
0225 SOC_DAPM_ENUM("Route", wm8971_enum[10]);
0226 
0227 /* Right PGA Mux */
0228 static const struct snd_kcontrol_new wm8971_right_pga_controls =
0229 SOC_DAPM_ENUM("Route", wm8971_enum[11]);
0230 
0231 /* Mono ADC Mux */
0232 static const struct snd_kcontrol_new wm8971_monomux_controls =
0233 SOC_DAPM_ENUM("Route", wm8971_enum[13]);
0234 
0235 static const struct snd_soc_dapm_widget wm8971_dapm_widgets[] = {
0236     SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
0237         &wm8971_left_mixer_controls[0],
0238         ARRAY_SIZE(wm8971_left_mixer_controls)),
0239     SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
0240         &wm8971_right_mixer_controls[0],
0241         ARRAY_SIZE(wm8971_right_mixer_controls)),
0242     SND_SOC_DAPM_MIXER("Mono Mixer", WM8971_PWR2, 2, 0,
0243         &wm8971_mono_mixer_controls[0],
0244         ARRAY_SIZE(wm8971_mono_mixer_controls)),
0245 
0246     SND_SOC_DAPM_PGA("Right Out 2", WM8971_PWR2, 3, 0, NULL, 0),
0247     SND_SOC_DAPM_PGA("Left Out 2", WM8971_PWR2, 4, 0, NULL, 0),
0248     SND_SOC_DAPM_PGA("Right Out 1", WM8971_PWR2, 5, 0, NULL, 0),
0249     SND_SOC_DAPM_PGA("Left Out 1", WM8971_PWR2, 6, 0, NULL, 0),
0250     SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8971_PWR2, 7, 0),
0251     SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8971_PWR2, 8, 0),
0252     SND_SOC_DAPM_PGA("Mono Out 1", WM8971_PWR2, 2, 0, NULL, 0),
0253 
0254     SND_SOC_DAPM_SUPPLY("Mic Bias", WM8971_PWR1, 1, 0, NULL, 0),
0255     SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8971_PWR1, 2, 0),
0256     SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8971_PWR1, 3, 0),
0257 
0258     SND_SOC_DAPM_MUX("Left PGA Mux", WM8971_PWR1, 5, 0,
0259         &wm8971_left_pga_controls),
0260     SND_SOC_DAPM_MUX("Right PGA Mux", WM8971_PWR1, 4, 0,
0261         &wm8971_right_pga_controls),
0262     SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
0263         &wm8971_left_line_controls),
0264     SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
0265         &wm8971_right_line_controls),
0266 
0267     SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
0268         &wm8971_monomux_controls),
0269     SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
0270         &wm8971_monomux_controls),
0271 
0272     SND_SOC_DAPM_OUTPUT("LOUT1"),
0273     SND_SOC_DAPM_OUTPUT("ROUT1"),
0274     SND_SOC_DAPM_OUTPUT("LOUT2"),
0275     SND_SOC_DAPM_OUTPUT("ROUT2"),
0276     SND_SOC_DAPM_OUTPUT("MONO"),
0277 
0278     SND_SOC_DAPM_INPUT("LINPUT1"),
0279     SND_SOC_DAPM_INPUT("RINPUT1"),
0280     SND_SOC_DAPM_INPUT("MIC"),
0281 };
0282 
0283 static const struct snd_soc_dapm_route wm8971_dapm_routes[] = {
0284     /* left mixer */
0285     {"Left Mixer", "Playback Switch", "Left DAC"},
0286     {"Left Mixer", "Left Bypass Switch", "Left Line Mux"},
0287     {"Left Mixer", "Right Playback Switch", "Right DAC"},
0288     {"Left Mixer", "Right Bypass Switch", "Right Line Mux"},
0289 
0290     /* right mixer */
0291     {"Right Mixer", "Left Playback Switch", "Left DAC"},
0292     {"Right Mixer", "Left Bypass Switch", "Left Line Mux"},
0293     {"Right Mixer", "Playback Switch", "Right DAC"},
0294     {"Right Mixer", "Right Bypass Switch", "Right Line Mux"},
0295 
0296     /* left out 1 */
0297     {"Left Out 1", NULL, "Left Mixer"},
0298     {"LOUT1", NULL, "Left Out 1"},
0299 
0300     /* left out 2 */
0301     {"Left Out 2", NULL, "Left Mixer"},
0302     {"LOUT2", NULL, "Left Out 2"},
0303 
0304     /* right out 1 */
0305     {"Right Out 1", NULL, "Right Mixer"},
0306     {"ROUT1", NULL, "Right Out 1"},
0307 
0308     /* right out 2 */
0309     {"Right Out 2", NULL, "Right Mixer"},
0310     {"ROUT2", NULL, "Right Out 2"},
0311 
0312     /* mono mixer */
0313     {"Mono Mixer", "Left Playback Switch", "Left DAC"},
0314     {"Mono Mixer", "Left Bypass Switch", "Left Line Mux"},
0315     {"Mono Mixer", "Right Playback Switch", "Right DAC"},
0316     {"Mono Mixer", "Right Bypass Switch", "Right Line Mux"},
0317 
0318     /* mono out */
0319     {"Mono Out", NULL, "Mono Mixer"},
0320     {"MONO1", NULL, "Mono Out"},
0321 
0322     /* Left Line Mux */
0323     {"Left Line Mux", "Line", "LINPUT1"},
0324     {"Left Line Mux", "PGA", "Left PGA Mux"},
0325     {"Left Line Mux", "Differential", "Differential Mux"},
0326 
0327     /* Right Line Mux */
0328     {"Right Line Mux", "Line", "RINPUT1"},
0329     {"Right Line Mux", "Mic", "MIC"},
0330     {"Right Line Mux", "PGA", "Right PGA Mux"},
0331     {"Right Line Mux", "Differential", "Differential Mux"},
0332 
0333     /* Left PGA Mux */
0334     {"Left PGA Mux", "Line", "LINPUT1"},
0335     {"Left PGA Mux", "Differential", "Differential Mux"},
0336 
0337     /* Right PGA Mux */
0338     {"Right PGA Mux", "Line", "RINPUT1"},
0339     {"Right PGA Mux", "Differential", "Differential Mux"},
0340 
0341     /* Differential Mux */
0342     {"Differential Mux", "Line", "LINPUT1"},
0343     {"Differential Mux", "Line", "RINPUT1"},
0344 
0345     /* Left ADC Mux */
0346     {"Left ADC Mux", "Stereo", "Left PGA Mux"},
0347     {"Left ADC Mux", "Mono (Left)", "Left PGA Mux"},
0348     {"Left ADC Mux", "Digital Mono", "Left PGA Mux"},
0349 
0350     /* Right ADC Mux */
0351     {"Right ADC Mux", "Stereo", "Right PGA Mux"},
0352     {"Right ADC Mux", "Mono (Right)", "Right PGA Mux"},
0353     {"Right ADC Mux", "Digital Mono", "Right PGA Mux"},
0354 
0355     /* ADC */
0356     {"Left ADC", NULL, "Left ADC Mux"},
0357     {"Right ADC", NULL, "Right ADC Mux"},
0358 };
0359 
0360 struct _coeff_div {
0361     u32 mclk;
0362     u32 rate;
0363     u16 fs;
0364     u8 sr:5;
0365     u8 usb:1;
0366 };
0367 
0368 /* codec hifi mclk clock divider coefficients */
0369 static const struct _coeff_div coeff_div[] = {
0370     /* 8k */
0371     {12288000, 8000, 1536, 0x6, 0x0},
0372     {11289600, 8000, 1408, 0x16, 0x0},
0373     {18432000, 8000, 2304, 0x7, 0x0},
0374     {16934400, 8000, 2112, 0x17, 0x0},
0375     {12000000, 8000, 1500, 0x6, 0x1},
0376 
0377     /* 11.025k */
0378     {11289600, 11025, 1024, 0x18, 0x0},
0379     {16934400, 11025, 1536, 0x19, 0x0},
0380     {12000000, 11025, 1088, 0x19, 0x1},
0381 
0382     /* 16k */
0383     {12288000, 16000, 768, 0xa, 0x0},
0384     {18432000, 16000, 1152, 0xb, 0x0},
0385     {12000000, 16000, 750, 0xa, 0x1},
0386 
0387     /* 22.05k */
0388     {11289600, 22050, 512, 0x1a, 0x0},
0389     {16934400, 22050, 768, 0x1b, 0x0},
0390     {12000000, 22050, 544, 0x1b, 0x1},
0391 
0392     /* 32k */
0393     {12288000, 32000, 384, 0xc, 0x0},
0394     {18432000, 32000, 576, 0xd, 0x0},
0395     {12000000, 32000, 375, 0xa, 0x1},
0396 
0397     /* 44.1k */
0398     {11289600, 44100, 256, 0x10, 0x0},
0399     {16934400, 44100, 384, 0x11, 0x0},
0400     {12000000, 44100, 272, 0x11, 0x1},
0401 
0402     /* 48k */
0403     {12288000, 48000, 256, 0x0, 0x0},
0404     {18432000, 48000, 384, 0x1, 0x0},
0405     {12000000, 48000, 250, 0x0, 0x1},
0406 
0407     /* 88.2k */
0408     {11289600, 88200, 128, 0x1e, 0x0},
0409     {16934400, 88200, 192, 0x1f, 0x0},
0410     {12000000, 88200, 136, 0x1f, 0x1},
0411 
0412     /* 96k */
0413     {12288000, 96000, 128, 0xe, 0x0},
0414     {18432000, 96000, 192, 0xf, 0x0},
0415     {12000000, 96000, 125, 0xe, 0x1},
0416 };
0417 
0418 static int get_coeff(int mclk, int rate)
0419 {
0420     int i;
0421 
0422     for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
0423         if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
0424             return i;
0425     }
0426     return -EINVAL;
0427 }
0428 
0429 static int wm8971_set_dai_sysclk(struct snd_soc_dai *codec_dai,
0430         int clk_id, unsigned int freq, int dir)
0431 {
0432     struct snd_soc_component *component = codec_dai->component;
0433     struct wm8971_priv *wm8971 = snd_soc_component_get_drvdata(component);
0434 
0435     switch (freq) {
0436     case 11289600:
0437     case 12000000:
0438     case 12288000:
0439     case 16934400:
0440     case 18432000:
0441         wm8971->sysclk = freq;
0442         return 0;
0443     }
0444     return -EINVAL;
0445 }
0446 
0447 static int wm8971_set_dai_fmt(struct snd_soc_dai *codec_dai,
0448         unsigned int fmt)
0449 {
0450     struct snd_soc_component *component = codec_dai->component;
0451     u16 iface = 0;
0452 
0453     /* set master/slave audio interface */
0454     switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
0455     case SND_SOC_DAIFMT_CBM_CFM:
0456         iface = 0x0040;
0457         break;
0458     case SND_SOC_DAIFMT_CBS_CFS:
0459         break;
0460     default:
0461         return -EINVAL;
0462     }
0463 
0464     /* interface format */
0465     switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
0466     case SND_SOC_DAIFMT_I2S:
0467         iface |= 0x0002;
0468         break;
0469     case SND_SOC_DAIFMT_RIGHT_J:
0470         break;
0471     case SND_SOC_DAIFMT_LEFT_J:
0472         iface |= 0x0001;
0473         break;
0474     case SND_SOC_DAIFMT_DSP_A:
0475         iface |= 0x0003;
0476         break;
0477     case SND_SOC_DAIFMT_DSP_B:
0478         iface |= 0x0013;
0479         break;
0480     default:
0481         return -EINVAL;
0482     }
0483 
0484     /* clock inversion */
0485     switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
0486     case SND_SOC_DAIFMT_NB_NF:
0487         break;
0488     case SND_SOC_DAIFMT_IB_IF:
0489         iface |= 0x0090;
0490         break;
0491     case SND_SOC_DAIFMT_IB_NF:
0492         iface |= 0x0080;
0493         break;
0494     case SND_SOC_DAIFMT_NB_IF:
0495         iface |= 0x0010;
0496         break;
0497     default:
0498         return -EINVAL;
0499     }
0500 
0501     snd_soc_component_write(component, WM8971_IFACE, iface);
0502     return 0;
0503 }
0504 
0505 static int wm8971_pcm_hw_params(struct snd_pcm_substream *substream,
0506     struct snd_pcm_hw_params *params,
0507     struct snd_soc_dai *dai)
0508 {
0509     struct snd_soc_component *component = dai->component;
0510     struct wm8971_priv *wm8971 = snd_soc_component_get_drvdata(component);
0511     u16 iface = snd_soc_component_read(component, WM8971_IFACE) & 0x1f3;
0512     u16 srate = snd_soc_component_read(component, WM8971_SRATE) & 0x1c0;
0513     int coeff = get_coeff(wm8971->sysclk, params_rate(params));
0514 
0515     /* bit size */
0516     switch (params_width(params)) {
0517     case 16:
0518         break;
0519     case 20:
0520         iface |= 0x0004;
0521         break;
0522     case 24:
0523         iface |= 0x0008;
0524         break;
0525     case 32:
0526         iface |= 0x000c;
0527         break;
0528     }
0529 
0530     /* set iface & srate */
0531     snd_soc_component_write(component, WM8971_IFACE, iface);
0532     if (coeff >= 0)
0533         snd_soc_component_write(component, WM8971_SRATE, srate |
0534             (coeff_div[coeff].sr << 1) | coeff_div[coeff].usb);
0535 
0536     return 0;
0537 }
0538 
0539 static int wm8971_mute(struct snd_soc_dai *dai, int mute, int direction)
0540 {
0541     struct snd_soc_component *component = dai->component;
0542     u16 mute_reg = snd_soc_component_read(component, WM8971_ADCDAC) & 0xfff7;
0543 
0544     if (mute)
0545         snd_soc_component_write(component, WM8971_ADCDAC, mute_reg | 0x8);
0546     else
0547         snd_soc_component_write(component, WM8971_ADCDAC, mute_reg);
0548     return 0;
0549 }
0550 
0551 static void wm8971_charge_work(struct work_struct *work)
0552 {
0553     struct wm8971_priv *wm8971 =
0554         container_of(work, struct wm8971_priv, charge_work.work);
0555 
0556     /* Set to 500k */
0557     regmap_update_bits(wm8971->regmap, WM8971_PWR1, 0x0180, 0x0100);
0558 }
0559 
0560 static int wm8971_set_bias_level(struct snd_soc_component *component,
0561     enum snd_soc_bias_level level)
0562 {
0563     struct wm8971_priv *wm8971 = snd_soc_component_get_drvdata(component);
0564     u16 pwr_reg = snd_soc_component_read(component, WM8971_PWR1) & 0xfe3e;
0565 
0566     switch (level) {
0567     case SND_SOC_BIAS_ON:
0568         /* set vmid to 50k and unmute dac */
0569         snd_soc_component_write(component, WM8971_PWR1, pwr_reg | 0x00c1);
0570         break;
0571     case SND_SOC_BIAS_PREPARE:
0572         /* Wait until fully charged */
0573         flush_delayed_work(&wm8971->charge_work);
0574         break;
0575     case SND_SOC_BIAS_STANDBY:
0576         if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
0577             snd_soc_component_cache_sync(component);
0578             /* charge output caps - set vmid to 5k for quick power up */
0579             snd_soc_component_write(component, WM8971_PWR1, pwr_reg | 0x01c0);
0580             queue_delayed_work(system_power_efficient_wq,
0581                 &wm8971->charge_work, msecs_to_jiffies(1000));
0582         } else {
0583             /* mute dac and set vmid to 500k, enable VREF */
0584             snd_soc_component_write(component, WM8971_PWR1, pwr_reg | 0x0140);
0585         }
0586 
0587         break;
0588     case SND_SOC_BIAS_OFF:
0589         cancel_delayed_work_sync(&wm8971->charge_work);
0590         snd_soc_component_write(component, WM8971_PWR1, 0x0001);
0591         break;
0592     }
0593     return 0;
0594 }
0595 
0596 #define WM8971_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
0597         SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
0598         SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
0599 
0600 #define WM8971_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
0601     SNDRV_PCM_FMTBIT_S24_LE)
0602 
0603 static const struct snd_soc_dai_ops wm8971_dai_ops = {
0604     .hw_params  = wm8971_pcm_hw_params,
0605     .mute_stream    = wm8971_mute,
0606     .set_fmt    = wm8971_set_dai_fmt,
0607     .set_sysclk = wm8971_set_dai_sysclk,
0608     .no_capture_mute = 1,
0609 };
0610 
0611 static struct snd_soc_dai_driver wm8971_dai = {
0612     .name = "wm8971-hifi",
0613     .playback = {
0614         .stream_name = "Playback",
0615         .channels_min = 1,
0616         .channels_max = 2,
0617         .rates = WM8971_RATES,
0618         .formats = WM8971_FORMATS,},
0619     .capture = {
0620         .stream_name = "Capture",
0621         .channels_min = 1,
0622         .channels_max = 2,
0623         .rates = WM8971_RATES,
0624         .formats = WM8971_FORMATS,},
0625     .ops = &wm8971_dai_ops,
0626 };
0627 
0628 static int wm8971_probe(struct snd_soc_component *component)
0629 {
0630     struct wm8971_priv *wm8971 = snd_soc_component_get_drvdata(component);
0631 
0632     INIT_DELAYED_WORK(&wm8971->charge_work, wm8971_charge_work);
0633 
0634     wm8971_reset(component);
0635 
0636     /* set the update bits */
0637     snd_soc_component_update_bits(component, WM8971_LDAC, 0x0100, 0x0100);
0638     snd_soc_component_update_bits(component, WM8971_RDAC, 0x0100, 0x0100);
0639     snd_soc_component_update_bits(component, WM8971_LOUT1V, 0x0100, 0x0100);
0640     snd_soc_component_update_bits(component, WM8971_ROUT1V, 0x0100, 0x0100);
0641     snd_soc_component_update_bits(component, WM8971_LOUT2V, 0x0100, 0x0100);
0642     snd_soc_component_update_bits(component, WM8971_ROUT2V, 0x0100, 0x0100);
0643     snd_soc_component_update_bits(component, WM8971_LINVOL, 0x0100, 0x0100);
0644     snd_soc_component_update_bits(component, WM8971_RINVOL, 0x0100, 0x0100);
0645 
0646     return 0;
0647 }
0648 
0649 static const struct snd_soc_component_driver soc_component_dev_wm8971 = {
0650     .probe          = wm8971_probe,
0651     .set_bias_level     = wm8971_set_bias_level,
0652     .controls       = wm8971_snd_controls,
0653     .num_controls       = ARRAY_SIZE(wm8971_snd_controls),
0654     .dapm_widgets       = wm8971_dapm_widgets,
0655     .num_dapm_widgets   = ARRAY_SIZE(wm8971_dapm_widgets),
0656     .dapm_routes        = wm8971_dapm_routes,
0657     .num_dapm_routes    = ARRAY_SIZE(wm8971_dapm_routes),
0658     .suspend_bias_off   = 1,
0659     .idle_bias_on       = 1,
0660     .use_pmdown_time    = 1,
0661     .endianness     = 1,
0662 };
0663 
0664 static const struct regmap_config wm8971_regmap = {
0665     .reg_bits = 7,
0666     .val_bits = 9,
0667     .max_register = WM8971_MOUTV,
0668 
0669     .reg_defaults = wm8971_reg_defaults,
0670     .num_reg_defaults = ARRAY_SIZE(wm8971_reg_defaults),
0671     .cache_type = REGCACHE_RBTREE,
0672 };
0673 
0674 static int wm8971_i2c_probe(struct i2c_client *i2c)
0675 {
0676     struct wm8971_priv *wm8971;
0677 
0678     wm8971 = devm_kzalloc(&i2c->dev, sizeof(struct wm8971_priv),
0679                   GFP_KERNEL);
0680     if (wm8971 == NULL)
0681         return -ENOMEM;
0682 
0683     wm8971->regmap = devm_regmap_init_i2c(i2c, &wm8971_regmap);
0684     if (IS_ERR(wm8971->regmap))
0685         return PTR_ERR(wm8971->regmap);
0686 
0687     i2c_set_clientdata(i2c, wm8971);
0688 
0689     return devm_snd_soc_register_component(&i2c->dev,
0690             &soc_component_dev_wm8971, &wm8971_dai, 1);
0691 }
0692 
0693 static const struct i2c_device_id wm8971_i2c_id[] = {
0694     { "wm8971", 0 },
0695     { }
0696 };
0697 MODULE_DEVICE_TABLE(i2c, wm8971_i2c_id);
0698 
0699 static struct i2c_driver wm8971_i2c_driver = {
0700     .driver = {
0701         .name = "wm8971",
0702     },
0703     .probe_new = wm8971_i2c_probe,
0704     .id_table = wm8971_i2c_id,
0705 };
0706 
0707 module_i2c_driver(wm8971_i2c_driver);
0708 
0709 MODULE_DESCRIPTION("ASoC WM8971 driver");
0710 MODULE_AUTHOR("Lab126");
0711 MODULE_LICENSE("GPL");