| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * nvmem framework core. |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org> |
|---|
| 5 | 6 | * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com> |
|---|
| 6 | | - * |
|---|
| 7 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 8 | | - * it under the terms of the GNU General Public License version 2 and |
|---|
| 9 | | - * only version 2 as published by the Free Software Foundation. |
|---|
| 10 | | - * |
|---|
| 11 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 12 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | | - * GNU General Public License for more details. |
|---|
| 15 | 7 | */ |
|---|
| 16 | 8 | |
|---|
| 17 | 9 | #include <linux/device.h> |
|---|
| .. | .. |
|---|
| 23 | 15 | #include <linux/module.h> |
|---|
| 24 | 16 | #include <linux/nvmem-consumer.h> |
|---|
| 25 | 17 | #include <linux/nvmem-provider.h> |
|---|
| 18 | +#include <linux/gpio/consumer.h> |
|---|
| 26 | 19 | #include <linux/of.h> |
|---|
| 27 | 20 | #include <linux/slab.h> |
|---|
| 28 | 21 | |
|---|
| 29 | | -#include "nvmem.h" |
|---|
| 22 | +struct nvmem_device { |
|---|
| 23 | + struct module *owner; |
|---|
| 24 | + struct device dev; |
|---|
| 25 | + int stride; |
|---|
| 26 | + int word_size; |
|---|
| 27 | + int id; |
|---|
| 28 | + struct kref refcnt; |
|---|
| 29 | + size_t size; |
|---|
| 30 | + bool read_only; |
|---|
| 31 | + bool root_only; |
|---|
| 32 | + int flags; |
|---|
| 33 | + enum nvmem_type type; |
|---|
| 34 | + struct bin_attribute eeprom; |
|---|
| 35 | + struct device *base_dev; |
|---|
| 36 | + struct list_head cells; |
|---|
| 37 | + nvmem_reg_read_t reg_read; |
|---|
| 38 | + nvmem_reg_write_t reg_write; |
|---|
| 39 | + struct gpio_desc *wp_gpio; |
|---|
| 40 | + void *priv; |
|---|
| 41 | +}; |
|---|
| 42 | + |
|---|
| 43 | +#define to_nvmem_device(d) container_of(d, struct nvmem_device, dev) |
|---|
| 44 | + |
|---|
| 45 | +#define FLAG_COMPAT BIT(0) |
|---|
| 30 | 46 | |
|---|
| 31 | 47 | struct nvmem_cell { |
|---|
| 32 | 48 | const char *name; |
|---|
| .. | .. |
|---|
| 45 | 61 | static DEFINE_MUTEX(nvmem_cell_mutex); |
|---|
| 46 | 62 | static LIST_HEAD(nvmem_cell_tables); |
|---|
| 47 | 63 | |
|---|
| 64 | +static DEFINE_MUTEX(nvmem_lookup_mutex); |
|---|
| 65 | +static LIST_HEAD(nvmem_lookup_list); |
|---|
| 66 | + |
|---|
| 67 | +static BLOCKING_NOTIFIER_HEAD(nvmem_notifier); |
|---|
| 68 | + |
|---|
| 48 | 69 | static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, |
|---|
| 49 | 70 | void *val, size_t bytes) |
|---|
| 50 | 71 | { |
|---|
| .. | .. |
|---|
| 57 | 78 | static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, |
|---|
| 58 | 79 | void *val, size_t bytes) |
|---|
| 59 | 80 | { |
|---|
| 60 | | - if (nvmem->reg_write) |
|---|
| 61 | | - return nvmem->reg_write(nvmem->priv, offset, val, bytes); |
|---|
| 81 | + int ret; |
|---|
| 82 | + |
|---|
| 83 | + if (nvmem->reg_write) { |
|---|
| 84 | + gpiod_set_value_cansleep(nvmem->wp_gpio, 0); |
|---|
| 85 | + ret = nvmem->reg_write(nvmem->priv, offset, val, bytes); |
|---|
| 86 | + gpiod_set_value_cansleep(nvmem->wp_gpio, 1); |
|---|
| 87 | + return ret; |
|---|
| 88 | + } |
|---|
| 62 | 89 | |
|---|
| 63 | 90 | return -EINVAL; |
|---|
| 64 | 91 | } |
|---|
| 92 | + |
|---|
| 93 | +#ifdef CONFIG_NVMEM_SYSFS |
|---|
| 94 | +static const char * const nvmem_type_str[] = { |
|---|
| 95 | + [NVMEM_TYPE_UNKNOWN] = "Unknown", |
|---|
| 96 | + [NVMEM_TYPE_EEPROM] = "EEPROM", |
|---|
| 97 | + [NVMEM_TYPE_OTP] = "OTP", |
|---|
| 98 | + [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed", |
|---|
| 99 | +}; |
|---|
| 100 | + |
|---|
| 101 | +#ifdef CONFIG_DEBUG_LOCK_ALLOC |
|---|
| 102 | +static struct lock_class_key eeprom_lock_key; |
|---|
| 103 | +#endif |
|---|
| 104 | + |
|---|
| 105 | +static ssize_t type_show(struct device *dev, |
|---|
| 106 | + struct device_attribute *attr, char *buf) |
|---|
| 107 | +{ |
|---|
| 108 | + struct nvmem_device *nvmem = to_nvmem_device(dev); |
|---|
| 109 | + |
|---|
| 110 | + return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]); |
|---|
| 111 | +} |
|---|
| 112 | + |
|---|
| 113 | +static DEVICE_ATTR_RO(type); |
|---|
| 114 | + |
|---|
| 115 | +static struct attribute *nvmem_attrs[] = { |
|---|
| 116 | + &dev_attr_type.attr, |
|---|
| 117 | + NULL, |
|---|
| 118 | +}; |
|---|
| 119 | + |
|---|
| 120 | +static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj, |
|---|
| 121 | + struct bin_attribute *attr, char *buf, |
|---|
| 122 | + loff_t pos, size_t count) |
|---|
| 123 | +{ |
|---|
| 124 | + struct device *dev; |
|---|
| 125 | + struct nvmem_device *nvmem; |
|---|
| 126 | + int rc; |
|---|
| 127 | + |
|---|
| 128 | + if (attr->private) |
|---|
| 129 | + dev = attr->private; |
|---|
| 130 | + else |
|---|
| 131 | + dev = kobj_to_dev(kobj); |
|---|
| 132 | + nvmem = to_nvmem_device(dev); |
|---|
| 133 | + |
|---|
| 134 | + /* Stop the user from reading */ |
|---|
| 135 | + if (pos >= nvmem->size) |
|---|
| 136 | + return 0; |
|---|
| 137 | + |
|---|
| 138 | + if (!IS_ALIGNED(pos, nvmem->stride)) |
|---|
| 139 | + return -EINVAL; |
|---|
| 140 | + |
|---|
| 141 | + if (count < nvmem->word_size) |
|---|
| 142 | + return -EINVAL; |
|---|
| 143 | + |
|---|
| 144 | + if (pos + count > nvmem->size) |
|---|
| 145 | + count = nvmem->size - pos; |
|---|
| 146 | + |
|---|
| 147 | + count = round_down(count, nvmem->word_size); |
|---|
| 148 | + |
|---|
| 149 | + if (!nvmem->reg_read) |
|---|
| 150 | + return -EPERM; |
|---|
| 151 | + |
|---|
| 152 | + rc = nvmem_reg_read(nvmem, pos, buf, count); |
|---|
| 153 | + |
|---|
| 154 | + if (rc) |
|---|
| 155 | + return rc; |
|---|
| 156 | + |
|---|
| 157 | + return count; |
|---|
| 158 | +} |
|---|
| 159 | + |
|---|
| 160 | +static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj, |
|---|
| 161 | + struct bin_attribute *attr, char *buf, |
|---|
| 162 | + loff_t pos, size_t count) |
|---|
| 163 | +{ |
|---|
| 164 | + struct device *dev; |
|---|
| 165 | + struct nvmem_device *nvmem; |
|---|
| 166 | + int rc; |
|---|
| 167 | + |
|---|
| 168 | + if (attr->private) |
|---|
| 169 | + dev = attr->private; |
|---|
| 170 | + else |
|---|
| 171 | + dev = kobj_to_dev(kobj); |
|---|
| 172 | + nvmem = to_nvmem_device(dev); |
|---|
| 173 | + |
|---|
| 174 | + /* Stop the user from writing */ |
|---|
| 175 | + if (pos >= nvmem->size) |
|---|
| 176 | + return -EFBIG; |
|---|
| 177 | + |
|---|
| 178 | + if (!IS_ALIGNED(pos, nvmem->stride)) |
|---|
| 179 | + return -EINVAL; |
|---|
| 180 | + |
|---|
| 181 | + if (count < nvmem->word_size) |
|---|
| 182 | + return -EINVAL; |
|---|
| 183 | + |
|---|
| 184 | + if (pos + count > nvmem->size) |
|---|
| 185 | + count = nvmem->size - pos; |
|---|
| 186 | + |
|---|
| 187 | + count = round_down(count, nvmem->word_size); |
|---|
| 188 | + |
|---|
| 189 | + if (!nvmem->reg_write) |
|---|
| 190 | + return -EPERM; |
|---|
| 191 | + |
|---|
| 192 | + rc = nvmem_reg_write(nvmem, pos, buf, count); |
|---|
| 193 | + |
|---|
| 194 | + if (rc) |
|---|
| 195 | + return rc; |
|---|
| 196 | + |
|---|
| 197 | + return count; |
|---|
| 198 | +} |
|---|
| 199 | + |
|---|
| 200 | +static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem) |
|---|
| 201 | +{ |
|---|
| 202 | + umode_t mode = 0400; |
|---|
| 203 | + |
|---|
| 204 | + if (!nvmem->root_only) |
|---|
| 205 | + mode |= 0044; |
|---|
| 206 | + |
|---|
| 207 | + if (!nvmem->read_only) |
|---|
| 208 | + mode |= 0200; |
|---|
| 209 | + |
|---|
| 210 | + if (!nvmem->reg_write) |
|---|
| 211 | + mode &= ~0200; |
|---|
| 212 | + |
|---|
| 213 | + if (!nvmem->reg_read) |
|---|
| 214 | + mode &= ~0444; |
|---|
| 215 | + |
|---|
| 216 | + return mode; |
|---|
| 217 | +} |
|---|
| 218 | + |
|---|
| 219 | +static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj, |
|---|
| 220 | + struct bin_attribute *attr, int i) |
|---|
| 221 | +{ |
|---|
| 222 | + struct device *dev = kobj_to_dev(kobj); |
|---|
| 223 | + struct nvmem_device *nvmem = to_nvmem_device(dev); |
|---|
| 224 | + |
|---|
| 225 | + attr->size = nvmem->size; |
|---|
| 226 | + |
|---|
| 227 | + return nvmem_bin_attr_get_umode(nvmem); |
|---|
| 228 | +} |
|---|
| 229 | + |
|---|
| 230 | +/* default read/write permissions */ |
|---|
| 231 | +static struct bin_attribute bin_attr_rw_nvmem = { |
|---|
| 232 | + .attr = { |
|---|
| 233 | + .name = "nvmem", |
|---|
| 234 | + .mode = 0644, |
|---|
| 235 | + }, |
|---|
| 236 | + .read = bin_attr_nvmem_read, |
|---|
| 237 | + .write = bin_attr_nvmem_write, |
|---|
| 238 | +}; |
|---|
| 239 | + |
|---|
| 240 | +static struct bin_attribute *nvmem_bin_attributes[] = { |
|---|
| 241 | + &bin_attr_rw_nvmem, |
|---|
| 242 | + NULL, |
|---|
| 243 | +}; |
|---|
| 244 | + |
|---|
| 245 | +static const struct attribute_group nvmem_bin_group = { |
|---|
| 246 | + .bin_attrs = nvmem_bin_attributes, |
|---|
| 247 | + .attrs = nvmem_attrs, |
|---|
| 248 | + .is_bin_visible = nvmem_bin_attr_is_visible, |
|---|
| 249 | +}; |
|---|
| 250 | + |
|---|
| 251 | +static const struct attribute_group *nvmem_dev_groups[] = { |
|---|
| 252 | + &nvmem_bin_group, |
|---|
| 253 | + NULL, |
|---|
| 254 | +}; |
|---|
| 255 | + |
|---|
| 256 | +static struct bin_attribute bin_attr_nvmem_eeprom_compat = { |
|---|
| 257 | + .attr = { |
|---|
| 258 | + .name = "eeprom", |
|---|
| 259 | + }, |
|---|
| 260 | + .read = bin_attr_nvmem_read, |
|---|
| 261 | + .write = bin_attr_nvmem_write, |
|---|
| 262 | +}; |
|---|
| 263 | + |
|---|
| 264 | +/* |
|---|
| 265 | + * nvmem_setup_compat() - Create an additional binary entry in |
|---|
| 266 | + * drivers sys directory, to be backwards compatible with the older |
|---|
| 267 | + * drivers/misc/eeprom drivers. |
|---|
| 268 | + */ |
|---|
| 269 | +static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, |
|---|
| 270 | + const struct nvmem_config *config) |
|---|
| 271 | +{ |
|---|
| 272 | + int rval; |
|---|
| 273 | + |
|---|
| 274 | + if (!config->compat) |
|---|
| 275 | + return 0; |
|---|
| 276 | + |
|---|
| 277 | + if (!config->base_dev) |
|---|
| 278 | + return -EINVAL; |
|---|
| 279 | + |
|---|
| 280 | + nvmem->eeprom = bin_attr_nvmem_eeprom_compat; |
|---|
| 281 | + nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem); |
|---|
| 282 | + nvmem->eeprom.size = nvmem->size; |
|---|
| 283 | +#ifdef CONFIG_DEBUG_LOCK_ALLOC |
|---|
| 284 | + nvmem->eeprom.attr.key = &eeprom_lock_key; |
|---|
| 285 | +#endif |
|---|
| 286 | + nvmem->eeprom.private = &nvmem->dev; |
|---|
| 287 | + nvmem->base_dev = config->base_dev; |
|---|
| 288 | + |
|---|
| 289 | + rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom); |
|---|
| 290 | + if (rval) { |
|---|
| 291 | + dev_err(&nvmem->dev, |
|---|
| 292 | + "Failed to create eeprom binary file %d\n", rval); |
|---|
| 293 | + return rval; |
|---|
| 294 | + } |
|---|
| 295 | + |
|---|
| 296 | + nvmem->flags |= FLAG_COMPAT; |
|---|
| 297 | + |
|---|
| 298 | + return 0; |
|---|
| 299 | +} |
|---|
| 300 | + |
|---|
| 301 | +static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, |
|---|
| 302 | + const struct nvmem_config *config) |
|---|
| 303 | +{ |
|---|
| 304 | + if (config->compat) |
|---|
| 305 | + device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); |
|---|
| 306 | +} |
|---|
| 307 | + |
|---|
| 308 | +#else /* CONFIG_NVMEM_SYSFS */ |
|---|
| 309 | + |
|---|
| 310 | +static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, |
|---|
| 311 | + const struct nvmem_config *config) |
|---|
| 312 | +{ |
|---|
| 313 | + return -ENOSYS; |
|---|
| 314 | +} |
|---|
| 315 | +static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, |
|---|
| 316 | + const struct nvmem_config *config) |
|---|
| 317 | +{ |
|---|
| 318 | +} |
|---|
| 319 | + |
|---|
| 320 | +#endif /* CONFIG_NVMEM_SYSFS */ |
|---|
| 65 | 321 | |
|---|
| 66 | 322 | static void nvmem_release(struct device *dev) |
|---|
| 67 | 323 | { |
|---|
| 68 | 324 | struct nvmem_device *nvmem = to_nvmem_device(dev); |
|---|
| 69 | 325 | |
|---|
| 70 | | - ida_simple_remove(&nvmem_ida, nvmem->id); |
|---|
| 326 | + ida_free(&nvmem_ida, nvmem->id); |
|---|
| 327 | + gpiod_put(nvmem->wp_gpio); |
|---|
| 71 | 328 | kfree(nvmem); |
|---|
| 72 | 329 | } |
|---|
| 73 | 330 | |
|---|
| .. | .. |
|---|
| 79 | 336 | .name = "nvmem", |
|---|
| 80 | 337 | }; |
|---|
| 81 | 338 | |
|---|
| 82 | | -static int of_nvmem_match(struct device *dev, void *nvmem_np) |
|---|
| 83 | | -{ |
|---|
| 84 | | - return dev->of_node == nvmem_np; |
|---|
| 85 | | -} |
|---|
| 86 | | - |
|---|
| 87 | | -static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np) |
|---|
| 88 | | -{ |
|---|
| 89 | | - struct device *d; |
|---|
| 90 | | - |
|---|
| 91 | | - if (!nvmem_np) |
|---|
| 92 | | - return NULL; |
|---|
| 93 | | - |
|---|
| 94 | | - d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match); |
|---|
| 95 | | - |
|---|
| 96 | | - if (!d) |
|---|
| 97 | | - return NULL; |
|---|
| 98 | | - |
|---|
| 99 | | - return to_nvmem_device(d); |
|---|
| 100 | | -} |
|---|
| 101 | | - |
|---|
| 102 | 339 | static void nvmem_cell_drop(struct nvmem_cell *cell) |
|---|
| 103 | 340 | { |
|---|
| 341 | + blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell); |
|---|
| 104 | 342 | mutex_lock(&nvmem_mutex); |
|---|
| 105 | 343 | list_del(&cell->node); |
|---|
| 106 | 344 | mutex_unlock(&nvmem_mutex); |
|---|
| 107 | 345 | of_node_put(cell->np); |
|---|
| 346 | + kfree_const(cell->name); |
|---|
| 108 | 347 | kfree(cell); |
|---|
| 109 | 348 | } |
|---|
| 110 | 349 | |
|---|
| .. | .. |
|---|
| 121 | 360 | mutex_lock(&nvmem_mutex); |
|---|
| 122 | 361 | list_add_tail(&cell->node, &cell->nvmem->cells); |
|---|
| 123 | 362 | mutex_unlock(&nvmem_mutex); |
|---|
| 363 | + blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell); |
|---|
| 124 | 364 | } |
|---|
| 125 | 365 | |
|---|
| 126 | | -static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, |
|---|
| 127 | | - const struct nvmem_cell_info *info, |
|---|
| 128 | | - struct nvmem_cell *cell) |
|---|
| 366 | +static int nvmem_cell_info_to_nvmem_cell_nodup(struct nvmem_device *nvmem, |
|---|
| 367 | + const struct nvmem_cell_info *info, |
|---|
| 368 | + struct nvmem_cell *cell) |
|---|
| 129 | 369 | { |
|---|
| 130 | 370 | cell->nvmem = nvmem; |
|---|
| 131 | 371 | cell->offset = info->offset; |
|---|
| .. | .. |
|---|
| 142 | 382 | if (!IS_ALIGNED(cell->offset, nvmem->stride)) { |
|---|
| 143 | 383 | dev_err(&nvmem->dev, |
|---|
| 144 | 384 | "cell %s unaligned to nvmem stride %d\n", |
|---|
| 145 | | - cell->name, nvmem->stride); |
|---|
| 385 | + cell->name ?: "<unknown>", nvmem->stride); |
|---|
| 146 | 386 | return -EINVAL; |
|---|
| 147 | 387 | } |
|---|
| 388 | + |
|---|
| 389 | + return 0; |
|---|
| 390 | +} |
|---|
| 391 | + |
|---|
| 392 | +static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, |
|---|
| 393 | + const struct nvmem_cell_info *info, |
|---|
| 394 | + struct nvmem_cell *cell) |
|---|
| 395 | +{ |
|---|
| 396 | + int err; |
|---|
| 397 | + |
|---|
| 398 | + err = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, cell); |
|---|
| 399 | + if (err) |
|---|
| 400 | + return err; |
|---|
| 401 | + |
|---|
| 402 | + cell->name = kstrdup_const(info->name, GFP_KERNEL); |
|---|
| 403 | + if (!cell->name) |
|---|
| 404 | + return -ENOMEM; |
|---|
| 148 | 405 | |
|---|
| 149 | 406 | return 0; |
|---|
| 150 | 407 | } |
|---|
| .. | .. |
|---|
| 158 | 415 | * |
|---|
| 159 | 416 | * Return: 0 or negative error code on failure. |
|---|
| 160 | 417 | */ |
|---|
| 161 | | -int nvmem_add_cells(struct nvmem_device *nvmem, |
|---|
| 418 | +static int nvmem_add_cells(struct nvmem_device *nvmem, |
|---|
| 162 | 419 | const struct nvmem_cell_info *info, |
|---|
| 163 | 420 | int ncells) |
|---|
| 164 | 421 | { |
|---|
| .. | .. |
|---|
| 197 | 454 | |
|---|
| 198 | 455 | return rval; |
|---|
| 199 | 456 | } |
|---|
| 200 | | -EXPORT_SYMBOL_GPL(nvmem_add_cells); |
|---|
| 457 | + |
|---|
| 458 | +/** |
|---|
| 459 | + * nvmem_register_notifier() - Register a notifier block for nvmem events. |
|---|
| 460 | + * |
|---|
| 461 | + * @nb: notifier block to be called on nvmem events. |
|---|
| 462 | + * |
|---|
| 463 | + * Return: 0 on success, negative error number on failure. |
|---|
| 464 | + */ |
|---|
| 465 | +int nvmem_register_notifier(struct notifier_block *nb) |
|---|
| 466 | +{ |
|---|
| 467 | + return blocking_notifier_chain_register(&nvmem_notifier, nb); |
|---|
| 468 | +} |
|---|
| 469 | +EXPORT_SYMBOL_GPL(nvmem_register_notifier); |
|---|
| 470 | + |
|---|
| 471 | +/** |
|---|
| 472 | + * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events. |
|---|
| 473 | + * |
|---|
| 474 | + * @nb: notifier block to be unregistered. |
|---|
| 475 | + * |
|---|
| 476 | + * Return: 0 on success, negative error number on failure. |
|---|
| 477 | + */ |
|---|
| 478 | +int nvmem_unregister_notifier(struct notifier_block *nb) |
|---|
| 479 | +{ |
|---|
| 480 | + return blocking_notifier_chain_unregister(&nvmem_notifier, nb); |
|---|
| 481 | +} |
|---|
| 482 | +EXPORT_SYMBOL_GPL(nvmem_unregister_notifier); |
|---|
| 201 | 483 | |
|---|
| 202 | 484 | static int nvmem_add_cells_from_table(struct nvmem_device *nvmem) |
|---|
| 203 | 485 | { |
|---|
| .. | .. |
|---|
| 236 | 518 | return rval; |
|---|
| 237 | 519 | } |
|---|
| 238 | 520 | |
|---|
| 521 | +static struct nvmem_cell * |
|---|
| 522 | +nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id) |
|---|
| 523 | +{ |
|---|
| 524 | + struct nvmem_cell *iter, *cell = NULL; |
|---|
| 525 | + |
|---|
| 526 | + mutex_lock(&nvmem_mutex); |
|---|
| 527 | + list_for_each_entry(iter, &nvmem->cells, node) { |
|---|
| 528 | + if (strcmp(cell_id, iter->name) == 0) { |
|---|
| 529 | + cell = iter; |
|---|
| 530 | + break; |
|---|
| 531 | + } |
|---|
| 532 | + } |
|---|
| 533 | + mutex_unlock(&nvmem_mutex); |
|---|
| 534 | + |
|---|
| 535 | + return cell; |
|---|
| 536 | +} |
|---|
| 537 | + |
|---|
| 239 | 538 | static int nvmem_add_cells_from_of(struct nvmem_device *nvmem) |
|---|
| 240 | 539 | { |
|---|
| 241 | 540 | struct device_node *parent, *child; |
|---|
| .. | .. |
|---|
| 248 | 547 | |
|---|
| 249 | 548 | for_each_child_of_node(parent, child) { |
|---|
| 250 | 549 | addr = of_get_property(child, "reg", &len); |
|---|
| 251 | | - if (!addr || (len < 2 * sizeof(u32))) { |
|---|
| 550 | + if (!addr) |
|---|
| 551 | + continue; |
|---|
| 552 | + if (len < 2 * sizeof(u32)) { |
|---|
| 252 | 553 | dev_err(dev, "nvmem: invalid reg on %pOF\n", child); |
|---|
| 554 | + of_node_put(child); |
|---|
| 253 | 555 | return -EINVAL; |
|---|
| 254 | 556 | } |
|---|
| 255 | 557 | |
|---|
| 256 | 558 | cell = kzalloc(sizeof(*cell), GFP_KERNEL); |
|---|
| 257 | | - if (!cell) |
|---|
| 559 | + if (!cell) { |
|---|
| 560 | + of_node_put(child); |
|---|
| 258 | 561 | return -ENOMEM; |
|---|
| 562 | + } |
|---|
| 259 | 563 | |
|---|
| 260 | 564 | cell->nvmem = nvmem; |
|---|
| 261 | | - cell->np = of_node_get(child); |
|---|
| 262 | 565 | cell->offset = be32_to_cpup(addr++); |
|---|
| 263 | 566 | cell->bytes = be32_to_cpup(addr); |
|---|
| 264 | | - cell->name = child->name; |
|---|
| 567 | + cell->name = kasprintf(GFP_KERNEL, "%pOFn", child); |
|---|
| 265 | 568 | |
|---|
| 266 | 569 | addr = of_get_property(child, "bits", &len); |
|---|
| 267 | 570 | if (addr && len == (2 * sizeof(u32))) { |
|---|
| .. | .. |
|---|
| 278 | 581 | dev_err(dev, "cell %s unaligned to nvmem stride %d\n", |
|---|
| 279 | 582 | cell->name, nvmem->stride); |
|---|
| 280 | 583 | /* Cells already added will be freed later. */ |
|---|
| 584 | + kfree_const(cell->name); |
|---|
| 281 | 585 | kfree(cell); |
|---|
| 586 | + of_node_put(child); |
|---|
| 282 | 587 | return -EINVAL; |
|---|
| 283 | 588 | } |
|---|
| 284 | 589 | |
|---|
| 590 | + cell->np = of_node_get(child); |
|---|
| 285 | 591 | nvmem_cell_add(cell); |
|---|
| 286 | 592 | } |
|---|
| 287 | 593 | |
|---|
| .. | .. |
|---|
| 290 | 596 | |
|---|
| 291 | 597 | /** |
|---|
| 292 | 598 | * nvmem_register() - Register a nvmem device for given nvmem_config. |
|---|
| 293 | | - * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem |
|---|
| 599 | + * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem |
|---|
| 294 | 600 | * |
|---|
| 295 | 601 | * @config: nvmem device configuration with which nvmem device is created. |
|---|
| 296 | 602 | * |
|---|
| .. | .. |
|---|
| 306 | 612 | if (!config->dev) |
|---|
| 307 | 613 | return ERR_PTR(-EINVAL); |
|---|
| 308 | 614 | |
|---|
| 615 | + if (!config->reg_read && !config->reg_write) |
|---|
| 616 | + return ERR_PTR(-EINVAL); |
|---|
| 617 | + |
|---|
| 309 | 618 | nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL); |
|---|
| 310 | 619 | if (!nvmem) |
|---|
| 311 | 620 | return ERR_PTR(-ENOMEM); |
|---|
| 312 | 621 | |
|---|
| 313 | | - rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL); |
|---|
| 622 | + rval = ida_alloc(&nvmem_ida, GFP_KERNEL); |
|---|
| 314 | 623 | if (rval < 0) { |
|---|
| 624 | + kfree(nvmem); |
|---|
| 625 | + return ERR_PTR(rval); |
|---|
| 626 | + } |
|---|
| 627 | + |
|---|
| 628 | + if (config->wp_gpio) |
|---|
| 629 | + nvmem->wp_gpio = config->wp_gpio; |
|---|
| 630 | + else |
|---|
| 631 | + nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp", |
|---|
| 632 | + GPIOD_OUT_HIGH); |
|---|
| 633 | + if (IS_ERR(nvmem->wp_gpio)) { |
|---|
| 634 | + ida_free(&nvmem_ida, nvmem->id); |
|---|
| 635 | + rval = PTR_ERR(nvmem->wp_gpio); |
|---|
| 315 | 636 | kfree(nvmem); |
|---|
| 316 | 637 | return ERR_PTR(rval); |
|---|
| 317 | 638 | } |
|---|
| .. | .. |
|---|
| 329 | 650 | nvmem->dev.type = &nvmem_provider_type; |
|---|
| 330 | 651 | nvmem->dev.bus = &nvmem_bus_type; |
|---|
| 331 | 652 | nvmem->dev.parent = config->dev; |
|---|
| 653 | + nvmem->root_only = config->root_only; |
|---|
| 332 | 654 | nvmem->priv = config->priv; |
|---|
| 655 | + nvmem->type = config->type; |
|---|
| 333 | 656 | nvmem->reg_read = config->reg_read; |
|---|
| 334 | 657 | nvmem->reg_write = config->reg_write; |
|---|
| 335 | | - nvmem->dev.of_node = config->dev->of_node; |
|---|
| 658 | + if (!config->no_of_node) |
|---|
| 659 | + nvmem->dev.of_node = config->dev->of_node; |
|---|
| 336 | 660 | |
|---|
| 337 | | - if (config->id == -1 && config->name) { |
|---|
| 661 | + switch (config->id) { |
|---|
| 662 | + case NVMEM_DEVID_NONE: |
|---|
| 338 | 663 | dev_set_name(&nvmem->dev, "%s", config->name); |
|---|
| 339 | | - } else { |
|---|
| 664 | + break; |
|---|
| 665 | + case NVMEM_DEVID_AUTO: |
|---|
| 666 | + dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id); |
|---|
| 667 | + break; |
|---|
| 668 | + default: |
|---|
| 340 | 669 | dev_set_name(&nvmem->dev, "%s%d", |
|---|
| 341 | 670 | config->name ? : "nvmem", |
|---|
| 342 | 671 | config->name ? config->id : nvmem->id); |
|---|
| 672 | + break; |
|---|
| 343 | 673 | } |
|---|
| 344 | 674 | |
|---|
| 345 | | - nvmem->read_only = device_property_present(config->dev, "read-only") | |
|---|
| 346 | | - config->read_only; |
|---|
| 675 | + nvmem->read_only = device_property_present(config->dev, "read-only") || |
|---|
| 676 | + config->read_only || !nvmem->reg_write; |
|---|
| 347 | 677 | |
|---|
| 348 | | - nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config); |
|---|
| 349 | | - |
|---|
| 350 | | - device_initialize(&nvmem->dev); |
|---|
| 678 | +#ifdef CONFIG_NVMEM_SYSFS |
|---|
| 679 | + nvmem->dev.groups = nvmem_dev_groups; |
|---|
| 680 | +#endif |
|---|
| 351 | 681 | |
|---|
| 352 | 682 | dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); |
|---|
| 353 | 683 | |
|---|
| 354 | | - rval = device_add(&nvmem->dev); |
|---|
| 684 | + rval = device_register(&nvmem->dev); |
|---|
| 355 | 685 | if (rval) |
|---|
| 356 | 686 | goto err_put_device; |
|---|
| 357 | 687 | |
|---|
| .. | .. |
|---|
| 375 | 705 | if (rval) |
|---|
| 376 | 706 | goto err_remove_cells; |
|---|
| 377 | 707 | |
|---|
| 708 | + blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); |
|---|
| 709 | + |
|---|
| 378 | 710 | return nvmem; |
|---|
| 379 | 711 | |
|---|
| 380 | 712 | err_remove_cells: |
|---|
| .. | .. |
|---|
| 397 | 729 | |
|---|
| 398 | 730 | nvmem = container_of(kref, struct nvmem_device, refcnt); |
|---|
| 399 | 731 | |
|---|
| 732 | + blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem); |
|---|
| 733 | + |
|---|
| 400 | 734 | if (nvmem->flags & FLAG_COMPAT) |
|---|
| 401 | 735 | device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); |
|---|
| 402 | 736 | |
|---|
| 403 | 737 | nvmem_device_remove_all_cells(nvmem); |
|---|
| 404 | | - device_del(&nvmem->dev); |
|---|
| 405 | | - put_device(&nvmem->dev); |
|---|
| 738 | + device_unregister(&nvmem->dev); |
|---|
| 406 | 739 | } |
|---|
| 407 | 740 | |
|---|
| 408 | 741 | /** |
|---|
| 409 | 742 | * nvmem_unregister() - Unregister previously registered nvmem device |
|---|
| 410 | 743 | * |
|---|
| 411 | 744 | * @nvmem: Pointer to previously registered nvmem device. |
|---|
| 412 | | - * |
|---|
| 413 | | - * Return: Will be an negative on error or a zero on success. |
|---|
| 414 | 745 | */ |
|---|
| 415 | | -int nvmem_unregister(struct nvmem_device *nvmem) |
|---|
| 746 | +void nvmem_unregister(struct nvmem_device *nvmem) |
|---|
| 416 | 747 | { |
|---|
| 417 | 748 | kref_put(&nvmem->refcnt, nvmem_device_release); |
|---|
| 418 | | - |
|---|
| 419 | | - return 0; |
|---|
| 420 | 749 | } |
|---|
| 421 | 750 | EXPORT_SYMBOL_GPL(nvmem_unregister); |
|---|
| 422 | 751 | |
|---|
| 423 | 752 | static void devm_nvmem_release(struct device *dev, void *res) |
|---|
| 424 | 753 | { |
|---|
| 425 | | - WARN_ON(nvmem_unregister(*(struct nvmem_device **)res)); |
|---|
| 754 | + nvmem_unregister(*(struct nvmem_device **)res); |
|---|
| 426 | 755 | } |
|---|
| 427 | 756 | |
|---|
| 428 | 757 | /** |
|---|
| 429 | 758 | * devm_nvmem_register() - Register a managed nvmem device for given |
|---|
| 430 | 759 | * nvmem_config. |
|---|
| 431 | | - * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem |
|---|
| 760 | + * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem |
|---|
| 432 | 761 | * |
|---|
| 433 | 762 | * @dev: Device that uses the nvmem device. |
|---|
| 434 | 763 | * @config: nvmem device configuration with which nvmem device is created. |
|---|
| .. | .. |
|---|
| 472 | 801 | * @dev: Device that uses the nvmem device. |
|---|
| 473 | 802 | * @nvmem: Pointer to previously registered nvmem device. |
|---|
| 474 | 803 | * |
|---|
| 475 | | - * Return: Will be an negative on error or a zero on success. |
|---|
| 804 | + * Return: Will be negative on error or zero on success. |
|---|
| 476 | 805 | */ |
|---|
| 477 | 806 | int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem) |
|---|
| 478 | 807 | { |
|---|
| .. | .. |
|---|
| 480 | 809 | } |
|---|
| 481 | 810 | EXPORT_SYMBOL(devm_nvmem_unregister); |
|---|
| 482 | 811 | |
|---|
| 483 | | - |
|---|
| 484 | | -static struct nvmem_device *__nvmem_device_get(struct device_node *np, |
|---|
| 485 | | - struct nvmem_cell **cellp, |
|---|
| 486 | | - const char *cell_id) |
|---|
| 812 | +static struct nvmem_device *__nvmem_device_get(void *data, |
|---|
| 813 | + int (*match)(struct device *dev, const void *data)) |
|---|
| 487 | 814 | { |
|---|
| 488 | 815 | struct nvmem_device *nvmem = NULL; |
|---|
| 489 | | - |
|---|
| 490 | | - if (!np) |
|---|
| 491 | | - return ERR_PTR(-ENOENT); |
|---|
| 816 | + struct device *dev; |
|---|
| 492 | 817 | |
|---|
| 493 | 818 | mutex_lock(&nvmem_mutex); |
|---|
| 494 | | - nvmem = of_nvmem_find(np); |
|---|
| 819 | + dev = bus_find_device(&nvmem_bus_type, NULL, data, match); |
|---|
| 820 | + if (dev) |
|---|
| 821 | + nvmem = to_nvmem_device(dev); |
|---|
| 495 | 822 | mutex_unlock(&nvmem_mutex); |
|---|
| 496 | 823 | if (!nvmem) |
|---|
| 497 | 824 | return ERR_PTR(-EPROBE_DEFER); |
|---|
| .. | .. |
|---|
| 499 | 826 | if (!try_module_get(nvmem->owner)) { |
|---|
| 500 | 827 | dev_err(&nvmem->dev, |
|---|
| 501 | 828 | "could not increase module refcount for cell %s\n", |
|---|
| 502 | | - nvmem->name); |
|---|
| 829 | + nvmem_dev_name(nvmem)); |
|---|
| 503 | 830 | |
|---|
| 831 | + put_device(&nvmem->dev); |
|---|
| 504 | 832 | return ERR_PTR(-EINVAL); |
|---|
| 505 | 833 | } |
|---|
| 506 | 834 | |
|---|
| .. | .. |
|---|
| 511 | 839 | |
|---|
| 512 | 840 | static void __nvmem_device_put(struct nvmem_device *nvmem) |
|---|
| 513 | 841 | { |
|---|
| 842 | + put_device(&nvmem->dev); |
|---|
| 514 | 843 | module_put(nvmem->owner); |
|---|
| 515 | 844 | kref_put(&nvmem->refcnt, nvmem_device_release); |
|---|
| 516 | | -} |
|---|
| 517 | | - |
|---|
| 518 | | -static struct nvmem_device *nvmem_find(const char *name) |
|---|
| 519 | | -{ |
|---|
| 520 | | - struct device *d; |
|---|
| 521 | | - |
|---|
| 522 | | - d = bus_find_device_by_name(&nvmem_bus_type, NULL, name); |
|---|
| 523 | | - |
|---|
| 524 | | - if (!d) |
|---|
| 525 | | - return ERR_PTR(-ENOENT); |
|---|
| 526 | | - |
|---|
| 527 | | - return to_nvmem_device(d); |
|---|
| 528 | 845 | } |
|---|
| 529 | 846 | |
|---|
| 530 | 847 | #if IS_ENABLED(CONFIG_OF) |
|---|
| .. | .. |
|---|
| 541 | 858 | { |
|---|
| 542 | 859 | |
|---|
| 543 | 860 | struct device_node *nvmem_np; |
|---|
| 544 | | - int index; |
|---|
| 861 | + struct nvmem_device *nvmem; |
|---|
| 862 | + int index = 0; |
|---|
| 545 | 863 | |
|---|
| 546 | | - index = of_property_match_string(np, "nvmem-names", id); |
|---|
| 864 | + if (id) |
|---|
| 865 | + index = of_property_match_string(np, "nvmem-names", id); |
|---|
| 547 | 866 | |
|---|
| 548 | 867 | nvmem_np = of_parse_phandle(np, "nvmem", index); |
|---|
| 549 | 868 | if (!nvmem_np) |
|---|
| 550 | | - return ERR_PTR(-EINVAL); |
|---|
| 869 | + return ERR_PTR(-ENOENT); |
|---|
| 551 | 870 | |
|---|
| 552 | | - return __nvmem_device_get(nvmem_np, NULL, NULL); |
|---|
| 871 | + nvmem = __nvmem_device_get(nvmem_np, device_match_of_node); |
|---|
| 872 | + of_node_put(nvmem_np); |
|---|
| 873 | + return nvmem; |
|---|
| 553 | 874 | } |
|---|
| 554 | 875 | EXPORT_SYMBOL_GPL(of_nvmem_device_get); |
|---|
| 555 | 876 | #endif |
|---|
| .. | .. |
|---|
| 575 | 896 | |
|---|
| 576 | 897 | } |
|---|
| 577 | 898 | |
|---|
| 578 | | - return nvmem_find(dev_name); |
|---|
| 899 | + return __nvmem_device_get((void *)dev_name, device_match_name); |
|---|
| 579 | 900 | } |
|---|
| 580 | 901 | EXPORT_SYMBOL_GPL(nvmem_device_get); |
|---|
| 902 | + |
|---|
| 903 | +/** |
|---|
| 904 | + * nvmem_device_find() - Find nvmem device with matching function |
|---|
| 905 | + * |
|---|
| 906 | + * @data: Data to pass to match function |
|---|
| 907 | + * @match: Callback function to check device |
|---|
| 908 | + * |
|---|
| 909 | + * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device |
|---|
| 910 | + * on success. |
|---|
| 911 | + */ |
|---|
| 912 | +struct nvmem_device *nvmem_device_find(void *data, |
|---|
| 913 | + int (*match)(struct device *dev, const void *data)) |
|---|
| 914 | +{ |
|---|
| 915 | + return __nvmem_device_get(data, match); |
|---|
| 916 | +} |
|---|
| 917 | +EXPORT_SYMBOL_GPL(nvmem_device_find); |
|---|
| 581 | 918 | |
|---|
| 582 | 919 | static int devm_nvmem_device_match(struct device *dev, void *res, void *data) |
|---|
| 583 | 920 | { |
|---|
| .. | .. |
|---|
| 653 | 990 | } |
|---|
| 654 | 991 | EXPORT_SYMBOL_GPL(devm_nvmem_device_get); |
|---|
| 655 | 992 | |
|---|
| 656 | | -static struct nvmem_cell *nvmem_cell_get_from_list(const char *cell_id) |
|---|
| 993 | +static struct nvmem_cell * |
|---|
| 994 | +nvmem_cell_get_from_lookup(struct device *dev, const char *con_id) |
|---|
| 657 | 995 | { |
|---|
| 658 | | - struct nvmem_cell *cell = NULL; |
|---|
| 996 | + struct nvmem_cell *cell = ERR_PTR(-ENOENT); |
|---|
| 997 | + struct nvmem_cell_lookup *lookup; |
|---|
| 659 | 998 | struct nvmem_device *nvmem; |
|---|
| 999 | + const char *dev_id; |
|---|
| 660 | 1000 | |
|---|
| 661 | | - nvmem = __nvmem_device_get(NULL, &cell, cell_id); |
|---|
| 662 | | - if (IS_ERR(nvmem)) |
|---|
| 663 | | - return ERR_CAST(nvmem); |
|---|
| 1001 | + if (!dev) |
|---|
| 1002 | + return ERR_PTR(-EINVAL); |
|---|
| 664 | 1003 | |
|---|
| 1004 | + dev_id = dev_name(dev); |
|---|
| 1005 | + |
|---|
| 1006 | + mutex_lock(&nvmem_lookup_mutex); |
|---|
| 1007 | + |
|---|
| 1008 | + list_for_each_entry(lookup, &nvmem_lookup_list, node) { |
|---|
| 1009 | + if ((strcmp(lookup->dev_id, dev_id) == 0) && |
|---|
| 1010 | + (strcmp(lookup->con_id, con_id) == 0)) { |
|---|
| 1011 | + /* This is the right entry. */ |
|---|
| 1012 | + nvmem = __nvmem_device_get((void *)lookup->nvmem_name, |
|---|
| 1013 | + device_match_name); |
|---|
| 1014 | + if (IS_ERR(nvmem)) { |
|---|
| 1015 | + /* Provider may not be registered yet. */ |
|---|
| 1016 | + cell = ERR_CAST(nvmem); |
|---|
| 1017 | + break; |
|---|
| 1018 | + } |
|---|
| 1019 | + |
|---|
| 1020 | + cell = nvmem_find_cell_by_name(nvmem, |
|---|
| 1021 | + lookup->cell_name); |
|---|
| 1022 | + if (!cell) { |
|---|
| 1023 | + __nvmem_device_put(nvmem); |
|---|
| 1024 | + cell = ERR_PTR(-ENOENT); |
|---|
| 1025 | + } |
|---|
| 1026 | + break; |
|---|
| 1027 | + } |
|---|
| 1028 | + } |
|---|
| 1029 | + |
|---|
| 1030 | + mutex_unlock(&nvmem_lookup_mutex); |
|---|
| 665 | 1031 | return cell; |
|---|
| 666 | 1032 | } |
|---|
| 667 | 1033 | |
|---|
| .. | .. |
|---|
| 669 | 1035 | static struct nvmem_cell * |
|---|
| 670 | 1036 | nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np) |
|---|
| 671 | 1037 | { |
|---|
| 672 | | - struct nvmem_cell *cell = NULL; |
|---|
| 1038 | + struct nvmem_cell *iter, *cell = NULL; |
|---|
| 673 | 1039 | |
|---|
| 674 | 1040 | mutex_lock(&nvmem_mutex); |
|---|
| 675 | | - list_for_each_entry(cell, &nvmem->cells, node) { |
|---|
| 676 | | - if (np == cell->np) |
|---|
| 1041 | + list_for_each_entry(iter, &nvmem->cells, node) { |
|---|
| 1042 | + if (np == iter->np) { |
|---|
| 1043 | + cell = iter; |
|---|
| 677 | 1044 | break; |
|---|
| 1045 | + } |
|---|
| 678 | 1046 | } |
|---|
| 679 | 1047 | mutex_unlock(&nvmem_mutex); |
|---|
| 680 | 1048 | |
|---|
| .. | .. |
|---|
| 685 | 1053 | * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id |
|---|
| 686 | 1054 | * |
|---|
| 687 | 1055 | * @np: Device tree node that uses the nvmem cell. |
|---|
| 688 | | - * @name: nvmem cell name from nvmem-cell-names property, or NULL |
|---|
| 689 | | - * for the cell at index 0 (the lone cell with no accompanying |
|---|
| 690 | | - * nvmem-cell-names property). |
|---|
| 1056 | + * @id: nvmem cell name from nvmem-cell-names property, or NULL |
|---|
| 1057 | + * for the cell at index 0 (the lone cell with no accompanying |
|---|
| 1058 | + * nvmem-cell-names property). |
|---|
| 691 | 1059 | * |
|---|
| 692 | 1060 | * Return: Will be an ERR_PTR() on error or a valid pointer |
|---|
| 693 | 1061 | * to a struct nvmem_cell. The nvmem_cell will be freed by the |
|---|
| 694 | 1062 | * nvmem_cell_put(). |
|---|
| 695 | 1063 | */ |
|---|
| 696 | | -struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, |
|---|
| 697 | | - const char *name) |
|---|
| 1064 | +struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id) |
|---|
| 698 | 1065 | { |
|---|
| 699 | 1066 | struct device_node *cell_np, *nvmem_np; |
|---|
| 700 | 1067 | struct nvmem_device *nvmem; |
|---|
| .. | .. |
|---|
| 702 | 1069 | int index = 0; |
|---|
| 703 | 1070 | |
|---|
| 704 | 1071 | /* if cell name exists, find index to the name */ |
|---|
| 705 | | - if (name) |
|---|
| 706 | | - index = of_property_match_string(np, "nvmem-cell-names", name); |
|---|
| 1072 | + if (id) |
|---|
| 1073 | + index = of_property_match_string(np, "nvmem-cell-names", id); |
|---|
| 707 | 1074 | |
|---|
| 708 | 1075 | cell_np = of_parse_phandle(np, "nvmem-cells", index); |
|---|
| 709 | 1076 | if (!cell_np) |
|---|
| 710 | | - return ERR_PTR(-EINVAL); |
|---|
| 1077 | + return ERR_PTR(-ENOENT); |
|---|
| 711 | 1078 | |
|---|
| 712 | 1079 | nvmem_np = of_get_next_parent(cell_np); |
|---|
| 713 | 1080 | if (!nvmem_np) |
|---|
| 714 | 1081 | return ERR_PTR(-EINVAL); |
|---|
| 715 | 1082 | |
|---|
| 716 | | - nvmem = __nvmem_device_get(nvmem_np, NULL, NULL); |
|---|
| 1083 | + nvmem = __nvmem_device_get(nvmem_np, device_match_of_node); |
|---|
| 717 | 1084 | of_node_put(nvmem_np); |
|---|
| 718 | 1085 | if (IS_ERR(nvmem)) |
|---|
| 719 | 1086 | return ERR_CAST(nvmem); |
|---|
| .. | .. |
|---|
| 733 | 1100 | * nvmem_cell_get() - Get nvmem cell of device form a given cell name |
|---|
| 734 | 1101 | * |
|---|
| 735 | 1102 | * @dev: Device that requests the nvmem cell. |
|---|
| 736 | | - * @cell_id: nvmem cell name to get. |
|---|
| 1103 | + * @id: nvmem cell name to get (this corresponds with the name from the |
|---|
| 1104 | + * nvmem-cell-names property for DT systems and with the con_id from |
|---|
| 1105 | + * the lookup entry for non-DT systems). |
|---|
| 737 | 1106 | * |
|---|
| 738 | 1107 | * Return: Will be an ERR_PTR() on error or a valid pointer |
|---|
| 739 | 1108 | * to a struct nvmem_cell. The nvmem_cell will be freed by the |
|---|
| 740 | 1109 | * nvmem_cell_put(). |
|---|
| 741 | 1110 | */ |
|---|
| 742 | | -struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *cell_id) |
|---|
| 1111 | +struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id) |
|---|
| 743 | 1112 | { |
|---|
| 744 | 1113 | struct nvmem_cell *cell; |
|---|
| 745 | 1114 | |
|---|
| 746 | 1115 | if (dev->of_node) { /* try dt first */ |
|---|
| 747 | | - cell = of_nvmem_cell_get(dev->of_node, cell_id); |
|---|
| 1116 | + cell = of_nvmem_cell_get(dev->of_node, id); |
|---|
| 748 | 1117 | if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER) |
|---|
| 749 | 1118 | return cell; |
|---|
| 750 | 1119 | } |
|---|
| 751 | 1120 | |
|---|
| 752 | | - /* NULL cell_id only allowed for device tree; invalid otherwise */ |
|---|
| 753 | | - if (!cell_id) |
|---|
| 1121 | + /* NULL cell id only allowed for device tree; invalid otherwise */ |
|---|
| 1122 | + if (!id) |
|---|
| 754 | 1123 | return ERR_PTR(-EINVAL); |
|---|
| 755 | 1124 | |
|---|
| 756 | | - return nvmem_cell_get_from_list(cell_id); |
|---|
| 1125 | + return nvmem_cell_get_from_lookup(dev, id); |
|---|
| 757 | 1126 | } |
|---|
| 758 | 1127 | EXPORT_SYMBOL_GPL(nvmem_cell_get); |
|---|
| 759 | 1128 | |
|---|
| .. | .. |
|---|
| 1010 | 1379 | } |
|---|
| 1011 | 1380 | EXPORT_SYMBOL_GPL(nvmem_cell_write); |
|---|
| 1012 | 1381 | |
|---|
| 1013 | | -/** |
|---|
| 1014 | | - * nvmem_cell_read_u32() - Read a cell value as an u32 |
|---|
| 1015 | | - * |
|---|
| 1016 | | - * @dev: Device that requests the nvmem cell. |
|---|
| 1017 | | - * @cell_id: Name of nvmem cell to read. |
|---|
| 1018 | | - * @val: pointer to output value. |
|---|
| 1019 | | - * |
|---|
| 1020 | | - * Return: 0 on success or negative errno. |
|---|
| 1021 | | - */ |
|---|
| 1022 | | -int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val) |
|---|
| 1382 | +static int nvmem_cell_read_common(struct device *dev, const char *cell_id, |
|---|
| 1383 | + void *val, size_t count) |
|---|
| 1023 | 1384 | { |
|---|
| 1024 | 1385 | struct nvmem_cell *cell; |
|---|
| 1025 | 1386 | void *buf; |
|---|
| .. | .. |
|---|
| 1034 | 1395 | nvmem_cell_put(cell); |
|---|
| 1035 | 1396 | return PTR_ERR(buf); |
|---|
| 1036 | 1397 | } |
|---|
| 1037 | | - if (len != sizeof(*val)) { |
|---|
| 1398 | + if (len != count) { |
|---|
| 1038 | 1399 | kfree(buf); |
|---|
| 1039 | 1400 | nvmem_cell_put(cell); |
|---|
| 1040 | 1401 | return -EINVAL; |
|---|
| 1041 | 1402 | } |
|---|
| 1042 | | - memcpy(val, buf, sizeof(*val)); |
|---|
| 1043 | | - |
|---|
| 1403 | + memcpy(val, buf, count); |
|---|
| 1044 | 1404 | kfree(buf); |
|---|
| 1045 | 1405 | nvmem_cell_put(cell); |
|---|
| 1406 | + |
|---|
| 1046 | 1407 | return 0; |
|---|
| 1047 | 1408 | } |
|---|
| 1409 | + |
|---|
| 1410 | +/** |
|---|
| 1411 | + * nvmem_cell_read_u8() - Read a cell value as a u8 |
|---|
| 1412 | + * |
|---|
| 1413 | + * @dev: Device that requests the nvmem cell. |
|---|
| 1414 | + * @cell_id: Name of nvmem cell to read. |
|---|
| 1415 | + * @val: pointer to output value. |
|---|
| 1416 | + * |
|---|
| 1417 | + * Return: 0 on success or negative errno. |
|---|
| 1418 | + */ |
|---|
| 1419 | +int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val) |
|---|
| 1420 | +{ |
|---|
| 1421 | + return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val)); |
|---|
| 1422 | +} |
|---|
| 1423 | +EXPORT_SYMBOL_GPL(nvmem_cell_read_u8); |
|---|
| 1424 | + |
|---|
| 1425 | +/** |
|---|
| 1426 | + * nvmem_cell_read_u16() - Read a cell value as a u16 |
|---|
| 1427 | + * |
|---|
| 1428 | + * @dev: Device that requests the nvmem cell. |
|---|
| 1429 | + * @cell_id: Name of nvmem cell to read. |
|---|
| 1430 | + * @val: pointer to output value. |
|---|
| 1431 | + * |
|---|
| 1432 | + * Return: 0 on success or negative errno. |
|---|
| 1433 | + */ |
|---|
| 1434 | +int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val) |
|---|
| 1435 | +{ |
|---|
| 1436 | + return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val)); |
|---|
| 1437 | +} |
|---|
| 1438 | +EXPORT_SYMBOL_GPL(nvmem_cell_read_u16); |
|---|
| 1439 | + |
|---|
| 1440 | +/** |
|---|
| 1441 | + * nvmem_cell_read_u32() - Read a cell value as a u32 |
|---|
| 1442 | + * |
|---|
| 1443 | + * @dev: Device that requests the nvmem cell. |
|---|
| 1444 | + * @cell_id: Name of nvmem cell to read. |
|---|
| 1445 | + * @val: pointer to output value. |
|---|
| 1446 | + * |
|---|
| 1447 | + * Return: 0 on success or negative errno. |
|---|
| 1448 | + */ |
|---|
| 1449 | +int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val) |
|---|
| 1450 | +{ |
|---|
| 1451 | + return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val)); |
|---|
| 1452 | +} |
|---|
| 1048 | 1453 | EXPORT_SYMBOL_GPL(nvmem_cell_read_u32); |
|---|
| 1454 | + |
|---|
| 1455 | +/** |
|---|
| 1456 | + * nvmem_cell_read_u64() - Read a cell value as a u64 |
|---|
| 1457 | + * |
|---|
| 1458 | + * @dev: Device that requests the nvmem cell. |
|---|
| 1459 | + * @cell_id: Name of nvmem cell to read. |
|---|
| 1460 | + * @val: pointer to output value. |
|---|
| 1461 | + * |
|---|
| 1462 | + * Return: 0 on success or negative errno. |
|---|
| 1463 | + */ |
|---|
| 1464 | +int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val) |
|---|
| 1465 | +{ |
|---|
| 1466 | + return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val)); |
|---|
| 1467 | +} |
|---|
| 1468 | +EXPORT_SYMBOL_GPL(nvmem_cell_read_u64); |
|---|
| 1049 | 1469 | |
|---|
| 1050 | 1470 | /** |
|---|
| 1051 | 1471 | * nvmem_device_cell_read() - Read a given nvmem device and cell |
|---|
| .. | .. |
|---|
| 1067 | 1487 | if (!nvmem) |
|---|
| 1068 | 1488 | return -EINVAL; |
|---|
| 1069 | 1489 | |
|---|
| 1070 | | - rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); |
|---|
| 1490 | + rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell); |
|---|
| 1071 | 1491 | if (rc) |
|---|
| 1072 | 1492 | return rc; |
|---|
| 1073 | 1493 | |
|---|
| .. | .. |
|---|
| 1087 | 1507 | * @buf: buffer to be written to cell. |
|---|
| 1088 | 1508 | * |
|---|
| 1089 | 1509 | * Return: length of bytes written or negative error code on failure. |
|---|
| 1090 | | - * */ |
|---|
| 1510 | + */ |
|---|
| 1091 | 1511 | int nvmem_device_cell_write(struct nvmem_device *nvmem, |
|---|
| 1092 | 1512 | struct nvmem_cell_info *info, void *buf) |
|---|
| 1093 | 1513 | { |
|---|
| .. | .. |
|---|
| 1097 | 1517 | if (!nvmem) |
|---|
| 1098 | 1518 | return -EINVAL; |
|---|
| 1099 | 1519 | |
|---|
| 1100 | | - rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); |
|---|
| 1520 | + rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell); |
|---|
| 1101 | 1521 | if (rc) |
|---|
| 1102 | 1522 | return rc; |
|---|
| 1103 | 1523 | |
|---|
| .. | .. |
|---|
| 1143 | 1563 | * @buf: buffer to be written. |
|---|
| 1144 | 1564 | * |
|---|
| 1145 | 1565 | * Return: length of bytes written or negative error code on failure. |
|---|
| 1146 | | - * */ |
|---|
| 1566 | + */ |
|---|
| 1147 | 1567 | int nvmem_device_write(struct nvmem_device *nvmem, |
|---|
| 1148 | 1568 | unsigned int offset, |
|---|
| 1149 | 1569 | size_t bytes, void *buf) |
|---|
| .. | .. |
|---|
| 1190 | 1610 | EXPORT_SYMBOL_GPL(nvmem_del_cell_table); |
|---|
| 1191 | 1611 | |
|---|
| 1192 | 1612 | /** |
|---|
| 1613 | + * nvmem_add_cell_lookups() - register a list of cell lookup entries |
|---|
| 1614 | + * |
|---|
| 1615 | + * @entries: array of cell lookup entries |
|---|
| 1616 | + * @nentries: number of cell lookup entries in the array |
|---|
| 1617 | + */ |
|---|
| 1618 | +void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries) |
|---|
| 1619 | +{ |
|---|
| 1620 | + int i; |
|---|
| 1621 | + |
|---|
| 1622 | + mutex_lock(&nvmem_lookup_mutex); |
|---|
| 1623 | + for (i = 0; i < nentries; i++) |
|---|
| 1624 | + list_add_tail(&entries[i].node, &nvmem_lookup_list); |
|---|
| 1625 | + mutex_unlock(&nvmem_lookup_mutex); |
|---|
| 1626 | +} |
|---|
| 1627 | +EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups); |
|---|
| 1628 | + |
|---|
| 1629 | +/** |
|---|
| 1630 | + * nvmem_del_cell_lookups() - remove a list of previously added cell lookup |
|---|
| 1631 | + * entries |
|---|
| 1632 | + * |
|---|
| 1633 | + * @entries: array of cell lookup entries |
|---|
| 1634 | + * @nentries: number of cell lookup entries in the array |
|---|
| 1635 | + */ |
|---|
| 1636 | +void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries) |
|---|
| 1637 | +{ |
|---|
| 1638 | + int i; |
|---|
| 1639 | + |
|---|
| 1640 | + mutex_lock(&nvmem_lookup_mutex); |
|---|
| 1641 | + for (i = 0; i < nentries; i++) |
|---|
| 1642 | + list_del(&entries[i].node); |
|---|
| 1643 | + mutex_unlock(&nvmem_lookup_mutex); |
|---|
| 1644 | +} |
|---|
| 1645 | +EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups); |
|---|
| 1646 | + |
|---|
| 1647 | +/** |
|---|
| 1193 | 1648 | * nvmem_dev_name() - Get the name of a given nvmem device. |
|---|
| 1194 | 1649 | * |
|---|
| 1195 | 1650 | * @nvmem: nvmem device. |
|---|