blob: c61ea3a1030f7a683f55bd087049392fe4867bfb [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
Leon Romanovsky58783fa2011-12-19 21:51:52 +020016#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19#include <linux/gpio.h>
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020020#include <linux/of_gpio.h>
Leon Romanovsky58783fa2011-12-19 21:51:52 +020021
22#include <sound/core.h>
23#include <sound/jack.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/soc.h>
27
28#include "../codecs/alc5632.h"
29
Leon Romanovsky58783fa2011-12-19 21:51:52 +020030#include "tegra_asoc_utils.h"
31
32#define DRV_NAME "tegra-alc5632"
33
34struct tegra_alc5632 {
35 struct tegra_asoc_utils_data util_data;
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020036 int gpio_hp_det;
Leon Romanovsky58783fa2011-12-19 21:51:52 +020037};
38
39static int tegra_alc5632_asoc_hw_params(struct snd_pcm_substream *substream,
40 struct snd_pcm_hw_params *params)
41{
42 struct snd_soc_pcm_runtime *rtd = substream->private_data;
43 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Stephen Warren40db77a2012-06-06 17:15:49 -060044 struct snd_soc_codec *codec = codec_dai->codec;
Leon Romanovsky58783fa2011-12-19 21:51:52 +020045 struct snd_soc_card *card = codec->card;
46 struct tegra_alc5632 *alc5632 = snd_soc_card_get_drvdata(card);
47 int srate, mclk;
48 int err;
49
50 srate = params_rate(params);
51 mclk = 512 * srate;
52
53 err = tegra_asoc_utils_set_rate(&alc5632->util_data, srate, mclk);
54 if (err < 0) {
55 dev_err(card->dev, "Can't configure clocks\n");
56 return err;
57 }
58
59 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
60 SND_SOC_CLOCK_IN);
61 if (err < 0) {
62 dev_err(card->dev, "codec_dai clock not set\n");
63 return err;
64 }
65
66 return 0;
67}
68
69static struct snd_soc_ops tegra_alc5632_asoc_ops = {
70 .hw_params = tegra_alc5632_asoc_hw_params,
71};
72
73static struct snd_soc_jack tegra_alc5632_hs_jack;
74
75static struct snd_soc_jack_pin tegra_alc5632_hs_jack_pins[] = {
76 {
77 .pin = "Headset Mic",
78 .mask = SND_JACK_MICROPHONE,
79 },
80 {
81 .pin = "Headset Stereophone",
82 .mask = SND_JACK_HEADPHONE,
83 },
84};
85
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020086static struct snd_soc_jack_gpio tegra_alc5632_hp_jack_gpio = {
87 .name = "Headset detection",
88 .report = SND_JACK_HEADSET,
89 .debounce_time = 150,
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020090};
91
Leon Romanovsky58783fa2011-12-19 21:51:52 +020092static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = {
93 SND_SOC_DAPM_SPK("Int Spk", NULL),
94 SND_SOC_DAPM_HP("Headset Stereophone", NULL),
95 SND_SOC_DAPM_MIC("Headset Mic", NULL),
Leon Romanovskydd7d3a22012-02-13 21:27:36 +020096 SND_SOC_DAPM_MIC("Digital Mic", NULL),
Leon Romanovsky58783fa2011-12-19 21:51:52 +020097};
98
Leon Romanovsky58783fa2011-12-19 21:51:52 +020099static const struct snd_kcontrol_new tegra_alc5632_controls[] = {
100 SOC_DAPM_PIN_SWITCH("Int Spk"),
101};
102
103static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd)
104{
Stephen Warren40db77a2012-06-06 17:15:49 -0600105 struct snd_soc_dai *codec_dai = rtd->codec_dai;
106 struct snd_soc_codec *codec = codec_dai->codec;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200107 struct snd_soc_dapm_context *dapm = &codec->dapm;
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200108 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(codec->card);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200109
110 snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
111 &tegra_alc5632_hs_jack);
112 snd_soc_jack_add_pins(&tegra_alc5632_hs_jack,
113 ARRAY_SIZE(tegra_alc5632_hs_jack_pins),
114 tegra_alc5632_hs_jack_pins);
115
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200116 if (gpio_is_valid(machine->gpio_hp_det)) {
117 tegra_alc5632_hp_jack_gpio.gpio = machine->gpio_hp_det;
118 snd_soc_jack_add_gpios(&tegra_alc5632_hs_jack,
119 1,
120 &tegra_alc5632_hp_jack_gpio);
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200121 }
122
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200123 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
124
125 return 0;
126}
127
128static struct snd_soc_dai_link tegra_alc5632_dai = {
129 .name = "ALC5632",
130 .stream_name = "ALC5632 PCM",
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200131 .codec_dai_name = "alc5632-hifi",
132 .init = tegra_alc5632_asoc_init,
133 .ops = &tegra_alc5632_asoc_ops,
134 .dai_fmt = SND_SOC_DAIFMT_I2S
135 | SND_SOC_DAIFMT_NB_NF
136 | SND_SOC_DAIFMT_CBS_CFS,
137};
138
139static struct snd_soc_card snd_soc_tegra_alc5632 = {
140 .name = "tegra-alc5632",
Axel Linb16eaf92011-12-22 21:23:01 +0800141 .owner = THIS_MODULE,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200142 .dai_link = &tegra_alc5632_dai,
143 .num_links = 1,
144 .controls = tegra_alc5632_controls,
145 .num_controls = ARRAY_SIZE(tegra_alc5632_controls),
146 .dapm_widgets = tegra_alc5632_dapm_widgets,
147 .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets),
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200148 .fully_routed = true,
149};
150
Bill Pemberton4652a0d2012-12-07 09:26:33 -0500151static int tegra_alc5632_probe(struct platform_device *pdev)
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200152{
Stephen Warrenaef9a372012-05-22 16:09:51 -0600153 struct device_node *np = pdev->dev.of_node;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200154 struct snd_soc_card *card = &snd_soc_tegra_alc5632;
155 struct tegra_alc5632 *alc5632;
156 int ret;
157
158 alc5632 = devm_kzalloc(&pdev->dev,
159 sizeof(struct tegra_alc5632), GFP_KERNEL);
160 if (!alc5632) {
161 dev_err(&pdev->dev, "Can't allocate tegra_alc5632\n");
Stephen Warrenf7265362013-02-15 17:07:33 -0700162 return -ENOMEM;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200163 }
164
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200165 card->dev = &pdev->dev;
166 platform_set_drvdata(pdev, card);
167 snd_soc_card_set_drvdata(card, alc5632);
168
Stephen Warrenaef9a372012-05-22 16:09:51 -0600169 alc5632->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
Roland Stiggebbfc3282012-06-18 18:42:22 +0200170 if (alc5632->gpio_hp_det == -EPROBE_DEFER)
Stephen Warrenaef9a372012-05-22 16:09:51 -0600171 return -EPROBE_DEFER;
172
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200173 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
174 if (ret)
175 goto err;
176
177 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
178 if (ret)
179 goto err;
180
181 tegra_alc5632_dai.codec_of_node = of_parse_phandle(
182 pdev->dev.of_node, "nvidia,audio-codec", 0);
183
184 if (!tegra_alc5632_dai.codec_of_node) {
185 dev_err(&pdev->dev,
186 "Property 'nvidia,audio-codec' missing or invalid\n");
187 ret = -EINVAL;
188 goto err;
189 }
190
Stephen Warrenf7265362013-02-15 17:07:33 -0700191 tegra_alc5632_dai.cpu_of_node = of_parse_phandle(np,
192 "nvidia,i2s-controller", 0);
Stephen Warrenbc926572012-05-25 18:22:11 -0600193 if (!tegra_alc5632_dai.cpu_of_node) {
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200194 dev_err(&pdev->dev,
Stephen Warrenf7265362013-02-15 17:07:33 -0700195 "Property 'nvidia,i2s-controller' missing or invalid\n");
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200196 ret = -EINVAL;
197 goto err;
198 }
199
Stephen Warrenbc926572012-05-25 18:22:11 -0600200 tegra_alc5632_dai.platform_of_node = tegra_alc5632_dai.cpu_of_node;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200201
202 ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev);
203 if (ret)
Stephen Warren518de862012-03-20 14:55:49 -0600204 goto err;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200205
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200206 ret = snd_soc_register_card(card);
207 if (ret) {
208 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
209 ret);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200210 goto err_fini_utils;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200211 }
212
213 return 0;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200214
215err_fini_utils:
216 tegra_asoc_utils_fini(&alc5632->util_data);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200217err:
218 return ret;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200219}
220
Bill Pemberton4652a0d2012-12-07 09:26:33 -0500221static int tegra_alc5632_remove(struct platform_device *pdev)
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200222{
223 struct snd_soc_card *card = platform_get_drvdata(pdev);
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200224 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card);
225
Stephen Warren9f6328d2012-05-22 16:09:52 -0600226 snd_soc_jack_free_gpios(&tegra_alc5632_hs_jack, 1,
227 &tegra_alc5632_hp_jack_gpio);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200228
229 snd_soc_unregister_card(card);
230
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200231 tegra_asoc_utils_fini(&machine->util_data);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200232
233 return 0;
234}
235
Bill Pembertonf6e65742012-11-19 13:25:33 -0500236static const struct of_device_id tegra_alc5632_of_match[] = {
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200237 { .compatible = "nvidia,tegra-audio-alc5632", },
238 {},
239};
240
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200241static struct platform_driver tegra_alc5632_driver = {
242 .driver = {
243 .name = DRV_NAME,
244 .owner = THIS_MODULE,
245 .pm = &snd_soc_pm_ops,
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200246 .of_match_table = tegra_alc5632_of_match,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200247 },
248 .probe = tegra_alc5632_probe,
Bill Pemberton4652a0d2012-12-07 09:26:33 -0500249 .remove = tegra_alc5632_remove,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200250};
251module_platform_driver(tegra_alc5632_driver);
252
253MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
254MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver");
255MODULE_LICENSE("GPL");
256MODULE_ALIAS("platform:" DRV_NAME);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200257MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match);