f7c08362f41f222fb628400a49551c91b011342e
[cascardo/linux.git] / sound / soc / samsung / daisy_max98095.c
1 /*
2  * Exynos machine ASoC driver for boards using MAX98095 or MAX98088
3  * codec.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  *
19  */
20
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/platform_device.h>
24 #include <linux/clk.h>
25 #include <linux/io.h>
26 #include <linux/of_gpio.h>
27 #include <linux/of.h>
28 #include <linux/of_device.h>
29
30 #include <sound/soc.h>
31 #include <sound/soc-dapm.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/jack.h>
35 #include <sound/max98095.h>
36 #include <sound/max98088.h>
37
38 #include <mach/regs-clock.h>
39 #include <mach/gpio.h>
40
41 #include "i2s.h"
42 #include "s3c-i2s-v2.h"
43 #include "../codecs/max98095.h"
44 #include "../codecs/max98088.h"
45 #include "codec_plugin.h"
46
47 #define DRV_NAME "daisy-snd-max98095"
48
49 struct daisy_max98095 {
50         struct platform_device *pcm_dev;
51 };
52
53 /* Audio clock settings are belonged to board specific part. Every
54  * board can set audio source clock setting which is matched with H/W
55  * like this function-'set_audio_clock_heirachy'.
56  */
57 static int set_audio_clock_heirachy(struct platform_device *pdev)
58 {
59         struct clk *fout_epll, *mout_epll, *sclk_audbus, *audss, *i2sclk;
60         int ret = 0;
61
62         fout_epll = clk_get(NULL, "fout_epll");
63         if (IS_ERR(fout_epll)) {
64                 printk(KERN_WARNING "%s: Cannot find fout_epll.\n",
65                                 __func__);
66                 return -EINVAL;
67         }
68
69         mout_epll = clk_get(NULL, "mout_epll");
70         if (IS_ERR(mout_epll)) {
71                 printk(KERN_WARNING "%s: Cannot find mout_epll.\n",
72                                 __func__);
73                 ret = -EINVAL;
74                 goto out1;
75         }
76
77         sclk_audbus = clk_get(&pdev->dev, "audio-bus");
78         if (IS_ERR(sclk_audbus)) {
79                 printk(KERN_WARNING "%s: Cannot find audio-bus.\n",
80                                 __func__);
81                 ret = -EINVAL;
82                 goto out2;
83         }
84
85         audss = clk_get(&pdev->dev, "mout_audss");
86         if (IS_ERR(audss)) {
87                 printk(KERN_WARNING "%s: Cannot find audss.\n",
88                                 __func__);
89                 ret = -EINVAL;
90                 goto out3;
91         }
92
93         i2sclk = clk_get(NULL, "i2sclk");
94         if (IS_ERR(i2sclk)) {
95                 printk(KERN_WARNING "%s: Cannot find i2sclk.\n",
96                                 __func__);
97                 ret = -EINVAL;
98                 goto out4;
99         }
100
101         /* Set audio clock hierarchy for S/PDIF */
102         if (clk_set_parent(mout_epll, fout_epll))
103                 printk(KERN_WARNING "Failed to set parent of epll.\n");
104         if (clk_set_parent(sclk_audbus, mout_epll))
105                 printk(KERN_WARNING "Failed to set parent of audbus.\n");
106         if (clk_set_parent(audss, fout_epll))
107                 printk(KERN_WARNING "Failed to set parent of audss.\n");
108         if (clk_set_parent(i2sclk, sclk_audbus))
109                 printk(KERN_WARNING "Failed to set parent of i2sclk.\n");
110
111         clk_put(i2sclk);
112 out4:
113         clk_put(audss);
114 out3:
115         clk_put(sclk_audbus);
116 out2:
117         clk_put(mout_epll);
118 out1:
119         clk_put(fout_epll);
120
121         return ret;
122 }
123
124 static int set_epll_rate(unsigned long rate)
125 {
126         int ret;
127         struct clk *fout_epll;
128
129         fout_epll = clk_get(NULL, "fout_epll");
130
131         if (IS_ERR(fout_epll)) {
132                 printk(KERN_ERR "%s: failed to get fout_epll\n", __func__);
133                 return PTR_ERR(fout_epll);
134         }
135
136         if (rate == clk_get_rate(fout_epll))
137                 goto out;
138
139         ret = clk_set_rate(fout_epll, rate);
140         if (ret < 0) {
141                 printk(KERN_ERR "failed to clk_set_rate of fout_epll for audio\n");
142                 goto out;
143         }
144 out:
145         clk_put(fout_epll);
146
147         return 0;
148 }
149
150 static int daisy_hw_params(struct snd_pcm_substream *substream,
151         struct snd_pcm_hw_params *params)
152 {
153         struct snd_soc_pcm_runtime *rtd = substream->private_data;
154         struct snd_soc_dai *codec_dai = rtd->codec_dai;
155         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
156         int bfs, psr, rfs, ret;
157         unsigned long rclk;
158         unsigned long xtal;
159         struct clk *xtal_clk;
160
161         switch (params_format(params)) {
162         case SNDRV_PCM_FORMAT_U24:
163         case SNDRV_PCM_FORMAT_S24:
164                 bfs = 48;
165                 break;
166         case SNDRV_PCM_FORMAT_U16_LE:
167         case SNDRV_PCM_FORMAT_S16_LE:
168                 bfs = 32;
169                 break;
170         default:
171                 return -EINVAL;
172         }
173
174         switch (params_rate(params)) {
175         case 16000:
176         case 22050:
177         case 24000:
178         case 32000:
179         case 44100:
180         case 48000:
181         case 88200:
182         case 96000:
183                 if (bfs == 48)
184                         rfs = 384;
185                 else
186                         rfs = 256;
187                 break;
188         case 64000:
189                 rfs = 384;
190                 break;
191         case 8000:
192         case 11025:
193         case 12000:
194                 if (bfs == 48)
195                         rfs = 768;
196                 else
197                         rfs = 512;
198                 break;
199         default:
200                 return -EINVAL;
201         }
202
203         rclk = params_rate(params) * rfs;
204
205         switch (rclk) {
206         case 4096000:
207         case 5644800:
208         case 6144000:
209         case 8467200:
210         case 9216000:
211                 psr = 8;
212                 break;
213         case 8192000:
214         case 11289600:
215         case 12288000:
216         case 16934400:
217         case 18432000:
218                 psr = 4;
219                 break;
220         case 22579200:
221         case 24576000:
222         case 33868800:
223         case 36864000:
224                 psr = 2;
225                 break;
226         case 67737600:
227         case 73728000:
228                 psr = 1;
229                 break;
230         default:
231                 printk(KERN_ERR "rclk = %lu is not yet supported!\n", rclk);
232                 return -EINVAL;
233         }
234
235         ret = set_epll_rate(rclk * psr);
236         if (ret < 0)
237                 return ret;
238
239         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
240                                              SND_SOC_DAIFMT_NB_NF |
241                                              SND_SOC_DAIFMT_CBS_CFS);
242         if (ret < 0)
243                 return ret;
244
245         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
246                                            SND_SOC_DAIFMT_NB_NF |
247                                            SND_SOC_DAIFMT_CBS_CFS);
248         if (ret < 0)
249                 return ret;
250
251         xtal_clk = clk_get(NULL, "xtal"); /*xtal clk is input to codec MCLK1*/
252         if (IS_ERR(xtal_clk)) {
253                 printk(KERN_ERR "%s: failed to get xtal clock\n", __func__);
254                 return PTR_ERR(xtal_clk);
255         }
256
257         xtal = clk_get_rate(xtal_clk);
258         clk_put(xtal_clk);
259
260         ret = snd_soc_dai_set_sysclk(codec_dai, 0, xtal, SND_SOC_CLOCK_IN);
261         if (ret < 0)
262                 return ret;
263
264         ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_CDCLK,
265                                         0, SND_SOC_CLOCK_OUT);
266         if (ret < 0)
267                 return ret;
268
269         ret = snd_soc_dai_set_clkdiv(cpu_dai, SAMSUNG_I2S_DIV_BCLK, bfs);
270         if (ret < 0)
271                 return ret;
272
273         return 0;
274 }
275
276 /*
277  * MAX98095 DAI operations.
278  */
279 static struct snd_soc_ops daisy_ops = {
280         .hw_params = daisy_hw_params,
281 };
282
283 static struct snd_soc_jack daisy_hp_jack;
284 static struct snd_soc_jack_pin daisy_hp_jack_pins[] = {
285         {
286                 .pin = "Headphone Jack",
287                 .mask = SND_JACK_HEADPHONE,
288         },
289 };
290
291 static struct snd_soc_jack_gpio daisy_hp_jack_gpio = {
292         .name = "headphone detect",
293         .report = SND_JACK_HEADPHONE,
294 };
295
296 static struct snd_soc_jack daisy_mic_jack;
297 static struct snd_soc_jack_pin daisy_mic_jack_pins[] = {
298         {
299                 .pin = "Mic Jack",
300                 .mask = SND_JACK_MICROPHONE,
301         },
302 };
303
304 static struct snd_soc_jack_gpio daisy_mic_jack_gpio = {
305         .name = "mic detect",
306         .report = SND_JACK_MICROPHONE,
307 };
308
309 static const struct snd_soc_dapm_route daisy_audio_map[] = {
310         {"Mic Jack", "NULL", "MICBIAS2"},
311         {"MIC2", "NULL", "Mic Jack"},
312 };
313
314 static const struct snd_soc_dapm_widget daisy_dapm_widgets[] = {
315         SND_SOC_DAPM_MIC("Mic Jack", NULL),
316         SND_SOC_DAPM_HP("Headphone Jack", NULL),
317 };
318
319 static struct snd_soc_jack daisy_hdmi_jack;
320
321 static int get_hdmi(struct snd_kcontrol *kcontrol,
322                                  struct snd_ctl_elem_value *ucontrol)
323 {
324         struct audio_codec_plugin *plugin;
325         int ret = 0, state = 0;
326
327         plugin = (struct audio_codec_plugin *)kcontrol->private_value;
328
329         if (!plugin)
330                 return 0;
331
332         if (!plugin->ops.hw_params)
333                 return 0;
334
335         ret = plugin->ops.get_state(plugin->dev, &state);
336         if (ret < 0)
337                 return 0;
338
339         ucontrol->value.integer.value[0] = (long int)state;
340         return 1;
341 }
342
343 static int put_hdmi(struct snd_kcontrol *kcontrol,
344                                   struct snd_ctl_elem_value *ucontrol)
345 {
346         struct audio_codec_plugin *plugin;
347         int ret = 0, state;
348
349         plugin = (struct audio_codec_plugin *)kcontrol->private_value;
350
351         if (!plugin)
352                 return 0;
353
354         if (!plugin->ops.hw_params)
355                 return 0;
356
357         state = (int)ucontrol->value.integer.value[0];
358         ret = plugin->ops.set_state(plugin->dev,
359                 ucontrol->value.integer.value[0]);
360
361         if (ret < 0)
362                 return 0;
363         return 1;
364 }
365
366 static struct snd_kcontrol_new daisy_dapm_controls[] = {
367         SOC_SINGLE_BOOL_EXT("HDMI Playback Switch", 0, get_hdmi, put_hdmi),
368 };
369
370 static int daisy_init(struct snd_soc_pcm_runtime *rtd)
371 {
372         struct snd_soc_codec *codec = rtd->codec;
373         struct snd_soc_dapm_context *dapm = &codec->dapm;
374         struct snd_soc_card *card = codec->card;
375         struct device_node *dn = card->dev->of_node;
376         struct audio_codec_plugin *plugin;
377
378         if (dn) {
379                 enum of_gpio_flags flags;
380
381                 daisy_mic_jack_gpio.gpio = of_get_named_gpio_flags(
382                                 dn, "samsung,mic-det-gpios", 0, &flags);
383                 daisy_mic_jack_gpio.invert = !!(flags & OF_GPIO_ACTIVE_LOW);
384
385                 daisy_hp_jack_gpio.gpio = of_get_named_gpio_flags(
386                                 dn, "samsung,hp-det-gpios", 0, &flags);
387                 daisy_hp_jack_gpio.invert = !!(flags & OF_GPIO_ACTIVE_LOW);
388         }
389
390         if (gpio_is_valid(daisy_mic_jack_gpio.gpio)) {
391                 snd_soc_jack_new(codec, "Mic Jack", SND_JACK_MICROPHONE,
392                                  &daisy_mic_jack);
393                 snd_soc_jack_add_pins(&daisy_mic_jack,
394                                       ARRAY_SIZE(daisy_mic_jack_pins),
395                                       daisy_mic_jack_pins);
396                 snd_soc_jack_add_gpios(&daisy_mic_jack, 1,
397                                        &daisy_mic_jack_gpio);
398         }
399
400         if (gpio_is_valid(daisy_hp_jack_gpio.gpio)) {
401                 snd_soc_jack_new(codec, "Headphone Jack",
402                                  SND_JACK_HEADPHONE, &daisy_hp_jack);
403                 snd_soc_jack_add_pins(&daisy_hp_jack,
404                                       ARRAY_SIZE(daisy_hp_jack_pins),
405                                       daisy_hp_jack_pins);
406                 snd_soc_jack_add_gpios(&daisy_hp_jack, 1,
407                                        &daisy_hp_jack_gpio);
408         }
409
410         plugin = (void *)daisy_dapm_controls[0].private_value;
411         if (plugin)
412                 snd_soc_jack_new(codec, "HDMI Jack",
413                                  SND_JACK_AVOUT, &daisy_hdmi_jack);
414
415         /* Microphone BIAS is needed to power the analog mic.
416          * MICBIAS2 is connected to analog mic (MIC3, which is in turn
417          * connected to MIC2 via 'External MIC') on Daisy.
418          *
419          * Ultimately, the following should hold:
420          *
421          *   Microphone in jack     => MICBIAS2 enabled &&
422          *                             'External Mic' = MIC2
423          *   Microphone not in jack => MICBIAS2 disabled &&
424          *                             'External Mic' = MIC1
425         */
426         snd_soc_dapm_force_enable_pin(dapm, "MICBIAS2");
427
428         snd_soc_dapm_sync(dapm);
429
430         return 0;
431 }
432
433 static int daisy_resume_post(struct snd_soc_card *card)
434 {
435         if (gpio_is_valid(daisy_mic_jack_gpio.gpio))
436                 snd_soc_jack_gpio_detect(&daisy_mic_jack_gpio);
437
438         if (gpio_is_valid(daisy_hp_jack_gpio.gpio))
439                 snd_soc_jack_gpio_detect(&daisy_hp_jack_gpio);
440
441         return 0;
442 }
443
444 static int daisy_hdmi_jack_report(int plugged)
445 {
446         snd_soc_jack_report(&daisy_hdmi_jack,
447                             plugged ? SND_JACK_AVOUT : 0,
448                             SND_JACK_AVOUT);
449         return 0;
450 }
451
452 static struct snd_soc_dai_link daisy_dai[] = {
453         { /* Primary DAI i/f */
454                 .name = "MAX98095 RX",
455                 .stream_name = "Playback",
456                 .cpu_dai_name = "samsung-i2s.0",
457                 .codec_dai_name = "HiFi",
458                 .platform_name = "samsung-audio",
459                 .init = daisy_init,
460                 .ops = &daisy_ops,
461         }, { /* Capture i/f */
462                 .name = "MAX98095 TX",
463                 .stream_name = "Capture",
464                 .cpu_dai_name = "samsung-i2s.0",
465                 .codec_dai_name = "HiFi",
466                 .platform_name = "samsung-audio",
467                 .ops = &daisy_ops,
468         },
469 };
470
471 static struct snd_soc_card daisy_snd = {
472         .name = "DAISY-I2S",
473         .dai_link = daisy_dai,
474         .num_links = ARRAY_SIZE(daisy_dai),
475         .controls = daisy_dapm_controls,
476         .num_controls = ARRAY_SIZE(daisy_dapm_controls),
477         .dapm_widgets = daisy_dapm_widgets,
478         .num_dapm_widgets = ARRAY_SIZE(daisy_dapm_widgets),
479         .dapm_routes = daisy_audio_map,
480         .num_dapm_routes = ARRAY_SIZE(daisy_audio_map),
481         .resume_post = daisy_resume_post,
482 };
483
484 static int plugin_init(struct audio_codec_plugin **pplugin)
485 {
486         struct device_node *plugin_node = NULL;
487         struct platform_device *plugin_pdev;
488         struct audio_codec_plugin *plugin;
489
490         plugin_node = of_find_node_by_name(NULL, "hdmi-audio");
491         if (!plugin_node)
492                 return -EFAULT;
493
494         plugin_pdev = of_find_device_by_node(plugin_node);
495         if (!plugin_pdev)
496                 return -EFAULT;
497
498         plugin = dev_get_drvdata(&plugin_pdev->dev);
499         if (!plugin)
500                 return -EFAULT;
501         else
502                 *pplugin = plugin;
503
504         plugin->jack_cb = daisy_hdmi_jack_report;
505
506         return 0;
507 }
508
509 static __devinit int daisy_max98095_driver_probe(struct platform_device *pdev)
510 {
511         struct snd_soc_card *card = &daisy_snd;
512         struct device_node *dn;
513         struct daisy_max98095 *machine;
514         struct audio_codec_plugin *plugin = NULL;
515         int i, ret;
516
517         if (!pdev->dev.platform_data && !pdev->dev.of_node) {
518                 dev_err(&pdev->dev, "No platform data supplied\n");
519                 return -EINVAL;
520         }
521
522         /* The below needs to be replaced with proper full device-tree probing
523          * of the ASoC device, but the core plumbing hasn't been completed yet
524          * so we're doing this only half-way now.
525          */
526
527         if (!of_machine_is_compatible("google,snow") &&
528             !of_machine_is_compatible("google,spring") &&
529             !of_machine_is_compatible("google,daisy"))
530                 return -ENODEV;
531
532         dn = of_find_compatible_node(NULL, NULL, "maxim,max98095");
533         if (!dn) {
534                 dn = of_find_compatible_node(NULL, NULL, "maxim,max98089");
535                 if (!dn)
536                         return -ENODEV;
537         }
538
539         for (i = 0; i < ARRAY_SIZE(daisy_dai); i++)
540                 daisy_dai[i].codec_of_node = of_node_get(dn);
541
542         of_node_put(dn);
543
544         machine = devm_kzalloc(&pdev->dev, sizeof(struct daisy_max98095),
545                                GFP_KERNEL);
546         if (!machine) {
547                 dev_err(&pdev->dev, "Can't allocate daisy_max98095 struct\n");
548                 return -ENOMEM;
549         }
550
551         plugin_init(&plugin);
552         daisy_dapm_controls[0].private_value = (unsigned long)plugin;
553
554         card->dev = &pdev->dev;
555         platform_set_drvdata(pdev, card);
556         snd_soc_card_set_drvdata(card, machine);
557
558         machine->pcm_dev = platform_device_register_simple(
559                                 "daisy-pcm-audio", -1, NULL, 0);
560         if (IS_ERR(machine->pcm_dev)) {
561                 dev_err(&pdev->dev, "Can't instantiate daisy-pcm-audio\n");
562                 ret = PTR_ERR(machine->pcm_dev);
563                 return ret;
564         }
565
566         ret = snd_soc_register_card(card);
567         if (ret) {
568                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
569                 goto err_unregister;
570         }
571
572         return set_audio_clock_heirachy(pdev);
573
574 err_unregister:
575         if (!IS_ERR(machine->pcm_dev))
576                 platform_device_unregister(machine->pcm_dev);
577         return ret;
578 }
579
580 static int __devexit daisy_max98095_driver_remove(struct platform_device *pdev)
581 {
582         struct snd_soc_card *card = platform_get_drvdata(pdev);
583         struct daisy_max98095 *machine = snd_soc_card_get_drvdata(card);
584
585         snd_soc_unregister_card(card);
586
587         if (!IS_ERR(machine->pcm_dev))
588                 platform_device_unregister(machine->pcm_dev);
589
590         return 0;
591 }
592
593 static const struct of_device_id daisy_max98095_of_match[] __devinitconst = {
594         { .compatible = "google,daisy-audio-max98095", },
595         { .compatible = "google,daisy-audio-max98089", },
596         {},
597 };
598
599 static struct platform_driver daisy_max98095_driver = {
600         .driver = {
601                 .name = DRV_NAME,
602                 .owner = THIS_MODULE,
603                 .pm = &snd_soc_pm_ops,
604                 .of_match_table = daisy_max98095_of_match,
605         },
606         .probe = daisy_max98095_driver_probe,
607         .remove = __devexit_p(daisy_max98095_driver_remove),
608 };
609
610 module_platform_driver(daisy_max98095_driver);
611
612 MODULE_DESCRIPTION("ALSA SoC DAISY MAX98095 machine driver");
613 MODULE_LICENSE("GPL");