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