blob: 7fe3009b1c43c63c055c4f9ea8f2b25376ad5c7b [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
Andrew Lunn2942a0e2014-05-22 17:31:49 +020042static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream,
43 struct snd_pcm_hw_params *params)
44{
45 struct snd_soc_pcm_runtime *rtd = substream->private_data;
46 struct snd_soc_dai *codec_dai = rtd->codec_dai;
47 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
48 unsigned int mclk;
49 int ret = 0;
50
51 if (priv->mclk_fs) {
52 mclk = params_rate(params) * priv->mclk_fs;
53 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
54 SND_SOC_CLOCK_IN);
55 }
56
57 return ret;
58}
59
60static struct snd_soc_ops asoc_simple_card_ops = {
61 .hw_params = asoc_simple_card_hw_params,
62};
63
Dylan Reid3fe24032014-10-01 14:25:20 -070064static struct snd_soc_jack simple_card_hp_jack;
65static struct snd_soc_jack_pin simple_card_hp_jack_pins[] = {
66 {
67 .pin = "Headphones",
68 .mask = SND_JACK_HEADPHONE,
69 },
70};
71static struct snd_soc_jack_gpio simple_card_hp_jack_gpio = {
72 .name = "Headphone detection",
73 .report = SND_JACK_HEADPHONE,
74 .debounce_time = 150,
75};
76
77static struct snd_soc_jack simple_card_mic_jack;
78static struct snd_soc_jack_pin simple_card_mic_jack_pins[] = {
79 {
80 .pin = "Mic Jack",
81 .mask = SND_JACK_MICROPHONE,
82 },
83};
84static struct snd_soc_jack_gpio simple_card_mic_jack_gpio = {
85 .name = "Mic detection",
86 .report = SND_JACK_MICROPHONE,
87 .debounce_time = 150,
88};
89
Kuninori Morimotoa4a29922013-01-10 16:49:11 -080090static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
Xiubo Li30d03412014-01-24 15:43:01 +080091 struct asoc_simple_dai *set)
Kuninori Morimotoa4a29922013-01-10 16:49:11 -080092{
Xiubo Li4763ebe2014-01-24 15:43:00 +080093 int ret;
Kuninori Morimotoa4a29922013-01-10 16:49:11 -080094
Xiubo Li30d03412014-01-24 15:43:01 +080095 if (set->fmt) {
96 ret = snd_soc_dai_set_fmt(dai, set->fmt);
Xiubo Li4763ebe2014-01-24 15:43:00 +080097 if (ret && ret != -ENOTSUPP) {
98 dev_err(dai->dev, "simple-card: set_fmt error\n");
99 goto err;
100 }
Kuninori Morimotoe244bb92013-10-17 22:46:49 -0700101 }
102
Xiubo Li4763ebe2014-01-24 15:43:00 +0800103 if (set->sysclk) {
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800104 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
Xiubo Li4763ebe2014-01-24 15:43:00 +0800105 if (ret && ret != -ENOTSUPP) {
106 dev_err(dai->dev, "simple-card: set_sysclk error\n");
107 goto err;
108 }
109 }
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800110
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800111 if (set->slots) {
112 ret = snd_soc_dai_set_tdm_slot(dai, 0, 0,
113 set->slots,
114 set->slot_width);
115 if (ret && ret != -ENOTSUPP) {
116 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
117 goto err;
118 }
119 }
120
Xiubo Li4763ebe2014-01-24 15:43:00 +0800121 ret = 0;
122
123err:
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800124 return ret;
125}
126
Kuninori Morimotof2390882012-04-08 21:17:50 -0700127static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
128{
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800129 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimotof2390882012-04-08 21:17:50 -0700130 struct snd_soc_dai *codec = rtd->codec_dai;
131 struct snd_soc_dai *cpu = rtd->cpu_dai;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100132 struct simple_dai_props *dai_props;
133 int num, ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700134
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100135 num = rtd - rtd->card->rtd;
136 dai_props = &priv->dai_props[num];
137 ret = __asoc_simple_card_dai_init(codec, &dai_props->codec_dai);
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800138 if (ret < 0)
139 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700140
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100141 ret = __asoc_simple_card_dai_init(cpu, &dai_props->cpu_dai);
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800142 if (ret < 0)
143 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700144
Dylan Reid3fe24032014-10-01 14:25:20 -0700145 if (gpio_is_valid(priv->gpio_hp_det)) {
146 snd_soc_jack_new(codec->codec, "Headphones", SND_JACK_HEADPHONE,
147 &simple_card_hp_jack);
148 snd_soc_jack_add_pins(&simple_card_hp_jack,
149 ARRAY_SIZE(simple_card_hp_jack_pins),
150 simple_card_hp_jack_pins);
151
152 simple_card_hp_jack_gpio.gpio = priv->gpio_hp_det;
Jianqun44761592014-11-01 11:22:18 +0800153 simple_card_hp_jack_gpio.invert = priv->gpio_hp_det_invert;
Dylan Reid3fe24032014-10-01 14:25:20 -0700154 snd_soc_jack_add_gpios(&simple_card_hp_jack, 1,
155 &simple_card_hp_jack_gpio);
156 }
157
158 if (gpio_is_valid(priv->gpio_mic_det)) {
159 snd_soc_jack_new(codec->codec, "Mic Jack", SND_JACK_MICROPHONE,
160 &simple_card_mic_jack);
161 snd_soc_jack_add_pins(&simple_card_mic_jack,
162 ARRAY_SIZE(simple_card_mic_jack_pins),
163 simple_card_mic_jack_pins);
164 simple_card_mic_jack_gpio.gpio = priv->gpio_mic_det;
Jianqun44761592014-11-01 11:22:18 +0800165 simple_card_mic_jack_gpio.invert = priv->gpio_mic_det_invert;
Dylan Reid3fe24032014-10-01 14:25:20 -0700166 snd_soc_jack_add_gpios(&simple_card_mic_jack, 1,
167 &simple_card_mic_jack_gpio);
168 }
Kuninori Morimotof2390882012-04-08 21:17:50 -0700169 return 0;
170}
171
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900172static int
173asoc_simple_card_sub_parse_of(struct device_node *np,
174 struct asoc_simple_dai *dai,
Xiubo Li8ea21342014-08-18 15:00:15 +0800175 struct device_node **p_node,
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700176 const char **name,
177 int *args_count)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900178{
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700179 struct of_phandle_args args;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900180 struct clk *clk;
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300181 u32 val;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900182 int ret;
183
184 /*
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800185 * Get node via "sound-dai = <&phandle port>"
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900186 * it will be used as xxx_of_node on soc_bind_dai_link()
187 */
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700188 ret = of_parse_phandle_with_args(np, "sound-dai",
189 "#sound-dai-cells", 0, &args);
190 if (ret)
191 return ret;
192
193 *p_node = args.np;
194
195 if (args_count)
196 *args_count = args.args_count;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900197
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800198 /* Get dai->name */
Jean-Francois Moine52008472014-01-15 16:51:48 +0100199 ret = snd_soc_of_get_dai_name(np, name);
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900200 if (ret < 0)
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100201 return ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900202
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800203 /* Parse TDM slot */
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800204 ret = snd_soc_of_parse_tdm_slot(np, &dai->slots, &dai->slot_width);
205 if (ret)
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100206 return ret;
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800207
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900208 /*
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800209 * Parse dai->sysclk come from "clocks = <&xxx>"
210 * (if system has common clock)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900211 * or "system-clock-frequency = <xxx>"
Xiubo Li71467e42013-12-23 15:25:38 +0800212 * or device's module clock.
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900213 */
Xiubo Li71467e42013-12-23 15:25:38 +0800214 if (of_property_read_bool(np, "clocks")) {
215 clk = of_clk_get(np, 0);
216 if (IS_ERR(clk)) {
217 ret = PTR_ERR(clk);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100218 return ret;
Xiubo Li71467e42013-12-23 15:25:38 +0800219 }
220
221 dai->sysclk = clk_get_rate(clk);
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300222 } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) {
223 dai->sysclk = val;
Xiubo Li71467e42013-12-23 15:25:38 +0800224 } else {
Arnd Bergmann88a60e52014-09-08 13:14:05 +0200225 clk = of_clk_get(args.np, 0);
Xiubo Lie2a19ac2014-01-06 12:34:36 +0800226 if (!IS_ERR(clk))
227 dai->sysclk = clk_get_rate(clk);
Xiubo Li71467e42013-12-23 15:25:38 +0800228 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900229
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100230 return 0;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900231}
232
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700233static int asoc_simple_card_parse_daifmt(struct device_node *node,
234 struct simple_card_data *priv,
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700235 struct device_node *codec,
236 char *prefix, int idx)
237{
238 struct device *dev = simple_priv_to_dev(priv);
239 struct device_node *bitclkmaster = NULL;
240 struct device_node *framemaster = NULL;
241 struct simple_dai_props *dai_props = simple_priv_to_props(priv, idx);
242 struct asoc_simple_dai *cpu_dai = &dai_props->cpu_dai;
243 struct asoc_simple_dai *codec_dai = &dai_props->codec_dai;
244 unsigned int daifmt;
245
246 daifmt = snd_soc_of_parse_daifmt(node, prefix,
247 &bitclkmaster, &framemaster);
248 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
249
250 if (strlen(prefix) && !bitclkmaster && !framemaster) {
251 /*
252 * No dai-link level and master setting was not found from
253 * sound node level, revert back to legacy DT parsing and
254 * take the settings from codec node.
255 */
256 dev_dbg(dev, "Revert to legacy daifmt parsing\n");
257
258 cpu_dai->fmt = codec_dai->fmt =
259 snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) |
260 (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
261 } else {
262 if (codec == bitclkmaster)
263 daifmt |= (codec == framemaster) ?
264 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
265 else
266 daifmt |= (codec == framemaster) ?
267 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
268
269 cpu_dai->fmt = daifmt;
270 codec_dai->fmt = daifmt;
271 }
272
273 of_node_put(bitclkmaster);
274 of_node_put(framemaster);
275
276 return 0;
277}
278
Kuninori Morimoto2d82eeb2014-08-27 20:07:46 -0700279static int asoc_simple_card_dai_link_of(struct device_node *node,
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700280 struct simple_card_data *priv,
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700281 int idx,
Kuninori Morimoto2d82eeb2014-08-27 20:07:46 -0700282 bool is_top_level_node)
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100283{
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700284 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700285 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx);
286 struct simple_dai_props *dai_props = simple_priv_to_props(priv, idx);
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700287 struct device_node *cpu = NULL;
288 struct device_node *codec = NULL;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200289 char *name;
290 char prop[128];
291 char *prefix = "";
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700292 int ret, cpu_args;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100293
Xiubo Li20804372014-09-03 10:23:39 +0800294 /* For single DAI link & old style of DT node */
Jyri Sarha64872212014-04-24 19:42:00 +0300295 if (is_top_level_node)
296 prefix = "simple-audio-card,";
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100297
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200298 snprintf(prop, sizeof(prop), "%scpu", prefix);
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700299 cpu = of_get_child_by_name(node, prop);
300
301 snprintf(prop, sizeof(prop), "%scodec", prefix);
302 codec = of_get_child_by_name(node, prop);
303
304 if (!cpu || !codec) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200305 ret = -EINVAL;
Nicolin Chen966b8062014-04-24 19:13:59 +0800306 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200307 goto dai_link_of_err;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100308 }
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200309
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700310 ret = asoc_simple_card_parse_daifmt(node, priv,
Jean-Francois Moine7195d922014-11-25 13:22:45 +0100311 codec, prefix, idx);
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700312 if (ret < 0)
313 goto dai_link_of_err;
314
315 ret = asoc_simple_card_sub_parse_of(cpu, &dai_props->cpu_dai,
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200316 &dai_link->cpu_of_node,
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700317 &dai_link->cpu_dai_name,
318 &cpu_args);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200319 if (ret < 0)
320 goto dai_link_of_err;
321
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700322 ret = asoc_simple_card_sub_parse_of(codec, &dai_props->codec_dai,
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200323 &dai_link->codec_of_node,
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700324 &dai_link->codec_dai_name, NULL);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200325 if (ret < 0)
326 goto dai_link_of_err;
327
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200328 if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name) {
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800329 ret = -EINVAL;
330 goto dai_link_of_err;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200331 }
332
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800333 /* Simple Card assumes platform == cpu */
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200334 dai_link->platform_of_node = dai_link->cpu_of_node;
335
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800336 /* DAI link name is created from CPU/CODEC dai name */
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200337 name = devm_kzalloc(dev,
338 strlen(dai_link->cpu_dai_name) +
339 strlen(dai_link->codec_dai_name) + 2,
340 GFP_KERNEL);
341 sprintf(name, "%s-%s", dai_link->cpu_dai_name,
342 dai_link->codec_dai_name);
343 dai_link->name = dai_link->stream_name = name;
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200344 dai_link->ops = &asoc_simple_card_ops;
Kuninori Morimotoa5960bd2014-08-27 20:08:27 -0700345 dai_link->init = asoc_simple_card_dai_init;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200346
347 dev_dbg(dev, "\tname : %s\n", dai_link->stream_name);
348 dev_dbg(dev, "\tcpu : %s / %04x / %d\n",
349 dai_link->cpu_dai_name,
350 dai_props->cpu_dai.fmt,
351 dai_props->cpu_dai.sysclk);
352 dev_dbg(dev, "\tcodec : %s / %04x / %d\n",
353 dai_link->codec_dai_name,
354 dai_props->codec_dai.fmt,
355 dai_props->codec_dai.sysclk);
356
Kuninori Morimoto179949b2014-08-27 20:08:06 -0700357 /*
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800358 * In soc_bind_dai_link() will check cpu name after
359 * of_node matching if dai_link has cpu_dai_name.
360 * but, it will never match if name was created by
361 * fmt_single_name() remove cpu_dai_name if cpu_args
362 * was 0. See:
Kuninori Morimoto179949b2014-08-27 20:08:06 -0700363 * fmt_single_name()
364 * fmt_multiple_name()
365 */
Kuninori Morimoto7c7b9cf2014-09-02 04:05:30 -0700366 if (!cpu_args)
367 dai_link->cpu_dai_name = NULL;
Kuninori Morimoto179949b2014-08-27 20:08:06 -0700368
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200369dai_link_of_err:
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700370 of_node_put(cpu);
371 of_node_put(codec);
372
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100373 return ret;
374}
375
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900376static int asoc_simple_card_parse_of(struct device_node *node,
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700377 struct simple_card_data *priv)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900378{
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700379 struct device *dev = simple_priv_to_dev(priv);
Jianqun44761592014-11-01 11:22:18 +0800380 enum of_gpio_flags flags;
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300381 u32 val;
Xiubo Lid4c22092013-12-23 12:57:01 +0800382 int ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900383
Xiubo Li20804372014-09-03 10:23:39 +0800384 if (!node)
385 return -EINVAL;
386
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800387 /* Parse the card name from DT */
Xiubo Li27725552014-01-24 15:43:02 +0800388 snd_soc_of_parse_card_name(&priv->snd_card, "simple-audio-card,name");
389
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800390 /* The off-codec widgets */
Xiubo Li9d681f52014-02-08 15:59:53 +0800391 if (of_property_read_bool(node, "simple-audio-card,widgets")) {
392 ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card,
393 "simple-audio-card,widgets");
394 if (ret)
395 return ret;
396 }
397
Xiubo Lid4c22092013-12-23 12:57:01 +0800398 /* DAPM routes */
Xiubo Li8c0b8232014-01-07 09:15:16 +0800399 if (of_property_read_bool(node, "simple-audio-card,routing")) {
Jean-Francois Moineb367a322014-01-15 16:52:00 +0100400 ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
Xiubo Li8c0b8232014-01-07 09:15:16 +0800401 "simple-audio-card,routing");
Xiubo Lif87a3e82014-01-07 09:13:42 +0800402 if (ret)
403 return ret;
404 }
Xiubo Lid4c22092013-12-23 12:57:01 +0800405
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200406 /* Factor to mclk, used in hw_params() */
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300407 ret = of_property_read_u32(node, "simple-audio-card,mclk-fs", &val);
408 if (ret == 0)
409 priv->mclk_fs = val;
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200410
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200411 dev_dbg(dev, "New simple-card: %s\n", priv->snd_card.name ?
412 priv->snd_card.name : "");
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100413
Xiubo Li20804372014-09-03 10:23:39 +0800414 /* Single/Muti DAI link(s) & New style of DT node */
415 if (of_get_child_by_name(node, "simple-audio-card,dai-link")) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200416 struct device_node *np = NULL;
Kuninori Morimotoa44a7502014-08-27 20:08:47 -0700417 int i = 0;
418
419 for_each_child_of_node(node, np) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200420 dev_dbg(dev, "\tlink %d:\n", i);
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700421 ret = asoc_simple_card_dai_link_of(np, priv,
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700422 i, false);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200423 if (ret < 0) {
424 of_node_put(np);
425 return ret;
426 }
Kuninori Morimotoa44a7502014-08-27 20:08:47 -0700427 i++;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200428 }
429 } else {
Xiubo Li20804372014-09-03 10:23:39 +0800430 /* For single DAI link & old style of DT node */
Kuninori Morimoto9810f532014-09-09 21:38:24 -0700431 ret = asoc_simple_card_dai_link_of(node, priv, 0, true);
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100432 if (ret < 0)
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200433 return ret;
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100434 }
Xiubo Lidd41e0c2013-12-20 14:39:50 +0800435
Jianqun44761592014-11-01 11:22:18 +0800436 priv->gpio_hp_det = of_get_named_gpio_flags(node,
437 "simple-audio-card,hp-det-gpio", 0, &flags);
438 priv->gpio_hp_det_invert = !!(flags & OF_GPIO_ACTIVE_LOW);
Dylan Reid3fe24032014-10-01 14:25:20 -0700439 if (priv->gpio_hp_det == -EPROBE_DEFER)
440 return -EPROBE_DEFER;
441
Jianqun44761592014-11-01 11:22:18 +0800442 priv->gpio_mic_det = of_get_named_gpio_flags(node,
443 "simple-audio-card,mic-det-gpio", 0, &flags);
444 priv->gpio_mic_det_invert = !!(flags & OF_GPIO_ACTIVE_LOW);
Dylan Reid3fe24032014-10-01 14:25:20 -0700445 if (priv->gpio_mic_det == -EPROBE_DEFER)
446 return -EPROBE_DEFER;
447
Xiubo Li27725552014-01-24 15:43:02 +0800448 if (!priv->snd_card.name)
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200449 priv->snd_card.name = priv->snd_card.dai_link->name;
Kuninori Morimotof687d902014-02-27 18:25:24 -0800450
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900451 return 0;
452}
453
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800454/* Decrease the reference count of the device nodes */
Geert Uytterhoeven7ddfdb52015-01-13 21:03:37 +0100455static int asoc_simple_card_unref(struct snd_soc_card *card)
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100456{
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100457 struct snd_soc_dai_link *dai_link;
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100458 int num_links;
459
460 for (num_links = 0, dai_link = card->dai_link;
461 num_links < card->num_links;
462 num_links++, dai_link++) {
Jean-Francois Moine0099c762014-11-09 12:38:56 +0100463 of_node_put(dai_link->cpu_of_node);
464 of_node_put(dai_link->codec_of_node);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100465 }
466 return 0;
467}
468
Kuninori Morimotof2390882012-04-08 21:17:50 -0700469static int asoc_simple_card_probe(struct platform_device *pdev)
470{
Jean-Francois Moine45fce592014-01-15 16:51:56 +0100471 struct simple_card_data *priv;
Jean-Francois Moine5ca8ba42014-01-15 16:51:45 +0100472 struct snd_soc_dai_link *dai_link;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900473 struct device_node *np = pdev->dev.of_node;
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800474 struct device *dev = &pdev->dev;
Xiubo Li20804372014-09-03 10:23:39 +0800475 int num_links, ret;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100476
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800477 /* Get the number of DAI links */
Xiubo Li20804372014-09-03 10:23:39 +0800478 if (np && of_get_child_by_name(np, "simple-audio-card,dai-link"))
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100479 num_links = of_get_child_count(np);
Xiubo Li20804372014-09-03 10:23:39 +0800480 else
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100481 num_links = 1;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700482
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800483 /* Allocate the private data and the DAI link array */
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100484 priv = devm_kzalloc(dev,
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100485 sizeof(*priv) + sizeof(*dai_link) * num_links,
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100486 GFP_KERNEL);
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100487 if (!priv)
Xiubo Lica919fe2014-01-14 12:35:32 +0800488 return -ENOMEM;
489
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800490 /* Init snd_soc_card */
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100491 priv->snd_card.owner = THIS_MODULE;
492 priv->snd_card.dev = dev;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100493 dai_link = priv->dai_link;
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100494 priv->snd_card.dai_link = dai_link;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100495 priv->snd_card.num_links = num_links;
Xiubo Lica919fe2014-01-14 12:35:32 +0800496
Geert Uytterhoeven2dbab972014-10-07 15:09:26 +0200497 priv->gpio_hp_det = -ENOENT;
498 priv->gpio_mic_det = -ENOENT;
499
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800500 /* Get room for the other properties */
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100501 priv->dai_props = devm_kzalloc(dev,
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100502 sizeof(*priv->dai_props) * num_links,
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100503 GFP_KERNEL);
504 if (!priv->dai_props)
505 return -ENOMEM;
506
Jean-Francois Moine201a0ea2014-01-15 16:51:41 +0100507 if (np && of_device_is_available(np)) {
508
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700509 ret = asoc_simple_card_parse_of(np, priv);
Xiubo Lica919fe2014-01-14 12:35:32 +0800510 if (ret < 0) {
511 if (ret != -EPROBE_DEFER)
512 dev_err(dev, "parse error %d\n", ret);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100513 goto err;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900514 }
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100515
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900516 } else {
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100517 struct asoc_simple_card_info *cinfo;
518
519 cinfo = dev->platform_data;
520 if (!cinfo) {
Xiubo Li34787d0a2014-01-09 17:49:40 +0800521 dev_err(dev, "no info for asoc-simple-card\n");
522 return -EINVAL;
523 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900524
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800525 if (!cinfo->name ||
526 !cinfo->codec_dai.name ||
527 !cinfo->codec ||
528 !cinfo->platform ||
Jean-Francois Moine7722f832014-01-15 16:51:33 +0100529 !cinfo->cpu_dai.name) {
530 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
531 return -EINVAL;
532 }
Jean-Francois Moine2bee9912014-01-15 16:51:37 +0100533
Kuninori Morimoto12ffa6f2014-03-09 19:37:56 -0700534 priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name;
Jean-Francois Moine5ca8ba42014-01-15 16:51:45 +0100535 dai_link->name = cinfo->name;
536 dai_link->stream_name = cinfo->name;
537 dai_link->platform_name = cinfo->platform;
538 dai_link->codec_name = cinfo->codec;
Jean-Francois Moine52008472014-01-15 16:51:48 +0100539 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
540 dai_link->codec_dai_name = cinfo->codec_dai.name;
Kuninori Morimotoa5960bd2014-08-27 20:08:27 -0700541 dai_link->init = asoc_simple_card_dai_init;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100542 memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai,
543 sizeof(priv->dai_props->cpu_dai));
544 memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai,
545 sizeof(priv->dai_props->codec_dai));
Kuninori Morimoto81985bd2014-03-02 20:32:43 -0800546
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100547 priv->dai_props->cpu_dai.fmt |= cinfo->daifmt;
548 priv->dai_props->codec_dai.fmt |= cinfo->daifmt;
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
567 if (gpio_is_valid(priv->gpio_hp_det))
568 snd_soc_jack_free_gpios(&simple_card_hp_jack, 1,
569 &simple_card_hp_jack_gpio);
570 if (gpio_is_valid(priv->gpio_mic_det))
571 snd_soc_jack_free_gpios(&simple_card_mic_jack, 1,
572 &simple_card_mic_jack_gpio);
573
Geert Uytterhoeven7ddfdb52015-01-13 21:03:37 +0100574 return asoc_simple_card_unref(card);
Xiubo Lie3c4a282014-09-01 14:46:52 +0800575}
576
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900577static const struct of_device_id asoc_simple_of_match[] = {
578 { .compatible = "simple-audio-card", },
579 {},
580};
581MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
582
Kuninori Morimotof2390882012-04-08 21:17:50 -0700583static struct platform_driver asoc_simple_card = {
584 .driver = {
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800585 .name = "asoc-simple-card",
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900586 .of_match_table = asoc_simple_of_match,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700587 },
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800588 .probe = asoc_simple_card_probe,
Xiubo Lie3c4a282014-09-01 14:46:52 +0800589 .remove = asoc_simple_card_remove,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700590};
591
592module_platform_driver(asoc_simple_card);
593
Fabio Estevamc445be32013-08-23 14:35:17 -0300594MODULE_ALIAS("platform:asoc-simple-card");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700595MODULE_LICENSE("GPL");
596MODULE_DESCRIPTION("ASoC Simple Sound Card");
597MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");