|
| 1 | +/* |
| 2 | + * ASoC Driver for RPi-DAC. |
| 3 | + * |
| 4 | + * Author: Florian Meier <florian.meier@koalo.de> |
| 5 | + * Copyright 2013 |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or |
| 8 | + * modify it under the terms of the GNU General Public License |
| 9 | + * version 2 as published by the Free Software Foundation. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, but |
| 12 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * General Public License for more details. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <linux/module.h> |
| 18 | +#include <linux/platform_device.h> |
| 19 | + |
| 20 | +#include <sound/core.h> |
| 21 | +#include <sound/pcm.h> |
| 22 | +#include <sound/pcm_params.h> |
| 23 | +#include <sound/soc.h> |
| 24 | +#include <sound/jack.h> |
| 25 | + |
| 26 | +#ifdef ROCKCHIP_AUDIO |
| 27 | +#define ROCKCHIP_I2S_MCLK 128 |
| 28 | +#endif |
| 29 | + |
| 30 | +static int snd_rpi_rpi_dac_init(struct snd_soc_pcm_runtime *rtd) |
| 31 | +{ |
| 32 | + return 0; |
| 33 | +} |
| 34 | + |
| 35 | +static int snd_rpi_rpi_dac_hw_params(struct snd_pcm_substream *substream, |
| 36 | + struct snd_pcm_hw_params *params) |
| 37 | +{ |
| 38 | + struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 39 | + struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 40 | + |
| 41 | +#ifdef ROCKCHIP_AUDIO |
| 42 | + unsigned int mclk = params_rate(params) * ROCKCHIP_I2S_MCLK; |
| 43 | + |
| 44 | + return snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, SND_SOC_CLOCK_OUT); |
| 45 | +#else |
| 46 | + unsigned int sample_bits = snd_pcm_format_physical_width(params_format(params)); |
| 47 | + |
| 48 | + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2); |
| 49 | +#endif |
| 50 | +} |
| 51 | + |
| 52 | +/* machine stream operations */ |
| 53 | +static struct snd_soc_ops snd_rpi_rpi_dac_ops = { |
| 54 | + .hw_params = snd_rpi_rpi_dac_hw_params, |
| 55 | +}; |
| 56 | + |
| 57 | +static struct snd_soc_dai_link snd_rpi_rpi_dac_dai[] = { |
| 58 | +{ |
| 59 | + .name = "RPi-DAC", |
| 60 | + .stream_name = "RPi-DAC HiFi", |
| 61 | + .cpu_dai_name = "bcm2708-i2s.0", |
| 62 | + .codec_dai_name = "pcm1794a-hifi", |
| 63 | + .platform_name = "bcm2708-i2s.0", |
| 64 | + .codec_name = "pcm1794a-codec", |
| 65 | + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
| 66 | + SND_SOC_DAIFMT_CBS_CFS, |
| 67 | + .ops = &snd_rpi_rpi_dac_ops, |
| 68 | + .init = snd_rpi_rpi_dac_init, |
| 69 | +}, |
| 70 | +}; |
| 71 | + |
| 72 | +/* audio machine driver */ |
| 73 | +static struct snd_soc_card snd_rpi_rpi_dac = { |
| 74 | + .name = "snd_rpi_rpi_dac", |
| 75 | + .owner = THIS_MODULE, |
| 76 | + .dai_link = snd_rpi_rpi_dac_dai, |
| 77 | + .num_links = ARRAY_SIZE(snd_rpi_rpi_dac_dai), |
| 78 | +}; |
| 79 | + |
| 80 | +static int snd_rpi_rpi_dac_probe(struct platform_device *pdev) |
| 81 | +{ |
| 82 | + int ret = 0; |
| 83 | + |
| 84 | + snd_rpi_rpi_dac.dev = &pdev->dev; |
| 85 | + |
| 86 | + if (pdev->dev.of_node) { |
| 87 | + struct device_node *i2s_node; |
| 88 | + struct snd_soc_dai_link *dai = &snd_rpi_rpi_dac_dai[0]; |
| 89 | + i2s_node = of_parse_phandle(pdev->dev.of_node, "i2s-controller", 0); |
| 90 | + |
| 91 | + if (i2s_node) { |
| 92 | + dai->cpu_dai_name = NULL; |
| 93 | + dai->cpu_of_node = i2s_node; |
| 94 | + dai->platform_name = NULL; |
| 95 | + dai->platform_of_node = i2s_node; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + ret = snd_soc_register_card(&snd_rpi_rpi_dac); |
| 100 | + if (ret) |
| 101 | + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret); |
| 102 | + |
| 103 | + return ret; |
| 104 | +} |
| 105 | + |
| 106 | +static int snd_rpi_rpi_dac_remove(struct platform_device *pdev) |
| 107 | +{ |
| 108 | + return snd_soc_unregister_card(&snd_rpi_rpi_dac); |
| 109 | +} |
| 110 | + |
| 111 | +static const struct of_device_id snd_rpi_rpi_dac_of_match[] = { |
| 112 | + { .compatible = "rpi,rpi-dac", }, |
| 113 | + {}, |
| 114 | +}; |
| 115 | +MODULE_DEVICE_TABLE(of, snd_rpi_rpi_dac_of_match); |
| 116 | + |
| 117 | +static struct platform_driver snd_rpi_rpi_dac_driver = { |
| 118 | + .driver = { |
| 119 | + .name = "snd-rpi-dac", |
| 120 | + .owner = THIS_MODULE, |
| 121 | + .of_match_table = snd_rpi_rpi_dac_of_match, |
| 122 | + }, |
| 123 | + .probe = snd_rpi_rpi_dac_probe, |
| 124 | + .remove = snd_rpi_rpi_dac_remove, |
| 125 | +}; |
| 126 | + |
| 127 | +module_platform_driver(snd_rpi_rpi_dac_driver); |
| 128 | + |
| 129 | +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>"); |
| 130 | +MODULE_DESCRIPTION("ASoC Driver for RPi-DAC"); |
| 131 | +MODULE_LICENSE("GPL v2"); |
0 commit comments