blob: 06409a6c40bcb2a0ca7e8ba9408fce159b0d9b6a [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
8
9#ifndef _LINUX_NVMEM_PROVIDER_H
10#define _LINUX_NVMEM_PROVIDER_H
11
12#include <linux/err.h>
13#include <linux/errno.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020014#include <linux/gpio/consumer.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015
16struct nvmem_device;
17struct nvmem_cell_info;
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);
22
David Brazdil0f672f62019-12-10 10:32:29 +000023enum nvmem_type {
24 NVMEM_TYPE_UNKNOWN = 0,
25 NVMEM_TYPE_EEPROM,
26 NVMEM_TYPE_OTP,
27 NVMEM_TYPE_BATTERY_BACKED,
28};
29
Olivier Deprez157378f2022-04-04 15:47:50 +020030#define NVMEM_DEVID_NONE (-1)
31#define NVMEM_DEVID_AUTO (-2)
32
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000033/**
34 * struct nvmem_config - NVMEM device configuration
35 *
36 * @dev: Parent device.
37 * @name: Optional name.
38 * @id: Optional device ID used in full name. Ignored if name is NULL.
39 * @owner: Pointer to exporter module. Used for refcounting.
40 * @cells: Optional array of pre-defined NVMEM cells.
41 * @ncells: Number of elements in cells.
David Brazdil0f672f62019-12-10 10:32:29 +000042 * @type: Type of the nvmem storage
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043 * @read_only: Device is read-only.
44 * @root_only: Device is accessibly to root only.
David Brazdil0f672f62019-12-10 10:32:29 +000045 * @no_of_node: Device should not use the parent's of_node even if it's !NULL.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000046 * @reg_read: Callback to read data.
47 * @reg_write: Callback to write data.
48 * @size: Device size.
49 * @word_size: Minimum read/write access granularity.
50 * @stride: Minimum read/write access stride.
51 * @priv: User context passed to read/write callbacks.
Olivier Deprez157378f2022-04-04 15:47:50 +020052 * @wp-gpio: Write protect pin
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053 *
54 * Note: A default "nvmem<id>" name will be assigned to the device if
55 * no name is specified in its configuration. In such case "<id>" is
56 * generated with ida_simple_get() and provided id field is ignored.
57 *
58 * Note: Specifying name and setting id to -1 implies a unique device
59 * whose name is provided as-is (kept unaltered).
60 */
61struct nvmem_config {
62 struct device *dev;
63 const char *name;
64 int id;
65 struct module *owner;
Olivier Deprez157378f2022-04-04 15:47:50 +020066 struct gpio_desc *wp_gpio;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067 const struct nvmem_cell_info *cells;
68 int ncells;
David Brazdil0f672f62019-12-10 10:32:29 +000069 enum nvmem_type type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070 bool read_only;
71 bool root_only;
David Brazdil0f672f62019-12-10 10:32:29 +000072 bool no_of_node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073 nvmem_reg_read_t reg_read;
74 nvmem_reg_write_t reg_write;
75 int size;
76 int word_size;
77 int stride;
78 void *priv;
79 /* To be only used by old driver/misc/eeprom drivers */
80 bool compat;
81 struct device *base_dev;
82};
83
David Brazdil0f672f62019-12-10 10:32:29 +000084/**
85 * struct nvmem_cell_table - NVMEM cell definitions for given provider
86 *
87 * @nvmem_name: Provider name.
88 * @cells: Array of cell definitions.
89 * @ncells: Number of cell definitions in the array.
90 * @node: List node.
91 *
92 * This structure together with related helper functions is provided for users
93 * that don't can't access the nvmem provided structure but wish to register
94 * cell definitions for it e.g. board files registering an EEPROM device.
95 */
96struct nvmem_cell_table {
97 const char *nvmem_name;
98 const struct nvmem_cell_info *cells;
99 size_t ncells;
100 struct list_head node;
101};
102
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000103#if IS_ENABLED(CONFIG_NVMEM)
104
105struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
David Brazdil0f672f62019-12-10 10:32:29 +0000106void nvmem_unregister(struct nvmem_device *nvmem);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000107
108struct nvmem_device *devm_nvmem_register(struct device *dev,
109 const struct nvmem_config *cfg);
110
111int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem);
112
David Brazdil0f672f62019-12-10 10:32:29 +0000113void nvmem_add_cell_table(struct nvmem_cell_table *table);
114void nvmem_del_cell_table(struct nvmem_cell_table *table);
115
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116#else
117
118static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
119{
David Brazdil0f672f62019-12-10 10:32:29 +0000120 return ERR_PTR(-EOPNOTSUPP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121}
122
David Brazdil0f672f62019-12-10 10:32:29 +0000123static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000124
125static inline struct nvmem_device *
126devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
127{
128 return nvmem_register(c);
129}
130
131static inline int
132devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
133{
David Brazdil0f672f62019-12-10 10:32:29 +0000134 return -EOPNOTSUPP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000135}
136
David Brazdil0f672f62019-12-10 10:32:29 +0000137static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
138static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000139
140#endif /* CONFIG_NVMEM */
141#endif /* ifndef _LINUX_NVMEM_PROVIDER_H */