blob: facf6f00c6b0fcf7d860fd482a98b10ce40df398 [file] [log] [blame]
Leon Romanovsky58783fa2011-12-19 21:51:52 +02001/*
Stephen Warrenaef9a372012-05-22 16:09:51 -06002* tegra_alc5632.c -- Toshiba AC100(PAZ00) machine ASoC driver
Stephen Warren1ae93b92012-03-20 14:55:48 -06003 *
4 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
Stephen Warren518de862012-03-20 14:55:49 -06005 * Copyright (C) 2012 - NVIDIA, Inc.
Stephen Warren1ae93b92012-03-20 14:55:48 -06006 *
7 * Authors: Leon Romanovsky <leon@leon.nu>
8 * Andrey Danin <danindrey@mail.ru>
9 * Marc Dietrich <marvin24@gmx.de>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
Leon Romanovsky58783fa2011-12-19 21:51:52 +020015
16#include <asm/mach-types.h>
17
18#include <linux/module.h>
19#include <linux/platform_device.h>
20#include <linux/slab.h>
21#include <linux/gpio.h>
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020022#include <linux/of_gpio.h>
Leon Romanovsky58783fa2011-12-19 21:51:52 +020023
24#include <sound/core.h>
25#include <sound/jack.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
29
30#include "../codecs/alc5632.h"
31
Leon Romanovsky58783fa2011-12-19 21:51:52 +020032#include "tegra_asoc_utils.h"
33
34#define DRV_NAME "tegra-alc5632"
35
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020036#define GPIO_HP_DET BIT(0)
37
Leon Romanovsky58783fa2011-12-19 21:51:52 +020038struct tegra_alc5632 {
39 struct tegra_asoc_utils_data util_data;
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020040 int gpio_requested;
41 int gpio_hp_det;
Leon Romanovsky58783fa2011-12-19 21:51:52 +020042};
43
44static int tegra_alc5632_asoc_hw_params(struct snd_pcm_substream *substream,
45 struct snd_pcm_hw_params *params)
46{
47 struct snd_soc_pcm_runtime *rtd = substream->private_data;
48 struct snd_soc_dai *codec_dai = rtd->codec_dai;
49 struct snd_soc_codec *codec = rtd->codec;
50 struct snd_soc_card *card = codec->card;
51 struct tegra_alc5632 *alc5632 = snd_soc_card_get_drvdata(card);
52 int srate, mclk;
53 int err;
54
55 srate = params_rate(params);
56 mclk = 512 * srate;
57
58 err = tegra_asoc_utils_set_rate(&alc5632->util_data, srate, mclk);
59 if (err < 0) {
60 dev_err(card->dev, "Can't configure clocks\n");
61 return err;
62 }
63
64 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
65 SND_SOC_CLOCK_IN);
66 if (err < 0) {
67 dev_err(card->dev, "codec_dai clock not set\n");
68 return err;
69 }
70
71 return 0;
72}
73
74static struct snd_soc_ops tegra_alc5632_asoc_ops = {
75 .hw_params = tegra_alc5632_asoc_hw_params,
76};
77
78static struct snd_soc_jack tegra_alc5632_hs_jack;
79
80static struct snd_soc_jack_pin tegra_alc5632_hs_jack_pins[] = {
81 {
82 .pin = "Headset Mic",
83 .mask = SND_JACK_MICROPHONE,
84 },
85 {
86 .pin = "Headset Stereophone",
87 .mask = SND_JACK_HEADPHONE,
88 },
89};
90
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020091static struct snd_soc_jack_gpio tegra_alc5632_hp_jack_gpio = {
92 .name = "Headset detection",
93 .report = SND_JACK_HEADSET,
94 .debounce_time = 150,
95 .invert = 1,
96};
97
Leon Romanovsky58783fa2011-12-19 21:51:52 +020098static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = {
99 SND_SOC_DAPM_SPK("Int Spk", NULL),
100 SND_SOC_DAPM_HP("Headset Stereophone", NULL),
101 SND_SOC_DAPM_MIC("Headset Mic", NULL),
Leon Romanovskydd7d3a22012-02-13 21:27:36 +0200102 SND_SOC_DAPM_MIC("Digital Mic", NULL),
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200103};
104
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200105static const struct snd_kcontrol_new tegra_alc5632_controls[] = {
106 SOC_DAPM_PIN_SWITCH("Int Spk"),
107};
108
109static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd)
110{
111 struct snd_soc_codec *codec = rtd->codec;
112 struct snd_soc_dapm_context *dapm = &codec->dapm;
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200113 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(codec->card);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200114
115 snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
116 &tegra_alc5632_hs_jack);
117 snd_soc_jack_add_pins(&tegra_alc5632_hs_jack,
118 ARRAY_SIZE(tegra_alc5632_hs_jack_pins),
119 tegra_alc5632_hs_jack_pins);
120
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200121 if (gpio_is_valid(machine->gpio_hp_det)) {
122 tegra_alc5632_hp_jack_gpio.gpio = machine->gpio_hp_det;
123 snd_soc_jack_add_gpios(&tegra_alc5632_hs_jack,
124 1,
125 &tegra_alc5632_hp_jack_gpio);
126 machine->gpio_requested |= GPIO_HP_DET;
127 }
128
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200129 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
130
131 return 0;
132}
133
134static struct snd_soc_dai_link tegra_alc5632_dai = {
135 .name = "ALC5632",
136 .stream_name = "ALC5632 PCM",
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200137 .codec_dai_name = "alc5632-hifi",
138 .init = tegra_alc5632_asoc_init,
139 .ops = &tegra_alc5632_asoc_ops,
140 .dai_fmt = SND_SOC_DAIFMT_I2S
141 | SND_SOC_DAIFMT_NB_NF
142 | SND_SOC_DAIFMT_CBS_CFS,
143};
144
145static struct snd_soc_card snd_soc_tegra_alc5632 = {
146 .name = "tegra-alc5632",
Axel Linb16eaf92011-12-22 21:23:01 +0800147 .owner = THIS_MODULE,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200148 .dai_link = &tegra_alc5632_dai,
149 .num_links = 1,
150 .controls = tegra_alc5632_controls,
151 .num_controls = ARRAY_SIZE(tegra_alc5632_controls),
152 .dapm_widgets = tegra_alc5632_dapm_widgets,
153 .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets),
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200154 .fully_routed = true,
155};
156
157static __devinit int tegra_alc5632_probe(struct platform_device *pdev)
158{
Stephen Warrenaef9a372012-05-22 16:09:51 -0600159 struct device_node *np = pdev->dev.of_node;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200160 struct snd_soc_card *card = &snd_soc_tegra_alc5632;
161 struct tegra_alc5632 *alc5632;
162 int ret;
163
164 alc5632 = devm_kzalloc(&pdev->dev,
165 sizeof(struct tegra_alc5632), GFP_KERNEL);
166 if (!alc5632) {
167 dev_err(&pdev->dev, "Can't allocate tegra_alc5632\n");
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200168 ret = -ENOMEM;
169 goto err;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200170 }
171
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200172 card->dev = &pdev->dev;
173 platform_set_drvdata(pdev, card);
174 snd_soc_card_set_drvdata(card, alc5632);
175
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200176 if (!(pdev->dev.of_node)) {
177 dev_err(&pdev->dev, "Must be instantiated using device tree\n");
178 ret = -EINVAL;
179 goto err;
180 }
181
Stephen Warrenaef9a372012-05-22 16:09:51 -0600182 alc5632->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
183 if (alc5632->gpio_hp_det == -ENODEV)
184 return -EPROBE_DEFER;
185
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200186 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
187 if (ret)
188 goto err;
189
190 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
191 if (ret)
192 goto err;
193
194 tegra_alc5632_dai.codec_of_node = of_parse_phandle(
195 pdev->dev.of_node, "nvidia,audio-codec", 0);
196
197 if (!tegra_alc5632_dai.codec_of_node) {
198 dev_err(&pdev->dev,
199 "Property 'nvidia,audio-codec' missing or invalid\n");
200 ret = -EINVAL;
201 goto err;
202 }
203
204 tegra_alc5632_dai.cpu_dai_of_node = of_parse_phandle(
205 pdev->dev.of_node, "nvidia,i2s-controller", 0);
206 if (!tegra_alc5632_dai.cpu_dai_of_node) {
207 dev_err(&pdev->dev,
208 "Property 'nvidia,i2s-controller' missing or invalid\n");
209 ret = -EINVAL;
210 goto err;
211 }
212
Stephen Warren518de862012-03-20 14:55:49 -0600213 tegra_alc5632_dai.platform_of_node = tegra_alc5632_dai.cpu_dai_of_node;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200214
215 ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev);
216 if (ret)
Stephen Warren518de862012-03-20 14:55:49 -0600217 goto err;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200218
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200219 ret = snd_soc_register_card(card);
220 if (ret) {
221 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
222 ret);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200223 goto err_fini_utils;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200224 }
225
226 return 0;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200227
228err_fini_utils:
229 tegra_asoc_utils_fini(&alc5632->util_data);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200230err:
231 return ret;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200232}
233
234static int __devexit tegra_alc5632_remove(struct platform_device *pdev)
235{
236 struct snd_soc_card *card = platform_get_drvdata(pdev);
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200237 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card);
238
239 if (machine->gpio_requested & GPIO_HP_DET)
240 snd_soc_jack_free_gpios(&tegra_alc5632_hs_jack,
241 1,
242 &tegra_alc5632_hp_jack_gpio);
243 machine->gpio_requested = 0;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200244
245 snd_soc_unregister_card(card);
246
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200247 tegra_asoc_utils_fini(&machine->util_data);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200248
249 return 0;
250}
251
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200252static const struct of_device_id tegra_alc5632_of_match[] __devinitconst = {
253 { .compatible = "nvidia,tegra-audio-alc5632", },
254 {},
255};
256
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200257static struct platform_driver tegra_alc5632_driver = {
258 .driver = {
259 .name = DRV_NAME,
260 .owner = THIS_MODULE,
261 .pm = &snd_soc_pm_ops,
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200262 .of_match_table = tegra_alc5632_of_match,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200263 },
264 .probe = tegra_alc5632_probe,
265 .remove = __devexit_p(tegra_alc5632_remove),
266};
267module_platform_driver(tegra_alc5632_driver);
268
269MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
270MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver");
271MODULE_LICENSE("GPL");
272MODULE_ALIAS("platform:" DRV_NAME);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200273MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match);