mfd: fusb302: change to host when connect type-c to standard-a cable
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / twl6030-madc.c
1 /*
2  *
3  * TWL6030 MADC module driver-This driver only implements the ADC read
4  * functions
5  *
6  * Copyright (C) 2011 Samsung Telecommunications of America
7  *
8  * Based on twl4030-madc.c
9  * Copyright (C) 2008 Nokia Corporation
10  * Mikko Ylinen <mikko.k.ylinen@nokia.com>
11  *
12  * Amit Kucheria <amit.kucheria@canonical.com>
13  *
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.
17  *
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.
22  *
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
26  * 02110-1301 USA
27  *
28  */
29
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>
50
51 #define GPADCS          (1 << 1)
52 #define GPADCR          (1 << 0)
53 #define REG_TOGGLE1     0x90
54
55 #define DRIVER_NAME     (twl6030_madc_driver.driver.name)
56 static struct platform_driver twl6030_madc_driver;
57
58 /*
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
62  */
63 struct twl6030_madc_data {
64         struct device *dev;
65         struct mutex lock;
66         struct dentry           *file;
67         struct wake_lock wakelock;
68 };
69
70 static struct twl6030_madc_data *twl6030_madc;
71 static u8 gpadc_ctrl_reg;
72
73 static inline int twl6030_madc_start_conversion(struct twl6030_madc_data *madc)
74 {
75         int ret;
76
77         ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, GPADCS, REG_TOGGLE1);
78         if (ret) {
79                 dev_err(madc->dev, "unable to write register 0x%X\n",
80                         REG_TOGGLE1);
81                 return ret;
82         }
83
84         udelay(100);
85         ret = twl_i2c_write_u8(TWL_MODULE_MADC, TWL6030_MADC_SP1,
86                                 TWL6030_MADC_CTRL_P1);
87         if (ret) {
88                 dev_err(madc->dev, "unable to write register 0x%X\n",
89                         TWL6030_MADC_CTRL_P1);
90                 return ret;
91         }
92         return 0;
93 }
94
95 /*
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
101  */
102 static int twl6030_madc_wait_conversion_ready(struct twl6030_madc_data *madc,
103                                               unsigned int timeout_ms,
104                                               u8 status_reg)
105 {
106         unsigned long timeout;
107         unsigned long delta;
108         u8 reg;
109         int ret;
110
111         delta = msecs_to_jiffies(timeout_ms);
112
113         if (delta < 2)
114                 delta = 2;
115
116         wake_lock(&madc->wakelock);
117         timeout = jiffies + delta;
118         do {
119                 ret = twl_i2c_read_u8(TWL6030_MODULE_MADC, &reg, status_reg);
120                 if (ret) {
121                         dev_err(madc->dev,
122                                 "unable to read status register 0x%X\n",
123                                 status_reg);
124                         goto unlock;
125                 }
126                 if (!(reg & TWL6030_MADC_BUSY) && (reg & TWL6030_MADC_EOCP1)) {
127                         ret = 0;
128                         goto unlock;
129                 }
130
131                 if (time_after(jiffies, timeout))
132                         break;
133
134                 usleep_range(500, 2000);
135         } while (1);
136
137         dev_err(madc->dev, "conversion timeout, ctrl_px=0x%08x\n", reg);
138         ret = -EAGAIN;
139
140 unlock:
141         wake_unlock(&madc->wakelock);
142         return ret;
143 }
144
145 /*
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.
150  */
151 static int twl6030_madc_channel_raw_read(struct twl6030_madc_data *madc,
152                                         u8 reg)
153 {
154         u8 msb, lsb;
155         int ret;
156
157         mutex_lock(&madc->lock);
158         ret = twl6030_madc_start_conversion(twl6030_madc);
159         if (ret)
160                 goto unlock;
161
162         ret = twl6030_madc_wait_conversion_ready(twl6030_madc, 5,
163                                                 TWL6030_MADC_CTRL_P1);
164         if (ret)
165                 goto unlock;
166
167         /*
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
171          */
172         ret = twl_i2c_read_u8(TWL6030_MODULE_MADC, &msb, reg + 1);
173         if (ret) {
174                 dev_err(madc->dev, "unable to read MSB register 0x%X\n",
175                         reg + 1);
176                 goto unlock;
177         }
178         ret = twl_i2c_read_u8(TWL6030_MODULE_MADC, &lsb, reg);
179         if (ret) {
180                 dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
181                 goto unlock;
182         }
183         ret = (int)((msb << 8) | lsb);
184 unlock:
185         /* Disable GPADC for power savings. */
186         twl_i2c_write_u8(TWL6030_MODULE_ID1, GPADCR, REG_TOGGLE1);
187         mutex_unlock(&madc->lock);
188         return ret;
189 }
190
191 /*
192  * Return channel value
193  * Or < 0 on failure.
194  */
195 int twl6030_get_madc_conversion(int channel_no)
196 {
197         u8 reg = TWL6030_MADC_GPCH0_LSB + (2 * channel_no);
198         if (!twl6030_madc) {
199                 pr_err("%s: No ADC device\n", __func__);
200                 return -EINVAL;
201         }
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);
206                 return -EINVAL;
207         }
208
209         return twl6030_madc_channel_raw_read(twl6030_madc, reg);
210 }
211 EXPORT_SYMBOL_GPL(twl6030_get_madc_conversion);
212
213 #ifdef CONFIG_DEBUG_FS
214
215 static int debug_twl6030_madc_show(struct seq_file *s, void *_)
216 {
217         int i, result;
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",
221                         i, result);
222         }
223         return 0;
224 }
225
226 static int debug_twl6030_madc_open(struct inode *inode, struct file *file)
227 {
228         return single_open(file, debug_twl6030_madc_show, inode->i_private);
229 }
230
231 static const struct file_operations debug_fops = {
232         .open           = debug_twl6030_madc_open,
233         .read           = seq_read,
234         .llseek         = seq_lseek,
235         .release        = single_release,
236 };
237
238 #define DEBUG_FOPS      (&debug_fops)
239
240 #else
241 #define DEBUG_FOPS      NULL
242 #endif
243
244 /*
245  * Initialize MADC
246  */
247 static int __devinit twl6030_madc_probe(struct platform_device *pdev)
248 {
249         struct twl6030_madc_data *madc;
250         struct twl4030_madc_platform_data *pdata = pdev->dev.platform_data;
251
252         if (!pdata) {
253                 dev_err(&pdev->dev, "platform_data not available\n");
254                 return -EINVAL;
255         }
256         madc = kzalloc(sizeof(*madc), GFP_KERNEL);
257         if (!madc)
258                 return -ENOMEM;
259
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,
264                                         madc, DEBUG_FOPS);
265         wake_lock_init(&madc->wakelock, WAKE_LOCK_SUSPEND, "twl6030 adc");
266         twl6030_madc = madc;
267         return 0;
268 }
269
270 static int __devexit twl6030_madc_remove(struct platform_device *pdev)
271 {
272         struct twl6030_madc_data *madc = platform_get_drvdata(pdev);
273
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);
278         twl6030_madc = NULL;
279         debugfs_remove(madc->file);
280         kfree(madc);
281
282         return 0;
283 }
284
285 static int twl6030_madc_suspend(struct device *pdev)
286 {
287         int ret;
288         u8 reg_val;
289
290         ret = twl_i2c_read_u8(TWL_MODULE_MADC, &reg_val, TWL6030_MADC_CTRL);
291         if (!ret) {
292                 reg_val &= ~(TWL6030_MADC_TEMP1_EN);
293                 ret = twl_i2c_write_u8(TWL_MODULE_MADC, reg_val,
294                                         TWL6030_MADC_CTRL);
295         }
296
297         if (ret) {
298                 dev_err(twl6030_madc->dev, "unable to disable madc temp1!\n");
299                 gpadc_ctrl_reg = TWL6030_MADC_TEMP1_EN;
300         } else
301                 gpadc_ctrl_reg = reg_val;
302
303         return 0;
304 };
305
306 static int twl6030_madc_resume(struct device *pdev)
307 {
308         int ret;
309
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,
313                                         TWL6030_MADC_CTRL);
314                 if (ret)
315                         dev_err(twl6030_madc->dev,
316                                 "unable to enable madc temp1!\n");
317         }
318
319         return 0;
320 };
321
322 static const struct dev_pm_ops twl6030_madc_pm_ops = {
323         .suspend = twl6030_madc_suspend,
324         .resume = twl6030_madc_resume,
325 };
326
327 static struct platform_driver twl6030_madc_driver = {
328         .probe = twl6030_madc_probe,
329         .remove = __exit_p(twl6030_madc_remove),
330         .driver = {
331                    .name = "twl6030_madc",
332                    .owner = THIS_MODULE,
333                    .pm = &twl6030_madc_pm_ops,
334                    },
335 };
336
337 static int __init twl6030_madc_init(void)
338 {
339         return platform_driver_register(&twl6030_madc_driver);
340 }
341
342 module_init(twl6030_madc_init);
343
344 static void __exit twl6030_madc_exit(void)
345 {
346         platform_driver_unregister(&twl6030_madc_driver);
347 }
348
349 module_exit(twl6030_madc_exit);
350
351 MODULE_DESCRIPTION("TWL6030 ADC driver");
352 MODULE_LICENSE("GPL");
353 MODULE_AUTHOR("J Keerthy");
354 MODULE_ALIAS("platform:twl6030_madc");