136edc939SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2b9d453a5SGwendal Grignou /* 3366a3270STiberiu Breana * Aosong AM2315 relative humidity and temperature 4366a3270STiberiu Breana * 5366a3270STiberiu Breana * Copyright (c) 2016, Intel Corporation. 6366a3270STiberiu Breana * 7366a3270STiberiu Breana * 7-bit I2C address: 0x5C. 8366a3270STiberiu Breana */ 9366a3270STiberiu Breana 10366a3270STiberiu Breana #include <linux/delay.h> 11366a3270STiberiu Breana #include <linux/i2c.h> 12366a3270STiberiu Breana #include <linux/kernel.h> 13366a3270STiberiu Breana #include <linux/module.h> 140d96d5eaSTiberiu Breana #include <linux/iio/buffer.h> 15366a3270STiberiu Breana #include <linux/iio/iio.h> 16366a3270STiberiu Breana #include <linux/iio/sysfs.h> 170d96d5eaSTiberiu Breana #include <linux/iio/trigger_consumer.h> 180d96d5eaSTiberiu Breana #include <linux/iio/triggered_buffer.h> 19366a3270STiberiu Breana 20366a3270STiberiu Breana #define AM2315_REG_HUM_MSB 0x00 21366a3270STiberiu Breana #define AM2315_REG_HUM_LSB 0x01 22366a3270STiberiu Breana #define AM2315_REG_TEMP_MSB 0x02 23366a3270STiberiu Breana #define AM2315_REG_TEMP_LSB 0x03 24366a3270STiberiu Breana 25366a3270STiberiu Breana #define AM2315_FUNCTION_READ 0x03 26366a3270STiberiu Breana #define AM2315_HUM_OFFSET 2 27366a3270STiberiu Breana #define AM2315_TEMP_OFFSET 4 280d96d5eaSTiberiu Breana #define AM2315_ALL_CHANNEL_MASK GENMASK(1, 0) 29366a3270STiberiu Breana 30366a3270STiberiu Breana #define AM2315_DRIVER_NAME "am2315" 31366a3270STiberiu Breana 32366a3270STiberiu Breana struct am2315_data { 33366a3270STiberiu Breana struct i2c_client *client; 34366a3270STiberiu Breana struct mutex lock; 35f4ca2e25SJonathan Cameron /* Ensure timestamp is naturally aligned */ 36f4ca2e25SJonathan Cameron struct { 37f4ca2e25SJonathan Cameron s16 chans[2]; 38f4ca2e25SJonathan Cameron s64 timestamp __aligned(8); 39f4ca2e25SJonathan Cameron } scan; 40366a3270STiberiu Breana }; 41366a3270STiberiu Breana 42366a3270STiberiu Breana struct am2315_sensor_data { 43366a3270STiberiu Breana s16 hum_data; 44366a3270STiberiu Breana s16 temp_data; 45366a3270STiberiu Breana }; 46366a3270STiberiu Breana 47366a3270STiberiu Breana static const struct iio_chan_spec am2315_channels[] = { 48366a3270STiberiu Breana { 49366a3270STiberiu Breana .type = IIO_HUMIDITYRELATIVE, 50366a3270STiberiu Breana .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 510d96d5eaSTiberiu Breana BIT(IIO_CHAN_INFO_SCALE), 520d96d5eaSTiberiu Breana .scan_index = 0, 530d96d5eaSTiberiu Breana .scan_type = { 540d96d5eaSTiberiu Breana .sign = 's', 550d96d5eaSTiberiu Breana .realbits = 16, 560d96d5eaSTiberiu Breana .storagebits = 16, 570d96d5eaSTiberiu Breana .endianness = IIO_CPU, 580d96d5eaSTiberiu Breana }, 59366a3270STiberiu Breana }, 60366a3270STiberiu Breana { 61366a3270STiberiu Breana .type = IIO_TEMP, 62366a3270STiberiu Breana .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 630d96d5eaSTiberiu Breana BIT(IIO_CHAN_INFO_SCALE), 640d96d5eaSTiberiu Breana .scan_index = 1, 650d96d5eaSTiberiu Breana .scan_type = { 660d96d5eaSTiberiu Breana .sign = 's', 670d96d5eaSTiberiu Breana .realbits = 16, 680d96d5eaSTiberiu Breana .storagebits = 16, 690d96d5eaSTiberiu Breana .endianness = IIO_CPU, 70366a3270STiberiu Breana }, 710d96d5eaSTiberiu Breana }, 720d96d5eaSTiberiu Breana IIO_CHAN_SOFT_TIMESTAMP(2), 73366a3270STiberiu Breana }; 74366a3270STiberiu Breana 75366a3270STiberiu Breana /* CRC calculation algorithm, as specified in the datasheet (page 13). */ 76366a3270STiberiu Breana static u16 am2315_crc(u8 *data, u8 nr_bytes) 77366a3270STiberiu Breana { 78366a3270STiberiu Breana int i; 79366a3270STiberiu Breana u16 crc = 0xffff; 80366a3270STiberiu Breana 81366a3270STiberiu Breana while (nr_bytes--) { 82366a3270STiberiu Breana crc ^= *data++; 83366a3270STiberiu Breana for (i = 0; i < 8; i++) { 84366a3270STiberiu Breana if (crc & 0x01) { 85366a3270STiberiu Breana crc >>= 1; 86366a3270STiberiu Breana crc ^= 0xA001; 87366a3270STiberiu Breana } else { 88366a3270STiberiu Breana crc >>= 1; 89366a3270STiberiu Breana } 90366a3270STiberiu Breana } 91366a3270STiberiu Breana } 92366a3270STiberiu Breana 93366a3270STiberiu Breana return crc; 94366a3270STiberiu Breana } 95366a3270STiberiu Breana 96366a3270STiberiu Breana /* Simple function that sends a few bytes to the device to wake it up. */ 97366a3270STiberiu Breana static void am2315_ping(struct i2c_client *client) 98366a3270STiberiu Breana { 99366a3270STiberiu Breana i2c_smbus_read_byte_data(client, AM2315_REG_HUM_MSB); 100366a3270STiberiu Breana } 101366a3270STiberiu Breana 102366a3270STiberiu Breana static int am2315_read_data(struct am2315_data *data, 103366a3270STiberiu Breana struct am2315_sensor_data *sensor_data) 104366a3270STiberiu Breana { 105366a3270STiberiu Breana int ret; 106366a3270STiberiu Breana /* tx_buf format: <function code> <start addr> <nr of regs to read> */ 107366a3270STiberiu Breana u8 tx_buf[3] = { AM2315_FUNCTION_READ, AM2315_REG_HUM_MSB, 4 }; 108366a3270STiberiu Breana /* 109366a3270STiberiu Breana * rx_buf format: 110366a3270STiberiu Breana * <function code> <number of registers read> 111366a3270STiberiu Breana * <humidity MSB> <humidity LSB> <temp MSB> <temp LSB> 112366a3270STiberiu Breana * <CRC LSB> <CRC MSB> 113366a3270STiberiu Breana */ 114366a3270STiberiu Breana u8 rx_buf[8]; 115366a3270STiberiu Breana u16 crc; 116366a3270STiberiu Breana 117366a3270STiberiu Breana /* First wake up the device. */ 118366a3270STiberiu Breana am2315_ping(data->client); 119366a3270STiberiu Breana 120366a3270STiberiu Breana mutex_lock(&data->lock); 121366a3270STiberiu Breana ret = i2c_master_send(data->client, tx_buf, sizeof(tx_buf)); 122366a3270STiberiu Breana if (ret < 0) { 123366a3270STiberiu Breana dev_err(&data->client->dev, "failed to send read request\n"); 124366a3270STiberiu Breana goto exit_unlock; 125366a3270STiberiu Breana } 126366a3270STiberiu Breana /* Wait 2-3 ms, then read back the data sent by the device. */ 127366a3270STiberiu Breana usleep_range(2000, 3000); 128366a3270STiberiu Breana /* Do a bulk data read, then pick out what we need. */ 129366a3270STiberiu Breana ret = i2c_master_recv(data->client, rx_buf, sizeof(rx_buf)); 130366a3270STiberiu Breana if (ret < 0) { 131366a3270STiberiu Breana dev_err(&data->client->dev, "failed to read sensor data\n"); 132366a3270STiberiu Breana goto exit_unlock; 133366a3270STiberiu Breana } 134366a3270STiberiu Breana mutex_unlock(&data->lock); 135366a3270STiberiu Breana /* 136366a3270STiberiu Breana * Do a CRC check on the data and compare it to the value 137366a3270STiberiu Breana * calculated by the device. 138366a3270STiberiu Breana */ 139366a3270STiberiu Breana crc = am2315_crc(rx_buf, sizeof(rx_buf) - 2); 140366a3270STiberiu Breana if ((crc & 0xff) != rx_buf[6] || (crc >> 8) != rx_buf[7]) { 141366a3270STiberiu Breana dev_err(&data->client->dev, "failed to verify sensor data\n"); 142366a3270STiberiu Breana return -EIO; 143366a3270STiberiu Breana } 144366a3270STiberiu Breana 145366a3270STiberiu Breana sensor_data->hum_data = (rx_buf[AM2315_HUM_OFFSET] << 8) | 146366a3270STiberiu Breana rx_buf[AM2315_HUM_OFFSET + 1]; 147366a3270STiberiu Breana sensor_data->temp_data = (rx_buf[AM2315_TEMP_OFFSET] << 8) | 148366a3270STiberiu Breana rx_buf[AM2315_TEMP_OFFSET + 1]; 149366a3270STiberiu Breana 150366a3270STiberiu Breana return ret; 151366a3270STiberiu Breana 152366a3270STiberiu Breana exit_unlock: 153366a3270STiberiu Breana mutex_unlock(&data->lock); 154366a3270STiberiu Breana return ret; 155366a3270STiberiu Breana } 156366a3270STiberiu Breana 1570d96d5eaSTiberiu Breana static irqreturn_t am2315_trigger_handler(int irq, void *p) 1580d96d5eaSTiberiu Breana { 1590d96d5eaSTiberiu Breana int i; 1600d96d5eaSTiberiu Breana int ret; 1610d96d5eaSTiberiu Breana int bit; 1620d96d5eaSTiberiu Breana struct iio_poll_func *pf = p; 1630d96d5eaSTiberiu Breana struct iio_dev *indio_dev = pf->indio_dev; 1640d96d5eaSTiberiu Breana struct am2315_data *data = iio_priv(indio_dev); 1650d96d5eaSTiberiu Breana struct am2315_sensor_data sensor_data; 1660d96d5eaSTiberiu Breana 1670d96d5eaSTiberiu Breana ret = am2315_read_data(data, &sensor_data); 1681cb0d06aSDan Carpenter if (ret < 0) 1690d96d5eaSTiberiu Breana goto err; 1700d96d5eaSTiberiu Breana 1710d96d5eaSTiberiu Breana mutex_lock(&data->lock); 1720d96d5eaSTiberiu Breana if (*(indio_dev->active_scan_mask) == AM2315_ALL_CHANNEL_MASK) { 173f4ca2e25SJonathan Cameron data->scan.chans[0] = sensor_data.hum_data; 174f4ca2e25SJonathan Cameron data->scan.chans[1] = sensor_data.temp_data; 1750d96d5eaSTiberiu Breana } else { 1760d96d5eaSTiberiu Breana i = 0; 177*72933488SNuno Sa iio_for_each_active_channel(indio_dev, bit) { 178f4ca2e25SJonathan Cameron data->scan.chans[i] = (bit ? sensor_data.temp_data : 1790d96d5eaSTiberiu Breana sensor_data.hum_data); 1800d96d5eaSTiberiu Breana i++; 1810d96d5eaSTiberiu Breana } 1820d96d5eaSTiberiu Breana } 1830d96d5eaSTiberiu Breana mutex_unlock(&data->lock); 1840d96d5eaSTiberiu Breana 185f4ca2e25SJonathan Cameron iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, 1860d96d5eaSTiberiu Breana pf->timestamp); 1870d96d5eaSTiberiu Breana err: 1880d96d5eaSTiberiu Breana iio_trigger_notify_done(indio_dev->trig); 1890d96d5eaSTiberiu Breana return IRQ_HANDLED; 1900d96d5eaSTiberiu Breana } 1910d96d5eaSTiberiu Breana 192366a3270STiberiu Breana static int am2315_read_raw(struct iio_dev *indio_dev, 193366a3270STiberiu Breana struct iio_chan_spec const *chan, 194366a3270STiberiu Breana int *val, int *val2, long mask) 195366a3270STiberiu Breana { 196366a3270STiberiu Breana int ret; 197366a3270STiberiu Breana struct am2315_sensor_data sensor_data; 198366a3270STiberiu Breana struct am2315_data *data = iio_priv(indio_dev); 199366a3270STiberiu Breana 200366a3270STiberiu Breana switch (mask) { 201366a3270STiberiu Breana case IIO_CHAN_INFO_RAW: 202366a3270STiberiu Breana ret = am2315_read_data(data, &sensor_data); 203366a3270STiberiu Breana if (ret < 0) 204366a3270STiberiu Breana return ret; 205366a3270STiberiu Breana *val = (chan->type == IIO_HUMIDITYRELATIVE) ? 206366a3270STiberiu Breana sensor_data.hum_data : sensor_data.temp_data; 207366a3270STiberiu Breana return IIO_VAL_INT; 208366a3270STiberiu Breana case IIO_CHAN_INFO_SCALE: 209366a3270STiberiu Breana *val = 100; 210366a3270STiberiu Breana return IIO_VAL_INT; 211366a3270STiberiu Breana } 212366a3270STiberiu Breana 213366a3270STiberiu Breana return -EINVAL; 214366a3270STiberiu Breana } 215366a3270STiberiu Breana 216366a3270STiberiu Breana static const struct iio_info am2315_info = { 217366a3270STiberiu Breana .read_raw = am2315_read_raw, 218366a3270STiberiu Breana }; 219366a3270STiberiu Breana 2201b1a60e7SUwe Kleine-König static int am2315_probe(struct i2c_client *client) 221366a3270STiberiu Breana { 2220d96d5eaSTiberiu Breana int ret; 223366a3270STiberiu Breana struct iio_dev *indio_dev; 224366a3270STiberiu Breana struct am2315_data *data; 225366a3270STiberiu Breana 226366a3270STiberiu Breana indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 227366a3270STiberiu Breana if (!indio_dev) { 228366a3270STiberiu Breana dev_err(&client->dev, "iio allocation failed!\n"); 229366a3270STiberiu Breana return -ENOMEM; 230366a3270STiberiu Breana } 231366a3270STiberiu Breana 232366a3270STiberiu Breana data = iio_priv(indio_dev); 233366a3270STiberiu Breana data->client = client; 234366a3270STiberiu Breana i2c_set_clientdata(client, indio_dev); 235366a3270STiberiu Breana mutex_init(&data->lock); 236366a3270STiberiu Breana 237366a3270STiberiu Breana indio_dev->info = &am2315_info; 238366a3270STiberiu Breana indio_dev->name = AM2315_DRIVER_NAME; 239366a3270STiberiu Breana indio_dev->modes = INDIO_DIRECT_MODE; 240366a3270STiberiu Breana indio_dev->channels = am2315_channels; 241366a3270STiberiu Breana indio_dev->num_channels = ARRAY_SIZE(am2315_channels); 242366a3270STiberiu Breana 24323f1ce3cSChuhong Yuan ret = devm_iio_triggered_buffer_setup(&client->dev, 24423f1ce3cSChuhong Yuan indio_dev, iio_pollfunc_store_time, 2450d96d5eaSTiberiu Breana am2315_trigger_handler, NULL); 2460d96d5eaSTiberiu Breana if (ret < 0) { 2470d96d5eaSTiberiu Breana dev_err(&client->dev, "iio triggered buffer setup failed\n"); 2480d96d5eaSTiberiu Breana return ret; 2490d96d5eaSTiberiu Breana } 2500d96d5eaSTiberiu Breana 25123f1ce3cSChuhong Yuan return devm_iio_device_register(&client->dev, indio_dev); 252366a3270STiberiu Breana } 253366a3270STiberiu Breana 254366a3270STiberiu Breana static const struct i2c_device_id am2315_i2c_id[] = { 2554391affaSUwe Kleine-König { "am2315" }, 256366a3270STiberiu Breana {} 257366a3270STiberiu Breana }; 258cdd469adSJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, am2315_i2c_id); 259366a3270STiberiu Breana 260366a3270STiberiu Breana static struct i2c_driver am2315_driver = { 261366a3270STiberiu Breana .driver = { 262366a3270STiberiu Breana .name = "am2315", 263366a3270STiberiu Breana }, 2647cf15f42SUwe Kleine-König .probe = am2315_probe, 265366a3270STiberiu Breana .id_table = am2315_i2c_id, 266366a3270STiberiu Breana }; 267366a3270STiberiu Breana 268366a3270STiberiu Breana module_i2c_driver(am2315_driver); 269366a3270STiberiu Breana 270366a3270STiberiu Breana MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>"); 271366a3270STiberiu Breana MODULE_DESCRIPTION("Aosong AM2315 relative humidity and temperature"); 272366a3270STiberiu Breana MODULE_LICENSE("GPL v2"); 273