1 /* sensor_isl29035.c - driver for ISL29035 light sensor */
2
3 /*
4 * Copyright (c) 2016 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #define DT_DRV_COMPAT isil_isl29035
10
11 #include <zephyr/kernel.h>
12 #include <zephyr/init.h>
13 #include <zephyr/drivers/i2c.h>
14 #include <zephyr/drivers/sensor.h>
15 #include <zephyr/sys/__assert.h>
16 #include <zephyr/logging/log.h>
17
18 #include "isl29035.h"
19
20 LOG_MODULE_REGISTER(ISL29035, CONFIG_SENSOR_LOG_LEVEL);
21
isl29035_sample_fetch(const struct device * dev,enum sensor_channel chan)22 static int isl29035_sample_fetch(const struct device *dev,
23 enum sensor_channel chan)
24 {
25 struct isl29035_driver_data *drv_data = dev->data;
26 const struct isl29035_config *config = dev->config;
27 uint8_t msb, lsb;
28
29 __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
30
31 if (i2c_reg_read_byte_dt(&config->i2c,
32 ISL29035_DATA_MSB_REG, &msb) < 0) {
33 return -EIO;
34 }
35
36 if (i2c_reg_read_byte_dt(&config->i2c, ISL29035_DATA_LSB_REG,
37 &lsb) < 0) {
38 return -EIO;
39 }
40
41 drv_data->data_sample = (msb << 8) + lsb;
42
43 return 0;
44 }
45
isl29035_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)46 static int isl29035_channel_get(const struct device *dev,
47 enum sensor_channel chan,
48 struct sensor_value *val)
49 {
50 struct isl29035_driver_data *drv_data = dev->data;
51 uint64_t tmp;
52
53 #if CONFIG_ISL29035_MODE_ALS
54 /* val = sample_val * lux_range / (2 ^ adc_data_bits) */
55 tmp = (uint64_t)drv_data->data_sample * ISL29035_LUX_RANGE;
56 val->val1 = tmp >> ISL29035_ADC_DATA_BITS;
57 tmp = (tmp & ISL29035_ADC_DATA_MASK) * 1000000U;
58 val->val2 = tmp >> ISL29035_ADC_DATA_BITS;
59 #elif CONFIG_ISL29035_MODE_IR
60 ARG_UNUSED(tmp);
61 val->val1 = drv_data->data_sample;
62 val->val2 = 0;
63 #endif
64
65 return 0;
66 }
67
68 static const struct sensor_driver_api isl29035_api = {
69 #if CONFIG_ISL29035_TRIGGER
70 .attr_set = &isl29035_attr_set,
71 .trigger_set = &isl29035_trigger_set,
72 #endif
73 .sample_fetch = &isl29035_sample_fetch,
74 .channel_get = &isl29035_channel_get,
75 };
76
isl29035_init(const struct device * dev)77 static int isl29035_init(const struct device *dev)
78 {
79 struct isl29035_driver_data *drv_data = dev->data;
80 const struct isl29035_config *config = dev->config;
81
82 if (!device_is_ready(config->i2c.bus)) {
83 LOG_ERR("I2C bus device not ready");
84 return -ENODEV;
85 }
86
87 drv_data->data_sample = 0U;
88
89 /* clear blownout status bit */
90 if (i2c_reg_update_byte_dt(&config->i2c, ISL29035_ID_REG,
91 ISL29035_BOUT_MASK, 0) < 0) {
92 LOG_DBG("Failed to clear blownout status bit.");
93 return -EIO;
94 }
95
96 /* set command registers to set default attributes */
97 if (i2c_reg_write_byte_dt(&config->i2c,
98 ISL29035_COMMAND_I_REG, 0) < 0) {
99 LOG_DBG("Failed to clear COMMAND-I.");
100 return -EIO;
101 }
102
103 if (i2c_reg_write_byte_dt(&config->i2c,
104 ISL29035_COMMAND_II_REG, 0) < 0) {
105 LOG_DBG("Failed to clear COMMAND-II.");
106 return -EIO;
107 }
108
109 /* set operation mode */
110 if (i2c_reg_update_byte_dt(&config->i2c,
111 ISL29035_COMMAND_I_REG,
112 ISL29035_OPMODE_MASK,
113 ISL29035_ACTIVE_OPMODE_BITS) < 0) {
114 LOG_DBG("Failed to set opmode.");
115 return -EIO;
116 }
117
118 /* set lux range */
119 if (i2c_reg_update_byte_dt(&config->i2c,
120 ISL29035_COMMAND_II_REG,
121 ISL29035_LUX_RANGE_MASK,
122 ISL29035_LUX_RANGE_BITS) < 0) {
123 LOG_DBG("Failed to set lux range.");
124 return -EIO;
125 }
126
127 /* set ADC resolution */
128 if (i2c_reg_update_byte_dt(&config->i2c,
129 ISL29035_COMMAND_II_REG,
130 ISL29035_ADC_RES_MASK,
131 ISL29035_ADC_RES_BITS) < 0) {
132 LOG_DBG("Failed to set ADC resolution.");
133 return -EIO;
134 }
135
136 #ifdef CONFIG_ISL29035_TRIGGER
137 if (config->int_gpio.port) {
138 if (isl29035_init_interrupt(dev) < 0) {
139 LOG_DBG("Failed to initialize interrupt.");
140 return -EIO;
141 }
142 }
143 #endif
144
145 return 0;
146 }
147
148 #define ISL29035_DEFINE(inst) \
149 static struct isl29035_driver_data isl29035_data_##inst; \
150 \
151 static const struct isl29035_config isl29035_config_##inst = { \
152 .i2c = I2C_DT_SPEC_INST_GET(inst), \
153 IF_ENABLED(CONFIG_ISL29035_TRIGGER, \
154 (.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, { 0 }),)) \
155 }; \
156 \
157 SENSOR_DEVICE_DT_INST_DEFINE(inst, &isl29035_init, NULL, \
158 &isl29035_data_##inst, &isl29035_config_##inst, POST_KERNEL, \
159 CONFIG_SENSOR_INIT_PRIORITY, &isl29035_api); \
160
161 DT_INST_FOREACH_STATUS_OKAY(ISL29035_DEFINE)
162