2 * ASoC Driver for HiFiBerry DAC+ / DAC Pro / AMP100
4 * Author: Daniel Matuschek, Stuart MacLean <stuart@hifiberry.com>
6 * based on code by Florian Meier <florian.meier@koalo.de>
7 * Headphone/AMP100 Joerg Schambacher <joerg@hifiberry.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/gpio/consumer.h>
21 #include <../drivers/gpio/gpiolib.h>
22 #include <linux/platform_device.h>
23 #include <linux/kernel.h>
24 #include <linux/clk.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/delay.h>
30 #include <linux/i2c.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
36 #include <sound/jack.h>
38 #include "../codecs/pcm512x.h"
40 #define HIFIBERRY_DACPRO_NOCLOCK 0
41 #define HIFIBERRY_DACPRO_CLK44EN 1
42 #define HIFIBERRY_DACPRO_CLK48EN 2
45 struct regmap *regmap;
49 /* Clock rate of CLK44EN attached to GPIO6 pin */
50 #define CLK_44EN_RATE 22579200UL
51 /* Clock rate of CLK48EN attached to GPIO3 pin */
52 #define CLK_48EN_RATE 24576000UL
55 static bool snd_rpi_hifiberry_is_dacpro;
56 static bool digital_gain_0db_limit = true;
58 static bool auto_mute;
59 static int mute_ext_ctl;
61 static struct gpio_desc *snd_mute_gpio;
62 static struct gpio_desc *snd_reset_gpio;
63 static struct snd_soc_card snd_rpi_hifiberry_dacplus;
65 static int snd_rpi_hifiberry_dacplus_mute_set(int mute)
67 gpiod_set_value_cansleep(snd_mute_gpio, mute);
71 static int snd_rpi_hifiberry_dacplus_mute_get(struct snd_kcontrol *kcontrol,
72 struct snd_ctl_elem_value *ucontrol)
74 ucontrol->value.integer.value[0] = mute_ext;
79 static int snd_rpi_hifiberry_dacplus_mute_put(struct snd_kcontrol *kcontrol,
80 struct snd_ctl_elem_value *ucontrol)
82 if (mute_ext == ucontrol->value.integer.value[0])
85 mute_ext = ucontrol->value.integer.value[0];
87 return snd_rpi_hifiberry_dacplus_mute_set(mute_ext);
90 static const char * const mute_text[] = {"Play", "Mute"};
91 static const struct soc_enum hb_dacplus_opt_mute_enum =
92 SOC_ENUM_SINGLE_EXT(2, mute_text);
94 static const struct snd_kcontrol_new hb_dacplus_opt_mute_controls[] = {
95 SOC_ENUM_EXT("Mute(ext)", hb_dacplus_opt_mute_enum,
96 snd_rpi_hifiberry_dacplus_mute_get,
97 snd_rpi_hifiberry_dacplus_mute_put),
100 static void snd_rpi_hifiberry_dacplus_select_clk(struct snd_soc_component *component,
104 case HIFIBERRY_DACPRO_NOCLOCK:
105 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x00);
107 case HIFIBERRY_DACPRO_CLK44EN:
108 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x20);
110 case HIFIBERRY_DACPRO_CLK48EN:
111 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x04);
114 usleep_range(3000, 4000);
117 static void snd_rpi_hifiberry_dacplus_clk_gpio(struct snd_soc_component *component)
119 snd_soc_component_update_bits(component, PCM512x_GPIO_EN, 0x24, 0x24);
120 snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_3, 0x0f, 0x02);
121 snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_6, 0x0f, 0x02);
124 static bool snd_rpi_hifiberry_dacplus_is_sclk(struct snd_soc_component *component)
128 sck = snd_soc_component_read(component, PCM512x_RATE_DET_4);
129 return (!(sck & 0x40));
132 static bool snd_rpi_hifiberry_dacplus_is_pro_card(struct snd_soc_component *component)
134 bool isClk44EN, isClk48En, isNoClk;
136 snd_rpi_hifiberry_dacplus_clk_gpio(component);
138 snd_rpi_hifiberry_dacplus_select_clk(component, HIFIBERRY_DACPRO_CLK44EN);
139 isClk44EN = snd_rpi_hifiberry_dacplus_is_sclk(component);
141 snd_rpi_hifiberry_dacplus_select_clk(component, HIFIBERRY_DACPRO_NOCLOCK);
142 isNoClk = snd_rpi_hifiberry_dacplus_is_sclk(component);
144 snd_rpi_hifiberry_dacplus_select_clk(component, HIFIBERRY_DACPRO_CLK48EN);
145 isClk48En = snd_rpi_hifiberry_dacplus_is_sclk(component);
147 return (isClk44EN && isClk48En && !isNoClk);
150 static int snd_rpi_hifiberry_dacplus_clk_for_rate(int sample_rate)
154 switch (sample_rate) {
161 type = HIFIBERRY_DACPRO_CLK44EN;
164 type = HIFIBERRY_DACPRO_CLK48EN;
170 static void snd_rpi_hifiberry_dacplus_set_sclk(struct snd_soc_component *component,
173 struct pcm512x_priv *pcm512x = snd_soc_component_get_drvdata(component);
175 if (!IS_ERR(pcm512x->sclk)) {
178 ctype = snd_rpi_hifiberry_dacplus_clk_for_rate(sample_rate);
179 clk_set_rate(pcm512x->sclk, (ctype == HIFIBERRY_DACPRO_CLK44EN)
180 ? CLK_44EN_RATE : CLK_48EN_RATE);
181 snd_rpi_hifiberry_dacplus_select_clk(component, ctype);
185 static int snd_rpi_hifiberry_dacplus_init(struct snd_soc_pcm_runtime *rtd)
187 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
188 struct pcm512x_priv *priv;
189 struct snd_soc_card *card = &snd_rpi_hifiberry_dacplus;
192 snd_rpi_hifiberry_is_dacpro = false;
194 snd_rpi_hifiberry_is_dacpro =
195 snd_rpi_hifiberry_dacplus_is_pro_card(component);
197 if (snd_rpi_hifiberry_is_dacpro) {
198 struct snd_soc_dai_link *dai = rtd->dai_link;
200 dai->name = "HiFiBerry DAC+ Pro";
201 dai->stream_name = "HiFiBerry DAC+ Pro HiFi";
202 dai->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
203 | SND_SOC_DAIFMT_CBM_CFM;
205 snd_soc_component_update_bits(component, PCM512x_BCLK_LRCLK_CFG, 0x31, 0x11);
206 snd_soc_component_update_bits(component, PCM512x_MASTER_MODE, 0x03, 0x03);
207 snd_soc_component_update_bits(component, PCM512x_MASTER_CLKDIV_2, 0x7f, 63);
209 priv = snd_soc_component_get_drvdata(component);
210 priv->sclk = ERR_PTR(-ENOENT);
213 snd_soc_component_update_bits(component, PCM512x_GPIO_EN, 0x08, 0x08);
214 snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_4, 0x0f, 0x02);
216 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x00);
218 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
220 if (digital_gain_0db_limit) {
222 struct snd_soc_card *card = rtd->card;
224 ret = snd_soc_limit_volume(card, "Digital Playback Volume", 207);
226 dev_warn(card->dev, "Failed to set volume limit: %d\n", ret);
228 if (snd_reset_gpio) {
229 gpiod_set_value_cansleep(snd_reset_gpio, 0);
231 gpiod_set_value_cansleep(snd_reset_gpio, 1);
233 gpiod_set_value_cansleep(snd_reset_gpio, 0);
237 snd_soc_add_card_controls(card, hb_dacplus_opt_mute_controls,
238 ARRAY_SIZE(hb_dacplus_opt_mute_controls));
241 gpiod_set_value_cansleep(snd_mute_gpio, mute_ext);
246 static int snd_rpi_hifiberry_dacplus_update_rate_den(
247 struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
249 struct snd_soc_pcm_runtime *rtd = substream->private_data;
250 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
251 struct pcm512x_priv *pcm512x = snd_soc_component_get_drvdata(component);
252 struct snd_ratnum *rats_no_pll;
253 unsigned int num = 0, den = 0;
256 rats_no_pll = devm_kzalloc(rtd->dev, sizeof(*rats_no_pll), GFP_KERNEL);
260 rats_no_pll->num = clk_get_rate(pcm512x->sclk) / 64;
261 rats_no_pll->den_min = 1;
262 rats_no_pll->den_max = 128;
263 rats_no_pll->den_step = 1;
265 err = snd_interval_ratnum(hw_param_interval(params,
266 SNDRV_PCM_HW_PARAM_RATE), 1, rats_no_pll, &num, &den);
267 if (err >= 0 && den) {
268 params->rate_num = num;
269 params->rate_den = den;
272 devm_kfree(rtd->dev, rats_no_pll);
276 static int snd_rpi_hifiberry_dacplus_hw_params(
277 struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
280 struct snd_soc_pcm_runtime *rtd = substream->private_data;
281 int channels = params_channels(params);
284 if (snd_rpi_hifiberry_is_dacpro) {
285 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
287 width = snd_pcm_format_physical_width(params_format(params));
289 snd_rpi_hifiberry_dacplus_set_sclk(component,
290 params_rate(params));
292 ret = snd_rpi_hifiberry_dacplus_update_rate_den(
296 ret = snd_soc_dai_set_bclk_ratio(asoc_rtd_to_cpu(rtd, 0), channels * width);
299 ret = snd_soc_dai_set_bclk_ratio(asoc_rtd_to_codec(rtd, 0), channels * width);
303 static int snd_rpi_hifiberry_dacplus_startup(
304 struct snd_pcm_substream *substream)
306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
307 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
310 gpiod_set_value_cansleep(snd_mute_gpio, 0);
313 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
317 static void snd_rpi_hifiberry_dacplus_shutdown(
318 struct snd_pcm_substream *substream)
320 struct snd_soc_pcm_runtime *rtd = substream->private_data;
321 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
323 snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x00);
325 gpiod_set_value_cansleep(snd_mute_gpio, 1);
328 /* machine stream operations */
329 static struct snd_soc_ops snd_rpi_hifiberry_dacplus_ops = {
330 .hw_params = snd_rpi_hifiberry_dacplus_hw_params,
331 .startup = snd_rpi_hifiberry_dacplus_startup,
332 .shutdown = snd_rpi_hifiberry_dacplus_shutdown,
335 SND_SOC_DAILINK_DEFS(rpi_hifiberry_dacplus,
336 DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
337 DAILINK_COMP_ARRAY(COMP_CODEC("pcm512x.1-004d", "pcm512x-hifi")),
338 DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2708-i2s.0")));
340 static struct snd_soc_dai_link snd_rpi_hifiberry_dacplus_dai[] = {
342 .name = "HiFiBerry DAC+",
343 .stream_name = "HiFiBerry DAC+ HiFi",
344 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
345 SND_SOC_DAIFMT_CBS_CFS,
346 .ops = &snd_rpi_hifiberry_dacplus_ops,
347 .init = snd_rpi_hifiberry_dacplus_init,
348 SND_SOC_DAILINK_REG(rpi_hifiberry_dacplus),
352 /* aux device for optional headphone amp */
353 static struct snd_soc_aux_dev hifiberry_dacplus_aux_devs[] = {
356 .name = "tpa6130a2.1-0060",
361 /* audio machine driver */
362 static struct snd_soc_card snd_rpi_hifiberry_dacplus = {
363 .name = "snd_rpi_hifiberry_dacplus",
364 .driver_name = "HifiberryDacp",
365 .owner = THIS_MODULE,
366 .dai_link = snd_rpi_hifiberry_dacplus_dai,
367 .num_links = ARRAY_SIZE(snd_rpi_hifiberry_dacplus_dai),
370 static int hb_hp_detect(void)
372 struct i2c_adapter *adap = i2c_get_adapter(1);
374 struct i2c_client tpa_i2c_client = {
380 return -EPROBE_DEFER; /* I2C module not yet available */
382 ret = i2c_smbus_read_byte(&tpa_i2c_client) >= 0;
383 i2c_put_adapter(adap);
387 static struct property tpa_enable_prop = {
389 .length = 4 + 1, /* length 'okay' + 1 */
393 static int snd_rpi_hifiberry_dacplus_probe(struct platform_device *pdev)
396 struct snd_soc_card *card = &snd_rpi_hifiberry_dacplus;
398 struct device_node *tpa_node;
399 struct property *tpa_prop;
400 struct of_changeset ocs;
404 /* probe for head phone amp */
405 ret = hb_hp_detect();
409 card->aux_dev = hifiberry_dacplus_aux_devs;
411 ARRAY_SIZE(hifiberry_dacplus_aux_devs);
412 tpa_node = of_find_compatible_node(NULL, NULL, "ti,tpa6130a2");
413 tpa_prop = of_find_property(tpa_node, "status", &len);
415 if (strcmp((char *)tpa_prop->value, "okay")) {
416 /* and activate headphone using change_sets */
417 dev_info(&pdev->dev, "activating headphone amplifier");
418 of_changeset_init(&ocs);
419 ret = of_changeset_update_property(&ocs, tpa_node,
423 "cannot activate headphone amplifier\n");
426 ret = of_changeset_apply(&ocs);
429 "cannot activate headphone amplifier\n");
435 snd_rpi_hifiberry_dacplus.dev = &pdev->dev;
436 if (pdev->dev.of_node) {
437 struct device_node *i2s_node;
438 struct snd_soc_dai_link *dai;
440 dai = &snd_rpi_hifiberry_dacplus_dai[0];
441 i2s_node = of_parse_phandle(pdev->dev.of_node,
442 "i2s-controller", 0);
445 dai->cpus->dai_name = NULL;
446 dai->cpus->of_node = i2s_node;
447 dai->platforms->name = NULL;
448 dai->platforms->of_node = i2s_node;
451 digital_gain_0db_limit = !of_property_read_bool(
452 pdev->dev.of_node, "hifiberry,24db_digital_gain");
453 slave = of_property_read_bool(pdev->dev.of_node,
454 "hifiberry-dacplus,slave");
455 leds_off = of_property_read_bool(pdev->dev.of_node,
456 "hifiberry-dacplus,leds_off");
457 auto_mute = of_property_read_bool(pdev->dev.of_node,
458 "hifiberry-dacplus,auto_mute");
461 * check for HW MUTE as defined in DT-overlay
462 * active high, therefore default to HIGH to MUTE
464 snd_mute_gpio = devm_gpiod_get_optional(&pdev->dev,
465 "mute", GPIOD_OUT_HIGH);
466 if (IS_ERR(snd_mute_gpio)) {
467 dev_err(&pdev->dev, "Can't allocate GPIO (HW-MUTE)");
468 return PTR_ERR(snd_mute_gpio);
471 /* add ALSA control if requested in DT-overlay (AMP100) */
472 pp = of_find_property(pdev->dev.of_node,
473 "hifiberry-dacplus,mute_ext_ctl", &tmp);
475 if (!of_property_read_u32(pdev->dev.of_node,
476 "hifiberry-dacplus,mute_ext_ctl", &mute_ext)) {
477 /* ALSA control will be used */
482 /* check for HW RESET (AMP100) */
483 snd_reset_gpio = devm_gpiod_get_optional(&pdev->dev,
484 "reset", GPIOD_OUT_HIGH);
485 if (IS_ERR(snd_reset_gpio)) {
486 dev_err(&pdev->dev, "Can't allocate GPIO (HW-RESET)");
487 return PTR_ERR(snd_reset_gpio);
492 ret = devm_snd_soc_register_card(&pdev->dev,
493 &snd_rpi_hifiberry_dacplus);
494 if (ret && ret != -EPROBE_DEFER)
496 "snd_soc_register_card() failed: %d\n", ret);
499 dev_info(&pdev->dev, "GPIO%i for HW-MUTE selected",
500 gpio_chip_hwgpio(snd_mute_gpio));
502 dev_info(&pdev->dev, "GPIO%i for HW-RESET selected",
503 gpio_chip_hwgpio(snd_reset_gpio));
508 static const struct of_device_id snd_rpi_hifiberry_dacplus_of_match[] = {
509 { .compatible = "hifiberry,hifiberry-dacplus", },
512 MODULE_DEVICE_TABLE(of, snd_rpi_hifiberry_dacplus_of_match);
514 static struct platform_driver snd_rpi_hifiberry_dacplus_driver = {
516 .name = "snd-rpi-hifiberry-dacplus",
517 .owner = THIS_MODULE,
518 .of_match_table = snd_rpi_hifiberry_dacplus_of_match,
520 .probe = snd_rpi_hifiberry_dacplus_probe,
523 module_platform_driver(snd_rpi_hifiberry_dacplus_driver);
525 MODULE_AUTHOR("Daniel Matuschek <daniel@hifiberry.com>");
526 MODULE_DESCRIPTION("ASoC Driver for HiFiBerry DAC+");
527 MODULE_LICENSE("GPL v2");