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 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 72 | clk_disable(data->clk_cdev1); |
| 73 | clk_disable(data->clk_pll_a_out0); |
| 74 | clk_disable(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 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 90 | err = clk_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 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 96 | err = clk_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 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 102 | err = clk_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 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 115 | int tegra_asoc_utils_init(struct tegra_asoc_utils_data *data, |
| 116 | struct device *dev) |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 117 | { |
| 118 | int ret; |
| 119 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 120 | data->dev = dev; |
| 121 | |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame^] | 122 | if (!of_have_populated_dt()) |
| 123 | data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA20; |
| 124 | else if (of_machine_is_compatible("nvidia,tegra20")) |
| 125 | data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA20; |
| 126 | else if (of_machine_is_compatible("nvidia,tegra30")) |
| 127 | data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA30; |
| 128 | else |
| 129 | return -EINVAL; |
| 130 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 131 | data->clk_pll_a = clk_get_sys(NULL, "pll_a"); |
| 132 | if (IS_ERR(data->clk_pll_a)) { |
| 133 | dev_err(data->dev, "Can't retrieve clk pll_a\n"); |
| 134 | ret = PTR_ERR(data->clk_pll_a); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 135 | goto err; |
| 136 | } |
| 137 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 138 | data->clk_pll_a_out0 = clk_get_sys(NULL, "pll_a_out0"); |
| 139 | if (IS_ERR(data->clk_pll_a_out0)) { |
| 140 | dev_err(data->dev, "Can't retrieve clk pll_a_out0\n"); |
| 141 | ret = PTR_ERR(data->clk_pll_a_out0); |
Stephen Warren | 422650e | 2011-01-11 12:48:53 -0700 | [diff] [blame] | 142 | goto err_put_pll_a; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame^] | 145 | if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20) |
| 146 | data->clk_cdev1 = clk_get_sys(NULL, "cdev1"); |
| 147 | else |
| 148 | data->clk_cdev1 = clk_get_sys("extern1", NULL); |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 149 | if (IS_ERR(data->clk_cdev1)) { |
| 150 | dev_err(data->dev, "Can't retrieve clk cdev1\n"); |
| 151 | ret = PTR_ERR(data->clk_cdev1); |
Stephen Warren | 422650e | 2011-01-11 12:48:53 -0700 | [diff] [blame] | 152 | goto err_put_pll_a_out0; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Stephen Warren | a9005b6 | 2012-04-06 11:18:16 -0600 | [diff] [blame] | 155 | ret = tegra_asoc_utils_set_rate(data, 44100, 256 * 44100); |
| 156 | if (ret) |
| 157 | goto err_put_cdev1; |
| 158 | |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 159 | return 0; |
| 160 | |
Stephen Warren | a9005b6 | 2012-04-06 11:18:16 -0600 | [diff] [blame] | 161 | err_put_cdev1: |
| 162 | clk_put(data->clk_cdev1); |
Stephen Warren | 422650e | 2011-01-11 12:48:53 -0700 | [diff] [blame] | 163 | err_put_pll_a_out0: |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 164 | clk_put(data->clk_pll_a_out0); |
Stephen Warren | 422650e | 2011-01-11 12:48:53 -0700 | [diff] [blame] | 165 | err_put_pll_a: |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 166 | clk_put(data->clk_pll_a); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 167 | err: |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 168 | return ret; |
| 169 | } |
Stephen Warren | a3cd50d | 2011-02-22 17:23:56 -0700 | [diff] [blame] | 170 | EXPORT_SYMBOL_GPL(tegra_asoc_utils_init); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 171 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 172 | void tegra_asoc_utils_fini(struct tegra_asoc_utils_data *data) |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 173 | { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 174 | clk_put(data->clk_cdev1); |
| 175 | clk_put(data->clk_pll_a_out0); |
| 176 | clk_put(data->clk_pll_a); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 177 | } |
Stephen Warren | a3cd50d | 2011-02-22 17:23:56 -0700 | [diff] [blame] | 178 | EXPORT_SYMBOL_GPL(tegra_asoc_utils_fini); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 179 | |
Stephen Warren | a3cd50d | 2011-02-22 17:23:56 -0700 | [diff] [blame] | 180 | MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>"); |
| 181 | MODULE_DESCRIPTION("Tegra ASoC utility code"); |
| 182 | MODULE_LICENSE("GPL"); |