Staging: iio/adc: strict_strtoul was used with a long type variable
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / adc / ad7280a.c
index 2d8d650dc2746c7946c2fb552fa77952d00ba634..3fac9b1fc66c3fa0c11a0663baef725f4c5e52a9 100644 (file)
 #include <linux/err.h>
 #include <linux/delay.h>
 #include <linux/interrupt.h>
+#include <linux/module.h>
 
 #include "../iio.h"
 #include "../sysfs.h"
+#include "../events.h"
 
 #include "ad7280a.h"
 
@@ -382,8 +384,8 @@ static ssize_t ad7280_show_balance_sw(struct device *dev,
                                        struct device_attribute *attr,
                                        char *buf)
 {
-       struct iio_dev *dev_info = dev_get_drvdata(dev);
-       struct ad7280_state *st = iio_priv(dev_info);
+       struct iio_dev *indio_dev = dev_get_drvdata(dev);
+       struct ad7280_state *st = iio_priv(indio_dev);
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 
        return sprintf(buf, "%d\n",
@@ -396,8 +398,8 @@ static ssize_t ad7280_store_balance_sw(struct device *dev,
                                         const char *buf,
                                         size_t len)
 {
-       struct iio_dev *dev_info = dev_get_drvdata(dev);
-       struct ad7280_state *st = iio_priv(dev_info);
+       struct iio_dev *indio_dev = dev_get_drvdata(dev);
+       struct ad7280_state *st = iio_priv(indio_dev);
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
        bool readin;
        int ret;
@@ -410,7 +412,7 @@ static ssize_t ad7280_store_balance_sw(struct device *dev,
        devaddr = this_attr->address >> 8;
        ch = this_attr->address & 0xFF;
 
-       mutex_lock(&dev_info->mlock);
+       mutex_lock(&indio_dev->mlock);
        if (readin)
                st->cb_mask[devaddr] |= 1 << (ch + 2);
        else
@@ -418,7 +420,7 @@ static ssize_t ad7280_store_balance_sw(struct device *dev,
 
        ret = ad7280_write(st, devaddr, AD7280A_CELL_BALANCE,
                           0, st->cb_mask[devaddr]);
-       mutex_unlock(&dev_info->mlock);
+       mutex_unlock(&indio_dev->mlock);
 
        return ret ? ret : len;
 }
@@ -427,16 +429,16 @@ static ssize_t ad7280_show_balance_timer(struct device *dev,
                                        struct device_attribute *attr,
                                        char *buf)
 {
-       struct iio_dev *dev_info = dev_get_drvdata(dev);
-       struct ad7280_state *st = iio_priv(dev_info);
+       struct iio_dev *indio_dev = dev_get_drvdata(dev);
+       struct ad7280_state *st = iio_priv(indio_dev);
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
        int ret;
        unsigned msecs;
 
-       mutex_lock(&dev_info->mlock);
+       mutex_lock(&indio_dev->mlock);
        ret = ad7280_read(st, this_attr->address >> 8,
                        this_attr->address & 0xFF);
-       mutex_unlock(&dev_info->mlock);
+       mutex_unlock(&indio_dev->mlock);
 
        if (ret < 0)
                return ret;
@@ -451,13 +453,13 @@ static ssize_t ad7280_store_balance_timer(struct device *dev,
                                         const char *buf,
                                         size_t len)
 {
-       struct iio_dev *dev_info = dev_get_drvdata(dev);
-       struct ad7280_state *st = iio_priv(dev_info);
+       struct iio_dev *indio_dev = dev_get_drvdata(dev);
+       struct ad7280_state *st = iio_priv(indio_dev);
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
-       long val;
+       unsigned long val;
        int ret;
 
-       ret = strict_strtoul(buf, 10, &val);
+       ret = kstrtoul(buf, 10, &val);
        if (ret)
                return ret;
 
@@ -466,11 +468,11 @@ static ssize_t ad7280_store_balance_timer(struct device *dev,
        if (val > 31)
                return -EINVAL;
 
-       mutex_lock(&dev_info->mlock);
+       mutex_lock(&indio_dev->mlock);
        ret = ad7280_write(st, this_attr->address >> 8,
                           this_attr->address & 0xFF,
                           0, (val & 0x1F) << 3);
-       mutex_unlock(&dev_info->mlock);
+       mutex_unlock(&indio_dev->mlock);
 
        return ret ? ret : len;
 }
@@ -486,8 +488,8 @@ static int ad7280_channel_init(struct ad7280_state *st)
 {
        int dev, ch, cnt;
 
-       st->channels = kzalloc(sizeof(*st->channels) *
-                               ((st->slave_num + 1) * 12 + 2), GFP_KERNEL);
+       st->channels = kcalloc((st->slave_num + 1) * 12 + 2,
+                              sizeof(*st->channels), GFP_KERNEL);
        if (st->channels == NULL)
                return -ENOMEM;
 
@@ -506,7 +508,7 @@ static int ad7280_channel_init(struct ad7280_state *st)
                        }
                        st->channels[cnt].indexed = 1;
                        st->channels[cnt].info_mask =
-                               (1 << IIO_CHAN_INFO_SCALE_SHARED);
+                               IIO_CHAN_INFO_SCALE_SHARED_BIT;
                        st->channels[cnt].address =
                                AD7280A_DEVADDR(dev) << 8 | ch;
                        st->channels[cnt].scan_index = cnt;
@@ -522,7 +524,7 @@ static int ad7280_channel_init(struct ad7280_state *st)
        st->channels[cnt].channel2 = dev * 6;
        st->channels[cnt].address = AD7280A_ALL_CELLS;
        st->channels[cnt].indexed = 1;
-       st->channels[cnt].info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED);
+       st->channels[cnt].info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT;
        st->channels[cnt].scan_index = cnt;
        st->channels[cnt].scan_type.sign = 'u';
        st->channels[cnt].scan_type.realbits = 32;
@@ -594,12 +596,12 @@ static ssize_t ad7280_read_channel_config(struct device *dev,
                                        struct device_attribute *attr,
                                        char *buf)
 {
-       struct iio_dev *dev_info = dev_get_drvdata(dev);
-       struct ad7280_state *st = iio_priv(dev_info);
+       struct iio_dev *indio_dev = dev_get_drvdata(dev);
+       struct ad7280_state *st = iio_priv(indio_dev);
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
        unsigned val;
 
-       switch (this_attr->address) {
+       switch ((u32) this_attr->address) {
        case AD7280A_CELL_OVERVOLTAGE:
                val = 1000 + (st->cell_threshhigh * 1568) / 100;
                break;
@@ -624,8 +626,8 @@ static ssize_t ad7280_write_channel_config(struct device *dev,
                                         const char *buf,
                                         size_t len)
 {
-       struct iio_dev *dev_info = dev_get_drvdata(dev);
-       struct ad7280_state *st = iio_priv(dev_info);
+       struct iio_dev *indio_dev = dev_get_drvdata(dev);
+       struct ad7280_state *st = iio_priv(indio_dev);
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 
        long val;
@@ -635,7 +637,7 @@ static ssize_t ad7280_write_channel_config(struct device *dev,
        if (ret)
                return ret;
 
-       switch (this_attr->address) {
+       switch ((u32) this_attr->address) {
        case AD7280A_CELL_OVERVOLTAGE:
        case AD7280A_CELL_UNDERVOLTAGE:
                val = ((val - 1000) * 100) / 1568; /* LSB 15.68mV */
@@ -650,8 +652,8 @@ static ssize_t ad7280_write_channel_config(struct device *dev,
 
        val = clamp(val, 0L, 0xFFL);
 
-       mutex_lock(&dev_info->mlock);
-       switch (this_attr->address) {
+       mutex_lock(&indio_dev->mlock);
+       switch ((u32) this_attr->address) {
        case AD7280A_CELL_OVERVOLTAGE:
                st->cell_threshhigh = val;
                break;
@@ -669,31 +671,31 @@ static ssize_t ad7280_write_channel_config(struct device *dev,
        ret = ad7280_write(st, AD7280A_DEVADDR_MASTER,
                           this_attr->address, 1, val);
 
-       mutex_unlock(&dev_info->mlock);
+       mutex_unlock(&indio_dev->mlock);
 
        return ret ? ret : len;
 }
 
 static irqreturn_t ad7280_event_handler(int irq, void *private)
 {
-       struct iio_dev *dev_info = private;
-       struct ad7280_state *st = iio_priv(dev_info);
+       struct iio_dev *indio_dev = private;
+       struct ad7280_state *st = iio_priv(indio_dev);
        unsigned *channels;
        int i, ret;
 
-       channels = kzalloc(sizeof(*channels) * st->scan_cnt, GFP_KERNEL);
+       channels = kcalloc(st->scan_cnt, sizeof(*channels), GFP_KERNEL);
        if (channels == NULL)
                return IRQ_HANDLED;
 
        ret = ad7280_read_all_channels(st, st->scan_cnt, channels);
        if (ret < 0)
-               return IRQ_HANDLED;
+               goto out;
 
        for (i = 0; i < st->scan_cnt; i++) {
                if (((channels[i] >> 23) & 0xF) <= AD7280A_CELL_VOLTAGE_6) {
                        if (((channels[i] >> 11) & 0xFFF) >=
                                st->cell_threshhigh)
-                               iio_push_event(dev_info,
+                               iio_push_event(indio_dev,
                                        IIO_EVENT_CODE(IIO_VOLTAGE,
                                                       1,
                                                       0,
@@ -703,7 +705,7 @@ static irqreturn_t ad7280_event_handler(int irq, void *private)
                                        iio_get_time_ns());
                        else if (((channels[i] >> 11) & 0xFFF) <=
                                st->cell_threshlow)
-                               iio_push_event(dev_info,
+                               iio_push_event(indio_dev,
                                        IIO_EVENT_CODE(IIO_VOLTAGE,
                                                       1,
                                                       0,
@@ -713,7 +715,7 @@ static irqreturn_t ad7280_event_handler(int irq, void *private)
                                        iio_get_time_ns());
                } else {
                        if (((channels[i] >> 11) & 0xFFF) >= st->aux_threshhigh)
-                               iio_push_event(dev_info,
+                               iio_push_event(indio_dev,
                                        IIO_UNMOD_EVENT_CODE(IIO_TEMP,
                                        0,
                                        IIO_EV_TYPE_THRESH,
@@ -721,7 +723,7 @@ static irqreturn_t ad7280_event_handler(int irq, void *private)
                                        iio_get_time_ns());
                        else if (((channels[i] >> 11) & 0xFFF) <=
                                st->aux_threshlow)
-                               iio_push_event(dev_info,
+                               iio_push_event(indio_dev,
                                        IIO_UNMOD_EVENT_CODE(IIO_TEMP,
                                        0,
                                        IIO_EV_TYPE_THRESH,
@@ -730,32 +732,33 @@ static irqreturn_t ad7280_event_handler(int irq, void *private)
                }
        }
 
+out:
        kfree(channels);
 
        return IRQ_HANDLED;
 }
 
 static IIO_DEVICE_ATTR_NAMED(in_thresh_low_value,
-               in-in_thresh_low_value,
+               in_voltage-voltage_thresh_low_value,
                S_IRUGO | S_IWUSR,
                ad7280_read_channel_config,
                ad7280_write_channel_config,
                AD7280A_CELL_UNDERVOLTAGE);
 
 static IIO_DEVICE_ATTR_NAMED(in_thresh_high_value,
-               in-in_thresh_high_value,
+               in_voltage-voltage_thresh_high_value,
                S_IRUGO | S_IWUSR,
                ad7280_read_channel_config,
                ad7280_write_channel_config,
                AD7280A_CELL_OVERVOLTAGE);
 
-static IIO_DEVICE_ATTR(temp_thresh_low_value,
+static IIO_DEVICE_ATTR(in_temp_thresh_low_value,
                S_IRUGO | S_IWUSR,
                ad7280_read_channel_config,
                ad7280_write_channel_config,
                AD7280A_AUX_ADC_UNDERVOLTAGE);
 
-static IIO_DEVICE_ATTR(temp_thresh_high_value,
+static IIO_DEVICE_ATTR(in_temp_thresh_high_value,
                S_IRUGO | S_IWUSR,
                ad7280_read_channel_config,
                ad7280_write_channel_config,
@@ -765,8 +768,8 @@ static IIO_DEVICE_ATTR(temp_thresh_high_value,
 static struct attribute *ad7280_event_attributes[] = {
        &iio_dev_attr_in_thresh_low_value.dev_attr.attr,
        &iio_dev_attr_in_thresh_high_value.dev_attr.attr,
-       &iio_dev_attr_temp_thresh_low_value.dev_attr.attr,
-       &iio_dev_attr_temp_thresh_high_value.dev_attr.attr,
+       &iio_dev_attr_in_temp_thresh_low_value.dev_attr.attr,
+       &iio_dev_attr_in_temp_thresh_high_value.dev_attr.attr,
        NULL,
 };
 
@@ -774,25 +777,25 @@ static struct attribute_group ad7280_event_attrs_group = {
        .attrs = ad7280_event_attributes,
 };
 
-static int ad7280_read_raw(struct iio_dev *dev_info,
+static int ad7280_read_raw(struct iio_dev *indio_dev,
                           struct iio_chan_spec const *chan,
                           int *val,
                           int *val2,
                           long m)
 {
-       struct ad7280_state *st = iio_priv(dev_info);
+       struct ad7280_state *st = iio_priv(indio_dev);
        unsigned int scale_uv;
        int ret;
 
        switch (m) {
        case 0:
-               mutex_lock(&dev_info->mlock);
+               mutex_lock(&indio_dev->mlock);
                if (chan->address == AD7280A_ALL_CELLS)
                        ret = ad7280_read_all_channels(st, st->scan_cnt, NULL);
                else
                        ret = ad7280_read_channel(st, chan->address >> 8,
                                                  chan->address & 0xFF);
-               mutex_unlock(&dev_info->mlock);
+               mutex_unlock(&indio_dev->mlock);
 
                if (ret < 0)
                        return ret;
@@ -800,7 +803,7 @@ static int ad7280_read_raw(struct iio_dev *dev_info,
                *val = ret;
 
                return IIO_VAL_INT;
-       case (1 << IIO_CHAN_INFO_SCALE_SHARED):
+       case IIO_CHAN_INFO_SCALE:
                if ((chan->address & 0xFF) <= AD7280A_CELL_VOLTAGE_6)
                        scale_uv = (4000 * 1000) >> AD7280A_BITS;
                else
@@ -830,7 +833,7 @@ static int __devinit ad7280_probe(struct spi_device *spi)
 {
        const struct ad7280_platform_data *pdata = spi->dev.platform_data;
        struct ad7280_state *st;
-       int ret, regdone = 0;
+       int ret;
        const unsigned short tACQ_ns[4] = {465, 1010, 1460, 1890};
        const unsigned short nAVG[4] = {1, 2, 4, 8};
        struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
@@ -902,21 +905,20 @@ static int __devinit ad7280_probe(struct spi_device *spi)
        ret = iio_device_register(indio_dev);
        if (ret)
                goto error_free_attr;
-       regdone = 1;
 
        if (spi->irq > 0) {
                ret = ad7280_write(st, AD7280A_DEVADDR_MASTER,
                                   AD7280A_ALERT, 1,
                                   AD7280A_ALERT_RELAY_SIG_CHAIN_DOWN);
                if (ret)
-                       goto error_free_attr;
+                       goto error_unregister;
 
                ret = ad7280_write(st, AD7280A_DEVADDR(st->slave_num),
                                   AD7280A_ALERT, 0,
                                   AD7280A_ALERT_GEN_STATIC_HIGH |
                                   (pdata->chain_last_alert_ignore & 0xF));
                if (ret)
-                       goto error_free_attr;
+                       goto error_unregister;
 
                ret = request_threaded_irq(spi->irq,
                                           NULL,
@@ -926,10 +928,12 @@ static int __devinit ad7280_probe(struct spi_device *spi)
                                           indio_dev->name,
                                           indio_dev);
                if (ret)
-                       goto error_free_attr;
+                       goto error_unregister;
        }
 
        return 0;
+error_unregister:
+       iio_device_unregister(indio_dev);
 
 error_free_attr:
        kfree(st->iio_attr);
@@ -938,10 +942,7 @@ error_free_channels:
        kfree(st->channels);
 
 error_free_device:
-       if (regdone)
-               iio_device_unregister(indio_dev);
-       else
-               iio_free_device(indio_dev);
+       iio_free_device(indio_dev);
 
        return ret;
 }
@@ -953,13 +954,14 @@ static int __devexit ad7280_remove(struct spi_device *spi)
 
        if (spi->irq > 0)
                free_irq(spi->irq, indio_dev);
+       iio_device_unregister(indio_dev);
 
        ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CONTROL_HB, 1,
                        AD7280A_CTRL_HB_PWRDN_SW | st->ctrl_hb);
 
        kfree(st->channels);
        kfree(st->iio_attr);
-       iio_device_unregister(indio_dev);
+       iio_free_device(indio_dev);
 
        return 0;
 }
@@ -968,6 +970,7 @@ static const struct spi_device_id ad7280_id[] = {
        {"ad7280a", 0},
        {}
 };
+MODULE_DEVICE_TABLE(spi, ad7280_id);
 
 static struct spi_driver ad7280_driver = {
        .driver = {