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