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
15struct nvmem_device;
16struct nvmem_cell_info;
17typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset,
18 void *val, size_t bytes);
19typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
20 void *val, size_t bytes);
21
22enum nvmem_type {
23 NVMEM_TYPE_UNKNOWN = 0,
24 NVMEM_TYPE_EEPROM,
25 NVMEM_TYPE_OTP,
26 NVMEM_TYPE_BATTERY_BACKED,
27};
28
29/**
30 * struct nvmem_config - NVMEM device configuration
31 *
32 * @dev: Parent device.
33 * @name: Optional name.
34 * @id: Optional device ID used in full name. Ignored if name is NULL.
35 * @owner: Pointer to exporter module. Used for refcounting.
36 * @cells: Optional array of pre-defined NVMEM cells.
37 * @ncells: Number of elements in cells.
38 * @type: Type of the nvmem storage
39 * @read_only: Device is read-only.
40 * @root_only: Device is accessibly to root only.
41 * @no_of_node: Device should not use the parent's of_node even if it's !NULL.
42 * @reg_read: Callback to read data.
43 * @reg_write: Callback to write data.
44 * @size: Device size.
45 * @word_size: Minimum read/write access granularity.
46 * @stride: Minimum read/write access stride.
47 * @priv: User context passed to read/write callbacks.
48 *
49 * Note: A default "nvmem<id>" name will be assigned to the device if
50 * no name is specified in its configuration. In such case "<id>" is
51 * generated with ida_simple_get() and provided id field is ignored.
52 *
53 * Note: Specifying name and setting id to -1 implies a unique device
54 * whose name is provided as-is (kept unaltered).
55 */
56struct nvmem_config {
57 struct device *dev;
58 const char *name;
59 int id;
60 struct module *owner;
61 const struct nvmem_cell_info *cells;
62 int ncells;
63 enum nvmem_type type;
64 bool read_only;
65 bool root_only;
66 bool no_of_node;
67 nvmem_reg_read_t reg_read;
68 nvmem_reg_write_t reg_write;
69 int size;
70 int word_size;
71 int stride;
72 void *priv;
73 /* To be only used by old driver/misc/eeprom drivers */
74 bool compat;
75 struct device *base_dev;
76};
77
78/**
79 * struct nvmem_cell_table - NVMEM cell definitions for given provider
80 *
81 * @nvmem_name: Provider name.
82 * @cells: Array of cell definitions.
83 * @ncells: Number of cell definitions in the array.
84 * @node: List node.
85 *
86 * This structure together with related helper functions is provided for users
87 * that don't can't access the nvmem provided structure but wish to register
88 * cell definitions for it e.g. board files registering an EEPROM device.
89 */
90struct nvmem_cell_table {
91 const char *nvmem_name;
92 const struct nvmem_cell_info *cells;
93 size_t ncells;
94 struct list_head node;
95};
96
97#if IS_ENABLED(CONFIG_NVMEM)
98
99struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
100void nvmem_unregister(struct nvmem_device *nvmem);
101
102struct nvmem_device *devm_nvmem_register(struct device *dev,
103 const struct nvmem_config *cfg);
104
105int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem);
106
107void nvmem_add_cell_table(struct nvmem_cell_table *table);
108void nvmem_del_cell_table(struct nvmem_cell_table *table);
109
110#else
111
112static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
113{
114 return ERR_PTR(-EOPNOTSUPP);
115}
116
117static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
118
119static inline struct nvmem_device *
120devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
121{
122 return nvmem_register(c);
123}
124
125static inline int
126devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
127{
128 return -EOPNOTSUPP;
129}
130
131static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
132static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
133
134#endif /* CONFIG_NVMEM */
135#endif /* ifndef _LINUX_NVMEM_PROVIDER_H */
136