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 | /* sensor_mcp9808.c - Driver for MCP9808 temperature 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 <errno.h> #include <nanokernel.h> #include <i2c.h> #include <init.h> #include <misc/byteorder.h> #include "sensor_mcp9808.h" int mcp9808_reg_read(struct mcp9808_data *data, uint8_t reg, uint16_t *val) { int ret; struct i2c_msg msgs[2] = { { .buf = ®, .len = 1, .flags = I2C_MSG_WRITE | I2C_MSG_RESTART, }, { .buf = (uint8_t *)val, .len = 2, .flags = I2C_MSG_READ | I2C_MSG_STOP, }, }; ret = i2c_transfer(data->i2c_master, msgs, 2, data->i2c_slave_addr); if (ret) { return ret; } *val = sys_be16_to_cpu(*val); return 0; } static int mcp9808_sample_fetch(struct device *dev) { struct mcp9808_data *data = dev->driver_data; return mcp9808_reg_read(data, MCP9808_REG_TEMP_AMB, &data->reg_val); } static int mcp9808_channel_get(struct device *dev, enum sensor_channel chan, struct sensor_value *val) { struct mcp9808_data *data = dev->driver_data; #ifdef CONFIG_SENSOR_DEBUG if (chan != SENSOR_CHAN_TEMP) { return -ENOTSUP; } #endif val->type = SENSOR_TYPE_INT_PLUS_MICRO; val->val1 = (data->reg_val & MCP9808_TEMP_INT_MASK) >> MCP9808_TEMP_INT_SHIFT; val->val2 = (data->reg_val & MCP9808_TEMP_FRAC_MASK) * 62500; if (data->reg_val & MCP9808_SIGN_BIT) { val->val1 -= 256; } return 0; } static struct sensor_driver_api mcp9808_api_funcs = { .sample_fetch = mcp9808_sample_fetch, .channel_get = mcp9808_channel_get, .attr_set = mcp9808_attr_set, .trigger_set = mcp9808_trigger_set, }; int mcp9808_init(struct device *dev) { struct mcp9808_data *data = dev->driver_data; dev->driver_api = &mcp9808_api_funcs; data->i2c_master = device_get_binding(CONFIG_MCP9808_I2C_DEV_NAME); if (!data->i2c_master) { DBG("mcp9808: i2c master not found: %s\n", CONFIG_MCP9808_I2C_DEV_NAME); return -EINVAL; } data->i2c_slave_addr = CONFIG_MCP9808_I2C_ADDR; mcp9808_setup_interrupt(dev); return 0; } struct mcp9808_data mcp9808_data; DEVICE_INIT(mcp9808, CONFIG_MCP9808_DEV_NAME, mcp9808_init, &mcp9808_data, NULL, SECONDARY, CONFIG_MCP9808_INIT_PRIORITY); |