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