2 * sn95031.c - TI sn95031 Codec driver
4 * Copyright (C) 2010 Intel Corp
5 * Author: Vinod Koul <vinod.koul@intel.com>
6 * Author: Harsha Priya <priya.harsha@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
30 #include <asm/intel_scu_ipc.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/soc.h>
34 #include <sound/soc-dapm.h>
35 #include <sound/initval.h>
38 #define SN95031_RATES (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100)
39 #define SN95031_FORMATS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
48 static inline unsigned int sn95031_read(struct snd_soc_codec *codec,
54 ret = intel_scu_ipc_ioread8(reg, &value);
56 pr_err("read of %x failed, err %d\n", reg, ret);
61 static inline int sn95031_write(struct snd_soc_codec *codec,
62 unsigned int reg, unsigned int value)
66 ret = intel_scu_ipc_iowrite8(reg, value);
68 pr_err("write of %x failed, err %d\n", reg, ret);
72 static int sn95031_set_vaud_bias(struct snd_soc_codec *codec,
73 enum snd_soc_bias_level level)
79 case SND_SOC_BIAS_PREPARE:
80 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
81 pr_debug("vaud_bias powering up pll\n");
82 /* power up the pll */
83 snd_soc_write(codec, SN95031_AUDPLLCTRL, BIT(5));
85 snd_soc_update_bits(codec, SN95031_PCM2C2,
90 case SND_SOC_BIAS_STANDBY:
91 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
92 pr_debug("vaud_bias power up rail\n");
93 /* power up the rail */
94 snd_soc_write(codec, SN95031_VAUD,
95 BIT(2)|BIT(1)|BIT(0));
97 } else if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE) {
99 pr_debug("vaud_bias power dn pcm\n");
100 snd_soc_update_bits(codec, SN95031_PCM2C2, BIT(0), 0);
101 snd_soc_write(codec, SN95031_AUDPLLCTRL, 0);
106 case SND_SOC_BIAS_OFF:
107 pr_debug("vaud_bias _OFF doing rail shutdown\n");
108 snd_soc_write(codec, SN95031_VAUD, BIT(3));
112 codec->dapm.bias_level = level;
116 static int sn95031_vhs_event(struct snd_soc_dapm_widget *w,
117 struct snd_kcontrol *kcontrol, int event)
119 if (SND_SOC_DAPM_EVENT_ON(event)) {
120 pr_debug("VHS SND_SOC_DAPM_EVENT_ON doing rail startup now\n");
121 /* power up the rail */
122 snd_soc_write(w->codec, SN95031_VHSP, 0x3D);
123 snd_soc_write(w->codec, SN95031_VHSN, 0x3F);
125 } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
126 pr_debug("VHS SND_SOC_DAPM_EVENT_OFF doing rail shutdown\n");
127 snd_soc_write(w->codec, SN95031_VHSP, 0xC4);
128 snd_soc_write(w->codec, SN95031_VHSN, 0x04);
133 static int sn95031_vihf_event(struct snd_soc_dapm_widget *w,
134 struct snd_kcontrol *kcontrol, int event)
136 if (SND_SOC_DAPM_EVENT_ON(event)) {
137 pr_debug("VIHF SND_SOC_DAPM_EVENT_ON doing rail startup now\n");
138 /* power up the rail */
139 snd_soc_write(w->codec, SN95031_VIHF, 0x27);
141 } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
142 pr_debug("VIHF SND_SOC_DAPM_EVENT_OFF doing rail shutdown\n");
143 snd_soc_write(w->codec, SN95031_VIHF, 0x24);
149 static const struct snd_soc_dapm_widget sn95031_dapm_widgets[] = {
151 /* all end points mic, hs etc */
152 SND_SOC_DAPM_OUTPUT("HPOUTL"),
153 SND_SOC_DAPM_OUTPUT("HPOUTR"),
154 SND_SOC_DAPM_OUTPUT("EPOUT"),
155 SND_SOC_DAPM_OUTPUT("IHFOUTL"),
156 SND_SOC_DAPM_OUTPUT("IHFOUTR"),
157 SND_SOC_DAPM_OUTPUT("LINEOUTL"),
158 SND_SOC_DAPM_OUTPUT("LINEOUTR"),
159 SND_SOC_DAPM_OUTPUT("VIB1OUT"),
160 SND_SOC_DAPM_OUTPUT("VIB2OUT"),
162 SND_SOC_DAPM_SUPPLY("Headset Rail", SND_SOC_NOPM, 0, 0,
164 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
165 SND_SOC_DAPM_SUPPLY("Speaker Rail", SND_SOC_NOPM, 0, 0,
167 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
169 /* playback path driver enables */
170 SND_SOC_DAPM_PGA("Headset Left Playback",
171 SN95031_DRIVEREN, 0, 0, NULL, 0),
172 SND_SOC_DAPM_PGA("Headset Right Playback",
173 SN95031_DRIVEREN, 1, 0, NULL, 0),
174 SND_SOC_DAPM_PGA("Speaker Left Playback",
175 SN95031_DRIVEREN, 2, 0, NULL, 0),
176 SND_SOC_DAPM_PGA("Speaker Right Playback",
177 SN95031_DRIVEREN, 3, 0, NULL, 0),
178 SND_SOC_DAPM_PGA("Vibra1 Playback",
179 SN95031_DRIVEREN, 4, 0, NULL, 0),
180 SND_SOC_DAPM_PGA("Vibra2 Playback",
181 SN95031_DRIVEREN, 5, 0, NULL, 0),
182 SND_SOC_DAPM_PGA("Earpiece Playback",
183 SN95031_DRIVEREN, 6, 0, NULL, 0),
184 SND_SOC_DAPM_PGA("Lineout Left Playback",
185 SN95031_LOCTL, 0, 0, NULL, 0),
186 SND_SOC_DAPM_PGA("Lineout Right Playback",
187 SN95031_LOCTL, 4, 0, NULL, 0),
189 /* playback path filter enable */
190 SND_SOC_DAPM_PGA("Headset Left Filter",
191 SN95031_HSEPRXCTRL, 4, 0, NULL, 0),
192 SND_SOC_DAPM_PGA("Headset Right Filter",
193 SN95031_HSEPRXCTRL, 5, 0, NULL, 0),
194 SND_SOC_DAPM_PGA("Speaker Left Filter",
195 SN95031_IHFRXCTRL, 0, 0, NULL, 0),
196 SND_SOC_DAPM_PGA("Speaker Right Filter",
197 SN95031_IHFRXCTRL, 1, 0, NULL, 0),
200 SND_SOC_DAPM_DAC("HSDAC Left", "Headset",
201 SN95031_DACCONFIG, 0, 0),
202 SND_SOC_DAPM_DAC("HSDAC Right", "Headset",
203 SN95031_DACCONFIG, 1, 0),
204 SND_SOC_DAPM_DAC("IHFDAC Left", "Speaker",
205 SN95031_DACCONFIG, 2, 0),
206 SND_SOC_DAPM_DAC("IHFDAC Right", "Speaker",
207 SN95031_DACCONFIG, 3, 0),
208 SND_SOC_DAPM_DAC("Vibra1 DAC", "Vibra1",
209 SN95031_VIB1C5, 1, 0),
210 SND_SOC_DAPM_DAC("Vibra2 DAC", "Vibra2",
211 SN95031_VIB2C5, 1, 0),
214 static const struct snd_soc_dapm_route sn95031_audio_map[] = {
215 /* headset and earpiece map */
216 { "HPOUTL", NULL, "Headset Left Playback" },
217 { "HPOUTR", NULL, "Headset Right Playback" },
218 { "EPOUT", NULL, "Earpiece Playback" },
219 { "Headset Left Playback", NULL, "Headset Left Filter"},
220 { "Headset Right Playback", NULL, "Headset Right Filter"},
221 { "Earpiece Playback", NULL, "Headset Left Filter"},
222 { "Headset Left Filter", NULL, "HSDAC Left"},
223 { "Headset Right Filter", NULL, "HSDAC Right"},
224 { "HSDAC Left", NULL, "Headset Rail"},
225 { "HSDAC Right", NULL, "Headset Rail"},
228 { "IHFOUTL", "NULL", "Speaker Left Playback"},
229 { "IHFOUTR", "NULL", "Speaker Right Playback"},
230 { "Speaker Left Playback", NULL, "Speaker Left Filter"},
231 { "Speaker Right Playback", NULL, "Speaker Right Filter"},
232 { "Speaker Left Filter", NULL, "IHFDAC Left"},
233 { "Speaker Right Filter", NULL, "IHFDAC Right"},
234 { "IHFDAC Left", NULL, "Speaker Rail"},
235 { "IHFDAC Right", NULL, "Speaker Rail"},
238 { "VIB1OUT", NULL, "Vibra1 Playback"},
239 { "Vibra1 Playback", NULL, "Vibra1 DAC"},
241 { "VIB2OUT", NULL, "Vibra2 Playback"},
242 { "Vibra2 Playback", NULL, "Vibra2 DAC"},
245 { "LINEOUTL", NULL, "Lineout Left Playback"},
246 { "LINEOUTR", NULL, "Lineout Right Playback"},
247 { "Lineout Left Playback", NULL, "Headset Left Filter"},
248 { "Lineout Left Playback", NULL, "Speaker Left Filter"},
249 { "Lineout Left Playback", NULL, "Vibra1 DAC"},
250 { "Lineout Right Playback", NULL, "Headset Right Filter"},
251 { "Lineout Right Playback", NULL, "Speaker Right Filter"},
252 { "Lineout Right Playback", NULL, "Vibra2 DAC"},
255 /* speaker and headset mutes, for audio pops and clicks */
256 static int sn95031_pcm_hs_mute(struct snd_soc_dai *dai, int mute)
258 snd_soc_update_bits(dai->codec,
259 SN95031_HSLVOLCTRL, BIT(7), (!mute << 7));
260 snd_soc_update_bits(dai->codec,
261 SN95031_HSRVOLCTRL, BIT(7), (!mute << 7));
265 static int sn95031_pcm_spkr_mute(struct snd_soc_dai *dai, int mute)
267 snd_soc_update_bits(dai->codec,
268 SN95031_IHFLVOLCTRL, BIT(7), (!mute << 7));
269 snd_soc_update_bits(dai->codec,
270 SN95031_IHFRVOLCTRL, BIT(7), (!mute << 7));
274 int sn95031_pcm_hw_params(struct snd_pcm_substream *substream,
275 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
277 unsigned int format, rate;
279 switch (params_format(params)) {
280 case SNDRV_PCM_FORMAT_S16_LE:
281 format = BIT(4)|BIT(5);
284 case SNDRV_PCM_FORMAT_S24_LE:
290 snd_soc_update_bits(dai->codec, SN95031_PCM2C2,
291 BIT(4)|BIT(5), format);
293 switch (params_rate(params)) {
295 pr_debug("RATE_48000\n");
300 pr_debug("RATE_44100\n");
305 pr_err("ERR rate %d\n", params_rate(params));
308 snd_soc_update_bits(dai->codec, SN95031_PCM1C1, BIT(7), rate);
313 /* Codec DAI section */
314 static struct snd_soc_dai_ops sn95031_headset_dai_ops = {
315 .digital_mute = sn95031_pcm_hs_mute,
316 .hw_params = sn95031_pcm_hw_params,
319 static struct snd_soc_dai_ops sn95031_speaker_dai_ops = {
320 .digital_mute = sn95031_pcm_spkr_mute,
321 .hw_params = sn95031_pcm_hw_params,
324 static struct snd_soc_dai_ops sn95031_vib1_dai_ops = {
325 .hw_params = sn95031_pcm_hw_params,
328 static struct snd_soc_dai_ops sn95031_vib2_dai_ops = {
329 .hw_params = sn95031_pcm_hw_params,
332 struct snd_soc_dai_driver sn95031_dais[] = {
334 .name = "SN95031 Headset",
336 .stream_name = "Headset",
339 .rates = SN95031_RATES,
340 .formats = SN95031_FORMATS,
342 .ops = &sn95031_headset_dai_ops,
344 { .name = "SN95031 Speaker",
346 .stream_name = "Speaker",
349 .rates = SN95031_RATES,
350 .formats = SN95031_FORMATS,
352 .ops = &sn95031_speaker_dai_ops,
354 { .name = "SN95031 Vibra1",
356 .stream_name = "Vibra1",
359 .rates = SN95031_RATES,
360 .formats = SN95031_FORMATS,
362 .ops = &sn95031_vib1_dai_ops,
364 { .name = "SN95031 Vibra2",
366 .stream_name = "Vibra2",
369 .rates = SN95031_RATES,
370 .formats = SN95031_FORMATS,
372 .ops = &sn95031_vib2_dai_ops,
376 /* codec registration */
377 static int sn95031_codec_probe(struct snd_soc_codec *codec)
381 pr_debug("codec_probe called\n");
383 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
384 codec->dapm.idle_bias_off = 1;
386 /* PCM interface config
387 * This sets the pcm rx slot conguration to max 6 slots
388 * for max 4 dais (2 stereo and 2 mono)
390 snd_soc_write(codec, SN95031_PCM2RXSLOT01, 0x10);
391 snd_soc_write(codec, SN95031_PCM2RXSLOT23, 0x32);
392 snd_soc_write(codec, SN95031_PCM2RXSLOT45, 0x54);
394 * This sets the pcm port to slave and clock at 19.2Mhz which
395 * can support 6slots, sampling rate set per stream in hw-params
397 snd_soc_write(codec, SN95031_PCM1C1, 0x00);
398 snd_soc_write(codec, SN95031_PCM2C1, 0x01);
399 snd_soc_write(codec, SN95031_PCM2C2, 0x0A);
400 snd_soc_write(codec, SN95031_HSMIXER, BIT(0)|BIT(4));
401 /* vendor vibra workround, the vibras are muted by
402 * custom register so unmute them
404 snd_soc_write(codec, SN95031_SSR5, 0x80);
405 snd_soc_write(codec, SN95031_SSR6, 0x80);
406 snd_soc_write(codec, SN95031_VIB1C5, 0x00);
407 snd_soc_write(codec, SN95031_VIB2C5, 0x00);
408 /* configure vibras for pcm port */
409 snd_soc_write(codec, SN95031_VIB1C3, 0x00);
410 snd_soc_write(codec, SN95031_VIB2C3, 0x00);
412 /* soft mute ramp time */
413 snd_soc_write(codec, SN95031_SOFTMUTE, 0x3);
414 /* fix the initial volume at 1dB,
416 * 1dB give optimal swing on DAC, amps
418 snd_soc_write(codec, SN95031_HSLVOLCTRL, 0x08);
419 snd_soc_write(codec, SN95031_HSRVOLCTRL, 0x08);
420 snd_soc_write(codec, SN95031_IHFLVOLCTRL, 0x08);
421 snd_soc_write(codec, SN95031_IHFRVOLCTRL, 0x08);
422 /* dac mode and lineout workaround */
423 snd_soc_write(codec, SN95031_SSR2, 0x10);
424 snd_soc_write(codec, SN95031_SSR3, 0x40);
426 ret = snd_soc_dapm_new_controls(&codec->dapm, sn95031_dapm_widgets,
427 ARRAY_SIZE(sn95031_dapm_widgets));
429 pr_err("soc_dapm_new_control failed %d", ret);
430 ret = snd_soc_dapm_add_routes(&codec->dapm, sn95031_audio_map,
431 ARRAY_SIZE(sn95031_audio_map));
433 pr_err("soc_dapm_add_routes failed %d", ret);
438 static int sn95031_codec_remove(struct snd_soc_codec *codec)
440 pr_debug("codec_remove called\n");
441 sn95031_set_vaud_bias(codec, SND_SOC_BIAS_OFF);
446 struct snd_soc_codec_driver sn95031_codec = {
447 .probe = sn95031_codec_probe,
448 .remove = sn95031_codec_remove,
449 .read = sn95031_read,
450 .write = sn95031_write,
451 .set_bias_level = sn95031_set_vaud_bias,
454 static int __devinit sn95031_device_probe(struct platform_device *pdev)
456 pr_debug("codec device probe called for %s\n", dev_name(&pdev->dev));
457 return snd_soc_register_codec(&pdev->dev, &sn95031_codec,
458 sn95031_dais, ARRAY_SIZE(sn95031_dais));
461 static int __devexit sn95031_device_remove(struct platform_device *pdev)
463 pr_debug("codec device remove called\n");
464 snd_soc_unregister_codec(&pdev->dev);
468 static struct platform_driver sn95031_codec_driver = {
471 .owner = THIS_MODULE,
473 .probe = sn95031_device_probe,
474 .remove = sn95031_device_remove,
477 static int __init sn95031_init(void)
479 pr_debug("driver init called\n");
480 return platform_driver_register(&sn95031_codec_driver);
482 module_init(sn95031_init);
484 static void __exit sn95031_exit(void)
486 pr_debug("driver exit called\n");
487 platform_driver_unregister(&sn95031_codec_driver);
489 module_exit(sn95031_exit);
491 MODULE_DESCRIPTION("ASoC TI SN95031 codec driver");
492 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
493 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
494 MODULE_LICENSE("GPL v2");
495 MODULE_ALIAS("platform:sn95031");