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 37b3c590ceSMatt Ranostay #define MAX30102_REG_INT_STATUS 0x00 38b3c590ceSMatt Ranostay #define MAX30102_REG_INT_STATUS_PWR_RDY BIT(0) 39b3c590ceSMatt Ranostay #define MAX30102_REG_INT_STATUS_PROX_INT BIT(4) 40b3c590ceSMatt Ranostay #define MAX30102_REG_INT_STATUS_ALC_OVF BIT(5) 41b3c590ceSMatt Ranostay #define MAX30102_REG_INT_STATUS_PPG_RDY BIT(6) 42b3c590ceSMatt Ranostay #define MAX30102_REG_INT_STATUS_FIFO_RDY BIT(7) 43b3c590ceSMatt Ranostay 44b3c590ceSMatt Ranostay #define MAX30102_REG_INT_ENABLE 0x02 45b3c590ceSMatt Ranostay #define MAX30102_REG_INT_ENABLE_PROX_INT_EN BIT(4) 46b3c590ceSMatt Ranostay #define MAX30102_REG_INT_ENABLE_ALC_OVF_EN BIT(5) 47b3c590ceSMatt Ranostay #define MAX30102_REG_INT_ENABLE_PPG_EN BIT(6) 48b3c590ceSMatt Ranostay #define MAX30102_REG_INT_ENABLE_FIFO_EN BIT(7) 49b3c590ceSMatt Ranostay #define MAX30102_REG_INT_ENABLE_MASK 0xf0 50b3c590ceSMatt Ranostay #define MAX30102_REG_INT_ENABLE_MASK_SHIFT 4 51b3c590ceSMatt Ranostay 52b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_WR_PTR 0x04 53b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_OVR_CTR 0x05 54b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_RD_PTR 0x06 55b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_DATA 0x07 56b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_DATA_ENTRY_LEN 6 57b3c590ceSMatt Ranostay 58b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_CONFIG 0x08 59b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_CONFIG_AVG_4SAMPLES BIT(1) 60b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_CONFIG_AVG_SHIFT 5 61b3c590ceSMatt Ranostay #define MAX30102_REG_FIFO_CONFIG_AFULL BIT(0) 62b3c590ceSMatt Ranostay 63b3c590ceSMatt Ranostay #define MAX30102_REG_MODE_CONFIG 0x09 647b0b0ec1SPeter Meerwald-Stadler #define MAX30102_REG_MODE_CONFIG_MODE_HR 0x02 /* red LED */ 657b0b0ec1SPeter Meerwald-Stadler #define MAX30102_REG_MODE_CONFIG_MODE_HR_SPO2 0x03 /* red + IR LED */ 667b0b0ec1SPeter Meerwald-Stadler #define MAX30102_REG_MODE_CONFIG_MODE_MULTI 0x07 /* multi-LED mode */ 677b0b0ec1SPeter Meerwald-Stadler #define MAX30102_REG_MODE_CONFIG_MODE_MASK GENMASK(2, 0) 68b3c590ceSMatt Ranostay #define MAX30102_REG_MODE_CONFIG_PWR BIT(7) 69b3c590ceSMatt Ranostay 70b3c590ceSMatt Ranostay #define MAX30102_REG_SPO2_CONFIG 0x0a 71b3c590ceSMatt Ranostay #define MAX30102_REG_SPO2_CONFIG_PULSE_411_US 0x03 72b3c590ceSMatt Ranostay #define MAX30102_REG_SPO2_CONFIG_SR_400HZ 0x03 73b3c590ceSMatt Ranostay #define MAX30102_REG_SPO2_CONFIG_SR_MASK 0x07 74b3c590ceSMatt Ranostay #define MAX30102_REG_SPO2_CONFIG_SR_MASK_SHIFT 2 75b3c590ceSMatt Ranostay #define MAX30102_REG_SPO2_CONFIG_ADC_4096_STEPS BIT(0) 76b3c590ceSMatt Ranostay #define MAX30102_REG_SPO2_CONFIG_ADC_MASK_SHIFT 5 77b3c590ceSMatt Ranostay 78b3c590ceSMatt Ranostay #define MAX30102_REG_RED_LED_CONFIG 0x0c 79b3c590ceSMatt Ranostay #define MAX30102_REG_IR_LED_CONFIG 0x0d 80b3c590ceSMatt Ranostay 81b3c590ceSMatt Ranostay #define MAX30102_REG_TEMP_CONFIG 0x21 82b3c590ceSMatt Ranostay #define MAX30102_REG_TEMP_CONFIG_TEMP_EN BIT(0) 83b3c590ceSMatt Ranostay 84b3c590ceSMatt Ranostay #define MAX30102_REG_TEMP_INTEGER 0x1f 85b3c590ceSMatt Ranostay #define MAX30102_REG_TEMP_FRACTION 0x20 86b3c590ceSMatt Ranostay 8784b0ce05SPeter Meerwald-Stadler #define MAX30102_REG_REV_ID 0xfe 8884b0ce05SPeter Meerwald-Stadler #define MAX30102_REG_PART_ID 0xff 8984b0ce05SPeter Meerwald-Stadler 90b3c590ceSMatt Ranostay struct max30102_data { 91b3c590ceSMatt Ranostay struct i2c_client *client; 92b3c590ceSMatt Ranostay struct iio_dev *indio_dev; 93b3c590ceSMatt Ranostay struct mutex lock; 94b3c590ceSMatt Ranostay struct regmap *regmap; 95b3c590ceSMatt Ranostay 96b3c590ceSMatt Ranostay u8 buffer[8]; 97b3c590ceSMatt Ranostay __be32 processed_buffer[2]; /* 2 x 18-bit (padded to 32-bits) */ 98b3c590ceSMatt Ranostay }; 99b3c590ceSMatt Ranostay 100b3c590ceSMatt Ranostay static const struct regmap_config max30102_regmap_config = { 101b3c590ceSMatt Ranostay .name = MAX30102_REGMAP_NAME, 102b3c590ceSMatt Ranostay 103b3c590ceSMatt Ranostay .reg_bits = 8, 104b3c590ceSMatt Ranostay .val_bits = 8, 105b3c590ceSMatt Ranostay }; 106b3c590ceSMatt Ranostay 107b3c590ceSMatt Ranostay static const unsigned long max30102_scan_masks[] = {0x3, 0}; 108b3c590ceSMatt Ranostay 109839a74cdSPeter Meerwald-Stadler #define MAX30102_INTENSITY_CHANNEL(_si, _mod) { \ 110839a74cdSPeter Meerwald-Stadler .type = IIO_INTENSITY, \ 111839a74cdSPeter Meerwald-Stadler .channel2 = _mod, \ 112839a74cdSPeter Meerwald-Stadler .modified = 1, \ 113839a74cdSPeter Meerwald-Stadler .scan_index = _si, \ 114839a74cdSPeter Meerwald-Stadler .scan_type = { \ 115839a74cdSPeter Meerwald-Stadler .sign = 'u', \ 116839a74cdSPeter Meerwald-Stadler .shift = 8, \ 117839a74cdSPeter Meerwald-Stadler .realbits = 18, \ 118839a74cdSPeter Meerwald-Stadler .storagebits = 32, \ 119839a74cdSPeter Meerwald-Stadler .endianness = IIO_BE, \ 120839a74cdSPeter Meerwald-Stadler }, \ 121839a74cdSPeter Meerwald-Stadler } 122839a74cdSPeter Meerwald-Stadler 123b3c590ceSMatt Ranostay static const struct iio_chan_spec max30102_channels[] = { 124839a74cdSPeter Meerwald-Stadler MAX30102_INTENSITY_CHANNEL(0, IIO_MOD_LIGHT_RED), 125839a74cdSPeter Meerwald-Stadler MAX30102_INTENSITY_CHANNEL(1, IIO_MOD_LIGHT_IR), 126b3c590ceSMatt Ranostay { 127b3c590ceSMatt Ranostay .type = IIO_TEMP, 128b3c590ceSMatt Ranostay .info_mask_separate = 129b3c590ceSMatt Ranostay BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 130b3c590ceSMatt Ranostay .scan_index = -1, 131b3c590ceSMatt Ranostay }, 132b3c590ceSMatt Ranostay }; 133b3c590ceSMatt Ranostay 134b3c590ceSMatt Ranostay static int max30102_set_powermode(struct max30102_data *data, bool state) 135b3c590ceSMatt Ranostay { 136b3c590ceSMatt Ranostay return regmap_update_bits(data->regmap, MAX30102_REG_MODE_CONFIG, 137b3c590ceSMatt Ranostay MAX30102_REG_MODE_CONFIG_PWR, 138b3c590ceSMatt Ranostay state ? 0 : MAX30102_REG_MODE_CONFIG_PWR); 139b3c590ceSMatt Ranostay } 140b3c590ceSMatt Ranostay 141b3c590ceSMatt Ranostay static int max30102_buffer_postenable(struct iio_dev *indio_dev) 142b3c590ceSMatt Ranostay { 143b3c590ceSMatt Ranostay struct max30102_data *data = iio_priv(indio_dev); 144b3c590ceSMatt Ranostay 145b3c590ceSMatt Ranostay return max30102_set_powermode(data, true); 146b3c590ceSMatt Ranostay } 147b3c590ceSMatt Ranostay 148b3c590ceSMatt Ranostay static int max30102_buffer_predisable(struct iio_dev *indio_dev) 149b3c590ceSMatt Ranostay { 150b3c590ceSMatt Ranostay struct max30102_data *data = iio_priv(indio_dev); 151b3c590ceSMatt Ranostay 152b3c590ceSMatt Ranostay return max30102_set_powermode(data, false); 153b3c590ceSMatt Ranostay } 154b3c590ceSMatt Ranostay 155b3c590ceSMatt Ranostay static const struct iio_buffer_setup_ops max30102_buffer_setup_ops = { 156b3c590ceSMatt Ranostay .postenable = max30102_buffer_postenable, 157b3c590ceSMatt Ranostay .predisable = max30102_buffer_predisable, 158b3c590ceSMatt Ranostay }; 159b3c590ceSMatt Ranostay 160b3c590ceSMatt Ranostay static inline int max30102_fifo_count(struct max30102_data *data) 161b3c590ceSMatt Ranostay { 162b3c590ceSMatt Ranostay unsigned int val; 163b3c590ceSMatt Ranostay int ret; 164b3c590ceSMatt Ranostay 165b3c590ceSMatt Ranostay ret = regmap_read(data->regmap, MAX30102_REG_INT_STATUS, &val); 166b3c590ceSMatt Ranostay if (ret) 167b3c590ceSMatt Ranostay return ret; 168b3c590ceSMatt Ranostay 169b3c590ceSMatt Ranostay /* FIFO has one sample slot left */ 170b3c590ceSMatt Ranostay if (val & MAX30102_REG_INT_STATUS_FIFO_RDY) 171b3c590ceSMatt Ranostay return 1; 172b3c590ceSMatt Ranostay 173b3c590ceSMatt Ranostay return 0; 174b3c590ceSMatt Ranostay } 175b3c590ceSMatt Ranostay 176b3c590ceSMatt Ranostay static int max30102_read_measurement(struct max30102_data *data) 177b3c590ceSMatt Ranostay { 178b3c590ceSMatt Ranostay int ret; 179b3c590ceSMatt Ranostay u8 *buffer = (u8 *) &data->buffer; 180b3c590ceSMatt Ranostay 181b3c590ceSMatt Ranostay ret = i2c_smbus_read_i2c_block_data(data->client, 182b3c590ceSMatt Ranostay MAX30102_REG_FIFO_DATA, 183b3c590ceSMatt Ranostay MAX30102_REG_FIFO_DATA_ENTRY_LEN, 184b3c590ceSMatt Ranostay buffer); 185b3c590ceSMatt Ranostay 186b3c590ceSMatt Ranostay memcpy(&data->processed_buffer[0], &buffer[0], 3); 187b3c590ceSMatt Ranostay memcpy(&data->processed_buffer[1], &buffer[3], 3); 188b3c590ceSMatt Ranostay 189b3c590ceSMatt Ranostay return (ret == MAX30102_REG_FIFO_DATA_ENTRY_LEN) ? 0 : -EINVAL; 190b3c590ceSMatt Ranostay } 191b3c590ceSMatt Ranostay 192b3c590ceSMatt Ranostay static irqreturn_t max30102_interrupt_handler(int irq, void *private) 193b3c590ceSMatt Ranostay { 194b3c590ceSMatt Ranostay struct iio_dev *indio_dev = private; 195b3c590ceSMatt Ranostay struct max30102_data *data = iio_priv(indio_dev); 196b3c590ceSMatt Ranostay int ret, cnt = 0; 197b3c590ceSMatt Ranostay 198b3c590ceSMatt Ranostay mutex_lock(&data->lock); 199b3c590ceSMatt Ranostay 200b3c590ceSMatt Ranostay while (cnt || (cnt = max30102_fifo_count(data)) > 0) { 201b3c590ceSMatt Ranostay ret = max30102_read_measurement(data); 202b3c590ceSMatt Ranostay if (ret) 203b3c590ceSMatt Ranostay break; 204b3c590ceSMatt Ranostay 205b3c590ceSMatt Ranostay iio_push_to_buffers(data->indio_dev, data->processed_buffer); 206b3c590ceSMatt Ranostay cnt--; 207b3c590ceSMatt Ranostay } 208b3c590ceSMatt Ranostay 209b3c590ceSMatt Ranostay mutex_unlock(&data->lock); 210b3c590ceSMatt Ranostay 211b3c590ceSMatt Ranostay return IRQ_HANDLED; 212b3c590ceSMatt Ranostay } 213b3c590ceSMatt Ranostay 214b3c590ceSMatt Ranostay static int max30102_get_current_idx(unsigned int val, int *reg) 215b3c590ceSMatt Ranostay { 216b3c590ceSMatt Ranostay /* each step is 0.200 mA */ 217b3c590ceSMatt Ranostay *reg = val / 200; 218b3c590ceSMatt Ranostay 219b3c590ceSMatt Ranostay return *reg > 0xff ? -EINVAL : 0; 220b3c590ceSMatt Ranostay } 221b3c590ceSMatt Ranostay 222b3c590ceSMatt Ranostay static int max30102_led_init(struct max30102_data *data) 223b3c590ceSMatt Ranostay { 224b3c590ceSMatt Ranostay struct device *dev = &data->client->dev; 225b3c590ceSMatt Ranostay struct device_node *np = dev->of_node; 226b3c590ceSMatt Ranostay unsigned int val; 227b3c590ceSMatt Ranostay int reg, ret; 228b3c590ceSMatt Ranostay 229b3c590ceSMatt Ranostay ret = of_property_read_u32(np, "maxim,red-led-current-microamp", &val); 230b3c590ceSMatt Ranostay if (ret) { 231b3c590ceSMatt Ranostay dev_info(dev, "no red-led-current-microamp set\n"); 232b3c590ceSMatt Ranostay 233b3c590ceSMatt Ranostay /* Default to 7 mA RED LED */ 234b3c590ceSMatt Ranostay val = 7000; 235b3c590ceSMatt Ranostay } 236b3c590ceSMatt Ranostay 237b3c590ceSMatt Ranostay ret = max30102_get_current_idx(val, ®); 238b3c590ceSMatt Ranostay if (ret) { 239b3c590ceSMatt Ranostay dev_err(dev, "invalid RED LED current setting %d\n", val); 240b3c590ceSMatt Ranostay return ret; 241b3c590ceSMatt Ranostay } 242b3c590ceSMatt Ranostay 243b3c590ceSMatt Ranostay ret = regmap_write(data->regmap, MAX30102_REG_RED_LED_CONFIG, reg); 244b3c590ceSMatt Ranostay if (ret) 245b3c590ceSMatt Ranostay return ret; 246b3c590ceSMatt Ranostay 247b3c590ceSMatt Ranostay ret = of_property_read_u32(np, "maxim,ir-led-current-microamp", &val); 248b3c590ceSMatt Ranostay if (ret) { 249b3c590ceSMatt Ranostay dev_info(dev, "no ir-led-current-microamp set\n"); 250b3c590ceSMatt Ranostay 251b3c590ceSMatt Ranostay /* Default to 7 mA IR LED */ 252b3c590ceSMatt Ranostay val = 7000; 253b3c590ceSMatt Ranostay } 254b3c590ceSMatt Ranostay 255b3c590ceSMatt Ranostay ret = max30102_get_current_idx(val, ®); 256b3c590ceSMatt Ranostay if (ret) { 257dd86dbf9SPeter Meerwald-Stadler dev_err(dev, "invalid IR LED current setting %d\n", val); 258b3c590ceSMatt Ranostay return ret; 259b3c590ceSMatt Ranostay } 260b3c590ceSMatt Ranostay 261b3c590ceSMatt Ranostay return regmap_write(data->regmap, MAX30102_REG_IR_LED_CONFIG, reg); 262b3c590ceSMatt Ranostay } 263b3c590ceSMatt Ranostay 264b3c590ceSMatt Ranostay static int max30102_chip_init(struct max30102_data *data) 265b3c590ceSMatt Ranostay { 266b3c590ceSMatt Ranostay int ret; 267b3c590ceSMatt Ranostay 268b3c590ceSMatt Ranostay /* setup LED current settings */ 269b3c590ceSMatt Ranostay ret = max30102_led_init(data); 270b3c590ceSMatt Ranostay if (ret) 271b3c590ceSMatt Ranostay return ret; 272b3c590ceSMatt Ranostay 273b3c590ceSMatt Ranostay /* enable 18-bit HR + SPO2 readings at 400Hz */ 274b3c590ceSMatt Ranostay ret = regmap_write(data->regmap, MAX30102_REG_SPO2_CONFIG, 275b3c590ceSMatt Ranostay (MAX30102_REG_SPO2_CONFIG_ADC_4096_STEPS 276b3c590ceSMatt Ranostay << MAX30102_REG_SPO2_CONFIG_ADC_MASK_SHIFT) | 277b3c590ceSMatt Ranostay (MAX30102_REG_SPO2_CONFIG_SR_400HZ 278b3c590ceSMatt Ranostay << MAX30102_REG_SPO2_CONFIG_SR_MASK_SHIFT) | 279b3c590ceSMatt Ranostay MAX30102_REG_SPO2_CONFIG_PULSE_411_US); 280b3c590ceSMatt Ranostay if (ret) 281b3c590ceSMatt Ranostay return ret; 282b3c590ceSMatt Ranostay 2837b0b0ec1SPeter Meerwald-Stadler /* enable HR + SPO2 mode */ 284b3c590ceSMatt Ranostay ret = regmap_update_bits(data->regmap, MAX30102_REG_MODE_CONFIG, 285b3c590ceSMatt Ranostay MAX30102_REG_MODE_CONFIG_MODE_MASK, 2867b0b0ec1SPeter Meerwald-Stadler MAX30102_REG_MODE_CONFIG_MODE_HR_SPO2); 287b3c590ceSMatt Ranostay if (ret) 288b3c590ceSMatt Ranostay return ret; 289b3c590ceSMatt Ranostay 290b3c590ceSMatt Ranostay /* average 4 samples + generate FIFO interrupt */ 291b3c590ceSMatt Ranostay ret = regmap_write(data->regmap, MAX30102_REG_FIFO_CONFIG, 292b3c590ceSMatt Ranostay (MAX30102_REG_FIFO_CONFIG_AVG_4SAMPLES 293b3c590ceSMatt Ranostay << MAX30102_REG_FIFO_CONFIG_AVG_SHIFT) | 294b3c590ceSMatt Ranostay MAX30102_REG_FIFO_CONFIG_AFULL); 295b3c590ceSMatt Ranostay if (ret) 296b3c590ceSMatt Ranostay return ret; 297b3c590ceSMatt Ranostay 298b3c590ceSMatt Ranostay /* enable FIFO interrupt */ 299b3c590ceSMatt Ranostay return regmap_update_bits(data->regmap, MAX30102_REG_INT_ENABLE, 300b3c590ceSMatt Ranostay MAX30102_REG_INT_ENABLE_MASK, 301b3c590ceSMatt Ranostay MAX30102_REG_INT_ENABLE_FIFO_EN); 302b3c590ceSMatt Ranostay } 303b3c590ceSMatt Ranostay 304b3c590ceSMatt Ranostay static int max30102_read_temp(struct max30102_data *data, int *val) 305b3c590ceSMatt Ranostay { 306b3c590ceSMatt Ranostay int ret; 307b3c590ceSMatt Ranostay unsigned int reg; 308b3c590ceSMatt Ranostay 309b3c590ceSMatt Ranostay ret = regmap_read(data->regmap, MAX30102_REG_TEMP_INTEGER, ®); 310b3c590ceSMatt Ranostay if (ret < 0) 311b3c590ceSMatt Ranostay return ret; 312b3c590ceSMatt Ranostay *val = reg << 4; 313b3c590ceSMatt Ranostay 314b3c590ceSMatt Ranostay ret = regmap_read(data->regmap, MAX30102_REG_TEMP_FRACTION, ®); 315b3c590ceSMatt Ranostay if (ret < 0) 316b3c590ceSMatt Ranostay return ret; 317b3c590ceSMatt Ranostay 318b3c590ceSMatt Ranostay *val |= reg & 0xf; 319b3c590ceSMatt Ranostay *val = sign_extend32(*val, 11); 320b3c590ceSMatt Ranostay 321b3c590ceSMatt Ranostay return 0; 322b3c590ceSMatt Ranostay } 323b3c590ceSMatt Ranostay 324*a9c47abbSPeter Meerwald-Stadler static int max30102_get_temp(struct max30102_data *data, int *val, bool en) 325b3c590ceSMatt Ranostay { 326b3c590ceSMatt Ranostay int ret; 327b3c590ceSMatt Ranostay 328*a9c47abbSPeter Meerwald-Stadler if (en) { 329*a9c47abbSPeter Meerwald-Stadler ret = max30102_set_powermode(data, true); 330*a9c47abbSPeter Meerwald-Stadler if (ret) 331*a9c47abbSPeter Meerwald-Stadler return ret; 332*a9c47abbSPeter Meerwald-Stadler } 333*a9c47abbSPeter Meerwald-Stadler 334b3c590ceSMatt Ranostay /* start acquisition */ 335b3c590ceSMatt Ranostay ret = regmap_update_bits(data->regmap, MAX30102_REG_TEMP_CONFIG, 336b3c590ceSMatt Ranostay MAX30102_REG_TEMP_CONFIG_TEMP_EN, 337b3c590ceSMatt Ranostay MAX30102_REG_TEMP_CONFIG_TEMP_EN); 338b3c590ceSMatt Ranostay if (ret) 339*a9c47abbSPeter Meerwald-Stadler goto out; 340b3c590ceSMatt Ranostay 341b3c590ceSMatt Ranostay msleep(35); 342*a9c47abbSPeter Meerwald-Stadler ret = max30102_read_temp(data, val); 343b3c590ceSMatt Ranostay 344*a9c47abbSPeter Meerwald-Stadler out: 345*a9c47abbSPeter Meerwald-Stadler if (en) 346*a9c47abbSPeter Meerwald-Stadler max30102_set_powermode(data, false); 347*a9c47abbSPeter Meerwald-Stadler 348*a9c47abbSPeter Meerwald-Stadler return ret; 349b3c590ceSMatt Ranostay } 350b3c590ceSMatt Ranostay 351b3c590ceSMatt Ranostay static int max30102_read_raw(struct iio_dev *indio_dev, 352b3c590ceSMatt Ranostay struct iio_chan_spec const *chan, 353b3c590ceSMatt Ranostay int *val, int *val2, long mask) 354b3c590ceSMatt Ranostay { 355b3c590ceSMatt Ranostay struct max30102_data *data = iio_priv(indio_dev); 356b3c590ceSMatt Ranostay int ret = -EINVAL; 357b3c590ceSMatt Ranostay 358b3c590ceSMatt Ranostay switch (mask) { 359b3c590ceSMatt Ranostay case IIO_CHAN_INFO_RAW: 360b3c590ceSMatt Ranostay /* 361*a9c47abbSPeter Meerwald-Stadler * Temperature reading can only be acquired when not in 362*a9c47abbSPeter Meerwald-Stadler * shutdown; leave shutdown briefly when buffer not running 363b3c590ceSMatt Ranostay */ 364b3c590ceSMatt Ranostay mutex_lock(&indio_dev->mlock); 365b3c590ceSMatt Ranostay if (!iio_buffer_enabled(indio_dev)) 366*a9c47abbSPeter Meerwald-Stadler ret = max30102_get_temp(data, val, true); 367*a9c47abbSPeter Meerwald-Stadler else 368*a9c47abbSPeter Meerwald-Stadler ret = max30102_get_temp(data, val, false); 369b3c590ceSMatt Ranostay mutex_unlock(&indio_dev->mlock); 370*a9c47abbSPeter Meerwald-Stadler if (ret) 371*a9c47abbSPeter Meerwald-Stadler return ret; 372*a9c47abbSPeter Meerwald-Stadler 373*a9c47abbSPeter Meerwald-Stadler ret = IIO_VAL_INT; 374b3c590ceSMatt Ranostay break; 375b3c590ceSMatt Ranostay case IIO_CHAN_INFO_SCALE: 376b3c590ceSMatt Ranostay *val = 1; /* 0.0625 */ 377b3c590ceSMatt Ranostay *val2 = 16; 378b3c590ceSMatt Ranostay ret = IIO_VAL_FRACTIONAL; 379b3c590ceSMatt Ranostay break; 380b3c590ceSMatt Ranostay } 381b3c590ceSMatt Ranostay 382b3c590ceSMatt Ranostay return ret; 383b3c590ceSMatt Ranostay } 384b3c590ceSMatt Ranostay 385b3c590ceSMatt Ranostay static const struct iio_info max30102_info = { 386b3c590ceSMatt Ranostay .read_raw = max30102_read_raw, 387b3c590ceSMatt Ranostay }; 388b3c590ceSMatt Ranostay 389b3c590ceSMatt Ranostay static int max30102_probe(struct i2c_client *client, 390b3c590ceSMatt Ranostay const struct i2c_device_id *id) 391b3c590ceSMatt Ranostay { 392b3c590ceSMatt Ranostay struct max30102_data *data; 393b3c590ceSMatt Ranostay struct iio_buffer *buffer; 394b3c590ceSMatt Ranostay struct iio_dev *indio_dev; 395b3c590ceSMatt Ranostay int ret; 39684b0ce05SPeter Meerwald-Stadler unsigned int reg; 397b3c590ceSMatt Ranostay 398b3c590ceSMatt Ranostay indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 399b3c590ceSMatt Ranostay if (!indio_dev) 400b3c590ceSMatt Ranostay return -ENOMEM; 401b3c590ceSMatt Ranostay 402b3c590ceSMatt Ranostay buffer = devm_iio_kfifo_allocate(&client->dev); 403b3c590ceSMatt Ranostay if (!buffer) 404b3c590ceSMatt Ranostay return -ENOMEM; 405b3c590ceSMatt Ranostay 406b3c590ceSMatt Ranostay iio_device_attach_buffer(indio_dev, buffer); 407b3c590ceSMatt Ranostay 408b3c590ceSMatt Ranostay indio_dev->name = MAX30102_DRV_NAME; 409b3c590ceSMatt Ranostay indio_dev->channels = max30102_channels; 410b3c590ceSMatt Ranostay indio_dev->info = &max30102_info; 411b3c590ceSMatt Ranostay indio_dev->num_channels = ARRAY_SIZE(max30102_channels); 412b3c590ceSMatt Ranostay indio_dev->available_scan_masks = max30102_scan_masks; 413b3c590ceSMatt Ranostay indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE); 414b3c590ceSMatt Ranostay indio_dev->setup_ops = &max30102_buffer_setup_ops; 415fa722499SLars-Peter Clausen indio_dev->dev.parent = &client->dev; 416b3c590ceSMatt Ranostay 417b3c590ceSMatt Ranostay data = iio_priv(indio_dev); 418b3c590ceSMatt Ranostay data->indio_dev = indio_dev; 419b3c590ceSMatt Ranostay data->client = client; 420b3c590ceSMatt Ranostay 421b3c590ceSMatt Ranostay mutex_init(&data->lock); 422b3c590ceSMatt Ranostay i2c_set_clientdata(client, indio_dev); 423b3c590ceSMatt Ranostay 424b3c590ceSMatt Ranostay data->regmap = devm_regmap_init_i2c(client, &max30102_regmap_config); 425b3c590ceSMatt Ranostay if (IS_ERR(data->regmap)) { 426c31c946aSPeter Meerwald-Stadler dev_err(&client->dev, "regmap initialization failed\n"); 427b3c590ceSMatt Ranostay return PTR_ERR(data->regmap); 428b3c590ceSMatt Ranostay } 429d0b950c7SPeter Meerwald-Stadler 43084b0ce05SPeter Meerwald-Stadler /* check part ID */ 43184b0ce05SPeter Meerwald-Stadler ret = regmap_read(data->regmap, MAX30102_REG_PART_ID, ®); 43284b0ce05SPeter Meerwald-Stadler if (ret) 43384b0ce05SPeter Meerwald-Stadler return ret; 43484b0ce05SPeter Meerwald-Stadler if (reg != MAX30102_PART_NUMBER) 43584b0ce05SPeter Meerwald-Stadler return -ENODEV; 43684b0ce05SPeter Meerwald-Stadler 43784b0ce05SPeter Meerwald-Stadler /* show revision ID */ 43884b0ce05SPeter Meerwald-Stadler ret = regmap_read(data->regmap, MAX30102_REG_REV_ID, ®); 43984b0ce05SPeter Meerwald-Stadler if (ret) 44084b0ce05SPeter Meerwald-Stadler return ret; 44184b0ce05SPeter Meerwald-Stadler dev_dbg(&client->dev, "max3010x revision %02x\n", reg); 44284b0ce05SPeter Meerwald-Stadler 443d0b950c7SPeter Meerwald-Stadler ret = max30102_set_powermode(data, false); 444d0b950c7SPeter Meerwald-Stadler if (ret) 445d0b950c7SPeter Meerwald-Stadler return ret; 446b3c590ceSMatt Ranostay 447b3c590ceSMatt Ranostay ret = max30102_chip_init(data); 448b3c590ceSMatt Ranostay if (ret) 449b3c590ceSMatt Ranostay return ret; 450b3c590ceSMatt Ranostay 451b3c590ceSMatt Ranostay if (client->irq <= 0) { 452b3c590ceSMatt Ranostay dev_err(&client->dev, "no valid irq defined\n"); 453b3c590ceSMatt Ranostay return -EINVAL; 454b3c590ceSMatt Ranostay } 455b3c590ceSMatt Ranostay 456b3c590ceSMatt Ranostay ret = devm_request_threaded_irq(&client->dev, client->irq, 457b3c590ceSMatt Ranostay NULL, max30102_interrupt_handler, 458b3c590ceSMatt Ranostay IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 459b3c590ceSMatt Ranostay "max30102_irq", indio_dev); 460b3c590ceSMatt Ranostay if (ret) { 461b3c590ceSMatt Ranostay dev_err(&client->dev, "request irq (%d) failed\n", client->irq); 462b3c590ceSMatt Ranostay return ret; 463b3c590ceSMatt Ranostay } 464b3c590ceSMatt Ranostay 465b3c590ceSMatt Ranostay return iio_device_register(indio_dev); 466b3c590ceSMatt Ranostay } 467b3c590ceSMatt Ranostay 468b3c590ceSMatt Ranostay static int max30102_remove(struct i2c_client *client) 469b3c590ceSMatt Ranostay { 470b3c590ceSMatt Ranostay struct iio_dev *indio_dev = i2c_get_clientdata(client); 471b3c590ceSMatt Ranostay struct max30102_data *data = iio_priv(indio_dev); 472b3c590ceSMatt Ranostay 473b3c590ceSMatt Ranostay iio_device_unregister(indio_dev); 474b3c590ceSMatt Ranostay max30102_set_powermode(data, false); 475b3c590ceSMatt Ranostay 476b3c590ceSMatt Ranostay return 0; 477b3c590ceSMatt Ranostay } 478b3c590ceSMatt Ranostay 479b3c590ceSMatt Ranostay static const struct i2c_device_id max30102_id[] = { 480b3c590ceSMatt Ranostay { "max30102", 0 }, 481b3c590ceSMatt Ranostay {} 482b3c590ceSMatt Ranostay }; 483b3c590ceSMatt Ranostay MODULE_DEVICE_TABLE(i2c, max30102_id); 484b3c590ceSMatt Ranostay 485b3c590ceSMatt Ranostay static const struct of_device_id max30102_dt_ids[] = { 486b3c590ceSMatt Ranostay { .compatible = "maxim,max30102" }, 487b3c590ceSMatt Ranostay { } 488b3c590ceSMatt Ranostay }; 489b3c590ceSMatt Ranostay MODULE_DEVICE_TABLE(of, max30102_dt_ids); 490b3c590ceSMatt Ranostay 491b3c590ceSMatt Ranostay static struct i2c_driver max30102_driver = { 492b3c590ceSMatt Ranostay .driver = { 493b3c590ceSMatt Ranostay .name = MAX30102_DRV_NAME, 494b3c590ceSMatt Ranostay .of_match_table = of_match_ptr(max30102_dt_ids), 495b3c590ceSMatt Ranostay }, 496b3c590ceSMatt Ranostay .probe = max30102_probe, 497b3c590ceSMatt Ranostay .remove = max30102_remove, 498b3c590ceSMatt Ranostay .id_table = max30102_id, 499b3c590ceSMatt Ranostay }; 500b3c590ceSMatt Ranostay module_i2c_driver(max30102_driver); 501b3c590ceSMatt Ranostay 502b3c590ceSMatt Ranostay MODULE_AUTHOR("Matt Ranostay <matt@ranostay.consulting>"); 503b3c590ceSMatt Ranostay MODULE_DESCRIPTION("MAX30102 heart rate and pulse oximeter sensor"); 504b3c590ceSMatt Ranostay MODULE_LICENSE("GPL"); 505