|
| 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 | + |
| 12 | +#include "si7060.h" |
| 13 | + |
| 14 | +#define SIGN_BIT_MASK 0x7F |
| 15 | + |
| 16 | +LOG_MODULE_REGISTER(si7060, CONFIG_SENSOR_LOG_LEVEL); |
| 17 | + |
| 18 | +struct si7060_data { |
| 19 | + struct device *i2c_dev; |
| 20 | + u16_t temperature; |
| 21 | +}; |
| 22 | + |
| 23 | +static int si7060_reg_read(struct si7060_data *drv_data, u8_t reg, |
| 24 | + u8_t *val) |
| 25 | +{ |
| 26 | + if (i2c_reg_read_byte(drv_data->i2c_dev, |
| 27 | + DT_INST_0_SILABS_SI7060_BASE_ADDRESS, reg, val)) { |
| 28 | + return -EIO; |
| 29 | + } |
| 30 | + |
| 31 | + return 0; |
| 32 | +} |
| 33 | + |
| 34 | +static int si7060_reg_write(struct si7060_data *drv_data, u8_t reg, |
| 35 | + u8_t val) |
| 36 | +{ |
| 37 | + return i2c_reg_write_byte(drv_data->i2c_dev, |
| 38 | + DT_INST_0_SILABS_SI7060_BASE_ADDRESS, reg, val); |
| 39 | +} |
| 40 | + |
| 41 | +static int si7060_sample_fetch(struct device *dev, enum sensor_channel chan) |
| 42 | +{ |
| 43 | + struct si7060_data *drv_data = dev->driver_data; |
| 44 | + |
| 45 | + if (si7060_reg_write(drv_data, SI7060_REG_CONFIG, |
| 46 | + SI7060_ONE_BURST_VALUE) != 0) { |
| 47 | + return -EIO; |
| 48 | + } |
| 49 | + |
| 50 | + int retval; |
| 51 | + u8_t dspsigm; |
| 52 | + u8_t dspsigl; |
| 53 | + |
| 54 | + retval = si7060_reg_read(drv_data, SI7060_REG_TEMP_HIGH, |
| 55 | + &dspsigm); |
| 56 | + retval += si7060_reg_read(drv_data, SI7060_REG_TEMP_LOW, |
| 57 | + &dspsigl); |
| 58 | + |
| 59 | + if (retval == 0) { |
| 60 | + drv_data->temperature = (256 * (dspsigm & SIGN_BIT_MASK)) |
| 61 | + + dspsigl; |
| 62 | + } else { |
| 63 | + LOG_ERR("Read register err"); |
| 64 | + } |
| 65 | + |
| 66 | + LOG_DBG("Sample_fetch retval: %d", retval); |
| 67 | + |
| 68 | + return retval; |
| 69 | +} |
| 70 | + |
| 71 | +static int si7060_channel_get(struct device *dev, enum sensor_channel chan, |
| 72 | + struct sensor_value *val) |
| 73 | +{ |
| 74 | + struct si7060_data *drv_data = dev->driver_data; |
| 75 | + s32_t uval; |
| 76 | + |
| 77 | + if (chan == SENSOR_CHAN_AMBIENT_TEMP) { |
| 78 | + uval = ((55 * 160) + (drv_data->temperature - 16384)) >> 4; |
| 79 | + val->val1 = uval / 10; |
| 80 | + val->val2 = (uval % 10) * 100000; |
| 81 | + |
| 82 | + LOG_DBG("Temperature = val1:%d, val2:%d", val->val1, val->val2); |
| 83 | + |
| 84 | + return 0; |
| 85 | + } else { |
| 86 | + return -ENOTSUP; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +static const struct sensor_driver_api si7060_api = { |
| 91 | + .sample_fetch = &si7060_sample_fetch, |
| 92 | + .channel_get = &si7060_channel_get, |
| 93 | +}; |
| 94 | + |
| 95 | +static int si7060_chip_init(struct device *dev) |
| 96 | +{ |
| 97 | + struct si7060_data *drv_data = dev->driver_data; |
| 98 | + u8_t value; |
| 99 | + |
| 100 | + drv_data->i2c_dev = device_get_binding( |
| 101 | + DT_INST_0_SILABS_SI7060_BUS_NAME); |
| 102 | + |
| 103 | + if (!drv_data->i2c_dev) { |
| 104 | + LOG_ERR("Failed to get pointer to %s device!", |
| 105 | + DT_INST_0_SILABS_SI7060_BUS_NAME); |
| 106 | + return -EINVAL; |
| 107 | + } |
| 108 | + |
| 109 | + if (si7060_reg_read(drv_data, SI7060_REG_CHIP_INFO, |
| 110 | + &value) != 0) { |
| 111 | + return -EIO; |
| 112 | + } |
| 113 | + |
| 114 | + if ((value >> 4) != SI7060_CHIP_ID_VALUE) { |
| 115 | + LOG_ERR("Bad chip id 0x%x", value); |
| 116 | + return -ENOTSUP; |
| 117 | + } |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
| 122 | +static int si7060_init(struct device *dev) |
| 123 | +{ |
| 124 | + if (si7060_chip_init(dev) < 0) { |
| 125 | + return -EINVAL; |
| 126 | + } |
| 127 | + |
| 128 | + return 0; |
| 129 | +} |
| 130 | + |
| 131 | +static struct si7060_data si_data; |
| 132 | + |
| 133 | +DEVICE_AND_API_INIT(si7060, DT_INST_0_SILABS_SI7060_LABEL, si7060_init, |
| 134 | + &si_data, NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &si7060_api); |
0 commit comments