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/tmp11x.h>
12 
13 #define DT_DRV_COMPAT ti_tmp11x_eeprom
14 
15 struct eeprom_tmp11x_config {
16 	const struct device *parent;
17 };
18 
eeprom_tmp11x_size(const struct device * dev)19 static size_t eeprom_tmp11x_size(const struct device *dev)
20 {
21 	return EEPROM_TMP11X_SIZE;
22 }
23 
eeprom_tmp11x_write(const struct device * dev,off_t offset,const void * data,size_t len)24 static int eeprom_tmp11x_write(const struct device *dev, off_t offset,
25 			       const void *data, size_t len)
26 {
27 	const struct eeprom_tmp11x_config *config = dev->config;
28 
29 	return tmp11x_eeprom_write(config->parent, offset, data, len);
30 }
31 
eeprom_tmp11x_read(const struct device * dev,off_t offset,void * data,size_t len)32 static int eeprom_tmp11x_read(const struct device *dev, off_t offset, void *data,
33 			      size_t len)
34 {
35 	const struct eeprom_tmp11x_config *config = dev->config;
36 
37 	return tmp11x_eeprom_read(config->parent, offset, data, len);
38 }
39 
eeprom_tmp11x_init(const struct device * dev)40 static int eeprom_tmp11x_init(const struct device *dev)
41 {
42 	const struct eeprom_tmp11x_config *config = dev->config;
43 
44 	if (!device_is_ready(config->parent)) {
45 		return -ENODEV;
46 	}
47 
48 	return 0;
49 }
50 
51 static DEVICE_API(eeprom, eeprom_tmp11x_api) = {
52 	.read = eeprom_tmp11x_read,
53 	.write = eeprom_tmp11x_write,
54 	.size = eeprom_tmp11x_size,
55 };
56 
57 #define DEFINE_TMP11X(_num)						       \
58 	static const struct eeprom_tmp11x_config eeprom_tmp11x_config##_num = { \
59 		.parent =  DEVICE_DT_GET(DT_INST_BUS(_num))		       \
60 	};								       \
61 	DEVICE_DT_INST_DEFINE(_num, eeprom_tmp11x_init, NULL,		       \
62 			      NULL, &eeprom_tmp11x_config##_num, POST_KERNEL,   \
63 			      CONFIG_EEPROM_INIT_PRIORITY, &eeprom_tmp11x_api);
64 
65 DT_INST_FOREACH_STATUS_OKAY(DEFINE_TMP11X);
66