18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * AFE4403 Heart Rate Monitors and Low-Cost Pulse Oximeters 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2015-2016 Texas Instruments Incorporated - https://www.ti.com/ 68c2ecf20Sopenharmony_ci * Andrew F. Davis <afd@ti.com> 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/device.h> 108c2ecf20Sopenharmony_ci#include <linux/err.h> 118c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 128c2ecf20Sopenharmony_ci#include <linux/kernel.h> 138c2ecf20Sopenharmony_ci#include <linux/module.h> 148c2ecf20Sopenharmony_ci#include <linux/regmap.h> 158c2ecf20Sopenharmony_ci#include <linux/spi/spi.h> 168c2ecf20Sopenharmony_ci#include <linux/sysfs.h> 178c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 208c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h> 218c2ecf20Sopenharmony_ci#include <linux/iio/buffer.h> 228c2ecf20Sopenharmony_ci#include <linux/iio/trigger.h> 238c2ecf20Sopenharmony_ci#include <linux/iio/triggered_buffer.h> 248c2ecf20Sopenharmony_ci#include <linux/iio/trigger_consumer.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#include "afe440x.h" 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#define AFE4403_DRIVER_NAME "afe4403" 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/* AFE4403 Registers */ 338c2ecf20Sopenharmony_ci#define AFE4403_TIAGAIN 0x20 348c2ecf20Sopenharmony_ci#define AFE4403_TIA_AMB_GAIN 0x21 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cienum afe4403_fields { 378c2ecf20Sopenharmony_ci /* Gains */ 388c2ecf20Sopenharmony_ci F_RF_LED1, F_CF_LED1, 398c2ecf20Sopenharmony_ci F_RF_LED, F_CF_LED, 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci /* LED Current */ 428c2ecf20Sopenharmony_ci F_ILED1, F_ILED2, 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci /* sentinel */ 458c2ecf20Sopenharmony_ci F_MAX_FIELDS 468c2ecf20Sopenharmony_ci}; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistatic const struct reg_field afe4403_reg_fields[] = { 498c2ecf20Sopenharmony_ci /* Gains */ 508c2ecf20Sopenharmony_ci [F_RF_LED1] = REG_FIELD(AFE4403_TIAGAIN, 0, 2), 518c2ecf20Sopenharmony_ci [F_CF_LED1] = REG_FIELD(AFE4403_TIAGAIN, 3, 7), 528c2ecf20Sopenharmony_ci [F_RF_LED] = REG_FIELD(AFE4403_TIA_AMB_GAIN, 0, 2), 538c2ecf20Sopenharmony_ci [F_CF_LED] = REG_FIELD(AFE4403_TIA_AMB_GAIN, 3, 7), 548c2ecf20Sopenharmony_ci /* LED Current */ 558c2ecf20Sopenharmony_ci [F_ILED1] = REG_FIELD(AFE440X_LEDCNTRL, 0, 7), 568c2ecf20Sopenharmony_ci [F_ILED2] = REG_FIELD(AFE440X_LEDCNTRL, 8, 15), 578c2ecf20Sopenharmony_ci}; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci/** 608c2ecf20Sopenharmony_ci * struct afe4403_data - AFE4403 device instance data 618c2ecf20Sopenharmony_ci * @dev: Device structure 628c2ecf20Sopenharmony_ci * @spi: SPI device handle 638c2ecf20Sopenharmony_ci * @regmap: Register map of the device 648c2ecf20Sopenharmony_ci * @fields: Register fields of the device 658c2ecf20Sopenharmony_ci * @regulator: Pointer to the regulator for the IC 668c2ecf20Sopenharmony_ci * @trig: IIO trigger for this device 678c2ecf20Sopenharmony_ci * @irq: ADC_RDY line interrupt number 688c2ecf20Sopenharmony_ci * @buffer: Used to construct data layout to push into IIO buffer. 698c2ecf20Sopenharmony_ci */ 708c2ecf20Sopenharmony_cistruct afe4403_data { 718c2ecf20Sopenharmony_ci struct device *dev; 728c2ecf20Sopenharmony_ci struct spi_device *spi; 738c2ecf20Sopenharmony_ci struct regmap *regmap; 748c2ecf20Sopenharmony_ci struct regmap_field *fields[F_MAX_FIELDS]; 758c2ecf20Sopenharmony_ci struct regulator *regulator; 768c2ecf20Sopenharmony_ci struct iio_trigger *trig; 778c2ecf20Sopenharmony_ci int irq; 788c2ecf20Sopenharmony_ci /* Ensure suitable alignment for timestamp */ 798c2ecf20Sopenharmony_ci s32 buffer[8] __aligned(8); 808c2ecf20Sopenharmony_ci}; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cienum afe4403_chan_id { 838c2ecf20Sopenharmony_ci LED2 = 1, 848c2ecf20Sopenharmony_ci ALED2, 858c2ecf20Sopenharmony_ci LED1, 868c2ecf20Sopenharmony_ci ALED1, 878c2ecf20Sopenharmony_ci LED2_ALED2, 888c2ecf20Sopenharmony_ci LED1_ALED1, 898c2ecf20Sopenharmony_ci}; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cistatic const unsigned int afe4403_channel_values[] = { 928c2ecf20Sopenharmony_ci [LED2] = AFE440X_LED2VAL, 938c2ecf20Sopenharmony_ci [ALED2] = AFE440X_ALED2VAL, 948c2ecf20Sopenharmony_ci [LED1] = AFE440X_LED1VAL, 958c2ecf20Sopenharmony_ci [ALED1] = AFE440X_ALED1VAL, 968c2ecf20Sopenharmony_ci [LED2_ALED2] = AFE440X_LED2_ALED2VAL, 978c2ecf20Sopenharmony_ci [LED1_ALED1] = AFE440X_LED1_ALED1VAL, 988c2ecf20Sopenharmony_ci}; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic const unsigned int afe4403_channel_leds[] = { 1018c2ecf20Sopenharmony_ci [LED2] = F_ILED2, 1028c2ecf20Sopenharmony_ci [LED1] = F_ILED1, 1038c2ecf20Sopenharmony_ci}; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_cistatic const struct iio_chan_spec afe4403_channels[] = { 1068c2ecf20Sopenharmony_ci /* ADC values */ 1078c2ecf20Sopenharmony_ci AFE440X_INTENSITY_CHAN(LED2, 0), 1088c2ecf20Sopenharmony_ci AFE440X_INTENSITY_CHAN(ALED2, 0), 1098c2ecf20Sopenharmony_ci AFE440X_INTENSITY_CHAN(LED1, 0), 1108c2ecf20Sopenharmony_ci AFE440X_INTENSITY_CHAN(ALED1, 0), 1118c2ecf20Sopenharmony_ci AFE440X_INTENSITY_CHAN(LED2_ALED2, 0), 1128c2ecf20Sopenharmony_ci AFE440X_INTENSITY_CHAN(LED1_ALED1, 0), 1138c2ecf20Sopenharmony_ci /* LED current */ 1148c2ecf20Sopenharmony_ci AFE440X_CURRENT_CHAN(LED2), 1158c2ecf20Sopenharmony_ci AFE440X_CURRENT_CHAN(LED1), 1168c2ecf20Sopenharmony_ci}; 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic const struct afe440x_val_table afe4403_res_table[] = { 1198c2ecf20Sopenharmony_ci { 500000 }, { 250000 }, { 100000 }, { 50000 }, 1208c2ecf20Sopenharmony_ci { 25000 }, { 10000 }, { 1000000 }, { 0 }, 1218c2ecf20Sopenharmony_ci}; 1228c2ecf20Sopenharmony_ciAFE440X_TABLE_ATTR(in_intensity_resistance_available, afe4403_res_table); 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cistatic const struct afe440x_val_table afe4403_cap_table[] = { 1258c2ecf20Sopenharmony_ci { 0, 5000 }, { 0, 10000 }, { 0, 20000 }, { 0, 25000 }, 1268c2ecf20Sopenharmony_ci { 0, 30000 }, { 0, 35000 }, { 0, 45000 }, { 0, 50000 }, 1278c2ecf20Sopenharmony_ci { 0, 55000 }, { 0, 60000 }, { 0, 70000 }, { 0, 75000 }, 1288c2ecf20Sopenharmony_ci { 0, 80000 }, { 0, 85000 }, { 0, 95000 }, { 0, 100000 }, 1298c2ecf20Sopenharmony_ci { 0, 155000 }, { 0, 160000 }, { 0, 170000 }, { 0, 175000 }, 1308c2ecf20Sopenharmony_ci { 0, 180000 }, { 0, 185000 }, { 0, 195000 }, { 0, 200000 }, 1318c2ecf20Sopenharmony_ci { 0, 205000 }, { 0, 210000 }, { 0, 220000 }, { 0, 225000 }, 1328c2ecf20Sopenharmony_ci { 0, 230000 }, { 0, 235000 }, { 0, 245000 }, { 0, 250000 }, 1338c2ecf20Sopenharmony_ci}; 1348c2ecf20Sopenharmony_ciAFE440X_TABLE_ATTR(in_intensity_capacitance_available, afe4403_cap_table); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic ssize_t afe440x_show_register(struct device *dev, 1378c2ecf20Sopenharmony_ci struct device_attribute *attr, 1388c2ecf20Sopenharmony_ci char *buf) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1418c2ecf20Sopenharmony_ci struct afe4403_data *afe = iio_priv(indio_dev); 1428c2ecf20Sopenharmony_ci struct afe440x_attr *afe440x_attr = to_afe440x_attr(attr); 1438c2ecf20Sopenharmony_ci unsigned int reg_val; 1448c2ecf20Sopenharmony_ci int vals[2]; 1458c2ecf20Sopenharmony_ci int ret; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci ret = regmap_field_read(afe->fields[afe440x_attr->field], ®_val); 1488c2ecf20Sopenharmony_ci if (ret) 1498c2ecf20Sopenharmony_ci return ret; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci if (reg_val >= afe440x_attr->table_size) 1528c2ecf20Sopenharmony_ci return -EINVAL; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci vals[0] = afe440x_attr->val_table[reg_val].integer; 1558c2ecf20Sopenharmony_ci vals[1] = afe440x_attr->val_table[reg_val].fract; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci return iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, 2, vals); 1588c2ecf20Sopenharmony_ci} 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_cistatic ssize_t afe440x_store_register(struct device *dev, 1618c2ecf20Sopenharmony_ci struct device_attribute *attr, 1628c2ecf20Sopenharmony_ci const char *buf, size_t count) 1638c2ecf20Sopenharmony_ci{ 1648c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1658c2ecf20Sopenharmony_ci struct afe4403_data *afe = iio_priv(indio_dev); 1668c2ecf20Sopenharmony_ci struct afe440x_attr *afe440x_attr = to_afe440x_attr(attr); 1678c2ecf20Sopenharmony_ci int val, integer, fract, ret; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci ret = iio_str_to_fixpoint(buf, 100000, &integer, &fract); 1708c2ecf20Sopenharmony_ci if (ret) 1718c2ecf20Sopenharmony_ci return ret; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci for (val = 0; val < afe440x_attr->table_size; val++) 1748c2ecf20Sopenharmony_ci if (afe440x_attr->val_table[val].integer == integer && 1758c2ecf20Sopenharmony_ci afe440x_attr->val_table[val].fract == fract) 1768c2ecf20Sopenharmony_ci break; 1778c2ecf20Sopenharmony_ci if (val == afe440x_attr->table_size) 1788c2ecf20Sopenharmony_ci return -EINVAL; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci ret = regmap_field_write(afe->fields[afe440x_attr->field], val); 1818c2ecf20Sopenharmony_ci if (ret) 1828c2ecf20Sopenharmony_ci return ret; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci return count; 1858c2ecf20Sopenharmony_ci} 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_cistatic AFE440X_ATTR(in_intensity1_resistance, F_RF_LED, afe4403_res_table); 1888c2ecf20Sopenharmony_cistatic AFE440X_ATTR(in_intensity1_capacitance, F_CF_LED, afe4403_cap_table); 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cistatic AFE440X_ATTR(in_intensity2_resistance, F_RF_LED, afe4403_res_table); 1918c2ecf20Sopenharmony_cistatic AFE440X_ATTR(in_intensity2_capacitance, F_CF_LED, afe4403_cap_table); 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_cistatic AFE440X_ATTR(in_intensity3_resistance, F_RF_LED1, afe4403_res_table); 1948c2ecf20Sopenharmony_cistatic AFE440X_ATTR(in_intensity3_capacitance, F_CF_LED1, afe4403_cap_table); 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_cistatic AFE440X_ATTR(in_intensity4_resistance, F_RF_LED1, afe4403_res_table); 1978c2ecf20Sopenharmony_cistatic AFE440X_ATTR(in_intensity4_capacitance, F_CF_LED1, afe4403_cap_table); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_cistatic struct attribute *afe440x_attributes[] = { 2008c2ecf20Sopenharmony_ci &dev_attr_in_intensity_resistance_available.attr, 2018c2ecf20Sopenharmony_ci &dev_attr_in_intensity_capacitance_available.attr, 2028c2ecf20Sopenharmony_ci &afe440x_attr_in_intensity1_resistance.dev_attr.attr, 2038c2ecf20Sopenharmony_ci &afe440x_attr_in_intensity1_capacitance.dev_attr.attr, 2048c2ecf20Sopenharmony_ci &afe440x_attr_in_intensity2_resistance.dev_attr.attr, 2058c2ecf20Sopenharmony_ci &afe440x_attr_in_intensity2_capacitance.dev_attr.attr, 2068c2ecf20Sopenharmony_ci &afe440x_attr_in_intensity3_resistance.dev_attr.attr, 2078c2ecf20Sopenharmony_ci &afe440x_attr_in_intensity3_capacitance.dev_attr.attr, 2088c2ecf20Sopenharmony_ci &afe440x_attr_in_intensity4_resistance.dev_attr.attr, 2098c2ecf20Sopenharmony_ci &afe440x_attr_in_intensity4_capacitance.dev_attr.attr, 2108c2ecf20Sopenharmony_ci NULL 2118c2ecf20Sopenharmony_ci}; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_cistatic const struct attribute_group afe440x_attribute_group = { 2148c2ecf20Sopenharmony_ci .attrs = afe440x_attributes 2158c2ecf20Sopenharmony_ci}; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_cistatic int afe4403_read(struct afe4403_data *afe, unsigned int reg, u32 *val) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci u8 tx[4] = {AFE440X_CONTROL0, 0x0, 0x0, AFE440X_CONTROL0_READ}; 2208c2ecf20Sopenharmony_ci u8 rx[3]; 2218c2ecf20Sopenharmony_ci int ret; 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci /* Enable reading from the device */ 2248c2ecf20Sopenharmony_ci ret = spi_write_then_read(afe->spi, tx, 4, NULL, 0); 2258c2ecf20Sopenharmony_ci if (ret) 2268c2ecf20Sopenharmony_ci return ret; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci ret = spi_write_then_read(afe->spi, ®, 1, rx, sizeof(rx)); 2298c2ecf20Sopenharmony_ci if (ret) 2308c2ecf20Sopenharmony_ci return ret; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci *val = get_unaligned_be24(&rx[0]); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci /* Disable reading from the device */ 2358c2ecf20Sopenharmony_ci tx[3] = AFE440X_CONTROL0_WRITE; 2368c2ecf20Sopenharmony_ci ret = spi_write_then_read(afe->spi, tx, 4, NULL, 0); 2378c2ecf20Sopenharmony_ci if (ret) 2388c2ecf20Sopenharmony_ci return ret; 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci return 0; 2418c2ecf20Sopenharmony_ci} 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_cistatic int afe4403_read_raw(struct iio_dev *indio_dev, 2448c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 2458c2ecf20Sopenharmony_ci int *val, int *val2, long mask) 2468c2ecf20Sopenharmony_ci{ 2478c2ecf20Sopenharmony_ci struct afe4403_data *afe = iio_priv(indio_dev); 2488c2ecf20Sopenharmony_ci unsigned int reg, field; 2498c2ecf20Sopenharmony_ci int ret; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci switch (chan->type) { 2528c2ecf20Sopenharmony_ci case IIO_INTENSITY: 2538c2ecf20Sopenharmony_ci switch (mask) { 2548c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 2558c2ecf20Sopenharmony_ci reg = afe4403_channel_values[chan->address]; 2568c2ecf20Sopenharmony_ci ret = afe4403_read(afe, reg, val); 2578c2ecf20Sopenharmony_ci if (ret) 2588c2ecf20Sopenharmony_ci return ret; 2598c2ecf20Sopenharmony_ci return IIO_VAL_INT; 2608c2ecf20Sopenharmony_ci } 2618c2ecf20Sopenharmony_ci break; 2628c2ecf20Sopenharmony_ci case IIO_CURRENT: 2638c2ecf20Sopenharmony_ci switch (mask) { 2648c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 2658c2ecf20Sopenharmony_ci field = afe4403_channel_leds[chan->address]; 2668c2ecf20Sopenharmony_ci ret = regmap_field_read(afe->fields[field], val); 2678c2ecf20Sopenharmony_ci if (ret) 2688c2ecf20Sopenharmony_ci return ret; 2698c2ecf20Sopenharmony_ci return IIO_VAL_INT; 2708c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SCALE: 2718c2ecf20Sopenharmony_ci *val = 0; 2728c2ecf20Sopenharmony_ci *val2 = 800000; 2738c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 2748c2ecf20Sopenharmony_ci } 2758c2ecf20Sopenharmony_ci break; 2768c2ecf20Sopenharmony_ci default: 2778c2ecf20Sopenharmony_ci break; 2788c2ecf20Sopenharmony_ci } 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci return -EINVAL; 2818c2ecf20Sopenharmony_ci} 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_cistatic int afe4403_write_raw(struct iio_dev *indio_dev, 2848c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 2858c2ecf20Sopenharmony_ci int val, int val2, long mask) 2868c2ecf20Sopenharmony_ci{ 2878c2ecf20Sopenharmony_ci struct afe4403_data *afe = iio_priv(indio_dev); 2888c2ecf20Sopenharmony_ci unsigned int field = afe4403_channel_leds[chan->address]; 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci switch (chan->type) { 2918c2ecf20Sopenharmony_ci case IIO_CURRENT: 2928c2ecf20Sopenharmony_ci switch (mask) { 2938c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 2948c2ecf20Sopenharmony_ci return regmap_field_write(afe->fields[field], val); 2958c2ecf20Sopenharmony_ci } 2968c2ecf20Sopenharmony_ci break; 2978c2ecf20Sopenharmony_ci default: 2988c2ecf20Sopenharmony_ci break; 2998c2ecf20Sopenharmony_ci } 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci return -EINVAL; 3028c2ecf20Sopenharmony_ci} 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_cistatic const struct iio_info afe4403_iio_info = { 3058c2ecf20Sopenharmony_ci .attrs = &afe440x_attribute_group, 3068c2ecf20Sopenharmony_ci .read_raw = afe4403_read_raw, 3078c2ecf20Sopenharmony_ci .write_raw = afe4403_write_raw, 3088c2ecf20Sopenharmony_ci}; 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_cistatic irqreturn_t afe4403_trigger_handler(int irq, void *private) 3118c2ecf20Sopenharmony_ci{ 3128c2ecf20Sopenharmony_ci struct iio_poll_func *pf = private; 3138c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = pf->indio_dev; 3148c2ecf20Sopenharmony_ci struct afe4403_data *afe = iio_priv(indio_dev); 3158c2ecf20Sopenharmony_ci int ret, bit, i = 0; 3168c2ecf20Sopenharmony_ci u8 tx[4] = {AFE440X_CONTROL0, 0x0, 0x0, AFE440X_CONTROL0_READ}; 3178c2ecf20Sopenharmony_ci u8 rx[3]; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci /* Enable reading from the device */ 3208c2ecf20Sopenharmony_ci ret = spi_write_then_read(afe->spi, tx, 4, NULL, 0); 3218c2ecf20Sopenharmony_ci if (ret) 3228c2ecf20Sopenharmony_ci goto err; 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci for_each_set_bit(bit, indio_dev->active_scan_mask, 3258c2ecf20Sopenharmony_ci indio_dev->masklength) { 3268c2ecf20Sopenharmony_ci ret = spi_write_then_read(afe->spi, 3278c2ecf20Sopenharmony_ci &afe4403_channel_values[bit], 1, 3288c2ecf20Sopenharmony_ci rx, sizeof(rx)); 3298c2ecf20Sopenharmony_ci if (ret) 3308c2ecf20Sopenharmony_ci goto err; 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci afe->buffer[i++] = get_unaligned_be24(&rx[0]); 3338c2ecf20Sopenharmony_ci } 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci /* Disable reading from the device */ 3368c2ecf20Sopenharmony_ci tx[3] = AFE440X_CONTROL0_WRITE; 3378c2ecf20Sopenharmony_ci ret = spi_write_then_read(afe->spi, tx, 4, NULL, 0); 3388c2ecf20Sopenharmony_ci if (ret) 3398c2ecf20Sopenharmony_ci goto err; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci iio_push_to_buffers_with_timestamp(indio_dev, afe->buffer, 3428c2ecf20Sopenharmony_ci pf->timestamp); 3438c2ecf20Sopenharmony_cierr: 3448c2ecf20Sopenharmony_ci iio_trigger_notify_done(indio_dev->trig); 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci return IRQ_HANDLED; 3478c2ecf20Sopenharmony_ci} 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_cistatic const struct iio_trigger_ops afe4403_trigger_ops = { 3508c2ecf20Sopenharmony_ci}; 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci#define AFE4403_TIMING_PAIRS \ 3538c2ecf20Sopenharmony_ci { AFE440X_LED2STC, 0x000050 }, \ 3548c2ecf20Sopenharmony_ci { AFE440X_LED2ENDC, 0x0003e7 }, \ 3558c2ecf20Sopenharmony_ci { AFE440X_LED1LEDSTC, 0x0007d0 }, \ 3568c2ecf20Sopenharmony_ci { AFE440X_LED1LEDENDC, 0x000bb7 }, \ 3578c2ecf20Sopenharmony_ci { AFE440X_ALED2STC, 0x000438 }, \ 3588c2ecf20Sopenharmony_ci { AFE440X_ALED2ENDC, 0x0007cf }, \ 3598c2ecf20Sopenharmony_ci { AFE440X_LED1STC, 0x000820 }, \ 3608c2ecf20Sopenharmony_ci { AFE440X_LED1ENDC, 0x000bb7 }, \ 3618c2ecf20Sopenharmony_ci { AFE440X_LED2LEDSTC, 0x000000 }, \ 3628c2ecf20Sopenharmony_ci { AFE440X_LED2LEDENDC, 0x0003e7 }, \ 3638c2ecf20Sopenharmony_ci { AFE440X_ALED1STC, 0x000c08 }, \ 3648c2ecf20Sopenharmony_ci { AFE440X_ALED1ENDC, 0x000f9f }, \ 3658c2ecf20Sopenharmony_ci { AFE440X_LED2CONVST, 0x0003ef }, \ 3668c2ecf20Sopenharmony_ci { AFE440X_LED2CONVEND, 0x0007cf }, \ 3678c2ecf20Sopenharmony_ci { AFE440X_ALED2CONVST, 0x0007d7 }, \ 3688c2ecf20Sopenharmony_ci { AFE440X_ALED2CONVEND, 0x000bb7 }, \ 3698c2ecf20Sopenharmony_ci { AFE440X_LED1CONVST, 0x000bbf }, \ 3708c2ecf20Sopenharmony_ci { AFE440X_LED1CONVEND, 0x009c3f }, \ 3718c2ecf20Sopenharmony_ci { AFE440X_ALED1CONVST, 0x000fa7 }, \ 3728c2ecf20Sopenharmony_ci { AFE440X_ALED1CONVEND, 0x001387 }, \ 3738c2ecf20Sopenharmony_ci { AFE440X_ADCRSTSTCT0, 0x0003e8 }, \ 3748c2ecf20Sopenharmony_ci { AFE440X_ADCRSTENDCT0, 0x0003eb }, \ 3758c2ecf20Sopenharmony_ci { AFE440X_ADCRSTSTCT1, 0x0007d0 }, \ 3768c2ecf20Sopenharmony_ci { AFE440X_ADCRSTENDCT1, 0x0007d3 }, \ 3778c2ecf20Sopenharmony_ci { AFE440X_ADCRSTSTCT2, 0x000bb8 }, \ 3788c2ecf20Sopenharmony_ci { AFE440X_ADCRSTENDCT2, 0x000bbb }, \ 3798c2ecf20Sopenharmony_ci { AFE440X_ADCRSTSTCT3, 0x000fa0 }, \ 3808c2ecf20Sopenharmony_ci { AFE440X_ADCRSTENDCT3, 0x000fa3 }, \ 3818c2ecf20Sopenharmony_ci { AFE440X_PRPCOUNT, 0x009c3f }, \ 3828c2ecf20Sopenharmony_ci { AFE440X_PDNCYCLESTC, 0x001518 }, \ 3838c2ecf20Sopenharmony_ci { AFE440X_PDNCYCLEENDC, 0x00991f } 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_cistatic const struct reg_sequence afe4403_reg_sequences[] = { 3868c2ecf20Sopenharmony_ci AFE4403_TIMING_PAIRS, 3878c2ecf20Sopenharmony_ci { AFE440X_CONTROL1, AFE440X_CONTROL1_TIMEREN }, 3888c2ecf20Sopenharmony_ci { AFE4403_TIAGAIN, AFE440X_TIAGAIN_ENSEPGAIN }, 3898c2ecf20Sopenharmony_ci}; 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_cistatic const struct regmap_range afe4403_yes_ranges[] = { 3928c2ecf20Sopenharmony_ci regmap_reg_range(AFE440X_LED2VAL, AFE440X_LED1_ALED1VAL), 3938c2ecf20Sopenharmony_ci}; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_cistatic const struct regmap_access_table afe4403_volatile_table = { 3968c2ecf20Sopenharmony_ci .yes_ranges = afe4403_yes_ranges, 3978c2ecf20Sopenharmony_ci .n_yes_ranges = ARRAY_SIZE(afe4403_yes_ranges), 3988c2ecf20Sopenharmony_ci}; 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_cistatic const struct regmap_config afe4403_regmap_config = { 4018c2ecf20Sopenharmony_ci .reg_bits = 8, 4028c2ecf20Sopenharmony_ci .val_bits = 24, 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci .max_register = AFE440X_PDNCYCLEENDC, 4058c2ecf20Sopenharmony_ci .cache_type = REGCACHE_RBTREE, 4068c2ecf20Sopenharmony_ci .volatile_table = &afe4403_volatile_table, 4078c2ecf20Sopenharmony_ci}; 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_cistatic const struct of_device_id afe4403_of_match[] = { 4108c2ecf20Sopenharmony_ci { .compatible = "ti,afe4403", }, 4118c2ecf20Sopenharmony_ci { /* sentinel */ } 4128c2ecf20Sopenharmony_ci}; 4138c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, afe4403_of_match); 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_cistatic int __maybe_unused afe4403_suspend(struct device *dev) 4168c2ecf20Sopenharmony_ci{ 4178c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = spi_get_drvdata(to_spi_device(dev)); 4188c2ecf20Sopenharmony_ci struct afe4403_data *afe = iio_priv(indio_dev); 4198c2ecf20Sopenharmony_ci int ret; 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci ret = regmap_update_bits(afe->regmap, AFE440X_CONTROL2, 4228c2ecf20Sopenharmony_ci AFE440X_CONTROL2_PDN_AFE, 4238c2ecf20Sopenharmony_ci AFE440X_CONTROL2_PDN_AFE); 4248c2ecf20Sopenharmony_ci if (ret) 4258c2ecf20Sopenharmony_ci return ret; 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci ret = regulator_disable(afe->regulator); 4288c2ecf20Sopenharmony_ci if (ret) { 4298c2ecf20Sopenharmony_ci dev_err(dev, "Unable to disable regulator\n"); 4308c2ecf20Sopenharmony_ci return ret; 4318c2ecf20Sopenharmony_ci } 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci return 0; 4348c2ecf20Sopenharmony_ci} 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_cistatic int __maybe_unused afe4403_resume(struct device *dev) 4378c2ecf20Sopenharmony_ci{ 4388c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = spi_get_drvdata(to_spi_device(dev)); 4398c2ecf20Sopenharmony_ci struct afe4403_data *afe = iio_priv(indio_dev); 4408c2ecf20Sopenharmony_ci int ret; 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci ret = regulator_enable(afe->regulator); 4438c2ecf20Sopenharmony_ci if (ret) { 4448c2ecf20Sopenharmony_ci dev_err(dev, "Unable to enable regulator\n"); 4458c2ecf20Sopenharmony_ci return ret; 4468c2ecf20Sopenharmony_ci } 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ci ret = regmap_update_bits(afe->regmap, AFE440X_CONTROL2, 4498c2ecf20Sopenharmony_ci AFE440X_CONTROL2_PDN_AFE, 0); 4508c2ecf20Sopenharmony_ci if (ret) 4518c2ecf20Sopenharmony_ci return ret; 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci return 0; 4548c2ecf20Sopenharmony_ci} 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(afe4403_pm_ops, afe4403_suspend, afe4403_resume); 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_cistatic int afe4403_probe(struct spi_device *spi) 4598c2ecf20Sopenharmony_ci{ 4608c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 4618c2ecf20Sopenharmony_ci struct afe4403_data *afe; 4628c2ecf20Sopenharmony_ci int i, ret; 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*afe)); 4658c2ecf20Sopenharmony_ci if (!indio_dev) 4668c2ecf20Sopenharmony_ci return -ENOMEM; 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci afe = iio_priv(indio_dev); 4698c2ecf20Sopenharmony_ci spi_set_drvdata(spi, indio_dev); 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci afe->dev = &spi->dev; 4728c2ecf20Sopenharmony_ci afe->spi = spi; 4738c2ecf20Sopenharmony_ci afe->irq = spi->irq; 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci afe->regmap = devm_regmap_init_spi(spi, &afe4403_regmap_config); 4768c2ecf20Sopenharmony_ci if (IS_ERR(afe->regmap)) { 4778c2ecf20Sopenharmony_ci dev_err(afe->dev, "Unable to allocate register map\n"); 4788c2ecf20Sopenharmony_ci return PTR_ERR(afe->regmap); 4798c2ecf20Sopenharmony_ci } 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci for (i = 0; i < F_MAX_FIELDS; i++) { 4828c2ecf20Sopenharmony_ci afe->fields[i] = devm_regmap_field_alloc(afe->dev, afe->regmap, 4838c2ecf20Sopenharmony_ci afe4403_reg_fields[i]); 4848c2ecf20Sopenharmony_ci if (IS_ERR(afe->fields[i])) { 4858c2ecf20Sopenharmony_ci dev_err(afe->dev, "Unable to allocate regmap fields\n"); 4868c2ecf20Sopenharmony_ci return PTR_ERR(afe->fields[i]); 4878c2ecf20Sopenharmony_ci } 4888c2ecf20Sopenharmony_ci } 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci afe->regulator = devm_regulator_get(afe->dev, "tx_sup"); 4918c2ecf20Sopenharmony_ci if (IS_ERR(afe->regulator)) { 4928c2ecf20Sopenharmony_ci dev_err(afe->dev, "Unable to get regulator\n"); 4938c2ecf20Sopenharmony_ci return PTR_ERR(afe->regulator); 4948c2ecf20Sopenharmony_ci } 4958c2ecf20Sopenharmony_ci ret = regulator_enable(afe->regulator); 4968c2ecf20Sopenharmony_ci if (ret) { 4978c2ecf20Sopenharmony_ci dev_err(afe->dev, "Unable to enable regulator\n"); 4988c2ecf20Sopenharmony_ci return ret; 4998c2ecf20Sopenharmony_ci } 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci ret = regmap_write(afe->regmap, AFE440X_CONTROL0, 5028c2ecf20Sopenharmony_ci AFE440X_CONTROL0_SW_RESET); 5038c2ecf20Sopenharmony_ci if (ret) { 5048c2ecf20Sopenharmony_ci dev_err(afe->dev, "Unable to reset device\n"); 5058c2ecf20Sopenharmony_ci goto err_disable_reg; 5068c2ecf20Sopenharmony_ci } 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci ret = regmap_multi_reg_write(afe->regmap, afe4403_reg_sequences, 5098c2ecf20Sopenharmony_ci ARRAY_SIZE(afe4403_reg_sequences)); 5108c2ecf20Sopenharmony_ci if (ret) { 5118c2ecf20Sopenharmony_ci dev_err(afe->dev, "Unable to set register defaults\n"); 5128c2ecf20Sopenharmony_ci goto err_disable_reg; 5138c2ecf20Sopenharmony_ci } 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci indio_dev->modes = INDIO_DIRECT_MODE; 5168c2ecf20Sopenharmony_ci indio_dev->channels = afe4403_channels; 5178c2ecf20Sopenharmony_ci indio_dev->num_channels = ARRAY_SIZE(afe4403_channels); 5188c2ecf20Sopenharmony_ci indio_dev->name = AFE4403_DRIVER_NAME; 5198c2ecf20Sopenharmony_ci indio_dev->info = &afe4403_iio_info; 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci if (afe->irq > 0) { 5228c2ecf20Sopenharmony_ci afe->trig = devm_iio_trigger_alloc(afe->dev, 5238c2ecf20Sopenharmony_ci "%s-dev%d", 5248c2ecf20Sopenharmony_ci indio_dev->name, 5258c2ecf20Sopenharmony_ci indio_dev->id); 5268c2ecf20Sopenharmony_ci if (!afe->trig) { 5278c2ecf20Sopenharmony_ci dev_err(afe->dev, "Unable to allocate IIO trigger\n"); 5288c2ecf20Sopenharmony_ci ret = -ENOMEM; 5298c2ecf20Sopenharmony_ci goto err_disable_reg; 5308c2ecf20Sopenharmony_ci } 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci iio_trigger_set_drvdata(afe->trig, indio_dev); 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_ci afe->trig->ops = &afe4403_trigger_ops; 5358c2ecf20Sopenharmony_ci afe->trig->dev.parent = afe->dev; 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci ret = iio_trigger_register(afe->trig); 5388c2ecf20Sopenharmony_ci if (ret) { 5398c2ecf20Sopenharmony_ci dev_err(afe->dev, "Unable to register IIO trigger\n"); 5408c2ecf20Sopenharmony_ci goto err_disable_reg; 5418c2ecf20Sopenharmony_ci } 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ci ret = devm_request_threaded_irq(afe->dev, afe->irq, 5448c2ecf20Sopenharmony_ci iio_trigger_generic_data_rdy_poll, 5458c2ecf20Sopenharmony_ci NULL, IRQF_ONESHOT, 5468c2ecf20Sopenharmony_ci AFE4403_DRIVER_NAME, 5478c2ecf20Sopenharmony_ci afe->trig); 5488c2ecf20Sopenharmony_ci if (ret) { 5498c2ecf20Sopenharmony_ci dev_err(afe->dev, "Unable to request IRQ\n"); 5508c2ecf20Sopenharmony_ci goto err_trig; 5518c2ecf20Sopenharmony_ci } 5528c2ecf20Sopenharmony_ci } 5538c2ecf20Sopenharmony_ci 5548c2ecf20Sopenharmony_ci ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 5558c2ecf20Sopenharmony_ci afe4403_trigger_handler, NULL); 5568c2ecf20Sopenharmony_ci if (ret) { 5578c2ecf20Sopenharmony_ci dev_err(afe->dev, "Unable to setup buffer\n"); 5588c2ecf20Sopenharmony_ci goto err_trig; 5598c2ecf20Sopenharmony_ci } 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci ret = iio_device_register(indio_dev); 5628c2ecf20Sopenharmony_ci if (ret) { 5638c2ecf20Sopenharmony_ci dev_err(afe->dev, "Unable to register IIO device\n"); 5648c2ecf20Sopenharmony_ci goto err_buff; 5658c2ecf20Sopenharmony_ci } 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_ci return 0; 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_cierr_buff: 5708c2ecf20Sopenharmony_ci iio_triggered_buffer_cleanup(indio_dev); 5718c2ecf20Sopenharmony_cierr_trig: 5728c2ecf20Sopenharmony_ci if (afe->irq > 0) 5738c2ecf20Sopenharmony_ci iio_trigger_unregister(afe->trig); 5748c2ecf20Sopenharmony_cierr_disable_reg: 5758c2ecf20Sopenharmony_ci regulator_disable(afe->regulator); 5768c2ecf20Sopenharmony_ci 5778c2ecf20Sopenharmony_ci return ret; 5788c2ecf20Sopenharmony_ci} 5798c2ecf20Sopenharmony_ci 5808c2ecf20Sopenharmony_cistatic int afe4403_remove(struct spi_device *spi) 5818c2ecf20Sopenharmony_ci{ 5828c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = spi_get_drvdata(spi); 5838c2ecf20Sopenharmony_ci struct afe4403_data *afe = iio_priv(indio_dev); 5848c2ecf20Sopenharmony_ci int ret; 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci iio_device_unregister(indio_dev); 5878c2ecf20Sopenharmony_ci 5888c2ecf20Sopenharmony_ci iio_triggered_buffer_cleanup(indio_dev); 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_ci if (afe->irq > 0) 5918c2ecf20Sopenharmony_ci iio_trigger_unregister(afe->trig); 5928c2ecf20Sopenharmony_ci 5938c2ecf20Sopenharmony_ci ret = regulator_disable(afe->regulator); 5948c2ecf20Sopenharmony_ci if (ret) { 5958c2ecf20Sopenharmony_ci dev_err(afe->dev, "Unable to disable regulator\n"); 5968c2ecf20Sopenharmony_ci return ret; 5978c2ecf20Sopenharmony_ci } 5988c2ecf20Sopenharmony_ci 5998c2ecf20Sopenharmony_ci return 0; 6008c2ecf20Sopenharmony_ci} 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_cistatic const struct spi_device_id afe4403_ids[] = { 6038c2ecf20Sopenharmony_ci { "afe4403", 0 }, 6048c2ecf20Sopenharmony_ci { /* sentinel */ } 6058c2ecf20Sopenharmony_ci}; 6068c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(spi, afe4403_ids); 6078c2ecf20Sopenharmony_ci 6088c2ecf20Sopenharmony_cistatic struct spi_driver afe4403_spi_driver = { 6098c2ecf20Sopenharmony_ci .driver = { 6108c2ecf20Sopenharmony_ci .name = AFE4403_DRIVER_NAME, 6118c2ecf20Sopenharmony_ci .of_match_table = afe4403_of_match, 6128c2ecf20Sopenharmony_ci .pm = &afe4403_pm_ops, 6138c2ecf20Sopenharmony_ci }, 6148c2ecf20Sopenharmony_ci .probe = afe4403_probe, 6158c2ecf20Sopenharmony_ci .remove = afe4403_remove, 6168c2ecf20Sopenharmony_ci .id_table = afe4403_ids, 6178c2ecf20Sopenharmony_ci}; 6188c2ecf20Sopenharmony_cimodule_spi_driver(afe4403_spi_driver); 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andrew F. Davis <afd@ti.com>"); 6218c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TI AFE4403 Heart Rate Monitor and Pulse Oximeter AFE"); 6228c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 623