1 /* ST Microelectronics IIS2MDC 3-axis magnetometer sensor
2 *
3 * Copyright (c) 2020 STMicroelectronics
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Datasheet:
8 * https://www.st.com/resource/en/datasheet/iis2mdc.pdf
9 */
10
11 #define DT_DRV_COMPAT st_iis2mdc
12
13 #include <string.h>
14 #include <zephyr/logging/log.h>
15
16 #include "iis2mdc.h"
17
18 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
19
20 LOG_MODULE_DECLARE(IIS2MDC, CONFIG_SENSOR_LOG_LEVEL);
21
iis2mdc_i2c_read(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint16_t len)22 static int iis2mdc_i2c_read(const struct device *dev, uint8_t reg_addr,
23 uint8_t *value, uint16_t len)
24 {
25 const struct iis2mdc_dev_config *cfg = dev->config;
26
27 return i2c_burst_read_dt(&cfg->i2c, reg_addr, value, len);
28 }
29
iis2mdc_i2c_write(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint16_t len)30 static int iis2mdc_i2c_write(const struct device *dev, uint8_t reg_addr,
31 uint8_t *value, uint16_t len)
32 {
33 const struct iis2mdc_dev_config *cfg = dev->config;
34
35 return i2c_burst_write_dt(&cfg->i2c, reg_addr, value, len);
36 }
37
iis2mdc_i2c_init(const struct device * dev)38 int iis2mdc_i2c_init(const struct device *dev)
39 {
40 struct iis2mdc_data *data = dev->data;
41 const struct iis2mdc_dev_config *cfg = dev->config;
42
43 if (!device_is_ready(cfg->i2c.bus)) {
44 LOG_ERR("I2C bus is not ready");
45 return -ENODEV;
46 }
47
48 data->ctx_i2c.read_reg = (stmdev_read_ptr) iis2mdc_i2c_read;
49 data->ctx_i2c.write_reg = (stmdev_write_ptr) iis2mdc_i2c_write;
50 data->ctx_i2c.mdelay = (stmdev_mdelay_ptr) stmemsc_mdelay;
51
52 data->ctx = &data->ctx_i2c;
53 data->ctx->handle = (void *)dev;
54
55 return 0;
56 }
57 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
58