*
* (7-bit I2C slave address 0x5a, 100KHz bus speed only!)
*
- * TODO: sleep mode, configuration EEPROM
+ * TODO: sleep mode, filter configuration
*/
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/module.h>
+#include <linux/delay.h>
#include <linux/iio/iio.h>
struct mlx90614_data {
struct i2c_client *client;
+ struct mutex lock; /* for EEPROM access only */
};
+/*
+ * Erase an address and write word.
+ * The mutex must be locked before calling.
+ */
+static s32 mlx90614_write_word(const struct i2c_client *client, u8 command,
+ u16 value)
+{
+ /*
+ * Note: The mlx90614 requires a PEC on writing but does not send us a
+ * valid PEC on reading. Hence, we cannot set I2C_CLIENT_PEC in
+ * i2c_client.flags. As a workaround, we use i2c_smbus_xfer here.
+ */
+ union i2c_smbus_data data;
+ s32 ret;
+
+ dev_dbg(&client->dev, "Writing 0x%x to address 0x%x", value, command);
+
+ data.word = 0x0000; /* erase command */
+ ret = i2c_smbus_xfer(client->adapter, client->addr,
+ client->flags | I2C_CLIENT_PEC,
+ I2C_SMBUS_WRITE, command,
+ I2C_SMBUS_WORD_DATA, &data);
+ if (ret < 0)
+ return ret;
+
+ msleep(MLX90614_TIMING_EEPROM);
+
+ data.word = value; /* actual write */
+ ret = i2c_smbus_xfer(client->adapter, client->addr,
+ client->flags | I2C_CLIENT_PEC,
+ I2C_SMBUS_WRITE, command,
+ I2C_SMBUS_WORD_DATA, &data);
+
+ msleep(MLX90614_TIMING_EEPROM);
+
+ return ret;
+}
+
static int mlx90614_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *channel, int *val,
int *val2, long mask)
case IIO_CHAN_INFO_SCALE:
*val = 20;
return IIO_VAL_INT;
+ case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/65535 / LSB */
+ mutex_lock(&data->lock);
+ ret = i2c_smbus_read_word_data(data->client,
+ MLX90614_EMISSIVITY);
+ mutex_unlock(&data->lock);
+
+ if (ret < 0)
+ return ret;
+
+ if (ret == 65535) {
+ *val = 1;
+ *val2 = 0;
+ } else {
+ *val = 0;
+ *val2 = ret * 15259; /* 1/65535 ~ 0.000015259 */
+ }
+ return IIO_VAL_INT_PLUS_NANO;
+ default:
+ return -EINVAL;
+ }
+}
+
+static int mlx90614_write_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *channel, int val,
+ int val2, long mask)
+{
+ struct mlx90614_data *data = iio_priv(indio_dev);
+ s32 ret;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/65535 / LSB */
+ if (val < 0 || val2 < 0 || val > 1 || (val == 1 && val2 != 0))
+ return -EINVAL;
+ val = val * 65535 + val2 / 15259; /* 1/65535 ~ 0.000015259 */
+
+ mutex_lock(&data->lock);
+ ret = mlx90614_write_word(data->client, MLX90614_EMISSIVITY,
+ val);
+ mutex_unlock(&data->lock);
+
+ if (ret < 0)
+ return ret;
+ return 0;
+ default:
+ return -EINVAL;
+ }
+}
+
+static int mlx90614_write_raw_get_fmt(struct iio_dev *indio_dev,
+ const struct iio_chan_spec const *channel,
+ long mask)
+{
+ switch (mask) {
+ case IIO_CHAN_INFO_CALIBEMISSIVITY:
+ return IIO_VAL_INT_PLUS_NANO;
default:
return -EINVAL;
}
.type = IIO_TEMP,
.modified = 1,
.channel2 = IIO_MOD_TEMP_OBJECT,
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+ BIT(IIO_CHAN_INFO_CALIBEMISSIVITY),
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
BIT(IIO_CHAN_INFO_SCALE),
},
.modified = 1,
.channel = 1,
.channel2 = IIO_MOD_TEMP_OBJECT,
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+ BIT(IIO_CHAN_INFO_CALIBEMISSIVITY),
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
BIT(IIO_CHAN_INFO_SCALE),
},
static const struct iio_info mlx90614_info = {
.read_raw = mlx90614_read_raw,
+ .write_raw = mlx90614_write_raw,
+ .write_raw_get_fmt = mlx90614_write_raw_get_fmt,
.driver_module = THIS_MODULE,
};
data = iio_priv(indio_dev);
i2c_set_clientdata(client, indio_dev);
data->client = client;
+ mutex_init(&data->lock);
indio_dev->dev.parent = &client->dev;
indio_dev->name = id->name;