forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-09-20 cf4ce59b3b70238352c7f1729f0f7223214828ad
kernel/sound/soc/rockchip/rockchip_multicodecs.c
....@@ -19,9 +19,18 @@
1919 *
2020 */
2121
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>
2229 #include <linux/module.h>
30
+#include <linux/of_gpio.h>
2331 #include <linux/platform_device.h>
2432 #include <linux/slab.h>
33
+#include <linux/workqueue.h>
2534 #include <sound/core.h>
2635 #include <sound/jack.h>
2736 #include <sound/pcm.h>
....@@ -29,39 +38,320 @@
2938 #include <sound/soc.h>
3039 #include <sound/soc-dapm.h>
3140
32
-#include "../codecs/rk3308_codec_provider.h"
33
-
3441 #define DRV_NAME "rk-multicodecs"
35
-#define MAX_CODECS 2
3642 #define WAIT_CARDS (SNDRV_CARDS - 1)
3743 #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
+};
3857
3958 struct multicodecs_data {
4059 struct snd_soc_card snd_card;
4160 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;
4268 unsigned int mclk_fs;
4369 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;
4476 };
4577
46
-static struct snd_soc_jack mc_hp_jack;
78
+static const unsigned int headset_extcon_cable[] = {
79
+ EXTCON_JACK_MICROPHONE,
80
+ EXTCON_JACK_HEADPHONE,
81
+ EXTCON_NONE,
82
+};
83
+
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
+
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
+
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
+
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
+
118
+static void mc_keys_poller_stop(struct input_dev_poller *poller)
119
+{
120
+ cancel_delayed_work_sync(&poller->work);
121
+}
122
+
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
+
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
+
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
+
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
+
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
+
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
+
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
+};
47335
48336 static int rk_multicodecs_hw_params(struct snd_pcm_substream *substream,
49337 struct snd_pcm_hw_params *params)
50338 {
51
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
52
- struct snd_soc_dai *codec_dai = rtd->codec_dai;
53
- struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
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;
54342 struct multicodecs_data *mc_data = snd_soc_card_get_drvdata(rtd->card);
55343 unsigned int mclk;
56
- int ret;
344
+ int ret, i;
57345
58346 mclk = params_rate(params) * mc_data->mclk_fs;
59347
60
- ret = snd_soc_dai_set_sysclk(codec_dai, substream->stream, mclk,
61
- SND_SOC_CLOCK_IN);
62
- if (ret && ret != -ENOTSUPP) {
63
- pr_err("Set codec_dai sysclk failed: %d\n", ret);
64
- goto out;
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
+ }
65355 }
66356
67357 ret = snd_soc_dai_set_sysclk(cpu_dai, substream->stream, mclk,
....@@ -80,16 +370,93 @@
80370 static int rk_dailink_init(struct snd_soc_pcm_runtime *rtd)
81371 {
82372 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;
83433
84434 if (mc_data->codec_hp_det) {
85
- snd_soc_card_jack_new(rtd->card, "Headphones",
86
- SND_JACK_HEADPHONE,
87
- &mc_hp_jack, NULL, 0);
435
+ struct snd_soc_dai *codec_dai;
436
+ int i;
88437
89
-#ifdef CONFIG_SND_SOC_RK3308
90
- if (rk3308_codec_set_jack_detect_cb)
91
- rk3308_codec_set_jack_detect_cb(rtd->codec_dai->component, &mc_hp_jack);
92
-#endif
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
+ }
93460 }
94461
95462 return 0;
....@@ -212,12 +579,15 @@
212579 struct snd_soc_card *card;
213580 struct device_node *np = pdev->dev.of_node;
214581 struct snd_soc_dai_link *link;
582
+ struct snd_soc_dai_link_component *cpus;
583
+ struct snd_soc_dai_link_component *platforms;
215584 struct snd_soc_dai_link_component *codecs;
216585 struct multicodecs_data *mc_data;
217586 struct of_phandle_args args;
218587 struct device_node *node;
588
+ struct input_dev *input;
219589 u32 val;
220
- int count;
590
+ int count, value, irq;
221591 int ret = 0, i = 0, idx = 0;
222592 const char *prefix = "rockchip,";
223593
....@@ -229,6 +599,14 @@
229599
230600 mc_data = devm_kzalloc(&pdev->dev, sizeof(*mc_data), GFP_KERNEL);
231601 if (!mc_data)
602
+ return -ENOMEM;
603
+
604
+ cpus = devm_kzalloc(&pdev->dev, sizeof(*cpus), GFP_KERNEL);
605
+ if (!cpus)
606
+ return -ENOMEM;
607
+
608
+ platforms = devm_kzalloc(&pdev->dev, sizeof(*platforms), GFP_KERNEL);
609
+ if (!platforms)
232610 return -ENOMEM;
233611
234612 card = &mc_data->snd_card;
....@@ -244,13 +622,22 @@
244622 link->stream_name = link->name;
245623 link->init = rk_dailink_init;
246624 link->ops = &rk_ops;
625
+ link->cpus = cpus;
626
+ link->platforms = platforms;
627
+ link->num_cpus = 1;
628
+ link->num_platforms = 1;
629
+ link->ignore_pmdown_time = 1;
247630
248631 card->dai_link = link;
249632 card->num_links = 1;
633
+ card->dapm_widgets = mc_dapm_widgets;
634
+ card->num_dapm_widgets = ARRAY_SIZE(mc_dapm_widgets);
635
+ card->controls = mc_controls;
636
+ card->num_controls = ARRAY_SIZE(mc_controls);
250637 card->num_aux_devs = 0;
251638
252639 count = of_count_phandle_with_args(np, "rockchip,codec", NULL);
253
- if (count < 0 || count > MAX_CODECS)
640
+ if (count < 0)
254641 return -EINVAL;
255642
256643 /* refine codecs, remove unavailable node */
....@@ -267,6 +654,9 @@
267654
268655 codecs = devm_kcalloc(&pdev->dev, idx,
269656 sizeof(*codecs), GFP_KERNEL);
657
+ if (!codecs)
658
+ return -ENOMEM;
659
+
270660 link->codecs = codecs;
271661 link->num_codecs = idx;
272662 idx = 0;
....@@ -292,11 +682,11 @@
292682 /* Only reference the codecs[0].of_node which maybe as master. */
293683 rk_multicodecs_parse_daifmt(np, codecs[0].of_node, mc_data, prefix);
294684
295
- link->cpu_of_node = of_parse_phandle(np, "rockchip,cpu", 0);
296
- if (!link->cpu_of_node)
685
+ link->cpus->of_node = of_parse_phandle(np, "rockchip,cpu", 0);
686
+ if (!link->cpus->of_node)
297687 return -ENODEV;
298688
299
- link->platform_of_node = link->cpu_of_node;
689
+ link->platforms->of_node = link->cpus->of_node;
300690
301691 mc_data->mclk_fs = DEFAULT_MCLK_FS;
302692 if (!of_property_read_u32(np, "rockchip,mclk-fs", &val))
....@@ -305,19 +695,132 @@
305695 mc_data->codec_hp_det =
306696 of_property_read_bool(np, "rockchip,codec-hp-det");
307697
308
- snd_soc_card_set_drvdata(card, mc_data);
698
+ mc_data->adc = devm_iio_channel_get(&pdev->dev, "adc-detect");
309699
310
- ret = devm_snd_soc_register_card(&pdev->dev, card);
311
- if (ret == -EPROBE_DEFER)
312
- return -EPROBE_DEFER;
700
+ if (IS_ERR(mc_data->adc)) {
701
+ if (PTR_ERR(mc_data->adc) != -EPROBE_DEFER) {
702
+ mc_data->adc = NULL;
703
+ dev_warn(&pdev->dev, "Failed to get ADC channel");
704
+ }
705
+ } else {
706
+ if (mc_data->adc->channel->type != IIO_VOLTAGE)
707
+ return -EINVAL;
708
+
709
+ if (device_property_read_u32(&pdev->dev, "keyup-threshold-microvolt",
710
+ &mc_data->keyup_voltage)) {
711
+ dev_warn(&pdev->dev, "Invalid or missing keyup voltage\n");
712
+ return -EINVAL;
713
+ }
714
+ mc_data->keyup_voltage /= 1000;
715
+
716
+ ret = mc_keys_load_keymap(&pdev->dev, mc_data);
717
+ if (ret)
718
+ return ret;
719
+
720
+ input = devm_input_allocate_device(&pdev->dev);
721
+ if (IS_ERR(input)) {
722
+ dev_err(&pdev->dev, "Failed to allocate input device\n");
723
+ return PTR_ERR(input);
724
+ }
725
+
726
+ input_set_drvdata(input, mc_data);
727
+
728
+ input->name = "headset-keys";
729
+ input->phys = "headset-keys/input0";
730
+ input->id.bustype = BUS_HOST;
731
+ input->id.vendor = 0x0001;
732
+ input->id.product = 0x0001;
733
+ input->id.version = 0x0100;
734
+
735
+ __set_bit(EV_KEY, input->evbit);
736
+ for (i = 0; i < mc_data->num_keys; i++)
737
+ __set_bit(mc_data->map[i].keycode, input->keybit);
738
+
739
+ if (device_property_read_bool(&pdev->dev, "autorepeat"))
740
+ __set_bit(EV_REP, input->evbit);
741
+
742
+ mc_data->input = input;
743
+ ret = mc_keys_setup_polling(mc_data, mc_keys_poll);
744
+ if (ret) {
745
+ dev_err(&pdev->dev, "Failed to set up polling: %d\n", ret);
746
+ return ret;
747
+ }
748
+
749
+ if (!device_property_read_u32(&pdev->dev, "poll-interval", &value))
750
+ mc_set_poll_interval(mc_data->poller, value);
751
+
752
+ ret = input_register_device(mc_data->input);
753
+ if (ret) {
754
+ dev_err(&pdev->dev, "Failed to register input device: %d\n", ret);
755
+ return ret;
756
+ }
757
+ }
758
+
759
+ INIT_DEFERRABLE_WORK(&mc_data->handler, adc_jack_handler);
760
+
761
+ mc_data->spk_ctl_gpio = devm_gpiod_get_optional(&pdev->dev,
762
+ "spk-con",
763
+ GPIOD_OUT_LOW);
764
+ if (IS_ERR(mc_data->spk_ctl_gpio))
765
+ return PTR_ERR(mc_data->spk_ctl_gpio);
766
+
767
+ mc_data->hp_ctl_gpio = devm_gpiod_get_optional(&pdev->dev,
768
+ "hp-con",
769
+ GPIOD_OUT_LOW);
770
+ if (IS_ERR(mc_data->hp_ctl_gpio))
771
+ return PTR_ERR(mc_data->hp_ctl_gpio);
772
+
773
+ mc_data->hp_det_gpio = devm_gpiod_get_optional(&pdev->dev, "hp-det", GPIOD_IN);
774
+ if (IS_ERR(mc_data->hp_det_gpio))
775
+ return PTR_ERR(mc_data->hp_det_gpio);
776
+
777
+ mc_data->extcon = devm_extcon_dev_allocate(&pdev->dev, headset_extcon_cable);
778
+ if (IS_ERR(mc_data->extcon)) {
779
+ dev_err(&pdev->dev, "Failed to allocate extcon\n");
780
+ return PTR_ERR(mc_data->extcon);
781
+ }
782
+
783
+ ret = devm_extcon_dev_register(&pdev->dev, mc_data->extcon);
313784 if (ret) {
314
- dev_err(&pdev->dev, "card register failed %d\n", ret);
785
+ dev_err(&pdev->dev, "Failed to register extcon: %d\n", ret);
315786 return ret;
316787 }
317788
789
+ snd_soc_of_parse_audio_routing(card, "rockchip,audio-routing");
790
+
791
+ snd_soc_card_set_drvdata(card, mc_data);
318792 platform_set_drvdata(pdev, card);
319793
794
+ ret = devm_snd_soc_register_card(&pdev->dev, card);
795
+ if (ret) {
796
+ dev_err(&pdev->dev, "Failed to register card: %d\n", ret);
797
+ return ret;
798
+ }
799
+
800
+ irq = gpiod_to_irq(mc_data->hp_det_gpio);
801
+ if (irq >= 0)
802
+ queue_delayed_work(system_power_efficient_wq,
803
+ &mc_data->handler, msecs_to_jiffies(50));
804
+
320805 return ret;
806
+}
807
+
808
+static int rk_multicodec_remove(struct platform_device *pdev)
809
+{
810
+ struct snd_soc_card *card = platform_get_drvdata(pdev);
811
+ struct multicodecs_data *mc_data = snd_soc_card_get_drvdata(card);
812
+
813
+ cancel_delayed_work_sync(&mc_data->handler);
814
+
815
+ return 0;
816
+}
817
+
818
+static void rk_multicodec_shutdown(struct platform_device *pdev)
819
+{
820
+ struct snd_soc_card *card = platform_get_drvdata(pdev);
821
+ struct multicodecs_data *mc_data = snd_soc_card_get_drvdata(card);
822
+
823
+ cancel_delayed_work_sync(&mc_data->handler);
321824 }
322825
323826 static const struct of_device_id rockchip_multicodecs_of_match[] = {
....@@ -329,6 +832,8 @@
329832
330833 static struct platform_driver rockchip_multicodecs_driver = {
331834 .probe = rk_multicodecs_probe,
835
+ .remove = rk_multicodec_remove,
836
+ .shutdown = rk_multicodec_shutdown,
332837 .driver = {
333838 .name = DRV_NAME,
334839 .pm = &snd_soc_pm_ops,