File: sun8i-codec-analog.c - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

0001: /*
0002:  * This driver supports the analog controls for the internal codec
0003:  * found in Allwinner's A31s, A23, A33 and H3 SoCs.
0004:  *
0005:  * Copyright 2016 Chen-Yu Tsai <wens@csie.org>
0006:  *
0007:  * This program is free software; you can redistribute it and/or modify
0008:  * it under the terms of the GNU General Public License as published by
0009:  * the Free Software Foundation; either version 2 of the License, or
0010:  * (at your option) any later version.
0011:  *
0012:  * This program is distributed in the hope that it will be useful,
0013:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015:  * GNU General Public License for more details.
0016:  */
0017: 
0018: #include <linux/io.h>
0019: #include <linux/kernel.h>
0020: #include <linux/module.h>
0021: #include <linux/of.h>
0022: #include <linux/of_device.h>
0023: #include <linux/platform_device.h>
0024: #include <linux/regmap.h>
0025: 
0026: #include <sound/soc.h>
0027: #include <sound/soc-dapm.h>
0028: #include <sound/tlv.h>
0029: 
0030: #include <linux/gpio.h>
0031: #include <linux/gpio/consumer.h>
0032: 
0033: #include "sun8i-adda-pr-regmap.h"
0034: 
0035: /* Codec analog control register offsets and bit fields */
0036: #define SUN8I_ADDA_HP_VOLC              0x00
0037: #define SUN8I_ADDA_HP_VOLC_PA_CLK_GATE          7
0038: #define SUN8I_ADDA_HP_VOLC_HP_VOL               0
0039: #define SUN8I_ADDA_LOMIXSC              0x01
0040: #define SUN8I_ADDA_LOMIXSC_MIC1                 6
0041: #define SUN8I_ADDA_LOMIXSC_MIC2                 5
0042: #define SUN8I_ADDA_LOMIXSC_PHONE                4
0043: #define SUN8I_ADDA_LOMIXSC_PHONEN               3
0044: #define SUN8I_ADDA_LOMIXSC_LINEINL              2
0045: #define SUN8I_ADDA_LOMIXSC_DACL                 1
0046: #define SUN8I_ADDA_LOMIXSC_DACR                 0
0047: #define SUN8I_ADDA_ROMIXSC              0x02
0048: #define SUN8I_ADDA_ROMIXSC_MIC1                 6
0049: #define SUN8I_ADDA_ROMIXSC_MIC2                 5
0050: #define SUN8I_ADDA_ROMIXSC_PHONE                4
0051: #define SUN8I_ADDA_ROMIXSC_PHONEP               3
0052: #define SUN8I_ADDA_ROMIXSC_LINEINR              2
0053: #define SUN8I_ADDA_ROMIXSC_DACR                 1
0054: #define SUN8I_ADDA_ROMIXSC_DACL                 0
0055: #define SUN8I_ADDA_DAC_PA_SRC           0x03
0056: #define SUN8I_ADDA_DAC_PA_SRC_DACAREN           7
0057: #define SUN8I_ADDA_DAC_PA_SRC_DACALEN           6
0058: #define SUN8I_ADDA_DAC_PA_SRC_RMIXEN            5
0059: #define SUN8I_ADDA_DAC_PA_SRC_LMIXEN            4
0060: #define SUN8I_ADDA_DAC_PA_SRC_RHPPAMUTE         3
0061: #define SUN8I_ADDA_DAC_PA_SRC_LHPPAMUTE         2
0062: #define SUN8I_ADDA_DAC_PA_SRC_RHPIS             1
0063: #define SUN8I_ADDA_DAC_PA_SRC_LHPIS             0
0064: #define SUN8I_ADDA_PHONEIN_GCTRL        0x04
0065: #define SUN8I_ADDA_PHONEIN_GCTRL_PHONEPG        4
0066: #define SUN8I_ADDA_PHONEIN_GCTRL_PHONENG        0
0067: #define SUN8I_ADDA_LINEIN_GCTRL         0x05
0068: #define SUN8I_ADDA_LINEIN_GCTRL_LINEING         4
0069: #define SUN8I_ADDA_LINEIN_GCTRL_PHONEG          0
0070: #define SUN8I_ADDA_MICIN_GCTRL          0x06
0071: #define SUN8I_ADDA_MICIN_GCTRL_MIC1G            4
0072: #define SUN8I_ADDA_MICIN_GCTRL_MIC2G            0
0073: #define SUN8I_ADDA_PAEN_HP_CTRL         0x07
0074: #define SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN          7
0075: #define SUN8I_ADDA_PAEN_HP_CTRL_LINEOUTEN       7       /* H3 specific */
0076: #define SUN8I_ADDA_PAEN_HP_CTRL_HPCOM_FC        5
0077: #define SUN8I_ADDA_PAEN_HP_CTRL_COMPTEN         4
0078: #define SUN8I_ADDA_PAEN_HP_CTRL_PA_ANTI_POP_CTRL        2
0079: #define SUN8I_ADDA_PAEN_HP_CTRL_LTRNMUTE        1
0080: #define SUN8I_ADDA_PAEN_HP_CTRL_RTLNMUTE        0
0081: #define SUN8I_ADDA_PHONEOUT_CTRL        0x08
0082: #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUTG      5
0083: #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUTEN     4
0084: #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_MIC1  3
0085: #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_MIC2  2
0086: #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_RMIX  1
0087: #define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_LMIX  0
0088: #define SUN8I_ADDA_PHONE_GAIN_CTRL      0x09
0089: #define SUN8I_ADDA_PHONE_GAIN_CTRL_LINEOUT_VOL  3
0090: #define SUN8I_ADDA_PHONE_GAIN_CTRL_PHONEPREG    0
0091: #define SUN8I_ADDA_MIC2G_CTRL           0x0a
0092: #define SUN8I_ADDA_MIC2G_CTRL_MIC2AMPEN         7
0093: #define SUN8I_ADDA_MIC2G_CTRL_MIC2BOOST         4
0094: #define SUN8I_ADDA_MIC2G_CTRL_LINEOUTLEN        3
0095: #define SUN8I_ADDA_MIC2G_CTRL_LINEOUTREN        2
0096: #define SUN8I_ADDA_MIC2G_CTRL_LINEOUTLSRC       1
0097: #define SUN8I_ADDA_MIC2G_CTRL_LINEOUTRSRC       0
0098: #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL   0x0b
0099: #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_HMICBIASEN        7
0100: #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MMICBIASEN        6
0101: #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_HMICBIAS_MODE     5
0102: #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1AMPEN         3
0103: #define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1BOOST         0
0104: #define SUN8I_ADDA_LADCMIXSC            0x0c
0105: #define SUN8I_ADDA_LADCMIXSC_MIC1               6
0106: #define SUN8I_ADDA_LADCMIXSC_MIC2               5
0107: #define SUN8I_ADDA_LADCMIXSC_PHONE              4
0108: #define SUN8I_ADDA_LADCMIXSC_PHONEN             3
0109: #define SUN8I_ADDA_LADCMIXSC_LINEINL            2
0110: #define SUN8I_ADDA_LADCMIXSC_OMIXRL             1
0111: #define SUN8I_ADDA_LADCMIXSC_OMIXRR             0
0112: #define SUN8I_ADDA_RADCMIXSC            0x0d
0113: #define SUN8I_ADDA_RADCMIXSC_MIC1               6
0114: #define SUN8I_ADDA_RADCMIXSC_MIC2               5
0115: #define SUN8I_ADDA_RADCMIXSC_PHONE              4
0116: #define SUN8I_ADDA_RADCMIXSC_PHONEP             3
0117: #define SUN8I_ADDA_RADCMIXSC_LINEINR            2
0118: #define SUN8I_ADDA_RADCMIXSC_OMIXR              1
0119: #define SUN8I_ADDA_RADCMIXSC_OMIXL              0
0120: #define SUN8I_ADDA_RES                  0x0e
0121: #define SUN8I_ADDA_RES_MMICBIAS_SEL             4
0122: #define SUN8I_ADDA_RES_PA_ANTI_POP_CTRL         0
0123: #define SUN8I_ADDA_ADC_AP_EN            0x0f
0124: #define SUN8I_ADDA_ADC_AP_EN_ADCREN             7
0125: #define SUN8I_ADDA_ADC_AP_EN_ADCLEN             6
0126: #define SUN8I_ADDA_ADC_AP_EN_ADCG               0
0127: 
0128: /* mixer controls */
0129: static const struct snd_kcontrol_new sun8i_codec_mixer_controls[] = {
0130:         SOC_DAPM_DOUBLE_R("DAC Playback Switch",
0131:                           SUN8I_ADDA_LOMIXSC,
0132:                           SUN8I_ADDA_ROMIXSC,
0133:                           SUN8I_ADDA_LOMIXSC_DACL, 1, 0),
0134:         SOC_DAPM_DOUBLE_R("DAC Reversed Playback Switch",
0135:                           SUN8I_ADDA_LOMIXSC,
0136:                           SUN8I_ADDA_ROMIXSC,
0137:                           SUN8I_ADDA_LOMIXSC_DACR, 1, 0),
0138:         SOC_DAPM_DOUBLE_R("Line In Playback Switch",
0139:                           SUN8I_ADDA_LOMIXSC,
0140:                           SUN8I_ADDA_ROMIXSC,
0141:                           SUN8I_ADDA_LOMIXSC_LINEINL, 1, 0),
0142:         SOC_DAPM_DOUBLE_R("Mic1 Playback Switch",
0143:                           SUN8I_ADDA_LOMIXSC,
0144:                           SUN8I_ADDA_ROMIXSC,
0145:                           SUN8I_ADDA_LOMIXSC_MIC1, 1, 0),
0146:         SOC_DAPM_DOUBLE_R("Mic2 Playback Switch",
0147:                           SUN8I_ADDA_LOMIXSC,
0148:                           SUN8I_ADDA_ROMIXSC,
0149:                           SUN8I_ADDA_LOMIXSC_MIC2, 1, 0),
0150: };
0151: 
0152: /* mixer controls */
0153: static const struct snd_kcontrol_new sun8i_v3s_codec_mixer_controls[] = {
0154:         SOC_DAPM_DOUBLE_R("DAC Playback Switch",
0155:                           SUN8I_ADDA_LOMIXSC,
0156:                           SUN8I_ADDA_ROMIXSC,
0157:                           SUN8I_ADDA_LOMIXSC_DACL, 1, 0),
0158:         SOC_DAPM_DOUBLE_R("DAC Reversed Playback Switch",
0159:                           SUN8I_ADDA_LOMIXSC,
0160:                           SUN8I_ADDA_ROMIXSC,
0161:                           SUN8I_ADDA_LOMIXSC_DACR, 1, 0),
0162:         SOC_DAPM_DOUBLE_R("Mic1 Playback Switch",
0163:                           SUN8I_ADDA_LOMIXSC,
0164:                           SUN8I_ADDA_ROMIXSC,
0165:                           SUN8I_ADDA_LOMIXSC_MIC1, 1, 0),
0166: };
0167: 
0168: /* ADC mixer controls */
0169: static const struct snd_kcontrol_new sun8i_codec_adc_mixer_controls[] = {
0170:         SOC_DAPM_DOUBLE_R("Mixer Capture Switch",
0171:                           SUN8I_ADDA_LADCMIXSC,
0172:                           SUN8I_ADDA_RADCMIXSC,
0173:                           SUN8I_ADDA_LADCMIXSC_OMIXRL, 1, 0),
0174:         SOC_DAPM_DOUBLE_R("Mixer Reversed Capture Switch",
0175:                           SUN8I_ADDA_LADCMIXSC,
0176:                           SUN8I_ADDA_RADCMIXSC,
0177:                           SUN8I_ADDA_LADCMIXSC_OMIXRR, 1, 0),
0178:         SOC_DAPM_DOUBLE_R("Line In Capture Switch",
0179:                           SUN8I_ADDA_LADCMIXSC,
0180:                           SUN8I_ADDA_RADCMIXSC,
0181:                           SUN8I_ADDA_LADCMIXSC_LINEINL, 1, 0),
0182:         SOC_DAPM_DOUBLE_R("Mic1 Capture Switch",
0183:                           SUN8I_ADDA_LADCMIXSC,
0184:                           SUN8I_ADDA_RADCMIXSC,
0185:                           SUN8I_ADDA_LADCMIXSC_MIC1, 1, 0),
0186:         SOC_DAPM_DOUBLE_R("Mic2 Capture Switch",
0187:                           SUN8I_ADDA_LADCMIXSC,
0188:                           SUN8I_ADDA_RADCMIXSC,
0189:                           SUN8I_ADDA_LADCMIXSC_MIC2, 1, 0),
0190: };
0191: 
0192: /* ADC mixer controls */
0193: static const struct snd_kcontrol_new sun8i_v3s_codec_adc_mixer_controls[] = {
0194:         SOC_DAPM_DOUBLE_R("Mixer Capture Switch",
0195:                           SUN8I_ADDA_LADCMIXSC,
0196:                           SUN8I_ADDA_RADCMIXSC,
0197:                           SUN8I_ADDA_LADCMIXSC_OMIXRL, 1, 0),
0198:         SOC_DAPM_DOUBLE_R("Mixer Reversed Capture Switch",
0199:                           SUN8I_ADDA_LADCMIXSC,
0200:                           SUN8I_ADDA_RADCMIXSC,
0201:                           SUN8I_ADDA_LADCMIXSC_OMIXRR, 1, 0),
0202:         SOC_DAPM_DOUBLE_R("Mic1 Capture Switch",
0203:                           SUN8I_ADDA_LADCMIXSC,
0204:                           SUN8I_ADDA_RADCMIXSC,
0205:                           SUN8I_ADDA_LADCMIXSC_MIC1, 1, 0),
0206: };
0207: 
0208: /* volume / mute controls */
0209: static const DECLARE_TLV_DB_SCALE(sun8i_codec_out_mixer_pregain_scale,
0210:                                   -450, 150, 0);
0211: static const DECLARE_TLV_DB_RANGE(sun8i_codec_mic_gain_scale,
0212:         0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
0213:         1, 7, TLV_DB_SCALE_ITEM(2400, 300, 0),
0214: );
0215: 
0216: static const struct snd_kcontrol_new sun8i_codec_common_controls[] = {
0217:         /* Mixer pre-gain */
0218:         SOC_SINGLE_TLV("Mic1 Playback Volume", SUN8I_ADDA_MICIN_GCTRL,
0219:                        SUN8I_ADDA_MICIN_GCTRL_MIC1G,
0220:                        0x7, 0, sun8i_codec_out_mixer_pregain_scale),
0221: 
0222:         /* Microphone Amp boost gain */
0223:         SOC_SINGLE_TLV("Mic1 Boost Volume", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
0224:                        SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1BOOST, 0x7, 0,
0225:                        sun8i_codec_mic_gain_scale),
0226: 
0227:         /* ADC */
0228:         SOC_SINGLE_TLV("ADC Gain Capture Volume", SUN8I_ADDA_ADC_AP_EN,
0229:                        SUN8I_ADDA_ADC_AP_EN_ADCG, 0x7, 0,
0230:                        sun8i_codec_out_mixer_pregain_scale),
0231: };
0232: 
0233: static const struct snd_soc_dapm_widget sun8i_codec_common_widgets[] = {
0234:         /* ADC */
0235:         SND_SOC_DAPM_ADC("Left ADC", NULL, SUN8I_ADDA_ADC_AP_EN,
0236:                          SUN8I_ADDA_ADC_AP_EN_ADCLEN, 0),
0237:         SND_SOC_DAPM_ADC("Right ADC", NULL, SUN8I_ADDA_ADC_AP_EN,
0238:                          SUN8I_ADDA_ADC_AP_EN_ADCREN, 0),
0239: 
0240:         /* DAC */
0241:         SND_SOC_DAPM_DAC("Left DAC", NULL, SUN8I_ADDA_DAC_PA_SRC,
0242:                          SUN8I_ADDA_DAC_PA_SRC_DACALEN, 0),
0243:         SND_SOC_DAPM_DAC("Right DAC", NULL, SUN8I_ADDA_DAC_PA_SRC,
0244:                          SUN8I_ADDA_DAC_PA_SRC_DACAREN, 0),
0245:         /*
0246:          * Due to this component and the codec belonging to separate DAPM
0247:          * contexts, we need to manually link the above widgets to their
0248:          * stream widgets at the card level.
0249:          */
0250: 
0251:         /* Microphone input */
0252:         SND_SOC_DAPM_INPUT("MIC1"),
0253: 
0254:         /* Mic input path */
0255:         SND_SOC_DAPM_PGA("Mic1 Amplifier", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
0256:                          SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1AMPEN, 0, NULL, 0),
0257: };
0258: 
0259: static const struct snd_soc_dapm_widget sun8i_codec_mixer_widgets[] = {
0260:         SND_SOC_DAPM_MIXER("Left Mixer", SUN8I_ADDA_DAC_PA_SRC,
0261:                            SUN8I_ADDA_DAC_PA_SRC_LMIXEN, 0,
0262:                            sun8i_codec_mixer_controls,
0263:                            ARRAY_SIZE(sun8i_codec_mixer_controls)),
0264:         SND_SOC_DAPM_MIXER("Right Mixer", SUN8I_ADDA_DAC_PA_SRC,
0265:                            SUN8I_ADDA_DAC_PA_SRC_RMIXEN, 0,
0266:                            sun8i_codec_mixer_controls,
0267:                            ARRAY_SIZE(sun8i_codec_mixer_controls)),
0268:         SND_SOC_DAPM_MIXER("Left ADC Mixer", SUN8I_ADDA_ADC_AP_EN,
0269:                            SUN8I_ADDA_ADC_AP_EN_ADCLEN, 0,
0270:                            sun8i_codec_adc_mixer_controls,
0271:                            ARRAY_SIZE(sun8i_codec_adc_mixer_controls)),
0272:         SND_SOC_DAPM_MIXER("Right ADC Mixer", SUN8I_ADDA_ADC_AP_EN,
0273:                            SUN8I_ADDA_ADC_AP_EN_ADCREN, 0,
0274:                            sun8i_codec_adc_mixer_controls,
0275:                            ARRAY_SIZE(sun8i_codec_adc_mixer_controls)),
0276: };
0277: 
0278: static const struct snd_soc_dapm_widget sun8i_v3s_codec_mixer_widgets[] = {
0279:         SND_SOC_DAPM_MIXER("Left Mixer", SUN8I_ADDA_DAC_PA_SRC,
0280:                            SUN8I_ADDA_DAC_PA_SRC_LMIXEN, 0,
0281:                            sun8i_v3s_codec_mixer_controls,
0282:                            ARRAY_SIZE(sun8i_v3s_codec_mixer_controls)),
0283:         SND_SOC_DAPM_MIXER("Right Mixer", SUN8I_ADDA_DAC_PA_SRC,
0284:                            SUN8I_ADDA_DAC_PA_SRC_RMIXEN, 0,
0285:                            sun8i_v3s_codec_mixer_controls,
0286:                            ARRAY_SIZE(sun8i_v3s_codec_mixer_controls)),
0287:         SND_SOC_DAPM_MIXER("Left ADC Mixer", SUN8I_ADDA_ADC_AP_EN,
0288:                            SUN8I_ADDA_ADC_AP_EN_ADCLEN, 0,
0289:                            sun8i_v3s_codec_adc_mixer_controls,
0290:                            ARRAY_SIZE(sun8i_v3s_codec_adc_mixer_controls)),
0291:         SND_SOC_DAPM_MIXER("Right ADC Mixer", SUN8I_ADDA_ADC_AP_EN,
0292:                            SUN8I_ADDA_ADC_AP_EN_ADCREN, 0,
0293:                            sun8i_v3s_codec_adc_mixer_controls,
0294:                            ARRAY_SIZE(sun8i_v3s_codec_adc_mixer_controls)),
0295: };
0296: 
0297: static const struct snd_soc_dapm_route sun8i_codec_common_routes[] = {
0298:         /* Microphone Routes */
0299:         { "Mic1 Amplifier", NULL, "MIC1"},
0300: };
0301: 
0302: static const struct snd_soc_dapm_route sun8i_codec_mixer_routes[] = {
0303:         /* Left Mixer Routes */
0304:         { "Left Mixer", "DAC Playback Switch", "Left DAC" },
0305:         { "Left Mixer", "DAC Reversed Playback Switch", "Right DAC" },
0306:         { "Left Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
0307: 
0308:         /* Right Mixer Routes */
0309:         { "Right Mixer", "DAC Playback Switch", "Right DAC" },
0310:         { "Right Mixer", "DAC Reversed Playback Switch", "Left DAC" },
0311:         { "Right Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
0312: 
0313:         /* Left ADC Mixer Routes */
0314:         { "Left ADC Mixer", "Mixer Capture Switch", "Left Mixer" },
0315:         { "Left ADC Mixer", "Mixer Reversed Capture Switch", "Right Mixer" },
0316:         { "Left ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
0317: 
0318:         /* Right ADC Mixer Routes */
0319:         { "Right ADC Mixer", "Mixer Capture Switch", "Right Mixer" },
0320:         { "Right ADC Mixer", "Mixer Reversed Capture Switch", "Left Mixer" },
0321:         { "Right ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
0322: 
0323:         /* ADC Routes */
0324:         { "Left ADC", NULL, "Left ADC Mixer" },
0325:         { "Right ADC", NULL, "Right ADC Mixer" },
0326: };
0327: 
0328: /* headphone specific controls, widgets, and routes */
0329: static const DECLARE_TLV_DB_SCALE(sun8i_codec_hp_vol_scale, -6300, 100, 1);
0330: static const struct snd_kcontrol_new sun8i_codec_headphone_controls[] = {
0331:         SOC_SINGLE_TLV("Master Playback Volume",
0332:                        SUN8I_ADDA_HP_VOLC,
0333:                        SUN8I_ADDA_HP_VOLC_HP_VOL, 0x3f, 0,
0334:                        sun8i_codec_hp_vol_scale),
0335:         SOC_DOUBLE("Headphone Playback Switch",
0336:                    SUN8I_ADDA_DAC_PA_SRC,
0337:                    SUN8I_ADDA_DAC_PA_SRC_LHPPAMUTE,
0338:                    SUN8I_ADDA_DAC_PA_SRC_RHPPAMUTE, 1, 0),
0339: };
0340: 
0341: static const char * const sun8i_codec_hp_src_enum_text[] = {
0342:         "DAC", "Mixer",
0343: };
0344: 
0345: static SOC_ENUM_DOUBLE_DECL(sun8i_codec_hp_src_enum,
0346:                             SUN8I_ADDA_DAC_PA_SRC,
0347:                             SUN8I_ADDA_DAC_PA_SRC_LHPIS,
0348:                             SUN8I_ADDA_DAC_PA_SRC_RHPIS,
0349:                             sun8i_codec_hp_src_enum_text);
0350: 
0351: static const struct snd_kcontrol_new sun8i_codec_hp_src[] = {
0352:         SOC_DAPM_ENUM("Headphone Source Playback Route",
0353:                       sun8i_codec_hp_src_enum),
0354: };
0355: 
0356: static int sun8i_headphone_amp_event(struct snd_soc_dapm_widget *w,
0357:                                      struct snd_kcontrol *k, int event)
0358: {
0359:         struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
0360: 
0361:         if (SND_SOC_DAPM_EVENT_ON(event)) {
0362:                 snd_soc_component_update_bits(component, SUN8I_ADDA_PAEN_HP_CTRL,
0363:                                               BIT(SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN),
0364:                                               BIT(SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN));
0365:                 /*
0366:                  * Need a delay to have the amplifier up. 700ms seems the best
0367:                  * compromise between the time to let the amplifier up and the
0368:                  * time not to feel this delay while playing a sound.
0369:                  */
0370:                 msleep(700);
0371:         } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
0372:                 snd_soc_component_update_bits(component, SUN8I_ADDA_PAEN_HP_CTRL,
0373:                                               BIT(SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN),
0374:                                               0x0);
0375:         }
0376: 
0377:         return 0;
0378: }
0379: 
0380: static const struct snd_soc_dapm_widget sun8i_codec_headphone_widgets[] = {
0381:         SND_SOC_DAPM_MUX("Headphone Source Playback Route",
0382:                          SND_SOC_NOPM, 0, 0, sun8i_codec_hp_src),
0383:         SND_SOC_DAPM_OUT_DRV_E("Headphone Amp", SUN8I_ADDA_PAEN_HP_CTRL,
0384:                                SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN, 0, NULL, 0,
0385:                                sun8i_headphone_amp_event,
0386:                                SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
0387:         SND_SOC_DAPM_SUPPLY("HPCOM Protection", SUN8I_ADDA_PAEN_HP_CTRL,
0388:                             SUN8I_ADDA_PAEN_HP_CTRL_COMPTEN, 0, NULL, 0),
0389:         SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPCOM", SUN8I_ADDA_PAEN_HP_CTRL,
0390:                          SUN8I_ADDA_PAEN_HP_CTRL_HPCOM_FC, 0x3, 0x3, 0),
0391:         SND_SOC_DAPM_OUTPUT("HP"),
0392: };
0393: 
0394: static const struct snd_soc_dapm_route sun8i_codec_headphone_routes[] = {
0395:         { "Headphone Source Playback Route", "DAC", "Left DAC" },
0396:         { "Headphone Source Playback Route", "DAC", "Right DAC" },
0397:         { "Headphone Source Playback Route", "Mixer", "Left Mixer" },
0398:         { "Headphone Source Playback Route", "Mixer", "Right Mixer" },
0399:         { "Headphone Amp", NULL, "Headphone Source Playback Route" },
0400:         { "HPCOM", NULL, "HPCOM Protection" },
0401:         { "HP", NULL, "Headphone Amp" },
0402: };
0403: 
0404: static int sun8i_codec_add_headphone(struct snd_soc_component *cmpnt)
0405: {
0406:         struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
0407:         struct device *dev = cmpnt->dev;
0408:         int ret;
0409: 
0410:         ret = snd_soc_add_component_controls(cmpnt,
0411:                                              sun8i_codec_headphone_controls,
0412:                                              ARRAY_SIZE(sun8i_codec_headphone_controls));
0413:         if (ret) {
0414:                 dev_err(dev, "Failed to add Headphone controls: %d\n", ret);
0415:                 return ret;
0416:         }
0417: 
0418:         ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_headphone_widgets,
0419:                                         ARRAY_SIZE(sun8i_codec_headphone_widgets));
0420:         if (ret) {
0421:                 dev_err(dev, "Failed to add Headphone DAPM widgets: %d\n", ret);
0422:                 return ret;
0423:         }
0424: 
0425:         ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_headphone_routes,
0426:                                       ARRAY_SIZE(sun8i_codec_headphone_routes));
0427:         if (ret) {
0428:                 dev_err(dev, "Failed to add Headphone DAPM routes: %d\n", ret);
0429:                 return ret;
0430:         }
0431: 
0432:         return 0;
0433: }
0434: 
0435: /* mbias specific widget */
0436: static const struct snd_soc_dapm_widget sun8i_codec_mbias_widgets[] = {
0437:         SND_SOC_DAPM_SUPPLY("MBIAS", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
0438:                             SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MMICBIASEN,
0439:                             0, NULL, 0),
0440: };
0441: 
0442: static int sun8i_codec_add_mbias(struct snd_soc_component *cmpnt)
0443: {
0444:         struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
0445:         struct device *dev = cmpnt->dev;
0446:         int ret;
0447: 
0448:         ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_mbias_widgets,
0449:                                         ARRAY_SIZE(sun8i_codec_mbias_widgets));
0450:         if (ret)
0451:                 dev_err(dev, "Failed to add MBIAS DAPM widgets: %d\n", ret);
0452: 
0453:         return ret;
0454: }
0455: 
0456: /* hmic specific widget */
0457: static const struct snd_soc_dapm_widget sun8i_codec_hmic_widgets[] = {
0458:         SND_SOC_DAPM_SUPPLY("HBIAS", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
0459:                             SUN8I_ADDA_MIC1G_MICBIAS_CTRL_HMICBIASEN,
0460:                             0, NULL, 0),
0461: };
0462: 
0463: static int sun8i_codec_add_hmic(struct snd_soc_component *cmpnt)
0464: {
0465:         struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
0466:         struct device *dev = cmpnt->dev;
0467:         int ret;
0468: 
0469:         ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_hmic_widgets,
0470:                                         ARRAY_SIZE(sun8i_codec_hmic_widgets));
0471:         if (ret)
0472:                 dev_err(dev, "Failed to add Mic3 DAPM widgets: %d\n", ret);
0473: 
0474:         return ret;
0475: }
0476: 
0477: /* line in specific controls, widgets and rines */
0478: static const struct snd_kcontrol_new sun8i_codec_linein_controls[] = {
0479:         /* Mixer pre-gain */
0480:         SOC_SINGLE_TLV("Line In Playback Volume", SUN8I_ADDA_LINEIN_GCTRL,
0481:                        SUN8I_ADDA_LINEIN_GCTRL_LINEING,
0482:                        0x7, 0, sun8i_codec_out_mixer_pregain_scale),
0483: };
0484: 
0485: static const struct snd_soc_dapm_widget sun8i_codec_linein_widgets[] = {
0486:         /* Line input */
0487:         SND_SOC_DAPM_INPUT("LINEIN"),
0488: };
0489: 
0490: static const struct snd_soc_dapm_route sun8i_codec_linein_routes[] = {
0491:         { "Left Mixer", "Line In Playback Switch", "LINEIN" },
0492: 
0493:         { "Right Mixer", "Line In Playback Switch", "LINEIN" },
0494: 
0495:         { "Left ADC Mixer", "Line In Capture Switch", "LINEIN" },
0496: 
0497:         { "Right ADC Mixer", "Line In Capture Switch", "LINEIN" },
0498: };
0499: 
0500: static int sun8i_codec_add_linein(struct snd_soc_component *cmpnt)
0501: {
0502:         struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
0503:         struct device *dev = cmpnt->dev;
0504:         int ret;
0505: 
0506:         ret = snd_soc_add_component_controls(cmpnt,
0507:                                              sun8i_codec_linein_controls,
0508:                                              ARRAY_SIZE(sun8i_codec_linein_controls));
0509:         if (ret) {
0510:                 dev_err(dev, "Failed to add Line In controls: %d\n", ret);
0511:                 return ret;
0512:         }
0513: 
0514:         ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_linein_widgets,
0515:                                         ARRAY_SIZE(sun8i_codec_linein_widgets));
0516:         if (ret) {
0517:                 dev_err(dev, "Failed to add Line In DAPM widgets: %d\n", ret);
0518:                 return ret;
0519:         }
0520: 
0521:         ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_linein_routes,
0522:                                       ARRAY_SIZE(sun8i_codec_linein_routes));
0523:         if (ret) {
0524:                 dev_err(dev, "Failed to add Line In DAPM routes: %d\n", ret);
0525:                 return ret;
0526:         }
0527: 
0528:         return 0;
0529: }
0530: 
0531: 
0532: /* line out specific controls, widgets and routes */
0533: static const DECLARE_TLV_DB_RANGE(sun8i_codec_lineout_vol_scale,
0534:         0, 1, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
0535:         2, 31, TLV_DB_SCALE_ITEM(-4350, 150, 0),
0536: );
0537: static const struct snd_kcontrol_new sun8i_codec_lineout_controls[] = {
0538:         SOC_SINGLE_TLV("Line Out Playback Volume",
0539:                        SUN8I_ADDA_PHONE_GAIN_CTRL,
0540:                        SUN8I_ADDA_PHONE_GAIN_CTRL_LINEOUT_VOL, 0x1f, 0,
0541:                        sun8i_codec_lineout_vol_scale),
0542:         SOC_DOUBLE("Line Out Playback Switch",
0543:                    SUN8I_ADDA_MIC2G_CTRL,
0544:                    SUN8I_ADDA_MIC2G_CTRL_LINEOUTLEN,
0545:                    SUN8I_ADDA_MIC2G_CTRL_LINEOUTREN, 1, 0),
0546: };
0547: 
0548: static const char * const sun8i_codec_lineout_src_enum_text[] = {
0549:         "Stereo", "Mono Differential",
0550: };
0551: 
0552: static SOC_ENUM_DOUBLE_DECL(sun8i_codec_lineout_src_enum,
0553:                             SUN8I_ADDA_MIC2G_CTRL,
0554:                             SUN8I_ADDA_MIC2G_CTRL_LINEOUTLSRC,
0555:                             SUN8I_ADDA_MIC2G_CTRL_LINEOUTRSRC,
0556:                             sun8i_codec_lineout_src_enum_text);
0557: 
0558: static const struct snd_kcontrol_new sun8i_codec_lineout_src[] = {
0559:         SOC_DAPM_ENUM("Line Out Source Playback Route",
0560:                       sun8i_codec_lineout_src_enum),
0561: };
0562: 
0563: static const struct snd_soc_dapm_widget sun8i_codec_lineout_widgets[] = {
0564:         SND_SOC_DAPM_MUX("Line Out Source Playback Route",
0565:                          SND_SOC_NOPM, 0, 0, sun8i_codec_lineout_src),
0566:         /* It is unclear if this is a buffer or gate, model it as a supply */
0567:         SND_SOC_DAPM_SUPPLY("Line Out Enable", SUN8I_ADDA_PAEN_HP_CTRL,
0568:                             SUN8I_ADDA_PAEN_HP_CTRL_LINEOUTEN, 0, NULL, 0),
0569:         SND_SOC_DAPM_OUTPUT("LINEOUT"),
0570: };
0571: 
0572: static const struct snd_soc_dapm_route sun8i_codec_lineout_routes[] = {
0573:         { "Line Out Source Playback Route", "Stereo", "Left Mixer" },
0574:         { "Line Out Source Playback Route", "Stereo", "Right Mixer" },
0575:         { "Line Out Source Playback Route", "Mono Differential", "Left Mixer" },
0576:         { "Line Out Source Playback Route", "Mono Differential", "Right Mixer" },
0577:         { "LINEOUT", NULL, "Line Out Source Playback Route" },
0578:         { "LINEOUT", NULL, "Line Out Enable", },
0579: };
0580: 
0581: static int sun8i_codec_add_lineout(struct snd_soc_component *cmpnt)
0582: {
0583:         struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
0584:         struct device *dev = cmpnt->dev;
0585:         int ret;
0586: 
0587:         ret = snd_soc_add_component_controls(cmpnt,
0588:                                              sun8i_codec_lineout_controls,
0589:                                              ARRAY_SIZE(sun8i_codec_lineout_controls));
0590:         if (ret) {
0591:                 dev_err(dev, "Failed to add Line Out controls: %d\n", ret);
0592:                 return ret;
0593:         }
0594: 
0595:         ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_lineout_widgets,
0596:                                         ARRAY_SIZE(sun8i_codec_lineout_widgets));
0597:         if (ret) {
0598:                 dev_err(dev, "Failed to add Line Out DAPM widgets: %d\n", ret);
0599:                 return ret;
0600:         }
0601: 
0602:         ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_lineout_routes,
0603:                                       ARRAY_SIZE(sun8i_codec_lineout_routes));
0604:         if (ret) {
0605:                 dev_err(dev, "Failed to add Line Out DAPM routes: %d\n", ret);
0606:                 return ret;
0607:         }
0608: 
0609:         return 0;
0610: }
0611: 
0612: /* mic2 specific controls, widgets and routes */
0613: static const struct snd_kcontrol_new sun8i_codec_mic2_controls[] = {
0614:         /* Mixer pre-gain */
0615:         SOC_SINGLE_TLV("Mic2 Playback Volume",
0616:                        SUN8I_ADDA_MICIN_GCTRL, SUN8I_ADDA_MICIN_GCTRL_MIC2G,
0617:                        0x7, 0, sun8i_codec_out_mixer_pregain_scale),
0618: 
0619:         /* Microphone Amp boost gain */
0620:         SOC_SINGLE_TLV("Mic2 Boost Volume", SUN8I_ADDA_MIC2G_CTRL,
0621:                        SUN8I_ADDA_MIC2G_CTRL_MIC2BOOST, 0x7, 0,
0622:                        sun8i_codec_mic_gain_scale),
0623: };
0624: 
0625: static const struct snd_soc_dapm_widget sun8i_codec_mic2_widgets[] = {
0626:         /* Microphone input */
0627:         SND_SOC_DAPM_INPUT("MIC2"),
0628: 
0629:         /* Mic input path */
0630:         SND_SOC_DAPM_PGA("Mic2 Amplifier", SUN8I_ADDA_MIC2G_CTRL,
0631:                          SUN8I_ADDA_MIC2G_CTRL_MIC2AMPEN, 0, NULL, 0),
0632: };
0633: 
0634: static const struct snd_soc_dapm_route sun8i_codec_mic2_routes[] = {
0635:         { "Mic2 Amplifier", NULL, "MIC2"},
0636: 
0637:         { "Left Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
0638: 
0639:         { "Right Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
0640: 
0641:         { "Left ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
0642: 
0643:         { "Right ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
0644: };
0645: 
0646: static int sun8i_codec_add_mic2(struct snd_soc_component *cmpnt)
0647: {
0648:         struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
0649:         struct device *dev = cmpnt->dev;
0650:         int ret;
0651: 
0652:         ret = snd_soc_add_component_controls(cmpnt,
0653:                                              sun8i_codec_mic2_controls,
0654:                                              ARRAY_SIZE(sun8i_codec_mic2_controls));
0655:         if (ret) {
0656:                 dev_err(dev, "Failed to add MIC2 controls: %d\n", ret);
0657:                 return ret;
0658:         }
0659: 
0660:         ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_mic2_widgets,
0661:                                         ARRAY_SIZE(sun8i_codec_mic2_widgets));
0662:         if (ret) {
0663:                 dev_err(dev, "Failed to add MIC2 DAPM widgets: %d\n", ret);
0664:                 return ret;
0665:         }
0666: 
0667:         ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_mic2_routes,
0668:                                       ARRAY_SIZE(sun8i_codec_mic2_routes));
0669:         if (ret) {
0670:                 dev_err(dev, "Failed to add MIC2 DAPM routes: %d\n", ret);
0671:                 return ret;
0672:         }
0673: 
0674:         return 0;
0675: }
0676: 
0677: struct sun8i_codec_analog_quirks {
0678:         bool has_headphone;
0679:         bool has_hmic;
0680:         bool has_linein;
0681:         bool has_lineout;
0682:         bool has_mbias;
0683:         bool has_mic2;
0684: };
0685: 
0686: static const struct sun8i_codec_analog_quirks sun8i_a23_quirks = {
0687:         .has_headphone  = true,
0688:         //.has_hmic     = true,
0689:         .has_linein     = true,
0690:         .has_mbias      = true,
0691:         .has_mic2       = true,
0692: };
0693: 
0694: static const struct sun8i_codec_analog_quirks sun8i_h3_quirks = {
0695:         .has_linein     = true,
0696:         .has_lineout    = true,
0697:         .has_mbias      = true,
0698:         .has_mic2       = true,
0699: };
0700: 
0701: static int sun8i_codec_analog_add_mixer(struct snd_soc_component *cmpnt,
0702:                                         const struct sun8i_codec_analog_quirks *quirks)
0703: {
0704:         struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
0705:         struct device *dev = cmpnt->dev;
0706:         int ret;
0707: 
0708:         if (!quirks->has_mic2 && !quirks->has_linein) {
0709:                 /*
0710:                  * Apply the special widget set which has uses a control
0711:                  * without MIC2 and Line In, for SoCs without these.
0712:                  * TODO: not all special cases are supported now, this case
0713:                  * is present because it's the case of V3s.
0714:                  */
0715:                 ret = snd_soc_dapm_new_controls(dapm,
0716:                                                 sun8i_v3s_codec_mixer_widgets,
0717:                                                 ARRAY_SIZE(sun8i_v3s_codec_mixer_widgets));
0718:                 if (ret) {
0719:                         dev_err(dev, "Failed to add V3s Mixer DAPM widgets: %d\n", ret);
0720:                         return ret;
0721:                 }
0722:         } else {
0723:                 /* Apply the generic mixer widget set. */
0724:                 ret = snd_soc_dapm_new_controls(dapm,
0725:                                                 sun8i_codec_mixer_widgets,
0726:                                                 ARRAY_SIZE(sun8i_codec_mixer_widgets));
0727:                 if (ret) {
0728:                         dev_err(dev, "Failed to add Mixer DAPM widgets: %d\n", ret);
0729:                         return ret;
0730:                 }
0731:         }
0732: 
0733:         ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_mixer_routes,
0734:                                       ARRAY_SIZE(sun8i_codec_mixer_routes));
0735:         if (ret) {
0736:                 dev_err(dev, "Failed to add Mixer DAPM routes: %d\n", ret);
0737:                 return ret;
0738:         }
0739: 
0740:         return 0;
0741: }
0742: 
0743: static const struct sun8i_codec_analog_quirks sun8i_v3s_quirks = {
0744:         .has_headphone  = true,
0745:         .has_hmic       = true,
0746: };
0747: 
0748: static int sun8i_codec_analog_cmpnt_probe(struct snd_soc_component *cmpnt)
0749: {
0750:         struct device *dev = cmpnt->dev;
0751:         const struct sun8i_codec_analog_quirks *quirks;
0752:         int ret;
0753: 
0754:         /*
0755:          * This would never return NULL unless someone directly registers a
0756:          * platform device matching this driver's name, without specifying a
0757:          * device tree node.
0758:          */
0759:         quirks = of_device_get_match_data(dev);
0760: 
0761:         /* Add controls, widgets, and routes for individual features */
0762:         ret = sun8i_codec_analog_add_mixer(cmpnt, quirks);
0763:         if (ret)
0764:                 return ret;
0765: 
0766:         if (quirks->has_headphone) {
0767:                 ret = sun8i_codec_add_headphone(cmpnt);
0768:                 if (ret)
0769:                         return ret;
0770:         }
0771: 
0772:         if (quirks->has_hmic) {
0773:                 ret = sun8i_codec_add_hmic(cmpnt);
0774:                 if (ret)
0775:                         return ret;
0776:         }
0777: 
0778:         if (quirks->has_linein) {
0779:                 ret = sun8i_codec_add_linein(cmpnt);
0780:                 if (ret)
0781:                         return ret;
0782:         }
0783: 
0784:         if (quirks->has_lineout) {
0785:                 ret = sun8i_codec_add_lineout(cmpnt);
0786:                 if (ret)
0787:                         return ret;
0788:         }
0789: 
0790:         if (quirks->has_mbias) {
0791:                 ret = sun8i_codec_add_mbias(cmpnt);
0792:                 if (ret)
0793:                         return ret;
0794:         }
0795: 
0796:         if (quirks->has_mic2) {
0797:                 ret = sun8i_codec_add_mic2(cmpnt);
0798:                 if (ret)
0799:                         return ret;
0800:         }
0801: 
0802:         return 0;
0803: }
0804: 
0805: static const struct snd_soc_component_driver sun8i_codec_analog_cmpnt_drv = {
0806:         .controls               = sun8i_codec_common_controls,
0807:         .num_controls           = ARRAY_SIZE(sun8i_codec_common_controls),
0808:         .dapm_widgets           = sun8i_codec_common_widgets,
0809:         .num_dapm_widgets       = ARRAY_SIZE(sun8i_codec_common_widgets),
0810:         .dapm_routes            = sun8i_codec_common_routes,
0811:         .num_dapm_routes        = ARRAY_SIZE(sun8i_codec_common_routes),
0812:         .probe                  = sun8i_codec_analog_cmpnt_probe,
0813: };
0814: 
0815: static const struct of_device_id sun8i_codec_analog_of_match[] = {
0816:         {
0817:                 .compatible = "allwinner,sun8i-a23-codec-analog",
0818:                 .data = &sun8i_a23_quirks,
0819:         },
0820:         {
0821:                 .compatible = "allwinner,sun8i-h3-codec-analog",
0822:                 .data = &sun8i_h3_quirks,
0823:         },
0824:         {
0825:                 .compatible = "allwinner,sun8i-v3s-codec-analog",
0826:                 .data = &sun8i_v3s_quirks,
0827:         },
0828:         {}
0829: };
0830: MODULE_DEVICE_TABLE(of, sun8i_codec_analog_of_match);
0831: 
0832: #define SUNXI_HMIC_ENABLE          (0x4)
0833: #define SUNXI_HMIC_CTL             (0x8)
0834: #define SUNXI_HMIC_DATA            (0xc)
0835: 
0836: #define HMIC_M                                    (28)
0837: #define HMIC_N                                    (24)
0838: #define HMIC_DIRQ                                 (23)
0839: #define HMIC_TH1_HYS                      (21)
0840: #define HMIC_EARPHONE_OUT_IRQ_EN  (20)
0841: #define HMIC_EARPHONE_IN_IRQ_EN   (19)
0842: #define HMIC_KEY_UP_IRQ_EN                (18)
0843: #define HMIC_KEY_DOWN_IRQ_EN      (17)
0844: #define HMIC_DATA_IRQ_EN                  (16)
0845: #define HMIC_DS_SAMP                      (14)
0846: #define HMIC_TH2_HYS                      (13)
0847: #define HMIC_TH2_KEY                  (8)
0848: #define HMIC_SF_SMOOTH_FIL                (6)
0849: #define KEY_UP_IRQ_PEND                   (5)
0850: #define HMIC_TH1_EARPHONE                 (0)
0851: 
0852: #define HMIC_EARPHONE_OUT_IRQ_PEND  (20)
0853: #define HMIC_EARPHONE_IN_IRQ_PEND   (19)
0854: #define HMIC_KEY_UP_IRQ_PEND        (18)
0855: #define HMIC_KEY_DOWN_IRQ_PEND          (17)
0856: #define HMIC_DATA_IRQ_PEND                      (16)
0857: #define HMIC_ADC_DATA                           (0)
0858: 
0859: #define HP_VOLC                                   (0x00)
0860: #define LOMIXSC                                   (0x01)
0861: #define ROMIXSC                                   (0x02)
0862: #define DAC_PA_SRC                                (0x03)
0863: #define PAEN_HP_CTRL                      (0x07)
0864: #define ADDA_APT2                                 (0x12)
0865: #define MIC1G_MICBIAS_CTRL                (0x0B)
0866: #define PA_ANTI_POP_REG_CTRL      (0x0E)
0867: #define PA_SLOPE_SELECT   (3)
0868: #define PA_ANTI_POP_EN          (0)
0869: 
0870: static void __iomem *sun8i_codec_analog_base;
0871: static struct gpio_desc * speaker_amplifier_gpio;
0872: 
0873: struct regmap * rmap = 0;
0874: 
0875: static int hmic_wrreg_prcm_bits(unsigned short reg, unsigned int mask, unsigned int value)
0876: {
0877:         unsigned int old, new;
0878:         if( !rmap ) return 0;
0879:         regmap_read(rmap, reg, &old);
0880:         //adda_reg_read(sun8i_codec_analog_base, reg, &old);
0881:         new     =       (old & ~mask) | value;
0882:         //adda_reg_write(sun8i_codec_analog_base, reg,new);
0883:         regmap_write(rmap, reg,new);
0884: 
0885:         return 0;
0886: }
0887: 
0888: static int hmic_wr_prcm_control(u32 reg, u32 mask, u32 shift, u32 val)
0889: {
0890:         u32 reg_val;
0891:         reg_val = val << shift;
0892:         mask = mask << shift;
0893:         hmic_wrreg_prcm_bits(reg, mask, reg_val);
0894:         return 0;
0895: }
0896: 
0897: static int hmic_wrreg_bits(unsigned short reg, unsigned int     mask,   unsigned int value)
0898: {
0899:         unsigned int old, new;
0900: 
0901:         old     =       readl(sun8i_codec_analog_base + reg);
0902:         new     =       (old & ~mask) | value;
0903: 
0904:         writel(new, sun8i_codec_analog_base + reg);
0905: 
0906:         return 0;
0907: }
0908: 
0909: static int hmic_wr_control(u32 reg, u32 mask, u32 shift, u32 val)
0910: {
0911:         u32 reg_val;
0912:         reg_val = val << shift;
0913:         mask = mask << shift;
0914:         hmic_wrreg_bits(reg, mask, reg_val);
0915:         return 0;
0916: }
0917: 
0918: static irqreturn_t sunxi_codec_analog_irq(int irq, void *dev_id)
0919: {
0920:         u32 tmp;
0921: 
0922:         hmic_wr_control(SUNXI_HMIC_DATA, 0x1, HMIC_KEY_DOWN_IRQ_PEND, 0x1);
0923:         hmic_wr_control(SUNXI_HMIC_DATA, 0x1, HMIC_EARPHONE_IN_IRQ_PEND, 0x1);
0924:         hmic_wr_control(SUNXI_HMIC_DATA, 0x1, HMIC_KEY_UP_IRQ_PEND, 0x1);
0925:         hmic_wr_control(SUNXI_HMIC_DATA, 0x1, HMIC_EARPHONE_OUT_IRQ_PEND, 0x1);
0926:         hmic_wr_control(SUNXI_HMIC_DATA, 0x1, HMIC_DATA_IRQ_PEND, 0x1);
0927: 
0928:         tmp = readl(sun8i_codec_analog_base + SUNXI_HMIC_DATA);
0929:         if(tmp & 0x1f)
0930:                 gpiod_set_value(speaker_amplifier_gpio, 0);
0931:         else
0932:                 gpiod_set_value(speaker_amplifier_gpio, 1);
0933: 
0934:         return IRQ_HANDLED;
0935: }
0936: 
0937: static void sunxi_hppa_enable(void) {
0938:     /*fix the resume blaze blaze noise*/
0939:         hmic_wr_prcm_control(ADDA_APT2, 0x1, PA_SLOPE_SELECT, 0x0);
0940:         hmic_wr_prcm_control(SUN8I_ADDA_PAEN_HP_CTRL, 0x3, SUN8I_ADDA_PAEN_HP_CTRL_PA_ANTI_POP_CTRL, 0x1);
0941:         hmic_wr_prcm_control(PA_ANTI_POP_REG_CTRL, 0x7, PA_ANTI_POP_EN, 0x2);
0942:         usleep_range(100,200);
0943:         /*enable pa*/
0944:         hmic_wr_prcm_control(SUN8I_ADDA_PAEN_HP_CTRL, 0x1, SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN, 0x1);
0945: }
0946: 
0947: static void sunxi_hbias_enable(void) {
0948:         /*audio codec hardware bug. the HBIASADCEN bit must be enable in init*/
0949:         hmic_wr_prcm_control(SUN8I_ADDA_MIC1G_MICBIAS_CTRL, 0x1, SUN8I_ADDA_MIC1G_MICBIAS_CTRL_HMICBIAS_MODE, 0x1);
0950:         hmic_wr_prcm_control(SUN8I_ADDA_MIC1G_MICBIAS_CTRL, 0x1, SUN8I_ADDA_MIC1G_MICBIAS_CTRL_HMICBIASEN, 0x1);
0951: }
0952: 
0953: static void codec_init_events(void)
0954: {
0955:         /*fix the resume blaze blaze noise*/
0956:         sunxi_hppa_enable();
0957:         msleep(450);
0958:         /*audio codec hardware bug. the HBIASADCEN bit must be enable in init*/
0959:         sunxi_hbias_enable();
0960: }
0961: 
0962: static int sun8i_codec_analog_probe(struct platform_device *pdev)
0963: {
0964:         struct resource *res;
0965:         struct regmap *regmap;
0966:         void __iomem *base;
0967:         int irq, ret;
0968: 
0969:         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0970:         base = devm_ioremap_resource(&pdev->dev, res);
0971:         if (IS_ERR(base)) {
0972:                 dev_err(&pdev->dev, "Failed to map the registers\n");
0973:                 return PTR_ERR(base);
0974:         }
0975: 
0976:         sun8i_codec_analog_base = base;
0977: 
0978:         hmic_wr_control(SUNXI_HMIC_CTL, 0xf, HMIC_M, 0x0);                                              /*0xf should be get from hw_debug 28*/
0979:         hmic_wr_control(SUNXI_HMIC_CTL, 0xf, HMIC_N, 0x0);                                              /*0xf should be get from hw_debug 24 0xf*/
0980: //      hmic_wr_control(SUNXI_HMIC_CTL, 0x1, HMIC_DIRQ, 0x1);                                   /*23*/
0981:         hmic_wr_control(SUNXI_HMIC_CTL, 0x1, HMIC_EARPHONE_OUT_IRQ_EN, 0x1);    /*20*/
0982:         hmic_wr_control(SUNXI_HMIC_CTL, 0x1, HMIC_EARPHONE_IN_IRQ_EN, 0x1);     /*19*/
0983:         hmic_wr_control(SUNXI_HMIC_CTL, 0x1, HMIC_KEY_UP_IRQ_EN, 0x1);                  /*18*/
0984:         hmic_wr_control(SUNXI_HMIC_CTL, 0x1, HMIC_KEY_DOWN_IRQ_EN, 0x1);                /*17*/
0985:         hmic_wr_control(SUNXI_HMIC_CTL, 0x1, HMIC_DATA_IRQ_EN, 0x1);                    /*16*/
0986:         hmic_wr_control(SUNXI_HMIC_CTL, 0x3, HMIC_DS_SAMP, 0x0);                                /*14 */
0987:         hmic_wr_control(SUNXI_HMIC_CTL, 0x1f, HMIC_TH2_KEY, 0x0);                               /*0xf should be get from hw_debug 8*/
0988:         hmic_wr_control(SUNXI_HMIC_CTL, 0x1f, HMIC_TH1_EARPHONE, 0x1);                  /*0x1 should be get from hw_debug 0*/
0989: 
0990:         speaker_amplifier_gpio = devm_gpiod_get_optional(&pdev->dev, "speaker-amplifier", GPIOD_OUT_HIGH);
0991:         if (IS_ERR(speaker_amplifier_gpio))
0992:                 dev_err(&pdev->dev, "Can't get speaker_amplifier_gpio\n");
0993: 
0994:         irq = platform_get_irq(pdev, 0);
0995:         if (irq < 0) {
0996:                 dev_err(&pdev->dev, "Can't retrieve our interrupt\n");
0997:                 return irq;
0998:         }
0999: 
1000:         ret = devm_request_irq(&pdev->dev, irq, sunxi_codec_analog_irq, 0, "audio_hmic_irq", NULL);
1001:         if (ret) {
1002:                 dev_err(&pdev->dev, "can't register interrupt handler irq %d: %d\n",
1003:                         irq, ret);
1004:                 return ret;
1005:         }
1006: 
1007:         codec_init_events();
1008: 
1009:         regmap = sun8i_adda_pr_regmap_init(&pdev->dev, base);
1010:         if (IS_ERR(regmap)) {
1011:                 rmap = 0;
1012:                 dev_err(&pdev->dev, "Failed to create regmap\n");
1013:                 return PTR_ERR(regmap);
1014:         }
1015: 
1016:         rmap = regmap;
1017: 
1018:         return devm_snd_soc_register_component(&pdev->dev,
1019:                                                &sun8i_codec_analog_cmpnt_drv,
1020:                                                NULL, 0);
1021: }
1022: 
1023: static struct platform_driver sun8i_codec_analog_driver = {
1024:         .driver = {
1025:                 .name = "sun8i-codec-analog",
1026:                 .of_match_table = sun8i_codec_analog_of_match,
1027:         },
1028:         .probe = sun8i_codec_analog_probe,
1029: };
1030: module_platform_driver(sun8i_codec_analog_driver);
1031: 
1032: MODULE_DESCRIPTION("Allwinner internal codec analog controls driver");
1033: MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
1034: MODULE_LICENSE("GPL");
1035: MODULE_ALIAS("platform:sun8i-codec-analog");