brcm2708: add linux 4.19 support
[openwrt/staging/chunkeey.git] / target / linux / brcm2708 / patches-4.19 / 950-0416-Audiophonics-I-Sabre-9038Q2M-DAC-driver.patch
1 From b40f72a9aeee6ce7d7df3a3b3ff602234215fe79 Mon Sep 17 00:00:00 2001
2 From: FERHAT Nicolas <contact@audiophonics.fr>
3 Date: Fri, 5 Apr 2019 13:06:42 +0100
4 Subject: [PATCH 416/703] Audiophonics I-Sabre 9038Q2M DAC driver
5
6 Signed-off-by: Audiophonics <contact@audiophonics.fr>
7 ---
8 arch/arm/boot/dts/overlays/Makefile | 1 +
9 arch/arm/boot/dts/overlays/README | 6 +
10 .../boot/dts/overlays/i-sabre-q2m-overlay.dts | 39 ++
11 arch/arm/configs/bcm2709_defconfig | 1 +
12 arch/arm/configs/bcmrpi_defconfig | 1 +
13 arch/arm64/configs/bcmrpi3_defconfig | 1 +
14 sound/soc/bcm/Kconfig | 7 +
15 sound/soc/bcm/Makefile | 2 +
16 sound/soc/bcm/i-sabre-q2m.c | 157 +++++++
17 sound/soc/codecs/Kconfig | 5 +
18 sound/soc/codecs/Makefile | 2 +
19 sound/soc/codecs/i-sabre-codec.c | 392 ++++++++++++++++++
20 sound/soc/codecs/i-sabre-codec.h | 42 ++
21 13 files changed, 656 insertions(+)
22 create mode 100644 arch/arm/boot/dts/overlays/i-sabre-q2m-overlay.dts
23 create mode 100644 sound/soc/bcm/i-sabre-q2m.c
24 create mode 100644 sound/soc/codecs/i-sabre-codec.c
25 create mode 100644 sound/soc/codecs/i-sabre-codec.h
26
27 --- a/arch/arm/boot/dts/overlays/Makefile
28 +++ b/arch/arm/boot/dts/overlays/Makefile
29 @@ -55,6 +55,7 @@ dtbo-$(CONFIG_ARCH_BCM2835) += \
30 hy28a.dtbo \
31 hy28b.dtbo \
32 hy28b-2017.dtbo \
33 + i-sabre-q2m.dtbo \
34 i2c-bcm2708.dtbo \
35 i2c-gpio.dtbo \
36 i2c-mux.dtbo \
37 --- a/arch/arm/boot/dts/overlays/README
38 +++ b/arch/arm/boot/dts/overlays/README
39 @@ -869,6 +869,12 @@ Params: speed Display
40 ledgpio GPIO used to control backlight
41
42
43 +Name: i-sabre-q2m
44 +Info: Configures the Audiophonics I-SABRE Q2M DAC
45 +Load: dtoverlay=i-sabre-q2m
46 +Params: <None>
47 +
48 +
49 Name: i2c-bcm2708
50 Info: Fall back to the i2c_bcm2708 driver for the i2c_arm bus.
51 Load: dtoverlay=i2c-bcm2708
52 --- /dev/null
53 +++ b/arch/arm/boot/dts/overlays/i-sabre-q2m-overlay.dts
54 @@ -0,0 +1,39 @@
55 +// Definitions for I-Sabre Q2M
56 +/dts-v1/;
57 +/plugin/;
58 +
59 +/ {
60 + compatible = "brcm,bcm2708";
61 +
62 + fragment@0 {
63 + target = <&sound>;
64 + frag0: __overlay__ {
65 + compatible = "audiophonics,i-sabre-q2m";
66 + i2s-controller = <&i2s>;
67 + status = "okay";
68 + };
69 + };
70 +
71 + fragment@1 {
72 + target = <&i2s>;
73 + __overlay__ {
74 + status = "okay";
75 + };
76 + };
77 +
78 + fragment@2 {
79 + target = <&i2c1>;
80 + __overlay__ {
81 + #address-cells = <1>;
82 + #size-cells = <0>;
83 + status = "okay";
84 +
85 + i-sabre-codec@48 {
86 + #sound-dai-cells = <0>;
87 + compatible = "audiophonics,i-sabre-codec";
88 + reg = <0x48>;
89 + status = "okay";
90 + };
91 + };
92 + };
93 +};
94 --- a/arch/arm/configs/bcm2709_defconfig
95 +++ b/arch/arm/configs/bcm2709_defconfig
96 @@ -916,6 +916,7 @@ CONFIG_SND_BCM2708_SOC_JUSTBOOM_DAC=m
97 CONFIG_SND_BCM2708_SOC_JUSTBOOM_DIGI=m
98 CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC=m
99 CONFIG_SND_BCM2708_SOC_IQAUDIO_DIGI=m
100 +CONFIG_SND_BCM2708_SOC_I_SABRE_Q2M=m
101 CONFIG_SND_BCM2708_SOC_ADAU1977_ADC=m
102 CONFIG_SND_AUDIOINJECTOR_PI_SOUNDCARD=m
103 CONFIG_SND_AUDIOINJECTOR_OCTO_SOUNDCARD=m
104 --- a/arch/arm/configs/bcmrpi_defconfig
105 +++ b/arch/arm/configs/bcmrpi_defconfig
106 @@ -909,6 +909,7 @@ CONFIG_SND_BCM2708_SOC_JUSTBOOM_DAC=m
107 CONFIG_SND_BCM2708_SOC_JUSTBOOM_DIGI=m
108 CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC=m
109 CONFIG_SND_BCM2708_SOC_IQAUDIO_DIGI=m
110 +CONFIG_SND_BCM2708_SOC_I_SABRE_Q2M=m
111 CONFIG_SND_BCM2708_SOC_ADAU1977_ADC=m
112 CONFIG_SND_AUDIOINJECTOR_PI_SOUNDCARD=m
113 CONFIG_SND_AUDIOINJECTOR_OCTO_SOUNDCARD=m
114 --- a/arch/arm64/configs/bcmrpi3_defconfig
115 +++ b/arch/arm64/configs/bcmrpi3_defconfig
116 @@ -804,6 +804,7 @@ CONFIG_SND_BCM2708_SOC_JUSTBOOM_DAC=m
117 CONFIG_SND_BCM2708_SOC_JUSTBOOM_DIGI=m
118 CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC=m
119 CONFIG_SND_BCM2708_SOC_IQAUDIO_DIGI=m
120 +CONFIG_SND_BCM2708_SOC_I_SABRE_Q2M=m
121 CONFIG_SND_BCM2708_SOC_ADAU1977_ADC=m
122 CONFIG_SND_AUDIOINJECTOR_PI_SOUNDCARD=m
123 CONFIG_SND_AUDIOSENSE_PI=m
124 --- a/sound/soc/bcm/Kconfig
125 +++ b/sound/soc/bcm/Kconfig
126 @@ -123,6 +123,13 @@ config SND_BCM2708_SOC_IQAUDIO_DIGI
127 help
128 Say Y or M if you want to add support for IQAudIO Digital IO board.
129
130 +config SND_BCM2708_SOC_I_SABRE_Q2M
131 + tristate "Support for Audiophonics I-Sabre Q2M DAC"
132 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
133 + select SND_SOC_I_SABRE_CODEC
134 + help
135 + Say Y or M if you want to add support for Audiophonics I-SABRE Q2M DAC
136 +
137 config SND_BCM2708_SOC_ADAU1977_ADC
138 tristate "Support for ADAU1977 ADC"
139 depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
140 --- a/sound/soc/bcm/Makefile
141 +++ b/sound/soc/bcm/Makefile
142 @@ -19,6 +19,7 @@ snd-soc-justboom-dac-objs := justboom-da
143 snd-soc-rpi-cirrus-objs := rpi-cirrus.o
144 snd-soc-rpi-proto-objs := rpi-proto.o
145 snd-soc-iqaudio-dac-objs := iqaudio-dac.o
146 + snd-soc-i-sabre-q2m-objs := i-sabre-q2m.o
147 snd-soc-audioinjector-pi-soundcard-objs := audioinjector-pi-soundcard.o
148 snd-soc-audioinjector-octo-soundcard-objs := audioinjector-octo-soundcard.o
149 snd-soc-audiosense-pi-objs := audiosense-pi.o
150 @@ -42,6 +43,7 @@ obj-$(CONFIG_SND_BCM2708_SOC_JUSTBOOM_DA
151 obj-$(CONFIG_SND_BCM2708_SOC_RPI_CIRRUS) += snd-soc-rpi-cirrus.o
152 obj-$(CONFIG_SND_BCM2708_SOC_RPI_PROTO) += snd-soc-rpi-proto.o
153 obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) += snd-soc-iqaudio-dac.o
154 + obj-$(CONFIG_SND_BCM2708_SOC_I_SABRE_Q2M) += snd-soc-i-sabre-q2m.o
155 obj-$(CONFIG_SND_AUDIOINJECTOR_PI_SOUNDCARD) += snd-soc-audioinjector-pi-soundcard.o
156 obj-$(CONFIG_SND_AUDIOINJECTOR_OCTO_SOUNDCARD) += snd-soc-audioinjector-octo-soundcard.o
157 obj-$(CONFIG_SND_AUDIOSENSE_PI) += snd-soc-audiosense-pi.o
158 --- /dev/null
159 +++ b/sound/soc/bcm/i-sabre-q2m.c
160 @@ -0,0 +1,157 @@
161 +/*
162 + * ASoC Driver for I-Sabre Q2M
163 + *
164 + * Author: Satoru Kawase
165 + * Modified by: Xiao Qingyong
166 + * Update kernel v4.18+ by : Audiophonics
167 + * Copyright 2018 Audiophonics
168 + *
169 + * This program is free software; you can redistribute it and/or
170 + * modify it under the terms of the GNU General Public License
171 + * version 2 as published by the Free Software Foundation.
172 + *
173 + * This program is distributed in the hope that it will be useful, but
174 + * WITHOUT ANY WARRANTY; without even the implied warranty of
175 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
176 + * General Public License for more details.
177 + */
178 +
179 +#include <linux/kernel.h>
180 +#include <linux/init.h>
181 +#include <linux/module.h>
182 +#include <linux/delay.h>
183 +#include <linux/fs.h>
184 +#include <asm/uaccess.h>
185 +#include <sound/core.h>
186 +#include <sound/soc.h>
187 +#include <sound/pcm.h>
188 +#include <sound/pcm_params.h>
189 +
190 +#include "../codecs/i-sabre-codec.h"
191 +
192 +
193 +static int snd_rpi_i_sabre_q2m_init(struct snd_soc_pcm_runtime *rtd)
194 +{
195 + struct snd_soc_component *component = rtd->codec_dai->component;
196 + unsigned int value;
197 +
198 + /* Device ID */
199 + value = snd_soc_component_read32(component, ISABRECODEC_REG_01);
200 + dev_info(component->card->dev, "Audiophonics Device ID : %02X\n", value);
201 +
202 + /* API revision */
203 + value = snd_soc_component_read32(component, ISABRECODEC_REG_02);
204 + dev_info(component->card->dev, "Audiophonics API revision : %02X\n", value);
205 +
206 + return 0;
207 +}
208 +
209 +static int snd_rpi_i_sabre_q2m_hw_params(
210 + struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
211 +{
212 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
213 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
214 + int bclk_ratio;
215 +
216 + bclk_ratio = snd_pcm_format_physical_width(
217 + params_format(params)) * params_channels(params);
218 + return snd_soc_dai_set_bclk_ratio(cpu_dai, bclk_ratio);
219 +}
220 +
221 +/* machine stream operations */
222 +static struct snd_soc_ops snd_rpi_i_sabre_q2m_ops = {
223 + .hw_params = snd_rpi_i_sabre_q2m_hw_params,
224 +};
225 +
226 +
227 +static struct snd_soc_dai_link snd_rpi_i_sabre_q2m_dai[] = {
228 + {
229 + .name = "I-Sabre Q2M",
230 + .stream_name = "I-Sabre Q2M DAC",
231 + .cpu_dai_name = "bcm2708-i2s.0",
232 + .codec_dai_name = "i-sabre-codec-dai",
233 + .platform_name = "bcm2708-i2s.0",
234 + .codec_name = "i-sabre-codec-i2c.1-0048",
235 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
236 + | SND_SOC_DAIFMT_CBS_CFS,
237 + .init = snd_rpi_i_sabre_q2m_init,
238 + .ops = &snd_rpi_i_sabre_q2m_ops,
239 + }
240 +};
241 +
242 +/* audio machine driver */
243 +static struct snd_soc_card snd_rpi_i_sabre_q2m = {
244 + .name = "I-Sabre Q2M DAC",
245 + .owner = THIS_MODULE,
246 + .dai_link = snd_rpi_i_sabre_q2m_dai,
247 + .num_links = ARRAY_SIZE(snd_rpi_i_sabre_q2m_dai)
248 +};
249 +
250 +
251 +static int snd_rpi_i_sabre_q2m_probe(struct platform_device *pdev)
252 +{
253 + int ret = 0;
254 +
255 + snd_rpi_i_sabre_q2m.dev = &pdev->dev;
256 + if (pdev->dev.of_node) {
257 + struct device_node *i2s_node;
258 + struct snd_soc_dai_link *dai;
259 +
260 + dai = &snd_rpi_i_sabre_q2m_dai[0];
261 + i2s_node = of_parse_phandle(pdev->dev.of_node,
262 + "i2s-controller", 0);
263 + if (i2s_node) {
264 + dai->cpu_dai_name = NULL;
265 + dai->cpu_of_node = i2s_node;
266 + dai->platform_name = NULL;
267 + dai->platform_of_node = i2s_node;
268 + } else {
269 + dev_err(&pdev->dev,
270 + "Property 'i2s-controller' missing or invalid\n");
271 + return (-EINVAL);
272 + }
273 +
274 + dai->name = "I-Sabre Q2M";
275 + dai->stream_name = "I-Sabre Q2M DAC";
276 + dai->dai_fmt = SND_SOC_DAIFMT_I2S
277 + | SND_SOC_DAIFMT_NB_NF
278 + | SND_SOC_DAIFMT_CBS_CFS;
279 + }
280 +
281 + /* Wait for registering codec driver */
282 + mdelay(50);
283 +
284 + ret = snd_soc_register_card(&snd_rpi_i_sabre_q2m);
285 + if (ret) {
286 + dev_err(&pdev->dev,
287 + "snd_soc_register_card() failed: %d\n", ret);
288 + }
289 +
290 + return ret;
291 +}
292 +
293 +static int snd_rpi_i_sabre_q2m_remove(struct platform_device *pdev)
294 +{
295 + return snd_soc_unregister_card(&snd_rpi_i_sabre_q2m);
296 +}
297 +
298 +static const struct of_device_id snd_rpi_i_sabre_q2m_of_match[] = {
299 + { .compatible = "audiophonics,i-sabre-q2m", },
300 + {}
301 +};
302 +MODULE_DEVICE_TABLE(of, snd_rpi_i_sabre_q2m_of_match);
303 +
304 +static struct platform_driver snd_rpi_i_sabre_q2m_driver = {
305 + .driver = {
306 + .name = "snd-rpi-i-sabre-q2m",
307 + .owner = THIS_MODULE,
308 + .of_match_table = snd_rpi_i_sabre_q2m_of_match,
309 + },
310 + .probe = snd_rpi_i_sabre_q2m_probe,
311 + .remove = snd_rpi_i_sabre_q2m_remove,
312 +};
313 +module_platform_driver(snd_rpi_i_sabre_q2m_driver);
314 +
315 +MODULE_DESCRIPTION("ASoC Driver for I-Sabre Q2M");
316 +MODULE_AUTHOR("Audiophonics <http://www.audiophonics.fr>");
317 +MODULE_LICENSE("GPL");
318 --- a/sound/soc/codecs/Kconfig
319 +++ b/sound/soc/codecs/Kconfig
320 @@ -85,6 +85,7 @@ config SND_SOC_ALL_CODECS
321 select SND_SOC_ICS43432
322 select SND_SOC_INNO_RK3036
323 select SND_SOC_ISABELLE if I2C
324 + select SND_SOC_I_SABRE_CODEC if I2C
325 select SND_SOC_JZ4740_CODEC
326 select SND_SOC_LM4857 if I2C
327 select SND_SOC_LM49453 if I2C
328 @@ -1322,4 +1323,8 @@ config SND_SOC_TPA6130A2
329 tristate "Texas Instruments TPA6130A2 headphone amplifier"
330 depends on I2C
331
332 +config SND_SOC_I_SABRE_CODEC
333 + tristate "Audiophonics I-SABRE Codec"
334 + depends on I2C
335 +
336 endmenu
337 --- a/sound/soc/codecs/Makefile
338 +++ b/sound/soc/codecs/Makefile
339 @@ -81,6 +81,7 @@ snd-soc-hdac-hdmi-objs := hdac_hdmi.o
340 snd-soc-ics43432-objs := ics43432.o
341 snd-soc-inno-rk3036-objs := inno_rk3036.o
342 snd-soc-isabelle-objs := isabelle.o
343 +snd-soc-i-sabre-codec-objs := i-sabre-codec.o
344 snd-soc-jz4740-codec-objs := jz4740.o
345 snd-soc-l3-objs := l3.o
346 snd-soc-lm4857-objs := lm4857.o
347 @@ -343,6 +344,7 @@ obj-$(CONFIG_SND_SOC_HDAC_HDMI) += snd-s
348 obj-$(CONFIG_SND_SOC_ICS43432) += snd-soc-ics43432.o
349 obj-$(CONFIG_SND_SOC_INNO_RK3036) += snd-soc-inno-rk3036.o
350 obj-$(CONFIG_SND_SOC_ISABELLE) += snd-soc-isabelle.o
351 +obj-$(CONFIG_SND_SOC_I_SABRE_CODEC) += snd-soc-i-sabre-codec.o
352 obj-$(CONFIG_SND_SOC_JZ4740_CODEC) += snd-soc-jz4740-codec.o
353 obj-$(CONFIG_SND_SOC_L3) += snd-soc-l3.o
354 obj-$(CONFIG_SND_SOC_LM4857) += snd-soc-lm4857.o
355 --- /dev/null
356 +++ b/sound/soc/codecs/i-sabre-codec.c
357 @@ -0,0 +1,392 @@
358 +/*
359 + * Driver for I-Sabre Q2M
360 + *
361 + * Author: Satoru Kawase
362 + * Modified by: Xiao Qingyong
363 + * Modified by: JC BARBAUD (Mute)
364 + * Update kernel v4.18+ by : Audiophonics
365 + * Copyright 2018 Audiophonics
366 + *
367 + * This program is free software; you can redistribute it and/or
368 + * modify it under the terms of the GNU General Public License
369 + * version 2 as published by the Free Software Foundation.
370 + *
371 + * This program is distributed in the hope that it will be useful, but
372 + * WITHOUT ANY WARRANTY; without even the implied warranty of
373 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
374 + * General Public License for more details.
375 + */
376 +
377 +
378 +#include <linux/init.h>
379 +#include <linux/module.h>
380 +#include <linux/regmap.h>
381 +#include <linux/i2c.h>
382 +#include <sound/soc.h>
383 +#include <sound/pcm_params.h>
384 +#include <sound/tlv.h>
385 +
386 +#include "i-sabre-codec.h"
387 +
388 +
389 +/* I-Sabre Q2M Codec Private Data */
390 +struct i_sabre_codec_priv {
391 + struct regmap *regmap;
392 + unsigned int fmt;
393 +};
394 +
395 +
396 +/* I-Sabre Q2M Codec Default Register Value */
397 +static const struct reg_default i_sabre_codec_reg_defaults[] = {
398 + { ISABRECODEC_REG_10, 0x00 },
399 + { ISABRECODEC_REG_20, 0x00 },
400 + { ISABRECODEC_REG_21, 0x00 },
401 + { ISABRECODEC_REG_22, 0x00 },
402 + { ISABRECODEC_REG_24, 0x00 },
403 +};
404 +
405 +
406 +static bool i_sabre_codec_writeable(struct device *dev, unsigned int reg)
407 +{
408 + switch (reg) {
409 + case ISABRECODEC_REG_10:
410 + case ISABRECODEC_REG_20:
411 + case ISABRECODEC_REG_21:
412 + case ISABRECODEC_REG_22:
413 + case ISABRECODEC_REG_24:
414 + return true;
415 +
416 + default:
417 + return false;
418 + }
419 +}
420 +
421 +static bool i_sabre_codec_readable(struct device *dev, unsigned int reg)
422 +{
423 + switch (reg) {
424 + case ISABRECODEC_REG_01:
425 + case ISABRECODEC_REG_02:
426 + case ISABRECODEC_REG_10:
427 + case ISABRECODEC_REG_20:
428 + case ISABRECODEC_REG_21:
429 + case ISABRECODEC_REG_22:
430 + case ISABRECODEC_REG_24:
431 + return true;
432 +
433 + default:
434 + return false;
435 + }
436 +}
437 +
438 +static bool i_sabre_codec_volatile(struct device *dev, unsigned int reg)
439 +{
440 + switch (reg) {
441 + case ISABRECODEC_REG_01:
442 + case ISABRECODEC_REG_02:
443 + return true;
444 +
445 + default:
446 + return false;
447 + }
448 +}
449 +
450 +
451 +/* Volume Scale */
452 +static const DECLARE_TLV_DB_SCALE(volume_tlv, -10000, 100, 0);
453 +
454 +
455 +/* Filter Type */
456 +static const char * const fir_filter_type_texts[] = {
457 + "brick wall",
458 + "corrected minimum phase fast",
459 + "minimum phase slow",
460 + "minimum phase fast",
461 + "linear phase slow",
462 + "linear phase fast",
463 + "apodizing fast",
464 +};
465 +
466 +static SOC_ENUM_SINGLE_DECL(i_sabre_fir_filter_type_enum,
467 + ISABRECODEC_REG_22, 0, fir_filter_type_texts);
468 +
469 +
470 +/* I2S / SPDIF Select */
471 +static const char * const iis_spdif_sel_texts[] = {
472 + "I2S",
473 + "SPDIF",
474 +};
475 +
476 +static SOC_ENUM_SINGLE_DECL(i_sabre_iis_spdif_sel_enum,
477 + ISABRECODEC_REG_24, 0, iis_spdif_sel_texts);
478 +
479 +
480 +/* Control */
481 +static const struct snd_kcontrol_new i_sabre_codec_controls[] = {
482 +SOC_SINGLE_RANGE_TLV("Digital Playback Volume", ISABRECODEC_REG_20, 0, 0, 100, 1, volume_tlv),
483 +SOC_SINGLE("Digital Playback Switch", ISABRECODEC_REG_21, 0, 1, 1),
484 +SOC_ENUM("FIR Filter Type", i_sabre_fir_filter_type_enum),
485 +SOC_ENUM("I2S/SPDIF Select", i_sabre_iis_spdif_sel_enum),
486 +};
487 +
488 +
489 +static const u32 i_sabre_codec_dai_rates_slave[] = {
490 + 8000, 11025, 16000, 22050, 32000,
491 + 44100, 48000, 64000, 88200, 96000,
492 + 176400, 192000, 352800, 384000,
493 + 705600, 768000, 1411200, 1536000
494 +};
495 +
496 +static const struct snd_pcm_hw_constraint_list constraints_slave = {
497 + .list = i_sabre_codec_dai_rates_slave,
498 + .count = ARRAY_SIZE(i_sabre_codec_dai_rates_slave),
499 +};
500 +
501 +static int i_sabre_codec_dai_startup_slave(
502 + struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
503 +{
504 + struct snd_soc_component *component = dai->component;
505 + int ret;
506 +
507 + ret = snd_pcm_hw_constraint_list(substream->runtime,
508 + 0, SNDRV_PCM_HW_PARAM_RATE, &constraints_slave);
509 + if (ret != 0) {
510 + dev_err(component->card->dev, "Failed to setup rates constraints: %d\n", ret);
511 + }
512 +
513 + return ret;
514 +}
515 +
516 +static int i_sabre_codec_dai_startup(
517 + struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
518 +{
519 + struct snd_soc_component *component = dai->component;
520 + struct i_sabre_codec_priv *i_sabre_codec
521 + = snd_soc_component_get_drvdata(component);
522 +
523 + switch (i_sabre_codec->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
524 + case SND_SOC_DAIFMT_CBS_CFS:
525 + return i_sabre_codec_dai_startup_slave(substream, dai);
526 +
527 + default:
528 + return (-EINVAL);
529 + }
530 +}
531 +
532 +static int i_sabre_codec_hw_params(
533 + struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params,
534 + struct snd_soc_dai *dai)
535 +{
536 + struct snd_soc_component *component = dai->component;
537 + struct i_sabre_codec_priv *i_sabre_codec
538 + = snd_soc_component_get_drvdata(component);
539 + unsigned int daifmt;
540 + int format_width;
541 +
542 + dev_dbg(component->card->dev, "hw_params %u Hz, %u channels\n",
543 + params_rate(params), params_channels(params));
544 +
545 + /* Check I2S Format (Bit Size) */
546 + format_width = snd_pcm_format_width(params_format(params));
547 + if ((format_width != 32) && (format_width != 16)) {
548 + dev_err(component->card->dev, "Bad frame size: %d\n",
549 + snd_pcm_format_width(params_format(params)));
550 + return (-EINVAL);
551 + }
552 +
553 + /* Check Slave Mode */
554 + daifmt = i_sabre_codec->fmt & SND_SOC_DAIFMT_MASTER_MASK;
555 + if (daifmt != SND_SOC_DAIFMT_CBS_CFS) {
556 + return (-EINVAL);
557 + }
558 +
559 + /* Notify Sampling Frequency */
560 + switch (params_rate(params))
561 + {
562 + case 44100:
563 + case 48000:
564 + case 88200:
565 + case 96000:
566 + case 176400:
567 + case 192000:
568 + snd_soc_component_update_bits(component, ISABRECODEC_REG_10, 0x01, 0x00);
569 + break;
570 +
571 + case 352800:
572 + case 384000:
573 + case 705600:
574 + case 768000:
575 + case 1411200:
576 + case 1536000:
577 + snd_soc_component_update_bits(component, ISABRECODEC_REG_10, 0x01, 0x01);
578 + break;
579 + }
580 +
581 + return 0;
582 +}
583 +
584 +static int i_sabre_codec_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
585 +{
586 + struct snd_soc_component *component = dai->component;
587 + struct i_sabre_codec_priv *i_sabre_codec
588 + = snd_soc_component_get_drvdata(component);
589 +
590 + /* interface format */
591 + switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
592 + case SND_SOC_DAIFMT_I2S:
593 + break;
594 +
595 + case SND_SOC_DAIFMT_RIGHT_J:
596 + case SND_SOC_DAIFMT_LEFT_J:
597 + default:
598 + return (-EINVAL);
599 + }
600 +
601 + /* clock inversion */
602 + if ((fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF) {
603 + return (-EINVAL);
604 + }
605 +
606 + /* Set Audio Data Format */
607 + i_sabre_codec->fmt = fmt;
608 +
609 + return 0;
610 +}
611 +
612 +static int i_sabre_codec_dac_mute(struct snd_soc_dai *dai, int mute)
613 +{
614 + struct snd_soc_component *component = dai->component;
615 +
616 + if (mute) {
617 + snd_soc_component_update_bits(component, ISABRECODEC_REG_21, 0x01, 0x01);
618 + } else {
619 + snd_soc_component_update_bits(component, ISABRECODEC_REG_21, 0x01, 0x00);
620 + }
621 +
622 + return 0;
623 +}
624 +
625 +
626 +static const struct snd_soc_dai_ops i_sabre_codec_dai_ops = {
627 + .startup = i_sabre_codec_dai_startup,
628 + .hw_params = i_sabre_codec_hw_params,
629 + .set_fmt = i_sabre_codec_set_fmt,
630 + .digital_mute = i_sabre_codec_dac_mute,
631 +};
632 +
633 +static struct snd_soc_dai_driver i_sabre_codec_dai = {
634 + .name = "i-sabre-codec-dai",
635 + .playback = {
636 + .stream_name = "Playback",
637 + .channels_min = 2,
638 + .channels_max = 2,
639 + .rates = SNDRV_PCM_RATE_CONTINUOUS,
640 + .rate_min = 8000,
641 + .rate_max = 1536000,
642 + .formats = SNDRV_PCM_FMTBIT_S16_LE
643 + | SNDRV_PCM_FMTBIT_S32_LE,
644 + },
645 + .ops = &i_sabre_codec_dai_ops,
646 +};
647 +
648 +static struct snd_soc_component_driver i_sabre_codec_codec_driver = {
649 + .controls = i_sabre_codec_controls,
650 + .num_controls = ARRAY_SIZE(i_sabre_codec_controls),
651 +};
652 +
653 +
654 +static const struct regmap_config i_sabre_codec_regmap = {
655 + .reg_bits = 8,
656 + .val_bits = 8,
657 + .max_register = ISABRECODEC_MAX_REG,
658 +
659 + .reg_defaults = i_sabre_codec_reg_defaults,
660 + .num_reg_defaults = ARRAY_SIZE(i_sabre_codec_reg_defaults),
661 +
662 + .writeable_reg = i_sabre_codec_writeable,
663 + .readable_reg = i_sabre_codec_readable,
664 + .volatile_reg = i_sabre_codec_volatile,
665 +
666 + .cache_type = REGCACHE_RBTREE,
667 +};
668 +
669 +
670 +static int i_sabre_codec_probe(struct device *dev, struct regmap *regmap)
671 +{
672 + struct i_sabre_codec_priv *i_sabre_codec;
673 + int ret;
674 +
675 + i_sabre_codec = devm_kzalloc(dev, sizeof(*i_sabre_codec), GFP_KERNEL);
676 + if (!i_sabre_codec) {
677 + dev_err(dev, "devm_kzalloc");
678 + return (-ENOMEM);
679 + }
680 +
681 + i_sabre_codec->regmap = regmap;
682 +
683 + dev_set_drvdata(dev, i_sabre_codec);
684 +
685 + ret = snd_soc_register_component(dev,
686 + &i_sabre_codec_codec_driver, &i_sabre_codec_dai, 1);
687 + if (ret != 0) {
688 + dev_err(dev, "Failed to register CODEC: %d\n", ret);
689 + return ret;
690 + }
691 +
692 + return 0;
693 +}
694 +
695 +static void i_sabre_codec_remove(struct device *dev)
696 +{
697 + snd_soc_unregister_component(dev);
698 +}
699 +
700 +
701 +static int i_sabre_codec_i2c_probe(
702 + struct i2c_client *i2c, const struct i2c_device_id *id)
703 +{
704 + struct regmap *regmap;
705 +
706 + regmap = devm_regmap_init_i2c(i2c, &i_sabre_codec_regmap);
707 + if (IS_ERR(regmap)) {
708 + return PTR_ERR(regmap);
709 + }
710 +
711 + return i_sabre_codec_probe(&i2c->dev, regmap);
712 +}
713 +
714 +static int i_sabre_codec_i2c_remove(struct i2c_client *i2c)
715 +{
716 + i_sabre_codec_remove(&i2c->dev);
717 +
718 + return 0;
719 +}
720 +
721 +
722 +static const struct i2c_device_id i_sabre_codec_i2c_id[] = {
723 + { "i-sabre-codec", },
724 + { }
725 +};
726 +MODULE_DEVICE_TABLE(i2c, i_sabre_codec_i2c_id);
727 +
728 +static const struct of_device_id i_sabre_codec_of_match[] = {
729 + { .compatible = "audiophonics,i-sabre-codec", },
730 + { }
731 +};
732 +MODULE_DEVICE_TABLE(of, i_sabre_codec_of_match);
733 +
734 +static struct i2c_driver i_sabre_codec_i2c_driver = {
735 + .driver = {
736 + .name = "i-sabre-codec-i2c",
737 + .owner = THIS_MODULE,
738 + .of_match_table = of_match_ptr(i_sabre_codec_of_match),
739 + },
740 + .probe = i_sabre_codec_i2c_probe,
741 + .remove = i_sabre_codec_i2c_remove,
742 + .id_table = i_sabre_codec_i2c_id,
743 +};
744 +module_i2c_driver(i_sabre_codec_i2c_driver);
745 +
746 +
747 +MODULE_DESCRIPTION("ASoC I-Sabre Q2M codec driver");
748 +MODULE_AUTHOR("Audiophonics <http://www.audiophonics.fr>");
749 +MODULE_LICENSE("GPL");
750 --- /dev/null
751 +++ b/sound/soc/codecs/i-sabre-codec.h
752 @@ -0,0 +1,42 @@
753 +/*
754 + * Driver for I-Sabre Q2M
755 + *
756 + * Author: Satoru Kawase
757 + * Modified by: Xiao Qingyong
758 + * Copyright 2018 Audiophonics
759 + *
760 + * This program is free software; you can redistribute it and/or
761 + * modify it under the terms of the GNU General Public License
762 + * version 2 as published by the Free Software Foundation.
763 + *
764 + * This program is distributed in the hope that it will be useful, but
765 + * WITHOUT ANY WARRANTY; without even the implied warranty of
766 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
767 + * General Public License for more details.
768 + */
769 +
770 +#ifndef _SND_SOC_ISABRECODEC
771 +#define _SND_SOC_ISABRECODEC
772 +
773 +
774 +/* ISABRECODEC Register Address */
775 +#define ISABRECODEC_REG_01 0x01 /* Virtual Device ID : 0x01 = es9038q2m */
776 +#define ISABRECODEC_REG_02 0x02 /* API revision : 0x01 = Revision 01 */
777 +#define ISABRECODEC_REG_10 0x10 /* 0x01 = above 192kHz, 0x00 = otherwise */
778 +#define ISABRECODEC_REG_20 0x20 /* 0 - 100 (decimal value, 0 = min., 100 = max.) */
779 +#define ISABRECODEC_REG_21 0x21 /* 0x00 = Mute OFF, 0x01 = Mute ON */
780 +#define ISABRECODEC_REG_22 0x22
781 +/*
782 + 0x00 = brick wall,
783 + 0x01 = corrected minimum phase fast,
784 + 0x02 = minimum phase slow,
785 + 0x03 = minimum phase fast,
786 + 0x04 = linear phase slow,
787 + 0x05 = linear phase fast,
788 + 0x06 = apodizing fast,
789 +*/
790 +//#define ISABRECODEC_REG_23 0x23 /* reserved */
791 +#define ISABRECODEC_REG_24 0x24 /* 0x00 = I2S, 0x01 = SPDIF */
792 +#define ISABRECODEC_MAX_REG 0x24 /* Maximum Register Number */
793 +
794 +#endif /* _SND_SOC_ISABRECODEC */