GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / iio / humidity / am2315.c
1 /**
2  * Aosong AM2315 relative humidity and temperature
3  *
4  * Copyright (c) 2016, Intel Corporation.
5  *
6  * This file is subject to the terms and conditions of version 2 of
7  * the GNU General Public License. See the file COPYING in the main
8  * directory of this archive for more details.
9  *
10  * 7-bit I2C address: 0x5C.
11  */
12
13 #include <linux/acpi.h>
14 #include <linux/delay.h>
15 #include <linux/i2c.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/triggered_buffer.h>
23
24 #define AM2315_REG_HUM_MSB                      0x00
25 #define AM2315_REG_HUM_LSB                      0x01
26 #define AM2315_REG_TEMP_MSB                     0x02
27 #define AM2315_REG_TEMP_LSB                     0x03
28
29 #define AM2315_FUNCTION_READ                    0x03
30 #define AM2315_HUM_OFFSET                       2
31 #define AM2315_TEMP_OFFSET                      4
32 #define AM2315_ALL_CHANNEL_MASK                 GENMASK(1, 0)
33
34 #define AM2315_DRIVER_NAME                      "am2315"
35
36 struct am2315_data {
37         struct i2c_client *client;
38         struct mutex lock;
39         /* Ensure timestamp is naturally aligned */
40         struct {
41                 s16 chans[2];
42                 s64 timestamp __aligned(8);
43         } scan;
44 };
45
46 struct am2315_sensor_data {
47         s16 hum_data;
48         s16 temp_data;
49 };
50
51 static const struct iio_chan_spec am2315_channels[] = {
52         {
53                 .type = IIO_HUMIDITYRELATIVE,
54                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
55                                       BIT(IIO_CHAN_INFO_SCALE),
56                 .scan_index = 0,
57                 .scan_type = {
58                         .sign = 's',
59                         .realbits = 16,
60                         .storagebits = 16,
61                         .endianness = IIO_CPU,
62                 },
63         },
64         {
65                 .type = IIO_TEMP,
66                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
67                                       BIT(IIO_CHAN_INFO_SCALE),
68                 .scan_index = 1,
69                 .scan_type = {
70                         .sign = 's',
71                         .realbits = 16,
72                         .storagebits = 16,
73                         .endianness = IIO_CPU,
74                 },
75         },
76         IIO_CHAN_SOFT_TIMESTAMP(2),
77 };
78
79 /* CRC calculation algorithm, as specified in the datasheet (page 13). */
80 static u16 am2315_crc(u8 *data, u8 nr_bytes)
81 {
82         int i;
83         u16 crc = 0xffff;
84
85         while (nr_bytes--) {
86                 crc ^= *data++;
87                 for (i = 0; i < 8; i++) {
88                         if (crc & 0x01) {
89                                 crc >>= 1;
90                                 crc ^= 0xA001;
91                         } else {
92                                 crc >>= 1;
93                         }
94                 }
95         }
96
97         return crc;
98 }
99
100 /* Simple function that sends a few bytes to the device to wake it up. */
101 static void am2315_ping(struct i2c_client *client)
102 {
103         i2c_smbus_read_byte_data(client, AM2315_REG_HUM_MSB);
104 }
105
106 static int am2315_read_data(struct am2315_data *data,
107                             struct am2315_sensor_data *sensor_data)
108 {
109         int ret;
110         /* tx_buf format: <function code> <start addr> <nr of regs to read> */
111         u8 tx_buf[3] = { AM2315_FUNCTION_READ, AM2315_REG_HUM_MSB, 4 };
112         /*
113          * rx_buf format:
114          * <function code> <number of registers read>
115          * <humidity MSB> <humidity LSB> <temp MSB> <temp LSB>
116          * <CRC LSB> <CRC MSB>
117          */
118         u8 rx_buf[8];
119         u16 crc;
120
121         /* First wake up the device. */
122         am2315_ping(data->client);
123
124         mutex_lock(&data->lock);
125         ret = i2c_master_send(data->client, tx_buf, sizeof(tx_buf));
126         if (ret < 0) {
127                 dev_err(&data->client->dev, "failed to send read request\n");
128                 goto exit_unlock;
129         }
130         /* Wait 2-3 ms, then read back the data sent by the device. */
131         usleep_range(2000, 3000);
132         /* Do a bulk data read, then pick out what we need. */
133         ret = i2c_master_recv(data->client, rx_buf, sizeof(rx_buf));
134         if (ret < 0) {
135                 dev_err(&data->client->dev, "failed to read sensor data\n");
136                 goto exit_unlock;
137         }
138         mutex_unlock(&data->lock);
139         /*
140          * Do a CRC check on the data and compare it to the value
141          * calculated by the device.
142          */
143         crc = am2315_crc(rx_buf, sizeof(rx_buf) - 2);
144         if ((crc & 0xff) != rx_buf[6] || (crc >> 8) != rx_buf[7]) {
145                 dev_err(&data->client->dev, "failed to verify sensor data\n");
146                 return -EIO;
147         }
148
149         sensor_data->hum_data = (rx_buf[AM2315_HUM_OFFSET] << 8) |
150                                  rx_buf[AM2315_HUM_OFFSET + 1];
151         sensor_data->temp_data = (rx_buf[AM2315_TEMP_OFFSET] << 8) |
152                                   rx_buf[AM2315_TEMP_OFFSET + 1];
153
154         return ret;
155
156 exit_unlock:
157         mutex_unlock(&data->lock);
158         return ret;
159 }
160
161 static irqreturn_t am2315_trigger_handler(int irq, void *p)
162 {
163         int i;
164         int ret;
165         int bit;
166         struct iio_poll_func *pf = p;
167         struct iio_dev *indio_dev = pf->indio_dev;
168         struct am2315_data *data = iio_priv(indio_dev);
169         struct am2315_sensor_data sensor_data;
170
171         ret = am2315_read_data(data, &sensor_data);
172         if (ret < 0)
173                 goto err;
174
175         mutex_lock(&data->lock);
176         if (*(indio_dev->active_scan_mask) == AM2315_ALL_CHANNEL_MASK) {
177                 data->scan.chans[0] = sensor_data.hum_data;
178                 data->scan.chans[1] = sensor_data.temp_data;
179         } else {
180                 i = 0;
181                 for_each_set_bit(bit, indio_dev->active_scan_mask,
182                                  indio_dev->masklength) {
183                         data->scan.chans[i] = (bit ? sensor_data.temp_data :
184                                                sensor_data.hum_data);
185                         i++;
186                 }
187         }
188         mutex_unlock(&data->lock);
189
190         iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
191                                            pf->timestamp);
192 err:
193         iio_trigger_notify_done(indio_dev->trig);
194         return IRQ_HANDLED;
195 }
196
197 static int am2315_read_raw(struct iio_dev *indio_dev,
198                            struct iio_chan_spec const *chan,
199                            int *val, int *val2, long mask)
200 {
201         int ret;
202         struct am2315_sensor_data sensor_data;
203         struct am2315_data *data = iio_priv(indio_dev);
204
205         switch (mask) {
206         case IIO_CHAN_INFO_RAW:
207                 ret = am2315_read_data(data, &sensor_data);
208                 if (ret < 0)
209                         return ret;
210                 *val = (chan->type == IIO_HUMIDITYRELATIVE) ?
211                                 sensor_data.hum_data : sensor_data.temp_data;
212                 return IIO_VAL_INT;
213         case IIO_CHAN_INFO_SCALE:
214                 *val = 100;
215                 return IIO_VAL_INT;
216         }
217
218         return -EINVAL;
219 }
220
221 static const struct iio_info am2315_info = {
222         .driver_module          = THIS_MODULE,
223         .read_raw               = am2315_read_raw,
224 };
225
226 static int am2315_probe(struct i2c_client *client,
227                         const struct i2c_device_id *id)
228 {
229         int ret;
230         struct iio_dev *indio_dev;
231         struct am2315_data *data;
232
233         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
234         if (!indio_dev) {
235                 dev_err(&client->dev, "iio allocation failed!\n");
236                 return -ENOMEM;
237         }
238
239         data = iio_priv(indio_dev);
240         data->client = client;
241         i2c_set_clientdata(client, indio_dev);
242         mutex_init(&data->lock);
243
244         indio_dev->dev.parent = &client->dev;
245         indio_dev->info = &am2315_info;
246         indio_dev->name = AM2315_DRIVER_NAME;
247         indio_dev->modes = INDIO_DIRECT_MODE;
248         indio_dev->channels = am2315_channels;
249         indio_dev->num_channels = ARRAY_SIZE(am2315_channels);
250
251         ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
252                                          am2315_trigger_handler, NULL);
253         if (ret < 0) {
254                 dev_err(&client->dev, "iio triggered buffer setup failed\n");
255                 return ret;
256         }
257
258         ret = iio_device_register(indio_dev);
259         if (ret < 0)
260                 goto err_buffer_cleanup;
261
262         return 0;
263
264 err_buffer_cleanup:
265         iio_triggered_buffer_cleanup(indio_dev);
266         return ret;
267 }
268
269 static int am2315_remove(struct i2c_client *client)
270 {
271         struct iio_dev *indio_dev = i2c_get_clientdata(client);
272
273         iio_device_unregister(indio_dev);
274         iio_triggered_buffer_cleanup(indio_dev);
275
276         return 0;
277 }
278
279 static const struct i2c_device_id am2315_i2c_id[] = {
280         {"am2315", 0},
281         {}
282 };
283 MODULE_DEVICE_TABLE(i2c, am2315_i2c_id);
284
285 static const struct acpi_device_id am2315_acpi_id[] = {
286         {"AOS2315", 0},
287         {}
288 };
289
290 MODULE_DEVICE_TABLE(acpi, am2315_acpi_id);
291
292 static struct i2c_driver am2315_driver = {
293         .driver = {
294                 .name = "am2315",
295                 .acpi_match_table = ACPI_PTR(am2315_acpi_id),
296         },
297         .probe =            am2315_probe,
298         .remove =           am2315_remove,
299         .id_table =         am2315_i2c_id,
300 };
301
302 module_i2c_driver(am2315_driver);
303
304 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
305 MODULE_DESCRIPTION("Aosong AM2315 relative humidity and temperature");
306 MODULE_LICENSE("GPL v2");