Thomas Gleixner | 2b27bdc | 2019-05-29 16:57:50 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 2 | /* |
| 3 | * tegra_asoc_utils.c - Harmony machine ASoC driver |
| 4 | * |
| 5 | * Author: Stephen Warren <swarren@nvidia.com> |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 6 | * Copyright (C) 2010,2012 - NVIDIA, Inc. |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/clk.h> |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 10 | #include <linux/device.h> |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 11 | #include <linux/err.h> |
| 12 | #include <linux/kernel.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 13 | #include <linux/module.h> |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 14 | #include <linux/of.h> |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 15 | |
| 16 | #include "tegra_asoc_utils.h" |
| 17 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 18 | 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] | 19 | int mclk) |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 20 | { |
| 21 | int new_baseclock; |
Stephen Warren | 0754139 | 2011-04-19 15:25:09 -0600 | [diff] [blame] | 22 | bool clk_change; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 23 | int err; |
| 24 | |
| 25 | switch (srate) { |
| 26 | case 11025: |
| 27 | case 22050: |
| 28 | case 44100: |
| 29 | case 88200: |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 30 | if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20) |
| 31 | new_baseclock = 56448000; |
Stephen Warren | a7fc5d2 | 2013-03-21 13:56:42 -0600 | [diff] [blame] | 32 | else if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA30) |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 33 | new_baseclock = 564480000; |
Stephen Warren | a7fc5d2 | 2013-03-21 13:56:42 -0600 | [diff] [blame] | 34 | else |
| 35 | new_baseclock = 282240000; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 36 | break; |
| 37 | case 8000: |
| 38 | case 16000: |
| 39 | case 32000: |
| 40 | case 48000: |
| 41 | case 64000: |
| 42 | case 96000: |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 43 | if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20) |
| 44 | new_baseclock = 73728000; |
Stephen Warren | a7fc5d2 | 2013-03-21 13:56:42 -0600 | [diff] [blame] | 45 | else if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA30) |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 46 | new_baseclock = 552960000; |
Stephen Warren | a7fc5d2 | 2013-03-21 13:56:42 -0600 | [diff] [blame] | 47 | else |
| 48 | new_baseclock = 368640000; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 49 | break; |
| 50 | default: |
| 51 | return -EINVAL; |
| 52 | } |
| 53 | |
Stephen Warren | 0754139 | 2011-04-19 15:25:09 -0600 | [diff] [blame] | 54 | clk_change = ((new_baseclock != data->set_baseclock) || |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 55 | (mclk != data->set_mclk)); |
Stephen Warren | 0754139 | 2011-04-19 15:25:09 -0600 | [diff] [blame] | 56 | if (!clk_change) |
| 57 | return 0; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 58 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 59 | data->set_baseclock = 0; |
| 60 | data->set_mclk = 0; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 61 | |
Prashant Gaikwad | 65d2bdd | 2012-06-05 09:59:42 +0530 | [diff] [blame] | 62 | clk_disable_unprepare(data->clk_cdev1); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 63 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 64 | err = clk_set_rate(data->clk_pll_a, new_baseclock); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 65 | if (err) { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 66 | 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] | 67 | return err; |
| 68 | } |
| 69 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 70 | err = clk_set_rate(data->clk_pll_a_out0, mclk); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 71 | if (err) { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 72 | 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] | 73 | return err; |
| 74 | } |
| 75 | |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 76 | /* 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] | 77 | |
Prashant Gaikwad | 65d2bdd | 2012-06-05 09:59:42 +0530 | [diff] [blame] | 78 | err = clk_prepare_enable(data->clk_cdev1); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 79 | if (err) { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 80 | dev_err(data->dev, "Can't enable cdev1: %d\n", err); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 81 | return err; |
| 82 | } |
| 83 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 84 | data->set_baseclock = new_baseclock; |
| 85 | data->set_mclk = mclk; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 86 | |
| 87 | return 0; |
| 88 | } |
Stephen Warren | a3cd50d | 2011-02-22 17:23:56 -0700 | [diff] [blame] | 89 | EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_rate); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 90 | |
Lucas Stach | 919ad49 | 2012-12-20 00:17:33 +0100 | [diff] [blame] | 91 | int 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 Stach | 919ad49 | 2012-12-20 00:17:33 +0100 | [diff] [blame] | 98 | |
| 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 Stach | 919ad49 | 2012-12-20 00:17:33 +0100 | [diff] [blame] | 117 | 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 | } |
| 128 | EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_ac97_rate); |
| 129 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 130 | int tegra_asoc_utils_init(struct tegra_asoc_utils_data *data, |
| 131 | struct device *dev) |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 132 | { |
Sowjanya Komatineni | 1e4e0bf | 2020-01-13 23:24:23 -0800 | [diff] [blame^] | 133 | struct clk *clk_out_1, *clk_extern1; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 134 | int ret; |
| 135 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 136 | data->dev = dev; |
| 137 | |
Stephen Warren | 8127bf5 | 2012-04-10 13:11:17 -0600 | [diff] [blame] | 138 | if (of_machine_is_compatible("nvidia,tegra20")) |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 139 | 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 Warren | 110147c | 2013-05-13 13:26:12 -0600 | [diff] [blame] | 142 | else if (of_machine_is_compatible("nvidia,tegra114")) |
Stephen Warren | a7fc5d2 | 2013-03-21 13:56:42 -0600 | [diff] [blame] | 143 | data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA114; |
Stephen Warren | 5e049fc | 2013-10-11 15:43:17 -0600 | [diff] [blame] | 144 | else if (of_machine_is_compatible("nvidia,tegra124")) |
| 145 | data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA124; |
Stephen Warren | 110147c | 2013-05-13 13:26:12 -0600 | [diff] [blame] | 146 | else { |
Stephen Warren | a7fc5d2 | 2013-03-21 13:56:42 -0600 | [diff] [blame] | 147 | dev_err(data->dev, "SoC unknown to Tegra ASoC utils\n"); |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 148 | return -EINVAL; |
Stephen Warren | a7fc5d2 | 2013-03-21 13:56:42 -0600 | [diff] [blame] | 149 | } |
Stephen Warren | c2f6702 | 2012-04-06 11:15:55 -0600 | [diff] [blame] | 150 | |
Sowjanya Komatineni | 0de6db3 | 2020-01-13 23:24:17 -0800 | [diff] [blame] | 151 | data->clk_pll_a = devm_clk_get(dev, "pll_a"); |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 152 | if (IS_ERR(data->clk_pll_a)) { |
| 153 | dev_err(data->dev, "Can't retrieve clk pll_a\n"); |
Sowjanya Komatineni | 0de6db3 | 2020-01-13 23:24:17 -0800 | [diff] [blame] | 154 | return PTR_ERR(data->clk_pll_a); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Sowjanya Komatineni | 0de6db3 | 2020-01-13 23:24:17 -0800 | [diff] [blame] | 157 | data->clk_pll_a_out0 = devm_clk_get(dev, "pll_a_out0"); |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 158 | if (IS_ERR(data->clk_pll_a_out0)) { |
| 159 | dev_err(data->dev, "Can't retrieve clk pll_a_out0\n"); |
Sowjanya Komatineni | 0de6db3 | 2020-01-13 23:24:17 -0800 | [diff] [blame] | 160 | return PTR_ERR(data->clk_pll_a_out0); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Sowjanya Komatineni | 0de6db3 | 2020-01-13 23:24:17 -0800 | [diff] [blame] | 163 | data->clk_cdev1 = devm_clk_get(dev, "mclk"); |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 164 | if (IS_ERR(data->clk_cdev1)) { |
| 165 | dev_err(data->dev, "Can't retrieve clk cdev1\n"); |
Sowjanya Komatineni | 0de6db3 | 2020-01-13 23:24:17 -0800 | [diff] [blame] | 166 | return PTR_ERR(data->clk_cdev1); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Sowjanya Komatineni | 1e4e0bf | 2020-01-13 23:24:23 -0800 | [diff] [blame^] | 169 | /* |
| 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 Warren | a9005b6 | 2012-04-06 11:18:16 -0600 | [diff] [blame] | 208 | ret = tegra_asoc_utils_set_rate(data, 44100, 256 * 44100); |
| 209 | if (ret) |
Sowjanya Komatineni | 0de6db3 | 2020-01-13 23:24:17 -0800 | [diff] [blame] | 210 | return ret; |
Stephen Warren | a9005b6 | 2012-04-06 11:18:16 -0600 | [diff] [blame] | 211 | |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 212 | return 0; |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 213 | } |
Stephen Warren | a3cd50d | 2011-02-22 17:23:56 -0700 | [diff] [blame] | 214 | EXPORT_SYMBOL_GPL(tegra_asoc_utils_init); |
Stephen Warren | a50a399 | 2011-01-07 22:36:15 -0700 | [diff] [blame] | 215 | |
Stephen Warren | a3cd50d | 2011-02-22 17:23:56 -0700 | [diff] [blame] | 216 | MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>"); |
| 217 | MODULE_DESCRIPTION("Tegra ASoC utility code"); |
| 218 | MODULE_LICENSE("GPL"); |