1 // SPDX-License-Identifier: GPL-2.0
3 * max44009.c - Support for MAX44009 Ambient Light Sensor
5 * Copyright (c) 2019 Robert Eshleman <bobbyeshleman@gmail.com>
7 * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX44009.pdf
9 * TODO: Support continuous mode and configuring from manual mode to
12 * Default I2C address: 0x4a
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/bits.h>
18 #include <linux/i2c.h>
19 #include <linux/iio/events.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/interrupt.h>
23 #include <linux/module.h>
24 #include <linux/util_macros.h>
26 #define MAX44009_DRV_NAME "max44009"
28 /* Registers in datasheet order */
29 #define MAX44009_REG_INT_STATUS 0x0
30 #define MAX44009_REG_INT_EN 0x1
31 #define MAX44009_REG_CFG 0x2
32 #define MAX44009_REG_LUX_HI 0x3
33 #define MAX44009_REG_LUX_LO 0x4
34 #define MAX44009_REG_UPPER_THR 0x5
35 #define MAX44009_REG_LOWER_THR 0x6
36 #define MAX44009_REG_THR_TIMER 0x7
38 #define MAX44009_CFG_TIM_MASK GENMASK(2, 0)
39 #define MAX44009_CFG_MAN_MODE_MASK BIT(6)
41 /* The maximum rising threshold for the max44009 */
42 #define MAX44009_MAXIMUM_THRESHOLD 7520256
44 #define MAX44009_THRESH_EXP_MASK (0xf << 4)
45 #define MAX44009_THRESH_EXP_RSHIFT 4
46 #define MAX44009_THRESH_MANT_LSHIFT 4
47 #define MAX44009_THRESH_MANT_MASK 0xf
49 #define MAX44009_UPPER_THR_MINIMUM 15
51 /* The max44009 always scales raw readings by 0.045 and is non-configurable */
52 #define MAX44009_SCALE_NUMERATOR 45
53 #define MAX44009_SCALE_DENOMINATOR 1000
55 /* The fixed-point fractional multiplier for de-scaling threshold values */
56 #define MAX44009_FRACT_MULT 1000000
58 static const u32 max44009_int_time_ns_array
[] = {
63 50000000, /* Manual mode only */
64 25000000, /* Manual mode only */
65 12500000, /* Manual mode only */
66 6250000, /* Manual mode only */
69 static const char max44009_int_time_str
[] =
79 struct max44009_data
{
80 struct i2c_client
*client
;
84 static const struct iio_event_spec max44009_event_spec
[] = {
86 .type
= IIO_EV_TYPE_THRESH
,
87 .dir
= IIO_EV_DIR_RISING
,
88 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
89 BIT(IIO_EV_INFO_ENABLE
),
92 .type
= IIO_EV_TYPE_THRESH
,
93 .dir
= IIO_EV_DIR_FALLING
,
94 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
95 BIT(IIO_EV_INFO_ENABLE
),
99 static const struct iio_chan_spec max44009_channels
[] = {
102 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
103 BIT(IIO_CHAN_INFO_INT_TIME
),
104 .event_spec
= max44009_event_spec
,
105 .num_event_specs
= ARRAY_SIZE(max44009_event_spec
),
109 static int max44009_read_int_time(struct max44009_data
*data
)
112 int ret
= i2c_smbus_read_byte_data(data
->client
, MAX44009_REG_CFG
);
117 return max44009_int_time_ns_array
[ret
& MAX44009_CFG_TIM_MASK
];
120 static int max44009_write_int_time(struct max44009_data
*data
,
123 struct i2c_client
*client
= data
->client
;
124 int ret
, int_time
, config
;
127 ns
= val
* NSEC_PER_SEC
+ val2
;
128 int_time
= find_closest_descending(
130 max44009_int_time_ns_array
,
131 ARRAY_SIZE(max44009_int_time_ns_array
));
133 ret
= i2c_smbus_read_byte_data(client
, MAX44009_REG_CFG
);
141 * To set the integration time, the device must also be in manual
144 config
|= MAX44009_CFG_MAN_MODE_MASK
;
146 return i2c_smbus_write_byte_data(client
, MAX44009_REG_CFG
, config
);
149 static int max44009_write_raw(struct iio_dev
*indio_dev
,
150 struct iio_chan_spec
const *chan
, int val
,
153 struct max44009_data
*data
= iio_priv(indio_dev
);
156 if (mask
== IIO_CHAN_INFO_INT_TIME
&& chan
->type
== IIO_LIGHT
) {
157 mutex_lock(&data
->lock
);
158 ret
= max44009_write_int_time(data
, val
, val2
);
159 mutex_unlock(&data
->lock
);
165 static int max44009_write_raw_get_fmt(struct iio_dev
*indio_dev
,
166 struct iio_chan_spec
const *chan
,
169 return IIO_VAL_INT_PLUS_NANO
;
172 static int max44009_lux_raw(u8 hi
, u8 lo
)
178 * The mantissa consists of the low nibble of the Lux High Byte
179 * and the low nibble of the Lux Low Byte.
181 mantissa
= ((hi
& 0xf) << 4) | (lo
& 0xf);
183 /* The exponent byte is just the upper nibble of the Lux High Byte */
184 exponent
= (hi
>> 4) & 0xf;
187 * The exponent value is base 2 to the power of the raw exponent byte.
189 exponent
= 1 << exponent
;
191 return exponent
* mantissa
;
194 #define MAX44009_READ_LUX_XFER_LEN (4)
196 static int max44009_read_lux_raw(struct max44009_data
*data
)
199 u8 hireg
= MAX44009_REG_LUX_HI
;
200 u8 loreg
= MAX44009_REG_LUX_LO
;
204 struct i2c_msg msgs
[] = {
206 .addr
= data
->client
->addr
,
208 .len
= sizeof(hireg
),
212 .addr
= data
->client
->addr
,
218 .addr
= data
->client
->addr
,
220 .len
= sizeof(loreg
),
224 .addr
= data
->client
->addr
,
232 * Use i2c_transfer instead of smbus read because i2c_transfer
233 * does NOT use a stop bit between address write and data read.
234 * Using a stop bit causes disjoint upper/lower byte reads and
237 ret
= i2c_transfer(data
->client
->adapter
,
238 msgs
, MAX44009_READ_LUX_XFER_LEN
);
240 if (ret
!= MAX44009_READ_LUX_XFER_LEN
)
243 return max44009_lux_raw(hi
, lo
);
246 static int max44009_read_raw(struct iio_dev
*indio_dev
,
247 struct iio_chan_spec
const *chan
, int *val
,
248 int *val2
, long mask
)
250 struct max44009_data
*data
= iio_priv(indio_dev
);
255 case IIO_CHAN_INFO_PROCESSED
:
256 switch (chan
->type
) {
258 ret
= max44009_read_lux_raw(data
);
263 *val
= lux_raw
* MAX44009_SCALE_NUMERATOR
;
264 *val2
= MAX44009_SCALE_DENOMINATOR
;
265 return IIO_VAL_FRACTIONAL
;
269 case IIO_CHAN_INFO_INT_TIME
:
270 switch (chan
->type
) {
272 ret
= max44009_read_int_time(data
);
278 return IIO_VAL_INT_PLUS_NANO
;
287 static IIO_CONST_ATTR(illuminance_integration_time_available
,
288 max44009_int_time_str
);
290 static struct attribute
*max44009_attributes
[] = {
291 &iio_const_attr_illuminance_integration_time_available
.dev_attr
.attr
,
295 static const struct attribute_group max44009_attribute_group
= {
296 .attrs
= max44009_attributes
,
299 static int max44009_threshold_byte_from_fraction(int integral
, int fractional
)
303 if ((integral
<= 0 && fractional
<= 0) ||
304 integral
> MAX44009_MAXIMUM_THRESHOLD
||
305 (integral
== MAX44009_MAXIMUM_THRESHOLD
&& fractional
!= 0))
308 /* Reverse scaling of fixed-point integral */
309 mantissa
= integral
* MAX44009_SCALE_DENOMINATOR
;
310 mantissa
/= MAX44009_SCALE_NUMERATOR
;
312 /* Reverse scaling of fixed-point fractional */
313 mantissa
+= fractional
/ MAX44009_FRACT_MULT
*
314 (MAX44009_SCALE_DENOMINATOR
/ MAX44009_SCALE_NUMERATOR
);
316 for (exp
= 0; mantissa
> 0xff; exp
++)
323 return exp
| mantissa
;
326 static int max44009_get_thr_reg(enum iio_event_direction dir
)
329 case IIO_EV_DIR_RISING
:
330 return MAX44009_REG_UPPER_THR
;
331 case IIO_EV_DIR_FALLING
:
332 return MAX44009_REG_LOWER_THR
;
338 static int max44009_write_event_value(struct iio_dev
*indio_dev
,
339 const struct iio_chan_spec
*chan
,
340 enum iio_event_type type
,
341 enum iio_event_direction dir
,
342 enum iio_event_info info
,
345 struct max44009_data
*data
= iio_priv(indio_dev
);
348 if (info
!= IIO_EV_INFO_VALUE
|| chan
->type
!= IIO_LIGHT
)
351 threshold
= max44009_threshold_byte_from_fraction(val
, val2
);
355 reg
= max44009_get_thr_reg(dir
);
359 return i2c_smbus_write_byte_data(data
->client
, reg
, threshold
);
362 static int max44009_read_threshold(struct iio_dev
*indio_dev
,
363 enum iio_event_direction dir
)
365 struct max44009_data
*data
= iio_priv(indio_dev
);
367 int mantissa
, exponent
;
369 reg
= max44009_get_thr_reg(dir
);
373 byte
= i2c_smbus_read_byte_data(data
->client
, reg
);
377 mantissa
= byte
& MAX44009_THRESH_MANT_MASK
;
378 mantissa
<<= MAX44009_THRESH_MANT_LSHIFT
;
381 * To get the upper threshold, always adds the minimum upper threshold
382 * value to the shifted byte value (see datasheet).
384 if (dir
== IIO_EV_DIR_RISING
)
385 mantissa
+= MAX44009_UPPER_THR_MINIMUM
;
388 * Exponent is base 2 to the power of the threshold exponent byte
391 exponent
= byte
& MAX44009_THRESH_EXP_MASK
;
392 exponent
>>= MAX44009_THRESH_EXP_RSHIFT
;
394 return (1 << exponent
) * mantissa
;
397 static int max44009_read_event_value(struct iio_dev
*indio_dev
,
398 const struct iio_chan_spec
*chan
,
399 enum iio_event_type type
,
400 enum iio_event_direction dir
,
401 enum iio_event_info info
,
407 if (chan
->type
!= IIO_LIGHT
|| type
!= IIO_EV_TYPE_THRESH
)
410 ret
= max44009_read_threshold(indio_dev
, dir
);
415 *val
= threshold
* MAX44009_SCALE_NUMERATOR
;
416 *val2
= MAX44009_SCALE_DENOMINATOR
;
418 return IIO_VAL_FRACTIONAL
;
421 static int max44009_write_event_config(struct iio_dev
*indio_dev
,
422 const struct iio_chan_spec
*chan
,
423 enum iio_event_type type
,
424 enum iio_event_direction dir
,
427 struct max44009_data
*data
= iio_priv(indio_dev
);
430 if (chan
->type
!= IIO_LIGHT
|| type
!= IIO_EV_TYPE_THRESH
)
433 ret
= i2c_smbus_write_byte_data(data
->client
,
434 MAX44009_REG_INT_EN
, state
);
439 * Set device to trigger interrupt immediately upon exceeding
440 * the threshold limit.
442 return i2c_smbus_write_byte_data(data
->client
,
443 MAX44009_REG_THR_TIMER
, 0);
446 static int max44009_read_event_config(struct iio_dev
*indio_dev
,
447 const struct iio_chan_spec
*chan
,
448 enum iio_event_type type
,
449 enum iio_event_direction dir
)
451 struct max44009_data
*data
= iio_priv(indio_dev
);
453 if (chan
->type
!= IIO_LIGHT
|| type
!= IIO_EV_TYPE_THRESH
)
456 return i2c_smbus_read_byte_data(data
->client
, MAX44009_REG_INT_EN
);
459 static const struct iio_info max44009_info
= {
460 .read_raw
= max44009_read_raw
,
461 .write_raw
= max44009_write_raw
,
462 .write_raw_get_fmt
= max44009_write_raw_get_fmt
,
463 .read_event_value
= max44009_read_event_value
,
464 .read_event_config
= max44009_read_event_config
,
465 .write_event_value
= max44009_write_event_value
,
466 .write_event_config
= max44009_write_event_config
,
467 .attrs
= &max44009_attribute_group
,
470 static irqreturn_t
max44009_threaded_irq_handler(int irq
, void *p
)
472 struct iio_dev
*indio_dev
= p
;
473 struct max44009_data
*data
= iio_priv(indio_dev
);
476 ret
= i2c_smbus_read_byte_data(data
->client
, MAX44009_REG_INT_STATUS
);
478 iio_push_event(indio_dev
,
479 IIO_UNMOD_EVENT_CODE(IIO_LIGHT
, 0,
482 iio_get_time_ns(indio_dev
));
490 static int max44009_probe(struct i2c_client
*client
,
491 const struct i2c_device_id
*id
)
493 struct max44009_data
*data
;
494 struct iio_dev
*indio_dev
;
497 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
501 data
= iio_priv(indio_dev
);
502 i2c_set_clientdata(client
, indio_dev
);
503 data
->client
= client
;
504 indio_dev
->dev
.parent
= &client
->dev
;
505 indio_dev
->info
= &max44009_info
;
506 indio_dev
->modes
= INDIO_DIRECT_MODE
;
507 indio_dev
->name
= MAX44009_DRV_NAME
;
508 indio_dev
->channels
= max44009_channels
;
509 indio_dev
->num_channels
= ARRAY_SIZE(max44009_channels
);
510 mutex_init(&data
->lock
);
512 /* Clear any stale interrupt bit */
513 ret
= i2c_smbus_read_byte_data(client
, MAX44009_REG_CFG
);
517 if (client
->irq
> 0) {
518 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
520 max44009_threaded_irq_handler
,
521 IRQF_TRIGGER_FALLING
|
522 IRQF_ONESHOT
| IRQF_SHARED
,
529 return devm_iio_device_register(&client
->dev
, indio_dev
);
532 static const struct i2c_device_id max44009_id
[] = {
536 MODULE_DEVICE_TABLE(i2c
, max44009_id
);
538 static struct i2c_driver max44009_driver
= {
540 .name
= MAX44009_DRV_NAME
,
542 .probe
= max44009_probe
,
543 .id_table
= max44009_id
,
545 module_i2c_driver(max44009_driver
);
547 static const struct of_device_id max44009_of_match
[] = {
548 { .compatible
= "maxim,max44009" },
551 MODULE_DEVICE_TABLE(of
, max44009_of_match
);
553 MODULE_AUTHOR("Robert Eshleman <bobbyeshleman@gmail.com>");
554 MODULE_LICENSE("GPL v2");
555 MODULE_DESCRIPTION("MAX44009 ambient light sensor driver");