1b3c590ceSMatt Ranostay /* 2b3c590ceSMatt Ranostay * max30102.c - Support for MAX30102 heart rate and pulse oximeter sensor 3b3c590ceSMatt Ranostay * 4b3c590ceSMatt Ranostay * Copyright (C) 2017 Matt Ranostay <matt@ranostay.consulting> 5b3c590ceSMatt Ranostay * 6b3c590ceSMatt Ranostay * This program is free software; you can redistribute it and/or modify 7b3c590ceSMatt Ranostay * it under the terms of the GNU General Public License as published by 8b3c590ceSMatt Ranostay * the Free Software Foundation; either version 2 of the License, or 9b3c590ceSMatt Ranostay * (at your option) any later version. 10b3c590ceSMatt Ranostay * 11b3c590ceSMatt Ranostay * This program is distributed in the hope that it will be useful, 12b3c590ceSMatt Ranostay * but WITHOUT ANY WARRANTY; without even the implied warranty of 13b3c590ceSMatt Ranostay * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14b3c590ceSMatt Ranostay * GNU General Public License for more details. 15b3c590ceSMatt Ranostay * 16b3c590ceSMatt Ranostay * TODO: proximity power saving feature 17b3c590ceSMatt Ranostay */ 18b3c590ceSMatt Ranostay 19b3c590ceSMatt Ranostay #include <linux/module.h> 20b3c590ceSMatt Ranostay #include <linux/init.h> 21b3c590ceSMatt Ranostay #include <linux/interrupt.h> 22b3c590ceSMatt Ranostay #include <linux/delay.h> 23b3c590ceSMatt Ranostay #include <linux/err.h> 24b3c590ceSMatt Ranostay #include <linux/irq.h> 25b3c590ceSMatt Ranostay #include <linux/i2c.h> 26b3c590ceSMatt Ranostay #include <linux/mutex.h> 27b3c590ceSMatt Ranostay #include <linux/of.h> 28b3c590ceSMatt Ranostay #include <linux/regmap.h> 29b3c590ceSMatt Ranostay #include <linux/iio/iio.h> 30b3c590ceSMatt Ranostay #include <linux/iio/buffer.h> 31b3c590ceSMatt Ranostay #include <linux/iio/kfifo_buf.h> 32b3c590ceSMatt Ranostay 33b3c590ceSMatt Ranostay #define MAX30102_REGMAP_NAME "max30102_regmap" 34b3c590ceSMatt Ranostay #define MAX30102_DRV_NAME "max30102" 3584b0ce05SPeter Meerwald-Stadler #define MAX30102_PART_NUMBER 0x15 36b3c590ceSMatt Ranostay 37ad90e570SPeter Meerwald-Stadler enum max3012_led_idx { 38ad90e570SPeter Meerwald-Stadler MAX30102_LED_RED, 39ad90e570SPeter Meerwald-Stadler MAX30102_LED_IR, 40ad90e570SPeter Meerwald-Stadler }; 41ad90e570SPeter Meerwald-Stadler 42b3c590ceSMatt Ranostay #define MAX30102_REG_INT_STATUS 0x00 43b3c590ceSMatt Ranostay #define MAX30102_REG_INT_STATUS_PWR_RDY BIT(0) 44b3c590ceSMatt Ranostay #define MAX30102_REG_INT_STATUS_PROX_INT BIT(4) 45b3c590ceSMatt Ranostay #define MAX30102_REG_INT_STATUS_ALC_OVF BIT(5) 46b3c590ceSMatt Ranostay #define MAX30102_REG_INT_STATUS_PPG_RDY BIT(6) 47b3c590ceSMatt Ranostay #define MAX30102_REG_INT_STATUS_FIFO_RDY BIT(7) 48b3c590ceSMatt Ranostay 49b3c590ceSMatt Ranostay #define MAX30102_REG_INT_ENABLE 0x02 50b3c590ceSMatt Ranostay #define MAX30102_REG_INT_ENABLE_PROX_INT_EN BIT(4) 51b3c590ceSMatt Ranostay #define MAX30102_REG_INT_ENABLE_ALC_OVF_EN BIT(5) 52b3c590ceSMatt Ranostay #define MAX30102_REG_INT_ENABLE_PPG_EN BIT(6) 53b3c590ceSMatt Ranostay #define MAX30102_REG_INT_ENABLE_FIFO_EN BIT(7) 54b3c590ceSMatt Ranostay #define MAX30102_REG_INT_ENABLE_MASK 0xf0 55b3c590ceSMatt Ranostay #define MAX30102_REG_INT_ENABLE_MASK_SHIFT 4 56b3c590ceSMatt Ranostay 57b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_WR_PTR 0x04 58b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_OVR_CTR 0x05 59b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_RD_PTR 0x06 60b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_DATA 0x07 61*fef79edcSPeter Meerwald-Stadler #define MAX30102_REG_FIFO_DATA_BYTES 3 62b3c590ceSMatt Ranostay 63b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_CONFIG 0x08 64b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_CONFIG_AVG_4SAMPLES BIT(1) 65b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_CONFIG_AVG_SHIFT 5 66b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_CONFIG_AFULL BIT(0) 67b3c590ceSMatt Ranostay 68b3c590ceSMatt Ranostay #define MAX30102_REG_MODE_CONFIG 0x09 6983e6415dSPeter Meerwald-Stadler #define MAX30102_REG_MODE_CONFIG_MODE_NONE 0x00 707b0b0ec1SPeter Meerwald-Stadler #define MAX30102_REG_MODE_CONFIG_MODE_HR 0x02 /* red LED */ 717b0b0ec1SPeter Meerwald-Stadler #define MAX30102_REG_MODE_CONFIG_MODE_HR_SPO2 0x03 /* red + IR LED */ 727b0b0ec1SPeter Meerwald-Stadler #define MAX30102_REG_MODE_CONFIG_MODE_MULTI 0x07 /* multi-LED mode */ 737b0b0ec1SPeter Meerwald-Stadler #define MAX30102_REG_MODE_CONFIG_MODE_MASK GENMASK(2, 0) 74b3c590ceSMatt Ranostay #define MAX30102_REG_MODE_CONFIG_PWR BIT(7) 75b3c590ceSMatt Ranostay 76b3c590ceSMatt Ranostay #define MAX30102_REG_SPO2_CONFIG 0x0a 77b3c590ceSMatt Ranostay #define MAX30102_REG_SPO2_CONFIG_PULSE_411_US 0x03 78b3c590ceSMatt Ranostay #define MAX30102_REG_SPO2_CONFIG_SR_400HZ 0x03 79b3c590ceSMatt Ranostay #define MAX30102_REG_SPO2_CONFIG_SR_MASK 0x07 80b3c590ceSMatt Ranostay #define MAX30102_REG_SPO2_CONFIG_SR_MASK_SHIFT 2 81b3c590ceSMatt Ranostay #define MAX30102_REG_SPO2_CONFIG_ADC_4096_STEPS BIT(0) 82b3c590ceSMatt Ranostay #define MAX30102_REG_SPO2_CONFIG_ADC_MASK_SHIFT 5 83b3c590ceSMatt Ranostay 84b3c590ceSMatt Ranostay #define MAX30102_REG_RED_LED_CONFIG 0x0c 85b3c590ceSMatt Ranostay #define MAX30102_REG_IR_LED_CONFIG 0x0d 86b3c590ceSMatt Ranostay 87b3c590ceSMatt Ranostay #define MAX30102_REG_TEMP_CONFIG 0x21 88b3c590ceSMatt Ranostay #define MAX30102_REG_TEMP_CONFIG_TEMP_EN BIT(0) 89b3c590ceSMatt Ranostay 90b3c590ceSMatt Ranostay #define MAX30102_REG_TEMP_INTEGER 0x1f 91b3c590ceSMatt Ranostay #define MAX30102_REG_TEMP_FRACTION 0x20 92b3c590ceSMatt Ranostay 9384b0ce05SPeter Meerwald-Stadler #define MAX30102_REG_REV_ID 0xfe 9484b0ce05SPeter Meerwald-Stadler #define MAX30102_REG_PART_ID 0xff 9584b0ce05SPeter Meerwald-Stadler 96b3c590ceSMatt Ranostay struct max30102_data { 97b3c590ceSMatt Ranostay struct i2c_client *client; 98b3c590ceSMatt Ranostay struct iio_dev *indio_dev; 99b3c590ceSMatt Ranostay struct mutex lock; 100b3c590ceSMatt Ranostay struct regmap *regmap; 101b3c590ceSMatt Ranostay 102b3c590ceSMatt Ranostay u8 buffer[8]; 103b3c590ceSMatt Ranostay __be32 processed_buffer[2]; /* 2 x 18-bit (padded to 32-bits) */ 104b3c590ceSMatt Ranostay }; 105b3c590ceSMatt Ranostay 106b3c590ceSMatt Ranostay static const struct regmap_config max30102_regmap_config = { 107b3c590ceSMatt Ranostay .name = MAX30102_REGMAP_NAME, 108b3c590ceSMatt Ranostay 109b3c590ceSMatt Ranostay .reg_bits = 8, 110b3c590ceSMatt Ranostay .val_bits = 8, 111b3c590ceSMatt Ranostay }; 112b3c590ceSMatt Ranostay 113ad90e570SPeter Meerwald-Stadler static const unsigned long max30102_scan_masks[] = { 114ad90e570SPeter Meerwald-Stadler BIT(MAX30102_LED_RED) | BIT(MAX30102_LED_IR), 115ad90e570SPeter Meerwald-Stadler 0 116ad90e570SPeter Meerwald-Stadler }; 117b3c590ceSMatt Ranostay 118839a74cdSPeter Meerwald-Stadler #define MAX30102_INTENSITY_CHANNEL(_si, _mod) { \ 119839a74cdSPeter Meerwald-Stadler .type = IIO_INTENSITY, \ 120839a74cdSPeter Meerwald-Stadler .channel2 = _mod, \ 121839a74cdSPeter Meerwald-Stadler .modified = 1, \ 122839a74cdSPeter Meerwald-Stadler .scan_index = _si, \ 123839a74cdSPeter Meerwald-Stadler .scan_type = { \ 124839a74cdSPeter Meerwald-Stadler .sign = 'u', \ 125839a74cdSPeter Meerwald-Stadler .shift = 8, \ 126839a74cdSPeter Meerwald-Stadler .realbits = 18, \ 127839a74cdSPeter Meerwald-Stadler .storagebits = 32, \ 128839a74cdSPeter Meerwald-Stadler .endianness = IIO_BE, \ 129839a74cdSPeter Meerwald-Stadler }, \ 130839a74cdSPeter Meerwald-Stadler } 131839a74cdSPeter Meerwald-Stadler 132b3c590ceSMatt Ranostay static const struct iio_chan_spec max30102_channels[] = { 133ad90e570SPeter Meerwald-Stadler MAX30102_INTENSITY_CHANNEL(MAX30102_LED_RED, IIO_MOD_LIGHT_RED), 134ad90e570SPeter Meerwald-Stadler MAX30102_INTENSITY_CHANNEL(MAX30102_LED_IR, IIO_MOD_LIGHT_IR), 135b3c590ceSMatt Ranostay { 136b3c590ceSMatt Ranostay .type = IIO_TEMP, 137b3c590ceSMatt Ranostay .info_mask_separate = 138b3c590ceSMatt Ranostay BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 139b3c590ceSMatt Ranostay .scan_index = -1, 140b3c590ceSMatt Ranostay }, 141b3c590ceSMatt Ranostay }; 142b3c590ceSMatt Ranostay 14383e6415dSPeter Meerwald-Stadler static int max30102_set_power(struct max30102_data *data, bool en) 144b3c590ceSMatt Ranostay { 145b3c590ceSMatt Ranostay return regmap_update_bits(data->regmap, MAX30102_REG_MODE_CONFIG, 146b3c590ceSMatt Ranostay MAX30102_REG_MODE_CONFIG_PWR, 14783e6415dSPeter Meerwald-Stadler en ? 0 : MAX30102_REG_MODE_CONFIG_PWR); 14883e6415dSPeter Meerwald-Stadler } 14983e6415dSPeter Meerwald-Stadler 15083e6415dSPeter Meerwald-Stadler static int max30102_set_powermode(struct max30102_data *data, u8 mode, bool en) 15183e6415dSPeter Meerwald-Stadler { 15283e6415dSPeter Meerwald-Stadler u8 reg = mode; 15383e6415dSPeter Meerwald-Stadler 15483e6415dSPeter Meerwald-Stadler if (!en) 15583e6415dSPeter Meerwald-Stadler reg |= MAX30102_REG_MODE_CONFIG_PWR; 15683e6415dSPeter Meerwald-Stadler 15783e6415dSPeter Meerwald-Stadler return regmap_update_bits(data->regmap, MAX30102_REG_MODE_CONFIG, 15883e6415dSPeter Meerwald-Stadler MAX30102_REG_MODE_CONFIG_PWR | 15983e6415dSPeter Meerwald-Stadler MAX30102_REG_MODE_CONFIG_MODE_MASK, reg); 160b3c590ceSMatt Ranostay } 161b3c590ceSMatt Ranostay 162b3c590ceSMatt Ranostay static int max30102_buffer_postenable(struct iio_dev *indio_dev) 163b3c590ceSMatt Ranostay { 164b3c590ceSMatt Ranostay struct max30102_data *data = iio_priv(indio_dev); 16583e6415dSPeter Meerwald-Stadler u8 reg; 166b3c590ceSMatt Ranostay 16783e6415dSPeter Meerwald-Stadler reg = MAX30102_REG_MODE_CONFIG_MODE_HR_SPO2; 16883e6415dSPeter Meerwald-Stadler 16983e6415dSPeter Meerwald-Stadler return max30102_set_powermode(data, reg, true); 170b3c590ceSMatt Ranostay } 171b3c590ceSMatt Ranostay 172b3c590ceSMatt Ranostay static int max30102_buffer_predisable(struct iio_dev *indio_dev) 173b3c590ceSMatt Ranostay { 174b3c590ceSMatt Ranostay struct max30102_data *data = iio_priv(indio_dev); 175b3c590ceSMatt Ranostay 17683e6415dSPeter Meerwald-Stadler return max30102_set_powermode(data, MAX30102_REG_MODE_CONFIG_MODE_NONE, 17783e6415dSPeter Meerwald-Stadler false); 178b3c590ceSMatt Ranostay } 179b3c590ceSMatt Ranostay 180b3c590ceSMatt Ranostay static const struct iio_buffer_setup_ops max30102_buffer_setup_ops = { 181b3c590ceSMatt Ranostay .postenable = max30102_buffer_postenable, 182b3c590ceSMatt Ranostay .predisable = max30102_buffer_predisable, 183b3c590ceSMatt Ranostay }; 184b3c590ceSMatt Ranostay 185b3c590ceSMatt Ranostay static inline int max30102_fifo_count(struct max30102_data *data) 186b3c590ceSMatt Ranostay { 187b3c590ceSMatt Ranostay unsigned int val; 188b3c590ceSMatt Ranostay int ret; 189b3c590ceSMatt Ranostay 190b3c590ceSMatt Ranostay ret = regmap_read(data->regmap, MAX30102_REG_INT_STATUS, &val); 191b3c590ceSMatt Ranostay if (ret) 192b3c590ceSMatt Ranostay return ret; 193b3c590ceSMatt Ranostay 194b3c590ceSMatt Ranostay /* FIFO has one sample slot left */ 195b3c590ceSMatt Ranostay if (val & MAX30102_REG_INT_STATUS_FIFO_RDY) 196b3c590ceSMatt Ranostay return 1; 197b3c590ceSMatt Ranostay 198b3c590ceSMatt Ranostay return 0; 199b3c590ceSMatt Ranostay } 200b3c590ceSMatt Ranostay 201*fef79edcSPeter Meerwald-Stadler #define MAX30102_COPY_DATA(i) \ 202*fef79edcSPeter Meerwald-Stadler memcpy(&data->processed_buffer[(i)], \ 203*fef79edcSPeter Meerwald-Stadler &buffer[(i) * MAX30102_REG_FIFO_DATA_BYTES], \ 204*fef79edcSPeter Meerwald-Stadler MAX30102_REG_FIFO_DATA_BYTES) 205*fef79edcSPeter Meerwald-Stadler 206b3c590ceSMatt Ranostay static int max30102_read_measurement(struct max30102_data *data) 207b3c590ceSMatt Ranostay { 208b3c590ceSMatt Ranostay int ret; 209b3c590ceSMatt Ranostay u8 *buffer = (u8 *) &data->buffer; 210b3c590ceSMatt Ranostay 211b3c590ceSMatt Ranostay ret = i2c_smbus_read_i2c_block_data(data->client, 212b3c590ceSMatt Ranostay MAX30102_REG_FIFO_DATA, 213*fef79edcSPeter Meerwald-Stadler 2 * MAX30102_REG_FIFO_DATA_BYTES, 214b3c590ceSMatt Ranostay buffer); 215b3c590ceSMatt Ranostay 216*fef79edcSPeter Meerwald-Stadler MAX30102_COPY_DATA(0); 217*fef79edcSPeter Meerwald-Stadler MAX30102_COPY_DATA(1); 218b3c590ceSMatt Ranostay 219*fef79edcSPeter Meerwald-Stadler return (ret == 2 * MAX30102_REG_FIFO_DATA_BYTES) ? 0 : -EINVAL; 220b3c590ceSMatt Ranostay } 221b3c590ceSMatt Ranostay 222b3c590ceSMatt Ranostay static irqreturn_t max30102_interrupt_handler(int irq, void *private) 223b3c590ceSMatt Ranostay { 224b3c590ceSMatt Ranostay struct iio_dev *indio_dev = private; 225b3c590ceSMatt Ranostay struct max30102_data *data = iio_priv(indio_dev); 226b3c590ceSMatt Ranostay int ret, cnt = 0; 227b3c590ceSMatt Ranostay 228b3c590ceSMatt Ranostay mutex_lock(&data->lock); 229b3c590ceSMatt Ranostay 230b3c590ceSMatt Ranostay while (cnt || (cnt = max30102_fifo_count(data)) > 0) { 231b3c590ceSMatt Ranostay ret = max30102_read_measurement(data); 232b3c590ceSMatt Ranostay if (ret) 233b3c590ceSMatt Ranostay break; 234b3c590ceSMatt Ranostay 235b3c590ceSMatt Ranostay iio_push_to_buffers(data->indio_dev, data->processed_buffer); 236b3c590ceSMatt Ranostay cnt--; 237b3c590ceSMatt Ranostay } 238b3c590ceSMatt Ranostay 239b3c590ceSMatt Ranostay mutex_unlock(&data->lock); 240b3c590ceSMatt Ranostay 241b3c590ceSMatt Ranostay return IRQ_HANDLED; 242b3c590ceSMatt Ranostay } 243b3c590ceSMatt Ranostay 244b3c590ceSMatt Ranostay static int max30102_get_current_idx(unsigned int val, int *reg) 245b3c590ceSMatt Ranostay { 246b3c590ceSMatt Ranostay /* each step is 0.200 mA */ 247b3c590ceSMatt Ranostay *reg = val / 200; 248b3c590ceSMatt Ranostay 249b3c590ceSMatt Ranostay return *reg > 0xff ? -EINVAL : 0; 250b3c590ceSMatt Ranostay } 251b3c590ceSMatt Ranostay 252b3c590ceSMatt Ranostay static int max30102_led_init(struct max30102_data *data) 253b3c590ceSMatt Ranostay { 254b3c590ceSMatt Ranostay struct device *dev = &data->client->dev; 255b3c590ceSMatt Ranostay struct device_node *np = dev->of_node; 256b3c590ceSMatt Ranostay unsigned int val; 257b3c590ceSMatt Ranostay int reg, ret; 258b3c590ceSMatt Ranostay 259b3c590ceSMatt Ranostay ret = of_property_read_u32(np, "maxim,red-led-current-microamp", &val); 260b3c590ceSMatt Ranostay if (ret) { 261b3c590ceSMatt Ranostay dev_info(dev, "no red-led-current-microamp set\n"); 262b3c590ceSMatt Ranostay 263b3c590ceSMatt Ranostay /* Default to 7 mA RED LED */ 264b3c590ceSMatt Ranostay val = 7000; 265b3c590ceSMatt Ranostay } 266b3c590ceSMatt Ranostay 267b3c590ceSMatt Ranostay ret = max30102_get_current_idx(val, ®); 268b3c590ceSMatt Ranostay if (ret) { 269b3c590ceSMatt Ranostay dev_err(dev, "invalid RED LED current setting %d\n", val); 270b3c590ceSMatt Ranostay return ret; 271b3c590ceSMatt Ranostay } 272b3c590ceSMatt Ranostay 273b3c590ceSMatt Ranostay ret = regmap_write(data->regmap, MAX30102_REG_RED_LED_CONFIG, reg); 274b3c590ceSMatt Ranostay if (ret) 275b3c590ceSMatt Ranostay return ret; 276b3c590ceSMatt Ranostay 277b3c590ceSMatt Ranostay ret = of_property_read_u32(np, "maxim,ir-led-current-microamp", &val); 278b3c590ceSMatt Ranostay if (ret) { 279b3c590ceSMatt Ranostay dev_info(dev, "no ir-led-current-microamp set\n"); 280b3c590ceSMatt Ranostay 281b3c590ceSMatt Ranostay /* Default to 7 mA IR LED */ 282b3c590ceSMatt Ranostay val = 7000; 283b3c590ceSMatt Ranostay } 284b3c590ceSMatt Ranostay 285b3c590ceSMatt Ranostay ret = max30102_get_current_idx(val, ®); 286b3c590ceSMatt Ranostay if (ret) { 287dd86dbf9SPeter Meerwald-Stadler dev_err(dev, "invalid IR LED current setting %d\n", val); 288b3c590ceSMatt Ranostay return ret; 289b3c590ceSMatt Ranostay } 290b3c590ceSMatt Ranostay 291b3c590ceSMatt Ranostay return regmap_write(data->regmap, MAX30102_REG_IR_LED_CONFIG, reg); 292b3c590ceSMatt Ranostay } 293b3c590ceSMatt Ranostay 294b3c590ceSMatt Ranostay static int max30102_chip_init(struct max30102_data *data) 295b3c590ceSMatt Ranostay { 296b3c590ceSMatt Ranostay int ret; 297b3c590ceSMatt Ranostay 298b3c590ceSMatt Ranostay /* setup LED current settings */ 299b3c590ceSMatt Ranostay ret = max30102_led_init(data); 300b3c590ceSMatt Ranostay if (ret) 301b3c590ceSMatt Ranostay return ret; 302b3c590ceSMatt Ranostay 30383e6415dSPeter Meerwald-Stadler /* configure 18-bit HR + SpO2 readings at 400Hz */ 304b3c590ceSMatt Ranostay ret = regmap_write(data->regmap, MAX30102_REG_SPO2_CONFIG, 305b3c590ceSMatt Ranostay (MAX30102_REG_SPO2_CONFIG_ADC_4096_STEPS 306b3c590ceSMatt Ranostay << MAX30102_REG_SPO2_CONFIG_ADC_MASK_SHIFT) | 307b3c590ceSMatt Ranostay (MAX30102_REG_SPO2_CONFIG_SR_400HZ 308b3c590ceSMatt Ranostay << MAX30102_REG_SPO2_CONFIG_SR_MASK_SHIFT) | 309b3c590ceSMatt Ranostay MAX30102_REG_SPO2_CONFIG_PULSE_411_US); 310b3c590ceSMatt Ranostay if (ret) 311b3c590ceSMatt Ranostay return ret; 312b3c590ceSMatt Ranostay 313b3c590ceSMatt Ranostay /* average 4 samples + generate FIFO interrupt */ 314b3c590ceSMatt Ranostay ret = regmap_write(data->regmap, MAX30102_REG_FIFO_CONFIG, 315b3c590ceSMatt Ranostay (MAX30102_REG_FIFO_CONFIG_AVG_4SAMPLES 316b3c590ceSMatt Ranostay << MAX30102_REG_FIFO_CONFIG_AVG_SHIFT) | 317b3c590ceSMatt Ranostay MAX30102_REG_FIFO_CONFIG_AFULL); 318b3c590ceSMatt Ranostay if (ret) 319b3c590ceSMatt Ranostay return ret; 320b3c590ceSMatt Ranostay 321b3c590ceSMatt Ranostay /* enable FIFO interrupt */ 322b3c590ceSMatt Ranostay return regmap_update_bits(data->regmap, MAX30102_REG_INT_ENABLE, 323b3c590ceSMatt Ranostay MAX30102_REG_INT_ENABLE_MASK, 324b3c590ceSMatt Ranostay MAX30102_REG_INT_ENABLE_FIFO_EN); 325b3c590ceSMatt Ranostay } 326b3c590ceSMatt Ranostay 327b3c590ceSMatt Ranostay static int max30102_read_temp(struct max30102_data *data, int *val) 328b3c590ceSMatt Ranostay { 329b3c590ceSMatt Ranostay int ret; 330b3c590ceSMatt Ranostay unsigned int reg; 331b3c590ceSMatt Ranostay 332b3c590ceSMatt Ranostay ret = regmap_read(data->regmap, MAX30102_REG_TEMP_INTEGER, ®); 333b3c590ceSMatt Ranostay if (ret < 0) 334b3c590ceSMatt Ranostay return ret; 335b3c590ceSMatt Ranostay *val = reg << 4; 336b3c590ceSMatt Ranostay 337b3c590ceSMatt Ranostay ret = regmap_read(data->regmap, MAX30102_REG_TEMP_FRACTION, ®); 338b3c590ceSMatt Ranostay if (ret < 0) 339b3c590ceSMatt Ranostay return ret; 340b3c590ceSMatt Ranostay 341b3c590ceSMatt Ranostay *val |= reg & 0xf; 342b3c590ceSMatt Ranostay *val = sign_extend32(*val, 11); 343b3c590ceSMatt Ranostay 344b3c590ceSMatt Ranostay return 0; 345b3c590ceSMatt Ranostay } 346b3c590ceSMatt Ranostay 347a9c47abbSPeter Meerwald-Stadler static int max30102_get_temp(struct max30102_data *data, int *val, bool en) 348b3c590ceSMatt Ranostay { 349b3c590ceSMatt Ranostay int ret; 350b3c590ceSMatt Ranostay 351a9c47abbSPeter Meerwald-Stadler if (en) { 35283e6415dSPeter Meerwald-Stadler ret = max30102_set_power(data, true); 353a9c47abbSPeter Meerwald-Stadler if (ret) 354a9c47abbSPeter Meerwald-Stadler return ret; 355a9c47abbSPeter Meerwald-Stadler } 356a9c47abbSPeter Meerwald-Stadler 357b3c590ceSMatt Ranostay /* start acquisition */ 358b3c590ceSMatt Ranostay ret = regmap_update_bits(data->regmap, MAX30102_REG_TEMP_CONFIG, 359b3c590ceSMatt Ranostay MAX30102_REG_TEMP_CONFIG_TEMP_EN, 360b3c590ceSMatt Ranostay MAX30102_REG_TEMP_CONFIG_TEMP_EN); 361b3c590ceSMatt Ranostay if (ret) 362a9c47abbSPeter Meerwald-Stadler goto out; 363b3c590ceSMatt Ranostay 364b3c590ceSMatt Ranostay msleep(35); 365a9c47abbSPeter Meerwald-Stadler ret = max30102_read_temp(data, val); 366b3c590ceSMatt Ranostay 367a9c47abbSPeter Meerwald-Stadler out: 368a9c47abbSPeter Meerwald-Stadler if (en) 36983e6415dSPeter Meerwald-Stadler max30102_set_power(data, false); 370a9c47abbSPeter Meerwald-Stadler 371a9c47abbSPeter Meerwald-Stadler return ret; 372b3c590ceSMatt Ranostay } 373b3c590ceSMatt Ranostay 374b3c590ceSMatt Ranostay static int max30102_read_raw(struct iio_dev *indio_dev, 375b3c590ceSMatt Ranostay struct iio_chan_spec const *chan, 376b3c590ceSMatt Ranostay int *val, int *val2, long mask) 377b3c590ceSMatt Ranostay { 378b3c590ceSMatt Ranostay struct max30102_data *data = iio_priv(indio_dev); 379b3c590ceSMatt Ranostay int ret = -EINVAL; 380b3c590ceSMatt Ranostay 381b3c590ceSMatt Ranostay switch (mask) { 382b3c590ceSMatt Ranostay case IIO_CHAN_INFO_RAW: 383b3c590ceSMatt Ranostay /* 384a9c47abbSPeter Meerwald-Stadler * Temperature reading can only be acquired when not in 385a9c47abbSPeter Meerwald-Stadler * shutdown; leave shutdown briefly when buffer not running 386b3c590ceSMatt Ranostay */ 387b3c590ceSMatt Ranostay mutex_lock(&indio_dev->mlock); 388b3c590ceSMatt Ranostay if (!iio_buffer_enabled(indio_dev)) 389a9c47abbSPeter Meerwald-Stadler ret = max30102_get_temp(data, val, true); 390a9c47abbSPeter Meerwald-Stadler else 391a9c47abbSPeter Meerwald-Stadler ret = max30102_get_temp(data, val, false); 392b3c590ceSMatt Ranostay mutex_unlock(&indio_dev->mlock); 393a9c47abbSPeter Meerwald-Stadler if (ret) 394a9c47abbSPeter Meerwald-Stadler return ret; 395a9c47abbSPeter Meerwald-Stadler 396a9c47abbSPeter Meerwald-Stadler ret = IIO_VAL_INT; 397b3c590ceSMatt Ranostay break; 398b3c590ceSMatt Ranostay case IIO_CHAN_INFO_SCALE: 399b3c590ceSMatt Ranostay *val = 1; /* 0.0625 */ 400b3c590ceSMatt Ranostay *val2 = 16; 401b3c590ceSMatt Ranostay ret = IIO_VAL_FRACTIONAL; 402b3c590ceSMatt Ranostay break; 403b3c590ceSMatt Ranostay } 404b3c590ceSMatt Ranostay 405b3c590ceSMatt Ranostay return ret; 406b3c590ceSMatt Ranostay } 407b3c590ceSMatt Ranostay 408b3c590ceSMatt Ranostay static const struct iio_info max30102_info = { 409b3c590ceSMatt Ranostay .read_raw = max30102_read_raw, 410b3c590ceSMatt Ranostay }; 411b3c590ceSMatt Ranostay 412b3c590ceSMatt Ranostay static int max30102_probe(struct i2c_client *client, 413b3c590ceSMatt Ranostay const struct i2c_device_id *id) 414b3c590ceSMatt Ranostay { 415b3c590ceSMatt Ranostay struct max30102_data *data; 416b3c590ceSMatt Ranostay struct iio_buffer *buffer; 417b3c590ceSMatt Ranostay struct iio_dev *indio_dev; 418b3c590ceSMatt Ranostay int ret; 41984b0ce05SPeter Meerwald-Stadler unsigned int reg; 420b3c590ceSMatt Ranostay 421b3c590ceSMatt Ranostay indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 422b3c590ceSMatt Ranostay if (!indio_dev) 423b3c590ceSMatt Ranostay return -ENOMEM; 424b3c590ceSMatt Ranostay 425b3c590ceSMatt Ranostay buffer = devm_iio_kfifo_allocate(&client->dev); 426b3c590ceSMatt Ranostay if (!buffer) 427b3c590ceSMatt Ranostay return -ENOMEM; 428b3c590ceSMatt Ranostay 429b3c590ceSMatt Ranostay iio_device_attach_buffer(indio_dev, buffer); 430b3c590ceSMatt Ranostay 431b3c590ceSMatt Ranostay indio_dev->name = MAX30102_DRV_NAME; 432b3c590ceSMatt Ranostay indio_dev->channels = max30102_channels; 433b3c590ceSMatt Ranostay indio_dev->info = &max30102_info; 434b3c590ceSMatt Ranostay indio_dev->num_channels = ARRAY_SIZE(max30102_channels); 435b3c590ceSMatt Ranostay indio_dev->available_scan_masks = max30102_scan_masks; 436b3c590ceSMatt Ranostay indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE); 437b3c590ceSMatt Ranostay indio_dev->setup_ops = &max30102_buffer_setup_ops; 438fa722499SLars-Peter Clausen indio_dev->dev.parent = &client->dev; 439b3c590ceSMatt Ranostay 440b3c590ceSMatt Ranostay data = iio_priv(indio_dev); 441b3c590ceSMatt Ranostay data->indio_dev = indio_dev; 442b3c590ceSMatt Ranostay data->client = client; 443b3c590ceSMatt Ranostay 444b3c590ceSMatt Ranostay mutex_init(&data->lock); 445b3c590ceSMatt Ranostay i2c_set_clientdata(client, indio_dev); 446b3c590ceSMatt Ranostay 447b3c590ceSMatt Ranostay data->regmap = devm_regmap_init_i2c(client, &max30102_regmap_config); 448b3c590ceSMatt Ranostay if (IS_ERR(data->regmap)) { 449c31c946aSPeter Meerwald-Stadler dev_err(&client->dev, "regmap initialization failed\n"); 450b3c590ceSMatt Ranostay return PTR_ERR(data->regmap); 451b3c590ceSMatt Ranostay } 452d0b950c7SPeter Meerwald-Stadler 45384b0ce05SPeter Meerwald-Stadler /* check part ID */ 45484b0ce05SPeter Meerwald-Stadler ret = regmap_read(data->regmap, MAX30102_REG_PART_ID, ®); 45584b0ce05SPeter Meerwald-Stadler if (ret) 45684b0ce05SPeter Meerwald-Stadler return ret; 45784b0ce05SPeter Meerwald-Stadler if (reg != MAX30102_PART_NUMBER) 45884b0ce05SPeter Meerwald-Stadler return -ENODEV; 45984b0ce05SPeter Meerwald-Stadler 46084b0ce05SPeter Meerwald-Stadler /* show revision ID */ 46184b0ce05SPeter Meerwald-Stadler ret = regmap_read(data->regmap, MAX30102_REG_REV_ID, ®); 46284b0ce05SPeter Meerwald-Stadler if (ret) 46384b0ce05SPeter Meerwald-Stadler return ret; 46484b0ce05SPeter Meerwald-Stadler dev_dbg(&client->dev, "max3010x revision %02x\n", reg); 46584b0ce05SPeter Meerwald-Stadler 46683e6415dSPeter Meerwald-Stadler /* clear mode setting, chip shutdown */ 46783e6415dSPeter Meerwald-Stadler ret = max30102_set_powermode(data, MAX30102_REG_MODE_CONFIG_MODE_NONE, 46883e6415dSPeter Meerwald-Stadler false); 469d0b950c7SPeter Meerwald-Stadler if (ret) 470d0b950c7SPeter Meerwald-Stadler return ret; 471b3c590ceSMatt Ranostay 472b3c590ceSMatt Ranostay ret = max30102_chip_init(data); 473b3c590ceSMatt Ranostay if (ret) 474b3c590ceSMatt Ranostay return ret; 475b3c590ceSMatt Ranostay 476b3c590ceSMatt Ranostay if (client->irq <= 0) { 477b3c590ceSMatt Ranostay dev_err(&client->dev, "no valid irq defined\n"); 478b3c590ceSMatt Ranostay return -EINVAL; 479b3c590ceSMatt Ranostay } 480b3c590ceSMatt Ranostay 481b3c590ceSMatt Ranostay ret = devm_request_threaded_irq(&client->dev, client->irq, 482b3c590ceSMatt Ranostay NULL, max30102_interrupt_handler, 483b3c590ceSMatt Ranostay IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 484b3c590ceSMatt Ranostay "max30102_irq", indio_dev); 485b3c590ceSMatt Ranostay if (ret) { 486b3c590ceSMatt Ranostay dev_err(&client->dev, "request irq (%d) failed\n", client->irq); 487b3c590ceSMatt Ranostay return ret; 488b3c590ceSMatt Ranostay } 489b3c590ceSMatt Ranostay 490b3c590ceSMatt Ranostay return iio_device_register(indio_dev); 491b3c590ceSMatt Ranostay } 492b3c590ceSMatt Ranostay 493b3c590ceSMatt Ranostay static int max30102_remove(struct i2c_client *client) 494b3c590ceSMatt Ranostay { 495b3c590ceSMatt Ranostay struct iio_dev *indio_dev = i2c_get_clientdata(client); 496b3c590ceSMatt Ranostay struct max30102_data *data = iio_priv(indio_dev); 497b3c590ceSMatt Ranostay 498b3c590ceSMatt Ranostay iio_device_unregister(indio_dev); 49983e6415dSPeter Meerwald-Stadler max30102_set_power(data, false); 500b3c590ceSMatt Ranostay 501b3c590ceSMatt Ranostay return 0; 502b3c590ceSMatt Ranostay } 503b3c590ceSMatt Ranostay 504b3c590ceSMatt Ranostay static const struct i2c_device_id max30102_id[] = { 505b3c590ceSMatt Ranostay { "max30102", 0 }, 506b3c590ceSMatt Ranostay {} 507b3c590ceSMatt Ranostay }; 508b3c590ceSMatt Ranostay MODULE_DEVICE_TABLE(i2c, max30102_id); 509b3c590ceSMatt Ranostay 510b3c590ceSMatt Ranostay static const struct of_device_id max30102_dt_ids[] = { 511b3c590ceSMatt Ranostay { .compatible = "maxim,max30102" }, 512b3c590ceSMatt Ranostay { } 513b3c590ceSMatt Ranostay }; 514b3c590ceSMatt Ranostay MODULE_DEVICE_TABLE(of, max30102_dt_ids); 515b3c590ceSMatt Ranostay 516b3c590ceSMatt Ranostay static struct i2c_driver max30102_driver = { 517b3c590ceSMatt Ranostay .driver = { 518b3c590ceSMatt Ranostay .name = MAX30102_DRV_NAME, 519b3c590ceSMatt Ranostay .of_match_table = of_match_ptr(max30102_dt_ids), 520b3c590ceSMatt Ranostay }, 521b3c590ceSMatt Ranostay .probe = max30102_probe, 522b3c590ceSMatt Ranostay .remove = max30102_remove, 523b3c590ceSMatt Ranostay .id_table = max30102_id, 524b3c590ceSMatt Ranostay }; 525b3c590ceSMatt Ranostay module_i2c_driver(max30102_driver); 526b3c590ceSMatt Ranostay 527b3c590ceSMatt Ranostay MODULE_AUTHOR("Matt Ranostay <matt@ranostay.consulting>"); 528b3c590ceSMatt Ranostay MODULE_DESCRIPTION("MAX30102 heart rate and pulse oximeter sensor"); 529b3c590ceSMatt Ranostay MODULE_LICENSE("GPL"); 530