--- zzzz-none-000/linux-3.10.107/drivers/iio/dac/ad5504.c 2017-06-27 09:49:32.000000000 +0000 +++ scorpion-7490-727/linux-3.10.107/drivers/iio/dac/ad5504.c 2021-02-04 17:41:59.000000000 +0000 @@ -15,17 +15,16 @@ #include #include #include +#include #include #include #include #include -#define AD5505_BITS 12 -#define AD5504_RES_MASK ((1 << (AD5505_BITS)) - 1) - -#define AD5504_CMD_READ (1 << 15) -#define AD5504_CMD_WRITE (0 << 15) +#define AD5504_RES_MASK GENMASK(11, 0) +#define AD5504_CMD_READ BIT(15) +#define AD5504_CMD_WRITE 0 #define AD5504_ADDR(addr) ((addr) << 12) /* Registers */ @@ -42,19 +41,21 @@ /** * struct ad5446_state - driver instance specific data - * @us: spi_device + * @spi: spi_device * @reg: supply regulator * @vref_mv: actual reference voltage used * @pwr_down_mask power down mask * @pwr_down_mode current power down mode + * @data: transfer buffer */ - struct ad5504_state { struct spi_device *spi; struct regulator *reg; unsigned short vref_mv; unsigned pwr_down_mask; unsigned pwr_down_mode; + + __be16 data[2] ____cacheline_aligned; }; /** @@ -66,31 +67,29 @@ ID_AD5501, }; -static int ad5504_spi_write(struct spi_device *spi, u8 addr, u16 val) +static int ad5504_spi_write(struct ad5504_state *st, u8 addr, u16 val) { - u16 tmp = cpu_to_be16(AD5504_CMD_WRITE | - AD5504_ADDR(addr) | + st->data[0] = cpu_to_be16(AD5504_CMD_WRITE | AD5504_ADDR(addr) | (val & AD5504_RES_MASK)); - return spi_write(spi, (u8 *)&tmp, 2); + return spi_write(st->spi, &st->data[0], 2); } -static int ad5504_spi_read(struct spi_device *spi, u8 addr) +static int ad5504_spi_read(struct ad5504_state *st, u8 addr) { - u16 tmp = cpu_to_be16(AD5504_CMD_READ | AD5504_ADDR(addr)); - u16 val; int ret; - struct spi_transfer t = { - .tx_buf = &tmp, - .rx_buf = &val, - .len = 2, - }; - ret = spi_sync_transfer(spi, &t, 1); + struct spi_transfer t = { + .tx_buf = &st->data[0], + .rx_buf = &st->data[1], + .len = 2, + }; + st->data[0] = cpu_to_be16(AD5504_CMD_READ | AD5504_ADDR(addr)); + ret = spi_sync_transfer(st->spi, &t, 1); if (ret < 0) return ret; - return be16_to_cpu(val) & AD5504_RES_MASK; + return be16_to_cpu(st->data[1]) & AD5504_RES_MASK; } static int ad5504_read_raw(struct iio_dev *indio_dev, @@ -100,12 +99,11 @@ long m) { struct ad5504_state *st = iio_priv(indio_dev); - unsigned long scale_uv; int ret; switch (m) { case IIO_CHAN_INFO_RAW: - ret = ad5504_spi_read(st->spi, chan->address); + ret = ad5504_spi_read(st, chan->address); if (ret < 0) return ret; @@ -113,11 +111,9 @@ return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: - scale_uv = (st->vref_mv * 1000) >> chan->scan_type.realbits; - *val = scale_uv / 1000; - *val2 = (scale_uv % 1000) * 1000; - return IIO_VAL_INT_PLUS_MICRO; - + *val = st->vref_mv; + *val2 = chan->scan_type.realbits; + return IIO_VAL_FRACTIONAL_LOG2; } return -EINVAL; } @@ -129,19 +125,16 @@ long mask) { struct ad5504_state *st = iio_priv(indio_dev); - int ret; switch (mask) { case IIO_CHAN_INFO_RAW: if (val >= (1 << chan->scan_type.realbits) || val < 0) return -EINVAL; - return ad5504_spi_write(st->spi, chan->address, val); + return ad5504_spi_write(st, chan->address, val); default: - ret = -EINVAL; + return -EINVAL; } - - return -EINVAL; } static const char * const ad5504_powerdown_modes[] = { @@ -200,12 +193,12 @@ else st->pwr_down_mask &= ~(1 << chan->channel); - ret = ad5504_spi_write(st->spi, AD5504_ADDR_CTRL, + ret = ad5504_spi_write(st, AD5504_ADDR_CTRL, AD5504_DAC_PWRDWN_MODE(st->pwr_down_mode) | AD5504_DAC_PWR(st->pwr_down_mask)); /* writes to the CTRL register must be followed by a NOOP */ - ad5504_spi_write(st->spi, AD5504_ADDR_NOOP, 0); + ad5504_spi_write(st, AD5504_ADDR_NOOP, 0); return ret ? ret : len; } @@ -221,7 +214,6 @@ static struct attribute_group ad5504_ev_attribute_group = { .attrs = ad5504_ev_attributes, - .name = "events", }; static irqreturn_t ad5504_event_handler(int irq, void *private) @@ -248,8 +240,10 @@ .name = "powerdown", .read = ad5504_read_dac_powerdown, .write = ad5504_write_dac_powerdown, + .shared = IIO_SEPARATE, }, - IIO_ENUM("powerdown_mode", true, &ad5504_powerdown_mode_enum), + IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE, + &ad5504_powerdown_mode_enum), IIO_ENUM_AVAILABLE("powerdown_mode", &ad5504_powerdown_mode_enum), { }, }; @@ -262,7 +256,11 @@ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ .address = AD5504_ADDR_DAC(_chan), \ - .scan_type = IIO_ST('u', 12, 16, 0), \ + .scan_type = { \ + .sign = 'u', \ + .realbits = 12, \ + .storagebits = 16, \ + }, \ .ext_info = ad5504_ext_info, \ } @@ -281,16 +279,14 @@ struct regulator *reg; int ret, voltage_uv = 0; - indio_dev = iio_device_alloc(sizeof(*st)); - if (indio_dev == NULL) { - ret = -ENOMEM; - goto error_ret; - } - reg = regulator_get(&spi->dev, "vcc"); + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); + if (!indio_dev) + return -ENOMEM; + reg = devm_regulator_get(&spi->dev, "vcc"); if (!IS_ERR(reg)) { ret = regulator_enable(reg); if (ret) - goto error_put_reg; + return ret; ret = regulator_get_voltage(reg); if (ret < 0) @@ -321,7 +317,7 @@ indio_dev->modes = INDIO_DIRECT_MODE; if (spi->irq) { - ret = request_threaded_irq(spi->irq, + ret = devm_request_threaded_irq(&spi->dev, spi->irq, NULL, &ad5504_event_handler, IRQF_TRIGGER_FALLING | IRQF_ONESHOT, @@ -333,22 +329,14 @@ ret = iio_device_register(indio_dev); if (ret) - goto error_free_irq; + goto error_disable_reg; return 0; -error_free_irq: - if (spi->irq) - free_irq(spi->irq, indio_dev); error_disable_reg: if (!IS_ERR(reg)) regulator_disable(reg); -error_put_reg: - if (!IS_ERR(reg)) - regulator_put(reg); - iio_device_free(indio_dev); -error_ret: return ret; } @@ -358,14 +346,9 @@ struct ad5504_state *st = iio_priv(indio_dev); iio_device_unregister(indio_dev); - if (spi->irq) - free_irq(spi->irq, indio_dev); - if (!IS_ERR(st->reg)) { + if (!IS_ERR(st->reg)) regulator_disable(st->reg); - regulator_put(st->reg); - } - iio_device_free(indio_dev); return 0; } @@ -380,7 +363,6 @@ static struct spi_driver ad5504_driver = { .driver = { .name = "ad5504", - .owner = THIS_MODULE, }, .probe = ad5504_probe, .remove = ad5504_remove,