blob: ba419f86384d4cbd6bc568be7273e968100c7312 [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;
46 else
47 new_baseclock = 564480000;
Stephen Warrena50a3992011-01-07 22:36:15 -070048 break;
49 case 8000:
50 case 16000:
51 case 32000:
52 case 48000:
53 case 64000:
54 case 96000:
Stephen Warrenc2f67022012-04-06 11:15:55 -060055 if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20)
56 new_baseclock = 73728000;
57 else
58 new_baseclock = 552960000;
Stephen Warrena50a3992011-01-07 22:36:15 -070059 break;
60 default:
61 return -EINVAL;
62 }
63
Stephen Warren07541392011-04-19 15:25:09 -060064 clk_change = ((new_baseclock != data->set_baseclock) ||
Stephen Warrend64e57c2011-01-28 14:26:40 -070065 (mclk != data->set_mclk));
Stephen Warren07541392011-04-19 15:25:09 -060066 if (!clk_change)
67 return 0;
Stephen Warrena50a3992011-01-07 22:36:15 -070068
Stephen Warrend64e57c2011-01-28 14:26:40 -070069 data->set_baseclock = 0;
70 data->set_mclk = 0;
Stephen Warrena50a3992011-01-07 22:36:15 -070071
Prashant Gaikwad65d2bdd2012-06-05 09:59:42 +053072 clk_disable_unprepare(data->clk_cdev1);
73 clk_disable_unprepare(data->clk_pll_a_out0);
74 clk_disable_unprepare(data->clk_pll_a);
Stephen Warrena50a3992011-01-07 22:36:15 -070075
Stephen Warrend64e57c2011-01-28 14:26:40 -070076 err = clk_set_rate(data->clk_pll_a, new_baseclock);
Stephen Warrena50a3992011-01-07 22:36:15 -070077 if (err) {
Stephen Warrend64e57c2011-01-28 14:26:40 -070078 dev_err(data->dev, "Can't set pll_a rate: %d\n", err);
Stephen Warrena50a3992011-01-07 22:36:15 -070079 return err;
80 }
81
Stephen Warrend64e57c2011-01-28 14:26:40 -070082 err = clk_set_rate(data->clk_pll_a_out0, mclk);
Stephen Warrena50a3992011-01-07 22:36:15 -070083 if (err) {
Stephen Warrend64e57c2011-01-28 14:26:40 -070084 dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err);
Stephen Warrena50a3992011-01-07 22:36:15 -070085 return err;
86 }
87
Stephen Warrenc2f67022012-04-06 11:15:55 -060088 /* Don't set cdev1/extern1 rate; it's locked to pll_a_out0 */
Stephen Warrena50a3992011-01-07 22:36:15 -070089
Prashant Gaikwad65d2bdd2012-06-05 09:59:42 +053090 err = clk_prepare_enable(data->clk_pll_a);
Stephen Warrena50a3992011-01-07 22:36:15 -070091 if (err) {
Stephen Warrend64e57c2011-01-28 14:26:40 -070092 dev_err(data->dev, "Can't enable pll_a: %d\n", err);
Stephen Warrena50a3992011-01-07 22:36:15 -070093 return err;
94 }
95
Prashant Gaikwad65d2bdd2012-06-05 09:59:42 +053096 err = clk_prepare_enable(data->clk_pll_a_out0);
Stephen Warrena50a3992011-01-07 22:36:15 -070097 if (err) {
Stephen Warrend64e57c2011-01-28 14:26:40 -070098 dev_err(data->dev, "Can't enable pll_a_out0: %d\n", err);
Stephen Warrena50a3992011-01-07 22:36:15 -070099 return err;
100 }
101
Prashant Gaikwad65d2bdd2012-06-05 09:59:42 +0530102 err = clk_prepare_enable(data->clk_cdev1);
Stephen Warrena50a3992011-01-07 22:36:15 -0700103 if (err) {
Stephen Warrend64e57c2011-01-28 14:26:40 -0700104 dev_err(data->dev, "Can't enable cdev1: %d\n", err);
Stephen Warrena50a3992011-01-07 22:36:15 -0700105 return err;
106 }
107
Stephen Warrend64e57c2011-01-28 14:26:40 -0700108 data->set_baseclock = new_baseclock;
109 data->set_mclk = mclk;
Stephen Warrena50a3992011-01-07 22:36:15 -0700110
111 return 0;
112}
Stephen Warrena3cd50d2011-02-22 17:23:56 -0700113EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_rate);
Stephen Warrena50a3992011-01-07 22:36:15 -0700114
Lucas Stach919ad492012-12-20 00:17:33 +0100115int tegra_asoc_utils_set_ac97_rate(struct tegra_asoc_utils_data *data)
116{
117 const int pll_rate = 73728000;
118 const int ac97_rate = 24576000;
119 int err;
120
121 clk_disable_unprepare(data->clk_cdev1);
122 clk_disable_unprepare(data->clk_pll_a_out0);
123 clk_disable_unprepare(data->clk_pll_a);
124
125 /*
126 * AC97 rate is fixed at 24.576MHz and is used for both the host
127 * controller and the external codec
128 */
129 err = clk_set_rate(data->clk_pll_a, pll_rate);
130 if (err) {
131 dev_err(data->dev, "Can't set pll_a rate: %d\n", err);
132 return err;
133 }
134
135 err = clk_set_rate(data->clk_pll_a_out0, ac97_rate);
136 if (err) {
137 dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err);
138 return err;
139 }
140
141 /* Don't set cdev1/extern1 rate; it's locked to pll_a_out0 */
142
143 err = clk_prepare_enable(data->clk_pll_a);
144 if (err) {
145 dev_err(data->dev, "Can't enable pll_a: %d\n", err);
146 return err;
147 }
148
149 err = clk_prepare_enable(data->clk_pll_a_out0);
150 if (err) {
151 dev_err(data->dev, "Can't enable pll_a_out0: %d\n", err);
152 return err;
153 }
154
155 err = clk_prepare_enable(data->clk_cdev1);
156 if (err) {
157 dev_err(data->dev, "Can't enable cdev1: %d\n", err);
158 return err;
159 }
160
161 data->set_baseclock = pll_rate;
162 data->set_mclk = ac97_rate;
163
164 return 0;
165}
166EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_ac97_rate);
167
Stephen Warrend64e57c2011-01-28 14:26:40 -0700168int tegra_asoc_utils_init(struct tegra_asoc_utils_data *data,
169 struct device *dev)
Stephen Warrena50a3992011-01-07 22:36:15 -0700170{
171 int ret;
172
Stephen Warrend64e57c2011-01-28 14:26:40 -0700173 data->dev = dev;
174
Stephen Warren8127bf52012-04-10 13:11:17 -0600175 if (of_machine_is_compatible("nvidia,tegra20"))
Stephen Warrenc2f67022012-04-06 11:15:55 -0600176 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA20;
177 else if (of_machine_is_compatible("nvidia,tegra30"))
178 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA30;
Stephen Warren8127bf52012-04-10 13:11:17 -0600179 else if (!dev->of_node)
180 /* non-DT is always Tegra20 */
181 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA20;
Stephen Warrenc2f67022012-04-06 11:15:55 -0600182 else
Stephen Warren8127bf52012-04-10 13:11:17 -0600183 /* DT boot, but unknown SoC */
Stephen Warrenc2f67022012-04-06 11:15:55 -0600184 return -EINVAL;
185
Stephen Warrend64e57c2011-01-28 14:26:40 -0700186 data->clk_pll_a = clk_get_sys(NULL, "pll_a");
187 if (IS_ERR(data->clk_pll_a)) {
188 dev_err(data->dev, "Can't retrieve clk pll_a\n");
189 ret = PTR_ERR(data->clk_pll_a);
Stephen Warrena50a3992011-01-07 22:36:15 -0700190 goto err;
191 }
192
Stephen Warrend64e57c2011-01-28 14:26:40 -0700193 data->clk_pll_a_out0 = clk_get_sys(NULL, "pll_a_out0");
194 if (IS_ERR(data->clk_pll_a_out0)) {
195 dev_err(data->dev, "Can't retrieve clk pll_a_out0\n");
196 ret = PTR_ERR(data->clk_pll_a_out0);
Stephen Warren422650e2011-01-11 12:48:53 -0700197 goto err_put_pll_a;
Stephen Warrena50a3992011-01-07 22:36:15 -0700198 }
199
Stephen Warrenc2f67022012-04-06 11:15:55 -0600200 if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20)
201 data->clk_cdev1 = clk_get_sys(NULL, "cdev1");
202 else
203 data->clk_cdev1 = clk_get_sys("extern1", NULL);
Stephen Warrend64e57c2011-01-28 14:26:40 -0700204 if (IS_ERR(data->clk_cdev1)) {
205 dev_err(data->dev, "Can't retrieve clk cdev1\n");
206 ret = PTR_ERR(data->clk_cdev1);
Stephen Warren422650e2011-01-11 12:48:53 -0700207 goto err_put_pll_a_out0;
Stephen Warrena50a3992011-01-07 22:36:15 -0700208 }
209
Stephen Warrena9005b62012-04-06 11:18:16 -0600210 ret = tegra_asoc_utils_set_rate(data, 44100, 256 * 44100);
211 if (ret)
212 goto err_put_cdev1;
213
Stephen Warrena50a3992011-01-07 22:36:15 -0700214 return 0;
215
Stephen Warrena9005b62012-04-06 11:18:16 -0600216err_put_cdev1:
217 clk_put(data->clk_cdev1);
Stephen Warren422650e2011-01-11 12:48:53 -0700218err_put_pll_a_out0:
Stephen Warrend64e57c2011-01-28 14:26:40 -0700219 clk_put(data->clk_pll_a_out0);
Stephen Warren422650e2011-01-11 12:48:53 -0700220err_put_pll_a:
Stephen Warrend64e57c2011-01-28 14:26:40 -0700221 clk_put(data->clk_pll_a);
Stephen Warrena50a3992011-01-07 22:36:15 -0700222err:
Stephen Warrena50a3992011-01-07 22:36:15 -0700223 return ret;
224}
Stephen Warrena3cd50d2011-02-22 17:23:56 -0700225EXPORT_SYMBOL_GPL(tegra_asoc_utils_init);
Stephen Warrena50a3992011-01-07 22:36:15 -0700226
Stephen Warrend64e57c2011-01-28 14:26:40 -0700227void tegra_asoc_utils_fini(struct tegra_asoc_utils_data *data)
Stephen Warrena50a3992011-01-07 22:36:15 -0700228{
Stephen Warrend64e57c2011-01-28 14:26:40 -0700229 clk_put(data->clk_cdev1);
230 clk_put(data->clk_pll_a_out0);
231 clk_put(data->clk_pll_a);
Stephen Warrena50a3992011-01-07 22:36:15 -0700232}
Stephen Warrena3cd50d2011-02-22 17:23:56 -0700233EXPORT_SYMBOL_GPL(tegra_asoc_utils_fini);
Stephen Warrena50a3992011-01-07 22:36:15 -0700234
Stephen Warrena3cd50d2011-02-22 17:23:56 -0700235MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
236MODULE_DESCRIPTION("Tegra ASoC utility code");
237MODULE_LICENSE("GPL");