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 | |
Andrey Smirnov | 0b2ed74 | 2018-03-09 14:46:55 +0000 | [diff] [blame] | 22 | /** |
| 23 | * struct nvmem_config - NVMEM device configuration |
| 24 | * |
| 25 | * @dev: Parent device. |
| 26 | * @name: Optional name. |
| 27 | * @id: Optional device ID used in full name. Ignored if name is NULL. |
| 28 | * @owner: Pointer to exporter module. Used for refcounting. |
| 29 | * @cells: Optional array of pre-defined NVMEM cells. |
| 30 | * @ncells: Number of elements in cells. |
| 31 | * @read_only: Device is read-only. |
| 32 | * @root_only: Device is accessibly to root only. |
| 33 | * @reg_read: Callback to read data. |
| 34 | * @reg_write: Callback to write data. |
| 35 | * @size: Device size. |
| 36 | * @word_size: Minimum read/write access granularity. |
| 37 | * @stride: Minimum read/write access stride. |
| 38 | * @priv: User context passed to read/write callbacks. |
| 39 | * |
| 40 | * Note: A default "nvmem<id>" name will be assigned to the device if |
| 41 | * no name is specified in its configuration. In such case "<id>" is |
| 42 | * generated with ida_simple_get() and provided id field is ignored. |
Andrey Smirnov | fd0f490 | 2018-03-09 14:46:56 +0000 | [diff] [blame] | 43 | * |
| 44 | * Note: Specifying name and setting id to -1 implies a unique device |
| 45 | * whose name is provided as-is (kept unaltered). |
Andrey Smirnov | 0b2ed74 | 2018-03-09 14:46:55 +0000 | [diff] [blame] | 46 | */ |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 47 | struct nvmem_config { |
| 48 | struct device *dev; |
| 49 | const char *name; |
| 50 | int id; |
| 51 | struct module *owner; |
| 52 | const struct nvmem_cell_info *cells; |
| 53 | int ncells; |
| 54 | bool read_only; |
Andrew Lunn | 811b0d6 | 2016-02-26 20:59:18 +0100 | [diff] [blame] | 55 | bool root_only; |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 56 | nvmem_reg_read_t reg_read; |
| 57 | nvmem_reg_write_t reg_write; |
| 58 | int size; |
| 59 | int word_size; |
| 60 | int stride; |
| 61 | void *priv; |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 62 | /* To be only used by old driver/misc/eeprom drivers */ |
| 63 | bool compat; |
| 64 | struct device *base_dev; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 65 | }; |
| 66 | |
Bartosz Golaszewski | b985f4c | 2018-09-21 06:40:15 -0700 | [diff] [blame] | 67 | /** |
| 68 | * struct nvmem_cell_table - NVMEM cell definitions for given provider |
| 69 | * |
| 70 | * @nvmem_name: Provider name. |
| 71 | * @cells: Array of cell definitions. |
| 72 | * @ncells: Number of cell definitions in the array. |
| 73 | * @node: List node. |
| 74 | * |
| 75 | * This structure together with related helper functions is provided for users |
| 76 | * that don't can't access the nvmem provided structure but wish to register |
| 77 | * cell definitions for it e.g. board files registering an EEPROM device. |
| 78 | */ |
| 79 | struct nvmem_cell_table { |
| 80 | const char *nvmem_name; |
| 81 | const struct nvmem_cell_info *cells; |
| 82 | size_t ncells; |
| 83 | struct list_head node; |
| 84 | }; |
| 85 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 86 | #if IS_ENABLED(CONFIG_NVMEM) |
| 87 | |
| 88 | struct nvmem_device *nvmem_register(const struct nvmem_config *cfg); |
Bartosz Golaszewski | bf58e88 | 2018-09-21 06:40:13 -0700 | [diff] [blame] | 89 | void nvmem_unregister(struct nvmem_device *nvmem); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 90 | |
Andrey Smirnov | f1f50ec | 2018-03-09 14:46:57 +0000 | [diff] [blame] | 91 | struct nvmem_device *devm_nvmem_register(struct device *dev, |
| 92 | const struct nvmem_config *cfg); |
| 93 | |
| 94 | int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem); |
| 95 | |
Bartosz Golaszewski | b985f4c | 2018-09-21 06:40:15 -0700 | [diff] [blame] | 96 | void nvmem_add_cell_table(struct nvmem_cell_table *table); |
| 97 | void nvmem_del_cell_table(struct nvmem_cell_table *table); |
| 98 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 99 | #else |
| 100 | |
| 101 | static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c) |
| 102 | { |
Bartosz Golaszewski | 20167b7 | 2018-09-21 06:40:22 -0700 | [diff] [blame] | 103 | return ERR_PTR(-EOPNOTSUPP); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Bartosz Golaszewski | bf58e88 | 2018-09-21 06:40:13 -0700 | [diff] [blame] | 106 | static inline void nvmem_unregister(struct nvmem_device *nvmem) {} |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 107 | |
Andrey Smirnov | f1f50ec | 2018-03-09 14:46:57 +0000 | [diff] [blame] | 108 | static inline struct nvmem_device * |
| 109 | devm_nvmem_register(struct device *dev, const struct nvmem_config *c) |
| 110 | { |
| 111 | return nvmem_register(c); |
| 112 | } |
| 113 | |
| 114 | static inline int |
| 115 | devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem) |
| 116 | { |
Bartosz Golaszewski | 20167b7 | 2018-09-21 06:40:22 -0700 | [diff] [blame] | 117 | return -EOPNOTSUPP; |
Andrew Lunn | b3db17e | 2018-05-11 12:06:56 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Bartosz Golaszewski | b985f4c | 2018-09-21 06:40:15 -0700 | [diff] [blame] | 120 | static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {} |
| 121 | static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {} |
Andrey Smirnov | f1f50ec | 2018-03-09 14:46:57 +0000 | [diff] [blame] | 122 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 123 | #endif /* CONFIG_NVMEM */ |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 124 | #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */ |