1 // SPDX-License-Identifier: GPL-2.0-only
3 * hp206c.c - HOPERF HP206C precision barometer and altimeter sensor
5 * Copyright (c) 2016, Intel Corporation.
7 * (7-bit I2C slave address 0x76)
10 * http://www.hoperf.com/upload/sensor/HP206C_DataSheet_EN_V2.0.pdf
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
17 #include <linux/delay.h>
18 #include <linux/util_macros.h>
19 #include <linux/acpi.h>
22 #define HP206C_CMD_SOFT_RST 0x06
24 #define HP206C_CMD_ADC_CVT 0x40
26 #define HP206C_CMD_ADC_CVT_OSR_4096 0x00
27 #define HP206C_CMD_ADC_CVT_OSR_2048 0x04
28 #define HP206C_CMD_ADC_CVT_OSR_1024 0x08
29 #define HP206C_CMD_ADC_CVT_OSR_512 0x0c
30 #define HP206C_CMD_ADC_CVT_OSR_256 0x10
31 #define HP206C_CMD_ADC_CVT_OSR_128 0x14
33 #define HP206C_CMD_ADC_CVT_CHNL_PT 0x00
34 #define HP206C_CMD_ADC_CVT_CHNL_T 0x02
36 #define HP206C_CMD_READ_P 0x30
37 #define HP206C_CMD_READ_T 0x32
39 #define HP206C_CMD_READ_REG 0x80
40 #define HP206C_CMD_WRITE_REG 0xc0
42 #define HP206C_REG_INT_EN 0x0b
43 #define HP206C_REG_INT_CFG 0x0c
45 #define HP206C_REG_INT_SRC 0x0d
46 #define HP206C_FLAG_DEV_RDY 0x40
48 #define HP206C_REG_PARA 0x0f
49 #define HP206C_FLAG_CMPS_EN 0x80
51 /* Maximum spin for DEV_RDY */
52 #define HP206C_MAX_DEV_RDY_WAIT_COUNT 20
53 #define HP206C_DEV_RDY_WAIT_US 20000
57 struct i2c_client *client;
62 struct hp206c_osr_setting {
64 unsigned int temp_conv_time_us;
65 unsigned int pres_conv_time_us;
68 /* Data from Table 5 in datasheet. */
69 static const struct hp206c_osr_setting hp206c_osr_settings[] = {
70 { HP206C_CMD_ADC_CVT_OSR_4096, 65600, 131100 },
71 { HP206C_CMD_ADC_CVT_OSR_2048, 32800, 65600 },
72 { HP206C_CMD_ADC_CVT_OSR_1024, 16400, 32800 },
73 { HP206C_CMD_ADC_CVT_OSR_512, 8200, 16400 },
74 { HP206C_CMD_ADC_CVT_OSR_256, 4100, 8200 },
75 { HP206C_CMD_ADC_CVT_OSR_128, 2100, 4100 },
77 static const int hp206c_osr_rates[] = { 4096, 2048, 1024, 512, 256, 128 };
78 static const char hp206c_osr_rates_str[] = "4096 2048 1024 512 256 128";
80 static inline int hp206c_read_reg(struct i2c_client *client, u8 reg)
82 return i2c_smbus_read_byte_data(client, HP206C_CMD_READ_REG | reg);
85 static inline int hp206c_write_reg(struct i2c_client *client, u8 reg, u8 val)
87 return i2c_smbus_write_byte_data(client,
88 HP206C_CMD_WRITE_REG | reg, val);
91 static int hp206c_read_20bit(struct i2c_client *client, u8 cmd)
96 ret = i2c_smbus_read_i2c_block_data(client, cmd, 3, values);
101 return ((values[0] & 0xF) << 16) | (values[1] << 8) | (values[2]);
104 /* Spin for max 160ms until DEV_RDY is 1, or return error. */
105 static int hp206c_wait_dev_rdy(struct iio_dev *indio_dev)
109 struct hp206c_data *data = iio_priv(indio_dev);
110 struct i2c_client *client = data->client;
112 while (++count <= HP206C_MAX_DEV_RDY_WAIT_COUNT) {
113 ret = hp206c_read_reg(client, HP206C_REG_INT_SRC);
115 dev_err(&indio_dev->dev, "Failed READ_REG INT_SRC: %d\n", ret);
118 if (ret & HP206C_FLAG_DEV_RDY)
120 usleep_range(HP206C_DEV_RDY_WAIT_US, HP206C_DEV_RDY_WAIT_US * 3 / 2);
125 static int hp206c_set_compensation(struct i2c_client *client, bool enabled)
129 val = hp206c_read_reg(client, HP206C_REG_PARA);
133 val |= HP206C_FLAG_CMPS_EN;
135 val &= ~HP206C_FLAG_CMPS_EN;
137 return hp206c_write_reg(client, HP206C_REG_PARA, val);
140 /* Do a soft reset */
141 static int hp206c_soft_reset(struct iio_dev *indio_dev)
144 struct hp206c_data *data = iio_priv(indio_dev);
145 struct i2c_client *client = data->client;
147 ret = i2c_smbus_write_byte(client, HP206C_CMD_SOFT_RST);
149 dev_err(&client->dev, "Failed to reset device: %d\n", ret);
153 usleep_range(400, 600);
155 ret = hp206c_wait_dev_rdy(indio_dev);
157 dev_err(&client->dev, "Device not ready after soft reset: %d\n", ret);
161 ret = hp206c_set_compensation(client, true);
163 dev_err(&client->dev, "Failed to enable compensation: %d\n", ret);
167 static int hp206c_conv_and_read(struct iio_dev *indio_dev,
168 u8 conv_cmd, u8 read_cmd,
169 unsigned int sleep_us)
172 struct hp206c_data *data = iio_priv(indio_dev);
173 struct i2c_client *client = data->client;
175 ret = hp206c_wait_dev_rdy(indio_dev);
177 dev_err(&indio_dev->dev, "Device not ready: %d\n", ret);
181 ret = i2c_smbus_write_byte(client, conv_cmd);
183 dev_err(&indio_dev->dev, "Failed convert: %d\n", ret);
187 usleep_range(sleep_us, sleep_us * 3 / 2);
189 ret = hp206c_wait_dev_rdy(indio_dev);
191 dev_err(&indio_dev->dev, "Device not ready: %d\n", ret);
195 ret = hp206c_read_20bit(client, read_cmd);
197 dev_err(&indio_dev->dev, "Failed read: %d\n", ret);
202 static int hp206c_read_raw(struct iio_dev *indio_dev,
203 struct iio_chan_spec const *chan, int *val,
204 int *val2, long mask)
207 struct hp206c_data *data = iio_priv(indio_dev);
208 const struct hp206c_osr_setting *osr_setting;
211 mutex_lock(&data->mutex);
214 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
215 switch (chan->type) {
217 *val = hp206c_osr_rates[data->temp_osr_index];
222 *val = hp206c_osr_rates[data->pres_osr_index];
230 case IIO_CHAN_INFO_RAW:
231 switch (chan->type) {
233 osr_setting = &hp206c_osr_settings[data->temp_osr_index];
234 conv_cmd = HP206C_CMD_ADC_CVT |
235 osr_setting->osr_mask |
236 HP206C_CMD_ADC_CVT_CHNL_T;
237 ret = hp206c_conv_and_read(indio_dev,
240 osr_setting->temp_conv_time_us);
242 /* 20 significant bits are provided.
243 * Extend sign over the rest.
245 *val = sign_extend32(ret, 19);
251 osr_setting = &hp206c_osr_settings[data->pres_osr_index];
252 conv_cmd = HP206C_CMD_ADC_CVT |
253 osr_setting->osr_mask |
254 HP206C_CMD_ADC_CVT_CHNL_PT;
255 ret = hp206c_conv_and_read(indio_dev,
258 osr_setting->pres_conv_time_us);
269 case IIO_CHAN_INFO_SCALE:
270 switch (chan->type) {
274 ret = IIO_VAL_INT_PLUS_MICRO;
280 ret = IIO_VAL_INT_PLUS_MICRO;
291 mutex_unlock(&data->mutex);
295 static int hp206c_write_raw(struct iio_dev *indio_dev,
296 struct iio_chan_spec const *chan,
297 int val, int val2, long mask)
300 struct hp206c_data *data = iio_priv(indio_dev);
302 if (mask != IIO_CHAN_INFO_OVERSAMPLING_RATIO)
304 mutex_lock(&data->mutex);
305 switch (chan->type) {
307 data->temp_osr_index = find_closest_descending(val,
308 hp206c_osr_rates, ARRAY_SIZE(hp206c_osr_rates));
311 data->pres_osr_index = find_closest_descending(val,
312 hp206c_osr_rates, ARRAY_SIZE(hp206c_osr_rates));
317 mutex_unlock(&data->mutex);
321 static const struct iio_chan_spec hp206c_channels[] = {
324 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
325 BIT(IIO_CHAN_INFO_SCALE) |
326 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
329 .type = IIO_PRESSURE,
330 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
331 BIT(IIO_CHAN_INFO_SCALE) |
332 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
336 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(hp206c_osr_rates_str);
338 static struct attribute *hp206c_attributes[] = {
339 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
343 static const struct attribute_group hp206c_attribute_group = {
344 .attrs = hp206c_attributes,
347 static const struct iio_info hp206c_info = {
348 .attrs = &hp206c_attribute_group,
349 .read_raw = hp206c_read_raw,
350 .write_raw = hp206c_write_raw,
353 static int hp206c_probe(struct i2c_client *client,
354 const struct i2c_device_id *id)
356 struct iio_dev *indio_dev;
357 struct hp206c_data *data;
360 if (!i2c_check_functionality(client->adapter,
361 I2C_FUNC_SMBUS_BYTE |
362 I2C_FUNC_SMBUS_BYTE_DATA |
363 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
364 dev_err(&client->dev, "Adapter does not support "
365 "all required i2c functionality\n");
369 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
373 data = iio_priv(indio_dev);
374 data->client = client;
375 mutex_init(&data->mutex);
377 indio_dev->info = &hp206c_info;
378 indio_dev->name = id->name;
379 indio_dev->dev.parent = &client->dev;
380 indio_dev->modes = INDIO_DIRECT_MODE;
381 indio_dev->channels = hp206c_channels;
382 indio_dev->num_channels = ARRAY_SIZE(hp206c_channels);
384 i2c_set_clientdata(client, indio_dev);
386 /* Do a soft reset on probe */
387 ret = hp206c_soft_reset(indio_dev);
389 dev_err(&client->dev, "Failed to reset on startup: %d\n", ret);
393 return devm_iio_device_register(&client->dev, indio_dev);
396 static const struct i2c_device_id hp206c_id[] = {
400 MODULE_DEVICE_TABLE(i2c, hp206c_id);
403 static const struct acpi_device_id hp206c_acpi_match[] = {
407 MODULE_DEVICE_TABLE(acpi, hp206c_acpi_match);
410 static struct i2c_driver hp206c_driver = {
411 .probe = hp206c_probe,
412 .id_table = hp206c_id,
415 .acpi_match_table = ACPI_PTR(hp206c_acpi_match),
419 module_i2c_driver(hp206c_driver);
421 MODULE_DESCRIPTION("HOPERF HP206C precision barometer and altimeter sensor");
422 MODULE_AUTHOR("Leonard Crestez <leonard.crestez@intel.com>");
423 MODULE_LICENSE("GPL v2");