Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1 | /* |
| 2 | * sgtl5000.c -- SGTL5000 ALSA SoC Audio driver |
| 3 | * |
| 4 | * Copyright 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/moduleparam.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/pm.h> |
| 17 | #include <linux/i2c.h> |
| 18 | #include <linux/clk.h> |
Jean-Michel Hautbois | bd0593f | 2014-10-14 08:43:11 +0200 | [diff] [blame] | 19 | #include <linux/log2.h> |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 20 | #include <linux/regmap.h> |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 21 | #include <linux/regulator/driver.h> |
| 22 | #include <linux/regulator/machine.h> |
| 23 | #include <linux/regulator/consumer.h> |
Shawn Guo | 58e4942 | 2011-07-22 00:28:51 +0800 | [diff] [blame] | 24 | #include <linux/of_device.h> |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 25 | #include <sound/core.h> |
| 26 | #include <sound/tlv.h> |
| 27 | #include <sound/pcm.h> |
| 28 | #include <sound/pcm_params.h> |
| 29 | #include <sound/soc.h> |
| 30 | #include <sound/soc-dapm.h> |
| 31 | #include <sound/initval.h> |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 32 | |
| 33 | #include "sgtl5000.h" |
| 34 | |
| 35 | #define SGTL5000_DAP_REG_OFFSET 0x0100 |
| 36 | #define SGTL5000_MAX_REG_OFFSET 0x013A |
| 37 | |
Wolfram Sang | 151798f | 2011-08-02 19:42:19 +0200 | [diff] [blame] | 38 | /* default value of sgtl5000 registers */ |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 39 | static const struct reg_default sgtl5000_reg_defaults[] = { |
Fabio Estevam | 29aa37c | 2014-05-26 10:34:20 -0300 | [diff] [blame] | 40 | { SGTL5000_CHIP_DIG_POWER, 0x0000 }, |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 41 | { SGTL5000_CHIP_CLK_CTRL, 0x0008 }, |
| 42 | { SGTL5000_CHIP_I2S_CTRL, 0x0010 }, |
Fabio Estevam | 016fcab | 2013-07-04 20:01:02 -0300 | [diff] [blame] | 43 | { SGTL5000_CHIP_SSS_CTRL, 0x0010 }, |
Fabio Estevam | 29aa37c | 2014-05-26 10:34:20 -0300 | [diff] [blame] | 44 | { SGTL5000_CHIP_ADCDAC_CTRL, 0x020c }, |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 45 | { SGTL5000_CHIP_DAC_VOL, 0x3c3c }, |
| 46 | { SGTL5000_CHIP_PAD_STRENGTH, 0x015f }, |
Fabio Estevam | 29aa37c | 2014-05-26 10:34:20 -0300 | [diff] [blame] | 47 | { SGTL5000_CHIP_ANA_ADC_CTRL, 0x0000 }, |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 48 | { SGTL5000_CHIP_ANA_HP_CTRL, 0x1818 }, |
| 49 | { SGTL5000_CHIP_ANA_CTRL, 0x0111 }, |
Fabio Estevam | 29aa37c | 2014-05-26 10:34:20 -0300 | [diff] [blame] | 50 | { SGTL5000_CHIP_LINREG_CTRL, 0x0000 }, |
| 51 | { SGTL5000_CHIP_REF_CTRL, 0x0000 }, |
| 52 | { SGTL5000_CHIP_MIC_CTRL, 0x0000 }, |
| 53 | { SGTL5000_CHIP_LINE_OUT_CTRL, 0x0000 }, |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 54 | { SGTL5000_CHIP_LINE_OUT_VOL, 0x0404 }, |
| 55 | { SGTL5000_CHIP_ANA_POWER, 0x7060 }, |
| 56 | { SGTL5000_CHIP_PLL_CTRL, 0x5000 }, |
Fabio Estevam | 29aa37c | 2014-05-26 10:34:20 -0300 | [diff] [blame] | 57 | { SGTL5000_CHIP_CLK_TOP_CTRL, 0x0000 }, |
| 58 | { SGTL5000_CHIP_ANA_STATUS, 0x0000 }, |
| 59 | { SGTL5000_CHIP_SHORT_CTRL, 0x0000 }, |
| 60 | { SGTL5000_CHIP_ANA_TEST2, 0x0000 }, |
| 61 | { SGTL5000_DAP_CTRL, 0x0000 }, |
| 62 | { SGTL5000_DAP_PEQ, 0x0000 }, |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 63 | { SGTL5000_DAP_BASS_ENHANCE, 0x0040 }, |
| 64 | { SGTL5000_DAP_BASS_ENHANCE_CTRL, 0x051f }, |
Fabio Estevam | 29aa37c | 2014-05-26 10:34:20 -0300 | [diff] [blame] | 65 | { SGTL5000_DAP_AUDIO_EQ, 0x0000 }, |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 66 | { SGTL5000_DAP_SURROUND, 0x0040 }, |
| 67 | { SGTL5000_DAP_EQ_BASS_BAND0, 0x002f }, |
| 68 | { SGTL5000_DAP_EQ_BASS_BAND1, 0x002f }, |
| 69 | { SGTL5000_DAP_EQ_BASS_BAND2, 0x002f }, |
| 70 | { SGTL5000_DAP_EQ_BASS_BAND3, 0x002f }, |
| 71 | { SGTL5000_DAP_EQ_BASS_BAND4, 0x002f }, |
| 72 | { SGTL5000_DAP_MAIN_CHAN, 0x8000 }, |
Fabio Estevam | 29aa37c | 2014-05-26 10:34:20 -0300 | [diff] [blame] | 73 | { SGTL5000_DAP_MIX_CHAN, 0x0000 }, |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 74 | { SGTL5000_DAP_AVC_CTRL, 0x0510 }, |
| 75 | { SGTL5000_DAP_AVC_THRESHOLD, 0x1473 }, |
| 76 | { SGTL5000_DAP_AVC_ATTACK, 0x0028 }, |
| 77 | { SGTL5000_DAP_AVC_DECAY, 0x0050 }, |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | /* regulator supplies for sgtl5000, VDDD is an optional external supply */ |
| 81 | enum sgtl5000_regulator_supplies { |
| 82 | VDDA, |
| 83 | VDDIO, |
| 84 | VDDD, |
| 85 | SGTL5000_SUPPLY_NUM |
| 86 | }; |
| 87 | |
| 88 | /* vddd is optional supply */ |
| 89 | static const char *supply_names[SGTL5000_SUPPLY_NUM] = { |
| 90 | "VDDA", |
| 91 | "VDDIO", |
| 92 | "VDDD" |
| 93 | }; |
| 94 | |
| 95 | #define LDO_CONSUMER_NAME "VDDD_LDO" |
| 96 | #define LDO_VOLTAGE 1200000 |
| 97 | |
| 98 | static struct regulator_consumer_supply ldo_consumer[] = { |
| 99 | REGULATOR_SUPPLY(LDO_CONSUMER_NAME, NULL), |
| 100 | }; |
| 101 | |
Mark Brown | 61a142b | 2011-02-28 14:33:01 +0000 | [diff] [blame] | 102 | static struct regulator_init_data ldo_init_data = { |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 103 | .constraints = { |
Fabio Estevam | cd041f6 | 2012-04-03 18:05:20 -0300 | [diff] [blame] | 104 | .min_uV = 1200000, |
| 105 | .max_uV = 1200000, |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 106 | .valid_modes_mask = REGULATOR_MODE_NORMAL, |
| 107 | .valid_ops_mask = REGULATOR_CHANGE_STATUS, |
| 108 | }, |
| 109 | .num_consumer_supplies = 1, |
| 110 | .consumer_supplies = &ldo_consumer[0], |
| 111 | }; |
| 112 | |
| 113 | /* |
| 114 | * sgtl5000 internal ldo regulator, |
| 115 | * enabled when VDDD not provided |
| 116 | */ |
| 117 | struct ldo_regulator { |
| 118 | struct regulator_desc desc; |
| 119 | struct regulator_dev *dev; |
| 120 | int voltage; |
| 121 | void *codec_data; |
| 122 | bool enabled; |
| 123 | }; |
| 124 | |
Jean-Michel Hautbois | bd0593f | 2014-10-14 08:43:11 +0200 | [diff] [blame] | 125 | enum sgtl5000_micbias_resistor { |
| 126 | SGTL5000_MICBIAS_OFF = 0, |
| 127 | SGTL5000_MICBIAS_2K = 2, |
| 128 | SGTL5000_MICBIAS_4K = 4, |
| 129 | SGTL5000_MICBIAS_8K = 8, |
| 130 | }; |
| 131 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 132 | /* sgtl5000 private structure in codec */ |
| 133 | struct sgtl5000_priv { |
| 134 | int sysclk; /* sysclk rate */ |
| 135 | int master; /* i2s master or not */ |
| 136 | int fmt; /* i2s data format */ |
| 137 | struct regulator_bulk_data supplies[SGTL5000_SUPPLY_NUM]; |
| 138 | struct ldo_regulator *ldo; |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 139 | struct regmap *regmap; |
Fabio Estevam | 9e13f34 | 2013-06-09 22:07:46 -0300 | [diff] [blame] | 140 | struct clk *mclk; |
Shawn Guo | 252e91f | 2013-12-13 14:43:02 +0800 | [diff] [blame] | 141 | int revision; |
Jean-Michel Hautbois | bd0593f | 2014-10-14 08:43:11 +0200 | [diff] [blame] | 142 | u8 micbias_resistor; |
Jean-Michel Hautbois | 8735779 | 2014-10-14 08:43:12 +0200 | [diff] [blame] | 143 | u8 micbias_voltage; |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | /* |
| 147 | * mic_bias power on/off share the same register bits with |
| 148 | * output impedance of mic bias, when power on mic bias, we |
| 149 | * need reclaim it to impedance value. |
| 150 | * 0x0 = Powered off |
| 151 | * 0x1 = 2Kohm |
| 152 | * 0x2 = 4Kohm |
| 153 | * 0x3 = 8Kohm |
| 154 | */ |
| 155 | static int mic_bias_event(struct snd_soc_dapm_widget *w, |
| 156 | struct snd_kcontrol *kcontrol, int event) |
| 157 | { |
Jean-Michel Hautbois | bd0593f | 2014-10-14 08:43:11 +0200 | [diff] [blame] | 158 | struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(w->codec); |
| 159 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 160 | switch (event) { |
| 161 | case SND_SOC_DAPM_POST_PMU: |
Jean-Michel Hautbois | bd0593f | 2014-10-14 08:43:11 +0200 | [diff] [blame] | 162 | /* change mic bias resistor */ |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 163 | snd_soc_update_bits(w->codec, SGTL5000_CHIP_MIC_CTRL, |
Jean-Michel Hautbois | bd0593f | 2014-10-14 08:43:11 +0200 | [diff] [blame] | 164 | SGTL5000_BIAS_R_MASK, |
| 165 | sgtl5000->micbias_resistor << SGTL5000_BIAS_R_SHIFT); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 166 | break; |
| 167 | |
| 168 | case SND_SOC_DAPM_PRE_PMD: |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 169 | snd_soc_update_bits(w->codec, SGTL5000_CHIP_MIC_CTRL, |
Axel Lin | dc56c5a | 2011-10-19 11:00:42 +0800 | [diff] [blame] | 170 | SGTL5000_BIAS_R_MASK, 0); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 171 | break; |
| 172 | } |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /* |
Zeng Zhaoming | f0cdcf3 | 2012-03-30 00:13:02 +0800 | [diff] [blame] | 177 | * As manual described, ADC/DAC only works when VAG powerup, |
| 178 | * So enabled VAG before ADC/DAC up. |
| 179 | * In power down case, we need wait 400ms when vag fully ramped down. |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 180 | */ |
Zeng Zhaoming | f0cdcf3 | 2012-03-30 00:13:02 +0800 | [diff] [blame] | 181 | static int power_vag_event(struct snd_soc_dapm_widget *w, |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 182 | struct snd_kcontrol *kcontrol, int event) |
| 183 | { |
Lothar Waßmann | f091f3f | 2013-07-31 16:44:29 +0200 | [diff] [blame] | 184 | const u32 mask = SGTL5000_DAC_POWERUP | SGTL5000_ADC_POWERUP; |
| 185 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 186 | switch (event) { |
Marek Vasut | dd4d2d6 | 2013-05-28 20:55:56 +0200 | [diff] [blame] | 187 | case SND_SOC_DAPM_POST_PMU: |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 188 | snd_soc_update_bits(w->codec, SGTL5000_CHIP_ANA_POWER, |
| 189 | SGTL5000_VAG_POWERUP, SGTL5000_VAG_POWERUP); |
| 190 | break; |
| 191 | |
Marek Vasut | dd4d2d6 | 2013-05-28 20:55:56 +0200 | [diff] [blame] | 192 | case SND_SOC_DAPM_PRE_PMD: |
Lothar Waßmann | f091f3f | 2013-07-31 16:44:29 +0200 | [diff] [blame] | 193 | /* |
| 194 | * Don't clear VAG_POWERUP, when both DAC and ADC are |
| 195 | * operational to prevent inadvertently starving the |
| 196 | * other one of them. |
| 197 | */ |
| 198 | if ((snd_soc_read(w->codec, SGTL5000_CHIP_ANA_POWER) & |
| 199 | mask) != mask) { |
| 200 | snd_soc_update_bits(w->codec, SGTL5000_CHIP_ANA_POWER, |
| 201 | SGTL5000_VAG_POWERUP, 0); |
| 202 | msleep(400); |
| 203 | } |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 204 | break; |
| 205 | default: |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* input sources for ADC */ |
| 213 | static const char *adc_mux_text[] = { |
| 214 | "MIC_IN", "LINE_IN" |
| 215 | }; |
| 216 | |
Takashi Iwai | c8ed650 | 2014-02-18 10:16:31 +0100 | [diff] [blame] | 217 | static SOC_ENUM_SINGLE_DECL(adc_enum, |
| 218 | SGTL5000_CHIP_ANA_CTRL, 2, |
| 219 | adc_mux_text); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 220 | |
| 221 | static const struct snd_kcontrol_new adc_mux = |
| 222 | SOC_DAPM_ENUM("Capture Mux", adc_enum); |
| 223 | |
| 224 | /* input sources for DAC */ |
| 225 | static const char *dac_mux_text[] = { |
| 226 | "DAC", "LINE_IN" |
| 227 | }; |
| 228 | |
Takashi Iwai | c8ed650 | 2014-02-18 10:16:31 +0100 | [diff] [blame] | 229 | static SOC_ENUM_SINGLE_DECL(dac_enum, |
| 230 | SGTL5000_CHIP_ANA_CTRL, 6, |
| 231 | dac_mux_text); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 232 | |
| 233 | static const struct snd_kcontrol_new dac_mux = |
| 234 | SOC_DAPM_ENUM("Headphone Mux", dac_enum); |
| 235 | |
| 236 | static const struct snd_soc_dapm_widget sgtl5000_dapm_widgets[] = { |
| 237 | SND_SOC_DAPM_INPUT("LINE_IN"), |
| 238 | SND_SOC_DAPM_INPUT("MIC_IN"), |
| 239 | |
| 240 | SND_SOC_DAPM_OUTPUT("HP_OUT"), |
| 241 | SND_SOC_DAPM_OUTPUT("LINE_OUT"), |
| 242 | |
Mark Brown | 8fc8ec9 | 2012-03-28 20:51:43 +0100 | [diff] [blame] | 243 | SND_SOC_DAPM_SUPPLY("Mic Bias", SGTL5000_CHIP_MIC_CTRL, 8, 0, |
| 244 | mic_bias_event, |
| 245 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 246 | |
Zeng Zhaoming | f0cdcf3 | 2012-03-30 00:13:02 +0800 | [diff] [blame] | 247 | SND_SOC_DAPM_PGA("HP", SGTL5000_CHIP_ANA_POWER, 4, 0, NULL, 0), |
| 248 | SND_SOC_DAPM_PGA("LO", SGTL5000_CHIP_ANA_POWER, 0, 0, NULL, 0), |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 249 | |
| 250 | SND_SOC_DAPM_MUX("Capture Mux", SND_SOC_NOPM, 0, 0, &adc_mux), |
| 251 | SND_SOC_DAPM_MUX("Headphone Mux", SND_SOC_NOPM, 0, 0, &dac_mux), |
| 252 | |
| 253 | /* aif for i2s input */ |
| 254 | SND_SOC_DAPM_AIF_IN("AIFIN", "Playback", |
| 255 | 0, SGTL5000_CHIP_DIG_POWER, |
| 256 | 0, 0), |
| 257 | |
| 258 | /* aif for i2s output */ |
| 259 | SND_SOC_DAPM_AIF_OUT("AIFOUT", "Capture", |
| 260 | 0, SGTL5000_CHIP_DIG_POWER, |
| 261 | 1, 0), |
| 262 | |
Zeng Zhaoming | f0cdcf3 | 2012-03-30 00:13:02 +0800 | [diff] [blame] | 263 | SND_SOC_DAPM_ADC("ADC", "Capture", SGTL5000_CHIP_ANA_POWER, 1, 0), |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 264 | SND_SOC_DAPM_DAC("DAC", "Playback", SGTL5000_CHIP_ANA_POWER, 3, 0), |
Marek Vasut | dd4d2d6 | 2013-05-28 20:55:56 +0200 | [diff] [blame] | 265 | |
| 266 | SND_SOC_DAPM_PRE("VAG_POWER_PRE", power_vag_event), |
| 267 | SND_SOC_DAPM_POST("VAG_POWER_POST", power_vag_event), |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | /* routes for sgtl5000 */ |
Fabio Estevam | 8998963 | 2012-01-22 14:49:42 -0200 | [diff] [blame] | 271 | static const struct snd_soc_dapm_route sgtl5000_dapm_routes[] = { |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 272 | {"Capture Mux", "LINE_IN", "LINE_IN"}, /* line_in --> adc_mux */ |
| 273 | {"Capture Mux", "MIC_IN", "MIC_IN"}, /* mic_in --> adc_mux */ |
| 274 | |
| 275 | {"ADC", NULL, "Capture Mux"}, /* adc_mux --> adc */ |
| 276 | {"AIFOUT", NULL, "ADC"}, /* adc --> i2s_out */ |
| 277 | |
| 278 | {"DAC", NULL, "AIFIN"}, /* i2s-->dac,skip audio mux */ |
| 279 | {"Headphone Mux", "DAC", "DAC"}, /* dac --> hp_mux */ |
| 280 | {"LO", NULL, "DAC"}, /* dac --> line_out */ |
| 281 | |
| 282 | {"Headphone Mux", "LINE_IN", "LINE_IN"},/* line_in --> hp_mux */ |
| 283 | {"HP", NULL, "Headphone Mux"}, /* hp_mux --> hp */ |
| 284 | |
| 285 | {"LINE_OUT", NULL, "LO"}, |
| 286 | {"HP_OUT", NULL, "HP"}, |
| 287 | }; |
| 288 | |
| 289 | /* custom function to fetch info of PCM playback volume */ |
| 290 | static int dac_info_volsw(struct snd_kcontrol *kcontrol, |
| 291 | struct snd_ctl_elem_info *uinfo) |
| 292 | { |
| 293 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 294 | uinfo->count = 2; |
| 295 | uinfo->value.integer.min = 0; |
| 296 | uinfo->value.integer.max = 0xfc - 0x3c; |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * custom function to get of PCM playback volume |
| 302 | * |
| 303 | * dac volume register |
| 304 | * 15-------------8-7--------------0 |
| 305 | * | R channel vol | L channel vol | |
| 306 | * ------------------------------- |
| 307 | * |
| 308 | * PCM volume with 0.5017 dB steps from 0 to -90 dB |
| 309 | * |
| 310 | * register values map to dB |
| 311 | * 0x3B and less = Reserved |
| 312 | * 0x3C = 0 dB |
| 313 | * 0x3D = -0.5 dB |
| 314 | * 0xF0 = -90 dB |
| 315 | * 0xFC and greater = Muted |
| 316 | * |
| 317 | * register value map to userspace value |
| 318 | * |
| 319 | * register value 0x3c(0dB) 0xf0(-90dB)0xfc |
| 320 | * ------------------------------ |
| 321 | * userspace value 0xc0 0 |
| 322 | */ |
| 323 | static int dac_get_volsw(struct snd_kcontrol *kcontrol, |
| 324 | struct snd_ctl_elem_value *ucontrol) |
| 325 | { |
Lars-Peter Clausen | ea53bf7 | 2014-03-18 09:02:04 +0100 | [diff] [blame] | 326 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 327 | int reg; |
| 328 | int l; |
| 329 | int r; |
| 330 | |
| 331 | reg = snd_soc_read(codec, SGTL5000_CHIP_DAC_VOL); |
| 332 | |
| 333 | /* get left channel volume */ |
| 334 | l = (reg & SGTL5000_DAC_VOL_LEFT_MASK) >> SGTL5000_DAC_VOL_LEFT_SHIFT; |
| 335 | |
| 336 | /* get right channel volume */ |
| 337 | r = (reg & SGTL5000_DAC_VOL_RIGHT_MASK) >> SGTL5000_DAC_VOL_RIGHT_SHIFT; |
| 338 | |
| 339 | /* make sure value fall in (0x3c,0xfc) */ |
| 340 | l = clamp(l, 0x3c, 0xfc); |
| 341 | r = clamp(r, 0x3c, 0xfc); |
| 342 | |
| 343 | /* invert it and map to userspace value */ |
| 344 | l = 0xfc - l; |
| 345 | r = 0xfc - r; |
| 346 | |
| 347 | ucontrol->value.integer.value[0] = l; |
| 348 | ucontrol->value.integer.value[1] = r; |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * custom function to put of PCM playback volume |
| 355 | * |
| 356 | * dac volume register |
| 357 | * 15-------------8-7--------------0 |
| 358 | * | R channel vol | L channel vol | |
| 359 | * ------------------------------- |
| 360 | * |
| 361 | * PCM volume with 0.5017 dB steps from 0 to -90 dB |
| 362 | * |
| 363 | * register values map to dB |
| 364 | * 0x3B and less = Reserved |
| 365 | * 0x3C = 0 dB |
| 366 | * 0x3D = -0.5 dB |
| 367 | * 0xF0 = -90 dB |
| 368 | * 0xFC and greater = Muted |
| 369 | * |
| 370 | * userspace value map to register value |
| 371 | * |
| 372 | * userspace value 0xc0 0 |
| 373 | * ------------------------------ |
| 374 | * register value 0x3c(0dB) 0xf0(-90dB)0xfc |
| 375 | */ |
| 376 | static int dac_put_volsw(struct snd_kcontrol *kcontrol, |
| 377 | struct snd_ctl_elem_value *ucontrol) |
| 378 | { |
Lars-Peter Clausen | ea53bf7 | 2014-03-18 09:02:04 +0100 | [diff] [blame] | 379 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 380 | int reg; |
| 381 | int l; |
| 382 | int r; |
| 383 | |
| 384 | l = ucontrol->value.integer.value[0]; |
| 385 | r = ucontrol->value.integer.value[1]; |
| 386 | |
| 387 | /* make sure userspace volume fall in (0, 0xfc-0x3c) */ |
| 388 | l = clamp(l, 0, 0xfc - 0x3c); |
| 389 | r = clamp(r, 0, 0xfc - 0x3c); |
| 390 | |
| 391 | /* invert it, get the value can be set to register */ |
| 392 | l = 0xfc - l; |
| 393 | r = 0xfc - r; |
| 394 | |
| 395 | /* shift to get the register value */ |
| 396 | reg = l << SGTL5000_DAC_VOL_LEFT_SHIFT | |
| 397 | r << SGTL5000_DAC_VOL_RIGHT_SHIFT; |
| 398 | |
| 399 | snd_soc_write(codec, SGTL5000_CHIP_DAC_VOL, reg); |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static const DECLARE_TLV_DB_SCALE(capture_6db_attenuate, -600, 600, 0); |
| 405 | |
| 406 | /* tlv for mic gain, 0db 20db 30db 40db */ |
| 407 | static const unsigned int mic_gain_tlv[] = { |
Clemens Ladisch | 740fb9d | 2011-11-20 15:12:26 +0100 | [diff] [blame] | 408 | TLV_DB_RANGE_HEAD(2), |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 409 | 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0), |
| 410 | 1, 3, TLV_DB_SCALE_ITEM(2000, 1000, 0), |
| 411 | }; |
| 412 | |
| 413 | /* tlv for hp volume, -51.5db to 12.0db, step .5db */ |
| 414 | static const DECLARE_TLV_DB_SCALE(headphone_volume, -5150, 50, 0); |
| 415 | |
| 416 | static const struct snd_kcontrol_new sgtl5000_snd_controls[] = { |
| 417 | /* SOC_DOUBLE_S8_TLV with invert */ |
| 418 | { |
| 419 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 420 | .name = "PCM Playback Volume", |
| 421 | .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | |
| 422 | SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 423 | .info = dac_info_volsw, |
| 424 | .get = dac_get_volsw, |
| 425 | .put = dac_put_volsw, |
| 426 | }, |
| 427 | |
| 428 | SOC_DOUBLE("Capture Volume", SGTL5000_CHIP_ANA_ADC_CTRL, 0, 4, 0xf, 0), |
| 429 | SOC_SINGLE_TLV("Capture Attenuate Switch (-6dB)", |
| 430 | SGTL5000_CHIP_ANA_ADC_CTRL, |
Lothar Waßmann | 65f2b22 | 2013-07-31 16:44:30 +0200 | [diff] [blame] | 431 | 8, 1, 0, capture_6db_attenuate), |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 432 | SOC_SINGLE("Capture ZC Switch", SGTL5000_CHIP_ANA_CTRL, 1, 1, 0), |
| 433 | |
| 434 | SOC_DOUBLE_TLV("Headphone Playback Volume", |
| 435 | SGTL5000_CHIP_ANA_HP_CTRL, |
| 436 | 0, 8, |
| 437 | 0x7f, 1, |
| 438 | headphone_volume), |
| 439 | SOC_SINGLE("Headphone Playback ZC Switch", SGTL5000_CHIP_ANA_CTRL, |
| 440 | 5, 1, 0), |
| 441 | |
| 442 | SOC_SINGLE_TLV("Mic Volume", SGTL5000_CHIP_MIC_CTRL, |
Fabio Estevam | b50684d | 2012-12-23 15:45:31 -0200 | [diff] [blame] | 443 | 0, 3, 0, mic_gain_tlv), |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 444 | }; |
| 445 | |
| 446 | /* mute the codec used by alsa core */ |
| 447 | static int sgtl5000_digital_mute(struct snd_soc_dai *codec_dai, int mute) |
| 448 | { |
| 449 | struct snd_soc_codec *codec = codec_dai->codec; |
| 450 | u16 adcdac_ctrl = SGTL5000_DAC_MUTE_LEFT | SGTL5000_DAC_MUTE_RIGHT; |
| 451 | |
| 452 | snd_soc_update_bits(codec, SGTL5000_CHIP_ADCDAC_CTRL, |
| 453 | adcdac_ctrl, mute ? adcdac_ctrl : 0); |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | /* set codec format */ |
| 459 | static int sgtl5000_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt) |
| 460 | { |
| 461 | struct snd_soc_codec *codec = codec_dai->codec; |
| 462 | struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec); |
| 463 | u16 i2sctl = 0; |
| 464 | |
| 465 | sgtl5000->master = 0; |
| 466 | /* |
| 467 | * i2s clock and frame master setting. |
| 468 | * ONLY support: |
| 469 | * - clock and frame slave, |
| 470 | * - clock and frame master |
| 471 | */ |
| 472 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 473 | case SND_SOC_DAIFMT_CBS_CFS: |
| 474 | break; |
| 475 | case SND_SOC_DAIFMT_CBM_CFM: |
| 476 | i2sctl |= SGTL5000_I2S_MASTER; |
| 477 | sgtl5000->master = 1; |
| 478 | break; |
| 479 | default: |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | |
| 483 | /* setting i2s data format */ |
| 484 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 485 | case SND_SOC_DAIFMT_DSP_A: |
| 486 | i2sctl |= SGTL5000_I2S_MODE_PCM; |
| 487 | break; |
| 488 | case SND_SOC_DAIFMT_DSP_B: |
| 489 | i2sctl |= SGTL5000_I2S_MODE_PCM; |
| 490 | i2sctl |= SGTL5000_I2S_LRALIGN; |
| 491 | break; |
| 492 | case SND_SOC_DAIFMT_I2S: |
| 493 | i2sctl |= SGTL5000_I2S_MODE_I2S_LJ; |
| 494 | break; |
| 495 | case SND_SOC_DAIFMT_RIGHT_J: |
| 496 | i2sctl |= SGTL5000_I2S_MODE_RJ; |
| 497 | i2sctl |= SGTL5000_I2S_LRPOL; |
| 498 | break; |
| 499 | case SND_SOC_DAIFMT_LEFT_J: |
| 500 | i2sctl |= SGTL5000_I2S_MODE_I2S_LJ; |
| 501 | i2sctl |= SGTL5000_I2S_LRALIGN; |
| 502 | break; |
| 503 | default: |
| 504 | return -EINVAL; |
| 505 | } |
| 506 | |
| 507 | sgtl5000->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK; |
| 508 | |
| 509 | /* Clock inversion */ |
| 510 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 511 | case SND_SOC_DAIFMT_NB_NF: |
| 512 | break; |
| 513 | case SND_SOC_DAIFMT_IB_NF: |
| 514 | i2sctl |= SGTL5000_I2S_SCLK_INV; |
| 515 | break; |
| 516 | default: |
| 517 | return -EINVAL; |
| 518 | } |
| 519 | |
| 520 | snd_soc_write(codec, SGTL5000_CHIP_I2S_CTRL, i2sctl); |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | /* set codec sysclk */ |
| 526 | static int sgtl5000_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 527 | int clk_id, unsigned int freq, int dir) |
| 528 | { |
| 529 | struct snd_soc_codec *codec = codec_dai->codec; |
| 530 | struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec); |
| 531 | |
| 532 | switch (clk_id) { |
| 533 | case SGTL5000_SYSCLK: |
| 534 | sgtl5000->sysclk = freq; |
| 535 | break; |
| 536 | default: |
| 537 | return -EINVAL; |
| 538 | } |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | /* |
| 544 | * set clock according to i2s frame clock, |
Fabio Estevam | 7f6d75d | 2014-10-07 10:50:56 -0300 | [diff] [blame] | 545 | * sgtl5000 provides 2 clock sources: |
| 546 | * 1. sys_mclk: sample freq can only be configured to |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 547 | * 1/256, 1/384, 1/512 of sys_mclk. |
Fabio Estevam | 7f6d75d | 2014-10-07 10:50:56 -0300 | [diff] [blame] | 548 | * 2. pll: can derive any audio clocks. |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 549 | * |
| 550 | * clock setting rules: |
Fabio Estevam | 7f6d75d | 2014-10-07 10:50:56 -0300 | [diff] [blame] | 551 | * 1. in slave mode, only sys_mclk can be used |
| 552 | * 2. as constraint by sys_mclk, sample freq should be set to 32 kHz, 44.1 kHz |
| 553 | * and above. |
| 554 | * 3. usage of sys_mclk is preferred over pll to save power. |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 555 | */ |
| 556 | static int sgtl5000_set_clock(struct snd_soc_codec *codec, int frame_rate) |
| 557 | { |
| 558 | struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec); |
| 559 | int clk_ctl = 0; |
| 560 | int sys_fs; /* sample freq */ |
| 561 | |
| 562 | /* |
| 563 | * sample freq should be divided by frame clock, |
Fabio Estevam | 7f6d75d | 2014-10-07 10:50:56 -0300 | [diff] [blame] | 564 | * if frame clock is lower than 44.1 kHz, sample freq should be set to |
| 565 | * 32 kHz or 44.1 kHz. |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 566 | */ |
| 567 | switch (frame_rate) { |
| 568 | case 8000: |
| 569 | case 16000: |
| 570 | sys_fs = 32000; |
| 571 | break; |
| 572 | case 11025: |
| 573 | case 22050: |
| 574 | sys_fs = 44100; |
| 575 | break; |
| 576 | default: |
| 577 | sys_fs = frame_rate; |
| 578 | break; |
| 579 | } |
| 580 | |
| 581 | /* set divided factor of frame clock */ |
| 582 | switch (sys_fs / frame_rate) { |
| 583 | case 4: |
| 584 | clk_ctl |= SGTL5000_RATE_MODE_DIV_4 << SGTL5000_RATE_MODE_SHIFT; |
| 585 | break; |
| 586 | case 2: |
| 587 | clk_ctl |= SGTL5000_RATE_MODE_DIV_2 << SGTL5000_RATE_MODE_SHIFT; |
| 588 | break; |
| 589 | case 1: |
| 590 | clk_ctl |= SGTL5000_RATE_MODE_DIV_1 << SGTL5000_RATE_MODE_SHIFT; |
| 591 | break; |
| 592 | default: |
| 593 | return -EINVAL; |
| 594 | } |
| 595 | |
| 596 | /* set the sys_fs according to frame rate */ |
| 597 | switch (sys_fs) { |
| 598 | case 32000: |
| 599 | clk_ctl |= SGTL5000_SYS_FS_32k << SGTL5000_SYS_FS_SHIFT; |
| 600 | break; |
| 601 | case 44100: |
| 602 | clk_ctl |= SGTL5000_SYS_FS_44_1k << SGTL5000_SYS_FS_SHIFT; |
| 603 | break; |
| 604 | case 48000: |
| 605 | clk_ctl |= SGTL5000_SYS_FS_48k << SGTL5000_SYS_FS_SHIFT; |
| 606 | break; |
| 607 | case 96000: |
| 608 | clk_ctl |= SGTL5000_SYS_FS_96k << SGTL5000_SYS_FS_SHIFT; |
| 609 | break; |
| 610 | default: |
| 611 | dev_err(codec->dev, "frame rate %d not supported\n", |
| 612 | frame_rate); |
| 613 | return -EINVAL; |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * calculate the divider of mclk/sample_freq, |
Fabio Estevam | 7f6d75d | 2014-10-07 10:50:56 -0300 | [diff] [blame] | 618 | * factor of freq = 96 kHz can only be 256, since mclk is in the range |
| 619 | * of 8 MHz - 27 MHz |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 620 | */ |
| 621 | switch (sgtl5000->sysclk / sys_fs) { |
| 622 | case 256: |
| 623 | clk_ctl |= SGTL5000_MCLK_FREQ_256FS << |
| 624 | SGTL5000_MCLK_FREQ_SHIFT; |
| 625 | break; |
| 626 | case 384: |
| 627 | clk_ctl |= SGTL5000_MCLK_FREQ_384FS << |
| 628 | SGTL5000_MCLK_FREQ_SHIFT; |
| 629 | break; |
| 630 | case 512: |
| 631 | clk_ctl |= SGTL5000_MCLK_FREQ_512FS << |
| 632 | SGTL5000_MCLK_FREQ_SHIFT; |
| 633 | break; |
| 634 | default: |
Fabio Estevam | 7f6d75d | 2014-10-07 10:50:56 -0300 | [diff] [blame] | 635 | /* if mclk does not satisfy the divider, use pll */ |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 636 | if (sgtl5000->master) { |
| 637 | clk_ctl |= SGTL5000_MCLK_FREQ_PLL << |
| 638 | SGTL5000_MCLK_FREQ_SHIFT; |
| 639 | } else { |
| 640 | dev_err(codec->dev, |
| 641 | "PLL not supported in slave mode\n"); |
Fabio Estevam | fa558d0 | 2014-10-02 16:16:50 -0300 | [diff] [blame] | 642 | dev_err(codec->dev, "%d ratio is not supported. " |
| 643 | "SYS_MCLK needs to be 256, 384 or 512 * fs\n", |
| 644 | sgtl5000->sysclk / sys_fs); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 645 | return -EINVAL; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | /* if using pll, please check manual 6.4.2 for detail */ |
| 650 | if ((clk_ctl & SGTL5000_MCLK_FREQ_MASK) == SGTL5000_MCLK_FREQ_PLL) { |
| 651 | u64 out, t; |
| 652 | int div2; |
| 653 | int pll_ctl; |
| 654 | unsigned int in, int_div, frac_div; |
| 655 | |
| 656 | if (sgtl5000->sysclk > 17000000) { |
| 657 | div2 = 1; |
| 658 | in = sgtl5000->sysclk / 2; |
| 659 | } else { |
| 660 | div2 = 0; |
| 661 | in = sgtl5000->sysclk; |
| 662 | } |
| 663 | if (sys_fs == 44100) |
| 664 | out = 180633600; |
| 665 | else |
| 666 | out = 196608000; |
| 667 | t = do_div(out, in); |
| 668 | int_div = out; |
| 669 | t *= 2048; |
| 670 | do_div(t, in); |
| 671 | frac_div = t; |
| 672 | pll_ctl = int_div << SGTL5000_PLL_INT_DIV_SHIFT | |
| 673 | frac_div << SGTL5000_PLL_FRAC_DIV_SHIFT; |
| 674 | |
| 675 | snd_soc_write(codec, SGTL5000_CHIP_PLL_CTRL, pll_ctl); |
| 676 | if (div2) |
| 677 | snd_soc_update_bits(codec, |
| 678 | SGTL5000_CHIP_CLK_TOP_CTRL, |
| 679 | SGTL5000_INPUT_FREQ_DIV2, |
| 680 | SGTL5000_INPUT_FREQ_DIV2); |
| 681 | else |
| 682 | snd_soc_update_bits(codec, |
| 683 | SGTL5000_CHIP_CLK_TOP_CTRL, |
| 684 | SGTL5000_INPUT_FREQ_DIV2, |
| 685 | 0); |
| 686 | |
| 687 | /* power up pll */ |
| 688 | snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, |
| 689 | SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP, |
| 690 | SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP); |
Oskar Schirmer | e06e4c2 | 2013-08-05 07:36:02 +0000 | [diff] [blame] | 691 | |
| 692 | /* if using pll, clk_ctrl must be set after pll power up */ |
| 693 | snd_soc_write(codec, SGTL5000_CHIP_CLK_CTRL, clk_ctl); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 694 | } else { |
Oskar Schirmer | e06e4c2 | 2013-08-05 07:36:02 +0000 | [diff] [blame] | 695 | /* otherwise, clk_ctrl must be set before pll power down */ |
| 696 | snd_soc_write(codec, SGTL5000_CHIP_CLK_CTRL, clk_ctl); |
| 697 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 698 | /* power down pll */ |
| 699 | snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, |
| 700 | SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP, |
| 701 | 0); |
| 702 | } |
| 703 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * Set PCM DAI bit size and sample rate. |
| 709 | * input: params_rate, params_fmt |
| 710 | */ |
| 711 | static int sgtl5000_pcm_hw_params(struct snd_pcm_substream *substream, |
| 712 | struct snd_pcm_hw_params *params, |
| 713 | struct snd_soc_dai *dai) |
| 714 | { |
Mark Brown | e6968a1 | 2012-04-04 15:58:16 +0100 | [diff] [blame] | 715 | struct snd_soc_codec *codec = dai->codec; |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 716 | struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec); |
| 717 | int channels = params_channels(params); |
| 718 | int i2s_ctl = 0; |
| 719 | int stereo; |
| 720 | int ret; |
| 721 | |
| 722 | /* sysclk should already set */ |
| 723 | if (!sgtl5000->sysclk) { |
| 724 | dev_err(codec->dev, "%s: set sysclk first!\n", __func__); |
| 725 | return -EFAULT; |
| 726 | } |
| 727 | |
| 728 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 729 | stereo = SGTL5000_DAC_STEREO; |
| 730 | else |
| 731 | stereo = SGTL5000_ADC_STEREO; |
| 732 | |
| 733 | /* set mono to save power */ |
| 734 | snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, stereo, |
| 735 | channels == 1 ? 0 : stereo); |
| 736 | |
| 737 | /* set codec clock base on lrclk */ |
| 738 | ret = sgtl5000_set_clock(codec, params_rate(params)); |
| 739 | if (ret) |
| 740 | return ret; |
| 741 | |
| 742 | /* set i2s data format */ |
Mark Brown | dacc2ae | 2014-07-31 12:46:05 +0100 | [diff] [blame] | 743 | switch (params_width(params)) { |
| 744 | case 16: |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 745 | if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J) |
| 746 | return -EINVAL; |
| 747 | i2s_ctl |= SGTL5000_I2S_DLEN_16 << SGTL5000_I2S_DLEN_SHIFT; |
| 748 | i2s_ctl |= SGTL5000_I2S_SCLKFREQ_32FS << |
| 749 | SGTL5000_I2S_SCLKFREQ_SHIFT; |
| 750 | break; |
Mark Brown | dacc2ae | 2014-07-31 12:46:05 +0100 | [diff] [blame] | 751 | case 20: |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 752 | i2s_ctl |= SGTL5000_I2S_DLEN_20 << SGTL5000_I2S_DLEN_SHIFT; |
| 753 | i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS << |
| 754 | SGTL5000_I2S_SCLKFREQ_SHIFT; |
| 755 | break; |
Mark Brown | dacc2ae | 2014-07-31 12:46:05 +0100 | [diff] [blame] | 756 | case 24: |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 757 | i2s_ctl |= SGTL5000_I2S_DLEN_24 << SGTL5000_I2S_DLEN_SHIFT; |
| 758 | i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS << |
| 759 | SGTL5000_I2S_SCLKFREQ_SHIFT; |
| 760 | break; |
Mark Brown | dacc2ae | 2014-07-31 12:46:05 +0100 | [diff] [blame] | 761 | case 32: |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 762 | if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J) |
| 763 | return -EINVAL; |
| 764 | i2s_ctl |= SGTL5000_I2S_DLEN_32 << SGTL5000_I2S_DLEN_SHIFT; |
| 765 | i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS << |
| 766 | SGTL5000_I2S_SCLKFREQ_SHIFT; |
| 767 | break; |
| 768 | default: |
| 769 | return -EINVAL; |
| 770 | } |
| 771 | |
Axel Lin | 33cb92c | 2011-10-21 09:54:43 +0800 | [diff] [blame] | 772 | snd_soc_update_bits(codec, SGTL5000_CHIP_I2S_CTRL, |
| 773 | SGTL5000_I2S_DLEN_MASK | SGTL5000_I2S_SCLKFREQ_MASK, |
| 774 | i2s_ctl); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | |
Mark Brown | 333802e | 2011-03-22 12:02:33 +0000 | [diff] [blame] | 779 | #ifdef CONFIG_REGULATOR |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 780 | static int ldo_regulator_is_enabled(struct regulator_dev *dev) |
| 781 | { |
| 782 | struct ldo_regulator *ldo = rdev_get_drvdata(dev); |
| 783 | |
| 784 | return ldo->enabled; |
| 785 | } |
| 786 | |
| 787 | static int ldo_regulator_enable(struct regulator_dev *dev) |
| 788 | { |
| 789 | struct ldo_regulator *ldo = rdev_get_drvdata(dev); |
| 790 | struct snd_soc_codec *codec = (struct snd_soc_codec *)ldo->codec_data; |
| 791 | int reg; |
| 792 | |
| 793 | if (ldo_regulator_is_enabled(dev)) |
| 794 | return 0; |
| 795 | |
| 796 | /* set regulator value firstly */ |
| 797 | reg = (1600 - ldo->voltage / 1000) / 50; |
| 798 | reg = clamp(reg, 0x0, 0xf); |
| 799 | |
| 800 | /* amend the voltage value, unit: uV */ |
| 801 | ldo->voltage = (1600 - reg * 50) * 1000; |
| 802 | |
| 803 | /* set voltage to register */ |
| 804 | snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL, |
Axel Lin | 064a4bc | 2011-10-20 18:49:29 +0800 | [diff] [blame] | 805 | SGTL5000_LINREG_VDDD_MASK, reg); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 806 | |
| 807 | snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, |
| 808 | SGTL5000_LINEREG_D_POWERUP, |
| 809 | SGTL5000_LINEREG_D_POWERUP); |
| 810 | |
Fabio Estevam | 7f6d75d | 2014-10-07 10:50:56 -0300 | [diff] [blame] | 811 | /* when internal ldo is enabled, simple digital power can be disabled */ |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 812 | snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, |
| 813 | SGTL5000_LINREG_SIMPLE_POWERUP, |
| 814 | 0); |
| 815 | |
| 816 | ldo->enabled = 1; |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | static int ldo_regulator_disable(struct regulator_dev *dev) |
| 821 | { |
| 822 | struct ldo_regulator *ldo = rdev_get_drvdata(dev); |
| 823 | struct snd_soc_codec *codec = (struct snd_soc_codec *)ldo->codec_data; |
| 824 | |
| 825 | snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, |
| 826 | SGTL5000_LINEREG_D_POWERUP, |
| 827 | 0); |
| 828 | |
| 829 | /* clear voltage info */ |
| 830 | snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL, |
Axel Lin | 064a4bc | 2011-10-20 18:49:29 +0800 | [diff] [blame] | 831 | SGTL5000_LINREG_VDDD_MASK, 0); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 832 | |
| 833 | ldo->enabled = 0; |
| 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | static int ldo_regulator_get_voltage(struct regulator_dev *dev) |
| 839 | { |
| 840 | struct ldo_regulator *ldo = rdev_get_drvdata(dev); |
| 841 | |
| 842 | return ldo->voltage; |
| 843 | } |
| 844 | |
| 845 | static struct regulator_ops ldo_regulator_ops = { |
| 846 | .is_enabled = ldo_regulator_is_enabled, |
| 847 | .enable = ldo_regulator_enable, |
| 848 | .disable = ldo_regulator_disable, |
| 849 | .get_voltage = ldo_regulator_get_voltage, |
| 850 | }; |
| 851 | |
| 852 | static int ldo_regulator_register(struct snd_soc_codec *codec, |
| 853 | struct regulator_init_data *init_data, |
| 854 | int voltage) |
| 855 | { |
| 856 | struct ldo_regulator *ldo; |
Axel Lin | 5b13de7 | 2011-10-20 18:32:59 +0800 | [diff] [blame] | 857 | struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 858 | struct regulator_config config = { }; |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 859 | |
| 860 | ldo = kzalloc(sizeof(struct ldo_regulator), GFP_KERNEL); |
| 861 | |
Sachin Kamat | be81333 | 2014-06-20 15:28:59 +0530 | [diff] [blame] | 862 | if (!ldo) |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 863 | return -ENOMEM; |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 864 | |
| 865 | ldo->desc.name = kstrdup(dev_name(codec->dev), GFP_KERNEL); |
| 866 | if (!ldo->desc.name) { |
| 867 | kfree(ldo); |
| 868 | dev_err(codec->dev, "failed to allocate decs name memory\n"); |
| 869 | return -ENOMEM; |
| 870 | } |
| 871 | |
| 872 | ldo->desc.type = REGULATOR_VOLTAGE; |
| 873 | ldo->desc.owner = THIS_MODULE; |
| 874 | ldo->desc.ops = &ldo_regulator_ops; |
| 875 | ldo->desc.n_voltages = 1; |
| 876 | |
| 877 | ldo->codec_data = codec; |
| 878 | ldo->voltage = voltage; |
| 879 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 880 | config.dev = codec->dev; |
| 881 | config.driver_data = ldo; |
| 882 | config.init_data = init_data; |
| 883 | |
| 884 | ldo->dev = regulator_register(&ldo->desc, &config); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 885 | if (IS_ERR(ldo->dev)) { |
Dan Carpenter | 62f75aa | 2011-03-08 14:39:24 +0300 | [diff] [blame] | 886 | int ret = PTR_ERR(ldo->dev); |
| 887 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 888 | dev_err(codec->dev, "failed to register regulator\n"); |
| 889 | kfree(ldo->desc.name); |
| 890 | kfree(ldo); |
| 891 | |
Dan Carpenter | 62f75aa | 2011-03-08 14:39:24 +0300 | [diff] [blame] | 892 | return ret; |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 893 | } |
Axel Lin | 5b13de7 | 2011-10-20 18:32:59 +0800 | [diff] [blame] | 894 | sgtl5000->ldo = ldo; |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | static int ldo_regulator_remove(struct snd_soc_codec *codec) |
| 900 | { |
| 901 | struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec); |
| 902 | struct ldo_regulator *ldo = sgtl5000->ldo; |
| 903 | |
| 904 | if (!ldo) |
| 905 | return 0; |
| 906 | |
| 907 | regulator_unregister(ldo->dev); |
| 908 | kfree(ldo->desc.name); |
| 909 | kfree(ldo); |
| 910 | |
| 911 | return 0; |
| 912 | } |
Mark Brown | 333802e | 2011-03-22 12:02:33 +0000 | [diff] [blame] | 913 | #else |
| 914 | static int ldo_regulator_register(struct snd_soc_codec *codec, |
| 915 | struct regulator_init_data *init_data, |
| 916 | int voltage) |
| 917 | { |
Wolfram Sang | 09bddc8 | 2011-07-18 17:53:04 +0200 | [diff] [blame] | 918 | dev_err(codec->dev, "this setup needs regulator support in the kernel\n"); |
Mark Brown | 333802e | 2011-03-22 12:02:33 +0000 | [diff] [blame] | 919 | return -EINVAL; |
| 920 | } |
| 921 | |
| 922 | static int ldo_regulator_remove(struct snd_soc_codec *codec) |
| 923 | { |
| 924 | return 0; |
| 925 | } |
| 926 | #endif |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 927 | |
| 928 | /* |
| 929 | * set dac bias |
| 930 | * common state changes: |
| 931 | * startup: |
| 932 | * off --> standby --> prepare --> on |
| 933 | * standby --> prepare --> on |
| 934 | * |
| 935 | * stop: |
| 936 | * on --> prepare --> standby |
| 937 | */ |
| 938 | static int sgtl5000_set_bias_level(struct snd_soc_codec *codec, |
| 939 | enum snd_soc_bias_level level) |
| 940 | { |
| 941 | int ret; |
| 942 | struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec); |
| 943 | |
| 944 | switch (level) { |
| 945 | case SND_SOC_BIAS_ON: |
| 946 | case SND_SOC_BIAS_PREPARE: |
| 947 | break; |
| 948 | case SND_SOC_BIAS_STANDBY: |
| 949 | if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) { |
| 950 | ret = regulator_bulk_enable( |
| 951 | ARRAY_SIZE(sgtl5000->supplies), |
| 952 | sgtl5000->supplies); |
| 953 | if (ret) |
| 954 | return ret; |
| 955 | udelay(10); |
Mark Brown | 2bdc1bb | 2013-06-03 10:20:39 +0100 | [diff] [blame] | 956 | |
| 957 | regcache_cache_only(sgtl5000->regmap, false); |
| 958 | |
| 959 | ret = regcache_sync(sgtl5000->regmap); |
| 960 | if (ret != 0) { |
| 961 | dev_err(codec->dev, |
| 962 | "Failed to restore cache: %d\n", ret); |
| 963 | |
| 964 | regcache_cache_only(sgtl5000->regmap, true); |
| 965 | regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies), |
| 966 | sgtl5000->supplies); |
| 967 | |
| 968 | return ret; |
| 969 | } |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | break; |
| 973 | case SND_SOC_BIAS_OFF: |
Mark Brown | 2bdc1bb | 2013-06-03 10:20:39 +0100 | [diff] [blame] | 974 | regcache_cache_only(sgtl5000->regmap, true); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 975 | regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies), |
| 976 | sgtl5000->supplies); |
| 977 | break; |
| 978 | } |
| 979 | |
| 980 | codec->dapm.bias_level = level; |
| 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | #define SGTL5000_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\ |
| 985 | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 986 | SNDRV_PCM_FMTBIT_S24_LE |\ |
| 987 | SNDRV_PCM_FMTBIT_S32_LE) |
| 988 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 989 | static const struct snd_soc_dai_ops sgtl5000_ops = { |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 990 | .hw_params = sgtl5000_pcm_hw_params, |
| 991 | .digital_mute = sgtl5000_digital_mute, |
| 992 | .set_fmt = sgtl5000_set_dai_fmt, |
| 993 | .set_sysclk = sgtl5000_set_dai_sysclk, |
| 994 | }; |
| 995 | |
| 996 | static struct snd_soc_dai_driver sgtl5000_dai = { |
| 997 | .name = "sgtl5000", |
| 998 | .playback = { |
| 999 | .stream_name = "Playback", |
| 1000 | .channels_min = 1, |
| 1001 | .channels_max = 2, |
| 1002 | /* |
| 1003 | * only support 8~48K + 96K, |
| 1004 | * TODO modify hw_param to support more |
| 1005 | */ |
| 1006 | .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000, |
| 1007 | .formats = SGTL5000_FORMATS, |
| 1008 | }, |
| 1009 | .capture = { |
| 1010 | .stream_name = "Capture", |
| 1011 | .channels_min = 1, |
| 1012 | .channels_max = 2, |
| 1013 | .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000, |
| 1014 | .formats = SGTL5000_FORMATS, |
| 1015 | }, |
| 1016 | .ops = &sgtl5000_ops, |
| 1017 | .symmetric_rates = 1, |
| 1018 | }; |
| 1019 | |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 1020 | static bool sgtl5000_volatile(struct device *dev, unsigned int reg) |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1021 | { |
| 1022 | switch (reg) { |
| 1023 | case SGTL5000_CHIP_ID: |
| 1024 | case SGTL5000_CHIP_ADCDAC_CTRL: |
| 1025 | case SGTL5000_CHIP_ANA_STATUS: |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 1026 | return true; |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1027 | } |
| 1028 | |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 1029 | return false; |
| 1030 | } |
| 1031 | |
| 1032 | static bool sgtl5000_readable(struct device *dev, unsigned int reg) |
| 1033 | { |
| 1034 | switch (reg) { |
| 1035 | case SGTL5000_CHIP_ID: |
| 1036 | case SGTL5000_CHIP_DIG_POWER: |
| 1037 | case SGTL5000_CHIP_CLK_CTRL: |
| 1038 | case SGTL5000_CHIP_I2S_CTRL: |
| 1039 | case SGTL5000_CHIP_SSS_CTRL: |
| 1040 | case SGTL5000_CHIP_ADCDAC_CTRL: |
| 1041 | case SGTL5000_CHIP_DAC_VOL: |
| 1042 | case SGTL5000_CHIP_PAD_STRENGTH: |
| 1043 | case SGTL5000_CHIP_ANA_ADC_CTRL: |
| 1044 | case SGTL5000_CHIP_ANA_HP_CTRL: |
| 1045 | case SGTL5000_CHIP_ANA_CTRL: |
| 1046 | case SGTL5000_CHIP_LINREG_CTRL: |
| 1047 | case SGTL5000_CHIP_REF_CTRL: |
| 1048 | case SGTL5000_CHIP_MIC_CTRL: |
| 1049 | case SGTL5000_CHIP_LINE_OUT_CTRL: |
| 1050 | case SGTL5000_CHIP_LINE_OUT_VOL: |
| 1051 | case SGTL5000_CHIP_ANA_POWER: |
| 1052 | case SGTL5000_CHIP_PLL_CTRL: |
| 1053 | case SGTL5000_CHIP_CLK_TOP_CTRL: |
| 1054 | case SGTL5000_CHIP_ANA_STATUS: |
| 1055 | case SGTL5000_CHIP_SHORT_CTRL: |
| 1056 | case SGTL5000_CHIP_ANA_TEST2: |
| 1057 | case SGTL5000_DAP_CTRL: |
| 1058 | case SGTL5000_DAP_PEQ: |
| 1059 | case SGTL5000_DAP_BASS_ENHANCE: |
| 1060 | case SGTL5000_DAP_BASS_ENHANCE_CTRL: |
| 1061 | case SGTL5000_DAP_AUDIO_EQ: |
| 1062 | case SGTL5000_DAP_SURROUND: |
| 1063 | case SGTL5000_DAP_FLT_COEF_ACCESS: |
| 1064 | case SGTL5000_DAP_COEF_WR_B0_MSB: |
| 1065 | case SGTL5000_DAP_COEF_WR_B0_LSB: |
| 1066 | case SGTL5000_DAP_EQ_BASS_BAND0: |
| 1067 | case SGTL5000_DAP_EQ_BASS_BAND1: |
| 1068 | case SGTL5000_DAP_EQ_BASS_BAND2: |
| 1069 | case SGTL5000_DAP_EQ_BASS_BAND3: |
| 1070 | case SGTL5000_DAP_EQ_BASS_BAND4: |
| 1071 | case SGTL5000_DAP_MAIN_CHAN: |
| 1072 | case SGTL5000_DAP_MIX_CHAN: |
| 1073 | case SGTL5000_DAP_AVC_CTRL: |
| 1074 | case SGTL5000_DAP_AVC_THRESHOLD: |
| 1075 | case SGTL5000_DAP_AVC_ATTACK: |
| 1076 | case SGTL5000_DAP_AVC_DECAY: |
| 1077 | case SGTL5000_DAP_COEF_WR_B1_MSB: |
| 1078 | case SGTL5000_DAP_COEF_WR_B1_LSB: |
| 1079 | case SGTL5000_DAP_COEF_WR_B2_MSB: |
| 1080 | case SGTL5000_DAP_COEF_WR_B2_LSB: |
| 1081 | case SGTL5000_DAP_COEF_WR_A1_MSB: |
| 1082 | case SGTL5000_DAP_COEF_WR_A1_LSB: |
| 1083 | case SGTL5000_DAP_COEF_WR_A2_MSB: |
| 1084 | case SGTL5000_DAP_COEF_WR_A2_LSB: |
| 1085 | return true; |
| 1086 | |
| 1087 | default: |
| 1088 | return false; |
| 1089 | } |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1090 | } |
| 1091 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1092 | /* |
| 1093 | * sgtl5000 has 3 internal power supplies: |
| 1094 | * 1. VAG, normally set to vdda/2 |
Fabio Estevam | 7f6d75d | 2014-10-07 10:50:56 -0300 | [diff] [blame] | 1095 | * 2. charge pump, set to different value |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1096 | * according to voltage of vdda and vddio |
| 1097 | * 3. line out VAG, normally set to vddio/2 |
| 1098 | * |
| 1099 | * and should be set according to: |
| 1100 | * 1. vddd provided by external or not |
| 1101 | * 2. vdda and vddio voltage value. > 3.1v or not |
| 1102 | * 3. chip revision >=0x11 or not. If >=0x11, not use external vddd. |
| 1103 | */ |
| 1104 | static int sgtl5000_set_power_regs(struct snd_soc_codec *codec) |
| 1105 | { |
| 1106 | int vddd; |
| 1107 | int vdda; |
| 1108 | int vddio; |
| 1109 | u16 ana_pwr; |
| 1110 | u16 lreg_ctrl; |
| 1111 | int vag; |
| 1112 | struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec); |
| 1113 | |
| 1114 | vdda = regulator_get_voltage(sgtl5000->supplies[VDDA].consumer); |
| 1115 | vddio = regulator_get_voltage(sgtl5000->supplies[VDDIO].consumer); |
| 1116 | vddd = regulator_get_voltage(sgtl5000->supplies[VDDD].consumer); |
| 1117 | |
| 1118 | vdda = vdda / 1000; |
| 1119 | vddio = vddio / 1000; |
| 1120 | vddd = vddd / 1000; |
| 1121 | |
| 1122 | if (vdda <= 0 || vddio <= 0 || vddd < 0) { |
| 1123 | dev_err(codec->dev, "regulator voltage not set correctly\n"); |
| 1124 | |
| 1125 | return -EINVAL; |
| 1126 | } |
| 1127 | |
| 1128 | /* according to datasheet, maximum voltage of supplies */ |
| 1129 | if (vdda > 3600 || vddio > 3600 || vddd > 1980) { |
| 1130 | dev_err(codec->dev, |
Fabio Estevam | cf1ee98 | 2011-12-28 09:55:15 -0200 | [diff] [blame] | 1131 | "exceed max voltage vdda %dmV vddio %dmV vddd %dmV\n", |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1132 | vdda, vddio, vddd); |
| 1133 | |
| 1134 | return -EINVAL; |
| 1135 | } |
| 1136 | |
| 1137 | /* reset value */ |
| 1138 | ana_pwr = snd_soc_read(codec, SGTL5000_CHIP_ANA_POWER); |
| 1139 | ana_pwr |= SGTL5000_DAC_STEREO | |
| 1140 | SGTL5000_ADC_STEREO | |
| 1141 | SGTL5000_REFTOP_POWERUP; |
| 1142 | lreg_ctrl = snd_soc_read(codec, SGTL5000_CHIP_LINREG_CTRL); |
| 1143 | |
| 1144 | if (vddio < 3100 && vdda < 3100) { |
| 1145 | /* enable internal oscillator used for charge pump */ |
| 1146 | snd_soc_update_bits(codec, SGTL5000_CHIP_CLK_TOP_CTRL, |
| 1147 | SGTL5000_INT_OSC_EN, |
| 1148 | SGTL5000_INT_OSC_EN); |
| 1149 | /* Enable VDDC charge pump */ |
| 1150 | ana_pwr |= SGTL5000_VDDC_CHRGPMP_POWERUP; |
| 1151 | } else if (vddio >= 3100 && vdda >= 3100) { |
| 1152 | /* |
| 1153 | * if vddio and vddd > 3.1v, |
| 1154 | * charge pump should be clean before set ana_pwr |
| 1155 | */ |
| 1156 | snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, |
| 1157 | SGTL5000_VDDC_CHRGPMP_POWERUP, 0); |
| 1158 | |
| 1159 | /* VDDC use VDDIO rail */ |
| 1160 | lreg_ctrl |= SGTL5000_VDDC_ASSN_OVRD; |
| 1161 | lreg_ctrl |= SGTL5000_VDDC_MAN_ASSN_VDDIO << |
| 1162 | SGTL5000_VDDC_MAN_ASSN_SHIFT; |
| 1163 | } |
| 1164 | |
| 1165 | snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL, lreg_ctrl); |
| 1166 | |
| 1167 | snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER, ana_pwr); |
| 1168 | |
| 1169 | /* set voltage to register */ |
| 1170 | snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL, |
Axel Lin | 064a4bc | 2011-10-20 18:49:29 +0800 | [diff] [blame] | 1171 | SGTL5000_LINREG_VDDD_MASK, 0x8); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1172 | |
| 1173 | /* |
| 1174 | * if vddd linear reg has been enabled, |
| 1175 | * simple digital supply should be clear to get |
| 1176 | * proper VDDD voltage. |
| 1177 | */ |
| 1178 | if (ana_pwr & SGTL5000_LINEREG_D_POWERUP) |
| 1179 | snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, |
| 1180 | SGTL5000_LINREG_SIMPLE_POWERUP, |
| 1181 | 0); |
| 1182 | else |
| 1183 | snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, |
| 1184 | SGTL5000_LINREG_SIMPLE_POWERUP | |
| 1185 | SGTL5000_STARTUP_POWERUP, |
| 1186 | 0); |
| 1187 | |
| 1188 | /* |
| 1189 | * set ADC/DAC VAG to vdda / 2, |
| 1190 | * should stay in range (0.8v, 1.575v) |
| 1191 | */ |
| 1192 | vag = vdda / 2; |
| 1193 | if (vag <= SGTL5000_ANA_GND_BASE) |
| 1194 | vag = 0; |
| 1195 | else if (vag >= SGTL5000_ANA_GND_BASE + SGTL5000_ANA_GND_STP * |
| 1196 | (SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT)) |
| 1197 | vag = SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT; |
| 1198 | else |
| 1199 | vag = (vag - SGTL5000_ANA_GND_BASE) / SGTL5000_ANA_GND_STP; |
| 1200 | |
| 1201 | snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL, |
Axel Lin | 33cb92c | 2011-10-21 09:54:43 +0800 | [diff] [blame] | 1202 | SGTL5000_ANA_GND_MASK, vag << SGTL5000_ANA_GND_SHIFT); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1203 | |
| 1204 | /* set line out VAG to vddio / 2, in range (0.8v, 1.675v) */ |
| 1205 | vag = vddio / 2; |
| 1206 | if (vag <= SGTL5000_LINE_OUT_GND_BASE) |
| 1207 | vag = 0; |
| 1208 | else if (vag >= SGTL5000_LINE_OUT_GND_BASE + |
| 1209 | SGTL5000_LINE_OUT_GND_STP * SGTL5000_LINE_OUT_GND_MAX) |
| 1210 | vag = SGTL5000_LINE_OUT_GND_MAX; |
| 1211 | else |
| 1212 | vag = (vag - SGTL5000_LINE_OUT_GND_BASE) / |
| 1213 | SGTL5000_LINE_OUT_GND_STP; |
| 1214 | |
| 1215 | snd_soc_update_bits(codec, SGTL5000_CHIP_LINE_OUT_CTRL, |
Axel Lin | 33cb92c | 2011-10-21 09:54:43 +0800 | [diff] [blame] | 1216 | SGTL5000_LINE_OUT_CURRENT_MASK | |
| 1217 | SGTL5000_LINE_OUT_GND_MASK, |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1218 | vag << SGTL5000_LINE_OUT_GND_SHIFT | |
| 1219 | SGTL5000_LINE_OUT_CURRENT_360u << |
| 1220 | SGTL5000_LINE_OUT_CURRENT_SHIFT); |
| 1221 | |
| 1222 | return 0; |
| 1223 | } |
| 1224 | |
Wolfram Sang | e94a406 | 2011-07-18 17:53:03 +0200 | [diff] [blame] | 1225 | static int sgtl5000_replace_vddd_with_ldo(struct snd_soc_codec *codec) |
| 1226 | { |
| 1227 | struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec); |
| 1228 | int ret; |
| 1229 | |
| 1230 | /* set internal ldo to 1.2v */ |
| 1231 | ret = ldo_regulator_register(codec, &ldo_init_data, LDO_VOLTAGE); |
| 1232 | if (ret) { |
| 1233 | dev_err(codec->dev, |
| 1234 | "Failed to register vddd internal supplies: %d\n", ret); |
| 1235 | return ret; |
| 1236 | } |
| 1237 | |
| 1238 | sgtl5000->supplies[VDDD].supply = LDO_CONSUMER_NAME; |
| 1239 | |
Wolfram Sang | e94a406 | 2011-07-18 17:53:03 +0200 | [diff] [blame] | 1240 | dev_info(codec->dev, "Using internal LDO instead of VDDD\n"); |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1244 | static int sgtl5000_enable_regulators(struct snd_soc_codec *codec) |
| 1245 | { |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1246 | int ret; |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1247 | int i; |
| 1248 | int external_vddd = 0; |
| 1249 | struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec); |
Shawn Guo | 11db0da | 2013-12-13 14:43:03 +0800 | [diff] [blame] | 1250 | struct regulator *vddd; |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1251 | |
| 1252 | for (i = 0; i < ARRAY_SIZE(sgtl5000->supplies); i++) |
| 1253 | sgtl5000->supplies[i].supply = supply_names[i]; |
| 1254 | |
Shawn Guo | 11db0da | 2013-12-13 14:43:03 +0800 | [diff] [blame] | 1255 | /* External VDDD only works before revision 0x11 */ |
| 1256 | if (sgtl5000->revision < 0x11) { |
| 1257 | vddd = regulator_get_optional(codec->dev, "VDDD"); |
| 1258 | if (IS_ERR(vddd)) { |
| 1259 | /* See if it's just not registered yet */ |
| 1260 | if (PTR_ERR(vddd) == -EPROBE_DEFER) |
| 1261 | return -EPROBE_DEFER; |
| 1262 | } else { |
| 1263 | external_vddd = 1; |
| 1264 | regulator_put(vddd); |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | if (!external_vddd) { |
Wolfram Sang | e94a406 | 2011-07-18 17:53:03 +0200 | [diff] [blame] | 1269 | ret = sgtl5000_replace_vddd_with_ldo(codec); |
| 1270 | if (ret) |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1271 | return ret; |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1272 | } |
| 1273 | |
Fabio Estevam | e42be7e | 2014-07-07 14:26:48 -0300 | [diff] [blame] | 1274 | ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(sgtl5000->supplies), |
Shawn Guo | 11db0da | 2013-12-13 14:43:03 +0800 | [diff] [blame] | 1275 | sgtl5000->supplies); |
| 1276 | if (ret) |
| 1277 | goto err_ldo_remove; |
| 1278 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1279 | ret = regulator_bulk_enable(ARRAY_SIZE(sgtl5000->supplies), |
| 1280 | sgtl5000->supplies); |
| 1281 | if (ret) |
Fabio Estevam | e42be7e | 2014-07-07 14:26:48 -0300 | [diff] [blame] | 1282 | goto err_regulator_free; |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1283 | |
| 1284 | /* wait for all power rails bring up */ |
| 1285 | udelay(10); |
| 1286 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1287 | return 0; |
| 1288 | |
Fabio Estevam | e42be7e | 2014-07-07 14:26:48 -0300 | [diff] [blame] | 1289 | err_regulator_free: |
| 1290 | regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies), |
| 1291 | sgtl5000->supplies); |
Shawn Guo | 11db0da | 2013-12-13 14:43:03 +0800 | [diff] [blame] | 1292 | err_ldo_remove: |
| 1293 | if (!external_vddd) |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1294 | ldo_regulator_remove(codec); |
| 1295 | return ret; |
| 1296 | |
| 1297 | } |
| 1298 | |
| 1299 | static int sgtl5000_probe(struct snd_soc_codec *codec) |
| 1300 | { |
| 1301 | int ret; |
| 1302 | struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec); |
| 1303 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1304 | ret = sgtl5000_enable_regulators(codec); |
| 1305 | if (ret) |
| 1306 | return ret; |
| 1307 | |
| 1308 | /* power up sgtl5000 */ |
| 1309 | ret = sgtl5000_set_power_regs(codec); |
| 1310 | if (ret) |
| 1311 | goto err; |
| 1312 | |
| 1313 | /* enable small pop, introduce 400ms delay in turning off */ |
| 1314 | snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL, |
| 1315 | SGTL5000_SMALL_POP, |
| 1316 | SGTL5000_SMALL_POP); |
| 1317 | |
| 1318 | /* disable short cut detector */ |
| 1319 | snd_soc_write(codec, SGTL5000_CHIP_SHORT_CTRL, 0); |
| 1320 | |
| 1321 | /* |
| 1322 | * set i2s as default input of sound switch |
| 1323 | * TODO: add sound switch to control and dapm widge. |
| 1324 | */ |
| 1325 | snd_soc_write(codec, SGTL5000_CHIP_SSS_CTRL, |
| 1326 | SGTL5000_DAC_SEL_I2S_IN << SGTL5000_DAC_SEL_SHIFT); |
| 1327 | snd_soc_write(codec, SGTL5000_CHIP_DIG_POWER, |
| 1328 | SGTL5000_ADC_EN | SGTL5000_DAC_EN); |
| 1329 | |
| 1330 | /* enable dac volume ramp by default */ |
| 1331 | snd_soc_write(codec, SGTL5000_CHIP_ADCDAC_CTRL, |
| 1332 | SGTL5000_DAC_VOL_RAMP_EN | |
| 1333 | SGTL5000_DAC_MUTE_RIGHT | |
| 1334 | SGTL5000_DAC_MUTE_LEFT); |
| 1335 | |
| 1336 | snd_soc_write(codec, SGTL5000_CHIP_PAD_STRENGTH, 0x015f); |
| 1337 | |
| 1338 | snd_soc_write(codec, SGTL5000_CHIP_ANA_CTRL, |
| 1339 | SGTL5000_HP_ZCD_EN | |
| 1340 | SGTL5000_ADC_ZCD_EN); |
| 1341 | |
Jean-Michel Hautbois | bd0593f | 2014-10-14 08:43:11 +0200 | [diff] [blame] | 1342 | snd_soc_update_bits(codec, SGTL5000_CHIP_MIC_CTRL, |
| 1343 | SGTL5000_BIAS_R_MASK, |
| 1344 | sgtl5000->micbias_resistor << SGTL5000_BIAS_R_SHIFT); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1345 | |
Jean-Michel Hautbois | 8735779 | 2014-10-14 08:43:12 +0200 | [diff] [blame] | 1346 | snd_soc_update_bits(codec, SGTL5000_CHIP_MIC_CTRL, |
| 1347 | SGTL5000_BIAS_R_MASK, |
| 1348 | sgtl5000->micbias_voltage << SGTL5000_BIAS_R_SHIFT); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1349 | /* |
| 1350 | * disable DAP |
| 1351 | * TODO: |
| 1352 | * Enable DAP in kcontrol and dapm. |
| 1353 | */ |
| 1354 | snd_soc_write(codec, SGTL5000_DAP_CTRL, 0); |
| 1355 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1356 | return 0; |
| 1357 | |
| 1358 | err: |
| 1359 | regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies), |
| 1360 | sgtl5000->supplies); |
Fabio Estevam | e42be7e | 2014-07-07 14:26:48 -0300 | [diff] [blame] | 1361 | regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies), |
| 1362 | sgtl5000->supplies); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1363 | ldo_regulator_remove(codec); |
| 1364 | |
| 1365 | return ret; |
| 1366 | } |
| 1367 | |
| 1368 | static int sgtl5000_remove(struct snd_soc_codec *codec) |
| 1369 | { |
| 1370 | struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec); |
| 1371 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1372 | regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies), |
| 1373 | sgtl5000->supplies); |
Fabio Estevam | e42be7e | 2014-07-07 14:26:48 -0300 | [diff] [blame] | 1374 | regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies), |
| 1375 | sgtl5000->supplies); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1376 | ldo_regulator_remove(codec); |
| 1377 | |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
Mark Brown | 61a142b | 2011-02-28 14:33:01 +0000 | [diff] [blame] | 1381 | static struct snd_soc_codec_driver sgtl5000_driver = { |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1382 | .probe = sgtl5000_probe, |
| 1383 | .remove = sgtl5000_remove, |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1384 | .set_bias_level = sgtl5000_set_bias_level, |
Lars-Peter Clausen | e649057 | 2014-09-06 14:29:35 +0200 | [diff] [blame] | 1385 | .suspend_bias_off = true, |
Fabio Estevam | 8998963 | 2012-01-22 14:49:42 -0200 | [diff] [blame] | 1386 | .controls = sgtl5000_snd_controls, |
| 1387 | .num_controls = ARRAY_SIZE(sgtl5000_snd_controls), |
Mark Brown | 5e0ac52 | 2012-01-23 10:16:31 +0000 | [diff] [blame] | 1388 | .dapm_widgets = sgtl5000_dapm_widgets, |
| 1389 | .num_dapm_widgets = ARRAY_SIZE(sgtl5000_dapm_widgets), |
| 1390 | .dapm_routes = sgtl5000_dapm_routes, |
| 1391 | .num_dapm_routes = ARRAY_SIZE(sgtl5000_dapm_routes), |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1392 | }; |
| 1393 | |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 1394 | static const struct regmap_config sgtl5000_regmap = { |
| 1395 | .reg_bits = 16, |
| 1396 | .val_bits = 16, |
Fabio Estevam | cb23e85 | 2013-07-04 20:01:01 -0300 | [diff] [blame] | 1397 | .reg_stride = 2, |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 1398 | |
| 1399 | .max_register = SGTL5000_MAX_REG_OFFSET, |
| 1400 | .volatile_reg = sgtl5000_volatile, |
| 1401 | .readable_reg = sgtl5000_readable, |
| 1402 | |
| 1403 | .cache_type = REGCACHE_RBTREE, |
| 1404 | .reg_defaults = sgtl5000_reg_defaults, |
| 1405 | .num_reg_defaults = ARRAY_SIZE(sgtl5000_reg_defaults), |
| 1406 | }; |
| 1407 | |
Fabio Estevam | af8ee11 | 2013-05-09 21:15:47 -0300 | [diff] [blame] | 1408 | /* |
| 1409 | * Write all the default values from sgtl5000_reg_defaults[] array into the |
| 1410 | * sgtl5000 registers, to make sure we always start with the sane registers |
| 1411 | * values as stated in the datasheet. |
| 1412 | * |
| 1413 | * Since sgtl5000 does not have a reset line, nor a reset command in software, |
| 1414 | * we follow this approach to guarantee we always start from the default values |
| 1415 | * and avoid problems like, not being able to probe after an audio playback |
| 1416 | * followed by a system reset or a 'reboot' command in Linux |
| 1417 | */ |
| 1418 | static int sgtl5000_fill_defaults(struct sgtl5000_priv *sgtl5000) |
| 1419 | { |
| 1420 | int i, ret, val, index; |
| 1421 | |
| 1422 | for (i = 0; i < ARRAY_SIZE(sgtl5000_reg_defaults); i++) { |
| 1423 | val = sgtl5000_reg_defaults[i].def; |
| 1424 | index = sgtl5000_reg_defaults[i].reg; |
| 1425 | ret = regmap_write(sgtl5000->regmap, index, val); |
| 1426 | if (ret) |
| 1427 | return ret; |
| 1428 | } |
| 1429 | |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 1433 | static int sgtl5000_i2c_probe(struct i2c_client *client, |
| 1434 | const struct i2c_device_id *id) |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1435 | { |
| 1436 | struct sgtl5000_priv *sgtl5000; |
Fabio Estevam | b871f1a | 2013-05-09 21:15:46 -0300 | [diff] [blame] | 1437 | int ret, reg, rev; |
Fabio Estevam | 6f4d2b3 | 2014-10-02 17:36:05 -0300 | [diff] [blame] | 1438 | unsigned int mclk; |
Jean-Michel Hautbois | bd0593f | 2014-10-14 08:43:11 +0200 | [diff] [blame] | 1439 | struct device_node *np = client->dev.of_node; |
| 1440 | u32 value; |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1441 | |
Fabio Estevam | 512fa7c | 2011-12-28 11:30:11 -0200 | [diff] [blame] | 1442 | sgtl5000 = devm_kzalloc(&client->dev, sizeof(struct sgtl5000_priv), |
| 1443 | GFP_KERNEL); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1444 | if (!sgtl5000) |
| 1445 | return -ENOMEM; |
| 1446 | |
Fabio Estevam | e5d80e8 | 2013-05-04 15:39:34 -0300 | [diff] [blame] | 1447 | sgtl5000->regmap = devm_regmap_init_i2c(client, &sgtl5000_regmap); |
| 1448 | if (IS_ERR(sgtl5000->regmap)) { |
| 1449 | ret = PTR_ERR(sgtl5000->regmap); |
| 1450 | dev_err(&client->dev, "Failed to allocate regmap: %d\n", ret); |
| 1451 | return ret; |
| 1452 | } |
| 1453 | |
Fabio Estevam | 9e13f34 | 2013-06-09 22:07:46 -0300 | [diff] [blame] | 1454 | sgtl5000->mclk = devm_clk_get(&client->dev, NULL); |
| 1455 | if (IS_ERR(sgtl5000->mclk)) { |
| 1456 | ret = PTR_ERR(sgtl5000->mclk); |
| 1457 | dev_err(&client->dev, "Failed to get mclock: %d\n", ret); |
Shawn Guo | 46a5905 | 2013-07-16 09:17:27 +0800 | [diff] [blame] | 1458 | /* Defer the probe to see if the clk will be provided later */ |
| 1459 | if (ret == -ENOENT) |
| 1460 | return -EPROBE_DEFER; |
Fabio Estevam | 9e13f34 | 2013-06-09 22:07:46 -0300 | [diff] [blame] | 1461 | return ret; |
| 1462 | } |
| 1463 | |
Fabio Estevam | 6f4d2b3 | 2014-10-02 17:36:05 -0300 | [diff] [blame] | 1464 | /* SGTL5000 SYS_MCLK should be between 8 and 27 MHz */ |
| 1465 | mclk = clk_get_rate(sgtl5000->mclk); |
| 1466 | if (mclk < 8000000 || mclk > 27000000) { |
| 1467 | dev_err(&client->dev, "Invalid SYS_CLK frequency: %u.%03uMHz\n", |
| 1468 | mclk / 1000000, mclk / 1000 % 1000); |
| 1469 | return -EINVAL; |
| 1470 | } |
| 1471 | |
Fabio Estevam | 9e13f34 | 2013-06-09 22:07:46 -0300 | [diff] [blame] | 1472 | ret = clk_prepare_enable(sgtl5000->mclk); |
| 1473 | if (ret) |
| 1474 | return ret; |
| 1475 | |
Fabio Estevam | b871f1a | 2013-05-09 21:15:46 -0300 | [diff] [blame] | 1476 | /* read chip information */ |
| 1477 | ret = regmap_read(sgtl5000->regmap, SGTL5000_CHIP_ID, ®); |
| 1478 | if (ret) |
Fabio Estevam | 9e13f34 | 2013-06-09 22:07:46 -0300 | [diff] [blame] | 1479 | goto disable_clk; |
Fabio Estevam | b871f1a | 2013-05-09 21:15:46 -0300 | [diff] [blame] | 1480 | |
| 1481 | if (((reg & SGTL5000_PARTID_MASK) >> SGTL5000_PARTID_SHIFT) != |
| 1482 | SGTL5000_PARTID_PART_ID) { |
| 1483 | dev_err(&client->dev, |
| 1484 | "Device with ID register %x is not a sgtl5000\n", reg); |
Fabio Estevam | 9e13f34 | 2013-06-09 22:07:46 -0300 | [diff] [blame] | 1485 | ret = -ENODEV; |
| 1486 | goto disable_clk; |
Fabio Estevam | b871f1a | 2013-05-09 21:15:46 -0300 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | rev = (reg & SGTL5000_REVID_MASK) >> SGTL5000_REVID_SHIFT; |
| 1490 | dev_info(&client->dev, "sgtl5000 revision 0x%x\n", rev); |
Shawn Guo | 252e91f | 2013-12-13 14:43:02 +0800 | [diff] [blame] | 1491 | sgtl5000->revision = rev; |
Fabio Estevam | b871f1a | 2013-05-09 21:15:46 -0300 | [diff] [blame] | 1492 | |
Jean-Michel Hautbois | bd0593f | 2014-10-14 08:43:11 +0200 | [diff] [blame] | 1493 | if (np) { |
| 1494 | if (!of_property_read_u32(np, |
| 1495 | "micbias-resistor-k-ohms", &value)) { |
| 1496 | switch (value) { |
| 1497 | case SGTL5000_MICBIAS_OFF: |
| 1498 | sgtl5000->micbias_resistor = 0; |
| 1499 | break; |
| 1500 | case SGTL5000_MICBIAS_2K: |
| 1501 | sgtl5000->micbias_resistor = 1; |
| 1502 | break; |
| 1503 | case SGTL5000_MICBIAS_4K: |
| 1504 | sgtl5000->micbias_resistor = 2; |
| 1505 | break; |
| 1506 | case SGTL5000_MICBIAS_8K: |
| 1507 | sgtl5000->micbias_resistor = 3; |
| 1508 | break; |
| 1509 | default: |
| 1510 | sgtl5000->micbias_resistor = 2; |
| 1511 | dev_err(&client->dev, |
| 1512 | "Unsuitable MicBias resistor\n"); |
| 1513 | } |
| 1514 | } else { |
| 1515 | /* default is 4Kohms */ |
| 1516 | sgtl5000->micbias_resistor = 2; |
| 1517 | } |
Jean-Michel Hautbois | 8735779 | 2014-10-14 08:43:12 +0200 | [diff] [blame] | 1518 | if (!of_property_read_u32(np, |
| 1519 | "micbias-voltage-m-volts", &value)) { |
| 1520 | /* 1250mV => 0 */ |
| 1521 | /* steps of 250mV */ |
| 1522 | if ((value >= 1250) && (value <= 3000)) |
| 1523 | sgtl5000->micbias_voltage = (value / 250) - 5; |
| 1524 | else { |
| 1525 | sgtl5000->micbias_voltage = 0; |
Jean-Michel Hautbois | bd0593f | 2014-10-14 08:43:11 +0200 | [diff] [blame] | 1526 | dev_err(&client->dev, |
| 1527 | "Unsuitable MicBias resistor\n"); |
| 1528 | } |
| 1529 | } else { |
Jean-Michel Hautbois | 8735779 | 2014-10-14 08:43:12 +0200 | [diff] [blame] | 1530 | sgtl5000->micbias_voltage = 0; |
Jean-Michel Hautbois | bd0593f | 2014-10-14 08:43:11 +0200 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1534 | i2c_set_clientdata(client, sgtl5000); |
| 1535 | |
Fabio Estevam | af8ee11 | 2013-05-09 21:15:47 -0300 | [diff] [blame] | 1536 | /* Ensure sgtl5000 will start with sane register values */ |
| 1537 | ret = sgtl5000_fill_defaults(sgtl5000); |
| 1538 | if (ret) |
Fabio Estevam | 9e13f34 | 2013-06-09 22:07:46 -0300 | [diff] [blame] | 1539 | goto disable_clk; |
Fabio Estevam | af8ee11 | 2013-05-09 21:15:47 -0300 | [diff] [blame] | 1540 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1541 | ret = snd_soc_register_codec(&client->dev, |
| 1542 | &sgtl5000_driver, &sgtl5000_dai, 1); |
Fabio Estevam | 9e13f34 | 2013-06-09 22:07:46 -0300 | [diff] [blame] | 1543 | if (ret) |
| 1544 | goto disable_clk; |
| 1545 | |
| 1546 | return 0; |
| 1547 | |
| 1548 | disable_clk: |
| 1549 | clk_disable_unprepare(sgtl5000->mclk); |
Fabio Estevam | 512fa7c | 2011-12-28 11:30:11 -0200 | [diff] [blame] | 1550 | return ret; |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1551 | } |
| 1552 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 1553 | static int sgtl5000_i2c_remove(struct i2c_client *client) |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1554 | { |
Fabio Estevam | 7c647af | 2013-06-10 10:24:41 -0300 | [diff] [blame] | 1555 | struct sgtl5000_priv *sgtl5000 = i2c_get_clientdata(client); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1556 | |
Fabio Estevam | 9e13f34 | 2013-06-09 22:07:46 -0300 | [diff] [blame] | 1557 | snd_soc_unregister_codec(&client->dev); |
| 1558 | clk_disable_unprepare(sgtl5000->mclk); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1559 | return 0; |
| 1560 | } |
| 1561 | |
| 1562 | static const struct i2c_device_id sgtl5000_id[] = { |
| 1563 | {"sgtl5000", 0}, |
| 1564 | {}, |
| 1565 | }; |
| 1566 | |
| 1567 | MODULE_DEVICE_TABLE(i2c, sgtl5000_id); |
| 1568 | |
Shawn Guo | 58e4942 | 2011-07-22 00:28:51 +0800 | [diff] [blame] | 1569 | static const struct of_device_id sgtl5000_dt_ids[] = { |
| 1570 | { .compatible = "fsl,sgtl5000", }, |
| 1571 | { /* sentinel */ } |
| 1572 | }; |
Axel Lin | 4c54c6d | 2011-08-11 22:19:16 +0800 | [diff] [blame] | 1573 | MODULE_DEVICE_TABLE(of, sgtl5000_dt_ids); |
Shawn Guo | 58e4942 | 2011-07-22 00:28:51 +0800 | [diff] [blame] | 1574 | |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1575 | static struct i2c_driver sgtl5000_i2c_driver = { |
| 1576 | .driver = { |
| 1577 | .name = "sgtl5000", |
| 1578 | .owner = THIS_MODULE, |
Shawn Guo | 58e4942 | 2011-07-22 00:28:51 +0800 | [diff] [blame] | 1579 | .of_match_table = sgtl5000_dt_ids, |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1580 | }, |
| 1581 | .probe = sgtl5000_i2c_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 1582 | .remove = sgtl5000_i2c_remove, |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1583 | .id_table = sgtl5000_id, |
| 1584 | }; |
| 1585 | |
Mark Brown | 67d4509 | 2012-04-03 22:35:18 +0100 | [diff] [blame] | 1586 | module_i2c_driver(sgtl5000_i2c_driver); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1587 | |
| 1588 | MODULE_DESCRIPTION("Freescale SGTL5000 ALSA SoC Codec Driver"); |
Zeng Zhaoming | f7cb8a4 | 2012-01-16 15:18:11 +0800 | [diff] [blame] | 1589 | MODULE_AUTHOR("Zeng Zhaoming <zengzm.kernel@gmail.com>"); |
Zeng Zhaoming | 9b34e6c | 2011-02-24 02:08:21 +0800 | [diff] [blame] | 1590 | MODULE_LICENSE("GPL"); |