OSDN Git Service

staging: iio: adc: ad7280a: use devm_* APIs
[uclinux-h8/linux.git] / drivers / staging / iio / adc / ad7280a.c
index cbeb524..0bb9ab1 100644 (file)
@@ -6,6 +6,7 @@
  * Licensed under the GPL-2.
  */
 
+#include <linux/crc8.h>
 #include <linux/device.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
@@ -121,8 +122,6 @@ static unsigned int ad7280a_devaddr(unsigned int addr)
  * P(x) = x^8 + x^5 + x^3 + x^2 + x^1 + x^0 = 0b100101111 => 0x2F
  */
 #define POLYNOM                0x2F
-#define POLYNOM_ORDER  8
-#define HIGHBIT                (1 << (POLYNOM_ORDER - 1))
 
 struct ad7280_state {
        struct spi_device               *spi;
@@ -131,7 +130,7 @@ struct ad7280_state {
        int                             slave_num;
        int                             scan_cnt;
        int                             readback_delay_us;
-       unsigned char                   crc_tab[256];
+       unsigned char                   crc_tab[CRC8_TABLE_SIZE];
        unsigned char                   ctrl_hb;
        unsigned char                   ctrl_lb;
        unsigned char                   cell_threshhigh;
@@ -144,23 +143,6 @@ struct ad7280_state {
        __be32                          buf[2] ____cacheline_aligned;
 };
 
-static void ad7280_crc8_build_table(unsigned char *crc_tab)
-{
-       unsigned char bit, crc;
-       int cnt, i;
-
-       for (cnt = 0; cnt < 256; cnt++) {
-               crc = cnt;
-               for (i = 0; i < 8; i++) {
-                       bit = crc & HIGHBIT;
-                       crc <<= 1;
-                       if (bit)
-                               crc ^= POLYNOM;
-               }
-               crc_tab[cnt] = crc;
-       }
-}
-
 static unsigned char ad7280_calc_crc8(unsigned char *crc_tab, unsigned int val)
 {
        unsigned char crc;
@@ -348,6 +330,14 @@ static int ad7280_read_all_channels(struct ad7280_state *st, unsigned int cnt,
        return sum;
 }
 
+static void ad7280_sw_power_down(void *data)
+{
+       struct ad7280_state *st = data;
+
+       ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CONTROL_HB, 1,
+                    AD7280A_CTRL_HB_PWRDN_SW | st->ctrl_hb);
+}
+
 static int ad7280_chain_setup(struct ad7280_state *st)
 {
        unsigned int val, n;
@@ -368,29 +358,38 @@ static int ad7280_chain_setup(struct ad7280_state *st)
                           AD7280A_CTRL_LB_MUST_SET |
                           st->ctrl_lb);
        if (ret)
-               return ret;
+               goto error_power_down;
 
        ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_READ, 1,
                           AD7280A_CONTROL_LB << 2);
        if (ret)
-               return ret;
+               goto error_power_down;
 
        for (n = 0; n <= AD7280A_MAX_CHAIN; n++) {
                ret = __ad7280_read32(st, &val);
                if (ret)
-                       return ret;
+                       goto error_power_down;
 
                if (val == 0)
                        return n - 1;
 
-               if (ad7280_check_crc(st, val))
-                       return -EIO;
+               if (ad7280_check_crc(st, val)) {
+                       ret = -EIO;
+                       goto error_power_down;
+               }
 
-               if (n != ad7280a_devaddr(val >> 27))
-                       return -EIO;
+               if (n != ad7280a_devaddr(val >> 27)) {
+                       ret = -EIO;
+                       goto error_power_down;
+               }
        }
+       ret = -EFAULT;
 
-       return -EFAULT;
+error_power_down:
+       ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CONTROL_HB, 1,
+                    AD7280A_CTRL_HB_PWRDN_SW | st->ctrl_hb);
+
+       return ret;
 }
 
 static ssize_t ad7280_show_balance_sw(struct device *dev,
@@ -501,8 +500,8 @@ static int ad7280_channel_init(struct ad7280_state *st)
 {
        int dev, ch, cnt;
 
-       st->channels = kcalloc((st->slave_num + 1) * 12 + 2,
-                              sizeof(*st->channels), GFP_KERNEL);
+       st->channels = devm_kcalloc(&st->spi->dev, (st->slave_num + 1) * 12 + 2,
+                                   sizeof(*st->channels), GFP_KERNEL);
        if (!st->channels)
                return -ENOMEM;
 
@@ -561,16 +560,18 @@ static int ad7280_channel_init(struct ad7280_state *st)
 static int ad7280_attr_init(struct ad7280_state *st)
 {
        int dev, ch, cnt;
+       unsigned int index;
 
-       st->iio_attr = kcalloc(2, sizeof(*st->iio_attr) *
-                              (st->slave_num + 1) * AD7280A_CELLS_PER_DEV,
-                              GFP_KERNEL);
+       st->iio_attr = devm_kcalloc(&st->spi->dev, 2, sizeof(*st->iio_attr) *
+                                   (st->slave_num + 1) * AD7280A_CELLS_PER_DEV,
+                                   GFP_KERNEL);
        if (!st->iio_attr)
                return -ENOMEM;
 
        for (dev = 0, cnt = 0; dev <= st->slave_num; dev++)
                for (ch = AD7280A_CELL_VOLTAGE_1; ch <= AD7280A_CELL_VOLTAGE_6;
                        ch++, cnt++) {
+                       index = dev * AD7280A_CELLS_PER_DEV + ch;
                        st->iio_attr[cnt].address =
                                ad7280a_devaddr(dev) << 8 | ch;
                        st->iio_attr[cnt].dev_attr.attr.mode =
@@ -580,10 +581,9 @@ static int ad7280_attr_init(struct ad7280_state *st)
                        st->iio_attr[cnt].dev_attr.store =
                                ad7280_store_balance_sw;
                        st->iio_attr[cnt].dev_attr.attr.name =
-                               kasprintf(GFP_KERNEL,
-                                         "in%d-in%d_balance_switch_en",
-                                         dev * AD7280A_CELLS_PER_DEV + ch,
-                                         dev * AD7280A_CELLS_PER_DEV + ch + 1);
+                               devm_kasprintf(&st->spi->dev, GFP_KERNEL,
+                                              "in%d-in%d_balance_switch_en",
+                                              index, index + 1);
                        ad7280_attributes[cnt] =
                                &st->iio_attr[cnt].dev_attr.attr;
                        cnt++;
@@ -597,10 +597,9 @@ static int ad7280_attr_init(struct ad7280_state *st)
                        st->iio_attr[cnt].dev_attr.store =
                                ad7280_store_balance_timer;
                        st->iio_attr[cnt].dev_attr.attr.name =
-                               kasprintf(GFP_KERNEL,
-                                         "in%d-in%d_balance_timer",
-                                         dev * AD7280A_CELLS_PER_DEV + ch,
-                                         dev * AD7280A_CELLS_PER_DEV + ch + 1);
+                               devm_kasprintf(&st->spi->dev, GFP_KERNEL,
+                                              "in%d-in%d_balance_timer",
+                                              index, index + 1);
                        ad7280_attributes[cnt] =
                                &st->iio_attr[cnt].dev_attr.attr;
                }
@@ -619,7 +618,7 @@ static ssize_t ad7280_read_channel_config(struct device *dev,
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
        unsigned int val;
 
-       switch ((u32)this_attr->address) {
+       switch (this_attr->address) {
        case AD7280A_CELL_OVERVOLTAGE:
                val = 1000 + (st->cell_threshhigh * 1568) / 100;
                break;
@@ -655,7 +654,7 @@ static ssize_t ad7280_write_channel_config(struct device *dev,
        if (ret)
                return ret;
 
-       switch ((u32)this_attr->address) {
+       switch (this_attr->address) {
        case AD7280A_CELL_OVERVOLTAGE:
        case AD7280A_CELL_UNDERVOLTAGE:
                val = ((val - 1000) * 100) / 1568; /* LSB 15.68mV */
@@ -671,7 +670,7 @@ static ssize_t ad7280_write_channel_config(struct device *dev,
        val = clamp(val, 0L, 0xFFL);
 
        mutex_lock(&st->lock);
-       switch ((u32)this_attr->address) {
+       switch (this_attr->address) {
        case AD7280A_CELL_OVERVOLTAGE:
                st->cell_threshhigh = val;
                break;
@@ -866,7 +865,7 @@ static int ad7280_probe(struct spi_device *spi)
        if (!pdata)
                pdata = &ad7793_default_pdata;
 
-       ad7280_crc8_build_table(st->crc_tab);
+       crc8_populate_msb(st->crc_tab, POLYNOM);
 
        st->spi->max_speed_hz = AD7280A_MAX_SPI_CLK_HZ;
        st->spi->mode = SPI_MODE_1;
@@ -881,6 +880,10 @@ static int ad7280_probe(struct spi_device *spi)
        if (ret < 0)
                return ret;
 
+       ret = devm_add_action_or_reset(&spi->dev, ad7280_sw_power_down, st);
+       if (ret)
+               return ret;
+
        st->slave_num = ret;
        st->scan_cnt = (st->slave_num + 1) * AD7280A_NUM_CH;
        st->cell_threshhigh = 0xFF;
@@ -918,66 +921,38 @@ static int ad7280_probe(struct spi_device *spi)
 
        ret = ad7280_attr_init(st);
        if (ret < 0)
-               goto error_free_channels;
+               return ret;
 
-       ret = iio_device_register(indio_dev);
+       ret = devm_iio_device_register(&spi->dev, indio_dev);
        if (ret)
-               goto error_free_attr;
+               return ret;
 
        if (spi->irq > 0) {
                ret = ad7280_write(st, AD7280A_DEVADDR_MASTER,
                                   AD7280A_ALERT, 1,
                                   AD7280A_ALERT_RELAY_SIG_CHAIN_DOWN);
                if (ret)
-                       goto error_unregister;
+                       return ret;
 
                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_unregister;
-
-               ret = request_threaded_irq(spi->irq,
-                                          NULL,
-                                          ad7280_event_handler,
-                                          IRQF_TRIGGER_FALLING |
-                                          IRQF_ONESHOT,
-                                          indio_dev->name,
-                                          indio_dev);
+                       return ret;
+
+               ret = devm_request_threaded_irq(&spi->dev, spi->irq,
+                                               NULL,
+                                               ad7280_event_handler,
+                                               IRQF_TRIGGER_FALLING |
+                                               IRQF_ONESHOT,
+                                               indio_dev->name,
+                                               indio_dev);
                if (ret)
-                       goto error_unregister;
+                       return ret;
        }
 
        return 0;
-error_unregister:
-       iio_device_unregister(indio_dev);
-
-error_free_attr:
-       kfree(st->iio_attr);
-
-error_free_channels:
-       kfree(st->channels);
-
-       return ret;
-}
-
-static int ad7280_remove(struct spi_device *spi)
-{
-       struct iio_dev *indio_dev = spi_get_drvdata(spi);
-       struct ad7280_state *st = iio_priv(indio_dev);
-
-       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);
-
-       return 0;
 }
 
 static const struct spi_device_id ad7280_id[] = {
@@ -991,7 +966,6 @@ static struct spi_driver ad7280_driver = {
                .name   = "ad7280",
        },
        .probe          = ad7280_probe,
-       .remove         = ad7280_remove,
        .id_table       = ad7280_id,
 };
 module_spi_driver(ad7280_driver);