Skip to content

Commit 94a6d5c

Browse files
janaszewskijic23
authored andcommitted
iio:ak8975 Implement data ready interrupt handling
Implement "data ready" interrupt handling in addition to the two existing read modes - DRDY GPIO polling and ST1 register DRDY bit polling. Signed-off-by: Jacek Anaszewski <j.anaszewski@samsung.com> Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent f4b7f75 commit 94a6d5c

File tree

1 file changed

+89
-2
lines changed

1 file changed

+89
-2
lines changed

drivers/iio/magnetometer/ak8975.c

+89-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
#include <linux/kernel.h>
2525
#include <linux/slab.h>
2626
#include <linux/i2c.h>
27+
#include <linux/interrupt.h>
2728
#include <linux/err.h>
2829
#include <linux/mutex.h>
2930
#include <linux/delay.h>
30-
31+
#include <linux/bitops.h>
3132
#include <linux/gpio.h>
3233
#include <linux/of_gpio.h>
3334

@@ -83,6 +84,7 @@
8384
*/
8485
#define AK8975_MAX_CONVERSION_TIMEOUT 500
8586
#define AK8975_CONVERSION_DONE_POLL_TIME 10
87+
#define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
8688

8789
/*
8890
* Per-instance context data for the device.
@@ -95,6 +97,9 @@ struct ak8975_data {
9597
long raw_to_gauss[3];
9698
u8 reg_cache[AK8975_MAX_REGS];
9799
int eoc_gpio;
100+
int eoc_irq;
101+
wait_queue_head_t data_ready_queue;
102+
unsigned long flags;
98103
};
99104

100105
static const int ak8975_index_to_reg[] = {
@@ -123,6 +128,51 @@ static int ak8975_write_data(struct i2c_client *client,
123128
return 0;
124129
}
125130

131+
/*
132+
* Handle data ready irq
133+
*/
134+
static irqreturn_t ak8975_irq_handler(int irq, void *data)
135+
{
136+
struct ak8975_data *ak8975 = data;
137+
138+
set_bit(0, &ak8975->flags);
139+
wake_up(&ak8975->data_ready_queue);
140+
141+
return IRQ_HANDLED;
142+
}
143+
144+
/*
145+
* Install data ready interrupt handler
146+
*/
147+
static int ak8975_setup_irq(struct ak8975_data *data)
148+
{
149+
struct i2c_client *client = data->client;
150+
int rc;
151+
int irq;
152+
153+
if (client->irq)
154+
irq = client->irq;
155+
else
156+
irq = gpio_to_irq(data->eoc_gpio);
157+
158+
rc = request_irq(irq, ak8975_irq_handler,
159+
IRQF_TRIGGER_RISING | IRQF_ONESHOT,
160+
dev_name(&client->dev), data);
161+
if (rc < 0) {
162+
dev_err(&client->dev,
163+
"irq %d request failed, (gpio %d): %d\n",
164+
irq, data->eoc_gpio, rc);
165+
return rc;
166+
}
167+
168+
init_waitqueue_head(&data->data_ready_queue);
169+
clear_bit(0, &data->flags);
170+
data->eoc_irq = irq;
171+
172+
return rc;
173+
}
174+
175+
126176
/*
127177
* Perform some start-of-day setup, including reading the asa calibration
128178
* values and caching them.
@@ -171,6 +221,16 @@ static int ak8975_setup(struct i2c_client *client)
171221
AK8975_REG_CNTL_MODE_POWER_DOWN,
172222
AK8975_REG_CNTL_MODE_MASK,
173223
AK8975_REG_CNTL_MODE_SHIFT);
224+
225+
if (data->eoc_gpio > 0 || client->irq) {
226+
ret = ak8975_setup_irq(data);
227+
if (ret < 0) {
228+
dev_err(&client->dev,
229+
"Error setting data ready interrupt\n");
230+
return ret;
231+
}
232+
}
233+
174234
if (ret < 0) {
175235
dev_err(&client->dev, "Error in setting power-down mode\n");
176236
return ret;
@@ -267,9 +327,23 @@ static int wait_conversion_complete_polled(struct ak8975_data *data)
267327
dev_err(&client->dev, "Conversion timeout happened\n");
268328
return -EINVAL;
269329
}
330+
270331
return read_status;
271332
}
272333

334+
/* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
335+
static int wait_conversion_complete_interrupt(struct ak8975_data *data)
336+
{
337+
int ret;
338+
339+
ret = wait_event_timeout(data->data_ready_queue,
340+
test_bit(0, &data->flags),
341+
AK8975_DATA_READY_TIMEOUT);
342+
clear_bit(0, &data->flags);
343+
344+
return ret > 0 ? 0 : -ETIME;
345+
}
346+
273347
/*
274348
* Emits the raw flux value for the x, y, or z axis.
275349
*/
@@ -295,13 +369,16 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
295369
}
296370

297371
/* Wait for the conversion to complete. */
298-
if (gpio_is_valid(data->eoc_gpio))
372+
if (data->eoc_irq)
373+
ret = wait_conversion_complete_interrupt(data);
374+
else if (gpio_is_valid(data->eoc_gpio))
299375
ret = wait_conversion_complete_gpio(data);
300376
else
301377
ret = wait_conversion_complete_polled(data);
302378
if (ret < 0)
303379
goto exit;
304380

381+
/* This will be executed only for non-interrupt based waiting case */
305382
if (ret & AK8975_REG_ST1_DRDY_MASK) {
306383
ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2);
307384
if (ret < 0) {
@@ -415,6 +492,11 @@ static int ak8975_probe(struct i2c_client *client,
415492
}
416493
data = iio_priv(indio_dev);
417494
i2c_set_clientdata(client, indio_dev);
495+
496+
data->client = client;
497+
data->eoc_gpio = eoc_gpio;
498+
data->eoc_irq = 0;
499+
418500
/* Perform some basic start-of-day setup of the device. */
419501
err = ak8975_setup(client);
420502
if (err < 0) {
@@ -439,6 +521,8 @@ static int ak8975_probe(struct i2c_client *client,
439521

440522
exit_free_iio:
441523
iio_device_free(indio_dev);
524+
if (data->eoc_irq)
525+
free_irq(data->eoc_irq, data);
442526
exit_gpio:
443527
if (gpio_is_valid(eoc_gpio))
444528
gpio_free(eoc_gpio);
@@ -453,6 +537,9 @@ static int ak8975_remove(struct i2c_client *client)
453537

454538
iio_device_unregister(indio_dev);
455539

540+
if (data->eoc_irq)
541+
free_irq(data->eoc_irq, data);
542+
456543
if (gpio_is_valid(data->eoc_gpio))
457544
gpio_free(data->eoc_gpio);
458545

0 commit comments

Comments
 (0)