Merge tag 'extcon-next-for-4.20' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / sound / soc / tegra / tegra_asoc_utils.c
blob1be311c51a181996753c830ac77a902371c0553b
1 /*
2 * tegra_asoc_utils.c - Harmony machine ASoC driver
4 * Author: Stephen Warren <swarren@nvidia.com>
5 * Copyright (C) 2010,2012 - NVIDIA, Inc.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
23 #include <linux/clk.h>
24 #include <linux/device.h>
25 #include <linux/err.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/of.h>
30 #include "tegra_asoc_utils.h"
32 int tegra_asoc_utils_set_rate(struct tegra_asoc_utils_data *data, int srate,
33 int mclk)
35 int new_baseclock;
36 bool clk_change;
37 int err;
39 switch (srate) {
40 case 11025:
41 case 22050:
42 case 44100:
43 case 88200:
44 if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20)
45 new_baseclock = 56448000;
46 else if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA30)
47 new_baseclock = 564480000;
48 else
49 new_baseclock = 282240000;
50 break;
51 case 8000:
52 case 16000:
53 case 32000:
54 case 48000:
55 case 64000:
56 case 96000:
57 if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20)
58 new_baseclock = 73728000;
59 else if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA30)
60 new_baseclock = 552960000;
61 else
62 new_baseclock = 368640000;
63 break;
64 default:
65 return -EINVAL;
68 clk_change = ((new_baseclock != data->set_baseclock) ||
69 (mclk != data->set_mclk));
70 if (!clk_change)
71 return 0;
73 data->set_baseclock = 0;
74 data->set_mclk = 0;
76 clk_disable_unprepare(data->clk_cdev1);
77 clk_disable_unprepare(data->clk_pll_a_out0);
78 clk_disable_unprepare(data->clk_pll_a);
80 err = clk_set_rate(data->clk_pll_a, new_baseclock);
81 if (err) {
82 dev_err(data->dev, "Can't set pll_a rate: %d\n", err);
83 return err;
86 err = clk_set_rate(data->clk_pll_a_out0, mclk);
87 if (err) {
88 dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err);
89 return err;
92 /* Don't set cdev1/extern1 rate; it's locked to pll_a_out0 */
94 err = clk_prepare_enable(data->clk_pll_a);
95 if (err) {
96 dev_err(data->dev, "Can't enable pll_a: %d\n", err);
97 return err;
100 err = clk_prepare_enable(data->clk_pll_a_out0);
101 if (err) {
102 dev_err(data->dev, "Can't enable pll_a_out0: %d\n", err);
103 return err;
106 err = clk_prepare_enable(data->clk_cdev1);
107 if (err) {
108 dev_err(data->dev, "Can't enable cdev1: %d\n", err);
109 return err;
112 data->set_baseclock = new_baseclock;
113 data->set_mclk = mclk;
115 return 0;
117 EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_rate);
119 int tegra_asoc_utils_set_ac97_rate(struct tegra_asoc_utils_data *data)
121 const int pll_rate = 73728000;
122 const int ac97_rate = 24576000;
123 int err;
125 clk_disable_unprepare(data->clk_cdev1);
126 clk_disable_unprepare(data->clk_pll_a_out0);
127 clk_disable_unprepare(data->clk_pll_a);
130 * AC97 rate is fixed at 24.576MHz and is used for both the host
131 * controller and the external codec
133 err = clk_set_rate(data->clk_pll_a, pll_rate);
134 if (err) {
135 dev_err(data->dev, "Can't set pll_a rate: %d\n", err);
136 return err;
139 err = clk_set_rate(data->clk_pll_a_out0, ac97_rate);
140 if (err) {
141 dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err);
142 return err;
145 /* Don't set cdev1/extern1 rate; it's locked to pll_a_out0 */
147 err = clk_prepare_enable(data->clk_pll_a);
148 if (err) {
149 dev_err(data->dev, "Can't enable pll_a: %d\n", err);
150 return err;
153 err = clk_prepare_enable(data->clk_pll_a_out0);
154 if (err) {
155 dev_err(data->dev, "Can't enable pll_a_out0: %d\n", err);
156 return err;
159 err = clk_prepare_enable(data->clk_cdev1);
160 if (err) {
161 dev_err(data->dev, "Can't enable cdev1: %d\n", err);
162 return err;
165 data->set_baseclock = pll_rate;
166 data->set_mclk = ac97_rate;
168 return 0;
170 EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_ac97_rate);
172 int tegra_asoc_utils_init(struct tegra_asoc_utils_data *data,
173 struct device *dev)
175 int ret;
177 data->dev = dev;
179 if (of_machine_is_compatible("nvidia,tegra20"))
180 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA20;
181 else if (of_machine_is_compatible("nvidia,tegra30"))
182 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA30;
183 else if (of_machine_is_compatible("nvidia,tegra114"))
184 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA114;
185 else if (of_machine_is_compatible("nvidia,tegra124"))
186 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA124;
187 else {
188 dev_err(data->dev, "SoC unknown to Tegra ASoC utils\n");
189 return -EINVAL;
192 data->clk_pll_a = clk_get(dev, "pll_a");
193 if (IS_ERR(data->clk_pll_a)) {
194 dev_err(data->dev, "Can't retrieve clk pll_a\n");
195 ret = PTR_ERR(data->clk_pll_a);
196 goto err;
199 data->clk_pll_a_out0 = clk_get(dev, "pll_a_out0");
200 if (IS_ERR(data->clk_pll_a_out0)) {
201 dev_err(data->dev, "Can't retrieve clk pll_a_out0\n");
202 ret = PTR_ERR(data->clk_pll_a_out0);
203 goto err_put_pll_a;
206 data->clk_cdev1 = clk_get(dev, "mclk");
207 if (IS_ERR(data->clk_cdev1)) {
208 dev_err(data->dev, "Can't retrieve clk cdev1\n");
209 ret = PTR_ERR(data->clk_cdev1);
210 goto err_put_pll_a_out0;
213 ret = tegra_asoc_utils_set_rate(data, 44100, 256 * 44100);
214 if (ret)
215 goto err_put_cdev1;
217 return 0;
219 err_put_cdev1:
220 clk_put(data->clk_cdev1);
221 err_put_pll_a_out0:
222 clk_put(data->clk_pll_a_out0);
223 err_put_pll_a:
224 clk_put(data->clk_pll_a);
225 err:
226 return ret;
228 EXPORT_SYMBOL_GPL(tegra_asoc_utils_init);
230 void tegra_asoc_utils_fini(struct tegra_asoc_utils_data *data)
232 clk_put(data->clk_cdev1);
233 clk_put(data->clk_pll_a_out0);
234 clk_put(data->clk_pll_a);
236 EXPORT_SYMBOL_GPL(tegra_asoc_utils_fini);
238 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
239 MODULE_DESCRIPTION("Tegra ASoC utility code");
240 MODULE_LICENSE("GPL");