gp2ap002a00f ambient light/proximity sensor
This driver in the current form supports:
- reading channels in 'one shot' mode through read_raw callback,
- three events - rising and falling ambient light events and rising proximity event.
It has infrastructure for triggers implemented, but either it is not implemented properly or I don't know how to initialize it from user space.
What I'd like to achieve through this RFC is (besides regular review) gain some clarification on how to properly implemeint support for both events and triggers. At first I'd like to mention that I managed to implement support for triggers without events. I did it by following implementations of the existing drivers that support triggers, but no events. Briefly, they perform following steps to setup a trigger:
static irqreturn_t gp2ap002a00f_irq_handler(int irq, void *data) {
struct gp2ap002a00f_data *lgt = iio_priv(indio_dev);
// Filling the lgt->buffer with the output data from the
// device is performed here, according to the
// indio_dev->active_scan_mask bits.
iio_push_to_buffers(indio_dev, lgt->buffer);
iio_trigger_notify_done(indio_dev->trig);}
int gp2ap002a00f_trig_set_state(struct iio_trigger *trig, bool state) {
// Here a buffer is allocated/deallocated and the device
// related operations are performed to enable/disable // generating data, according to the // indio_dev->active_scan_mask bits .
}
static const struct iio_trigger_ops gp2ap002a00f_trigger_ops = {
.owner = THIS_MODULE,
.set_trigger_state = &gp2ap002a00f_trig_set_state,
};
iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
gp2ap002a00f_irq_handler, &gp2ap002a00f_setup_ops);
iio_trigger_alloc("%s-trigger", indio_dev->name);
err = devm_request_threaded_irq(&client->dev, client->irq,
iio_trigger_generic_data_rdy_poll,
NULL,
IRQF_TRIGGER_FALLING,
data->trig->name,
data->trig); data->trig->private_data = indio_dev;
data->trig->ops = &gp2ap002a00f_trigger_ops;
data->trig->dev.parent = &data->client->dev
ret = iio_trigger_register(data->trig);
With the implementation shown above I can setup a trigger using generic_buffer application.
Nonetheless, drivers that implement both triggers and events are implemented in a somehow different manner (I followed recommended max1363 driver):
static irqreturn_t gp2ap002a00f_event_handler(int irq, void *data)
{
iio_push_event(indio_dev,
IIO_MOD_EVENT_CODE(
IIO_LIGHT,
SCAN_MODE_LIGHT_CLEAR,
IIO_MOD_LIGHT_CLEAR,
IIO_EV_TYPE_THRESH,
IIO_EV_DIR_RISING),
iio_get_time_ns());}
err = iio_triggered_buffer_setup(indio_dev, NULL,
&gp2ap002a00f_trigger_handler, &gp2ap002a00f_buffer_setup_ops);
err = devm_request_threaded_irq(&client->dev, client->irq,
&gp2ap002a00f_event_handler,
NULL,
IRQF_TRIGGER_FALLING,
"gp2ap002a00f_event",
indio_dev);Enabling/disabling data generation seems to be handled by buffer_setup_ops' preenable/postenable/predisable callbacks. I've noticed also that there is update_scan_mode callback exploited in some of the implementations, which I suspect serves for updating scan_mode without the need for disabling a trigger?
With this implementation the trigger/current_trigger file is empty and generic_buffer application fails. Therefore I ask for clarification on what I am doing wrong here.
Regarding the remaining parts of the driver - I used flags, not bit fields as Jonathan requested in my driver for the lps331ap device because of the need for one flag for wait_event_timeout function. This flag is set in the interrupt handler and access to it must be atomic. I could have gone for bit fields for the rest of the flags in the driver, but I think that now the implementation is more consistent.
I've also noticed that once initialized triggers keep some IIO resources unreleased as execution of rmmod on the driver module fails even after triggered capture finish. Is it a known issue?
Thanks,
Jacek Anaszewski
Jacek Anaszewski (1):
iio: gp2ap002a00f: Add a driver for the device.
.../devicetree/bindings/iio/light/gp2ap002a00f.txt | 20 + drivers/iio/light/Kconfig | 12 + drivers/iio/light/Makefile | 1 + drivers/iio/light/gp2ap002a00f.c | 1158 ++++++++++++++++++++ 4 files changed, 1191 insertions(+), 0 deletions(-) create mode 100644 Documentation/devicetree/bindings/iio/light/gp2ap002a00f.txt create mode 100644 drivers/iio/light/gp2ap002a00f.c
Signature · 1 line
1.7.5.4
Jonathan Cameron
maintainerQuoted 27 lines · Jacek Anaszewski
This driver in the current form supports:
- reading channels in 'one shot' mode throughread_rawcallback,
- three events - rising and falling ambient light events and rising proximity event.It has infrastructure for triggers implemented, but either it is not implemented properly or I don't know how to initialize it from user space.
What I'd like to achieve through this RFC is (besides regular review) gain some clarification on how to properly implemeint support for both events and triggers. At first I'd like to mention that I managed to implement support for triggers without events. I did it by following implementations of the existing drivers that support triggers, but no events. Briefly, they perform following steps to setup a trigger:
static
irqreturn_tgp2ap002a00f_irq_handler(int irq, void *data){(quoted diff) +0
-0struct gp2ap002a00f_data *lgt = iio_priv(indio_dev); // Filling the lgt->buffer with the output data from the// device is performed here, according to the // indio_dev->active_scan_mask bits. iio_push_to_buffers(indio_dev, lgt->buffer); iio_trigger_notify_done(indio_dev->trig);
Quoted 1 line
}
THat's a fairly standard handler for the the trigger. That is the trigger interrupt handler itself fires off sub interrupts to any devices 'consuming' it resulting in this function being called.
Quoted 13 lines
int
gp2ap002a00f_trig_set_state(struct iio_trigger *trig, bool state){
// Here a buffer is allocated/deallocated and the device// related operations are performed to enable/disable // generating data, according to the // indio_dev->active_scan_mask bits .}
static const struct
iio_trigger_opsgp2ap002a00f_trigger_ops= {(quoted diff) +0
-0.owner = THIS_MODULE,
Review
Quoted 10 lines
(quoted diff) +0
-0.set_trigger_state = &gp2ap002a00f_trig_set_state, }; iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, gp2ap002a00f_irq_handler, &gp2ap002a00f_setup_ops); iio_trigger_alloc("%s-trigger", indio_dev->name); err = devm_request_threaded_irq(&client->dev, client->irq, iio_trigger_generic_data_rdy_poll,
This generic hander simply fires off the set of consumer interrupts.
Quoted 9 lines
NULL, IRQF_TRIGGER_FALLING, data->trig->name, data->trig);
data->trig->private_data=indio_dev;
data->trig->ops=&gp2ap002a00f_trigger_ops;
data->trig->dev.parent=&data->client->dev
Quoted 8 lines
ret =
iio_trigger_register(data->trig);With the implementation shown above I can setup a trigger using
generic_bufferapplication.Nonetheless, drivers that implement both triggers and events are implemented in a somehow different manner (I followed recommended max1363 driver):
Note the max1363 does not supply a trigger of it's own. That part captures data on demand and has no 'data ready' signal or similar. The above example of buffering suggests that this device has an internal clock and fires a dataready interrupt at regular intervals when monitoring?
Quoted 12 lines
static
irqreturn_tgp2ap002a00f_event_handler(int irq, void *data){ iio_push_event(indio_dev, IIO_MOD_EVENT_CODE( IIO_LIGHT, SCAN_MODE_LIGHT_CLEAR, IIO_MOD_LIGHT_CLEAR, IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING), iio_get_time_ns());
Quoted 2 lines
}
This handles the event interrupts, but does nothing about firing off a trigger to any consumers.
Quoted 3 lines
err = iio_triggered_buffer_setup(indio_dev, NULL,
&gp2ap002a00f_trigger_handler, &gp2ap002a00f_buffer_setup_ops);
Just note that your event handler in the actual driver sleeps so should the handler should be the 4th parameter with the 3rd null. Note this causes some complexity for iio as 'normally' the triggers are initialized from interrupt context. To that end we play some tricks.
Right now I can't actually find a driver doing this, I though the lis3l02dq driver did, but it seems that one won't actually run both events and the buffer at the same time.
Anyhow to sketch out the solution you want, combine a standard threaded handler with the trick used in drivers/iio/trigger/iio-trig-sysfs.c in which and irq_work queue is used to get us back into interrupt context and fire off the trigger interrupts.
Mostly hardware designers make our lives easy by directing event and dataready interrupts to different pins!
Quoted 7 lines
err = devm_request_threaded_irq(&client->dev,
client->irq,&gp2ap002a00f_event_handler, NULL, IRQF_TRIGGER_FALLING, "gp2ap002a00f_event", indio_dev);
Quoted 5 lines
Enabling/disabling data generation seems to be handled by
buffer_setup_ops' preenable/postenable/predisable callbacks. I've noticed also that there isupdate_scan_modecallback exploited in some of the implementations, which I suspect serves for updatingscan_modewithout the need for disabling a trigger?
No. To change a scan mask you must disconnect from the trigger. If there are other consumers this means the trigger will briefly stop whilst this one is disconnected then resume for any other consumers.
Quoted 4 lines
With this implementation the trigger/current_trigger file is empty and
generic_bufferapplication fails. Therefore I ask for clarification on what I am doing wrong here.
You aren't actually registering a trigger as far as I can see?
Quoted 9 lines
Regarding the remaining parts of the driver - I used flags, not bit fields as Jonathan requested in my driver for the lps331ap device because of the need for one flag for
wait_event_timeoutfunction. This flag is set in the interrupt handler and access to it must be atomic. I could have gone for bit fields for the rest of the flags in the driver, but I think that now the implementation is more consistent.
Quoted 3 lines
I've also noticed that once initialized triggers keep some IIO resources unreleased as execution of rmmod on the driver module fails even after triggered capture finish. Is it a known issue?
I have not observed this, perhaps you have not succesfully disconnected the consumer from the trigger? (e.g. if current_trigger is not empty it will not succeed). There is a probable double free bug in the trigger unregistering that we are chasing down a the moment in another thread.
Jonathan
Quoted 15 lines
Thanks,
Jacek AnaszewskiJacek Anaszewski (1):
iio: gp2ap002a00f: Add a driver for the device..../devicetree/bindings/iio/light/gp2ap002a00f.txt | 20 + drivers/iio/light/Kconfig | 12 + drivers/iio/light/Makefile | 1 + drivers/iio/light/gp2ap002a00f.c | 1158 ++++++++++++++++++++ 4 files changed, 1191 insertions(+), 0 deletions(-) create mode 100644 Documentation/devicetree/bindings/iio/light/gp2ap002a00f.txt create mode 100644 drivers/iio/light/gp2ap002a00f.c
Re: [PATCH/RFC] iio: gp2ap002a00f: Add a driver for the device.
In reply to a message not in the archiveOn 06/17/2013 01:59 PM, Jacek Anaszewski wrote:
[...]
Quoted 109 lines
(quoted diff) +109
-0+ +/* Registers */ +#define OP_REG 0x00 /* Basic operations */ +#define ALS_REG 0x01 /* ALS related settings */ +#define PS_REG 0x02 /* PS related settings */ +#define LED_REG 0x03 /* LED reg */ +#define TL_L_REG 0x04 /* ALS: Threshold low LSB */ +#define TL_H_REG 0x05 /* ALS: Threshold low MSB */ +#define TH_L_REG 0x06 /* ALS: Threshold high LSB */ +#define TH_H_REG 0x07 /* ALS: Threshold high MSB */ +#define PL_L_REG 0x08 /* PS: Threshold low LSB */ +#define PL_H_REG 0x09 /* PS: Threshold low MSB */ +#define PH_L_REG 0x0a /* PS: Threshold high LSB */ +#define PH_H_REG 0x0b /* PS: Threshold high MSB */ +#define D0_L_REG 0x0c /* ALS result: Clear/Illuminance LSB */ +#define D0_H_REG 0x0d /* ALS result: Clear/Illuminance MSB */ +#define D1_L_REG 0x0e /* ALS result: IR LSB */ +#define D1_H_REG 0x0f /* ALS result: IR LSB */ +#define D2_L_REG 0x10 /* PS result LSB */ +#define D2_H_REG 0x11 /* PS result MSB */ +#define REGS_NUM 0x12 /* Number of registers */ + +/* OP_REG bits */ +#define OP3_MASK 0x80 /* Software shutdown */ +#define OP3_SHUTDOWN 0x00 +#define OP3_OPERATION 0x80 +#define OP2_MASK 0x40 /* Auto shutdown/Continuous operation */ +#define OP2_AUTO_SHUTDOWN 0x00 +#define OP2_CONT_OPERATION 0x40 +#define OP_MASK 0x30 /* Operating mode selection */ +#define OP_ALS_AND_PS 0x00 +#define OP_ALS 0x10 +#define OP_PS 0x20 +#define OP_DEBUG 0x30 +#define PROX_MASK 0x08 /* PS: detection/non-detection */ +#define PROX_NON_DETECT 0x00 +#define PROX_DETECT 0x08 +#define FLAG_P 0x04 /* PS: interrupt result */ +#define FLAG_A 0x02 /* ALS: interrupt result */ +#define TYPE_MASK 0x01 /* Output data type selection */ +#define TYPE_MANUAL_CALC 0x00 +#define TYPE_AUTO_CALC 0x01 + +/* ALS_REG bits */ +#define PRST_MASK 0xc0 /* Number of measurement cycles */ +#define PRST_ONCE 0x00 +#define PRST_4_CYCLES 0x40 +#define PRST_8_CYCLES 0x80 +#define PRST_16_CYCLES 0xc0 +#define RES_A_MASK 0x38 /* ALS: Resolution (0.39ms - 800ms) */ +#define RES_A_800ms 0x00 +#define RES_A_400ms 0x08 +#define RES_A_200ms 0x10 +#define RES_A_100ms 0x18 +#define RES_A_25ms 0x20 +#define RES_A_6_25ms 0x28 +#define RES_A_1_56ms 0x30 +#define RES_A_0_39ms 0x38 +#define RANGE_A_MASK 0x07 /* ALS: Max measurable range (x1 - x128) */ +#define RANGE_A_x1 0x00 +#define RANGE_A_x2 0x01 +#define RANGE_A_x4 0x02 +#define RANGE_A_x8 0x03 +#define RANGE_A_x16 0x04 +#define RANGE_A_x32 0x05 +#define RANGE_A_x64 0x06 +#define RANGE_A_x128 0x07 + +/* PS_REG bits */ +#define ALC_MASK 0x80 /* Auto light cancel */ +#define ALC_ON 0x80 +#define ALC_OFF 0x00 +#define INTTYPE_MASK 0x40 /* Interrupt type setting */ +#define INTTYPE_LEVEL 0x00 +#define INTTYPE_PULSE 0x40 +#define RES_P_MASK 0x38 /* PS: Resolution (0.39ms - 800ms) */ +#define RES_P_800ms_x2 0x00 +#define RES_P_400ms_x2 0x08 +#define RES_P_200ms_x2 0x10 +#define RES_P_100ms_x2 0x18 +#define RES_P_25ms_x2 0x20 +#define RES_P_6_25ms_x2 0x28 +#define RES_P_1_56ms_x2 0x30 +#define RES_P_0_39ms_x2 0x38 +#define RANGE_P_MASK 0x07 /* PS: Max measurable range (x1 - x128) */ +#define RANGE_P_x1 0x00 +#define RANGE_P_x2 0x01 +#define RANGE_P_x4 0x02 +#define RANGE_P_x8 0x03 +#define RANGE_P_x16 0x04 +#define RANGE_P_x32 0x05 +#define RANGE_P_x64 0x06 +#define RANGE_P_x128 0x07 + +/* LED reg bits */ +#define INTVAL_MASK 0xc0 /* Intermittent operating */ +#define INTVAL_0 0x00 +#define INTVAL_4 0x40 +#define INTVAL_8 0x80 +#define INTVAL_16 0xc0 +#define IS_MASK 0x30 /* ILED drive peak current setting */ +#define IS_13_8mA 0x00 +#define IS_27_5mA 0x10 +#define IS_55mA 0x20 +#define IS_110mA 0x30 +#define PIN_MASK 0x0c /* INT terminal setting */ +#define PIN_ALS_OR_PS 0x00 +#define PIN_ALS 0x04 +#define PIN_PS 0x08
Review
Quoted 10 lines
(quoted diff) +10
-0+#define PIN_PS_DETECT 0x0c +#define FREQ_MASK 0x02 /* LED modulation frequency */ +#define FREQ_327_5kHz 0x00 +#define FREQ_81_8kHz 0x02 +#define RST 0x01 /* Software reset */ + +#define SCAN_MODE_LIGHT_CLEAR 0 +#define SCAN_MODE_LIGHT_IR 1 +#define SCAN_MODE_PROXIMITY 2 +#define CHAN_TIMESTAMP 3
The defines above should all have namespacing if possible.
Review
Quoted 2 lines
(quoted diff) +2
-0+ +static unsigned long gp2ap002a00f_available_scan_masks[] = {
const
Review
Quoted 3 lines
(quoted diff) +3
-0+ 0x01, + 0x02, + 0x04,
Maybe use BIT(SCAN_MODE_LIGHT_CLEAR), etc. to make this more clear
Quoted 19 lines
(quoted diff) +18
-0+}; + + +static int gp2ap002a00f_exec_cmd(struct gp2ap002a00f_data *data, + enum gp2ap002a00f_cmd cmd) +{ + const u8 thresh_off_buf[2] = {0x00, 0x00}; + int err = 0; + + switch (cmd) { + case GP2AP002A00F_CMD_READ_RAW_CLEAR: + if (data->cur_opmode != GP2AP002A00F_OPMODE_SHUTDOWN) + return -EBUSY; + err = gp2ap002a00f_set_operation_mode(data, + GP2AP002A00F_CMD_READ_RAW_CLEAR); + break; + case GP2AP002A00F_CMD_READ_RAW_IR: + if (data->cur_opmode != GP2AP002A00F_OPMODE_SHUTDOWN)
Review
Quoted 10 lines
(quoted diff) +10
-0+ return -EBUSY; + err = gp2ap002a00f_set_operation_mode(data, + GP2AP002A00F_CMD_READ_RAW_IR); + break; + case GP2AP002A00F_CMD_READ_RAW_PROXIMITY: + if (data->cur_opmode != GP2AP002A00F_OPMODE_SHUTDOWN) + return -EBUSY; + err = gp2ap002a00f_set_operation_mode(data, + GP2AP002A00F_CMD_READ_RAW_PROXIMITY); + break;
There is a lot of copy'n paste here.
I think something like
case GP2AP002A00F_CMD_READ_RAW_CLEAR: case GP2AP002A00F_CMD_READ_RAW_IR: case GP2AP002A00F_CMD_READ_RAW_PROXIMITY: if (data->cur_opmode != GP2AP002A00F_OPMODE_SHUTDOWN) return -EBUSY; err = gp2ap002a00f_set_operation_mode(data, cmd); break;
should also work.
Review
Quoted 7 lines
[...]
(quoted diff) +6
-0+static int gp2ap002a00f_get_reg_cache_word(struct gp2ap002a00f_data *data, + u8 reg_addr) +{ + return (data->reg_cache[reg_addr + 1] << 8) | + data->reg_cache[reg_addr]; +}
This looks like you want to use regmap.
Quoted 14 lines
(quoted diff) +14
-0+ +static irqreturn_t gp2ap002a00f_trigger_handler(int irq, void *data) +{ + struct iio_poll_func *pf = data; + struct iio_dev *indio_dev = pf->indio_dev; + struct gp2ap002a00f_data *lgt = iio_priv(indio_dev); + s64 time_ns; + size_t d_size = 0; + int i, ret; + + for_each_set_bit(i, indio_dev->active_scan_mask, + indio_dev->masklength) { + ret = i2c_smbus_read_i2c_block_data(lgt->client, + GP2AP002A00F_DATA_REG(i), 2,
Review
Quoted 10 lines
(quoted diff) +10
-0+ &lgt->buffer[d_size]); + if (ret < 0) + goto done; + d_size += 2; + } + + if (indio_dev->scan_timestamp) + d_size += sizeof(s64); + + lgt->buffer = kmalloc(d_size, GFP_KERNEL);
Does this make sense? You first write to the buffer in the loop above and afterwards allocate it? The best is probably to allocate the buffer in preenable, so you don't have to allocate and free a new buffer for each transfer.
Quoted 25 lines
(quoted diff) +25
-0+ if (lgt->buffer == NULL) + goto done; + + time_ns = iio_get_time_ns(); + + if (indio_dev->scan_timestamp) + memcpy(lgt->buffer + d_size - sizeof(s64), &time_ns, + sizeof(time_ns)); + + iio_push_to_buffers(indio_dev, lgt->buffer); + + kfree(lgt->buffer); +done: + iio_trigger_notify_done(indio_dev->trig); + + return IRQ_HANDLED; +} + +static int gp2ap002a00f_setup(struct gp2ap002a00f_data *data) +{ + int err = 0; + + data->reg_cache[OP_REG] = OP3_SHUTDOWN; + data->reg_cache[LED_REG] = INTVAL_0 | IS_110mA | FREQ_327_5kHz; + data->reg_cache[ALS_REG] = RES_A_25ms | RANGE_A_x8;
Review
Quoted 10 lines
(quoted diff) +10
-0+ data->reg_cache[PS_REG] = ALC_ON | INTTYPE_LEVEL | RES_P_1_56ms_x2 + | RANGE_P_x4; + + err = i2c_smbus_write_i2c_block_data(data->client, OP_REG, REGS_NUM, + &data->reg_cache[OP_REG]); + + return err; +} + +static u8 get_reg_by_event_code(u64 event_code)
This needs namespacing
Quoted 1 line
+{
[...]
Quoted 9 lines
(quoted diff) +2
-0+} +Quoted 3 lines
+static const struct
iio_buffer_setup_opsgp2ap002a00f_buffer_setup_ops= {(quoted diff) +4
-0+ .preenable = &gp2ap002a00f_buffer_preenable, + .postenable = &iio_triggered_buffer_postenable, + .predisable = &gp2ap002a00f_buffer_predisable, +};
Review
Quoted 10 lines
(quoted diff) +10
-0+ +static int gp2ap002a00f_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct gp2ap002a00f_data *data; + struct iio_dev *indio_dev; + int err; + + pr_info("gp2ap002a00f probe\n"); +
The pr_info probably shouldn't be in the final version.
Quoted 38 lines
(quoted diff) +38
-0+ /* Register with IIO */ + indio_dev = iio_device_alloc(sizeof(*data)); + if (indio_dev == NULL) { + err = -ENOMEM; + goto error_alloc; + } + + data = iio_priv(indio_dev); + + data->vled_reg = devm_regulator_get(&client->dev, "vled"); + if (IS_ERR(data->vled_reg)) { + err = PTR_ERR(data->vled_reg); + goto error_regulator_get; + } + + err = regulator_enable(data->vled_reg); + if (err) + goto error_free_data; + + i2c_set_clientdata(client, indio_dev); + + data->client = client; + data->cur_opmode = GP2AP002A00F_OPMODE_SHUTDOWN; + + init_waitqueue_head(&data->data_ready_queue); + + err = gp2ap002a00f_setup(data); + if (err < 0) + goto error_free_data; + + mutex_init(&data->lock); + indio_dev->dev.parent = &client->dev; + indio_dev->channels = gp2ap002a00f_channels; + indio_dev->num_channels = ARRAY_SIZE(gp2ap002a00f_channels); + indio_dev->info = &gp2ap002a00f_info; + indio_dev->name = id->name; + indio_dev->modes = INDIO_DIRECT_MODE; + indio_dev->available_scan_masks = gp2ap002a00f_available_scan_masks;
Review
Quoted 10 lines
(quoted diff) +10
-0+ + err = iio_triggered_buffer_setup(indio_dev, NULL, + &gp2ap002a00f_trigger_handler, &gp2ap002a00f_buffer_setup_ops); + + err = devm_request_threaded_irq(&client->dev, client->irq, + &gp2ap002a00f_event_handler, + NULL, + IRQF_TRIGGER_FALLING, + "gp2ap002a00f_event", + indio_dev);
Using devm_... here means that the IRQ is freed after the data structures which are used in the IRQ handler are used, so this will cause a use-after-free bug.
Quoted 34 lines
(quoted diff) +34
-0+ if (err < 0) + goto error_uninit_buffer; + + err = iio_device_register(indio_dev); + if (err < 0) + goto error_uninit_buffer; + + return 0; + +error_uninit_buffer: + iio_triggered_buffer_cleanup(indio_dev); +error_free_data: + regulator_disable(data->vled_reg); +error_regulator_get: + iio_device_free(indio_dev); +error_alloc: + return err; +} + +static int gp2ap002a00f_remove(struct i2c_client *client) +{ + struct iio_dev *indio_dev = i2c_get_clientdata(client); + struct gp2ap002a00f_data *data = iio_priv(indio_dev); + + iio_device_unregister(indio_dev); + iio_triggered_buffer_cleanup(indio_dev); + regulator_disable(data->vled_reg); + + iio_device_free(indio_dev); + + return 0; +} + +static const struct i2c_device_id gp2ap002a00f_id[] = {
Review
Quoted 10 lines
(quoted diff) +10
-0+ { GP2A_I2C_NAME, 0 }, + { } +}; + +MODULE_DEVICE_TABLE(i2c, gp2ap002a00f_id); + +#ifdef CONFIG_OF +static const struct of_device_id gp2ap002a00f_of_match[] = { + { .compatible = "sharp,gp2ap002a00f" }, + { .compatible = "gp2ap002a00f" },
The second one obviously shouldn't be there.
Quoted 3 lines
(quoted diff) +3
-0+ { } +}; +#endif
[...]
On 06/22/2013 01:49 PM, Jonathan Cameron wrote:
[...]
Quoted 10 lines
Quoted 6 lines
With the implementation shown above I can setup a trigger using
generic_bufferapplication.Nonetheless, drivers that implement both triggers and events are implemented in a somehow different manner (I followed recommended max1363 driver):
Note the max1363 does not supply a trigger of it's own. That part captures data on demand and has no 'data ready' signal or similar. The above example of buffering suggests that this device has an internal clock and fires a dataready interrupt at regular intervals when monitoring?
Yes.
Quoted 26 lines
Quoted 12 lines
static
irqreturn_tgp2ap002a00f_event_handler(int irq, void *data){ iio_push_event(indio_dev, IIO_MOD_EVENT_CODE( IIO_LIGHT, SCAN_MODE_LIGHT_CLEAR, IIO_MOD_LIGHT_CLEAR, IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING), iio_get_time_ns());Quoted 2 lines
}
This handles the event interrupts, but does nothing about firing off a trigger to any consumers.
Quoted 3 lines
err = iio_triggered_buffer_setup(indio_dev, NULL,
&gp2ap002a00f_trigger_handler,&gp2ap002a00f_buffer_setup_ops);Just note that your event handler in the actual driver sleeps so should the handler should be the 4th parameter with the 3rd null. Note this causes some complexity for iio as 'normally' the triggers are initialized from interrupt context. To that end we play some tricks.
Quoted 8 lines
Right now I can't actually find a driver doing this, I though the lis3l02dq driver did, but it seems that one won't actually run both events and the buffer at the same time.
Anyhow to sketch out the solution you want, combine a standard threaded handler with the trick used in
drivers/iio/trigger/iio-trig-sysfs.cin which andirq_workqueue is used to get us back into interrupt context and fire off the trigger interrupts.
I've applied this solution in the second version of the patch. It works, but I am getting a warning message.
WARNING: at kernel/irq/handle.c:146handle_irq_event_percpu+0x2a4/0x2b4() irq 8 handler iio_pollfunc_store_time+0x0/0x38 enabled interrupts
on a call to iio_trigger_poll from my event irq handler. I suppose it isn't acceptable?
Quoted 2 lines
Mostly hardware designers make our lives easy by directing event and dataready interrupts to different pins!
Unfortunately this is not the case :/
Quoted 7 lines
Quoted 7 lines
err = devm_request_threaded_irq(&client->dev,
client->irq,&gp2ap002a00f_event_handler, NULL, IRQF_TRIGGER_FALLING, "gp2ap002a00f_event", indio_dev);
Quoted 8 lines
Quoted 5 lines
Enabling/disabling data generation seems to be handled by
buffer_setup_ops' preenable/postenable/predisable callbacks. I've noticed also that there isupdate_scan_modecallback exploited in some of the implementations, which I suspect serves for updatingscan_modewithout the need for disabling a trigger?No. To change a scan mask you must disconnect from the trigger. If there are other consumers this means the trigger will briefly stop whilst this one is disconnected then resume for any other consumers.
So what does update_scan_mode callback offer what can't be accomplished in a buffer_preenable callback?
I need to make enabling ambient light related triggers impossible if proximity detection event is enabled. Is there a mechanism I can exploit for this? Currently I am returning -EBUSY from buffer_preenable callback but it doesn't have direct effect for the user, besides kernel log "Buffer not started:buffer parameter update failed". generic_buffer application starts without complaints, it just doesn't display any data.
Quoted 5 lines
Quoted 4 lines
With this implementation the trigger/current_trigger file is empty and
generic_bufferapplication fails. Therefore I ask for clarification on what I am doing wrong here.You aren't actually registering a trigger as far as I can see?
Thank you for your explanation. Now triggers work, but with reservations as above.
Quoted 9 lines
Quoted 9 lines
Regarding the remaining parts of the driver - I used flags, not bit fields as Jonathan requested in my driver for the lps331ap device because of the need for one flag for
wait_event_timeoutfunction. This flag is set in the interrupt handler and access to it must be atomic. I could have gone for bit fields for the rest of the flags in the driver, but I think that now the implementation is more consistent.
Quoted 8 lines
Quoted 3 lines
I've also noticed that once initialized triggers keep some IIO resources unreleased as execution of rmmod on the driver module fails even after triggered capture finish. Is it a known issue?
I have not observed this, perhaps you have not succesfully disconnected the consumer from the trigger? (e.g. if
current_triggeris not empty it will not succeed). There is a probable double free bug in the trigger unregistering that we are chasing down a the moment in another thread.
I observed this also for lps331ap driver. iio_triggered_buffer_cleanup is called in the gp2ap020a00f_remove and it has no chance to be executed as this function is never called on rmmod due to "module in use" issue.
Thanks,
Jacek