2 * Driver for I-Sabre Q2M
4 * Author: Satoru Kawase
5 * Modified by: Xiao Qingyong
6 * Modified by: JC BARBAUD (Mute)
7 * Update kernel v4.18+ by : Audiophonics
8 * Copyright 2018 Audiophonics
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/regmap.h>
24 #include <linux/i2c.h>
25 #include <sound/soc.h>
26 #include <sound/pcm_params.h>
27 #include <sound/tlv.h>
29 #include "i-sabre-codec.h"
32 /* I-Sabre Q2M Codec Private Data */
33 struct i_sabre_codec_priv {
34 struct regmap *regmap;
39 /* I-Sabre Q2M Codec Default Register Value */
40 static const struct reg_default i_sabre_codec_reg_defaults[] = {
41 { ISABRECODEC_REG_10, 0x00 },
42 { ISABRECODEC_REG_20, 0x00 },
43 { ISABRECODEC_REG_21, 0x00 },
44 { ISABRECODEC_REG_22, 0x00 },
45 { ISABRECODEC_REG_24, 0x00 },
49 static bool i_sabre_codec_writeable(struct device *dev, unsigned int reg)
52 case ISABRECODEC_REG_10:
53 case ISABRECODEC_REG_20:
54 case ISABRECODEC_REG_21:
55 case ISABRECODEC_REG_22:
56 case ISABRECODEC_REG_24:
64 static bool i_sabre_codec_readable(struct device *dev, unsigned int reg)
67 case ISABRECODEC_REG_01:
68 case ISABRECODEC_REG_02:
69 case ISABRECODEC_REG_10:
70 case ISABRECODEC_REG_20:
71 case ISABRECODEC_REG_21:
72 case ISABRECODEC_REG_22:
73 case ISABRECODEC_REG_24:
81 static bool i_sabre_codec_volatile(struct device *dev, unsigned int reg)
84 case ISABRECODEC_REG_01:
85 case ISABRECODEC_REG_02:
95 static const DECLARE_TLV_DB_SCALE(volume_tlv, -10000, 100, 0);
99 static const char * const fir_filter_type_texts[] = {
101 "corrected minimum phase fast",
102 "minimum phase slow",
103 "minimum phase fast",
109 static SOC_ENUM_SINGLE_DECL(i_sabre_fir_filter_type_enum,
110 ISABRECODEC_REG_22, 0, fir_filter_type_texts);
113 /* I2S / SPDIF Select */
114 static const char * const iis_spdif_sel_texts[] = {
119 static SOC_ENUM_SINGLE_DECL(i_sabre_iis_spdif_sel_enum,
120 ISABRECODEC_REG_24, 0, iis_spdif_sel_texts);
124 static const struct snd_kcontrol_new i_sabre_codec_controls[] = {
125 SOC_SINGLE_RANGE_TLV("Digital Playback Volume", ISABRECODEC_REG_20, 0, 0, 100, 1, volume_tlv),
126 SOC_SINGLE("Digital Playback Switch", ISABRECODEC_REG_21, 0, 1, 1),
127 SOC_ENUM("FIR Filter Type", i_sabre_fir_filter_type_enum),
128 SOC_ENUM("I2S/SPDIF Select", i_sabre_iis_spdif_sel_enum),
132 static const u32 i_sabre_codec_dai_rates_slave[] = {
133 8000, 11025, 16000, 22050, 32000,
134 44100, 48000, 64000, 88200, 96000,
135 176400, 192000, 352800, 384000,
136 705600, 768000, 1411200, 1536000
139 static const struct snd_pcm_hw_constraint_list constraints_slave = {
140 .list = i_sabre_codec_dai_rates_slave,
141 .count = ARRAY_SIZE(i_sabre_codec_dai_rates_slave),
144 static int i_sabre_codec_dai_startup_slave(
145 struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
147 struct snd_soc_component *component = dai->component;
150 ret = snd_pcm_hw_constraint_list(substream->runtime,
151 0, SNDRV_PCM_HW_PARAM_RATE, &constraints_slave);
153 dev_err(component->card->dev, "Failed to setup rates constraints: %d\n", ret);
159 static int i_sabre_codec_dai_startup(
160 struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
162 struct snd_soc_component *component = dai->component;
163 struct i_sabre_codec_priv *i_sabre_codec
164 = snd_soc_component_get_drvdata(component);
166 switch (i_sabre_codec->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
167 case SND_SOC_DAIFMT_CBS_CFS:
168 return i_sabre_codec_dai_startup_slave(substream, dai);
175 static int i_sabre_codec_hw_params(
176 struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params,
177 struct snd_soc_dai *dai)
179 struct snd_soc_component *component = dai->component;
180 struct i_sabre_codec_priv *i_sabre_codec
181 = snd_soc_component_get_drvdata(component);
185 dev_dbg(component->card->dev, "hw_params %u Hz, %u channels\n",
186 params_rate(params), params_channels(params));
188 /* Check I2S Format (Bit Size) */
189 format_width = snd_pcm_format_width(params_format(params));
190 if ((format_width != 32) && (format_width != 16)) {
191 dev_err(component->card->dev, "Bad frame size: %d\n",
192 snd_pcm_format_width(params_format(params)));
196 /* Check Slave Mode */
197 daifmt = i_sabre_codec->fmt & SND_SOC_DAIFMT_MASTER_MASK;
198 if (daifmt != SND_SOC_DAIFMT_CBS_CFS) {
202 /* Notify Sampling Frequency */
203 switch (params_rate(params))
211 snd_soc_component_update_bits(component, ISABRECODEC_REG_10, 0x01, 0x00);
220 snd_soc_component_update_bits(component, ISABRECODEC_REG_10, 0x01, 0x01);
227 static int i_sabre_codec_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
229 struct snd_soc_component *component = dai->component;
230 struct i_sabre_codec_priv *i_sabre_codec
231 = snd_soc_component_get_drvdata(component);
233 /* interface format */
234 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
235 case SND_SOC_DAIFMT_I2S:
238 case SND_SOC_DAIFMT_RIGHT_J:
239 case SND_SOC_DAIFMT_LEFT_J:
244 /* clock inversion */
245 if ((fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF) {
249 /* Set Audio Data Format */
250 i_sabre_codec->fmt = fmt;
255 static int i_sabre_codec_dac_mute(struct snd_soc_dai *dai, int mute, int direction)
257 struct snd_soc_component *component = dai->component;
260 snd_soc_component_update_bits(component, ISABRECODEC_REG_21, 0x01, 0x01);
262 snd_soc_component_update_bits(component, ISABRECODEC_REG_21, 0x01, 0x00);
269 static const struct snd_soc_dai_ops i_sabre_codec_dai_ops = {
270 .startup = i_sabre_codec_dai_startup,
271 .hw_params = i_sabre_codec_hw_params,
272 .set_fmt = i_sabre_codec_set_fmt,
273 .mute_stream = i_sabre_codec_dac_mute,
276 static struct snd_soc_dai_driver i_sabre_codec_dai = {
277 .name = "i-sabre-codec-dai",
279 .stream_name = "Playback",
282 .rates = SNDRV_PCM_RATE_CONTINUOUS,
285 .formats = SNDRV_PCM_FMTBIT_S16_LE
286 | SNDRV_PCM_FMTBIT_S32_LE,
288 .ops = &i_sabre_codec_dai_ops,
291 static struct snd_soc_component_driver i_sabre_codec_codec_driver = {
292 .controls = i_sabre_codec_controls,
293 .num_controls = ARRAY_SIZE(i_sabre_codec_controls),
297 static const struct regmap_config i_sabre_codec_regmap = {
300 .max_register = ISABRECODEC_MAX_REG,
302 .reg_defaults = i_sabre_codec_reg_defaults,
303 .num_reg_defaults = ARRAY_SIZE(i_sabre_codec_reg_defaults),
305 .writeable_reg = i_sabre_codec_writeable,
306 .readable_reg = i_sabre_codec_readable,
307 .volatile_reg = i_sabre_codec_volatile,
309 .cache_type = REGCACHE_RBTREE,
313 static int i_sabre_codec_probe(struct device *dev, struct regmap *regmap)
315 struct i_sabre_codec_priv *i_sabre_codec;
318 i_sabre_codec = devm_kzalloc(dev, sizeof(*i_sabre_codec), GFP_KERNEL);
319 if (!i_sabre_codec) {
320 dev_err(dev, "devm_kzalloc");
324 i_sabre_codec->regmap = regmap;
326 dev_set_drvdata(dev, i_sabre_codec);
328 ret = snd_soc_register_component(dev,
329 &i_sabre_codec_codec_driver, &i_sabre_codec_dai, 1);
331 dev_err(dev, "Failed to register CODEC: %d\n", ret);
338 static void i_sabre_codec_remove(struct device *dev)
340 snd_soc_unregister_component(dev);
344 static int i_sabre_codec_i2c_probe(
345 struct i2c_client *i2c, const struct i2c_device_id *id)
347 struct regmap *regmap;
349 regmap = devm_regmap_init_i2c(i2c, &i_sabre_codec_regmap);
350 if (IS_ERR(regmap)) {
351 return PTR_ERR(regmap);
354 return i_sabre_codec_probe(&i2c->dev, regmap);
357 static int i_sabre_codec_i2c_remove(struct i2c_client *i2c)
359 i_sabre_codec_remove(&i2c->dev);
365 static const struct i2c_device_id i_sabre_codec_i2c_id[] = {
366 { "i-sabre-codec", },
369 MODULE_DEVICE_TABLE(i2c, i_sabre_codec_i2c_id);
371 static const struct of_device_id i_sabre_codec_of_match[] = {
372 { .compatible = "audiophonics,i-sabre-codec", },
375 MODULE_DEVICE_TABLE(of, i_sabre_codec_of_match);
377 static struct i2c_driver i_sabre_codec_i2c_driver = {
379 .name = "i-sabre-codec-i2c",
380 .owner = THIS_MODULE,
381 .of_match_table = of_match_ptr(i_sabre_codec_of_match),
383 .probe = i_sabre_codec_i2c_probe,
384 .remove = i_sabre_codec_i2c_remove,
385 .id_table = i_sabre_codec_i2c_id,
387 module_i2c_driver(i_sabre_codec_i2c_driver);
390 MODULE_DESCRIPTION("ASoC I-Sabre Q2M codec driver");
391 MODULE_AUTHOR("Audiophonics <http://www.audiophonics.fr>");
392 MODULE_LICENSE("GPL");