2 * t5403.c - Support for EPCOS T5403 pressure/temperature sensor
4 * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * (7-bit I2C slave address 0x77)
12 * TODO: end-of-conversion irq
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/delay.h>
21 #define T5403_DATA 0xf5 /* data, LSB first, 16 bit */
22 #define T5403_CALIB_DATA 0x8e /* 10 calibration coeff., LSB first, 16 bit */
23 #define T5403_SLAVE_ADDR 0x88 /* I2C slave address, 0x77 */
24 #define T5403_COMMAND 0xf1
27 #define T5403_MODE_SHIFT 3 /* conversion time: 2, 8, 16, 66 ms */
28 #define T5403_PT BIT(1) /* 0 .. pressure, 1 .. temperature measurement */
29 #define T5403_SCO BIT(0) /* start conversion */
31 #define T5403_MODE_LOW 0
32 #define T5403_MODE_STANDARD 1
33 #define T5403_MODE_HIGH 2
34 #define T5403_MODE_ULTRA_HIGH 3
36 #define T5403_I2C_MASK (~BIT(7))
37 #define T5403_I2C_ADDR 0x77
39 static const int t5403_pressure_conv_ms
[] = {2, 8, 16, 66};
42 struct i2c_client
*client
;
48 #define T5403_C_U16(i) le16_to_cpu(data->c[(i) - 1])
49 #define T5403_C(i) sign_extend32(T5403_C_U16(i), 15)
51 static int t5403_read(struct t5403_data
*data
, bool pressure
)
53 int wait_time
= 3; /* wakeup time in ms */
55 int ret
= i2c_smbus_write_byte_data(data
->client
, T5403_COMMAND
,
56 (pressure
? (data
->mode
<< T5403_MODE_SHIFT
) : T5403_PT
) |
61 wait_time
+= pressure
? t5403_pressure_conv_ms
[data
->mode
] : 2;
65 return i2c_smbus_read_word_data(data
->client
, T5403_DATA
);
68 static int t5403_comp_pressure(struct t5403_data
*data
, int *val
, int *val2
)
75 mutex_lock(&data
->lock
);
77 ret
= t5403_read(data
, false);
82 ret
= t5403_read(data
, true);
87 /* see EPCOS application note */
88 S
= T5403_C_U16(3) + (s32
) T5403_C_U16(4) * t_r
/ 0x20000 +
89 T5403_C(5) * t_r
/ 0x8000 * t_r
/ 0x80000 +
90 T5403_C(9) * t_r
/ 0x8000 * t_r
/ 0x8000 * t_r
/ 0x10000;
92 O
= T5403_C(6) * 0x4000 + T5403_C(7) * t_r
/ 8 +
93 T5403_C(8) * t_r
/ 0x8000 * t_r
/ 16 +
94 T5403_C(9) * t_r
/ 0x8000 * t_r
/ 0x10000 * t_r
;
96 X
= (S
* p_r
+ O
) / 0x4000;
98 X
+= ((X
- 75000) * (X
- 75000) / 0x10000 - 9537) *
99 T5403_C(10) / 0x10000;
102 *val2
= (X
% 1000) * 1000;
105 mutex_unlock(&data
->lock
);
109 static int t5403_comp_temp(struct t5403_data
*data
, int *val
)
114 mutex_lock(&data
->lock
);
115 ret
= t5403_read(data
, false);
120 /* see EPCOS application note */
121 *val
= ((s32
) T5403_C_U16(1) * t_r
/ 0x100 +
122 (s32
) T5403_C_U16(2) * 0x40) * 1000 / 0x10000;
125 mutex_unlock(&data
->lock
);
129 static int t5403_read_raw(struct iio_dev
*indio_dev
,
130 struct iio_chan_spec
const *chan
,
131 int *val
, int *val2
, long mask
)
133 struct t5403_data
*data
= iio_priv(indio_dev
);
137 case IIO_CHAN_INFO_PROCESSED
:
138 switch (chan
->type
) {
140 ret
= t5403_comp_pressure(data
, val
, val2
);
143 return IIO_VAL_INT_PLUS_MICRO
;
145 ret
= t5403_comp_temp(data
, val
);
152 case IIO_CHAN_INFO_INT_TIME
:
154 *val2
= t5403_pressure_conv_ms
[data
->mode
] * 1000;
155 return IIO_VAL_INT_PLUS_MICRO
;
161 static int t5403_write_raw(struct iio_dev
*indio_dev
,
162 struct iio_chan_spec
const *chan
,
163 int val
, int val2
, long mask
)
165 struct t5403_data
*data
= iio_priv(indio_dev
);
169 case IIO_CHAN_INFO_INT_TIME
:
172 for (i
= 0; i
< ARRAY_SIZE(t5403_pressure_conv_ms
); i
++)
173 if (val2
== t5403_pressure_conv_ms
[i
] * 1000) {
174 mutex_lock(&data
->lock
);
176 mutex_unlock(&data
->lock
);
185 static const struct iio_chan_spec t5403_channels
[] = {
187 .type
= IIO_PRESSURE
,
188 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
189 BIT(IIO_CHAN_INFO_INT_TIME
),
193 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
197 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.002 0.008 0.016 0.066");
199 static struct attribute
*t5403_attributes
[] = {
200 &iio_const_attr_integration_time_available
.dev_attr
.attr
,
204 static const struct attribute_group t5403_attribute_group
= {
205 .attrs
= t5403_attributes
,
208 static const struct iio_info t5403_info
= {
209 .read_raw
= &t5403_read_raw
,
210 .write_raw
= &t5403_write_raw
,
211 .attrs
= &t5403_attribute_group
,
212 .driver_module
= THIS_MODULE
,
215 static int t5403_probe(struct i2c_client
*client
,
216 const struct i2c_device_id
*id
)
218 struct t5403_data
*data
;
219 struct iio_dev
*indio_dev
;
222 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
|
223 I2C_FUNC_SMBUS_I2C_BLOCK
))
226 ret
= i2c_smbus_read_byte_data(client
, T5403_SLAVE_ADDR
);
229 if ((ret
& T5403_I2C_MASK
) != T5403_I2C_ADDR
)
232 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
236 data
= iio_priv(indio_dev
);
237 data
->client
= client
;
238 mutex_init(&data
->lock
);
240 i2c_set_clientdata(client
, indio_dev
);
241 indio_dev
->info
= &t5403_info
;
242 indio_dev
->name
= id
->name
;
243 indio_dev
->dev
.parent
= &client
->dev
;
244 indio_dev
->modes
= INDIO_DIRECT_MODE
;
245 indio_dev
->channels
= t5403_channels
;
246 indio_dev
->num_channels
= ARRAY_SIZE(t5403_channels
);
248 data
->mode
= T5403_MODE_STANDARD
;
250 ret
= i2c_smbus_read_i2c_block_data(data
->client
, T5403_CALIB_DATA
,
251 sizeof(data
->c
), (u8
*) data
->c
);
255 return devm_iio_device_register(&client
->dev
, indio_dev
);
258 static const struct i2c_device_id t5403_id
[] = {
262 MODULE_DEVICE_TABLE(i2c
, t5403_id
);
264 static struct i2c_driver t5403_driver
= {
268 .probe
= t5403_probe
,
269 .id_table
= t5403_id
,
271 module_i2c_driver(t5403_driver
);
273 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
274 MODULE_DESCRIPTION("EPCOS T5403 pressure/temperature sensor driver");
275 MODULE_LICENSE("GPL");