linux/sound/soc/codecs/mt6660.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2
   3// Copyright (c) 2019 MediaTek Inc.
   4
   5#include <linux/module.h>
   6#include <linux/kernel.h>
   7#include <linux/version.h>
   8#include <linux/err.h>
   9#include <linux/i2c.h>
  10#include <linux/pm_runtime.h>
  11#include <linux/delay.h>
  12#include <sound/soc.h>
  13#include <sound/tlv.h>
  14#include <sound/pcm_params.h>
  15
  16#include "mt6660.h"
  17
  18struct reg_size_table {
  19        u32 addr;
  20        u8 size;
  21};
  22
  23static const struct reg_size_table mt6660_reg_size_table[] = {
  24        { MT6660_REG_HPF1_COEF, 4 },
  25        { MT6660_REG_HPF2_COEF, 4 },
  26        { MT6660_REG_TDM_CFG3, 2 },
  27        { MT6660_REG_RESV17, 2 },
  28        { MT6660_REG_RESV23, 2 },
  29        { MT6660_REG_SIGMAX, 2 },
  30        { MT6660_REG_DEVID, 2 },
  31        { MT6660_REG_HCLIP_CTRL, 2 },
  32        { MT6660_REG_DA_GAIN, 2 },
  33};
  34
  35static int mt6660_get_reg_size(uint32_t addr)
  36{
  37        int i;
  38
  39        for (i = 0; i < ARRAY_SIZE(mt6660_reg_size_table); i++) {
  40                if (mt6660_reg_size_table[i].addr == addr)
  41                        return mt6660_reg_size_table[i].size;
  42        }
  43        return 1;
  44}
  45
  46static int mt6660_reg_write(void *context, unsigned int reg, unsigned int val)
  47{
  48        struct mt6660_chip *chip = context;
  49        int size = mt6660_get_reg_size(reg);
  50        u8 reg_data[4];
  51        int i, ret;
  52
  53        for (i = 0; i < size; i++)
  54                reg_data[size - i - 1] = (val >> (8 * i)) & 0xff;
  55
  56        ret = i2c_smbus_write_i2c_block_data(chip->i2c, reg, size, reg_data);
  57        return ret;
  58}
  59
  60static int mt6660_reg_read(void *context, unsigned int reg, unsigned int *val)
  61{
  62        struct mt6660_chip *chip = context;
  63        int size = mt6660_get_reg_size(reg);
  64        int i, ret;
  65        u8 data[4];
  66        u32 reg_data = 0;
  67
  68        ret = i2c_smbus_read_i2c_block_data(chip->i2c, reg, size, data);
  69        if (ret < 0)
  70                return ret;
  71        for (i = 0; i < size; i++) {
  72                reg_data <<= 8;
  73                reg_data |= data[i];
  74        }
  75        *val = reg_data;
  76        return 0;
  77}
  78
  79static const struct regmap_config mt6660_regmap_config = {
  80        .reg_bits = 8,
  81        .val_bits = 32,
  82        .reg_write = mt6660_reg_write,
  83        .reg_read = mt6660_reg_read,
  84};
  85
  86static int mt6660_codec_dac_event(struct snd_soc_dapm_widget *w,
  87        struct snd_kcontrol *kcontrol, int event)
  88{
  89        if (event == SND_SOC_DAPM_POST_PMU)
  90                usleep_range(1000, 1100);
  91        return 0;
  92}
  93
  94static int mt6660_codec_classd_event(struct snd_soc_dapm_widget *w,
  95        struct snd_kcontrol *kcontrol, int event)
  96{
  97        struct snd_soc_component *component =
  98                snd_soc_dapm_to_component(w->dapm);
  99        int ret;
 100
 101        switch (event) {
 102        case SND_SOC_DAPM_PRE_PMU:
 103                dev_dbg(component->dev,
 104                        "%s: before classd turn on\n", __func__);
 105                /* config to adaptive mode */
 106                ret = snd_soc_component_update_bits(component,
 107                        MT6660_REG_BST_CTRL, 0x03, 0x03);
 108                if (ret < 0) {
 109                        dev_err(component->dev, "config mode adaptive fail\n");
 110                        return ret;
 111                }
 112                break;
 113        case SND_SOC_DAPM_POST_PMU:
 114                /* voltage sensing enable */
 115                ret = snd_soc_component_update_bits(component,
 116                        MT6660_REG_RESV7, 0x04, 0x04);
 117                if (ret < 0) {
 118                        dev_err(component->dev,
 119                                "enable voltage sensing fail\n");
 120                        return ret;
 121                }
 122                dev_dbg(component->dev, "Amp on\n");
 123                break;
 124        case SND_SOC_DAPM_PRE_PMD:
 125                dev_dbg(component->dev, "Amp off\n");
 126                /* voltage sensing disable */
 127                ret = snd_soc_component_update_bits(component,
 128                        MT6660_REG_RESV7, 0x04, 0x00);
 129                if (ret < 0) {
 130                        dev_err(component->dev,
 131                                "disable voltage sensing fail\n");
 132                        return ret;
 133                }
 134                /* pop-noise improvement 1 */
 135                ret = snd_soc_component_update_bits(component,
 136                        MT6660_REG_RESV10, 0x10, 0x10);
 137                if (ret < 0) {
 138                        dev_err(component->dev,
 139                                "pop-noise improvement 1 fail\n");
 140                        return ret;
 141                }
 142                break;
 143        case SND_SOC_DAPM_POST_PMD:
 144                dev_dbg(component->dev,
 145                        "%s: after classd turn off\n", __func__);
 146                /* pop-noise improvement 2 */
 147                ret = snd_soc_component_update_bits(component,
 148                        MT6660_REG_RESV10, 0x10, 0x00);
 149                if (ret < 0) {
 150                        dev_err(component->dev,
 151                                "pop-noise improvement 2 fail\n");
 152                        return ret;
 153                }
 154                /* config to off mode */
 155                ret = snd_soc_component_update_bits(component,
 156                        MT6660_REG_BST_CTRL, 0x03, 0x00);
 157                if (ret < 0) {
 158                        dev_err(component->dev, "config mode off fail\n");
 159                        return ret;
 160                }
 161                break;
 162        }
 163        return 0;
 164}
 165
 166static const struct snd_soc_dapm_widget mt6660_component_dapm_widgets[] = {
 167        SND_SOC_DAPM_DAC_E("DAC", NULL, MT6660_REG_PLL_CFG1,
 168                0, 1, mt6660_codec_dac_event, SND_SOC_DAPM_POST_PMU),
 169        SND_SOC_DAPM_ADC("VI ADC", NULL, SND_SOC_NOPM, 0, 0),
 170        SND_SOC_DAPM_PGA("PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
 171        SND_SOC_DAPM_OUT_DRV_E("ClassD", MT6660_REG_SYSTEM_CTRL, 2, 0,
 172                               NULL, 0, mt6660_codec_classd_event,
 173                               SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
 174                               SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
 175        SND_SOC_DAPM_OUTPUT("OUTP"),
 176        SND_SOC_DAPM_OUTPUT("OUTN"),
 177};
 178
 179static const struct snd_soc_dapm_route mt6660_component_dapm_routes[] = {
 180        { "DAC", NULL, "aif_playback" },
 181        { "PGA", NULL, "DAC" },
 182        { "ClassD", NULL, "PGA" },
 183        { "OUTP", NULL, "ClassD" },
 184        { "OUTN", NULL, "ClassD" },
 185        { "VI ADC", NULL, "ClassD" },
 186        { "aif_capture", NULL, "VI ADC" },
 187};
 188
 189static int mt6660_component_get_volsw(struct snd_kcontrol *kcontrol,
 190                                  struct snd_ctl_elem_value *ucontrol)
 191{
 192        struct snd_soc_component *component =
 193                snd_soc_kcontrol_component(kcontrol);
 194        struct mt6660_chip *chip = (struct mt6660_chip *)
 195                snd_soc_component_get_drvdata(component);
 196
 197        ucontrol->value.integer.value[0] = chip->chip_rev & 0x0f;
 198        return 0;
 199}
 200
 201static const DECLARE_TLV_DB_SCALE(vol_ctl_tlv, -1155, 5, 0);
 202
 203static const struct snd_kcontrol_new mt6660_component_snd_controls[] = {
 204        SOC_SINGLE_TLV("Digital Volume", MT6660_REG_VOL_CTRL, 0, 255,
 205                           1, vol_ctl_tlv),
 206        SOC_SINGLE("Hard Clip Switch", MT6660_REG_HCLIP_CTRL, 8, 1, 0),
 207        SOC_SINGLE("Clip Switch", MT6660_REG_SPS_CTRL, 0, 1, 0),
 208        SOC_SINGLE("Boost Mode", MT6660_REG_BST_CTRL, 0, 3, 0),
 209        SOC_SINGLE("DRE Switch", MT6660_REG_DRE_CTRL, 0, 1, 0),
 210        SOC_SINGLE("DC Protect Switch", MT6660_REG_DC_PROTECT_CTRL, 3, 1, 0),
 211        SOC_SINGLE("Data Output Left Channel Selection",
 212                   MT6660_REG_DATAO_SEL, 3, 7, 0),
 213        SOC_SINGLE("Data Output Right Channel Selection",
 214                   MT6660_REG_DATAO_SEL, 0, 7, 0),
 215        SOC_SINGLE_EXT("T0 SEL", MT6660_REG_CALI_T0, 0, 7, 0,
 216                       snd_soc_get_volsw, NULL),
 217        SOC_SINGLE_EXT("Chip Rev", MT6660_REG_DEVID, 8, 15, 0,
 218                       mt6660_component_get_volsw, NULL),
 219};
 220
 221static int _mt6660_chip_power_on(struct mt6660_chip *chip, int on_off)
 222{
 223        return regmap_write_bits(chip->regmap, MT6660_REG_SYSTEM_CTRL,
 224                                 0x01, on_off ? 0x00 : 0x01);
 225}
 226
 227struct reg_table {
 228        uint32_t addr;
 229        uint32_t mask;
 230        uint32_t val;
 231};
 232
 233static const struct reg_table mt6660_setting_table[] = {
 234        { 0x20, 0x80, 0x00 },
 235        { 0x30, 0x01, 0x00 },
 236        { 0x50, 0x1c, 0x04 },
 237        { 0xB1, 0x0c, 0x00 },
 238        { 0xD3, 0x03, 0x03 },
 239        { 0xE0, 0x01, 0x00 },
 240        { 0x98, 0x44, 0x04 },
 241        { 0xB9, 0xff, 0x82 },
 242        { 0xB7, 0x7777, 0x7273 },
 243        { 0xB6, 0x07, 0x03 },
 244        { 0x6B, 0xe0, 0x20 },
 245        { 0x07, 0xff, 0x70 },
 246        { 0xBB, 0xff, 0x20 },
 247        { 0x69, 0xff, 0x40 },
 248        { 0xBD, 0xffff, 0x17f8 },
 249        { 0x70, 0xff, 0x15 },
 250        { 0x7C, 0xff, 0x00 },
 251        { 0x46, 0xff, 0x1d },
 252        { 0x1A, 0xffffffff, 0x7fdb7ffe },
 253        { 0x1B, 0xffffffff, 0x7fdb7ffe },
 254        { 0x51, 0xff, 0x58 },
 255        { 0xA2, 0xff, 0xce },
 256        { 0x33, 0xffff, 0x7fff },
 257        { 0x4C, 0xffff, 0x0116 },
 258        { 0x16, 0x1800, 0x0800 },
 259        { 0x68, 0x1f, 0x07 },
 260};
 261
 262static int mt6660_component_setting(struct snd_soc_component *component)
 263{
 264        struct mt6660_chip *chip = snd_soc_component_get_drvdata(component);
 265        int ret = 0;
 266        size_t i = 0;
 267
 268        ret = _mt6660_chip_power_on(chip, 1);
 269        if (ret < 0) {
 270                dev_err(component->dev, "%s chip power on failed\n", __func__);
 271                return ret;
 272        }
 273
 274        for (i = 0; i < ARRAY_SIZE(mt6660_setting_table); i++) {
 275                ret = snd_soc_component_update_bits(component,
 276                                mt6660_setting_table[i].addr,
 277                                mt6660_setting_table[i].mask,
 278                                mt6660_setting_table[i].val);
 279                if (ret < 0) {
 280                        dev_err(component->dev, "%s update 0x%02x failed\n",
 281                                __func__, mt6660_setting_table[i].addr);
 282                        return ret;
 283                }
 284        }
 285
 286        ret = _mt6660_chip_power_on(chip, 0);
 287        if (ret < 0) {
 288                dev_err(component->dev, "%s chip power off failed\n", __func__);
 289                return ret;
 290        }
 291
 292        return 0;
 293}
 294
 295static int mt6660_component_probe(struct snd_soc_component *component)
 296{
 297        struct mt6660_chip *chip = snd_soc_component_get_drvdata(component);
 298        int ret;
 299
 300        dev_dbg(component->dev, "%s\n", __func__);
 301        snd_soc_component_init_regmap(component, chip->regmap);
 302
 303        ret = mt6660_component_setting(component);
 304        if (ret < 0)
 305                dev_err(chip->dev, "mt6660 component setting failed\n");
 306
 307        return ret;
 308}
 309
 310static void mt6660_component_remove(struct snd_soc_component *component)
 311{
 312        dev_dbg(component->dev, "%s\n", __func__);
 313        snd_soc_component_exit_regmap(component);
 314}
 315
 316static const struct snd_soc_component_driver mt6660_component_driver = {
 317        .probe = mt6660_component_probe,
 318        .remove = mt6660_component_remove,
 319
 320        .controls = mt6660_component_snd_controls,
 321        .num_controls = ARRAY_SIZE(mt6660_component_snd_controls),
 322        .dapm_widgets = mt6660_component_dapm_widgets,
 323        .num_dapm_widgets = ARRAY_SIZE(mt6660_component_dapm_widgets),
 324        .dapm_routes = mt6660_component_dapm_routes,
 325        .num_dapm_routes = ARRAY_SIZE(mt6660_component_dapm_routes),
 326
 327        .idle_bias_on = false, /* idle_bias_off = true */
 328};
 329
 330static int mt6660_component_aif_hw_params(struct snd_pcm_substream *substream,
 331        struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
 332{
 333        int word_len = params_physical_width(hw_params);
 334        int aud_bit = params_width(hw_params);
 335        u16 reg_data = 0;
 336        int ret;
 337
 338        dev_dbg(dai->dev, "%s: ++\n", __func__);
 339        dev_dbg(dai->dev, "format: 0x%08x\n", params_format(hw_params));
 340        dev_dbg(dai->dev, "rate: 0x%08x\n", params_rate(hw_params));
 341        dev_dbg(dai->dev, "word_len: %d, aud_bit: %d\n", word_len, aud_bit);
 342        if (word_len > 32 || word_len < 16) {
 343                dev_err(dai->dev, "not supported word length\n");
 344                return -ENOTSUPP;
 345        }
 346        switch (aud_bit) {
 347        case 16:
 348                reg_data = 3;
 349                break;
 350        case 18:
 351                reg_data = 2;
 352                break;
 353        case 20:
 354                reg_data = 1;
 355                break;
 356        case 24:
 357        case 32:
 358                reg_data = 0;
 359                break;
 360        default:
 361                return -ENOTSUPP;
 362        }
 363        ret = snd_soc_component_update_bits(dai->component,
 364                MT6660_REG_SERIAL_CFG1, 0xc0, (reg_data << 6));
 365        if (ret < 0) {
 366                dev_err(dai->dev, "config aud bit fail\n");
 367                return ret;
 368        }
 369        ret = snd_soc_component_update_bits(dai->component,
 370                MT6660_REG_TDM_CFG3, 0x3f0, word_len << 4);
 371        if (ret < 0) {
 372                dev_err(dai->dev, "config word len fail\n");
 373                return ret;
 374        }
 375        dev_dbg(dai->dev, "%s: --\n", __func__);
 376        return 0;
 377}
 378
 379static const struct snd_soc_dai_ops mt6660_component_aif_ops = {
 380        .hw_params = mt6660_component_aif_hw_params,
 381};
 382
 383#define STUB_RATES      SNDRV_PCM_RATE_8000_192000
 384#define STUB_FORMATS    (SNDRV_PCM_FMTBIT_S16_LE | \
 385                        SNDRV_PCM_FMTBIT_U16_LE | \
 386                        SNDRV_PCM_FMTBIT_S24_LE | \
 387                        SNDRV_PCM_FMTBIT_U24_LE | \
 388                        SNDRV_PCM_FMTBIT_S32_LE | \
 389                        SNDRV_PCM_FMTBIT_U32_LE)
 390
 391static struct snd_soc_dai_driver mt6660_codec_dai = {
 392        .name = "mt6660-aif",
 393        .playback = {
 394                .stream_name    = "aif_playback",
 395                .channels_min   = 1,
 396                .channels_max   = 2,
 397                .rates          = STUB_RATES,
 398                .formats        = STUB_FORMATS,
 399        },
 400        .capture = {
 401                .stream_name    = "aif_capture",
 402                .channels_min   = 1,
 403                .channels_max   = 2,
 404                .rates = STUB_RATES,
 405                .formats = STUB_FORMATS,
 406        },
 407        /* dai properties */
 408        .symmetric_rate = 1,
 409        .symmetric_channels = 1,
 410        .symmetric_sample_bits = 1,
 411        /* dai operations */
 412        .ops = &mt6660_component_aif_ops,
 413};
 414
 415static int _mt6660_chip_id_check(struct mt6660_chip *chip)
 416{
 417        int ret;
 418        unsigned int val;
 419
 420        ret = regmap_read(chip->regmap, MT6660_REG_DEVID, &val);
 421        if (ret < 0)
 422                return ret;
 423        val &= 0x0ff0;
 424        if (val != 0x00e0 && val != 0x01e0) {
 425                dev_err(chip->dev, "%s id(%x) not match\n", __func__, val);
 426                return -ENODEV;
 427        }
 428        return 0;
 429}
 430
 431static int _mt6660_chip_sw_reset(struct mt6660_chip *chip)
 432{
 433        int ret;
 434
 435        /* turn on main pll first, then trigger reset */
 436        ret = regmap_write(chip->regmap, MT6660_REG_SYSTEM_CTRL, 0x00);
 437        if (ret < 0)
 438                return ret;
 439        ret = regmap_write(chip->regmap, MT6660_REG_SYSTEM_CTRL, 0x80);
 440        if (ret < 0)
 441                return ret;
 442        msleep(30);
 443        return 0;
 444}
 445
 446static int _mt6660_read_chip_revision(struct mt6660_chip *chip)
 447{
 448        int ret;
 449        unsigned int val;
 450
 451        ret = regmap_read(chip->regmap, MT6660_REG_DEVID, &val);
 452        if (ret < 0) {
 453                dev_err(chip->dev, "get chip revision fail\n");
 454                return ret;
 455        }
 456        chip->chip_rev = val&0xff;
 457        dev_info(chip->dev, "%s chip_rev = %x\n", __func__, chip->chip_rev);
 458        return 0;
 459}
 460
 461static int mt6660_i2c_probe(struct i2c_client *client,
 462                            const struct i2c_device_id *id)
 463{
 464        struct mt6660_chip *chip = NULL;
 465        int ret;
 466
 467        dev_dbg(&client->dev, "%s\n", __func__);
 468        chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
 469        if (!chip)
 470                return -ENOMEM;
 471        chip->i2c = client;
 472        chip->dev = &client->dev;
 473        mutex_init(&chip->io_lock);
 474        i2c_set_clientdata(client, chip);
 475
 476        chip->regmap = devm_regmap_init(&client->dev,
 477                NULL, chip, &mt6660_regmap_config);
 478        if (IS_ERR(chip->regmap)) {
 479                ret = PTR_ERR(chip->regmap);
 480                dev_err(&client->dev, "failed to initialise regmap: %d\n", ret);
 481                return ret;
 482        }
 483
 484        /* chip reset first */
 485        ret = _mt6660_chip_sw_reset(chip);
 486        if (ret < 0) {
 487                dev_err(chip->dev, "chip reset fail\n");
 488                goto probe_fail;
 489        }
 490        /* chip power on */
 491        ret = _mt6660_chip_power_on(chip, 1);
 492        if (ret < 0) {
 493                dev_err(chip->dev, "chip power on 2 fail\n");
 494                goto probe_fail;
 495        }
 496        /* chip devid check */
 497        ret = _mt6660_chip_id_check(chip);
 498        if (ret < 0) {
 499                dev_err(chip->dev, "chip id check fail\n");
 500                goto probe_fail;
 501        }
 502        /* chip revision get */
 503        ret = _mt6660_read_chip_revision(chip);
 504        if (ret < 0) {
 505                dev_err(chip->dev, "read chip revision fail\n");
 506                goto probe_fail;
 507        }
 508        pm_runtime_set_active(chip->dev);
 509        pm_runtime_enable(chip->dev);
 510
 511        ret = devm_snd_soc_register_component(chip->dev,
 512                                               &mt6660_component_driver,
 513                                               &mt6660_codec_dai, 1);
 514        return ret;
 515probe_fail:
 516        _mt6660_chip_power_on(chip, 0);
 517        mutex_destroy(&chip->io_lock);
 518        return ret;
 519}
 520
 521static int mt6660_i2c_remove(struct i2c_client *client)
 522{
 523        struct mt6660_chip *chip = i2c_get_clientdata(client);
 524
 525        pm_runtime_disable(chip->dev);
 526        pm_runtime_set_suspended(chip->dev);
 527        mutex_destroy(&chip->io_lock);
 528        return 0;
 529}
 530
 531static int __maybe_unused mt6660_i2c_runtime_suspend(struct device *dev)
 532{
 533        struct mt6660_chip *chip = dev_get_drvdata(dev);
 534
 535        dev_dbg(dev, "enter low power mode\n");
 536        return regmap_update_bits(chip->regmap,
 537                MT6660_REG_SYSTEM_CTRL, 0x01, 0x01);
 538}
 539
 540static int __maybe_unused mt6660_i2c_runtime_resume(struct device *dev)
 541{
 542        struct mt6660_chip *chip = dev_get_drvdata(dev);
 543
 544        dev_dbg(dev, "exit low power mode\n");
 545        return regmap_update_bits(chip->regmap,
 546                MT6660_REG_SYSTEM_CTRL, 0x01, 0x00);
 547}
 548
 549static const struct dev_pm_ops mt6660_dev_pm_ops = {
 550        SET_RUNTIME_PM_OPS(mt6660_i2c_runtime_suspend,
 551                           mt6660_i2c_runtime_resume, NULL)
 552};
 553
 554static const struct of_device_id __maybe_unused mt6660_of_id[] = {
 555        { .compatible = "mediatek,mt6660",},
 556        {},
 557};
 558MODULE_DEVICE_TABLE(of, mt6660_of_id);
 559
 560static const struct i2c_device_id mt6660_i2c_id[] = {
 561        {"mt6660", 0 },
 562        {},
 563};
 564MODULE_DEVICE_TABLE(i2c, mt6660_i2c_id);
 565
 566static struct i2c_driver mt6660_i2c_driver = {
 567        .driver = {
 568                .name = "mt6660",
 569                .of_match_table = of_match_ptr(mt6660_of_id),
 570                .pm = &mt6660_dev_pm_ops,
 571        },
 572        .probe = mt6660_i2c_probe,
 573        .remove = mt6660_i2c_remove,
 574        .id_table = mt6660_i2c_id,
 575};
 576module_i2c_driver(mt6660_i2c_driver);
 577
 578MODULE_AUTHOR("Jeff Chang <jeff_chang@richtek.com>");
 579MODULE_DESCRIPTION("MT6660 SPKAMP Driver");
 580MODULE_LICENSE("GPL");
 581MODULE_VERSION("1.0.8_G");
 582