blob: fe051323be0a2c0c44778426f3012731d03b9015 [file] [log] [blame]
Bartosz Golaszewskib1c1db92018-09-21 06:40:20 -07001/* SPDX-License-Identifier: GPL-2.0 */
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +01002/*
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>
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +01007 */
8
9#ifndef _LINUX_NVMEM_PROVIDER_H
10#define _LINUX_NVMEM_PROVIDER_H
11
Arnd Bergmann42a6e092017-07-10 13:22:50 +020012#include <linux/err.h>
13#include <linux/errno.h>
14
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010015struct nvmem_device;
16struct nvmem_cell_info;
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +010017typedef 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);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010021
Alexandre Belloni16688452018-11-30 11:53:20 +000022enum nvmem_type {
23 NVMEM_TYPE_UNKNOWN = 0,
24 NVMEM_TYPE_EEPROM,
25 NVMEM_TYPE_OTP,
26 NVMEM_TYPE_BATTERY_BACKED,
27};
28
Andrey Smirnov0b2ed742018-03-09 14:46:55 +000029/**
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.
Alexandre Belloni16688452018-11-30 11:53:20 +000038 * @type: Type of the nvmem storage
Andrey Smirnov0b2ed742018-03-09 14:46:55 +000039 * @read_only: Device is read-only.
40 * @root_only: Device is accessibly to root only.
Bartosz Golaszewski517f14d2018-11-30 11:53:25 +000041 * @no_of_node: Device should not use the parent's of_node even if it's !NULL.
Andrey Smirnov0b2ed742018-03-09 14:46:55 +000042 * @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.
Andrey Smirnovfd0f4902018-03-09 14:46:56 +000052 *
53 * Note: Specifying name and setting id to -1 implies a unique device
54 * whose name is provided as-is (kept unaltered).
Andrey Smirnov0b2ed742018-03-09 14:46:55 +000055 */
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010056struct 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;
Alexandre Belloni16688452018-11-30 11:53:20 +000063 enum nvmem_type type;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010064 bool read_only;
Andrew Lunn811b0d62016-02-26 20:59:18 +010065 bool root_only;
Bartosz Golaszewski517f14d2018-11-30 11:53:25 +000066 bool no_of_node;
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +010067 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;
Andrew Lunnb6c217a2016-02-26 20:59:19 +010073 /* To be only used by old driver/misc/eeprom drivers */
74 bool compat;
75 struct device *base_dev;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010076};
77
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -070078/**
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
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010097#if IS_ENABLED(CONFIG_NVMEM)
98
99struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
Bartosz Golaszewskibf58e882018-09-21 06:40:13 -0700100void nvmem_unregister(struct nvmem_device *nvmem);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100101
Andrey Smirnovf1f50ec2018-03-09 14:46:57 +0000102struct 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
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -0700107void nvmem_add_cell_table(struct nvmem_cell_table *table);
108void nvmem_del_cell_table(struct nvmem_cell_table *table);
109
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100110#else
111
112static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
113{
Bartosz Golaszewski20167b72018-09-21 06:40:22 -0700114 return ERR_PTR(-EOPNOTSUPP);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100115}
116
Bartosz Golaszewskibf58e882018-09-21 06:40:13 -0700117static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100118
Andrey Smirnovf1f50ec2018-03-09 14:46:57 +0000119static 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{
Bartosz Golaszewski20167b72018-09-21 06:40:22 -0700128 return -EOPNOTSUPP;
Andrew Lunnb3db17e2018-05-11 12:06:56 +0100129}
130
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -0700131static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
132static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
Andrey Smirnovf1f50ec2018-03-09 14:46:57 +0000133
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100134#endif /* CONFIG_NVMEM */
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100135#endif /* ifndef _LINUX_NVMEM_PROVIDER_H */