2 * Measurements Specialties driver common i2c functions
4 * Copyright (c) 2015 Measurement-Specialties
6 * Licensed under the GPL-2.
9 #include <linux/module.h>
10 #include <linux/iio/iio.h>
11 #include <linux/device.h>
12 #include <linux/delay.h>
14 #include "ms_sensors_i2c.h"
16 /* Conversion times in us */
17 static const u16 ms_sensors_ht_t_conversion_time[] = { 50000, 25000,
19 static const u16 ms_sensors_ht_h_conversion_time[] = { 16000, 3000,
21 static const u16 ms_sensors_tp_conversion_time[] = { 500, 1100, 2100,
24 #define MS_SENSORS_SERIAL_READ_MSB 0xFA0F
25 #define MS_SENSORS_SERIAL_READ_LSB 0xFCC9
26 #define MS_SENSORS_CONFIG_REG_WRITE 0xE6
27 #define MS_SENSORS_CONFIG_REG_READ 0xE7
28 #define MS_SENSORS_HT_T_CONVERSION_START 0xF3
29 #define MS_SENSORS_HT_H_CONVERSION_START 0xF5
31 #define MS_SENSORS_TP_PROM_READ 0xA0
32 #define MS_SENSORS_TP_T_CONVERSION_START 0x50
33 #define MS_SENSORS_TP_P_CONVERSION_START 0x40
34 #define MS_SENSORS_TP_ADC_READ 0x00
36 #define MS_SENSORS_NO_READ_CMD 0xFF
39 * ms_sensors_reset() - Reset function
40 * @cli: pointer to device client
41 * @cmd: reset cmd. Depends on device in use
42 * @delay: usleep minimal delay after reset command is issued
44 * Generic I2C reset function for Measurement Specialties devices.
46 * Return: 0 on success, negative errno otherwise.
48 int ms_sensors_reset(void *cli, u8 cmd, unsigned int delay)
51 struct i2c_client *client = cli;
53 ret = i2c_smbus_write_byte(client, cmd);
55 dev_err(&client->dev, "Failed to reset device\n");
58 usleep_range(delay, delay + 1000);
62 EXPORT_SYMBOL(ms_sensors_reset);
65 * ms_sensors_read_prom_word() - PROM word read function
66 * @cli: pointer to device client
67 * @cmd: PROM read cmd. Depends on device and prom id
68 * @word: pointer to word destination value
70 * Generic i2c prom word read function for Measurement Specialties devices.
72 * Return: 0 on success, negative errno otherwise.
74 int ms_sensors_read_prom_word(void *cli, int cmd, u16 *word)
77 struct i2c_client *client = cli;
79 ret = i2c_smbus_read_word_swapped(client, cmd);
81 dev_err(&client->dev, "Failed to read prom word\n");
88 EXPORT_SYMBOL(ms_sensors_read_prom_word);
91 * ms_sensors_convert_and_read() - ADC conversion & read function
92 * @cli: pointer to device client
93 * @conv: ADC conversion command. Depends on device in use
94 * @rd: ADC read command. Depends on device in use
95 * @delay: usleep minimal delay after conversion command is issued
96 * @adc: pointer to ADC destination value
98 * Generic ADC conversion & read function for Measurement Specialties
100 * The function will issue conversion command, sleep appopriate delay, and
101 * issue command to read ADC.
103 * Return: 0 on success, negative errno otherwise.
105 int ms_sensors_convert_and_read(void *cli, u8 conv, u8 rd,
106 unsigned int delay, u32 *adc)
110 struct i2c_client *client = cli;
112 /* Trigger conversion */
113 ret = i2c_smbus_write_byte(client, conv);
116 usleep_range(delay, delay + 1000);
118 /* Retrieve ADC value */
119 if (rd != MS_SENSORS_NO_READ_CMD)
120 ret = i2c_smbus_read_i2c_block_data(client, rd, 3, (u8 *)&buf);
122 ret = i2c_master_recv(client, (u8 *)&buf, 3);
126 dev_dbg(&client->dev, "ADC raw value : %x\n", be32_to_cpu(buf) >> 8);
127 *adc = be32_to_cpu(buf) >> 8;
131 dev_err(&client->dev, "Unable to make sensor adc conversion\n");
134 EXPORT_SYMBOL(ms_sensors_convert_and_read);
137 * ms_sensors_crc_valid() - CRC check function
138 * @value: input and CRC compare value
140 * Cyclic Redundancy Check function used in TSYS02D, HTU21, MS8607.
141 * This function performs a x^8 + x^5 + x^4 + 1 polynomial CRC.
142 * The argument contains CRC value in LSB byte while the bytes 1 and 2
143 * are used for CRC computation.
145 * Return: 1 if CRC is valid, 0 otherwise.
147 static bool ms_sensors_crc_valid(u32 value)
149 u32 polynom = 0x988000; /* x^8 + x^5 + x^4 + 1 */
152 u32 result = value & 0xFFFF00;
153 u8 crc = value & 0xFF;
155 while (msb != 0x80) {
157 result = ((result ^ polynom) & mask)
164 return result == crc;
168 * ms_sensors_read_serial() - Serial number read function
169 * @cli: pointer to i2c client
170 * @sn: pointer to 64-bits destination value
172 * Generic i2c serial number read function for Measurement Specialties devices.
173 * This function is used for TSYS02d, HTU21, MS8607 chipset.
174 * Refer to datasheet:
175 * http://www.meas-spec.com/downloads/HTU2X_Serial_Number_Reading.pdf
177 * Sensor raw MSB serial number format is the following :
178 * [ SNB3, CRC, SNB2, CRC, SNB1, CRC, SNB0, CRC]
179 * Sensor raw LSB serial number format is the following :
180 * [ X, X, SNC1, SNC0, CRC, SNA1, SNA0, CRC]
181 * The resulting serial number is following :
182 * [ SNA1, SNA0, SNB3, SNB2, SNB1, SNB0, SNC1, SNC0]
184 * Return: 0 on success, negative errno otherwise.
186 int ms_sensors_read_serial(struct i2c_client *client, u64 *sn)
194 struct i2c_msg msg[2] = {
196 .addr = client->addr,
197 .flags = client->flags,
199 .buf = (__u8 *)&send_buf,
202 .addr = client->addr,
203 .flags = client->flags | I2C_M_RD,
204 .buf = (__u8 *)&rcv_buf,
208 /* Read MSB part of serial number */
209 send_buf = cpu_to_be16(MS_SENSORS_SERIAL_READ_MSB);
211 ret = i2c_transfer(client->adapter, msg, 2);
213 dev_err(&client->dev, "Unable to read device serial number");
217 rcv_val = be64_to_cpu(rcv_buf);
218 dev_dbg(&client->dev, "Serial MSB raw : %llx\n", rcv_val);
220 for (i = 0; i < 64; i += 16) {
221 if (!ms_sensors_crc_valid((rcv_val >> i) & 0xFFFF))
225 *sn = (((rcv_val >> 32) & 0xFF000000) |
226 ((rcv_val >> 24) & 0x00FF0000) |
227 ((rcv_val >> 16) & 0x0000FF00) |
228 ((rcv_val >> 8) & 0x000000FF)) << 16;
230 /* Read LSB part of serial number */
231 send_buf = cpu_to_be16(MS_SENSORS_SERIAL_READ_LSB);
234 ret = i2c_transfer(client->adapter, msg, 2);
236 dev_err(&client->dev, "Unable to read device serial number");
240 rcv_val = be64_to_cpu(rcv_buf) >> 16;
241 dev_dbg(&client->dev, "Serial MSB raw : %llx\n", rcv_val);
243 for (i = 0; i < 48; i += 24) {
244 if (!ms_sensors_crc_valid((rcv_val >> i) & 0xFFFFFF))
248 *sn |= (rcv_val & 0xFFFF00) << 40 | (rcv_val >> 32);
252 EXPORT_SYMBOL(ms_sensors_read_serial);
254 static int ms_sensors_read_config_reg(struct i2c_client *client,
259 ret = i2c_smbus_write_byte(client, MS_SENSORS_CONFIG_REG_READ);
261 dev_err(&client->dev, "Unable to read config register");
265 ret = i2c_master_recv(client, config_reg, 1);
267 dev_err(&client->dev, "Unable to read config register");
270 dev_dbg(&client->dev, "Config register :%x\n", *config_reg);
276 * ms_sensors_write_resolution() - Set resolution function
277 * @dev_data: pointer to temperature/humidity device data
278 * @i: resolution index to set
280 * This function will program the appropriate resolution based on the index
281 * provided when user space will set samp_freq channel.
282 * This function is used for TSYS02D, HTU21 and MS8607 chipsets.
284 * Return: 0 on success, negative errno otherwise.
286 ssize_t ms_sensors_write_resolution(struct ms_ht_dev *dev_data,
292 ret = ms_sensors_read_config_reg(dev_data->client, &config_reg);
297 config_reg |= ((i & 1) << 7) + ((i & 2) >> 1);
299 return i2c_smbus_write_byte_data(dev_data->client,
300 MS_SENSORS_CONFIG_REG_WRITE,
303 EXPORT_SYMBOL(ms_sensors_write_resolution);
306 * ms_sensors_show_battery_low() - Show device battery low indicator
307 * @dev_data: pointer to temperature/humidity device data
308 * @buf: pointer to char buffer to write result
310 * This function will read battery indicator value in the device and
311 * return 1 if the device voltage is below 2.25V.
312 * This function is used for TSYS02D, HTU21 and MS8607 chipsets.
314 * Return: length of sprintf on success, negative errno otherwise.
316 ssize_t ms_sensors_show_battery_low(struct ms_ht_dev *dev_data,
322 mutex_lock(&dev_data->lock);
323 ret = ms_sensors_read_config_reg(dev_data->client, &config_reg);
324 mutex_unlock(&dev_data->lock);
328 return sprintf(buf, "%d\n", (config_reg & 0x40) >> 6);
330 EXPORT_SYMBOL(ms_sensors_show_battery_low);
333 * ms_sensors_show_heater() - Show device heater
334 * @dev_data: pointer to temperature/humidity device data
335 * @buf: pointer to char buffer to write result
337 * This function will read heater enable value in the device and
338 * return 1 if the heater is enabled.
339 * This function is used for HTU21 and MS8607 chipsets.
341 * Return: length of sprintf on success, negative errno otherwise.
343 ssize_t ms_sensors_show_heater(struct ms_ht_dev *dev_data,
349 mutex_lock(&dev_data->lock);
350 ret = ms_sensors_read_config_reg(dev_data->client, &config_reg);
351 mutex_unlock(&dev_data->lock);
355 return sprintf(buf, "%d\n", (config_reg & 0x4) >> 2);
357 EXPORT_SYMBOL(ms_sensors_show_heater);
360 * ms_sensors_write_heater() - Write device heater
361 * @dev_data: pointer to temperature/humidity device data
362 * @buf: pointer to char buffer from user space
363 * @len: length of buf
365 * This function will write 1 or 0 value in the device
366 * to enable or disable heater.
367 * This function is used for HTU21 and MS8607 chipsets.
369 * Return: length of buffer, negative errno otherwise.
371 ssize_t ms_sensors_write_heater(struct ms_ht_dev *dev_data,
372 const char *buf, size_t len)
377 ret = kstrtou8(buf, 10, &val);
384 mutex_lock(&dev_data->lock);
385 ret = ms_sensors_read_config_reg(dev_data->client, &config_reg);
387 mutex_unlock(&dev_data->lock);
392 config_reg |= val << 2;
394 ret = i2c_smbus_write_byte_data(dev_data->client,
395 MS_SENSORS_CONFIG_REG_WRITE,
397 mutex_unlock(&dev_data->lock);
399 dev_err(&dev_data->client->dev, "Unable to write config register\n");
405 EXPORT_SYMBOL(ms_sensors_write_heater);
408 * ms_sensors_ht_read_temperature() - Read temperature
409 * @dev_data: pointer to temperature/humidity device data
410 * @temperature:pointer to temperature destination value
412 * This function will get temperature ADC value from the device,
413 * check the CRC and compute the temperature value.
414 * This function is used for TSYS02D, HTU21 and MS8607 chipsets.
416 * Return: 0 on success, negative errno otherwise.
418 int ms_sensors_ht_read_temperature(struct ms_ht_dev *dev_data,
425 mutex_lock(&dev_data->lock);
426 delay = ms_sensors_ht_t_conversion_time[dev_data->res_index];
427 ret = ms_sensors_convert_and_read(dev_data->client,
428 MS_SENSORS_HT_T_CONVERSION_START,
429 MS_SENSORS_NO_READ_CMD,
431 mutex_unlock(&dev_data->lock);
435 if (!ms_sensors_crc_valid(adc)) {
436 dev_err(&dev_data->client->dev,
437 "Temperature read crc check error\n");
441 /* Temperature algorithm */
442 *temperature = (((s64)(adc >> 8) * 175720) >> 16) - 46850;
446 EXPORT_SYMBOL(ms_sensors_ht_read_temperature);
449 * ms_sensors_ht_read_humidity() - Read humidity
450 * @dev_data: pointer to temperature/humidity device data
451 * @humidity: pointer to humidity destination value
453 * This function will get humidity ADC value from the device,
454 * check the CRC and compute the temperature value.
455 * This function is used for HTU21 and MS8607 chipsets.
457 * Return: 0 on success, negative errno otherwise.
459 int ms_sensors_ht_read_humidity(struct ms_ht_dev *dev_data,
466 mutex_lock(&dev_data->lock);
467 delay = ms_sensors_ht_h_conversion_time[dev_data->res_index];
468 ret = ms_sensors_convert_and_read(dev_data->client,
469 MS_SENSORS_HT_H_CONVERSION_START,
470 MS_SENSORS_NO_READ_CMD,
472 mutex_unlock(&dev_data->lock);
476 if (!ms_sensors_crc_valid(adc)) {
477 dev_err(&dev_data->client->dev,
478 "Humidity read crc check error\n");
482 /* Humidity algorithm */
483 *humidity = (((s32)(adc >> 8) * 12500) >> 16) * 10 - 6000;
484 if (*humidity >= 100000)
489 EXPORT_SYMBOL(ms_sensors_ht_read_humidity);
492 * ms_sensors_tp_crc_valid() - CRC check function for
493 * Temperature and pressure devices.
494 * This function is only used when reading PROM coefficients
496 * @prom: pointer to PROM coefficients array
497 * @len: length of PROM coefficients array
499 * Return: True if CRC is ok.
501 static bool ms_sensors_tp_crc_valid(u16 *prom, u8 len)
503 unsigned int cnt, n_bit;
504 u16 n_rem = 0x0000, crc_read = prom[0], crc = (*prom & 0xF000) >> 12;
507 prom[0] &= 0x0FFF; /* Clear the CRC computation part */
509 for (cnt = 0; cnt < len * 2; cnt++) {
511 n_rem ^= prom[cnt >> 1] & 0x00FF;
513 n_rem ^= prom[cnt >> 1] >> 8;
515 for (n_bit = 8; n_bit > 0; n_bit--) {
517 n_rem = (n_rem << 1) ^ 0x3000;
529 * ms_sensors_tp_read_prom() - prom coeff read function
530 * @dev_data: pointer to temperature/pressure device data
532 * This function will read prom coefficients and check CRC.
533 * This function is used for MS5637 and MS8607 chipsets.
535 * Return: 0 on success, negative errno otherwise.
537 int ms_sensors_tp_read_prom(struct ms_tp_dev *dev_data)
541 for (i = 0; i < MS_SENSORS_TP_PROM_WORDS_NB; i++) {
542 ret = ms_sensors_read_prom_word(
544 MS_SENSORS_TP_PROM_READ + (i << 1),
551 if (!ms_sensors_tp_crc_valid(dev_data->prom,
552 MS_SENSORS_TP_PROM_WORDS_NB + 1)) {
553 dev_err(&dev_data->client->dev,
554 "Calibration coefficients crc check error\n");
560 EXPORT_SYMBOL(ms_sensors_tp_read_prom);
563 * ms_sensors_read_temp_and_pressure() - read temp and pressure
564 * @dev_data: pointer to temperature/pressure device data
565 * @temperature:pointer to temperature destination value
566 * @pressure: pointer to pressure destination value
568 * This function will read ADC and compute pressure and temperature value.
569 * This function is used for MS5637 and MS8607 chipsets.
571 * Return: 0 on success, negative errno otherwise.
573 int ms_sensors_read_temp_and_pressure(struct ms_tp_dev *dev_data,
575 unsigned int *pressure)
580 s64 off, sens, t2, off2, sens2;
581 u16 *prom = dev_data->prom, delay;
583 mutex_lock(&dev_data->lock);
584 delay = ms_sensors_tp_conversion_time[dev_data->res_index];
586 ret = ms_sensors_convert_and_read(
588 MS_SENSORS_TP_T_CONVERSION_START +
589 dev_data->res_index * 2,
590 MS_SENSORS_TP_ADC_READ,
593 mutex_unlock(&dev_data->lock);
597 ret = ms_sensors_convert_and_read(
599 MS_SENSORS_TP_P_CONVERSION_START +
600 dev_data->res_index * 2,
601 MS_SENSORS_TP_ADC_READ,
603 mutex_unlock(&dev_data->lock);
607 dt = (s32)t_adc - (prom[5] << 8);
609 /* Actual temperature = 2000 + dT * TEMPSENS */
610 temp = 2000 + (((s64)dt * prom[6]) >> 23);
612 /* Second order temperature compensation */
614 s64 tmp = (s64)temp - 2000;
616 t2 = (3 * ((s64)dt * (s64)dt)) >> 33;
617 off2 = (61 * tmp * tmp) >> 4;
618 sens2 = (29 * tmp * tmp) >> 4;
621 s64 tmp = (s64)temp + 1500;
623 off2 += 17 * tmp * tmp;
624 sens2 += 9 * tmp * tmp;
627 t2 = (5 * ((s64)dt * (s64)dt)) >> 38;
632 /* OFF = OFF_T1 + TCO * dT */
633 off = (((s64)prom[2]) << 17) + ((((s64)prom[4]) * (s64)dt) >> 6);
636 /* Sensitivity at actual temperature = SENS_T1 + TCS * dT */
637 sens = (((s64)prom[1]) << 16) + (((s64)prom[3] * dt) >> 7);
640 /* Temperature compensated pressure = D1 * SENS - OFF */
641 *temperature = (temp - t2) * 10;
642 *pressure = (u32)(((((s64)p_adc * sens) >> 21) - off) >> 15);
646 EXPORT_SYMBOL(ms_sensors_read_temp_and_pressure);
648 MODULE_DESCRIPTION("Measurement-Specialties common i2c driver");
649 MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
650 MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
651 MODULE_LICENSE("GPL v2");