blob: d25a9f3968d012ae4f7adbe971c1dfac9a78ec90 [file] [log] [blame]
Christian Pellegrin1cad1de2008-11-15 08:58:16 +01001/*
2 * uda134x.c -- UDA134X ALSA SoC Codec driver
3 *
4 * Modifications by Christian Pellegrin <chripell@evolware.org>
5 *
6 * Copyright 2007 Dension Audio Systems Ltd.
7 * Author: Zoltan Devai
8 *
9 * Based on the WM87xx drivers by Liam Girdwood and Richard Purdie
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16#include <linux/module.h>
17#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010019#include <sound/pcm.h>
20#include <sound/pcm_params.h>
21#include <sound/soc.h>
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010022#include <sound/initval.h>
23
24#include <sound/uda134x.h>
25#include <sound/l3.h>
26
Mark Brown72f2b892008-11-18 12:25:46 +000027#include "uda134x.h"
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010028
29
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010030#define UDA134X_RATES SNDRV_PCM_RATE_8000_48000
31#define UDA134X_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
32 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE)
33
34struct uda134x_priv {
35 int sysclk;
36 int dai_fmt;
37
38 struct snd_pcm_substream *master_substream;
39 struct snd_pcm_substream *slave_substream;
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +020040
41 struct regmap *regmap;
42 struct uda134x_platform_data *pd;
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010043};
44
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +020045static const struct reg_default uda134x_reg_defaults[] = {
46 { UDA134X_EA000, 0x04 },
47 { UDA134X_EA001, 0x04 },
48 { UDA134X_EA010, 0x04 },
49 { UDA134X_EA011, 0x00 },
50 { UDA134X_EA100, 0x00 },
51 { UDA134X_EA101, 0x00 },
52 { UDA134X_EA110, 0x00 },
53 { UDA134X_EA111, 0x00 },
54 { UDA134X_STATUS0, 0x00 },
55 { UDA134X_STATUS1, 0x03 },
56 { UDA134X_DATA000, 0x00 },
57 { UDA134X_DATA001, 0x00 },
58 { UDA134X_DATA010, 0x00 },
59 { UDA134X_DATA011, 0x00 },
60 { UDA134X_DATA1, 0x00 },
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010061};
62
63/*
64 * The codec has no support for reading its registers except for peak level...
65 */
66static inline unsigned int uda134x_read_reg_cache(struct snd_soc_codec *codec,
67 unsigned int reg)
68{
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +020069 struct uda134x_priv *uda134x = snd_soc_codec_get_drvdata(codec);
70 unsigned int val;
71 int ret;
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010072
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +020073 ret = regmap_read(uda134x->regmap, reg, &val);
74 if (ret)
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010075 return -1;
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +020076
77 return val;
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010078}
79
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +020080static void uda134x_write(struct snd_soc_codec *codec, unsigned int reg,
81 unsigned int val)
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010082{
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +020083 struct uda134x_priv *uda134x = snd_soc_codec_get_drvdata(codec);
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010084
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +020085 regmap_write(uda134x->regmap, reg, val);
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010086}
87
88/*
89 * Write to the uda134x registers
90 *
91 */
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +020092static int uda134x_regmap_write(void *context, unsigned int reg,
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010093 unsigned int value)
94{
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +020095 struct uda134x_platform_data *pd = context;
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010096 int ret;
97 u8 addr;
98 u8 data = value;
Christian Pellegrin1cad1de2008-11-15 08:58:16 +010099
100 switch (reg) {
101 case UDA134X_STATUS0:
102 case UDA134X_STATUS1:
103 addr = UDA134X_STATUS_ADDR;
Lars-Peter Clausen82c7b532015-07-13 12:26:46 +0200104 data |= (reg - UDA134X_STATUS0) << 7;
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100105 break;
106 case UDA134X_DATA000:
107 case UDA134X_DATA001:
108 case UDA134X_DATA010:
Vladimir Zapolskiyed632ad2010-06-24 15:17:07 +0400109 case UDA134X_DATA011:
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100110 addr = UDA134X_DATA0_ADDR;
Lars-Peter Clausen82c7b532015-07-13 12:26:46 +0200111 data |= (reg - UDA134X_DATA000) << 6;
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100112 break;
113 case UDA134X_DATA1:
114 addr = UDA134X_DATA1_ADDR;
115 break;
116 default:
117 /* It's an extended address register */
118 addr = (reg | UDA134X_EXTADDR_PREFIX);
119
120 ret = l3_write(&pd->l3,
121 UDA134X_DATA0_ADDR, &addr, 1);
122 if (ret != 1)
123 return -EIO;
124
125 addr = UDA134X_DATA0_ADDR;
126 data = (value | UDA134X_EXTDATA_PREFIX);
127 break;
128 }
129
130 ret = l3_write(&pd->l3,
131 addr, &data, 1);
132 if (ret != 1)
133 return -EIO;
134
135 return 0;
136}
137
138static inline void uda134x_reset(struct snd_soc_codec *codec)
139{
140 u8 reset_reg = uda134x_read_reg_cache(codec, UDA134X_STATUS0);
141 uda134x_write(codec, UDA134X_STATUS0, reset_reg | (1<<6));
142 msleep(1);
143 uda134x_write(codec, UDA134X_STATUS0, reset_reg & ~(1<<6));
144}
145
146static int uda134x_mute(struct snd_soc_dai *dai, int mute)
147{
148 struct snd_soc_codec *codec = dai->codec;
149 u8 mute_reg = uda134x_read_reg_cache(codec, UDA134X_DATA010);
150
151 pr_debug("%s mute: %d\n", __func__, mute);
152
153 if (mute)
154 mute_reg |= (1<<2);
155 else
156 mute_reg &= ~(1<<2);
157
Shine Liu0c093fb2009-08-17 18:52:01 +0800158 uda134x_write(codec, UDA134X_DATA010, mute_reg);
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100159
160 return 0;
161}
162
Mark Browndee89c42008-11-18 22:11:38 +0000163static int uda134x_startup(struct snd_pcm_substream *substream,
164 struct snd_soc_dai *dai)
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100165{
Mark Browne6968a12012-04-04 15:58:16 +0100166 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900167 struct uda134x_priv *uda134x = snd_soc_codec_get_drvdata(codec);
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100168 struct snd_pcm_runtime *master_runtime;
169
170 if (uda134x->master_substream) {
171 master_runtime = uda134x->master_substream->runtime;
172
173 pr_debug("%s constraining to %d bits at %d\n", __func__,
174 master_runtime->sample_bits,
175 master_runtime->rate);
176
177 snd_pcm_hw_constraint_minmax(substream->runtime,
178 SNDRV_PCM_HW_PARAM_RATE,
179 master_runtime->rate,
180 master_runtime->rate);
181
182 snd_pcm_hw_constraint_minmax(substream->runtime,
183 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
184 master_runtime->sample_bits,
185 master_runtime->sample_bits);
186
187 uda134x->slave_substream = substream;
188 } else
189 uda134x->master_substream = substream;
190
191 return 0;
192}
193
Mark Browndee89c42008-11-18 22:11:38 +0000194static void uda134x_shutdown(struct snd_pcm_substream *substream,
195 struct snd_soc_dai *dai)
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100196{
Mark Browne6968a12012-04-04 15:58:16 +0100197 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900198 struct uda134x_priv *uda134x = snd_soc_codec_get_drvdata(codec);
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100199
200 if (uda134x->master_substream == substream)
201 uda134x->master_substream = uda134x->slave_substream;
202
203 uda134x->slave_substream = NULL;
204}
205
206static int uda134x_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000207 struct snd_pcm_hw_params *params,
208 struct snd_soc_dai *dai)
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100209{
Lars-Peter Clausenab642462014-03-13 21:24:54 +0100210 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900211 struct uda134x_priv *uda134x = snd_soc_codec_get_drvdata(codec);
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100212 u8 hw_params;
213
214 if (substream == uda134x->slave_substream) {
215 pr_debug("%s ignoring hw_params for slave substream\n",
216 __func__);
217 return 0;
218 }
219
220 hw_params = uda134x_read_reg_cache(codec, UDA134X_STATUS0);
221 hw_params &= STATUS0_SYSCLK_MASK;
222 hw_params &= STATUS0_DAIFMT_MASK;
223
224 pr_debug("%s sysclk: %d, rate:%d\n", __func__,
225 uda134x->sysclk, params_rate(params));
226
227 /* set SYSCLK / fs ratio */
228 switch (uda134x->sysclk / params_rate(params)) {
229 case 512:
230 break;
231 case 384:
232 hw_params |= (1<<4);
233 break;
234 case 256:
235 hw_params |= (1<<5);
236 break;
237 default:
238 printk(KERN_ERR "%s unsupported fs\n", __func__);
239 return -EINVAL;
240 }
241
242 pr_debug("%s dai_fmt: %d, params_format:%d\n", __func__,
243 uda134x->dai_fmt, params_format(params));
244
245 /* set DAI format and word length */
246 switch (uda134x->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
247 case SND_SOC_DAIFMT_I2S:
248 break;
249 case SND_SOC_DAIFMT_RIGHT_J:
Mark Brownaa9ffad2014-07-31 12:49:26 +0100250 switch (params_width(params)) {
251 case 16:
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100252 hw_params |= (1<<1);
253 break;
Mark Brownaa9ffad2014-07-31 12:49:26 +0100254 case 18:
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100255 hw_params |= (1<<2);
256 break;
Mark Brownaa9ffad2014-07-31 12:49:26 +0100257 case 20:
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100258 hw_params |= ((1<<2) | (1<<1));
259 break;
260 default:
261 printk(KERN_ERR "%s unsupported format (right)\n",
262 __func__);
263 return -EINVAL;
264 }
265 break;
266 case SND_SOC_DAIFMT_LEFT_J:
267 hw_params |= (1<<3);
268 break;
269 default:
270 printk(KERN_ERR "%s unsupported format\n", __func__);
271 return -EINVAL;
272 }
273
274 uda134x_write(codec, UDA134X_STATUS0, hw_params);
275
276 return 0;
277}
278
279static int uda134x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
280 int clk_id, unsigned int freq, int dir)
281{
282 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900283 struct uda134x_priv *uda134x = snd_soc_codec_get_drvdata(codec);
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100284
Roel Kluin449bd542009-05-27 17:08:39 -0700285 pr_debug("%s clk_id: %d, freq: %u, dir: %d\n", __func__,
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100286 clk_id, freq, dir);
287
288 /* Anything between 256fs*8Khz and 512fs*48Khz should be acceptable
289 because the codec is slave. Of course limitations of the clock
290 master (the IIS controller) apply.
291 We'll error out on set_hw_params if it's not OK */
292 if ((freq >= (256 * 8000)) && (freq <= (512 * 48000))) {
293 uda134x->sysclk = freq;
294 return 0;
295 }
296
297 printk(KERN_ERR "%s unsupported sysclk\n", __func__);
298 return -EINVAL;
299}
300
301static int uda134x_set_dai_fmt(struct snd_soc_dai *codec_dai,
302 unsigned int fmt)
303{
304 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900305 struct uda134x_priv *uda134x = snd_soc_codec_get_drvdata(codec);
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100306
307 pr_debug("%s fmt: %08X\n", __func__, fmt);
308
309 /* codec supports only full slave mode */
310 if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) {
311 printk(KERN_ERR "%s unsupported slave mode\n", __func__);
312 return -EINVAL;
313 }
314
315 /* no support for clock inversion */
316 if ((fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF) {
317 printk(KERN_ERR "%s unsupported clock inversion\n", __func__);
318 return -EINVAL;
319 }
320
321 /* We can't setup DAI format here as it depends on the word bit num */
322 /* so let's just store the value for later */
323 uda134x->dai_fmt = fmt;
324
325 return 0;
326}
327
328static int uda134x_set_bias_level(struct snd_soc_codec *codec,
329 enum snd_soc_bias_level level)
330{
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +0200331 struct uda134x_priv *uda134x = snd_soc_codec_get_drvdata(codec);
332 struct uda134x_platform_data *pd = uda134x->pd;
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100333 pr_debug("%s bias level %d\n", __func__, level);
334
335 switch (level) {
336 case SND_SOC_BIAS_ON:
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100337 break;
338 case SND_SOC_BIAS_PREPARE:
339 /* power on */
340 if (pd->power) {
341 pd->power(1);
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +0200342 regcache_sync(uda134x->regmap);
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100343 }
344 break;
345 case SND_SOC_BIAS_STANDBY:
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100346 break;
347 case SND_SOC_BIAS_OFF:
348 /* power off */
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +0200349 if (pd->power) {
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100350 pd->power(0);
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +0200351 regcache_mark_dirty(uda134x->regmap);
352 }
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100353 break;
354 }
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100355 return 0;
356}
357
358static const char *uda134x_dsp_setting[] = {"Flat", "Minimum1",
359 "Minimum2", "Maximum"};
360static const char *uda134x_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
361static const char *uda134x_mixmode[] = {"Differential", "Analog1",
362 "Analog2", "Both"};
363
364static const struct soc_enum uda134x_mixer_enum[] = {
365SOC_ENUM_SINGLE(UDA134X_DATA010, 0, 0x04, uda134x_dsp_setting),
366SOC_ENUM_SINGLE(UDA134X_DATA010, 3, 0x04, uda134x_deemph),
367SOC_ENUM_SINGLE(UDA134X_EA010, 0, 0x04, uda134x_mixmode),
368};
369
370static const struct snd_kcontrol_new uda1341_snd_controls[] = {
371SOC_SINGLE("Master Playback Volume", UDA134X_DATA000, 0, 0x3F, 1),
372SOC_SINGLE("Capture Volume", UDA134X_EA010, 2, 0x07, 0),
373SOC_SINGLE("Analog1 Volume", UDA134X_EA000, 0, 0x1F, 1),
374SOC_SINGLE("Analog2 Volume", UDA134X_EA001, 0, 0x1F, 1),
375
376SOC_SINGLE("Mic Sensitivity", UDA134X_EA010, 2, 7, 0),
377SOC_SINGLE("Mic Volume", UDA134X_EA101, 0, 0x1F, 0),
378
379SOC_SINGLE("Tone Control - Bass", UDA134X_DATA001, 2, 0xF, 0),
380SOC_SINGLE("Tone Control - Treble", UDA134X_DATA001, 0, 3, 0),
381
382SOC_ENUM("Sound Processing Filter", uda134x_mixer_enum[0]),
383SOC_ENUM("PCM Playback De-emphasis", uda134x_mixer_enum[1]),
384SOC_ENUM("Input Mux", uda134x_mixer_enum[2]),
385
386SOC_SINGLE("AGC Switch", UDA134X_EA100, 4, 1, 0),
387SOC_SINGLE("AGC Target Volume", UDA134X_EA110, 0, 0x03, 1),
388SOC_SINGLE("AGC Timing", UDA134X_EA110, 2, 0x07, 0),
389
390SOC_SINGLE("DAC +6dB Switch", UDA134X_STATUS1, 6, 1, 0),
391SOC_SINGLE("ADC +6dB Switch", UDA134X_STATUS1, 5, 1, 0),
392SOC_SINGLE("ADC Polarity Switch", UDA134X_STATUS1, 4, 1, 0),
393SOC_SINGLE("DAC Polarity Switch", UDA134X_STATUS1, 3, 1, 0),
394SOC_SINGLE("Double Speed Playback Switch", UDA134X_STATUS1, 2, 1, 0),
395SOC_SINGLE("DC Filter Enable Switch", UDA134X_STATUS0, 0, 1, 0),
396};
397
398static const struct snd_kcontrol_new uda1340_snd_controls[] = {
399SOC_SINGLE("Master Playback Volume", UDA134X_DATA000, 0, 0x3F, 1),
400
401SOC_SINGLE("Tone Control - Bass", UDA134X_DATA001, 2, 0xF, 0),
402SOC_SINGLE("Tone Control - Treble", UDA134X_DATA001, 0, 3, 0),
403
404SOC_ENUM("Sound Processing Filter", uda134x_mixer_enum[0]),
405SOC_ENUM("PCM Playback De-emphasis", uda134x_mixer_enum[1]),
406
407SOC_SINGLE("DC Filter Enable Switch", UDA134X_STATUS0, 0, 1, 0),
408};
409
Vladimir Zapolskiyb28528a2010-04-26 14:56:57 +0400410static const struct snd_kcontrol_new uda1345_snd_controls[] = {
411SOC_SINGLE("Master Playback Volume", UDA134X_DATA000, 0, 0x3F, 1),
412
413SOC_ENUM("PCM Playback De-emphasis", uda134x_mixer_enum[1]),
414
415SOC_SINGLE("DC Filter Enable Switch", UDA134X_STATUS0, 0, 1, 0),
416};
417
Russell King113591e2013-07-30 11:18:52 +0100418/* UDA1341 has the DAC/ADC power down in STATUS1 */
419static const struct snd_soc_dapm_widget uda1341_dapm_widgets[] = {
420 SND_SOC_DAPM_DAC("DAC", "Playback", UDA134X_STATUS1, 0, 0),
421 SND_SOC_DAPM_ADC("ADC", "Capture", UDA134X_STATUS1, 1, 0),
422};
423
424/* UDA1340/4/5 has the DAC/ADC pwoer down in DATA0 11 */
425static const struct snd_soc_dapm_widget uda1340_dapm_widgets[] = {
426 SND_SOC_DAPM_DAC("DAC", "Playback", UDA134X_DATA011, 0, 0),
427 SND_SOC_DAPM_ADC("ADC", "Capture", UDA134X_DATA011, 1, 0),
428};
429
430/* Common DAPM widgets */
431static const struct snd_soc_dapm_widget uda134x_dapm_widgets[] = {
432 SND_SOC_DAPM_INPUT("VINL1"),
433 SND_SOC_DAPM_INPUT("VINR1"),
434 SND_SOC_DAPM_INPUT("VINL2"),
435 SND_SOC_DAPM_INPUT("VINR2"),
436 SND_SOC_DAPM_OUTPUT("VOUTL"),
437 SND_SOC_DAPM_OUTPUT("VOUTR"),
438};
439
440static const struct snd_soc_dapm_route uda134x_dapm_routes[] = {
441 { "ADC", NULL, "VINL1" },
442 { "ADC", NULL, "VINR1" },
443 { "ADC", NULL, "VINL2" },
444 { "ADC", NULL, "VINR2" },
445 { "VOUTL", NULL, "DAC" },
446 { "VOUTR", NULL, "DAC" },
447};
448
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100449static const struct snd_soc_dai_ops uda134x_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +0800450 .startup = uda134x_startup,
451 .shutdown = uda134x_shutdown,
452 .hw_params = uda134x_hw_params,
453 .digital_mute = uda134x_mute,
454 .set_sysclk = uda134x_set_dai_sysclk,
455 .set_fmt = uda134x_set_dai_fmt,
456};
457
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000458static struct snd_soc_dai_driver uda134x_dai = {
459 .name = "uda134x-hifi",
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100460 /* playback capabilities */
461 .playback = {
462 .stream_name = "Playback",
463 .channels_min = 1,
464 .channels_max = 2,
465 .rates = UDA134X_RATES,
466 .formats = UDA134X_FORMATS,
467 },
468 /* capture capabilities */
469 .capture = {
470 .stream_name = "Capture",
471 .channels_min = 1,
472 .channels_max = 2,
473 .rates = UDA134X_RATES,
474 .formats = UDA134X_FORMATS,
475 },
476 /* pcm operations */
Eric Miao6335d052009-03-03 09:41:00 +0800477 .ops = &uda134x_dai_ops,
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100478};
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100479
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000480static int uda134x_soc_probe(struct snd_soc_codec *codec)
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100481{
Lars-Peter Clausen81024b12015-05-11 09:42:33 +0200482 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
Lars-Peter Clausenf15c4442015-07-13 12:26:45 +0200483 struct uda134x_priv *uda134x = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +0200484 struct uda134x_platform_data *pd = uda134x->pd;
Russell King113591e2013-07-30 11:18:52 +0100485 const struct snd_soc_dapm_widget *widgets;
486 unsigned num_widgets;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000487 int ret;
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100488
489 printk(KERN_INFO "UDA134X SoC Audio Codec\n");
490
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100491 switch (pd->model) {
492 case UDA134X_UDA1340:
493 case UDA134X_UDA1341:
494 case UDA134X_UDA1344:
Vladimir Zapolskiyb28528a2010-04-26 14:56:57 +0400495 case UDA134X_UDA1345:
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100496 break;
497 default:
498 printk(KERN_ERR "UDA134X SoC codec: "
499 "unsupported model %d\n",
500 pd->model);
501 return -EINVAL;
502 }
503
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100504 if (pd->power)
505 pd->power(1);
506
507 uda134x_reset(codec);
508
Russell King113591e2013-07-30 11:18:52 +0100509 if (pd->model == UDA134X_UDA1341) {
510 widgets = uda1341_dapm_widgets;
511 num_widgets = ARRAY_SIZE(uda1341_dapm_widgets);
512 } else {
513 widgets = uda1340_dapm_widgets;
514 num_widgets = ARRAY_SIZE(uda1340_dapm_widgets);
515 }
516
Lars-Peter Clausen81024b12015-05-11 09:42:33 +0200517 ret = snd_soc_dapm_new_controls(dapm, widgets, num_widgets);
Russell King113591e2013-07-30 11:18:52 +0100518 if (ret) {
519 printk(KERN_ERR "%s failed to register dapm controls: %d",
520 __func__, ret);
Russell King113591e2013-07-30 11:18:52 +0100521 return ret;
522 }
523
Ian Molton3e8e1952009-01-09 00:23:21 +0000524 switch (pd->model) {
525 case UDA134X_UDA1340:
526 case UDA134X_UDA1344:
Liam Girdwood022658b2012-02-03 17:43:09 +0000527 ret = snd_soc_add_codec_controls(codec, uda1340_snd_controls,
Ian Molton3e8e1952009-01-09 00:23:21 +0000528 ARRAY_SIZE(uda1340_snd_controls));
529 break;
530 case UDA134X_UDA1341:
Liam Girdwood022658b2012-02-03 17:43:09 +0000531 ret = snd_soc_add_codec_controls(codec, uda1341_snd_controls,
Ian Molton3e8e1952009-01-09 00:23:21 +0000532 ARRAY_SIZE(uda1341_snd_controls));
533 break;
Vladimir Zapolskiyb28528a2010-04-26 14:56:57 +0400534 case UDA134X_UDA1345:
Liam Girdwood022658b2012-02-03 17:43:09 +0000535 ret = snd_soc_add_codec_controls(codec, uda1345_snd_controls,
Vladimir Zapolskiyb28528a2010-04-26 14:56:57 +0400536 ARRAY_SIZE(uda1345_snd_controls));
537 break;
Ian Molton3e8e1952009-01-09 00:23:21 +0000538 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200539 printk(KERN_ERR "%s unknown codec type: %d",
Ian Molton3e8e1952009-01-09 00:23:21 +0000540 __func__, pd->model);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 return -EINVAL;
Ian Molton3e8e1952009-01-09 00:23:21 +0000542 }
543
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100544 if (ret < 0) {
545 printk(KERN_ERR "UDA134X: failed to register controls\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000546 return ret;
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100547 }
548
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100549 return 0;
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100550}
551
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552static struct snd_soc_codec_driver soc_codec_dev_uda134x = {
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100553 .probe = uda134x_soc_probe,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 .set_bias_level = uda134x_set_bias_level,
Lars-Peter Clausene03b9752014-11-23 15:04:13 +0100555 .suspend_bias_off = true,
556
Russell King113591e2013-07-30 11:18:52 +0100557 .dapm_widgets = uda134x_dapm_widgets,
558 .num_dapm_widgets = ARRAY_SIZE(uda134x_dapm_widgets),
559 .dapm_routes = uda134x_dapm_routes,
560 .num_dapm_routes = ARRAY_SIZE(uda134x_dapm_routes),
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100561};
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100562
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +0200563static const struct regmap_config uda134x_regmap_config = {
564 .reg_bits = 8,
565 .val_bits = 8,
566 .max_register = UDA134X_DATA1,
567 .reg_defaults = uda134x_reg_defaults,
568 .num_reg_defaults = ARRAY_SIZE(uda134x_reg_defaults),
569 .cache_type = REGCACHE_RBTREE,
570
571 .reg_write = uda134x_regmap_write,
572};
573
Bill Pemberton7a79e942012-12-07 09:26:37 -0500574static int uda134x_codec_probe(struct platform_device *pdev)
Mark Brown64089b82008-12-08 19:17:58 +0000575{
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +0200576 struct uda134x_platform_data *pd = pdev->dev.platform_data;
Lars-Peter Clausenf15c4442015-07-13 12:26:45 +0200577 struct uda134x_priv *uda134x;
578
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +0200579 if (!pd) {
580 dev_err(&pdev->dev, "Missing L3 bitbang function\n");
581 return -ENODEV;
582 }
583
Lars-Peter Clausenf15c4442015-07-13 12:26:45 +0200584 uda134x = devm_kzalloc(&pdev->dev, sizeof(*uda134x), GFP_KERNEL);
585 if (!uda134x)
586 return -ENOMEM;
587
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +0200588 uda134x->pd = pd;
Lars-Peter Clausenf15c4442015-07-13 12:26:45 +0200589 platform_set_drvdata(pdev, uda134x);
590
Lars-Peter Clausenf33c3402015-07-13 12:26:47 +0200591 uda134x->regmap = devm_regmap_init(&pdev->dev, NULL, pd,
592 &uda134x_regmap_config);
593 if (IS_ERR(uda134x->regmap))
594 return PTR_ERR(uda134x->regmap);
595
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 return snd_soc_register_codec(&pdev->dev,
597 &soc_codec_dev_uda134x, &uda134x_dai, 1);
Mark Brown64089b82008-12-08 19:17:58 +0000598}
Mark Brown64089b82008-12-08 19:17:58 +0000599
Bill Pemberton7a79e942012-12-07 09:26:37 -0500600static int uda134x_codec_remove(struct platform_device *pdev)
Mark Brown64089b82008-12-08 19:17:58 +0000601{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000602 snd_soc_unregister_codec(&pdev->dev);
603 return 0;
Mark Brown64089b82008-12-08 19:17:58 +0000604}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605
606static struct platform_driver uda134x_codec_driver = {
607 .driver = {
608 .name = "uda134x-codec",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609 },
610 .probe = uda134x_codec_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500611 .remove = uda134x_codec_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612};
613
Mark Brown5bbcc3c2011-11-23 22:52:08 +0000614module_platform_driver(uda134x_codec_driver);
Mark Brown64089b82008-12-08 19:17:58 +0000615
Christian Pellegrin1cad1de2008-11-15 08:58:16 +0100616MODULE_DESCRIPTION("UDA134X ALSA soc codec driver");
617MODULE_AUTHOR("Zoltan Devai, Christian Pellegrin <chripell@evolware.org>");
618MODULE_LICENSE("GPL");