2 * htu21.c - Support for Measurement-Specialties
3 * htu21 temperature & humidity sensor
5 * Copyright (c) 2014 Measurement-Specialties
7 * Licensed under the GPL-2.
9 * (7-bit I2C slave address 0x40)
12 * http://www.meas-spec.com/downloads/HTU21D.pdf
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>
23 #include "../common/ms_sensors/ms_sensors_i2c.h"
25 #define HTU21_RESET 0xFE
27 static const int htu21_samp_freq
[4] = { 20, 40, 70, 120 };
28 /* String copy of the above const for readability purpose */
29 static const char htu21_show_samp_freq
[] = "20 40 70 120";
31 static int htu21_read_raw(struct iio_dev
*indio_dev
,
32 struct iio_chan_spec
const *channel
, int *val
,
36 unsigned int humidity
;
37 struct ms_ht_dev
*dev_data
= iio_priv(indio_dev
);
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
,
50 case IIO_HUMIDITYRELATIVE
: /* in milli %RH */
51 ret
= ms_sensors_ht_read_humidity(dev_data
,
61 case IIO_CHAN_INFO_SAMP_FREQ
:
62 *val
= htu21_samp_freq
[dev_data
->res_index
];
70 static int htu21_write_raw(struct iio_dev
*indio_dev
,
71 struct iio_chan_spec
const *chan
,
72 int val
, int val2
, long mask
)
74 struct ms_ht_dev
*dev_data
= iio_priv(indio_dev
);
78 case IIO_CHAN_INFO_SAMP_FREQ
:
79 i
= ARRAY_SIZE(htu21_samp_freq
);
81 if (val
== htu21_samp_freq
[i
])
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
);
96 static const struct iio_chan_spec htu21_channels
[] = {
99 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_PROCESSED
),
100 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
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
),
109 static ssize_t
htu21_show_battery_low(struct device
*dev
,
110 struct device_attribute
*attr
, char *buf
)
112 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
113 struct ms_ht_dev
*dev_data
= iio_priv(indio_dev
);
115 return ms_sensors_show_battery_low(dev_data
, buf
);
118 static ssize_t
htu21_show_heater(struct device
*dev
,
119 struct device_attribute
*attr
, char *buf
)
121 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
122 struct ms_ht_dev
*dev_data
= iio_priv(indio_dev
);
124 return ms_sensors_show_heater(dev_data
, buf
);
127 static ssize_t
htu21_write_heater(struct device
*dev
,
128 struct device_attribute
*attr
,
129 const char *buf
, size_t len
)
131 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
132 struct ms_ht_dev
*dev_data
= iio_priv(indio_dev
);
134 return ms_sensors_write_heater(dev_data
, buf
, len
);
137 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(htu21_show_samp_freq
);
138 static IIO_DEVICE_ATTR(battery_low
, S_IRUGO
,
139 htu21_show_battery_low
, NULL
, 0);
140 static IIO_DEVICE_ATTR(heater_enable
, S_IRUGO
| S_IWUSR
,
141 htu21_show_heater
, htu21_write_heater
, 0);
143 static 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
,
150 static const struct attribute_group htu21_attribute_group
= {
151 .attrs
= htu21_attributes
,
154 static 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
,
161 static int htu21_probe(struct i2c_client
*client
,
162 const struct i2c_device_id
*id
)
164 struct ms_ht_dev
*dev_data
;
165 struct iio_dev
*indio_dev
;
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");
178 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*dev_data
));
182 dev_data
= iio_priv(indio_dev
);
183 dev_data
->client
= client
;
184 dev_data
->res_index
= 0;
185 mutex_init(&dev_data
->lock
);
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
);
194 i2c_set_clientdata(client
, indio_dev
);
196 ret
= ms_sensors_reset(client
, HTU21_RESET
, 15000);
200 ret
= ms_sensors_read_serial(client
, &serial_number
);
203 dev_info(&client
->dev
, "Serial number : %llx", serial_number
);
205 return devm_iio_device_register(&client
->dev
, indio_dev
);
208 static const struct i2c_device_id htu21_id
[] = {
213 static struct i2c_driver htu21_driver
= {
214 .probe
= htu21_probe
,
215 .id_table
= htu21_id
,
221 module_i2c_driver(htu21_driver
);
223 MODULE_DESCRIPTION("Measurement-Specialties htu21 temperature and humidity driver");
224 MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
225 MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
226 MODULE_LICENSE("GPL v2");