mfd: Reentrance and revamp ab8500 gpadc fetching interface

This revamps the interface so that AB8500 GPADCs are fetched by
name. Probed GPADCs are added to a list and this list is searched
for a matching GPADC. This makes it possible to have multiple
AB8500 GPADC instances instead of it being a singleton, and
rids the need to keep a GPADC pointer around in the core AB8500
MFD struct.

Currently the match is made to the device name which is by default
numbered from the device instance such as "ab8500-gpadc.0" but
by using the .init_name field of the device a more intiutive
naming for the GPADC blocks can be achieved if desired.

Signed-off-by: Daniel Willerud <daniel.willerud@stericsson.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
This commit is contained in:
Daniel Willerud 2011-03-05 11:46:13 +01:00 committed by Samuel Ortiz
parent cf16943947
commit 6321992cd3
2 changed files with 75 additions and 46 deletions

View File

@ -3,6 +3,7 @@
* *
* License Terms: GNU General Public License v2 * License Terms: GNU General Public License v2
* Author: Arun R Murthy <arun.murthy@stericsson.com> * Author: Arun R Murthy <arun.murthy@stericsson.com>
* Author: Daniel Willerud <daniel.willerud@stericsson.com>
*/ */
#include <linux/init.h> #include <linux/init.h>
#include <linux/module.h> #include <linux/module.h>
@ -15,6 +16,7 @@
#include <linux/regulator/consumer.h> #include <linux/regulator/consumer.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/list.h>
#include <linux/mfd/ab8500.h> #include <linux/mfd/ab8500.h>
#include <linux/mfd/abx500.h> #include <linux/mfd/abx500.h>
#include <linux/mfd/ab8500/ab8500-gpadc.h> #include <linux/mfd/ab8500/ab8500-gpadc.h>
@ -48,21 +50,41 @@
/** /**
* struct ab8500_gpadc - ab8500 GPADC device information * struct ab8500_gpadc - ab8500 GPADC device information
* @dev: pointer to the struct device * @dev: pointer to the struct device
* @parent: pointer to the parent device structure ab8500 * @node: a list of AB8500 GPADCs, hence prepared for
reentrance
* @ab8500_gpadc_complete: pointer to the struct completion, to indicate * @ab8500_gpadc_complete: pointer to the struct completion, to indicate
* the completion of gpadc conversion * the completion of gpadc conversion
* @ab8500_gpadc_lock: structure of type mutex * @ab8500_gpadc_lock: structure of type mutex
* @regu: pointer to the struct regulator * @regu: pointer to the struct regulator
* @irq: interrupt number that is used by gpadc * @irq: interrupt number that is used by gpadc
*/ */
static struct ab8500_gpadc { struct ab8500_gpadc {
struct device *dev; struct device *dev;
struct ab8500 *parent; struct list_head node;
struct completion ab8500_gpadc_complete; struct completion ab8500_gpadc_complete;
struct mutex ab8500_gpadc_lock; struct mutex ab8500_gpadc_lock;
struct regulator *regu; struct regulator *regu;
int irq; int irq;
} *di; };
static LIST_HEAD(ab8500_gpadc_list);
/**
* ab8500_gpadc_get() - returns a reference to the primary AB8500 GPADC
* (i.e. the first GPADC in the instance list)
*/
struct ab8500_gpadc *ab8500_gpadc_get(char *name)
{
struct ab8500_gpadc *gpadc;
list_for_each_entry(gpadc, &ab8500_gpadc_list, node) {
if (!strcmp(name, dev_name(gpadc->dev)))
return gpadc;
}
return ERR_PTR(-ENOENT);
}
EXPORT_SYMBOL(ab8500_gpadc_get);
/** /**
* ab8500_gpadc_convert() - gpadc conversion * ab8500_gpadc_convert() - gpadc conversion
@ -72,24 +94,24 @@ static struct ab8500_gpadc {
* data. Thereafter calibration has to be made to obtain the * data. Thereafter calibration has to be made to obtain the
* data in the required quantity measurement. * data in the required quantity measurement.
*/ */
int ab8500_gpadc_convert(u8 input) int ab8500_gpadc_convert(struct ab8500_gpadc *gpadc, u8 input)
{ {
int ret; int ret;
u16 data = 0; u16 data = 0;
int looplimit = 0; int looplimit = 0;
u8 val, low_data, high_data; u8 val, low_data, high_data;
if (!di) if (!gpadc)
return -ENODEV; return -ENODEV;
mutex_lock(&di->ab8500_gpadc_lock); mutex_lock(&gpadc->ab8500_gpadc_lock);
/* Enable VTVout LDO this is required for GPADC */ /* Enable VTVout LDO this is required for GPADC */
regulator_enable(di->regu); regulator_enable(gpadc->regu);
/* Check if ADC is not busy, lock and proceed */ /* Check if ADC is not busy, lock and proceed */
do { do {
ret = abx500_get_register_interruptible(di->dev, AB8500_GPADC, ret = abx500_get_register_interruptible(gpadc->dev,
AB8500_GPADC_STAT_REG, &val); AB8500_GPADC, AB8500_GPADC_STAT_REG, &val);
if (ret < 0) if (ret < 0)
goto out; goto out;
if (!(val & GPADC_BUSY)) if (!(val & GPADC_BUSY))
@ -97,75 +119,76 @@ int ab8500_gpadc_convert(u8 input)
msleep(10); msleep(10);
} while (++looplimit < 10); } while (++looplimit < 10);
if (looplimit >= 10 && (val & GPADC_BUSY)) { if (looplimit >= 10 && (val & GPADC_BUSY)) {
dev_err(di->dev, "gpadc_conversion: GPADC busy"); dev_err(gpadc->dev, "gpadc_conversion: GPADC busy");
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
/* Enable GPADC */ /* Enable GPADC */
ret = abx500_mask_and_set_register_interruptible(di->dev, AB8500_GPADC, ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
AB8500_GPADC_CTRL1_REG, EN_GPADC, EN_GPADC); AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_GPADC, EN_GPADC);
if (ret < 0) { if (ret < 0) {
dev_err(di->dev, "gpadc_conversion: enable gpadc failed\n"); dev_err(gpadc->dev, "gpadc_conversion: enable gpadc failed\n");
goto out; goto out;
} }
/* Select the input source and set average samples to 16 */ /* Select the input source and set average samples to 16 */
ret = abx500_set_register_interruptible(di->dev, AB8500_GPADC, ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
AB8500_GPADC_CTRL2_REG, (input | SW_AVG_16)); AB8500_GPADC_CTRL2_REG, (input | SW_AVG_16));
if (ret < 0) { if (ret < 0) {
dev_err(di->dev, dev_err(gpadc->dev,
"gpadc_conversion: set avg samples failed\n"); "gpadc_conversion: set avg samples failed\n");
goto out; goto out;
} }
/* Enable ADC, Buffering and select rising edge, start Conversion */ /* Enable ADC, Buffering and select rising edge, start Conversion */
ret = abx500_mask_and_set_register_interruptible(di->dev, AB8500_GPADC, ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
AB8500_GPADC_CTRL1_REG, EN_BUF, EN_BUF); AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_BUF, EN_BUF);
if (ret < 0) { if (ret < 0) {
dev_err(di->dev, dev_err(gpadc->dev,
"gpadc_conversion: select falling edge failed\n"); "gpadc_conversion: select falling edge failed\n");
goto out; goto out;
} }
ret = abx500_mask_and_set_register_interruptible(di->dev, AB8500_GPADC, ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
AB8500_GPADC_CTRL1_REG, ADC_SW_CONV, ADC_SW_CONV); AB8500_GPADC, AB8500_GPADC_CTRL1_REG, ADC_SW_CONV, ADC_SW_CONV);
if (ret < 0) { if (ret < 0) {
dev_err(di->dev, dev_err(gpadc->dev,
"gpadc_conversion: start s/w conversion failed\n"); "gpadc_conversion: start s/w conversion failed\n");
goto out; goto out;
} }
/* wait for completion of conversion */ /* wait for completion of conversion */
if (!wait_for_completion_timeout(&di->ab8500_gpadc_complete, 2*HZ)) { if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete, 2*HZ)) {
dev_err(di->dev, dev_err(gpadc->dev,
"timeout: didnt recieve GPADC conversion interrupt\n"); "timeout: didnt recieve GPADC conversion interrupt\n");
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
/* Read the converted RAW data */ /* Read the converted RAW data */
ret = abx500_get_register_interruptible(di->dev, AB8500_GPADC, ret = abx500_get_register_interruptible(gpadc->dev, AB8500_GPADC,
AB8500_GPADC_MANDATAL_REG, &low_data); AB8500_GPADC_MANDATAL_REG, &low_data);
if (ret < 0) { if (ret < 0) {
dev_err(di->dev, "gpadc_conversion: read low data failed\n"); dev_err(gpadc->dev, "gpadc_conversion: read low data failed\n");
goto out; goto out;
} }
ret = abx500_get_register_interruptible(di->dev, AB8500_GPADC, ret = abx500_get_register_interruptible(gpadc->dev, AB8500_GPADC,
AB8500_GPADC_MANDATAH_REG, &high_data); AB8500_GPADC_MANDATAH_REG, &high_data);
if (ret < 0) { if (ret < 0) {
dev_err(di->dev, "gpadc_conversion: read high data failed\n"); dev_err(gpadc->dev,
"gpadc_conversion: read high data failed\n");
goto out; goto out;
} }
data = (high_data << 8) | low_data; data = (high_data << 8) | low_data;
/* Disable GPADC */ /* Disable GPADC */
ret = abx500_set_register_interruptible(di->dev, AB8500_GPADC, ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
AB8500_GPADC_CTRL1_REG, DIS_GPADC); AB8500_GPADC_CTRL1_REG, DIS_GPADC);
if (ret < 0) { if (ret < 0) {
dev_err(di->dev, "gpadc_conversion: disable gpadc failed\n"); dev_err(gpadc->dev, "gpadc_conversion: disable gpadc failed\n");
goto out; goto out;
} }
/* Disable VTVout LDO this is required for GPADC */ /* Disable VTVout LDO this is required for GPADC */
regulator_disable(di->regu); regulator_disable(gpadc->regu);
mutex_unlock(&di->ab8500_gpadc_lock); mutex_unlock(&gpadc->ab8500_gpadc_lock);
return data; return data;
out: out:
@ -175,12 +198,12 @@ out:
* GPADC status register to go low. In V1.1 there wait_for_completion * 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 * seems to timeout when waiting for an interrupt.. Not seen in V2.0
*/ */
(void) abx500_set_register_interruptible(di->dev, AB8500_GPADC, (void) abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
AB8500_GPADC_CTRL1_REG, DIS_GPADC); AB8500_GPADC_CTRL1_REG, DIS_GPADC);
regulator_disable(di->regu); regulator_disable(gpadc->regu);
mutex_unlock(&di->ab8500_gpadc_lock); mutex_unlock(&gpadc->ab8500_gpadc_lock);
dev_err(di->dev, "gpadc_conversion: Failed to AD convert channel %d\n", dev_err(gpadc->dev,
input); "gpadc_conversion: Failed to AD convert channel %d\n", input);
return ret; return ret;
} }
EXPORT_SYMBOL(ab8500_gpadc_convert); EXPORT_SYMBOL(ab8500_gpadc_convert);
@ -195,9 +218,9 @@ EXPORT_SYMBOL(ab8500_gpadc_convert);
* can be read from the registers. * can be read from the registers.
* Returns IRQ status(IRQ_HANDLED) * Returns IRQ status(IRQ_HANDLED)
*/ */
static irqreturn_t ab8500_bm_gpswadcconvend_handler(int irq, void *_di) static irqreturn_t ab8500_bm_gpswadcconvend_handler(int irq, void *_gpadc)
{ {
struct ab8500_gpadc *gpadc = _di; struct ab8500_gpadc *gpadc = _gpadc;
complete(&gpadc->ab8500_gpadc_complete); complete(&gpadc->ab8500_gpadc_complete);
@ -215,16 +238,16 @@ static int __devinit ab8500_gpadc_probe(struct platform_device *pdev)
return -ENOMEM; return -ENOMEM;
} }
gpadc->parent = dev_get_drvdata(pdev->dev.parent);
gpadc->irq = platform_get_irq_byname(pdev, "SW_CONV_END"); gpadc->irq = platform_get_irq_byname(pdev, "SW_CONV_END");
if (gpadc->irq < 0) { if (gpadc->irq < 0) {
dev_err(gpadc->dev, "failed to get platform irq-%d\n", di->irq); dev_err(gpadc->dev, "failed to get platform irq-%d\n",
gpadc->irq);
ret = gpadc->irq; ret = gpadc->irq;
goto fail; goto fail;
} }
gpadc->dev = &pdev->dev; gpadc->dev = &pdev->dev;
mutex_init(&di->ab8500_gpadc_lock); mutex_init(&gpadc->ab8500_gpadc_lock);
/* Initialize completion used to notify completion of conversion */ /* Initialize completion used to notify completion of conversion */
init_completion(&gpadc->ab8500_gpadc_complete); init_completion(&gpadc->ab8500_gpadc_complete);
@ -246,7 +269,7 @@ static int __devinit ab8500_gpadc_probe(struct platform_device *pdev)
dev_err(gpadc->dev, "failed to get vtvout LDO\n"); dev_err(gpadc->dev, "failed to get vtvout LDO\n");
goto fail; goto fail;
} }
di = gpadc; list_add_tail(&gpadc->node, &ab8500_gpadc_list);
dev_dbg(gpadc->dev, "probe success\n"); dev_dbg(gpadc->dev, "probe success\n");
return 0; return 0;
fail: fail:
@ -259,8 +282,10 @@ static int __devexit ab8500_gpadc_remove(struct platform_device *pdev)
{ {
struct ab8500_gpadc *gpadc = platform_get_drvdata(pdev); struct ab8500_gpadc *gpadc = platform_get_drvdata(pdev);
/* remove this gpadc entry from the list */
list_del(&gpadc->node);
/* remove interrupt - completion of Sw ADC conversion */ /* remove interrupt - completion of Sw ADC conversion */
free_irq(gpadc->irq, di); free_irq(gpadc->irq, gpadc);
/* disable VTVout LDO that is being used by GPADC */ /* disable VTVout LDO that is being used by GPADC */
regulator_put(gpadc->regu); regulator_put(gpadc->regu);
kfree(gpadc); kfree(gpadc);
@ -291,6 +316,6 @@ subsys_initcall_sync(ab8500_gpadc_init);
module_exit(ab8500_gpadc_exit); module_exit(ab8500_gpadc_exit);
MODULE_LICENSE("GPL v2"); MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Arun R Murthy"); MODULE_AUTHOR("Arun R Murthy, Daniel Willerud");
MODULE_ALIAS("platform:ab8500_gpadc"); MODULE_ALIAS("platform:ab8500_gpadc");
MODULE_DESCRIPTION("AB8500 GPADC driver"); MODULE_DESCRIPTION("AB8500 GPADC driver");

View File

@ -3,6 +3,7 @@
* Licensed under GPLv2. * Licensed under GPLv2.
* *
* Author: Arun R Murthy <arun.murthy@stericsson.com> * Author: Arun R Murthy <arun.murthy@stericsson.com>
* Author: Daniel Willerud <daniel.willerud@stericsson.com>
*/ */
#ifndef _AB8500_GPADC_H #ifndef _AB8500_GPADC_H
@ -23,6 +24,9 @@
#define BK_BAT_V 0x0C #define BK_BAT_V 0x0C
#define DIE_TEMP 0x0D #define DIE_TEMP 0x0D
int ab8500_gpadc_convert(u8 input); struct ab8500_gpadc;
struct ab8500_gpadc *ab8500_gpadc_get(char *name);
int ab8500_gpadc_convert(struct ab8500_gpadc *gpadc, u8 input);
#endif /* _AB8500_GPADC_H */ #endif /* _AB8500_GPADC_H */