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