2 * ms5637.c - Support for Measurement-Specialties ms5637 and ms8607
3 * pressure & temperature sensor
5 * Copyright (c) 2015 Measurement-Specialties
7 * Licensed under the GPL-2.
9 * (7-bit I2C slave address 0x76)
12 * http://www.meas-spec.com/downloads/MS5637-02BA03.pdf
14 * http://www.meas-spec.com/downloads/MS8607-02BA01.pdf
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/kernel.h>
20 #include <linux/stat.h>
21 #include <linux/module.h>
22 #include <linux/i2c.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/mutex.h>
27 #include "../common/ms_sensors/ms_sensors_i2c.h"
29 static const int ms5637_samp_freq
[6] = { 960, 480, 240, 120, 60, 30 };
30 /* String copy of the above const for readability purpose */
31 static const char ms5637_show_samp_freq
[] = "960 480 240 120 60 30";
33 static int ms5637_read_raw(struct iio_dev
*indio_dev
,
34 struct iio_chan_spec
const *channel
, int *val
,
39 unsigned int pressure
;
40 struct ms_tp_dev
*dev_data
= iio_priv(indio_dev
);
43 case IIO_CHAN_INFO_PROCESSED
:
44 ret
= ms_sensors_read_temp_and_pressure(dev_data
,
50 switch (channel
->type
) {
51 case IIO_TEMP
: /* in milli °C */
55 case IIO_PRESSURE
: /* in kPa */
56 *val
= pressure
/ 1000;
57 *val2
= (pressure
% 1000) * 1000;
59 return IIO_VAL_INT_PLUS_MICRO
;
63 case IIO_CHAN_INFO_SAMP_FREQ
:
64 *val
= ms5637_samp_freq
[dev_data
->res_index
];
72 static int ms5637_write_raw(struct iio_dev
*indio_dev
,
73 struct iio_chan_spec
const *chan
,
74 int val
, int val2
, long mask
)
76 struct ms_tp_dev
*dev_data
= iio_priv(indio_dev
);
80 case IIO_CHAN_INFO_SAMP_FREQ
:
81 i
= ARRAY_SIZE(ms5637_samp_freq
);
83 if (val
== ms5637_samp_freq
[i
])
87 dev_data
->res_index
= i
;
95 static const struct iio_chan_spec ms5637_channels
[] = {
98 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
99 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
102 .type
= IIO_PRESSURE
,
103 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
104 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
108 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(ms5637_show_samp_freq
);
110 static struct attribute
*ms5637_attributes
[] = {
111 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
115 static const struct attribute_group ms5637_attribute_group
= {
116 .attrs
= ms5637_attributes
,
119 static const struct iio_info ms5637_info
= {
120 .read_raw
= ms5637_read_raw
,
121 .write_raw
= ms5637_write_raw
,
122 .attrs
= &ms5637_attribute_group
,
123 .driver_module
= THIS_MODULE
,
126 static int ms5637_probe(struct i2c_client
*client
,
127 const struct i2c_device_id
*id
)
129 struct ms_tp_dev
*dev_data
;
130 struct iio_dev
*indio_dev
;
133 if (!i2c_check_functionality(client
->adapter
,
134 I2C_FUNC_SMBUS_READ_WORD_DATA
|
135 I2C_FUNC_SMBUS_WRITE_BYTE
|
136 I2C_FUNC_SMBUS_READ_I2C_BLOCK
)) {
137 dev_err(&client
->dev
,
138 "Adapter does not support some i2c transaction\n");
142 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*dev_data
));
146 dev_data
= iio_priv(indio_dev
);
147 dev_data
->client
= client
;
148 dev_data
->res_index
= 5;
149 mutex_init(&dev_data
->lock
);
151 indio_dev
->info
= &ms5637_info
;
152 indio_dev
->name
= id
->name
;
153 indio_dev
->dev
.parent
= &client
->dev
;
154 indio_dev
->modes
= INDIO_DIRECT_MODE
;
155 indio_dev
->channels
= ms5637_channels
;
156 indio_dev
->num_channels
= ARRAY_SIZE(ms5637_channels
);
158 i2c_set_clientdata(client
, indio_dev
);
160 ret
= ms_sensors_reset(client
, 0x1E, 3000);
164 ret
= ms_sensors_tp_read_prom(dev_data
);
168 return devm_iio_device_register(&client
->dev
, indio_dev
);
171 static const struct i2c_device_id ms5637_id
[] = {
173 {"ms8607-temppressure", 1},
177 static struct i2c_driver ms5637_driver
= {
178 .probe
= ms5637_probe
,
179 .id_table
= ms5637_id
,
185 module_i2c_driver(ms5637_driver
);
187 MODULE_DESCRIPTION("Measurement-Specialties ms5637 temperature & pressure driver");
188 MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
189 MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
190 MODULE_LICENSE("GPL v2");