Bartosz Golaszewski | b1c1db9 | 2018-09-21 06:40:20 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [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> |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #ifndef _LINUX_NVMEM_PROVIDER_H |
| 10 | #define _LINUX_NVMEM_PROVIDER_H |
| 11 | |
Arnd Bergmann | 42a6e09 | 2017-07-10 13:22:50 +0200 | [diff] [blame] | 12 | #include <linux/err.h> |
| 13 | #include <linux/errno.h> |
| 14 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 15 | struct nvmem_device; |
| 16 | struct nvmem_cell_info; |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 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); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 21 | |
Alexandre Belloni | 1668845 | 2018-11-30 11:53:20 +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 | |
Andrey Smirnov | 0b2ed74 | 2018-03-09 14:46: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. |
Alexandre Belloni | 1668845 | 2018-11-30 11:53:20 +0000 | [diff] [blame] | 38 | * @type: Type of the nvmem storage |
Andrey Smirnov | 0b2ed74 | 2018-03-09 14:46:55 +0000 | [diff] [blame] | 39 | * @read_only: Device is read-only. |
| 40 | * @root_only: Device is accessibly to root only. |
Bartosz Golaszewski | 517f14d | 2018-11-30 11:53:25 +0000 | [diff] [blame] | 41 | * @no_of_node: Device should not use the parent's of_node even if it's !NULL. |
Andrey Smirnov | 0b2ed74 | 2018-03-09 14:46: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. |
Andrey Smirnov | fd0f490 | 2018-03-09 14:46:56 +0000 | [diff] [blame] | 52 | * |
| 53 | * Note: Specifying name and setting id to -1 implies a unique device |
| 54 | * whose name is provided as-is (kept unaltered). |
Andrey Smirnov | 0b2ed74 | 2018-03-09 14:46:55 +0000 | [diff] [blame] | 55 | */ |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 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; |
Alexandre Belloni | 1668845 | 2018-11-30 11:53:20 +0000 | [diff] [blame] | 63 | enum nvmem_type type; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 64 | bool read_only; |
Andrew Lunn | 811b0d6 | 2016-02-26 20:59:18 +0100 | [diff] [blame] | 65 | bool root_only; |
Bartosz Golaszewski | 517f14d | 2018-11-30 11:53:25 +0000 | [diff] [blame] | 66 | bool no_of_node; |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [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; |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 73 | /* To be only used by old driver/misc/eeprom drivers */ |
| 74 | bool compat; |
| 75 | struct device *base_dev; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 76 | }; |
| 77 | |
Bartosz Golaszewski | b985f4c | 2018-09-21 06:40:15 -0700 | [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 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 97 | #if IS_ENABLED(CONFIG_NVMEM) |
| 98 | |
| 99 | struct nvmem_device *nvmem_register(const struct nvmem_config *cfg); |
Bartosz Golaszewski | bf58e88 | 2018-09-21 06:40:13 -0700 | [diff] [blame] | 100 | void nvmem_unregister(struct nvmem_device *nvmem); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 101 | |
Andrey Smirnov | f1f50ec | 2018-03-09 14:46:57 +0000 | [diff] [blame] | 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 | |
Bartosz Golaszewski | b985f4c | 2018-09-21 06:40:15 -0700 | [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 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 110 | #else |
| 111 | |
| 112 | static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c) |
| 113 | { |
Bartosz Golaszewski | 20167b7 | 2018-09-21 06:40:22 -0700 | [diff] [blame] | 114 | return ERR_PTR(-EOPNOTSUPP); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Bartosz Golaszewski | bf58e88 | 2018-09-21 06:40:13 -0700 | [diff] [blame] | 117 | static inline void nvmem_unregister(struct nvmem_device *nvmem) {} |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 118 | |
Andrey Smirnov | f1f50ec | 2018-03-09 14:46:57 +0000 | [diff] [blame] | 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 | { |
Bartosz Golaszewski | 20167b7 | 2018-09-21 06:40:22 -0700 | [diff] [blame] | 128 | return -EOPNOTSUPP; |
Andrew Lunn | b3db17e | 2018-05-11 12:06:56 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Bartosz Golaszewski | b985f4c | 2018-09-21 06:40:15 -0700 | [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) {} |
Andrey Smirnov | f1f50ec | 2018-03-09 14:46:57 +0000 | [diff] [blame] | 133 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 134 | #endif /* CONFIG_NVMEM */ |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 135 | #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */ |