Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * nvmem framework core. |
| 3 | * |
| 4 | * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org> |
| 5 | * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 and |
| 9 | * only version 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/export.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/idr.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/nvmem-consumer.h> |
| 24 | #include <linux/nvmem-provider.h> |
| 25 | #include <linux/of.h> |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | |
Swetha Chikkaboraiah | aa9b066 | 2019-04-26 15:21:52 +0530 | [diff] [blame] | 28 | #include "nvmem.h" |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 29 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 30 | struct nvmem_cell { |
| 31 | const char *name; |
| 32 | int offset; |
| 33 | int bytes; |
| 34 | int bit_offset; |
| 35 | int nbits; |
| 36 | struct nvmem_device *nvmem; |
| 37 | struct list_head node; |
| 38 | }; |
| 39 | |
| 40 | static DEFINE_MUTEX(nvmem_mutex); |
| 41 | static DEFINE_IDA(nvmem_ida); |
| 42 | |
| 43 | static LIST_HEAD(nvmem_cells); |
| 44 | static DEFINE_MUTEX(nvmem_cells_mutex); |
| 45 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 46 | static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, |
| 47 | void *val, size_t bytes) |
| 48 | { |
| 49 | if (nvmem->reg_read) |
| 50 | return nvmem->reg_read(nvmem->priv, offset, val, bytes); |
| 51 | |
| 52 | return -EINVAL; |
| 53 | } |
| 54 | |
| 55 | static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, |
| 56 | void *val, size_t bytes) |
| 57 | { |
| 58 | if (nvmem->reg_write) |
| 59 | return nvmem->reg_write(nvmem->priv, offset, val, bytes); |
| 60 | |
| 61 | return -EINVAL; |
| 62 | } |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 63 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 64 | static void nvmem_release(struct device *dev) |
| 65 | { |
| 66 | struct nvmem_device *nvmem = to_nvmem_device(dev); |
| 67 | |
| 68 | ida_simple_remove(&nvmem_ida, nvmem->id); |
| 69 | kfree(nvmem); |
| 70 | } |
| 71 | |
| 72 | static const struct device_type nvmem_provider_type = { |
| 73 | .release = nvmem_release, |
| 74 | }; |
| 75 | |
| 76 | static struct bus_type nvmem_bus_type = { |
| 77 | .name = "nvmem", |
| 78 | }; |
| 79 | |
| 80 | static int of_nvmem_match(struct device *dev, void *nvmem_np) |
| 81 | { |
| 82 | return dev->of_node == nvmem_np; |
| 83 | } |
| 84 | |
| 85 | static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np) |
| 86 | { |
| 87 | struct device *d; |
| 88 | |
| 89 | if (!nvmem_np) |
| 90 | return NULL; |
| 91 | |
| 92 | d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match); |
| 93 | |
| 94 | if (!d) |
| 95 | return NULL; |
| 96 | |
| 97 | return to_nvmem_device(d); |
| 98 | } |
| 99 | |
| 100 | static struct nvmem_cell *nvmem_find_cell(const char *cell_id) |
| 101 | { |
| 102 | struct nvmem_cell *p; |
| 103 | |
| 104 | list_for_each_entry(p, &nvmem_cells, node) |
| 105 | if (p && !strcmp(p->name, cell_id)) |
| 106 | return p; |
| 107 | |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | static void nvmem_cell_drop(struct nvmem_cell *cell) |
| 112 | { |
| 113 | mutex_lock(&nvmem_cells_mutex); |
| 114 | list_del(&cell->node); |
| 115 | mutex_unlock(&nvmem_cells_mutex); |
| 116 | kfree(cell); |
| 117 | } |
| 118 | |
| 119 | static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem) |
| 120 | { |
| 121 | struct nvmem_cell *cell; |
| 122 | struct list_head *p, *n; |
| 123 | |
| 124 | list_for_each_safe(p, n, &nvmem_cells) { |
| 125 | cell = list_entry(p, struct nvmem_cell, node); |
| 126 | if (cell->nvmem == nvmem) |
| 127 | nvmem_cell_drop(cell); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static void nvmem_cell_add(struct nvmem_cell *cell) |
| 132 | { |
| 133 | mutex_lock(&nvmem_cells_mutex); |
| 134 | list_add_tail(&cell->node, &nvmem_cells); |
| 135 | mutex_unlock(&nvmem_cells_mutex); |
| 136 | } |
| 137 | |
| 138 | static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, |
| 139 | const struct nvmem_cell_info *info, |
| 140 | struct nvmem_cell *cell) |
| 141 | { |
| 142 | cell->nvmem = nvmem; |
| 143 | cell->offset = info->offset; |
| 144 | cell->bytes = info->bytes; |
| 145 | cell->name = info->name; |
| 146 | |
| 147 | cell->bit_offset = info->bit_offset; |
| 148 | cell->nbits = info->nbits; |
| 149 | |
| 150 | if (cell->nbits) |
| 151 | cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, |
| 152 | BITS_PER_BYTE); |
| 153 | |
| 154 | if (!IS_ALIGNED(cell->offset, nvmem->stride)) { |
| 155 | dev_err(&nvmem->dev, |
| 156 | "cell %s unaligned to nvmem stride %d\n", |
| 157 | cell->name, nvmem->stride); |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int nvmem_add_cells(struct nvmem_device *nvmem, |
| 165 | const struct nvmem_config *cfg) |
| 166 | { |
| 167 | struct nvmem_cell **cells; |
| 168 | const struct nvmem_cell_info *info = cfg->cells; |
| 169 | int i, rval; |
| 170 | |
| 171 | cells = kcalloc(cfg->ncells, sizeof(*cells), GFP_KERNEL); |
| 172 | if (!cells) |
| 173 | return -ENOMEM; |
| 174 | |
| 175 | for (i = 0; i < cfg->ncells; i++) { |
| 176 | cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL); |
| 177 | if (!cells[i]) { |
| 178 | rval = -ENOMEM; |
| 179 | goto err; |
| 180 | } |
| 181 | |
| 182 | rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]); |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 183 | if (rval) { |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 184 | kfree(cells[i]); |
| 185 | goto err; |
| 186 | } |
| 187 | |
| 188 | nvmem_cell_add(cells[i]); |
| 189 | } |
| 190 | |
| 191 | nvmem->ncells = cfg->ncells; |
| 192 | /* remove tmp array */ |
| 193 | kfree(cells); |
| 194 | |
| 195 | return 0; |
| 196 | err: |
Rasmus Villemoes | dfdf141 | 2016-02-08 22:04:29 +0100 | [diff] [blame] | 197 | while (i--) |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 198 | nvmem_cell_drop(cells[i]); |
| 199 | |
Rasmus Villemoes | dfdf141 | 2016-02-08 22:04:29 +0100 | [diff] [blame] | 200 | kfree(cells); |
| 201 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 202 | return rval; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * nvmem_register() - Register a nvmem device for given nvmem_config. |
| 207 | * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem |
| 208 | * |
| 209 | * @config: nvmem device configuration with which nvmem device is created. |
| 210 | * |
| 211 | * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device |
| 212 | * on success. |
| 213 | */ |
| 214 | |
| 215 | struct nvmem_device *nvmem_register(const struct nvmem_config *config) |
| 216 | { |
| 217 | struct nvmem_device *nvmem; |
| 218 | struct device_node *np; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 219 | int rval; |
| 220 | |
| 221 | if (!config->dev) |
| 222 | return ERR_PTR(-EINVAL); |
| 223 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 224 | nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL); |
| 225 | if (!nvmem) |
| 226 | return ERR_PTR(-ENOMEM); |
| 227 | |
| 228 | rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL); |
| 229 | if (rval < 0) { |
| 230 | kfree(nvmem); |
| 231 | return ERR_PTR(rval); |
| 232 | } |
| 233 | |
| 234 | nvmem->id = rval; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 235 | nvmem->owner = config->owner; |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 236 | nvmem->stride = config->stride; |
| 237 | nvmem->word_size = config->word_size; |
| 238 | nvmem->size = config->size; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 239 | nvmem->dev.type = &nvmem_provider_type; |
| 240 | nvmem->dev.bus = &nvmem_bus_type; |
| 241 | nvmem->dev.parent = config->dev; |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 242 | nvmem->priv = config->priv; |
| 243 | nvmem->reg_read = config->reg_read; |
| 244 | nvmem->reg_write = config->reg_write; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 245 | np = config->dev->of_node; |
| 246 | nvmem->dev.of_node = np; |
| 247 | dev_set_name(&nvmem->dev, "%s%d", |
| 248 | config->name ? : "nvmem", config->id); |
| 249 | |
| 250 | nvmem->read_only = of_property_read_bool(np, "read-only") | |
| 251 | config->read_only; |
| 252 | |
Swetha Chikkaboraiah | aa9b066 | 2019-04-26 15:21:52 +0530 | [diff] [blame] | 253 | nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 254 | device_initialize(&nvmem->dev); |
| 255 | |
| 256 | dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); |
| 257 | |
| 258 | rval = device_add(&nvmem->dev); |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 259 | if (rval) |
Johan Hovold | 7d976da | 2017-06-09 10:59:07 +0100 | [diff] [blame] | 260 | goto err_put_device; |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 261 | |
| 262 | if (config->compat) { |
Swetha Chikkaboraiah | aa9b066 | 2019-04-26 15:21:52 +0530 | [diff] [blame] | 263 | rval = nvmem_sysfs_setup_compat(nvmem, config); |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 264 | if (rval) |
Johan Hovold | 7d976da | 2017-06-09 10:59:07 +0100 | [diff] [blame] | 265 | goto err_device_del; |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | if (config->cells) |
| 269 | nvmem_add_cells(nvmem, config); |
| 270 | |
| 271 | return nvmem; |
Johan Hovold | 7d976da | 2017-06-09 10:59:07 +0100 | [diff] [blame] | 272 | |
| 273 | err_device_del: |
| 274 | device_del(&nvmem->dev); |
| 275 | err_put_device: |
| 276 | put_device(&nvmem->dev); |
| 277 | |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 278 | return ERR_PTR(rval); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 279 | } |
| 280 | EXPORT_SYMBOL_GPL(nvmem_register); |
| 281 | |
| 282 | /** |
| 283 | * nvmem_unregister() - Unregister previously registered nvmem device |
| 284 | * |
| 285 | * @nvmem: Pointer to previously registered nvmem device. |
| 286 | * |
| 287 | * Return: Will be an negative on error or a zero on success. |
| 288 | */ |
| 289 | int nvmem_unregister(struct nvmem_device *nvmem) |
| 290 | { |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 291 | mutex_lock(&nvmem_mutex); |
| 292 | if (nvmem->users) { |
| 293 | mutex_unlock(&nvmem_mutex); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 294 | return -EBUSY; |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 295 | } |
| 296 | mutex_unlock(&nvmem_mutex); |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 297 | |
Andrew Lunn | b6c217a | 2016-02-26 20:59:19 +0100 | [diff] [blame] | 298 | if (nvmem->flags & FLAG_COMPAT) |
| 299 | device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); |
| 300 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 301 | nvmem_device_remove_all_cells(nvmem); |
| 302 | device_del(&nvmem->dev); |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | EXPORT_SYMBOL_GPL(nvmem_unregister); |
| 307 | |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 308 | static struct nvmem_device *__nvmem_device_get(struct device_node *np, |
| 309 | struct nvmem_cell **cellp, |
| 310 | const char *cell_id) |
| 311 | { |
| 312 | struct nvmem_device *nvmem = NULL; |
| 313 | |
| 314 | mutex_lock(&nvmem_mutex); |
| 315 | |
| 316 | if (np) { |
| 317 | nvmem = of_nvmem_find(np); |
| 318 | if (!nvmem) { |
| 319 | mutex_unlock(&nvmem_mutex); |
| 320 | return ERR_PTR(-EPROBE_DEFER); |
| 321 | } |
| 322 | } else { |
| 323 | struct nvmem_cell *cell = nvmem_find_cell(cell_id); |
| 324 | |
| 325 | if (cell) { |
| 326 | nvmem = cell->nvmem; |
| 327 | *cellp = cell; |
| 328 | } |
| 329 | |
| 330 | if (!nvmem) { |
| 331 | mutex_unlock(&nvmem_mutex); |
| 332 | return ERR_PTR(-ENOENT); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | nvmem->users++; |
| 337 | mutex_unlock(&nvmem_mutex); |
| 338 | |
| 339 | if (!try_module_get(nvmem->owner)) { |
| 340 | dev_err(&nvmem->dev, |
| 341 | "could not increase module refcount for cell %s\n", |
| 342 | nvmem->name); |
| 343 | |
| 344 | mutex_lock(&nvmem_mutex); |
| 345 | nvmem->users--; |
| 346 | mutex_unlock(&nvmem_mutex); |
| 347 | |
| 348 | return ERR_PTR(-EINVAL); |
| 349 | } |
| 350 | |
| 351 | return nvmem; |
| 352 | } |
| 353 | |
| 354 | static void __nvmem_device_put(struct nvmem_device *nvmem) |
| 355 | { |
| 356 | module_put(nvmem->owner); |
| 357 | mutex_lock(&nvmem_mutex); |
| 358 | nvmem->users--; |
| 359 | mutex_unlock(&nvmem_mutex); |
| 360 | } |
| 361 | |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 362 | static int nvmem_match(struct device *dev, void *data) |
| 363 | { |
| 364 | return !strcmp(dev_name(dev), data); |
| 365 | } |
| 366 | |
| 367 | static struct nvmem_device *nvmem_find(const char *name) |
| 368 | { |
| 369 | struct device *d; |
| 370 | |
| 371 | d = bus_find_device(&nvmem_bus_type, NULL, (void *)name, nvmem_match); |
| 372 | |
| 373 | if (!d) |
| 374 | return NULL; |
| 375 | |
| 376 | return to_nvmem_device(d); |
| 377 | } |
| 378 | |
| 379 | #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF) |
| 380 | /** |
| 381 | * of_nvmem_device_get() - Get nvmem device from a given id |
| 382 | * |
| 383 | * @dev node: Device tree node that uses the nvmem device |
| 384 | * @id: nvmem name from nvmem-names property. |
| 385 | * |
| 386 | * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device |
| 387 | * on success. |
| 388 | */ |
| 389 | struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id) |
| 390 | { |
| 391 | |
| 392 | struct device_node *nvmem_np; |
| 393 | int index; |
| 394 | |
| 395 | index = of_property_match_string(np, "nvmem-names", id); |
| 396 | |
| 397 | nvmem_np = of_parse_phandle(np, "nvmem", index); |
| 398 | if (!nvmem_np) |
| 399 | return ERR_PTR(-EINVAL); |
| 400 | |
| 401 | return __nvmem_device_get(nvmem_np, NULL, NULL); |
| 402 | } |
| 403 | EXPORT_SYMBOL_GPL(of_nvmem_device_get); |
| 404 | #endif |
| 405 | |
| 406 | /** |
| 407 | * nvmem_device_get() - Get nvmem device from a given id |
| 408 | * |
| 409 | * @dev : Device that uses the nvmem device |
| 410 | * @id: nvmem name from nvmem-names property. |
| 411 | * |
| 412 | * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device |
| 413 | * on success. |
| 414 | */ |
| 415 | struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name) |
| 416 | { |
| 417 | if (dev->of_node) { /* try dt first */ |
| 418 | struct nvmem_device *nvmem; |
| 419 | |
| 420 | nvmem = of_nvmem_device_get(dev->of_node, dev_name); |
| 421 | |
| 422 | if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER) |
| 423 | return nvmem; |
| 424 | |
| 425 | } |
| 426 | |
| 427 | return nvmem_find(dev_name); |
| 428 | } |
| 429 | EXPORT_SYMBOL_GPL(nvmem_device_get); |
| 430 | |
| 431 | static int devm_nvmem_device_match(struct device *dev, void *res, void *data) |
| 432 | { |
| 433 | struct nvmem_device **nvmem = res; |
| 434 | |
| 435 | if (WARN_ON(!nvmem || !*nvmem)) |
| 436 | return 0; |
| 437 | |
| 438 | return *nvmem == data; |
| 439 | } |
| 440 | |
| 441 | static void devm_nvmem_device_release(struct device *dev, void *res) |
| 442 | { |
| 443 | nvmem_device_put(*(struct nvmem_device **)res); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * devm_nvmem_device_put() - put alredy got nvmem device |
| 448 | * |
| 449 | * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(), |
| 450 | * that needs to be released. |
| 451 | */ |
| 452 | void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem) |
| 453 | { |
| 454 | int ret; |
| 455 | |
| 456 | ret = devres_release(dev, devm_nvmem_device_release, |
| 457 | devm_nvmem_device_match, nvmem); |
| 458 | |
| 459 | WARN_ON(ret); |
| 460 | } |
| 461 | EXPORT_SYMBOL_GPL(devm_nvmem_device_put); |
| 462 | |
| 463 | /** |
| 464 | * nvmem_device_put() - put alredy got nvmem device |
| 465 | * |
| 466 | * @nvmem: pointer to nvmem device that needs to be released. |
| 467 | */ |
| 468 | void nvmem_device_put(struct nvmem_device *nvmem) |
| 469 | { |
| 470 | __nvmem_device_put(nvmem); |
| 471 | } |
| 472 | EXPORT_SYMBOL_GPL(nvmem_device_put); |
| 473 | |
| 474 | /** |
| 475 | * devm_nvmem_device_get() - Get nvmem cell of device form a given id |
| 476 | * |
| 477 | * @dev node: Device tree node that uses the nvmem cell |
| 478 | * @id: nvmem name in nvmems property. |
| 479 | * |
| 480 | * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell |
| 481 | * on success. The nvmem_cell will be freed by the automatically once the |
| 482 | * device is freed. |
| 483 | */ |
| 484 | struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id) |
| 485 | { |
| 486 | struct nvmem_device **ptr, *nvmem; |
| 487 | |
| 488 | ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL); |
| 489 | if (!ptr) |
| 490 | return ERR_PTR(-ENOMEM); |
| 491 | |
| 492 | nvmem = nvmem_device_get(dev, id); |
| 493 | if (!IS_ERR(nvmem)) { |
| 494 | *ptr = nvmem; |
| 495 | devres_add(dev, ptr); |
| 496 | } else { |
| 497 | devres_free(ptr); |
| 498 | } |
| 499 | |
| 500 | return nvmem; |
| 501 | } |
| 502 | EXPORT_SYMBOL_GPL(devm_nvmem_device_get); |
| 503 | |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 504 | static struct nvmem_cell *nvmem_cell_get_from_list(const char *cell_id) |
| 505 | { |
| 506 | struct nvmem_cell *cell = NULL; |
| 507 | struct nvmem_device *nvmem; |
| 508 | |
| 509 | nvmem = __nvmem_device_get(NULL, &cell, cell_id); |
| 510 | if (IS_ERR(nvmem)) |
| 511 | return ERR_CAST(nvmem); |
| 512 | |
| 513 | return cell; |
| 514 | } |
| 515 | |
| 516 | #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF) |
| 517 | /** |
| 518 | * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id |
| 519 | * |
| 520 | * @dev node: Device tree node that uses the nvmem cell |
| 521 | * @id: nvmem cell name from nvmem-cell-names property. |
| 522 | * |
| 523 | * Return: Will be an ERR_PTR() on error or a valid pointer |
| 524 | * to a struct nvmem_cell. The nvmem_cell will be freed by the |
| 525 | * nvmem_cell_put(). |
| 526 | */ |
| 527 | struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, |
| 528 | const char *name) |
| 529 | { |
| 530 | struct device_node *cell_np, *nvmem_np; |
| 531 | struct nvmem_cell *cell; |
| 532 | struct nvmem_device *nvmem; |
| 533 | const __be32 *addr; |
| 534 | int rval, len, index; |
| 535 | |
| 536 | index = of_property_match_string(np, "nvmem-cell-names", name); |
| 537 | |
| 538 | cell_np = of_parse_phandle(np, "nvmem-cells", index); |
| 539 | if (!cell_np) |
| 540 | return ERR_PTR(-EINVAL); |
| 541 | |
| 542 | nvmem_np = of_get_next_parent(cell_np); |
| 543 | if (!nvmem_np) |
| 544 | return ERR_PTR(-EINVAL); |
| 545 | |
| 546 | nvmem = __nvmem_device_get(nvmem_np, NULL, NULL); |
| 547 | if (IS_ERR(nvmem)) |
| 548 | return ERR_CAST(nvmem); |
| 549 | |
| 550 | addr = of_get_property(cell_np, "reg", &len); |
| 551 | if (!addr || (len < 2 * sizeof(u32))) { |
| 552 | dev_err(&nvmem->dev, "nvmem: invalid reg on %s\n", |
| 553 | cell_np->full_name); |
| 554 | rval = -EINVAL; |
| 555 | goto err_mem; |
| 556 | } |
| 557 | |
| 558 | cell = kzalloc(sizeof(*cell), GFP_KERNEL); |
| 559 | if (!cell) { |
| 560 | rval = -ENOMEM; |
| 561 | goto err_mem; |
| 562 | } |
| 563 | |
| 564 | cell->nvmem = nvmem; |
| 565 | cell->offset = be32_to_cpup(addr++); |
| 566 | cell->bytes = be32_to_cpup(addr); |
| 567 | cell->name = cell_np->name; |
| 568 | |
| 569 | addr = of_get_property(cell_np, "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(cell->nbits + cell->bit_offset, |
| 577 | BITS_PER_BYTE); |
| 578 | |
| 579 | if (!IS_ALIGNED(cell->offset, nvmem->stride)) { |
| 580 | dev_err(&nvmem->dev, |
| 581 | "cell %s unaligned to nvmem stride %d\n", |
| 582 | cell->name, nvmem->stride); |
| 583 | rval = -EINVAL; |
| 584 | goto err_sanity; |
| 585 | } |
| 586 | |
| 587 | nvmem_cell_add(cell); |
| 588 | |
| 589 | return cell; |
| 590 | |
| 591 | err_sanity: |
| 592 | kfree(cell); |
| 593 | |
| 594 | err_mem: |
| 595 | __nvmem_device_put(nvmem); |
| 596 | |
| 597 | return ERR_PTR(rval); |
| 598 | } |
| 599 | EXPORT_SYMBOL_GPL(of_nvmem_cell_get); |
| 600 | #endif |
| 601 | |
| 602 | /** |
| 603 | * nvmem_cell_get() - Get nvmem cell of device form a given cell name |
| 604 | * |
| 605 | * @dev node: Device tree node that uses the nvmem cell |
| 606 | * @id: nvmem cell name to get. |
| 607 | * |
| 608 | * Return: Will be an ERR_PTR() on error or a valid pointer |
| 609 | * to a struct nvmem_cell. The nvmem_cell will be freed by the |
| 610 | * nvmem_cell_put(). |
| 611 | */ |
| 612 | struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *cell_id) |
| 613 | { |
| 614 | struct nvmem_cell *cell; |
| 615 | |
| 616 | if (dev->of_node) { /* try dt first */ |
| 617 | cell = of_nvmem_cell_get(dev->of_node, cell_id); |
| 618 | if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER) |
| 619 | return cell; |
| 620 | } |
| 621 | |
| 622 | return nvmem_cell_get_from_list(cell_id); |
| 623 | } |
| 624 | EXPORT_SYMBOL_GPL(nvmem_cell_get); |
| 625 | |
| 626 | static void devm_nvmem_cell_release(struct device *dev, void *res) |
| 627 | { |
| 628 | nvmem_cell_put(*(struct nvmem_cell **)res); |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * devm_nvmem_cell_get() - Get nvmem cell of device form a given id |
| 633 | * |
| 634 | * @dev node: Device tree node that uses the nvmem cell |
| 635 | * @id: nvmem id in nvmem-names property. |
| 636 | * |
| 637 | * Return: Will be an ERR_PTR() on error or a valid pointer |
| 638 | * to a struct nvmem_cell. The nvmem_cell will be freed by the |
| 639 | * automatically once the device is freed. |
| 640 | */ |
| 641 | struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id) |
| 642 | { |
| 643 | struct nvmem_cell **ptr, *cell; |
| 644 | |
| 645 | ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL); |
| 646 | if (!ptr) |
| 647 | return ERR_PTR(-ENOMEM); |
| 648 | |
| 649 | cell = nvmem_cell_get(dev, id); |
| 650 | if (!IS_ERR(cell)) { |
| 651 | *ptr = cell; |
| 652 | devres_add(dev, ptr); |
| 653 | } else { |
| 654 | devres_free(ptr); |
| 655 | } |
| 656 | |
| 657 | return cell; |
| 658 | } |
| 659 | EXPORT_SYMBOL_GPL(devm_nvmem_cell_get); |
| 660 | |
| 661 | static int devm_nvmem_cell_match(struct device *dev, void *res, void *data) |
| 662 | { |
| 663 | struct nvmem_cell **c = res; |
| 664 | |
| 665 | if (WARN_ON(!c || !*c)) |
| 666 | return 0; |
| 667 | |
| 668 | return *c == data; |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * devm_nvmem_cell_put() - Release previously allocated nvmem cell |
| 673 | * from devm_nvmem_cell_get. |
| 674 | * |
| 675 | * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get() |
| 676 | */ |
| 677 | void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell) |
| 678 | { |
| 679 | int ret; |
| 680 | |
| 681 | ret = devres_release(dev, devm_nvmem_cell_release, |
| 682 | devm_nvmem_cell_match, cell); |
| 683 | |
| 684 | WARN_ON(ret); |
| 685 | } |
| 686 | EXPORT_SYMBOL(devm_nvmem_cell_put); |
| 687 | |
| 688 | /** |
| 689 | * nvmem_cell_put() - Release previously allocated nvmem cell. |
| 690 | * |
| 691 | * @cell: Previously allocated nvmem cell by nvmem_cell_get() |
| 692 | */ |
| 693 | void nvmem_cell_put(struct nvmem_cell *cell) |
| 694 | { |
| 695 | struct nvmem_device *nvmem = cell->nvmem; |
| 696 | |
| 697 | __nvmem_device_put(nvmem); |
| 698 | nvmem_cell_drop(cell); |
| 699 | } |
| 700 | EXPORT_SYMBOL_GPL(nvmem_cell_put); |
| 701 | |
| 702 | static inline void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, |
| 703 | void *buf) |
| 704 | { |
| 705 | u8 *p, *b; |
| 706 | int i, bit_offset = cell->bit_offset; |
| 707 | |
| 708 | p = b = buf; |
| 709 | if (bit_offset) { |
| 710 | /* First shift */ |
| 711 | *b++ >>= bit_offset; |
| 712 | |
| 713 | /* setup rest of the bytes if any */ |
| 714 | for (i = 1; i < cell->bytes; i++) { |
| 715 | /* Get bits from next byte and shift them towards msb */ |
| 716 | *p |= *b << (BITS_PER_BYTE - bit_offset); |
| 717 | |
| 718 | p = b; |
| 719 | *b++ >>= bit_offset; |
| 720 | } |
| 721 | |
| 722 | /* result fits in less bytes */ |
| 723 | if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE)) |
| 724 | *p-- = 0; |
| 725 | } |
| 726 | /* clear msb bits if any leftover in the last byte */ |
| 727 | *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0); |
| 728 | } |
| 729 | |
| 730 | static int __nvmem_cell_read(struct nvmem_device *nvmem, |
| 731 | struct nvmem_cell *cell, |
| 732 | void *buf, size_t *len) |
| 733 | { |
| 734 | int rc; |
| 735 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 736 | rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 737 | |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 738 | if (rc) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 739 | return rc; |
| 740 | |
| 741 | /* shift bits in-place */ |
Axel Lin | cbf854a | 2015-09-30 13:35:15 +0100 | [diff] [blame] | 742 | if (cell->bit_offset || cell->nbits) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 743 | nvmem_shift_read_buffer_in_place(cell, buf); |
| 744 | |
| 745 | *len = cell->bytes; |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * nvmem_cell_read() - Read a given nvmem cell |
| 752 | * |
| 753 | * @cell: nvmem cell to be read. |
| 754 | * @len: pointer to length of cell which will be populated on successful read. |
| 755 | * |
| 756 | * Return: ERR_PTR() on error or a valid pointer to a char * buffer on success. |
| 757 | * The buffer should be freed by the consumer with a kfree(). |
| 758 | */ |
| 759 | void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len) |
| 760 | { |
| 761 | struct nvmem_device *nvmem = cell->nvmem; |
| 762 | u8 *buf; |
| 763 | int rc; |
| 764 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 765 | if (!nvmem) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 766 | return ERR_PTR(-EINVAL); |
| 767 | |
| 768 | buf = kzalloc(cell->bytes, GFP_KERNEL); |
| 769 | if (!buf) |
| 770 | return ERR_PTR(-ENOMEM); |
| 771 | |
| 772 | rc = __nvmem_cell_read(nvmem, cell, buf, len); |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 773 | if (rc) { |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 774 | kfree(buf); |
| 775 | return ERR_PTR(rc); |
| 776 | } |
| 777 | |
| 778 | return buf; |
| 779 | } |
| 780 | EXPORT_SYMBOL_GPL(nvmem_cell_read); |
| 781 | |
| 782 | static inline void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell, |
| 783 | u8 *_buf, int len) |
| 784 | { |
| 785 | struct nvmem_device *nvmem = cell->nvmem; |
| 786 | int i, rc, nbits, bit_offset = cell->bit_offset; |
| 787 | u8 v, *p, *buf, *b, pbyte, pbits; |
| 788 | |
| 789 | nbits = cell->nbits; |
| 790 | buf = kzalloc(cell->bytes, GFP_KERNEL); |
| 791 | if (!buf) |
| 792 | return ERR_PTR(-ENOMEM); |
| 793 | |
| 794 | memcpy(buf, _buf, len); |
| 795 | p = b = buf; |
| 796 | |
| 797 | if (bit_offset) { |
| 798 | pbyte = *b; |
| 799 | *b <<= bit_offset; |
| 800 | |
| 801 | /* setup the first byte with lsb bits from nvmem */ |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 802 | rc = nvmem_reg_read(nvmem, cell->offset, &v, 1); |
Mathieu Malaterre | 30ac755 | 2018-05-11 12:07:03 +0100 | [diff] [blame] | 803 | if (rc) |
| 804 | goto err; |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 805 | *b++ |= GENMASK(bit_offset - 1, 0) & v; |
| 806 | |
| 807 | /* setup rest of the byte if any */ |
| 808 | for (i = 1; i < cell->bytes; i++) { |
| 809 | /* Get last byte bits and shift them towards lsb */ |
| 810 | pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset); |
| 811 | pbyte = *b; |
| 812 | p = b; |
| 813 | *b <<= bit_offset; |
| 814 | *b++ |= pbits; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | /* if it's not end on byte boundary */ |
| 819 | if ((nbits + bit_offset) % BITS_PER_BYTE) { |
| 820 | /* setup the last byte with msb bits from nvmem */ |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 821 | rc = nvmem_reg_read(nvmem, |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 822 | cell->offset + cell->bytes - 1, &v, 1); |
Mathieu Malaterre | 30ac755 | 2018-05-11 12:07:03 +0100 | [diff] [blame] | 823 | if (rc) |
| 824 | goto err; |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 825 | *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v; |
| 826 | |
| 827 | } |
| 828 | |
| 829 | return buf; |
Mathieu Malaterre | 30ac755 | 2018-05-11 12:07:03 +0100 | [diff] [blame] | 830 | err: |
| 831 | kfree(buf); |
| 832 | return ERR_PTR(rc); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /** |
| 836 | * nvmem_cell_write() - Write to a given nvmem cell |
| 837 | * |
| 838 | * @cell: nvmem cell to be written. |
| 839 | * @buf: Buffer to be written. |
| 840 | * @len: length of buffer to be written to nvmem cell. |
| 841 | * |
| 842 | * Return: length of bytes written or negative on failure. |
| 843 | */ |
| 844 | int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len) |
| 845 | { |
| 846 | struct nvmem_device *nvmem = cell->nvmem; |
| 847 | int rc; |
| 848 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 849 | if (!nvmem || nvmem->read_only || |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 850 | (cell->bit_offset == 0 && len != cell->bytes)) |
| 851 | return -EINVAL; |
| 852 | |
| 853 | if (cell->bit_offset || cell->nbits) { |
| 854 | buf = nvmem_cell_prepare_write_buffer(cell, buf, len); |
| 855 | if (IS_ERR(buf)) |
| 856 | return PTR_ERR(buf); |
| 857 | } |
| 858 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 859 | rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes); |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 860 | |
| 861 | /* free the tmp buffer */ |
Axel Lin | ace2217 | 2015-09-30 13:36:10 +0100 | [diff] [blame] | 862 | if (cell->bit_offset || cell->nbits) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 863 | kfree(buf); |
| 864 | |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 865 | if (rc) |
Srinivas Kandagatla | 69aba79 | 2015-07-27 12:13:34 +0100 | [diff] [blame] | 866 | return rc; |
| 867 | |
| 868 | return len; |
| 869 | } |
| 870 | EXPORT_SYMBOL_GPL(nvmem_cell_write); |
| 871 | |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 872 | /** |
| 873 | * nvmem_device_cell_read() - Read a given nvmem device and cell |
| 874 | * |
| 875 | * @nvmem: nvmem device to read from. |
| 876 | * @info: nvmem cell info to be read. |
| 877 | * @buf: buffer pointer which will be populated on successful read. |
| 878 | * |
| 879 | * Return: length of successful bytes read on success and negative |
| 880 | * error code on error. |
| 881 | */ |
| 882 | ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem, |
| 883 | struct nvmem_cell_info *info, void *buf) |
| 884 | { |
| 885 | struct nvmem_cell cell; |
| 886 | int rc; |
| 887 | ssize_t len; |
| 888 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 889 | if (!nvmem) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 890 | return -EINVAL; |
| 891 | |
| 892 | rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 893 | if (rc) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 894 | return rc; |
| 895 | |
| 896 | rc = __nvmem_cell_read(nvmem, &cell, buf, &len); |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 897 | if (rc) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 898 | return rc; |
| 899 | |
| 900 | return len; |
| 901 | } |
| 902 | EXPORT_SYMBOL_GPL(nvmem_device_cell_read); |
| 903 | |
| 904 | /** |
| 905 | * nvmem_device_cell_write() - Write cell to a given nvmem device |
| 906 | * |
| 907 | * @nvmem: nvmem device to be written to. |
| 908 | * @info: nvmem cell info to be written |
| 909 | * @buf: buffer to be written to cell. |
| 910 | * |
| 911 | * Return: length of bytes written or negative error code on failure. |
| 912 | * */ |
| 913 | int nvmem_device_cell_write(struct nvmem_device *nvmem, |
| 914 | struct nvmem_cell_info *info, void *buf) |
| 915 | { |
| 916 | struct nvmem_cell cell; |
| 917 | int rc; |
| 918 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 919 | if (!nvmem) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 920 | return -EINVAL; |
| 921 | |
| 922 | rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 923 | if (rc) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 924 | return rc; |
| 925 | |
| 926 | return nvmem_cell_write(&cell, buf, cell.bytes); |
| 927 | } |
| 928 | EXPORT_SYMBOL_GPL(nvmem_device_cell_write); |
| 929 | |
| 930 | /** |
| 931 | * nvmem_device_read() - Read from a given nvmem device |
| 932 | * |
| 933 | * @nvmem: nvmem device to read from. |
| 934 | * @offset: offset in nvmem device. |
| 935 | * @bytes: number of bytes to read. |
| 936 | * @buf: buffer pointer which will be populated on successful read. |
| 937 | * |
| 938 | * Return: length of successful bytes read on success and negative |
| 939 | * error code on error. |
| 940 | */ |
| 941 | int nvmem_device_read(struct nvmem_device *nvmem, |
| 942 | unsigned int offset, |
| 943 | size_t bytes, void *buf) |
| 944 | { |
| 945 | int rc; |
| 946 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 947 | if (!nvmem) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 948 | return -EINVAL; |
| 949 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 950 | rc = nvmem_reg_read(nvmem, offset, buf, bytes); |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 951 | |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 952 | if (rc) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 953 | return rc; |
| 954 | |
| 955 | return bytes; |
| 956 | } |
| 957 | EXPORT_SYMBOL_GPL(nvmem_device_read); |
| 958 | |
| 959 | /** |
| 960 | * nvmem_device_write() - Write cell to a given nvmem device |
| 961 | * |
| 962 | * @nvmem: nvmem device to be written to. |
| 963 | * @offset: offset in nvmem device. |
| 964 | * @bytes: number of bytes to write. |
| 965 | * @buf: buffer to be written. |
| 966 | * |
| 967 | * Return: length of bytes written or negative error code on failure. |
| 968 | * */ |
| 969 | int nvmem_device_write(struct nvmem_device *nvmem, |
| 970 | unsigned int offset, |
| 971 | size_t bytes, void *buf) |
| 972 | { |
| 973 | int rc; |
| 974 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 975 | if (!nvmem) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 976 | return -EINVAL; |
| 977 | |
Srinivas Kandagatla | 795ddd1 | 2016-04-24 20:28:05 +0100 | [diff] [blame] | 978 | rc = nvmem_reg_write(nvmem, offset, buf, bytes); |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 979 | |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 980 | if (rc) |
Srinivas Kandagatla | e2a5402 | 2015-07-27 12:13:45 +0100 | [diff] [blame] | 981 | return rc; |
| 982 | |
| 983 | |
| 984 | return bytes; |
| 985 | } |
| 986 | EXPORT_SYMBOL_GPL(nvmem_device_write); |
| 987 | |
Srinivas Kandagatla | eace75c | 2015-07-27 12:13:19 +0100 | [diff] [blame] | 988 | static int __init nvmem_init(void) |
| 989 | { |
| 990 | return bus_register(&nvmem_bus_type); |
| 991 | } |
| 992 | |
| 993 | static void __exit nvmem_exit(void) |
| 994 | { |
| 995 | bus_unregister(&nvmem_bus_type); |
| 996 | } |
| 997 | |
| 998 | subsys_initcall(nvmem_init); |
| 999 | module_exit(nvmem_exit); |
| 1000 | |
| 1001 | MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org"); |
| 1002 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com"); |
| 1003 | MODULE_DESCRIPTION("nvmem Driver Core"); |
| 1004 | MODULE_LICENSE("GPL v2"); |