blob: fb550b5869d21f73307d36d2edc88ebcffa61f0c [file] [log] [blame]
Kuninori Morimotof2390882012-04-08 21:17:50 -07001/*
2 * ASoC simple sound card support
3 *
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
Kuninori Morimotofa558c22013-11-20 15:25:02 +090011#include <linux/clk.h>
Xiubo Li6ff62ee2014-02-14 09:34:36 +080012#include <linux/device.h>
Dylan Reid3fe24032014-10-01 14:25:20 -070013#include <linux/gpio.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070014#include <linux/module.h>
Kuninori Morimotofa558c22013-11-20 15:25:02 +090015#include <linux/of.h>
Dylan Reid3fe24032014-10-01 14:25:20 -070016#include <linux/of_gpio.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070017#include <linux/platform_device.h>
Xiubo Lica919fe2014-01-14 12:35:32 +080018#include <linux/string.h>
Dylan Reid3fe24032014-10-01 14:25:20 -070019#include <sound/jack.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070020#include <sound/simple_card.h>
Xiubo Li6ff62ee2014-02-14 09:34:36 +080021#include <sound/soc-dai.h>
22#include <sound/soc.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070023
Jean-Francois Moine45fce592014-01-15 16:51:56 +010024struct simple_card_data {
25 struct snd_soc_card snd_card;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +010026 struct simple_dai_props {
27 struct asoc_simple_dai cpu_dai;
28 struct asoc_simple_dai codec_dai;
29 } *dai_props;
Andrew Lunn2942a0e2014-05-22 17:31:49 +020030 unsigned int mclk_fs;
Dylan Reid3fe24032014-10-01 14:25:20 -070031 int gpio_hp_det;
Jianqun44761592014-11-01 11:22:18 +080032 int gpio_hp_det_invert;
Dylan Reid3fe24032014-10-01 14:25:20 -070033 int gpio_mic_det;
Jianqun44761592014-11-01 11:22:18 +080034 int gpio_mic_det_invert;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +010035 struct snd_soc_dai_link dai_link[]; /* dynamically allocated */
Jean-Francois Moine45fce592014-01-15 16:51:56 +010036};
37
Kuninori Morimotof531913f2014-09-09 21:37:57 -070038#define simple_priv_to_dev(priv) ((priv)->snd_card.dev)
Kuninori Morimoto9810f532014-09-09 21:38:24 -070039#define simple_priv_to_link(priv, i) ((priv)->snd_card.dai_link + i)
40#define simple_priv_to_props(priv, i) ((priv)->dai_props + i)
Kuninori Morimotof531913f2014-09-09 21:37:57 -070041
Jyri Sarhaf9911802015-01-13 21:16:34 +020042static int asoc_simple_card_startup(struct snd_pcm_substream *substream)
43{
44 struct snd_soc_pcm_runtime *rtd = substream->private_data;
45 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
46 struct simple_dai_props *dai_props =
47 &priv->dai_props[rtd - rtd->card->rtd];
48 int ret;
49
50 ret = clk_prepare_enable(dai_props->cpu_dai.clk);
51 if (ret)
52 return ret;
53
54 ret = clk_prepare_enable(dai_props->codec_dai.clk);
55 if (ret)
56 clk_disable_unprepare(dai_props->cpu_dai.clk);
57
58 return ret;
59}
60
61static void asoc_simple_card_shutdown(struct snd_pcm_substream *substream)
62{
63 struct snd_soc_pcm_runtime *rtd = substream->private_data;
64 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
65 struct simple_dai_props *dai_props =
66 &priv->dai_props[rtd - rtd->card->rtd];
67
68 clk_disable_unprepare(dai_props->cpu_dai.clk);
69
70 clk_disable_unprepare(dai_props->codec_dai.clk);
71}
72
Andrew Lunn2942a0e2014-05-22 17:31:49 +020073static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream,
74 struct snd_pcm_hw_params *params)
75{
76 struct snd_soc_pcm_runtime *rtd = substream->private_data;
77 struct snd_soc_dai *codec_dai = rtd->codec_dai;
78 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
79 unsigned int mclk;
80 int ret = 0;
81
82 if (priv->mclk_fs) {
83 mclk = params_rate(params) * priv->mclk_fs;
84 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
85 SND_SOC_CLOCK_IN);
86 }
87
88 return ret;
89}
90
91static struct snd_soc_ops asoc_simple_card_ops = {
Jyri Sarhaf9911802015-01-13 21:16:34 +020092 .startup = asoc_simple_card_startup,
93 .shutdown = asoc_simple_card_shutdown,
Andrew Lunn2942a0e2014-05-22 17:31:49 +020094 .hw_params = asoc_simple_card_hw_params,
95};
96
Dylan Reid3fe24032014-10-01 14:25:20 -070097static struct snd_soc_jack simple_card_hp_jack;
98static struct snd_soc_jack_pin simple_card_hp_jack_pins[] = {
99 {
100 .pin = "Headphones",
101 .mask = SND_JACK_HEADPHONE,
102 },
103};
104static struct snd_soc_jack_gpio simple_card_hp_jack_gpio = {
105 .name = "Headphone detection",
106 .report = SND_JACK_HEADPHONE,
107 .debounce_time = 150,
108};
109
110static struct snd_soc_jack simple_card_mic_jack;
111static struct snd_soc_jack_pin simple_card_mic_jack_pins[] = {
112 {
113 .pin = "Mic Jack",
114 .mask = SND_JACK_MICROPHONE,
115 },
116};
117static struct snd_soc_jack_gpio simple_card_mic_jack_gpio = {
118 .name = "Mic detection",
119 .report = SND_JACK_MICROPHONE,
120 .debounce_time = 150,
121};
122
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800123static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
Xiubo Li30d03412014-01-24 15:43:01 +0800124 struct asoc_simple_dai *set)
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800125{
Xiubo Li4763ebe2014-01-24 15:43:00 +0800126 int ret;
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800127
Xiubo Li30d03412014-01-24 15:43:01 +0800128 if (set->fmt) {
129 ret = snd_soc_dai_set_fmt(dai, set->fmt);
Xiubo Li4763ebe2014-01-24 15:43:00 +0800130 if (ret && ret != -ENOTSUPP) {
131 dev_err(dai->dev, "simple-card: set_fmt error\n");
132 goto err;
133 }
Kuninori Morimotoe244bb92013-10-17 22:46:49 -0700134 }
135
Xiubo Li4763ebe2014-01-24 15:43:00 +0800136 if (set->sysclk) {
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800137 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
Xiubo Li4763ebe2014-01-24 15:43:00 +0800138 if (ret && ret != -ENOTSUPP) {
139 dev_err(dai->dev, "simple-card: set_sysclk error\n");
140 goto err;
141 }
142 }
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800143
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800144 if (set->slots) {
145 ret = snd_soc_dai_set_tdm_slot(dai, 0, 0,
146 set->slots,
147 set->slot_width);
148 if (ret && ret != -ENOTSUPP) {
149 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
150 goto err;
151 }
152 }
153
Xiubo Li4763ebe2014-01-24 15:43:00 +0800154 ret = 0;
155
156err:
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800157 return ret;
158}
159
Kuninori Morimotof2390882012-04-08 21:17:50 -0700160static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
161{
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800162 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimotof2390882012-04-08 21:17:50 -0700163 struct snd_soc_dai *codec = rtd->codec_dai;
164 struct snd_soc_dai *cpu = rtd->cpu_dai;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100165 struct simple_dai_props *dai_props;
166 int num, ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700167
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100168 num = rtd - rtd->card->rtd;
169 dai_props = &priv->dai_props[num];
170 ret = __asoc_simple_card_dai_init(codec, &dai_props->codec_dai);
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800171 if (ret < 0)
172 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700173
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100174 ret = __asoc_simple_card_dai_init(cpu, &dai_props->cpu_dai);
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800175 if (ret < 0)
176 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700177
Dylan Reid3fe24032014-10-01 14:25:20 -0700178 if (gpio_is_valid(priv->gpio_hp_det)) {
179 snd_soc_jack_new(codec->codec, "Headphones", SND_JACK_HEADPHONE,
180 &simple_card_hp_jack);
181 snd_soc_jack_add_pins(&simple_card_hp_jack,
182 ARRAY_SIZE(simple_card_hp_jack_pins),
183 simple_card_hp_jack_pins);
184
185 simple_card_hp_jack_gpio.gpio = priv->gpio_hp_det;
Jianqun44761592014-11-01 11:22:18 +0800186 simple_card_hp_jack_gpio.invert = priv->gpio_hp_det_invert;
Dylan Reid3fe24032014-10-01 14:25:20 -0700187 snd_soc_jack_add_gpios(&simple_card_hp_jack, 1,
188 &simple_card_hp_jack_gpio);
189 }
190
191 if (gpio_is_valid(priv->gpio_mic_det)) {
192 snd_soc_jack_new(codec->codec, "Mic Jack", SND_JACK_MICROPHONE,
193 &simple_card_mic_jack);
194 snd_soc_jack_add_pins(&simple_card_mic_jack,
195 ARRAY_SIZE(simple_card_mic_jack_pins),
196 simple_card_mic_jack_pins);
197 simple_card_mic_jack_gpio.gpio = priv->gpio_mic_det;
Jianqun44761592014-11-01 11:22:18 +0800198 simple_card_mic_jack_gpio.invert = priv->gpio_mic_det_invert;
Dylan Reid3fe24032014-10-01 14:25:20 -0700199 snd_soc_jack_add_gpios(&simple_card_mic_jack, 1,
200 &simple_card_mic_jack_gpio);
201 }
Kuninori Morimotof2390882012-04-08 21:17:50 -0700202 return 0;
203}
204
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900205static int
206asoc_simple_card_sub_parse_of(struct device_node *np,
207 struct asoc_simple_dai *dai,
Xiubo Li8ea21342014-08-18 15:00:15 +0800208 struct device_node **p_node,
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700209 const char **name,
210 int *args_count)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900211{
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700212 struct of_phandle_args args;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900213 struct clk *clk;
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300214 u32 val;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900215 int ret;
216
217 /*
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800218 * Get node via "sound-dai = <&phandle port>"
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900219 * it will be used as xxx_of_node on soc_bind_dai_link()
220 */
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700221 ret = of_parse_phandle_with_args(np, "sound-dai",
222 "#sound-dai-cells", 0, &args);
223 if (ret)
224 return ret;
225
226 *p_node = args.np;
227
228 if (args_count)
229 *args_count = args.args_count;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900230
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800231 /* Get dai->name */
Jean-Francois Moine52008472014-01-15 16:51:48 +0100232 ret = snd_soc_of_get_dai_name(np, name);
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900233 if (ret < 0)
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100234 return ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900235
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800236 /* Parse TDM slot */
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800237 ret = snd_soc_of_parse_tdm_slot(np, &dai->slots, &dai->slot_width);
238 if (ret)
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100239 return ret;
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800240
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900241 /*
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800242 * Parse dai->sysclk come from "clocks = <&xxx>"
243 * (if system has common clock)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900244 * or "system-clock-frequency = <xxx>"
Xiubo Li71467e42013-12-23 15:25:38 +0800245 * or device's module clock.
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900246 */
Xiubo Li71467e42013-12-23 15:25:38 +0800247 if (of_property_read_bool(np, "clocks")) {
248 clk = of_clk_get(np, 0);
249 if (IS_ERR(clk)) {
250 ret = PTR_ERR(clk);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100251 return ret;
Xiubo Li71467e42013-12-23 15:25:38 +0800252 }
253
254 dai->sysclk = clk_get_rate(clk);
Jyri Sarhaf9911802015-01-13 21:16:34 +0200255 dai->clk = clk;
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300256 } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) {
257 dai->sysclk = val;
Xiubo Li71467e42013-12-23 15:25:38 +0800258 } else {
Arnd Bergmann88a60e52014-09-08 13:14:05 +0200259 clk = of_clk_get(args.np, 0);
Xiubo Lie2a19ac2014-01-06 12:34:36 +0800260 if (!IS_ERR(clk))
261 dai->sysclk = clk_get_rate(clk);
Xiubo Li71467e42013-12-23 15:25:38 +0800262 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900263
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100264 return 0;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900265}
266
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700267static int asoc_simple_card_parse_daifmt(struct device_node *node,
268 struct simple_card_data *priv,
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700269 struct device_node *codec,
270 char *prefix, int idx)
271{
272 struct device *dev = simple_priv_to_dev(priv);
273 struct device_node *bitclkmaster = NULL;
274 struct device_node *framemaster = NULL;
275 struct simple_dai_props *dai_props = simple_priv_to_props(priv, idx);
276 struct asoc_simple_dai *cpu_dai = &dai_props->cpu_dai;
277 struct asoc_simple_dai *codec_dai = &dai_props->codec_dai;
278 unsigned int daifmt;
279
280 daifmt = snd_soc_of_parse_daifmt(node, prefix,
281 &bitclkmaster, &framemaster);
282 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
283
284 if (strlen(prefix) && !bitclkmaster && !framemaster) {
285 /*
286 * No dai-link level and master setting was not found from
287 * sound node level, revert back to legacy DT parsing and
288 * take the settings from codec node.
289 */
290 dev_dbg(dev, "Revert to legacy daifmt parsing\n");
291
292 cpu_dai->fmt = codec_dai->fmt =
293 snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) |
294 (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
295 } else {
296 if (codec == bitclkmaster)
297 daifmt |= (codec == framemaster) ?
298 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
299 else
300 daifmt |= (codec == framemaster) ?
301 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
302
303 cpu_dai->fmt = daifmt;
304 codec_dai->fmt = daifmt;
305 }
306
307 of_node_put(bitclkmaster);
308 of_node_put(framemaster);
309
310 return 0;
311}
312
Kuninori Morimoto2d82eeb2014-08-27 20:07:46 -0700313static int asoc_simple_card_dai_link_of(struct device_node *node,
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700314 struct simple_card_data *priv,
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700315 int idx,
Kuninori Morimoto2d82eeb2014-08-27 20:07:46 -0700316 bool is_top_level_node)
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100317{
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700318 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700319 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx);
320 struct simple_dai_props *dai_props = simple_priv_to_props(priv, idx);
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700321 struct device_node *cpu = NULL;
322 struct device_node *codec = NULL;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200323 char *name;
324 char prop[128];
325 char *prefix = "";
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700326 int ret, cpu_args;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100327
Xiubo Li20804372014-09-03 10:23:39 +0800328 /* For single DAI link & old style of DT node */
Jyri Sarha64872212014-04-24 19:42:00 +0300329 if (is_top_level_node)
330 prefix = "simple-audio-card,";
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100331
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200332 snprintf(prop, sizeof(prop), "%scpu", prefix);
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700333 cpu = of_get_child_by_name(node, prop);
334
335 snprintf(prop, sizeof(prop), "%scodec", prefix);
336 codec = of_get_child_by_name(node, prop);
337
338 if (!cpu || !codec) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200339 ret = -EINVAL;
Nicolin Chen966b8062014-04-24 19:13:59 +0800340 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200341 goto dai_link_of_err;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100342 }
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200343
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700344 ret = asoc_simple_card_parse_daifmt(node, priv,
Jean-Francois Moine7195d922014-11-25 13:22:45 +0100345 codec, prefix, idx);
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700346 if (ret < 0)
347 goto dai_link_of_err;
348
349 ret = asoc_simple_card_sub_parse_of(cpu, &dai_props->cpu_dai,
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200350 &dai_link->cpu_of_node,
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700351 &dai_link->cpu_dai_name,
352 &cpu_args);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200353 if (ret < 0)
354 goto dai_link_of_err;
355
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700356 ret = asoc_simple_card_sub_parse_of(codec, &dai_props->codec_dai,
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200357 &dai_link->codec_of_node,
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700358 &dai_link->codec_dai_name, NULL);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200359 if (ret < 0)
360 goto dai_link_of_err;
361
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200362 if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name) {
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800363 ret = -EINVAL;
364 goto dai_link_of_err;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200365 }
366
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800367 /* Simple Card assumes platform == cpu */
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200368 dai_link->platform_of_node = dai_link->cpu_of_node;
369
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800370 /* DAI link name is created from CPU/CODEC dai name */
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200371 name = devm_kzalloc(dev,
372 strlen(dai_link->cpu_dai_name) +
373 strlen(dai_link->codec_dai_name) + 2,
374 GFP_KERNEL);
Vishal Thanki31f30322015-03-03 18:59:00 +0530375 if (!name) {
376 ret = -ENOMEM;
377 goto dai_link_of_err;
378 }
379
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200380 sprintf(name, "%s-%s", dai_link->cpu_dai_name,
381 dai_link->codec_dai_name);
382 dai_link->name = dai_link->stream_name = name;
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200383 dai_link->ops = &asoc_simple_card_ops;
Kuninori Morimotoa5960bd2014-08-27 20:08:27 -0700384 dai_link->init = asoc_simple_card_dai_init;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200385
386 dev_dbg(dev, "\tname : %s\n", dai_link->stream_name);
387 dev_dbg(dev, "\tcpu : %s / %04x / %d\n",
388 dai_link->cpu_dai_name,
389 dai_props->cpu_dai.fmt,
390 dai_props->cpu_dai.sysclk);
391 dev_dbg(dev, "\tcodec : %s / %04x / %d\n",
392 dai_link->codec_dai_name,
393 dai_props->codec_dai.fmt,
394 dai_props->codec_dai.sysclk);
395
Kuninori Morimoto179949b2014-08-27 20:08:06 -0700396 /*
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800397 * In soc_bind_dai_link() will check cpu name after
398 * of_node matching if dai_link has cpu_dai_name.
399 * but, it will never match if name was created by
400 * fmt_single_name() remove cpu_dai_name if cpu_args
401 * was 0. See:
Kuninori Morimoto179949b2014-08-27 20:08:06 -0700402 * fmt_single_name()
403 * fmt_multiple_name()
404 */
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700405 if (!cpu_args)
406 dai_link->cpu_dai_name = NULL;
Kuninori Morimoto179949b2014-08-27 20:08:06 -0700407
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200408dai_link_of_err:
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700409 of_node_put(cpu);
410 of_node_put(codec);
411
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100412 return ret;
413}
414
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900415static int asoc_simple_card_parse_of(struct device_node *node,
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700416 struct simple_card_data *priv)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900417{
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700418 struct device *dev = simple_priv_to_dev(priv);
Jianqun44761592014-11-01 11:22:18 +0800419 enum of_gpio_flags flags;
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300420 u32 val;
Xiubo Lid4c22092013-12-23 12:57:01 +0800421 int ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900422
Xiubo Li20804372014-09-03 10:23:39 +0800423 if (!node)
424 return -EINVAL;
425
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800426 /* Parse the card name from DT */
Xiubo Li27725552014-01-24 15:43:02 +0800427 snd_soc_of_parse_card_name(&priv->snd_card, "simple-audio-card,name");
428
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800429 /* The off-codec widgets */
Xiubo Li9d681f52014-02-08 15:59:53 +0800430 if (of_property_read_bool(node, "simple-audio-card,widgets")) {
431 ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card,
432 "simple-audio-card,widgets");
433 if (ret)
434 return ret;
435 }
436
Xiubo Lid4c22092013-12-23 12:57:01 +0800437 /* DAPM routes */
Xiubo Li8c0b8232014-01-07 09:15:16 +0800438 if (of_property_read_bool(node, "simple-audio-card,routing")) {
Jean-Francois Moineb367a322014-01-15 16:52:00 +0100439 ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
Xiubo Li8c0b8232014-01-07 09:15:16 +0800440 "simple-audio-card,routing");
Xiubo Lif87a3e82014-01-07 09:13:42 +0800441 if (ret)
442 return ret;
443 }
Xiubo Lid4c22092013-12-23 12:57:01 +0800444
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200445 /* Factor to mclk, used in hw_params() */
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300446 ret = of_property_read_u32(node, "simple-audio-card,mclk-fs", &val);
447 if (ret == 0)
448 priv->mclk_fs = val;
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200449
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200450 dev_dbg(dev, "New simple-card: %s\n", priv->snd_card.name ?
451 priv->snd_card.name : "");
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100452
Xiubo Li20804372014-09-03 10:23:39 +0800453 /* Single/Muti DAI link(s) & New style of DT node */
454 if (of_get_child_by_name(node, "simple-audio-card,dai-link")) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200455 struct device_node *np = NULL;
Kuninori Morimotoa44a7502014-08-27 20:08:47 -0700456 int i = 0;
457
458 for_each_child_of_node(node, np) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200459 dev_dbg(dev, "\tlink %d:\n", i);
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700460 ret = asoc_simple_card_dai_link_of(np, priv,
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700461 i, false);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200462 if (ret < 0) {
463 of_node_put(np);
464 return ret;
465 }
Kuninori Morimotoa44a7502014-08-27 20:08:47 -0700466 i++;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200467 }
468 } else {
Xiubo Li20804372014-09-03 10:23:39 +0800469 /* For single DAI link & old style of DT node */
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700470 ret = asoc_simple_card_dai_link_of(node, priv, 0, true);
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100471 if (ret < 0)
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200472 return ret;
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100473 }
Xiubo Lidd41e0c2013-12-20 14:39:50 +0800474
Jianqun44761592014-11-01 11:22:18 +0800475 priv->gpio_hp_det = of_get_named_gpio_flags(node,
476 "simple-audio-card,hp-det-gpio", 0, &flags);
477 priv->gpio_hp_det_invert = !!(flags & OF_GPIO_ACTIVE_LOW);
Dylan Reid3fe24032014-10-01 14:25:20 -0700478 if (priv->gpio_hp_det == -EPROBE_DEFER)
479 return -EPROBE_DEFER;
480
Jianqun44761592014-11-01 11:22:18 +0800481 priv->gpio_mic_det = of_get_named_gpio_flags(node,
482 "simple-audio-card,mic-det-gpio", 0, &flags);
483 priv->gpio_mic_det_invert = !!(flags & OF_GPIO_ACTIVE_LOW);
Dylan Reid3fe24032014-10-01 14:25:20 -0700484 if (priv->gpio_mic_det == -EPROBE_DEFER)
485 return -EPROBE_DEFER;
486
Xiubo Li27725552014-01-24 15:43:02 +0800487 if (!priv->snd_card.name)
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200488 priv->snd_card.name = priv->snd_card.dai_link->name;
Kuninori Morimotof687d902014-02-27 18:25:24 -0800489
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900490 return 0;
491}
492
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800493/* Decrease the reference count of the device nodes */
Geert Uytterhoeven7ddfdb52015-01-13 21:03:37 +0100494static int asoc_simple_card_unref(struct snd_soc_card *card)
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100495{
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100496 struct snd_soc_dai_link *dai_link;
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100497 int num_links;
498
499 for (num_links = 0, dai_link = card->dai_link;
500 num_links < card->num_links;
501 num_links++, dai_link++) {
Jean-Francois Moine0099c762014-11-09 12:38:56 +0100502 of_node_put(dai_link->cpu_of_node);
503 of_node_put(dai_link->codec_of_node);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100504 }
505 return 0;
506}
507
Kuninori Morimotof2390882012-04-08 21:17:50 -0700508static int asoc_simple_card_probe(struct platform_device *pdev)
509{
Jean-Francois Moine45fce592014-01-15 16:51:56 +0100510 struct simple_card_data *priv;
Jean-Francois Moine5ca8ba42014-01-15 16:51:45 +0100511 struct snd_soc_dai_link *dai_link;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900512 struct device_node *np = pdev->dev.of_node;
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800513 struct device *dev = &pdev->dev;
Xiubo Li20804372014-09-03 10:23:39 +0800514 int num_links, ret;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100515
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800516 /* Get the number of DAI links */
Xiubo Li20804372014-09-03 10:23:39 +0800517 if (np && of_get_child_by_name(np, "simple-audio-card,dai-link"))
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100518 num_links = of_get_child_count(np);
Xiubo Li20804372014-09-03 10:23:39 +0800519 else
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100520 num_links = 1;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700521
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800522 /* Allocate the private data and the DAI link array */
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100523 priv = devm_kzalloc(dev,
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100524 sizeof(*priv) + sizeof(*dai_link) * num_links,
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100525 GFP_KERNEL);
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100526 if (!priv)
Xiubo Lica919fe2014-01-14 12:35:32 +0800527 return -ENOMEM;
528
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800529 /* Init snd_soc_card */
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100530 priv->snd_card.owner = THIS_MODULE;
531 priv->snd_card.dev = dev;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100532 dai_link = priv->dai_link;
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100533 priv->snd_card.dai_link = dai_link;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100534 priv->snd_card.num_links = num_links;
Xiubo Lica919fe2014-01-14 12:35:32 +0800535
Geert Uytterhoeven2dbab972014-10-07 15:09:26 +0200536 priv->gpio_hp_det = -ENOENT;
537 priv->gpio_mic_det = -ENOENT;
538
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800539 /* Get room for the other properties */
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100540 priv->dai_props = devm_kzalloc(dev,
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100541 sizeof(*priv->dai_props) * num_links,
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100542 GFP_KERNEL);
543 if (!priv->dai_props)
544 return -ENOMEM;
545
Jean-Francois Moine201a0ea2014-01-15 16:51:41 +0100546 if (np && of_device_is_available(np)) {
547
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700548 ret = asoc_simple_card_parse_of(np, priv);
Xiubo Lica919fe2014-01-14 12:35:32 +0800549 if (ret < 0) {
550 if (ret != -EPROBE_DEFER)
551 dev_err(dev, "parse error %d\n", ret);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100552 goto err;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900553 }
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100554
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900555 } else {
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100556 struct asoc_simple_card_info *cinfo;
557
558 cinfo = dev->platform_data;
559 if (!cinfo) {
Xiubo Li34787d0a2014-01-09 17:49:40 +0800560 dev_err(dev, "no info for asoc-simple-card\n");
561 return -EINVAL;
562 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900563
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800564 if (!cinfo->name ||
565 !cinfo->codec_dai.name ||
566 !cinfo->codec ||
567 !cinfo->platform ||
Jean-Francois Moine7722f832014-01-15 16:51:33 +0100568 !cinfo->cpu_dai.name) {
569 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
570 return -EINVAL;
571 }
Jean-Francois Moine2bee9912014-01-15 16:51:37 +0100572
Kuninori Morimoto12ffa6f2014-03-09 19:37:56 -0700573 priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name;
Jean-Francois Moine5ca8ba42014-01-15 16:51:45 +0100574 dai_link->name = cinfo->name;
575 dai_link->stream_name = cinfo->name;
576 dai_link->platform_name = cinfo->platform;
577 dai_link->codec_name = cinfo->codec;
Jean-Francois Moine52008472014-01-15 16:51:48 +0100578 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
579 dai_link->codec_dai_name = cinfo->codec_dai.name;
Kuninori Morimotoa5960bd2014-08-27 20:08:27 -0700580 dai_link->init = asoc_simple_card_dai_init;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100581 memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai,
582 sizeof(priv->dai_props->cpu_dai));
583 memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai,
584 sizeof(priv->dai_props->codec_dai));
Kuninori Morimoto81985bd2014-03-02 20:32:43 -0800585
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100586 priv->dai_props->cpu_dai.fmt |= cinfo->daifmt;
587 priv->dai_props->codec_dai.fmt |= cinfo->daifmt;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700588 }
589
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100590 snd_soc_card_set_drvdata(&priv->snd_card, priv);
Xiubo Liba194a42014-01-13 17:08:08 +0800591
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100592 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
Xiubo Lie3c4a282014-09-01 14:46:52 +0800593 if (ret >= 0)
594 return ret;
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100595
596err:
Geert Uytterhoeven7ddfdb52015-01-13 21:03:37 +0100597 asoc_simple_card_unref(&priv->snd_card);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100598 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700599}
600
Xiubo Lie3c4a282014-09-01 14:46:52 +0800601static int asoc_simple_card_remove(struct platform_device *pdev)
602{
Dylan Reid3fe24032014-10-01 14:25:20 -0700603 struct snd_soc_card *card = platform_get_drvdata(pdev);
604 struct simple_card_data *priv = snd_soc_card_get_drvdata(card);
605
606 if (gpio_is_valid(priv->gpio_hp_det))
607 snd_soc_jack_free_gpios(&simple_card_hp_jack, 1,
608 &simple_card_hp_jack_gpio);
609 if (gpio_is_valid(priv->gpio_mic_det))
610 snd_soc_jack_free_gpios(&simple_card_mic_jack, 1,
611 &simple_card_mic_jack_gpio);
612
Geert Uytterhoeven7ddfdb52015-01-13 21:03:37 +0100613 return asoc_simple_card_unref(card);
Xiubo Lie3c4a282014-09-01 14:46:52 +0800614}
615
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900616static const struct of_device_id asoc_simple_of_match[] = {
617 { .compatible = "simple-audio-card", },
618 {},
619};
620MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
621
Kuninori Morimotof2390882012-04-08 21:17:50 -0700622static struct platform_driver asoc_simple_card = {
623 .driver = {
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800624 .name = "asoc-simple-card",
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900625 .of_match_table = asoc_simple_of_match,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700626 },
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800627 .probe = asoc_simple_card_probe,
Xiubo Lie3c4a282014-09-01 14:46:52 +0800628 .remove = asoc_simple_card_remove,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700629};
630
631module_platform_driver(asoc_simple_card);
632
Fabio Estevamc445be32013-08-23 14:35:17 -0300633MODULE_ALIAS("platform:asoc-simple-card");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700634MODULE_LICENSE("GPL");
635MODULE_DESCRIPTION("ASoC Simple Sound Card");
636MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");