blob: 05d6e02c42f33cf3113408d672ef3cba6a1c4371 [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
Kuninori Morimoto9eac3612016-05-31 08:59:46 +000024struct asoc_simple_jack {
25 struct snd_soc_jack jack;
26 struct snd_soc_jack_pin pin;
27 struct snd_soc_jack_gpio gpio;
28};
29
Jean-Francois Moine45fce592014-01-15 16:51:56 +010030struct simple_card_data {
31 struct snd_soc_card snd_card;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +010032 struct simple_dai_props {
33 struct asoc_simple_dai cpu_dai;
34 struct asoc_simple_dai codec_dai;
Arnaud Pouliquen85a4bfd2015-06-05 10:19:05 +020035 unsigned int mclk_fs;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +010036 } *dai_props;
Andrew Lunn2942a0e2014-05-22 17:31:49 +020037 unsigned int mclk_fs;
Kuninori Morimoto9eac3612016-05-31 08:59:46 +000038 struct asoc_simple_jack hp_jack;
39 struct asoc_simple_jack mic_jack;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +010040 struct snd_soc_dai_link dai_link[]; /* dynamically allocated */
Jean-Francois Moine45fce592014-01-15 16:51:56 +010041};
42
Kuninori Morimotof531913f2014-09-09 21:37:57 -070043#define simple_priv_to_dev(priv) ((priv)->snd_card.dev)
Kuninori Morimoto9810f532014-09-09 21:38:24 -070044#define simple_priv_to_link(priv, i) ((priv)->snd_card.dai_link + i)
45#define simple_priv_to_props(priv, i) ((priv)->dai_props + i)
Kuninori Morimotof531913f2014-09-09 21:37:57 -070046
Kuninori Morimoto44c16af2016-08-08 06:02:07 +000047#define DAI "sound-dai"
48#define CELL "#sound-dai-cells"
Kuninori Morimoto548563f2016-05-31 08:59:01 +000049#define PREFIX "simple-audio-card,"
50
Kuninori Morimoto9eac3612016-05-31 08:59:46 +000051#define asoc_simple_card_init_hp(card, sjack, prefix)\
52 asoc_simple_card_init_jack(card, sjack, 1, prefix)
53#define asoc_simple_card_init_mic(card, sjack, prefix)\
54 asoc_simple_card_init_jack(card, sjack, 0, prefix)
55static int asoc_simple_card_init_jack(struct snd_soc_card *card,
56 struct asoc_simple_jack *sjack,
57 int is_hp, char *prefix)
58{
59 struct device *dev = card->dev;
60 enum of_gpio_flags flags;
61 char prop[128];
62 char *pin_name;
63 char *gpio_name;
64 int mask;
65 int det;
66
67 sjack->gpio.gpio = -ENOENT;
68
69 if (is_hp) {
70 snprintf(prop, sizeof(prop), "%shp-det-gpio", prefix);
71 pin_name = "Headphones";
72 gpio_name = "Headphone detection";
73 mask = SND_JACK_HEADPHONE;
74 } else {
75 snprintf(prop, sizeof(prop), "%smic-det-gpio", prefix);
76 pin_name = "Mic Jack";
77 gpio_name = "Mic detection";
78 mask = SND_JACK_MICROPHONE;
79 }
80
81 det = of_get_named_gpio_flags(dev->of_node, prop, 0, &flags);
82 if (det == -EPROBE_DEFER)
83 return -EPROBE_DEFER;
84
85 if (gpio_is_valid(det)) {
86 sjack->pin.pin = pin_name;
87 sjack->pin.mask = mask;
88
89 sjack->gpio.name = gpio_name;
90 sjack->gpio.report = mask;
91 sjack->gpio.gpio = det;
92 sjack->gpio.invert = !!(flags & OF_GPIO_ACTIVE_LOW);
93 sjack->gpio.debounce_time = 150;
94
95 snd_soc_card_jack_new(card, pin_name, mask,
96 &sjack->jack,
97 &sjack->pin, 1);
98
99 snd_soc_jack_add_gpios(&sjack->jack, 1,
100 &sjack->gpio);
101 }
102
103 return 0;
104}
105
106static void asoc_simple_card_remove_jack(struct asoc_simple_jack *sjack)
107{
108 if (gpio_is_valid(sjack->gpio.gpio))
109 snd_soc_jack_free_gpios(&sjack->jack, 1, &sjack->gpio);
110}
111
Jyri Sarhaf9911802015-01-13 21:16:34 +0200112static int asoc_simple_card_startup(struct snd_pcm_substream *substream)
113{
114 struct snd_soc_pcm_runtime *rtd = substream->private_data;
115 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
116 struct simple_dai_props *dai_props =
Mengdong Lin1a497982015-11-18 02:34:11 -0500117 &priv->dai_props[rtd->num];
Jyri Sarhaf9911802015-01-13 21:16:34 +0200118 int ret;
119
120 ret = clk_prepare_enable(dai_props->cpu_dai.clk);
121 if (ret)
122 return ret;
123
124 ret = clk_prepare_enable(dai_props->codec_dai.clk);
125 if (ret)
126 clk_disable_unprepare(dai_props->cpu_dai.clk);
127
128 return ret;
129}
130
131static void asoc_simple_card_shutdown(struct snd_pcm_substream *substream)
132{
133 struct snd_soc_pcm_runtime *rtd = substream->private_data;
134 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
135 struct simple_dai_props *dai_props =
Mengdong Lin1a497982015-11-18 02:34:11 -0500136 &priv->dai_props[rtd->num];
Jyri Sarhaf9911802015-01-13 21:16:34 +0200137
138 clk_disable_unprepare(dai_props->cpu_dai.clk);
139
140 clk_disable_unprepare(dai_props->codec_dai.clk);
141}
142
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200143static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream,
144 struct snd_pcm_hw_params *params)
145{
146 struct snd_soc_pcm_runtime *rtd = substream->private_data;
147 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Arnaud Pouliquene2257972015-06-05 10:19:06 +0200148 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200149 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
Mengdong Lin1a497982015-11-18 02:34:11 -0500150 struct simple_dai_props *dai_props = &priv->dai_props[rtd->num];
Arnaud Pouliquen85a4bfd2015-06-05 10:19:05 +0200151 unsigned int mclk, mclk_fs = 0;
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200152 int ret = 0;
153
Arnaud Pouliquen85a4bfd2015-06-05 10:19:05 +0200154 if (priv->mclk_fs)
155 mclk_fs = priv->mclk_fs;
156 else if (dai_props->mclk_fs)
157 mclk_fs = dai_props->mclk_fs;
158
159 if (mclk_fs) {
160 mclk = params_rate(params) * mclk_fs;
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200161 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
162 SND_SOC_CLOCK_IN);
Arnaud Pouliquene2257972015-06-05 10:19:06 +0200163 if (ret && ret != -ENOTSUPP)
164 goto err;
165
166 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
167 SND_SOC_CLOCK_OUT);
168 if (ret && ret != -ENOTSUPP)
169 goto err;
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200170 }
Aaro Koskinenee43a1a2016-01-24 00:36:40 +0200171 return 0;
Arnaud Pouliquene2257972015-06-05 10:19:06 +0200172err:
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200173 return ret;
174}
175
176static struct snd_soc_ops asoc_simple_card_ops = {
Jyri Sarhaf9911802015-01-13 21:16:34 +0200177 .startup = asoc_simple_card_startup,
178 .shutdown = asoc_simple_card_shutdown,
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200179 .hw_params = asoc_simple_card_hw_params,
180};
181
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800182static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
Xiubo Li30d03412014-01-24 15:43:01 +0800183 struct asoc_simple_dai *set)
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800184{
Xiubo Li4763ebe2014-01-24 15:43:00 +0800185 int ret;
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800186
Xiubo Li4763ebe2014-01-24 15:43:00 +0800187 if (set->sysclk) {
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800188 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
Xiubo Li4763ebe2014-01-24 15:43:00 +0800189 if (ret && ret != -ENOTSUPP) {
190 dev_err(dai->dev, "simple-card: set_sysclk error\n");
191 goto err;
192 }
193 }
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800194
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800195 if (set->slots) {
Jyri Sarha61310842015-09-09 21:27:43 +0300196 ret = snd_soc_dai_set_tdm_slot(dai,
197 set->tx_slot_mask,
198 set->rx_slot_mask,
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800199 set->slots,
200 set->slot_width);
201 if (ret && ret != -ENOTSUPP) {
202 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
203 goto err;
204 }
205 }
206
Xiubo Li4763ebe2014-01-24 15:43:00 +0800207 ret = 0;
208
209err:
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800210 return ret;
211}
212
Kuninori Morimotof2390882012-04-08 21:17:50 -0700213static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
214{
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800215 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimotof2390882012-04-08 21:17:50 -0700216 struct snd_soc_dai *codec = rtd->codec_dai;
217 struct snd_soc_dai *cpu = rtd->cpu_dai;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100218 struct simple_dai_props *dai_props;
Mengdong Lin1a497982015-11-18 02:34:11 -0500219 int ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700220
Mengdong Lin1a497982015-11-18 02:34:11 -0500221 dai_props = &priv->dai_props[rtd->num];
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100222 ret = __asoc_simple_card_dai_init(codec, &dai_props->codec_dai);
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800223 if (ret < 0)
224 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700225
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100226 ret = __asoc_simple_card_dai_init(cpu, &dai_props->cpu_dai);
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800227 if (ret < 0)
228 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700229
Kuninori Morimoto9eac3612016-05-31 08:59:46 +0000230 ret = asoc_simple_card_init_hp(rtd->card, &priv->hp_jack, PREFIX);
231 if (ret < 0)
232 return ret;
Dylan Reid3fe24032014-10-01 14:25:20 -0700233
Kuninori Morimoto9eac3612016-05-31 08:59:46 +0000234 ret = asoc_simple_card_init_mic(rtd->card, &priv->hp_jack, PREFIX);
235 if (ret < 0)
236 return ret;
Dylan Reid3fe24032014-10-01 14:25:20 -0700237
Kuninori Morimotof2390882012-04-08 21:17:50 -0700238 return 0;
239}
240
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900241static int
242asoc_simple_card_sub_parse_of(struct device_node *np,
243 struct asoc_simple_dai *dai,
Xiubo Li8ea21342014-08-18 15:00:15 +0800244 struct device_node **p_node,
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700245 const char **name,
246 int *args_count)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900247{
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900248 int ret;
249
Kuninori Morimoto5fb9cb12016-05-20 09:40:41 +0000250 if (!np)
251 return 0;
252
Kuninori Morimoto5fb9cb12016-05-20 09:40:41 +0000253 if (!dai)
254 return 0;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900255
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800256 /* Parse TDM slot */
Jyri Sarha61310842015-09-09 21:27:43 +0300257 ret = snd_soc_of_parse_tdm_slot(np, &dai->tx_slot_mask,
258 &dai->rx_slot_mask,
259 &dai->slots, &dai->slot_width);
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800260 if (ret)
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100261 return ret;
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800262
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100263 return 0;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900264}
265
Kuninori Morimoto2d82eeb2014-08-27 20:07:46 -0700266static int asoc_simple_card_dai_link_of(struct device_node *node,
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700267 struct simple_card_data *priv,
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700268 int idx,
Kuninori Morimoto2d82eeb2014-08-27 20:07:46 -0700269 bool is_top_level_node)
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100270{
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700271 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700272 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx);
273 struct simple_dai_props *dai_props = simple_priv_to_props(priv, idx);
Kuninori Morimoto28abd992016-07-19 02:53:13 +0000274 struct asoc_simple_dai *cpu_dai = &dai_props->cpu_dai;
275 struct asoc_simple_dai *codec_dai = &dai_props->codec_dai;
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700276 struct device_node *cpu = NULL;
Jun Niee0ae2252015-04-29 18:11:07 +0800277 struct device_node *plat = NULL;
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700278 struct device_node *codec = NULL;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200279 char prop[128];
280 char *prefix = "";
Kuninori Morimoto44c16af2016-08-08 06:02:07 +0000281 int ret, single_cpu;
Arnaud Pouliquen85a4bfd2015-06-05 10:19:05 +0200282 u32 val;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100283
Xiubo Li20804372014-09-03 10:23:39 +0800284 /* For single DAI link & old style of DT node */
Jyri Sarha64872212014-04-24 19:42:00 +0300285 if (is_top_level_node)
Kuninori Morimoto548563f2016-05-31 08:59:01 +0000286 prefix = PREFIX;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100287
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200288 snprintf(prop, sizeof(prop), "%scpu", prefix);
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700289 cpu = of_get_child_by_name(node, prop);
290
Jun Niee0ae2252015-04-29 18:11:07 +0800291 snprintf(prop, sizeof(prop), "%splat", prefix);
292 plat = of_get_child_by_name(node, prop);
293
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700294 snprintf(prop, sizeof(prop), "%scodec", prefix);
295 codec = of_get_child_by_name(node, prop);
296
297 if (!cpu || !codec) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200298 ret = -EINVAL;
Nicolin Chen966b8062014-04-24 19:13:59 +0800299 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200300 goto dai_link_of_err;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100301 }
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200302
Kuninori Morimotocecdef32016-06-30 06:02:46 +0000303 ret = asoc_simple_card_parse_daifmt(dev, node, codec,
304 prefix, &dai_link->dai_fmt);
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700305 if (ret < 0)
306 goto dai_link_of_err;
307
Arnaud Pouliquen85a4bfd2015-06-05 10:19:05 +0200308 if (!of_property_read_u32(node, "mclk-fs", &val))
309 dai_props->mclk_fs = val;
310
Kuninori Morimoto44c16af2016-08-08 06:02:07 +0000311 ret = asoc_simple_card_parse_cpu(cpu, dai_link,
312 DAI, CELL, &single_cpu);
313 if (ret < 0)
314 goto dai_link_of_err;
315
316 ret = asoc_simple_card_parse_codec(codec, dai_link, DAI, CELL);
317 if (ret < 0)
318 goto dai_link_of_err;
319
320 ret = asoc_simple_card_parse_platform(plat, dai_link, DAI, CELL);
321 if (ret < 0)
322 goto dai_link_of_err;
323
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700324 ret = asoc_simple_card_sub_parse_of(cpu, &dai_props->cpu_dai,
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200325 &dai_link->cpu_of_node,
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700326 &dai_link->cpu_dai_name,
Kuninori Morimoto44c16af2016-08-08 06:02:07 +0000327 &single_cpu);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200328 if (ret < 0)
329 goto dai_link_of_err;
330
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700331 ret = asoc_simple_card_sub_parse_of(codec, &dai_props->codec_dai,
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200332 &dai_link->codec_of_node,
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700333 &dai_link->codec_dai_name, NULL);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200334 if (ret < 0)
335 goto dai_link_of_err;
336
Kuninori Morimoto5fb9cb12016-05-20 09:40:41 +0000337 ret = asoc_simple_card_sub_parse_of(plat, NULL,
338 &dai_link->platform_of_node,
339 NULL, NULL);
340 if (ret < 0)
341 goto dai_link_of_err;
342
Kuninori Morimoto28abd992016-07-19 02:53:13 +0000343 ret = asoc_simple_card_parse_clk_cpu(cpu, dai_link, cpu_dai);
344 if (ret < 0)
345 goto dai_link_of_err;
346
347 ret = asoc_simple_card_parse_clk_codec(codec, dai_link, codec_dai);
348 if (ret < 0)
349 goto dai_link_of_err;
350
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200351 if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name) {
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800352 ret = -EINVAL;
353 goto dai_link_of_err;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200354 }
355
Kuninori Morimoto5fb9cb12016-05-20 09:40:41 +0000356 /* Assumes platform == cpu */
357 if (!dai_link->platform_of_node)
Jun Niee0ae2252015-04-29 18:11:07 +0800358 dai_link->platform_of_node = dai_link->cpu_of_node;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200359
Kuninori Morimoto2e8d1c72016-07-11 23:57:34 +0000360 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
361 "%s-%s",
362 dai_link->cpu_dai_name,
363 dai_link->codec_dai_name);
364 if (ret < 0)
Vishal Thanki31f30322015-03-03 18:59:00 +0530365 goto dai_link_of_err;
Vishal Thanki31f30322015-03-03 18:59:00 +0530366
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200367 dai_link->ops = &asoc_simple_card_ops;
Kuninori Morimotoa5960bd2014-08-27 20:08:27 -0700368 dai_link->init = asoc_simple_card_dai_init;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200369
370 dev_dbg(dev, "\tname : %s\n", dai_link->stream_name);
Lars-Peter Clausen1efb53a2015-03-24 01:07:08 +0000371 dev_dbg(dev, "\tformat : %04x\n", dai_link->dai_fmt);
372 dev_dbg(dev, "\tcpu : %s / %d\n",
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200373 dai_link->cpu_dai_name,
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200374 dai_props->cpu_dai.sysclk);
Lars-Peter Clausen1efb53a2015-03-24 01:07:08 +0000375 dev_dbg(dev, "\tcodec : %s / %d\n",
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200376 dai_link->codec_dai_name,
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200377 dai_props->codec_dai.sysclk);
378
Kuninori Morimoto179949b2014-08-27 20:08:06 -0700379 /*
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800380 * In soc_bind_dai_link() will check cpu name after
381 * of_node matching if dai_link has cpu_dai_name.
382 * but, it will never match if name was created by
383 * fmt_single_name() remove cpu_dai_name if cpu_args
384 * was 0. See:
Kuninori Morimoto179949b2014-08-27 20:08:06 -0700385 * fmt_single_name()
386 * fmt_multiple_name()
387 */
Kuninori Morimoto44c16af2016-08-08 06:02:07 +0000388 if (single_cpu)
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700389 dai_link->cpu_dai_name = NULL;
Kuninori Morimoto179949b2014-08-27 20:08:06 -0700390
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200391dai_link_of_err:
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700392 of_node_put(cpu);
393 of_node_put(codec);
394
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100395 return ret;
396}
397
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900398static int asoc_simple_card_parse_of(struct device_node *node,
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700399 struct simple_card_data *priv)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900400{
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700401 struct device *dev = simple_priv_to_dev(priv);
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300402 u32 val;
Xiubo Lid4c22092013-12-23 12:57:01 +0800403 int ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900404
Xiubo Li20804372014-09-03 10:23:39 +0800405 if (!node)
406 return -EINVAL;
407
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800408 /* The off-codec widgets */
Kuninori Morimoto548563f2016-05-31 08:59:01 +0000409 if (of_property_read_bool(node, PREFIX "widgets")) {
Xiubo Li9d681f52014-02-08 15:59:53 +0800410 ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card,
Kuninori Morimoto548563f2016-05-31 08:59:01 +0000411 PREFIX "widgets");
Xiubo Li9d681f52014-02-08 15:59:53 +0800412 if (ret)
413 return ret;
414 }
415
Xiubo Lid4c22092013-12-23 12:57:01 +0800416 /* DAPM routes */
Kuninori Morimoto548563f2016-05-31 08:59:01 +0000417 if (of_property_read_bool(node, PREFIX "routing")) {
Jean-Francois Moineb367a322014-01-15 16:52:00 +0100418 ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
Kuninori Morimoto548563f2016-05-31 08:59:01 +0000419 PREFIX "routing");
Xiubo Lif87a3e82014-01-07 09:13:42 +0800420 if (ret)
421 return ret;
422 }
Xiubo Lid4c22092013-12-23 12:57:01 +0800423
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200424 /* Factor to mclk, used in hw_params() */
Kuninori Morimoto548563f2016-05-31 08:59:01 +0000425 ret = of_property_read_u32(node, PREFIX "mclk-fs", &val);
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300426 if (ret == 0)
427 priv->mclk_fs = val;
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200428
Xiubo Li20804372014-09-03 10:23:39 +0800429 /* Single/Muti DAI link(s) & New style of DT node */
Kuninori Morimoto548563f2016-05-31 08:59:01 +0000430 if (of_get_child_by_name(node, PREFIX "dai-link")) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200431 struct device_node *np = NULL;
Kuninori Morimotoa44a7502014-08-27 20:08:47 -0700432 int i = 0;
433
434 for_each_child_of_node(node, np) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200435 dev_dbg(dev, "\tlink %d:\n", i);
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700436 ret = asoc_simple_card_dai_link_of(np, priv,
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700437 i, false);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200438 if (ret < 0) {
439 of_node_put(np);
440 return ret;
441 }
Kuninori Morimotoa44a7502014-08-27 20:08:47 -0700442 i++;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200443 }
444 } else {
Xiubo Li20804372014-09-03 10:23:39 +0800445 /* For single DAI link & old style of DT node */
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700446 ret = asoc_simple_card_dai_link_of(node, priv, 0, true);
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100447 if (ret < 0)
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200448 return ret;
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100449 }
Xiubo Lidd41e0c2013-12-20 14:39:50 +0800450
Kuninori Morimoto3527d852016-07-11 23:59:40 +0000451 ret = asoc_simple_card_parse_card_name(&priv->snd_card, PREFIX);
452 if (ret)
453 return ret;
Kuninori Morimotof687d902014-02-27 18:25:24 -0800454
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900455 return 0;
456}
457
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800458/* Decrease the reference count of the device nodes */
Geert Uytterhoeven7ddfdb52015-01-13 21:03:37 +0100459static int asoc_simple_card_unref(struct snd_soc_card *card)
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100460{
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100461 struct snd_soc_dai_link *dai_link;
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100462 int num_links;
463
464 for (num_links = 0, dai_link = card->dai_link;
465 num_links < card->num_links;
466 num_links++, dai_link++) {
Jean-Francois Moine0099c762014-11-09 12:38:56 +0100467 of_node_put(dai_link->cpu_of_node);
468 of_node_put(dai_link->codec_of_node);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100469 }
470 return 0;
471}
472
Kuninori Morimotof2390882012-04-08 21:17:50 -0700473static int asoc_simple_card_probe(struct platform_device *pdev)
474{
Jean-Francois Moine45fce592014-01-15 16:51:56 +0100475 struct simple_card_data *priv;
Jean-Francois Moine5ca8ba42014-01-15 16:51:45 +0100476 struct snd_soc_dai_link *dai_link;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900477 struct device_node *np = pdev->dev.of_node;
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800478 struct device *dev = &pdev->dev;
Xiubo Li20804372014-09-03 10:23:39 +0800479 int num_links, ret;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100480
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800481 /* Get the number of DAI links */
Kuninori Morimoto548563f2016-05-31 08:59:01 +0000482 if (np && of_get_child_by_name(np, PREFIX "dai-link"))
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100483 num_links = of_get_child_count(np);
Xiubo Li20804372014-09-03 10:23:39 +0800484 else
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100485 num_links = 1;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700486
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800487 /* Allocate the private data and the DAI link array */
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100488 priv = devm_kzalloc(dev,
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100489 sizeof(*priv) + sizeof(*dai_link) * num_links,
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100490 GFP_KERNEL);
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100491 if (!priv)
Xiubo Lica919fe2014-01-14 12:35:32 +0800492 return -ENOMEM;
493
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800494 /* Init snd_soc_card */
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100495 priv->snd_card.owner = THIS_MODULE;
496 priv->snd_card.dev = dev;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100497 dai_link = priv->dai_link;
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100498 priv->snd_card.dai_link = dai_link;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100499 priv->snd_card.num_links = num_links;
Xiubo Lica919fe2014-01-14 12:35:32 +0800500
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800501 /* Get room for the other properties */
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100502 priv->dai_props = devm_kzalloc(dev,
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100503 sizeof(*priv->dai_props) * num_links,
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100504 GFP_KERNEL);
505 if (!priv->dai_props)
506 return -ENOMEM;
507
Jean-Francois Moine201a0ea2014-01-15 16:51:41 +0100508 if (np && of_device_is_available(np)) {
509
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700510 ret = asoc_simple_card_parse_of(np, priv);
Xiubo Lica919fe2014-01-14 12:35:32 +0800511 if (ret < 0) {
512 if (ret != -EPROBE_DEFER)
513 dev_err(dev, "parse error %d\n", ret);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100514 goto err;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900515 }
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100516
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900517 } else {
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100518 struct asoc_simple_card_info *cinfo;
519
520 cinfo = dev->platform_data;
521 if (!cinfo) {
Xiubo Li34787d0a2014-01-09 17:49:40 +0800522 dev_err(dev, "no info for asoc-simple-card\n");
523 return -EINVAL;
524 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900525
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800526 if (!cinfo->name ||
527 !cinfo->codec_dai.name ||
528 !cinfo->codec ||
529 !cinfo->platform ||
Jean-Francois Moine7722f832014-01-15 16:51:33 +0100530 !cinfo->cpu_dai.name) {
531 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
532 return -EINVAL;
533 }
Jean-Francois Moine2bee9912014-01-15 16:51:37 +0100534
Kuninori Morimoto12ffa6f2014-03-09 19:37:56 -0700535 priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name;
Jean-Francois Moine5ca8ba42014-01-15 16:51:45 +0100536 dai_link->name = cinfo->name;
537 dai_link->stream_name = cinfo->name;
538 dai_link->platform_name = cinfo->platform;
539 dai_link->codec_name = cinfo->codec;
Jean-Francois Moine52008472014-01-15 16:51:48 +0100540 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
541 dai_link->codec_dai_name = cinfo->codec_dai.name;
Lars-Peter Clausen1efb53a2015-03-24 01:07:08 +0000542 dai_link->dai_fmt = cinfo->daifmt;
Kuninori Morimotoa5960bd2014-08-27 20:08:27 -0700543 dai_link->init = asoc_simple_card_dai_init;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100544 memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai,
545 sizeof(priv->dai_props->cpu_dai));
546 memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai,
547 sizeof(priv->dai_props->codec_dai));
Kuninori Morimoto81985bd2014-03-02 20:32:43 -0800548
Kuninori Morimotof2390882012-04-08 21:17:50 -0700549 }
550
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100551 snd_soc_card_set_drvdata(&priv->snd_card, priv);
Xiubo Liba194a42014-01-13 17:08:08 +0800552
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100553 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
Xiubo Lie3c4a282014-09-01 14:46:52 +0800554 if (ret >= 0)
555 return ret;
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100556
557err:
Geert Uytterhoeven7ddfdb52015-01-13 21:03:37 +0100558 asoc_simple_card_unref(&priv->snd_card);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100559 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700560}
561
Xiubo Lie3c4a282014-09-01 14:46:52 +0800562static int asoc_simple_card_remove(struct platform_device *pdev)
563{
Dylan Reid3fe24032014-10-01 14:25:20 -0700564 struct snd_soc_card *card = platform_get_drvdata(pdev);
565 struct simple_card_data *priv = snd_soc_card_get_drvdata(card);
566
Kuninori Morimoto9eac3612016-05-31 08:59:46 +0000567 asoc_simple_card_remove_jack(&priv->hp_jack);
568 asoc_simple_card_remove_jack(&priv->mic_jack);
Dylan Reid3fe24032014-10-01 14:25:20 -0700569
Geert Uytterhoeven7ddfdb52015-01-13 21:03:37 +0100570 return asoc_simple_card_unref(card);
Xiubo Lie3c4a282014-09-01 14:46:52 +0800571}
572
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900573static const struct of_device_id asoc_simple_of_match[] = {
574 { .compatible = "simple-audio-card", },
575 {},
576};
577MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
578
Kuninori Morimotof2390882012-04-08 21:17:50 -0700579static struct platform_driver asoc_simple_card = {
580 .driver = {
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800581 .name = "asoc-simple-card",
Peter Ujfalusi7c376712016-05-09 13:38:10 +0300582 .pm = &snd_soc_pm_ops,
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900583 .of_match_table = asoc_simple_of_match,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700584 },
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800585 .probe = asoc_simple_card_probe,
Xiubo Lie3c4a282014-09-01 14:46:52 +0800586 .remove = asoc_simple_card_remove,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700587};
588
589module_platform_driver(asoc_simple_card);
590
Fabio Estevamc445be32013-08-23 14:35:17 -0300591MODULE_ALIAS("platform:asoc-simple-card");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700592MODULE_LICENSE("GPL");
593MODULE_DESCRIPTION("ASoC Simple Sound Card");
594MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");