mfd: Add new ab8500 GPADC driver
authorArun Murthy <arun.murthy@stericsson.com>
Tue, 22 Feb 2011 09:11:13 +0000 (10:11 +0100)
committerSamuel Ortiz <sameo@linux.intel.com>
Wed, 23 Mar 2011 09:41:48 +0000 (10:41 +0100)
AB8500 GPADC driver used to convert Acc and battery/ac/usb voltage

Signed-off-by: Arun Murthy <arun.murthy@stericsson.com>
Acked-by: Linus Walleij <linus.walleij@stericsson.com>
Acked-by: Mattias Wallin <mattias.wallin@stericsson.com>
Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
drivers/mfd/Kconfig
drivers/mfd/Makefile
drivers/mfd/ab8500-gpadc.c [new file with mode: 0644]
include/linux/mfd/ab8500-gpadc.h [new file with mode: 0644]

index fdca643249e1743c5030e081ca525ccac8f7255a..d71af4531b14bfb057748c5d0069bc4bc7b6a5d8 100644 (file)
@@ -534,6 +534,13 @@ config AB8500_DEBUG
          Select this option if you want debug information using the debug
          filesystem, debugfs.
 
+config AB8500_GPADC
+       bool "AB8500 GPADC driver"
+       depends on AB8500_CORE && REGULATOR_AB8500
+       default y
+       help
+         AB8500 GPADC driver used to convert Acc and battery/ac/usb voltage
+
 config AB3550_CORE
         bool "ST-Ericsson AB3550 Mixed Signal Circuit core functions"
        select MFD_CORE
index 61a0b0f901a2bb15ba670754ae95be36a23e3aff..ad71bd59345f2936e72fe363aa591a8e11ccc584 100644 (file)
@@ -74,6 +74,7 @@ obj-$(CONFIG_AB3550_CORE)     += ab3550-core.o
 obj-$(CONFIG_AB8500_CORE)      += ab8500-core.o ab8500-sysctrl.o
 obj-$(CONFIG_AB8500_I2C_CORE)  += ab8500-i2c.o
 obj-$(CONFIG_AB8500_DEBUG)     += ab8500-debugfs.o
+obj-$(CONFIG_AB8500_GPADC)     += ab8500-gpadc.o
 obj-$(CONFIG_MFD_TIMBERDALE)    += timberdale.o
 obj-$(CONFIG_PMIC_ADP5520)     += adp5520.o
 obj-$(CONFIG_LPC_SCH)          += lpc_sch.o
