|
@@ -37,8 +37,151 @@
|
|
|
#include <linux/iio/events.h>
|
|
|
#include <linux/iio/buffer.h>
|
|
|
#include <linux/iio/driver.h>
|
|
|
+#include <linux/iio/kfifo_buf.h>
|
|
|
+#include <linux/iio/trigger_consumer.h>
|
|
|
+
|
|
|
+#define MAX1363_SETUP_BYTE(a) ((a) | 0x80)
|
|
|
+
|
|
|
+/* There is a fair bit more defined here than currently
|
|
|
+ * used, but the intention is to support everything these
|
|
|
+ * chips do in the long run */
|
|
|
+
|
|
|
+/* see data sheets */
|
|
|
+/* max1363 and max1236, max1237, max1238, max1239 */
|
|
|
+#define MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_VDD 0x00
|
|
|
+#define MAX1363_SETUP_AIN3_IS_REF_EXT_TO_REF 0x20
|
|
|
+#define MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_INT 0x40
|
|
|
+#define MAX1363_SETUP_AIN3_IS_REF_REF_IS_INT 0x60
|
|
|
+#define MAX1363_SETUP_POWER_UP_INT_REF 0x10
|
|
|
+#define MAX1363_SETUP_POWER_DOWN_INT_REF 0x00
|
|
|
+
|
|
|
+/* think about includeing max11600 etc - more settings */
|
|
|
+#define MAX1363_SETUP_EXT_CLOCK 0x08
|
|
|
+#define MAX1363_SETUP_INT_CLOCK 0x00
|
|
|
+#define MAX1363_SETUP_UNIPOLAR 0x00
|
|
|
+#define MAX1363_SETUP_BIPOLAR 0x04
|
|
|
+#define MAX1363_SETUP_RESET 0x00
|
|
|
+#define MAX1363_SETUP_NORESET 0x02
|
|
|
+/* max1363 only - though don't care on others.
|
|
|
+ * For now monitor modes are not implemented as the relevant
|
|
|
+ * line is not connected on my test board.
|
|
|
+ * The definitions are here as I intend to add this soon.
|
|
|
+ */
|
|
|
+#define MAX1363_SETUP_MONITOR_SETUP 0x01
|
|
|
+
|
|
|
+/* Specific to the max1363 */
|
|
|
+#define MAX1363_MON_RESET_CHAN(a) (1 << ((a) + 4))
|
|
|
+#define MAX1363_MON_INT_ENABLE 0x01
|
|
|
+
|
|
|
+/* defined for readability reasons */
|
|
|
+/* All chips */
|
|
|
+#define MAX1363_CONFIG_BYTE(a) ((a))
|
|
|
+
|
|
|
+#define MAX1363_CONFIG_SE 0x01
|
|
|
+#define MAX1363_CONFIG_DE 0x00
|
|
|
+#define MAX1363_CONFIG_SCAN_TO_CS 0x00
|
|
|
+#define MAX1363_CONFIG_SCAN_SINGLE_8 0x20
|
|
|
+#define MAX1363_CONFIG_SCAN_MONITOR_MODE 0x40
|
|
|
+#define MAX1363_CONFIG_SCAN_SINGLE_1 0x60
|
|
|
+/* max123{6-9} only */
|
|
|
+#define MAX1236_SCAN_MID_TO_CHANNEL 0x40
|
|
|
+
|
|
|
+/* max1363 only - merely part of channel selects or don't care for others*/
|
|
|
+#define MAX1363_CONFIG_EN_MON_MODE_READ 0x18
|
|
|
+
|
|
|
+#define MAX1363_CHANNEL_SEL(a) ((a) << 1)
|
|
|
+
|
|
|
+/* max1363 strictly 0x06 - but doesn't matter */
|
|
|
+#define MAX1363_CHANNEL_SEL_MASK 0x1E
|
|
|
+#define MAX1363_SCAN_MASK 0x60
|
|
|
+#define MAX1363_SE_DE_MASK 0x01
|
|
|
+
|
|
|
+#define MAX1363_MAX_CHANNELS 25
|
|
|
+/**
|
|
|
+ * struct max1363_mode - scan mode information
|
|
|
+ * @conf: The corresponding value of the configuration register
|
|
|
+ * @modemask: Bit mask corresponding to channels enabled in this mode
|
|
|
+ */
|
|
|
+struct max1363_mode {
|
|
|
+ int8_t conf;
|
|
|
+ DECLARE_BITMAP(modemask, MAX1363_MAX_CHANNELS);
|
|
|
+};
|
|
|
|
|
|
-#include "max1363.h"
|
|
|
+/* This must be maintained along side the max1363_mode_table in max1363_core */
|
|
|
+enum max1363_modes {
|
|
|
+ /* Single read of a single channel */
|
|
|
+ _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7, _s8, _s9, _s10, _s11,
|
|
|
+ /* Differential single read */
|
|
|
+ d0m1, d2m3, d4m5, d6m7, d8m9, d10m11,
|
|
|
+ d1m0, d3m2, d5m4, d7m6, d9m8, d11m10,
|
|
|
+ /* Scan to channel and mid to channel where overlapping */
|
|
|
+ s0to1, s0to2, s2to3, s0to3, s0to4, s0to5, s0to6,
|
|
|
+ s6to7, s0to7, s6to8, s0to8, s6to9,
|
|
|
+ s0to9, s6to10, s0to10, s6to11, s0to11,
|
|
|
+ /* Differential scan to channel and mid to channel where overlapping */
|
|
|
+ d0m1to2m3, d0m1to4m5, d0m1to6m7, d6m7to8m9,
|
|
|
+ d0m1to8m9, d6m7to10m11, d0m1to10m11, d1m0to3m2,
|
|
|
+ d1m0to5m4, d1m0to7m6, d7m6to9m8, d1m0to9m8,
|
|
|
+ d7m6to11m10, d1m0to11m10,
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * struct max1363_chip_info - chip specifc information
|
|
|
+ * @info: iio core function callbacks structure
|
|
|
+ * @channels: channel specification
|
|
|
+ * @num_channels: number of channels
|
|
|
+ * @mode_list: array of available scan modes
|
|
|
+ * @default_mode: the scan mode in which the chip starts up
|
|
|
+ * @int_vref_mv: the internal reference voltage
|
|
|
+ * @num_channels: number of channels
|
|
|
+ * @bits: accuracy of the adc in bits
|
|
|
+ */
|
|
|
+struct max1363_chip_info {
|
|
|
+ const struct iio_info *info;
|
|
|
+ const struct iio_chan_spec *channels;
|
|
|
+ int num_channels;
|
|
|
+ const enum max1363_modes *mode_list;
|
|
|
+ enum max1363_modes default_mode;
|
|
|
+ u16 int_vref_mv;
|
|
|
+ u8 num_modes;
|
|
|
+ u8 bits;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * struct max1363_state - driver instance specific data
|
|
|
+ * @client: i2c_client
|
|
|
+ * @setupbyte: cache of current device setup byte
|
|
|
+ * @configbyte: cache of current device config byte
|
|
|
+ * @chip_info: chip model specific constants, available modes etc
|
|
|
+ * @current_mode: the scan mode of this chip
|
|
|
+ * @requestedmask: a valid requested set of channels
|
|
|
+ * @reg: supply regulator
|
|
|
+ * @monitor_on: whether monitor mode is enabled
|
|
|
+ * @monitor_speed: parameter corresponding to device monitor speed setting
|
|
|
+ * @mask_high: bitmask for enabled high thresholds
|
|
|
+ * @mask_low: bitmask for enabled low thresholds
|
|
|
+ * @thresh_high: high threshold values
|
|
|
+ * @thresh_low: low threshold values
|
|
|
+ */
|
|
|
+struct max1363_state {
|
|
|
+ struct i2c_client *client;
|
|
|
+ u8 setupbyte;
|
|
|
+ u8 configbyte;
|
|
|
+ const struct max1363_chip_info *chip_info;
|
|
|
+ const struct max1363_mode *current_mode;
|
|
|
+ u32 requestedmask;
|
|
|
+ struct regulator *reg;
|
|
|
+
|
|
|
+ /* Using monitor modes and buffer at the same time is
|
|
|
+ currently not supported */
|
|
|
+ bool monitor_on;
|
|
|
+ unsigned int monitor_speed:3;
|
|
|
+ u8 mask_high;
|
|
|
+ u8 mask_low;
|
|
|
+ /* 4x unipolar first then the fours bipolar ones */
|
|
|
+ s16 thresh_high[8];
|
|
|
+ s16 thresh_low[8];
|
|
|
+};
|
|
|
|
|
|
#define MAX1363_MODE_SINGLE(_num, _mask) { \
|
|
|
.conf = MAX1363_CHANNEL_SEL(_num) \
|
|
@@ -148,7 +291,7 @@ static const struct max1363_mode max1363_mode_table[] = {
|
|
|
MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(11, 6, 0xFC0000),
|
|
|
};
|
|
|
|
|
|
-const struct max1363_mode
|
|
|
+static const struct max1363_mode
|
|
|
*max1363_match_mode(const unsigned long *mask,
|
|
|
const struct max1363_chip_info *ci)
|
|
|
{
|
|
@@ -172,7 +315,7 @@ static int max1363_write_basic_config(struct i2c_client *client,
|
|
|
return i2c_master_send(client, tx_buf, 2);
|
|
|
}
|
|
|
|
|
|
-int max1363_set_scan_mode(struct max1363_state *st)
|
|
|
+static int max1363_set_scan_mode(struct max1363_state *st)
|
|
|
{
|
|
|
st->configbyte &= ~(MAX1363_CHANNEL_SEL_MASK
|
|
|
| MAX1363_SCAN_MASK
|
|
@@ -622,9 +765,9 @@ static int max1363_read_event_config(struct iio_dev *indio_dev,
|
|
|
u64 event_code)
|
|
|
{
|
|
|
struct max1363_state *st = iio_priv(indio_dev);
|
|
|
-
|
|
|
int val;
|
|
|
int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
|
|
|
+
|
|
|
mutex_lock(&indio_dev->mlock);
|
|
|
if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_FALLING)
|
|
|
val = (1 << number) & st->mask_low;
|
|
@@ -644,7 +787,7 @@ static int max1363_monitor_mode_update(struct max1363_state *st, int enabled)
|
|
|
const long *modemask;
|
|
|
|
|
|
if (!enabled) {
|
|
|
- /* transition to ring capture is not currently supported */
|
|
|
+ /* transition to buffered capture is not currently supported */
|
|
|
st->setupbyte &= ~MAX1363_SETUP_MONITOR_SETUP;
|
|
|
st->configbyte &= ~MAX1363_SCAN_MASK;
|
|
|
st->monitor_on = false;
|
|
@@ -826,8 +969,21 @@ static struct attribute_group max1363_event_attribute_group = {
|
|
|
.name = "events",
|
|
|
};
|
|
|
|
|
|
-#define MAX1363_EVENT_FUNCS \
|
|
|
+static int max1363_update_scan_mode(struct iio_dev *indio_dev,
|
|
|
+ const unsigned long *scan_mask)
|
|
|
+{
|
|
|
+ struct max1363_state *st = iio_priv(indio_dev);
|
|
|
|
|
|
+ /*
|
|
|
+ * Need to figure out the current mode based upon the requested
|
|
|
+ * scan mask in iio_dev
|
|
|
+ */
|
|
|
+ st->current_mode = max1363_match_mode(scan_mask, st->chip_info);
|
|
|
+ if (!st->current_mode)
|
|
|
+ return -EINVAL;
|
|
|
+ max1363_set_scan_mode(st);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
|
|
|
static const struct iio_info max1238_info = {
|
|
|
.read_raw = &max1363_read_raw,
|
|
@@ -1230,8 +1386,6 @@ static const struct max1363_chip_info max1363_chip_info_tbl[] = {
|
|
|
},
|
|
|
};
|
|
|
|
|
|
-
|
|
|
-
|
|
|
static int max1363_initial_setup(struct max1363_state *st)
|
|
|
{
|
|
|
st->setupbyte = MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_VDD
|
|
@@ -1269,34 +1423,137 @@ static int __devinit max1363_alloc_scan_masks(struct iio_dev *indio_dev)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+static irqreturn_t max1363_trigger_handler(int irq, void *p)
|
|
|
+{
|
|
|
+ struct iio_poll_func *pf = p;
|
|
|
+ struct iio_dev *indio_dev = pf->indio_dev;
|
|
|
+ struct max1363_state *st = iio_priv(indio_dev);
|
|
|
+ s64 time_ns;
|
|
|
+ __u8 *rxbuf;
|
|
|
+ int b_sent;
|
|
|
+ size_t d_size;
|
|
|
+ unsigned long numvals = bitmap_weight(st->current_mode->modemask,
|
|
|
+ MAX1363_MAX_CHANNELS);
|
|
|
+
|
|
|
+ /* Ensure the timestamp is 8 byte aligned */
|
|
|
+ if (st->chip_info->bits != 8)
|
|
|
+ d_size = numvals*2;
|
|
|
+ else
|
|
|
+ d_size = numvals;
|
|
|
+ if (indio_dev->scan_timestamp) {
|
|
|
+ d_size += sizeof(s64);
|
|
|
+ if (d_size % sizeof(s64))
|
|
|
+ d_size += sizeof(s64) - (d_size % sizeof(s64));
|
|
|
+ }
|
|
|
+ /* Monitor mode prevents reading. Whilst not currently implemented
|
|
|
+ * might as well have this test in here in the meantime as it does
|
|
|
+ * no harm.
|
|
|
+ */
|
|
|
+ if (numvals == 0)
|
|
|
+ goto done;
|
|
|
+
|
|
|
+ rxbuf = kmalloc(d_size, GFP_KERNEL);
|
|
|
+ if (rxbuf == NULL)
|
|
|
+ goto done;
|
|
|
+ if (st->chip_info->bits != 8)
|
|
|
+ b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
|
|
|
+ else
|
|
|
+ b_sent = i2c_master_recv(st->client, rxbuf, numvals);
|
|
|
+ if (b_sent < 0)
|
|
|
+ goto done_free;
|
|
|
+
|
|
|
+ time_ns = iio_get_time_ns();
|
|
|
+
|
|
|
+ if (indio_dev->scan_timestamp)
|
|
|
+ memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
|
|
|
+ iio_push_to_buffers(indio_dev, rxbuf);
|
|
|
+
|
|
|
+done_free:
|
|
|
+ kfree(rxbuf);
|
|
|
+done:
|
|
|
+ iio_trigger_notify_done(indio_dev->trig);
|
|
|
+
|
|
|
+ return IRQ_HANDLED;
|
|
|
+}
|
|
|
+
|
|
|
+static const struct iio_buffer_setup_ops max1363_buffered_setup_ops = {
|
|
|
+ .postenable = &iio_triggered_buffer_postenable,
|
|
|
+ .preenable = &iio_sw_buffer_preenable,
|
|
|
+ .predisable = &iio_triggered_buffer_predisable,
|
|
|
+};
|
|
|
+
|
|
|
+static int max1363_register_buffered_funcs_and_init(struct iio_dev *indio_dev)
|
|
|
+{
|
|
|
+ struct max1363_state *st = iio_priv(indio_dev);
|
|
|
+ int ret = 0;
|
|
|
+
|
|
|
+ indio_dev->buffer = iio_kfifo_allocate(indio_dev);
|
|
|
+ if (!indio_dev->buffer) {
|
|
|
+ ret = -ENOMEM;
|
|
|
+ goto error_ret;
|
|
|
+ }
|
|
|
+ indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
|
|
|
+ &max1363_trigger_handler,
|
|
|
+ IRQF_ONESHOT,
|
|
|
+ indio_dev,
|
|
|
+ "%s_consumer%d",
|
|
|
+ st->client->name,
|
|
|
+ indio_dev->id);
|
|
|
+ if (indio_dev->pollfunc == NULL) {
|
|
|
+ ret = -ENOMEM;
|
|
|
+ goto error_deallocate_sw_rb;
|
|
|
+ }
|
|
|
+ /* Buffer functions - here trigger setup related */
|
|
|
+ indio_dev->setup_ops = &max1363_buffered_setup_ops;
|
|
|
+
|
|
|
+ /* Flag that polled buffering is possible */
|
|
|
+ indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+
|
|
|
+error_deallocate_sw_rb:
|
|
|
+ iio_kfifo_free(indio_dev->buffer);
|
|
|
+error_ret:
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+static void max1363_buffer_cleanup(struct iio_dev *indio_dev)
|
|
|
+{
|
|
|
+ /* ensure that the trigger has been detached */
|
|
|
+ iio_dealloc_pollfunc(indio_dev->pollfunc);
|
|
|
+ iio_kfifo_free(indio_dev->buffer);
|
|
|
+}
|
|
|
+
|
|
|
static int __devinit max1363_probe(struct i2c_client *client,
|
|
|
const struct i2c_device_id *id)
|
|
|
{
|
|
|
int ret;
|
|
|
struct max1363_state *st;
|
|
|
struct iio_dev *indio_dev;
|
|
|
- struct regulator *reg;
|
|
|
-
|
|
|
- reg = regulator_get(&client->dev, "vcc");
|
|
|
- if (IS_ERR(reg)) {
|
|
|
- ret = PTR_ERR(reg);
|
|
|
- goto error_out;
|
|
|
- }
|
|
|
-
|
|
|
- ret = regulator_enable(reg);
|
|
|
- if (ret)
|
|
|
- goto error_put_reg;
|
|
|
|
|
|
indio_dev = iio_device_alloc(sizeof(struct max1363_state));
|
|
|
if (indio_dev == NULL) {
|
|
|
ret = -ENOMEM;
|
|
|
- goto error_disable_reg;
|
|
|
+ goto error_out;
|
|
|
}
|
|
|
+
|
|
|
ret = iio_map_array_register(indio_dev, client->dev.platform_data);
|
|
|
if (ret < 0)
|
|
|
goto error_free_device;
|
|
|
+
|
|
|
st = iio_priv(indio_dev);
|
|
|
- st->reg = reg;
|
|
|
+
|
|
|
+ st->reg = regulator_get(&client->dev, "vcc");
|
|
|
+ if (IS_ERR(st->reg)) {
|
|
|
+ ret = PTR_ERR(st->reg);
|
|
|
+ goto error_unregister_map;
|
|
|
+ }
|
|
|
+
|
|
|
+ ret = regulator_enable(st->reg);
|
|
|
+ if (ret)
|
|
|
+ goto error_put_reg;
|
|
|
+
|
|
|
/* this is only used for device removal purposes */
|
|
|
i2c_set_clientdata(client, indio_dev);
|
|
|
|
|
@@ -1305,7 +1562,7 @@ static int __devinit max1363_probe(struct i2c_client *client,
|
|
|
|
|
|
ret = max1363_alloc_scan_masks(indio_dev);
|
|
|
if (ret)
|
|
|
- goto error_unregister_map;
|
|
|
+ goto error_disable_reg;
|
|
|
|
|
|
/* Estabilish that the iio_dev is a child of the i2c device */
|
|
|
indio_dev->dev.parent = &client->dev;
|
|
@@ -1320,7 +1577,7 @@ static int __devinit max1363_probe(struct i2c_client *client,
|
|
|
if (ret < 0)
|
|
|
goto error_free_available_scan_masks;
|
|
|
|
|
|
- ret = max1363_register_ring_funcs_and_init(indio_dev);
|
|
|
+ ret = max1363_register_buffered_funcs_and_init(indio_dev);
|
|
|
if (ret)
|
|
|
goto error_free_available_scan_masks;
|
|
|
|
|
@@ -1328,7 +1585,7 @@ static int __devinit max1363_probe(struct i2c_client *client,
|
|
|
st->chip_info->channels,
|
|
|
st->chip_info->num_channels);
|
|
|
if (ret)
|
|
|
- goto error_cleanup_ring;
|
|
|
+ goto error_cleanup_buffer;
|
|
|
|
|
|
if (client->irq) {
|
|
|
ret = request_threaded_irq(st->client->irq,
|
|
@@ -1339,7 +1596,7 @@ static int __devinit max1363_probe(struct i2c_client *client,
|
|
|
indio_dev);
|
|
|
|
|
|
if (ret)
|
|
|
- goto error_uninit_ring;
|
|
|
+ goto error_uninit_buffer;
|
|
|
}
|
|
|
|
|
|
ret = iio_device_register(indio_dev);
|
|
@@ -1349,20 +1606,20 @@ static int __devinit max1363_probe(struct i2c_client *client,
|
|
|
return 0;
|
|
|
error_free_irq:
|
|
|
free_irq(st->client->irq, indio_dev);
|
|
|
-error_uninit_ring:
|
|
|
+error_uninit_buffer:
|
|
|
iio_buffer_unregister(indio_dev);
|
|
|
-error_cleanup_ring:
|
|
|
- max1363_ring_cleanup(indio_dev);
|
|
|
+error_cleanup_buffer:
|
|
|
+ max1363_buffer_cleanup(indio_dev);
|
|
|
error_free_available_scan_masks:
|
|
|
kfree(indio_dev->available_scan_masks);
|
|
|
error_unregister_map:
|
|
|
iio_map_array_unregister(indio_dev, client->dev.platform_data);
|
|
|
-error_free_device:
|
|
|
- iio_device_free(indio_dev);
|
|
|
error_disable_reg:
|
|
|
- regulator_disable(reg);
|
|
|
+ regulator_disable(st->reg);
|
|
|
error_put_reg:
|
|
|
- regulator_put(reg);
|
|
|
+ regulator_put(st->reg);
|
|
|
+error_free_device:
|
|
|
+ iio_device_free(indio_dev);
|
|
|
error_out:
|
|
|
return ret;
|
|
|
}
|
|
@@ -1371,17 +1628,16 @@ static int __devexit max1363_remove(struct i2c_client *client)
|
|
|
{
|
|
|
struct iio_dev *indio_dev = i2c_get_clientdata(client);
|
|
|
struct max1363_state *st = iio_priv(indio_dev);
|
|
|
- struct regulator *reg = st->reg;
|
|
|
|
|
|
iio_device_unregister(indio_dev);
|
|
|
if (client->irq)
|
|
|
free_irq(st->client->irq, indio_dev);
|
|
|
iio_buffer_unregister(indio_dev);
|
|
|
- max1363_ring_cleanup(indio_dev);
|
|
|
+ max1363_buffer_cleanup(indio_dev);
|
|
|
kfree(indio_dev->available_scan_masks);
|
|
|
- if (!IS_ERR(reg)) {
|
|
|
- regulator_disable(reg);
|
|
|
- regulator_put(reg);
|
|
|
+ if (!IS_ERR(st->reg)) {
|
|
|
+ regulator_disable(st->reg);
|
|
|
+ regulator_put(st->reg);
|
|
|
}
|
|
|
iio_map_array_unregister(indio_dev, client->dev.platform_data);
|
|
|
iio_device_free(indio_dev);
|