1 // SPDX-License-Identifier: GPL-2.0
3 * VCNL4035 Ambient Light and Proximity Sensor - 7-bit I2C slave address 0x60
5 * Copyright (c) 2018, DENX Software Engineering GmbH
6 * Author: Parthiban Nallathambi <pn@denx.de>
10 #include <linux/bitops.h>
11 #include <linux/bitfield.h>
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regmap.h>
17 #include <linux/iio/buffer.h>
18 #include <linux/iio/events.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
25 #define VCNL4035_DRV_NAME "vcnl4035"
26 #define VCNL4035_IRQ_NAME "vcnl4035_event"
27 #define VCNL4035_REGMAP_NAME "vcnl4035_regmap"
29 /* Device registers */
30 #define VCNL4035_ALS_CONF 0x00
31 #define VCNL4035_ALS_THDH 0x01
32 #define VCNL4035_ALS_THDL 0x02
33 #define VCNL4035_ALS_DATA 0x0B
34 #define VCNL4035_WHITE_DATA 0x0C
35 #define VCNL4035_INT_FLAG 0x0D
36 #define VCNL4035_DEV_ID 0x0E
39 #define VCNL4035_MODE_ALS_MASK BIT(0)
40 #define VCNL4035_MODE_ALS_WHITE_CHAN BIT(8)
41 #define VCNL4035_MODE_ALS_INT_MASK BIT(1)
42 #define VCNL4035_ALS_IT_MASK GENMASK(7, 5)
43 #define VCNL4035_ALS_PERS_MASK GENMASK(3, 2)
44 #define VCNL4035_INT_ALS_IF_H_MASK BIT(12)
45 #define VCNL4035_INT_ALS_IF_L_MASK BIT(13)
46 #define VCNL4035_DEV_ID_MASK GENMASK(7, 0)
49 #define VCNL4035_MODE_ALS_ENABLE BIT(0)
50 #define VCNL4035_MODE_ALS_DISABLE 0x00
51 #define VCNL4035_MODE_ALS_INT_ENABLE BIT(1)
52 #define VCNL4035_MODE_ALS_INT_DISABLE 0
53 #define VCNL4035_DEV_ID_VAL 0x80
54 #define VCNL4035_ALS_IT_DEFAULT 0x01
55 #define VCNL4035_ALS_PERS_DEFAULT 0x00
56 #define VCNL4035_ALS_THDH_DEFAULT 5000
57 #define VCNL4035_ALS_THDL_DEFAULT 100
58 #define VCNL4035_SLEEP_DELAY_MS 2000
60 struct vcnl4035_data
{
61 struct i2c_client
*client
;
62 struct regmap
*regmap
;
63 unsigned int als_it_val
;
64 unsigned int als_persistence
;
65 unsigned int als_thresh_low
;
66 unsigned int als_thresh_high
;
67 struct iio_trigger
*drdy_trigger0
;
70 static inline bool vcnl4035_is_triggered(struct vcnl4035_data
*data
)
75 ret
= regmap_read(data
->regmap
, VCNL4035_INT_FLAG
, ®
);
80 (VCNL4035_INT_ALS_IF_H_MASK
| VCNL4035_INT_ALS_IF_L_MASK
));
83 static irqreturn_t
vcnl4035_drdy_irq_thread(int irq
, void *private)
85 struct iio_dev
*indio_dev
= private;
86 struct vcnl4035_data
*data
= iio_priv(indio_dev
);
88 if (vcnl4035_is_triggered(data
)) {
89 iio_push_event(indio_dev
, IIO_UNMOD_EVENT_CODE(IIO_LIGHT
,
93 iio_get_time_ns(indio_dev
));
94 iio_trigger_poll_nested(data
->drdy_trigger0
);
101 /* Triggered buffer */
102 static irqreturn_t
vcnl4035_trigger_consumer_handler(int irq
, void *p
)
104 struct iio_poll_func
*pf
= p
;
105 struct iio_dev
*indio_dev
= pf
->indio_dev
;
106 struct vcnl4035_data
*data
= iio_priv(indio_dev
);
107 /* Ensure naturally aligned timestamp */
108 u8 buffer
[ALIGN(sizeof(u16
), sizeof(s64
)) + sizeof(s64
)] __aligned(8);
111 ret
= regmap_read(data
->regmap
, VCNL4035_ALS_DATA
, (int *)buffer
);
113 dev_err(&data
->client
->dev
,
114 "Trigger consumer can't read from sensor.\n");
117 iio_push_to_buffers_with_timestamp(indio_dev
, buffer
,
118 iio_get_time_ns(indio_dev
));
121 iio_trigger_notify_done(indio_dev
->trig
);
126 static int vcnl4035_als_drdy_set_state(struct iio_trigger
*trigger
,
129 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trigger
);
130 struct vcnl4035_data
*data
= iio_priv(indio_dev
);
131 int val
= enable_drdy
? VCNL4035_MODE_ALS_INT_ENABLE
:
132 VCNL4035_MODE_ALS_INT_DISABLE
;
134 return regmap_update_bits(data
->regmap
, VCNL4035_ALS_CONF
,
135 VCNL4035_MODE_ALS_INT_MASK
,
139 static const struct iio_trigger_ops vcnl4035_trigger_ops
= {
140 .validate_device
= iio_trigger_validate_own_device
,
141 .set_trigger_state
= vcnl4035_als_drdy_set_state
,
144 static int vcnl4035_set_pm_runtime_state(struct vcnl4035_data
*data
, bool on
)
147 struct device
*dev
= &data
->client
->dev
;
150 ret
= pm_runtime_resume_and_get(dev
);
152 pm_runtime_mark_last_busy(dev
);
153 ret
= pm_runtime_put_autosuspend(dev
);
160 * Device IT INT Time (ms) Scale (lux/step)
165 * 101 - 111 800 0.004
166 * Values are proportional, so ALS INT is selected for input due to
167 * simplicity reason. Integration time value and scaling is
168 * calculated based on device INT value
170 * Raw value needs to be scaled using ALS steps
172 static int vcnl4035_read_raw(struct iio_dev
*indio_dev
,
173 struct iio_chan_spec
const *chan
, int *val
,
174 int *val2
, long mask
)
176 struct vcnl4035_data
*data
= iio_priv(indio_dev
);
182 case IIO_CHAN_INFO_RAW
:
183 ret
= vcnl4035_set_pm_runtime_state(data
, true);
187 ret
= iio_device_claim_direct_mode(indio_dev
);
190 reg
= VCNL4035_ALS_DATA
;
192 reg
= VCNL4035_WHITE_DATA
;
193 ret
= regmap_read(data
->regmap
, reg
, &raw_data
);
194 iio_device_release_direct_mode(indio_dev
);
200 vcnl4035_set_pm_runtime_state(data
, false);
202 case IIO_CHAN_INFO_INT_TIME
:
204 if (data
->als_it_val
)
205 *val
= data
->als_it_val
* 100;
207 case IIO_CHAN_INFO_SCALE
:
209 if (!data
->als_it_val
)
212 *val2
= data
->als_it_val
* 2 * 1000;
213 return IIO_VAL_FRACTIONAL
;
219 static int vcnl4035_write_raw(struct iio_dev
*indio_dev
,
220 struct iio_chan_spec
const *chan
,
221 int val
, int val2
, long mask
)
224 struct vcnl4035_data
*data
= iio_priv(indio_dev
);
227 case IIO_CHAN_INFO_INT_TIME
:
228 if (val
<= 0 || val
> 800)
231 ret
= vcnl4035_set_pm_runtime_state(data
, true);
235 ret
= regmap_update_bits(data
->regmap
, VCNL4035_ALS_CONF
,
236 VCNL4035_ALS_IT_MASK
,
239 data
->als_it_val
= val
/ 100;
241 vcnl4035_set_pm_runtime_state(data
, false);
248 /* No direct ABI for persistence and threshold, so eventing */
249 static int vcnl4035_read_thresh(struct iio_dev
*indio_dev
,
250 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
251 enum iio_event_direction dir
, enum iio_event_info info
,
254 struct vcnl4035_data
*data
= iio_priv(indio_dev
);
257 case IIO_EV_INFO_VALUE
:
259 case IIO_EV_DIR_RISING
:
260 *val
= data
->als_thresh_high
;
262 case IIO_EV_DIR_FALLING
:
263 *val
= data
->als_thresh_low
;
269 case IIO_EV_INFO_PERIOD
:
270 *val
= data
->als_persistence
;
278 static int vcnl4035_write_thresh(struct iio_dev
*indio_dev
,
279 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
280 enum iio_event_direction dir
, enum iio_event_info info
, int val
,
283 struct vcnl4035_data
*data
= iio_priv(indio_dev
);
287 case IIO_EV_INFO_VALUE
:
288 /* 16 bit threshold range 0 - 65535 */
289 if (val
< 0 || val
> 65535)
291 if (dir
== IIO_EV_DIR_RISING
) {
292 if (val
< data
->als_thresh_low
)
294 ret
= regmap_write(data
->regmap
, VCNL4035_ALS_THDH
,
298 data
->als_thresh_high
= val
;
300 if (val
> data
->als_thresh_high
)
302 ret
= regmap_write(data
->regmap
, VCNL4035_ALS_THDL
,
306 data
->als_thresh_low
= val
;
309 case IIO_EV_INFO_PERIOD
:
310 /* allow only 1 2 4 8 as persistence value */
311 if (val
< 0 || val
> 8 || hweight8(val
) != 1)
313 ret
= regmap_update_bits(data
->regmap
, VCNL4035_ALS_CONF
,
314 VCNL4035_ALS_PERS_MASK
, val
);
316 data
->als_persistence
= val
;
323 static IIO_CONST_ATTR_INT_TIME_AVAIL("50 100 200 400 800");
325 static struct attribute
*vcnl4035_attributes
[] = {
326 &iio_const_attr_integration_time_available
.dev_attr
.attr
,
330 static const struct attribute_group vcnl4035_attribute_group
= {
331 .attrs
= vcnl4035_attributes
,
334 static const struct iio_info vcnl4035_info
= {
335 .read_raw
= vcnl4035_read_raw
,
336 .write_raw
= vcnl4035_write_raw
,
337 .read_event_value
= vcnl4035_read_thresh
,
338 .write_event_value
= vcnl4035_write_thresh
,
339 .attrs
= &vcnl4035_attribute_group
,
342 static const struct iio_event_spec vcnl4035_event_spec
[] = {
344 .type
= IIO_EV_TYPE_THRESH
,
345 .dir
= IIO_EV_DIR_RISING
,
346 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
348 .type
= IIO_EV_TYPE_THRESH
,
349 .dir
= IIO_EV_DIR_FALLING
,
350 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
352 .type
= IIO_EV_TYPE_THRESH
,
353 .dir
= IIO_EV_DIR_EITHER
,
354 .mask_separate
= BIT(IIO_EV_INFO_PERIOD
),
358 enum vcnl4035_scan_index_order
{
359 VCNL4035_CHAN_INDEX_LIGHT
,
360 VCNL4035_CHAN_INDEX_WHITE_LED
,
363 static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops
= {
364 .validate_scan_mask
= &iio_validate_scan_mask_onehot
,
367 static const struct iio_chan_spec vcnl4035_channels
[] = {
371 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
372 BIT(IIO_CHAN_INFO_INT_TIME
) |
373 BIT(IIO_CHAN_INFO_SCALE
),
374 .event_spec
= vcnl4035_event_spec
,
375 .num_event_specs
= ARRAY_SIZE(vcnl4035_event_spec
),
376 .scan_index
= VCNL4035_CHAN_INDEX_LIGHT
,
381 .endianness
= IIO_LE
,
385 .type
= IIO_INTENSITY
,
388 .channel2
= IIO_MOD_LIGHT_BOTH
,
389 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
390 .scan_index
= VCNL4035_CHAN_INDEX_WHITE_LED
,
395 .endianness
= IIO_LE
,
400 static int vcnl4035_set_als_power_state(struct vcnl4035_data
*data
, u8 status
)
402 return regmap_update_bits(data
->regmap
, VCNL4035_ALS_CONF
,
403 VCNL4035_MODE_ALS_MASK
,
407 static int vcnl4035_init(struct vcnl4035_data
*data
)
412 ret
= regmap_read(data
->regmap
, VCNL4035_DEV_ID
, &id
);
414 dev_err(&data
->client
->dev
, "Failed to read DEV_ID register\n");
418 id
= FIELD_GET(VCNL4035_DEV_ID_MASK
, id
);
419 if (id
!= VCNL4035_DEV_ID_VAL
) {
420 dev_err(&data
->client
->dev
, "Wrong id, got %x, expected %x\n",
421 id
, VCNL4035_DEV_ID_VAL
);
425 ret
= vcnl4035_set_als_power_state(data
, VCNL4035_MODE_ALS_ENABLE
);
429 /* ALS white channel enable */
430 ret
= regmap_update_bits(data
->regmap
, VCNL4035_ALS_CONF
,
431 VCNL4035_MODE_ALS_WHITE_CHAN
,
434 dev_err(&data
->client
->dev
, "set white channel enable %d\n",
439 /* set default integration time - 100 ms for ALS */
440 ret
= regmap_update_bits(data
->regmap
, VCNL4035_ALS_CONF
,
441 VCNL4035_ALS_IT_MASK
,
442 VCNL4035_ALS_IT_DEFAULT
);
444 dev_err(&data
->client
->dev
, "set default ALS IT returned %d\n",
448 data
->als_it_val
= VCNL4035_ALS_IT_DEFAULT
;
450 /* set default persistence time - 1 for ALS */
451 ret
= regmap_update_bits(data
->regmap
, VCNL4035_ALS_CONF
,
452 VCNL4035_ALS_PERS_MASK
,
453 VCNL4035_ALS_PERS_DEFAULT
);
455 dev_err(&data
->client
->dev
, "set default PERS returned %d\n",
459 data
->als_persistence
= VCNL4035_ALS_PERS_DEFAULT
;
461 /* set default HIGH threshold for ALS */
462 ret
= regmap_write(data
->regmap
, VCNL4035_ALS_THDH
,
463 VCNL4035_ALS_THDH_DEFAULT
);
465 dev_err(&data
->client
->dev
, "set default THDH returned %d\n",
469 data
->als_thresh_high
= VCNL4035_ALS_THDH_DEFAULT
;
471 /* set default LOW threshold for ALS */
472 ret
= regmap_write(data
->regmap
, VCNL4035_ALS_THDL
,
473 VCNL4035_ALS_THDL_DEFAULT
);
475 dev_err(&data
->client
->dev
, "set default THDL returned %d\n",
479 data
->als_thresh_low
= VCNL4035_ALS_THDL_DEFAULT
;
484 static bool vcnl4035_is_volatile_reg(struct device
*dev
, unsigned int reg
)
487 case VCNL4035_ALS_CONF
:
488 case VCNL4035_DEV_ID
:
495 static const struct regmap_config vcnl4035_regmap_config
= {
496 .name
= VCNL4035_REGMAP_NAME
,
499 .max_register
= VCNL4035_DEV_ID
,
500 .cache_type
= REGCACHE_RBTREE
,
501 .volatile_reg
= vcnl4035_is_volatile_reg
,
502 .val_format_endian
= REGMAP_ENDIAN_LITTLE
,
505 static int vcnl4035_probe_trigger(struct iio_dev
*indio_dev
)
508 struct vcnl4035_data
*data
= iio_priv(indio_dev
);
510 data
->drdy_trigger0
= devm_iio_trigger_alloc(
511 indio_dev
->dev
.parent
,
512 "%s-dev%d", indio_dev
->name
, iio_device_id(indio_dev
));
513 if (!data
->drdy_trigger0
)
516 data
->drdy_trigger0
->ops
= &vcnl4035_trigger_ops
;
517 iio_trigger_set_drvdata(data
->drdy_trigger0
, indio_dev
);
518 ret
= devm_iio_trigger_register(indio_dev
->dev
.parent
,
519 data
->drdy_trigger0
);
521 dev_err(&data
->client
->dev
, "iio trigger register failed\n");
526 ret
= devm_iio_triggered_buffer_setup(indio_dev
->dev
.parent
, indio_dev
,
527 NULL
, vcnl4035_trigger_consumer_handler
,
528 &iio_triggered_buffer_setup_ops
);
530 dev_err(&data
->client
->dev
, "iio triggered buffer setup failed\n");
534 /* IRQ to trigger mapping */
535 ret
= devm_request_threaded_irq(&data
->client
->dev
, data
->client
->irq
,
536 NULL
, vcnl4035_drdy_irq_thread
,
537 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
538 VCNL4035_IRQ_NAME
, indio_dev
);
540 dev_err(&data
->client
->dev
, "request irq %d for trigger0 failed\n",
545 static int vcnl4035_probe(struct i2c_client
*client
)
547 struct vcnl4035_data
*data
;
548 struct iio_dev
*indio_dev
;
549 struct regmap
*regmap
;
552 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
556 regmap
= devm_regmap_init_i2c(client
, &vcnl4035_regmap_config
);
557 if (IS_ERR(regmap
)) {
558 dev_err(&client
->dev
, "regmap_init failed!\n");
559 return PTR_ERR(regmap
);
562 data
= iio_priv(indio_dev
);
563 i2c_set_clientdata(client
, indio_dev
);
564 data
->client
= client
;
565 data
->regmap
= regmap
;
567 indio_dev
->info
= &vcnl4035_info
;
568 indio_dev
->name
= VCNL4035_DRV_NAME
;
569 indio_dev
->channels
= vcnl4035_channels
;
570 indio_dev
->num_channels
= ARRAY_SIZE(vcnl4035_channels
);
571 indio_dev
->modes
= INDIO_DIRECT_MODE
;
573 ret
= vcnl4035_init(data
);
575 dev_err(&client
->dev
, "vcnl4035 chip init failed\n");
579 if (client
->irq
> 0) {
580 ret
= vcnl4035_probe_trigger(indio_dev
);
582 dev_err(&client
->dev
, "vcnl4035 unable init trigger\n");
587 ret
= pm_runtime_set_active(&client
->dev
);
591 ret
= iio_device_register(indio_dev
);
595 pm_runtime_enable(&client
->dev
);
596 pm_runtime_set_autosuspend_delay(&client
->dev
, VCNL4035_SLEEP_DELAY_MS
);
597 pm_runtime_use_autosuspend(&client
->dev
);
602 vcnl4035_set_als_power_state(data
, VCNL4035_MODE_ALS_DISABLE
);
606 static void vcnl4035_remove(struct i2c_client
*client
)
608 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
611 pm_runtime_dont_use_autosuspend(&client
->dev
);
612 pm_runtime_disable(&client
->dev
);
613 iio_device_unregister(indio_dev
);
614 pm_runtime_set_suspended(&client
->dev
);
616 ret
= vcnl4035_set_als_power_state(iio_priv(indio_dev
),
617 VCNL4035_MODE_ALS_DISABLE
);
619 dev_warn(&client
->dev
, "Failed to put device into standby (%pe)\n",
623 static int vcnl4035_runtime_suspend(struct device
*dev
)
625 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
626 struct vcnl4035_data
*data
= iio_priv(indio_dev
);
629 ret
= vcnl4035_set_als_power_state(data
, VCNL4035_MODE_ALS_DISABLE
);
630 regcache_mark_dirty(data
->regmap
);
635 static int vcnl4035_runtime_resume(struct device
*dev
)
637 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
638 struct vcnl4035_data
*data
= iio_priv(indio_dev
);
641 regcache_sync(data
->regmap
);
642 ret
= vcnl4035_set_als_power_state(data
, VCNL4035_MODE_ALS_ENABLE
);
646 /* wait for 1 ALS integration cycle */
647 msleep(data
->als_it_val
* 100);
652 static DEFINE_RUNTIME_DEV_PM_OPS(vcnl4035_pm_ops
, vcnl4035_runtime_suspend
,
653 vcnl4035_runtime_resume
, NULL
);
655 static const struct i2c_device_id vcnl4035_id
[] = {
659 MODULE_DEVICE_TABLE(i2c
, vcnl4035_id
);
661 static const struct of_device_id vcnl4035_of_match
[] = {
662 { .compatible
= "vishay,vcnl4035", },
665 MODULE_DEVICE_TABLE(of
, vcnl4035_of_match
);
667 static struct i2c_driver vcnl4035_driver
= {
669 .name
= VCNL4035_DRV_NAME
,
670 .pm
= pm_ptr(&vcnl4035_pm_ops
),
671 .of_match_table
= vcnl4035_of_match
,
673 .probe
= vcnl4035_probe
,
674 .remove
= vcnl4035_remove
,
675 .id_table
= vcnl4035_id
,
678 module_i2c_driver(vcnl4035_driver
);
680 MODULE_AUTHOR("Parthiban Nallathambi <pn@denx.de>");
681 MODULE_DESCRIPTION("VCNL4035 Ambient Light Sensor driver");
682 MODULE_LICENSE("GPL v2");