Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

iio: mpl3115: add support for DRDY interrupt

MPL3115 sensor features a "data ready" interrupt which indicates the
presence of new measurements.

Signed-off-by: Antoni Pokusinski <apokusinski01@gmail.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Antoni Pokusinski and committed by
Jonathan Cameron
8464f610 b4105b20

+175 -8
+175 -8
drivers/iio/pressure/mpl3115.c
··· 7 7 * (7-bit I2C slave address 0x60) 8 8 * 9 9 * TODO: FIFO buffer, altimeter mode, oversampling, continuous mode, 10 - * interrupts, user offset correction, raw mode 10 + * user offset correction, raw mode 11 11 */ 12 12 13 - #include <linux/module.h> 13 + #include <linux/cleanup.h> 14 + #include <linux/delay.h> 14 15 #include <linux/i2c.h> 16 + #include <linux/module.h> 17 + #include <linux/property.h> 18 + 19 + #include <linux/iio/buffer.h> 15 20 #include <linux/iio/iio.h> 16 21 #include <linux/iio/sysfs.h> 17 - #include <linux/iio/trigger_consumer.h> 18 - #include <linux/iio/buffer.h> 19 22 #include <linux/iio/triggered_buffer.h> 20 - #include <linux/delay.h> 23 + #include <linux/iio/trigger_consumer.h> 24 + #include <linux/iio/trigger.h> 21 25 22 26 #define MPL3115_STATUS 0x00 23 27 #define MPL3115_OUT_PRESS 0x01 /* MSB first, 20 bit */ 24 28 #define MPL3115_OUT_TEMP 0x04 /* MSB first, 12 bit */ 25 29 #define MPL3115_WHO_AM_I 0x0c 30 + #define MPL3115_INT_SOURCE 0x12 31 + #define MPL3115_PT_DATA_CFG 0x13 26 32 #define MPL3115_CTRL_REG1 0x26 33 + #define MPL3115_CTRL_REG3 0x28 34 + #define MPL3115_CTRL_REG4 0x29 35 + #define MPL3115_CTRL_REG5 0x2a 27 36 28 37 #define MPL3115_DEVICE_ID 0xc4 29 38 30 39 #define MPL3115_STATUS_PRESS_RDY BIT(2) 31 40 #define MPL3115_STATUS_TEMP_RDY BIT(1) 32 41 42 + #define MPL3115_INT_SRC_DRDY BIT(7) 43 + 44 + #define MPL3115_PT_DATA_EVENT_ALL GENMASK(2, 0) 45 + 33 46 #define MPL3115_CTRL1_RESET BIT(2) /* software reset */ 34 47 #define MPL3115_CTRL1_OST BIT(1) /* initiate measurement */ 35 48 #define MPL3115_CTRL1_ACTIVE BIT(0) /* continuous measurement */ 36 49 #define MPL3115_CTRL1_OS_258MS GENMASK(5, 4) /* 64x oversampling */ 37 50 51 + #define MPL3115_CTRL3_IPOL1 BIT(5) 52 + #define MPL3115_CTRL3_IPOL2 BIT(1) 53 + 54 + #define MPL3115_CTRL4_INT_EN_DRDY BIT(7) 55 + 56 + #define MPL3115_CTRL5_INT_CFG_DRDY BIT(7) 57 + 38 58 struct mpl3115_data { 39 59 struct i2c_client *client; 60 + struct iio_trigger *drdy_trig; 40 61 struct mutex lock; 41 62 u8 ctrl_reg1; 63 + }; 64 + 65 + enum mpl3115_irq_pin { 66 + MPL3115_IRQ_INT1, 67 + MPL3115_IRQ_INT2, 42 68 }; 43 69 44 70 static int mpl3115_request(struct mpl3115_data *data) ··· 179 153 struct mpl3115_data *data = iio_priv(indio_dev); 180 154 int ret, pos = 0; 181 155 182 - ret = mpl3115_request(data); 183 - if (ret < 0) 184 - return ret; 156 + if (!(data->ctrl_reg1 & MPL3115_CTRL1_ACTIVE)) { 157 + ret = mpl3115_request(data); 158 + if (ret < 0) 159 + return ret; 160 + } 185 161 186 162 if (test_bit(0, indio_dev->active_scan_mask)) { 187 163 ret = i2c_smbus_read_i2c_block_data(data->client, ··· 262 234 IIO_CHAN_SOFT_TIMESTAMP(2), 263 235 }; 264 236 237 + static irqreturn_t mpl3115_interrupt_handler(int irq, void *private) 238 + { 239 + struct iio_dev *indio_dev = private; 240 + struct mpl3115_data *data = iio_priv(indio_dev); 241 + int ret; 242 + 243 + ret = i2c_smbus_read_byte_data(data->client, MPL3115_INT_SOURCE); 244 + if (ret < 0) 245 + return IRQ_HANDLED; 246 + 247 + if (!(ret & MPL3115_INT_SRC_DRDY)) 248 + return IRQ_NONE; 249 + 250 + iio_trigger_poll_nested(data->drdy_trig); 251 + 252 + return IRQ_HANDLED; 253 + } 254 + 255 + static int mpl3115_config_interrupt(struct mpl3115_data *data, 256 + u8 ctrl_reg1, u8 ctrl_reg4) 257 + { 258 + int ret; 259 + 260 + ret = i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1, 261 + ctrl_reg1); 262 + if (ret < 0) 263 + return ret; 264 + 265 + ret = i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG4, 266 + ctrl_reg4); 267 + if (ret < 0) 268 + goto reg1_cleanup; 269 + 270 + data->ctrl_reg1 = ctrl_reg1; 271 + 272 + return 0; 273 + 274 + reg1_cleanup: 275 + i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1, 276 + data->ctrl_reg1); 277 + return ret; 278 + } 279 + 280 + static int mpl3115_set_trigger_state(struct iio_trigger *trig, bool state) 281 + { 282 + struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 283 + struct mpl3115_data *data = iio_priv(indio_dev); 284 + u8 ctrl_reg1 = data->ctrl_reg1; 285 + u8 ctrl_reg4 = state ? MPL3115_CTRL4_INT_EN_DRDY : 0; 286 + 287 + if (state) 288 + ctrl_reg1 |= MPL3115_CTRL1_ACTIVE; 289 + else 290 + ctrl_reg1 &= ~MPL3115_CTRL1_ACTIVE; 291 + 292 + guard(mutex)(&data->lock); 293 + 294 + return mpl3115_config_interrupt(data, ctrl_reg1, ctrl_reg4); 295 + } 296 + 297 + static const struct iio_trigger_ops mpl3115_trigger_ops = { 298 + .set_trigger_state = mpl3115_set_trigger_state, 299 + }; 300 + 265 301 static const struct iio_info mpl3115_info = { 266 302 .read_raw = &mpl3115_read_raw, 267 303 }; 304 + 305 + static int mpl3115_trigger_probe(struct mpl3115_data *data, 306 + struct iio_dev *indio_dev) 307 + { 308 + struct fwnode_handle *fwnode = dev_fwnode(&data->client->dev); 309 + int ret, irq, irq_type, irq_pin = MPL3115_IRQ_INT1; 310 + 311 + irq = fwnode_irq_get_byname(fwnode, "INT1"); 312 + if (irq < 0) { 313 + irq = fwnode_irq_get_byname(fwnode, "INT2"); 314 + if (irq < 0) 315 + return 0; 316 + 317 + irq_pin = MPL3115_IRQ_INT2; 318 + } 319 + 320 + irq_type = irq_get_trigger_type(irq); 321 + if (irq_type != IRQF_TRIGGER_RISING && irq_type != IRQF_TRIGGER_FALLING) 322 + return -EINVAL; 323 + 324 + ret = i2c_smbus_write_byte_data(data->client, MPL3115_PT_DATA_CFG, 325 + MPL3115_PT_DATA_EVENT_ALL); 326 + if (ret < 0) 327 + return ret; 328 + 329 + if (irq_pin == MPL3115_IRQ_INT1) { 330 + ret = i2c_smbus_write_byte_data(data->client, 331 + MPL3115_CTRL_REG5, 332 + MPL3115_CTRL5_INT_CFG_DRDY); 333 + if (ret) 334 + return ret; 335 + 336 + if (irq_type == IRQF_TRIGGER_RISING) { 337 + ret = i2c_smbus_write_byte_data(data->client, 338 + MPL3115_CTRL_REG3, 339 + MPL3115_CTRL3_IPOL1); 340 + if (ret) 341 + return ret; 342 + } 343 + } else if (irq_type == IRQF_TRIGGER_RISING) { 344 + ret = i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG3, 345 + MPL3115_CTRL3_IPOL2); 346 + if (ret) 347 + return ret; 348 + } 349 + 350 + data->drdy_trig = devm_iio_trigger_alloc(&data->client->dev, 351 + "%s-dev%d", 352 + indio_dev->name, 353 + iio_device_id(indio_dev)); 354 + if (!data->drdy_trig) 355 + return -ENOMEM; 356 + 357 + data->drdy_trig->ops = &mpl3115_trigger_ops; 358 + iio_trigger_set_drvdata(data->drdy_trig, indio_dev); 359 + 360 + ret = devm_request_threaded_irq(&data->client->dev, irq, NULL, 361 + mpl3115_interrupt_handler, 362 + IRQF_ONESHOT, 363 + "mpl3115_irq", indio_dev); 364 + if (ret) 365 + return ret; 366 + 367 + ret = devm_iio_trigger_register(&data->client->dev, data->drdy_trig); 368 + if (ret) 369 + return ret; 370 + 371 + indio_dev->trig = iio_trigger_get(data->drdy_trig); 372 + 373 + return 0; 374 + } 268 375 269 376 static int mpl3115_probe(struct i2c_client *client) 270 377 { ··· 438 275 ret = i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1, 439 276 data->ctrl_reg1); 440 277 if (ret < 0) 278 + return ret; 279 + 280 + ret = mpl3115_trigger_probe(data, indio_dev); 281 + if (ret) 441 282 return ret; 442 283 443 284 ret = iio_triggered_buffer_setup(indio_dev, NULL,