Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* Sensirion SHTC1 humidity and temperature sensor driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2014 Sensirion AG, Switzerland
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Author: Johannes Winkelmann <johannes.winkelmann@sensirion.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/platform_data/shtc1.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /* commands (high precision mode) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static const unsigned char shtc1_cmd_measure_blocking_hpm[]    = { 0x7C, 0xA2 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static const unsigned char shtc1_cmd_measure_nonblocking_hpm[] = { 0x78, 0x66 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) /* commands (low precision mode) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static const unsigned char shtc1_cmd_measure_blocking_lpm[]    = { 0x64, 0x58 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static const unsigned char shtc1_cmd_measure_nonblocking_lpm[] = { 0x60, 0x9c };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) /* command for reading the ID register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static const unsigned char shtc1_cmd_read_id_reg[]             = { 0xef, 0xc8 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * constants for reading the ID register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * SHTC1: 0x0007 with mask 0x003f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * SHTW1: 0x0007 with mask 0x003f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * SHTC3: 0x0807 with mask 0x083f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define SHTC3_ID      0x0807
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define SHTC3_ID_MASK 0x083f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define SHTC1_ID      0x0007
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define SHTC1_ID_MASK 0x003f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) /* delays for non-blocking i2c commands, both in us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define SHTC1_NONBLOCKING_WAIT_TIME_HPM  14400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define SHTC1_NONBLOCKING_WAIT_TIME_LPM   1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define SHTC3_NONBLOCKING_WAIT_TIME_HPM  12100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define SHTC3_NONBLOCKING_WAIT_TIME_LPM    800
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define SHTC1_CMD_LENGTH      2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define SHTC1_RESPONSE_LENGTH 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) enum shtcx_chips {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	shtc1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	shtc3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) struct shtc1_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	bool valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	unsigned long last_updated; /* in jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	const unsigned char *command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	unsigned int nonblocking_wait_time; /* in us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	struct shtc1_platform_data setup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	enum shtcx_chips chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	int temperature; /* 1000 * temperature in dgr C */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	int humidity; /* 1000 * relative humidity in %RH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static int shtc1_update_values(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			       struct shtc1_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			       char *buf, int bufsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	int ret = i2c_master_send(client, data->command, SHTC1_CMD_LENGTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (ret != SHTC1_CMD_LENGTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		dev_err(&client->dev, "failed to send command: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return ret < 0 ? ret : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	 * In blocking mode (clock stretching mode) the I2C bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	 * is blocked for other traffic, thus the call to i2c_master_recv()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	 * will wait until the data is ready. For non blocking mode, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	 * have to wait ourselves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (!data->setup.blocking_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		usleep_range(data->nonblocking_wait_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			     data->nonblocking_wait_time + 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	ret = i2c_master_recv(client, buf, bufsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (ret != bufsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		dev_err(&client->dev, "failed to read values: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		return ret < 0 ? ret : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* sysfs attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static struct shtc1_data *shtc1_update_client(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	struct shtc1_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	unsigned char buf[SHTC1_RESPONSE_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		ret = shtc1_update_values(client, data, buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		 * From datasheet:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		 * T = -45 + 175 * ST / 2^16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		 * RH = 100 * SRH / 2^16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		 * Adapted for integer fixed point (3 digit) arithmetic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		val = be16_to_cpup((__be16 *)buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		data->temperature = ((21875 * val) >> 13) - 45000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		val = be16_to_cpup((__be16 *)(buf + 3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		data->humidity = ((12500 * val) >> 13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		data->valid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return ret == 0 ? data : ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static ssize_t temp1_input_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 				struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 				char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	struct shtc1_data *data = shtc1_update_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return sprintf(buf, "%d\n", data->temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static ssize_t humidity1_input_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 				    struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct shtc1_data *data = shtc1_update_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	return sprintf(buf, "%d\n", data->humidity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static DEVICE_ATTR_RO(temp1_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static DEVICE_ATTR_RO(humidity1_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static struct attribute *shtc1_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	&dev_attr_temp1_input.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	&dev_attr_humidity1_input.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ATTRIBUTE_GROUPS(shtc1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static void shtc1_select_command(struct shtc1_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (data->setup.high_precision) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		data->command = data->setup.blocking_io ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 				shtc1_cmd_measure_blocking_hpm :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 				shtc1_cmd_measure_nonblocking_hpm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		data->nonblocking_wait_time = (data->chip == shtc1) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 				SHTC1_NONBLOCKING_WAIT_TIME_HPM :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 				SHTC3_NONBLOCKING_WAIT_TIME_HPM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		data->command = data->setup.blocking_io ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 				shtc1_cmd_measure_blocking_lpm :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 				shtc1_cmd_measure_nonblocking_lpm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		data->nonblocking_wait_time = (data->chip == shtc1) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 				SHTC1_NONBLOCKING_WAIT_TIME_LPM :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 				SHTC3_NONBLOCKING_WAIT_TIME_LPM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static const struct i2c_device_id shtc1_id[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static int shtc1_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	u16 id_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	char id_reg_buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	struct shtc1_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	enum shtcx_chips chip = i2c_match_id(shtc1_id, client)->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	struct i2c_adapter *adap = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (!i2c_check_functionality(adap, I2C_FUNC_I2C)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		dev_err(dev, "plain i2c transactions not supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	ret = i2c_master_send(client, shtc1_cmd_read_id_reg, SHTC1_CMD_LENGTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	if (ret != SHTC1_CMD_LENGTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		dev_err(dev, "could not send read_id_reg command: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		return ret < 0 ? ret : -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	ret = i2c_master_recv(client, id_reg_buf, sizeof(id_reg_buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	if (ret != sizeof(id_reg_buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		dev_err(dev, "could not read ID register: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	id_reg = be16_to_cpup((__be16 *)id_reg_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (chip == shtc3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		if ((id_reg & SHTC3_ID_MASK) != SHTC3_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			dev_err(dev, "SHTC3 ID register does not match\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	} else if ((id_reg & SHTC1_ID_MASK) != SHTC1_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		dev_err(dev, "SHTC1 ID register does not match\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	data->setup.blocking_io = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	data->setup.high_precision = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	data->chip = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		data->setup.blocking_io = of_property_read_bool(np, "sensirion,blocking-io");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		data->setup.high_precision = !of_property_read_bool(np, "sensicon,low-precision");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		if (client->dev.platform_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			data->setup = *(struct shtc1_platform_data *)dev->platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	shtc1_select_command(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 							   client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 							   data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 							   shtc1_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (IS_ERR(hwmon_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		dev_dbg(dev, "unable to register hwmon device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /* device ID table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static const struct i2c_device_id shtc1_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	{ "shtc1", shtc1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	{ "shtw1", shtc1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	{ "shtc3", shtc3 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) MODULE_DEVICE_TABLE(i2c, shtc1_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static const struct of_device_id shtc1_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	{ .compatible = "sensirion,shtc1" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	{ .compatible = "sensirion,shtw1" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	{ .compatible = "sensirion,shtc3" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) MODULE_DEVICE_TABLE(of, shtc1_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static struct i2c_driver shtc1_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		.name = "shtc1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		.of_match_table = shtc1_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	.probe_new    = shtc1_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	.id_table     = shtc1_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) module_i2c_driver(shtc1_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) MODULE_AUTHOR("Johannes Winkelmann <johannes.winkelmann@sensirion.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) MODULE_DESCRIPTION("Sensirion SHTC1 humidity and temperature sensor driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) MODULE_LICENSE("GPL");