1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT asahi_kasei_ak8975
8 
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/drivers/sensor.h>
13 #include <zephyr/sys/__assert.h>
14 #include <zephyr/sys/byteorder.h>
15 #include <zephyr/sys/util.h>
16 #include <zephyr/logging/log.h>
17 
18 #include "ak8975.h"
19 
20 LOG_MODULE_REGISTER(AK8975, CONFIG_SENSOR_LOG_LEVEL);
21 
ak8975_sample_fetch(const struct device * dev,enum sensor_channel chan)22 static int ak8975_sample_fetch(const struct device *dev,
23 			       enum sensor_channel chan)
24 {
25 	struct ak8975_data *drv_data = dev->data;
26 	const struct ak8975_config *drv_config = dev->config;
27 	uint8_t buf[6];
28 
29 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
30 
31 	if (i2c_reg_write_byte_dt(&drv_config->i2c, AK8975_REG_CNTL, AK8975_MODE_MEASURE) < 0) {
32 		LOG_ERR("Failed to start measurement.");
33 		return -EIO;
34 	}
35 
36 	k_busy_wait(AK8975_MEASURE_TIME_US);
37 
38 	if (i2c_burst_read_dt(&drv_config->i2c, AK8975_REG_DATA_START, buf, 6) < 0) {
39 		LOG_ERR("Failed to read sample data.");
40 		return -EIO;
41 	}
42 
43 	drv_data->x_sample = sys_le16_to_cpu(buf[0] | (buf[1] << 8));
44 	drv_data->y_sample = sys_le16_to_cpu(buf[2] | (buf[3] << 8));
45 	drv_data->z_sample = sys_le16_to_cpu(buf[4] | (buf[5] << 8));
46 
47 	return 0;
48 }
49 
ak8975_convert(struct sensor_value * val,int16_t sample,uint8_t adjustment)50 static void ak8975_convert(struct sensor_value *val, int16_t sample,
51 			   uint8_t adjustment)
52 {
53 	int32_t conv_val;
54 
55 	conv_val = sample * AK8975_MICRO_GAUSS_PER_BIT *
56 		   ((uint16_t)adjustment + 128) / 256;
57 	val->val1 = conv_val / 1000000;
58 	val->val2 = conv_val % 1000000;
59 }
60 
ak8975_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)61 static int ak8975_channel_get(const struct device *dev,
62 			      enum sensor_channel chan,
63 			      struct sensor_value *val)
64 {
65 	struct ak8975_data *drv_data = dev->data;
66 
67 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_MAGN_XYZ ||
68 			chan == SENSOR_CHAN_MAGN_X ||
69 			chan == SENSOR_CHAN_MAGN_Y ||
70 			chan == SENSOR_CHAN_MAGN_Z);
71 
72 	if (chan == SENSOR_CHAN_MAGN_XYZ) {
73 		ak8975_convert(val, drv_data->x_sample, drv_data->x_adj);
74 		ak8975_convert(val + 1, drv_data->y_sample, drv_data->y_adj);
75 		ak8975_convert(val + 2, drv_data->z_sample, drv_data->z_adj);
76 	} else if (chan == SENSOR_CHAN_MAGN_X) {
77 		ak8975_convert(val, drv_data->x_sample, drv_data->x_adj);
78 	} else if (chan == SENSOR_CHAN_MAGN_Y) {
79 		ak8975_convert(val, drv_data->y_sample, drv_data->y_adj);
80 	} else if (chan == SENSOR_CHAN_MAGN_Z) {
81 		ak8975_convert(val, drv_data->z_sample, drv_data->z_adj);
82 	} else {
83 		return -ENOTSUP;
84 	}
85 
86 	return 0;
87 }
88 
89 static DEVICE_API(sensor, ak8975_driver_api) = {
90 	.sample_fetch = ak8975_sample_fetch,
91 	.channel_get = ak8975_channel_get,
92 };
93 
ak8975_read_adjustment_data(const struct device * dev)94 static int ak8975_read_adjustment_data(const struct device *dev)
95 {
96 	struct ak8975_data *drv_data = dev->data;
97 	const struct ak8975_config *drv_config = dev->config;
98 	uint8_t buf[3];
99 
100 	if (i2c_reg_write_byte_dt(&drv_config->i2c, AK8975_REG_CNTL, AK8975_MODE_FUSE_ACCESS) < 0) {
101 		LOG_ERR("Failed to set chip in fuse access mode.");
102 		return -EIO;
103 	}
104 
105 	if (i2c_burst_read_dt(&drv_config->i2c, AK8975_REG_ADJ_DATA_START, buf, 3) < 0) {
106 		LOG_ERR("Failed to read adjustment data.");
107 		return -EIO;
108 	}
109 
110 	drv_data->x_adj = buf[0];
111 	drv_data->y_adj = buf[1];
112 	drv_data->z_adj = buf[2];
113 
114 	return 0;
115 }
116 
ak8975_init(const struct device * dev)117 int ak8975_init(const struct device *dev)
118 {
119 	const struct ak8975_config *drv_config = dev->config;
120 	uint8_t id;
121 
122 	if (!device_is_ready(drv_config->i2c.bus)) {
123 		LOG_ERR("I2C bus device not ready");
124 		return -ENODEV;
125 	}
126 
127 	/* check chip ID */
128 	if (i2c_reg_read_byte_dt(&drv_config->i2c, AK8975_REG_CHIP_ID, &id) < 0) {
129 		LOG_ERR("Failed to read chip ID.");
130 		return -EIO;
131 	}
132 
133 	if (id != AK8975_CHIP_ID) {
134 		LOG_ERR("Invalid chip ID.");
135 		return -EINVAL;
136 	}
137 
138 	if (ak8975_read_adjustment_data(dev) < 0) {
139 		return -EIO;
140 	}
141 
142 	return 0;
143 }
144 
145 #define AK8975_DEFINE(inst)								\
146 	static struct ak8975_data ak8975_data_##inst;					\
147 											\
148 	static const struct ak8975_config ak8975_config_##inst = {			\
149 		.i2c = I2C_DT_SPEC_INST_GET(inst),					\
150 	};										\
151 											\
152 	SENSOR_DEVICE_DT_INST_DEFINE(inst, ak8975_init, NULL,				\
153 			      &ak8975_data_##inst, &ak8975_config_##inst,		\
154 			      POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,			\
155 			      &ak8975_driver_api);					\
156 
157 DT_INST_FOREACH_STATUS_OKAY(AK8975_DEFINE)
158