Linux 3.11-rc3
[cris-mirror.git] / drivers / mfd / ti_am335x_tscadc.c
blobb003a16ba227384d8deddb861d89515a0f29b525
1 /*
2 * TI Touch Screen / ADC MFD driver
4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
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.
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.
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 #include <linux/of.h>
26 #include <linux/of_device.h>
28 #include <linux/mfd/ti_am335x_tscadc.h>
30 static unsigned int tscadc_readl(struct ti_tscadc_dev *tsadc, unsigned int reg)
32 unsigned int val;
34 regmap_read(tsadc->regmap_tscadc, reg, &val);
35 return val;
38 static void tscadc_writel(struct ti_tscadc_dev *tsadc, unsigned int reg,
39 unsigned int val)
41 regmap_write(tsadc->regmap_tscadc, reg, val);
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,
51 void am335x_tsc_se_update(struct ti_tscadc_dev *tsadc)
53 tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);
55 EXPORT_SYMBOL_GPL(am335x_tsc_se_update);
57 void am335x_tsc_se_set(struct ti_tscadc_dev *tsadc, u32 val)
59 spin_lock(&tsadc->reg_lock);
60 tsadc->reg_se_cache |= val;
61 spin_unlock(&tsadc->reg_lock);
63 am335x_tsc_se_update(tsadc);
65 EXPORT_SYMBOL_GPL(am335x_tsc_se_set);
67 void am335x_tsc_se_clr(struct ti_tscadc_dev *tsadc, u32 val)
69 spin_lock(&tsadc->reg_lock);
70 tsadc->reg_se_cache &= ~val;
71 spin_unlock(&tsadc->reg_lock);
73 am335x_tsc_se_update(tsadc);
75 EXPORT_SYMBOL_GPL(am335x_tsc_se_clr);
77 static void tscadc_idle_config(struct ti_tscadc_dev *config)
79 unsigned int idleconfig;
81 idleconfig = STEPCONFIG_YNN | STEPCONFIG_INM_ADCREFM |
82 STEPCONFIG_INP_ADCREFM | STEPCONFIG_YPN;
84 tscadc_writel(config, REG_IDLECONFIG, idleconfig);
87 static int ti_tscadc_probe(struct platform_device *pdev)
89 struct ti_tscadc_dev *tscadc;
90 struct resource *res;
91 struct clk *clk;
92 struct device_node *node = pdev->dev.of_node;
93 struct mfd_cell *cell;
94 struct property *prop;
95 const __be32 *cur;
96 u32 val;
97 int err, ctrl;
98 int clk_value, clock_rate;
99 int tsc_wires = 0, adc_channels = 0, total_channels;
100 int readouts = 0;
102 if (!pdev->dev.of_node) {
103 dev_err(&pdev->dev, "Could not find valid DT data.\n");
104 return -EINVAL;
107 node = of_get_child_by_name(pdev->dev.of_node, "tsc");
108 of_property_read_u32(node, "ti,wires", &tsc_wires);
109 of_property_read_u32(node, "ti,coordiante-readouts", &readouts);
111 node = of_get_child_by_name(pdev->dev.of_node, "adc");
112 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;
120 total_channels = tsc_wires + adc_channels;
121 if (total_channels > 8) {
122 dev_err(&pdev->dev, "Number of i/p channels more than 8\n");
123 return -EINVAL;
125 if (total_channels == 0) {
126 dev_err(&pdev->dev, "Need atleast one channel.\n");
127 return -EINVAL;
130 if (readouts * 2 + 2 + adc_channels > 16) {
131 dev_err(&pdev->dev, "Too many step configurations requested\n");
132 return -EINVAL;
135 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
136 if (!res) {
137 dev_err(&pdev->dev, "no memory resource defined.\n");
138 return -EINVAL;
141 /* 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;
148 tscadc->dev = &pdev->dev;
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;
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");
161 return -EBUSY;
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");
168 return -ENOMEM;
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);
176 goto ret;
179 spin_lock_init(&tscadc->reg_lock);
180 pm_runtime_enable(&pdev->dev);
181 pm_runtime_get_sync(&pdev->dev);
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.
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;
197 clock_rate = clk_get_rate(clk);
198 clk_put(clk);
199 clk_value = clock_rate / ADC_CLK;
200 if (clk_value < MAX_CLK_DIV) {
201 dev_err(&pdev->dev, "clock input less than min clock requirement\n");
202 err = -EINVAL;
203 goto err_disable_clk;
205 /* TSCADC_CLKDIV needs to be configured to the value minus 1 */
206 clk_value = clk_value - 1;
207 tscadc_writel(tscadc, REG_CLKDIV, clk_value);
209 /* Set the control register bits */
210 ctrl = CNTRLREG_STEPCONFIGWRT |
211 CNTRLREG_TSCENB |
212 CNTRLREG_STEPID |
213 CNTRLREG_4WIRE;
214 tscadc_writel(tscadc, REG_CTRL, ctrl);
216 /* Set register bits for Idle Config Mode */
217 tscadc_idle_config(tscadc);
219 /* Enable the TSC module enable bit */
220 ctrl = tscadc_readl(tscadc, REG_CTRL);
221 ctrl |= CNTRLREG_TSCSSENB;
222 tscadc_writel(tscadc, REG_CTRL, ctrl);
224 tscadc->used_cells = 0;
225 tscadc->tsc_cell = -1;
226 tscadc->adc_cell = -1;
228 /* TSC Cell */
229 if (tsc_wires > 0) {
230 tscadc->tsc_cell = tscadc->used_cells;
231 cell = &tscadc->cells[tscadc->used_cells++];
232 cell->name = "TI-am335x-tsc";
233 cell->of_compatible = "ti,am3359-tsc";
234 cell->platform_data = &tscadc;
235 cell->pdata_size = sizeof(tscadc);
238 /* ADC Cell */
239 if (adc_channels > 0) {
240 tscadc->adc_cell = tscadc->used_cells;
241 cell = &tscadc->cells[tscadc->used_cells++];
242 cell->name = "TI-am335x-adc";
243 cell->of_compatible = "ti,am3359-adc";
244 cell->platform_data = &tscadc;
245 cell->pdata_size = sizeof(tscadc);
248 err = mfd_add_devices(&pdev->dev, pdev->id, tscadc->cells,
249 tscadc->used_cells, NULL, 0, NULL);
250 if (err < 0)
251 goto err_disable_clk;
253 device_init_wakeup(&pdev->dev, true);
254 platform_set_drvdata(pdev, tscadc);
255 return 0;
257 err_disable_clk:
258 pm_runtime_put_sync(&pdev->dev);
259 pm_runtime_disable(&pdev->dev);
260 ret:
261 return err;
264 static int ti_tscadc_remove(struct platform_device *pdev)
266 struct ti_tscadc_dev *tscadc = platform_get_drvdata(pdev);
268 tscadc_writel(tscadc, REG_SE, 0x00);
270 pm_runtime_put_sync(&pdev->dev);
271 pm_runtime_disable(&pdev->dev);
273 mfd_remove_devices(tscadc->dev);
275 return 0;
278 #ifdef CONFIG_PM
279 static int tscadc_suspend(struct device *dev)
281 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
283 tscadc_writel(tscadc_dev, REG_SE, 0x00);
284 pm_runtime_put_sync(dev);
286 return 0;
289 static int tscadc_resume(struct device *dev)
291 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
292 unsigned int restore, ctrl;
294 pm_runtime_get_sync(dev);
296 /* context restore */
297 ctrl = CNTRLREG_STEPCONFIGWRT | CNTRLREG_TSCENB |
298 CNTRLREG_STEPID | CNTRLREG_4WIRE;
299 tscadc_writel(tscadc_dev, REG_CTRL, ctrl);
300 tscadc_idle_config(tscadc_dev);
301 am335x_tsc_se_update(tscadc_dev);
302 restore = tscadc_readl(tscadc_dev, REG_CTRL);
303 tscadc_writel(tscadc_dev, REG_CTRL,
304 (restore | CNTRLREG_TSCSSENB));
306 return 0;
309 static const struct dev_pm_ops tscadc_pm_ops = {
310 .suspend = tscadc_suspend,
311 .resume = tscadc_resume,
313 #define TSCADC_PM_OPS (&tscadc_pm_ops)
314 #else
315 #define TSCADC_PM_OPS NULL
316 #endif
318 static const struct of_device_id ti_tscadc_dt_ids[] = {
319 { .compatible = "ti,am3359-tscadc", },
322 MODULE_DEVICE_TABLE(of, ti_tscadc_dt_ids);
324 static struct platform_driver ti_tscadc_driver = {
325 .driver = {
326 .name = "ti_am3359-tscadc",
327 .owner = THIS_MODULE,
328 .pm = TSCADC_PM_OPS,
329 .of_match_table = of_match_ptr(ti_tscadc_dt_ids),
331 .probe = ti_tscadc_probe,
332 .remove = ti_tscadc_remove,
336 module_platform_driver(ti_tscadc_driver);
338 MODULE_DESCRIPTION("TI touchscreen / ADC MFD controller driver");
339 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
340 MODULE_LICENSE("GPL");