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