3 * Copyright 2015 Google Inc.
4 * Copyright 2015 Linaro Ltd.
6 * Released under the GPLv2 only.
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <sound/soc.h>
11 #include <sound/pcm_params.h>
13 #include "audio_codec.h"
14 #include "audio_apbridgea.h"
15 #include "audio_manager.h"
18 * gb_snd management functions
21 static int gbaudio_request_jack(struct gbaudio_module_info *module,
22 struct gb_audio_jack_event_request *req)
25 struct snd_jack *jack = module->headset_jack.jack;
26 struct snd_jack *btn_jack = module->button_jack.jack;
29 dev_err_ratelimited(module->dev,
30 "Invalid jack event received:type: %u, event: %u\n",
31 req->jack_attribute, req->event);
35 dev_warn_ratelimited(module->dev,
36 "Jack Event received: type: %u, event: %u\n",
37 req->jack_attribute, req->event);
39 if (req->event == GB_AUDIO_JACK_EVENT_REMOVAL) {
40 module->jack_type = 0;
41 if (btn_jack && module->button_status) {
42 snd_soc_jack_report(&module->button_jack, 0,
44 module->button_status = 0;
46 snd_soc_jack_report(&module->headset_jack, 0,
51 report = req->jack_attribute & module->jack_mask;
53 dev_err_ratelimited(module->dev,
54 "Invalid jack event received:type: %u, event: %u\n",
55 req->jack_attribute, req->event);
59 if (module->jack_type)
60 dev_warn_ratelimited(module->dev,
61 "Modifying jack from %d to %d\n",
62 module->jack_type, report);
64 module->jack_type = report;
65 snd_soc_jack_report(&module->headset_jack, report, module->jack_mask);
70 static int gbaudio_request_button(struct gbaudio_module_info *module,
71 struct gb_audio_button_event_request *req)
73 int soc_button_id, report;
74 struct snd_jack *btn_jack = module->button_jack.jack;
77 dev_err_ratelimited(module->dev,
78 "Invalid button event received:type: %u, event: %u\n",
79 req->button_id, req->event);
83 dev_warn_ratelimited(module->dev,
84 "Button Event received: id: %u, event: %u\n",
85 req->button_id, req->event);
87 /* currently supports 4 buttons only */
88 if (!module->jack_type) {
89 dev_err_ratelimited(module->dev,
90 "Jack not present. Bogus event!!\n");
94 report = module->button_status & module->button_mask;
97 switch (req->button_id) {
99 soc_button_id = SND_JACK_BTN_0 & module->button_mask;
103 soc_button_id = SND_JACK_BTN_1 & module->button_mask;
107 soc_button_id = SND_JACK_BTN_2 & module->button_mask;
111 soc_button_id = SND_JACK_BTN_3 & module->button_mask;
115 if (!soc_button_id) {
116 dev_err_ratelimited(module->dev,
117 "Invalid button request received\n");
121 if (req->event == GB_AUDIO_BUTTON_EVENT_PRESS)
122 report = report | soc_button_id;
124 report = report & ~soc_button_id;
126 module->button_status = report;
128 snd_soc_jack_report(&module->button_jack, report, module->button_mask);
133 static int gbaudio_request_stream(struct gbaudio_module_info *module,
134 struct gb_audio_streaming_event_request *req)
136 dev_warn(module->dev, "Audio Event received: cport: %u, event: %u\n",
137 req->data_cport, req->event);
142 static int gbaudio_codec_request_handler(struct gb_operation *op)
144 struct gb_connection *connection = op->connection;
145 struct gbaudio_module_info *module =
146 greybus_get_drvdata(connection->bundle);
147 struct gb_operation_msg_hdr *header = op->request->header;
148 struct gb_audio_streaming_event_request *stream_req;
149 struct gb_audio_jack_event_request *jack_req;
150 struct gb_audio_button_event_request *button_req;
153 switch (header->type) {
154 case GB_AUDIO_TYPE_STREAMING_EVENT:
155 stream_req = op->request->payload;
156 ret = gbaudio_request_stream(module, stream_req);
159 case GB_AUDIO_TYPE_JACK_EVENT:
160 jack_req = op->request->payload;
161 ret = gbaudio_request_jack(module, jack_req);
164 case GB_AUDIO_TYPE_BUTTON_EVENT:
165 button_req = op->request->payload;
166 ret = gbaudio_request_button(module, button_req);
170 dev_err_ratelimited(&connection->bundle->dev,
171 "Invalid Audio Event received\n");
178 static int gb_audio_add_mgmt_connection(struct gbaudio_module_info *gbmodule,
179 struct greybus_descriptor_cport *cport_desc,
180 struct gb_bundle *bundle)
182 struct gb_connection *connection;
184 /* Management Cport */
185 if (gbmodule->mgmt_connection) {
186 dev_err(&bundle->dev,
187 "Can't have multiple Management connections\n");
191 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
192 gbaudio_codec_request_handler);
193 if (IS_ERR(connection))
194 return PTR_ERR(connection);
196 greybus_set_drvdata(bundle, gbmodule);
197 gbmodule->mgmt_connection = connection;
202 static int gb_audio_add_data_connection(struct gbaudio_module_info *gbmodule,
203 struct greybus_descriptor_cport *cport_desc,
204 struct gb_bundle *bundle)
206 struct gb_connection *connection;
207 struct gbaudio_data_connection *dai;
209 dai = devm_kzalloc(gbmodule->dev, sizeof(*dai), GFP_KERNEL);
211 dev_err(gbmodule->dev, "DAI Malloc failure\n");
215 connection = gb_connection_create_offloaded(bundle,
216 le16_to_cpu(cport_desc->id),
217 GB_CONNECTION_FLAG_CSD);
218 if (IS_ERR(connection)) {
219 devm_kfree(gbmodule->dev, dai);
220 return PTR_ERR(connection);
223 greybus_set_drvdata(bundle, gbmodule);
225 dai->data_cport = connection->intf_cport_id;
226 dai->connection = connection;
227 list_add(&dai->list, &gbmodule->data_list);
233 * This is the basic hook get things initialized and registered w/ gb
236 static int gb_audio_probe(struct gb_bundle *bundle,
237 const struct greybus_bundle_id *id)
239 struct device *dev = &bundle->dev;
240 struct gbaudio_module_info *gbmodule;
241 struct greybus_descriptor_cport *cport_desc;
242 struct gb_audio_manager_module_descriptor desc;
243 struct gbaudio_data_connection *dai, *_dai;
245 struct gb_audio_topology *topology;
247 /* There should be at least one Management and one Data cport */
248 if (bundle->num_cports < 2)
252 * There can be only one Management connection and any number of data
255 gbmodule = devm_kzalloc(dev, sizeof(*gbmodule), GFP_KERNEL);
259 gbmodule->num_data_connections = bundle->num_cports - 1;
260 INIT_LIST_HEAD(&gbmodule->data_list);
261 INIT_LIST_HEAD(&gbmodule->widget_list);
262 INIT_LIST_HEAD(&gbmodule->ctl_list);
263 INIT_LIST_HEAD(&gbmodule->widget_ctl_list);
265 snprintf(gbmodule->name, NAME_SIZE, "%s.%s", dev->driver->name,
267 greybus_set_drvdata(bundle, gbmodule);
269 /* Create all connections */
270 for (i = 0; i < bundle->num_cports; i++) {
271 cport_desc = &bundle->cport_desc[i];
273 switch (cport_desc->protocol_id) {
274 case GREYBUS_PROTOCOL_AUDIO_MGMT:
275 ret = gb_audio_add_mgmt_connection(gbmodule, cport_desc,
278 goto destroy_connections;
280 case GREYBUS_PROTOCOL_AUDIO_DATA:
281 ret = gb_audio_add_data_connection(gbmodule, cport_desc,
284 goto destroy_connections;
287 dev_err(dev, "Unsupported protocol: 0x%02x\n",
288 cport_desc->protocol_id);
290 goto destroy_connections;
294 /* There must be a management cport */
295 if (!gbmodule->mgmt_connection) {
297 dev_err(dev, "Missing management connection\n");
298 goto destroy_connections;
301 /* Initialize management connection */
302 ret = gb_connection_enable(gbmodule->mgmt_connection);
304 dev_err(dev, "%d: Error while enabling mgmt connection\n", ret);
305 goto destroy_connections;
307 gbmodule->dev_id = gbmodule->mgmt_connection->intf->interface_id;
310 * FIXME: malloc for topology happens via audio_gb driver
311 * should be done within codec driver itself
313 ret = gb_audio_gb_get_topology(gbmodule->mgmt_connection, &topology);
315 dev_err(dev, "%d:Error while fetching topology\n", ret);
316 goto disable_connection;
319 /* process topology data */
320 ret = gbaudio_tplg_parse_data(gbmodule, topology);
322 dev_err(dev, "%d:Error while parsing topology data\n",
326 gbmodule->topology = topology;
328 /* Initialize data connections */
329 list_for_each_entry(dai, &gbmodule->data_list, list) {
330 ret = gb_connection_enable(dai->connection);
333 "%d:Error while enabling %d:data connection\n",
334 ret, dai->data_cport);
335 goto disable_data_connection;
339 /* register module with gbcodec */
340 ret = gbaudio_register_module(gbmodule);
342 goto disable_data_connection;
344 /* inform above layer for uevent */
345 dev_dbg(dev, "Inform set_event:%d to above layer\n", 1);
346 /* prepare for the audio manager */
347 strlcpy(desc.name, gbmodule->name, GB_AUDIO_MANAGER_MODULE_NAME_LEN);
348 desc.slot = 1; /* todo */
349 desc.vid = 2; /* todo */
350 desc.pid = 3; /* todo */
351 desc.cport = gbmodule->dev_id;
352 desc.op_devices = gbmodule->op_devices;
353 desc.ip_devices = gbmodule->ip_devices;
354 gbmodule->manager_id = gb_audio_manager_add(&desc);
356 dev_dbg(dev, "Add GB Audio device:%s\n", gbmodule->name);
358 gb_pm_runtime_put_autosuspend(bundle);
362 disable_data_connection:
363 list_for_each_entry_safe(dai, _dai, &gbmodule->data_list, list)
364 gb_connection_disable(dai->connection);
365 gbaudio_tplg_release(gbmodule);
366 gbmodule->topology = NULL;
372 gb_connection_disable(gbmodule->mgmt_connection);
375 list_for_each_entry_safe(dai, _dai, &gbmodule->data_list, list) {
376 gb_connection_destroy(dai->connection);
377 list_del(&dai->list);
378 devm_kfree(dev, dai);
381 if (gbmodule->mgmt_connection)
382 gb_connection_destroy(gbmodule->mgmt_connection);
384 devm_kfree(dev, gbmodule);
389 static void gb_audio_disconnect(struct gb_bundle *bundle)
391 struct gbaudio_module_info *gbmodule = greybus_get_drvdata(bundle);
392 struct gbaudio_data_connection *dai, *_dai;
394 gb_pm_runtime_get_sync(bundle);
396 /* cleanup module related resources first */
397 gbaudio_unregister_module(gbmodule);
399 /* inform uevent to above layers */
400 gb_audio_manager_remove(gbmodule->manager_id);
402 gbaudio_tplg_release(gbmodule);
403 kfree(gbmodule->topology);
404 gbmodule->topology = NULL;
405 gb_connection_disable(gbmodule->mgmt_connection);
406 list_for_each_entry_safe(dai, _dai, &gbmodule->data_list, list) {
407 gb_connection_disable(dai->connection);
408 gb_connection_destroy(dai->connection);
409 list_del(&dai->list);
410 devm_kfree(gbmodule->dev, dai);
412 gb_connection_destroy(gbmodule->mgmt_connection);
413 gbmodule->mgmt_connection = NULL;
415 devm_kfree(&bundle->dev, gbmodule);
418 static const struct greybus_bundle_id gb_audio_id_table[] = {
419 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_AUDIO) },
422 MODULE_DEVICE_TABLE(greybus, gb_audio_id_table);
425 static int gb_audio_suspend(struct device *dev)
427 struct gb_bundle *bundle = to_gb_bundle(dev);
428 struct gbaudio_module_info *gbmodule = greybus_get_drvdata(bundle);
429 struct gbaudio_data_connection *dai;
431 list_for_each_entry(dai, &gbmodule->data_list, list)
432 gb_connection_disable(dai->connection);
434 gb_connection_disable(gbmodule->mgmt_connection);
439 static int gb_audio_resume(struct device *dev)
441 struct gb_bundle *bundle = to_gb_bundle(dev);
442 struct gbaudio_module_info *gbmodule = greybus_get_drvdata(bundle);
443 struct gbaudio_data_connection *dai;
446 ret = gb_connection_enable(gbmodule->mgmt_connection);
448 dev_err(dev, "%d:Error while enabling mgmt connection\n", ret);
452 list_for_each_entry(dai, &gbmodule->data_list, list) {
453 ret = gb_connection_enable(dai->connection);
456 "%d:Error while enabling %d:data connection\n",
457 ret, dai->data_cport);
466 static const struct dev_pm_ops gb_audio_pm_ops = {
467 SET_RUNTIME_PM_OPS(gb_audio_suspend, gb_audio_resume, NULL)
470 static struct greybus_driver gb_audio_driver = {
472 .probe = gb_audio_probe,
473 .disconnect = gb_audio_disconnect,
474 .id_table = gb_audio_id_table,
475 .driver.pm = &gb_audio_pm_ops,
477 module_greybus_driver(gb_audio_driver);
479 MODULE_DESCRIPTION("Greybus Audio module driver");
480 MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@linaro.org>");
481 MODULE_LICENSE("GPL v2");
482 MODULE_ALIAS("platform:gbaudio-module");