1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT st_lis3mdl_magn
8 
9 #include <zephyr/drivers/i2c.h>
10 #include <zephyr/init.h>
11 #include <zephyr/sys/__assert.h>
12 #include <zephyr/sys/byteorder.h>
13 #include <zephyr/drivers/sensor.h>
14 #include <string.h>
15 #include <zephyr/logging/log.h>
16 
17 #include "lis3mdl.h"
18 
19 LOG_MODULE_REGISTER(LIS3MDL, CONFIG_SENSOR_LOG_LEVEL);
20 
lis3mdl_convert(struct sensor_value * val,int16_t raw_val,uint16_t divider)21 static void lis3mdl_convert(struct sensor_value *val, int16_t raw_val,
22 			    uint16_t divider)
23 {
24 	/* val = raw_val / divider */
25 	val->val1 = raw_val / divider;
26 	val->val2 = (((int64_t)raw_val % divider) * 1000000L) / divider;
27 }
28 
lis3mdl_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)29 static int lis3mdl_channel_get(const struct device *dev,
30 			       enum sensor_channel chan,
31 			       struct sensor_value *val)
32 {
33 	struct lis3mdl_data *drv_data = dev->data;
34 
35 	if (chan == SENSOR_CHAN_MAGN_XYZ) {
36 		/* magn_val = sample / magn_gain */
37 		lis3mdl_convert(val, drv_data->x_sample,
38 				lis3mdl_magn_gain[LIS3MDL_FS_IDX]);
39 		lis3mdl_convert(val + 1, drv_data->y_sample,
40 				lis3mdl_magn_gain[LIS3MDL_FS_IDX]);
41 		lis3mdl_convert(val + 2, drv_data->z_sample,
42 				lis3mdl_magn_gain[LIS3MDL_FS_IDX]);
43 	} else if (chan == SENSOR_CHAN_MAGN_X) {
44 		lis3mdl_convert(val, drv_data->x_sample,
45 				lis3mdl_magn_gain[LIS3MDL_FS_IDX]);
46 	} else if (chan == SENSOR_CHAN_MAGN_Y) {
47 		lis3mdl_convert(val, drv_data->y_sample,
48 				lis3mdl_magn_gain[LIS3MDL_FS_IDX]);
49 	} else if (chan == SENSOR_CHAN_MAGN_Z) {
50 		lis3mdl_convert(val, drv_data->z_sample,
51 				lis3mdl_magn_gain[LIS3MDL_FS_IDX]);
52 	} else if (chan == SENSOR_CHAN_DIE_TEMP) {
53 		/* temp_val = 25 + sample / 8 */
54 		lis3mdl_convert(val, drv_data->temp_sample, 8);
55 		val->val1 += 25;
56 	} else {
57 		return -ENOTSUP;
58 	}
59 
60 	return 0;
61 }
62 
lis3mdl_sample_fetch(const struct device * dev,enum sensor_channel chan)63 int lis3mdl_sample_fetch(const struct device *dev, enum sensor_channel chan)
64 {
65 	struct lis3mdl_data *drv_data = dev->data;
66 	const struct lis3mdl_config *config = dev->config;
67 	int16_t buf[4];
68 
69 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
70 
71 	/* fetch magnetometer sample */
72 	if (i2c_burst_read_dt(&config->i2c, LIS3MDL_REG_SAMPLE_START,
73 			      (uint8_t *)buf, 8) < 0) {
74 		LOG_DBG("Failed to fetch magnetometer sample.");
75 		return -EIO;
76 	}
77 
78 	/*
79 	 * the chip doesn't allow fetching temperature data in
80 	 * the same read as magnetometer data, so do another
81 	 * burst read to fetch the temperature sample
82 	 */
83 	if (i2c_burst_read_dt(&config->i2c, LIS3MDL_REG_SAMPLE_START + 6,
84 			      (uint8_t *)(buf + 3), 2) < 0) {
85 		LOG_DBG("Failed to fetch temperature sample.");
86 		return -EIO;
87 	}
88 
89 	drv_data->x_sample = sys_le16_to_cpu(buf[0]);
90 	drv_data->y_sample = sys_le16_to_cpu(buf[1]);
91 	drv_data->z_sample = sys_le16_to_cpu(buf[2]);
92 	drv_data->temp_sample = sys_le16_to_cpu(buf[3]);
93 
94 	return 0;
95 }
96 
97 static const struct sensor_driver_api lis3mdl_driver_api = {
98 #if CONFIG_LIS3MDL_TRIGGER
99 	.trigger_set = lis3mdl_trigger_set,
100 #endif
101 	.sample_fetch = lis3mdl_sample_fetch,
102 	.channel_get = lis3mdl_channel_get,
103 };
104 
lis3mdl_init(const struct device * dev)105 int lis3mdl_init(const struct device *dev)
106 {
107 	const struct lis3mdl_config *config = dev->config;
108 	uint8_t chip_cfg[6];
109 	uint8_t id, idx;
110 
111 	if (!device_is_ready(config->i2c.bus)) {
112 		LOG_ERR("I2C bus device not ready");
113 		return -ENODEV;
114 	}
115 
116 	/* check chip ID */
117 	if (i2c_reg_read_byte_dt(&config->i2c, LIS3MDL_REG_WHO_AM_I, &id) < 0) {
118 		LOG_ERR("Failed to read chip ID.");
119 		return -EIO;
120 	}
121 
122 	if (id != LIS3MDL_CHIP_ID) {
123 		LOG_ERR("Invalid chip ID.");
124 		return -EINVAL;
125 	}
126 
127 	/* check if CONFIG_LIS3MDL_ODR is valid */
128 	for (idx = 0U; idx < ARRAY_SIZE(lis3mdl_odr_strings); idx++) {
129 		if (!strcmp(lis3mdl_odr_strings[idx], CONFIG_LIS3MDL_ODR)) {
130 			break;
131 		}
132 	}
133 
134 	if (idx == ARRAY_SIZE(lis3mdl_odr_strings)) {
135 		LOG_ERR("Invalid ODR value.");
136 		return -EINVAL;
137 	}
138 
139 	/* Configure sensor */
140 	chip_cfg[0] = LIS3MDL_REG_CTRL1;
141 	chip_cfg[1] = LIS3MDL_TEMP_EN_MASK | lis3mdl_odr_bits[idx];
142 	chip_cfg[2] = LIS3MDL_FS_IDX << LIS3MDL_FS_SHIFT;
143 	chip_cfg[3] = LIS3MDL_MD_CONTINUOUS;
144 	chip_cfg[4] = ((lis3mdl_odr_bits[idx] & LIS3MDL_OM_MASK) >>
145 		       LIS3MDL_OM_SHIFT) << LIS3MDL_OMZ_SHIFT;
146 	chip_cfg[5] = LIS3MDL_BDU_EN;
147 
148 	if (i2c_write_dt(&config->i2c, chip_cfg, 6) < 0) {
149 		LOG_DBG("Failed to configure chip.");
150 		return -EIO;
151 	}
152 
153 #ifdef CONFIG_LIS3MDL_TRIGGER
154 	if (config->irq_gpio.port) {
155 		if (lis3mdl_init_interrupt(dev) < 0) {
156 			LOG_DBG("Failed to initialize interrupts.");
157 			return -EIO;
158 		}
159 	}
160 #endif
161 
162 	return 0;
163 }
164 
165 #define LIS3MDL_DEFINE(inst)									\
166 	static struct lis3mdl_data lis3mdl_data_##inst;						\
167 												\
168 	static struct lis3mdl_config lis3mdl_config_##inst = {					\
169 		.i2c = I2C_DT_SPEC_INST_GET(inst),						\
170 		IF_ENABLED(CONFIG_LIS3MDL_TRIGGER,						\
171 			   (.irq_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, irq_gpios, { 0 }),))	\
172 	};											\
173 												\
174 	SENSOR_DEVICE_DT_INST_DEFINE(inst, lis3mdl_init, NULL,					\
175 			      &lis3mdl_data_##inst, &lis3mdl_config_##inst, POST_KERNEL,	\
176 			      CONFIG_SENSOR_INIT_PRIORITY, &lis3mdl_driver_api);		\
177 
178 DT_INST_FOREACH_STATUS_OKAY(LIS3MDL_DEFINE)
179