blob: b9d989f25515533583c74daed2d03c1b97b6796b [file] [log] [blame]
anish kumar805d1322016-02-17 17:28:49 -08001/*
2 * max9867.c -- max9867 ALSA SoC Audio driver
3 *
4 * Copyright 2013-15 Maxim Integrated Products
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/delay.h>
12#include <linux/i2c.h>
13#include <linux/module.h>
14#include <linux/regmap.h>
15#include <sound/pcm_params.h>
16#include <sound/soc.h>
17#include <sound/tlv.h>
18#include "max9867.h"
19
20static const char *const max9867_spmode[] = {
21 "Stereo Diff", "Mono Diff",
22 "Stereo Cap", "Mono Cap",
23 "Stereo Single", "Mono Single",
24 "Stereo Single Fast", "Mono Single Fast"
25};
26static const char *const max9867_sidetone_text[] = {
27 "None", "Left", "Right", "LeftRight", "LeftRightDiv2",
28};
29static const char *const max9867_filter_text[] = {"IIR", "FIR"};
30
31static SOC_ENUM_SINGLE_DECL(max9867_filter, MAX9867_CODECFLTR, 7,
32 max9867_filter_text);
33static SOC_ENUM_SINGLE_DECL(max9867_spkmode, MAX9867_MODECONFIG, 0,
34 max9867_spmode);
35static SOC_ENUM_SINGLE_DECL(max9867_sidetone, MAX9867_DACGAIN, 6,
36 max9867_sidetone_text);
37static DECLARE_TLV_DB_SCALE(max9860_capture_tlv, -600, 200, 0);
38static DECLARE_TLV_DB_SCALE(max9860_mic_tlv, 2000, 100, 1);
39static DECLARE_TLV_DB_SCALE(max9860_adc_left_tlv, -1200, 100, 1);
40static DECLARE_TLV_DB_SCALE(max9860_adc_right_tlv, -1200, 100, 1);
41static const unsigned int max98088_micboost_tlv[] = {
42 TLV_DB_RANGE_HEAD(2),
43 0, 1, TLV_DB_SCALE_ITEM(0, 2000, 0),
44 2, 2, TLV_DB_SCALE_ITEM(3000, 0, 0),
45};
46
47static const struct snd_kcontrol_new max9867_snd_controls[] = {
48 SOC_DOUBLE_R("Master Playback Volume", MAX9867_LEFTVOL,
49 MAX9867_RIGHTVOL, 0, 63, 1),
50 SOC_DOUBLE_R_TLV("Capture Volume", MAX9867_LEFTMICGAIN,
51 MAX9867_RIGHTMICGAIN,
52 0, 15, 1, max9860_capture_tlv),
53 SOC_DOUBLE_R_TLV("Mic Volume", MAX9867_LEFTMICGAIN,
54 MAX9867_RIGHTMICGAIN, 0, 31, 1, max9860_mic_tlv),
55 SOC_DOUBLE_R_TLV("Mic Boost Volume", MAX9867_LEFTMICGAIN,
56 MAX9867_RIGHTMICGAIN, 5, 3, 0, max98088_micboost_tlv),
57 SOC_ENUM("Digital Sidetone Src", max9867_sidetone),
58 SOC_SINGLE("Sidetone Volume", MAX9867_DACGAIN, 0, 31, 1),
59 SOC_SINGLE("DAC Volume", MAX9867_DACLEVEL, 4, 3, 0),
60 SOC_SINGLE("DAC Attenuation", MAX9867_DACLEVEL, 0, 15, 1),
61 SOC_SINGLE_TLV("ADC Left Volume", MAX9867_ADCLEVEL,
62 4, 15, 1, max9860_adc_left_tlv),
63 SOC_SINGLE_TLV("ADC Right Volume", MAX9867_ADCLEVEL,
64 0, 15, 1, max9860_adc_right_tlv),
65 SOC_ENUM("Speaker Mode", max9867_spkmode),
66 SOC_SINGLE("Volume Smoothing Switch", MAX9867_MODECONFIG, 6, 1, 0),
67 SOC_SINGLE("ZCD Switch", MAX9867_MODECONFIG, 5, 1, 0),
68 SOC_ENUM("DSP Filter", max9867_filter),
69};
70
71static const char *const max9867_mux[] = {"None", "Mic", "Line", "Mic_Line"};
72
73static SOC_ENUM_SINGLE_DECL(max9867_mux_enum,
74 MAX9867_INPUTCONFIG, MAX9867_INPUT_SHIFT,
75 max9867_mux);
76
77static const struct snd_kcontrol_new max9867_dapm_mux_controls =
78 SOC_DAPM_ENUM("Route", max9867_mux_enum);
79
80static const struct snd_kcontrol_new max9867_left_dapm_control =
81 SOC_DAPM_SINGLE("Switch", MAX9867_PWRMAN, 6, 1, 0);
82static const struct snd_kcontrol_new max9867_right_dapm_control =
83 SOC_DAPM_SINGLE("Switch", MAX9867_PWRMAN, 5, 1, 0);
84static const struct snd_kcontrol_new max9867_line_dapm_control =
85 SOC_DAPM_SINGLE("Switch", MAX9867_LEFTLINELVL, 6, 1, 1);
86
87static const struct snd_soc_dapm_widget max9867_dapm_widgets[] = {
88 SND_SOC_DAPM_AIF_IN("DAI_OUT", "HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
89 SND_SOC_DAPM_DAC("Left DAC", NULL, MAX9867_PWRMAN, 3, 0),
90 SND_SOC_DAPM_DAC("Right DAC", NULL, MAX9867_PWRMAN, 2, 0),
91 SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
92 SND_SOC_DAPM_OUTPUT("HPOUT"),
93
94 SND_SOC_DAPM_AIF_IN("DAI_IN", "HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
95 SND_SOC_DAPM_ADC("Left ADC", "HiFi Capture", MAX9867_PWRMAN, 1, 0),
96 SND_SOC_DAPM_ADC("Right ADC", "HiFi Capture", MAX9867_PWRMAN, 0, 0),
97 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0,
98 &max9867_dapm_mux_controls),
99
100 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
101 SND_SOC_DAPM_SWITCH("Left Line", MAX9867_LEFTLINELVL, 6, 1,
102 &max9867_left_dapm_control),
103 SND_SOC_DAPM_SWITCH("Right Line", MAX9867_RIGTHLINELVL, 6, 1,
104 &max9867_right_dapm_control),
105 SND_SOC_DAPM_SWITCH("Line Mixer", SND_SOC_NOPM, 0, 0,
106 &max9867_line_dapm_control),
107 SND_SOC_DAPM_INPUT("LINE_IN"),
108};
109
110static const struct snd_soc_dapm_route max9867_audio_map[] = {
111 {"Left DAC", NULL, "DAI_OUT"},
112 {"Right DAC", NULL, "DAI_OUT"},
113 {"Output Mixer", NULL, "Left DAC"},
114 {"Output Mixer", NULL, "Right DAC"},
115 {"HPOUT", NULL, "Output Mixer"},
116
117 {"Left ADC", NULL, "DAI_IN"},
118 {"Right ADC", NULL, "DAI_IN"},
119 {"Input Mixer", NULL, "Left ADC"},
120 {"Input Mixer", NULL, "Right ADC"},
121 {"Input Mux", "Line", "Input Mixer"},
122 {"Input Mux", "Mic", "Input Mixer"},
123 {"Input Mux", "Mic_Line", "Input Mixer"},
124 {"Right Line", "Switch", "Input Mux"},
125 {"Left Line", "Switch", "Input Mux"},
126 {"LINE_IN", NULL, "Left Line"},
127 {"LINE_IN", NULL, "Right Line"},
128};
129
130enum rates {
131 pcm_rate_8, pcm_rate_16, pcm_rate_24,
132 pcm_rate_32, pcm_rate_44,
133 pcm_rate_48, max_pcm_rate,
134};
135
136struct ni_div_rates {
137 u32 mclk;
138 u16 ni[max_pcm_rate];
139} ni_div[] = {
140 {11289600, {0x116A, 0x22D4, 0x343F, 0x45A9, 0x6000, 0x687D} },
141 {12000000, {0x1062, 0x20C5, 0x3127, 0x4189, 0x5A51, 0x624E} },
142 {12288000, {0x1000, 0x2000, 0x3000, 0x4000, 0x5833, 0x6000} },
143 {13000000, {0x0F20, 0x1E3F, 0x2D5F, 0x3C7F, 0x535F, 0x5ABE} },
144 {19200000, {0x0A3D, 0x147B, 0x1EB8, 0x28F6, 0x3873, 0x3D71} },
145 {24000000, {0x1062, 0x20C5, 0x1893, 0x4189, 0x5A51, 0x624E} },
146 {26000000, {0x0F20, 0x1E3F, 0x16AF, 0x3C7F, 0x535F, 0x5ABE} },
147 {27000000, {0x0E90, 0x1D21, 0x15D8, 0x3A41, 0x5048, 0x5762} },
148};
149
150static inline int get_ni_value(int mclk, int rate)
151{
152 int i, ret = 0;
153
154 /* find the closest rate index*/
155 for (i = 0; i < ARRAY_SIZE(ni_div); i++) {
156 if (ni_div[i].mclk >= mclk)
157 break;
158 }
159
160 switch (rate) {
161 case 8000:
162 return ni_div[i].ni[pcm_rate_8];
163 case 16000:
164 return ni_div[i].ni[pcm_rate_16];
165 case 32000:
166 return ni_div[i].ni[pcm_rate_32];
167 case 44100:
168 return ni_div[i].ni[pcm_rate_44];
169 case 48000:
170 return ni_div[i].ni[pcm_rate_48];
171 default:
172 pr_err("%s wrong rate %d\n", __func__, rate);
173 ret = -EINVAL;
174 }
175 return ret;
176}
177
178static int max9867_dai_hw_params(struct snd_pcm_substream *substream,
179 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
180{
181 struct snd_soc_codec *codec = dai->codec;
182 struct max9867_priv *max9867 = snd_soc_codec_get_drvdata(codec);
183 unsigned int ni_h, ni_l, value;
184
185 value = get_ni_value(max9867->sysclk, params_rate(params));
186 if (value < 0)
187 return value;
188
189 ni_h = (0xFF00 & value) >> 8;
190 ni_l = 0x00FF & value;
191 /* set up the ni value */
192 regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKHIGH,
193 MAX9867_NI_HIGH_MASK, ni_h);
194 regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKLOW,
195 MAX9867_NI_LOW_MASK, ni_l);
196 if (!max9867->master) {
197 /*
198 * digital pll locks on to any externally supplied LRCLK signal
199 * and also enable rapid lock mode.
200 */
201 regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKLOW,
202 MAX9867_RAPID_LOCK, MAX9867_RAPID_LOCK);
203 regmap_update_bits(max9867->regmap, MAX9867_AUDIOCLKHIGH,
204 MAX9867_PLL, MAX9867_PLL);
205 } else {
206 unsigned long int bclk_rate, pclk_bclk_ratio;
207 int bclk_value;
208
209 bclk_rate = params_rate(params) * 2 *
210 snd_pcm_format_width(params_format(params));
211 pclk_bclk_ratio = max9867->pclk/bclk_rate;
212 switch (snd_pcm_format_width(params_format(params))) {
213 case 8:
214 case 16:
215 if (pclk_bclk_ratio == 2)
216 bclk_value = MAX9867_IFC1B_PCLK_2;
217 else if (pclk_bclk_ratio == 4)
218 bclk_value = MAX9867_IFC1B_PCLK_4;
219 else if (pclk_bclk_ratio == 8)
220 bclk_value = MAX9867_IFC1B_PCLK_8;
221 else if (pclk_bclk_ratio == 16)
222 bclk_value = MAX9867_IFC1B_PCLK_16;
223 else {
224 dev_err(codec->dev, "unsupported sampling rate\n");
225 return -EINVAL;
226 }
227 break;
228 case 24:
229 bclk_value = MAX9867_IFC1B_24BIT;
230 break;
231 case 32:
232 bclk_value = MAX9867_IFC1B_32BIT;
233 break;
234 default:
235 dev_err(codec->dev, "unsupported sampling rate\n");
236 return -EINVAL;
237 }
238 regmap_update_bits(max9867->regmap, MAX9867_IFC1B,
239 MAX9867_IFC1B_BCLK_MASK, bclk_value);
240 }
241 return 0;
242}
243
244static int max9867_prepare(struct snd_pcm_substream *substream,
245 struct snd_soc_dai *dai)
246{
247 struct snd_soc_codec *codec = dai->codec;
248 struct max9867_priv *max9867 = snd_soc_codec_get_drvdata(codec);
249
250 regmap_update_bits(max9867->regmap, MAX9867_PWRMAN,
251 MAX9867_SHTDOWN_MASK, MAX9867_SHTDOWN_MASK);
252 return 0;
253}
254
255static int max9867_mute(struct snd_soc_dai *dai, int mute)
256{
257 struct snd_soc_codec *codec = dai->codec;
258 struct max9867_priv *max9867 = snd_soc_codec_get_drvdata(codec);
259
260 if (mute)
261 regmap_update_bits(max9867->regmap, MAX9867_DACLEVEL,
262 MAX9867_DAC_MUTE_MASK, MAX9867_DAC_MUTE_MASK);
263 else
264 regmap_update_bits(max9867->regmap, MAX9867_DACLEVEL,
265 MAX9867_DAC_MUTE_MASK, 0);
266 return 0;
267}
268
269static int max9867_set_dai_sysclk(struct snd_soc_dai *codec_dai,
270 int clk_id, unsigned int freq, int dir)
271{
272 struct snd_soc_codec *codec = codec_dai->codec;
273 struct max9867_priv *max9867 = snd_soc_codec_get_drvdata(codec);
274 int value = 0;
275
276 /* Set the prescaler based on the master clock frequency*/
277 if (freq >= 10000000 && freq <= 20000000) {
278 value |= MAX9867_PSCLK_10_20;
279 max9867->pclk = freq;
280 } else if (freq >= 20000000 && freq <= 40000000) {
281 value |= MAX9867_PSCLK_20_40;
282 max9867->pclk = freq/2;
283 } else if (freq >= 40000000 && freq <= 60000000) {
284 value |= MAX9867_PSCLK_40_60;
285 max9867->pclk = freq/4;
286 } else {
287 pr_err("bad clock frequency %d", freq);
288 return -EINVAL;
289 }
290 value = value << MAX9867_PSCLK_SHIFT;
291 max9867->sysclk = freq;
292 /* exact integer mode is not supported */
293 value &= ~MAX9867_FREQ_MASK;
294 regmap_update_bits(max9867->regmap, MAX9867_SYSCLK,
295 MAX9867_PSCLK_MASK, value);
296 return 0;
297}
298
299static int max9867_dai_set_fmt(struct snd_soc_dai *codec_dai,
300 unsigned int fmt)
301{
302 struct snd_soc_codec *codec = codec_dai->codec;
303 struct max9867_priv *max9867 = snd_soc_codec_get_drvdata(codec);
304 u8 iface1A = 0, iface1B = 0;
305 int ret;
306
307 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
308 case SND_SOC_DAIFMT_CBM_CFM:
309 max9867->master = 1;
310 iface1A |= MAX9867_MASTER;
311 break;
312 case SND_SOC_DAIFMT_CBS_CFS:
313 max9867->master = 0;
314 iface1A &= ~MAX9867_MASTER;
315 break;
316 default:
317 return -EINVAL;
318 }
319
320 /* for i2s compatible mode */
321 iface1A |= MAX9867_I2S_DLY;
322 /* SDOUT goes to hiz state after all data is transferred */
323 iface1A |= MAX9867_SDOUT_HIZ;
324
325 /* Clock inversion bits, BCI and WCI */
326 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
327 case SND_SOC_DAIFMT_NB_NF:
328 break;
329 case SND_SOC_DAIFMT_IB_IF:
330 iface1A |= MAX9867_WCI_MODE | MAX9867_BCI_MODE;
331 break;
332 case SND_SOC_DAIFMT_IB_NF:
333 iface1A |= MAX9867_BCI_MODE;
334 break;
335 case SND_SOC_DAIFMT_NB_IF:
336 iface1A |= MAX9867_WCI_MODE;
337 break;
338 default:
339 return -EINVAL;
340 }
341
342 ret = regmap_write(max9867->regmap, MAX9867_IFC1A, iface1A);
343 ret = regmap_write(max9867->regmap, MAX9867_IFC1B, iface1B);
344 return 0;
345}
346
347static struct snd_soc_dai_ops max9867_dai_ops = {
348 .set_fmt = max9867_dai_set_fmt,
349 .set_sysclk = max9867_set_dai_sysclk,
350 .prepare = max9867_prepare,
351 .digital_mute = max9867_mute,
352 .hw_params = max9867_dai_hw_params,
353};
354
355#define MAX9867_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
356 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
357#define MAX9867_FORMATS (SNDRV_PCM_FMTBIT_S16_LE)
358
359static struct snd_soc_dai_driver max9867_dai[] = {
360 {
361 .name = "max9867-aif1",
362 .playback = {
363 .stream_name = "HiFi Playback",
364 .channels_min = 1,
365 .channels_max = 2,
366 .rates = MAX9867_RATES,
367 .formats = MAX9867_FORMATS,
368 },
369 .capture = {
370 .stream_name = "HiFi Capture",
371 .channels_min = 1,
372 .channels_max = 2,
373 .rates = MAX9867_RATES,
374 .formats = MAX9867_FORMATS,
375 },
376 .ops = &max9867_dai_ops,
377 }
378};
379
380#ifdef CONFIG_PM_SLEEP
381static int max9867_suspend(struct device *dev)
382{
383 struct max9867_priv *max9867 = dev_get_drvdata(dev);
384
385 /* Drop down to power saving mode when system is suspended */
386 regmap_update_bits(max9867->regmap, MAX9867_PWRMAN,
387 MAX9867_SHTDOWN_MASK, ~MAX9867_SHTDOWN_MASK);
388 return 0;
389}
390
391static int max9867_resume(struct device *dev)
392{
393 struct max9867_priv *max9867 = dev_get_drvdata(dev);
394
395 regmap_update_bits(max9867->regmap, MAX9867_PWRMAN,
396 MAX9867_SHTDOWN_MASK, MAX9867_SHTDOWN_MASK);
397 return 0;
398}
399#endif
400
401static int max9867_probe(struct snd_soc_codec *codec)
402{
403 struct max9867_priv *max9867 = snd_soc_codec_get_drvdata(codec);
404
405 dev_dbg(codec->dev, "max98090_probe\n");
406 max9867->codec = codec;
407 return 0;
408}
409
410static struct snd_soc_codec_driver max9867_codec = {
411 .probe = max9867_probe,
412 .controls = max9867_snd_controls,
413 .num_controls = ARRAY_SIZE(max9867_snd_controls),
414 .dapm_routes = max9867_audio_map,
415 .num_dapm_routes = ARRAY_SIZE(max9867_audio_map),
416 .dapm_widgets = max9867_dapm_widgets,
417 .num_dapm_widgets = ARRAY_SIZE(max9867_dapm_widgets),
418};
419
420static bool max9867_volatile_register(struct device *dev, unsigned int reg)
421{
422 switch (reg) {
423 case MAX9867_STATUS:
424 case MAX9867_JACKSTATUS:
425 case MAX9867_AUXHIGH:
426 case MAX9867_AUXLOW:
427 return true;
428 default:
429 return false;
430 }
431}
432
433static struct reg_default max9867_reg[] = {
434 { 0x04, 0x00 },
435 { 0x05, 0x00 },
436 { 0x06, 0x00 },
437 { 0x07, 0x00 },
438 { 0x08, 0x00 },
439 { 0x09, 0x00 },
440 { 0x0A, 0x00 },
441 { 0x0B, 0x00 },
442 { 0x0C, 0x00 },
443 { 0x0D, 0x00 },
444 { 0x0E, 0x00 },
445 { 0x0F, 0x00 },
446 { 0x10, 0x00 },
447 { 0x11, 0x00 },
448 { 0x12, 0x00 },
449 { 0x13, 0x00 },
450 { 0x14, 0x00 },
451 { 0x15, 0x00 },
452 { 0x16, 0x00 },
453 { 0x17, 0x00 },
454};
455
456static struct regmap_config max9867_regmap = {
457 .reg_bits = 8,
458 .val_bits = 8,
459 .max_register = MAX9867_REVISION,
460 .reg_defaults = max9867_reg,
461 .num_reg_defaults = ARRAY_SIZE(max9867_reg),
462 .volatile_reg = max9867_volatile_register,
463 .cache_type = REGCACHE_RBTREE,
464};
465
466static int max9867_i2c_probe(struct i2c_client *i2c,
467 const struct i2c_device_id *id)
468{
469 struct max9867_priv *max9867;
470 int ret = 0, reg;
471
472 max9867 = devm_kzalloc(&i2c->dev,
473 sizeof(*max9867), GFP_KERNEL);
474 if (!max9867)
475 return -ENOMEM;
476
477 i2c_set_clientdata(i2c, max9867);
478 max9867->regmap = devm_regmap_init_i2c(i2c, &max9867_regmap);
479 if (IS_ERR(max9867->regmap)) {
480 ret = PTR_ERR(max9867->regmap);
481 dev_err(&i2c->dev,
482 "Failed to allocate regmap: %d\n", ret);
483 return ret;
484 }
485 ret = regmap_read(max9867->regmap,
486 MAX9867_REVISION, &reg);
487 if (ret < 0) {
488 dev_err(&i2c->dev, "Failed to read: %d\n", ret);
489 return ret;
490 }
491 dev_info(&i2c->dev, "device revision: %x\n", reg);
492 ret = snd_soc_register_codec(&i2c->dev, &max9867_codec,
493 max9867_dai, ARRAY_SIZE(max9867_dai));
494 if (ret < 0) {
495 dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);
496 return ret;
497 }
498 return ret;
499}
500
501static int max9867_i2c_remove(struct i2c_client *client)
502{
503 snd_soc_unregister_codec(&client->dev);
504 return 0;
505}
506
507static const struct i2c_device_id max9867_i2c_id[] = {
508 { "max9867", 0 },
509};
510
511static const struct of_device_id max9867_of_match[] = {
512 { .compatible = "maxim,max9867", },
513 { }
514};
515
516MODULE_DEVICE_TABLE(i2c, max9867_i2c_id);
517
518static const struct dev_pm_ops max9867_pm_ops = {
519 SET_SYSTEM_SLEEP_PM_OPS(max9867_suspend, max9867_resume)
520};
521
522static struct i2c_driver max9867_i2c_driver = {
523 .driver = {
524 .name = "max9867",
525 .of_match_table = of_match_ptr(max9867_of_match),
526 .pm = &max9867_pm_ops,
527 },
528 .probe = max9867_i2c_probe,
529 .remove = max9867_i2c_remove,
530 .id_table = max9867_i2c_id,
531};
532
533module_i2c_driver(max9867_i2c_driver);
534
535MODULE_AUTHOR("anish kumar <yesanishhere@gmail.com>");
536MODULE_DESCRIPTION("ALSA SoC MAX9867 driver");
537MODULE_LICENSE("GPL");