blob: 7e2194975360ed357daad656ed2387c1c60108b6 [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>
Mike Frysingerb39e2852011-04-07 02:05:11 -040035#include <linux/spi/spi.h>
Cliff Caib7138212008-09-05 18:09:57 +080036#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Cliff Caib7138212008-09-05 18:09:57 +080038#include <sound/core.h>
39#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/soc.h>
Cliff Caib7138212008-09-05 18:09:57 +080042#include <sound/initval.h>
43
44#include "ssm2602.h"
45
Cliff Caib7138212008-09-05 18:09:57 +080046#define SSM2602_VERSION "0.1"
47
Cliff Caib7138212008-09-05 18:09:57 +080048/* codec private data */
49struct ssm2602_priv {
50 unsigned int sysclk;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000051 enum snd_soc_control_type control_type;
Cliff Caib7138212008-09-05 18:09:57 +080052 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
Cliff Cai93547e82011-03-27 17:22:57 -040068#define ssm2602_reset(c) snd_soc_write(c, SSM2602_RESET, 0)
Cliff Caib7138212008-09-05 18:09:57 +080069
70/*Appending several "None"s just for OSS mixer use*/
71static const char *ssm2602_input_select[] = {
72 "Line", "Mic", "None", "None", "None",
73 "None", "None", "None",
74};
75
76static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
77
78static const struct soc_enum ssm2602_enum[] = {
79 SOC_ENUM_SINGLE(SSM2602_APANA, 2, 2, ssm2602_input_select),
80 SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, 4, ssm2602_deemph),
81};
82
83static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
84
85SOC_DOUBLE_R("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
86 0, 127, 0),
87SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
88 7, 1, 0),
89
90SOC_DOUBLE_R("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 31, 0),
91SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
92
93SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
Cliff Cai4b527e22010-03-09 12:58:09 -050094SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 7, 1, 0),
Cliff Caib7138212008-09-05 18:09:57 +080095SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
96
97SOC_SINGLE("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1),
98
99SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
100SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
101
102SOC_ENUM("Capture Source", ssm2602_enum[0]),
103
104SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
105};
106
Cliff Caib7138212008-09-05 18:09:57 +0800107/* Output Mixer */
108static const struct snd_kcontrol_new ssm2602_output_mixer_controls[] = {
109SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
110SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
111SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
112};
113
114/* Input mux */
115static const struct snd_kcontrol_new ssm2602_input_mux_controls =
116SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
117
118static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
119SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
120 &ssm2602_output_mixer_controls[0],
121 ARRAY_SIZE(ssm2602_output_mixer_controls)),
122SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
123SND_SOC_DAPM_OUTPUT("LOUT"),
124SND_SOC_DAPM_OUTPUT("LHPOUT"),
125SND_SOC_DAPM_OUTPUT("ROUT"),
126SND_SOC_DAPM_OUTPUT("RHPOUT"),
127SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
128SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
129SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
130SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
131SND_SOC_DAPM_INPUT("MICIN"),
132SND_SOC_DAPM_INPUT("RLINEIN"),
133SND_SOC_DAPM_INPUT("LLINEIN"),
134};
135
136static const struct snd_soc_dapm_route audio_conn[] = {
137 /* output mixer */
138 {"Output Mixer", "Line Bypass Switch", "Line Input"},
139 {"Output Mixer", "HiFi Playback Switch", "DAC"},
140 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
141
142 /* outputs */
143 {"RHPOUT", NULL, "Output Mixer"},
144 {"ROUT", NULL, "Output Mixer"},
145 {"LHPOUT", NULL, "Output Mixer"},
146 {"LOUT", NULL, "Output Mixer"},
147
148 /* input mux */
149 {"Input Mux", "Line", "Line Input"},
150 {"Input Mux", "Mic", "Mic Bias"},
151 {"ADC", NULL, "Input Mux"},
152
153 /* inputs */
154 {"Line Input", NULL, "LLINEIN"},
155 {"Line Input", NULL, "RLINEIN"},
156 {"Mic Bias", NULL, "MICIN"},
157};
158
159static int ssm2602_add_widgets(struct snd_soc_codec *codec)
160{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200161 struct snd_soc_dapm_context *dapm = &codec->dapm;
Cliff Caib7138212008-09-05 18:09:57 +0800162
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200163 snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
164 ARRAY_SIZE(ssm2602_dapm_widgets));
165 snd_soc_dapm_add_routes(dapm, audio_conn, ARRAY_SIZE(audio_conn));
Cliff Caib7138212008-09-05 18:09:57 +0800166
Cliff Caib7138212008-09-05 18:09:57 +0800167 return 0;
168}
169
170struct _coeff_div {
171 u32 mclk;
172 u32 rate;
173 u16 fs;
174 u8 sr:4;
175 u8 bosr:1;
176 u8 usb:1;
177};
178
179/* codec mclk clock divider coefficients */
180static const struct _coeff_div coeff_div[] = {
181 /* 48k */
182 {12288000, 48000, 256, 0x0, 0x0, 0x0},
183 {18432000, 48000, 384, 0x0, 0x1, 0x0},
184 {12000000, 48000, 250, 0x0, 0x0, 0x1},
185
186 /* 32k */
187 {12288000, 32000, 384, 0x6, 0x0, 0x0},
188 {18432000, 32000, 576, 0x6, 0x1, 0x0},
189 {12000000, 32000, 375, 0x6, 0x0, 0x1},
190
191 /* 8k */
192 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
193 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
194 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
195 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
196 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
197
198 /* 96k */
199 {12288000, 96000, 128, 0x7, 0x0, 0x0},
200 {18432000, 96000, 192, 0x7, 0x1, 0x0},
201 {12000000, 96000, 125, 0x7, 0x0, 0x1},
202
203 /* 44.1k */
204 {11289600, 44100, 256, 0x8, 0x0, 0x0},
205 {16934400, 44100, 384, 0x8, 0x1, 0x0},
206 {12000000, 44100, 272, 0x8, 0x1, 0x1},
207
208 /* 88.2k */
209 {11289600, 88200, 128, 0xf, 0x0, 0x0},
210 {16934400, 88200, 192, 0xf, 0x1, 0x0},
211 {12000000, 88200, 136, 0xf, 0x1, 0x1},
212};
213
214static inline int get_coeff(int mclk, int rate)
215{
216 int i;
217
218 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
219 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
220 return i;
221 }
222 return i;
223}
224
225static int ssm2602_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000226 struct snd_pcm_hw_params *params,
227 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800228{
229 u16 srate;
230 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000231 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900232 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Cai93547e82011-03-27 17:22:57 -0400233 u16 iface = snd_soc_read(codec, SSM2602_IFACE) & 0xfff3;
Cliff Caib7138212008-09-05 18:09:57 +0800234 int i = get_coeff(ssm2602->sysclk, params_rate(params));
235
Karl Beldanfaab5a32008-11-20 15:39:27 +0100236 if (substream == ssm2602->slave_substream) {
Cliff Cai93547e82011-03-27 17:22:57 -0400237 dev_dbg(codec->dev, "Ignoring hw_params for slave substream\n");
Karl Beldanfaab5a32008-11-20 15:39:27 +0100238 return 0;
239 }
240
Cliff Caib7138212008-09-05 18:09:57 +0800241 /*no match is found*/
242 if (i == ARRAY_SIZE(coeff_div))
243 return -EINVAL;
244
245 srate = (coeff_div[i].sr << 2) |
246 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
247
Cliff Cai93547e82011-03-27 17:22:57 -0400248 snd_soc_write(codec, SSM2602_ACTIVE, 0);
249 snd_soc_write(codec, SSM2602_SRATE, srate);
Cliff Caib7138212008-09-05 18:09:57 +0800250
251 /* bit size */
252 switch (params_format(params)) {
253 case SNDRV_PCM_FORMAT_S16_LE:
254 break;
255 case SNDRV_PCM_FORMAT_S20_3LE:
256 iface |= 0x0004;
257 break;
258 case SNDRV_PCM_FORMAT_S24_LE:
259 iface |= 0x0008;
260 break;
261 case SNDRV_PCM_FORMAT_S32_LE:
262 iface |= 0x000c;
263 break;
264 }
Cliff Cai93547e82011-03-27 17:22:57 -0400265 snd_soc_write(codec, SSM2602_IFACE, iface);
266 snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
Cliff Caib7138212008-09-05 18:09:57 +0800267 return 0;
268}
269
Mark Browndee89c42008-11-18 22:11:38 +0000270static int ssm2602_startup(struct snd_pcm_substream *substream,
271 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800272{
273 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000274 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900275 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100276 struct i2c_client *i2c = codec->control_data;
Cliff Caib7138212008-09-05 18:09:57 +0800277 struct snd_pcm_runtime *master_runtime;
278
279 /* The DAI has shared clocks so if we already have a playback or
280 * capture going then constrain this substream to match it.
Karl Beldanfaab5a32008-11-20 15:39:27 +0100281 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
Cliff Caib7138212008-09-05 18:09:57 +0800282 */
283 if (ssm2602->master_substream) {
284 master_runtime = ssm2602->master_substream->runtime;
Karl Beldanfaab5a32008-11-20 15:39:27 +0100285 dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
286 master_runtime->sample_bits,
287 master_runtime->rate);
288
Cliff Caif692fce2009-06-02 00:18:54 -0400289 if (master_runtime->rate != 0)
290 snd_pcm_hw_constraint_minmax(substream->runtime,
291 SNDRV_PCM_HW_PARAM_RATE,
292 master_runtime->rate,
293 master_runtime->rate);
Cliff Caib7138212008-09-05 18:09:57 +0800294
Cliff Caif692fce2009-06-02 00:18:54 -0400295 if (master_runtime->sample_bits != 0)
296 snd_pcm_hw_constraint_minmax(substream->runtime,
297 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
298 master_runtime->sample_bits,
299 master_runtime->sample_bits);
Cliff Caib7138212008-09-05 18:09:57 +0800300
301 ssm2602->slave_substream = substream;
302 } else
303 ssm2602->master_substream = substream;
304
305 return 0;
306}
307
Mark Browndee89c42008-11-18 22:11:38 +0000308static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream,
309 struct snd_soc_dai *dai)
Cliff Caib7138212008-09-05 18:09:57 +0800310{
311 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000312 struct snd_soc_codec *codec = rtd->codec;
Cliff Caib7138212008-09-05 18:09:57 +0800313 /* set active */
Cliff Cai93547e82011-03-27 17:22:57 -0400314 snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
Cliff Caib7138212008-09-05 18:09:57 +0800315
316 return 0;
317}
318
Mark Browndee89c42008-11-18 22:11:38 +0000319static void ssm2602_shutdown(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;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000323 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900324 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caif692fce2009-06-02 00:18:54 -0400325
Cliff Caib7138212008-09-05 18:09:57 +0800326 /* deactivate */
327 if (!codec->active)
Cliff Cai93547e82011-03-27 17:22:57 -0400328 snd_soc_write(codec, SSM2602_ACTIVE, 0);
Karl Beldanfaab5a32008-11-20 15:39:27 +0100329
330 if (ssm2602->master_substream == substream)
331 ssm2602->master_substream = ssm2602->slave_substream;
332
333 ssm2602->slave_substream = NULL;
Cliff Caib7138212008-09-05 18:09:57 +0800334}
335
336static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
337{
338 struct snd_soc_codec *codec = dai->codec;
Cliff Cai93547e82011-03-27 17:22:57 -0400339 u16 mute_reg = snd_soc_read(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE;
Cliff Caib7138212008-09-05 18:09:57 +0800340 if (mute)
Cliff Cai93547e82011-03-27 17:22:57 -0400341 snd_soc_write(codec, SSM2602_APDIGI,
Cliff Caib7138212008-09-05 18:09:57 +0800342 mute_reg | APDIGI_ENABLE_DAC_MUTE);
343 else
Cliff Cai93547e82011-03-27 17:22:57 -0400344 snd_soc_write(codec, SSM2602_APDIGI, mute_reg);
Cliff Caib7138212008-09-05 18:09:57 +0800345 return 0;
346}
347
348static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
349 int clk_id, unsigned int freq, int dir)
350{
351 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900352 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800353 switch (freq) {
354 case 11289600:
355 case 12000000:
356 case 12288000:
357 case 16934400:
358 case 18432000:
359 ssm2602->sysclk = freq;
360 return 0;
361 }
362 return -EINVAL;
363}
364
365static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
366 unsigned int fmt)
367{
368 struct snd_soc_codec *codec = codec_dai->codec;
369 u16 iface = 0;
370
371 /* set master/slave audio interface */
372 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
373 case SND_SOC_DAIFMT_CBM_CFM:
374 iface |= 0x0040;
375 break;
376 case SND_SOC_DAIFMT_CBS_CFS:
377 break;
378 default:
379 return -EINVAL;
380 }
381
382 /* interface format */
383 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
384 case SND_SOC_DAIFMT_I2S:
385 iface |= 0x0002;
386 break;
387 case SND_SOC_DAIFMT_RIGHT_J:
388 break;
389 case SND_SOC_DAIFMT_LEFT_J:
390 iface |= 0x0001;
391 break;
392 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200393 iface |= 0x0013;
Cliff Caib7138212008-09-05 18:09:57 +0800394 break;
395 case SND_SOC_DAIFMT_DSP_B:
Jarkko Nikulac6913482008-12-22 10:57:33 +0200396 iface |= 0x0003;
Cliff Caib7138212008-09-05 18:09:57 +0800397 break;
398 default:
399 return -EINVAL;
400 }
401
402 /* clock inversion */
403 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
404 case SND_SOC_DAIFMT_NB_NF:
405 break;
406 case SND_SOC_DAIFMT_IB_IF:
407 iface |= 0x0090;
408 break;
409 case SND_SOC_DAIFMT_IB_NF:
410 iface |= 0x0080;
411 break;
412 case SND_SOC_DAIFMT_NB_IF:
413 iface |= 0x0010;
414 break;
415 default:
416 return -EINVAL;
417 }
418
419 /* set iface */
Cliff Cai93547e82011-03-27 17:22:57 -0400420 snd_soc_write(codec, SSM2602_IFACE, iface);
Cliff Caib7138212008-09-05 18:09:57 +0800421 return 0;
422}
423
424static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
425 enum snd_soc_bias_level level)
426{
Cliff Cai93547e82011-03-27 17:22:57 -0400427 u16 reg = snd_soc_read(codec, SSM2602_PWR) & 0xff7f;
Cliff Caib7138212008-09-05 18:09:57 +0800428
429 switch (level) {
430 case SND_SOC_BIAS_ON:
431 /* vref/mid, osc on, dac unmute */
Cliff Cai93547e82011-03-27 17:22:57 -0400432 snd_soc_write(codec, SSM2602_PWR, reg);
Cliff Caib7138212008-09-05 18:09:57 +0800433 break;
434 case SND_SOC_BIAS_PREPARE:
435 break;
436 case SND_SOC_BIAS_STANDBY:
437 /* everything off except vref/vmid, */
Cliff Cai93547e82011-03-27 17:22:57 -0400438 snd_soc_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN);
Cliff Caib7138212008-09-05 18:09:57 +0800439 break;
440 case SND_SOC_BIAS_OFF:
441 /* everything off, dac mute, inactive */
Cliff Cai93547e82011-03-27 17:22:57 -0400442 snd_soc_write(codec, SSM2602_ACTIVE, 0);
443 snd_soc_write(codec, SSM2602_PWR, 0xffff);
Cliff Caib7138212008-09-05 18:09:57 +0800444 break;
445
446 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200447 codec->dapm.bias_level = level;
Cliff Caib7138212008-09-05 18:09:57 +0800448 return 0;
449}
450
Cliff Cai2552a712009-06-02 00:18:53 -0400451#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
452 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
453 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
Cliff Caib7138212008-09-05 18:09:57 +0800454
Karl Beldan5de27b62008-11-20 15:39:31 +0100455#define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
456 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
457
Eric Miao6335d052009-03-03 09:41:00 +0800458static struct snd_soc_dai_ops ssm2602_dai_ops = {
459 .startup = ssm2602_startup,
460 .prepare = ssm2602_pcm_prepare,
461 .hw_params = ssm2602_hw_params,
462 .shutdown = ssm2602_shutdown,
463 .digital_mute = ssm2602_mute,
464 .set_sysclk = ssm2602_set_dai_sysclk,
465 .set_fmt = ssm2602_set_dai_fmt,
466};
467
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000468static struct snd_soc_dai_driver ssm2602_dai = {
469 .name = "ssm2602-hifi",
Cliff Caib7138212008-09-05 18:09:57 +0800470 .playback = {
471 .stream_name = "Playback",
472 .channels_min = 2,
473 .channels_max = 2,
474 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100475 .formats = SSM2602_FORMATS,},
Cliff Caib7138212008-09-05 18:09:57 +0800476 .capture = {
477 .stream_name = "Capture",
478 .channels_min = 2,
479 .channels_max = 2,
480 .rates = SSM2602_RATES,
Karl Beldan5de27b62008-11-20 15:39:31 +0100481 .formats = SSM2602_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800482 .ops = &ssm2602_dai_ops,
Cliff Caib7138212008-09-05 18:09:57 +0800483};
Cliff Caib7138212008-09-05 18:09:57 +0800484
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000485static int ssm2602_suspend(struct snd_soc_codec *codec, pm_message_t state)
Cliff Caib7138212008-09-05 18:09:57 +0800486{
Cliff Caib7138212008-09-05 18:09:57 +0800487 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
488 return 0;
489}
490
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000491static int ssm2602_resume(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800492{
Cliff Cai93547e82011-03-27 17:22:57 -0400493 snd_soc_cache_sync(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800494
Cliff Caib7138212008-09-05 18:09:57 +0800495 ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Cliff Cai93547e82011-03-27 17:22:57 -0400496
Cliff Caib7138212008-09-05 18:09:57 +0800497 return 0;
498}
499
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000500static int ssm2602_probe(struct snd_soc_codec *codec)
Cliff Caib7138212008-09-05 18:09:57 +0800501{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000502 struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
503 int ret = 0, reg;
Cliff Caib7138212008-09-05 18:09:57 +0800504
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000505 pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
506
Cliff Cai93547e82011-03-27 17:22:57 -0400507 ret = snd_soc_codec_set_cache_io(codec, 7, 9, ssm2602->control_type);
508 if (ret < 0) {
509 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
510 return ret;
511 }
Cliff Caib7138212008-09-05 18:09:57 +0800512
Cliff Cai93547e82011-03-27 17:22:57 -0400513 ret = ssm2602_reset(codec);
514 if (ret < 0) {
515 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
516 return ret;
517 }
Cliff Caib7138212008-09-05 18:09:57 +0800518
Cliff Caib7138212008-09-05 18:09:57 +0800519 /*power on device*/
Cliff Cai93547e82011-03-27 17:22:57 -0400520 snd_soc_write(codec, SSM2602_ACTIVE, 0);
Cliff Caib7138212008-09-05 18:09:57 +0800521 /* set the update bits */
Cliff Cai93547e82011-03-27 17:22:57 -0400522 reg = snd_soc_read(codec, SSM2602_LINVOL);
523 snd_soc_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
524 reg = snd_soc_read(codec, SSM2602_RINVOL);
525 snd_soc_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
526 reg = snd_soc_read(codec, SSM2602_LOUT1V);
527 snd_soc_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
528 reg = snd_soc_read(codec, SSM2602_ROUT1V);
529 snd_soc_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
Cliff Caib7138212008-09-05 18:09:57 +0800530 /*select Line in as default input*/
Cliff Cai93547e82011-03-27 17:22:57 -0400531 snd_soc_write(codec, SSM2602_APANA, APANA_SELECT_DAC |
Cliff Caib7138212008-09-05 18:09:57 +0800532 APANA_ENABLE_MIC_BOOST);
Cliff Cai93547e82011-03-27 17:22:57 -0400533 snd_soc_write(codec, SSM2602_PWR, 0);
Cliff Caib7138212008-09-05 18:09:57 +0800534
Ian Molton3e8e1952009-01-09 00:23:21 +0000535 snd_soc_add_controls(codec, ssm2602_snd_controls,
536 ARRAY_SIZE(ssm2602_snd_controls));
Cliff Caib7138212008-09-05 18:09:57 +0800537 ssm2602_add_widgets(codec);
Cliff Caib7138212008-09-05 18:09:57 +0800538
Cliff Cai93547e82011-03-27 17:22:57 -0400539 return 0;
Cliff Caib7138212008-09-05 18:09:57 +0800540}
541
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000542/* remove everything here */
543static int ssm2602_remove(struct snd_soc_codec *codec)
544{
545 ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
546 return 0;
547}
548
549static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = {
550 .probe = ssm2602_probe,
551 .remove = ssm2602_remove,
552 .suspend = ssm2602_suspend,
553 .resume = ssm2602_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 .set_bias_level = ssm2602_set_bias_level,
555 .reg_cache_size = sizeof(ssm2602_reg),
556 .reg_word_size = sizeof(u16),
557 .reg_cache_default = ssm2602_reg,
558};
Cliff Caib7138212008-09-05 18:09:57 +0800559
Mike Frysingerb39e2852011-04-07 02:05:11 -0400560#if defined(CONFIG_SPI_MASTER)
561static int __devinit ssm2602_spi_probe(struct spi_device *spi)
562{
563 struct ssm2602_priv *ssm2602;
564 int ret;
565
566 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
567 if (ssm2602 == NULL)
568 return -ENOMEM;
569
570 spi_set_drvdata(spi, ssm2602);
571 ssm2602->control_type = SND_SOC_SPI;
572
573 ret = snd_soc_register_codec(&spi->dev,
574 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
575 if (ret < 0)
576 kfree(ssm2602);
577 return ret;
578}
579
580static int __devexit ssm2602_spi_remove(struct spi_device *spi)
581{
582 snd_soc_unregister_codec(&spi->dev);
583 kfree(spi_get_drvdata(spi));
584 return 0;
585}
586
587static struct spi_driver ssm2602_spi_driver = {
588 .driver = {
589 .name = "ssm2602",
590 .owner = THIS_MODULE,
591 },
592 .probe = ssm2602_spi_probe,
593 .remove = __devexit_p(ssm2602_spi_remove),
594};
595#endif
596
Cliff Caib7138212008-09-05 18:09:57 +0800597#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
598/*
599 * ssm2602 2 wire address is determined by GPIO5
600 * state during powerup.
601 * low = 0x1a
602 * high = 0x1b
603 */
604static int ssm2602_i2c_probe(struct i2c_client *i2c,
605 const struct i2c_device_id *id)
606{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000607 struct ssm2602_priv *ssm2602;
Cliff Caib7138212008-09-05 18:09:57 +0800608 int ret;
609
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
611 if (ssm2602 == NULL)
612 return -ENOMEM;
Cliff Caib7138212008-09-05 18:09:57 +0800613
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000614 i2c_set_clientdata(i2c, ssm2602);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000615 ssm2602->control_type = SND_SOC_I2C;
616
617 ret = snd_soc_register_codec(&i2c->dev,
618 &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
Cliff Caib7138212008-09-05 18:09:57 +0800619 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 kfree(ssm2602);
Cliff Caib7138212008-09-05 18:09:57 +0800621 return ret;
622}
623
624static int ssm2602_i2c_remove(struct i2c_client *client)
625{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626 snd_soc_unregister_codec(&client->dev);
627 kfree(i2c_get_clientdata(client));
Cliff Caib7138212008-09-05 18:09:57 +0800628 return 0;
629}
630
631static const struct i2c_device_id ssm2602_i2c_id[] = {
632 { "ssm2602", 0 },
633 { }
634};
635MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000636
Cliff Caib7138212008-09-05 18:09:57 +0800637/* corgi i2c codec control layer */
638static struct i2c_driver ssm2602_i2c_driver = {
639 .driver = {
Mike Frysinger2a161012011-03-27 00:44:10 -0400640 .name = "ssm2602",
Cliff Caib7138212008-09-05 18:09:57 +0800641 .owner = THIS_MODULE,
642 },
643 .probe = ssm2602_i2c_probe,
644 .remove = ssm2602_i2c_remove,
645 .id_table = ssm2602_i2c_id,
646};
Cliff Caib7138212008-09-05 18:09:57 +0800647#endif
648
Cliff Caib7138212008-09-05 18:09:57 +0800649
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100650static int __init ssm2602_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000651{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000652 int ret = 0;
Mike Frysingerb39e2852011-04-07 02:05:11 -0400653
654#if defined(CONFIG_SPI_MASTER)
655 ret = spi_register_driver(&ssm2602_spi_driver);
656 if (ret)
657 return ret;
658#endif
659
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
661 ret = i2c_add_driver(&ssm2602_i2c_driver);
Mike Frysingerb39e2852011-04-07 02:05:11 -0400662 if (ret)
663 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000664#endif
Mike Frysingerb39e2852011-04-07 02:05:11 -0400665
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000666 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000667}
668module_init(ssm2602_modinit);
669
670static void __exit ssm2602_exit(void)
671{
Mike Frysingerb39e2852011-04-07 02:05:11 -0400672#if defined(CONFIG_SPI_MASTER)
673 spi_unregister_driver(&ssm2602_spi_driver);
674#endif
675
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000676#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
677 i2c_del_driver(&ssm2602_i2c_driver);
678#endif
Mark Brown64089b82008-12-08 19:17:58 +0000679}
680module_exit(ssm2602_exit);
681
Cliff Caib7138212008-09-05 18:09:57 +0800682MODULE_DESCRIPTION("ASoC ssm2602 driver");
683MODULE_AUTHOR("Cliff Cai");
684MODULE_LICENSE("GPL");