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