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