1*53bf4d06SLudovic Tancerel /* 2*53bf4d06SLudovic Tancerel * tsys02d.c - Support for Measurement-Specialties tsys02d temperature sensor 3*53bf4d06SLudovic Tancerel * 4*53bf4d06SLudovic Tancerel * Copyright (c) 2015 Measurement-Specialties 5*53bf4d06SLudovic Tancerel * 6*53bf4d06SLudovic Tancerel * Licensed under the GPL-2. 7*53bf4d06SLudovic Tancerel * 8*53bf4d06SLudovic Tancerel * (7-bit I2C slave address 0x40) 9*53bf4d06SLudovic Tancerel * 10*53bf4d06SLudovic Tancerel * Datasheet: 11*53bf4d06SLudovic Tancerel * http://www.meas-spec.com/downloads/Digital_Sensor_TSYS02D.pdf 12*53bf4d06SLudovic Tancerel */ 13*53bf4d06SLudovic Tancerel 14*53bf4d06SLudovic Tancerel #include <linux/init.h> 15*53bf4d06SLudovic Tancerel #include <linux/device.h> 16*53bf4d06SLudovic Tancerel #include <linux/kernel.h> 17*53bf4d06SLudovic Tancerel #include <linux/stat.h> 18*53bf4d06SLudovic Tancerel #include <linux/module.h> 19*53bf4d06SLudovic Tancerel #include <linux/iio/iio.h> 20*53bf4d06SLudovic Tancerel #include <linux/iio/sysfs.h> 21*53bf4d06SLudovic Tancerel 22*53bf4d06SLudovic Tancerel #include "../common/ms_sensors/ms_sensors_i2c.h" 23*53bf4d06SLudovic Tancerel 24*53bf4d06SLudovic Tancerel #define TSYS02D_RESET 0xFE 25*53bf4d06SLudovic Tancerel 26*53bf4d06SLudovic Tancerel static const int tsys02d_samp_freq[4] = { 20, 40, 70, 140 }; 27*53bf4d06SLudovic Tancerel /* String copy of the above const for readability purpose */ 28*53bf4d06SLudovic Tancerel static const char tsys02d_show_samp_freq[] = "20 40 70 140"; 29*53bf4d06SLudovic Tancerel 30*53bf4d06SLudovic Tancerel static int tsys02d_read_raw(struct iio_dev *indio_dev, 31*53bf4d06SLudovic Tancerel struct iio_chan_spec const *channel, int *val, 32*53bf4d06SLudovic Tancerel int *val2, long mask) 33*53bf4d06SLudovic Tancerel { 34*53bf4d06SLudovic Tancerel int ret; 35*53bf4d06SLudovic Tancerel s32 temperature; 36*53bf4d06SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 37*53bf4d06SLudovic Tancerel 38*53bf4d06SLudovic Tancerel switch (mask) { 39*53bf4d06SLudovic Tancerel case IIO_CHAN_INFO_PROCESSED: 40*53bf4d06SLudovic Tancerel switch (channel->type) { 41*53bf4d06SLudovic Tancerel case IIO_TEMP: /* in milli °C */ 42*53bf4d06SLudovic Tancerel ret = ms_sensors_ht_read_temperature(dev_data, 43*53bf4d06SLudovic Tancerel &temperature); 44*53bf4d06SLudovic Tancerel if (ret) 45*53bf4d06SLudovic Tancerel return ret; 46*53bf4d06SLudovic Tancerel *val = temperature; 47*53bf4d06SLudovic Tancerel 48*53bf4d06SLudovic Tancerel return IIO_VAL_INT; 49*53bf4d06SLudovic Tancerel default: 50*53bf4d06SLudovic Tancerel return -EINVAL; 51*53bf4d06SLudovic Tancerel } 52*53bf4d06SLudovic Tancerel case IIO_CHAN_INFO_SAMP_FREQ: 53*53bf4d06SLudovic Tancerel *val = tsys02d_samp_freq[dev_data->res_index]; 54*53bf4d06SLudovic Tancerel 55*53bf4d06SLudovic Tancerel return IIO_VAL_INT; 56*53bf4d06SLudovic Tancerel default: 57*53bf4d06SLudovic Tancerel return -EINVAL; 58*53bf4d06SLudovic Tancerel } 59*53bf4d06SLudovic Tancerel } 60*53bf4d06SLudovic Tancerel 61*53bf4d06SLudovic Tancerel static int tsys02d_write_raw(struct iio_dev *indio_dev, 62*53bf4d06SLudovic Tancerel struct iio_chan_spec const *chan, 63*53bf4d06SLudovic Tancerel int val, int val2, long mask) 64*53bf4d06SLudovic Tancerel { 65*53bf4d06SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 66*53bf4d06SLudovic Tancerel int i, ret; 67*53bf4d06SLudovic Tancerel 68*53bf4d06SLudovic Tancerel switch (mask) { 69*53bf4d06SLudovic Tancerel case IIO_CHAN_INFO_SAMP_FREQ: 70*53bf4d06SLudovic Tancerel i = ARRAY_SIZE(tsys02d_samp_freq); 71*53bf4d06SLudovic Tancerel while (i-- > 0) 72*53bf4d06SLudovic Tancerel if (val == tsys02d_samp_freq[i]) 73*53bf4d06SLudovic Tancerel break; 74*53bf4d06SLudovic Tancerel if (i < 0) 75*53bf4d06SLudovic Tancerel return -EINVAL; 76*53bf4d06SLudovic Tancerel mutex_lock(&dev_data->lock); 77*53bf4d06SLudovic Tancerel dev_data->res_index = i; 78*53bf4d06SLudovic Tancerel ret = ms_sensors_write_resolution(dev_data, i); 79*53bf4d06SLudovic Tancerel mutex_unlock(&dev_data->lock); 80*53bf4d06SLudovic Tancerel 81*53bf4d06SLudovic Tancerel return ret; 82*53bf4d06SLudovic Tancerel default: 83*53bf4d06SLudovic Tancerel return -EINVAL; 84*53bf4d06SLudovic Tancerel } 85*53bf4d06SLudovic Tancerel } 86*53bf4d06SLudovic Tancerel 87*53bf4d06SLudovic Tancerel static const struct iio_chan_spec tsys02d_channels[] = { 88*53bf4d06SLudovic Tancerel { 89*53bf4d06SLudovic Tancerel .type = IIO_TEMP, 90*53bf4d06SLudovic Tancerel .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED), 91*53bf4d06SLudovic Tancerel .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 92*53bf4d06SLudovic Tancerel } 93*53bf4d06SLudovic Tancerel }; 94*53bf4d06SLudovic Tancerel 95*53bf4d06SLudovic Tancerel static ssize_t tsys02_read_battery_low(struct device *dev, 96*53bf4d06SLudovic Tancerel struct device_attribute *attr, 97*53bf4d06SLudovic Tancerel char *buf) 98*53bf4d06SLudovic Tancerel { 99*53bf4d06SLudovic Tancerel struct iio_dev *indio_dev = dev_to_iio_dev(dev); 100*53bf4d06SLudovic Tancerel struct ms_ht_dev *dev_data = iio_priv(indio_dev); 101*53bf4d06SLudovic Tancerel 102*53bf4d06SLudovic Tancerel return ms_sensors_show_battery_low(dev_data, buf); 103*53bf4d06SLudovic Tancerel } 104*53bf4d06SLudovic Tancerel 105*53bf4d06SLudovic Tancerel static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(tsys02d_show_samp_freq); 106*53bf4d06SLudovic Tancerel static IIO_DEVICE_ATTR(battery_low, S_IRUGO, 107*53bf4d06SLudovic Tancerel tsys02_read_battery_low, NULL, 0); 108*53bf4d06SLudovic Tancerel 109*53bf4d06SLudovic Tancerel static struct attribute *tsys02d_attributes[] = { 110*53bf4d06SLudovic Tancerel &iio_const_attr_sampling_frequency_available.dev_attr.attr, 111*53bf4d06SLudovic Tancerel &iio_dev_attr_battery_low.dev_attr.attr, 112*53bf4d06SLudovic Tancerel NULL, 113*53bf4d06SLudovic Tancerel }; 114*53bf4d06SLudovic Tancerel 115*53bf4d06SLudovic Tancerel static const struct attribute_group tsys02d_attribute_group = { 116*53bf4d06SLudovic Tancerel .attrs = tsys02d_attributes, 117*53bf4d06SLudovic Tancerel }; 118*53bf4d06SLudovic Tancerel 119*53bf4d06SLudovic Tancerel static const struct iio_info tsys02d_info = { 120*53bf4d06SLudovic Tancerel .read_raw = tsys02d_read_raw, 121*53bf4d06SLudovic Tancerel .write_raw = tsys02d_write_raw, 122*53bf4d06SLudovic Tancerel .attrs = &tsys02d_attribute_group, 123*53bf4d06SLudovic Tancerel .driver_module = THIS_MODULE, 124*53bf4d06SLudovic Tancerel }; 125*53bf4d06SLudovic Tancerel 126*53bf4d06SLudovic Tancerel static int tsys02d_probe(struct i2c_client *client, 127*53bf4d06SLudovic Tancerel const struct i2c_device_id *id) 128*53bf4d06SLudovic Tancerel { 129*53bf4d06SLudovic Tancerel struct ms_ht_dev *dev_data; 130*53bf4d06SLudovic Tancerel struct iio_dev *indio_dev; 131*53bf4d06SLudovic Tancerel int ret; 132*53bf4d06SLudovic Tancerel u64 serial_number; 133*53bf4d06SLudovic Tancerel 134*53bf4d06SLudovic Tancerel if (!i2c_check_functionality(client->adapter, 135*53bf4d06SLudovic Tancerel I2C_FUNC_SMBUS_WRITE_BYTE_DATA | 136*53bf4d06SLudovic Tancerel I2C_FUNC_SMBUS_WRITE_BYTE | 137*53bf4d06SLudovic Tancerel I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { 138*53bf4d06SLudovic Tancerel dev_err(&client->dev, 139*53bf4d06SLudovic Tancerel "Adapter does not support some i2c transaction\n"); 140*53bf4d06SLudovic Tancerel return -ENODEV; 141*53bf4d06SLudovic Tancerel } 142*53bf4d06SLudovic Tancerel 143*53bf4d06SLudovic Tancerel indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data)); 144*53bf4d06SLudovic Tancerel if (!indio_dev) 145*53bf4d06SLudovic Tancerel return -ENOMEM; 146*53bf4d06SLudovic Tancerel 147*53bf4d06SLudovic Tancerel dev_data = iio_priv(indio_dev); 148*53bf4d06SLudovic Tancerel dev_data->client = client; 149*53bf4d06SLudovic Tancerel dev_data->res_index = 0; 150*53bf4d06SLudovic Tancerel mutex_init(&dev_data->lock); 151*53bf4d06SLudovic Tancerel 152*53bf4d06SLudovic Tancerel indio_dev->info = &tsys02d_info; 153*53bf4d06SLudovic Tancerel indio_dev->name = id->name; 154*53bf4d06SLudovic Tancerel indio_dev->dev.parent = &client->dev; 155*53bf4d06SLudovic Tancerel indio_dev->modes = INDIO_DIRECT_MODE; 156*53bf4d06SLudovic Tancerel indio_dev->channels = tsys02d_channels; 157*53bf4d06SLudovic Tancerel indio_dev->num_channels = ARRAY_SIZE(tsys02d_channels); 158*53bf4d06SLudovic Tancerel 159*53bf4d06SLudovic Tancerel i2c_set_clientdata(client, indio_dev); 160*53bf4d06SLudovic Tancerel 161*53bf4d06SLudovic Tancerel ret = ms_sensors_reset(client, TSYS02D_RESET, 15000); 162*53bf4d06SLudovic Tancerel if (ret) 163*53bf4d06SLudovic Tancerel return ret; 164*53bf4d06SLudovic Tancerel 165*53bf4d06SLudovic Tancerel ret = ms_sensors_read_serial(client, &serial_number); 166*53bf4d06SLudovic Tancerel if (ret) 167*53bf4d06SLudovic Tancerel return ret; 168*53bf4d06SLudovic Tancerel dev_info(&client->dev, "Serial number : %llx", serial_number); 169*53bf4d06SLudovic Tancerel 170*53bf4d06SLudovic Tancerel return devm_iio_device_register(&client->dev, indio_dev); 171*53bf4d06SLudovic Tancerel } 172*53bf4d06SLudovic Tancerel 173*53bf4d06SLudovic Tancerel static const struct i2c_device_id tsys02d_id[] = { 174*53bf4d06SLudovic Tancerel {"tsys02d", 0}, 175*53bf4d06SLudovic Tancerel {} 176*53bf4d06SLudovic Tancerel }; 177*53bf4d06SLudovic Tancerel 178*53bf4d06SLudovic Tancerel static struct i2c_driver tsys02d_driver = { 179*53bf4d06SLudovic Tancerel .probe = tsys02d_probe, 180*53bf4d06SLudovic Tancerel .id_table = tsys02d_id, 181*53bf4d06SLudovic Tancerel .driver = { 182*53bf4d06SLudovic Tancerel .name = "tsys02d", 183*53bf4d06SLudovic Tancerel }, 184*53bf4d06SLudovic Tancerel }; 185*53bf4d06SLudovic Tancerel 186*53bf4d06SLudovic Tancerel module_i2c_driver(tsys02d_driver); 187*53bf4d06SLudovic Tancerel 188*53bf4d06SLudovic Tancerel MODULE_DESCRIPTION("Measurement-Specialties tsys02d temperature driver"); 189*53bf4d06SLudovic Tancerel MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>"); 190*53bf4d06SLudovic Tancerel MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>"); 191*53bf4d06SLudovic Tancerel MODULE_LICENSE("GPL v2"); 192