136edc939SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2fa4c9c93SCrestez Dan Leonard /* 3fa4c9c93SCrestez Dan Leonard * hp206c.c - HOPERF HP206C precision barometer and altimeter sensor 4fa4c9c93SCrestez Dan Leonard * 5fa4c9c93SCrestez Dan Leonard * Copyright (c) 2016, Intel Corporation. 6fa4c9c93SCrestez Dan Leonard * 7fa4c9c93SCrestez Dan Leonard * (7-bit I2C slave address 0x76) 8fa4c9c93SCrestez Dan Leonard * 9fa4c9c93SCrestez Dan Leonard * Datasheet: 10fa4c9c93SCrestez Dan Leonard * http://www.hoperf.com/upload/sensor/HP206C_DataSheet_EN_V2.0.pdf 11fa4c9c93SCrestez Dan Leonard */ 12fa4c9c93SCrestez Dan Leonard 13fa4c9c93SCrestez Dan Leonard #include <linux/module.h> 14fa4c9c93SCrestez Dan Leonard #include <linux/i2c.h> 15fa4c9c93SCrestez Dan Leonard #include <linux/iio/iio.h> 16fa4c9c93SCrestez Dan Leonard #include <linux/iio/sysfs.h> 17fa4c9c93SCrestez Dan Leonard #include <linux/delay.h> 18fa4c9c93SCrestez Dan Leonard #include <linux/util_macros.h> 19fa4c9c93SCrestez Dan Leonard #include <linux/acpi.h> 20fa4c9c93SCrestez Dan Leonard 21b3ab0adbSAndy Shevchenko #include <asm/unaligned.h> 22b3ab0adbSAndy Shevchenko 23fa4c9c93SCrestez Dan Leonard /* I2C commands: */ 24fa4c9c93SCrestez Dan Leonard #define HP206C_CMD_SOFT_RST 0x06 25fa4c9c93SCrestez Dan Leonard 26fa4c9c93SCrestez Dan Leonard #define HP206C_CMD_ADC_CVT 0x40 27fa4c9c93SCrestez Dan Leonard 28fa4c9c93SCrestez Dan Leonard #define HP206C_CMD_ADC_CVT_OSR_4096 0x00 29fa4c9c93SCrestez Dan Leonard #define HP206C_CMD_ADC_CVT_OSR_2048 0x04 30fa4c9c93SCrestez Dan Leonard #define HP206C_CMD_ADC_CVT_OSR_1024 0x08 31fa4c9c93SCrestez Dan Leonard #define HP206C_CMD_ADC_CVT_OSR_512 0x0c 32fa4c9c93SCrestez Dan Leonard #define HP206C_CMD_ADC_CVT_OSR_256 0x10 33fa4c9c93SCrestez Dan Leonard #define HP206C_CMD_ADC_CVT_OSR_128 0x14 34fa4c9c93SCrestez Dan Leonard 35fa4c9c93SCrestez Dan Leonard #define HP206C_CMD_ADC_CVT_CHNL_PT 0x00 36fa4c9c93SCrestez Dan Leonard #define HP206C_CMD_ADC_CVT_CHNL_T 0x02 37fa4c9c93SCrestez Dan Leonard 38fa4c9c93SCrestez Dan Leonard #define HP206C_CMD_READ_P 0x30 39fa4c9c93SCrestez Dan Leonard #define HP206C_CMD_READ_T 0x32 40fa4c9c93SCrestez Dan Leonard 41fa4c9c93SCrestez Dan Leonard #define HP206C_CMD_READ_REG 0x80 42fa4c9c93SCrestez Dan Leonard #define HP206C_CMD_WRITE_REG 0xc0 43fa4c9c93SCrestez Dan Leonard 44fa4c9c93SCrestez Dan Leonard #define HP206C_REG_INT_EN 0x0b 45fa4c9c93SCrestez Dan Leonard #define HP206C_REG_INT_CFG 0x0c 46fa4c9c93SCrestez Dan Leonard 47fa4c9c93SCrestez Dan Leonard #define HP206C_REG_INT_SRC 0x0d 48fa4c9c93SCrestez Dan Leonard #define HP206C_FLAG_DEV_RDY 0x40 49fa4c9c93SCrestez Dan Leonard 50fa4c9c93SCrestez Dan Leonard #define HP206C_REG_PARA 0x0f 51fa4c9c93SCrestez Dan Leonard #define HP206C_FLAG_CMPS_EN 0x80 52fa4c9c93SCrestez Dan Leonard 53fa4c9c93SCrestez Dan Leonard /* Maximum spin for DEV_RDY */ 54fa4c9c93SCrestez Dan Leonard #define HP206C_MAX_DEV_RDY_WAIT_COUNT 20 55fa4c9c93SCrestez Dan Leonard #define HP206C_DEV_RDY_WAIT_US 20000 56fa4c9c93SCrestez Dan Leonard 57fa4c9c93SCrestez Dan Leonard struct hp206c_data { 58fa4c9c93SCrestez Dan Leonard struct mutex mutex; 59fa4c9c93SCrestez Dan Leonard struct i2c_client *client; 60fa4c9c93SCrestez Dan Leonard int temp_osr_index; 61fa4c9c93SCrestez Dan Leonard int pres_osr_index; 62fa4c9c93SCrestez Dan Leonard }; 63fa4c9c93SCrestez Dan Leonard 64fa4c9c93SCrestez Dan Leonard struct hp206c_osr_setting { 65fa4c9c93SCrestez Dan Leonard u8 osr_mask; 66fa4c9c93SCrestez Dan Leonard unsigned int temp_conv_time_us; 67fa4c9c93SCrestez Dan Leonard unsigned int pres_conv_time_us; 68fa4c9c93SCrestez Dan Leonard }; 69fa4c9c93SCrestez Dan Leonard 70fa4c9c93SCrestez Dan Leonard /* Data from Table 5 in datasheet. */ 71fa4c9c93SCrestez Dan Leonard static const struct hp206c_osr_setting hp206c_osr_settings[] = { 72fa4c9c93SCrestez Dan Leonard { HP206C_CMD_ADC_CVT_OSR_4096, 65600, 131100 }, 73fa4c9c93SCrestez Dan Leonard { HP206C_CMD_ADC_CVT_OSR_2048, 32800, 65600 }, 74fa4c9c93SCrestez Dan Leonard { HP206C_CMD_ADC_CVT_OSR_1024, 16400, 32800 }, 75fa4c9c93SCrestez Dan Leonard { HP206C_CMD_ADC_CVT_OSR_512, 8200, 16400 }, 76fa4c9c93SCrestez Dan Leonard { HP206C_CMD_ADC_CVT_OSR_256, 4100, 8200 }, 77fa4c9c93SCrestez Dan Leonard { HP206C_CMD_ADC_CVT_OSR_128, 2100, 4100 }, 78fa4c9c93SCrestez Dan Leonard }; 79fa4c9c93SCrestez Dan Leonard static const int hp206c_osr_rates[] = { 4096, 2048, 1024, 512, 256, 128 }; 80fa4c9c93SCrestez Dan Leonard static const char hp206c_osr_rates_str[] = "4096 2048 1024 512 256 128"; 81fa4c9c93SCrestez Dan Leonard 82fa4c9c93SCrestez Dan Leonard static inline int hp206c_read_reg(struct i2c_client *client, u8 reg) 83fa4c9c93SCrestez Dan Leonard { 84fa4c9c93SCrestez Dan Leonard return i2c_smbus_read_byte_data(client, HP206C_CMD_READ_REG | reg); 85fa4c9c93SCrestez Dan Leonard } 86fa4c9c93SCrestez Dan Leonard 87fa4c9c93SCrestez Dan Leonard static inline int hp206c_write_reg(struct i2c_client *client, u8 reg, u8 val) 88fa4c9c93SCrestez Dan Leonard { 89fa4c9c93SCrestez Dan Leonard return i2c_smbus_write_byte_data(client, 90fa4c9c93SCrestez Dan Leonard HP206C_CMD_WRITE_REG | reg, val); 91fa4c9c93SCrestez Dan Leonard } 92fa4c9c93SCrestez Dan Leonard 93fa4c9c93SCrestez Dan Leonard static int hp206c_read_20bit(struct i2c_client *client, u8 cmd) 94fa4c9c93SCrestez Dan Leonard { 95fa4c9c93SCrestez Dan Leonard int ret; 96fa4c9c93SCrestez Dan Leonard u8 values[3]; 97fa4c9c93SCrestez Dan Leonard 98b3ab0adbSAndy Shevchenko ret = i2c_smbus_read_i2c_block_data(client, cmd, sizeof(values), values); 99fa4c9c93SCrestez Dan Leonard if (ret < 0) 100fa4c9c93SCrestez Dan Leonard return ret; 101b3ab0adbSAndy Shevchenko if (ret != sizeof(values)) 102fa4c9c93SCrestez Dan Leonard return -EIO; 103b3ab0adbSAndy Shevchenko return get_unaligned_be24(&values[0]) & GENMASK(19, 0); 104fa4c9c93SCrestez Dan Leonard } 105fa4c9c93SCrestez Dan Leonard 106fa4c9c93SCrestez Dan Leonard /* Spin for max 160ms until DEV_RDY is 1, or return error. */ 107fa4c9c93SCrestez Dan Leonard static int hp206c_wait_dev_rdy(struct iio_dev *indio_dev) 108fa4c9c93SCrestez Dan Leonard { 109fa4c9c93SCrestez Dan Leonard int ret; 110fa4c9c93SCrestez Dan Leonard int count = 0; 111fa4c9c93SCrestez Dan Leonard struct hp206c_data *data = iio_priv(indio_dev); 112fa4c9c93SCrestez Dan Leonard struct i2c_client *client = data->client; 113fa4c9c93SCrestez Dan Leonard 114fa4c9c93SCrestez Dan Leonard while (++count <= HP206C_MAX_DEV_RDY_WAIT_COUNT) { 115fa4c9c93SCrestez Dan Leonard ret = hp206c_read_reg(client, HP206C_REG_INT_SRC); 116fa4c9c93SCrestez Dan Leonard if (ret < 0) { 117fa4c9c93SCrestez Dan Leonard dev_err(&indio_dev->dev, "Failed READ_REG INT_SRC: %d\n", ret); 118fa4c9c93SCrestez Dan Leonard return ret; 119fa4c9c93SCrestez Dan Leonard } 120fa4c9c93SCrestez Dan Leonard if (ret & HP206C_FLAG_DEV_RDY) 121fa4c9c93SCrestez Dan Leonard return 0; 122fa4c9c93SCrestez Dan Leonard usleep_range(HP206C_DEV_RDY_WAIT_US, HP206C_DEV_RDY_WAIT_US * 3 / 2); 123fa4c9c93SCrestez Dan Leonard } 124fa4c9c93SCrestez Dan Leonard return -ETIMEDOUT; 125fa4c9c93SCrestez Dan Leonard } 126fa4c9c93SCrestez Dan Leonard 127fa4c9c93SCrestez Dan Leonard static int hp206c_set_compensation(struct i2c_client *client, bool enabled) 128fa4c9c93SCrestez Dan Leonard { 129fa4c9c93SCrestez Dan Leonard int val; 130fa4c9c93SCrestez Dan Leonard 131fa4c9c93SCrestez Dan Leonard val = hp206c_read_reg(client, HP206C_REG_PARA); 132fa4c9c93SCrestez Dan Leonard if (val < 0) 133fa4c9c93SCrestez Dan Leonard return val; 134fa4c9c93SCrestez Dan Leonard if (enabled) 135fa4c9c93SCrestez Dan Leonard val |= HP206C_FLAG_CMPS_EN; 136fa4c9c93SCrestez Dan Leonard else 137fa4c9c93SCrestez Dan Leonard val &= ~HP206C_FLAG_CMPS_EN; 138fa4c9c93SCrestez Dan Leonard 139fa4c9c93SCrestez Dan Leonard return hp206c_write_reg(client, HP206C_REG_PARA, val); 140fa4c9c93SCrestez Dan Leonard } 141fa4c9c93SCrestez Dan Leonard 142fa4c9c93SCrestez Dan Leonard /* Do a soft reset */ 143fa4c9c93SCrestez Dan Leonard static int hp206c_soft_reset(struct iio_dev *indio_dev) 144fa4c9c93SCrestez Dan Leonard { 145fa4c9c93SCrestez Dan Leonard int ret; 146fa4c9c93SCrestez Dan Leonard struct hp206c_data *data = iio_priv(indio_dev); 147fa4c9c93SCrestez Dan Leonard struct i2c_client *client = data->client; 148fa4c9c93SCrestez Dan Leonard 149fa4c9c93SCrestez Dan Leonard ret = i2c_smbus_write_byte(client, HP206C_CMD_SOFT_RST); 150fa4c9c93SCrestez Dan Leonard if (ret) { 151fa4c9c93SCrestez Dan Leonard dev_err(&client->dev, "Failed to reset device: %d\n", ret); 152fa4c9c93SCrestez Dan Leonard return ret; 153fa4c9c93SCrestez Dan Leonard } 154fa4c9c93SCrestez Dan Leonard 155fa4c9c93SCrestez Dan Leonard usleep_range(400, 600); 156fa4c9c93SCrestez Dan Leonard 157fa4c9c93SCrestez Dan Leonard ret = hp206c_wait_dev_rdy(indio_dev); 158fa4c9c93SCrestez Dan Leonard if (ret) { 159fa4c9c93SCrestez Dan Leonard dev_err(&client->dev, "Device not ready after soft reset: %d\n", ret); 160fa4c9c93SCrestez Dan Leonard return ret; 161fa4c9c93SCrestez Dan Leonard } 162fa4c9c93SCrestez Dan Leonard 163fa4c9c93SCrestez Dan Leonard ret = hp206c_set_compensation(client, true); 164fa4c9c93SCrestez Dan Leonard if (ret) 165fa4c9c93SCrestez Dan Leonard dev_err(&client->dev, "Failed to enable compensation: %d\n", ret); 166fa4c9c93SCrestez Dan Leonard return ret; 167fa4c9c93SCrestez Dan Leonard } 168fa4c9c93SCrestez Dan Leonard 169fa4c9c93SCrestez Dan Leonard static int hp206c_conv_and_read(struct iio_dev *indio_dev, 170fa4c9c93SCrestez Dan Leonard u8 conv_cmd, u8 read_cmd, 171fa4c9c93SCrestez Dan Leonard unsigned int sleep_us) 172fa4c9c93SCrestez Dan Leonard { 173fa4c9c93SCrestez Dan Leonard int ret; 174fa4c9c93SCrestez Dan Leonard struct hp206c_data *data = iio_priv(indio_dev); 175fa4c9c93SCrestez Dan Leonard struct i2c_client *client = data->client; 176fa4c9c93SCrestez Dan Leonard 177fa4c9c93SCrestez Dan Leonard ret = hp206c_wait_dev_rdy(indio_dev); 178fa4c9c93SCrestez Dan Leonard if (ret < 0) { 179fa4c9c93SCrestez Dan Leonard dev_err(&indio_dev->dev, "Device not ready: %d\n", ret); 180fa4c9c93SCrestez Dan Leonard return ret; 181fa4c9c93SCrestez Dan Leonard } 182fa4c9c93SCrestez Dan Leonard 183fa4c9c93SCrestez Dan Leonard ret = i2c_smbus_write_byte(client, conv_cmd); 184fa4c9c93SCrestez Dan Leonard if (ret < 0) { 185fa4c9c93SCrestez Dan Leonard dev_err(&indio_dev->dev, "Failed convert: %d\n", ret); 186fa4c9c93SCrestez Dan Leonard return ret; 187fa4c9c93SCrestez Dan Leonard } 188fa4c9c93SCrestez Dan Leonard 189fa4c9c93SCrestez Dan Leonard usleep_range(sleep_us, sleep_us * 3 / 2); 190fa4c9c93SCrestez Dan Leonard 191fa4c9c93SCrestez Dan Leonard ret = hp206c_wait_dev_rdy(indio_dev); 192fa4c9c93SCrestez Dan Leonard if (ret < 0) { 193fa4c9c93SCrestez Dan Leonard dev_err(&indio_dev->dev, "Device not ready: %d\n", ret); 194fa4c9c93SCrestez Dan Leonard return ret; 195fa4c9c93SCrestez Dan Leonard } 196fa4c9c93SCrestez Dan Leonard 197fa4c9c93SCrestez Dan Leonard ret = hp206c_read_20bit(client, read_cmd); 198fa4c9c93SCrestez Dan Leonard if (ret < 0) 199fa4c9c93SCrestez Dan Leonard dev_err(&indio_dev->dev, "Failed read: %d\n", ret); 200fa4c9c93SCrestez Dan Leonard 201fa4c9c93SCrestez Dan Leonard return ret; 202fa4c9c93SCrestez Dan Leonard } 203fa4c9c93SCrestez Dan Leonard 204fa4c9c93SCrestez Dan Leonard static int hp206c_read_raw(struct iio_dev *indio_dev, 205fa4c9c93SCrestez Dan Leonard struct iio_chan_spec const *chan, int *val, 206fa4c9c93SCrestez Dan Leonard int *val2, long mask) 207fa4c9c93SCrestez Dan Leonard { 208fa4c9c93SCrestez Dan Leonard int ret; 209fa4c9c93SCrestez Dan Leonard struct hp206c_data *data = iio_priv(indio_dev); 210fa4c9c93SCrestez Dan Leonard const struct hp206c_osr_setting *osr_setting; 211fa4c9c93SCrestez Dan Leonard u8 conv_cmd; 212fa4c9c93SCrestez Dan Leonard 213fa4c9c93SCrestez Dan Leonard mutex_lock(&data->mutex); 214fa4c9c93SCrestez Dan Leonard 215fa4c9c93SCrestez Dan Leonard switch (mask) { 216fa4c9c93SCrestez Dan Leonard case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 217fa4c9c93SCrestez Dan Leonard switch (chan->type) { 218fa4c9c93SCrestez Dan Leonard case IIO_TEMP: 219fa4c9c93SCrestez Dan Leonard *val = hp206c_osr_rates[data->temp_osr_index]; 220fa4c9c93SCrestez Dan Leonard ret = IIO_VAL_INT; 221fa4c9c93SCrestez Dan Leonard break; 222fa4c9c93SCrestez Dan Leonard 223fa4c9c93SCrestez Dan Leonard case IIO_PRESSURE: 224fa4c9c93SCrestez Dan Leonard *val = hp206c_osr_rates[data->pres_osr_index]; 225fa4c9c93SCrestez Dan Leonard ret = IIO_VAL_INT; 226fa4c9c93SCrestez Dan Leonard break; 227fa4c9c93SCrestez Dan Leonard default: 228fa4c9c93SCrestez Dan Leonard ret = -EINVAL; 229fa4c9c93SCrestez Dan Leonard } 230fa4c9c93SCrestez Dan Leonard break; 231fa4c9c93SCrestez Dan Leonard 232fa4c9c93SCrestez Dan Leonard case IIO_CHAN_INFO_RAW: 233fa4c9c93SCrestez Dan Leonard switch (chan->type) { 234fa4c9c93SCrestez Dan Leonard case IIO_TEMP: 235fa4c9c93SCrestez Dan Leonard osr_setting = &hp206c_osr_settings[data->temp_osr_index]; 236fa4c9c93SCrestez Dan Leonard conv_cmd = HP206C_CMD_ADC_CVT | 237fa4c9c93SCrestez Dan Leonard osr_setting->osr_mask | 238fa4c9c93SCrestez Dan Leonard HP206C_CMD_ADC_CVT_CHNL_T; 239fa4c9c93SCrestez Dan Leonard ret = hp206c_conv_and_read(indio_dev, 240fa4c9c93SCrestez Dan Leonard conv_cmd, 241fa4c9c93SCrestez Dan Leonard HP206C_CMD_READ_T, 242fa4c9c93SCrestez Dan Leonard osr_setting->temp_conv_time_us); 243fa4c9c93SCrestez Dan Leonard if (ret >= 0) { 244fa4c9c93SCrestez Dan Leonard /* 20 significant bits are provided. 245fa4c9c93SCrestez Dan Leonard * Extend sign over the rest. 246fa4c9c93SCrestez Dan Leonard */ 247fa4c9c93SCrestez Dan Leonard *val = sign_extend32(ret, 19); 248fa4c9c93SCrestez Dan Leonard ret = IIO_VAL_INT; 249fa4c9c93SCrestez Dan Leonard } 250fa4c9c93SCrestez Dan Leonard break; 251fa4c9c93SCrestez Dan Leonard 252fa4c9c93SCrestez Dan Leonard case IIO_PRESSURE: 253fa4c9c93SCrestez Dan Leonard osr_setting = &hp206c_osr_settings[data->pres_osr_index]; 254fa4c9c93SCrestez Dan Leonard conv_cmd = HP206C_CMD_ADC_CVT | 255fa4c9c93SCrestez Dan Leonard osr_setting->osr_mask | 256fa4c9c93SCrestez Dan Leonard HP206C_CMD_ADC_CVT_CHNL_PT; 257fa4c9c93SCrestez Dan Leonard ret = hp206c_conv_and_read(indio_dev, 258fa4c9c93SCrestez Dan Leonard conv_cmd, 259fa4c9c93SCrestez Dan Leonard HP206C_CMD_READ_P, 260fa4c9c93SCrestez Dan Leonard osr_setting->pres_conv_time_us); 261fa4c9c93SCrestez Dan Leonard if (ret >= 0) { 262fa4c9c93SCrestez Dan Leonard *val = ret; 263fa4c9c93SCrestez Dan Leonard ret = IIO_VAL_INT; 264fa4c9c93SCrestez Dan Leonard } 265fa4c9c93SCrestez Dan Leonard break; 266fa4c9c93SCrestez Dan Leonard default: 267fa4c9c93SCrestez Dan Leonard ret = -EINVAL; 268fa4c9c93SCrestez Dan Leonard } 269fa4c9c93SCrestez Dan Leonard break; 270fa4c9c93SCrestez Dan Leonard 271fa4c9c93SCrestez Dan Leonard case IIO_CHAN_INFO_SCALE: 272fa4c9c93SCrestez Dan Leonard switch (chan->type) { 273fa4c9c93SCrestez Dan Leonard case IIO_TEMP: 274fa4c9c93SCrestez Dan Leonard *val = 0; 275fa4c9c93SCrestez Dan Leonard *val2 = 10000; 276fa4c9c93SCrestez Dan Leonard ret = IIO_VAL_INT_PLUS_MICRO; 277fa4c9c93SCrestez Dan Leonard break; 278fa4c9c93SCrestez Dan Leonard 279fa4c9c93SCrestez Dan Leonard case IIO_PRESSURE: 280fa4c9c93SCrestez Dan Leonard *val = 0; 281fa4c9c93SCrestez Dan Leonard *val2 = 1000; 282fa4c9c93SCrestez Dan Leonard ret = IIO_VAL_INT_PLUS_MICRO; 283fa4c9c93SCrestez Dan Leonard break; 284fa4c9c93SCrestez Dan Leonard default: 285fa4c9c93SCrestez Dan Leonard ret = -EINVAL; 286fa4c9c93SCrestez Dan Leonard } 287fa4c9c93SCrestez Dan Leonard break; 288fa4c9c93SCrestez Dan Leonard 289fa4c9c93SCrestez Dan Leonard default: 290fa4c9c93SCrestez Dan Leonard ret = -EINVAL; 291fa4c9c93SCrestez Dan Leonard } 292fa4c9c93SCrestez Dan Leonard 293fa4c9c93SCrestez Dan Leonard mutex_unlock(&data->mutex); 294fa4c9c93SCrestez Dan Leonard return ret; 295fa4c9c93SCrestez Dan Leonard } 296fa4c9c93SCrestez Dan Leonard 297fa4c9c93SCrestez Dan Leonard static int hp206c_write_raw(struct iio_dev *indio_dev, 298fa4c9c93SCrestez Dan Leonard struct iio_chan_spec const *chan, 299fa4c9c93SCrestez Dan Leonard int val, int val2, long mask) 300fa4c9c93SCrestez Dan Leonard { 301fa4c9c93SCrestez Dan Leonard int ret = 0; 302fa4c9c93SCrestez Dan Leonard struct hp206c_data *data = iio_priv(indio_dev); 303fa4c9c93SCrestez Dan Leonard 304fa4c9c93SCrestez Dan Leonard if (mask != IIO_CHAN_INFO_OVERSAMPLING_RATIO) 305fa4c9c93SCrestez Dan Leonard return -EINVAL; 306fa4c9c93SCrestez Dan Leonard mutex_lock(&data->mutex); 307fa4c9c93SCrestez Dan Leonard switch (chan->type) { 308fa4c9c93SCrestez Dan Leonard case IIO_TEMP: 309fa4c9c93SCrestez Dan Leonard data->temp_osr_index = find_closest_descending(val, 310fa4c9c93SCrestez Dan Leonard hp206c_osr_rates, ARRAY_SIZE(hp206c_osr_rates)); 311fa4c9c93SCrestez Dan Leonard break; 312fa4c9c93SCrestez Dan Leonard case IIO_PRESSURE: 313fa4c9c93SCrestez Dan Leonard data->pres_osr_index = find_closest_descending(val, 314fa4c9c93SCrestez Dan Leonard hp206c_osr_rates, ARRAY_SIZE(hp206c_osr_rates)); 315fa4c9c93SCrestez Dan Leonard break; 316fa4c9c93SCrestez Dan Leonard default: 317fa4c9c93SCrestez Dan Leonard ret = -EINVAL; 318fa4c9c93SCrestez Dan Leonard } 319fa4c9c93SCrestez Dan Leonard mutex_unlock(&data->mutex); 320fa4c9c93SCrestez Dan Leonard return ret; 321fa4c9c93SCrestez Dan Leonard } 322fa4c9c93SCrestez Dan Leonard 323fa4c9c93SCrestez Dan Leonard static const struct iio_chan_spec hp206c_channels[] = { 324fa4c9c93SCrestez Dan Leonard { 325fa4c9c93SCrestez Dan Leonard .type = IIO_TEMP, 326fa4c9c93SCrestez Dan Leonard .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 327fa4c9c93SCrestez Dan Leonard BIT(IIO_CHAN_INFO_SCALE) | 328fa4c9c93SCrestez Dan Leonard BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 329fa4c9c93SCrestez Dan Leonard }, 330fa4c9c93SCrestez Dan Leonard { 331fa4c9c93SCrestez Dan Leonard .type = IIO_PRESSURE, 332fa4c9c93SCrestez Dan Leonard .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 333fa4c9c93SCrestez Dan Leonard BIT(IIO_CHAN_INFO_SCALE) | 334fa4c9c93SCrestez Dan Leonard BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 335fa4c9c93SCrestez Dan Leonard } 336fa4c9c93SCrestez Dan Leonard }; 337fa4c9c93SCrestez Dan Leonard 338fa4c9c93SCrestez Dan Leonard static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(hp206c_osr_rates_str); 339fa4c9c93SCrestez Dan Leonard 340fa4c9c93SCrestez Dan Leonard static struct attribute *hp206c_attributes[] = { 341fa4c9c93SCrestez Dan Leonard &iio_const_attr_sampling_frequency_available.dev_attr.attr, 342fa4c9c93SCrestez Dan Leonard NULL, 343fa4c9c93SCrestez Dan Leonard }; 344fa4c9c93SCrestez Dan Leonard 345fa4c9c93SCrestez Dan Leonard static const struct attribute_group hp206c_attribute_group = { 346fa4c9c93SCrestez Dan Leonard .attrs = hp206c_attributes, 347fa4c9c93SCrestez Dan Leonard }; 348fa4c9c93SCrestez Dan Leonard 349fa4c9c93SCrestez Dan Leonard static const struct iio_info hp206c_info = { 350fa4c9c93SCrestez Dan Leonard .attrs = &hp206c_attribute_group, 351fa4c9c93SCrestez Dan Leonard .read_raw = hp206c_read_raw, 352fa4c9c93SCrestez Dan Leonard .write_raw = hp206c_write_raw, 353fa4c9c93SCrestez Dan Leonard }; 354fa4c9c93SCrestez Dan Leonard 355*240c69e0SUwe Kleine-König static int hp206c_probe(struct i2c_client *client) 356fa4c9c93SCrestez Dan Leonard { 357*240c69e0SUwe Kleine-König const struct i2c_device_id *id = i2c_client_get_device_id(client); 358fa4c9c93SCrestez Dan Leonard struct iio_dev *indio_dev; 359fa4c9c93SCrestez Dan Leonard struct hp206c_data *data; 360fa4c9c93SCrestez Dan Leonard int ret; 361fa4c9c93SCrestez Dan Leonard 362fa4c9c93SCrestez Dan Leonard if (!i2c_check_functionality(client->adapter, 363fa4c9c93SCrestez Dan Leonard I2C_FUNC_SMBUS_BYTE | 364fa4c9c93SCrestez Dan Leonard I2C_FUNC_SMBUS_BYTE_DATA | 365fa4c9c93SCrestez Dan Leonard I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { 366fa4c9c93SCrestez Dan Leonard dev_err(&client->dev, "Adapter does not support " 367fa4c9c93SCrestez Dan Leonard "all required i2c functionality\n"); 368fa4c9c93SCrestez Dan Leonard return -ENODEV; 369fa4c9c93SCrestez Dan Leonard } 370fa4c9c93SCrestez Dan Leonard 371fa4c9c93SCrestez Dan Leonard indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 372fa4c9c93SCrestez Dan Leonard if (!indio_dev) 373fa4c9c93SCrestez Dan Leonard return -ENOMEM; 374fa4c9c93SCrestez Dan Leonard 375fa4c9c93SCrestez Dan Leonard data = iio_priv(indio_dev); 376fa4c9c93SCrestez Dan Leonard data->client = client; 377fa4c9c93SCrestez Dan Leonard mutex_init(&data->mutex); 378fa4c9c93SCrestez Dan Leonard 379fa4c9c93SCrestez Dan Leonard indio_dev->info = &hp206c_info; 380fa4c9c93SCrestez Dan Leonard indio_dev->name = id->name; 381fa4c9c93SCrestez Dan Leonard indio_dev->modes = INDIO_DIRECT_MODE; 382fa4c9c93SCrestez Dan Leonard indio_dev->channels = hp206c_channels; 383fa4c9c93SCrestez Dan Leonard indio_dev->num_channels = ARRAY_SIZE(hp206c_channels); 384fa4c9c93SCrestez Dan Leonard 385fa4c9c93SCrestez Dan Leonard i2c_set_clientdata(client, indio_dev); 386fa4c9c93SCrestez Dan Leonard 387fa4c9c93SCrestez Dan Leonard /* Do a soft reset on probe */ 388fa4c9c93SCrestez Dan Leonard ret = hp206c_soft_reset(indio_dev); 389fa4c9c93SCrestez Dan Leonard if (ret) { 390fa4c9c93SCrestez Dan Leonard dev_err(&client->dev, "Failed to reset on startup: %d\n", ret); 391fa4c9c93SCrestez Dan Leonard return -ENODEV; 392fa4c9c93SCrestez Dan Leonard } 393fa4c9c93SCrestez Dan Leonard 394fa4c9c93SCrestez Dan Leonard return devm_iio_device_register(&client->dev, indio_dev); 395fa4c9c93SCrestez Dan Leonard } 396fa4c9c93SCrestez Dan Leonard 397fa4c9c93SCrestez Dan Leonard static const struct i2c_device_id hp206c_id[] = { 398fa4c9c93SCrestez Dan Leonard {"hp206c"}, 399fa4c9c93SCrestez Dan Leonard {} 400fa4c9c93SCrestez Dan Leonard }; 401cdd469adSJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, hp206c_id); 402fa4c9c93SCrestez Dan Leonard 403fa4c9c93SCrestez Dan Leonard #ifdef CONFIG_ACPI 404fa4c9c93SCrestez Dan Leonard static const struct acpi_device_id hp206c_acpi_match[] = { 405fa4c9c93SCrestez Dan Leonard {"HOP206C", 0}, 406fa4c9c93SCrestez Dan Leonard { }, 407fa4c9c93SCrestez Dan Leonard }; 408fa4c9c93SCrestez Dan Leonard MODULE_DEVICE_TABLE(acpi, hp206c_acpi_match); 409fa4c9c93SCrestez Dan Leonard #endif 410fa4c9c93SCrestez Dan Leonard 411fa4c9c93SCrestez Dan Leonard static struct i2c_driver hp206c_driver = { 412*240c69e0SUwe Kleine-König .probe_new = hp206c_probe, 413fa4c9c93SCrestez Dan Leonard .id_table = hp206c_id, 414fa4c9c93SCrestez Dan Leonard .driver = { 415fa4c9c93SCrestez Dan Leonard .name = "hp206c", 416fa4c9c93SCrestez Dan Leonard .acpi_match_table = ACPI_PTR(hp206c_acpi_match), 417fa4c9c93SCrestez Dan Leonard }, 418fa4c9c93SCrestez Dan Leonard }; 419fa4c9c93SCrestez Dan Leonard 420fa4c9c93SCrestez Dan Leonard module_i2c_driver(hp206c_driver); 421fa4c9c93SCrestez Dan Leonard 422fa4c9c93SCrestez Dan Leonard MODULE_DESCRIPTION("HOPERF HP206C precision barometer and altimeter sensor"); 423fa4c9c93SCrestez Dan Leonard MODULE_AUTHOR("Leonard Crestez <leonard.crestez@intel.com>"); 424fa4c9c93SCrestez Dan Leonard MODULE_LICENSE("GPL v2"); 425