Linux Audio

Check our new training course

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Copyright (c) 2021 Vestas Wind Systems A/S
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#define DT_DRV_COMPAT lm77

#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/logging/log.h>
#include <zephyr/pm/device.h>
#include <zephyr/sys/byteorder.h>

LOG_MODULE_REGISTER(lm77, CONFIG_SENSOR_LOG_LEVEL);

/*
 * Only compile in trigger support if enabled in Kconfig and at least one
 * enabled lm77 devicetree node has the int-gpios property.
 */
#define LM77_TRIGGER_SUPPORT_INST(inst) DT_INST_NODE_HAS_PROP(inst, int_gpios) ||
#define LM77_TRIGGER_SUPPORT \
	(CONFIG_LM77_TRIGGER && (DT_INST_FOREACH_STATUS_OKAY(LM77_TRIGGER_SUPPORT_INST) 0))

/* LM77 registers */
#define LM77_REG_TEMP   0x00U
#define LM77_REG_CONFIG 0x01U
#define LM77_REG_THYST  0x02U
#define LM77_REG_TCRIT  0x03U
#define LM77_REG_TLOW   0x04U
#define LM77_REG_THIGH  0x05U

/* LM77 configuration register bits */
union lm77_reg_config {
	uint8_t reg;
	struct {
		uint8_t shutdown : 1;
		uint8_t int_mode : 1;
		uint8_t tcrita_pol : 1;
		uint8_t int_pol : 1;
		uint8_t fault_queue : 1;
		uint8_t reserved : 3;
	} __packed;
};

struct lm77_config {
	struct i2c_dt_spec i2c;
	union lm77_reg_config config_dt;
#if LM77_TRIGGER_SUPPORT
	struct gpio_dt_spec int_gpio;
#endif /* LM77_TRIGGER_SUPPORT */
};

struct lm77_data {
	int16_t temp;
#if LM77_TRIGGER_SUPPORT
	const struct device *dev;
	struct k_work_q workq;
	struct k_work work;
	struct gpio_callback int_gpio_cb;
	const struct sensor_trigger *trigger;
	sensor_trigger_handler_t trigger_handler;

	K_KERNEL_STACK_MEMBER(stack, CONFIG_LM77_TRIGGER_THREAD_STACK_SIZE);
#endif /* LM77_TRIGGER_SUPPORT */
};

static int lm77_write_config(const struct device *dev, uint8_t value)
{
	const struct lm77_config *config = dev->config;
	uint8_t buf[2] = { LM77_REG_CONFIG, value };

	return i2c_write_dt(&config->i2c, buf, sizeof(buf));
}

static int lm77_read_temp(const struct device *dev, uint8_t reg, int16_t *value)
{
	const struct lm77_config *config = dev->config;
	uint8_t buf[2];
	int err;

	err = i2c_write_read_dt(&config->i2c, &reg, sizeof(reg), &buf, sizeof(buf));
	if (err < 0) {
		LOG_ERR("failed to read temperature register 0x%02x (err %d)", reg, err);
		return err;
	}

	*value = sys_get_be16(buf);

	return 0;
}

static int lm77_write_temp(const struct device *dev, uint8_t reg, int16_t value)
{
	const struct lm77_config *config = dev->config;
	uint8_t buf[3] = { reg, value >> 8, value };

	return i2c_write_dt(&config->i2c, buf, sizeof(buf));
}

static void lm77_sensor_value_to_temp(const struct sensor_value *val, int16_t *temp)
{
	/* Integer part in degrees Celsius (LSB = 0.5 degrees Celsius) */
	*temp = val->val1 << 1;

	/* Fractional part in micro degrees Celsius */
	*temp += (val->val2 * 2) / 1000000;

	/* Shift up to include "status" bits */
	*temp <<= 3;
}

static void lm77_temp_to_sensor_value(int16_t temp, struct sensor_value *val)
{
	/* Shift down to remove "status" bits (LSB = 0.5 degrees Celsius) */
	temp = (temp >> 3) * 5;

	/* Integer part in degrees Celsius */
	val->val1 = temp / 10;

	/* Fractional part in micro degrees Celsius */
	val->val2 = temp % 10;
	val->val2 *= 100000;
}

static int lm77_attr_set(const struct device *dev, enum sensor_channel chan,
			 enum sensor_attribute attr, const struct sensor_value *val)
{
	int16_t temp = 0;
	uint8_t reg;
	int err;

	__ASSERT_NO_MSG(val != NULL);

