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