e1ebf3d04935b37991e603deb290f7edf5a8fa17
[firefly-linux-kernel-4.4.55.git] / sound / soc / codecs / ssm2602.c
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/spi/spi.h>
36 #include <linux/platform_device.h>
37 #include <linux/slab.h>
38 #include <sound/core.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/soc.h>
42 #include <sound/initval.h>
43
44 #include "ssm2602.h"
45
46 #define SSM2602_VERSION "0.1"
47
48 /* codec private data */
49 struct ssm2602_priv {
50         unsigned int sysclk;
51         enum snd_soc_control_type control_type;
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  */
62 static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = {
63         0x0097, 0x0097, 0x0079, 0x0079,
64         0x000a, 0x0008, 0x009f, 0x000a,
65         0x0000, 0x0000
66 };
67
68 #define ssm2602_reset(c)        snd_soc_write(c, SSM2602_RESET, 0)
69
70 /*Appending several "None"s just for OSS mixer use*/
71 static const char *ssm2602_input_select[] = {
72         "Line", "Mic", "None", "None", "None",
73         "None", "None", "None",
74 };
75
76 static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
77
78 static 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
83 static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
84
85 SOC_DOUBLE_R("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
86         0, 127, 0),
87 SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
88         7, 1, 0),
89
90 SOC_DOUBLE_R("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 31, 0),
91 SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
92
93 SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
94 SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 8, 1, 0),
95 SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
96
97 SOC_SINGLE("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1),
98
99 SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
100 SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
101
102 SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
103 };
104
105 /* Output Mixer */
106 static const struct snd_kcontrol_new ssm2602_output_mixer_controls[] = {
107 SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
108 SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
109 SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
110 };
111
112 /* Input mux */
113 static const struct snd_kcontrol_new ssm2602_input_mux_controls =
114 SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
115
116 static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
117 SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
118         &ssm2602_output_mixer_controls[0],
119         ARRAY_SIZE(ssm2602_output_mixer_controls)),
120 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
121 SND_SOC_DAPM_OUTPUT("LOUT"),
122 SND_SOC_DAPM_OUTPUT("LHPOUT"),
123 SND_SOC_DAPM_OUTPUT("ROUT"),
124 SND_SOC_DAPM_OUTPUT("RHPOUT"),
125 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
126 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
127 SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
128 SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
129 SND_SOC_DAPM_INPUT("MICIN"),
130 SND_SOC_DAPM_INPUT("RLINEIN"),
131 SND_SOC_DAPM_INPUT("LLINEIN"),
132 };
133
134 static const struct snd_soc_dapm_route audio_conn[] = {
135         /* output mixer */
136         {"Output Mixer", "Line Bypass Switch", "Line Input"},
137         {"Output Mixer", "HiFi Playback Switch", "DAC"},
138         {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
139
140         /* outputs */
141         {"RHPOUT", NULL, "Output Mixer"},
142         {"ROUT", NULL, "Output Mixer"},
143         {"LHPOUT", NULL, "Output Mixer"},
144         {"LOUT", NULL, "Output Mixer"},
145
146         /* input mux */
147         {"Input Mux", "Line", "Line Input"},
148         {"Input Mux", "Mic", "Mic Bias"},
149         {"ADC", NULL, "Input Mux"},
150
151         /* inputs */
152         {"Line Input", NULL, "LLINEIN"},
153         {"Line Input", NULL, "RLINEIN"},
154         {"Mic Bias", NULL, "MICIN"},
155 };
156
157 static int ssm2602_add_widgets(struct snd_soc_codec *codec)
158 {
159         struct snd_soc_dapm_context *dapm = &codec->dapm;
160
161         snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
162                                   ARRAY_SIZE(ssm2602_dapm_widgets));
163         snd_soc_dapm_add_routes(dapm, audio_conn, ARRAY_SIZE(audio_conn));
164
165         return 0;
166 }
167
168 struct ssm2602_coeff {
169         u32 mclk;
170         u32 rate;
171         u8 srate;
172 };
173
174 #define SSM2602_COEFF_SRATE(sr, bosr, usb) (((sr) << 2) | ((bosr) << 1) | (usb))
175
176 /* codec mclk clock coefficients */
177 static const struct ssm2602_coeff ssm2602_coeff_table[] = {
178         /* 48k */
179         {12288000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x0)},
180         {18432000, 48000, SSM2602_COEFF_SRATE(0x0, 0x1, 0x0)},
181         {12000000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x1)},
182
183         /* 32k */
184         {12288000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x0)},
185         {18432000, 32000, SSM2602_COEFF_SRATE(0x6, 0x1, 0x0)},
186         {12000000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x1)},
187
188         /* 8k */
189         {12288000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x0)},
190         {18432000, 8000, SSM2602_COEFF_SRATE(0x3, 0x1, 0x0)},
191         {11289600, 8000, SSM2602_COEFF_SRATE(0xb, 0x0, 0x0)},
192         {16934400, 8000, SSM2602_COEFF_SRATE(0xb, 0x1, 0x0)},
193         {12000000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x1)},
194
195         /* 96k */
196         {12288000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x0)},
197         {18432000, 96000, SSM2602_COEFF_SRATE(0x7, 0x1, 0x0)},
198         {12000000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x1)},
199
200         /* 44.1k */
201         {11289600, 44100, SSM2602_COEFF_SRATE(0x8, 0x0, 0x0)},
202         {16934400, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x0)},
203         {12000000, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x1)},
204
205         /* 88.2k */
206         {11289600, 88200, SSM2602_COEFF_SRATE(0xf, 0x0, 0x0)},
207         {16934400, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x0)},
208         {12000000, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x1)},
209 };
210
211 static inline int ssm2602_get_coeff(int mclk, int rate)
212 {
213         int i;
214
215         for (i = 0; i < ARRAY_SIZE(ssm2602_coeff_table); i++) {
216                 if (ssm2602_coeff_table[i].rate == rate &&
217                         ssm2602_coeff_table[i].mclk == mclk)
218                         return ssm2602_coeff_table[i].srate;
219         }
220         return -EINVAL;
221 }
222
223 static int ssm2602_hw_params(struct snd_pcm_substream *substream,
224         struct snd_pcm_hw_params *params,
225         struct snd_soc_dai *dai)
226 {
227         struct snd_soc_pcm_runtime *rtd = substream->private_data;
228         struct snd_soc_codec *codec = rtd->codec;
229         struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
230         u16 iface = snd_soc_read(codec, SSM2602_IFACE) & 0xfff3;
231         int srate = ssm2602_get_coeff(ssm2602->sysclk, params_rate(params));
232
233         if (substream == ssm2602->slave_substream) {
234                 dev_dbg(codec->dev, "Ignoring hw_params for slave substream\n");
235                 return 0;
236         }
237
238         if (srate < 0)
239                 return srate;
240
241         snd_soc_write(codec, SSM2602_ACTIVE, 0);
242         snd_soc_write(codec, SSM2602_SRATE, srate);
243
244         /* bit size */
245         switch (params_format(params)) {
246         case SNDRV_PCM_FORMAT_S16_LE:
247                 break;
248         case SNDRV_PCM_FORMAT_S20_3LE:
249                 iface |= 0x0004;
250                 break;
251         case SNDRV_PCM_FORMAT_S24_LE:
252                 iface |= 0x0008;
253                 break;
254         case SNDRV_PCM_FORMAT_S32_LE:
255                 iface |= 0x000c;
256                 break;
257         }
258         snd_soc_write(codec, SSM2602_IFACE, iface);
259         snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
260         return 0;
261 }
262
263 static int ssm2602_startup(struct snd_pcm_substream *substream,
264                            struct snd_soc_dai *dai)
265 {
266         struct snd_soc_pcm_runtime *rtd = substream->private_data;
267         struct snd_soc_codec *codec = rtd->codec;
268         struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
269         struct i2c_client *i2c = codec->control_data;
270         struct snd_pcm_runtime *master_runtime;
271
272         /* The DAI has shared clocks so if we already have a playback or
273          * capture going then constrain this substream to match it.
274          * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
275          */
276         if (ssm2602->master_substream) {
277                 master_runtime = ssm2602->master_substream->runtime;
278                 dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
279                         master_runtime->sample_bits,
280                         master_runtime->rate);
281
282                 if (master_runtime->rate != 0)
283                         snd_pcm_hw_constraint_minmax(substream->runtime,
284                                                      SNDRV_PCM_HW_PARAM_RATE,
285                                                      master_runtime->rate,
286                                                      master_runtime->rate);
287
288                 if (master_runtime->sample_bits != 0)
289                         snd_pcm_hw_constraint_minmax(substream->runtime,
290                                                      SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
291                                                      master_runtime->sample_bits,
292                                                      master_runtime->sample_bits);
293
294                 ssm2602->slave_substream = substream;
295         } else
296                 ssm2602->master_substream = substream;
297
298         return 0;
299 }
300
301 static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream,
302                                struct snd_soc_dai *dai)
303 {
304         struct snd_soc_pcm_runtime *rtd = substream->private_data;
305         struct snd_soc_codec *codec = rtd->codec;
306         /* set active */
307         snd_soc_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
308
309         return 0;
310 }
311
312 static void ssm2602_shutdown(struct snd_pcm_substream *substream,
313                              struct snd_soc_dai *dai)
314 {
315         struct snd_soc_pcm_runtime *rtd = substream->private_data;
316         struct snd_soc_codec *codec = rtd->codec;
317         struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
318
319         /* deactivate */
320         if (!codec->active)
321                 snd_soc_write(codec, SSM2602_ACTIVE, 0);
322
323         if (ssm2602->master_substream == substream)
324                 ssm2602->master_substream = ssm2602->slave_substream;
325
326         ssm2602->slave_substream = NULL;
327 }
328
329 static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
330 {
331         struct snd_soc_codec *codec = dai->codec;
332         u16 mute_reg = snd_soc_read(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE;
333         if (mute)
334                 snd_soc_write(codec, SSM2602_APDIGI,
335                                 mute_reg | APDIGI_ENABLE_DAC_MUTE);
336         else
337                 snd_soc_write(codec, SSM2602_APDIGI, mute_reg);
338         return 0;
339 }
340
341 static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
342                 int clk_id, unsigned int freq, int dir)
343 {
344         struct snd_soc_codec *codec = codec_dai->codec;
345         struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
346         switch (freq) {
347         case 11289600:
348         case 12000000:
349         case 12288000:
350         case 16934400:
351         case 18432000:
352                 ssm2602->sysclk = freq;
353                 return 0;
354         }
355         return -EINVAL;
356 }
357
358 static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
359                 unsigned int fmt)
360 {
361         struct snd_soc_codec *codec = codec_dai->codec;
362         u16 iface = 0;
363
364         /* set master/slave audio interface */
365         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
366         case SND_SOC_DAIFMT_CBM_CFM:
367                 iface |= 0x0040;
368                 break;
369         case SND_SOC_DAIFMT_CBS_CFS:
370                 break;
371         default:
372                 return -EINVAL;
373         }
374
375         /* interface format */
376         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
377         case SND_SOC_DAIFMT_I2S:
378                 iface |= 0x0002;
379                 break;
380         case SND_SOC_DAIFMT_RIGHT_J:
381                 break;
382         case SND_SOC_DAIFMT_LEFT_J:
383                 iface |= 0x0001;
384                 break;
385         case SND_SOC_DAIFMT_DSP_A:
386                 iface |= 0x0013;
387                 break;
388         case SND_SOC_DAIFMT_DSP_B:
389                 iface |= 0x0003;
390                 break;
391         default:
392                 return -EINVAL;
393         }
394
395         /* clock inversion */
396         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
397         case SND_SOC_DAIFMT_NB_NF:
398                 break;
399         case SND_SOC_DAIFMT_IB_IF:
400                 iface |= 0x0090;
401                 break;
402         case SND_SOC_DAIFMT_IB_NF:
403                 iface |= 0x0080;
404                 break;
405         case SND_SOC_DAIFMT_NB_IF:
406                 iface |= 0x0010;
407                 break;
408         default:
409                 return -EINVAL;
410         }
411
412         /* set iface */
413         snd_soc_write(codec, SSM2602_IFACE, iface);
414         return 0;
415 }
416
417 static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
418                                  enum snd_soc_bias_level level)
419 {
420         u16 reg = snd_soc_read(codec, SSM2602_PWR) & 0xff7f;
421
422         switch (level) {
423         case SND_SOC_BIAS_ON:
424                 /* vref/mid, osc on, dac unmute */
425                 snd_soc_write(codec, SSM2602_PWR, reg);
426                 break;
427         case SND_SOC_BIAS_PREPARE:
428                 break;
429         case SND_SOC_BIAS_STANDBY:
430                 /* everything off except vref/vmid, */
431                 snd_soc_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN);
432                 break;
433         case SND_SOC_BIAS_OFF:
434                 /* everything off, dac mute, inactive */
435                 snd_soc_write(codec, SSM2602_ACTIVE, 0);
436                 snd_soc_write(codec, SSM2602_PWR, 0xffff);
437                 break;
438
439         }
440         codec->dapm.bias_level = level;
441         return 0;
442 }
443
444 #define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
445                 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
446                 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
447
448 #define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
449                 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
450
451 static struct snd_soc_dai_ops ssm2602_dai_ops = {
452         .startup        = ssm2602_startup,
453         .prepare        = ssm2602_pcm_prepare,
454         .hw_params      = ssm2602_hw_params,
455         .shutdown       = ssm2602_shutdown,
456         .digital_mute   = ssm2602_mute,
457         .set_sysclk     = ssm2602_set_dai_sysclk,
458         .set_fmt        = ssm2602_set_dai_fmt,
459 };
460
461 static struct snd_soc_dai_driver ssm2602_dai = {
462         .name = "ssm2602-hifi",
463         .playback = {
464                 .stream_name = "Playback",
465                 .channels_min = 2,
466                 .channels_max = 2,
467                 .rates = SSM2602_RATES,
468                 .formats = SSM2602_FORMATS,},
469         .capture = {
470                 .stream_name = "Capture",
471                 .channels_min = 2,
472                 .channels_max = 2,
473                 .rates = SSM2602_RATES,
474                 .formats = SSM2602_FORMATS,},
475         .ops = &ssm2602_dai_ops,
476 };
477
478 static int ssm2602_suspend(struct snd_soc_codec *codec, pm_message_t state)
479 {
480         ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
481         return 0;
482 }
483
484 static int ssm2602_resume(struct snd_soc_codec *codec)
485 {
486         snd_soc_cache_sync(codec);
487
488         ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
489
490         return 0;
491 }
492
493 static int ssm2602_probe(struct snd_soc_codec *codec)
494 {
495         struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
496         int ret = 0, reg;
497
498         pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
499
500         ret = snd_soc_codec_set_cache_io(codec, 7, 9, ssm2602->control_type);
501         if (ret < 0) {
502                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
503                 return ret;
504         }
505
506         ret = ssm2602_reset(codec);
507         if (ret < 0) {
508                 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
509                 return ret;
510         }
511
512         /* set the update bits */
513         reg = snd_soc_read(codec, SSM2602_LINVOL);
514         snd_soc_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
515         reg = snd_soc_read(codec, SSM2602_RINVOL);
516         snd_soc_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
517         reg = snd_soc_read(codec, SSM2602_LOUT1V);
518         snd_soc_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
519         reg = snd_soc_read(codec, SSM2602_ROUT1V);
520         snd_soc_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
521         /*select Line in as default input*/
522         snd_soc_write(codec, SSM2602_APANA, APANA_SELECT_DAC |
523                         APANA_ENABLE_MIC_BOOST);
524
525         snd_soc_add_controls(codec, ssm2602_snd_controls,
526                                 ARRAY_SIZE(ssm2602_snd_controls));
527         ssm2602_add_widgets(codec);
528
529         return 0;
530 }
531
532 /* remove everything here */
533 static int ssm2602_remove(struct snd_soc_codec *codec)
534 {
535         ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
536         return 0;
537 }
538
539 static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = {
540         .probe =        ssm2602_probe,
541         .remove =       ssm2602_remove,
542         .suspend =      ssm2602_suspend,
543         .resume =       ssm2602_resume,
544         .set_bias_level = ssm2602_set_bias_level,
545         .reg_cache_size = ARRAY_SIZE(ssm2602_reg),
546         .reg_word_size = sizeof(u16),
547         .reg_cache_default = ssm2602_reg,
548 };
549
550 #if defined(CONFIG_SPI_MASTER)
551 static int __devinit ssm2602_spi_probe(struct spi_device *spi)
552 {
553         struct ssm2602_priv *ssm2602;
554         int ret;
555
556         ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
557         if (ssm2602 == NULL)
558                 return -ENOMEM;
559
560         spi_set_drvdata(spi, ssm2602);
561         ssm2602->control_type = SND_SOC_SPI;
562
563         ret = snd_soc_register_codec(&spi->dev,
564                         &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
565         if (ret < 0)
566                 kfree(ssm2602);
567         return ret;
568 }
569
570 static int __devexit ssm2602_spi_remove(struct spi_device *spi)
571 {
572         snd_soc_unregister_codec(&spi->dev);
573         kfree(spi_get_drvdata(spi));
574         return 0;
575 }
576
577 static struct spi_driver ssm2602_spi_driver = {
578         .driver = {
579                 .name   = "ssm2602",
580                 .owner  = THIS_MODULE,
581         },
582         .probe          = ssm2602_spi_probe,
583         .remove         = __devexit_p(ssm2602_spi_remove),
584 };
585 #endif
586
587 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
588 /*
589  * ssm2602 2 wire address is determined by GPIO5
590  * state during powerup.
591  *    low  = 0x1a
592  *    high = 0x1b
593  */
594 static int __devinit ssm2602_i2c_probe(struct i2c_client *i2c,
595                              const struct i2c_device_id *id)
596 {
597         struct ssm2602_priv *ssm2602;
598         int ret;
599
600         ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
601         if (ssm2602 == NULL)
602                 return -ENOMEM;
603
604         i2c_set_clientdata(i2c, ssm2602);
605         ssm2602->control_type = SND_SOC_I2C;
606
607         ret = snd_soc_register_codec(&i2c->dev,
608                         &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
609         if (ret < 0)
610                 kfree(ssm2602);
611         return ret;
612 }
613
614 static int __devexit ssm2602_i2c_remove(struct i2c_client *client)
615 {
616         snd_soc_unregister_codec(&client->dev);
617         kfree(i2c_get_clientdata(client));
618         return 0;
619 }
620
621 static const struct i2c_device_id ssm2602_i2c_id[] = {
622         { "ssm2602", 0 },
623         { }
624 };
625 MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
626
627 /* corgi i2c codec control layer */
628 static struct i2c_driver ssm2602_i2c_driver = {
629         .driver = {
630                 .name = "ssm2602",
631                 .owner = THIS_MODULE,
632         },
633         .probe = ssm2602_i2c_probe,
634         .remove = __devexit_p(ssm2602_i2c_remove),
635         .id_table = ssm2602_i2c_id,
636 };
637 #endif
638
639
640 static int __init ssm2602_modinit(void)
641 {
642         int ret = 0;
643
644 #if defined(CONFIG_SPI_MASTER)
645         ret = spi_register_driver(&ssm2602_spi_driver);
646         if (ret)
647                 return ret;
648 #endif
649
650 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
651         ret = i2c_add_driver(&ssm2602_i2c_driver);
652         if (ret)
653                 return ret;
654 #endif
655
656         return ret;
657 }
658 module_init(ssm2602_modinit);
659
660 static void __exit ssm2602_exit(void)
661 {
662 #if defined(CONFIG_SPI_MASTER)
663         spi_unregister_driver(&ssm2602_spi_driver);
664 #endif
665
666 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
667         i2c_del_driver(&ssm2602_i2c_driver);
668 #endif
669 }
670 module_exit(ssm2602_exit);
671
672 MODULE_DESCRIPTION("ASoC ssm2602 driver");
673 MODULE_AUTHOR("Cliff Cai");
674 MODULE_LICENSE("GPL");