Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * ms5637.c - Support for Measurement-Specialties MS5637, MS5805
0004  *            MS5837 and MS8607 pressure & temperature sensor
0005  *
0006  * Copyright (c) 2015 Measurement-Specialties
0007  *
0008  * (7-bit I2C slave address 0x76)
0009  *
0010  * Datasheet:
0011  *  http://www.meas-spec.com/downloads/MS5637-02BA03.pdf
0012  * Datasheet:
0013  *  http://www.meas-spec.com/downloads/MS5805-02BA01.pdf
0014  * Datasheet:
0015  *  http://www.meas-spec.com/downloads/MS5837-30BA.pdf
0016  * Datasheet:
0017  *  http://www.meas-spec.com/downloads/MS8607-02BA01.pdf
0018  */
0019 
0020 #include <linux/init.h>
0021 #include <linux/device.h>
0022 #include <linux/kernel.h>
0023 #include <linux/stat.h>
0024 #include <linux/module.h>
0025 #include <linux/mod_devicetable.h>
0026 #include <linux/i2c.h>
0027 #include <linux/iio/iio.h>
0028 #include <linux/iio/sysfs.h>
0029 #include <linux/mutex.h>
0030 
0031 #include "../common/ms_sensors/ms_sensors_i2c.h"
0032 
0033 struct ms_tp_data {
0034     const char *name;
0035     const struct ms_tp_hw_data *hw;
0036 };
0037 
0038 static const int ms5637_samp_freq[6] = { 960, 480, 240, 120, 60, 30 };
0039 
0040 static ssize_t ms5637_show_samp_freq(struct device *dev, struct device_attribute *attr, char *buf)
0041 {
0042     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0043     struct ms_tp_dev *dev_data = iio_priv(indio_dev);
0044     int i, len = 0;
0045 
0046     for (i = 0; i <= dev_data->hw->max_res_index; i++)
0047         len += sysfs_emit_at(buf, len, "%u ", ms5637_samp_freq[i]);
0048     sysfs_emit_at(buf, len - 1, "\n");
0049 
0050     return len;
0051 }
0052 
0053 static int ms5637_read_raw(struct iio_dev *indio_dev,
0054                struct iio_chan_spec const *channel, int *val,
0055                int *val2, long mask)
0056 {
0057     int ret;
0058     int temperature;
0059     unsigned int pressure;
0060     struct ms_tp_dev *dev_data = iio_priv(indio_dev);
0061 
0062     switch (mask) {
0063     case IIO_CHAN_INFO_PROCESSED:
0064         ret = ms_sensors_read_temp_and_pressure(dev_data,
0065                             &temperature,
0066                             &pressure);
0067         if (ret)
0068             return ret;
0069 
0070         switch (channel->type) {
0071         case IIO_TEMP:  /* in milli °C */
0072             *val = temperature;
0073 
0074             return IIO_VAL_INT;
0075         case IIO_PRESSURE:  /* in kPa */
0076             *val = pressure / 1000;
0077             *val2 = (pressure % 1000) * 1000;
0078 
0079             return IIO_VAL_INT_PLUS_MICRO;
0080         default:
0081             return -EINVAL;
0082         }
0083     case IIO_CHAN_INFO_SAMP_FREQ:
0084         *val = ms5637_samp_freq[dev_data->res_index];
0085 
0086         return IIO_VAL_INT;
0087     default:
0088         return -EINVAL;
0089     }
0090 }
0091 
0092 static int ms5637_write_raw(struct iio_dev *indio_dev,
0093                 struct iio_chan_spec const *chan,
0094                 int val, int val2, long mask)
0095 {
0096     struct ms_tp_dev *dev_data = iio_priv(indio_dev);
0097     int i;
0098 
0099     switch (mask) {
0100     case IIO_CHAN_INFO_SAMP_FREQ:
0101         i = ARRAY_SIZE(ms5637_samp_freq);
0102         while (i-- > 0)
0103             if (val == ms5637_samp_freq[i])
0104                 break;
0105         if (i < 0)
0106             return -EINVAL;
0107         dev_data->res_index = i;
0108 
0109         return 0;
0110     default:
0111         return -EINVAL;
0112     }
0113 }
0114 
0115 static const struct iio_chan_spec ms5637_channels[] = {
0116     {
0117         .type = IIO_TEMP,
0118         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
0119         .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
0120     },
0121     {
0122         .type = IIO_PRESSURE,
0123         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
0124         .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
0125     }
0126 };
0127 
0128 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(ms5637_show_samp_freq);
0129 
0130 static struct attribute *ms5637_attributes[] = {
0131     &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
0132     NULL,
0133 };
0134 
0135 static const struct attribute_group ms5637_attribute_group = {
0136     .attrs = ms5637_attributes,
0137 };
0138 
0139 static const struct iio_info ms5637_info = {
0140     .read_raw = ms5637_read_raw,
0141     .write_raw = ms5637_write_raw,
0142     .attrs = &ms5637_attribute_group,
0143 };
0144 
0145 static int ms5637_probe(struct i2c_client *client,
0146             const struct i2c_device_id *id)
0147 {
0148     const struct ms_tp_data *data;
0149     struct ms_tp_dev *dev_data;
0150     struct iio_dev *indio_dev;
0151     int ret;
0152 
0153     if (!i2c_check_functionality(client->adapter,
0154                      I2C_FUNC_SMBUS_READ_WORD_DATA |
0155                      I2C_FUNC_SMBUS_WRITE_BYTE |
0156                      I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
0157         dev_err(&client->dev,
0158             "Adapter does not support some i2c transaction\n");
0159         return -EOPNOTSUPP;
0160     }
0161 
0162     if (id)
0163         data = (const struct ms_tp_data *)id->driver_data;
0164     else
0165         data = device_get_match_data(&client->dev);
0166     if (!data)
0167         return -EINVAL;
0168 
0169     indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data));
0170     if (!indio_dev)
0171         return -ENOMEM;
0172 
0173     dev_data = iio_priv(indio_dev);
0174     dev_data->client = client;
0175     dev_data->res_index = data->hw->max_res_index;
0176     dev_data->hw = data->hw;
0177     mutex_init(&dev_data->lock);
0178 
0179     indio_dev->info = &ms5637_info;
0180     indio_dev->name = data->name;
0181     indio_dev->modes = INDIO_DIRECT_MODE;
0182     indio_dev->channels = ms5637_channels;
0183     indio_dev->num_channels = ARRAY_SIZE(ms5637_channels);
0184 
0185     i2c_set_clientdata(client, indio_dev);
0186 
0187     ret = ms_sensors_reset(client, 0x1E, 3000);
0188     if (ret)
0189         return ret;
0190 
0191     ret = ms_sensors_tp_read_prom(dev_data);
0192     if (ret)
0193         return ret;
0194 
0195     return devm_iio_device_register(&client->dev, indio_dev);
0196 }
0197 
0198 static const struct ms_tp_hw_data ms5637_hw_data  = {
0199     .prom_len = 7,
0200     .max_res_index = 5
0201 };
0202 
0203 static const struct ms_tp_hw_data ms5803_hw_data  = {
0204     .prom_len = 8,
0205     .max_res_index = 4
0206 };
0207 
0208 static const struct ms_tp_data ms5637_data = { .name = "ms5637", .hw = &ms5637_hw_data };
0209 
0210 static const struct ms_tp_data ms5803_data = { .name = "ms5803", .hw = &ms5803_hw_data };
0211 
0212 static const struct ms_tp_data ms5805_data = { .name = "ms5805", .hw = &ms5637_hw_data };
0213 
0214 static const struct ms_tp_data ms5837_data = { .name = "ms5837", .hw = &ms5637_hw_data };
0215 
0216 static const struct ms_tp_data ms8607_data = {
0217     .name = "ms8607-temppressure",
0218     .hw = &ms5637_hw_data,
0219 };
0220 
0221 static const struct i2c_device_id ms5637_id[] = {
0222     {"ms5637", (kernel_ulong_t)&ms5637_data },
0223     {"ms5805", (kernel_ulong_t)&ms5805_data },
0224     {"ms5837", (kernel_ulong_t)&ms5837_data },
0225     {"ms8607-temppressure", (kernel_ulong_t)&ms8607_data },
0226     {}
0227 };
0228 MODULE_DEVICE_TABLE(i2c, ms5637_id);
0229 
0230 static const struct of_device_id ms5637_of_match[] = {
0231     { .compatible = "meas,ms5637", .data = &ms5637_data },
0232     { .compatible = "meas,ms5803", .data = &ms5803_data },
0233     { .compatible = "meas,ms5805", .data = &ms5805_data },
0234     { .compatible = "meas,ms5837", .data = &ms5837_data },
0235     { .compatible = "meas,ms8607-temppressure", .data = &ms8607_data },
0236     { },
0237 };
0238 MODULE_DEVICE_TABLE(of, ms5637_of_match);
0239 
0240 static struct i2c_driver ms5637_driver = {
0241     .probe = ms5637_probe,
0242     .id_table = ms5637_id,
0243     .driver = {
0244            .name = "ms5637",
0245            .of_match_table = ms5637_of_match,
0246            },
0247 };
0248 
0249 module_i2c_driver(ms5637_driver);
0250 
0251 MODULE_DESCRIPTION("Measurement-Specialties ms5637 temperature & pressure driver");
0252 MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
0253 MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
0254 MODULE_LICENSE("GPL v2");
0255 MODULE_IMPORT_NS(IIO_MEAS_SPEC_SENSORS);