0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/init.h>
0010 #include <linux/delay.h>
0011 #include <linux/mod_devicetable.h>
0012 #include <linux/module.h>
0013 #include <linux/mutex.h>
0014 #include <linux/property.h>
0015 #include <linux/err.h>
0016 #include <linux/i2c.h>
0017
0018 #include <linux/iio/iio.h>
0019
0020 #define ATLAS_EZO_DRV_NAME "atlas-ezo-sensor"
0021 #define ATLAS_INT_TIME_IN_MS 950
0022 #define ATLAS_INT_HUM_TIME_IN_MS 350
0023
0024 enum {
0025 ATLAS_CO2_EZO,
0026 ATLAS_O2_EZO,
0027 ATLAS_HUM_EZO,
0028 };
0029
0030 struct atlas_ezo_device {
0031 const struct iio_chan_spec *channels;
0032 int num_channels;
0033 int delay;
0034 };
0035
0036 struct atlas_ezo_data {
0037 struct i2c_client *client;
0038 const struct atlas_ezo_device *chip;
0039
0040
0041 struct mutex lock;
0042
0043 u8 buffer[8];
0044 };
0045
0046 #define ATLAS_CONCENTRATION_CHANNEL(_modifier) \
0047 { \
0048 .type = IIO_CONCENTRATION, \
0049 .modified = 1,\
0050 .channel2 = _modifier, \
0051 .info_mask_separate = \
0052 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
0053 .scan_index = 0, \
0054 .scan_type = { \
0055 .sign = 'u', \
0056 .realbits = 32, \
0057 .storagebits = 32, \
0058 .endianness = IIO_CPU, \
0059 }, \
0060 }
0061
0062 static const struct iio_chan_spec atlas_co2_ezo_channels[] = {
0063 ATLAS_CONCENTRATION_CHANNEL(IIO_MOD_CO2),
0064 };
0065
0066 static const struct iio_chan_spec atlas_o2_ezo_channels[] = {
0067 ATLAS_CONCENTRATION_CHANNEL(IIO_MOD_O2),
0068 };
0069
0070 static const struct iio_chan_spec atlas_hum_ezo_channels[] = {
0071 {
0072 .type = IIO_HUMIDITYRELATIVE,
0073 .info_mask_separate =
0074 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
0075 .scan_index = 0,
0076 .scan_type = {
0077 .sign = 'u',
0078 .realbits = 32,
0079 .storagebits = 32,
0080 .endianness = IIO_CPU,
0081 },
0082 },
0083 };
0084
0085 static struct atlas_ezo_device atlas_ezo_devices[] = {
0086 [ATLAS_CO2_EZO] = {
0087 .channels = atlas_co2_ezo_channels,
0088 .num_channels = 1,
0089 .delay = ATLAS_INT_TIME_IN_MS,
0090 },
0091 [ATLAS_O2_EZO] = {
0092 .channels = atlas_o2_ezo_channels,
0093 .num_channels = 1,
0094 .delay = ATLAS_INT_TIME_IN_MS,
0095 },
0096 [ATLAS_HUM_EZO] = {
0097 .channels = atlas_hum_ezo_channels,
0098 .num_channels = 1,
0099 .delay = ATLAS_INT_HUM_TIME_IN_MS,
0100 },
0101 };
0102
0103 static void atlas_ezo_sanitize(char *buf)
0104 {
0105 char *ptr = strchr(buf, '.');
0106
0107 if (!ptr)
0108 return;
0109
0110 memmove(ptr, ptr + 1, strlen(ptr));
0111 }
0112
0113 static int atlas_ezo_read_raw(struct iio_dev *indio_dev,
0114 struct iio_chan_spec const *chan,
0115 int *val, int *val2, long mask)
0116 {
0117 struct atlas_ezo_data *data = iio_priv(indio_dev);
0118 struct i2c_client *client = data->client;
0119
0120 if (chan->type != IIO_CONCENTRATION)
0121 return -EINVAL;
0122
0123 switch (mask) {
0124 case IIO_CHAN_INFO_RAW: {
0125 int ret;
0126 long tmp;
0127
0128 mutex_lock(&data->lock);
0129
0130 tmp = i2c_smbus_write_byte(client, 'R');
0131
0132 if (tmp < 0) {
0133 mutex_unlock(&data->lock);
0134 return tmp;
0135 }
0136
0137 msleep(data->chip->delay);
0138
0139 tmp = i2c_master_recv(client, data->buffer, sizeof(data->buffer));
0140
0141 if (tmp < 0 || data->buffer[0] != 1) {
0142 mutex_unlock(&data->lock);
0143 return -EBUSY;
0144 }
0145
0146
0147 atlas_ezo_sanitize(data->buffer + 2);
0148
0149 ret = kstrtol(data->buffer + 1, 10, &tmp);
0150
0151 *val = tmp;
0152
0153 mutex_unlock(&data->lock);
0154
0155 return ret ? ret : IIO_VAL_INT;
0156 }
0157 case IIO_CHAN_INFO_SCALE:
0158 switch (chan->type) {
0159 case IIO_HUMIDITYRELATIVE:
0160 *val = 10;
0161 return IIO_VAL_INT;
0162 case IIO_CONCENTRATION:
0163 break;
0164 default:
0165 return -EINVAL;
0166 }
0167
0168
0169 switch (chan->channel2) {
0170 case IIO_MOD_CO2:
0171 *val = 0;
0172 *val2 = 100;
0173 return IIO_VAL_INT_PLUS_MICRO;
0174 case IIO_MOD_O2:
0175 *val = 100;
0176 return IIO_VAL_INT;
0177 }
0178 return -EINVAL;
0179 }
0180
0181 return 0;
0182 }
0183
0184 static const struct iio_info atlas_info = {
0185 .read_raw = atlas_ezo_read_raw,
0186 };
0187
0188 static const struct i2c_device_id atlas_ezo_id[] = {
0189 { "atlas-co2-ezo", (kernel_ulong_t)&atlas_ezo_devices[ATLAS_CO2_EZO] },
0190 { "atlas-o2-ezo", (kernel_ulong_t)&atlas_ezo_devices[ATLAS_O2_EZO] },
0191 { "atlas-hum-ezo", (kernel_ulong_t)&atlas_ezo_devices[ATLAS_HUM_EZO] },
0192 {}
0193 };
0194 MODULE_DEVICE_TABLE(i2c, atlas_ezo_id);
0195
0196 static const struct of_device_id atlas_ezo_dt_ids[] = {
0197 { .compatible = "atlas,co2-ezo", .data = &atlas_ezo_devices[ATLAS_CO2_EZO], },
0198 { .compatible = "atlas,o2-ezo", .data = &atlas_ezo_devices[ATLAS_O2_EZO], },
0199 { .compatible = "atlas,hum-ezo", .data = &atlas_ezo_devices[ATLAS_HUM_EZO], },
0200 {}
0201 };
0202 MODULE_DEVICE_TABLE(of, atlas_ezo_dt_ids);
0203
0204 static int atlas_ezo_probe(struct i2c_client *client,
0205 const struct i2c_device_id *id)
0206 {
0207 const struct atlas_ezo_device *chip;
0208 struct atlas_ezo_data *data;
0209 struct iio_dev *indio_dev;
0210
0211 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
0212 if (!indio_dev)
0213 return -ENOMEM;
0214
0215 if (dev_fwnode(&client->dev))
0216 chip = device_get_match_data(&client->dev);
0217 else
0218 chip = (const struct atlas_ezo_device *)id->driver_data;
0219 if (!chip)
0220 return -EINVAL;
0221
0222 indio_dev->info = &atlas_info;
0223 indio_dev->name = ATLAS_EZO_DRV_NAME;
0224 indio_dev->channels = chip->channels;
0225 indio_dev->num_channels = chip->num_channels;
0226 indio_dev->modes = INDIO_DIRECT_MODE;
0227
0228 data = iio_priv(indio_dev);
0229 data->client = client;
0230 data->chip = chip;
0231 mutex_init(&data->lock);
0232
0233 return devm_iio_device_register(&client->dev, indio_dev);
0234 };
0235
0236 static struct i2c_driver atlas_ezo_driver = {
0237 .driver = {
0238 .name = ATLAS_EZO_DRV_NAME,
0239 .of_match_table = atlas_ezo_dt_ids,
0240 },
0241 .probe = atlas_ezo_probe,
0242 .id_table = atlas_ezo_id,
0243 };
0244 module_i2c_driver(atlas_ezo_driver);
0245
0246 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
0247 MODULE_DESCRIPTION("Atlas Scientific EZO sensors");
0248 MODULE_LICENSE("GPL");