]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/greybus/audio_codec.c
greybus: audio: codec driver cleanup
[karo-tx-linux.git] / drivers / staging / greybus / audio_codec.c
1 /*
2  * Greybus audio driver
3  * Copyright 2015 Google Inc.
4  * Copyright 2015 Linaro Ltd.
5  *
6  * Released under the GPLv2 only.
7  */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <sound/soc.h>
11 #include <sound/pcm_params.h>
12 #include <sound/msm-dynamic-dailink.h>
13
14 #include "audio_codec.h"
15 #include "audio_apbridgea.h"
16 #include "audio_manager.h"
17
18 static DEFINE_MUTEX(gb_codec_list_lock);
19 static LIST_HEAD(gb_codec_list);
20
21 struct gbaudio_dai *gbaudio_find_dai(struct gbaudio_codec_info *gbcodec,
22                                      int data_cport, const char *name)
23 {
24         struct gbaudio_dai *dai;
25
26         list_for_each_entry(dai, &gbcodec->dai_list, list) {
27                 if (name && !strncmp(dai->name, name, NAME_SIZE))
28                         return dai;
29                 if ((data_cport != -1) && (dai->data_cport == data_cport))
30                         return dai;
31         }
32         return NULL;
33 }
34
35 /*
36  * codec DAI ops
37  */
38 static int gbcodec_startup(struct snd_pcm_substream *substream,
39                            struct snd_soc_dai *dai)
40 {
41         int ret;
42         __u16 i2s_port, cportid;
43
44         struct gbaudio_dai *gb_dai;
45         struct gbaudio_codec_info *gb = dev_get_drvdata(dai->dev);
46
47         /* find the dai */
48         mutex_lock(&gb->lock);
49         gb_dai = gbaudio_find_dai(gb, -1, dai->name);
50         if (!gb_dai) {
51                 dev_err(dai->dev, "%s: DAI not registered\n", dai->name);
52                 mutex_unlock(&gb->lock);
53                 return -EINVAL;
54         }
55
56         /* register cport */
57         i2s_port = 0;   /* fixed for now */
58         cportid = gb_dai->connection->hd_cport_id;
59         ret = gb_audio_apbridgea_register_cport(gb_dai->connection, i2s_port,
60                                                 cportid);
61         dev_dbg(dai->dev, "Register %s:%d DAI, ret:%d\n", dai->name, cportid,
62                 ret);
63
64         if (!ret)
65                 atomic_inc(&gb_dai->users);
66         mutex_unlock(&gb->lock);
67
68         return ret;
69 }
70
71 static void gbcodec_shutdown(struct snd_pcm_substream *substream,
72                              struct snd_soc_dai *dai)
73 {
74         int ret;
75         __u16 i2s_port, cportid;
76
77         struct gbaudio_dai *gb_dai;
78         struct gbaudio_codec_info *gb = dev_get_drvdata(dai->dev);
79
80         /* find the dai */
81         mutex_lock(&gb->lock);
82         gb_dai = gbaudio_find_dai(gb, -1, dai->name);
83         if (!gb_dai) {
84                 dev_err(dai->dev, "%s: DAI not registered\n", dai->name);
85                 goto func_exit;
86         }
87
88         atomic_dec(&gb_dai->users);
89
90         /* deactivate rx/tx */
91         cportid = gb_dai->connection->intf_cport_id;
92
93         switch (substream->stream) {
94         case SNDRV_PCM_STREAM_CAPTURE:
95                 ret = gb_audio_gb_deactivate_rx(gb->mgmt_connection, cportid);
96                 break;
97         case SNDRV_PCM_STREAM_PLAYBACK:
98                 ret = gb_audio_gb_deactivate_tx(gb->mgmt_connection, cportid);
99                 break;
100         default:
101                 dev_err(dai->dev, "Invalid stream type during shutdown\n");
102                 goto func_exit;
103         }
104
105         if (ret)
106                 dev_err(dai->dev, "%d:Error during deactivate\n", ret);
107
108         /* un register cport */
109         i2s_port = 0;   /* fixed for now */
110         ret = gb_audio_apbridgea_unregister_cport(gb_dai->connection, i2s_port,
111                                         gb_dai->connection->hd_cport_id);
112
113         dev_dbg(dai->dev, "Unregister %s:%d DAI, ret:%d\n", dai->name,
114                 gb_dai->connection->hd_cport_id, ret);
115 func_exit:
116         mutex_unlock(&gb->lock);
117
118         return;
119 }
120
121 static int gbcodec_hw_params(struct snd_pcm_substream *substream,
122                              struct snd_pcm_hw_params *hwparams,
123                              struct snd_soc_dai *dai)
124 {
125         int ret;
126         uint8_t sig_bits, channels;
127         uint32_t format, rate;
128         uint16_t data_cport;
129         struct gbaudio_dai *gb_dai;
130         struct gbaudio_codec_info *gb = dev_get_drvdata(dai->dev);
131
132         /* find the dai */
133         mutex_lock(&gb->lock);
134         gb_dai = gbaudio_find_dai(gb, -1, dai->name);
135         if (!gb_dai) {
136                 dev_err(dai->dev, "%s: DAI not registered\n", dai->name);
137                 ret = -EINVAL;
138                 goto func_exit;
139         }
140
141         /*
142          * assuming, currently only 48000 Hz, 16BIT_LE, stereo
143          * is supported, validate params before configuring codec
144          */
145         if (params_channels(hwparams) != 2) {
146                 dev_err(dai->dev, "Invalid channel count:%d\n",
147                         params_channels(hwparams));
148                 ret = -EINVAL;
149                 goto func_exit;
150         }
151         channels = params_channels(hwparams);
152
153         if (params_rate(hwparams) != 48000) {
154                 dev_err(dai->dev, "Invalid sampling rate:%d\n",
155                         params_rate(hwparams));
156                 ret = -EINVAL;
157                 goto func_exit;
158         }
159         rate = GB_AUDIO_PCM_RATE_48000;
160
161         if (params_format(hwparams) != SNDRV_PCM_FORMAT_S16_LE) {
162                 dev_err(dai->dev, "Invalid format:%d\n",
163                         params_format(hwparams));
164                 ret = -EINVAL;
165                 goto func_exit;
166         }
167         format = GB_AUDIO_PCM_FMT_S16_LE;
168
169         data_cport = gb_dai->connection->intf_cport_id;
170         /* XXX check impact of sig_bit
171          * it should not change ideally
172          */
173
174         dev_dbg(dai->dev, "cport:%d, rate:%d, channel %d, format %d, sig_bits:%d\n",
175                 data_cport, rate, channels, format, sig_bits);
176         ret = gb_audio_gb_set_pcm(gb->mgmt_connection, data_cport, format,
177                                   rate, channels, sig_bits);
178         if (ret) {
179                 dev_err(dai->dev, "%d: Error during set_pcm\n", ret);
180                 goto func_exit;
181         }
182
183         /*
184          * XXX need to check if
185          * set config is always required
186          * check for mclk_freq as well
187          */
188         ret = gb_audio_apbridgea_set_config(gb_dai->connection, 0,
189                                             AUDIO_APBRIDGEA_PCM_FMT_16,
190                                             AUDIO_APBRIDGEA_PCM_RATE_48000,
191                                             6144000);
192         if (ret)
193                 dev_err(dai->dev, "%d: Error during set_config\n", ret);
194 func_exit:
195         mutex_unlock(&gb->lock);
196         return ret;
197 }
198
199 static int gbcodec_prepare(struct snd_pcm_substream *substream,
200                            struct snd_soc_dai *dai)
201 {
202         int ret;
203         uint16_t data_cport;
204         struct gbaudio_dai *gb_dai;
205         struct gbaudio_codec_info *gb = dev_get_drvdata(dai->dev);
206
207         /* find the dai */
208         mutex_lock(&gb->lock);
209         gb_dai = gbaudio_find_dai(gb, -1, dai->name);
210         if (!gb_dai) {
211                 dev_err(dai->dev, "%s: DAI not registered\n", dai->name);
212                 ret = -EINVAL;
213                 goto func_exit;
214         }
215
216         /* deactivate rx/tx */
217         data_cport = gb_dai->connection->intf_cport_id;
218
219         switch (substream->stream) {
220         case SNDRV_PCM_STREAM_CAPTURE:
221                 ret = gb_audio_gb_set_rx_data_size(gb->mgmt_connection,
222                                                    data_cport, 192);
223                 if (ret) {
224                         dev_err(dai->dev,
225                                 "%d:Error during set_rx_data_size, cport:%d\n",
226                                 ret, data_cport);
227                         goto func_exit;
228                 }
229                 ret = gb_audio_apbridgea_set_rx_data_size(gb_dai->connection, 0,
230                                                           192);
231                 if (ret) {
232                         dev_err(dai->dev,
233                                 "%d:Error during apbridgea_set_rx_data_size\n",
234                                 ret);
235                         goto func_exit;
236                 }
237                 ret = gb_audio_gb_activate_rx(gb->mgmt_connection, data_cport);
238                 break;
239         case SNDRV_PCM_STREAM_PLAYBACK:
240                 ret = gb_audio_gb_set_tx_data_size(gb->mgmt_connection,
241                                                    data_cport, 192);
242                 if (ret) {
243                         dev_err(dai->dev,
244                                 "%d:Error during module set_tx_data_size, cport:%d\n",
245                                 ret, data_cport);
246                         goto func_exit;
247                 }
248                 ret = gb_audio_apbridgea_set_tx_data_size(gb_dai->connection, 0,
249                                                           192);
250                 if (ret) {
251                         dev_err(dai->dev,
252                                 "%d:Error during apbridgea set_tx_data_size, cport\n",
253                                 ret);
254                         goto func_exit;
255                 }
256                 ret = gb_audio_gb_activate_tx(gb->mgmt_connection, data_cport);
257                 break;
258         default:
259                 dev_err(dai->dev, "Invalid stream type %d during prepare\n",
260                         substream->stream);
261                 ret = -EINVAL;
262                 goto func_exit;
263         }
264
265         if (ret)
266                 dev_err(dai->dev, "%d: Error during activate stream\n", ret);
267
268 func_exit:
269         mutex_unlock(&gb->lock);
270         return ret;
271 }
272
273 static int gbcodec_trigger(struct snd_pcm_substream *substream, int cmd,
274                 struct snd_soc_dai *dai)
275 {
276         int ret;
277         int tx, rx, start, stop;
278         struct gbaudio_dai *gb_dai;
279         struct gbaudio_codec_info *gb = dev_get_drvdata(dai->dev);
280
281         /* find the dai */
282         mutex_lock(&gb->lock);
283         gb_dai = gbaudio_find_dai(gb, -1, dai->name);
284         if (!gb_dai) {
285                 dev_err(dai->dev, "%s: DAI not registered\n", dai->name);
286                 ret = -EINVAL;
287                 goto func_exit;
288         }
289
290         tx = rx = start = stop = 0;
291         switch (cmd) {
292         case SNDRV_PCM_TRIGGER_START:
293         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
294                 start = 1;
295                 break;
296         case SNDRV_PCM_TRIGGER_STOP:
297         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
298                 stop = 1;
299                 break;
300         default:
301                 dev_err(dai->dev, "Invalid tigger cmd:%d\n", cmd);
302                 ret = -EINVAL;
303                 goto func_exit;
304         }
305
306         switch (substream->stream) {
307         case SNDRV_PCM_STREAM_CAPTURE:
308                 rx = 1;
309                 break;
310         case SNDRV_PCM_STREAM_PLAYBACK:
311                 tx = 1;
312                 break;
313         default:
314                 dev_err(dai->dev, "Invalid stream type:%d\n",
315                         substream->stream);
316                 ret = -EINVAL;
317                 goto func_exit;
318         }
319
320         if (start && tx)
321                 ret = gb_audio_apbridgea_start_tx(gb_dai->connection, 0, 0);
322
323         else if (start && rx)
324                 ret = gb_audio_apbridgea_start_rx(gb_dai->connection, 0);
325
326         else if (stop && tx)
327                 ret = gb_audio_apbridgea_stop_tx(gb_dai->connection, 0);
328
329         else if (stop && rx)
330                 ret = gb_audio_apbridgea_stop_rx(gb_dai->connection, 0);
331
332         else
333                 ret = -EINVAL;
334
335         if (ret)
336                 dev_err(dai->dev, "%d:Error during %s stream\n", ret,
337                         start ? "Start" : "Stop");
338
339 func_exit:
340         mutex_unlock(&gb->lock);
341         return ret;
342 }
343
344 static int gbcodec_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
345 {
346         return 0;
347 }
348
349 static int gbcodec_digital_mute(struct snd_soc_dai *dai, int mute)
350 {
351         return 0;
352 }
353
354 static struct snd_soc_dai_ops gbcodec_dai_ops = {
355         .startup = gbcodec_startup,
356         .shutdown = gbcodec_shutdown,
357         .hw_params = gbcodec_hw_params,
358         .trigger = gbcodec_trigger,
359         .prepare = gbcodec_prepare,
360         .set_fmt = gbcodec_set_dai_fmt,
361         .digital_mute = gbcodec_digital_mute,
362 };
363
364 /*
365  * codec driver ops
366  */
367 static int gbcodec_probe(struct snd_soc_codec *codec)
368 {
369         /* Empty function for now */
370         return 0;
371 }
372
373 static int gbcodec_remove(struct snd_soc_codec *codec)
374 {
375         /* Empty function for now */
376         return 0;
377 }
378
379 static int gbcodec_write(struct snd_soc_codec *codec, unsigned int reg,
380                          unsigned int value)
381 {
382         int ret = 0;
383         struct gbaudio_codec_info *gbcodec = snd_soc_codec_get_drvdata(codec);
384         u8 *gbcodec_reg = gbcodec->reg;
385
386         if (reg == SND_SOC_NOPM)
387                 return 0;
388
389         if (reg >= GBCODEC_REG_COUNT)
390                 return 0;
391
392         gbcodec_reg[reg] = value;
393         dev_dbg(codec->dev, "reg[%d] = 0x%x\n", reg, value);
394
395         return ret;
396 }
397
398 static unsigned int gbcodec_read(struct snd_soc_codec *codec,
399                                  unsigned int reg)
400 {
401         unsigned int val = 0;
402
403         struct gbaudio_codec_info *gbcodec = snd_soc_codec_get_drvdata(codec);
404         u8 *gbcodec_reg = gbcodec->reg;
405
406         if (reg == SND_SOC_NOPM)
407                 return 0;
408
409         if (reg >= GBCODEC_REG_COUNT)
410                 return 0;
411
412         val = gbcodec_reg[reg];
413         dev_dbg(codec->dev, "reg[%d] = 0x%x\n", reg, val);
414
415         return val;
416 }
417
418 static struct snd_soc_codec_driver soc_codec_dev_gbcodec = {
419         .probe = gbcodec_probe,
420         .remove = gbcodec_remove,
421
422         .read = gbcodec_read,
423         .write = gbcodec_write,
424
425         .reg_cache_size = GBCODEC_REG_COUNT,
426         .reg_cache_default = gbcodec_reg_defaults,
427         .reg_word_size = 1,
428
429         .idle_bias_off = true,
430         .ignore_pmdown_time = 1,
431 };
432
433 /*
434  * GB codec DAI link related
435  */
436 static struct snd_soc_dai_link gbaudio_dailink = {
437         .name = "PRI_MI2S_RX",
438         .stream_name = "Primary MI2S Playback",
439         .platform_name = "msm-pcm-routing",
440         .cpu_dai_name = "msm-dai-q6-mi2s.0",
441         .no_pcm = 1,
442         .be_id = 34,
443 };
444
445 static int gbaudio_add_dailinks(struct gbaudio_codec_info *gbcodec)
446 {
447         int ret, i;
448         char *dai_link_name;
449         struct snd_soc_dai_link *dailink;
450         struct device *dev = gbcodec->dev;
451
452         dailink = &gbaudio_dailink;
453         dailink->codec_name = gbcodec->name;
454
455         /* FIXME
456          * allocate memory for DAI links based on count.
457          * currently num_dai_links=1, so using static struct
458          */
459         gbcodec->num_dai_links = 1;
460
461         for (i = 0; i < gbcodec->num_dai_links; i++) {
462                 gbcodec->dailink_name[i] = dai_link_name =
463                         devm_kzalloc(dev, NAME_SIZE, GFP_KERNEL);
464                 snprintf(dai_link_name, NAME_SIZE, "GB %d.%d PRI_MI2S_RX",
465                          gbcodec->dev_id, i);
466                 dailink->name = dai_link_name;
467                 dailink->codec_dai_name = gbcodec->dais[i].name;
468         }
469
470         ret = msm8994_add_dailink("msm8994-tomtom-mtp-snd-card", dailink, 1);
471         if (ret)
472                 dev_err(dev, "%d:Error while adding DAI link\n", ret);
473
474         return ret;
475 }
476
477 /*
478  * gb_snd management functions
479  */
480
481 /* XXX
482  * since BE DAI path is not yet properly closed from above layer,
483  * dsp dai.mi2s_dai_data.status_mask is still set to STATUS_PORT_STARTED
484  * this causes immediate playback/capture to fail in case relevant mixer
485  * control is not turned OFF
486  * user need to try once again after failure to recover DSP state.
487  */
488 static void gb_audio_cleanup(struct gbaudio_codec_info *gb)
489 {
490         int cportid, ret;
491         struct gbaudio_dai *gb_dai;
492         struct gb_connection *connection;
493         struct device *dev = gb->dev;
494
495         list_for_each_entry(gb_dai, &gb->dai_list, list) {
496                 /*
497                  * In case of BE dailink, need to deactivate APBridge
498                  * manually
499                  */
500                 if (atomic_read(&gb_dai->users)) {
501                         connection = gb_dai->connection;
502                         /* PB active */
503                         ret = gb_audio_apbridgea_stop_tx(connection, 0);
504                         if (ret)
505                                 dev_info(dev, "%d:Failed during APBridge stop_tx\n",
506                                          ret);
507                         cportid = connection->intf_cport_id;
508                         ret = gb_audio_gb_deactivate_tx(gb->mgmt_connection,
509                                                         cportid);
510                         if (ret)
511                                 dev_info(dev,
512                                          "%d:Failed during deactivate_tx\n",
513                                          ret);
514                         cportid = connection->hd_cport_id;
515                         ret = gb_audio_apbridgea_unregister_cport(connection, 0,
516                                                                   cportid);
517                         if (ret)
518                                 dev_info(dev, "%d:Failed during unregister cport\n",
519                                          ret);
520                         atomic_dec(&gb_dai->users);
521                 }
522         }
523 }
524
525 static int gbaudio_register_codec(struct gbaudio_codec_info *gbcodec)
526 {
527         int ret, i;
528         struct device *dev = gbcodec->dev;
529         struct gb_connection *connection = gbcodec->mgmt_connection;
530         /*
531          * FIXME: malloc for topology happens via audio_gb driver
532          * should be done within codec driver itself
533          */
534         struct gb_audio_topology *topology;
535
536         ret = gb_connection_enable(connection);
537         if (ret) {
538                 dev_err(dev, "%d: Error while enabling mgmt connection\n", ret);
539                 return ret;
540         }
541
542         gbcodec->dev_id = connection->intf->interface_id;
543         /* fetch topology data */
544         ret = gb_audio_gb_get_topology(connection, &topology);
545         if (ret) {
546                 dev_err(dev, "%d:Error while fetching topology\n", ret);
547                 goto tplg_fetch_err;
548         }
549
550         /* process topology data */
551         ret = gbaudio_tplg_parse_data(gbcodec, topology);
552         if (ret) {
553                 dev_err(dev, "%d:Error while parsing topology data\n",
554                           ret);
555                 goto tplg_parse_err;
556         }
557         gbcodec->topology = topology;
558
559         /* update codec info */
560         soc_codec_dev_gbcodec.controls = gbcodec->kctls;
561         soc_codec_dev_gbcodec.num_controls = gbcodec->num_kcontrols;
562         soc_codec_dev_gbcodec.dapm_widgets = gbcodec->widgets;
563         soc_codec_dev_gbcodec.num_dapm_widgets = gbcodec->num_dapm_widgets;
564         soc_codec_dev_gbcodec.dapm_routes = gbcodec->routes;
565         soc_codec_dev_gbcodec.num_dapm_routes = gbcodec->num_dapm_routes;
566
567         /* update DAI info */
568         for (i = 0; i < gbcodec->num_dais; i++)
569                 gbcodec->dais[i].ops = &gbcodec_dai_ops;
570
571         /* register codec */
572         ret = snd_soc_register_codec(dev, &soc_codec_dev_gbcodec,
573                                      gbcodec->dais, 1);
574         if (ret) {
575                 dev_err(dev, "%d:Failed to register codec\n", ret);
576                 goto codec_reg_err;
577         }
578
579         /* update DAI links in response to this codec */
580         ret = gbaudio_add_dailinks(gbcodec);
581         if (ret) {
582                 dev_err(dev, "%d: Failed to add DAI links\n", ret);
583                 goto add_dailink_err;
584         }
585
586         return 0;
587
588 add_dailink_err:
589         snd_soc_unregister_codec(dev);
590 codec_reg_err:
591         gbaudio_tplg_release(gbcodec);
592         gbcodec->topology = NULL;
593 tplg_parse_err:
594         kfree(topology);
595 tplg_fetch_err:
596         gb_connection_disable(gbcodec->mgmt_connection);
597         return ret;
598 }
599
600 static void gbaudio_unregister_codec(struct gbaudio_codec_info *gbcodec)
601 {
602         gb_audio_cleanup(gbcodec);
603         msm8994_remove_dailink("msm8994-tomtom-mtp-snd-card", &gbaudio_dailink,
604                                1);
605         snd_soc_unregister_codec(gbcodec->dev);
606         gbaudio_tplg_release(gbcodec);
607         kfree(gbcodec->topology);
608         gb_connection_disable(gbcodec->mgmt_connection);
609 }
610
611 static int gbaudio_codec_request_handler(struct gb_operation *op)
612 {
613         struct gb_connection *connection = op->connection;
614         struct gb_audio_streaming_event_request *req = op->request->payload;
615
616         dev_warn(&connection->bundle->dev,
617                  "Audio Event received: cport: %u, event: %u\n",
618                  req->data_cport, req->event);
619
620         return 0;
621 }
622
623 static int gbaudio_dai_request_handler(struct gb_operation *op)
624 {
625         struct gb_connection *connection = op->connection;
626
627         dev_warn(&connection->bundle->dev, "Audio Event received\n");
628
629         return 0;
630 }
631
632 static int gb_audio_add_mgmt_connection(struct gbaudio_codec_info *gbcodec,
633                                 struct greybus_descriptor_cport *cport_desc,
634                                 struct gb_bundle *bundle)
635 {
636         struct gb_connection *connection;
637
638         /* Management Cport */
639         if (gbcodec->mgmt_connection) {
640                 dev_err(&bundle->dev,
641                         "Can't have multiple Management connections\n");
642                 return -ENODEV;
643         }
644
645         connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
646                                           gbaudio_codec_request_handler);
647         if (IS_ERR(connection))
648                 return PTR_ERR(connection);
649
650         connection->private = gbcodec;
651         gbcodec->mgmt_connection = connection;
652
653         return 0;
654 }
655
656 static int gb_audio_add_data_connection(struct gbaudio_codec_info *gbcodec,
657                                 struct greybus_descriptor_cport *cport_desc,
658                                 struct gb_bundle *bundle)
659 {
660         struct gb_connection *connection;
661         struct gbaudio_dai *dai;
662
663         dai = devm_kzalloc(gbcodec->dev, sizeof(*dai), GFP_KERNEL);
664         if (!dai) {
665                 dev_err(gbcodec->dev, "DAI Malloc failure\n");
666                 return -ENOMEM;
667         }
668
669         connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
670                                           gbaudio_dai_request_handler);
671         if (IS_ERR(connection)) {
672                 devm_kfree(gbcodec->dev, dai);
673                 return PTR_ERR(connection);
674         }
675
676         connection->private = gbcodec;
677         atomic_set(&dai->users, 0);
678         dai->data_cport = connection->intf_cport_id;
679         dai->connection = connection;
680         list_add(&dai->list, &gbcodec->dai_list);
681
682         return 0;
683 }
684 /*
685  * This is the basic hook get things initialized and registered w/ gb
686  */
687
688 static int gb_audio_probe(struct gb_bundle *bundle,
689                           const struct greybus_bundle_id *id)
690 {
691         struct device *dev = &bundle->dev;
692         struct gbaudio_codec_info *gbcodec;
693         struct greybus_descriptor_cport *cport_desc;
694         struct gb_audio_manager_module_descriptor desc;
695         struct gbaudio_dai *dai, *_dai;
696         int ret, i;
697
698         /* There should be at least one Management and one Data cport */
699         if (bundle->num_cports < 2)
700                 return -ENODEV;
701
702         mutex_lock(&gb_codec_list_lock);
703         /*
704          * There can be only one Management connection and any number of data
705          * connections.
706          */
707         gbcodec = devm_kzalloc(dev, sizeof(*gbcodec), GFP_KERNEL);
708         if (!gbcodec) {
709                 mutex_unlock(&gb_codec_list_lock);
710                 return -ENOMEM;
711         }
712
713         gbcodec->num_data_connections = bundle->num_cports - 1;
714         mutex_init(&gbcodec->lock);
715         INIT_LIST_HEAD(&gbcodec->dai_list);
716         INIT_LIST_HEAD(&gbcodec->widget_list);
717         INIT_LIST_HEAD(&gbcodec->codec_ctl_list);
718         INIT_LIST_HEAD(&gbcodec->widget_ctl_list);
719         gbcodec->dev = dev;
720         snprintf(gbcodec->name, NAME_SIZE, "%s.%s", dev->driver->name,
721                  dev_name(dev));
722         greybus_set_drvdata(bundle, gbcodec);
723
724         /* Create all connections */
725         for (i = 0; i < bundle->num_cports; i++) {
726                 cport_desc = &bundle->cport_desc[i];
727
728                 switch (cport_desc->protocol_id) {
729                 case GREYBUS_PROTOCOL_AUDIO_MGMT:
730                         ret = gb_audio_add_mgmt_connection(gbcodec, cport_desc,
731                                                            bundle);
732                         if (ret)
733                                 goto destroy_connections;
734                         break;
735                 case GREYBUS_PROTOCOL_AUDIO_DATA:
736                         ret = gb_audio_add_data_connection(gbcodec, cport_desc,
737                                                            bundle);
738                         if (ret)
739                                 goto destroy_connections;
740                         break;
741                 default:
742                         dev_err(dev, "Unsupported protocol: 0x%02x\n",
743                                 cport_desc->protocol_id);
744                         ret = -ENODEV;
745                         goto destroy_connections;
746                 }
747         }
748
749         /* There must be a management cport */
750         if (!gbcodec->mgmt_connection) {
751                 ret = -EINVAL;
752                 dev_err(dev, "Missing management connection\n");
753                 goto destroy_connections;
754         }
755
756         /* Initialize management connection */
757         ret = gbaudio_register_codec(gbcodec);
758         if (ret)
759                 goto destroy_connections;
760
761         /* Initialize data connections */
762         list_for_each_entry(dai, &gbcodec->dai_list, list) {
763                 ret = gb_connection_enable(dai->connection);
764                 if (ret)
765                         goto remove_dai;
766         }
767
768         /* inform above layer for uevent */
769         dev_dbg(dev, "Inform set_event:%d to above layer\n", 1);
770         /* prepare for the audio manager */
771         strlcpy(desc.name, gbcodec->name, GB_AUDIO_MANAGER_MODULE_NAME_LEN);
772         desc.slot = 1; /* todo */
773         desc.vid = 2; /* todo */
774         desc.pid = 3; /* todo */
775         desc.cport = gbcodec->dev_id;
776         desc.devices = 0x2; /* todo */
777         gbcodec->manager_id = gb_audio_manager_add(&desc);
778
779         list_add(&gbcodec->list, &gb_codec_list);
780         dev_dbg(dev, "Add GB Audio device:%s\n", gbcodec->name);
781         mutex_unlock(&gb_codec_list_lock);
782
783         return 0;
784
785 remove_dai:
786         list_for_each_entry_safe(dai, _dai, &gbcodec->dai_list, list)
787                 gb_connection_disable(dai->connection);
788
789         gbaudio_unregister_codec(gbcodec);
790 destroy_connections:
791         list_for_each_entry_safe(dai, _dai, &gbcodec->dai_list, list) {
792                 gb_connection_destroy(dai->connection);
793                 list_del(&dai->list);
794                 devm_kfree(dev, dai);
795         }
796
797         if (gbcodec->mgmt_connection)
798                 gb_connection_destroy(gbcodec->mgmt_connection);
799
800         devm_kfree(dev, gbcodec);
801         mutex_unlock(&gb_codec_list_lock);
802
803         return ret;
804 }
805
806 static void gb_audio_disconnect(struct gb_bundle *bundle)
807 {
808         struct gbaudio_codec_info *gbcodec = greybus_get_drvdata(bundle);
809         struct gbaudio_dai *dai, *_dai;
810
811         mutex_lock(&gb_codec_list_lock);
812         list_del(&gbcodec->list);
813         /* inform uevent to above layers */
814         gb_audio_manager_remove(gbcodec->manager_id);
815
816         mutex_lock(&gbcodec->lock);
817         list_for_each_entry_safe(dai, _dai, &gbcodec->dai_list, list)
818                 gb_connection_disable(dai->connection);
819         gbaudio_unregister_codec(gbcodec);
820
821         list_for_each_entry_safe(dai, _dai, &gbcodec->dai_list, list) {
822                 gb_connection_destroy(dai->connection);
823                 list_del(&dai->list);
824                 devm_kfree(gbcodec->dev, dai);
825         }
826         gb_connection_destroy(gbcodec->mgmt_connection);
827         gbcodec->mgmt_connection = NULL;
828         mutex_unlock(&gbcodec->lock);
829
830         devm_kfree(&bundle->dev, gbcodec);
831         mutex_unlock(&gb_codec_list_lock);
832 }
833
834 static const struct greybus_bundle_id gb_audio_id_table[] = {
835         { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_AUDIO) },
836         { }
837 };
838 MODULE_DEVICE_TABLE(greybus, gb_audio_id_table);
839
840 static struct greybus_driver gb_audio_driver = {
841         .name           = "gb-audio",
842         .probe          = gb_audio_probe,
843         .disconnect     = gb_audio_disconnect,
844         .id_table       = gb_audio_id_table,
845 };
846 module_greybus_driver(gb_audio_driver);
847
848 MODULE_DESCRIPTION("Greybus Audio codec driver");
849 MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@linaro.org>");
850 MODULE_LICENSE("GPL v2");
851 MODULE_ALIAS("platform:gbaudio-codec");