1ef4ba63fSStefan Binding // SPDX-License-Identifier: GPL-2.0 2ef4ba63fSStefan Binding // 3ef4ba63fSStefan Binding // CS35L41 ALSA HDA Property driver 4ef4ba63fSStefan Binding // 5ef4ba63fSStefan Binding // Copyright 2023 Cirrus Logic, Inc. 6ef4ba63fSStefan Binding // 7ef4ba63fSStefan Binding // Author: Stefan Binding <sbinding@opensource.cirrus.com> 8ef4ba63fSStefan Binding 9*8c4c216dSStefan Binding #include <linux/acpi.h> 10ef4ba63fSStefan Binding #include <linux/gpio/consumer.h> 11ef4ba63fSStefan Binding #include <linux/string.h> 12ef4ba63fSStefan Binding #include "cs35l41_hda_property.h" 13*8c4c216dSStefan Binding #include <linux/spi/spi.h> 14*8c4c216dSStefan Binding 15*8c4c216dSStefan Binding #define MAX_AMPS 4 16*8c4c216dSStefan Binding 17*8c4c216dSStefan Binding struct cs35l41_config { 18*8c4c216dSStefan Binding const char *ssid; 19*8c4c216dSStefan Binding enum { 20*8c4c216dSStefan Binding SPI, 21*8c4c216dSStefan Binding I2C 22*8c4c216dSStefan Binding } bus; 23*8c4c216dSStefan Binding int num_amps; 24*8c4c216dSStefan Binding enum { 25*8c4c216dSStefan Binding INTERNAL, 26*8c4c216dSStefan Binding EXTERNAL 27*8c4c216dSStefan Binding } boost_type; 28*8c4c216dSStefan Binding u8 channel[MAX_AMPS]; 29*8c4c216dSStefan Binding int reset_gpio_index; /* -1 if no reset gpio */ 30*8c4c216dSStefan Binding int spkid_gpio_index; /* -1 if no spkid gpio */ 31*8c4c216dSStefan Binding int cs_gpio_index; /* -1 if no cs gpio, or cs-gpios already exists, max num amps == 2 */ 32*8c4c216dSStefan Binding int boost_ind_nanohenry; /* Required if boost_type == Internal */ 33*8c4c216dSStefan Binding int boost_peak_milliamp; /* Required if boost_type == Internal */ 34*8c4c216dSStefan Binding int boost_cap_microfarad; /* Required if boost_type == Internal */ 35*8c4c216dSStefan Binding }; 36*8c4c216dSStefan Binding 37*8c4c216dSStefan Binding static const struct cs35l41_config cs35l41_config_table[] = { 38*8c4c216dSStefan Binding /* 39*8c4c216dSStefan Binding * Device 103C89C6 does have _DSD, however it is setup to use the wrong boost type. 40*8c4c216dSStefan Binding * We can override the _DSD to correct the boost type here. 41*8c4c216dSStefan Binding * Since this laptop has valid ACPI, we do not need to handle cs-gpios, since that already exists 42*8c4c216dSStefan Binding * in the ACPI. The Reset GPIO is also valid, so we can use the Reset defined in _DSD. 43*8c4c216dSStefan Binding */ 44*8c4c216dSStefan Binding { "103C89C6", SPI, 2, INTERNAL, { CS35L41_RIGHT, CS35L41_LEFT, 0, 0 }, -1, -1, -1, 1000, 4500, 24 }, 45*8c4c216dSStefan Binding {} 46*8c4c216dSStefan Binding }; 47*8c4c216dSStefan Binding 48*8c4c216dSStefan Binding static int cs35l41_add_gpios(struct cs35l41_hda *cs35l41, struct device *physdev, int reset_gpio, 49*8c4c216dSStefan Binding int spkid_gpio, int cs_gpio_index, int num_amps) 50*8c4c216dSStefan Binding { 51*8c4c216dSStefan Binding struct acpi_gpio_mapping *gpio_mapping; 52*8c4c216dSStefan Binding struct acpi_gpio_params *reset_gpio_params; 53*8c4c216dSStefan Binding struct acpi_gpio_params *spkid_gpio_params; 54*8c4c216dSStefan Binding struct acpi_gpio_params *cs_gpio_params; 55*8c4c216dSStefan Binding unsigned int num_entries = 0; 56*8c4c216dSStefan Binding unsigned int reset_index, spkid_index, csgpio_index; 57*8c4c216dSStefan Binding int i; 58*8c4c216dSStefan Binding 59*8c4c216dSStefan Binding /* 60*8c4c216dSStefan Binding * GPIO Mapping only needs to be done once, since it would be available for subsequent amps 61*8c4c216dSStefan Binding */ 62*8c4c216dSStefan Binding if (cs35l41->dacpi->driver_gpios) 63*8c4c216dSStefan Binding return 0; 64*8c4c216dSStefan Binding 65*8c4c216dSStefan Binding if (reset_gpio >= 0) { 66*8c4c216dSStefan Binding reset_index = num_entries; 67*8c4c216dSStefan Binding num_entries++; 68*8c4c216dSStefan Binding } 69*8c4c216dSStefan Binding 70*8c4c216dSStefan Binding if (spkid_gpio >= 0) { 71*8c4c216dSStefan Binding spkid_index = num_entries; 72*8c4c216dSStefan Binding num_entries++; 73*8c4c216dSStefan Binding } 74*8c4c216dSStefan Binding 75*8c4c216dSStefan Binding if ((cs_gpio_index >= 0) && (num_amps == 2)) { 76*8c4c216dSStefan Binding csgpio_index = num_entries; 77*8c4c216dSStefan Binding num_entries++; 78*8c4c216dSStefan Binding } 79*8c4c216dSStefan Binding 80*8c4c216dSStefan Binding if (!num_entries) 81*8c4c216dSStefan Binding return 0; 82*8c4c216dSStefan Binding 83*8c4c216dSStefan Binding /* must include termination entry */ 84*8c4c216dSStefan Binding num_entries++; 85*8c4c216dSStefan Binding 86*8c4c216dSStefan Binding gpio_mapping = devm_kcalloc(physdev, num_entries, sizeof(struct acpi_gpio_mapping), 87*8c4c216dSStefan Binding GFP_KERNEL); 88*8c4c216dSStefan Binding 89*8c4c216dSStefan Binding if (!gpio_mapping) 90*8c4c216dSStefan Binding goto err; 91*8c4c216dSStefan Binding 92*8c4c216dSStefan Binding if (reset_gpio >= 0) { 93*8c4c216dSStefan Binding gpio_mapping[reset_index].name = "reset-gpios"; 94*8c4c216dSStefan Binding reset_gpio_params = devm_kcalloc(physdev, num_amps, sizeof(struct acpi_gpio_params), 95*8c4c216dSStefan Binding GFP_KERNEL); 96*8c4c216dSStefan Binding if (!reset_gpio_params) 97*8c4c216dSStefan Binding goto err; 98*8c4c216dSStefan Binding 99*8c4c216dSStefan Binding for (i = 0; i < num_amps; i++) 100*8c4c216dSStefan Binding reset_gpio_params[i].crs_entry_index = reset_gpio; 101*8c4c216dSStefan Binding 102*8c4c216dSStefan Binding gpio_mapping[reset_index].data = reset_gpio_params; 103*8c4c216dSStefan Binding gpio_mapping[reset_index].size = num_amps; 104*8c4c216dSStefan Binding } 105*8c4c216dSStefan Binding 106*8c4c216dSStefan Binding if (spkid_gpio >= 0) { 107*8c4c216dSStefan Binding gpio_mapping[spkid_index].name = "spk-id-gpios"; 108*8c4c216dSStefan Binding spkid_gpio_params = devm_kcalloc(physdev, num_amps, sizeof(struct acpi_gpio_params), 109*8c4c216dSStefan Binding GFP_KERNEL); 110*8c4c216dSStefan Binding if (!spkid_gpio_params) 111*8c4c216dSStefan Binding goto err; 112*8c4c216dSStefan Binding 113*8c4c216dSStefan Binding for (i = 0; i < num_amps; i++) 114*8c4c216dSStefan Binding spkid_gpio_params[i].crs_entry_index = spkid_gpio; 115*8c4c216dSStefan Binding 116*8c4c216dSStefan Binding gpio_mapping[spkid_index].data = spkid_gpio_params; 117*8c4c216dSStefan Binding gpio_mapping[spkid_index].size = num_amps; 118*8c4c216dSStefan Binding } 119*8c4c216dSStefan Binding 120*8c4c216dSStefan Binding if ((cs_gpio_index >= 0) && (num_amps == 2)) { 121*8c4c216dSStefan Binding gpio_mapping[csgpio_index].name = "cs-gpios"; 122*8c4c216dSStefan Binding /* only one GPIO CS is supported without using _DSD, obtained using index 0 */ 123*8c4c216dSStefan Binding cs_gpio_params = devm_kzalloc(physdev, sizeof(struct acpi_gpio_params), GFP_KERNEL); 124*8c4c216dSStefan Binding if (!cs_gpio_params) 125*8c4c216dSStefan Binding goto err; 126*8c4c216dSStefan Binding 127*8c4c216dSStefan Binding cs_gpio_params->crs_entry_index = cs_gpio_index; 128*8c4c216dSStefan Binding 129*8c4c216dSStefan Binding gpio_mapping[csgpio_index].data = cs_gpio_params; 130*8c4c216dSStefan Binding gpio_mapping[csgpio_index].size = 1; 131*8c4c216dSStefan Binding } 132*8c4c216dSStefan Binding 133*8c4c216dSStefan Binding return devm_acpi_dev_add_driver_gpios(physdev, gpio_mapping); 134*8c4c216dSStefan Binding err: 135*8c4c216dSStefan Binding devm_kfree(physdev, gpio_mapping); 136*8c4c216dSStefan Binding devm_kfree(physdev, reset_gpio_params); 137*8c4c216dSStefan Binding devm_kfree(physdev, spkid_gpio_params); 138*8c4c216dSStefan Binding devm_kfree(physdev, cs_gpio_params); 139*8c4c216dSStefan Binding return -ENOMEM; 140*8c4c216dSStefan Binding } 141*8c4c216dSStefan Binding 142*8c4c216dSStefan Binding static int generic_dsd_config(struct cs35l41_hda *cs35l41, struct device *physdev, int id, 143*8c4c216dSStefan Binding const char *hid) 144*8c4c216dSStefan Binding { 145*8c4c216dSStefan Binding struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg; 146*8c4c216dSStefan Binding const struct cs35l41_config *cfg; 147*8c4c216dSStefan Binding struct gpio_desc *cs_gpiod; 148*8c4c216dSStefan Binding struct spi_device *spi; 149*8c4c216dSStefan Binding bool dsd_found; 150*8c4c216dSStefan Binding int ret; 151*8c4c216dSStefan Binding 152*8c4c216dSStefan Binding for (cfg = cs35l41_config_table; cfg->ssid; cfg++) { 153*8c4c216dSStefan Binding if (!strcasecmp(cfg->ssid, cs35l41->acpi_subsystem_id)) 154*8c4c216dSStefan Binding break; 155*8c4c216dSStefan Binding } 156*8c4c216dSStefan Binding 157*8c4c216dSStefan Binding if (!cfg->ssid) 158*8c4c216dSStefan Binding return -ENOENT; 159*8c4c216dSStefan Binding 160*8c4c216dSStefan Binding if (!cs35l41->dacpi || cs35l41->dacpi != ACPI_COMPANION(physdev)) { 161*8c4c216dSStefan Binding dev_err(cs35l41->dev, "ACPI Device does not match, cannot override _DSD.\n"); 162*8c4c216dSStefan Binding return -ENODEV; 163*8c4c216dSStefan Binding } 164*8c4c216dSStefan Binding 165*8c4c216dSStefan Binding dev_info(cs35l41->dev, "Adding DSD properties for %s\n", cs35l41->acpi_subsystem_id); 166*8c4c216dSStefan Binding 167*8c4c216dSStefan Binding dsd_found = acpi_dev_has_props(cs35l41->dacpi); 168*8c4c216dSStefan Binding 169*8c4c216dSStefan Binding if (!dsd_found) { 170*8c4c216dSStefan Binding ret = cs35l41_add_gpios(cs35l41, physdev, cfg->reset_gpio_index, 171*8c4c216dSStefan Binding cfg->spkid_gpio_index, cfg->cs_gpio_index, 172*8c4c216dSStefan Binding cfg->num_amps); 173*8c4c216dSStefan Binding if (ret) { 174*8c4c216dSStefan Binding dev_err(cs35l41->dev, "Error adding GPIO mapping: %d\n", ret); 175*8c4c216dSStefan Binding return ret; 176*8c4c216dSStefan Binding } 177*8c4c216dSStefan Binding } else if (cfg->reset_gpio_index >= 0 || cfg->spkid_gpio_index >= 0) { 178*8c4c216dSStefan Binding dev_warn(cs35l41->dev, "Cannot add Reset/Speaker ID/SPI CS GPIO Mapping, " 179*8c4c216dSStefan Binding "_DSD already exists.\n"); 180*8c4c216dSStefan Binding } 181*8c4c216dSStefan Binding 182*8c4c216dSStefan Binding if (cfg->bus == SPI) { 183*8c4c216dSStefan Binding cs35l41->index = id; 184*8c4c216dSStefan Binding /* 185*8c4c216dSStefan Binding * Manually set the Chip Select for the second amp <cs_gpio_index> in the node. 186*8c4c216dSStefan Binding * This is only supported for systems with 2 amps, since we cannot expand the 187*8c4c216dSStefan Binding * default number of chip selects without using cs-gpios 188*8c4c216dSStefan Binding * The CS GPIO must be set high prior to communicating with the first amp (which 189*8c4c216dSStefan Binding * uses a native chip select), to ensure the second amp does not clash with the 190*8c4c216dSStefan Binding * first. 191*8c4c216dSStefan Binding */ 192*8c4c216dSStefan Binding if (cfg->cs_gpio_index >= 0) { 193*8c4c216dSStefan Binding spi = to_spi_device(cs35l41->dev); 194*8c4c216dSStefan Binding 195*8c4c216dSStefan Binding if (cfg->num_amps != 2) { 196*8c4c216dSStefan Binding dev_warn(cs35l41->dev, 197*8c4c216dSStefan Binding "Cannot update SPI CS, Number of Amps (%d) != 2\n", 198*8c4c216dSStefan Binding cfg->num_amps); 199*8c4c216dSStefan Binding } else if (dsd_found) { 200*8c4c216dSStefan Binding dev_warn(cs35l41->dev, 201*8c4c216dSStefan Binding "Cannot update SPI CS, _DSD already exists.\n"); 202*8c4c216dSStefan Binding } else { 203*8c4c216dSStefan Binding /* 204*8c4c216dSStefan Binding * This is obtained using driver_gpios, since only one GPIO for CS 205*8c4c216dSStefan Binding * exists, this can be obtained using index 0. 206*8c4c216dSStefan Binding */ 207*8c4c216dSStefan Binding cs_gpiod = gpiod_get_index(physdev, "cs", 0, GPIOD_OUT_LOW); 208*8c4c216dSStefan Binding if (IS_ERR(cs_gpiod)) { 209*8c4c216dSStefan Binding dev_err(cs35l41->dev, 210*8c4c216dSStefan Binding "Unable to get Chip Select GPIO descriptor\n"); 211*8c4c216dSStefan Binding return PTR_ERR(cs_gpiod); 212*8c4c216dSStefan Binding } 213*8c4c216dSStefan Binding if (id == 1) { 214*8c4c216dSStefan Binding spi_set_csgpiod(spi, 0, cs_gpiod); 215*8c4c216dSStefan Binding cs35l41->cs_gpio = cs_gpiod; 216*8c4c216dSStefan Binding } else { 217*8c4c216dSStefan Binding gpiod_set_value_cansleep(cs_gpiod, true); 218*8c4c216dSStefan Binding gpiod_put(cs_gpiod); 219*8c4c216dSStefan Binding } 220*8c4c216dSStefan Binding spi_setup(spi); 221*8c4c216dSStefan Binding } 222*8c4c216dSStefan Binding } 223*8c4c216dSStefan Binding } else { 224*8c4c216dSStefan Binding if (cfg->num_amps > 2) 225*8c4c216dSStefan Binding /* 226*8c4c216dSStefan Binding * i2c addresses for 3/4 amps are used in order: 0x40, 0x41, 0x42, 0x43, 227*8c4c216dSStefan Binding * subtracting 0x40 would give zero-based index 228*8c4c216dSStefan Binding */ 229*8c4c216dSStefan Binding cs35l41->index = id - 0x40; 230*8c4c216dSStefan Binding else 231*8c4c216dSStefan Binding /* i2c addr 0x40 for first amp (always), 0x41/0x42 for 2nd amp */ 232*8c4c216dSStefan Binding cs35l41->index = id == 0x40 ? 0 : 1; 233*8c4c216dSStefan Binding } 234*8c4c216dSStefan Binding 235*8c4c216dSStefan Binding if (cfg->num_amps == 3) 236*8c4c216dSStefan Binding /* 3 amps means a center channel, so no duplicate channels */ 237*8c4c216dSStefan Binding cs35l41->channel_index = 0; 238*8c4c216dSStefan Binding else 239*8c4c216dSStefan Binding /* 240*8c4c216dSStefan Binding * if 4 amps, there are duplicate channels, so they need different indexes 241*8c4c216dSStefan Binding * if 2 amps, no duplicate channels, channel_index would be 0 242*8c4c216dSStefan Binding */ 243*8c4c216dSStefan Binding cs35l41->channel_index = cs35l41->index / 2; 244*8c4c216dSStefan Binding 245*8c4c216dSStefan Binding cs35l41->reset_gpio = fwnode_gpiod_get_index(acpi_fwnode_handle(cs35l41->dacpi), "reset", 246*8c4c216dSStefan Binding cs35l41->index, GPIOD_OUT_LOW, 247*8c4c216dSStefan Binding "cs35l41-reset"); 248*8c4c216dSStefan Binding cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, cs35l41->index, cfg->num_amps, -1); 249*8c4c216dSStefan Binding 250*8c4c216dSStefan Binding hw_cfg->spk_pos = cfg->channel[cs35l41->index]; 251*8c4c216dSStefan Binding 252*8c4c216dSStefan Binding if (cfg->boost_type == INTERNAL) { 253*8c4c216dSStefan Binding hw_cfg->bst_type = CS35L41_INT_BOOST; 254*8c4c216dSStefan Binding hw_cfg->bst_ind = cfg->boost_ind_nanohenry; 255*8c4c216dSStefan Binding hw_cfg->bst_ipk = cfg->boost_peak_milliamp; 256*8c4c216dSStefan Binding hw_cfg->bst_cap = cfg->boost_cap_microfarad; 257*8c4c216dSStefan Binding hw_cfg->gpio1.func = CS35L41_NOT_USED; 258*8c4c216dSStefan Binding hw_cfg->gpio1.valid = true; 259*8c4c216dSStefan Binding } else { 260*8c4c216dSStefan Binding hw_cfg->bst_type = CS35L41_EXT_BOOST; 261*8c4c216dSStefan Binding hw_cfg->bst_ind = -1; 262*8c4c216dSStefan Binding hw_cfg->bst_ipk = -1; 263*8c4c216dSStefan Binding hw_cfg->bst_cap = -1; 264*8c4c216dSStefan Binding hw_cfg->gpio1.func = CS35l41_VSPK_SWITCH; 265*8c4c216dSStefan Binding hw_cfg->gpio1.valid = true; 266*8c4c216dSStefan Binding } 267*8c4c216dSStefan Binding 268*8c4c216dSStefan Binding hw_cfg->gpio2.func = CS35L41_INTERRUPT; 269*8c4c216dSStefan Binding hw_cfg->gpio2.valid = true; 270*8c4c216dSStefan Binding hw_cfg->valid = true; 271*8c4c216dSStefan Binding 272*8c4c216dSStefan Binding return 0; 273*8c4c216dSStefan Binding } 274ef4ba63fSStefan Binding 275ef4ba63fSStefan Binding /* 276ef4ba63fSStefan Binding * Device CLSA010(0/1) doesn't have _DSD so a gpiod_get by the label reset won't work. 277ef4ba63fSStefan Binding * And devices created by serial-multi-instantiate don't have their device struct 278ef4ba63fSStefan Binding * pointing to the correct fwnode, so acpi_dev must be used here. 279ef4ba63fSStefan Binding * And devm functions expect that the device requesting the resource has the correct 280ef4ba63fSStefan Binding * fwnode. 281ef4ba63fSStefan Binding */ 282ef4ba63fSStefan Binding static int lenovo_legion_no_acpi(struct cs35l41_hda *cs35l41, struct device *physdev, int id, 283ef4ba63fSStefan Binding const char *hid) 284ef4ba63fSStefan Binding { 285ef4ba63fSStefan Binding struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg; 286ef4ba63fSStefan Binding 287ef4ba63fSStefan Binding /* check I2C address to assign the index */ 288ef4ba63fSStefan Binding cs35l41->index = id == 0x40 ? 0 : 1; 289ef4ba63fSStefan Binding cs35l41->channel_index = 0; 290ef4ba63fSStefan Binding cs35l41->reset_gpio = gpiod_get_index(physdev, NULL, 0, GPIOD_OUT_HIGH); 291ef4ba63fSStefan Binding cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, 0, 0, 2); 292ef4ba63fSStefan Binding hw_cfg->spk_pos = cs35l41->index; 293ef4ba63fSStefan Binding hw_cfg->gpio2.func = CS35L41_INTERRUPT; 294ef4ba63fSStefan Binding hw_cfg->gpio2.valid = true; 295ef4ba63fSStefan Binding hw_cfg->valid = true; 296ef4ba63fSStefan Binding 297ef4ba63fSStefan Binding if (strcmp(hid, "CLSA0100") == 0) { 298ef4ba63fSStefan Binding hw_cfg->bst_type = CS35L41_EXT_BOOST_NO_VSPK_SWITCH; 299ef4ba63fSStefan Binding } else if (strcmp(hid, "CLSA0101") == 0) { 300ef4ba63fSStefan Binding hw_cfg->bst_type = CS35L41_EXT_BOOST; 301ef4ba63fSStefan Binding hw_cfg->gpio1.func = CS35l41_VSPK_SWITCH; 302ef4ba63fSStefan Binding hw_cfg->gpio1.valid = true; 303ef4ba63fSStefan Binding } 304ef4ba63fSStefan Binding 305ef4ba63fSStefan Binding return 0; 306ef4ba63fSStefan Binding } 307ef4ba63fSStefan Binding 308ef4ba63fSStefan Binding struct cs35l41_prop_model { 309ef4ba63fSStefan Binding const char *hid; 310ef4ba63fSStefan Binding const char *ssid; 311ef4ba63fSStefan Binding int (*add_prop)(struct cs35l41_hda *cs35l41, struct device *physdev, int id, 312ef4ba63fSStefan Binding const char *hid); 313ef4ba63fSStefan Binding }; 314ef4ba63fSStefan Binding 3152e6f9790SYang Yingliang static const struct cs35l41_prop_model cs35l41_prop_model_table[] = { 316ef4ba63fSStefan Binding { "CLSA0100", NULL, lenovo_legion_no_acpi }, 317ef4ba63fSStefan Binding { "CLSA0101", NULL, lenovo_legion_no_acpi }, 318*8c4c216dSStefan Binding { "CSC3551", "103C89C6", generic_dsd_config }, 319ef4ba63fSStefan Binding {} 320ef4ba63fSStefan Binding }; 321ef4ba63fSStefan Binding 322ef4ba63fSStefan Binding int cs35l41_add_dsd_properties(struct cs35l41_hda *cs35l41, struct device *physdev, int id, 323ef4ba63fSStefan Binding const char *hid) 324ef4ba63fSStefan Binding { 325ef4ba63fSStefan Binding const struct cs35l41_prop_model *model; 326ef4ba63fSStefan Binding 32740989679STakashi Iwai for (model = cs35l41_prop_model_table; model->hid; model++) { 328ef4ba63fSStefan Binding if (!strcmp(model->hid, hid) && 329ef4ba63fSStefan Binding (!model->ssid || 330ef4ba63fSStefan Binding (cs35l41->acpi_subsystem_id && 331*8c4c216dSStefan Binding !strcasecmp(model->ssid, cs35l41->acpi_subsystem_id)))) 332ef4ba63fSStefan Binding return model->add_prop(cs35l41, physdev, id, hid); 333ef4ba63fSStefan Binding } 334ef4ba63fSStefan Binding 335ef4ba63fSStefan Binding return -ENOENT; 336ef4ba63fSStefan Binding } 337