18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * max30102.c - Support for MAX30102 heart rate and pulse oximeter sensor 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2017 Matt Ranostay <matt.ranostay@konsulko.com> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Support for MAX30105 optical particle sensor 88c2ecf20Sopenharmony_ci * Copyright (C) 2017 Peter Meerwald-Stadler <pmeerw@pmeerw.net> 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * 7-bit I2C chip address: 0x57 118c2ecf20Sopenharmony_ci * TODO: proximity power saving feature 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/module.h> 158c2ecf20Sopenharmony_ci#include <linux/init.h> 168c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 178c2ecf20Sopenharmony_ci#include <linux/delay.h> 188c2ecf20Sopenharmony_ci#include <linux/err.h> 198c2ecf20Sopenharmony_ci#include <linux/irq.h> 208c2ecf20Sopenharmony_ci#include <linux/i2c.h> 218c2ecf20Sopenharmony_ci#include <linux/mutex.h> 228c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h> 238c2ecf20Sopenharmony_ci#include <linux/regmap.h> 248c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 258c2ecf20Sopenharmony_ci#include <linux/iio/buffer.h> 268c2ecf20Sopenharmony_ci#include <linux/iio/kfifo_buf.h> 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define MAX30102_REGMAP_NAME "max30102_regmap" 298c2ecf20Sopenharmony_ci#define MAX30102_DRV_NAME "max30102" 308c2ecf20Sopenharmony_ci#define MAX30102_PART_NUMBER 0x15 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_cienum max30102_chip_id { 338c2ecf20Sopenharmony_ci max30102, 348c2ecf20Sopenharmony_ci max30105, 358c2ecf20Sopenharmony_ci}; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cienum max3012_led_idx { 388c2ecf20Sopenharmony_ci MAX30102_LED_RED, 398c2ecf20Sopenharmony_ci MAX30102_LED_IR, 408c2ecf20Sopenharmony_ci MAX30105_LED_GREEN, 418c2ecf20Sopenharmony_ci}; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci#define MAX30102_REG_INT_STATUS 0x00 448c2ecf20Sopenharmony_ci#define MAX30102_REG_INT_STATUS_PWR_RDY BIT(0) 458c2ecf20Sopenharmony_ci#define MAX30102_REG_INT_STATUS_PROX_INT BIT(4) 468c2ecf20Sopenharmony_ci#define MAX30102_REG_INT_STATUS_ALC_OVF BIT(5) 478c2ecf20Sopenharmony_ci#define MAX30102_REG_INT_STATUS_PPG_RDY BIT(6) 488c2ecf20Sopenharmony_ci#define MAX30102_REG_INT_STATUS_FIFO_RDY BIT(7) 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci#define MAX30102_REG_INT_ENABLE 0x02 518c2ecf20Sopenharmony_ci#define MAX30102_REG_INT_ENABLE_PROX_INT_EN BIT(4) 528c2ecf20Sopenharmony_ci#define MAX30102_REG_INT_ENABLE_ALC_OVF_EN BIT(5) 538c2ecf20Sopenharmony_ci#define MAX30102_REG_INT_ENABLE_PPG_EN BIT(6) 548c2ecf20Sopenharmony_ci#define MAX30102_REG_INT_ENABLE_FIFO_EN BIT(7) 558c2ecf20Sopenharmony_ci#define MAX30102_REG_INT_ENABLE_MASK 0xf0 568c2ecf20Sopenharmony_ci#define MAX30102_REG_INT_ENABLE_MASK_SHIFT 4 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#define MAX30102_REG_FIFO_WR_PTR 0x04 598c2ecf20Sopenharmony_ci#define MAX30102_REG_FIFO_OVR_CTR 0x05 608c2ecf20Sopenharmony_ci#define MAX30102_REG_FIFO_RD_PTR 0x06 618c2ecf20Sopenharmony_ci#define MAX30102_REG_FIFO_DATA 0x07 628c2ecf20Sopenharmony_ci#define MAX30102_REG_FIFO_DATA_BYTES 3 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci#define MAX30102_REG_FIFO_CONFIG 0x08 658c2ecf20Sopenharmony_ci#define MAX30102_REG_FIFO_CONFIG_AVG_4SAMPLES BIT(1) 668c2ecf20Sopenharmony_ci#define MAX30102_REG_FIFO_CONFIG_AVG_SHIFT 5 678c2ecf20Sopenharmony_ci#define MAX30102_REG_FIFO_CONFIG_AFULL BIT(0) 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci#define MAX30102_REG_MODE_CONFIG 0x09 708c2ecf20Sopenharmony_ci#define MAX30102_REG_MODE_CONFIG_MODE_NONE 0x00 718c2ecf20Sopenharmony_ci#define MAX30102_REG_MODE_CONFIG_MODE_HR 0x02 /* red LED */ 728c2ecf20Sopenharmony_ci#define MAX30102_REG_MODE_CONFIG_MODE_HR_SPO2 0x03 /* red + IR LED */ 738c2ecf20Sopenharmony_ci#define MAX30102_REG_MODE_CONFIG_MODE_MULTI 0x07 /* multi-LED mode */ 748c2ecf20Sopenharmony_ci#define MAX30102_REG_MODE_CONFIG_MODE_MASK GENMASK(2, 0) 758c2ecf20Sopenharmony_ci#define MAX30102_REG_MODE_CONFIG_PWR BIT(7) 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci#define MAX30102_REG_MODE_CONTROL_SLOT21 0x11 /* multi-LED control */ 788c2ecf20Sopenharmony_ci#define MAX30102_REG_MODE_CONTROL_SLOT43 0x12 798c2ecf20Sopenharmony_ci#define MAX30102_REG_MODE_CONTROL_SLOT_MASK (GENMASK(6, 4) | GENMASK(2, 0)) 808c2ecf20Sopenharmony_ci#define MAX30102_REG_MODE_CONTROL_SLOT_SHIFT 4 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci#define MAX30102_REG_SPO2_CONFIG 0x0a 838c2ecf20Sopenharmony_ci#define MAX30102_REG_SPO2_CONFIG_PULSE_411_US 0x03 848c2ecf20Sopenharmony_ci#define MAX30102_REG_SPO2_CONFIG_SR_400HZ 0x03 858c2ecf20Sopenharmony_ci#define MAX30102_REG_SPO2_CONFIG_SR_MASK 0x07 868c2ecf20Sopenharmony_ci#define MAX30102_REG_SPO2_CONFIG_SR_MASK_SHIFT 2 878c2ecf20Sopenharmony_ci#define MAX30102_REG_SPO2_CONFIG_ADC_4096_STEPS BIT(0) 888c2ecf20Sopenharmony_ci#define MAX30102_REG_SPO2_CONFIG_ADC_MASK_SHIFT 5 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci#define MAX30102_REG_RED_LED_CONFIG 0x0c 918c2ecf20Sopenharmony_ci#define MAX30102_REG_IR_LED_CONFIG 0x0d 928c2ecf20Sopenharmony_ci#define MAX30105_REG_GREEN_LED_CONFIG 0x0e 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci#define MAX30102_REG_TEMP_CONFIG 0x21 958c2ecf20Sopenharmony_ci#define MAX30102_REG_TEMP_CONFIG_TEMP_EN BIT(0) 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci#define MAX30102_REG_TEMP_INTEGER 0x1f 988c2ecf20Sopenharmony_ci#define MAX30102_REG_TEMP_FRACTION 0x20 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci#define MAX30102_REG_REV_ID 0xfe 1018c2ecf20Sopenharmony_ci#define MAX30102_REG_PART_ID 0xff 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_cistruct max30102_data { 1048c2ecf20Sopenharmony_ci struct i2c_client *client; 1058c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 1068c2ecf20Sopenharmony_ci struct mutex lock; 1078c2ecf20Sopenharmony_ci struct regmap *regmap; 1088c2ecf20Sopenharmony_ci enum max30102_chip_id chip_id; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci u8 buffer[12]; 1118c2ecf20Sopenharmony_ci __be32 processed_buffer[3]; /* 3 x 18-bit (padded to 32-bits) */ 1128c2ecf20Sopenharmony_ci}; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistatic const struct regmap_config max30102_regmap_config = { 1158c2ecf20Sopenharmony_ci .name = MAX30102_REGMAP_NAME, 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci .reg_bits = 8, 1188c2ecf20Sopenharmony_ci .val_bits = 8, 1198c2ecf20Sopenharmony_ci}; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_cistatic const unsigned long max30102_scan_masks[] = { 1228c2ecf20Sopenharmony_ci BIT(MAX30102_LED_RED) | BIT(MAX30102_LED_IR), 1238c2ecf20Sopenharmony_ci 0 1248c2ecf20Sopenharmony_ci}; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_cistatic const unsigned long max30105_scan_masks[] = { 1278c2ecf20Sopenharmony_ci BIT(MAX30102_LED_RED) | BIT(MAX30102_LED_IR), 1288c2ecf20Sopenharmony_ci BIT(MAX30102_LED_RED) | BIT(MAX30102_LED_IR) | 1298c2ecf20Sopenharmony_ci BIT(MAX30105_LED_GREEN), 1308c2ecf20Sopenharmony_ci 0 1318c2ecf20Sopenharmony_ci}; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci#define MAX30102_INTENSITY_CHANNEL(_si, _mod) { \ 1348c2ecf20Sopenharmony_ci .type = IIO_INTENSITY, \ 1358c2ecf20Sopenharmony_ci .channel2 = _mod, \ 1368c2ecf20Sopenharmony_ci .modified = 1, \ 1378c2ecf20Sopenharmony_ci .scan_index = _si, \ 1388c2ecf20Sopenharmony_ci .scan_type = { \ 1398c2ecf20Sopenharmony_ci .sign = 'u', \ 1408c2ecf20Sopenharmony_ci .shift = 8, \ 1418c2ecf20Sopenharmony_ci .realbits = 18, \ 1428c2ecf20Sopenharmony_ci .storagebits = 32, \ 1438c2ecf20Sopenharmony_ci .endianness = IIO_BE, \ 1448c2ecf20Sopenharmony_ci }, \ 1458c2ecf20Sopenharmony_ci } 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_cistatic const struct iio_chan_spec max30102_channels[] = { 1488c2ecf20Sopenharmony_ci MAX30102_INTENSITY_CHANNEL(MAX30102_LED_RED, IIO_MOD_LIGHT_RED), 1498c2ecf20Sopenharmony_ci MAX30102_INTENSITY_CHANNEL(MAX30102_LED_IR, IIO_MOD_LIGHT_IR), 1508c2ecf20Sopenharmony_ci { 1518c2ecf20Sopenharmony_ci .type = IIO_TEMP, 1528c2ecf20Sopenharmony_ci .info_mask_separate = 1538c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 1548c2ecf20Sopenharmony_ci .scan_index = -1, 1558c2ecf20Sopenharmony_ci }, 1568c2ecf20Sopenharmony_ci}; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_cistatic const struct iio_chan_spec max30105_channels[] = { 1598c2ecf20Sopenharmony_ci MAX30102_INTENSITY_CHANNEL(MAX30102_LED_RED, IIO_MOD_LIGHT_RED), 1608c2ecf20Sopenharmony_ci MAX30102_INTENSITY_CHANNEL(MAX30102_LED_IR, IIO_MOD_LIGHT_IR), 1618c2ecf20Sopenharmony_ci MAX30102_INTENSITY_CHANNEL(MAX30105_LED_GREEN, IIO_MOD_LIGHT_GREEN), 1628c2ecf20Sopenharmony_ci { 1638c2ecf20Sopenharmony_ci .type = IIO_TEMP, 1648c2ecf20Sopenharmony_ci .info_mask_separate = 1658c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 1668c2ecf20Sopenharmony_ci .scan_index = -1, 1678c2ecf20Sopenharmony_ci }, 1688c2ecf20Sopenharmony_ci}; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_cistatic int max30102_set_power(struct max30102_data *data, bool en) 1718c2ecf20Sopenharmony_ci{ 1728c2ecf20Sopenharmony_ci return regmap_update_bits(data->regmap, MAX30102_REG_MODE_CONFIG, 1738c2ecf20Sopenharmony_ci MAX30102_REG_MODE_CONFIG_PWR, 1748c2ecf20Sopenharmony_ci en ? 0 : MAX30102_REG_MODE_CONFIG_PWR); 1758c2ecf20Sopenharmony_ci} 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_cistatic int max30102_set_powermode(struct max30102_data *data, u8 mode, bool en) 1788c2ecf20Sopenharmony_ci{ 1798c2ecf20Sopenharmony_ci u8 reg = mode; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci if (!en) 1828c2ecf20Sopenharmony_ci reg |= MAX30102_REG_MODE_CONFIG_PWR; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci return regmap_update_bits(data->regmap, MAX30102_REG_MODE_CONFIG, 1858c2ecf20Sopenharmony_ci MAX30102_REG_MODE_CONFIG_PWR | 1868c2ecf20Sopenharmony_ci MAX30102_REG_MODE_CONFIG_MODE_MASK, reg); 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci#define MAX30102_MODE_CONTROL_LED_SLOTS(slot2, slot1) \ 1908c2ecf20Sopenharmony_ci ((slot2 << MAX30102_REG_MODE_CONTROL_SLOT_SHIFT) | slot1) 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_cistatic int max30102_buffer_postenable(struct iio_dev *indio_dev) 1938c2ecf20Sopenharmony_ci{ 1948c2ecf20Sopenharmony_ci struct max30102_data *data = iio_priv(indio_dev); 1958c2ecf20Sopenharmony_ci int ret; 1968c2ecf20Sopenharmony_ci u8 reg; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci switch (*indio_dev->active_scan_mask) { 1998c2ecf20Sopenharmony_ci case BIT(MAX30102_LED_RED) | BIT(MAX30102_LED_IR): 2008c2ecf20Sopenharmony_ci reg = MAX30102_REG_MODE_CONFIG_MODE_HR_SPO2; 2018c2ecf20Sopenharmony_ci break; 2028c2ecf20Sopenharmony_ci case BIT(MAX30102_LED_RED) | BIT(MAX30102_LED_IR) | 2038c2ecf20Sopenharmony_ci BIT(MAX30105_LED_GREEN): 2048c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, 2058c2ecf20Sopenharmony_ci MAX30102_REG_MODE_CONTROL_SLOT21, 2068c2ecf20Sopenharmony_ci MAX30102_REG_MODE_CONTROL_SLOT_MASK, 2078c2ecf20Sopenharmony_ci MAX30102_MODE_CONTROL_LED_SLOTS(2, 1)); 2088c2ecf20Sopenharmony_ci if (ret) 2098c2ecf20Sopenharmony_ci return ret; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, 2128c2ecf20Sopenharmony_ci MAX30102_REG_MODE_CONTROL_SLOT43, 2138c2ecf20Sopenharmony_ci MAX30102_REG_MODE_CONTROL_SLOT_MASK, 2148c2ecf20Sopenharmony_ci MAX30102_MODE_CONTROL_LED_SLOTS(0, 3)); 2158c2ecf20Sopenharmony_ci if (ret) 2168c2ecf20Sopenharmony_ci return ret; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci reg = MAX30102_REG_MODE_CONFIG_MODE_MULTI; 2198c2ecf20Sopenharmony_ci break; 2208c2ecf20Sopenharmony_ci default: 2218c2ecf20Sopenharmony_ci return -EINVAL; 2228c2ecf20Sopenharmony_ci } 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci return max30102_set_powermode(data, reg, true); 2258c2ecf20Sopenharmony_ci} 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_cistatic int max30102_buffer_predisable(struct iio_dev *indio_dev) 2288c2ecf20Sopenharmony_ci{ 2298c2ecf20Sopenharmony_ci struct max30102_data *data = iio_priv(indio_dev); 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci return max30102_set_powermode(data, MAX30102_REG_MODE_CONFIG_MODE_NONE, 2328c2ecf20Sopenharmony_ci false); 2338c2ecf20Sopenharmony_ci} 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_cistatic const struct iio_buffer_setup_ops max30102_buffer_setup_ops = { 2368c2ecf20Sopenharmony_ci .postenable = max30102_buffer_postenable, 2378c2ecf20Sopenharmony_ci .predisable = max30102_buffer_predisable, 2388c2ecf20Sopenharmony_ci}; 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_cistatic inline int max30102_fifo_count(struct max30102_data *data) 2418c2ecf20Sopenharmony_ci{ 2428c2ecf20Sopenharmony_ci unsigned int val; 2438c2ecf20Sopenharmony_ci int ret; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MAX30102_REG_INT_STATUS, &val); 2468c2ecf20Sopenharmony_ci if (ret) 2478c2ecf20Sopenharmony_ci return ret; 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci /* FIFO has one sample slot left */ 2508c2ecf20Sopenharmony_ci if (val & MAX30102_REG_INT_STATUS_FIFO_RDY) 2518c2ecf20Sopenharmony_ci return 1; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci return 0; 2548c2ecf20Sopenharmony_ci} 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci#define MAX30102_COPY_DATA(i) \ 2578c2ecf20Sopenharmony_ci memcpy(&data->processed_buffer[(i)], \ 2588c2ecf20Sopenharmony_ci &buffer[(i) * MAX30102_REG_FIFO_DATA_BYTES], \ 2598c2ecf20Sopenharmony_ci MAX30102_REG_FIFO_DATA_BYTES) 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_cistatic int max30102_read_measurement(struct max30102_data *data, 2628c2ecf20Sopenharmony_ci unsigned int measurements) 2638c2ecf20Sopenharmony_ci{ 2648c2ecf20Sopenharmony_ci int ret; 2658c2ecf20Sopenharmony_ci u8 *buffer = (u8 *) &data->buffer; 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci ret = i2c_smbus_read_i2c_block_data(data->client, 2688c2ecf20Sopenharmony_ci MAX30102_REG_FIFO_DATA, 2698c2ecf20Sopenharmony_ci measurements * 2708c2ecf20Sopenharmony_ci MAX30102_REG_FIFO_DATA_BYTES, 2718c2ecf20Sopenharmony_ci buffer); 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci switch (measurements) { 2748c2ecf20Sopenharmony_ci case 3: 2758c2ecf20Sopenharmony_ci MAX30102_COPY_DATA(2); 2768c2ecf20Sopenharmony_ci fallthrough; 2778c2ecf20Sopenharmony_ci case 2: 2788c2ecf20Sopenharmony_ci MAX30102_COPY_DATA(1); 2798c2ecf20Sopenharmony_ci fallthrough; 2808c2ecf20Sopenharmony_ci case 1: 2818c2ecf20Sopenharmony_ci MAX30102_COPY_DATA(0); 2828c2ecf20Sopenharmony_ci break; 2838c2ecf20Sopenharmony_ci default: 2848c2ecf20Sopenharmony_ci return -EINVAL; 2858c2ecf20Sopenharmony_ci } 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci return (ret == measurements * MAX30102_REG_FIFO_DATA_BYTES) ? 2888c2ecf20Sopenharmony_ci 0 : -EINVAL; 2898c2ecf20Sopenharmony_ci} 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_cistatic irqreturn_t max30102_interrupt_handler(int irq, void *private) 2928c2ecf20Sopenharmony_ci{ 2938c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = private; 2948c2ecf20Sopenharmony_ci struct max30102_data *data = iio_priv(indio_dev); 2958c2ecf20Sopenharmony_ci unsigned int measurements = bitmap_weight(indio_dev->active_scan_mask, 2968c2ecf20Sopenharmony_ci indio_dev->masklength); 2978c2ecf20Sopenharmony_ci int ret, cnt = 0; 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci while (cnt || (cnt = max30102_fifo_count(data)) > 0) { 3028c2ecf20Sopenharmony_ci ret = max30102_read_measurement(data, measurements); 3038c2ecf20Sopenharmony_ci if (ret) 3048c2ecf20Sopenharmony_ci break; 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci iio_push_to_buffers(data->indio_dev, data->processed_buffer); 3078c2ecf20Sopenharmony_ci cnt--; 3088c2ecf20Sopenharmony_ci } 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci return IRQ_HANDLED; 3138c2ecf20Sopenharmony_ci} 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_cistatic int max30102_get_current_idx(unsigned int val, int *reg) 3168c2ecf20Sopenharmony_ci{ 3178c2ecf20Sopenharmony_ci /* each step is 0.200 mA */ 3188c2ecf20Sopenharmony_ci *reg = val / 200; 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci return *reg > 0xff ? -EINVAL : 0; 3218c2ecf20Sopenharmony_ci} 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_cistatic int max30102_led_init(struct max30102_data *data) 3248c2ecf20Sopenharmony_ci{ 3258c2ecf20Sopenharmony_ci struct device *dev = &data->client->dev; 3268c2ecf20Sopenharmony_ci unsigned int val; 3278c2ecf20Sopenharmony_ci int reg, ret; 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci ret = device_property_read_u32(dev, "maxim,red-led-current-microamp", &val); 3308c2ecf20Sopenharmony_ci if (ret) { 3318c2ecf20Sopenharmony_ci dev_info(dev, "no red-led-current-microamp set\n"); 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci /* Default to 7 mA RED LED */ 3348c2ecf20Sopenharmony_ci val = 7000; 3358c2ecf20Sopenharmony_ci } 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci ret = max30102_get_current_idx(val, ®); 3388c2ecf20Sopenharmony_ci if (ret) { 3398c2ecf20Sopenharmony_ci dev_err(dev, "invalid RED LED current setting %d\n", val); 3408c2ecf20Sopenharmony_ci return ret; 3418c2ecf20Sopenharmony_ci } 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, MAX30102_REG_RED_LED_CONFIG, reg); 3448c2ecf20Sopenharmony_ci if (ret) 3458c2ecf20Sopenharmony_ci return ret; 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci if (data->chip_id == max30105) { 3488c2ecf20Sopenharmony_ci ret = device_property_read_u32(dev, 3498c2ecf20Sopenharmony_ci "maxim,green-led-current-microamp", &val); 3508c2ecf20Sopenharmony_ci if (ret) { 3518c2ecf20Sopenharmony_ci dev_info(dev, "no green-led-current-microamp set\n"); 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci /* Default to 7 mA green LED */ 3548c2ecf20Sopenharmony_ci val = 7000; 3558c2ecf20Sopenharmony_ci } 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci ret = max30102_get_current_idx(val, ®); 3588c2ecf20Sopenharmony_ci if (ret) { 3598c2ecf20Sopenharmony_ci dev_err(dev, "invalid green LED current setting %d\n", 3608c2ecf20Sopenharmony_ci val); 3618c2ecf20Sopenharmony_ci return ret; 3628c2ecf20Sopenharmony_ci } 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, MAX30105_REG_GREEN_LED_CONFIG, 3658c2ecf20Sopenharmony_ci reg); 3668c2ecf20Sopenharmony_ci if (ret) 3678c2ecf20Sopenharmony_ci return ret; 3688c2ecf20Sopenharmony_ci } 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci ret = device_property_read_u32(dev, "maxim,ir-led-current-microamp", &val); 3718c2ecf20Sopenharmony_ci if (ret) { 3728c2ecf20Sopenharmony_ci dev_info(dev, "no ir-led-current-microamp set\n"); 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci /* Default to 7 mA IR LED */ 3758c2ecf20Sopenharmony_ci val = 7000; 3768c2ecf20Sopenharmony_ci } 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci ret = max30102_get_current_idx(val, ®); 3798c2ecf20Sopenharmony_ci if (ret) { 3808c2ecf20Sopenharmony_ci dev_err(dev, "invalid IR LED current setting %d\n", val); 3818c2ecf20Sopenharmony_ci return ret; 3828c2ecf20Sopenharmony_ci } 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci return regmap_write(data->regmap, MAX30102_REG_IR_LED_CONFIG, reg); 3858c2ecf20Sopenharmony_ci} 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_cistatic int max30102_chip_init(struct max30102_data *data) 3888c2ecf20Sopenharmony_ci{ 3898c2ecf20Sopenharmony_ci int ret; 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci /* setup LED current settings */ 3928c2ecf20Sopenharmony_ci ret = max30102_led_init(data); 3938c2ecf20Sopenharmony_ci if (ret) 3948c2ecf20Sopenharmony_ci return ret; 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci /* configure 18-bit HR + SpO2 readings at 400Hz */ 3978c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, MAX30102_REG_SPO2_CONFIG, 3988c2ecf20Sopenharmony_ci (MAX30102_REG_SPO2_CONFIG_ADC_4096_STEPS 3998c2ecf20Sopenharmony_ci << MAX30102_REG_SPO2_CONFIG_ADC_MASK_SHIFT) | 4008c2ecf20Sopenharmony_ci (MAX30102_REG_SPO2_CONFIG_SR_400HZ 4018c2ecf20Sopenharmony_ci << MAX30102_REG_SPO2_CONFIG_SR_MASK_SHIFT) | 4028c2ecf20Sopenharmony_ci MAX30102_REG_SPO2_CONFIG_PULSE_411_US); 4038c2ecf20Sopenharmony_ci if (ret) 4048c2ecf20Sopenharmony_ci return ret; 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci /* average 4 samples + generate FIFO interrupt */ 4078c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, MAX30102_REG_FIFO_CONFIG, 4088c2ecf20Sopenharmony_ci (MAX30102_REG_FIFO_CONFIG_AVG_4SAMPLES 4098c2ecf20Sopenharmony_ci << MAX30102_REG_FIFO_CONFIG_AVG_SHIFT) | 4108c2ecf20Sopenharmony_ci MAX30102_REG_FIFO_CONFIG_AFULL); 4118c2ecf20Sopenharmony_ci if (ret) 4128c2ecf20Sopenharmony_ci return ret; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci /* enable FIFO interrupt */ 4158c2ecf20Sopenharmony_ci return regmap_update_bits(data->regmap, MAX30102_REG_INT_ENABLE, 4168c2ecf20Sopenharmony_ci MAX30102_REG_INT_ENABLE_MASK, 4178c2ecf20Sopenharmony_ci MAX30102_REG_INT_ENABLE_FIFO_EN); 4188c2ecf20Sopenharmony_ci} 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_cistatic int max30102_read_temp(struct max30102_data *data, int *val) 4218c2ecf20Sopenharmony_ci{ 4228c2ecf20Sopenharmony_ci int ret; 4238c2ecf20Sopenharmony_ci unsigned int reg; 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MAX30102_REG_TEMP_INTEGER, ®); 4268c2ecf20Sopenharmony_ci if (ret < 0) 4278c2ecf20Sopenharmony_ci return ret; 4288c2ecf20Sopenharmony_ci *val = reg << 4; 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MAX30102_REG_TEMP_FRACTION, ®); 4318c2ecf20Sopenharmony_ci if (ret < 0) 4328c2ecf20Sopenharmony_ci return ret; 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci *val |= reg & 0xf; 4358c2ecf20Sopenharmony_ci *val = sign_extend32(*val, 11); 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci return 0; 4388c2ecf20Sopenharmony_ci} 4398c2ecf20Sopenharmony_ci 4408c2ecf20Sopenharmony_cistatic int max30102_get_temp(struct max30102_data *data, int *val, bool en) 4418c2ecf20Sopenharmony_ci{ 4428c2ecf20Sopenharmony_ci int ret; 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci if (en) { 4458c2ecf20Sopenharmony_ci ret = max30102_set_power(data, true); 4468c2ecf20Sopenharmony_ci if (ret) 4478c2ecf20Sopenharmony_ci return ret; 4488c2ecf20Sopenharmony_ci } 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci /* start acquisition */ 4518c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, MAX30102_REG_TEMP_CONFIG, 4528c2ecf20Sopenharmony_ci MAX30102_REG_TEMP_CONFIG_TEMP_EN, 4538c2ecf20Sopenharmony_ci MAX30102_REG_TEMP_CONFIG_TEMP_EN); 4548c2ecf20Sopenharmony_ci if (ret) 4558c2ecf20Sopenharmony_ci goto out; 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci msleep(35); 4588c2ecf20Sopenharmony_ci ret = max30102_read_temp(data, val); 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ciout: 4618c2ecf20Sopenharmony_ci if (en) 4628c2ecf20Sopenharmony_ci max30102_set_power(data, false); 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci return ret; 4658c2ecf20Sopenharmony_ci} 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_cistatic int max30102_read_raw(struct iio_dev *indio_dev, 4688c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 4698c2ecf20Sopenharmony_ci int *val, int *val2, long mask) 4708c2ecf20Sopenharmony_ci{ 4718c2ecf20Sopenharmony_ci struct max30102_data *data = iio_priv(indio_dev); 4728c2ecf20Sopenharmony_ci int ret = -EINVAL; 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci switch (mask) { 4758c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 4768c2ecf20Sopenharmony_ci /* 4778c2ecf20Sopenharmony_ci * Temperature reading can only be acquired when not in 4788c2ecf20Sopenharmony_ci * shutdown; leave shutdown briefly when buffer not running 4798c2ecf20Sopenharmony_ci */ 4808c2ecf20Sopenharmony_ci mutex_lock(&indio_dev->mlock); 4818c2ecf20Sopenharmony_ci if (!iio_buffer_enabled(indio_dev)) 4828c2ecf20Sopenharmony_ci ret = max30102_get_temp(data, val, true); 4838c2ecf20Sopenharmony_ci else 4848c2ecf20Sopenharmony_ci ret = max30102_get_temp(data, val, false); 4858c2ecf20Sopenharmony_ci mutex_unlock(&indio_dev->mlock); 4868c2ecf20Sopenharmony_ci if (ret) 4878c2ecf20Sopenharmony_ci return ret; 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci ret = IIO_VAL_INT; 4908c2ecf20Sopenharmony_ci break; 4918c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SCALE: 4928c2ecf20Sopenharmony_ci *val = 1000; /* 62.5 */ 4938c2ecf20Sopenharmony_ci *val2 = 16; 4948c2ecf20Sopenharmony_ci ret = IIO_VAL_FRACTIONAL; 4958c2ecf20Sopenharmony_ci break; 4968c2ecf20Sopenharmony_ci } 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci return ret; 4998c2ecf20Sopenharmony_ci} 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_cistatic const struct iio_info max30102_info = { 5028c2ecf20Sopenharmony_ci .read_raw = max30102_read_raw, 5038c2ecf20Sopenharmony_ci}; 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_cistatic int max30102_probe(struct i2c_client *client, 5068c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 5078c2ecf20Sopenharmony_ci{ 5088c2ecf20Sopenharmony_ci struct max30102_data *data; 5098c2ecf20Sopenharmony_ci struct iio_buffer *buffer; 5108c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 5118c2ecf20Sopenharmony_ci int ret; 5128c2ecf20Sopenharmony_ci unsigned int reg; 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 5158c2ecf20Sopenharmony_ci if (!indio_dev) 5168c2ecf20Sopenharmony_ci return -ENOMEM; 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci buffer = devm_iio_kfifo_allocate(&client->dev); 5198c2ecf20Sopenharmony_ci if (!buffer) 5208c2ecf20Sopenharmony_ci return -ENOMEM; 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci iio_device_attach_buffer(indio_dev, buffer); 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci indio_dev->name = MAX30102_DRV_NAME; 5258c2ecf20Sopenharmony_ci indio_dev->info = &max30102_info; 5268c2ecf20Sopenharmony_ci indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE); 5278c2ecf20Sopenharmony_ci indio_dev->setup_ops = &max30102_buffer_setup_ops; 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ci data = iio_priv(indio_dev); 5308c2ecf20Sopenharmony_ci data->indio_dev = indio_dev; 5318c2ecf20Sopenharmony_ci data->client = client; 5328c2ecf20Sopenharmony_ci data->chip_id = id->driver_data; 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_ci mutex_init(&data->lock); 5358c2ecf20Sopenharmony_ci i2c_set_clientdata(client, indio_dev); 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci switch (data->chip_id) { 5388c2ecf20Sopenharmony_ci case max30105: 5398c2ecf20Sopenharmony_ci indio_dev->channels = max30105_channels; 5408c2ecf20Sopenharmony_ci indio_dev->num_channels = ARRAY_SIZE(max30105_channels); 5418c2ecf20Sopenharmony_ci indio_dev->available_scan_masks = max30105_scan_masks; 5428c2ecf20Sopenharmony_ci break; 5438c2ecf20Sopenharmony_ci case max30102: 5448c2ecf20Sopenharmony_ci indio_dev->channels = max30102_channels; 5458c2ecf20Sopenharmony_ci indio_dev->num_channels = ARRAY_SIZE(max30102_channels); 5468c2ecf20Sopenharmony_ci indio_dev->available_scan_masks = max30102_scan_masks; 5478c2ecf20Sopenharmony_ci break; 5488c2ecf20Sopenharmony_ci default: 5498c2ecf20Sopenharmony_ci return -ENODEV; 5508c2ecf20Sopenharmony_ci } 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_ci data->regmap = devm_regmap_init_i2c(client, &max30102_regmap_config); 5538c2ecf20Sopenharmony_ci if (IS_ERR(data->regmap)) { 5548c2ecf20Sopenharmony_ci dev_err(&client->dev, "regmap initialization failed\n"); 5558c2ecf20Sopenharmony_ci return PTR_ERR(data->regmap); 5568c2ecf20Sopenharmony_ci } 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_ci /* check part ID */ 5598c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MAX30102_REG_PART_ID, ®); 5608c2ecf20Sopenharmony_ci if (ret) 5618c2ecf20Sopenharmony_ci return ret; 5628c2ecf20Sopenharmony_ci if (reg != MAX30102_PART_NUMBER) 5638c2ecf20Sopenharmony_ci return -ENODEV; 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_ci /* show revision ID */ 5668c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MAX30102_REG_REV_ID, ®); 5678c2ecf20Sopenharmony_ci if (ret) 5688c2ecf20Sopenharmony_ci return ret; 5698c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "max3010x revision %02x\n", reg); 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci /* clear mode setting, chip shutdown */ 5728c2ecf20Sopenharmony_ci ret = max30102_set_powermode(data, MAX30102_REG_MODE_CONFIG_MODE_NONE, 5738c2ecf20Sopenharmony_ci false); 5748c2ecf20Sopenharmony_ci if (ret) 5758c2ecf20Sopenharmony_ci return ret; 5768c2ecf20Sopenharmony_ci 5778c2ecf20Sopenharmony_ci ret = max30102_chip_init(data); 5788c2ecf20Sopenharmony_ci if (ret) 5798c2ecf20Sopenharmony_ci return ret; 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_ci if (client->irq <= 0) { 5828c2ecf20Sopenharmony_ci dev_err(&client->dev, "no valid irq defined\n"); 5838c2ecf20Sopenharmony_ci return -EINVAL; 5848c2ecf20Sopenharmony_ci } 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci ret = devm_request_threaded_irq(&client->dev, client->irq, 5878c2ecf20Sopenharmony_ci NULL, max30102_interrupt_handler, 5888c2ecf20Sopenharmony_ci IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 5898c2ecf20Sopenharmony_ci "max30102_irq", indio_dev); 5908c2ecf20Sopenharmony_ci if (ret) { 5918c2ecf20Sopenharmony_ci dev_err(&client->dev, "request irq (%d) failed\n", client->irq); 5928c2ecf20Sopenharmony_ci return ret; 5938c2ecf20Sopenharmony_ci } 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci return iio_device_register(indio_dev); 5968c2ecf20Sopenharmony_ci} 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_cistatic int max30102_remove(struct i2c_client *client) 5998c2ecf20Sopenharmony_ci{ 6008c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = i2c_get_clientdata(client); 6018c2ecf20Sopenharmony_ci struct max30102_data *data = iio_priv(indio_dev); 6028c2ecf20Sopenharmony_ci 6038c2ecf20Sopenharmony_ci iio_device_unregister(indio_dev); 6048c2ecf20Sopenharmony_ci max30102_set_power(data, false); 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_ci return 0; 6078c2ecf20Sopenharmony_ci} 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_cistatic const struct i2c_device_id max30102_id[] = { 6108c2ecf20Sopenharmony_ci { "max30102", max30102 }, 6118c2ecf20Sopenharmony_ci { "max30105", max30105 }, 6128c2ecf20Sopenharmony_ci {} 6138c2ecf20Sopenharmony_ci}; 6148c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, max30102_id); 6158c2ecf20Sopenharmony_ci 6168c2ecf20Sopenharmony_cistatic const struct of_device_id max30102_dt_ids[] = { 6178c2ecf20Sopenharmony_ci { .compatible = "maxim,max30102" }, 6188c2ecf20Sopenharmony_ci { .compatible = "maxim,max30105" }, 6198c2ecf20Sopenharmony_ci { } 6208c2ecf20Sopenharmony_ci}; 6218c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, max30102_dt_ids); 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_cistatic struct i2c_driver max30102_driver = { 6248c2ecf20Sopenharmony_ci .driver = { 6258c2ecf20Sopenharmony_ci .name = MAX30102_DRV_NAME, 6268c2ecf20Sopenharmony_ci .of_match_table = max30102_dt_ids, 6278c2ecf20Sopenharmony_ci }, 6288c2ecf20Sopenharmony_ci .probe = max30102_probe, 6298c2ecf20Sopenharmony_ci .remove = max30102_remove, 6308c2ecf20Sopenharmony_ci .id_table = max30102_id, 6318c2ecf20Sopenharmony_ci}; 6328c2ecf20Sopenharmony_cimodule_i2c_driver(max30102_driver); 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_ciMODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>"); 6358c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MAX30102 heart rate/pulse oximeter and MAX30105 particle sensor driver"); 6368c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 637