Commit | Line | Data |
---|---|---|
fda8d26e | 1 | // SPDX-License-Identifier: GPL-2.0-only |
64a70c65 | 2 | /* |
47146eb8 MW |
3 | * ms5637.c - Support for Measurement-Specialties MS5637, MS5805 |
4 | * MS5837 and MS8607 pressure & temperature sensor | |
64a70c65 LT |
5 | * |
6 | * Copyright (c) 2015 Measurement-Specialties | |
7 | * | |
64a70c65 LT |
8 | * (7-bit I2C slave address 0x76) |
9 | * | |
10 | * Datasheet: | |
11 | * http://www.meas-spec.com/downloads/MS5637-02BA03.pdf | |
1b75ce65 | 12 | * Datasheet: |
47146eb8 MW |
13 | * http://www.meas-spec.com/downloads/MS5805-02BA01.pdf |
14 | * Datasheet: | |
15 | * http://www.meas-spec.com/downloads/MS5837-30BA.pdf | |
16 | * Datasheet: | |
1b75ce65 | 17 | * http://www.meas-spec.com/downloads/MS8607-02BA01.pdf |
64a70c65 | 18 | */ |
1b75ce65 | 19 | |
64a70c65 LT |
20 | #include <linux/init.h> |
21 | #include <linux/device.h> | |
22 | #include <linux/kernel.h> | |
23 | #include <linux/stat.h> | |
24 | #include <linux/module.h> | |
444f5f85 | 25 | #include <linux/mod_devicetable.h> |
64a70c65 LT |
26 | #include <linux/i2c.h> |
27 | #include <linux/iio/iio.h> | |
28 | #include <linux/iio/sysfs.h> | |
29 | #include <linux/mutex.h> | |
30 | ||
31 | #include "../common/ms_sensors/ms_sensors_i2c.h" | |
32 | ||
8c125f5f AB |
33 | struct ms_tp_data { |
34 | const char *name; | |
35 | const struct ms_tp_hw_data *hw; | |
36 | }; | |
37 | ||
64a70c65 | 38 | static const int ms5637_samp_freq[6] = { 960, 480, 240, 120, 60, 30 }; |
07498719 AB |
39 | |
40 | static ssize_t ms5637_show_samp_freq(struct device *dev, struct device_attribute *attr, char *buf) | |
41 | { | |
42 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); | |
43 | struct ms_tp_dev *dev_data = iio_priv(indio_dev); | |
44 | int i, len = 0; | |
45 | ||
46 | for (i = 0; i <= dev_data->hw->max_res_index; i++) | |
47 | len += sysfs_emit_at(buf, len, "%u ", ms5637_samp_freq[i]); | |
48 | sysfs_emit_at(buf, len - 1, "\n"); | |
49 | ||
50 | return len; | |
51 | } | |
64a70c65 LT |
52 | |
53 | static int ms5637_read_raw(struct iio_dev *indio_dev, | |
54 | struct iio_chan_spec const *channel, int *val, | |
55 | int *val2, long mask) | |
56 | { | |
57 | int ret; | |
58 | int temperature; | |
59 | unsigned int pressure; | |
60 | struct ms_tp_dev *dev_data = iio_priv(indio_dev); | |
61 | ||
62 | switch (mask) { | |
63 | case IIO_CHAN_INFO_PROCESSED: | |
64 | ret = ms_sensors_read_temp_and_pressure(dev_data, | |
65 | &temperature, | |
66 | &pressure); | |
67 | if (ret) | |
68 | return ret; | |
69 | ||
70 | switch (channel->type) { | |
71 | case IIO_TEMP: /* in milli °C */ | |
72 | *val = temperature; | |
73 | ||
74 | return IIO_VAL_INT; | |
75 | case IIO_PRESSURE: /* in kPa */ | |
76 | *val = pressure / 1000; | |
77 | *val2 = (pressure % 1000) * 1000; | |
78 | ||
79 | return IIO_VAL_INT_PLUS_MICRO; | |
80 | default: | |
81 | return -EINVAL; | |
82 | } | |
83 | case IIO_CHAN_INFO_SAMP_FREQ: | |
84 | *val = ms5637_samp_freq[dev_data->res_index]; | |
85 | ||
86 | return IIO_VAL_INT; | |
87 | default: | |
88 | return -EINVAL; | |
89 | } | |
90 | } | |
91 | ||
92 | static int ms5637_write_raw(struct iio_dev *indio_dev, | |
93 | struct iio_chan_spec const *chan, | |
94 | int val, int val2, long mask) | |
95 | { | |
96 | struct ms_tp_dev *dev_data = iio_priv(indio_dev); | |
97 | int i; | |
98 | ||
99 | switch (mask) { | |
100 | case IIO_CHAN_INFO_SAMP_FREQ: | |
101 | i = ARRAY_SIZE(ms5637_samp_freq); | |
102 | while (i-- > 0) | |
103 | if (val == ms5637_samp_freq[i]) | |
104 | break; | |
105 | if (i < 0) | |
106 | return -EINVAL; | |
107 | dev_data->res_index = i; | |
108 | ||
109 | return 0; | |
110 | default: | |
111 | return -EINVAL; | |
112 | } | |
113 | } | |
114 | ||
115 | static const struct iio_chan_spec ms5637_channels[] = { | |
116 | { | |
117 | .type = IIO_TEMP, | |
118 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), | |
119 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), | |
120 | }, | |
121 | { | |
122 | .type = IIO_PRESSURE, | |
123 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), | |
124 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), | |
125 | } | |
126 | }; | |
127 | ||
07498719 | 128 | static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(ms5637_show_samp_freq); |
64a70c65 LT |
129 | |
130 | static struct attribute *ms5637_attributes[] = { | |
07498719 | 131 | &iio_dev_attr_sampling_frequency_available.dev_attr.attr, |
64a70c65 LT |
132 | NULL, |
133 | }; | |
134 | ||
135 | static const struct attribute_group ms5637_attribute_group = { | |
136 | .attrs = ms5637_attributes, | |
137 | }; | |
138 | ||
139 | static const struct iio_info ms5637_info = { | |
140 | .read_raw = ms5637_read_raw, | |
141 | .write_raw = ms5637_write_raw, | |
142 | .attrs = &ms5637_attribute_group, | |
64a70c65 LT |
143 | }; |
144 | ||
f80ccc71 | 145 | static int ms5637_probe(struct i2c_client *client) |
64a70c65 | 146 | { |
8c125f5f | 147 | const struct ms_tp_data *data; |
64a70c65 LT |
148 | struct ms_tp_dev *dev_data; |
149 | struct iio_dev *indio_dev; | |
150 | int ret; | |
151 | ||
152 | if (!i2c_check_functionality(client->adapter, | |
153 | I2C_FUNC_SMBUS_READ_WORD_DATA | | |
154 | I2C_FUNC_SMBUS_WRITE_BYTE | | |
155 | I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { | |
156 | dev_err(&client->dev, | |
157 | "Adapter does not support some i2c transaction\n"); | |
f8d9d3b4 | 158 | return -EOPNOTSUPP; |
64a70c65 LT |
159 | } |
160 | ||
22da192f | 161 | data = i2c_get_match_data(client); |
8c125f5f AB |
162 | if (!data) |
163 | return -EINVAL; | |
164 | ||
64a70c65 LT |
165 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data)); |
166 | if (!indio_dev) | |
167 | return -ENOMEM; | |
168 | ||
169 | dev_data = iio_priv(indio_dev); | |
170 | dev_data->client = client; | |
8c125f5f AB |
171 | dev_data->res_index = data->hw->max_res_index; |
172 | dev_data->hw = data->hw; | |
64a70c65 LT |
173 | mutex_init(&dev_data->lock); |
174 | ||
175 | indio_dev->info = &ms5637_info; | |
8c125f5f | 176 | indio_dev->name = data->name; |
64a70c65 LT |
177 | indio_dev->modes = INDIO_DIRECT_MODE; |
178 | indio_dev->channels = ms5637_channels; | |
179 | indio_dev->num_channels = ARRAY_SIZE(ms5637_channels); | |
180 | ||
181 | i2c_set_clientdata(client, indio_dev); | |
182 | ||
183 | ret = ms_sensors_reset(client, 0x1E, 3000); | |
184 | if (ret) | |
185 | return ret; | |
186 | ||
187 | ret = ms_sensors_tp_read_prom(dev_data); | |
188 | if (ret) | |
189 | return ret; | |
190 | ||
191 | return devm_iio_device_register(&client->dev, indio_dev); | |
192 | } | |
193 | ||
8c125f5f AB |
194 | static const struct ms_tp_hw_data ms5637_hw_data = { |
195 | .prom_len = 7, | |
196 | .max_res_index = 5 | |
197 | }; | |
198 | ||
649ef114 AB |
199 | static const struct ms_tp_hw_data ms5803_hw_data = { |
200 | .prom_len = 8, | |
201 | .max_res_index = 4 | |
202 | }; | |
203 | ||
8c125f5f AB |
204 | static const struct ms_tp_data ms5637_data = { .name = "ms5637", .hw = &ms5637_hw_data }; |
205 | ||
649ef114 AB |
206 | static const struct ms_tp_data ms5803_data = { .name = "ms5803", .hw = &ms5803_hw_data }; |
207 | ||
8c125f5f AB |
208 | static const struct ms_tp_data ms5805_data = { .name = "ms5805", .hw = &ms5637_hw_data }; |
209 | ||
210 | static const struct ms_tp_data ms5837_data = { .name = "ms5837", .hw = &ms5637_hw_data }; | |
211 | ||
212 | static const struct ms_tp_data ms8607_data = { | |
213 | .name = "ms8607-temppressure", | |
214 | .hw = &ms5637_hw_data, | |
215 | }; | |
216 | ||
64a70c65 | 217 | static const struct i2c_device_id ms5637_id[] = { |
8c125f5f AB |
218 | {"ms5637", (kernel_ulong_t)&ms5637_data }, |
219 | {"ms5805", (kernel_ulong_t)&ms5805_data }, | |
220 | {"ms5837", (kernel_ulong_t)&ms5837_data }, | |
221 | {"ms8607-temppressure", (kernel_ulong_t)&ms8607_data }, | |
64a70c65 LT |
222 | {} |
223 | }; | |
cdd469ad | 224 | MODULE_DEVICE_TABLE(i2c, ms5637_id); |
64a70c65 | 225 | |
1813c713 | 226 | static const struct of_device_id ms5637_of_match[] = { |
8c125f5f | 227 | { .compatible = "meas,ms5637", .data = &ms5637_data }, |
649ef114 | 228 | { .compatible = "meas,ms5803", .data = &ms5803_data }, |
8c125f5f AB |
229 | { .compatible = "meas,ms5805", .data = &ms5805_data }, |
230 | { .compatible = "meas,ms5837", .data = &ms5837_data }, | |
231 | { .compatible = "meas,ms8607-temppressure", .data = &ms8607_data }, | |
1813c713 MS |
232 | { }, |
233 | }; | |
234 | MODULE_DEVICE_TABLE(of, ms5637_of_match); | |
235 | ||
64a70c65 | 236 | static struct i2c_driver ms5637_driver = { |
7cf15f42 | 237 | .probe = ms5637_probe, |
64a70c65 LT |
238 | .id_table = ms5637_id, |
239 | .driver = { | |
1813c713 | 240 | .name = "ms5637", |
444f5f85 | 241 | .of_match_table = ms5637_of_match, |
64a70c65 LT |
242 | }, |
243 | }; | |
244 | ||
245 | module_i2c_driver(ms5637_driver); | |
246 | ||
247 | MODULE_DESCRIPTION("Measurement-Specialties ms5637 temperature & pressure driver"); | |
248 | MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>"); | |
249 | MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>"); | |
250 | MODULE_LICENSE("GPL v2"); | |
a7f6cecf | 251 | MODULE_IMPORT_NS(IIO_MEAS_SPEC_SENSORS); |