blob: cb0c211fc7d8adb0063230179886e51f8e4663aa [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
Sebastian Andrzej Siewior3466bd22013-12-19 16:28:28 +010051static void am335x_tsc_se_update(struct ti_tscadc_dev *tsadc)
Patil, Rachnaabeccee2013-01-24 03:45:05 +000052{
53 tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);
54}
Patil, Rachnaabeccee2013-01-24 03:45:05 +000055
Sebastian Andrzej Siewior7e170c62013-12-19 16:28:29 +010056void am335x_tsc_se_set_cache(struct ti_tscadc_dev *tsadc, u32 val)
Patil, Rachnaabeccee2013-01-24 03:45:05 +000057{
Sebastian Andrzej Siewior317b2092013-10-22 16:12:39 +020058 unsigned long flags;
59
60 spin_lock_irqsave(&tsadc->reg_lock, flags);
Patil, Rachnaabeccee2013-01-24 03:45:05 +000061 tsadc->reg_se_cache |= val;
Patil, Rachnaabeccee2013-01-24 03:45:05 +000062 am335x_tsc_se_update(tsadc);
Sebastian Andrzej Siewior317b2092013-10-22 16:12:39 +020063 spin_unlock_irqrestore(&tsadc->reg_lock, flags);
Patil, Rachnaabeccee2013-01-24 03:45:05 +000064}
Sebastian Andrzej Siewior7e170c62013-12-19 16:28:29 +010065EXPORT_SYMBOL_GPL(am335x_tsc_se_set_cache);
66
67void am335x_tsc_se_set_once(struct ti_tscadc_dev *tsadc, u32 val)
68{
69 unsigned long flags;
70
71 spin_lock_irqsave(&tsadc->reg_lock, flags);
72 tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache | val);
73 spin_unlock_irqrestore(&tsadc->reg_lock, flags);
74}
75EXPORT_SYMBOL_GPL(am335x_tsc_se_set_once);
Patil, Rachnaabeccee2013-01-24 03:45:05 +000076
77void am335x_tsc_se_clr(struct ti_tscadc_dev *tsadc, u32 val)
78{
Sebastian Andrzej Siewior317b2092013-10-22 16:12:39 +020079 unsigned long flags;
80
81 spin_lock_irqsave(&tsadc->reg_lock, flags);
Patil, Rachnaabeccee2013-01-24 03:45:05 +000082 tsadc->reg_se_cache &= ~val;
Patil, Rachnaabeccee2013-01-24 03:45:05 +000083 am335x_tsc_se_update(tsadc);
Sebastian Andrzej Siewior317b2092013-10-22 16:12:39 +020084 spin_unlock_irqrestore(&tsadc->reg_lock, flags);
Patil, Rachnaabeccee2013-01-24 03:45:05 +000085}
86EXPORT_SYMBOL_GPL(am335x_tsc_se_clr);
87
Patil, Rachna01636eb2012-10-16 12:55:43 +053088static void tscadc_idle_config(struct ti_tscadc_dev *config)
89{
90 unsigned int idleconfig;
91
92 idleconfig = STEPCONFIG_YNN | STEPCONFIG_INM_ADCREFM |
93 STEPCONFIG_INP_ADCREFM | STEPCONFIG_YPN;
94
95 tscadc_writel(config, REG_IDLECONFIG, idleconfig);
96}
97
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -080098static int ti_tscadc_probe(struct platform_device *pdev)
Patil, Rachna01636eb2012-10-16 12:55:43 +053099{
100 struct ti_tscadc_dev *tscadc;
101 struct resource *res;
102 struct clk *clk;
Patil, Rachnaa6543a12013-01-24 03:45:09 +0000103 struct device_node *node = pdev->dev.of_node;
Patil, Rachna2b99baf2012-10-16 12:55:44 +0530104 struct mfd_cell *cell;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200105 struct property *prop;
106 const __be32 *cur;
107 u32 val;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530108 int err, ctrl;
Matthias Kaehlckee90f8752013-09-23 22:43:29 +0200109 int clock_rate;
Patil, Rachnaa6543a12013-01-24 03:45:09 +0000110 int tsc_wires = 0, adc_channels = 0, total_channels;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200111 int readouts = 0;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530112
Sebastian Andrzej Siewior9e5775f2013-05-21 17:56:49 +0200113 if (!pdev->dev.of_node) {
114 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna01636eb2012-10-16 12:55:43 +0530115 return -EINVAL;
116 }
117
Sebastian Andrzej Siewior9e5775f2013-05-21 17:56:49 +0200118 node = of_get_child_by_name(pdev->dev.of_node, "tsc");
119 of_property_read_u32(node, "ti,wires", &tsc_wires);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200120 of_property_read_u32(node, "ti,coordiante-readouts", &readouts);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530121
Sebastian Andrzej Siewior9e5775f2013-05-21 17:56:49 +0200122 node = of_get_child_by_name(pdev->dev.of_node, "adc");
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200123 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
124 adc_channels++;
125 if (val > 7) {
126 dev_err(&pdev->dev, " PIN numbers are 0..7 (not %d)\n",
127 val);
128 return -EINVAL;
129 }
130 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530131 total_channels = tsc_wires + adc_channels;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530132 if (total_channels > 8) {
133 dev_err(&pdev->dev, "Number of i/p channels more than 8\n");
134 return -EINVAL;
135 }
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300136 if (total_channels == 0) {
137 dev_err(&pdev->dev, "Need atleast one channel.\n");
138 return -EINVAL;
139 }
Patil, Rachna2b99baf2012-10-16 12:55:44 +0530140
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200141 if (readouts * 2 + 2 + adc_channels > 16) {
142 dev_err(&pdev->dev, "Too many step configurations requested\n");
143 return -EINVAL;
144 }
145
Patil, Rachna01636eb2012-10-16 12:55:43 +0530146 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
147 if (!res) {
148 dev_err(&pdev->dev, "no memory resource defined.\n");
149 return -EINVAL;
150 }
151
Patil, Rachna01636eb2012-10-16 12:55:43 +0530152 /* Allocate memory for device */
153 tscadc = devm_kzalloc(&pdev->dev,
154 sizeof(struct ti_tscadc_dev), GFP_KERNEL);
155 if (!tscadc) {
156 dev_err(&pdev->dev, "failed to allocate memory.\n");
157 return -ENOMEM;
158 }
159 tscadc->dev = &pdev->dev;
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530160
161 err = platform_get_irq(pdev, 0);
162 if (err < 0) {
163 dev_err(&pdev->dev, "no irq ID is specified.\n");
164 goto ret;
165 } else
166 tscadc->irq = err;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530167
168 res = devm_request_mem_region(&pdev->dev,
169 res->start, resource_size(res), pdev->name);
170 if (!res) {
171 dev_err(&pdev->dev, "failed to reserve registers.\n");
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530172 return -EBUSY;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530173 }
174
175 tscadc->tscadc_base = devm_ioremap(&pdev->dev,
176 res->start, resource_size(res));
177 if (!tscadc->tscadc_base) {
178 dev_err(&pdev->dev, "failed to map registers.\n");
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530179 return -ENOMEM;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530180 }
181
182 tscadc->regmap_tscadc = devm_regmap_init_mmio(&pdev->dev,
183 tscadc->tscadc_base, &tscadc_regmap_config);
184 if (IS_ERR(tscadc->regmap_tscadc)) {
185 dev_err(&pdev->dev, "regmap init failed\n");
186 err = PTR_ERR(tscadc->regmap_tscadc);
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530187 goto ret;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530188 }
189
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000190 spin_lock_init(&tscadc->reg_lock);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530191 pm_runtime_enable(&pdev->dev);
192 pm_runtime_get_sync(&pdev->dev);
193
194 /*
195 * The TSC_ADC_Subsystem has 2 clock domains
196 * OCP_CLK and ADC_CLK.
197 * The ADC clock is expected to run at target of 3MHz,
198 * and expected to capture 12-bit data at a rate of 200 KSPS.
199 * The TSC_ADC_SS controller design assumes the OCP clock is
200 * at least 6x faster than the ADC clock.
201 */
202 clk = clk_get(&pdev->dev, "adc_tsc_fck");
203 if (IS_ERR(clk)) {
204 dev_err(&pdev->dev, "failed to get TSC fck\n");
205 err = PTR_ERR(clk);
206 goto err_disable_clk;
207 }
208 clock_rate = clk_get_rate(clk);
209 clk_put(clk);
Matthias Kaehlckee90f8752013-09-23 22:43:29 +0200210 tscadc->clk_div = clock_rate / ADC_CLK;
Patil, Rachnaefe31262013-07-20 17:27:35 +0100211
Patil, Rachna01636eb2012-10-16 12:55:43 +0530212 /* TSCADC_CLKDIV needs to be configured to the value minus 1 */
Matthias Kaehlckee90f8752013-09-23 22:43:29 +0200213 tscadc->clk_div--;
214 tscadc_writel(tscadc, REG_CLKDIV, tscadc->clk_div);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530215
216 /* Set the control register bits */
217 ctrl = CNTRLREG_STEPCONFIGWRT |
Patil, Rachnab5f8b762013-07-20 17:27:34 +0100218 CNTRLREG_STEPID;
219 if (tsc_wires > 0)
220 ctrl |= CNTRLREG_4WIRE | CNTRLREG_TSCENB;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530221 tscadc_writel(tscadc, REG_CTRL, ctrl);
222
223 /* Set register bits for Idle Config Mode */
Patil, Rachnab5f8b762013-07-20 17:27:34 +0100224 if (tsc_wires > 0)
225 tscadc_idle_config(tscadc);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530226
227 /* Enable the TSC module enable bit */
228 ctrl = tscadc_readl(tscadc, REG_CTRL);
229 ctrl |= CNTRLREG_TSCSSENB;
230 tscadc_writel(tscadc, REG_CTRL, ctrl);
231
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300232 tscadc->used_cells = 0;
233 tscadc->tsc_cell = -1;
234 tscadc->adc_cell = -1;
235
Patil, Rachna2b99baf2012-10-16 12:55:44 +0530236 /* TSC Cell */
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300237 if (tsc_wires > 0) {
238 tscadc->tsc_cell = tscadc->used_cells;
239 cell = &tscadc->cells[tscadc->used_cells++];
Sebastian Andrzej Siewior5f184e62013-05-27 17:08:28 +0200240 cell->name = "TI-am335x-tsc";
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300241 cell->of_compatible = "ti,am3359-tsc";
242 cell->platform_data = &tscadc;
243 cell->pdata_size = sizeof(tscadc);
244 }
Patil, Rachna2b99baf2012-10-16 12:55:44 +0530245
Patil, Rachna5e53a692012-10-16 12:55:45 +0530246 /* ADC Cell */
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300247 if (adc_channels > 0) {
248 tscadc->adc_cell = tscadc->used_cells;
249 cell = &tscadc->cells[tscadc->used_cells++];
Sebastian Andrzej Siewior9f999282013-05-27 17:12:52 +0200250 cell->name = "TI-am335x-adc";
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300251 cell->of_compatible = "ti,am3359-adc";
252 cell->platform_data = &tscadc;
253 cell->pdata_size = sizeof(tscadc);
254 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530255
Patil, Rachna01636eb2012-10-16 12:55:43 +0530256 err = mfd_add_devices(&pdev->dev, pdev->id, tscadc->cells,
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300257 tscadc->used_cells, NULL, 0, NULL);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530258 if (err < 0)
259 goto err_disable_clk;
260
261 device_init_wakeup(&pdev->dev, true);
262 platform_set_drvdata(pdev, tscadc);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530263 return 0;
264
265err_disable_clk:
266 pm_runtime_put_sync(&pdev->dev);
267 pm_runtime_disable(&pdev->dev);
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530268ret:
Patil, Rachna01636eb2012-10-16 12:55:43 +0530269 return err;
270}
271
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800272static int ti_tscadc_remove(struct platform_device *pdev)
Patil, Rachna01636eb2012-10-16 12:55:43 +0530273{
274 struct ti_tscadc_dev *tscadc = platform_get_drvdata(pdev);
275
276 tscadc_writel(tscadc, REG_SE, 0x00);
277
278 pm_runtime_put_sync(&pdev->dev);
279 pm_runtime_disable(&pdev->dev);
280
281 mfd_remove_devices(tscadc->dev);
282
283 return 0;
284}
285
286#ifdef CONFIG_PM
287static int tscadc_suspend(struct device *dev)
288{
289 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
290
291 tscadc_writel(tscadc_dev, REG_SE, 0x00);
292 pm_runtime_put_sync(dev);
293
294 return 0;
295}
296
297static int tscadc_resume(struct device *dev)
298{
299 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
300 unsigned int restore, ctrl;
301
302 pm_runtime_get_sync(dev);
303
304 /* context restore */
Patil, Rachnab5f8b762013-07-20 17:27:34 +0100305 ctrl = CNTRLREG_STEPCONFIGWRT | CNTRLREG_STEPID;
306 if (tscadc_dev->tsc_cell != -1)
307 ctrl |= CNTRLREG_TSCENB | CNTRLREG_4WIRE;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530308 tscadc_writel(tscadc_dev, REG_CTRL, ctrl);
Patil, Rachnab5f8b762013-07-20 17:27:34 +0100309
310 if (tscadc_dev->tsc_cell != -1)
311 tscadc_idle_config(tscadc_dev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000312 am335x_tsc_se_update(tscadc_dev);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530313 restore = tscadc_readl(tscadc_dev, REG_CTRL);
314 tscadc_writel(tscadc_dev, REG_CTRL,
315 (restore | CNTRLREG_TSCSSENB));
316
Matthias Kaehlckee90f8752013-09-23 22:43:29 +0200317 tscadc_writel(tscadc_dev, REG_CLKDIV, tscadc_dev->clk_div);
318
Patil, Rachna01636eb2012-10-16 12:55:43 +0530319 return 0;
320}
321
322static const struct dev_pm_ops tscadc_pm_ops = {
323 .suspend = tscadc_suspend,
324 .resume = tscadc_resume,
325};
326#define TSCADC_PM_OPS (&tscadc_pm_ops)
327#else
328#define TSCADC_PM_OPS NULL
329#endif
330
Patil, Rachnaa6543a12013-01-24 03:45:09 +0000331static const struct of_device_id ti_tscadc_dt_ids[] = {
332 { .compatible = "ti,am3359-tscadc", },
333 { }
334};
335MODULE_DEVICE_TABLE(of, ti_tscadc_dt_ids);
336
Patil, Rachna01636eb2012-10-16 12:55:43 +0530337static struct platform_driver ti_tscadc_driver = {
338 .driver = {
Patil, Rachnaa6543a12013-01-24 03:45:09 +0000339 .name = "ti_am3359-tscadc",
Patil, Rachna01636eb2012-10-16 12:55:43 +0530340 .owner = THIS_MODULE,
341 .pm = TSCADC_PM_OPS,
Sachin Kamat131221b2013-10-15 09:18:49 +0530342 .of_match_table = ti_tscadc_dt_ids,
Patil, Rachna01636eb2012-10-16 12:55:43 +0530343 },
344 .probe = ti_tscadc_probe,
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800345 .remove = ti_tscadc_remove,
Patil, Rachna01636eb2012-10-16 12:55:43 +0530346
347};
348
349module_platform_driver(ti_tscadc_driver);
350
351MODULE_DESCRIPTION("TI touchscreen / ADC MFD controller driver");
352MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
353MODULE_LICENSE("GPL");