2 * drivers/i2c/chips/twl4030-madc.c
4 * TWL4030 MADC module driver
6 * Copyright (C) 2008 Nokia Corporation
7 * Mikko Ylinen <mikko.k.ylinen@nokia.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
28 #include <linux/types.h>
29 #include <linux/module.h>
30 #include <linux/delay.h>
32 #include <linux/platform_device.h>
33 #include <linux/miscdevice.h>
34 #include <linux/i2c/twl4030.h>
35 #include <linux/i2c/twl4030-madc.h>
37 #include <asm/uaccess.h>
39 #define TWL4030_MADC_PFX "twl4030-madc: "
41 struct twl4030_madc_data {
44 struct work_struct ws;
45 struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
50 static struct twl4030_madc_data *the_madc;
53 const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
55 .sel = TWL4030_MADC_RTSELECT_LSB,
56 .avg = TWL4030_MADC_RTAVERAGE_LSB,
57 .rbase = TWL4030_MADC_RTCH0_LSB,
59 [TWL4030_MADC_SW1] = {
60 .sel = TWL4030_MADC_SW1SELECT_LSB,
61 .avg = TWL4030_MADC_SW1AVERAGE_LSB,
62 .rbase = TWL4030_MADC_GPCH0_LSB,
63 .ctrl = TWL4030_MADC_CTRL_SW1,
65 [TWL4030_MADC_SW2] = {
66 .sel = TWL4030_MADC_SW2SELECT_LSB,
67 .avg = TWL4030_MADC_SW2AVERAGE_LSB,
68 .rbase = TWL4030_MADC_GPCH0_LSB,
69 .ctrl = TWL4030_MADC_CTRL_SW2,
73 static int twl4030_madc_read(struct twl4030_madc_data *madc, u8 reg)
78 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MADC, &val, reg);
80 dev_dbg(madc->dev, "unable to read register 0x%X\n", reg);
87 static void twl4030_madc_write(struct twl4030_madc_data *madc, u8 reg, u8 val)
91 ret = twl4030_i2c_write_u8(TWL4030_MODULE_MADC, val, reg);
93 dev_err(madc->dev, "unable to write register 0x%X\n", reg);
96 static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
100 /* For each ADC channel, we have MSB and LSB register pair. MSB address
101 * is always LSB address+1. reg parameter is the addr of LSB register */
102 msb = twl4030_madc_read(madc, reg + 1);
103 lsb = twl4030_madc_read(madc, reg);
105 return (int)(((msb << 8) | lsb) >> 6);
108 static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
109 u8 reg_base, u16 channels, int *buf)
117 for (i = 0; i < TWL4030_MADC_MAX_CHANNELS; i++) {
118 if (channels & (1<<i)) {
119 reg = reg_base + 2*i;
120 buf[i] = twl4030_madc_channel_raw_read(madc, reg);
127 static void twl4030_madc_enable_irq(struct twl4030_madc_data *madc, int id)
131 val = twl4030_madc_read(madc, madc->imr);
133 twl4030_madc_write(madc, madc->imr, val);
136 static void twl4030_madc_disable_irq(struct twl4030_madc_data *madc, int id)
140 val = twl4030_madc_read(madc, madc->imr);
142 twl4030_madc_write(madc, madc->imr, val);
145 static irqreturn_t twl4030_madc_irq_handler(int irq, void *_madc)
147 struct twl4030_madc_data *madc = _madc;
151 /* Use COR to ack interrupts since we have no shared IRQs in ISRx */
152 isr_val = twl4030_madc_read(madc, madc->isr);
153 imr_val = twl4030_madc_read(madc, madc->imr);
157 for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
159 if (!(isr_val & (1<<i)))
162 twl4030_madc_disable_irq(madc, i);
163 madc->requests[i].result_pending = 1;
166 schedule_work(&madc->ws);
171 static void twl4030_madc_work(struct work_struct *ws)
173 const struct twl4030_madc_conversion_method *method;
174 struct twl4030_madc_data *madc;
175 struct twl4030_madc_request *r;
178 madc = container_of(ws, struct twl4030_madc_data, ws);
179 mutex_lock(&madc->lock);
181 for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
183 r = &madc->requests[i];
185 /* No pending results for this method, move to next one */
186 if (!r->result_pending)
189 method = &twl4030_conversion_methods[r->method];
192 len = twl4030_madc_read_channels(madc, method->rbase,
193 r->channels, r->rbuf);
195 /* Return results to caller */
196 if (r->func_cb != NULL) {
197 r->func_cb(len, r->channels, r->rbuf);
202 r->result_pending = 0;
206 mutex_unlock(&madc->lock);
209 static int twl4030_madc_set_irq(struct twl4030_madc_data *madc,
210 struct twl4030_madc_request *req)
212 struct twl4030_madc_request *p;
214 p = &madc->requests[req->method];
216 memcpy(p, req, sizeof *req);
218 twl4030_madc_enable_irq(madc, req->method);
223 static inline void twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
226 const struct twl4030_madc_conversion_method *method;
228 method = &twl4030_conversion_methods[conv_method];
230 switch (conv_method) {
231 case TWL4030_MADC_SW1:
232 case TWL4030_MADC_SW2:
233 twl4030_madc_write(madc, method->ctrl, TWL4030_MADC_SW_START);
235 case TWL4030_MADC_RT:
241 static void twl4030_madc_wait_conversion_ready_ms(
242 struct twl4030_madc_data *madc,
243 u8 *time, u8 status_reg)
250 reg = twl4030_madc_read(madc, status_reg);
251 } while (((reg & TWL4030_MADC_BUSY) && !(reg & TWL4030_MADC_EOC_SW)) &&
255 int twl4030_madc_conversion(struct twl4030_madc_request *req)
257 const struct twl4030_madc_conversion_method *method;
258 u8 wait_time, ch_msb, ch_lsb;
264 /* Do we have a conversion request ongoing */
265 if (the_madc->requests[req->method].active)
268 ch_msb = (req->channels >> 8) & 0xff;
269 ch_lsb = req->channels & 0xff;
271 method = &twl4030_conversion_methods[req->method];
273 mutex_lock(&the_madc->lock);
275 /* Select channels to be converted */
276 twl4030_madc_write(the_madc, method->sel + 1, ch_msb);
277 twl4030_madc_write(the_madc, method->sel, ch_lsb);
279 /* Select averaging for all channels if do_avg is set */
281 twl4030_madc_write(the_madc, method->avg + 1, ch_msb);
282 twl4030_madc_write(the_madc, method->avg, ch_lsb);
285 if ((req->type == TWL4030_MADC_IRQ_ONESHOT) && (req->func_cb != NULL)) {
286 twl4030_madc_set_irq(the_madc, req);
287 twl4030_madc_start_conversion(the_madc, req->method);
288 the_madc->requests[req->method].active = 1;
293 /* With RT method we should not be here anymore */
294 if (req->method == TWL4030_MADC_RT) {
299 twl4030_madc_start_conversion(the_madc, req->method);
300 the_madc->requests[req->method].active = 1;
302 /* Wait until conversion is ready (ctrl register returns EOC) */
304 twl4030_madc_wait_conversion_ready_ms(the_madc,
305 &wait_time, method->ctrl);
306 if (wait_time == 0) {
307 dev_dbg(the_madc->dev, "conversion timeout!\n");
312 ret = twl4030_madc_read_channels(the_madc, method->rbase, req->channels,
315 the_madc->requests[req->method].active = 0;
318 mutex_unlock(&the_madc->lock);
322 EXPORT_SYMBOL(twl4030_madc_conversion);
324 static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
330 /* Current generator is only available for ADCIN0 and ADCIN1. NB:
331 * ADCIN1 current generator only works when AC or VBUS is present */
335 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE,
336 ®val, TWL4030_BCI_BCICTL1);
338 regval |= (chan) ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
340 regval &= (chan) ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
341 ret = twl4030_i2c_write_u8(TWL4030_MODULE_MAIN_CHARGE,
342 regval, TWL4030_BCI_BCICTL1);
347 static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
351 regval = twl4030_madc_read(madc, TWL4030_MADC_CTRL1);
353 regval |= TWL4030_MADC_MADCON;
355 regval &= ~TWL4030_MADC_MADCON;
356 twl4030_madc_write(madc, TWL4030_MADC_CTRL1, regval);
361 static int twl4030_madc_ioctl(struct inode *inode, struct file *filp,
362 unsigned int cmd, unsigned long arg)
364 struct twl4030_madc_user_parms par;
367 ret = copy_from_user(&par, (void __user *) arg, sizeof(par));
369 dev_dbg(the_madc->dev, "copy_from_user: %d\n", ret);
374 case TWL4030_MADC_IOCX_ADC_RAW_READ: {
375 struct twl4030_madc_request req;
376 if (par.channel >= TWL4030_MADC_MAX_CHANNELS)
379 req.channels = (1 << par.channel);
380 req.do_avg = par.average;
381 req.method = TWL4030_MADC_SW1;
384 val = twl4030_madc_conversion(&req);
389 par.result = (u16)req.rbuf[par.channel];
397 ret = copy_to_user((void __user *) arg, &par, sizeof(par));
399 dev_dbg(the_madc->dev, "copy_to_user: %d\n", ret);
406 static struct file_operations twl4030_madc_fileops = {
407 .owner = THIS_MODULE,
408 .ioctl = twl4030_madc_ioctl
411 static struct miscdevice twl4030_madc_device = {
412 .minor = MISC_DYNAMIC_MINOR,
413 .name = "twl4030-madc",
414 .fops = &twl4030_madc_fileops
417 static int __init twl4030_madc_probe(struct platform_device *pdev)
419 struct twl4030_madc_data *madc;
420 struct twl4030_madc_platform_data *pdata = pdev->dev.platform_data;
424 madc = kzalloc(sizeof *madc, GFP_KERNEL);
429 dev_dbg(&pdev->dev, "platform_data not available\n");
434 madc->imr = (pdata->irq_line == 1) ? TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
435 madc->isr = (pdata->irq_line == 1) ? TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
437 ret = misc_register(&twl4030_madc_device);
439 dev_dbg(&pdev->dev, "could not register misc_device\n");
442 twl4030_madc_set_power(madc, 1);
443 twl4030_madc_set_current_generator(madc, 0, 1);
445 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE,
446 ®val, TWL4030_BCI_BCICTL1);
448 regval |= TWL4030_BCI_MESBAT;
450 ret = twl4030_i2c_write_u8(TWL4030_MODULE_MAIN_CHARGE,
451 regval, TWL4030_BCI_BCICTL1);
453 ret = request_irq(TWL4030_MODIRQ_MADC, twl4030_madc_irq_handler,
454 IRQF_DISABLED, "twl4030_madc", madc);
456 dev_dbg(&pdev->dev, "could not request irq\n");
460 platform_set_drvdata(pdev, madc);
461 mutex_init(&madc->lock);
462 INIT_WORK(&madc->ws, twl4030_madc_work);
469 misc_deregister(&twl4030_madc_device);
478 static int __exit twl4030_madc_remove(struct platform_device *pdev)
480 struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
482 twl4030_madc_set_power(madc, 0);
483 twl4030_madc_set_current_generator(madc, 0, 0);
484 free_irq(TWL4030_MODIRQ_MADC, madc);
485 cancel_work_sync(&madc->ws);
486 misc_deregister(&twl4030_madc_device);
491 static struct platform_driver twl4030_madc_driver = {
492 .probe = twl4030_madc_probe,
493 .remove = __exit_p(twl4030_madc_remove),
495 .name = "twl4030_madc",
496 .owner = THIS_MODULE,
500 static int __init twl4030_madc_init(void)
502 return platform_driver_register(&twl4030_madc_driver);
504 module_init(twl4030_madc_init);
506 static void __exit twl4030_madc_exit(void)
508 platform_driver_unregister(&twl4030_madc_driver);
510 module_exit(twl4030_madc_exit);
512 MODULE_ALIAS("platform:twl4030-madc");
513 MODULE_AUTHOR("Nokia Corporation");
514 MODULE_DESCRIPTION("twl4030 ADC driver");
515 MODULE_LICENSE("GPL");