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; |
| 46 | else |
| 47 | new_baseclock = 564480000; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 48 | break; |
| 49 | case 8000: |
| 50 | case 16000: |
| 51 | case 32000: |
| 52 | case 48000: |
| 53 | case 64000: |
| 54 | case 96000: |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 55 | if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20) |
| 56 | new_baseclock = 73728000; |
| 57 | else |
| 58 | new_baseclock = 552960000; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 59 | break; |
| 60 | default: |
| 61 | return -EINVAL; |
| 62 | } |
| 63 | |
Stephen Warren | 0754139 | 2011-04-19 15:25:09 -0600 | [diff] [blame] | 64 | clk_change = ((new_baseclock != data->set_baseclock) || |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 65 | (mclk != data->set_mclk)); |
Stephen Warren | 0754139 | 2011-04-19 15:25:09 -0600 | [diff] [blame] | 66 | if (!clk_change) |
| 67 | return 0; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 68 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 69 | data->set_baseclock = 0; |
| 70 | data->set_mclk = 0; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 71 | |
Prashant Gaikwad | 65d2bdd | 2012-06-05 09:59:42 +0530 | [diff] [blame] | 72 | clk_disable_unprepare(data->clk_cdev1); |
| 73 | clk_disable_unprepare(data->clk_pll_a_out0); |
| 74 | clk_disable_unprepare(data->clk_pll_a); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 75 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 76 | err = clk_set_rate(data->clk_pll_a, new_baseclock); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 77 | if (err) { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 78 | 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] | 79 | return err; |
| 80 | } |
| 81 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 82 | err = clk_set_rate(data->clk_pll_a_out0, mclk); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 83 | if (err) { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 84 | 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] | 85 | return err; |
| 86 | } |
| 87 | |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 88 | /* 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] | 89 | |
Prashant Gaikwad | 65d2bdd | 2012-06-05 09:59:42 +0530 | [diff] [blame] | 90 | err = clk_prepare_enable(data->clk_pll_a); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 91 | if (err) { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 92 | dev_err(data->dev, "Can't enable pll_a: %d\n", err); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 93 | return err; |
| 94 | } |
| 95 | |
Prashant Gaikwad | 65d2bdd | 2012-06-05 09:59:42 +0530 | [diff] [blame] | 96 | err = clk_prepare_enable(data->clk_pll_a_out0); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 97 | if (err) { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 98 | 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] | 99 | return err; |
| 100 | } |
| 101 | |
Prashant Gaikwad | 65d2bdd | 2012-06-05 09:59:42 +0530 | [diff] [blame] | 102 | err = clk_prepare_enable(data->clk_cdev1); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 103 | if (err) { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 104 | dev_err(data->dev, "Can't enable cdev1: %d\n", err); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 105 | return err; |
| 106 | } |
| 107 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 108 | data->set_baseclock = new_baseclock; |
| 109 | data->set_mclk = mclk; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
Stephen Warren | a3cd50d | 2011-02-22 17:23:56 -0700 | [diff] [blame] | 113 | EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_rate); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 114 | |
Lucas Stach | 919ad49 | 2012-12-20 00:17:33 +0100 | [diff] [blame] | 115 | int 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 | } |
| 166 | EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_ac97_rate); |
| 167 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 168 | int tegra_asoc_utils_init(struct tegra_asoc_utils_data *data, |
| 169 | struct device *dev) |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 170 | { |
| 171 | int ret; |
| 172 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 173 | data->dev = dev; |
| 174 | |
Stephen Warren | 8127bf5 | 2012-04-10 13:11:17 -0600 | [diff] [blame] | 175 | if (of_machine_is_compatible("nvidia,tegra20")) |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 176 | 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 Warren | 8127bf5 | 2012-04-10 13:11:17 -0600 | [diff] [blame] | 179 | else if (!dev->of_node) |
| 180 | /* non-DT is always Tegra20 */ |
| 181 | data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA20; |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 182 | else |
Stephen Warren | 8127bf5 | 2012-04-10 13:11:17 -0600 | [diff] [blame] | 183 | /* DT boot, but unknown SoC */ |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 184 | return -EINVAL; |
| 185 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 186 | 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 Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 190 | goto err; |
| 191 | } |
| 192 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 193 | 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 Warren | 422650e | 2011-01-11 12:48:53 -0700 | [diff] [blame] | 197 | goto err_put_pll_a; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 200 | 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 Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 204 | 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 Warren | 422650e | 2011-01-11 12:48:53 -0700 | [diff] [blame] | 207 | goto err_put_pll_a_out0; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Stephen Warren | a9005b6 | 2012-04-06 11:18:16 -0600 | [diff] [blame] | 210 | ret = tegra_asoc_utils_set_rate(data, 44100, 256 * 44100); |
| 211 | if (ret) |
| 212 | goto err_put_cdev1; |
| 213 | |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 214 | return 0; |
| 215 | |
Stephen Warren | a9005b6 | 2012-04-06 11:18:16 -0600 | [diff] [blame] | 216 | err_put_cdev1: |
| 217 | clk_put(data->clk_cdev1); |
Stephen Warren | 422650e | 2011-01-11 12:48:53 -0700 | [diff] [blame] | 218 | err_put_pll_a_out0: |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 219 | clk_put(data->clk_pll_a_out0); |
Stephen Warren | 422650e | 2011-01-11 12:48:53 -0700 | [diff] [blame] | 220 | err_put_pll_a: |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 221 | clk_put(data->clk_pll_a); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 222 | err: |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 223 | return ret; |
| 224 | } |
Stephen Warren | a3cd50d | 2011-02-22 17:23:56 -0700 | [diff] [blame] | 225 | EXPORT_SYMBOL_GPL(tegra_asoc_utils_init); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 226 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 227 | void tegra_asoc_utils_fini(struct tegra_asoc_utils_data *data) |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 228 | { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 229 | clk_put(data->clk_cdev1); |
| 230 | clk_put(data->clk_pll_a_out0); |
| 231 | clk_put(data->clk_pll_a); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 232 | } |
Stephen Warren | a3cd50d | 2011-02-22 17:23:56 -0700 | [diff] [blame] | 233 | EXPORT_SYMBOL_GPL(tegra_asoc_utils_fini); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 234 | |
Stephen Warren | a3cd50d | 2011-02-22 17:23:56 -0700 | [diff] [blame] | 235 | MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>"); |
| 236 | MODULE_DESCRIPTION("Tegra ASoC utility code"); |
| 237 | MODULE_LICENSE("GPL"); |