153bf4d06SLudovic Tancerel /* 253bf4d06SLudovic Tancerel * tsys02d.c - Support for Measurement-Specialties tsys02d temperature sensor 353bf4d06SLudovic Tancerel * 453bf4d06SLudovic Tancerel * Copyright (c) 2015 Measurement-Specialties 553bf4d06SLudovic Tancerel * 653bf4d06SLudovic Tancerel * Licensed under the GPL-2. 753bf4d06SLudovic Tancerel * 853bf4d06SLudovic Tancerel * (7-bit I2C slave address 0x40) 953bf4d06SLudovic Tancerel * 1053bf4d06SLudovic Tancerel * Datasheet: 1153bf4d06SLudovic Tancerel * http://www.meas-spec.com/downloads/Digital_Sensor_TSYS02D.pdf 1253bf4d06SLudovic Tancerel */ 1353bf4d06SLudovic Tancerel 1453bf4d06SLudovic Tancerel #include <linux/init.h> 1553bf4d06SLudovic Tancerel #include <linux/device.h> 1653bf4d06SLudovic Tancerel #include <linux/kernel.h> 1753bf4d06SLudovic Tancerel #include <linux/stat.h> 1853bf4d06SLudovic Tancerel #include <linux/module.h> 1953bf4d06SLudovic Tancerel #include <linux/iio/iio.h> 2053bf4d06SLudovic Tancerel #include <linux/iio/sysfs.h> 2153bf4d06SLudovic Tancerel 2253bf4d06SLudovic Tancerel #include "../common/ms_sensors/ms_sensors_i2c.h" 2353bf4d06SLudovic Tancerel 2453bf4d06SLudovic Tancerel #define TSYS02D_RESET 0xFE 2553bf4d06SLudovic Tancerel 2653bf4d06SLudovic Tancerel static const int tsys02d_samp_freq[4] = { 20, 40, 70, 140 }; 2753bf4d06SLudovic Tancerel /* String copy of the above const for readability purpose */ 2853bf4d06SLudovic Tancerel static const char tsys02d_show_samp_freq[] = "20 40 70 140"; 2953bf4d06SLudovic Tancerel 3053bf4d06SLudovic Tancerel static int tsys02d_read_raw(struct iio_dev *indio_dev, 3153bf4d06SLudovic Tancerel struct iio_chan_spec const *channel, int *val, 3253bf4d06SLudovic Tancerel int *val2, long mask) 3353bf4d06SLudovic Tancerel { 3453bf4d06SLudovic Tancerel int ret; 3553bf4d06SLudovic Tancerel s32 temperature; 3653bf4d06SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 3753bf4d06SLudovic Tancerel 3853bf4d06SLudovic Tancerel switch (mask) { 3953bf4d06SLudovic Tancerel case IIO_CHAN_INFO_PROCESSED: 4053bf4d06SLudovic Tancerel switch (channel->type) { 4153bf4d06SLudovic Tancerel case IIO_TEMP: /* in milli °C */ 4253bf4d06SLudovic Tancerel ret = ms_sensors_ht_read_temperature(dev_data, 4353bf4d06SLudovic Tancerel &temperature); 4453bf4d06SLudovic Tancerel if (ret) 4553bf4d06SLudovic Tancerel return ret; 4653bf4d06SLudovic Tancerel *val = temperature; 4753bf4d06SLudovic Tancerel 4853bf4d06SLudovic Tancerel return IIO_VAL_INT; 4953bf4d06SLudovic Tancerel default: 5053bf4d06SLudovic Tancerel return -EINVAL; 5153bf4d06SLudovic Tancerel } 5253bf4d06SLudovic Tancerel case IIO_CHAN_INFO_SAMP_FREQ: 5353bf4d06SLudovic Tancerel *val = tsys02d_samp_freq[dev_data->res_index]; 5453bf4d06SLudovic Tancerel 5553bf4d06SLudovic Tancerel return IIO_VAL_INT; 5653bf4d06SLudovic Tancerel default: 5753bf4d06SLudovic Tancerel return -EINVAL; 5853bf4d06SLudovic Tancerel } 5953bf4d06SLudovic Tancerel } 6053bf4d06SLudovic Tancerel 6153bf4d06SLudovic Tancerel static int tsys02d_write_raw(struct iio_dev *indio_dev, 6253bf4d06SLudovic Tancerel struct iio_chan_spec const *chan, 6353bf4d06SLudovic Tancerel int val, int val2, long mask) 6453bf4d06SLudovic Tancerel { 6553bf4d06SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 6653bf4d06SLudovic Tancerel int i, ret; 6753bf4d06SLudovic Tancerel 6853bf4d06SLudovic Tancerel switch (mask) { 6953bf4d06SLudovic Tancerel case IIO_CHAN_INFO_SAMP_FREQ: 7053bf4d06SLudovic Tancerel i = ARRAY_SIZE(tsys02d_samp_freq); 7153bf4d06SLudovic Tancerel while (i-- > 0) 7253bf4d06SLudovic Tancerel if (val == tsys02d_samp_freq[i]) 7353bf4d06SLudovic Tancerel break; 7453bf4d06SLudovic Tancerel if (i < 0) 7553bf4d06SLudovic Tancerel return -EINVAL; 7653bf4d06SLudovic Tancerel mutex_lock(&dev_data->lock); 7753bf4d06SLudovic Tancerel dev_data->res_index = i; 7853bf4d06SLudovic Tancerel ret = ms_sensors_write_resolution(dev_data, i); 7953bf4d06SLudovic Tancerel mutex_unlock(&dev_data->lock); 8053bf4d06SLudovic Tancerel 8153bf4d06SLudovic Tancerel return ret; 8253bf4d06SLudovic Tancerel default: 8353bf4d06SLudovic Tancerel return -EINVAL; 8453bf4d06SLudovic Tancerel } 8553bf4d06SLudovic Tancerel } 8653bf4d06SLudovic Tancerel 8753bf4d06SLudovic Tancerel static const struct iio_chan_spec tsys02d_channels[] = { 8853bf4d06SLudovic Tancerel { 8953bf4d06SLudovic Tancerel .type = IIO_TEMP, 9053bf4d06SLudovic Tancerel .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED), 9153bf4d06SLudovic Tancerel .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 9253bf4d06SLudovic Tancerel } 9353bf4d06SLudovic Tancerel }; 9453bf4d06SLudovic Tancerel 9553bf4d06SLudovic Tancerel static ssize_t tsys02_read_battery_low(struct device *dev, 9653bf4d06SLudovic Tancerel struct device_attribute *attr, 9753bf4d06SLudovic Tancerel char *buf) 9853bf4d06SLudovic Tancerel { 9953bf4d06SLudovic Tancerel struct iio_dev *indio_dev = dev_to_iio_dev(dev); 10053bf4d06SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 10153bf4d06SLudovic Tancerel 10253bf4d06SLudovic Tancerel return ms_sensors_show_battery_low(dev_data, buf); 10353bf4d06SLudovic Tancerel } 10453bf4d06SLudovic Tancerel 10553bf4d06SLudovic Tancerel static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(tsys02d_show_samp_freq); 10653bf4d06SLudovic Tancerel static IIO_DEVICE_ATTR(battery_low, S_IRUGO, 10753bf4d06SLudovic Tancerel tsys02_read_battery_low, NULL, 0); 10853bf4d06SLudovic Tancerel 10953bf4d06SLudovic Tancerel static struct attribute *tsys02d_attributes[] = { 11053bf4d06SLudovic Tancerel &iio_const_attr_sampling_frequency_available.dev_attr.attr, 11153bf4d06SLudovic Tancerel &iio_dev_attr_battery_low.dev_attr.attr, 11253bf4d06SLudovic Tancerel NULL, 11353bf4d06SLudovic Tancerel }; 11453bf4d06SLudovic Tancerel 11553bf4d06SLudovic Tancerel static const struct attribute_group tsys02d_attribute_group = { 11653bf4d06SLudovic Tancerel .attrs = tsys02d_attributes, 11753bf4d06SLudovic Tancerel }; 11853bf4d06SLudovic Tancerel 11953bf4d06SLudovic Tancerel static const struct iio_info tsys02d_info = { 12053bf4d06SLudovic Tancerel .read_raw = tsys02d_read_raw, 12153bf4d06SLudovic Tancerel .write_raw = tsys02d_write_raw, 12253bf4d06SLudovic Tancerel .attrs = &tsys02d_attribute_group, 12353bf4d06SLudovic Tancerel .driver_module = THIS_MODULE, 12453bf4d06SLudovic Tancerel }; 12553bf4d06SLudovic Tancerel 12653bf4d06SLudovic Tancerel static int tsys02d_probe(struct i2c_client *client, 12753bf4d06SLudovic Tancerel const struct i2c_device_id *id) 12853bf4d06SLudovic Tancerel { 12953bf4d06SLudovic Tancerel struct ms_ht_dev *dev_data; 13053bf4d06SLudovic Tancerel struct iio_dev *indio_dev; 13153bf4d06SLudovic Tancerel int ret; 13253bf4d06SLudovic Tancerel u64 serial_number; 13353bf4d06SLudovic Tancerel 13453bf4d06SLudovic Tancerel if (!i2c_check_functionality(client->adapter, 13553bf4d06SLudovic Tancerel I2C_FUNC_SMBUS_WRITE_BYTE_DATA | 13653bf4d06SLudovic Tancerel I2C_FUNC_SMBUS_WRITE_BYTE | 13753bf4d06SLudovic Tancerel I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { 13853bf4d06SLudovic Tancerel dev_err(&client->dev, 13953bf4d06SLudovic Tancerel "Adapter does not support some i2c transaction\n"); 140f8d9d3b4SMatt Ranostay return -EOPNOTSUPP; 14153bf4d06SLudovic Tancerel } 14253bf4d06SLudovic Tancerel 14353bf4d06SLudovic Tancerel indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data)); 14453bf4d06SLudovic Tancerel if (!indio_dev) 14553bf4d06SLudovic Tancerel return -ENOMEM; 14653bf4d06SLudovic Tancerel 14753bf4d06SLudovic Tancerel dev_data = iio_priv(indio_dev); 14853bf4d06SLudovic Tancerel dev_data->client = client; 14953bf4d06SLudovic Tancerel dev_data->res_index = 0; 15053bf4d06SLudovic Tancerel mutex_init(&dev_data->lock); 15153bf4d06SLudovic Tancerel 15253bf4d06SLudovic Tancerel indio_dev->info = &tsys02d_info; 15353bf4d06SLudovic Tancerel indio_dev->name = id->name; 15453bf4d06SLudovic Tancerel indio_dev->dev.parent = &client->dev; 15553bf4d06SLudovic Tancerel indio_dev->modes = INDIO_DIRECT_MODE; 15653bf4d06SLudovic Tancerel indio_dev->channels = tsys02d_channels; 15753bf4d06SLudovic Tancerel indio_dev->num_channels = ARRAY_SIZE(tsys02d_channels); 15853bf4d06SLudovic Tancerel 15953bf4d06SLudovic Tancerel i2c_set_clientdata(client, indio_dev); 16053bf4d06SLudovic Tancerel 16153bf4d06SLudovic Tancerel ret = ms_sensors_reset(client, TSYS02D_RESET, 15000); 16253bf4d06SLudovic Tancerel if (ret) 16353bf4d06SLudovic Tancerel return ret; 16453bf4d06SLudovic Tancerel 16553bf4d06SLudovic Tancerel ret = ms_sensors_read_serial(client, &serial_number); 16653bf4d06SLudovic Tancerel if (ret) 16753bf4d06SLudovic Tancerel return ret; 16853bf4d06SLudovic Tancerel dev_info(&client->dev, "Serial number : %llx", serial_number); 16953bf4d06SLudovic Tancerel 17053bf4d06SLudovic Tancerel return devm_iio_device_register(&client->dev, indio_dev); 17153bf4d06SLudovic Tancerel } 17253bf4d06SLudovic Tancerel 17353bf4d06SLudovic Tancerel static const struct i2c_device_id tsys02d_id[] = { 17453bf4d06SLudovic Tancerel {"tsys02d", 0}, 17553bf4d06SLudovic Tancerel {} 17653bf4d06SLudovic Tancerel }; 177*cdd469adSJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, tsys02d_id); 17853bf4d06SLudovic Tancerel 17953bf4d06SLudovic Tancerel static struct i2c_driver tsys02d_driver = { 18053bf4d06SLudovic Tancerel .probe = tsys02d_probe, 18153bf4d06SLudovic Tancerel .id_table = tsys02d_id, 18253bf4d06SLudovic Tancerel .driver = { 18353bf4d06SLudovic Tancerel .name = "tsys02d", 18453bf4d06SLudovic Tancerel }, 18553bf4d06SLudovic Tancerel }; 18653bf4d06SLudovic Tancerel 18753bf4d06SLudovic Tancerel module_i2c_driver(tsys02d_driver); 18853bf4d06SLudovic Tancerel 18953bf4d06SLudovic Tancerel MODULE_DESCRIPTION("Measurement-Specialties tsys02d temperature driver"); 19053bf4d06SLudovic Tancerel MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>"); 19153bf4d06SLudovic Tancerel MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>"); 19253bf4d06SLudovic Tancerel MODULE_LICENSE("GPL v2"); 193