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