Linux Audio

Check our new training course

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