blob: d173880f290d0f762ec0151fb442e815354b8815 [file] [log] [blame]
Stephen Warrena50a3992011-01-07 22:36:15 -07001/*
2 * tegra_asoc_utils.c - Harmony machine ASoC driver
3 *
4 * Author: Stephen Warren <swarren@nvidia.com>
Stephen Warrenc2f67022012-04-06 11:15:55 -06005 * Copyright (C) 2010,2012 - NVIDIA, Inc.
Stephen Warrena50a3992011-01-07 22:36:15 -07006 *
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.
10 *
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.
15 *
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
20 *
21 */
22
23#include <linux/clk.h>
Stephen Warrend64e57c2011-01-28 14:26:40 -070024#include <linux/device.h>
Stephen Warrena50a3992011-01-07 22:36:15 -070025#include <linux/err.h>
26#include <linux/kernel.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Stephen Warrenc2f67022012-04-06 11:15:55 -060028#include <linux/of.h>
Stephen Warrena50a3992011-01-07 22:36:15 -070029
30#include "tegra_asoc_utils.h"
31
Stephen Warrend64e57c2011-01-28 14:26:40 -070032int tegra_asoc_utils_set_rate(struct tegra_asoc_utils_data *data, int srate,
Stephen Warren07541392011-04-19 15:25:09 -060033 int mclk)
Stephen Warrena50a3992011-01-07 22:36:15 -070034{
35 int new_baseclock;
Stephen Warren07541392011-04-19 15:25:09 -060036 bool clk_change;
Stephen Warrena50a3992011-01-07 22:36:15 -070037 int err;
38
39 switch (srate) {
40 case 11025:
41 case 22050:
42 case 44100:
43 case 88200:
Stephen Warrenc2f67022012-04-06 11:15:55 -060044 if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20)
45 new_baseclock = 56448000;
Stephen Warrena7fc5d22013-03-21 13:56:42 -060046 else if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA30)
Stephen Warrenc2f67022012-04-06 11:15:55 -060047 new_baseclock = 564480000;
Stephen Warrena7fc5d22013-03-21 13:56:42 -060048 else
49 new_baseclock = 282240000;
Stephen Warrena50a3992011-01-07 22:36:15 -070050 break;
51 case 8000:
52 case 16000:
53 case 32000:
54 case 48000:
55 case 64000:
56 case 96000:
Stephen Warrenc2f67022012-04-06 11:15:55 -060057 if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20)
58 new_baseclock = 73728000;
Stephen Warrena7fc5d22013-03-21 13:56:42 -060059 else if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA30)
Stephen Warrenc2f67022012-04-06 11:15:55 -060060 new_baseclock = 552960000;
Stephen Warrena7fc5d22013-03-21 13:56:42 -060061 else
62 new_baseclock = 368640000;
Stephen Warrena50a3992011-01-07 22:36:15 -070063 break;
64 default:
65 return -EINVAL;
66 }
67
Stephen Warren07541392011-04-19 15:25:09 -060068 clk_change = ((new_baseclock != data->set_baseclock) ||
Stephen Warrend64e57c2011-01-28 14:26:40 -070069 (mclk != data->set_mclk));
Stephen Warren07541392011-04-19 15:25:09 -060070 if (!clk_change)
71 return 0;
Stephen Warrena50a3992011-01-07 22:36:15 -070072
Stephen Warrend64e57c2011-01-28 14:26:40 -070073 data->set_baseclock = 0;
74 data->set_mclk = 0;
Stephen Warrena50a3992011-01-07 22:36:15 -070075
Prashant Gaikwad65d2bdd2012-06-05 09:59:42 +053076 clk_disable_unprepare(data->clk_cdev1);
77 clk_disable_unprepare(data->clk_pll_a_out0);
78 clk_disable_unprepare(data->clk_pll_a);
Stephen Warrena50a3992011-01-07 22:36:15 -070079
Stephen Warrend64e57c2011-01-28 14:26:40 -070080 err = clk_set_rate(data->clk_pll_a, new_baseclock);
Stephen Warrena50a3992011-01-07 22:36:15 -070081 if (err) {
Stephen Warrend64e57c2011-01-28 14:26:40 -070082 dev_err(data->dev, "Can't set pll_a rate: %d\n", err);
Stephen Warrena50a3992011-01-07 22:36:15 -070083 return err;
84 }
85
Stephen Warrend64e57c2011-01-28 14:26:40 -070086 err = clk_set_rate(data->clk_pll_a_out0, mclk);
Stephen Warrena50a3992011-01-07 22:36:15 -070087 if (err) {
Stephen Warrend64e57c2011-01-28 14:26:40 -070088 dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err);
Stephen Warrena50a3992011-01-07 22:36:15 -070089 return err;
90 }
91
Stephen Warrenc2f67022012-04-06 11:15:55 -060092 /* Don't set cdev1/extern1 rate; it's locked to pll_a_out0 */
Stephen Warrena50a3992011-01-07 22:36:15 -070093
Prashant Gaikwad65d2bdd2012-06-05 09:59:42 +053094 err = clk_prepare_enable(data->clk_pll_a);
Stephen Warrena50a3992011-01-07 22:36:15 -070095 if (err) {
Stephen Warrend64e57c2011-01-28 14:26:40 -070096 dev_err(data->dev, "Can't enable pll_a: %d\n", err);
Stephen Warrena50a3992011-01-07 22:36:15 -070097 return err;
98 }
99
Prashant Gaikwad65d2bdd2012-06-05 09:59:42 +0530100 err = clk_prepare_enable(data->clk_pll_a_out0);
Stephen Warrena50a3992011-01-07 22:36:15 -0700101 if (err) {
Stephen Warrend64e57c2011-01-28 14:26:40 -0700102 dev_err(data->dev, "Can't enable pll_a_out0: %d\n", err);
Stephen Warrena50a3992011-01-07 22:36:15 -0700103 return err;
104 }
105
Prashant Gaikwad65d2bdd2012-06-05 09:59:42 +0530106 err = clk_prepare_enable(data->clk_cdev1);
Stephen Warrena50a3992011-01-07 22:36:15 -0700107 if (err) {
Stephen Warrend64e57c2011-01-28 14:26:40 -0700108 dev_err(data->dev, "Can't enable cdev1: %d\n", err);
Stephen Warrena50a3992011-01-07 22:36:15 -0700109 return err;
110 }
111
Stephen Warrend64e57c2011-01-28 14:26:40 -0700112 data->set_baseclock = new_baseclock;
113 data->set_mclk = mclk;
Stephen Warrena50a3992011-01-07 22:36:15 -0700114
115 return 0;
116}
Stephen Warrena3cd50d2011-02-22 17:23:56 -0700117EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_rate);
Stephen Warrena50a3992011-01-07 22:36:15 -0700118
Lucas Stach919ad492012-12-20 00:17:33 +0100119int tegra_asoc_utils_set_ac97_rate(struct tegra_asoc_utils_data *data)
120{
121 const int pll_rate = 73728000;
122 const int ac97_rate = 24576000;
123 int err;
124
125 clk_disable_unprepare(data->clk_cdev1);
126 clk_disable_unprepare(data->clk_pll_a_out0);
127 clk_disable_unprepare(data->clk_pll_a);
128
129 /*
130 * AC97 rate is fixed at 24.576MHz and is used for both the host
131 * controller and the external codec
132 */
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;
137 }
138
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;
143 }
144
145 /* Don't set cdev1/extern1 rate; it's locked to pll_a_out0 */
146
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;
151 }
152
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;
157 }
158
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;
163 }
164
165 data->set_baseclock = pll_rate;
166 data->set_mclk = ac97_rate;
167
168 return 0;
169}
170EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_ac97_rate);
171
Stephen Warrend64e57c2011-01-28 14:26:40 -0700172int tegra_asoc_utils_init(struct tegra_asoc_utils_data *data,
173 struct device *dev)
Stephen Warrena50a3992011-01-07 22:36:15 -0700174{
175 int ret;
176
Stephen Warrend64e57c2011-01-28 14:26:40 -0700177 data->dev = dev;
178
Stephen Warren8127bf52012-04-10 13:11:17 -0600179 if (of_machine_is_compatible("nvidia,tegra20"))
Stephen Warrenc2f67022012-04-06 11:15:55 -0600180 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA20;
181 else if (of_machine_is_compatible("nvidia,tegra30"))
182 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA30;
Stephen Warren110147c2013-05-13 13:26:12 -0600183 else if (of_machine_is_compatible("nvidia,tegra114"))
Stephen Warrena7fc5d22013-03-21 13:56:42 -0600184 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA114;
Stephen Warren110147c2013-05-13 13:26:12 -0600185 else {
Stephen Warrena7fc5d22013-03-21 13:56:42 -0600186 dev_err(data->dev, "SoC unknown to Tegra ASoC utils\n");
Stephen Warrenc2f67022012-04-06 11:15:55 -0600187 return -EINVAL;
Stephen Warrena7fc5d22013-03-21 13:56:42 -0600188 }
Stephen Warrenc2f67022012-04-06 11:15:55 -0600189
Stephen Warren110147c2013-05-13 13:26:12 -0600190 data->clk_pll_a = clk_get(dev, "pll_a");
Stephen Warrend64e57c2011-01-28 14:26:40 -0700191 if (IS_ERR(data->clk_pll_a)) {
192 dev_err(data->dev, "Can't retrieve clk pll_a\n");
193 ret = PTR_ERR(data->clk_pll_a);
Stephen Warrena50a3992011-01-07 22:36:15 -0700194 goto err;
195 }
196
Stephen Warren110147c2013-05-13 13:26:12 -0600197 data->clk_pll_a_out0 = clk_get(dev, "pll_a_out0");
Stephen Warrend64e57c2011-01-28 14:26:40 -0700198 if (IS_ERR(data->clk_pll_a_out0)) {
199 dev_err(data->dev, "Can't retrieve clk pll_a_out0\n");
200 ret = PTR_ERR(data->clk_pll_a_out0);
Stephen Warren422650e2011-01-11 12:48:53 -0700201 goto err_put_pll_a;
Stephen Warrena50a3992011-01-07 22:36:15 -0700202 }
203
Stephen Warren110147c2013-05-13 13:26:12 -0600204 data->clk_cdev1 = clk_get(dev, "mclk");
Stephen Warrend64e57c2011-01-28 14:26:40 -0700205 if (IS_ERR(data->clk_cdev1)) {
206 dev_err(data->dev, "Can't retrieve clk cdev1\n");
207 ret = PTR_ERR(data->clk_cdev1);
Stephen Warren422650e2011-01-11 12:48:53 -0700208 goto err_put_pll_a_out0;
Stephen Warrena50a3992011-01-07 22:36:15 -0700209 }
210
Stephen Warrena9005b62012-04-06 11:18:16 -0600211 ret = tegra_asoc_utils_set_rate(data, 44100, 256 * 44100);
212 if (ret)
213 goto err_put_cdev1;
214
Stephen Warrena50a3992011-01-07 22:36:15 -0700215 return 0;
216
Stephen Warrena9005b62012-04-06 11:18:16 -0600217err_put_cdev1:
218 clk_put(data->clk_cdev1);
Stephen Warren422650e2011-01-11 12:48:53 -0700219err_put_pll_a_out0:
Stephen Warrend64e57c2011-01-28 14:26:40 -0700220 clk_put(data->clk_pll_a_out0);
Stephen Warren422650e2011-01-11 12:48:53 -0700221err_put_pll_a:
Stephen Warrend64e57c2011-01-28 14:26:40 -0700222 clk_put(data->clk_pll_a);
Stephen Warrena50a3992011-01-07 22:36:15 -0700223err:
Stephen Warrena50a3992011-01-07 22:36:15 -0700224 return ret;
225}
Stephen Warrena3cd50d2011-02-22 17:23:56 -0700226EXPORT_SYMBOL_GPL(tegra_asoc_utils_init);
Stephen Warrena50a3992011-01-07 22:36:15 -0700227
Stephen Warrend64e57c2011-01-28 14:26:40 -0700228void tegra_asoc_utils_fini(struct tegra_asoc_utils_data *data)
Stephen Warrena50a3992011-01-07 22:36:15 -0700229{
Stephen Warrend64e57c2011-01-28 14:26:40 -0700230 clk_put(data->clk_cdev1);
231 clk_put(data->clk_pll_a_out0);
232 clk_put(data->clk_pll_a);
Stephen Warrena50a3992011-01-07 22:36:15 -0700233}
Stephen Warrena3cd50d2011-02-22 17:23:56 -0700234EXPORT_SYMBOL_GPL(tegra_asoc_utils_fini);
Stephen Warrena50a3992011-01-07 22:36:15 -0700235
Stephen Warrena3cd50d2011-02-22 17:23:56 -0700236MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
237MODULE_DESCRIPTION("Tegra ASoC utility code");
238MODULE_LICENSE("GPL");