Message ID | 1392383861-14169-1-git-send-email-marek@goldelico.com |
---|---|
State | Superseded, archived |
Headers | show |
> Signed-off-by: Marek Belisko <marek@goldelico.com> > --- > .../devicetree/bindings/mfd/twl4030-madc.txt | 18 +++++++++++++ > drivers/mfd/twl4030-madc.c | 31 > ++++++++++++++++++++-- Please separate these into different patches. > 2 files changed, 47 insertions(+), 2 deletions(-) > create mode 100644 Documentation/devicetree/bindings/mfd/twl4030-madc.txt <snip> > +++ b/drivers/mfd/twl4030-madc.c > @@ -695,6 +695,29 @@ static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) > return 0; > } > > +#ifdef CONFIG_OF I believe we're heading for a more: if (IS_ENABLED(CONFIG_OF)) ... approach. I won't enforce it, but please consider using it. > +static struct twl4030_madc_platform_data * > + twl4030_madc_of_parse(struct platform_device *pdev) > +{ > + struct twl4030_madc_platform_data *pdata; > + > + pdata = devm_kzalloc(&pdev->dev, > + sizeof(struct twl4030_madc_platform_data), GFP_KERNEL); s/struct twl4030_madc_platform_data/*pdata/ > + if (!pdata) > + return ERR_PTR(-ENOMEM); > + > + pdata->irq_line = platform_get_irq(pdev, 0); Why weren't 'resources' used in the original implementation? > + return pdata; > +} > + > +static const struct of_device_id twl4030_madc_dt_match_table[] = { > + { .compatible = "ti,twl4030-madc" }, > + {}, > +}; > + > +#endif > + > /* > * Initialize MADC and request for threaded irq > */ > @@ -706,8 +729,11 @@ static int twl4030_madc_probe(struct platform_device *pdev) > u8 regval; > > if (!pdata) { > - dev_err(&pdev->dev, "platform_data not available\n"); > - return -EINVAL; > + pdata = twl4030_madc_of_parse(pdev); > + if (!pdata) { And if you received -ENOMEM? > + dev_err(&pdev->dev, "platform_data not available\n"); > + return -EINVAL; > + } > } > madc = kzalloc(sizeof(*madc), GFP_KERNEL); > if (!madc) > @@ -807,6 +833,7 @@ static struct platform_driver twl4030_madc_driver = { > .driver = { > .name = "twl4030_madc", > .owner = THIS_MODULE, > + .of_match_table = of_match_ptr(twl4030_madc_dt_match_table), > }, > }; >
On Fri, Feb 14, 2014 at 2:48 PM, Lee Jones <lee.jones@linaro.org> wrote: >> Signed-off-by: Marek Belisko <marek@goldelico.com> >> --- >> .../devicetree/bindings/mfd/twl4030-madc.txt | 18 +++++++++++++ >> drivers/mfd/twl4030-madc.c | 31 >> ++++++++++++++++++++-- > > Please separate these into different patches. OK. > >> 2 files changed, 47 insertions(+), 2 deletions(-) >> create mode 100644 Documentation/devicetree/bindings/mfd/twl4030-madc.txt > > <snip> > >> +++ b/drivers/mfd/twl4030-madc.c >> @@ -695,6 +695,29 @@ static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) >> return 0; >> } >> >> +#ifdef CONFIG_OF > > I believe we're heading for a more: > > if (IS_ENABLED(CONFIG_OF)) > > ... approach. I won't enforce it, but please consider using it. OK I'll use it in next version. > >> +static struct twl4030_madc_platform_data * >> + twl4030_madc_of_parse(struct platform_device *pdev) >> +{ >> + struct twl4030_madc_platform_data *pdata; >> + >> + pdata = devm_kzalloc(&pdev->dev, >> + sizeof(struct twl4030_madc_platform_data), GFP_KERNEL); > > s/struct twl4030_madc_platform_data/*pdata/ Right typo. > >> + if (!pdata) >> + return ERR_PTR(-ENOMEM); >> + >> + pdata->irq_line = platform_get_irq(pdev, 0); > > Why weren't 'resources' used in the original implementation? Not sure I'm not an author :). It's passed in platform data. > >> + return pdata; >> +} >> + >> +static const struct of_device_id twl4030_madc_dt_match_table[] = { >> + { .compatible = "ti,twl4030-madc" }, >> + {}, >> +}; >> + >> +#endif >> + >> /* >> * Initialize MADC and request for threaded irq >> */ >> @@ -706,8 +729,11 @@ static int twl4030_madc_probe(struct platform_device *pdev) >> u8 regval; >> >> if (!pdata) { >> - dev_err(&pdev->dev, "platform_data not available\n"); >> - return -EINVAL; >> + pdata = twl4030_madc_of_parse(pdev); >> + if (!pdata) { > > And if you received -ENOMEM? Hmm right. I'll fix that. > >> + dev_err(&pdev->dev, "platform_data not available\n"); >> + return -EINVAL; >> + } >> } >> madc = kzalloc(sizeof(*madc), GFP_KERNEL); >> if (!madc) >> @@ -807,6 +833,7 @@ static struct platform_driver twl4030_madc_driver = { >> .driver = { >> .name = "twl4030_madc", >> .owner = THIS_MODULE, >> + .of_match_table = of_match_ptr(twl4030_madc_dt_match_table), >> }, >> }; >> > > -- > Lee Jones > Linaro STMicroelectronics Landing Team Lead > Linaro.org │ Open source software for ARM SoCs > Follow Linaro: Facebook | Twitter | Blog BR, marek
> >> Signed-off-by: Marek Belisko <marek@goldelico.com> > >> --- > >> .../devicetree/bindings/mfd/twl4030-madc.txt | 18 +++++++++++++ > >> drivers/mfd/twl4030-madc.c | 31 > >> ++++++++++++++++++++-- > > <snip> > >> +static struct twl4030_madc_platform_data * > >> + twl4030_madc_of_parse(struct platform_device *pdev) > >> +{ > >> + struct twl4030_madc_platform_data *pdata; > >> + > >> + pdata = devm_kzalloc(&pdev->dev, > >> + sizeof(struct twl4030_madc_platform_data), GFP_KERNEL); > > > > s/struct twl4030_madc_platform_data/*pdata/ > Right typo. Sorry, my comment of ambiguous. I mean do this: sizeof(*pdata) ... instead of this: sizeof(struct twl4030_madc_platform_data) > >> + if (!pdata) > >> + return ERR_PTR(-ENOMEM); > >> + > >> + pdata->irq_line = platform_get_irq(pdev, 0); > > > > Why weren't 'resources' used in the original implementation? > Not sure I'm not an author :). It's passed in platform data. Yes, I saw that. It should be changed.
Hi Marek, I have prepared a patchset, which adds DT bindings for twl4030-madc using the standard IIO DT API. I have not yet send the patchset, since I have not yet found the time to test the patchset. I will send them as RFC now. -- Sebastian
Hi Sebastian, On Fri, Feb 14, 2014 at 7:46 PM, Sebastian Reichel <sre@debian.org> wrote: > This converts twl4030-madc module to use the Industrial IO ADC > framework and adds device tree support. > > Signed-off-by: Sebastian Reichel <sre@debian.org> > --- > drivers/mfd/twl4030-madc.c | 121 ++++++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 114 insertions(+), 7 deletions(-) > > diff --git a/drivers/mfd/twl4030-madc.c b/drivers/mfd/twl4030-madc.c > index 5458561..4da61c4 100644 > --- a/drivers/mfd/twl4030-madc.c > +++ b/drivers/mfd/twl4030-madc.c > @@ -47,6 +47,10 @@ > #include <linux/gfp.h> > #include <linux/err.h> > > +#include <linux/iio/iio.h> > +#include <linux/iio/machine.h> > +#include <linux/iio/driver.h> > + > /* > * struct twl4030_madc_data - a container for madc info > * @dev - pointer to device structure for madc > @@ -59,10 +63,72 @@ struct twl4030_madc_data { > struct device *dev; > struct mutex lock; /* mutex protecting this data structure */ > struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS]; > + bool use_second_irq; > int imr; > int isr; > }; > > +static int twl4030_madc_read(struct iio_dev *iio_dev, > + const struct iio_chan_spec *chan, > + int *val, int *val2, long mask) > +{ > + struct twl4030_madc_data *madc = iio_priv(iio_dev); > + struct twl4030_madc_request req; > + int channel = chan->channel; > + int ret; > + > + req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1; > + > + req.channels = BIT(channel); > + req.active = 0; > + req.func_cb = NULL; > + req.raw = (mask & IIO_CHAN_INFO_PROCESSED) ? false : true; > + req.do_avg = (mask & IIO_CHAN_INFO_AVERAGE_RAW) ? true : false; > + > + ret = twl4030_madc_conversion(&req); > + if (ret < 0) > + return ret; > + > + *val = req.rbuf[channel]; > + > + return IIO_VAL_INT; > +} > + > +static const struct iio_info twl4030_madc_iio_info = { > + .read_raw = &twl4030_madc_read, > + .driver_module = THIS_MODULE, > +}; > + > +#define ADC_CHANNEL(_channel, _type, _name, _mask) { \ > + .type = _type, \ > + .scan_type = IIO_ST('u', 10, 16, 0), \ > + .channel = _channel, \ > + .info_mask_separate = _mask, \ > + .datasheet_name = _name, \ > + .indexed = 1, \ > +} > + > +static const struct iio_chan_spec twl4030_madc_iio_channels[] = { > + ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(1, IIO_TEMP, "ADCIN1", BIT(IIO_CHAN_INFO_PROCESSED) | > + BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10", BIT(IIO_CHAN_INFO_PROCESSED) | > + BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > +}; > + > static struct twl4030_madc_data *twl4030_madc; > > struct twl4030_prescale_divider_ratios { > @@ -702,28 +768,49 @@ static int twl4030_madc_probe(struct platform_device *pdev) > { > struct twl4030_madc_data *madc; > struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev); > + struct device_node *np = pdev->dev.of_node; > int irq, ret; > u8 regval; > + struct iio_dev *iio_dev = NULL; > > - if (!pdata) { > + if (!pdata && !np) { > dev_err(&pdev->dev, "platform_data not available\n"); > return -EINVAL; > } > - madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL); > - if (!madc) > + > + iio_dev = devm_iio_device_alloc(&pdev->dev, > + sizeof(struct twl4030_madc_data)); > + if (!iio_dev) { > + dev_err(&pdev->dev, "failed allocating iio device\n"); > return -ENOMEM; > + } > > + madc = iio_priv(iio_dev); > madc->dev = &pdev->dev; > > + iio_dev->name = dev_name(&pdev->dev); > + iio_dev->dev.parent = &pdev->dev; > + iio_dev->dev.of_node = pdev->dev.of_node; > + iio_dev->info = &twl4030_madc_iio_info; > + iio_dev->modes = INDIO_DIRECT_MODE; > + iio_dev->channels = twl4030_madc_iio_channels; > + iio_dev->num_channels = 16; > + > /* > * Phoenix provides 2 interrupt lines. The first one is connected to > * the OMAP. The other one can be connected to the other processor such > * as modem. Hence two separate ISR and IMR registers. > */ > - madc->imr = (pdata->irq_line == 1) ? > + if (pdata) > + madc->use_second_irq = pdata->irq_line != 1; > + else > + madc->use_second_irq = false; Can we add some property to use second IRQ also in DT? > + > + madc->imr = (madc->use_second_irq == 1) ? > TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2; > - madc->isr = (pdata->irq_line == 1) ? > + madc->isr = (madc->use_second_irq == 1) ? > TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2; > + > ret = twl4030_madc_set_power(madc, 1); > if (ret < 0) > return ret; > @@ -768,7 +855,7 @@ static int twl4030_madc_probe(struct platform_device *pdev) > } > } > > - platform_set_drvdata(pdev, madc); > + platform_set_drvdata(pdev, iio_dev); > mutex_init(&madc->lock); > > irq = platform_get_irq(pdev, 0); > @@ -780,7 +867,15 @@ static int twl4030_madc_probe(struct platform_device *pdev) > goto err_i2c; > } > twl4030_madc = madc; > + > + ret = iio_device_register(iio_dev); > + if (ret) { > + dev_dbg(&pdev->dev, "could not register iio device\n"); > + goto err_i2c; > + } > + > return 0; > + > err_i2c: > twl4030_madc_set_current_generator(madc, 0, 0); > err_current_generator: > @@ -790,20 +885,32 @@ err_current_generator: > > static int twl4030_madc_remove(struct platform_device *pdev) > { > - struct twl4030_madc_data *madc = platform_get_drvdata(pdev); > + struct iio_dev *iio_dev = platform_get_drvdata(pdev); > + struct twl4030_madc_data *madc = iio_priv(iio_dev); > > twl4030_madc_set_current_generator(madc, 0, 0); > twl4030_madc_set_power(madc, 0); > > + iio_device_unregister(iio_dev); > + > return 0; > } > > +#ifdef CONFIG_OF > +static const struct of_device_id twl_madc_of_match[] = { > + {.compatible = "ti,twl4030-madc", }, > + { }, > +}; > +MODULE_DEVICE_TABLE(of, twl_madc_of_match); > +#endif > + > static struct platform_driver twl4030_madc_driver = { > .probe = twl4030_madc_probe, > .remove = twl4030_madc_remove, > .driver = { > .name = "twl4030_madc", > .owner = THIS_MODULE, > + .of_match_table = of_match_ptr(twl_madc_of_match), > }, > }; > > -- > 1.8.5.3 > BR, marek
Hi Sebastian, On Fri, Feb 14, 2014 at 6:40 PM, Sebastian Reichel <sre@ring0.de> wrote: > Hi Marek, > > I have prepared a patchset, which adds DT bindings for twl4030-madc > using the standard IIO DT API. I have not yet send the patchset, > since I have not yet found the time to test the patchset. I will > send them as RFC now. Great. I'll look on that today evening and test on gta04 board. Thanks. I think my patch can be dropped :). > > -- Sebastian BR, marek
Hi Marek, On Sat, Feb 15, 2014 at 02:31:41PM +0100, Belisko Marek wrote: > > /* > > * Phoenix provides 2 interrupt lines. The first one is connected to > > * the OMAP. The other one can be connected to the other processor such > > * as modem. Hence two separate ISR and IMR registers. > > */ > > - madc->imr = (pdata->irq_line == 1) ? > > + if (pdata) > > + madc->use_second_irq = pdata->irq_line != 1; > > + else > > + madc->use_second_irq = false; > > Can we add some property to use second IRQ also in DT? Sure, a boolean property like this could be added: ti,system-uses-second-madc-irq; I did not add support for it initially, since I could not find any board in the mainline kernel with this setup. If such a board is added at some later point the driver can be extended easily. -- Sebastian
On 14/02/14 18:46, Sebastian Reichel wrote: > This is a driver for an A/D converter, which belongs into > drivers/iio/adc. > > Signed-off-by: Sebastian Reichel <sre@debian.org> Being inherently lazy I'm going to review this patch as it gives the complete driver rather than taking on the conversion patch directly! It's a long shot, but are there any docs freely available for this part? Obviously that may well mean that some of my comments apply to the driver in general rather than the changes you've made. Please feel free to pick and choose! I'd like ideally to see a little more generic tidying up in this driver. As you'll notice inline I get irritated at having lots and lots of error messages. Still that's personal preference but I felt a lot more justified in moaning after finding one that was incorrect ;) There is also a bit of functionality in here that I'm not sure is ever used (the request callback functions). It complicates the code so if no using it I'd be tempted to drop it. It's nice in a way that the driver before your conversion provided only a very limited and in kernel interface given we don't have to hang on to any old ABI elements. > --- > drivers/iio/adc/Kconfig | 10 + > drivers/iio/adc/Makefile | 1 + > drivers/iio/adc/twl4030-madc.c | 922 +++++++++++++++++++++++++++++++++++++++++ > drivers/mfd/Kconfig | 10 - > drivers/mfd/Makefile | 1 - > drivers/mfd/twl4030-madc.c | 922 ----------------------------------------- > 6 files changed, 933 insertions(+), 933 deletions(-) > create mode 100644 drivers/iio/adc/twl4030-madc.c > delete mode 100644 drivers/mfd/twl4030-madc.c > > diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig > index 2209f28..427f75c 100644 > --- a/drivers/iio/adc/Kconfig > +++ b/drivers/iio/adc/Kconfig > @@ -183,6 +183,16 @@ config TI_AM335X_ADC > Say yes here to build support for Texas Instruments ADC > driver which is also a MFD client. > > +config TWL4030_MADC > + tristate "TWL4030 MADC (Monitoring A/D Converter)" > + depends on TWL4030_CORE > + help > + This driver provides support for Triton TWL4030-MADC. The > + driver supports both RT and SW conversion methods. > + > + This driver can also be built as a module. If so, the module will be > + called twl4030-madc. > + > config TWL6030_GPADC > tristate "TWL6030 GPADC (General Purpose A/D Converter) Support" > depends on TWL4030_CORE > diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile > index ba9a10a..9acf2df 100644 > --- a/drivers/iio/adc/Makefile > +++ b/drivers/iio/adc/Makefile > @@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o > obj-$(CONFIG_NAU7802) += nau7802.o > obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o > obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o > +obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o > obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o > obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o > diff --git a/drivers/iio/adc/twl4030-madc.c b/drivers/iio/adc/twl4030-madc.c > new file mode 100644 > index 0000000..4da61c4 > --- /dev/null > +++ b/drivers/iio/adc/twl4030-madc.c > @@ -0,0 +1,922 @@ > +/* > + * > + * TWL4030 MADC module driver-This driver monitors the real time > + * conversion of analog signals like battery temperature, > + * battery type, battery level etc. > + * > + * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ > + * J Keerthy <j-keerthy@ti.com> > + * > + * Based on twl4030-madc.c > + * Copyright (C) 2008 Nokia Corporation > + * Mikko Ylinen <mikko.k.ylinen@nokia.com> > + * > + * Amit Kucheria <amit.kucheria@canonical.com> > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU General Public License > + * version 2 as published by the Free Software Foundation. > + * > + * This program is distributed in the hope that it will be useful, but > + * WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * General Public License for more details. > + * > + * You should have received a copy of the GNU General Public License > + * along with this program; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA > + * 02110-1301 USA > + * > + */ > + > +#include <linux/init.h> > +#include <linux/device.h> > +#include <linux/interrupt.h> > +#include <linux/kernel.h> > +#include <linux/delay.h> > +#include <linux/platform_device.h> > +#include <linux/slab.h> > +#include <linux/i2c/twl.h> > +#include <linux/i2c/twl4030-madc.h> > +#include <linux/module.h> > +#include <linux/stddef.h> > +#include <linux/mutex.h> > +#include <linux/bitops.h> > +#include <linux/jiffies.h> > +#include <linux/types.h> > +#include <linux/gfp.h> > +#include <linux/err.h> > + > +#include <linux/iio/iio.h> Why include machine.h or driver.h. You aren't currently using the mapping coding anyway that these provide. > +#include <linux/iio/machine.h> > +#include <linux/iio/driver.h> > + > +/* > + * struct twl4030_madc_data - a container for madc info > + * @dev - pointer to device structure for madc > + * @lock - mutex protecting this data structure > + * @requests - Array of request struct corresponding to SW1, SW2 and RT > + * @imr - Interrupt mask register of MADC > + * @isr - Interrupt status register of MADC > + */ > +struct twl4030_madc_data { > + struct device *dev; > + struct mutex lock; /* mutex protecting this data structure */ > + struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS]; > + bool use_second_irq; These are 32 bit signed values? > + int imr; > + int isr; > +}; > + > +static int twl4030_madc_read(struct iio_dev *iio_dev, > + const struct iio_chan_spec *chan, > + int *val, int *val2, long mask) > +{ > + struct twl4030_madc_data *madc = iio_priv(iio_dev); > + struct twl4030_madc_request req; > + int channel = chan->channel; > + int ret; > + > + req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1; > + > + req.channels = BIT(channel); > + req.active = 0; > + req.func_cb = NULL; req.raw = !(mask & IIO_CHAN_INFO_PROCESSED); req.do_avg = !!(mask & IIO_CHAN_INFO_AVERAGE_RAW); > + req.raw = (mask & IIO_CHAN_INFO_PROCESSED) ? false : true; > + req.do_avg = (mask & IIO_CHAN_INFO_AVERAGE_RAW) ? true : false; > + > + ret = twl4030_madc_conversion(&req); > + if (ret < 0) > + return ret; > + > + *val = req.rbuf[channel]; > + > + return IIO_VAL_INT; > +} > + > +static const struct iio_info twl4030_madc_iio_info = { > + .read_raw = &twl4030_madc_read, > + .driver_module = THIS_MODULE, > +}; > + Please give this a prefixed name. Chances of ADC_CHANNEL turning up in an IIO header is a little too high to do this without! e.g. #define TWL4030_ADC_CHANNEL > +#define ADC_CHANNEL(_channel, _type, _name, _mask) { \ > + .type = _type, \ I don't think you use scan_type anywhere so don't bother specifying it. here. > + .scan_type = IIO_ST('u', 10, 16, 0), \ > + .channel = _channel, \ > + .info_mask_separate = _mask, \ > + .datasheet_name = _name, \ > + .indexed = 1, \ > +} > + Is the average have an adjustable number of samples? If not I'd be tempted to use the more common option of IIO_CHAN_INFO_RAW rather than the average version (tends only to be used in fairly obscure cases where both a raw access and an averaged one are available). > +static const struct iio_chan_spec twl4030_madc_iio_channels[] = { > + ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(1, IIO_TEMP, "ADCIN1", BIT(IIO_CHAN_INFO_PROCESSED) | > + BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10", BIT(IIO_CHAN_INFO_PROCESSED) | > + BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > + ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > +}; > + Why the artificial limitation of one of these devices? I guess this is to allow the exported function to work without needing to be associated with any particular device... Hmm. > +static struct twl4030_madc_data *twl4030_madc; > + > +struct twl4030_prescale_divider_ratios { > + s16 numerator; > + s16 denominator; > +}; > + > +static const struct twl4030_prescale_divider_ratios > +twl4030_divider_ratios[16] = { > + {1, 1}, /* CHANNEL 0 No Prescaler */ > + {1, 1}, /* CHANNEL 1 No Prescaler */ > + {6, 10}, /* CHANNEL 2 */ > + {6, 10}, /* CHANNEL 3 */ > + {6, 10}, /* CHANNEL 4 */ > + {6, 10}, /* CHANNEL 5 */ > + {6, 10}, /* CHANNEL 6 */ > + {6, 10}, /* CHANNEL 7 */ > + {3, 14}, /* CHANNEL 8 */ > + {1, 3}, /* CHANNEL 9 */ > + {1, 1}, /* CHANNEL 10 No Prescaler */ > + {15, 100}, /* CHANNEL 11 */ > + {1, 4}, /* CHANNEL 12 */ > + {1, 1}, /* CHANNEL 13 Reserved channels */ > + {1, 1}, /* CHANNEL 14 Reseved channels */ > + {5, 11}, /* CHANNEL 15 */ > +}; > + > + > +/* > + * Conversion table from -3 to 55 degree Celcius > + */ > +static int therm_tbl[] = { > +30800, 29500, 28300, 27100, > +26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700, 17900, > +17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100, 12600, 12100, > +11600, 11200, 10800, 10400, 10000, 9630, 9280, 8950, 8620, 8310, > +8020, 7730, 7460, 7200, 6950, 6710, 6470, 6250, 6040, 5830, > +5640, 5450, 5260, 5090, 4920, 4760, 4600, 4450, 4310, 4170, > +4040, 3910, 3790, 3670, 3550 > +}; > + > +/* > + * Structure containing the registers > + * of different conversion methods supported by MADC. > + * Hardware or RT real time conversion request initiated by external host > + * processor for RT Signal conversions. > + * External host processors can also request for non RT conversions > + * SW1 and SW2 software conversions also called asynchronous or GPC request. > + */ > +static > +const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = { > + [TWL4030_MADC_RT] = { > + .sel = TWL4030_MADC_RTSELECT_LSB, > + .avg = TWL4030_MADC_RTAVERAGE_LSB, > + .rbase = TWL4030_MADC_RTCH0_LSB, > + }, > + [TWL4030_MADC_SW1] = { > + .sel = TWL4030_MADC_SW1SELECT_LSB, > + .avg = TWL4030_MADC_SW1AVERAGE_LSB, > + .rbase = TWL4030_MADC_GPCH0_LSB, > + .ctrl = TWL4030_MADC_CTRL_SW1, > + }, > + [TWL4030_MADC_SW2] = { > + .sel = TWL4030_MADC_SW2SELECT_LSB, > + .avg = TWL4030_MADC_SW2AVERAGE_LSB, > + .rbase = TWL4030_MADC_GPCH0_LSB, > + .ctrl = TWL4030_MADC_CTRL_SW2, > + }, > +}; > + > +/* > + * Function to read a particular channel value. > + * @madc - pointer to struct twl4030_madc_data > + * @reg - lsb of ADC Channel > + * If the i2c read fails it returns an error else returns 0. > + */ > +static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg) > +{ > + u8 msb, lsb; > + int ret; > + /* > + * For each ADC channel, we have MSB and LSB register pair. MSB address > + * is always LSB address+1. reg parameter is the address of LSB register > + */ > + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1); > + if (ret) { > + dev_err(madc->dev, "unable to read MSB register 0x%X\n", > + reg + 1); > + return ret; > + } > + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg); > + if (ret) { > + dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg); > + return ret; > + } > + > + return (int)(((msb << 8) | lsb) >> 6); > +} > + > +/* > + * Return battery temperature > + * Or < 0 on failure. > + */ > +static int twl4030battery_temperature(int raw_volt) > +{ > + u8 val; > + int temp, curr, volt, res, ret; > + > + volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R; > + /* Getting and calculating the supply current in micro ampers */ > + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, > + REG_BCICTL2); > + if (ret < 0) > + return ret; blank line here and after similar error cases makes it a little easier to read. > + curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10; > + /* Getting and calculating the thermistor resistance in ohms */ > + res = volt * 1000 / curr; > + /* calculating temperature */ > + for (temp = 58; temp >= 0; temp--) { > + int actual = therm_tbl[temp]; > + No blank line here. > + if ((actual - res) >= 0) > + break; > + } > + > + return temp + 1; > +} > + > +static int twl4030battery_current(int raw_volt) > +{ > + int ret; > + u8 val; > + > + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, > + TWL4030_BCI_BCICTL1); > + if (ret) > + return ret; > + if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */ > + return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1; > + else /* slope of 0.88 mV/mA */ > + return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2; > +} blank line here please. Various comments on this in the next function: This would be much simpler if any error immediately resulted in an exit with error code. If it's gone wrong enough to issue a dev_err then don't try to muddle through. Given there is nothing to clear up in here, just error out directly on the first problem. > +/* > + * Function to read channel values > + * @madc - pointer to twl4030_madc_data struct > + * @reg_base - Base address of the first channel > + * @Channels - 16 bit bitmap. If the bit is set, channel value is read > + * @buf - The channel values are stored here. if read fails error > + * @raw - Return raw values without conversion > + * value is stored > + * Returns the number of successfully read channels. > + */ > +static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, > + u8 reg_base, unsigned > + long channels, int *buf, > + bool raw) > +{ > + int count = 0, count_req = 0, i; > + u8 reg; > + > + for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) { Skip the temporary for reg. Yes, it gets used twice, but easier to read inline. > + reg = reg_base + 2 * i; > + buf[i] = twl4030_madc_channel_raw_read(madc, reg); > + if (buf[i] < 0) { > + dev_err(madc->dev, > + "Unable to read register 0x%X\n", reg); > + count_req++; > + continue; > + } I'd prefer this as if (raw) { count ++; } else { switch (i) { .. } Also near as I can tell count gets incremented unless there is an error anyway. So just increment it outside and allow this function to return the error code. > + if (raw) { > + count++; > + continue; > + } > + switch (i) { These cases could do with a suitable #define to give them a name. > + case 10: > + buf[i] = twl4030battery_current(buf[i]); > + if (buf[i] < 0) { > + dev_err(madc->dev, "err reading current\n"); > + count_req++; > + } else { > + count++; > + buf[i] = buf[i] - 750; > + } > + break; > + case 1: > + buf[i] = twl4030battery_temperature(buf[i]); > + if (buf[i] < 0) { > + dev_err(madc->dev, "err reading temperature\n"); > + count_req++; > + } else { > + buf[i] -= 3; > + count++; > + } > + break; > + default: > + count++; > + /* Analog Input (V) = conv_result * step_size / R > + * conv_result = decimal value of 10-bit conversion > + * result > + * step size = 1.5 / (2 ^ 10 -1) > + * R = Prescaler ratio for input channels. > + * Result given in mV hence multiplied by 1000. > + */ > + buf[i] = (buf[i] * 3 * 1000 * > + twl4030_divider_ratios[i].denominator) > + / (2 * 1023 * > + twl4030_divider_ratios[i].numerator); > + } > + } This is already apparant from the errors emmited earlier. I'd drop this and the count_req counter in general. Personally I'd error out in those cases anyway rather than carrying on with the rest of the channels. If it's worth of a dev_err it's worthy of getting out as fast as possible rather than trying to muddle on. > + if (count_req) > + dev_err(madc->dev, "%d channel conversion failed\n", count_req); > + > + return count; > +} > + > +/* > + * Enables irq. > + * @madc - pointer to twl4030_madc_data struct > + * @id - irq number to be enabled > + * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2 > + * corresponding to RT, SW1, SW2 conversion requests. > + * If the i2c read fails it returns an error else returns 0. > + */ > +static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id) > +{ > + u8 val; > + int ret; > + > + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr); > + if (ret) { > + dev_err(madc->dev, "unable to read imr register 0x%X\n", > + madc->imr); > + return ret; > + } A blank line here and in similar cases would slight aid readability. > + val &= ~(1 << id); > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); > + if (ret) { > + dev_err(madc->dev, > + "unable to write imr register 0x%X\n", madc->imr); > + return ret; > + Loose blank line here. > + } > + > + return 0; > +} > + > +/* > + * Disables irq. > + * @madc - pointer to twl4030_madc_data struct > + * @id - irq number to be disabled > + * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2 > + * corresponding to RT, SW1, SW2 conversion requests. > + * Returns error if i2c read/write fails. > + */ > +static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id) > +{ > + u8 val; > + int ret; > + > + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr); > + if (ret) { > + dev_err(madc->dev, "unable to read imr register 0x%X\n", > + madc->imr); > + return ret; > + } > + val |= (1 << id); > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); > + if (ret) { > + dev_err(madc->dev, > + "unable to write imr register 0x%X\n", madc->imr); > + return ret; > + } > + > + return 0; > +} > + > +static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc) > +{ > + struct twl4030_madc_data *madc = _madc; > + const struct twl4030_madc_conversion_method *method; > + u8 isr_val, imr_val; > + int i, len, ret; > + struct twl4030_madc_request *r; > + > + mutex_lock(&madc->lock); > + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr); > + if (ret) { > + dev_err(madc->dev, "unable to read isr register 0x%X\n", > + madc->isr); > + goto err_i2c; > + } > + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr); > + if (ret) { > + dev_err(madc->dev, "unable to read imr register 0x%X\n", > + madc->imr); > + goto err_i2c; > + } > + isr_val &= ~imr_val; > + for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { > + if (!(isr_val & (1 << i))) > + continue; > + ret = twl4030_madc_disable_irq(madc, i); > + if (ret < 0) > + dev_dbg(madc->dev, "Disable interrupt failed%d\n", i); > + madc->requests[i].result_pending = 1; > + } Can this and the previous loop not be combined thus simplifying some tests? > + for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { > + r = &madc->requests[i]; > + /* No pending results for this method, move to next one */ > + if (!r->result_pending) > + continue; > + method = &twl4030_conversion_methods[r->method]; > + /* Read results */ > + len = twl4030_madc_read_channels(madc, method->rbase, > + r->channels, r->rbuf, r->raw); > + /* Return results to caller */ > + if (r->func_cb != NULL) { > + r->func_cb(len, r->channels, r->rbuf); > + r->func_cb = NULL; > + } > + /* Free request */ > + r->result_pending = 0; > + r->active = 0; > + } > + mutex_unlock(&madc->lock); > + > + return IRQ_HANDLED; > + > +err_i2c: > + /* > + * In case of error check whichever request is active > + * and service the same. > + */ > + for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { > + r = &madc->requests[i]; > + if (r->active == 0) > + continue; > + method = &twl4030_conversion_methods[r->method]; > + /* Read results */ > + len = twl4030_madc_read_channels(madc, method->rbase, > + r->channels, r->rbuf, r->raw); > + /* Return results to caller */ > + if (r->func_cb != NULL) { > + r->func_cb(len, r->channels, r->rbuf); > + r->func_cb = NULL; > + } > + /* Free request */ > + r->result_pending = 0; > + r->active = 0; > + } > + mutex_unlock(&madc->lock); > + > + return IRQ_HANDLED; > +} > + This structure does seem a little complex. I'd have been more tempted by using a completion and having the calling function wait on it. How often does an actual callback make sense? > +static int twl4030_madc_set_irq(struct twl4030_madc_data *madc, > + struct twl4030_madc_request *req) > +{ > + struct twl4030_madc_request *p; > + int ret; > + > + p = &madc->requests[req->method]; > + memcpy(p, req, sizeof(*req)); > + ret = twl4030_madc_enable_irq(madc, req->method); > + if (ret < 0) { > + dev_err(madc->dev, "enable irq failed!!\n"); > + return ret; > + } > + > + return 0; > +} > + > +/* > + * Function which enables the madc conversion > + * by writing to the control register. > + * @madc - pointer to twl4030_madc_data struct > + * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1 > + * corresponding to RT SW1 or SW2 conversion methods. > + * Returns 0 if succeeds else a negative error value > + */ > +static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc, > + int conv_method) > +{ > + const struct twl4030_madc_conversion_method *method; > + int ret = 0; > + method = &twl4030_conversion_methods[conv_method]; > + switch (conv_method) { Can we get here via any paths where these aren't the methods set? > + case TWL4030_MADC_SW1: > + case TWL4030_MADC_SW2: > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, > + TWL4030_MADC_SW_START, method->ctrl); > + if (ret) { > + dev_err(madc->dev, > + "unable to write ctrl register 0x%X\n", > + method->ctrl); > + return ret; > + } > + break; > + default: > + break; > + } > + > + return 0; > +} > + Could we fix up the kernel doc in this file in general. It's so nearly there but not quite! > +/* > + * Function that waits for conversion to be ready > + * @madc - pointer to twl4030_madc_data struct > + * @timeout_ms - timeout value in milliseconds > + * @status_reg - ctrl register > + * returns 0 if succeeds else a negative error value > + */ > +static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc, > + unsigned int timeout_ms, > + u8 status_reg) > +{ > + unsigned long timeout; > + int ret; > + > + timeout = jiffies + msecs_to_jiffies(timeout_ms); > + do { > + u8 reg; > + > + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, ®, status_reg); > + if (ret) { > + dev_err(madc->dev, > + "unable to read status register 0x%X\n", > + status_reg); > + return ret; > + } > + if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW)) > + return 0; > + usleep_range(500, 2000); > + } while (!time_after(jiffies, timeout)); > + dev_err(madc->dev, "conversion timeout!\n"); > + > + return -EAGAIN; > +} > + > +/* > + * An exported function which can be called from other kernel drivers. What uses this currently? Ideally we'd do this through the standard IIO paths for in kernel users. Right now the device tree mappings for that are less than ideal (ask Mark Rutland if you want some choice comments ;) Perhaps we take the view this can be cleaned up later. There are some elements in here we haven't yet looked at supporting for client drivers. Will have to think about that. It would be particularly nice if possible to use a generic battery driver for the two cases that seem to be using this functionality at the moment. > + * @req twl4030_madc_request structure > + * req->rbuf will be filled with read values of channels based on the > + * channel index. If a particular channel reading fails there will > + * be a negative error value in the corresponding array element. > + * returns 0 if succeeds else error value > + */ > +int twl4030_madc_conversion(struct twl4030_madc_request *req) > +{ > + const struct twl4030_madc_conversion_method *method; > + u8 ch_msb, ch_lsb; > + int ret; > + > + if (!req || !twl4030_madc) > + return -EINVAL; > + > + mutex_lock(&twl4030_madc->lock); > + if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) { > + ret = -EINVAL; > + goto out; > + } > + /* Do we have a conversion request ongoing */ > + if (twl4030_madc->requests[req->method].active) { > + ret = -EBUSY; > + goto out; > + } > + ch_msb = (req->channels >> 8) & 0xff; > + ch_lsb = req->channels & 0xff; > + method = &twl4030_conversion_methods[req->method]; > + /* Select channels to be converted */ > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1); > + if (ret) { > + dev_err(twl4030_madc->dev, > + "unable to write sel register 0x%X\n", method->sel + 1); > + goto out; > + } > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel); > + if (ret) { > + dev_err(twl4030_madc->dev, > + "unable to write sel register 0x%X\n", method->sel + 1); > + goto out; > + } > + /* Select averaging for all channels if do_avg is set */ > + if (req->do_avg) { > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, > + ch_msb, method->avg + 1); > + if (ret) { > + dev_err(twl4030_madc->dev, > + "unable to write avg register 0x%X\n", > + method->avg + 1); > + goto out; > + } > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, > + ch_lsb, method->avg); > + if (ret) { > + dev_err(twl4030_madc->dev, Excesive error messages are sometimes irritating as they break up code flow. They are really irritating when incorrect ;) > + "unable to write sel reg 0x%X\n", > + method->sel + 1); > + goto out; > + } > + } > + if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) { > + ret = twl4030_madc_set_irq(twl4030_madc, req); > + if (ret < 0) > + goto out; > + ret = twl4030_madc_start_conversion(twl4030_madc, req->method); > + if (ret < 0) > + goto out; > + twl4030_madc->requests[req->method].active = 1; > + ret = 0; > + goto out; > + } Is it possible to get here? This comment suggests not. If so, why have the test? > + /* With RT method we should not be here anymore */ > + if (req->method == TWL4030_MADC_RT) { > + ret = -EINVAL; > + goto out; > + } > + ret = twl4030_madc_start_conversion(twl4030_madc, req->method); > + if (ret < 0) > + goto out; > + twl4030_madc->requests[req->method].active = 1; > + /* Wait until conversion is ready (ctrl register returns EOC) */ So no interrupts in this case? > + ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl); > + if (ret) { > + twl4030_madc->requests[req->method].active = 0; > + goto out; > + } > + ret = twl4030_madc_read_channels(twl4030_madc, method->rbase, > + req->channels, req->rbuf, req->raw); > + twl4030_madc->requests[req->method].active = 0; > + > +out: > + mutex_unlock(&twl4030_madc->lock); > + > + return ret; > +} > +EXPORT_SYMBOL_GPL(twl4030_madc_conversion); > + > +/* Proper kerneldoc would be nice here. Otherwise the comment doesn't really add anything so I'd be tempted to drop it ;) > + * Return channel value > + * Or < 0 on failure. > + */ > +int twl4030_get_madc_conversion(int channel_no) > +{ > + struct twl4030_madc_request req; > + int temp = 0; > + int ret; > + > + req.channels = (1 << channel_no); > + req.method = TWL4030_MADC_SW2; > + req.active = 0; > + req.func_cb = NULL; > + ret = twl4030_madc_conversion(&req); > + if (ret < 0) > + return ret; > + if (req.rbuf[channel_no] > 0) > + temp = req.rbuf[channel_no]; > + > + return temp; > +} > +EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion); > + > +/* > + * Function to enable or disable bias current for > + * main battery type reading or temperature sensing > + * @madc - pointer to twl4030_madc_data struct > + * @chan - can be one of the two values What is a battery type reading? Voltage? Current? Charge? > + * TWL4030_BCI_ITHEN - Enables bias current for main battery type reading > + * TWL4030_BCI_TYPEN - Enables bias current for main battery temperature These constants have rather incomprehensible names. Whilst I gues they are straight off the data sheet, seems to me that we could make them a little longer and easier to follow! The comment here helps, but sensible names would be better! > + * sensing > + * @on - enable or disable chan. > + */ > +static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc, > + int chan, int on) > +{ > + int ret; > + u8 regval; > + > + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, > + ®val, TWL4030_BCI_BCICTL1); > + if (ret) { > + dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X", > + TWL4030_BCI_BCICTL1); > + return ret; > + } Introduce an intermediate variable and this could be nice and easy to read Say int regmask; if (chan == 0) regmask = TWL4030_BCI_TYPEN; else regmask = TWL4030_BFI_ITHEN; if (on) regval |= regmask; else regval &= ~regmask; It's longer but doesn't give me a headache. > + if (on) > + regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN; > + else > + regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN; > + ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, > + regval, TWL4030_BCI_BCICTL1); > + if (ret) { > + dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n", > + TWL4030_BCI_BCICTL1); > + return ret; > + } > + > + return 0; > +} > + > +/* > + * Function that sets MADC software power on bit to enable MADC > + * @madc - pointer to twl4030_madc_data struct > + * @on - Enable or disable MADC software powen on bit. > + * returns error if i2c read/write fails else 0 > + */ > +static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) > +{ > + u8 regval; > + int ret; > + > + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, > + ®val, TWL4030_MADC_CTRL1); > + if (ret) { > + dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n", > + TWL4030_MADC_CTRL1); > + return ret; > + } > + if (on) > + regval |= TWL4030_MADC_MADCON; > + else > + regval &= ~TWL4030_MADC_MADCON; > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1); > + if (ret) { > + dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n", > + TWL4030_MADC_CTRL1); > + return ret; > + } > + > + return 0; > +} > + > +/* > + * Initialize MADC and request for threaded irq > + */ > +static int twl4030_madc_probe(struct platform_device *pdev) > +{ > + struct twl4030_madc_data *madc; > + struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev); > + struct device_node *np = pdev->dev.of_node; > + int irq, ret; > + u8 regval; > + struct iio_dev *iio_dev = NULL; > + > + if (!pdata && !np) { > + dev_err(&pdev->dev, "platform_data not available\n"); > + return -EINVAL; > + } > + > + iio_dev = devm_iio_device_alloc(&pdev->dev, > + sizeof(struct twl4030_madc_data)); > + if (!iio_dev) { > + dev_err(&pdev->dev, "failed allocating iio device\n"); > + return -ENOMEM; > + } > + > + madc = iio_priv(iio_dev); > + madc->dev = &pdev->dev; > + > + iio_dev->name = dev_name(&pdev->dev); > + iio_dev->dev.parent = &pdev->dev; > + iio_dev->dev.of_node = pdev->dev.of_node; > + iio_dev->info = &twl4030_madc_iio_info; > + iio_dev->modes = INDIO_DIRECT_MODE; > + iio_dev->channels = twl4030_madc_iio_channels; > + iio_dev->num_channels = 16; > + > + /* > + * Phoenix provides 2 interrupt lines. The first one is connected to > + * the OMAP. The other one can be connected to the other processor such > + * as modem. Hence two separate ISR and IMR registers. > + */ > + if (pdata) > + madc->use_second_irq = pdata->irq_line != 1; > + else > + madc->use_second_irq = false; > + > + madc->imr = (madc->use_second_irq == 1) ? > + TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2; > + madc->isr = (madc->use_second_irq == 1) ? > + TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2; > + > + ret = twl4030_madc_set_power(madc, 1); > + if (ret < 0) > + return ret; > + ret = twl4030_madc_set_current_generator(madc, 0, 1); > + if (ret < 0) > + goto err_current_generator; > + > + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, > + ®val, TWL4030_BCI_BCICTL1); > + if (ret) { > + dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n", > + TWL4030_BCI_BCICTL1); > + goto err_i2c; > + } > + regval |= TWL4030_BCI_MESBAT; > + ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, > + regval, TWL4030_BCI_BCICTL1); > + if (ret) { > + dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n", > + TWL4030_BCI_BCICTL1); > + goto err_i2c; > + } > + > + /* Check that MADC clock is on */ > + ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, ®val, TWL4030_REG_GPBR1); > + if (ret) { Personally I'd rank the driver as rather to vebose on read error messages given they are pretty unusual. Ah well I guess each to their own. > + dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n", > + TWL4030_REG_GPBR1); > + goto err_i2c; > + } > + > + /* If MADC clk is not on, turn it on */ > + if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) { > + dev_info(&pdev->dev, "clk disabled, enabling\n"); > + regval |= TWL4030_GPBR1_MADC_HFCLK_EN; > + ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval, > + TWL4030_REG_GPBR1); > + if (ret) { > + dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n", > + TWL4030_REG_GPBR1); > + goto err_i2c; > + } > + } > + > + platform_set_drvdata(pdev, iio_dev); > + mutex_init(&madc->lock); > + > + irq = platform_get_irq(pdev, 0); As ever using devm for a irq request makes for a bit of a review nightmare as we have to be very careful that nothing has been removed that this might use before the devm cleanup occurs (at end of remove). In this case the fact that the current generator is off and the adc unit is disabled sounds to me like it might cause interesting results in the interrupt handler? It's one of those things that will probably never happen but I find it hard to convince myself 'can't happen' > + ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, > + twl4030_madc_threaded_irq_handler, > + IRQF_TRIGGER_RISING, "twl4030_madc", madc); > + if (ret) { > + dev_dbg(&pdev->dev, "could not request irq\n"); > + goto err_i2c; > + } Err, where is this defined? Ah, found it above. I'm not keen on preventing multiple instances of a device like this. It is so easy to not do this I'd prefer to see the driver fixed to remove this. > + twl4030_madc = madc; > + > + ret = iio_device_register(iio_dev); > + if (ret) { > + dev_dbg(&pdev->dev, "could not register iio device\n"); > + goto err_i2c; > + } > + > + return 0; > + > +err_i2c: > + twl4030_madc_set_current_generator(madc, 0, 0); > +err_current_generator: > + twl4030_madc_set_power(madc, 0); > + return ret; > +} > + > +static int twl4030_madc_remove(struct platform_device *pdev) > +{ > + struct iio_dev *iio_dev = platform_get_drvdata(pdev); > + struct twl4030_madc_data *madc = iio_priv(iio_dev); > + > + twl4030_madc_set_current_generator(madc, 0, 0); > + twl4030_madc_set_power(madc, 0); > + > + iio_device_unregister(iio_dev); > + > + return 0; > +} > + > +#ifdef CONFIG_OF > +static const struct of_device_id twl_madc_of_match[] = { > + {.compatible = "ti,twl4030-madc", }, > + { }, > +}; > +MODULE_DEVICE_TABLE(of, twl_madc_of_match); > +#endif > + > +static struct platform_driver twl4030_madc_driver = { > + .probe = twl4030_madc_probe, > + .remove = twl4030_madc_remove, > + .driver = { > + .name = "twl4030_madc", > + .owner = THIS_MODULE, > + .of_match_table = of_match_ptr(twl_madc_of_match), > + }, > +}; > + > +module_platform_driver(twl4030_madc_driver); > + > +MODULE_DESCRIPTION("TWL4030 ADC driver"); > +MODULE_LICENSE("GPL"); > +MODULE_AUTHOR("J Keerthy"); > +MODULE_ALIAS("platform:twl4030_madc"); Rest of patch was removal of code so no comments! -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On Sat, Feb 22, 2014 at 12:47:03PM +0000, Jonathan Cameron wrote: > On 14/02/14 18:46, Sebastian Reichel wrote: > >This is a driver for an A/D converter, which belongs into > >drivers/iio/adc. > > > >Signed-off-by: Sebastian Reichel <sre@debian.org> > > Being inherently lazy I'm going to review this patch as it gives the complete > driver rather than taking on the conversion patch directly! OK. > It's a long shot, but are there any docs freely available for this part? There is documentation on Texas Instrument's website for TPS65950, which is supposed to be very similar to the TWL4030 and contains a section about the MADC module: http://www.ti.com/lit/gpn/tps65950 > Obviously that may well mean that some of my comments apply to the driver > in general rather than the changes you've made. Please feel free to > pick and choose! Having skipped over your comments I guess 70% are about the driver in general, but I can add another patch to the patchset, which fixes the driver style. > I'd like ideally to see a little more generic tidying up in this driver. > As you'll notice inline I get irritated at having lots and lots of error > messages. Still that's personal preference but I felt a lot more justified > in moaning after finding one that was incorrect ;) > > There is also a bit of functionality in here that I'm not sure is ever > used (the request callback functions). It complicates the code so if no > using it I'd be tempted to drop it. I think the optimal workflow is: 1. convert madc driver to IIO 2. convert twl4030-madc-battery driver to IIO API 3. convert rx51-battery to IIO API 4. convert twl4030-madc-hwmon to IIO API / deprecate it 5. remove old in-kernel ABI from madc 6. cleanup/simplify madc I guess its much simpler to do the driver cleanup/simplification once we can get rid of the old API. > It's nice in a way that the driver before your conversion provided only > a very limited and in kernel interface given we don't have to hang on to > any old ABI elements. > > >--- > > drivers/iio/adc/Kconfig | 10 + > > drivers/iio/adc/Makefile | 1 + > > drivers/iio/adc/twl4030-madc.c | 922 +++++++++++++++++++++++++++++++++++++++++ > > drivers/mfd/Kconfig | 10 - > > drivers/mfd/Makefile | 1 - > > drivers/mfd/twl4030-madc.c | 922 ----------------------------------------- > > 6 files changed, 933 insertions(+), 933 deletions(-) > > create mode 100644 drivers/iio/adc/twl4030-madc.c > > delete mode 100644 drivers/mfd/twl4030-madc.c > > > >diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig > >index 2209f28..427f75c 100644 > >--- a/drivers/iio/adc/Kconfig > >+++ b/drivers/iio/adc/Kconfig > >@@ -183,6 +183,16 @@ config TI_AM335X_ADC > > Say yes here to build support for Texas Instruments ADC > > driver which is also a MFD client. > > > >+config TWL4030_MADC > >+ tristate "TWL4030 MADC (Monitoring A/D Converter)" > >+ depends on TWL4030_CORE > >+ help > >+ This driver provides support for Triton TWL4030-MADC. The > >+ driver supports both RT and SW conversion methods. > >+ > >+ This driver can also be built as a module. If so, the module will be > >+ called twl4030-madc. > >+ > > config TWL6030_GPADC > > tristate "TWL6030 GPADC (General Purpose A/D Converter) Support" > > depends on TWL4030_CORE > >diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile > >index ba9a10a..9acf2df 100644 > >--- a/drivers/iio/adc/Makefile > >+++ b/drivers/iio/adc/Makefile > >@@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o > > obj-$(CONFIG_NAU7802) += nau7802.o > > obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o > > obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o > >+obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o > > obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o > > obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o > >diff --git a/drivers/iio/adc/twl4030-madc.c b/drivers/iio/adc/twl4030-madc.c > >new file mode 100644 > >index 0000000..4da61c4 > >--- /dev/null > >+++ b/drivers/iio/adc/twl4030-madc.c > >@@ -0,0 +1,922 @@ > >+/* > >+ * > >+ * TWL4030 MADC module driver-This driver monitors the real time > >+ * conversion of analog signals like battery temperature, > >+ * battery type, battery level etc. > >+ * > >+ * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ > >+ * J Keerthy <j-keerthy@ti.com> > >+ * > >+ * Based on twl4030-madc.c > >+ * Copyright (C) 2008 Nokia Corporation > >+ * Mikko Ylinen <mikko.k.ylinen@nokia.com> > >+ * > >+ * Amit Kucheria <amit.kucheria@canonical.com> > >+ * > >+ * This program is free software; you can redistribute it and/or > >+ * modify it under the terms of the GNU General Public License > >+ * version 2 as published by the Free Software Foundation. > >+ * > >+ * This program is distributed in the hope that it will be useful, but > >+ * WITHOUT ANY WARRANTY; without even the implied warranty of > >+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > >+ * General Public License for more details. > >+ * > >+ * You should have received a copy of the GNU General Public License > >+ * along with this program; if not, write to the Free Software > >+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA > >+ * 02110-1301 USA > >+ * > >+ */ > >+ > >+#include <linux/init.h> > >+#include <linux/device.h> > >+#include <linux/interrupt.h> > >+#include <linux/kernel.h> > >+#include <linux/delay.h> > >+#include <linux/platform_device.h> > >+#include <linux/slab.h> > >+#include <linux/i2c/twl.h> > >+#include <linux/i2c/twl4030-madc.h> > >+#include <linux/module.h> > >+#include <linux/stddef.h> > >+#include <linux/mutex.h> > >+#include <linux/bitops.h> > >+#include <linux/jiffies.h> > >+#include <linux/types.h> > >+#include <linux/gfp.h> > >+#include <linux/err.h> > >+ > >+#include <linux/iio/iio.h> > Why include machine.h or driver.h. You aren't currently using the > mapping coding anyway that these provide. My bad. I copied that over from another driver without checking if it is actually needed. > >+#include <linux/iio/machine.h> > >+#include <linux/iio/driver.h> > >+ > >+/* > >+ * struct twl4030_madc_data - a container for madc info > >+ * @dev - pointer to device structure for madc > >+ * @lock - mutex protecting this data structure > >+ * @requests - Array of request struct corresponding to SW1, SW2 and RT > >+ * @imr - Interrupt mask register of MADC > >+ * @isr - Interrupt status register of MADC > >+ */ > >+struct twl4030_madc_data { > >+ struct device *dev; > >+ struct mutex lock; /* mutex protecting this data structure */ > >+ struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS]; > >+ bool use_second_irq; > > These are 32 bit signed values? These were already defined as int in the original driver. I will change these to u8 in an additional patch. > >+ int imr; > >+ int isr; > >+}; > >+ > >+static int twl4030_madc_read(struct iio_dev *iio_dev, > >+ const struct iio_chan_spec *chan, > >+ int *val, int *val2, long mask) > >+{ > >+ struct twl4030_madc_data *madc = iio_priv(iio_dev); > >+ struct twl4030_madc_request req; > >+ int channel = chan->channel; > >+ int ret; > >+ > >+ req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1; > >+ > >+ req.channels = BIT(channel); > >+ req.active = 0; > >+ req.func_cb = NULL; > req.raw = !(mask & IIO_CHAN_INFO_PROCESSED); > req.do_avg = !!(mask & IIO_CHAN_INFO_AVERAGE_RAW); Right. > >+ req.raw = (mask & IIO_CHAN_INFO_PROCESSED) ? false : true; > >+ req.do_avg = (mask & IIO_CHAN_INFO_AVERAGE_RAW) ? true : false; > >+ > >+ ret = twl4030_madc_conversion(&req); > >+ if (ret < 0) > >+ return ret; > >+ > >+ *val = req.rbuf[channel]; > >+ > >+ return IIO_VAL_INT; > >+} > >+ > >+static const struct iio_info twl4030_madc_iio_info = { > >+ .read_raw = &twl4030_madc_read, > >+ .driver_module = THIS_MODULE, > >+}; > >+ > Please give this a prefixed name. Chances of ADC_CHANNEL > turning up in an IIO header is a little too high to do this > without! e.g. #define TWL4030_ADC_CHANNEL Probably drivers/iio/adc/exynos_adc.c should also be fixed. > >+#define ADC_CHANNEL(_channel, _type, _name, _mask) { \ > >+ .type = _type, \ > I don't think you use scan_type anywhere so don't bother specifying it. > here. ok. > >+ .scan_type = IIO_ST('u', 10, 16, 0), \ > >+ .channel = _channel, \ > >+ .info_mask_separate = _mask, \ > >+ .datasheet_name = _name, \ > >+ .indexed = 1, \ > >+} > >+ > > Is the average have an adjustable number of samples? The TWL supports reading a value directly (1 sample) or reading the average of 4 samples. > If not I'd be tempted to use the more common option of IIO_CHAN_INFO_RAW > rather than the average version (tends only to be used in fairly obscure > cases where both a raw access and an averaged one are available). currently some drivers use the averaged read and some use the direct read. > >+static const struct iio_chan_spec twl4030_madc_iio_channels[] = { > >+ ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(1, IIO_TEMP, "ADCIN1", BIT(IIO_CHAN_INFO_PROCESSED) | > >+ BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10", BIT(IIO_CHAN_INFO_PROCESSED) | > >+ BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+ ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >+}; > >+ > Why the artificial limitation of one of these devices? I guess this is to > allow the exported function to work without needing to be associated with > any particular device... Hmm. Artificial limitation? The madc is a ADC, which has some special functions defined for some pins in the datasheet: ADC0 = Battery Voltage ADC1 = Battery Temperature ADC10 = Battery Current Nokia misused some of those pins on the Nokia N900, though. For example they have their own conversion tables for the temperature, which is not compatible with the generic one for the madc module. I planned to convert the rx51-battery driver to simply read the raw values. All other users can use the processed information. > >+static struct twl4030_madc_data *twl4030_madc; > >+ > >+struct twl4030_prescale_divider_ratios { > >+ s16 numerator; > >+ s16 denominator; > >+}; > >+ > >+static const struct twl4030_prescale_divider_ratios > >+twl4030_divider_ratios[16] = { > >+ {1, 1}, /* CHANNEL 0 No Prescaler */ > >+ {1, 1}, /* CHANNEL 1 No Prescaler */ > >+ {6, 10}, /* CHANNEL 2 */ > >+ {6, 10}, /* CHANNEL 3 */ > >+ {6, 10}, /* CHANNEL 4 */ > >+ {6, 10}, /* CHANNEL 5 */ > >+ {6, 10}, /* CHANNEL 6 */ > >+ {6, 10}, /* CHANNEL 7 */ > >+ {3, 14}, /* CHANNEL 8 */ > >+ {1, 3}, /* CHANNEL 9 */ > >+ {1, 1}, /* CHANNEL 10 No Prescaler */ > >+ {15, 100}, /* CHANNEL 11 */ > >+ {1, 4}, /* CHANNEL 12 */ > >+ {1, 1}, /* CHANNEL 13 Reserved channels */ > >+ {1, 1}, /* CHANNEL 14 Reseved channels */ > >+ {5, 11}, /* CHANNEL 15 */ > >+}; > >+ > >+ > >+/* > >+ * Conversion table from -3 to 55 degree Celcius > >+ */ > >+static int therm_tbl[] = { > >+30800, 29500, 28300, 27100, > >+26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700, 17900, > >+17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100, 12600, 12100, > >+11600, 11200, 10800, 10400, 10000, 9630, 9280, 8950, 8620, 8310, > >+8020, 7730, 7460, 7200, 6950, 6710, 6470, 6250, 6040, 5830, > >+5640, 5450, 5260, 5090, 4920, 4760, 4600, 4450, 4310, 4170, > >+4040, 3910, 3790, 3670, 3550 > >+}; > >+ > >+/* > >+ * Structure containing the registers > >+ * of different conversion methods supported by MADC. > >+ * Hardware or RT real time conversion request initiated by external host > >+ * processor for RT Signal conversions. > >+ * External host processors can also request for non RT conversions > >+ * SW1 and SW2 software conversions also called asynchronous or GPC request. > >+ */ > >+static > >+const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = { > >+ [TWL4030_MADC_RT] = { > >+ .sel = TWL4030_MADC_RTSELECT_LSB, > >+ .avg = TWL4030_MADC_RTAVERAGE_LSB, > >+ .rbase = TWL4030_MADC_RTCH0_LSB, > >+ }, > >+ [TWL4030_MADC_SW1] = { > >+ .sel = TWL4030_MADC_SW1SELECT_LSB, > >+ .avg = TWL4030_MADC_SW1AVERAGE_LSB, > >+ .rbase = TWL4030_MADC_GPCH0_LSB, > >+ .ctrl = TWL4030_MADC_CTRL_SW1, > >+ }, > >+ [TWL4030_MADC_SW2] = { > >+ .sel = TWL4030_MADC_SW2SELECT_LSB, > >+ .avg = TWL4030_MADC_SW2AVERAGE_LSB, > >+ .rbase = TWL4030_MADC_GPCH0_LSB, > >+ .ctrl = TWL4030_MADC_CTRL_SW2, > >+ }, > >+}; > >+ > >+/* > >+ * Function to read a particular channel value. > >+ * @madc - pointer to struct twl4030_madc_data > >+ * @reg - lsb of ADC Channel > >+ * If the i2c read fails it returns an error else returns 0. > >+ */ > >+static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg) > >+{ > >+ u8 msb, lsb; > >+ int ret; > >+ /* > >+ * For each ADC channel, we have MSB and LSB register pair. MSB address > >+ * is always LSB address+1. reg parameter is the address of LSB register > >+ */ > >+ ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1); > >+ if (ret) { > >+ dev_err(madc->dev, "unable to read MSB register 0x%X\n", > >+ reg + 1); > >+ return ret; > >+ } > >+ ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg); > >+ if (ret) { > >+ dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg); > >+ return ret; > >+ } > >+ > >+ return (int)(((msb << 8) | lsb) >> 6); > >+} > >+ > >+/* > >+ * Return battery temperature > >+ * Or < 0 on failure. > >+ */ > >+static int twl4030battery_temperature(int raw_volt) > >+{ > >+ u8 val; > >+ int temp, curr, volt, res, ret; > >+ > >+ volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R; > >+ /* Getting and calculating the supply current in micro ampers */ > >+ ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, > >+ REG_BCICTL2); > >+ if (ret < 0) > >+ return ret; > blank line here and after similar error cases makes it a little easier to > read. > > >+ curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10; > >+ /* Getting and calculating the thermistor resistance in ohms */ > >+ res = volt * 1000 / curr; > >+ /* calculating temperature */ > >+ for (temp = 58; temp >= 0; temp--) { > >+ int actual = therm_tbl[temp]; > >+ > No blank line here. I added the newline to the style fix patch, since these lines were not touched by me before. > >+ if ((actual - res) >= 0) > >+ break; > >+ } > >+ > >+ return temp + 1; > >+} > >+ > >+static int twl4030battery_current(int raw_volt) > >+{ > >+ int ret; > >+ u8 val; > >+ > >+ ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, > >+ TWL4030_BCI_BCICTL1); > >+ if (ret) > >+ return ret; > >+ if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */ > >+ return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1; > >+ else /* slope of 0.88 mV/mA */ > >+ return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2; > >+} > blank line here please. Also added to the style fix patch. > Various comments on this in the next function: > This would be much simpler if any error immediately resulted in an > exit with error code. If it's gone wrong enough to issue a dev_err > then don't try to muddle through. Given there is nothing to clear > up in here, just error out directly on the first problem. I guess this should be fixed once the old API is removed. > >+/* > >+ * Function to read channel values > >+ * @madc - pointer to twl4030_madc_data struct > >+ * @reg_base - Base address of the first channel > >+ * @Channels - 16 bit bitmap. If the bit is set, channel value is read > >+ * @buf - The channel values are stored here. if read fails error > >+ * @raw - Return raw values without conversion > >+ * value is stored > >+ * Returns the number of successfully read channels. > >+ */ > >+static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, > >+ u8 reg_base, unsigned > >+ long channels, int *buf, > >+ bool raw) > >+{ > >+ int count = 0, count_req = 0, i; > >+ u8 reg; > >+ > >+ for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) { > Skip the temporary for reg. Yes, it gets used twice, but easier to read > inline. Also fixed in the style patch. > >+ reg = reg_base + 2 * i; > >+ buf[i] = twl4030_madc_channel_raw_read(madc, reg); > >+ if (buf[i] < 0) { > >+ dev_err(madc->dev, > >+ "Unable to read register 0x%X\n", reg); > >+ count_req++; > >+ continue; > >+ } > I'd prefer this as > if (raw) { > count ++; > } else { > switch (i) { > .. > } That increases the code indention by another level. I don't think that's a good idea. > Also near as I can tell count gets incremented unless there is an error > anyway. So just increment it outside and allow this function to return the > error code. Returning error codes would change the existing API. I would prefer to do this change after the old API is removed. > >+ if (raw) { > >+ count++; > >+ continue; > >+ } > >+ switch (i) { > These cases could do with a suitable #define to give them a name. TWL4030_MADC_ADCIN10 is already in use for (1 << 10). I guess this can be simplified more easily once the old API is removed. > >+ case 10: > >+ buf[i] = twl4030battery_current(buf[i]); > >+ if (buf[i] < 0) { > >+ dev_err(madc->dev, "err reading current\n"); > >+ count_req++; > >+ } else { > >+ count++; > >+ buf[i] = buf[i] - 750; > >+ } > >+ break; > >+ case 1: > >+ buf[i] = twl4030battery_temperature(buf[i]); > >+ if (buf[i] < 0) { > >+ dev_err(madc->dev, "err reading temperature\n"); > >+ count_req++; > >+ } else { > >+ buf[i] -= 3; > >+ count++; > >+ } > >+ break; > >+ default: > >+ count++; > >+ /* Analog Input (V) = conv_result * step_size / R > >+ * conv_result = decimal value of 10-bit conversion > >+ * result > >+ * step size = 1.5 / (2 ^ 10 -1) > >+ * R = Prescaler ratio for input channels. > >+ * Result given in mV hence multiplied by 1000. > >+ */ > >+ buf[i] = (buf[i] * 3 * 1000 * > >+ twl4030_divider_ratios[i].denominator) > >+ / (2 * 1023 * > >+ twl4030_divider_ratios[i].numerator); > >+ } > >+ } > This is already apparant from the errors emmited earlier. I'd drop this > and the count_req counter in general. > Personally I'd error out in those cases anyway rather than carrying on > with the rest of the channels. If it's worth of a dev_err it's worthy > of getting out as fast as possible rather than trying to muddle on. I also think this should be postponed until the old API is removed. > >+ if (count_req) > >+ dev_err(madc->dev, "%d channel conversion failed\n", count_req); > >+ > >+ return count; > >+} > >+ > >+/* > >+ * Enables irq. > >+ * @madc - pointer to twl4030_madc_data struct > >+ * @id - irq number to be enabled > >+ * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2 > >+ * corresponding to RT, SW1, SW2 conversion requests. > >+ * If the i2c read fails it returns an error else returns 0. > >+ */ > >+static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id) > >+{ > >+ u8 val; > >+ int ret; > >+ > >+ ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr); > >+ if (ret) { > >+ dev_err(madc->dev, "unable to read imr register 0x%X\n", > >+ madc->imr); > >+ return ret; > >+ } > A blank line here and in similar cases would slight aid readability. > >+ val &= ~(1 << id); > >+ ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); > >+ if (ret) { > >+ dev_err(madc->dev, > >+ "unable to write imr register 0x%X\n", madc->imr); > >+ return ret; > >+ > Loose blank line here. both fixed in the style patch. > >+ } > >+ > >+ return 0; > >+} > >+ > >+/* > >+ * Disables irq. > >+ * @madc - pointer to twl4030_madc_data struct > >+ * @id - irq number to be disabled > >+ * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2 > >+ * corresponding to RT, SW1, SW2 conversion requests. > >+ * Returns error if i2c read/write fails. > >+ */ > >+static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id) > >+{ > >+ u8 val; > >+ int ret; > >+ > >+ ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr); > >+ if (ret) { > >+ dev_err(madc->dev, "unable to read imr register 0x%X\n", > >+ madc->imr); > >+ return ret; > >+ } > >+ val |= (1 << id); > >+ ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); > >+ if (ret) { > >+ dev_err(madc->dev, > >+ "unable to write imr register 0x%X\n", madc->imr); > >+ return ret; > >+ } > >+ > >+ return 0; > >+} > >+ > >+static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc) > >+{ > >+ struct twl4030_madc_data *madc = _madc; > >+ const struct twl4030_madc_conversion_method *method; > >+ u8 isr_val, imr_val; > >+ int i, len, ret; > >+ struct twl4030_madc_request *r; > >+ > >+ mutex_lock(&madc->lock); > >+ ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr); > >+ if (ret) { > >+ dev_err(madc->dev, "unable to read isr register 0x%X\n", > >+ madc->isr); > >+ goto err_i2c; > >+ } > >+ ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr); > >+ if (ret) { > >+ dev_err(madc->dev, "unable to read imr register 0x%X\n", > >+ madc->imr); > >+ goto err_i2c; > >+ } > >+ isr_val &= ~imr_val; > >+ for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { > >+ if (!(isr_val & (1 << i))) > >+ continue; > >+ ret = twl4030_madc_disable_irq(madc, i); > >+ if (ret < 0) > >+ dev_dbg(madc->dev, "Disable interrupt failed%d\n", i); > >+ madc->requests[i].result_pending = 1; > >+ } > Can this and the previous loop not be combined thus simplifying some tests? It probably can be, but I think this can wait until the old API is removed. > >+ for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { > >+ r = &madc->requests[i]; > >+ /* No pending results for this method, move to next one */ > >+ if (!r->result_pending) > >+ continue; > >+ method = &twl4030_conversion_methods[r->method]; > >+ /* Read results */ > >+ len = twl4030_madc_read_channels(madc, method->rbase, > >+ r->channels, r->rbuf, r->raw); > >+ /* Return results to caller */ > >+ if (r->func_cb != NULL) { > >+ r->func_cb(len, r->channels, r->rbuf); > >+ r->func_cb = NULL; > >+ } > >+ /* Free request */ > >+ r->result_pending = 0; > >+ r->active = 0; > >+ } > >+ mutex_unlock(&madc->lock); > >+ > >+ return IRQ_HANDLED; > >+ > >+err_i2c: > >+ /* > >+ * In case of error check whichever request is active > >+ * and service the same. > >+ */ > >+ for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { > >+ r = &madc->requests[i]; > >+ if (r->active == 0) > >+ continue; > >+ method = &twl4030_conversion_methods[r->method]; > >+ /* Read results */ > >+ len = twl4030_madc_read_channels(madc, method->rbase, > >+ r->channels, r->rbuf, r->raw); > >+ /* Return results to caller */ > >+ if (r->func_cb != NULL) { > >+ r->func_cb(len, r->channels, r->rbuf); > >+ r->func_cb = NULL; > >+ } > >+ /* Free request */ > >+ r->result_pending = 0; > >+ r->active = 0; > >+ } > >+ mutex_unlock(&madc->lock); > >+ > >+ return IRQ_HANDLED; > >+} > >+ > This structure does seem a little complex. I'd have been more tempted > by using a completion and having the calling function wait on it. How > often does an actual callback make sense? This is part of the old API, so let's convert all consumers to the IIO API and deprecate the callback stuff. > >+static int twl4030_madc_set_irq(struct twl4030_madc_data *madc, > >+ struct twl4030_madc_request *req) > >+{ > >+ struct twl4030_madc_request *p; > >+ int ret; > >+ > >+ p = &madc->requests[req->method]; > >+ memcpy(p, req, sizeof(*req)); > >+ ret = twl4030_madc_enable_irq(madc, req->method); > >+ if (ret < 0) { > >+ dev_err(madc->dev, "enable irq failed!!\n"); > >+ return ret; > >+ } > >+ > >+ return 0; > >+} > >+ > >+/* > >+ * Function which enables the madc conversion > >+ * by writing to the control register. > >+ * @madc - pointer to twl4030_madc_data struct > >+ * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1 > >+ * corresponding to RT SW1 or SW2 conversion methods. > >+ * Returns 0 if succeeds else a negative error value > >+ */ > >+static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc, > >+ int conv_method) > >+{ > >+ const struct twl4030_madc_conversion_method *method; > >+ int ret = 0; > >+ method = &twl4030_conversion_methods[conv_method]; > >+ switch (conv_method) { > Can we get here via any paths where these aren't the methods set? conv_method is set from outside of the driver, so it's probably safer to check. This can be simplified once the old API is removed. > >+ case TWL4030_MADC_SW1: > >+ case TWL4030_MADC_SW2: > >+ ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, > >+ TWL4030_MADC_SW_START, method->ctrl); > >+ if (ret) { > >+ dev_err(madc->dev, > >+ "unable to write ctrl register 0x%X\n", > >+ method->ctrl); > >+ return ret; > >+ } > >+ break; > >+ default: > >+ break; > >+ } > >+ > >+ return 0; > >+} > >+ > Could we fix up the kernel doc in this file in general. It's so nearly > there but not quite! I assume, that the driver will get a huge cleanup once all consumers of the old API are converted. Maybe a deprecation flag should be added together with this patchset? > >+/* > >+ * Function that waits for conversion to be ready > >+ * @madc - pointer to twl4030_madc_data struct > >+ * @timeout_ms - timeout value in milliseconds > >+ * @status_reg - ctrl register > >+ * returns 0 if succeeds else a negative error value > >+ */ > >+static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc, > >+ unsigned int timeout_ms, > >+ u8 status_reg) > >+{ > >+ unsigned long timeout; > >+ int ret; > >+ > >+ timeout = jiffies + msecs_to_jiffies(timeout_ms); > >+ do { > >+ u8 reg; > >+ > >+ ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, ®, status_reg); > >+ if (ret) { > >+ dev_err(madc->dev, > >+ "unable to read status register 0x%X\n", > >+ status_reg); > >+ return ret; > >+ } > >+ if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW)) > >+ return 0; > >+ usleep_range(500, 2000); > >+ } while (!time_after(jiffies, timeout)); > >+ dev_err(madc->dev, "conversion timeout!\n"); > >+ > >+ return -EAGAIN; > >+} > >+ > > >+/* > >+ * An exported function which can be called from other kernel drivers. > What uses this currently? Ideally we'd do this through the standard IIO paths > for in kernel users. Right now the device tree mappings for that are less > than ideal (ask Mark Rutland if you want some choice comments ;) Perhaps > we take the view this can be cleaned up later. There are some elements in here > we haven't yet looked at supporting for client drivers. Will have to think > about that. > > It would be particularly nice if possible to use a generic battery driver > for the two cases that seem to be using this functionality at the moment. Currently there are 3 consumer drivers using this: * twl4030-madc-battery * rx51-battery * twl4030-madc-hwmon I guess the first two can be updated to use the IIO API and the last one can be replaced by the IIO hwmon driver. Since it's an in-kernel-api there should be no problems to simply remove the API afterwards. > >+ * @req twl4030_madc_request structure > >+ * req->rbuf will be filled with read values of channels based on the > >+ * channel index. If a particular channel reading fails there will > >+ * be a negative error value in the corresponding array element. > >+ * returns 0 if succeeds else error value > >+ */ > >+int twl4030_madc_conversion(struct twl4030_madc_request *req) > >+{ > >+ const struct twl4030_madc_conversion_method *method; > >+ u8 ch_msb, ch_lsb; > >+ int ret; > >+ > >+ if (!req || !twl4030_madc) > >+ return -EINVAL; > >+ > >+ mutex_lock(&twl4030_madc->lock); > >+ if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) { > >+ ret = -EINVAL; > >+ goto out; > >+ } > >+ /* Do we have a conversion request ongoing */ > >+ if (twl4030_madc->requests[req->method].active) { > >+ ret = -EBUSY; > >+ goto out; > >+ } > >+ ch_msb = (req->channels >> 8) & 0xff; > >+ ch_lsb = req->channels & 0xff; > >+ method = &twl4030_conversion_methods[req->method]; > >+ /* Select channels to be converted */ > >+ ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1); > >+ if (ret) { > >+ dev_err(twl4030_madc->dev, > >+ "unable to write sel register 0x%X\n", method->sel + 1); > >+ goto out; > >+ } > >+ ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel); > >+ if (ret) { > >+ dev_err(twl4030_madc->dev, > >+ "unable to write sel register 0x%X\n", method->sel + 1); > >+ goto out; > >+ } > >+ /* Select averaging for all channels if do_avg is set */ > >+ if (req->do_avg) { > >+ ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, > >+ ch_msb, method->avg + 1); > >+ if (ret) { > >+ dev_err(twl4030_madc->dev, > >+ "unable to write avg register 0x%X\n", > >+ method->avg + 1); > >+ goto out; > >+ } > >+ ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, > >+ ch_lsb, method->avg); > >+ if (ret) { > >+ dev_err(twl4030_madc->dev, > Excesive error messages are sometimes irritating as they break up code > flow. They are really irritating when incorrect ;) Added to the fixes patch. > >+ "unable to write sel reg 0x%X\n", > >+ method->sel + 1); > >+ goto out; > >+ } > >+ } > >+ if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) { > >+ ret = twl4030_madc_set_irq(twl4030_madc, req); > >+ if (ret < 0) > >+ goto out; > >+ ret = twl4030_madc_start_conversion(twl4030_madc, req->method); > >+ if (ret < 0) > >+ goto out; > >+ twl4030_madc->requests[req->method].active = 1; > >+ ret = 0; > >+ goto out; > >+ } > Is it possible to get here? This comment suggests not. If so, why have > the test? I guess it's an additional safety check. This function can be simplified a lot once the old API is removed, so I guess this can also be postponed. > >+ /* With RT method we should not be here anymore */ > >+ if (req->method == TWL4030_MADC_RT) { > >+ ret = -EINVAL; > >+ goto out; > >+ } > >+ ret = twl4030_madc_start_conversion(twl4030_madc, req->method); > >+ if (ret < 0) > >+ goto out; > >+ twl4030_madc->requests[req->method].active = 1; > >+ /* Wait until conversion is ready (ctrl register returns EOC) */ > So no interrupts in this case? > >+ ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl); > >+ if (ret) { > >+ twl4030_madc->requests[req->method].active = 0; > >+ goto out; > >+ } > >+ ret = twl4030_madc_read_channels(twl4030_madc, method->rbase, > >+ req->channels, req->rbuf, req->raw); > >+ twl4030_madc->requests[req->method].active = 0; > >+ > >+out: > >+ mutex_unlock(&twl4030_madc->lock); > >+ > >+ return ret; > >+} > >+EXPORT_SYMBOL_GPL(twl4030_madc_conversion); > >+ > >+/* > Proper kerneldoc would be nice here. Otherwise the comment doesn't > really add anything so I'd be tempted to drop it ;) removed in fixes patch. > >+ * Return channel value > >+ * Or < 0 on failure. > >+ */ > >+int twl4030_get_madc_conversion(int channel_no) > >+{ > >+ struct twl4030_madc_request req; > >+ int temp = 0; > >+ int ret; > >+ > >+ req.channels = (1 << channel_no); > >+ req.method = TWL4030_MADC_SW2; > >+ req.active = 0; > >+ req.func_cb = NULL; > >+ ret = twl4030_madc_conversion(&req); > >+ if (ret < 0) > >+ return ret; > >+ if (req.rbuf[channel_no] > 0) > >+ temp = req.rbuf[channel_no]; > >+ > >+ return temp; > >+} > >+EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion); > >+ > >+/* > >+ * Function to enable or disable bias current for > >+ * main battery type reading or temperature sensing > >+ * @madc - pointer to twl4030_madc_data struct > >+ * @chan - can be one of the two values > What is a battery type reading? Voltage? Current? Charge? It's used for battery type identification. > >+ * TWL4030_BCI_ITHEN - Enables bias current for main battery type reading > >+ * TWL4030_BCI_TYPEN - Enables bias current for main battery temperature > These constants have rather incomprehensible names. Whilst I gues they > are straight off the data sheet, seems to me that we could make them a little > longer and easier to follow! The comment here helps, but sensible names > would be better! The names come from the datasheet. I think we should fix this after the old API is removed, since the constants are exposed for the API. > >+ * sensing > >+ * @on - enable or disable chan. > >+ */ > >+static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc, > >+ int chan, int on) > >+{ > >+ int ret; > >+ u8 regval; > >+ > >+ ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, > >+ ®val, TWL4030_BCI_BCICTL1); > >+ if (ret) { > >+ dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X", > >+ TWL4030_BCI_BCICTL1); > >+ return ret; > >+ } > Introduce an intermediate variable and this could be nice and easy to read > Say > > int regmask; > if (chan == 0) > regmask = TWL4030_BCI_TYPEN; > else > regmask = TWL4030_BFI_ITHEN; > > if (on) > regval |= regmask; > else > regval &= ~regmask; > > It's longer but doesn't give me a headache. I added this to the fixes patch: int regmask; regmask = (chan == 0) ? TWL4030_BCI_TYPEN : TWL4030_BFI_ITHEN; if (on) regval |= regmask; else regval &= ~regmask; > >+ if (on) > >+ regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN; > >+ else > >+ regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN; > >+ ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, > >+ regval, TWL4030_BCI_BCICTL1); > >+ if (ret) { > >+ dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n", > >+ TWL4030_BCI_BCICTL1); > >+ return ret; > >+ } > >+ > >+ return 0; > >+} > >+ > >+/* > >+ * Function that sets MADC software power on bit to enable MADC > >+ * @madc - pointer to twl4030_madc_data struct > >+ * @on - Enable or disable MADC software powen on bit. > >+ * returns error if i2c read/write fails else 0 > >+ */ > >+static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) > >+{ > >+ u8 regval; > >+ int ret; > >+ > >+ ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, > >+ ®val, TWL4030_MADC_CTRL1); > >+ if (ret) { > >+ dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n", > >+ TWL4030_MADC_CTRL1); > >+ return ret; > >+ } > >+ if (on) > >+ regval |= TWL4030_MADC_MADCON; > >+ else > >+ regval &= ~TWL4030_MADC_MADCON; > >+ ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1); > >+ if (ret) { > >+ dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n", > >+ TWL4030_MADC_CTRL1); > >+ return ret; > >+ } > >+ > >+ return 0; > >+} > >+ > >+/* > >+ * Initialize MADC and request for threaded irq > >+ */ > >+static int twl4030_madc_probe(struct platform_device *pdev) > >+{ > >+ struct twl4030_madc_data *madc; > >+ struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev); > >+ struct device_node *np = pdev->dev.of_node; > >+ int irq, ret; > >+ u8 regval; > >+ struct iio_dev *iio_dev = NULL; > >+ > >+ if (!pdata && !np) { > >+ dev_err(&pdev->dev, "platform_data not available\n"); > >+ return -EINVAL; > >+ } > >+ > >+ iio_dev = devm_iio_device_alloc(&pdev->dev, > >+ sizeof(struct twl4030_madc_data)); > >+ if (!iio_dev) { > >+ dev_err(&pdev->dev, "failed allocating iio device\n"); > >+ return -ENOMEM; > >+ } > >+ > >+ madc = iio_priv(iio_dev); > >+ madc->dev = &pdev->dev; > >+ > >+ iio_dev->name = dev_name(&pdev->dev); > >+ iio_dev->dev.parent = &pdev->dev; > >+ iio_dev->dev.of_node = pdev->dev.of_node; > >+ iio_dev->info = &twl4030_madc_iio_info; > >+ iio_dev->modes = INDIO_DIRECT_MODE; > >+ iio_dev->channels = twl4030_madc_iio_channels; > >+ iio_dev->num_channels = 16; > >+ > >+ /* > >+ * Phoenix provides 2 interrupt lines. The first one is connected to > >+ * the OMAP. The other one can be connected to the other processor such > >+ * as modem. Hence two separate ISR and IMR registers. > >+ */ > >+ if (pdata) > >+ madc->use_second_irq = pdata->irq_line != 1; > >+ else > >+ madc->use_second_irq = false; > >+ > >+ madc->imr = (madc->use_second_irq == 1) ? > >+ TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2; > >+ madc->isr = (madc->use_second_irq == 1) ? > >+ TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2; > >+ > >+ ret = twl4030_madc_set_power(madc, 1); > >+ if (ret < 0) > >+ return ret; > >+ ret = twl4030_madc_set_current_generator(madc, 0, 1); > >+ if (ret < 0) > >+ goto err_current_generator; > >+ > >+ ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, > >+ ®val, TWL4030_BCI_BCICTL1); > >+ if (ret) { > >+ dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n", > >+ TWL4030_BCI_BCICTL1); > >+ goto err_i2c; > >+ } > >+ regval |= TWL4030_BCI_MESBAT; > >+ ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, > >+ regval, TWL4030_BCI_BCICTL1); > >+ if (ret) { > >+ dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n", > >+ TWL4030_BCI_BCICTL1); > >+ goto err_i2c; > >+ } > >+ > >+ /* Check that MADC clock is on */ > >+ ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, ®val, TWL4030_REG_GPBR1); > >+ if (ret) { > Personally I'd rank the driver as rather to vebose on read error messages > given they are pretty unusual. Ah well I guess each to their own. All of this is not written by me. > >+ dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n", > >+ TWL4030_REG_GPBR1); > >+ goto err_i2c; > >+ } > >+ > >+ /* If MADC clk is not on, turn it on */ > >+ if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) { > >+ dev_info(&pdev->dev, "clk disabled, enabling\n"); > >+ regval |= TWL4030_GPBR1_MADC_HFCLK_EN; > >+ ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval, > >+ TWL4030_REG_GPBR1); > >+ if (ret) { > >+ dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n", > >+ TWL4030_REG_GPBR1); > >+ goto err_i2c; > >+ } > >+ } > >+ > >+ platform_set_drvdata(pdev, iio_dev); > >+ mutex_init(&madc->lock); > >+ > >+ irq = platform_get_irq(pdev, 0); > > As ever using devm for a irq request makes for a bit of a review > nightmare as we have to be very careful that nothing has been removed > that this might use before the devm cleanup occurs (at end of remove). > In this case the fact that the current generator is off and the adc unit > is disabled sounds to me like it might cause interesting results in the > interrupt handler? > > It's one of those things that will probably never happen but I find it > hard to convince myself 'can't happen' > > >+ ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, > >+ twl4030_madc_threaded_irq_handler, > >+ IRQF_TRIGGER_RISING, "twl4030_madc", madc); > >+ if (ret) { > >+ dev_dbg(&pdev->dev, "could not request irq\n"); > >+ goto err_i2c; > >+ } > > Err, where is this defined? Ah, found it above. I'm not keen on > preventing multiple instances of a device like this. It is so easy > to not do this I'd prefer to see the driver fixed to remove this. This concept has also not introduced by me of course. I think we should fix this after removal of the old API, since the old API does not have an device parameter. > >+ twl4030_madc = madc; > >+ > >+ ret = iio_device_register(iio_dev); > >+ if (ret) { > >+ dev_dbg(&pdev->dev, "could not register iio device\n"); > >+ goto err_i2c; > >+ } > >+ > >+ return 0; > >+ > >+err_i2c: > >+ twl4030_madc_set_current_generator(madc, 0, 0); > >+err_current_generator: > >+ twl4030_madc_set_power(madc, 0); > >+ return ret; > >+} > >+ > >+static int twl4030_madc_remove(struct platform_device *pdev) > >+{ > >+ struct iio_dev *iio_dev = platform_get_drvdata(pdev); > >+ struct twl4030_madc_data *madc = iio_priv(iio_dev); > >+ > >+ twl4030_madc_set_current_generator(madc, 0, 0); > >+ twl4030_madc_set_power(madc, 0); > >+ > >+ iio_device_unregister(iio_dev); > >+ > >+ return 0; > >+} > >+ > >+#ifdef CONFIG_OF > >+static const struct of_device_id twl_madc_of_match[] = { > >+ {.compatible = "ti,twl4030-madc", }, > >+ { }, > >+}; > >+MODULE_DEVICE_TABLE(of, twl_madc_of_match); > >+#endif > >+ > >+static struct platform_driver twl4030_madc_driver = { > >+ .probe = twl4030_madc_probe, > >+ .remove = twl4030_madc_remove, > >+ .driver = { > >+ .name = "twl4030_madc", > >+ .owner = THIS_MODULE, > >+ .of_match_table = of_match_ptr(twl_madc_of_match), > >+ }, > >+}; > >+ > >+module_platform_driver(twl4030_madc_driver); > >+ > >+MODULE_DESCRIPTION("TWL4030 ADC driver"); > >+MODULE_LICENSE("GPL"); > >+MODULE_AUTHOR("J Keerthy"); > >+MODULE_ALIAS("platform:twl4030_madc"); > > Rest of patch was removal of code so no comments! FYI: For me it was a bit odd to see inline comments about code lines coming after the inline comment. Normally I receive patch reviews with comments after the referenced code, which makes more sense imho. -- Sebastian
On 23/02/14 00:35, Sebastian Reichel wrote: > On Sat, Feb 22, 2014 at 12:47:03PM +0000, Jonathan Cameron wrote: >> On 14/02/14 18:46, Sebastian Reichel wrote: >>> This is a driver for an A/D converter, which belongs into >>> drivers/iio/adc. >>> >>> Signed-off-by: Sebastian Reichel <sre@debian.org> >> >> Being inherently lazy I'm going to review this patch as it gives the complete >> driver rather than taking on the conversion patch directly! > > OK. > >> It's a long shot, but are there any docs freely available for this part? > > There is documentation on Texas Instrument's website for TPS65950, > which is supposed to be very similar to the TWL4030 and contains a > section about the MADC module: > > http://www.ti.com/lit/gpn/tps65950 I had a look at that and to put it lightly the docs aren't particularly informative. Ah well, such is life. > >> Obviously that may well mean that some of my comments apply to the driver >> in general rather than the changes you've made. Please feel free to >> pick and choose! > > Having skipped over your comments I guess 70% are about the driver > in general, but I can add another patch to the patchset, which fixes > the driver style. That would be great thanks! > >> I'd like ideally to see a little more generic tidying up in this driver. >> As you'll notice inline I get irritated at having lots and lots of error >> messages. Still that's personal preference but I felt a lot more justified >> in moaning after finding one that was incorrect ;) >> >> There is also a bit of functionality in here that I'm not sure is ever >> used (the request callback functions). It complicates the code so if no >> using it I'd be tempted to drop it. > > I think the optimal workflow is: > > 1. convert madc driver to IIO > 2. convert twl4030-madc-battery driver to IIO API > 3. convert rx51-battery to IIO API > 4. convert twl4030-madc-hwmon to IIO API / deprecate it > 5. remove old in-kernel ABI from madc > 6. cleanup/simplify madc > > I guess its much simpler to do the driver cleanup/simplification > once we can get rid of the old API. Perhaps. I guess it depends on your planned schedule. If you are intending to plough through the above in the near future then fair enough. If the chances are it'll take a while, I'd be inclined to fix some of the more glarringly hideous bits now (such as error code reporting) and then visit your list above over time. Note that to my mind it is converted drivers like this that sneak some nasty code into an a subsystem I look after. We have a lot of 'interesting' code out in staging still, but for new submissions we are being increasingly fussy about little bits. So I hope I don't come across as being too mean about this driver. I'm glad you are putting in the work to convert it over. There are a quite a few similar drivers sculking through the tree so it's nice that someone has gotten started on bringing one over to IIO. > >> It's nice in a way that the driver before your conversion provided only >> a very limited and in kernel interface given we don't have to hang on to >> any old ABI elements. >> >>> --- >>> drivers/iio/adc/Kconfig | 10 + >>> drivers/iio/adc/Makefile | 1 + >>> drivers/iio/adc/twl4030-madc.c | 922 +++++++++++++++++++++++++++++++++++++++++ >>> drivers/mfd/Kconfig | 10 - >>> drivers/mfd/Makefile | 1 - >>> drivers/mfd/twl4030-madc.c | 922 ----------------------------------------- >>> 6 files changed, 933 insertions(+), 933 deletions(-) >>> create mode 100644 drivers/iio/adc/twl4030-madc.c >>> delete mode 100644 drivers/mfd/twl4030-madc.c >>> >>> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig >>> index 2209f28..427f75c 100644 >>> --- a/drivers/iio/adc/Kconfig >>> +++ b/drivers/iio/adc/Kconfig >>> @@ -183,6 +183,16 @@ config TI_AM335X_ADC >>> Say yes here to build support for Texas Instruments ADC >>> driver which is also a MFD client. >>> >>> +config TWL4030_MADC >>> + tristate "TWL4030 MADC (Monitoring A/D Converter)" >>> + depends on TWL4030_CORE >>> + help >>> + This driver provides support for Triton TWL4030-MADC. The >>> + driver supports both RT and SW conversion methods. >>> + >>> + This driver can also be built as a module. If so, the module will be >>> + called twl4030-madc. >>> + >>> config TWL6030_GPADC >>> tristate "TWL6030 GPADC (General Purpose A/D Converter) Support" >>> depends on TWL4030_CORE >>> diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile >>> index ba9a10a..9acf2df 100644 >>> --- a/drivers/iio/adc/Makefile >>> +++ b/drivers/iio/adc/Makefile >>> @@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o >>> obj-$(CONFIG_NAU7802) += nau7802.o >>> obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o >>> obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o >>> +obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o >>> obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o >>> obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o >>> diff --git a/drivers/iio/adc/twl4030-madc.c b/drivers/iio/adc/twl4030-madc.c >>> new file mode 100644 >>> index 0000000..4da61c4 >>> --- /dev/null >>> +++ b/drivers/iio/adc/twl4030-madc.c >>> @@ -0,0 +1,922 @@ >>> +/* >>> + * >>> + * TWL4030 MADC module driver-This driver monitors the real time >>> + * conversion of analog signals like battery temperature, >>> + * battery type, battery level etc. >>> + * >>> + * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ >>> + * J Keerthy <j-keerthy@ti.com> >>> + * >>> + * Based on twl4030-madc.c >>> + * Copyright (C) 2008 Nokia Corporation >>> + * Mikko Ylinen <mikko.k.ylinen@nokia.com> >>> + * >>> + * Amit Kucheria <amit.kucheria@canonical.com> >>> + * >>> + * This program is free software; you can redistribute it and/or >>> + * modify it under the terms of the GNU General Public License >>> + * version 2 as published by the Free Software Foundation. >>> + * >>> + * This program is distributed in the hope that it will be useful, but >>> + * WITHOUT ANY WARRANTY; without even the implied warranty of >>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU >>> + * General Public License for more details. >>> + * >>> + * You should have received a copy of the GNU General Public License >>> + * along with this program; if not, write to the Free Software >>> + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA >>> + * 02110-1301 USA >>> + * >>> + */ >>> + >>> +#include <linux/init.h> >>> +#include <linux/device.h> >>> +#include <linux/interrupt.h> >>> +#include <linux/kernel.h> >>> +#include <linux/delay.h> >>> +#include <linux/platform_device.h> >>> +#include <linux/slab.h> >>> +#include <linux/i2c/twl.h> >>> +#include <linux/i2c/twl4030-madc.h> >>> +#include <linux/module.h> >>> +#include <linux/stddef.h> >>> +#include <linux/mutex.h> >>> +#include <linux/bitops.h> >>> +#include <linux/jiffies.h> >>> +#include <linux/types.h> >>> +#include <linux/gfp.h> >>> +#include <linux/err.h> >>> + >>> +#include <linux/iio/iio.h> >> Why include machine.h or driver.h. You aren't currently using the >> mapping coding anyway that these provide. > > My bad. I copied that over from another driver without checking if > it is actually needed. > >>> +#include <linux/iio/machine.h> >>> +#include <linux/iio/driver.h> >>> + >>> +/* >>> + * struct twl4030_madc_data - a container for madc info >>> + * @dev - pointer to device structure for madc >>> + * @lock - mutex protecting this data structure >>> + * @requests - Array of request struct corresponding to SW1, SW2 and RT >>> + * @imr - Interrupt mask register of MADC >>> + * @isr - Interrupt status register of MADC >>> + */ >>> +struct twl4030_madc_data { >>> + struct device *dev; >>> + struct mutex lock; /* mutex protecting this data structure */ >>> + struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS]; >>> + bool use_second_irq; >> >> These are 32 bit signed values? > > These were already defined as int in the original driver. I will > change these to u8 in an additional patch. > >>> + int imr; >>> + int isr; >>> +}; >>> + >>> +static int twl4030_madc_read(struct iio_dev *iio_dev, >>> + const struct iio_chan_spec *chan, >>> + int *val, int *val2, long mask) >>> +{ >>> + struct twl4030_madc_data *madc = iio_priv(iio_dev); >>> + struct twl4030_madc_request req; >>> + int channel = chan->channel; >>> + int ret; >>> + >>> + req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1; >>> + >>> + req.channels = BIT(channel); >>> + req.active = 0; >>> + req.func_cb = NULL; >> req.raw = !(mask & IIO_CHAN_INFO_PROCESSED); >> req.do_avg = !!(mask & IIO_CHAN_INFO_AVERAGE_RAW); > > Right. > >>> + req.raw = (mask & IIO_CHAN_INFO_PROCESSED) ? false : true; >>> + req.do_avg = (mask & IIO_CHAN_INFO_AVERAGE_RAW) ? true : false; >>> + >>> + ret = twl4030_madc_conversion(&req); >>> + if (ret < 0) >>> + return ret; >>> + >>> + *val = req.rbuf[channel]; >>> + >>> + return IIO_VAL_INT; >>> +} >>> + >>> +static const struct iio_info twl4030_madc_iio_info = { >>> + .read_raw = &twl4030_madc_read, >>> + .driver_module = THIS_MODULE, >>> +}; >>> + >> Please give this a prefixed name. Chances of ADC_CHANNEL >> turning up in an IIO header is a little too high to do this >> without! e.g. #define TWL4030_ADC_CHANNEL > > Probably drivers/iio/adc/exynos_adc.c should also be fixed. Sounds like it. Patches always welcome ;) Like any other reviewer I'm not known for absolute consistency. > >>> +#define ADC_CHANNEL(_channel, _type, _name, _mask) { \ >>> + .type = _type, \ >> I don't think you use scan_type anywhere so don't bother specifying it. >> here. > > ok. > >>> + .scan_type = IIO_ST('u', 10, 16, 0), \ >>> + .channel = _channel, \ >>> + .info_mask_separate = _mask, \ >>> + .datasheet_name = _name, \ >>> + .indexed = 1, \ >>> +} >>> + >> >> Is the average have an adjustable number of samples? > > The TWL supports reading a value directly (1 sample) or reading > the average of 4 samples. > >> If not I'd be tempted to use the more common option of IIO_CHAN_INFO_RAW >> rather than the average version (tends only to be used in fairly obscure >> cases where both a raw access and an averaged one are available). > > currently some drivers use the averaged read and some use the direct > read. > In that case I'd expose both the 1 sample and averaged versions via the IIO interfaces. If you want to pick 1 then do IIO_CHAN_INFO_RAW not the averaged version. >>> +static const struct iio_chan_spec twl4030_madc_iio_channels[] = { >>> + ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(1, IIO_TEMP, "ADCIN1", BIT(IIO_CHAN_INFO_PROCESSED) | >>> + BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10", BIT(IIO_CHAN_INFO_PROCESSED) | >>> + BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> + ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), >>> +}; >>> + >> Why the artificial limitation of one of these devices? I guess this is to >> allow the exported function to work without needing to be associated with >> any particular device... Hmm. > > Artificial limitation? The madc is a ADC, which has some special > functions defined for some pins in the datasheet: > > ADC0 = Battery Voltage > ADC1 = Battery Temperature > ADC10 = Battery Current > > Nokia misused some of those pins on the Nokia N900, though. For > example they have their own conversion tables for the temperature, > which is not compatible with the generic one for the madc module. *laughs*. If someone documents a pin as having a particular purpose but it isn't wired inside a package. Someone will always think they know better. Basic principal is to never limit ourselves to one instance of a chip because the chances of someone deciding to do something interesting with multiple chips is way to high. > > I planned to convert the rx51-battery driver to simply read the raw > values. All other users can use the processed information. Makes sense. > >>> +static struct twl4030_madc_data *twl4030_madc; >>> + >>> +struct twl4030_prescale_divider_ratios { >>> + s16 numerator; >>> + s16 denominator; >>> +}; >>> + >>> +static const struct twl4030_prescale_divider_ratios >>> +twl4030_divider_ratios[16] = { >>> + {1, 1}, /* CHANNEL 0 No Prescaler */ >>> + {1, 1}, /* CHANNEL 1 No Prescaler */ >>> + {6, 10}, /* CHANNEL 2 */ >>> + {6, 10}, /* CHANNEL 3 */ >>> + {6, 10}, /* CHANNEL 4 */ >>> + {6, 10}, /* CHANNEL 5 */ >>> + {6, 10}, /* CHANNEL 6 */ >>> + {6, 10}, /* CHANNEL 7 */ >>> + {3, 14}, /* CHANNEL 8 */ >>> + {1, 3}, /* CHANNEL 9 */ >>> + {1, 1}, /* CHANNEL 10 No Prescaler */ >>> + {15, 100}, /* CHANNEL 11 */ >>> + {1, 4}, /* CHANNEL 12 */ >>> + {1, 1}, /* CHANNEL 13 Reserved channels */ >>> + {1, 1}, /* CHANNEL 14 Reseved channels */ >>> + {5, 11}, /* CHANNEL 15 */ >>> +}; >>> + >>> + >>> +/* >>> + * Conversion table from -3 to 55 degree Celcius >>> + */ >>> +static int therm_tbl[] = { >>> +30800, 29500, 28300, 27100, >>> +26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700, 17900, >>> +17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100, 12600, 12100, >>> +11600, 11200, 10800, 10400, 10000, 9630, 9280, 8950, 8620, 8310, >>> +8020, 7730, 7460, 7200, 6950, 6710, 6470, 6250, 6040, 5830, >>> +5640, 5450, 5260, 5090, 4920, 4760, 4600, 4450, 4310, 4170, >>> +4040, 3910, 3790, 3670, 3550 >>> +}; >>> + >>> +/* >>> + * Structure containing the registers >>> + * of different conversion methods supported by MADC. >>> + * Hardware or RT real time conversion request initiated by external host >>> + * processor for RT Signal conversions. >>> + * External host processors can also request for non RT conversions >>> + * SW1 and SW2 software conversions also called asynchronous or GPC request. >>> + */ >>> +static >>> +const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = { >>> + [TWL4030_MADC_RT] = { >>> + .sel = TWL4030_MADC_RTSELECT_LSB, >>> + .avg = TWL4030_MADC_RTAVERAGE_LSB, >>> + .rbase = TWL4030_MADC_RTCH0_LSB, >>> + }, >>> + [TWL4030_MADC_SW1] = { >>> + .sel = TWL4030_MADC_SW1SELECT_LSB, >>> + .avg = TWL4030_MADC_SW1AVERAGE_LSB, >>> + .rbase = TWL4030_MADC_GPCH0_LSB, >>> + .ctrl = TWL4030_MADC_CTRL_SW1, >>> + }, >>> + [TWL4030_MADC_SW2] = { >>> + .sel = TWL4030_MADC_SW2SELECT_LSB, >>> + .avg = TWL4030_MADC_SW2AVERAGE_LSB, >>> + .rbase = TWL4030_MADC_GPCH0_LSB, >>> + .ctrl = TWL4030_MADC_CTRL_SW2, >>> + }, >>> +}; >>> + >>> +/* >>> + * Function to read a particular channel value. >>> + * @madc - pointer to struct twl4030_madc_data >>> + * @reg - lsb of ADC Channel >>> + * If the i2c read fails it returns an error else returns 0. >>> + */ >>> +static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg) >>> +{ >>> + u8 msb, lsb; >>> + int ret; >>> + /* >>> + * For each ADC channel, we have MSB and LSB register pair. MSB address >>> + * is always LSB address+1. reg parameter is the address of LSB register >>> + */ >>> + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1); >>> + if (ret) { >>> + dev_err(madc->dev, "unable to read MSB register 0x%X\n", >>> + reg + 1); >>> + return ret; >>> + } >>> + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg); >>> + if (ret) { >>> + dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg); >>> + return ret; >>> + } >>> + >>> + return (int)(((msb << 8) | lsb) >> 6); >>> +} >>> + >>> +/* >>> + * Return battery temperature >>> + * Or < 0 on failure. >>> + */ >>> +static int twl4030battery_temperature(int raw_volt) >>> +{ >>> + u8 val; >>> + int temp, curr, volt, res, ret; >>> + >>> + volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R; >>> + /* Getting and calculating the supply current in micro ampers */ >>> + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, >>> + REG_BCICTL2); >>> + if (ret < 0) >>> + return ret; >> blank line here and after similar error cases makes it a little easier to >> read. >> >>> + curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10; >>> + /* Getting and calculating the thermistor resistance in ohms */ >>> + res = volt * 1000 / curr; >>> + /* calculating temperature */ >>> + for (temp = 58; temp >= 0; temp--) { >>> + int actual = therm_tbl[temp]; >>> + >> No blank line here. > > I added the newline to the style fix patch, since these lines were > not touched by me before. > >>> + if ((actual - res) >= 0) >>> + break; >>> + } >>> + >>> + return temp + 1; >>> +} >>> + >>> +static int twl4030battery_current(int raw_volt) >>> +{ >>> + int ret; >>> + u8 val; >>> + >>> + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, >>> + TWL4030_BCI_BCICTL1); >>> + if (ret) >>> + return ret; >>> + if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */ >>> + return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1; >>> + else /* slope of 0.88 mV/mA */ >>> + return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2; >>> +} >> blank line here please. > > Also added to the style fix patch. > >> Various comments on this in the next function: >> This would be much simpler if any error immediately resulted in an >> exit with error code. If it's gone wrong enough to issue a dev_err >> then don't try to muddle through. Given there is nothing to clear >> up in here, just error out directly on the first problem. > > I guess this should be fixed once the old API is removed. > >>> +/* >>> + * Function to read channel values >>> + * @madc - pointer to twl4030_madc_data struct >>> + * @reg_base - Base address of the first channel >>> + * @Channels - 16 bit bitmap. If the bit is set, channel value is read >>> + * @buf - The channel values are stored here. if read fails error >>> + * @raw - Return raw values without conversion >>> + * value is stored >>> + * Returns the number of successfully read channels. >>> + */ >>> +static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, >>> + u8 reg_base, unsigned >>> + long channels, int *buf, >>> + bool raw) >>> +{ >>> + int count = 0, count_req = 0, i; >>> + u8 reg; >>> + >>> + for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) { >> Skip the temporary for reg. Yes, it gets used twice, but easier to read >> inline. > > Also fixed in the style patch. > >>> + reg = reg_base + 2 * i; >>> + buf[i] = twl4030_madc_channel_raw_read(madc, reg); >>> + if (buf[i] < 0) { >>> + dev_err(madc->dev, >>> + "Unable to read register 0x%X\n", reg); >>> + count_req++; >>> + continue; >>> + } >> I'd prefer this as >> if (raw) { >> count ++; >> } else { >> switch (i) { >> .. >> } > > That increases the code indention by another level. I don't think > that's a good idea. For clarity I'd do it anyway, but if the count is brought out of the statements then the flow will be more obvious anyway. > >> Also near as I can tell count gets incremented unless there is an error >> anyway. So just increment it outside and allow this function to return the >> error code. >> Returning error codes would change the existing API. I would prefer > to do this change after the old API is removed. This function isn't exported directly and I suspect a quick look would show no one relying on the successfully reading some subset of drivers. It might make sense to eat the error at the exported interface and return a length of 0. > >>> + if (raw) { >>> + count++; >>> + continue; >>> + } >>> + switch (i) { >> These cases could do with a suitable #define to give them a name. > > TWL4030_MADC_ADCIN10 is already in use for (1 << 10). I guess this > can be simplified more easily once the old API is removed. > >>> + case 10: >>> + buf[i] = twl4030battery_current(buf[i]); >>> + if (buf[i] < 0) { >>> + dev_err(madc->dev, "err reading current\n"); >>> + count_req++; >>> + } else { >>> + count++; >>> + buf[i] = buf[i] - 750; >>> + } >>> + break; >>> + case 1: >>> + buf[i] = twl4030battery_temperature(buf[i]); >>> + if (buf[i] < 0) { >>> + dev_err(madc->dev, "err reading temperature\n"); >>> + count_req++; >>> + } else { >>> + buf[i] -= 3; >>> + count++; >>> + } >>> + break; >>> + default: >>> + count++; >>> + /* Analog Input (V) = conv_result * step_size / R >>> + * conv_result = decimal value of 10-bit conversion >>> + * result >>> + * step size = 1.5 / (2 ^ 10 -1) >>> + * R = Prescaler ratio for input channels. >>> + * Result given in mV hence multiplied by 1000. >>> + */ >>> + buf[i] = (buf[i] * 3 * 1000 * >>> + twl4030_divider_ratios[i].denominator) >>> + / (2 * 1023 * >>> + twl4030_divider_ratios[i].numerator); >>> + } >>> + } >> This is already apparant from the errors emmited earlier. I'd drop this >> and the count_req counter in general. >> Personally I'd error out in those cases anyway rather than carrying on >> with the rest of the channels. If it's worth of a dev_err it's worthy >> of getting out as fast as possible rather than trying to muddle on. > > I also think this should be postponed until the old API is removed. I'd prefer to see standalone cleanups and fixes integrated first as I suspect it may be a little while before we manage to fully drop the old API. > >>> + if (count_req) >>> + dev_err(madc->dev, "%d channel conversion failed\n", count_req); >>> + >>> + return count; >>> +} >>> + >>> +/* >>> + * Enables irq. >>> + * @madc - pointer to twl4030_madc_data struct >>> + * @id - irq number to be enabled >>> + * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2 >>> + * corresponding to RT, SW1, SW2 conversion requests. >>> + * If the i2c read fails it returns an error else returns 0. >>> + */ >>> +static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id) >>> +{ >>> + u8 val; >>> + int ret; >>> + >>> + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr); >>> + if (ret) { >>> + dev_err(madc->dev, "unable to read imr register 0x%X\n", >>> + madc->imr); >>> + return ret; >>> + } >> A blank line here and in similar cases would slight aid readability. > > > >>> + val &= ~(1 << id); >>> + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); >>> + if (ret) { >>> + dev_err(madc->dev, >>> + "unable to write imr register 0x%X\n", madc->imr); >>> + return ret; >>> + >> Loose blank line here. > > both fixed in the style patch. > >>> + } >>> + >>> + return 0; >>> +} >>> + >>> +/* >>> + * Disables irq. >>> + * @madc - pointer to twl4030_madc_data struct >>> + * @id - irq number to be disabled >>> + * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2 >>> + * corresponding to RT, SW1, SW2 conversion requests. >>> + * Returns error if i2c read/write fails. >>> + */ >>> +static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id) >>> +{ >>> + u8 val; >>> + int ret; >>> + >>> + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr); >>> + if (ret) { >>> + dev_err(madc->dev, "unable to read imr register 0x%X\n", >>> + madc->imr); >>> + return ret; >>> + } >>> + val |= (1 << id); >>> + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); >>> + if (ret) { >>> + dev_err(madc->dev, >>> + "unable to write imr register 0x%X\n", madc->imr); >>> + return ret; >>> + } >>> + >>> + return 0; >>> +} >>> + >>> +static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc) >>> +{ >>> + struct twl4030_madc_data *madc = _madc; >>> + const struct twl4030_madc_conversion_method *method; >>> + u8 isr_val, imr_val; >>> + int i, len, ret; >>> + struct twl4030_madc_request *r; >>> + >>> + mutex_lock(&madc->lock); >>> + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr); >>> + if (ret) { >>> + dev_err(madc->dev, "unable to read isr register 0x%X\n", >>> + madc->isr); >>> + goto err_i2c; >>> + } >>> + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr); >>> + if (ret) { >>> + dev_err(madc->dev, "unable to read imr register 0x%X\n", >>> + madc->imr); >>> + goto err_i2c; >>> + } >>> + isr_val &= ~imr_val; >>> + for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { >>> + if (!(isr_val & (1 << i))) >>> + continue; >>> + ret = twl4030_madc_disable_irq(madc, i); >>> + if (ret < 0) >>> + dev_dbg(madc->dev, "Disable interrupt failed%d\n", i); >>> + madc->requests[i].result_pending = 1; >>> + } >> Can this and the previous loop not be combined thus simplifying some tests? > > It probably can be, but I think this can wait until the old API is > removed. Hmm. As I state below, if this is moving over to IIO (which is sensible) it becomes my problem so I reserve the right to be fussy about less than optimal code. If it was coming in clean as a new driver I'd push back hard on a lot of this stuff before merging it. I have plenty of slightly uggly drivers in staging to look after as it is! > >>> + for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { >>> + r = &madc->requests[i]; >>> + /* No pending results for this method, move to next one */ >>> + if (!r->result_pending) >>> + continue; >>> + method = &twl4030_conversion_methods[r->method]; >>> + /* Read results */ >>> + len = twl4030_madc_read_channels(madc, method->rbase, >>> + r->channels, r->rbuf, r->raw); >>> + /* Return results to caller */ >>> + if (r->func_cb != NULL) { >>> + r->func_cb(len, r->channels, r->rbuf); >>> + r->func_cb = NULL; >>> + } >>> + /* Free request */ >>> + r->result_pending = 0; >>> + r->active = 0; >>> + } >>> + mutex_unlock(&madc->lock); >>> + >>> + return IRQ_HANDLED; >>> + >>> +err_i2c: >>> + /* >>> + * In case of error check whichever request is active >>> + * and service the same. >>> + */ >>> + for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { >>> + r = &madc->requests[i]; >>> + if (r->active == 0) >>> + continue; >>> + method = &twl4030_conversion_methods[r->method]; >>> + /* Read results */ >>> + len = twl4030_madc_read_channels(madc, method->rbase, >>> + r->channels, r->rbuf, r->raw); >>> + /* Return results to caller */ >>> + if (r->func_cb != NULL) { >>> + r->func_cb(len, r->channels, r->rbuf); >>> + r->func_cb = NULL; >>> + } >>> + /* Free request */ >>> + r->result_pending = 0; >>> + r->active = 0; >>> + } >>> + mutex_unlock(&madc->lock); >>> + >>> + return IRQ_HANDLED; >>> +} >>> + >> This structure does seem a little complex. I'd have been more tempted >> by using a completion and having the calling function wait on it. How >> often does an actual callback make sense? > > This is part of the old API, so let's convert all consumers to the > IIO API and deprecate the callback stuff. > >>> +static int twl4030_madc_set_irq(struct twl4030_madc_data *madc, >>> + struct twl4030_madc_request *req) >>> +{ >>> + struct twl4030_madc_request *p; >>> + int ret; >>> + >>> + p = &madc->requests[req->method]; >>> + memcpy(p, req, sizeof(*req)); >>> + ret = twl4030_madc_enable_irq(madc, req->method); >>> + if (ret < 0) { >>> + dev_err(madc->dev, "enable irq failed!!\n"); >>> + return ret; >>> + } >>> + >>> + return 0; >>> +} >>> + >>> +/* >>> + * Function which enables the madc conversion >>> + * by writing to the control register. >>> + * @madc - pointer to twl4030_madc_data struct >>> + * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1 >>> + * corresponding to RT SW1 or SW2 conversion methods. >>> + * Returns 0 if succeeds else a negative error value >>> + */ >>> +static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc, >>> + int conv_method) >>> +{ >>> + const struct twl4030_madc_conversion_method *method; >>> + int ret = 0; >>> + method = &twl4030_conversion_methods[conv_method]; >>> + switch (conv_method) { >> Can we get here via any paths where these aren't the methods set? > > conv_method is set from outside of the driver, so it's probably > safer to check. This can be simplified once the old API is removed. Then we should be returning an error if it's not one of these. > >>> + case TWL4030_MADC_SW1: >>> + case TWL4030_MADC_SW2: >>> + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, >>> + TWL4030_MADC_SW_START, method->ctrl); >>> + if (ret) { >>> + dev_err(madc->dev, >>> + "unable to write ctrl register 0x%X\n", >>> + method->ctrl); >>> + return ret; >>> + } >>> + break; >>> + default: >>> + break; >>> + } >>> + >>> + return 0; >>> +} >>> + >> Could we fix up the kernel doc in this file in general. It's so nearly >> there but not quite! > > I assume, that the driver will get a huge cleanup once all consumers > of the old API are converted. Maybe a deprecation flag should be > added together with this patchset? Probably not worth bothering for an inkernel interface that seems to have relatively few users. How many out of tree users will be moving to new kernels and using this device? > >>> +/* >>> + * Function that waits for conversion to be ready >>> + * @madc - pointer to twl4030_madc_data struct >>> + * @timeout_ms - timeout value in milliseconds >>> + * @status_reg - ctrl register >>> + * returns 0 if succeeds else a negative error value >>> + */ >>> +static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc, >>> + unsigned int timeout_ms, >>> + u8 status_reg) >>> +{ >>> + unsigned long timeout; >>> + int ret; >>> + >>> + timeout = jiffies + msecs_to_jiffies(timeout_ms); >>> + do { >>> + u8 reg; >>> + >>> + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, ®, status_reg); >>> + if (ret) { >>> + dev_err(madc->dev, >>> + "unable to read status register 0x%X\n", >>> + status_reg); >>> + return ret; >>> + } >>> + if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW)) >>> + return 0; >>> + usleep_range(500, 2000); >>> + } while (!time_after(jiffies, timeout)); >>> + dev_err(madc->dev, "conversion timeout!\n"); >>> + >>> + return -EAGAIN; >>> +} >>> + >> >>> +/* >>> + * An exported function which can be called from other kernel drivers. >> What uses this currently? Ideally we'd do this through the standard IIO paths >> for in kernel users. Right now the device tree mappings for that are less >> than ideal (ask Mark Rutland if you want some choice comments ;) Perhaps >> we take the view this can be cleaned up later. There are some elements in here >> we haven't yet looked at supporting for client drivers. Will have to think >> about that. >> >> It would be particularly nice if possible to use a generic battery driver >> for the two cases that seem to be using this functionality at the moment. > > Currently there are 3 consumer drivers using this: > > * twl4030-madc-battery > * rx51-battery > * twl4030-madc-hwmon > > I guess the first two can be updated to use the IIO API and the last > one can be replaced by the IIO hwmon driver. Since it's an > in-kernel-api there should be no problems to simply remove the API > afterwards. Yes that sounds like a good paln. > >>> + * @req twl4030_madc_request structure >>> + * req->rbuf will be filled with read values of channels based on the >>> + * channel index. If a particular channel reading fails there will >>> + * be a negative error value in the corresponding array element. >>> + * returns 0 if succeeds else error value >>> + */ >>> +int twl4030_madc_conversion(struct twl4030_madc_request *req) >>> +{ >>> + const struct twl4030_madc_conversion_method *method; >>> + u8 ch_msb, ch_lsb; >>> + int ret; >>> + >>> + if (!req || !twl4030_madc) >>> + return -EINVAL; >>> + >>> + mutex_lock(&twl4030_madc->lock); >>> + if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) { >>> + ret = -EINVAL; >>> + goto out; >>> + } >>> + /* Do we have a conversion request ongoing */ >>> + if (twl4030_madc->requests[req->method].active) { >>> + ret = -EBUSY; >>> + goto out; >>> + } >>> + ch_msb = (req->channels >> 8) & 0xff; >>> + ch_lsb = req->channels & 0xff; >>> + method = &twl4030_conversion_methods[req->method]; >>> + /* Select channels to be converted */ >>> + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1); >>> + if (ret) { >>> + dev_err(twl4030_madc->dev, >>> + "unable to write sel register 0x%X\n", method->sel + 1); >>> + goto out; >>> + } >>> + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel); >>> + if (ret) { >>> + dev_err(twl4030_madc->dev, >>> + "unable to write sel register 0x%X\n", method->sel + 1); >>> + goto out; >>> + } >>> + /* Select averaging for all channels if do_avg is set */ >>> + if (req->do_avg) { >>> + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, >>> + ch_msb, method->avg + 1); >>> + if (ret) { >>> + dev_err(twl4030_madc->dev, >>> + "unable to write avg register 0x%X\n", >>> + method->avg + 1); >>> + goto out; >>> + } >>> + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, >>> + ch_lsb, method->avg); >>> + if (ret) { >>> + dev_err(twl4030_madc->dev, >> Excesive error messages are sometimes irritating as they break up code >> flow. They are really irritating when incorrect ;) > > Added to the fixes patch. > >>> + "unable to write sel reg 0x%X\n", >>> + method->sel + 1); >>> + goto out; >>> + } >>> + } >>> + if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) { >>> + ret = twl4030_madc_set_irq(twl4030_madc, req); >>> + if (ret < 0) >>> + goto out; >>> + ret = twl4030_madc_start_conversion(twl4030_madc, req->method); >>> + if (ret < 0) >>> + goto out; >>> + twl4030_madc->requests[req->method].active = 1; >>> + ret = 0; >>> + goto out; >>> + } >> Is it possible to get here? This comment suggests not. If so, why have >> the test? > > I guess it's an additional safety check. This function can be > simplified a lot once the old API is removed, so I guess this > can also be postponed. > >>> + /* With RT method we should not be here anymore */ >>> + if (req->method == TWL4030_MADC_RT) { >>> + ret = -EINVAL; >>> + goto out; >>> + } >>> + ret = twl4030_madc_start_conversion(twl4030_madc, req->method); >>> + if (ret < 0) >>> + goto out; >>> + twl4030_madc->requests[req->method].active = 1; >>> + /* Wait until conversion is ready (ctrl register returns EOC) */ >> So no interrupts in this case? >>> + ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl); >>> + if (ret) { >>> + twl4030_madc->requests[req->method].active = 0; >>> + goto out; >>> + } >>> + ret = twl4030_madc_read_channels(twl4030_madc, method->rbase, >>> + req->channels, req->rbuf, req->raw); >>> + twl4030_madc->requests[req->method].active = 0; >>> + >>> +out: >>> + mutex_unlock(&twl4030_madc->lock); >>> + >>> + return ret; >>> +} >>> +EXPORT_SYMBOL_GPL(twl4030_madc_conversion); >>> + >>> +/* >> Proper kerneldoc would be nice here. Otherwise the comment doesn't >> really add anything so I'd be tempted to drop it ;) > > removed in fixes patch. > >>> + * Return channel value >>> + * Or < 0 on failure. >>> + */ >>> +int twl4030_get_madc_conversion(int channel_no) >>> +{ >>> + struct twl4030_madc_request req; >>> + int temp = 0; >>> + int ret; >>> + >>> + req.channels = (1 << channel_no); >>> + req.method = TWL4030_MADC_SW2; >>> + req.active = 0; >>> + req.func_cb = NULL; >>> + ret = twl4030_madc_conversion(&req); >>> + if (ret < 0) >>> + return ret; >>> + if (req.rbuf[channel_no] > 0) >>> + temp = req.rbuf[channel_no]; >>> + >>> + return temp; >>> +} >>> +EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion); >>> + >>> +/* >>> + * Function to enable or disable bias current for >>> + * main battery type reading or temperature sensing >>> + * @madc - pointer to twl4030_madc_data struct >>> + * @chan - can be one of the two values >> What is a battery type reading? Voltage? Current? Charge? > > It's used for battery type identification. Ah the comment really did mean what it said! > >>> + * TWL4030_BCI_ITHEN - Enables bias current for main battery type reading >>> + * TWL4030_BCI_TYPEN - Enables bias current for main battery temperature >> These constants have rather incomprehensible names. Whilst I gues they >> are straight off the data sheet, seems to me that we could make them a little >> longer and easier to follow! The comment here helps, but sensible names >> would be better! > > The names come from the datasheet. I think we should fix this after > the old API is removed, since the constants are exposed for the API. Fair enough. > >>> + * sensing >>> + * @on - enable or disable chan. >>> + */ >>> +static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc, >>> + int chan, int on) >>> +{ >>> + int ret; >>> + u8 regval; >>> + >>> + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, >>> + ®val, TWL4030_BCI_BCICTL1); >>> + if (ret) { >>> + dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X", >>> + TWL4030_BCI_BCICTL1); >>> + return ret; >>> + } >> Introduce an intermediate variable and this could be nice and easy to read >> Say >> >> int regmask; >> if (chan == 0) >> regmask = TWL4030_BCI_TYPEN; >> else >> regmask = TWL4030_BFI_ITHEN; >> >> if (on) >> regval |= regmask; >> else >> regval &= ~regmask; >> >> It's longer but doesn't give me a headache. > > I added this to the fixes patch: > > int regmask; > regmask = (chan == 0) ? TWL4030_BCI_TYPEN : TWL4030_BFI_ITHEN; > if (on) > regval |= regmask; > else > regval &= ~regmask; > >>> + if (on) >>> + regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN; >>> + else >>> + regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN; >>> + ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, >>> + regval, TWL4030_BCI_BCICTL1); >>> + if (ret) { >>> + dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n", >>> + TWL4030_BCI_BCICTL1); >>> + return ret; >>> + } >>> + >>> + return 0; >>> +} >>> + >>> +/* >>> + * Function that sets MADC software power on bit to enable MADC >>> + * @madc - pointer to twl4030_madc_data struct >>> + * @on - Enable or disable MADC software powen on bit. >>> + * returns error if i2c read/write fails else 0 >>> + */ >>> +static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) >>> +{ >>> + u8 regval; >>> + int ret; >>> + >>> + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, >>> + ®val, TWL4030_MADC_CTRL1); >>> + if (ret) { >>> + dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n", >>> + TWL4030_MADC_CTRL1); >>> + return ret; >>> + } >>> + if (on) >>> + regval |= TWL4030_MADC_MADCON; >>> + else >>> + regval &= ~TWL4030_MADC_MADCON; >>> + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1); >>> + if (ret) { >>> + dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n", >>> + TWL4030_MADC_CTRL1); >>> + return ret; >>> + } >>> + >>> + return 0; >>> +} >>> + >>> +/* >>> + * Initialize MADC and request for threaded irq >>> + */ >>> +static int twl4030_madc_probe(struct platform_device *pdev) >>> +{ >>> + struct twl4030_madc_data *madc; >>> + struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev); >>> + struct device_node *np = pdev->dev.of_node; >>> + int irq, ret; >>> + u8 regval; >>> + struct iio_dev *iio_dev = NULL; >>> + >>> + if (!pdata && !np) { >>> + dev_err(&pdev->dev, "platform_data not available\n"); >>> + return -EINVAL; >>> + } >>> + >>> + iio_dev = devm_iio_device_alloc(&pdev->dev, >>> + sizeof(struct twl4030_madc_data)); >>> + if (!iio_dev) { >>> + dev_err(&pdev->dev, "failed allocating iio device\n"); >>> + return -ENOMEM; >>> + } >>> + >>> + madc = iio_priv(iio_dev); >>> + madc->dev = &pdev->dev; >>> + >>> + iio_dev->name = dev_name(&pdev->dev); >>> + iio_dev->dev.parent = &pdev->dev; >>> + iio_dev->dev.of_node = pdev->dev.of_node; >>> + iio_dev->info = &twl4030_madc_iio_info; >>> + iio_dev->modes = INDIO_DIRECT_MODE; >>> + iio_dev->channels = twl4030_madc_iio_channels; >>> + iio_dev->num_channels = 16; >>> + >>> + /* >>> + * Phoenix provides 2 interrupt lines. The first one is connected to >>> + * the OMAP. The other one can be connected to the other processor such >>> + * as modem. Hence two separate ISR and IMR registers. >>> + */ >>> + if (pdata) >>> + madc->use_second_irq = pdata->irq_line != 1; >>> + else >>> + madc->use_second_irq = false; >>> + >>> + madc->imr = (madc->use_second_irq == 1) ? >>> + TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2; >>> + madc->isr = (madc->use_second_irq == 1) ? >>> + TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2; >>> + >>> + ret = twl4030_madc_set_power(madc, 1); >>> + if (ret < 0) >>> + return ret; >>> + ret = twl4030_madc_set_current_generator(madc, 0, 1); >>> + if (ret < 0) >>> + goto err_current_generator; >>> + >>> + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, >>> + ®val, TWL4030_BCI_BCICTL1); >>> + if (ret) { >>> + dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n", >>> + TWL4030_BCI_BCICTL1); >>> + goto err_i2c; >>> + } >>> + regval |= TWL4030_BCI_MESBAT; >>> + ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, >>> + regval, TWL4030_BCI_BCICTL1); >>> + if (ret) { >>> + dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n", >>> + TWL4030_BCI_BCICTL1); >>> + goto err_i2c; >>> + } >>> + >>> + /* Check that MADC clock is on */ >>> + ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, ®val, TWL4030_REG_GPBR1); >>> + if (ret) { >> Personally I'd rank the driver as rather to vebose on read error messages >> given they are pretty unusual. Ah well I guess each to their own. > > All of this is not written by me. Sure. Doesn't stop me moaning :) At the end of the day you are proposing moving this driver into a place where it becomes at least partly my problem. As such I'm keen on any tidying / cleaning occuring ASAP. > >>> + dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n", >>> + TWL4030_REG_GPBR1); >>> + goto err_i2c; >>> + } >>> + >>> + /* If MADC clk is not on, turn it on */ >>> + if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) { >>> + dev_info(&pdev->dev, "clk disabled, enabling\n"); >>> + regval |= TWL4030_GPBR1_MADC_HFCLK_EN; >>> + ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval, >>> + TWL4030_REG_GPBR1); >>> + if (ret) { >>> + dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n", >>> + TWL4030_REG_GPBR1); >>> + goto err_i2c; >>> + } >>> + } >>> + >>> + platform_set_drvdata(pdev, iio_dev); >>> + mutex_init(&madc->lock); >>> + >>> + irq = platform_get_irq(pdev, 0); >> >> As ever using devm for a irq request makes for a bit of a review >> nightmare as we have to be very careful that nothing has been removed >> that this might use before the devm cleanup occurs (at end of remove). >> In this case the fact that the current generator is off and the adc unit >> is disabled sounds to me like it might cause interesting results in the >> interrupt handler? >> >> It's one of those things that will probably never happen but I find it >> hard to convince myself 'can't happen' >> >>> + ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, >>> + twl4030_madc_threaded_irq_handler, >>> + IRQF_TRIGGER_RISING, "twl4030_madc", madc); >>> + if (ret) { >>> + dev_dbg(&pdev->dev, "could not request irq\n"); >>> + goto err_i2c; >>> + } >> >> Err, where is this defined? Ah, found it above. I'm not keen on >> preventing multiple instances of a device like this. It is so easy >> to not do this I'd prefer to see the driver fixed to remove this. > > This concept has also not introduced by me of course. I think we > should fix this after removal of the old API, since the old API does > not have an device parameter. Fair enough. > >>> + twl4030_madc = madc; >>> + >>> + ret = iio_device_register(iio_dev); >>> + if (ret) { >>> + dev_dbg(&pdev->dev, "could not register iio device\n"); >>> + goto err_i2c; >>> + } >>> + >>> + return 0; >>> + >>> +err_i2c: >>> + twl4030_madc_set_current_generator(madc, 0, 0); >>> +err_current_generator: >>> + twl4030_madc_set_power(madc, 0); >>> + return ret; >>> +} >>> + >>> +static int twl4030_madc_remove(struct platform_device *pdev) >>> +{ >>> + struct iio_dev *iio_dev = platform_get_drvdata(pdev); >>> + struct twl4030_madc_data *madc = iio_priv(iio_dev); >>> + >>> + twl4030_madc_set_current_generator(madc, 0, 0); >>> + twl4030_madc_set_power(madc, 0); >>> + >>> + iio_device_unregister(iio_dev); >>> + >>> + return 0; >>> +} >>> + >>> +#ifdef CONFIG_OF >>> +static const struct of_device_id twl_madc_of_match[] = { >>> + {.compatible = "ti,twl4030-madc", }, >>> + { }, >>> +}; >>> +MODULE_DEVICE_TABLE(of, twl_madc_of_match); >>> +#endif >>> + >>> +static struct platform_driver twl4030_madc_driver = { >>> + .probe = twl4030_madc_probe, >>> + .remove = twl4030_madc_remove, >>> + .driver = { >>> + .name = "twl4030_madc", >>> + .owner = THIS_MODULE, >>> + .of_match_table = of_match_ptr(twl_madc_of_match), >>> + }, >>> +}; >>> + >>> +module_platform_driver(twl4030_madc_driver); >>> + >>> +MODULE_DESCRIPTION("TWL4030 ADC driver"); >>> +MODULE_LICENSE("GPL"); >>> +MODULE_AUTHOR("J Keerthy"); >>> +MODULE_ALIAS("platform:twl4030_madc"); >> >> Rest of patch was removal of code so no comments! > > FYI: For me it was a bit odd to see inline comments about code lines > coming after the inline comment. Normally I receive patch reviews > with comments after the referenced code, which makes more sense imho. I never claimed to be consistent ;) Tend to review backwards that means that sometimes comments end up in fairly random places. > > -- Sebastian > -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi, On Sun, Feb 23, 2014 at 11:02:14AM +0000, Jonathan Cameron wrote: > On 23/02/14 00:35, Sebastian Reichel wrote: > >I think the optimal workflow is: > > > >1. convert madc driver to IIO > >2. convert twl4030-madc-battery driver to IIO API > >3. convert rx51-battery to IIO API > >4. convert twl4030-madc-hwmon to IIO API / deprecate it > >5. remove old in-kernel ABI from madc > >6. cleanup/simplify madc > > > >I guess its much simpler to do the driver cleanup/simplification > >once we can get rid of the old API. > > Perhaps. I guess it depends on your planned schedule. If you are > intending to plough through the above in the near future then fair > enough. If the chances are it'll take a while, I'd be inclined to > fix some of the more glarringly hideous bits now (such as error > code reporting) and then visit your list above over time. I plan to update the rx51-battery driver, since that's the only one I can test. Actually I only did the changes to the twl4030-madc driver, so that I can use the standard DT API for AD converters for rx51-battery. I hope to get it updated ASAP. Marek seems to work on getting DT support for the other two consumers (twl4030-madc-battery and twl4030-madc-hwmon), so chances are, that the old API can be removed in the near future :) > Note that to my mind it is converted drivers like this that sneak some > nasty code into an a subsystem I look after. We have a lot of 'interesting' > code out in staging still, but for new submissions we are being increasingly > fussy about little bits. So I hope I don't come across as being too mean > about this driver. I'm glad you are putting in the work to convert > it over. There are a quite a few similar drivers sculking through the tree > so it's nice that someone has gotten started on bringing one over to IIO. If it makes you feel more at ease I will try to forward the errors and replace the error code with 0 at the end. > [...] > > >>Is the average have an adjustable number of samples? > > > >The TWL supports reading a value directly (1 sample) or reading > >the average of 4 samples. > > > >>If not I'd be tempted to use the more common option of IIO_CHAN_INFO_RAW > >>rather than the average version (tends only to be used in fairly obscure > >>cases where both a raw access and an averaged one are available). > > > >currently some drivers use the averaged read and some use the direct > >read. > > > > In that case I'd expose both the 1 sample and averaged versions via the > IIO interfaces. If you want to pick 1 then do IIO_CHAN_INFO_RAW not the > averaged version. That's what I intended to do. Now I see, that I forgot to expose IIO_CHAN_INFO_RAW. Sorry for the confusion. > >>>+static const struct iio_chan_spec twl4030_madc_iio_channels[] = { > >>>+ ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(1, IIO_TEMP, "ADCIN1", BIT(IIO_CHAN_INFO_PROCESSED) | > >>>+ BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10", BIT(IIO_CHAN_INFO_PROCESSED) | > >>>+ BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+ ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", BIT(IIO_CHAN_INFO_AVERAGE_RAW)), > >>>+}; > >>>+ > >>Why the artificial limitation of one of these devices? I guess this is to > >>allow the exported function to work without needing to be associated with > >>any particular device... Hmm. > > > >Artificial limitation? The madc is a ADC, which has some special > >functions defined for some pins in the datasheet: > > > >ADC0 = Battery Voltage > >ADC1 = Battery Temperature > >ADC10 = Battery Current > > > >Nokia misused some of those pins on the Nokia N900, though. For > >example they have their own conversion tables for the temperature, > >which is not compatible with the generic one for the madc module. > > *laughs*. If someone documents a pin as having a particular purpose > but it isn't wired inside a package. Someone will always think they know > better. Basic principal is to never limit ourselves to one instance of > a chip because the chances of someone deciding to do something interesting > with multiple chips is way to high. ;) > > > >I planned to convert the rx51-battery driver to simply read the raw > >values. All other users can use the processed information. > Makes sense. ok :) > [...] > >>>+ if (count_req) > >>>+ dev_err(madc->dev, "%d channel conversion failed\n", count_req); > >>>+ > >>>+ return count; > >>This is already apparant from the errors emmited earlier. I'd drop this > >>and the count_req counter in general. > >>Personally I'd error out in those cases anyway rather than carrying on > >>with the rest of the channels. If it's worth of a dev_err it's worthy > >>of getting out as fast as possible rather than trying to muddle on. > > > >I also think this should be postponed until the old API is removed. > I'd prefer to see standalone cleanups and fixes integrated first as I suspect > it may be a little while before we manage to fully drop the old API. ok. I have included this change in the fixup patch. This slightly changes the old API (callback may be called with negative error values), but the callback feature doesn't seem to be used by any mainline user. > [...] > >>Can this and the previous loop not be combined thus simplifying some tests? > > > >It probably can be, but I think this can wait until the old API is > >removed. > Hmm. As I state below, if this is moving over to IIO (which is sensible) it > becomes my problem so I reserve the right to be fussy about less than optimal > code. If it was coming in clean as a new driver I'd push back hard on a lot > of this stuff before merging it. I have plenty of slightly uggly drivers > in staging to look after as it is! I would feel more comfortable leaving the second loop as is for now. It's a speed optimization, which acknowledges all IRQs as fast as possible (=> before triggering further conversion of the raw data). > [...] > >>>+static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc, > >>>+ int conv_method) > >>>+{ > >>>+ const struct twl4030_madc_conversion_method *method; > >>>+ int ret = 0; > >>>+ method = &twl4030_conversion_methods[conv_method]; > >>>+ switch (conv_method) { > >>Can we get here via any paths where these aren't the methods set? > > > >conv_method is set from outside of the driver, so it's probably > >safer to check. This can be simplified once the old API is removed. > Then we should be returning an error if it's not one of these. right and reading the code more carefully it should be tested before using it for array access. > >I assume, that the driver will get a huge cleanup once all consumers > >of the old API are converted. Maybe a deprecation flag should be > >added together with this patchset? > Probably not worth bothering for an inkernel interface that seems to have > relatively few users. How many out of tree users will be moving to > new kernels and using this device? At least no new twl4030-madc consumer has been posted to linux-kernel or linux-omap in the last months. So I will ignore this. > [...] > >>Personally I'd rank the driver as rather to vebose on read error messages > >>given they are pretty unusual. Ah well I guess each to their own. > > > >All of this is not written by me. > Sure. Doesn't stop me moaning :) At the end of the day you are proposing moving > this driver into a place where it becomes at least partly my problem. As such > I'm keen on any tidying / cleaning occuring ASAP. Fair enough. -- Sebastian
Hi, This is RFCv2 for converting twl4030-madc to the IIO API and adding DT support. The patchset compiles, but has not yet been tested on real hardware. I plan to convert rx51-battery to the IIO framework in the near future and test this patchset on my Nokia N900. Changes since RFCv1: * Add ti,system-uses-second-madc-irq DT property needed by Belisko Marek * Fix issues reported by Jonathan Cameron TODO: * Test patchset on real hardware * Convert consumer drivers to IIO API - rx51-battery - twl4030-madc-battery - twl4030-madc-hwmon (=> deprecate in favour of iio-hwmon) * remove old kernel API from twl4030-madc -- Sebastian Sebastian Reichel (5): mfd: twl4030-madc: Use managed resources mfd: twl4030-madc: Add DT support and convert to IIO framework mfd: twl4030-madc: Cleanup driver Documentation: DT: Document twl4030-madc binding mfd: twl4030-madc: Move driver to drivers/iio/adc .../devicetree/bindings/iio/adc/twl4030-madc.txt | 24 + drivers/iio/adc/Kconfig | 10 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/twl4030-madc.c | 914 +++++++++++++++++++++ drivers/mfd/Kconfig | 10 - drivers/mfd/Makefile | 1 - drivers/mfd/twl4030-madc.c | 818 ------------------ 7 files changed, 949 insertions(+), 829 deletions(-) create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt create mode 100644 drivers/iio/adc/twl4030-madc.c delete mode 100644 drivers/mfd/twl4030-madc.c
On Sun, 2014-02-23 at 23:07 +0100, Sebastian Reichel wrote: > This is a driver for an A/D converter, which belongs into > drivers/iio/adc. Please use git format-patch -M > diff --git a/drivers/iio/adc/twl4030-madc.c b/drivers/iio/adc/twl4030-madc.c [] > +static int twl4030_madc_read(struct iio_dev *iio_dev, > + const struct iio_chan_spec *chan, > + int *val, int *val2, long mask) > +{ [] > + req.do_avg = !!(mask & IIO_CHAN_INFO_AVERAGE_RAW); It looks like do_avg should be bool not u16 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
> This is a driver for an A/D converter, which belongs into > drivers/iio/adc. like! please find some comments below; not all might relate to the iio conversion I plan to test this on a beagleboard-xm I always needed the following setup to make the twl4030_madc work for me /* TWL4030 CATKIT_ANA_CTRL Register */ #define TWL4030_CARKIT_ANA_CTRL 0xBB #define SEL_MADC_MCPC (1 << 3) /* * set SEL_MADC_MCPC in CARKIT_ANA_CTRL * makes ADCIN3 to ADCIN6 available to the MADC */ twl_i2c_read_u8(TWL4030_MODULE_USB, &pin_mux, TWL4030_CARKIT_ANA_CTRL); pin_mux |= SEL_MADC_MCPC; twl_i2c_write_u8(TWL4030_MODULE_USB, pin_mux, TWL4030_CARKIT_ANA_CTRL); regards, p. > Signed-off-by: Sebastian Reichel <sre@debian.org> > --- > drivers/iio/adc/Kconfig | 10 + > drivers/iio/adc/Makefile | 1 + > drivers/iio/adc/twl4030-madc.c | 914 +++++++++++++++++++++++++++++++++++++++++ > drivers/mfd/Kconfig | 10 - > drivers/mfd/Makefile | 1 - > drivers/mfd/twl4030-madc.c | 914 ----------------------------------------- > 6 files changed, 925 insertions(+), 925 deletions(-) > create mode 100644 drivers/iio/adc/twl4030-madc.c > delete mode 100644 drivers/mfd/twl4030-madc.c > > diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig > index 2209f28..427f75c 100644 > --- a/drivers/iio/adc/Kconfig > +++ b/drivers/iio/adc/Kconfig > @@ -183,6 +183,16 @@ config TI_AM335X_ADC > Say yes here to build support for Texas Instruments ADC > driver which is also a MFD client. > > +config TWL4030_MADC > + tristate "TWL4030 MADC (Monitoring A/D Converter)" > + depends on TWL4030_CORE > + help > + This driver provides support for Triton TWL4030-MADC. The > + driver supports both RT and SW conversion methods. > + > + This driver can also be built as a module. If so, the module will be > + called twl4030-madc. > + > config TWL6030_GPADC > tristate "TWL6030 GPADC (General Purpose A/D Converter) Support" > depends on TWL4030_CORE > diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile > index ba9a10a..9acf2df 100644 > --- a/drivers/iio/adc/Makefile > +++ b/drivers/iio/adc/Makefile > @@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o > obj-$(CONFIG_NAU7802) += nau7802.o > obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o > obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o > +obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o > obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o > obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o > diff --git a/drivers/iio/adc/twl4030-madc.c b/drivers/iio/adc/twl4030-madc.c > new file mode 100644 > index 0000000..0bc79a7 > --- /dev/null > +++ b/drivers/iio/adc/twl4030-madc.c > @@ -0,0 +1,914 @@ > +/* > + * > + * TWL4030 MADC module driver-This driver monitors the real time > + * conversion of analog signals like battery temperature, > + * battery type, battery level etc. > + * > + * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ > + * J Keerthy <j-keerthy@ti.com> > + * > + * Based on twl4030-madc.c > + * Copyright (C) 2008 Nokia Corporation > + * Mikko Ylinen <mikko.k.ylinen@nokia.com> > + * > + * Amit Kucheria <amit.kucheria@canonical.com> > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU General Public License > + * version 2 as published by the Free Software Foundation. > + * > + * This program is distributed in the hope that it will be useful, but > + * WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * General Public License for more details. > + * > + * You should have received a copy of the GNU General Public License > + * along with this program; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA > + * 02110-1301 USA > + * > + */ > + > +#include <linux/init.h> > +#include <linux/device.h> > +#include <linux/interrupt.h> > +#include <linux/kernel.h> > +#include <linux/delay.h> > +#include <linux/platform_device.h> > +#include <linux/slab.h> > +#include <linux/i2c/twl.h> > +#include <linux/i2c/twl4030-madc.h> > +#include <linux/module.h> > +#include <linux/stddef.h> > +#include <linux/mutex.h> > +#include <linux/bitops.h> > +#include <linux/jiffies.h> > +#include <linux/types.h> > +#include <linux/gfp.h> > +#include <linux/err.h> > + > +#include <linux/iio/iio.h> > + > +/* this could/should be kerneldoc, hecen /** > + * struct twl4030_madc_data - a container for madc info > + * @dev - pointer to device structure for madc > + * @lock - mutex protecting this data structure > + * @requests - Array of request struct corresponding to SW1, SW2 and RT > + * @imr - Interrupt mask register of MADC > + * @isr - Interrupt status register of MADC > + */ > +struct twl4030_madc_data { > + struct device *dev; > + struct mutex lock; /* mutex protecting this data structure */ > + struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS]; > + bool use_second_irq; > + u8 imr; > + u8 isr; > +}; > + > +static int twl4030_madc_read(struct iio_dev *iio_dev, > + const struct iio_chan_spec *chan, > + int *val, int *val2, long mask) > +{ > + struct twl4030_madc_data *madc = iio_priv(iio_dev); > + struct twl4030_madc_request req; > + int channel = chan->channel; > + int ret; > + > + req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1; > + > + req.channels = BIT(channel); > + req.active = 0; > + req.func_cb = NULL; > + req.raw = !(mask & IIO_CHAN_INFO_PROCESSED); > + req.do_avg = !!(mask & IIO_CHAN_INFO_AVERAGE_RAW); > + > + ret = twl4030_madc_conversion(&req); > + if (ret < 0) > + return ret; > + > + *val = req.rbuf[channel]; > + > + return IIO_VAL_INT; > +} > + > +static const struct iio_info twl4030_madc_iio_info = { > + .read_raw = &twl4030_madc_read, > + .driver_module = THIS_MODULE, > +}; > + > +#define TWL4030_ADC_CHANNEL(_channel, _type, _name, _mask) { \ > + .type = _type, \ > + .channel = _channel, \ > + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ > + BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \ > + _mask, \ > + .datasheet_name = _name, \ > + .indexed = 1, \ > +} > + > +static const struct iio_chan_spec twl4030_madc_iio_channels[] = { > + TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", 0), > + TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1", > + BIT(IIO_CHAN_INFO_PROCESSED)), > + TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", 0), > + TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", 0), > + TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", 0), > + TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", 0), > + TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", 0), > + TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", 0), > + TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", 0), > + TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", 0), > + TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10", > + BIT(IIO_CHAN_INFO_PROCESSED)), > + TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", 0), > + TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", 0), > + TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", 0), > + TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", 0), > + TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", 0), > +}; > + > +static struct twl4030_madc_data *twl4030_madc; > + > +struct twl4030_prescale_divider_ratios { > + s16 numerator; > + s16 denominator; > +}; > + > +static const struct twl4030_prescale_divider_ratios > +twl4030_divider_ratios[16] = { > + {1, 1}, /* CHANNEL 0 No Prescaler */ > + {1, 1}, /* CHANNEL 1 No Prescaler */ > + {6, 10}, /* CHANNEL 2 */ > + {6, 10}, /* CHANNEL 3 */ > + {6, 10}, /* CHANNEL 4 */ > + {6, 10}, /* CHANNEL 5 */ > + {6, 10}, /* CHANNEL 6 */ > + {6, 10}, /* CHANNEL 7 */ > + {3, 14}, /* CHANNEL 8 */ > + {1, 3}, /* CHANNEL 9 */ > + {1, 1}, /* CHANNEL 10 No Prescaler */ > + {15, 100}, /* CHANNEL 11 */ > + {1, 4}, /* CHANNEL 12 */ > + {1, 1}, /* CHANNEL 13 Reserved channels */ > + {1, 1}, /* CHANNEL 14 Reseved channels */ > + {5, 11}, /* CHANNEL 15 */ > +}; > + > + > +/* > + * Conversion table from -3 to 55 degree Celcius > + */ could use a single-line comment > +static int therm_tbl[] = { no twl4030_ prefix here > +30800, 29500, 28300, 27100, > +26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700, 17900, > +17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100, 12600, 12100, > +11600, 11200, 10800, 10400, 10000, 9630, 9280, 8950, 8620, 8310, > +8020, 7730, 7460, 7200, 6950, 6710, 6470, 6250, 6040, 5830, > +5640, 5450, 5260, 5090, 4920, 4760, 4600, 4450, 4310, 4170, > +4040, 3910, 3790, 3670, 3550 some indentation would be nice > +}; > + > +/* > + * Structure containing the registers > + * of different conversion methods supported by MADC. > + * Hardware or RT real time conversion request initiated by external host > + * processor for RT Signal conversions. > + * External host processors can also request for non RT conversions > + * SW1 and SW2 software conversions also called asynchronous or GPC request. > + */ > +static > +const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = { > + [TWL4030_MADC_RT] = { > + .sel = TWL4030_MADC_RTSELECT_LSB, > + .avg = TWL4030_MADC_RTAVERAGE_LSB, > + .rbase = TWL4030_MADC_RTCH0_LSB, > + }, > + [TWL4030_MADC_SW1] = { > + .sel = TWL4030_MADC_SW1SELECT_LSB, > + .avg = TWL4030_MADC_SW1AVERAGE_LSB, > + .rbase = TWL4030_MADC_GPCH0_LSB, > + .ctrl = TWL4030_MADC_CTRL_SW1, > + }, > + [TWL4030_MADC_SW2] = { > + .sel = TWL4030_MADC_SW2SELECT_LSB, > + .avg = TWL4030_MADC_SW2AVERAGE_LSB, > + .rbase = TWL4030_MADC_GPCH0_LSB, > + .ctrl = TWL4030_MADC_CTRL_SW2, > + }, > +}; > + > +/* /** > + * Function to read a particular channel value. > + * @madc - pointer to struct twl4030_madc_data > + * @reg - lsb of ADC Channel > + * If the i2c read fails it returns an error else returns 0. > + */ > +static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg) > +{ > + u8 msb, lsb; > + int ret; > + /* > + * For each ADC channel, we have MSB and LSB register pair. MSB address > + * is always LSB address+1. reg parameter is the address of LSB register > + */ > + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1); > + if (ret) { > + dev_err(madc->dev, "unable to read MSB register 0x%X\n", > + reg + 1); > + return ret; > + } > + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg); > + if (ret) { > + dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg); > + return ret; > + } could this be a word/u16 read? I haven't checked if there is autoincrement support... > + > + return (int)(((msb << 8) | lsb) >> 6); > +} > + > +/* > + * Return battery temperature stating the unit should nice > + * Or < 0 on failure. > + */ > +static int twl4030battery_temperature(int raw_volt) > +{ > + u8 val; > + int temp, curr, volt, res, ret; > + > + volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R; > + /* Getting and calculating the supply current in micro ampers */ amperes > + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, > + REG_BCICTL2); > + if (ret < 0) > + return ret; > + > + curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10; > + /* Getting and calculating the thermistor resistance in ohms */ > + res = volt * 1000 / curr; > + /* calculating temperature */ > + for (temp = 58; temp >= 0; temp--) { > + int actual = therm_tbl[temp]; > + if ((actual - res) >= 0) > + break; > + } > + > + return temp + 1; > +} > + > +static int twl4030battery_current(int raw_volt) > +{ > + int ret; > + u8 val; > + > + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, > + TWL4030_BCI_BCICTL1); > + if (ret) > + return ret; > + if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */ > + return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1; > + else /* slope of 0.88 mV/mA */ > + return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2; > +} > + > +/* > + * Function to read channel values > + * @madc - pointer to twl4030_madc_data struct > + * @reg_base - Base address of the first channel > + * @Channels - 16 bit bitmap. If the bit is set, channel value is read channels > + * @buf - The channel values are stored here. if read fails error > + * @raw - Return raw values without conversion > + * value is stored > + * Returns the number of successfully read channels. > + */ > +static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, > + u8 reg_base, unsigned > + long channels, int *buf, > + bool raw) > +{ > + int count = 0, i; > + > + for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) { > + buf[i] = twl4030_madc_channel_raw_read(madc, reg_base + 2 * i); > + if (buf[i] < 0) { > + dev_err(madc->dev, "Unable to read register 0x%X\n", > + reg_base + 2 * i); > + return buf[i]; > + } > + if (raw) { > + count++; > + continue; > + } > + switch (i) { > + case 10: > + buf[i] = twl4030battery_current(buf[i]); > + if (buf[i] < 0) { > + dev_err(madc->dev, "err reading current\n"); > + return buf[i]; > + } else { > + count++; > + buf[i] = buf[i] - 750; > + } > + break; > + case 1: > + buf[i] = twl4030battery_temperature(buf[i]); > + if (buf[i] < 0) { > + dev_err(madc->dev, "err reading temperature\n"); > + return buf[i]; > + } else { > + buf[i] -= 3; > + count++; > + } > + break; > + default: > + count++; > + /* Analog Input (V) = conv_result * step_size / R > + * conv_result = decimal value of 10-bit conversion > + * result > + * step size = 1.5 / (2 ^ 10 -1) > + * R = Prescaler ratio for input channels. > + * Result given in mV hence multiplied by 1000. > + */ > + buf[i] = (buf[i] * 3 * 1000 * > + twl4030_divider_ratios[i].denominator) > + / (2 * 1023 * > + twl4030_divider_ratios[i].numerator); > + } > + } > + > + return count; > +} > + > +/* > + * Enables irq. > + * @madc - pointer to twl4030_madc_data struct > + * @id - irq number to be enabled > + * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2 > + * corresponding to RT, SW1, SW2 conversion requests. > + * If the i2c read fails it returns an error else returns 0. > + */ > +static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id) > +{ > + u8 val; > + int ret; > + > + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr); > + if (ret) { > + dev_err(madc->dev, "unable to read imr register 0x%X\n", > + madc->imr); > + return ret; > + } > + > + val &= ~(1 << id); > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); > + if (ret) { > + dev_err(madc->dev, > + "unable to write imr register 0x%X\n", madc->imr); > + return ret; > + } > + > + return 0; > +} > + > +/* > + * Disables irq. > + * @madc - pointer to twl4030_madc_data struct > + * @id - irq number to be disabled > + * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2 > + * corresponding to RT, SW1, SW2 conversion requests. > + * Returns error if i2c read/write fails. > + */ > +static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id) > +{ > + u8 val; > + int ret; > + > + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr); > + if (ret) { > + dev_err(madc->dev, "unable to read imr register 0x%X\n", > + madc->imr); > + return ret; > + } > + val |= (1 << id); > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); > + if (ret) { > + dev_err(madc->dev, > + "unable to write imr register 0x%X\n", madc->imr); > + return ret; > + } > + > + return 0; > +} > + > +static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc) > +{ > + struct twl4030_madc_data *madc = _madc; > + const struct twl4030_madc_conversion_method *method; > + u8 isr_val, imr_val; > + int i, len, ret; > + struct twl4030_madc_request *r; > + > + mutex_lock(&madc->lock); > + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr); > + if (ret) { > + dev_err(madc->dev, "unable to read isr register 0x%X\n", > + madc->isr); > + goto err_i2c; > + } > + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr); > + if (ret) { > + dev_err(madc->dev, "unable to read imr register 0x%X\n", > + madc->imr); > + goto err_i2c; > + } > + isr_val &= ~imr_val; > + for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { > + if (!(isr_val & (1 << i))) > + continue; > + ret = twl4030_madc_disable_irq(madc, i); > + if (ret < 0) > + dev_dbg(madc->dev, "Disable interrupt failed%d\n", i); some space before %d > + madc->requests[i].result_pending = 1; > + } > + for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { > + r = &madc->requests[i]; > + /* No pending results for this method, move to next one */ > + if (!r->result_pending) > + continue; > + method = &twl4030_conversion_methods[r->method]; > + /* Read results */ > + len = twl4030_madc_read_channels(madc, method->rbase, > + r->channels, r->rbuf, r->raw); > + /* Return results to caller */ > + if (r->func_cb != NULL) { > + r->func_cb(len, r->channels, r->rbuf); > + r->func_cb = NULL; > + } > + /* Free request */ > + r->result_pending = 0; > + r->active = 0; > + } > + mutex_unlock(&madc->lock); > + > + return IRQ_HANDLED; > + > +err_i2c: > + /* > + * In case of error check whichever request is active > + * and service the same. > + */ > + for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { > + r = &madc->requests[i]; > + if (r->active == 0) > + continue; > + method = &twl4030_conversion_methods[r->method]; > + /* Read results */ > + len = twl4030_madc_read_channels(madc, method->rbase, > + r->channels, r->rbuf, r->raw); > + /* Return results to caller */ > + if (r->func_cb != NULL) { > + r->func_cb(len, r->channels, r->rbuf); > + r->func_cb = NULL; > + } > + /* Free request */ > + r->result_pending = 0; > + r->active = 0; > + } > + mutex_unlock(&madc->lock); > + > + return IRQ_HANDLED; > +} > + > +static int twl4030_madc_set_irq(struct twl4030_madc_data *madc, > + struct twl4030_madc_request *req) > +{ > + struct twl4030_madc_request *p; > + int ret; > + > + p = &madc->requests[req->method]; > + memcpy(p, req, sizeof(*req)); > + ret = twl4030_madc_enable_irq(madc, req->method); > + if (ret < 0) { > + dev_err(madc->dev, "enable irq failed!!\n"); > + return ret; > + } > + > + return 0; > +} > + > +/* > + * Function which enables the madc conversion > + * by writing to the control register. > + * @madc - pointer to twl4030_madc_data struct > + * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1 > + * corresponding to RT SW1 or SW2 conversion methods. > + * Returns 0 if succeeds else a negative error value > + */ > +static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc, > + int conv_method) > +{ > + const struct twl4030_madc_conversion_method *method; > + int ret = 0; > + > + if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2) > + return -ENOTSUPP; > + > + method = &twl4030_conversion_methods[conv_method]; > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START, > + method->ctrl); > + if (ret) { > + dev_err(madc->dev, "unable to write ctrl register 0x%X\n", > + method->ctrl); > + return ret; > + } > + > + return 0; > +} > + > +/* > + * Function that waits for conversion to be ready > + * @madc - pointer to twl4030_madc_data struct > + * @timeout_ms - timeout value in milliseconds > + * @status_reg - ctrl register > + * returns 0 if succeeds else a negative error value > + */ > +static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc, > + unsigned int timeout_ms, > + u8 status_reg) > +{ > + unsigned long timeout; > + int ret; > + > + timeout = jiffies + msecs_to_jiffies(timeout_ms); > + do { > + u8 reg; > + > + ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, ®, status_reg); > + if (ret) { > + dev_err(madc->dev, > + "unable to read status register 0x%X\n", > + status_reg); > + return ret; > + } > + if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW)) > + return 0; > + usleep_range(500, 2000); > + } while (!time_after(jiffies, timeout)); > + dev_err(madc->dev, "conversion timeout!\n"); > + > + return -EAGAIN; > +} > + > +/* > + * An exported function which can be called from other kernel drivers. > + * @req twl4030_madc_request structure > + * req->rbuf will be filled with read values of channels based on the > + * channel index. If a particular channel reading fails there will > + * be a negative error value in the corresponding array element. > + * returns 0 if succeeds else error value > + */ > +int twl4030_madc_conversion(struct twl4030_madc_request *req) > +{ > + const struct twl4030_madc_conversion_method *method; > + u8 ch_msb, ch_lsb; > + int ret; > + > + if (!req || !twl4030_madc) > + return -EINVAL; > + > + mutex_lock(&twl4030_madc->lock); > + if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) { > + ret = -EINVAL; > + goto out; > + } > + /* Do we have a conversion request ongoing */ > + if (twl4030_madc->requests[req->method].active) { > + ret = -EBUSY; > + goto out; > + } > + ch_msb = (req->channels >> 8) & 0xff; > + ch_lsb = req->channels & 0xff; > + method = &twl4030_conversion_methods[req->method]; > + /* Select channels to be converted */ > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1); > + if (ret) { > + dev_err(twl4030_madc->dev, > + "unable to write sel register 0x%X\n", method->sel + 1); > + goto out; > + } > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel); > + if (ret) { > + dev_err(twl4030_madc->dev, > + "unable to write sel register 0x%X\n", method->sel + 1); > + goto out; > + } > + /* Select averaging for all channels if do_avg is set */ > + if (req->do_avg) { > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, > + ch_msb, method->avg + 1); > + if (ret) { > + dev_err(twl4030_madc->dev, > + "unable to write avg register 0x%X\n", > + method->avg + 1); > + goto out; > + } > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, > + ch_lsb, method->avg); > + if (ret) { > + dev_err(twl4030_madc->dev, > + "unable to write sel reg 0x%X\n", > + method->avg); > + goto out; > + } > + } > + if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) { > + ret = twl4030_madc_set_irq(twl4030_madc, req); > + if (ret < 0) > + goto out; > + ret = twl4030_madc_start_conversion(twl4030_madc, req->method); > + if (ret < 0) > + goto out; > + twl4030_madc->requests[req->method].active = 1; > + ret = 0; > + goto out; > + } > + /* With RT method we should not be here anymore */ > + if (req->method == TWL4030_MADC_RT) { > + ret = -EINVAL; > + goto out; > + } > + ret = twl4030_madc_start_conversion(twl4030_madc, req->method); > + if (ret < 0) > + goto out; > + twl4030_madc->requests[req->method].active = 1; > + /* Wait until conversion is ready (ctrl register returns EOC) */ > + ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl); > + if (ret) { > + twl4030_madc->requests[req->method].active = 0; > + goto out; > + } > + ret = twl4030_madc_read_channels(twl4030_madc, method->rbase, > + req->channels, req->rbuf, req->raw); > + twl4030_madc->requests[req->method].active = 0; > + > +out: > + mutex_unlock(&twl4030_madc->lock); > + > + return ret; > +} > +EXPORT_SYMBOL_GPL(twl4030_madc_conversion); > + > +int twl4030_get_madc_conversion(int channel_no) > +{ > + struct twl4030_madc_request req; > + int temp = 0; > + int ret; > + > + req.channels = (1 << channel_no); > + req.method = TWL4030_MADC_SW2; > + req.active = 0; > + req.func_cb = NULL; > + ret = twl4030_madc_conversion(&req); > + if (ret < 0) > + return ret; > + if (req.rbuf[channel_no] > 0) > + temp = req.rbuf[channel_no]; > + > + return temp; > +} > +EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion); > + > +/* > + * Function to enable or disable bias current for > + * main battery type reading or temperature sensing > + * @madc - pointer to twl4030_madc_data struct > + * @chan - can be one of the two values > + * TWL4030_BCI_ITHEN - Enables bias current for main battery type reading > + * TWL4030_BCI_TYPEN - Enables bias current for main battery temperature > + * sensing > + * @on - enable or disable chan. > + */ > +static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc, > + int chan, int on) > +{ > + int ret; > + int regmask; > + u8 regval; > + > + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, > + ®val, TWL4030_BCI_BCICTL1); > + if (ret) { > + dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X", > + TWL4030_BCI_BCICTL1); > + return ret; > + } > + > + regmask = (chan == 0) ? TWL4030_BCI_TYPEN : TWL4030_BCI_ITHEN; > + if (on) > + regval |= regmask; > + else > + regval &= ~regmask; > + > + ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, > + regval, TWL4030_BCI_BCICTL1); > + if (ret) { > + dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n", > + TWL4030_BCI_BCICTL1); > + return ret; > + } > + > + return 0; > +} > + > +/* > + * Function that sets MADC software power on bit to enable MADC > + * @madc - pointer to twl4030_madc_data struct > + * @on - Enable or disable MADC software powen on bit. power > + * returns error if i2c read/write fails else 0 > + */ > +static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) > +{ > + u8 regval; > + int ret; > + > + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, > + ®val, TWL4030_MADC_CTRL1); > + if (ret) { > + dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n", > + TWL4030_MADC_CTRL1); > + return ret; > + } > + if (on) > + regval |= TWL4030_MADC_MADCON; > + else > + regval &= ~TWL4030_MADC_MADCON; > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1); > + if (ret) { > + dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n", > + TWL4030_MADC_CTRL1); > + return ret; > + } > + > + return 0; > +} > + > +/* > + * Initialize MADC and request for threaded irq > + */ > +static int twl4030_madc_probe(struct platform_device *pdev) > +{ > + struct twl4030_madc_data *madc; > + struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev); > + struct device_node *np = pdev->dev.of_node; > + int irq, ret; > + u8 regval; > + struct iio_dev *iio_dev = NULL; > + > + if (!pdata && !np) { > + dev_err(&pdev->dev, "platform_data not available\n"); > + return -EINVAL; > + } > + > + iio_dev = devm_iio_device_alloc(&pdev->dev, > + sizeof(struct twl4030_madc_data)); > + if (!iio_dev) { > + dev_err(&pdev->dev, "failed allocating iio device\n"); > + return -ENOMEM; > + } > + > + madc = iio_priv(iio_dev); > + madc->dev = &pdev->dev; > + > + iio_dev->name = dev_name(&pdev->dev); > + iio_dev->dev.parent = &pdev->dev; > + iio_dev->dev.of_node = pdev->dev.of_node; > + iio_dev->info = &twl4030_madc_iio_info; > + iio_dev->modes = INDIO_DIRECT_MODE; > + iio_dev->channels = twl4030_madc_iio_channels; > + iio_dev->num_channels = 16; ARRAY_SIZE(twl4030_madc_iio_channels) > + > + /* > + * Phoenix provides 2 interrupt lines. The first one is connected to > + * the OMAP. The other one can be connected to the other processor such > + * as modem. Hence two separate ISR and IMR registers. > + */ > + if (pdata) > + madc->use_second_irq = pdata->irq_line != 1; > + else > + madc->use_second_irq = of_property_read_bool(np, > + "ti,system-uses-second-madc-irq"); > + > + madc->imr = (madc->use_second_irq == 1) ? > + TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2; > + madc->isr = (madc->use_second_irq == 1) ? > + TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2; > + > + ret = twl4030_madc_set_power(madc, 1); > + if (ret < 0) > + return ret; > + ret = twl4030_madc_set_current_generator(madc, 0, 1); > + if (ret < 0) > + goto err_current_generator; > + > + ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, > + ®val, TWL4030_BCI_BCICTL1); > + if (ret) { > + dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n", > + TWL4030_BCI_BCICTL1); > + goto err_i2c; > + } > + regval |= TWL4030_BCI_MESBAT; > + ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, > + regval, TWL4030_BCI_BCICTL1); > + if (ret) { > + dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n", > + TWL4030_BCI_BCICTL1); > + goto err_i2c; > + } > + > + /* Check that MADC clock is on */ > + ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, ®val, TWL4030_REG_GPBR1); > + if (ret) { > + dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n", > + TWL4030_REG_GPBR1); > + goto err_i2c; > + } > + > + /* If MADC clk is not on, turn it on */ > + if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) { > + dev_info(&pdev->dev, "clk disabled, enabling\n"); > + regval |= TWL4030_GPBR1_MADC_HFCLK_EN; > + ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval, > + TWL4030_REG_GPBR1); > + if (ret) { > + dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n", > + TWL4030_REG_GPBR1); > + goto err_i2c; > + } > + } > + > + platform_set_drvdata(pdev, iio_dev); > + mutex_init(&madc->lock); > + > + irq = platform_get_irq(pdev, 0); > + ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, > + twl4030_madc_threaded_irq_handler, > + IRQF_TRIGGER_RISING, "twl4030_madc", madc); > + if (ret) { > + dev_dbg(&pdev->dev, "could not request irq\n"); > + goto err_i2c; > + } > + twl4030_madc = madc; > + > + ret = iio_device_register(iio_dev); > + if (ret) { > + dev_dbg(&pdev->dev, "could not register iio device\n"); > + goto err_i2c; > + } > + > + return 0; > + > +err_i2c: > + twl4030_madc_set_current_generator(madc, 0, 0); > +err_current_generator: > + twl4030_madc_set_power(madc, 0); > + return ret; > +} > + > +static int twl4030_madc_remove(struct platform_device *pdev) > +{ > + struct iio_dev *iio_dev = platform_get_drvdata(pdev); > + struct twl4030_madc_data *madc = iio_priv(iio_dev); > + > + twl4030_madc_set_current_generator(madc, 0, 0); > + twl4030_madc_set_power(madc, 0); > + > + iio_device_unregister(iio_dev); > + > + return 0; > +} > + > +#ifdef CONFIG_OF > +static const struct of_device_id twl_madc_of_match[] = { > + {.compatible = "ti,twl4030-madc", }, > + { }, > +}; > +MODULE_DEVICE_TABLE(of, twl_madc_of_match); > +#endif > + > +static struct platform_driver twl4030_madc_driver = { > + .probe = twl4030_madc_probe, > + .remove = twl4030_madc_remove, > + .driver = { > + .name = "twl4030_madc", > + .owner = THIS_MODULE, > + .of_match_table = of_match_ptr(twl_madc_of_match), > + }, > +}; > + > +module_platform_driver(twl4030_madc_driver); > + > +MODULE_DESCRIPTION("TWL4030 ADC driver"); > +MODULE_LICENSE("GPL"); > +MODULE_AUTHOR("J Keerthy"); > +MODULE_ALIAS("platform:twl4030_madc"); > diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig > index 49bb445..23a8a51 100644 > --- a/drivers/mfd/Kconfig > +++ b/drivers/mfd/Kconfig > @@ -935,16 +935,6 @@ config TWL4030_CORE > high speed USB OTG transceiver, an audio codec (on most > versions) and many other features. > > -config TWL4030_MADC > - tristate "TI TWL4030 MADC" > - depends on TWL4030_CORE > - help > - This driver provides support for triton TWL4030-MADC. The > - driver supports both RT and SW conversion methods. > - > - This driver can be built as a module. If so it will be > - named twl4030-madc > - > config TWL4030_POWER > bool "TI TWL4030 power resources" > depends on TWL4030_CORE && ARM > diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile > index 5aea5ef..c8eb0bc 100644 > --- a/drivers/mfd/Makefile > +++ b/drivers/mfd/Makefile > @@ -71,7 +71,6 @@ obj-$(CONFIG_MFD_TPS80031) += tps80031.o > obj-$(CONFIG_MENELAUS) += menelaus.o > > obj-$(CONFIG_TWL4030_CORE) += twl-core.o twl4030-irq.o twl6030-irq.o > -obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o > obj-$(CONFIG_TWL4030_POWER) += twl4030-power.o > obj-$(CONFIG_MFD_TWL4030_AUDIO) += twl4030-audio.o > obj-$(CONFIG_TWL6040_CORE) += twl6040.o > diff --git a/drivers/mfd/twl4030-madc.c b/drivers/mfd/twl4030-madc.c > deleted file mode 100644 > index 0bc79a7..0000000 > --- a/drivers/mfd/twl4030-madc.c > +++ /dev/null > @@ -1,914 +0,0 @@ > -/* > - * > - * TWL4030 MADC module driver-This driver monitors the real time > - * conversion of analog signals like battery temperature, > - * battery type, battery level etc. > - * > - * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ > - * J Keerthy <j-keerthy@ti.com> > - * > - * Based on twl4030-madc.c > - * Copyright (C) 2008 Nokia Corporation > - * Mikko Ylinen <mikko.k.ylinen@nokia.com> > - * > - * Amit Kucheria <amit.kucheria@canonical.com> > - * > - * This program is free software; you can redistribute it and/or > - * modify it under the terms of the GNU General Public License > - * version 2 as published by the Free Software Foundation. > - * > - * This program is distributed in the hope that it will be useful, but > - * WITHOUT ANY WARRANTY; without even the implied warranty of > - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > - * General Public License for more details. > - * > - * You should have received a copy of the GNU General Public License > - * along with this program; if not, write to the Free Software > - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA > - * 02110-1301 USA > - * > - */ > - > -#include <linux/init.h> > -#include <linux/device.h> > -#include <linux/interrupt.h> > -#include <linux/kernel.h> > -#include <linux/delay.h> > -#include <linux/platform_device.h> > -#include <linux/slab.h> > -#include <linux/i2c/twl.h> > -#include <linux/i2c/twl4030-madc.h> > -#include <linux/module.h> > -#include <linux/stddef.h> > -#include <linux/mutex.h> > -#include <linux/bitops.h> > -#include <linux/jiffies.h> > -#include <linux/types.h> > -#include <linux/gfp.h> > -#include <linux/err.h> > - > -#include <linux/iio/iio.h> > - > -/* > - * struct twl4030_madc_data - a container for madc info > - * @dev - pointer to device structure for madc > - * @lock - mutex protecting this data structure > - * @requests - Array of request struct corresponding to SW1, SW2 and RT > - * @imr - Interrupt mask register of MADC > - * @isr - Interrupt status register of MADC > - */ > -struct twl4030_madc_data { > - struct device *dev; > - struct mutex lock; /* mutex protecting this data structure */ > - struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS]; > - bool use_second_irq; > - u8 imr; > - u8 isr; > -}; > - > -static int twl4030_madc_read(struct iio_dev *iio_dev, > - const struct iio_chan_spec *chan, > - int *val, int *val2, long mask) > -{ > - struct twl4030_madc_data *madc = iio_priv(iio_dev); > - struct twl4030_madc_request req; > - int channel = chan->channel; > - int ret; > - > - req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1; > - > - req.channels = BIT(channel); > - req.active = 0; > - req.func_cb = NULL; > - req.raw = !(mask & IIO_CHAN_INFO_PROCESSED); > - req.do_avg = !!(mask & IIO_CHAN_INFO_AVERAGE_RAW); > - > - ret = twl4030_madc_conversion(&req); > - if (ret < 0) > - return ret; > - > - *val = req.rbuf[channel]; > - > - return IIO_VAL_INT; > -} > - > -static const struct iio_info twl4030_madc_iio_info = { > - .read_raw = &twl4030_madc_read, > - .driver_module = THIS_MODULE, > -}; > - > -#define TWL4030_ADC_CHANNEL(_channel, _type, _name, _mask) { \ > - .type = _type, \ > - .channel = _channel, \ > - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ > - BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \ > - _mask, \ > - .datasheet_name = _name, \ > - .indexed = 1, \ > -} > - > -static const struct iio_chan_spec twl4030_madc_iio_channels[] = { > - TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", 0), > - TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1", > - BIT(IIO_CHAN_INFO_PROCESSED)), > - TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", 0), > - TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", 0), > - TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", 0), > - TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", 0), > - TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", 0), > - TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", 0), > - TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", 0), > - TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", 0), > - TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10", > - BIT(IIO_CHAN_INFO_PROCESSED)), > - TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", 0), > - TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", 0), > - TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", 0), > - TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", 0), > - TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", 0), > -}; > - > -static struct twl4030_madc_data *twl4030_madc; > - > -struct twl4030_prescale_divider_ratios { > - s16 numerator; > - s16 denominator; > -}; > - > -static const struct twl4030_prescale_divider_ratios > -twl4030_divider_ratios[16] = { > - {1, 1}, /* CHANNEL 0 No Prescaler */ > - {1, 1}, /* CHANNEL 1 No Prescaler */ > - {6, 10}, /* CHANNEL 2 */ > - {6, 10}, /* CHANNEL 3 */ > - {6, 10}, /* CHANNEL 4 */ > - {6, 10}, /* CHANNEL 5 */ > - {6, 10}, /* CHANNEL 6 */ > - {6, 10}, /* CHANNEL 7 */ > - {3, 14}, /* CHANNEL 8 */ > - {1, 3}, /* CHANNEL 9 */ > - {1, 1}, /* CHANNEL 10 No Prescaler */ > - {15, 100}, /* CHANNEL 11 */ > - {1, 4}, /* CHANNEL 12 */ > - {1, 1}, /* CHANNEL 13 Reserved channels */ > - {1, 1}, /* CHANNEL 14 Reseved channels */ > - {5, 11}, /* CHANNEL 15 */ > -}; > - > - > -/* > - * Conversion table from -3 to 55 degree Celcius > - */ > -static int therm_tbl[] = { > -30800, 29500, 28300, 27100, > -26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700, 17900, > -17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100, 12600, 12100, > -11600, 11200, 10800, 10400, 10000, 9630, 9280, 8950, 8620, 8310, > -8020, 7730, 7460, 7200, 6950, 6710, 6470, 6250, 6040, 5830, > -5640, 5450, 5260, 5090, 4920, 4760, 4600, 4450, 4310, 4170, > -4040, 3910, 3790, 3670, 3550 > -}; > - > -/* > - * Structure containing the registers > - * of different conversion methods supported by MADC. > - * Hardware or RT real time conversion request initiated by external host > - * processor for RT Signal conversions. > - * External host processors can also request for non RT conversions > - * SW1 and SW2 software conversions also called asynchronous or GPC request. > - */ > -static > -const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = { > - [TWL4030_MADC_RT] = { > - .sel = TWL4030_MADC_RTSELECT_LSB, > - .avg = TWL4030_MADC_RTAVERAGE_LSB, > - .rbase = TWL4030_MADC_RTCH0_LSB, > - }, > - [TWL4030_MADC_SW1] = { > - .sel = TWL4030_MADC_SW1SELECT_LSB, > - .avg = TWL4030_MADC_SW1AVERAGE_LSB, > - .rbase = TWL4030_MADC_GPCH0_LSB, > - .ctrl = TWL4030_MADC_CTRL_SW1, > - }, > - [TWL4030_MADC_SW2] = { > - .sel = TWL4030_MADC_SW2SELECT_LSB, > - .avg = TWL4030_MADC_SW2AVERAGE_LSB, > - .rbase = TWL4030_MADC_GPCH0_LSB, > - .ctrl = TWL4030_MADC_CTRL_SW2, > - }, > -}; > - > -/* > - * Function to read a particular channel value. > - * @madc - pointer to struct twl4030_madc_data > - * @reg - lsb of ADC Channel > - * If the i2c read fails it returns an error else returns 0. > - */ > -static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg) > -{ > - u8 msb, lsb; > - int ret; > - /* > - * For each ADC channel, we have MSB and LSB register pair. MSB address > - * is always LSB address+1. reg parameter is the address of LSB register > - */ > - ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1); > - if (ret) { > - dev_err(madc->dev, "unable to read MSB register 0x%X\n", > - reg + 1); > - return ret; > - } > - ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg); > - if (ret) { > - dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg); > - return ret; > - } > - > - return (int)(((msb << 8) | lsb) >> 6); > -} > - > -/* > - * Return battery temperature > - * Or < 0 on failure. > - */ > -static int twl4030battery_temperature(int raw_volt) > -{ > - u8 val; > - int temp, curr, volt, res, ret; > - > - volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R; > - /* Getting and calculating the supply current in micro ampers */ > - ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, > - REG_BCICTL2); > - if (ret < 0) > - return ret; > - > - curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10; > - /* Getting and calculating the thermistor resistance in ohms */ > - res = volt * 1000 / curr; > - /* calculating temperature */ > - for (temp = 58; temp >= 0; temp--) { > - int actual = therm_tbl[temp]; > - if ((actual - res) >= 0) > - break; > - } > - > - return temp + 1; > -} > - > -static int twl4030battery_current(int raw_volt) > -{ > - int ret; > - u8 val; > - > - ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, > - TWL4030_BCI_BCICTL1); > - if (ret) > - return ret; > - if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */ > - return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1; > - else /* slope of 0.88 mV/mA */ > - return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2; > -} > - > -/* > - * Function to read channel values > - * @madc - pointer to twl4030_madc_data struct > - * @reg_base - Base address of the first channel > - * @Channels - 16 bit bitmap. If the bit is set, channel value is read > - * @buf - The channel values are stored here. if read fails error > - * @raw - Return raw values without conversion > - * value is stored > - * Returns the number of successfully read channels. > - */ > -static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, > - u8 reg_base, unsigned > - long channels, int *buf, > - bool raw) > -{ > - int count = 0, i; > - > - for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) { > - buf[i] = twl4030_madc_channel_raw_read(madc, reg_base + 2 * i); > - if (buf[i] < 0) { > - dev_err(madc->dev, "Unable to read register 0x%X\n", > - reg_base + 2 * i); > - return buf[i]; > - } > - if (raw) { > - count++; > - continue; > - } > - switch (i) { > - case 10: > - buf[i] = twl4030battery_current(buf[i]); > - if (buf[i] < 0) { > - dev_err(madc->dev, "err reading current\n"); > - return buf[i]; > - } else { > - count++; > - buf[i] = buf[i] - 750; > - } > - break; > - case 1: > - buf[i] = twl4030battery_temperature(buf[i]); > - if (buf[i] < 0) { > - dev_err(madc->dev, "err reading temperature\n"); > - return buf[i]; > - } else { > - buf[i] -= 3; > - count++; > - } > - break; > - default: > - count++; > - /* Analog Input (V) = conv_result * step_size / R > - * conv_result = decimal value of 10-bit conversion > - * result > - * step size = 1.5 / (2 ^ 10 -1) > - * R = Prescaler ratio for input channels. > - * Result given in mV hence multiplied by 1000. > - */ > - buf[i] = (buf[i] * 3 * 1000 * > - twl4030_divider_ratios[i].denominator) > - / (2 * 1023 * > - twl4030_divider_ratios[i].numerator); > - } > - } > - > - return count; > -} > - > -/* > - * Enables irq. > - * @madc - pointer to twl4030_madc_data struct > - * @id - irq number to be enabled > - * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2 > - * corresponding to RT, SW1, SW2 conversion requests. > - * If the i2c read fails it returns an error else returns 0. > - */ > -static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id) > -{ > - u8 val; > - int ret; > - > - ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr); > - if (ret) { > - dev_err(madc->dev, "unable to read imr register 0x%X\n", > - madc->imr); > - return ret; > - } > - > - val &= ~(1 << id); > - ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); > - if (ret) { > - dev_err(madc->dev, > - "unable to write imr register 0x%X\n", madc->imr); > - return ret; > - } > - > - return 0; > -} > - > -/* > - * Disables irq. > - * @madc - pointer to twl4030_madc_data struct > - * @id - irq number to be disabled > - * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2 > - * corresponding to RT, SW1, SW2 conversion requests. > - * Returns error if i2c read/write fails. > - */ > -static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id) > -{ > - u8 val; > - int ret; > - > - ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr); > - if (ret) { > - dev_err(madc->dev, "unable to read imr register 0x%X\n", > - madc->imr); > - return ret; > - } > - val |= (1 << id); > - ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); > - if (ret) { > - dev_err(madc->dev, > - "unable to write imr register 0x%X\n", madc->imr); > - return ret; > - } > - > - return 0; > -} > - > -static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc) > -{ > - struct twl4030_madc_data *madc = _madc; > - const struct twl4030_madc_conversion_method *method; > - u8 isr_val, imr_val; > - int i, len, ret; > - struct twl4030_madc_request *r; > - > - mutex_lock(&madc->lock); > - ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr); > - if (ret) { > - dev_err(madc->dev, "unable to read isr register 0x%X\n", > - madc->isr); > - goto err_i2c; > - } > - ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr); > - if (ret) { > - dev_err(madc->dev, "unable to read imr register 0x%X\n", > - madc->imr); > - goto err_i2c; > - } > - isr_val &= ~imr_val; > - for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { > - if (!(isr_val & (1 << i))) > - continue; > - ret = twl4030_madc_disable_irq(madc, i); > - if (ret < 0) > - dev_dbg(madc->dev, "Disable interrupt failed%d\n", i); > - madc->requests[i].result_pending = 1; > - } > - for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { > - r = &madc->requests[i]; > - /* No pending results for this method, move to next one */ > - if (!r->result_pending) > - continue; > - method = &twl4030_conversion_methods[r->method]; > - /* Read results */ > - len = twl4030_madc_read_channels(madc, method->rbase, > - r->channels, r->rbuf, r->raw); > - /* Return results to caller */ > - if (r->func_cb != NULL) { > - r->func_cb(len, r->channels, r->rbuf); > - r->func_cb = NULL; > - } > - /* Free request */ > - r->result_pending = 0; > - r->active = 0; > - } > - mutex_unlock(&madc->lock); > - > - return IRQ_HANDLED; > - > -err_i2c: > - /* > - * In case of error check whichever request is active > - * and service the same. > - */ > - for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { > - r = &madc->requests[i]; > - if (r->active == 0) > - continue; > - method = &twl4030_conversion_methods[r->method]; > - /* Read results */ > - len = twl4030_madc_read_channels(madc, method->rbase, > - r->channels, r->rbuf, r->raw); > - /* Return results to caller */ > - if (r->func_cb != NULL) { > - r->func_cb(len, r->channels, r->rbuf); > - r->func_cb = NULL; > - } > - /* Free request */ > - r->result_pending = 0; > - r->active = 0; > - } > - mutex_unlock(&madc->lock); > - > - return IRQ_HANDLED; > -} > - > -static int twl4030_madc_set_irq(struct twl4030_madc_data *madc, > - struct twl4030_madc_request *req) > -{ > - struct twl4030_madc_request *p; > - int ret; > - > - p = &madc->requests[req->method]; > - memcpy(p, req, sizeof(*req)); > - ret = twl4030_madc_enable_irq(madc, req->method); > - if (ret < 0) { > - dev_err(madc->dev, "enable irq failed!!\n"); > - return ret; > - } > - > - return 0; > -} > - > -/* > - * Function which enables the madc conversion > - * by writing to the control register. > - * @madc - pointer to twl4030_madc_data struct > - * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1 > - * corresponding to RT SW1 or SW2 conversion methods. > - * Returns 0 if succeeds else a negative error value > - */ > -static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc, > - int conv_method) > -{ > - const struct twl4030_madc_conversion_method *method; > - int ret = 0; > - > - if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2) > - return -ENOTSUPP; > - > - method = &twl4030_conversion_methods[conv_method]; > - ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START, > - method->ctrl); > - if (ret) { > - dev_err(madc->dev, "unable to write ctrl register 0x%X\n", > - method->ctrl); > - return ret; > - } > - > - return 0; > -} > - > -/* > - * Function that waits for conversion to be ready > - * @madc - pointer to twl4030_madc_data struct > - * @timeout_ms - timeout value in milliseconds > - * @status_reg - ctrl register > - * returns 0 if succeeds else a negative error value > - */ > -static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc, > - unsigned int timeout_ms, > - u8 status_reg) > -{ > - unsigned long timeout; > - int ret; > - > - timeout = jiffies + msecs_to_jiffies(timeout_ms); > - do { > - u8 reg; > - > - ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, ®, status_reg); > - if (ret) { > - dev_err(madc->dev, > - "unable to read status register 0x%X\n", > - status_reg); > - return ret; > - } > - if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW)) > - return 0; > - usleep_range(500, 2000); > - } while (!time_after(jiffies, timeout)); > - dev_err(madc->dev, "conversion timeout!\n"); > - > - return -EAGAIN; > -} > - > -/* > - * An exported function which can be called from other kernel drivers. > - * @req twl4030_madc_request structure > - * req->rbuf will be filled with read values of channels based on the > - * channel index. If a particular channel reading fails there will > - * be a negative error value in the corresponding array element. > - * returns 0 if succeeds else error value > - */ > -int twl4030_madc_conversion(struct twl4030_madc_request *req) > -{ > - const struct twl4030_madc_conversion_method *method; > - u8 ch_msb, ch_lsb; > - int ret; > - > - if (!req || !twl4030_madc) > - return -EINVAL; > - > - mutex_lock(&twl4030_madc->lock); > - if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) { > - ret = -EINVAL; > - goto out; > - } > - /* Do we have a conversion request ongoing */ > - if (twl4030_madc->requests[req->method].active) { > - ret = -EBUSY; > - goto out; > - } > - ch_msb = (req->channels >> 8) & 0xff; > - ch_lsb = req->channels & 0xff; > - method = &twl4030_conversion_methods[req->method]; > - /* Select channels to be converted */ > - ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1); > - if (ret) { > - dev_err(twl4030_madc->dev, > - "unable to write sel register 0x%X\n", method->sel + 1); > - goto out; > - } > - ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel); > - if (ret) { > - dev_err(twl4030_madc->dev, > - "unable to write sel register 0x%X\n", method->sel + 1); > - goto out; > - } > - /* Select averaging for all channels if do_avg is set */ > - if (req->do_avg) { > - ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, > - ch_msb, method->avg + 1); > - if (ret) { > - dev_err(twl4030_madc->dev, > - "unable to write avg register 0x%X\n", > - method->avg + 1); > - goto out; > - } > - ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, > - ch_lsb, method->avg); > - if (ret) { > - dev_err(twl4030_madc->dev, > - "unable to write sel reg 0x%X\n", > - method->avg); > - goto out; > - } > - } > - if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) { > - ret = twl4030_madc_set_irq(twl4030_madc, req); > - if (ret < 0) > - goto out; > - ret = twl4030_madc_start_conversion(twl4030_madc, req->method); > - if (ret < 0) > - goto out; > - twl4030_madc->requests[req->method].active = 1; > - ret = 0; > - goto out; > - } > - /* With RT method we should not be here anymore */ > - if (req->method == TWL4030_MADC_RT) { > - ret = -EINVAL; > - goto out; > - } > - ret = twl4030_madc_start_conversion(twl4030_madc, req->method); > - if (ret < 0) > - goto out; > - twl4030_madc->requests[req->method].active = 1; > - /* Wait until conversion is ready (ctrl register returns EOC) */ > - ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl); > - if (ret) { > - twl4030_madc->requests[req->method].active = 0; > - goto out; > - } > - ret = twl4030_madc_read_channels(twl4030_madc, method->rbase, > - req->channels, req->rbuf, req->raw); > - twl4030_madc->requests[req->method].active = 0; > - > -out: > - mutex_unlock(&twl4030_madc->lock); > - > - return ret; > -} > -EXPORT_SYMBOL_GPL(twl4030_madc_conversion); > - > -int twl4030_get_madc_conversion(int channel_no) > -{ > - struct twl4030_madc_request req; > - int temp = 0; > - int ret; > - > - req.channels = (1 << channel_no); > - req.method = TWL4030_MADC_SW2; > - req.active = 0; > - req.func_cb = NULL; > - ret = twl4030_madc_conversion(&req); > - if (ret < 0) > - return ret; > - if (req.rbuf[channel_no] > 0) > - temp = req.rbuf[channel_no]; > - > - return temp; > -} > -EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion); > - > -/* > - * Function to enable or disable bias current for > - * main battery type reading or temperature sensing > - * @madc - pointer to twl4030_madc_data struct > - * @chan - can be one of the two values > - * TWL4030_BCI_ITHEN - Enables bias current for main battery type reading > - * TWL4030_BCI_TYPEN - Enables bias current for main battery temperature > - * sensing > - * @on - enable or disable chan. > - */ > -static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc, > - int chan, int on) > -{ > - int ret; > - int regmask; > - u8 regval; > - > - ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, > - ®val, TWL4030_BCI_BCICTL1); > - if (ret) { > - dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X", > - TWL4030_BCI_BCICTL1); > - return ret; > - } > - > - regmask = (chan == 0) ? TWL4030_BCI_TYPEN : TWL4030_BCI_ITHEN; > - if (on) > - regval |= regmask; > - else > - regval &= ~regmask; > - > - ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, > - regval, TWL4030_BCI_BCICTL1); > - if (ret) { > - dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n", > - TWL4030_BCI_BCICTL1); > - return ret; > - } > - > - return 0; > -} > - > -/* > - * Function that sets MADC software power on bit to enable MADC > - * @madc - pointer to twl4030_madc_data struct > - * @on - Enable or disable MADC software powen on bit. > - * returns error if i2c read/write fails else 0 > - */ > -static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) > -{ > - u8 regval; > - int ret; > - > - ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, > - ®val, TWL4030_MADC_CTRL1); > - if (ret) { > - dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n", > - TWL4030_MADC_CTRL1); > - return ret; > - } > - if (on) > - regval |= TWL4030_MADC_MADCON; > - else > - regval &= ~TWL4030_MADC_MADCON; > - ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1); > - if (ret) { > - dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n", > - TWL4030_MADC_CTRL1); > - return ret; > - } > - > - return 0; > -} > - > -/* > - * Initialize MADC and request for threaded irq > - */ > -static int twl4030_madc_probe(struct platform_device *pdev) > -{ > - struct twl4030_madc_data *madc; > - struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev); > - struct device_node *np = pdev->dev.of_node; > - int irq, ret; > - u8 regval; > - struct iio_dev *iio_dev = NULL; > - > - if (!pdata && !np) { > - dev_err(&pdev->dev, "platform_data not available\n"); > - return -EINVAL; > - } > - > - iio_dev = devm_iio_device_alloc(&pdev->dev, > - sizeof(struct twl4030_madc_data)); > - if (!iio_dev) { > - dev_err(&pdev->dev, "failed allocating iio device\n"); > - return -ENOMEM; > - } > - > - madc = iio_priv(iio_dev); > - madc->dev = &pdev->dev; > - > - iio_dev->name = dev_name(&pdev->dev); > - iio_dev->dev.parent = &pdev->dev; > - iio_dev->dev.of_node = pdev->dev.of_node; > - iio_dev->info = &twl4030_madc_iio_info; > - iio_dev->modes = INDIO_DIRECT_MODE; > - iio_dev->channels = twl4030_madc_iio_channels; > - iio_dev->num_channels = 16; > - > - /* > - * Phoenix provides 2 interrupt lines. The first one is connected to > - * the OMAP. The other one can be connected to the other processor such > - * as modem. Hence two separate ISR and IMR registers. > - */ > - if (pdata) > - madc->use_second_irq = pdata->irq_line != 1; > - else > - madc->use_second_irq = of_property_read_bool(np, > - "ti,system-uses-second-madc-irq"); > - > - madc->imr = (madc->use_second_irq == 1) ? > - TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2; > - madc->isr = (madc->use_second_irq == 1) ? > - TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2; > - > - ret = twl4030_madc_set_power(madc, 1); > - if (ret < 0) > - return ret; > - ret = twl4030_madc_set_current_generator(madc, 0, 1); > - if (ret < 0) > - goto err_current_generator; > - > - ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, > - ®val, TWL4030_BCI_BCICTL1); > - if (ret) { > - dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n", > - TWL4030_BCI_BCICTL1); > - goto err_i2c; > - } > - regval |= TWL4030_BCI_MESBAT; > - ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, > - regval, TWL4030_BCI_BCICTL1); > - if (ret) { > - dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n", > - TWL4030_BCI_BCICTL1); > - goto err_i2c; > - } > - > - /* Check that MADC clock is on */ > - ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, ®val, TWL4030_REG_GPBR1); > - if (ret) { > - dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n", > - TWL4030_REG_GPBR1); > - goto err_i2c; > - } > - > - /* If MADC clk is not on, turn it on */ > - if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) { > - dev_info(&pdev->dev, "clk disabled, enabling\n"); > - regval |= TWL4030_GPBR1_MADC_HFCLK_EN; > - ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval, > - TWL4030_REG_GPBR1); > - if (ret) { > - dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n", > - TWL4030_REG_GPBR1); > - goto err_i2c; > - } > - } > - > - platform_set_drvdata(pdev, iio_dev); > - mutex_init(&madc->lock); > - > - irq = platform_get_irq(pdev, 0); > - ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, > - twl4030_madc_threaded_irq_handler, > - IRQF_TRIGGER_RISING, "twl4030_madc", madc); > - if (ret) { > - dev_dbg(&pdev->dev, "could not request irq\n"); > - goto err_i2c; > - } > - twl4030_madc = madc; > - > - ret = iio_device_register(iio_dev); > - if (ret) { > - dev_dbg(&pdev->dev, "could not register iio device\n"); > - goto err_i2c; > - } > - > - return 0; > - > -err_i2c: > - twl4030_madc_set_current_generator(madc, 0, 0); > -err_current_generator: > - twl4030_madc_set_power(madc, 0); > - return ret; > -} > - > -static int twl4030_madc_remove(struct platform_device *pdev) > -{ > - struct iio_dev *iio_dev = platform_get_drvdata(pdev); > - struct twl4030_madc_data *madc = iio_priv(iio_dev); > - > - twl4030_madc_set_current_generator(madc, 0, 0); > - twl4030_madc_set_power(madc, 0); > - > - iio_device_unregister(iio_dev); > - > - return 0; > -} > - > -#ifdef CONFIG_OF > -static const struct of_device_id twl_madc_of_match[] = { > - {.compatible = "ti,twl4030-madc", }, > - { }, > -}; > -MODULE_DEVICE_TABLE(of, twl_madc_of_match); > -#endif > - > -static struct platform_driver twl4030_madc_driver = { > - .probe = twl4030_madc_probe, > - .remove = twl4030_madc_remove, > - .driver = { > - .name = "twl4030_madc", > - .owner = THIS_MODULE, > - .of_match_table = of_match_ptr(twl_madc_of_match), > - }, > -}; > - > -module_platform_driver(twl4030_madc_driver); > - > -MODULE_DESCRIPTION("TWL4030 ADC driver"); > -MODULE_LICENSE("GPL"); > -MODULE_AUTHOR("J Keerthy"); > -MODULE_ALIAS("platform:twl4030_madc"); >
> Update twl4030-madc driver to use managed resources. > > Signed-off-by: Sebastian Reichel <sre@debian.org> > --- > drivers/mfd/twl4030-madc.c | 15 ++++++--------- > 1 file changed, 6 insertions(+), 9 deletions(-) Acked-by: Lee Jones <lee.jones@linaro.org>
> This is a driver for an A/D converter, which belongs into > drivers/iio/adc. > > Signed-off-by: Sebastian Reichel <sre@debian.org> > --- > drivers/iio/adc/Kconfig | 10 + > drivers/iio/adc/Makefile | 1 + > drivers/iio/adc/twl4030-madc.c | 922 +++++++++++++++++++++++++++++++++++++++++ > drivers/mfd/Kconfig | 10 - > drivers/mfd/Makefile | 1 - > drivers/mfd/twl4030-madc.c | 922 ----------------------------------------- > 6 files changed, 933 insertions(+), 933 deletions(-) > create mode 100644 drivers/iio/adc/twl4030-madc.c > delete mode 100644 drivers/mfd/twl4030-madc.c For the MFD changes: Acked-by: Lee Jones <lee.jones@linaro.org>
Hi, This is RFCv3 for converting twl4030-madc to the IIO API and adding DT support. The patchset compiles and has been tested on my Nokia N900. Changes since RFCv2: * Use "--find-renames" for the move from mfd/ to iio/adc/ ;) * Added Acked-by from Lee Jones (MFD subsystem maintainer) * Fixed issue found by Joe Perches - twl4030_madc_request.do_avg should be bool instead of u16 * Fixed issues found by Peter Meerwald - some style issues (e.g. /** instead of /*) - typos - usage of u16 read/write * Tested with converted rx51-battery driver on Nokia N900 TODO: * Remove twl4030-madc-hwmon in favour of iio-hwmon This driver is currently only used by board-omap3beagle.c and board-rx51-peripherals.c. The instanciation in the rx51 boardcode is a bug, since actually only twl4030-madc is needed. Thus board-omap3beagle.c needs to be updated to iio-hwmon and board-rx51-peripherals.c to twl4030-madc. Both tasks are pretty trivial and twl4030-madc-hwmon can be removed afterwards :) * Update twl4030-madc-battery to the IIO API. I guess Marek will do this update in the near future, since he is currently writing DT bindings for the driver. * I updated twl4030-madc-battery already and will send it in a separate patchset. * Once all of the above steps happened the old API can be removed from the driver, which allows some more cleanups. -- Sebastian Sebastian Reichel (7): mfd: twl4030-madc: Use managed resources mfd: twl4030-madc: Add DT support and convert to IIO framework mfd: twl4030-madc: Cleanup driver mfd: twl-core: Add twl_i2c_read/write_u16 mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers Documentation: DT: Document twl4030-madc binding mfd: twl4030-madc: Move driver to drivers/iio/adc .../devicetree/bindings/iio/adc/twl4030-madc.txt | 24 ++ drivers/iio/adc/Kconfig | 10 + drivers/iio/adc/Makefile | 1 + drivers/{mfd => iio/adc}/twl4030-madc.c | 278 +++++++++++++-------- drivers/mfd/Kconfig | 10 - drivers/mfd/Makefile | 1 - include/linux/i2c/twl.h | 12 + include/linux/i2c/twl4030-madc.h | 2 +- 8 files changed, 224 insertions(+), 114 deletions(-) create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt rename drivers/{mfd => iio/adc}/twl4030-madc.c (77%)
> Simplify reading and writing of 16 bit TWL registers in the > driver by using twl_i2c_read_u16 and twl_i2c_write_u16. > > Signed-off-by: Sebastian Reichel <sre@debian.org> > --- > drivers/mfd/twl4030-madc.c | 39 ++++++++------------------------------- > 1 file changed, 8 insertions(+), 31 deletions(-) Acked-by: Lee Jones <lee.jones@linaro.org>
> Add a simple twl_i2c_read/write_u16 wrapper over > the twl_i2c_read/write, which is similar to the > twl_i2c_read/write_u8 wrapper. > > Signed-off-by: Sebastian Reichel <sre@debian.org> > --- > include/linux/i2c/twl.h | 12 ++++++++++++ > 1 file changed, 12 insertions(+) Acked-by: Lee Jones <lee.jones@linaro.org>
> Some style fixes in twl4030-madc driver. > > Reported-by: Jonathan Cameron <jic23@kernel.org> > Signed-off-by: Sebastian Reichel <sre@debian.org> > --- > drivers/mfd/twl4030-madc.c | 108 ++++++++++++++++++--------------------- > include/linux/i2c/twl4030-madc.h | 2 +- > 2 files changed, 51 insertions(+), 59 deletions(-) > > diff --git a/drivers/mfd/twl4030-madc.c b/drivers/mfd/twl4030-madc.c > index 81484ee..c23025b 100644 > --- a/drivers/mfd/twl4030-madc.c > +++ b/drivers/mfd/twl4030-madc.c > /* > - * Return battery temperature > + * Return battery temperature in degree Celsius degrees <snip> > - int count = 0, count_req = 0, i; > - u8 reg; > + int count = 0, i; Ugly, please declare on separate lines. > for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) { > - reg = reg_base + 2 * i; Odd for this to be removed, as you calculate more than once now. > - buf[i] = twl4030_madc_channel_raw_read(madc, reg); > + buf[i] = twl4030_madc_channel_raw_read(madc, reg_base + 2 * i); Better to bracket the math for clarity. <snip> > + dev_err(madc->dev, "Unable to read register 0x%X\n", > + reg_base + 2 * i); Here's the other calculation. > + regmask = (chan == 0) ? TWL4030_BCI_TYPEN : TWL4030_BCI_ITHEN; regmask = chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
> This converts twl4030-madc module to use the Industrial IO ADC > framework and adds device tree support. > > Signed-off-by: Sebastian Reichel <sre@debian.org> > --- > drivers/mfd/twl4030-madc.c | 122 ++++++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 115 insertions(+), 7 deletions(-) > > diff --git a/drivers/mfd/twl4030-madc.c b/drivers/mfd/twl4030-madc.c > index 5458561..81484ee 100644 > --- a/drivers/mfd/twl4030-madc.c > +++ b/drivers/mfd/twl4030-madc.c > @@ -47,6 +47,8 @@ > #include <linux/gfp.h> > #include <linux/err.h> > > +#include <linux/iio/iio.h> > + > /* > * struct twl4030_madc_data - a container for madc info > * @dev - pointer to device structure for madc > @@ -59,10 +61,74 @@ struct twl4030_madc_data { > struct device *dev; > struct mutex lock; /* mutex protecting this data structure */ > struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS]; > + bool use_second_irq; This should go into the same patch that adds the header doc, or visa versa. > int imr; > int isr; > }; > > +static int twl4030_madc_read(struct iio_dev *iio_dev, > + const struct iio_chan_spec *chan, > + int *val, int *val2, long mask) > +{ > + struct twl4030_madc_data *madc = iio_priv(iio_dev); > + struct twl4030_madc_request req; > + int channel = chan->channel; Does this really add anything you save 6 chars. <snip> > /* > * Phoenix provides 2 interrupt lines. The first one is connected to > * the OMAP. The other one can be connected to the other processor such > * as modem. Hence two separate ISR and IMR registers. > */ > - madc->imr = (pdata->irq_line == 1) ? > + if (pdata) > + madc->use_second_irq = pdata->irq_line != 1; Unconventional an unclear. Stick the comparison in the if statement. > + else > + madc->use_second_irq = of_property_read_bool(np, > + "ti,system-uses-second-madc-irq"); > + > + madc->imr = (madc->use_second_irq == 1) ? madc->imr = madc->use_second_irq ? <blah> : <blah>; > TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2; > - madc->isr = (pdata->irq_line == 1) ? > + madc->isr = (madc->use_second_irq == 1) ? Same here. <snip>
Hi Sebastian, On Wed, Feb 26, 2014 at 1:24 AM, Sebastian Reichel <sre@debian.org> wrote: > Hi, > > This is RFCv3 for converting twl4030-madc to the IIO API and > adding DT support. The patchset compiles and has been tested > on my Nokia N900. > > Changes since RFCv2: > * Use "--find-renames" for the move from mfd/ to iio/adc/ ;) > * Added Acked-by from Lee Jones (MFD subsystem maintainer) > * Fixed issue found by Joe Perches > - twl4030_madc_request.do_avg should be bool instead of u16 > * Fixed issues found by Peter Meerwald > - some style issues (e.g. /** instead of /*) > - typos > - usage of u16 read/write > * Tested with converted rx51-battery driver on Nokia N900 > > TODO: > * Remove twl4030-madc-hwmon in favour of iio-hwmon > This driver is currently only used by board-omap3beagle.c > and board-rx51-peripherals.c. The instanciation in the > rx51 boardcode is a bug, since actually only twl4030-madc > is needed. Thus board-omap3beagle.c needs to be updated to > iio-hwmon and board-rx51-peripherals.c to twl4030-madc. > Both tasks are pretty trivial and twl4030-madc-hwmon can > be removed afterwards :) > * Update twl4030-madc-battery to the IIO API. I guess Marek > will do this update in the near future, since he is currently > writing DT bindings for the driver. Yes I'm working on DT for twl4030-madc-battery. I'll convert it also to IIO and post for comments. Thanks for doing that it's really nice work! > * I updated twl4030-madc-battery already and will send it > in a separate patchset. > * Once all of the above steps happened the old API can be > removed from the driver, which allows some more cleanups. > > -- Sebastian > > Sebastian Reichel (7): > mfd: twl4030-madc: Use managed resources > mfd: twl4030-madc: Add DT support and convert to IIO framework > mfd: twl4030-madc: Cleanup driver > mfd: twl-core: Add twl_i2c_read/write_u16 > mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers > Documentation: DT: Document twl4030-madc binding > mfd: twl4030-madc: Move driver to drivers/iio/adc > > .../devicetree/bindings/iio/adc/twl4030-madc.txt | 24 ++ > drivers/iio/adc/Kconfig | 10 + > drivers/iio/adc/Makefile | 1 + > drivers/{mfd => iio/adc}/twl4030-madc.c | 278 +++++++++++++-------- > drivers/mfd/Kconfig | 10 - > drivers/mfd/Makefile | 1 - > include/linux/i2c/twl.h | 12 + > include/linux/i2c/twl4030-madc.h | 2 +- > 8 files changed, 224 insertions(+), 114 deletions(-) > create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt > rename drivers/{mfd => iio/adc}/twl4030-madc.c (77%) > > -- > 1.8.5.3 > BR, marek
Hi, This is RFCv4 for converting twl4030-madc to the IIO API and adding DT support. The patchset compiles and has been tested on my Nokia N900. Changes since RFCv3 [0]: * Added Acked-by from Lee Jones (MFD subsystem maintainer) to twl_i2c_read/write_u16 patches * Fixed style issues found by Lee Jones This readded the temp variable Jonathan Cameron wanted to be removed, but makes the code cleaner - especially once the brackets for math for clarity are added. * Tested with converted rx51-battery driver on Nokia N900, which can be found at [1]. [0] https://lkml.org/lkml/2014/2/25/627 [1] https://lkml.org/lkml/2014/2/25/639 -- Sebastian Sebastian Reichel (7): mfd: twl4030-madc: Use managed resources mfd: twl4030-madc: Add DT support and convert to IIO framework mfd: twl4030-madc: Cleanup driver mfd: twl-core: Add twl_i2c_read/write_u16 mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers Documentation: DT: Document twl4030-madc binding mfd: twl4030-madc: Move driver to drivers/iio/adc .../devicetree/bindings/iio/adc/twl4030-madc.txt | 24 ++ drivers/iio/adc/Kconfig | 10 + drivers/iio/adc/Makefile | 1 + drivers/{mfd => iio/adc}/twl4030-madc.c | 281 +++++++++++++-------- drivers/mfd/Kconfig | 10 - drivers/mfd/Makefile | 1 - include/linux/i2c/twl.h | 12 + include/linux/i2c/twl4030-madc.h | 2 +- 8 files changed, 227 insertions(+), 114 deletions(-) create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt rename drivers/{mfd => iio/adc}/twl4030-madc.c (77%)
> Some style fixes in twl4030-madc driver. > > Reported-by: Jonathan Cameron <jic23@kernel.org> > Signed-off-by: Sebastian Reichel <sre@debian.org> > --- > drivers/mfd/twl4030-madc.c | 106 ++++++++++++++++++--------------------- > include/linux/i2c/twl4030-madc.h | 2 +- > 2 files changed, 51 insertions(+), 57 deletions(-) > > diff --git a/drivers/mfd/twl4030-madc.c b/drivers/mfd/twl4030-madc.c Acked-by: Lee Jones <lee.jones@linaro.org>
On Wed, 26 Feb 2014, Sebastian Reichel wrote: > This converts twl4030-madc module to use the Industrial IO ADC > framework and adds device tree support. > > Signed-off-by: Sebastian Reichel <sre@debian.org> > --- > drivers/mfd/twl4030-madc.c | 127 +++++++++++++++++++++++++++++++++++++++++---- > 1 file changed, 118 insertions(+), 9 deletions(-) > > diff --git a/drivers/mfd/twl4030-madc.c b/drivers/mfd/twl4030-madc.c > index 5458561..37cb3ad 100644 > --- a/drivers/mfd/twl4030-madc.c > +++ b/drivers/mfd/twl4030-madc.c <snip> > - if (!pdata) { > + if (!pdata && !np) { > dev_err(&pdev->dev, "platform_data not available\n"); and Device Tree node ... <snip> > + iio_dev->channels = twl4030_madc_iio_channels; > + iio_dev->num_channels = 16; ARRAY_SIZE() <snip>
On 26/02/14 20:03, Sebastian Reichel wrote: > This converts twl4030-madc module to use the Industrial IO ADC > framework and adds device tree support. > > Signed-off-by: Sebastian Reichel <sre@debian.org> Mostly fine. I've replicated a few things Lee spotted + one logic bug I think in handling the platform_data, device tree interaction. > --- > drivers/mfd/twl4030-madc.c | 127 +++++++++++++++++++++++++++++++++++++++++---- > 1 file changed, 118 insertions(+), 9 deletions(-) > > diff --git a/drivers/mfd/twl4030-madc.c b/drivers/mfd/twl4030-madc.c > index 5458561..37cb3ad 100644 > --- a/drivers/mfd/twl4030-madc.c > +++ b/drivers/mfd/twl4030-madc.c > @@ -47,11 +47,14 @@ > #include <linux/gfp.h> > #include <linux/err.h> > > +#include <linux/iio/iio.h> > + > /* > * struct twl4030_madc_data - a container for madc info > * @dev - pointer to device structure for madc > * @lock - mutex protecting this data structure > * @requests - Array of request struct corresponding to SW1, SW2 and RT > + * @use_second_irq - IRQ selection (main or co-processor) > * @imr - Interrupt mask register of MADC > * @isr - Interrupt status register of MADC > */ > @@ -59,10 +62,73 @@ struct twl4030_madc_data { > struct device *dev; > struct mutex lock; /* mutex protecting this data structure */ > struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS]; > + bool use_second_irq; > int imr; > int isr; > }; > > +static int twl4030_madc_read(struct iio_dev *iio_dev, > + const struct iio_chan_spec *chan, > + int *val, int *val2, long mask) > +{ > + struct twl4030_madc_data *madc = iio_priv(iio_dev); > + struct twl4030_madc_request req; > + int ret; > + > + req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1; > + > + req.channels = BIT(chan->channel); > + req.active = false; > + req.func_cb = NULL; > + req.type = TWL4030_MADC_WAIT; > + req.raw = !(mask == IIO_CHAN_INFO_PROCESSED); > + req.do_avg = (mask == IIO_CHAN_INFO_AVERAGE_RAW); > + > + ret = twl4030_madc_conversion(&req); > + if (ret < 0) > + return ret; > + > + *val = req.rbuf[chan->channel]; > + > + return IIO_VAL_INT; > +} > + > +static const struct iio_info twl4030_madc_iio_info = { > + .read_raw = &twl4030_madc_read, > + .driver_module = THIS_MODULE, > +}; > + > +#define TWL4030_ADC_CHANNEL(_channel, _type, _name, _mask) { \ > + .type = _type, \ > + .channel = _channel, \ > + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ > + BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \ > + _mask, \ > + .datasheet_name = _name, \ > + .indexed = 1, \ > +} A small addition to the ABI docs to include the average case would be good. Obviously it should be there already but it isn't as far as I can see. > + > +static const struct iio_chan_spec twl4030_madc_iio_channels[] = { > + TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", 0), > + TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1", > + BIT(IIO_CHAN_INFO_PROCESSED)), > + TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", 0), > + TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", 0), > + TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", 0), > + TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", 0), > + TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", 0), > + TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", 0), > + TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", 0), > + TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", 0), > + TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10", > + BIT(IIO_CHAN_INFO_PROCESSED)), > + TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", 0), > + TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", 0), > + TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", 0), > + TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", 0), > + TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", 0), > +}; > + > static struct twl4030_madc_data *twl4030_madc; > > struct twl4030_prescale_divider_ratios { > @@ -702,28 +768,51 @@ static int twl4030_madc_probe(struct platform_device *pdev) > { > struct twl4030_madc_data *madc; > struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev); > + struct device_node *np = pdev->dev.of_node; > int irq, ret; > u8 regval; > + struct iio_dev *iio_dev = NULL; > > - if (!pdata) { > + if (!pdata && !np) { > dev_err(&pdev->dev, "platform_data not available\n"); > return -EINVAL; > } > - madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL); > - if (!madc) > + > + iio_dev = devm_iio_device_alloc(&pdev->dev, > + sizeof(struct twl4030_madc_data)); sizeof(*madc)) is shorter and slightly prefered. > + if (!iio_dev) { > + dev_err(&pdev->dev, "failed allocating iio device\n"); > return -ENOMEM; > + } > > + madc = iio_priv(iio_dev); > madc->dev = &pdev->dev; > + madc->use_second_irq = false; > + > + iio_dev->name = dev_name(&pdev->dev); > + iio_dev->dev.parent = &pdev->dev; > + iio_dev->dev.of_node = pdev->dev.of_node; > + iio_dev->info = &twl4030_madc_iio_info; > + iio_dev->modes = INDIO_DIRECT_MODE; > + iio_dev->channels = twl4030_madc_iio_channels; > + iio_dev->num_channels = 16; Preferred to use ARRAY_SIZE(twl4030_madc_iio_channels); That's obviously right, whereas with an explicity number I had to go check :( > > /* > * Phoenix provides 2 interrupt lines. The first one is connected to > * the OMAP. The other one can be connected to the other processor such > * as modem. Hence two separate ISR and IMR registers. > */ > - madc->imr = (pdata->irq_line == 1) ? > - TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2; > - madc->isr = (pdata->irq_line == 1) ? > - TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2; > + if (pdata && pdata->irq_line != 1) > + madc->use_second_irq = true; This logic looks wrong. If pdata is true, but pdata->irq_line = 0 it will read the of data which might not be there. > + else > + madc->use_second_irq = of_property_read_bool(np, > + "ti,system-uses-second-madc-irq"); > + > + madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 : > + TWL4030_MADC_IMR1; > + madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 : > + TWL4030_MADC_ISR1; > + > ret = twl4030_madc_set_power(madc, 1); > if (ret < 0) > return ret; > @@ -768,7 +857,7 @@ static int twl4030_madc_probe(struct platform_device *pdev) > } > } > > - platform_set_drvdata(pdev, madc); > + platform_set_drvdata(pdev, iio_dev); > mutex_init(&madc->lock); > > irq = platform_get_irq(pdev, 0); > @@ -780,7 +869,15 @@ static int twl4030_madc_probe(struct platform_device *pdev) > goto err_i2c; > } > twl4030_madc = madc; > + > + ret = iio_device_register(iio_dev); > + if (ret) { > + dev_dbg(&pdev->dev, "could not register iio device\n"); > + goto err_i2c; > + } > + > return 0; > + > err_i2c: > twl4030_madc_set_current_generator(madc, 0, 0); > err_current_generator: > @@ -790,20 +887,32 @@ err_current_generator: > > static int twl4030_madc_remove(struct platform_device *pdev) > { > - struct twl4030_madc_data *madc = platform_get_drvdata(pdev); > + struct iio_dev *iio_dev = platform_get_drvdata(pdev); > + struct twl4030_madc_data *madc = iio_priv(iio_dev); > > twl4030_madc_set_current_generator(madc, 0, 0); > twl4030_madc_set_power(madc, 0); > > + iio_device_unregister(iio_dev); > + > return 0; > } > > +#ifdef CONFIG_OF > +static const struct of_device_id twl_madc_of_match[] = { > + {.compatible = "ti,twl4030-madc", }, > + { }, > +}; > +MODULE_DEVICE_TABLE(of, twl_madc_of_match); > +#endif > + > static struct platform_driver twl4030_madc_driver = { > .probe = twl4030_madc_probe, > .remove = twl4030_madc_remove, > .driver = { > .name = "twl4030_madc", > .owner = THIS_MODULE, > + .of_match_table = of_match_ptr(twl_madc_of_match), > }, > }; > > -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On 26/02/14 20:03, Sebastian Reichel wrote: > Some style fixes in twl4030-madc driver. > > Reported-by: Jonathan Cameron <jic23@kernel.org> > Signed-off-by: Sebastian Reichel <sre@debian.org> Good stuff, but would be nice to hammer the comments into proper kernel-doc style rather than going part of the way. Also, the ARRAY_SIZE bit that both Lee and I picked up on in the previous patch snuck into this one instead. Please move it back one patch. > --- > drivers/mfd/twl4030-madc.c | 106 ++++++++++++++++++--------------------- > include/linux/i2c/twl4030-madc.h | 2 +- > 2 files changed, 51 insertions(+), 57 deletions(-) > > diff --git a/drivers/mfd/twl4030-madc.c b/drivers/mfd/twl4030-madc.c > index 37cb3ad..c90013e 100644 > --- a/drivers/mfd/twl4030-madc.c > +++ b/drivers/mfd/twl4030-madc.c > @@ -49,7 +49,7 @@ > > #include <linux/iio/iio.h> > > -/* > +/** > * struct twl4030_madc_data - a container for madc info @dev: for kerneldoc. > * @dev - pointer to device structure for madc > * @lock - mutex protecting this data structure > @@ -63,8 +63,8 @@ struct twl4030_madc_data { > struct mutex lock; /* mutex protecting this data structure */ > struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS]; > bool use_second_irq; > - int imr; > - int isr; > + u8 imr; > + u8 isr; > }; > > static int twl4030_madc_read(struct iio_dev *iio_dev, > @@ -157,17 +157,16 @@ twl4030_divider_ratios[16] = { > }; > > > -/* > - * Conversion table from -3 to 55 degree Celcius > - */ > -static int therm_tbl[] = { > -30800, 29500, 28300, 27100, > -26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700, 17900, > -17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100, 12600, 12100, > -11600, 11200, 10800, 10400, 10000, 9630, 9280, 8950, 8620, 8310, > -8020, 7730, 7460, 7200, 6950, 6710, 6470, 6250, 6040, 5830, > -5640, 5450, 5260, 5090, 4920, 4760, 4600, 4450, 4310, 4170, > -4040, 3910, 3790, 3670, 3550 > +/* Conversion table from -3 to 55 degrees Celcius */ > +static int twl4030_therm_tbl[] = { > + 30800, 29500, 28300, 27100, > + 26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700, > + 17900, 17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100, > + 12600, 12100, 11600, 11200, 10800, 10400, 10000, 9630, 9280, > + 8950, 8620, 8310, 8020, 7730, 7460, 7200, 6950, 6710, > + 6470, 6250, 6040, 5830, 5640, 5450, 5260, 5090, 4920, > + 4760, 4600, 4450, 4310, 4170, 4040, 3910, 3790, 3670, > + 3550 > }; > > /* > @@ -199,7 +198,7 @@ const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = { > }, > }; > > -/* > +/** Whilst better - these are still not in kernel doc. Please read the nano howto and fix them up. > * Function to read a particular channel value. > * @madc - pointer to struct twl4030_madc_data > * @reg - lsb of ADC Channel > @@ -229,7 +228,7 @@ static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg) > } > > /* > - * Return battery temperature > + * Return battery temperature in degrees Celsius > * Or < 0 on failure. > */ > static int twl4030battery_temperature(int raw_volt) > @@ -238,18 +237,18 @@ static int twl4030battery_temperature(int raw_volt) > int temp, curr, volt, res, ret; > > volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R; > - /* Getting and calculating the supply current in micro ampers */ > + /* Getting and calculating the supply current in micro amperes */ > ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, > REG_BCICTL2); > if (ret < 0) > return ret; > + > curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10; > /* Getting and calculating the thermistor resistance in ohms */ > res = volt * 1000 / curr; > /* calculating temperature */ > for (temp = 58; temp >= 0; temp--) { > - int actual = therm_tbl[temp]; > - > + int actual = twl4030_therm_tbl[temp]; > if ((actual - res) >= 0) > break; > } > @@ -271,11 +270,12 @@ static int twl4030battery_current(int raw_volt) > else /* slope of 0.88 mV/mA */ > return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2; > } > + > /* > * Function to read channel values > * @madc - pointer to twl4030_madc_data struct > * @reg_base - Base address of the first channel > - * @Channels - 16 bit bitmap. If the bit is set, channel value is read > + * @Channels - 16 bit bitmap. If the bit is set, channel's value is read > * @buf - The channel values are stored here. if read fails error > * @raw - Return raw values without conversion > * value is stored > @@ -286,17 +286,17 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, > long channels, int *buf, > bool raw) > { > - int count = 0, count_req = 0, i; > + int count = 0; > + int i; > u8 reg; > > for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) { > - reg = reg_base + 2 * i; > + u8 reg = reg_base + (2 * i); Precedence means the brackets aren't needed. I don't suppose they do much harm though if you want to keep them. > buf[i] = twl4030_madc_channel_raw_read(madc, reg); > if (buf[i] < 0) { > - dev_err(madc->dev, > - "Unable to read register 0x%X\n", reg); > - count_req++; > - continue; > + dev_err(madc->dev, "Unable to read register 0x%X\n", > + reg); > + return buf[i]; > } > if (raw) { > count++; > @@ -307,7 +307,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, > buf[i] = twl4030battery_current(buf[i]); > if (buf[i] < 0) { > dev_err(madc->dev, "err reading current\n"); > - count_req++; > + return buf[i]; > } else { > count++; > buf[i] = buf[i] - 750; > @@ -317,7 +317,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, > buf[i] = twl4030battery_temperature(buf[i]); > if (buf[i] < 0) { > dev_err(madc->dev, "err reading temperature\n"); > - count_req++; > + return buf[i]; > } else { > buf[i] -= 3; > count++; > @@ -338,8 +338,6 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, > twl4030_divider_ratios[i].numerator); > } > } > - if (count_req) > - dev_err(madc->dev, "%d channel conversion failed\n", count_req); > > return count; > } > @@ -363,13 +361,13 @@ static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id) > madc->imr); > return ret; > } > + > val &= ~(1 << id); > ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); > if (ret) { > dev_err(madc->dev, > "unable to write imr register 0x%X\n", madc->imr); > return ret; > - > } > > return 0; > @@ -432,7 +430,7 @@ static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc) > continue; > ret = twl4030_madc_disable_irq(madc, i); > if (ret < 0) > - dev_dbg(madc->dev, "Disable interrupt failed%d\n", i); > + dev_dbg(madc->dev, "Disable interrupt failed %d\n", i); > madc->requests[i].result_pending = 1; > } > for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { > @@ -514,21 +512,17 @@ static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc, > { > const struct twl4030_madc_conversion_method *method; > int ret = 0; > + > + if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2) > + return -ENOTSUPP; > + > method = &twl4030_conversion_methods[conv_method]; > - switch (conv_method) { > - case TWL4030_MADC_SW1: > - case TWL4030_MADC_SW2: > - ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, > - TWL4030_MADC_SW_START, method->ctrl); > - if (ret) { > - dev_err(madc->dev, > - "unable to write ctrl register 0x%X\n", > - method->ctrl); > - return ret; > - } > - break; > - default: > - break; > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START, > + method->ctrl); > + if (ret) { > + dev_err(madc->dev, "unable to write ctrl register 0x%X\n", > + method->ctrl); > + return ret; > } > > return 0; > @@ -625,8 +619,8 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req) > ch_lsb, method->avg); > if (ret) { > dev_err(twl4030_madc->dev, > - "unable to write sel reg 0x%X\n", > - method->sel + 1); > + "unable to write avg reg 0x%X\n", > + method->avg); > goto out; > } > } > @@ -667,10 +661,6 @@ out: > } > EXPORT_SYMBOL_GPL(twl4030_madc_conversion); > > -/* > - * Return channel value > - * Or < 0 on failure. > - */ > int twl4030_get_madc_conversion(int channel_no) > { > struct twl4030_madc_request req; > @@ -705,6 +695,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc, > int chan, int on) > { > int ret; > + int regmask; > u8 regval; > > ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, > @@ -714,10 +705,13 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc, > TWL4030_BCI_BCICTL1); > return ret; > } > + > + regmask = chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN; > if (on) > - regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN; > + regval |= regmask; > else > - regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN; > + regval &= ~regmask; > + > ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, > regval, TWL4030_BCI_BCICTL1); > if (ret) { > @@ -732,7 +726,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc, > /* > * Function that sets MADC software power on bit to enable MADC > * @madc - pointer to twl4030_madc_data struct > - * @on - Enable or disable MADC software powen on bit. > + * @on - Enable or disable MADC software power on bit. > * returns error if i2c read/write fails else 0 > */ > static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) > @@ -795,7 +789,7 @@ static int twl4030_madc_probe(struct platform_device *pdev) > iio_dev->info = &twl4030_madc_iio_info; > iio_dev->modes = INDIO_DIRECT_MODE; > iio_dev->channels = twl4030_madc_iio_channels; > - iio_dev->num_channels = 16; > + iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels); Move this back into the previous patch please. We like to pretend that within a series we got everything right first time ;) > > /* > * Phoenix provides 2 interrupt lines. The first one is connected to > diff --git a/include/linux/i2c/twl4030-madc.h b/include/linux/i2c/twl4030-madc.h > index 01f5951..1c0134d 100644 > --- a/include/linux/i2c/twl4030-madc.h > +++ b/include/linux/i2c/twl4030-madc.h > @@ -44,7 +44,7 @@ struct twl4030_madc_conversion_method { > > struct twl4030_madc_request { > unsigned long channels; > - u16 do_avg; > + bool do_avg; > u16 method; > u16 type; > bool active; > -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On 26/02/14 20:03, Sebastian Reichel wrote: > Add a simple twl_i2c_read/write_u16 wrapper over > the twl_i2c_read/write, which is similar to the > twl_i2c_read/write_u8 wrapper. > > Signed-off-by: Sebastian Reichel <sre@debian.org> > Acked-by: Lee Jones <lee.jones@linaro.org> Acked-by: Jonathan Cameron <jic23@kernel.org> (I'll start a separate conversation about who will take this series in a mo). > --- > include/linux/i2c/twl.h | 12 ++++++++++++ > 1 file changed, 12 insertions(+) > > diff --git a/include/linux/i2c/twl.h b/include/linux/i2c/twl.h > index ade1c06..d2b1670 100644 > --- a/include/linux/i2c/twl.h > +++ b/include/linux/i2c/twl.h > @@ -195,6 +195,18 @@ static inline int twl_i2c_read_u8(u8 mod_no, u8 *val, u8 reg) { > return twl_i2c_read(mod_no, val, reg, 1); > } > > Hmm. This had me confused for a few mins given word rights in i2c are guaranteed to be little endian unless the swapped versions are used. I see that the twl driver is using a generic i2c bulk write. Ah well, I can see that simplifies the regmap i2c code and works in most cases... +static inline int twl_i2c_write_u16(u8 mod_no, u16 val, u8 reg) { > + val = cpu_to_le16(val); > + return twl_i2c_write(mod_no, (u8*) &val, reg, 2); > +} > + > +static inline int twl_i2c_read_u16(u8 mod_no, u16 *val, u8 reg) { > + int ret; > + ret = twl_i2c_read(mod_no, (u8*) val, reg, 2); > + *val = le16_to_cpu(*val); > + return ret; > +} > + > int twl_get_type(void); > int twl_get_version(void); > int twl_get_hfclk_rate(void); > -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On 26/02/14 20:03, Sebastian Reichel wrote: > Simplify reading and writing of 16 bit TWL registers in the > driver by using twl_i2c_read_u16 and twl_i2c_write_u16. > > Signed-off-by: Sebastian Reichel <sre@debian.org> > Acked-by: Lee Jones <lee.jones@linaro.org> Nice Acked-by: Jonathan Cameron <jic23@kernel.org> > --- > drivers/mfd/twl4030-madc.c | 39 ++++++++------------------------------- > 1 file changed, 8 insertions(+), 31 deletions(-) > > diff --git a/drivers/mfd/twl4030-madc.c b/drivers/mfd/twl4030-madc.c > index c90013e..d918670 100644 > --- a/drivers/mfd/twl4030-madc.c > +++ b/drivers/mfd/twl4030-madc.c > @@ -206,25 +206,19 @@ const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = { > */ > static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg) > { > - u8 msb, lsb; > + u16 val; > int ret; > /* > * For each ADC channel, we have MSB and LSB register pair. MSB address > * is always LSB address+1. reg parameter is the address of LSB register > */ > - ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1); > + ret = twl_i2c_read_u16(TWL4030_MODULE_MADC, &val, reg); > if (ret) { > - dev_err(madc->dev, "unable to read MSB register 0x%X\n", > - reg + 1); > - return ret; > - } > - ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg); > - if (ret) { > - dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg); > + dev_err(madc->dev, "unable to read register 0x%X\n", reg); > return ret; > } > > - return (int)(((msb << 8) | lsb) >> 6); > + return (int)(val >> 6); > } > > /* > @@ -573,7 +567,6 @@ static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc, > int twl4030_madc_conversion(struct twl4030_madc_request *req) > { > const struct twl4030_madc_conversion_method *method; > - u8 ch_msb, ch_lsb; > int ret; > > if (!req || !twl4030_madc) > @@ -589,37 +582,21 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req) > ret = -EBUSY; > goto out; > } > - ch_msb = (req->channels >> 8) & 0xff; > - ch_lsb = req->channels & 0xff; > method = &twl4030_conversion_methods[req->method]; > /* Select channels to be converted */ > - ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1); > + ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, method->sel); > if (ret) { > dev_err(twl4030_madc->dev, > - "unable to write sel register 0x%X\n", method->sel + 1); > - goto out; > - } > - ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel); > - if (ret) { > - dev_err(twl4030_madc->dev, > - "unable to write sel register 0x%X\n", method->sel + 1); > + "unable to write sel register 0x%X\n", method->sel); > goto out; > } > /* Select averaging for all channels if do_avg is set */ > if (req->do_avg) { > - ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, > - ch_msb, method->avg + 1); > + ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, > + method->avg); > if (ret) { > dev_err(twl4030_madc->dev, > "unable to write avg register 0x%X\n", > - method->avg + 1); > - goto out; > - } > - ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, > - ch_lsb, method->avg); > - if (ret) { > - dev_err(twl4030_madc->dev, > - "unable to write avg reg 0x%X\n", > method->avg); > goto out; > } > -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On 26/02/14 20:03, Sebastian Reichel wrote: > This is a driver for an A/D converter, which belongs into > drivers/iio/adc. > > Signed-off-by: Sebastian Reichel <sre@debian.org> > Acked-by: Lee Jones <lee.jones@linaro.org> Acked-by: Jonathan Cameron <jic23@kernel.org> > --- > drivers/iio/adc/Kconfig | 10 ++++++++++ > drivers/iio/adc/Makefile | 1 + > drivers/{mfd => iio/adc}/twl4030-madc.c | 0 > drivers/mfd/Kconfig | 10 ---------- > drivers/mfd/Makefile | 1 - > 5 files changed, 11 insertions(+), 11 deletions(-) > rename drivers/{mfd => iio/adc}/twl4030-madc.c (100%) > > diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig > index 2209f28..427f75c 100644 > --- a/drivers/iio/adc/Kconfig > +++ b/drivers/iio/adc/Kconfig > @@ -183,6 +183,16 @@ config TI_AM335X_ADC > Say yes here to build support for Texas Instruments ADC > driver which is also a MFD client. > > +config TWL4030_MADC > + tristate "TWL4030 MADC (Monitoring A/D Converter)" > + depends on TWL4030_CORE > + help > + This driver provides support for Triton TWL4030-MADC. The > + driver supports both RT and SW conversion methods. > + > + This driver can also be built as a module. If so, the module will be > + called twl4030-madc. > + > config TWL6030_GPADC > tristate "TWL6030 GPADC (General Purpose A/D Converter) Support" > depends on TWL4030_CORE > diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile > index ba9a10a..9acf2df 100644 > --- a/drivers/iio/adc/Makefile > +++ b/drivers/iio/adc/Makefile > @@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o > obj-$(CONFIG_NAU7802) += nau7802.o > obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o > obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o > +obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o > obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o > obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o > diff --git a/drivers/mfd/twl4030-madc.c b/drivers/iio/adc/twl4030-madc.c > similarity index 100% > rename from drivers/mfd/twl4030-madc.c > rename to drivers/iio/adc/twl4030-madc.c > diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig > index 49bb445..23a8a51 100644 > --- a/drivers/mfd/Kconfig > +++ b/drivers/mfd/Kconfig > @@ -935,16 +935,6 @@ config TWL4030_CORE > high speed USB OTG transceiver, an audio codec (on most > versions) and many other features. > > -config TWL4030_MADC > - tristate "TI TWL4030 MADC" > - depends on TWL4030_CORE > - help > - This driver provides support for triton TWL4030-MADC. The > - driver supports both RT and SW conversion methods. > - > - This driver can be built as a module. If so it will be > - named twl4030-madc > - > config TWL4030_POWER > bool "TI TWL4030 power resources" > depends on TWL4030_CORE && ARM > diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile > index 5aea5ef..c8eb0bc 100644 > --- a/drivers/mfd/Makefile > +++ b/drivers/mfd/Makefile > @@ -71,7 +71,6 @@ obj-$(CONFIG_MFD_TPS80031) += tps80031.o > obj-$(CONFIG_MENELAUS) += menelaus.o > > obj-$(CONFIG_TWL4030_CORE) += twl-core.o twl4030-irq.o twl6030-irq.o > -obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o > obj-$(CONFIG_TWL4030_POWER) += twl4030-power.o > obj-$(CONFIG_MFD_TWL4030_AUDIO) += twl4030-audio.o > obj-$(CONFIG_TWL6040_CORE) += twl6040.o > -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On 26/02/14 20:03, Sebastian Reichel wrote: > Hi, > > This is RFCv4 for converting twl4030-madc to the IIO API and > adding DT support. The patchset compiles and has been tested > on my Nokia N900. > > Changes since RFCv3 [0]: > * Added Acked-by from Lee Jones (MFD subsystem maintainer) > to twl_i2c_read/write_u16 patches > * Fixed style issues found by Lee Jones > This readded the temp variable Jonathan Cameron wanted to > be removed, but makes the code cleaner - especially once > the brackets for math for clarity are added. > * Tested with converted rx51-battery driver on Nokia N900, > which can be found at [1]. > > [0] https://lkml.org/lkml/2014/2/25/627 > [1] https://lkml.org/lkml/2014/2/25/639 Hi All, Obviously there are a few outstanding bits and pieces to be addressed. To get this lined up for a merge... Lee, do you want to take this through mfd or shall I take it through IIO? I don't mind either way. It stands pretty separate in IIO so shouldn't cause any merge issues beyond minor fuzz. Jonathan > > -- Sebastian > > Sebastian Reichel (7): > mfd: twl4030-madc: Use managed resources > mfd: twl4030-madc: Add DT support and convert to IIO framework > mfd: twl4030-madc: Cleanup driver > mfd: twl-core: Add twl_i2c_read/write_u16 > mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers > Documentation: DT: Document twl4030-madc binding > mfd: twl4030-madc: Move driver to drivers/iio/adc > > .../devicetree/bindings/iio/adc/twl4030-madc.txt | 24 ++ > drivers/iio/adc/Kconfig | 10 + > drivers/iio/adc/Makefile | 1 + > drivers/{mfd => iio/adc}/twl4030-madc.c | 281 +++++++++++++-------- > drivers/mfd/Kconfig | 10 - > drivers/mfd/Makefile | 1 - > include/linux/i2c/twl.h | 12 + > include/linux/i2c/twl4030-madc.h | 2 +- > 8 files changed, 227 insertions(+), 114 deletions(-) > create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt > rename drivers/{mfd => iio/adc}/twl4030-madc.c (77%) > -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi Jonathan, On Sat, Mar 01, 2014 at 11:52:52AM +0000, Jonathan Cameron wrote: > Obviously there are a few outstanding bits and pieces to be > addressed. To get this lined up for a merge... I will send out a new patchset in a few minutes. > Lee, do you want to take this through mfd or shall I take it through > IIO? I don't mind either way. It stands pretty separate in IIO so > shouldn't cause any merge issues beyond minor fuzz. I think it makes sense to queue it via IIO once I add the ABI documentation for the IIO *_mean_raw sysfs entries. Also I think it makes sense to add iio_read_channel_average_raw() in this patchset, since its needed to convert users of the old madc API to the IIO API. -- Sebastian
Hi, This is PATCHv1 for converting twl4030-madc to the IIO API and adding DT support. The plan is to remove the private twl4030-madc API once all users have been removed or converted to the IIO API. The patchset compiles and has been tested on my Nokia N900. Changes since RFCv4 [0]: * Fixup all stuff found by Lee Jones and Jonathan Cameron in RFCv4. - Fixup kerneldoc comments - Move ARRAY_SIZE() into correct patch. I accidently rebased it into the wrong one. - Use sizeof(*madc) instead of sizeof(struct twl4030_madc_data) - Only call of_property_read_bool() if there is no pdata - I left the brackets in "reg_base + (2 * i)". They have been requested by Lee Jones and I think they make it easier to understand what is going on. * Add a bunch of Acked-By statements from those two * Add new patch, which adds *_mean_raw sysfs ABI documentation as requested by Jonathan Cameron * Add another patch, which provides iio_read_channel_average_raw(), so that drivers using the old API can be converted to the IIO API. * I switched from RFC to PATCH prefix. TODO: * Wait for feedback from DT binding maintainers * Wait for feedback from Marek Belisko, who is testing the change with twl4030-madc-battery driver on GTA04 [0] https://lkml.org/lkml/2014/2/26/482 -- Sebastian Sebastian Reichel (9): mfd: twl4030-madc: Use managed resources mfd: twl4030-madc: Add DT support and convert to IIO framework mfd: twl4030-madc: Cleanup driver mfd: twl-core: Add twl_i2c_read/write_u16 mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers Documentation: DT: Document twl4030-madc binding mfd: twl4030-madc: Move driver to drivers/iio/adc iio: documentation: Add ABI documentation for *_mean_raw iio: inkern: add iio_read_channel_average_raw Documentation/ABI/testing/sysfs-bus-iio | 8 + .../devicetree/bindings/iio/adc/twl4030-madc.txt | 24 ++ drivers/iio/adc/Kconfig | 10 + drivers/iio/adc/Makefile | 1 + drivers/{mfd => iio/adc}/twl4030-madc.c | 301 +++++++++++++-------- drivers/iio/inkern.c | 18 ++ drivers/mfd/Kconfig | 10 - drivers/mfd/Makefile | 1 - include/linux/i2c/twl.h | 12 + include/linux/i2c/twl4030-madc.h | 2 +- include/linux/iio/consumer.h | 13 + 11 files changed, 276 insertions(+), 124 deletions(-) create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt rename drivers/{mfd => iio/adc}/twl4030-madc.c (75%)
On Sat, 01 Mar 2014, Jonathan Cameron wrote: > On 26/02/14 20:03, Sebastian Reichel wrote: > >Hi, > > > >This is RFCv4 for converting twl4030-madc to the IIO API and > >adding DT support. The patchset compiles and has been tested > >on my Nokia N900. > > > >Changes since RFCv3 [0]: > > * Added Acked-by from Lee Jones (MFD subsystem maintainer) > > to twl_i2c_read/write_u16 patches > > * Fixed style issues found by Lee Jones > > This readded the temp variable Jonathan Cameron wanted to > > be removed, but makes the code cleaner - especially once > > the brackets for math for clarity are added. > > * Tested with converted rx51-battery driver on Nokia N900, > > which can be found at [1]. > > > >[0] https://lkml.org/lkml/2014/2/25/627 > >[1] https://lkml.org/lkml/2014/2/25/639 > > Hi All, > > Obviously there are a few outstanding bits and pieces to be addressed. > > To get this lined up for a merge... > > Lee, do you want to take this through mfd or shall I take it through > IIO? I don't mind either way. It stands pretty separate in IIO so > shouldn't cause any merge issues beyond minor fuzz. I'm happy to take it through MFD with your Ack.
Hi, This is PATCHv2 for converting twl4030-madc to the IIO API and adding DT support. The plan is to remove the private twl4030-madc API once all users have been removed or converted to the IIO API. The patchset compiles and has been tested on my Nokia N900. Changes since PATCHv1 [0]: * Fix warning about unused variable * Add IIO_CHAN_INFO_PROCESSED to all channels, since voltage channels also need some postprocessing TODO: * Wait for feedback from DT binding maintainers * Wait for feedback from Marek Belisko, who is testing the changes with twl4030-madc-battery driver on GTA04 [0] https://lkml.org/lkml/2014/3/1/105 -- Sebastian Sebastian Reichel (9): mfd: twl4030-madc: Use managed resources mfd: twl4030-madc: Add DT support and convert to IIO framework mfd: twl4030-madc: Cleanup driver mfd: twl-core: Add twl_i2c_read/write_u16 mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers Documentation: DT: Document twl4030-madc binding mfd: twl4030-madc: Move driver to drivers/iio/adc iio: documentation: Add ABI documentation for *_mean_raw iio: inkern: add iio_read_channel_average_raw Documentation/ABI/testing/sysfs-bus-iio | 8 + .../devicetree/bindings/iio/adc/twl4030-madc.txt | 24 ++ drivers/iio/adc/Kconfig | 10 + drivers/iio/adc/Makefile | 1 + drivers/{mfd => iio/adc}/twl4030-madc.c | 299 +++++++++++++-------- drivers/iio/inkern.c | 18 ++ drivers/mfd/Kconfig | 10 - drivers/mfd/Makefile | 1 - include/linux/i2c/twl.h | 12 + include/linux/i2c/twl4030-madc.h | 2 +- include/linux/iio/consumer.h | 13 + 11 files changed, 274 insertions(+), 124 deletions(-) create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt rename drivers/{mfd => iio/adc}/twl4030-madc.c (75%)
> This converts twl4030-madc module to use the Industrial IO ADC > framework and adds device tree support. > > Signed-off-by: Sebastian Reichel <sre@debian.org> > --- > drivers/mfd/twl4030-madc.c | 126 +++++++++++++++++++++++++++++++++++++++++---- > 1 file changed, 116 insertions(+), 10 deletions(-) > > diff --git a/drivers/mfd/twl4030-madc.c b/drivers/mfd/twl4030-madc.c > index 5458561..0479af0 100644 > --- a/drivers/mfd/twl4030-madc.c > +++ b/drivers/mfd/twl4030-madc.c <snip> @@ -702,28 +766,50 @@ static int twl4030_madc_probe(struct platform_device *pdev) > - madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL); > - if (!madc) > + > + iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*madc)); > + if (!iio_dev) { > + dev_err(&pdev->dev, "failed allocating iio device\n"); > return -ENOMEM; > + } > > + madc = iio_priv(iio_dev); > madc->dev = &pdev->dev; > + madc->use_second_irq = false; You don't need to do this a) it will be set below and b) it's already false since you used kzalloc() to zero the region. <snip> > - madc->imr = (pdata->irq_line == 1) ? > - TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2; > - madc->isr = (pdata->irq_line == 1) ? > - TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2; > + if (pdata && pdata->irq_line != 1) > + madc->use_second_irq = true; > + else if (!pdata) > + madc->use_second_irq = of_property_read_bool(np, > + "ti,system-uses-second-madc-irq"); How about: if (pdata) madc->use_second_irq = (pdata->irq_line != 1); else madc->use_second_irq = of_property_read_bool(np, "ti,system-uses-second-madc-irq"); <snip> > + ret = iio_device_register(iio_dev); > + if (ret) { > + dev_dbg(&pdev->dev, "could not register iio device\n"); This should be a dev_err(). <snip> > +#ifdef CONFIG_OF > +static const struct of_device_id twl_madc_of_match[] = { > + {.compatible = "ti,twl4030-madc", }, nit: "{ .compatible" > static struct platform_driver twl4030_madc_driver = { > .probe = twl4030_madc_probe, > .remove = twl4030_madc_remove, > .driver = { > .name = "twl4030_madc", > .owner = THIS_MODULE, > + .of_match_table = of_match_ptr(twl_madc_of_match), > }, Also fix this please. > }; >
Hi, On Tue, Mar 4, 2014 at 11:05 PM, Sebastian Reichel <sre@debian.org> wrote: > Hi, > > This is PATCHv2 for converting twl4030-madc to the IIO API and > adding DT support. The plan is to remove the private twl4030-madc > API once all users have been removed or converted to the IIO API. > The patchset compiles and has been tested on my Nokia N900. > > Changes since PATCHv1 [0]: > * Fix warning about unused variable > * Add IIO_CHAN_INFO_PROCESSED to all channels, since voltage > channels also need some postprocessing > > TODO: > * Wait for feedback from DT binding maintainers > * Wait for feedback from Marek Belisko, who is testing the > changes with twl4030-madc-battery driver on GTA04 Tested on gta04 board. Works as expected with twl4030_madc_battery patches [1] on top. So you can add my Tested-by: Marek Belisko <marek@goldelico.com> > > [0] https://lkml.org/lkml/2014/3/1/105 > > -- Sebastian > > Sebastian Reichel (9): > mfd: twl4030-madc: Use managed resources > mfd: twl4030-madc: Add DT support and convert to IIO framework > mfd: twl4030-madc: Cleanup driver > mfd: twl-core: Add twl_i2c_read/write_u16 > mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers > Documentation: DT: Document twl4030-madc binding > mfd: twl4030-madc: Move driver to drivers/iio/adc > iio: documentation: Add ABI documentation for *_mean_raw > iio: inkern: add iio_read_channel_average_raw > > Documentation/ABI/testing/sysfs-bus-iio | 8 + > .../devicetree/bindings/iio/adc/twl4030-madc.txt | 24 ++ > drivers/iio/adc/Kconfig | 10 + > drivers/iio/adc/Makefile | 1 + > drivers/{mfd => iio/adc}/twl4030-madc.c | 299 +++++++++++++-------- > drivers/iio/inkern.c | 18 ++ > drivers/mfd/Kconfig | 10 - > drivers/mfd/Makefile | 1 - > include/linux/i2c/twl.h | 12 + > include/linux/i2c/twl4030-madc.h | 2 +- > include/linux/iio/consumer.h | 13 + > 11 files changed, 274 insertions(+), 124 deletions(-) > create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt > rename drivers/{mfd => iio/adc}/twl4030-madc.c (75%) > > -- > 1.9.0 > [1]: http://www.spinics.net/lists/kernel/msg1700793.html BR, marek
> TODO: > * Wait for feedback from DT binding maintainers > * Wait for feedback from Marek Belisko, who is testing the > changes with twl4030-madc-battery driver on GTA04 This patch-set has stalled. Can you send a fresh patch-set (not linked to the old ones, as the thread is getting very complicated again) complete with all the Acks you have received? > [0] https://lkml.org/lkml/2014/3/1/105 > > -- Sebastian > > Sebastian Reichel (9): > mfd: twl4030-madc: Use managed resources > mfd: twl4030-madc: Add DT support and convert to IIO framework > mfd: twl4030-madc: Cleanup driver > mfd: twl-core: Add twl_i2c_read/write_u16 > mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers > Documentation: DT: Document twl4030-madc binding > mfd: twl4030-madc: Move driver to drivers/iio/adc > iio: documentation: Add ABI documentation for *_mean_raw > iio: inkern: add iio_read_channel_average_raw > > Documentation/ABI/testing/sysfs-bus-iio | 8 + > .../devicetree/bindings/iio/adc/twl4030-madc.txt | 24 ++ > drivers/iio/adc/Kconfig | 10 + > drivers/iio/adc/Makefile | 1 + > drivers/{mfd => iio/adc}/twl4030-madc.c | 299 +++++++++++++-------- > drivers/iio/inkern.c | 18 ++ > drivers/mfd/Kconfig | 10 - > drivers/mfd/Makefile | 1 - > include/linux/i2c/twl.h | 12 + > include/linux/i2c/twl4030-madc.h | 2 +- > include/linux/iio/consumer.h | 13 + > 11 files changed, 274 insertions(+), 124 deletions(-) > create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt > rename drivers/{mfd => iio/adc}/twl4030-madc.c (75%) >
On Mon, Mar 10, 2014 at 10:43:56AM +0000, Lee Jones wrote: > > TODO: > > * Wait for feedback from DT binding maintainers > > * Wait for feedback from Marek Belisko, who is testing the > > changes with twl4030-madc-battery driver on GTA04 > > This patch-set has stalled. Right. I waited a few days for more feedback. > Can you send a fresh patch-set (not linked to the old ones, as the > thread is getting very complicated again) complete with all the Acks > you have received? Sure. I will send it later. -- Sebastian
On 04/03/14 22:05, Sebastian Reichel wrote: > Update twl4030-madc driver to use managed resources. > > Signed-off-by: Sebastian Reichel <sre@debian.org> > Acked-by: Lee Jones <lee.jones@linaro.org> Acked-by: Jonathan Cameron <jic23@kernel.org> > --- > drivers/mfd/twl4030-madc.c | 15 ++++++--------- > 1 file changed, 6 insertions(+), 9 deletions(-) > > diff --git a/drivers/mfd/twl4030-madc.c b/drivers/mfd/twl4030-madc.c > index 4c583e4..5458561 100644 > --- a/drivers/mfd/twl4030-madc.c > +++ b/drivers/mfd/twl4030-madc.c > @@ -702,14 +702,14 @@ static int twl4030_madc_probe(struct platform_device *pdev) > { > struct twl4030_madc_data *madc; > struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev); > - int ret; > + int irq, ret; > u8 regval; > > if (!pdata) { > dev_err(&pdev->dev, "platform_data not available\n"); > return -EINVAL; > } > - madc = kzalloc(sizeof(*madc), GFP_KERNEL); > + madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL); > if (!madc) > return -ENOMEM; > > @@ -726,7 +726,7 @@ static int twl4030_madc_probe(struct platform_device *pdev) > TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2; > ret = twl4030_madc_set_power(madc, 1); > if (ret < 0) > - goto err_power; > + return ret; > ret = twl4030_madc_set_current_generator(madc, 0, 1); > if (ret < 0) > goto err_current_generator; > @@ -770,7 +770,9 @@ static int twl4030_madc_probe(struct platform_device *pdev) > > platform_set_drvdata(pdev, madc); > mutex_init(&madc->lock); > - ret = request_threaded_irq(platform_get_irq(pdev, 0), NULL, > + > + irq = platform_get_irq(pdev, 0); > + ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, > twl4030_madc_threaded_irq_handler, > IRQF_TRIGGER_RISING, "twl4030_madc", madc); > if (ret) { > @@ -783,9 +785,6 @@ err_i2c: > twl4030_madc_set_current_generator(madc, 0, 0); > err_current_generator: > twl4030_madc_set_power(madc, 0); > -err_power: > - kfree(madc); > - > return ret; > } > > @@ -793,10 +792,8 @@ static int twl4030_madc_remove(struct platform_device *pdev) > { > struct twl4030_madc_data *madc = platform_get_drvdata(pdev); > > - free_irq(platform_get_irq(pdev, 0), madc); > twl4030_madc_set_current_generator(madc, 0, 0); > twl4030_madc_set_power(madc, 0); > - kfree(madc); > > return 0; > } > -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On 04/03/14 22:05, Sebastian Reichel wrote: > This converts twl4030-madc module to use the Industrial IO ADC > framework and adds device tree support. > > Signed-off-by: Sebastian Reichel <sre@debian.org> One issues right down in the remove function alongside the ones Lee raised. Otherwise looks pretty much there to me. > --- > drivers/mfd/twl4030-madc.c | 126 +++++++++++++++++++++++++++++++++++++++++---- > 1 file changed, 116 insertions(+), 10 deletions(-) > > diff --git a/drivers/mfd/twl4030-madc.c b/drivers/mfd/twl4030-madc.c > index 5458561..0479af0 100644 > --- a/drivers/mfd/twl4030-madc.c > +++ b/drivers/mfd/twl4030-madc.c > @@ -47,11 +47,14 @@ > #include <linux/gfp.h> > #include <linux/err.h> > > +#include <linux/iio/iio.h> > + > /* > * struct twl4030_madc_data - a container for madc info > * @dev - pointer to device structure for madc > * @lock - mutex protecting this data structure > * @requests - Array of request struct corresponding to SW1, SW2 and RT > + * @use_second_irq - IRQ selection (main or co-processor) > * @imr - Interrupt mask register of MADC > * @isr - Interrupt status register of MADC > */ > @@ -59,10 +62,71 @@ struct twl4030_madc_data { > struct device *dev; > struct mutex lock; /* mutex protecting this data structure */ > struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS]; > + bool use_second_irq; > int imr; > int isr; > }; > > +static int twl4030_madc_read(struct iio_dev *iio_dev, > + const struct iio_chan_spec *chan, > + int *val, int *val2, long mask) > +{ > + struct twl4030_madc_data *madc = iio_priv(iio_dev); > + struct twl4030_madc_request req; > + int ret; > + > + req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1; > + > + req.channels = BIT(chan->channel); > + req.active = false; > + req.func_cb = NULL; > + req.type = TWL4030_MADC_WAIT; > + req.raw = !(mask == IIO_CHAN_INFO_PROCESSED); > + req.do_avg = (mask == IIO_CHAN_INFO_AVERAGE_RAW); > + > + ret = twl4030_madc_conversion(&req); > + if (ret < 0) > + return ret; > + > + *val = req.rbuf[chan->channel]; > + > + return IIO_VAL_INT; > +} > + > +static const struct iio_info twl4030_madc_iio_info = { > + .read_raw = &twl4030_madc_read, > + .driver_module = THIS_MODULE, > +}; > + > +#define TWL4030_ADC_CHANNEL(_channel, _type, _name) { \ > + .type = _type, \ > + .channel = _channel, \ > + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ > + BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \ > + BIT(IIO_CHAN_INFO_PROCESSED), \ > + .datasheet_name = _name, \ > + .indexed = 1, \ > +} > + > +static const struct iio_chan_spec twl4030_madc_iio_channels[] = { > + TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0"), > + TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1"), > + TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2"), > + TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3"), > + TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4"), > + TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5"), > + TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6"), > + TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7"), > + TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8"), > + TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9"), > + TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10"), > + TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11"), > + TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12"), > + TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13"), > + TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14"), > + TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15"), > +}; > + > static struct twl4030_madc_data *twl4030_madc; > > struct twl4030_prescale_divider_ratios { > @@ -702,28 +766,50 @@ static int twl4030_madc_probe(struct platform_device *pdev) > { > struct twl4030_madc_data *madc; > struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev); > + struct device_node *np = pdev->dev.of_node; > int irq, ret; > u8 regval; > + struct iio_dev *iio_dev = NULL; > > - if (!pdata) { > - dev_err(&pdev->dev, "platform_data not available\n"); > + if (!pdata && !np) { > + dev_err(&pdev->dev, "neither platform data nor Device Tree node available\n"); > return -EINVAL; > } > - madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL); > - if (!madc) > + > + iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*madc)); > + if (!iio_dev) { > + dev_err(&pdev->dev, "failed allocating iio device\n"); > return -ENOMEM; > + } > > + madc = iio_priv(iio_dev); > madc->dev = &pdev->dev; > + madc->use_second_irq = false; > + > + iio_dev->name = dev_name(&pdev->dev); > + iio_dev->dev.parent = &pdev->dev; > + iio_dev->dev.of_node = pdev->dev.of_node; > + iio_dev->info = &twl4030_madc_iio_info; > + iio_dev->modes = INDIO_DIRECT_MODE; > + iio_dev->channels = twl4030_madc_iio_channels; > + iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels); > > /* > * Phoenix provides 2 interrupt lines. The first one is connected to > * the OMAP. The other one can be connected to the other processor such > * as modem. Hence two separate ISR and IMR registers. > */ > - madc->imr = (pdata->irq_line == 1) ? > - TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2; > - madc->isr = (pdata->irq_line == 1) ? > - TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2; > + if (pdata && pdata->irq_line != 1) > + madc->use_second_irq = true; > + else if (!pdata) > + madc->use_second_irq = of_property_read_bool(np, > + "ti,system-uses-second-madc-irq"); > + > + madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 : > + TWL4030_MADC_IMR1; > + madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 : > + TWL4030_MADC_ISR1; > + > ret = twl4030_madc_set_power(madc, 1); > if (ret < 0) > return ret; > @@ -768,7 +854,7 @@ static int twl4030_madc_probe(struct platform_device *pdev) > } > } > > - platform_set_drvdata(pdev, madc); > + platform_set_drvdata(pdev, iio_dev); > mutex_init(&madc->lock); > > irq = platform_get_irq(pdev, 0); > @@ -780,7 +866,15 @@ static int twl4030_madc_probe(struct platform_device *pdev) > goto err_i2c; > } > twl4030_madc = madc; > + > + ret = iio_device_register(iio_dev); > + if (ret) { > + dev_dbg(&pdev->dev, "could not register iio device\n"); > + goto err_i2c; > + } > + > return 0; > + > err_i2c: > twl4030_madc_set_current_generator(madc, 0, 0); > err_current_generator: > @@ -790,20 +884,32 @@ err_current_generator: > > static int twl4030_madc_remove(struct platform_device *pdev) > { > - struct twl4030_madc_data *madc = platform_get_drvdata(pdev); > + struct iio_dev *iio_dev = platform_get_drvdata(pdev); > + struct twl4030_madc_data *madc = iio_priv(iio_dev); > > twl4030_madc_set_current_generator(madc, 0, 0); > twl4030_madc_set_power(madc, 0); > > + iio_device_unregister(iio_dev); Removing the user interfaces after disabling the current generator and turning the device off seems to me to be a bad idea. There is a reason iio_device_unregister is almost always the first thing done in the remove function. Also from a general ease of review point of view. The remove function should be in 'precise' reverse order of the probe function. Makes it nice and obviously correct assuming the functions it is calling are sensible! > + > return 0; > } > > +#ifdef CONFIG_OF > +static const struct of_device_id twl_madc_of_match[] = { > + {.compatible = "ti,twl4030-madc", }, > + { }, > +}; > +MODULE_DEVICE_TABLE(of, twl_madc_of_match); > +#endif > + > static struct platform_driver twl4030_madc_driver = { > .probe = twl4030_madc_probe, > .remove = twl4030_madc_remove, > .driver = { > .name = "twl4030_madc", > .owner = THIS_MODULE, > + .of_match_table = of_match_ptr(twl_madc_of_match), > }, > }; > > -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On 04/03/14 22:05, Sebastian Reichel wrote: > Some style fixes in twl4030-madc driver. > > Reported-by: Jonathan Cameron <jic23@kernel.org> Gah - not sure I want to be known for reporting style issues :) > Signed-off-by: Sebastian Reichel <sre@debian.org> > Acked-by: Lee Jones <lee.jones@linaro.org> Looks good - one little whilst you are here comment below. Acked-by: Jonathan Cameron <jic23@kernel.org> whether you do that or not. We can hardly hold there being remaining style issues against a style cleanup patch ;) > --- > drivers/mfd/twl4030-madc.c | 125 +++++++++++++++++++-------------------- > include/linux/i2c/twl4030-madc.h | 2 +- > 2 files changed, 61 insertions(+), 66 deletions(-) > > diff --git a/drivers/mfd/twl4030-madc.c b/drivers/mfd/twl4030-madc.c > index 0479af0..f37da65 100644 > --- a/drivers/mfd/twl4030-madc.c > +++ b/drivers/mfd/twl4030-madc.c > @@ -49,22 +49,22 @@ > > #include <linux/iio/iio.h> > > -/* > +/** > * struct twl4030_madc_data - a container for madc info > - * @dev - pointer to device structure for madc > - * @lock - mutex protecting this data structure > - * @requests - Array of request struct corresponding to SW1, SW2 and RT > - * @use_second_irq - IRQ selection (main or co-processor) > - * @imr - Interrupt mask register of MADC > - * @isr - Interrupt status register of MADC > + * @dev: Pointer to device structure for madc > + * @lock: Mutex protecting this data structure > + * @requests: Array of request struct corresponding to SW1, SW2 and RT > + * @use_second_irq: IRQ selection (main or co-processor) > + * @imr: Interrupt mask register of MADC > + * @isr: Interrupt status register of MADC > */ > struct twl4030_madc_data { > struct device *dev; > struct mutex lock; /* mutex protecting this data structure */ > struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS]; > bool use_second_irq; > - int imr; > - int isr; > + u8 imr; > + u8 isr; > }; > > static int twl4030_madc_read(struct iio_dev *iio_dev, > @@ -155,17 +155,16 @@ twl4030_divider_ratios[16] = { > }; > > > -/* > - * Conversion table from -3 to 55 degree Celcius > - */ > -static int therm_tbl[] = { > -30800, 29500, 28300, 27100, > -26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700, 17900, > -17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100, 12600, 12100, > -11600, 11200, 10800, 10400, 10000, 9630, 9280, 8950, 8620, 8310, > -8020, 7730, 7460, 7200, 6950, 6710, 6470, 6250, 6040, 5830, > -5640, 5450, 5260, 5090, 4920, 4760, 4600, 4450, 4310, 4170, > -4040, 3910, 3790, 3670, 3550 > +/* Conversion table from -3 to 55 degrees Celcius */ > +static int twl4030_therm_tbl[] = { > + 30800, 29500, 28300, 27100, > + 26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700, > + 17900, 17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100, > + 12600, 12100, 11600, 11200, 10800, 10400, 10000, 9630, 9280, > + 8950, 8620, 8310, 8020, 7730, 7460, 7200, 6950, 6710, > + 6470, 6250, 6040, 5830, 5640, 5450, 5260, 5090, 4920, > + 4760, 4600, 4450, 4310, 4170, 4040, 3910, 3790, 3670, > + 3550 > }; > > /* > @@ -197,11 +196,12 @@ const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = { > }, > }; > > -/* > - * Function to read a particular channel value. > - * @madc - pointer to struct twl4030_madc_data > - * @reg - lsb of ADC Channel > - * If the i2c read fails it returns an error else returns 0. > +/** > + * twl4030_madc_channel_raw_read() - Function to read a particular channel value > + * @madc: pointer to struct twl4030_madc_data > + * @reg: lsb of ADC Channel > + * > + * Return: 0 on success, an error code otherwise. > */ > static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg) > { > @@ -227,7 +227,7 @@ static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg) > } > > /* > - * Return battery temperature > + * Return battery temperature in degrees Celsius > * Or < 0 on failure. > */ > static int twl4030battery_temperature(int raw_volt) > @@ -236,18 +236,18 @@ static int twl4030battery_temperature(int raw_volt) > int temp, curr, volt, res, ret; > > volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R; > - /* Getting and calculating the supply current in micro ampers */ > + /* Getting and calculating the supply current in micro amperes */ > ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, > REG_BCICTL2); > if (ret < 0) > return ret; > + > curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10; > /* Getting and calculating the thermistor resistance in ohms */ > res = volt * 1000 / curr; > /* calculating temperature */ > for (temp = 58; temp >= 0; temp--) { > - int actual = therm_tbl[temp]; > - > + int actual = twl4030_therm_tbl[temp]; > if ((actual - res) >= 0) > break; > } > @@ -269,11 +269,12 @@ static int twl4030battery_current(int raw_volt) > else /* slope of 0.88 mV/mA */ > return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2; > } > + > /* > * Function to read channel values > * @madc - pointer to twl4030_madc_data struct > * @reg_base - Base address of the first channel > - * @Channels - 16 bit bitmap. If the bit is set, channel value is read > + * @Channels - 16 bit bitmap. If the bit is set, channel's value is read > * @buf - The channel values are stored here. if read fails error > * @raw - Return raw values without conversion > * value is stored > @@ -284,17 +285,17 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, > long channels, int *buf, > bool raw) > { > - int count = 0, count_req = 0, i; > + int count = 0; > + int i; > u8 reg; > > for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) { > - reg = reg_base + 2 * i; > + reg = reg_base + (2 * i); > buf[i] = twl4030_madc_channel_raw_read(madc, reg); > if (buf[i] < 0) { > - dev_err(madc->dev, > - "Unable to read register 0x%X\n", reg); > - count_req++; > - continue; > + dev_err(madc->dev, "Unable to read register 0x%X\n", > + reg); > + return buf[i]; > } > if (raw) { > count++; > @@ -305,7 +306,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, > buf[i] = twl4030battery_current(buf[i]); > if (buf[i] < 0) { > dev_err(madc->dev, "err reading current\n"); > - count_req++; > + return buf[i]; > } else { > count++; > buf[i] = buf[i] - 750; > @@ -315,7 +316,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, > buf[i] = twl4030battery_temperature(buf[i]); > if (buf[i] < 0) { > dev_err(madc->dev, "err reading temperature\n"); > - count_req++; > + return buf[i]; > } else { > buf[i] -= 3; > count++; > @@ -336,8 +337,6 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, > twl4030_divider_ratios[i].numerator); > } > } > - if (count_req) > - dev_err(madc->dev, "%d channel conversion failed\n", count_req); > > return count; > } > @@ -361,13 +360,13 @@ static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id) > madc->imr); > return ret; > } > + > val &= ~(1 << id); > ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr); > if (ret) { > dev_err(madc->dev, > "unable to write imr register 0x%X\n", madc->imr); > return ret; > - > } > > return 0; > @@ -430,7 +429,7 @@ static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc) > continue; > ret = twl4030_madc_disable_irq(madc, i); > if (ret < 0) > - dev_dbg(madc->dev, "Disable interrupt failed%d\n", i); > + dev_dbg(madc->dev, "Disable interrupt failed %d\n", i); > madc->requests[i].result_pending = 1; > } > for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) { > @@ -512,21 +511,17 @@ static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc, > { > const struct twl4030_madc_conversion_method *method; > int ret = 0; > + > + if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2) > + return -ENOTSUPP; > + > method = &twl4030_conversion_methods[conv_method]; > - switch (conv_method) { > - case TWL4030_MADC_SW1: > - case TWL4030_MADC_SW2: > - ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, > - TWL4030_MADC_SW_START, method->ctrl); > - if (ret) { > - dev_err(madc->dev, > - "unable to write ctrl register 0x%X\n", > - method->ctrl); > - return ret; > - } > - break; > - default: > - break; > + ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START, > + method->ctrl); > + if (ret) { > + dev_err(madc->dev, "unable to write ctrl register 0x%X\n", > + method->ctrl); > + return ret; > } > > return 0; > @@ -623,8 +618,8 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req) > ch_lsb, method->avg); > if (ret) { > dev_err(twl4030_madc->dev, > - "unable to write sel reg 0x%X\n", > - method->sel + 1); > + "unable to write avg reg 0x%X\n", > + method->avg); > goto out; > } > } > @@ -665,10 +660,6 @@ out: > } > EXPORT_SYMBOL_GPL(twl4030_madc_conversion); > > -/* > - * Return channel value > - * Or < 0 on failure. > - */ > int twl4030_get_madc_conversion(int channel_no) > { > struct twl4030_madc_request req; > @@ -703,6 +694,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc, > int chan, int on) > { > int ret; > + int regmask; > u8 regval; > > ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, > @@ -712,10 +704,13 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc, > TWL4030_BCI_BCICTL1); > return ret; > } > + > + regmask = chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN; > if (on) > - regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN; > + regval |= regmask; > else > - regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN; > + regval &= ~regmask; > + > ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, > regval, TWL4030_BCI_BCICTL1); > if (ret) { > @@ -730,7 +725,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc, > /* If you are cleaning up anyway. Lets put this in kernel doc format. > * Function that sets MADC software power on bit to enable MADC > * @madc - pointer to twl4030_madc_data struct > - * @on - Enable or disable MADC software powen on bit. > + * @on - Enable or disable MADC software power on bit. > * returns error if i2c read/write fails else 0 > */ > static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) > diff --git a/include/linux/i2c/twl4030-madc.h b/include/linux/i2c/twl4030-madc.h > index 01f5951..1c0134d 100644 > --- a/include/linux/i2c/twl4030-madc.h > +++ b/include/linux/i2c/twl4030-madc.h > @@ -44,7 +44,7 @@ struct twl4030_madc_conversion_method { > > struct twl4030_madc_request { > unsigned long channels; > - u16 do_avg; > + bool do_avg; > u16 method; > u16 type; > bool active; > -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On 04/03/14 22:05, Sebastian Reichel wrote: > Add ABI documentation for in_*_mean_raw files, which are > already supported and used in the kernel for some time. > > Signed-off-by: Sebastian Reichel <sre@debian.org> Acked-by: Jonathan Cameron <jic23@kernel.org> Thanks for doing this. > --- > Documentation/ABI/testing/sysfs-bus-iio | 8 ++++++++ > 1 file changed, 8 insertions(+) > > diff --git a/Documentation/ABI/testing/sysfs-bus-iio b/Documentation/ABI/testing/sysfs-bus-iio > index 6e02c50..58ba333 100644 > --- a/Documentation/ABI/testing/sysfs-bus-iio > +++ b/Documentation/ABI/testing/sysfs-bus-iio > @@ -210,6 +210,14 @@ Contact: linux-iio@vger.kernel.org > Description: > Scaled humidity measurement in milli percent. > > +What: /sys/bus/iio/devices/iio:deviceX/in_X_mean_raw > +KernelVersion: 3.5 > +Contact: linux-iio@vger.kernel.org > +Description: > + Averaged raw measurement from channel X. The number of values > + used for averaging is device specific. The converting rules for > + normal raw values also applies to the averaged raw values. > + > What: /sys/bus/iio/devices/iio:deviceX/in_accel_offset > What: /sys/bus/iio/devices/iio:deviceX/in_accel_x_offset > What: /sys/bus/iio/devices/iio:deviceX/in_accel_y_offset > -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On 04/03/14 22:05, Sebastian Reichel wrote: > Add iio_read_channel_average_raw to support reading > averaged raw values in consumer drivers. > > Signed-off-by: Sebastian Reichel <sre@debian.org> Acked-by: Jonathan Cameron <jic23@kernel.org> Sometimes these wrappers seem a little bit silly. There might be a case for automating them somewhat via some macros, but for now this is fine. Docs should in theory probably be with the implementation, but that's wrong throughout consumer.h so this isn't the time to change it! Jonathan > --- > drivers/iio/inkern.c | 18 ++++++++++++++++++ > include/linux/iio/consumer.h | 13 +++++++++++++ > 2 files changed, 31 insertions(+) > > diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c > index 0cf5f8e..adeba5a 100644 > --- a/drivers/iio/inkern.c > +++ b/drivers/iio/inkern.c > @@ -443,6 +443,24 @@ err_unlock: > } > EXPORT_SYMBOL_GPL(iio_read_channel_raw); > > +int iio_read_channel_average_raw(struct iio_channel *chan, int *val) > +{ > + int ret; > + > + mutex_lock(&chan->indio_dev->info_exist_lock); > + if (chan->indio_dev->info == NULL) { > + ret = -ENODEV; > + goto err_unlock; > + } > + > + ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW); > +err_unlock: > + mutex_unlock(&chan->indio_dev->info_exist_lock); > + > + return ret; > +} > +EXPORT_SYMBOL_GPL(iio_read_channel_average_raw); > + > static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, > int raw, int *processed, unsigned int scale) > { > diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h > index 2752b1f..651f9a0 100644 > --- a/include/linux/iio/consumer.h > +++ b/include/linux/iio/consumer.h > @@ -123,6 +123,19 @@ int iio_read_channel_raw(struct iio_channel *chan, > int *val); > Ah, my bad habits propogate onwards. The docs should be with the implementation. Oh well, at somepoint I'll clean all remaining cases of this up. > /** > + * iio_read_channel_average_raw() - read from a given channel > + * @chan: The channel being queried. > + * @val: Value read back. > + * > + * Note raw reads from iio channels are in adc counts and hence > + * scale will need to be applied if standard units required. > + * > + * In opposit to the normal iio_read_channel_raw this function > + * returns the average of multiple reads. > + */ > +int iio_read_channel_average_raw(struct iio_channel *chan, int *val); > + > +/** > * iio_read_channel_processed() - read processed value from a given channel > * @chan: The channel being queried. > * @val: Value read back. > -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On Tue 2014-03-04 23:05:43, Sebastian Reichel wrote: > Update twl4030-madc driver to use managed resources. > > Signed-off-by: Sebastian Reichel <sre@debian.org> > Acked-by: Lee Jones <lee.jones@linaro.org> Reviewed-by: Pavel Machek <pavel@ucw.cz>
On Tue 2014-03-04 23:05:44, Sebastian Reichel wrote: > This converts twl4030-madc module to use the Industrial IO ADC > framework and adds device tree support. > > Signed-off-by: Sebastian Reichel <sre@debian.org> Reviewed-by: Pavel Machek <pavel@ucw.cz>
Hi! > Some style fixes in twl4030-madc driver. > > Reported-by: Jonathan Cameron <jic23@kernel.org> > Signed-off-by: Sebastian Reichel <sre@debian.org> > Acked-by: Lee Jones <lee.jones@linaro.org> Reviewed-by: Pavel Machek <pavel@ucw.cz> > @@ -665,10 +660,6 @@ out: > } > EXPORT_SYMBOL_GPL(twl4030_madc_conversion); > > -/* > - * Return channel value > - * Or < 0 on failure. > - */ > int twl4030_get_madc_conversion(int channel_no) > { > struct twl4030_madc_request req; Maybe I'd put the comment on one line, but it seems useful...?
diff --git a/Documentation/devicetree/bindings/mfd/twl4030-madc.txt b/Documentation/devicetree/bindings/mfd/twl4030-madc.txt new file mode 100644 index 0000000..9df66ab --- /dev/null +++ b/Documentation/devicetree/bindings/mfd/twl4030-madc.txt @@ -0,0 +1,18 @@ +Texas Instruments TWL family (twl4030) analog signal conversion monitor module + +Required properties: +- compatible : must be "ti,twl4030-madc" +- interrupts : interrupt number + +Example: +twl: twl@48 { + reg = <0x48>; + interrupts = <7>; /* SYS_NIRQ cascaded to intc */ + interrupt-parent = <&intc>; + + twl_madc: madc { + compatible = "ti,twl4030-madc"; + interrupts = <1>; + }; +}; + diff --git a/drivers/mfd/twl4030-madc.c b/drivers/mfd/twl4030-madc.c index 4c583e4..9a2bbcf 100644 --- a/drivers/mfd/twl4030-madc.c +++ b/drivers/mfd/twl4030-madc.c @@ -695,6 +695,29 @@ static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) return 0; } +#ifdef CONFIG_OF +static struct twl4030_madc_platform_data * + twl4030_madc_of_parse(struct platform_device *pdev) +{ + struct twl4030_madc_platform_data *pdata; + + pdata = devm_kzalloc(&pdev->dev, + sizeof(struct twl4030_madc_platform_data), GFP_KERNEL); + if (!pdata) + return ERR_PTR(-ENOMEM); + + pdata->irq_line = platform_get_irq(pdev, 0); + + return pdata; +} + +static const struct of_device_id twl4030_madc_dt_match_table[] = { + { .compatible = "ti,twl4030-madc" }, + {}, +}; + +#endif + /* * Initialize MADC and request for threaded irq */ @@ -706,8 +729,11 @@ static int twl4030_madc_probe(struct platform_device *pdev) u8 regval; if (!pdata) { - dev_err(&pdev->dev, "platform_data not available\n"); - return -EINVAL; + pdata = twl4030_madc_of_parse(pdev); + if (!pdata) { + dev_err(&pdev->dev, "platform_data not available\n"); + return -EINVAL; + } } madc = kzalloc(sizeof(*madc), GFP_KERNEL); if (!madc) @@ -807,6 +833,7 @@ static struct platform_driver twl4030_madc_driver = { .driver = { .name = "twl4030_madc", .owner = THIS_MODULE, + .of_match_table = of_match_ptr(twl4030_madc_dt_match_table), }, };
Signed-off-by: Marek Belisko <marek@goldelico.com> --- .../devicetree/bindings/mfd/twl4030-madc.txt | 18 +++++++++++++ drivers/mfd/twl4030-madc.c | 31 ++++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 Documentation/devicetree/bindings/mfd/twl4030-madc.txt