1 From 545e6262c2218c2e6e440a08b72b58902646f654 Mon Sep 17 00:00:00 2001
2 From: dev-3Dlab <45081440+dev-3Dlab@users.noreply.github.com>
3 Date: Wed, 5 Dec 2018 10:59:11 +0100
4 Subject: [PATCH 288/773] ASoC: add driver for 3Dlab Nano soundcard (#2758)
6 Signed-off-by: GT <dev@3d-lab-av.com>
8 .../overlays/3dlab-nano-player-overlay.dts | 32 ++
9 arch/arm/boot/dts/overlays/Makefile | 1 +
10 arch/arm/boot/dts/overlays/README | 6 +
11 arch/arm/configs/bcm2709_defconfig | 1 +
12 arch/arm/configs/bcmrpi_defconfig | 1 +
13 sound/soc/bcm/3dlab-nano-player.c | 370 ++++++++++++++++++
14 sound/soc/bcm/Kconfig | 6 +
15 sound/soc/bcm/Makefile | 2 +
16 8 files changed, 419 insertions(+)
17 create mode 100644 arch/arm/boot/dts/overlays/3dlab-nano-player-overlay.dts
18 create mode 100644 sound/soc/bcm/3dlab-nano-player.c
21 +++ b/arch/arm/boot/dts/overlays/3dlab-nano-player-overlay.dts
23 +// Definitions for 3Dlab Nano Player
28 + compatible = "brcm,bcm2708";
40 + #address-cells = <1>;
45 + compatible = "3dlab,nano-player";
47 + i2s-controller = <&i2s>;
55 --- a/arch/arm/boot/dts/overlays/Makefile
56 +++ b/arch/arm/boot/dts/overlays/Makefile
58 # Overlays for the Raspberry Pi platform
60 dtbo-$(CONFIG_ARCH_BCM2835) += \
61 + 3dlab-nano-player.dtbo \
63 adau7002-simple.dtbo \
65 --- a/arch/arm/boot/dts/overlays/README
66 +++ b/arch/arm/boot/dts/overlays/README
67 @@ -199,6 +199,12 @@ Params:
68 and the other i2c baudrate parameters.
71 +Name: 3dlab-nano-player
72 +Info: Configures the 3Dlab Nano Player
73 +Load: dtoverlay=3dlab-nano-player
78 Info: Overlay for activation of ADAU1977 ADC codec over I2C for control
80 --- a/arch/arm/configs/bcm2709_defconfig
81 +++ b/arch/arm/configs/bcm2709_defconfig
82 @@ -902,6 +902,7 @@ CONFIG_SND_USB_6FIRE=m
83 CONFIG_SND_USB_HIFACE=m
85 CONFIG_SND_BCM2835_SOC_I2S=m
86 +CONFIG_SND_BCM2708_SOC_3DLAB_NANO_PLAYER=m
87 CONFIG_SND_BCM2708_SOC_GOOGLEVOICEHAT_SOUNDCARD=m
88 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC=m
89 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS=m
90 --- a/arch/arm/configs/bcmrpi_defconfig
91 +++ b/arch/arm/configs/bcmrpi_defconfig
92 @@ -895,6 +895,7 @@ CONFIG_SND_USB_6FIRE=m
93 CONFIG_SND_USB_HIFACE=m
95 CONFIG_SND_BCM2835_SOC_I2S=m
96 +CONFIG_SND_BCM2708_SOC_3DLAB_NANO_PLAYER=m
97 CONFIG_SND_BCM2708_SOC_GOOGLEVOICEHAT_SOUNDCARD=m
98 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC=m
99 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS=m
101 +++ b/sound/soc/bcm/3dlab-nano-player.c
104 + * 3Dlab Nano Player ALSA SoC Audio driver.
106 + * Copyright (C) 2018 3Dlab.
108 + * Author: GT <dev@3d-lab-av.com>
110 + * This program is free software; you can redistribute it and/or
111 + * modify it under the terms of the GNU General Public License
112 + * version 2 as published by the Free Software Foundation.
114 + * This program is distributed in the hope that it will be useful, but
115 + * WITHOUT ANY WARRANTY; without even the implied warranty of
116 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
117 + * General Public License for more details.
120 +#include <linux/module.h>
121 +#include <linux/i2c.h>
122 +#include <sound/soc.h>
123 +#include <sound/pcm.h>
124 +#include <sound/pcm_params.h>
125 +#include <sound/control.h>
127 +#define NANO_ID 0x00
128 +#define NANO_VER 0x01
129 +#define NANO_CFG 0x02
130 +#define NANO_STATUS 0x03
131 +#define NANO_SPI_ADDR 0x04
132 +#define NANO_SPI_DATA 0x05
134 +#define NANO_ID_VAL 0x3D
135 +#define NANO_CFG_OFF 0x00
136 +#define NANO_CFG_MULT1 0
137 +#define NANO_CFG_MULT2 1
138 +#define NANO_CFG_MULT4 2
139 +#define NANO_CFG_MULT8 3
140 +#define NANO_CFG_MULT16 4
141 +#define NANO_CFG_CLK22 0
142 +#define NANO_CFG_CLK24 BIT(3)
143 +#define NANO_CFG_DSD BIT(4)
144 +#define NANO_CFG_ENA BIT(5)
145 +#define NANO_CFG_BLINK BIT(6)
146 +#define NANO_STATUS_P1 BIT(0)
147 +#define NANO_STATUS_P2 BIT(1)
148 +#define NANO_STATUS_FLG BIT(2)
149 +#define NANO_STATUS_CLK BIT(3)
150 +#define NANO_SPI_READ 0
151 +#define NANO_SPI_WRITE BIT(5)
153 +#define NANO_DAC_CTRL1 0x00
154 +#define NANO_DAC_CTRL2 0x01
155 +#define NANO_DAC_CTRL3 0x02
156 +#define NANO_DAC_LATT 0x03
157 +#define NANO_DAC_RATT 0x04
159 +#define NANO_CTRL2_VAL 0x22
161 +static int nano_player_spi_write(struct regmap *map,
162 + unsigned int reg, unsigned int val)
164 + /* indirect register access */
165 + regmap_write(map, NANO_SPI_DATA, val);
166 + regmap_write(map, NANO_SPI_ADDR, reg | NANO_SPI_WRITE);
170 +static int nano_player_ctrl_info(struct snd_kcontrol *kcontrol,
171 + struct snd_ctl_elem_info *uinfo)
173 + /* describe control element */
174 + if (strstr(kcontrol->id.name, "Volume")) {
175 + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
177 + uinfo->value.integer.min = 0;
178 + uinfo->value.integer.max = 100;
180 + uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
182 + uinfo->value.integer.min = 0;
183 + uinfo->value.integer.max = 1;
189 +static int nano_player_ctrl_put(struct snd_kcontrol *kcontrol,
190 + struct snd_ctl_elem_value *ucontrol)
192 + /* program control value to hardware */
193 + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
194 + struct regmap *regmap = snd_soc_card_get_drvdata(card);
196 + if (strstr(kcontrol->id.name, "Volume")) {
197 + unsigned int vol = ucontrol->value.integer.value[0];
198 + unsigned int att = 255 - (2 * (100 - vol));
200 + nano_player_spi_write(regmap, NANO_DAC_LATT, att);
201 + nano_player_spi_write(regmap, NANO_DAC_RATT, att);
202 + kcontrol->private_value = vol;
204 + unsigned int mute = ucontrol->value.integer.value[0];
205 + unsigned int reg = NANO_CTRL2_VAL | mute;
207 + nano_player_spi_write(regmap, NANO_DAC_CTRL2, reg);
208 + kcontrol->private_value = mute;
213 +static int nano_player_ctrl_get(struct snd_kcontrol *kcontrol,
214 + struct snd_ctl_elem_value *ucontrol)
216 + /* return last programmed value */
217 + ucontrol->value.integer.value[0] = kcontrol->private_value;
221 +#define SOC_NANO_PLAYER_CTRL(xname) \
222 +{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
223 + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
224 + .info = nano_player_ctrl_info, \
225 + .put = nano_player_ctrl_put, \
226 + .get = nano_player_ctrl_get }
228 +static const struct snd_kcontrol_new nano_player_controls[] = {
229 + SOC_NANO_PLAYER_CTRL("Master Playback Volume"),
230 + SOC_NANO_PLAYER_CTRL("Master Playback Switch"),
233 +static const unsigned int nano_player_rates[] = {
234 + 44100, 48000, 88200, 96000, 176400, 192000, 352800, 384000,
235 + 705600, 768000 /* only possible with fast clocks */
238 +static struct snd_pcm_hw_constraint_list nano_player_constraint_rates = {
239 + .list = nano_player_rates,
240 + .count = ARRAY_SIZE(nano_player_rates),
243 +static int nano_player_init(struct snd_soc_pcm_runtime *rtd)
245 + struct snd_soc_card *card = rtd->card;
246 + struct regmap *regmap = snd_soc_card_get_drvdata(card);
247 + struct snd_soc_pcm_stream *cpu = &rtd->cpu_dai->driver->playback;
248 + struct snd_soc_pcm_stream *codec = &rtd->codec_dai->driver->playback;
249 + unsigned int sample_bits = 32;
252 + /* configure cpu dai */
253 + cpu->formats |= SNDRV_PCM_FMTBIT_DSD_U32_LE;
254 + cpu->rate_max = 768000;
256 + /* configure dummy codec dai */
257 + codec->rate_min = 44100;
258 + codec->rates = SNDRV_PCM_RATE_KNOT;
259 + codec->formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_DSD_U32_LE;
261 + /* configure max supported rate */
262 + regmap_read(regmap, NANO_STATUS, &val);
263 + if (val & NANO_STATUS_CLK) {
264 + dev_notice(card->dev, "Board with fast clocks installed\n");
265 + codec->rate_max = 768000;
267 + dev_notice(card->dev, "Board with normal clocks installed\n");
268 + codec->rate_max = 384000;
271 + /* frame length enforced by hardware */
272 + return snd_soc_dai_set_bclk_ratio(rtd->cpu_dai, sample_bits * 2);
275 +static int nano_player_startup(struct snd_pcm_substream *substream)
277 + return snd_pcm_hw_constraint_list(substream->runtime, 0,
278 + SNDRV_PCM_HW_PARAM_RATE,
279 + &nano_player_constraint_rates);
282 +static int nano_player_hw_params(struct snd_pcm_substream *substream,
283 + struct snd_pcm_hw_params *params)
285 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
286 + struct snd_soc_card *card = rtd->card;
287 + struct regmap *regmap = snd_soc_card_get_drvdata(card);
288 + unsigned int config = NANO_CFG_ENA;
289 + struct snd_mask *fmt;
291 + /* configure PCM or DSD */
292 + fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
293 + if (snd_mask_test(fmt, SNDRV_PCM_FORMAT_DSD_U32_LE)) {
294 + /* embed DSD in PCM data */
295 + snd_mask_none(fmt);
296 + snd_mask_set(fmt, SNDRV_PCM_FORMAT_S32_LE);
297 + /* enable DSD mode */
298 + config |= NANO_CFG_DSD;
301 + /* configure clocks */
302 + switch (params_rate(params)) {
304 + config |= NANO_CFG_MULT1 | NANO_CFG_CLK22;
307 + config |= NANO_CFG_MULT2 | NANO_CFG_CLK22;
310 + config |= NANO_CFG_MULT4 | NANO_CFG_CLK22;
313 + config |= NANO_CFG_MULT8 | NANO_CFG_CLK22;
316 + config |= NANO_CFG_MULT16 | NANO_CFG_CLK22;
319 + config |= NANO_CFG_MULT1 | NANO_CFG_CLK24;
322 + config |= NANO_CFG_MULT2 | NANO_CFG_CLK24;
325 + config |= NANO_CFG_MULT4 | NANO_CFG_CLK24;
328 + config |= NANO_CFG_MULT8 | NANO_CFG_CLK24;
331 + config |= NANO_CFG_MULT16 | NANO_CFG_CLK24;
337 + dev_dbg(card->dev, "Send CFG register 0x%02X\n", config);
338 + return regmap_write(regmap, NANO_CFG, config);
341 +static struct snd_soc_ops nano_player_ops = {
342 + .startup = nano_player_startup,
343 + .hw_params = nano_player_hw_params,
346 +static struct snd_soc_dai_link nano_player_link = {
347 + .name = "3Dlab Nano Player",
348 + .stream_name = "3Dlab Nano Player HiFi",
349 + .platform_name = "bcm2708-i2s.0",
350 + .cpu_dai_name = "bcm2708-i2s.0",
351 + .codec_name = "snd-soc-dummy",
352 + .codec_dai_name = "snd-soc-dummy-dai",
353 + .dai_fmt = SND_SOC_DAIFMT_I2S |
354 + SND_SOC_DAIFMT_CONT |
355 + SND_SOC_DAIFMT_NB_NF |
356 + SND_SOC_DAIFMT_CBM_CFM,
357 + .init = nano_player_init,
358 + .ops = &nano_player_ops,
361 +static const struct regmap_config nano_player_regmap = {
364 + .max_register = 128,
365 + .cache_type = REGCACHE_RBTREE,
368 +static int nano_player_card_probe(struct snd_soc_card *card)
370 + struct regmap *regmap = snd_soc_card_get_drvdata(card);
373 + /* check hardware integrity */
374 + regmap_read(regmap, NANO_ID, &val);
375 + if (val != NANO_ID_VAL) {
376 + dev_err(card->dev, "Invalid ID register 0x%02X\n", val);
380 + /* report version to the user */
381 + regmap_read(regmap, NANO_VER, &val);
382 + dev_notice(card->dev, "Started 3Dlab Nano Player driver (v%d)\n", val);
384 + /* enable internal audio bus and blink status LED */
385 + return regmap_write(regmap, NANO_CFG, NANO_CFG_ENA | NANO_CFG_BLINK);
388 +static int nano_player_card_remove(struct snd_soc_card *card)
390 + /* disable internal audio bus */
391 + struct regmap *regmap = snd_soc_card_get_drvdata(card);
393 + return regmap_write(regmap, NANO_CFG, NANO_CFG_OFF);
396 +static struct snd_soc_card nano_player_card = {
397 + .name = "3Dlab_Nano_Player",
398 + .owner = THIS_MODULE,
399 + .dai_link = &nano_player_link,
401 + .controls = nano_player_controls,
402 + .num_controls = ARRAY_SIZE(nano_player_controls),
403 + .probe = nano_player_card_probe,
404 + .remove = nano_player_card_remove,
407 +static int nano_player_i2c_probe(struct i2c_client *i2c,
408 + const struct i2c_device_id *id)
410 + struct regmap *regmap;
413 + regmap = devm_regmap_init_i2c(i2c, &nano_player_regmap);
414 + if (IS_ERR(regmap)) {
415 + ret = PTR_ERR(regmap);
416 + dev_err(&i2c->dev, "Failed to init regmap %d\n", ret);
420 + if (i2c->dev.of_node) {
421 + struct snd_soc_dai_link *dai = &nano_player_link;
422 + struct device_node *node;
424 + /* cpu handle configured by device tree */
425 + node = of_parse_phandle(i2c->dev.of_node, "i2s-controller", 0);
427 + dai->platform_name = NULL;
428 + dai->platform_of_node = node;
429 + dai->cpu_dai_name = NULL;
430 + dai->cpu_of_node = node;
434 + nano_player_card.dev = &i2c->dev;
435 + snd_soc_card_set_drvdata(&nano_player_card, regmap);
436 + ret = devm_snd_soc_register_card(&i2c->dev, &nano_player_card);
438 + if (ret && ret != -EPROBE_DEFER)
439 + dev_err(&i2c->dev, "Failed to register card %d\n", ret);
444 +static const struct of_device_id nano_player_of_match[] = {
445 + { .compatible = "3dlab,nano-player", },
448 +MODULE_DEVICE_TABLE(of, nano_player_of_match);
450 +static const struct i2c_device_id nano_player_i2c_id[] = {
451 + { "nano-player", 0 },
454 +MODULE_DEVICE_TABLE(i2c, nano_player_i2c_id);
456 +static struct i2c_driver nano_player_i2c_driver = {
457 + .probe = nano_player_i2c_probe,
458 + .id_table = nano_player_i2c_id,
460 + .name = "nano-player",
461 + .owner = THIS_MODULE,
462 + .of_match_table = nano_player_of_match,
466 +module_i2c_driver(nano_player_i2c_driver);
468 +MODULE_DESCRIPTION("ASoC 3Dlab Nano Player driver");
469 +MODULE_AUTHOR("GT <dev@3d-lab-av.com>");
470 +MODULE_LICENSE("GPL v2");
473 --- a/sound/soc/bcm/Kconfig
474 +++ b/sound/soc/bcm/Kconfig
475 @@ -17,6 +17,12 @@ config SND_SOC_CYGNUS
477 If you don't know what to do here, say N.
479 +config SND_BCM2708_SOC_3DLAB_NANO_PLAYER
480 + tristate "Support for 3Dlab Nano Player"
481 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
483 + Say Y or M if you want to add support for 3Dlab Nano Player.
485 config SND_BCM2708_SOC_GOOGLEVOICEHAT_SOUNDCARD
486 tristate "Support for Google voiceHAT soundcard"
487 depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
488 --- a/sound/soc/bcm/Makefile
489 +++ b/sound/soc/bcm/Makefile
490 @@ -12,6 +12,7 @@ obj-$(CONFIG_SND_SOC_CYGNUS) += snd-soc-
491 snd-soc-googlevoicehat-codec-objs := googlevoicehat-codec.o
493 # BCM2708 Machine Support
494 +snd-soc-3dlab-nano-player-objs := 3dlab-nano-player.o
495 snd-soc-hifiberry-dacplus-objs := hifiberry_dacplus.o
496 snd-soc-justboom-dac-objs := justboom-dac.o
497 snd-soc-rpi-cirrus-objs := rpi-cirrus.o
498 @@ -31,6 +32,7 @@ snd-soc-fe-pi-audio-objs := fe-pi-audio.
499 snd-soc-rpi-simple-soundcard-objs := rpi-simple-soundcard.o
500 snd-soc-rpi-wm8804-soundcard-objs := rpi-wm8804-soundcard.o
502 +obj-$(CONFIG_SND_BCM2708_SOC_3DLAB_NANO_PLAYER) += snd-soc-3dlab-nano-player.o
503 obj-$(CONFIG_SND_BCM2708_SOC_GOOGLEVOICEHAT_SOUNDCARD) += snd-soc-googlevoicehat-codec.o
504 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) += snd-soc-hifiberry-dacplus.o
505 obj-$(CONFIG_SND_BCM2708_SOC_JUSTBOOM_DAC) += snd-soc-justboom-dac.o