blob: e0852ecbc76718f9f497fe4a2fe5d1f7e369325e [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{
59 spin_lock(&tsadc->reg_lock);
60 tsadc->reg_se_cache |= val;
61 spin_unlock(&tsadc->reg_lock);
62
63 am335x_tsc_se_update(tsadc);
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);
70 tsadc->reg_se_cache &= ~val;
71 spin_unlock(&tsadc->reg_lock);
72
73 am335x_tsc_se_update(tsadc);
74}
75EXPORT_SYMBOL_GPL(am335x_tsc_se_clr);
76
Patil, Rachna01636eb2012-10-16 12:55:43 +053077static 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
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -080087static int ti_tscadc_probe(struct platform_device *pdev)
Patil, Rachna01636eb2012-10-16 12:55:43 +053088{
89 struct ti_tscadc_dev *tscadc;
90 struct resource *res;
91 struct clk *clk;
Patil, Rachnaa6543a12013-01-24 03:45:09 +000092 struct device_node *node = pdev->dev.of_node;
Patil, Rachna2b99baf2012-10-16 12:55:44 +053093 struct mfd_cell *cell;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020094 struct property *prop;
95 const __be32 *cur;
96 u32 val;
Patil, Rachna01636eb2012-10-16 12:55:43 +053097 int err, ctrl;
98 int clk_value, clock_rate;
Patil, Rachnaa6543a12013-01-24 03:45:09 +000099 int tsc_wires = 0, adc_channels = 0, total_channels;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200100 int readouts = 0;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530101
Sebastian Andrzej Siewior9e5775f2013-05-21 17:56:49 +0200102 if (!pdev->dev.of_node) {
103 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna01636eb2012-10-16 12:55:43 +0530104 return -EINVAL;
105 }
106
Sebastian Andrzej Siewior9e5775f2013-05-21 17:56:49 +0200107 node = of_get_child_by_name(pdev->dev.of_node, "tsc");
108 of_property_read_u32(node, "ti,wires", &tsc_wires);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200109 of_property_read_u32(node, "ti,coordiante-readouts", &readouts);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530110
Sebastian Andrzej Siewior9e5775f2013-05-21 17:56:49 +0200111 node = of_get_child_by_name(pdev->dev.of_node, "adc");
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200112 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 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530120 total_channels = tsc_wires + adc_channels;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530121 if (total_channels > 8) {
122 dev_err(&pdev->dev, "Number of i/p channels more than 8\n");
123 return -EINVAL;
124 }
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300125 if (total_channels == 0) {
126 dev_err(&pdev->dev, "Need atleast one channel.\n");
127 return -EINVAL;
128 }
Patil, Rachna2b99baf2012-10-16 12:55:44 +0530129
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200130 if (readouts * 2 + 2 + adc_channels > 16) {
131 dev_err(&pdev->dev, "Too many step configurations requested\n");
132 return -EINVAL;
133 }
134
Patil, Rachna01636eb2012-10-16 12:55:43 +0530135 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
Patil, Rachna01636eb2012-10-16 12:55:43 +0530141 /* 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;
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530149
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;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530156
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");
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530161 return -EBUSY;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530162 }
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");
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530168 return -ENOMEM;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530169 }
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);
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530176 goto ret;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530177 }
178
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000179 spin_lock_init(&tscadc->reg_lock);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530180 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;
Patil, Rachnaefe31262013-07-20 17:27:35 +0100200
Patil, Rachna01636eb2012-10-16 12:55:43 +0530201 /* 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 |
207 CNTRLREG_TSCENB |
208 CNTRLREG_STEPID |
209 CNTRLREG_4WIRE;
210 tscadc_writel(tscadc, REG_CTRL, ctrl);
211
212 /* Set register bits for Idle Config Mode */
213 tscadc_idle_config(tscadc);
214
215 /* Enable the TSC module enable bit */
216 ctrl = tscadc_readl(tscadc, REG_CTRL);
217 ctrl |= CNTRLREG_TSCSSENB;
218 tscadc_writel(tscadc, REG_CTRL, ctrl);
219
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300220 tscadc->used_cells = 0;
221 tscadc->tsc_cell = -1;
222 tscadc->adc_cell = -1;
223
Patil, Rachna2b99baf2012-10-16 12:55:44 +0530224 /* TSC Cell */
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300225 if (tsc_wires > 0) {
226 tscadc->tsc_cell = tscadc->used_cells;
227 cell = &tscadc->cells[tscadc->used_cells++];
Sebastian Andrzej Siewior5f184e62013-05-27 17:08:28 +0200228 cell->name = "TI-am335x-tsc";
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300229 cell->of_compatible = "ti,am3359-tsc";
230 cell->platform_data = &tscadc;
231 cell->pdata_size = sizeof(tscadc);
232 }
Patil, Rachna2b99baf2012-10-16 12:55:44 +0530233
Patil, Rachna5e53a692012-10-16 12:55:45 +0530234 /* ADC Cell */
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300235 if (adc_channels > 0) {
236 tscadc->adc_cell = tscadc->used_cells;
237 cell = &tscadc->cells[tscadc->used_cells++];
Sebastian Andrzej Siewior9f999282013-05-27 17:12:52 +0200238 cell->name = "TI-am335x-adc";
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300239 cell->of_compatible = "ti,am3359-adc";
240 cell->platform_data = &tscadc;
241 cell->pdata_size = sizeof(tscadc);
242 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530243
Patil, Rachna01636eb2012-10-16 12:55:43 +0530244 err = mfd_add_devices(&pdev->dev, pdev->id, tscadc->cells,
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300245 tscadc->used_cells, NULL, 0, NULL);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530246 if (err < 0)
247 goto err_disable_clk;
248
249 device_init_wakeup(&pdev->dev, true);
250 platform_set_drvdata(pdev, tscadc);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530251 return 0;
252
253err_disable_clk:
254 pm_runtime_put_sync(&pdev->dev);
255 pm_runtime_disable(&pdev->dev);
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530256ret:
Patil, Rachna01636eb2012-10-16 12:55:43 +0530257 return err;
258}
259
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800260static int ti_tscadc_remove(struct platform_device *pdev)
Patil, Rachna01636eb2012-10-16 12:55:43 +0530261{
262 struct ti_tscadc_dev *tscadc = platform_get_drvdata(pdev);
263
264 tscadc_writel(tscadc, REG_SE, 0x00);
265
266 pm_runtime_put_sync(&pdev->dev);
267 pm_runtime_disable(&pdev->dev);
268
269 mfd_remove_devices(tscadc->dev);
270
271 return 0;
272}
273
274#ifdef CONFIG_PM
275static int tscadc_suspend(struct device *dev)
276{
277 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
278
279 tscadc_writel(tscadc_dev, REG_SE, 0x00);
280 pm_runtime_put_sync(dev);
281
282 return 0;
283}
284
285static int tscadc_resume(struct device *dev)
286{
287 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
288 unsigned int restore, ctrl;
289
290 pm_runtime_get_sync(dev);
291
292 /* context restore */
293 ctrl = CNTRLREG_STEPCONFIGWRT | CNTRLREG_TSCENB |
294 CNTRLREG_STEPID | CNTRLREG_4WIRE;
295 tscadc_writel(tscadc_dev, REG_CTRL, ctrl);
296 tscadc_idle_config(tscadc_dev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000297 am335x_tsc_se_update(tscadc_dev);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530298 restore = tscadc_readl(tscadc_dev, REG_CTRL);
299 tscadc_writel(tscadc_dev, REG_CTRL,
300 (restore | CNTRLREG_TSCSSENB));
301
302 return 0;
303}
304
305static const struct dev_pm_ops tscadc_pm_ops = {
306 .suspend = tscadc_suspend,
307 .resume = tscadc_resume,
308};
309#define TSCADC_PM_OPS (&tscadc_pm_ops)
310#else
311#define TSCADC_PM_OPS NULL
312#endif
313
Patil, Rachnaa6543a12013-01-24 03:45:09 +0000314static const struct of_device_id ti_tscadc_dt_ids[] = {
315 { .compatible = "ti,am3359-tscadc", },
316 { }
317};
318MODULE_DEVICE_TABLE(of, ti_tscadc_dt_ids);
319
Patil, Rachna01636eb2012-10-16 12:55:43 +0530320static struct platform_driver ti_tscadc_driver = {
321 .driver = {
Patil, Rachnaa6543a12013-01-24 03:45:09 +0000322 .name = "ti_am3359-tscadc",
Patil, Rachna01636eb2012-10-16 12:55:43 +0530323 .owner = THIS_MODULE,
324 .pm = TSCADC_PM_OPS,
Patil, Rachnaa6543a12013-01-24 03:45:09 +0000325 .of_match_table = of_match_ptr(ti_tscadc_dt_ids),
Patil, Rachna01636eb2012-10-16 12:55:43 +0530326 },
327 .probe = ti_tscadc_probe,
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800328 .remove = ti_tscadc_remove,
Patil, Rachna01636eb2012-10-16 12:55:43 +0530329
330};
331
332module_platform_driver(ti_tscadc_driver);
333
334MODULE_DESCRIPTION("TI touchscreen / ADC MFD controller driver");
335MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
336MODULE_LICENSE("GPL");