blob: 2532339150096ced65f27e19e7db264fd89be6ed [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;
Patil, Rachna01636eb2012-10-16 12:55:43 +053094 int err, ctrl;
95 int clk_value, clock_rate;
Patil, Rachnaa6543a12013-01-24 03:45:09 +000096 int tsc_wires = 0, adc_channels = 0, total_channels;
Patil, Rachna01636eb2012-10-16 12:55:43 +053097
Sebastian Andrzej Siewior9e5775f2013-05-21 17:56:49 +020098 if (!pdev->dev.of_node) {
99 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna01636eb2012-10-16 12:55:43 +0530100 return -EINVAL;
101 }
102
Sebastian Andrzej Siewior9e5775f2013-05-21 17:56:49 +0200103 node = of_get_child_by_name(pdev->dev.of_node, "tsc");
104 of_property_read_u32(node, "ti,wires", &tsc_wires);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530105
Sebastian Andrzej Siewior9e5775f2013-05-21 17:56:49 +0200106 node = of_get_child_by_name(pdev->dev.of_node, "adc");
107 of_property_read_u32(node, "ti,adc-channels", &adc_channels);
Patil, Rachnaa6543a12013-01-24 03:45:09 +0000108
Patil, Rachna5e53a692012-10-16 12:55:45 +0530109 total_channels = tsc_wires + adc_channels;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530110 if (total_channels > 8) {
111 dev_err(&pdev->dev, "Number of i/p channels more than 8\n");
112 return -EINVAL;
113 }
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300114 if (total_channels == 0) {
115 dev_err(&pdev->dev, "Need atleast one channel.\n");
116 return -EINVAL;
117 }
Patil, Rachna2b99baf2012-10-16 12:55:44 +0530118
Patil, Rachna01636eb2012-10-16 12:55:43 +0530119 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
120 if (!res) {
121 dev_err(&pdev->dev, "no memory resource defined.\n");
122 return -EINVAL;
123 }
124
Patil, Rachna01636eb2012-10-16 12:55:43 +0530125 /* Allocate memory for device */
126 tscadc = devm_kzalloc(&pdev->dev,
127 sizeof(struct ti_tscadc_dev), GFP_KERNEL);
128 if (!tscadc) {
129 dev_err(&pdev->dev, "failed to allocate memory.\n");
130 return -ENOMEM;
131 }
132 tscadc->dev = &pdev->dev;
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530133
134 err = platform_get_irq(pdev, 0);
135 if (err < 0) {
136 dev_err(&pdev->dev, "no irq ID is specified.\n");
137 goto ret;
138 } else
139 tscadc->irq = err;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530140
141 res = devm_request_mem_region(&pdev->dev,
142 res->start, resource_size(res), pdev->name);
143 if (!res) {
144 dev_err(&pdev->dev, "failed to reserve registers.\n");
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530145 return -EBUSY;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530146 }
147
148 tscadc->tscadc_base = devm_ioremap(&pdev->dev,
149 res->start, resource_size(res));
150 if (!tscadc->tscadc_base) {
151 dev_err(&pdev->dev, "failed to map registers.\n");
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530152 return -ENOMEM;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530153 }
154
155 tscadc->regmap_tscadc = devm_regmap_init_mmio(&pdev->dev,
156 tscadc->tscadc_base, &tscadc_regmap_config);
157 if (IS_ERR(tscadc->regmap_tscadc)) {
158 dev_err(&pdev->dev, "regmap init failed\n");
159 err = PTR_ERR(tscadc->regmap_tscadc);
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530160 goto ret;
Patil, Rachna01636eb2012-10-16 12:55:43 +0530161 }
162
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000163 spin_lock_init(&tscadc->reg_lock);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530164 pm_runtime_enable(&pdev->dev);
165 pm_runtime_get_sync(&pdev->dev);
166
167 /*
168 * The TSC_ADC_Subsystem has 2 clock domains
169 * OCP_CLK and ADC_CLK.
170 * The ADC clock is expected to run at target of 3MHz,
171 * and expected to capture 12-bit data at a rate of 200 KSPS.
172 * The TSC_ADC_SS controller design assumes the OCP clock is
173 * at least 6x faster than the ADC clock.
174 */
175 clk = clk_get(&pdev->dev, "adc_tsc_fck");
176 if (IS_ERR(clk)) {
177 dev_err(&pdev->dev, "failed to get TSC fck\n");
178 err = PTR_ERR(clk);
179 goto err_disable_clk;
180 }
181 clock_rate = clk_get_rate(clk);
182 clk_put(clk);
183 clk_value = clock_rate / ADC_CLK;
184 if (clk_value < MAX_CLK_DIV) {
185 dev_err(&pdev->dev, "clock input less than min clock requirement\n");
186 err = -EINVAL;
187 goto err_disable_clk;
188 }
189 /* TSCADC_CLKDIV needs to be configured to the value minus 1 */
190 clk_value = clk_value - 1;
191 tscadc_writel(tscadc, REG_CLKDIV, clk_value);
192
193 /* Set the control register bits */
194 ctrl = CNTRLREG_STEPCONFIGWRT |
195 CNTRLREG_TSCENB |
196 CNTRLREG_STEPID |
197 CNTRLREG_4WIRE;
198 tscadc_writel(tscadc, REG_CTRL, ctrl);
199
200 /* Set register bits for Idle Config Mode */
201 tscadc_idle_config(tscadc);
202
203 /* Enable the TSC module enable bit */
204 ctrl = tscadc_readl(tscadc, REG_CTRL);
205 ctrl |= CNTRLREG_TSCSSENB;
206 tscadc_writel(tscadc, REG_CTRL, ctrl);
207
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300208 tscadc->used_cells = 0;
209 tscadc->tsc_cell = -1;
210 tscadc->adc_cell = -1;
211
Patil, Rachna2b99baf2012-10-16 12:55:44 +0530212 /* TSC Cell */
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300213 if (tsc_wires > 0) {
214 tscadc->tsc_cell = tscadc->used_cells;
215 cell = &tscadc->cells[tscadc->used_cells++];
Sebastian Andrzej Siewior5f184e62013-05-27 17:08:28 +0200216 cell->name = "TI-am335x-tsc";
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300217 cell->of_compatible = "ti,am3359-tsc";
218 cell->platform_data = &tscadc;
219 cell->pdata_size = sizeof(tscadc);
220 }
Patil, Rachna2b99baf2012-10-16 12:55:44 +0530221
Patil, Rachna5e53a692012-10-16 12:55:45 +0530222 /* ADC Cell */
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300223 if (adc_channels > 0) {
224 tscadc->adc_cell = tscadc->used_cells;
225 cell = &tscadc->cells[tscadc->used_cells++];
Sebastian Andrzej Siewior9f999282013-05-27 17:12:52 +0200226 cell->name = "TI-am335x-adc";
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300227 cell->of_compatible = "ti,am3359-adc";
228 cell->platform_data = &tscadc;
229 cell->pdata_size = sizeof(tscadc);
230 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530231
Patil, Rachna01636eb2012-10-16 12:55:43 +0530232 err = mfd_add_devices(&pdev->dev, pdev->id, tscadc->cells,
Pantelis Antoniou24d5c822012-10-13 16:37:24 +0300233 tscadc->used_cells, NULL, 0, NULL);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530234 if (err < 0)
235 goto err_disable_clk;
236
237 device_init_wakeup(&pdev->dev, true);
238 platform_set_drvdata(pdev, tscadc);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530239 return 0;
240
241err_disable_clk:
242 pm_runtime_put_sync(&pdev->dev);
243 pm_runtime_disable(&pdev->dev);
Patil, Rachna3c39c9c2012-11-06 13:39:03 +0530244ret:
Patil, Rachna01636eb2012-10-16 12:55:43 +0530245 return err;
246}
247
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800248static int ti_tscadc_remove(struct platform_device *pdev)
Patil, Rachna01636eb2012-10-16 12:55:43 +0530249{
250 struct ti_tscadc_dev *tscadc = platform_get_drvdata(pdev);
251
252 tscadc_writel(tscadc, REG_SE, 0x00);
253
254 pm_runtime_put_sync(&pdev->dev);
255 pm_runtime_disable(&pdev->dev);
256
257 mfd_remove_devices(tscadc->dev);
258
259 return 0;
260}
261
262#ifdef CONFIG_PM
263static int tscadc_suspend(struct device *dev)
264{
265 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
266
267 tscadc_writel(tscadc_dev, REG_SE, 0x00);
268 pm_runtime_put_sync(dev);
269
270 return 0;
271}
272
273static int tscadc_resume(struct device *dev)
274{
275 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
276 unsigned int restore, ctrl;
277
278 pm_runtime_get_sync(dev);
279
280 /* context restore */
281 ctrl = CNTRLREG_STEPCONFIGWRT | CNTRLREG_TSCENB |
282 CNTRLREG_STEPID | CNTRLREG_4WIRE;
283 tscadc_writel(tscadc_dev, REG_CTRL, ctrl);
284 tscadc_idle_config(tscadc_dev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000285 am335x_tsc_se_update(tscadc_dev);
Patil, Rachna01636eb2012-10-16 12:55:43 +0530286 restore = tscadc_readl(tscadc_dev, REG_CTRL);
287 tscadc_writel(tscadc_dev, REG_CTRL,
288 (restore | CNTRLREG_TSCSSENB));
289
290 return 0;
291}
292
293static const struct dev_pm_ops tscadc_pm_ops = {
294 .suspend = tscadc_suspend,
295 .resume = tscadc_resume,
296};
297#define TSCADC_PM_OPS (&tscadc_pm_ops)
298#else
299#define TSCADC_PM_OPS NULL
300#endif
301
Patil, Rachnaa6543a12013-01-24 03:45:09 +0000302static const struct of_device_id ti_tscadc_dt_ids[] = {
303 { .compatible = "ti,am3359-tscadc", },
304 { }
305};
306MODULE_DEVICE_TABLE(of, ti_tscadc_dt_ids);
307
Patil, Rachna01636eb2012-10-16 12:55:43 +0530308static struct platform_driver ti_tscadc_driver = {
309 .driver = {
Patil, Rachnaa6543a12013-01-24 03:45:09 +0000310 .name = "ti_am3359-tscadc",
Patil, Rachna01636eb2012-10-16 12:55:43 +0530311 .owner = THIS_MODULE,
312 .pm = TSCADC_PM_OPS,
Patil, Rachnaa6543a12013-01-24 03:45:09 +0000313 .of_match_table = of_match_ptr(ti_tscadc_dt_ids),
Patil, Rachna01636eb2012-10-16 12:55:43 +0530314 },
315 .probe = ti_tscadc_probe,
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800316 .remove = ti_tscadc_remove,
Patil, Rachna01636eb2012-10-16 12:55:43 +0530317
318};
319
320module_platform_driver(ti_tscadc_driver);
321
322MODULE_DESCRIPTION("TI touchscreen / ADC MFD controller driver");
323MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
324MODULE_LICENSE("GPL");