1 // SPDX-License-Identifier: GPL-2.0-only
3 * Measurements Specialties driver common i2c functions
5 * Copyright (c) 2015 Measurement-Specialties
8 #include <linux/module.h>
9 #include <linux/iio/iio.h>
10 #include <linux/device.h>
11 #include <linux/delay.h>
13 #include "ms_sensors_i2c.h"
15 /* Conversion times in us */
16 static const u16 ms_sensors_ht_t_conversion_time
[] = { 50000, 25000,
18 static const u16 ms_sensors_ht_h_conversion_time
[] = { 16000, 3000,
20 static const u16 ms_sensors_tp_conversion_time
[] = { 500, 1100, 2100,
23 #define MS_SENSORS_SERIAL_READ_MSB 0xFA0F
24 #define MS_SENSORS_SERIAL_READ_LSB 0xFCC9
25 #define MS_SENSORS_CONFIG_REG_WRITE 0xE6
26 #define MS_SENSORS_CONFIG_REG_READ 0xE7
27 #define MS_SENSORS_HT_T_CONVERSION_START 0xF3
28 #define MS_SENSORS_HT_H_CONVERSION_START 0xF5
30 #define MS_SENSORS_TP_PROM_READ 0xA0
31 #define MS_SENSORS_TP_T_CONVERSION_START 0x50
32 #define MS_SENSORS_TP_P_CONVERSION_START 0x40
33 #define MS_SENSORS_TP_ADC_READ 0x00
35 #define MS_SENSORS_NO_READ_CMD 0xFF
38 * ms_sensors_reset() - Reset function
39 * @cli: pointer to device client
40 * @cmd: reset cmd. Depends on device in use
41 * @delay: usleep minimal delay after reset command is issued
43 * Generic I2C reset function for Measurement Specialties devices.
45 * Return: 0 on success, negative errno otherwise.
47 int ms_sensors_reset(void *cli
, u8 cmd
, unsigned int delay
)
50 struct i2c_client
*client
= cli
;
52 ret
= i2c_smbus_write_byte(client
, cmd
);
54 dev_err(&client
->dev
, "Failed to reset device\n");
57 usleep_range(delay
, delay
+ 1000);
61 EXPORT_SYMBOL(ms_sensors_reset
);
64 * ms_sensors_read_prom_word() - PROM word read function
65 * @cli: pointer to device client
66 * @cmd: PROM read cmd. Depends on device and prom id
67 * @word: pointer to word destination value
69 * Generic i2c prom word read function for Measurement Specialties devices.
71 * Return: 0 on success, negative errno otherwise.
73 int ms_sensors_read_prom_word(void *cli
, int cmd
, u16
*word
)
76 struct i2c_client
*client
= cli
;
78 ret
= i2c_smbus_read_word_swapped(client
, cmd
);
80 dev_err(&client
->dev
, "Failed to read prom word\n");
87 EXPORT_SYMBOL(ms_sensors_read_prom_word
);
90 * ms_sensors_convert_and_read() - ADC conversion & read function
91 * @cli: pointer to device client
92 * @conv: ADC conversion command. Depends on device in use
93 * @rd: ADC read command. Depends on device in use
94 * @delay: usleep minimal delay after conversion command is issued
95 * @adc: pointer to ADC destination value
97 * Generic ADC conversion & read function for Measurement Specialties
99 * The function will issue conversion command, sleep appopriate delay, and
100 * issue command to read ADC.
102 * Return: 0 on success, negative errno otherwise.
104 int ms_sensors_convert_and_read(void *cli
, u8 conv
, u8 rd
,
105 unsigned int delay
, u32
*adc
)
109 struct i2c_client
*client
= cli
;
111 /* Trigger conversion */
112 ret
= i2c_smbus_write_byte(client
, conv
);
115 usleep_range(delay
, delay
+ 1000);
117 /* Retrieve ADC value */
118 if (rd
!= MS_SENSORS_NO_READ_CMD
)
119 ret
= i2c_smbus_read_i2c_block_data(client
, rd
, 3, (u8
*)&buf
);
121 ret
= i2c_master_recv(client
, (u8
*)&buf
, 3);
125 dev_dbg(&client
->dev
, "ADC raw value : %x\n", be32_to_cpu(buf
) >> 8);
126 *adc
= be32_to_cpu(buf
) >> 8;
130 dev_err(&client
->dev
, "Unable to make sensor adc conversion\n");
133 EXPORT_SYMBOL(ms_sensors_convert_and_read
);
136 * ms_sensors_crc_valid() - CRC check function
137 * @value: input and CRC compare value
139 * Cyclic Redundancy Check function used in TSYS02D, HTU21, MS8607.
140 * This function performs a x^8 + x^5 + x^4 + 1 polynomial CRC.
141 * The argument contains CRC value in LSB byte while the bytes 1 and 2
142 * are used for CRC computation.
144 * Return: 1 if CRC is valid, 0 otherwise.
146 static bool ms_sensors_crc_valid(u32 value
)
148 u32 polynom
= 0x988000; /* x^8 + x^5 + x^4 + 1 */
151 u32 result
= value
& 0xFFFF00;
152 u8 crc
= value
& 0xFF;
154 while (msb
!= 0x80) {
156 result
= ((result
^ polynom
) & mask
)
163 return result
== crc
;
167 * ms_sensors_read_serial() - Serial number read function
168 * @client: pointer to i2c client
169 * @sn: pointer to 64-bits destination value
171 * Generic i2c serial number read function for Measurement Specialties devices.
172 * This function is used for TSYS02d, HTU21, MS8607 chipset.
173 * Refer to datasheet:
174 * http://www.meas-spec.com/downloads/HTU2X_Serial_Number_Reading.pdf
176 * Sensor raw MSB serial number format is the following :
177 * [ SNB3, CRC, SNB2, CRC, SNB1, CRC, SNB0, CRC]
178 * Sensor raw LSB serial number format is the following :
179 * [ X, X, SNC1, SNC0, CRC, SNA1, SNA0, CRC]
180 * The resulting serial number is following :
181 * [ SNA1, SNA0, SNB3, SNB2, SNB1, SNB0, SNC1, SNC0]
183 * Return: 0 on success, negative errno otherwise.
185 int ms_sensors_read_serial(struct i2c_client
*client
, u64
*sn
)
193 struct i2c_msg msg
[2] = {
195 .addr
= client
->addr
,
196 .flags
= client
->flags
,
198 .buf
= (__u8
*)&send_buf
,
201 .addr
= client
->addr
,
202 .flags
= client
->flags
| I2C_M_RD
,
203 .buf
= (__u8
*)&rcv_buf
,
207 /* Read MSB part of serial number */
208 send_buf
= cpu_to_be16(MS_SENSORS_SERIAL_READ_MSB
);
210 ret
= i2c_transfer(client
->adapter
, msg
, 2);
212 dev_err(&client
->dev
, "Unable to read device serial number");
216 rcv_val
= be64_to_cpu(rcv_buf
);
217 dev_dbg(&client
->dev
, "Serial MSB raw : %llx\n", rcv_val
);
219 for (i
= 0; i
< 64; i
+= 16) {
220 if (!ms_sensors_crc_valid((rcv_val
>> i
) & 0xFFFF))
224 *sn
= (((rcv_val
>> 32) & 0xFF000000) |
225 ((rcv_val
>> 24) & 0x00FF0000) |
226 ((rcv_val
>> 16) & 0x0000FF00) |
227 ((rcv_val
>> 8) & 0x000000FF)) << 16;
229 /* Read LSB part of serial number */
230 send_buf
= cpu_to_be16(MS_SENSORS_SERIAL_READ_LSB
);
233 ret
= i2c_transfer(client
->adapter
, msg
, 2);
235 dev_err(&client
->dev
, "Unable to read device serial number");
239 rcv_val
= be64_to_cpu(rcv_buf
) >> 16;
240 dev_dbg(&client
->dev
, "Serial MSB raw : %llx\n", rcv_val
);
242 for (i
= 0; i
< 48; i
+= 24) {
243 if (!ms_sensors_crc_valid((rcv_val
>> i
) & 0xFFFFFF))
247 *sn
|= (rcv_val
& 0xFFFF00) << 40 | (rcv_val
>> 32);
251 EXPORT_SYMBOL(ms_sensors_read_serial
);
253 static int ms_sensors_read_config_reg(struct i2c_client
*client
,
258 ret
= i2c_smbus_write_byte(client
, MS_SENSORS_CONFIG_REG_READ
);
260 dev_err(&client
->dev
, "Unable to read config register");
264 ret
= i2c_master_recv(client
, config_reg
, 1);
266 dev_err(&client
->dev
, "Unable to read config register");
269 dev_dbg(&client
->dev
, "Config register :%x\n", *config_reg
);
275 * ms_sensors_write_resolution() - Set resolution function
276 * @dev_data: pointer to temperature/humidity device data
277 * @i: resolution index to set
279 * This function will program the appropriate resolution based on the index
280 * provided when user space will set samp_freq channel.
281 * This function is used for TSYS02D, HTU21 and MS8607 chipsets.
283 * Return: 0 on success, negative errno otherwise.
285 ssize_t
ms_sensors_write_resolution(struct ms_ht_dev
*dev_data
,
291 ret
= ms_sensors_read_config_reg(dev_data
->client
, &config_reg
);
296 config_reg
|= ((i
& 1) << 7) + ((i
& 2) >> 1);
298 return i2c_smbus_write_byte_data(dev_data
->client
,
299 MS_SENSORS_CONFIG_REG_WRITE
,
302 EXPORT_SYMBOL(ms_sensors_write_resolution
);
305 * ms_sensors_show_battery_low() - Show device battery low indicator
306 * @dev_data: pointer to temperature/humidity device data
307 * @buf: pointer to char buffer to write result
309 * This function will read battery indicator value in the device and
310 * return 1 if the device voltage is below 2.25V.
311 * This function is used for TSYS02D, HTU21 and MS8607 chipsets.
313 * Return: length of sprintf on success, negative errno otherwise.
315 ssize_t
ms_sensors_show_battery_low(struct ms_ht_dev
*dev_data
,
321 mutex_lock(&dev_data
->lock
);
322 ret
= ms_sensors_read_config_reg(dev_data
->client
, &config_reg
);
323 mutex_unlock(&dev_data
->lock
);
327 return sprintf(buf
, "%d\n", (config_reg
& 0x40) >> 6);
329 EXPORT_SYMBOL(ms_sensors_show_battery_low
);
332 * ms_sensors_show_heater() - Show device heater
333 * @dev_data: pointer to temperature/humidity device data
334 * @buf: pointer to char buffer to write result
336 * This function will read heater enable value in the device and
337 * return 1 if the heater is enabled.
338 * This function is used for HTU21 and MS8607 chipsets.
340 * Return: length of sprintf on success, negative errno otherwise.
342 ssize_t
ms_sensors_show_heater(struct ms_ht_dev
*dev_data
,
348 mutex_lock(&dev_data
->lock
);
349 ret
= ms_sensors_read_config_reg(dev_data
->client
, &config_reg
);
350 mutex_unlock(&dev_data
->lock
);
354 return sprintf(buf
, "%d\n", (config_reg
& 0x4) >> 2);
356 EXPORT_SYMBOL(ms_sensors_show_heater
);
359 * ms_sensors_write_heater() - Write device heater
360 * @dev_data: pointer to temperature/humidity device data
361 * @buf: pointer to char buffer from user space
362 * @len: length of buf
364 * This function will write 1 or 0 value in the device
365 * to enable or disable heater.
366 * This function is used for HTU21 and MS8607 chipsets.
368 * Return: length of buffer, negative errno otherwise.
370 ssize_t
ms_sensors_write_heater(struct ms_ht_dev
*dev_data
,
371 const char *buf
, size_t len
)
376 ret
= kstrtou8(buf
, 10, &val
);
383 mutex_lock(&dev_data
->lock
);
384 ret
= ms_sensors_read_config_reg(dev_data
->client
, &config_reg
);
386 mutex_unlock(&dev_data
->lock
);
391 config_reg
|= val
<< 2;
393 ret
= i2c_smbus_write_byte_data(dev_data
->client
,
394 MS_SENSORS_CONFIG_REG_WRITE
,
396 mutex_unlock(&dev_data
->lock
);
398 dev_err(&dev_data
->client
->dev
, "Unable to write config register\n");
404 EXPORT_SYMBOL(ms_sensors_write_heater
);
407 * ms_sensors_ht_read_temperature() - Read temperature
408 * @dev_data: pointer to temperature/humidity device data
409 * @temperature:pointer to temperature destination value
411 * This function will get temperature ADC value from the device,
412 * check the CRC and compute the temperature value.
413 * This function is used for TSYS02D, HTU21 and MS8607 chipsets.
415 * Return: 0 on success, negative errno otherwise.
417 int ms_sensors_ht_read_temperature(struct ms_ht_dev
*dev_data
,
424 mutex_lock(&dev_data
->lock
);
425 delay
= ms_sensors_ht_t_conversion_time
[dev_data
->res_index
];
426 ret
= ms_sensors_convert_and_read(dev_data
->client
,
427 MS_SENSORS_HT_T_CONVERSION_START
,
428 MS_SENSORS_NO_READ_CMD
,
430 mutex_unlock(&dev_data
->lock
);
434 if (!ms_sensors_crc_valid(adc
)) {
435 dev_err(&dev_data
->client
->dev
,
436 "Temperature read crc check error\n");
440 /* Temperature algorithm */
441 *temperature
= (((s64
)(adc
>> 8) * 175720) >> 16) - 46850;
445 EXPORT_SYMBOL(ms_sensors_ht_read_temperature
);
448 * ms_sensors_ht_read_humidity() - Read humidity
449 * @dev_data: pointer to temperature/humidity device data
450 * @humidity: pointer to humidity destination value
452 * This function will get humidity ADC value from the device,
453 * check the CRC and compute the temperature value.
454 * This function is used for HTU21 and MS8607 chipsets.
456 * Return: 0 on success, negative errno otherwise.
458 int ms_sensors_ht_read_humidity(struct ms_ht_dev
*dev_data
,
465 mutex_lock(&dev_data
->lock
);
466 delay
= ms_sensors_ht_h_conversion_time
[dev_data
->res_index
];
467 ret
= ms_sensors_convert_and_read(dev_data
->client
,
468 MS_SENSORS_HT_H_CONVERSION_START
,
469 MS_SENSORS_NO_READ_CMD
,
471 mutex_unlock(&dev_data
->lock
);
475 if (!ms_sensors_crc_valid(adc
)) {
476 dev_err(&dev_data
->client
->dev
,
477 "Humidity read crc check error\n");
481 /* Humidity algorithm */
482 *humidity
= (((s32
)(adc
>> 8) * 12500) >> 16) * 10 - 6000;
483 if (*humidity
>= 100000)
488 EXPORT_SYMBOL(ms_sensors_ht_read_humidity
);
491 * ms_sensors_tp_crc_valid() - CRC check function for
492 * Temperature and pressure devices.
493 * This function is only used when reading PROM coefficients
495 * @prom: pointer to PROM coefficients array
496 * @len: length of PROM coefficients array
498 * Return: True if CRC is ok.
500 static bool ms_sensors_tp_crc_valid(u16
*prom
, u8 len
)
502 unsigned int cnt
, n_bit
;
503 u16 n_rem
= 0x0000, crc_read
= prom
[0], crc
= (*prom
& 0xF000) >> 12;
506 prom
[0] &= 0x0FFF; /* Clear the CRC computation part */
508 for (cnt
= 0; cnt
< len
* 2; cnt
++) {
510 n_rem
^= prom
[cnt
>> 1] & 0x00FF;
512 n_rem
^= prom
[cnt
>> 1] >> 8;
514 for (n_bit
= 8; n_bit
> 0; n_bit
--) {
516 n_rem
= (n_rem
<< 1) ^ 0x3000;
528 * ms_sensors_tp_read_prom() - prom coeff read function
529 * @dev_data: pointer to temperature/pressure device data
531 * This function will read prom coefficients and check CRC.
532 * This function is used for MS5637 and MS8607 chipsets.
534 * Return: 0 on success, negative errno otherwise.
536 int ms_sensors_tp_read_prom(struct ms_tp_dev
*dev_data
)
540 for (i
= 0; i
< MS_SENSORS_TP_PROM_WORDS_NB
; i
++) {
541 ret
= ms_sensors_read_prom_word(
543 MS_SENSORS_TP_PROM_READ
+ (i
<< 1),
550 if (!ms_sensors_tp_crc_valid(dev_data
->prom
,
551 MS_SENSORS_TP_PROM_WORDS_NB
+ 1)) {
552 dev_err(&dev_data
->client
->dev
,
553 "Calibration coefficients crc check error\n");
559 EXPORT_SYMBOL(ms_sensors_tp_read_prom
);
562 * ms_sensors_read_temp_and_pressure() - read temp and pressure
563 * @dev_data: pointer to temperature/pressure device data
564 * @temperature:pointer to temperature destination value
565 * @pressure: pointer to pressure destination value
567 * This function will read ADC and compute pressure and temperature value.
568 * This function is used for MS5637 and MS8607 chipsets.
570 * Return: 0 on success, negative errno otherwise.
572 int ms_sensors_read_temp_and_pressure(struct ms_tp_dev
*dev_data
,
574 unsigned int *pressure
)
579 s64 off
, sens
, t2
, off2
, sens2
;
580 u16
*prom
= dev_data
->prom
, delay
;
582 mutex_lock(&dev_data
->lock
);
583 delay
= ms_sensors_tp_conversion_time
[dev_data
->res_index
];
585 ret
= ms_sensors_convert_and_read(
587 MS_SENSORS_TP_T_CONVERSION_START
+
588 dev_data
->res_index
* 2,
589 MS_SENSORS_TP_ADC_READ
,
592 mutex_unlock(&dev_data
->lock
);
596 ret
= ms_sensors_convert_and_read(
598 MS_SENSORS_TP_P_CONVERSION_START
+
599 dev_data
->res_index
* 2,
600 MS_SENSORS_TP_ADC_READ
,
602 mutex_unlock(&dev_data
->lock
);
606 dt
= (s32
)t_adc
- (prom
[5] << 8);
608 /* Actual temperature = 2000 + dT * TEMPSENS */
609 temp
= 2000 + (((s64
)dt
* prom
[6]) >> 23);
611 /* Second order temperature compensation */
613 s64 tmp
= (s64
)temp
- 2000;
615 t2
= (3 * ((s64
)dt
* (s64
)dt
)) >> 33;
616 off2
= (61 * tmp
* tmp
) >> 4;
617 sens2
= (29 * tmp
* tmp
) >> 4;
620 s64 tmp
= (s64
)temp
+ 1500;
622 off2
+= 17 * tmp
* tmp
;
623 sens2
+= 9 * tmp
* tmp
;
626 t2
= (5 * ((s64
)dt
* (s64
)dt
)) >> 38;
631 /* OFF = OFF_T1 + TCO * dT */
632 off
= (((s64
)prom
[2]) << 17) + ((((s64
)prom
[4]) * (s64
)dt
) >> 6);
635 /* Sensitivity at actual temperature = SENS_T1 + TCS * dT */
636 sens
= (((s64
)prom
[1]) << 16) + (((s64
)prom
[3] * dt
) >> 7);
639 /* Temperature compensated pressure = D1 * SENS - OFF */
640 *temperature
= (temp
- t2
) * 10;
641 *pressure
= (u32
)(((((s64
)p_adc
* sens
) >> 21) - off
) >> 15);
645 EXPORT_SYMBOL(ms_sensors_read_temp_and_pressure
);
647 MODULE_DESCRIPTION("Measurement-Specialties common i2c driver");
648 MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
649 MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
650 MODULE_LICENSE("GPL v2");