xref: /linux/drivers/iio/pressure/ms5637.c (revision 649ef114a0a05541f9241442fa6b9c9bef457bb4)
1fda8d26eSThomas 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>
25444f5f85SJonathan Cameron #include <linux/mod_devicetable.h>
2664a70c65SLudovic Tancerel #include <linux/i2c.h>
2764a70c65SLudovic Tancerel #include <linux/iio/iio.h>
2864a70c65SLudovic Tancerel #include <linux/iio/sysfs.h>
2964a70c65SLudovic Tancerel #include <linux/mutex.h>
3064a70c65SLudovic Tancerel 
3164a70c65SLudovic Tancerel #include "../common/ms_sensors/ms_sensors_i2c.h"
3264a70c65SLudovic Tancerel 
338c125f5fSAlexandre Belloni struct ms_tp_data {
348c125f5fSAlexandre Belloni 	const char *name;
358c125f5fSAlexandre Belloni 	const struct ms_tp_hw_data *hw;
368c125f5fSAlexandre Belloni };
378c125f5fSAlexandre Belloni 
3864a70c65SLudovic Tancerel static const int ms5637_samp_freq[6] = { 960, 480, 240, 120, 60, 30 };
3907498719SAlexandre Belloni 
4007498719SAlexandre Belloni static ssize_t ms5637_show_samp_freq(struct device *dev, struct device_attribute *attr, char *buf)
4107498719SAlexandre Belloni {
4207498719SAlexandre Belloni 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
4307498719SAlexandre Belloni 	struct ms_tp_dev *dev_data = iio_priv(indio_dev);
4407498719SAlexandre Belloni 	int i, len = 0;
4507498719SAlexandre Belloni 
4607498719SAlexandre Belloni 	for (i = 0; i <= dev_data->hw->max_res_index; i++)
4707498719SAlexandre Belloni 		len += sysfs_emit_at(buf, len, "%u ", ms5637_samp_freq[i]);
4807498719SAlexandre Belloni 	sysfs_emit_at(buf, len - 1, "\n");
4907498719SAlexandre Belloni 
5007498719SAlexandre Belloni 	return len;
5107498719SAlexandre Belloni }
5264a70c65SLudovic Tancerel 
5364a70c65SLudovic Tancerel static int ms5637_read_raw(struct iio_dev *indio_dev,
5464a70c65SLudovic Tancerel 			   struct iio_chan_spec const *channel, int *val,
5564a70c65SLudovic Tancerel 			   int *val2, long mask)
5664a70c65SLudovic Tancerel {
5764a70c65SLudovic Tancerel 	int ret;
5864a70c65SLudovic Tancerel 	int temperature;
5964a70c65SLudovic Tancerel 	unsigned int pressure;
6064a70c65SLudovic Tancerel 	struct ms_tp_dev *dev_data = iio_priv(indio_dev);
6164a70c65SLudovic Tancerel 
6264a70c65SLudovic Tancerel 	switch (mask) {
6364a70c65SLudovic Tancerel 	case IIO_CHAN_INFO_PROCESSED:
6464a70c65SLudovic Tancerel 		ret = ms_sensors_read_temp_and_pressure(dev_data,
6564a70c65SLudovic Tancerel 							&temperature,
6664a70c65SLudovic Tancerel 							&pressure);
6764a70c65SLudovic Tancerel 		if (ret)
6864a70c65SLudovic Tancerel 			return ret;
6964a70c65SLudovic Tancerel 
7064a70c65SLudovic Tancerel 		switch (channel->type) {
7164a70c65SLudovic Tancerel 		case IIO_TEMP:	/* in milli °C */
7264a70c65SLudovic Tancerel 			*val = temperature;
7364a70c65SLudovic Tancerel 
7464a70c65SLudovic Tancerel 			return IIO_VAL_INT;
7564a70c65SLudovic Tancerel 		case IIO_PRESSURE:	/* in kPa */
7664a70c65SLudovic Tancerel 			*val = pressure / 1000;
7764a70c65SLudovic Tancerel 			*val2 = (pressure % 1000) * 1000;
7864a70c65SLudovic Tancerel 
7964a70c65SLudovic Tancerel 			return IIO_VAL_INT_PLUS_MICRO;
8064a70c65SLudovic Tancerel 		default:
8164a70c65SLudovic Tancerel 			return -EINVAL;
8264a70c65SLudovic Tancerel 		}
8364a70c65SLudovic Tancerel 	case IIO_CHAN_INFO_SAMP_FREQ:
8464a70c65SLudovic Tancerel 		*val = ms5637_samp_freq[dev_data->res_index];
8564a70c65SLudovic Tancerel 
8664a70c65SLudovic Tancerel 		return IIO_VAL_INT;
8764a70c65SLudovic Tancerel 	default:
8864a70c65SLudovic Tancerel 		return -EINVAL;
8964a70c65SLudovic Tancerel 	}
9064a70c65SLudovic Tancerel }
9164a70c65SLudovic Tancerel 
9264a70c65SLudovic Tancerel static int ms5637_write_raw(struct iio_dev *indio_dev,
9364a70c65SLudovic Tancerel 			    struct iio_chan_spec const *chan,
9464a70c65SLudovic Tancerel 			    int val, int val2, long mask)
9564a70c65SLudovic Tancerel {
9664a70c65SLudovic Tancerel 	struct ms_tp_dev *dev_data = iio_priv(indio_dev);
9764a70c65SLudovic Tancerel 	int i;
9864a70c65SLudovic Tancerel 
9964a70c65SLudovic Tancerel 	switch (mask) {
10064a70c65SLudovic Tancerel 	case IIO_CHAN_INFO_SAMP_FREQ:
10164a70c65SLudovic Tancerel 		i = ARRAY_SIZE(ms5637_samp_freq);
10264a70c65SLudovic Tancerel 		while (i-- > 0)
10364a70c65SLudovic Tancerel 			if (val == ms5637_samp_freq[i])
10464a70c65SLudovic Tancerel 				break;
10564a70c65SLudovic Tancerel 		if (i < 0)
10664a70c65SLudovic Tancerel 			return -EINVAL;
10764a70c65SLudovic Tancerel 		dev_data->res_index = i;
10864a70c65SLudovic Tancerel 
10964a70c65SLudovic Tancerel 		return 0;
11064a70c65SLudovic Tancerel 	default:
11164a70c65SLudovic Tancerel 		return -EINVAL;
11264a70c65SLudovic Tancerel 	}
11364a70c65SLudovic Tancerel }
11464a70c65SLudovic Tancerel 
11564a70c65SLudovic Tancerel static const struct iio_chan_spec ms5637_channels[] = {
11664a70c65SLudovic Tancerel 	{
11764a70c65SLudovic Tancerel 		.type = IIO_TEMP,
11864a70c65SLudovic Tancerel 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
11964a70c65SLudovic Tancerel 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
12064a70c65SLudovic Tancerel 	},
12164a70c65SLudovic Tancerel 	{
12264a70c65SLudovic Tancerel 		.type = IIO_PRESSURE,
12364a70c65SLudovic Tancerel 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
12464a70c65SLudovic Tancerel 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
12564a70c65SLudovic Tancerel 	}
12664a70c65SLudovic Tancerel };
12764a70c65SLudovic Tancerel 
12807498719SAlexandre Belloni static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(ms5637_show_samp_freq);
12964a70c65SLudovic Tancerel 
13064a70c65SLudovic Tancerel static struct attribute *ms5637_attributes[] = {
13107498719SAlexandre Belloni 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
13264a70c65SLudovic Tancerel 	NULL,
13364a70c65SLudovic Tancerel };
13464a70c65SLudovic Tancerel 
13564a70c65SLudovic Tancerel static const struct attribute_group ms5637_attribute_group = {
13664a70c65SLudovic Tancerel 	.attrs = ms5637_attributes,
13764a70c65SLudovic Tancerel };
13864a70c65SLudovic Tancerel 
13964a70c65SLudovic Tancerel static const struct iio_info ms5637_info = {
14064a70c65SLudovic Tancerel 	.read_raw = ms5637_read_raw,
14164a70c65SLudovic Tancerel 	.write_raw = ms5637_write_raw,
14264a70c65SLudovic Tancerel 	.attrs = &ms5637_attribute_group,
14364a70c65SLudovic Tancerel };
14464a70c65SLudovic Tancerel 
14564a70c65SLudovic Tancerel static int ms5637_probe(struct i2c_client *client,
14664a70c65SLudovic Tancerel 			const struct i2c_device_id *id)
14764a70c65SLudovic Tancerel {
1488c125f5fSAlexandre Belloni 	const struct ms_tp_data *data;
14964a70c65SLudovic Tancerel 	struct ms_tp_dev *dev_data;
15064a70c65SLudovic Tancerel 	struct iio_dev *indio_dev;
15164a70c65SLudovic Tancerel 	int ret;
15264a70c65SLudovic Tancerel 
15364a70c65SLudovic Tancerel 	if (!i2c_check_functionality(client->adapter,
15464a70c65SLudovic Tancerel 				     I2C_FUNC_SMBUS_READ_WORD_DATA |
15564a70c65SLudovic Tancerel 				     I2C_FUNC_SMBUS_WRITE_BYTE |
15664a70c65SLudovic Tancerel 				     I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
15764a70c65SLudovic Tancerel 		dev_err(&client->dev,
15864a70c65SLudovic Tancerel 			"Adapter does not support some i2c transaction\n");
159f8d9d3b4SMatt Ranostay 		return -EOPNOTSUPP;
16064a70c65SLudovic Tancerel 	}
16164a70c65SLudovic Tancerel 
1628c125f5fSAlexandre Belloni 	if (id)
1638c125f5fSAlexandre Belloni 		data = (const struct ms_tp_data *)id->driver_data;
1648c125f5fSAlexandre Belloni 	else
1658c125f5fSAlexandre Belloni 		data = device_get_match_data(&client->dev);
1668c125f5fSAlexandre Belloni 	if (!data)
1678c125f5fSAlexandre Belloni 		return -EINVAL;
1688c125f5fSAlexandre Belloni 
16964a70c65SLudovic Tancerel 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data));
17064a70c65SLudovic Tancerel 	if (!indio_dev)
17164a70c65SLudovic Tancerel 		return -ENOMEM;
17264a70c65SLudovic Tancerel 
17364a70c65SLudovic Tancerel 	dev_data = iio_priv(indio_dev);
17464a70c65SLudovic Tancerel 	dev_data->client = client;
1758c125f5fSAlexandre Belloni 	dev_data->res_index = data->hw->max_res_index;
1768c125f5fSAlexandre Belloni 	dev_data->hw = data->hw;
17764a70c65SLudovic Tancerel 	mutex_init(&dev_data->lock);
17864a70c65SLudovic Tancerel 
17964a70c65SLudovic Tancerel 	indio_dev->info = &ms5637_info;
1808c125f5fSAlexandre Belloni 	indio_dev->name = data->name;
18164a70c65SLudovic Tancerel 	indio_dev->modes = INDIO_DIRECT_MODE;
18264a70c65SLudovic Tancerel 	indio_dev->channels = ms5637_channels;
18364a70c65SLudovic Tancerel 	indio_dev->num_channels = ARRAY_SIZE(ms5637_channels);
18464a70c65SLudovic Tancerel 
18564a70c65SLudovic Tancerel 	i2c_set_clientdata(client, indio_dev);
18664a70c65SLudovic Tancerel 
18764a70c65SLudovic Tancerel 	ret = ms_sensors_reset(client, 0x1E, 3000);
18864a70c65SLudovic Tancerel 	if (ret)
18964a70c65SLudovic Tancerel 		return ret;
19064a70c65SLudovic Tancerel 
19164a70c65SLudovic Tancerel 	ret = ms_sensors_tp_read_prom(dev_data);
19264a70c65SLudovic Tancerel 	if (ret)
19364a70c65SLudovic Tancerel 		return ret;
19464a70c65SLudovic Tancerel 
19564a70c65SLudovic Tancerel 	return devm_iio_device_register(&client->dev, indio_dev);
19664a70c65SLudovic Tancerel }
19764a70c65SLudovic Tancerel 
1988c125f5fSAlexandre Belloni static const struct ms_tp_hw_data ms5637_hw_data  = {
1998c125f5fSAlexandre Belloni 	.prom_len = 7,
2008c125f5fSAlexandre Belloni 	.max_res_index = 5
2018c125f5fSAlexandre Belloni };
2028c125f5fSAlexandre Belloni 
203*649ef114SAlexandre Belloni static const struct ms_tp_hw_data ms5803_hw_data  = {
204*649ef114SAlexandre Belloni 	.prom_len = 8,
205*649ef114SAlexandre Belloni 	.max_res_index = 4
206*649ef114SAlexandre Belloni };
207*649ef114SAlexandre Belloni 
2088c125f5fSAlexandre Belloni static const struct ms_tp_data ms5637_data = { .name = "ms5637", .hw = &ms5637_hw_data };
2098c125f5fSAlexandre Belloni 
210*649ef114SAlexandre Belloni static const struct ms_tp_data ms5803_data = { .name = "ms5803", .hw = &ms5803_hw_data };
211*649ef114SAlexandre Belloni 
2128c125f5fSAlexandre Belloni static const struct ms_tp_data ms5805_data = { .name = "ms5805", .hw = &ms5637_hw_data };
2138c125f5fSAlexandre Belloni 
2148c125f5fSAlexandre Belloni static const struct ms_tp_data ms5837_data = { .name = "ms5837", .hw = &ms5637_hw_data };
2158c125f5fSAlexandre Belloni 
2168c125f5fSAlexandre Belloni static const struct ms_tp_data ms8607_data = {
2178c125f5fSAlexandre Belloni 	.name = "ms8607-temppressure",
2188c125f5fSAlexandre Belloni 	.hw = &ms5637_hw_data,
2198c125f5fSAlexandre Belloni };
2208c125f5fSAlexandre Belloni 
22164a70c65SLudovic Tancerel static const struct i2c_device_id ms5637_id[] = {
2228c125f5fSAlexandre Belloni 	{"ms5637", (kernel_ulong_t)&ms5637_data },
2238c125f5fSAlexandre Belloni 	{"ms5805", (kernel_ulong_t)&ms5805_data },
2248c125f5fSAlexandre Belloni 	{"ms5837", (kernel_ulong_t)&ms5837_data },
2258c125f5fSAlexandre Belloni 	{"ms8607-temppressure", (kernel_ulong_t)&ms8607_data },
22664a70c65SLudovic Tancerel 	{}
22764a70c65SLudovic Tancerel };
228cdd469adSJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, ms5637_id);
22964a70c65SLudovic Tancerel 
2301813c713SManivannan Sadhasivam static const struct of_device_id ms5637_of_match[] = {
2318c125f5fSAlexandre Belloni 	{ .compatible = "meas,ms5637", .data = &ms5637_data },
232*649ef114SAlexandre Belloni 	{ .compatible = "meas,ms5803", .data = &ms5803_data },
2338c125f5fSAlexandre Belloni 	{ .compatible = "meas,ms5805", .data = &ms5805_data },
2348c125f5fSAlexandre Belloni 	{ .compatible = "meas,ms5837", .data = &ms5837_data },
2358c125f5fSAlexandre Belloni 	{ .compatible = "meas,ms8607-temppressure", .data = &ms8607_data },
2361813c713SManivannan Sadhasivam 	{ },
2371813c713SManivannan Sadhasivam };
2381813c713SManivannan Sadhasivam MODULE_DEVICE_TABLE(of, ms5637_of_match);
2391813c713SManivannan Sadhasivam 
24064a70c65SLudovic Tancerel static struct i2c_driver ms5637_driver = {
24164a70c65SLudovic Tancerel 	.probe = ms5637_probe,
24264a70c65SLudovic Tancerel 	.id_table = ms5637_id,
24364a70c65SLudovic Tancerel 	.driver = {
2441813c713SManivannan Sadhasivam 		   .name = "ms5637",
245444f5f85SJonathan Cameron 		   .of_match_table = ms5637_of_match,
24664a70c65SLudovic Tancerel 		   },
24764a70c65SLudovic Tancerel };
24864a70c65SLudovic Tancerel 
24964a70c65SLudovic Tancerel module_i2c_driver(ms5637_driver);
25064a70c65SLudovic Tancerel 
25164a70c65SLudovic Tancerel MODULE_DESCRIPTION("Measurement-Specialties ms5637 temperature & pressure driver");
25264a70c65SLudovic Tancerel MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
25364a70c65SLudovic Tancerel MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
25464a70c65SLudovic Tancerel MODULE_LICENSE("GPL v2");
255