1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HD audio Component Binding Interface
5 * Copyright (C) 2021, 2023 Cirrus Logic, Inc. and
6 * Cirrus Logic International Semiconductor Ltd.
9 #include <linux/acpi.h>
10 #include <linux/component.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <sound/hda_codec.h>
14 #include "hda_component.h"
15 #include "hda_local.h"
18 void hda_component_acpi_device_notify(struct hda_component *comps, int num_comps,
19 acpi_handle handle, u32 event, void *data)
23 for (i = 0; i < num_comps; i++) {
24 if (comps[i].dev && comps[i].acpi_notify)
25 comps[i].acpi_notify(acpi_device_handle(comps[i].adev), event,
29 EXPORT_SYMBOL_NS_GPL(hda_component_acpi_device_notify, SND_HDA_SCODEC_COMPONENT);
31 int hda_component_manager_bind_acpi_notifications(struct hda_codec *cdc,
32 struct hda_component *comps, int num_comps,
33 acpi_notify_handler handler, void *data)
35 bool support_notifications = false;
36 struct acpi_device *adev;
41 if (!acpi_device_handle(adev))
44 for (i = 0; i < num_comps; i++)
45 support_notifications = support_notifications ||
46 comps[i].acpi_notifications_supported;
48 if (support_notifications) {
49 ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
52 codec_warn(cdc, "Failed to install notify handler: %d\n", ret);
56 codec_dbg(cdc, "Notify handler installed\n");
61 EXPORT_SYMBOL_NS_GPL(hda_component_manager_bind_acpi_notifications, SND_HDA_SCODEC_COMPONENT);
63 void hda_component_manager_unbind_acpi_notifications(struct hda_codec *cdc,
64 struct hda_component *comps,
65 acpi_notify_handler handler)
67 struct acpi_device *adev;
71 if (!acpi_device_handle(adev))
74 ret = acpi_remove_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY, handler);
76 codec_warn(cdc, "Failed to uninstall notify handler: %d\n", ret);
78 EXPORT_SYMBOL_NS_GPL(hda_component_manager_unbind_acpi_notifications, SND_HDA_SCODEC_COMPONENT);
79 #endif /* ifdef CONFIG_ACPI */
81 void hda_component_manager_playback_hook(struct hda_component *comps, int num_comps, int action)
85 for (i = 0; i < num_comps; i++) {
86 if (comps[i].dev && comps[i].pre_playback_hook)
87 comps[i].pre_playback_hook(comps[i].dev, action);
89 for (i = 0; i < num_comps; i++) {
90 if (comps[i].dev && comps[i].playback_hook)
91 comps[i].playback_hook(comps[i].dev, action);
93 for (i = 0; i < num_comps; i++) {
94 if (comps[i].dev && comps[i].post_playback_hook)
95 comps[i].post_playback_hook(comps[i].dev, action);
98 EXPORT_SYMBOL_NS_GPL(hda_component_manager_playback_hook, SND_HDA_SCODEC_COMPONENT);
100 struct hda_scodec_match {
103 const char *match_str;
107 /* match the device name in a slightly relaxed manner */
108 static int hda_comp_match_dev_name(struct device *dev, void *data)
110 struct hda_scodec_match *p = data;
111 const char *d = dev_name(dev);
112 int n = strlen(p->bus);
115 /* check the bus name */
116 if (strncmp(d, p->bus, n))
118 /* skip the bus number */
121 /* the rest must be exact matching */
122 snprintf(tmp, sizeof(tmp), p->match_str, p->hid, p->index);
123 return !strcmp(d + n, tmp);
126 int hda_component_manager_bind(struct hda_codec *cdc,
127 struct hda_component *comps, int count)
131 /* Init shared data */
132 for (i = 0; i < count; ++i) {
133 memset(&comps[i], 0, sizeof(comps[i]));
134 comps[i].codec = cdc;
137 return component_bind_all(hda_codec_dev(cdc), comps);
139 EXPORT_SYMBOL_NS_GPL(hda_component_manager_bind, SND_HDA_SCODEC_COMPONENT);
141 int hda_component_manager_init(struct hda_codec *cdc,
142 struct hda_component *comps, int count,
143 const char *bus, const char *hid,
144 const char *match_str,
145 const struct component_master_ops *ops)
147 struct device *dev = hda_codec_dev(cdc);
148 struct component_match *match = NULL;
149 struct hda_scodec_match *sm;
152 for (i = 0; i < count; i++) {
153 sm = devm_kmalloc(dev, sizeof(*sm), GFP_KERNEL);
159 sm->match_str = match_str;
161 component_match_add(dev, &match, hda_comp_match_dev_name, sm);
164 ret = component_master_add_with_match(dev, ops, match);
166 codec_err(cdc, "Fail to register component aggregator %d\n", ret);
170 EXPORT_SYMBOL_NS_GPL(hda_component_manager_init, SND_HDA_SCODEC_COMPONENT);
172 void hda_component_manager_free(struct hda_codec *cdc,
173 const struct component_master_ops *ops)
175 struct device *dev = hda_codec_dev(cdc);
177 component_master_del(dev, ops);
179 EXPORT_SYMBOL_NS_GPL(hda_component_manager_free, SND_HDA_SCODEC_COMPONENT);
181 MODULE_DESCRIPTION("HD Audio component binding library");
182 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
183 MODULE_LICENSE("GPL");