blob: d1b7293c133eaa456f9d7761f273ee2887251121 [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;
32 int gpio_mic_det;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +010033 struct snd_soc_dai_link dai_link[]; /* dynamically allocated */
Jean-Francois Moine45fce592014-01-15 16:51:56 +010034};
35
Kuninori Morimotof531913f2014-09-09 21:37:57 -070036#define simple_priv_to_dev(priv) ((priv)->snd_card.dev)
Kuninori Morimoto9810f532014-09-09 21:38:24 -070037#define simple_priv_to_link(priv, i) ((priv)->snd_card.dai_link + i)
38#define simple_priv_to_props(priv, i) ((priv)->dai_props + i)
Kuninori Morimotof531913f2014-09-09 21:37:57 -070039
Andrew Lunn2942a0e2014-05-22 17:31:49 +020040static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream,
41 struct snd_pcm_hw_params *params)
42{
43 struct snd_soc_pcm_runtime *rtd = substream->private_data;
44 struct snd_soc_dai *codec_dai = rtd->codec_dai;
45 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
46 unsigned int mclk;
47 int ret = 0;
48
49 if (priv->mclk_fs) {
50 mclk = params_rate(params) * priv->mclk_fs;
51 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
52 SND_SOC_CLOCK_IN);
53 }
54
55 return ret;
56}
57
58static struct snd_soc_ops asoc_simple_card_ops = {
59 .hw_params = asoc_simple_card_hw_params,
60};
61
Dylan Reid3fe24032014-10-01 14:25:20 -070062static struct snd_soc_jack simple_card_hp_jack;
63static struct snd_soc_jack_pin simple_card_hp_jack_pins[] = {
64 {
65 .pin = "Headphones",
66 .mask = SND_JACK_HEADPHONE,
67 },
68};
69static struct snd_soc_jack_gpio simple_card_hp_jack_gpio = {
70 .name = "Headphone detection",
71 .report = SND_JACK_HEADPHONE,
72 .debounce_time = 150,
73};
74
75static struct snd_soc_jack simple_card_mic_jack;
76static struct snd_soc_jack_pin simple_card_mic_jack_pins[] = {
77 {
78 .pin = "Mic Jack",
79 .mask = SND_JACK_MICROPHONE,
80 },
81};
82static struct snd_soc_jack_gpio simple_card_mic_jack_gpio = {
83 .name = "Mic detection",
84 .report = SND_JACK_MICROPHONE,
85 .debounce_time = 150,
86};
87
Kuninori Morimotoa4a29922013-01-10 16:49:11 -080088static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
Xiubo Li30d03412014-01-24 15:43:01 +080089 struct asoc_simple_dai *set)
Kuninori Morimotoa4a29922013-01-10 16:49:11 -080090{
Xiubo Li4763ebe2014-01-24 15:43:00 +080091 int ret;
Kuninori Morimotoa4a29922013-01-10 16:49:11 -080092
Xiubo Li30d03412014-01-24 15:43:01 +080093 if (set->fmt) {
94 ret = snd_soc_dai_set_fmt(dai, set->fmt);
Xiubo Li4763ebe2014-01-24 15:43:00 +080095 if (ret && ret != -ENOTSUPP) {
96 dev_err(dai->dev, "simple-card: set_fmt error\n");
97 goto err;
98 }
Kuninori Morimotoe244bb92013-10-17 22:46:49 -070099 }
100
Xiubo Li4763ebe2014-01-24 15:43:00 +0800101 if (set->sysclk) {
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800102 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
Xiubo Li4763ebe2014-01-24 15:43:00 +0800103 if (ret && ret != -ENOTSUPP) {
104 dev_err(dai->dev, "simple-card: set_sysclk error\n");
105 goto err;
106 }
107 }
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800108
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800109 if (set->slots) {
110 ret = snd_soc_dai_set_tdm_slot(dai, 0, 0,
111 set->slots,
112 set->slot_width);
113 if (ret && ret != -ENOTSUPP) {
114 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
115 goto err;
116 }
117 }
118
Xiubo Li4763ebe2014-01-24 15:43:00 +0800119 ret = 0;
120
121err:
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800122 return ret;
123}
124
Kuninori Morimotof2390882012-04-08 21:17:50 -0700125static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
126{
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800127 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimotof2390882012-04-08 21:17:50 -0700128 struct snd_soc_dai *codec = rtd->codec_dai;
129 struct snd_soc_dai *cpu = rtd->cpu_dai;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100130 struct simple_dai_props *dai_props;
131 int num, ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700132
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100133 num = rtd - rtd->card->rtd;
134 dai_props = &priv->dai_props[num];
135 ret = __asoc_simple_card_dai_init(codec, &dai_props->codec_dai);
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800136 if (ret < 0)
137 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700138
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100139 ret = __asoc_simple_card_dai_init(cpu, &dai_props->cpu_dai);
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800140 if (ret < 0)
141 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700142
Dylan Reid3fe24032014-10-01 14:25:20 -0700143 if (gpio_is_valid(priv->gpio_hp_det)) {
144 snd_soc_jack_new(codec->codec, "Headphones", SND_JACK_HEADPHONE,
145 &simple_card_hp_jack);
146 snd_soc_jack_add_pins(&simple_card_hp_jack,
147 ARRAY_SIZE(simple_card_hp_jack_pins),
148 simple_card_hp_jack_pins);
149
150 simple_card_hp_jack_gpio.gpio = priv->gpio_hp_det;
151 snd_soc_jack_add_gpios(&simple_card_hp_jack, 1,
152 &simple_card_hp_jack_gpio);
153 }
154
155 if (gpio_is_valid(priv->gpio_mic_det)) {
156 snd_soc_jack_new(codec->codec, "Mic Jack", SND_JACK_MICROPHONE,
157 &simple_card_mic_jack);
158 snd_soc_jack_add_pins(&simple_card_mic_jack,
159 ARRAY_SIZE(simple_card_mic_jack_pins),
160 simple_card_mic_jack_pins);
161 simple_card_mic_jack_gpio.gpio = priv->gpio_mic_det;
162 snd_soc_jack_add_gpios(&simple_card_mic_jack, 1,
163 &simple_card_mic_jack_gpio);
164 }
Kuninori Morimotof2390882012-04-08 21:17:50 -0700165 return 0;
166}
167
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900168static int
169asoc_simple_card_sub_parse_of(struct device_node *np,
170 struct asoc_simple_dai *dai,
Xiubo Li8ea21342014-08-18 15:00:15 +0800171 struct device_node **p_node,
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700172 const char **name,
173 int *args_count)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900174{
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700175 struct of_phandle_args args;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900176 struct clk *clk;
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300177 u32 val;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900178 int ret;
179
180 /*
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800181 * Get node via "sound-dai = <&phandle port>"
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900182 * it will be used as xxx_of_node on soc_bind_dai_link()
183 */
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700184 ret = of_parse_phandle_with_args(np, "sound-dai",
185 "#sound-dai-cells", 0, &args);
186 if (ret)
187 return ret;
188
189 *p_node = args.np;
190
191 if (args_count)
192 *args_count = args.args_count;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900193
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800194 /* Get dai->name */
Jean-Francois Moine52008472014-01-15 16:51:48 +0100195 ret = snd_soc_of_get_dai_name(np, name);
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900196 if (ret < 0)
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100197 return ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900198
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800199 /* Parse TDM slot */
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800200 ret = snd_soc_of_parse_tdm_slot(np, &dai->slots, &dai->slot_width);
201 if (ret)
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100202 return ret;
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800203
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900204 /*
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800205 * Parse dai->sysclk come from "clocks = <&xxx>"
206 * (if system has common clock)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900207 * or "system-clock-frequency = <xxx>"
Xiubo Li71467e42013-12-23 15:25:38 +0800208 * or device's module clock.
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900209 */
Xiubo Li71467e42013-12-23 15:25:38 +0800210 if (of_property_read_bool(np, "clocks")) {
211 clk = of_clk_get(np, 0);
212 if (IS_ERR(clk)) {
213 ret = PTR_ERR(clk);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100214 return ret;
Xiubo Li71467e42013-12-23 15:25:38 +0800215 }
216
217 dai->sysclk = clk_get_rate(clk);
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300218 } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) {
219 dai->sysclk = val;
Xiubo Li71467e42013-12-23 15:25:38 +0800220 } else {
Arnd Bergmann88a60e52014-09-08 13:14:05 +0200221 clk = of_clk_get(args.np, 0);
Xiubo Lie2a19ac2014-01-06 12:34:36 +0800222 if (!IS_ERR(clk))
223 dai->sysclk = clk_get_rate(clk);
Xiubo Li71467e42013-12-23 15:25:38 +0800224 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900225
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100226 return 0;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900227}
228
Kuninori Morimoto2d82eeb2014-08-27 20:07:46 -0700229static int asoc_simple_card_dai_link_of(struct device_node *node,
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700230 struct simple_card_data *priv,
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700231 int idx,
Kuninori Morimoto2d82eeb2014-08-27 20:07:46 -0700232 bool is_top_level_node)
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100233{
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700234 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700235 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx);
236 struct simple_dai_props *dai_props = simple_priv_to_props(priv, idx);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200237 struct device_node *np = NULL;
238 struct device_node *bitclkmaster = NULL;
239 struct device_node *framemaster = NULL;
240 unsigned int daifmt;
241 char *name;
242 char prop[128];
243 char *prefix = "";
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700244 int ret, cpu_args;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100245
Xiubo Li20804372014-09-03 10:23:39 +0800246 /* For single DAI link & old style of DT node */
Jyri Sarha64872212014-04-24 19:42:00 +0300247 if (is_top_level_node)
248 prefix = "simple-audio-card,";
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100249
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200250 daifmt = snd_soc_of_parse_daifmt(node, prefix,
251 &bitclkmaster, &framemaster);
252 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
253
254 snprintf(prop, sizeof(prop), "%scpu", prefix);
255 np = of_get_child_by_name(node, prop);
256 if (!np) {
257 ret = -EINVAL;
Nicolin Chen966b8062014-04-24 19:13:59 +0800258 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200259 goto dai_link_of_err;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100260 }
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200261
262 ret = asoc_simple_card_sub_parse_of(np, &dai_props->cpu_dai,
263 &dai_link->cpu_of_node,
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700264 &dai_link->cpu_dai_name,
265 &cpu_args);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200266 if (ret < 0)
267 goto dai_link_of_err;
268
269 dai_props->cpu_dai.fmt = daifmt;
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800270 switch (((np == bitclkmaster) << 4) | (np == framemaster)) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200271 case 0x11:
272 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBS_CFS;
273 break;
274 case 0x10:
275 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBS_CFM;
276 break;
277 case 0x01:
278 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBM_CFS;
279 break;
280 default:
281 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBM_CFM;
282 break;
283 }
284
285 of_node_put(np);
286 snprintf(prop, sizeof(prop), "%scodec", prefix);
287 np = of_get_child_by_name(node, prop);
288 if (!np) {
289 ret = -EINVAL;
Nicolin Chen966b8062014-04-24 19:13:59 +0800290 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200291 goto dai_link_of_err;
292 }
293
294 ret = asoc_simple_card_sub_parse_of(np, &dai_props->codec_dai,
295 &dai_link->codec_of_node,
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700296 &dai_link->codec_dai_name, NULL);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200297 if (ret < 0)
298 goto dai_link_of_err;
299
300 if (strlen(prefix) && !bitclkmaster && !framemaster) {
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800301 /*
302 * No DAI link level and master setting was found
303 * from sound node level, revert back to legacy DT
304 * parsing and take the settings from codec node.
305 */
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200306 dev_dbg(dev, "%s: Revert to legacy daifmt parsing\n",
307 __func__);
308 dai_props->cpu_dai.fmt = dai_props->codec_dai.fmt =
309 snd_soc_of_parse_daifmt(np, NULL, NULL, NULL) |
310 (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
311 } else {
312 dai_props->codec_dai.fmt = daifmt;
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800313 switch (((np == bitclkmaster) << 4) | (np == framemaster)) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200314 case 0x11:
315 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBM_CFM;
316 break;
317 case 0x10:
318 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBM_CFS;
319 break;
320 case 0x01:
321 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBS_CFM;
322 break;
323 default:
324 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBS_CFS;
325 break;
326 }
327 }
328
329 if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name) {
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800330 ret = -EINVAL;
331 goto dai_link_of_err;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200332 }
333
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800334 /* Simple Card assumes platform == cpu */
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200335 dai_link->platform_of_node = dai_link->cpu_of_node;
336
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800337 /* DAI link name is created from CPU/CODEC dai name */
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200338 name = devm_kzalloc(dev,
339 strlen(dai_link->cpu_dai_name) +
340 strlen(dai_link->codec_dai_name) + 2,
341 GFP_KERNEL);
342 sprintf(name, "%s-%s", dai_link->cpu_dai_name,
343 dai_link->codec_dai_name);
344 dai_link->name = dai_link->stream_name = name;
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200345 dai_link->ops = &asoc_simple_card_ops;
Kuninori Morimotoa5960bd2014-08-27 20:08:27 -0700346 dai_link->init = asoc_simple_card_dai_init;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200347
348 dev_dbg(dev, "\tname : %s\n", dai_link->stream_name);
349 dev_dbg(dev, "\tcpu : %s / %04x / %d\n",
350 dai_link->cpu_dai_name,
351 dai_props->cpu_dai.fmt,
352 dai_props->cpu_dai.sysclk);
353 dev_dbg(dev, "\tcodec : %s / %04x / %d\n",
354 dai_link->codec_dai_name,
355 dai_props->codec_dai.fmt,
356 dai_props->codec_dai.sysclk);
357
Kuninori Morimoto179949b2014-08-27 20:08:06 -0700358 /*
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800359 * In soc_bind_dai_link() will check cpu name after
360 * of_node matching if dai_link has cpu_dai_name.
361 * but, it will never match if name was created by
362 * fmt_single_name() remove cpu_dai_name if cpu_args
363 * was 0. See:
Kuninori Morimoto179949b2014-08-27 20:08:06 -0700364 * fmt_single_name()
365 * fmt_multiple_name()
366 */
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700367 if (!cpu_args)
368 dai_link->cpu_dai_name = NULL;
Kuninori Morimoto179949b2014-08-27 20:08:06 -0700369
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200370dai_link_of_err:
371 if (np)
372 of_node_put(np);
373 if (bitclkmaster)
374 of_node_put(bitclkmaster);
375 if (framemaster)
376 of_node_put(framemaster);
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100377 return ret;
378}
379
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900380static int asoc_simple_card_parse_of(struct device_node *node,
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700381 struct simple_card_data *priv)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900382{
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700383 struct device *dev = simple_priv_to_dev(priv);
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300384 u32 val;
Xiubo Lid4c22092013-12-23 12:57:01 +0800385 int ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900386
Xiubo Li20804372014-09-03 10:23:39 +0800387 if (!node)
388 return -EINVAL;
389
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800390 /* Parse the card name from DT */
Xiubo Li27725552014-01-24 15:43:02 +0800391 snd_soc_of_parse_card_name(&priv->snd_card, "simple-audio-card,name");
392
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800393 /* The off-codec widgets */
Xiubo Li9d681f52014-02-08 15:59:53 +0800394 if (of_property_read_bool(node, "simple-audio-card,widgets")) {
395 ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card,
396 "simple-audio-card,widgets");
397 if (ret)
398 return ret;
399 }
400
Xiubo Lid4c22092013-12-23 12:57:01 +0800401 /* DAPM routes */
Xiubo Li8c0b8232014-01-07 09:15:16 +0800402 if (of_property_read_bool(node, "simple-audio-card,routing")) {
Jean-Francois Moineb367a322014-01-15 16:52:00 +0100403 ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
Xiubo Li8c0b8232014-01-07 09:15:16 +0800404 "simple-audio-card,routing");
Xiubo Lif87a3e82014-01-07 09:13:42 +0800405 if (ret)
406 return ret;
407 }
Xiubo Lid4c22092013-12-23 12:57:01 +0800408
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200409 /* Factor to mclk, used in hw_params() */
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300410 ret = of_property_read_u32(node, "simple-audio-card,mclk-fs", &val);
411 if (ret == 0)
412 priv->mclk_fs = val;
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200413
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200414 dev_dbg(dev, "New simple-card: %s\n", priv->snd_card.name ?
415 priv->snd_card.name : "");
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100416
Xiubo Li20804372014-09-03 10:23:39 +0800417 /* Single/Muti DAI link(s) & New style of DT node */
418 if (of_get_child_by_name(node, "simple-audio-card,dai-link")) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200419 struct device_node *np = NULL;
Kuninori Morimotoa44a7502014-08-27 20:08:47 -0700420 int i = 0;
421
422 for_each_child_of_node(node, np) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200423 dev_dbg(dev, "\tlink %d:\n", i);
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700424 ret = asoc_simple_card_dai_link_of(np, priv,
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700425 i, false);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200426 if (ret < 0) {
427 of_node_put(np);
428 return ret;
429 }
Kuninori Morimotoa44a7502014-08-27 20:08:47 -0700430 i++;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200431 }
432 } else {
Xiubo Li20804372014-09-03 10:23:39 +0800433 /* For single DAI link & old style of DT node */
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700434 ret = asoc_simple_card_dai_link_of(node, priv, 0, true);
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100435 if (ret < 0)
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200436 return ret;
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100437 }
Xiubo Lidd41e0c2013-12-20 14:39:50 +0800438
Dylan Reid3fe24032014-10-01 14:25:20 -0700439 priv->gpio_hp_det = of_get_named_gpio(node,
440 "simple-audio-card,hp-det-gpio", 0);
441 if (priv->gpio_hp_det == -EPROBE_DEFER)
442 return -EPROBE_DEFER;
443
444 priv->gpio_mic_det = of_get_named_gpio(node,
445 "simple-audio-card,mic-det-gpio", 0);
446 if (priv->gpio_mic_det == -EPROBE_DEFER)
447 return -EPROBE_DEFER;
448
Xiubo Li27725552014-01-24 15:43:02 +0800449 if (!priv->snd_card.name)
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200450 priv->snd_card.name = priv->snd_card.dai_link->name;
Kuninori Morimotof687d902014-02-27 18:25:24 -0800451
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900452 return 0;
453}
454
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800455/* Decrease the reference count of the device nodes */
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100456static int asoc_simple_card_unref(struct platform_device *pdev)
457{
458 struct snd_soc_card *card = platform_get_drvdata(pdev);
459 struct snd_soc_dai_link *dai_link;
460 struct device_node *np;
461 int num_links;
462
463 for (num_links = 0, dai_link = card->dai_link;
464 num_links < card->num_links;
465 num_links++, dai_link++) {
466 np = (struct device_node *) dai_link->cpu_of_node;
467 if (np)
468 of_node_put(np);
469 np = (struct device_node *) dai_link->codec_of_node;
470 if (np)
471 of_node_put(np);
472 }
473 return 0;
474}
475
Kuninori Morimotof2390882012-04-08 21:17:50 -0700476static int asoc_simple_card_probe(struct platform_device *pdev)
477{
Jean-Francois Moine45fce592014-01-15 16:51:56 +0100478 struct simple_card_data *priv;
Jean-Francois Moine5ca8ba42014-01-15 16:51:45 +0100479 struct snd_soc_dai_link *dai_link;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900480 struct device_node *np = pdev->dev.of_node;
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800481 struct device *dev = &pdev->dev;
Xiubo Li20804372014-09-03 10:23:39 +0800482 int num_links, ret;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100483
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800484 /* Get the number of DAI links */
Xiubo Li20804372014-09-03 10:23:39 +0800485 if (np && of_get_child_by_name(np, "simple-audio-card,dai-link"))
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100486 num_links = of_get_child_count(np);
Xiubo Li20804372014-09-03 10:23:39 +0800487 else
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100488 num_links = 1;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700489
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800490 /* Allocate the private data and the DAI link array */
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100491 priv = devm_kzalloc(dev,
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100492 sizeof(*priv) + sizeof(*dai_link) * num_links,
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100493 GFP_KERNEL);
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100494 if (!priv)
Xiubo Lica919fe2014-01-14 12:35:32 +0800495 return -ENOMEM;
496
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800497 /* Init snd_soc_card */
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100498 priv->snd_card.owner = THIS_MODULE;
499 priv->snd_card.dev = dev;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100500 dai_link = priv->dai_link;
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100501 priv->snd_card.dai_link = dai_link;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100502 priv->snd_card.num_links = num_links;
Xiubo Lica919fe2014-01-14 12:35:32 +0800503
Geert Uytterhoeven2dbab972014-10-07 15:09:26 +0200504 priv->gpio_hp_det = -ENOENT;
505 priv->gpio_mic_det = -ENOENT;
506
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800507 /* Get room for the other properties */
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100508 priv->dai_props = devm_kzalloc(dev,
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100509 sizeof(*priv->dai_props) * num_links,
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100510 GFP_KERNEL);
511 if (!priv->dai_props)
512 return -ENOMEM;
513
Jean-Francois Moine201a0ea2014-01-15 16:51:41 +0100514 if (np && of_device_is_available(np)) {
515
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700516 ret = asoc_simple_card_parse_of(np, priv);
Xiubo Lica919fe2014-01-14 12:35:32 +0800517 if (ret < 0) {
518 if (ret != -EPROBE_DEFER)
519 dev_err(dev, "parse error %d\n", ret);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100520 goto err;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900521 }
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100522
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900523 } else {
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100524 struct asoc_simple_card_info *cinfo;
525
526 cinfo = dev->platform_data;
527 if (!cinfo) {
Xiubo Li34787d0a2014-01-09 17:49:40 +0800528 dev_err(dev, "no info for asoc-simple-card\n");
529 return -EINVAL;
530 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900531
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800532 if (!cinfo->name ||
533 !cinfo->codec_dai.name ||
534 !cinfo->codec ||
535 !cinfo->platform ||
Jean-Francois Moine7722f832014-01-15 16:51:33 +0100536 !cinfo->cpu_dai.name) {
537 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
538 return -EINVAL;
539 }
Jean-Francois Moine2bee9912014-01-15 16:51:37 +0100540
Kuninori Morimoto12ffa6f2014-03-09 19:37:56 -0700541 priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name;
Jean-Francois Moine5ca8ba42014-01-15 16:51:45 +0100542 dai_link->name = cinfo->name;
543 dai_link->stream_name = cinfo->name;
544 dai_link->platform_name = cinfo->platform;
545 dai_link->codec_name = cinfo->codec;
Jean-Francois Moine52008472014-01-15 16:51:48 +0100546 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
547 dai_link->codec_dai_name = cinfo->codec_dai.name;
Kuninori Morimotoa5960bd2014-08-27 20:08:27 -0700548 dai_link->init = asoc_simple_card_dai_init;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100549 memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai,
550 sizeof(priv->dai_props->cpu_dai));
551 memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai,
552 sizeof(priv->dai_props->codec_dai));
Kuninori Morimoto81985bd2014-03-02 20:32:43 -0800553
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100554 priv->dai_props->cpu_dai.fmt |= cinfo->daifmt;
555 priv->dai_props->codec_dai.fmt |= cinfo->daifmt;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700556 }
557
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100558 snd_soc_card_set_drvdata(&priv->snd_card, priv);
Xiubo Liba194a42014-01-13 17:08:08 +0800559
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100560 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
Xiubo Lie3c4a282014-09-01 14:46:52 +0800561 if (ret >= 0)
562 return ret;
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100563
564err:
565 asoc_simple_card_unref(pdev);
566 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700567}
568
Xiubo Lie3c4a282014-09-01 14:46:52 +0800569static int asoc_simple_card_remove(struct platform_device *pdev)
570{
Dylan Reid3fe24032014-10-01 14:25:20 -0700571 struct snd_soc_card *card = platform_get_drvdata(pdev);
572 struct simple_card_data *priv = snd_soc_card_get_drvdata(card);
573
574 if (gpio_is_valid(priv->gpio_hp_det))
575 snd_soc_jack_free_gpios(&simple_card_hp_jack, 1,
576 &simple_card_hp_jack_gpio);
577 if (gpio_is_valid(priv->gpio_mic_det))
578 snd_soc_jack_free_gpios(&simple_card_mic_jack, 1,
579 &simple_card_mic_jack_gpio);
580
Xiubo Lie3c4a282014-09-01 14:46:52 +0800581 return asoc_simple_card_unref(pdev);
582}
583
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900584static const struct of_device_id asoc_simple_of_match[] = {
585 { .compatible = "simple-audio-card", },
586 {},
587};
588MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
589
Kuninori Morimotof2390882012-04-08 21:17:50 -0700590static struct platform_driver asoc_simple_card = {
591 .driver = {
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800592 .name = "asoc-simple-card",
Fabio Estevamc445be32013-08-23 14:35:17 -0300593 .owner = THIS_MODULE,
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900594 .of_match_table = asoc_simple_of_match,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700595 },
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800596 .probe = asoc_simple_card_probe,
Xiubo Lie3c4a282014-09-01 14:46:52 +0800597 .remove = asoc_simple_card_remove,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700598};
599
600module_platform_driver(asoc_simple_card);
601
Fabio Estevamc445be32013-08-23 14:35:17 -0300602MODULE_ALIAS("platform:asoc-simple-card");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700603MODULE_LICENSE("GPL");
604MODULE_DESCRIPTION("ASoC Simple Sound Card");
605MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");