Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / sound / soc / bcm / fe-pi-audio.c
1 /*
2  * ASoC Driver for Fe-Pi Audio Sound Card
3  *
4  * Author:      Henry Kupis <kuupaz@gmail.com>
5  *              Copyright 2016
6  *              based on code by Florian Meier <florian.meier@koalo.de>
7  *              based on code by Shawn Guo <shawn.guo@linaro.org>
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 #include <linux/io.h>
22
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/jack.h>
28
29 #include "../codecs/sgtl5000.h"
30
31 static int snd_fe_pi_audio_init(struct snd_soc_pcm_runtime *rtd)
32 {
33         struct snd_soc_card *card = rtd->card;
34         struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
35
36         snd_soc_dapm_force_enable_pin(&card->dapm, "LO");
37         snd_soc_dapm_force_enable_pin(&card->dapm, "ADC");
38         snd_soc_dapm_force_enable_pin(&card->dapm, "DAC");
39         snd_soc_dapm_force_enable_pin(&card->dapm, "HP");
40         snd_soc_component_update_bits(component, SGTL5000_CHIP_ANA_POWER,
41                         SGTL5000_VAG_POWERUP, SGTL5000_VAG_POWERUP);
42
43         return 0;
44 }
45
46 static int snd_fe_pi_audio_hw_params(struct snd_pcm_substream *substream,
47         struct snd_pcm_hw_params *params)
48 {
49         struct snd_soc_pcm_runtime *rtd = substream->private_data;
50         struct device *dev = rtd->card->dev;
51         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
52
53         int ret;
54
55         /* Set SGTL5000's SYSCLK */
56         ret = snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, 12288000, SND_SOC_CLOCK_IN);
57         if (ret) {
58                 dev_err(dev, "could not set codec driver clock params\n");
59                 return ret;
60         }
61
62         return 0;
63 }
64
65
66 static struct snd_soc_ops snd_fe_pi_audio_ops = {
67         .hw_params = snd_fe_pi_audio_hw_params,
68 };
69
70 SND_SOC_DAILINK_DEFS(fe_pi,
71         DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
72         DAILINK_COMP_ARRAY(COMP_CODEC("sgtl5000.1-000a", "sgtl5000")),
73         DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2708-i2s.0")));
74
75 static struct snd_soc_dai_link snd_fe_pi_audio_dai[] = {
76         {
77                 .name           = "FE-PI",
78                 .stream_name    = "Fe-Pi HiFi",
79                 .dai_fmt        = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
80                                         SND_SOC_DAIFMT_CBM_CFM,
81                 .ops            = &snd_fe_pi_audio_ops,
82                 .init           = snd_fe_pi_audio_init,
83                 SND_SOC_DAILINK_REG(fe_pi),
84         },
85 };
86
87 static const struct snd_soc_dapm_route fe_pi_audio_dapm_routes[] = {
88         {"ADC", NULL, "Mic Bias"},
89 };
90
91
92 static struct snd_soc_card fe_pi_audio = {
93         .name         = "Fe-Pi Audio",
94         .owner        = THIS_MODULE,
95         .dai_link     = snd_fe_pi_audio_dai,
96         .num_links    = ARRAY_SIZE(snd_fe_pi_audio_dai),
97
98         .dapm_routes = fe_pi_audio_dapm_routes,
99         .num_dapm_routes = ARRAY_SIZE(fe_pi_audio_dapm_routes),
100 };
101
102 static int snd_fe_pi_audio_probe(struct platform_device *pdev)
103 {
104         int ret = 0;
105         struct snd_soc_card *card = &fe_pi_audio;
106         struct device_node *np = pdev->dev.of_node;
107         struct device_node *i2s_node;
108         struct snd_soc_dai_link *dai = &snd_fe_pi_audio_dai[0];
109
110         fe_pi_audio.dev = &pdev->dev;
111
112         i2s_node = of_parse_phandle(np, "i2s-controller", 0);
113         if (!i2s_node) {
114                 dev_err(&pdev->dev, "i2s_node phandle missing or invalid\n");
115                 return -EINVAL;
116         }
117
118         dai->cpus->dai_name = NULL;
119         dai->cpus->of_node = i2s_node;
120         dai->platforms->name = NULL;
121         dai->platforms->of_node = i2s_node;
122
123         of_node_put(i2s_node);
124
125         card->dev = &pdev->dev;
126         platform_set_drvdata(pdev, card);
127
128         ret = devm_snd_soc_register_card(&pdev->dev, card);
129         if (ret && ret != -EPROBE_DEFER)
130                 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
131
132         return ret;
133 }
134
135 static const struct of_device_id snd_fe_pi_audio_of_match[] = {
136         { .compatible = "fe-pi,fe-pi-audio", },
137         {},
138 };
139 MODULE_DEVICE_TABLE(of, snd_fe_pi_audio_of_match);
140
141 static struct platform_driver snd_fe_pi_audio_driver = {
142         .driver = {
143                 .name   = "snd-fe-pi-audio",
144                 .owner  = THIS_MODULE,
145                 .of_match_table = snd_fe_pi_audio_of_match,
146         },
147         .probe          = snd_fe_pi_audio_probe,
148 };
149
150 module_platform_driver(snd_fe_pi_audio_driver);
151
152 MODULE_AUTHOR("Henry Kupis <fe-pi@cox.net>");
153 MODULE_DESCRIPTION("ASoC Driver for Fe-Pi Audio");
154 MODULE_LICENSE("GPL v2");