xref: /linux/drivers/iio/pressure/ms5637.c (revision fda8d26e61fc518499ddc78ae74ec1aaa89c4134)
1*fda8d26eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
264a70c65SLudovic Tancerel /*
347146eb8SMarkezana, William  * ms5637.c - Support for Measurement-Specialties MS5637, MS5805
447146eb8SMarkezana, William  *            MS5837 and MS8607 pressure & temperature sensor
564a70c65SLudovic Tancerel  *
664a70c65SLudovic Tancerel  * Copyright (c) 2015 Measurement-Specialties
764a70c65SLudovic Tancerel  *
864a70c65SLudovic Tancerel  * (7-bit I2C slave address 0x76)
964a70c65SLudovic Tancerel  *
1064a70c65SLudovic Tancerel  * Datasheet:
1164a70c65SLudovic Tancerel  *  http://www.meas-spec.com/downloads/MS5637-02BA03.pdf
121b75ce65SLudovic Tancerel  * Datasheet:
1347146eb8SMarkezana, William  *  http://www.meas-spec.com/downloads/MS5805-02BA01.pdf
1447146eb8SMarkezana, William  * Datasheet:
1547146eb8SMarkezana, William  *  http://www.meas-spec.com/downloads/MS5837-30BA.pdf
1647146eb8SMarkezana, William  * Datasheet:
171b75ce65SLudovic Tancerel  *  http://www.meas-spec.com/downloads/MS8607-02BA01.pdf
1864a70c65SLudovic Tancerel  */
191b75ce65SLudovic Tancerel 
2064a70c65SLudovic Tancerel #include <linux/init.h>
2164a70c65SLudovic Tancerel #include <linux/device.h>
2264a70c65SLudovic Tancerel #include <linux/kernel.h>
2364a70c65SLudovic Tancerel #include <linux/stat.h>
2464a70c65SLudovic Tancerel #include <linux/module.h>
2564a70c65SLudovic Tancerel #include <linux/i2c.h>
2664a70c65SLudovic Tancerel #include <linux/iio/iio.h>
2764a70c65SLudovic Tancerel #include <linux/iio/sysfs.h>
2864a70c65SLudovic Tancerel #include <linux/mutex.h>
2964a70c65SLudovic Tancerel 
3064a70c65SLudovic Tancerel #include "../common/ms_sensors/ms_sensors_i2c.h"
3164a70c65SLudovic Tancerel 
3264a70c65SLudovic Tancerel static const int ms5637_samp_freq[6] = { 960, 480, 240, 120, 60, 30 };
3364a70c65SLudovic Tancerel /* String copy of the above const for readability purpose */
3464a70c65SLudovic Tancerel static const char ms5637_show_samp_freq[] = "960 480 240 120 60 30";
3564a70c65SLudovic Tancerel 
3664a70c65SLudovic Tancerel static int ms5637_read_raw(struct iio_dev *indio_dev,
3764a70c65SLudovic Tancerel 			   struct iio_chan_spec const *channel, int *val,
3864a70c65SLudovic Tancerel 			   int *val2, long mask)
3964a70c65SLudovic Tancerel {
4064a70c65SLudovic Tancerel 	int ret;
4164a70c65SLudovic Tancerel 	int temperature;
4264a70c65SLudovic Tancerel 	unsigned int pressure;
4364a70c65SLudovic Tancerel 	struct ms_tp_dev *dev_data = iio_priv(indio_dev);
4464a70c65SLudovic Tancerel 
4564a70c65SLudovic Tancerel 	switch (mask) {
4664a70c65SLudovic Tancerel 	case IIO_CHAN_INFO_PROCESSED:
4764a70c65SLudovic Tancerel 		ret = ms_sensors_read_temp_and_pressure(dev_data,
4864a70c65SLudovic Tancerel 							&temperature,
4964a70c65SLudovic Tancerel 							&pressure);
5064a70c65SLudovic Tancerel 		if (ret)
5164a70c65SLudovic Tancerel 			return ret;
5264a70c65SLudovic Tancerel 
5364a70c65SLudovic Tancerel 		switch (channel->type) {
5464a70c65SLudovic Tancerel 		case IIO_TEMP:	/* in milli °C */
5564a70c65SLudovic Tancerel 			*val = temperature;
5664a70c65SLudovic Tancerel 
5764a70c65SLudovic Tancerel 			return IIO_VAL_INT;
5864a70c65SLudovic Tancerel 		case IIO_PRESSURE:	/* in kPa */
5964a70c65SLudovic Tancerel 			*val = pressure / 1000;
6064a70c65SLudovic Tancerel 			*val2 = (pressure % 1000) * 1000;
6164a70c65SLudovic Tancerel 
6264a70c65SLudovic Tancerel 			return IIO_VAL_INT_PLUS_MICRO;
6364a70c65SLudovic Tancerel 		default:
6464a70c65SLudovic Tancerel 			return -EINVAL;
6564a70c65SLudovic Tancerel 		}
6664a70c65SLudovic Tancerel 	case IIO_CHAN_INFO_SAMP_FREQ:
6764a70c65SLudovic Tancerel 		*val = ms5637_samp_freq[dev_data->res_index];
6864a70c65SLudovic Tancerel 
6964a70c65SLudovic Tancerel 		return IIO_VAL_INT;
7064a70c65SLudovic Tancerel 	default:
7164a70c65SLudovic Tancerel 		return -EINVAL;
7264a70c65SLudovic Tancerel 	}
7364a70c65SLudovic Tancerel }
7464a70c65SLudovic Tancerel 
7564a70c65SLudovic Tancerel static int ms5637_write_raw(struct iio_dev *indio_dev,
7664a70c65SLudovic Tancerel 			    struct iio_chan_spec const *chan,
7764a70c65SLudovic Tancerel 			    int val, int val2, long mask)
7864a70c65SLudovic Tancerel {
7964a70c65SLudovic Tancerel 	struct ms_tp_dev *dev_data = iio_priv(indio_dev);
8064a70c65SLudovic Tancerel 	int i;
8164a70c65SLudovic Tancerel 
8264a70c65SLudovic Tancerel 	switch (mask) {
8364a70c65SLudovic Tancerel 	case IIO_CHAN_INFO_SAMP_FREQ:
8464a70c65SLudovic Tancerel 		i = ARRAY_SIZE(ms5637_samp_freq);
8564a70c65SLudovic Tancerel 		while (i-- > 0)
8664a70c65SLudovic Tancerel 			if (val == ms5637_samp_freq[i])
8764a70c65SLudovic Tancerel 				break;
8864a70c65SLudovic Tancerel 		if (i < 0)
8964a70c65SLudovic Tancerel 			return -EINVAL;
9064a70c65SLudovic Tancerel 		dev_data->res_index = i;
9164a70c65SLudovic Tancerel 
9264a70c65SLudovic Tancerel 		return 0;
9364a70c65SLudovic Tancerel 	default:
9464a70c65SLudovic Tancerel 		return -EINVAL;
9564a70c65SLudovic Tancerel 	}
9664a70c65SLudovic Tancerel }
9764a70c65SLudovic Tancerel 
9864a70c65SLudovic Tancerel static const struct iio_chan_spec ms5637_channels[] = {
9964a70c65SLudovic Tancerel 	{
10064a70c65SLudovic Tancerel 		.type = IIO_TEMP,
10164a70c65SLudovic Tancerel 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
10264a70c65SLudovic Tancerel 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
10364a70c65SLudovic Tancerel 	},
10464a70c65SLudovic Tancerel 	{
10564a70c65SLudovic Tancerel 		.type = IIO_PRESSURE,
10664a70c65SLudovic Tancerel 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
10764a70c65SLudovic Tancerel 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
10864a70c65SLudovic Tancerel 	}
10964a70c65SLudovic Tancerel };
11064a70c65SLudovic Tancerel 
11164a70c65SLudovic Tancerel static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(ms5637_show_samp_freq);
11264a70c65SLudovic Tancerel 
11364a70c65SLudovic Tancerel static struct attribute *ms5637_attributes[] = {
11464a70c65SLudovic Tancerel 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
11564a70c65SLudovic Tancerel 	NULL,
11664a70c65SLudovic Tancerel };
11764a70c65SLudovic Tancerel 
11864a70c65SLudovic Tancerel static const struct attribute_group ms5637_attribute_group = {
11964a70c65SLudovic Tancerel 	.attrs = ms5637_attributes,
12064a70c65SLudovic Tancerel };
12164a70c65SLudovic Tancerel 
12264a70c65SLudovic Tancerel static const struct iio_info ms5637_info = {
12364a70c65SLudovic Tancerel 	.read_raw = ms5637_read_raw,
12464a70c65SLudovic Tancerel 	.write_raw = ms5637_write_raw,
12564a70c65SLudovic Tancerel 	.attrs = &ms5637_attribute_group,
12664a70c65SLudovic Tancerel };
12764a70c65SLudovic Tancerel 
12864a70c65SLudovic Tancerel static int ms5637_probe(struct i2c_client *client,
12964a70c65SLudovic Tancerel 			const struct i2c_device_id *id)
13064a70c65SLudovic Tancerel {
13164a70c65SLudovic Tancerel 	struct ms_tp_dev *dev_data;
13264a70c65SLudovic Tancerel 	struct iio_dev *indio_dev;
13364a70c65SLudovic Tancerel 	int ret;
13464a70c65SLudovic Tancerel 
13564a70c65SLudovic Tancerel 	if (!i2c_check_functionality(client->adapter,
13664a70c65SLudovic Tancerel 				     I2C_FUNC_SMBUS_READ_WORD_DATA |
13764a70c65SLudovic Tancerel 				     I2C_FUNC_SMBUS_WRITE_BYTE |
13864a70c65SLudovic Tancerel 				     I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
13964a70c65SLudovic Tancerel 		dev_err(&client->dev,
14064a70c65SLudovic Tancerel 			"Adapter does not support some i2c transaction\n");
141f8d9d3b4SMatt Ranostay 		return -EOPNOTSUPP;
14264a70c65SLudovic Tancerel 	}
14364a70c65SLudovic Tancerel 
14464a70c65SLudovic Tancerel 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data));
14564a70c65SLudovic Tancerel 	if (!indio_dev)
14664a70c65SLudovic Tancerel 		return -ENOMEM;
14764a70c65SLudovic Tancerel 
14864a70c65SLudovic Tancerel 	dev_data = iio_priv(indio_dev);
14964a70c65SLudovic Tancerel 	dev_data->client = client;
15064a70c65SLudovic Tancerel 	dev_data->res_index = 5;
15164a70c65SLudovic Tancerel 	mutex_init(&dev_data->lock);
15264a70c65SLudovic Tancerel 
15364a70c65SLudovic Tancerel 	indio_dev->info = &ms5637_info;
15464a70c65SLudovic Tancerel 	indio_dev->name = id->name;
15564a70c65SLudovic Tancerel 	indio_dev->dev.parent = &client->dev;
15664a70c65SLudovic Tancerel 	indio_dev->modes = INDIO_DIRECT_MODE;
15764a70c65SLudovic Tancerel 	indio_dev->channels = ms5637_channels;
15864a70c65SLudovic Tancerel 	indio_dev->num_channels = ARRAY_SIZE(ms5637_channels);
15964a70c65SLudovic Tancerel 
16064a70c65SLudovic Tancerel 	i2c_set_clientdata(client, indio_dev);
16164a70c65SLudovic Tancerel 
16264a70c65SLudovic Tancerel 	ret = ms_sensors_reset(client, 0x1E, 3000);
16364a70c65SLudovic Tancerel 	if (ret)
16464a70c65SLudovic Tancerel 		return ret;
16564a70c65SLudovic Tancerel 
16664a70c65SLudovic Tancerel 	ret = ms_sensors_tp_read_prom(dev_data);
16764a70c65SLudovic Tancerel 	if (ret)
16864a70c65SLudovic Tancerel 		return ret;
16964a70c65SLudovic Tancerel 
17064a70c65SLudovic Tancerel 	return devm_iio_device_register(&client->dev, indio_dev);
17164a70c65SLudovic Tancerel }
17264a70c65SLudovic Tancerel 
17364a70c65SLudovic Tancerel static const struct i2c_device_id ms5637_id[] = {
17464a70c65SLudovic Tancerel 	{"ms5637", 0},
17547146eb8SMarkezana, William 	{"ms5805", 0},
17647146eb8SMarkezana, William 	{"ms5837", 0},
17747146eb8SMarkezana, William 	{"ms8607-temppressure", 0},
17864a70c65SLudovic Tancerel 	{}
17964a70c65SLudovic Tancerel };
180cdd469adSJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, ms5637_id);
18164a70c65SLudovic Tancerel 
1821813c713SManivannan Sadhasivam static const struct of_device_id ms5637_of_match[] = {
1831813c713SManivannan Sadhasivam 	{ .compatible = "meas,ms5637", },
1841813c713SManivannan Sadhasivam 	{ .compatible = "meas,ms5805", },
1851813c713SManivannan Sadhasivam 	{ .compatible = "meas,ms5837", },
1861813c713SManivannan Sadhasivam 	{ .compatible = "meas,ms8607-temppressure", },
1871813c713SManivannan Sadhasivam 	{ },
1881813c713SManivannan Sadhasivam };
1891813c713SManivannan Sadhasivam MODULE_DEVICE_TABLE(of, ms5637_of_match);
1901813c713SManivannan Sadhasivam 
19164a70c65SLudovic Tancerel static struct i2c_driver ms5637_driver = {
19264a70c65SLudovic Tancerel 	.probe = ms5637_probe,
19364a70c65SLudovic Tancerel 	.id_table = ms5637_id,
19464a70c65SLudovic Tancerel 	.driver = {
1951813c713SManivannan Sadhasivam 		   .name = "ms5637",
1961813c713SManivannan Sadhasivam 		   .of_match_table = of_match_ptr(ms5637_of_match),
19764a70c65SLudovic Tancerel 		   },
19864a70c65SLudovic Tancerel };
19964a70c65SLudovic Tancerel 
20064a70c65SLudovic Tancerel module_i2c_driver(ms5637_driver);
20164a70c65SLudovic Tancerel 
20264a70c65SLudovic Tancerel MODULE_DESCRIPTION("Measurement-Specialties ms5637 temperature & pressure driver");
20364a70c65SLudovic Tancerel MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
20464a70c65SLudovic Tancerel MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
20564a70c65SLudovic Tancerel MODULE_LICENSE("GPL v2");
206