sb_edac: allow different dram_rule arrays
[deliverable/linux.git] / drivers / mfd / ti_am335x_tscadc.c
CommitLineData
01636eb9
PR
1/*
2 * TI Touch Screen / ADC MFD driver
3 *
4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
9 *
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/err.h>
20#include <linux/io.h>
21#include <linux/clk.h>
22#include <linux/regmap.h>
23#include <linux/mfd/core.h>
24#include <linux/pm_runtime.h>
a6543a1c
PR
25#include <linux/of.h>
26#include <linux/of_device.h>
01636eb9
PR
27
28#include <linux/mfd/ti_am335x_tscadc.h>
29
30static unsigned int tscadc_readl(struct ti_tscadc_dev *tsadc, unsigned int reg)
31{
32 unsigned int val;
33
34 regmap_read(tsadc->regmap_tscadc, reg, &val);
35 return val;
36}
37
38static void tscadc_writel(struct ti_tscadc_dev *tsadc, unsigned int reg,
39 unsigned int val)
40{
41 regmap_write(tsadc->regmap_tscadc, reg, val);
42}
43
44static const struct regmap_config tscadc_regmap_config = {
45 .name = "ti_tscadc",
46 .reg_bits = 32,
47 .reg_stride = 4,
48 .val_bits = 32,
49};
50
abeccee4
PR
51void am335x_tsc_se_update(struct ti_tscadc_dev *tsadc)
52{
53 tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);
54}
55EXPORT_SYMBOL_GPL(am335x_tsc_se_update);
56
57void am335x_tsc_se_set(struct ti_tscadc_dev *tsadc, u32 val)
58{
59 spin_lock(&tsadc->reg_lock);
45491ada 60 tsadc->reg_se_cache = tscadc_readl(tsadc, REG_SE);
abeccee4 61 tsadc->reg_se_cache |= val;
abeccee4 62 am335x_tsc_se_update(tsadc);
45491ada 63 spin_unlock(&tsadc->reg_lock);
abeccee4
PR
64}
65EXPORT_SYMBOL_GPL(am335x_tsc_se_set);
66
67void am335x_tsc_se_clr(struct ti_tscadc_dev *tsadc, u32 val)
68{
69 spin_lock(&tsadc->reg_lock);
5d945d94 70 tsadc->reg_se_cache = tscadc_readl(tsadc, REG_SE);
abeccee4 71 tsadc->reg_se_cache &= ~val;
abeccee4 72 am335x_tsc_se_update(tsadc);
5d945d94 73 spin_unlock(&tsadc->reg_lock);
abeccee4
PR
74}
75EXPORT_SYMBOL_GPL(am335x_tsc_se_clr);
76
01636eb9
PR
77static void tscadc_idle_config(struct ti_tscadc_dev *config)
78{
79 unsigned int idleconfig;
80
81 idleconfig = STEPCONFIG_YNN | STEPCONFIG_INM_ADCREFM |
82 STEPCONFIG_INP_ADCREFM | STEPCONFIG_YPN;
83
84 tscadc_writel(config, REG_IDLECONFIG, idleconfig);
85}
86
612b95cd 87static int ti_tscadc_probe(struct platform_device *pdev)
01636eb9
PR
88{
89 struct ti_tscadc_dev *tscadc;
90 struct resource *res;
91 struct clk *clk;
a6543a1c 92 struct device_node *node = pdev->dev.of_node;
2b99bafa 93 struct mfd_cell *cell;
18926ede
SAS
94 struct property *prop;
95 const __be32 *cur;
96 u32 val;
01636eb9
PR
97 int err, ctrl;
98 int clk_value, clock_rate;
a6543a1c 99 int tsc_wires = 0, adc_channels = 0, total_channels;
18926ede 100 int readouts = 0;
01636eb9 101
9e5775f3
SAS
102 if (!pdev->dev.of_node) {
103 dev_err(&pdev->dev, "Could not find valid DT data.\n");
01636eb9
PR
104 return -EINVAL;
105 }
106
9e5775f3
SAS
107 node = of_get_child_by_name(pdev->dev.of_node, "tsc");
108 of_property_read_u32(node, "ti,wires", &tsc_wires);
18926ede 109 of_property_read_u32(node, "ti,coordiante-readouts", &readouts);
a6543a1c 110
9e5775f3 111 node = of_get_child_by_name(pdev->dev.of_node, "adc");
18926ede
SAS
112 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
113 adc_channels++;
114 if (val > 7) {
115 dev_err(&pdev->dev, " PIN numbers are 0..7 (not %d)\n",
116 val);
117 return -EINVAL;
118 }
119 }
5e53a69b 120 total_channels = tsc_wires + adc_channels;
5e53a69b
PR
121 if (total_channels > 8) {
122 dev_err(&pdev->dev, "Number of i/p channels more than 8\n");
123 return -EINVAL;
124 }
24d5c82f
PA
125 if (total_channels == 0) {
126 dev_err(&pdev->dev, "Need atleast one channel.\n");
127 return -EINVAL;
128 }
2b99bafa 129
18926ede
SAS
130 if (readouts * 2 + 2 + adc_channels > 16) {
131 dev_err(&pdev->dev, "Too many step configurations requested\n");
132 return -EINVAL;
133 }
134
01636eb9
PR
135 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
136 if (!res) {
137 dev_err(&pdev->dev, "no memory resource defined.\n");
138 return -EINVAL;
139 }
140
01636eb9
PR
141 /* Allocate memory for device */
142 tscadc = devm_kzalloc(&pdev->dev,
143 sizeof(struct ti_tscadc_dev), GFP_KERNEL);
144 if (!tscadc) {
145 dev_err(&pdev->dev, "failed to allocate memory.\n");
146 return -ENOMEM;
147 }
148 tscadc->dev = &pdev->dev;
3c39c9c6
PR
149
150 err = platform_get_irq(pdev, 0);
151 if (err < 0) {
152 dev_err(&pdev->dev, "no irq ID is specified.\n");
153 goto ret;
154 } else
155 tscadc->irq = err;
01636eb9
PR
156
157 res = devm_request_mem_region(&pdev->dev,
158 res->start, resource_size(res), pdev->name);
159 if (!res) {
160 dev_err(&pdev->dev, "failed to reserve registers.\n");
3c39c9c6 161 return -EBUSY;
01636eb9
PR
162 }
163
164 tscadc->tscadc_base = devm_ioremap(&pdev->dev,
165 res->start, resource_size(res));
166 if (!tscadc->tscadc_base) {
167 dev_err(&pdev->dev, "failed to map registers.\n");
3c39c9c6 168 return -ENOMEM;
01636eb9
PR
169 }
170
171 tscadc->regmap_tscadc = devm_regmap_init_mmio(&pdev->dev,
172 tscadc->tscadc_base, &tscadc_regmap_config);
173 if (IS_ERR(tscadc->regmap_tscadc)) {
174 dev_err(&pdev->dev, "regmap init failed\n");
175 err = PTR_ERR(tscadc->regmap_tscadc);
3c39c9c6 176 goto ret;
01636eb9
PR
177 }
178
abeccee4 179 spin_lock_init(&tscadc->reg_lock);
01636eb9
PR
180 pm_runtime_enable(&pdev->dev);
181 pm_runtime_get_sync(&pdev->dev);
182
183 /*
184 * The TSC_ADC_Subsystem has 2 clock domains
185 * OCP_CLK and ADC_CLK.
186 * The ADC clock is expected to run at target of 3MHz,
187 * and expected to capture 12-bit data at a rate of 200 KSPS.
188 * The TSC_ADC_SS controller design assumes the OCP clock is
189 * at least 6x faster than the ADC clock.
190 */
191 clk = clk_get(&pdev->dev, "adc_tsc_fck");
192 if (IS_ERR(clk)) {
193 dev_err(&pdev->dev, "failed to get TSC fck\n");
194 err = PTR_ERR(clk);
195 goto err_disable_clk;
196 }
197 clock_rate = clk_get_rate(clk);
198 clk_put(clk);
199 clk_value = clock_rate / ADC_CLK;
efe3126a 200
01636eb9
PR
201 /* TSCADC_CLKDIV needs to be configured to the value minus 1 */
202 clk_value = clk_value - 1;
203 tscadc_writel(tscadc, REG_CLKDIV, clk_value);
204
205 /* Set the control register bits */
206 ctrl = CNTRLREG_STEPCONFIGWRT |
b5f8b763
PR
207 CNTRLREG_STEPID;
208 if (tsc_wires > 0)
209 ctrl |= CNTRLREG_4WIRE | CNTRLREG_TSCENB;
01636eb9
PR
210 tscadc_writel(tscadc, REG_CTRL, ctrl);
211
212 /* Set register bits for Idle Config Mode */
b5f8b763
PR
213 if (tsc_wires > 0)
214 tscadc_idle_config(tscadc);
01636eb9
PR
215
216 /* Enable the TSC module enable bit */
217 ctrl = tscadc_readl(tscadc, REG_CTRL);
218 ctrl |= CNTRLREG_TSCSSENB;
219 tscadc_writel(tscadc, REG_CTRL, ctrl);
220
24d5c82f
PA
221 tscadc->used_cells = 0;
222 tscadc->tsc_cell = -1;
223 tscadc->adc_cell = -1;
224
2b99bafa 225 /* TSC Cell */
24d5c82f
PA
226 if (tsc_wires > 0) {
227 tscadc->tsc_cell = tscadc->used_cells;
228 cell = &tscadc->cells[tscadc->used_cells++];
5f184e63 229 cell->name = "TI-am335x-tsc";
24d5c82f
PA
230 cell->of_compatible = "ti,am3359-tsc";
231 cell->platform_data = &tscadc;
232 cell->pdata_size = sizeof(tscadc);
233 }
2b99bafa 234
5e53a69b 235 /* ADC Cell */
24d5c82f
PA
236 if (adc_channels > 0) {
237 tscadc->adc_cell = tscadc->used_cells;
238 cell = &tscadc->cells[tscadc->used_cells++];
9f99928f 239 cell->name = "TI-am335x-adc";
24d5c82f
PA
240 cell->of_compatible = "ti,am3359-adc";
241 cell->platform_data = &tscadc;
242 cell->pdata_size = sizeof(tscadc);
243 }
5e53a69b 244
01636eb9 245 err = mfd_add_devices(&pdev->dev, pdev->id, tscadc->cells,
24d5c82f 246 tscadc->used_cells, NULL, 0, NULL);
01636eb9
PR
247 if (err < 0)
248 goto err_disable_clk;
249
250 device_init_wakeup(&pdev->dev, true);
251 platform_set_drvdata(pdev, tscadc);
01636eb9
PR
252 return 0;
253
254err_disable_clk:
255 pm_runtime_put_sync(&pdev->dev);
256 pm_runtime_disable(&pdev->dev);
3c39c9c6 257ret:
01636eb9
PR
258 return err;
259}
260
612b95cd 261static int ti_tscadc_remove(struct platform_device *pdev)
01636eb9
PR
262{
263 struct ti_tscadc_dev *tscadc = platform_get_drvdata(pdev);
264
265 tscadc_writel(tscadc, REG_SE, 0x00);
266
267 pm_runtime_put_sync(&pdev->dev);
268 pm_runtime_disable(&pdev->dev);
269
270 mfd_remove_devices(tscadc->dev);
271
272 return 0;
273}
274
275#ifdef CONFIG_PM
276static int tscadc_suspend(struct device *dev)
277{
278 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
279
280 tscadc_writel(tscadc_dev, REG_SE, 0x00);
281 pm_runtime_put_sync(dev);
282
283 return 0;
284}
285
286static int tscadc_resume(struct device *dev)
287{
288 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
289 unsigned int restore, ctrl;
290
291 pm_runtime_get_sync(dev);
292
293 /* context restore */
b5f8b763
PR
294 ctrl = CNTRLREG_STEPCONFIGWRT | CNTRLREG_STEPID;
295 if (tscadc_dev->tsc_cell != -1)
296 ctrl |= CNTRLREG_TSCENB | CNTRLREG_4WIRE;
01636eb9 297 tscadc_writel(tscadc_dev, REG_CTRL, ctrl);
b5f8b763
PR
298
299 if (tscadc_dev->tsc_cell != -1)
300 tscadc_idle_config(tscadc_dev);
abeccee4 301 am335x_tsc_se_update(tscadc_dev);
01636eb9
PR
302 restore = tscadc_readl(tscadc_dev, REG_CTRL);
303 tscadc_writel(tscadc_dev, REG_CTRL,
304 (restore | CNTRLREG_TSCSSENB));
305
306 return 0;
307}
308
309static const struct dev_pm_ops tscadc_pm_ops = {
310 .suspend = tscadc_suspend,
311 .resume = tscadc_resume,
312};
313#define TSCADC_PM_OPS (&tscadc_pm_ops)
314#else
315#define TSCADC_PM_OPS NULL
316#endif
317
a6543a1c
PR
318static const struct of_device_id ti_tscadc_dt_ids[] = {
319 { .compatible = "ti,am3359-tscadc", },
320 { }
321};
322MODULE_DEVICE_TABLE(of, ti_tscadc_dt_ids);
323
01636eb9
PR
324static struct platform_driver ti_tscadc_driver = {
325 .driver = {
a6543a1c 326 .name = "ti_am3359-tscadc",
01636eb9
PR
327 .owner = THIS_MODULE,
328 .pm = TSCADC_PM_OPS,
a6543a1c 329 .of_match_table = of_match_ptr(ti_tscadc_dt_ids),
01636eb9
PR
330 },
331 .probe = ti_tscadc_probe,
612b95cd 332 .remove = ti_tscadc_remove,
01636eb9
PR
333
334};
335
336module_platform_driver(ti_tscadc_driver);
337
338MODULE_DESCRIPTION("TI touchscreen / ADC MFD controller driver");
339MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
340MODULE_LICENSE("GPL");
This page took 0.127459 seconds and 5 git commands to generate.