Lines Matching +full:i2c +full:- +full:int +full:- +full:rising
1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright 2010-2011 Analog Devices Inc.
13 #include <linux/i2c.h>
63 * struct ad7150_chip_info - instance specific chip data
64 * @client: i2c client for this device
71 * 3:0 are for timeout receding - applies if below lower threshold
72 * 7:4 are for timeout approaching - applies if above upper threshold
86 int interrupts[2];
101 static int ad7150_read_raw(struct iio_dev *indio_dev, in ad7150_read_raw()
103 int *val, in ad7150_read_raw()
104 int *val2, in ad7150_read_raw()
108 int channel = chan->channel; in ad7150_read_raw()
109 int ret; in ad7150_read_raw()
113 ret = i2c_smbus_read_word_swapped(chip->client, in ad7150_read_raw()
121 ret = i2c_smbus_read_word_swapped(chip->client, in ad7150_read_raw()
138 *val = -(12288 >> 4); /* To match shift in _RAW */ in ad7150_read_raw()
145 return -EINVAL; in ad7150_read_raw()
149 static int ad7150_read_event_config(struct iio_dev *indio_dev, in ad7150_read_event_config()
157 int ret; in ad7150_read_event_config()
159 ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG_REG); in ad7150_read_event_config()
180 return -EINVAL; in ad7150_read_event_config()
184 static int ad7150_write_event_params(struct iio_dev *indio_dev, in ad7150_write_event_params()
185 unsigned int chan, in ad7150_write_event_params()
190 int rising = (dir == IIO_EV_DIR_RISING); in ad7150_write_event_params() local
193 if ((type != chip->type) || (dir != chip->dir)) in ad7150_write_event_params()
199 u16 value = chip->threshold[rising][chan]; in ad7150_write_event_params()
200 return i2c_smbus_write_word_swapped(chip->client, in ad7150_write_event_params()
205 int ret; in ad7150_write_event_params()
208 sens = chip->thresh_sensitivity[rising][chan]; in ad7150_write_event_params()
209 ret = i2c_smbus_write_byte_data(chip->client, in ad7150_write_event_params()
220 chip->thresh_timeout[1][chan]); in ad7150_write_event_params()
222 chip->thresh_timeout[0][chan]); in ad7150_write_event_params()
223 return i2c_smbus_write_byte_data(chip->client, in ad7150_write_event_params()
228 return -EINVAL; in ad7150_write_event_params()
232 static int ad7150_write_event_config(struct iio_dev *indio_dev, in ad7150_write_event_config()
235 enum iio_event_direction dir, int state) in ad7150_write_event_config()
238 int ret = 0; in ad7150_write_event_config()
249 if ((chip->int_enabled[chan->channel]) && in ad7150_write_event_config()
250 (type == chip->type) && (dir == chip->dir)) { in ad7150_write_event_config()
251 disable_irq(chip->interrupts[chan->channel]); in ad7150_write_event_config()
252 chip->int_enabled[chan->channel] = false; in ad7150_write_event_config()
257 mutex_lock(&chip->state_lock); in ad7150_write_event_config()
258 if ((type != chip->type) || (dir != chip->dir)) { in ad7150_write_event_config()
259 int rising = (dir == IIO_EV_DIR_RISING); in ad7150_write_event_config() local
264 * enabled - this is to avoid races around changing in ad7150_write_event_config()
269 disable_irq(chip->interrupts[0]); in ad7150_write_event_config()
270 disable_irq(chip->interrupts[1]); in ad7150_write_event_config()
272 ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG_REG); in ad7150_write_event_config()
283 if (rising) in ad7150_write_event_config()
291 ret = i2c_smbus_write_byte_data(chip->client, AD7150_CFG_REG, in ad7150_write_event_config()
301 chip->type = type; in ad7150_write_event_config()
302 chip->dir = dir; in ad7150_write_event_config()
305 ret = ad7150_write_event_params(indio_dev, chan->channel, type, in ad7150_write_event_config()
310 enable_irq(chip->interrupts[0]); in ad7150_write_event_config()
311 enable_irq(chip->interrupts[1]); in ad7150_write_event_config()
313 if (!chip->int_enabled[chan->channel]) { in ad7150_write_event_config()
314 enable_irq(chip->interrupts[chan->channel]); in ad7150_write_event_config()
315 chip->int_enabled[chan->channel] = true; in ad7150_write_event_config()
319 mutex_unlock(&chip->state_lock); in ad7150_write_event_config()
324 static int ad7150_read_event_value(struct iio_dev *indio_dev, in ad7150_read_event_value()
329 int *val, int *val2) in ad7150_read_event_value()
332 int rising = (dir == IIO_EV_DIR_RISING); in ad7150_read_event_value() local
339 *val = chip->thresh_sensitivity[rising][chan->channel]; in ad7150_read_event_value()
342 *val = chip->threshold[rising][chan->channel]; in ad7150_read_event_value()
345 return -EINVAL; in ad7150_read_event_value()
349 *val2 = chip->thresh_timeout[rising][chan->channel] * 10000; in ad7150_read_event_value()
352 return -EINVAL; in ad7150_read_event_value()
356 static int ad7150_write_event_value(struct iio_dev *indio_dev, in ad7150_write_event_value()
361 int val, int val2) in ad7150_write_event_value()
363 int ret; in ad7150_write_event_value()
365 int rising = (dir == IIO_EV_DIR_RISING); in ad7150_write_event_value() local
367 mutex_lock(&chip->state_lock); in ad7150_write_event_value()
372 chip->thresh_sensitivity[rising][chan->channel] = val; in ad7150_write_event_value()
375 chip->threshold[rising][chan->channel] = val; in ad7150_write_event_value()
378 ret = -EINVAL; in ad7150_write_event_value()
388 int timeout = val2 / 10000; in ad7150_write_event_value()
391 ret = -EINVAL; in ad7150_write_event_value()
395 chip->thresh_timeout[rising][chan->channel] = timeout; in ad7150_write_event_value()
399 ret = -EINVAL; in ad7150_write_event_value()
404 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir); in ad7150_write_event_value()
407 mutex_unlock(&chip->state_lock); in ad7150_write_event_value()
480 int channel) in __ad7150_event_handler()
485 int int_status; in __ad7150_event_handler()
487 int_status = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS_REG); in __ad7150_event_handler()
496 chip->type, chip->dir), in __ad7150_event_handler()
502 static irqreturn_t ad7150_event_handler_ch1(int irq, void *private) in ad7150_event_handler_ch1()
507 static irqreturn_t ad7150_event_handler_ch2(int irq, void *private) in ad7150_event_handler_ch2()
546 static int ad7150_probe(struct i2c_client *client, in ad7150_probe()
552 int ret; in ad7150_probe()
554 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); in ad7150_probe()
556 return -ENOMEM; in ad7150_probe()
559 mutex_init(&chip->state_lock); in ad7150_probe()
560 chip->client = client; in ad7150_probe()
562 indio_dev->name = id->name; in ad7150_probe()
564 indio_dev->modes = INDIO_DIRECT_MODE; in ad7150_probe()
566 reg = devm_regulator_get(&client->dev, "vdd"); in ad7150_probe()
574 ret = devm_add_action_or_reset(&client->dev, ad7150_reg_disable, reg); in ad7150_probe()
578 chip->interrupts[0] = fwnode_irq_get(dev_fwnode(&client->dev), 0); in ad7150_probe()
579 if (chip->interrupts[0] < 0) in ad7150_probe()
580 return chip->interrupts[0]; in ad7150_probe()
581 if (id->driver_data == AD7150) { in ad7150_probe()
582 chip->interrupts[1] = fwnode_irq_get(dev_fwnode(&client->dev), 1); in ad7150_probe()
583 if (chip->interrupts[1] < 0) in ad7150_probe()
584 return chip->interrupts[1]; in ad7150_probe()
586 if (chip->interrupts[0] && in ad7150_probe()
587 (id->driver_data == AD7151 || chip->interrupts[1])) { in ad7150_probe()
588 irq_set_status_flags(chip->interrupts[0], IRQ_NOAUTOEN); in ad7150_probe()
589 ret = devm_request_threaded_irq(&client->dev, in ad7150_probe()
590 chip->interrupts[0], in ad7150_probe()
600 indio_dev->info = &ad7150_info; in ad7150_probe()
601 switch (id->driver_data) { in ad7150_probe()
603 indio_dev->channels = ad7150_channels; in ad7150_probe()
604 indio_dev->num_channels = ARRAY_SIZE(ad7150_channels); in ad7150_probe()
605 irq_set_status_flags(chip->interrupts[1], IRQ_NOAUTOEN); in ad7150_probe()
606 ret = devm_request_threaded_irq(&client->dev, in ad7150_probe()
607 chip->interrupts[1], in ad7150_probe()
618 indio_dev->channels = ad7151_channels; in ad7150_probe()
619 indio_dev->num_channels = ARRAY_SIZE(ad7151_channels); in ad7150_probe()
622 return -EINVAL; in ad7150_probe()
626 indio_dev->info = &ad7150_info_no_irq; in ad7150_probe()
627 switch (id->driver_data) { in ad7150_probe()
629 indio_dev->channels = ad7150_channels_no_irq; in ad7150_probe()
630 indio_dev->num_channels = in ad7150_probe()
634 indio_dev->channels = ad7151_channels_no_irq; in ad7150_probe()
635 indio_dev->num_channels = in ad7150_probe()
639 return -EINVAL; in ad7150_probe()
643 return devm_iio_device_register(indio_dev->dev.parent, indio_dev); in ad7150_probe()
653 MODULE_DEVICE_TABLE(i2c, ad7150_id);