Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 1 | /* |
Stephen Warren | ef280d3 | 2012-04-05 15:54:53 -0600 | [diff] [blame] | 2 | * tegra20_das.c - Tegra20 DAS driver |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 3 | * |
| 4 | * Author: Stephen Warren <swarren@nvidia.com> |
| 5 | * Copyright (C) 2010 - NVIDIA, Inc. |
| 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 | |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 23 | #include <linux/device.h> |
Stephen Warren | 7613c50 | 2012-04-06 11:12:25 -0600 | [diff] [blame] | 24 | #include <linux/io.h> |
| 25 | #include <linux/module.h> |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
Stephen Warren | 7203a62 | 2012-04-13 12:14:08 -0600 | [diff] [blame] | 27 | #include <linux/regmap.h> |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 28 | #include <linux/slab.h> |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 29 | #include <sound/soc.h> |
Stephen Warren | ef280d3 | 2012-04-05 15:54:53 -0600 | [diff] [blame] | 30 | #include "tegra20_das.h" |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 31 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 32 | #define DRV_NAME "tegra20-das" |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 33 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 34 | static struct tegra20_das *das; |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 35 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 36 | static inline void tegra20_das_write(u32 reg, u32 val) |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 37 | { |
Stephen Warren | 7203a62 | 2012-04-13 12:14:08 -0600 | [diff] [blame] | 38 | regmap_write(das->regmap, reg, val); |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 41 | static inline u32 tegra20_das_read(u32 reg) |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 42 | { |
Stephen Warren | 7203a62 | 2012-04-13 12:14:08 -0600 | [diff] [blame] | 43 | u32 val; |
| 44 | regmap_read(das->regmap, reg, &val); |
| 45 | return val; |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 48 | int tegra20_das_connect_dap_to_dac(int dap, int dac) |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 49 | { |
| 50 | u32 addr; |
| 51 | u32 reg; |
| 52 | |
| 53 | if (!das) |
| 54 | return -ENODEV; |
| 55 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 56 | addr = TEGRA20_DAS_DAP_CTRL_SEL + |
| 57 | (dap * TEGRA20_DAS_DAP_CTRL_SEL_STRIDE); |
| 58 | reg = dac << TEGRA20_DAS_DAP_CTRL_SEL_DAP_CTRL_SEL_P; |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 59 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 60 | tegra20_das_write(addr, reg); |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 64 | EXPORT_SYMBOL_GPL(tegra20_das_connect_dap_to_dac); |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 65 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 66 | int tegra20_das_connect_dap_to_dap(int dap, int otherdap, int master, |
| 67 | int sdata1rx, int sdata2rx) |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 68 | { |
| 69 | u32 addr; |
| 70 | u32 reg; |
| 71 | |
| 72 | if (!das) |
| 73 | return -ENODEV; |
| 74 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 75 | addr = TEGRA20_DAS_DAP_CTRL_SEL + |
| 76 | (dap * TEGRA20_DAS_DAP_CTRL_SEL_STRIDE); |
| 77 | reg = otherdap << TEGRA20_DAS_DAP_CTRL_SEL_DAP_CTRL_SEL_P | |
| 78 | !!sdata2rx << TEGRA20_DAS_DAP_CTRL_SEL_DAP_SDATA2_TX_RX_P | |
| 79 | !!sdata1rx << TEGRA20_DAS_DAP_CTRL_SEL_DAP_SDATA1_TX_RX_P | |
| 80 | !!master << TEGRA20_DAS_DAP_CTRL_SEL_DAP_MS_SEL_P; |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 81 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 82 | tegra20_das_write(addr, reg); |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 86 | EXPORT_SYMBOL_GPL(tegra20_das_connect_dap_to_dap); |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 87 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 88 | int tegra20_das_connect_dac_to_dap(int dac, int dap) |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 89 | { |
| 90 | u32 addr; |
| 91 | u32 reg; |
| 92 | |
| 93 | if (!das) |
| 94 | return -ENODEV; |
| 95 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 96 | addr = TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL + |
| 97 | (dac * TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_STRIDE); |
| 98 | reg = dap << TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_DAC_CLK_SEL_P | |
| 99 | dap << TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_DAC_SDATA1_SEL_P | |
| 100 | dap << TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_DAC_SDATA2_SEL_P; |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 101 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 102 | tegra20_das_write(addr, reg); |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 106 | EXPORT_SYMBOL_GPL(tegra20_das_connect_dac_to_dap); |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 107 | |
Stephen Warren | 7203a62 | 2012-04-13 12:14:08 -0600 | [diff] [blame] | 108 | #define LAST_REG(name) \ |
| 109 | (TEGRA20_DAS_##name + \ |
| 110 | (TEGRA20_DAS_##name##_STRIDE * (TEGRA20_DAS_##name##_COUNT - 1))) |
| 111 | |
| 112 | static bool tegra20_das_wr_rd_reg(struct device *dev, unsigned int reg) |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 113 | { |
Stephen Warren | 7203a62 | 2012-04-13 12:14:08 -0600 | [diff] [blame] | 114 | if ((reg >= TEGRA20_DAS_DAP_CTRL_SEL) && |
| 115 | (reg <= LAST_REG(DAP_CTRL_SEL))) |
| 116 | return true; |
| 117 | if ((reg >= TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL) && |
| 118 | (reg <= LAST_REG(DAC_INPUT_DATA_CLK_SEL))) |
| 119 | return true; |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 120 | |
Stephen Warren | 7203a62 | 2012-04-13 12:14:08 -0600 | [diff] [blame] | 121 | return false; |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Stephen Warren | 7203a62 | 2012-04-13 12:14:08 -0600 | [diff] [blame] | 124 | static const struct regmap_config tegra20_das_regmap_config = { |
| 125 | .reg_bits = 32, |
| 126 | .reg_stride = 4, |
| 127 | .val_bits = 32, |
| 128 | .max_register = LAST_REG(DAC_INPUT_DATA_CLK_SEL), |
| 129 | .writeable_reg = tegra20_das_wr_rd_reg, |
| 130 | .readable_reg = tegra20_das_wr_rd_reg, |
| 131 | .cache_type = REGCACHE_RBTREE, |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 134 | static int __devinit tegra20_das_probe(struct platform_device *pdev) |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 135 | { |
| 136 | struct resource *res, *region; |
Stephen Warren | 7203a62 | 2012-04-13 12:14:08 -0600 | [diff] [blame] | 137 | void __iomem *regs; |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 138 | int ret = 0; |
| 139 | |
| 140 | if (das) |
| 141 | return -ENODEV; |
| 142 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 143 | das = devm_kzalloc(&pdev->dev, sizeof(struct tegra20_das), GFP_KERNEL); |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 144 | if (!das) { |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 145 | dev_err(&pdev->dev, "Can't allocate tegra20_das\n"); |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 146 | ret = -ENOMEM; |
Stephen Warren | f2296d7 | 2011-11-22 18:21:15 -0700 | [diff] [blame] | 147 | goto err; |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 148 | } |
| 149 | das->dev = &pdev->dev; |
| 150 | |
| 151 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 152 | if (!res) { |
| 153 | dev_err(&pdev->dev, "No memory resource\n"); |
| 154 | ret = -ENODEV; |
Stephen Warren | f2296d7 | 2011-11-22 18:21:15 -0700 | [diff] [blame] | 155 | goto err; |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Stephen Warren | f2296d7 | 2011-11-22 18:21:15 -0700 | [diff] [blame] | 158 | region = devm_request_mem_region(&pdev->dev, res->start, |
| 159 | resource_size(res), pdev->name); |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 160 | if (!region) { |
| 161 | dev_err(&pdev->dev, "Memory region already claimed\n"); |
| 162 | ret = -EBUSY; |
Stephen Warren | f2296d7 | 2011-11-22 18:21:15 -0700 | [diff] [blame] | 163 | goto err; |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Stephen Warren | 7203a62 | 2012-04-13 12:14:08 -0600 | [diff] [blame] | 166 | regs = devm_ioremap(&pdev->dev, res->start, resource_size(res)); |
| 167 | if (!regs) { |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 168 | dev_err(&pdev->dev, "ioremap failed\n"); |
| 169 | ret = -ENOMEM; |
Stephen Warren | f2296d7 | 2011-11-22 18:21:15 -0700 | [diff] [blame] | 170 | goto err; |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Stephen Warren | 7203a62 | 2012-04-13 12:14:08 -0600 | [diff] [blame] | 173 | das->regmap = devm_regmap_init_mmio(&pdev->dev, regs, |
| 174 | &tegra20_das_regmap_config); |
| 175 | if (IS_ERR(das->regmap)) { |
| 176 | dev_err(&pdev->dev, "regmap init failed\n"); |
| 177 | ret = PTR_ERR(das->regmap); |
| 178 | goto err; |
| 179 | } |
| 180 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 181 | ret = tegra20_das_connect_dap_to_dac(TEGRA20_DAS_DAP_ID_1, |
| 182 | TEGRA20_DAS_DAP_SEL_DAC1); |
Stephen Warren | 7b9b5e1 | 2011-12-07 13:58:29 -0700 | [diff] [blame] | 183 | if (ret) { |
| 184 | dev_err(&pdev->dev, "Can't set up DAS DAP connection\n"); |
| 185 | goto err; |
| 186 | } |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 187 | ret = tegra20_das_connect_dac_to_dap(TEGRA20_DAS_DAC_ID_1, |
| 188 | TEGRA20_DAS_DAC_SEL_DAP1); |
Stephen Warren | 7b9b5e1 | 2011-12-07 13:58:29 -0700 | [diff] [blame] | 189 | if (ret) { |
| 190 | dev_err(&pdev->dev, "Can't set up DAS DAC connection\n"); |
| 191 | goto err; |
| 192 | } |
| 193 | |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 194 | platform_set_drvdata(pdev, das); |
| 195 | |
| 196 | return 0; |
| 197 | |
Stephen Warren | f2296d7 | 2011-11-22 18:21:15 -0700 | [diff] [blame] | 198 | err: |
Olof Johansson | 01840bb | 2011-10-14 15:54:19 -0700 | [diff] [blame] | 199 | das = NULL; |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 200 | return ret; |
| 201 | } |
| 202 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 203 | static int __devexit tegra20_das_remove(struct platform_device *pdev) |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 204 | { |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 205 | if (!das) |
| 206 | return -ENODEV; |
| 207 | |
Olof Johansson | 01840bb | 2011-10-14 15:54:19 -0700 | [diff] [blame] | 208 | das = NULL; |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 213 | static const struct of_device_id tegra20_das_of_match[] __devinitconst = { |
Stephen Warren | 186bcda | 2011-11-22 18:21:18 -0700 | [diff] [blame] | 214 | { .compatible = "nvidia,tegra20-das", }, |
| 215 | {}, |
| 216 | }; |
| 217 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 218 | static struct platform_driver tegra20_das_driver = { |
| 219 | .probe = tegra20_das_probe, |
| 220 | .remove = __devexit_p(tegra20_das_remove), |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 221 | .driver = { |
| 222 | .name = DRV_NAME, |
Stephen Warren | 186bcda | 2011-11-22 18:21:18 -0700 | [diff] [blame] | 223 | .owner = THIS_MODULE, |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 224 | .of_match_table = tegra20_das_of_match, |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 225 | }, |
| 226 | }; |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 227 | module_platform_driver(tegra20_das_driver); |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 228 | |
| 229 | MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>"); |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 230 | MODULE_DESCRIPTION("Tegra20 DAS driver"); |
Stephen Warren | f0d8af4 | 2011-01-07 22:36:12 -0700 | [diff] [blame] | 231 | MODULE_LICENSE("GPL"); |
Stephen Warren | 8eb3420 | 2011-02-10 15:37:19 -0700 | [diff] [blame] | 232 | MODULE_ALIAS("platform:" DRV_NAME); |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame] | 233 | MODULE_DEVICE_TABLE(of, tegra20_das_of_match); |