Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * tegra_asoc_utils.c - Harmony machine ASoC driver |
| 3 | * |
| 4 | * Author: Stephen Warren <swarren@nvidia.com> |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 5 | * Copyright (C) 2010,2012 - NVIDIA, Inc. |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 6 | * |
| 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 Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 24 | #include <linux/device.h> |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 25 | #include <linux/err.h> |
| 26 | #include <linux/kernel.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 28 | #include <linux/of.h> |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 29 | |
| 30 | #include "tegra_asoc_utils.h" |
| 31 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 32 | int tegra_asoc_utils_set_rate(struct tegra_asoc_utils_data *data, int srate, |
Stephen Warren | 0754139 | 2011-04-19 15:25:09 -0600 | [diff] [blame] | 33 | int mclk) |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 34 | { |
| 35 | int new_baseclock; |
Stephen Warren | 0754139 | 2011-04-19 15:25:09 -0600 | [diff] [blame] | 36 | bool clk_change; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 37 | int err; |
| 38 | |
| 39 | switch (srate) { |
| 40 | case 11025: |
| 41 | case 22050: |
| 42 | case 44100: |
| 43 | case 88200: |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 44 | if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20) |
| 45 | new_baseclock = 56448000; |
Stephen Warren | a7fc5d2 | 2013-03-21 13:56:42 -0600 | [diff] [blame] | 46 | else if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA30) |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 47 | new_baseclock = 564480000; |
Stephen Warren | a7fc5d2 | 2013-03-21 13:56:42 -0600 | [diff] [blame] | 48 | else |
| 49 | new_baseclock = 282240000; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 50 | break; |
| 51 | case 8000: |
| 52 | case 16000: |
| 53 | case 32000: |
| 54 | case 48000: |
| 55 | case 64000: |
| 56 | case 96000: |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 57 | if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20) |
| 58 | new_baseclock = 73728000; |
Stephen Warren | a7fc5d2 | 2013-03-21 13:56:42 -0600 | [diff] [blame] | 59 | else if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA30) |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 60 | new_baseclock = 552960000; |
Stephen Warren | a7fc5d2 | 2013-03-21 13:56:42 -0600 | [diff] [blame] | 61 | else |
| 62 | new_baseclock = 368640000; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 63 | break; |
| 64 | default: |
| 65 | return -EINVAL; |
| 66 | } |
| 67 | |
Stephen Warren | 0754139 | 2011-04-19 15:25:09 -0600 | [diff] [blame] | 68 | clk_change = ((new_baseclock != data->set_baseclock) || |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 69 | (mclk != data->set_mclk)); |
Stephen Warren | 0754139 | 2011-04-19 15:25:09 -0600 | [diff] [blame] | 70 | if (!clk_change) |
| 71 | return 0; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 72 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 73 | data->set_baseclock = 0; |
| 74 | data->set_mclk = 0; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 75 | |
Prashant Gaikwad | 65d2bdd | 2012-06-05 09:59:42 +0530 | [diff] [blame] | 76 | clk_disable_unprepare(data->clk_cdev1); |
| 77 | clk_disable_unprepare(data->clk_pll_a_out0); |
| 78 | clk_disable_unprepare(data->clk_pll_a); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 79 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 80 | err = clk_set_rate(data->clk_pll_a, new_baseclock); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 81 | if (err) { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 82 | dev_err(data->dev, "Can't set pll_a rate: %d\n", err); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 83 | return err; |
| 84 | } |
| 85 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 86 | err = clk_set_rate(data->clk_pll_a_out0, mclk); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 87 | if (err) { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 88 | dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 89 | return err; |
| 90 | } |
| 91 | |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 92 | /* Don't set cdev1/extern1 rate; it's locked to pll_a_out0 */ |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 93 | |
Prashant Gaikwad | 65d2bdd | 2012-06-05 09:59:42 +0530 | [diff] [blame] | 94 | err = clk_prepare_enable(data->clk_pll_a); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 95 | if (err) { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 96 | dev_err(data->dev, "Can't enable pll_a: %d\n", err); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 97 | return err; |
| 98 | } |
| 99 | |
Prashant Gaikwad | 65d2bdd | 2012-06-05 09:59:42 +0530 | [diff] [blame] | 100 | err = clk_prepare_enable(data->clk_pll_a_out0); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 101 | if (err) { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 102 | dev_err(data->dev, "Can't enable pll_a_out0: %d\n", err); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 103 | return err; |
| 104 | } |
| 105 | |
Prashant Gaikwad | 65d2bdd | 2012-06-05 09:59:42 +0530 | [diff] [blame] | 106 | err = clk_prepare_enable(data->clk_cdev1); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 107 | if (err) { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 108 | dev_err(data->dev, "Can't enable cdev1: %d\n", err); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 109 | return err; |
| 110 | } |
| 111 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 112 | data->set_baseclock = new_baseclock; |
| 113 | data->set_mclk = mclk; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 114 | |
| 115 | return 0; |
| 116 | } |
Stephen Warren | a3cd50d | 2011-02-22 17:23:56 -0700 | [diff] [blame] | 117 | EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_rate); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 118 | |
Lucas Stach | 919ad49 | 2012-12-20 00:17:33 +0100 | [diff] [blame] | 119 | int 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 | } |
| 170 | EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_ac97_rate); |
| 171 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 172 | int tegra_asoc_utils_init(struct tegra_asoc_utils_data *data, |
| 173 | struct device *dev) |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 174 | { |
| 175 | int ret; |
| 176 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 177 | data->dev = dev; |
| 178 | |
Stephen Warren | 8127bf5 | 2012-04-10 13:11:17 -0600 | [diff] [blame] | 179 | if (of_machine_is_compatible("nvidia,tegra20")) |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 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; |
Stephen Warren | 110147c | 2013-05-13 13:26:12 -0600 | [diff] [blame] | 183 | else if (of_machine_is_compatible("nvidia,tegra114")) |
Stephen Warren | a7fc5d2 | 2013-03-21 13:56:42 -0600 | [diff] [blame] | 184 | data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA114; |
Stephen Warren | 5e049fc | 2013-10-11 15:43:17 -0600 | [diff] [blame] | 185 | else if (of_machine_is_compatible("nvidia,tegra124")) |
| 186 | data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA124; |
Stephen Warren | 110147c | 2013-05-13 13:26:12 -0600 | [diff] [blame] | 187 | else { |
Stephen Warren | a7fc5d2 | 2013-03-21 13:56:42 -0600 | [diff] [blame] | 188 | dev_err(data->dev, "SoC unknown to Tegra ASoC utils\n"); |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 189 | return -EINVAL; |
Stephen Warren | a7fc5d2 | 2013-03-21 13:56:42 -0600 | [diff] [blame] | 190 | } |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 191 | |
Stephen Warren | 110147c | 2013-05-13 13:26:12 -0600 | [diff] [blame] | 192 | data->clk_pll_a = clk_get(dev, "pll_a"); |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 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); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 196 | goto err; |
| 197 | } |
| 198 | |
Stephen Warren | 110147c | 2013-05-13 13:26:12 -0600 | [diff] [blame] | 199 | data->clk_pll_a_out0 = clk_get(dev, "pll_a_out0"); |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 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); |
Stephen Warren | 422650e | 2011-01-11 12:48:53 -0700 | [diff] [blame] | 203 | goto err_put_pll_a; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Stephen Warren | 110147c | 2013-05-13 13:26:12 -0600 | [diff] [blame] | 206 | data->clk_cdev1 = clk_get(dev, "mclk"); |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 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); |
Stephen Warren | 422650e | 2011-01-11 12:48:53 -0700 | [diff] [blame] | 210 | goto err_put_pll_a_out0; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Stephen Warren | a9005b6 | 2012-04-06 11:18:16 -0600 | [diff] [blame] | 213 | ret = tegra_asoc_utils_set_rate(data, 44100, 256 * 44100); |
| 214 | if (ret) |
| 215 | goto err_put_cdev1; |
| 216 | |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 217 | return 0; |
| 218 | |
Stephen Warren | a9005b6 | 2012-04-06 11:18:16 -0600 | [diff] [blame] | 219 | err_put_cdev1: |
| 220 | clk_put(data->clk_cdev1); |
Stephen Warren | 422650e | 2011-01-11 12:48:53 -0700 | [diff] [blame] | 221 | err_put_pll_a_out0: |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 222 | clk_put(data->clk_pll_a_out0); |
Stephen Warren | 422650e | 2011-01-11 12:48:53 -0700 | [diff] [blame] | 223 | err_put_pll_a: |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 224 | clk_put(data->clk_pll_a); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 225 | err: |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 226 | return ret; |
| 227 | } |
Stephen Warren | a3cd50d | 2011-02-22 17:23:56 -0700 | [diff] [blame] | 228 | EXPORT_SYMBOL_GPL(tegra_asoc_utils_init); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 229 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 230 | void tegra_asoc_utils_fini(struct tegra_asoc_utils_data *data) |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 231 | { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 232 | clk_put(data->clk_cdev1); |
| 233 | clk_put(data->clk_pll_a_out0); |
| 234 | clk_put(data->clk_pll_a); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 235 | } |
Stephen Warren | a3cd50d | 2011-02-22 17:23:56 -0700 | [diff] [blame] | 236 | EXPORT_SYMBOL_GPL(tegra_asoc_utils_fini); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 237 | |
Stephen Warren | a3cd50d | 2011-02-22 17:23:56 -0700 | [diff] [blame] | 238 | MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>"); |
| 239 | MODULE_DESCRIPTION("Tegra ASoC utility code"); |
| 240 | MODULE_LICENSE("GPL"); |