2 * mlx90614.c - Support for Melexis MLX90614 contactless IR temperature sensor
4 * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * Driver for the Melexis MLX90614 I2C 16-bit IR thermopile sensor
12 * (7-bit I2C slave address 0x5a, 100KHz bus speed only!)
14 * TODO: sleep mode, configuration EEPROM
17 #include <linux/err.h>
18 #include <linux/i2c.h>
19 #include <linux/module.h>
21 #include <linux/iio/iio.h>
23 #define MLX90614_OP_RAM 0x00
25 /* RAM offsets with 16-bit data, MSB first */
26 #define MLX90614_TA 0x06 /* ambient temperature */
27 #define MLX90614_TOBJ1 0x07 /* object temperature */
29 struct mlx90614_data
{
30 struct i2c_client
*client
;
33 static int mlx90614_read_raw(struct iio_dev
*indio_dev
,
34 struct iio_chan_spec
const *channel
, int *val
,
37 struct mlx90614_data
*data
= iio_priv(indio_dev
);
41 case IIO_CHAN_INFO_RAW
: /* 0.02K / LSB */
42 switch (channel
->channel2
) {
43 case IIO_MOD_TEMP_AMBIENT
:
44 ret
= i2c_smbus_read_word_data(data
->client
,
45 MLX90614_OP_RAM
| MLX90614_TA
);
49 case IIO_MOD_TEMP_OBJECT
:
50 ret
= i2c_smbus_read_word_data(data
->client
,
51 MLX90614_OP_RAM
| MLX90614_TOBJ1
);
60 case IIO_CHAN_INFO_OFFSET
:
63 return IIO_VAL_INT_PLUS_MICRO
;
64 case IIO_CHAN_INFO_SCALE
:
72 static const struct iio_chan_spec mlx90614_channels
[] = {
76 .channel2
= IIO_MOD_TEMP_AMBIENT
,
77 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
78 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
79 BIT(IIO_CHAN_INFO_SCALE
),
84 .channel2
= IIO_MOD_TEMP_OBJECT
,
85 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
86 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
87 BIT(IIO_CHAN_INFO_SCALE
),
91 static const struct iio_info mlx90614_info
= {
92 .read_raw
= mlx90614_read_raw
,
93 .driver_module
= THIS_MODULE
,
96 static int mlx90614_probe(struct i2c_client
*client
,
97 const struct i2c_device_id
*id
)
99 struct iio_dev
*indio_dev
;
100 struct mlx90614_data
*data
;
102 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
105 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
109 data
= iio_priv(indio_dev
);
110 i2c_set_clientdata(client
, indio_dev
);
111 data
->client
= client
;
113 indio_dev
->dev
.parent
= &client
->dev
;
114 indio_dev
->name
= id
->name
;
115 indio_dev
->modes
= INDIO_DIRECT_MODE
;
116 indio_dev
->info
= &mlx90614_info
;
118 indio_dev
->channels
= mlx90614_channels
;
119 indio_dev
->num_channels
= ARRAY_SIZE(mlx90614_channels
);
121 return iio_device_register(indio_dev
);
124 static int mlx90614_remove(struct i2c_client
*client
)
126 iio_device_unregister(i2c_get_clientdata(client
));
131 static const struct i2c_device_id mlx90614_id
[] = {
135 MODULE_DEVICE_TABLE(i2c
, mlx90614_id
);
137 static struct i2c_driver mlx90614_driver
= {
140 .owner
= THIS_MODULE
,
142 .probe
= mlx90614_probe
,
143 .remove
= mlx90614_remove
,
144 .id_table
= mlx90614_id
,
146 module_i2c_driver(mlx90614_driver
);
148 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
149 MODULE_DESCRIPTION("Melexis MLX90614 contactless IR temperature sensor driver");
150 MODULE_LICENSE("GPL");