xref: /linux/sound/pci/hda/hda_component.c (revision 46e6acfe3501fa938af9c5bd730f0020235b08a2)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * HD audio Component Binding Interface
4  *
5  * Copyright (C) 2021, 2023 Cirrus Logic, Inc. and
6  *			Cirrus Logic International Semiconductor Ltd.
7  */
8 
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"
16 
17 #ifdef CONFIG_ACPI
18 void hda_component_acpi_device_notify(struct hda_component_parent *parent,
19 				      acpi_handle handle, u32 event, void *data)
20 {
21 	struct hda_component *comp;
22 	int i;
23 
24 	mutex_lock(&parent->mutex);
25 	for (i = 0; i < ARRAY_SIZE(parent->comps); i++) {
26 		comp = hda_component_from_index(parent, i);
27 		if (comp->dev && comp->acpi_notify)
28 			comp->acpi_notify(acpi_device_handle(comp->adev), event, comp->dev);
29 	}
30 	mutex_unlock(&parent->mutex);
31 }
32 EXPORT_SYMBOL_NS_GPL(hda_component_acpi_device_notify, SND_HDA_SCODEC_COMPONENT);
33 
34 int hda_component_manager_bind_acpi_notifications(struct hda_codec *cdc,
35 						  struct hda_component_parent *parent,
36 						  acpi_notify_handler handler, void *data)
37 {
38 	bool support_notifications = false;
39 	struct acpi_device *adev;
40 	struct hda_component *comp;
41 	int ret;
42 	int i;
43 
44 	adev = parent->comps[0].adev;
45 	if (!acpi_device_handle(adev))
46 		return 0;
47 
48 	for (i = 0; i < ARRAY_SIZE(parent->comps); i++) {
49 		comp = hda_component_from_index(parent, i);
50 		support_notifications = support_notifications ||
51 			comp->acpi_notifications_supported;
52 	}
53 
54 	if (support_notifications) {
55 		ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
56 						  handler, data);
57 		if (ret < 0) {
58 			codec_warn(cdc, "Failed to install notify handler: %d\n", ret);
59 			return 0;
60 		}
61 
62 		codec_dbg(cdc, "Notify handler installed\n");
63 	}
64 
65 	return 0;
66 }
67 EXPORT_SYMBOL_NS_GPL(hda_component_manager_bind_acpi_notifications, SND_HDA_SCODEC_COMPONENT);
68 
69 void hda_component_manager_unbind_acpi_notifications(struct hda_codec *cdc,
70 						     struct hda_component_parent *parent,
71 						     acpi_notify_handler handler)
72 {
73 	struct acpi_device *adev;
74 	int ret;
75 
76 	adev = parent->comps[0].adev;
77 	if (!acpi_device_handle(adev))
78 		return;
79 
80 	ret = acpi_remove_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY, handler);
81 	if (ret < 0)
82 		codec_warn(cdc, "Failed to uninstall notify handler: %d\n", ret);
83 }
84 EXPORT_SYMBOL_NS_GPL(hda_component_manager_unbind_acpi_notifications, SND_HDA_SCODEC_COMPONENT);
85 #endif /* ifdef CONFIG_ACPI */
86 
87 void hda_component_manager_playback_hook(struct hda_component_parent *parent, int action)
88 {
89 	struct hda_component *comp;
90 	int i;
91 
92 	mutex_lock(&parent->mutex);
93 	for (i = 0; i < ARRAY_SIZE(parent->comps); i++) {
94 		comp = hda_component_from_index(parent, i);
95 		if (comp->dev && comp->pre_playback_hook)
96 			comp->pre_playback_hook(comp->dev, action);
97 	}
98 	for (i = 0; i < ARRAY_SIZE(parent->comps); i++) {
99 		comp = hda_component_from_index(parent, i);
100 		if (comp->dev && comp->playback_hook)
101 			comp->playback_hook(comp->dev, action);
102 	}
103 	for (i = 0; i < ARRAY_SIZE(parent->comps); i++) {
104 		comp = hda_component_from_index(parent, i);
105 		if (comp->dev && comp->post_playback_hook)
106 			comp->post_playback_hook(comp->dev, action);
107 	}
108 	mutex_unlock(&parent->mutex);
109 }
110 EXPORT_SYMBOL_NS_GPL(hda_component_manager_playback_hook, SND_HDA_SCODEC_COMPONENT);
111 
112 struct hda_scodec_match {
113 	const char *bus;
114 	const char *hid;
115 	const char *match_str;
116 	int index;
117 };
118 
119 /* match the device name in a slightly relaxed manner */
120 static int hda_comp_match_dev_name(struct device *dev, void *data)
121 {
122 	struct hda_scodec_match *p = data;
123 	const char *d = dev_name(dev);
124 	int n = strlen(p->bus);
125 	char tmp[32];
126 
127 	/* check the bus name */
128 	if (strncmp(d, p->bus, n))
129 		return 0;
130 	/* skip the bus number */
131 	if (isdigit(d[n]))
132 		n++;
133 	/* the rest must be exact matching */
134 	snprintf(tmp, sizeof(tmp), p->match_str, p->hid, p->index);
135 	return !strcmp(d + n, tmp);
136 }
137 
138 int hda_component_manager_bind(struct hda_codec *cdc,
139 			       struct hda_component_parent *parent)
140 {
141 	int ret;
142 
143 	/* Init shared and component specific data */
144 	memset(parent, 0, sizeof(*parent));
145 	mutex_init(&parent->mutex);
146 	parent->codec = cdc;
147 
148 	mutex_lock(&parent->mutex);
149 	ret = component_bind_all(hda_codec_dev(cdc), parent);
150 	mutex_unlock(&parent->mutex);
151 
152 	return ret;
153 }
154 EXPORT_SYMBOL_NS_GPL(hda_component_manager_bind, SND_HDA_SCODEC_COMPONENT);
155 
156 int hda_component_manager_init(struct hda_codec *cdc,
157 			       struct hda_component_parent *parent, int count,
158 			       const char *bus, const char *hid,
159 			       const char *match_str,
160 			       const struct component_master_ops *ops)
161 {
162 	struct device *dev = hda_codec_dev(cdc);
163 	struct component_match *match = NULL;
164 	struct hda_scodec_match *sm;
165 	int ret, i;
166 
167 	for (i = 0; i < count; i++) {
168 		sm = devm_kmalloc(dev, sizeof(*sm), GFP_KERNEL);
169 		if (!sm)
170 			return -ENOMEM;
171 
172 		sm->bus = bus;
173 		sm->hid = hid;
174 		sm->match_str = match_str;
175 		sm->index = i;
176 		component_match_add(dev, &match, hda_comp_match_dev_name, sm);
177 	}
178 
179 	ret = component_master_add_with_match(dev, ops, match);
180 	if (ret)
181 		codec_err(cdc, "Fail to register component aggregator %d\n", ret);
182 
183 	return ret;
184 }
185 EXPORT_SYMBOL_NS_GPL(hda_component_manager_init, SND_HDA_SCODEC_COMPONENT);
186 
187 void hda_component_manager_free(struct hda_codec *cdc,
188 				const struct component_master_ops *ops)
189 {
190 	struct device *dev = hda_codec_dev(cdc);
191 
192 	component_master_del(dev, ops);
193 }
194 EXPORT_SYMBOL_NS_GPL(hda_component_manager_free, SND_HDA_SCODEC_COMPONENT);
195 
196 MODULE_DESCRIPTION("HD Audio component binding library");
197 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
198 MODULE_LICENSE("GPL");
199