Commit fd895a74 authored by Richard Fitzgerald's avatar Richard Fitzgerald Committed by Takashi Iwai
Browse files

ALSA: hda: realtek: Move hda_component implementation to module



Move the generic parts of the hda_component implementation into a new
hda_component module. This will allow other HDA codecs to add support
for the component binding API without duplicating all the code.

Signed-off-by: default avatarRichard Fitzgerald <rf@opensource.cirrus.com>
Tested-by: default avatarGergo Koteles <soyer@irl.hu>
Link: https://lore.kernel.org/r/20240124112607.77614-3-rf@opensource.cirrus.com


Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent cf0d9566
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -5014,6 +5014,7 @@ F: include/linux/mfd/cs42l43*
F:	include/sound/cs*
F:	sound/pci/hda/cirrus*
F:	sound/pci/hda/cs*
F:	sound/pci/hda/hda_component*
F:	sound/pci/hda/hda_cs_dsp_ctl.*
F:	sound/soc/codecs/cs*
+4 −0
Original line number Diff line number Diff line
@@ -116,6 +116,9 @@ config SND_HDA_CS_DSP_CONTROLS
	tristate
	select FW_CS_DSP

config SND_HDA_SCODEC_COMPONENT
	tristate

config SND_HDA_SCODEC_CS35L41_I2C
	tristate "Build CS35L41 HD-audio side codec support for I2C Bus"
	depends on I2C
@@ -201,6 +204,7 @@ config SND_HDA_CODEC_REALTEK
	tristate "Build Realtek HD-audio codec support"
	select SND_HDA_GENERIC
	select SND_HDA_GENERIC_LEDS
	select SND_HDA_SCODEC_COMPONENT
	help
	  Say Y or M here to include Realtek HD-audio codec support in
	  snd-hda-intel driver, such as ALC880.
+2 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ snd-hda-scodec-cs35l56-objs := cs35l56_hda.o
snd-hda-scodec-cs35l56-i2c-objs :=	cs35l56_hda_i2c.o
snd-hda-scodec-cs35l56-spi-objs :=	cs35l56_hda_spi.o
snd-hda-cs-dsp-ctls-objs :=		hda_cs_dsp_ctl.o
snd-hda-scodec-component-objs :=	hda_component.o
snd-hda-scodec-tas2781-i2c-objs :=	tas2781_hda_i2c.o

# common driver
@@ -67,6 +68,7 @@ obj-$(CONFIG_SND_HDA_SCODEC_CS35L56) += snd-hda-scodec-cs35l56.o
obj-$(CONFIG_SND_HDA_SCODEC_CS35L56_I2C) += snd-hda-scodec-cs35l56-i2c.o
obj-$(CONFIG_SND_HDA_SCODEC_CS35L56_SPI) += snd-hda-scodec-cs35l56-spi.o
obj-$(CONFIG_SND_HDA_CS_DSP_CONTROLS) += snd-hda-cs-dsp-ctls.o
obj-$(CONFIG_SND_HDA_SCODEC_COMPONENT) += snd-hda-scodec-component.o
obj-$(CONFIG_SND_HDA_SCODEC_TAS2781_I2C) += snd-hda-scodec-tas2781-i2c.o

# this must be the last entry after codec drivers;
+169 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * HD audio Component Binding Interface
 *
 * Copyright (C) 2021, 2023 Cirrus Logic, Inc. and
 *			Cirrus Logic International Semiconductor Ltd.
 */

#include <linux/acpi.h>
#include <linux/component.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <sound/hda_codec.h>
#include "hda_component.h"
#include "hda_local.h"

#ifdef CONFIG_ACPI
void hda_component_acpi_device_notify(struct hda_component *comps, int num_comps,
				      acpi_handle handle, u32 event, void *data)
{
	int i;

	for (i = 0; i < num_comps; i++) {
		if (comps[i].dev && comps[i].acpi_notify)
			comps[i].acpi_notify(acpi_device_handle(comps[i].adev), event,
					     comps[i].dev);
	}
}
EXPORT_SYMBOL_NS_GPL(hda_component_acpi_device_notify, SND_HDA_SCODEC_COMPONENT);

int hda_component_manager_bind_acpi_notifications(struct hda_codec *cdc,
						  struct hda_component *comps, int num_comps,
						  acpi_notify_handler handler, void *data)
{
	bool support_notifications = false;
	struct acpi_device *adev;
	int ret;
	int i;

	adev = comps[0].adev;
	if (!acpi_device_handle(adev))
		return 0;

	for (i = 0; i < num_comps; i++)
		support_notifications = support_notifications ||
			comps[i].acpi_notifications_supported;

	if (support_notifications) {
		ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
						  handler, data);
		if (ret < 0) {
			codec_warn(cdc, "Failed to install notify handler: %d\n", ret);
			return 0;
		}

		codec_dbg(cdc, "Notify handler installed\n");
	}

	return 0;
}
EXPORT_SYMBOL_NS_GPL(hda_component_manager_bind_acpi_notifications, SND_HDA_SCODEC_COMPONENT);

void hda_component_manager_unbind_acpi_notifications(struct hda_codec *cdc,
						     struct hda_component *comps,
						     acpi_notify_handler handler)
{
	struct acpi_device *adev;
	int ret;

	adev = comps[0].adev;
	if (!acpi_device_handle(adev))
		return;

	ret = acpi_remove_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY, handler);
	if (ret < 0)
		codec_warn(cdc, "Failed to uninstall notify handler: %d\n", ret);
}
EXPORT_SYMBOL_NS_GPL(hda_component_manager_unbind_acpi_notifications, SND_HDA_SCODEC_COMPONENT);
#endif /* ifdef CONFIG_ACPI */