	if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
		return -ENOTSUP;
	}

	switch (attr) {
	case SENSOR_ATTR_LOWER_THRESH:
		reg = LM77_REG_TLOW;
		break;
	case SENSOR_ATTR_UPPER_THRESH:
		reg = LM77_REG_THIGH;
		break;
	case SENSOR_ATTR_ALERT:
		reg = LM77_REG_TCRIT;
		break;
	case SENSOR_ATTR_HYSTERESIS:
		reg = LM77_REG_THYST;
		break;
	default:
		return -ENOTSUP;
	}

	lm77_sensor_value_to_temp(val, &temp);

	err = lm77_write_temp(dev, reg, temp);
	if (err < 0) {
		LOG_ERR("failed to write register 0x%02x (err %d)", reg, err);
		return err;
	}

	return 0;
}

static int lm77_attr_get(const struct device *dev, enum sensor_channel chan,
			 enum sensor_attribute attr, struct sensor_value *val)
{
	int16_t temp;
	uint8_t reg;
	int err;

	__ASSERT_NO_MSG(val != NULL);

	if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
		return -ENOTSUP;
	}

	switch (attr) {
	case SENSOR_ATTR_LOWER_THRESH:
		reg = LM77_REG_TLOW;
		break;
	case SENSOR_ATTR_UPPER_THRESH:
		reg = LM77_REG_THIGH;
		break;
	case SENSOR_ATTR_ALERT:
		reg = LM77_REG_TCRIT;
		break;
	case SENSOR_ATTR_HYSTERESIS:
		reg = LM77_REG_THYST;
		break;
	default:
		return -ENOTSUP;
	}

	err = lm77_read_temp(dev, reg, &temp);
	if (err < 0) {
		LOG_ERR("failed to read register 0x%02x (err %d)", reg, err);
		return err;
	}

	lm77_temp_to_sensor_value(temp, val);

	return 0;
}

#if LM77_TRIGGER_SUPPORT
static int lm77_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
			    sensor_trigger_handler_t handler)
{
	const struct lm77_config *config = dev->config;
	struct lm77_data *data = dev->data;
	gpio_flags_t flags;
	int err;

	__ASSERT_NO_MSG(trig != NULL);

	if (trig->type != SENSOR_TRIG_THRESHOLD || trig->chan != SENSOR_CHAN_AMBIENT_TEMP ||
	    config->int_gpio.port == NULL) {
		return -ENOTSUP;
	}

	if (handler != NULL) {
		flags = GPIO_INT_EDGE_TO_ACTIVE;
	} else {
		flags = GPIO_INT_DISABLE;
	}

	err = gpio_pin_interrupt_configure_dt(&config->int_gpio, flags);
	if (err < 0) {
		LOG_ERR("failed to configure INT GPIO IRQ (err %d)", err);
		return err;
	}

	data->trigger = trig;
	data->trigger_handler = handler;

	return 0;
}

static void lm77_trigger_work_handler(struct k_work *item)
{
	struct lm77_data *data = CONTAINER_OF(item, struct lm77_data, work);
	sensor_trigger_handler_t handler = data->trigger_handler;

	if (handler != NULL) {
		handler(data->dev, (struct sensor_trigger *)data->trigger);
	}
}

static void lm77_int_gpio_callback_handler(const struct device *port,
					   struct gpio_callback *cb,
					   gpio_port_pins_t pins)
{
	struct lm77_data *data = CONTAINER_OF(cb, struct lm77_data, int_gpio_cb);

	ARG_UNUSED(port);
	ARG_UNUSED(pins);

	k_work_submit_to_queue(&data->workq, &data->work);
}
#endif /* LM77_TRIGGER_SUPPORT */

static int lm77_sample_fetch(const struct device *dev, enum sensor_channel chan)
{
	struct lm77_data *data = dev->data;
	int16_t temp;
	int err;

	if (chan != SENSOR_CHAN_ALL && chan != SENSOR_CHAN_AMBIENT_TEMP) {
		return -ENOTSUP;
	}

	err = lm77_read_temp(dev, LM77_REG_TEMP, &temp);
	if (err < 0) {
		LOG_ERR("failed to read temperature (err %d)", err);
		return err;
	}

	data->temp = temp;

	return 0;
}

static int lm77_channel_get(const struct device *dev, enum sensor_channel chan,
			    struct sensor_value *val)
{
	struct lm77_data *data = dev->data;

	if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
		return -ENOTSUP;
	}

	lm77_temp_to_sensor_value(data->temp, val);

	return 0;
}

