blob: 396181cd2c786e3e8d312e35208d51a85def4c60 [file] [log] [blame]
Leon Romanovsky58783fa2011-12-19 21:51:52 +02001/*
Stephen Warren1ae93b92012-03-20 14:55:48 -06002 * tegra_alc5632.c -- Toshiba AC100(PAZ00) machine ASoC driver
3 *
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
32#include "tegra_das.h"
33#include "tegra_i2s.h"
34#include "tegra_pcm.h"
35#include "tegra_asoc_utils.h"
36
37#define DRV_NAME "tegra-alc5632"
38
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020039#define GPIO_HP_DET BIT(0)
40
Leon Romanovsky58783fa2011-12-19 21:51:52 +020041struct tegra_alc5632 {
42 struct tegra_asoc_utils_data util_data;
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020043 int gpio_requested;
44 int gpio_hp_det;
Leon Romanovsky58783fa2011-12-19 21:51:52 +020045};
46
47static int tegra_alc5632_asoc_hw_params(struct snd_pcm_substream *substream,
48 struct snd_pcm_hw_params *params)
49{
50 struct snd_soc_pcm_runtime *rtd = substream->private_data;
51 struct snd_soc_dai *codec_dai = rtd->codec_dai;
52 struct snd_soc_codec *codec = rtd->codec;
53 struct snd_soc_card *card = codec->card;
54 struct tegra_alc5632 *alc5632 = snd_soc_card_get_drvdata(card);
55 int srate, mclk;
56 int err;
57
58 srate = params_rate(params);
59 mclk = 512 * srate;
60
61 err = tegra_asoc_utils_set_rate(&alc5632->util_data, srate, mclk);
62 if (err < 0) {
63 dev_err(card->dev, "Can't configure clocks\n");
64 return err;
65 }
66
67 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
68 SND_SOC_CLOCK_IN);
69 if (err < 0) {
70 dev_err(card->dev, "codec_dai clock not set\n");
71 return err;
72 }
73
74 return 0;
75}
76
77static struct snd_soc_ops tegra_alc5632_asoc_ops = {
78 .hw_params = tegra_alc5632_asoc_hw_params,
79};
80
81static struct snd_soc_jack tegra_alc5632_hs_jack;
82
83static struct snd_soc_jack_pin tegra_alc5632_hs_jack_pins[] = {
84 {
85 .pin = "Headset Mic",
86 .mask = SND_JACK_MICROPHONE,
87 },
88 {
89 .pin = "Headset Stereophone",
90 .mask = SND_JACK_HEADPHONE,
91 },
92};
93
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020094static struct snd_soc_jack_gpio tegra_alc5632_hp_jack_gpio = {
95 .name = "Headset detection",
96 .report = SND_JACK_HEADSET,
97 .debounce_time = 150,
98 .invert = 1,
99};
100
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200101static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = {
102 SND_SOC_DAPM_SPK("Int Spk", NULL),
103 SND_SOC_DAPM_HP("Headset Stereophone", NULL),
104 SND_SOC_DAPM_MIC("Headset Mic", NULL),
Leon Romanovskydd7d3a22012-02-13 21:27:36 +0200105 SND_SOC_DAPM_MIC("Digital Mic", NULL),
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200106};
107
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200108static const struct snd_kcontrol_new tegra_alc5632_controls[] = {
109 SOC_DAPM_PIN_SWITCH("Int Spk"),
110};
111
112static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd)
113{
114 struct snd_soc_codec *codec = rtd->codec;
115 struct snd_soc_dapm_context *dapm = &codec->dapm;
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200116 struct device_node *np = codec->card->dev->of_node;
117 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(codec->card);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200118
119 snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
120 &tegra_alc5632_hs_jack);
121 snd_soc_jack_add_pins(&tegra_alc5632_hs_jack,
122 ARRAY_SIZE(tegra_alc5632_hs_jack_pins),
123 tegra_alc5632_hs_jack_pins);
124
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200125 machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
126
127 if (gpio_is_valid(machine->gpio_hp_det)) {
128 tegra_alc5632_hp_jack_gpio.gpio = machine->gpio_hp_det;
129 snd_soc_jack_add_gpios(&tegra_alc5632_hs_jack,
130 1,
131 &tegra_alc5632_hp_jack_gpio);
132 machine->gpio_requested |= GPIO_HP_DET;
133 }
134
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200135 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
136
137 return 0;
138}
139
140static struct snd_soc_dai_link tegra_alc5632_dai = {
141 .name = "ALC5632",
142 .stream_name = "ALC5632 PCM",
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200143 .codec_dai_name = "alc5632-hifi",
144 .init = tegra_alc5632_asoc_init,
145 .ops = &tegra_alc5632_asoc_ops,
146 .dai_fmt = SND_SOC_DAIFMT_I2S
147 | SND_SOC_DAIFMT_NB_NF
148 | SND_SOC_DAIFMT_CBS_CFS,
149};
150
151static struct snd_soc_card snd_soc_tegra_alc5632 = {
152 .name = "tegra-alc5632",
Axel Linb16eaf92011-12-22 21:23:01 +0800153 .owner = THIS_MODULE,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200154 .dai_link = &tegra_alc5632_dai,
155 .num_links = 1,
156 .controls = tegra_alc5632_controls,
157 .num_controls = ARRAY_SIZE(tegra_alc5632_controls),
158 .dapm_widgets = tegra_alc5632_dapm_widgets,
159 .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets),
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200160 .fully_routed = true,
161};
162
163static __devinit int tegra_alc5632_probe(struct platform_device *pdev)
164{
165 struct snd_soc_card *card = &snd_soc_tegra_alc5632;
166 struct tegra_alc5632 *alc5632;
167 int ret;
168
169 alc5632 = devm_kzalloc(&pdev->dev,
170 sizeof(struct tegra_alc5632), GFP_KERNEL);
171 if (!alc5632) {
172 dev_err(&pdev->dev, "Can't allocate tegra_alc5632\n");
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200173 ret = -ENOMEM;
174 goto err;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200175 }
176
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200177 card->dev = &pdev->dev;
178 platform_set_drvdata(pdev, card);
179 snd_soc_card_set_drvdata(card, alc5632);
180
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200181 if (!(pdev->dev.of_node)) {
182 dev_err(&pdev->dev, "Must be instantiated using device tree\n");
183 ret = -EINVAL;
184 goto err;
185 }
186
187 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
188 if (ret)
189 goto err;
190
191 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
192 if (ret)
193 goto err;
194
195 tegra_alc5632_dai.codec_of_node = of_parse_phandle(
196 pdev->dev.of_node, "nvidia,audio-codec", 0);
197
198 if (!tegra_alc5632_dai.codec_of_node) {
199 dev_err(&pdev->dev,
200 "Property 'nvidia,audio-codec' missing or invalid\n");
201 ret = -EINVAL;
202 goto err;
203 }
204
205 tegra_alc5632_dai.cpu_dai_of_node = of_parse_phandle(
206 pdev->dev.of_node, "nvidia,i2s-controller", 0);
207 if (!tegra_alc5632_dai.cpu_dai_of_node) {
208 dev_err(&pdev->dev,
209 "Property 'nvidia,i2s-controller' missing or invalid\n");
210 ret = -EINVAL;
211 goto err;
212 }
213
Stephen Warren518de862012-03-20 14:55:49 -0600214 tegra_alc5632_dai.platform_of_node = tegra_alc5632_dai.cpu_dai_of_node;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200215
216 ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev);
217 if (ret)
Stephen Warren518de862012-03-20 14:55:49 -0600218 goto err;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200219
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200220 ret = snd_soc_register_card(card);
221 if (ret) {
222 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
223 ret);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200224 goto err_fini_utils;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200225 }
226
227 return 0;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200228
229err_fini_utils:
230 tegra_asoc_utils_fini(&alc5632->util_data);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200231err:
232 return ret;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200233}
234
235static int __devexit tegra_alc5632_remove(struct platform_device *pdev)
236{
237 struct snd_soc_card *card = platform_get_drvdata(pdev);
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200238 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card);
239
240 if (machine->gpio_requested & GPIO_HP_DET)
241 snd_soc_jack_free_gpios(&tegra_alc5632_hs_jack,
242 1,
243 &tegra_alc5632_hp_jack_gpio);
244 machine->gpio_requested = 0;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200245
246 snd_soc_unregister_card(card);
247
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200248 tegra_asoc_utils_fini(&machine->util_data);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200249
250 return 0;
251}
252
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200253static const struct of_device_id tegra_alc5632_of_match[] __devinitconst = {
254 { .compatible = "nvidia,tegra-audio-alc5632", },
255 {},
256};
257
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200258static struct platform_driver tegra_alc5632_driver = {
259 .driver = {
260 .name = DRV_NAME,
261 .owner = THIS_MODULE,
262 .pm = &snd_soc_pm_ops,
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200263 .of_match_table = tegra_alc5632_of_match,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200264 },
265 .probe = tegra_alc5632_probe,
266 .remove = __devexit_p(tegra_alc5632_remove),
267};
268module_platform_driver(tegra_alc5632_driver);
269
270MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
271MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver");
272MODULE_LICENSE("GPL");
273MODULE_ALIAS("platform:" DRV_NAME);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200274MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match);