void hda_component_manager_playback_hook(struct hda_component *comps, int num_comps, int action)
{
	int i;

	for (i = 0; i < num_comps; i++) {
		if (comps[i].dev && comps[i].pre_playback_hook)
			comps[i].pre_playback_hook(comps[i].dev, action);
	}
	for (i = 0; i < num_comps; i++) {
		if (comps[i].dev && comps[i].playback_hook)
			comps[i].playback_hook(comps[i].dev, action);
	}
	for (i = 0; i < num_comps; i++) {
		if (comps[i].dev && comps[i].post_playback_hook)
			comps[i].post_playback_hook(comps[i].dev, action);
	}
}
EXPORT_SYMBOL_NS_GPL(hda_component_manager_playback_hook, SND_HDA_SCODEC_COMPONENT);

struct hda_scodec_match {
	const char *bus;
	const char *hid;
	const char *match_str;
	int index;
};

/* match the device name in a slightly relaxed manner */
static int hda_comp_match_dev_name(struct device *dev, void *data)
{
	struct hda_scodec_match *p = data;
	const char *d = dev_name(dev);
	int n = strlen(p->bus);
	char tmp[32];

	/* check the bus name */
	if (strncmp(d, p->bus, n))
		return 0;
	/* skip the bus number */
	if (isdigit(d[n]))
		n++;
	/* the rest must be exact matching */
	snprintf(tmp, sizeof(tmp), p->match_str, p->hid, p->index);
	return !strcmp(d + n, tmp);
}

int hda_component_manager_init(struct hda_codec *cdc,
			       struct hda_component *comps, int count,
			       const char *bus, const char *hid,
			       const char *match_str,
			       const struct component_master_ops *ops)
{
	struct device *dev = hda_codec_dev(cdc);
	struct component_match *match = NULL;
	struct hda_scodec_match *sm;
	int ret, i;

	for (i = 0; i < count; i++) {
		sm = devm_kmalloc(dev, sizeof(*sm), GFP_KERNEL);
		if (!sm)
			return -ENOMEM;

		sm->bus = bus;
		sm->hid = hid;
		sm->match_str = match_str;
		sm->index = i;
		comps[i].codec = cdc;
		component_match_add(dev, &match, hda_comp_match_dev_name, sm);
	}

	ret = component_master_add_with_match(dev, ops, match);
	if (ret)
		codec_err(cdc, "Fail to register component aggregator %d\n", ret);

	return ret;
}
EXPORT_SYMBOL_NS_GPL(hda_component_manager_init, SND_HDA_SCODEC_COMPONENT);

void hda_component_manager_free(struct hda_codec *cdc,
				const struct component_master_ops *ops)
{
	struct device *dev = hda_codec_dev(cdc);

	component_master_del(dev, ops);
}
EXPORT_SYMBOL_NS_GPL(hda_component_manager_free, SND_HDA_SCODEC_COMPONENT);

MODULE_DESCRIPTION("HD Audio component binding library");
MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
MODULE_LICENSE("GPL");
+59 −0
Original line number Diff line number Diff line
@@ -23,3 +23,62 @@ struct hda_component {
	void (*playback_hook)(struct device *dev, int action);
	void (*post_playback_hook)(struct device *dev, int action);
};

#ifdef CONFIG_ACPI
void hda_component_acpi_device_notify(struct hda_component *comps, int num_comps,
				      acpi_handle handle, u32 event, void *data);
int hda_component_manager_bind_acpi_notifications(struct hda_codec *cdc,
						  struct hda_component *comps, int num_comps,
						  acpi_notify_handler handler, void *data);
void hda_component_manager_unbind_acpi_notifications(struct hda_codec *cdc,
						     struct hda_component *comps,
						     acpi_notify_handler handler);
#else
static inline void hda_component_acpi_device_notify(struct hda_component *comps,
						    int num_comps,
						    acpi_handle handle,
						    u32 event,
						    void *data)
{
}

static inline int hda_component_manager_bind_acpi_notifications(struct hda_codec *cdc,
								struct hda_component *comps,
								int num_comps,
								acpi_notify_handler handler,
								void *data)

{
	return 0;
}

static inline void hda_component_manager_unbind_acpi_notifications(struct hda_codec *cdc,
								   struct hda_component *comps,
								   acpi_notify_handler handler)
{
}
#endif /* ifdef CONFIG_ACPI */

void hda_component_manager_playback_hook(struct hda_component *comps, int num_comps,
					 int action);

int hda_component_manager_init(struct hda_codec *cdc,
			       struct hda_component *comps, int count,
			       const char *bus, const char *hid,
			       const char *match_str,
			       const struct component_master_ops *ops);

void hda_component_manager_free(struct hda_codec *cdc,
				const struct component_master_ops *ops);

static inline int hda_component_manager_bind(struct hda_codec *cdc,
					     struct hda_component *comps)
{
	return component_bind_all(hda_codec_dev(cdc), comps);
}

static inline void hda_component_manager_unbind(struct hda_codec *cdc,
					       struct hda_component *comps)
{
	component_unbind_all(hda_codec_dev(cdc), comps);
}
Loading