xref: /openbmc/linux/drivers/iio/humidity/htu21.c (revision ff1f8558)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * htu21.c - Support for Measurement-Specialties
4  *           htu21 temperature & humidity sensor
5  *	     and humidity part of MS8607 sensor
6  *
7  * Copyright (c) 2014 Measurement-Specialties
8  *
9  * (7-bit I2C slave address 0x40)
10  *
11  * Datasheet:
12  *  http://www.meas-spec.com/downloads/HTU21D.pdf
13  * Datasheet:
14  *  http://www.meas-spec.com/downloads/MS8607-02BA01.pdf
15  */
16 
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/kernel.h>
20 #include <linux/stat.h>
21 #include <linux/module.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 
25 #include "../common/ms_sensors/ms_sensors_i2c.h"
26 
27 #define HTU21_RESET				0xFE
28 
29 enum {
30 	HTU21,
31 	MS8607
32 };
33 
34 static const int htu21_samp_freq[4] = { 20, 40, 70, 120 };
35 /* String copy of the above const for readability purpose */
36 static const char htu21_show_samp_freq[] = "20 40 70 120";
37 
38 static int htu21_read_raw(struct iio_dev *indio_dev,
39 			  struct iio_chan_spec const *channel, int *val,
40 			  int *val2, long mask)
41 {
42 	int ret, temperature;
43 	unsigned int humidity;
44 	struct ms_ht_dev *dev_data = iio_priv(indio_dev);
45 
46 	switch (mask) {
47 	case IIO_CHAN_INFO_PROCESSED:
48 		switch (channel->type) {
49 		case IIO_TEMP:	/* in milli °C */
50 			ret = ms_sensors_ht_read_temperature(dev_data,
51 							     &temperature);
52 			if (ret)
53 				return ret;
54 			*val = temperature;
55 
56 			return IIO_VAL_INT;
57 		case IIO_HUMIDITYRELATIVE:	/* in milli %RH */
58 			ret = ms_sensors_ht_read_humidity(dev_data,
59 							  &humidity);
60 			if (ret)
61 				return ret;
62 			*val = humidity;
63 
64 			return IIO_VAL_INT;
65 		default:
66 			return -EINVAL;
67 		}
68 	case IIO_CHAN_INFO_SAMP_FREQ:
69 		*val = htu21_samp_freq[dev_data->res_index];
70 
71 		return IIO_VAL_INT;
72 	default:
73 		return -EINVAL;
74 	}
75 }
76 
77 static int htu21_write_raw(struct iio_dev *indio_dev,
78 			   struct iio_chan_spec const *chan,
79 			   int val, int val2, long mask)
80 {
81 	struct ms_ht_dev *dev_data = iio_priv(indio_dev);
82 	int i, ret;
83 
84 	switch (mask) {
85 	case IIO_CHAN_INFO_SAMP_FREQ:
86 		i = ARRAY_SIZE(htu21_samp_freq);
87 		while (i-- > 0)
88 			if (val == htu21_samp_freq[i])
89 				break;
90 		if (i < 0)
91 			return -EINVAL;
92 		mutex_lock(&dev_data->lock);
93 		dev_data->res_index = i;
94 		ret = ms_sensors_write_resolution(dev_data, i);
95 		mutex_unlock(&dev_data->lock);
96 
97 		return ret;
98 	default:
99 		return -EINVAL;
100 	}
101 }
102 
103 static const struct iio_chan_spec htu21_channels[] = {
104 	{
105 		.type = IIO_TEMP,
106 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED),
107 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
108 	 },
109 	{
110 		.type = IIO_HUMIDITYRELATIVE,
111 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED),
112 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
113 	 }
114 };
115 
116 /*
117  * Meas Spec recommendation is to not read temperature
118  * on this driver part for MS8607
119  */
120 static const struct iio_chan_spec ms8607_channels[] = {
121 	{
122 		.type = IIO_HUMIDITYRELATIVE,
123 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED),
124 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
125 	 }
126 };
127 
128 static ssize_t htu21_show_battery_low(struct device *dev,
129 				      struct device_attribute *attr, char *buf)
130 {
131 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
132 	struct ms_ht_dev *dev_data = iio_priv(indio_dev);
133 
134 	return ms_sensors_show_battery_low(dev_data, buf);
135 }
136 
137 static ssize_t htu21_show_heater(struct device *dev,
138 				 struct device_attribute *attr, char *buf)
139 {
140 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
141 	struct ms_ht_dev *dev_data = iio_priv(indio_dev);
142 
143 	return ms_sensors_show_heater(dev_data, buf);
144 }
145 
146 static ssize_t htu21_write_heater(struct device *dev,
147 				  struct device_attribute *attr,
148 				  const char *buf, size_t len)
149 {
150 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
151 	struct ms_ht_dev *dev_data = iio_priv(indio_dev);
152 
153 	return ms_sensors_write_heater(dev_data, buf, len);
154 }
155 
156 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(htu21_show_samp_freq);
157 static IIO_DEVICE_ATTR(battery_low, S_IRUGO,
158 		       htu21_show_battery_low, NULL, 0);
159 static IIO_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR,
160 		       htu21_show_heater, htu21_write_heater, 0);
161 
162 static struct attribute *htu21_attributes[] = {
163 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
164 	&iio_dev_attr_battery_low.dev_attr.attr,
165 	&iio_dev_attr_heater_enable.dev_attr.attr,
166 	NULL,
167 };
168 
169 static const struct attribute_group htu21_attribute_group = {
170 	.attrs = htu21_attributes,
171 };
172 
173 static const struct iio_info htu21_info = {
174 	.read_raw = htu21_read_raw,
175 	.write_raw = htu21_write_raw,
176 	.attrs = &htu21_attribute_group,
177 };
178 
179 static int htu21_probe(struct i2c_client *client,
180 		       const struct i2c_device_id *id)
181 {
182 	struct ms_ht_dev *dev_data;
183 	struct iio_dev *indio_dev;
184 	int ret;
185 	u64 serial_number;
186 
187 	if (!i2c_check_functionality(client->adapter,
188 				     I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
189 				     I2C_FUNC_SMBUS_WRITE_BYTE |
190 				     I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
191 		dev_err(&client->dev,
192 			"Adapter does not support some i2c transaction\n");
193 		return -EOPNOTSUPP;
194 	}
195 
196 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data));
197 	if (!indio_dev)
198 		return -ENOMEM;
199 
200 	dev_data = iio_priv(indio_dev);
201 	dev_data->client = client;
202 	dev_data->res_index = 0;
203 	mutex_init(&dev_data->lock);
204 
205 	indio_dev->info = &htu21_info;
206 	indio_dev->name = id->name;
207 	indio_dev->modes = INDIO_DIRECT_MODE;
208 
209 	if (id->driver_data == MS8607) {
210 		indio_dev->channels = ms8607_channels;
211 		indio_dev->num_channels = ARRAY_SIZE(ms8607_channels);
212 	} else {
213 		indio_dev->channels = htu21_channels;
214 		indio_dev->num_channels = ARRAY_SIZE(htu21_channels);
215 	}
216 
217 	i2c_set_clientdata(client, indio_dev);
218 
219 	ret = ms_sensors_reset(client, HTU21_RESET, 15000);
220 	if (ret)
221 		return ret;
222 
223 	ret = ms_sensors_read_serial(client, &serial_number);
224 	if (ret)
225 		return ret;
226 	dev_info(&client->dev, "Serial number : %llx", serial_number);
227 
228 	return devm_iio_device_register(&client->dev, indio_dev);
229 }
230 
231 static const struct i2c_device_id htu21_id[] = {
232 	{"htu21", HTU21},
233 	{"ms8607-humidity", MS8607},
234 	{}
235 };
236 MODULE_DEVICE_TABLE(i2c, htu21_id);
237 
238 static const struct of_device_id htu21_of_match[] = {
239 	{ .compatible = "meas,htu21", },
240 	{ .compatible = "meas,ms8607-humidity", },
241 	{ },
242 };
243 MODULE_DEVICE_TABLE(of, htu21_of_match);
244 
245 static struct i2c_driver htu21_driver = {
246 	.probe = htu21_probe,
247 	.id_table = htu21_id,
248 	.driver = {
249 		   .name = "htu21",
250 		   .of_match_table = of_match_ptr(htu21_of_match),
251 		   },
252 };
253 
254 module_i2c_driver(htu21_driver);
255 
256 MODULE_DESCRIPTION("Measurement-Specialties htu21 temperature and humidity driver");
257 MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
258 MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
259 MODULE_LICENSE("GPL v2");
260