1 // SPDX-License-Identifier: GPL-2.0-only
3 * tsys02d.c - Support for Measurement-Specialties tsys02d temperature sensor
5 * Copyright (c) 2015 Measurement-Specialties
7 * (7-bit I2C slave address 0x40)
10 * http://www.meas-spec.com/downloads/Digital_Sensor_TSYS02D.pdf
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>
21 #include "../common/ms_sensors/ms_sensors_i2c.h"
23 #define TSYS02D_RESET 0xFE
25 static const int tsys02d_samp_freq
[4] = { 20, 40, 70, 140 };
26 /* String copy of the above const for readability purpose */
27 static const char tsys02d_show_samp_freq
[] = "20 40 70 140";
29 static int tsys02d_read_raw(struct iio_dev
*indio_dev
,
30 struct iio_chan_spec
const *channel
, int *val
,
35 struct ms_ht_dev
*dev_data
= iio_priv(indio_dev
);
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
,
51 case IIO_CHAN_INFO_SAMP_FREQ
:
52 *val
= tsys02d_samp_freq
[dev_data
->res_index
];
60 static int tsys02d_write_raw(struct iio_dev
*indio_dev
,
61 struct iio_chan_spec
const *chan
,
62 int val
, int val2
, long mask
)
64 struct ms_ht_dev
*dev_data
= iio_priv(indio_dev
);
68 case IIO_CHAN_INFO_SAMP_FREQ
:
69 i
= ARRAY_SIZE(tsys02d_samp_freq
);
71 if (val
== tsys02d_samp_freq
[i
])
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
);
86 static const struct iio_chan_spec tsys02d_channels
[] = {
89 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_PROCESSED
),
90 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
94 static ssize_t
tsys02_read_battery_low(struct device
*dev
,
95 struct device_attribute
*attr
,
98 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
99 struct ms_ht_dev
*dev_data
= iio_priv(indio_dev
);
101 return ms_sensors_show_battery_low(dev_data
, buf
);
104 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(tsys02d_show_samp_freq
);
105 static IIO_DEVICE_ATTR(battery_low
, S_IRUGO
,
106 tsys02_read_battery_low
, NULL
, 0);
108 static struct attribute
*tsys02d_attributes
[] = {
109 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
110 &iio_dev_attr_battery_low
.dev_attr
.attr
,
114 static const struct attribute_group tsys02d_attribute_group
= {
115 .attrs
= tsys02d_attributes
,
118 static const struct iio_info tsys02d_info
= {
119 .read_raw
= tsys02d_read_raw
,
120 .write_raw
= tsys02d_write_raw
,
121 .attrs
= &tsys02d_attribute_group
,
124 static int tsys02d_probe(struct i2c_client
*client
,
125 const struct i2c_device_id
*id
)
127 struct ms_ht_dev
*dev_data
;
128 struct iio_dev
*indio_dev
;
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");
141 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*dev_data
));
145 dev_data
= iio_priv(indio_dev
);
146 dev_data
->client
= client
;
147 dev_data
->res_index
= 0;
148 mutex_init(&dev_data
->lock
);
150 indio_dev
->info
= &tsys02d_info
;
151 indio_dev
->name
= id
->name
;
152 indio_dev
->modes
= INDIO_DIRECT_MODE
;
153 indio_dev
->channels
= tsys02d_channels
;
154 indio_dev
->num_channels
= ARRAY_SIZE(tsys02d_channels
);
156 i2c_set_clientdata(client
, indio_dev
);
158 ret
= ms_sensors_reset(client
, TSYS02D_RESET
, 15000);
162 ret
= ms_sensors_read_serial(client
, &serial_number
);
165 dev_info(&client
->dev
, "Serial number : %llx", serial_number
);
167 return devm_iio_device_register(&client
->dev
, indio_dev
);
170 static const struct i2c_device_id tsys02d_id
[] = {
174 MODULE_DEVICE_TABLE(i2c
, tsys02d_id
);
176 static struct i2c_driver tsys02d_driver
= {
177 .probe
= tsys02d_probe
,
178 .id_table
= tsys02d_id
,
184 module_i2c_driver(tsys02d_driver
);
186 MODULE_DESCRIPTION("Measurement-Specialties tsys02d temperature driver");
187 MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
188 MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
189 MODULE_LICENSE("GPL v2");