Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | /* sensor_isl29035.c - driver for ISL29035 light sensor */ /* * Copyright (c) 2016 Intel Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include <arch/cpu.h> #include <device.h> #include <i2c.h> #include <init.h> #include <nanokernel.h> #include <sensor.h> #include <zephyr.h> #include "sensor_isl29035.h" int isl29035_write_reg(struct isl29035_driver_data *drv_data, uint8_t reg, uint8_t val) { uint8_t tx_buf[2] = {reg, val}; return i2c_write(drv_data->i2c, tx_buf, sizeof(tx_buf), ISL29035_I2C_ADDRESS); } int isl29035_read_reg(struct isl29035_driver_data *drv_data, uint8_t reg, uint8_t *val) { struct i2c_msg msgs[2] = { { .buf = ®, .len = 1, .flags = I2C_MSG_WRITE | I2C_MSG_RESTART, }, { .buf = val, .len = 1, .flags = I2C_MSG_READ | I2C_MSG_STOP, } }; return i2c_transfer(drv_data->i2c, msgs, 2, ISL29035_I2C_ADDRESS); } int isl29035_update_reg(struct isl29035_driver_data *drv_data, uint8_t reg, uint8_t mask, uint8_t val) { uint8_t old_val = 0; uint8_t new_val; if (isl29035_read_reg(drv_data, reg, &old_val) != 0) { return -EIO; } new_val = old_val & ~mask; new_val |= val & mask; return isl29035_write_reg(drv_data, reg, new_val); } static int isl29035_sample_fetch(struct device *dev) { struct isl29035_driver_data *drv_data = dev->driver_data; uint8_t msb, lsb; int ret; ret = isl29035_read_reg(drv_data, ISL29035_DATA_MSB_REG, &msb); if (ret != 0) { return -EIO; } ret = isl29035_read_reg(drv_data, ISL29035_DATA_LSB_REG, &lsb); if (ret != 0) { return -EIO; } drv_data->data_sample = (msb << 8) + lsb; return 0; } static int isl29035_channel_get(struct device *dev, enum sensor_channel chan, struct sensor_value *val) { struct isl29035_driver_data *drv_data = dev->driver_data; uint64_t tmp; #if CONFIG_ISL29035_MODE_ALS /* val = sample_val * lux_range / (2 ^ adc_data_bits) */ tmp = (uint64_t)drv_data->data_sample * ISL29035_LUX_RANGE; val->type = SENSOR_TYPE_INT_PLUS_MICRO; val->val1 = tmp >> ISL29035_ADC_DATA_BITS; tmp = (tmp & ISL29035_ADC_DATA_MASK) * 1000000; val->val2 = tmp >> ISL29035_ADC_DATA_BITS; #elif CONFIG_ISL29035_MODE_IR ARG_UNUSED(tmp); val->type = SENSOR_TYPE_INT; val->val1 = drv_data->data_sample; #endif return 0; } static struct sensor_driver_api isl29035_api = { #if CONFIG_ISL29035_TRIGGER .attr_set = &isl29035_attr_set, .trigger_set = &isl29035_trigger_set, #endif .sample_fetch = &isl29035_sample_fetch, .channel_get = &isl29035_channel_get, }; static int isl29035_init(struct device *dev) { struct isl29035_driver_data *drv_data = dev->driver_data; int ret; drv_data->i2c = device_get_binding(CONFIG_ISL29035_I2C_MASTER_DEV_NAME); if (drv_data->i2c == NULL) { DBG("Failed to get I2C device.\n"); return -EINVAL; } dev->driver_api = &isl29035_api; drv_data->data_sample = 0; /* clear blownout status bit */ ret = isl29035_update_reg(drv_data, ISL29035_ID_REG, ISL29035_BOUT_MASK, 0); if (ret != 0) { DBG("Failed to clear blownout status bit.\n"); return ret; } /* set command registers to set default attributes */ ret = isl29035_write_reg(drv_data, ISL29035_COMMAND_I_REG, 0); if (ret != 0) { DBG("Failed to clear COMMAND-I.\n"); return ret; } ret = isl29035_write_reg(drv_data, ISL29035_COMMAND_II_REG, 0); if (ret != 0) { DBG("Failed to clear COMMAND-II.\n"); return ret; } /* set operation mode */ ret = isl29035_update_reg(drv_data, ISL29035_COMMAND_I_REG, ISL29035_OPMODE_MASK, ISL29035_ACTIVE_OPMODE_BITS); if (ret != 0) { DBG("Failed to set opmode.\n"); return ret; } /* set lux range */ ret = isl29035_update_reg(drv_data, ISL29035_COMMAND_II_REG, ISL29035_LUX_RANGE_MASK, ISL29035_LUX_RANGE_BITS); if (ret != 0) { DBG("Failed to set lux range.\n"); return ret; } /* set ADC resolution */ ret = isl29035_update_reg(drv_data, ISL29035_COMMAND_II_REG, ISL29035_ADC_RES_MASK, ISL29035_ADC_RES_BITS); if (ret != 0) { DBG("Failed to set ADC resolution.\n"); return ret; } #ifdef CONFIG_ISL29035_TRIGGER ret = isl29035_init_interrupt(dev); if (ret != 0) { DBG("Failed to initialize interrupt.\n"); return ret; } #endif return 0; } struct isl29035_driver_data isl29035_data; DEVICE_INIT(isl29035_dev, CONFIG_ISL29035_NAME, &isl29035_init, &isl29035_data, NULL, SECONDARY, CONFIG_ISL29035_INIT_PRIORITY); |