Commit | Line | Data |
---|---|---|
474fe212 PM |
1 | /* |
2 | * mlx90614.c - Support for Melexis MLX90614 contactless IR temperature sensor | |
3 | * | |
4 | * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net> | |
bad4d1a0 | 5 | * Copyright (c) 2015 Essensium NV |
764589b6 | 6 | * Copyright (c) 2015 Melexis |
474fe212 PM |
7 | * |
8 | * This file is subject to the terms and conditions of version 2 of | |
9 | * the GNU General Public License. See the file COPYING in the main | |
10 | * directory of this archive for more details. | |
11 | * | |
12 | * Driver for the Melexis MLX90614 I2C 16-bit IR thermopile sensor | |
13 | * | |
14 | * (7-bit I2C slave address 0x5a, 100KHz bus speed only!) | |
15 | * | |
eb4b07da VCSM |
16 | * To wake up from sleep mode, the SDA line must be held low while SCL is high |
17 | * for at least 33ms. This is achieved with an extra GPIO that can be connected | |
18 | * directly to the SDA line. In normal operation, the GPIO is set as input and | |
19 | * will not interfere in I2C communication. While the GPIO is driven low, the | |
20 | * i2c adapter is locked since it cannot be used by other clients. The SCL line | |
21 | * always has a pull-up so we do not need an extra GPIO to drive it high. If | |
22 | * the "wakeup" GPIO is not given, power management will be disabled. | |
23 | * | |
474fe212 PM |
24 | */ |
25 | ||
26 | #include <linux/err.h> | |
27 | #include <linux/i2c.h> | |
28 | #include <linux/module.h> | |
fad65a8f | 29 | #include <linux/delay.h> |
eb4b07da VCSM |
30 | #include <linux/jiffies.h> |
31 | #include <linux/gpio/consumer.h> | |
32 | #include <linux/pm_runtime.h> | |
474fe212 PM |
33 | |
34 | #include <linux/iio/iio.h> | |
764589b6 | 35 | #include <linux/iio/sysfs.h> |
474fe212 | 36 | |
209c0069 VCSM |
37 | #define MLX90614_OP_RAM 0x00 |
38 | #define MLX90614_OP_EEPROM 0x20 | |
39 | #define MLX90614_OP_SLEEP 0xff | |
474fe212 PM |
40 | |
41 | /* RAM offsets with 16-bit data, MSB first */ | |
209c0069 VCSM |
42 | #define MLX90614_RAW1 (MLX90614_OP_RAM | 0x04) /* raw data IR channel 1 */ |
43 | #define MLX90614_RAW2 (MLX90614_OP_RAM | 0x05) /* raw data IR channel 2 */ | |
c7586584 VCSM |
44 | #define MLX90614_TA (MLX90614_OP_RAM | 0x06) /* ambient temperature */ |
45 | #define MLX90614_TOBJ1 (MLX90614_OP_RAM | 0x07) /* object 1 temperature */ | |
209c0069 VCSM |
46 | #define MLX90614_TOBJ2 (MLX90614_OP_RAM | 0x08) /* object 2 temperature */ |
47 | ||
48 | /* EEPROM offsets with 16-bit data, MSB first */ | |
49 | #define MLX90614_EMISSIVITY (MLX90614_OP_EEPROM | 0x04) /* emissivity correction coefficient */ | |
50 | #define MLX90614_CONFIG (MLX90614_OP_EEPROM | 0x05) /* configuration register */ | |
51 | ||
52 | /* Control bits in configuration register */ | |
53 | #define MLX90614_CONFIG_IIR_SHIFT 0 /* IIR coefficient */ | |
54 | #define MLX90614_CONFIG_IIR_MASK (0x7 << MLX90614_CONFIG_IIR_SHIFT) | |
55 | #define MLX90614_CONFIG_DUAL_SHIFT 6 /* single (0) or dual (1) IR sensor */ | |
56 | #define MLX90614_CONFIG_DUAL_MASK (1 << MLX90614_CONFIG_DUAL_SHIFT) | |
57 | #define MLX90614_CONFIG_FIR_SHIFT 8 /* FIR coefficient */ | |
58 | #define MLX90614_CONFIG_FIR_MASK (0x7 << MLX90614_CONFIG_FIR_SHIFT) | |
59 | #define MLX90614_CONFIG_GAIN_SHIFT 11 /* gain */ | |
60 | #define MLX90614_CONFIG_GAIN_MASK (0x7 << MLX90614_CONFIG_GAIN_SHIFT) | |
61 | ||
62 | /* Timings (in ms) */ | |
63 | #define MLX90614_TIMING_EEPROM 20 /* time for EEPROM write/erase to complete */ | |
64 | #define MLX90614_TIMING_WAKEUP 34 /* time to hold SDA low for wake-up */ | |
65 | #define MLX90614_TIMING_STARTUP 250 /* time before first data after wake-up */ | |
474fe212 | 66 | |
eb4b07da VCSM |
67 | #define MLX90614_AUTOSLEEP_DELAY 5000 /* default autosleep delay */ |
68 | ||
f9ba1ab4 CM |
69 | /* Magic constants */ |
70 | #define MLX90614_CONST_OFFSET_DEC -13657 /* decimal part of the Kelvin offset */ | |
71 | #define MLX90614_CONST_OFFSET_REM 500000 /* remainder of offset (273.15*50) */ | |
72 | #define MLX90614_CONST_SCALE 20 /* Scale in milliKelvin (0.02 * 1000) */ | |
73 | #define MLX90614_CONST_RAW_EMISSIVITY_MAX 65535 /* max value for emissivity */ | |
74 | #define MLX90614_CONST_EMISSIVITY_RESOLUTION 15259 /* 1/65535 ~ 0.000015259 */ | |
75 | ||
474fe212 PM |
76 | struct mlx90614_data { |
77 | struct i2c_client *client; | |
fad65a8f | 78 | struct mutex lock; /* for EEPROM access only */ |
eb4b07da VCSM |
79 | struct gpio_desc *wakeup_gpio; /* NULL to disable sleep/wake-up */ |
80 | unsigned long ready_timestamp; /* in jiffies */ | |
474fe212 PM |
81 | }; |
82 | ||
764589b6 CM |
83 | /* Bandwidth values for IIR filtering */ |
84 | static const int mlx90614_iir_values[] = {77, 31, 20, 15, 723, 153, 110, 86}; | |
85 | static IIO_CONST_ATTR(in_temp_object_filter_low_pass_3db_frequency_available, | |
86 | "0.15 0.20 0.31 0.77 0.86 1.10 1.53 7.23"); | |
87 | ||
88 | static struct attribute *mlx90614_attributes[] = { | |
89 | &iio_const_attr_in_temp_object_filter_low_pass_3db_frequency_available.dev_attr.attr, | |
90 | NULL, | |
91 | }; | |
92 | ||
93 | static const struct attribute_group mlx90614_attr_group = { | |
94 | .attrs = mlx90614_attributes, | |
95 | }; | |
96 | ||
fad65a8f VCSM |
97 | /* |
98 | * Erase an address and write word. | |
99 | * The mutex must be locked before calling. | |
100 | */ | |
101 | static s32 mlx90614_write_word(const struct i2c_client *client, u8 command, | |
102 | u16 value) | |
103 | { | |
104 | /* | |
105 | * Note: The mlx90614 requires a PEC on writing but does not send us a | |
106 | * valid PEC on reading. Hence, we cannot set I2C_CLIENT_PEC in | |
107 | * i2c_client.flags. As a workaround, we use i2c_smbus_xfer here. | |
108 | */ | |
109 | union i2c_smbus_data data; | |
110 | s32 ret; | |
111 | ||
112 | dev_dbg(&client->dev, "Writing 0x%x to address 0x%x", value, command); | |
113 | ||
114 | data.word = 0x0000; /* erase command */ | |
115 | ret = i2c_smbus_xfer(client->adapter, client->addr, | |
116 | client->flags | I2C_CLIENT_PEC, | |
117 | I2C_SMBUS_WRITE, command, | |
118 | I2C_SMBUS_WORD_DATA, &data); | |
119 | if (ret < 0) | |
120 | return ret; | |
121 | ||
122 | msleep(MLX90614_TIMING_EEPROM); | |
123 | ||
124 | data.word = value; /* actual write */ | |
125 | ret = i2c_smbus_xfer(client->adapter, client->addr, | |
126 | client->flags | I2C_CLIENT_PEC, | |
127 | I2C_SMBUS_WRITE, command, | |
128 | I2C_SMBUS_WORD_DATA, &data); | |
129 | ||
130 | msleep(MLX90614_TIMING_EEPROM); | |
131 | ||
132 | return ret; | |
133 | } | |
134 | ||
764589b6 CM |
135 | /* |
136 | * Find the IIR value inside mlx90614_iir_values array and return its position | |
137 | * which is equivalent to the bit value in sensor register | |
138 | */ | |
139 | static inline s32 mlx90614_iir_search(const struct i2c_client *client, | |
140 | int value) | |
141 | { | |
142 | int i; | |
143 | s32 ret; | |
144 | ||
145 | for (i = 0; i < ARRAY_SIZE(mlx90614_iir_values); ++i) { | |
146 | if (value == mlx90614_iir_values[i]) | |
147 | break; | |
148 | } | |
149 | ||
150 | if (i == ARRAY_SIZE(mlx90614_iir_values)) | |
151 | return -EINVAL; | |
152 | ||
153 | /* | |
154 | * CONFIG register values must not be changed so | |
155 | * we must read them before we actually write | |
156 | * changes | |
157 | */ | |
158 | ret = i2c_smbus_read_word_data(client, MLX90614_CONFIG); | |
159 | if (ret > 0) | |
160 | return ret; | |
161 | ||
162 | /* Write changed values */ | |
163 | ret = mlx90614_write_word(client, MLX90614_CONFIG, | |
164 | (i << MLX90614_CONFIG_IIR_SHIFT) | | |
165 | (((u16) ((0x7 << MLX90614_CONFIG_FIR_SHIFT) | | |
166 | ((u16) ret & (~((u16) MLX90614_CONFIG_FIR_MASK))))) & | |
167 | (~(u16) MLX90614_CONFIG_IIR_MASK))); | |
168 | return ret; | |
169 | } | |
170 | ||
eb4b07da VCSM |
171 | #ifdef CONFIG_PM |
172 | /* | |
173 | * If @startup is true, make sure MLX90614_TIMING_STARTUP ms have elapsed since | |
174 | * the last wake-up. This is normally only needed to get a valid temperature | |
175 | * reading. EEPROM access does not need such delay. | |
176 | * Return 0 on success, <0 on error. | |
177 | */ | |
178 | static int mlx90614_power_get(struct mlx90614_data *data, bool startup) | |
179 | { | |
180 | unsigned long now; | |
181 | ||
182 | if (!data->wakeup_gpio) | |
183 | return 0; | |
184 | ||
185 | pm_runtime_get_sync(&data->client->dev); | |
186 | ||
187 | if (startup) { | |
188 | now = jiffies; | |
189 | if (time_before(now, data->ready_timestamp) && | |
190 | msleep_interruptible(jiffies_to_msecs( | |
191 | data->ready_timestamp - now)) != 0) { | |
192 | pm_runtime_put_autosuspend(&data->client->dev); | |
193 | return -EINTR; | |
194 | } | |
195 | } | |
196 | ||
197 | return 0; | |
198 | } | |
199 | ||
200 | static void mlx90614_power_put(struct mlx90614_data *data) | |
201 | { | |
202 | if (!data->wakeup_gpio) | |
203 | return; | |
204 | ||
205 | pm_runtime_mark_last_busy(&data->client->dev); | |
206 | pm_runtime_put_autosuspend(&data->client->dev); | |
207 | } | |
208 | #else | |
209 | static inline int mlx90614_power_get(struct mlx90614_data *data, bool startup) | |
210 | { | |
211 | return 0; | |
212 | } | |
213 | ||
214 | static inline void mlx90614_power_put(struct mlx90614_data *data) | |
215 | { | |
216 | } | |
217 | #endif | |
218 | ||
474fe212 PM |
219 | static int mlx90614_read_raw(struct iio_dev *indio_dev, |
220 | struct iio_chan_spec const *channel, int *val, | |
221 | int *val2, long mask) | |
222 | { | |
223 | struct mlx90614_data *data = iio_priv(indio_dev); | |
bad4d1a0 | 224 | u8 cmd; |
474fe212 PM |
225 | s32 ret; |
226 | ||
227 | switch (mask) { | |
228 | case IIO_CHAN_INFO_RAW: /* 0.02K / LSB */ | |
229 | switch (channel->channel2) { | |
230 | case IIO_MOD_TEMP_AMBIENT: | |
bad4d1a0 | 231 | cmd = MLX90614_TA; |
474fe212 PM |
232 | break; |
233 | case IIO_MOD_TEMP_OBJECT: | |
bad4d1a0 VCSM |
234 | switch (channel->channel) { |
235 | case 0: | |
236 | cmd = MLX90614_TOBJ1; | |
237 | break; | |
238 | case 1: | |
239 | cmd = MLX90614_TOBJ2; | |
240 | break; | |
241 | default: | |
242 | return -EINVAL; | |
243 | } | |
474fe212 PM |
244 | break; |
245 | default: | |
246 | return -EINVAL; | |
247 | } | |
bad4d1a0 | 248 | |
eb4b07da VCSM |
249 | ret = mlx90614_power_get(data, true); |
250 | if (ret < 0) | |
251 | return ret; | |
bad4d1a0 | 252 | ret = i2c_smbus_read_word_data(data->client, cmd); |
eb4b07da VCSM |
253 | mlx90614_power_put(data); |
254 | ||
bad4d1a0 VCSM |
255 | if (ret < 0) |
256 | return ret; | |
d02e0f8f VCSM |
257 | |
258 | /* MSB is an error flag */ | |
259 | if (ret & 0x8000) | |
260 | return -EIO; | |
261 | ||
474fe212 PM |
262 | *val = ret; |
263 | return IIO_VAL_INT; | |
264 | case IIO_CHAN_INFO_OFFSET: | |
f9ba1ab4 CM |
265 | *val = MLX90614_CONST_OFFSET_DEC; |
266 | *val2 = MLX90614_CONST_OFFSET_REM; | |
474fe212 PM |
267 | return IIO_VAL_INT_PLUS_MICRO; |
268 | case IIO_CHAN_INFO_SCALE: | |
f9ba1ab4 | 269 | *val = MLX90614_CONST_SCALE; |
474fe212 | 270 | return IIO_VAL_INT; |
fad65a8f | 271 | case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/65535 / LSB */ |
eb4b07da | 272 | mlx90614_power_get(data, false); |
fad65a8f VCSM |
273 | mutex_lock(&data->lock); |
274 | ret = i2c_smbus_read_word_data(data->client, | |
275 | MLX90614_EMISSIVITY); | |
276 | mutex_unlock(&data->lock); | |
eb4b07da | 277 | mlx90614_power_put(data); |
fad65a8f VCSM |
278 | |
279 | if (ret < 0) | |
280 | return ret; | |
281 | ||
f9ba1ab4 | 282 | if (ret == MLX90614_CONST_RAW_EMISSIVITY_MAX) { |
fad65a8f VCSM |
283 | *val = 1; |
284 | *val2 = 0; | |
285 | } else { | |
286 | *val = 0; | |
f9ba1ab4 | 287 | *val2 = ret * MLX90614_CONST_EMISSIVITY_RESOLUTION; |
fad65a8f VCSM |
288 | } |
289 | return IIO_VAL_INT_PLUS_NANO; | |
764589b6 CM |
290 | case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: /* IIR setting with |
291 | FIR = 1024 */ | |
292 | mlx90614_power_get(data, false); | |
293 | mutex_lock(&data->lock); | |
294 | ret = i2c_smbus_read_word_data(data->client, MLX90614_CONFIG); | |
295 | mutex_unlock(&data->lock); | |
296 | mlx90614_power_put(data); | |
297 | ||
298 | if (ret < 0) | |
299 | return ret; | |
300 | ||
301 | *val = mlx90614_iir_values[ret & MLX90614_CONFIG_IIR_MASK] / 100; | |
302 | *val2 = (mlx90614_iir_values[ret & MLX90614_CONFIG_IIR_MASK] % 100) * | |
303 | 10000; | |
304 | return IIO_VAL_INT_PLUS_MICRO; | |
fad65a8f VCSM |
305 | default: |
306 | return -EINVAL; | |
307 | } | |
308 | } | |
309 | ||
310 | static int mlx90614_write_raw(struct iio_dev *indio_dev, | |
311 | struct iio_chan_spec const *channel, int val, | |
312 | int val2, long mask) | |
313 | { | |
314 | struct mlx90614_data *data = iio_priv(indio_dev); | |
315 | s32 ret; | |
316 | ||
317 | switch (mask) { | |
318 | case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/65535 / LSB */ | |
319 | if (val < 0 || val2 < 0 || val > 1 || (val == 1 && val2 != 0)) | |
320 | return -EINVAL; | |
f9ba1ab4 CM |
321 | val = val * MLX90614_CONST_RAW_EMISSIVITY_MAX + |
322 | val2 / MLX90614_CONST_EMISSIVITY_RESOLUTION; | |
fad65a8f | 323 | |
eb4b07da | 324 | mlx90614_power_get(data, false); |
fad65a8f VCSM |
325 | mutex_lock(&data->lock); |
326 | ret = mlx90614_write_word(data->client, MLX90614_EMISSIVITY, | |
327 | val); | |
328 | mutex_unlock(&data->lock); | |
eb4b07da | 329 | mlx90614_power_put(data); |
fad65a8f | 330 | |
764589b6 CM |
331 | return ret; |
332 | case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: /* IIR Filter setting */ | |
333 | if (val < 0 || val2 < 0) | |
334 | return -EINVAL; | |
335 | ||
336 | mlx90614_power_get(data, false); | |
337 | mutex_lock(&data->lock); | |
338 | ret = mlx90614_iir_search(data->client, | |
339 | val * 100 + val2 / 10000); | |
340 | mutex_unlock(&data->lock); | |
341 | mlx90614_power_put(data); | |
342 | ||
f2c714a0 | 343 | return ret; |
fad65a8f VCSM |
344 | default: |
345 | return -EINVAL; | |
346 | } | |
347 | } | |
348 | ||
349 | static int mlx90614_write_raw_get_fmt(struct iio_dev *indio_dev, | |
6069f47f | 350 | struct iio_chan_spec const *channel, |
fad65a8f VCSM |
351 | long mask) |
352 | { | |
353 | switch (mask) { | |
354 | case IIO_CHAN_INFO_CALIBEMISSIVITY: | |
355 | return IIO_VAL_INT_PLUS_NANO; | |
764589b6 CM |
356 | case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: |
357 | return IIO_VAL_INT_PLUS_MICRO; | |
474fe212 PM |
358 | default: |
359 | return -EINVAL; | |
360 | } | |
361 | } | |
362 | ||
363 | static const struct iio_chan_spec mlx90614_channels[] = { | |
364 | { | |
365 | .type = IIO_TEMP, | |
366 | .modified = 1, | |
367 | .channel2 = IIO_MOD_TEMP_AMBIENT, | |
368 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), | |
369 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | | |
370 | BIT(IIO_CHAN_INFO_SCALE), | |
371 | }, | |
372 | { | |
373 | .type = IIO_TEMP, | |
374 | .modified = 1, | |
375 | .channel2 = IIO_MOD_TEMP_OBJECT, | |
fad65a8f | 376 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
764589b6 CM |
377 | BIT(IIO_CHAN_INFO_CALIBEMISSIVITY) | |
378 | BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), | |
474fe212 PM |
379 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | |
380 | BIT(IIO_CHAN_INFO_SCALE), | |
381 | }, | |
bad4d1a0 VCSM |
382 | { |
383 | .type = IIO_TEMP, | |
384 | .indexed = 1, | |
385 | .modified = 1, | |
386 | .channel = 1, | |
387 | .channel2 = IIO_MOD_TEMP_OBJECT, | |
fad65a8f | 388 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
764589b6 CM |
389 | BIT(IIO_CHAN_INFO_CALIBEMISSIVITY) | |
390 | BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), | |
bad4d1a0 VCSM |
391 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | |
392 | BIT(IIO_CHAN_INFO_SCALE), | |
393 | }, | |
474fe212 PM |
394 | }; |
395 | ||
396 | static const struct iio_info mlx90614_info = { | |
397 | .read_raw = mlx90614_read_raw, | |
fad65a8f VCSM |
398 | .write_raw = mlx90614_write_raw, |
399 | .write_raw_get_fmt = mlx90614_write_raw_get_fmt, | |
764589b6 | 400 | .attrs = &mlx90614_attr_group, |
474fe212 PM |
401 | .driver_module = THIS_MODULE, |
402 | }; | |
403 | ||
eb4b07da VCSM |
404 | #ifdef CONFIG_PM |
405 | static int mlx90614_sleep(struct mlx90614_data *data) | |
406 | { | |
407 | s32 ret; | |
408 | ||
409 | if (!data->wakeup_gpio) { | |
410 | dev_dbg(&data->client->dev, "Sleep disabled"); | |
411 | return -ENOSYS; | |
412 | } | |
413 | ||
414 | dev_dbg(&data->client->dev, "Requesting sleep"); | |
415 | ||
416 | mutex_lock(&data->lock); | |
417 | ret = i2c_smbus_xfer(data->client->adapter, data->client->addr, | |
418 | data->client->flags | I2C_CLIENT_PEC, | |
419 | I2C_SMBUS_WRITE, MLX90614_OP_SLEEP, | |
420 | I2C_SMBUS_BYTE, NULL); | |
421 | mutex_unlock(&data->lock); | |
422 | ||
423 | return ret; | |
424 | } | |
425 | ||
426 | static int mlx90614_wakeup(struct mlx90614_data *data) | |
427 | { | |
428 | if (!data->wakeup_gpio) { | |
429 | dev_dbg(&data->client->dev, "Wake-up disabled"); | |
430 | return -ENOSYS; | |
431 | } | |
432 | ||
433 | dev_dbg(&data->client->dev, "Requesting wake-up"); | |
434 | ||
435 | i2c_lock_adapter(data->client->adapter); | |
436 | gpiod_direction_output(data->wakeup_gpio, 0); | |
437 | msleep(MLX90614_TIMING_WAKEUP); | |
438 | gpiod_direction_input(data->wakeup_gpio); | |
439 | i2c_unlock_adapter(data->client->adapter); | |
440 | ||
441 | data->ready_timestamp = jiffies + | |
442 | msecs_to_jiffies(MLX90614_TIMING_STARTUP); | |
443 | ||
444 | /* | |
445 | * Quirk: the i2c controller may get confused right after the | |
446 | * wake-up signal has been sent. As a workaround, do a dummy read. | |
447 | * If the read fails, the controller will probably be reset so that | |
448 | * further reads will work. | |
449 | */ | |
450 | i2c_smbus_read_word_data(data->client, MLX90614_CONFIG); | |
451 | ||
452 | return 0; | |
453 | } | |
454 | ||
455 | /* Return wake-up GPIO or NULL if sleep functionality should be disabled. */ | |
456 | static struct gpio_desc *mlx90614_probe_wakeup(struct i2c_client *client) | |
457 | { | |
458 | struct gpio_desc *gpio; | |
459 | ||
460 | if (!i2c_check_functionality(client->adapter, | |
461 | I2C_FUNC_SMBUS_WRITE_BYTE)) { | |
462 | dev_info(&client->dev, | |
463 | "i2c adapter does not support SMBUS_WRITE_BYTE, sleep disabled"); | |
464 | return NULL; | |
465 | } | |
466 | ||
467 | gpio = devm_gpiod_get_optional(&client->dev, "wakeup", GPIOD_IN); | |
468 | ||
469 | if (IS_ERR(gpio)) { | |
470 | dev_warn(&client->dev, | |
471 | "gpio acquisition failed with error %ld, sleep disabled", | |
472 | PTR_ERR(gpio)); | |
473 | return NULL; | |
474 | } else if (!gpio) { | |
475 | dev_info(&client->dev, | |
476 | "wakeup-gpio not found, sleep disabled"); | |
477 | } | |
478 | ||
479 | return gpio; | |
480 | } | |
481 | #else | |
482 | static inline int mlx90614_sleep(struct mlx90614_data *data) | |
483 | { | |
484 | return -ENOSYS; | |
485 | } | |
486 | static inline int mlx90614_wakeup(struct mlx90614_data *data) | |
487 | { | |
488 | return -ENOSYS; | |
489 | } | |
490 | static inline struct gpio_desc *mlx90614_probe_wakeup(struct i2c_client *client) | |
491 | { | |
492 | return NULL; | |
493 | } | |
494 | #endif | |
495 | ||
bad4d1a0 VCSM |
496 | /* Return 0 for single sensor, 1 for dual sensor, <0 on error. */ |
497 | static int mlx90614_probe_num_ir_sensors(struct i2c_client *client) | |
498 | { | |
499 | s32 ret; | |
500 | ||
501 | ret = i2c_smbus_read_word_data(client, MLX90614_CONFIG); | |
502 | ||
503 | if (ret < 0) | |
504 | return ret; | |
505 | ||
506 | return (ret & MLX90614_CONFIG_DUAL_MASK) ? 1 : 0; | |
507 | } | |
508 | ||
474fe212 PM |
509 | static int mlx90614_probe(struct i2c_client *client, |
510 | const struct i2c_device_id *id) | |
511 | { | |
512 | struct iio_dev *indio_dev; | |
513 | struct mlx90614_data *data; | |
bad4d1a0 | 514 | int ret; |
474fe212 PM |
515 | |
516 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) | |
517 | return -ENODEV; | |
518 | ||
519 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | |
520 | if (!indio_dev) | |
521 | return -ENOMEM; | |
522 | ||
523 | data = iio_priv(indio_dev); | |
524 | i2c_set_clientdata(client, indio_dev); | |
525 | data->client = client; | |
fad65a8f | 526 | mutex_init(&data->lock); |
eb4b07da VCSM |
527 | data->wakeup_gpio = mlx90614_probe_wakeup(client); |
528 | ||
529 | mlx90614_wakeup(data); | |
474fe212 PM |
530 | |
531 | indio_dev->dev.parent = &client->dev; | |
532 | indio_dev->name = id->name; | |
533 | indio_dev->modes = INDIO_DIRECT_MODE; | |
534 | indio_dev->info = &mlx90614_info; | |
535 | ||
bad4d1a0 VCSM |
536 | ret = mlx90614_probe_num_ir_sensors(client); |
537 | switch (ret) { | |
538 | case 0: | |
539 | dev_dbg(&client->dev, "Found single sensor"); | |
540 | indio_dev->channels = mlx90614_channels; | |
541 | indio_dev->num_channels = 2; | |
542 | break; | |
543 | case 1: | |
544 | dev_dbg(&client->dev, "Found dual sensor"); | |
545 | indio_dev->channels = mlx90614_channels; | |
546 | indio_dev->num_channels = 3; | |
547 | break; | |
548 | default: | |
549 | return ret; | |
550 | } | |
474fe212 | 551 | |
eb4b07da VCSM |
552 | if (data->wakeup_gpio) { |
553 | pm_runtime_set_autosuspend_delay(&client->dev, | |
554 | MLX90614_AUTOSLEEP_DELAY); | |
555 | pm_runtime_use_autosuspend(&client->dev); | |
556 | pm_runtime_set_active(&client->dev); | |
557 | pm_runtime_enable(&client->dev); | |
558 | } | |
559 | ||
474fe212 PM |
560 | return iio_device_register(indio_dev); |
561 | } | |
562 | ||
563 | static int mlx90614_remove(struct i2c_client *client) | |
564 | { | |
eb4b07da VCSM |
565 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
566 | struct mlx90614_data *data = iio_priv(indio_dev); | |
567 | ||
568 | iio_device_unregister(indio_dev); | |
569 | ||
570 | if (data->wakeup_gpio) { | |
571 | pm_runtime_disable(&client->dev); | |
572 | if (!pm_runtime_status_suspended(&client->dev)) | |
573 | mlx90614_sleep(data); | |
574 | pm_runtime_set_suspended(&client->dev); | |
575 | } | |
474fe212 PM |
576 | |
577 | return 0; | |
578 | } | |
579 | ||
580 | static const struct i2c_device_id mlx90614_id[] = { | |
581 | { "mlx90614", 0 }, | |
582 | { } | |
583 | }; | |
584 | MODULE_DEVICE_TABLE(i2c, mlx90614_id); | |
585 | ||
eb4b07da VCSM |
586 | #ifdef CONFIG_PM_SLEEP |
587 | static int mlx90614_pm_suspend(struct device *dev) | |
588 | { | |
589 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | |
590 | struct mlx90614_data *data = iio_priv(indio_dev); | |
591 | ||
592 | if (data->wakeup_gpio && pm_runtime_active(dev)) | |
593 | return mlx90614_sleep(data); | |
594 | ||
595 | return 0; | |
596 | } | |
597 | ||
598 | static int mlx90614_pm_resume(struct device *dev) | |
599 | { | |
600 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | |
601 | struct mlx90614_data *data = iio_priv(indio_dev); | |
602 | int err; | |
603 | ||
604 | if (data->wakeup_gpio) { | |
605 | err = mlx90614_wakeup(data); | |
606 | if (err < 0) | |
607 | return err; | |
608 | ||
609 | pm_runtime_disable(dev); | |
610 | pm_runtime_set_active(dev); | |
611 | pm_runtime_enable(dev); | |
612 | } | |
613 | ||
614 | return 0; | |
615 | } | |
616 | #endif | |
617 | ||
618 | #ifdef CONFIG_PM | |
619 | static int mlx90614_pm_runtime_suspend(struct device *dev) | |
620 | { | |
621 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | |
622 | struct mlx90614_data *data = iio_priv(indio_dev); | |
623 | ||
624 | return mlx90614_sleep(data); | |
625 | } | |
626 | ||
627 | static int mlx90614_pm_runtime_resume(struct device *dev) | |
628 | { | |
629 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | |
630 | struct mlx90614_data *data = iio_priv(indio_dev); | |
631 | ||
632 | return mlx90614_wakeup(data); | |
633 | } | |
634 | #endif | |
635 | ||
636 | static const struct dev_pm_ops mlx90614_pm_ops = { | |
637 | SET_SYSTEM_SLEEP_PM_OPS(mlx90614_pm_suspend, mlx90614_pm_resume) | |
638 | SET_RUNTIME_PM_OPS(mlx90614_pm_runtime_suspend, | |
639 | mlx90614_pm_runtime_resume, NULL) | |
640 | }; | |
641 | ||
474fe212 PM |
642 | static struct i2c_driver mlx90614_driver = { |
643 | .driver = { | |
644 | .name = "mlx90614", | |
eb4b07da | 645 | .pm = &mlx90614_pm_ops, |
474fe212 PM |
646 | }, |
647 | .probe = mlx90614_probe, | |
648 | .remove = mlx90614_remove, | |
649 | .id_table = mlx90614_id, | |
650 | }; | |
651 | module_i2c_driver(mlx90614_driver); | |
652 | ||
653 | MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); | |
bad4d1a0 | 654 | MODULE_AUTHOR("Vianney le Clément de Saint-Marcq <vianney.leclement@essensium.com>"); |
764589b6 | 655 | MODULE_AUTHOR("Crt Mori <cmo@melexis.com>"); |
474fe212 PM |
656 | MODULE_DESCRIPTION("Melexis MLX90614 contactless IR temperature sensor driver"); |
657 | MODULE_LICENSE("GPL"); |