Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / sound / soc / codecs / i-sabre-codec.c
1 /*
2  * Driver for I-Sabre Q2M
3  *
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
9  *
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.
13  *
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.
18  */
19
20
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>
28
29 #include "i-sabre-codec.h"
30
31
32 /* I-Sabre Q2M Codec Private Data */
33 struct i_sabre_codec_priv {
34         struct regmap *regmap;
35         unsigned int fmt;
36 };
37
38
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 },
46 };
47
48
49 static bool i_sabre_codec_writeable(struct device *dev, unsigned int reg)
50 {
51         switch (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:
57                 return true;
58
59         default:
60                 return false;
61         }
62 }
63
64 static bool i_sabre_codec_readable(struct device *dev, unsigned int reg)
65 {
66         switch (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:
74                 return true;
75
76         default:
77                 return false;
78         }
79 }
80
81 static bool i_sabre_codec_volatile(struct device *dev, unsigned int reg)
82 {
83         switch (reg) {
84         case ISABRECODEC_REG_01:
85         case ISABRECODEC_REG_02:
86                 return true;
87
88         default:
89                 return false;
90         }
91 }
92
93
94 /* Volume Scale */
95 static const DECLARE_TLV_DB_SCALE(volume_tlv, -10000, 100, 0);
96
97
98 /* Filter Type */
99 static const char * const fir_filter_type_texts[] = {
100         "brick wall",
101         "corrected minimum phase fast",
102         "minimum phase slow",
103         "minimum phase fast",
104         "linear phase slow",
105         "linear phase fast",
106         "apodizing fast",
107 };
108
109 static SOC_ENUM_SINGLE_DECL(i_sabre_fir_filter_type_enum,
110                                 ISABRECODEC_REG_22, 0, fir_filter_type_texts);
111
112
113 /* I2S / SPDIF Select */
114 static const char * const iis_spdif_sel_texts[] = {
115         "I2S",
116         "SPDIF",
117 };
118
119 static SOC_ENUM_SINGLE_DECL(i_sabre_iis_spdif_sel_enum,
120                                 ISABRECODEC_REG_24, 0, iis_spdif_sel_texts);
121
122
123 /* Control */
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),
129 };
130
131
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
137 };
138
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),
142 };
143
144 static int i_sabre_codec_dai_startup_slave(
145                 struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
146 {
147         struct snd_soc_component *component = dai->component;
148         int ret;
149
150         ret = snd_pcm_hw_constraint_list(substream->runtime,
151                         0, SNDRV_PCM_HW_PARAM_RATE, &constraints_slave);
152         if (ret != 0) {
153                 dev_err(component->card->dev, "Failed to setup rates constraints: %d\n", ret);
154         }
155
156         return ret;
157 }
158
159 static int i_sabre_codec_dai_startup(
160                 struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
161 {
162         struct snd_soc_component      *component = dai->component;
163         struct i_sabre_codec_priv *i_sabre_codec
164                                         = snd_soc_component_get_drvdata(component);
165
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);
169
170         default:
171                 return (-EINVAL);
172         }
173 }
174
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)
178 {
179         struct snd_soc_component      *component = dai->component;
180         struct i_sabre_codec_priv *i_sabre_codec
181                                         = snd_soc_component_get_drvdata(component);
182         unsigned int daifmt;
183         int format_width;
184
185         dev_dbg(component->card->dev, "hw_params %u Hz, %u channels\n",
186                         params_rate(params), params_channels(params));
187
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)));
193                 return (-EINVAL);
194         }
195
196         /* Check Slave Mode */
197         daifmt = i_sabre_codec->fmt & SND_SOC_DAIFMT_MASTER_MASK;
198         if (daifmt != SND_SOC_DAIFMT_CBS_CFS) {
199                 return (-EINVAL);
200         }
201
202         /* Notify Sampling Frequency  */
203         switch (params_rate(params))
204         {
205         case 44100:
206         case 48000:
207         case 88200:
208         case 96000:
209         case 176400:
210         case 192000:
211                 snd_soc_component_update_bits(component, ISABRECODEC_REG_10, 0x01, 0x00);
212                 break;
213
214         case 352800:
215         case 384000:
216         case 705600:
217         case 768000:
218         case 1411200:
219         case 1536000:
220                 snd_soc_component_update_bits(component, ISABRECODEC_REG_10, 0x01, 0x01);
221                 break;
222         }
223
224         return 0;
225 }
226
227 static int i_sabre_codec_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
228 {
229         struct snd_soc_component      *component = dai->component;
230         struct i_sabre_codec_priv *i_sabre_codec
231                                         = snd_soc_component_get_drvdata(component);
232
233         /* interface format */
234         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
235         case SND_SOC_DAIFMT_I2S:
236                 break;
237
238         case SND_SOC_DAIFMT_RIGHT_J:
239         case SND_SOC_DAIFMT_LEFT_J:
240         default:
241                 return (-EINVAL);
242         }
243
244         /* clock inversion */
245         if ((fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF) {
246                 return (-EINVAL);
247         }
248
249         /* Set Audio Data Format */
250         i_sabre_codec->fmt = fmt;
251
252         return 0;
253 }
254
255 static int i_sabre_codec_dac_mute(struct snd_soc_dai *dai, int mute, int direction)
256 {
257         struct snd_soc_component *component = dai->component;
258
259         if (mute) {
260                 snd_soc_component_update_bits(component, ISABRECODEC_REG_21, 0x01, 0x01);
261         } else {
262                 snd_soc_component_update_bits(component, ISABRECODEC_REG_21, 0x01, 0x00);
263         }
264
265         return 0;
266 }
267
268
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,
274 };
275
276 static struct snd_soc_dai_driver i_sabre_codec_dai = {
277         .name = "i-sabre-codec-dai",
278         .playback = {
279                 .stream_name  = "Playback",
280                 .channels_min = 2,
281                 .channels_max = 2,
282                 .rates = SNDRV_PCM_RATE_CONTINUOUS,
283                 .rate_min = 8000,
284                 .rate_max = 1536000,
285                 .formats      = SNDRV_PCM_FMTBIT_S16_LE
286                                 | SNDRV_PCM_FMTBIT_S32_LE,
287         },
288         .ops = &i_sabre_codec_dai_ops,
289 };
290
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),
294 };
295
296
297 static const struct regmap_config i_sabre_codec_regmap = {
298         .reg_bits         = 8,
299         .val_bits         = 8,
300         .max_register     = ISABRECODEC_MAX_REG,
301
302         .reg_defaults     = i_sabre_codec_reg_defaults,
303         .num_reg_defaults = ARRAY_SIZE(i_sabre_codec_reg_defaults),
304
305         .writeable_reg    = i_sabre_codec_writeable,
306         .readable_reg     = i_sabre_codec_readable,
307         .volatile_reg     = i_sabre_codec_volatile,
308
309         .cache_type       = REGCACHE_RBTREE,
310 };
311
312
313 static int i_sabre_codec_probe(struct device *dev, struct regmap *regmap)
314 {
315         struct i_sabre_codec_priv *i_sabre_codec;
316         int ret;
317
318         i_sabre_codec = devm_kzalloc(dev, sizeof(*i_sabre_codec), GFP_KERNEL);
319         if (!i_sabre_codec) {
320                 dev_err(dev, "devm_kzalloc");
321                 return (-ENOMEM);
322         }
323
324         i_sabre_codec->regmap = regmap;
325
326         dev_set_drvdata(dev, i_sabre_codec);
327
328         ret = snd_soc_register_component(dev,
329                         &i_sabre_codec_codec_driver, &i_sabre_codec_dai, 1);
330         if (ret != 0) {
331                 dev_err(dev, "Failed to register CODEC: %d\n", ret);
332                 return ret;
333         }
334
335         return 0;
336 }
337
338 static void i_sabre_codec_remove(struct device *dev)
339 {
340         snd_soc_unregister_component(dev);
341 }
342
343
344 static int i_sabre_codec_i2c_probe(
345                 struct i2c_client *i2c, const struct i2c_device_id *id)
346 {
347         struct regmap *regmap;
348
349         regmap = devm_regmap_init_i2c(i2c, &i_sabre_codec_regmap);
350         if (IS_ERR(regmap)) {
351                 return PTR_ERR(regmap);
352         }
353
354         return i_sabre_codec_probe(&i2c->dev, regmap);
355 }
356
357 static int i_sabre_codec_i2c_remove(struct i2c_client *i2c)
358 {
359         i_sabre_codec_remove(&i2c->dev);
360
361         return 0;
362 }
363
364
365 static const struct i2c_device_id i_sabre_codec_i2c_id[] = {
366         { "i-sabre-codec", },
367         { }
368 };
369 MODULE_DEVICE_TABLE(i2c, i_sabre_codec_i2c_id);
370
371 static const struct of_device_id i_sabre_codec_of_match[] = {
372         { .compatible = "audiophonics,i-sabre-codec", },
373         { }
374 };
375 MODULE_DEVICE_TABLE(of, i_sabre_codec_of_match);
376
377 static struct i2c_driver i_sabre_codec_i2c_driver = {
378         .driver = {
379                 .name           = "i-sabre-codec-i2c",
380                 .owner          = THIS_MODULE,
381                 .of_match_table = of_match_ptr(i_sabre_codec_of_match),
382         },
383         .probe    = i_sabre_codec_i2c_probe,
384         .remove   = i_sabre_codec_i2c_remove,
385         .id_table = i_sabre_codec_i2c_id,
386 };
387 module_i2c_driver(i_sabre_codec_i2c_driver);
388
389
390 MODULE_DESCRIPTION("ASoC I-Sabre Q2M codec driver");
391 MODULE_AUTHOR("Audiophonics <http://www.audiophonics.fr>");
392 MODULE_LICENSE("GPL");