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