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