Modified: trunk/drivers/staging/iio/gyro/adis16260_core.c (8697 => 8698)
--- trunk/drivers/staging/iio/gyro/adis16260_core.c 2010-05-10 08:02:46 UTC (rev 8697)
+++ trunk/drivers/staging/iio/gyro/adis16260_core.c 2010-05-10 08:25:01 UTC (rev 8698)
@@ -27,10 +27,7 @@
#define DRIVER_NAME "adis16260"
-/* At the moment the spi framework doesn't allow global setting of cs_change.
- * It's in the likely to be added comment at the top of spi.h.
- * This means that use cannot be made of spi_write etc.
- */
+static int adis16260_check_status(struct device *dev);
/**
* adis16260_spi_write_reg_8() - write single byte to a register
@@ -38,7 +35,7 @@
* @reg_address: the address of the register to be written
* @val: the value to write
**/
-int adis16260_spi_write_reg_8(struct device *dev,
+static int adis16260_spi_write_reg_8(struct device *dev,
u8 reg_address,
u8 val)
{
@@ -77,11 +74,13 @@
.bits_per_word = 8,
.len = 2,
.cs_change = 1,
+ .delay_usecs = 20,
}, {
.tx_buf = st->tx + 2,
.bits_per_word = 8,
.len = 2,
.cs_change = 1,
+ .delay_usecs = 20,
},
};
@@ -121,11 +120,13 @@
.bits_per_word = 8,
.len = 2,
.cs_change = 1,
+ .delay_usecs = 30,
}, {
.rx_buf = st->rx,
.bits_per_word = 8,
.len = 2,
.cs_change = 1,
+ .delay_usecs = 30,
},
};
@@ -151,103 +152,6 @@
return ret;
}
-/**
- * adis16260_spi_read_burst() - read all data registers
- * @dev: device associated with child of actual device (iio_dev or iio_trig)
- * @rx: somewhere to pass back the value read (min size is 24 bytes)
- **/
-int adis16260_spi_read_burst(struct device *dev, u8 *rx)
-{
- struct spi_message msg;
- struct iio_dev *indio_dev = dev_get_drvdata(dev);
- struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
- u32 old_speed_hz = st->us->max_speed_hz;
- int ret;
-
- struct spi_transfer xfers[] = {
- {
- .tx_buf = st->tx,
- .bits_per_word = 8,
- .len = 2,
- .cs_change = 0,
- }, {
- .rx_buf = rx,
- .bits_per_word = 8,
- .len = 24,
- .cs_change = 1,
- },
- };
-
- mutex_lock(&st->buf_lock);
- st->tx[0] = ADIS16260_READ_REG(ADIS16260_GLOB_CMD);
- st->tx[1] = 0;
-
- spi_message_init(&msg);
- spi_message_add_tail(&xfers[0], &msg);
- spi_message_add_tail(&xfers[1], &msg);
-
- st->us->max_speed_hz = min(ADIS16260_SPI_BURST, old_speed_hz);
- spi_setup(st->us);
-
- ret = spi_sync(st->us, &msg);
- if (ret)
- dev_err(&st->us->dev, "problem when burst reading");
-
- st->us->max_speed_hz = old_speed_hz;
- spi_setup(st->us);
- mutex_unlock(&st->buf_lock);
- return ret;
-}
-
-/**
- * adis16260_spi_read_sequence() - read a sequence of 16-bit registers
- * @dev: device associated with child of actual device (iio_dev or iio_trig)
- * @tx: register addresses in bytes 0,2,4,6... (min size is 2*num bytes)
- * @rx: somewhere to pass back the value read (min size is 2*num bytes)
- **/
-int adis16260_spi_read_sequence(struct device *dev,
- u8 *tx, u8 *rx, int num)
-{
- struct spi_message msg;
- struct spi_transfer *xfers;
- struct iio_dev *indio_dev = dev_get_drvdata(dev);
- struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
- int ret, i;
-
- xfers = kzalloc(num + 1, GFP_KERNEL);
- if (xfers == NULL) {
- dev_err(&st->us->dev, "memory alloc failed");
- ret = -ENOMEM;
- goto error_ret;
- }
-
- /* tx: |add1|addr2|addr3|...|addrN |zero|
- * rx: |zero|res1 |res2 |...|resN-1|resN| */
- spi_message_init(&msg);
- for (i = 0; i < num + 1; i++) {
- if (i > 0)
- xfers[i].rx_buf = st->rx + 2*(i - 1);
- if (i < num)
- xfers[i].tx_buf = st->tx + 2*i;
- xfers[i].bits_per_word = 8;
- xfers[i].len = 2;
- xfers[i].cs_change = 1;
- spi_message_add_tail(&xfers[i], &msg);
- }
-
- mutex_lock(&st->buf_lock);
-
- ret = spi_sync(st->us, &msg);
- if (ret)
- dev_err(&st->us->dev, "problem when reading sequence");
-
- mutex_unlock(&st->buf_lock);
- kfree(xfers);
-
-error_ret:
- return ret;
-}
-
static ssize_t adis16260_spi_read_signed(struct device *dev,
struct device_attribute *attr,
char *buf,
@@ -286,7 +190,7 @@
return sprintf(buf, "%u\n", val & 0x0FFF);
}
-static ssize_t adis16260_read_14bit_signed(struct device *dev,
+static ssize_t adis16260_read_12bit_signed(struct device *dev,
struct device_attribute *attr,
char *buf)
{
@@ -295,13 +199,13 @@
/* Take the iio_dev status lock */
mutex_lock(&indio_dev->mlock);
- ret = adis16260_spi_read_signed(dev, attr, buf, 14);
+ ret = adis16260_spi_read_signed(dev, attr, buf, 12);
mutex_unlock(&indio_dev->mlock);
return ret;
}
-static ssize_t adis16260_read_12bit_signed(struct device *dev,
+static ssize_t adis16260_read_14bit_signed(struct device *dev,
struct device_attribute *attr,
char *buf)
{
@@ -310,7 +214,7 @@
/* Take the iio_dev status lock */
mutex_lock(&indio_dev->mlock);
- ret = adis16260_spi_read_signed(dev, attr, buf, 12);
+ ret = adis16260_spi_read_signed(dev, attr, buf, 14);
mutex_unlock(&indio_dev->mlock);
return ret;
@@ -387,6 +291,18 @@
return ret ? ret : len;
}
+static int adis16260_reset(struct device *dev)
+{
+ int ret;
+ ret = adis16260_spi_write_reg_8(dev,
+ ADIS16260_GLOB_CMD,
+ ADIS16260_GLOB_CMD_SW_RESET);
+ if (ret)
+ dev_err(dev, "problem resetting device");
+
+ return ret;
+}
+
static ssize_t adis16260_write_reset(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len)
@@ -402,8 +318,6 @@
return -1;
}
-
-
int adis16260_set_irq(struct device *dev, bool enable)
{
int ret;
@@ -426,20 +340,8 @@
return ret;
}
-int adis16260_reset(struct device *dev)
-{
- int ret;
- ret = adis16260_spi_write_reg_8(dev,
- ADIS16260_GLOB_CMD,
- ADIS16260_GLOB_CMD_SW_RESET);
- if (ret)
- dev_err(dev, "problem resetting device");
-
- return ret;
-}
-
/* Power down the device */
-int adis16260_stop_device(struct device *dev)
+static int adis16260_stop_device(struct device *dev)
{
int ret;
u16 val = ADIS16260_SLP_CNT_POWER_OFF;
@@ -451,7 +353,7 @@
return ret;
}
-int adis16260_self_test(struct device *dev)
+static int adis16260_self_test(struct device *dev)
{
int ret;
ret = adis16260_spi_write_reg_16(dev,
@@ -468,7 +370,7 @@
return ret;
}
-int adis16260_check_status(struct device *dev)
+static int adis16260_check_status(struct device *dev)
{
u16 status;
int ret;
@@ -479,11 +381,7 @@
dev_err(dev, "Reading status failed\n");
goto error_ret;
}
- ret = status;
- if (status & ADIS16260_DIAG_STAT_ALARM2)
- dev_err(dev, "Alarm 2 active\n");
- if (status & ADIS16260_DIAG_STAT_ALARM1)
- dev_err(dev, "Alarm 1 active\n");
+ ret = status & 0x7F;
if (status & ADIS16260_DIAG_STAT_FLASH_CHK)
dev_err(dev, "Flash checksum error\n");
if (status & ADIS16260_DIAG_STAT_SELF_TEST)
@@ -506,14 +404,8 @@
static int adis16260_initial_setup(struct adis16260_state *st)
{
int ret;
- u16 smp_prd;
struct device *dev = &st->indio_dev->dev;
- /* use low spi speed for init */
- st->us->max_speed_hz = ADIS16260_SPI_SLOW;
- st->us->mode = SPI_MODE_3;
- spi_setup(st->us);
-
/* Disable IRQ */
ret = adis16260_set_irq(dev, false);
if (ret) {
@@ -522,6 +414,11 @@
}
/* Do self test */
+ ret = adis16260_self_test(dev);
+ if (ret) {
+ dev_err(dev, "self test failure");
+ goto err_ret;
+ }
/* Read status register to check the result */
ret = adis16260_check_status(dev);
@@ -539,25 +436,18 @@
printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
st->us->chip_select, st->us->irq);
- /* use high spi speed if possible */
- ret = adis16260_spi_read_reg_16(dev, ADIS16260_SMPL_PRD, &smp_prd);
- if (!ret && (smp_prd & ADIS16260_SMPL_PRD_DIV_MASK) < 0x0A) {
- st->us->max_speed_hz = ADIS16260_SPI_SLOW;
- spi_setup(st->us);
- }
-
err_ret:
return ret;
}
-static IIO_DEV_ATTR_VOLT(supply, adis16260_read_12bit_signed,
+static IIO_DEV_ATTR_VOLT(supply, adis16260_read_12bit_unsigned,
ADIS16260_SUPPLY_OUT);
static IIO_CONST_ATTR(volt_supply_scale, "0.0018315");
static IIO_DEV_ATTR_GYRO(adis16260_read_14bit_signed,
ADIS16260_GYRO_OUT);
static IIO_DEV_ATTR_GYRO_SCALE(S_IWUSR | S_IRUGO,
- adis16260_read_12bit_signed,
+ adis16260_read_14bit_signed,
adis16260_write_16bit,
ADIS16260_GYRO_SCALE);
static IIO_DEV_ATTR_GYRO_OFFSET(S_IWUSR | S_IRUGO,
@@ -565,9 +455,9 @@
adis16260_write_16bit,
ADIS16260_GYRO_OFF);
-static IIO_DEV_ATTR_TEMP(adis16260_read_12bit_signed);
-static IIO_CONST_ATTR(temp_offset, "25 K");
-static IIO_CONST_ATTR(temp_scale, "0.1453 K");
+static IIO_DEV_ATTR_TEMP(adis16260_read_12bit_unsigned);
+static IIO_CONST_ATTR(temp_offset, "25");
+static IIO_CONST_ATTR(temp_scale, "0.1453");
static IIO_DEV_ATTR_VOLT(aux, adis16260_read_12bit_unsigned,
ADIS16260_AUX_ADC);
@@ -670,15 +560,7 @@
goto error_unreg_ring_funcs;
}
- if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) {
-#if 0 /* fixme: here we should support */
- iio_init_work_cont(&st->work_cont_thresh,
- NULL,
- adis16260_thresh_handler_bh_no_check,
- 0,
- 0,
- st);
-#endif
+ if (spi->irq) {
ret = iio_register_interrupt_line(spi->irq,
st->indio_dev,
0,
@@ -699,10 +581,9 @@
return 0;
error_remove_trigger:
- if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
- adis16260_remove_trigger(st->indio_dev);
+ adis16260_remove_trigger(st->indio_dev);
error_unregister_line:
- if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
+ if (spi->irq)
iio_unregister_interrupt_line(st->indio_dev, 0);
error_uninitialize_ring:
adis16260_uninitialize_ring(st->indio_dev->ring);
@@ -723,7 +604,6 @@
return ret;
}
-/* fixme, confirm ordering in this function */
static int adis16260_remove(struct spi_device *spi)
{
int ret;
@@ -737,12 +617,12 @@
flush_scheduled_work();
adis16260_remove_trigger(indio_dev);
- if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
+ if (spi->irq)
iio_unregister_interrupt_line(indio_dev, 0);
adis16260_uninitialize_ring(indio_dev->ring);
- adis16260_unconfigure_ring(indio_dev);
iio_device_unregister(indio_dev);
+ adis16260_unconfigure_ring(indio_dev);
kfree(st->tx);
kfree(st->rx);
kfree(st);
Added: trunk/drivers/staging/iio/gyro/adis16260_ring.c (0 => 8698)
--- trunk/drivers/staging/iio/gyro/adis16260_ring.c (rev 0)
+++ trunk/drivers/staging/iio/gyro/adis16260_ring.c 2010-05-10 08:25:01 UTC (rev 8698)
@@ -0,0 +1,250 @@
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/gpio.h>
+#include <linux/workqueue.h>
+#include <linux/mutex.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/spi/spi.h>
+#include <linux/sysfs.h>
+#include <linux/list.h>
+
+#include "../iio.h"
+#include "../sysfs.h"
+#include "../ring_sw.h"
+#include "../accel/accel.h"
+#include "../trigger.h"
+#include "adis16260.h"
+
+/**
+ * combine_8_to_16() utility function to munge to u8s into u16
+ **/
+static inline u16 combine_8_to_16(u8 lower, u8 upper)
+{
+ u16 _lower = lower;
+ u16 _upper = upper;
+ return _lower | (_upper << 8);
+}
+
+static IIO_SCAN_EL_C(supply, ADIS16260_SCAN_SUPPLY, IIO_UNSIGNED(12),
+ ADIS16260_SUPPLY_OUT, NULL);
+static IIO_SCAN_EL_C(gyro, ADIS16260_SCAN_GYRO, IIO_SIGNED(14),
+ ADIS16260_GYRO_OUT, NULL);
+static IIO_SCAN_EL_C(aux_adc, ADIS16260_SCAN_AUX_ADC, IIO_SIGNED(14),
+ ADIS16260_AUX_ADC, NULL);
+static IIO_SCAN_EL_C(temp, ADIS16260_SCAN_TEMP, IIO_UNSIGNED(12),
+ ADIS16260_TEMP_OUT, NULL);
+static IIO_SCAN_EL_C(angl, ADIS16260_SCAN_ANGL, IIO_UNSIGNED(12),
+ ADIS16260_ANGL_OUT, NULL);
+
+static IIO_SCAN_EL_TIMESTAMP;
+
+static struct attribute *adis16260_scan_el_attrs[] = {
+ &iio_scan_el_supply.dev_attr.attr,
+ &iio_scan_el_gyro.dev_attr.attr,
+ &iio_scan_el_aux_adc.dev_attr.attr,
+ &iio_scan_el_temp.dev_attr.attr,
+ &iio_scan_el_angl.dev_attr.attr,
+ &iio_scan_el_timestamp.dev_attr.attr,
+ NULL,
+};
+
+static struct attribute_group adis16260_scan_el_group = {
+ .attrs = adis16260_scan_el_attrs,
+ .name = "scan_elements",
+};
+
+/**
+ * adis16260_poll_func_th() top half interrupt handler called by trigger
+ * @private_data: iio_dev
+ **/
+static void adis16260_poll_func_th(struct iio_dev *indio_dev)
+{
+ struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
+ st->last_timestamp = indio_dev->trig->timestamp;
+ schedule_work(&st->work_trigger_to_ring);
+}
+
+/**
+ * adis16260_read_ring_data() read data registers which will be placed into ring
+ * @dev: device associated with child of actual device (iio_dev or iio_trig)
+ * @rx: somewhere to pass back the value read
+ **/
+static int adis16260_read_ring_data(struct device *dev, u8 *rx)
+{
+ struct spi_message msg;
+ struct iio_dev *indio_dev = dev_get_drvdata(dev);
+ struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
+ struct spi_transfer xfers[ADIS16260_OUTPUTS + 1];
+ int ret;
+ int i;
+
+ mutex_lock(&st->buf_lock);
+
+ spi_message_init(&msg);
+
+ memset(xfers, 0, sizeof(xfers));
+ for (i = 0; i <= ADIS16260_OUTPUTS; i++) {
+ xfers[i].bits_per_word = 8;
+ xfers[i].cs_change = 1;
+ xfers[i].len = 2;
+ xfers[i].delay_usecs = 30;
+ xfers[i].tx_buf = st->tx + 2 * i;
+ if (i < 2) /* SUPPLY_OUT:0x02 GYRO_OUT:0x04 */
+ st->tx[2 * i] = ADIS16260_READ_REG(ADIS16260_SUPPLY_OUT + 2 * i);
+ else /* 0x06 to 0x09 is reserved */
+ st->tx[2 * i] = ADIS16260_READ_REG(ADIS16260_SUPPLY_OUT + 2 * i + 4);
+ st->tx[2 * i + 1] = 0;
+ if (i >= 1)
+ xfers[i].rx_buf = rx + 2 * (i - 1);
+ spi_message_add_tail(&xfers[i], &msg);
+ }
+
+ ret = spi_sync(st->us, &msg);
+ if (ret)
+ dev_err(&st->us->dev, "problem when burst reading");
+
+ mutex_unlock(&st->buf_lock);
+
+ return ret;
+}
+
+
+static void adis16260_trigger_bh_to_ring(struct work_struct *work_s)
+{
+ struct adis16260_state *st
+ = container_of(work_s, struct adis16260_state,
+ work_trigger_to_ring);
+
+ int i = 0;
+ s16 *data;
+ size_t datasize = st->indio_dev
+ ->ring->access.get_bpd(st->indio_dev->ring);
+
+ data = "" , GFP_KERNEL);
+ if (data == NULL) {
+ dev_err(&st->us->dev, "memory alloc failed in ring bh");
+ return;
+ }
+
+ if (st->indio_dev->scan_count)
+ if (adis16260_read_ring_data(&st->indio_dev->dev, st->rx) >= 0)
+ for (; i < st->indio_dev->scan_count; i++) {
+ data[i] = combine_8_to_16(st->rx[i*2+1],
+ st->rx[i*2]);
+ }
+
+ /* Guaranteed to be aligned with 8 byte boundary */
+ if (st->indio_dev->scan_timestamp)
+ *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
+
+ st->indio_dev->ring->access.store_to(st->indio_dev->ring,
+ (u8 *)data,
+ st->last_timestamp);
+
+ iio_trigger_notify_done(st->indio_dev->trig);
+ kfree(data);
+
+ return;
+}
+
+static int adis16260_data_rdy_ring_preenable(struct iio_dev *indio_dev)
+{
+ size_t size;
+ dev_dbg(&indio_dev->dev, "%s\n", __func__);
+ /* Check if there are any scan elements enabled, if not fail*/
+ if (!(indio_dev->scan_count || indio_dev->scan_timestamp))
+ return -EINVAL;
+
+ if (indio_dev->ring->access.set_bpd) {
+ if (indio_dev->scan_timestamp)
+ if (indio_dev->scan_count)
+ /* Timestamp and data, let timestamp aligned with sizeof(s64) */
+ size = (((indio_dev->scan_count * sizeof(s16)) + sizeof(s64) - 1) & ~(sizeof(s64) - 1))
+ + sizeof(s64);
+ else /* Timestamp only */
+ size = sizeof(s64);
+ else /* Data only */
+ size = indio_dev->scan_count*sizeof(s16);
+ indio_dev->ring->access.set_bpd(indio_dev->ring, size);
+ }
+
+ return 0;
+}
+
+static int adis16260_data_rdy_ring_postenable(struct iio_dev *indio_dev)
+{
+ return indio_dev->trig
+ ? iio_trigger_attach_poll_func(indio_dev->trig,
+ indio_dev->pollfunc)
+ : 0;
+}
+
+static int adis16260_data_rdy_ring_predisable(struct iio_dev *indio_dev)
+{
+ return indio_dev->trig
+ ? iio_trigger_dettach_poll_func(indio_dev->trig,
+ indio_dev->pollfunc)
+ : 0;
+}
+
+void adis16260_unconfigure_ring(struct iio_dev *indio_dev)
+{
+ kfree(indio_dev->pollfunc);
+ iio_sw_rb_free(indio_dev->ring);
+}
+
+int adis16260_configure_ring(struct iio_dev *indio_dev)
+{
+ int ret = 0;
+ struct adis16260_state *st = indio_dev->dev_data;
+ struct iio_ring_buffer *ring;
+ INIT_WORK(&st->work_trigger_to_ring, adis16260_trigger_bh_to_ring);
+ /* Set default scan mode */
+
+ iio_scan_mask_set(indio_dev, iio_scan_el_supply.number);
+ iio_scan_mask_set(indio_dev, iio_scan_el_gyro.number);
+ iio_scan_mask_set(indio_dev, iio_scan_el_aux_adc.number);
+ iio_scan_mask_set(indio_dev, iio_scan_el_temp.number);
+ iio_scan_mask_set(indio_dev, iio_scan_el_angl.number);
+ indio_dev->scan_timestamp = true;
+
+ indio_dev->scan_el_attrs = &adis16260_scan_el_group;
+
+ ring = iio_sw_rb_allocate(indio_dev);
+ if (!ring) {
+ ret = -ENOMEM;
+ return ret;
+ }
+ indio_dev->ring = ring;
+ /* Effectively select the ring buffer implementation */
+ iio_ring_sw_register_funcs(&ring->access);
+ ring->preenable = &adis16260_data_rdy_ring_preenable;
+ ring->postenable = &adis16260_data_rdy_ring_postenable;
+ ring->predisable = &adis16260_data_rdy_ring_predisable;
+ ring->owner = THIS_MODULE;
+
+ indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
+ if (indio_dev->pollfunc == NULL) {
+ ret = -ENOMEM;
+ goto error_iio_sw_rb_free;;
+ }
+ indio_dev->pollfunc->poll_func_main = &adis16260_poll_func_th;
+ indio_dev->pollfunc->private_data = indio_dev;
+ indio_dev->modes |= INDIO_RING_TRIGGERED;
+ return 0;
+
+error_iio_sw_rb_free:
+ iio_sw_rb_free(indio_dev->ring);
+ return ret;
+}
+
+int adis16260_initialize_ring(struct iio_ring_buffer *ring)
+{
+ return iio_ring_buffer_register(ring);
+}
+
+void adis16260_uninitialize_ring(struct iio_ring_buffer *ring)
+{
+ iio_ring_buffer_unregister(ring);
+}