blob: 0e522e718dfcdb9f6cc28b0ae19024f2dd20cc5e [file] [log] [blame]
Cliff Caib7138212008-09-05 18:09:57 +08001/*
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
Cliff Caib7138212008-09-05 18:09:57 +080045#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
154/* add non dapm controls */
155static int ssm2602_add_controls(struct snd_soc_codec *codec)
156{
157 int err, i;
158
159 for (i = 0; i < ARRAY_SIZE(ssm2602_snd_controls); i++) {
160 err = snd_ctl_add(codec->card,
161 snd_soc_cnew(&ssm2602_snd_controls[i], codec, NULL));
162 if (err < 0)
163 return err;
164 }
165
166 return 0;
167}
168
169/* Output Mixer */
170static const struct snd_kcontrol_new ssm2602_output_mixer_controls[] = {
171SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
172SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
173SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
174};
175
176/* Input mux */
177static const struct snd_kcontrol_new ssm2602_input_mux_controls =
178SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
179
180static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
181SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
182 &ssm2602_output_mixer_controls[0],
183 ARRAY_SIZE(ssm2602_output_mixer_controls)),
184SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
185SND_SOC_DAPM_OUTPUT("LOUT"),
186SND_SOC_DAPM_OUTPUT("LHPOUT"),
187SND_SOC_DAPM_OUTPUT("ROUT"),
188SND_SOC_DAPM_OUTPUT("RHPOUT"),
189SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
190SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
191SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
192SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
193SND_SOC_DAPM_INPUT("MICIN"),
194SND_SOC_DAPM_INPUT("RLINEIN"),
195SND_SOC_DAPM_INPUT("LLINEIN"),
196};
197
198static const struct snd_soc_dapm_route audio_conn[] = {
199 /* output mixer */
200 {"Output Mixer", "Line Bypass Switch", "Line Input"},
201 {"Output Mixer", "HiFi Playback Switch", "DAC"},
202 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
203
204 /* outputs */
205 {"RHPOUT", NULL, "Output Mixer"},
206 {"ROUT", NULL, "Output Mixer"},
207 {"LHPOUT", NULL, "Output Mixer"},
208 {"LOUT", NULL, "Output Mixer"},
209
210 /* input mux */
211 {"Input Mux", "Line", "Line Input"},
212 {"Input Mux", "Mic", "Mic Bias"},
213 {"ADC", NULL, "Input Mux"},
214
215 /* inputs */
216 {"Line Input", NULL, "LLINEIN"},
217 {"Line Input", NULL, "RLINEIN"},
218 {"Mic Bias", NULL, "MICIN"},
219};
220
221static int ssm2602_add_widgets(struct snd_soc_codec *codec)
222{
223 snd_soc_dapm_new_controls(codec, ssm2602_dapm_widgets,
224 ARRAY_SIZE(ssm2602_dapm_widgets));
225
226 snd_soc_dapm_add_routes(codec, audio_conn, ARRAY_SIZE(audio_conn));
227
228 snd_soc_dapm_new_widgets(codec);
229 return 0;
230}
231
232struct _coeff_div {
233 u32 mclk;
234 u32 rate;
235 u16 fs;
236 u8 sr:4;
237 u8 bosr:1;
238 u8 usb:1;
239};
240
241/* codec mclk clock divider coefficients */
242static const struct _coeff_div coeff_div[] = {
243 /* 48k */
244 {12288000, 48000, 256, 0x0, 0x0, 0x0},
245 {18432000, 48000, 384, 0x0, 0x1, 0x0},
246 {12000000, 48000, 250, 0x0, 0x0, 0x1},
247
248 /* 32k */
249 {12288000, 32000, 384, 0x6, 0x0, 0x0},
250 {18432000, 32000, 576, 0x6, 0x1, 0x0},
251 {12000000, 32000, 375, 0x6, 0x0, 0x1},
252
253 /* 8k */
254 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
255 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
256 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
257 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
258 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
259
260 /* 96k */
261 {12288000, 96000, 128, 0x7, 0x0, 0x0},
262 {18432000, 96000, 192, 0x7, 0x1, 0x0},
263 {12000000, 96000, 125, 0x7, 0x0, 0x1},
264
265 /* 44.1k */
266 {11289600, 44100, 256, 0x8, 0x0, 0x0},
267 {16934400, 44100, 384, 0x8, 0x1, 0x0},
268 {12000000, 44100, 272, 0x8, 0x1, 0x1},
269
270 /* 88.2k */
271 {11289600, 88200, 128, 0xf, 0x0, 0x0},
272 {16934400, 88200, 192, 0xf, 0x1, 0x0},
273 {12000000, 88200, 136, 0xf, 0x1, 0x1},
274};
275
276static inline int get_coeff(int mclk, int rate)
277{
278 int i;
279
280 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
281 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
282 return i;
283 }
284 return i;
285}
286
287static int ssm2602_hw_params(struct snd_pcm_substream *substream,
288 struct snd_pcm_hw_params *params)
289{
290 u16 srate;
291 struct snd_soc_pcm_runtime *rtd = substream->private_data;
292 struct snd_soc_device *socdev = rtd->socdev;
293 struct snd_soc_codec *codec = socdev->codec;
294 struct ssm2602_priv *ssm2602 = codec->private_data;
Karl Beldanfaab5a32008-11-20 15:39:27 +0100295 struct i2c_client *i2c = codec->control_data;
Cliff Caib7138212008-09-05 18:09:57 +0800296 u16 iface = ssm2602_read_reg_cache(codec, SSM2602_IFACE) & 0xfff3;
297 int i = get_coeff(ssm2602->sysclk, params_rate(params));
298
Karl Beldanfaab5a32008-11-20 15:39:27 +0100299 if (substream == ssm2602->slave_substream) {
300 dev_dbg(&i2c->dev, "Ignoring hw_params for slave substream\n");
301 return 0;
302 }
303
Cliff Caib7138212008-09-05 18:09:57 +0800304 /*no match is found*/
305 if (i == ARRAY_SIZE(coeff_div))
306 return -EINVAL;
307
308 srate = (coeff_div[i].sr << 2) |
309 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
310
311 ssm2602_write(codec, SSM2602_ACTIVE, 0);
312 ssm2602_write(codec, SSM2602_SRATE, srate);
313
314 /* bit size */
315 switch (params_format(params)) {
316 case SNDRV_PCM_FORMAT_S16_LE:
317 break;
318 case SNDRV_PCM_FORMAT_S20_3LE:
319 iface |= 0x0004;
320 break;
321 case SNDRV_PCM_FORMAT_S24_LE:
322 iface |= 0x0008;
323 break;
324 case SNDRV_PCM_FORMAT_S32_LE:
325 iface |= 0x000c;
326 break;
327 }
328 ssm2602_write(codec, SSM2602_IFACE, iface);
329 ssm2602_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
330 return 0;
331}
332
333static int ssm2602_startup(struct snd_pcm_substream *substream)
334{
335 struct snd_soc_pcm_runtime *rtd = substream->private_data;
336 struct snd_soc_device *socdev = rtd->socdev;
337 struct snd_soc_codec *codec = socdev->codec;
338 struct ssm2602_priv *ssm2602 = codec->private_data;
Karl Beldanfaab5a32008-11-20 15:39:27 +0100339 struct i2c_client *i2c = codec->control_data;
Cliff Caib7138212008-09-05 18:09:57 +0800340 struct snd_pcm_runtime *master_runtime;
341
342 /* The DAI has shared clocks so if we already have a playback or
343 * capture going then constrain this substream to match it.
Karl Beldanfaab5a32008-11-20 15:39:27 +0100344 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
Cliff Caib7138212008-09-05 18:09:57 +0800345 */
346 if (ssm2602->master_substream) {
347 master_runtime = ssm2602->master_substream->runtime;
Karl Beldanfaab5a32008-11-20 15:39:27 +0100348 dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
349 master_runtime->sample_bits,
350 master_runtime->rate);
351
Cliff Caib7138212008-09-05 18:09:57 +0800352 snd_pcm_hw_constraint_minmax(substream->runtime,
353 SNDRV_PCM_HW_PARAM_RATE,
354 master_runtime->rate,
355 master_runtime->rate);
356
357 snd_pcm_hw_constraint_minmax(substream->runtime,
358 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
359 master_runtime->sample_bits,
360 master_runtime->sample_bits);
361
362 ssm2602->slave_substream = substream;
363 } else
364 ssm2602->master_substream = substream;
365
366 return 0;
367}
368
369static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream)
370{
371 struct snd_soc_pcm_runtime *rtd = substream->private_data;
372 struct snd_soc_device *socdev = rtd->socdev;
373 struct snd_soc_codec *codec = socdev->codec;
374 /* set active */
375 ssm2602_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
376
377 return 0;
378}
379
380static void ssm2602_shutdown(struct snd_pcm_substream *substream)
381{
382 struct snd_soc_pcm_runtime *rtd = substream->private_data;
383 struct snd_soc_device *socdev = rtd->socdev;
384 struct snd_soc_codec *codec = socdev->codec;
Karl Beldanfaab5a32008-11-20 15:39:27 +0100385 struct ssm2602_priv *ssm2602 = codec->private_data;
Cliff Caib7138212008-09-05 18:09:57 +0800386 /* deactivate */
387 if (!codec->active)
388 ssm2602_write(codec, SSM2602_ACTIVE, 0);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100389
390 if (ssm2602->master_substream == substream)
391 ssm2602->master_substream = ssm2602->slave_substream;
392
393 ssm2602->slave_substream = NULL;
Cliff Caib7138212008-09-05 18:09:57 +0800394}
395
396static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
397{
398 struct snd_soc_codec *codec = dai->codec;
399 u16 mute_reg = ssm2602_read_reg_cache(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE;
400 if (mute)
401 ssm2602_write(codec, SSM2602_APDIGI,
402 mute_reg | APDIGI_ENABLE_DAC_MUTE);
403 else
404 ssm2602_write(codec, SSM2602_APDIGI, mute_reg);
405 return 0;
406}
407
408static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
409 int clk_id, unsigned int freq, int dir)
410{
411 struct snd_soc_codec *codec = codec_dai->codec;
412 struct ssm2602_priv *ssm2602 = codec->private_data;
413 switch (freq) {
414 case 11289600:
415 case 12000000:
416 case 12288000:
417 case 16934400:
418 case 18432000:
419 ssm2602->sysclk = freq;
420 return 0;
421 }
422 return -EINVAL;
423}
424
425static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
426 unsigned int fmt)
427{
428 struct snd_soc_codec *codec = codec_dai->codec;
429 u16 iface = 0;
430
431 /* set master/slave audio interface */
432 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
433 case SND_SOC_DAIFMT_CBM_CFM:
434 iface |= 0x0040;
435 break;
436 case SND_SOC_DAIFMT_CBS_CFS:
437 break;
438 default:
439 return -EINVAL;
440 }
441
442 /* interface format */
443 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
444 case SND_SOC_DAIFMT_I2S:
445 iface |= 0x0002;
446 break;
447 case SND_SOC_DAIFMT_RIGHT_J:
448 break;
449 case SND_SOC_DAIFMT_LEFT_J:
450 iface |= 0x0001;
451 break;
452 case SND_SOC_DAIFMT_DSP_A:
453 iface |= 0x0003;
454 break;
455 case SND_SOC_DAIFMT_DSP_B:
456 iface |= 0x0013;
457 break;
458 default:
459 return -EINVAL;
460 }
461
462 /* clock inversion */
463 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
464 case SND_SOC_DAIFMT_NB_NF:
465 break;
466 case SND_SOC_DAIFMT_IB_IF:
467 iface |= 0x0090;
468 break;
469 case SND_SOC_DAIFMT_IB_NF:
470 iface |= 0x0080;
471 break;
472 case SND_SOC_DAIFMT_NB_IF:
473 iface |= 0x0010;
474 break;
475 default:
476 return -EINVAL;
477 }
478
479 /* set iface */
480 ssm2602_write(codec, SSM2602_IFACE, iface);
481 return 0;
482}
483
484static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
485 enum snd_soc_bias_level level)
486{
487 u16 reg = ssm2602_read_reg_cache(codec, SSM2602_PWR) & 0xff7f;
488
489 switch (level) {
490 case SND_SOC_BIAS_ON:
491 /* vref/mid, osc on, dac unmute */
492 ssm2602_write(codec, SSM2602_PWR, reg);
493 break;
494 case SND_SOC_BIAS_PREPARE:
495 break;
496 case SND_SOC_BIAS_STANDBY:
497 /* everything off except vref/vmid, */
498 ssm2602_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN);
499 break;
500 case SND_SOC_BIAS_OFF:
501 /* everything off, dac mute, inactive */
502 ssm2602_write(codec, SSM2602_ACTIVE, 0);
503 ssm2602_write(codec, SSM2602_PWR, 0xffff);
504 break;
505
506 }
507 codec->bias_level = level;
508 return 0;
509}
510
511#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
512 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
513 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
514 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
515 SNDRV_PCM_RATE_96000)
516
517struct snd_soc_dai ssm2602_dai = {
518 .name = "SSM2602",
519 .playback = {
520 .stream_name = "Playback",
521 .channels_min = 2,
522 .channels_max = 2,
523 .rates = SSM2602_RATES,
524 .formats = SNDRV_PCM_FMTBIT_S32_LE,},
525 .capture = {
526 .stream_name = "Capture",
527 .channels_min = 2,
528 .channels_max = 2,
529 .rates = SSM2602_RATES,
530 .formats = SNDRV_PCM_FMTBIT_S32_LE,},
531 .ops = {
532 .startup = ssm2602_startup,
533 .prepare = ssm2602_pcm_prepare,
534 .hw_params = ssm2602_hw_params,
535 .shutdown = ssm2602_shutdown,
536 },
537 .dai_ops = {
538 .digital_mute = ssm2602_mute,
539 .set_sysclk = ssm2602_set_dai_sysclk,
540 .set_fmt = ssm2602_set_dai_fmt,
541 }
542};
543EXPORT_SYMBOL_GPL(ssm2602_dai);
544
545static int ssm2602_suspend(struct platform_device *pdev, pm_message_t state)
546{
547 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
548 struct snd_soc_codec *codec = socdev->codec;
549
550 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
551 return 0;
552}
553
554static int ssm2602_resume(struct platform_device *pdev)
555{
556 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
557 struct snd_soc_codec *codec = socdev->codec;
558 int i;
559 u8 data[2];
560 u16 *cache = codec->reg_cache;
561
562 /* Sync reg_cache with the hardware */
563 for (i = 0; i < ARRAY_SIZE(ssm2602_reg); i++) {
564 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
565 data[1] = cache[i] & 0x00ff;
566 codec->hw_write(codec->control_data, data, 2);
567 }
568 ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
569 ssm2602_set_bias_level(codec, codec->suspend_bias_level);
570 return 0;
571}
572
573/*
574 * initialise the ssm2602 driver
575 * register the mixer and dsp interfaces with the kernel
576 */
577static int ssm2602_init(struct snd_soc_device *socdev)
578{
579 struct snd_soc_codec *codec = socdev->codec;
580 int reg, ret = 0;
581
582 codec->name = "SSM2602";
583 codec->owner = THIS_MODULE;
584 codec->read = ssm2602_read_reg_cache;
585 codec->write = ssm2602_write;
586 codec->set_bias_level = ssm2602_set_bias_level;
587 codec->dai = &ssm2602_dai;
588 codec->num_dai = 1;
589 codec->reg_cache_size = sizeof(ssm2602_reg);
590 codec->reg_cache = kmemdup(ssm2602_reg, sizeof(ssm2602_reg),
591 GFP_KERNEL);
592 if (codec->reg_cache == NULL)
593 return -ENOMEM;
594
595 ssm2602_reset(codec);
596
597 /* register pcms */
598 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
599 if (ret < 0) {
600 pr_err("ssm2602: failed to create pcms\n");
601 goto pcm_err;
602 }
603 /*power on device*/
604 ssm2602_write(codec, SSM2602_ACTIVE, 0);
605 /* set the update bits */
606 reg = ssm2602_read_reg_cache(codec, SSM2602_LINVOL);
607 ssm2602_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
608 reg = ssm2602_read_reg_cache(codec, SSM2602_RINVOL);
609 ssm2602_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
610 reg = ssm2602_read_reg_cache(codec, SSM2602_LOUT1V);
611 ssm2602_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
612 reg = ssm2602_read_reg_cache(codec, SSM2602_ROUT1V);
613 ssm2602_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
614 /*select Line in as default input*/
615 ssm2602_write(codec, SSM2602_APANA,
616 APANA_ENABLE_MIC_BOOST2 | APANA_SELECT_DAC |
617 APANA_ENABLE_MIC_BOOST);
618 ssm2602_write(codec, SSM2602_PWR, 0);
619
620 ssm2602_add_controls(codec);
621 ssm2602_add_widgets(codec);
622 ret = snd_soc_register_card(socdev);
623 if (ret < 0) {
624 pr_err("ssm2602: failed to register card\n");
625 goto card_err;
626 }
627
628 return ret;
629
630card_err:
631 snd_soc_free_pcms(socdev);
632 snd_soc_dapm_free(socdev);
633pcm_err:
634 kfree(codec->reg_cache);
635 return ret;
636}
637
638static struct snd_soc_device *ssm2602_socdev;
639
640#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
641/*
642 * ssm2602 2 wire address is determined by GPIO5
643 * state during powerup.
644 * low = 0x1a
645 * high = 0x1b
646 */
647static int ssm2602_i2c_probe(struct i2c_client *i2c,
648 const struct i2c_device_id *id)
649{
650 struct snd_soc_device *socdev = ssm2602_socdev;
651 struct snd_soc_codec *codec = socdev->codec;
652 int ret;
653
654 i2c_set_clientdata(i2c, codec);
655 codec->control_data = i2c;
656
657 ret = ssm2602_init(socdev);
658 if (ret < 0)
659 pr_err("failed to initialise SSM2602\n");
660
661 return ret;
662}
663
664static int ssm2602_i2c_remove(struct i2c_client *client)
665{
666 struct snd_soc_codec *codec = i2c_get_clientdata(client);
667 kfree(codec->reg_cache);
668 return 0;
669}
670
671static const struct i2c_device_id ssm2602_i2c_id[] = {
672 { "ssm2602", 0 },
673 { }
674};
675MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
676/* corgi i2c codec control layer */
677static struct i2c_driver ssm2602_i2c_driver = {
678 .driver = {
679 .name = "SSM2602 I2C Codec",
680 .owner = THIS_MODULE,
681 },
682 .probe = ssm2602_i2c_probe,
683 .remove = ssm2602_i2c_remove,
684 .id_table = ssm2602_i2c_id,
685};
686
687static int ssm2602_add_i2c_device(struct platform_device *pdev,
688 const struct ssm2602_setup_data *setup)
689{
690 struct i2c_board_info info;
691 struct i2c_adapter *adapter;
692 struct i2c_client *client;
693 int ret;
694
695 ret = i2c_add_driver(&ssm2602_i2c_driver);
696 if (ret != 0) {
697 dev_err(&pdev->dev, "can't add i2c driver\n");
698 return ret;
699 }
700 memset(&info, 0, sizeof(struct i2c_board_info));
701 info.addr = setup->i2c_address;
702 strlcpy(info.type, "ssm2602", I2C_NAME_SIZE);
703 adapter = i2c_get_adapter(setup->i2c_bus);
704 if (!adapter) {
705 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
706 setup->i2c_bus);
707 goto err_driver;
708 }
709 client = i2c_new_device(adapter, &info);
710 i2c_put_adapter(adapter);
711 if (!client) {
712 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
713 (unsigned int)info.addr);
714 goto err_driver;
715 }
716 return 0;
717err_driver:
718 i2c_del_driver(&ssm2602_i2c_driver);
719 return -ENODEV;
720}
721#endif
722
723static int ssm2602_probe(struct platform_device *pdev)
724{
725 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
726 struct ssm2602_setup_data *setup;
727 struct snd_soc_codec *codec;
728 struct ssm2602_priv *ssm2602;
729 int ret = 0;
730
731 pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
732
733 setup = socdev->codec_data;
734 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
735 if (codec == NULL)
736 return -ENOMEM;
737
738 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
739 if (ssm2602 == NULL) {
740 kfree(codec);
741 return -ENOMEM;
742 }
743
744 codec->private_data = ssm2602;
745 socdev->codec = codec;
746 mutex_init(&codec->mutex);
747 INIT_LIST_HEAD(&codec->dapm_widgets);
748 INIT_LIST_HEAD(&codec->dapm_paths);
749
750 ssm2602_socdev = socdev;
751#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
752 if (setup->i2c_address) {
753 codec->hw_write = (hw_write_t)i2c_master_send;
754 ret = ssm2602_add_i2c_device(pdev, setup);
755 }
756#else
757 /* other interfaces */
758#endif
759 return ret;
760}
761
762/* remove everything here */
763static int ssm2602_remove(struct platform_device *pdev)
764{
765 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
766 struct snd_soc_codec *codec = socdev->codec;
767
768 if (codec->control_data)
769 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
770
771 snd_soc_free_pcms(socdev);
772 snd_soc_dapm_free(socdev);
773#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
774 i2c_unregister_device(codec->control_data);
775 i2c_del_driver(&ssm2602_i2c_driver);
776#endif
777 kfree(codec->private_data);
778 kfree(codec);
779
780 return 0;
781}
782
783struct snd_soc_codec_device soc_codec_dev_ssm2602 = {
784 .probe = ssm2602_probe,
785 .remove = ssm2602_remove,
786 .suspend = ssm2602_suspend,
787 .resume = ssm2602_resume,
788};
789EXPORT_SYMBOL_GPL(soc_codec_dev_ssm2602);
790
791MODULE_DESCRIPTION("ASoC ssm2602 driver");
792MODULE_AUTHOR("Cliff Cai");
793MODULE_LICENSE("GPL");