xref: /openbmc/linux/sound/soc/qcom/storm.c (revision 3d3337de)
1 /*
2  * Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 and
6  * only version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * storm.c -- ALSA SoC machine driver for QTi ipq806x-based Storm board
14  */
15 
16 #include <linux/device.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/platform_device.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
24 
25 #define STORM_SYSCLK_MULT			4
26 
27 static int storm_ops_hw_params(struct snd_pcm_substream *substream,
28 		struct snd_pcm_hw_params *params)
29 {
30 	struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
31 	struct snd_soc_card *card = soc_runtime->card;
32 	snd_pcm_format_t format = params_format(params);
33 	unsigned int rate = params_rate(params);
34 	unsigned int sysclk_freq;
35 	int bitwidth, ret;
36 
37 	bitwidth = snd_pcm_format_width(format);
38 	if (bitwidth < 0) {
39 		dev_err(card->dev, "%s() invalid bit width given: %d\n",
40 				__func__, bitwidth);
41 		return bitwidth;
42 	}
43 
44 	/*
45 	 * as the CPU DAI is the I2S bus master and no system clock is needed by
46 	 * the MAX98357a DAC, simply set the system clock to be a constant
47 	 * multiple of the bit clock for the clock divider
48 	 */
49 	sysclk_freq = rate * bitwidth * 2 * STORM_SYSCLK_MULT;
50 
51 	ret = snd_soc_dai_set_sysclk(soc_runtime->cpu_dai, 0, sysclk_freq, 0);
52 	if (ret) {
53 		dev_err(card->dev, "%s() error setting sysclk to %u: %d\n",
54 				__func__, sysclk_freq, ret);
55 		return ret;
56 	}
57 
58 	return 0;
59 }
60 
61 static struct snd_soc_ops storm_soc_ops = {
62 	.hw_params	= storm_ops_hw_params,
63 };
64 
65 static struct snd_soc_dai_link storm_dai_link = {
66 	.name		= "Primary",
67 	.stream_name	= "Primary",
68 	.codec_dai_name	= "HiFi",
69 	.ops		= &storm_soc_ops,
70 };
71 
72 static struct snd_soc_card storm_soc_card = {
73 	.name	= "ipq806x-storm",
74 	.dev	= NULL,
75 };
76 
77 static int storm_parse_of(struct snd_soc_card *card)
78 {
79 	struct snd_soc_dai_link *dai_link = card->dai_link;
80 	struct device_node *np = card->dev->of_node;
81 
82 	dai_link->cpu_of_node = of_parse_phandle(np, "cpu", 0);
83 	if (!dai_link->cpu_of_node) {
84 		dev_err(card->dev, "%s() error getting cpu phandle\n",
85 				__func__);
86 		return -EINVAL;
87 	}
88 	dai_link->platform_of_node = dai_link->cpu_of_node;
89 
90 	dai_link->codec_of_node = of_parse_phandle(np, "codec", 0);
91 	if (!dai_link->codec_of_node) {
92 		dev_err(card->dev, "%s() error getting codec phandle\n",
93 				__func__);
94 		return -EINVAL;
95 	}
96 
97 	return 0;
98 }
99 
100 static int storm_platform_probe(struct platform_device *pdev)
101 {
102 	struct snd_soc_card *card = &storm_soc_card;
103 	int ret;
104 
105 	if (card->dev) {
106 		dev_err(&pdev->dev, "%s() error, existing soundcard\n",
107 				__func__);
108 		return -ENODEV;
109 	}
110 	card->dev = &pdev->dev;
111 	platform_set_drvdata(pdev, card);
112 
113 	ret = snd_soc_of_parse_card_name(card, "qcom,model");
114 	if (ret) {
115 		dev_err(&pdev->dev, "%s() error parsing card name: %d\n",
116 				__func__, ret);
117 		return ret;
118 	}
119 
120 	card->dai_link	= &storm_dai_link;
121 	card->num_links	= 1;
122 
123 	ret = storm_parse_of(card);
124 	if (ret) {
125 		dev_err(&pdev->dev, "%s() error resolving dai links: %d\n",
126 				__func__, ret);
127 		return ret;
128 	}
129 
130 	ret = devm_snd_soc_register_card(&pdev->dev, card);
131 	if (ret == -EPROBE_DEFER) {
132 		card->dev = NULL;
133 		return ret;
134 	} else if (ret) {
135 		dev_err(&pdev->dev, "%s() error registering soundcard: %d\n",
136 				__func__, ret);
137 		return ret;
138 	}
139 
140 	return 0;
141 }
142 
143 #ifdef CONFIG_OF
144 static const struct of_device_id storm_device_id[]  = {
145 	{ .compatible = "google,storm-audio" },
146 	{},
147 };
148 MODULE_DEVICE_TABLE(of, storm_device_id);
149 #endif
150 
151 static struct platform_driver storm_platform_driver = {
152 	.driver = {
153 		.name = "storm-audio",
154 		.of_match_table =
155 			of_match_ptr(storm_device_id),
156 	},
157 	.probe = storm_platform_probe,
158 };
159 module_platform_driver(storm_platform_driver);
160 
161 MODULE_DESCRIPTION("QTi IPQ806x-based Storm Machine Driver");
162 MODULE_LICENSE("GPL v2");
163