xref: /OK3568_Linux_fs/kernel/sound/soc/qcom/storm.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * storm.c -- ALSA SoC machine driver for QTi ipq806x-based Storm board
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/device.h>
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/of.h>
11*4882a593Smuzhiyun #include <linux/mod_devicetable.h>
12*4882a593Smuzhiyun #include <linux/platform_device.h>
13*4882a593Smuzhiyun #include <sound/pcm.h>
14*4882a593Smuzhiyun #include <sound/pcm_params.h>
15*4882a593Smuzhiyun #include <sound/soc.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #define STORM_SYSCLK_MULT			4
18*4882a593Smuzhiyun 
storm_ops_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)19*4882a593Smuzhiyun static int storm_ops_hw_params(struct snd_pcm_substream *substream,
20*4882a593Smuzhiyun 		struct snd_pcm_hw_params *params)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun 	struct snd_soc_pcm_runtime *soc_runtime = asoc_substream_to_rtd(substream);
23*4882a593Smuzhiyun 	struct snd_soc_card *card = soc_runtime->card;
24*4882a593Smuzhiyun 	snd_pcm_format_t format = params_format(params);
25*4882a593Smuzhiyun 	unsigned int rate = params_rate(params);
26*4882a593Smuzhiyun 	unsigned int sysclk_freq;
27*4882a593Smuzhiyun 	int bitwidth, ret;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	bitwidth = snd_pcm_format_width(format);
30*4882a593Smuzhiyun 	if (bitwidth < 0) {
31*4882a593Smuzhiyun 		dev_err(card->dev, "invalid bit width given: %d\n", bitwidth);
32*4882a593Smuzhiyun 		return bitwidth;
33*4882a593Smuzhiyun 	}
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	/*
36*4882a593Smuzhiyun 	 * as the CPU DAI is the I2S bus master and no system clock is needed by
37*4882a593Smuzhiyun 	 * the MAX98357a DAC, simply set the system clock to be a constant
38*4882a593Smuzhiyun 	 * multiple of the bit clock for the clock divider
39*4882a593Smuzhiyun 	 */
40*4882a593Smuzhiyun 	sysclk_freq = rate * bitwidth * 2 * STORM_SYSCLK_MULT;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(soc_runtime, 0), 0, sysclk_freq, 0);
43*4882a593Smuzhiyun 	if (ret) {
44*4882a593Smuzhiyun 		dev_err(card->dev, "error setting sysclk to %u: %d\n",
45*4882a593Smuzhiyun 			sysclk_freq, ret);
46*4882a593Smuzhiyun 		return ret;
47*4882a593Smuzhiyun 	}
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	return 0;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun static const struct snd_soc_ops storm_soc_ops = {
53*4882a593Smuzhiyun 	.hw_params	= storm_ops_hw_params,
54*4882a593Smuzhiyun };
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun SND_SOC_DAILINK_DEFS(hifi,
57*4882a593Smuzhiyun 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
58*4882a593Smuzhiyun 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")),
59*4882a593Smuzhiyun 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun static struct snd_soc_dai_link storm_dai_link = {
62*4882a593Smuzhiyun 	.name		= "Primary",
63*4882a593Smuzhiyun 	.stream_name	= "Primary",
64*4882a593Smuzhiyun 	.ops		= &storm_soc_ops,
65*4882a593Smuzhiyun 	SND_SOC_DAILINK_REG(hifi),
66*4882a593Smuzhiyun };
67*4882a593Smuzhiyun 
storm_parse_of(struct snd_soc_card * card)68*4882a593Smuzhiyun static int storm_parse_of(struct snd_soc_card *card)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	struct snd_soc_dai_link *dai_link = card->dai_link;
71*4882a593Smuzhiyun 	struct device_node *np = card->dev->of_node;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	dai_link->cpus->of_node = of_parse_phandle(np, "cpu", 0);
74*4882a593Smuzhiyun 	if (!dai_link->cpus->of_node) {
75*4882a593Smuzhiyun 		dev_err(card->dev, "error getting cpu phandle\n");
76*4882a593Smuzhiyun 		return -EINVAL;
77*4882a593Smuzhiyun 	}
78*4882a593Smuzhiyun 	dai_link->platforms->of_node = dai_link->cpus->of_node;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	dai_link->codecs->of_node = of_parse_phandle(np, "codec", 0);
81*4882a593Smuzhiyun 	if (!dai_link->codecs->of_node) {
82*4882a593Smuzhiyun 		dev_err(card->dev, "error getting codec phandle\n");
83*4882a593Smuzhiyun 		return -EINVAL;
84*4882a593Smuzhiyun 	}
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	return 0;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
storm_platform_probe(struct platform_device * pdev)89*4882a593Smuzhiyun static int storm_platform_probe(struct platform_device *pdev)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	struct snd_soc_card *card;
92*4882a593Smuzhiyun 	int ret;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL);
95*4882a593Smuzhiyun 	if (!card)
96*4882a593Smuzhiyun 		return -ENOMEM;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	card->dev = &pdev->dev;
99*4882a593Smuzhiyun 	card->owner = THIS_MODULE;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	ret = snd_soc_of_parse_card_name(card, "qcom,model");
102*4882a593Smuzhiyun 	if (ret) {
103*4882a593Smuzhiyun 		dev_err(&pdev->dev, "error parsing card name: %d\n", ret);
104*4882a593Smuzhiyun 		return ret;
105*4882a593Smuzhiyun 	}
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	card->dai_link	= &storm_dai_link;
108*4882a593Smuzhiyun 	card->num_links	= 1;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	ret = storm_parse_of(card);
111*4882a593Smuzhiyun 	if (ret) {
112*4882a593Smuzhiyun 		dev_err(&pdev->dev, "error resolving dai links: %d\n", ret);
113*4882a593Smuzhiyun 		return ret;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	ret = devm_snd_soc_register_card(&pdev->dev, card);
117*4882a593Smuzhiyun 	if (ret)
118*4882a593Smuzhiyun 		dev_err(&pdev->dev, "error registering soundcard: %d\n", ret);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	return ret;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun #ifdef CONFIG_OF
125*4882a593Smuzhiyun static const struct of_device_id storm_device_id[]  = {
126*4882a593Smuzhiyun 	{ .compatible = "google,storm-audio" },
127*4882a593Smuzhiyun 	{},
128*4882a593Smuzhiyun };
129*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, storm_device_id);
130*4882a593Smuzhiyun #endif
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun static struct platform_driver storm_platform_driver = {
133*4882a593Smuzhiyun 	.driver = {
134*4882a593Smuzhiyun 		.name = "storm-audio",
135*4882a593Smuzhiyun 		.of_match_table =
136*4882a593Smuzhiyun 			of_match_ptr(storm_device_id),
137*4882a593Smuzhiyun 	},
138*4882a593Smuzhiyun 	.probe = storm_platform_probe,
139*4882a593Smuzhiyun };
140*4882a593Smuzhiyun module_platform_driver(storm_platform_driver);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun MODULE_DESCRIPTION("QTi IPQ806x-based Storm Machine Driver");
143*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
144