2 * hp206c.c - HOPERF HP206C precision barometer and altimeter sensor
4 * Copyright (c) 2016, Intel Corporation.
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * (7-bit I2C slave address 0x76)
13 * http://www.hoperf.com/upload/sensor/HP206C_DataSheet_EN_V2.0.pdf
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/delay.h>
21 #include <linux/util_macros.h>
22 #include <linux/acpi.h>
25 #define HP206C_CMD_SOFT_RST 0x06
27 #define HP206C_CMD_ADC_CVT 0x40
29 #define HP206C_CMD_ADC_CVT_OSR_4096 0x00
30 #define HP206C_CMD_ADC_CVT_OSR_2048 0x04
31 #define HP206C_CMD_ADC_CVT_OSR_1024 0x08
32 #define HP206C_CMD_ADC_CVT_OSR_512 0x0c
33 #define HP206C_CMD_ADC_CVT_OSR_256 0x10
34 #define HP206C_CMD_ADC_CVT_OSR_128 0x14
36 #define HP206C_CMD_ADC_CVT_CHNL_PT 0x00
37 #define HP206C_CMD_ADC_CVT_CHNL_T 0x02
39 #define HP206C_CMD_READ_P 0x30
40 #define HP206C_CMD_READ_T 0x32
42 #define HP206C_CMD_READ_REG 0x80
43 #define HP206C_CMD_WRITE_REG 0xc0
45 #define HP206C_REG_INT_EN 0x0b
46 #define HP206C_REG_INT_CFG 0x0c
48 #define HP206C_REG_INT_SRC 0x0d
49 #define HP206C_FLAG_DEV_RDY 0x40
51 #define HP206C_REG_PARA 0x0f
52 #define HP206C_FLAG_CMPS_EN 0x80
54 /* Maximum spin for DEV_RDY */
55 #define HP206C_MAX_DEV_RDY_WAIT_COUNT 20
56 #define HP206C_DEV_RDY_WAIT_US 20000
60 struct i2c_client
*client
;
65 struct hp206c_osr_setting
{
67 unsigned int temp_conv_time_us
;
68 unsigned int pres_conv_time_us
;
71 /* Data from Table 5 in datasheet. */
72 static const struct hp206c_osr_setting hp206c_osr_settings
[] = {
73 { HP206C_CMD_ADC_CVT_OSR_4096
, 65600, 131100 },
74 { HP206C_CMD_ADC_CVT_OSR_2048
, 32800, 65600 },
75 { HP206C_CMD_ADC_CVT_OSR_1024
, 16400, 32800 },
76 { HP206C_CMD_ADC_CVT_OSR_512
, 8200, 16400 },
77 { HP206C_CMD_ADC_CVT_OSR_256
, 4100, 8200 },
78 { HP206C_CMD_ADC_CVT_OSR_128
, 2100, 4100 },
80 static const int hp206c_osr_rates
[] = { 4096, 2048, 1024, 512, 256, 128 };
81 static const char hp206c_osr_rates_str
[] = "4096 2048 1024 512 256 128";
83 static inline int hp206c_read_reg(struct i2c_client
*client
, u8 reg
)
85 return i2c_smbus_read_byte_data(client
, HP206C_CMD_READ_REG
| reg
);
88 static inline int hp206c_write_reg(struct i2c_client
*client
, u8 reg
, u8 val
)
90 return i2c_smbus_write_byte_data(client
,
91 HP206C_CMD_WRITE_REG
| reg
, val
);
94 static int hp206c_read_20bit(struct i2c_client
*client
, u8 cmd
)
99 ret
= i2c_smbus_read_i2c_block_data(client
, cmd
, 3, values
);
104 return ((values
[0] & 0xF) << 16) | (values
[1] << 8) | (values
[2]);
107 /* Spin for max 160ms until DEV_RDY is 1, or return error. */
108 static int hp206c_wait_dev_rdy(struct iio_dev
*indio_dev
)
112 struct hp206c_data
*data
= iio_priv(indio_dev
);
113 struct i2c_client
*client
= data
->client
;
115 while (++count
<= HP206C_MAX_DEV_RDY_WAIT_COUNT
) {
116 ret
= hp206c_read_reg(client
, HP206C_REG_INT_SRC
);
118 dev_err(&indio_dev
->dev
, "Failed READ_REG INT_SRC: %d\n", ret
);
121 if (ret
& HP206C_FLAG_DEV_RDY
)
123 usleep_range(HP206C_DEV_RDY_WAIT_US
, HP206C_DEV_RDY_WAIT_US
* 3 / 2);
128 static int hp206c_set_compensation(struct i2c_client
*client
, bool enabled
)
132 val
= hp206c_read_reg(client
, HP206C_REG_PARA
);
136 val
|= HP206C_FLAG_CMPS_EN
;
138 val
&= ~HP206C_FLAG_CMPS_EN
;
140 return hp206c_write_reg(client
, HP206C_REG_PARA
, val
);
143 /* Do a soft reset */
144 static int hp206c_soft_reset(struct iio_dev
*indio_dev
)
147 struct hp206c_data
*data
= iio_priv(indio_dev
);
148 struct i2c_client
*client
= data
->client
;
150 ret
= i2c_smbus_write_byte(client
, HP206C_CMD_SOFT_RST
);
152 dev_err(&client
->dev
, "Failed to reset device: %d\n", ret
);
156 usleep_range(400, 600);
158 ret
= hp206c_wait_dev_rdy(indio_dev
);
160 dev_err(&client
->dev
, "Device not ready after soft reset: %d\n", ret
);
164 ret
= hp206c_set_compensation(client
, true);
166 dev_err(&client
->dev
, "Failed to enable compensation: %d\n", ret
);
170 static int hp206c_conv_and_read(struct iio_dev
*indio_dev
,
171 u8 conv_cmd
, u8 read_cmd
,
172 unsigned int sleep_us
)
175 struct hp206c_data
*data
= iio_priv(indio_dev
);
176 struct i2c_client
*client
= data
->client
;
178 ret
= hp206c_wait_dev_rdy(indio_dev
);
180 dev_err(&indio_dev
->dev
, "Device not ready: %d\n", ret
);
184 ret
= i2c_smbus_write_byte(client
, conv_cmd
);
186 dev_err(&indio_dev
->dev
, "Failed convert: %d\n", ret
);
190 usleep_range(sleep_us
, sleep_us
* 3 / 2);
192 ret
= hp206c_wait_dev_rdy(indio_dev
);
194 dev_err(&indio_dev
->dev
, "Device not ready: %d\n", ret
);
198 ret
= hp206c_read_20bit(client
, read_cmd
);
200 dev_err(&indio_dev
->dev
, "Failed read: %d\n", ret
);
205 static int hp206c_read_raw(struct iio_dev
*indio_dev
,
206 struct iio_chan_spec
const *chan
, int *val
,
207 int *val2
, long mask
)
210 struct hp206c_data
*data
= iio_priv(indio_dev
);
211 const struct hp206c_osr_setting
*osr_setting
;
214 mutex_lock(&data
->mutex
);
217 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
218 switch (chan
->type
) {
220 *val
= hp206c_osr_rates
[data
->temp_osr_index
];
225 *val
= hp206c_osr_rates
[data
->pres_osr_index
];
233 case IIO_CHAN_INFO_RAW
:
234 switch (chan
->type
) {
236 osr_setting
= &hp206c_osr_settings
[data
->temp_osr_index
];
237 conv_cmd
= HP206C_CMD_ADC_CVT
|
238 osr_setting
->osr_mask
|
239 HP206C_CMD_ADC_CVT_CHNL_T
;
240 ret
= hp206c_conv_and_read(indio_dev
,
243 osr_setting
->temp_conv_time_us
);
245 /* 20 significant bits are provided.
246 * Extend sign over the rest.
248 *val
= sign_extend32(ret
, 19);
254 osr_setting
= &hp206c_osr_settings
[data
->pres_osr_index
];
255 conv_cmd
= HP206C_CMD_ADC_CVT
|
256 osr_setting
->osr_mask
|
257 HP206C_CMD_ADC_CVT_CHNL_PT
;
258 ret
= hp206c_conv_and_read(indio_dev
,
261 osr_setting
->pres_conv_time_us
);
272 case IIO_CHAN_INFO_SCALE
:
273 switch (chan
->type
) {
277 ret
= IIO_VAL_INT_PLUS_MICRO
;
283 ret
= IIO_VAL_INT_PLUS_MICRO
;
294 mutex_unlock(&data
->mutex
);
298 static int hp206c_write_raw(struct iio_dev
*indio_dev
,
299 struct iio_chan_spec
const *chan
,
300 int val
, int val2
, long mask
)
303 struct hp206c_data
*data
= iio_priv(indio_dev
);
305 if (mask
!= IIO_CHAN_INFO_OVERSAMPLING_RATIO
)
307 mutex_lock(&data
->mutex
);
308 switch (chan
->type
) {
310 data
->temp_osr_index
= find_closest_descending(val
,
311 hp206c_osr_rates
, ARRAY_SIZE(hp206c_osr_rates
));
314 data
->pres_osr_index
= find_closest_descending(val
,
315 hp206c_osr_rates
, ARRAY_SIZE(hp206c_osr_rates
));
320 mutex_unlock(&data
->mutex
);
324 static const struct iio_chan_spec hp206c_channels
[] = {
327 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
328 BIT(IIO_CHAN_INFO_SCALE
) |
329 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
332 .type
= IIO_PRESSURE
,
333 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
334 BIT(IIO_CHAN_INFO_SCALE
) |
335 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
339 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(hp206c_osr_rates_str
);
341 static struct attribute
*hp206c_attributes
[] = {
342 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
346 static const struct attribute_group hp206c_attribute_group
= {
347 .attrs
= hp206c_attributes
,
350 static const struct iio_info hp206c_info
= {
351 .attrs
= &hp206c_attribute_group
,
352 .read_raw
= hp206c_read_raw
,
353 .write_raw
= hp206c_write_raw
,
354 .driver_module
= THIS_MODULE
,
357 static int hp206c_probe(struct i2c_client
*client
,
358 const struct i2c_device_id
*id
)
360 struct iio_dev
*indio_dev
;
361 struct hp206c_data
*data
;
364 if (!i2c_check_functionality(client
->adapter
,
365 I2C_FUNC_SMBUS_BYTE
|
366 I2C_FUNC_SMBUS_BYTE_DATA
|
367 I2C_FUNC_SMBUS_READ_I2C_BLOCK
)) {
368 dev_err(&client
->dev
, "Adapter does not support "
369 "all required i2c functionality\n");
373 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
377 data
= iio_priv(indio_dev
);
378 data
->client
= client
;
379 mutex_init(&data
->mutex
);
381 indio_dev
->info
= &hp206c_info
;
382 indio_dev
->name
= id
->name
;
383 indio_dev
->dev
.parent
= &client
->dev
;
384 indio_dev
->modes
= INDIO_DIRECT_MODE
;
385 indio_dev
->channels
= hp206c_channels
;
386 indio_dev
->num_channels
= ARRAY_SIZE(hp206c_channels
);
388 i2c_set_clientdata(client
, indio_dev
);
390 /* Do a soft reset on probe */
391 ret
= hp206c_soft_reset(indio_dev
);
393 dev_err(&client
->dev
, "Failed to reset on startup: %d\n", ret
);
397 return devm_iio_device_register(&client
->dev
, indio_dev
);
400 static const struct i2c_device_id hp206c_id
[] = {
404 MODULE_DEVICE_TABLE(i2c
, hp206c_id
);
407 static const struct acpi_device_id hp206c_acpi_match
[] = {
411 MODULE_DEVICE_TABLE(acpi
, hp206c_acpi_match
);
414 static struct i2c_driver hp206c_driver
= {
415 .probe
= hp206c_probe
,
416 .id_table
= hp206c_id
,
419 .acpi_match_table
= ACPI_PTR(hp206c_acpi_match
),
423 module_i2c_driver(hp206c_driver
);
425 MODULE_DESCRIPTION("HOPERF HP206C precision barometer and altimeter sensor");
426 MODULE_AUTHOR("Leonard Crestez <leonard.crestez@intel.com>");
427 MODULE_LICENSE("GPL v2");