136edc939SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2366a3270STiberiu Breana /** 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/acpi.h> 11366a3270STiberiu Breana #include <linux/delay.h> 12366a3270STiberiu Breana #include <linux/i2c.h> 13366a3270STiberiu Breana #include <linux/kernel.h> 14366a3270STiberiu Breana #include <linux/module.h> 150d96d5eaSTiberiu Breana #include <linux/iio/buffer.h> 16366a3270STiberiu Breana #include <linux/iio/iio.h> 17366a3270STiberiu Breana #include <linux/iio/sysfs.h> 180d96d5eaSTiberiu Breana #include <linux/iio/trigger_consumer.h> 190d96d5eaSTiberiu Breana #include <linux/iio/triggered_buffer.h> 20366a3270STiberiu Breana 21366a3270STiberiu Breana #define AM2315_REG_HUM_MSB 0x00 22366a3270STiberiu Breana #define AM2315_REG_HUM_LSB 0x01 23366a3270STiberiu Breana #define AM2315_REG_TEMP_MSB 0x02 24366a3270STiberiu Breana #define AM2315_REG_TEMP_LSB 0x03 25366a3270STiberiu Breana 26366a3270STiberiu Breana #define AM2315_FUNCTION_READ 0x03 27366a3270STiberiu Breana #define AM2315_HUM_OFFSET 2 28366a3270STiberiu Breana #define AM2315_TEMP_OFFSET 4 290d96d5eaSTiberiu Breana #define AM2315_ALL_CHANNEL_MASK GENMASK(1, 0) 30366a3270STiberiu Breana 31366a3270STiberiu Breana #define AM2315_DRIVER_NAME "am2315" 32366a3270STiberiu Breana 33366a3270STiberiu Breana struct am2315_data { 34366a3270STiberiu Breana struct i2c_client *client; 35366a3270STiberiu Breana struct mutex lock; 360d96d5eaSTiberiu Breana s16 buffer[8]; /* 2x16-bit channels + 2x16 padding + 4x16 timestamp */ 37366a3270STiberiu Breana }; 38366a3270STiberiu Breana 39366a3270STiberiu Breana struct am2315_sensor_data { 40366a3270STiberiu Breana s16 hum_data; 41366a3270STiberiu Breana s16 temp_data; 42366a3270STiberiu Breana }; 43366a3270STiberiu Breana 44366a3270STiberiu Breana static const struct iio_chan_spec am2315_channels[] = { 45366a3270STiberiu Breana { 46366a3270STiberiu Breana .type = IIO_HUMIDITYRELATIVE, 47366a3270STiberiu Breana .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 480d96d5eaSTiberiu Breana BIT(IIO_CHAN_INFO_SCALE), 490d96d5eaSTiberiu Breana .scan_index = 0, 500d96d5eaSTiberiu Breana .scan_type = { 510d96d5eaSTiberiu Breana .sign = 's', 520d96d5eaSTiberiu Breana .realbits = 16, 530d96d5eaSTiberiu Breana .storagebits = 16, 540d96d5eaSTiberiu Breana .endianness = IIO_CPU, 550d96d5eaSTiberiu Breana }, 56366a3270STiberiu Breana }, 57366a3270STiberiu Breana { 58366a3270STiberiu Breana .type = IIO_TEMP, 59366a3270STiberiu Breana .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 600d96d5eaSTiberiu Breana BIT(IIO_CHAN_INFO_SCALE), 610d96d5eaSTiberiu Breana .scan_index = 1, 620d96d5eaSTiberiu Breana .scan_type = { 630d96d5eaSTiberiu Breana .sign = 's', 640d96d5eaSTiberiu Breana .realbits = 16, 650d96d5eaSTiberiu Breana .storagebits = 16, 660d96d5eaSTiberiu Breana .endianness = IIO_CPU, 67366a3270STiberiu Breana }, 680d96d5eaSTiberiu Breana }, 690d96d5eaSTiberiu Breana IIO_CHAN_SOFT_TIMESTAMP(2), 70366a3270STiberiu Breana }; 71366a3270STiberiu Breana 72366a3270STiberiu Breana /* CRC calculation algorithm, as specified in the datasheet (page 13). */ 73366a3270STiberiu Breana static u16 am2315_crc(u8 *data, u8 nr_bytes) 74366a3270STiberiu Breana { 75366a3270STiberiu Breana int i; 76366a3270STiberiu Breana u16 crc = 0xffff; 77366a3270STiberiu Breana 78366a3270STiberiu Breana while (nr_bytes--) { 79366a3270STiberiu Breana crc ^= *data++; 80366a3270STiberiu Breana for (i = 0; i < 8; i++) { 81366a3270STiberiu Breana if (crc & 0x01) { 82366a3270STiberiu Breana crc >>= 1; 83366a3270STiberiu Breana crc ^= 0xA001; 84366a3270STiberiu Breana } else { 85366a3270STiberiu Breana crc >>= 1; 86366a3270STiberiu Breana } 87366a3270STiberiu Breana } 88366a3270STiberiu Breana } 89366a3270STiberiu Breana 90366a3270STiberiu Breana return crc; 91366a3270STiberiu Breana } 92366a3270STiberiu Breana 93366a3270STiberiu Breana /* Simple function that sends a few bytes to the device to wake it up. */ 94366a3270STiberiu Breana static void am2315_ping(struct i2c_client *client) 95366a3270STiberiu Breana { 96366a3270STiberiu Breana i2c_smbus_read_byte_data(client, AM2315_REG_HUM_MSB); 97366a3270STiberiu Breana } 98366a3270STiberiu Breana 99366a3270STiberiu Breana static int am2315_read_data(struct am2315_data *data, 100366a3270STiberiu Breana struct am2315_sensor_data *sensor_data) 101366a3270STiberiu Breana { 102366a3270STiberiu Breana int ret; 103366a3270STiberiu Breana /* tx_buf format: <function code> <start addr> <nr of regs to read> */ 104366a3270STiberiu Breana u8 tx_buf[3] = { AM2315_FUNCTION_READ, AM2315_REG_HUM_MSB, 4 }; 105366a3270STiberiu Breana /* 106366a3270STiberiu Breana * rx_buf format: 107366a3270STiberiu Breana * <function code> <number of registers read> 108366a3270STiberiu Breana * <humidity MSB> <humidity LSB> <temp MSB> <temp LSB> 109366a3270STiberiu Breana * <CRC LSB> <CRC MSB> 110366a3270STiberiu Breana */ 111366a3270STiberiu Breana u8 rx_buf[8]; 112366a3270STiberiu Breana u16 crc; 113366a3270STiberiu Breana 114366a3270STiberiu Breana /* First wake up the device. */ 115366a3270STiberiu Breana am2315_ping(data->client); 116366a3270STiberiu Breana 117366a3270STiberiu Breana mutex_lock(&data->lock); 118366a3270STiberiu Breana ret = i2c_master_send(data->client, tx_buf, sizeof(tx_buf)); 119366a3270STiberiu Breana if (ret < 0) { 120366a3270STiberiu Breana dev_err(&data->client->dev, "failed to send read request\n"); 121366a3270STiberiu Breana goto exit_unlock; 122366a3270STiberiu Breana } 123366a3270STiberiu Breana /* Wait 2-3 ms, then read back the data sent by the device. */ 124366a3270STiberiu Breana usleep_range(2000, 3000); 125366a3270STiberiu Breana /* Do a bulk data read, then pick out what we need. */ 126366a3270STiberiu Breana ret = i2c_master_recv(data->client, rx_buf, sizeof(rx_buf)); 127366a3270STiberiu Breana if (ret < 0) { 128366a3270STiberiu Breana dev_err(&data->client->dev, "failed to read sensor data\n"); 129366a3270STiberiu Breana goto exit_unlock; 130366a3270STiberiu Breana } 131366a3270STiberiu Breana mutex_unlock(&data->lock); 132366a3270STiberiu Breana /* 133366a3270STiberiu Breana * Do a CRC check on the data and compare it to the value 134366a3270STiberiu Breana * calculated by the device. 135366a3270STiberiu Breana */ 136366a3270STiberiu Breana crc = am2315_crc(rx_buf, sizeof(rx_buf) - 2); 137366a3270STiberiu Breana if ((crc & 0xff) != rx_buf[6] || (crc >> 8) != rx_buf[7]) { 138366a3270STiberiu Breana dev_err(&data->client->dev, "failed to verify sensor data\n"); 139366a3270STiberiu Breana return -EIO; 140366a3270STiberiu Breana } 141366a3270STiberiu Breana 142366a3270STiberiu Breana sensor_data->hum_data = (rx_buf[AM2315_HUM_OFFSET] << 8) | 143366a3270STiberiu Breana rx_buf[AM2315_HUM_OFFSET + 1]; 144366a3270STiberiu Breana sensor_data->temp_data = (rx_buf[AM2315_TEMP_OFFSET] << 8) | 145366a3270STiberiu Breana rx_buf[AM2315_TEMP_OFFSET + 1]; 146366a3270STiberiu Breana 147366a3270STiberiu Breana return ret; 148366a3270STiberiu Breana 149366a3270STiberiu Breana exit_unlock: 150366a3270STiberiu Breana mutex_unlock(&data->lock); 151366a3270STiberiu Breana return ret; 152366a3270STiberiu Breana } 153366a3270STiberiu Breana 1540d96d5eaSTiberiu Breana static irqreturn_t am2315_trigger_handler(int irq, void *p) 1550d96d5eaSTiberiu Breana { 1560d96d5eaSTiberiu Breana int i; 1570d96d5eaSTiberiu Breana int ret; 1580d96d5eaSTiberiu Breana int bit; 1590d96d5eaSTiberiu Breana struct iio_poll_func *pf = p; 1600d96d5eaSTiberiu Breana struct iio_dev *indio_dev = pf->indio_dev; 1610d96d5eaSTiberiu Breana struct am2315_data *data = iio_priv(indio_dev); 1620d96d5eaSTiberiu Breana struct am2315_sensor_data sensor_data; 1630d96d5eaSTiberiu Breana 1640d96d5eaSTiberiu Breana ret = am2315_read_data(data, &sensor_data); 1651cb0d06aSDan Carpenter if (ret < 0) 1660d96d5eaSTiberiu Breana goto err; 1670d96d5eaSTiberiu Breana 1680d96d5eaSTiberiu Breana mutex_lock(&data->lock); 1690d96d5eaSTiberiu Breana if (*(indio_dev->active_scan_mask) == AM2315_ALL_CHANNEL_MASK) { 1700d96d5eaSTiberiu Breana data->buffer[0] = sensor_data.hum_data; 1710d96d5eaSTiberiu Breana data->buffer[1] = sensor_data.temp_data; 1720d96d5eaSTiberiu Breana } else { 1730d96d5eaSTiberiu Breana i = 0; 1740d96d5eaSTiberiu Breana for_each_set_bit(bit, indio_dev->active_scan_mask, 1750d96d5eaSTiberiu Breana indio_dev->masklength) { 1760d96d5eaSTiberiu Breana data->buffer[i] = (bit ? sensor_data.temp_data : 1770d96d5eaSTiberiu Breana sensor_data.hum_data); 1780d96d5eaSTiberiu Breana i++; 1790d96d5eaSTiberiu Breana } 1800d96d5eaSTiberiu Breana } 1810d96d5eaSTiberiu Breana mutex_unlock(&data->lock); 1820d96d5eaSTiberiu Breana 1830d96d5eaSTiberiu Breana iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 1840d96d5eaSTiberiu Breana pf->timestamp); 1850d96d5eaSTiberiu Breana err: 1860d96d5eaSTiberiu Breana iio_trigger_notify_done(indio_dev->trig); 1870d96d5eaSTiberiu Breana return IRQ_HANDLED; 1880d96d5eaSTiberiu Breana } 1890d96d5eaSTiberiu Breana 190366a3270STiberiu Breana static int am2315_read_raw(struct iio_dev *indio_dev, 191366a3270STiberiu Breana struct iio_chan_spec const *chan, 192366a3270STiberiu Breana int *val, int *val2, long mask) 193366a3270STiberiu Breana { 194366a3270STiberiu Breana int ret; 195366a3270STiberiu Breana struct am2315_sensor_data sensor_data; 196366a3270STiberiu Breana struct am2315_data *data = iio_priv(indio_dev); 197366a3270STiberiu Breana 198366a3270STiberiu Breana switch (mask) { 199366a3270STiberiu Breana case IIO_CHAN_INFO_RAW: 200366a3270STiberiu Breana ret = am2315_read_data(data, &sensor_data); 201366a3270STiberiu Breana if (ret < 0) 202366a3270STiberiu Breana return ret; 203366a3270STiberiu Breana *val = (chan->type == IIO_HUMIDITYRELATIVE) ? 204366a3270STiberiu Breana sensor_data.hum_data : sensor_data.temp_data; 205366a3270STiberiu Breana return IIO_VAL_INT; 206366a3270STiberiu Breana case IIO_CHAN_INFO_SCALE: 207366a3270STiberiu Breana *val = 100; 208366a3270STiberiu Breana return IIO_VAL_INT; 209366a3270STiberiu Breana } 210366a3270STiberiu Breana 211366a3270STiberiu Breana return -EINVAL; 212366a3270STiberiu Breana } 213366a3270STiberiu Breana 214366a3270STiberiu Breana static const struct iio_info am2315_info = { 215366a3270STiberiu Breana .read_raw = am2315_read_raw, 216366a3270STiberiu Breana }; 217366a3270STiberiu Breana 218366a3270STiberiu Breana static int am2315_probe(struct i2c_client *client, 219366a3270STiberiu Breana const struct i2c_device_id *id) 220366a3270STiberiu Breana { 2210d96d5eaSTiberiu Breana int ret; 222366a3270STiberiu Breana struct iio_dev *indio_dev; 223366a3270STiberiu Breana struct am2315_data *data; 224366a3270STiberiu Breana 225366a3270STiberiu Breana indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 226366a3270STiberiu Breana if (!indio_dev) { 227366a3270STiberiu Breana dev_err(&client->dev, "iio allocation failed!\n"); 228366a3270STiberiu Breana return -ENOMEM; 229366a3270STiberiu Breana } 230366a3270STiberiu Breana 231366a3270STiberiu Breana data = iio_priv(indio_dev); 232366a3270STiberiu Breana data->client = client; 233366a3270STiberiu Breana i2c_set_clientdata(client, indio_dev); 234366a3270STiberiu Breana mutex_init(&data->lock); 235366a3270STiberiu Breana 236366a3270STiberiu Breana indio_dev->dev.parent = &client->dev; 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 243*23f1ce3cSChuhong Yuan ret = devm_iio_triggered_buffer_setup(&client->dev, 244*23f1ce3cSChuhong 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 251*23f1ce3cSChuhong 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[] = { 255366a3270STiberiu Breana {"am2315", 0}, 256366a3270STiberiu Breana {} 257366a3270STiberiu Breana }; 258cdd469adSJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, am2315_i2c_id); 259366a3270STiberiu Breana 260366a3270STiberiu Breana static const struct acpi_device_id am2315_acpi_id[] = { 261366a3270STiberiu Breana {"AOS2315", 0}, 262366a3270STiberiu Breana {} 263366a3270STiberiu Breana }; 264366a3270STiberiu Breana 265366a3270STiberiu Breana MODULE_DEVICE_TABLE(acpi, am2315_acpi_id); 266366a3270STiberiu Breana 267366a3270STiberiu Breana static struct i2c_driver am2315_driver = { 268366a3270STiberiu Breana .driver = { 269366a3270STiberiu Breana .name = "am2315", 270366a3270STiberiu Breana .acpi_match_table = ACPI_PTR(am2315_acpi_id), 271366a3270STiberiu Breana }, 272366a3270STiberiu Breana .probe = am2315_probe, 273366a3270STiberiu Breana .id_table = am2315_i2c_id, 274366a3270STiberiu Breana }; 275366a3270STiberiu Breana 276366a3270STiberiu Breana module_i2c_driver(am2315_driver); 277366a3270STiberiu Breana 278366a3270STiberiu Breana MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>"); 279366a3270STiberiu Breana MODULE_DESCRIPTION("Aosong AM2315 relative humidity and temperature"); 280366a3270STiberiu Breana MODULE_LICENSE("GPL v2"); 281