diff options
Diffstat (limited to 'drivers/staging/iio/adc')
-rw-r--r-- | drivers/staging/iio/adc/Kconfig | 8 | ||||
-rw-r--r-- | drivers/staging/iio/adc/ad7192.c | 15 | ||||
-rw-r--r-- | drivers/staging/iio/adc/ad7280a.c | 21 | ||||
-rw-r--r-- | drivers/staging/iio/adc/ad7291.c | 246 | ||||
-rw-r--r-- | drivers/staging/iio/adc/ad7606_core.c | 35 | ||||
-rw-r--r-- | drivers/staging/iio/adc/ad7606_ring.c | 8 | ||||
-rw-r--r-- | drivers/staging/iio/adc/ad7780.c | 37 | ||||
-rw-r--r-- | drivers/staging/iio/adc/ad7816.c | 59 | ||||
-rw-r--r-- | drivers/staging/iio/adc/ad799x.h | 16 | ||||
-rw-r--r-- | drivers/staging/iio/adc/ad799x_core.c | 297 | ||||
-rw-r--r-- | drivers/staging/iio/adc/ad799x_ring.c | 10 | ||||
-rw-r--r-- | drivers/staging/iio/adc/lpc32xx_adc.c | 51 | ||||
-rw-r--r-- | drivers/staging/iio/adc/mxs-lradc.c | 996 | ||||
-rw-r--r-- | drivers/staging/iio/adc/spear_adc.c | 12 |
14 files changed, 969 insertions, 842 deletions
diff --git a/drivers/staging/iio/adc/Kconfig b/drivers/staging/iio/adc/Kconfig index cabc7a3..e3d6430 100644 --- a/drivers/staging/iio/adc/Kconfig +++ b/drivers/staging/iio/adc/Kconfig @@ -102,7 +102,7 @@ config AD7280 config LPC32XX_ADC tristate "NXP LPC32XX ADC" - depends on ARCH_LPC32XX + depends on ARCH_LPC32XX || COMPILE_TEST help Say yes here to build support for the integrated ADC inside the LPC32XX SoC. Note that this feature uses the same hardware as the @@ -113,7 +113,9 @@ config LPC32XX_ADC config MXS_LRADC tristate "Freescale i.MX23/i.MX28 LRADC" - depends on ARCH_MXS + depends on ARCH_MXS || COMPILE_TEST + depends on INPUT + select STMP_DEVICE select IIO_BUFFER select IIO_TRIGGERED_BUFFER help @@ -125,7 +127,7 @@ config MXS_LRADC config SPEAR_ADC tristate "ST SPEAr ADC" - depends on PLAT_SPEAR + depends on PLAT_SPEAR || COMPILE_TEST help Say yes here to build support for the integrated ADC inside the ST SPEAr SoC. Provides direct access via sysfs. diff --git a/drivers/staging/iio/adc/ad7192.c b/drivers/staging/iio/adc/ad7192.c index 3283e28..83bb44b 100644 --- a/drivers/staging/iio/adc/ad7192.c +++ b/drivers/staging/iio/adc/ad7192.c @@ -623,17 +623,17 @@ static int ad7192_probe(struct spi_device *spi) return -ENODEV; } - indio_dev = iio_device_alloc(sizeof(*st)); + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); if (indio_dev == NULL) return -ENOMEM; st = iio_priv(indio_dev); - st->reg = regulator_get(&spi->dev, "vcc"); + st->reg = devm_regulator_get(&spi->dev, "vcc"); if (!IS_ERR(st->reg)) { ret = regulator_enable(st->reg); if (ret) - goto error_put_reg; + return ret; voltage_uv = regulator_get_voltage(st->reg); } @@ -677,11 +677,6 @@ error_remove_trigger: error_disable_reg: if (!IS_ERR(st->reg)) regulator_disable(st->reg); -error_put_reg: - if (!IS_ERR(st->reg)) - regulator_put(st->reg); - - iio_device_free(indio_dev); return ret; } @@ -694,10 +689,8 @@ static int ad7192_remove(struct spi_device *spi) iio_device_unregister(indio_dev); ad_sd_cleanup_buffer_and_trigger(indio_dev); - if (!IS_ERR(st->reg)) { + if (!IS_ERR(st->reg)) regulator_disable(st->reg); - regulator_put(st->reg); - } return 0; } diff --git a/drivers/staging/iio/adc/ad7280a.c b/drivers/staging/iio/adc/ad7280a.c index c19618b..8209fa5 100644 --- a/drivers/staging/iio/adc/ad7280a.c +++ b/drivers/staging/iio/adc/ad7280a.c @@ -783,7 +783,6 @@ static int ad7280_read_raw(struct iio_dev *indio_dev, long m) { struct ad7280_state *st = iio_priv(indio_dev); - unsigned int scale_uv; int ret; switch (m) { @@ -804,13 +803,12 @@ static int ad7280_read_raw(struct iio_dev *indio_dev, return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: if ((chan->address & 0xFF) <= AD7280A_CELL_VOLTAGE_6) - scale_uv = (4000 * 1000) >> AD7280A_BITS; + *val = 4000; else - scale_uv = (5000 * 1000) >> AD7280A_BITS; + *val = 5000; - *val = scale_uv / 1000; - *val2 = (scale_uv % 1000) * 1000; - return IIO_VAL_INT_PLUS_MICRO; + *val2 = AD7280A_BITS; + return IIO_VAL_FRACTIONAL_LOG2; } return -EINVAL; } @@ -835,8 +833,9 @@ static int ad7280_probe(struct spi_device *spi) 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_device_alloc(sizeof(*st)); + struct iio_dev *indio_dev; + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); if (indio_dev == NULL) return -ENOMEM; @@ -860,7 +859,7 @@ static int ad7280_probe(struct spi_device *spi) ret = ad7280_chain_setup(st); if (ret < 0) - goto error_free_device; + return ret; st->slave_num = ret; st->scan_cnt = (st->slave_num + 1) * AD7280A_NUM_CH; @@ -891,7 +890,7 @@ static int ad7280_probe(struct spi_device *spi) ret = ad7280_channel_init(st); if (ret < 0) - goto error_free_device; + return ret; indio_dev->num_channels = ret; indio_dev->channels = st->channels; @@ -940,9 +939,6 @@ error_free_attr: error_free_channels: kfree(st->channels); -error_free_device: - iio_device_free(indio_dev); - return ret; } @@ -960,7 +956,6 @@ static int ad7280_remove(struct spi_device *spi) kfree(st->channels); kfree(st->iio_attr); - iio_device_free(indio_dev); return 0; } diff --git a/drivers/staging/iio/adc/ad7291.c b/drivers/staging/iio/adc/ad7291.c index a2e61c2..d13f8ae 100644 --- a/drivers/staging/iio/adc/ad7291.c +++ b/drivers/staging/iio/adc/ad7291.c @@ -164,97 +164,14 @@ static irqreturn_t ad7291_event_handler(int irq, void *private) return IRQ_HANDLED; } -static inline ssize_t ad7291_show_hyst(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct iio_dev *indio_dev = dev_to_iio_dev(dev); - struct ad7291_chip_info *chip = iio_priv(indio_dev); - struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); - u16 data; - int ret; - - ret = ad7291_i2c_read(chip, this_attr->address, &data); - if (ret < 0) - return ret; - - return sprintf(buf, "%d\n", data & AD7291_VALUE_MASK); -} - -static inline ssize_t ad7291_set_hyst(struct device *dev, - struct device_attribute *attr, - const char *buf, - size_t len) -{ - struct iio_dev *indio_dev = dev_to_iio_dev(dev); - struct ad7291_chip_info *chip = iio_priv(indio_dev); - struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); - u16 data; - int ret; - - ret = kstrtou16(buf, 10, &data); - - if (ret < 0) - return ret; - if (data > AD7291_VALUE_MASK) - return -EINVAL; - - ret = ad7291_i2c_write(chip, this_attr->address, data); - if (ret < 0) - return ret; - - return len; -} - -static IIO_DEVICE_ATTR(in_temp0_thresh_both_hyst_raw, - S_IRUGO | S_IWUSR, - ad7291_show_hyst, ad7291_set_hyst, - AD7291_HYST(8)); -static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw, - S_IRUGO | S_IWUSR, - ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(0)); -static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw, - S_IRUGO | S_IWUSR, - ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(1)); -static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw, - S_IRUGO | S_IWUSR, - ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(2)); -static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw, - S_IRUGO | S_IWUSR, - ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(3)); -static IIO_DEVICE_ATTR(in_voltage4_thresh_both_hyst_raw, - S_IRUGO | S_IWUSR, - ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(4)); -static IIO_DEVICE_ATTR(in_voltage5_thresh_both_hyst_raw, - S_IRUGO | S_IWUSR, - ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(5)); -static IIO_DEVICE_ATTR(in_voltage6_thresh_both_hyst_raw, - S_IRUGO | S_IWUSR, - ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(6)); -static IIO_DEVICE_ATTR(in_voltage7_thresh_both_hyst_raw, - S_IRUGO | S_IWUSR, - ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(7)); - -static struct attribute *ad7291_event_attributes[] = { - &iio_dev_attr_in_temp0_thresh_both_hyst_raw.dev_attr.attr, - &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr, - &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr, - &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr, - &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr, - &iio_dev_attr_in_voltage4_thresh_both_hyst_raw.dev_attr.attr, - &iio_dev_attr_in_voltage5_thresh_both_hyst_raw.dev_attr.attr, - &iio_dev_attr_in_voltage6_thresh_both_hyst_raw.dev_attr.attr, - &iio_dev_attr_in_voltage7_thresh_both_hyst_raw.dev_attr.attr, - NULL, -}; - -static unsigned int ad7291_threshold_reg(u64 event_code) +static unsigned int ad7291_threshold_reg(const struct iio_chan_spec *chan, + enum iio_event_direction dir, enum iio_event_info info) { unsigned int offset; - switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) { + switch (chan->type) { case IIO_VOLTAGE: - offset = IIO_EVENT_CODE_EXTRACT_CHAN(event_code); + offset = chan->channel; break; case IIO_TEMP: offset = 8; @@ -263,69 +180,78 @@ static unsigned int ad7291_threshold_reg(u64 event_code) return 0; } - if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_FALLING) - return AD7291_DATA_LOW(offset); - else - return AD7291_DATA_HIGH(offset); + switch (info) { + case IIO_EV_INFO_VALUE: + if (dir == IIO_EV_DIR_FALLING) + return AD7291_DATA_HIGH(offset); + else + return AD7291_DATA_LOW(offset); + case IIO_EV_INFO_HYSTERESIS: + return AD7291_HYST(offset); + default: + break; + } + return 0; } static int ad7291_read_event_value(struct iio_dev *indio_dev, - u64 event_code, - int *val) + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, + enum iio_event_info info, + int *val, int *val2) { struct ad7291_chip_info *chip = iio_priv(indio_dev); int ret; u16 uval; - ret = ad7291_i2c_read(chip, ad7291_threshold_reg(event_code), &uval); + ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info), + &uval); if (ret < 0) return ret; - switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) { - case IIO_VOLTAGE: + if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) *val = uval & AD7291_VALUE_MASK; - return 0; - case IIO_TEMP: + + else *val = sign_extend32(uval, 11); - return 0; - default: - return -EINVAL; - }; + + return IIO_VAL_INT; } static int ad7291_write_event_value(struct iio_dev *indio_dev, - u64 event_code, - int val) + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, + enum iio_event_info info, + int val, int val2) { struct ad7291_chip_info *chip = iio_priv(indio_dev); - switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) { - case IIO_VOLTAGE: + if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) { if (val > AD7291_VALUE_MASK || val < 0) return -EINVAL; - break; - case IIO_TEMP: + } else { if (val > 2047 || val < -2048) return -EINVAL; - break; - default: - return -EINVAL; } - return ad7291_i2c_write(chip, ad7291_threshold_reg(event_code), val); + return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info), + val); } static int ad7291_read_event_config(struct iio_dev *indio_dev, - u64 event_code) + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir) { struct ad7291_chip_info *chip = iio_priv(indio_dev); /* To be enabled the channel must simply be on. If any are enabled we are in continuous sampling mode */ - switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) { + switch (chan->type) { case IIO_VOLTAGE: - if (chip->c_mask & - (1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN(event_code)))) + if (chip->c_mask & (1 << (15 - chan->channel))) return 1; else return 0; @@ -339,11 +265,14 @@ static int ad7291_read_event_config(struct iio_dev *indio_dev, } static int ad7291_write_event_config(struct iio_dev *indio_dev, - u64 event_code, + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, int state) { int ret = 0; struct ad7291_chip_info *chip = iio_priv(indio_dev); + unsigned int mask; u16 regval; mutex_lock(&chip->state_lock); @@ -354,16 +283,14 @@ static int ad7291_write_event_config(struct iio_dev *indio_dev, * Possible to disable temp as well but that makes single read tricky. */ - switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) { + mask = BIT(15 - chan->channel); + + switch (chan->type) { case IIO_VOLTAGE: - if ((!state) && (chip->c_mask & (1 << (15 - - IIO_EVENT_CODE_EXTRACT_CHAN(event_code))))) - chip->c_mask &= ~(1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN - (event_code))); - else if (state && (!(chip->c_mask & (1 << (15 - - IIO_EVENT_CODE_EXTRACT_CHAN(event_code)))))) - chip->c_mask |= (1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN - (event_code))); + if ((!state) && (chip->c_mask & mask)) + chip->c_mask &= ~mask; + else if (state && (!(chip->c_mask & mask))) + chip->c_mask |= mask; else break; @@ -473,6 +400,24 @@ static int ad7291_read_raw(struct iio_dev *indio_dev, } } +static const struct iio_event_spec ad7291_events[] = { + { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_RISING, + .mask_separate = BIT(IIO_EV_INFO_VALUE) | + BIT(IIO_EV_INFO_ENABLE), + }, { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_FALLING, + .mask_separate = BIT(IIO_EV_INFO_VALUE) | + BIT(IIO_EV_INFO_ENABLE), + }, { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_EITHER, + .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS), + }, +}; + #define AD7291_VOLTAGE_CHAN(_chan) \ { \ .type = IIO_VOLTAGE, \ @@ -480,8 +425,8 @@ static int ad7291_read_raw(struct iio_dev *indio_dev, .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ .indexed = 1, \ .channel = _chan, \ - .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\ - IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING) \ + .event_spec = ad7291_events, \ + .num_event_specs = ARRAY_SIZE(ad7291_events), \ } static const struct iio_chan_spec ad7291_channels[] = { @@ -500,23 +445,17 @@ static const struct iio_chan_spec ad7291_channels[] = { BIT(IIO_CHAN_INFO_SCALE), .indexed = 1, .channel = 0, - .event_mask = - IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)| - IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING) + .event_spec = ad7291_events, + .num_event_specs = ARRAY_SIZE(ad7291_events), } }; -static struct attribute_group ad7291_event_attribute_group = { - .attrs = ad7291_event_attributes, -}; - static const struct iio_info ad7291_info = { .read_raw = &ad7291_read_raw, - .read_event_config = &ad7291_read_event_config, - .write_event_config = &ad7291_write_event_config, - .read_event_value = &ad7291_read_event_value, - .write_event_value = &ad7291_write_event_value, - .event_attrs = &ad7291_event_attribute_group, + .read_event_config_new = &ad7291_read_event_config, + .write_event_config_new = &ad7291_write_event_config, + .read_event_value_new = &ad7291_read_event_value, + .write_event_value_new = &ad7291_write_event_value, .driver_module = THIS_MODULE, }; @@ -528,21 +467,19 @@ static int ad7291_probe(struct i2c_client *client, struct iio_dev *indio_dev; int ret = 0; - indio_dev = iio_device_alloc(sizeof(*chip)); - if (indio_dev == NULL) { - ret = -ENOMEM; - goto error_ret; - } + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); + if (!indio_dev) + return -ENOMEM; chip = iio_priv(indio_dev); if (pdata && pdata->use_external_ref) { - chip->reg = regulator_get(&client->dev, "vref"); + chip->reg = devm_regulator_get(&client->dev, "vref"); if (IS_ERR(chip->reg)) - goto error_free; + return ret; ret = regulator_enable(chip->reg); if (ret) - goto error_put_reg; + return ret; } mutex_init(&chip->state_lock); @@ -601,12 +538,7 @@ error_unreg_irq: error_disable_reg: if (chip->reg) regulator_disable(chip->reg); -error_put_reg: - if (chip->reg) - regulator_put(chip->reg); -error_free: - iio_device_free(indio_dev); -error_ret: + return ret; } @@ -620,12 +552,8 @@ static int ad7291_remove(struct i2c_client *client) if (client->irq) free_irq(client->irq, indio_dev); - if (chip->reg) { + if (chip->reg) regulator_disable(chip->reg); - regulator_put(chip->reg); - } - - iio_device_free(indio_dev); return 0; } diff --git a/drivers/staging/iio/adc/ad7606_core.c b/drivers/staging/iio/adc/ad7606_core.c index 72868ce..2083673 100644 --- a/drivers/staging/iio/adc/ad7606_core.c +++ b/drivers/staging/iio/adc/ad7606_core.c @@ -85,7 +85,6 @@ static int ad7606_read_raw(struct iio_dev *indio_dev, { int ret; struct ad7606_state *st = iio_priv(indio_dev); - unsigned int scale_uv; switch (m) { case IIO_CHAN_INFO_RAW: @@ -101,11 +100,9 @@ static int ad7606_read_raw(struct iio_dev *indio_dev, *val = (short) ret; return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: - scale_uv = (st->range * 1000 * 2) - >> st->chip_info->channels[0].scan_type.realbits; - *val = scale_uv / 1000; - *val2 = (scale_uv % 1000) * 1000; - return IIO_VAL_INT_PLUS_MICRO; + *val = st->range * 2; + *val2 = st->chip_info->channels[0].scan_type.realbits; + return IIO_VAL_FRACTIONAL_LOG2; } return -EINVAL; } @@ -425,8 +422,7 @@ static irqreturn_t ad7606_interrupt(int irq, void *dev_id) struct ad7606_state *st = iio_priv(indio_dev); if (iio_buffer_enabled(indio_dev)) { - if (!work_pending(&st->poll_work)) - schedule_work(&st->poll_work); + schedule_work(&st->poll_work); } else { st->done = true; wake_up_interruptible(&st->wq_data_avail); @@ -466,12 +462,11 @@ struct iio_dev *ad7606_probe(struct device *dev, int irq, struct ad7606_platform_data *pdata = dev->platform_data; struct ad7606_state *st; int ret; - struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st)); + struct iio_dev *indio_dev; - if (indio_dev == NULL) { - ret = -ENOMEM; - goto error_ret; - } + indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); + if (!indio_dev) + return ERR_PTR(-ENOMEM); st = iio_priv(indio_dev); @@ -489,11 +484,11 @@ struct iio_dev *ad7606_probe(struct device *dev, int irq, st->oversampling = pdata->default_os; } - st->reg = regulator_get(dev, "vcc"); + st->reg = devm_regulator_get(dev, "vcc"); if (!IS_ERR(st->reg)) { ret = regulator_enable(st->reg); if (ret) - goto error_put_reg; + return ERR_PTR(ret); } st->pdata = pdata; @@ -554,11 +549,6 @@ error_free_gpios: error_disable_reg: if (!IS_ERR(st->reg)) regulator_disable(st->reg); -error_put_reg: - if (!IS_ERR(st->reg)) - regulator_put(st->reg); - iio_device_free(indio_dev); -error_ret: return ERR_PTR(ret); } @@ -570,13 +560,10 @@ int ad7606_remove(struct iio_dev *indio_dev, int irq) ad7606_ring_cleanup(indio_dev); free_irq(irq, indio_dev); - if (!IS_ERR(st->reg)) { + if (!IS_ERR(st->reg)) regulator_disable(st->reg); - regulator_put(st->reg); - } ad7606_free_gpios(st); - iio_device_free(indio_dev); return 0; } diff --git a/drivers/staging/iio/adc/ad7606_ring.c b/drivers/staging/iio/adc/ad7606_ring.c index 2b25cb0..3bf174c 100644 --- a/drivers/staging/iio/adc/ad7606_ring.c +++ b/drivers/staging/iio/adc/ad7606_ring.c @@ -46,7 +46,6 @@ static void ad7606_poll_bh_to_ring(struct work_struct *work_s) struct ad7606_state *st = container_of(work_s, struct ad7606_state, poll_work); struct iio_dev *indio_dev = iio_priv_to_dev(st); - s64 time_ns; __u8 *buf; int ret; @@ -78,12 +77,7 @@ static void ad7606_poll_bh_to_ring(struct work_struct *work_s) goto done; } - time_ns = iio_get_time_ns(); - - if (indio_dev->scan_timestamp) - *((s64 *)(buf + indio_dev->scan_bytes - sizeof(s64))) = time_ns; - - iio_push_to_buffers(indio_dev, buf); + iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns()); done: gpio_set_value(st->pdata->gpio_convst, 0); iio_trigger_notify_done(indio_dev->trig); diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c index e1f8860..273add3 100644 --- a/drivers/staging/iio/adc/ad7780.c +++ b/drivers/staging/iio/adc/ad7780.c @@ -90,17 +90,14 @@ static int ad7780_read_raw(struct iio_dev *indio_dev, long m) { struct ad7780_state *st = iio_priv(indio_dev); - unsigned long scale_uv; switch (m) { case IIO_CHAN_INFO_RAW: return ad_sigma_delta_single_conversion(indio_dev, chan, val); case IIO_CHAN_INFO_SCALE: - scale_uv = (st->int_vref_mv * 100000 * st->gain) - >> (chan->scan_type.realbits - 1); - *val = scale_uv / 100000; - *val2 = (scale_uv % 100000) * 10; - return IIO_VAL_INT_PLUS_MICRO; + *val = st->int_vref_mv * st->gain; + *val2 = chan->scan_type.realbits - 1; + return IIO_VAL_FRACTIONAL_LOG2; case IIO_CHAN_INFO_OFFSET: *val -= (1 << (chan->scan_type.realbits - 1)); return IIO_VAL_INT; @@ -171,7 +168,7 @@ static int ad7780_probe(struct spi_device *spi) struct iio_dev *indio_dev; int ret, voltage_uv = 0; - indio_dev = iio_device_alloc(sizeof(*st)); + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); if (indio_dev == NULL) return -ENOMEM; @@ -180,11 +177,11 @@ static int ad7780_probe(struct spi_device *spi) ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info); - st->reg = regulator_get(&spi->dev, "vcc"); + st->reg = devm_regulator_get(&spi->dev, "vcc"); if (!IS_ERR(st->reg)) { ret = regulator_enable(st->reg); if (ret) - goto error_put_reg; + return ret; voltage_uv = regulator_get_voltage(st->reg); } @@ -210,8 +207,8 @@ static int ad7780_probe(struct spi_device *spi) if (pdata && gpio_is_valid(pdata->gpio_pdrst)) { - ret = gpio_request_one(pdata->gpio_pdrst, GPIOF_OUT_INIT_LOW, - "AD7780 /PDRST"); + ret = devm_gpio_request_one(&spi->dev, pdata->gpio_pdrst, + GPIOF_OUT_INIT_LOW, "AD7780 /PDRST"); if (ret) { dev_err(&spi->dev, "failed to request GPIO PDRST\n"); goto error_disable_reg; @@ -223,7 +220,7 @@ static int ad7780_probe(struct spi_device *spi) ret = ad_sd_setup_buffer_and_trigger(indio_dev); if (ret) - goto error_free_gpio; + goto error_disable_reg; ret = iio_device_register(indio_dev); if (ret) @@ -233,17 +230,9 @@ static int ad7780_probe(struct spi_device *spi) error_cleanup_buffer_and_trigger: ad_sd_cleanup_buffer_and_trigger(indio_dev); -error_free_gpio: - if (pdata && gpio_is_valid(pdata->gpio_pdrst)) - gpio_free(pdata->gpio_pdrst); error_disable_reg: if (!IS_ERR(st->reg)) regulator_disable(st->reg); -error_put_reg: - if (!IS_ERR(st->reg)) - regulator_put(st->reg); - - iio_device_free(indio_dev); return ret; } @@ -256,14 +245,8 @@ static int ad7780_remove(struct spi_device *spi) iio_device_unregister(indio_dev); ad_sd_cleanup_buffer_and_trigger(indio_dev); - if (gpio_is_valid(st->powerdown_gpio)) - gpio_free(st->powerdown_gpio); - - 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; } diff --git a/drivers/staging/iio/adc/ad7816.c b/drivers/staging/iio/adc/ad7816.c index 8470036..9f48e5c 100644 --- a/drivers/staging/iio/adc/ad7816.c +++ b/drivers/staging/iio/adc/ad7816.c @@ -356,11 +356,9 @@ static int ad7816_probe(struct spi_device *spi_dev) return -EINVAL; } - indio_dev = iio_device_alloc(sizeof(*chip)); - if (indio_dev == NULL) { - ret = -ENOMEM; - goto error_ret; - } + indio_dev = devm_iio_device_alloc(&spi_dev->dev, sizeof(*chip)); + if (!indio_dev) + return -ENOMEM; chip = iio_priv(indio_dev); /* this is only used for device removal purposes */ dev_set_drvdata(&spi_dev->dev, indio_dev); @@ -372,25 +370,28 @@ static int ad7816_probe(struct spi_device *spi_dev) chip->convert_pin = pins[1]; chip->busy_pin = pins[2]; - ret = gpio_request(chip->rdwr_pin, spi_get_device_id(spi_dev)->name); + ret = devm_gpio_request(&spi_dev->dev, chip->rdwr_pin, + spi_get_device_id(spi_dev)->name); if (ret) { dev_err(&spi_dev->dev, "Fail to request rdwr gpio PIN %d.\n", chip->rdwr_pin); - goto error_free_device; + return ret; } gpio_direction_input(chip->rdwr_pin); - ret = gpio_request(chip->convert_pin, spi_get_device_id(spi_dev)->name); + ret = devm_gpio_request(&spi_dev->dev, chip->convert_pin, + spi_get_device_id(spi_dev)->name); if (ret) { dev_err(&spi_dev->dev, "Fail to request convert gpio PIN %d.\n", chip->convert_pin); - goto error_free_gpio_rdwr; + return ret; } gpio_direction_input(chip->convert_pin); - ret = gpio_request(chip->busy_pin, spi_get_device_id(spi_dev)->name); + ret = devm_gpio_request(&spi_dev->dev, chip->busy_pin, + spi_get_device_id(spi_dev)->name); if (ret) { dev_err(&spi_dev->dev, "Fail to request busy gpio PIN %d.\n", chip->busy_pin); - goto error_free_gpio_convert; + return ret; } gpio_direction_input(chip->busy_pin); @@ -401,51 +402,31 @@ static int ad7816_probe(struct spi_device *spi_dev) if (spi_dev->irq) { /* Only low trigger is supported in ad7816/7/8 */ - ret = request_threaded_irq(spi_dev->irq, - NULL, - &ad7816_event_handler, - IRQF_TRIGGER_LOW | IRQF_ONESHOT, - indio_dev->name, - indio_dev); + ret = devm_request_threaded_irq(&spi_dev->dev, spi_dev->irq, + NULL, + &ad7816_event_handler, + IRQF_TRIGGER_LOW | IRQF_ONESHOT, + indio_dev->name, + indio_dev); if (ret) - goto error_free_gpio; + return ret; } ret = iio_device_register(indio_dev); if (ret) - goto error_free_irq; + return ret; dev_info(&spi_dev->dev, "%s temperature sensor and ADC registered.\n", indio_dev->name); return 0; -error_free_irq: - free_irq(spi_dev->irq, indio_dev); -error_free_gpio: - gpio_free(chip->busy_pin); -error_free_gpio_convert: - gpio_free(chip->convert_pin); -error_free_gpio_rdwr: - gpio_free(chip->rdwr_pin); -error_free_device: - iio_device_free(indio_dev); -error_ret: - return ret; } static int ad7816_remove(struct spi_device *spi_dev) { struct iio_dev *indio_dev = dev_get_drvdata(&spi_dev->dev); - struct ad7816_chip_info *chip = iio_priv(indio_dev); iio_device_unregister(indio_dev); - dev_set_drvdata(&spi_dev->dev, NULL); - if (spi_dev->irq) - free_irq(spi_dev->irq, indio_dev); - gpio_free(chip->busy_pin); - gpio_free(chip->convert_pin); - gpio_free(chip->rdwr_pin); - iio_device_free(indio_dev); return 0; } diff --git a/drivers/staging/iio/adc/ad799x.h b/drivers/staging/iio/adc/ad799x.h index b51680c..a591aa6 100644 --- a/drivers/staging/iio/adc/ad799x.h +++ b/drivers/staging/iio/adc/ad799x.h @@ -36,18 +36,10 @@ #define AD7998_ALERT_STAT_REG 0x1 #define AD7998_CONF_REG 0x2 #define AD7998_CYCLE_TMR_REG 0x3 -#define AD7998_DATALOW_CH1_REG 0x4 -#define AD7998_DATAHIGH_CH1_REG 0x5 -#define AD7998_HYST_CH1_REG 0x6 -#define AD7998_DATALOW_CH2_REG 0x7 -#define AD7998_DATAHIGH_CH2_REG 0x8 -#define AD7998_HYST_CH2_REG 0x9 -#define AD7998_DATALOW_CH3_REG 0xA -#define AD7998_DATAHIGH_CH3_REG 0xB -#define AD7998_HYST_CH3_REG 0xC -#define AD7998_DATALOW_CH4_REG 0xD -#define AD7998_DATAHIGH_CH4_REG 0xE -#define AD7998_HYST_CH4_REG 0xF + +#define AD7998_DATALOW_REG(x) ((x) * 3 + 0x4) +#define AD7998_DATAHIGH_REG(x) ((x) * 3 + 0x5) +#define AD7998_HYST_REG(x) ((x) * 3 + 0x6) #define AD7998_CYC_MASK 0x7 #define AD7998_CYC_DIS 0x0 diff --git a/drivers/staging/iio/adc/ad799x_core.c b/drivers/staging/iio/adc/ad799x_core.c index 2b2049c..9428be8 100644 --- a/drivers/staging/iio/adc/ad799x_core.c +++ b/drivers/staging/iio/adc/ad799x_core.c @@ -163,7 +163,6 @@ static int ad799x_read_raw(struct iio_dev *indio_dev, { int ret; struct ad799x_state *st = iio_priv(indio_dev); - unsigned int scale_uv; switch (m) { case IIO_CHAN_INFO_RAW: @@ -180,10 +179,9 @@ static int ad799x_read_raw(struct iio_dev *indio_dev, RES_MASK(chan->scan_type.realbits); return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: - scale_uv = (st->int_vref_mv * 1000) >> chan->scan_type.realbits; - *val = scale_uv / 1000; - *val2 = (scale_uv % 1000) * 1000; - return IIO_VAL_INT_PLUS_MICRO; + *val = st->int_vref_mv; + *val2 = chan->scan_type.realbits; + return IIO_VAL_FRACTIONAL_LOG2; } return -EINVAL; } @@ -254,98 +252,70 @@ error_ret_mutex: } static int ad799x_read_event_config(struct iio_dev *indio_dev, - u64 event_code) + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir) { return 1; } -static const u8 ad799x_threshold_addresses[][2] = { - { AD7998_DATALOW_CH1_REG, AD7998_DATAHIGH_CH1_REG }, - { AD7998_DATALOW_CH2_REG, AD7998_DATAHIGH_CH2_REG }, - { AD7998_DATALOW_CH3_REG, AD7998_DATAHIGH_CH3_REG }, - { AD7998_DATALOW_CH4_REG, AD7998_DATAHIGH_CH4_REG }, -}; +static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan, + enum iio_event_direction dir, + enum iio_event_info info) +{ + switch (info) { + case IIO_EV_INFO_VALUE: + if (dir == IIO_EV_DIR_FALLING) + return AD7998_DATALOW_REG(chan->channel); + else + return AD7998_DATAHIGH_REG(chan->channel); + case IIO_EV_INFO_HYSTERESIS: + return AD7998_HYST_REG(chan->channel); + default: + return -EINVAL; + } + + return 0; +} static int ad799x_write_event_value(struct iio_dev *indio_dev, - u64 event_code, - int val) + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, + enum iio_event_info info, + int val, int val2) { int ret; struct ad799x_state *st = iio_priv(indio_dev); - int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) == - IIO_EV_DIR_FALLING); - int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code); mutex_lock(&indio_dev->mlock); - ret = ad799x_i2c_write16(st, - ad799x_threshold_addresses[number][direction], - val); + ret = ad799x_i2c_write16(st, ad799x_threshold_reg(chan, dir, info), + val); mutex_unlock(&indio_dev->mlock); return ret; } static int ad799x_read_event_value(struct iio_dev *indio_dev, - u64 event_code, - int *val) + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, + enum iio_event_info info, + int *val, int *val2) { int ret; struct ad799x_state *st = iio_priv(indio_dev); - int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) == - IIO_EV_DIR_FALLING); - int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code); u16 valin; mutex_lock(&indio_dev->mlock); - ret = ad799x_i2c_read16(st, - ad799x_threshold_addresses[number][direction], - &valin); + ret = ad799x_i2c_read16(st, ad799x_threshold_reg(chan, dir, info), + &valin); mutex_unlock(&indio_dev->mlock); if (ret < 0) return ret; *val = valin; - return 0; -} - -static ssize_t ad799x_read_channel_config(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct iio_dev *indio_dev = dev_to_iio_dev(dev); - struct ad799x_state *st = iio_priv(indio_dev); - struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); - - int ret; - u16 val; - ret = ad799x_i2c_read16(st, this_attr->address, &val); - if (ret) - return ret; - - return sprintf(buf, "%d\n", val); -} - -static ssize_t ad799x_write_channel_config(struct device *dev, - struct device_attribute *attr, - const char *buf, - size_t len) -{ - struct iio_dev *indio_dev = dev_to_iio_dev(dev); - struct ad799x_state *st = iio_priv(indio_dev); - struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); - - long val; - int ret; - - ret = kstrtol(buf, 10, &val); - if (ret) - return ret; - - mutex_lock(&indio_dev->mlock); - ret = ad799x_i2c_write16(st, this_attr->address, val); - mutex_unlock(&indio_dev->mlock); - - return ret ? ret : len; + return IIO_VAL_INT; } static irqreturn_t ad799x_event_handler(int irq, void *private) @@ -383,60 +353,19 @@ done: return IRQ_HANDLED; } -static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw, - S_IRUGO | S_IWUSR, - ad799x_read_channel_config, - ad799x_write_channel_config, - AD7998_HYST_CH1_REG); - -static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw, - S_IRUGO | S_IWUSR, - ad799x_read_channel_config, - ad799x_write_channel_config, - AD7998_HYST_CH2_REG); - -static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw, - S_IRUGO | S_IWUSR, - ad799x_read_channel_config, - ad799x_write_channel_config, - AD7998_HYST_CH3_REG); - -static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw, - S_IRUGO | S_IWUSR, - ad799x_read_channel_config, - ad799x_write_channel_config, - AD7998_HYST_CH4_REG); - static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, ad799x_read_frequency, ad799x_write_frequency); static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0"); -static struct attribute *ad7993_4_7_8_event_attributes[] = { - &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr, - &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr, - &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr, - &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr, - &iio_dev_attr_sampling_frequency.dev_attr.attr, - &iio_const_attr_sampling_frequency_available.dev_attr.attr, - NULL, -}; - -static struct attribute_group ad7993_4_7_8_event_attrs_group = { - .attrs = ad7993_4_7_8_event_attributes, - .name = "events", -}; - -static struct attribute *ad7992_event_attributes[] = { - &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr, - &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr, +static struct attribute *ad799x_event_attributes[] = { &iio_dev_attr_sampling_frequency.dev_attr.attr, &iio_const_attr_sampling_frequency_available.dev_attr.attr, NULL, }; -static struct attribute_group ad7992_event_attrs_group = { - .attrs = ad7992_event_attributes, +static struct attribute_group ad799x_event_attrs_group = { + .attrs = ad799x_event_attributes, .name = "events", }; @@ -445,29 +374,35 @@ static const struct iio_info ad7991_info = { .driver_module = THIS_MODULE, }; -static const struct iio_info ad7992_info = { - .read_raw = &ad799x_read_raw, - .event_attrs = &ad7992_event_attrs_group, - .read_event_config = &ad799x_read_event_config, - .read_event_value = &ad799x_read_event_value, - .write_event_value = &ad799x_write_event_value, - .driver_module = THIS_MODULE, -}; - static const struct iio_info ad7993_4_7_8_info = { .read_raw = &ad799x_read_raw, - .event_attrs = &ad7993_4_7_8_event_attrs_group, - .read_event_config = &ad799x_read_event_config, - .read_event_value = &ad799x_read_event_value, - .write_event_value = &ad799x_write_event_value, + .event_attrs = &ad799x_event_attrs_group, + .read_event_config_new = &ad799x_read_event_config, + .read_event_value_new = &ad799x_read_event_value, + .write_event_value_new = &ad799x_write_event_value, .driver_module = THIS_MODULE, .update_scan_mode = ad7997_8_update_scan_mode, }; -#define AD799X_EV_MASK (IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) | \ - IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)) +static const struct iio_event_spec ad799x_events[] = { + { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_RISING, + .mask_separate = BIT(IIO_EV_INFO_VALUE) | + BIT(IIO_EV_INFO_ENABLE), + }, { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_FALLING, + .mask_separate = BIT(IIO_EV_INFO_VALUE), + BIT(IIO_EV_INFO_ENABLE), + }, { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_EITHER, + .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS), + }, +}; -#define AD799X_CHANNEL(_index, _realbits, _evmask) { \ +#define _AD799X_CHANNEL(_index, _realbits, _ev_spec, _num_ev_spec) { \ .type = IIO_VOLTAGE, \ .indexed = 1, \ .channel = (_index), \ @@ -475,16 +410,24 @@ static const struct iio_info ad7993_4_7_8_info = { .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ .scan_index = (_index), \ .scan_type = IIO_ST('u', _realbits, 16, 12 - (_realbits)), \ - .event_mask = (_evmask), \ + .event_spec = _ev_spec, \ + .num_event_specs = _num_ev_spec, \ } +#define AD799X_CHANNEL(_index, _realbits) \ + _AD799X_CHANNEL(_index, _realbits, NULL, 0) + +#define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \ + _AD799X_CHANNEL(_index, _realbits, ad799x_events, \ + ARRAY_SIZE(ad799x_events)) + static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { [ad7991] = { .channel = { - AD799X_CHANNEL(0, 12, 0), - AD799X_CHANNEL(1, 12, 0), - AD799X_CHANNEL(2, 12, 0), - AD799X_CHANNEL(3, 12, 0), + AD799X_CHANNEL(0, 12), + AD799X_CHANNEL(1, 12), + AD799X_CHANNEL(2, 12), + AD799X_CHANNEL(3, 12), IIO_CHAN_SOFT_TIMESTAMP(4), }, .num_channels = 5, @@ -492,10 +435,10 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { }, [ad7995] = { .channel = { - AD799X_CHANNEL(0, 10, 0), - AD799X_CHANNEL(1, 10, 0), - AD799X_CHANNEL(2, 10, 0), - AD799X_CHANNEL(3, 10, 0), + AD799X_CHANNEL(0, 10), + AD799X_CHANNEL(1, 10), + AD799X_CHANNEL(2, 10), + AD799X_CHANNEL(3, 10), IIO_CHAN_SOFT_TIMESTAMP(4), }, .num_channels = 5, @@ -503,10 +446,10 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { }, [ad7999] = { .channel = { - AD799X_CHANNEL(0, 8, 0), - AD799X_CHANNEL(1, 8, 0), - AD799X_CHANNEL(2, 8, 0), - AD799X_CHANNEL(3, 8, 0), + AD799X_CHANNEL(0, 8), + AD799X_CHANNEL(1, 8), + AD799X_CHANNEL(2, 8), + AD799X_CHANNEL(3, 8), IIO_CHAN_SOFT_TIMESTAMP(4), }, .num_channels = 5, @@ -514,20 +457,20 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { }, [ad7992] = { .channel = { - AD799X_CHANNEL(0, 12, AD799X_EV_MASK), - AD799X_CHANNEL(1, 12, AD799X_EV_MASK), + AD799X_CHANNEL_WITH_EVENTS(0, 12), + AD799X_CHANNEL_WITH_EVENTS(1, 12), IIO_CHAN_SOFT_TIMESTAMP(3), }, .num_channels = 3, .default_config = AD7998_ALERT_EN, - .info = &ad7992_info, + .info = &ad7993_4_7_8_info, }, [ad7993] = { .channel = { - AD799X_CHANNEL(0, 10, AD799X_EV_MASK), - AD799X_CHANNEL(1, 10, AD799X_EV_MASK), - AD799X_CHANNEL(2, 10, AD799X_EV_MASK), - AD799X_CHANNEL(3, 10, AD799X_EV_MASK), + AD799X_CHANNEL_WITH_EVENTS(0, 10), + AD799X_CHANNEL_WITH_EVENTS(1, 10), + AD799X_CHANNEL_WITH_EVENTS(2, 10), + AD799X_CHANNEL_WITH_EVENTS(3, 10), IIO_CHAN_SOFT_TIMESTAMP(4), }, .num_channels = 5, @@ -536,10 +479,10 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { }, [ad7994] = { .channel = { - AD799X_CHANNEL(0, 12, AD799X_EV_MASK), - AD799X_CHANNEL(1, 12, AD799X_EV_MASK), - AD799X_CHANNEL(2, 12, AD799X_EV_MASK), - AD799X_CHANNEL(3, 12, AD799X_EV_MASK), + AD799X_CHANNEL_WITH_EVENTS(0, 12), + AD799X_CHANNEL_WITH_EVENTS(1, 12), + AD799X_CHANNEL_WITH_EVENTS(2, 12), + AD799X_CHANNEL_WITH_EVENTS(3, 12), IIO_CHAN_SOFT_TIMESTAMP(4), }, .num_channels = 5, @@ -548,14 +491,14 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { }, [ad7997] = { .channel = { - AD799X_CHANNEL(0, 10, AD799X_EV_MASK), - AD799X_CHANNEL(1, 10, AD799X_EV_MASK), - AD799X_CHANNEL(2, 10, AD799X_EV_MASK), - AD799X_CHANNEL(3, 10, AD799X_EV_MASK), - AD799X_CHANNEL(4, 10, 0), - AD799X_CHANNEL(5, 10, 0), - AD799X_CHANNEL(6, 10, 0), - AD799X_CHANNEL(7, 10, 0), + AD799X_CHANNEL_WITH_EVENTS(0, 10), + AD799X_CHANNEL_WITH_EVENTS(1, 10), + AD799X_CHANNEL_WITH_EVENTS(2, 10), + AD799X_CHANNEL_WITH_EVENTS(3, 10), + AD799X_CHANNEL(4, 10), + AD799X_CHANNEL(5, 10), + AD799X_CHANNEL(6, 10), + AD799X_CHANNEL(7, 10), IIO_CHAN_SOFT_TIMESTAMP(8), }, .num_channels = 9, @@ -564,14 +507,14 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { }, [ad7998] = { .channel = { - AD799X_CHANNEL(0, 12, AD799X_EV_MASK), - AD799X_CHANNEL(1, 12, AD799X_EV_MASK), - AD799X_CHANNEL(2, 12, AD799X_EV_MASK), - AD799X_CHANNEL(3, 12, AD799X_EV_MASK), - AD799X_CHANNEL(4, 12, 0), - AD799X_CHANNEL(5, 12, 0), - AD799X_CHANNEL(6, 12, 0), - AD799X_CHANNEL(7, 12, 0), + AD799X_CHANNEL_WITH_EVENTS(0, 12), + AD799X_CHANNEL_WITH_EVENTS(1, 12), + AD799X_CHANNEL_WITH_EVENTS(2, 12), + AD799X_CHANNEL_WITH_EVENTS(3, 12), + AD799X_CHANNEL(4, 12), + AD799X_CHANNEL(5, 12), + AD799X_CHANNEL(6, 12), + AD799X_CHANNEL(7, 12), IIO_CHAN_SOFT_TIMESTAMP(8), }, .num_channels = 9, @@ -586,8 +529,9 @@ static int ad799x_probe(struct i2c_client *client, int ret; struct ad799x_platform_data *pdata = client->dev.platform_data; struct ad799x_state *st; - struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st)); + struct iio_dev *indio_dev; + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st)); if (indio_dev == NULL) return -ENOMEM; @@ -606,11 +550,11 @@ static int ad799x_probe(struct i2c_client *client, st->int_vref_mv = pdata->vref_mv; - st->reg = regulator_get(&client->dev, "vcc"); + st->reg = devm_regulator_get(&client->dev, "vcc"); if (!IS_ERR(st->reg)) { ret = regulator_enable(st->reg); if (ret) - goto error_put_reg; + return ret; } st->client = client; @@ -650,10 +594,6 @@ error_cleanup_ring: error_disable_reg: if (!IS_ERR(st->reg)) regulator_disable(st->reg); -error_put_reg: - if (!IS_ERR(st->reg)) - regulator_put(st->reg); - iio_device_free(indio_dev); return ret; } @@ -668,12 +608,9 @@ static int ad799x_remove(struct i2c_client *client) free_irq(client->irq, indio_dev); ad799x_ring_cleanup(indio_dev); - if (!IS_ERR(st->reg)) { + if (!IS_ERR(st->reg)) regulator_disable(st->reg); - regulator_put(st->reg); - } kfree(st->rx_buf); - iio_device_free(indio_dev); return 0; } diff --git a/drivers/staging/iio/adc/ad799x_ring.c b/drivers/staging/iio/adc/ad799x_ring.c index c2ebae1..0ff6c03 100644 --- a/drivers/staging/iio/adc/ad799x_ring.c +++ b/drivers/staging/iio/adc/ad799x_ring.c @@ -35,7 +35,6 @@ static irqreturn_t ad799x_trigger_handler(int irq, void *p) struct iio_poll_func *pf = p; struct iio_dev *indio_dev = pf->indio_dev; struct ad799x_state *st = iio_priv(indio_dev); - s64 time_ns; int b_sent; u8 cmd; @@ -65,13 +64,8 @@ static irqreturn_t ad799x_trigger_handler(int irq, void *p) if (b_sent < 0) goto out; - time_ns = iio_get_time_ns(); - - if (indio_dev->scan_timestamp) - memcpy(st->rx_buf + indio_dev->scan_bytes - sizeof(s64), - &time_ns, sizeof(time_ns)); - - iio_push_to_buffers(indio_dev, st->rx_buf); + iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf, + iio_get_time_ns()); out: iio_trigger_notify_done(indio_dev->trig); diff --git a/drivers/staging/iio/adc/lpc32xx_adc.c b/drivers/staging/iio/adc/lpc32xx_adc.c index 9a4bb09..ef0a21d 100644 --- a/drivers/staging/iio/adc/lpc32xx_adc.c +++ b/drivers/staging/iio/adc/lpc32xx_adc.c @@ -137,43 +137,39 @@ static int lpc32xx_adc_probe(struct platform_device *pdev) res = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!res) { dev_err(&pdev->dev, "failed to get platform I/O memory\n"); - retval = -EBUSY; - goto errout1; + return -EBUSY; } - iodev = iio_device_alloc(sizeof(struct lpc32xx_adc_info)); - if (!iodev) { - dev_err(&pdev->dev, "failed allocating iio device\n"); - retval = -ENOMEM; - goto errout1; - } + iodev = devm_iio_device_alloc(&pdev->dev, sizeof(*info)); + if (!iodev) + return -ENOMEM; info = iio_priv(iodev); - info->adc_base = ioremap(res->start, resource_size(res)); + info->adc_base = devm_ioremap(&pdev->dev, res->start, + resource_size(res)); if (!info->adc_base) { dev_err(&pdev->dev, "failed mapping memory\n"); - retval = -EBUSY; - goto errout2; + return -EBUSY; } - info->clk = clk_get(&pdev->dev, NULL); + info->clk = devm_clk_get(&pdev->dev, NULL); if (IS_ERR(info->clk)) { dev_err(&pdev->dev, "failed getting clock\n"); - goto errout3; + return PTR_ERR(info->clk); } irq = platform_get_irq(pdev, 0); - if ((irq < 0) || (irq >= NR_IRQS)) { + if (irq <= 0) { dev_err(&pdev->dev, "failed getting interrupt resource\n"); - retval = -EINVAL; - goto errout4; + return -EINVAL; } - retval = request_irq(irq, lpc32xx_adc_isr, 0, MOD_NAME, info); + retval = devm_request_irq(&pdev->dev, irq, lpc32xx_adc_isr, 0, + MOD_NAME, info); if (retval < 0) { dev_err(&pdev->dev, "failed requesting interrupt\n"); - goto errout4; + return retval; } platform_set_drvdata(pdev, iodev); @@ -189,35 +185,18 @@ static int lpc32xx_adc_probe(struct platform_device *pdev) retval = iio_device_register(iodev); if (retval) - goto errout5; + return retval; dev_info(&pdev->dev, "LPC32XX ADC driver loaded, IRQ %d\n", irq); return 0; - -errout5: - free_irq(irq, info); -errout4: - clk_put(info->clk); -errout3: - iounmap(info->adc_base); -errout2: - iio_device_free(iodev); -errout1: - return retval; } static int lpc32xx_adc_remove(struct platform_device *pdev) { struct iio_dev *iodev = platform_get_drvdata(pdev); - struct lpc32xx_adc_info *info = iio_priv(iodev); - int irq = platform_get_irq(pdev, 0); iio_device_unregister(iodev); - free_irq(irq, info); - clk_put(info->clk); - iounmap(info->adc_base); - iio_device_free(iodev); return 0; } diff --git a/drivers/staging/iio/adc/mxs-lradc.c b/drivers/staging/iio/adc/mxs-lradc.c index a08c173..aeae76b 100644 --- a/drivers/staging/iio/adc/mxs-lradc.c +++ b/drivers/staging/iio/adc/mxs-lradc.c @@ -35,6 +35,7 @@ #include <linux/completion.h> #include <linux/delay.h> #include <linux/input.h> +#include <linux/clk.h> #include <linux/iio/iio.h> #include <linux/iio/buffer.h> @@ -129,11 +130,24 @@ enum mxs_lradc_ts { MXS_LRADC_TOUCHSCREEN_5WIRE, }; +/* + * Touchscreen handling + */ +enum lradc_ts_plate { + LRADC_TOUCH = 0, + LRADC_SAMPLE_X, + LRADC_SAMPLE_Y, + LRADC_SAMPLE_PRESSURE, + LRADC_SAMPLE_VALID, +}; + struct mxs_lradc { struct device *dev; void __iomem *base; int irq[13]; + struct clk *clk; + uint32_t *buffer; struct iio_trigger *trig; @@ -169,32 +183,63 @@ struct mxs_lradc { #define CHAN_MASK_TOUCHSCREEN_4WIRE (0xf << 2) #define CHAN_MASK_TOUCHSCREEN_5WIRE (0x1f << 2) enum mxs_lradc_ts use_touchscreen; - bool stop_touchscreen; bool use_touchbutton; struct input_dev *ts_input; - struct work_struct ts_work; + + enum mxs_lradc_id soc; + enum lradc_ts_plate cur_plate; /* statemachine */ + bool ts_valid; + unsigned ts_x_pos; + unsigned ts_y_pos; + unsigned ts_pressure; + + /* handle touchscreen's physical behaviour */ + /* samples per coordinate */ + unsigned over_sample_cnt; + /* time clocks between samples */ + unsigned over_sample_delay; + /* time in clocks to wait after the plates where switched */ + unsigned settling_delay; }; #define LRADC_CTRL0 0x00 -#define LRADC_CTRL0_TOUCH_DETECT_ENABLE (1 << 23) -#define LRADC_CTRL0_TOUCH_SCREEN_TYPE (1 << 22) -#define LRADC_CTRL0_YNNSW /* YM */ (1 << 21) -#define LRADC_CTRL0_YPNSW /* YP */ (1 << 20) -#define LRADC_CTRL0_YPPSW /* YP */ (1 << 19) -#define LRADC_CTRL0_XNNSW /* XM */ (1 << 18) -#define LRADC_CTRL0_XNPSW /* XM */ (1 << 17) -#define LRADC_CTRL0_XPPSW /* XP */ (1 << 16) -#define LRADC_CTRL0_PLATE_MASK (0x3f << 16) +# define LRADC_CTRL0_MX28_TOUCH_DETECT_ENABLE (1 << 23) +# define LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE (1 << 22) +# define LRADC_CTRL0_MX28_YNNSW /* YM */ (1 << 21) +# define LRADC_CTRL0_MX28_YPNSW /* YP */ (1 << 20) +# define LRADC_CTRL0_MX28_YPPSW /* YP */ (1 << 19) +# define LRADC_CTRL0_MX28_XNNSW /* XM */ (1 << 18) +# define LRADC_CTRL0_MX28_XNPSW /* XM */ (1 << 17) +# define LRADC_CTRL0_MX28_XPPSW /* XP */ (1 << 16) + +# define LRADC_CTRL0_MX23_TOUCH_DETECT_ENABLE (1 << 20) +# define LRADC_CTRL0_MX23_YM (1 << 19) +# define LRADC_CTRL0_MX23_XM (1 << 18) +# define LRADC_CTRL0_MX23_YP (1 << 17) +# define LRADC_CTRL0_MX23_XP (1 << 16) + +# define LRADC_CTRL0_MX28_PLATE_MASK \ + (LRADC_CTRL0_MX28_TOUCH_DETECT_ENABLE | \ + LRADC_CTRL0_MX28_YNNSW | LRADC_CTRL0_MX28_YPNSW | \ + LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_XNNSW | \ + LRADC_CTRL0_MX28_XNPSW | LRADC_CTRL0_MX28_XPPSW) + +# define LRADC_CTRL0_MX23_PLATE_MASK \ + (LRADC_CTRL0_MX23_TOUCH_DETECT_ENABLE | \ + LRADC_CTRL0_MX23_YM | LRADC_CTRL0_MX23_XM | \ + LRADC_CTRL0_MX23_YP | LRADC_CTRL0_MX23_XP) #define LRADC_CTRL1 0x10 #define LRADC_CTRL1_TOUCH_DETECT_IRQ_EN (1 << 24) #define LRADC_CTRL1_LRADC_IRQ_EN(n) (1 << ((n) + 16)) -#define LRADC_CTRL1_LRADC_IRQ_EN_MASK (0x1fff << 16) +#define LRADC_CTRL1_MX28_LRADC_IRQ_EN_MASK (0x1fff << 16) +#define LRADC_CTRL1_MX23_LRADC_IRQ_EN_MASK (0x01ff << 16) #define LRADC_CTRL1_LRADC_IRQ_EN_OFFSET 16 #define LRADC_CTRL1_TOUCH_DETECT_IRQ (1 << 8) #define LRADC_CTRL1_LRADC_IRQ(n) (1 << (n)) -#define LRADC_CTRL1_LRADC_IRQ_MASK 0x1fff +#define LRADC_CTRL1_MX28_LRADC_IRQ_MASK 0x1fff +#define LRADC_CTRL1_MX23_LRADC_IRQ_MASK 0x01ff #define LRADC_CTRL1_LRADC_IRQ_OFFSET 0 #define LRADC_CTRL2 0x20 @@ -207,19 +252,33 @@ struct mxs_lradc { #define LRADC_CH_ACCUMULATE (1 << 29) #define LRADC_CH_NUM_SAMPLES_MASK (0x1f << 24) #define LRADC_CH_NUM_SAMPLES_OFFSET 24 +#define LRADC_CH_NUM_SAMPLES(x) \ + ((x) << LRADC_CH_NUM_SAMPLES_OFFSET) #define LRADC_CH_VALUE_MASK 0x3ffff #define LRADC_CH_VALUE_OFFSET 0 #define LRADC_DELAY(n) (0xd0 + (0x10 * (n))) #define LRADC_DELAY_TRIGGER_LRADCS_MASK (0xff << 24) #define LRADC_DELAY_TRIGGER_LRADCS_OFFSET 24 +#define LRADC_DELAY_TRIGGER(x) \ + (((x) << LRADC_DELAY_TRIGGER_LRADCS_OFFSET) & \ + LRADC_DELAY_TRIGGER_LRADCS_MASK) #define LRADC_DELAY_KICK (1 << 20) #define LRADC_DELAY_TRIGGER_DELAYS_MASK (0xf << 16) #define LRADC_DELAY_TRIGGER_DELAYS_OFFSET 16 +#define LRADC_DELAY_TRIGGER_DELAYS(x) \ + (((x) << LRADC_DELAY_TRIGGER_DELAYS_OFFSET) & \ + LRADC_DELAY_TRIGGER_DELAYS_MASK) #define LRADC_DELAY_LOOP_COUNT_MASK (0x1f << 11) #define LRADC_DELAY_LOOP_COUNT_OFFSET 11 +#define LRADC_DELAY_LOOP(x) \ + (((x) << LRADC_DELAY_LOOP_COUNT_OFFSET) & \ + LRADC_DELAY_LOOP_COUNT_MASK) #define LRADC_DELAY_DELAY_MASK 0x7ff #define LRADC_DELAY_DELAY_OFFSET 0 +#define LRADC_DELAY_DELAY(x) \ + (((x) << LRADC_DELAY_DELAY_OFFSET) & \ + LRADC_DELAY_DELAY_MASK) #define LRADC_CTRL4 0x140 #define LRADC_CTRL4_LRADCSELECT_MASK(n) (0xf << ((n) * 4)) @@ -228,6 +287,475 @@ struct mxs_lradc { #define LRADC_RESOLUTION 12 #define LRADC_SINGLE_SAMPLE_MASK ((1 << LRADC_RESOLUTION) - 1) +static void mxs_lradc_reg_set(struct mxs_lradc *lradc, u32 val, u32 reg) +{ + writel(val, lradc->base + reg + STMP_OFFSET_REG_SET); +} + +static void mxs_lradc_reg_clear(struct mxs_lradc *lradc, u32 val, u32 reg) +{ + writel(val, lradc->base + reg + STMP_OFFSET_REG_CLR); +} + +static void mxs_lradc_reg_wrt(struct mxs_lradc *lradc, u32 val, u32 reg) +{ + writel(val, lradc->base + reg); +} + +static u32 mxs_lradc_plate_mask(struct mxs_lradc *lradc) +{ + if (lradc->soc == IMX23_LRADC) + return LRADC_CTRL0_MX23_PLATE_MASK; + else + return LRADC_CTRL0_MX28_PLATE_MASK; +} + +static u32 mxs_lradc_irq_en_mask(struct mxs_lradc *lradc) +{ + if (lradc->soc == IMX23_LRADC) + return LRADC_CTRL1_MX23_LRADC_IRQ_EN_MASK; + else + return LRADC_CTRL1_MX28_LRADC_IRQ_EN_MASK; +} + +static u32 mxs_lradc_irq_mask(struct mxs_lradc *lradc) +{ + if (lradc->soc == IMX23_LRADC) + return LRADC_CTRL1_MX23_LRADC_IRQ_MASK; + else + return LRADC_CTRL1_MX28_LRADC_IRQ_MASK; +} + +static u32 mxs_lradc_touch_detect_bit(struct mxs_lradc *lradc) +{ + if (lradc->soc == IMX23_LRADC) + return LRADC_CTRL0_MX23_TOUCH_DETECT_ENABLE; + else + return LRADC_CTRL0_MX28_TOUCH_DETECT_ENABLE; +} + +static u32 mxs_lradc_drive_x_plate(struct mxs_lradc *lradc) +{ + if (lradc->soc == IMX23_LRADC) + return LRADC_CTRL0_MX23_XP | LRADC_CTRL0_MX23_XM; + else + return LRADC_CTRL0_MX28_XPPSW | LRADC_CTRL0_MX28_XNNSW; +} + +static u32 mxs_lradc_drive_y_plate(struct mxs_lradc *lradc) +{ + if (lradc->soc == IMX23_LRADC) + return LRADC_CTRL0_MX23_YP | LRADC_CTRL0_MX23_YM; + else + return LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_YNNSW; +} + +static u32 mxs_lradc_drive_pressure(struct mxs_lradc *lradc) +{ + if (lradc->soc == IMX23_LRADC) + return LRADC_CTRL0_MX23_YP | LRADC_CTRL0_MX23_XM; + else + return LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_XNNSW; +} + +static bool mxs_lradc_check_touch_event(struct mxs_lradc *lradc) +{ + return !!(readl(lradc->base + LRADC_STATUS) & + LRADC_STATUS_TOUCH_DETECT_RAW); +} + +static void mxs_lradc_setup_ts_channel(struct mxs_lradc *lradc, unsigned ch) +{ + /* + * prepare for oversampling conversion + * + * from the datasheet: + * "The ACCUMULATE bit in the appropriate channel register + * HW_LRADC_CHn must be set to 1 if NUM_SAMPLES is greater then 0; + * otherwise, the IRQs will not fire." + */ + mxs_lradc_reg_wrt(lradc, LRADC_CH_ACCUMULATE | + LRADC_CH_NUM_SAMPLES(lradc->over_sample_cnt - 1), + LRADC_CH(ch)); + + /* from the datasheet: + * "Software must clear this register in preparation for a + * multi-cycle accumulation. + */ + mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch)); + + /* prepare the delay/loop unit according to the oversampling count */ + mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(1 << ch) | + LRADC_DELAY_TRIGGER_DELAYS(0) | + LRADC_DELAY_LOOP(lradc->over_sample_cnt - 1) | + LRADC_DELAY_DELAY(lradc->over_sample_delay - 1), + LRADC_DELAY(3)); + + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ(2) | + LRADC_CTRL1_LRADC_IRQ(3) | LRADC_CTRL1_LRADC_IRQ(4) | + LRADC_CTRL1_LRADC_IRQ(5), LRADC_CTRL1); + + /* wake us again, when the complete conversion is done */ + mxs_lradc_reg_set(lradc, LRADC_CTRL1_LRADC_IRQ_EN(ch), LRADC_CTRL1); + /* + * after changing the touchscreen plates setting + * the signals need some initial time to settle. Start the + * SoC's delay unit and start the conversion later + * and automatically. + */ + mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(0) | /* don't trigger ADC */ + LRADC_DELAY_TRIGGER_DELAYS(1 << 3) | /* trigger DELAY unit#3 */ + LRADC_DELAY_KICK | + LRADC_DELAY_DELAY(lradc->settling_delay), + LRADC_DELAY(2)); +} + +/* + * Pressure detection is special: + * We want to do both required measurements for the pressure detection in + * one turn. Use the hardware features to chain both conversions and let the + * hardware report one interrupt if both conversions are done + */ +static void mxs_lradc_setup_ts_pressure(struct mxs_lradc *lradc, unsigned ch1, + unsigned ch2) +{ + u32 reg; + + /* + * prepare for oversampling conversion + * + * from the datasheet: + * "The ACCUMULATE bit in the appropriate channel register + * HW_LRADC_CHn must be set to 1 if NUM_SAMPLES is greater then 0; + * otherwise, the IRQs will not fire." + */ + reg = LRADC_CH_ACCUMULATE | + LRADC_CH_NUM_SAMPLES(lradc->over_sample_cnt - 1); + mxs_lradc_reg_wrt(lradc, reg, LRADC_CH(ch1)); + mxs_lradc_reg_wrt(lradc, reg, LRADC_CH(ch2)); + + /* from the datasheet: + * "Software must clear this register in preparation for a + * multi-cycle accumulation. + */ + mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch1)); + mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch2)); + + /* prepare the delay/loop unit according to the oversampling count */ + mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(1 << ch1) | + LRADC_DELAY_TRIGGER(1 << ch2) | /* start both channels */ + LRADC_DELAY_TRIGGER_DELAYS(0) | + LRADC_DELAY_LOOP(lradc->over_sample_cnt - 1) | + LRADC_DELAY_DELAY(lradc->over_sample_delay - 1), + LRADC_DELAY(3)); + + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ(2) | + LRADC_CTRL1_LRADC_IRQ(3) | LRADC_CTRL1_LRADC_IRQ(4) | + LRADC_CTRL1_LRADC_IRQ(5), LRADC_CTRL1); + + /* wake us again, when the conversions are done */ + mxs_lradc_reg_set(lradc, LRADC_CTRL1_LRADC_IRQ_EN(ch2), LRADC_CTRL1); + /* + * after changing the touchscreen plates setting + * the signals need some initial time to settle. Start the + * SoC's delay unit and start the conversion later + * and automatically. + */ + mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(0) | /* don't trigger ADC */ + LRADC_DELAY_TRIGGER_DELAYS(1 << 3) | /* trigger DELAY unit#3 */ + LRADC_DELAY_KICK | + LRADC_DELAY_DELAY(lradc->settling_delay), LRADC_DELAY(2)); +} + +static unsigned mxs_lradc_read_raw_channel(struct mxs_lradc *lradc, + unsigned channel) +{ + u32 reg; + unsigned num_samples, val; + + reg = readl(lradc->base + LRADC_CH(channel)); + if (reg & LRADC_CH_ACCUMULATE) + num_samples = lradc->over_sample_cnt; + else + num_samples = 1; + + val = (reg & LRADC_CH_VALUE_MASK) >> LRADC_CH_VALUE_OFFSET; + return val / num_samples; +} + +static unsigned mxs_lradc_read_ts_pressure(struct mxs_lradc *lradc, + unsigned ch1, unsigned ch2) +{ + u32 reg, mask; + unsigned pressure, m1, m2; + + mask = LRADC_CTRL1_LRADC_IRQ(ch1) | LRADC_CTRL1_LRADC_IRQ(ch2); + reg = readl(lradc->base + LRADC_CTRL1) & mask; + + while (reg != mask) { + reg = readl(lradc->base + LRADC_CTRL1) & mask; + dev_dbg(lradc->dev, "One channel is still busy: %X\n", reg); + } + + m1 = mxs_lradc_read_raw_channel(lradc, ch1); + m2 = mxs_lradc_read_raw_channel(lradc, ch2); + + if (m2 == 0) { + dev_warn(lradc->dev, "Cannot calculate pressure\n"); + return 1 << (LRADC_RESOLUTION - 1); + } + + /* simply scale the value from 0 ... max ADC resolution */ + pressure = m1; + pressure *= (1 << LRADC_RESOLUTION); + pressure /= m2; + + dev_dbg(lradc->dev, "Pressure = %u\n", pressure); + return pressure; +} + +#define TS_CH_XP 2 +#define TS_CH_YP 3 +#define TS_CH_XM 4 +#define TS_CH_YM 5 + +static int mxs_lradc_read_ts_channel(struct mxs_lradc *lradc) +{ + u32 reg; + int val; + + reg = readl(lradc->base + LRADC_CTRL1); + + /* only channels 3 to 5 are of interest here */ + if (reg & LRADC_CTRL1_LRADC_IRQ(TS_CH_YP)) { + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(TS_CH_YP) | + LRADC_CTRL1_LRADC_IRQ(TS_CH_YP), LRADC_CTRL1); + val = mxs_lradc_read_raw_channel(lradc, TS_CH_YP); + } else if (reg & LRADC_CTRL1_LRADC_IRQ(TS_CH_XM)) { + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(TS_CH_XM) | + LRADC_CTRL1_LRADC_IRQ(TS_CH_XM), LRADC_CTRL1); + val = mxs_lradc_read_raw_channel(lradc, TS_CH_XM); + } else if (reg & LRADC_CTRL1_LRADC_IRQ(TS_CH_YM)) { + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(TS_CH_YM) | + LRADC_CTRL1_LRADC_IRQ(TS_CH_YM), LRADC_CTRL1); + val = mxs_lradc_read_raw_channel(lradc, TS_CH_YM); + } else { + return -EIO; + } + + mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(2)); + mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(3)); + + return val; +} + +/* + * YP(open)--+-------------+ + * | |--+ + * | | | + * YM(-)--+-------------+ | + * +--------------+ + * | | + * XP(weak+) XM(open) + * + * "weak+" means 200k Ohm VDDIO + * (-) means GND + */ +static void mxs_lradc_setup_touch_detection(struct mxs_lradc *lradc) +{ + /* + * In order to detect a touch event the 'touch detect enable' bit + * enables: + * - a weak pullup to the X+ connector + * - a strong ground at the Y- connector + */ + mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0); + mxs_lradc_reg_set(lradc, mxs_lradc_touch_detect_bit(lradc), + LRADC_CTRL0); +} + +/* + * YP(meas)--+-------------+ + * | |--+ + * | | | + * YM(open)--+-------------+ | + * +--------------+ + * | | + * XP(+) XM(-) + * + * (+) means here 1.85 V + * (-) means here GND + */ +static void mxs_lradc_prepare_x_pos(struct mxs_lradc *lradc) +{ + mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0); + mxs_lradc_reg_set(lradc, mxs_lradc_drive_x_plate(lradc), LRADC_CTRL0); + + lradc->cur_plate = LRADC_SAMPLE_X; + mxs_lradc_setup_ts_channel(lradc, TS_CH_YP); +} + +/* + * YP(+)--+-------------+ + * | |--+ + * | | | + * YM(-)--+-------------+ | + * +--------------+ + * | | + * XP(open) XM(meas) + * + * (+) means here 1.85 V + * (-) means here GND + */ +static void mxs_lradc_prepare_y_pos(struct mxs_lradc *lradc) +{ + mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0); + mxs_lradc_reg_set(lradc, mxs_lradc_drive_y_plate(lradc), LRADC_CTRL0); + + lradc->cur_plate = LRADC_SAMPLE_Y; + mxs_lradc_setup_ts_channel(lradc, TS_CH_XM); +} + +/* + * YP(+)--+-------------+ + * | |--+ + * | | | + * YM(meas)--+-------------+ | + * +--------------+ + * | | + * XP(meas) XM(-) + * + * (+) means here 1.85 V + * (-) means here GND + */ +static void mxs_lradc_prepare_pressure(struct mxs_lradc *lradc) +{ + mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0); + mxs_lradc_reg_set(lradc, mxs_lradc_drive_pressure(lradc), LRADC_CTRL0); + + lradc->cur_plate = LRADC_SAMPLE_PRESSURE; + mxs_lradc_setup_ts_pressure(lradc, TS_CH_XP, TS_CH_YM); +} + +static void mxs_lradc_enable_touch_detection(struct mxs_lradc *lradc) +{ + mxs_lradc_setup_touch_detection(lradc); + + lradc->cur_plate = LRADC_TOUCH; + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ | + LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1); + mxs_lradc_reg_set(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1); +} + +static void mxs_lradc_report_ts_event(struct mxs_lradc *lradc) +{ + input_report_abs(lradc->ts_input, ABS_X, lradc->ts_x_pos); + input_report_abs(lradc->ts_input, ABS_Y, lradc->ts_y_pos); + input_report_abs(lradc->ts_input, ABS_PRESSURE, lradc->ts_pressure); + input_report_key(lradc->ts_input, BTN_TOUCH, 1); + input_sync(lradc->ts_input); +} + +static void mxs_lradc_complete_touch_event(struct mxs_lradc *lradc) +{ + mxs_lradc_setup_touch_detection(lradc); + lradc->cur_plate = LRADC_SAMPLE_VALID; + /* + * start a dummy conversion to burn time to settle the signals + * note: we are not interested in the conversion's value + */ + mxs_lradc_reg_wrt(lradc, 0, LRADC_CH(5)); + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ(5), LRADC_CTRL1); + mxs_lradc_reg_set(lradc, LRADC_CTRL1_LRADC_IRQ_EN(5), LRADC_CTRL1); + mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(1 << 5) | + LRADC_DELAY_KICK | LRADC_DELAY_DELAY(10), /* waste 5 ms */ + LRADC_DELAY(2)); +} + +/* + * in order to avoid false measurements, report only samples where + * the surface is still touched after the position measurement + */ +static void mxs_lradc_finish_touch_event(struct mxs_lradc *lradc, bool valid) +{ + /* if it is still touched, report the sample */ + if (valid && mxs_lradc_check_touch_event(lradc)) { + lradc->ts_valid = true; + mxs_lradc_report_ts_event(lradc); + } + + /* if it is even still touched, continue with the next measurement */ + if (mxs_lradc_check_touch_event(lradc)) { + mxs_lradc_prepare_y_pos(lradc); + return; + } + + if (lradc->ts_valid) { + /* signal the release */ + lradc->ts_valid = false; + input_report_key(lradc->ts_input, BTN_TOUCH, 0); + input_sync(lradc->ts_input); + } + + /* if it is released, wait for the next touch via IRQ */ + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ, LRADC_CTRL1); + mxs_lradc_reg_set(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1); +} + +/* touchscreen's state machine */ +static void mxs_lradc_handle_touch(struct mxs_lradc *lradc) +{ + int val; + + switch (lradc->cur_plate) { + case LRADC_TOUCH: + /* + * start with the Y-pos, because it uses nearly the same plate + * settings like the touch detection + */ + if (mxs_lradc_check_touch_event(lradc)) { + mxs_lradc_reg_clear(lradc, + LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, + LRADC_CTRL1); + mxs_lradc_prepare_y_pos(lradc); + } + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ, + LRADC_CTRL1); + return; + + case LRADC_SAMPLE_Y: + val = mxs_lradc_read_ts_channel(lradc); + if (val < 0) { + mxs_lradc_enable_touch_detection(lradc); /* re-start */ + return; + } + lradc->ts_y_pos = val; + mxs_lradc_prepare_x_pos(lradc); + return; + + case LRADC_SAMPLE_X: + val = mxs_lradc_read_ts_channel(lradc); + if (val < 0) { + mxs_lradc_enable_touch_detection(lradc); /* re-start */ + return; + } + lradc->ts_x_pos = val; + mxs_lradc_prepare_pressure(lradc); + return; + + case LRADC_SAMPLE_PRESSURE: + lradc->ts_pressure = + mxs_lradc_read_ts_pressure(lradc, TS_CH_XP, TS_CH_YM); + mxs_lradc_complete_touch_event(lradc); + return; + + case LRADC_SAMPLE_VALID: + val = mxs_lradc_read_ts_channel(lradc); /* ignore the value */ + mxs_lradc_finish_touch_event(lradc, 1); + break; + } +} + /* * Raw I/O operations */ @@ -262,21 +790,20 @@ static int mxs_lradc_read_raw(struct iio_dev *iio_dev, * Virtual channel 0 is always used here as the others are always not * used if doing raw sampling. */ - writel(LRADC_CTRL1_LRADC_IRQ_EN_MASK, - lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); - writel(0xff, lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR); + if (lradc->soc == IMX28_LRADC) + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_MX28_LRADC_IRQ_EN_MASK, + LRADC_CTRL1); + mxs_lradc_reg_clear(lradc, 0xff, LRADC_CTRL0); /* Clean the slot's previous content, then set new one. */ - writel(LRADC_CTRL4_LRADCSELECT_MASK(0), - lradc->base + LRADC_CTRL4 + STMP_OFFSET_REG_CLR); - writel(chan->channel, lradc->base + LRADC_CTRL4 + STMP_OFFSET_REG_SET); + mxs_lradc_reg_clear(lradc, LRADC_CTRL4_LRADCSELECT_MASK(0), LRADC_CTRL4); + mxs_lradc_reg_set(lradc, chan->channel, LRADC_CTRL4); - writel(0, lradc->base + LRADC_CH(0)); + mxs_lradc_reg_wrt(lradc, 0, LRADC_CH(0)); /* Enable the IRQ and start sampling the channel. */ - writel(LRADC_CTRL1_LRADC_IRQ_EN(0), - lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_SET); - writel(1 << 0, lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET); + mxs_lradc_reg_set(lradc, LRADC_CTRL1_LRADC_IRQ_EN(0), LRADC_CTRL1); + mxs_lradc_reg_set(lradc, 1 << 0, LRADC_CTRL0); /* Wait for completion on the channel, 1 second max. */ ret = wait_for_completion_killable_timeout(&lradc->completion, HZ); @@ -290,8 +817,7 @@ static int mxs_lradc_read_raw(struct iio_dev *iio_dev, ret = IIO_VAL_INT; err: - writel(LRADC_CTRL1_LRADC_IRQ_EN(0), - lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(0), LRADC_CTRL1); mutex_unlock(&lradc->lock); @@ -303,220 +829,33 @@ static const struct iio_info mxs_lradc_iio_info = { .read_raw = mxs_lradc_read_raw, }; -/* - * Touchscreen handling - */ -enum lradc_ts_plate { - LRADC_SAMPLE_X, - LRADC_SAMPLE_Y, - LRADC_SAMPLE_PRESSURE, -}; - -static int mxs_lradc_ts_touched(struct mxs_lradc *lradc) -{ - uint32_t reg; - - /* Enable touch detection. */ - writel(LRADC_CTRL0_PLATE_MASK, - lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR); - writel(LRADC_CTRL0_TOUCH_DETECT_ENABLE, - lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET); - - msleep(LRADC_TS_SAMPLE_DELAY_MS); - - reg = readl(lradc->base + LRADC_STATUS); - - return reg & LRADC_STATUS_TOUCH_DETECT_RAW; -} - -static int32_t mxs_lradc_ts_sample(struct mxs_lradc *lradc, - enum lradc_ts_plate plate, int change) -{ - unsigned long delay, jiff; - uint32_t reg, ctrl0 = 0, chan = 0; - /* The touchscreen always uses CTRL4 slot #7. */ - const uint8_t slot = 7; - uint32_t val; - - /* - * There are three correct configurations of the controller sampling - * the touchscreen, each of these configuration provides different - * information from the touchscreen. - * - * The following table describes the sampling configurations: - * +-------------+-------+-------+-------+ - * | Wire \ Axis | X | Y | Z | - * +---------------------+-------+-------+ - * | X+ (CH2) | HI | TS | TS | - * +-------------+-------+-------+-------+ - * | X- (CH4) | LO | SH | HI | - * +-------------+-------+-------+-------+ - * | Y+ (CH3) | SH | HI | HI | - * +-------------+-------+-------+-------+ - * | Y- (CH5) | TS | LO | SH | - * +-------------+-------+-------+-------+ - * - * HI ... strong '1' ; LO ... strong '0' - * SH ... sample here ; TS ... tri-state - * - * There are a few other ways of obtaining the Z coordinate - * (aka. pressure), but the one in the table seems to be the - * most reliable one. - */ - switch (plate) { - case LRADC_SAMPLE_X: - ctrl0 = LRADC_CTRL0_XPPSW | LRADC_CTRL0_XNNSW; - chan = 3; - break; - case LRADC_SAMPLE_Y: - ctrl0 = LRADC_CTRL0_YPPSW | LRADC_CTRL0_YNNSW; - chan = 4; - break; - case LRADC_SAMPLE_PRESSURE: - ctrl0 = LRADC_CTRL0_YPPSW | LRADC_CTRL0_XNNSW; - chan = 5; - break; - } - - if (change) { - writel(LRADC_CTRL0_PLATE_MASK, - lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR); - writel(ctrl0, lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET); - - writel(LRADC_CTRL4_LRADCSELECT_MASK(slot), - lradc->base + LRADC_CTRL4 + STMP_OFFSET_REG_CLR); - writel(chan << LRADC_CTRL4_LRADCSELECT_OFFSET(slot), - lradc->base + LRADC_CTRL4 + STMP_OFFSET_REG_SET); - } - - writel(0xffffffff, lradc->base + LRADC_CH(slot) + STMP_OFFSET_REG_CLR); - writel(1 << slot, lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET); - - delay = jiffies + msecs_to_jiffies(LRADC_TS_SAMPLE_DELAY_MS); - do { - jiff = jiffies; - reg = readl_relaxed(lradc->base + LRADC_CTRL1); - if (reg & LRADC_CTRL1_LRADC_IRQ(slot)) - break; - } while (time_before(jiff, delay)); - - writel(LRADC_CTRL1_LRADC_IRQ(slot), - lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); - - if (time_after_eq(jiff, delay)) - return -ETIMEDOUT; - - val = readl(lradc->base + LRADC_CH(slot)); - val &= LRADC_CH_VALUE_MASK; - - return val; -} - -static int32_t mxs_lradc_ts_sample_filter(struct mxs_lradc *lradc, - enum lradc_ts_plate plate) -{ - int32_t val, tot = 0; - int i; - - val = mxs_lradc_ts_sample(lradc, plate, 1); - - /* Delay a bit so the touchscreen is stable. */ - mdelay(2); - - for (i = 0; i < LRADC_TS_SAMPLE_AMOUNT; i++) { - val = mxs_lradc_ts_sample(lradc, plate, 0); - tot += val; - } - - return tot / LRADC_TS_SAMPLE_AMOUNT; -} - -static void mxs_lradc_ts_work(struct work_struct *ts_work) -{ - struct mxs_lradc *lradc = container_of(ts_work, - struct mxs_lradc, ts_work); - int val_x, val_y, val_p; - bool valid = false; - - while (mxs_lradc_ts_touched(lradc)) { - /* Disable touch detector so we can sample the touchscreen. */ - writel(LRADC_CTRL0_TOUCH_DETECT_ENABLE, - lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR); - - if (likely(valid)) { - input_report_abs(lradc->ts_input, ABS_X, val_x); - input_report_abs(lradc->ts_input, ABS_Y, val_y); - input_report_abs(lradc->ts_input, ABS_PRESSURE, val_p); - input_report_key(lradc->ts_input, BTN_TOUCH, 1); - input_sync(lradc->ts_input); - } - - valid = false; - - val_x = mxs_lradc_ts_sample_filter(lradc, LRADC_SAMPLE_X); - if (val_x < 0) - continue; - val_y = mxs_lradc_ts_sample_filter(lradc, LRADC_SAMPLE_Y); - if (val_y < 0) - continue; - val_p = mxs_lradc_ts_sample_filter(lradc, LRADC_SAMPLE_PRESSURE); - if (val_p < 0) - continue; - - valid = true; - } - - input_report_abs(lradc->ts_input, ABS_PRESSURE, 0); - input_report_key(lradc->ts_input, BTN_TOUCH, 0); - input_sync(lradc->ts_input); - - /* Do not restart the TS IRQ if the driver is shutting down. */ - if (lradc->stop_touchscreen) - return; - - /* Restart the touchscreen interrupts. */ - writel(LRADC_CTRL1_TOUCH_DETECT_IRQ, - lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); - writel(LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, - lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_SET); -} - static int mxs_lradc_ts_open(struct input_dev *dev) { struct mxs_lradc *lradc = input_get_drvdata(dev); - /* The touchscreen is starting. */ - lradc->stop_touchscreen = false; - /* Enable the touch-detect circuitry. */ - writel(LRADC_CTRL0_TOUCH_DETECT_ENABLE, - lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET); - - /* Enable the touch-detect IRQ. */ - writel(LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, - lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_SET); + mxs_lradc_enable_touch_detection(lradc); return 0; } -static void mxs_lradc_ts_close(struct input_dev *dev) +static void mxs_lradc_disable_ts(struct mxs_lradc *lradc) { - struct mxs_lradc *lradc = input_get_drvdata(dev); + /* stop all interrupts from firing */ + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN | + LRADC_CTRL1_LRADC_IRQ_EN(2) | LRADC_CTRL1_LRADC_IRQ_EN(3) | + LRADC_CTRL1_LRADC_IRQ_EN(4) | LRADC_CTRL1_LRADC_IRQ_EN(5), + LRADC_CTRL1); - /* Indicate the touchscreen is stopping. */ - lradc->stop_touchscreen = true; - mb(); - - /* Wait until touchscreen thread finishes any possible remnants. */ - cancel_work_sync(&lradc->ts_work); + /* Power-down touchscreen touch-detect circuitry. */ + mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0); +} - /* Disable touchscreen touch-detect IRQ. */ - writel(LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, - lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); +static void mxs_lradc_ts_close(struct input_dev *dev) +{ + struct mxs_lradc *lradc = input_get_drvdata(dev); - /* Power-down touchscreen touch-detect circuitry. */ - writel(LRADC_CTRL0_TOUCH_DETECT_ENABLE, - lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR); + mxs_lradc_disable_ts(lradc); } static int mxs_lradc_ts_register(struct mxs_lradc *lradc) @@ -529,10 +868,8 @@ static int mxs_lradc_ts_register(struct mxs_lradc *lradc) return 0; input = input_allocate_device(); - if (!input) { - dev_err(dev, "Failed to allocate TS device!\n"); + if (!input) return -ENOMEM; - } input->name = DRIVER_NAME; input->id.bustype = BUS_HOST; @@ -562,8 +899,7 @@ static void mxs_lradc_ts_unregister(struct mxs_lradc *lradc) if (!lradc->use_touchscreen) return; - cancel_work_sync(&lradc->ts_work); - + mxs_lradc_disable_ts(lradc); input_unregister_device(lradc->ts_input); } @@ -576,31 +912,24 @@ static irqreturn_t mxs_lradc_handle_irq(int irq, void *data) struct mxs_lradc *lradc = iio_priv(iio); unsigned long reg = readl(lradc->base + LRADC_CTRL1); const uint32_t ts_irq_mask = - LRADC_CTRL1_TOUCH_DETECT_IRQ_EN | - LRADC_CTRL1_TOUCH_DETECT_IRQ; + LRADC_CTRL1_TOUCH_DETECT_IRQ | + LRADC_CTRL1_LRADC_IRQ(2) | + LRADC_CTRL1_LRADC_IRQ(3) | + LRADC_CTRL1_LRADC_IRQ(4) | + LRADC_CTRL1_LRADC_IRQ(5); - if (!(reg & LRADC_CTRL1_LRADC_IRQ_MASK)) + if (!(reg & mxs_lradc_irq_mask(lradc))) return IRQ_NONE; - /* - * Touchscreen IRQ handling code has priority and therefore - * is placed here. In case touchscreen IRQ arrives, disable - * it ASAP - */ - if (reg & LRADC_CTRL1_TOUCH_DETECT_IRQ) { - writel(ts_irq_mask, - lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); - if (!lradc->stop_touchscreen) - schedule_work(&lradc->ts_work); - } + if (lradc->use_touchscreen && (reg & ts_irq_mask)) + mxs_lradc_handle_touch(lradc); if (iio_buffer_enabled(iio)) iio_trigger_poll(iio->trig, iio_get_time_ns()); else if (reg & LRADC_CTRL1_LRADC_IRQ(0)) complete(&lradc->completion); - writel(reg & LRADC_CTRL1_LRADC_IRQ_MASK, - lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); + mxs_lradc_reg_clear(lradc, reg & mxs_lradc_irq_mask(lradc), LRADC_CTRL1); return IRQ_HANDLED; } @@ -619,19 +948,13 @@ static irqreturn_t mxs_lradc_trigger_handler(int irq, void *p) for_each_set_bit(i, iio->active_scan_mask, LRADC_MAX_TOTAL_CHANS) { lradc->buffer[j] = readl(lradc->base + LRADC_CH(j)); - writel(chan_value, lradc->base + LRADC_CH(j)); + mxs_lradc_reg_wrt(lradc, chan_value, LRADC_CH(j)); lradc->buffer[j] &= LRADC_CH_VALUE_MASK; lradc->buffer[j] /= LRADC_DELAY_TIMER_LOOP; j++; } - if (iio->scan_timestamp) { - s64 *timestamp = (s64 *)((u8 *)lradc->buffer + - ALIGN(j, sizeof(s64))); - *timestamp = pf->timestamp; - } - - iio_push_to_buffers(iio, (u8 *)lradc->buffer); + iio_push_to_buffers_with_timestamp(iio, lradc->buffer, pf->timestamp); iio_trigger_notify_done(iio->trig); @@ -644,7 +967,7 @@ static int mxs_lradc_configure_trigger(struct iio_trigger *trig, bool state) struct mxs_lradc *lradc = iio_priv(iio); const uint32_t st = state ? STMP_OFFSET_REG_SET : STMP_OFFSET_REG_CLR; - writel(LRADC_DELAY_KICK, lradc->base + LRADC_DELAY(0) + st); + mxs_lradc_reg_wrt(lradc, LRADC_DELAY_KICK, LRADC_DELAY(0) + st); return 0; } @@ -716,38 +1039,30 @@ static int mxs_lradc_buffer_preenable(struct iio_dev *iio) goto err_mem; } - ret = iio_sw_buffer_preenable(iio); - if (ret < 0) - goto err_buf; - - writel(LRADC_CTRL1_LRADC_IRQ_EN_MASK, - lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); - writel(0xff, lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR); + if (lradc->soc == IMX28_LRADC) + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_MX28_LRADC_IRQ_EN_MASK, + LRADC_CTRL1); + mxs_lradc_reg_clear(lradc, 0xff, LRADC_CTRL0); for_each_set_bit(chan, iio->active_scan_mask, LRADC_MAX_TOTAL_CHANS) { ctrl4_set |= chan << LRADC_CTRL4_LRADCSELECT_OFFSET(ofs); ctrl4_clr |= LRADC_CTRL4_LRADCSELECT_MASK(ofs); ctrl1_irq |= LRADC_CTRL1_LRADC_IRQ_EN(ofs); - writel(chan_value, lradc->base + LRADC_CH(ofs)); + mxs_lradc_reg_wrt(lradc, chan_value, LRADC_CH(ofs)); bitmap_set(&enable, ofs, 1); ofs++; } - writel(LRADC_DELAY_TRIGGER_LRADCS_MASK | LRADC_DELAY_KICK, - lradc->base + LRADC_DELAY(0) + STMP_OFFSET_REG_CLR); - - writel(ctrl4_clr, lradc->base + LRADC_CTRL4 + STMP_OFFSET_REG_CLR); - writel(ctrl4_set, lradc->base + LRADC_CTRL4 + STMP_OFFSET_REG_SET); - - writel(ctrl1_irq, lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_SET); - - writel(enable << LRADC_DELAY_TRIGGER_LRADCS_OFFSET, - lradc->base + LRADC_DELAY(0) + STMP_OFFSET_REG_SET); + mxs_lradc_reg_clear(lradc, LRADC_DELAY_TRIGGER_LRADCS_MASK | + LRADC_DELAY_KICK, LRADC_DELAY(0)); + mxs_lradc_reg_clear(lradc, ctrl4_clr, LRADC_CTRL4); + mxs_lradc_reg_set(lradc, ctrl4_set, LRADC_CTRL4); + mxs_lradc_reg_set(lradc, ctrl1_irq, LRADC_CTRL1); + mxs_lradc_reg_set(lradc, enable << LRADC_DELAY_TRIGGER_LRADCS_OFFSET, + LRADC_DELAY(0)); return 0; -err_buf: - kfree(lradc->buffer); err_mem: mutex_unlock(&lradc->lock); return ret; @@ -757,12 +1072,13 @@ static int mxs_lradc_buffer_postdisable(struct iio_dev *iio) { struct mxs_lradc *lradc = iio_priv(iio); - writel(LRADC_DELAY_TRIGGER_LRADCS_MASK | LRADC_DELAY_KICK, - lradc->base + LRADC_DELAY(0) + STMP_OFFSET_REG_CLR); + mxs_lradc_reg_clear(lradc, LRADC_DELAY_TRIGGER_LRADCS_MASK | + LRADC_DELAY_KICK, LRADC_DELAY(0)); - writel(0xff, lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR); - writel(LRADC_CTRL1_LRADC_IRQ_EN_MASK, - lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); + mxs_lradc_reg_clear(lradc, 0xff, LRADC_CTRL0); + if (lradc->soc == IMX28_LRADC) + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_MX28_LRADC_IRQ_EN_MASK, + LRADC_CTRL1); kfree(lradc->buffer); mutex_unlock(&lradc->lock); @@ -857,24 +1173,25 @@ static int mxs_lradc_hw_init(struct mxs_lradc *lradc) return ret; /* Configure DELAY CHANNEL 0 for generic ADC sampling. */ - writel(adc_cfg, lradc->base + LRADC_DELAY(0)); + mxs_lradc_reg_wrt(lradc, adc_cfg, LRADC_DELAY(0)); /* Disable remaining DELAY CHANNELs */ - writel(0, lradc->base + LRADC_DELAY(1)); - writel(0, lradc->base + LRADC_DELAY(2)); - writel(0, lradc->base + LRADC_DELAY(3)); + mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(1)); + mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(2)); + mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(3)); /* Configure the touchscreen type */ - writel(LRADC_CTRL0_TOUCH_SCREEN_TYPE, - lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR); + if (lradc->soc == IMX28_LRADC) { + mxs_lradc_reg_clear(lradc, LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE, + LRADC_CTRL0); - if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_5WIRE) { - writel(LRADC_CTRL0_TOUCH_SCREEN_TYPE, - lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET); + if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_5WIRE) + mxs_lradc_reg_set(lradc, LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE, + LRADC_CTRL0); } /* Start internal temperature sensing. */ - writel(0, lradc->base + LRADC_CTRL2); + mxs_lradc_reg_wrt(lradc, 0, LRADC_CTRL2); return 0; } @@ -883,11 +1200,10 @@ static void mxs_lradc_hw_stop(struct mxs_lradc *lradc) { int i; - writel(LRADC_CTRL1_LRADC_IRQ_EN_MASK, - lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); + mxs_lradc_reg_clear(lradc, mxs_lradc_irq_en_mask(lradc), LRADC_CTRL1); for (i = 0; i < LRADC_MAX_DELAY_CHANS; i++) - writel(0, lradc->base + LRADC_DELAY(i)); + mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(i)); } static const struct of_device_id mxs_lradc_dt_ids[] = { @@ -897,6 +1213,52 @@ static const struct of_device_id mxs_lradc_dt_ids[] = { }; MODULE_DEVICE_TABLE(of, mxs_lradc_dt_ids); +static int mxs_lradc_probe_touchscreen(struct mxs_lradc *lradc, + struct device_node *lradc_node) +{ + int ret; + u32 ts_wires = 0, adapt; + + ret = of_property_read_u32(lradc_node, "fsl,lradc-touchscreen-wires", + &ts_wires); + if (ret) + return -ENODEV; /* touchscreen feature disabled */ + + switch (ts_wires) { + case 4: + lradc->use_touchscreen = MXS_LRADC_TOUCHSCREEN_4WIRE; + break; + case 5: + if (lradc->soc == IMX28_LRADC) { + lradc->use_touchscreen = MXS_LRADC_TOUCHSCREEN_5WIRE; + break; + } + /* fall through an error message for i.MX23 */ + default: + dev_err(lradc->dev, + "Unsupported number of touchscreen wires (%d)\n", + ts_wires); + return -EINVAL; + } + + lradc->over_sample_cnt = 4; + ret = of_property_read_u32(lradc_node, "fsl,ave-ctrl", &adapt); + if (ret == 0) + lradc->over_sample_cnt = adapt; + + lradc->over_sample_delay = 2; + ret = of_property_read_u32(lradc_node, "fsl,ave-delay", &adapt); + if (ret == 0) + lradc->over_sample_delay = adapt; + + lradc->settling_delay = 10; + ret = of_property_read_u32(lradc_node, "fsl,settling", &adapt); + if (ret == 0) + lradc->settling_delay = adapt; + + return 0; +} + static int mxs_lradc_probe(struct platform_device *pdev) { const struct of_device_id *of_id = @@ -908,8 +1270,7 @@ static int mxs_lradc_probe(struct platform_device *pdev) struct mxs_lradc *lradc; struct iio_dev *iio; struct resource *iores; - uint32_t ts_wires = 0; - int ret = 0; + int ret = 0, touch_ret; int i; /* Allocate the IIO device. */ @@ -920,6 +1281,7 @@ static int mxs_lradc_probe(struct platform_device *pdev) } lradc = iio_priv(iio); + lradc->soc = (enum mxs_lradc_id)of_id->data; /* Grab the memory area */ iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); @@ -928,20 +1290,18 @@ static int mxs_lradc_probe(struct platform_device *pdev) if (IS_ERR(lradc->base)) return PTR_ERR(lradc->base); - INIT_WORK(&lradc->ts_work, mxs_lradc_ts_work); + lradc->clk = devm_clk_get(&pdev->dev, NULL); + if (IS_ERR(lradc->clk)) { + dev_err(dev, "Failed to get the delay unit clock\n"); + return PTR_ERR(lradc->clk); + } + ret = clk_prepare_enable(lradc->clk); + if (ret != 0) { + dev_err(dev, "Failed to enable the delay unit clock\n"); + return ret; + } - /* Check if touchscreen is enabled in DT. */ - ret = of_property_read_u32(node, "fsl,lradc-touchscreen-wires", - &ts_wires); - if (ret) - dev_info(dev, "Touchscreen not enabled.\n"); - else if (ts_wires == 4) - lradc->use_touchscreen = MXS_LRADC_TOUCHSCREEN_4WIRE; - else if (ts_wires == 5) - lradc->use_touchscreen = MXS_LRADC_TOUCHSCREEN_5WIRE; - else - dev_warn(dev, "Unsupported number of touchscreen wires (%d)\n", - ts_wires); + touch_ret = mxs_lradc_probe_touchscreen(lradc, node); /* Grab all IRQ sources */ for (i = 0; i < of_cfg->irq_count; i++) { @@ -985,9 +1345,11 @@ static int mxs_lradc_probe(struct platform_device *pdev) goto err_dev; /* Register the touchscreen input device. */ - ret = mxs_lradc_ts_register(lradc); - if (ret) - goto err_dev; + if (touch_ret == 0) { + ret = mxs_lradc_ts_register(lradc); + if (ret) + goto err_ts_register; + } /* Register IIO device. */ ret = iio_device_register(iio); @@ -1000,6 +1362,8 @@ static int mxs_lradc_probe(struct platform_device *pdev) err_ts: mxs_lradc_ts_unregister(lradc); +err_ts_register: + mxs_lradc_hw_stop(lradc); err_dev: mxs_lradc_trigger_remove(iio); err_trig: @@ -1012,14 +1376,13 @@ static int mxs_lradc_remove(struct platform_device *pdev) struct iio_dev *iio = platform_get_drvdata(pdev); struct mxs_lradc *lradc = iio_priv(iio); + iio_device_unregister(iio); mxs_lradc_ts_unregister(lradc); - mxs_lradc_hw_stop(lradc); - - iio_device_unregister(iio); - iio_triggered_buffer_cleanup(iio); mxs_lradc_trigger_remove(iio); + iio_triggered_buffer_cleanup(iio); + clk_disable_unprepare(lradc->clk); return 0; } @@ -1038,3 +1401,4 @@ module_platform_driver(mxs_lradc_driver); MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); MODULE_DESCRIPTION("Freescale i.MX28 LRADC driver"); MODULE_LICENSE("GPL v2"); +MODULE_ALIAS("platform:" DRIVER_NAME); diff --git a/drivers/staging/iio/adc/spear_adc.c b/drivers/staging/iio/adc/spear_adc.c index 20f2d55..970d9ed 100644 --- a/drivers/staging/iio/adc/spear_adc.c +++ b/drivers/staging/iio/adc/spear_adc.c @@ -146,7 +146,6 @@ static int spear_read_raw(struct iio_dev *indio_dev, long mask) { struct spear_adc_info *info = iio_priv(indio_dev); - u32 scale_mv; u32 status; switch (mask) { @@ -168,10 +167,9 @@ static int spear_read_raw(struct iio_dev *indio_dev, return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: - scale_mv = (info->vref_external * 1000) >> DATA_BITS; - *val = scale_mv / 1000; - *val2 = (scale_mv % 1000) * 1000; - return IIO_VAL_INT_PLUS_MICRO; + *val = info->vref_external; + *val2 = DATA_BITS; + return IIO_VAL_FRACTIONAL_LOG2; } return -EINVAL; @@ -320,7 +318,7 @@ static int spear_adc_probe(struct platform_device *pdev) return -ENOMEM; } info->adc_base_spear3xx = - (struct adc_regs_spear3xx *)info->adc_base_spear6xx; + (struct adc_regs_spear3xx __iomem *)info->adc_base_spear6xx; info->clk = clk_get(dev, NULL); if (IS_ERR(info->clk)) { @@ -335,7 +333,7 @@ static int spear_adc_probe(struct platform_device *pdev) } irq = platform_get_irq(pdev, 0); - if ((irq < 0) || (irq >= NR_IRQS)) { + if (irq <= 0) { dev_err(dev, "failed getting interrupt resource\n"); ret = -EINVAL; goto errout3; |