1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT maxim_max44009
8 
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/drivers/sensor.h>
12 #include <zephyr/sys/__assert.h>
13 #include <zephyr/logging/log.h>
14 
15 #include "max44009.h"
16 
17 LOG_MODULE_REGISTER(MAX44009, CONFIG_SENSOR_LOG_LEVEL);
18 
max44009_reg_read(const struct device * dev,uint8_t reg,uint8_t * val,bool send_stop)19 static int max44009_reg_read(const struct device *dev, uint8_t reg,
20 			     uint8_t *val, bool send_stop)
21 {
22 	const struct max44009_config *config = dev->config;
23 	struct i2c_msg msgs[2] = {
24 		{
25 			.buf = &reg,
26 			.len = 1,
27 			.flags = I2C_MSG_WRITE,
28 		},
29 		{
30 			.buf = val,
31 			.len = 1,
32 			.flags = I2C_MSG_READ,
33 		},
34 	};
35 
36 	if (send_stop) {
37 		msgs[1].flags |= I2C_MSG_STOP;
38 	}
39 
40 	if (i2c_transfer_dt(&config->i2c, msgs, 2) != 0) {
41 		return -EIO;
42 	}
43 
44 	return 0;
45 }
46 
max44009_reg_write(const struct device * dev,uint8_t reg,uint8_t val)47 static int max44009_reg_write(const struct device *dev, uint8_t reg,
48 			      uint8_t val)
49 {
50 	const struct max44009_config *config = dev->config;
51 	uint8_t tx_buf[2] = {reg, val};
52 
53 	return i2c_write_dt(&config->i2c, tx_buf, sizeof(tx_buf));
54 }
55 
max44009_reg_update(const struct device * dev,uint8_t reg,uint8_t mask,uint8_t val)56 static int max44009_reg_update(const struct device *dev, uint8_t reg,
57 			       uint8_t mask, uint8_t val)
58 {
59 	uint8_t old_val = 0U;
60 	uint8_t new_val = 0U;
61 
62 	if (max44009_reg_read(dev, reg, &old_val, true) != 0) {
63 		return -EIO;
64 	}
65 
66 	new_val = old_val & ~mask;
67 	new_val |= val & mask;
68 
69 	return max44009_reg_write(dev, reg, new_val);
70 }
71 
max44009_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)72 static int max44009_attr_set(const struct device *dev,
73 			     enum sensor_channel chan,
74 			     enum sensor_attribute attr,
75 			     const struct sensor_value *val)
76 {
77 	uint8_t value;
78 	uint32_t cr;
79 
80 	if (chan != SENSOR_CHAN_LIGHT) {
81 		return -ENOTSUP;
82 	}
83 
84 	switch (attr) {
85 	case SENSOR_ATTR_SAMPLING_FREQUENCY:
86 		/* convert rate to mHz */
87 		cr = val->val1 * 1000 + val->val2 / 1000;
88 
89 		/* the sensor supports 1.25Hz or continuous conversion */
90 		switch (cr) {
91 		case 1250:
92 			value = 0U;
93 			break;
94 		default:
95 			value = MAX44009_CONTINUOUS_SAMPLING;
96 		}
97 
98 		if (max44009_reg_update(dev, MAX44009_REG_CONFIG,
99 					MAX44009_SAMPLING_CONTROL_BIT,
100 					value) != 0) {
101 			LOG_DBG("Failed to set attribute!");
102 			return -EIO;
103 		}
104 
105 		return 0;
106 
107 	default:
108 		return -ENOTSUP;
109 	}
110 
111 	return 0;
112 }
113 
max44009_sample_fetch(const struct device * dev,enum sensor_channel chan)114 static int max44009_sample_fetch(const struct device *dev,
115 				 enum sensor_channel chan)
116 {
117 	struct max44009_data *drv_data = dev->data;
118 	uint8_t val_h, val_l;
119 
120 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_LIGHT);
121 
122 	drv_data->sample = 0U;
123 
124 	if (max44009_reg_read(dev, MAX44009_REG_LUX_HIGH_BYTE, &val_h,
125 			      false) != 0) {
126 		return -EIO;
127 	}
128 
129 	if (max44009_reg_read(dev, MAX44009_REG_LUX_LOW_BYTE, &val_l,
130 			      true) != 0) {
131 		return -EIO;
132 	}
133 
134 	drv_data->sample = ((uint16_t)val_h) << 8;
135 	drv_data->sample += val_l;
136 
137 	return 0;
138 }
139 
max44009_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)140 static int max44009_channel_get(const struct device *dev,
141 				enum sensor_channel chan,
142 				struct sensor_value *val)
143 {
144 	struct max44009_data *drv_data = dev->data;
145 	uint32_t uval;
146 
147 	if (chan != SENSOR_CHAN_LIGHT) {
148 		return -ENOTSUP;
149 	}
150 
151 	/**
152 	 * sample consists of 4 bits of exponent and 8 bits of mantissa
153 	 * bits 15 to 12 are exponent bits
154 	 * bits 11 to 8 and 3 to 0 are the mantissa bits
155 	 */
156 	uval = drv_data->sample;
157 	uval = (uval & MAX44009_MANTISSA_LOW_NIBBLE_MASK) +
158 	       ((uval & MAX44009_MANTISSA_HIGH_NIBBLE_MASK) >> 4);
159 	uval = uval << (drv_data->sample >> MAX44009_SAMPLE_EXPONENT_SHIFT);
160 
161 	/* lux is the integer of sample output multiplied by 0.045. */
162 	val->val1 = (uval * 45U) / 1000;
163 	val->val2 = ((uval * 45U) % 1000) * 1000U;
164 
165 	return 0;
166 }
167 
168 static const struct sensor_driver_api max44009_driver_api = {
169 	.attr_set = max44009_attr_set,
170 	.sample_fetch = max44009_sample_fetch,
171 	.channel_get = max44009_channel_get,
172 };
173 
max44009_init(const struct device * dev)174 int max44009_init(const struct device *dev)
175 {
176 	const struct max44009_config *config = dev->config;
177 
178 	if (!device_is_ready(config->i2c.bus)) {
179 		LOG_ERR("Bus device is not ready");
180 		return -ENODEV;
181 	}
182 
183 	return 0;
184 }
185 
186 #define MAX44009_DEFINE(inst)									\
187 	static struct max44009_data max44009_data_##inst;					\
188 												\
189 	static const struct max44009_config max44009_config_##inst = {				\
190 		.i2c = I2C_DT_SPEC_INST_GET(inst),						\
191 	};											\
192 												\
193 	SENSOR_DEVICE_DT_INST_DEFINE(inst, max44009_init, NULL,					\
194 			      &max44009_data_##inst, &max44009_config_##inst, POST_KERNEL,	\
195 			      CONFIG_SENSOR_INIT_PRIORITY, &max44009_driver_api);		\
196 
197 DT_INST_FOREACH_STATUS_OKAY(MAX44009_DEFINE)
198