672edee2ea93d2aade43c8578df865a52c4c823b
[platform/kernel/linux-rpi.git] / sound / soc / qcom / storm.c
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, "invalid bit width given: %d\n", bitwidth);
40                 return bitwidth;
41         }
42
43         /*
44          * as the CPU DAI is the I2S bus master and no system clock is needed by
45          * the MAX98357a DAC, simply set the system clock to be a constant
46          * multiple of the bit clock for the clock divider
47          */
48         sysclk_freq = rate * bitwidth * 2 * STORM_SYSCLK_MULT;
49
50         ret = snd_soc_dai_set_sysclk(soc_runtime->cpu_dai, 0, sysclk_freq, 0);
51         if (ret) {
52                 dev_err(card->dev, "error setting sysclk to %u: %d\n",
53                         sysclk_freq, ret);
54                 return ret;
55         }
56
57         return 0;
58 }
59
60 static const struct snd_soc_ops storm_soc_ops = {
61         .hw_params      = storm_ops_hw_params,
62 };
63
64 SND_SOC_DAILINK_DEFS(hifi,
65         DAILINK_COMP_ARRAY(COMP_EMPTY()),
66         DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")),
67         DAILINK_COMP_ARRAY(COMP_EMPTY()));
68
69 static struct snd_soc_dai_link storm_dai_link = {
70         .name           = "Primary",
71         .stream_name    = "Primary",
72         .ops            = &storm_soc_ops,
73         SND_SOC_DAILINK_REG(hifi),
74 };
75
76 static int storm_parse_of(struct snd_soc_card *card)
77 {
78         struct snd_soc_dai_link *dai_link = card->dai_link;
79         struct device_node *np = card->dev->of_node;
80
81         dai_link->cpus->of_node = of_parse_phandle(np, "cpu", 0);
82         if (!dai_link->cpus->of_node) {
83                 dev_err(card->dev, "error getting cpu phandle\n");
84                 return -EINVAL;
85         }
86         dai_link->platforms->of_node = dai_link->cpus->of_node;
87
88         dai_link->codecs->of_node = of_parse_phandle(np, "codec", 0);
89         if (!dai_link->codecs->of_node) {
90                 dev_err(card->dev, "error getting codec phandle\n");
91                 return -EINVAL;
92         }
93
94         return 0;
95 }
96
97 static int storm_platform_probe(struct platform_device *pdev)
98 {
99         struct snd_soc_card *card;
100         int ret;
101
102         card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL);
103         if (!card)
104                 return -ENOMEM;
105
106         card->dev = &pdev->dev;
107
108         ret = snd_soc_of_parse_card_name(card, "qcom,model");
109         if (ret) {
110                 dev_err(&pdev->dev, "error parsing card name: %d\n", ret);
111                 return ret;
112         }
113
114         card->dai_link  = &storm_dai_link;
115         card->num_links = 1;
116
117         ret = storm_parse_of(card);
118         if (ret) {
119                 dev_err(&pdev->dev, "error resolving dai links: %d\n", ret);
120                 return ret;
121         }
122
123         ret = devm_snd_soc_register_card(&pdev->dev, card);
124         if (ret)
125                 dev_err(&pdev->dev, "error registering soundcard: %d\n", ret);
126
127         return ret;
128
129 }
130
131 #ifdef CONFIG_OF
132 static const struct of_device_id storm_device_id[]  = {
133         { .compatible = "google,storm-audio" },
134         {},
135 };
136 MODULE_DEVICE_TABLE(of, storm_device_id);
137 #endif
138
139 static struct platform_driver storm_platform_driver = {
140         .driver = {
141                 .name = "storm-audio",
142                 .of_match_table =
143                         of_match_ptr(storm_device_id),
144         },
145         .probe = storm_platform_probe,
146 };
147 module_platform_driver(storm_platform_driver);
148
149 MODULE_DESCRIPTION("QTi IPQ806x-based Storm Machine Driver");
150 MODULE_LICENSE("GPL v2");