| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * cs42l51.c |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 7 | 8 | * |
|---|
| 8 | 9 | * Based on cs4270.c - Copyright (c) Freescale Semiconductor |
|---|
| 9 | 10 | * |
|---|
| 10 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 11 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 12 | | - * published by the Free Software Foundation. |
|---|
| 13 | | - * |
|---|
| 14 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 15 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | | - * GNU General Public License for more details. |
|---|
| 18 | | - * |
|---|
| 19 | 11 | * For now: |
|---|
| 20 | 12 | * - Only I2C is support. Not SPI |
|---|
| 21 | 13 | * - master mode *NOT* supported |
|---|
| 22 | 14 | */ |
|---|
| 23 | 15 | |
|---|
| 16 | +#include <linux/clk.h> |
|---|
| 24 | 17 | #include <linux/module.h> |
|---|
| 25 | 18 | #include <linux/slab.h> |
|---|
| 26 | 19 | #include <sound/core.h> |
|---|
| .. | .. |
|---|
| 29 | 22 | #include <sound/initval.h> |
|---|
| 30 | 23 | #include <sound/pcm_params.h> |
|---|
| 31 | 24 | #include <sound/pcm.h> |
|---|
| 25 | +#include <linux/gpio/consumer.h> |
|---|
| 32 | 26 | #include <linux/regmap.h> |
|---|
| 27 | +#include <linux/regulator/consumer.h> |
|---|
| 33 | 28 | |
|---|
| 34 | 29 | #include "cs42l51.h" |
|---|
| 35 | 30 | |
|---|
| .. | .. |
|---|
| 39 | 34 | MODE_MASTER, |
|---|
| 40 | 35 | }; |
|---|
| 41 | 36 | |
|---|
| 37 | +static const char * const cs42l51_supply_names[] = { |
|---|
| 38 | + "VL", |
|---|
| 39 | + "VD", |
|---|
| 40 | + "VA", |
|---|
| 41 | + "VAHP", |
|---|
| 42 | +}; |
|---|
| 43 | + |
|---|
| 42 | 44 | struct cs42l51_private { |
|---|
| 43 | 45 | unsigned int mclk; |
|---|
| 46 | + struct clk *mclk_handle; |
|---|
| 44 | 47 | unsigned int audio_mode; /* The mode (I2S or left-justified) */ |
|---|
| 45 | 48 | enum master_slave_mode func; |
|---|
| 49 | + struct regulator_bulk_data supplies[ARRAY_SIZE(cs42l51_supply_names)]; |
|---|
| 50 | + struct gpio_desc *reset_gpio; |
|---|
| 51 | + struct regmap *regmap; |
|---|
| 46 | 52 | }; |
|---|
| 47 | 53 | |
|---|
| 48 | 54 | #define CS42L51_FORMATS ( \ |
|---|
| .. | .. |
|---|
| 55 | 61 | struct snd_ctl_elem_value *ucontrol) |
|---|
| 56 | 62 | { |
|---|
| 57 | 63 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
|---|
| 58 | | - unsigned long value = snd_soc_component_read32(component, CS42L51_PCM_MIXER)&3; |
|---|
| 64 | + unsigned long value = snd_soc_component_read(component, CS42L51_PCM_MIXER)&3; |
|---|
| 59 | 65 | |
|---|
| 60 | 66 | switch (value) { |
|---|
| 61 | 67 | default: |
|---|
| .. | .. |
|---|
| 109 | 115 | static const DECLARE_TLV_DB_SCALE(aout_tlv, -10200, 50, 0); |
|---|
| 110 | 116 | |
|---|
| 111 | 117 | static const DECLARE_TLV_DB_SCALE(boost_tlv, 1600, 1600, 0); |
|---|
| 118 | +static const DECLARE_TLV_DB_SCALE(adc_boost_tlv, 2000, 2000, 0); |
|---|
| 112 | 119 | static const char *chan_mix[] = { |
|---|
| 113 | 120 | "L R", |
|---|
| 114 | 121 | "L+R", |
|---|
| 115 | 122 | "R L", |
|---|
| 116 | 123 | }; |
|---|
| 124 | + |
|---|
| 125 | +static const DECLARE_TLV_DB_SCALE(pga_tlv, -300, 50, 0); |
|---|
| 126 | +static const DECLARE_TLV_DB_SCALE(adc_att_tlv, -9600, 100, 0); |
|---|
| 117 | 127 | |
|---|
| 118 | 128 | static SOC_ENUM_SINGLE_EXT_DECL(cs42l51_chan_mix, chan_mix); |
|---|
| 119 | 129 | |
|---|
| .. | .. |
|---|
| 131 | 141 | 0, 0x19, 0x7F, adc_pcm_tlv), |
|---|
| 132 | 142 | SOC_DOUBLE_R("ADC Mixer Switch", |
|---|
| 133 | 143 | CS42L51_ADCA_VOL, CS42L51_ADCB_VOL, 7, 1, 1), |
|---|
| 144 | + SOC_DOUBLE_R_SX_TLV("ADC Attenuator Volume", |
|---|
| 145 | + CS42L51_ADCA_ATT, CS42L51_ADCB_ATT, |
|---|
| 146 | + 0, 0xA0, 96, adc_att_tlv), |
|---|
| 147 | + SOC_DOUBLE_R_SX_TLV("PGA Volume", |
|---|
| 148 | + CS42L51_ALC_PGA_CTL, CS42L51_ALC_PGB_CTL, |
|---|
| 149 | + 0, 0x1A, 30, pga_tlv), |
|---|
| 134 | 150 | SOC_SINGLE("Playback Deemphasis Switch", CS42L51_DAC_CTL, 3, 1, 0), |
|---|
| 135 | 151 | SOC_SINGLE("Auto-Mute Switch", CS42L51_DAC_CTL, 2, 1, 0), |
|---|
| 136 | 152 | SOC_SINGLE("Soft Ramp Switch", CS42L51_DAC_CTL, 1, 1, 0), |
|---|
| 137 | 153 | SOC_SINGLE("Zero Cross Switch", CS42L51_DAC_CTL, 0, 0, 0), |
|---|
| 138 | 154 | SOC_DOUBLE_TLV("Mic Boost Volume", |
|---|
| 139 | 155 | CS42L51_MIC_CTL, 0, 1, 1, 0, boost_tlv), |
|---|
| 156 | + SOC_DOUBLE_TLV("ADC Boost Volume", |
|---|
| 157 | + CS42L51_MIC_CTL, 5, 6, 1, 0, adc_boost_tlv), |
|---|
| 140 | 158 | SOC_SINGLE_TLV("Bass Volume", CS42L51_TONE_CTL, 0, 0xf, 1, tone_tlv), |
|---|
| 141 | 159 | SOC_SINGLE_TLV("Treble Volume", CS42L51_TONE_CTL, 4, 0xf, 1, tone_tlv), |
|---|
| 142 | 160 | SOC_ENUM_EXT("PCM channel mixer", |
|---|
| .. | .. |
|---|
| 193 | 211 | SOC_DAPM_ENUM("Route", cs42l51_adcr_mux_enum); |
|---|
| 194 | 212 | |
|---|
| 195 | 213 | static const struct snd_soc_dapm_widget cs42l51_dapm_widgets[] = { |
|---|
| 196 | | - SND_SOC_DAPM_MICBIAS("Mic Bias", CS42L51_MIC_POWER_CTL, 1, 1), |
|---|
| 214 | + SND_SOC_DAPM_SUPPLY("Mic Bias", CS42L51_MIC_POWER_CTL, 1, 1, NULL, |
|---|
| 215 | + SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), |
|---|
| 197 | 216 | SND_SOC_DAPM_PGA_E("Left PGA", CS42L51_POWER_CTL1, 3, 1, NULL, 0, |
|---|
| 198 | 217 | cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD), |
|---|
| 199 | 218 | SND_SOC_DAPM_PGA_E("Right PGA", CS42L51_POWER_CTL1, 4, 1, NULL, 0, |
|---|
| .. | .. |
|---|
| 204 | 223 | SND_SOC_DAPM_ADC_E("Right ADC", "Right HiFi Capture", |
|---|
| 205 | 224 | CS42L51_POWER_CTL1, 2, 1, |
|---|
| 206 | 225 | cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD), |
|---|
| 207 | | - SND_SOC_DAPM_DAC_E("Left DAC", "Left HiFi Playback", |
|---|
| 208 | | - CS42L51_POWER_CTL1, 5, 1, |
|---|
| 209 | | - cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD), |
|---|
| 210 | | - SND_SOC_DAPM_DAC_E("Right DAC", "Right HiFi Playback", |
|---|
| 211 | | - CS42L51_POWER_CTL1, 6, 1, |
|---|
| 212 | | - cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD), |
|---|
| 226 | + SND_SOC_DAPM_DAC_E("Left DAC", NULL, CS42L51_POWER_CTL1, 5, 1, |
|---|
| 227 | + cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD), |
|---|
| 228 | + SND_SOC_DAPM_DAC_E("Right DAC", NULL, CS42L51_POWER_CTL1, 6, 1, |
|---|
| 229 | + cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD), |
|---|
| 213 | 230 | |
|---|
| 214 | 231 | /* analog/mic */ |
|---|
| 215 | 232 | SND_SOC_DAPM_INPUT("AIN1L"), |
|---|
| .. | .. |
|---|
| 237 | 254 | &cs42l51_adcr_mux_controls), |
|---|
| 238 | 255 | }; |
|---|
| 239 | 256 | |
|---|
| 257 | +static int mclk_event(struct snd_soc_dapm_widget *w, |
|---|
| 258 | + struct snd_kcontrol *kcontrol, int event) |
|---|
| 259 | +{ |
|---|
| 260 | + struct snd_soc_component *comp = snd_soc_dapm_to_component(w->dapm); |
|---|
| 261 | + struct cs42l51_private *cs42l51 = snd_soc_component_get_drvdata(comp); |
|---|
| 262 | + |
|---|
| 263 | + switch (event) { |
|---|
| 264 | + case SND_SOC_DAPM_PRE_PMU: |
|---|
| 265 | + return clk_prepare_enable(cs42l51->mclk_handle); |
|---|
| 266 | + case SND_SOC_DAPM_POST_PMD: |
|---|
| 267 | + /* Delay mclk shutdown to fulfill power-down sequence requirements */ |
|---|
| 268 | + msleep(20); |
|---|
| 269 | + clk_disable_unprepare(cs42l51->mclk_handle); |
|---|
| 270 | + break; |
|---|
| 271 | + } |
|---|
| 272 | + |
|---|
| 273 | + return 0; |
|---|
| 274 | +} |
|---|
| 275 | + |
|---|
| 276 | +static const struct snd_soc_dapm_widget cs42l51_dapm_mclk_widgets[] = { |
|---|
| 277 | + SND_SOC_DAPM_SUPPLY("MCLK", SND_SOC_NOPM, 0, 0, mclk_event, |
|---|
| 278 | + SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), |
|---|
| 279 | +}; |
|---|
| 280 | + |
|---|
| 240 | 281 | static const struct snd_soc_dapm_route cs42l51_routes[] = { |
|---|
| 241 | 282 | {"HPL", NULL, "Left DAC"}, |
|---|
| 242 | 283 | {"HPR", NULL, "Right DAC"}, |
|---|
| 284 | + |
|---|
| 285 | + {"Right DAC", NULL, "DAC Mux"}, |
|---|
| 286 | + {"Left DAC", NULL, "DAC Mux"}, |
|---|
| 287 | + |
|---|
| 288 | + {"DAC Mux", "Direct PCM", "Playback"}, |
|---|
| 289 | + {"DAC Mux", "DSP PCM", "Playback"}, |
|---|
| 243 | 290 | |
|---|
| 244 | 291 | {"Left ADC", NULL, "Left PGA"}, |
|---|
| 245 | 292 | {"Right ADC", NULL, "Right PGA"}, |
|---|
| .. | .. |
|---|
| 323 | 370 | { 256, CS42L51_DSM_MODE, 1 }, { 384, CS42L51_DSM_MODE, 1 }, |
|---|
| 324 | 371 | }; |
|---|
| 325 | 372 | |
|---|
| 373 | +/* |
|---|
| 374 | + * Master mode mclk/fs ratios. |
|---|
| 375 | + * Recommended configurations are SSM for 4-50khz and DSM for 50-100kHz ranges |
|---|
| 376 | + * The table below provides support of following ratios: |
|---|
| 377 | + * 128: SSM (%128) with div2 disabled |
|---|
| 378 | + * 256: SSM (%128) with div2 enabled |
|---|
| 379 | + * In both cases, if sampling rate is above 50kHz, SSM is overridden |
|---|
| 380 | + * with DSM (%128) configuration |
|---|
| 381 | + */ |
|---|
| 382 | +static struct cs42l51_ratios master_ratios[] = { |
|---|
| 383 | + { 128, CS42L51_SSM_MODE, 0 }, { 256, CS42L51_SSM_MODE, 1 }, |
|---|
| 384 | +}; |
|---|
| 385 | + |
|---|
| 326 | 386 | static int cs42l51_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
|---|
| 327 | 387 | int clk_id, unsigned int freq, int dir) |
|---|
| 328 | 388 | { |
|---|
| .. | .. |
|---|
| 345 | 405 | unsigned int ratio; |
|---|
| 346 | 406 | struct cs42l51_ratios *ratios = NULL; |
|---|
| 347 | 407 | int nr_ratios = 0; |
|---|
| 348 | | - int intf_ctl, power_ctl, fmt; |
|---|
| 408 | + int intf_ctl, power_ctl, fmt, mode; |
|---|
| 349 | 409 | |
|---|
| 350 | 410 | switch (cs42l51->func) { |
|---|
| 351 | 411 | case MODE_MASTER: |
|---|
| 352 | | - return -EINVAL; |
|---|
| 412 | + ratios = master_ratios; |
|---|
| 413 | + nr_ratios = ARRAY_SIZE(master_ratios); |
|---|
| 414 | + break; |
|---|
| 353 | 415 | case MODE_SLAVE: |
|---|
| 354 | 416 | ratios = slave_ratios; |
|---|
| 355 | 417 | nr_ratios = ARRAY_SIZE(slave_ratios); |
|---|
| .. | .. |
|---|
| 374 | 436 | return -EINVAL; |
|---|
| 375 | 437 | } |
|---|
| 376 | 438 | |
|---|
| 377 | | - intf_ctl = snd_soc_component_read32(component, CS42L51_INTF_CTL); |
|---|
| 378 | | - power_ctl = snd_soc_component_read32(component, CS42L51_MIC_POWER_CTL); |
|---|
| 439 | + intf_ctl = snd_soc_component_read(component, CS42L51_INTF_CTL); |
|---|
| 440 | + power_ctl = snd_soc_component_read(component, CS42L51_MIC_POWER_CTL); |
|---|
| 379 | 441 | |
|---|
| 380 | 442 | intf_ctl &= ~(CS42L51_INTF_CTL_MASTER | CS42L51_INTF_CTL_ADC_I2S |
|---|
| 381 | 443 | | CS42L51_INTF_CTL_DAC_FORMAT(7)); |
|---|
| .. | .. |
|---|
| 385 | 447 | switch (cs42l51->func) { |
|---|
| 386 | 448 | case MODE_MASTER: |
|---|
| 387 | 449 | intf_ctl |= CS42L51_INTF_CTL_MASTER; |
|---|
| 388 | | - power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode); |
|---|
| 450 | + mode = ratios[i].speed_mode; |
|---|
| 451 | + /* Force DSM mode if sampling rate is above 50kHz */ |
|---|
| 452 | + if (rate > 50000) |
|---|
| 453 | + mode = CS42L51_DSM_MODE; |
|---|
| 454 | + power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(mode); |
|---|
| 455 | + /* |
|---|
| 456 | + * Auto detect mode is not applicable for master mode and has to |
|---|
| 457 | + * be disabled. Otherwise SPEED[1:0] bits will be ignored. |
|---|
| 458 | + */ |
|---|
| 459 | + power_ctl &= ~CS42L51_MIC_POWER_CTL_AUTO; |
|---|
| 389 | 460 | break; |
|---|
| 390 | 461 | case MODE_SLAVE: |
|---|
| 391 | 462 | power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode); |
|---|
| .. | .. |
|---|
| 442 | 513 | return 0; |
|---|
| 443 | 514 | } |
|---|
| 444 | 515 | |
|---|
| 445 | | -static int cs42l51_dai_mute(struct snd_soc_dai *dai, int mute) |
|---|
| 516 | +static int cs42l51_dai_mute(struct snd_soc_dai *dai, int mute, int direction) |
|---|
| 446 | 517 | { |
|---|
| 447 | 518 | struct snd_soc_component *component = dai->component; |
|---|
| 448 | 519 | int reg; |
|---|
| 449 | 520 | int mask = CS42L51_DAC_OUT_CTL_DACA_MUTE|CS42L51_DAC_OUT_CTL_DACB_MUTE; |
|---|
| 450 | 521 | |
|---|
| 451 | | - reg = snd_soc_component_read32(component, CS42L51_DAC_OUT_CTL); |
|---|
| 522 | + reg = snd_soc_component_read(component, CS42L51_DAC_OUT_CTL); |
|---|
| 452 | 523 | |
|---|
| 453 | 524 | if (mute) |
|---|
| 454 | 525 | reg |= mask; |
|---|
| .. | .. |
|---|
| 458 | 529 | return snd_soc_component_write(component, CS42L51_DAC_OUT_CTL, reg); |
|---|
| 459 | 530 | } |
|---|
| 460 | 531 | |
|---|
| 532 | +static int cs42l51_of_xlate_dai_id(struct snd_soc_component *component, |
|---|
| 533 | + struct device_node *endpoint) |
|---|
| 534 | +{ |
|---|
| 535 | + /* return dai id 0, whatever the endpoint index */ |
|---|
| 536 | + return 0; |
|---|
| 537 | +} |
|---|
| 538 | + |
|---|
| 461 | 539 | static const struct snd_soc_dai_ops cs42l51_dai_ops = { |
|---|
| 462 | 540 | .hw_params = cs42l51_hw_params, |
|---|
| 463 | 541 | .set_sysclk = cs42l51_set_dai_sysclk, |
|---|
| 464 | 542 | .set_fmt = cs42l51_set_dai_fmt, |
|---|
| 465 | | - .digital_mute = cs42l51_dai_mute, |
|---|
| 543 | + .mute_stream = cs42l51_dai_mute, |
|---|
| 544 | + .no_capture_mute = 1, |
|---|
| 466 | 545 | }; |
|---|
| 467 | 546 | |
|---|
| 468 | 547 | static struct snd_soc_dai_driver cs42l51_dai = { |
|---|
| .. | .. |
|---|
| 487 | 566 | static int cs42l51_component_probe(struct snd_soc_component *component) |
|---|
| 488 | 567 | { |
|---|
| 489 | 568 | int ret, reg; |
|---|
| 569 | + struct snd_soc_dapm_context *dapm; |
|---|
| 570 | + struct cs42l51_private *cs42l51; |
|---|
| 571 | + |
|---|
| 572 | + cs42l51 = snd_soc_component_get_drvdata(component); |
|---|
| 573 | + dapm = snd_soc_component_get_dapm(component); |
|---|
| 574 | + |
|---|
| 575 | + if (cs42l51->mclk_handle) |
|---|
| 576 | + snd_soc_dapm_new_controls(dapm, cs42l51_dapm_mclk_widgets, 1); |
|---|
| 490 | 577 | |
|---|
| 491 | 578 | /* |
|---|
| 492 | 579 | * DAC configuration |
|---|
| .. | .. |
|---|
| 512 | 599 | .num_dapm_widgets = ARRAY_SIZE(cs42l51_dapm_widgets), |
|---|
| 513 | 600 | .dapm_routes = cs42l51_routes, |
|---|
| 514 | 601 | .num_dapm_routes = ARRAY_SIZE(cs42l51_routes), |
|---|
| 602 | + .of_xlate_dai_id = cs42l51_of_xlate_dai_id, |
|---|
| 515 | 603 | .idle_bias_on = 1, |
|---|
| 516 | 604 | .use_pmdown_time = 1, |
|---|
| 517 | 605 | .endianness = 1, |
|---|
| 518 | 606 | .non_legacy_dai_naming = 1, |
|---|
| 519 | 607 | }; |
|---|
| 520 | 608 | |
|---|
| 609 | +static bool cs42l51_writeable_reg(struct device *dev, unsigned int reg) |
|---|
| 610 | +{ |
|---|
| 611 | + switch (reg) { |
|---|
| 612 | + case CS42L51_POWER_CTL1: |
|---|
| 613 | + case CS42L51_MIC_POWER_CTL: |
|---|
| 614 | + case CS42L51_INTF_CTL: |
|---|
| 615 | + case CS42L51_MIC_CTL: |
|---|
| 616 | + case CS42L51_ADC_CTL: |
|---|
| 617 | + case CS42L51_ADC_INPUT: |
|---|
| 618 | + case CS42L51_DAC_OUT_CTL: |
|---|
| 619 | + case CS42L51_DAC_CTL: |
|---|
| 620 | + case CS42L51_ALC_PGA_CTL: |
|---|
| 621 | + case CS42L51_ALC_PGB_CTL: |
|---|
| 622 | + case CS42L51_ADCA_ATT: |
|---|
| 623 | + case CS42L51_ADCB_ATT: |
|---|
| 624 | + case CS42L51_ADCA_VOL: |
|---|
| 625 | + case CS42L51_ADCB_VOL: |
|---|
| 626 | + case CS42L51_PCMA_VOL: |
|---|
| 627 | + case CS42L51_PCMB_VOL: |
|---|
| 628 | + case CS42L51_BEEP_FREQ: |
|---|
| 629 | + case CS42L51_BEEP_VOL: |
|---|
| 630 | + case CS42L51_BEEP_CONF: |
|---|
| 631 | + case CS42L51_TONE_CTL: |
|---|
| 632 | + case CS42L51_AOUTA_VOL: |
|---|
| 633 | + case CS42L51_AOUTB_VOL: |
|---|
| 634 | + case CS42L51_PCM_MIXER: |
|---|
| 635 | + case CS42L51_LIMIT_THRES_DIS: |
|---|
| 636 | + case CS42L51_LIMIT_REL: |
|---|
| 637 | + case CS42L51_LIMIT_ATT: |
|---|
| 638 | + case CS42L51_ALC_EN: |
|---|
| 639 | + case CS42L51_ALC_REL: |
|---|
| 640 | + case CS42L51_ALC_THRES: |
|---|
| 641 | + case CS42L51_NOISE_CONF: |
|---|
| 642 | + case CS42L51_CHARGE_FREQ: |
|---|
| 643 | + return true; |
|---|
| 644 | + default: |
|---|
| 645 | + return false; |
|---|
| 646 | + } |
|---|
| 647 | +} |
|---|
| 648 | + |
|---|
| 649 | +static bool cs42l51_volatile_reg(struct device *dev, unsigned int reg) |
|---|
| 650 | +{ |
|---|
| 651 | + switch (reg) { |
|---|
| 652 | + case CS42L51_STATUS: |
|---|
| 653 | + return true; |
|---|
| 654 | + default: |
|---|
| 655 | + return false; |
|---|
| 656 | + } |
|---|
| 657 | +} |
|---|
| 658 | + |
|---|
| 659 | +static bool cs42l51_readable_reg(struct device *dev, unsigned int reg) |
|---|
| 660 | +{ |
|---|
| 661 | + switch (reg) { |
|---|
| 662 | + case CS42L51_CHIP_REV_ID: |
|---|
| 663 | + case CS42L51_POWER_CTL1: |
|---|
| 664 | + case CS42L51_MIC_POWER_CTL: |
|---|
| 665 | + case CS42L51_INTF_CTL: |
|---|
| 666 | + case CS42L51_MIC_CTL: |
|---|
| 667 | + case CS42L51_ADC_CTL: |
|---|
| 668 | + case CS42L51_ADC_INPUT: |
|---|
| 669 | + case CS42L51_DAC_OUT_CTL: |
|---|
| 670 | + case CS42L51_DAC_CTL: |
|---|
| 671 | + case CS42L51_ALC_PGA_CTL: |
|---|
| 672 | + case CS42L51_ALC_PGB_CTL: |
|---|
| 673 | + case CS42L51_ADCA_ATT: |
|---|
| 674 | + case CS42L51_ADCB_ATT: |
|---|
| 675 | + case CS42L51_ADCA_VOL: |
|---|
| 676 | + case CS42L51_ADCB_VOL: |
|---|
| 677 | + case CS42L51_PCMA_VOL: |
|---|
| 678 | + case CS42L51_PCMB_VOL: |
|---|
| 679 | + case CS42L51_BEEP_FREQ: |
|---|
| 680 | + case CS42L51_BEEP_VOL: |
|---|
| 681 | + case CS42L51_BEEP_CONF: |
|---|
| 682 | + case CS42L51_TONE_CTL: |
|---|
| 683 | + case CS42L51_AOUTA_VOL: |
|---|
| 684 | + case CS42L51_AOUTB_VOL: |
|---|
| 685 | + case CS42L51_PCM_MIXER: |
|---|
| 686 | + case CS42L51_LIMIT_THRES_DIS: |
|---|
| 687 | + case CS42L51_LIMIT_REL: |
|---|
| 688 | + case CS42L51_LIMIT_ATT: |
|---|
| 689 | + case CS42L51_ALC_EN: |
|---|
| 690 | + case CS42L51_ALC_REL: |
|---|
| 691 | + case CS42L51_ALC_THRES: |
|---|
| 692 | + case CS42L51_NOISE_CONF: |
|---|
| 693 | + case CS42L51_STATUS: |
|---|
| 694 | + case CS42L51_CHARGE_FREQ: |
|---|
| 695 | + return true; |
|---|
| 696 | + default: |
|---|
| 697 | + return false; |
|---|
| 698 | + } |
|---|
| 699 | +} |
|---|
| 700 | + |
|---|
| 521 | 701 | const struct regmap_config cs42l51_regmap = { |
|---|
| 702 | + .reg_bits = 8, |
|---|
| 703 | + .reg_stride = 1, |
|---|
| 704 | + .val_bits = 8, |
|---|
| 705 | + .use_single_write = true, |
|---|
| 706 | + .readable_reg = cs42l51_readable_reg, |
|---|
| 707 | + .volatile_reg = cs42l51_volatile_reg, |
|---|
| 708 | + .writeable_reg = cs42l51_writeable_reg, |
|---|
| 522 | 709 | .max_register = CS42L51_CHARGE_FREQ, |
|---|
| 523 | 710 | .cache_type = REGCACHE_RBTREE, |
|---|
| 524 | 711 | }; |
|---|
| .. | .. |
|---|
| 528 | 715 | { |
|---|
| 529 | 716 | struct cs42l51_private *cs42l51; |
|---|
| 530 | 717 | unsigned int val; |
|---|
| 531 | | - int ret; |
|---|
| 718 | + int ret, i; |
|---|
| 532 | 719 | |
|---|
| 533 | 720 | if (IS_ERR(regmap)) |
|---|
| 534 | 721 | return PTR_ERR(regmap); |
|---|
| .. | .. |
|---|
| 539 | 726 | return -ENOMEM; |
|---|
| 540 | 727 | |
|---|
| 541 | 728 | dev_set_drvdata(dev, cs42l51); |
|---|
| 729 | + cs42l51->regmap = regmap; |
|---|
| 730 | + |
|---|
| 731 | + cs42l51->mclk_handle = devm_clk_get(dev, "MCLK"); |
|---|
| 732 | + if (IS_ERR(cs42l51->mclk_handle)) { |
|---|
| 733 | + if (PTR_ERR(cs42l51->mclk_handle) != -ENOENT) |
|---|
| 734 | + return PTR_ERR(cs42l51->mclk_handle); |
|---|
| 735 | + cs42l51->mclk_handle = NULL; |
|---|
| 736 | + } |
|---|
| 737 | + |
|---|
| 738 | + for (i = 0; i < ARRAY_SIZE(cs42l51->supplies); i++) |
|---|
| 739 | + cs42l51->supplies[i].supply = cs42l51_supply_names[i]; |
|---|
| 740 | + |
|---|
| 741 | + ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(cs42l51->supplies), |
|---|
| 742 | + cs42l51->supplies); |
|---|
| 743 | + if (ret != 0) { |
|---|
| 744 | + dev_err(dev, "Failed to request supplies: %d\n", ret); |
|---|
| 745 | + return ret; |
|---|
| 746 | + } |
|---|
| 747 | + |
|---|
| 748 | + ret = regulator_bulk_enable(ARRAY_SIZE(cs42l51->supplies), |
|---|
| 749 | + cs42l51->supplies); |
|---|
| 750 | + if (ret != 0) { |
|---|
| 751 | + dev_err(dev, "Failed to enable supplies: %d\n", ret); |
|---|
| 752 | + return ret; |
|---|
| 753 | + } |
|---|
| 754 | + |
|---|
| 755 | + cs42l51->reset_gpio = devm_gpiod_get_optional(dev, "reset", |
|---|
| 756 | + GPIOD_OUT_LOW); |
|---|
| 757 | + if (IS_ERR(cs42l51->reset_gpio)) |
|---|
| 758 | + return PTR_ERR(cs42l51->reset_gpio); |
|---|
| 759 | + |
|---|
| 760 | + if (cs42l51->reset_gpio) { |
|---|
| 761 | + dev_dbg(dev, "Release reset gpio\n"); |
|---|
| 762 | + gpiod_set_value_cansleep(cs42l51->reset_gpio, 0); |
|---|
| 763 | + mdelay(2); |
|---|
| 764 | + } |
|---|
| 542 | 765 | |
|---|
| 543 | 766 | /* Verify that we have a CS42L51 */ |
|---|
| 544 | 767 | ret = regmap_read(regmap, CS42L51_CHIP_REV_ID, &val); |
|---|
| .. | .. |
|---|
| 558 | 781 | |
|---|
| 559 | 782 | ret = devm_snd_soc_register_component(dev, |
|---|
| 560 | 783 | &soc_component_device_cs42l51, &cs42l51_dai, 1); |
|---|
| 784 | + if (ret < 0) |
|---|
| 785 | + goto error; |
|---|
| 786 | + |
|---|
| 787 | + return 0; |
|---|
| 788 | + |
|---|
| 561 | 789 | error: |
|---|
| 790 | + regulator_bulk_disable(ARRAY_SIZE(cs42l51->supplies), |
|---|
| 791 | + cs42l51->supplies); |
|---|
| 562 | 792 | return ret; |
|---|
| 563 | 793 | } |
|---|
| 564 | 794 | EXPORT_SYMBOL_GPL(cs42l51_probe); |
|---|
| 565 | 795 | |
|---|
| 796 | +int cs42l51_remove(struct device *dev) |
|---|
| 797 | +{ |
|---|
| 798 | + struct cs42l51_private *cs42l51 = dev_get_drvdata(dev); |
|---|
| 799 | + |
|---|
| 800 | + gpiod_set_value_cansleep(cs42l51->reset_gpio, 1); |
|---|
| 801 | + |
|---|
| 802 | + return regulator_bulk_disable(ARRAY_SIZE(cs42l51->supplies), |
|---|
| 803 | + cs42l51->supplies); |
|---|
| 804 | +} |
|---|
| 805 | +EXPORT_SYMBOL_GPL(cs42l51_remove); |
|---|
| 806 | + |
|---|
| 807 | +int __maybe_unused cs42l51_suspend(struct device *dev) |
|---|
| 808 | +{ |
|---|
| 809 | + struct cs42l51_private *cs42l51 = dev_get_drvdata(dev); |
|---|
| 810 | + |
|---|
| 811 | + regcache_cache_only(cs42l51->regmap, true); |
|---|
| 812 | + regcache_mark_dirty(cs42l51->regmap); |
|---|
| 813 | + |
|---|
| 814 | + return 0; |
|---|
| 815 | +} |
|---|
| 816 | +EXPORT_SYMBOL_GPL(cs42l51_suspend); |
|---|
| 817 | + |
|---|
| 818 | +int __maybe_unused cs42l51_resume(struct device *dev) |
|---|
| 819 | +{ |
|---|
| 820 | + struct cs42l51_private *cs42l51 = dev_get_drvdata(dev); |
|---|
| 821 | + |
|---|
| 822 | + regcache_cache_only(cs42l51->regmap, false); |
|---|
| 823 | + |
|---|
| 824 | + return regcache_sync(cs42l51->regmap); |
|---|
| 825 | +} |
|---|
| 826 | +EXPORT_SYMBOL_GPL(cs42l51_resume); |
|---|
| 827 | + |
|---|
| 566 | 828 | const struct of_device_id cs42l51_of_match[] = { |
|---|
| 567 | 829 | { .compatible = "cirrus,cs42l51", }, |
|---|
| 568 | 830 | { } |
|---|