xref: /linux/sound/pci/hda/cs35l41_hda_property.c (revision 79997eda0d31bc68203c95ecb978773ee6ce7a1f)
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/gpio/consumer.h>
10 #include <linux/string.h>
11 #include "cs35l41_hda_property.h"
12 
13 /*
14  * Device CLSA010(0/1) doesn't have _DSD so a gpiod_get by the label reset won't work.
15  * And devices created by serial-multi-instantiate don't have their device struct
16  * pointing to the correct fwnode, so acpi_dev must be used here.
17  * And devm functions expect that the device requesting the resource has the correct
18  * fwnode.
19  */
20 static int lenovo_legion_no_acpi(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
21 				 const char *hid)
22 {
23 	struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
24 
25 	/* check I2C address to assign the index */
26 	cs35l41->index = id == 0x40 ? 0 : 1;
27 	cs35l41->channel_index = 0;
28 	cs35l41->reset_gpio = gpiod_get_index(physdev, NULL, 0, GPIOD_OUT_HIGH);
29 	cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, 0, 0, 2);
30 	hw_cfg->spk_pos = cs35l41->index;
31 	hw_cfg->gpio2.func = CS35L41_INTERRUPT;
32 	hw_cfg->gpio2.valid = true;
33 	hw_cfg->valid = true;
34 
35 	if (strcmp(hid, "CLSA0100") == 0) {
36 		hw_cfg->bst_type = CS35L41_EXT_BOOST_NO_VSPK_SWITCH;
37 	} else if (strcmp(hid, "CLSA0101") == 0) {
38 		hw_cfg->bst_type = CS35L41_EXT_BOOST;
39 		hw_cfg->gpio1.func = CS35l41_VSPK_SWITCH;
40 		hw_cfg->gpio1.valid = true;
41 	}
42 
43 	return 0;
44 }
45 
46 /*
47  * Device 103C89C6 does have _DSD, however it is setup to use the wrong boost type.
48  * We can override the _DSD to correct the boost type here.
49  * Since this laptop has valid ACPI, we do not need to handle cs-gpios, since that already exists
50  * in the ACPI.
51  */
52 static int hp_vision_acpi_fix(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
53 			      const char *hid)
54 {
55 	struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
56 
57 	dev_info(cs35l41->dev, "Adding DSD properties for %s\n", cs35l41->acpi_subsystem_id);
58 
59 	cs35l41->index = id;
60 	cs35l41->channel_index = 0;
61 
62 	/*
63 	 * This system has _DSD, it just contains an error, so we can still get the reset using
64 	 * the "reset" label.
65 	 */
66 	cs35l41->reset_gpio = fwnode_gpiod_get_index(acpi_fwnode_handle(cs35l41->dacpi), "reset",
67 						     cs35l41->index, GPIOD_OUT_LOW,
68 						     "cs35l41-reset");
69 	cs35l41->speaker_id = -ENOENT;
70 	hw_cfg->spk_pos = cs35l41->index ? 0 : 1; // right:left
71 	hw_cfg->gpio1.func = CS35L41_NOT_USED;
72 	hw_cfg->gpio1.valid = true;
73 	hw_cfg->gpio2.func = CS35L41_INTERRUPT;
74 	hw_cfg->gpio2.valid = true;
75 	hw_cfg->bst_type = CS35L41_INT_BOOST;
76 	hw_cfg->bst_ind = 1000;
77 	hw_cfg->bst_ipk = 4500;
78 	hw_cfg->bst_cap = 24;
79 	hw_cfg->valid = true;
80 
81 	return 0;
82 }
83 
84 struct cs35l41_prop_model {
85 	const char *hid;
86 	const char *ssid;
87 	int (*add_prop)(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
88 			const char *hid);
89 };
90 
91 static const struct cs35l41_prop_model cs35l41_prop_model_table[] = {
92 	{ "CLSA0100", NULL, lenovo_legion_no_acpi },
93 	{ "CLSA0101", NULL, lenovo_legion_no_acpi },
94 	{ "CSC3551", "103C89C6", hp_vision_acpi_fix },
95 	{}
96 };
97 
98 int cs35l41_add_dsd_properties(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
99 			       const char *hid)
100 {
101 	const struct cs35l41_prop_model *model;
102 
103 	for (model = cs35l41_prop_model_table; model->hid; model++) {
104 		if (!strcmp(model->hid, hid) &&
105 		    (!model->ssid ||
106 		     (cs35l41->acpi_subsystem_id &&
107 		      !strcmp(model->ssid, cs35l41->acpi_subsystem_id))))
108 			return model->add_prop(cs35l41, physdev, id, hid);
109 	}
110 
111 	return -ENOENT;
112 }
113