1e5a63942SPeter Meerwald /* 2e5a63942SPeter Meerwald * tmp006.c - Support for TI TMP006 IR thermopile sensor 3e5a63942SPeter Meerwald * 4e5a63942SPeter Meerwald * Copyright (c) 2013 Peter Meerwald <pmeerw@pmeerw.net> 5e5a63942SPeter Meerwald * 6e5a63942SPeter Meerwald * This file is subject to the terms and conditions of version 2 of 7e5a63942SPeter Meerwald * the GNU General Public License. See the file COPYING in the main 8e5a63942SPeter Meerwald * directory of this archive for more details. 9e5a63942SPeter Meerwald * 10e5a63942SPeter Meerwald * Driver for the Texas Instruments I2C 16-bit IR thermopile sensor 11e5a63942SPeter Meerwald * 12e5a63942SPeter Meerwald * (7-bit I2C slave address 0x40, changeable via ADR pins) 13e5a63942SPeter Meerwald * 14e5a63942SPeter Meerwald * TODO: data ready irq 15e5a63942SPeter Meerwald */ 16e5a63942SPeter Meerwald 17e5a63942SPeter Meerwald #include <linux/err.h> 18e5a63942SPeter Meerwald #include <linux/i2c.h> 19e5a63942SPeter Meerwald #include <linux/delay.h> 20e5a63942SPeter Meerwald #include <linux/module.h> 21e5a63942SPeter Meerwald #include <linux/pm.h> 22e5a63942SPeter Meerwald #include <linux/bitops.h> 23e5a63942SPeter Meerwald 24e5a63942SPeter Meerwald #include <linux/iio/iio.h> 25e5a63942SPeter Meerwald #include <linux/iio/sysfs.h> 26e5a63942SPeter Meerwald 27e5a63942SPeter Meerwald #define TMP006_VOBJECT 0x00 28e5a63942SPeter Meerwald #define TMP006_TAMBIENT 0x01 29e5a63942SPeter Meerwald #define TMP006_CONFIG 0x02 30e5a63942SPeter Meerwald #define TMP006_MANUFACTURER_ID 0xfe 31e5a63942SPeter Meerwald #define TMP006_DEVICE_ID 0xff 32e5a63942SPeter Meerwald 33e5a63942SPeter Meerwald #define TMP006_TAMBIENT_SHIFT 2 34e5a63942SPeter Meerwald 35e5a63942SPeter Meerwald #define TMP006_CONFIG_RESET BIT(15) 36e5a63942SPeter Meerwald #define TMP006_CONFIG_DRDY_EN BIT(8) 37e5a63942SPeter Meerwald #define TMP006_CONFIG_DRDY BIT(7) 38e5a63942SPeter Meerwald 39e5a63942SPeter Meerwald #define TMP006_CONFIG_MOD_MASK 0x7000 40e5a63942SPeter Meerwald 41e5a63942SPeter Meerwald #define TMP006_CONFIG_CR_MASK 0x0e00 42e5a63942SPeter Meerwald #define TMP006_CONFIG_CR_SHIFT 9 43e5a63942SPeter Meerwald 44e5a63942SPeter Meerwald #define MANUFACTURER_MAGIC 0x5449 45e5a63942SPeter Meerwald #define DEVICE_MAGIC 0x0067 46e5a63942SPeter Meerwald 47e5a63942SPeter Meerwald struct tmp006_data { 48e5a63942SPeter Meerwald struct i2c_client *client; 49e5a63942SPeter Meerwald u16 config; 50e5a63942SPeter Meerwald }; 51e5a63942SPeter Meerwald 52e5a63942SPeter Meerwald static int tmp006_read_measurement(struct tmp006_data *data, u8 reg) 53e5a63942SPeter Meerwald { 54e5a63942SPeter Meerwald s32 ret; 55e5a63942SPeter Meerwald int tries = 50; 56e5a63942SPeter Meerwald 57e5a63942SPeter Meerwald while (tries-- > 0) { 58e5a63942SPeter Meerwald ret = i2c_smbus_read_word_swapped(data->client, 59e5a63942SPeter Meerwald TMP006_CONFIG); 60e5a63942SPeter Meerwald if (ret < 0) 61e5a63942SPeter Meerwald return ret; 62e5a63942SPeter Meerwald if (ret & TMP006_CONFIG_DRDY) 63e5a63942SPeter Meerwald break; 64e5a63942SPeter Meerwald msleep(100); 65e5a63942SPeter Meerwald } 66e5a63942SPeter Meerwald 67e5a63942SPeter Meerwald if (tries < 0) 68e5a63942SPeter Meerwald return -EIO; 69e5a63942SPeter Meerwald 70e5a63942SPeter Meerwald return i2c_smbus_read_word_swapped(data->client, reg); 71e5a63942SPeter Meerwald } 72e5a63942SPeter Meerwald 73e5a63942SPeter Meerwald static int tmp006_read_raw(struct iio_dev *indio_dev, 74e5a63942SPeter Meerwald struct iio_chan_spec const *channel, int *val, 75e5a63942SPeter Meerwald int *val2, long mask) 76e5a63942SPeter Meerwald { 77e5a63942SPeter Meerwald struct tmp006_data *data = iio_priv(indio_dev); 78e5a63942SPeter Meerwald s32 ret; 79e5a63942SPeter Meerwald 80e5a63942SPeter Meerwald switch (mask) { 81e5a63942SPeter Meerwald case IIO_CHAN_INFO_RAW: 82e5a63942SPeter Meerwald if (channel->type == IIO_VOLTAGE) { 83e5a63942SPeter Meerwald /* LSB is 156.25 nV */ 84e5a63942SPeter Meerwald ret = tmp006_read_measurement(data, TMP006_VOBJECT); 85e5a63942SPeter Meerwald if (ret < 0) 86e5a63942SPeter Meerwald return ret; 87e5a63942SPeter Meerwald *val = sign_extend32(ret, 15); 88e5a63942SPeter Meerwald } else if (channel->type == IIO_TEMP) { 89e5a63942SPeter Meerwald /* LSB is 0.03125 degrees Celsius */ 90e5a63942SPeter Meerwald ret = tmp006_read_measurement(data, TMP006_TAMBIENT); 91e5a63942SPeter Meerwald if (ret < 0) 92e5a63942SPeter Meerwald return ret; 93e5a63942SPeter Meerwald *val = sign_extend32(ret, 15) >> TMP006_TAMBIENT_SHIFT; 94e5a63942SPeter Meerwald } else { 95e5a63942SPeter Meerwald break; 96e5a63942SPeter Meerwald } 97e5a63942SPeter Meerwald return IIO_VAL_INT; 98e5a63942SPeter Meerwald case IIO_CHAN_INFO_SCALE: 99e5a63942SPeter Meerwald if (channel->type == IIO_VOLTAGE) { 100e5a63942SPeter Meerwald *val = 0; 101e5a63942SPeter Meerwald *val2 = 156250; 102e5a63942SPeter Meerwald } else if (channel->type == IIO_TEMP) { 103e5a63942SPeter Meerwald *val = 31; 104e5a63942SPeter Meerwald *val2 = 250000; 105e5a63942SPeter Meerwald } else { 106e5a63942SPeter Meerwald break; 107e5a63942SPeter Meerwald } 108e5a63942SPeter Meerwald return IIO_VAL_INT_PLUS_MICRO; 109e5a63942SPeter Meerwald default: 110e5a63942SPeter Meerwald break; 111e5a63942SPeter Meerwald } 112e5a63942SPeter Meerwald 113e5a63942SPeter Meerwald return -EINVAL; 114e5a63942SPeter Meerwald } 115e5a63942SPeter Meerwald 116e5a63942SPeter Meerwald static const char * const tmp006_freqs[] = { "4", "2", "1", "0.5", "0.25" }; 117e5a63942SPeter Meerwald 118e5a63942SPeter Meerwald static ssize_t tmp006_show_freq(struct device *dev, 119e5a63942SPeter Meerwald struct device_attribute *attr, char *buf) 120e5a63942SPeter Meerwald { 121e5a63942SPeter Meerwald struct tmp006_data *data = iio_priv(dev_to_iio_dev(dev)); 122e5a63942SPeter Meerwald int cr = (data->config & TMP006_CONFIG_CR_MASK) 123e5a63942SPeter Meerwald >> TMP006_CONFIG_CR_SHIFT; 124e5a63942SPeter Meerwald return sprintf(buf, "%s\n", tmp006_freqs[cr]); 125e5a63942SPeter Meerwald } 126e5a63942SPeter Meerwald 127e5a63942SPeter Meerwald static ssize_t tmp006_store_freq(struct device *dev, 128e5a63942SPeter Meerwald struct device_attribute *attr, 129e5a63942SPeter Meerwald const char *buf, size_t len) 130e5a63942SPeter Meerwald { 131e5a63942SPeter Meerwald struct iio_dev *indio_dev = dev_to_iio_dev(dev); 132e5a63942SPeter Meerwald struct tmp006_data *data = iio_priv(indio_dev); 133e5a63942SPeter Meerwald int i; 134e5a63942SPeter Meerwald bool found = false; 135e5a63942SPeter Meerwald 136e5a63942SPeter Meerwald for (i = 0; i < ARRAY_SIZE(tmp006_freqs); i++) 137e5a63942SPeter Meerwald if (sysfs_streq(buf, tmp006_freqs[i])) { 138e5a63942SPeter Meerwald found = true; 139e5a63942SPeter Meerwald break; 140e5a63942SPeter Meerwald } 141e5a63942SPeter Meerwald if (!found) 142e5a63942SPeter Meerwald return -EINVAL; 143e5a63942SPeter Meerwald 144e5a63942SPeter Meerwald data->config &= ~TMP006_CONFIG_CR_MASK; 145e5a63942SPeter Meerwald data->config |= i << TMP006_CONFIG_CR_SHIFT; 146e5a63942SPeter Meerwald 147e5a63942SPeter Meerwald return i2c_smbus_write_word_swapped(data->client, TMP006_CONFIG, 148e5a63942SPeter Meerwald data->config); 149e5a63942SPeter Meerwald } 150e5a63942SPeter Meerwald 151e5a63942SPeter Meerwald static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR, 152e5a63942SPeter Meerwald tmp006_show_freq, tmp006_store_freq); 153e5a63942SPeter Meerwald 154e5a63942SPeter Meerwald static IIO_CONST_ATTR(sampling_frequency_available, "4 2 1 0.5 0.25"); 155e5a63942SPeter Meerwald 156e5a63942SPeter Meerwald static struct attribute *tmp006_attributes[] = { 157e5a63942SPeter Meerwald &iio_dev_attr_sampling_frequency.dev_attr.attr, 158e5a63942SPeter Meerwald &iio_const_attr_sampling_frequency_available.dev_attr.attr, 159e5a63942SPeter Meerwald NULL 160e5a63942SPeter Meerwald }; 161e5a63942SPeter Meerwald 162e5a63942SPeter Meerwald static const struct attribute_group tmp006_attribute_group = { 163e5a63942SPeter Meerwald .attrs = tmp006_attributes, 164e5a63942SPeter Meerwald }; 165e5a63942SPeter Meerwald 166e5a63942SPeter Meerwald static const struct iio_chan_spec tmp006_channels[] = { 167e5a63942SPeter Meerwald { 168e5a63942SPeter Meerwald .type = IIO_VOLTAGE, 169e5a63942SPeter Meerwald .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 170e5a63942SPeter Meerwald BIT(IIO_CHAN_INFO_SCALE), 171e5a63942SPeter Meerwald }, 172e5a63942SPeter Meerwald { 173e5a63942SPeter Meerwald .type = IIO_TEMP, 174e5a63942SPeter Meerwald .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 175e5a63942SPeter Meerwald BIT(IIO_CHAN_INFO_SCALE), 176e5a63942SPeter Meerwald } 177e5a63942SPeter Meerwald }; 178e5a63942SPeter Meerwald 179e5a63942SPeter Meerwald static const struct iio_info tmp006_info = { 180e5a63942SPeter Meerwald .read_raw = tmp006_read_raw, 181e5a63942SPeter Meerwald .attrs = &tmp006_attribute_group, 182e5a63942SPeter Meerwald .driver_module = THIS_MODULE, 183e5a63942SPeter Meerwald }; 184e5a63942SPeter Meerwald 185e5a63942SPeter Meerwald static bool tmp006_check_identification(struct i2c_client *client) 186e5a63942SPeter Meerwald { 187e5a63942SPeter Meerwald int mid, did; 188e5a63942SPeter Meerwald 189e5a63942SPeter Meerwald mid = i2c_smbus_read_word_swapped(client, TMP006_MANUFACTURER_ID); 190e5a63942SPeter Meerwald if (mid < 0) 191e5a63942SPeter Meerwald return false; 192e5a63942SPeter Meerwald 193e5a63942SPeter Meerwald did = i2c_smbus_read_word_swapped(client, TMP006_DEVICE_ID); 194e5a63942SPeter Meerwald if (did < 0) 195e5a63942SPeter Meerwald return false; 196e5a63942SPeter Meerwald 197e5a63942SPeter Meerwald return mid == MANUFACTURER_MAGIC && did == DEVICE_MAGIC; 198e5a63942SPeter Meerwald } 199e5a63942SPeter Meerwald 200e5a63942SPeter Meerwald static int tmp006_probe(struct i2c_client *client, 201e5a63942SPeter Meerwald const struct i2c_device_id *id) 202e5a63942SPeter Meerwald { 203e5a63942SPeter Meerwald struct iio_dev *indio_dev; 204e5a63942SPeter Meerwald struct tmp006_data *data; 205e5a63942SPeter Meerwald int ret; 206e5a63942SPeter Meerwald 207e5a63942SPeter Meerwald if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) 208e5a63942SPeter Meerwald return -ENODEV; 209e5a63942SPeter Meerwald 210e5a63942SPeter Meerwald if (!tmp006_check_identification(client)) { 211e5a63942SPeter Meerwald dev_err(&client->dev, "no TMP006 sensor\n"); 212e5a63942SPeter Meerwald return -ENODEV; 213e5a63942SPeter Meerwald } 214e5a63942SPeter Meerwald 215e5a63942SPeter Meerwald indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 216e5a63942SPeter Meerwald if (!indio_dev) 217e5a63942SPeter Meerwald return -ENOMEM; 218e5a63942SPeter Meerwald 219e5a63942SPeter Meerwald data = iio_priv(indio_dev); 220e5a63942SPeter Meerwald i2c_set_clientdata(client, indio_dev); 221e5a63942SPeter Meerwald data->client = client; 222e5a63942SPeter Meerwald 223e5a63942SPeter Meerwald indio_dev->dev.parent = &client->dev; 224e5a63942SPeter Meerwald indio_dev->name = dev_name(&client->dev); 225e5a63942SPeter Meerwald indio_dev->modes = INDIO_DIRECT_MODE; 226e5a63942SPeter Meerwald indio_dev->info = &tmp006_info; 227e5a63942SPeter Meerwald 228e5a63942SPeter Meerwald indio_dev->channels = tmp006_channels; 229e5a63942SPeter Meerwald indio_dev->num_channels = ARRAY_SIZE(tmp006_channels); 230e5a63942SPeter Meerwald 231e5a63942SPeter Meerwald ret = i2c_smbus_read_word_swapped(data->client, TMP006_CONFIG); 232e5a63942SPeter Meerwald if (ret < 0) 233e5a63942SPeter Meerwald return ret; 234e5a63942SPeter Meerwald data->config = ret; 235e5a63942SPeter Meerwald 236e5a63942SPeter Meerwald return iio_device_register(indio_dev); 237e5a63942SPeter Meerwald } 238e5a63942SPeter Meerwald 239e5a63942SPeter Meerwald static int tmp006_powerdown(struct tmp006_data *data) 240e5a63942SPeter Meerwald { 241e5a63942SPeter Meerwald return i2c_smbus_write_word_swapped(data->client, TMP006_CONFIG, 242e5a63942SPeter Meerwald data->config & ~TMP006_CONFIG_MOD_MASK); 243e5a63942SPeter Meerwald } 244e5a63942SPeter Meerwald 245e5a63942SPeter Meerwald static int tmp006_remove(struct i2c_client *client) 246e5a63942SPeter Meerwald { 247e5a63942SPeter Meerwald struct iio_dev *indio_dev = i2c_get_clientdata(client); 248e5a63942SPeter Meerwald 249e5a63942SPeter Meerwald iio_device_unregister(indio_dev); 250e5a63942SPeter Meerwald tmp006_powerdown(iio_priv(indio_dev)); 251e5a63942SPeter Meerwald 252e5a63942SPeter Meerwald return 0; 253e5a63942SPeter Meerwald } 254e5a63942SPeter Meerwald 255e5a63942SPeter Meerwald #ifdef CONFIG_PM_SLEEP 256e5a63942SPeter Meerwald static int tmp006_suspend(struct device *dev) 257e5a63942SPeter Meerwald { 258*d320f1b4SPeter Meerwald struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 259*d320f1b4SPeter Meerwald return tmp006_powerdown(iio_priv(indio_dev)); 260e5a63942SPeter Meerwald } 261e5a63942SPeter Meerwald 262e5a63942SPeter Meerwald static int tmp006_resume(struct device *dev) 263e5a63942SPeter Meerwald { 264*d320f1b4SPeter Meerwald struct tmp006_data *data = iio_priv(i2c_get_clientdata( 265*d320f1b4SPeter Meerwald to_i2c_client(dev))); 266e5a63942SPeter Meerwald return i2c_smbus_write_word_swapped(data->client, TMP006_CONFIG, 267e5a63942SPeter Meerwald data->config | TMP006_CONFIG_MOD_MASK); 268e5a63942SPeter Meerwald } 269e5a63942SPeter Meerwald #endif 270e5a63942SPeter Meerwald 271e5a63942SPeter Meerwald static SIMPLE_DEV_PM_OPS(tmp006_pm_ops, tmp006_suspend, tmp006_resume); 272e5a63942SPeter Meerwald 273e5a63942SPeter Meerwald static const struct i2c_device_id tmp006_id[] = { 274e5a63942SPeter Meerwald { "tmp006", 0 }, 275e5a63942SPeter Meerwald { } 276e5a63942SPeter Meerwald }; 277e5a63942SPeter Meerwald MODULE_DEVICE_TABLE(i2c, tmp006_id); 278e5a63942SPeter Meerwald 279e5a63942SPeter Meerwald static struct i2c_driver tmp006_driver = { 280e5a63942SPeter Meerwald .driver = { 281e5a63942SPeter Meerwald .name = "tmp006", 282e5a63942SPeter Meerwald .pm = &tmp006_pm_ops, 283e5a63942SPeter Meerwald .owner = THIS_MODULE, 284e5a63942SPeter Meerwald }, 285e5a63942SPeter Meerwald .probe = tmp006_probe, 286e5a63942SPeter Meerwald .remove = tmp006_remove, 287e5a63942SPeter Meerwald .id_table = tmp006_id, 288e5a63942SPeter Meerwald }; 289e5a63942SPeter Meerwald module_i2c_driver(tmp006_driver); 290e5a63942SPeter Meerwald 291e5a63942SPeter Meerwald MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); 292e5a63942SPeter Meerwald MODULE_DESCRIPTION("TI TMP006 IR thermopile sensor driver"); 293e5a63942SPeter Meerwald MODULE_LICENSE("GPL"); 294