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 le16_to_cpu(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);
213 connection = gb_connection_create_offloaded(bundle,
214 le16_to_cpu(cport_desc->id),
215 GB_CONNECTION_FLAG_CSD);
216 if (IS_ERR(connection)) {
217 devm_kfree(gbmodule->dev, dai);
218 return PTR_ERR(connection);
221 greybus_set_drvdata(bundle, gbmodule);
223 dai->data_cport = connection->intf_cport_id;
224 dai->connection = connection;
225 list_add(&dai->list, &gbmodule->data_list);
231 * This is the basic hook get things initialized and registered w/ gb
234 static int gb_audio_probe(struct gb_bundle *bundle,
235 const struct greybus_bundle_id *id)
237 struct device *dev = &bundle->dev;
238 struct gbaudio_module_info *gbmodule;
239 struct greybus_descriptor_cport *cport_desc;
240 struct gb_audio_manager_module_descriptor desc;
241 struct gbaudio_data_connection *dai, *_dai;
243 struct gb_audio_topology *topology;
245 /* There should be at least one Management and one Data cport */
246 if (bundle->num_cports < 2)
250 * There can be only one Management connection and any number of data
253 gbmodule = devm_kzalloc(dev, sizeof(*gbmodule), GFP_KERNEL);
257 gbmodule->num_data_connections = bundle->num_cports - 1;
258 INIT_LIST_HEAD(&gbmodule->data_list);
259 INIT_LIST_HEAD(&gbmodule->widget_list);
260 INIT_LIST_HEAD(&gbmodule->ctl_list);
261 INIT_LIST_HEAD(&gbmodule->widget_ctl_list);
263 snprintf(gbmodule->name, NAME_SIZE, "%s.%s", dev->driver->name,
265 greybus_set_drvdata(bundle, gbmodule);
267 /* Create all connections */
268 for (i = 0; i < bundle->num_cports; i++) {
269 cport_desc = &bundle->cport_desc[i];
271 switch (cport_desc->protocol_id) {
272 case GREYBUS_PROTOCOL_AUDIO_MGMT:
273 ret = gb_audio_add_mgmt_connection(gbmodule, cport_desc,
276 goto destroy_connections;
278 case GREYBUS_PROTOCOL_AUDIO_DATA:
279 ret = gb_audio_add_data_connection(gbmodule, cport_desc,
282 goto destroy_connections;
285 dev_err(dev, "Unsupported protocol: 0x%02x\n",
286 cport_desc->protocol_id);
288 goto destroy_connections;
292 /* There must be a management cport */
293 if (!gbmodule->mgmt_connection) {
295 dev_err(dev, "Missing management connection\n");
296 goto destroy_connections;
299 /* Initialize management connection */
300 ret = gb_connection_enable(gbmodule->mgmt_connection);
302 dev_err(dev, "%d: Error while enabling mgmt connection\n", ret);
303 goto destroy_connections;
305 gbmodule->dev_id = gbmodule->mgmt_connection->intf->interface_id;
308 * FIXME: malloc for topology happens via audio_gb driver
309 * should be done within codec driver itself
311 ret = gb_audio_gb_get_topology(gbmodule->mgmt_connection, &topology);
313 dev_err(dev, "%d:Error while fetching topology\n", ret);
314 goto disable_connection;
317 /* process topology data */
318 ret = gbaudio_tplg_parse_data(gbmodule, topology);
320 dev_err(dev, "%d:Error while parsing topology data\n",
324 gbmodule->topology = topology;
326 /* Initialize data connections */
327 list_for_each_entry(dai, &gbmodule->data_list, list) {
328 ret = gb_connection_enable(dai->connection);
331 "%d:Error while enabling %d:data connection\n",
332 ret, dai->data_cport);
333 goto disable_data_connection;
337 /* register module with gbcodec */
338 ret = gbaudio_register_module(gbmodule);
340 goto disable_data_connection;
342 /* inform above layer for uevent */
343 dev_dbg(dev, "Inform set_event:%d to above layer\n", 1);
344 /* prepare for the audio manager */
345 strlcpy(desc.name, gbmodule->name, GB_AUDIO_MANAGER_MODULE_NAME_LEN);
346 desc.vid = 2; /* todo */
347 desc.pid = 3; /* todo */
348 desc.intf_id = gbmodule->dev_id;
349 desc.op_devices = gbmodule->op_devices;
350 desc.ip_devices = gbmodule->ip_devices;
351 gbmodule->manager_id = gb_audio_manager_add(&desc);
353 dev_dbg(dev, "Add GB Audio device:%s\n", gbmodule->name);
355 gb_pm_runtime_put_autosuspend(bundle);
359 disable_data_connection:
360 list_for_each_entry_safe(dai, _dai, &gbmodule->data_list, list)
361 gb_connection_disable(dai->connection);
362 gbaudio_tplg_release(gbmodule);
363 gbmodule->topology = NULL;
369 gb_connection_disable(gbmodule->mgmt_connection);
372 list_for_each_entry_safe(dai, _dai, &gbmodule->data_list, list) {
373 gb_connection_destroy(dai->connection);
374 list_del(&dai->list);
375 devm_kfree(dev, dai);
378 if (gbmodule->mgmt_connection)
379 gb_connection_destroy(gbmodule->mgmt_connection);
381 devm_kfree(dev, gbmodule);
386 static void gb_audio_disconnect(struct gb_bundle *bundle)
388 struct gbaudio_module_info *gbmodule = greybus_get_drvdata(bundle);
389 struct gbaudio_data_connection *dai, *_dai;
391 gb_pm_runtime_get_sync(bundle);
393 /* cleanup module related resources first */
394 gbaudio_unregister_module(gbmodule);
396 /* inform uevent to above layers */
397 gb_audio_manager_remove(gbmodule->manager_id);
399 gbaudio_tplg_release(gbmodule);
400 kfree(gbmodule->topology);
401 gbmodule->topology = NULL;
402 gb_connection_disable(gbmodule->mgmt_connection);
403 list_for_each_entry_safe(dai, _dai, &gbmodule->data_list, list) {
404 gb_connection_disable(dai->connection);
405 gb_connection_destroy(dai->connection);
406 list_del(&dai->list);
407 devm_kfree(gbmodule->dev, dai);
409 gb_connection_destroy(gbmodule->mgmt_connection);
410 gbmodule->mgmt_connection = NULL;
412 devm_kfree(&bundle->dev, gbmodule);
415 static const struct greybus_bundle_id gb_audio_id_table[] = {
416 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_AUDIO) },
419 MODULE_DEVICE_TABLE(greybus, gb_audio_id_table);
422 static int gb_audio_suspend(struct device *dev)
424 struct gb_bundle *bundle = to_gb_bundle(dev);
425 struct gbaudio_module_info *gbmodule = greybus_get_drvdata(bundle);
426 struct gbaudio_data_connection *dai;
428 list_for_each_entry(dai, &gbmodule->data_list, list)
429 gb_connection_disable(dai->connection);
431 gb_connection_disable(gbmodule->mgmt_connection);
436 static int gb_audio_resume(struct device *dev)
438 struct gb_bundle *bundle = to_gb_bundle(dev);
439 struct gbaudio_module_info *gbmodule = greybus_get_drvdata(bundle);
440 struct gbaudio_data_connection *dai;
443 ret = gb_connection_enable(gbmodule->mgmt_connection);
445 dev_err(dev, "%d:Error while enabling mgmt connection\n", ret);
449 list_for_each_entry(dai, &gbmodule->data_list, list) {
450 ret = gb_connection_enable(dai->connection);
453 "%d:Error while enabling %d:data connection\n",
454 ret, dai->data_cport);
463 static const struct dev_pm_ops gb_audio_pm_ops = {
464 SET_RUNTIME_PM_OPS(gb_audio_suspend, gb_audio_resume, NULL)
467 static struct greybus_driver gb_audio_driver = {
469 .probe = gb_audio_probe,
470 .disconnect = gb_audio_disconnect,
471 .id_table = gb_audio_id_table,
472 .driver.pm = &gb_audio_pm_ops,
474 module_greybus_driver(gb_audio_driver);
476 MODULE_DESCRIPTION("Greybus Audio module driver");
478 MODULE_LICENSE("GPL v2");
479 MODULE_ALIAS("platform:gbaudio-module");