David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | |
| 15 | struct nvmem_device; |
| 16 | struct nvmem_cell_info; |
| 17 | typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset, |
| 18 | void *val, size_t bytes); |
| 19 | typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset, |
| 20 | void *val, size_t bytes); |
| 21 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 22 | enum nvmem_type { |
| 23 | NVMEM_TYPE_UNKNOWN = 0, |
| 24 | NVMEM_TYPE_EEPROM, |
| 25 | NVMEM_TYPE_OTP, |
| 26 | NVMEM_TYPE_BATTERY_BACKED, |
| 27 | }; |
| 28 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 38 | * @type: Type of the nvmem storage |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 39 | * @read_only: Device is read-only. |
| 40 | * @root_only: Device is accessibly to root only. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 41 | * @no_of_node: Device should not use the parent's of_node even if it's !NULL. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | */ |
| 56 | struct 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; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 63 | enum nvmem_type type; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 64 | bool read_only; |
| 65 | bool root_only; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 66 | bool no_of_node; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 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 | */ |
| 90 | struct 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 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 97 | #if IS_ENABLED(CONFIG_NVMEM) |
| 98 | |
| 99 | struct nvmem_device *nvmem_register(const struct nvmem_config *cfg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 100 | void nvmem_unregister(struct nvmem_device *nvmem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 101 | |
| 102 | struct nvmem_device *devm_nvmem_register(struct device *dev, |
| 103 | const struct nvmem_config *cfg); |
| 104 | |
| 105 | int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem); |
| 106 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 107 | void nvmem_add_cell_table(struct nvmem_cell_table *table); |
| 108 | void nvmem_del_cell_table(struct nvmem_cell_table *table); |
| 109 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 110 | #else |
| 111 | |
| 112 | static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c) |
| 113 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 114 | return ERR_PTR(-EOPNOTSUPP); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 115 | } |
| 116 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 117 | static inline void nvmem_unregister(struct nvmem_device *nvmem) {} |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 118 | |
| 119 | static inline struct nvmem_device * |
| 120 | devm_nvmem_register(struct device *dev, const struct nvmem_config *c) |
| 121 | { |
| 122 | return nvmem_register(c); |
| 123 | } |
| 124 | |
| 125 | static inline int |
| 126 | devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem) |
| 127 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 128 | return -EOPNOTSUPP; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 129 | } |
| 130 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 131 | static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {} |
| 132 | static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {} |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 133 | |
| 134 | #endif /* CONFIG_NVMEM */ |
| 135 | #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */ |