Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * ak4641.c -- AK4641 ALSA Soc Audio driver
4 *
5 * Copyright (C) 2008 Harald Welte <laforge@gnufiish.org>
6 * Copyright (C) 2011 Dmitry Artamonow <mad_soft@inbox.ru>
7 *
8 * Based on ak4535.c by Richard Purdie
9 */
10
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/delay.h>
14#include <linux/gpio.h>
15#include <linux/pm.h>
16#include <linux/i2c.h>
17#include <linux/regmap.h>
18#include <linux/slab.h>
19#include <sound/core.h>
20#include <sound/pcm.h>
21#include <sound/pcm_params.h>
22#include <sound/soc.h>
23#include <sound/initval.h>
24#include <sound/tlv.h>
25#include <sound/ak4641.h>
26
27/* AK4641 register space */
28#define AK4641_PM1 0x00
29#define AK4641_PM2 0x01
30#define AK4641_SIG1 0x02
31#define AK4641_SIG2 0x03
32#define AK4641_MODE1 0x04
33#define AK4641_MODE2 0x05
34#define AK4641_DAC 0x06
35#define AK4641_MIC 0x07
36#define AK4641_TIMER 0x08
37#define AK4641_ALC1 0x09
38#define AK4641_ALC2 0x0a
39#define AK4641_PGA 0x0b
40#define AK4641_LATT 0x0c
41#define AK4641_RATT 0x0d
42#define AK4641_VOL 0x0e
43#define AK4641_STATUS 0x0f
44#define AK4641_EQLO 0x10
45#define AK4641_EQMID 0x11
46#define AK4641_EQHI 0x12
47#define AK4641_BTIF 0x13
48
49/* codec private data */
50struct ak4641_priv {
51 struct regmap *regmap;
52 unsigned int sysclk;
53 int deemph;
54 int playback_fs;
55};
56
57/*
58 * ak4641 register cache
59 */
60static const struct reg_default ak4641_reg_defaults[] = {
61 { 0, 0x00 }, { 1, 0x80 }, { 2, 0x00 }, { 3, 0x80 },
62 { 4, 0x02 }, { 5, 0x00 }, { 6, 0x11 }, { 7, 0x05 },
63 { 8, 0x00 }, { 9, 0x00 }, { 10, 0x36 }, { 11, 0x10 },
64 { 12, 0x00 }, { 13, 0x00 }, { 14, 0x57 }, { 15, 0x00 },
65 { 16, 0x88 }, { 17, 0x88 }, { 18, 0x08 }, { 19, 0x08 }
66};
67
68static const int deemph_settings[] = {44100, 0, 48000, 32000};
69
70static int ak4641_set_deemph(struct snd_soc_component *component)
71{
72 struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component);
73 int i, best = 0;
74
75 for (i = 0 ; i < ARRAY_SIZE(deemph_settings); i++) {
76 /* if deemphasis is on, select the nearest available rate */
77 if (ak4641->deemph && deemph_settings[i] != 0 &&
78 abs(deemph_settings[i] - ak4641->playback_fs) <
79 abs(deemph_settings[best] - ak4641->playback_fs))
80 best = i;
81
82 if (!ak4641->deemph && deemph_settings[i] == 0)
83 best = i;
84 }
85
86 dev_dbg(component->dev, "Set deemphasis %d\n", best);
87
88 return snd_soc_component_update_bits(component, AK4641_DAC, 0x3, best);
89}
90
91static int ak4641_put_deemph(struct snd_kcontrol *kcontrol,
92 struct snd_ctl_elem_value *ucontrol)
93{
94 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
95 struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component);
96 int deemph = ucontrol->value.integer.value[0];
97
98 if (deemph > 1)
99 return -EINVAL;
100
101 ak4641->deemph = deemph;
102
103 return ak4641_set_deemph(component);
104}
105
106static int ak4641_get_deemph(struct snd_kcontrol *kcontrol,
107 struct snd_ctl_elem_value *ucontrol)
108{
109 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
110 struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component);
111
112 ucontrol->value.integer.value[0] = ak4641->deemph;
113 return 0;
114};
115
116static const char *ak4641_mono_out[] = {"(L + R)/2", "Hi-Z"};
117static const char *ak4641_hp_out[] = {"Stereo", "Mono"};
118static const char *ak4641_mic_select[] = {"Internal", "External"};
119static const char *ak4641_mic_or_dac[] = {"Microphone", "Voice DAC"};
120
121
122static const DECLARE_TLV_DB_SCALE(mono_gain_tlv, -1700, 2300, 0);
123static const DECLARE_TLV_DB_SCALE(mic_boost_tlv, 0, 2000, 0);
124static const DECLARE_TLV_DB_SCALE(eq_tlv, -1050, 150, 0);
125static const DECLARE_TLV_DB_SCALE(master_tlv, -12750, 50, 0);
126static const DECLARE_TLV_DB_SCALE(mic_stereo_sidetone_tlv, -2700, 300, 0);
127static const DECLARE_TLV_DB_SCALE(mic_mono_sidetone_tlv, -400, 400, 0);
128static const DECLARE_TLV_DB_SCALE(capture_tlv, -800, 50, 0);
129static const DECLARE_TLV_DB_SCALE(alc_tlv, -800, 50, 0);
130static const DECLARE_TLV_DB_SCALE(aux_in_tlv, -2100, 300, 0);
131
132
133static SOC_ENUM_SINGLE_DECL(ak4641_mono_out_enum,
134 AK4641_SIG1, 6, ak4641_mono_out);
135static SOC_ENUM_SINGLE_DECL(ak4641_hp_out_enum,
136 AK4641_MODE2, 2, ak4641_hp_out);
137static SOC_ENUM_SINGLE_DECL(ak4641_mic_select_enum,
138 AK4641_MIC, 1, ak4641_mic_select);
139static SOC_ENUM_SINGLE_DECL(ak4641_mic_or_dac_enum,
140 AK4641_BTIF, 4, ak4641_mic_or_dac);
141
142static const struct snd_kcontrol_new ak4641_snd_controls[] = {
143 SOC_ENUM("Mono 1 Output", ak4641_mono_out_enum),
144 SOC_SINGLE_TLV("Mono 1 Gain Volume", AK4641_SIG1, 7, 1, 1,
145 mono_gain_tlv),
146 SOC_ENUM("Headphone Output", ak4641_hp_out_enum),
147 SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
148 ak4641_get_deemph, ak4641_put_deemph),
149
150 SOC_SINGLE_TLV("Mic Boost Volume", AK4641_MIC, 0, 1, 0, mic_boost_tlv),
151
152 SOC_SINGLE("ALC Operation Time", AK4641_TIMER, 0, 3, 0),
153 SOC_SINGLE("ALC Recovery Time", AK4641_TIMER, 2, 3, 0),
154 SOC_SINGLE("ALC ZC Time", AK4641_TIMER, 4, 3, 0),
155
156 SOC_SINGLE("ALC 1 Switch", AK4641_ALC1, 5, 1, 0),
157
158 SOC_SINGLE_TLV("ALC Volume", AK4641_ALC2, 0, 71, 0, alc_tlv),
159 SOC_SINGLE("Left Out Enable Switch", AK4641_SIG2, 1, 1, 0),
160 SOC_SINGLE("Right Out Enable Switch", AK4641_SIG2, 0, 1, 0),
161
162 SOC_SINGLE_TLV("Capture Volume", AK4641_PGA, 0, 71, 0, capture_tlv),
163
164 SOC_DOUBLE_R_TLV("Master Playback Volume", AK4641_LATT,
165 AK4641_RATT, 0, 255, 1, master_tlv),
166
167 SOC_SINGLE_TLV("AUX In Volume", AK4641_VOL, 0, 15, 0, aux_in_tlv),
168
169 SOC_SINGLE("Equalizer Switch", AK4641_DAC, 2, 1, 0),
170 SOC_SINGLE_TLV("EQ1 100 Hz Volume", AK4641_EQLO, 0, 15, 1, eq_tlv),
171 SOC_SINGLE_TLV("EQ2 250 Hz Volume", AK4641_EQLO, 4, 15, 1, eq_tlv),
172 SOC_SINGLE_TLV("EQ3 1 kHz Volume", AK4641_EQMID, 0, 15, 1, eq_tlv),
173 SOC_SINGLE_TLV("EQ4 3.5 kHz Volume", AK4641_EQMID, 4, 15, 1, eq_tlv),
174 SOC_SINGLE_TLV("EQ5 10 kHz Volume", AK4641_EQHI, 0, 15, 1, eq_tlv),
175};
176
177/* Mono 1 Mixer */
178static const struct snd_kcontrol_new ak4641_mono1_mixer_controls[] = {
179 SOC_DAPM_SINGLE_TLV("Mic Mono Sidetone Volume", AK4641_VOL, 7, 1, 0,
180 mic_mono_sidetone_tlv),
181 SOC_DAPM_SINGLE("Mic Mono Sidetone Switch", AK4641_SIG1, 4, 1, 0),
182 SOC_DAPM_SINGLE("Mono Playback Switch", AK4641_SIG1, 5, 1, 0),
183};
184
185/* Stereo Mixer */
186static const struct snd_kcontrol_new ak4641_stereo_mixer_controls[] = {
187 SOC_DAPM_SINGLE_TLV("Mic Sidetone Volume", AK4641_VOL, 4, 7, 0,
188 mic_stereo_sidetone_tlv),
189 SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4641_SIG2, 4, 1, 0),
190 SOC_DAPM_SINGLE("Playback Switch", AK4641_SIG2, 7, 1, 0),
191 SOC_DAPM_SINGLE("Aux Bypass Switch", AK4641_SIG2, 5, 1, 0),
192};
193
194/* Input Mixer */
195static const struct snd_kcontrol_new ak4641_input_mixer_controls[] = {
196 SOC_DAPM_SINGLE("Mic Capture Switch", AK4641_MIC, 2, 1, 0),
197 SOC_DAPM_SINGLE("Aux Capture Switch", AK4641_MIC, 5, 1, 0),
198};
199
200/* Mic mux */
201static const struct snd_kcontrol_new ak4641_mic_mux_control =
202 SOC_DAPM_ENUM("Mic Select", ak4641_mic_select_enum);
203
204/* Input mux */
205static const struct snd_kcontrol_new ak4641_input_mux_control =
206 SOC_DAPM_ENUM("Input Select", ak4641_mic_or_dac_enum);
207
208/* mono 2 switch */
209static const struct snd_kcontrol_new ak4641_mono2_control =
210 SOC_DAPM_SINGLE("Switch", AK4641_SIG1, 0, 1, 0);
211
212/* ak4641 dapm widgets */
213static const struct snd_soc_dapm_widget ak4641_dapm_widgets[] = {
214 SND_SOC_DAPM_MIXER("Stereo Mixer", SND_SOC_NOPM, 0, 0,
215 &ak4641_stereo_mixer_controls[0],
216 ARRAY_SIZE(ak4641_stereo_mixer_controls)),
217 SND_SOC_DAPM_MIXER("Mono1 Mixer", SND_SOC_NOPM, 0, 0,
218 &ak4641_mono1_mixer_controls[0],
219 ARRAY_SIZE(ak4641_mono1_mixer_controls)),
220 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0,
221 &ak4641_input_mixer_controls[0],
222 ARRAY_SIZE(ak4641_input_mixer_controls)),
223 SND_SOC_DAPM_MUX("Mic Mux", SND_SOC_NOPM, 0, 0,
224 &ak4641_mic_mux_control),
225 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0,
226 &ak4641_input_mux_control),
227 SND_SOC_DAPM_SWITCH("Mono 2 Enable", SND_SOC_NOPM, 0, 0,
228 &ak4641_mono2_control),
229
230 SND_SOC_DAPM_OUTPUT("LOUT"),
231 SND_SOC_DAPM_OUTPUT("ROUT"),
232 SND_SOC_DAPM_OUTPUT("MOUT1"),
233 SND_SOC_DAPM_OUTPUT("MOUT2"),
234 SND_SOC_DAPM_OUTPUT("MICOUT"),
235
236 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", AK4641_PM1, 0, 0),
237 SND_SOC_DAPM_PGA("Mic", AK4641_PM1, 1, 0, NULL, 0),
238 SND_SOC_DAPM_PGA("AUX In", AK4641_PM1, 2, 0, NULL, 0),
239 SND_SOC_DAPM_PGA("Mono Out", AK4641_PM1, 3, 0, NULL, 0),
240 SND_SOC_DAPM_PGA("Line Out", AK4641_PM1, 4, 0, NULL, 0),
241
242 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", AK4641_PM2, 0, 0),
243 SND_SOC_DAPM_PGA("Mono Out 2", AK4641_PM2, 3, 0, NULL, 0),
244
245 SND_SOC_DAPM_ADC("Voice ADC", "Voice Capture", AK4641_BTIF, 0, 0),
246 SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", AK4641_BTIF, 1, 0),
247
248 SND_SOC_DAPM_MICBIAS("Mic Int Bias", AK4641_MIC, 3, 0),
249 SND_SOC_DAPM_MICBIAS("Mic Ext Bias", AK4641_MIC, 4, 0),
250
251 SND_SOC_DAPM_INPUT("MICIN"),
252 SND_SOC_DAPM_INPUT("MICEXT"),
253 SND_SOC_DAPM_INPUT("AUX"),
254 SND_SOC_DAPM_INPUT("AIN"),
255};
256
257static const struct snd_soc_dapm_route ak4641_audio_map[] = {
258 /* Stereo Mixer */
259 {"Stereo Mixer", "Playback Switch", "DAC"},
260 {"Stereo Mixer", "Mic Sidetone Switch", "Input Mux"},
261 {"Stereo Mixer", "Aux Bypass Switch", "AUX In"},
262
263 /* Mono 1 Mixer */
264 {"Mono1 Mixer", "Mic Mono Sidetone Switch", "Input Mux"},
265 {"Mono1 Mixer", "Mono Playback Switch", "DAC"},
266
267 /* Mic */
268 {"Mic", NULL, "AIN"},
269 {"Mic Mux", "Internal", "Mic Int Bias"},
270 {"Mic Mux", "External", "Mic Ext Bias"},
271 {"Mic Int Bias", NULL, "MICIN"},
272 {"Mic Ext Bias", NULL, "MICEXT"},
273 {"MICOUT", NULL, "Mic Mux"},
274
275 /* Input Mux */
276 {"Input Mux", "Microphone", "Mic"},
277 {"Input Mux", "Voice DAC", "Voice DAC"},
278
279 /* Line Out */
280 {"LOUT", NULL, "Line Out"},
281 {"ROUT", NULL, "Line Out"},
282 {"Line Out", NULL, "Stereo Mixer"},
283
284 /* Mono 1 Out */
285 {"MOUT1", NULL, "Mono Out"},
286 {"Mono Out", NULL, "Mono1 Mixer"},
287
288 /* Mono 2 Out */
289 {"MOUT2", NULL, "Mono 2 Enable"},
290 {"Mono 2 Enable", "Switch", "Mono Out 2"},
291 {"Mono Out 2", NULL, "Stereo Mixer"},
292
293 {"Voice ADC", NULL, "Mono 2 Enable"},
294
295 /* Aux In */
296 {"AUX In", NULL, "AUX"},
297
298 /* ADC */
299 {"ADC", NULL, "Input Mixer"},
300 {"Input Mixer", "Mic Capture Switch", "Mic"},
301 {"Input Mixer", "Aux Capture Switch", "AUX In"},
302};
303
304static int ak4641_set_dai_sysclk(struct snd_soc_dai *codec_dai,
305 int clk_id, unsigned int freq, int dir)
306{
307 struct snd_soc_component *component = codec_dai->component;
308 struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component);
309
310 ak4641->sysclk = freq;
311 return 0;
312}
313
314static int ak4641_i2s_hw_params(struct snd_pcm_substream *substream,
315 struct snd_pcm_hw_params *params,
316 struct snd_soc_dai *dai)
317{
318 struct snd_soc_component *component = dai->component;
319 struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component);
320 int rate = params_rate(params), fs = 256;
321 u8 mode2;
322
323 if (rate)
324 fs = ak4641->sysclk / rate;
325 else
326 return -EINVAL;
327
328 /* set fs */
329 switch (fs) {
330 case 1024:
331 mode2 = (0x2 << 5);
332 break;
333 case 512:
334 mode2 = (0x1 << 5);
335 break;
336 case 256:
337 mode2 = (0x0 << 5);
338 break;
339 default:
340 dev_err(component->dev, "Error: unsupported fs=%d\n", fs);
341 return -EINVAL;
342 }
343
344 snd_soc_component_update_bits(component, AK4641_MODE2, (0x3 << 5), mode2);
345
346 /* Update de-emphasis filter for the new rate */
347 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
348 ak4641->playback_fs = rate;
349 ak4641_set_deemph(component);
350 }
351
352 return 0;
353}
354
355static int ak4641_pcm_set_dai_fmt(struct snd_soc_dai *codec_dai,
356 unsigned int fmt)
357{
358 struct snd_soc_component *component = codec_dai->component;
359 u8 btif;
360 int ret;
361
362 /* interface format */
363 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
364 case SND_SOC_DAIFMT_I2S:
365 btif = (0x3 << 5);
366 break;
367 case SND_SOC_DAIFMT_LEFT_J:
368 btif = (0x2 << 5);
369 break;
370 case SND_SOC_DAIFMT_DSP_A: /* MSB after FRM */
371 btif = (0x0 << 5);
372 break;
373 case SND_SOC_DAIFMT_DSP_B: /* MSB during FRM */
374 btif = (0x1 << 5);
375 break;
376 default:
377 return -EINVAL;
378 }
379
380 ret = snd_soc_component_update_bits(component, AK4641_BTIF, (0x3 << 5), btif);
381 if (ret < 0)
382 return ret;
383
384 return 0;
385}
386
387static int ak4641_i2s_set_dai_fmt(struct snd_soc_dai *codec_dai,
388 unsigned int fmt)
389{
390 struct snd_soc_component *component = codec_dai->component;
391 u8 mode1 = 0;
392
393 /* interface format */
394 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
395 case SND_SOC_DAIFMT_I2S:
396 mode1 = 0x02;
397 break;
398 case SND_SOC_DAIFMT_LEFT_J:
399 mode1 = 0x01;
400 break;
401 default:
402 return -EINVAL;
403 }
404
405 return snd_soc_component_write(component, AK4641_MODE1, mode1);
406}
407
408static int ak4641_mute(struct snd_soc_dai *dai, int mute, int direction)
409{
410 struct snd_soc_component *component = dai->component;
411
412 return snd_soc_component_update_bits(component, AK4641_DAC, 0x20, mute ? 0x20 : 0);
413}
414
415static int ak4641_set_bias_level(struct snd_soc_component *component,
416 enum snd_soc_bias_level level)
417{
418 struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component);
419 struct ak4641_platform_data *pdata = component->dev->platform_data;
420 int ret;
421
422 switch (level) {
423 case SND_SOC_BIAS_ON:
424 /* unmute */
425 snd_soc_component_update_bits(component, AK4641_DAC, 0x20, 0);
426 break;
427 case SND_SOC_BIAS_PREPARE:
428 /* mute */
429 snd_soc_component_update_bits(component, AK4641_DAC, 0x20, 0x20);
430 break;
431 case SND_SOC_BIAS_STANDBY:
432 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
433 if (pdata && gpio_is_valid(pdata->gpio_power))
434 gpio_set_value(pdata->gpio_power, 1);
435 mdelay(1);
436 if (pdata && gpio_is_valid(pdata->gpio_npdn))
437 gpio_set_value(pdata->gpio_npdn, 1);
438 mdelay(1);
439
440 ret = regcache_sync(ak4641->regmap);
441 if (ret) {
442 dev_err(component->dev,
443 "Failed to sync cache: %d\n", ret);
444 return ret;
445 }
446 }
447 snd_soc_component_update_bits(component, AK4641_PM1, 0x80, 0x80);
448 snd_soc_component_update_bits(component, AK4641_PM2, 0x80, 0);
449 break;
450 case SND_SOC_BIAS_OFF:
451 snd_soc_component_update_bits(component, AK4641_PM1, 0x80, 0);
452 if (pdata && gpio_is_valid(pdata->gpio_npdn))
453 gpio_set_value(pdata->gpio_npdn, 0);
454 if (pdata && gpio_is_valid(pdata->gpio_power))
455 gpio_set_value(pdata->gpio_power, 0);
456 regcache_mark_dirty(ak4641->regmap);
457 break;
458 }
459 return 0;
460}
461
462#define AK4641_RATES (SNDRV_PCM_RATE_8000_48000)
463#define AK4641_RATES_BT (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
464 SNDRV_PCM_RATE_16000)
465#define AK4641_FORMATS (SNDRV_PCM_FMTBIT_S16_LE)
466
467static const struct snd_soc_dai_ops ak4641_i2s_dai_ops = {
468 .hw_params = ak4641_i2s_hw_params,
469 .set_fmt = ak4641_i2s_set_dai_fmt,
470 .mute_stream = ak4641_mute,
471 .set_sysclk = ak4641_set_dai_sysclk,
472 .no_capture_mute = 1,
473};
474
475static const struct snd_soc_dai_ops ak4641_pcm_dai_ops = {
476 .hw_params = NULL, /* rates are controlled by BT chip */
477 .set_fmt = ak4641_pcm_set_dai_fmt,
478 .mute_stream = ak4641_mute,
479 .set_sysclk = ak4641_set_dai_sysclk,
480 .no_capture_mute = 1,
481};
482
483static struct snd_soc_dai_driver ak4641_dai[] = {
484{
485 .name = "ak4641-hifi",
486 .id = 1,
487 .playback = {
488 .stream_name = "HiFi Playback",
489 .channels_min = 1,
490 .channels_max = 2,
491 .rates = AK4641_RATES,
492 .formats = AK4641_FORMATS,
493 },
494 .capture = {
495 .stream_name = "HiFi Capture",
496 .channels_min = 1,
497 .channels_max = 2,
498 .rates = AK4641_RATES,
499 .formats = AK4641_FORMATS,
500 },
501 .ops = &ak4641_i2s_dai_ops,
502 .symmetric_rate = 1,
503},
504{
505 .name = "ak4641-voice",
506 .id = 1,
507 .playback = {
508 .stream_name = "Voice Playback",
509 .channels_min = 1,
510 .channels_max = 1,
511 .rates = AK4641_RATES_BT,
512 .formats = AK4641_FORMATS,
513 },
514 .capture = {
515 .stream_name = "Voice Capture",
516 .channels_min = 1,
517 .channels_max = 1,
518 .rates = AK4641_RATES_BT,
519 .formats = AK4641_FORMATS,
520 },
521 .ops = &ak4641_pcm_dai_ops,
522 .symmetric_rate = 1,
523},
524};
525
526static const struct snd_soc_component_driver soc_component_dev_ak4641 = {
527 .controls = ak4641_snd_controls,
528 .num_controls = ARRAY_SIZE(ak4641_snd_controls),
529 .dapm_widgets = ak4641_dapm_widgets,
530 .num_dapm_widgets = ARRAY_SIZE(ak4641_dapm_widgets),
531 .dapm_routes = ak4641_audio_map,
532 .num_dapm_routes = ARRAY_SIZE(ak4641_audio_map),
533 .set_bias_level = ak4641_set_bias_level,
534 .suspend_bias_off = 1,
535 .idle_bias_on = 1,
536 .use_pmdown_time = 1,
537 .endianness = 1,
538};
539
540static const struct regmap_config ak4641_regmap = {
541 .reg_bits = 8,
542 .val_bits = 8,
543
544 .max_register = AK4641_BTIF,
545 .reg_defaults = ak4641_reg_defaults,
546 .num_reg_defaults = ARRAY_SIZE(ak4641_reg_defaults),
547 .cache_type = REGCACHE_RBTREE,
548};
549
550static int ak4641_i2c_probe(struct i2c_client *i2c)
551{
552 struct ak4641_platform_data *pdata = i2c->dev.platform_data;
553 struct ak4641_priv *ak4641;
554 int ret;
555
556 ak4641 = devm_kzalloc(&i2c->dev, sizeof(struct ak4641_priv),
557 GFP_KERNEL);
558 if (!ak4641)
559 return -ENOMEM;
560
561 ak4641->regmap = devm_regmap_init_i2c(i2c, &ak4641_regmap);
562 if (IS_ERR(ak4641->regmap))
563 return PTR_ERR(ak4641->regmap);
564
565 if (pdata) {
566 if (gpio_is_valid(pdata->gpio_power)) {
567 ret = gpio_request_one(pdata->gpio_power,
568 GPIOF_OUT_INIT_LOW, "ak4641 power");
569 if (ret)
570 goto err_out;
571 }
572 if (gpio_is_valid(pdata->gpio_npdn)) {
573 ret = gpio_request_one(pdata->gpio_npdn,
574 GPIOF_OUT_INIT_LOW, "ak4641 npdn");
575 if (ret)
576 goto err_gpio;
577
578 udelay(1); /* > 150 ns */
579 gpio_set_value(pdata->gpio_npdn, 1);
580 }
581 }
582
583 i2c_set_clientdata(i2c, ak4641);
584
585 ret = devm_snd_soc_register_component(&i2c->dev,
586 &soc_component_dev_ak4641,
587 ak4641_dai, ARRAY_SIZE(ak4641_dai));
588 if (ret != 0)
589 goto err_gpio2;
590
591 return 0;
592
593err_gpio2:
594 if (pdata) {
595 if (gpio_is_valid(pdata->gpio_power))
596 gpio_set_value(pdata->gpio_power, 0);
597 if (gpio_is_valid(pdata->gpio_npdn))
598 gpio_free(pdata->gpio_npdn);
599 }
600err_gpio:
601 if (pdata && gpio_is_valid(pdata->gpio_power))
602 gpio_free(pdata->gpio_power);
603err_out:
604 return ret;
605}
606
607static void ak4641_i2c_remove(struct i2c_client *i2c)
608{
609 struct ak4641_platform_data *pdata = i2c->dev.platform_data;
610
611 if (pdata) {
612 if (gpio_is_valid(pdata->gpio_power)) {
613 gpio_set_value(pdata->gpio_power, 0);
614 gpio_free(pdata->gpio_power);
615 }
616 if (gpio_is_valid(pdata->gpio_npdn))
617 gpio_free(pdata->gpio_npdn);
618 }
619}
620
621static const struct i2c_device_id ak4641_i2c_id[] = {
622 { "ak4641" },
623 { }
624};
625MODULE_DEVICE_TABLE(i2c, ak4641_i2c_id);
626
627static struct i2c_driver ak4641_i2c_driver = {
628 .driver = {
629 .name = "ak4641",
630 },
631 .probe = ak4641_i2c_probe,
632 .remove = ak4641_i2c_remove,
633 .id_table = ak4641_i2c_id,
634};
635
636module_i2c_driver(ak4641_i2c_driver);
637
638MODULE_DESCRIPTION("SoC AK4641 driver");
639MODULE_AUTHOR("Harald Welte <laforge@gnufiish.org>");
640MODULE_LICENSE("GPL");
1/*
2 * ak4641.c -- AK4641 ALSA Soc Audio driver
3 *
4 * Copyright (C) 2008 Harald Welte <laforge@gnufiish.org>
5 * Copyright (C) 2011 Dmitry Artamonow <mad_soft@inbox.ru>
6 *
7 * Based on ak4535.c by Richard Purdie
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/delay.h>
17#include <linux/gpio.h>
18#include <linux/pm.h>
19#include <linux/i2c.h>
20#include <linux/regmap.h>
21#include <linux/slab.h>
22#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/soc.h>
26#include <sound/initval.h>
27#include <sound/tlv.h>
28#include <sound/ak4641.h>
29
30#include "ak4641.h"
31
32/* codec private data */
33struct ak4641_priv {
34 struct regmap *regmap;
35 unsigned int sysclk;
36 int deemph;
37 int playback_fs;
38};
39
40/*
41 * ak4641 register cache
42 */
43static const struct reg_default ak4641_reg_defaults[] = {
44 { 0, 0x00 }, { 1, 0x80 }, { 2, 0x00 }, { 3, 0x80 },
45 { 4, 0x02 }, { 5, 0x00 }, { 6, 0x11 }, { 7, 0x05 },
46 { 8, 0x00 }, { 9, 0x00 }, { 10, 0x36 }, { 11, 0x10 },
47 { 12, 0x00 }, { 13, 0x00 }, { 14, 0x57 }, { 15, 0x00 },
48 { 16, 0x88 }, { 17, 0x88 }, { 18, 0x08 }, { 19, 0x08 }
49};
50
51static const int deemph_settings[] = {44100, 0, 48000, 32000};
52
53static int ak4641_set_deemph(struct snd_soc_codec *codec)
54{
55 struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
56 int i, best = 0;
57
58 for (i = 0 ; i < ARRAY_SIZE(deemph_settings); i++) {
59 /* if deemphasis is on, select the nearest available rate */
60 if (ak4641->deemph && deemph_settings[i] != 0 &&
61 abs(deemph_settings[i] - ak4641->playback_fs) <
62 abs(deemph_settings[best] - ak4641->playback_fs))
63 best = i;
64
65 if (!ak4641->deemph && deemph_settings[i] == 0)
66 best = i;
67 }
68
69 dev_dbg(codec->dev, "Set deemphasis %d\n", best);
70
71 return snd_soc_update_bits(codec, AK4641_DAC, 0x3, best);
72}
73
74static int ak4641_put_deemph(struct snd_kcontrol *kcontrol,
75 struct snd_ctl_elem_value *ucontrol)
76{
77 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
78 struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
79 int deemph = ucontrol->value.integer.value[0];
80
81 if (deemph > 1)
82 return -EINVAL;
83
84 ak4641->deemph = deemph;
85
86 return ak4641_set_deemph(codec);
87}
88
89static int ak4641_get_deemph(struct snd_kcontrol *kcontrol,
90 struct snd_ctl_elem_value *ucontrol)
91{
92 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
93 struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
94
95 ucontrol->value.integer.value[0] = ak4641->deemph;
96 return 0;
97};
98
99static const char *ak4641_mono_out[] = {"(L + R)/2", "Hi-Z"};
100static const char *ak4641_hp_out[] = {"Stereo", "Mono"};
101static const char *ak4641_mic_select[] = {"Internal", "External"};
102static const char *ak4641_mic_or_dac[] = {"Microphone", "Voice DAC"};
103
104
105static const DECLARE_TLV_DB_SCALE(mono_gain_tlv, -1700, 2300, 0);
106static const DECLARE_TLV_DB_SCALE(mic_boost_tlv, 0, 2000, 0);
107static const DECLARE_TLV_DB_SCALE(eq_tlv, -1050, 150, 0);
108static const DECLARE_TLV_DB_SCALE(master_tlv, -12750, 50, 0);
109static const DECLARE_TLV_DB_SCALE(mic_stereo_sidetone_tlv, -2700, 300, 0);
110static const DECLARE_TLV_DB_SCALE(mic_mono_sidetone_tlv, -400, 400, 0);
111static const DECLARE_TLV_DB_SCALE(capture_tlv, -800, 50, 0);
112static const DECLARE_TLV_DB_SCALE(alc_tlv, -800, 50, 0);
113static const DECLARE_TLV_DB_SCALE(aux_in_tlv, -2100, 300, 0);
114
115
116static SOC_ENUM_SINGLE_DECL(ak4641_mono_out_enum,
117 AK4641_SIG1, 6, ak4641_mono_out);
118static SOC_ENUM_SINGLE_DECL(ak4641_hp_out_enum,
119 AK4641_MODE2, 2, ak4641_hp_out);
120static SOC_ENUM_SINGLE_DECL(ak4641_mic_select_enum,
121 AK4641_MIC, 1, ak4641_mic_select);
122static SOC_ENUM_SINGLE_DECL(ak4641_mic_or_dac_enum,
123 AK4641_BTIF, 4, ak4641_mic_or_dac);
124
125static const struct snd_kcontrol_new ak4641_snd_controls[] = {
126 SOC_ENUM("Mono 1 Output", ak4641_mono_out_enum),
127 SOC_SINGLE_TLV("Mono 1 Gain Volume", AK4641_SIG1, 7, 1, 1,
128 mono_gain_tlv),
129 SOC_ENUM("Headphone Output", ak4641_hp_out_enum),
130 SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
131 ak4641_get_deemph, ak4641_put_deemph),
132
133 SOC_SINGLE_TLV("Mic Boost Volume", AK4641_MIC, 0, 1, 0, mic_boost_tlv),
134
135 SOC_SINGLE("ALC Operation Time", AK4641_TIMER, 0, 3, 0),
136 SOC_SINGLE("ALC Recovery Time", AK4641_TIMER, 2, 3, 0),
137 SOC_SINGLE("ALC ZC Time", AK4641_TIMER, 4, 3, 0),
138
139 SOC_SINGLE("ALC 1 Switch", AK4641_ALC1, 5, 1, 0),
140
141 SOC_SINGLE_TLV("ALC Volume", AK4641_ALC2, 0, 71, 0, alc_tlv),
142 SOC_SINGLE("Left Out Enable Switch", AK4641_SIG2, 1, 1, 0),
143 SOC_SINGLE("Right Out Enable Switch", AK4641_SIG2, 0, 1, 0),
144
145 SOC_SINGLE_TLV("Capture Volume", AK4641_PGA, 0, 71, 0, capture_tlv),
146
147 SOC_DOUBLE_R_TLV("Master Playback Volume", AK4641_LATT,
148 AK4641_RATT, 0, 255, 1, master_tlv),
149
150 SOC_SINGLE_TLV("AUX In Volume", AK4641_VOL, 0, 15, 0, aux_in_tlv),
151
152 SOC_SINGLE("Equalizer Switch", AK4641_DAC, 2, 1, 0),
153 SOC_SINGLE_TLV("EQ1 100 Hz Volume", AK4641_EQLO, 0, 15, 1, eq_tlv),
154 SOC_SINGLE_TLV("EQ2 250 Hz Volume", AK4641_EQLO, 4, 15, 1, eq_tlv),
155 SOC_SINGLE_TLV("EQ3 1 kHz Volume", AK4641_EQMID, 0, 15, 1, eq_tlv),
156 SOC_SINGLE_TLV("EQ4 3.5 kHz Volume", AK4641_EQMID, 4, 15, 1, eq_tlv),
157 SOC_SINGLE_TLV("EQ5 10 kHz Volume", AK4641_EQHI, 0, 15, 1, eq_tlv),
158};
159
160/* Mono 1 Mixer */
161static const struct snd_kcontrol_new ak4641_mono1_mixer_controls[] = {
162 SOC_DAPM_SINGLE_TLV("Mic Mono Sidetone Volume", AK4641_VOL, 7, 1, 0,
163 mic_mono_sidetone_tlv),
164 SOC_DAPM_SINGLE("Mic Mono Sidetone Switch", AK4641_SIG1, 4, 1, 0),
165 SOC_DAPM_SINGLE("Mono Playback Switch", AK4641_SIG1, 5, 1, 0),
166};
167
168/* Stereo Mixer */
169static const struct snd_kcontrol_new ak4641_stereo_mixer_controls[] = {
170 SOC_DAPM_SINGLE_TLV("Mic Sidetone Volume", AK4641_VOL, 4, 7, 0,
171 mic_stereo_sidetone_tlv),
172 SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4641_SIG2, 4, 1, 0),
173 SOC_DAPM_SINGLE("Playback Switch", AK4641_SIG2, 7, 1, 0),
174 SOC_DAPM_SINGLE("Aux Bypass Switch", AK4641_SIG2, 5, 1, 0),
175};
176
177/* Input Mixer */
178static const struct snd_kcontrol_new ak4641_input_mixer_controls[] = {
179 SOC_DAPM_SINGLE("Mic Capture Switch", AK4641_MIC, 2, 1, 0),
180 SOC_DAPM_SINGLE("Aux Capture Switch", AK4641_MIC, 5, 1, 0),
181};
182
183/* Mic mux */
184static const struct snd_kcontrol_new ak4641_mic_mux_control =
185 SOC_DAPM_ENUM("Mic Select", ak4641_mic_select_enum);
186
187/* Input mux */
188static const struct snd_kcontrol_new ak4641_input_mux_control =
189 SOC_DAPM_ENUM("Input Select", ak4641_mic_or_dac_enum);
190
191/* mono 2 switch */
192static const struct snd_kcontrol_new ak4641_mono2_control =
193 SOC_DAPM_SINGLE("Switch", AK4641_SIG1, 0, 1, 0);
194
195/* ak4641 dapm widgets */
196static const struct snd_soc_dapm_widget ak4641_dapm_widgets[] = {
197 SND_SOC_DAPM_MIXER("Stereo Mixer", SND_SOC_NOPM, 0, 0,
198 &ak4641_stereo_mixer_controls[0],
199 ARRAY_SIZE(ak4641_stereo_mixer_controls)),
200 SND_SOC_DAPM_MIXER("Mono1 Mixer", SND_SOC_NOPM, 0, 0,
201 &ak4641_mono1_mixer_controls[0],
202 ARRAY_SIZE(ak4641_mono1_mixer_controls)),
203 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0,
204 &ak4641_input_mixer_controls[0],
205 ARRAY_SIZE(ak4641_input_mixer_controls)),
206 SND_SOC_DAPM_MUX("Mic Mux", SND_SOC_NOPM, 0, 0,
207 &ak4641_mic_mux_control),
208 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0,
209 &ak4641_input_mux_control),
210 SND_SOC_DAPM_SWITCH("Mono 2 Enable", SND_SOC_NOPM, 0, 0,
211 &ak4641_mono2_control),
212
213 SND_SOC_DAPM_OUTPUT("LOUT"),
214 SND_SOC_DAPM_OUTPUT("ROUT"),
215 SND_SOC_DAPM_OUTPUT("MOUT1"),
216 SND_SOC_DAPM_OUTPUT("MOUT2"),
217 SND_SOC_DAPM_OUTPUT("MICOUT"),
218
219 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", AK4641_PM1, 0, 0),
220 SND_SOC_DAPM_PGA("Mic", AK4641_PM1, 1, 0, NULL, 0),
221 SND_SOC_DAPM_PGA("AUX In", AK4641_PM1, 2, 0, NULL, 0),
222 SND_SOC_DAPM_PGA("Mono Out", AK4641_PM1, 3, 0, NULL, 0),
223 SND_SOC_DAPM_PGA("Line Out", AK4641_PM1, 4, 0, NULL, 0),
224
225 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", AK4641_PM2, 0, 0),
226 SND_SOC_DAPM_PGA("Mono Out 2", AK4641_PM2, 3, 0, NULL, 0),
227
228 SND_SOC_DAPM_ADC("Voice ADC", "Voice Capture", AK4641_BTIF, 0, 0),
229 SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", AK4641_BTIF, 1, 0),
230
231 SND_SOC_DAPM_MICBIAS("Mic Int Bias", AK4641_MIC, 3, 0),
232 SND_SOC_DAPM_MICBIAS("Mic Ext Bias", AK4641_MIC, 4, 0),
233
234 SND_SOC_DAPM_INPUT("MICIN"),
235 SND_SOC_DAPM_INPUT("MICEXT"),
236 SND_SOC_DAPM_INPUT("AUX"),
237 SND_SOC_DAPM_INPUT("AIN"),
238};
239
240static const struct snd_soc_dapm_route ak4641_audio_map[] = {
241 /* Stereo Mixer */
242 {"Stereo Mixer", "Playback Switch", "DAC"},
243 {"Stereo Mixer", "Mic Sidetone Switch", "Input Mux"},
244 {"Stereo Mixer", "Aux Bypass Switch", "AUX In"},
245
246 /* Mono 1 Mixer */
247 {"Mono1 Mixer", "Mic Mono Sidetone Switch", "Input Mux"},
248 {"Mono1 Mixer", "Mono Playback Switch", "DAC"},
249
250 /* Mic */
251 {"Mic", NULL, "AIN"},
252 {"Mic Mux", "Internal", "Mic Int Bias"},
253 {"Mic Mux", "External", "Mic Ext Bias"},
254 {"Mic Int Bias", NULL, "MICIN"},
255 {"Mic Ext Bias", NULL, "MICEXT"},
256 {"MICOUT", NULL, "Mic Mux"},
257
258 /* Input Mux */
259 {"Input Mux", "Microphone", "Mic"},
260 {"Input Mux", "Voice DAC", "Voice DAC"},
261
262 /* Line Out */
263 {"LOUT", NULL, "Line Out"},
264 {"ROUT", NULL, "Line Out"},
265 {"Line Out", NULL, "Stereo Mixer"},
266
267 /* Mono 1 Out */
268 {"MOUT1", NULL, "Mono Out"},
269 {"Mono Out", NULL, "Mono1 Mixer"},
270
271 /* Mono 2 Out */
272 {"MOUT2", NULL, "Mono 2 Enable"},
273 {"Mono 2 Enable", "Switch", "Mono Out 2"},
274 {"Mono Out 2", NULL, "Stereo Mixer"},
275
276 {"Voice ADC", NULL, "Mono 2 Enable"},
277
278 /* Aux In */
279 {"AUX In", NULL, "AUX"},
280
281 /* ADC */
282 {"ADC", NULL, "Input Mixer"},
283 {"Input Mixer", "Mic Capture Switch", "Mic"},
284 {"Input Mixer", "Aux Capture Switch", "AUX In"},
285};
286
287static int ak4641_set_dai_sysclk(struct snd_soc_dai *codec_dai,
288 int clk_id, unsigned int freq, int dir)
289{
290 struct snd_soc_codec *codec = codec_dai->codec;
291 struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
292
293 ak4641->sysclk = freq;
294 return 0;
295}
296
297static int ak4641_i2s_hw_params(struct snd_pcm_substream *substream,
298 struct snd_pcm_hw_params *params,
299 struct snd_soc_dai *dai)
300{
301 struct snd_soc_codec *codec = dai->codec;
302 struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
303 int rate = params_rate(params), fs = 256;
304 u8 mode2;
305
306 if (rate)
307 fs = ak4641->sysclk / rate;
308 else
309 return -EINVAL;
310
311 /* set fs */
312 switch (fs) {
313 case 1024:
314 mode2 = (0x2 << 5);
315 break;
316 case 512:
317 mode2 = (0x1 << 5);
318 break;
319 case 256:
320 mode2 = (0x0 << 5);
321 break;
322 default:
323 dev_err(codec->dev, "Error: unsupported fs=%d\n", fs);
324 return -EINVAL;
325 }
326
327 snd_soc_update_bits(codec, AK4641_MODE2, (0x3 << 5), mode2);
328
329 /* Update de-emphasis filter for the new rate */
330 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
331 ak4641->playback_fs = rate;
332 ak4641_set_deemph(codec);
333 }
334
335 return 0;
336}
337
338static int ak4641_pcm_set_dai_fmt(struct snd_soc_dai *codec_dai,
339 unsigned int fmt)
340{
341 struct snd_soc_codec *codec = codec_dai->codec;
342 u8 btif;
343 int ret;
344
345 /* interface format */
346 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
347 case SND_SOC_DAIFMT_I2S:
348 btif = (0x3 << 5);
349 break;
350 case SND_SOC_DAIFMT_LEFT_J:
351 btif = (0x2 << 5);
352 break;
353 case SND_SOC_DAIFMT_DSP_A: /* MSB after FRM */
354 btif = (0x0 << 5);
355 break;
356 case SND_SOC_DAIFMT_DSP_B: /* MSB during FRM */
357 btif = (0x1 << 5);
358 break;
359 default:
360 return -EINVAL;
361 }
362
363 ret = snd_soc_update_bits(codec, AK4641_BTIF, (0x3 << 5), btif);
364 if (ret < 0)
365 return ret;
366
367 return 0;
368}
369
370static int ak4641_i2s_set_dai_fmt(struct snd_soc_dai *codec_dai,
371 unsigned int fmt)
372{
373 struct snd_soc_codec *codec = codec_dai->codec;
374 u8 mode1 = 0;
375
376 /* interface format */
377 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
378 case SND_SOC_DAIFMT_I2S:
379 mode1 = 0x02;
380 break;
381 case SND_SOC_DAIFMT_LEFT_J:
382 mode1 = 0x01;
383 break;
384 default:
385 return -EINVAL;
386 }
387
388 return snd_soc_write(codec, AK4641_MODE1, mode1);
389}
390
391static int ak4641_mute(struct snd_soc_dai *dai, int mute)
392{
393 struct snd_soc_codec *codec = dai->codec;
394
395 return snd_soc_update_bits(codec, AK4641_DAC, 0x20, mute ? 0x20 : 0);
396}
397
398static int ak4641_set_bias_level(struct snd_soc_codec *codec,
399 enum snd_soc_bias_level level)
400{
401 struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
402 struct ak4641_platform_data *pdata = codec->dev->platform_data;
403 int ret;
404
405 switch (level) {
406 case SND_SOC_BIAS_ON:
407 /* unmute */
408 snd_soc_update_bits(codec, AK4641_DAC, 0x20, 0);
409 break;
410 case SND_SOC_BIAS_PREPARE:
411 /* mute */
412 snd_soc_update_bits(codec, AK4641_DAC, 0x20, 0x20);
413 break;
414 case SND_SOC_BIAS_STANDBY:
415 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
416 if (pdata && gpio_is_valid(pdata->gpio_power))
417 gpio_set_value(pdata->gpio_power, 1);
418 mdelay(1);
419 if (pdata && gpio_is_valid(pdata->gpio_npdn))
420 gpio_set_value(pdata->gpio_npdn, 1);
421 mdelay(1);
422
423 ret = regcache_sync(ak4641->regmap);
424 if (ret) {
425 dev_err(codec->dev,
426 "Failed to sync cache: %d\n", ret);
427 return ret;
428 }
429 }
430 snd_soc_update_bits(codec, AK4641_PM1, 0x80, 0x80);
431 snd_soc_update_bits(codec, AK4641_PM2, 0x80, 0);
432 break;
433 case SND_SOC_BIAS_OFF:
434 snd_soc_update_bits(codec, AK4641_PM1, 0x80, 0);
435 if (pdata && gpio_is_valid(pdata->gpio_npdn))
436 gpio_set_value(pdata->gpio_npdn, 0);
437 if (pdata && gpio_is_valid(pdata->gpio_power))
438 gpio_set_value(pdata->gpio_power, 0);
439 regcache_mark_dirty(ak4641->regmap);
440 break;
441 }
442 return 0;
443}
444
445#define AK4641_RATES (SNDRV_PCM_RATE_8000_48000)
446#define AK4641_RATES_BT (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
447 SNDRV_PCM_RATE_16000)
448#define AK4641_FORMATS (SNDRV_PCM_FMTBIT_S16_LE)
449
450static const struct snd_soc_dai_ops ak4641_i2s_dai_ops = {
451 .hw_params = ak4641_i2s_hw_params,
452 .set_fmt = ak4641_i2s_set_dai_fmt,
453 .digital_mute = ak4641_mute,
454 .set_sysclk = ak4641_set_dai_sysclk,
455};
456
457static const struct snd_soc_dai_ops ak4641_pcm_dai_ops = {
458 .hw_params = NULL, /* rates are controlled by BT chip */
459 .set_fmt = ak4641_pcm_set_dai_fmt,
460 .digital_mute = ak4641_mute,
461 .set_sysclk = ak4641_set_dai_sysclk,
462};
463
464static struct snd_soc_dai_driver ak4641_dai[] = {
465{
466 .name = "ak4641-hifi",
467 .id = 1,
468 .playback = {
469 .stream_name = "HiFi Playback",
470 .channels_min = 1,
471 .channels_max = 2,
472 .rates = AK4641_RATES,
473 .formats = AK4641_FORMATS,
474 },
475 .capture = {
476 .stream_name = "HiFi Capture",
477 .channels_min = 1,
478 .channels_max = 2,
479 .rates = AK4641_RATES,
480 .formats = AK4641_FORMATS,
481 },
482 .ops = &ak4641_i2s_dai_ops,
483 .symmetric_rates = 1,
484},
485{
486 .name = "ak4641-voice",
487 .id = 1,
488 .playback = {
489 .stream_name = "Voice Playback",
490 .channels_min = 1,
491 .channels_max = 1,
492 .rates = AK4641_RATES_BT,
493 .formats = AK4641_FORMATS,
494 },
495 .capture = {
496 .stream_name = "Voice Capture",
497 .channels_min = 1,
498 .channels_max = 1,
499 .rates = AK4641_RATES_BT,
500 .formats = AK4641_FORMATS,
501 },
502 .ops = &ak4641_pcm_dai_ops,
503 .symmetric_rates = 1,
504},
505};
506
507static struct snd_soc_codec_driver soc_codec_dev_ak4641 = {
508 .controls = ak4641_snd_controls,
509 .num_controls = ARRAY_SIZE(ak4641_snd_controls),
510 .dapm_widgets = ak4641_dapm_widgets,
511 .num_dapm_widgets = ARRAY_SIZE(ak4641_dapm_widgets),
512 .dapm_routes = ak4641_audio_map,
513 .num_dapm_routes = ARRAY_SIZE(ak4641_audio_map),
514 .set_bias_level = ak4641_set_bias_level,
515 .suspend_bias_off = true,
516};
517
518static const struct regmap_config ak4641_regmap = {
519 .reg_bits = 8,
520 .val_bits = 8,
521
522 .max_register = AK4641_BTIF,
523 .reg_defaults = ak4641_reg_defaults,
524 .num_reg_defaults = ARRAY_SIZE(ak4641_reg_defaults),
525 .cache_type = REGCACHE_RBTREE,
526};
527
528static int ak4641_i2c_probe(struct i2c_client *i2c,
529 const struct i2c_device_id *id)
530{
531 struct ak4641_platform_data *pdata = i2c->dev.platform_data;
532 struct ak4641_priv *ak4641;
533 int ret;
534
535 ak4641 = devm_kzalloc(&i2c->dev, sizeof(struct ak4641_priv),
536 GFP_KERNEL);
537 if (!ak4641)
538 return -ENOMEM;
539
540 ak4641->regmap = devm_regmap_init_i2c(i2c, &ak4641_regmap);
541 if (IS_ERR(ak4641->regmap))
542 return PTR_ERR(ak4641->regmap);
543
544 if (pdata) {
545 if (gpio_is_valid(pdata->gpio_power)) {
546 ret = gpio_request_one(pdata->gpio_power,
547 GPIOF_OUT_INIT_LOW, "ak4641 power");
548 if (ret)
549 goto err_out;
550 }
551 if (gpio_is_valid(pdata->gpio_npdn)) {
552 ret = gpio_request_one(pdata->gpio_npdn,
553 GPIOF_OUT_INIT_LOW, "ak4641 npdn");
554 if (ret)
555 goto err_gpio;
556
557 udelay(1); /* > 150 ns */
558 gpio_set_value(pdata->gpio_npdn, 1);
559 }
560 }
561
562 i2c_set_clientdata(i2c, ak4641);
563
564 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_ak4641,
565 ak4641_dai, ARRAY_SIZE(ak4641_dai));
566 if (ret != 0)
567 goto err_gpio2;
568
569 return 0;
570
571err_gpio2:
572 if (pdata) {
573 if (gpio_is_valid(pdata->gpio_power))
574 gpio_set_value(pdata->gpio_power, 0);
575 if (gpio_is_valid(pdata->gpio_npdn))
576 gpio_free(pdata->gpio_npdn);
577 }
578err_gpio:
579 if (pdata && gpio_is_valid(pdata->gpio_power))
580 gpio_free(pdata->gpio_power);
581err_out:
582 return ret;
583}
584
585static int ak4641_i2c_remove(struct i2c_client *i2c)
586{
587 struct ak4641_platform_data *pdata = i2c->dev.platform_data;
588
589 snd_soc_unregister_codec(&i2c->dev);
590
591 if (pdata) {
592 if (gpio_is_valid(pdata->gpio_power)) {
593 gpio_set_value(pdata->gpio_power, 0);
594 gpio_free(pdata->gpio_power);
595 }
596 if (gpio_is_valid(pdata->gpio_npdn))
597 gpio_free(pdata->gpio_npdn);
598 }
599
600 return 0;
601}
602
603static const struct i2c_device_id ak4641_i2c_id[] = {
604 { "ak4641", 0 },
605 { }
606};
607MODULE_DEVICE_TABLE(i2c, ak4641_i2c_id);
608
609static struct i2c_driver ak4641_i2c_driver = {
610 .driver = {
611 .name = "ak4641",
612 },
613 .probe = ak4641_i2c_probe,
614 .remove = ak4641_i2c_remove,
615 .id_table = ak4641_i2c_id,
616};
617
618module_i2c_driver(ak4641_i2c_driver);
619
620MODULE_DESCRIPTION("SoC AK4641 driver");
621MODULE_AUTHOR("Harald Welte <laforge@gnufiish.org>");
622MODULE_LICENSE("GPL");