1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * nvmem framework provider.
4  *
5  * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
6  * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
7  */
8 
9 #ifndef _LINUX_NVMEM_PROVIDER_H
10 #define _LINUX_NVMEM_PROVIDER_H
11 
12 #include <linux/err.h>
13 #include <linux/errno.h>
14 #include <linux/gpio/consumer.h>
15 
16 struct nvmem_device;
17 struct nvmem_cell_info;
18 typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset,
19 				void *val, size_t bytes);
20 typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
21 				 void *val, size_t bytes);
22 
23 enum nvmem_type {
24 	NVMEM_TYPE_UNKNOWN = 0,
25 	NVMEM_TYPE_EEPROM,
26 	NVMEM_TYPE_OTP,
27 	NVMEM_TYPE_BATTERY_BACKED,
28 	NVMEM_TYPE_FRAM,
29 };
30 
31 #define NVMEM_DEVID_NONE	(-1)
32 #define NVMEM_DEVID_AUTO	(-2)
33 
34 /**
35  * struct nvmem_keepout - NVMEM register keepout range.
36  *
37  * @start:	The first byte offset to avoid.
38  * @end:	One beyond the last byte offset to avoid.
39  * @value:	The byte to fill reads with for this region.
40  */
41 struct nvmem_keepout {
42 	unsigned int start;
43 	unsigned int end;
44 	unsigned char value;
45 };
46 
47 /**
48  * struct nvmem_config - NVMEM device configuration
49  *
50  * @dev:	Parent device.
51  * @name:	Optional name.
52  * @id:		Optional device ID used in full name. Ignored if name is NULL.
53  * @owner:	Pointer to exporter module. Used for refcounting.
54  * @cells:	Optional array of pre-defined NVMEM cells.
55  * @ncells:	Number of elements in cells.
56  * @keepout:	Optional array of keepout ranges (sorted ascending by start).
57  * @nkeepout:	Number of elements in the keepout array.
58  * @type:	Type of the nvmem storage
59  * @read_only:	Device is read-only.
60  * @root_only:	Device is accessibly to root only.
61  * @of_node:	If given, this will be used instead of the parent's of_node.
62  * @no_of_node:	Device should not use the parent's of_node even if it's !NULL.
63  * @reg_read:	Callback to read data.
64  * @reg_write:	Callback to write data.
65  * @size:	Device size.
66  * @word_size:	Minimum read/write access granularity.
67  * @stride:	Minimum read/write access stride.
68  * @priv:	User context passed to read/write callbacks.
69  * @wp-gpio:   Write protect pin
70  *
71  * Note: A default "nvmem<id>" name will be assigned to the device if
72  * no name is specified in its configuration. In such case "<id>" is
73  * generated with ida_simple_get() and provided id field is ignored.
74  *
75  * Note: Specifying name and setting id to -1 implies a unique device
76  * whose name is provided as-is (kept unaltered).
77  */
78 struct nvmem_config {
79 	struct device		*dev;
80 	const char		*name;
81 	int			id;
82 	struct module		*owner;
83 	struct gpio_desc	*wp_gpio;
84 	const struct nvmem_cell_info	*cells;
85 	int			ncells;
86 	const struct nvmem_keepout *keepout;
87 	unsigned int		nkeepout;
88 	enum nvmem_type		type;
89 	bool			read_only;
90 	bool			root_only;
91 	struct device_node	*of_node;
92 	bool			no_of_node;
93 	nvmem_reg_read_t	reg_read;
94 	nvmem_reg_write_t	reg_write;
95 	int	size;
96 	int	word_size;
97 	int	stride;
98 	void	*priv;
99 	/* To be only used by old driver/misc/eeprom drivers */
100 	bool			compat;
101 	struct device		*base_dev;
102 };
103 
104 /**
105  * struct nvmem_cell_table - NVMEM cell definitions for given provider
106  *
107  * @nvmem_name:		Provider name.
108  * @cells:		Array of cell definitions.
109  * @ncells:		Number of cell definitions in the array.
110  * @node:		List node.
111  *
112  * This structure together with related helper functions is provided for users
113  * that don't can't access the nvmem provided structure but wish to register
114  * cell definitions for it e.g. board files registering an EEPROM device.
115  */
116 struct nvmem_cell_table {
117 	const char		*nvmem_name;
118 	const struct nvmem_cell_info	*cells;
119 	size_t			ncells;
120 	struct list_head	node;
121 };
122 
123 #if IS_ENABLED(CONFIG_NVMEM)
124 
125 struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
126 void nvmem_unregister(struct nvmem_device *nvmem);
127 
128 struct nvmem_device *devm_nvmem_register(struct device *dev,
129 					 const struct nvmem_config *cfg);
130 
131 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem);
132 
133 void nvmem_add_cell_table(struct nvmem_cell_table *table);
134 void nvmem_del_cell_table(struct nvmem_cell_table *table);
135 
136 #else
137 
nvmem_register(const struct nvmem_config * c)138 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
139 {
140 	return ERR_PTR(-EOPNOTSUPP);
141 }
142 
nvmem_unregister(struct nvmem_device * nvmem)143 static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
144 
145 static inline struct nvmem_device *
devm_nvmem_register(struct device * dev,const struct nvmem_config * c)146 devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
147 {
148 	return nvmem_register(c);
149 }
150 
151 static inline int
devm_nvmem_unregister(struct device * dev,struct nvmem_device * nvmem)152 devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
153 {
154 	return -EOPNOTSUPP;
155 }
156 
nvmem_add_cell_table(struct nvmem_cell_table * table)157 static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
nvmem_del_cell_table(struct nvmem_cell_table * table)158 static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
159 
160 #endif /* CONFIG_NVMEM */
161 #endif  /* ifndef _LINUX_NVMEM_PROVIDER_H */
162