hwmon: add support for Sensirion SHT3x sensors
Subsystems (2)
PATCH 1/1 hwmon: add support for Sensirion SHT3x sensors
From: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees Celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees Celsius is 31500.
---
Patch was generated against kernel v4.5-rc7
Documentation/hwmon/sht3x | 42 ++++ drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/sht3x.c | 389 ++++++++++++++++++++++++++++++++++++ include/linux/platform_data/sht3x.h | 23 +++ 5 files changed, 465 insertions(+) create mode 100644 Documentation/hwmon/sht3x create mode 100644 drivers/hwmon/sht3x.c create mode 100644 include/linux/platform_data/sht3x.h
Documentation/hwmon/sht3x new file +41 -0
@@ -0,0 +1,42 @@ +Kernel driver sht3x +=========+ +Supported chips: + * Sensirion SHT3x-DIS + Prefix: 'sht3x' + Addresses scanned: none + Datasheet: http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_Datasheet_SHT3x_DIS.pdf + +Author: + David Frey <[email protected]> + +Description +----------- + +This driver implements support for the Sensirion SHT3x-DIS chip, a humidity +and temperature sensor. Temperature is measured in degrees celsius, relative +humidity is expressed as a percentage. In the sysfs interface, all values are +scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +The device communicates with the I2C protocol. Sensors can have the I2C +addresses 0x44 or 0x45, depending on the wiring. See +Documentation/i2c/instantiating-devices for methods to instantiate the device. + +There are two options configurable by means of sht3x_platform_data: +1. blocking (pull the I2C clock line down while performing the measurement) or + non-blocking mode. Blocking mode will guarantee the fastest result but + the I2C bus will be busy during that time. By default, non-blocking mode + is used. Make sure clock-stretching works properly on your device if you + want to use blocking mode. +2. high or low accuracy. High accuracy is used by default and using it is + strongly recommended. + +sysfs-Interface +--------------- + +temp1_input: temperature input +humidity1_input: humidity input +high_alert_set: temperature and humidity at which to set the high alert +high_alert_clear: temperature and humidity at which to clear the high alert +low_alert_clear: temperature and humidity at which to clear the low alert +low_alert_set: temperature and humidity at which to set the low alert
drivers/hwmon/Kconfig +10 -0
@@ -1235,6 +1235,16 @@ config SENSORS_SHTC1 This driver can also be built as a module. If so, the module will be called shtc1. +config SENSORS_SHT3x + tristate "Sensiron humidity and temperature sensors. SHT3x and compat." + depends on I2C + help + If you say yes here you get support for the Sensiron SHT30 and SHT31 + humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht3x. + config SENSORS_S3C tristate "Samsung built-in ADC" depends on S3C_ADC
drivers/hwmon/Makefile +1 -0
@@ -136,6 +136,7 @@ obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o +obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.o obj-$(CONFIG_SENSORS_SMSC47B397)+= smsc47b397.o
drivers/hwmon/sht3x.c new file +387 -0
387 lines not on this page · raw message
include/linux/platform_data/sht3x.h new file +23 -0
@@ -0,0 +1,23 @@ +/* +* Copyright (C) 2015 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef __SHT3X_H_ +#define __SHT3X_H_ + +struct sht3x_platform_data { +bool blocking_io; +bool high_precision; +}; +#endif /* __SHT3X_H_ */
--
1.9.1
________________________________
Confidentiality: The information contained in this email message and all attachments transmitted with it is legally privileged and confidential information intended only for the use of the addressee. If you have received this email in error, please immediately notify us and delete this email and any attachments from your system. Thank you.
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Guenter Roeck
maintainerQuoted 8 lines · Pascal Sachs
From: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees Celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees Celsius is 31500.
Your patch is whitespace damaged (spaces instead of tabs). Please fix in the next revision.
Quoted 51 lines
Signed-off-by Pascal Sachs <[email protected]>---
Patch was generated against kernel v4.5-rc7Documentation/hwmon/sht3x | 42 ++++ drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/sht3x.c | 389 ++++++++++++++++++++++++++++++++++++ include/linux/platform_data/sht3x.h | 23 +++ 5 files changed, 465 insertions(+) create mode 100644 Documentation/hwmon/sht3x create mode 100644 drivers/hwmon/sht3x.c create mode 100644 include/linux/platform_data/sht3x.hDocumentation/hwmon/sht3x new file +31
-0@@ -0,0 +1,42 @@ +Kernel driver sht3x +=========> + +Supported chips: + * Sensirion SHT3x-DIS + Prefix: 'sht3x' + Addresses scanned: none + Datasheet: http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_Datasheet_SHT3x_DIS.pdf + +Author: + David Frey <[email protected]> + +Description +----------- + +This driver implements support for the Sensirion SHT3x-DIS chip, a humidity +and temperature sensor. Temperature is measured in degrees celsius, relative +humidity is expressed as a percentage. In the sysfs interface, all values are +scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +The device communicates with the I2C protocol. Sensors can have the I2C +addresses 0x44 or 0x45, depending on the wiring. See +Documentation/i2c/instantiating-devices for methods to instantiate the device. + +There are two options configurable by means of sht3x_platform_data: +1. blocking (pull the I2C clock line down while performing the measurement) or + non-blocking mode. Blocking mode will guarantee the fastest result but + the I2C bus will be busy during that time. By default, non-blocking mode + is used. Make sure clock-stretching works properly on your device if you + want to use blocking mode. +2. high or low accuracy. High accuracy is used by default and using it is + strongly recommended.
Documentation/hwmon/sht3x line 41
Quoted 10 lines
Documentation/hwmon/sht3x new file +10
-0+ +sysfs-Interface +--------------- + +temp1_input: temperature input +humidity1_input: humidity input +high_alert_set: temperature and humidity at which to set the high alert +high_alert_clear: temperature and humidity at which to clear the high alert +low_alert_clear: temperature and humidity at which to clear the low alert +low_alert_set: temperature and humidity at which to set the low alert
Please use standard attribute names as documented in Documentation/hwmon/sysfs-interface. This would be temp1_{min,max}, temp1_{min,max}_hyst, humidity1_{min,max}, humidity1_{min,max}_hyst.
Quoted 8 lines
drivers/hwmon/Kconfig +0
-0@@ -1235,6 +1235,16 @@ config SENSORS_SHTC1 This driver can also be built as a module. If so, the module will be called shtc1.
drivers/hwmon/Kconfig config SENSORS_SHTC1 · line 1247
Quoted 10 lines
drivers/hwmon/Kconfig +10
-0+config SENSORS_SHT3x + tristate "Sensiron humidity and temperature sensors. SHT3x and compat." + depends on I2C + help + If you say yes here you get support for the Sensiron SHT30 and SHT31 + humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht3x. +
Please try to keep in alphabetic order (ahead of SHTC1).
drivers/hwmon/Makefile obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o · line 139
Quoted 12 lines
drivers/hwmon/Kconfig +0
-0config SENSORS_S3C tristate "Samsung built-in ADC" depends on S3C_ADCdrivers/hwmon/Makefile +1
-0@@ -136,6 +136,7 @@ obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o +obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o
Please try to keep files in alphabetic order.
Quoted 51 lines
drivers/hwmon/Makefile +0
-0obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.o obj-$(CONFIG_SENSORS_SMSC47B397)+= smsc47b397.odrivers/hwmon/sht3x.c new file +42
-0@@ -0,0 +1,389 @@ +/* Sensirion SHT3x-DIS humidity and temperature sensor driver. +* The SHT3x comes in many different versions, this driver is for the +* I2C version only. +* +* Copyright (C) 2015 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#include <asm/page.h> +#include <linux/crc8.h> +#include <linux/delay.h> +#include <linux/err.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/i2c.h> +#include <linux/init.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/platform_data/sht3x.h> + +/* commands (high precision mode) */ +static const unsigned char sht3x_cmd_measure_blocking_hpm[] = {0x2c ,0x06 }; +static const unsigned char sht3x_cmd_measure_nonblocking_hpm[] = {0x24 ,0x00 }; + +/* commands (low power mode) */ +static const unsigned char sht3x_cmd_measure_blocking_lpm[] = {0x2c ,0x10 }; +static const unsigned char sht3x_cmd_measure_nonblocking_lpm[] = {0x24 ,0x16 }; + +static const unsigned char sht3x_cmd_read_status_reg[] = {0xF3 ,0x2D }; + +#define HIGH_ALERT_SET_NAME "high_alert_set"
drivers/hwmon/sht3x.c line 52
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+#define HIGH_ALERT_CLEAR_NAME "high_alert_clear" +#define LOW_ALERT_SET_NAME "low_alert_set" +#define LOW_ALERT_CLEAR_NAME "low_alert_clear" + +/* delays for non-blocking i2c commands, both in us */ +#define SHT3X_NONBLOCKING_WAIT_TIME_HPM 15000 +#define SHT3X_NONBLOCKING_WAIT_TIME_LPM 4000 + +#define SHT3X_WORD_LEN 2 +#define SHT3X_CMD_LENGTH SHT3X_WORD_LEN
Why separate defines if they are both the same ? Sure, they are logically different as one is used for data and the other for the command (I think), but then they should be defined separately without having one reference the other.
Quoted 12 lines
drivers/hwmon/sht3x.c new file +12
-0+#define SHT3X_CRC8_LEN 1 +#define SHT3X_RESPONSE_LENGTH 6 +#define SHT3X_THRESHOLD_LENGTH 2 +#define SHT3X_THRESHOLD_COUNT \ + (sizeof(threshold_commands) / sizeof(struct sht3x_alert_commands)) +#define SHT3X_CRC8_LEN 1 +#define SHT3X_CRC8_POLYNOMIAL 0x31 +#define SHT3X_CRC8_INIT 0xFF + +DECLARE_CRC8_TABLE (sht3x_crc8_table); + +struct sht3x_alert_commands {
drivers/hwmon/sht3x.c line 74
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+const char *threshold_name; +const char read_command[SHT3X_CMD_LENGTH ]; +const char write_command[SHT3X_CMD_LENGTH ]; +}; + +const struct sht3x_alert_commands threshold_commands[] = { + {HIGH_ALERT_SET_NAME , {0xe1 ,0x1f }, {0x61 ,0x1d } }, + {HIGH_ALERT_CLEAR_NAME , {0xe1 ,0x14 }, {0x61 ,0x16 } }, + {LOW_ALERT_SET_NAME , {0xe1 ,0x02 }, {0x61 ,0x00 } }, + {LOW_ALERT_CLEAR_NAME , {0xe1 ,0x09 }, {0x61 ,0x0B } },
Please use array index values to identify an attribute/sensor, not names. See below.
Quoted 18 lines
drivers/hwmon/sht3x.c new file +18
-0+}; + +struct sht3x_data { +struct i2c_client *client; +struct mutex update_lock; + +const unsigned char *command; +unsigned int nonblocking_wait_time;/* in us */ + +struct sht3x_platform_data setup; + +int temperature;/* 1000 * temperature in dgr C */ +int humidity;/* 1000 * relative humidity in %RH */ +}; + + +static const struct sht3x_alert_commands * +find_commands (const char *threshold_name)
drivers/hwmon/sht3x.c line 102
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+{ +int i; + +for (i =0 ; i <SHT3X_THRESHOLD_COUNT ; i++) { +if (strcmp (threshold_name, threshold_commands[i].threshold_name) + =0 ) +return &threshold_commands[i]; + } +return 0 ; +}
This is unnecessary (and unacceptable). See below.
drivers/hwmon/sht3x.c line 110
Quoted 8 lines
drivers/hwmon/sht3x.c new file +8
-0+ +static int sht3x_read_from_command (struct i2c_client *client, +const char *command, +char *buf,int length, +unsigned int wait_time) +{ +int ret =i2c_master_send (client, command,SHT3X_CMD_LENGTH ); +
This warrants an explanation why you can not use standard (smbus) send/receive commands.
drivers/hwmon/sht3x.c line 112
Quoted 2 lines
drivers/hwmon/sht3x.c new file +2
-0+if (ret !=SHT3X_CMD_LENGTH ) { +dev_err (&client->dev,"failed to send command: %d\n" , ret);
Please no logging noise on errors. If you really think you have to log, consider making the messages dev_dbg().
Quoted 33 lines
drivers/hwmon/sht3x.c new file +33
-0+return ret <0 ? ret : -EIO ; + } + +if (wait_time) +usleep_range (wait_time, wait_time +1000 ); + + ret =i2c_master_recv (client, buf, length); +if (ret != length) { +dev_err (&client->dev,"failed to read values: %d\n" , ret); +return ret <0 ? ret : -EIO ; + } + +return 0 ; +} + +static int sht3x_extract_temperature (u16 raw) +{ +/* +* From datasheet: +* T = -45 + 175 * ST / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return ((21875 * (int )raw) >>13 ) -45000 ; +} + +static int sht3x_extract_humidity (u16 raw) +{ +/* +* From datasheet: +* RH = 100 * SRH / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return ((12500 * (int )raw) >>13 );
drivers/hwmon/sht3x.c line 155
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+} + +/* sysfs attributes */ +static struct sht3x_data *sht3x_update_client (struct device *dev) +{ +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +unsigned char buf[SHT3X_RESPONSE_LENGTH ]; +u16 val; +int ret =0 ;
Unnecessary initialization.
drivers/hwmon/sht3x.c line 163
Quoted 8 lines
drivers/hwmon/sht3x.c new file +8
-0+/* +* In blocking mode (clock stretching mode) the I2C bus +* is blocked for other traffic, thus the call to i2c_master_recv() +* will wait until the data is ready. For non blocking mode, we +* have to wait ourselves. +*/ +unsigned int wait_time = data->setup.blocking_io ? +0 : data->nonblocking_wait_time;
It might make more sense to store the wait time in sht3x_data, or does it ever change during runtime ?
Quoted 42 lines
drivers/hwmon/sht3x.c new file +42
-0+ +mutex_lock (&data->update_lock); + + ret =sht3x_read_from_command (client, data->command, + buf,sizeof (buf), wait_time); +if (ret) + goto out; + + val =be16_to_cpup ((__be16 *) buf); + data->temperature =sht3x_extract_temperature (val); + val =be16_to_cpup ((__be16 *) (buf +3 )); + data->humidity =sht3x_extract_humidity (val); + +out: +mutex_unlock (&data->update_lock); + +return ret =0 ? data :ERR_PTR (ret); +} + +static ssize_t temp1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->temperature); +} + +static ssize_t humidity1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->humidity); +} + +static ssize_t alert_show(struct device *dev,
drivers/hwmon/sht3x.c line 215
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+struct device_attribute *attr, + char *buf) +{ + char buffer[SHT3X_WORD_LEN]; +u16 raw; +int temperature, humidity; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + const struct sht3x_alert_commands *commands > + find_commands(attr->attr.name); +
Please use SENSOR_DEVICE_ATTR for the attribute declarations.
It lets you specify an additional parameter, which you can use as array index.
drivers/hwmon/sht3x.c line 217
Quoted 2 lines
drivers/hwmon/sht3x.c new file +2
-0+int ret =sht3x_read_from_command (client, commands->read_command, + buffer,sizeof (buffer),0 );
I am quite sure this results in a checkpatch warning. It is quite long anyway for a variable declaration combined with initialization. Please split.
In general, if a variable declaration plus initialization is longer than one line, please split declaration from initialization.
I am also concerned that the call to sht3x_read_from_command() is not mutex protected. This means it can be called twice, while the update function is waiting for a response. Hard to imagine that this won't cause trouble. It might be better to move the locking into sht3x_read_from_command().
drivers/hwmon/sht3x.c line 229
Quoted 12 lines
drivers/hwmon/sht3x.c new file +12
-0+if (ret) +return ret; + + raw =be16_to_cpup ((__be16 *) buffer); +/* +* top 7 bits are the humidity MSB, +* lower 9 bits are temperature MSB +*/ + temperature =sht3x_extract_temperature ((raw &0x01ff ) <<7 ); + humidity =sht3x_extract_humidity (raw &0xfe00 ); + +return scnprintf (buf,PAGE_SIZE ,"%d %d\n" , temperature, humidity);
Again, please use (separate) standard attributes.
drivers/hwmon/sht3x.c line 237
Quoted 8 lines
drivers/hwmon/sht3x.c new file +8
-0+} + +static ssize_t alert_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) +{ +char buffer[2 *SHT3X_WORD_LEN +SHT3X_CRC8_LEN ];
Strictly speaking, since you have separate defines, this should be
char buffer[SHT3X_CMD_LENGTH + SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
Quoted 11 lines
drivers/hwmon/sht3x.c new file +11
-0+char *position = buffer; +u16 raw; +int temperature, humidity; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + const struct sht3x_alert_commands *commands > + find_commands(attr->attr.name); + +int ret =sscanf (buf,"%d %d" , &temperature, &humidity); + +if (ret !=SHT3X_THRESHOLD_LENGTH ) { +dev_err (&client->dev,"wrong number of arguments: %d" , ret);
drivers/hwmon/sht3x.c line 258
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+return -EINVAL ; + } +if (temperature <-45000 || temperature >130000 ) { +dev_err (&client->dev,"temperature out of range" ); +return -EINVAL ; + } +if (humidity <0 || humidity >100000 ) { +dev_err (&client->dev,"humidity out of range" ); +return -EINVAL ; + }
Please use clamp_val() to limit number ranges.
Also, as mentioned before, please no kernel log messages on errors. We don't usually log ABI errors; this could easily be used to fill up the kernel log with junk.
Quoted 5 lines
drivers/hwmon/sht3x.c new file +5
-0+ +memcpy (position, commands->write_command,SHT3X_CMD_LENGTH ); + position +=SHT3X_CMD_LENGTH ; +/* +* ST = (T + 45) / 175 * 2^16
drivers/hwmon/sht3x.c line 273
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+* SRH = RH / 100 * 2^16 +* adapted for fixed point arithmetic and packed the same as +* in alert_show() +*/ + raw = ((temperature +45000 ) *24543 ) >> (16 +7 ); + raw |= ((humidity *42950 ) >>16 ) &0xfe00 ; + *(__be16 *)(position) =cpu_to_be16 (raw); + position[SHT3X_WORD_LEN ] =crc8 (sht3x_crc8_table, + position,SHT3X_WORD_LEN , +SHT3X_CRC8_INIT );
This is confusing. First you add to position, then you index the result. Since index values are all constant, can you instead just work with buffer pointers directly ?
Quoted 62 lines
drivers/hwmon/sht3x.c new file +62
-0+ + ret =i2c_master_send (client, buffer,sizeof (buffer)); +if (ret !=sizeof (buffer)) { +dev_err (&client->dev,"failed to set alert threshold\n" ); +return ret <0 ? ret : -EIO ; + } +return count; +} + +static DEVICE_ATTR_RO (temp1_input); +static DEVICE_ATTR_RO (humidity1_input); +static DEVICE_ATTR(high_alert_set, 0664, alert_show, alert_store); +static DEVICE_ATTR(high_alert_clear, 0664, alert_show, alert_store); +static DEVICE_ATTR(low_alert_set, 0664, alert_show, alert_store); +static DEVICE_ATTR(low_alert_clear, 0664, alert_show, alert_store); + +static struct attribute *sht3x_attrs[] = { + &dev_attr_temp1_input.attr, + &dev_attr_humidity1_input.attr, + &dev_attr_high_alert_set.attr, + &dev_attr_high_alert_clear.attr, + &dev_attr_low_alert_set.attr, + &dev_attr_low_alert_clear.attr, +NULL +}; + +ATTRIBUTE_GROUPS (sht3x); + +static void sht3x_select_command (struct sht3x_data *data) +{ +if (data->setup.high_precision) { + data->command = data->setup.blocking_io ? + sht3x_cmd_measure_blocking_hpm : + sht3x_cmd_measure_nonblocking_hpm; + data->nonblocking_wait_time =SHT3X_NONBLOCKING_WAIT_TIME_HPM ; + + }else { + data->command = data->setup.blocking_io ? + sht3x_cmd_measure_blocking_lpm : + sht3x_cmd_measure_nonblocking_lpm; + data->nonblocking_wait_time =SHT3X_NONBLOCKING_WAIT_TIME_LPM ; + } +} + +static int sht3x_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +int ret; +char status_reg[2 ]; +struct sht3x_data *data; +struct device *hwmon_dev; +struct i2c_adapter *adap = client->adapter; +struct device *dev = &client->dev; + +if (!i2c_check_functionality (adap,I2C_FUNC_I2C )) { +dev_err (dev,"plain i2c transactions not supported\n" ); +return -ENODEV ; + } + + ret =i2c_master_send (client, sht3x_cmd_read_status_reg, +SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) {
drivers/hwmon/sht3x.c line 345
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+dev_err (dev,"could not send read_status_reg command: %d\n" , + ret); +return ret <0 ? ret : -ENODEV ; + } + ret =i2c_master_recv (client, status_reg,sizeof (status_reg)); +if (ret !=sizeof (status_reg)) { +dev_err (dev,"could not read status register: %d\n" , ret); +return -ENODEV ; + } +
Unless I am missing something, those commands are only to check if the device is present. This unnecessarily slows down boot time. Please drop; the device should not be instantiated if it isn't there.
Quoted 48 lines
drivers/hwmon/sht3x.c new file +42
-0+ data =devm_kzalloc (dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->setup.blocking_io = false; + data->setup.high_precision = true; + data->client = client; +crc8_populate_msb (sht3x_crc8_table,SHT3X_CRC8_POLYNOMIAL ); + +if (client->dev.platform_data) + data->setup = *(struct sht3x_platform_data *)dev->platform_data; +sht3x_select_command (data); +mutex_init (&data->update_lock); + + hwmon_dev =devm_hwmon_device_register_with_groups (dev, + client->name, + data, sht3x_groups); +if (IS_ERR (hwmon_dev)) +dev_dbg (dev,"unable to register hwmon device\n" ); + +return PTR_ERR_OR_ZERO (hwmon_dev); +} + +/* device ID table */ +static const struct i2c_device_id sht3x_id[] = { + {"sht3x" ,0 }, + {} +}; + +MODULE_DEVICE_TABLE (i2c, sht3x_id); + +static struct i2c_driver sht3x_i2c_driver = { + .driver.name ="sht3x" , + .probe = sht3x_probe, + .id_table = sht3x_id, +}; + +module_i2c_driver (sht3x_i2c_driver); + +MODULE_AUTHOR ("David Frey <[email protected]>" ); +MODULE_DESCRIPTION ("Sensirion SHT3x humidity and temperature sensor driver" ); +MODULE_LICENSE ("GPL" );include/linux/platform_data/sht3x.h new file +0
-0
include/linux/platform_data/sht3x.h line 8
Quoted 8 lines
include/linux/platform_data/sht3x.h new file +8
-0@@ -0,0 +1,23 @@ +/* +* Copyright (C) 2015 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +*
Source code is GPLv2 or later, header file is GPLv2. Is this accidential or on purpose ?
Quoted 16 lines
include/linux/platform_data/sht3x.h new file +15
-1+* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef __SHT3X_H_ +#define __SHT3X_H_ + +struct sht3x_platform_data { +bool blocking_io; +bool high_precision; +}; +#endif /* __SHT3X_H_ */ --
Quoted 3 lines
1.9.1
________________________________
Confidentiality: The information contained in this email message and all attachments transmitted with it is legally privileged and confidential information intended only for the use of the addressee. If you have received this email in error, please immediately notify us and delete this email and any attachments from your system. Thank you.
Uuhh ... maybe I was not authorized to even read this mail after all. How would I or anyone else know ? This is really a red flag.
Guenter
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Hi Guenter
Thank you for the fast code review.
During extended testing of the updated driver we found some other minor issues with the alert feature we would like to fix before submitting again.
We as well have to update our submission process, since the company mail server converts tabs to whitespaces and adds this confidentiality message to the end. I am very sorry about that, but I did not knew that.
Regards,
Pascal
Von: Guenter Roeck [[email protected]]
Gesendet: Donnerstag, 10. März 2016 20:14
An: Pascal Sachs
Cc: Jean Delvare; Jonathan Corbet; [email protected]; [email protected]; [email protected]; David Frey
Betreff: Re: [PATCH 1/1] hwmon: add support for Sensirion SHT3x sensors
Quoted 8 lines · Pascal Sachs
From: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees Celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees Celsius is 31500.
Your patch is whitespace damaged (spaces instead of tabs). Please fix in the next revision.
Quoted 52 lines
Signed-off-by Pascal Sachs <[email protected]>---
Patch was generated against kernel v4.5-rc7Documentation/hwmon/sht3x | 42 ++++ drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/sht3x.c | 389 ++++++++++++++++++++++++++++++++++++ include/linux/platform_data/sht3x.h | 23 +++ 5 files changed, 465 insertions(+) create mode 100644 Documentation/hwmon/sht3x create mode 100644 drivers/hwmon/sht3x.c create mode 100644 include/linux/platform_data/sht3x.hDocumentation/hwmon/sht3x new file +32
-0@@ -0,0 +1,42 @@ +Kernel driver sht3x +=================== + +Supported chips: + * Sensirion SHT3x-DIS + Prefix: 'sht3x' + Addresses scanned: none + Datasheet: http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_Datasheet_SHT3x_DIS.pdf + +Author: + David Frey <[email protected]> + +Description +----------- + +This driver implements support for the Sensirion SHT3x-DIS chip, a humidity +and temperature sensor. Temperature is measured in degrees celsius, relative +humidity is expressed as a percentage. In the sysfs interface, all values are +scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +The device communicates with the I2C protocol. Sensors can have the I2C +addresses 0x44 or 0x45, depending on the wiring. See +Documentation/i2c/instantiating-devices for methods to instantiate the device. + +There are two options configurable by means of sht3x_platform_data: +1. blocking (pull the I2C clock line down while performing the measurement) or + non-blocking mode. Blocking mode will guarantee the fastest result but + the I2C bus will be busy during that time. By default, non-blocking mode + is used. Make sure clock-stretching works properly on your device if you + want to use blocking mode. +2. high or low accuracy. High accuracy is used by default and using it is + strongly recommended.
Documentation/hwmon/sht3x line 42
Quoted 10 lines
Documentation/hwmon/sht3x new file +10
-0+ +sysfs-Interface +--------------- + +temp1_input: temperature input +humidity1_input: humidity input +high_alert_set: temperature and humidity at which to set the high alert +high_alert_clear: temperature and humidity at which to clear the high alert +low_alert_clear: temperature and humidity at which to clear the low alert +low_alert_set: temperature and humidity at which to set the low alert
Please use standard attribute names as documented in Documentation/hwmon/sysfs-interface. This would be temp1_{min,max}, temp1_{min,max}_hyst, humidity1_{min,max}, humidity1_{min,max}_hyst.
Quoted 8 lines
drivers/hwmon/Kconfig +0
-0@@ -1235,6 +1235,16 @@ config SENSORS_SHTC1 This driver can also be built as a module. If so, the module will be called shtc1.
drivers/hwmon/Kconfig config SENSORS_SHTC1 · line 1247
Quoted 10 lines
drivers/hwmon/Kconfig +10
-0+config SENSORS_SHT3x + tristate "Sensiron humidity and temperature sensors. SHT3x and compat." + depends on I2C + help + If you say yes here you get support for the Sensiron SHT30 and SHT31 + humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht3x. +
Please try to keep in alphabetic order (ahead of SHTC1).
drivers/hwmon/Makefile obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o · line 139
Quoted 12 lines
drivers/hwmon/Kconfig +0
-0config SENSORS_S3C tristate "Samsung built-in ADC" depends on S3C_ADCdrivers/hwmon/Makefile +1
-0@@ -136,6 +136,7 @@ obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o +obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o
Please try to keep files in alphabetic order.
Quoted 51 lines
drivers/hwmon/Makefile +0
-0obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.o obj-$(CONFIG_SENSORS_SMSC47B397)+= smsc47b397.odrivers/hwmon/sht3x.c new file +42
-0@@ -0,0 +1,389 @@ +/* Sensirion SHT3x-DIS humidity and temperature sensor driver. +* The SHT3x comes in many different versions, this driver is for the +* I2C version only. +* +* Copyright (C) 2015 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#include <asm/page.h> +#include <linux/crc8.h> +#include <linux/delay.h> +#include <linux/err.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/i2c.h> +#include <linux/init.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/platform_data/sht3x.h> + +/* commands (high precision mode) */ +static const unsigned char sht3x_cmd_measure_blocking_hpm[] = {0x2c ,0x06 }; +static const unsigned char sht3x_cmd_measure_nonblocking_hpm[] = {0x24 ,0x00 }; + +/* commands (low power mode) */ +static const unsigned char sht3x_cmd_measure_blocking_lpm[] = {0x2c ,0x10 }; +static const unsigned char sht3x_cmd_measure_nonblocking_lpm[] = {0x24 ,0x16 }; + +static const unsigned char sht3x_cmd_read_status_reg[] = {0xF3 ,0x2D }; + +#define HIGH_ALERT_SET_NAME "high_alert_set"
drivers/hwmon/sht3x.c line 52
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+#define HIGH_ALERT_CLEAR_NAME "high_alert_clear" +#define LOW_ALERT_SET_NAME "low_alert_set" +#define LOW_ALERT_CLEAR_NAME "low_alert_clear" + +/* delays for non-blocking i2c commands, both in us */ +#define SHT3X_NONBLOCKING_WAIT_TIME_HPM 15000 +#define SHT3X_NONBLOCKING_WAIT_TIME_LPM 4000 + +#define SHT3X_WORD_LEN 2 +#define SHT3X_CMD_LENGTH SHT3X_WORD_LEN
Why separate defines if they are both the same ? Sure, they are logically different as one is used for data and the other for the command (I think), but then they should be defined separately without having one reference the other.
Quoted 12 lines
drivers/hwmon/sht3x.c new file +12
-0+#define SHT3X_CRC8_LEN 1 +#define SHT3X_RESPONSE_LENGTH 6 +#define SHT3X_THRESHOLD_LENGTH 2 +#define SHT3X_THRESHOLD_COUNT \ + (sizeof(threshold_commands) / sizeof(struct sht3x_alert_commands)) +#define SHT3X_CRC8_LEN 1 +#define SHT3X_CRC8_POLYNOMIAL 0x31 +#define SHT3X_CRC8_INIT 0xFF + +DECLARE_CRC8_TABLE (sht3x_crc8_table); + +struct sht3x_alert_commands {
drivers/hwmon/sht3x.c line 74
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+const char *threshold_name; +const char read_command[SHT3X_CMD_LENGTH ]; +const char write_command[SHT3X_CMD_LENGTH ]; +}; + +const struct sht3x_alert_commands threshold_commands[] = { + {HIGH_ALERT_SET_NAME , {0xe1 ,0x1f }, {0x61 ,0x1d } }, + {HIGH_ALERT_CLEAR_NAME , {0xe1 ,0x14 }, {0x61 ,0x16 } }, + {LOW_ALERT_SET_NAME , {0xe1 ,0x02 }, {0x61 ,0x00 } }, + {LOW_ALERT_CLEAR_NAME , {0xe1 ,0x09 }, {0x61 ,0x0B } },
Please use array index values to identify an attribute/sensor, not names. See below.
Quoted 18 lines
drivers/hwmon/sht3x.c new file +18
-0+}; + +struct sht3x_data { +struct i2c_client *client; +struct mutex update_lock; + +const unsigned char *command; +unsigned int nonblocking_wait_time;/* in us */ + +struct sht3x_platform_data setup; + +int temperature;/* 1000 * temperature in dgr C */ +int humidity;/* 1000 * relative humidity in %RH */ +}; + + +static const struct sht3x_alert_commands * +find_commands (const char *threshold_name)
drivers/hwmon/sht3x.c line 102
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+{ +int i; + +for (i =0 ; i <SHT3X_THRESHOLD_COUNT ; i++) { +if (strcmp (threshold_name, threshold_commands[i].threshold_name) + ==0 ) +return &threshold_commands[i]; + } +return 0 ; +}
This is unnecessary (and unacceptable). See below.
drivers/hwmon/sht3x.c line 110
Quoted 8 lines
drivers/hwmon/sht3x.c new file +8
-0+ +static int sht3x_read_from_command (struct i2c_client *client, +const char *command, +char *buf,int length, +unsigned int wait_time) +{ +int ret =i2c_master_send (client, command,SHT3X_CMD_LENGTH ); +
This warrants an explanation why you can not use standard (smbus) send/receive commands.
drivers/hwmon/sht3x.c line 112
Quoted 2 lines
drivers/hwmon/sht3x.c new file +2
-0+if (ret !=SHT3X_CMD_LENGTH ) { +dev_err (&client->dev,"failed to send command: %d\n" , ret);
Please no logging noise on errors. If you really think you have to log, consider making the messages dev_dbg().
Quoted 33 lines
drivers/hwmon/sht3x.c new file +33
-0+return ret <0 ? ret : -EIO ; + } + +if (wait_time) +usleep_range (wait_time, wait_time +1000 ); + + ret =i2c_master_recv (client, buf, length); +if (ret != length) { +dev_err (&client->dev,"failed to read values: %d\n" , ret); +return ret <0 ? ret : -EIO ; + } + +return 0 ; +} + +static int sht3x_extract_temperature (u16 raw) +{ +/* +* From datasheet: +* T = -45 + 175 * ST / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return ((21875 * (int )raw) >>13 ) -45000 ; +} + +static int sht3x_extract_humidity (u16 raw) +{ +/* +* From datasheet: +* RH = 100 * SRH / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return ((12500 * (int )raw) >>13 );
drivers/hwmon/sht3x.c line 155
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+} + +/* sysfs attributes */ +static struct sht3x_data *sht3x_update_client (struct device *dev) +{ +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +unsigned char buf[SHT3X_RESPONSE_LENGTH ]; +u16 val; +int ret =0 ;
Unnecessary initialization.
drivers/hwmon/sht3x.c line 163
Quoted 8 lines
drivers/hwmon/sht3x.c new file +8
-0+/* +* In blocking mode (clock stretching mode) the I2C bus +* is blocked for other traffic, thus the call to i2c_master_recv() +* will wait until the data is ready. For non blocking mode, we +* have to wait ourselves. +*/ +unsigned int wait_time = data->setup.blocking_io ? +0 : data->nonblocking_wait_time;
It might make more sense to store the wait time in sht3x_data, or does it ever change during runtime ?
Quoted 43 lines
drivers/hwmon/sht3x.c new file +43
-0+ +mutex_lock (&data->update_lock); + + ret =sht3x_read_from_command (client, data->command, + buf,sizeof (buf), wait_time); +if (ret) + goto out; + + val =be16_to_cpup ((__be16 *) buf); + data->temperature =sht3x_extract_temperature (val); + val =be16_to_cpup ((__be16 *) (buf +3 )); + data->humidity =sht3x_extract_humidity (val); + +out: +mutex_unlock (&data->update_lock); + +return ret ==0 ? data :ERR_PTR (ret); +} + +static ssize_t temp1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->temperature); +} + +static ssize_t humidity1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->humidity); +} + +static ssize_t alert_show(struct device *dev, + struct device_attribute *attr,
drivers/hwmon/sht3x.c line 215
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ char *buf) +{ +char buffer[SHT3X_WORD_LEN ]; +u16 raw; +int temperature, humidity; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +const struct sht3x_alert_commands *commands = +find_commands (attr->attr.name); +
Please use SENSOR_DEVICE_ATTR for the attribute declarations.
It lets you specify an additional parameter, which you can use as array index.
drivers/hwmon/sht3x.c line 217
Quoted 2 lines
drivers/hwmon/sht3x.c new file +2
-0+int ret =sht3x_read_from_command (client, commands->read_command, + buffer,sizeof (buffer),0 );
I am quite sure this results in a checkpatch warning. It is quite long anyway for a variable declaration combined with initialization. Please split.
In general, if a variable declaration plus initialization is longer than one line, please split declaration from initialization.
I am also concerned that the call to sht3x_read_from_command() is not mutex protected. This means it can be called twice, while the update function is waiting for a response. Hard to imagine that this won't cause trouble. It might be better to move the locking into sht3x_read_from_command().
drivers/hwmon/sht3x.c line 229
Quoted 12 lines
drivers/hwmon/sht3x.c new file +12
-0+if (ret) +return ret; + + raw =be16_to_cpup ((__be16 *) buffer); +/* +* top 7 bits are the humidity MSB, +* lower 9 bits are temperature MSB +*/ + temperature =sht3x_extract_temperature ((raw &0x01ff ) <<7 ); + humidity =sht3x_extract_humidity (raw &0xfe00 ); + +return scnprintf (buf,PAGE_SIZE ,"%d %d\n" , temperature, humidity);
Again, please use (separate) standard attributes.
drivers/hwmon/sht3x.c line 237
Quoted 8 lines
drivers/hwmon/sht3x.c new file +8
-0+} + +static ssize_t alert_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) +{ +char buffer[2 *SHT3X_WORD_LEN +SHT3X_CRC8_LEN ];
Strictly speaking, since you have separate defines, this should be
char buffer[SHT3X_CMD_LENGTH + SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
Quoted 12 lines
drivers/hwmon/sht3x.c new file +12
-0+char *position = buffer; +u16 raw; +int temperature, humidity; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +const struct sht3x_alert_commands *commands = +find_commands (attr->attr.name); + +int ret =sscanf (buf,"%d %d" , &temperature, &humidity); + +if (ret !=SHT3X_THRESHOLD_LENGTH ) { +dev_err (&client->dev,"wrong number of arguments: %d" , ret);
drivers/hwmon/sht3x.c line 258
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+return -EINVAL ; + } +if (temperature <-45000 || temperature >130000 ) { +dev_err (&client->dev,"temperature out of range" ); +return -EINVAL ; + } +if (humidity <0 || humidity >100000 ) { +dev_err (&client->dev,"humidity out of range" ); +return -EINVAL ; + }
Please use clamp_val() to limit number ranges.
Also, as mentioned before, please no kernel log messages on errors. We don't usually log ABI errors; this could easily be used to fill up the kernel log with junk.
Quoted 5 lines
drivers/hwmon/sht3x.c new file +5
-0+ +memcpy (position, commands->write_command,SHT3X_CMD_LENGTH ); + position +=SHT3X_CMD_LENGTH ; +/* +* ST = (T + 45) / 175 * 2^16
drivers/hwmon/sht3x.c line 273
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+* SRH = RH / 100 * 2^16 +* adapted for fixed point arithmetic and packed the same as +* in alert_show() +*/ + raw = ((temperature +45000 ) *24543 ) >> (16 +7 ); + raw |= ((humidity *42950 ) >>16 ) &0xfe00 ; + *(__be16 *)(position) =cpu_to_be16 (raw); + position[SHT3X_WORD_LEN ] =crc8 (sht3x_crc8_table, + position,SHT3X_WORD_LEN , +SHT3X_CRC8_INIT );
This is confusing. First you add to position, then you index the result. Since index values are all constant, can you instead just work with buffer pointers directly ?
Quoted 62 lines
drivers/hwmon/sht3x.c new file +62
-0+ + ret =i2c_master_send (client, buffer,sizeof (buffer)); +if (ret !=sizeof (buffer)) { +dev_err (&client->dev,"failed to set alert threshold\n" ); +return ret <0 ? ret : -EIO ; + } +return count; +} + +static DEVICE_ATTR_RO (temp1_input); +static DEVICE_ATTR_RO (humidity1_input); +static DEVICE_ATTR(high_alert_set, 0664, alert_show, alert_store); +static DEVICE_ATTR(high_alert_clear, 0664, alert_show, alert_store); +static DEVICE_ATTR(low_alert_set, 0664, alert_show, alert_store); +static DEVICE_ATTR(low_alert_clear, 0664, alert_show, alert_store); + +static struct attribute *sht3x_attrs[] = { + &dev_attr_temp1_input.attr, + &dev_attr_humidity1_input.attr, + &dev_attr_high_alert_set.attr, + &dev_attr_high_alert_clear.attr, + &dev_attr_low_alert_set.attr, + &dev_attr_low_alert_clear.attr, +NULL +}; + +ATTRIBUTE_GROUPS (sht3x); + +static void sht3x_select_command (struct sht3x_data *data) +{ +if (data->setup.high_precision) { + data->command = data->setup.blocking_io ? + sht3x_cmd_measure_blocking_hpm : + sht3x_cmd_measure_nonblocking_hpm; + data->nonblocking_wait_time =SHT3X_NONBLOCKING_WAIT_TIME_HPM ; + + }else { + data->command = data->setup.blocking_io ? + sht3x_cmd_measure_blocking_lpm : + sht3x_cmd_measure_nonblocking_lpm; + data->nonblocking_wait_time =SHT3X_NONBLOCKING_WAIT_TIME_LPM ; + } +} + +static int sht3x_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +int ret; +char status_reg[2 ]; +struct sht3x_data *data; +struct device *hwmon_dev; +struct i2c_adapter *adap = client->adapter; +struct device *dev = &client->dev; + +if (!i2c_check_functionality (adap,I2C_FUNC_I2C )) { +dev_err (dev,"plain i2c transactions not supported\n" ); +return -ENODEV ; + } + + ret =i2c_master_send (client, sht3x_cmd_read_status_reg, +SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) {
drivers/hwmon/sht3x.c line 345
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+dev_err (dev,"could not send read_status_reg command: %d\n" , + ret); +return ret <0 ? ret : -ENODEV ; + } + ret =i2c_master_recv (client, status_reg,sizeof (status_reg)); +if (ret !=sizeof (status_reg)) { +dev_err (dev,"could not read status register: %d\n" , ret); +return -ENODEV ; + } +
Unless I am missing something, those commands are only to check if the device is present. This unnecessarily slows down boot time. Please drop; the device should not be instantiated if it isn't there.
Quoted 48 lines
drivers/hwmon/sht3x.c new file +42
-0+ data =devm_kzalloc (dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->setup.blocking_io = false; + data->setup.high_precision = true; + data->client = client; +crc8_populate_msb (sht3x_crc8_table,SHT3X_CRC8_POLYNOMIAL ); + +if (client->dev.platform_data) + data->setup = *(struct sht3x_platform_data *)dev->platform_data; +sht3x_select_command (data); +mutex_init (&data->update_lock); + + hwmon_dev =devm_hwmon_device_register_with_groups (dev, + client->name, + data, sht3x_groups); +if (IS_ERR (hwmon_dev)) +dev_dbg (dev,"unable to register hwmon device\n" ); + +return PTR_ERR_OR_ZERO (hwmon_dev); +} + +/* device ID table */ +static const struct i2c_device_id sht3x_id[] = { + {"sht3x" ,0 }, + {} +}; + +MODULE_DEVICE_TABLE (i2c, sht3x_id); + +static struct i2c_driver sht3x_i2c_driver = { + .driver.name ="sht3x" , + .probe = sht3x_probe, + .id_table = sht3x_id, +}; + +module_i2c_driver (sht3x_i2c_driver); + +MODULE_AUTHOR ("David Frey <[email protected]>" ); +MODULE_DESCRIPTION ("Sensirion SHT3x humidity and temperature sensor driver" ); +MODULE_LICENSE ("GPL" );include/linux/platform_data/sht3x.h new file +0
-0
include/linux/platform_data/sht3x.h line 8
Quoted 8 lines
include/linux/platform_data/sht3x.h new file +8
-0@@ -0,0 +1,23 @@ +/* +* Copyright (C) 2015 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* +* This software is licensed under the terms of the GNU General Public +* License version 2, as published by the Free Software Foundation, and +* may be copied, distributed, and modified under those terms. +*
Source code is GPLv2 or later, header file is GPLv2. Is this accidential or on purpose ?
Quoted 16 lines
include/linux/platform_data/sht3x.h new file +15
-1+* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef __SHT3X_H_ +#define __SHT3X_H_ + +struct sht3x_platform_data { +bool blocking_io; +bool high_precision; +}; +#endif /* __SHT3X_H_ */ --
Quoted 3 lines
1.9.1
________________________________
Confidentiality: The information contained in this email message and all attachments transmitted with it is legally privileged and confidential information intended only for the use of the addressee. If you have received this email in error, please immediately notify us and delete this email and any attachments from your system. Thank you.
Uuhh ... maybe I was not authorized to even read this mail after all. How would I or anyone else know ? This is really a red flag.
Guenter
________________________________
Confidentiality: The information contained in this email message and all attachments transmitted with it is legally privileged and confidential information intended only for the use of the addressee. If you have received this email in error, please immediately notify us and delete this email and any attachments from your system. Thank you.
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Guenter Roeck
maintainerHi Pascal,
Quoted 5 lines · Pascal Sachs
Hi Guenter
Thank you for the fast code review.
During extended testing of the updated driver we found some other minor issues with the alert feature we would like to fix before submitting again.
Take your time - you missed the commit window anyway.
Quoted 3 lines
We as well have to update our submission process, since the company mail server converts tabs to whitespaces and adds this confidentiality message to the end. I am very sorry about that, but I did not knew that.
No problem. Must be a server from that company in Redmond, WA ;-).
In case it helps, I use "git send-email" in combination with msmtp, which in turn is configured to send mail either through a gmail account or, if that happens to be tagged as spam source, through a web hosting account.
Guenter
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
PATCH v2 1/1 hwmon: add support for Sensirion SHT3x sensors
In reply to Pascal SachsFrom: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500.
---
Patch was generated against kernel v4.6-rc5
Documentation/hwmon/sht3x | 66 ++++ drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/sht3x.c | 675 ++++++++++++++++++++++++++++++++++++ drivers/hwmon/twl4030-madc-hwmon.c | 1 + include/linux/platform_data/sht3x.h | 25 ++ 6 files changed, 778 insertions(+) create mode 100644 Documentation/hwmon/sht3x create mode 100644 drivers/hwmon/sht3x.c create mode 100644 include/linux/platform_data/sht3x.h
Documentation/hwmon/sht3x new file +66 -0
@@ -0,0 +1,66 @@ +Kernel driver sht3x +=================== + +Supported chips: + * Sensirion SHT3x-DIS + Prefix: 'sht3x' + Addresses scanned: none + Datasheet: http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_Datasheet_SHT3x_DIS.pdf + +Author: + David Frey <[email protected]> + Pascal Sachs <[email protected]> + +Description +----------- + +This driver implements support for the Sensirion SHT3x-DIS chip, a humidity +and temperature sensor. Temperature is measured in degrees celsius, relative +humidity is expressed as a percentage. In the sysfs interface, all values are +scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +The device communicates with the I2C protocol. Sensors can have the I2C +addresses 0x44 or 0x45, depending on the wiring. See +Documentation/i2c/instantiating-devices for methods to instantiate the device. + +There are two options configurable by means of sht3x_platform_data: +1. blocking (pull the I2C clock line down while performing the measurement) or + non-blocking mode. Blocking mode will guarantee the fastest result but + the I2C bus will be busy during that time. By default, non-blocking mode + is used. Make sure clock-stretching works properly on your device if you + want to use blocking mode. +2. high or low accuracy. High accuracy is used by default and using it is + strongly recommended. + +The sht3x sensor supports a single shot mode as well as 5 periodic measure +modes, which can be controlled with the frequency sysfs interface. +The allowed frequencies are as follows: + * 0 single shot mode + * 500 0.5 Hz periodic measurement + * 1000 1 Hz periodic measurement + * 2000 2 Hz periodic measurement + * 4000 4 Hz periodic measurement + * 10000 10 Hz periodic measurement + +While in periodic measure mode, read out of humidity and temperature values are +not supported. Nevertheless it is possible to read out the values with maximal +the same frequency as the periodic measure mode. + +sysfs-Interface +--------------- + +temp1_input: temperature input +humidity1_input: humidity input +temp1_max: temperature max value +temp1_max_hyst: temperature hysteresis value for max limit +humidity1_max: humidity max value +humidity1_max_hyst: humidity hysteresis value for max limit +temp1_min: temperature min value +temp1_min_hyst: temperature hysteresis value for min limit +humidity1_min: humidity min value +humidity1_min_hyst: humidity hysteresis value for min limit +frequency: Measurement frequency, 0 for single shot, frequency in + mHz for periodic measurement +soft_reset: Soft reset the internal state of the sensor, clear the + status register, clear the alert and switch back to + single shot mode
drivers/hwmon/Kconfig +10 -0
@@ -1254,6 +1254,16 @@ config SENSORS_SHT21 This driver can also be built as a module. If so, the module will be called sht21. +config SENSORS_SHT3x + tristate "Sensiron humidity and temperature sensors. SHT3x and compat." + depends on I2C && CRC8 + help + If you say yes here you get support for the Sensiron SHT30 and SHT31 + humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht3x. + config SENSORS_SHTC1 tristate "Sensiron humidity and temperature sensors. SHTC1 and compat." depends on I2C
drivers/hwmon/Makefile +1 -0
@@ -137,6 +137,7 @@ obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o +obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.o
drivers/hwmon/sht3x.c new file +675 -0
675 lines not on this page · raw message
drivers/hwmon/twl4030-madc-hwmon.c +1 -0
@@ -25,6 +25,7 @@*/ #include <linux/init.h> #include <linux/module.h> +//FIXME this is used for clamp_val #include <linux/kernel.h> #include <linux/i2c/twl.h> #include <linux/device.h>
include/linux/platform_data/sht3x.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* Copyright (C) 2015 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef __SHT3X_H_ +#define __SHT3X_H_ + +struct sht3x_platform_data { +bool blocking_io; +bool high_precision; +}; +#endif /* __SHT3X_H_ */
Signature · 1 line
1.9.1
Guenter Roeck
maintainerQuoted 9 lines · Pascal Sachs
From: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500.
Signed-off-by Pascal Sachs <[email protected]>
Quoted 8 lines
---
Patch was generated against kernel v4.6-rc5Documentation/hwmon/sht3x | 66 ++++ drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/sht3x.c | 675 ++++++++++++++++++++++++++++++++++++ drivers/hwmon/twl4030-madc-hwmon.c | 1 +
Please drop this change.
Quoted 6 lines
include/linux/platform_data/sht3x.h | 25 ++ 6 files changed, 778 insertions(+)create mode 100644
Documentation/hwmon/sht3xcreate mode 100644drivers/hwmon/sht3x.ccreate mode 100644include/linux/platform_data/sht3x.h
Please run checkpatch --strict over your patch. There are lots of unnecessary/undesirable empty lines and spaces. It would also tell you about the presumably left-over FIXME in twl4030-madc-hwmon.c.
Quoted 42 lines
Documentation/hwmon/sht3x new file +36
-0@@ -0,0 +1,66 @@ +Kernel driver sht3x +=================== + +Supported chips: + * Sensirion SHT3x-DIS + Prefix: 'sht3x' + Addresses scanned: none + Datasheet: http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_Datasheet_SHT3x_DIS.pdf + +Author: + David Frey <[email protected]> + Pascal Sachs <[email protected]> + +Description +----------- + +This driver implements support for the Sensirion SHT3x-DIS chip, a humidity +and temperature sensor. Temperature is measured in degrees celsius, relative +humidity is expressed as a percentage. In the sysfs interface, all values are +scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +The device communicates with the I2C protocol. Sensors can have the I2C +addresses 0x44 or 0x45, depending on the wiring. See +Documentation/i2c/instantiating-devices for methods to instantiate the device. + +There are two options configurable by means of sht3x_platform_data: +1. blocking (pull the I2C clock line down while performing the measurement) or + non-blocking mode. Blocking mode will guarantee the fastest result but + the I2C bus will be busy during that time. By default, non-blocking mode + is used. Make sure clock-stretching works properly on your device if you + want to use blocking mode. +2. high or low accuracy. High accuracy is used by default and using it is + strongly recommended. + +The sht3x sensor supports a single shot mode as well as 5 periodic measure +modes, which can be controlled with the frequency sysfs interface.
Documentation/hwmon/sht3x line 46
Quoted 10 lines
Documentation/hwmon/sht3x new file +10
-0+The allowed frequencies are as follows: + * 0 single shot mode + * 500 0.5 Hz periodic measurement + * 1000 1 Hz periodic measurement + * 2000 2 Hz periodic measurement + * 4000 4 Hz periodic measurement + * 10000 10 Hz periodic measurement + +While in periodic measure mode, read out of humidity and temperature values are +not supported. Nevertheless it is possible to read out the values with maximal
Really ? I seem to be missing this in the datasheet. Section 4.4 suggests that both are supported. Besides, this would be really odd - what would be the point of periodic mode (or any mode, for that matter) if it doesn't really measure anything ?
Documentation/hwmon/sht3x line 47
Quoted 1 line
Documentation/hwmon/sht3x new file +1
-0+the same frequency as the periodic measure mode.
This text does not really make sense to me. Should one of those "periodic" be "single shot" ?
Quoted 6 lines
Documentation/hwmon/sht3x new file +6
-0+ +sysfs-Interface +--------------- + +temp1_input: temperature input +humidity1_input: humidity input
Documentation/hwmon/sht3x line 63
Quoted 10 lines
Documentation/hwmon/sht3x new file +10
-0+temp1_max: temperature max value +temp1_max_hyst: temperature hysteresis value for max limit +humidity1_max: humidity max value +humidity1_max_hyst: humidity hysteresis value for max limit +temp1_min: temperature min value +temp1_min_hyst: temperature hysteresis value for min limit +humidity1_min: humidity min value +humidity1_min_hyst: humidity hysteresis value for min limit +frequency: Measurement frequency, 0 for single shot, frequency in + mHz for periodic measurement
Please use standard attributes (update_interval).
Documentation/hwmon/sht3x line 66
Quoted 3 lines
Documentation/hwmon/sht3x new file +3
-0+soft_reset: Soft reset the internal state of the sensor, clear the + status register, clear the alert and switch back to + single shot mode
Makes me really unhappy. It is not a standard attribute, there is no clear indication why it is needed, and it affects other attributes (mode) without updating the command pointer, thus probably breaking things.
In general, if the chip is that unstable that it needs to be reset once in a while, that should be auto-detected if possible and be handled automatically. A manual "please reset me" attribute is the worst possible solution and should only be used if absolutely necessary.
drivers/hwmon/Kconfig config SENSORS_SHT21 · line 1259
Quoted 11 lines
drivers/hwmon/Kconfig +3
-0@@ -1254,6 +1254,16 @@ config SENSORS_SHT21 This driver can also be built as a module. If so, the module will be called sht21. +config SENSORS_SHT3x + tristate "Sensiron humidity and temperature sensors. SHT3x and compat." + depends on I2C && CRC8
Per lib/Kconfig, drivers are supposed to select CRC8 if it is needed.
[ Kind of surprising that it doesn't seem to be used anywhere, but maybe
I am missing something. ]
Quoted 76 lines
drivers/hwmon/Kconfig +7
-0+ help + If you say yes here you get support for the Sensiron SHT30 and SHT31 + humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht3x. + config SENSORS_SHTC1 tristate "Sensiron humidity and temperature sensors. SHTC1 and compat." depends on I2Cdrivers/hwmon/Makefile +1
-0@@ -137,6 +137,7 @@ obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o +obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.odrivers/hwmon/sht3x.c new file +48
-0@@ -0,0 +1,675 @@ +/* Sensirion SHT3x-DIS humidity and temperature sensor driver. +* The SHT3x comes in many different versions, this driver is for the +* I2C version only. +* +* Copyright (C) 2016 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#include <asm/page.h> +#include <linux/crc8.h> +#include <linux/delay.h> +#include <linux/err.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/i2c.h> +#include <linux/init.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/platform_data/sht3x.h> + +/* commands (high precision mode) */ +static const unsigned char sht3x_cmd_measure_blocking_hpm[] = {0x2c ,0x06 }; +static const unsigned char sht3x_cmd_measure_nonblocking_hpm[] = {0x24 ,0x00 }; + +/* commands (low power mode) */ +static const unsigned char sht3x_cmd_measure_blocking_lpm[] = {0x2c ,0x10 }; +static const unsigned char sht3x_cmd_measure_nonblocking_lpm[] = {0x24 ,0x16 }; + +/* commands for periodic mode */ +static const unsigned char sht3x_cmd_measure_periodic_mode[] = {0xe0 ,0x00 }; +static const unsigned char sht3x_cmd_break[] = {0x30 ,0x93 }; + +/* other commands */ +static const unsigned char sht3x_cmd_clear_status_reg[] = {0x30 ,0x41 }; +static const unsigned char sht3x_cmd_soft_reset[] = {0x30 ,0xa2 };
drivers/hwmon/sht3x.c line 58
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ +/* delays for non-blocking i2c commands, both in us */ +#define SHT3X_NONBLOCKING_WAIT_TIME_HPM 15000 +#define SHT3X_NONBLOCKING_WAIT_TIME_LPM 4000 + +#define SHT3X_WORD_LEN 2 +#define SHT3X_CMD_LENGTH 2 +#define SHT3X_CRC8_LEN 1 +#define SHT3X_RESPONSE_LENGTH 6 +#define SHT3X_CRC8_LEN 1
Defined twice.
drivers/hwmon/sht3x.c line 62
Quoted 4 lines
drivers/hwmon/sht3x.c new file +4
-0+#define SHT3X_CRC8_POLYNOMIAL 0x31 +#define SHT3X_CRC8_INIT 0xFF +#define SHT3X_ID_SHT 0 +#define SHT3X_ID_STS 1
Please use an enum for the IDs.
Quoted 64 lines
drivers/hwmon/sht3x.c new file +64
-0+ +DECLARE_CRC8_TABLE (sht3x_crc8_table); + +/* periodic measure commands (high precision mode) */ +static const char periodic_measure_commands_hpm[][SHT3X_CMD_LENGTH ] = { +/* 0.5 measurements per second */ + {0x20 ,0x32 }, +/* 1 measurements per second */ + {0x21 ,0x30 }, +/* 2 measurements per second */ + {0x22 ,0x36 }, +/* 4 measurements per second */ + {0x23 ,0x34 }, +/* 10 measurements per second */ + {0x27 ,0x37 }, +}; + +/* periodic measure commands (low power mode) */ +static const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH ] = { +/* 0.5 measurements per second */ + {0x20 ,0x2f }, +/* 1 measurements per second */ + {0x21 ,0x2d }, +/* 2 measurements per second */ + {0x22 ,0x2b }, +/* 4 measurements per second */ + {0x23 ,0x29 }, +/* 10 measurements per second */ + {0x27 ,0x2a }, +}; + +struct sht3x_alert_commands { +const char read_command[SHT3X_CMD_LENGTH ]; +const char write_command[SHT3X_CMD_LENGTH ]; +}; + +const struct sht3x_alert_commands alert_commands[] = { +/* temp1_max, humidity1_max */ + { {0xe1 ,0x1f }, {0x61 ,0x1d } }, +/* temp_1_max_hyst, humidity1_max_hyst */ + { {0xe1 ,0x14 }, {0x61 ,0x16 } }, +/* temp1_min, humidity1_min */ + { {0xe1 ,0x02 }, {0x61 ,0x00 } }, +/* temp_1_min_hyst, humidity1_min_hyst */ + { {0xe1 ,0x09 }, {0x61 ,0x0B } }, +}; + + +static const u16 mode_to_frequency[] = { +0 , +500 , +1000 , +2000 , +4000 , +10000 , +}; + +struct sht3x_data { +struct i2c_client *client; +struct mutex update_lock; + +u8 mode; +const unsigned char *command; +u32 wait_time;/* in us*/
drivers/hwmon/sht3x.c line 136
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ +struct sht3x_platform_data setup; + +int temperature;/* 1000 * temperature in dgr C */ +u32 humidity;/* 1000 * relative humidity in %RH */ +}; + + +static int find_index (const u16 *list,size_t size,u16 value) +{
Two of those parameters are really constants, since only mode_to_frequency[] and its size are passed as argument. Please reference the array directly.
drivers/hwmon/sht3x.c line 142
Quoted 6 lines
drivers/hwmon/sht3x.c new file +6
-0+size_t index =0 ; + +while (index < size && list[index] != value) + index++; + +return index == size ?-1 : index;
If you return an error, please use a proper error code, and pass it on in the calling code.
I would suggest, however, to implement a best-match algorithm instead of expecting the user to find valid values.
Quoted 63 lines
drivers/hwmon/sht3x.c new file +63
-0+} + +static int sht3x_read_from_command (struct i2c_client *client, +struct sht3x_data *data, +const char *command, +char *buf,int length,u32 wait_time) +{ +int ret; + +mutex_lock (&data->update_lock); + + ret =i2c_master_send (client, command,SHT3X_CMD_LENGTH ); + +if (ret !=SHT3X_CMD_LENGTH ) { + ret = ret <0 ? ret : -EIO ; + goto out; + } + +if (wait_time) +usleep_range (wait_time, wait_time +1000 ); + + ret =i2c_master_recv (client, buf, length); +if (ret != length) { + ret = ret <0 ? ret : -EIO ; + goto out; + } + + ret =0 ; +out: +mutex_unlock (&data->update_lock); +return ret; + +} + +static int sht3x_extract_temperature (u16 raw) +{ +/* +* From datasheet: +* T = -45 + 175 * ST / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return ((21875 * (int )raw) >>13 ) -45000 ; +} + +static u32 sht3x_extract_humidity (u16 raw) +{ +/* +* From datasheet: +* RH = 100 * SRH / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return (12500 * (int )raw) >>13 ; +} + +/* sysfs attributes */ +static struct sht3x_data *sht3x_update_client (struct device *dev) +{ +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +unsigned char buf[SHT3X_RESPONSE_LENGTH ]; +u16 val; +int ret; +
drivers/hwmon/sht3x.c line 215
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ ret =sht3x_read_from_command (client, data, data->command, + buf,sizeof (buf), data->wait_time); +if (ret) +return ERR_PTR (ret); + + val =be16_to_cpup ((__be16 *) buf); + data->temperature =sht3x_extract_temperature (val); + val =be16_to_cpup ((__be16 *) (buf +3 )); + data->humidity =sht3x_extract_humidity (val); +
Having this code not mutex-protected means that the variables can have more or less random values if there are multiple readers.
Quoted 30 lines
drivers/hwmon/sht3x.c new file +30
-0+return data; +} + +static ssize_t temp1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->temperature); +} + +static ssize_t humidity1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->humidity); +} + +static int alert_read_raw (struct device *dev, +struct device_attribute *attr, +char *buffer,int length) +{ +int ret;
drivers/hwmon/sht3x.c line 255
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+u8 index; +const struct sht3x_alert_commands *commands; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + + index =to_sensor_dev_attr (attr)->index; + commands = &alert_commands[index]; + + ret =sht3x_read_from_command (client, data, commands->read_command, + buffer, length,0 );
Normally, limit temperatures don't change. Reading it from the chip repeatedly doesn't really add much if any value, and in this case will cause substantial latencies. Why not just cache the values ? This would also simplify and speed up changing values a lot.
Quoted 96 lines
drivers/hwmon/sht3x.c new file +96
-0+ +return ret; +} + +static ssize_t temp1_alert_read (struct device *dev, +struct device_attribute *attr, +int *temperature) +{ +int ret; +u16 raw; +char buffer[SHT3X_RESPONSE_LENGTH ]; + + ret =alert_read_raw (dev, attr, buffer,SHT3X_RESPONSE_LENGTH ); +if (ret) +return ret; + + raw =be16_to_cpup ((__be16 *) buffer); + +/* +* lower 9 bits are temperature MSB +*/ + *temperature =sht3x_extract_temperature ((raw &0x01ff ) <<7 ); + +return 0 ; +} + + +static ssize_t temp1_alert_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int ret; +int temperature; + + ret =temp1_alert_read (dev, attr, &temperature); +if (ret) +return ret; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , temperature); +} + + +static ssize_t humidity1_alert_read (struct device *dev, +struct device_attribute *attr, +u32 *humidity) +{ +int ret; +u16 raw; +char buffer[SHT3X_RESPONSE_LENGTH ]; + + ret =alert_read_raw (dev, attr, buffer,SHT3X_RESPONSE_LENGTH ); +if (ret) +return ret; + + raw =be16_to_cpup ((__be16 *) buffer); + +/* +* top 7 bits are the humidity MSB, +*/ + *humidity =sht3x_extract_humidity (raw &0xfe00 ); + +return 0 ; +} + +static ssize_t humidity1_alert_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int ret; +u32 humidity; + + ret =humidity1_alert_read (dev, attr, &humidity); +if (ret) +return ret; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , humidity); +} + +static size_t alert_store (struct device *dev, +struct device_attribute *attr, +size_t count, +int temperature, +u32 humidity) +{ +char buffer[SHT3X_CMD_LENGTH +SHT3X_WORD_LEN +SHT3X_CRC8_LEN ]; +char *position = buffer; +int ret; +u16 raw; +u8 index; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +const struct sht3x_alert_commands *commands; + + index =to_sensor_dev_attr (attr)->index; + commands = &alert_commands[index]; +
drivers/hwmon/sht3x.c line 361
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+memcpy (position, commands->write_command,SHT3X_CMD_LENGTH ); + position +=SHT3X_CMD_LENGTH ; +/* +* ST = (T + 45) / 175 * 2^16 +* SRH = RH / 100 * 2^16 +* adapted for fixed point arithmetic and packed the same as +* in alert_show() +*/ + raw = ((u32)(temperature +45000 ) *24543 ) >> (16 +7 ); + raw |= (((u32 )humidity *42950 ) >>16 ) &0xfe00 ;
humidity is already u32.
drivers/hwmon/sht3x.c line 366
Quoted 5 lines
drivers/hwmon/sht3x.c new file +5
-0+ + *(__be16 *)(position) =cpu_to_be16 (raw); + position +=SHT3X_WORD_LEN ; + *position = crc8(sht3x_crc8_table, + (position - SHT3X_WORD_LEN),
Unnecessary ( )
Quoted 144 lines
drivers/hwmon/sht3x.c new file +144
-0+SHT3X_WORD_LEN , + SHT3X_CRC8_INIT); + + + ret =i2c_master_send (client, buffer,sizeof (buffer)); +if (ret !=sizeof (buffer)) +return ret <0 ? ret : -EIO ; + +return count; + +} + +static ssize_t temp1_alert_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) +{ +int temperature; +u32 humidity; +int ret; + + ret =kstrtoint (buf,0 , &temperature); + +if (ret) +return -EINVAL ; + + temperature =clamp_val (temperature,-45000 ,130000 ); + +/* +* use old humidity alert value since we can only update +* temperature and humidity alerts together +*/ + ret =humidity1_alert_read (dev, attr, &humidity); +if (ret) +return ret; + + ret =alert_store (dev, attr, count, temperature, humidity); + +return ret; +} + +static ssize_t humidity1_alert_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) +{ +int temperature; +u32 humidity; +int ret; + + ret =kstrtou32 (buf,0 , &humidity); + +if (ret) +return -EINVAL ; + + humidity =clamp_val (humidity,0 ,100000 ); + +/* +* use old temperature alert value since we can only update +* temperature and humidity alerts together +*/ + ret =temp1_alert_read (dev, attr, &temperature); +if (ret) +return ret; + + ret =alert_store (dev, attr, count, temperature, humidity); + +return ret; +} + +static void sht3x_select_command (struct sht3x_data *data) +{ +/* +* In blocking mode (clock stretching mode) the I2C bus +* is blocked for other traffic, thus the call to i2c_master_recv() +* will wait until the data is ready. For non blocking mode, we +* have to wait ourselves. +*/ +if (data->mode >0 ) { + data->command = sht3x_cmd_measure_periodic_mode; + data->wait_time =0 ; + }else if (data->setup.blocking_io) { + data->command = data->setup.high_precision ? + sht3x_cmd_measure_blocking_hpm : + sht3x_cmd_measure_blocking_lpm; + data->wait_time =0 ; + }else { +if (data->setup.high_precision) { + data->command = sht3x_cmd_measure_nonblocking_hpm; + data->wait_time =SHT3X_NONBLOCKING_WAIT_TIME_HPM ; + }else { + data->command = sht3x_cmd_measure_nonblocking_lpm; + data->wait_time =SHT3X_NONBLOCKING_WAIT_TIME_LPM ; + } + } +} + +static ssize_t frequency_show (struct device *dev, +struct device_attribute *attr, +char *buf) { +struct sht3x_data *data =dev_get_drvdata (dev); + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , mode_to_frequency[data->mode]); +} + +static ssize_t frequency_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) { +u16 frequency; +int mode; +int ret; +const char *command; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + + ret =kstrtou16 (buf,0 , &frequency); + +if (ret) +return -EINVAL ; + + mode =find_index (mode_to_frequency,sizeof (mode_to_frequency), + frequency); + +/* invalid frequency */ +if (mode <0 ) +return -EINVAL ; + +/* mode did not change */ +if (mode == data->mode) +return count; + +/* abort periodic measure */ + ret =i2c_master_send (client, sht3x_cmd_break,SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -EIO ; + data->mode =0 ; + +if (mode >0 ) { +if (data->setup.high_precision) + command = periodic_measure_commands_hpm[mode -1 ]; +else + command = periodic_measure_commands_lpm[mode -1 ]; +
drivers/hwmon/sht3x.c line 520
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+/* select mode */ + ret =i2c_master_send (client, command,SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -EIO ; + } + +/* select mode and command */ + data->mode = mode; +sht3x_select_command (data); +
All that unprotected ? What happens if multiple processes write different values in parallel ?
Quoted 60 lines
drivers/hwmon/sht3x.c new file +60
-0+return count; +} + +static ssize_t soft_reset (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) { +int ret; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + +/* break if in periodic measure mode */ +if (data->mode >0 ) { + ret =i2c_master_send (client, sht3x_cmd_break, +SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -EIO ; + data->mode =0 ; + } + +/* clear status register */ + ret =i2c_master_send (client, sht3x_cmd_clear_status_reg, +SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -EIO ; + +/* soft reset */ + ret =i2c_master_send (client, sht3x_cmd_soft_reset,SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -EIO ; + +return count; +} + +static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, temp1_input_show, NULL, 0); +static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, humidity1_input_show, + NULL, 0); +static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, temp1_alert_show, + temp1_alert_store, 0); +static SENSOR_DEVICE_ATTR(humidity1_max, S_IRUGO | S_IWUSR, + humidity1_alert_show, humidity1_alert_store, 0); +static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, temp1_alert_show, + temp1_alert_store, 1); +static SENSOR_DEVICE_ATTR(humidity1_max_hyst, S_IRUGO | S_IWUSR, + humidity1_alert_show, humidity1_alert_store, 1); +static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, temp1_alert_show, + temp1_alert_store, 2); +static SENSOR_DEVICE_ATTR(humidity1_min, S_IRUGO | S_IWUSR, + humidity1_alert_show, humidity1_alert_store, 2); +static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO | S_IWUSR, + temp1_alert_show, temp1_alert_store, 3); +static SENSOR_DEVICE_ATTR(humidity1_min_hyst, S_IRUGO | S_IWUSR, + humidity1_alert_show, humidity1_alert_store, 3); +static SENSOR_DEVICE_ATTR(frequency, S_IRUGO | S_IWUSR, frequency_show, + frequency_store, 0); +static SENSOR_DEVICE_ATTR(soft_reset, S_IWUSR, NULL, soft_reset, 0); + +static struct attribute *sht3x_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, + &sensor_dev_attr_humidity1_input.dev_attr.attr,
drivers/hwmon/sht3x.c line 590
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ &sensor_dev_attr_temp1_max.dev_attr.attr, + &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, + &sensor_dev_attr_humidity1_max.dev_attr.attr, + &sensor_dev_attr_humidity1_max_hyst.dev_attr.attr, + &sensor_dev_attr_temp1_min.dev_attr.attr, + &sensor_dev_attr_temp1_min_hyst.dev_attr.attr, + &sensor_dev_attr_humidity1_min.dev_attr.attr, + &sensor_dev_attr_humidity1_min_hyst.dev_attr.attr, + &sensor_dev_attr_frequency.dev_attr.attr, + &sensor_dev_attr_soft_reset.dev_attr.attr,
Limit attributes without alarm attributes is kind of unusual. Looking into the datasheet, the chip does support a status register with alert bits. Any reason for not providing alert attributes ?
Quoted 90 lines
drivers/hwmon/sht3x.c new file +85
-0+ NULL +}; + +static struct attribute *sts3x_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, +NULL +}; + +ATTRIBUTE_GROUPS (sht3x); +ATTRIBUTE_GROUPS (sts3x); + +static int sht3x_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +int ret; +struct sht3x_data *data; +struct device *hwmon_dev; +struct i2c_adapter *adap = client->adapter; +struct device *dev = &client->dev; +const struct attribute_group **attribute_groups; + +/* +* we require full i2c support since the sht3x uses multi-byte read and +* writes as well as multi-byte commands which are not supported by +* the smbus protocol +*/ +if (!i2c_check_functionality (adap,I2C_FUNC_I2C )) +return -ENODEV ; + + ret =i2c_master_send (client, sht3x_cmd_clear_status_reg, +SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -ENODEV ; + + data =devm_kzalloc (dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->setup.blocking_io = false; + data->setup.high_precision = true; + data->mode =0 ; + data->client = client; +crc8_populate_msb (sht3x_crc8_table,SHT3X_CRC8_POLYNOMIAL ); + +if (client->dev.platform_data) + data->setup = *(struct sht3x_platform_data *)dev->platform_data; +sht3x_select_command (data); +mutex_init (&data->update_lock); + +if (id->driver_data ==SHT3X_ID_STS ) + attribute_groups = sts3x_groups; +else + attribute_groups = sht3x_groups; + + hwmon_dev =devm_hwmon_device_register_with_groups (dev, + client->name, + data, attribute_groups); + +if (IS_ERR (hwmon_dev)) +dev_dbg (dev,"unable to register hwmon device\n" ); + +return PTR_ERR_OR_ZERO (hwmon_dev); +} + +/* device ID table */ +static const struct i2c_device_id sht3x_id[] = { + {"sht3x" ,SHT3X_ID_SHT }, + {"sts3x" ,SHT3X_ID_STS }, + {} +}; + +MODULE_DEVICE_TABLE (i2c, sht3x_id); + +static struct i2c_driver sht3x_i2c_driver = { + .driver.name ="sht3x" , + .probe = sht3x_probe, + .id_table = sht3x_id, +}; + +module_i2c_driver (sht3x_i2c_driver); + +MODULE_AUTHOR ("David Frey <[email protected]>" ); +MODULE_AUTHOR ("Pascal Sachs <[email protected]>" ); +MODULE_DESCRIPTION ("Sensirion SHT3x humidity and temperature sensor driver" ); +MODULE_LICENSE ("GPL" );drivers/hwmon/twl4030-madc-hwmon.c +0
-0
drivers/hwmon/twl4030-madc-hwmon.c line 28
Quoted 4 lines
drivers/hwmon/twl4030-madc-hwmon.c +1
-0@@ -25,6 +25,7 @@*/ #include <linux/init.h> #include <linux/module.h> +//FIXME this is used for clamp_val
Drop.
Quoted 35 lines
drivers/hwmon/twl4030-madc-hwmon.c +0
-0#include <linux/kernel.h> #include <linux/i2c/twl.h> #include <linux/device.h> include/linux/platform_data/sht3x.h new file +25
-0@@ -0,0 +1,25 @@ +/* +* Copyright (C) 2015 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef __SHT3X_H_ +#define __SHT3X_H_ + +struct sht3x_platform_data { +bool blocking_io; +bool high_precision; +}; +#endif /* __SHT3X_H_ */
Am 15.05.2016 um 03:34 schrieb Guenter Roeck:
Quoted 13 lines · Pascal Sachs
Quoted 12 lines
From: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500.
Signed-off-by Pascal Sachs <[email protected]>---
Patch was generated against kernel v4.6-rc5
Quoted 8 lines
Quoted 6 lines
Documentation/hwmon/sht3x | 66 ++++ drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/sht3x.c | 675 ++++++++++++++++++++++++++++++++++++ drivers/hwmon/twl4030-madc-hwmon.c | 1 +Please drop this change.
My bad, don't know how I could miss this :-(
Quoted 11 lines
Quoted 7 lines
include/linux/platform_data/sht3x.h | 25 ++ 6 files changed, 778 insertions(+)create mode 100644
Documentation/hwmon/sht3xcreate mode 100644drivers/hwmon/sht3x.ccreate mode 100644include/linux/platform_data/sht3x.hPlease run checkpatch --strict over your patch. There are lots of unnecessary/undesirable empty lines and spaces. It would also tell you about the presumably left-over FIXME in
twl4030-madc-hwmon.c.
Thank you, I added the --strict argument to our tests, so that we don't miss such things in the future
Quoted 67 lines
Quoted 63 lines
Documentation/hwmon/sht3x new file +8
-0@@ -0,0 +1,66 @@ +Kernel driver sht3x +=================== + +Supported chips: + * Sensirion SHT3x-DIS + Prefix: 'sht3x' + Addresses scanned: none + Datasheet:Documentation/hwmon/sht3x new file +9
-0+ +Author: + David Frey <[email protected]> + Pascal Sachs <[email protected]> + +Description +----------- + +This driver implements support for the Sensirion SHT3x-DIS chip, ahumidity
+and temperature sensor. Temperature is measured in degrees celsius, relative
+humidity is expressed as a percentage. In the sysfs interface, all values areDocumentation/hwmon/sht3x new file +5
-0+scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +The device communicates with the I2C protocol. Sensors can have the I2C +addresses 0x44 or 0x45, depending on the wiring. See +Documentation/i2c/instantiating-devices for methods to instantiatethe device.
Documentation/hwmon/sht3x new file +3
-0+ +There are two options configurable by means of sht3x_platform_data: +1. blocking (pull the I2C clock line down while performing themeasurement) or
+ non-blocking mode. Blocking mode will guarantee the fastest
result but
+ the I2C bus will be busy during that time. By default,
non-blocking mode
+ is used. Make sure clock-stretching works properly on your device
if you
+ want to use blocking mode.
+2. high or low accuracy. High accuracy is used by default and using it isDocumentation/hwmon/sht3x new file +3
-0+ strongly recommended. + +The sht3x sensor supports a single shot mode as well as 5 periodicmeasure
Documentation/hwmon/sht3x new file +10
-0+modes, which can be controlled with the frequency sysfs interface. +The allowed frequencies are as follows: + * 0 single shot mode + * 500 0.5 Hz periodic measurement + * 1000 1 Hz periodic measurement + * 2000 2 Hz periodic measurement + * 4000 4 Hz periodic measurement + * 10000 10 Hz periodic measurement + +While in periodic measure mode, read out of humidity and temperatureQuoted 3 lines
values are
+not supported. Nevertheless it is possible to read out the values with maximal
Quoted 7 lines
Really ? I seem to be missing this in the datasheet. Section 4.4 suggests that
both are supported. Besides, this would be really odd - what would be the point
of periodic mode (or any mode, for that matter) if it doesn't really measure
anything ?
When you start the periodic measurement, the sensor will measure independently
in the background and keep up to 8 values in an internal buffer. Once the client
reads out the buffer, the sensor will invalidate the buffer and therefore return
an error on the I2C bus until at least one new measurement was generated.
This feature is used to automatically set the alert bit according to the configured
limits and hysteresis values.
Our Product Manager for the SHT3x sensor told me to not handle this behavior in
the driver by e.g. caching the last value, since it will confuse our customers when the
hardware behaves differently then the driver.
If you have an other opinion and would like to share it, I'm open for any input.
Quoted 6 lines
Quoted 2 lines
+the same frequency as the periodic measure mode.
This text does not really make sense to me. Should one of those "periodic" be
"single shot" ?
See above for an explanation. I will update the wording in the documentation in the next patch.
Quoted 12 lines
Quoted 12 lines
Documentation/hwmon/sht3x new file +11
-0+ +sysfs-Interface +--------------- + +temp1_input: temperature input +humidity1_input: humidity input +temp1_max: temperature max value +temp1_max_hyst: temperature hysteresis value for max limit +humidity1_max: humidity max value +humidity1_max_hyst: humidity hysteresis value for max limit +temp1_min: temperature min value
Quoted 8 lines
Quoted 6 lines
Documentation/hwmon/sht3x new file +4
-0+temp1_min_hyst: temperature hysteresis value for min limit +humidity1_min: humidity min value +humidity1_min_hyst: humidity hysteresis value for min limit +frequency: Measurement frequency, 0 for single shot,frequency in
+ mHz for periodic measurementPlease use standard attributes (
update_interval).
OK
Quoted 11 lines
Documentation/hwmon/sht3x line 66
Quoted 5 lines
+soft_reset: Soft reset the internal state of the sensor,
clear theDocumentation/hwmon/sht3x new file +2
-0+ status register, clear the alert and switch back to + single shot modeMakes me really unhappy. It is not a standard attribute, there is no clear
indication why it is needed, and it affects other attributes (mode) without updating the command pointer, thus probably breaking things.
Quoted 4 lines
In general, if the chip is that unstable that it needs to be reset once in a while, that should be auto-detected if possible and be handled automatically. A manual "please reset me" attribute is the worst possible solution and should only be used if absolutely necessary.
The soft_reset is actually not really needed. It's just a way for the user to
clear his configuration. I will remove this interface if it makes you unhappy.
Quoted 16 lines
Quoted 11 lines
drivers/hwmon/Kconfig +2
-0@@ -1254,6 +1254,16 @@ config SENSORS_SHT21 This driver can also be built as a module. If so, the module will be called sht21. +config SENSORS_SHT3x + tristate "Sensiron humidity and temperature sensors. SHT3x andQuoted 2 lines
compat."
+ depends on I2C && CRC8Per lib/Kconfig, drivers are supposed to select CRC8 if it is needed.
Quoted 2 lines
[ Kind of surprising that it doesn't seem to be used anywhere, but maybe
I am missing something. ]
I was surprised as well, but it seems that no other driver is using the CRC8 module
by now. I only saw some older drivers which implement a crc8 lookup table by themself.
Maybe I'm missing something as well
Quoted 91 lines
Quoted 91 lines
drivers/hwmon/Kconfig +2
-0+ help + If you say yes here you get support for the Sensiron SHT30 andSHT31
drivers/hwmon/Kconfig +5
-0+ humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht3x. + config SENSORS_SHTC1 tristate "Sensiron humidity and temperature sensors. SHTC1 andcompat."
depends on I2Cdrivers/hwmon/Makefile +1
-0@@ -137,6 +137,7 @@ obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o +obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.odrivers/hwmon/sht3x.c new file +35
-0@@ -0,0 +1,675 @@ +/* Sensirion SHT3x-DIS humidity and temperature sensor driver. +* The SHT3x comes in many different versions, this driver is for the +* I2C version only. +* +* Copyright (C) 2016 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#include <asm/page.h> +#include <linux/crc8.h> +#include <linux/delay.h> +#include <linux/err.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/i2c.h> +#include <linux/init.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/platform_data/sht3x.h> + +/* commands (high precision mode) */ +static const unsigned char sht3x_cmd_measure_blocking_hpm[] = {
0x2c,0x06};
+static const unsigned char sht3x_cmd_measure_nonblocking_hpm[] = {0x24,0x00};drivers/hwmon/sht3x.c new file +3
-0+ +/* commands (low power mode) */ +static const unsigned char sht3x_cmd_measure_blocking_lpm[] = {
0x2c,0x10};
+static const unsigned char sht3x_cmd_measure_nonblocking_lpm[] = {0x24,0x16};drivers/hwmon/sht3x.c new file +3
-0+ +/* commands for periodic mode */ +static const unsigned char sht3x_cmd_measure_periodic_mode[] = {
0xe0,0x00};
+static const unsigned char sht3x_cmd_break[] = {0x30,0x93};drivers/hwmon/sht3x.c new file +3
-0+ +/* other commands */ +static const unsigned char sht3x_cmd_clear_status_reg[] = {
0x30,0x41};
+static const unsigned char sht3x_cmd_soft_reset[] = {0x30,0xa2};drivers/hwmon/sht3x.c new file +4
-0+ +/* delays for non-blocking i2c commands, both in us */ +#define SHT3X_NONBLOCKING_WAIT_TIME_HPM 15000 +#define SHT3X_NONBLOCKING_WAIT_TIME_LPM 4000
Quoted 8 lines
drivers/hwmon/sht3x.c line 58
Quoted 6 lines
drivers/hwmon/sht3x.c new file +6
-0+ +#define SHT3X_WORD_LEN 2 +#define SHT3X_CMD_LENGTH 2 +#define SHT3X_CRC8_LEN 1 +#define SHT3X_RESPONSE_LENGTH 6 +#define SHT3X_CRC8_LEN 1Defined twice.
Oops
Quoted 6 lines
drivers/hwmon/sht3x.c line 62
Quoted 4 lines
drivers/hwmon/sht3x.c new file +4
-0+#define SHT3X_CRC8_POLYNOMIAL 0x31 +#define SHT3X_CRC8_INIT 0xFF +#define SHT3X_ID_SHT 0 +#define SHT3X_ID_STS 1Please use an enum for the IDs.
Ok, makes sense
Quoted 89 lines
Quoted 64 lines
drivers/hwmon/sht3x.c new file +64
-0+ +DECLARE_CRC8_TABLE (sht3x_crc8_table); + +/* periodic measure commands (high precision mode) */ +static const char periodic_measure_commands_hpm[][SHT3X_CMD_LENGTH ] = { +/* 0.5 measurements per second */ + {0x20 ,0x32 }, +/* 1 measurements per second */ + {0x21 ,0x30 }, +/* 2 measurements per second */ + {0x22 ,0x36 }, +/* 4 measurements per second */ + {0x23 ,0x34 }, +/* 10 measurements per second */ + {0x27 ,0x37 }, +}; + +/* periodic measure commands (low power mode) */ +static const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH ] = { +/* 0.5 measurements per second */ + {0x20 ,0x2f }, +/* 1 measurements per second */ + {0x21 ,0x2d }, +/* 2 measurements per second */ + {0x22 ,0x2b }, +/* 4 measurements per second */ + {0x23 ,0x29 }, +/* 10 measurements per second */ + {0x27 ,0x2a }, +}; + +struct sht3x_alert_commands { +const char read_command[SHT3X_CMD_LENGTH ]; +const char write_command[SHT3X_CMD_LENGTH ]; +}; + +const struct sht3x_alert_commands alert_commands[] = { +/* temp1_max, humidity1_max */ + { {0xe1 ,0x1f }, {0x61 ,0x1d } }, +/* temp_1_max_hyst, humidity1_max_hyst */ + { {0xe1 ,0x14 }, {0x61 ,0x16 } }, +/* temp1_min, humidity1_min */ + { {0xe1 ,0x02 }, {0x61 ,0x00 } }, +/* temp_1_min_hyst, humidity1_min_hyst */ + { {0xe1 ,0x09 }, {0x61 ,0x0B } }, +}; + + +static const u16 mode_to_frequency[] = { +0 , +500 , +1000 , +2000 , +4000 , +10000 , +}; + +struct sht3x_data { +struct i2c_client *client; +struct mutex update_lock; + +u8 mode; +const unsigned char *command; +u32 wait_time;/* in us*/ drivers/hwmon/sht3x.c line 136
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ +struct sht3x_platform_data setup; + +int temperature;/* 1000 * temperature in dgr C */ +u32 humidity;/* 1000 * relative humidity in %RH */ +}; + + +static int find_index (const u16 *list,size_t size,u16 value) +{Two of those parameters are really constants, since only mode_to_frequency[]
and its size are passed as argument. Please reference the array directly.drivers/hwmon/sht3x.c line 142
Quoted 6 lines
drivers/hwmon/sht3x.c new file +6
-0+size_t index =0 ; + +while (index < size && list[index] != value) + index++; + +return index == size ?-1 : index;If you return an error, please use a proper error code, and pass it on in the calling code.
Quoted 2 lines
I would suggest, however, to implement a best-match algorithm instead of expecting the user to find valid values.
Ok, will fix it
Quoted 68 lines
Quoted 68 lines
drivers/hwmon/sht3x.c new file +67
-0+} + +static int sht3x_read_from_command (struct i2c_client *client, +struct sht3x_data *data, +const char *command, +char *buf,int length,u32 wait_time) +{ +int ret; + +mutex_lock (&data->update_lock); + + ret =i2c_master_send (client, command,SHT3X_CMD_LENGTH ); + +if (ret !=SHT3X_CMD_LENGTH ) { + ret = ret <0 ? ret : -EIO ; + goto out; + } + +if (wait_time) +usleep_range (wait_time, wait_time +1000 ); + + ret =i2c_master_recv (client, buf, length); +if (ret != length) { + ret = ret <0 ? ret : -EIO ; + goto out; + } + + ret =0 ; +out: +mutex_unlock (&data->update_lock); +return ret; + +} + +static int sht3x_extract_temperature (u16 raw) +{ +/* +* From datasheet: +* T = -45 + 175 * ST / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return ((21875 * (int )raw) >>13 ) -45000 ; +} + +static u32 sht3x_extract_humidity (u16 raw) +{ +/* +* From datasheet: +* RH = 100 * SRH / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return (12500 * (int )raw) >>13 ; +} + +/* sysfs attributes */ +static struct sht3x_data *sht3x_update_client (struct device *dev) +{ +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +unsigned char buf[SHT3X_RESPONSE_LENGTH ]; +u16 val; +int ret; + + ret =sht3x_read_from_command (client, data, data->command, + buf,sizeof (buf), data->wait_time); +if (ret) +return ERR_PTR (ret);
Quoted 9 lines
drivers/hwmon/sht3x.c line 215
Quoted 6 lines
drivers/hwmon/sht3x.c new file +6
-0+ + val =be16_to_cpup ((__be16 *) buf); + data->temperature =sht3x_extract_temperature (val); + val =be16_to_cpup ((__be16 *) (buf +3 )); + data->humidity =sht3x_extract_humidity (val); +Having this code not mutex-protected means that the variables can have more or less random values if there are multiple readers.
Good point
Quoted 37 lines
Quoted 37 lines
drivers/hwmon/sht3x.c new file +37
-0+return data; +} + +static ssize_t temp1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->temperature); +} + +static ssize_t humidity1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->humidity); +} + +static int alert_read_raw (struct device *dev, +struct device_attribute *attr, +char *buffer,int length) +{ +int ret; +u8 index; +const struct sht3x_alert_commands *commands; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + + index =to_sensor_dev_attr (attr)->index; + commands = &alert_commands[index];
Quoted 8 lines
drivers/hwmon/sht3x.c line 255
Quoted 3 lines
drivers/hwmon/sht3x.c new file +3
-0+ + ret =sht3x_read_from_command (client, data, commands->read_command, + buffer, length,0 );Normally, limit temperatures don't change. Reading it from the chip repeatedly doesn't really add much if any value, and in this case will cause substantial latencies. Why not just cache the values ? This would also simplify and speed up changing values a lot.
Yes, makes sense
Quoted 101 lines
Quoted 101 lines
drivers/hwmon/sht3x.c new file +100
-0+ +return ret; +} + +static ssize_t temp1_alert_read (struct device *dev, +struct device_attribute *attr, +int *temperature) +{ +int ret; +u16 raw; +char buffer[SHT3X_RESPONSE_LENGTH ]; + + ret =alert_read_raw (dev, attr, buffer,SHT3X_RESPONSE_LENGTH ); +if (ret) +return ret; + + raw =be16_to_cpup ((__be16 *) buffer); + +/* +* lower 9 bits are temperature MSB +*/ + *temperature =sht3x_extract_temperature ((raw &0x01ff ) <<7 ); + +return 0 ; +} + + +static ssize_t temp1_alert_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int ret; +int temperature; + + ret =temp1_alert_read (dev, attr, &temperature); +if (ret) +return ret; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , temperature); +} + + +static ssize_t humidity1_alert_read (struct device *dev, +struct device_attribute *attr, +u32 *humidity) +{ +int ret; +u16 raw; +char buffer[SHT3X_RESPONSE_LENGTH ]; + + ret =alert_read_raw (dev, attr, buffer,SHT3X_RESPONSE_LENGTH ); +if (ret) +return ret; + + raw =be16_to_cpup ((__be16 *) buffer); + +/* +* top 7 bits are the humidity MSB, +*/ + *humidity =sht3x_extract_humidity (raw &0xfe00 ); + +return 0 ; +} + +static ssize_t humidity1_alert_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int ret; +u32 humidity; + + ret =humidity1_alert_read (dev, attr, &humidity); +if (ret) +return ret; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , humidity); +} + +static size_t alert_store (struct device *dev, +struct device_attribute *attr, +size_t count, +int temperature, +u32 humidity) +{ +char buffer[SHT3X_CMD_LENGTH +SHT3X_WORD_LEN +SHT3X_CRC8_LEN ]; +char *position = buffer; +int ret; +u16 raw; +u8 index; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +const struct sht3x_alert_commands *commands; + + index =to_sensor_dev_attr (attr)->index; + commands = &alert_commands[index]; + +memcpy (position, commands->write_command,SHT3X_CMD_LENGTH ); + position +=SHT3X_CMD_LENGTH ; +/* +* ST = (T + 45) / 175 * 2^16
Quoted 8 lines
drivers/hwmon/sht3x.c line 361
Quoted 6 lines
drivers/hwmon/sht3x.c new file +6
-0+* SRH = RH / 100 * 2^16 +* adapted for fixed point arithmetic and packed the same as +* in alert_show() +*/ + raw = ((u32)(temperature +45000 ) *24543 ) >> (16 +7 ); + raw |= (((u32 )humidity *42950 ) >>16 ) &0xfe00 ;humidity is already u32.
OK
Quoted 9 lines
drivers/hwmon/sht3x.c line 366
Quoted 6 lines
drivers/hwmon/sht3x.c new file +5
-0+ + *(__be16 *)(position) =cpu_to_be16 (raw); + position +=SHT3X_WORD_LEN ; + *position = crc8(sht3x_crc8_table, + (position - SHT3X_WORD_LEN),Unnecessary ( )
OK
Quoted 149 lines
Quoted 149 lines
drivers/hwmon/sht3x.c new file +103
-0+SHT3X_WORD_LEN , + SHT3X_CRC8_INIT); + + + ret =i2c_master_send (client, buffer,sizeof (buffer)); +if (ret !=sizeof (buffer)) +return ret <0 ? ret : -EIO ; + +return count; + +} + +static ssize_t temp1_alert_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) +{ +int temperature; +u32 humidity; +int ret; + + ret =kstrtoint (buf,0 , &temperature); + +if (ret) +return -EINVAL ; + + temperature =clamp_val (temperature,-45000 ,130000 ); + +/* +* use old humidity alert value since we can only update +* temperature and humidity alerts together +*/ + ret =humidity1_alert_read (dev, attr, &humidity); +if (ret) +return ret; + + ret =alert_store (dev, attr, count, temperature, humidity); + +return ret; +} + +static ssize_t humidity1_alert_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) +{ +int temperature; +u32 humidity; +int ret; + + ret =kstrtou32 (buf,0 , &humidity); + +if (ret) +return -EINVAL ; + + humidity =clamp_val (humidity,0 ,100000 ); + +/* +* use old temperature alert value since we can only update +* temperature and humidity alerts together +*/ + ret =temp1_alert_read (dev, attr, &temperature); +if (ret) +return ret; + + ret =alert_store (dev, attr, count, temperature, humidity); + +return ret; +} + +static void sht3x_select_command (struct sht3x_data *data) +{ +/* +* In blocking mode (clock stretching mode) the I2C bus +* is blocked for other traffic, thus the call to i2c_master_recv() +* will wait until the data is ready. For non blocking mode, we +* have to wait ourselves. +*/ +if (data->mode >0 ) { + data->command = sht3x_cmd_measure_periodic_mode; + data->wait_time =0 ; + }else if (data->setup.blocking_io) { + data->command = data->setup.high_precision ? + sht3x_cmd_measure_blocking_hpm : + sht3x_cmd_measure_blocking_lpm; + data->wait_time =0 ; + }else { +if (data->setup.high_precision) { + data->command = sht3x_cmd_measure_nonblocking_hpm; + data->wait_time =SHT3X_NONBLOCKING_WAIT_TIME_HPM ; + }else { + data->command = sht3x_cmd_measure_nonblocking_lpm; + data->wait_time =SHT3X_NONBLOCKING_WAIT_TIME_LPM ; + } + } +} + +static ssize_t frequency_show (struct device *dev, +struct device_attribute *attr, +char *buf) { +struct sht3x_data *data =dev_get_drvdata (dev); + + return scnprintf(buf, PAGE_SIZE, "%d\n",mode_to_frequency[data->mode]);
drivers/hwmon/sht3x.c new file +45
-0+} + +static ssize_t frequency_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) { +u16 frequency; +int mode; +int ret; +const char *command; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + + ret =kstrtou16 (buf,0 , &frequency); + +if (ret) +return -EINVAL ; + + mode =find_index (mode_to_frequency,sizeof (mode_to_frequency), + frequency); + +/* invalid frequency */ +if (mode <0 ) +return -EINVAL ; + +/* mode did not change */ +if (mode == data->mode) +return count; + +/* abort periodic measure */ + ret =i2c_master_send (client, sht3x_cmd_break,SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -EIO ; + data->mode =0 ; + +if (mode >0 ) { +if (data->setup.high_precision) + command = periodic_measure_commands_hpm[mode -1 ]; +else + command = periodic_measure_commands_lpm[mode -1 ]; + +/* select mode */ + ret =i2c_master_send (client, command,SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -EIO ;
Quoted 9 lines
drivers/hwmon/sht3x.c line 520
Quoted 6 lines
drivers/hwmon/sht3x.c new file +6
-0+ } + +/* select mode and command */ + data->mode = mode; +sht3x_select_command (data); +All that unprotected ? What happens if multiple processes write different values in parallel ?
I will rethink the mutex protection of the interface
Quoted 72 lines
Quoted 72 lines
drivers/hwmon/sht3x.c new file +28
-0+return count; +} + +static ssize_t soft_reset (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) { +int ret; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + +/* break if in periodic measure mode */ +if (data->mode >0 ) { + ret =i2c_master_send (client, sht3x_cmd_break, +SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -EIO ; + data->mode =0 ; + } + +/* clear status register */ + ret =i2c_master_send (client, sht3x_cmd_clear_status_reg, +SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -EIO ; + +/* soft reset */ + ret = i2c_master_send(client, sht3x_cmd_soft_reset,
SHT3X_CMD_LENGTH);drivers/hwmon/sht3x.c new file +7
-0+if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -EIO ; + +return count; +} + +static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, temp1_input_show,NULL, 0);
+static SENSOR_DEVICE_ATTR(humidity1_input,S_IRUGO,humidity1_input_show,drivers/hwmon/sht3x.c new file +2
-0+ NULL, 0); +static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
temp1_alert_show,drivers/hwmon/sht3x.c new file +4
-0+ temp1_alert_store, 0); +static SENSOR_DEVICE_ATTR(humidity1_max, S_IRUGO | S_IWUSR, + humidity1_alert_show, humidity1_alert_store, 0); +static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
temp1_alert_show,drivers/hwmon/sht3x.c new file +4
-0+ temp1_alert_store, 1); +static SENSOR_DEVICE_ATTR(humidity1_max_hyst, S_IRUGO | S_IWUSR, + humidity1_alert_show, humidity1_alert_store, 1); +static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR,
temp1_alert_show,drivers/hwmon/sht3x.c new file +20
-0+ temp1_alert_store, 2); +static SENSOR_DEVICE_ATTR(humidity1_min, S_IRUGO | S_IWUSR, + humidity1_alert_show, humidity1_alert_store, 2); +static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO | S_IWUSR, + temp1_alert_show, temp1_alert_store, 3); +static SENSOR_DEVICE_ATTR(humidity1_min_hyst, S_IRUGO | S_IWUSR, + humidity1_alert_show, humidity1_alert_store, 3); +static SENSOR_DEVICE_ATTR(frequency, S_IRUGO | S_IWUSR, frequency_show, + frequency_store, 0); +static SENSOR_DEVICE_ATTR(soft_reset, S_IWUSR, NULL, soft_reset, 0); + +static struct attribute *sht3x_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, + &sensor_dev_attr_humidity1_input.dev_attr.attr, + &sensor_dev_attr_temp1_max.dev_attr.attr, + &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, + &sensor_dev_attr_humidity1_max.dev_attr.attr, + &sensor_dev_attr_humidity1_max_hyst.dev_attr.attr, + &sensor_dev_attr_temp1_min.dev_attr.attr, + &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
Quoted 9 lines
drivers/hwmon/sht3x.c line 590
Quoted 4 lines
drivers/hwmon/sht3x.c new file +4
-0+ &sensor_dev_attr_humidity1_min.dev_attr.attr, + &sensor_dev_attr_humidity1_min_hyst.dev_attr.attr, + &sensor_dev_attr_frequency.dev_attr.attr, + &sensor_dev_attr_soft_reset.dev_attr.attr,Limit attributes without alarm attributes is kind of unusual. Looking into the datasheet, the chip does support a status register with alert bits. Any reason for not providing alert attributes ?
The alarm feature is only present in periodic mode. In this mode the sensor measures by itself without the influence of the user (or a program). If there
is an alert, the sensor set the alert pin to high. As long as there is no e.g.
watchdog mechanism in place which notifies the driver, we can not know when an alert happens without some sort of polling mechanism. During periodic measurement the sensor measures temperature and humidity without any interaction of the user.
We can of course implement an alarm interface which asks the status register whenever it is called by the user.
I can't find a humidity alarm flag in the documentation? Is there a reason for that or did nobody used this so far?
Quoted 92 lines
Quoted 92 lines
drivers/hwmon/sht3x.c new file +23
-0+ NULL +}; + +static struct attribute *sts3x_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, +NULL +}; + +ATTRIBUTE_GROUPS (sht3x); +ATTRIBUTE_GROUPS (sts3x); + +static int sht3x_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +int ret; +struct sht3x_data *data; +struct device *hwmon_dev; +struct i2c_adapter *adap = client->adapter; +struct device *dev = &client->dev; +const struct attribute_group **attribute_groups; + +/* +* we require full i2c support since the sht3x uses multi-byte read and
drivers/hwmon/sht3x.c new file +23
-0+* writes as well as multi-byte commands which are not supported by +* the smbus protocol +*/ +if (!i2c_check_functionality (adap,I2C_FUNC_I2C )) +return -ENODEV ; + + ret =i2c_master_send (client, sht3x_cmd_clear_status_reg, +SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -ENODEV ; + + data =devm_kzalloc (dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->setup.blocking_io = false; + data->setup.high_precision = true; + data->mode =0 ; + data->client = client; +crc8_populate_msb (sht3x_crc8_table,SHT3X_CRC8_POLYNOMIAL ); + +if (client->dev.platform_data) + data->setup = *(struct sht3x_platform_data*)dev->platform_data;
drivers/hwmon/sht3x.c new file +38
-0+sht3x_select_command (data); +mutex_init (&data->update_lock); + +if (id->driver_data ==SHT3X_ID_STS ) + attribute_groups = sts3x_groups; +else + attribute_groups = sht3x_groups; + + hwmon_dev =devm_hwmon_device_register_with_groups (dev, + client->name, + data, attribute_groups); + +if (IS_ERR (hwmon_dev)) +dev_dbg (dev,"unable to register hwmon device\n" ); + +return PTR_ERR_OR_ZERO (hwmon_dev); +} + +/* device ID table */ +static const struct i2c_device_id sht3x_id[] = { + {"sht3x" ,SHT3X_ID_SHT }, + {"sts3x" ,SHT3X_ID_STS }, + {} +}; + +MODULE_DEVICE_TABLE (i2c, sht3x_id); + +static struct i2c_driver sht3x_i2c_driver = { + .driver.name ="sht3x" , + .probe = sht3x_probe, + .id_table = sht3x_id, +}; + +module_i2c_driver (sht3x_i2c_driver); + +MODULE_AUTHOR ("David Frey <[email protected]>" ); +MODULE_AUTHOR ("Pascal Sachs <[email protected]>" ); +MODULE_DESCRIPTION("Sensirion SHT3x humidity and temperature sensordriver");
drivers/hwmon/sht3x.c new file +1
-0+MODULE_LICENSE ("GPL" );drivers/hwmon/twl4030-madc-hwmon.c +0
-1--- a/drivers/hwmon/twl4030-madc-hwmon.c
Quoted 8 lines
drivers/hwmon/twl4030-madc-hwmon.c line 28
Quoted 6 lines
+++
b/drivers/hwmon/twl4030-madc-hwmon.cdrivers/hwmon/twl4030-madc-hwmon.c +1
-0@@ -25,6 +25,7 @@*/ #include <linux/init.h> #include <linux/module.h> +//FIXME this is used for clamp_val Drop.
OK, my bad :-(
Quoted 38 lines
Quoted 37 lines
drivers/hwmon/twl4030-madc-hwmon.c +0
-0#include <linux/kernel.h> #include <linux/i2c/twl.h> #include <linux/device.h> include/linux/platform_data/sht3x.h new file +25
-0@@ -0,0 +1,25 @@ +/* +* Copyright (C) 2015 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef __SHT3X_H_ +#define __SHT3X_H_ + +struct sht3x_platform_data { +bool blocking_io; +bool high_precision; +}; +#endif /* __SHT3X_H_ */
Guenter Roeck
maintainerHi Pascal,
Quoted 1 line · Pascal Sachs
[ ... ]
Quoted 13 lines
Quoted 5 lines
Quoted 4 lines
+While in periodic measure mode, read out of humidity and temperature values are
+not supported. Nevertheless it is possible to read out the values with maximalQuoted 7 lines
Really ? I seem to be missing this in the datasheet. Section 4.4 suggests that
both are supported. Besides, this would be really odd - what would be the point
of periodic mode (or any mode, for that matter) if it doesn't really measure
anything ?
Quoted 8 lines
When you start the periodic measurement, the sensor will measure independently
in the background and keep up to 8 values in an internal buffer. Once the client
reads out the buffer, the sensor will invalidate the buffer and therefore return
an error on the I2C bus until at least one new measurement was generated.
Hope that doesn't mean that it reports old values if the result is not read.
Quoted 4 lines
This feature is used to automatically set the alert bit according to the configured
limits and hysteresis values.
Quoted 8 lines
Our Product Manager for the SHT3x sensor told me to not handle this behavior in
the driver by e.g. caching the last value, since it will confuse our customers when the
hardware behaves differently then the driver.If you have an other opinion and would like to share it, I'm open for any input.
Caching is quite widely used in hwmon drivers if it is known that the chip doesn't report new values faster than read. If the chip's periodic measurement interval is set to, say, 500ms, it doesn't make sense for the driver to try to read a new value less than 500ms after the previous reading.
[ ... ]
Quoted 16 lines
Quoted 11 lines
Documentation/hwmon/sht3x line 66
Quoted 5 lines
+soft_reset: Soft reset the internal state of the sensor,
clear theDocumentation/hwmon/sht3x new file +2
-0+ status register, clear the alert and switch back to + single shot modeMakes me really unhappy. It is not a standard attribute, there is no clear
indication why it is needed, and it affects other attributes (mode) without updating the command pointer, thus probably breaking things.Quoted 4 lines
In general, if the chip is that unstable that it needs to be reset once in a while, that should be auto-detected if possible and be handled automatically. A manual "please reset me" attribute is the worst possible solution and should only be used if absolutely necessary.
Quoted 4 lines
The
soft_resetis actually not really needed. It's just a way for the user to
clear his configuration. I will remove this interface if it makes you unhappy.
Please do.
[ ... ]
Quoted 5 lines
Quoted 5 lines
Limit attributes without alarm attributes is kind of unusual. Looking into the datasheet, the chip does support a status register with alert bits. Any reason for not providing alert attributes ?
Quoted 10 lines
The alarm feature is only present in periodic mode. In this mode the sensor measures by itself without the influence of the user (or a program). If there
is an alert, the sensor set the alert pin to high. As long as there is no e.g.
watchdog mechanism in place which notifies the driver, we can not know when an alert happens without some sort of polling mechanism. During periodic measurement the sensor measures temperature and humidity without any interaction of the user.
Most drivers expect alarm attribute polling from user space. Some implement an interrupt handler which notifies user space (eg gpio-fan). The alert pin of your chip can be connected to an interrupt line, in which case it could be used for that purpose.
Quoted 5 lines
We can of course implement an alarm interface which asks the status register whenever it is called by the user.
I can't find a humidity alarm flag in the documentation? Is there a reason for that or did nobody used this so far?
Nobody used it. No special reason. Feel free to start using it; we can just add it to the ABI. We don't have humidityX_{min,max} and humidityX_{min,max}_hyst documented either, so we should add those to the ABI as well.
Thanks,
Guenter
PATCH v3 1/1 hwmon: add support for Sensirion SHT3x sensors
In reply to Pascal SachsFrom: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500.
---
Patch was generated against kernel v4.6-rc7
Documentation/hwmon/sht3x | 72 ++++ drivers/hwmon/Kconfig | 11 + drivers/hwmon/Makefile | 1 + drivers/hwmon/sht3x.c | 701 ++++++++++++++++++++++++++++++++++++ include/linux/platform_data/sht3x.h | 25 ++ 5 files changed, 810 insertions(+) create mode 100644 Documentation/hwmon/sht3x create mode 100644 drivers/hwmon/sht3x.c create mode 100644 include/linux/platform_data/sht3x.h
Documentation/hwmon/sht3x new file +72 -0
@@ -0,0 +1,72 @@ +Kernel driver sht3x +=================== + +Supported chips: + * Sensirion SHT3x-DIS + Prefix: 'sht3x' + Addresses scanned: none + Datasheet: http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_Datasheet_SHT3x_DIS.pdf + +Author: + David Frey <[email protected]> + Pascal Sachs <[email protected]> + +Description +----------- + +This driver implements support for the Sensirion SHT3x-DIS chip, a humidity +and temperature sensor. Temperature is measured in degrees celsius, relative +humidity is expressed as a percentage. In the sysfs interface, all values are +scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +The device communicates with the I2C protocol. Sensors can have the I2C +addresses 0x44 or 0x45, depending on the wiring. See +Documentation/i2c/instantiating-devices for methods to instantiate the device. + +There are two options configurable by means of sht3x_platform_data: +1. blocking (pull the I2C clock line down while performing the measurement) or + non-blocking mode. Blocking mode will guarantee the fastest result but + the I2C bus will be busy during that time. By default, non-blocking mode + is used. Make sure clock-stretching works properly on your device if you + want to use blocking mode. +2. high or low accuracy. High accuracy is used by default and using it is + strongly recommended. + +The sht3x sensor supports a single shot mode as well as 5 periodic measure +modes, which can be controlled with the update_interval sysfs interface. +The allowed update_interval in milliseconds are as follows: + * 0 single shot mode + * 2000 0.5 Hz periodic measurement + * 1000 1 Hz periodic measurement + * 500 2 Hz periodic measurement + * 250 4 Hz periodic measurement + * 100 10 Hz periodic measurement + +In the periodic measure mode, the sensor automatically triggers a measurement +with the configured update interval on the chip. When a temperature or humidity +reading exceeds the configured limits, the alert attribute is set to 1 and +the alert pin on the sensor is set to high. +When the temperature and humidity readings move back between the hysteresis +values, the alert bit is set to 0 and the alert pin on the sensor is set to +low. + +sysfs-Interface +--------------- + +temp1_input: temperature input +humidity1_input: humidity input +temp1_max: temperature max value +temp1_max_hyst: temperature hysteresis value for max limit +humidity1_max: humidity max value +humidity1_max_hyst: humidity hysteresis value for max limit +temp1_min: temperature min value +temp1_min_hyst: temperature hysteresis value for min limit +humidity1_min: humidity min value +humidity1_min_hyst: humidity hysteresis value for min limit +temp1_alarm: alarm flag is set to 1 if the temperature is outside the + configured limits. Alarm only works in periodic measure mode +humidity1_alarm: alarm flag is set to 1 if the humidity is outside the + configured limits. Alarm only works in periodic measure mode +update_interval: update interval, 0 for single shot, interval in msec + for periodic measurement. If the interval is not supported + by the sensor, the next faster interval is chosen
drivers/hwmon/Kconfig +11 -0
@@ -1254,6 +1254,17 @@ config SENSORS_SHT21 This driver can also be built as a module. If so, the module will be called sht21. +config SENSORS_SHT3x + tristate "Sensiron humidity and temperature sensors. SHT3x and compat." + depends on I2C + select CRC8 + help + If you say yes here you get support for the Sensiron SHT30 and SHT31 + humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht3x. + config SENSORS_SHTC1 tristate "Sensiron humidity and temperature sensors. SHTC1 and compat." depends on I2C
drivers/hwmon/Makefile +1 -0
@@ -137,6 +137,7 @@ obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o +obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.o
drivers/hwmon/sht3x.c new file +701 -0
701 lines not on this page · raw message
include/linux/platform_data/sht3x.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* Copyright (C) 2015 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef __SHT3X_H_ +#define __SHT3X_H_ + +struct sht3x_platform_data { +bool blocking_io; +bool high_precision; +}; +#endif /* __SHT3X_H_ */
Signature · 1 line
1.9.1
Guenter Roeck
maintainerQuoted 353 lines · Pascal Sachs
From: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500.
Signed-off-by Pascal Sachs <[email protected]>---
Patch was generated against kernel v4.6-rc7Documentation/hwmon/sht3x | 72 ++++ drivers/hwmon/Kconfig | 11 + drivers/hwmon/Makefile | 1 + drivers/hwmon/sht3x.c | 701 ++++++++++++++++++++++++++++++++++++ include/linux/platform_data/sht3x.h | 25 ++ 5 files changed, 810 insertions(+)create mode 100644
Documentation/hwmon/sht3xcreate mode 100644drivers/hwmon/sht3x.ccreate mode 100644include/linux/platform_data/sht3x.hDocumentation/hwmon/sht3x new file +72
-0@@ -0,0 +1,72 @@ +Kernel driver sht3x +=================== + +Supported chips: + * Sensirion SHT3x-DIS + Prefix: 'sht3x' + Addresses scanned: none + Datasheet: http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_Datasheet_SHT3x_DIS.pdf + +Author: + David Frey <[email protected]> + Pascal Sachs <[email protected]> + +Description +----------- + +This driver implements support for the Sensirion SHT3x-DIS chip, a humidity +and temperature sensor. Temperature is measured in degrees celsius, relative +humidity is expressed as a percentage. In the sysfs interface, all values are +scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +The device communicates with the I2C protocol. Sensors can have the I2C +addresses 0x44 or 0x45, depending on the wiring. See +Documentation/i2c/instantiating-devices for methods to instantiate the device. + +There are two options configurable by means of sht3x_platform_data: +1. blocking (pull the I2C clock line down while performing the measurement) or + non-blocking mode. Blocking mode will guarantee the fastest result but + the I2C bus will be busy during that time. By default, non-blocking mode + is used. Make sure clock-stretching works properly on your device if you + want to use blocking mode. +2. high or low accuracy. High accuracy is used by default and using it is + strongly recommended. + +The sht3x sensor supports a single shot mode as well as 5 periodic measure +modes, which can be controlled with the update_interval sysfs interface. +The allowed update_interval in milliseconds are as follows: + * 0 single shot mode + * 2000 0.5 Hz periodic measurement + * 1000 1 Hz periodic measurement + * 500 2 Hz periodic measurement + * 250 4 Hz periodic measurement + * 100 10 Hz periodic measurement + +In the periodic measure mode, the sensor automatically triggers a measurement +with the configured update interval on the chip. When a temperature or humidity +reading exceeds the configured limits, the alert attribute is set to 1 and +the alert pin on the sensor is set to high. +When the temperature and humidity readings move back between the hysteresis +values, the alert bit is set to 0 and the alert pin on the sensor is set to +low. + +sysfs-Interface +--------------- + +temp1_input: temperature input +humidity1_input: humidity input +temp1_max: temperature max value +temp1_max_hyst: temperature hysteresis value for max limit +humidity1_max: humidity max value +humidity1_max_hyst: humidity hysteresis value for max limit +temp1_min: temperature min value +temp1_min_hyst: temperature hysteresis value for min limit +humidity1_min: humidity min value +humidity1_min_hyst: humidity hysteresis value for min limit +temp1_alarm: alarm flag is set to 1 if the temperature is outside the + configured limits. Alarm only works in periodic measure mode +humidity1_alarm: alarm flag is set to 1 if the humidity is outside the + configured limits. Alarm only works in periodic measure mode +update_interval: update interval, 0 for single shot, interval in msec + for periodic measurement. If the interval is not supported + by the sensor, the next faster interval is chosendrivers/hwmon/Kconfig +11
-0@@ -1254,6 +1254,17 @@ config SENSORS_SHT21 This driver can also be built as a module. If so, the module will be called sht21. +config SENSORS_SHT3x + tristate "Sensiron humidity and temperature sensors. SHT3x and compat." + depends on I2C + select CRC8 + help + If you say yes here you get support for the Sensiron SHT30 and SHT31 + humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht3x. + config SENSORS_SHTC1 tristate "Sensiron humidity and temperature sensors. SHTC1 and compat." depends on I2Cdrivers/hwmon/Makefile +1
-0@@ -137,6 +137,7 @@ obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o +obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.odrivers/hwmon/sht3x.c new file +213
-0@@ -0,0 +1,701 @@ +/* Sensirion SHT3x-DIS humidity and temperature sensor driver. +* The SHT3x comes in many different versions, this driver is for the +* I2C version only. +* +* Copyright (C) 2016 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#include <asm/page.h> +#include <linux/crc8.h> +#include <linux/delay.h> +#include <linux/err.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/i2c.h> +#include <linux/init.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/jiffies.h> +#include <linux/platform_data/sht3x.h> + +/* commands (high precision mode) */ +static const unsigned char sht3x_cmd_measure_blocking_hpm[] = {0x2c ,0x06 }; +static const unsigned char sht3x_cmd_measure_nonblocking_hpm[] = {0x24 ,0x00 }; + +/* commands (low power mode) */ +static const unsigned char sht3x_cmd_measure_blocking_lpm[] = {0x2c ,0x10 }; +static const unsigned char sht3x_cmd_measure_nonblocking_lpm[] = {0x24 ,0x16 }; + +/* commands for periodic mode */ +static const unsigned char sht3x_cmd_measure_periodic_mode[] = {0xe0 ,0x00 }; +static const unsigned char sht3x_cmd_break[] = {0x30 ,0x93 }; + +/* other commands */ +static const unsigned char sht3x_cmd_read_status_reg[] = {0xf3 ,0x2d }; +static const unsigned char sht3x_cmd_clear_status_reg[] = {0x30 ,0x41 }; + +/* delays for non-blocking i2c commands, both in us */ +#define SHT3X_NONBLOCKING_WAIT_TIME_HPM 15000 +#define SHT3X_NONBLOCKING_WAIT_TIME_LPM 4000 + +#define SHT3X_WORD_LEN 2 +#define SHT3X_CMD_LENGTH 2 +#define SHT3X_CRC8_LEN 1 +#define SHT3X_RESPONSE_LENGTH 6 +#define SHT3X_CRC8_POLYNOMIAL 0x31 +#define SHT3X_CRC8_INIT 0xFF + +enum sht3x_chips { + sht3x, + sts3x, +}; + +DECLARE_CRC8_TABLE (sht3x_crc8_table); + +/* periodic measure commands (high precision mode) */ +static const char periodic_measure_commands_hpm[][SHT3X_CMD_LENGTH ] = { +/* 0.5 measurements per second */ + {0x20 ,0x32 }, +/* 1 measurements per second */ + {0x21 ,0x30 }, +/* 2 measurements per second */ + {0x22 ,0x36 }, +/* 4 measurements per second */ + {0x23 ,0x34 }, +/* 10 measurements per second */ + {0x27 ,0x37 }, +}; + +/* periodic measure commands (low power mode) */ +static const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH ] = { +/* 0.5 measurements per second */ + {0x20 ,0x2f }, +/* 1 measurements per second */ + {0x21 ,0x2d }, +/* 2 measurements per second */ + {0x22 ,0x2b }, +/* 4 measurements per second */ + {0x23 ,0x29 }, +/* 10 measurements per second */ + {0x27 ,0x2a }, +}; + +struct sht3x_limit_commands { +const char read_command[SHT3X_CMD_LENGTH ]; +const char write_command[SHT3X_CMD_LENGTH ]; +}; + +static const struct sht3x_limit_commands limit_commands[] = { +/* temp1_max, humidity1_max */ + { {0xe1 ,0x1f }, {0x61 ,0x1d } }, +/* temp_1_max_hyst, humidity1_max_hyst */ + { {0xe1 ,0x14 }, {0x61 ,0x16 } }, +/* temp1_min, humidity1_min */ + { {0xe1 ,0x02 }, {0x61 ,0x00 } }, +/* temp_1_min_hyst, humidity1_min_hyst */ + { {0xe1 ,0x09 }, {0x61 ,0x0B } }, +}; + +#define SHT3X_NUM_LIMIT_CMD ARRAY_SIZE(limit_commands) + +static const u16 mode_to_update_interval[] = { +0 , +2000 , +1000 , +500 , +250 , +100 , +}; + +struct sht3x_data { +struct i2c_client *client; +struct mutex i2c_lock;/* lock for sending i2c commands */ +struct mutex data_lock;/* lock for updating driver data */ + +u8 mode; +const unsigned char *command; +u32 wait_time;/* in us*/ +unsigned long last_update;/* last update in periodic mode*/ + +struct sht3x_platform_data setup; + +/* +* cached values for temperature and humidity and limits +* the limits arrays have the following order: +* max, max_hyst, min, min_hyst +*/ +int temperature; +int temperature_limits[SHT3X_NUM_LIMIT_CMD ]; +u32 humidity; +u32 humidity_limits[SHT3X_NUM_LIMIT_CMD ]; +}; + +static u8 get_mode_from_update_interval (u16 value) +{ +size_t index; +u8 number_of_modes =ARRAY_SIZE (mode_to_update_interval); + +if (value ==0 ) +return 0 ; + +/* find next faster update interval */ +for (index =1 ; index < number_of_modes; index++) { +if (mode_to_update_interval[index] <= value) +return index; + } + +return number_of_modes -1 ; +} + +static int sht3x_read_from_command (struct i2c_client *client, +struct sht3x_data *data, +const char *command, +char *buf,int length,u32 wait_time) +{ +int ret; + +mutex_lock (&data->i2c_lock); + ret =i2c_master_send (client, command,SHT3X_CMD_LENGTH ); + +if (ret !=SHT3X_CMD_LENGTH ) { + ret = ret <0 ? ret : -EIO ; + goto out; + } + +if (wait_time) +usleep_range (wait_time, wait_time +1000 ); + + ret =i2c_master_recv (client, buf, length); +if (ret != length) { + ret = ret <0 ? ret : -EIO ; + goto out; + } + + ret =0 ; +out: +mutex_unlock (&data->i2c_lock); +return ret; +} + +static int sht3x_extract_temperature (u16 raw) +{ +/* +* From datasheet: +* T = -45 + 175 * ST / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return ((21875 * (int )raw) >>13 ) -45000 ; +} + +static u32 sht3x_extract_humidity (u16 raw) +{ +/* +* From datasheet: +* RH = 100 * SRH / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return (12500 * (int )raw) >>13 ; +} +
drivers/hwmon/sht3x.c line 223
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+static struct sht3x_data *sht3x_update_client (struct device *dev) +{ +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +u16 interval_ms = mode_to_update_interval[data->mode]; +unsigned long interval_jiffies =msecs_to_jiffies (interval_ms); +unsigned char buf[SHT3X_RESPONSE_LENGTH ]; +u16 val; +int ret; +
<<<<
Quoted 4 lines
drivers/hwmon/sht3x.c new file +4
-0+/* only update once per interval in periodic mode */ +if (time_after (jiffies, data->last_update + interval_jiffies)) { + ret =sht3x_read_from_command (client, data, data->command, buf, +sizeof (buf), data->wait_time);
drivers/hwmon/sht3x.c line 237
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+if (ret) +return ERR_PTR (ret); + +mutex_lock (&data->data_lock); + val =be16_to_cpup ((__be16 *)buf); + data->temperature =sht3x_extract_temperature (val); + val =be16_to_cpup ((__be16 *)(buf +3 )); + data->humidity =sht3x_extract_humidity (val); + data->last_update = jiffies; +mutex_unlock (&data->data_lock);
Locking is incomplete. data_lock needs to be acquired at <<<< to be useful (ie it needs to cover last_update).
Quoted 45 lines
drivers/hwmon/sht3x.c new file +45
-0+ } + +return data; +} + +/* sysfs attributes */ +static ssize_t temp1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->temperature); +} + +static ssize_t humidity1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->humidity); +} + +static int limits_update (struct sht3x_data *data) +{ +int ret; +u8 index; +int temperature; +u32 humidity; +u16 raw; +char buffer[SHT3X_RESPONSE_LENGTH ]; +const struct sht3x_limit_commands *commands; +struct i2c_client *client = data->client; + +for (index =0 ; index <SHT3X_NUM_LIMIT_CMD ; index++) { + commands = &limit_commands[index]; + ret =sht3x_read_from_command (client, data, + commands->read_command, buffer, +SHT3X_RESPONSE_LENGTH ,0 ); +
drivers/hwmon/sht3x.c line 292
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+if (ret) +return ret; + + raw =be16_to_cpup ((__be16 *)buffer); + temperature =sht3x_extract_temperature ((raw &0x01ff ) <<7 ); + humidity =sht3x_extract_humidity (raw &0xfe00 ); +mutex_lock (&data->data_lock); + data->temperature_limits[index] = temperature; + data->humidity_limits[index] = humidity; +mutex_unlock (&data->data_lock);
Since the function is only called during probe, you don't need locks here. Just mark in the header that the function must only be called from probe or with data_lock held. Alternatively, data_lock would have to cover the entire function for it to be useful.
Quoted 53 lines
drivers/hwmon/sht3x.c new file +53
-0+ } + +return ret; +} + +static ssize_t temp1_limit_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int temperature_limit; +u8 index; +struct sht3x_data *data =dev_get_drvdata (dev); + + index =to_sensor_dev_attr (attr)->index; + temperature_limit = data->temperature_limits[index]; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , temperature_limit); +} + +static ssize_t humidity1_limit_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int humidity_limit; +u8 index; +struct sht3x_data *data =dev_get_drvdata (dev); + + index =to_sensor_dev_attr (attr)->index; + humidity_limit = data->humidity_limits[index]; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , humidity_limit); +} + +static size_t limit_store (struct device *dev, +struct device_attribute *attr, +size_t count, +int temperature, +u32 humidity) +{ +char buffer[SHT3X_CMD_LENGTH +SHT3X_WORD_LEN +SHT3X_CRC8_LEN ]; +char *position = buffer; +int ret; +u16 raw; +u8 index; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +const struct sht3x_limit_commands *commands; + + index =to_sensor_dev_attr (attr)->index; + commands = &limit_commands[index]; + +memcpy (position, commands->write_command,SHT3X_CMD_LENGTH ); + position +=SHT3X_CMD_LENGTH ;
drivers/hwmon/sht3x.c line 355
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+/* +* ST = (T + 45) / 175 * 2^16 +* SRH = RH / 100 * 2^16 +* adapted for fixed point arithmetic and packed the same as +* in limit_show() +*/ + raw = ((u32)(temperature +45000 ) *24543 ) >> (16 +7 ); + raw |= ((humidity *42950 ) >>16 ) &0xfe00 ; + + *(__be16 *)(position) =cpu_to_be16 (raw);
(position) seems unnecessary. Did you mean
*((__be16 *)position) ?
Quoted 9 lines
drivers/hwmon/sht3x.c new file +9
-0+ position +=SHT3X_WORD_LEN ; + *position =crc8 (sht3x_crc8_table, + position -SHT3X_WORD_LEN , +SHT3X_WORD_LEN , +SHT3X_CRC8_INIT ); + +mutex_lock (&data->i2c_lock); + ret =i2c_master_send (client, buffer,sizeof (buffer)); +mutex_unlock (&data->i2c_lock);
drivers/hwmon/sht3x.c line 374
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+if (ret !=sizeof (buffer)) { + ret = ret <0 ? ret : -EIO ; + }else { +mutex_lock (&data->data_lock); + data->temperature_limits[index] = temperature; + data->humidity_limits[index] = humidity; +mutex_unlock (&data->data_lock); + } + ret = count; +
'}' at the wrong location ? Note that per coding style you should use goto for error handling.
The locking is incomplete. You have to acquire data_lock before sending the i2c data, and hold it until the data is stored in the local variables.
Quoted 100 lines
drivers/hwmon/sht3x.c new file +100
-0+return ret; +} + +static ssize_t temp1_limit_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) +{ +int temperature; +u32 humidity; +u8 index; +int ret; +struct sht3x_data *data =dev_get_drvdata (dev); + + ret =kstrtoint (buf,0 , &temperature); + +if (ret) +return -EINVAL ; + + temperature =clamp_val (temperature,-45000 ,130000 ); + + index =to_sensor_dev_attr (attr)->index; + humidity = data->humidity_limits[index]; + + ret =limit_store (dev, attr, count, temperature, humidity); + +return ret; +} + +static ssize_t humidity1_limit_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) +{ +int temperature; +u32 humidity; +u8 index; +int ret; +struct sht3x_data *data =dev_get_drvdata (dev); + + ret =kstrtou32 (buf,0 , &humidity); + +if (ret) +return -EINVAL ; + + humidity =clamp_val (humidity,0 ,100000 ); + index =to_sensor_dev_attr (attr)->index; + temperature = data->temperature_limits[index]; + + ret =limit_store (dev, attr, count, temperature, humidity); + +return ret; +} + +static void sht3x_select_command (struct sht3x_data *data) +{ +/* +* In blocking mode (clock stretching mode) the I2C bus +* is blocked for other traffic, thus the call to i2c_master_recv() +* will wait until the data is ready. For non blocking mode, we +* have to wait ourselves. +*/ +if (data->mode >0 ) { + data->command = sht3x_cmd_measure_periodic_mode; + data->wait_time =0 ; + }else if (data->setup.blocking_io) { + data->command = data->setup.high_precision ? + sht3x_cmd_measure_blocking_hpm : + sht3x_cmd_measure_blocking_lpm; + data->wait_time =0 ; + }else { +if (data->setup.high_precision) { + data->command = sht3x_cmd_measure_nonblocking_hpm; + data->wait_time =SHT3X_NONBLOCKING_WAIT_TIME_HPM ; + }else { + data->command = sht3x_cmd_measure_nonblocking_lpm; + data->wait_time =SHT3X_NONBLOCKING_WAIT_TIME_LPM ; + } + } +} + +static int status_register_read (struct device *dev, +struct device_attribute *attr, +char *buffer,int length) +{ +int ret; + +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + + ret =sht3x_read_from_command (client, data, sht3x_cmd_read_status_reg, + buffer, length,0 ); + +return ret; +} + +static ssize_t temp1_alarm_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{
drivers/hwmon/sht3x.c line 484
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+int ret; +int length =SHT3X_WORD_LEN +SHT3X_CRC8_LEN ; +char buffer[length]; + + ret =status_register_read (dev, attr, buffer, length); + +if (ret) +return ret; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , (buffer[0 ] &0x04 ) ==0x04 );
!!(buffer[0] & 0x04)
would be a bit easier to understand.
Quoted 6 lines
drivers/hwmon/sht3x.c new file +6
-0+} + +static ssize_t humidity1_alarm_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{
drivers/hwmon/sht3x.c line 500
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+int ret; +int length =SHT3X_WORD_LEN +SHT3X_CRC8_LEN ; +char buffer[length]; + + ret =status_register_read (dev, attr, buffer, length); + +if (ret) +return ret; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , (buffer[0 ] &0x08 ) ==0x08 );
!!(buffer[0] & 0x08)
would be a bit easier to understand.
Quoted 52 lines
drivers/hwmon/sht3x.c new file +52
-0+} + +static ssize_t update_interval_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct sht3x_data *data =dev_get_drvdata (dev); + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , + mode_to_update_interval[data->mode]); +} + +static ssize_t update_interval_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) +{ +u16 update_interval; +u8 mode; +int ret; +const char *command; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + + ret =kstrtou16 (buf,0 , &update_interval); + +if (ret) +return -EINVAL ; + + mode =get_mode_from_update_interval (update_interval); + +/* mode did not change */ +if (mode == data->mode) +return count; + +mutex_lock (&data->i2c_lock); +mutex_lock (&data->data_lock); +/* abort periodic measure */ + ret =i2c_master_send (client, sht3x_cmd_break,SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) + goto out; + data->mode =0 ; + +if (mode >0 ) { +if (data->setup.high_precision) + command = periodic_measure_commands_hpm[mode -1 ]; +else + command = periodic_measure_commands_lpm[mode -1 ]; + +/* select mode */ + ret =i2c_master_send (client, command,SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH )
drivers/hwmon/sht3x.c line 562
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ goto out; + } + +/* select mode and command */ + data->mode = mode; +sht3x_select_command (data); + +out: +mutex_unlock (&data->i2c_lock); +mutex_unlock (&data->data_lock);
You need to unlock in the order you locked.
Quoted 171 lines
drivers/hwmon/sht3x.c new file +139
-0+if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -EIO ; + +return count; +} + +static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, temp1_input_show, NULL, 0); +static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, humidity1_input_show, + NULL, 0); +static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, temp1_limit_show, + temp1_limit_store, 0); +static SENSOR_DEVICE_ATTR(humidity1_max, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, 0); +static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, temp1_limit_show, + temp1_limit_store, 1); +static SENSOR_DEVICE_ATTR(humidity1_max_hyst, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, 1); +static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, temp1_limit_show, + temp1_limit_store, 2); +static SENSOR_DEVICE_ATTR(humidity1_min, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, 2); +static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO | S_IWUSR, + temp1_limit_show, temp1_limit_store, 3); +static SENSOR_DEVICE_ATTR(humidity1_min_hyst, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, 3); +static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, temp1_alarm_show, NULL, 3); +static SENSOR_DEVICE_ATTR(humidity1_alarm, S_IRUGO, humidity1_alarm_show, + NULL, 3); +static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, + update_interval_show, update_interval_store, 0); + +static struct attribute *sht3x_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, + &sensor_dev_attr_humidity1_input.dev_attr.attr, + &sensor_dev_attr_temp1_max.dev_attr.attr, + &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, + &sensor_dev_attr_humidity1_max.dev_attr.attr, + &sensor_dev_attr_humidity1_max_hyst.dev_attr.attr, + &sensor_dev_attr_temp1_min.dev_attr.attr, + &sensor_dev_attr_temp1_min_hyst.dev_attr.attr, + &sensor_dev_attr_humidity1_min.dev_attr.attr, + &sensor_dev_attr_humidity1_min_hyst.dev_attr.attr, + &sensor_dev_attr_temp1_alarm.dev_attr.attr, + &sensor_dev_attr_humidity1_alarm.dev_attr.attr, + &sensor_dev_attr_update_interval.dev_attr.attr, +NULL +}; + +static struct attribute *sts3x_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, +NULL +}; + +ATTRIBUTE_GROUPS (sht3x); +ATTRIBUTE_GROUPS (sts3x); + +static int sht3x_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +int ret; +struct sht3x_data *data; +struct device *hwmon_dev; +struct i2c_adapter *adap = client->adapter; +struct device *dev = &client->dev; +const struct attribute_group **attribute_groups; + +/* +* we require full i2c support since the sht3x uses multi-byte read and +* writes as well as multi-byte commands which are not supported by +* the smbus protocol +*/ +if (!i2c_check_functionality (adap,I2C_FUNC_I2C )) +return -ENODEV ; + + ret =i2c_master_send (client, sht3x_cmd_clear_status_reg, +SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -ENODEV ; + + data =devm_kzalloc (dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->setup.blocking_io = false; + data->setup.high_precision = true; + data->mode =0 ; + data->last_update =0 ; + data->client = client; +crc8_populate_msb (sht3x_crc8_table,SHT3X_CRC8_POLYNOMIAL ); + +if (client->dev.platform_data) + data->setup = *(struct sht3x_platform_data *)dev->platform_data; + +sht3x_select_command (data); + +mutex_init (&data->i2c_lock); +mutex_init (&data->data_lock); + + ret =limits_update (data); +if (ret) +return ret; + +if (id->driver_data == sts3x) + attribute_groups = sts3x_groups; +else + attribute_groups = sht3x_groups; + + hwmon_dev =devm_hwmon_device_register_with_groups (dev, + client->name, + data, + attribute_groups); + +if (IS_ERR (hwmon_dev)) +dev_dbg (dev,"unable to register hwmon device\n" ); + +return PTR_ERR_OR_ZERO (hwmon_dev); +} + +/* device ID table */ +static const struct i2c_device_id sht3x_ids[] = { + {"sht3x" , sht3x}, + {"sts3x" , sts3x}, + {} +}; + +MODULE_DEVICE_TABLE (i2c, sht3x_ids); + +static struct i2c_driver sht3x_i2c_driver = { + .driver.name ="sht3x" , + .probe = sht3x_probe, + .id_table = sht3x_ids, +}; + +module_i2c_driver (sht3x_i2c_driver); + +MODULE_AUTHOR ("David Frey <[email protected]>" ); +MODULE_AUTHOR ("Pascal Sachs <[email protected]>" ); +MODULE_DESCRIPTION ("Sensirion SHT3x humidity and temperature sensor driver" ); +MODULE_LICENSE ("GPL" );include/linux/platform_data/sht3x.h new file +25
-0@@ -0,0 +1,25 @@ +/* +* Copyright (C) 2015 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef __SHT3X_H_ +#define __SHT3X_H_ + +struct sht3x_platform_data { +bool blocking_io; +bool high_precision; +}; +#endif /* __SHT3X_H_ */
Guenter Roeck
maintainerQuoted 11 lines · Pascal Sachs
From: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500.
Signed-off-by Pascal Sachs <[email protected]>---
Patch was generated against kernel v4.6-rc7
Another note: Please Cc: [email protected]; that makes it easier for me to track your patches.
Thanks,
Guenter
Quoted 862 lines
Documentation/hwmon/sht3x | 72 ++++ drivers/hwmon/Kconfig | 11 + drivers/hwmon/Makefile | 1 + drivers/hwmon/sht3x.c | 701 ++++++++++++++++++++++++++++++++++++ include/linux/platform_data/sht3x.h | 25 ++ 5 files changed, 810 insertions(+)create mode 100644
Documentation/hwmon/sht3xcreate mode 100644drivers/hwmon/sht3x.ccreate mode 100644include/linux/platform_data/sht3x.hDocumentation/hwmon/sht3x new file +72
-0@@ -0,0 +1,72 @@ +Kernel driver sht3x +=================== + +Supported chips: + * Sensirion SHT3x-DIS + Prefix: 'sht3x' + Addresses scanned: none + Datasheet: http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_Datasheet_SHT3x_DIS.pdf + +Author: + David Frey <[email protected]> + Pascal Sachs <[email protected]> + +Description +----------- + +This driver implements support for the Sensirion SHT3x-DIS chip, a humidity +and temperature sensor. Temperature is measured in degrees celsius, relative +humidity is expressed as a percentage. In the sysfs interface, all values are +scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +The device communicates with the I2C protocol. Sensors can have the I2C +addresses 0x44 or 0x45, depending on the wiring. See +Documentation/i2c/instantiating-devices for methods to instantiate the device. + +There are two options configurable by means of sht3x_platform_data: +1. blocking (pull the I2C clock line down while performing the measurement) or + non-blocking mode. Blocking mode will guarantee the fastest result but + the I2C bus will be busy during that time. By default, non-blocking mode + is used. Make sure clock-stretching works properly on your device if you + want to use blocking mode. +2. high or low accuracy. High accuracy is used by default and using it is + strongly recommended. + +The sht3x sensor supports a single shot mode as well as 5 periodic measure +modes, which can be controlled with the update_interval sysfs interface. +The allowed update_interval in milliseconds are as follows: + * 0 single shot mode + * 2000 0.5 Hz periodic measurement + * 1000 1 Hz periodic measurement + * 500 2 Hz periodic measurement + * 250 4 Hz periodic measurement + * 100 10 Hz periodic measurement + +In the periodic measure mode, the sensor automatically triggers a measurement +with the configured update interval on the chip. When a temperature or humidity +reading exceeds the configured limits, the alert attribute is set to 1 and +the alert pin on the sensor is set to high. +When the temperature and humidity readings move back between the hysteresis +values, the alert bit is set to 0 and the alert pin on the sensor is set to +low. + +sysfs-Interface +--------------- + +temp1_input: temperature input +humidity1_input: humidity input +temp1_max: temperature max value +temp1_max_hyst: temperature hysteresis value for max limit +humidity1_max: humidity max value +humidity1_max_hyst: humidity hysteresis value for max limit +temp1_min: temperature min value +temp1_min_hyst: temperature hysteresis value for min limit +humidity1_min: humidity min value +humidity1_min_hyst: humidity hysteresis value for min limit +temp1_alarm: alarm flag is set to 1 if the temperature is outside the + configured limits. Alarm only works in periodic measure mode +humidity1_alarm: alarm flag is set to 1 if the humidity is outside the + configured limits. Alarm only works in periodic measure mode +update_interval: update interval, 0 for single shot, interval in msec + for periodic measurement. If the interval is not supported + by the sensor, the next faster interval is chosendrivers/hwmon/Kconfig +11
-0@@ -1254,6 +1254,17 @@ config SENSORS_SHT21 This driver can also be built as a module. If so, the module will be called sht21. +config SENSORS_SHT3x + tristate "Sensiron humidity and temperature sensors. SHT3x and compat." + depends on I2C + select CRC8 + help + If you say yes here you get support for the Sensiron SHT30 and SHT31 + humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht3x. + config SENSORS_SHTC1 tristate "Sensiron humidity and temperature sensors. SHTC1 and compat." depends on I2Cdrivers/hwmon/Makefile +1
-0@@ -137,6 +137,7 @@ obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o +obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.odrivers/hwmon/sht3x.c new file +701
-0@@ -0,0 +1,701 @@ +/* Sensirion SHT3x-DIS humidity and temperature sensor driver. +* The SHT3x comes in many different versions, this driver is for the +* I2C version only. +* +* Copyright (C) 2016 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#include <asm/page.h> +#include <linux/crc8.h> +#include <linux/delay.h> +#include <linux/err.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/i2c.h> +#include <linux/init.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/jiffies.h> +#include <linux/platform_data/sht3x.h> + +/* commands (high precision mode) */ +static const unsigned char sht3x_cmd_measure_blocking_hpm[] = {0x2c ,0x06 }; +static const unsigned char sht3x_cmd_measure_nonblocking_hpm[] = {0x24 ,0x00 }; + +/* commands (low power mode) */ +static const unsigned char sht3x_cmd_measure_blocking_lpm[] = {0x2c ,0x10 }; +static const unsigned char sht3x_cmd_measure_nonblocking_lpm[] = {0x24 ,0x16 }; + +/* commands for periodic mode */ +static const unsigned char sht3x_cmd_measure_periodic_mode[] = {0xe0 ,0x00 }; +static const unsigned char sht3x_cmd_break[] = {0x30 ,0x93 }; + +/* other commands */ +static const unsigned char sht3x_cmd_read_status_reg[] = {0xf3 ,0x2d }; +static const unsigned char sht3x_cmd_clear_status_reg[] = {0x30 ,0x41 }; + +/* delays for non-blocking i2c commands, both in us */ +#define SHT3X_NONBLOCKING_WAIT_TIME_HPM 15000 +#define SHT3X_NONBLOCKING_WAIT_TIME_LPM 4000 + +#define SHT3X_WORD_LEN 2 +#define SHT3X_CMD_LENGTH 2 +#define SHT3X_CRC8_LEN 1 +#define SHT3X_RESPONSE_LENGTH 6 +#define SHT3X_CRC8_POLYNOMIAL 0x31 +#define SHT3X_CRC8_INIT 0xFF + +enum sht3x_chips { + sht3x, + sts3x, +}; + +DECLARE_CRC8_TABLE (sht3x_crc8_table); + +/* periodic measure commands (high precision mode) */ +static const char periodic_measure_commands_hpm[][SHT3X_CMD_LENGTH ] = { +/* 0.5 measurements per second */ + {0x20 ,0x32 }, +/* 1 measurements per second */ + {0x21 ,0x30 }, +/* 2 measurements per second */ + {0x22 ,0x36 }, +/* 4 measurements per second */ + {0x23 ,0x34 }, +/* 10 measurements per second */ + {0x27 ,0x37 }, +}; + +/* periodic measure commands (low power mode) */ +static const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH ] = { +/* 0.5 measurements per second */ + {0x20 ,0x2f }, +/* 1 measurements per second */ + {0x21 ,0x2d }, +/* 2 measurements per second */ + {0x22 ,0x2b }, +/* 4 measurements per second */ + {0x23 ,0x29 }, +/* 10 measurements per second */ + {0x27 ,0x2a }, +}; + +struct sht3x_limit_commands { +const char read_command[SHT3X_CMD_LENGTH ]; +const char write_command[SHT3X_CMD_LENGTH ]; +}; + +static const struct sht3x_limit_commands limit_commands[] = { +/* temp1_max, humidity1_max */ + { {0xe1 ,0x1f }, {0x61 ,0x1d } }, +/* temp_1_max_hyst, humidity1_max_hyst */ + { {0xe1 ,0x14 }, {0x61 ,0x16 } }, +/* temp1_min, humidity1_min */ + { {0xe1 ,0x02 }, {0x61 ,0x00 } }, +/* temp_1_min_hyst, humidity1_min_hyst */ + { {0xe1 ,0x09 }, {0x61 ,0x0B } }, +}; + +#define SHT3X_NUM_LIMIT_CMD ARRAY_SIZE(limit_commands) + +static const u16 mode_to_update_interval[] = { +0 , +2000 , +1000 , +500 , +250 , +100 , +}; + +struct sht3x_data { +struct i2c_client *client; +struct mutex i2c_lock;/* lock for sending i2c commands */ +struct mutex data_lock;/* lock for updating driver data */ + +u8 mode; +const unsigned char *command; +u32 wait_time;/* in us*/ +unsigned long last_update;/* last update in periodic mode*/ + +struct sht3x_platform_data setup; + +/* +* cached values for temperature and humidity and limits +* the limits arrays have the following order: +* max, max_hyst, min, min_hyst +*/ +int temperature; +int temperature_limits[SHT3X_NUM_LIMIT_CMD ]; +u32 humidity; +u32 humidity_limits[SHT3X_NUM_LIMIT_CMD ]; +}; + +static u8 get_mode_from_update_interval (u16 value) +{ +size_t index; +u8 number_of_modes =ARRAY_SIZE (mode_to_update_interval); + +if (value ==0 ) +return 0 ; + +/* find next faster update interval */ +for (index =1 ; index < number_of_modes; index++) { +if (mode_to_update_interval[index] <= value) +return index; + } + +return number_of_modes -1 ; +} + +static int sht3x_read_from_command (struct i2c_client *client, +struct sht3x_data *data, +const char *command, +char *buf,int length,u32 wait_time) +{ +int ret; + +mutex_lock (&data->i2c_lock); + ret =i2c_master_send (client, command,SHT3X_CMD_LENGTH ); + +if (ret !=SHT3X_CMD_LENGTH ) { + ret = ret <0 ? ret : -EIO ; + goto out; + } + +if (wait_time) +usleep_range (wait_time, wait_time +1000 ); + + ret =i2c_master_recv (client, buf, length); +if (ret != length) { + ret = ret <0 ? ret : -EIO ; + goto out; + } + + ret =0 ; +out: +mutex_unlock (&data->i2c_lock); +return ret; +} + +static int sht3x_extract_temperature (u16 raw) +{ +/* +* From datasheet: +* T = -45 + 175 * ST / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return ((21875 * (int )raw) >>13 ) -45000 ; +} + +static u32 sht3x_extract_humidity (u16 raw) +{ +/* +* From datasheet: +* RH = 100 * SRH / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return (12500 * (int )raw) >>13 ; +} + +static struct sht3x_data *sht3x_update_client (struct device *dev) +{ +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +u16 interval_ms = mode_to_update_interval[data->mode]; +unsigned long interval_jiffies =msecs_to_jiffies (interval_ms); +unsigned char buf[SHT3X_RESPONSE_LENGTH ]; +u16 val; +int ret; + +/* only update once per interval in periodic mode */ +if (time_after (jiffies, data->last_update + interval_jiffies)) { + ret =sht3x_read_from_command (client, data, data->command, buf, +sizeof (buf), data->wait_time); +if (ret) +return ERR_PTR (ret); + +mutex_lock (&data->data_lock); + val =be16_to_cpup ((__be16 *)buf); + data->temperature =sht3x_extract_temperature (val); + val =be16_to_cpup ((__be16 *)(buf +3 )); + data->humidity =sht3x_extract_humidity (val); + data->last_update = jiffies; +mutex_unlock (&data->data_lock); + } + +return data; +} + +/* sysfs attributes */ +static ssize_t temp1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->temperature); +} + +static ssize_t humidity1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->humidity); +} + +static int limits_update (struct sht3x_data *data) +{ +int ret; +u8 index; +int temperature; +u32 humidity; +u16 raw; +char buffer[SHT3X_RESPONSE_LENGTH ]; +const struct sht3x_limit_commands *commands; +struct i2c_client *client = data->client; + +for (index =0 ; index <SHT3X_NUM_LIMIT_CMD ; index++) { + commands = &limit_commands[index]; + ret =sht3x_read_from_command (client, data, + commands->read_command, buffer, +SHT3X_RESPONSE_LENGTH ,0 ); + +if (ret) +return ret; + + raw =be16_to_cpup ((__be16 *)buffer); + temperature =sht3x_extract_temperature ((raw &0x01ff ) <<7 ); + humidity =sht3x_extract_humidity (raw &0xfe00 ); +mutex_lock (&data->data_lock); + data->temperature_limits[index] = temperature; + data->humidity_limits[index] = humidity; +mutex_unlock (&data->data_lock); + } + +return ret; +} + +static ssize_t temp1_limit_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int temperature_limit; +u8 index; +struct sht3x_data *data =dev_get_drvdata (dev); + + index =to_sensor_dev_attr (attr)->index; + temperature_limit = data->temperature_limits[index]; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , temperature_limit); +} + +static ssize_t humidity1_limit_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int humidity_limit; +u8 index; +struct sht3x_data *data =dev_get_drvdata (dev); + + index =to_sensor_dev_attr (attr)->index; + humidity_limit = data->humidity_limits[index]; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , humidity_limit); +} + +static size_t limit_store (struct device *dev, +struct device_attribute *attr, +size_t count, +int temperature, +u32 humidity) +{ +char buffer[SHT3X_CMD_LENGTH +SHT3X_WORD_LEN +SHT3X_CRC8_LEN ]; +char *position = buffer; +int ret; +u16 raw; +u8 index; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +const struct sht3x_limit_commands *commands; + + index =to_sensor_dev_attr (attr)->index; + commands = &limit_commands[index]; + +memcpy (position, commands->write_command,SHT3X_CMD_LENGTH ); + position +=SHT3X_CMD_LENGTH ; +/* +* ST = (T + 45) / 175 * 2^16 +* SRH = RH / 100 * 2^16 +* adapted for fixed point arithmetic and packed the same as +* in limit_show() +*/ + raw = ((u32)(temperature +45000 ) *24543 ) >> (16 +7 ); + raw |= ((humidity *42950 ) >>16 ) &0xfe00 ; + + *(__be16 *)(position) =cpu_to_be16 (raw); + position +=SHT3X_WORD_LEN ; + *position =crc8 (sht3x_crc8_table, + position -SHT3X_WORD_LEN , +SHT3X_WORD_LEN , +SHT3X_CRC8_INIT ); + +mutex_lock (&data->i2c_lock); + ret =i2c_master_send (client, buffer,sizeof (buffer)); +mutex_unlock (&data->i2c_lock); +if (ret !=sizeof (buffer)) { + ret = ret <0 ? ret : -EIO ; + }else { +mutex_lock (&data->data_lock); + data->temperature_limits[index] = temperature; + data->humidity_limits[index] = humidity; +mutex_unlock (&data->data_lock); + } + ret = count; + +return ret; +} + +static ssize_t temp1_limit_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) +{ +int temperature; +u32 humidity; +u8 index; +int ret; +struct sht3x_data *data =dev_get_drvdata (dev); + + ret =kstrtoint (buf,0 , &temperature); + +if (ret) +return -EINVAL ; + + temperature =clamp_val (temperature,-45000 ,130000 ); + + index =to_sensor_dev_attr (attr)->index; + humidity = data->humidity_limits[index]; + + ret =limit_store (dev, attr, count, temperature, humidity); + +return ret; +} + +static ssize_t humidity1_limit_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) +{ +int temperature; +u32 humidity; +u8 index; +int ret; +struct sht3x_data *data =dev_get_drvdata (dev); + + ret =kstrtou32 (buf,0 , &humidity); + +if (ret) +return -EINVAL ; + + humidity =clamp_val (humidity,0 ,100000 ); + index =to_sensor_dev_attr (attr)->index; + temperature = data->temperature_limits[index]; + + ret =limit_store (dev, attr, count, temperature, humidity); + +return ret; +} + +static void sht3x_select_command (struct sht3x_data *data) +{ +/* +* In blocking mode (clock stretching mode) the I2C bus +* is blocked for other traffic, thus the call to i2c_master_recv() +* will wait until the data is ready. For non blocking mode, we +* have to wait ourselves. +*/ +if (data->mode >0 ) { + data->command = sht3x_cmd_measure_periodic_mode; + data->wait_time =0 ; + }else if (data->setup.blocking_io) { + data->command = data->setup.high_precision ? + sht3x_cmd_measure_blocking_hpm : + sht3x_cmd_measure_blocking_lpm; + data->wait_time =0 ; + }else { +if (data->setup.high_precision) { + data->command = sht3x_cmd_measure_nonblocking_hpm; + data->wait_time =SHT3X_NONBLOCKING_WAIT_TIME_HPM ; + }else { + data->command = sht3x_cmd_measure_nonblocking_lpm; + data->wait_time =SHT3X_NONBLOCKING_WAIT_TIME_LPM ; + } + } +} + +static int status_register_read (struct device *dev, +struct device_attribute *attr, +char *buffer,int length) +{ +int ret; + +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + + ret =sht3x_read_from_command (client, data, sht3x_cmd_read_status_reg, + buffer, length,0 ); + +return ret; +} + +static ssize_t temp1_alarm_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int ret; +int length =SHT3X_WORD_LEN +SHT3X_CRC8_LEN ; +char buffer[length]; + + ret =status_register_read (dev, attr, buffer, length); + +if (ret) +return ret; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , (buffer[0 ] &0x04 ) ==0x04 ); +} + +static ssize_t humidity1_alarm_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int ret; +int length =SHT3X_WORD_LEN +SHT3X_CRC8_LEN ; +char buffer[length]; + + ret =status_register_read (dev, attr, buffer, length); + +if (ret) +return ret; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , (buffer[0 ] &0x08 ) ==0x08 ); +} + +static ssize_t update_interval_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct sht3x_data *data =dev_get_drvdata (dev); + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , + mode_to_update_interval[data->mode]); +} + +static ssize_t update_interval_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count) +{ +u16 update_interval; +u8 mode; +int ret; +const char *command; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + + ret =kstrtou16 (buf,0 , &update_interval); + +if (ret) +return -EINVAL ; + + mode =get_mode_from_update_interval (update_interval); + +/* mode did not change */ +if (mode == data->mode) +return count; + +mutex_lock (&data->i2c_lock); +mutex_lock (&data->data_lock); +/* abort periodic measure */ + ret =i2c_master_send (client, sht3x_cmd_break,SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) + goto out; + data->mode =0 ; + +if (mode >0 ) { +if (data->setup.high_precision) + command = periodic_measure_commands_hpm[mode -1 ]; +else + command = periodic_measure_commands_lpm[mode -1 ]; + +/* select mode */ + ret =i2c_master_send (client, command,SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) + goto out; + } + +/* select mode and command */ + data->mode = mode; +sht3x_select_command (data); + +out: +mutex_unlock (&data->i2c_lock); +mutex_unlock (&data->data_lock); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -EIO ; + +return count; +} + +static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, temp1_input_show, NULL, 0); +static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, humidity1_input_show, + NULL, 0); +static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, temp1_limit_show, + temp1_limit_store, 0); +static SENSOR_DEVICE_ATTR(humidity1_max, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, 0); +static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, temp1_limit_show, + temp1_limit_store, 1); +static SENSOR_DEVICE_ATTR(humidity1_max_hyst, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, 1); +static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, temp1_limit_show, + temp1_limit_store, 2); +static SENSOR_DEVICE_ATTR(humidity1_min, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, 2); +static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO | S_IWUSR, + temp1_limit_show, temp1_limit_store, 3); +static SENSOR_DEVICE_ATTR(humidity1_min_hyst, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, 3); +static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, temp1_alarm_show, NULL, 3); +static SENSOR_DEVICE_ATTR(humidity1_alarm, S_IRUGO, humidity1_alarm_show, + NULL, 3); +static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, + update_interval_show, update_interval_store, 0); + +static struct attribute *sht3x_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, + &sensor_dev_attr_humidity1_input.dev_attr.attr, + &sensor_dev_attr_temp1_max.dev_attr.attr, + &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, + &sensor_dev_attr_humidity1_max.dev_attr.attr, + &sensor_dev_attr_humidity1_max_hyst.dev_attr.attr, + &sensor_dev_attr_temp1_min.dev_attr.attr, + &sensor_dev_attr_temp1_min_hyst.dev_attr.attr, + &sensor_dev_attr_humidity1_min.dev_attr.attr, + &sensor_dev_attr_humidity1_min_hyst.dev_attr.attr, + &sensor_dev_attr_temp1_alarm.dev_attr.attr, + &sensor_dev_attr_humidity1_alarm.dev_attr.attr, + &sensor_dev_attr_update_interval.dev_attr.attr, +NULL +}; + +static struct attribute *sts3x_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, +NULL +}; + +ATTRIBUTE_GROUPS (sht3x); +ATTRIBUTE_GROUPS (sts3x); + +static int sht3x_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +int ret; +struct sht3x_data *data; +struct device *hwmon_dev; +struct i2c_adapter *adap = client->adapter; +struct device *dev = &client->dev; +const struct attribute_group **attribute_groups; + +/* +* we require full i2c support since the sht3x uses multi-byte read and +* writes as well as multi-byte commands which are not supported by +* the smbus protocol +*/ +if (!i2c_check_functionality (adap,I2C_FUNC_I2C )) +return -ENODEV ; + + ret =i2c_master_send (client, sht3x_cmd_clear_status_reg, +SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -ENODEV ; + + data =devm_kzalloc (dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->setup.blocking_io = false; + data->setup.high_precision = true; + data->mode =0 ; + data->last_update =0 ; + data->client = client; +crc8_populate_msb (sht3x_crc8_table,SHT3X_CRC8_POLYNOMIAL ); + +if (client->dev.platform_data) + data->setup = *(struct sht3x_platform_data *)dev->platform_data; + +sht3x_select_command (data); + +mutex_init (&data->i2c_lock); +mutex_init (&data->data_lock); + + ret =limits_update (data); +if (ret) +return ret; + +if (id->driver_data == sts3x) + attribute_groups = sts3x_groups; +else + attribute_groups = sht3x_groups; + + hwmon_dev =devm_hwmon_device_register_with_groups (dev, + client->name, + data, + attribute_groups); + +if (IS_ERR (hwmon_dev)) +dev_dbg (dev,"unable to register hwmon device\n" ); + +return PTR_ERR_OR_ZERO (hwmon_dev); +} + +/* device ID table */ +static const struct i2c_device_id sht3x_ids[] = { + {"sht3x" , sht3x}, + {"sts3x" , sts3x}, + {} +}; + +MODULE_DEVICE_TABLE (i2c, sht3x_ids); + +static struct i2c_driver sht3x_i2c_driver = { + .driver.name ="sht3x" , + .probe = sht3x_probe, + .id_table = sht3x_ids, +}; + +module_i2c_driver (sht3x_i2c_driver); + +MODULE_AUTHOR ("David Frey <[email protected]>" ); +MODULE_AUTHOR ("Pascal Sachs <[email protected]>" ); +MODULE_DESCRIPTION ("Sensirion SHT3x humidity and temperature sensor driver" ); +MODULE_LICENSE ("GPL" );include/linux/platform_data/sht3x.h new file +25
-0@@ -0,0 +1,25 @@ +/* +* Copyright (C) 2015 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef __SHT3X_H_ +#define __SHT3X_H_ + +struct sht3x_platform_data { +bool blocking_io; +bool high_precision; +}; +#endif /* __SHT3X_H_ */
PATCH v4 1/1 hwmon: add support for Sensirion SHT3x sensors
In reply to Pascal SachsFrom: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500.
---
Patch was generated against kernel v4.7-rc1
Documentation/hwmon/sht3x | 72 ++++ drivers/hwmon/Kconfig | 11 + drivers/hwmon/Makefile | 1 + drivers/hwmon/sht3x.c | 724 ++++++++++++++++++++++++++++++++++++ include/linux/platform_data/sht3x.h | 25 ++ 5 files changed, 833 insertions(+) create mode 100644 Documentation/hwmon/sht3x create mode 100644 drivers/hwmon/sht3x.c create mode 100644 include/linux/platform_data/sht3x.h
Documentation/hwmon/sht3x new file +72 -0
@@ -0,0 +1,72 @@ +Kernel driver sht3x +=================== + +Supported chips: + * Sensirion SHT3x-DIS + Prefix: 'sht3x' + Addresses scanned: none + Datasheet: http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_Datasheet_SHT3x_DIS.pdf + +Author: + David Frey <[email protected]> + Pascal Sachs <[email protected]> + +Description +----------- + +This driver implements support for the Sensirion SHT3x-DIS chip, a humidity +and temperature sensor. Temperature is measured in degrees celsius, relative +humidity is expressed as a percentage. In the sysfs interface, all values are +scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +The device communicates with the I2C protocol. Sensors can have the I2C +addresses 0x44 or 0x45, depending on the wiring. See +Documentation/i2c/instantiating-devices for methods to instantiate the device. + +There are two options configurable by means of sht3x_platform_data: +1. blocking (pull the I2C clock line down while performing the measurement) or + non-blocking mode. Blocking mode will guarantee the fastest result but + the I2C bus will be busy during that time. By default, non-blocking mode + is used. Make sure clock-stretching works properly on your device if you + want to use blocking mode. +2. high or low accuracy. High accuracy is used by default and using it is + strongly recommended. + +The sht3x sensor supports a single shot mode as well as 5 periodic measure +modes, which can be controlled with the update_interval sysfs interface. +The allowed update_interval in milliseconds are as follows: + * 0 single shot mode + * 2000 0.5 Hz periodic measurement + * 1000 1 Hz periodic measurement + * 500 2 Hz periodic measurement + * 250 4 Hz periodic measurement + * 100 10 Hz periodic measurement + +In the periodic measure mode, the sensor automatically triggers a measurement +with the configured update interval on the chip. When a temperature or humidity +reading exceeds the configured limits, the alert attribute is set to 1 and +the alert pin on the sensor is set to high. +When the temperature and humidity readings move back between the hysteresis +values, the alert bit is set to 0 and the alert pin on the sensor is set to +low. + +sysfs-Interface +--------------- + +temp1_input: temperature input +humidity1_input: humidity input +temp1_max: temperature max value +temp1_max_hyst: temperature hysteresis value for max limit +humidity1_max: humidity max value +humidity1_max_hyst: humidity hysteresis value for max limit +temp1_min: temperature min value +temp1_min_hyst: temperature hysteresis value for min limit +humidity1_min: humidity min value +humidity1_min_hyst: humidity hysteresis value for min limit +temp1_alarm: alarm flag is set to 1 if the temperature is outside the + configured limits. Alarm only works in periodic measure mode +humidity1_alarm: alarm flag is set to 1 if the humidity is outside the + configured limits. Alarm only works in periodic measure mode +update_interval: update interval, 0 for single shot, interval in msec + for periodic measurement. If the interval is not supported + by the sensor, the next faster interval is chosen
drivers/hwmon/Kconfig +11 -0
@@ -1265,6 +1265,17 @@ config SENSORS_SHT21 This driver can also be built as a module. If so, the module will be called sht21. +config SENSORS_SHT3x + tristate "Sensiron humidity and temperature sensors. SHT3x and compat." + depends on I2C + select CRC8 + help + If you say yes here you get support for the Sensiron SHT30 and SHT31 + humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht3x. + config SENSORS_SHTC1 tristate "Sensiron humidity and temperature sensors. SHTC1 and compat." depends on I2C
drivers/hwmon/Makefile +1 -0
@@ -138,6 +138,7 @@ obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o +obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.o
drivers/hwmon/sht3x.c new file +724 -0
724 lines not on this page · raw message
include/linux/platform_data/sht3x.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* Copyright (C) 2015 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef __SHT3X_H_ +#define __SHT3X_H_ + +struct sht3x_platform_data { +bool blocking_io; +bool high_precision; +}; +#endif /* __SHT3X_H_ */
Signature · 1 line
1.9.1
Guenter Roeck
maintainerHi Pascal,
Quoted 8 lines · Pascal Sachs
From: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500.
Getting there. Mostly nitpicks this time.
Quoted 4 lines
Signed-off-by Pascal Sachs <[email protected]>---
Patch was generated against kernel v4.7-rc1
A changelog would be useful.
Quoted 241 lines
Documentation/hwmon/sht3x | 72 ++++ drivers/hwmon/Kconfig | 11 + drivers/hwmon/Makefile | 1 + drivers/hwmon/sht3x.c | 724 ++++++++++++++++++++++++++++++++++++ include/linux/platform_data/sht3x.h | 25 ++ 5 files changed, 833 insertions(+)create mode 100644
Documentation/hwmon/sht3xcreate mode 100644drivers/hwmon/sht3x.ccreate mode 100644include/linux/platform_data/sht3x.hDocumentation/hwmon/sht3x new file +72
-0@@ -0,0 +1,72 @@ +Kernel driver sht3x +=================== + +Supported chips: + * Sensirion SHT3x-DIS + Prefix: 'sht3x' + Addresses scanned: none + Datasheet: http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_Datasheet_SHT3x_DIS.pdf + +Author: + David Frey <[email protected]> + Pascal Sachs <[email protected]> + +Description +----------- + +This driver implements support for the Sensirion SHT3x-DIS chip, a humidity +and temperature sensor. Temperature is measured in degrees celsius, relative +humidity is expressed as a percentage. In the sysfs interface, all values are +scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +The device communicates with the I2C protocol. Sensors can have the I2C +addresses 0x44 or 0x45, depending on the wiring. See +Documentation/i2c/instantiating-devices for methods to instantiate the device. + +There are two options configurable by means of sht3x_platform_data: +1. blocking (pull the I2C clock line down while performing the measurement) or + non-blocking mode. Blocking mode will guarantee the fastest result but + the I2C bus will be busy during that time. By default, non-blocking mode + is used. Make sure clock-stretching works properly on your device if you + want to use blocking mode. +2. high or low accuracy. High accuracy is used by default and using it is + strongly recommended. + +The sht3x sensor supports a single shot mode as well as 5 periodic measure +modes, which can be controlled with the update_interval sysfs interface. +The allowed update_interval in milliseconds are as follows: + * 0 single shot mode + * 2000 0.5 Hz periodic measurement + * 1000 1 Hz periodic measurement + * 500 2 Hz periodic measurement + * 250 4 Hz periodic measurement + * 100 10 Hz periodic measurement + +In the periodic measure mode, the sensor automatically triggers a measurement +with the configured update interval on the chip. When a temperature or humidity +reading exceeds the configured limits, the alert attribute is set to 1 and +the alert pin on the sensor is set to high. +When the temperature and humidity readings move back between the hysteresis +values, the alert bit is set to 0 and the alert pin on the sensor is set to +low. + +sysfs-Interface +--------------- + +temp1_input: temperature input +humidity1_input: humidity input +temp1_max: temperature max value +temp1_max_hyst: temperature hysteresis value for max limit +humidity1_max: humidity max value +humidity1_max_hyst: humidity hysteresis value for max limit +temp1_min: temperature min value +temp1_min_hyst: temperature hysteresis value for min limit +humidity1_min: humidity min value +humidity1_min_hyst: humidity hysteresis value for min limit +temp1_alarm: alarm flag is set to 1 if the temperature is outside the + configured limits. Alarm only works in periodic measure mode +humidity1_alarm: alarm flag is set to 1 if the humidity is outside the + configured limits. Alarm only works in periodic measure mode +update_interval: update interval, 0 for single shot, interval in msec + for periodic measurement. If the interval is not supported + by the sensor, the next faster interval is chosendrivers/hwmon/Kconfig +11
-0@@ -1265,6 +1265,17 @@ config SENSORS_SHT21 This driver can also be built as a module. If so, the module will be called sht21. +config SENSORS_SHT3x + tristate "Sensiron humidity and temperature sensors. SHT3x and compat." + depends on I2C + select CRC8 + help + If you say yes here you get support for the Sensiron SHT30 and SHT31 + humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht3x. + config SENSORS_SHTC1 tristate "Sensiron humidity and temperature sensors. SHTC1 and compat." depends on I2Cdrivers/hwmon/Makefile +1
-0@@ -138,6 +138,7 @@ obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o +obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.odrivers/hwmon/sht3x.c new file +113
-0@@ -0,0 +1,724 @@ +/* Sensirion SHT3x-DIS humidity and temperature sensor driver. +* The SHT3x comes in many different versions, this driver is for the +* I2C version only. +* +* Copyright (C) 2016 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#include <asm/page.h> +#include <linux/crc8.h> +#include <linux/delay.h> +#include <linux/err.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/i2c.h> +#include <linux/init.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/jiffies.h> +#include <linux/platform_data/sht3x.h> + +/* commands (high precision mode) */ +static const unsigned char sht3x_cmd_measure_blocking_hpm[] = {0x2c ,0x06 }; +static const unsigned char sht3x_cmd_measure_nonblocking_hpm[] = {0x24 ,0x00 }; + +/* commands (low power mode) */ +static const unsigned char sht3x_cmd_measure_blocking_lpm[] = {0x2c ,0x10 }; +static const unsigned char sht3x_cmd_measure_nonblocking_lpm[] = {0x24 ,0x16 }; + +/* commands for periodic mode */ +static const unsigned char sht3x_cmd_measure_periodic_mode[] = {0xe0 ,0x00 }; +static const unsigned char sht3x_cmd_break[] = {0x30 ,0x93 }; + +/* other commands */ +static const unsigned char sht3x_cmd_read_status_reg[] = {0xf3 ,0x2d }; +static const unsigned char sht3x_cmd_clear_status_reg[] = {0x30 ,0x41 }; + +/* delays for non-blocking i2c commands, both in us */ +#define SHT3X_NONBLOCKING_WAIT_TIME_HPM 15000 +#define SHT3X_NONBLOCKING_WAIT_TIME_LPM 4000 + +#define SHT3X_WORD_LEN 2 +#define SHT3X_CMD_LENGTH 2 +#define SHT3X_CRC8_LEN 1 +#define SHT3X_RESPONSE_LENGTH 6 +#define SHT3X_CRC8_POLYNOMIAL 0x31 +#define SHT3X_CRC8_INIT 0xFF + +enum sht3x_chips { + sht3x, + sts3x, +}; + +enum sht3x_limits { + limit_max =0 , + limit_max_hyst, + limit_min, + limit_min_hyst, +}; + +DECLARE_CRC8_TABLE (sht3x_crc8_table); + +/* periodic measure commands (high precision mode) */ +static const char periodic_measure_commands_hpm[][SHT3X_CMD_LENGTH ] = { +/* 0.5 measurements per second */ + {0x20 ,0x32 }, +/* 1 measurements per second */ + {0x21 ,0x30 }, +/* 2 measurements per second */ + {0x22 ,0x36 }, +/* 4 measurements per second */ + {0x23 ,0x34 }, +/* 10 measurements per second */ + {0x27 ,0x37 }, +}; + +/* periodic measure commands (low power mode) */ +static const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH ] = { +/* 0.5 measurements per second */ + {0x20 ,0x2f }, +/* 1 measurements per second */ + {0x21 ,0x2d }, +/* 2 measurements per second */ + {0x22 ,0x2b }, +/* 4 measurements per second */ + {0x23 ,0x29 }, +/* 10 measurements per second */ + {0x27 ,0x2a }, +}; + +struct sht3x_limit_commands { +const char read_command[SHT3X_CMD_LENGTH ]; +const char write_command[SHT3X_CMD_LENGTH ]; +}; + +static const struct sht3x_limit_commands limit_commands[] = { +/* temp1_max, humidity1_max */ + [limit_max] = { {0xe1 ,0x1f }, {0x61 ,0x1d } }, +/* temp_1_max_hyst, humidity1_max_hyst */ + [limit_max_hyst] = { {0xe1 ,0x14 }, {0x61 ,0x16 } },
drivers/hwmon/sht3x.c line 123
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+/* temp1_min, humidity1_min */ + [limit_min] = { {0xe1 ,0x02 }, {0x61 ,0x00 } }, + /* temp_1_min_hyst, humidity1_min_hyst */ + [limit_min_hyst] = { {0xe1, 0x09}, {0x61, 0x0B} }, +}; + +#define SHT3X_NUM_LIMIT_CMD ARRAY_SIZE(limit_commands) + +static const u16 mode_to_update_interval[] = { +0 ,
I think this is problematic. See below.
Quoted 85 lines
drivers/hwmon/sht3x.c new file +85
-0+2000 , +1000 , +500 , +250 , + 100, +}; + +struct sht3x_data { +struct i2c_client *client; +struct mutex i2c_lock;/* lock for sending i2c commands */ +struct mutex data_lock;/* lock for updating driver data */ + +u8 mode; +const unsigned char *command; +u32 wait_time;/* in us*/ +unsigned long last_update;/* last update in periodic mode*/ + +struct sht3x_platform_data setup; + +/* +* cached values for temperature and humidity and limits +* the limits arrays have the following order: +* max, max_hyst, min, min_hyst +*/ +int temperature; +int temperature_limits[SHT3X_NUM_LIMIT_CMD ]; +u32 humidity; +u32 humidity_limits[SHT3X_NUM_LIMIT_CMD ]; +}; + +static u8 get_mode_from_update_interval (u16 value) +{ +size_t index; +u8 number_of_modes =ARRAY_SIZE (mode_to_update_interval); + +if (value ==0 ) +return 0 ; + +/* find next faster update interval */ +for (index =1 ; index < number_of_modes; index++) { +if (mode_to_update_interval[index] <= value) +return index; + } + +return number_of_modes -1 ; +} + +static int sht3x_read_from_command (struct i2c_client *client, +struct sht3x_data *data, +const char *command, +char *buf,int length,u32 wait_time) +{ +int ret; + +mutex_lock (&data->i2c_lock); + ret =i2c_master_send (client, command,SHT3X_CMD_LENGTH ); + +if (ret !=SHT3X_CMD_LENGTH ) { + ret = ret <0 ? ret : -EIO ; + goto out; + } + +if (wait_time) +usleep_range (wait_time, wait_time +1000 ); + + ret =i2c_master_recv (client, buf, length); +if (ret != length) { + ret = ret <0 ? ret : -EIO ; + goto out; + } + + ret =0 ; +out: +mutex_unlock (&data->i2c_lock); +return ret; +} + +static int sht3x_extract_temperature (u16 raw) +{ +/* +* From datasheet: +* T = -45 + 175 * ST / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return ((21875 * (int )raw) >>13 ) -45000 ;
drivers/hwmon/sht3x.c line 218
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+} + +static u32 sht3x_extract_humidity (u16 raw) +{ +/* +* From datasheet: +* RH = 100 * SRH / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return (12500 * (int )raw) >>13 ;
Why typecast to "int" ? raw is u16, return value is u32.
drivers/hwmon/sht3x.c line 225
Quoted 7 lines
drivers/hwmon/sht3x.c new file +7
-0+} + +static struct sht3x_data *sht3x_update_client (struct device *dev) +{ +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +u16 interval_ms = mode_to_update_interval[data->mode];
If data->mode == 0, interval_ms == 0,
drivers/hwmon/sht3x.c line 226
Quoted 1 line
drivers/hwmon/sht3x.c new file +1
-0+unsigned long interval_jiffies =msecs_to_jiffies (interval_ms);
... interval_jiffies == 1,
drivers/hwmon/sht3x.c line 233
Quoted 7 lines
drivers/hwmon/sht3x.c new file +7
-0+unsigned char buf[SHT3X_RESPONSE_LENGTH ]; +u16 val; +int ret =0 ; + +mutex_lock (&data->data_lock); +/* only update once per interval in periodic mode */ +if (time_after (jiffies, data->last_update + interval_jiffies)) {
... and the command is executed every other timer tick. Is that intentional ?
Quoted 31 lines
drivers/hwmon/sht3x.c new file +31
-0+ ret =sht3x_read_from_command (client, data, data->command, buf, +sizeof (buf), data->wait_time); +if (ret) + goto out; + + val =be16_to_cpup ((__be16 *)buf); + data->temperature =sht3x_extract_temperature (val); + val =be16_to_cpup ((__be16 *)(buf +3 )); + data->humidity =sht3x_extract_humidity (val); + data->last_update = jiffies; + } + +out: +mutex_unlock (&data->data_lock); +if (ret) +return ERR_PTR (ret); + +return data; +} + +/* sysfs attributes */ +static ssize_t temp1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->temperature); +}
drivers/hwmon/sht3x.c line 274
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ +static ssize_t humidity1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->humidity);
humidity is u32 -> %u.
Quoted 31 lines
drivers/hwmon/sht3x.c new file +31
-0+} + +/* +* limits_update must only be called from probe or with data_lock held +*/ +static int limits_update (struct sht3x_data *data) +{ +int ret; +u8 index; +int temperature; +u32 humidity; +u16 raw; +char buffer[SHT3X_RESPONSE_LENGTH ]; +const struct sht3x_limit_commands *commands; +struct i2c_client *client = data->client; + +for (index =0 ; index <SHT3X_NUM_LIMIT_CMD ; index++) { + commands = &limit_commands[index]; + ret =sht3x_read_from_command (client, data, + commands->read_command, buffer, +SHT3X_RESPONSE_LENGTH ,0 ); + +if (ret) +return ret; + + raw =be16_to_cpup ((__be16 *)buffer); + temperature =sht3x_extract_temperature ((raw &0x01ff ) <<7 ); + humidity =sht3x_extract_humidity (raw &0xfe00 ); + data->temperature_limits[index] = temperature; + data->humidity_limits[index] = humidity; + }
drivers/hwmon/sht3x.c line 315
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ +return ret; +} + +static ssize_t temp1_limit_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int temperature_limit; +u8 index;
How about:
struct sht3x_data *data = dev_get_drvdata(dev); int temperature_limit = data->temperature_limits[index]; u8 index = to_sensor_dev_attr(attr)->index;
Quoted 9 lines
drivers/hwmon/sht3x.c new file +9
-0+struct sht3x_data *data =dev_get_drvdata (dev); + + index =to_sensor_dev_attr (attr)->index; + temperature_limit = data->temperature_limits[index]; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , temperature_limit); +} + +static ssize_t humidity1_limit_show(struct device *dev,
drivers/hwmon/sht3x.c line 334
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+struct device_attribute *attr, + char *buf) +{ + int humidity_limit; +u8 index; +struct sht3x_data *data =dev_get_drvdata (dev); + + index =to_sensor_dev_attr (attr)->index; + humidity_limit = data->humidity_limits[index]; +
Initialize with declaration ?
drivers/hwmon/sht3x.c line 335
Quoted 1 line
drivers/hwmon/sht3x.c new file +1
-0+return scnprintf (buf,PAGE_SIZE ,"%d\n" , humidity_limit);
data->humidity_limit[] is u32, so humidity_limit should be u32 as well.
Quoted 8 lines
drivers/hwmon/sht3x.c new file +8
-0+} + +static size_t limit_store (struct device *dev, +struct device_attribute *attr, +size_t count, +int temperature, +u32 humidity) +{
drivers/hwmon/sht3x.c line 353
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+char buffer[SHT3X_CMD_LENGTH +SHT3X_WORD_LEN +SHT3X_CRC8_LEN ]; +char *position = buffer; +int ret; +u16 raw; +u8 index; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +const struct sht3x_limit_commands *commands; + + index =to_sensor_dev_attr (attr)->index;
Initialize with declaration ?
Quoted 27 lines
drivers/hwmon/sht3x.c new file +27
-0+ commands = &limit_commands[index]; + +memcpy (position, commands->write_command,SHT3X_CMD_LENGTH ); + position +=SHT3X_CMD_LENGTH ; +/* +* ST = (T + 45) / 175 * 2^16 +* SRH = RH / 100 * 2^16 +* adapted for fixed point arithmetic and packed the same as +* in limit_show() +*/ + raw = ((u32)(temperature +45000 ) *24543 ) >> (16 +7 ); + raw |= ((humidity *42950 ) >>16 ) &0xfe00 ; + + *((__be16 *)position) =cpu_to_be16 (raw); + position +=SHT3X_WORD_LEN ; + *position =crc8 (sht3x_crc8_table, + position -SHT3X_WORD_LEN , +SHT3X_WORD_LEN , +SHT3X_CRC8_INIT ); + +mutex_lock (&data->data_lock); +mutex_lock (&data->i2c_lock); + ret =i2c_master_send (client, buffer,sizeof (buffer)); +mutex_unlock (&data->i2c_lock); + +if (ret !=sizeof (buffer)) + goto out;
drivers/hwmon/sht3x.c line 390
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ + data->temperature_limits[index] = temperature; + data->humidity_limits[index] = humidity; + +out: +mutex_unlock (&data->data_lock); +if (ret !=sizeof (buffer)) +return ret <0 ? ret : -EIO ; + +return count;
I would prefer something like
if (ret != sizeof(buffer)) {
if (ret >= 0)
ret = -EIO;
goto out;
}
ret = count;
data->temperature_limits[index] = temperature;
data->humidity_limits[index] = humidity;
out:
mutex_unlock(&data->data_lock);
return ret;which I think would be cleaner. See below though for locking issues.
Quoted 5 lines
drivers/hwmon/sht3x.c new file +5
-0+} + +static ssize_t temp1_limit_store(struct device *dev, + struct device_attribute *attr, + const char *buf,
drivers/hwmon/sht3x.c line 405
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ size_t count) +{ +int temperature; +u32 humidity; +u8 index; +int ret; +struct sht3x_data *data =dev_get_drvdata (dev); + + ret =kstrtoint (buf,0 , &temperature); +
No empty line here please.
drivers/hwmon/sht3x.c line 407
Quoted 2 lines
drivers/hwmon/sht3x.c new file +2
-0+if (ret) +return -EINVAL ;
return ret;
drivers/hwmon/sht3x.c line 411
Quoted 4 lines
drivers/hwmon/sht3x.c new file +4
-0+ + temperature =clamp_val (temperature,-45000 ,130000 ); + + index =to_sensor_dev_attr (attr)->index;
Initialize with declaration ?
drivers/hwmon/sht3x.c line 413
Quoted 2 lines
drivers/hwmon/sht3x.c new file +2
-0+ humidity = data->humidity_limits[index]; +
You need to read humidity with the lock active, to avoid inconsistencies if humidity is updated in parallel.
Maybe
lock() ret = limit_store(dev, attr, count, temperature, data->humidity_limits[index]); unlock();
limit_store() only uses attr to get the index (again). Maybe just pass index ?
Quoted 8 lines
drivers/hwmon/sht3x.c new file +8
-0+ ret =limit_store (dev, attr, count, temperature, humidity); + +return ret; +} + +static ssize_t humidity1_limit_store(struct device *dev, + struct device_attribute *attr, + const char *buf,
drivers/hwmon/sht3x.c line 431
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ size_t count) +{ +int temperature; +u32 humidity; +u8 index; +int ret; +struct sht3x_data *data =dev_get_drvdata (dev); + + ret =kstrtou32 (buf,0 , &humidity); +
No empty line here please.
drivers/hwmon/sht3x.c line 433
Quoted 2 lines
drivers/hwmon/sht3x.c new file +2
-0+if (ret) +return -EINVAL ;
return ret;
drivers/hwmon/sht3x.c line 436
Quoted 3 lines
drivers/hwmon/sht3x.c new file +3
-0+ + humidity =clamp_val (humidity,0 ,100000 ); + index =to_sensor_dev_attr (attr)->index;
Initialize index with declaration ?
drivers/hwmon/sht3x.c line 437
Quoted 1 line
drivers/hwmon/sht3x.c new file +1
-0+ temperature = data->temperature_limits[index];
Same problem as above with locking.
Quoted 29 lines
drivers/hwmon/sht3x.c new file +29
-0+ + ret =limit_store (dev, attr, count, temperature, humidity); + +return ret; +} + +static void sht3x_select_command (struct sht3x_data *data) +{ +/* +* In blocking mode (clock stretching mode) the I2C bus +* is blocked for other traffic, thus the call to i2c_master_recv() +* will wait until the data is ready. For non blocking mode, we +* have to wait ourselves. +*/ +if (data->mode >0 ) { + data->command = sht3x_cmd_measure_periodic_mode; + data->wait_time =0 ; + }else if (data->setup.blocking_io) { + data->command = data->setup.high_precision ? + sht3x_cmd_measure_blocking_hpm : + sht3x_cmd_measure_blocking_lpm; + data->wait_time =0 ; + }else { +if (data->setup.high_precision) { + data->command = sht3x_cmd_measure_nonblocking_hpm; + data->wait_time =SHT3X_NONBLOCKING_WAIT_TIME_HPM ; + }else { + data->command = sht3x_cmd_measure_nonblocking_lpm; + data->wait_time =SHT3X_NONBLOCKING_WAIT_TIME_LPM ;
drivers/hwmon/sht3x.c line 476
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ } + } +} + +static int status_register_read (struct device *dev, +struct device_attribute *attr, +char *buffer,int length) +{ +int ret; +
No empty line here please.
Quoted 9 lines
drivers/hwmon/sht3x.c new file +9
-0+struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + + ret =sht3x_read_from_command (client, data, sht3x_cmd_read_status_reg, + buffer, length,0 ); + +return ret; +} +
drivers/hwmon/sht3x.c line 495
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+static ssize_t temp1_alarm_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int ret; +int length =SHT3X_WORD_LEN +SHT3X_CRC8_LEN ; +char buffer[length]; + + ret =status_register_read (dev, attr, buffer, length); +
No empty line here please.
Quoted 6 lines
drivers/hwmon/sht3x.c new file +6
-0+if (ret) +return ret; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , !!(buffer[0 ] &0x04 )); +} +
drivers/hwmon/sht3x.c line 511
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+static ssize_t humidity1_alarm_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int ret; +int length =SHT3X_WORD_LEN +SHT3X_CRC8_LEN ; +char buffer[length]; + + ret =status_register_read (dev, attr, buffer, length); +
No empty line here please.
Quoted 4 lines
drivers/hwmon/sht3x.c new file +4
-0+if (ret) +return ret; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , !!(buffer[0 ] &0x08 ));
drivers/hwmon/sht3x.c line 525
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+} + +static ssize_t update_interval_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct sht3x_data *data =dev_get_drvdata (dev); + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , + mode_to_update_interval[data->mode]);
%u
Quoted 6 lines
drivers/hwmon/sht3x.c new file +6
-0+} + +static ssize_t update_interval_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count)
drivers/hwmon/sht3x.c line 541
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+{ +u16 update_interval; +u8 mode; +int ret; +const char *command; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + + ret =kstrtou16 (buf,0 , &update_interval); +
No empty line here please.
drivers/hwmon/sht3x.c line 543
Quoted 2 lines
drivers/hwmon/sht3x.c new file +2
-0+if (ret) +return -EINVAL ;
return ret;
drivers/hwmon/sht3x.c line 550
Quoted 7 lines
drivers/hwmon/sht3x.c new file +7
-0+ + mode =get_mode_from_update_interval (update_interval); + +/* mode did not change */ +if (mode == data->mode) +return count; +
That doesn't really mean anything here, since the check is outside the lock, and mode may change right after you checked it. Either check from within the lock or not at all.
drivers/hwmon/sht3x.c line 558
Quoted 8 lines
drivers/hwmon/sht3x.c new file +8
-0+mutex_lock (&data->i2c_lock); +mutex_lock (&data->data_lock); +/* abort periodic measure */ + ret =i2c_master_send (client, sht3x_cmd_break,SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) + goto out; + data->mode =0 ; +
Can you explain why you set data->mode = 0 here ?
Quoted 9 lines
drivers/hwmon/sht3x.c new file +9
-0+if (mode >0 ) { +if (data->setup.high_precision) + command = periodic_measure_commands_hpm[mode -1 ]; +else + command = periodic_measure_commands_lpm[mode -1 ]; + +/* select mode */ + ret =i2c_master_send (client, command,SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH )
drivers/hwmon/sht3x.c line 577
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ goto out; + } + +/* select mode and command */ + data->mode = mode; +sht3x_select_command (data); + +out: +mutex_unlock (&data->data_lock); +mutex_unlock (&data->i2c_lock);
Reverse order would be better for lock/unlock since elsewhere you lock data_lock first, followed by i2c_lock.
Quoted 153 lines
drivers/hwmon/sht3x.c new file +147
-0+if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -EIO ; + +return count; +} + +static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, temp1_input_show, NULL, 0); +static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, humidity1_input_show, + NULL, 0); +static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, + temp1_limit_show, temp1_limit_store, + limit_max); +static SENSOR_DEVICE_ATTR(humidity1_max, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, + limit_max); +static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, + temp1_limit_show, temp1_limit_store, + limit_max_hyst); +static SENSOR_DEVICE_ATTR(humidity1_max_hyst, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, + limit_max_hyst); +static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, + temp1_limit_show, temp1_limit_store, + limit_min); +static SENSOR_DEVICE_ATTR(humidity1_min, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, + limit_min); +static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO | S_IWUSR, + temp1_limit_show, temp1_limit_store, + limit_min_hyst); +static SENSOR_DEVICE_ATTR(humidity1_min_hyst, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, + limit_min_hyst); +static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, temp1_alarm_show, NULL, 0); +static SENSOR_DEVICE_ATTR(humidity1_alarm, S_IRUGO, humidity1_alarm_show, + NULL, 0); +static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, + update_interval_show, update_interval_store, 0); + +static struct attribute *sht3x_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, + &sensor_dev_attr_humidity1_input.dev_attr.attr, + &sensor_dev_attr_temp1_max.dev_attr.attr, + &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, + &sensor_dev_attr_humidity1_max.dev_attr.attr, + &sensor_dev_attr_humidity1_max_hyst.dev_attr.attr, + &sensor_dev_attr_temp1_min.dev_attr.attr, + &sensor_dev_attr_temp1_min_hyst.dev_attr.attr, + &sensor_dev_attr_humidity1_min.dev_attr.attr, + &sensor_dev_attr_humidity1_min_hyst.dev_attr.attr, + &sensor_dev_attr_temp1_alarm.dev_attr.attr, + &sensor_dev_attr_humidity1_alarm.dev_attr.attr, + &sensor_dev_attr_update_interval.dev_attr.attr, +NULL +}; + +static struct attribute *sts3x_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, +NULL +}; + +ATTRIBUTE_GROUPS (sht3x); +ATTRIBUTE_GROUPS (sts3x); + +static int sht3x_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +int ret; +struct sht3x_data *data; +struct device *hwmon_dev; +struct i2c_adapter *adap = client->adapter; +struct device *dev = &client->dev; +const struct attribute_group **attribute_groups; + +/* +* we require full i2c support since the sht3x uses multi-byte read and +* writes as well as multi-byte commands which are not supported by +* the smbus protocol +*/ +if (!i2c_check_functionality (adap,I2C_FUNC_I2C )) +return -ENODEV ; + + ret =i2c_master_send (client, sht3x_cmd_clear_status_reg, +SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -ENODEV ; + + data =devm_kzalloc (dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->setup.blocking_io = false; + data->setup.high_precision = true; + data->mode =0 ; + data->last_update =0 ; + data->client = client; +crc8_populate_msb (sht3x_crc8_table,SHT3X_CRC8_POLYNOMIAL ); + +if (client->dev.platform_data) + data->setup = *(struct sht3x_platform_data *)dev->platform_data; + +sht3x_select_command (data); + +mutex_init (&data->i2c_lock); +mutex_init (&data->data_lock); + + ret =limits_update (data); +if (ret) +return ret; + +if (id->driver_data == sts3x) + attribute_groups = sts3x_groups; +else + attribute_groups = sht3x_groups; + + hwmon_dev =devm_hwmon_device_register_with_groups (dev, + client->name, + data, + attribute_groups); + +if (IS_ERR (hwmon_dev)) +dev_dbg (dev,"unable to register hwmon device\n" ); + +return PTR_ERR_OR_ZERO (hwmon_dev); +} + +/* device ID table */ +static const struct i2c_device_id sht3x_ids[] = { + {"sht3x" , sht3x}, + {"sts3x" , sts3x}, + {} +}; + +MODULE_DEVICE_TABLE (i2c, sht3x_ids); + +static struct i2c_driver sht3x_i2c_driver = { + .driver.name ="sht3x" , + .probe = sht3x_probe, + .id_table = sht3x_ids, +}; + +module_i2c_driver (sht3x_i2c_driver); + +MODULE_AUTHOR ("David Frey <[email protected]>" ); +MODULE_AUTHOR ("Pascal Sachs <[email protected]>" ); +MODULE_DESCRIPTION ("Sensirion SHT3x humidity and temperature sensor driver" ); +MODULE_LICENSE ("GPL" );include/linux/platform_data/sht3x.h new file +0
-0
include/linux/platform_data/sht3x.h line 2
Quoted 2 lines
include/linux/platform_data/sht3x.h new file +2
-0@@ -0,0 +1,25 @@ +/* +* Copyright (C) 2015 Sensirion AG, Switzerland
2016 ?
Quoted 24 lines
include/linux/platform_data/sht3x.h new file +23
-0+* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef __SHT3X_H_ +#define __SHT3X_H_ + +struct sht3x_platform_data { +bool blocking_io; +bool high_precision; +}; +#endif /* __SHT3X_H_ */
Hi Guenter
I want to thank you again for the fast and detailed code review. I really appreciate it.
Am 30.05.2016 um 17:45 schrieb Guenter Roeck:
Quoted 12 lines
Hi Pascal,
Quoted 8 lines · Pascal Sachs
From: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500.
Quoted 7 lines
Getting there. Mostly nitpicks this time.
Quoted 4 lines
Signed-off-by Pascal Sachs <[email protected]>---
Patch was generated against kernel v4.7-rc1A changelog would be useful.
Sure, I will add one with the next submission
Quoted 410 lines
Quoted 277 lines
Documentation/hwmon/sht3x | 72 ++++ drivers/hwmon/Kconfig | 11 + drivers/hwmon/Makefile | 1 + drivers/hwmon/sht3x.c | 724 ++++++++++++++++++++++++++++++++++++ include/linux/platform_data/sht3x.h | 25 ++ 5 files changed, 833 insertions(+)create mode 100644
Documentation/hwmon/sht3xcreate mode 100644drivers/hwmon/sht3x.ccreate mode 100644include/linux/platform_data/sht3x.hDocumentation/hwmon/sht3x new file +8
-0@@ -0,0 +1,72 @@ +Kernel driver sht3x +=================== + +Supported chips: + * Sensirion SHT3x-DIS + Prefix: 'sht3x' + Addresses scanned: none + Datasheet:Documentation/hwmon/sht3x new file +9
-0+ +Author: + David Frey <[email protected]> + Pascal Sachs <[email protected]> + +Description +----------- + +This driver implements support for the Sensirion SHT3x-DIS chip, ahumidity
+and temperature sensor. Temperature is measured in degrees celsius, relative
+humidity is expressed as a percentage. In the sysfs interface, all values areDocumentation/hwmon/sht3x new file +5
-0+scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +The device communicates with the I2C protocol. Sensors can have the I2C +addresses 0x44 or 0x45, depending on the wiring. See +Documentation/i2c/instantiating-devices for methods to instantiatethe device.
Documentation/hwmon/sht3x new file +3
-0+ +There are two options configurable by means of sht3x_platform_data: +1. blocking (pull the I2C clock line down while performing themeasurement) or
+ non-blocking mode. Blocking mode will guarantee the fastest
result but
+ the I2C bus will be busy during that time. By default,
non-blocking mode
+ is used. Make sure clock-stretching works properly on your device
if you
+ want to use blocking mode.
+2. high or low accuracy. High accuracy is used by default and using it isDocumentation/hwmon/sht3x new file +3
-0+ strongly recommended. + +The sht3x sensor supports a single shot mode as well as 5 periodicmeasure
+modes, which can be controlled with theupdate_intervalsysfs interface.Documentation/hwmon/sht3x new file +9
-0+The allowed update_interval in milliseconds are as follows: + * 0 single shot mode + * 2000 0.5 Hz periodic measurement + * 1000 1 Hz periodic measurement + * 500 2 Hz periodic measurement + * 250 4 Hz periodic measurement + * 100 10 Hz periodic measurement + +In the periodic measure mode, the sensor automatically triggers ameasurement
+with the configured update interval on the chip. When a temperature or humidity
+reading exceeds the configured limits, the alert attribute is set to 1 andDocumentation/hwmon/sht3x new file +2
-0+the alert pin on the sensor is set to high. +When the temperature and humidity readings move back between thehysteresis
+values, the alert bit is set to 0 and the alert pin on the sensor is set toDocumentation/hwmon/sht3x new file +16
-0+low. + +sysfs-Interface +--------------- + +temp1_input: temperature input +humidity1_input: humidity input +temp1_max: temperature max value +temp1_max_hyst: temperature hysteresis value for max limit +humidity1_max: humidity max value +humidity1_max_hyst: humidity hysteresis value for max limit +temp1_min: temperature min value +temp1_min_hyst: temperature hysteresis value for min limit +humidity1_min: humidity min value +humidity1_min_hyst: humidity hysteresis value for min limit +temp1_alarm: alarm flag is set to 1 if the temperature isoutside the
+ configured limits. Alarm only works in periodic
measure mode
+humidity1_alarm: alarm flag is set to 1 if the humidity is
outside the
+ configured limits. Alarm only works in periodic
measure mode
+update_interval: update interval, 0 for single shot, interval in
msec
+ for periodic measurement. If the interval is not
supportedDocumentation/hwmon/sht3x new file +1
-0+ by the sensor, the next faster interval is chosendrivers/hwmon/Kconfig +2
-0@@ -1265,6 +1265,17 @@ config SENSORS_SHT21 This driver can also be built as a module. If so, the module will be called sht21. +config SENSORS_SHT3x + tristate "Sensiron humidity and temperature sensors. SHT3x andcompat."
drivers/hwmon/Kconfig +4
-0+ depends on I2C + select CRC8 + help + If you say yes here you get support for the Sensiron SHT30 andSHT31
drivers/hwmon/Kconfig +5
-0+ humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht3x. + config SENSORS_SHTC1 tristate "Sensiron humidity and temperature sensors. SHTC1 andcompat."
depends on I2Cdrivers/hwmon/Makefile +1
-0@@ -138,6 +138,7 @@ obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o +obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.odrivers/hwmon/sht3x.c new file +36
-0@@ -0,0 +1,724 @@ +/* Sensirion SHT3x-DIS humidity and temperature sensor driver. +* The SHT3x comes in many different versions, this driver is for the +* I2C version only. +* +* Copyright (C) 2016 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#include <asm/page.h> +#include <linux/crc8.h> +#include <linux/delay.h> +#include <linux/err.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/i2c.h> +#include <linux/init.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/jiffies.h> +#include <linux/platform_data/sht3x.h> + +/* commands (high precision mode) */ +static const unsigned char sht3x_cmd_measure_blocking_hpm[] = {
0x2c,0x06};
+static const unsigned char sht3x_cmd_measure_nonblocking_hpm[] = {0x24,0x00};drivers/hwmon/sht3x.c new file +3
-0+ +/* commands (low power mode) */ +static const unsigned char sht3x_cmd_measure_blocking_lpm[] = {
0x2c,0x10};
+static const unsigned char sht3x_cmd_measure_nonblocking_lpm[] = {0x24,0x16};drivers/hwmon/sht3x.c new file +3
-0+ +/* commands for periodic mode */ +static const unsigned char sht3x_cmd_measure_periodic_mode[] = {
0xe0,0x00};drivers/hwmon/sht3x.c new file +4
-0+static const unsigned char sht3x_cmd_break[] = {0x30 ,0x93 }; + +/* other commands */ +static const unsigned char sht3x_cmd_read_status_reg[] = {0xf3 ,
0x2d};
+static const unsigned char sht3x_cmd_clear_status_reg[] = {0x30,0x41};drivers/hwmon/sht3x.c new file +64
-0+ +/* delays for non-blocking i2c commands, both in us */ +#define SHT3X_NONBLOCKING_WAIT_TIME_HPM 15000 +#define SHT3X_NONBLOCKING_WAIT_TIME_LPM 4000 + +#define SHT3X_WORD_LEN 2 +#define SHT3X_CMD_LENGTH 2 +#define SHT3X_CRC8_LEN 1 +#define SHT3X_RESPONSE_LENGTH 6 +#define SHT3X_CRC8_POLYNOMIAL 0x31 +#define SHT3X_CRC8_INIT 0xFF + +enum sht3x_chips { + sht3x, + sts3x, +}; + +enum sht3x_limits { + limit_max =0 , + limit_max_hyst, + limit_min, + limit_min_hyst, +}; + +DECLARE_CRC8_TABLE (sht3x_crc8_table); + +/* periodic measure commands (high precision mode) */ +static const char periodic_measure_commands_hpm[][SHT3X_CMD_LENGTH ] = { +/* 0.5 measurements per second */ + {0x20 ,0x32 }, +/* 1 measurements per second */ + {0x21 ,0x30 }, +/* 2 measurements per second */ + {0x22 ,0x36 }, +/* 4 measurements per second */ + {0x23 ,0x34 }, +/* 10 measurements per second */ + {0x27 ,0x37 }, +}; + +/* periodic measure commands (low power mode) */ +static const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH ] = { +/* 0.5 measurements per second */ + {0x20 ,0x2f }, +/* 1 measurements per second */ + {0x21 ,0x2d }, +/* 2 measurements per second */ + {0x22 ,0x2b }, +/* 4 measurements per second */ + {0x23 ,0x29 }, +/* 10 measurements per second */ + {0x27 ,0x2a }, +}; + +struct sht3x_limit_commands { +const char read_command[SHT3X_CMD_LENGTH ]; +const char write_command[SHT3X_CMD_LENGTH ]; +}; + +static const struct sht3x_limit_commands limit_commands[] = { +/* temp1_max, humidity1_max */ + [limit_max] = { {0xe1 ,0x1f }, {0x61 ,0x1d } }, +/* temp_1_max_hyst, humidity1_max_hyst */ + [limit_max_hyst] = { {0xe1 ,0x14 }, {0x61 ,0x16 } },drivers/hwmon/sht3x.c line 123
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+/* temp1_min, humidity1_min */ + [limit_min] = { {0xe1 ,0x02 }, {0x61 ,0x00 } }, + /* temp_1_min_hyst, humidity1_min_hyst */ + [limit_min_hyst] = { {0xe1, 0x09}, {0x61, 0x0B} }, +}; + +#define SHT3X_NUM_LIMIT_CMD ARRAY_SIZE(limit_commands) + +static const u16 mode_to_update_interval[] = { +0 ,I think this is problematic. See below.
Quoted 85 lines
drivers/hwmon/sht3x.c new file +85
-0+2000 , +1000 , +500 , +250 , + 100, +}; + +struct sht3x_data { +struct i2c_client *client; +struct mutex i2c_lock;/* lock for sending i2c commands */ +struct mutex data_lock;/* lock for updating driver data */ + +u8 mode; +const unsigned char *command; +u32 wait_time;/* in us*/ +unsigned long last_update;/* last update in periodic mode*/ + +struct sht3x_platform_data setup; + +/* +* cached values for temperature and humidity and limits +* the limits arrays have the following order: +* max, max_hyst, min, min_hyst +*/ +int temperature; +int temperature_limits[SHT3X_NUM_LIMIT_CMD ]; +u32 humidity; +u32 humidity_limits[SHT3X_NUM_LIMIT_CMD ]; +}; + +static u8 get_mode_from_update_interval (u16 value) +{ +size_t index; +u8 number_of_modes =ARRAY_SIZE (mode_to_update_interval); + +if (value ==0 ) +return 0 ; + +/* find next faster update interval */ +for (index =1 ; index < number_of_modes; index++) { +if (mode_to_update_interval[index] <= value) +return index; + } + +return number_of_modes -1 ; +} + +static int sht3x_read_from_command (struct i2c_client *client, +struct sht3x_data *data, +const char *command, +char *buf,int length,u32 wait_time) +{ +int ret; + +mutex_lock (&data->i2c_lock); + ret =i2c_master_send (client, command,SHT3X_CMD_LENGTH ); + +if (ret !=SHT3X_CMD_LENGTH ) { + ret = ret <0 ? ret : -EIO ; + goto out; + } + +if (wait_time) +usleep_range (wait_time, wait_time +1000 ); + + ret =i2c_master_recv (client, buf, length); +if (ret != length) { + ret = ret <0 ? ret : -EIO ; + goto out; + } + + ret =0 ; +out: +mutex_unlock (&data->i2c_lock); +return ret; +} + +static int sht3x_extract_temperature (u16 raw) +{ +/* +* From datasheet: +* T = -45 + 175 * ST / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return ((21875 * (int )raw) >>13 ) -45000 ;drivers/hwmon/sht3x.c line 218
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+} + +static u32 sht3x_extract_humidity (u16 raw) +{ +/* +* From datasheet: +* RH = 100 * SRH / 2^16 +* Adapted for integer fixed point (3 digit) arithmetic. +*/ +return (12500 * (int )raw) >>13 ;Why typecast to "int" ? raw is u16, return value is u32.
drivers/hwmon/sht3x.c line 225
Quoted 7 lines
drivers/hwmon/sht3x.c new file +7
-0+} + +static struct sht3x_data *sht3x_update_client (struct device *dev) +{ +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +u16 interval_ms = mode_to_update_interval[data->mode];If
data->mode== 0,interval_ms== 0,Quoted 1 line
+ unsigned long
interval_jiffies=msecs_to_jiffies(interval_ms);...
interval_jiffies== 1,drivers/hwmon/sht3x.c line 233
Quoted 7 lines
drivers/hwmon/sht3x.c new file +7
-0+unsigned char buf[SHT3X_RESPONSE_LENGTH ]; +u16 val; +int ret =0 ; + +mutex_lock (&data->data_lock); +/* only update once per interval in periodic mode */ +if (time_after (jiffies, data->last_update + interval_jiffies)) {
Quoted 1 line
... and the command is executed every other timer tick. Is that intentional ?
Yes, this is how it should behave. In single shot mode the sensor measures values on demand, which means every time the sysfs interface is called, a measurement is triggered. In periodic mode however the measurement process is handled internally by the sensor and read out only makes sense if a new reading is available.
Quoted 90 lines
Quoted 33 lines
drivers/hwmon/sht3x.c new file +31
-0+ ret =sht3x_read_from_command (client, data, data->command, buf, +sizeof (buf), data->wait_time); +if (ret) + goto out; + + val =be16_to_cpup ((__be16 *)buf); + data->temperature =sht3x_extract_temperature (val); + val =be16_to_cpup ((__be16 *)(buf +3 )); + data->humidity =sht3x_extract_humidity (val); + data->last_update = jiffies; + } + +out: +mutex_unlock (&data->data_lock); +if (ret) +return ERR_PTR (ret); + +return data; +} + +/* sysfs attributes */ +static ssize_t temp1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->temperature); +}drivers/hwmon/sht3x.c line 274
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ +static ssize_t humidity1_input_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct sht3x_data *data =sht3x_update_client (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%d\n" , data->humidity);humidity is u32 -> %u.
Quoted 31 lines
drivers/hwmon/sht3x.c new file +31
-0+} + +/* +* limits_update must only be called from probe or with data_lock held +*/ +static int limits_update (struct sht3x_data *data) +{ +int ret; +u8 index; +int temperature; +u32 humidity; +u16 raw; +char buffer[SHT3X_RESPONSE_LENGTH ]; +const struct sht3x_limit_commands *commands; +struct i2c_client *client = data->client; + +for (index =0 ; index <SHT3X_NUM_LIMIT_CMD ; index++) { + commands = &limit_commands[index]; + ret =sht3x_read_from_command (client, data, + commands->read_command, buffer, +SHT3X_RESPONSE_LENGTH ,0 ); + +if (ret) +return ret; + + raw =be16_to_cpup ((__be16 *)buffer); + temperature =sht3x_extract_temperature ((raw &0x01ff ) <<7 ); + humidity =sht3x_extract_humidity (raw &0xfe00 ); + data->temperature_limits[index] = temperature; + data->humidity_limits[index] = humidity; + }drivers/hwmon/sht3x.c line 315
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ +return ret; +} + +static ssize_t temp1_limit_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int temperature_limit; +u8 index;How about:
Quoted 3 lines
struct sht3x_data *data = dev_get_drvdata(dev);
int temperature_limit = data->temperature_limits[index];
u8 index = to_sensor_dev_attr(attr)->index;Ok, I can change that
Quoted 101 lines
Quoted 11 lines
drivers/hwmon/sht3x.c new file +9
-0+struct sht3x_data *data =dev_get_drvdata (dev); + + index =to_sensor_dev_attr (attr)->index; + temperature_limit = data->temperature_limits[index]; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , temperature_limit); +} + +static ssize_t humidity1_limit_show(struct device *dev,drivers/hwmon/sht3x.c line 334
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+struct device_attribute *attr, + char *buf) +{ + int humidity_limit; +u8 index; +struct sht3x_data *data =dev_get_drvdata (dev); + + index =to_sensor_dev_attr (attr)->index; + humidity_limit = data->humidity_limits[index]; +Initialize with declaration ?
Quoted 1 line
+ return
scnprintf(buf, PAGE_SIZE, "%d\n", humidity_limit);data->humidity_limit[] is u32, so
humidity_limitshould be u32 as well.Quoted 8 lines
drivers/hwmon/sht3x.c new file +8
-0+} + +static size_t limit_store (struct device *dev, +struct device_attribute *attr, +size_t count, +int temperature, +u32 humidity) +{drivers/hwmon/sht3x.c line 353
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+char buffer[SHT3X_CMD_LENGTH +SHT3X_WORD_LEN +SHT3X_CRC8_LEN ]; +char *position = buffer; +int ret; +u16 raw; +u8 index; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +const struct sht3x_limit_commands *commands; + + index =to_sensor_dev_attr (attr)->index;Initialize with declaration ?
Quoted 27 lines
drivers/hwmon/sht3x.c new file +27
-0+ commands = &limit_commands[index]; + +memcpy (position, commands->write_command,SHT3X_CMD_LENGTH ); + position +=SHT3X_CMD_LENGTH ; +/* +* ST = (T + 45) / 175 * 2^16 +* SRH = RH / 100 * 2^16 +* adapted for fixed point arithmetic and packed the same as +* in limit_show() +*/ + raw = ((u32)(temperature +45000 ) *24543 ) >> (16 +7 ); + raw |= ((humidity *42950 ) >>16 ) &0xfe00 ; + + *((__be16 *)position) =cpu_to_be16 (raw); + position +=SHT3X_WORD_LEN ; + *position =crc8 (sht3x_crc8_table, + position -SHT3X_WORD_LEN , +SHT3X_WORD_LEN , +SHT3X_CRC8_INIT ); + +mutex_lock (&data->data_lock); +mutex_lock (&data->i2c_lock); + ret =i2c_master_send (client, buffer,sizeof (buffer)); +mutex_unlock (&data->i2c_lock); + +if (ret !=sizeof (buffer)) + goto out;drivers/hwmon/sht3x.c line 390
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ + data->temperature_limits[index] = temperature; + data->humidity_limits[index] = humidity; + +out: +mutex_unlock (&data->data_lock); +if (ret !=sizeof (buffer)) +return ret <0 ? ret : -EIO ; + +return count;I would prefer something like
if (ret != sizeof(buffer)) { if (ret >= 0) ret = -EIO; goto out; } ret = count; data->temperature_limits[index] = temperature; data->humidity_limits[index] = humidity; out: mutex_unlock(&data->data_lock); return ret;
Quoted 1 line
which I think would be cleaner. See below though for locking issues.
OK, wasn't sure if it is appropriate to handle error and valid return in the same variable
Quoted 38 lines
Quoted 7 lines
drivers/hwmon/sht3x.c new file +5
-0+} + +static ssize_t temp1_limit_store(struct device *dev, + struct device_attribute *attr, + const char *buf,drivers/hwmon/sht3x.c line 405
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ size_t count) +{ +int temperature; +u32 humidity; +u8 index; +int ret; +struct sht3x_data *data =dev_get_drvdata (dev); + + ret =kstrtoint (buf,0 , &temperature); +No empty line here please.
drivers/hwmon/sht3x.c
Quoted 2 lines
drivers/hwmon/sht3x.c new file +2
-0+if (ret) +return -EINVAL ;return ret;drivers/hwmon/sht3x.c line 411
Quoted 4 lines
drivers/hwmon/sht3x.c new file +4
-0+ + temperature =clamp_val (temperature,-45000 ,130000 ); + + index =to_sensor_dev_attr (attr)->index;Initialize with declaration ?
drivers/hwmon/sht3x.c line 413
Quoted 2 lines
drivers/hwmon/sht3x.c new file +2
-0+ humidity = data->humidity_limits[index]; +You need to read humidity with the lock active, to avoid inconsistencies if humidity is updated in parallel.
Quoted 7 lines
Maybe
lock()
ret = limit_store(dev, attr, count, temperature,data->humidity_limits[index]); unlock();
limit_store()only uses attr to get the index (again). Maybe just pass index ?
Yes, seems easier to understand
Quoted 179 lines
Quoted 10 lines
drivers/hwmon/sht3x.c new file +8
-0+ ret =limit_store (dev, attr, count, temperature, humidity); + +return ret; +} + +static ssize_t humidity1_limit_store(struct device *dev, + struct device_attribute *attr, + const char *buf,drivers/hwmon/sht3x.c line 431
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ size_t count) +{ +int temperature; +u32 humidity; +u8 index; +int ret; +struct sht3x_data *data =dev_get_drvdata (dev); + + ret =kstrtou32 (buf,0 , &humidity); +No empty line here please.
drivers/hwmon/sht3x.c
Quoted 2 lines
drivers/hwmon/sht3x.c new file +2
-0+if (ret) +return -EINVAL ;return ret;drivers/hwmon/sht3x.c line 436
Quoted 3 lines
drivers/hwmon/sht3x.c new file +3
-0+ + humidity =clamp_val (humidity,0 ,100000 ); + index =to_sensor_dev_attr (attr)->index;Initialize index with declaration ?
Quoted 1 line
+ temperature = data->temperature_limits[index];
Same problem as above with locking.
Quoted 29 lines
drivers/hwmon/sht3x.c new file +29
-0+ + ret =limit_store (dev, attr, count, temperature, humidity); + +return ret; +} + +static void sht3x_select_command (struct sht3x_data *data) +{ +/* +* In blocking mode (clock stretching mode) the I2C bus +* is blocked for other traffic, thus the call to i2c_master_recv() +* will wait until the data is ready. For non blocking mode, we +* have to wait ourselves. +*/ +if (data->mode >0 ) { + data->command = sht3x_cmd_measure_periodic_mode; + data->wait_time =0 ; + }else if (data->setup.blocking_io) { + data->command = data->setup.high_precision ? + sht3x_cmd_measure_blocking_hpm : + sht3x_cmd_measure_blocking_lpm; + data->wait_time =0 ; + }else { +if (data->setup.high_precision) { + data->command = sht3x_cmd_measure_nonblocking_hpm; + data->wait_time =SHT3X_NONBLOCKING_WAIT_TIME_HPM ; + }else { + data->command = sht3x_cmd_measure_nonblocking_lpm; + data->wait_time =SHT3X_NONBLOCKING_WAIT_TIME_LPM ;drivers/hwmon/sht3x.c line 476
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ } + } +} + +static int status_register_read (struct device *dev, +struct device_attribute *attr, +char *buffer,int length) +{ +int ret; +No empty line here please.
Quoted 10 lines
drivers/hwmon/sht3x.c new file +4
-0+struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + + ret = sht3x_read_from_command(client, data,
sht3x_cmd_read_status_reg,drivers/hwmon/sht3x.c new file +5
-0+ buffer, length, 0); + +return ret; +} +drivers/hwmon/sht3x.c line 495
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+static ssize_t temp1_alarm_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int ret; +int length =SHT3X_WORD_LEN +SHT3X_CRC8_LEN ; +char buffer[length]; + + ret =status_register_read (dev, attr, buffer, length); +No empty line here please.
Quoted 6 lines
drivers/hwmon/sht3x.c new file +6
-0+if (ret) +return ret; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , !!(buffer[0 ] &0x04 )); +} +drivers/hwmon/sht3x.c line 511
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+static ssize_t humidity1_alarm_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int ret; +int length =SHT3X_WORD_LEN +SHT3X_CRC8_LEN ; +char buffer[length]; + + ret =status_register_read (dev, attr, buffer, length); +No empty line here please.
Quoted 4 lines
drivers/hwmon/sht3x.c new file +4
-0+if (ret) +return ret; + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , !!(buffer[0 ] &0x08 ));drivers/hwmon/sht3x.c line 525
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+} + +static ssize_t update_interval_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct sht3x_data *data =dev_get_drvdata (dev); + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , + mode_to_update_interval[data->mode]);%uQuoted 6 lines
drivers/hwmon/sht3x.c new file +6
-0+} + +static ssize_t update_interval_store (struct device *dev, +struct device_attribute *attr, +const char *buf, +size_t count)drivers/hwmon/sht3x.c line 541
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+{ +u16 update_interval; +u8 mode; +int ret; +const char *command; +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; + + ret =kstrtou16 (buf,0 , &update_interval); +No empty line here please.
drivers/hwmon/sht3x.c
Quoted 2 lines
drivers/hwmon/sht3x.c new file +2
-0+if (ret) +return -EINVAL ;return ret;drivers/hwmon/sht3x.c line 550
Quoted 7 lines
drivers/hwmon/sht3x.c new file +7
-0+ + mode =get_mode_from_update_interval (update_interval); + +/* mode did not change */ +if (mode == data->mode) +return count; +That doesn't really mean anything here, since the check is outside the lock, and mode may change right after you checked it. Either check from within the lock or not at all.
drivers/hwmon/sht3x.c line 558
Quoted 8 lines
drivers/hwmon/sht3x.c new file +8
-0+mutex_lock (&data->i2c_lock); +mutex_lock (&data->data_lock); +/* abort periodic measure */ + ret =i2c_master_send (client, sht3x_cmd_break,SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) + goto out; + data->mode =0 ; +
Quoted 1 line
Can you explain why you set
data->mode= 0 here ?
To do any changes to the configuration while in periodic mode we have to send a break command to the sensor, which then falls back to single shot mode (mode = 0). Therefore for consistency reasons we set the mode to the value it actually is at this point.
If setting the new mode fails, we are still in single shot mode. Like this we ensure that the data->mode is always correct.
Quoted 15 lines
Quoted 15 lines
drivers/hwmon/sht3x.c new file +14
-0+if (mode >0 ) { +if (data->setup.high_precision) + command = periodic_measure_commands_hpm[mode -1 ]; +else + command = periodic_measure_commands_lpm[mode -1 ]; + +/* select mode */ + ret =i2c_master_send (client, command,SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) + goto out; + } + +/* select mode and command */ + data->mode = mode;
Quoted 8 lines
drivers/hwmon/sht3x.c line 577
Quoted 5 lines
drivers/hwmon/sht3x.c new file +5
-0+sht3x_select_command (data); + +out: +mutex_unlock (&data->data_lock); +mutex_unlock (&data->i2c_lock);Reverse order would be better for lock/unlock since elsewhere you lock
data_lockfirst, followed byi2c_lock.
Ok
Quoted 158 lines
Quoted 158 lines
drivers/hwmon/sht3x.c new file +7
-0+if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -EIO ; + +return count; +} + +static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, temp1_input_show,NULL, 0);
+static SENSOR_DEVICE_ATTR(humidity1_input,S_IRUGO,humidity1_input_show,drivers/hwmon/sht3x.c new file +26
-0+ NULL, 0); +static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, + temp1_limit_show, temp1_limit_store, + limit_max); +static SENSOR_DEVICE_ATTR(humidity1_max, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, + limit_max); +static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, + temp1_limit_show, temp1_limit_store, + limit_max_hyst); +static SENSOR_DEVICE_ATTR(humidity1_max_hyst, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, + limit_max_hyst); +static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, + temp1_limit_show, temp1_limit_store, + limit_min); +static SENSOR_DEVICE_ATTR(humidity1_min, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, + limit_min); +static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO | S_IWUSR, + temp1_limit_show, temp1_limit_store, + limit_min_hyst); +static SENSOR_DEVICE_ATTR(humidity1_min_hyst, S_IRUGO | S_IWUSR, + humidity1_limit_show, humidity1_limit_store, + limit_min_hyst); +static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, temp1_alarm_show,NULL, 0);
+static SENSOR_DEVICE_ATTR(humidity1_alarm,S_IRUGO,humidity1_alarm_show,drivers/hwmon/sht3x.c new file +41
-0+ NULL, 0); +static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, + update_interval_show, update_interval_store, 0); + +static struct attribute *sht3x_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, + &sensor_dev_attr_humidity1_input.dev_attr.attr, + &sensor_dev_attr_temp1_max.dev_attr.attr, + &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, + &sensor_dev_attr_humidity1_max.dev_attr.attr, + &sensor_dev_attr_humidity1_max_hyst.dev_attr.attr, + &sensor_dev_attr_temp1_min.dev_attr.attr, + &sensor_dev_attr_temp1_min_hyst.dev_attr.attr, + &sensor_dev_attr_humidity1_min.dev_attr.attr, + &sensor_dev_attr_humidity1_min_hyst.dev_attr.attr, + &sensor_dev_attr_temp1_alarm.dev_attr.attr, + &sensor_dev_attr_humidity1_alarm.dev_attr.attr, + &sensor_dev_attr_update_interval.dev_attr.attr, +NULL +}; + +static struct attribute *sts3x_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, +NULL +}; + +ATTRIBUTE_GROUPS (sht3x); +ATTRIBUTE_GROUPS (sts3x); + +static int sht3x_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +int ret; +struct sht3x_data *data; +struct device *hwmon_dev; +struct i2c_adapter *adap = client->adapter; +struct device *dev = &client->dev; +const struct attribute_group **attribute_groups; + +/* +* we require full i2c support since the sht3x uses multi-byte read and
drivers/hwmon/sht3x.c new file +24
-0+* writes as well as multi-byte commands which are not supported by +* the smbus protocol +*/ +if (!i2c_check_functionality (adap,I2C_FUNC_I2C )) +return -ENODEV ; + + ret =i2c_master_send (client, sht3x_cmd_clear_status_reg, +SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) +return ret <0 ? ret : -ENODEV ; + + data =devm_kzalloc (dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->setup.blocking_io = false; + data->setup.high_precision = true; + data->mode =0 ; + data->last_update =0 ; + data->client = client; +crc8_populate_msb (sht3x_crc8_table,SHT3X_CRC8_POLYNOMIAL ); + +if (client->dev.platform_data) + data->setup = *(struct sht3x_platform_data*)dev->platform_data;
drivers/hwmon/sht3x.c new file +46
-0+ +sht3x_select_command (data); + +mutex_init (&data->i2c_lock); +mutex_init (&data->data_lock); + + ret =limits_update (data); +if (ret) +return ret; + +if (id->driver_data == sts3x) + attribute_groups = sts3x_groups; +else + attribute_groups = sht3x_groups; + + hwmon_dev =devm_hwmon_device_register_with_groups (dev, + client->name, + data, + attribute_groups); + +if (IS_ERR (hwmon_dev)) +dev_dbg (dev,"unable to register hwmon device\n" ); + +return PTR_ERR_OR_ZERO (hwmon_dev); +} + +/* device ID table */ +static const struct i2c_device_id sht3x_ids[] = { + {"sht3x" , sht3x}, + {"sts3x" , sts3x}, + {} +}; + +MODULE_DEVICE_TABLE (i2c, sht3x_ids); + +static struct i2c_driver sht3x_i2c_driver = { + .driver.name ="sht3x" , + .probe = sht3x_probe, + .id_table = sht3x_ids, +}; + +module_i2c_driver (sht3x_i2c_driver); + +MODULE_AUTHOR ("David Frey <[email protected]>" ); +MODULE_AUTHOR ("Pascal Sachs <[email protected]>" ); +MODULE_DESCRIPTION("Sensirion SHT3x humidity and temperature sensordriver");
drivers/hwmon/sht3x.c new file +1
-0+MODULE_LICENSE ("GPL" );include/linux/platform_data/sht3x.h new file +0
-0
Quoted 8 lines
include/linux/platform_data/sht3x.h line 2
Quoted 6 lines
index 0000000..46af9a0
include/linux/platform_data/sht3x.h new file +2
-0@@ -0,0 +1,25 @@ +/* +* Copyright (C) 2015 Sensirion AG, Switzerland 2016 ?
Yes, those years just change too fast ;-)
Quoted 27 lines
Quoted 26 lines
include/linux/platform_data/sht3x.h new file +23
-0+* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef __SHT3X_H_ +#define __SHT3X_H_ + +struct sht3x_platform_data { +bool blocking_io; +bool high_precision; +}; +#endif /* __SHT3X_H_ */
Guenter Roeck
maintainerQuoted 5 lines · Pascal Sachs
Hi Guenter
I want to thank you again for the fast and detailed code review. I really appreciate it.
Sorry it took so long.
[ ... ]
Quoted 20 lines
Quoted 20 lines
drivers/hwmon/sht3x.c line 225
Quoted 5 lines
drivers/hwmon/sht3x.c new file +5
-0+static struct sht3x_data *sht3x_update_client (struct device *dev) +{ +struct sht3x_data *data =dev_get_drvdata (dev); +struct i2c_client *client = data->client; +u16 interval_ms = mode_to_update_interval[data->mode];If
data->mode== 0,interval_ms== 0,Quoted 1 line
+ unsigned long
interval_jiffies=msecs_to_jiffies(interval_ms);...
interval_jiffies== 1,Quoted 7 lines
drivers/hwmon/sht3x.c new file +7
-0+unsigned char buf[SHT3X_RESPONSE_LENGTH ]; +u16 val; +int ret =0 ; + +mutex_lock (&data->data_lock); +/* only update once per interval in periodic mode */ +if (time_after (jiffies, data->last_update + interval_jiffies)) {
Quoted 7 lines
Quoted 2 lines
... and the command is executed every other timer tick. Is that intentional ?
Yes, this is how it should behave. In single shot mode the sensor measures values on demand, which means every time the sysfs interface is called, a measurement is triggered. In periodic mode however the measurement process is handled internally by the sensor and read out only makes sense if a new reading is available.
Please add the explanation as comment into the code.
[ ... ]
Quoted 53 lines
Quoted 53 lines
Quoted 27 lines
drivers/hwmon/sht3x.c new file +27
-0+ commands = &limit_commands[index]; + +memcpy (position, commands->write_command,SHT3X_CMD_LENGTH ); + position +=SHT3X_CMD_LENGTH ; +/* +* ST = (T + 45) / 175 * 2^16 +* SRH = RH / 100 * 2^16 +* adapted for fixed point arithmetic and packed the same as +* in limit_show() +*/ + raw = ((u32)(temperature +45000 ) *24543 ) >> (16 +7 ); + raw |= ((humidity *42950 ) >>16 ) &0xfe00 ; + + *((__be16 *)position) =cpu_to_be16 (raw); + position +=SHT3X_WORD_LEN ; + *position =crc8 (sht3x_crc8_table, + position -SHT3X_WORD_LEN , +SHT3X_WORD_LEN , +SHT3X_CRC8_INIT ); + +mutex_lock (&data->data_lock); +mutex_lock (&data->i2c_lock); + ret =i2c_master_send (client, buffer,sizeof (buffer)); +mutex_unlock (&data->i2c_lock); + +if (ret !=sizeof (buffer)) + goto out;drivers/hwmon/sht3x.c line 390
Quoted 10 lines
drivers/hwmon/sht3x.c new file +10
-0+ + data->temperature_limits[index] = temperature; + data->humidity_limits[index] = humidity; + +out: +mutex_unlock (&data->data_lock); +if (ret !=sizeof (buffer)) +return ret <0 ? ret : -EIO ; + +return count;I would prefer something like
if (ret != sizeof(buffer)) { if (ret >= 0) ret = -EIO; goto out; } ret = count; data->temperature_limits[index] = temperature; data->humidity_limits[index] = humidity; out: mutex_unlock(&data->data_lock); return ret;
Quoted 3 lines
Quoted 1 line
which I think would be cleaner. See below though for locking issues.
OK, wasn't sure if it is appropriate to handle error and valid return in the same variable
Done all over the place.
[ ... ]
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
drivers/hwmon/sht3x.c new file +8
-0+mutex_lock (&data->i2c_lock); +mutex_lock (&data->data_lock); +/* abort periodic measure */ + ret =i2c_master_send (client, sht3x_cmd_break,SHT3X_CMD_LENGTH ); +if (ret !=SHT3X_CMD_LENGTH ) + goto out; + data->mode =0 ; +
Quoted 8 lines
Quoted 1 line
Can you explain why you set
data->mode= 0 here ?To do any changes to the configuration while in periodic mode we have to send a break command to the sensor, which then falls back to single shot mode (mode = 0). Therefore for consistency reasons we set the mode to the value it actually is at this point.
If setting the new mode fails, we are still in single shot mode. Like this we
ensure that thedata->modeis always correct.
Ok; please add the explanation as comment into the code.
Thanks,
Guenter
PATCH v5 1/1 hwmon: add support for Sensirion SHT3x sensors
In reply to Pascal SachsFrom: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500.
---
Patch was generated against kernel v4.7-rc1
Changelog
=========
v2
* Change humidity type to u32 since it can't be negative
* Use sensor_device_attr instead of device_attr
* mutex protect sht3x_read_from_command
v3
* Wording in documentation
* Remove soft_reset interface
* Rename frequency => update_interval
* Best match algorithm for update_interval selection
* Cache values for limits
* Add alarm channel interface for temp and humidity
* Better mutex protection
* Use an enum to acces limits in the array of limits. This makes sure, the array definition and will always be in sync with the used indices.
v4
* Proper locking mutex
* Using goto for error handling as specified in coding styles
* Easier to understand syntax for read out alarm states from status register
v5
* Fix styling issues, remove unnecessary empty lines
* Improve usage of mutex
* Update copyright year to 2016 in sht3x.h
* Use define for clamping min,max temp and humidity limits
* Only send abort command if in periodic measure mode
Documentation/hwmon/sht3x | 72 ++++ drivers/hwmon/Kconfig | 11 + drivers/hwmon/Makefile | 1 + drivers/hwmon/sht3x.c | 713 ++++++++++++++++++++++++++++++++++++ include/linux/platform_data/sht3x.h | 25 ++ 5 files changed, 822 insertions(+) create mode 100644 Documentation/hwmon/sht3x create mode 100644 drivers/hwmon/sht3x.c create mode 100644 include/linux/platform_data/sht3x.h
Documentation/hwmon/sht3x new file +72 -0
@@ -0,0 +1,72 @@ +Kernel driver sht3x +=================== + +Supported chips: + * Sensirion SHT3x-DIS + Prefix: 'sht3x' + Addresses scanned: none + Datasheet: http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_Datasheet_SHT3x_DIS.pdf + +Author: + David Frey <[email protected]> + Pascal Sachs <[email protected]> + +Description +----------- + +This driver implements support for the Sensirion SHT3x-DIS chip, a humidity +and temperature sensor. Temperature is measured in degrees celsius, relative +humidity is expressed as a percentage. In the sysfs interface, all values are +scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +The device communicates with the I2C protocol. Sensors can have the I2C +addresses 0x44 or 0x45, depending on the wiring. See +Documentation/i2c/instantiating-devices for methods to instantiate the device. + +There are two options configurable by means of sht3x_platform_data: +1. blocking (pull the I2C clock line down while performing the measurement) or + non-blocking mode. Blocking mode will guarantee the fastest result but + the I2C bus will be busy during that time. By default, non-blocking mode + is used. Make sure clock-stretching works properly on your device if you + want to use blocking mode. +2. high or low accuracy. High accuracy is used by default and using it is + strongly recommended. + +The sht3x sensor supports a single shot mode as well as 5 periodic measure +modes, which can be controlled with the update_interval sysfs interface. +The allowed update_interval in milliseconds are as follows: + * 0 single shot mode + * 2000 0.5 Hz periodic measurement + * 1000 1 Hz periodic measurement + * 500 2 Hz periodic measurement + * 250 4 Hz periodic measurement + * 100 10 Hz periodic measurement + +In the periodic measure mode, the sensor automatically triggers a measurement +with the configured update interval on the chip. When a temperature or humidity +reading exceeds the configured limits, the alert attribute is set to 1 and +the alert pin on the sensor is set to high. +When the temperature and humidity readings move back between the hysteresis +values, the alert bit is set to 0 and the alert pin on the sensor is set to +low. + +sysfs-Interface +--------------- + +temp1_input: temperature input +humidity1_input: humidity input +temp1_max: temperature max value +temp1_max_hyst: temperature hysteresis value for max limit +humidity1_max: humidity max value +humidity1_max_hyst: humidity hysteresis value for max limit +temp1_min: temperature min value +temp1_min_hyst: temperature hysteresis value for min limit +humidity1_min: humidity min value +humidity1_min_hyst: humidity hysteresis value for min limit +temp1_alarm: alarm flag is set to 1 if the temperature is outside the + configured limits. Alarm only works in periodic measure mode +humidity1_alarm: alarm flag is set to 1 if the humidity is outside the + configured limits. Alarm only works in periodic measure mode +update_interval: update interval, 0 for single shot, interval in msec + for periodic measurement. If the interval is not supported + by the sensor, the next faster interval is chosen
drivers/hwmon/Kconfig +11 -0
@@ -1265,6 +1265,17 @@ config SENSORS_SHT21 This driver can also be built as a module. If so, the module will be called sht21. +config SENSORS_SHT3x + tristate "Sensiron humidity and temperature sensors. SHT3x and compat." + depends on I2C + select CRC8 + help + If you say yes here you get support for the Sensiron SHT30 and SHT31 + humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht3x. + config SENSORS_SHTC1 tristate "Sensiron humidity and temperature sensors. SHTC1 and compat." depends on I2C
drivers/hwmon/Makefile +1 -0
@@ -138,6 +138,7 @@ obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o +obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.o
drivers/hwmon/sht3x.c new file +713 -0
713 lines not on this page · raw message
include/linux/platform_data/sht3x.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* Copyright (C) 2016 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef __SHT3X_H_ +#define __SHT3X_H_ + +struct sht3x_platform_data { +bool blocking_io; +bool high_precision; +}; +#endif /* __SHT3X_H_ */
Signature · 1 line
1.9.1
PATCH v6 1/1 hwmon: add support for Sensirion SHT3x sensors
In reply to Pascal SachsFrom: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500.
---
Patch was generated against kernel v4.7-rc1
Changelog
=========
v2
* Change humidity type to u32 since it can't be negative
* Use sensor_device_attr instead of device_attr
* mutex protect sht3x_read_from_command
v3
* Wording in documentation
* Remove soft_reset interface
* Rename frequency => update_interval
* Best match algorithm for update_interval selection
* Cache values for limits
* Add alarm channel interface for temp and humidity
* Better mutex protection
* Use an enum to acces limits in the array of limits. This makes sure, the array definition and will always be in sync with the used indices.
v4
* Proper locking mutex
* Using goto for error handling as specified in coding styles
* Easier to understand syntax for read out alarm states from status register
v5
* Fix styling issues, remove unnecessary empty lines
* Improve usage of mutex
* Update copyright year to 2016 in sht3x.h
* Use define for clamping min,max temp and humidity limits
* Only send abort command if in periodic measure mode
v6
* Add comments explaining the behaviour of periodic measure and break commands
Documentation/hwmon/sht3x | 72 ++++ drivers/hwmon/Kconfig | 11 + drivers/hwmon/Makefile | 1 + drivers/hwmon/sht3x.c | 725 ++++++++++++++++++++++++++++++++++++ include/linux/platform_data/sht3x.h | 25 ++ 5 files changed, 834 insertions(+) create mode 100644 Documentation/hwmon/sht3x create mode 100644 drivers/hwmon/sht3x.c create mode 100644 include/linux/platform_data/sht3x.h
Documentation/hwmon/sht3x new file +72 -0
@@ -0,0 +1,72 @@ +Kernel driver sht3x +=================== + +Supported chips: + * Sensirion SHT3x-DIS + Prefix: 'sht3x' + Addresses scanned: none + Datasheet: http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_Datasheet_SHT3x_DIS.pdf + +Author: + David Frey <[email protected]> + Pascal Sachs <[email protected]> + +Description +----------- + +This driver implements support for the Sensirion SHT3x-DIS chip, a humidity +and temperature sensor. Temperature is measured in degrees celsius, relative +humidity is expressed as a percentage. In the sysfs interface, all values are +scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +The device communicates with the I2C protocol. Sensors can have the I2C +addresses 0x44 or 0x45, depending on the wiring. See +Documentation/i2c/instantiating-devices for methods to instantiate the device. + +There are two options configurable by means of sht3x_platform_data: +1. blocking (pull the I2C clock line down while performing the measurement) or + non-blocking mode. Blocking mode will guarantee the fastest result but + the I2C bus will be busy during that time. By default, non-blocking mode + is used. Make sure clock-stretching works properly on your device if you + want to use blocking mode. +2. high or low accuracy. High accuracy is used by default and using it is + strongly recommended. + +The sht3x sensor supports a single shot mode as well as 5 periodic measure +modes, which can be controlled with the update_interval sysfs interface. +The allowed update_interval in milliseconds are as follows: + * 0 single shot mode + * 2000 0.5 Hz periodic measurement + * 1000 1 Hz periodic measurement + * 500 2 Hz periodic measurement + * 250 4 Hz periodic measurement + * 100 10 Hz periodic measurement + +In the periodic measure mode, the sensor automatically triggers a measurement +with the configured update interval on the chip. When a temperature or humidity +reading exceeds the configured limits, the alert attribute is set to 1 and +the alert pin on the sensor is set to high. +When the temperature and humidity readings move back between the hysteresis +values, the alert bit is set to 0 and the alert pin on the sensor is set to +low. + +sysfs-Interface +--------------- + +temp1_input: temperature input +humidity1_input: humidity input +temp1_max: temperature max value +temp1_max_hyst: temperature hysteresis value for max limit +humidity1_max: humidity max value +humidity1_max_hyst: humidity hysteresis value for max limit +temp1_min: temperature min value +temp1_min_hyst: temperature hysteresis value for min limit +humidity1_min: humidity min value +humidity1_min_hyst: humidity hysteresis value for min limit +temp1_alarm: alarm flag is set to 1 if the temperature is outside the + configured limits. Alarm only works in periodic measure mode +humidity1_alarm: alarm flag is set to 1 if the humidity is outside the + configured limits. Alarm only works in periodic measure mode +update_interval: update interval, 0 for single shot, interval in msec + for periodic measurement. If the interval is not supported + by the sensor, the next faster interval is chosen
drivers/hwmon/Kconfig +11 -0
@@ -1265,6 +1265,17 @@ config SENSORS_SHT21 This driver can also be built as a module. If so, the module will be called sht21. +config SENSORS_SHT3x + tristate "Sensiron humidity and temperature sensors. SHT3x and compat." + depends on I2C + select CRC8 + help + If you say yes here you get support for the Sensiron SHT30 and SHT31 + humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht3x. + config SENSORS_SHTC1 tristate "Sensiron humidity and temperature sensors. SHTC1 and compat." depends on I2C
drivers/hwmon/Makefile +1 -0
@@ -138,6 +138,7 @@ obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o +obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.o
drivers/hwmon/sht3x.c new file +725 -0
725 lines not on this page · raw message
include/linux/platform_data/sht3x.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* Copyright (C) 2016 Sensirion AG, Switzerland +* Author: David Frey <[email protected]> +* Author: Pascal Sachs <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef __SHT3X_H_ +#define __SHT3X_H_ + +struct sht3x_platform_data { +bool blocking_io; +bool high_precision; +}; +#endif /* __SHT3X_H_ */
Signature · 1 line
1.9.1
Quoted 9 lines · Pascal Sachs
From: David Frey <[email protected]>
This driver implements support for the Sensirion SHT3x-DIS chip, a humidity and temperature sensor. Temperature is measured in degrees celsius, relative humidity is expressed as a percentage. In the sysfs interface, all values are scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500.
Signed-off-by Pascal Sachs <[email protected]>
Applied to -next.
Thanks,
Guenter