Lynx framebuffers multidomain implementation.
[linux/elbrus.git] / drivers / mfd / ti_am335x_tscadc.c
blobe87a2485468f27813ba82da1bde89cb810a243c3
1 /*
2 * TI Touch Screen / ADC MFD driver
4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
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.
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.
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>
25 #include <linux/of.h>
26 #include <linux/of_device.h>
27 #include <linux/sched.h>
29 #include <linux/mfd/ti_am335x_tscadc.h>
31 static unsigned int tscadc_readl(struct ti_tscadc_dev *tsadc, unsigned int reg)
33 unsigned int val;
35 regmap_read(tsadc->regmap_tscadc, reg, &val);
36 return val;
39 static void tscadc_writel(struct ti_tscadc_dev *tsadc, unsigned int reg,
40 unsigned int val)
42 regmap_write(tsadc->regmap_tscadc, reg, val);
45 static const struct regmap_config tscadc_regmap_config = {
46 .name = "ti_tscadc",
47 .reg_bits = 32,
48 .reg_stride = 4,
49 .val_bits = 32,
52 void am335x_tsc_se_set_cache(struct ti_tscadc_dev *tsadc, u32 val)
54 unsigned long flags;
56 spin_lock_irqsave(&tsadc->reg_lock, flags);
57 tsadc->reg_se_cache |= val;
58 if (tsadc->adc_waiting)
59 wake_up(&tsadc->reg_se_wait);
60 else if (!tsadc->adc_in_use)
61 tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);
63 spin_unlock_irqrestore(&tsadc->reg_lock, flags);
65 EXPORT_SYMBOL_GPL(am335x_tsc_se_set_cache);
67 static void am335x_tscadc_need_adc(struct ti_tscadc_dev *tsadc)
69 DEFINE_WAIT(wait);
70 u32 reg;
73 * disable TSC steps so it does not run while the ADC is using it. If
74 * write 0 while it is running (it just started or was already running)
75 * then it completes all steps that were enabled and stops then.
77 tscadc_writel(tsadc, REG_SE, 0);
78 reg = tscadc_readl(tsadc, REG_ADCFSM);
79 if (reg & SEQ_STATUS) {
80 tsadc->adc_waiting = true;
81 prepare_to_wait(&tsadc->reg_se_wait, &wait,
82 TASK_UNINTERRUPTIBLE);
83 spin_unlock_irq(&tsadc->reg_lock);
85 schedule();
87 spin_lock_irq(&tsadc->reg_lock);
88 finish_wait(&tsadc->reg_se_wait, &wait);
90 reg = tscadc_readl(tsadc, REG_ADCFSM);
91 WARN_ON(reg & SEQ_STATUS);
92 tsadc->adc_waiting = false;
94 tsadc->adc_in_use = true;
97 void am335x_tsc_se_set_once(struct ti_tscadc_dev *tsadc, u32 val)
99 spin_lock_irq(&tsadc->reg_lock);
100 tsadc->reg_se_cache |= val;
101 am335x_tscadc_need_adc(tsadc);
103 tscadc_writel(tsadc, REG_SE, val);
104 spin_unlock_irq(&tsadc->reg_lock);
106 EXPORT_SYMBOL_GPL(am335x_tsc_se_set_once);
108 void am335x_tsc_se_adc_done(struct ti_tscadc_dev *tsadc)
110 unsigned long flags;
112 spin_lock_irqsave(&tsadc->reg_lock, flags);
113 tsadc->adc_in_use = false;
114 tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);
115 spin_unlock_irqrestore(&tsadc->reg_lock, flags);
117 EXPORT_SYMBOL_GPL(am335x_tsc_se_adc_done);
119 void am335x_tsc_se_clr(struct ti_tscadc_dev *tsadc, u32 val)
121 unsigned long flags;
123 spin_lock_irqsave(&tsadc->reg_lock, flags);
124 tsadc->reg_se_cache &= ~val;
125 tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);
126 spin_unlock_irqrestore(&tsadc->reg_lock, flags);
128 EXPORT_SYMBOL_GPL(am335x_tsc_se_clr);
130 static void tscadc_idle_config(struct ti_tscadc_dev *config)
132 unsigned int idleconfig;
134 idleconfig = STEPCONFIG_YNN | STEPCONFIG_INM_ADCREFM |
135 STEPCONFIG_INP_ADCREFM | STEPCONFIG_YPN;
137 tscadc_writel(config, REG_IDLECONFIG, idleconfig);
140 static int ti_tscadc_probe(struct platform_device *pdev)
142 struct ti_tscadc_dev *tscadc;
143 struct resource *res;
144 struct clk *clk;
145 struct device_node *node = pdev->dev.of_node;
146 struct mfd_cell *cell;
147 struct property *prop;
148 const __be32 *cur;
149 u32 val;
150 int err, ctrl;
151 int clock_rate;
152 int tsc_wires = 0, adc_channels = 0, total_channels;
153 int readouts = 0;
155 if (!pdev->dev.of_node) {
156 dev_err(&pdev->dev, "Could not find valid DT data.\n");
157 return -EINVAL;
160 node = of_get_child_by_name(pdev->dev.of_node, "tsc");
161 of_property_read_u32(node, "ti,wires", &tsc_wires);
162 of_property_read_u32(node, "ti,coordiante-readouts", &readouts);
164 node = of_get_child_by_name(pdev->dev.of_node, "adc");
165 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
166 adc_channels++;
167 if (val > 7) {
168 dev_err(&pdev->dev, " PIN numbers are 0..7 (not %d)\n",
169 val);
170 return -EINVAL;
173 total_channels = tsc_wires + adc_channels;
174 if (total_channels > 8) {
175 dev_err(&pdev->dev, "Number of i/p channels more than 8\n");
176 return -EINVAL;
178 if (total_channels == 0) {
179 dev_err(&pdev->dev, "Need atleast one channel.\n");
180 return -EINVAL;
183 if (readouts * 2 + 2 + adc_channels > 16) {
184 dev_err(&pdev->dev, "Too many step configurations requested\n");
185 return -EINVAL;
188 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
189 if (!res) {
190 dev_err(&pdev->dev, "no memory resource defined.\n");
191 return -EINVAL;
194 /* Allocate memory for device */
195 tscadc = devm_kzalloc(&pdev->dev,
196 sizeof(struct ti_tscadc_dev), GFP_KERNEL);
197 if (!tscadc) {
198 dev_err(&pdev->dev, "failed to allocate memory.\n");
199 return -ENOMEM;
201 tscadc->dev = &pdev->dev;
203 err = platform_get_irq(pdev, 0);
204 if (err < 0) {
205 dev_err(&pdev->dev, "no irq ID is specified.\n");
206 goto ret;
207 } else
208 tscadc->irq = err;
210 res = devm_request_mem_region(&pdev->dev,
211 res->start, resource_size(res), pdev->name);
212 if (!res) {
213 dev_err(&pdev->dev, "failed to reserve registers.\n");
214 return -EBUSY;
217 tscadc->tscadc_base = devm_ioremap(&pdev->dev,
218 res->start, resource_size(res));
219 if (!tscadc->tscadc_base) {
220 dev_err(&pdev->dev, "failed to map registers.\n");
221 return -ENOMEM;
224 tscadc->regmap_tscadc = devm_regmap_init_mmio(&pdev->dev,
225 tscadc->tscadc_base, &tscadc_regmap_config);
226 if (IS_ERR(tscadc->regmap_tscadc)) {
227 dev_err(&pdev->dev, "regmap init failed\n");
228 err = PTR_ERR(tscadc->regmap_tscadc);
229 goto ret;
232 spin_lock_init(&tscadc->reg_lock);
233 init_waitqueue_head(&tscadc->reg_se_wait);
235 pm_runtime_enable(&pdev->dev);
236 pm_runtime_get_sync(&pdev->dev);
239 * The TSC_ADC_Subsystem has 2 clock domains
240 * OCP_CLK and ADC_CLK.
241 * The ADC clock is expected to run at target of 3MHz,
242 * and expected to capture 12-bit data at a rate of 200 KSPS.
243 * The TSC_ADC_SS controller design assumes the OCP clock is
244 * at least 6x faster than the ADC clock.
246 clk = clk_get(&pdev->dev, "adc_tsc_fck");
247 if (IS_ERR(clk)) {
248 dev_err(&pdev->dev, "failed to get TSC fck\n");
249 err = PTR_ERR(clk);
250 goto err_disable_clk;
252 clock_rate = clk_get_rate(clk);
253 clk_put(clk);
254 tscadc->clk_div = clock_rate / ADC_CLK;
256 /* TSCADC_CLKDIV needs to be configured to the value minus 1 */
257 tscadc->clk_div--;
258 tscadc_writel(tscadc, REG_CLKDIV, tscadc->clk_div);
260 /* Set the control register bits */
261 ctrl = CNTRLREG_STEPCONFIGWRT |
262 CNTRLREG_STEPID;
263 if (tsc_wires > 0)
264 ctrl |= CNTRLREG_4WIRE | CNTRLREG_TSCENB;
265 tscadc_writel(tscadc, REG_CTRL, ctrl);
267 /* Set register bits for Idle Config Mode */
268 if (tsc_wires > 0)
269 tscadc_idle_config(tscadc);
271 /* Enable the TSC module enable bit */
272 ctrl = tscadc_readl(tscadc, REG_CTRL);
273 ctrl |= CNTRLREG_TSCSSENB;
274 tscadc_writel(tscadc, REG_CTRL, ctrl);
276 tscadc->used_cells = 0;
277 tscadc->tsc_cell = -1;
278 tscadc->adc_cell = -1;
280 /* TSC Cell */
281 if (tsc_wires > 0) {
282 tscadc->tsc_cell = tscadc->used_cells;
283 cell = &tscadc->cells[tscadc->used_cells++];
284 cell->name = "TI-am335x-tsc";
285 cell->of_compatible = "ti,am3359-tsc";
286 cell->platform_data = &tscadc;
287 cell->pdata_size = sizeof(tscadc);
290 /* ADC Cell */
291 if (adc_channels > 0) {
292 tscadc->adc_cell = tscadc->used_cells;
293 cell = &tscadc->cells[tscadc->used_cells++];
294 cell->name = "TI-am335x-adc";
295 cell->of_compatible = "ti,am3359-adc";
296 cell->platform_data = &tscadc;
297 cell->pdata_size = sizeof(tscadc);
300 err = mfd_add_devices(&pdev->dev, pdev->id, tscadc->cells,
301 tscadc->used_cells, NULL, 0, NULL);
302 if (err < 0)
303 goto err_disable_clk;
305 device_init_wakeup(&pdev->dev, true);
306 platform_set_drvdata(pdev, tscadc);
307 return 0;
309 err_disable_clk:
310 pm_runtime_put_sync(&pdev->dev);
311 pm_runtime_disable(&pdev->dev);
312 ret:
313 return err;
316 static int ti_tscadc_remove(struct platform_device *pdev)
318 struct ti_tscadc_dev *tscadc = platform_get_drvdata(pdev);
320 tscadc_writel(tscadc, REG_SE, 0x00);
322 pm_runtime_put_sync(&pdev->dev);
323 pm_runtime_disable(&pdev->dev);
325 mfd_remove_devices(tscadc->dev);
327 return 0;
330 #ifdef CONFIG_PM
331 static int tscadc_suspend(struct device *dev)
333 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
335 tscadc_writel(tscadc_dev, REG_SE, 0x00);
336 pm_runtime_put_sync(dev);
338 return 0;
341 static int tscadc_resume(struct device *dev)
343 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
344 unsigned int restore, ctrl;
346 pm_runtime_get_sync(dev);
348 /* context restore */
349 ctrl = CNTRLREG_STEPCONFIGWRT | CNTRLREG_STEPID;
350 if (tscadc_dev->tsc_cell != -1)
351 ctrl |= CNTRLREG_TSCENB | CNTRLREG_4WIRE;
352 tscadc_writel(tscadc_dev, REG_CTRL, ctrl);
354 if (tscadc_dev->tsc_cell != -1)
355 tscadc_idle_config(tscadc_dev);
356 restore = tscadc_readl(tscadc_dev, REG_CTRL);
357 tscadc_writel(tscadc_dev, REG_CTRL,
358 (restore | CNTRLREG_TSCSSENB));
360 tscadc_writel(tscadc_dev, REG_CLKDIV, tscadc_dev->clk_div);
362 return 0;
365 static const struct dev_pm_ops tscadc_pm_ops = {
366 .suspend = tscadc_suspend,
367 .resume = tscadc_resume,
369 #define TSCADC_PM_OPS (&tscadc_pm_ops)
370 #else
371 #define TSCADC_PM_OPS NULL
372 #endif
374 static const struct of_device_id ti_tscadc_dt_ids[] = {
375 { .compatible = "ti,am3359-tscadc", },
378 MODULE_DEVICE_TABLE(of, ti_tscadc_dt_ids);
380 static struct platform_driver ti_tscadc_driver = {
381 .driver = {
382 .name = "ti_am3359-tscadc",
383 .owner = THIS_MODULE,
384 .pm = TSCADC_PM_OPS,
385 .of_match_table = ti_tscadc_dt_ids,
387 .probe = ti_tscadc_probe,
388 .remove = ti_tscadc_remove,
392 module_platform_driver(ti_tscadc_driver);
394 MODULE_DESCRIPTION("TI touchscreen / ADC MFD controller driver");
395 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
396 MODULE_LICENSE("GPL");