1 // SPDX-License-Identifier: GPL-2.0-only
3 // Common code for Cirrus side-codecs.
5 // Copyright (C) 2021, 2023 Cirrus Logic, Inc. and
6 // Cirrus Logic International Semiconductor Ltd.
8 #include <linux/dev_printk.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
12 #include "cirrus_scodec.h"
14 int cirrus_scodec_get_speaker_id(struct device *dev, int amp_index,
15 int num_amps, int fixed_gpio_id)
17 struct gpio_desc *speaker_id_desc;
18 int speaker_id = -ENOENT;
20 if (fixed_gpio_id >= 0) {
21 dev_dbg(dev, "Found Fixed Speaker ID GPIO (index = %d)\n", fixed_gpio_id);
22 speaker_id_desc = gpiod_get_index(dev, NULL, fixed_gpio_id, GPIOD_IN);
23 if (IS_ERR(speaker_id_desc)) {
24 speaker_id = PTR_ERR(speaker_id_desc);
27 speaker_id = gpiod_get_value_cansleep(speaker_id_desc);
28 gpiod_put(speaker_id_desc);
36 count = gpiod_count(dev, "spk-id");
39 gpios_per_amp = count / num_amps;
40 base_index = gpios_per_amp * amp_index;
45 dev_dbg(dev, "Found %d Speaker ID GPIOs per Amp\n", gpios_per_amp);
47 for (i = 0; i < gpios_per_amp; i++) {
48 speaker_id_desc = gpiod_get_index(dev, "spk-id", i + base_index,
50 if (IS_ERR(speaker_id_desc)) {
51 speaker_id = PTR_ERR(speaker_id_desc);
54 tmp = gpiod_get_value_cansleep(speaker_id_desc);
55 gpiod_put(speaker_id_desc);
60 speaker_id |= tmp << i;
65 dev_dbg(dev, "Speaker ID = %d\n", speaker_id);
69 EXPORT_SYMBOL_NS_GPL(cirrus_scodec_get_speaker_id, SND_HDA_CIRRUS_SCODEC);
71 MODULE_DESCRIPTION("HDA Cirrus side-codec library");
72 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
73 MODULE_LICENSE("GPL");