blob: 486d7b245cf4dc4f8f377f18580d87cbba638762 [file] [log] [blame]
Stephen Warrenf0d8af42011-01-07 22:36:12 -07001/*
Stephen Warrenef280d32012-04-05 15:54:53 -06002 * tegra20_das.c - Tegra20 DAS driver
Stephen Warrenf0d8af42011-01-07 22:36:12 -07003 *
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
23#include <linux/module.h>
24#include <linux/debugfs.h>
25#include <linux/device.h>
26#include <linux/platform_device.h>
27#include <linux/seq_file.h>
28#include <linux/slab.h>
29#include <linux/io.h>
30#include <mach/iomap.h>
31#include <sound/soc.h>
Stephen Warrenef280d32012-04-05 15:54:53 -060032#include "tegra20_das.h"
Stephen Warrenf0d8af42011-01-07 22:36:12 -070033
Stephen Warren896637a2012-04-06 10:30:52 -060034#define DRV_NAME "tegra20-das"
Stephen Warrenf0d8af42011-01-07 22:36:12 -070035
Stephen Warren896637a2012-04-06 10:30:52 -060036static struct tegra20_das *das;
Stephen Warrenf0d8af42011-01-07 22:36:12 -070037
Stephen Warren896637a2012-04-06 10:30:52 -060038static inline void tegra20_das_write(u32 reg, u32 val)
Stephen Warrenf0d8af42011-01-07 22:36:12 -070039{
40 __raw_writel(val, das->regs + reg);
41}
42
Stephen Warren896637a2012-04-06 10:30:52 -060043static inline u32 tegra20_das_read(u32 reg)
Stephen Warrenf0d8af42011-01-07 22:36:12 -070044{
45 return __raw_readl(das->regs + reg);
46}
47
Stephen Warren896637a2012-04-06 10:30:52 -060048int tegra20_das_connect_dap_to_dac(int dap, int dac)
Stephen Warrenf0d8af42011-01-07 22:36:12 -070049{
50 u32 addr;
51 u32 reg;
52
53 if (!das)
54 return -ENODEV;
55
Stephen Warren896637a2012-04-06 10:30:52 -060056 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 Warrenf0d8af42011-01-07 22:36:12 -070059
Stephen Warren896637a2012-04-06 10:30:52 -060060 tegra20_das_write(addr, reg);
Stephen Warrenf0d8af42011-01-07 22:36:12 -070061
62 return 0;
63}
Stephen Warren896637a2012-04-06 10:30:52 -060064EXPORT_SYMBOL_GPL(tegra20_das_connect_dap_to_dac);
Stephen Warrenf0d8af42011-01-07 22:36:12 -070065
Stephen Warren896637a2012-04-06 10:30:52 -060066int tegra20_das_connect_dap_to_dap(int dap, int otherdap, int master,
67 int sdata1rx, int sdata2rx)
Stephen Warrenf0d8af42011-01-07 22:36:12 -070068{
69 u32 addr;
70 u32 reg;
71
72 if (!das)
73 return -ENODEV;
74
Stephen Warren896637a2012-04-06 10:30:52 -060075 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 Warrenf0d8af42011-01-07 22:36:12 -070081
Stephen Warren896637a2012-04-06 10:30:52 -060082 tegra20_das_write(addr, reg);
Stephen Warrenf0d8af42011-01-07 22:36:12 -070083
84 return 0;
85}
Stephen Warren896637a2012-04-06 10:30:52 -060086EXPORT_SYMBOL_GPL(tegra20_das_connect_dap_to_dap);
Stephen Warrenf0d8af42011-01-07 22:36:12 -070087
Stephen Warren896637a2012-04-06 10:30:52 -060088int tegra20_das_connect_dac_to_dap(int dac, int dap)
Stephen Warrenf0d8af42011-01-07 22:36:12 -070089{
90 u32 addr;
91 u32 reg;
92
93 if (!das)
94 return -ENODEV;
95
Stephen Warren896637a2012-04-06 10:30:52 -060096 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 Warrenf0d8af42011-01-07 22:36:12 -0700101
Stephen Warren896637a2012-04-06 10:30:52 -0600102 tegra20_das_write(addr, reg);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700103
104 return 0;
105}
Stephen Warren896637a2012-04-06 10:30:52 -0600106EXPORT_SYMBOL_GPL(tegra20_das_connect_dac_to_dap);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700107
108#ifdef CONFIG_DEBUG_FS
Stephen Warren896637a2012-04-06 10:30:52 -0600109static int tegra20_das_show(struct seq_file *s, void *unused)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700110{
111 int i;
112 u32 addr;
113 u32 reg;
114
Stephen Warren896637a2012-04-06 10:30:52 -0600115 for (i = 0; i < TEGRA20_DAS_DAP_CTRL_SEL_COUNT; i++) {
116 addr = TEGRA20_DAS_DAP_CTRL_SEL +
117 (i * TEGRA20_DAS_DAP_CTRL_SEL_STRIDE);
118 reg = tegra20_das_read(addr);
119 seq_printf(s, "TEGRA20_DAS_DAP_CTRL_SEL[%d] = %08x\n", i, reg);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700120 }
121
Stephen Warren896637a2012-04-06 10:30:52 -0600122 for (i = 0; i < TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_COUNT; i++) {
123 addr = TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL +
124 (i * TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_STRIDE);
125 reg = tegra20_das_read(addr);
126 seq_printf(s, "TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL[%d] = %08x\n",
127 i, reg);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700128 }
129
130 return 0;
131}
132
Stephen Warren896637a2012-04-06 10:30:52 -0600133static int tegra20_das_debug_open(struct inode *inode, struct file *file)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700134{
Stephen Warren896637a2012-04-06 10:30:52 -0600135 return single_open(file, tegra20_das_show, inode->i_private);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700136}
137
Stephen Warren896637a2012-04-06 10:30:52 -0600138static const struct file_operations tegra20_das_debug_fops = {
139 .open = tegra20_das_debug_open,
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700140 .read = seq_read,
141 .llseek = seq_lseek,
142 .release = single_release,
143};
144
Stephen Warren896637a2012-04-06 10:30:52 -0600145static void tegra20_das_debug_add(struct tegra20_das *das)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700146{
Mark Brown8a9dab12011-01-10 22:25:21 +0000147 das->debug = debugfs_create_file(DRV_NAME, S_IRUGO,
148 snd_soc_debugfs_root, das,
Stephen Warren896637a2012-04-06 10:30:52 -0600149 &tegra20_das_debug_fops);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700150}
151
Stephen Warren896637a2012-04-06 10:30:52 -0600152static void tegra20_das_debug_remove(struct tegra20_das *das)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700153{
154 if (das->debug)
155 debugfs_remove(das->debug);
156}
157#else
Stephen Warren896637a2012-04-06 10:30:52 -0600158static inline void tegra20_das_debug_add(struct tegra20_das *das)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700159{
160}
161
Stephen Warren896637a2012-04-06 10:30:52 -0600162static inline void tegra20_das_debug_remove(struct tegra20_das *das)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700163{
164}
165#endif
166
Stephen Warren896637a2012-04-06 10:30:52 -0600167static int __devinit tegra20_das_probe(struct platform_device *pdev)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700168{
169 struct resource *res, *region;
170 int ret = 0;
171
172 if (das)
173 return -ENODEV;
174
Stephen Warren896637a2012-04-06 10:30:52 -0600175 das = devm_kzalloc(&pdev->dev, sizeof(struct tegra20_das), GFP_KERNEL);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700176 if (!das) {
Stephen Warren896637a2012-04-06 10:30:52 -0600177 dev_err(&pdev->dev, "Can't allocate tegra20_das\n");
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700178 ret = -ENOMEM;
Stephen Warrenf2296d72011-11-22 18:21:15 -0700179 goto err;
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700180 }
181 das->dev = &pdev->dev;
182
183 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
184 if (!res) {
185 dev_err(&pdev->dev, "No memory resource\n");
186 ret = -ENODEV;
Stephen Warrenf2296d72011-11-22 18:21:15 -0700187 goto err;
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700188 }
189
Stephen Warrenf2296d72011-11-22 18:21:15 -0700190 region = devm_request_mem_region(&pdev->dev, res->start,
191 resource_size(res), pdev->name);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700192 if (!region) {
193 dev_err(&pdev->dev, "Memory region already claimed\n");
194 ret = -EBUSY;
Stephen Warrenf2296d72011-11-22 18:21:15 -0700195 goto err;
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700196 }
197
Stephen Warrenf2296d72011-11-22 18:21:15 -0700198 das->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700199 if (!das->regs) {
200 dev_err(&pdev->dev, "ioremap failed\n");
201 ret = -ENOMEM;
Stephen Warrenf2296d72011-11-22 18:21:15 -0700202 goto err;
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700203 }
204
Stephen Warren896637a2012-04-06 10:30:52 -0600205 ret = tegra20_das_connect_dap_to_dac(TEGRA20_DAS_DAP_ID_1,
206 TEGRA20_DAS_DAP_SEL_DAC1);
Stephen Warren7b9b5e12011-12-07 13:58:29 -0700207 if (ret) {
208 dev_err(&pdev->dev, "Can't set up DAS DAP connection\n");
209 goto err;
210 }
Stephen Warren896637a2012-04-06 10:30:52 -0600211 ret = tegra20_das_connect_dac_to_dap(TEGRA20_DAS_DAC_ID_1,
212 TEGRA20_DAS_DAC_SEL_DAP1);
Stephen Warren7b9b5e12011-12-07 13:58:29 -0700213 if (ret) {
214 dev_err(&pdev->dev, "Can't set up DAS DAC connection\n");
215 goto err;
216 }
217
Stephen Warren896637a2012-04-06 10:30:52 -0600218 tegra20_das_debug_add(das);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700219
220 platform_set_drvdata(pdev, das);
221
222 return 0;
223
Stephen Warrenf2296d72011-11-22 18:21:15 -0700224err:
Olof Johansson01840bb2011-10-14 15:54:19 -0700225 das = NULL;
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700226 return ret;
227}
228
Stephen Warren896637a2012-04-06 10:30:52 -0600229static int __devexit tegra20_das_remove(struct platform_device *pdev)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700230{
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700231 if (!das)
232 return -ENODEV;
233
Stephen Warren896637a2012-04-06 10:30:52 -0600234 tegra20_das_debug_remove(das);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700235
Olof Johansson01840bb2011-10-14 15:54:19 -0700236 das = NULL;
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700237
238 return 0;
239}
240
Stephen Warren896637a2012-04-06 10:30:52 -0600241static const struct of_device_id tegra20_das_of_match[] __devinitconst = {
Stephen Warren186bcda2011-11-22 18:21:18 -0700242 { .compatible = "nvidia,tegra20-das", },
243 {},
244};
245
Stephen Warren896637a2012-04-06 10:30:52 -0600246static struct platform_driver tegra20_das_driver = {
247 .probe = tegra20_das_probe,
248 .remove = __devexit_p(tegra20_das_remove),
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700249 .driver = {
250 .name = DRV_NAME,
Stephen Warren186bcda2011-11-22 18:21:18 -0700251 .owner = THIS_MODULE,
Stephen Warren896637a2012-04-06 10:30:52 -0600252 .of_match_table = tegra20_das_of_match,
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700253 },
254};
Stephen Warren896637a2012-04-06 10:30:52 -0600255module_platform_driver(tegra20_das_driver);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700256
257MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
Stephen Warren896637a2012-04-06 10:30:52 -0600258MODULE_DESCRIPTION("Tegra20 DAS driver");
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700259MODULE_LICENSE("GPL");
Stephen Warren8eb34202011-02-10 15:37:19 -0700260MODULE_ALIAS("platform:" DRV_NAME);
Stephen Warren896637a2012-04-06 10:30:52 -0600261MODULE_DEVICE_TABLE(of, tegra20_das_of_match);