|
| 1 | +/* |
| 2 | + * ASoC Driver for JustBoom DAC Raspberry Pi HAT Sound Card |
| 3 | + * |
| 4 | + * Author: Milan Neskovic |
| 5 | + * Copyright 2016 |
| 6 | + * based on code by Daniel Matuschek <info@crazy-audio.com> |
| 7 | + * based on code by Florian Meier <florian.meier@koalo.de> |
| 8 | + * |
| 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. |
| 12 | + * |
| 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. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <linux/module.h> |
| 20 | +#include <linux/platform_device.h> |
| 21 | + |
| 22 | +#include <sound/core.h> |
| 23 | +#include <sound/pcm.h> |
| 24 | +#include <sound/pcm_params.h> |
| 25 | +#include <sound/soc.h> |
| 26 | +#include <sound/jack.h> |
| 27 | + |
| 28 | +#include "../codecs/pcm512x.h" |
| 29 | + |
| 30 | +#ifdef ROCKCHIP_AUDIO |
| 31 | +#define ROCKCHIP_I2S_MCLK 512 |
| 32 | +#endif |
| 33 | + |
| 34 | +static bool digital_gain_0db_limit = true; |
| 35 | + |
| 36 | +static int snd_rpi_justboom_dac_init(struct snd_soc_pcm_runtime *rtd) |
| 37 | +{ |
| 38 | + struct snd_soc_codec *codec = rtd->codec; |
| 39 | + snd_soc_update_bits(codec, PCM512x_GPIO_EN, 0x08, 0x08); |
| 40 | + snd_soc_update_bits(codec, PCM512x_GPIO_OUTPUT_4, 0xf, 0x02); |
| 41 | + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x08); |
| 42 | + |
| 43 | + if (digital_gain_0db_limit) |
| 44 | + { |
| 45 | + int ret; |
| 46 | + struct snd_soc_card *card = rtd->card; |
| 47 | + |
| 48 | + ret = snd_soc_limit_volume(card, "Digital Playback Volume", 207); |
| 49 | + if (ret < 0) |
| 50 | + dev_warn(card->dev, "Failed to set volume limit: %d\n", ret); |
| 51 | + } |
| 52 | + |
| 53 | + return 0; |
| 54 | +} |
| 55 | + |
| 56 | +static int snd_rpi_justboom_dac_hw_params(struct snd_pcm_substream *substream, |
| 57 | + struct snd_pcm_hw_params *params) |
| 58 | +{ |
| 59 | + struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 60 | + struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 61 | + /*return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);*/ |
| 62 | +#ifdef ROCKCHIP_AUDIO |
| 63 | + unsigned int mclk = params_rate(params) * ROCKCHIP_I2S_MCLK; |
| 64 | + return snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, SND_SOC_CLOCK_OUT); |
| 65 | +#else |
| 66 | + unsigned int sample_bits = |
| 67 | + snd_pcm_format_physical_width(params_format(params)); |
| 68 | + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2); |
| 69 | +#endif |
| 70 | +} |
| 71 | + |
| 72 | +static int snd_rpi_justboom_dac_startup(struct snd_pcm_substream *substream) { |
| 73 | + struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 74 | + struct snd_soc_codec *codec = rtd->codec; |
| 75 | + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x08); |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +static void snd_rpi_justboom_dac_shutdown(struct snd_pcm_substream *substream) { |
| 80 | + struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 81 | + struct snd_soc_codec *codec = rtd->codec; |
| 82 | + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x00); |
| 83 | +} |
| 84 | + |
| 85 | +/* machine stream operations */ |
| 86 | +static struct snd_soc_ops snd_rpi_justboom_dac_ops = { |
| 87 | + .hw_params = snd_rpi_justboom_dac_hw_params, |
| 88 | + .startup = snd_rpi_justboom_dac_startup, |
| 89 | + .shutdown = snd_rpi_justboom_dac_shutdown, |
| 90 | +}; |
| 91 | + |
| 92 | +static struct snd_soc_dai_link snd_rpi_justboom_dac_dai[] = { |
| 93 | +{ |
| 94 | + .name = "JustBoom DAC", |
| 95 | + .stream_name = "JustBoom DAC HiFi", |
| 96 | + .cpu_dai_name = "bcm2708-i2s.0", |
| 97 | + .codec_dai_name = "pcm512x-hifi", |
| 98 | + .platform_name = "bcm2708-i2s.0", |
| 99 | + .codec_name = "pcm512x.1-004d", |
| 100 | + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
| 101 | + SND_SOC_DAIFMT_CBS_CFS, |
| 102 | + .ops = &snd_rpi_justboom_dac_ops, |
| 103 | + .init = snd_rpi_justboom_dac_init, |
| 104 | +}, |
| 105 | +}; |
| 106 | + |
| 107 | +/* audio machine driver */ |
| 108 | +static struct snd_soc_card snd_rpi_justboom_dac = { |
| 109 | + .name = "snd_rpi_justboom_dac", |
| 110 | + .driver_name = "JustBoomDac", |
| 111 | + .owner = THIS_MODULE, |
| 112 | + .dai_link = snd_rpi_justboom_dac_dai, |
| 113 | + .num_links = ARRAY_SIZE(snd_rpi_justboom_dac_dai), |
| 114 | +}; |
| 115 | + |
| 116 | +static int snd_rpi_justboom_dac_probe(struct platform_device *pdev) |
| 117 | +{ |
| 118 | + int ret = 0; |
| 119 | + |
| 120 | + snd_rpi_justboom_dac.dev = &pdev->dev; |
| 121 | + |
| 122 | + if (pdev->dev.of_node) { |
| 123 | + struct device_node *i2s_node; |
| 124 | + struct snd_soc_dai_link *dai = &snd_rpi_justboom_dac_dai[0]; |
| 125 | + i2s_node = of_parse_phandle(pdev->dev.of_node, |
| 126 | + "i2s-controller", 0); |
| 127 | + |
| 128 | + if (i2s_node) { |
| 129 | + dai->cpu_dai_name = NULL; |
| 130 | + dai->cpu_of_node = i2s_node; |
| 131 | + dai->platform_name = NULL; |
| 132 | + dai->platform_of_node = i2s_node; |
| 133 | + } |
| 134 | + |
| 135 | + digital_gain_0db_limit = !of_property_read_bool( |
| 136 | + pdev->dev.of_node, "justboom,24db_digital_gain"); |
| 137 | + } |
| 138 | + |
| 139 | + ret = snd_soc_register_card(&snd_rpi_justboom_dac); |
| 140 | + if (ret && ret != -EPROBE_DEFER) |
| 141 | + dev_err(&pdev->dev, |
| 142 | + "snd_soc_register_card() failed: %d\n", ret); |
| 143 | + |
| 144 | + return ret; |
| 145 | +} |
| 146 | + |
| 147 | +static int snd_rpi_justboom_dac_remove(struct platform_device *pdev) |
| 148 | +{ |
| 149 | + return snd_soc_unregister_card(&snd_rpi_justboom_dac); |
| 150 | +} |
| 151 | + |
| 152 | +static const struct of_device_id snd_rpi_justboom_dac_of_match[] = { |
| 153 | + { .compatible = "justboom,justboom-dac", }, |
| 154 | + {}, |
| 155 | +}; |
| 156 | +MODULE_DEVICE_TABLE(of, snd_rpi_justboom_dac_of_match); |
| 157 | + |
| 158 | +static struct platform_driver snd_rpi_justboom_dac_driver = { |
| 159 | + .driver = { |
| 160 | + .name = "snd-rpi-justboom-dac", |
| 161 | + .owner = THIS_MODULE, |
| 162 | + .of_match_table = snd_rpi_justboom_dac_of_match, |
| 163 | + }, |
| 164 | + .probe = snd_rpi_justboom_dac_probe, |
| 165 | + .remove = snd_rpi_justboom_dac_remove, |
| 166 | +}; |
| 167 | + |
| 168 | +module_platform_driver(snd_rpi_justboom_dac_driver); |
| 169 | + |
| 170 | +MODULE_AUTHOR("Milan Neskovic <info@justboom.co>"); |
| 171 | +MODULE_DESCRIPTION("ASoC Driver for JustBoom PI DAC HAT Sound Card"); |
| 172 | +MODULE_LICENSE("GPL v2"); |
0 commit comments