blob: 1fc4c8e0899c18ec1f7f12750cf80023140cd5b7 [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
Cliff Caib7138212008-09-05 18:09:57 +0800154/* 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
213 snd_soc_dapm_new_widgets(codec);
214 return 0;
215}
216
217struct _coeff_div {
218 u32 mclk;
219 u32 rate;
220 u16 fs;
221 u8 sr:4;
222 u8 bosr:1;
223 u8 usb:1;
224};
225
226/* codec mclk clock divider coefficients */
227static const struct _coeff_div coeff_div[] = {
228 /* 48k */
229 {12288000, 48000, 256, 0x0, 0x0, 0x0},
230 {18432000, 48000, 384, 0x0, 0x1, 0x0},
231 {12000000, 48000, 250, 0x0, 0x0, 0x1},
232
233 /* 32k */
234 {12288000, 32000, 384, 0x6, 0x0, 0x0},
235 {18432000, 32000, 576, 0x6, 0x1, 0x0},
236 {12000000, 32000, 375, 0x6, 0x0, 0x1},
237
238 /* 8k */
239 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
240 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
241 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
242 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
243 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
244
245 /* 96k */
246 {12288000, 96000, 128, 0x7, 0x0, 0x0},
247 {18432000, 96000, 192, 0x7, 0x1, 0x0},
248 {12000000, 96000, 125, 0x7, 0x0, 0x1},
249
250 /* 44.1k */
251 {11289600, 44100, 256, 0x8, 0x0, 0x0},
252 {16934400, 44100, 384, 0x8, 0x1, 0x0},
253 {12000000, 44100, 272, 0x8, 0x1, 0x1},
254
255 /* 88.2k */
256 {11289600, 88200, 128, 0xf, 0x0, 0x0},
257 {16934400, 88200, 192, 0xf, 0x1, 0x0},
258 {12000000, 88200, 136, 0xf, 0x1, 0x1},
259};
260
261static inline int get_coeff(int mclk, int rate)
262{
263 int i;
264
265 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
266 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
267 return i;
268 }
269 return i;
270}
271
272static int ssm2602_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000273 struct snd_pcm_hw_params *params,
274 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800275{
276 u16 srate;
277 struct snd_soc_pcm_runtime *rtd = substream->private_data;
278 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000279 struct snd_soc_codec *codec = socdev->card->codec;
Cliff Caib7138212008-09-05 18:09:57 +0800280 struct ssm2602_priv *ssm2602 = codec->private_data;
Karl Beldanfaab5a32008-11-20 15:39:27 +0100281 struct i2c_client *i2c = codec->control_data;
Cliff Caib7138212008-09-05 18:09:57 +0800282 u16 iface = ssm2602_read_reg_cache(codec, SSM2602_IFACE) & 0xfff3;
283 int i = get_coeff(ssm2602->sysclk, params_rate(params));
284
Karl Beldanfaab5a32008-11-20 15:39:27 +0100285 if (substream == ssm2602->slave_substream) {
286 dev_dbg(&i2c->dev, "Ignoring hw_params for slave substream\n");
287 return 0;
288 }
289
Cliff Caib7138212008-09-05 18:09:57 +0800290 /*no match is found*/
291 if (i == ARRAY_SIZE(coeff_div))
292 return -EINVAL;
293
294 srate = (coeff_div[i].sr << 2) |
295 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
296
297 ssm2602_write(codec, SSM2602_ACTIVE, 0);
298 ssm2602_write(codec, SSM2602_SRATE, srate);
299
300 /* bit size */
301 switch (params_format(params)) {
302 case SNDRV_PCM_FORMAT_S16_LE:
303 break;
304 case SNDRV_PCM_FORMAT_S20_3LE:
305 iface |= 0x0004;
306 break;
307 case SNDRV_PCM_FORMAT_S24_LE:
308 iface |= 0x0008;
309 break;
310 case SNDRV_PCM_FORMAT_S32_LE:
311 iface |= 0x000c;
312 break;
313 }
314 ssm2602_write(codec, SSM2602_IFACE, iface);
315 ssm2602_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
316 return 0;
317}
318
Mark Browndee89c42008-11-18 22:11:38 +0000319static int ssm2602_startup(struct snd_pcm_substream *substream,
320 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800321{
322 struct snd_soc_pcm_runtime *rtd = substream->private_data;
323 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000324 struct snd_soc_codec *codec = socdev->card->codec;
Cliff Caib7138212008-09-05 18:09:57 +0800325 struct ssm2602_priv *ssm2602 = codec->private_data;
Karl Beldanfaab5a32008-11-20 15:39:27 +0100326 struct i2c_client *i2c = codec->control_data;
Cliff Caib7138212008-09-05 18:09:57 +0800327 struct snd_pcm_runtime *master_runtime;
328
329 /* The DAI has shared clocks so if we already have a playback or
330 * capture going then constrain this substream to match it.
Karl Beldanfaab5a32008-11-20 15:39:27 +0100331 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
Cliff Caib7138212008-09-05 18:09:57 +0800332 */
333 if (ssm2602->master_substream) {
334 master_runtime = ssm2602->master_substream->runtime;
Karl Beldanfaab5a32008-11-20 15:39:27 +0100335 dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
336 master_runtime->sample_bits,
337 master_runtime->rate);
338
Cliff Caif692fce2009-06-02 00:18:54 -0400339 if (master_runtime->rate != 0)
340 snd_pcm_hw_constraint_minmax(substream->runtime,
341 SNDRV_PCM_HW_PARAM_RATE,
342 master_runtime->rate,
343 master_runtime->rate);
Cliff Caib7138212008-09-05 18:09:57 +0800344
Cliff Caif692fce2009-06-02 00:18:54 -0400345 if (master_runtime->sample_bits != 0)
346 snd_pcm_hw_constraint_minmax(substream->runtime,
347 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
348 master_runtime->sample_bits,
349 master_runtime->sample_bits);
Cliff Caib7138212008-09-05 18:09:57 +0800350
351 ssm2602->slave_substream = substream;
352 } else
353 ssm2602->master_substream = substream;
354
355 return 0;
356}
357
Mark Browndee89c42008-11-18 22:11:38 +0000358static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream,
359 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800360{
361 struct snd_soc_pcm_runtime *rtd = substream->private_data;
362 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000363 struct snd_soc_codec *codec = socdev->card->codec;
Cliff Caib7138212008-09-05 18:09:57 +0800364 /* set active */
365 ssm2602_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
366
367 return 0;
368}
369
Mark Browndee89c42008-11-18 22:11:38 +0000370static void ssm2602_shutdown(struct snd_pcm_substream *substream,
371 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800372{
373 struct snd_soc_pcm_runtime *rtd = substream->private_data;
374 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000375 struct snd_soc_codec *codec = socdev->card->codec;
Karl Beldanfaab5a32008-11-20 15:39:27 +0100376 struct ssm2602_priv *ssm2602 = codec->private_data;
Cliff Caif692fce2009-06-02 00:18:54 -0400377
378 if (ssm2602->master_substream == substream)
379 ssm2602->master_substream = ssm2602->slave_substream;
380
381 ssm2602->slave_substream = NULL;
Cliff Caib7138212008-09-05 18:09:57 +0800382 /* deactivate */
383 if (!codec->active)
384 ssm2602_write(codec, SSM2602_ACTIVE, 0);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100385
386 if (ssm2602->master_substream == substream)
387 ssm2602->master_substream = ssm2602->slave_substream;
388
389 ssm2602->slave_substream = NULL;
Cliff Caib7138212008-09-05 18:09:57 +0800390}
391
392static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
393{
394 struct snd_soc_codec *codec = dai->codec;
395 u16 mute_reg = ssm2602_read_reg_cache(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE;
396 if (mute)
397 ssm2602_write(codec, SSM2602_APDIGI,
398 mute_reg | APDIGI_ENABLE_DAC_MUTE);
399 else
400 ssm2602_write(codec, SSM2602_APDIGI, mute_reg);
401 return 0;
402}
403
404static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
405 int clk_id, unsigned int freq, int dir)
406{
407 struct snd_soc_codec *codec = codec_dai->codec;
408 struct ssm2602_priv *ssm2602 = codec->private_data;
409 switch (freq) {
410 case 11289600:
411 case 12000000:
412 case 12288000:
413 case 16934400:
414 case 18432000:
415 ssm2602->sysclk = freq;
416 return 0;
417 }
418 return -EINVAL;
419}
420
421static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
422 unsigned int fmt)
423{
424 struct snd_soc_codec *codec = codec_dai->codec;
425 u16 iface = 0;
426
427 /* set master/slave audio interface */
428 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
429 case SND_SOC_DAIFMT_CBM_CFM:
430 iface |= 0x0040;
431 break;
432 case SND_SOC_DAIFMT_CBS_CFS:
433 break;
434 default:
435 return -EINVAL;
436 }
437
438 /* interface format */
439 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
440 case SND_SOC_DAIFMT_I2S:
441 iface |= 0x0002;
442 break;
443 case SND_SOC_DAIFMT_RIGHT_J:
444 break;
445 case SND_SOC_DAIFMT_LEFT_J:
446 iface |= 0x0001;
447 break;
448 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200449 iface |= 0x0013;
Cliff Caib7138212008-09-05 18:09:57 +0800450 break;
451 case SND_SOC_DAIFMT_DSP_B:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200452 iface |= 0x0003;
Cliff Caib7138212008-09-05 18:09:57 +0800453 break;
454 default:
455 return -EINVAL;
456 }
457
458 /* clock inversion */
459 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
460 case SND_SOC_DAIFMT_NB_NF:
461 break;
462 case SND_SOC_DAIFMT_IB_IF:
463 iface |= 0x0090;
464 break;
465 case SND_SOC_DAIFMT_IB_NF:
466 iface |= 0x0080;
467 break;
468 case SND_SOC_DAIFMT_NB_IF:
469 iface |= 0x0010;
470 break;
471 default:
472 return -EINVAL;
473 }
474
475 /* set iface */
476 ssm2602_write(codec, SSM2602_IFACE, iface);
477 return 0;
478}
479
480static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
481 enum snd_soc_bias_level level)
482{
483 u16 reg = ssm2602_read_reg_cache(codec, SSM2602_PWR) & 0xff7f;
484
485 switch (level) {
486 case SND_SOC_BIAS_ON:
487 /* vref/mid, osc on, dac unmute */
488 ssm2602_write(codec, SSM2602_PWR, reg);
489 break;
490 case SND_SOC_BIAS_PREPARE:
491 break;
492 case SND_SOC_BIAS_STANDBY:
493 /* everything off except vref/vmid, */
494 ssm2602_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN);
495 break;
496 case SND_SOC_BIAS_OFF:
497 /* everything off, dac mute, inactive */
498 ssm2602_write(codec, SSM2602_ACTIVE, 0);
499 ssm2602_write(codec, SSM2602_PWR, 0xffff);
500 break;
501
502 }
503 codec->bias_level = level;
504 return 0;
505}
506
Cliff Cai2552a712009-06-02 00:18:53 -0400507#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
508 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
509 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
Cliff Caib7138212008-09-05 18:09:57 +0800510
Karl Beldan5de27b62008-11-20 15:39:31 +0100511#define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
512 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
513
Eric Miao6335d052009-03-03 09:41:00 +0800514static struct snd_soc_dai_ops ssm2602_dai_ops = {
515 .startup = ssm2602_startup,
516 .prepare = ssm2602_pcm_prepare,
517 .hw_params = ssm2602_hw_params,
518 .shutdown = ssm2602_shutdown,
519 .digital_mute = ssm2602_mute,
520 .set_sysclk = ssm2602_set_dai_sysclk,
521 .set_fmt = ssm2602_set_dai_fmt,
522};
523
Cliff Caib7138212008-09-05 18:09:57 +0800524struct snd_soc_dai ssm2602_dai = {
525 .name = "SSM2602",
526 .playback = {
527 .stream_name = "Playback",
528 .channels_min = 2,
529 .channels_max = 2,
530 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100531 .formats = SSM2602_FORMATS,},
Cliff Caib7138212008-09-05 18:09:57 +0800532 .capture = {
533 .stream_name = "Capture",
534 .channels_min = 2,
535 .channels_max = 2,
536 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100537 .formats = SSM2602_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800538 .ops = &ssm2602_dai_ops,
Cliff Caib7138212008-09-05 18:09:57 +0800539};
540EXPORT_SYMBOL_GPL(ssm2602_dai);
541
542static int ssm2602_suspend(struct platform_device *pdev, pm_message_t state)
543{
544 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000545 struct snd_soc_codec *codec = socdev->card->codec;
Cliff Caib7138212008-09-05 18:09:57 +0800546
547 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
548 return 0;
549}
550
551static int ssm2602_resume(struct platform_device *pdev)
552{
553 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000554 struct snd_soc_codec *codec = socdev->card->codec;
Cliff Caib7138212008-09-05 18:09:57 +0800555 int i;
556 u8 data[2];
557 u16 *cache = codec->reg_cache;
558
559 /* Sync reg_cache with the hardware */
560 for (i = 0; i < ARRAY_SIZE(ssm2602_reg); i++) {
561 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
562 data[1] = cache[i] & 0x00ff;
563 codec->hw_write(codec->control_data, data, 2);
564 }
565 ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
566 ssm2602_set_bias_level(codec, codec->suspend_bias_level);
567 return 0;
568}
569
570/*
571 * initialise the ssm2602 driver
572 * register the mixer and dsp interfaces with the kernel
573 */
574static int ssm2602_init(struct snd_soc_device *socdev)
575{
Mark Brown6627a652009-01-23 22:55:23 +0000576 struct snd_soc_codec *codec = socdev->card->codec;
Cliff Caib7138212008-09-05 18:09:57 +0800577 int reg, ret = 0;
578
579 codec->name = "SSM2602";
580 codec->owner = THIS_MODULE;
581 codec->read = ssm2602_read_reg_cache;
582 codec->write = ssm2602_write;
583 codec->set_bias_level = ssm2602_set_bias_level;
584 codec->dai = &ssm2602_dai;
585 codec->num_dai = 1;
586 codec->reg_cache_size = sizeof(ssm2602_reg);
587 codec->reg_cache = kmemdup(ssm2602_reg, sizeof(ssm2602_reg),
588 GFP_KERNEL);
589 if (codec->reg_cache == NULL)
590 return -ENOMEM;
591
592 ssm2602_reset(codec);
593
594 /* register pcms */
595 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
596 if (ret < 0) {
597 pr_err("ssm2602: failed to create pcms\n");
598 goto pcm_err;
599 }
600 /*power on device*/
601 ssm2602_write(codec, SSM2602_ACTIVE, 0);
602 /* set the update bits */
603 reg = ssm2602_read_reg_cache(codec, SSM2602_LINVOL);
604 ssm2602_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
605 reg = ssm2602_read_reg_cache(codec, SSM2602_RINVOL);
606 ssm2602_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
607 reg = ssm2602_read_reg_cache(codec, SSM2602_LOUT1V);
608 ssm2602_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
609 reg = ssm2602_read_reg_cache(codec, SSM2602_ROUT1V);
610 ssm2602_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
611 /*select Line in as default input*/
612 ssm2602_write(codec, SSM2602_APANA,
613 APANA_ENABLE_MIC_BOOST2 | APANA_SELECT_DAC |
614 APANA_ENABLE_MIC_BOOST);
615 ssm2602_write(codec, SSM2602_PWR, 0);
616
Ian Molton3e8e1952009-01-09 00:23:21 +0000617 snd_soc_add_controls(codec, ssm2602_snd_controls,
618 ARRAY_SIZE(ssm2602_snd_controls));
Cliff Caib7138212008-09-05 18:09:57 +0800619 ssm2602_add_widgets(codec);
Mark Brown968a6022008-11-28 11:49:07 +0000620 ret = snd_soc_init_card(socdev);
Cliff Caib7138212008-09-05 18:09:57 +0800621 if (ret < 0) {
622 pr_err("ssm2602: failed to register card\n");
623 goto card_err;
624 }
625
626 return ret;
627
628card_err:
629 snd_soc_free_pcms(socdev);
630 snd_soc_dapm_free(socdev);
631pcm_err:
632 kfree(codec->reg_cache);
633 return ret;
634}
635
636static struct snd_soc_device *ssm2602_socdev;
637
638#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
639/*
640 * ssm2602 2 wire address is determined by GPIO5
641 * state during powerup.
642 * low = 0x1a
643 * high = 0x1b
644 */
645static int ssm2602_i2c_probe(struct i2c_client *i2c,
646 const struct i2c_device_id *id)
647{
648 struct snd_soc_device *socdev = ssm2602_socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000649 struct snd_soc_codec *codec = socdev->card->codec;
Cliff Caib7138212008-09-05 18:09:57 +0800650 int ret;
651
652 i2c_set_clientdata(i2c, codec);
653 codec->control_data = i2c;
654
655 ret = ssm2602_init(socdev);
656 if (ret < 0)
657 pr_err("failed to initialise SSM2602\n");
658
659 return ret;
660}
661
662static int ssm2602_i2c_remove(struct i2c_client *client)
663{
664 struct snd_soc_codec *codec = i2c_get_clientdata(client);
665 kfree(codec->reg_cache);
666 return 0;
667}
668
669static const struct i2c_device_id ssm2602_i2c_id[] = {
670 { "ssm2602", 0 },
671 { }
672};
673MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
674/* corgi i2c codec control layer */
675static struct i2c_driver ssm2602_i2c_driver = {
676 .driver = {
677 .name = "SSM2602 I2C Codec",
678 .owner = THIS_MODULE,
679 },
680 .probe = ssm2602_i2c_probe,
681 .remove = ssm2602_i2c_remove,
682 .id_table = ssm2602_i2c_id,
683};
684
685static int ssm2602_add_i2c_device(struct platform_device *pdev,
686 const struct ssm2602_setup_data *setup)
687{
688 struct i2c_board_info info;
689 struct i2c_adapter *adapter;
690 struct i2c_client *client;
691 int ret;
692
693 ret = i2c_add_driver(&ssm2602_i2c_driver);
694 if (ret != 0) {
695 dev_err(&pdev->dev, "can't add i2c driver\n");
696 return ret;
697 }
698 memset(&info, 0, sizeof(struct i2c_board_info));
699 info.addr = setup->i2c_address;
700 strlcpy(info.type, "ssm2602", I2C_NAME_SIZE);
701 adapter = i2c_get_adapter(setup->i2c_bus);
702 if (!adapter) {
703 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
704 setup->i2c_bus);
705 goto err_driver;
706 }
707 client = i2c_new_device(adapter, &info);
708 i2c_put_adapter(adapter);
709 if (!client) {
710 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
711 (unsigned int)info.addr);
712 goto err_driver;
713 }
714 return 0;
715err_driver:
716 i2c_del_driver(&ssm2602_i2c_driver);
717 return -ENODEV;
718}
719#endif
720
721static int ssm2602_probe(struct platform_device *pdev)
722{
723 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
724 struct ssm2602_setup_data *setup;
725 struct snd_soc_codec *codec;
726 struct ssm2602_priv *ssm2602;
727 int ret = 0;
728
729 pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
730
731 setup = socdev->codec_data;
732 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
733 if (codec == NULL)
734 return -ENOMEM;
735
736 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
737 if (ssm2602 == NULL) {
738 kfree(codec);
739 return -ENOMEM;
740 }
741
742 codec->private_data = ssm2602;
Mark Brown6627a652009-01-23 22:55:23 +0000743 socdev->card->codec = codec;
Cliff Caib7138212008-09-05 18:09:57 +0800744 mutex_init(&codec->mutex);
745 INIT_LIST_HEAD(&codec->dapm_widgets);
746 INIT_LIST_HEAD(&codec->dapm_paths);
747
748 ssm2602_socdev = socdev;
749#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
750 if (setup->i2c_address) {
751 codec->hw_write = (hw_write_t)i2c_master_send;
752 ret = ssm2602_add_i2c_device(pdev, setup);
753 }
754#else
755 /* other interfaces */
756#endif
757 return ret;
758}
759
760/* remove everything here */
761static int ssm2602_remove(struct platform_device *pdev)
762{
763 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000764 struct snd_soc_codec *codec = socdev->card->codec;
Cliff Caib7138212008-09-05 18:09:57 +0800765
766 if (codec->control_data)
767 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
768
769 snd_soc_free_pcms(socdev);
770 snd_soc_dapm_free(socdev);
771#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
772 i2c_unregister_device(codec->control_data);
773 i2c_del_driver(&ssm2602_i2c_driver);
774#endif
775 kfree(codec->private_data);
776 kfree(codec);
777
778 return 0;
779}
780
781struct snd_soc_codec_device soc_codec_dev_ssm2602 = {
782 .probe = ssm2602_probe,
783 .remove = ssm2602_remove,
784 .suspend = ssm2602_suspend,
785 .resume = ssm2602_resume,
786};
787EXPORT_SYMBOL_GPL(soc_codec_dev_ssm2602);
788
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100789static int __init ssm2602_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000790{
791 return snd_soc_register_dai(&ssm2602_dai);
792}
793module_init(ssm2602_modinit);
794
795static void __exit ssm2602_exit(void)
796{
797 snd_soc_unregister_dai(&ssm2602_dai);
798}
799module_exit(ssm2602_exit);
800
Cliff Caib7138212008-09-05 18:09:57 +0800801MODULE_DESCRIPTION("ASoC ssm2602 driver");
802MODULE_AUTHOR("Cliff Cai");
803MODULE_LICENSE("GPL");