diff --git a/drivers/mfd/ab8500-gpadc.c b/drivers/mfd/ab8500-gpadc.c
new file mode 100644 (file)
index 0000000..19339bc
--- /dev/null
@@ -0,0 +1,296 @@
+/*
+ * Copyright (C) ST-Ericsson SA 2010
+ *
+ * License Terms: GNU General Public License v2
+ * Author: Arun R Murthy <arun.murthy@stericsson.com>
+ */
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/delay.h>
+#include <linux/platform_device.h>
+#include <linux/completion.h>
+#include <linux/regulator/consumer.h>
+#include <linux/err.h>
+#include <linux/slab.h>
+#include <linux/mfd/ab8500.h>
+#include <linux/mfd/abx500.h>
+#include <linux/mfd/ab8500-gpadc.h>
+
+/*
+ * GPADC register offsets
+ * Bank : 0x0A
+ */
+#define AB8500_GPADC_CTRL1_REG         0x00
+#define AB8500_GPADC_CTRL2_REG         0x01
+#define AB8500_GPADC_CTRL3_REG         0x02
+#define AB8500_GPADC_AUTO_TIMER_REG    0x03
+#define AB8500_GPADC_STAT_REG          0x04
+#define AB8500_GPADC_MANDATAL_REG      0x05
+#define AB8500_GPADC_MANDATAH_REG      0x06
+#define AB8500_GPADC_AUTODATAL_REG     0x07
+#define AB8500_GPADC_AUTODATAH_REG     0x08
+#define AB8500_GPADC_MUX_CTRL_REG      0x09
+
+/* gpadc constants */
+#define EN_VINTCORE12                  0x04
+#define EN_VTVOUT                      0x02
+#define EN_GPADC                       0x01
+#define DIS_GPADC                      0x00
+#define SW_AVG_16                      0x60
+#define ADC_SW_CONV                    0x04
+#define EN_BUF                         0x40
+#define DIS_ZERO                       0x00
+#define GPADC_BUSY                     0x01
+
+/**
+ * struct ab8500_gpadc - ab8500 GPADC device information
+ * @dev:                       pointer to the struct device
+ * @parent:                    pointer to the parent device structure ab8500
+ * @ab8500_gpadc_complete:     pointer to the struct completion, to indicate
+ *                             the completion of gpadc conversion
+ * @ab8500_gpadc_lock:         structure of type mutex
+ * @regu:                      pointer to the struct regulator
+ * @irq:                       interrupt number that is used by gpadc
+ */
+static struct ab8500_gpadc {
+       struct device *dev;
+       struct ab8500 *parent;
+       struct completion ab8500_gpadc_complete;
+       struct mutex ab8500_gpadc_lock;
+       struct regulator *regu;
+       int irq;
+} *di;
+
+/**
+ * ab8500_gpadc_convert() - gpadc conversion
+ * @input:     analog input to be converted to digital data
+ *
+ * This function converts the selected analog i/p to digital
+ * data. Thereafter calibration has to be made to obtain the
+ * data in the required quantity measurement.
+ */
+int ab8500_gpadc_convert(u8 input)
+{
+       int ret;
+       u16 data = 0;
+       int looplimit = 0;
+       u8 val, low_data, high_data;
+
+       if (!di)
+               return -ENODEV;
+
+       mutex_lock(&di->ab8500_gpadc_lock);
+       /* Enable VTVout LDO this is required for GPADC */
+       regulator_enable(di->regu);
+
+       /* Check if ADC is not busy, lock and proceed */
+       do {
+               ret = abx500_get_register_interruptible(di->dev, AB8500_GPADC,
+                       AB8500_GPADC_STAT_REG, &val);
+               if (ret < 0)
+                       goto out;
+               if (!(val & GPADC_BUSY))
+                       break;
+               msleep(10);
+       } while (++looplimit < 10);
+       if (looplimit >= 10 && (val & GPADC_BUSY)) {
+               dev_err(di->dev, "gpadc_conversion: GPADC busy");
+               ret = -EINVAL;
+               goto out;
+       }
+
+       /* Enable GPADC */
+       ret = abx500_mask_and_set_register_interruptible(di->dev, AB8500_GPADC,
+               AB8500_GPADC_CTRL1_REG, EN_GPADC, EN_GPADC);
+       if (ret < 0) {
+               dev_err(di->dev, "gpadc_conversion: enable gpadc failed\n");
+               goto out;
+       }
+       /* Select the input source and set average samples to 16 */
+       ret = abx500_set_register_interruptible(di->dev, AB8500_GPADC,
+               AB8500_GPADC_CTRL2_REG, (input | SW_AVG_16));
+       if (ret < 0) {
+               dev_err(di->dev,
+                       "gpadc_conversion: set avg samples failed\n");
+               goto out;
+       }
+       /* Enable ADC, Buffering and select rising edge, start Conversion */
+       ret = abx500_mask_and_set_register_interruptible(di->dev, AB8500_GPADC,
+               AB8500_GPADC_CTRL1_REG, EN_BUF, EN_BUF);
+       if (ret < 0) {
+               dev_err(di->dev,
+                       "gpadc_conversion: select falling edge failed\n");
+               goto out;
+       }
+       ret = abx500_mask_and_set_register_interruptible(di->dev, AB8500_GPADC,
+               AB8500_GPADC_CTRL1_REG, ADC_SW_CONV, ADC_SW_CONV);
+       if (ret < 0) {
+               dev_err(di->dev,
+                       "gpadc_conversion: start s/w conversion failed\n");
+               goto out;
+       }
+       /* wait for completion of conversion */
+       if (!wait_for_completion_timeout(&di->ab8500_gpadc_complete, 2*HZ)) {
+               dev_err(di->dev,
+                       "timeout: didnt recieve GPADC conversion interrupt\n");
+               ret = -EINVAL;
+               goto out;
+       }
+
+       /* Read the converted RAW data */
+       ret = abx500_get_register_interruptible(di->dev, AB8500_GPADC,
+               AB8500_GPADC_MANDATAL_REG, &low_data);
+       if (ret < 0) {
+               dev_err(di->dev, "gpadc_conversion: read low data failed\n");
+               goto out;
+       }
+
+       ret = abx500_get_register_interruptible(di->dev, AB8500_GPADC,
+               AB8500_GPADC_MANDATAH_REG, &high_data);
+       if (ret < 0) {
+               dev_err(di->dev, "gpadc_conversion: read high data failed\n");
+               goto out;
+       }
+
+       data = (high_data << 8) | low_data;
+       /* Disable GPADC */
+       ret = abx500_set_register_interruptible(di->dev, AB8500_GPADC,
+               AB8500_GPADC_CTRL1_REG, DIS_GPADC);
+       if (ret < 0) {
+               dev_err(di->dev, "gpadc_conversion: disable gpadc failed\n");
+               goto out;
+       }
+       /* Disable VTVout LDO this is required for GPADC */
+       regulator_disable(di->regu);
+       mutex_unlock(&di->ab8500_gpadc_lock);
+       return data;
+
+out:
+       /*
+        * It has shown to be needed to turn off the GPADC if an error occurs,
+        * otherwise we might have problem when waiting for the busy bit in the
+        * GPADC status register to go low. In V1.1 there wait_for_completion
+        * seems to timeout when waiting for an interrupt.. Not seen in V2.0
+        */
+       (void) abx500_set_register_interruptible(di->dev, AB8500_GPADC,
+               AB8500_GPADC_CTRL1_REG, DIS_GPADC);
+       regulator_disable(di->regu);
+       mutex_unlock(&di->ab8500_gpadc_lock);
+       dev_err(di->dev, "gpadc_conversion: Failed to AD convert channel %d\n",
+               input);
+       return ret;
+}
+EXPORT_SYMBOL(ab8500_gpadc_convert);
+
+/**
+ * ab8500_bm_gpswadcconvend_handler() - isr for s/w gpadc conversion completion
+ * @irq:       irq number
+ * @data:      pointer to the data passed during request irq
+ *
+ * This is a interrupt service routine for s/w gpadc conversion completion.
+ * Notifies the gpadc completion is completed and the converted raw value
+ * can be read from the registers.
+ * Returns IRQ status(IRQ_HANDLED)
+ */
+static irqreturn_t ab8500_bm_gpswadcconvend_handler(int irq, void *_di)
+{
+       struct ab8500_gpadc *gpadc = _di;
+
+       complete(&gpadc->ab8500_gpadc_complete);
+
+       return IRQ_HANDLED;
+}
+
+static int __devinit ab8500_gpadc_probe(struct platform_device *pdev)
+{
+       int ret = 0;
+       struct ab8500_gpadc *gpadc;
+
+       gpadc = kzalloc(sizeof(struct ab8500_gpadc), GFP_KERNEL);
+       if (!gpadc) {
+               dev_err(&pdev->dev, "Error: No memory\n");
+               return -ENOMEM;
+       }
+
+       gpadc->parent = dev_get_drvdata(pdev->dev.parent);
+       gpadc->irq = platform_get_irq_byname(pdev, "SW_CONV_END");
+       if (gpadc->irq < 0) {
+               dev_err(gpadc->dev, "failed to get platform irq-%d\n", di->irq);
+               ret = gpadc->irq;
+               goto fail;
+       }
+
+       gpadc->dev = &pdev->dev;
+       mutex_init(&di->ab8500_gpadc_lock);
+
+       /* Initialize completion used to notify completion of conversion */
+       init_completion(&gpadc->ab8500_gpadc_complete);
+
+       /* Register interrupt  - SwAdcComplete */
+       ret = request_threaded_irq(gpadc->irq, NULL,
+               ab8500_bm_gpswadcconvend_handler,
+               IRQF_NO_SUSPEND | IRQF_SHARED, "ab8500-gpadc", gpadc);
+       if (ret < 0) {
+               dev_err(gpadc->dev, "Failed to register interrupt, irq: %d\n",
+                       gpadc->irq);
+               goto fail;
+       }
+
+       /* VTVout LDO used to power up ab8500-GPADC */
+       gpadc->regu = regulator_get(&pdev->dev, "vddadc");
+       if (IS_ERR(gpadc->regu)) {
+               ret = PTR_ERR(gpadc->regu);
+               dev_err(gpadc->dev, "failed to get vtvout LDO\n");
+               goto fail;
+       }
+       di = gpadc;
+       dev_dbg(gpadc->dev, "probe success\n");
+       return 0;
+fail:
+       kfree(gpadc);
+       gpadc = NULL;
+       return ret;
+}
+
+static int __devexit ab8500_gpadc_remove(struct platform_device *pdev)
+{
+       struct ab8500_gpadc *gpadc = platform_get_drvdata(pdev);
+
+       /* remove interrupt  - completion of Sw ADC conversion */
+       free_irq(gpadc->irq, di);
+       /* disable VTVout LDO that is being used by GPADC */
+       regulator_put(gpadc->regu);
+       kfree(gpadc);
+       gpadc = NULL;
+       return 0;
+}
+
+static struct platform_driver ab8500_gpadc_driver = {
+       .probe = ab8500_gpadc_probe,
+       .remove = __devexit_p(ab8500_gpadc_remove),
+       .driver = {
+               .name = "ab8500-gpadc",
+               .owner = THIS_MODULE,
+       },
+};
+
+static int __init ab8500_gpadc_init(void)
+{
+       return platform_driver_register(&ab8500_gpadc_driver);
+}
+
+static void __exit ab8500_gpadc_exit(void)
+{
+       platform_driver_unregister(&ab8500_gpadc_driver);
+}
+
+subsys_initcall_sync(ab8500_gpadc_init);
+module_exit(ab8500_gpadc_exit);
+
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Arun R Murthy");
+MODULE_ALIAS("platform:ab8500_gpadc");
+MODULE_DESCRIPTION("AB8500 GPADC driver");
diff --git a/include/linux/mfd/ab8500-gpadc.h b/include/linux/mfd/ab8500-gpadc.h
new file mode 100644 (file)
index 0000000..9f6cc26
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2010 ST-Ericsson SA
+ * Licensed under GPLv2.
+ *
+ * Author: Arun R Murthy <arun.murthy@stericsson.com>
+ */
+
+#ifndef        _AB8500_GPADC_H
+#define _AB8500_GPADC_H
+
+/* GPADC source: From datasheet(ADCSwSel[4:0] in GPADCCtrl2) */
+#define BAT_CTRL        0x01
+#define BTEMP_BALL      0x02
+#define MAIN_CHARGER_V  0x03
+#define ACC_DETECT1     0x04
+#define ACC_DETECT2     0x05
+#define ADC_AUX1       0x06
+#define ADC_AUX2       0x07
+#define MAIN_BAT_V      0x08
+#define VBUS_V          0x09
+#define MAIN_CHARGER_C  0x0A
+#define USB_CHARGER_C   0x0B
+#define BK_BAT_V        0x0C
+#define DIE_TEMP        0x0D
+
+int ab8500_gpadc_convert(u8 input);
+
+#endif /* _AB8500_GPADC_H */