| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * AD5421 Digital to analog converters driver |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright 2011 Analog Devices Inc. |
|---|
| 5 | | - * |
|---|
| 6 | | - * Licensed under the GPL-2. |
|---|
| 7 | 6 | */ |
|---|
| 8 | 7 | |
|---|
| 9 | 8 | #include <linux/device.h> |
|---|
| .. | .. |
|---|
| 63 | 62 | * @current_range: current range which the device is configured for |
|---|
| 64 | 63 | * @data: spi transfer buffers |
|---|
| 65 | 64 | * @fault_mask: software masking of events |
|---|
| 65 | + * @lock: lock to protect the data buffer during SPI ops |
|---|
| 66 | 66 | */ |
|---|
| 67 | 67 | struct ad5421_state { |
|---|
| 68 | 68 | struct spi_device *spi; |
|---|
| 69 | 69 | unsigned int ctrl; |
|---|
| 70 | 70 | enum ad5421_current_range current_range; |
|---|
| 71 | 71 | unsigned int fault_mask; |
|---|
| 72 | + struct mutex lock; |
|---|
| 72 | 73 | |
|---|
| 73 | 74 | /* |
|---|
| 74 | 75 | * DMA (thus cache coherency maintenance) requires the |
|---|
| .. | .. |
|---|
| 143 | 144 | static int ad5421_write(struct iio_dev *indio_dev, unsigned int reg, |
|---|
| 144 | 145 | unsigned int val) |
|---|
| 145 | 146 | { |
|---|
| 147 | + struct ad5421_state *st = iio_priv(indio_dev); |
|---|
| 146 | 148 | int ret; |
|---|
| 147 | 149 | |
|---|
| 148 | | - mutex_lock(&indio_dev->mlock); |
|---|
| 150 | + mutex_lock(&st->lock); |
|---|
| 149 | 151 | ret = ad5421_write_unlocked(indio_dev, reg, val); |
|---|
| 150 | | - mutex_unlock(&indio_dev->mlock); |
|---|
| 152 | + mutex_unlock(&st->lock); |
|---|
| 151 | 153 | |
|---|
| 152 | 154 | return ret; |
|---|
| 153 | 155 | } |
|---|
| .. | .. |
|---|
| 167 | 169 | }, |
|---|
| 168 | 170 | }; |
|---|
| 169 | 171 | |
|---|
| 170 | | - mutex_lock(&indio_dev->mlock); |
|---|
| 172 | + mutex_lock(&st->lock); |
|---|
| 171 | 173 | |
|---|
| 172 | 174 | st->data[0].d32 = cpu_to_be32((1 << 23) | (reg << 16)); |
|---|
| 173 | 175 | |
|---|
| .. | .. |
|---|
| 175 | 177 | if (ret >= 0) |
|---|
| 176 | 178 | ret = be32_to_cpu(st->data[1].d32) & 0xffff; |
|---|
| 177 | 179 | |
|---|
| 178 | | - mutex_unlock(&indio_dev->mlock); |
|---|
| 180 | + mutex_unlock(&st->lock); |
|---|
| 179 | 181 | |
|---|
| 180 | 182 | return ret; |
|---|
| 181 | 183 | } |
|---|
| .. | .. |
|---|
| 186 | 188 | struct ad5421_state *st = iio_priv(indio_dev); |
|---|
| 187 | 189 | unsigned int ret; |
|---|
| 188 | 190 | |
|---|
| 189 | | - mutex_lock(&indio_dev->mlock); |
|---|
| 191 | + mutex_lock(&st->lock); |
|---|
| 190 | 192 | |
|---|
| 191 | 193 | st->ctrl &= ~clr; |
|---|
| 192 | 194 | st->ctrl |= set; |
|---|
| 193 | 195 | |
|---|
| 194 | 196 | ret = ad5421_write_unlocked(indio_dev, AD5421_REG_CTRL, st->ctrl); |
|---|
| 195 | 197 | |
|---|
| 196 | | - mutex_unlock(&indio_dev->mlock); |
|---|
| 198 | + mutex_unlock(&st->lock); |
|---|
| 197 | 199 | |
|---|
| 198 | 200 | return ret; |
|---|
| 199 | 201 | } |
|---|
| .. | .. |
|---|
| 401 | 403 | return -EINVAL; |
|---|
| 402 | 404 | } |
|---|
| 403 | 405 | |
|---|
| 404 | | - mutex_lock(&indio_dev->mlock); |
|---|
| 406 | + mutex_lock(&st->lock); |
|---|
| 405 | 407 | if (state) |
|---|
| 406 | 408 | st->fault_mask |= mask; |
|---|
| 407 | 409 | else |
|---|
| 408 | 410 | st->fault_mask &= ~mask; |
|---|
| 409 | | - mutex_unlock(&indio_dev->mlock); |
|---|
| 411 | + mutex_unlock(&st->lock); |
|---|
| 410 | 412 | |
|---|
| 411 | 413 | return 0; |
|---|
| 412 | 414 | } |
|---|
| .. | .. |
|---|
| 485 | 487 | |
|---|
| 486 | 488 | st->spi = spi; |
|---|
| 487 | 489 | |
|---|
| 488 | | - indio_dev->dev.parent = &spi->dev; |
|---|
| 489 | 490 | indio_dev->name = "ad5421"; |
|---|
| 490 | 491 | indio_dev->info = &ad5421_info; |
|---|
| 491 | 492 | indio_dev->modes = INDIO_DIRECT_MODE; |
|---|
| 492 | 493 | indio_dev->channels = ad5421_channels; |
|---|
| 493 | 494 | indio_dev->num_channels = ARRAY_SIZE(ad5421_channels); |
|---|
| 494 | 495 | |
|---|
| 496 | + mutex_init(&st->lock); |
|---|
| 497 | + |
|---|
| 495 | 498 | st->ctrl = AD5421_CTRL_WATCHDOG_DISABLE | |
|---|
| 496 | 499 | AD5421_CTRL_AUTO_FAULT_READBACK; |
|---|
| 497 | 500 | |
|---|