static const struct sensor_driver_api lm77_driver_api = {
	.attr_set = lm77_attr_set,
	.attr_get = lm77_attr_get,
#if LM77_TRIGGER_SUPPORT
	.trigger_set = lm77_trigger_set,
#endif /* LM77_TRIGGER_SUPPORT */
	.sample_fetch = lm77_sample_fetch,
	.channel_get = lm77_channel_get,
};

static int lm77_init(const struct device *dev)
{
	const struct lm77_config *config = dev->config;
	int err;
#if LM77_TRIGGER_SUPPORT
	struct lm77_data *data = dev->data;
#endif /* LM77_TRIGGER_SUPPORT */

	if (!device_is_ready(config->i2c.bus)) {
		LOG_ERR("I2c bus not ready");
		return -EINVAL;
	}

	err = lm77_write_config(dev, config->config_dt.reg);
	if (err < 0) {
		LOG_ERR("failed to write configuration (err %d)", err);
		return err;
	}

#if LM77_TRIGGER_SUPPORT
	data->dev = dev;
	k_work_queue_start(&data->workq, data->stack, K_THREAD_STACK_SIZEOF(data->stack),
			   CONFIG_LM77_TRIGGER_THREAD_PRIO, NULL);
	k_thread_name_set(&data->workq.thread, "lm77_trigger");
	k_work_init(&data->work, lm77_trigger_work_handler);

	if (config->int_gpio.port != NULL) {
		if (!device_is_ready(config->int_gpio.port)) {
			LOG_ERR("INT GPIO not ready");
			return -EINVAL;
		}

		err = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
		if (err < 0) {
			LOG_ERR("failed to configure INT GPIO (err %d)", err);
			return err;
		}

		gpio_init_callback(&data->int_gpio_cb, lm77_int_gpio_callback_handler,
				   BIT(config->int_gpio.pin));

		err = gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb);
		if (err < 0) {
			LOG_ERR("failed to add INT GPIO callback (err %d)", err);
			return err;
		}
	}
#endif /* LM77_TRIGGER_SUPPORT */

	return 0;
}

#ifdef CONFIG_PM_DEVICE
static int lm77_pm_action(const struct device *dev,
			  enum pm_device_action action)
{
	const struct lm77_config *config = dev->config;
	union lm77_reg_config creg = config->config_dt;
	int err;

	switch (action) {
	case PM_DEVICE_ACTION_SUSPEND:
		creg.shutdown = 1;
		break;
	case PM_DEVICE_ACTION_RESUME:
		creg.shutdown = 0;
		break;
	default:
		return -ENOTSUP;
	}

	err = lm77_write_config(dev, creg.reg);
	if (err < 0) {
		LOG_ERR("failed to write configuration (err %d)", err);
		return err;
	}

	return 0;
}
#endif /* CONFIG_PM_DEVICE */

#if LM77_TRIGGER_SUPPORT
#define LM77_INT_GPIO_INIT(n) .int_gpio = GPIO_DT_SPEC_INST_GET_OR(n, int_gpios, { 0 })
#else /* LM77_TRIGGER_SUPPORT */
#define LM77_INT_GPIO_INIT(n)
#endif /* ! LM77_TRIGGER_SUPPORT */

#define LM77_INIT(n)							\
	static struct lm77_data lm77_data_##n;				\
									\
	static const struct lm77_config lm77_config_##n = {		\
		.i2c = I2C_DT_SPEC_INST_GET(n),				\
		.config_dt = {						\
			.shutdown = 0,					\
			.int_mode = DT_INST_NODE_HAS_PROP(n, int_gpios), \
			.tcrita_pol = DT_INST_PROP(n, tcrita_inverted), \
			.int_pol = DT_INST_PROP(n, int_inverted),	\
			.fault_queue = DT_INST_PROP(n, enable_fault_queue), \
			.reserved = 0,					\
		},							\
		LM77_INT_GPIO_INIT(n)					\
	};								\
									\
	PM_DEVICE_DT_INST_DEFINE(n, lm77_pm_action);			\
									\
	DEVICE_DT_INST_DEFINE(n, lm77_init,				\
			      PM_DEVICE_DT_INST_GET(n),			\
			      &lm77_data_##n,				\
			      &lm77_config_##n, POST_KERNEL,		\
			      CONFIG_SENSOR_INIT_PRIORITY,		\
			      &lm77_driver_api);

DT_INST_FOREACH_STATUS_OKAY(LM77_INIT)