Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Common library for ADIS16XXX devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2012 Analog Devices Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *   Author: Lars-Peter Clausen <lars@metafoo.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/spi/spi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/iio/buffer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/iio/trigger_consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/iio/triggered_buffer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/iio/imu/adis.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static int adis_update_scan_mode_burst(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	const unsigned long *scan_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct adis *adis = iio_device_get_drvdata(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	unsigned int burst_length, burst_max_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	u8 *tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	burst_length = adis->data->burst_len + adis->burst_extra_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	if (adis->data->burst_max_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		burst_max_length = adis->data->burst_max_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		burst_max_length = burst_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	adis->xfer = kcalloc(2, sizeof(*adis->xfer), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	if (!adis->xfer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	adis->buffer = kzalloc(burst_max_length + sizeof(u16), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (!adis->buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		kfree(adis->xfer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		adis->xfer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	tx = adis->buffer + burst_max_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	tx[0] = ADIS_READ_REG(adis->data->burst_reg_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	tx[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	adis->xfer[0].tx_buf = tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	adis->xfer[0].bits_per_word = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	adis->xfer[0].len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	adis->xfer[1].rx_buf = adis->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	adis->xfer[1].bits_per_word = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	adis->xfer[1].len = burst_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	spi_message_init(&adis->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	spi_message_add_tail(&adis->xfer[0], &adis->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	spi_message_add_tail(&adis->xfer[1], &adis->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) int adis_update_scan_mode(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	const unsigned long *scan_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct adis *adis = iio_device_get_drvdata(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	const struct iio_chan_spec *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	unsigned int scan_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	unsigned int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	__be16 *tx, *rx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	kfree(adis->xfer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	kfree(adis->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (adis->data->burst_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return adis_update_scan_mode_burst(indio_dev, scan_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	scan_count = indio_dev->scan_bytes / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	adis->xfer = kcalloc(scan_count + 1, sizeof(*adis->xfer), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (!adis->xfer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	adis->buffer = kcalloc(indio_dev->scan_bytes, 2, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (!adis->buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		kfree(adis->xfer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		adis->xfer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	rx = adis->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	tx = rx + scan_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	spi_message_init(&adis->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	for (j = 0; j <= scan_count; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		adis->xfer[j].bits_per_word = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		if (j != scan_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			adis->xfer[j].cs_change = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		adis->xfer[j].len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		adis->xfer[j].delay.value = adis->data->read_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		adis->xfer[j].delay.unit = SPI_DELAY_UNIT_USECS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		if (j < scan_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			adis->xfer[j].tx_buf = &tx[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		if (j >= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			adis->xfer[j].rx_buf = &rx[j - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		spi_message_add_tail(&adis->xfer[j], &adis->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	chan = indio_dev->channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	for (i = 0; i < indio_dev->num_channels; i++, chan++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		if (!test_bit(chan->scan_index, scan_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		if (chan->scan_type.storagebits == 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			*tx++ = cpu_to_be16((chan->address + 2) << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		*tx++ = cpu_to_be16(chan->address << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) EXPORT_SYMBOL_GPL(adis_update_scan_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static irqreturn_t adis_trigger_handler(int irq, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct iio_poll_func *pf = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct iio_dev *indio_dev = pf->indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct adis *adis = iio_device_get_drvdata(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (adis->data->has_paging) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		mutex_lock(&adis->state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		if (adis->current_page != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			adis->tx[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			spi_write(adis->spi, adis->tx, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	ret = spi_sync(adis->spi, &adis->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		dev_err(&adis->spi->dev, "Failed to read data: %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (adis->data->has_paging) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		adis->current_page = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		mutex_unlock(&adis->state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	iio_push_to_buffers_with_timestamp(indio_dev, adis->buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		pf->timestamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	iio_trigger_notify_done(indio_dev->trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static void adis_buffer_cleanup(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	struct adis *adis = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	kfree(adis->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	kfree(adis->xfer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * devm_adis_setup_buffer_and_trigger() - Sets up buffer and trigger for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  *					  the managed adis device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  * @adis: The adis device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  * @indio_dev: The IIO device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  * @trigger_handler: Optional trigger handler, may be NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  * Returns 0 on success, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  * This function sets up the buffer and trigger for a adis devices.  If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  * 'trigger_handler' is NULL the default trigger handler will be used. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  * default trigger handler will simply read the registers assigned to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  * currently active channels.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) devm_adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 				   irq_handler_t trigger_handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (!trigger_handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		trigger_handler = adis_trigger_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	ret = devm_iio_triggered_buffer_setup(&adis->spi->dev, indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 					      &iio_pollfunc_store_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 					      trigger_handler, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (adis->spi->irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		ret = devm_adis_probe_trigger(adis, indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	return devm_add_action_or_reset(&adis->spi->dev, adis_buffer_cleanup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 					adis);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) EXPORT_SYMBOL_GPL(devm_adis_setup_buffer_and_trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)