xref: /OK3568_Linux_fs/kernel/sound/soc/rockchip/rockchip_multicodecs.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Rockchip machine ASoC driver for Rockchip Multi-codecs audio
3  *
4  * Copyright (C) 2018 Fuzhou Rockchip Electronics Co., Ltd
5  *
6  * Authors: Sugar Zhang <sugar.zhang@rock-chips.com>,
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms and conditions of the GNU General Public License,
10  * version 2, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include <linux/extcon-provider.h>
23 #include <linux/gpio.h>
24 #include <linux/iio/consumer.h>
25 #include <linux/iio/iio.h>
26 #include <linux/input.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29 #include <linux/module.h>
30 #include <linux/of_gpio.h>
31 #include <linux/platform_device.h>
32 #include <linux/slab.h>
33 #include <linux/workqueue.h>
34 #include <sound/core.h>
35 #include <sound/jack.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/soc-dapm.h>
40 
41 #define DRV_NAME "rk-multicodecs"
42 #define WAIT_CARDS	(SNDRV_CARDS - 1)
43 #define DEFAULT_MCLK_FS	256
44 
45 struct adc_keys_button {
46 	u32 voltage;
47 	u32 keycode;
48 };
49 
50 struct input_dev_poller {
51 	void (*poll)(struct input_dev *dev);
52 
53 	unsigned int poll_interval_ms;
54 	struct input_dev *input;
55 	struct delayed_work work;
56 };
57 
58 struct multicodecs_data {
59 	struct snd_soc_card snd_card;
60 	struct snd_soc_dai_link dai_link;
61 	struct snd_soc_jack *jack_headset;
62 	struct gpio_desc *hp_ctl_gpio;
63 	struct gpio_desc *spk_ctl_gpio;
64 	struct gpio_desc *hp_det_gpio;
65 	struct iio_channel *adc;
66 	struct extcon_dev *extcon;
67 	struct delayed_work handler;
68 	unsigned int mclk_fs;
69 	bool codec_hp_det;
70 	u32 num_keys;
71 	u32 last_key;
72 	u32 keyup_voltage;
73 	const struct adc_keys_button *map;
74 	struct input_dev *input;
75 	struct input_dev_poller *poller;
76 };
77 
78 static const unsigned int headset_extcon_cable[] = {
79 	EXTCON_JACK_MICROPHONE,
80 	EXTCON_JACK_HEADPHONE,
81 	EXTCON_NONE,
82 };
83 
mc_set_poll_interval(struct input_dev_poller * poller,unsigned int interval)84 static void mc_set_poll_interval(struct input_dev_poller *poller, unsigned int interval)
85 {
86 	if (poller)
87 		poller->poll_interval_ms = interval;
88 }
89 
mc_keys_poller_queue_work(struct input_dev_poller * poller)90 static void mc_keys_poller_queue_work(struct input_dev_poller *poller)
91 {
92 	unsigned long delay;
93 
94 	delay = msecs_to_jiffies(poller->poll_interval_ms);
95 	if (delay >= HZ)
96 		delay = round_jiffies_relative(delay);
97 
98 	queue_delayed_work(system_freezable_wq, &poller->work, delay);
99 }
100 
mc_keys_poller_work(struct work_struct * work)101 static void mc_keys_poller_work(struct work_struct *work)
102 {
103 	struct input_dev_poller *poller =
104 		container_of(work, struct input_dev_poller, work.work);
105 
106 	poller->poll(poller->input);
107 	mc_keys_poller_queue_work(poller);
108 }
109 
mc_keys_poller_start(struct input_dev_poller * poller)110 static void mc_keys_poller_start(struct input_dev_poller *poller)
111 {
112 	if (poller->poll_interval_ms > 0) {
113 		poller->poll(poller->input);
114 		mc_keys_poller_queue_work(poller);
115 	}
116 }
117 
mc_keys_poller_stop(struct input_dev_poller * poller)118 static void mc_keys_poller_stop(struct input_dev_poller *poller)
119 {
120 	cancel_delayed_work_sync(&poller->work);
121 }
122 
mc_keys_setup_polling(struct multicodecs_data * mc_data,void (* poll_fn)(struct input_dev * dev))123 static int mc_keys_setup_polling(struct multicodecs_data *mc_data,
124 				 void (*poll_fn)(struct input_dev *dev))
125 {
126 	struct input_dev_poller *poller;
127 
128 	poller = devm_kzalloc(mc_data->snd_card.dev, sizeof(*poller), GFP_KERNEL);
129 	if (!poller)
130 		return -ENOMEM;
131 
132 	INIT_DELAYED_WORK(&poller->work, mc_keys_poller_work);
133 	poller->input = mc_data->input;
134 	poller->poll = poll_fn;
135 	mc_data->poller = poller;
136 
137 	return 0;
138 }
139 
mc_keys_poll(struct input_dev * input)140 static void mc_keys_poll(struct input_dev *input)
141 {
142 	struct multicodecs_data *mc_data = input_get_drvdata(input);
143 	int i, value, ret;
144 	u32 diff, closest = 0xffffffff;
145 	int keycode = 0;
146 
147 	ret = iio_read_channel_processed(mc_data->adc, &value);
148 	if (unlikely(ret < 0)) {
149 		/* Forcibly release key if any was pressed */
150 		value = mc_data->keyup_voltage;
151 	} else {
152 		for (i = 0; i < mc_data->num_keys; i++) {
153 			diff = abs(mc_data->map[i].voltage - value);
154 			if (diff < closest) {
155 				closest = diff;
156 				keycode = mc_data->map[i].keycode;
157 			}
158 		}
159 	}
160 
161 	if (abs(mc_data->keyup_voltage - value) < closest)
162 		keycode = 0;
163 
164 	if (mc_data->last_key && mc_data->last_key != keycode)
165 		input_report_key(input, mc_data->last_key, 0);
166 
167 	if (keycode)
168 		input_report_key(input, keycode, 1);
169 
170 	input_sync(input);
171 	mc_data->last_key = keycode;
172 }
173 
mc_keys_load_keymap(struct device * dev,struct multicodecs_data * mc_data)174 static int mc_keys_load_keymap(struct device *dev,
175 			       struct multicodecs_data *mc_data)
176 {
177 	struct adc_keys_button *map;
178 	struct fwnode_handle *child;
179 	int i = 0;
180 
181 	mc_data->num_keys = device_get_child_node_count(dev);
182 	if (mc_data->num_keys == 0) {
183 		dev_err(dev, "keymap is missing\n");
184 		return -EINVAL;
185 	}
186 
187 	map = devm_kmalloc_array(dev, mc_data->num_keys, sizeof(*map), GFP_KERNEL);
188 	if (!map)
189 		return -ENOMEM;
190 
191 	device_for_each_child_node(dev, child) {
192 		if (fwnode_property_read_u32(child, "press-threshold-microvolt",
193 					     &map[i].voltage)) {
194 			dev_err(dev, "Key with invalid or missing voltage\n");
195 			fwnode_handle_put(child);
196 			return -EINVAL;
197 		}
198 		map[i].voltage /= 1000;
199 
200 		if (fwnode_property_read_u32(child, "linux,code",
201 					     &map[i].keycode)) {
202 			dev_err(dev, "Key with invalid or missing linux,code\n");
203 			fwnode_handle_put(child);
204 			return -EINVAL;
205 		}
206 
207 		i++;
208 	}
209 	mc_data->map = map;
210 	return 0;
211 }
212 
adc_jack_handler(struct work_struct * work)213 static void adc_jack_handler(struct work_struct *work)
214 {
215 	struct multicodecs_data *mc_data = container_of(to_delayed_work(work),
216 						  struct multicodecs_data,
217 						  handler);
218 	struct snd_soc_jack *jack_headset = mc_data->jack_headset;
219 	int adc, ret = 0;
220 
221 	if (!gpiod_get_value(mc_data->hp_det_gpio)) {
222 		snd_soc_jack_report(jack_headset, 0, SND_JACK_HEADSET);
223 		extcon_set_state_sync(mc_data->extcon,
224 				EXTCON_JACK_HEADPHONE, false);
225 		extcon_set_state_sync(mc_data->extcon,
226 				EXTCON_JACK_MICROPHONE, false);
227 		if (mc_data->poller)
228 			mc_keys_poller_stop(mc_data->poller);
229 
230 		return;
231 	}
232 	if (!mc_data->adc) {
233 		/* no ADC, so is headphone */
234 		snd_soc_jack_report(jack_headset, SND_JACK_HEADPHONE, SND_JACK_HEADSET);
235 		extcon_set_state_sync(mc_data->extcon, EXTCON_JACK_HEADPHONE, true);
236 		extcon_set_state_sync(mc_data->extcon, EXTCON_JACK_MICROPHONE, false);
237 		return;
238 	}
239 	ret = iio_read_channel_processed(mc_data->adc, &adc);
240 	if (ret < 0) {
241 		/* failed to read ADC, so assume headphone */
242 		snd_soc_jack_report(jack_headset, SND_JACK_HEADPHONE, SND_JACK_HEADSET);
243 		extcon_set_state_sync(mc_data->extcon, EXTCON_JACK_HEADPHONE, true);
244 		extcon_set_state_sync(mc_data->extcon, EXTCON_JACK_MICROPHONE, false);
245 
246 	} else {
247 		snd_soc_jack_report(jack_headset,
248 				    snd_soc_jack_get_type(jack_headset, adc),
249 				    SND_JACK_HEADSET);
250 		extcon_set_state_sync(mc_data->extcon, EXTCON_JACK_HEADPHONE, true);
251 
252 		if (snd_soc_jack_get_type(jack_headset, adc) == SND_JACK_HEADSET) {
253 			extcon_set_state_sync(mc_data->extcon, EXTCON_JACK_MICROPHONE, true);
254 			if (mc_data->poller)
255 				mc_keys_poller_start(mc_data->poller);
256 		}
257 	}
258 };
259 
headset_det_irq_thread(int irq,void * data)260 static irqreturn_t headset_det_irq_thread(int irq, void *data)
261 {
262 	struct multicodecs_data *mc_data = (struct multicodecs_data *)data;
263 
264 	queue_delayed_work(system_power_efficient_wq, &mc_data->handler, msecs_to_jiffies(200));
265 
266 	return IRQ_HANDLED;
267 };
268 
mc_hp_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)269 static int mc_hp_event(struct snd_soc_dapm_widget *w,
270 		       struct snd_kcontrol *kcontrol, int event)
271 {
272 	struct snd_soc_card *card = w->dapm->card;
273 	struct multicodecs_data *mc_data = snd_soc_card_get_drvdata(card);
274 
275 	switch (event) {
276 	case SND_SOC_DAPM_POST_PMU:
277 		gpiod_set_value_cansleep(mc_data->hp_ctl_gpio, 1);
278 		break;
279 	case SND_SOC_DAPM_PRE_PMD:
280 		gpiod_set_value_cansleep(mc_data->hp_ctl_gpio, 0);
281 		break;
282 	default:
283 		return 0;
284 
285 	}
286 
287 	return 0;
288 }
289 
mc_spk_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)290 static int mc_spk_event(struct snd_soc_dapm_widget *w,
291 			struct snd_kcontrol *kcontrol, int event)
292 {
293 	struct snd_soc_card *card = w->dapm->card;
294 	struct multicodecs_data *mc_data = snd_soc_card_get_drvdata(card);
295 
296 	switch (event) {
297 	case SND_SOC_DAPM_POST_PMU:
298 		gpiod_set_value_cansleep(mc_data->spk_ctl_gpio, 1);
299 		break;
300 	case SND_SOC_DAPM_PRE_PMD:
301 		gpiod_set_value_cansleep(mc_data->spk_ctl_gpio, 0);
302 		break;
303 	default:
304 		return 0;
305 
306 	}
307 
308 	return 0;
309 }
310 
311 static const struct snd_soc_dapm_widget mc_dapm_widgets[] = {
312 
313 	SND_SOC_DAPM_HP("Headphone", NULL),
314 	SND_SOC_DAPM_SPK("Speaker", NULL),
315 	SND_SOC_DAPM_MIC("Main Mic", NULL),
316 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
317 	SND_SOC_DAPM_SUPPLY("Speaker Power",
318 			    SND_SOC_NOPM, 0, 0,
319 			    mc_spk_event,
320 			    SND_SOC_DAPM_POST_PMU |
321 			    SND_SOC_DAPM_PRE_PMD),
322 	SND_SOC_DAPM_SUPPLY("Headphone Power",
323 			    SND_SOC_NOPM, 0, 0,
324 			    mc_hp_event,
325 			    SND_SOC_DAPM_POST_PMU |
326 			    SND_SOC_DAPM_PRE_PMD),
327 };
328 
329 static const struct snd_kcontrol_new mc_controls[] = {
330 	SOC_DAPM_PIN_SWITCH("Headphone"),
331 	SOC_DAPM_PIN_SWITCH("Speaker"),
332 	SOC_DAPM_PIN_SWITCH("Main Mic"),
333 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
334 };
335 
rk_multicodecs_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)336 static int rk_multicodecs_hw_params(struct snd_pcm_substream *substream,
337 				    struct snd_pcm_hw_params *params)
338 {
339 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
340 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
341 	struct snd_soc_dai *codec_dai;
342 	struct multicodecs_data *mc_data = snd_soc_card_get_drvdata(rtd->card);
343 	unsigned int mclk;
344 	int ret, i;
345 
346 	mclk = params_rate(params) * mc_data->mclk_fs;
347 
348 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
349 		ret = snd_soc_dai_set_sysclk(codec_dai, substream->stream, mclk,
350 					     SND_SOC_CLOCK_IN);
351 		if (ret && ret != -ENOTSUPP) {
352 			pr_err("Set codec_dai sysclk failed: %d\n", ret);
353 			goto out;
354 		}
355 	}
356 
357 	ret = snd_soc_dai_set_sysclk(cpu_dai, substream->stream, mclk,
358 				     SND_SOC_CLOCK_OUT);
359 	if (ret && ret != -ENOTSUPP) {
360 		pr_err("Set cpu_dai sysclk failed: %d\n", ret);
361 		goto out;
362 	}
363 
364 	return 0;
365 
366 out:
367 	return ret;
368 }
369 
rk_dailink_init(struct snd_soc_pcm_runtime * rtd)370 static int rk_dailink_init(struct snd_soc_pcm_runtime *rtd)
371 {
372 	struct multicodecs_data *mc_data = snd_soc_card_get_drvdata(rtd->card);
373 	struct snd_soc_card *card = rtd->card;
374 	struct snd_soc_jack *jack_headset;
375 	int ret, irq;
376 	struct snd_soc_jack_pin *pins;
377 	struct snd_soc_jack_zone *zones;
378 	struct snd_soc_jack_pin jack_pins[] = {
379 		{
380 			.pin = "Headphone",
381 			.mask = SND_JACK_HEADPHONE,
382 		}, {
383 			.pin = "Headset Mic",
384 			.mask = SND_JACK_MICROPHONE,
385 		},
386 	};
387 	struct snd_soc_jack_zone headset_zones[] = {
388 		{
389 			.min_mv = 0,
390 			.max_mv = 222,
391 			.jack_type = SND_JACK_HEADPHONE,
392 		}, {
393 			.min_mv = 223,
394 			.max_mv = 1500,
395 			.jack_type = SND_JACK_HEADSET,
396 		}, {
397 			.min_mv = 1501,
398 			.max_mv = UINT_MAX,
399 			.jack_type = SND_JACK_HEADPHONE,
400 		}
401 	};
402 
403 	if ((!mc_data->codec_hp_det) && (gpiod_to_irq(mc_data->hp_det_gpio) < 0)) {
404 		dev_info(card->dev, "Don't need to map headset detect gpio to irq\n");
405 		return 0;
406 	}
407 
408 	jack_headset = devm_kzalloc(card->dev, sizeof(*jack_headset), GFP_KERNEL);
409 	if (!jack_headset)
410 		return -ENOMEM;
411 
412 	pins = devm_kmemdup(card->dev, jack_pins,
413 			    sizeof(*jack_pins) * ARRAY_SIZE(jack_pins), GFP_KERNEL);
414 	if (!pins)
415 		return -ENOMEM;
416 
417 	zones = devm_kmemdup(card->dev, headset_zones,
418 			     sizeof(*headset_zones) * ARRAY_SIZE(headset_zones), GFP_KERNEL);
419 	if (!zones)
420 		return -ENOMEM;
421 
422 	ret = snd_soc_card_jack_new(card, "Headset",
423 				    SND_JACK_HEADSET,
424 				    jack_headset,
425 				    pins, ARRAY_SIZE(jack_pins));
426 	if (ret)
427 		return ret;
428 	ret = snd_soc_jack_add_zones(jack_headset, ARRAY_SIZE(headset_zones), zones);
429 	if (ret)
430 		return ret;
431 
432 	mc_data->jack_headset = jack_headset;
433 
434 	if (mc_data->codec_hp_det) {
435 		struct snd_soc_dai *codec_dai;
436 		int i;
437 
438 		/* set jack for the first successful one */
439 		for_each_rtd_codec_dais(rtd, i, codec_dai) {
440 			ret = snd_soc_component_set_jack(codec_dai->component,
441 							 jack_headset, NULL);
442 			if (ret >= 0)
443 				break;
444 		}
445 	} else {
446 		irq = gpiod_to_irq(mc_data->hp_det_gpio);
447 		if (irq >= 0) {
448 			ret = devm_request_threaded_irq(card->dev, irq, NULL,
449 							headset_det_irq_thread,
450 							IRQF_TRIGGER_RISING |
451 							IRQF_TRIGGER_FALLING |
452 							IRQF_ONESHOT,
453 							"headset_detect",
454 							mc_data);
455 			if (ret) {
456 				dev_err(card->dev, "Failed to request headset detect irq");
457 				return ret;
458 			}
459 
460 			queue_delayed_work(system_power_efficient_wq,
461 					   &mc_data->handler, msecs_to_jiffies(50));
462 		}
463 	}
464 
465 	return 0;
466 }
467 
rk_multicodecs_parse_daifmt(struct device_node * node,struct device_node * codec,struct multicodecs_data * mc_data,const char * prefix)468 static int rk_multicodecs_parse_daifmt(struct device_node *node,
469 				       struct device_node *codec,
470 				       struct multicodecs_data *mc_data,
471 				       const char *prefix)
472 {
473 	struct snd_soc_dai_link *dai_link = &mc_data->dai_link;
474 	struct device_node *bitclkmaster = NULL;
475 	struct device_node *framemaster = NULL;
476 	unsigned int daifmt;
477 
478 	daifmt = snd_soc_of_parse_daifmt(node, prefix,
479 					 &bitclkmaster, &framemaster);
480 
481 	daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
482 
483 	if (strlen(prefix) && !bitclkmaster && !framemaster) {
484 		/*
485 		 * No dai-link level and master setting was not found from
486 		 * sound node level, revert back to legacy DT parsing and
487 		 * take the settings from codec node.
488 		 */
489 		pr_debug("%s: Revert to legacy daifmt parsing\n", __func__);
490 
491 		daifmt = snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) |
492 			(daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
493 	} else {
494 		if (codec == bitclkmaster)
495 			daifmt |= (codec == framemaster) ?
496 				SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
497 		else
498 			daifmt |= (codec == framemaster) ?
499 				SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
500 	}
501 
502 	/*
503 	 * If there is NULL format means that the format isn't specified, we
504 	 * need to set i2s format by default.
505 	 */
506 	if (!(daifmt & SND_SOC_DAIFMT_FORMAT_MASK))
507 		daifmt |= SND_SOC_DAIFMT_I2S;
508 
509 	dai_link->dai_fmt = daifmt;
510 
511 	of_node_put(bitclkmaster);
512 	of_node_put(framemaster);
513 
514 	return 0;
515 }
516 
wait_locked_card(struct device_node * np,struct device * dev)517 static int wait_locked_card(struct device_node *np, struct device *dev)
518 {
519 	char *propname = "rockchip,wait-card-locked";
520 	u32 cards[WAIT_CARDS];
521 	int num;
522 	int ret;
523 #ifndef MODULE
524 	int i;
525 #endif
526 
527 	ret = of_property_count_u32_elems(np, propname);
528 	if (ret < 0) {
529 		if (ret == -EINVAL) {
530 			/*
531 			 * -EINVAL means the property does not exist, this is
532 			 * fine.
533 			 */
534 			return 0;
535 		}
536 
537 		dev_err(dev, "Property '%s' elems could not be read: %d\n",
538 			propname, ret);
539 		return ret;
540 	}
541 
542 	num = ret;
543 	if (num > WAIT_CARDS)
544 		num = WAIT_CARDS;
545 
546 	ret = of_property_read_u32_array(np, propname, cards, num);
547 	if (ret < 0) {
548 		if (ret == -EINVAL) {
549 			/*
550 			 * -EINVAL means the property does not exist, this is
551 			 * fine.
552 			 */
553 			return 0;
554 		}
555 
556 		dev_err(dev, "Property '%s' could not be read: %d\n",
557 			propname, ret);
558 		return ret;
559 	}
560 
561 	ret = 0;
562 #ifndef MODULE
563 	for (i = 0; i < num; i++) {
564 		if (!snd_card_locked(cards[i])) {
565 			dev_warn(dev, "card: %d has not been locked, re-probe again\n",
566 				 cards[i]);
567 			ret = -EPROBE_DEFER;
568 			break;
569 		}
570 	}
571 #endif
572 
573 	return ret;
574 }
575 
576 static struct snd_soc_ops rk_ops = {
577 	.hw_params = rk_multicodecs_hw_params,
578 };
579 
rk_multicodecs_probe(struct platform_device * pdev)580 static int rk_multicodecs_probe(struct platform_device *pdev)
581 {
582 	struct snd_soc_card *card;
583 	struct device_node *np = pdev->dev.of_node;
584 	struct snd_soc_dai_link *link;
585 	struct snd_soc_dai_link_component *cpus;
586 	struct snd_soc_dai_link_component *platforms;
587 	struct snd_soc_dai_link_component *codecs;
588 	struct multicodecs_data *mc_data;
589 	struct of_phandle_args args;
590 	struct device_node *node;
591 	struct input_dev *input;
592 	u32 val;
593 	int count, value;
594 	int ret = 0, i = 0, idx = 0;
595 	const char *prefix = "rockchip,";
596 
597 	ret = wait_locked_card(np, &pdev->dev);
598 	if (ret < 0) {
599 		dev_err(&pdev->dev, "check_lock_card failed: %d\n", ret);
600 		return ret;
601 	}
602 
603 	mc_data = devm_kzalloc(&pdev->dev, sizeof(*mc_data), GFP_KERNEL);
604 	if (!mc_data)
605 		return -ENOMEM;
606 
607 	cpus = devm_kzalloc(&pdev->dev, sizeof(*cpus), GFP_KERNEL);
608 	if (!cpus)
609 		return -ENOMEM;
610 
611 	platforms = devm_kzalloc(&pdev->dev, sizeof(*platforms), GFP_KERNEL);
612 	if (!platforms)
613 		return -ENOMEM;
614 
615 	card = &mc_data->snd_card;
616 	card->dev = &pdev->dev;
617 
618 	/* Parse the card name from DT */
619 	ret = snd_soc_of_parse_card_name(card, "rockchip,card-name");
620 	if (ret < 0)
621 		return ret;
622 
623 	link = &mc_data->dai_link;
624 	link->name = "dailink-multicodecs";
625 	link->stream_name = link->name;
626 	link->init = rk_dailink_init;
627 	link->ops = &rk_ops;
628 	link->cpus = cpus;
629 	link->platforms	= platforms;
630 	link->num_cpus	= 1;
631 	link->num_platforms = 1;
632 	link->ignore_pmdown_time = 1;
633 
634 	card->dai_link = link;
635 	card->num_links = 1;
636 	card->dapm_widgets = mc_dapm_widgets;
637 	card->num_dapm_widgets = ARRAY_SIZE(mc_dapm_widgets);
638 	card->controls = mc_controls;
639 	card->num_controls = ARRAY_SIZE(mc_controls);
640 	card->num_aux_devs = 0;
641 
642 	count = of_count_phandle_with_args(np, "rockchip,codec", NULL);
643 	if (count < 0)
644 		return -EINVAL;
645 
646 	/* refine codecs, remove unavailable node */
647 	for (i = 0; i < count; i++) {
648 		node = of_parse_phandle(np, "rockchip,codec", i);
649 		if (!node)
650 			return -ENODEV;
651 		if (of_device_is_available(node))
652 			idx++;
653 	}
654 
655 	if (!idx)
656 		return -ENODEV;
657 
658 	codecs = devm_kcalloc(&pdev->dev, idx,
659 			      sizeof(*codecs), GFP_KERNEL);
660 	if (!codecs)
661 		return -ENOMEM;
662 
663 	link->codecs = codecs;
664 	link->num_codecs = idx;
665 	idx = 0;
666 	for (i = 0; i < count; i++) {
667 		node = of_parse_phandle(np, "rockchip,codec", i);
668 		if (!node)
669 			return -ENODEV;
670 		if (!of_device_is_available(node))
671 			continue;
672 
673 		ret = of_parse_phandle_with_fixed_args(np, "rockchip,codec",
674 						       0, i, &args);
675 		if (ret)
676 			return ret;
677 
678 		codecs[idx].of_node = node;
679 		ret = snd_soc_get_dai_name(&args, &codecs[idx].dai_name);
680 		if (ret)
681 			return ret;
682 		idx++;
683 	}
684 
685 	/* Only reference the codecs[0].of_node which maybe as master. */
686 	rk_multicodecs_parse_daifmt(np, codecs[0].of_node, mc_data, prefix);
687 
688 	link->cpus->of_node = of_parse_phandle(np, "rockchip,cpu", 0);
689 	if (!link->cpus->of_node)
690 		return -ENODEV;
691 
692 	link->platforms->of_node = link->cpus->of_node;
693 
694 	mc_data->mclk_fs = DEFAULT_MCLK_FS;
695 	if (!of_property_read_u32(np, "rockchip,mclk-fs", &val))
696 		mc_data->mclk_fs = val;
697 
698 	mc_data->codec_hp_det =
699 		of_property_read_bool(np, "rockchip,codec-hp-det");
700 
701 	mc_data->adc = devm_iio_channel_get(&pdev->dev, "adc-detect");
702 
703 	if (IS_ERR(mc_data->adc)) {
704 		if (PTR_ERR(mc_data->adc) != -EPROBE_DEFER) {
705 			mc_data->adc = NULL;
706 			dev_warn(&pdev->dev, "Failed to get ADC channel");
707 		}
708 	} else {
709 		if (mc_data->adc->channel->type != IIO_VOLTAGE)
710 			return -EINVAL;
711 
712 		if (device_property_read_u32(&pdev->dev, "keyup-threshold-microvolt",
713 					&mc_data->keyup_voltage)) {
714 			dev_warn(&pdev->dev, "Invalid or missing keyup voltage\n");
715 			return -EINVAL;
716 		}
717 		mc_data->keyup_voltage /= 1000;
718 
719 		ret = mc_keys_load_keymap(&pdev->dev, mc_data);
720 		if (ret)
721 			return ret;
722 
723 		input = devm_input_allocate_device(&pdev->dev);
724 		if (IS_ERR(input)) {
725 			dev_err(&pdev->dev, "failed to allocate input device\n");
726 			return PTR_ERR(input);
727 		}
728 
729 		input_set_drvdata(input, mc_data);
730 
731 		input->name = "headset-keys";
732 		input->phys = "headset-keys/input0";
733 		input->id.bustype = BUS_HOST;
734 		input->id.vendor = 0x0001;
735 		input->id.product = 0x0001;
736 		input->id.version = 0x0100;
737 
738 		__set_bit(EV_KEY, input->evbit);
739 		for (i = 0; i < mc_data->num_keys; i++)
740 			__set_bit(mc_data->map[i].keycode, input->keybit);
741 
742 		if (device_property_read_bool(&pdev->dev, "autorepeat"))
743 			__set_bit(EV_REP, input->evbit);
744 
745 		mc_data->input = input;
746 		ret = mc_keys_setup_polling(mc_data, mc_keys_poll);
747 		if (ret) {
748 			dev_err(&pdev->dev, "Unable to set up polling: %d\n", ret);
749 			return ret;
750 		}
751 
752 		if (!device_property_read_u32(&pdev->dev, "poll-interval", &value))
753 			mc_set_poll_interval(mc_data->poller, value);
754 
755 		ret = input_register_device(mc_data->input);
756 		if (ret) {
757 			dev_err(&pdev->dev, "Unable to register input device: %d\n", ret);
758 			return ret;
759 		}
760 	}
761 
762 	INIT_DEFERRABLE_WORK(&mc_data->handler, adc_jack_handler);
763 
764 	mc_data->spk_ctl_gpio = devm_gpiod_get_optional(&pdev->dev,
765 							"spk-con",
766 							GPIOD_OUT_LOW);
767 	if (IS_ERR(mc_data->spk_ctl_gpio))
768 		return PTR_ERR(mc_data->spk_ctl_gpio);
769 
770 	mc_data->hp_ctl_gpio = devm_gpiod_get_optional(&pdev->dev,
771 						       "hp-con",
772 						       GPIOD_OUT_LOW);
773 	if (IS_ERR(mc_data->hp_ctl_gpio))
774 		return PTR_ERR(mc_data->hp_ctl_gpio);
775 
776 	mc_data->hp_det_gpio = devm_gpiod_get_optional(&pdev->dev, "hp-det", GPIOD_IN);
777 	if (IS_ERR(mc_data->hp_det_gpio))
778 		return PTR_ERR(mc_data->hp_det_gpio);
779 
780 	mc_data->extcon = devm_extcon_dev_allocate(&pdev->dev, headset_extcon_cable);
781 	if (IS_ERR(mc_data->extcon)) {
782 		dev_err(&pdev->dev, "allocate extcon failed\n");
783 		return PTR_ERR(mc_data->extcon);
784 	}
785 
786 	ret = devm_extcon_dev_register(&pdev->dev, mc_data->extcon);
787 	if (ret) {
788 		dev_err(&pdev->dev, "failed to register extcon: %d\n", ret);
789 		return ret;
790 	}
791 
792 	ret = snd_soc_of_parse_audio_routing(card, "rockchip,audio-routing");
793 	if (ret < 0)
794 		dev_warn(&pdev->dev, "Audio routing invalid/unspecified\n");
795 
796 	snd_soc_card_set_drvdata(card, mc_data);
797 
798 	ret = devm_snd_soc_register_card(&pdev->dev, card);
799 	if (ret == -EPROBE_DEFER)
800 		return -EPROBE_DEFER;
801 	if (ret) {
802 		dev_err(&pdev->dev, "card register failed %d\n", ret);
803 		return ret;
804 	}
805 
806 	platform_set_drvdata(pdev, card);
807 
808 	return ret;
809 }
810 
811 static const struct of_device_id rockchip_multicodecs_of_match[] = {
812 	{ .compatible = "rockchip,multicodecs-card", },
813 	{},
814 };
815 
816 MODULE_DEVICE_TABLE(of, rockchip_multicodecs_of_match);
817 
818 static struct platform_driver rockchip_multicodecs_driver = {
819 	.probe = rk_multicodecs_probe,
820 	.driver = {
821 		.name = DRV_NAME,
822 		.pm = &snd_soc_pm_ops,
823 		.of_match_table = rockchip_multicodecs_of_match,
824 	},
825 };
826 
827 module_platform_driver(rockchip_multicodecs_driver);
828 
829 MODULE_AUTHOR("Sugar Zhang <sugar.zhang@rock-chips.com>");
830 MODULE_DESCRIPTION("Rockchip General Multicodecs ASoC driver");
831 MODULE_LICENSE("GPL v2");
832 MODULE_ALIAS("platform:" DRV_NAME);
833