blob: 812696d9c863761a4bef728cdc5a014cda7eb168 [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
Stephen Warrenf0d8af42011-01-07 22:36:12 -070023#include <linux/debugfs.h>
24#include <linux/device.h>
Stephen Warren7613c502012-04-06 11:12:25 -060025#include <linux/io.h>
26#include <linux/module.h>
Stephen Warrenf0d8af42011-01-07 22:36:12 -070027#include <linux/platform_device.h>
28#include <linux/seq_file.h>
29#include <linux/slab.h>
Stephen Warrenf0d8af42011-01-07 22:36:12 -070030#include <sound/soc.h>
Stephen Warrenef280d32012-04-05 15:54:53 -060031#include "tegra20_das.h"
Stephen Warrenf0d8af42011-01-07 22:36:12 -070032
Stephen Warren896637a2012-04-06 10:30:52 -060033#define DRV_NAME "tegra20-das"
Stephen Warrenf0d8af42011-01-07 22:36:12 -070034
Stephen Warren896637a2012-04-06 10:30:52 -060035static struct tegra20_das *das;
Stephen Warrenf0d8af42011-01-07 22:36:12 -070036
Stephen Warren896637a2012-04-06 10:30:52 -060037static inline void tegra20_das_write(u32 reg, u32 val)
Stephen Warrenf0d8af42011-01-07 22:36:12 -070038{
39 __raw_writel(val, das->regs + reg);
40}
41
Stephen Warren896637a2012-04-06 10:30:52 -060042static inline u32 tegra20_das_read(u32 reg)
Stephen Warrenf0d8af42011-01-07 22:36:12 -070043{
44 return __raw_readl(das->regs + reg);
45}
46
Stephen Warren896637a2012-04-06 10:30:52 -060047int tegra20_das_connect_dap_to_dac(int dap, int dac)
Stephen Warrenf0d8af42011-01-07 22:36:12 -070048{
49 u32 addr;
50 u32 reg;
51
52 if (!das)
53 return -ENODEV;
54
Stephen Warren896637a2012-04-06 10:30:52 -060055 addr = TEGRA20_DAS_DAP_CTRL_SEL +
56 (dap * TEGRA20_DAS_DAP_CTRL_SEL_STRIDE);
57 reg = dac << TEGRA20_DAS_DAP_CTRL_SEL_DAP_CTRL_SEL_P;
Stephen Warrenf0d8af42011-01-07 22:36:12 -070058
Stephen Warren896637a2012-04-06 10:30:52 -060059 tegra20_das_write(addr, reg);
Stephen Warrenf0d8af42011-01-07 22:36:12 -070060
61 return 0;
62}
Stephen Warren896637a2012-04-06 10:30:52 -060063EXPORT_SYMBOL_GPL(tegra20_das_connect_dap_to_dac);
Stephen Warrenf0d8af42011-01-07 22:36:12 -070064
Stephen Warren896637a2012-04-06 10:30:52 -060065int tegra20_das_connect_dap_to_dap(int dap, int otherdap, int master,
66 int sdata1rx, int sdata2rx)
Stephen Warrenf0d8af42011-01-07 22:36:12 -070067{
68 u32 addr;
69 u32 reg;
70
71 if (!das)
72 return -ENODEV;
73
Stephen Warren896637a2012-04-06 10:30:52 -060074 addr = TEGRA20_DAS_DAP_CTRL_SEL +
75 (dap * TEGRA20_DAS_DAP_CTRL_SEL_STRIDE);
76 reg = otherdap << TEGRA20_DAS_DAP_CTRL_SEL_DAP_CTRL_SEL_P |
77 !!sdata2rx << TEGRA20_DAS_DAP_CTRL_SEL_DAP_SDATA2_TX_RX_P |
78 !!sdata1rx << TEGRA20_DAS_DAP_CTRL_SEL_DAP_SDATA1_TX_RX_P |
79 !!master << TEGRA20_DAS_DAP_CTRL_SEL_DAP_MS_SEL_P;
Stephen Warrenf0d8af42011-01-07 22:36:12 -070080
Stephen Warren896637a2012-04-06 10:30:52 -060081 tegra20_das_write(addr, reg);
Stephen Warrenf0d8af42011-01-07 22:36:12 -070082
83 return 0;
84}
Stephen Warren896637a2012-04-06 10:30:52 -060085EXPORT_SYMBOL_GPL(tegra20_das_connect_dap_to_dap);
Stephen Warrenf0d8af42011-01-07 22:36:12 -070086
Stephen Warren896637a2012-04-06 10:30:52 -060087int tegra20_das_connect_dac_to_dap(int dac, int dap)
Stephen Warrenf0d8af42011-01-07 22:36:12 -070088{
89 u32 addr;
90 u32 reg;
91
92 if (!das)
93 return -ENODEV;
94
Stephen Warren896637a2012-04-06 10:30:52 -060095 addr = TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL +
96 (dac * TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_STRIDE);
97 reg = dap << TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_DAC_CLK_SEL_P |
98 dap << TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_DAC_SDATA1_SEL_P |
99 dap << TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_DAC_SDATA2_SEL_P;
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700100
Stephen Warren896637a2012-04-06 10:30:52 -0600101 tegra20_das_write(addr, reg);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700102
103 return 0;
104}
Stephen Warren896637a2012-04-06 10:30:52 -0600105EXPORT_SYMBOL_GPL(tegra20_das_connect_dac_to_dap);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700106
107#ifdef CONFIG_DEBUG_FS
Stephen Warren896637a2012-04-06 10:30:52 -0600108static int tegra20_das_show(struct seq_file *s, void *unused)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700109{
110 int i;
111 u32 addr;
112 u32 reg;
113
Stephen Warren896637a2012-04-06 10:30:52 -0600114 for (i = 0; i < TEGRA20_DAS_DAP_CTRL_SEL_COUNT; i++) {
115 addr = TEGRA20_DAS_DAP_CTRL_SEL +
116 (i * TEGRA20_DAS_DAP_CTRL_SEL_STRIDE);
117 reg = tegra20_das_read(addr);
118 seq_printf(s, "TEGRA20_DAS_DAP_CTRL_SEL[%d] = %08x\n", i, reg);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700119 }
120
Stephen Warren896637a2012-04-06 10:30:52 -0600121 for (i = 0; i < TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_COUNT; i++) {
122 addr = TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL +
123 (i * TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_STRIDE);
124 reg = tegra20_das_read(addr);
125 seq_printf(s, "TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL[%d] = %08x\n",
126 i, reg);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700127 }
128
129 return 0;
130}
131
Stephen Warren896637a2012-04-06 10:30:52 -0600132static int tegra20_das_debug_open(struct inode *inode, struct file *file)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700133{
Stephen Warren896637a2012-04-06 10:30:52 -0600134 return single_open(file, tegra20_das_show, inode->i_private);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700135}
136
Stephen Warren896637a2012-04-06 10:30:52 -0600137static const struct file_operations tegra20_das_debug_fops = {
138 .open = tegra20_das_debug_open,
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700139 .read = seq_read,
140 .llseek = seq_lseek,
141 .release = single_release,
142};
143
Stephen Warren896637a2012-04-06 10:30:52 -0600144static void tegra20_das_debug_add(struct tegra20_das *das)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700145{
Mark Brown8a9dab12011-01-10 22:25:21 +0000146 das->debug = debugfs_create_file(DRV_NAME, S_IRUGO,
147 snd_soc_debugfs_root, das,
Stephen Warren896637a2012-04-06 10:30:52 -0600148 &tegra20_das_debug_fops);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700149}
150
Stephen Warren896637a2012-04-06 10:30:52 -0600151static void tegra20_das_debug_remove(struct tegra20_das *das)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700152{
153 if (das->debug)
154 debugfs_remove(das->debug);
155}
156#else
Stephen Warren896637a2012-04-06 10:30:52 -0600157static inline void tegra20_das_debug_add(struct tegra20_das *das)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700158{
159}
160
Stephen Warren896637a2012-04-06 10:30:52 -0600161static inline void tegra20_das_debug_remove(struct tegra20_das *das)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700162{
163}
164#endif
165
Stephen Warren896637a2012-04-06 10:30:52 -0600166static int __devinit tegra20_das_probe(struct platform_device *pdev)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700167{
168 struct resource *res, *region;
169 int ret = 0;
170
171 if (das)
172 return -ENODEV;
173
Stephen Warren896637a2012-04-06 10:30:52 -0600174 das = devm_kzalloc(&pdev->dev, sizeof(struct tegra20_das), GFP_KERNEL);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700175 if (!das) {
Stephen Warren896637a2012-04-06 10:30:52 -0600176 dev_err(&pdev->dev, "Can't allocate tegra20_das\n");
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700177 ret = -ENOMEM;
Stephen Warrenf2296d72011-11-22 18:21:15 -0700178 goto err;
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700179 }
180 das->dev = &pdev->dev;
181
182 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
183 if (!res) {
184 dev_err(&pdev->dev, "No memory resource\n");
185 ret = -ENODEV;
Stephen Warrenf2296d72011-11-22 18:21:15 -0700186 goto err;
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700187 }
188
Stephen Warrenf2296d72011-11-22 18:21:15 -0700189 region = devm_request_mem_region(&pdev->dev, res->start,
190 resource_size(res), pdev->name);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700191 if (!region) {
192 dev_err(&pdev->dev, "Memory region already claimed\n");
193 ret = -EBUSY;
Stephen Warrenf2296d72011-11-22 18:21:15 -0700194 goto err;
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700195 }
196
Stephen Warrenf2296d72011-11-22 18:21:15 -0700197 das->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700198 if (!das->regs) {
199 dev_err(&pdev->dev, "ioremap failed\n");
200 ret = -ENOMEM;
Stephen Warrenf2296d72011-11-22 18:21:15 -0700201 goto err;
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700202 }
203
Stephen Warren896637a2012-04-06 10:30:52 -0600204 ret = tegra20_das_connect_dap_to_dac(TEGRA20_DAS_DAP_ID_1,
205 TEGRA20_DAS_DAP_SEL_DAC1);
Stephen Warren7b9b5e12011-12-07 13:58:29 -0700206 if (ret) {
207 dev_err(&pdev->dev, "Can't set up DAS DAP connection\n");
208 goto err;
209 }
Stephen Warren896637a2012-04-06 10:30:52 -0600210 ret = tegra20_das_connect_dac_to_dap(TEGRA20_DAS_DAC_ID_1,
211 TEGRA20_DAS_DAC_SEL_DAP1);
Stephen Warren7b9b5e12011-12-07 13:58:29 -0700212 if (ret) {
213 dev_err(&pdev->dev, "Can't set up DAS DAC connection\n");
214 goto err;
215 }
216
Stephen Warren896637a2012-04-06 10:30:52 -0600217 tegra20_das_debug_add(das);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700218
219 platform_set_drvdata(pdev, das);
220
221 return 0;
222
Stephen Warrenf2296d72011-11-22 18:21:15 -0700223err:
Olof Johansson01840bb2011-10-14 15:54:19 -0700224 das = NULL;
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700225 return ret;
226}
227
Stephen Warren896637a2012-04-06 10:30:52 -0600228static int __devexit tegra20_das_remove(struct platform_device *pdev)
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700229{
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700230 if (!das)
231 return -ENODEV;
232
Stephen Warren896637a2012-04-06 10:30:52 -0600233 tegra20_das_debug_remove(das);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700234
Olof Johansson01840bb2011-10-14 15:54:19 -0700235 das = NULL;
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700236
237 return 0;
238}
239
Stephen Warren896637a2012-04-06 10:30:52 -0600240static const struct of_device_id tegra20_das_of_match[] __devinitconst = {
Stephen Warren186bcda2011-11-22 18:21:18 -0700241 { .compatible = "nvidia,tegra20-das", },
242 {},
243};
244
Stephen Warren896637a2012-04-06 10:30:52 -0600245static struct platform_driver tegra20_das_driver = {
246 .probe = tegra20_das_probe,
247 .remove = __devexit_p(tegra20_das_remove),
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700248 .driver = {
249 .name = DRV_NAME,
Stephen Warren186bcda2011-11-22 18:21:18 -0700250 .owner = THIS_MODULE,
Stephen Warren896637a2012-04-06 10:30:52 -0600251 .of_match_table = tegra20_das_of_match,
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700252 },
253};
Stephen Warren896637a2012-04-06 10:30:52 -0600254module_platform_driver(tegra20_das_driver);
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700255
256MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
Stephen Warren896637a2012-04-06 10:30:52 -0600257MODULE_DESCRIPTION("Tegra20 DAS driver");
Stephen Warrenf0d8af42011-01-07 22:36:12 -0700258MODULE_LICENSE("GPL");
Stephen Warren8eb34202011-02-10 15:37:19 -0700259MODULE_ALIAS("platform:" DRV_NAME);
Stephen Warren896637a2012-04-06 10:30:52 -0600260MODULE_DEVICE_TABLE(of, tegra20_das_of_match);