xref: /OK3568_Linux_fs/kernel/drivers/hwmon/hih6130.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* Honeywell HIH-6130/HIH-6131 humidity and temperature sensor driver
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2012 Iain Paton <ipaton0@gmail.com>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * heavily based on the sht21 driver
7*4882a593Smuzhiyun  * Copyright (C) 2010 Urs Fleisch <urs.fleisch@sensirion.com>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Data sheets available (2012-06-22) at
10*4882a593Smuzhiyun  * http://sensing.honeywell.com/index.php?ci_id=3106&la_id=1&defId=44872
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/init.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun #include <linux/i2c.h>
17*4882a593Smuzhiyun #include <linux/hwmon.h>
18*4882a593Smuzhiyun #include <linux/hwmon-sysfs.h>
19*4882a593Smuzhiyun #include <linux/err.h>
20*4882a593Smuzhiyun #include <linux/mutex.h>
21*4882a593Smuzhiyun #include <linux/device.h>
22*4882a593Smuzhiyun #include <linux/delay.h>
23*4882a593Smuzhiyun #include <linux/jiffies.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /**
26*4882a593Smuzhiyun  * struct hih6130 - HIH-6130 device specific data
27*4882a593Smuzhiyun  * @client: pointer to I2C client device
28*4882a593Smuzhiyun  * @lock: mutex to protect measurement values
29*4882a593Smuzhiyun  * @valid: only false before first measurement is taken
30*4882a593Smuzhiyun  * @last_update: time of last update (jiffies)
31*4882a593Smuzhiyun  * @temperature: cached temperature measurement value
32*4882a593Smuzhiyun  * @humidity: cached humidity measurement value
33*4882a593Smuzhiyun  * @write_length: length for I2C measurement request
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun struct hih6130 {
36*4882a593Smuzhiyun 	struct i2c_client *client;
37*4882a593Smuzhiyun 	struct mutex lock;
38*4882a593Smuzhiyun 	bool valid;
39*4882a593Smuzhiyun 	unsigned long last_update;
40*4882a593Smuzhiyun 	int temperature;
41*4882a593Smuzhiyun 	int humidity;
42*4882a593Smuzhiyun 	size_t write_length;
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /**
46*4882a593Smuzhiyun  * hih6130_temp_ticks_to_millicelsius() - convert raw temperature ticks to
47*4882a593Smuzhiyun  * milli celsius
48*4882a593Smuzhiyun  * @ticks: temperature ticks value received from sensor
49*4882a593Smuzhiyun  */
hih6130_temp_ticks_to_millicelsius(int ticks)50*4882a593Smuzhiyun static inline int hih6130_temp_ticks_to_millicelsius(int ticks)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	ticks = ticks >> 2;
53*4882a593Smuzhiyun 	/*
54*4882a593Smuzhiyun 	 * from data sheet section 5.0
55*4882a593Smuzhiyun 	 * Formula T = ( ticks / ( 2^14 - 2 ) ) * 165 -40
56*4882a593Smuzhiyun 	 */
57*4882a593Smuzhiyun 	return (DIV_ROUND_CLOSEST(ticks * 1650, 16382) - 400) * 100;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun /**
61*4882a593Smuzhiyun  * hih6130_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
62*4882a593Smuzhiyun  * one-thousandths of a percent relative humidity
63*4882a593Smuzhiyun  * @ticks: humidity ticks value received from sensor
64*4882a593Smuzhiyun  */
hih6130_rh_ticks_to_per_cent_mille(int ticks)65*4882a593Smuzhiyun static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	ticks &= ~0xC000; /* clear status bits */
68*4882a593Smuzhiyun 	/*
69*4882a593Smuzhiyun 	 * from data sheet section 4.0
70*4882a593Smuzhiyun 	 * Formula RH = ( ticks / ( 2^14 -2 ) ) * 100
71*4882a593Smuzhiyun 	 */
72*4882a593Smuzhiyun 	return DIV_ROUND_CLOSEST(ticks * 1000, 16382) * 100;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun /**
76*4882a593Smuzhiyun  * hih6130_update_measurements() - get updated measurements from device
77*4882a593Smuzhiyun  * @dev: device
78*4882a593Smuzhiyun  *
79*4882a593Smuzhiyun  * Returns 0 on success, else negative errno.
80*4882a593Smuzhiyun  */
hih6130_update_measurements(struct device * dev)81*4882a593Smuzhiyun static int hih6130_update_measurements(struct device *dev)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	struct hih6130 *hih6130 = dev_get_drvdata(dev);
84*4882a593Smuzhiyun 	struct i2c_client *client = hih6130->client;
85*4882a593Smuzhiyun 	int ret = 0;
86*4882a593Smuzhiyun 	int t;
87*4882a593Smuzhiyun 	unsigned char tmp[4];
88*4882a593Smuzhiyun 	struct i2c_msg msgs[1] = {
89*4882a593Smuzhiyun 		{
90*4882a593Smuzhiyun 			.addr = client->addr,
91*4882a593Smuzhiyun 			.flags = I2C_M_RD,
92*4882a593Smuzhiyun 			.len = 4,
93*4882a593Smuzhiyun 			.buf = tmp,
94*4882a593Smuzhiyun 		}
95*4882a593Smuzhiyun 	};
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	mutex_lock(&hih6130->lock);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	/*
100*4882a593Smuzhiyun 	 * While the measurement can be completed in ~40ms the sensor takes
101*4882a593Smuzhiyun 	 * much longer to react to a change in external conditions. How quickly
102*4882a593Smuzhiyun 	 * it reacts depends on airflow and other factors outwith our control.
103*4882a593Smuzhiyun 	 * The datasheet specifies maximum 'Response time' for humidity at 8s
104*4882a593Smuzhiyun 	 * and temperature at 30s under specified conditions.
105*4882a593Smuzhiyun 	 * We therefore choose to only read the sensor at most once per second.
106*4882a593Smuzhiyun 	 * This trades off pointless activity polling the sensor much faster
107*4882a593Smuzhiyun 	 * than it can react against better response times in conditions more
108*4882a593Smuzhiyun 	 * favourable than specified in the datasheet.
109*4882a593Smuzhiyun 	 */
110*4882a593Smuzhiyun 	if (time_after(jiffies, hih6130->last_update + HZ) || !hih6130->valid) {
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 		/*
113*4882a593Smuzhiyun 		 * Write to slave address to request a measurement.
114*4882a593Smuzhiyun 		 * According with the datasheet it should be with no data, but
115*4882a593Smuzhiyun 		 * for systems with I2C bus drivers that do not allow zero
116*4882a593Smuzhiyun 		 * length packets we write one dummy byte to allow sensor
117*4882a593Smuzhiyun 		 * measurements on them.
118*4882a593Smuzhiyun 		 */
119*4882a593Smuzhiyun 		tmp[0] = 0;
120*4882a593Smuzhiyun 		ret = i2c_master_send(client, tmp, hih6130->write_length);
121*4882a593Smuzhiyun 		if (ret < 0)
122*4882a593Smuzhiyun 			goto out;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 		/* measurement cycle time is ~36.65msec */
125*4882a593Smuzhiyun 		msleep(40);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 		ret = i2c_transfer(client->adapter, msgs, 1);
128*4882a593Smuzhiyun 		if (ret < 0)
129*4882a593Smuzhiyun 			goto out;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 		if ((tmp[0] & 0xC0) != 0) {
132*4882a593Smuzhiyun 			dev_err(&client->dev, "Error while reading measurement result\n");
133*4882a593Smuzhiyun 			ret = -EIO;
134*4882a593Smuzhiyun 			goto out;
135*4882a593Smuzhiyun 		}
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 		t = (tmp[0] << 8) + tmp[1];
138*4882a593Smuzhiyun 		hih6130->humidity = hih6130_rh_ticks_to_per_cent_mille(t);
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 		t = (tmp[2] << 8) + tmp[3];
141*4882a593Smuzhiyun 		hih6130->temperature = hih6130_temp_ticks_to_millicelsius(t);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 		hih6130->last_update = jiffies;
144*4882a593Smuzhiyun 		hih6130->valid = true;
145*4882a593Smuzhiyun 	}
146*4882a593Smuzhiyun out:
147*4882a593Smuzhiyun 	mutex_unlock(&hih6130->lock);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	return ret >= 0 ? 0 : ret;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun /**
153*4882a593Smuzhiyun  * hih6130_show_temperature() - show temperature measurement value in sysfs
154*4882a593Smuzhiyun  * @dev: device
155*4882a593Smuzhiyun  * @attr: device attribute
156*4882a593Smuzhiyun  * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
157*4882a593Smuzhiyun  *
158*4882a593Smuzhiyun  * Will be called on read access to temp1_input sysfs attribute.
159*4882a593Smuzhiyun  * Returns number of bytes written into buffer, negative errno on error.
160*4882a593Smuzhiyun  */
hih6130_temperature_show(struct device * dev,struct device_attribute * attr,char * buf)161*4882a593Smuzhiyun static ssize_t hih6130_temperature_show(struct device *dev,
162*4882a593Smuzhiyun 					struct device_attribute *attr,
163*4882a593Smuzhiyun 					char *buf)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun 	struct hih6130 *hih6130 = dev_get_drvdata(dev);
166*4882a593Smuzhiyun 	int ret;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	ret = hih6130_update_measurements(dev);
169*4882a593Smuzhiyun 	if (ret < 0)
170*4882a593Smuzhiyun 		return ret;
171*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", hih6130->temperature);
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun /**
175*4882a593Smuzhiyun  * hih6130_show_humidity() - show humidity measurement value in sysfs
176*4882a593Smuzhiyun  * @dev: device
177*4882a593Smuzhiyun  * @attr: device attribute
178*4882a593Smuzhiyun  * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
179*4882a593Smuzhiyun  *
180*4882a593Smuzhiyun  * Will be called on read access to humidity1_input sysfs attribute.
181*4882a593Smuzhiyun  * Returns number of bytes written into buffer, negative errno on error.
182*4882a593Smuzhiyun  */
hih6130_humidity_show(struct device * dev,struct device_attribute * attr,char * buf)183*4882a593Smuzhiyun static ssize_t hih6130_humidity_show(struct device *dev,
184*4882a593Smuzhiyun 				     struct device_attribute *attr, char *buf)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	struct hih6130 *hih6130 = dev_get_drvdata(dev);
187*4882a593Smuzhiyun 	int ret;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	ret = hih6130_update_measurements(dev);
190*4882a593Smuzhiyun 	if (ret < 0)
191*4882a593Smuzhiyun 		return ret;
192*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", hih6130->humidity);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun /* sysfs attributes */
196*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp1_input, hih6130_temperature, 0);
197*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(humidity1_input, hih6130_humidity, 0);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun static struct attribute *hih6130_attrs[] = {
200*4882a593Smuzhiyun 	&sensor_dev_attr_temp1_input.dev_attr.attr,
201*4882a593Smuzhiyun 	&sensor_dev_attr_humidity1_input.dev_attr.attr,
202*4882a593Smuzhiyun 	NULL
203*4882a593Smuzhiyun };
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun ATTRIBUTE_GROUPS(hih6130);
206*4882a593Smuzhiyun 
hih6130_probe(struct i2c_client * client)207*4882a593Smuzhiyun static int hih6130_probe(struct i2c_client *client)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun 	struct device *dev = &client->dev;
210*4882a593Smuzhiyun 	struct hih6130 *hih6130;
211*4882a593Smuzhiyun 	struct device *hwmon_dev;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
214*4882a593Smuzhiyun 		dev_err(&client->dev, "adapter does not support true I2C\n");
215*4882a593Smuzhiyun 		return -ENODEV;
216*4882a593Smuzhiyun 	}
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	hih6130 = devm_kzalloc(dev, sizeof(*hih6130), GFP_KERNEL);
219*4882a593Smuzhiyun 	if (!hih6130)
220*4882a593Smuzhiyun 		return -ENOMEM;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	hih6130->client = client;
223*4882a593Smuzhiyun 	mutex_init(&hih6130->lock);
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK))
226*4882a593Smuzhiyun 		hih6130->write_length = 1;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
229*4882a593Smuzhiyun 							   hih6130,
230*4882a593Smuzhiyun 							   hih6130_groups);
231*4882a593Smuzhiyun 	return PTR_ERR_OR_ZERO(hwmon_dev);
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun /* Device ID table */
235*4882a593Smuzhiyun static const struct i2c_device_id hih6130_id[] = {
236*4882a593Smuzhiyun 	{ "hih6130", 0 },
237*4882a593Smuzhiyun 	{ }
238*4882a593Smuzhiyun };
239*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, hih6130_id);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun static const struct of_device_id __maybe_unused hih6130_of_match[] = {
242*4882a593Smuzhiyun 	{ .compatible = "honeywell,hih6130", },
243*4882a593Smuzhiyun 	{ }
244*4882a593Smuzhiyun };
245*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, hih6130_of_match);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun static struct i2c_driver hih6130_driver = {
248*4882a593Smuzhiyun 	.driver = {
249*4882a593Smuzhiyun 		.name = "hih6130",
250*4882a593Smuzhiyun 		.of_match_table = of_match_ptr(hih6130_of_match),
251*4882a593Smuzhiyun 	},
252*4882a593Smuzhiyun 	.probe_new   = hih6130_probe,
253*4882a593Smuzhiyun 	.id_table    = hih6130_id,
254*4882a593Smuzhiyun };
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun module_i2c_driver(hih6130_driver);
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
259*4882a593Smuzhiyun MODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver");
260*4882a593Smuzhiyun MODULE_LICENSE("GPL");
261