1 // SPDX-License-Identifier: GPL-2.0-only
3 * t5403.c - Support for EPCOS T5403 pressure/temperature sensor
5 * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
7 * (7-bit I2C slave address 0x77)
9 * TODO: end-of-conversion irq
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/delay.h>
18 #define T5403_DATA 0xf5 /* data, LSB first, 16 bit */
19 #define T5403_CALIB_DATA 0x8e /* 10 calibration coeff., LSB first, 16 bit */
20 #define T5403_SLAVE_ADDR 0x88 /* I2C slave address, 0x77 */
21 #define T5403_COMMAND 0xf1
24 #define T5403_MODE_SHIFT 3 /* conversion time: 2, 8, 16, 66 ms */
25 #define T5403_PT BIT(1) /* 0 .. pressure, 1 .. temperature measurement */
26 #define T5403_SCO BIT(0) /* start conversion */
28 #define T5403_MODE_LOW 0
29 #define T5403_MODE_STANDARD 1
30 #define T5403_MODE_HIGH 2
31 #define T5403_MODE_ULTRA_HIGH 3
33 #define T5403_I2C_MASK (~BIT(7))
34 #define T5403_I2C_ADDR 0x77
36 static const int t5403_pressure_conv_ms
[] = {2, 8, 16, 66};
39 struct i2c_client
*client
;
45 #define T5403_C_U16(i) le16_to_cpu(data->c[(i) - 1])
46 #define T5403_C(i) sign_extend32(T5403_C_U16(i), 15)
48 static int t5403_read(struct t5403_data
*data
, bool pressure
)
50 int wait_time
= 3; /* wakeup time in ms */
52 int ret
= i2c_smbus_write_byte_data(data
->client
, T5403_COMMAND
,
53 (pressure
? (data
->mode
<< T5403_MODE_SHIFT
) : T5403_PT
) |
58 wait_time
+= pressure
? t5403_pressure_conv_ms
[data
->mode
] : 2;
62 return i2c_smbus_read_word_data(data
->client
, T5403_DATA
);
65 static int t5403_comp_pressure(struct t5403_data
*data
, int *val
, int *val2
)
72 mutex_lock(&data
->lock
);
74 ret
= t5403_read(data
, false);
79 ret
= t5403_read(data
, true);
84 /* see EPCOS application note */
85 S
= T5403_C_U16(3) + (s32
) T5403_C_U16(4) * t_r
/ 0x20000 +
86 T5403_C(5) * t_r
/ 0x8000 * t_r
/ 0x80000 +
87 T5403_C(9) * t_r
/ 0x8000 * t_r
/ 0x8000 * t_r
/ 0x10000;
89 O
= T5403_C(6) * 0x4000 + T5403_C(7) * t_r
/ 8 +
90 T5403_C(8) * t_r
/ 0x8000 * t_r
/ 16 +
91 T5403_C(9) * t_r
/ 0x8000 * t_r
/ 0x10000 * t_r
;
93 X
= (S
* p_r
+ O
) / 0x4000;
95 X
+= ((X
- 75000) * (X
- 75000) / 0x10000 - 9537) *
96 T5403_C(10) / 0x10000;
99 *val2
= (X
% 1000) * 1000;
102 mutex_unlock(&data
->lock
);
106 static int t5403_comp_temp(struct t5403_data
*data
, int *val
)
111 mutex_lock(&data
->lock
);
112 ret
= t5403_read(data
, false);
117 /* see EPCOS application note */
118 *val
= ((s32
) T5403_C_U16(1) * t_r
/ 0x100 +
119 (s32
) T5403_C_U16(2) * 0x40) * 1000 / 0x10000;
122 mutex_unlock(&data
->lock
);
126 static int t5403_read_raw(struct iio_dev
*indio_dev
,
127 struct iio_chan_spec
const *chan
,
128 int *val
, int *val2
, long mask
)
130 struct t5403_data
*data
= iio_priv(indio_dev
);
134 case IIO_CHAN_INFO_PROCESSED
:
135 switch (chan
->type
) {
137 ret
= t5403_comp_pressure(data
, val
, val2
);
140 return IIO_VAL_INT_PLUS_MICRO
;
142 ret
= t5403_comp_temp(data
, val
);
149 case IIO_CHAN_INFO_INT_TIME
:
151 *val2
= t5403_pressure_conv_ms
[data
->mode
] * 1000;
152 return IIO_VAL_INT_PLUS_MICRO
;
158 static int t5403_write_raw(struct iio_dev
*indio_dev
,
159 struct iio_chan_spec
const *chan
,
160 int val
, int val2
, long mask
)
162 struct t5403_data
*data
= iio_priv(indio_dev
);
166 case IIO_CHAN_INFO_INT_TIME
:
169 for (i
= 0; i
< ARRAY_SIZE(t5403_pressure_conv_ms
); i
++)
170 if (val2
== t5403_pressure_conv_ms
[i
] * 1000) {
171 mutex_lock(&data
->lock
);
173 mutex_unlock(&data
->lock
);
182 static const struct iio_chan_spec t5403_channels
[] = {
184 .type
= IIO_PRESSURE
,
185 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
186 BIT(IIO_CHAN_INFO_INT_TIME
),
190 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
194 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.002 0.008 0.016 0.066");
196 static struct attribute
*t5403_attributes
[] = {
197 &iio_const_attr_integration_time_available
.dev_attr
.attr
,
201 static const struct attribute_group t5403_attribute_group
= {
202 .attrs
= t5403_attributes
,
205 static const struct iio_info t5403_info
= {
206 .read_raw
= &t5403_read_raw
,
207 .write_raw
= &t5403_write_raw
,
208 .attrs
= &t5403_attribute_group
,
211 static int t5403_probe(struct i2c_client
*client
,
212 const struct i2c_device_id
*id
)
214 struct t5403_data
*data
;
215 struct iio_dev
*indio_dev
;
218 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
|
219 I2C_FUNC_SMBUS_I2C_BLOCK
))
222 ret
= i2c_smbus_read_byte_data(client
, T5403_SLAVE_ADDR
);
225 if ((ret
& T5403_I2C_MASK
) != T5403_I2C_ADDR
)
228 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
232 data
= iio_priv(indio_dev
);
233 data
->client
= client
;
234 mutex_init(&data
->lock
);
236 i2c_set_clientdata(client
, indio_dev
);
237 indio_dev
->info
= &t5403_info
;
238 indio_dev
->name
= id
->name
;
239 indio_dev
->modes
= INDIO_DIRECT_MODE
;
240 indio_dev
->channels
= t5403_channels
;
241 indio_dev
->num_channels
= ARRAY_SIZE(t5403_channels
);
243 data
->mode
= T5403_MODE_STANDARD
;
245 ret
= i2c_smbus_read_i2c_block_data(data
->client
, T5403_CALIB_DATA
,
246 sizeof(data
->c
), (u8
*) data
->c
);
250 return devm_iio_device_register(&client
->dev
, indio_dev
);
253 static const struct i2c_device_id t5403_id
[] = {
257 MODULE_DEVICE_TABLE(i2c
, t5403_id
);
259 static struct i2c_driver t5403_driver
= {
263 .probe
= t5403_probe
,
264 .id_table
= t5403_id
,
266 module_i2c_driver(t5403_driver
);
268 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
269 MODULE_DESCRIPTION("EPCOS T5403 pressure/temperature sensor driver");
270 MODULE_LICENSE("GPL");