xref: /linux/sound/pci/hda/cirrus_scodec.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Common code for Cirrus side-codecs.
4 //
5 // Copyright (C) 2021, 2023 Cirrus Logic, Inc. and
6 //               Cirrus Logic International Semiconductor Ltd.
7 
8 #include <linux/dev_printk.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 
12 #include "cirrus_scodec.h"
13 
cirrus_scodec_get_speaker_id(struct device * dev,int amp_index,int num_amps,int fixed_gpio_id)14 int cirrus_scodec_get_speaker_id(struct device *dev, int amp_index,
15 				 int num_amps, int fixed_gpio_id)
16 {
17 	struct gpio_desc *speaker_id_desc;
18 	int speaker_id = -ENOENT;
19 
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);
25 			return speaker_id;
26 		}
27 		speaker_id = gpiod_get_value_cansleep(speaker_id_desc);
28 		gpiod_put(speaker_id_desc);
29 	} else {
30 		int base_index;
31 		int gpios_per_amp;
32 		int count;
33 		int tmp;
34 		int i;
35 
36 		count = gpiod_count(dev, "spk-id");
37 		if (count > 0) {
38 			speaker_id = 0;
39 			gpios_per_amp = count / num_amps;
40 			base_index = gpios_per_amp * amp_index;
41 
42 			if (count % num_amps)
43 				return -EINVAL;
44 
45 			dev_dbg(dev, "Found %d Speaker ID GPIOs per Amp\n", gpios_per_amp);
46 
47 			for (i = 0; i < gpios_per_amp; i++) {
48 				speaker_id_desc = gpiod_get_index(dev, "spk-id", i + base_index,
49 								  GPIOD_IN);
50 				if (IS_ERR(speaker_id_desc)) {
51 					speaker_id = PTR_ERR(speaker_id_desc);
52 					break;
53 				}
54 				tmp = gpiod_get_value_cansleep(speaker_id_desc);
55 				gpiod_put(speaker_id_desc);
56 				if (tmp < 0) {
57 					speaker_id = tmp;
58 					break;
59 				}
60 				speaker_id |= tmp << i;
61 			}
62 		}
63 	}
64 
65 	dev_dbg(dev, "Speaker ID = %d\n", speaker_id);
66 
67 	return speaker_id;
68 }
69 EXPORT_SYMBOL_NS_GPL(cirrus_scodec_get_speaker_id, SND_HDA_CIRRUS_SCODEC);
70 
71 MODULE_DESCRIPTION("HDA Cirrus side-codec library");
72 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
73 MODULE_LICENSE("GPL");
74