3 * TWL6030 MADC module driver-This driver only implements the ADC read
6 * Copyright (C) 2011 Samsung Telecommunications of America
8 * Based on twl4030-madc.c
9 * Copyright (C) 2008 Nokia Corporation
10 * Mikko Ylinen <mikko.k.ylinen@nokia.com>
12 * Amit Kucheria <amit.kucheria@canonical.com>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * version 2 as published by the Free Software Foundation.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
30 #include <linux/init.h>
31 #include <linux/device.h>
32 #include <linux/interrupt.h>
33 #include <linux/kernel.h>
34 #include <linux/delay.h>
35 #include <linux/platform_device.h>
36 #include <linux/slab.h>
37 #include <linux/i2c/twl.h>
38 #include <linux/i2c/twl6030-madc.h>
39 #include <linux/module.h>
40 #include <linux/stddef.h>
41 #include <linux/debugfs.h>
42 #include <linux/seq_file.h>
43 #include <linux/mutex.h>
44 #include <linux/bitops.h>
45 #include <linux/jiffies.h>
46 #include <linux/types.h>
47 #include <linux/gfp.h>
48 #include <linux/err.h>
49 #include <linux/wakelock.h>
51 #define GPADCS (1 << 1)
52 #define GPADCR (1 << 0)
53 #define REG_TOGGLE1 0x90
55 #define DRIVER_NAME (twl6030_madc_driver.driver.name)
56 static struct platform_driver twl6030_madc_driver;
59 * struct twl6030_madc_data - a container for madc info
60 * @dev - pointer to device structure for madc
61 * @lock - mutex protecting this data structure
63 struct twl6030_madc_data {
67 struct wake_lock wakelock;
70 static struct twl6030_madc_data *twl6030_madc;
71 static u8 gpadc_ctrl_reg;
73 static inline int twl6030_madc_start_conversion(struct twl6030_madc_data *madc)
77 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, GPADCS, REG_TOGGLE1);
79 dev_err(madc->dev, "unable to write register 0x%X\n",
85 ret = twl_i2c_write_u8(TWL_MODULE_MADC, TWL6030_MADC_SP1,
86 TWL6030_MADC_CTRL_P1);
88 dev_err(madc->dev, "unable to write register 0x%X\n",
89 TWL6030_MADC_CTRL_P1);
96 * Function that waits for conversion to be ready
97 * @madc - pointer to twl4030_madc_data struct
98 * @timeout_ms - timeout value in milliseconds
99 * @status_reg - ctrl register
100 * returns 0 if succeeds else a negative error value
102 static int twl6030_madc_wait_conversion_ready(struct twl6030_madc_data *madc,
103 unsigned int timeout_ms,
106 unsigned long timeout;
111 delta = msecs_to_jiffies(timeout_ms);
116 wake_lock(&madc->wakelock);
117 timeout = jiffies + delta;
119 ret = twl_i2c_read_u8(TWL6030_MODULE_MADC, ®, status_reg);
122 "unable to read status register 0x%X\n",
126 if (!(reg & TWL6030_MADC_BUSY) && (reg & TWL6030_MADC_EOCP1)) {
131 if (time_after(jiffies, timeout))
134 usleep_range(500, 2000);
137 dev_err(madc->dev, "conversion timeout, ctrl_px=0x%08x\n", reg);
141 wake_unlock(&madc->wakelock);
146 * Function to read a particular channel value.
147 * @madc - pointer to struct twl6030_madc_data
148 * @reg - lsb of ADC Channel
149 * If the i2c read fails it returns an error else returns 0.
151 static int twl6030_madc_channel_raw_read(struct twl6030_madc_data *madc,
157 mutex_lock(&madc->lock);
158 ret = twl6030_madc_start_conversion(twl6030_madc);
162 ret = twl6030_madc_wait_conversion_ready(twl6030_madc, 5,
163 TWL6030_MADC_CTRL_P1);
168 * For each ADC channel, we have MSB and LSB register
169 * pair. MSB address is always LSB address+1. reg parameter is
170 * the address of LSB register
172 ret = twl_i2c_read_u8(TWL6030_MODULE_MADC, &msb, reg + 1);
174 dev_err(madc->dev, "unable to read MSB register 0x%X\n",
178 ret = twl_i2c_read_u8(TWL6030_MODULE_MADC, &lsb, reg);
180 dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
183 ret = (int)((msb << 8) | lsb);
185 /* Disable GPADC for power savings. */
186 twl_i2c_write_u8(TWL6030_MODULE_ID1, GPADCR, REG_TOGGLE1);
187 mutex_unlock(&madc->lock);
192 * Return channel value
195 int twl6030_get_madc_conversion(int channel_no)
197 u8 reg = TWL6030_MADC_GPCH0_LSB + (2 * channel_no);
199 pr_err("%s: No ADC device\n", __func__);
202 if (channel_no >= TWL6030_MADC_MAX_CHANNELS) {
203 dev_err(twl6030_madc->dev,
204 "%s: Channel number (%d) exceeds max (%d)\n",
205 __func__, channel_no, TWL6030_MADC_MAX_CHANNELS);
209 return twl6030_madc_channel_raw_read(twl6030_madc, reg);
211 EXPORT_SYMBOL_GPL(twl6030_get_madc_conversion);
213 #ifdef CONFIG_DEBUG_FS
215 static int debug_twl6030_madc_show(struct seq_file *s, void *_)
218 for (i = 0; i < TWL6030_MADC_MAX_CHANNELS; i++) {
219 result = twl6030_get_madc_conversion(i);
220 seq_printf(s, "channel %3d returns result %d\n",
226 static int debug_twl6030_madc_open(struct inode *inode, struct file *file)
228 return single_open(file, debug_twl6030_madc_show, inode->i_private);
231 static const struct file_operations debug_fops = {
232 .open = debug_twl6030_madc_open,
235 .release = single_release,
238 #define DEBUG_FOPS (&debug_fops)
241 #define DEBUG_FOPS NULL
247 static int __devinit twl6030_madc_probe(struct platform_device *pdev)
249 struct twl6030_madc_data *madc;
250 struct twl4030_madc_platform_data *pdata = pdev->dev.platform_data;
253 dev_err(&pdev->dev, "platform_data not available\n");
256 madc = kzalloc(sizeof(*madc), GFP_KERNEL);
260 platform_set_drvdata(pdev, madc);
261 madc->dev = &pdev->dev;
262 mutex_init(&madc->lock);
263 madc->file = debugfs_create_file(DRIVER_NAME, S_IRUGO, NULL,
265 wake_lock_init(&madc->wakelock, WAKE_LOCK_SUSPEND, "twl6030 adc");
270 static int __devexit twl6030_madc_remove(struct platform_device *pdev)
272 struct twl6030_madc_data *madc = platform_get_drvdata(pdev);
274 wake_lock_destroy(&madc->wakelock);
275 mutex_destroy(&madc->lock);
276 free_irq(platform_get_irq(pdev, 0), madc);
277 platform_set_drvdata(pdev, NULL);
279 debugfs_remove(madc->file);
285 static int twl6030_madc_suspend(struct device *pdev)
290 ret = twl_i2c_read_u8(TWL_MODULE_MADC, ®_val, TWL6030_MADC_CTRL);
292 reg_val &= ~(TWL6030_MADC_TEMP1_EN);
293 ret = twl_i2c_write_u8(TWL_MODULE_MADC, reg_val,
298 dev_err(twl6030_madc->dev, "unable to disable madc temp1!\n");
299 gpadc_ctrl_reg = TWL6030_MADC_TEMP1_EN;
301 gpadc_ctrl_reg = reg_val;
306 static int twl6030_madc_resume(struct device *pdev)
310 if (!(gpadc_ctrl_reg & TWL6030_MADC_TEMP1_EN)) {
311 gpadc_ctrl_reg |= TWL6030_MADC_TEMP1_EN;
312 ret = twl_i2c_write_u8(TWL_MODULE_MADC, gpadc_ctrl_reg,
315 dev_err(twl6030_madc->dev,
316 "unable to enable madc temp1!\n");
322 static const struct dev_pm_ops twl6030_madc_pm_ops = {
323 .suspend = twl6030_madc_suspend,
324 .resume = twl6030_madc_resume,
327 static struct platform_driver twl6030_madc_driver = {
328 .probe = twl6030_madc_probe,
329 .remove = __exit_p(twl6030_madc_remove),
331 .name = "twl6030_madc",
332 .owner = THIS_MODULE,
333 .pm = &twl6030_madc_pm_ops,
337 static int __init twl6030_madc_init(void)
339 return platform_driver_register(&twl6030_madc_driver);
342 module_init(twl6030_madc_init);
344 static void __exit twl6030_madc_exit(void)
346 platform_driver_unregister(&twl6030_madc_driver);
349 module_exit(twl6030_madc_exit);
351 MODULE_DESCRIPTION("TWL6030 ADC driver");
352 MODULE_LICENSE("GPL");
353 MODULE_AUTHOR("J Keerthy");
354 MODULE_ALIAS("platform:twl6030_madc");