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 core. |
| 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 | #include <linux/device.h> |
| 10 | #include <linux/export.h> |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/idr.h> |
| 13 | #include <linux/init.h> |
Bartosz Golaszewski | c1de7f4 | 2018-09-21 06:40:08 -0700 | [diff] [blame] | 14 | #include <linux/kref.h> |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/nvmem-consumer.h> |
| 17 | #include <linux/nvmem-provider.h> |
| 18 | #include <linux/of.h> |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 19 | #include <linux/slab.h> |
| 20 | |
| 21 | struct nvmem_device { |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 22 | struct module *owner; |
| 23 | struct device dev; |
| 24 | int stride; |
| 25 | int word_size; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 26 | int id; |
Bartosz Golaszewski | c1de7f4 | 2018-09-21 06:40:08 -0700 | [diff] [blame] | 27 | struct kref refcnt; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 28 | size_t size; |
| 29 | bool read_only; |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 30 | int flags; |
| 31 | struct bin_attribute eeprom; |
| 32 | struct device *base_dev; |
Bartosz Golaszewski | c7235ee | 2018-09-21 06:40:14 -0700 | [diff] [blame] | 33 | struct list_head cells; |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 34 | nvmem_reg_read_t reg_read; |
| 35 | nvmem_reg_write_t reg_write; |
| 36 | void *priv; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 37 | }; |
| 38 | |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 39 | #define FLAG_COMPAT BIT(0) |
| 40 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 41 | struct nvmem_cell { |
| 42 | const char *name; |
| 43 | int offset; |
| 44 | int bytes; |
| 45 | int bit_offset; |
| 46 | int nbits; |
Srinivas Kandagatla | 0749aa2 | 2018-11-06 15:41:41 +0000 | [diff] [blame^] | 47 | struct device_node *np; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 48 | struct nvmem_device *nvmem; |
| 49 | struct list_head node; |
| 50 | }; |
| 51 | |
| 52 | static DEFINE_MUTEX(nvmem_mutex); |
| 53 | static DEFINE_IDA(nvmem_ida); |
| 54 | |
Bartosz Golaszewski | b985f4c | 2018-09-21 06:40:15 -0700 | [diff] [blame] | 55 | static DEFINE_MUTEX(nvmem_cell_mutex); |
| 56 | static LIST_HEAD(nvmem_cell_tables); |
| 57 | |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 58 | static DEFINE_MUTEX(nvmem_lookup_mutex); |
| 59 | static LIST_HEAD(nvmem_lookup_list); |
| 60 | |
Bartosz Golaszewski | bee1138 | 2018-09-21 06:40:19 -0700 | [diff] [blame] | 61 | static BLOCKING_NOTIFIER_HEAD(nvmem_notifier); |
| 62 | |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 63 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 64 | static struct lock_class_key eeprom_lock_key; |
| 65 | #endif |
| 66 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 67 | #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev) |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 68 | static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, |
| 69 | void *val, size_t bytes) |
| 70 | { |
| 71 | if (nvmem->reg_read) |
| 72 | return nvmem->reg_read(nvmem->priv, offset, val, bytes); |
| 73 | |
| 74 | return -EINVAL; |
| 75 | } |
| 76 | |
| 77 | static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, |
| 78 | void *val, size_t bytes) |
| 79 | { |
| 80 | if (nvmem->reg_write) |
| 81 | return nvmem->reg_write(nvmem->priv, offset, val, bytes); |
| 82 | |
| 83 | return -EINVAL; |
| 84 | } |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 85 | |
| 86 | static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj, |
| 87 | struct bin_attribute *attr, |
| 88 | char *buf, loff_t pos, size_t count) |
| 89 | { |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 90 | struct device *dev; |
| 91 | struct nvmem_device *nvmem; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 92 | int rc; |
| 93 | |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 94 | if (attr->private) |
| 95 | dev = attr->private; |
| 96 | else |
| 97 | dev = container_of(kobj, struct device, kobj); |
| 98 | nvmem = to_nvmem_device(dev); |
| 99 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 100 | /* Stop the user from reading */ |
ZhengShunQian | 7c80688 | 2015-09-30 13:33:56 +0100 | [diff] [blame] | 101 | if (pos >= nvmem->size) |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 102 | return 0; |
| 103 | |
Srinivas Kandagatla | 313a72f | 2015-11-17 09:12:41 +0000 | [diff] [blame] | 104 | if (count < nvmem->word_size) |
| 105 | return -EINVAL; |
| 106 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 107 | if (pos + count > nvmem->size) |
| 108 | count = nvmem->size - pos; |
| 109 | |
| 110 | count = round_down(count, nvmem->word_size); |
| 111 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 112 | rc = nvmem_reg_read(nvmem, pos, buf, count); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 113 | |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 114 | if (rc) |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 115 | return rc; |
| 116 | |
| 117 | return count; |
| 118 | } |
| 119 | |
| 120 | static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj, |
| 121 | struct bin_attribute *attr, |
| 122 | char *buf, loff_t pos, size_t count) |
| 123 | { |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 124 | struct device *dev; |
| 125 | struct nvmem_device *nvmem; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 126 | int rc; |
| 127 | |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 128 | if (attr->private) |
| 129 | dev = attr->private; |
| 130 | else |
| 131 | dev = container_of(kobj, struct device, kobj); |
| 132 | nvmem = to_nvmem_device(dev); |
| 133 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 134 | /* Stop the user from writing */ |
ZhengShunQian | 7c80688 | 2015-09-30 13:33:56 +0100 | [diff] [blame] | 135 | if (pos >= nvmem->size) |
Guy Shapiro | 38b0774 | 2017-09-11 11:00:11 +0200 | [diff] [blame] | 136 | return -EFBIG; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 137 | |
Srinivas Kandagatla | 313a72f | 2015-11-17 09:12:41 +0000 | [diff] [blame] | 138 | if (count < nvmem->word_size) |
| 139 | return -EINVAL; |
| 140 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 141 | if (pos + count > nvmem->size) |
| 142 | count = nvmem->size - pos; |
| 143 | |
| 144 | count = round_down(count, nvmem->word_size); |
| 145 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 146 | rc = nvmem_reg_write(nvmem, pos, buf, count); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 147 | |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 148 | if (rc) |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 149 | return rc; |
| 150 | |
| 151 | return count; |
| 152 | } |
| 153 | |
| 154 | /* default read/write permissions */ |
| 155 | static struct bin_attribute bin_attr_rw_nvmem = { |
| 156 | .attr = { |
| 157 | .name = "nvmem", |
Bartosz Golaszewski | e7e07f4 | 2018-09-21 06:40:24 -0700 | [diff] [blame] | 158 | .mode = 0644, |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 159 | }, |
| 160 | .read = bin_attr_nvmem_read, |
| 161 | .write = bin_attr_nvmem_write, |
| 162 | }; |
| 163 | |
| 164 | static struct bin_attribute *nvmem_bin_rw_attributes[] = { |
| 165 | &bin_attr_rw_nvmem, |
| 166 | NULL, |
| 167 | }; |
| 168 | |
| 169 | static const struct attribute_group nvmem_bin_rw_group = { |
| 170 | .bin_attrs = nvmem_bin_rw_attributes, |
| 171 | }; |
| 172 | |
| 173 | static const struct attribute_group *nvmem_rw_dev_groups[] = { |
| 174 | &nvmem_bin_rw_group, |
| 175 | NULL, |
| 176 | }; |
| 177 | |
| 178 | /* read only permission */ |
| 179 | static struct bin_attribute bin_attr_ro_nvmem = { |
| 180 | .attr = { |
| 181 | .name = "nvmem", |
Bartosz Golaszewski | e7e07f4 | 2018-09-21 06:40:24 -0700 | [diff] [blame] | 182 | .mode = 0444, |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 183 | }, |
| 184 | .read = bin_attr_nvmem_read, |
| 185 | }; |
| 186 | |
| 187 | static struct bin_attribute *nvmem_bin_ro_attributes[] = { |
| 188 | &bin_attr_ro_nvmem, |
| 189 | NULL, |
| 190 | }; |
| 191 | |
| 192 | static const struct attribute_group nvmem_bin_ro_group = { |
| 193 | .bin_attrs = nvmem_bin_ro_attributes, |
| 194 | }; |
| 195 | |
| 196 | static const struct attribute_group *nvmem_ro_dev_groups[] = { |
| 197 | &nvmem_bin_ro_group, |
| 198 | NULL, |
| 199 | }; |
| 200 | |
Andrew Lunn | 811b0d6 | 2016-02-26 20:59:18 +0100 | [diff] [blame] | 201 | /* default read/write permissions, root only */ |
| 202 | static struct bin_attribute bin_attr_rw_root_nvmem = { |
| 203 | .attr = { |
| 204 | .name = "nvmem", |
Bartosz Golaszewski | e7e07f4 | 2018-09-21 06:40:24 -0700 | [diff] [blame] | 205 | .mode = 0600, |
Andrew Lunn | 811b0d6 | 2016-02-26 20:59:18 +0100 | [diff] [blame] | 206 | }, |
| 207 | .read = bin_attr_nvmem_read, |
| 208 | .write = bin_attr_nvmem_write, |
| 209 | }; |
| 210 | |
| 211 | static struct bin_attribute *nvmem_bin_rw_root_attributes[] = { |
| 212 | &bin_attr_rw_root_nvmem, |
| 213 | NULL, |
| 214 | }; |
| 215 | |
| 216 | static const struct attribute_group nvmem_bin_rw_root_group = { |
| 217 | .bin_attrs = nvmem_bin_rw_root_attributes, |
| 218 | }; |
| 219 | |
| 220 | static const struct attribute_group *nvmem_rw_root_dev_groups[] = { |
| 221 | &nvmem_bin_rw_root_group, |
| 222 | NULL, |
| 223 | }; |
| 224 | |
| 225 | /* read only permission, root only */ |
| 226 | static struct bin_attribute bin_attr_ro_root_nvmem = { |
| 227 | .attr = { |
| 228 | .name = "nvmem", |
Bartosz Golaszewski | e7e07f4 | 2018-09-21 06:40:24 -0700 | [diff] [blame] | 229 | .mode = 0400, |
Andrew Lunn | 811b0d6 | 2016-02-26 20:59:18 +0100 | [diff] [blame] | 230 | }, |
| 231 | .read = bin_attr_nvmem_read, |
| 232 | }; |
| 233 | |
| 234 | static struct bin_attribute *nvmem_bin_ro_root_attributes[] = { |
| 235 | &bin_attr_ro_root_nvmem, |
| 236 | NULL, |
| 237 | }; |
| 238 | |
| 239 | static const struct attribute_group nvmem_bin_ro_root_group = { |
| 240 | .bin_attrs = nvmem_bin_ro_root_attributes, |
| 241 | }; |
| 242 | |
| 243 | static const struct attribute_group *nvmem_ro_root_dev_groups[] = { |
| 244 | &nvmem_bin_ro_root_group, |
| 245 | NULL, |
| 246 | }; |
| 247 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 248 | static void nvmem_release(struct device *dev) |
| 249 | { |
| 250 | struct nvmem_device *nvmem = to_nvmem_device(dev); |
| 251 | |
| 252 | ida_simple_remove(&nvmem_ida, nvmem->id); |
| 253 | kfree(nvmem); |
| 254 | } |
| 255 | |
| 256 | static const struct device_type nvmem_provider_type = { |
| 257 | .release = nvmem_release, |
| 258 | }; |
| 259 | |
| 260 | static struct bus_type nvmem_bus_type = { |
| 261 | .name = "nvmem", |
| 262 | }; |
| 263 | |
| 264 | static int of_nvmem_match(struct device *dev, void *nvmem_np) |
| 265 | { |
| 266 | return dev->of_node == nvmem_np; |
| 267 | } |
| 268 | |
| 269 | static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np) |
| 270 | { |
| 271 | struct device *d; |
| 272 | |
| 273 | if (!nvmem_np) |
| 274 | return NULL; |
| 275 | |
| 276 | d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match); |
| 277 | |
| 278 | if (!d) |
| 279 | return NULL; |
| 280 | |
| 281 | return to_nvmem_device(d); |
| 282 | } |
| 283 | |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 284 | static struct nvmem_device *nvmem_find(const char *name) |
| 285 | { |
| 286 | struct device *d; |
| 287 | |
| 288 | d = bus_find_device_by_name(&nvmem_bus_type, NULL, name); |
| 289 | |
| 290 | if (!d) |
| 291 | return NULL; |
| 292 | |
| 293 | return to_nvmem_device(d); |
| 294 | } |
| 295 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 296 | static void nvmem_cell_drop(struct nvmem_cell *cell) |
| 297 | { |
Bartosz Golaszewski | bee1138 | 2018-09-21 06:40:19 -0700 | [diff] [blame] | 298 | blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell); |
Bartosz Golaszewski | c7235ee | 2018-09-21 06:40:14 -0700 | [diff] [blame] | 299 | mutex_lock(&nvmem_mutex); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 300 | list_del(&cell->node); |
Bartosz Golaszewski | c7235ee | 2018-09-21 06:40:14 -0700 | [diff] [blame] | 301 | mutex_unlock(&nvmem_mutex); |
Srinivas Kandagatla | 0749aa2 | 2018-11-06 15:41:41 +0000 | [diff] [blame^] | 302 | of_node_put(cell->np); |
Rob Herring | badcdff | 2018-10-03 18:47:04 +0100 | [diff] [blame] | 303 | kfree(cell->name); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 304 | kfree(cell); |
| 305 | } |
| 306 | |
| 307 | static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem) |
| 308 | { |
Bartosz Golaszewski | 1852183 | 2018-09-21 06:40:05 -0700 | [diff] [blame] | 309 | struct nvmem_cell *cell, *p; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 310 | |
Bartosz Golaszewski | c7235ee | 2018-09-21 06:40:14 -0700 | [diff] [blame] | 311 | list_for_each_entry_safe(cell, p, &nvmem->cells, node) |
| 312 | nvmem_cell_drop(cell); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static void nvmem_cell_add(struct nvmem_cell *cell) |
| 316 | { |
Bartosz Golaszewski | c7235ee | 2018-09-21 06:40:14 -0700 | [diff] [blame] | 317 | mutex_lock(&nvmem_mutex); |
| 318 | list_add_tail(&cell->node, &cell->nvmem->cells); |
| 319 | mutex_unlock(&nvmem_mutex); |
Bartosz Golaszewski | bee1138 | 2018-09-21 06:40:19 -0700 | [diff] [blame] | 320 | blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, |
| 324 | const struct nvmem_cell_info *info, |
| 325 | struct nvmem_cell *cell) |
| 326 | { |
| 327 | cell->nvmem = nvmem; |
| 328 | cell->offset = info->offset; |
| 329 | cell->bytes = info->bytes; |
| 330 | cell->name = info->name; |
| 331 | |
| 332 | cell->bit_offset = info->bit_offset; |
| 333 | cell->nbits = info->nbits; |
| 334 | |
| 335 | if (cell->nbits) |
| 336 | cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, |
| 337 | BITS_PER_BYTE); |
| 338 | |
| 339 | if (!IS_ALIGNED(cell->offset, nvmem->stride)) { |
| 340 | dev_err(&nvmem->dev, |
| 341 | "cell %s unaligned to nvmem stride %d\n", |
| 342 | cell->name, nvmem->stride); |
| 343 | return -EINVAL; |
| 344 | } |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
Andrew Lunn | b3db17e | 2018-05-11 12:06:56 +0100 | [diff] [blame] | 349 | /** |
| 350 | * nvmem_add_cells() - Add cell information to an nvmem device |
| 351 | * |
| 352 | * @nvmem: nvmem device to add cells to. |
| 353 | * @info: nvmem cell info to add to the device |
| 354 | * @ncells: number of cells in info |
| 355 | * |
| 356 | * Return: 0 or negative error code on failure. |
| 357 | */ |
Srinivas Kandagatla | ef92ab3 | 2018-09-21 06:40:26 -0700 | [diff] [blame] | 358 | static int nvmem_add_cells(struct nvmem_device *nvmem, |
Andrew Lunn | b3db17e | 2018-05-11 12:06:56 +0100 | [diff] [blame] | 359 | const struct nvmem_cell_info *info, |
| 360 | int ncells) |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 361 | { |
| 362 | struct nvmem_cell **cells; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 363 | int i, rval; |
| 364 | |
Andrew Lunn | b3db17e | 2018-05-11 12:06:56 +0100 | [diff] [blame] | 365 | cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 366 | if (!cells) |
| 367 | return -ENOMEM; |
| 368 | |
Andrew Lunn | b3db17e | 2018-05-11 12:06:56 +0100 | [diff] [blame] | 369 | for (i = 0; i < ncells; i++) { |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 370 | cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL); |
| 371 | if (!cells[i]) { |
| 372 | rval = -ENOMEM; |
| 373 | goto err; |
| 374 | } |
| 375 | |
| 376 | rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]); |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 377 | if (rval) { |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 378 | kfree(cells[i]); |
| 379 | goto err; |
| 380 | } |
| 381 | |
| 382 | nvmem_cell_add(cells[i]); |
| 383 | } |
| 384 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 385 | /* remove tmp array */ |
| 386 | kfree(cells); |
| 387 | |
| 388 | return 0; |
| 389 | err: |
Rasmus Villemoes | dfdf141 | 2016-02-08 22:04:29 +0100 | [diff] [blame] | 390 | while (i--) |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 391 | nvmem_cell_drop(cells[i]); |
| 392 | |
Rasmus Villemoes | dfdf141 | 2016-02-08 22:04:29 +0100 | [diff] [blame] | 393 | kfree(cells); |
| 394 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 395 | return rval; |
| 396 | } |
| 397 | |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 398 | /* |
| 399 | * nvmem_setup_compat() - Create an additional binary entry in |
| 400 | * drivers sys directory, to be backwards compatible with the older |
| 401 | * drivers/misc/eeprom drivers. |
| 402 | */ |
| 403 | static int nvmem_setup_compat(struct nvmem_device *nvmem, |
| 404 | const struct nvmem_config *config) |
| 405 | { |
| 406 | int rval; |
| 407 | |
| 408 | if (!config->base_dev) |
| 409 | return -EINVAL; |
| 410 | |
| 411 | if (nvmem->read_only) |
| 412 | nvmem->eeprom = bin_attr_ro_root_nvmem; |
| 413 | else |
| 414 | nvmem->eeprom = bin_attr_rw_root_nvmem; |
| 415 | nvmem->eeprom.attr.name = "eeprom"; |
| 416 | nvmem->eeprom.size = nvmem->size; |
| 417 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 418 | nvmem->eeprom.attr.key = &eeprom_lock_key; |
| 419 | #endif |
| 420 | nvmem->eeprom.private = &nvmem->dev; |
| 421 | nvmem->base_dev = config->base_dev; |
| 422 | |
| 423 | rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom); |
| 424 | if (rval) { |
| 425 | dev_err(&nvmem->dev, |
| 426 | "Failed to create eeprom binary file %d\n", rval); |
| 427 | return rval; |
| 428 | } |
| 429 | |
| 430 | nvmem->flags |= FLAG_COMPAT; |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
Bartosz Golaszewski | bee1138 | 2018-09-21 06:40:19 -0700 | [diff] [blame] | 435 | /** |
| 436 | * nvmem_register_notifier() - Register a notifier block for nvmem events. |
| 437 | * |
| 438 | * @nb: notifier block to be called on nvmem events. |
| 439 | * |
| 440 | * Return: 0 on success, negative error number on failure. |
| 441 | */ |
| 442 | int nvmem_register_notifier(struct notifier_block *nb) |
| 443 | { |
| 444 | return blocking_notifier_chain_register(&nvmem_notifier, nb); |
| 445 | } |
| 446 | EXPORT_SYMBOL_GPL(nvmem_register_notifier); |
| 447 | |
| 448 | /** |
| 449 | * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events. |
| 450 | * |
| 451 | * @nb: notifier block to be unregistered. |
| 452 | * |
| 453 | * Return: 0 on success, negative error number on failure. |
| 454 | */ |
| 455 | int nvmem_unregister_notifier(struct notifier_block *nb) |
| 456 | { |
| 457 | return blocking_notifier_chain_unregister(&nvmem_notifier, nb); |
| 458 | } |
| 459 | EXPORT_SYMBOL_GPL(nvmem_unregister_notifier); |
| 460 | |
Bartosz Golaszewski | b985f4c | 2018-09-21 06:40:15 -0700 | [diff] [blame] | 461 | static int nvmem_add_cells_from_table(struct nvmem_device *nvmem) |
| 462 | { |
| 463 | const struct nvmem_cell_info *info; |
| 464 | struct nvmem_cell_table *table; |
| 465 | struct nvmem_cell *cell; |
| 466 | int rval = 0, i; |
| 467 | |
| 468 | mutex_lock(&nvmem_cell_mutex); |
| 469 | list_for_each_entry(table, &nvmem_cell_tables, node) { |
| 470 | if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) { |
| 471 | for (i = 0; i < table->ncells; i++) { |
| 472 | info = &table->cells[i]; |
| 473 | |
| 474 | cell = kzalloc(sizeof(*cell), GFP_KERNEL); |
| 475 | if (!cell) { |
| 476 | rval = -ENOMEM; |
| 477 | goto out; |
| 478 | } |
| 479 | |
| 480 | rval = nvmem_cell_info_to_nvmem_cell(nvmem, |
| 481 | info, |
| 482 | cell); |
| 483 | if (rval) { |
| 484 | kfree(cell); |
| 485 | goto out; |
| 486 | } |
| 487 | |
| 488 | nvmem_cell_add(cell); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | out: |
| 494 | mutex_unlock(&nvmem_cell_mutex); |
| 495 | return rval; |
| 496 | } |
| 497 | |
Bartosz Golaszewski | e888d44 | 2018-09-21 06:40:16 -0700 | [diff] [blame] | 498 | static struct nvmem_cell * |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 499 | nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id) |
| 500 | { |
| 501 | struct nvmem_cell *cell = NULL; |
| 502 | |
| 503 | mutex_lock(&nvmem_mutex); |
| 504 | list_for_each_entry(cell, &nvmem->cells, node) { |
| 505 | if (strcmp(cell_id, cell->name) == 0) |
| 506 | break; |
| 507 | } |
| 508 | mutex_unlock(&nvmem_mutex); |
| 509 | |
| 510 | return cell; |
| 511 | } |
| 512 | |
Bartosz Golaszewski | e888d44 | 2018-09-21 06:40:16 -0700 | [diff] [blame] | 513 | static int nvmem_add_cells_from_of(struct nvmem_device *nvmem) |
| 514 | { |
| 515 | struct device_node *parent, *child; |
| 516 | struct device *dev = &nvmem->dev; |
| 517 | struct nvmem_cell *cell; |
| 518 | const __be32 *addr; |
| 519 | int len; |
| 520 | |
| 521 | parent = dev->of_node; |
| 522 | |
| 523 | for_each_child_of_node(parent, child) { |
| 524 | addr = of_get_property(child, "reg", &len); |
| 525 | if (!addr || (len < 2 * sizeof(u32))) { |
| 526 | dev_err(dev, "nvmem: invalid reg on %pOF\n", child); |
| 527 | return -EINVAL; |
| 528 | } |
| 529 | |
| 530 | cell = kzalloc(sizeof(*cell), GFP_KERNEL); |
| 531 | if (!cell) |
| 532 | return -ENOMEM; |
| 533 | |
| 534 | cell->nvmem = nvmem; |
Srinivas Kandagatla | 0749aa2 | 2018-11-06 15:41:41 +0000 | [diff] [blame^] | 535 | cell->np = of_node_get(child); |
Bartosz Golaszewski | e888d44 | 2018-09-21 06:40:16 -0700 | [diff] [blame] | 536 | cell->offset = be32_to_cpup(addr++); |
| 537 | cell->bytes = be32_to_cpup(addr); |
Rob Herring | badcdff | 2018-10-03 18:47:04 +0100 | [diff] [blame] | 538 | cell->name = kasprintf(GFP_KERNEL, "%pOFn", child); |
Bartosz Golaszewski | e888d44 | 2018-09-21 06:40:16 -0700 | [diff] [blame] | 539 | |
| 540 | addr = of_get_property(child, "bits", &len); |
| 541 | if (addr && len == (2 * sizeof(u32))) { |
| 542 | cell->bit_offset = be32_to_cpup(addr++); |
| 543 | cell->nbits = be32_to_cpup(addr); |
| 544 | } |
| 545 | |
| 546 | if (cell->nbits) |
| 547 | cell->bytes = DIV_ROUND_UP( |
| 548 | cell->nbits + cell->bit_offset, |
| 549 | BITS_PER_BYTE); |
| 550 | |
| 551 | if (!IS_ALIGNED(cell->offset, nvmem->stride)) { |
| 552 | dev_err(dev, "cell %s unaligned to nvmem stride %d\n", |
| 553 | cell->name, nvmem->stride); |
| 554 | /* Cells already added will be freed later. */ |
Rob Herring | badcdff | 2018-10-03 18:47:04 +0100 | [diff] [blame] | 555 | kfree(cell->name); |
Bartosz Golaszewski | e888d44 | 2018-09-21 06:40:16 -0700 | [diff] [blame] | 556 | kfree(cell); |
| 557 | return -EINVAL; |
| 558 | } |
| 559 | |
| 560 | nvmem_cell_add(cell); |
| 561 | } |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 566 | /** |
| 567 | * nvmem_register() - Register a nvmem device for given nvmem_config. |
| 568 | * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem |
| 569 | * |
| 570 | * @config: nvmem device configuration with which nvmem device is created. |
| 571 | * |
| 572 | * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device |
| 573 | * on success. |
| 574 | */ |
| 575 | |
| 576 | struct nvmem_device *nvmem_register(const struct nvmem_config *config) |
| 577 | { |
| 578 | struct nvmem_device *nvmem; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 579 | int rval; |
| 580 | |
| 581 | if (!config->dev) |
| 582 | return ERR_PTR(-EINVAL); |
| 583 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 584 | nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL); |
| 585 | if (!nvmem) |
| 586 | return ERR_PTR(-ENOMEM); |
| 587 | |
| 588 | rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL); |
| 589 | if (rval < 0) { |
| 590 | kfree(nvmem); |
| 591 | return ERR_PTR(rval); |
| 592 | } |
| 593 | |
Bartosz Golaszewski | c1de7f4 | 2018-09-21 06:40:08 -0700 | [diff] [blame] | 594 | kref_init(&nvmem->refcnt); |
Bartosz Golaszewski | c7235ee | 2018-09-21 06:40:14 -0700 | [diff] [blame] | 595 | INIT_LIST_HEAD(&nvmem->cells); |
Bartosz Golaszewski | c1de7f4 | 2018-09-21 06:40:08 -0700 | [diff] [blame] | 596 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 597 | nvmem->id = rval; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 598 | nvmem->owner = config->owner; |
Masahiro Yamada | 17eb18d | 2017-10-21 01:57:42 +0900 | [diff] [blame] | 599 | if (!nvmem->owner && config->dev->driver) |
| 600 | nvmem->owner = config->dev->driver->owner; |
Heiner Kallweit | 99897ef | 2017-12-15 14:06:05 +0000 | [diff] [blame] | 601 | nvmem->stride = config->stride ?: 1; |
| 602 | nvmem->word_size = config->word_size ?: 1; |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 603 | nvmem->size = config->size; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 604 | nvmem->dev.type = &nvmem_provider_type; |
| 605 | nvmem->dev.bus = &nvmem_bus_type; |
| 606 | nvmem->dev.parent = config->dev; |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 607 | nvmem->priv = config->priv; |
| 608 | nvmem->reg_read = config->reg_read; |
| 609 | nvmem->reg_write = config->reg_write; |
Heiner Kallweit | fc2f997 | 2017-12-15 14:06:06 +0000 | [diff] [blame] | 610 | nvmem->dev.of_node = config->dev->of_node; |
Andrey Smirnov | fd0f490 | 2018-03-09 14:46:56 +0000 | [diff] [blame] | 611 | |
| 612 | if (config->id == -1 && config->name) { |
| 613 | dev_set_name(&nvmem->dev, "%s", config->name); |
| 614 | } else { |
| 615 | dev_set_name(&nvmem->dev, "%s%d", |
| 616 | config->name ? : "nvmem", |
| 617 | config->name ? config->id : nvmem->id); |
| 618 | } |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 619 | |
Heiner Kallweit | fc2f997 | 2017-12-15 14:06:06 +0000 | [diff] [blame] | 620 | nvmem->read_only = device_property_present(config->dev, "read-only") | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 621 | config->read_only; |
| 622 | |
Andrew Lunn | 811b0d6 | 2016-02-26 20:59:18 +0100 | [diff] [blame] | 623 | if (config->root_only) |
| 624 | nvmem->dev.groups = nvmem->read_only ? |
| 625 | nvmem_ro_root_dev_groups : |
| 626 | nvmem_rw_root_dev_groups; |
| 627 | else |
| 628 | nvmem->dev.groups = nvmem->read_only ? |
| 629 | nvmem_ro_dev_groups : |
| 630 | nvmem_rw_dev_groups; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 631 | |
| 632 | device_initialize(&nvmem->dev); |
| 633 | |
| 634 | dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); |
| 635 | |
| 636 | rval = device_add(&nvmem->dev); |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 637 | if (rval) |
Johan Hovold | 3360acd | 2017-06-09 10:59:07 +0100 | [diff] [blame] | 638 | goto err_put_device; |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 639 | |
| 640 | if (config->compat) { |
| 641 | rval = nvmem_setup_compat(nvmem, config); |
| 642 | if (rval) |
Johan Hovold | 3360acd | 2017-06-09 10:59:07 +0100 | [diff] [blame] | 643 | goto err_device_del; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 644 | } |
| 645 | |
Bartosz Golaszewski | fa72d84 | 2018-09-21 06:40:07 -0700 | [diff] [blame] | 646 | if (config->cells) { |
| 647 | rval = nvmem_add_cells(nvmem, config->cells, config->ncells); |
| 648 | if (rval) |
| 649 | goto err_teardown_compat; |
| 650 | } |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 651 | |
Bartosz Golaszewski | b985f4c | 2018-09-21 06:40:15 -0700 | [diff] [blame] | 652 | rval = nvmem_add_cells_from_table(nvmem); |
| 653 | if (rval) |
| 654 | goto err_remove_cells; |
| 655 | |
Bartosz Golaszewski | e888d44 | 2018-09-21 06:40:16 -0700 | [diff] [blame] | 656 | rval = nvmem_add_cells_from_of(nvmem); |
| 657 | if (rval) |
| 658 | goto err_remove_cells; |
| 659 | |
Bartosz Golaszewski | bee1138 | 2018-09-21 06:40:19 -0700 | [diff] [blame] | 660 | rval = blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); |
| 661 | if (rval) |
| 662 | goto err_remove_cells; |
| 663 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 664 | return nvmem; |
Johan Hovold | 3360acd | 2017-06-09 10:59:07 +0100 | [diff] [blame] | 665 | |
Bartosz Golaszewski | b985f4c | 2018-09-21 06:40:15 -0700 | [diff] [blame] | 666 | err_remove_cells: |
| 667 | nvmem_device_remove_all_cells(nvmem); |
Bartosz Golaszewski | fa72d84 | 2018-09-21 06:40:07 -0700 | [diff] [blame] | 668 | err_teardown_compat: |
| 669 | if (config->compat) |
| 670 | device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); |
Johan Hovold | 3360acd | 2017-06-09 10:59:07 +0100 | [diff] [blame] | 671 | err_device_del: |
| 672 | device_del(&nvmem->dev); |
| 673 | err_put_device: |
| 674 | put_device(&nvmem->dev); |
| 675 | |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 676 | return ERR_PTR(rval); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 677 | } |
| 678 | EXPORT_SYMBOL_GPL(nvmem_register); |
| 679 | |
Bartosz Golaszewski | c1de7f4 | 2018-09-21 06:40:08 -0700 | [diff] [blame] | 680 | static void nvmem_device_release(struct kref *kref) |
| 681 | { |
| 682 | struct nvmem_device *nvmem; |
| 683 | |
| 684 | nvmem = container_of(kref, struct nvmem_device, refcnt); |
| 685 | |
Bartosz Golaszewski | bee1138 | 2018-09-21 06:40:19 -0700 | [diff] [blame] | 686 | blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem); |
| 687 | |
Bartosz Golaszewski | c1de7f4 | 2018-09-21 06:40:08 -0700 | [diff] [blame] | 688 | if (nvmem->flags & FLAG_COMPAT) |
| 689 | device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); |
| 690 | |
| 691 | nvmem_device_remove_all_cells(nvmem); |
| 692 | device_del(&nvmem->dev); |
| 693 | put_device(&nvmem->dev); |
| 694 | } |
| 695 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 696 | /** |
| 697 | * nvmem_unregister() - Unregister previously registered nvmem device |
| 698 | * |
| 699 | * @nvmem: Pointer to previously registered nvmem device. |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 700 | */ |
Bartosz Golaszewski | bf58e88 | 2018-09-21 06:40:13 -0700 | [diff] [blame] | 701 | void nvmem_unregister(struct nvmem_device *nvmem) |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 702 | { |
Bartosz Golaszewski | c1de7f4 | 2018-09-21 06:40:08 -0700 | [diff] [blame] | 703 | kref_put(&nvmem->refcnt, nvmem_device_release); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 704 | } |
| 705 | EXPORT_SYMBOL_GPL(nvmem_unregister); |
| 706 | |
Andrey Smirnov | f1f50ec | 2018-03-09 14:46:57 +0000 | [diff] [blame] | 707 | static void devm_nvmem_release(struct device *dev, void *res) |
| 708 | { |
Bartosz Golaszewski | bf58e88 | 2018-09-21 06:40:13 -0700 | [diff] [blame] | 709 | nvmem_unregister(*(struct nvmem_device **)res); |
Andrey Smirnov | f1f50ec | 2018-03-09 14:46:57 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /** |
| 713 | * devm_nvmem_register() - Register a managed nvmem device for given |
| 714 | * nvmem_config. |
| 715 | * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem |
| 716 | * |
Srinivas Kandagatla | b378c77 | 2018-05-11 12:07:02 +0100 | [diff] [blame] | 717 | * @dev: Device that uses the nvmem device. |
Andrey Smirnov | f1f50ec | 2018-03-09 14:46:57 +0000 | [diff] [blame] | 718 | * @config: nvmem device configuration with which nvmem device is created. |
| 719 | * |
| 720 | * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device |
| 721 | * on success. |
| 722 | */ |
| 723 | struct nvmem_device *devm_nvmem_register(struct device *dev, |
| 724 | const struct nvmem_config *config) |
| 725 | { |
| 726 | struct nvmem_device **ptr, *nvmem; |
| 727 | |
| 728 | ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL); |
| 729 | if (!ptr) |
| 730 | return ERR_PTR(-ENOMEM); |
| 731 | |
| 732 | nvmem = nvmem_register(config); |
| 733 | |
| 734 | if (!IS_ERR(nvmem)) { |
| 735 | *ptr = nvmem; |
| 736 | devres_add(dev, ptr); |
| 737 | } else { |
| 738 | devres_free(ptr); |
| 739 | } |
| 740 | |
| 741 | return nvmem; |
| 742 | } |
| 743 | EXPORT_SYMBOL_GPL(devm_nvmem_register); |
| 744 | |
| 745 | static int devm_nvmem_match(struct device *dev, void *res, void *data) |
| 746 | { |
| 747 | struct nvmem_device **r = res; |
| 748 | |
| 749 | return *r == data; |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * devm_nvmem_unregister() - Unregister previously registered managed nvmem |
| 754 | * device. |
| 755 | * |
Srinivas Kandagatla | b378c77 | 2018-05-11 12:07:02 +0100 | [diff] [blame] | 756 | * @dev: Device that uses the nvmem device. |
Andrey Smirnov | f1f50ec | 2018-03-09 14:46:57 +0000 | [diff] [blame] | 757 | * @nvmem: Pointer to previously registered nvmem device. |
| 758 | * |
| 759 | * Return: Will be an negative on error or a zero on success. |
| 760 | */ |
| 761 | int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem) |
| 762 | { |
| 763 | return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem); |
| 764 | } |
| 765 | EXPORT_SYMBOL(devm_nvmem_unregister); |
| 766 | |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 767 | static struct nvmem_device *__nvmem_device_get(struct device_node *np, |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 768 | const char *nvmem_name) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 769 | { |
| 770 | struct nvmem_device *nvmem = NULL; |
| 771 | |
| 772 | mutex_lock(&nvmem_mutex); |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 773 | nvmem = np ? of_nvmem_find(np) : nvmem_find(nvmem_name); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 774 | mutex_unlock(&nvmem_mutex); |
Bartosz Golaszewski | c7235ee | 2018-09-21 06:40:14 -0700 | [diff] [blame] | 775 | if (!nvmem) |
| 776 | return ERR_PTR(-EPROBE_DEFER); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 777 | |
| 778 | if (!try_module_get(nvmem->owner)) { |
| 779 | dev_err(&nvmem->dev, |
| 780 | "could not increase module refcount for cell %s\n", |
Bartosz Golaszewski | 5db652c | 2018-09-21 06:40:04 -0700 | [diff] [blame] | 781 | nvmem_dev_name(nvmem)); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 782 | |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 783 | return ERR_PTR(-EINVAL); |
| 784 | } |
| 785 | |
Bartosz Golaszewski | c1de7f4 | 2018-09-21 06:40:08 -0700 | [diff] [blame] | 786 | kref_get(&nvmem->refcnt); |
| 787 | |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 788 | return nvmem; |
| 789 | } |
| 790 | |
| 791 | static void __nvmem_device_put(struct nvmem_device *nvmem) |
| 792 | { |
| 793 | module_put(nvmem->owner); |
Bartosz Golaszewski | c1de7f4 | 2018-09-21 06:40:08 -0700 | [diff] [blame] | 794 | kref_put(&nvmem->refcnt, nvmem_device_release); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 795 | } |
| 796 | |
Masahiro Yamada | e701c67 | 2017-09-11 11:00:14 +0200 | [diff] [blame] | 797 | #if IS_ENABLED(CONFIG_OF) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 798 | /** |
| 799 | * of_nvmem_device_get() - Get nvmem device from a given id |
| 800 | * |
Vivek Gautam | 2914326 | 2017-01-22 23:02:39 +0000 | [diff] [blame] | 801 | * @np: Device tree node that uses the nvmem device. |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 802 | * @id: nvmem name from nvmem-names property. |
| 803 | * |
| 804 | * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device |
| 805 | * on success. |
| 806 | */ |
| 807 | struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id) |
| 808 | { |
| 809 | |
| 810 | struct device_node *nvmem_np; |
| 811 | int index; |
| 812 | |
| 813 | index = of_property_match_string(np, "nvmem-names", id); |
| 814 | |
| 815 | nvmem_np = of_parse_phandle(np, "nvmem", index); |
| 816 | if (!nvmem_np) |
| 817 | return ERR_PTR(-EINVAL); |
| 818 | |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 819 | return __nvmem_device_get(nvmem_np, NULL); |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 820 | } |
| 821 | EXPORT_SYMBOL_GPL(of_nvmem_device_get); |
| 822 | #endif |
| 823 | |
| 824 | /** |
| 825 | * nvmem_device_get() - Get nvmem device from a given id |
| 826 | * |
Vivek Gautam | 2914326 | 2017-01-22 23:02:39 +0000 | [diff] [blame] | 827 | * @dev: Device that uses the nvmem device. |
| 828 | * @dev_name: name of the requested nvmem device. |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 829 | * |
| 830 | * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device |
| 831 | * on success. |
| 832 | */ |
| 833 | struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name) |
| 834 | { |
| 835 | if (dev->of_node) { /* try dt first */ |
| 836 | struct nvmem_device *nvmem; |
| 837 | |
| 838 | nvmem = of_nvmem_device_get(dev->of_node, dev_name); |
| 839 | |
| 840 | if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER) |
| 841 | return nvmem; |
| 842 | |
| 843 | } |
| 844 | |
| 845 | return nvmem_find(dev_name); |
| 846 | } |
| 847 | EXPORT_SYMBOL_GPL(nvmem_device_get); |
| 848 | |
| 849 | static int devm_nvmem_device_match(struct device *dev, void *res, void *data) |
| 850 | { |
| 851 | struct nvmem_device **nvmem = res; |
| 852 | |
| 853 | if (WARN_ON(!nvmem || !*nvmem)) |
| 854 | return 0; |
| 855 | |
| 856 | return *nvmem == data; |
| 857 | } |
| 858 | |
| 859 | static void devm_nvmem_device_release(struct device *dev, void *res) |
| 860 | { |
| 861 | nvmem_device_put(*(struct nvmem_device **)res); |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * devm_nvmem_device_put() - put alredy got nvmem device |
| 866 | * |
Vivek Gautam | 2914326 | 2017-01-22 23:02:39 +0000 | [diff] [blame] | 867 | * @dev: Device that uses the nvmem device. |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 868 | * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(), |
| 869 | * that needs to be released. |
| 870 | */ |
| 871 | void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem) |
| 872 | { |
| 873 | int ret; |
| 874 | |
| 875 | ret = devres_release(dev, devm_nvmem_device_release, |
| 876 | devm_nvmem_device_match, nvmem); |
| 877 | |
| 878 | WARN_ON(ret); |
| 879 | } |
| 880 | EXPORT_SYMBOL_GPL(devm_nvmem_device_put); |
| 881 | |
| 882 | /** |
| 883 | * nvmem_device_put() - put alredy got nvmem device |
| 884 | * |
| 885 | * @nvmem: pointer to nvmem device that needs to be released. |
| 886 | */ |
| 887 | void nvmem_device_put(struct nvmem_device *nvmem) |
| 888 | { |
| 889 | __nvmem_device_put(nvmem); |
| 890 | } |
| 891 | EXPORT_SYMBOL_GPL(nvmem_device_put); |
| 892 | |
| 893 | /** |
| 894 | * devm_nvmem_device_get() - Get nvmem cell of device form a given id |
| 895 | * |
Vivek Gautam | 2914326 | 2017-01-22 23:02:39 +0000 | [diff] [blame] | 896 | * @dev: Device that requests the nvmem device. |
| 897 | * @id: name id for the requested nvmem device. |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 898 | * |
| 899 | * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell |
| 900 | * on success. The nvmem_cell will be freed by the automatically once the |
| 901 | * device is freed. |
| 902 | */ |
| 903 | struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id) |
| 904 | { |
| 905 | struct nvmem_device **ptr, *nvmem; |
| 906 | |
| 907 | ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL); |
| 908 | if (!ptr) |
| 909 | return ERR_PTR(-ENOMEM); |
| 910 | |
| 911 | nvmem = nvmem_device_get(dev, id); |
| 912 | if (!IS_ERR(nvmem)) { |
| 913 | *ptr = nvmem; |
| 914 | devres_add(dev, ptr); |
| 915 | } else { |
| 916 | devres_free(ptr); |
| 917 | } |
| 918 | |
| 919 | return nvmem; |
| 920 | } |
| 921 | EXPORT_SYMBOL_GPL(devm_nvmem_device_get); |
| 922 | |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 923 | static struct nvmem_cell * |
| 924 | nvmem_cell_get_from_lookup(struct device *dev, const char *con_id) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 925 | { |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 926 | struct nvmem_cell *cell = ERR_PTR(-ENOENT); |
| 927 | struct nvmem_cell_lookup *lookup; |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 928 | struct nvmem_device *nvmem; |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 929 | const char *dev_id; |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 930 | |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 931 | if (!dev) |
| 932 | return ERR_PTR(-EINVAL); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 933 | |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 934 | dev_id = dev_name(dev); |
| 935 | |
| 936 | mutex_lock(&nvmem_lookup_mutex); |
| 937 | |
| 938 | list_for_each_entry(lookup, &nvmem_lookup_list, node) { |
| 939 | if ((strcmp(lookup->dev_id, dev_id) == 0) && |
| 940 | (strcmp(lookup->con_id, con_id) == 0)) { |
| 941 | /* This is the right entry. */ |
| 942 | nvmem = __nvmem_device_get(NULL, lookup->nvmem_name); |
Bartosz Golaszewski | cccb3b1 | 2018-10-03 09:31:11 +0200 | [diff] [blame] | 943 | if (IS_ERR(nvmem)) { |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 944 | /* Provider may not be registered yet. */ |
Bartosz Golaszewski | cccb3b1 | 2018-10-03 09:31:11 +0200 | [diff] [blame] | 945 | cell = ERR_CAST(nvmem); |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 946 | goto out; |
| 947 | } |
| 948 | |
| 949 | cell = nvmem_find_cell_by_name(nvmem, |
| 950 | lookup->cell_name); |
| 951 | if (!cell) { |
| 952 | __nvmem_device_put(nvmem); |
Bartosz Golaszewski | cccb3b1 | 2018-10-03 09:31:11 +0200 | [diff] [blame] | 953 | cell = ERR_PTR(-ENOENT); |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 954 | goto out; |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | out: |
| 960 | mutex_unlock(&nvmem_lookup_mutex); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 961 | return cell; |
| 962 | } |
| 963 | |
Masahiro Yamada | e701c67 | 2017-09-11 11:00:14 +0200 | [diff] [blame] | 964 | #if IS_ENABLED(CONFIG_OF) |
Arnd Bergmann | 3c53e23 | 2018-10-02 23:11:12 +0200 | [diff] [blame] | 965 | static struct nvmem_cell * |
Srinivas Kandagatla | 0749aa2 | 2018-11-06 15:41:41 +0000 | [diff] [blame^] | 966 | nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np) |
Arnd Bergmann | 3c53e23 | 2018-10-02 23:11:12 +0200 | [diff] [blame] | 967 | { |
| 968 | struct nvmem_cell *cell = NULL; |
Arnd Bergmann | 3c53e23 | 2018-10-02 23:11:12 +0200 | [diff] [blame] | 969 | |
| 970 | mutex_lock(&nvmem_mutex); |
| 971 | list_for_each_entry(cell, &nvmem->cells, node) { |
Srinivas Kandagatla | 0749aa2 | 2018-11-06 15:41:41 +0000 | [diff] [blame^] | 972 | if (np == cell->np) |
Arnd Bergmann | 3c53e23 | 2018-10-02 23:11:12 +0200 | [diff] [blame] | 973 | break; |
| 974 | } |
| 975 | mutex_unlock(&nvmem_mutex); |
| 976 | |
| 977 | return cell; |
| 978 | } |
| 979 | |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 980 | /** |
| 981 | * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id |
| 982 | * |
Vivek Gautam | 2914326 | 2017-01-22 23:02:39 +0000 | [diff] [blame] | 983 | * @np: Device tree node that uses the nvmem cell. |
Bartosz Golaszewski | 165589f | 2018-09-21 06:40:21 -0700 | [diff] [blame] | 984 | * @id: nvmem cell name from nvmem-cell-names property, or NULL |
| 985 | * for the cell at index 0 (the lone cell with no accompanying |
| 986 | * nvmem-cell-names property). |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 987 | * |
| 988 | * Return: Will be an ERR_PTR() on error or a valid pointer |
| 989 | * to a struct nvmem_cell. The nvmem_cell will be freed by the |
| 990 | * nvmem_cell_put(). |
| 991 | */ |
Bartosz Golaszewski | 165589f | 2018-09-21 06:40:21 -0700 | [diff] [blame] | 992 | struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 993 | { |
| 994 | struct device_node *cell_np, *nvmem_np; |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 995 | struct nvmem_device *nvmem; |
Bartosz Golaszewski | e888d44 | 2018-09-21 06:40:16 -0700 | [diff] [blame] | 996 | struct nvmem_cell *cell; |
Vivek Gautam | fd0c478 | 2017-01-22 23:02:40 +0000 | [diff] [blame] | 997 | int index = 0; |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 998 | |
Vivek Gautam | fd0c478 | 2017-01-22 23:02:40 +0000 | [diff] [blame] | 999 | /* if cell name exists, find index to the name */ |
Bartosz Golaszewski | 165589f | 2018-09-21 06:40:21 -0700 | [diff] [blame] | 1000 | if (id) |
| 1001 | index = of_property_match_string(np, "nvmem-cell-names", id); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1002 | |
| 1003 | cell_np = of_parse_phandle(np, "nvmem-cells", index); |
| 1004 | if (!cell_np) |
| 1005 | return ERR_PTR(-EINVAL); |
| 1006 | |
| 1007 | nvmem_np = of_get_next_parent(cell_np); |
| 1008 | if (!nvmem_np) |
| 1009 | return ERR_PTR(-EINVAL); |
| 1010 | |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 1011 | nvmem = __nvmem_device_get(nvmem_np, NULL); |
Masahiro Yamada | aad8d09 | 2017-09-11 11:00:12 +0200 | [diff] [blame] | 1012 | of_node_put(nvmem_np); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1013 | if (IS_ERR(nvmem)) |
| 1014 | return ERR_CAST(nvmem); |
| 1015 | |
Srinivas Kandagatla | 0749aa2 | 2018-11-06 15:41:41 +0000 | [diff] [blame^] | 1016 | cell = nvmem_find_cell_by_node(nvmem, cell_np); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1017 | if (!cell) { |
Bartosz Golaszewski | e888d44 | 2018-09-21 06:40:16 -0700 | [diff] [blame] | 1018 | __nvmem_device_put(nvmem); |
| 1019 | return ERR_PTR(-ENOENT); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1020 | } |
| 1021 | |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1022 | return cell; |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1023 | } |
| 1024 | EXPORT_SYMBOL_GPL(of_nvmem_cell_get); |
| 1025 | #endif |
| 1026 | |
| 1027 | /** |
| 1028 | * nvmem_cell_get() - Get nvmem cell of device form a given cell name |
| 1029 | * |
Vivek Gautam | 2914326 | 2017-01-22 23:02:39 +0000 | [diff] [blame] | 1030 | * @dev: Device that requests the nvmem cell. |
Bartosz Golaszewski | 165589f | 2018-09-21 06:40:21 -0700 | [diff] [blame] | 1031 | * @id: nvmem cell name to get (this corresponds with the name from the |
| 1032 | * nvmem-cell-names property for DT systems and with the con_id from |
| 1033 | * the lookup entry for non-DT systems). |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1034 | * |
| 1035 | * Return: Will be an ERR_PTR() on error or a valid pointer |
| 1036 | * to a struct nvmem_cell. The nvmem_cell will be freed by the |
| 1037 | * nvmem_cell_put(). |
| 1038 | */ |
Bartosz Golaszewski | 165589f | 2018-09-21 06:40:21 -0700 | [diff] [blame] | 1039 | struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1040 | { |
| 1041 | struct nvmem_cell *cell; |
| 1042 | |
| 1043 | if (dev->of_node) { /* try dt first */ |
Bartosz Golaszewski | 165589f | 2018-09-21 06:40:21 -0700 | [diff] [blame] | 1044 | cell = of_nvmem_cell_get(dev->of_node, id); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1045 | if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER) |
| 1046 | return cell; |
| 1047 | } |
| 1048 | |
Bartosz Golaszewski | 165589f | 2018-09-21 06:40:21 -0700 | [diff] [blame] | 1049 | /* NULL cell id only allowed for device tree; invalid otherwise */ |
| 1050 | if (!id) |
Douglas Anderson | 87ed140 | 2018-06-18 18:30:43 +0100 | [diff] [blame] | 1051 | return ERR_PTR(-EINVAL); |
| 1052 | |
Bartosz Golaszewski | 165589f | 2018-09-21 06:40:21 -0700 | [diff] [blame] | 1053 | return nvmem_cell_get_from_lookup(dev, id); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1054 | } |
| 1055 | EXPORT_SYMBOL_GPL(nvmem_cell_get); |
| 1056 | |
| 1057 | static void devm_nvmem_cell_release(struct device *dev, void *res) |
| 1058 | { |
| 1059 | nvmem_cell_put(*(struct nvmem_cell **)res); |
| 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * devm_nvmem_cell_get() - Get nvmem cell of device form a given id |
| 1064 | * |
Vivek Gautam | 2914326 | 2017-01-22 23:02:39 +0000 | [diff] [blame] | 1065 | * @dev: Device that requests the nvmem cell. |
| 1066 | * @id: nvmem cell name id to get. |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1067 | * |
| 1068 | * Return: Will be an ERR_PTR() on error or a valid pointer |
| 1069 | * to a struct nvmem_cell. The nvmem_cell will be freed by the |
| 1070 | * automatically once the device is freed. |
| 1071 | */ |
| 1072 | struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id) |
| 1073 | { |
| 1074 | struct nvmem_cell **ptr, *cell; |
| 1075 | |
| 1076 | ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL); |
| 1077 | if (!ptr) |
| 1078 | return ERR_PTR(-ENOMEM); |
| 1079 | |
| 1080 | cell = nvmem_cell_get(dev, id); |
| 1081 | if (!IS_ERR(cell)) { |
| 1082 | *ptr = cell; |
| 1083 | devres_add(dev, ptr); |
| 1084 | } else { |
| 1085 | devres_free(ptr); |
| 1086 | } |
| 1087 | |
| 1088 | return cell; |
| 1089 | } |
| 1090 | EXPORT_SYMBOL_GPL(devm_nvmem_cell_get); |
| 1091 | |
| 1092 | static int devm_nvmem_cell_match(struct device *dev, void *res, void *data) |
| 1093 | { |
| 1094 | struct nvmem_cell **c = res; |
| 1095 | |
| 1096 | if (WARN_ON(!c || !*c)) |
| 1097 | return 0; |
| 1098 | |
| 1099 | return *c == data; |
| 1100 | } |
| 1101 | |
| 1102 | /** |
| 1103 | * devm_nvmem_cell_put() - Release previously allocated nvmem cell |
| 1104 | * from devm_nvmem_cell_get. |
| 1105 | * |
Vivek Gautam | 2914326 | 2017-01-22 23:02:39 +0000 | [diff] [blame] | 1106 | * @dev: Device that requests the nvmem cell. |
| 1107 | * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get(). |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1108 | */ |
| 1109 | void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell) |
| 1110 | { |
| 1111 | int ret; |
| 1112 | |
| 1113 | ret = devres_release(dev, devm_nvmem_cell_release, |
| 1114 | devm_nvmem_cell_match, cell); |
| 1115 | |
| 1116 | WARN_ON(ret); |
| 1117 | } |
| 1118 | EXPORT_SYMBOL(devm_nvmem_cell_put); |
| 1119 | |
| 1120 | /** |
| 1121 | * nvmem_cell_put() - Release previously allocated nvmem cell. |
| 1122 | * |
Vivek Gautam | 2914326 | 2017-01-22 23:02:39 +0000 | [diff] [blame] | 1123 | * @cell: Previously allocated nvmem cell by nvmem_cell_get(). |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1124 | */ |
| 1125 | void nvmem_cell_put(struct nvmem_cell *cell) |
| 1126 | { |
| 1127 | struct nvmem_device *nvmem = cell->nvmem; |
| 1128 | |
| 1129 | __nvmem_device_put(nvmem); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1130 | } |
| 1131 | EXPORT_SYMBOL_GPL(nvmem_cell_put); |
| 1132 | |
Masahiro Yamada | f7c04f1 | 2017-09-11 11:00:13 +0200 | [diff] [blame] | 1133 | static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1134 | { |
| 1135 | u8 *p, *b; |
| 1136 | int i, bit_offset = cell->bit_offset; |
| 1137 | |
| 1138 | p = b = buf; |
| 1139 | if (bit_offset) { |
| 1140 | /* First shift */ |
| 1141 | *b++ >>= bit_offset; |
| 1142 | |
| 1143 | /* setup rest of the bytes if any */ |
| 1144 | for (i = 1; i < cell->bytes; i++) { |
| 1145 | /* Get bits from next byte and shift them towards msb */ |
| 1146 | *p |= *b << (BITS_PER_BYTE - bit_offset); |
| 1147 | |
| 1148 | p = b; |
| 1149 | *b++ >>= bit_offset; |
| 1150 | } |
| 1151 | |
| 1152 | /* result fits in less bytes */ |
| 1153 | if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE)) |
| 1154 | *p-- = 0; |
| 1155 | } |
| 1156 | /* clear msb bits if any leftover in the last byte */ |
| 1157 | *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0); |
| 1158 | } |
| 1159 | |
| 1160 | static int __nvmem_cell_read(struct nvmem_device *nvmem, |
| 1161 | struct nvmem_cell *cell, |
| 1162 | void *buf, size_t *len) |
| 1163 | { |
| 1164 | int rc; |
| 1165 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 1166 | rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1167 | |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 1168 | if (rc) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1169 | return rc; |
| 1170 | |
| 1171 | /* shift bits in-place */ |
Axel Lin | cbf854a | 2015-09-30 13:35:15 +0100 | [diff] [blame] | 1172 | if (cell->bit_offset || cell->nbits) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1173 | nvmem_shift_read_buffer_in_place(cell, buf); |
| 1174 | |
Vivek Gautam | 3b4a687 | 2017-01-22 23:02:38 +0000 | [diff] [blame] | 1175 | if (len) |
| 1176 | *len = cell->bytes; |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1177 | |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | /** |
| 1182 | * nvmem_cell_read() - Read a given nvmem cell |
| 1183 | * |
| 1184 | * @cell: nvmem cell to be read. |
Vivek Gautam | 3b4a687 | 2017-01-22 23:02:38 +0000 | [diff] [blame] | 1185 | * @len: pointer to length of cell which will be populated on successful read; |
| 1186 | * can be NULL. |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1187 | * |
Brian Norris | b577faf | 2017-01-04 16:18:11 +0000 | [diff] [blame] | 1188 | * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The |
| 1189 | * buffer should be freed by the consumer with a kfree(). |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1190 | */ |
| 1191 | void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len) |
| 1192 | { |
| 1193 | struct nvmem_device *nvmem = cell->nvmem; |
| 1194 | u8 *buf; |
| 1195 | int rc; |
| 1196 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 1197 | if (!nvmem) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1198 | return ERR_PTR(-EINVAL); |
| 1199 | |
| 1200 | buf = kzalloc(cell->bytes, GFP_KERNEL); |
| 1201 | if (!buf) |
| 1202 | return ERR_PTR(-ENOMEM); |
| 1203 | |
| 1204 | rc = __nvmem_cell_read(nvmem, cell, buf, len); |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 1205 | if (rc) { |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1206 | kfree(buf); |
| 1207 | return ERR_PTR(rc); |
| 1208 | } |
| 1209 | |
| 1210 | return buf; |
| 1211 | } |
| 1212 | EXPORT_SYMBOL_GPL(nvmem_cell_read); |
| 1213 | |
Masahiro Yamada | f7c04f1 | 2017-09-11 11:00:13 +0200 | [diff] [blame] | 1214 | static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell, |
| 1215 | u8 *_buf, int len) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1216 | { |
| 1217 | struct nvmem_device *nvmem = cell->nvmem; |
| 1218 | int i, rc, nbits, bit_offset = cell->bit_offset; |
| 1219 | u8 v, *p, *buf, *b, pbyte, pbits; |
| 1220 | |
| 1221 | nbits = cell->nbits; |
| 1222 | buf = kzalloc(cell->bytes, GFP_KERNEL); |
| 1223 | if (!buf) |
| 1224 | return ERR_PTR(-ENOMEM); |
| 1225 | |
| 1226 | memcpy(buf, _buf, len); |
| 1227 | p = b = buf; |
| 1228 | |
| 1229 | if (bit_offset) { |
| 1230 | pbyte = *b; |
| 1231 | *b <<= bit_offset; |
| 1232 | |
| 1233 | /* setup the first byte with lsb bits from nvmem */ |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 1234 | rc = nvmem_reg_read(nvmem, cell->offset, &v, 1); |
Mathieu Malaterre | 50808bf | 2018-05-11 12:07:03 +0100 | [diff] [blame] | 1235 | if (rc) |
| 1236 | goto err; |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1237 | *b++ |= GENMASK(bit_offset - 1, 0) & v; |
| 1238 | |
| 1239 | /* setup rest of the byte if any */ |
| 1240 | for (i = 1; i < cell->bytes; i++) { |
| 1241 | /* Get last byte bits and shift them towards lsb */ |
| 1242 | pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset); |
| 1243 | pbyte = *b; |
| 1244 | p = b; |
| 1245 | *b <<= bit_offset; |
| 1246 | *b++ |= pbits; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | /* if it's not end on byte boundary */ |
| 1251 | if ((nbits + bit_offset) % BITS_PER_BYTE) { |
| 1252 | /* setup the last byte with msb bits from nvmem */ |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 1253 | rc = nvmem_reg_read(nvmem, |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1254 | cell->offset + cell->bytes - 1, &v, 1); |
Mathieu Malaterre | 50808bf | 2018-05-11 12:07:03 +0100 | [diff] [blame] | 1255 | if (rc) |
| 1256 | goto err; |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1257 | *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v; |
| 1258 | |
| 1259 | } |
| 1260 | |
| 1261 | return buf; |
Mathieu Malaterre | 50808bf | 2018-05-11 12:07:03 +0100 | [diff] [blame] | 1262 | err: |
| 1263 | kfree(buf); |
| 1264 | return ERR_PTR(rc); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | /** |
| 1268 | * nvmem_cell_write() - Write to a given nvmem cell |
| 1269 | * |
| 1270 | * @cell: nvmem cell to be written. |
| 1271 | * @buf: Buffer to be written. |
| 1272 | * @len: length of buffer to be written to nvmem cell. |
| 1273 | * |
| 1274 | * Return: length of bytes written or negative on failure. |
| 1275 | */ |
| 1276 | int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len) |
| 1277 | { |
| 1278 | struct nvmem_device *nvmem = cell->nvmem; |
| 1279 | int rc; |
| 1280 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 1281 | if (!nvmem || nvmem->read_only || |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1282 | (cell->bit_offset == 0 && len != cell->bytes)) |
| 1283 | return -EINVAL; |
| 1284 | |
| 1285 | if (cell->bit_offset || cell->nbits) { |
| 1286 | buf = nvmem_cell_prepare_write_buffer(cell, buf, len); |
| 1287 | if (IS_ERR(buf)) |
| 1288 | return PTR_ERR(buf); |
| 1289 | } |
| 1290 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 1291 | rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1292 | |
| 1293 | /* free the tmp buffer */ |
Axel Lin | ace2217 | 2015-09-30 13:36:10 +0100 | [diff] [blame] | 1294 | if (cell->bit_offset || cell->nbits) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1295 | kfree(buf); |
| 1296 | |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 1297 | if (rc) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 1298 | return rc; |
| 1299 | |
| 1300 | return len; |
| 1301 | } |
| 1302 | EXPORT_SYMBOL_GPL(nvmem_cell_write); |
| 1303 | |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1304 | /** |
Leonard Crestez | d026d70 | 2017-07-26 11:34:46 +0200 | [diff] [blame] | 1305 | * nvmem_cell_read_u32() - Read a cell value as an u32 |
| 1306 | * |
| 1307 | * @dev: Device that requests the nvmem cell. |
| 1308 | * @cell_id: Name of nvmem cell to read. |
| 1309 | * @val: pointer to output value. |
| 1310 | * |
| 1311 | * Return: 0 on success or negative errno. |
| 1312 | */ |
| 1313 | int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val) |
| 1314 | { |
| 1315 | struct nvmem_cell *cell; |
| 1316 | void *buf; |
| 1317 | size_t len; |
| 1318 | |
| 1319 | cell = nvmem_cell_get(dev, cell_id); |
| 1320 | if (IS_ERR(cell)) |
| 1321 | return PTR_ERR(cell); |
| 1322 | |
| 1323 | buf = nvmem_cell_read(cell, &len); |
| 1324 | if (IS_ERR(buf)) { |
| 1325 | nvmem_cell_put(cell); |
| 1326 | return PTR_ERR(buf); |
| 1327 | } |
| 1328 | if (len != sizeof(*val)) { |
| 1329 | kfree(buf); |
| 1330 | nvmem_cell_put(cell); |
| 1331 | return -EINVAL; |
| 1332 | } |
| 1333 | memcpy(val, buf, sizeof(*val)); |
| 1334 | |
| 1335 | kfree(buf); |
| 1336 | nvmem_cell_put(cell); |
| 1337 | return 0; |
| 1338 | } |
| 1339 | EXPORT_SYMBOL_GPL(nvmem_cell_read_u32); |
| 1340 | |
| 1341 | /** |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1342 | * nvmem_device_cell_read() - Read a given nvmem device and cell |
| 1343 | * |
| 1344 | * @nvmem: nvmem device to read from. |
| 1345 | * @info: nvmem cell info to be read. |
| 1346 | * @buf: buffer pointer which will be populated on successful read. |
| 1347 | * |
| 1348 | * Return: length of successful bytes read on success and negative |
| 1349 | * error code on error. |
| 1350 | */ |
| 1351 | ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem, |
| 1352 | struct nvmem_cell_info *info, void *buf) |
| 1353 | { |
| 1354 | struct nvmem_cell cell; |
| 1355 | int rc; |
| 1356 | ssize_t len; |
| 1357 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 1358 | if (!nvmem) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1359 | return -EINVAL; |
| 1360 | |
| 1361 | rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 1362 | if (rc) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1363 | return rc; |
| 1364 | |
| 1365 | rc = __nvmem_cell_read(nvmem, &cell, buf, &len); |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 1366 | if (rc) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1367 | return rc; |
| 1368 | |
| 1369 | return len; |
| 1370 | } |
| 1371 | EXPORT_SYMBOL_GPL(nvmem_device_cell_read); |
| 1372 | |
| 1373 | /** |
| 1374 | * nvmem_device_cell_write() - Write cell to a given nvmem device |
| 1375 | * |
| 1376 | * @nvmem: nvmem device to be written to. |
Vivek Gautam | 2914326 | 2017-01-22 23:02:39 +0000 | [diff] [blame] | 1377 | * @info: nvmem cell info to be written. |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1378 | * @buf: buffer to be written to cell. |
| 1379 | * |
| 1380 | * Return: length of bytes written or negative error code on failure. |
Bartosz Golaszewski | 48f63a2 | 2018-09-21 06:40:23 -0700 | [diff] [blame] | 1381 | */ |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1382 | int nvmem_device_cell_write(struct nvmem_device *nvmem, |
| 1383 | struct nvmem_cell_info *info, void *buf) |
| 1384 | { |
| 1385 | struct nvmem_cell cell; |
| 1386 | int rc; |
| 1387 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 1388 | if (!nvmem) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1389 | return -EINVAL; |
| 1390 | |
| 1391 | rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 1392 | if (rc) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1393 | return rc; |
| 1394 | |
| 1395 | return nvmem_cell_write(&cell, buf, cell.bytes); |
| 1396 | } |
| 1397 | EXPORT_SYMBOL_GPL(nvmem_device_cell_write); |
| 1398 | |
| 1399 | /** |
| 1400 | * nvmem_device_read() - Read from a given nvmem device |
| 1401 | * |
| 1402 | * @nvmem: nvmem device to read from. |
| 1403 | * @offset: offset in nvmem device. |
| 1404 | * @bytes: number of bytes to read. |
| 1405 | * @buf: buffer pointer which will be populated on successful read. |
| 1406 | * |
| 1407 | * Return: length of successful bytes read on success and negative |
| 1408 | * error code on error. |
| 1409 | */ |
| 1410 | int nvmem_device_read(struct nvmem_device *nvmem, |
| 1411 | unsigned int offset, |
| 1412 | size_t bytes, void *buf) |
| 1413 | { |
| 1414 | int rc; |
| 1415 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 1416 | if (!nvmem) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1417 | return -EINVAL; |
| 1418 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 1419 | rc = nvmem_reg_read(nvmem, offset, buf, bytes); |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1420 | |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 1421 | if (rc) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1422 | return rc; |
| 1423 | |
| 1424 | return bytes; |
| 1425 | } |
| 1426 | EXPORT_SYMBOL_GPL(nvmem_device_read); |
| 1427 | |
| 1428 | /** |
| 1429 | * nvmem_device_write() - Write cell to a given nvmem device |
| 1430 | * |
| 1431 | * @nvmem: nvmem device to be written to. |
| 1432 | * @offset: offset in nvmem device. |
| 1433 | * @bytes: number of bytes to write. |
| 1434 | * @buf: buffer to be written. |
| 1435 | * |
| 1436 | * Return: length of bytes written or negative error code on failure. |
Bartosz Golaszewski | 48f63a2 | 2018-09-21 06:40:23 -0700 | [diff] [blame] | 1437 | */ |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1438 | int nvmem_device_write(struct nvmem_device *nvmem, |
| 1439 | unsigned int offset, |
| 1440 | size_t bytes, void *buf) |
| 1441 | { |
| 1442 | int rc; |
| 1443 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 1444 | if (!nvmem) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1445 | return -EINVAL; |
| 1446 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 1447 | rc = nvmem_reg_write(nvmem, offset, buf, bytes); |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1448 | |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 1449 | if (rc) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 1450 | return rc; |
| 1451 | |
| 1452 | |
| 1453 | return bytes; |
| 1454 | } |
| 1455 | EXPORT_SYMBOL_GPL(nvmem_device_write); |
| 1456 | |
Bartosz Golaszewski | d7b9fd1 | 2018-09-21 06:40:03 -0700 | [diff] [blame] | 1457 | /** |
Bartosz Golaszewski | b985f4c | 2018-09-21 06:40:15 -0700 | [diff] [blame] | 1458 | * nvmem_add_cell_table() - register a table of cell info entries |
| 1459 | * |
| 1460 | * @table: table of cell info entries |
| 1461 | */ |
| 1462 | void nvmem_add_cell_table(struct nvmem_cell_table *table) |
| 1463 | { |
| 1464 | mutex_lock(&nvmem_cell_mutex); |
| 1465 | list_add_tail(&table->node, &nvmem_cell_tables); |
| 1466 | mutex_unlock(&nvmem_cell_mutex); |
| 1467 | } |
| 1468 | EXPORT_SYMBOL_GPL(nvmem_add_cell_table); |
| 1469 | |
| 1470 | /** |
| 1471 | * nvmem_del_cell_table() - remove a previously registered cell info table |
| 1472 | * |
| 1473 | * @table: table of cell info entries |
| 1474 | */ |
| 1475 | void nvmem_del_cell_table(struct nvmem_cell_table *table) |
| 1476 | { |
| 1477 | mutex_lock(&nvmem_cell_mutex); |
| 1478 | list_del(&table->node); |
| 1479 | mutex_unlock(&nvmem_cell_mutex); |
| 1480 | } |
| 1481 | EXPORT_SYMBOL_GPL(nvmem_del_cell_table); |
| 1482 | |
| 1483 | /** |
Bartosz Golaszewski | 506157b | 2018-09-21 06:40:17 -0700 | [diff] [blame] | 1484 | * nvmem_add_cell_lookups() - register a list of cell lookup entries |
| 1485 | * |
| 1486 | * @entries: array of cell lookup entries |
| 1487 | * @nentries: number of cell lookup entries in the array |
| 1488 | */ |
| 1489 | void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries) |
| 1490 | { |
| 1491 | int i; |
| 1492 | |
| 1493 | mutex_lock(&nvmem_lookup_mutex); |
| 1494 | for (i = 0; i < nentries; i++) |
| 1495 | list_add_tail(&entries[i].node, &nvmem_lookup_list); |
| 1496 | mutex_unlock(&nvmem_lookup_mutex); |
| 1497 | } |
| 1498 | EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups); |
| 1499 | |
| 1500 | /** |
| 1501 | * nvmem_del_cell_lookups() - remove a list of previously added cell lookup |
| 1502 | * entries |
| 1503 | * |
| 1504 | * @entries: array of cell lookup entries |
| 1505 | * @nentries: number of cell lookup entries in the array |
| 1506 | */ |
| 1507 | void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries) |
| 1508 | { |
| 1509 | int i; |
| 1510 | |
| 1511 | mutex_lock(&nvmem_lookup_mutex); |
| 1512 | for (i = 0; i < nentries; i++) |
| 1513 | list_del(&entries[i].node); |
| 1514 | mutex_unlock(&nvmem_lookup_mutex); |
| 1515 | } |
| 1516 | EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups); |
| 1517 | |
| 1518 | /** |
Bartosz Golaszewski | d7b9fd1 | 2018-09-21 06:40:03 -0700 | [diff] [blame] | 1519 | * nvmem_dev_name() - Get the name of a given nvmem device. |
| 1520 | * |
| 1521 | * @nvmem: nvmem device. |
| 1522 | * |
| 1523 | * Return: name of the nvmem device. |
| 1524 | */ |
| 1525 | const char *nvmem_dev_name(struct nvmem_device *nvmem) |
| 1526 | { |
| 1527 | return dev_name(&nvmem->dev); |
| 1528 | } |
| 1529 | EXPORT_SYMBOL_GPL(nvmem_dev_name); |
| 1530 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 1531 | static int __init nvmem_init(void) |
| 1532 | { |
| 1533 | return bus_register(&nvmem_bus_type); |
| 1534 | } |
| 1535 | |
| 1536 | static void __exit nvmem_exit(void) |
| 1537 | { |
| 1538 | bus_unregister(&nvmem_bus_type); |
| 1539 | } |
| 1540 | |
| 1541 | subsys_initcall(nvmem_init); |
| 1542 | module_exit(nvmem_exit); |
| 1543 | |
| 1544 | MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org"); |
| 1545 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com"); |
| 1546 | MODULE_DESCRIPTION("nvmem Driver Core"); |
| 1547 | MODULE_LICENSE("GPL v2"); |