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>
21 #include <linux/clk.h>
22 #include <linux/regmap.h>
23 #include <linux/mfd/core.h>
24 #include <linux/pm_runtime.h>
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>
30 static unsigned int tscadc_readl(struct ti_tscadc_dev *tsadc, unsigned int reg)
34 regmap_read(tsadc->regmap_tscadc, reg, &val);
38 static void tscadc_writel(struct ti_tscadc_dev *tsadc, unsigned int reg,
41 regmap_write(tsadc->regmap_tscadc, reg, val);
44 static const struct regmap_config tscadc_regmap_config = {
51 static void tscadc_idle_config(struct ti_tscadc_dev *config)
53 unsigned int idleconfig;
55 idleconfig = STEPCONFIG_YNN | STEPCONFIG_INM_ADCREFM |
56 STEPCONFIG_INP_ADCREFM | STEPCONFIG_YPN;
58 tscadc_writel(config, REG_IDLECONFIG, idleconfig);
61 static int __devinit ti_tscadc_probe(struct platform_device *pdev)
63 struct ti_tscadc_dev *tscadc;
66 struct mfd_tscadc_board *pdata = pdev->dev.platform_data;
67 struct mfd_cell *cell;
70 int clk_value, clock_rate;
71 int tsc_wires, adc_channels = 0, total_channels;
74 dev_err(&pdev->dev, "Could not find platform data\n");
79 adc_channels = pdata->adc_init->adc_channels;
81 tsc_wires = pdata->tsc_init->wires;
82 total_channels = tsc_wires + adc_channels;
84 if (total_channels > 8) {
85 dev_err(&pdev->dev, "Number of i/p channels more than 8\n");
89 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
91 dev_err(&pdev->dev, "no memory resource defined.\n");
95 irq = platform_get_irq(pdev, 0);
97 dev_err(&pdev->dev, "no irq ID is specified.\n");
101 /* Allocate memory for device */
102 tscadc = devm_kzalloc(&pdev->dev,
103 sizeof(struct ti_tscadc_dev), GFP_KERNEL);
105 dev_err(&pdev->dev, "failed to allocate memory.\n");
108 tscadc->dev = &pdev->dev;
111 res = devm_request_mem_region(&pdev->dev,
112 res->start, resource_size(res), pdev->name);
114 dev_err(&pdev->dev, "failed to reserve registers.\n");
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");
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);
135 pm_runtime_enable(&pdev->dev);
136 pm_runtime_get_sync(&pdev->dev);
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.
146 clk = clk_get(&pdev->dev, "adc_tsc_fck");
148 dev_err(&pdev->dev, "failed to get TSC fck\n");
150 goto err_disable_clk;
152 clock_rate = clk_get_rate(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");
158 goto err_disable_clk;
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);
164 /* Set the control register bits */
165 ctrl = CNTRLREG_STEPCONFIGWRT |
169 tscadc_writel(tscadc, REG_CTRL, ctrl);
171 /* Set register bits for Idle Config Mode */
172 tscadc_idle_config(tscadc);
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);
180 cell = &tscadc->cells[TSC_CELL];
182 cell->platform_data = tscadc;
183 cell->pdata_size = sizeof(*tscadc);
186 cell = &tscadc->cells[ADC_CELL];
187 cell->name = "tiadc";
188 cell->platform_data = tscadc;
189 cell->pdata_size = sizeof(*tscadc);
191 err = mfd_add_devices(&pdev->dev, pdev->id, tscadc->cells,
192 TSCADC_CELLS, NULL, 0, NULL);
194 goto err_disable_clk;
196 device_init_wakeup(&pdev->dev, true);
197 platform_set_drvdata(pdev, tscadc);
202 pm_runtime_put_sync(&pdev->dev);
203 pm_runtime_disable(&pdev->dev);
208 static int __devexit ti_tscadc_remove(struct platform_device *pdev)
210 struct ti_tscadc_dev *tscadc = platform_get_drvdata(pdev);
212 tscadc_writel(tscadc, REG_SE, 0x00);
214 pm_runtime_put_sync(&pdev->dev);
215 pm_runtime_disable(&pdev->dev);
217 mfd_remove_devices(tscadc->dev);
223 static int tscadc_suspend(struct device *dev)
225 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
227 tscadc_writel(tscadc_dev, REG_SE, 0x00);
228 pm_runtime_put_sync(dev);
233 static int tscadc_resume(struct device *dev)
235 struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
236 unsigned int restore, ctrl;
238 pm_runtime_get_sync(dev);
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));
253 static const struct dev_pm_ops tscadc_pm_ops = {
254 .suspend = tscadc_suspend,
255 .resume = tscadc_resume,
257 #define TSCADC_PM_OPS (&tscadc_pm_ops)
259 #define TSCADC_PM_OPS NULL
262 static struct platform_driver ti_tscadc_driver = {
265 .owner = THIS_MODULE,
268 .probe = ti_tscadc_probe,
269 .remove = __devexit_p(ti_tscadc_remove),
273 module_platform_driver(ti_tscadc_driver);
275 MODULE_DESCRIPTION("TI touchscreen / ADC MFD controller driver");
276 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
277 MODULE_LICENSE("GPL");