xref: /linux/sound/pci/hda/cs35l41_hda_property.c (revision ae22a94997b8a03dcb3c922857c203246711f9d4)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // CS35L41 ALSA HDA Property driver
4 //
5 // Copyright 2023 Cirrus Logic, Inc.
6 //
7 // Author: Stefan Binding <sbinding@opensource.cirrus.com>
8 
9 #include <linux/acpi.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/string.h>
12 #include "cs35l41_hda_property.h"
13 #include <linux/spi/spi.h>
14 
15 #define MAX_AMPS 4
16 
17 struct cs35l41_config {
18 	const char *ssid;
19 	int num_amps;
20 	enum {
21 		INTERNAL,
22 		EXTERNAL
23 	} boost_type;
24 	u8 channel[MAX_AMPS];
25 	int reset_gpio_index; /* -1 if no reset gpio */
26 	int spkid_gpio_index; /* -1 if no spkid gpio */
27 	int cs_gpio_index; /* -1 if no cs gpio, or cs-gpios already exists, max num amps == 2 */
28 	int boost_ind_nanohenry; /* Required if boost_type == Internal */
29 	int boost_peak_milliamp; /* Required if boost_type == Internal */
30 	int boost_cap_microfarad; /* Required if boost_type == Internal */
31 };
32 
33 static const struct cs35l41_config cs35l41_config_table[] = {
34 	{ "10280B27", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
35 	{ "10280B28", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
36 	{ "10280BEB", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, -1, 0, 0, 0, 0 },
37 	{ "10280C4D", 4, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, CS35L41_LEFT, CS35L41_RIGHT }, 0, 1, -1, 1000, 4500, 24 },
38 /*
39  * Device 103C89C6 does have _DSD, however it is setup to use the wrong boost type.
40  * We can override the _DSD to correct the boost type here.
41  * Since this laptop has valid ACPI, we do not need to handle cs-gpios, since that already exists
42  * in the ACPI. The Reset GPIO is also valid, so we can use the Reset defined in _DSD.
43  */
44 	{ "103C89C6", 2, INTERNAL, { CS35L41_RIGHT, CS35L41_LEFT, 0, 0 }, -1, -1, -1, 1000, 4500, 24 },
45 	{ "103C8A28", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
46 	{ "103C8A29", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
47 	{ "103C8A2A", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
48 	{ "103C8A2B", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
49 	{ "103C8A2C", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
50 	{ "103C8A2D", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
51 	{ "103C8A2E", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
52 	{ "103C8A30", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
53 	{ "103C8A31", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
54 	{ "103C8A6E", 4, EXTERNAL, { CS35L41_LEFT, CS35L41_LEFT, CS35L41_RIGHT, CS35L41_RIGHT }, 0, -1, -1, 0, 0, 0 },
55 	{ "103C8BB3", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
56 	{ "103C8BB4", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
57 	{ "103C8BDD", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
58 	{ "103C8BDE", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
59 	{ "103C8BDF", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
60 	{ "103C8BE0", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
61 	{ "103C8BE1", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
62 	{ "103C8BE2", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
63 	{ "103C8BE3", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
64 	{ "103C8BE5", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
65 	{ "103C8BE6", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
66 	{ "103C8BE7", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
67 	{ "103C8BE8", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
68 	{ "103C8BE9", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
69 	{ "103C8B3A", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
70 	{ "103C8C15", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4000, 24 },
71 	{ "103C8C16", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4000, 24 },
72 	{ "103C8C17", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4000, 24 },
73 	{ "103C8C4F", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
74 	{ "103C8C50", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
75 	{ "103C8C51", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
76 	{ "103C8CDD", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
77 	{ "103C8CDE", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 3900, 24 },
78 	{ "104312AF", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
79 	{ "10431433", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
80 	{ "10431463", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
81 	{ "10431473", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, -1, 0, 1000, 4500, 24 },
82 	{ "10431483", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, -1, 0, 1000, 4500, 24 },
83 	{ "10431493", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
84 	{ "104314D3", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
85 	{ "104314E3", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
86 	{ "10431503", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
87 	{ "10431533", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
88 	{ "10431573", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
89 	{ "10431663", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, -1, 0, 1000, 4500, 24 },
90 	{ "10431683", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
91 	{ "104316A3", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
92 	{ "104316D3", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
93 	{ "104316F3", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
94 	{ "104317F3", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
95 	{ "10431863", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
96 	{ "104318D3", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
97 	{ "10431A83", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
98 	{ "10431C9F", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
99 	{ "10431CAF", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
100 	{ "10431CCF", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
101 	{ "10431CDF", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
102 	{ "10431CEF", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
103 	{ "10431D1F", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
104 	{ "10431DA2", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
105 	{ "10431E02", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
106 	{ "10431E12", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
107 	{ "10431EE2", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, -1, -1, 0, 0, 0 },
108 	{ "10431F12", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
109 	{ "10431F1F", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, -1, 0, 0, 0, 0 },
110 	{ "10431F62", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
111 	{ "17AA386F", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, -1, -1, 0, 0, 0 },
112 	{ "17AA38A9", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 2, -1, 0, 0, 0 },
113 	{ "17AA38AB", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 2, -1, 0, 0, 0 },
114 	{ "17AA38B4", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
115 	{ "17AA38B5", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
116 	{ "17AA38B6", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
117 	{ "17AA38B7", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
118 	{}
119 };
120 
121 static int cs35l41_add_gpios(struct cs35l41_hda *cs35l41, struct device *physdev, int reset_gpio,
122 			     int spkid_gpio, int cs_gpio_index, int num_amps)
123 {
124 	struct acpi_gpio_mapping *gpio_mapping = NULL;
125 	struct acpi_gpio_params *reset_gpio_params = NULL;
126 	struct acpi_gpio_params *spkid_gpio_params = NULL;
127 	struct acpi_gpio_params *cs_gpio_params = NULL;
128 	unsigned int num_entries = 0;
129 	unsigned int reset_index, spkid_index, csgpio_index;
130 	int i;
131 
132 	/*
133 	 * GPIO Mapping only needs to be done once, since it would be available for subsequent amps
134 	 */
135 	if (cs35l41->dacpi->driver_gpios)
136 		return 0;
137 
138 	if (reset_gpio >= 0) {
139 		reset_index = num_entries;
140 		num_entries++;
141 	}
142 
143 	if (spkid_gpio >= 0) {
144 		spkid_index = num_entries;
145 		num_entries++;
146 	}
147 
148 	if ((cs_gpio_index >= 0)  && (num_amps == 2)) {
149 		csgpio_index = num_entries;
150 		num_entries++;
151 	}
152 
153 	if (!num_entries)
154 		return 0;
155 
156 	/* must include termination entry */
157 	num_entries++;
158 
159 	gpio_mapping = devm_kcalloc(physdev, num_entries, sizeof(struct acpi_gpio_mapping),
160 				    GFP_KERNEL);
161 
162 	if (!gpio_mapping)
163 		goto err;
164 
165 	if (reset_gpio >= 0) {
166 		gpio_mapping[reset_index].name = "reset-gpios";
167 		reset_gpio_params = devm_kcalloc(physdev, num_amps, sizeof(struct acpi_gpio_params),
168 						 GFP_KERNEL);
169 		if (!reset_gpio_params)
170 			goto err;
171 
172 		for (i = 0; i < num_amps; i++)
173 			reset_gpio_params[i].crs_entry_index = reset_gpio;
174 
175 		gpio_mapping[reset_index].data = reset_gpio_params;
176 		gpio_mapping[reset_index].size = num_amps;
177 	}
178 
179 	if (spkid_gpio >= 0) {
180 		gpio_mapping[spkid_index].name = "spk-id-gpios";
181 		spkid_gpio_params = devm_kcalloc(physdev, num_amps, sizeof(struct acpi_gpio_params),
182 						 GFP_KERNEL);
183 		if (!spkid_gpio_params)
184 			goto err;
185 
186 		for (i = 0; i < num_amps; i++)
187 			spkid_gpio_params[i].crs_entry_index = spkid_gpio;
188 
189 		gpio_mapping[spkid_index].data = spkid_gpio_params;
190 		gpio_mapping[spkid_index].size = num_amps;
191 	}
192 
193 	if ((cs_gpio_index >= 0) && (num_amps == 2)) {
194 		gpio_mapping[csgpio_index].name = "cs-gpios";
195 		/* only one GPIO CS is supported without using _DSD, obtained using index 0 */
196 		cs_gpio_params = devm_kzalloc(physdev, sizeof(struct acpi_gpio_params), GFP_KERNEL);
197 		if (!cs_gpio_params)
198 			goto err;
199 
200 		cs_gpio_params->crs_entry_index = cs_gpio_index;
201 
202 		gpio_mapping[csgpio_index].data = cs_gpio_params;
203 		gpio_mapping[csgpio_index].size = 1;
204 	}
205 
206 	return devm_acpi_dev_add_driver_gpios(physdev, gpio_mapping);
207 err:
208 	devm_kfree(physdev, gpio_mapping);
209 	devm_kfree(physdev, reset_gpio_params);
210 	devm_kfree(physdev, spkid_gpio_params);
211 	devm_kfree(physdev, cs_gpio_params);
212 	return -ENOMEM;
213 }
214 
215 static int generic_dsd_config(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
216 			      const char *hid)
217 {
218 	struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
219 	const struct cs35l41_config *cfg;
220 	struct gpio_desc *cs_gpiod;
221 	struct spi_device *spi;
222 	bool dsd_found;
223 	int ret;
224 	int i;
225 
226 	for (cfg = cs35l41_config_table; cfg->ssid; cfg++) {
227 		if (!strcasecmp(cfg->ssid, cs35l41->acpi_subsystem_id))
228 			break;
229 	}
230 
231 	if (!cfg->ssid)
232 		return -ENOENT;
233 
234 	if (!cs35l41->dacpi || cs35l41->dacpi != ACPI_COMPANION(physdev)) {
235 		dev_err(cs35l41->dev, "ACPI Device does not match, cannot override _DSD.\n");
236 		return -ENODEV;
237 	}
238 
239 	dev_info(cs35l41->dev, "Adding DSD properties for %s\n", cs35l41->acpi_subsystem_id);
240 
241 	dsd_found = acpi_dev_has_props(cs35l41->dacpi);
242 
243 	if (!dsd_found) {
244 		ret = cs35l41_add_gpios(cs35l41, physdev, cfg->reset_gpio_index,
245 						 cfg->spkid_gpio_index, cfg->cs_gpio_index,
246 						 cfg->num_amps);
247 		if (ret) {
248 			dev_err(cs35l41->dev, "Error adding GPIO mapping: %d\n", ret);
249 			return ret;
250 		}
251 	} else if (cfg->reset_gpio_index >= 0 || cfg->spkid_gpio_index >= 0) {
252 		dev_warn(cs35l41->dev, "Cannot add Reset/Speaker ID/SPI CS GPIO Mapping, "
253 			 "_DSD already exists.\n");
254 	}
255 
256 	if (cs35l41->control_bus == SPI) {
257 		cs35l41->index = id;
258 
259 		/*
260 		 * Manually set the Chip Select for the second amp <cs_gpio_index> in the node.
261 		 * This is only supported for systems with 2 amps, since we cannot expand the
262 		 * default number of chip selects without using cs-gpios
263 		 * The CS GPIO must be set high prior to communicating with the first amp (which
264 		 * uses a native chip select), to ensure the second amp does not clash with the
265 		 * first.
266 		 */
267 		if (IS_ENABLED(CONFIG_SPI) && cfg->cs_gpio_index >= 0) {
268 			spi = to_spi_device(cs35l41->dev);
269 
270 			if (cfg->num_amps != 2) {
271 				dev_warn(cs35l41->dev,
272 					 "Cannot update SPI CS, Number of Amps (%d) != 2\n",
273 					 cfg->num_amps);
274 			} else if (dsd_found) {
275 				dev_warn(cs35l41->dev,
276 					"Cannot update SPI CS, _DSD already exists.\n");
277 			} else {
278 				/*
279 				 * This is obtained using driver_gpios, since only one GPIO for CS
280 				 * exists, this can be obtained using index 0.
281 				 */
282 				cs_gpiod = gpiod_get_index(physdev, "cs", 0, GPIOD_OUT_LOW);
283 				if (IS_ERR(cs_gpiod)) {
284 					dev_err(cs35l41->dev,
285 						"Unable to get Chip Select GPIO descriptor\n");
286 					return PTR_ERR(cs_gpiod);
287 				}
288 				if (id == 1) {
289 					spi_set_csgpiod(spi, 0, cs_gpiod);
290 					cs35l41->cs_gpio = cs_gpiod;
291 				} else {
292 					gpiod_set_value_cansleep(cs_gpiod, true);
293 					gpiod_put(cs_gpiod);
294 				}
295 				spi_setup(spi);
296 			}
297 		}
298 	} else {
299 		if (cfg->num_amps > 2)
300 			/*
301 			 * i2c addresses for 3/4 amps are used in order: 0x40, 0x41, 0x42, 0x43,
302 			 * subtracting 0x40 would give zero-based index
303 			 */
304 			cs35l41->index = id - 0x40;
305 		else
306 			/* i2c addr 0x40 for first amp (always), 0x41/0x42 for 2nd amp */
307 			cs35l41->index = id == 0x40 ? 0 : 1;
308 	}
309 
310 	cs35l41->reset_gpio = fwnode_gpiod_get_index(acpi_fwnode_handle(cs35l41->dacpi), "reset",
311 						     cs35l41->index, GPIOD_OUT_LOW,
312 						     "cs35l41-reset");
313 	cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, cs35l41->index, cfg->num_amps, -1);
314 
315 	hw_cfg->spk_pos = cfg->channel[cs35l41->index];
316 
317 	cs35l41->channel_index = 0;
318 	for (i = 0; i < cs35l41->index; i++)
319 		if (cfg->channel[i] == hw_cfg->spk_pos)
320 			cs35l41->channel_index++;
321 
322 	if (cfg->boost_type == INTERNAL) {
323 		hw_cfg->bst_type = CS35L41_INT_BOOST;
324 		hw_cfg->bst_ind = cfg->boost_ind_nanohenry;
325 		hw_cfg->bst_ipk = cfg->boost_peak_milliamp;
326 		hw_cfg->bst_cap = cfg->boost_cap_microfarad;
327 		hw_cfg->gpio1.func = CS35L41_NOT_USED;
328 		hw_cfg->gpio1.valid = true;
329 	} else {
330 		hw_cfg->bst_type = CS35L41_EXT_BOOST;
331 		hw_cfg->bst_ind = -1;
332 		hw_cfg->bst_ipk = -1;
333 		hw_cfg->bst_cap = -1;
334 		hw_cfg->gpio1.func = CS35l41_VSPK_SWITCH;
335 		hw_cfg->gpio1.valid = true;
336 	}
337 
338 	hw_cfg->gpio2.func = CS35L41_INTERRUPT;
339 	hw_cfg->gpio2.valid = true;
340 	hw_cfg->valid = true;
341 
342 	return 0;
343 }
344 
345 /*
346  * Systems 103C8C66, 103C8C67, 103C8C68, 103C8C6A use a dual speaker id system - each speaker has
347  * its own speaker id.
348  */
349 static int hp_i2c_int_2amp_dual_spkid(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
350 				      const char *hid)
351 {
352 	struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
353 
354 	/* If _DSD exists for this laptop, we cannot support it through here */
355 	if (acpi_dev_has_props(cs35l41->dacpi))
356 		return -ENOENT;
357 
358 	/* check I2C address to assign the index */
359 	cs35l41->index = id == 0x40 ? 0 : 1;
360 	cs35l41->channel_index = 0;
361 	cs35l41->reset_gpio = gpiod_get_index(physdev, NULL, 0, GPIOD_OUT_HIGH);
362 	if (cs35l41->index == 0)
363 		cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, 0, 0, 1);
364 	else
365 		cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, 0, 0, 2);
366 	hw_cfg->spk_pos = cs35l41->index;
367 	hw_cfg->gpio2.func = CS35L41_INTERRUPT;
368 	hw_cfg->gpio2.valid = true;
369 	hw_cfg->valid = true;
370 
371 	hw_cfg->bst_type = CS35L41_INT_BOOST;
372 	hw_cfg->bst_ind = 1000;
373 	hw_cfg->bst_ipk = 4100;
374 	hw_cfg->bst_cap = 24;
375 	hw_cfg->gpio1.func = CS35L41_NOT_USED;
376 	hw_cfg->gpio1.valid = true;
377 
378 	return 0;
379 }
380 
381 /*
382  * Device CLSA010(0/1) doesn't have _DSD so a gpiod_get by the label reset won't work.
383  * And devices created by serial-multi-instantiate don't have their device struct
384  * pointing to the correct fwnode, so acpi_dev must be used here.
385  * And devm functions expect that the device requesting the resource has the correct
386  * fwnode.
387  */
388 static int lenovo_legion_no_acpi(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
389 				 const char *hid)
390 {
391 	struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
392 
393 	/* check I2C address to assign the index */
394 	cs35l41->index = id == 0x40 ? 0 : 1;
395 	cs35l41->channel_index = 0;
396 	cs35l41->reset_gpio = gpiod_get_index(physdev, NULL, 0, GPIOD_OUT_HIGH);
397 	cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, 0, 0, 2);
398 	hw_cfg->spk_pos = cs35l41->index;
399 	hw_cfg->gpio2.func = CS35L41_INTERRUPT;
400 	hw_cfg->gpio2.valid = true;
401 	hw_cfg->valid = true;
402 
403 	if (strcmp(hid, "CLSA0100") == 0) {
404 		hw_cfg->bst_type = CS35L41_EXT_BOOST_NO_VSPK_SWITCH;
405 	} else if (strcmp(hid, "CLSA0101") == 0) {
406 		hw_cfg->bst_type = CS35L41_EXT_BOOST;
407 		hw_cfg->gpio1.func = CS35l41_VSPK_SWITCH;
408 		hw_cfg->gpio1.valid = true;
409 	}
410 
411 	return 0;
412 }
413 
414 struct cs35l41_prop_model {
415 	const char *hid;
416 	const char *ssid;
417 	int (*add_prop)(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
418 			const char *hid);
419 };
420 
421 static const struct cs35l41_prop_model cs35l41_prop_model_table[] = {
422 	{ "CLSA0100", NULL, lenovo_legion_no_acpi },
423 	{ "CLSA0101", NULL, lenovo_legion_no_acpi },
424 	{ "CSC3551", "10280B27", generic_dsd_config },
425 	{ "CSC3551", "10280B28", generic_dsd_config },
426 	{ "CSC3551", "10280BEB", generic_dsd_config },
427 	{ "CSC3551", "10280C4D", generic_dsd_config },
428 	{ "CSC3551", "103C89C6", generic_dsd_config },
429 	{ "CSC3551", "103C8A28", generic_dsd_config },
430 	{ "CSC3551", "103C8A29", generic_dsd_config },
431 	{ "CSC3551", "103C8A2A", generic_dsd_config },
432 	{ "CSC3551", "103C8A2B", generic_dsd_config },
433 	{ "CSC3551", "103C8A2C", generic_dsd_config },
434 	{ "CSC3551", "103C8A2D", generic_dsd_config },
435 	{ "CSC3551", "103C8A2E", generic_dsd_config },
436 	{ "CSC3551", "103C8A30", generic_dsd_config },
437 	{ "CSC3551", "103C8A31", generic_dsd_config },
438 	{ "CSC3551", "103C8A6E", generic_dsd_config },
439 	{ "CSC3551", "103C8BB3", generic_dsd_config },
440 	{ "CSC3551", "103C8BB4", generic_dsd_config },
441 	{ "CSC3551", "103C8BDD", generic_dsd_config },
442 	{ "CSC3551", "103C8BDE", generic_dsd_config },
443 	{ "CSC3551", "103C8BDF", generic_dsd_config },
444 	{ "CSC3551", "103C8BE0", generic_dsd_config },
445 	{ "CSC3551", "103C8BE1", generic_dsd_config },
446 	{ "CSC3551", "103C8BE2", generic_dsd_config },
447 	{ "CSC3551", "103C8BE3", generic_dsd_config },
448 	{ "CSC3551", "103C8BE5", generic_dsd_config },
449 	{ "CSC3551", "103C8BE6", generic_dsd_config },
450 	{ "CSC3551", "103C8BE7", generic_dsd_config },
451 	{ "CSC3551", "103C8BE8", generic_dsd_config },
452 	{ "CSC3551", "103C8BE9", generic_dsd_config },
453 	{ "CSC3551", "103C8B3A", generic_dsd_config },
454 	{ "CSC3551", "103C8C15", generic_dsd_config },
455 	{ "CSC3551", "103C8C16", generic_dsd_config },
456 	{ "CSC3551", "103C8C17", generic_dsd_config },
457 	{ "CSC3551", "103C8C4F", generic_dsd_config },
458 	{ "CSC3551", "103C8C50", generic_dsd_config },
459 	{ "CSC3551", "103C8C51", generic_dsd_config },
460 	{ "CSC3551", "103C8C66", hp_i2c_int_2amp_dual_spkid },
461 	{ "CSC3551", "103C8C67", hp_i2c_int_2amp_dual_spkid },
462 	{ "CSC3551", "103C8C68", hp_i2c_int_2amp_dual_spkid },
463 	{ "CSC3551", "103C8C6A", hp_i2c_int_2amp_dual_spkid },
464 	{ "CSC3551", "103C8CDD", generic_dsd_config },
465 	{ "CSC3551", "103C8CDE", generic_dsd_config },
466 	{ "CSC3551", "104312AF", generic_dsd_config },
467 	{ "CSC3551", "10431433", generic_dsd_config },
468 	{ "CSC3551", "10431463", generic_dsd_config },
469 	{ "CSC3551", "10431473", generic_dsd_config },
470 	{ "CSC3551", "10431483", generic_dsd_config },
471 	{ "CSC3551", "10431493", generic_dsd_config },
472 	{ "CSC3551", "104314D3", generic_dsd_config },
473 	{ "CSC3551", "104314E3", generic_dsd_config },
474 	{ "CSC3551", "10431503", generic_dsd_config },
475 	{ "CSC3551", "10431533", generic_dsd_config },
476 	{ "CSC3551", "10431573", generic_dsd_config },
477 	{ "CSC3551", "10431663", generic_dsd_config },
478 	{ "CSC3551", "10431683", generic_dsd_config },
479 	{ "CSC3551", "104316A3", generic_dsd_config },
480 	{ "CSC3551", "104316D3", generic_dsd_config },
481 	{ "CSC3551", "104316F3", generic_dsd_config },
482 	{ "CSC3551", "104317F3", generic_dsd_config },
483 	{ "CSC3551", "10431863", generic_dsd_config },
484 	{ "CSC3551", "104318D3", generic_dsd_config },
485 	{ "CSC3551", "10431A83", generic_dsd_config },
486 	{ "CSC3551", "10431C9F", generic_dsd_config },
487 	{ "CSC3551", "10431CAF", generic_dsd_config },
488 	{ "CSC3551", "10431CCF", generic_dsd_config },
489 	{ "CSC3551", "10431CDF", generic_dsd_config },
490 	{ "CSC3551", "10431CEF", generic_dsd_config },
491 	{ "CSC3551", "10431D1F", generic_dsd_config },
492 	{ "CSC3551", "10431DA2", generic_dsd_config },
493 	{ "CSC3551", "10431E02", generic_dsd_config },
494 	{ "CSC3551", "10431E12", generic_dsd_config },
495 	{ "CSC3551", "10431EE2", generic_dsd_config },
496 	{ "CSC3551", "10431F12", generic_dsd_config },
497 	{ "CSC3551", "10431F1F", generic_dsd_config },
498 	{ "CSC3551", "10431F62", generic_dsd_config },
499 	{ "CSC3551", "17AA386F", generic_dsd_config },
500 	{ "CSC3551", "17AA38A9", generic_dsd_config },
501 	{ "CSC3551", "17AA38AB", generic_dsd_config },
502 	{ "CSC3551", "17AA38B4", generic_dsd_config },
503 	{ "CSC3551", "17AA38B5", generic_dsd_config },
504 	{ "CSC3551", "17AA38B6", generic_dsd_config },
505 	{ "CSC3551", "17AA38B7", generic_dsd_config },
506 	{}
507 };
508 
509 int cs35l41_add_dsd_properties(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
510 			       const char *hid)
511 {
512 	const struct cs35l41_prop_model *model;
513 
514 	for (model = cs35l41_prop_model_table; model->hid; model++) {
515 		if (!strcmp(model->hid, hid) &&
516 		    (!model->ssid ||
517 		     (cs35l41->acpi_subsystem_id &&
518 		      !strcasecmp(model->ssid, cs35l41->acpi_subsystem_id))))
519 			return model->add_prop(cs35l41, physdev, id, hid);
520 	}
521 
522 	return -ENOENT;
523 }
524