1 /*
2  * Copyright (c) 2019 Vestas Wind Systems A/S
3  *
4  * Heavily based on drivers/flash.h which is:
5  * Copyright (c) 2017 Nordic Semiconductor ASA
6  * Copyright (c) 2016 Intel Corporation
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 /**
12  * @file
13  * @brief Public API for EEPROM drivers
14  */
15 
16 #ifndef ZEPHYR_INCLUDE_DRIVERS_EEPROM_H_
17 #define ZEPHYR_INCLUDE_DRIVERS_EEPROM_H_
18 
19 /**
20  * @brief EEPROM Interface
21  * @defgroup eeprom_interface EEPROM Interface
22  * @ingroup io_interfaces
23  * @{
24  */
25 
26 #include <zephyr/types.h>
27 #include <stddef.h>
28 #include <sys/types.h>
29 #include <zephyr/device.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 typedef int (*eeprom_api_read)(const struct device *dev, off_t offset,
36 			       void *data,
37 			       size_t len);
38 typedef int (*eeprom_api_write)(const struct device *dev, off_t offset,
39 				const void *data, size_t len);
40 typedef size_t (*eeprom_api_size)(const struct device *dev);
41 
42 __subsystem struct eeprom_driver_api {
43 	eeprom_api_read read;
44 	eeprom_api_write write;
45 	eeprom_api_size size;
46 };
47 
48 /**
49  *  @brief Read data from EEPROM
50  *
51  *  @param dev EEPROM device
52  *  @param offset Address offset to read from.
53  *  @param data Buffer to store read data.
54  *  @param len Number of bytes to read.
55  *
56  *  @return 0 on success, negative errno code on failure.
57  */
58 __syscall int eeprom_read(const struct device *dev, off_t offset, void *data,
59 			  size_t len);
60 
z_impl_eeprom_read(const struct device * dev,off_t offset,void * data,size_t len)61 static inline int z_impl_eeprom_read(const struct device *dev, off_t offset,
62 				     void *data, size_t len)
63 {
64 	const struct eeprom_driver_api *api =
65 		(const struct eeprom_driver_api *)dev->api;
66 
67 	return api->read(dev, offset, data, len);
68 }
69 
70 /**
71  *  @brief Write data to EEPROM
72  *
73  *  @param dev EEPROM device
74  *  @param offset Address offset to write data to.
75  *  @param data Buffer with data to write.
76  *  @param len Number of bytes to write.
77  *
78  *  @return 0 on success, negative errno code on failure.
79  */
80 __syscall int eeprom_write(const struct device *dev, off_t offset,
81 			   const void *data,
82 			   size_t len);
83 
z_impl_eeprom_write(const struct device * dev,off_t offset,const void * data,size_t len)84 static inline int z_impl_eeprom_write(const struct device *dev, off_t offset,
85 				      const void *data, size_t len)
86 {
87 	const struct eeprom_driver_api *api =
88 		(const struct eeprom_driver_api *)dev->api;
89 
90 	return api->write(dev, offset, data, len);
91 }
92 
93 /**
94  *  @brief Get the size of the EEPROM in bytes
95  *
96  *  @param dev EEPROM device.
97  *
98  *  @return EEPROM size in bytes.
99  */
100 __syscall size_t eeprom_get_size(const struct device *dev);
101 
z_impl_eeprom_get_size(const struct device * dev)102 static inline size_t z_impl_eeprom_get_size(const struct device *dev)
103 {
104 	const struct eeprom_driver_api *api =
105 		(const struct eeprom_driver_api *)dev->api;
106 
107 	return api->size(dev);
108 }
109 
110 #ifdef __cplusplus
111 }
112 #endif
113 
114 /**
115  * @}
116  */
117 
118 #include <syscalls/eeprom.h>
119 
120 #endif /* ZEPHYR_INCLUDE_DRIVERS_EEPROM_H_ */
121