10471cd93SMichael Trimarchi /* 20471cd93SMichael Trimarchi * PCM179X ASoC codec driver 30471cd93SMichael Trimarchi * 40471cd93SMichael Trimarchi * Copyright (c) Amarula Solutions B.V. 2013 50471cd93SMichael Trimarchi * 60471cd93SMichael Trimarchi * Michael Trimarchi <michael@amarulasolutions.com> 70471cd93SMichael Trimarchi * 80471cd93SMichael Trimarchi * This program is free software; you can redistribute it and/or 90471cd93SMichael Trimarchi * modify it under the terms of the GNU General Public License 100471cd93SMichael Trimarchi * as published by the Free Software Foundation; either version 2 110471cd93SMichael Trimarchi * of the License, or (at your option) any later version. 120471cd93SMichael Trimarchi * 130471cd93SMichael Trimarchi * This program is distributed in the hope that it will be useful, 140471cd93SMichael Trimarchi * but WITHOUT ANY WARRANTY; without even the implied warranty of 150471cd93SMichael Trimarchi * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 160471cd93SMichael Trimarchi * GNU General Public License for more details. 170471cd93SMichael Trimarchi */ 180471cd93SMichael Trimarchi 190471cd93SMichael Trimarchi #include <linux/module.h> 200471cd93SMichael Trimarchi #include <linux/slab.h> 210471cd93SMichael Trimarchi #include <linux/kernel.h> 220471cd93SMichael Trimarchi #include <linux/device.h> 230471cd93SMichael Trimarchi 240471cd93SMichael Trimarchi #include <sound/core.h> 250471cd93SMichael Trimarchi #include <sound/pcm.h> 260471cd93SMichael Trimarchi #include <sound/pcm_params.h> 270471cd93SMichael Trimarchi #include <sound/initval.h> 280471cd93SMichael Trimarchi #include <sound/soc.h> 290471cd93SMichael Trimarchi #include <sound/tlv.h> 300471cd93SMichael Trimarchi #include <linux/of.h> 310471cd93SMichael Trimarchi 320471cd93SMichael Trimarchi #include "pcm179x.h" 330471cd93SMichael Trimarchi 340471cd93SMichael Trimarchi #define PCM179X_DAC_VOL_LEFT 0x10 350471cd93SMichael Trimarchi #define PCM179X_DAC_VOL_RIGHT 0x11 360471cd93SMichael Trimarchi #define PCM179X_FMT_CONTROL 0x12 370471cd93SMichael Trimarchi #define PCM179X_MODE_CONTROL 0x13 380471cd93SMichael Trimarchi #define PCM179X_SOFT_MUTE PCM179X_FMT_CONTROL 390471cd93SMichael Trimarchi 400471cd93SMichael Trimarchi #define PCM179X_FMT_MASK 0x70 410471cd93SMichael Trimarchi #define PCM179X_FMT_SHIFT 4 420471cd93SMichael Trimarchi #define PCM179X_MUTE_MASK 0x01 430471cd93SMichael Trimarchi #define PCM179X_MUTE_SHIFT 0 440471cd93SMichael Trimarchi #define PCM179X_ATLD_ENABLE (1 << 7) 450471cd93SMichael Trimarchi 460471cd93SMichael Trimarchi static const struct reg_default pcm179x_reg_defaults[] = { 470471cd93SMichael Trimarchi { 0x10, 0xff }, 480471cd93SMichael Trimarchi { 0x11, 0xff }, 490471cd93SMichael Trimarchi { 0x12, 0x50 }, 500471cd93SMichael Trimarchi { 0x13, 0x00 }, 510471cd93SMichael Trimarchi { 0x14, 0x00 }, 520471cd93SMichael Trimarchi { 0x15, 0x01 }, 530471cd93SMichael Trimarchi { 0x16, 0x00 }, 540471cd93SMichael Trimarchi { 0x17, 0x00 }, 550471cd93SMichael Trimarchi }; 560471cd93SMichael Trimarchi 570471cd93SMichael Trimarchi static bool pcm179x_accessible_reg(struct device *dev, unsigned int reg) 580471cd93SMichael Trimarchi { 590471cd93SMichael Trimarchi return reg >= 0x10 && reg <= 0x17; 600471cd93SMichael Trimarchi } 610471cd93SMichael Trimarchi 62a074ae0eSArnd Bergmann static bool pcm179x_writeable_reg(struct device *dev, unsigned int reg) 630471cd93SMichael Trimarchi { 640471cd93SMichael Trimarchi bool accessible; 650471cd93SMichael Trimarchi 660471cd93SMichael Trimarchi accessible = pcm179x_accessible_reg(dev, reg); 670471cd93SMichael Trimarchi 680471cd93SMichael Trimarchi return accessible && reg != 0x16 && reg != 0x17; 690471cd93SMichael Trimarchi } 700471cd93SMichael Trimarchi 710471cd93SMichael Trimarchi struct pcm179x_private { 720471cd93SMichael Trimarchi struct regmap *regmap; 730471cd93SMichael Trimarchi unsigned int format; 740471cd93SMichael Trimarchi unsigned int rate; 750471cd93SMichael Trimarchi }; 760471cd93SMichael Trimarchi 770471cd93SMichael Trimarchi static int pcm179x_set_dai_fmt(struct snd_soc_dai *codec_dai, 780471cd93SMichael Trimarchi unsigned int format) 790471cd93SMichael Trimarchi { 80*db0a11d0SKuninori Morimoto struct snd_soc_component *component = codec_dai->component; 81*db0a11d0SKuninori Morimoto struct pcm179x_private *priv = snd_soc_component_get_drvdata(component); 820471cd93SMichael Trimarchi 830471cd93SMichael Trimarchi priv->format = format; 840471cd93SMichael Trimarchi 850471cd93SMichael Trimarchi return 0; 860471cd93SMichael Trimarchi } 870471cd93SMichael Trimarchi 880471cd93SMichael Trimarchi static int pcm179x_digital_mute(struct snd_soc_dai *dai, int mute) 890471cd93SMichael Trimarchi { 90*db0a11d0SKuninori Morimoto struct snd_soc_component *component = dai->component; 91*db0a11d0SKuninori Morimoto struct pcm179x_private *priv = snd_soc_component_get_drvdata(component); 920471cd93SMichael Trimarchi int ret; 930471cd93SMichael Trimarchi 940471cd93SMichael Trimarchi ret = regmap_update_bits(priv->regmap, PCM179X_SOFT_MUTE, 950471cd93SMichael Trimarchi PCM179X_MUTE_MASK, !!mute); 960471cd93SMichael Trimarchi if (ret < 0) 970471cd93SMichael Trimarchi return ret; 980471cd93SMichael Trimarchi 990471cd93SMichael Trimarchi return 0; 1000471cd93SMichael Trimarchi } 1010471cd93SMichael Trimarchi 1020471cd93SMichael Trimarchi static int pcm179x_hw_params(struct snd_pcm_substream *substream, 1030471cd93SMichael Trimarchi struct snd_pcm_hw_params *params, 1040471cd93SMichael Trimarchi struct snd_soc_dai *dai) 1050471cd93SMichael Trimarchi { 106*db0a11d0SKuninori Morimoto struct snd_soc_component *component = dai->component; 107*db0a11d0SKuninori Morimoto struct pcm179x_private *priv = snd_soc_component_get_drvdata(component); 1080471cd93SMichael Trimarchi int val = 0, ret; 1090471cd93SMichael Trimarchi 1100471cd93SMichael Trimarchi priv->rate = params_rate(params); 1110471cd93SMichael Trimarchi 1120471cd93SMichael Trimarchi switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) { 1130471cd93SMichael Trimarchi case SND_SOC_DAIFMT_RIGHT_J: 1140471cd93SMichael Trimarchi switch (params_width(params)) { 1150471cd93SMichael Trimarchi case 24: 1160471cd93SMichael Trimarchi case 32: 1170471cd93SMichael Trimarchi val = 2; 1180471cd93SMichael Trimarchi break; 1190471cd93SMichael Trimarchi case 16: 1200471cd93SMichael Trimarchi val = 0; 1210471cd93SMichael Trimarchi break; 1220471cd93SMichael Trimarchi default: 1230471cd93SMichael Trimarchi return -EINVAL; 1240471cd93SMichael Trimarchi } 1250471cd93SMichael Trimarchi break; 1260471cd93SMichael Trimarchi case SND_SOC_DAIFMT_I2S: 1270471cd93SMichael Trimarchi switch (params_width(params)) { 1280471cd93SMichael Trimarchi case 24: 1290471cd93SMichael Trimarchi case 32: 1300471cd93SMichael Trimarchi val = 5; 1310471cd93SMichael Trimarchi break; 1320471cd93SMichael Trimarchi case 16: 1330471cd93SMichael Trimarchi val = 4; 1340471cd93SMichael Trimarchi break; 1350471cd93SMichael Trimarchi default: 1360471cd93SMichael Trimarchi return -EINVAL; 1370471cd93SMichael Trimarchi } 1380471cd93SMichael Trimarchi break; 1390471cd93SMichael Trimarchi default: 140*db0a11d0SKuninori Morimoto dev_err(component->dev, "Invalid DAI format\n"); 1410471cd93SMichael Trimarchi return -EINVAL; 1420471cd93SMichael Trimarchi } 1430471cd93SMichael Trimarchi 1440471cd93SMichael Trimarchi val = val << PCM179X_FMT_SHIFT | PCM179X_ATLD_ENABLE; 1450471cd93SMichael Trimarchi 1460471cd93SMichael Trimarchi ret = regmap_update_bits(priv->regmap, PCM179X_FMT_CONTROL, 1470471cd93SMichael Trimarchi PCM179X_FMT_MASK | PCM179X_ATLD_ENABLE, val); 1480471cd93SMichael Trimarchi if (ret < 0) 1490471cd93SMichael Trimarchi return ret; 1500471cd93SMichael Trimarchi 1510471cd93SMichael Trimarchi return 0; 1520471cd93SMichael Trimarchi } 1530471cd93SMichael Trimarchi 1540471cd93SMichael Trimarchi static const struct snd_soc_dai_ops pcm179x_dai_ops = { 1550471cd93SMichael Trimarchi .set_fmt = pcm179x_set_dai_fmt, 1560471cd93SMichael Trimarchi .hw_params = pcm179x_hw_params, 1570471cd93SMichael Trimarchi .digital_mute = pcm179x_digital_mute, 1580471cd93SMichael Trimarchi }; 1590471cd93SMichael Trimarchi 1600471cd93SMichael Trimarchi static const DECLARE_TLV_DB_SCALE(pcm179x_dac_tlv, -12000, 50, 1); 1610471cd93SMichael Trimarchi 1620471cd93SMichael Trimarchi static const struct snd_kcontrol_new pcm179x_controls[] = { 1630471cd93SMichael Trimarchi SOC_DOUBLE_R_RANGE_TLV("DAC Playback Volume", PCM179X_DAC_VOL_LEFT, 1640471cd93SMichael Trimarchi PCM179X_DAC_VOL_RIGHT, 0, 0xf, 0xff, 0, 1650471cd93SMichael Trimarchi pcm179x_dac_tlv), 1660471cd93SMichael Trimarchi SOC_SINGLE("DAC Invert Output Switch", PCM179X_MODE_CONTROL, 7, 1, 0), 1670471cd93SMichael Trimarchi SOC_SINGLE("DAC Rolloff Filter Switch", PCM179X_MODE_CONTROL, 1, 1, 0), 1680471cd93SMichael Trimarchi }; 1690471cd93SMichael Trimarchi 1700471cd93SMichael Trimarchi static const struct snd_soc_dapm_widget pcm179x_dapm_widgets[] = { 1710471cd93SMichael Trimarchi SND_SOC_DAPM_OUTPUT("IOUTL+"), 1720471cd93SMichael Trimarchi SND_SOC_DAPM_OUTPUT("IOUTL-"), 1730471cd93SMichael Trimarchi SND_SOC_DAPM_OUTPUT("IOUTR+"), 1740471cd93SMichael Trimarchi SND_SOC_DAPM_OUTPUT("IOUTR-"), 1750471cd93SMichael Trimarchi }; 1760471cd93SMichael Trimarchi 1770471cd93SMichael Trimarchi static const struct snd_soc_dapm_route pcm179x_dapm_routes[] = { 1780471cd93SMichael Trimarchi { "IOUTL+", NULL, "Playback" }, 1790471cd93SMichael Trimarchi { "IOUTL-", NULL, "Playback" }, 1800471cd93SMichael Trimarchi { "IOUTR+", NULL, "Playback" }, 1810471cd93SMichael Trimarchi { "IOUTR-", NULL, "Playback" }, 1820471cd93SMichael Trimarchi }; 1830471cd93SMichael Trimarchi 1840471cd93SMichael Trimarchi static struct snd_soc_dai_driver pcm179x_dai = { 1850471cd93SMichael Trimarchi .name = "pcm179x-hifi", 1860471cd93SMichael Trimarchi .playback = { 1870471cd93SMichael Trimarchi .stream_name = "Playback", 1880471cd93SMichael Trimarchi .channels_min = 2, 1890471cd93SMichael Trimarchi .channels_max = 2, 190e0236f54SJacob Siverskog .rates = SNDRV_PCM_RATE_CONTINUOUS, 191e0236f54SJacob Siverskog .rate_min = 10000, 192e0236f54SJacob Siverskog .rate_max = 200000, 1930471cd93SMichael Trimarchi .formats = PCM1792A_FORMATS, }, 1940471cd93SMichael Trimarchi .ops = &pcm179x_dai_ops, 1950471cd93SMichael Trimarchi }; 1960471cd93SMichael Trimarchi 1971b347b68SJacob Siverskog const struct regmap_config pcm179x_regmap_config = { 1980471cd93SMichael Trimarchi .reg_bits = 8, 1990471cd93SMichael Trimarchi .val_bits = 8, 2000471cd93SMichael Trimarchi .max_register = 23, 2010471cd93SMichael Trimarchi .reg_defaults = pcm179x_reg_defaults, 2020471cd93SMichael Trimarchi .num_reg_defaults = ARRAY_SIZE(pcm179x_reg_defaults), 2030471cd93SMichael Trimarchi .writeable_reg = pcm179x_writeable_reg, 2040471cd93SMichael Trimarchi .readable_reg = pcm179x_accessible_reg, 2050471cd93SMichael Trimarchi }; 2061b347b68SJacob Siverskog EXPORT_SYMBOL_GPL(pcm179x_regmap_config); 2070471cd93SMichael Trimarchi 208*db0a11d0SKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_pcm179x = { 2090471cd93SMichael Trimarchi .controls = pcm179x_controls, 2100471cd93SMichael Trimarchi .num_controls = ARRAY_SIZE(pcm179x_controls), 2110471cd93SMichael Trimarchi .dapm_widgets = pcm179x_dapm_widgets, 2120471cd93SMichael Trimarchi .num_dapm_widgets = ARRAY_SIZE(pcm179x_dapm_widgets), 2130471cd93SMichael Trimarchi .dapm_routes = pcm179x_dapm_routes, 2140471cd93SMichael Trimarchi .num_dapm_routes = ARRAY_SIZE(pcm179x_dapm_routes), 215*db0a11d0SKuninori Morimoto .idle_bias_on = 1, 216*db0a11d0SKuninori Morimoto .use_pmdown_time = 1, 217*db0a11d0SKuninori Morimoto .endianness = 1, 218*db0a11d0SKuninori Morimoto .non_legacy_dai_naming = 1, 2190471cd93SMichael Trimarchi }; 2200471cd93SMichael Trimarchi 2211b347b68SJacob Siverskog int pcm179x_common_init(struct device *dev, struct regmap *regmap) 2220471cd93SMichael Trimarchi { 2230471cd93SMichael Trimarchi struct pcm179x_private *pcm179x; 2240471cd93SMichael Trimarchi 2251b347b68SJacob Siverskog pcm179x = devm_kzalloc(dev, sizeof(struct pcm179x_private), 2260471cd93SMichael Trimarchi GFP_KERNEL); 2270471cd93SMichael Trimarchi if (!pcm179x) 2280471cd93SMichael Trimarchi return -ENOMEM; 2290471cd93SMichael Trimarchi 2301b347b68SJacob Siverskog pcm179x->regmap = regmap; 2311b347b68SJacob Siverskog dev_set_drvdata(dev, pcm179x); 2320471cd93SMichael Trimarchi 233*db0a11d0SKuninori Morimoto return devm_snd_soc_register_component(dev, 234*db0a11d0SKuninori Morimoto &soc_component_dev_pcm179x, &pcm179x_dai, 1); 2350471cd93SMichael Trimarchi } 2361b347b68SJacob Siverskog EXPORT_SYMBOL_GPL(pcm179x_common_init); 2370471cd93SMichael Trimarchi 2380471cd93SMichael Trimarchi MODULE_DESCRIPTION("ASoC PCM179X driver"); 2390471cd93SMichael Trimarchi MODULE_AUTHOR("Michael Trimarchi <michael@amarulasolutions.com>"); 2400471cd93SMichael Trimarchi MODULE_LICENSE("GPL"); 241