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