xref: /linux/sound/pci/hda/hda_component.c (revision a36e9f5cfe9eb3a1dce8769c7058251c42705357)
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->comps, 0, sizeof(parent->comps));
145 	parent->codec = cdc;
146 
147 	mutex_lock(&parent->mutex);
148 	ret = component_bind_all(hda_codec_dev(cdc), parent);
149 	mutex_unlock(&parent->mutex);
150 
151 	return ret;
152 }
153 EXPORT_SYMBOL_NS_GPL(hda_component_manager_bind, SND_HDA_SCODEC_COMPONENT);
154 
155 int hda_component_manager_init(struct hda_codec *cdc,
156 			       struct hda_component_parent *parent, int count,
157 			       const char *bus, const char *hid,
158 			       const char *match_str,
159 			       const struct component_master_ops *ops)
160 {
161 	struct device *dev = hda_codec_dev(cdc);
162 	struct component_match *match = NULL;
163 	struct hda_scodec_match *sm;
164 	int ret, i;
165 
166 	mutex_init(&parent->mutex);
167 
168 	for (i = 0; i < count; i++) {
169 		sm = devm_kmalloc(dev, sizeof(*sm), GFP_KERNEL);
170 		if (!sm)
171 			return -ENOMEM;
172 
173 		sm->bus = bus;
174 		sm->hid = hid;
175 		sm->match_str = match_str;
176 		sm->index = i;
177 		component_match_add(dev, &match, hda_comp_match_dev_name, sm);
178 	}
179 
180 	ret = component_master_add_with_match(dev, ops, match);
181 	if (ret)
182 		codec_err(cdc, "Fail to register component aggregator %d\n", ret);
183 
184 	return ret;
185 }
186 EXPORT_SYMBOL_NS_GPL(hda_component_manager_init, SND_HDA_SCODEC_COMPONENT);
187 
188 void hda_component_manager_free(struct hda_codec *cdc,
189 				const struct component_master_ops *ops)
190 {
191 	struct device *dev = hda_codec_dev(cdc);
192 
193 	component_master_del(dev, ops);
194 }
195 EXPORT_SYMBOL_NS_GPL(hda_component_manager_free, SND_HDA_SCODEC_COMPONENT);
196 
197 MODULE_DESCRIPTION("HD Audio component binding library");
198 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
199 MODULE_LICENSE("GPL");
200