ASoC: es8323: update codec es8323 driver
[firefly-linux-kernel-4.4.55.git] / sound / soc / codecs / ak4396.c
1 /*
2  * AK4396 ALSA SoC (ASoC) driver
3  *
4  * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
5  *
6  *  This program is free software; you can redistribute  it and/or modify it
7  *  under the terms of  the GNU General  Public License as published by the
8  *  Free Software Foundation;  either version 2 of the  License, or (at your
9  *  option) any later version.
10  */
11
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <sound/core.h>
15 #include <sound/soc.h>
16 #include <sound/initval.h>
17 #include <linux/spi/spi.h>
18 #include <sound/asoundef.h>
19 #include <linux/delay.h>
20
21 /* AK4396 registers addresses */
22 #define AK4396_REG_CONTROL1             0x00
23 #define AK4396_REG_CONTROL2             0x01
24 #define AK4396_REG_CONTROL3             0x02
25 #define AK4396_REG_LCH_ATT              0x03
26 #define AK4396_REG_RCH_ATT    0x04
27 #define AK4396_NUM_REGS                         5
28
29 #define AK4396_REG_MASK                         0x1f
30 #define AK4396_WRITE                                    0x20  /*C1 C0 R/W A4 A3 A2 A1 A0 8bit==0010 0000 */
31
32 /* Bit masks for AK4396 registers */
33 #define AK4396_CONTROL1_RSTN            (1 << 0)
34 #define AK4396_CONTROL1_DIF0            (1 << 1)
35 #define AK4396_CONTROL1_DIF1            (1 << 2)
36 #define AK4396_CONTROL1_DIF2            (1 << 3)
37
38 #define DRV_NAME "AK4396"
39
40 struct ak4396_private {
41         enum snd_soc_control_type control_type;
42         void *control_data;
43         unsigned int sysclk;
44 };
45
46 #if 0
47
48 static const u16 ak4396_reg[AK4396_NUM_REGS] = {
49         0x87, 0x02, 0x00, 0xff, 0xff
50 }; //CONFIG_LINF
51 #else
52 static const u16 ak4396_reg[AK4396_NUM_REGS] = {
53         0x05, 0x02, 0x00, 0xff, 0xff
54 }; //CONFIG_LINF
55
56 #endif
57
58 static void on_off_ext_amp(int i)
59 {
60
61     #ifdef SPK_CTL
62     //gpio_direction_output(SPK_CTL, GPIO_LOW);
63     gpio_set_value(SPK_CTL, i);
64     printk("*** %s() SPEAKER set as %d\n", __FUNCTION__, i);
65     #endif
66     #ifdef EAR_CON_PIN
67     //gpio_direction_output(EAR_CON_PIN, GPIO_LOW);
68     gpio_set_value(EAR_CON_PIN, i);
69     printk("*** %s() HEADPHONE set as %d\n", __FUNCTION__, i);
70     mdelay(50);
71     #endif
72 }
73
74 static int ak4396_fill_cache(struct snd_soc_codec *codec)
75 {
76         int i;
77         u8 *reg_cache = codec->reg_cache;
78         struct spi_device *spi = codec->control_data;
79
80         for (i = 0; i < codec->driver->reg_cache_size; i++) {
81                 int ret = spi_w8r8(spi, i);
82                 if (ret < 0) {
83                         dev_err(&spi->dev, "SPI write failure\n");
84                         return ret;
85                 }
86
87                 reg_cache[i] = ret;
88         }
89
90         return 0;
91 }
92
93 /* read the reg_cache */
94 static unsigned int ak4396_read_reg_cache(struct snd_soc_codec *codec,
95                                           unsigned int reg)
96 {
97         u8 *reg_cache = codec->reg_cache;
98
99         if (reg >= codec->driver->reg_cache_size)
100                 return -EINVAL;
101         
102 //      printk("read reg_cache[%x]====%d\n", reg, reg_cache[reg]);
103         return reg_cache[reg];
104 }
105
106 static int ak4396_spi_write(struct snd_soc_codec *codec, unsigned int reg,
107                             unsigned int value)
108 {
109         u8 *cache = codec->reg_cache;
110         struct spi_device *spi = codec->control_data;
111
112         if (reg >= codec->driver->reg_cache_size)
113                 return -EINVAL;
114
115         /* only write to the hardware if value has changed */
116         //if (cache[reg] != value)
117         //{
118                 u8 tmp[2] = { (reg & AK4396_REG_MASK) | AK4396_WRITE, value};
119                 //printk("tmp[0]===%d\n", tmp[0]);
120                 //printk("tmp[1]===%d\n", tmp[1]);
121                 if (spi_write(spi, tmp, sizeof(tmp))) {
122                         dev_err(&spi->dev, "SPI write failed\n");
123                         return -EIO;
124                 }
125         
126                 cache[reg] = value;
127         //}     
128         return 0;
129 }
130
131 /* write the register space */
132 static ak4396_write(struct snd_soc_codec *codec)
133 {
134         int ret, val, i;
135         val = 0;
136         int addr[5] = {0x00, 0x01, 0x02, 0x03, 0x04};
137         int dat[5] = {0x87, 0x02, 0x00, 0xff, 0xff};
138 //      while(1){
139         val |= AK4396_CONTROL1_RSTN;
140         ak4396_spi_write(codec, AK4396_REG_CONTROL1, val);
141         
142         for(i=0; i<5; i++)
143         {
144                 ret = ak4396_spi_write(codec, addr[i], dat[i]); 
145                 if (ret < 0)
146                         printk("ak4396_spi_write failed!\n");
147         
148                 printk("write %d time(s)\n", i);        
149         }
150 //      }
151 }
152
153 /*
154  * Note that this should be called from init rather than from hw_params.
155  */
156 static int ak4396_set_dai_sysclk(struct snd_soc_dai *codec_dai,
157                 int clk_id, unsigned int freq, int dir)
158 {
159         struct snd_soc_codec *codec = codec_dai->codec;
160         struct ak4396_private *ak4396 = snd_soc_codec_get_drvdata(codec);
161
162         printk("Enter::%s----%d\n",__FUNCTION__,__LINE__);
163         printk("freq======%d\n", freq); 
164         ak4396->sysclk = freq;
165
166         return 0;
167 }
168
169 static int ak4396_set_dai_fmt(struct snd_soc_dai *codec_dai,
170                               unsigned int format)
171 {
172         struct snd_soc_codec *codec = codec_dai->codec;
173         int val = 0;
174         
175         printk("%s----%d, format[%02x]\n",__FUNCTION__,__LINE__,format);
176         val = ak4396_read_reg_cache(codec, AK4396_REG_CONTROL1);
177         if (val < 0)
178                 return val;
179         val &= ~(AK4396_CONTROL1_DIF0 | AK4396_CONTROL1_DIF1 | AK4396_CONTROL1_DIF2);
180 //      printk("ak4396 val=%d\n", val);
181
182         /* set DAI format */
183         switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
184         case SND_SOC_DAIFMT_RIGHT_J:
185                 val |= AK4396_CONTROL1_DIF2 ;
186                 printk("SND_SOC_DAIFMT_RIGHT_J: \n");
187                 break;
188         case SND_SOC_DAIFMT_LEFT_J:
189                 val |= AK4396_CONTROL1_DIF1 ;
190                 printk("SND_SOC_DAIFMT_LEFT_J: \n");
191                 break;
192         case SND_SOC_DAIFMT_I2S:
193                 val |= AK4396_CONTROL1_DIF0 | AK4396_CONTROL1_DIF1 ;
194                 //val |= 0x87;
195         
196                 printk("SND_SOC_DAIFMT_I2S is ok!\n");
197                 break;
198         default:
199                 dev_err(codec->dev, "invalid dai format\n");
200                 return -EINVAL;
201         }
202
203         /* This device can only be slave */
204         if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
205         {
206                 printk("%s failed!----%d\n",__FUNCTION__,__LINE__);
207                 return -EINVAL;
208         }
209         
210         //val |= AK4396_CONTROL1_RSTN;
211         printk("AK4396 CONTROL1 val ==== %d\n", val);
212         ak4396_spi_write(codec, AK4396_REG_CONTROL1, val);
213
214         return 0;
215 }
216
217 static int ak4396_hw_params(struct snd_pcm_substream *substream,
218                             struct snd_pcm_hw_params *params,
219                             struct snd_soc_dai *dai)
220 {
221         struct snd_soc_pcm_runtime *rtd = substream->private_data;
222         struct snd_soc_codec *codec = rtd->codec;
223         int val = 0;
224
225         switch (params_rate(params)) {
226
227         case 176400:
228                 val |= IEC958_AES3_CON_FS_176400;
229                 printk("params_rate::=176400!\n");
230                 break;
231
232         case 192000:
233                 val |= IEC958_AES3_CON_FS_192000;
234                 printk("params_rate::=192000!\n");
235                 break;
236
237         case 88200:
238                 val |= IEC958_AES3_CON_FS_88200;
239                 printk("params_rate::=88200!\n");
240                 break;
241
242
243         case 96000:
244                 val |= IEC958_AES3_CON_FS_96000;
245                 printk("params_rate::=96000!\n");
246                 break;
247
248         case 44100:
249                 val |= IEC958_AES3_CON_FS_44100;
250                 printk("params_rate::=44100!\n");
251                 break;
252         case 48000:
253                 val |= IEC958_AES3_CON_FS_48000;
254                 break;
255         case 32000:
256                 val |= IEC958_AES3_CON_FS_32000;
257                 break;
258         default:
259                 dev_err(codec->dev, "unsupported sampling rate\n");
260                 return -EINVAL;
261         }
262         val = 0;
263         val = ak4396_read_reg_cache(codec, AK4396_REG_CONTROL1);
264         //reset RSTN bit;
265         val &= 0xFE;
266         printk("val ==== %d\n", val);
267         ak4396_spi_write(codec, AK4396_REG_CONTROL1, val);
268         val |= 0x01;
269         printk("val ==== %d\n", val);
270         ak4396_spi_write(codec, AK4396_REG_CONTROL1, val);
271         
272         //printk("val === %d\n", val);
273         //ak4396_spi_write(codec, AK4396_REG_CONTROL2, val);
274         return 0;
275 }
276
277 static struct snd_soc_dai_ops ak4396_dai_ops = {
278         .hw_params = ak4396_hw_params,
279         .set_fmt = ak4396_set_dai_fmt,
280         .set_sysclk = ak4396_set_dai_sysclk,
281 };
282
283 static struct snd_soc_dai_driver ak4396_dai = {
284         .name = "AK4396 HiFi",
285         .playback = {
286                 .stream_name = "Playback",
287                 .channels_min = 2,
288                 .channels_max = 2,
289                 .rates = SNDRV_PCM_RATE_8000_192000,
290                 .formats = SNDRV_PCM_FMTBIT_S16_LE  |
291                            SNDRV_PCM_FMTBIT_S24_3LE |
292                            SNDRV_PCM_FMTBIT_S24_LE  |
293                            SNDRV_PCM_FMTBIT_S32_LE
294         },
295         .ops = &ak4396_dai_ops,
296 };
297
298 struct snd_soc_codec *codec_temp; 
299 static int ak4396_probe(struct snd_soc_codec *codec)
300 {
301         struct ak4396_private *ak4396 = snd_soc_codec_get_drvdata(codec);
302         int ret;
303         printk("ak4396_probe begin!\n");
304         codec->control_data = ak4396->control_data;
305         codec_temp = codec;
306 #if 0
307         /* read all regs and fill the cache */
308         ret = ak4396_fill_cache(codec);
309         if (ret < 0) {
310                 dev_err(codec->dev, "failed to fill register cache\n");
311                 return ret;
312         }
313 #endif
314         /* write to ak4396_reg */
315 //      ak4396_write(codec);
316         
317         printk("ak4396_probe is ok!\n");
318         dev_info(codec->dev, "SPI device initialized\n");
319         return 0;
320 }
321
322 static int ak4396_remove(struct snd_soc_codec *codec)
323 {
324         int val, ret;
325
326         val = ak4396_read_reg_cache(codec, AK4396_REG_CONTROL1);
327         if (val < 0)
328                 return val;
329
330         /* set non-reset bits */
331         val &= ~AK4396_CONTROL1_RSTN;
332         ret = ak4396_spi_write(codec, AK4396_REG_CONTROL1, val);
333
334         return ret;
335 }
336
337 static int ak4396_suspend(struct snd_soc_codec *codec)
338 {       
339         return 0;
340 }
341
342 static int ak4396_resume(struct snd_soc_codec *codec)
343 {
344         //ak4396_write(codec);
345         return 0;
346 }
347
348 static struct snd_soc_codec_driver soc_codec_device_ak4396 = {
349         .probe =        ak4396_probe,
350         .remove =       ak4396_remove,
351         .suspend =      ak4396_suspend,
352         .resume =       ak4396_resume,
353         .reg_cache_size = AK4396_NUM_REGS,
354         .reg_word_size = sizeof(u16),
355         .reg_cache_default = ak4396_reg,
356 };
357
358 static struct class *cls = NULL;
359
360 static ssize_t store_ak4396_reg(struct class *dev,
361                struct class_attribute *attr, const char *buf, size_t count)
362 {
363         int reg, value, ret;
364 //      char buf[10] = "123 11";
365         char *start = buf;
366
367         printk("%s, the first dat  is reg, the second dat is  data, data type is dex\n", __FUNCTION__);
368         while (*start == ' ')
369                 start++;
370         reg = simple_strtoull(start, &start, 16);
371
372         while (*start == ' ')
373                 start++;
374         value = simple_strtoull(start, &start, 16);
375
376         
377         ret = ak4396_spi_write(codec_temp, reg, value); 
378         if (ret < 0)
379                         printk("ak4396_spi_write failed!\n");
380
381         printk("reg = %d, value =%d\n", reg, value);
382 //      return 0;               
383 }
384 static struct class_attribute attr[] = {
385        __ATTR(write_reg, 0644, NULL, store_ak4396_reg),
386        __ATTR_NULL,
387 };
388 static int ak4396_spi_probe(struct spi_device *spi)
389 {
390         struct ak4396_private *ak4396;
391         int ret;
392         printk("ak4396_spi_probe begin!\n");
393
394 #if 0 //defined(CONFIG_ARCH_RK3188)
395         iomux_set(SPI1_CS0);
396         iomux_set(SPI1_CLK);
397         iomux_set(SPI1_TXD);
398         printk("iomux_set is OK!!!\n");
399 #endif
400
401         spi->bits_per_word = 8;
402         spi->mode = SPI_MODE_0;
403         ret = spi_setup(spi);
404         if (ret < 0)
405                 return ret;
406
407         ak4396 = kzalloc(sizeof(struct ak4396_private), GFP_KERNEL);
408         if (ak4396 == NULL)
409                 return -ENOMEM;
410
411         ak4396->control_data = spi;
412         ak4396->control_type = SND_SOC_SPI;
413         spi_set_drvdata(spi, ak4396);
414
415         cls = class_create(THIS_MODULE, DRV_NAME);
416         if (IS_ERR(cls))
417         {
418                 printk("class_create failed!\n");               
419         }
420         ret = class_create_file(cls, attr);
421         if (ret < 0)
422         {
423                 printk("class_create_file failed!\n");          
424         }
425
426         ret = snd_soc_register_codec(&spi->dev,
427                         &soc_codec_device_ak4396, &ak4396_dai, 1);
428         if (ret < 0)
429                 kfree(ak4396);
430
431         printk("ak4396_spi_probe successful!\n");
432         return ret;
433 }
434
435 static int ak4396_spi_remove(struct spi_device *spi)
436 {
437         snd_soc_unregister_codec(&spi->dev);
438         kfree(spi_get_drvdata(spi));
439         return 0;
440 }
441
442 static struct spi_driver ak4396_spi_driver = {
443         .driver  = {
444                 .name   = DRV_NAME,
445                 .owner  = THIS_MODULE,
446         },
447         .probe  = ak4396_spi_probe,
448         .remove = ak4396_spi_remove,
449 };
450
451 static int __init ak4396_init(void)
452 {
453         printk("%s\n", __FUNCTION__);
454         return spi_register_driver(&ak4396_spi_driver);
455 }
456 module_init(ak4396_init);
457
458 static void __exit ak4396_exit(void)
459 {
460         spi_unregister_driver(&ak4396_spi_driver);
461 }
462 module_exit(ak4396_exit);
463
464 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
465 MODULE_DESCRIPTION("Asahi Kasei AK4396 ALSA SoC driver");
466 MODULE_LICENSE("GPL");
467