Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/djbw/async_tx
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / soc / codecs / ssm2602.c
CommitLineData
b7138212
CC
1/*
2 * File: sound/soc/codecs/ssm2602.c
3 * Author: Cliff Cai <Cliff.Cai@analog.com>
4 *
5 * Created: Tue June 06 2008
6 * Description: Driver for ssm2602 sound chip
7 *
8 * Modified:
9 * Copyright 2008 Analog Devices Inc.
10 *
11 * Bugs: Enter bugs at http://blackfin.uclinux.org/
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see the file COPYING, or write
25 * to the Free Software Foundation, Inc.,
26 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#include <linux/module.h>
30#include <linux/moduleparam.h>
31#include <linux/init.h>
32#include <linux/delay.h>
33#include <linux/pm.h>
34#include <linux/i2c.h>
35#include <linux/platform_device.h>
36#include <sound/core.h>
37#include <sound/pcm.h>
38#include <sound/pcm_params.h>
39#include <sound/soc.h>
40#include <sound/soc-dapm.h>
41#include <sound/initval.h>
42
43#include "ssm2602.h"
44
b7138212
CC
45#define SSM2602_VERSION "0.1"
46
47struct snd_soc_codec_device soc_codec_dev_ssm2602;
48
49/* codec private data */
50struct ssm2602_priv {
51 unsigned int sysclk;
52 struct snd_pcm_substream *master_substream;
53 struct snd_pcm_substream *slave_substream;
54};
55
56/*
57 * ssm2602 register cache
58 * We can't read the ssm2602 register space when we are
59 * using 2 wire for device control, so we cache them instead.
60 * There is no point in caching the reset register
61 */
62static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = {
63 0x0017, 0x0017, 0x0079, 0x0079,
64 0x0000, 0x0000, 0x0000, 0x000a,
65 0x0000, 0x0000
66};
67
68/*
69 * read ssm2602 register cache
70 */
71static inline unsigned int ssm2602_read_reg_cache(struct snd_soc_codec *codec,
72 unsigned int reg)
73{
74 u16 *cache = codec->reg_cache;
75 if (reg == SSM2602_RESET)
76 return 0;
77 if (reg >= SSM2602_CACHEREGNUM)
78 return -1;
79 return cache[reg];
80}
81
82/*
83 * write ssm2602 register cache
84 */
85static inline void ssm2602_write_reg_cache(struct snd_soc_codec *codec,
86 u16 reg, unsigned int value)
87{
88 u16 *cache = codec->reg_cache;
89 if (reg >= SSM2602_CACHEREGNUM)
90 return;
91 cache[reg] = value;
92}
93
94/*
95 * write to the ssm2602 register space
96 */
97static int ssm2602_write(struct snd_soc_codec *codec, unsigned int reg,
98 unsigned int value)
99{
100 u8 data[2];
101
102 /* data is
103 * D15..D9 ssm2602 register offset
104 * D8...D0 register data
105 */
106 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
107 data[1] = value & 0x00ff;
108
109 ssm2602_write_reg_cache(codec, reg, value);
110 if (codec->hw_write(codec->control_data, data, 2) == 2)
111 return 0;
112 else
113 return -EIO;
114}
115
116#define ssm2602_reset(c) ssm2602_write(c, SSM2602_RESET, 0)
117
118/*Appending several "None"s just for OSS mixer use*/
119static const char *ssm2602_input_select[] = {
120 "Line", "Mic", "None", "None", "None",
121 "None", "None", "None",
122};
123
124static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
125
126static const struct soc_enum ssm2602_enum[] = {
127 SOC_ENUM_SINGLE(SSM2602_APANA, 2, 2, ssm2602_input_select),
128 SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, 4, ssm2602_deemph),
129};
130
131static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
132
133SOC_DOUBLE_R("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
134 0, 127, 0),
135SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
136 7, 1, 0),
137
138SOC_DOUBLE_R("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 31, 0),
139SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
140
141SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
142SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
143
144SOC_SINGLE("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1),
145
146SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
147SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
148
149SOC_ENUM("Capture Source", ssm2602_enum[0]),
150
151SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
152};
153
b7138212
CC
154/* Output Mixer */
155static const struct snd_kcontrol_new ssm2602_output_mixer_controls[] = {
156SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
157SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
158SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
159};
160
161/* Input mux */
162static const struct snd_kcontrol_new ssm2602_input_mux_controls =
163SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
164
165static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
166SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
167 &ssm2602_output_mixer_controls[0],
168 ARRAY_SIZE(ssm2602_output_mixer_controls)),
169SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
170SND_SOC_DAPM_OUTPUT("LOUT"),
171SND_SOC_DAPM_OUTPUT("LHPOUT"),
172SND_SOC_DAPM_OUTPUT("ROUT"),
173SND_SOC_DAPM_OUTPUT("RHPOUT"),
174SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
175SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
176SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
177SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
178SND_SOC_DAPM_INPUT("MICIN"),
179SND_SOC_DAPM_INPUT("RLINEIN"),
180SND_SOC_DAPM_INPUT("LLINEIN"),
181};
182
183static const struct snd_soc_dapm_route audio_conn[] = {
184 /* output mixer */
185 {"Output Mixer", "Line Bypass Switch", "Line Input"},
186 {"Output Mixer", "HiFi Playback Switch", "DAC"},
187 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
188
189 /* outputs */
190 {"RHPOUT", NULL, "Output Mixer"},
191 {"ROUT", NULL, "Output Mixer"},
192 {"LHPOUT", NULL, "Output Mixer"},
193 {"LOUT", NULL, "Output Mixer"},
194
195 /* input mux */
196 {"Input Mux", "Line", "Line Input"},
197 {"Input Mux", "Mic", "Mic Bias"},
198 {"ADC", NULL, "Input Mux"},
199
200 /* inputs */
201 {"Line Input", NULL, "LLINEIN"},
202 {"Line Input", NULL, "RLINEIN"},
203 {"Mic Bias", NULL, "MICIN"},
204};
205
206static int ssm2602_add_widgets(struct snd_soc_codec *codec)
207{
208 snd_soc_dapm_new_controls(codec, ssm2602_dapm_widgets,
209 ARRAY_SIZE(ssm2602_dapm_widgets));
210
211 snd_soc_dapm_add_routes(codec, audio_conn, ARRAY_SIZE(audio_conn));
212
b7138212
CC
213 return 0;
214}
215
216struct _coeff_div {
217 u32 mclk;
218 u32 rate;
219 u16 fs;
220 u8 sr:4;
221 u8 bosr:1;
222 u8 usb:1;
223};
224
225/* codec mclk clock divider coefficients */
226static const struct _coeff_div coeff_div[] = {
227 /* 48k */
228 {12288000, 48000, 256, 0x0, 0x0, 0x0},
229 {18432000, 48000, 384, 0x0, 0x1, 0x0},
230 {12000000, 48000, 250, 0x0, 0x0, 0x1},
231
232 /* 32k */
233 {12288000, 32000, 384, 0x6, 0x0, 0x0},
234 {18432000, 32000, 576, 0x6, 0x1, 0x0},
235 {12000000, 32000, 375, 0x6, 0x0, 0x1},
236
237 /* 8k */
238 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
239 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
240 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
241 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
242 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
243
244 /* 96k */
245 {12288000, 96000, 128, 0x7, 0x0, 0x0},
246 {18432000, 96000, 192, 0x7, 0x1, 0x0},
247 {12000000, 96000, 125, 0x7, 0x0, 0x1},
248
249 /* 44.1k */
250 {11289600, 44100, 256, 0x8, 0x0, 0x0},
251 {16934400, 44100, 384, 0x8, 0x1, 0x0},
252 {12000000, 44100, 272, 0x8, 0x1, 0x1},
253
254 /* 88.2k */
255 {11289600, 88200, 128, 0xf, 0x0, 0x0},
256 {16934400, 88200, 192, 0xf, 0x1, 0x0},
257 {12000000, 88200, 136, 0xf, 0x1, 0x1},
258};
259
260static inline int get_coeff(int mclk, int rate)
261{
262 int i;
263
264 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
265 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
266 return i;
267 }
268 return i;
269}
270
271static int ssm2602_hw_params(struct snd_pcm_substream *substream,
dee89c4d
MB
272 struct snd_pcm_hw_params *params,
273 struct snd_soc_dai *dai)
b7138212
CC
274{
275 u16 srate;
276 struct snd_soc_pcm_runtime *rtd = substream->private_data;
277 struct snd_soc_device *socdev = rtd->socdev;
6627a653 278 struct snd_soc_codec *codec = socdev->card->codec;
b7138212 279 struct ssm2602_priv *ssm2602 = codec->private_data;
faab5a32 280 struct i2c_client *i2c = codec->control_data;
b7138212
CC
281 u16 iface = ssm2602_read_reg_cache(codec, SSM2602_IFACE) & 0xfff3;
282 int i = get_coeff(ssm2602->sysclk, params_rate(params));
283
faab5a32
KB
284 if (substream == ssm2602->slave_substream) {
285 dev_dbg(&i2c->dev, "Ignoring hw_params for slave substream\n");
286 return 0;
287 }
288
b7138212
CC
289 /*no match is found*/
290 if (i == ARRAY_SIZE(coeff_div))
291 return -EINVAL;
292
293 srate = (coeff_div[i].sr << 2) |
294 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
295
296 ssm2602_write(codec, SSM2602_ACTIVE, 0);
297 ssm2602_write(codec, SSM2602_SRATE, srate);
298
299 /* bit size */
300 switch (params_format(params)) {
301 case SNDRV_PCM_FORMAT_S16_LE:
302 break;
303 case SNDRV_PCM_FORMAT_S20_3LE:
304 iface |= 0x0004;
305 break;
306 case SNDRV_PCM_FORMAT_S24_LE:
307 iface |= 0x0008;
308 break;
309 case SNDRV_PCM_FORMAT_S32_LE:
310 iface |= 0x000c;
311 break;
312 }
313 ssm2602_write(codec, SSM2602_IFACE, iface);
314 ssm2602_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
315 return 0;
316}
317
dee89c4d
MB
318static int ssm2602_startup(struct snd_pcm_substream *substream,
319 struct snd_soc_dai *dai)
b7138212
CC
320{
321 struct snd_soc_pcm_runtime *rtd = substream->private_data;
322 struct snd_soc_device *socdev = rtd->socdev;
6627a653 323 struct snd_soc_codec *codec = socdev->card->codec;
b7138212 324 struct ssm2602_priv *ssm2602 = codec->private_data;
faab5a32 325 struct i2c_client *i2c = codec->control_data;
b7138212
CC
326 struct snd_pcm_runtime *master_runtime;
327
328 /* The DAI has shared clocks so if we already have a playback or
329 * capture going then constrain this substream to match it.
faab5a32 330 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
b7138212
CC
331 */
332 if (ssm2602->master_substream) {
333 master_runtime = ssm2602->master_substream->runtime;
faab5a32
KB
334 dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
335 master_runtime->sample_bits,
336 master_runtime->rate);
337
f692fce0
CC
338 if (master_runtime->rate != 0)
339 snd_pcm_hw_constraint_minmax(substream->runtime,
340 SNDRV_PCM_HW_PARAM_RATE,
341 master_runtime->rate,
342 master_runtime->rate);
343
344 if (master_runtime->sample_bits != 0)
345 snd_pcm_hw_constraint_minmax(substream->runtime,
346 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
347 master_runtime->sample_bits,
348 master_runtime->sample_bits);
b7138212
CC
349
350 ssm2602->slave_substream = substream;
351 } else
352 ssm2602->master_substream = substream;
353
354 return 0;
355}
356
dee89c4d
MB
357static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream,
358 struct snd_soc_dai *dai)
b7138212
CC
359{
360 struct snd_soc_pcm_runtime *rtd = substream->private_data;
361 struct snd_soc_device *socdev = rtd->socdev;
6627a653 362 struct snd_soc_codec *codec = socdev->card->codec;
b7138212
CC
363 /* set active */
364 ssm2602_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
365
366 return 0;
367}
368
dee89c4d
MB
369static void ssm2602_shutdown(struct snd_pcm_substream *substream,
370 struct snd_soc_dai *dai)
b7138212
CC
371{
372 struct snd_soc_pcm_runtime *rtd = substream->private_data;
373 struct snd_soc_device *socdev = rtd->socdev;
6627a653 374 struct snd_soc_codec *codec = socdev->card->codec;
faab5a32 375 struct ssm2602_priv *ssm2602 = codec->private_data;
f692fce0 376
b7138212
CC
377 /* deactivate */
378 if (!codec->active)
379 ssm2602_write(codec, SSM2602_ACTIVE, 0);
faab5a32
KB
380
381 if (ssm2602->master_substream == substream)
382 ssm2602->master_substream = ssm2602->slave_substream;
383
384 ssm2602->slave_substream = NULL;
b7138212
CC
385}
386
387static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
388{
389 struct snd_soc_codec *codec = dai->codec;
390 u16 mute_reg = ssm2602_read_reg_cache(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE;
391 if (mute)
392 ssm2602_write(codec, SSM2602_APDIGI,
393 mute_reg | APDIGI_ENABLE_DAC_MUTE);
394 else
395 ssm2602_write(codec, SSM2602_APDIGI, mute_reg);
396 return 0;
397}
398
399static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
400 int clk_id, unsigned int freq, int dir)
401{
402 struct snd_soc_codec *codec = codec_dai->codec;
403 struct ssm2602_priv *ssm2602 = codec->private_data;
404 switch (freq) {
405 case 11289600:
406 case 12000000:
407 case 12288000:
408 case 16934400:
409 case 18432000:
410 ssm2602->sysclk = freq;
411 return 0;
412 }
413 return -EINVAL;
414}
415
416static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
417 unsigned int fmt)
418{
419 struct snd_soc_codec *codec = codec_dai->codec;
420 u16 iface = 0;
421
422 /* set master/slave audio interface */
423 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
424 case SND_SOC_DAIFMT_CBM_CFM:
425 iface |= 0x0040;
426 break;
427 case SND_SOC_DAIFMT_CBS_CFS:
428 break;
429 default:
430 return -EINVAL;
431 }
432
433 /* interface format */
434 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
435 case SND_SOC_DAIFMT_I2S:
436 iface |= 0x0002;
437 break;
438 case SND_SOC_DAIFMT_RIGHT_J:
439 break;
440 case SND_SOC_DAIFMT_LEFT_J:
441 iface |= 0x0001;
442 break;
443 case SND_SOC_DAIFMT_DSP_A:
c6913485 444 iface |= 0x0013;
b7138212
CC
445 break;
446 case SND_SOC_DAIFMT_DSP_B:
c6913485 447 iface |= 0x0003;
b7138212
CC
448 break;
449 default:
450 return -EINVAL;
451 }
452
453 /* clock inversion */
454 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
455 case SND_SOC_DAIFMT_NB_NF:
456 break;
457 case SND_SOC_DAIFMT_IB_IF:
458 iface |= 0x0090;
459 break;
460 case SND_SOC_DAIFMT_IB_NF:
461 iface |= 0x0080;
462 break;
463 case SND_SOC_DAIFMT_NB_IF:
464 iface |= 0x0010;
465 break;
466 default:
467 return -EINVAL;
468 }
469
470 /* set iface */
471 ssm2602_write(codec, SSM2602_IFACE, iface);
472 return 0;
473}
474
475static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
476 enum snd_soc_bias_level level)
477{
478 u16 reg = ssm2602_read_reg_cache(codec, SSM2602_PWR) & 0xff7f;
479
480 switch (level) {
481 case SND_SOC_BIAS_ON:
482 /* vref/mid, osc on, dac unmute */
483 ssm2602_write(codec, SSM2602_PWR, reg);
484 break;
485 case SND_SOC_BIAS_PREPARE:
486 break;
487 case SND_SOC_BIAS_STANDBY:
488 /* everything off except vref/vmid, */
489 ssm2602_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN);
490 break;
491 case SND_SOC_BIAS_OFF:
492 /* everything off, dac mute, inactive */
493 ssm2602_write(codec, SSM2602_ACTIVE, 0);
494 ssm2602_write(codec, SSM2602_PWR, 0xffff);
495 break;
496
497 }
498 codec->bias_level = level;
499 return 0;
500}
501
2552a710
CC
502#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
503 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
504 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
b7138212 505
5de27b6c
KB
506#define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
507 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
508
6335d055
EM
509static struct snd_soc_dai_ops ssm2602_dai_ops = {
510 .startup = ssm2602_startup,
511 .prepare = ssm2602_pcm_prepare,
512 .hw_params = ssm2602_hw_params,
513 .shutdown = ssm2602_shutdown,
514 .digital_mute = ssm2602_mute,
515 .set_sysclk = ssm2602_set_dai_sysclk,
516 .set_fmt = ssm2602_set_dai_fmt,
517};
518
b7138212
CC
519struct snd_soc_dai ssm2602_dai = {
520 .name = "SSM2602",
521 .playback = {
522 .stream_name = "Playback",
523 .channels_min = 2,
524 .channels_max = 2,
525 .rates = SSM2602_RATES,
5de27b6c 526 .formats = SSM2602_FORMATS,},
b7138212
CC
527 .capture = {
528 .stream_name = "Capture",
529 .channels_min = 2,
530 .channels_max = 2,
531 .rates = SSM2602_RATES,
5de27b6c 532 .formats = SSM2602_FORMATS,},
6335d055 533 .ops = &ssm2602_dai_ops,
b7138212
CC
534};
535EXPORT_SYMBOL_GPL(ssm2602_dai);
536
537static int ssm2602_suspend(struct platform_device *pdev, pm_message_t state)
538{
539 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
6627a653 540 struct snd_soc_codec *codec = socdev->card->codec;
b7138212
CC
541
542 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
543 return 0;
544}
545
546static int ssm2602_resume(struct platform_device *pdev)
547{
548 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
6627a653 549 struct snd_soc_codec *codec = socdev->card->codec;
b7138212
CC
550 int i;
551 u8 data[2];
552 u16 *cache = codec->reg_cache;
553
554 /* Sync reg_cache with the hardware */
555 for (i = 0; i < ARRAY_SIZE(ssm2602_reg); i++) {
556 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
557 data[1] = cache[i] & 0x00ff;
558 codec->hw_write(codec->control_data, data, 2);
559 }
560 ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
561 ssm2602_set_bias_level(codec, codec->suspend_bias_level);
562 return 0;
563}
564
565/*
566 * initialise the ssm2602 driver
567 * register the mixer and dsp interfaces with the kernel
568 */
569static int ssm2602_init(struct snd_soc_device *socdev)
570{
6627a653 571 struct snd_soc_codec *codec = socdev->card->codec;
b7138212
CC
572 int reg, ret = 0;
573
574 codec->name = "SSM2602";
575 codec->owner = THIS_MODULE;
576 codec->read = ssm2602_read_reg_cache;
577 codec->write = ssm2602_write;
578 codec->set_bias_level = ssm2602_set_bias_level;
579 codec->dai = &ssm2602_dai;
580 codec->num_dai = 1;
581 codec->reg_cache_size = sizeof(ssm2602_reg);
582 codec->reg_cache = kmemdup(ssm2602_reg, sizeof(ssm2602_reg),
583 GFP_KERNEL);
584 if (codec->reg_cache == NULL)
585 return -ENOMEM;
586
587 ssm2602_reset(codec);
588
589 /* register pcms */
590 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
591 if (ret < 0) {
592 pr_err("ssm2602: failed to create pcms\n");
593 goto pcm_err;
594 }
595 /*power on device*/
596 ssm2602_write(codec, SSM2602_ACTIVE, 0);
597 /* set the update bits */
598 reg = ssm2602_read_reg_cache(codec, SSM2602_LINVOL);
599 ssm2602_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
600 reg = ssm2602_read_reg_cache(codec, SSM2602_RINVOL);
601 ssm2602_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
602 reg = ssm2602_read_reg_cache(codec, SSM2602_LOUT1V);
603 ssm2602_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
604 reg = ssm2602_read_reg_cache(codec, SSM2602_ROUT1V);
605 ssm2602_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
606 /*select Line in as default input*/
607 ssm2602_write(codec, SSM2602_APANA,
608 APANA_ENABLE_MIC_BOOST2 | APANA_SELECT_DAC |
609 APANA_ENABLE_MIC_BOOST);
610 ssm2602_write(codec, SSM2602_PWR, 0);
611
3e8e1952
IM
612 snd_soc_add_controls(codec, ssm2602_snd_controls,
613 ARRAY_SIZE(ssm2602_snd_controls));
b7138212 614 ssm2602_add_widgets(codec);
b7138212
CC
615
616 return ret;
617
b7138212
CC
618pcm_err:
619 kfree(codec->reg_cache);
620 return ret;
621}
622
623static struct snd_soc_device *ssm2602_socdev;
624
625#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
626/*
627 * ssm2602 2 wire address is determined by GPIO5
628 * state during powerup.
629 * low = 0x1a
630 * high = 0x1b
631 */
632static int ssm2602_i2c_probe(struct i2c_client *i2c,
633 const struct i2c_device_id *id)
634{
635 struct snd_soc_device *socdev = ssm2602_socdev;
6627a653 636 struct snd_soc_codec *codec = socdev->card->codec;
b7138212
CC
637 int ret;
638
639 i2c_set_clientdata(i2c, codec);
640 codec->control_data = i2c;
641
642 ret = ssm2602_init(socdev);
643 if (ret < 0)
644 pr_err("failed to initialise SSM2602\n");
645
646 return ret;
647}
648
649static int ssm2602_i2c_remove(struct i2c_client *client)
650{
651 struct snd_soc_codec *codec = i2c_get_clientdata(client);
652 kfree(codec->reg_cache);
653 return 0;
654}
655
656static const struct i2c_device_id ssm2602_i2c_id[] = {
657 { "ssm2602", 0 },
658 { }
659};
660MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
661/* corgi i2c codec control layer */
662static struct i2c_driver ssm2602_i2c_driver = {
663 .driver = {
664 .name = "SSM2602 I2C Codec",
665 .owner = THIS_MODULE,
666 },
667 .probe = ssm2602_i2c_probe,
668 .remove = ssm2602_i2c_remove,
669 .id_table = ssm2602_i2c_id,
670};
671
672static int ssm2602_add_i2c_device(struct platform_device *pdev,
673 const struct ssm2602_setup_data *setup)
674{
675 struct i2c_board_info info;
676 struct i2c_adapter *adapter;
677 struct i2c_client *client;
678 int ret;
679
680 ret = i2c_add_driver(&ssm2602_i2c_driver);
681 if (ret != 0) {
682 dev_err(&pdev->dev, "can't add i2c driver\n");
683 return ret;
684 }
685 memset(&info, 0, sizeof(struct i2c_board_info));
686 info.addr = setup->i2c_address;
687 strlcpy(info.type, "ssm2602", I2C_NAME_SIZE);
688 adapter = i2c_get_adapter(setup->i2c_bus);
689 if (!adapter) {
690 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
691 setup->i2c_bus);
692 goto err_driver;
693 }
694 client = i2c_new_device(adapter, &info);
695 i2c_put_adapter(adapter);
696 if (!client) {
697 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
698 (unsigned int)info.addr);
699 goto err_driver;
700 }
701 return 0;
702err_driver:
703 i2c_del_driver(&ssm2602_i2c_driver);
704 return -ENODEV;
705}
706#endif
707
708static int ssm2602_probe(struct platform_device *pdev)
709{
710 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
711 struct ssm2602_setup_data *setup;
712 struct snd_soc_codec *codec;
713 struct ssm2602_priv *ssm2602;
714 int ret = 0;
715
716 pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
717
718 setup = socdev->codec_data;
719 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
720 if (codec == NULL)
721 return -ENOMEM;
722
723 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
724 if (ssm2602 == NULL) {
725 kfree(codec);
726 return -ENOMEM;
727 }
728
729 codec->private_data = ssm2602;
6627a653 730 socdev->card->codec = codec;
b7138212
CC
731 mutex_init(&codec->mutex);
732 INIT_LIST_HEAD(&codec->dapm_widgets);
733 INIT_LIST_HEAD(&codec->dapm_paths);
734
735 ssm2602_socdev = socdev;
736#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
737 if (setup->i2c_address) {
738 codec->hw_write = (hw_write_t)i2c_master_send;
739 ret = ssm2602_add_i2c_device(pdev, setup);
740 }
741#else
742 /* other interfaces */
743#endif
744 return ret;
745}
746
747/* remove everything here */
748static int ssm2602_remove(struct platform_device *pdev)
749{
750 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
6627a653 751 struct snd_soc_codec *codec = socdev->card->codec;
b7138212
CC
752
753 if (codec->control_data)
754 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
755
756 snd_soc_free_pcms(socdev);
757 snd_soc_dapm_free(socdev);
758#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
759 i2c_unregister_device(codec->control_data);
760 i2c_del_driver(&ssm2602_i2c_driver);
761#endif
762 kfree(codec->private_data);
763 kfree(codec);
764
765 return 0;
766}
767
768struct snd_soc_codec_device soc_codec_dev_ssm2602 = {
769 .probe = ssm2602_probe,
770 .remove = ssm2602_remove,
771 .suspend = ssm2602_suspend,
772 .resume = ssm2602_resume,
773};
774EXPORT_SYMBOL_GPL(soc_codec_dev_ssm2602);
775
c9b3a40f 776static int __init ssm2602_modinit(void)
64089b84
MB
777{
778 return snd_soc_register_dai(&ssm2602_dai);
779}
780module_init(ssm2602_modinit);
781
782static void __exit ssm2602_exit(void)
783{
784 snd_soc_unregister_dai(&ssm2602_dai);
785}
786module_exit(ssm2602_exit);
787
b7138212
CC
788MODULE_DESCRIPTION("ASoC ssm2602 driver");
789MODULE_AUTHOR("Cliff Cai");
790MODULE_LICENSE("GPL");