treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / sound / soc / tegra / tegra_asoc_utils.c
blob536a578e95126e9b6c2d90e0a92ede8882c2150c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * tegra_asoc_utils.c - Harmony machine ASoC driver
5 * Author: Stephen Warren <swarren@nvidia.com>
6 * Copyright (C) 2010,2012 - NVIDIA, Inc.
7 */
9 #include <linux/clk.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
16 #include "tegra_asoc_utils.h"
18 int tegra_asoc_utils_set_rate(struct tegra_asoc_utils_data *data, int srate,
19 int mclk)
21 int new_baseclock;
22 bool clk_change;
23 int err;
25 switch (srate) {
26 case 11025:
27 case 22050:
28 case 44100:
29 case 88200:
30 if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20)
31 new_baseclock = 56448000;
32 else if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA30)
33 new_baseclock = 564480000;
34 else
35 new_baseclock = 282240000;
36 break;
37 case 8000:
38 case 16000:
39 case 32000:
40 case 48000:
41 case 64000:
42 case 96000:
43 if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20)
44 new_baseclock = 73728000;
45 else if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA30)
46 new_baseclock = 552960000;
47 else
48 new_baseclock = 368640000;
49 break;
50 default:
51 return -EINVAL;
54 clk_change = ((new_baseclock != data->set_baseclock) ||
55 (mclk != data->set_mclk));
56 if (!clk_change)
57 return 0;
59 data->set_baseclock = 0;
60 data->set_mclk = 0;
62 clk_disable_unprepare(data->clk_cdev1);
63 clk_disable_unprepare(data->clk_pll_a_out0);
64 clk_disable_unprepare(data->clk_pll_a);
66 err = clk_set_rate(data->clk_pll_a, new_baseclock);
67 if (err) {
68 dev_err(data->dev, "Can't set pll_a rate: %d\n", err);
69 return err;
72 err = clk_set_rate(data->clk_pll_a_out0, mclk);
73 if (err) {
74 dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err);
75 return err;
78 /* Don't set cdev1/extern1 rate; it's locked to pll_a_out0 */
80 err = clk_prepare_enable(data->clk_pll_a);
81 if (err) {
82 dev_err(data->dev, "Can't enable pll_a: %d\n", err);
83 return err;
86 err = clk_prepare_enable(data->clk_pll_a_out0);
87 if (err) {
88 dev_err(data->dev, "Can't enable pll_a_out0: %d\n", err);
89 return err;
92 err = clk_prepare_enable(data->clk_cdev1);
93 if (err) {
94 dev_err(data->dev, "Can't enable cdev1: %d\n", err);
95 return err;
98 data->set_baseclock = new_baseclock;
99 data->set_mclk = mclk;
101 return 0;
103 EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_rate);
105 int tegra_asoc_utils_set_ac97_rate(struct tegra_asoc_utils_data *data)
107 const int pll_rate = 73728000;
108 const int ac97_rate = 24576000;
109 int err;
111 clk_disable_unprepare(data->clk_cdev1);
112 clk_disable_unprepare(data->clk_pll_a_out0);
113 clk_disable_unprepare(data->clk_pll_a);
116 * AC97 rate is fixed at 24.576MHz and is used for both the host
117 * controller and the external codec
119 err = clk_set_rate(data->clk_pll_a, pll_rate);
120 if (err) {
121 dev_err(data->dev, "Can't set pll_a rate: %d\n", err);
122 return err;
125 err = clk_set_rate(data->clk_pll_a_out0, ac97_rate);
126 if (err) {
127 dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err);
128 return err;
131 /* Don't set cdev1/extern1 rate; it's locked to pll_a_out0 */
133 err = clk_prepare_enable(data->clk_pll_a);
134 if (err) {
135 dev_err(data->dev, "Can't enable pll_a: %d\n", err);
136 return err;
139 err = clk_prepare_enable(data->clk_pll_a_out0);
140 if (err) {
141 dev_err(data->dev, "Can't enable pll_a_out0: %d\n", err);
142 return err;
145 err = clk_prepare_enable(data->clk_cdev1);
146 if (err) {
147 dev_err(data->dev, "Can't enable cdev1: %d\n", err);
148 return err;
151 data->set_baseclock = pll_rate;
152 data->set_mclk = ac97_rate;
154 return 0;
156 EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_ac97_rate);
158 int tegra_asoc_utils_init(struct tegra_asoc_utils_data *data,
159 struct device *dev)
161 int ret;
163 data->dev = dev;
165 if (of_machine_is_compatible("nvidia,tegra20"))
166 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA20;
167 else if (of_machine_is_compatible("nvidia,tegra30"))
168 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA30;
169 else if (of_machine_is_compatible("nvidia,tegra114"))
170 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA114;
171 else if (of_machine_is_compatible("nvidia,tegra124"))
172 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA124;
173 else {
174 dev_err(data->dev, "SoC unknown to Tegra ASoC utils\n");
175 return -EINVAL;
178 data->clk_pll_a = clk_get(dev, "pll_a");
179 if (IS_ERR(data->clk_pll_a)) {
180 dev_err(data->dev, "Can't retrieve clk pll_a\n");
181 ret = PTR_ERR(data->clk_pll_a);
182 goto err;
185 data->clk_pll_a_out0 = clk_get(dev, "pll_a_out0");
186 if (IS_ERR(data->clk_pll_a_out0)) {
187 dev_err(data->dev, "Can't retrieve clk pll_a_out0\n");
188 ret = PTR_ERR(data->clk_pll_a_out0);
189 goto err_put_pll_a;
192 data->clk_cdev1 = clk_get(dev, "mclk");
193 if (IS_ERR(data->clk_cdev1)) {
194 dev_err(data->dev, "Can't retrieve clk cdev1\n");
195 ret = PTR_ERR(data->clk_cdev1);
196 goto err_put_pll_a_out0;
199 ret = tegra_asoc_utils_set_rate(data, 44100, 256 * 44100);
200 if (ret)
201 goto err_put_cdev1;
203 return 0;
205 err_put_cdev1:
206 clk_put(data->clk_cdev1);
207 err_put_pll_a_out0:
208 clk_put(data->clk_pll_a_out0);
209 err_put_pll_a:
210 clk_put(data->clk_pll_a);
211 err:
212 return ret;
214 EXPORT_SYMBOL_GPL(tegra_asoc_utils_init);
216 void tegra_asoc_utils_fini(struct tegra_asoc_utils_data *data)
218 clk_put(data->clk_cdev1);
219 clk_put(data->clk_pll_a_out0);
220 clk_put(data->clk_pll_a);
222 EXPORT_SYMBOL_GPL(tegra_asoc_utils_fini);
224 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
225 MODULE_DESCRIPTION("Tegra ASoC utility code");
226 MODULE_LICENSE("GPL");