Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * nvmem framework provider.
0004  *
0005  * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
0006  * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
0007  */
0008 
0009 #ifndef _LINUX_NVMEM_PROVIDER_H
0010 #define _LINUX_NVMEM_PROVIDER_H
0011 
0012 #include <linux/err.h>
0013 #include <linux/errno.h>
0014 #include <linux/gpio/consumer.h>
0015 
0016 struct nvmem_device;
0017 struct nvmem_cell_info;
0018 typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset,
0019                 void *val, size_t bytes);
0020 typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
0021                  void *val, size_t bytes);
0022 /* used for vendor specific post processing of cell data */
0023 typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id, unsigned int offset,
0024                       void *buf, size_t bytes);
0025 
0026 enum nvmem_type {
0027     NVMEM_TYPE_UNKNOWN = 0,
0028     NVMEM_TYPE_EEPROM,
0029     NVMEM_TYPE_OTP,
0030     NVMEM_TYPE_BATTERY_BACKED,
0031     NVMEM_TYPE_FRAM,
0032 };
0033 
0034 #define NVMEM_DEVID_NONE    (-1)
0035 #define NVMEM_DEVID_AUTO    (-2)
0036 
0037 /**
0038  * struct nvmem_keepout - NVMEM register keepout range.
0039  *
0040  * @start:  The first byte offset to avoid.
0041  * @end:    One beyond the last byte offset to avoid.
0042  * @value:  The byte to fill reads with for this region.
0043  */
0044 struct nvmem_keepout {
0045     unsigned int start;
0046     unsigned int end;
0047     unsigned char value;
0048 };
0049 
0050 /**
0051  * struct nvmem_config - NVMEM device configuration
0052  *
0053  * @dev:    Parent device.
0054  * @name:   Optional name.
0055  * @id:     Optional device ID used in full name. Ignored if name is NULL.
0056  * @owner:  Pointer to exporter module. Used for refcounting.
0057  * @cells:  Optional array of pre-defined NVMEM cells.
0058  * @ncells: Number of elements in cells.
0059  * @keepout:    Optional array of keepout ranges (sorted ascending by start).
0060  * @nkeepout:   Number of elements in the keepout array.
0061  * @type:   Type of the nvmem storage
0062  * @read_only:  Device is read-only.
0063  * @root_only:  Device is accessibly to root only.
0064  * @of_node:    If given, this will be used instead of the parent's of_node.
0065  * @no_of_node: Device should not use the parent's of_node even if it's !NULL.
0066  * @reg_read:   Callback to read data.
0067  * @reg_write:  Callback to write data.
0068  * @cell_post_process:  Callback for vendor specific post processing of cell data
0069  * @size:   Device size.
0070  * @word_size:  Minimum read/write access granularity.
0071  * @stride: Minimum read/write access stride.
0072  * @priv:   User context passed to read/write callbacks.
0073  * @wp-gpio:    Write protect pin
0074  * @ignore_wp:  Write Protect pin is managed by the provider.
0075  *
0076  * Note: A default "nvmem<id>" name will be assigned to the device if
0077  * no name is specified in its configuration. In such case "<id>" is
0078  * generated with ida_simple_get() and provided id field is ignored.
0079  *
0080  * Note: Specifying name and setting id to -1 implies a unique device
0081  * whose name is provided as-is (kept unaltered).
0082  */
0083 struct nvmem_config {
0084     struct device       *dev;
0085     const char      *name;
0086     int         id;
0087     struct module       *owner;
0088     struct gpio_desc    *wp_gpio;
0089     const struct nvmem_cell_info    *cells;
0090     int         ncells;
0091     const struct nvmem_keepout *keepout;
0092     unsigned int        nkeepout;
0093     enum nvmem_type     type;
0094     bool            read_only;
0095     bool            root_only;
0096     bool            ignore_wp;
0097     struct device_node  *of_node;
0098     bool            no_of_node;
0099     nvmem_reg_read_t    reg_read;
0100     nvmem_reg_write_t   reg_write;
0101     nvmem_cell_post_process_t cell_post_process;
0102     int size;
0103     int word_size;
0104     int stride;
0105     void    *priv;
0106     /* To be only used by old driver/misc/eeprom drivers */
0107     bool            compat;
0108     struct device       *base_dev;
0109 };
0110 
0111 /**
0112  * struct nvmem_cell_table - NVMEM cell definitions for given provider
0113  *
0114  * @nvmem_name:     Provider name.
0115  * @cells:      Array of cell definitions.
0116  * @ncells:     Number of cell definitions in the array.
0117  * @node:       List node.
0118  *
0119  * This structure together with related helper functions is provided for users
0120  * that don't can't access the nvmem provided structure but wish to register
0121  * cell definitions for it e.g. board files registering an EEPROM device.
0122  */
0123 struct nvmem_cell_table {
0124     const char      *nvmem_name;
0125     const struct nvmem_cell_info    *cells;
0126     size_t          ncells;
0127     struct list_head    node;
0128 };
0129 
0130 #if IS_ENABLED(CONFIG_NVMEM)
0131 
0132 struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
0133 void nvmem_unregister(struct nvmem_device *nvmem);
0134 
0135 struct nvmem_device *devm_nvmem_register(struct device *dev,
0136                      const struct nvmem_config *cfg);
0137 
0138 void nvmem_add_cell_table(struct nvmem_cell_table *table);
0139 void nvmem_del_cell_table(struct nvmem_cell_table *table);
0140 
0141 #else
0142 
0143 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
0144 {
0145     return ERR_PTR(-EOPNOTSUPP);
0146 }
0147 
0148 static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
0149 
0150 static inline struct nvmem_device *
0151 devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
0152 {
0153     return nvmem_register(c);
0154 }
0155 
0156 static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
0157 static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
0158 
0159 #endif /* CONFIG_NVMEM */
0160 #endif  /* ifndef _LINUX_NVMEM_PROVIDER_H */