1 /* 2 * ms5637.c - Support for Measurement-Specialties ms5637 and ms8607 3 * pressure & temperature sensor 4 * 5 * Copyright (c) 2015 Measurement-Specialties 6 * 7 * Licensed under the GPL-2. 8 * 9 * (7-bit I2C slave address 0x76) 10 * 11 * Datasheet: 12 * http://www.meas-spec.com/downloads/MS5637-02BA03.pdf 13 * Datasheet: 14 * http://www.meas-spec.com/downloads/MS8607-02BA01.pdf 15 */ 16 17 #include <linux/init.h> 18 #include <linux/device.h> 19 #include <linux/kernel.h> 20 #include <linux/stat.h> 21 #include <linux/module.h> 22 #include <linux/i2c.h> 23 #include <linux/iio/iio.h> 24 #include <linux/iio/sysfs.h> 25 #include <linux/mutex.h> 26 27 #include "../common/ms_sensors/ms_sensors_i2c.h" 28 29 static const int ms5637_samp_freq[6] = { 960, 480, 240, 120, 60, 30 }; 30 /* String copy of the above const for readability purpose */ 31 static const char ms5637_show_samp_freq[] = "960 480 240 120 60 30"; 32 33 static int ms5637_read_raw(struct iio_dev *indio_dev, 34 struct iio_chan_spec const *channel, int *val, 35 int *val2, long mask) 36 { 37 int ret; 38 int temperature; 39 unsigned int pressure; 40 struct ms_tp_dev *dev_data = iio_priv(indio_dev); 41 42 switch (mask) { 43 case IIO_CHAN_INFO_PROCESSED: 44 ret = ms_sensors_read_temp_and_pressure(dev_data, 45 &temperature, 46 &pressure); 47 if (ret) 48 return ret; 49 50 switch (channel->type) { 51 case IIO_TEMP: /* in milli °C */ 52 *val = temperature; 53 54 return IIO_VAL_INT; 55 case IIO_PRESSURE: /* in kPa */ 56 *val = pressure / 1000; 57 *val2 = (pressure % 1000) * 1000; 58 59 return IIO_VAL_INT_PLUS_MICRO; 60 default: 61 return -EINVAL; 62 } 63 case IIO_CHAN_INFO_SAMP_FREQ: 64 *val = ms5637_samp_freq[dev_data->res_index]; 65 66 return IIO_VAL_INT; 67 default: 68 return -EINVAL; 69 } 70 } 71 72 static int ms5637_write_raw(struct iio_dev *indio_dev, 73 struct iio_chan_spec const *chan, 74 int val, int val2, long mask) 75 { 76 struct ms_tp_dev *dev_data = iio_priv(indio_dev); 77 int i; 78 79 switch (mask) { 80 case IIO_CHAN_INFO_SAMP_FREQ: 81 i = ARRAY_SIZE(ms5637_samp_freq); 82 while (i-- > 0) 83 if (val == ms5637_samp_freq[i]) 84 break; 85 if (i < 0) 86 return -EINVAL; 87 dev_data->res_index = i; 88 89 return 0; 90 default: 91 return -EINVAL; 92 } 93 } 94 95 static const struct iio_chan_spec ms5637_channels[] = { 96 { 97 .type = IIO_TEMP, 98 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 99 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 100 }, 101 { 102 .type = IIO_PRESSURE, 103 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 104 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 105 } 106 }; 107 108 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(ms5637_show_samp_freq); 109 110 static struct attribute *ms5637_attributes[] = { 111 &iio_const_attr_sampling_frequency_available.dev_attr.attr, 112 NULL, 113 }; 114 115 static const struct attribute_group ms5637_attribute_group = { 116 .attrs = ms5637_attributes, 117 }; 118 119 static const struct iio_info ms5637_info = { 120 .read_raw = ms5637_read_raw, 121 .write_raw = ms5637_write_raw, 122 .attrs = &ms5637_attribute_group, 123 .driver_module = THIS_MODULE, 124 }; 125 126 static int ms5637_probe(struct i2c_client *client, 127 const struct i2c_device_id *id) 128 { 129 struct ms_tp_dev *dev_data; 130 struct iio_dev *indio_dev; 131 int ret; 132 133 if (!i2c_check_functionality(client->adapter, 134 I2C_FUNC_SMBUS_READ_WORD_DATA | 135 I2C_FUNC_SMBUS_WRITE_BYTE | 136 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { 137 dev_err(&client->dev, 138 "Adapter does not support some i2c transaction\n"); 139 return -EOPNOTSUPP; 140 } 141 142 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data)); 143 if (!indio_dev) 144 return -ENOMEM; 145 146 dev_data = iio_priv(indio_dev); 147 dev_data->client = client; 148 dev_data->res_index = 5; 149 mutex_init(&dev_data->lock); 150 151 indio_dev->info = &ms5637_info; 152 indio_dev->name = id->name; 153 indio_dev->dev.parent = &client->dev; 154 indio_dev->modes = INDIO_DIRECT_MODE; 155 indio_dev->channels = ms5637_channels; 156 indio_dev->num_channels = ARRAY_SIZE(ms5637_channels); 157 158 i2c_set_clientdata(client, indio_dev); 159 160 ret = ms_sensors_reset(client, 0x1E, 3000); 161 if (ret) 162 return ret; 163 164 ret = ms_sensors_tp_read_prom(dev_data); 165 if (ret) 166 return ret; 167 168 return devm_iio_device_register(&client->dev, indio_dev); 169 } 170 171 static const struct i2c_device_id ms5637_id[] = { 172 {"ms5637", 0}, 173 {"ms8607-temppressure", 1}, 174 {} 175 }; 176 177 static struct i2c_driver ms5637_driver = { 178 .probe = ms5637_probe, 179 .id_table = ms5637_id, 180 .driver = { 181 .name = "ms5637" 182 }, 183 }; 184 185 module_i2c_driver(ms5637_driver); 186 187 MODULE_DESCRIPTION("Measurement-Specialties ms5637 temperature & pressure driver"); 188 MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>"); 189 MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>"); 190 MODULE_LICENSE("GPL v2"); 191