blob: 25903ed850c81497bfcb4c5c9b4b90b362b7f179 [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Stephen Warrena50a3992011-01-07 22:36:15 -07002/*
3 * tegra_asoc_utils.c - Harmony machine ASoC driver
4 *
5 * Author: Stephen Warren <swarren@nvidia.com>
Stephen Warrenc2f67022012-04-06 11:15:55 -06006 * Copyright (C) 2010,2012 - NVIDIA, Inc.
Stephen Warrena50a3992011-01-07 22:36:15 -07007 */
8
9#include <linux/clk.h>
Stephen Warrend64e57c2011-01-28 14:26:40 -070010#include <linux/device.h>
Stephen Warrena50a3992011-01-07 22:36:15 -070011#include <linux/err.h>
12#include <linux/kernel.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040013#include <linux/module.h>
Stephen Warrenc2f67022012-04-06 11:15:55 -060014#include <linux/of.h>
Stephen Warrena50a3992011-01-07 22:36:15 -070015
16#include "tegra_asoc_utils.h"
17
Stephen Warrend64e57c2011-01-28 14:26:40 -070018int tegra_asoc_utils_set_rate(struct tegra_asoc_utils_data *data, int srate,
Stephen Warren07541392011-04-19 15:25:09 -060019 int mclk)
Stephen Warrena50a3992011-01-07 22:36:15 -070020{
21 int new_baseclock;
Stephen Warren07541392011-04-19 15:25:09 -060022 bool clk_change;
Stephen Warrena50a3992011-01-07 22:36:15 -070023 int err;
24
25 switch (srate) {
26 case 11025:
27 case 22050:
28 case 44100:
29 case 88200:
Stephen Warrenc2f67022012-04-06 11:15:55 -060030 if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20)
31 new_baseclock = 56448000;
Stephen Warrena7fc5d22013-03-21 13:56:42 -060032 else if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA30)
Stephen Warrenc2f67022012-04-06 11:15:55 -060033 new_baseclock = 564480000;
Stephen Warrena7fc5d22013-03-21 13:56:42 -060034 else
35 new_baseclock = 282240000;
Stephen Warrena50a3992011-01-07 22:36:15 -070036 break;
37 case 8000:
38 case 16000:
39 case 32000:
40 case 48000:
41 case 64000:
42 case 96000:
Stephen Warrenc2f67022012-04-06 11:15:55 -060043 if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20)
44 new_baseclock = 73728000;
Stephen Warrena7fc5d22013-03-21 13:56:42 -060045 else if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA30)
Stephen Warrenc2f67022012-04-06 11:15:55 -060046 new_baseclock = 552960000;
Stephen Warrena7fc5d22013-03-21 13:56:42 -060047 else
48 new_baseclock = 368640000;
Stephen Warrena50a3992011-01-07 22:36:15 -070049 break;
50 default:
51 return -EINVAL;
52 }
53
Stephen Warren07541392011-04-19 15:25:09 -060054 clk_change = ((new_baseclock != data->set_baseclock) ||
Stephen Warrend64e57c2011-01-28 14:26:40 -070055 (mclk != data->set_mclk));
Stephen Warren07541392011-04-19 15:25:09 -060056 if (!clk_change)
57 return 0;
Stephen Warrena50a3992011-01-07 22:36:15 -070058
Stephen Warrend64e57c2011-01-28 14:26:40 -070059 data->set_baseclock = 0;
60 data->set_mclk = 0;
Stephen Warrena50a3992011-01-07 22:36:15 -070061
Prashant Gaikwad65d2bdd2012-06-05 09:59:42 +053062 clk_disable_unprepare(data->clk_cdev1);
Stephen Warrena50a3992011-01-07 22:36:15 -070063
Stephen Warrend64e57c2011-01-28 14:26:40 -070064 err = clk_set_rate(data->clk_pll_a, new_baseclock);
Stephen Warrena50a3992011-01-07 22:36:15 -070065 if (err) {
Stephen Warrend64e57c2011-01-28 14:26:40 -070066 dev_err(data->dev, "Can't set pll_a rate: %d\n", err);
Stephen Warrena50a3992011-01-07 22:36:15 -070067 return err;
68 }
69
Stephen Warrend64e57c2011-01-28 14:26:40 -070070 err = clk_set_rate(data->clk_pll_a_out0, mclk);
Stephen Warrena50a3992011-01-07 22:36:15 -070071 if (err) {
Stephen Warrend64e57c2011-01-28 14:26:40 -070072 dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err);
Stephen Warrena50a3992011-01-07 22:36:15 -070073 return err;
74 }
75
Stephen Warrenc2f67022012-04-06 11:15:55 -060076 /* Don't set cdev1/extern1 rate; it's locked to pll_a_out0 */
Stephen Warrena50a3992011-01-07 22:36:15 -070077
Prashant Gaikwad65d2bdd2012-06-05 09:59:42 +053078 err = clk_prepare_enable(data->clk_cdev1);
Stephen Warrena50a3992011-01-07 22:36:15 -070079 if (err) {
Stephen Warrend64e57c2011-01-28 14:26:40 -070080 dev_err(data->dev, "Can't enable cdev1: %d\n", err);
Stephen Warrena50a3992011-01-07 22:36:15 -070081 return err;
82 }
83
Stephen Warrend64e57c2011-01-28 14:26:40 -070084 data->set_baseclock = new_baseclock;
85 data->set_mclk = mclk;
Stephen Warrena50a3992011-01-07 22:36:15 -070086
87 return 0;
88}
Stephen Warrena3cd50d2011-02-22 17:23:56 -070089EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_rate);
Stephen Warrena50a3992011-01-07 22:36:15 -070090
Lucas Stach919ad492012-12-20 00:17:33 +010091int tegra_asoc_utils_set_ac97_rate(struct tegra_asoc_utils_data *data)
92{
93 const int pll_rate = 73728000;
94 const int ac97_rate = 24576000;
95 int err;
96
97 clk_disable_unprepare(data->clk_cdev1);
Lucas Stach919ad492012-12-20 00:17:33 +010098
99 /*
100 * AC97 rate is fixed at 24.576MHz and is used for both the host
101 * controller and the external codec
102 */
103 err = clk_set_rate(data->clk_pll_a, pll_rate);
104 if (err) {
105 dev_err(data->dev, "Can't set pll_a rate: %d\n", err);
106 return err;
107 }
108
109 err = clk_set_rate(data->clk_pll_a_out0, ac97_rate);
110 if (err) {
111 dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err);
112 return err;
113 }
114
115 /* Don't set cdev1/extern1 rate; it's locked to pll_a_out0 */
116
Lucas Stach919ad492012-12-20 00:17:33 +0100117 err = clk_prepare_enable(data->clk_cdev1);
118 if (err) {
119 dev_err(data->dev, "Can't enable cdev1: %d\n", err);
120 return err;
121 }
122
123 data->set_baseclock = pll_rate;
124 data->set_mclk = ac97_rate;
125
126 return 0;
127}
128EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_ac97_rate);
129
Stephen Warrend64e57c2011-01-28 14:26:40 -0700130int tegra_asoc_utils_init(struct tegra_asoc_utils_data *data,
131 struct device *dev)
Stephen Warrena50a3992011-01-07 22:36:15 -0700132{
Sowjanya Komatineni1e4e0bf2020-01-13 23:24:23 -0800133 struct clk *clk_out_1, *clk_extern1;
Stephen Warrena50a3992011-01-07 22:36:15 -0700134 int ret;
135
Stephen Warrend64e57c2011-01-28 14:26:40 -0700136 data->dev = dev;
137
Stephen Warren8127bf52012-04-10 13:11:17 -0600138 if (of_machine_is_compatible("nvidia,tegra20"))
Stephen Warrenc2f67022012-04-06 11:15:55 -0600139 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA20;
140 else if (of_machine_is_compatible("nvidia,tegra30"))
141 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA30;
Stephen Warren110147c2013-05-13 13:26:12 -0600142 else if (of_machine_is_compatible("nvidia,tegra114"))
Stephen Warrena7fc5d22013-03-21 13:56:42 -0600143 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA114;
Stephen Warren5e049fc2013-10-11 15:43:17 -0600144 else if (of_machine_is_compatible("nvidia,tegra124"))
145 data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA124;
Stephen Warren110147c2013-05-13 13:26:12 -0600146 else {
Stephen Warrena7fc5d22013-03-21 13:56:42 -0600147 dev_err(data->dev, "SoC unknown to Tegra ASoC utils\n");
Stephen Warrenc2f67022012-04-06 11:15:55 -0600148 return -EINVAL;
Stephen Warrena7fc5d22013-03-21 13:56:42 -0600149 }
Stephen Warrenc2f67022012-04-06 11:15:55 -0600150
Sowjanya Komatineni0de6db32020-01-13 23:24:17 -0800151 data->clk_pll_a = devm_clk_get(dev, "pll_a");
Stephen Warrend64e57c2011-01-28 14:26:40 -0700152 if (IS_ERR(data->clk_pll_a)) {
153 dev_err(data->dev, "Can't retrieve clk pll_a\n");
Sowjanya Komatineni0de6db32020-01-13 23:24:17 -0800154 return PTR_ERR(data->clk_pll_a);
Stephen Warrena50a3992011-01-07 22:36:15 -0700155 }
156
Sowjanya Komatineni0de6db32020-01-13 23:24:17 -0800157 data->clk_pll_a_out0 = devm_clk_get(dev, "pll_a_out0");
Stephen Warrend64e57c2011-01-28 14:26:40 -0700158 if (IS_ERR(data->clk_pll_a_out0)) {
159 dev_err(data->dev, "Can't retrieve clk pll_a_out0\n");
Sowjanya Komatineni0de6db32020-01-13 23:24:17 -0800160 return PTR_ERR(data->clk_pll_a_out0);
Stephen Warrena50a3992011-01-07 22:36:15 -0700161 }
162
Sowjanya Komatineni0de6db32020-01-13 23:24:17 -0800163 data->clk_cdev1 = devm_clk_get(dev, "mclk");
Stephen Warrend64e57c2011-01-28 14:26:40 -0700164 if (IS_ERR(data->clk_cdev1)) {
165 dev_err(data->dev, "Can't retrieve clk cdev1\n");
Sowjanya Komatineni0de6db32020-01-13 23:24:17 -0800166 return PTR_ERR(data->clk_cdev1);
Stephen Warrena50a3992011-01-07 22:36:15 -0700167 }
168
Sowjanya Komatineni1e4e0bf2020-01-13 23:24:23 -0800169 /*
170 * If clock parents are not set in DT, configure here to use clk_out_1
171 * as mclk and extern1 as parent for Tegra30 and higher.
172 */
173 if (!of_find_property(dev->of_node, "assigned-clock-parents", NULL) &&
174 data->soc > TEGRA_ASOC_UTILS_SOC_TEGRA20) {
175 dev_warn(data->dev,
176 "Configuring clocks for a legacy device-tree\n");
177 dev_warn(data->dev,
178 "Please update DT to use assigned-clock-parents\n");
179 clk_extern1 = devm_clk_get(dev, "extern1");
180 if (IS_ERR(clk_extern1)) {
181 dev_err(data->dev, "Can't retrieve clk extern1\n");
182 return PTR_ERR(clk_extern1);
183 }
184
185 ret = clk_set_parent(clk_extern1, data->clk_pll_a_out0);
186 if (ret < 0) {
187 dev_err(data->dev,
188 "Set parent failed for clk extern1\n");
189 return ret;
190 }
191
192 clk_out_1 = devm_clk_get(dev, "pmc_clk_out_1");
193 if (IS_ERR(clk_out_1)) {
194 dev_err(data->dev, "Can't retrieve pmc_clk_out_1\n");
195 return PTR_ERR(clk_out_1);
196 }
197
198 ret = clk_set_parent(clk_out_1, clk_extern1);
199 if (ret < 0) {
200 dev_err(data->dev,
201 "Set parent failed for pmc_clk_out_1\n");
202 return ret;
203 }
204
205 data->clk_cdev1 = clk_out_1;
206 }
207
Stephen Warrena9005b62012-04-06 11:18:16 -0600208 ret = tegra_asoc_utils_set_rate(data, 44100, 256 * 44100);
209 if (ret)
Sowjanya Komatineni0de6db32020-01-13 23:24:17 -0800210 return ret;
Stephen Warrena9005b62012-04-06 11:18:16 -0600211
Stephen Warrena50a3992011-01-07 22:36:15 -0700212 return 0;
Stephen Warrena50a3992011-01-07 22:36:15 -0700213}
Stephen Warrena3cd50d2011-02-22 17:23:56 -0700214EXPORT_SYMBOL_GPL(tegra_asoc_utils_init);
Stephen Warrena50a3992011-01-07 22:36:15 -0700215
Stephen Warrena3cd50d2011-02-22 17:23:56 -0700216MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
217MODULE_DESCRIPTION("Tegra ASoC utility code");
218MODULE_LICENSE("GPL");