|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Actinius |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <device.h> |
| 8 | +#include <drivers/i2c.h> |
| 9 | +#include <drivers/sensor.h> |
| 10 | +#include <logging/log.h> |
| 11 | +#include <misc/__assert.h> |
| 12 | + |
| 13 | +#include "opt3001.h" |
| 14 | + |
| 15 | +LOG_MODULE_REGISTER(opt3001, CONFIG_SENSOR_LOG_LEVEL); |
| 16 | + |
| 17 | +static int opt3001_reg_read(struct opt3001_data *drv_data, u8_t reg, |
| 18 | + u16_t *val) |
| 19 | +{ |
| 20 | + u8_t value[2]; |
| 21 | + |
| 22 | + if (i2c_burst_read(drv_data->i2c, DT_INST_0_TI_OPT3001_BASE_ADDRESS, |
| 23 | + reg, value, 2) != 0) { |
| 24 | + return -EIO; |
| 25 | + } |
| 26 | + |
| 27 | + *val = ((u16_t)value[0] << 8) + value[1]; |
| 28 | + |
| 29 | + return 0; |
| 30 | +} |
| 31 | + |
| 32 | +static int opt3001_reg_write(struct opt3001_data *drv_data, u8_t reg, |
| 33 | + u16_t val) |
| 34 | +{ |
| 35 | + u8_t new_value[2]; |
| 36 | + |
| 37 | + new_value[0] = val >> 8; |
| 38 | + new_value[1] = val & 0xff; |
| 39 | + |
| 40 | + u8_t tx_buf[3] = { reg, new_value[0], new_value[1] }; |
| 41 | + |
| 42 | + return i2c_write(drv_data->i2c, tx_buf, sizeof(tx_buf), |
| 43 | + DT_INST_0_TI_OPT3001_BASE_ADDRESS); |
| 44 | +} |
| 45 | + |
| 46 | +static int opt3001_reg_update(struct opt3001_data *drv_data, u8_t reg, |
| 47 | + u16_t mask, u16_t val) |
| 48 | +{ |
| 49 | + u16_t old_val; |
| 50 | + u16_t new_val; |
| 51 | + |
| 52 | + if (opt3001_reg_read(drv_data, reg, &old_val) != 0) { |
| 53 | + return -EIO; |
| 54 | + } |
| 55 | + |
| 56 | + new_val = old_val & ~mask; |
| 57 | + new_val |= val & mask; |
| 58 | + |
| 59 | + return opt3001_reg_write(drv_data, reg, new_val); |
| 60 | +} |
| 61 | + |
| 62 | +static int opt3001_sample_fetch(struct device *dev, enum sensor_channel chan) |
| 63 | +{ |
| 64 | + struct opt3001_data *drv_data = dev->driver_data; |
| 65 | + u16_t value; |
| 66 | + |
| 67 | + __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_LIGHT); |
| 68 | + |
| 69 | + drv_data->sample = 0U; |
| 70 | + |
| 71 | + if (opt3001_reg_read(drv_data, OPT3001_REG_RESULT, &value) != 0) { |
| 72 | + return -EIO; |
| 73 | + } |
| 74 | + |
| 75 | + drv_data->sample = value; |
| 76 | + |
| 77 | + return 0; |
| 78 | +} |
| 79 | + |
| 80 | +static int opt3001_channel_get(struct device *dev, enum sensor_channel chan, |
| 81 | + struct sensor_value *val) |
| 82 | +{ |
| 83 | + struct opt3001_data *drv_data = dev->driver_data; |
| 84 | + s32_t uval; |
| 85 | + |
| 86 | + if (chan != SENSOR_CHAN_LIGHT) { |
| 87 | + return -ENOTSUP; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * sample consists of 4 bits of exponent and 12 bits of mantissa |
| 92 | + * bits 15 to 12 are exponent bits |
| 93 | + * bits 11 to 0 are the mantissa bits |
| 94 | + * |
| 95 | + * lux is the integer obtained using the following formula: |
| 96 | + * (2^(exponent value)) * 0.01 * mantisa value |
| 97 | + */ |
| 98 | + uval = (1 << (drv_data->sample >> OPT3001_SAMPLE_EXPONENT_SHIFT)) |
| 99 | + * (drv_data->sample & OPT3001_MANTISSA_MASK); |
| 100 | + val->val1 = uval / 100; |
| 101 | + val->val2 = (uval % 100) * 10000; |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +static const struct sensor_driver_api opt3001_driver_api = { |
| 107 | + .sample_fetch = opt3001_sample_fetch, |
| 108 | + .channel_get = opt3001_channel_get, |
| 109 | +}; |
| 110 | + |
| 111 | +static int opt3001_chip_init(struct device *dev) |
| 112 | +{ |
| 113 | + struct opt3001_data *drv_data = dev->driver_data; |
| 114 | + u16_t value; |
| 115 | + |
| 116 | + drv_data->i2c = device_get_binding(DT_INST_0_TI_OPT3001_BUS_NAME); |
| 117 | + if (drv_data->i2c == NULL) { |
| 118 | + LOG_ERR("Failed to get pointer to %s device!", |
| 119 | + DT_INST_0_TI_OPT3001_BUS_NAME); |
| 120 | + return -EINVAL; |
| 121 | + } |
| 122 | + |
| 123 | + if (opt3001_reg_read(drv_data, OPT3001_REG_MANUFACTURER_ID, |
| 124 | + &value) != 0) { |
| 125 | + return -EIO; |
| 126 | + } |
| 127 | + |
| 128 | + if (value != OPT3001_MANUFACTURER_ID_VALUE) { |
| 129 | + LOG_ERR("Bad manufacturer id 0x%x", value); |
| 130 | + return -ENOTSUP; |
| 131 | + } |
| 132 | + |
| 133 | + if (opt3001_reg_read(drv_data, OPT3001_REG_DEVICE_ID, |
| 134 | + &value) != 0) { |
| 135 | + return -EIO; |
| 136 | + } |
| 137 | + |
| 138 | + if (value != OPT3001_DEVICE_ID_VALUE) { |
| 139 | + LOG_ERR("Bad device id 0x%x", value); |
| 140 | + return -ENOTSUP; |
| 141 | + } |
| 142 | + |
| 143 | + if (opt3001_reg_update(drv_data, OPT3001_REG_CONFIG, |
| 144 | + OPT3001_CONVERSION_MODE_MASK, |
| 145 | + OPT3001_CONVERSION_MODE_CONTINUOUS) != 0) { |
| 146 | + LOG_ERR("Failed to set mode to continuous conversion"); |
| 147 | + return -EIO; |
| 148 | + } |
| 149 | + |
| 150 | + return 0; |
| 151 | +} |
| 152 | + |
| 153 | +int opt3001_init(struct device *dev) |
| 154 | +{ |
| 155 | + if (opt3001_chip_init(dev) < 0) { |
| 156 | + return -EINVAL; |
| 157 | + } |
| 158 | + |
| 159 | + return 0; |
| 160 | +} |
| 161 | + |
| 162 | +static struct opt3001_data opt3001_drv_data; |
| 163 | + |
| 164 | +DEVICE_AND_API_INIT(opt3001, DT_INST_0_TI_OPT3001_LABEL, opt3001_init, |
| 165 | + &opt3001_drv_data, NULL, POST_KERNEL, |
| 166 | + CONFIG_SENSOR_INIT_PRIORITY, &opt3001_driver_api); |
0 commit comments