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