tegra20_das.c 5.5 KB
Newer Older
1
/*
2
 * tegra20_das.c - Tegra20 DAS driver
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Author: Stephen Warren <swarren@nvidia.com>
 * Copyright (C) 2010 - NVIDIA, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <linux/device.h>
24 25
#include <linux/io.h>
#include <linux/module.h>
26
#include <linux/platform_device.h>
27
#include <linux/regmap.h>
28 29
#include <linux/slab.h>
#include <sound/soc.h>
30
#include "tegra20_das.h"
31

32
#define DRV_NAME "tegra20-das"
33

34
static struct tegra20_das *das;
35

36
static inline void tegra20_das_write(u32 reg, u32 val)
37
{
38
	regmap_write(das->regmap, reg, val);
39 40
}

41
static inline u32 tegra20_das_read(u32 reg)
42
{
43 44 45
	u32 val;
	regmap_read(das->regmap, reg, &val);
	return val;
46 47
}

48
int tegra20_das_connect_dap_to_dac(int dap, int dac)
49 50 51 52 53 54 55
{
	u32 addr;
	u32 reg;

	if (!das)
		return -ENODEV;

56 57 58
	addr = TEGRA20_DAS_DAP_CTRL_SEL +
		(dap * TEGRA20_DAS_DAP_CTRL_SEL_STRIDE);
	reg = dac << TEGRA20_DAS_DAP_CTRL_SEL_DAP_CTRL_SEL_P;
59

60
	tegra20_das_write(addr, reg);
61 62 63

	return 0;
}
64
EXPORT_SYMBOL_GPL(tegra20_das_connect_dap_to_dac);
65

66 67
int tegra20_das_connect_dap_to_dap(int dap, int otherdap, int master,
				   int sdata1rx, int sdata2rx)
68 69 70 71 72 73 74
{
	u32 addr;
	u32 reg;

	if (!das)
		return -ENODEV;

75 76 77 78 79 80
	addr = TEGRA20_DAS_DAP_CTRL_SEL +
		(dap * TEGRA20_DAS_DAP_CTRL_SEL_STRIDE);
	reg = otherdap << TEGRA20_DAS_DAP_CTRL_SEL_DAP_CTRL_SEL_P |
		!!sdata2rx << TEGRA20_DAS_DAP_CTRL_SEL_DAP_SDATA2_TX_RX_P |
		!!sdata1rx << TEGRA20_DAS_DAP_CTRL_SEL_DAP_SDATA1_TX_RX_P |
		!!master << TEGRA20_DAS_DAP_CTRL_SEL_DAP_MS_SEL_P;
81

82
	tegra20_das_write(addr, reg);
83 84 85

	return 0;
}
86
EXPORT_SYMBOL_GPL(tegra20_das_connect_dap_to_dap);
87

88
int tegra20_das_connect_dac_to_dap(int dac, int dap)
89 90 91 92 93 94 95
{
	u32 addr;
	u32 reg;

	if (!das)
		return -ENODEV;

96 97 98 99 100
	addr = TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL +
		(dac * TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_STRIDE);
	reg = dap << TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_DAC_CLK_SEL_P |
		dap << TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_DAC_SDATA1_SEL_P |
		dap << TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL_DAC_SDATA2_SEL_P;
101

102
	tegra20_das_write(addr, reg);
103 104 105

	return 0;
}
106
EXPORT_SYMBOL_GPL(tegra20_das_connect_dac_to_dap);
107

108 109 110
#define LAST_REG(name) \
	(TEGRA20_DAS_##name + \
	 (TEGRA20_DAS_##name##_STRIDE * (TEGRA20_DAS_##name##_COUNT - 1)))
111

112
static bool tegra20_das_wr_rd_reg(struct device *dev, unsigned int reg)
113
{
114 115 116 117 118 119 120 121
	if ((reg >= TEGRA20_DAS_DAP_CTRL_SEL) &&
	    (reg <= LAST_REG(DAP_CTRL_SEL)))
		return true;
	if ((reg >= TEGRA20_DAS_DAC_INPUT_DATA_CLK_SEL) &&
	    (reg <= LAST_REG(DAC_INPUT_DATA_CLK_SEL)))
		return true;

	return false;
122 123
}

124 125 126 127 128 129 130 131
static const struct regmap_config tegra20_das_regmap_config = {
	.reg_bits = 32,
	.reg_stride = 4,
	.val_bits = 32,
	.max_register = LAST_REG(DAC_INPUT_DATA_CLK_SEL),
	.writeable_reg = tegra20_das_wr_rd_reg,
	.readable_reg = tegra20_das_wr_rd_reg,
	.cache_type = REGCACHE_RBTREE,
132 133
};

134
static int __devinit tegra20_das_probe(struct platform_device *pdev)
135 136
{
	struct resource *res, *region;
137
	void __iomem *regs;
138 139 140 141 142
	int ret = 0;

	if (das)
		return -ENODEV;

143
	das = devm_kzalloc(&pdev->dev, sizeof(struct tegra20_das), GFP_KERNEL);
144
	if (!das) {
145
		dev_err(&pdev->dev, "Can't allocate tegra20_das\n");
146
		ret = -ENOMEM;
147
		goto err;
148 149 150 151 152 153 154
	}
	das->dev = &pdev->dev;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(&pdev->dev, "No memory resource\n");
		ret = -ENODEV;
155
		goto err;
156 157
	}

158 159
	region = devm_request_mem_region(&pdev->dev, res->start,
					 resource_size(res), pdev->name);
160 161 162
	if (!region) {
		dev_err(&pdev->dev, "Memory region already claimed\n");
		ret = -EBUSY;
163
		goto err;
164 165
	}

166 167
	regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
	if (!regs) {
168 169
		dev_err(&pdev->dev, "ioremap failed\n");
		ret = -ENOMEM;
170
		goto err;
171 172
	}

173 174 175 176 177 178 179 180
	das->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
					    &tegra20_das_regmap_config);
	if (IS_ERR(das->regmap)) {
		dev_err(&pdev->dev, "regmap init failed\n");
		ret = PTR_ERR(das->regmap);
		goto err;
	}

181 182
	ret = tegra20_das_connect_dap_to_dac(TEGRA20_DAS_DAP_ID_1,
					     TEGRA20_DAS_DAP_SEL_DAC1);
183 184 185 186
	if (ret) {
		dev_err(&pdev->dev, "Can't set up DAS DAP connection\n");
		goto err;
	}
187 188
	ret = tegra20_das_connect_dac_to_dap(TEGRA20_DAS_DAC_ID_1,
					     TEGRA20_DAS_DAC_SEL_DAP1);
189 190 191 192 193
	if (ret) {
		dev_err(&pdev->dev, "Can't set up DAS DAC connection\n");
		goto err;
	}

194 195 196 197
	platform_set_drvdata(pdev, das);

	return 0;

198
err:
O
Olof Johansson 已提交
199
	das = NULL;
200 201 202
	return ret;
}

203
static int __devexit tegra20_das_remove(struct platform_device *pdev)
204 205 206 207
{
	if (!das)
		return -ENODEV;

O
Olof Johansson 已提交
208
	das = NULL;
209 210 211 212

	return 0;
}

213
static const struct of_device_id tegra20_das_of_match[] __devinitconst = {
214 215 216 217
	{ .compatible = "nvidia,tegra20-das", },
	{},
};

218 219 220
static struct platform_driver tegra20_das_driver = {
	.probe = tegra20_das_probe,
	.remove = __devexit_p(tegra20_das_remove),
221 222
	.driver = {
		.name = DRV_NAME,
223
		.owner = THIS_MODULE,
224
		.of_match_table = tegra20_das_of_match,
225 226
	},
};
227
module_platform_driver(tegra20_das_driver);
228 229

MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
230
MODULE_DESCRIPTION("Tegra20 DAS driver");
231
MODULE_LICENSE("GPL");
S
Stephen Warren 已提交
232
MODULE_ALIAS("platform:" DRV_NAME);
233
MODULE_DEVICE_TABLE(of, tegra20_das_of_match);