blob: 5c9f205cac8f14cd85bc3b4a69df843278c4e2eb [file] [log] [blame]
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +01001/*
2 * nvmem framework provider.
3 *
4 * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
5 * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
6 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#ifndef _LINUX_NVMEM_PROVIDER_H
13#define _LINUX_NVMEM_PROVIDER_H
14
Arnd Bergmann42a6e092017-07-10 13:22:50 +020015#include <linux/err.h>
16#include <linux/errno.h>
17
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010018struct nvmem_device;
19struct nvmem_cell_info;
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +010020typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset,
21 void *val, size_t bytes);
22typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
23 void *val, size_t bytes);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010024
Andrey Smirnov0b2ed742018-03-09 14:46:55 +000025/**
26 * struct nvmem_config - NVMEM device configuration
27 *
28 * @dev: Parent device.
29 * @name: Optional name.
30 * @id: Optional device ID used in full name. Ignored if name is NULL.
31 * @owner: Pointer to exporter module. Used for refcounting.
32 * @cells: Optional array of pre-defined NVMEM cells.
33 * @ncells: Number of elements in cells.
34 * @read_only: Device is read-only.
35 * @root_only: Device is accessibly to root only.
36 * @reg_read: Callback to read data.
37 * @reg_write: Callback to write data.
38 * @size: Device size.
39 * @word_size: Minimum read/write access granularity.
40 * @stride: Minimum read/write access stride.
41 * @priv: User context passed to read/write callbacks.
42 *
43 * Note: A default "nvmem<id>" name will be assigned to the device if
44 * no name is specified in its configuration. In such case "<id>" is
45 * generated with ida_simple_get() and provided id field is ignored.
Andrey Smirnovfd0f4902018-03-09 14:46:56 +000046 *
47 * Note: Specifying name and setting id to -1 implies a unique device
48 * whose name is provided as-is (kept unaltered).
Andrey Smirnov0b2ed742018-03-09 14:46:55 +000049 */
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010050struct nvmem_config {
51 struct device *dev;
52 const char *name;
53 int id;
54 struct module *owner;
55 const struct nvmem_cell_info *cells;
56 int ncells;
57 bool read_only;
Andrew Lunn811b0d62016-02-26 20:59:18 +010058 bool root_only;
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +010059 nvmem_reg_read_t reg_read;
60 nvmem_reg_write_t reg_write;
61 int size;
62 int word_size;
63 int stride;
64 void *priv;
Andrew Lunnb6c217a2016-02-26 20:59:19 +010065 /* To be only used by old driver/misc/eeprom drivers */
66 bool compat;
67 struct device *base_dev;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010068};
69
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -070070/**
71 * struct nvmem_cell_table - NVMEM cell definitions for given provider
72 *
73 * @nvmem_name: Provider name.
74 * @cells: Array of cell definitions.
75 * @ncells: Number of cell definitions in the array.
76 * @node: List node.
77 *
78 * This structure together with related helper functions is provided for users
79 * that don't can't access the nvmem provided structure but wish to register
80 * cell definitions for it e.g. board files registering an EEPROM device.
81 */
82struct nvmem_cell_table {
83 const char *nvmem_name;
84 const struct nvmem_cell_info *cells;
85 size_t ncells;
86 struct list_head node;
87};
88
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010089#if IS_ENABLED(CONFIG_NVMEM)
90
91struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
Bartosz Golaszewskibf58e882018-09-21 06:40:13 -070092void nvmem_unregister(struct nvmem_device *nvmem);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010093
Andrey Smirnovf1f50ec2018-03-09 14:46:57 +000094struct nvmem_device *devm_nvmem_register(struct device *dev,
95 const struct nvmem_config *cfg);
96
97int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem);
98
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -070099void nvmem_add_cell_table(struct nvmem_cell_table *table);
100void nvmem_del_cell_table(struct nvmem_cell_table *table);
101
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100102#else
103
104static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
105{
106 return ERR_PTR(-ENOSYS);
107}
108
Bartosz Golaszewskibf58e882018-09-21 06:40:13 -0700109static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100110
Andrey Smirnovf1f50ec2018-03-09 14:46:57 +0000111static inline struct nvmem_device *
112devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
113{
114 return nvmem_register(c);
115}
116
117static inline int
118devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
119{
Bartosz Golaszewskibf58e882018-09-21 06:40:13 -0700120 return -ENOSYS;
Andrew Lunnb3db17e2018-05-11 12:06:56 +0100121
122}
123
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -0700124static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
125static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
Andrey Smirnovf1f50ec2018-03-09 14:46:57 +0000126
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100127#endif /* CONFIG_NVMEM */
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100128#endif /* ifndef _LINUX_NVMEM_PROVIDER_H */