]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/mfd/ti_am335x_tscadc.c
Merge tag 'topic/domain' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[karo-tx-linux.git] / drivers / mfd / ti_am335x_tscadc.c
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>
25
26 #include <linux/mfd/ti_am335x_tscadc.h>
27 #include <linux/input/ti_am335x_tsc.h>
28 #include <linux/platform_data/ti_am335x_adc.h>
29
30 static 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
38 static 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
44 static 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
51 static void tscadc_idle_config(struct ti_tscadc_dev *config)
52 {
53         unsigned int idleconfig;
54
55         idleconfig = STEPCONFIG_YNN | STEPCONFIG_INM_ADCREFM |
56                         STEPCONFIG_INP_ADCREFM | STEPCONFIG_YPN;
57
58         tscadc_writel(config, REG_IDLECONFIG, idleconfig);
59 }
60
61 static  int __devinit ti_tscadc_probe(struct platform_device *pdev)
62 {
63         struct ti_tscadc_dev    *tscadc;
64         struct resource         *res;
65         struct clk              *clk;
66         struct mfd_tscadc_board *pdata = pdev->dev.platform_data;
67         struct mfd_cell         *cell;
68         int                     irq;
69         int                     err, ctrl;
70         int                     clk_value, clock_rate;
71         int                     tsc_wires, adc_channels = 0, total_channels;
72
73         if (!pdata) {
74                 dev_err(&pdev->dev, "Could not find platform data\n");
75                 return -EINVAL;
76         }
77
78         if (pdata->adc_init)
79                 adc_channels = pdata->adc_init->adc_channels;
80
81         tsc_wires = pdata->tsc_init->wires;
82         total_channels = tsc_wires + adc_channels;
83
84         if (total_channels > 8) {
85                 dev_err(&pdev->dev, "Number of i/p channels more than 8\n");
86                 return -EINVAL;
87         }
88
89         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
90         if (!res) {
91                 dev_err(&pdev->dev, "no memory resource defined.\n");
92                 return -EINVAL;
93         }
94
95         irq = platform_get_irq(pdev, 0);
96         if (irq < 0) {
97                 dev_err(&pdev->dev, "no irq ID is specified.\n");
98                 return -EINVAL;
99         }
100
101         /* Allocate memory for device */
102         tscadc = devm_kzalloc(&pdev->dev,
103                         sizeof(struct ti_tscadc_dev), GFP_KERNEL);
104         if (!tscadc) {
105                 dev_err(&pdev->dev, "failed to allocate memory.\n");
106                 return -ENOMEM;
107         }
108         tscadc->dev = &pdev->dev;
109         tscadc->irq = irq;
110
111         res = devm_request_mem_region(&pdev->dev,
112                         res->start, resource_size(res), pdev->name);
113         if (!res) {
114                 dev_err(&pdev->dev, "failed to reserve registers.\n");
115                 err = -EBUSY;
116                 goto err;
117         }
118
119         tscadc->tscadc_base = devm_ioremap(&pdev->dev,
120                         res->start, resource_size(res));
121         if (!tscadc->tscadc_base) {
122                 dev_err(&pdev->dev, "failed to map registers.\n");
123                 err = -ENOMEM;
124                 goto err;
125         }
126
127         tscadc->regmap_tscadc = devm_regmap_init_mmio(&pdev->dev,
128                         tscadc->tscadc_base, &tscadc_regmap_config);
129         if (IS_ERR(tscadc->regmap_tscadc)) {
130                 dev_err(&pdev->dev, "regmap init failed\n");
131                 err = PTR_ERR(tscadc->regmap_tscadc);
132                 goto err;
133         }
134
135         pm_runtime_enable(&pdev->dev);
136         pm_runtime_get_sync(&pdev->dev);
137
138         /*
139          * The TSC_ADC_Subsystem has 2 clock domains
140          * OCP_CLK and ADC_CLK.
141          * The ADC clock is expected to run at target of 3MHz,
142          * and expected to capture 12-bit data at a rate of 200 KSPS.
143          * The TSC_ADC_SS controller design assumes the OCP clock is
144          * at least 6x faster than the ADC clock.
145          */
146         clk = clk_get(&pdev->dev, "adc_tsc_fck");
147         if (IS_ERR(clk)) {
148                 dev_err(&pdev->dev, "failed to get TSC fck\n");
149                 err = PTR_ERR(clk);
150                 goto err_disable_clk;
151         }
152         clock_rate = clk_get_rate(clk);
153         clk_put(clk);
154         clk_value = clock_rate / ADC_CLK;
155         if (clk_value < MAX_CLK_DIV) {
156                 dev_err(&pdev->dev, "clock input less than min clock requirement\n");
157                 err = -EINVAL;
158                 goto err_disable_clk;
159         }
160         /* TSCADC_CLKDIV needs to be configured to the value minus 1 */
161         clk_value = clk_value - 1;
162         tscadc_writel(tscadc, REG_CLKDIV, clk_value);
163
164         /* Set the control register bits */
165         ctrl = CNTRLREG_STEPCONFIGWRT |
166                         CNTRLREG_TSCENB |
167                         CNTRLREG_STEPID |
168                         CNTRLREG_4WIRE;
169         tscadc_writel(tscadc, REG_CTRL, ctrl);
170
171         /* Set register bits for Idle Config Mode */
172         tscadc_idle_config(tscadc);
173
174         /* Enable the TSC module enable bit */
175         ctrl = tscadc_readl(tscadc, REG_CTRL);
176         ctrl |= CNTRLREG_TSCSSENB;
177         tscadc_writel(tscadc, REG_CTRL, ctrl);
178
179         /* TSC Cell */
180         cell = &tscadc->cells[TSC_CELL];
181         cell->name = "tsc";
182         cell->platform_data = tscadc;
183         cell->pdata_size = sizeof(*tscadc);
184
185         /* ADC Cell */
186         cell = &tscadc->cells[ADC_CELL];
187         cell->name = "tiadc";
188         cell->platform_data = tscadc;
189         cell->pdata_size = sizeof(*tscadc);
190
191         err = mfd_add_devices(&pdev->dev, pdev->id, tscadc->cells,
192                         TSCADC_CELLS, NULL, 0, NULL);
193         if (err < 0)
194                 goto err_disable_clk;
195
196         device_init_wakeup(&pdev->dev, true);
197         platform_set_drvdata(pdev, tscadc);
198
199         return 0;
200
201 err_disable_clk:
202         pm_runtime_put_sync(&pdev->dev);
203         pm_runtime_disable(&pdev->dev);
204 err:
205         return err;
206 }
207
208 static int __devexit ti_tscadc_remove(struct platform_device *pdev)
209 {
210         struct ti_tscadc_dev    *tscadc = platform_get_drvdata(pdev);
211
212         tscadc_writel(tscadc, REG_SE, 0x00);
213
214         pm_runtime_put_sync(&pdev->dev);
215         pm_runtime_disable(&pdev->dev);
216
217         mfd_remove_devices(tscadc->dev);
218
219         return 0;
220 }
221
222 #ifdef CONFIG_PM
223 static int tscadc_suspend(struct device *dev)
224 {
225         struct ti_tscadc_dev    *tscadc_dev = dev_get_drvdata(dev);
226
227         tscadc_writel(tscadc_dev, REG_SE, 0x00);
228         pm_runtime_put_sync(dev);
229
230         return 0;
231 }
232
233 static int tscadc_resume(struct device *dev)
234 {
235         struct ti_tscadc_dev    *tscadc_dev = dev_get_drvdata(dev);
236         unsigned int restore, ctrl;
237
238         pm_runtime_get_sync(dev);
239
240         /* context restore */
241         ctrl = CNTRLREG_STEPCONFIGWRT | CNTRLREG_TSCENB |
242                         CNTRLREG_STEPID | CNTRLREG_4WIRE;
243         tscadc_writel(tscadc_dev, REG_CTRL, ctrl);
244         tscadc_idle_config(tscadc_dev);
245         tscadc_writel(tscadc_dev, REG_SE, STPENB_STEPENB);
246         restore = tscadc_readl(tscadc_dev, REG_CTRL);
247         tscadc_writel(tscadc_dev, REG_CTRL,
248                         (restore | CNTRLREG_TSCSSENB));
249
250         return 0;
251 }
252
253 static const struct dev_pm_ops tscadc_pm_ops = {
254         .suspend = tscadc_suspend,
255         .resume = tscadc_resume,
256 };
257 #define TSCADC_PM_OPS (&tscadc_pm_ops)
258 #else
259 #define TSCADC_PM_OPS NULL
260 #endif
261
262 static struct platform_driver ti_tscadc_driver = {
263         .driver = {
264                 .name   = "ti_tscadc",
265                 .owner  = THIS_MODULE,
266                 .pm     = TSCADC_PM_OPS,
267         },
268         .probe  = ti_tscadc_probe,
269         .remove = __devexit_p(ti_tscadc_remove),
270
271 };
272
273 module_platform_driver(ti_tscadc_driver);
274
275 MODULE_DESCRIPTION("TI touchscreen / ADC MFD controller driver");
276 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
277 MODULE_LICENSE("GPL");