1 /*
2  * Copyright (c) 2020 Innoseis B.V
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <errno.h>
8 
9 #include <zephyr/devicetree.h>
10 #include <zephyr/drivers/eeprom.h>
11 #include <zephyr/drivers/sensor/tmp116.h>
12 
13 #define DT_DRV_COMPAT ti_tmp116_eeprom
14 
15 struct eeprom_tmp116_config {
16 	const struct device *parent;
17 };
18 
19 BUILD_ASSERT(CONFIG_EEPROM_INIT_PRIORITY >
20 	     CONFIG_SENSOR_INIT_PRIORITY,
21 	     "TMP116 eeprom driver must be initialized after TMP116 sensor "
22 	     "driver");
23 
eeprom_tmp116_size(const struct device * dev)24 static size_t eeprom_tmp116_size(const struct device *dev)
25 {
26 	return EEPROM_TMP116_SIZE;
27 }
28 
eeprom_tmp116_write(const struct device * dev,off_t offset,const void * data,size_t len)29 static int eeprom_tmp116_write(const struct device *dev, off_t offset,
30 			       const void *data, size_t len)
31 {
32 	const struct eeprom_tmp116_config *config = dev->config;
33 
34 	return tmp116_eeprom_write(config->parent, offset, data, len);
35 }
36 
eeprom_tmp116_read(const struct device * dev,off_t offset,void * data,size_t len)37 static int eeprom_tmp116_read(const struct device *dev, off_t offset, void *data,
38 			      size_t len)
39 {
40 	const struct eeprom_tmp116_config *config = dev->config;
41 
42 	return tmp116_eeprom_read(config->parent, offset, data, len);
43 }
44 
eeprom_tmp116_init(const struct device * dev)45 static int eeprom_tmp116_init(const struct device *dev)
46 {
47 	const struct eeprom_tmp116_config *config = dev->config;
48 
49 	if (!device_is_ready(config->parent)) {
50 		return -ENODEV;
51 	}
52 
53 	return 0;
54 }
55 
56 static const struct eeprom_driver_api eeprom_tmp116_api = {
57 	.read = eeprom_tmp116_read,
58 	.write = eeprom_tmp116_write,
59 	.size = eeprom_tmp116_size,
60 };
61 
62 #define DEFINE_TMP116(_num)						       \
63 	static const struct eeprom_tmp116_config eeprom_tmp116_config##_num = { \
64 		.parent =  DEVICE_DT_GET(DT_INST_BUS(_num))		       \
65 	};								       \
66 	DEVICE_DT_INST_DEFINE(_num, eeprom_tmp116_init, NULL,		       \
67 			      NULL, &eeprom_tmp116_config##_num, POST_KERNEL,   \
68 			      CONFIG_EEPROM_INIT_PRIORITY, &eeprom_tmp116_api);
69 
70 DT_INST_FOREACH_STATUS_OKAY(DEFINE_TMP116);
71