blob: 8e774d1a243c624dcca5b09c25065316629d886e [file] [log] [blame]
Stephen Warrend8259ca2012-05-17 16:59:51 -06001/*
2 * tegra_wm8753.c - Tegra machine ASoC driver for boards using WM8753 codec.
3 *
4 * Author: Stephen Warren <swarren@nvidia.com>
5 * Copyright (C) 2010-2012 - NVIDIA, Inc.
6 *
7 * Based on code copyright/by:
8 *
9 * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
10 *
11 * Copyright 2007 Wolfson Microelectronics PLC.
12 * Author: Graeme Gregory
13 * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * version 2 as published by the Free Software Foundation.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27 * 02110-1301 USA
28 *
29 */
30
Stephen Warrend8259ca2012-05-17 16:59:51 -060031#include <linux/module.h>
32#include <linux/platform_device.h>
33#include <linux/slab.h>
34#include <linux/gpio.h>
35#include <linux/of_gpio.h>
36
37#include <sound/core.h>
38#include <sound/jack.h>
39#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/soc.h>
42
43#include "../codecs/wm8753.h"
44
45#include "tegra_asoc_utils.h"
46
47#define DRV_NAME "tegra-snd-wm8753"
48
49struct tegra_wm8753 {
50 struct tegra_asoc_utils_data util_data;
51};
52
53static int tegra_wm8753_hw_params(struct snd_pcm_substream *substream,
54 struct snd_pcm_hw_params *params)
55{
56 struct snd_soc_pcm_runtime *rtd = substream->private_data;
57 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Stephen Warren40db77a2012-06-06 17:15:49 -060058 struct snd_soc_codec *codec = codec_dai->codec;
Stephen Warrend8259ca2012-05-17 16:59:51 -060059 struct snd_soc_card *card = codec->card;
60 struct tegra_wm8753 *machine = snd_soc_card_get_drvdata(card);
61 int srate, mclk;
62 int err;
63
64 srate = params_rate(params);
65 switch (srate) {
66 case 11025:
67 case 22050:
68 case 44100:
69 case 88200:
70 mclk = 11289600;
71 break;
72 default:
73 mclk = 12288000;
74 break;
75 }
76
77 err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
78 if (err < 0) {
79 dev_err(card->dev, "Can't configure clocks\n");
80 return err;
81 }
82
83 err = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, mclk,
84 SND_SOC_CLOCK_IN);
85 if (err < 0) {
86 dev_err(card->dev, "codec_dai clock not set\n");
87 return err;
88 }
89
90 return 0;
91}
92
93static struct snd_soc_ops tegra_wm8753_ops = {
94 .hw_params = tegra_wm8753_hw_params,
95};
96
97static const struct snd_soc_dapm_widget tegra_wm8753_dapm_widgets[] = {
98 SND_SOC_DAPM_HP("Headphone Jack", NULL),
99 SND_SOC_DAPM_MIC("Mic Jack", NULL),
100};
101
102static struct snd_soc_dai_link tegra_wm8753_dai = {
103 .name = "WM8753",
104 .stream_name = "WM8753 PCM",
105 .codec_dai_name = "wm8753-hifi",
106 .ops = &tegra_wm8753_ops,
107 .dai_fmt = SND_SOC_DAIFMT_I2S |
108 SND_SOC_DAIFMT_NB_NF |
109 SND_SOC_DAIFMT_CBS_CFS,
110};
111
112static struct snd_soc_card snd_soc_tegra_wm8753 = {
113 .name = "tegra-wm8753",
114 .owner = THIS_MODULE,
115 .dai_link = &tegra_wm8753_dai,
116 .num_links = 1,
117
118 .dapm_widgets = tegra_wm8753_dapm_widgets,
119 .num_dapm_widgets = ARRAY_SIZE(tegra_wm8753_dapm_widgets),
120 .fully_routed = true,
121};
122
Bill Pemberton4652a0d2012-12-07 09:26:33 -0500123static int tegra_wm8753_driver_probe(struct platform_device *pdev)
Stephen Warrend8259ca2012-05-17 16:59:51 -0600124{
Stephen Warrenbddd7d02013-02-15 17:07:31 -0700125 struct device_node *np = pdev->dev.of_node;
Stephen Warrend8259ca2012-05-17 16:59:51 -0600126 struct snd_soc_card *card = &snd_soc_tegra_wm8753;
127 struct tegra_wm8753 *machine;
128 int ret;
129
130 machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm8753),
131 GFP_KERNEL);
132 if (!machine) {
133 dev_err(&pdev->dev, "Can't allocate tegra_wm8753 struct\n");
Stephen Warrenbddd7d02013-02-15 17:07:31 -0700134 return -ENOMEM;
Stephen Warrend8259ca2012-05-17 16:59:51 -0600135 }
136
137 card->dev = &pdev->dev;
138 platform_set_drvdata(pdev, card);
139 snd_soc_card_set_drvdata(card, machine);
140
141 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
142 if (ret)
143 goto err;
144
145 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
146 if (ret)
147 goto err;
148
Stephen Warrenbddd7d02013-02-15 17:07:31 -0700149 tegra_wm8753_dai.codec_of_node = of_parse_phandle(np,
150 "nvidia,audio-codec", 0);
Stephen Warrend8259ca2012-05-17 16:59:51 -0600151 if (!tegra_wm8753_dai.codec_of_node) {
152 dev_err(&pdev->dev,
153 "Property 'nvidia,audio-codec' missing or invalid\n");
154 ret = -EINVAL;
155 goto err;
156 }
157
Stephen Warrenbddd7d02013-02-15 17:07:31 -0700158 tegra_wm8753_dai.cpu_of_node = of_parse_phandle(np,
159 "nvidia,i2s-controller", 0);
Stephen Warrenbc926572012-05-25 18:22:11 -0600160 if (!tegra_wm8753_dai.cpu_of_node) {
Stephen Warrend8259ca2012-05-17 16:59:51 -0600161 dev_err(&pdev->dev,
162 "Property 'nvidia,i2s-controller' missing or invalid\n");
163 ret = -EINVAL;
164 goto err;
165 }
166
Stephen Warrenbddd7d02013-02-15 17:07:31 -0700167 tegra_wm8753_dai.platform_of_node = tegra_wm8753_dai.cpu_of_node;
Stephen Warrend8259ca2012-05-17 16:59:51 -0600168
169 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
170 if (ret)
171 goto err;
172
173 ret = snd_soc_register_card(card);
174 if (ret) {
175 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
176 ret);
177 goto err_fini_utils;
178 }
179
180 return 0;
181
182err_fini_utils:
183 tegra_asoc_utils_fini(&machine->util_data);
184err:
185 return ret;
186}
187
Bill Pemberton4652a0d2012-12-07 09:26:33 -0500188static int tegra_wm8753_driver_remove(struct platform_device *pdev)
Stephen Warrend8259ca2012-05-17 16:59:51 -0600189{
190 struct snd_soc_card *card = platform_get_drvdata(pdev);
191 struct tegra_wm8753 *machine = snd_soc_card_get_drvdata(card);
192
193 snd_soc_unregister_card(card);
194
195 tegra_asoc_utils_fini(&machine->util_data);
196
197 return 0;
198}
199
Bill Pembertonf6e65742012-11-19 13:25:33 -0500200static const struct of_device_id tegra_wm8753_of_match[] = {
Stephen Warrend8259ca2012-05-17 16:59:51 -0600201 { .compatible = "nvidia,tegra-audio-wm8753", },
202 {},
203};
204
205static struct platform_driver tegra_wm8753_driver = {
206 .driver = {
207 .name = DRV_NAME,
208 .owner = THIS_MODULE,
209 .pm = &snd_soc_pm_ops,
210 .of_match_table = tegra_wm8753_of_match,
211 },
212 .probe = tegra_wm8753_driver_probe,
Bill Pemberton4652a0d2012-12-07 09:26:33 -0500213 .remove = tegra_wm8753_driver_remove,
Stephen Warrend8259ca2012-05-17 16:59:51 -0600214};
215module_platform_driver(tegra_wm8753_driver);
216
217MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
218MODULE_DESCRIPTION("Tegra+WM8753 machine ASoC driver");
219MODULE_LICENSE("GPL");
220MODULE_ALIAS("platform:" DRV_NAME);
221MODULE_DEVICE_TABLE(of, tegra_wm8753_of_match);