Linux 6.7-rc7
[linux-modified.git] / sound / pci / hda / cs35l41_hda_property.c
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         enum {
20                 SPI,
21                 I2C
22         } bus;
23         int num_amps;
24         enum {
25                 INTERNAL,
26                 EXTERNAL
27         } boost_type;
28         u8 channel[MAX_AMPS];
29         int reset_gpio_index; /* -1 if no reset gpio */
30         int spkid_gpio_index; /* -1 if no spkid gpio */
31         int cs_gpio_index; /* -1 if no cs gpio, or cs-gpios already exists, max num amps == 2 */
32         int boost_ind_nanohenry; /* Required if boost_type == Internal */
33         int boost_peak_milliamp; /* Required if boost_type == Internal */
34         int boost_cap_microfarad; /* Required if boost_type == Internal */
35 };
36
37 static const struct cs35l41_config cs35l41_config_table[] = {
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", SPI, 2, INTERNAL, { CS35L41_RIGHT, CS35L41_LEFT, 0, 0 }, -1, -1, -1, 1000, 4500, 24 },
45         { "104312AF", SPI, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
46         { "10431433", I2C, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
47         { "10431463", I2C, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
48         { "10431473", SPI, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, -1, 0, 1000, 4500, 24 },
49         { "10431483", SPI, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, -1, 0, 1000, 4500, 24 },
50         { "10431493", SPI, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
51         { "104314D3", SPI, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
52         { "104314E3", I2C, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
53         { "10431503", I2C, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
54         { "10431533", I2C, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
55         { "10431573", SPI, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
56         { "10431663", SPI, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, -1, 0, 1000, 4500, 24 },
57         { "104316D3", SPI, 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
58         { "104316F3", SPI, 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
59         { "104317F3", I2C, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
60         { "10431863", SPI, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
61         { "104318D3", I2C, 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
62         { "10431C9F", SPI, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
63         { "10431CAF", SPI, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
64         { "10431CCF", SPI, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
65         { "10431CDF", SPI, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
66         { "10431CEF", SPI, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
67         { "10431D1F", I2C, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
68         { "10431DA2", SPI, 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
69         { "10431E02", SPI, 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
70         { "10431EE2", I2C, 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, -1, -1, 0, 0, 0 },
71         { "10431F12", I2C, 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
72         { "10431F1F", SPI, 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, -1, 0, 0, 0, 0 },
73         { "10431F62", SPI, 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
74         {}
75 };
76
77 static int cs35l41_add_gpios(struct cs35l41_hda *cs35l41, struct device *physdev, int reset_gpio,
78                              int spkid_gpio, int cs_gpio_index, int num_amps)
79 {
80         struct acpi_gpio_mapping *gpio_mapping = NULL;
81         struct acpi_gpio_params *reset_gpio_params = NULL;
82         struct acpi_gpio_params *spkid_gpio_params = NULL;
83         struct acpi_gpio_params *cs_gpio_params = NULL;
84         unsigned int num_entries = 0;
85         unsigned int reset_index, spkid_index, csgpio_index;
86         int i;
87
88         /*
89          * GPIO Mapping only needs to be done once, since it would be available for subsequent amps
90          */
91         if (cs35l41->dacpi->driver_gpios)
92                 return 0;
93
94         if (reset_gpio >= 0) {
95                 reset_index = num_entries;
96                 num_entries++;
97         }
98
99         if (spkid_gpio >= 0) {
100                 spkid_index = num_entries;
101                 num_entries++;
102         }
103
104         if ((cs_gpio_index >= 0)  && (num_amps == 2)) {
105                 csgpio_index = num_entries;
106                 num_entries++;
107         }
108
109         if (!num_entries)
110                 return 0;
111
112         /* must include termination entry */
113         num_entries++;
114
115         gpio_mapping = devm_kcalloc(physdev, num_entries, sizeof(struct acpi_gpio_mapping),
116                                     GFP_KERNEL);
117
118         if (!gpio_mapping)
119                 goto err;
120
121         if (reset_gpio >= 0) {
122                 gpio_mapping[reset_index].name = "reset-gpios";
123                 reset_gpio_params = devm_kcalloc(physdev, num_amps, sizeof(struct acpi_gpio_params),
124                                                  GFP_KERNEL);
125                 if (!reset_gpio_params)
126                         goto err;
127
128                 for (i = 0; i < num_amps; i++)
129                         reset_gpio_params[i].crs_entry_index = reset_gpio;
130
131                 gpio_mapping[reset_index].data = reset_gpio_params;
132                 gpio_mapping[reset_index].size = num_amps;
133         }
134
135         if (spkid_gpio >= 0) {
136                 gpio_mapping[spkid_index].name = "spk-id-gpios";
137                 spkid_gpio_params = devm_kcalloc(physdev, num_amps, sizeof(struct acpi_gpio_params),
138                                                  GFP_KERNEL);
139                 if (!spkid_gpio_params)
140                         goto err;
141
142                 for (i = 0; i < num_amps; i++)
143                         spkid_gpio_params[i].crs_entry_index = spkid_gpio;
144
145                 gpio_mapping[spkid_index].data = spkid_gpio_params;
146                 gpio_mapping[spkid_index].size = num_amps;
147         }
148
149         if ((cs_gpio_index >= 0) && (num_amps == 2)) {
150                 gpio_mapping[csgpio_index].name = "cs-gpios";
151                 /* only one GPIO CS is supported without using _DSD, obtained using index 0 */
152                 cs_gpio_params = devm_kzalloc(physdev, sizeof(struct acpi_gpio_params), GFP_KERNEL);
153                 if (!cs_gpio_params)
154                         goto err;
155
156                 cs_gpio_params->crs_entry_index = cs_gpio_index;
157
158                 gpio_mapping[csgpio_index].data = cs_gpio_params;
159                 gpio_mapping[csgpio_index].size = 1;
160         }
161
162         return devm_acpi_dev_add_driver_gpios(physdev, gpio_mapping);
163 err:
164         devm_kfree(physdev, gpio_mapping);
165         devm_kfree(physdev, reset_gpio_params);
166         devm_kfree(physdev, spkid_gpio_params);
167         devm_kfree(physdev, cs_gpio_params);
168         return -ENOMEM;
169 }
170
171 static int generic_dsd_config(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
172                               const char *hid)
173 {
174         struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
175         const struct cs35l41_config *cfg;
176         struct gpio_desc *cs_gpiod;
177         struct spi_device *spi;
178         bool dsd_found;
179         int ret;
180
181         for (cfg = cs35l41_config_table; cfg->ssid; cfg++) {
182                 if (!strcasecmp(cfg->ssid, cs35l41->acpi_subsystem_id))
183                         break;
184         }
185
186         if (!cfg->ssid)
187                 return -ENOENT;
188
189         if (!cs35l41->dacpi || cs35l41->dacpi != ACPI_COMPANION(physdev)) {
190                 dev_err(cs35l41->dev, "ACPI Device does not match, cannot override _DSD.\n");
191                 return -ENODEV;
192         }
193
194         dev_info(cs35l41->dev, "Adding DSD properties for %s\n", cs35l41->acpi_subsystem_id);
195
196         dsd_found = acpi_dev_has_props(cs35l41->dacpi);
197
198         if (!dsd_found) {
199                 ret = cs35l41_add_gpios(cs35l41, physdev, cfg->reset_gpio_index,
200                                                  cfg->spkid_gpio_index, cfg->cs_gpio_index,
201                                                  cfg->num_amps);
202                 if (ret) {
203                         dev_err(cs35l41->dev, "Error adding GPIO mapping: %d\n", ret);
204                         return ret;
205                 }
206         } else if (cfg->reset_gpio_index >= 0 || cfg->spkid_gpio_index >= 0) {
207                 dev_warn(cs35l41->dev, "Cannot add Reset/Speaker ID/SPI CS GPIO Mapping, "
208                          "_DSD already exists.\n");
209         }
210
211         if (cfg->bus == SPI) {
212                 cs35l41->index = id;
213
214 #if IS_ENABLED(CONFIG_SPI)
215                 /*
216                  * Manually set the Chip Select for the second amp <cs_gpio_index> in the node.
217                  * This is only supported for systems with 2 amps, since we cannot expand the
218                  * default number of chip selects without using cs-gpios
219                  * The CS GPIO must be set high prior to communicating with the first amp (which
220                  * uses a native chip select), to ensure the second amp does not clash with the
221                  * first.
222                  */
223                 if (cfg->cs_gpio_index >= 0) {
224                         spi = to_spi_device(cs35l41->dev);
225
226                         if (cfg->num_amps != 2) {
227                                 dev_warn(cs35l41->dev,
228                                          "Cannot update SPI CS, Number of Amps (%d) != 2\n",
229                                          cfg->num_amps);
230                         } else if (dsd_found) {
231                                 dev_warn(cs35l41->dev,
232                                         "Cannot update SPI CS, _DSD already exists.\n");
233                         } else {
234                                 /*
235                                  * This is obtained using driver_gpios, since only one GPIO for CS
236                                  * exists, this can be obtained using index 0.
237                                  */
238                                 cs_gpiod = gpiod_get_index(physdev, "cs", 0, GPIOD_OUT_LOW);
239                                 if (IS_ERR(cs_gpiod)) {
240                                         dev_err(cs35l41->dev,
241                                                 "Unable to get Chip Select GPIO descriptor\n");
242                                         return PTR_ERR(cs_gpiod);
243                                 }
244                                 if (id == 1) {
245                                         spi_set_csgpiod(spi, 0, cs_gpiod);
246                                         cs35l41->cs_gpio = cs_gpiod;
247                                 } else {
248                                         gpiod_set_value_cansleep(cs_gpiod, true);
249                                         gpiod_put(cs_gpiod);
250                                 }
251                                 spi_setup(spi);
252                         }
253                 }
254 #endif
255         } else {
256                 if (cfg->num_amps > 2)
257                         /*
258                          * i2c addresses for 3/4 amps are used in order: 0x40, 0x41, 0x42, 0x43,
259                          * subtracting 0x40 would give zero-based index
260                          */
261                         cs35l41->index = id - 0x40;
262                 else
263                         /* i2c addr 0x40 for first amp (always), 0x41/0x42 for 2nd amp */
264                         cs35l41->index = id == 0x40 ? 0 : 1;
265         }
266
267         if (cfg->num_amps == 3)
268                 /* 3 amps means a center channel, so no duplicate channels */
269                 cs35l41->channel_index = 0;
270         else
271                 /*
272                  * if 4 amps, there are duplicate channels, so they need different indexes
273                  * if 2 amps, no duplicate channels, channel_index would be 0
274                  */
275                 cs35l41->channel_index = cs35l41->index / 2;
276
277         cs35l41->reset_gpio = fwnode_gpiod_get_index(acpi_fwnode_handle(cs35l41->dacpi), "reset",
278                                                      cs35l41->index, GPIOD_OUT_LOW,
279                                                      "cs35l41-reset");
280         cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, cs35l41->index, cfg->num_amps, -1);
281
282         hw_cfg->spk_pos = cfg->channel[cs35l41->index];
283
284         if (cfg->boost_type == INTERNAL) {
285                 hw_cfg->bst_type = CS35L41_INT_BOOST;
286                 hw_cfg->bst_ind = cfg->boost_ind_nanohenry;
287                 hw_cfg->bst_ipk = cfg->boost_peak_milliamp;
288                 hw_cfg->bst_cap = cfg->boost_cap_microfarad;
289                 hw_cfg->gpio1.func = CS35L41_NOT_USED;
290                 hw_cfg->gpio1.valid = true;
291         } else {
292                 hw_cfg->bst_type = CS35L41_EXT_BOOST;
293                 hw_cfg->bst_ind = -1;
294                 hw_cfg->bst_ipk = -1;
295                 hw_cfg->bst_cap = -1;
296                 hw_cfg->gpio1.func = CS35l41_VSPK_SWITCH;
297                 hw_cfg->gpio1.valid = true;
298         }
299
300         hw_cfg->gpio2.func = CS35L41_INTERRUPT;
301         hw_cfg->gpio2.valid = true;
302         hw_cfg->valid = true;
303
304         return 0;
305 }
306
307 /*
308  * Device CLSA010(0/1) doesn't have _DSD so a gpiod_get by the label reset won't work.
309  * And devices created by serial-multi-instantiate don't have their device struct
310  * pointing to the correct fwnode, so acpi_dev must be used here.
311  * And devm functions expect that the device requesting the resource has the correct
312  * fwnode.
313  */
314 static int lenovo_legion_no_acpi(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
315                                  const char *hid)
316 {
317         struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
318
319         /* check I2C address to assign the index */
320         cs35l41->index = id == 0x40 ? 0 : 1;
321         cs35l41->channel_index = 0;
322         cs35l41->reset_gpio = gpiod_get_index(physdev, NULL, 0, GPIOD_OUT_HIGH);
323         cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, 0, 0, 2);
324         hw_cfg->spk_pos = cs35l41->index;
325         hw_cfg->gpio2.func = CS35L41_INTERRUPT;
326         hw_cfg->gpio2.valid = true;
327         hw_cfg->valid = true;
328
329         if (strcmp(hid, "CLSA0100") == 0) {
330                 hw_cfg->bst_type = CS35L41_EXT_BOOST_NO_VSPK_SWITCH;
331         } else if (strcmp(hid, "CLSA0101") == 0) {
332                 hw_cfg->bst_type = CS35L41_EXT_BOOST;
333                 hw_cfg->gpio1.func = CS35l41_VSPK_SWITCH;
334                 hw_cfg->gpio1.valid = true;
335         }
336
337         return 0;
338 }
339
340 struct cs35l41_prop_model {
341         const char *hid;
342         const char *ssid;
343         int (*add_prop)(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
344                         const char *hid);
345 };
346
347 static const struct cs35l41_prop_model cs35l41_prop_model_table[] = {
348         { "CLSA0100", NULL, lenovo_legion_no_acpi },
349         { "CLSA0101", NULL, lenovo_legion_no_acpi },
350         { "CSC3551", "103C89C6", generic_dsd_config },
351         { "CSC3551", "104312AF", generic_dsd_config },
352         { "CSC3551", "10431433", generic_dsd_config },
353         { "CSC3551", "10431463", generic_dsd_config },
354         { "CSC3551", "10431473", generic_dsd_config },
355         { "CSC3551", "10431483", generic_dsd_config },
356         { "CSC3551", "10431493", generic_dsd_config },
357         { "CSC3551", "104314D3", generic_dsd_config },
358         { "CSC3551", "104314E3", generic_dsd_config },
359         { "CSC3551", "10431503", generic_dsd_config },
360         { "CSC3551", "10431533", generic_dsd_config },
361         { "CSC3551", "10431573", generic_dsd_config },
362         { "CSC3551", "10431663", generic_dsd_config },
363         { "CSC3551", "104316D3", generic_dsd_config },
364         { "CSC3551", "104316F3", generic_dsd_config },
365         { "CSC3551", "104317F3", generic_dsd_config },
366         { "CSC3551", "10431863", generic_dsd_config },
367         { "CSC3551", "104318D3", generic_dsd_config },
368         { "CSC3551", "10431C9F", generic_dsd_config },
369         { "CSC3551", "10431CAF", generic_dsd_config },
370         { "CSC3551", "10431CCF", generic_dsd_config },
371         { "CSC3551", "10431CDF", generic_dsd_config },
372         { "CSC3551", "10431CEF", generic_dsd_config },
373         { "CSC3551", "10431D1F", generic_dsd_config },
374         { "CSC3551", "10431DA2", generic_dsd_config },
375         { "CSC3551", "10431E02", generic_dsd_config },
376         { "CSC3551", "10431EE2", generic_dsd_config },
377         { "CSC3551", "10431F12", generic_dsd_config },
378         { "CSC3551", "10431F1F", generic_dsd_config },
379         { "CSC3551", "10431F62", generic_dsd_config },
380         {}
381 };
382
383 int cs35l41_add_dsd_properties(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
384                                const char *hid)
385 {
386         const struct cs35l41_prop_model *model;
387
388         for (model = cs35l41_prop_model_table; model->hid; model++) {
389                 if (!strcmp(model->hid, hid) &&
390                     (!model->ssid ||
391                      (cs35l41->acpi_subsystem_id &&
392                       !strcasecmp(model->ssid, cs35l41->acpi_subsystem_id))))
393                         return model->add_prop(cs35l41, physdev, id, hid);
394         }
395
396         return -ENOENT;
397 }