blob: 88718abfb9ba0169090201e8fc04037ea4f32854 [file] [log] [blame]
Patil, Rachna01636eb2012-10-16 12:55:43 +05301/*
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>
Patil, Rachnaa6543a12013-01-24 03:45:09 +000025#include <linux/of.h>
26#include <linux/of_device.h>
Patil, Rachna01636eb2012-10-16 12:55:43 +053027
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
Patil, Rachnaabeccee2013-01-24 03:45:05 +000051void 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{
Sebastian Andrzej Siewior317b2092013-10-22 16:12:39 +020059 unsigned long flags;
60
61 spin_lock_irqsave(&tsadc->reg_lock, flags);
Zubair Lutfullah45491ad2013-08-05 20:10:45 +010062 tsadc->reg_se_cache = tscadc_readl(tsadc, REG_SE);
Patil, Rachnaabeccee2013-01-24 03:45:05 +000063 tsadc->reg_se_cache |= val;
Patil, Rachnaabeccee2013-01-24 03:45:05 +000064 am335x_tsc_se_update(tsadc);
Sebastian Andrzej Siewior317b2092013-10-22 16:12:39 +020065 spin_unlock_irqrestore(&tsadc->reg_lock, flags);
Patil, Rachnaabeccee2013-01-24 03:45:05 +000066}
67EXPORT_SYMBOL_GPL(am335x_tsc_se_set);
68
69void am335x_tsc_se_clr(struct ti_tscadc_dev *tsadc, u32 val)
70{
Sebastian Andrzej Siewior317b2092013-10-22 16:12:39 +020071 unsigned long flags;
72
73 spin_lock_irqsave(&tsadc->reg_lock, flags);
Zubair Lutfullah5d945d92013-08-13 17:40:56 +010074 tsadc->reg_se_cache = tscadc_readl(tsadc, REG_SE);
Patil, Rachnaabeccee2013-01-24 03:45:05 +000075 tsadc->reg_se_cache &= ~val;
Patil, Rachnaabeccee2013-01-24 03:45:05 +000076 am335x_tsc_se_update(tsadc);
Sebastian Andrzej Siewior317b2092013-10-22 16:12:39 +020077 spin_unlock_irqrestore(&tsadc->reg_lock, flags);
Patil, Rachnaabeccee2013-01-24 03:45:05 +000078}
79EXPORT_SYMBOL_GPL(am335x_tsc_se_clr);
80
Patil, Rachna01636eb2012-10-16 12:55:43 +053081static void tscadc_idle_config(struct ti_tscadc_dev *config)
82{
83 unsigned int idleconfig;
84
85 idleconfig = STEPCONFIG_YNN | STEPCONFIG_INM_ADCREFM |
86 STEPCONFIG_INP_ADCREFM | STEPCONFIG_YPN;
87
88 tscadc_writel(config, REG_IDLECONFIG, idleconfig);
89}
90
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -080091static int ti_tscadc_probe(struct platform_device *pdev)
Patil, Rachna01636eb2012-10-16 12:55:43 +053092{
93 struct ti_tscadc_dev *tscadc;
94 struct resource *res;
95 struct clk *clk;
Patil, Rachnaa6543a12013-01-24 03:45:09 +000096 struct device_node *node = pdev->dev.of_node;
Patil, Rachna2b99baf2012-10-16 12:55:44 +053097 struct mfd_cell *cell;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020098 struct property *prop;
99 const __be32 *cur;
100 u32 val;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530101 int err, ctrl;
Matthias Kaehlckee90f8752013-09-23 22:43:29 +0200102 int clock_rate;
Patil, Rachnaa6543a12013-01-24 03:45:09 +0000103 int tsc_wires = 0, adc_channels = 0, total_channels;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200104 int readouts = 0;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530105
Sebastian Andrzej Siewior9e5775f2013-05-21 17:56:49 +0200106 if (!pdev->dev.of_node) {
107 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna01636eb2012-10-16 12:55:43 +0530108 return -EINVAL;
109 }
110
Sebastian Andrzej Siewior9e5775f2013-05-21 17:56:49 +0200111 node = of_get_child_by_name(pdev->dev.of_node, "tsc");
112 of_property_read_u32(node, "ti,wires", &tsc_wires);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200113 of_property_read_u32(node, "ti,coordiante-readouts", &readouts);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530114
Sebastian Andrzej Siewior9e5775f2013-05-21 17:56:49 +0200115 node = of_get_child_by_name(pdev->dev.of_node, "adc");
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200116 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
117 adc_channels++;
118 if (val > 7) {
119 dev_err(&pdev->dev, " PIN numbers are 0..7 (not %d)\n",
120 val);
121 return -EINVAL;
122 }
123 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530124 total_channels = tsc_wires + adc_channels;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530125 if (total_channels > 8) {
126 dev_err(&pdev->dev, "Number of i/p channels more than 8\n");
127 return -EINVAL;
128 }
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300129 if (total_channels == 0) {
130 dev_err(&pdev->dev, "Need atleast one channel.\n");
131 return -EINVAL;
132 }
Patil, Rachna2b99baf2012-10-16 12:55:44 +0530133
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200134 if (readouts * 2 + 2 + adc_channels > 16) {
135 dev_err(&pdev->dev, "Too many step configurations requested\n");
136 return -EINVAL;
137 }
138
Patil, Rachna01636eb2012-10-16 12:55:43 +0530139 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
140 if (!res) {
141 dev_err(&pdev->dev, "no memory resource defined.\n");
142 return -EINVAL;
143 }
144
Patil, Rachna01636eb2012-10-16 12:55:43 +0530145 /* Allocate memory for device */
146 tscadc = devm_kzalloc(&pdev->dev,
147 sizeof(struct ti_tscadc_dev), GFP_KERNEL);
148 if (!tscadc) {
149 dev_err(&pdev->dev, "failed to allocate memory.\n");
150 return -ENOMEM;
151 }
152 tscadc->dev = &pdev->dev;
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530153
154 err = platform_get_irq(pdev, 0);
155 if (err < 0) {
156 dev_err(&pdev->dev, "no irq ID is specified.\n");
157 goto ret;
158 } else
159 tscadc->irq = err;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530160
161 res = devm_request_mem_region(&pdev->dev,
162 res->start, resource_size(res), pdev->name);
163 if (!res) {
164 dev_err(&pdev->dev, "failed to reserve registers.\n");
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530165 return -EBUSY;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530166 }
167
168 tscadc->tscadc_base = devm_ioremap(&pdev->dev,
169 res->start, resource_size(res));
170 if (!tscadc->tscadc_base) {
171 dev_err(&pdev->dev, "failed to map registers.\n");
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530172 return -ENOMEM;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530173 }
174
175 tscadc->regmap_tscadc = devm_regmap_init_mmio(&pdev->dev,
176 tscadc->tscadc_base, &tscadc_regmap_config);
177 if (IS_ERR(tscadc->regmap_tscadc)) {
178 dev_err(&pdev->dev, "regmap init failed\n");
179 err = PTR_ERR(tscadc->regmap_tscadc);
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530180 goto ret;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530181 }
182
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000183 spin_lock_init(&tscadc->reg_lock);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530184 pm_runtime_enable(&pdev->dev);
185 pm_runtime_get_sync(&pdev->dev);
186
187 /*
188 * The TSC_ADC_Subsystem has 2 clock domains
189 * OCP_CLK and ADC_CLK.
190 * The ADC clock is expected to run at target of 3MHz,
191 * and expected to capture 12-bit data at a rate of 200 KSPS.
192 * The TSC_ADC_SS controller design assumes the OCP clock is
193 * at least 6x faster than the ADC clock.
194 */
195 clk = clk_get(&pdev->dev, "adc_tsc_fck");
196 if (IS_ERR(clk)) {
197 dev_err(&pdev->dev, "failed to get TSC fck\n");
198 err = PTR_ERR(clk);
199 goto err_disable_clk;
200 }
201 clock_rate = clk_get_rate(clk);
202 clk_put(clk);
Matthias Kaehlckee90f8752013-09-23 22:43:29 +0200203 tscadc->clk_div = clock_rate / ADC_CLK;
Patil, Rachnaefe31262013-07-20 17:27:35 +0100204
Patil, Rachna01636eb2012-10-16 12:55:43 +0530205 /* TSCADC_CLKDIV needs to be configured to the value minus 1 */
Matthias Kaehlckee90f8752013-09-23 22:43:29 +0200206 tscadc->clk_div--;
207 tscadc_writel(tscadc, REG_CLKDIV, tscadc->clk_div);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530208
209 /* Set the control register bits */
210 ctrl = CNTRLREG_STEPCONFIGWRT |
Patil, Rachnab5f8b762013-07-20 17:27:34 +0100211 CNTRLREG_STEPID;
212 if (tsc_wires > 0)
213 ctrl |= CNTRLREG_4WIRE | CNTRLREG_TSCENB;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530214 tscadc_writel(tscadc, REG_CTRL, ctrl);
215
216 /* Set register bits for Idle Config Mode */
Patil, Rachnab5f8b762013-07-20 17:27:34 +0100217 if (tsc_wires > 0)
218 tscadc_idle_config(tscadc);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530219
220 /* Enable the TSC module enable bit */
221 ctrl = tscadc_readl(tscadc, REG_CTRL);
222 ctrl |= CNTRLREG_TSCSSENB;
223 tscadc_writel(tscadc, REG_CTRL, ctrl);
224
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300225 tscadc->used_cells = 0;
226 tscadc->tsc_cell = -1;
227 tscadc->adc_cell = -1;
228
Patil, Rachna2b99baf2012-10-16 12:55:44 +0530229 /* TSC Cell */
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300230 if (tsc_wires > 0) {
231 tscadc->tsc_cell = tscadc->used_cells;
232 cell = &tscadc->cells[tscadc->used_cells++];
Sebastian Andrzej Siewior5f184e62013-05-27 17:08:28 +0200233 cell->name = "TI-am335x-tsc";
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300234 cell->of_compatible = "ti,am3359-tsc";
235 cell->platform_data = &tscadc;
236 cell->pdata_size = sizeof(tscadc);
237 }
Patil, Rachna2b99baf2012-10-16 12:55:44 +0530238
Patil, Rachna5e53a692012-10-16 12:55:45 +0530239 /* ADC Cell */
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300240 if (adc_channels > 0) {
241 tscadc->adc_cell = tscadc->used_cells;
242 cell = &tscadc->cells[tscadc->used_cells++];
Sebastian Andrzej Siewior9f999282013-05-27 17:12:52 +0200243 cell->name = "TI-am335x-adc";
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300244 cell->of_compatible = "ti,am3359-adc";
245 cell->platform_data = &tscadc;
246 cell->pdata_size = sizeof(tscadc);
247 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530248
Patil, Rachna01636eb2012-10-16 12:55:43 +0530249 err = mfd_add_devices(&pdev->dev, pdev->id, tscadc->cells,
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300250 tscadc->used_cells, NULL, 0, NULL);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530251 if (err < 0)
252 goto err_disable_clk;
253
254 device_init_wakeup(&pdev->dev, true);
255 platform_set_drvdata(pdev, tscadc);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530256 return 0;
257
258err_disable_clk:
259 pm_runtime_put_sync(&pdev->dev);
260 pm_runtime_disable(&pdev->dev);
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530261ret:
Patil, Rachna01636eb2012-10-16 12:55:43 +0530262 return err;
263}
264
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800265static int ti_tscadc_remove(struct platform_device *pdev)
Patil, Rachna01636eb2012-10-16 12:55:43 +0530266{
267 struct ti_tscadc_dev *tscadc = platform_get_drvdata(pdev);
268
269 tscadc_writel(tscadc, REG_SE, 0x00);
270
271 pm_runtime_put_sync(&pdev->dev);
272 pm_runtime_disable(&pdev->dev);
273
274 mfd_remove_devices(tscadc->dev);
275
276 return 0;
277}
278
279#ifdef CONFIG_PM
280static int tscadc_suspend(struct device *dev)
281{
282 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
283
284 tscadc_writel(tscadc_dev, REG_SE, 0x00);
285 pm_runtime_put_sync(dev);
286
287 return 0;
288}
289
290static int tscadc_resume(struct device *dev)
291{
292 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
293 unsigned int restore, ctrl;
294
295 pm_runtime_get_sync(dev);
296
297 /* context restore */
Patil, Rachnab5f8b762013-07-20 17:27:34 +0100298 ctrl = CNTRLREG_STEPCONFIGWRT | CNTRLREG_STEPID;
299 if (tscadc_dev->tsc_cell != -1)
300 ctrl |= CNTRLREG_TSCENB | CNTRLREG_4WIRE;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530301 tscadc_writel(tscadc_dev, REG_CTRL, ctrl);
Patil, Rachnab5f8b762013-07-20 17:27:34 +0100302
303 if (tscadc_dev->tsc_cell != -1)
304 tscadc_idle_config(tscadc_dev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000305 am335x_tsc_se_update(tscadc_dev);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530306 restore = tscadc_readl(tscadc_dev, REG_CTRL);
307 tscadc_writel(tscadc_dev, REG_CTRL,
308 (restore | CNTRLREG_TSCSSENB));
309
Matthias Kaehlckee90f8752013-09-23 22:43:29 +0200310 tscadc_writel(tscadc_dev, REG_CLKDIV, tscadc_dev->clk_div);
311
Patil, Rachna01636eb2012-10-16 12:55:43 +0530312 return 0;
313}
314
315static const struct dev_pm_ops tscadc_pm_ops = {
316 .suspend = tscadc_suspend,
317 .resume = tscadc_resume,
318};
319#define TSCADC_PM_OPS (&tscadc_pm_ops)
320#else
321#define TSCADC_PM_OPS NULL
322#endif
323
Patil, Rachnaa6543a12013-01-24 03:45:09 +0000324static const struct of_device_id ti_tscadc_dt_ids[] = {
325 { .compatible = "ti,am3359-tscadc", },
326 { }
327};
328MODULE_DEVICE_TABLE(of, ti_tscadc_dt_ids);
329
Patil, Rachna01636eb2012-10-16 12:55:43 +0530330static struct platform_driver ti_tscadc_driver = {
331 .driver = {
Patil, Rachnaa6543a12013-01-24 03:45:09 +0000332 .name = "ti_am3359-tscadc",
Patil, Rachna01636eb2012-10-16 12:55:43 +0530333 .owner = THIS_MODULE,
334 .pm = TSCADC_PM_OPS,
Sachin Kamat131221b2013-10-15 09:18:49 +0530335 .of_match_table = ti_tscadc_dt_ids,
Patil, Rachna01636eb2012-10-16 12:55:43 +0530336 },
337 .probe = ti_tscadc_probe,
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800338 .remove = ti_tscadc_remove,
Patil, Rachna01636eb2012-10-16 12:55:43 +0530339
340};
341
342module_platform_driver(ti_tscadc_driver);
343
344MODULE_DESCRIPTION("TI touchscreen / ADC MFD controller driver");
345MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
346MODULE_LICENSE("GPL");