nvmem: core: provide own priv pointer in post process callback
[platform/kernel/linux-starfive.git] / drivers / nvmem / core.c
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/of_device.h>
21 #include <linux/slab.h>
22
23 struct nvmem_device {
24         struct module           *owner;
25         struct device           dev;
26         int                     stride;
27         int                     word_size;
28         int                     id;
29         struct kref             refcnt;
30         size_t                  size;
31         bool                    read_only;
32         bool                    root_only;
33         int                     flags;
34         enum nvmem_type         type;
35         struct bin_attribute    eeprom;
36         struct device           *base_dev;
37         struct list_head        cells;
38         const struct nvmem_keepout *keepout;
39         unsigned int            nkeepout;
40         nvmem_reg_read_t        reg_read;
41         nvmem_reg_write_t       reg_write;
42         struct gpio_desc        *wp_gpio;
43         struct nvmem_layout     *layout;
44         void *priv;
45 };
46
47 #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
48
49 #define FLAG_COMPAT             BIT(0)
50 struct nvmem_cell_entry {
51         const char              *name;
52         int                     offset;
53         int                     bytes;
54         int                     bit_offset;
55         int                     nbits;
56         nvmem_cell_post_process_t read_post_process;
57         void                    *priv;
58         struct device_node      *np;
59         struct nvmem_device     *nvmem;
60         struct list_head        node;
61 };
62
63 struct nvmem_cell {
64         struct nvmem_cell_entry *entry;
65         const char              *id;
66         int                     index;
67 };
68
69 static DEFINE_MUTEX(nvmem_mutex);
70 static DEFINE_IDA(nvmem_ida);
71
72 static DEFINE_MUTEX(nvmem_cell_mutex);
73 static LIST_HEAD(nvmem_cell_tables);
74
75 static DEFINE_MUTEX(nvmem_lookup_mutex);
76 static LIST_HEAD(nvmem_lookup_list);
77
78 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
79
80 static DEFINE_SPINLOCK(nvmem_layout_lock);
81 static LIST_HEAD(nvmem_layouts);
82
83 static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
84                             void *val, size_t bytes)
85 {
86         if (nvmem->reg_read)
87                 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
88
89         return -EINVAL;
90 }
91
92 static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
93                              void *val, size_t bytes)
94 {
95         int ret;
96
97         if (nvmem->reg_write) {
98                 gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
99                 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
100                 gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
101                 return ret;
102         }
103
104         return -EINVAL;
105 }
106
107 static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
108                                       unsigned int offset, void *val,
109                                       size_t bytes, int write)
110 {
111
112         unsigned int end = offset + bytes;
113         unsigned int kend, ksize;
114         const struct nvmem_keepout *keepout = nvmem->keepout;
115         const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
116         int rc;
117
118         /*
119          * Skip all keepouts before the range being accessed.
120          * Keepouts are sorted.
121          */
122         while ((keepout < keepoutend) && (keepout->end <= offset))
123                 keepout++;
124
125         while ((offset < end) && (keepout < keepoutend)) {
126                 /* Access the valid portion before the keepout. */
127                 if (offset < keepout->start) {
128                         kend = min(end, keepout->start);
129                         ksize = kend - offset;
130                         if (write)
131                                 rc = __nvmem_reg_write(nvmem, offset, val, ksize);
132                         else
133                                 rc = __nvmem_reg_read(nvmem, offset, val, ksize);
134
135                         if (rc)
136                                 return rc;
137
138                         offset += ksize;
139                         val += ksize;
140                 }
141
142                 /*
143                  * Now we're aligned to the start of this keepout zone. Go
144                  * through it.
145                  */
146                 kend = min(end, keepout->end);
147                 ksize = kend - offset;
148                 if (!write)
149                         memset(val, keepout->value, ksize);
150
151                 val += ksize;
152                 offset += ksize;
153                 keepout++;
154         }
155
156         /*
157          * If we ran out of keepouts but there's still stuff to do, send it
158          * down directly
159          */
160         if (offset < end) {
161                 ksize = end - offset;
162                 if (write)
163                         return __nvmem_reg_write(nvmem, offset, val, ksize);
164                 else
165                         return __nvmem_reg_read(nvmem, offset, val, ksize);
166         }
167
168         return 0;
169 }
170
171 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
172                           void *val, size_t bytes)
173 {
174         if (!nvmem->nkeepout)
175                 return __nvmem_reg_read(nvmem, offset, val, bytes);
176
177         return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false);
178 }
179
180 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
181                            void *val, size_t bytes)
182 {
183         if (!nvmem->nkeepout)
184                 return __nvmem_reg_write(nvmem, offset, val, bytes);
185
186         return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true);
187 }
188
189 #ifdef CONFIG_NVMEM_SYSFS
190 static const char * const nvmem_type_str[] = {
191         [NVMEM_TYPE_UNKNOWN] = "Unknown",
192         [NVMEM_TYPE_EEPROM] = "EEPROM",
193         [NVMEM_TYPE_OTP] = "OTP",
194         [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
195         [NVMEM_TYPE_FRAM] = "FRAM",
196 };
197
198 #ifdef CONFIG_DEBUG_LOCK_ALLOC
199 static struct lock_class_key eeprom_lock_key;
200 #endif
201
202 static ssize_t type_show(struct device *dev,
203                          struct device_attribute *attr, char *buf)
204 {
205         struct nvmem_device *nvmem = to_nvmem_device(dev);
206
207         return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
208 }
209
210 static DEVICE_ATTR_RO(type);
211
212 static struct attribute *nvmem_attrs[] = {
213         &dev_attr_type.attr,
214         NULL,
215 };
216
217 static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
218                                    struct bin_attribute *attr, char *buf,
219                                    loff_t pos, size_t count)
220 {
221         struct device *dev;
222         struct nvmem_device *nvmem;
223         int rc;
224
225         if (attr->private)
226                 dev = attr->private;
227         else
228                 dev = kobj_to_dev(kobj);
229         nvmem = to_nvmem_device(dev);
230
231         /* Stop the user from reading */
232         if (pos >= nvmem->size)
233                 return 0;
234
235         if (!IS_ALIGNED(pos, nvmem->stride))
236                 return -EINVAL;
237
238         if (count < nvmem->word_size)
239                 return -EINVAL;
240
241         if (pos + count > nvmem->size)
242                 count = nvmem->size - pos;
243
244         count = round_down(count, nvmem->word_size);
245
246         if (!nvmem->reg_read)
247                 return -EPERM;
248
249         rc = nvmem_reg_read(nvmem, pos, buf, count);
250
251         if (rc)
252                 return rc;
253
254         return count;
255 }
256
257 static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
258                                     struct bin_attribute *attr, char *buf,
259                                     loff_t pos, size_t count)
260 {
261         struct device *dev;
262         struct nvmem_device *nvmem;
263         int rc;
264
265         if (attr->private)
266                 dev = attr->private;
267         else
268                 dev = kobj_to_dev(kobj);
269         nvmem = to_nvmem_device(dev);
270
271         /* Stop the user from writing */
272         if (pos >= nvmem->size)
273                 return -EFBIG;
274
275         if (!IS_ALIGNED(pos, nvmem->stride))
276                 return -EINVAL;
277
278         if (count < nvmem->word_size)
279                 return -EINVAL;
280
281         if (pos + count > nvmem->size)
282                 count = nvmem->size - pos;
283
284         count = round_down(count, nvmem->word_size);
285
286         if (!nvmem->reg_write)
287                 return -EPERM;
288
289         rc = nvmem_reg_write(nvmem, pos, buf, count);
290
291         if (rc)
292                 return rc;
293
294         return count;
295 }
296
297 static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
298 {
299         umode_t mode = 0400;
300
301         if (!nvmem->root_only)
302                 mode |= 0044;
303
304         if (!nvmem->read_only)
305                 mode |= 0200;
306
307         if (!nvmem->reg_write)
308                 mode &= ~0200;
309
310         if (!nvmem->reg_read)
311                 mode &= ~0444;
312
313         return mode;
314 }
315
316 static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
317                                          struct bin_attribute *attr, int i)
318 {
319         struct device *dev = kobj_to_dev(kobj);
320         struct nvmem_device *nvmem = to_nvmem_device(dev);
321
322         attr->size = nvmem->size;
323
324         return nvmem_bin_attr_get_umode(nvmem);
325 }
326
327 /* default read/write permissions */
328 static struct bin_attribute bin_attr_rw_nvmem = {
329         .attr   = {
330                 .name   = "nvmem",
331                 .mode   = 0644,
332         },
333         .read   = bin_attr_nvmem_read,
334         .write  = bin_attr_nvmem_write,
335 };
336
337 static struct bin_attribute *nvmem_bin_attributes[] = {
338         &bin_attr_rw_nvmem,
339         NULL,
340 };
341
342 static const struct attribute_group nvmem_bin_group = {
343         .bin_attrs      = nvmem_bin_attributes,
344         .attrs          = nvmem_attrs,
345         .is_bin_visible = nvmem_bin_attr_is_visible,
346 };
347
348 static const struct attribute_group *nvmem_dev_groups[] = {
349         &nvmem_bin_group,
350         NULL,
351 };
352
353 static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
354         .attr   = {
355                 .name   = "eeprom",
356         },
357         .read   = bin_attr_nvmem_read,
358         .write  = bin_attr_nvmem_write,
359 };
360
361 /*
362  * nvmem_setup_compat() - Create an additional binary entry in
363  * drivers sys directory, to be backwards compatible with the older
364  * drivers/misc/eeprom drivers.
365  */
366 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
367                                     const struct nvmem_config *config)
368 {
369         int rval;
370
371         if (!config->compat)
372                 return 0;
373
374         if (!config->base_dev)
375                 return -EINVAL;
376
377         if (config->type == NVMEM_TYPE_FRAM)
378                 bin_attr_nvmem_eeprom_compat.attr.name = "fram";
379
380         nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
381         nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
382         nvmem->eeprom.size = nvmem->size;
383 #ifdef CONFIG_DEBUG_LOCK_ALLOC
384         nvmem->eeprom.attr.key = &eeprom_lock_key;
385 #endif
386         nvmem->eeprom.private = &nvmem->dev;
387         nvmem->base_dev = config->base_dev;
388
389         rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
390         if (rval) {
391                 dev_err(&nvmem->dev,
392                         "Failed to create eeprom binary file %d\n", rval);
393                 return rval;
394         }
395
396         nvmem->flags |= FLAG_COMPAT;
397
398         return 0;
399 }
400
401 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
402                               const struct nvmem_config *config)
403 {
404         if (config->compat)
405                 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
406 }
407
408 #else /* CONFIG_NVMEM_SYSFS */
409
410 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
411                                     const struct nvmem_config *config)
412 {
413         return -ENOSYS;
414 }
415 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
416                                       const struct nvmem_config *config)
417 {
418 }
419
420 #endif /* CONFIG_NVMEM_SYSFS */
421
422 static void nvmem_release(struct device *dev)
423 {
424         struct nvmem_device *nvmem = to_nvmem_device(dev);
425
426         ida_free(&nvmem_ida, nvmem->id);
427         gpiod_put(nvmem->wp_gpio);
428         kfree(nvmem);
429 }
430
431 static const struct device_type nvmem_provider_type = {
432         .release        = nvmem_release,
433 };
434
435 static struct bus_type nvmem_bus_type = {
436         .name           = "nvmem",
437 };
438
439 static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
440 {
441         blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
442         mutex_lock(&nvmem_mutex);
443         list_del(&cell->node);
444         mutex_unlock(&nvmem_mutex);
445         of_node_put(cell->np);
446         kfree_const(cell->name);
447         kfree(cell);
448 }
449
450 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
451 {
452         struct nvmem_cell_entry *cell, *p;
453
454         list_for_each_entry_safe(cell, p, &nvmem->cells, node)
455                 nvmem_cell_entry_drop(cell);
456 }
457
458 static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
459 {
460         mutex_lock(&nvmem_mutex);
461         list_add_tail(&cell->node, &cell->nvmem->cells);
462         mutex_unlock(&nvmem_mutex);
463         blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
464 }
465
466 static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
467                                                      const struct nvmem_cell_info *info,
468                                                      struct nvmem_cell_entry *cell)
469 {
470         cell->nvmem = nvmem;
471         cell->offset = info->offset;
472         cell->bytes = info->bytes;
473         cell->name = info->name;
474         cell->read_post_process = info->read_post_process;
475         cell->priv = info->priv;
476
477         cell->bit_offset = info->bit_offset;
478         cell->nbits = info->nbits;
479         cell->np = info->np;
480
481         if (cell->nbits)
482                 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
483                                            BITS_PER_BYTE);
484
485         if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
486                 dev_err(&nvmem->dev,
487                         "cell %s unaligned to nvmem stride %d\n",
488                         cell->name ?: "<unknown>", nvmem->stride);
489                 return -EINVAL;
490         }
491
492         return 0;
493 }
494
495 static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
496                                                const struct nvmem_cell_info *info,
497                                                struct nvmem_cell_entry *cell)
498 {
499         int err;
500
501         err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell);
502         if (err)
503                 return err;
504
505         cell->name = kstrdup_const(info->name, GFP_KERNEL);
506         if (!cell->name)
507                 return -ENOMEM;
508
509         return 0;
510 }
511
512 /**
513  * nvmem_add_one_cell() - Add one cell information to an nvmem device
514  *
515  * @nvmem: nvmem device to add cells to.
516  * @info: nvmem cell info to add to the device
517  *
518  * Return: 0 or negative error code on failure.
519  */
520 int nvmem_add_one_cell(struct nvmem_device *nvmem,
521                        const struct nvmem_cell_info *info)
522 {
523         struct nvmem_cell_entry *cell;
524         int rval;
525
526         cell = kzalloc(sizeof(*cell), GFP_KERNEL);
527         if (!cell)
528                 return -ENOMEM;
529
530         rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
531         if (rval) {
532                 kfree(cell);
533                 return rval;
534         }
535
536         nvmem_cell_entry_add(cell);
537
538         return 0;
539 }
540 EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
541
542 /**
543  * nvmem_add_cells() - Add cell information to an nvmem device
544  *
545  * @nvmem: nvmem device to add cells to.
546  * @info: nvmem cell info to add to the device
547  * @ncells: number of cells in info
548  *
549  * Return: 0 or negative error code on failure.
550  */
551 static int nvmem_add_cells(struct nvmem_device *nvmem,
552                     const struct nvmem_cell_info *info,
553                     int ncells)
554 {
555         int i, rval;
556
557         for (i = 0; i < ncells; i++) {
558                 rval = nvmem_add_one_cell(nvmem, &info[i]);
559                 if (rval)
560                         return rval;
561         }
562
563         return 0;
564 }
565
566 /**
567  * nvmem_register_notifier() - Register a notifier block for nvmem events.
568  *
569  * @nb: notifier block to be called on nvmem events.
570  *
571  * Return: 0 on success, negative error number on failure.
572  */
573 int nvmem_register_notifier(struct notifier_block *nb)
574 {
575         return blocking_notifier_chain_register(&nvmem_notifier, nb);
576 }
577 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
578
579 /**
580  * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
581  *
582  * @nb: notifier block to be unregistered.
583  *
584  * Return: 0 on success, negative error number on failure.
585  */
586 int nvmem_unregister_notifier(struct notifier_block *nb)
587 {
588         return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
589 }
590 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
591
592 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
593 {
594         const struct nvmem_cell_info *info;
595         struct nvmem_cell_table *table;
596         struct nvmem_cell_entry *cell;
597         int rval = 0, i;
598
599         mutex_lock(&nvmem_cell_mutex);
600         list_for_each_entry(table, &nvmem_cell_tables, node) {
601                 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
602                         for (i = 0; i < table->ncells; i++) {
603                                 info = &table->cells[i];
604
605                                 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
606                                 if (!cell) {
607                                         rval = -ENOMEM;
608                                         goto out;
609                                 }
610
611                                 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
612                                 if (rval) {
613                                         kfree(cell);
614                                         goto out;
615                                 }
616
617                                 nvmem_cell_entry_add(cell);
618                         }
619                 }
620         }
621
622 out:
623         mutex_unlock(&nvmem_cell_mutex);
624         return rval;
625 }
626
627 static struct nvmem_cell_entry *
628 nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
629 {
630         struct nvmem_cell_entry *iter, *cell = NULL;
631
632         mutex_lock(&nvmem_mutex);
633         list_for_each_entry(iter, &nvmem->cells, node) {
634                 if (strcmp(cell_id, iter->name) == 0) {
635                         cell = iter;
636                         break;
637                 }
638         }
639         mutex_unlock(&nvmem_mutex);
640
641         return cell;
642 }
643
644 static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
645 {
646         unsigned int cur = 0;
647         const struct nvmem_keepout *keepout = nvmem->keepout;
648         const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
649
650         while (keepout < keepoutend) {
651                 /* Ensure keepouts are sorted and don't overlap. */
652                 if (keepout->start < cur) {
653                         dev_err(&nvmem->dev,
654                                 "Keepout regions aren't sorted or overlap.\n");
655
656                         return -ERANGE;
657                 }
658
659                 if (keepout->end < keepout->start) {
660                         dev_err(&nvmem->dev,
661                                 "Invalid keepout region.\n");
662
663                         return -EINVAL;
664                 }
665
666                 /*
667                  * Validate keepouts (and holes between) don't violate
668                  * word_size constraints.
669                  */
670                 if ((keepout->end - keepout->start < nvmem->word_size) ||
671                     ((keepout->start != cur) &&
672                      (keepout->start - cur < nvmem->word_size))) {
673
674                         dev_err(&nvmem->dev,
675                                 "Keepout regions violate word_size constraints.\n");
676
677                         return -ERANGE;
678                 }
679
680                 /* Validate keepouts don't violate stride (alignment). */
681                 if (!IS_ALIGNED(keepout->start, nvmem->stride) ||
682                     !IS_ALIGNED(keepout->end, nvmem->stride)) {
683
684                         dev_err(&nvmem->dev,
685                                 "Keepout regions violate stride.\n");
686
687                         return -EINVAL;
688                 }
689
690                 cur = keepout->end;
691                 keepout++;
692         }
693
694         return 0;
695 }
696
697 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
698 {
699         struct nvmem_layout *layout = nvmem->layout;
700         struct device *dev = &nvmem->dev;
701         struct device_node *child;
702         const __be32 *addr;
703         int len, ret;
704
705         for_each_child_of_node(dev->of_node, child) {
706                 struct nvmem_cell_info info = {0};
707
708                 addr = of_get_property(child, "reg", &len);
709                 if (!addr)
710                         continue;
711                 if (len < 2 * sizeof(u32)) {
712                         dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
713                         of_node_put(child);
714                         return -EINVAL;
715                 }
716
717                 info.offset = be32_to_cpup(addr++);
718                 info.bytes = be32_to_cpup(addr);
719                 info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
720
721                 addr = of_get_property(child, "bits", &len);
722                 if (addr && len == (2 * sizeof(u32))) {
723                         info.bit_offset = be32_to_cpup(addr++);
724                         info.nbits = be32_to_cpup(addr);
725                 }
726
727                 info.np = of_node_get(child);
728
729                 if (layout && layout->fixup_cell_info)
730                         layout->fixup_cell_info(nvmem, layout, &info);
731
732                 ret = nvmem_add_one_cell(nvmem, &info);
733                 kfree(info.name);
734                 if (ret) {
735                         of_node_put(child);
736                         return ret;
737                 }
738         }
739
740         return 0;
741 }
742
743 int __nvmem_layout_register(struct nvmem_layout *layout, struct module *owner)
744 {
745         layout->owner = owner;
746
747         spin_lock(&nvmem_layout_lock);
748         list_add(&layout->node, &nvmem_layouts);
749         spin_unlock(&nvmem_layout_lock);
750
751         return 0;
752 }
753 EXPORT_SYMBOL_GPL(__nvmem_layout_register);
754
755 void nvmem_layout_unregister(struct nvmem_layout *layout)
756 {
757         spin_lock(&nvmem_layout_lock);
758         list_del(&layout->node);
759         spin_unlock(&nvmem_layout_lock);
760 }
761 EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
762
763 static struct nvmem_layout *nvmem_layout_get(struct nvmem_device *nvmem)
764 {
765         struct device_node *layout_np, *np = nvmem->dev.of_node;
766         struct nvmem_layout *l, *layout = ERR_PTR(-EPROBE_DEFER);
767
768         layout_np = of_get_child_by_name(np, "nvmem-layout");
769         if (!layout_np)
770                 return NULL;
771
772         /*
773          * In case the nvmem device was built-in while the layout was built as a
774          * module, we shall manually request the layout driver loading otherwise
775          * we'll never have any match.
776          */
777         of_request_module(layout_np);
778
779         spin_lock(&nvmem_layout_lock);
780
781         list_for_each_entry(l, &nvmem_layouts, node) {
782                 if (of_match_node(l->of_match_table, layout_np)) {
783                         if (try_module_get(l->owner))
784                                 layout = l;
785
786                         break;
787                 }
788         }
789
790         spin_unlock(&nvmem_layout_lock);
791         of_node_put(layout_np);
792
793         return layout;
794 }
795
796 static void nvmem_layout_put(struct nvmem_layout *layout)
797 {
798         if (layout)
799                 module_put(layout->owner);
800 }
801
802 static int nvmem_add_cells_from_layout(struct nvmem_device *nvmem)
803 {
804         struct nvmem_layout *layout = nvmem->layout;
805         int ret;
806
807         if (layout && layout->add_cells) {
808                 ret = layout->add_cells(&nvmem->dev, nvmem, layout);
809                 if (ret)
810                         return ret;
811         }
812
813         return 0;
814 }
815
816 #if IS_ENABLED(CONFIG_OF)
817 /**
818  * of_nvmem_layout_get_container() - Get OF node to layout container.
819  *
820  * @nvmem: nvmem device.
821  *
822  * Return: a node pointer with refcount incremented or NULL if no
823  * container exists. Use of_node_put() on it when done.
824  */
825 struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem)
826 {
827         return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout");
828 }
829 EXPORT_SYMBOL_GPL(of_nvmem_layout_get_container);
830 #endif
831
832 const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem,
833                                         struct nvmem_layout *layout)
834 {
835         struct device_node __maybe_unused *layout_np;
836         const struct of_device_id *match;
837
838         layout_np = of_nvmem_layout_get_container(nvmem);
839         match = of_match_node(layout->of_match_table, layout_np);
840
841         return match ? match->data : NULL;
842 }
843 EXPORT_SYMBOL_GPL(nvmem_layout_get_match_data);
844
845 /**
846  * nvmem_register() - Register a nvmem device for given nvmem_config.
847  * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
848  *
849  * @config: nvmem device configuration with which nvmem device is created.
850  *
851  * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
852  * on success.
853  */
854
855 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
856 {
857         struct nvmem_device *nvmem;
858         int rval;
859
860         if (!config->dev)
861                 return ERR_PTR(-EINVAL);
862
863         if (!config->reg_read && !config->reg_write)
864                 return ERR_PTR(-EINVAL);
865
866         nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
867         if (!nvmem)
868                 return ERR_PTR(-ENOMEM);
869
870         rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
871         if (rval < 0) {
872                 kfree(nvmem);
873                 return ERR_PTR(rval);
874         }
875
876         nvmem->id = rval;
877
878         nvmem->dev.type = &nvmem_provider_type;
879         nvmem->dev.bus = &nvmem_bus_type;
880         nvmem->dev.parent = config->dev;
881
882         device_initialize(&nvmem->dev);
883
884         if (!config->ignore_wp)
885                 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
886                                                     GPIOD_OUT_HIGH);
887         if (IS_ERR(nvmem->wp_gpio)) {
888                 rval = PTR_ERR(nvmem->wp_gpio);
889                 nvmem->wp_gpio = NULL;
890                 goto err_put_device;
891         }
892
893         kref_init(&nvmem->refcnt);
894         INIT_LIST_HEAD(&nvmem->cells);
895
896         nvmem->owner = config->owner;
897         if (!nvmem->owner && config->dev->driver)
898                 nvmem->owner = config->dev->driver->owner;
899         nvmem->stride = config->stride ?: 1;
900         nvmem->word_size = config->word_size ?: 1;
901         nvmem->size = config->size;
902         nvmem->root_only = config->root_only;
903         nvmem->priv = config->priv;
904         nvmem->type = config->type;
905         nvmem->reg_read = config->reg_read;
906         nvmem->reg_write = config->reg_write;
907         nvmem->keepout = config->keepout;
908         nvmem->nkeepout = config->nkeepout;
909         if (config->of_node)
910                 nvmem->dev.of_node = config->of_node;
911         else if (!config->no_of_node)
912                 nvmem->dev.of_node = config->dev->of_node;
913
914         switch (config->id) {
915         case NVMEM_DEVID_NONE:
916                 rval = dev_set_name(&nvmem->dev, "%s", config->name);
917                 break;
918         case NVMEM_DEVID_AUTO:
919                 rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
920                 break;
921         default:
922                 rval = dev_set_name(&nvmem->dev, "%s%d",
923                              config->name ? : "nvmem",
924                              config->name ? config->id : nvmem->id);
925                 break;
926         }
927
928         if (rval)
929                 goto err_put_device;
930
931         nvmem->read_only = device_property_present(config->dev, "read-only") ||
932                            config->read_only || !nvmem->reg_write;
933
934 #ifdef CONFIG_NVMEM_SYSFS
935         nvmem->dev.groups = nvmem_dev_groups;
936 #endif
937
938         if (nvmem->nkeepout) {
939                 rval = nvmem_validate_keepouts(nvmem);
940                 if (rval)
941                         goto err_put_device;
942         }
943
944         if (config->compat) {
945                 rval = nvmem_sysfs_setup_compat(nvmem, config);
946                 if (rval)
947                         goto err_put_device;
948         }
949
950         /*
951          * If the driver supplied a layout by config->layout, the module
952          * pointer will be NULL and nvmem_layout_put() will be a noop.
953          */
954         nvmem->layout = config->layout ?: nvmem_layout_get(nvmem);
955         if (IS_ERR(nvmem->layout)) {
956                 rval = PTR_ERR(nvmem->layout);
957                 nvmem->layout = NULL;
958
959                 if (rval == -EPROBE_DEFER)
960                         goto err_teardown_compat;
961         }
962
963         if (config->cells) {
964                 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
965                 if (rval)
966                         goto err_remove_cells;
967         }
968
969         rval = nvmem_add_cells_from_table(nvmem);
970         if (rval)
971                 goto err_remove_cells;
972
973         rval = nvmem_add_cells_from_of(nvmem);
974         if (rval)
975                 goto err_remove_cells;
976
977         dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
978
979         rval = device_add(&nvmem->dev);
980         if (rval)
981                 goto err_remove_cells;
982
983         rval = nvmem_add_cells_from_layout(nvmem);
984         if (rval)
985                 goto err_remove_cells;
986
987         blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
988
989         return nvmem;
990
991 err_remove_cells:
992         nvmem_device_remove_all_cells(nvmem);
993         nvmem_layout_put(nvmem->layout);
994 err_teardown_compat:
995         if (config->compat)
996                 nvmem_sysfs_remove_compat(nvmem, config);
997 err_put_device:
998         put_device(&nvmem->dev);
999
1000         return ERR_PTR(rval);
1001 }
1002 EXPORT_SYMBOL_GPL(nvmem_register);
1003
1004 static void nvmem_device_release(struct kref *kref)
1005 {
1006         struct nvmem_device *nvmem;
1007
1008         nvmem = container_of(kref, struct nvmem_device, refcnt);
1009
1010         blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
1011
1012         if (nvmem->flags & FLAG_COMPAT)
1013                 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
1014
1015         nvmem_device_remove_all_cells(nvmem);
1016         nvmem_layout_put(nvmem->layout);
1017         device_unregister(&nvmem->dev);
1018 }
1019
1020 /**
1021  * nvmem_unregister() - Unregister previously registered nvmem device
1022  *
1023  * @nvmem: Pointer to previously registered nvmem device.
1024  */
1025 void nvmem_unregister(struct nvmem_device *nvmem)
1026 {
1027         if (nvmem)
1028                 kref_put(&nvmem->refcnt, nvmem_device_release);
1029 }
1030 EXPORT_SYMBOL_GPL(nvmem_unregister);
1031
1032 static void devm_nvmem_unregister(void *nvmem)
1033 {
1034         nvmem_unregister(nvmem);
1035 }
1036
1037 /**
1038  * devm_nvmem_register() - Register a managed nvmem device for given
1039  * nvmem_config.
1040  * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
1041  *
1042  * @dev: Device that uses the nvmem device.
1043  * @config: nvmem device configuration with which nvmem device is created.
1044  *
1045  * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
1046  * on success.
1047  */
1048 struct nvmem_device *devm_nvmem_register(struct device *dev,
1049                                          const struct nvmem_config *config)
1050 {
1051         struct nvmem_device *nvmem;
1052         int ret;
1053
1054         nvmem = nvmem_register(config);
1055         if (IS_ERR(nvmem))
1056                 return nvmem;
1057
1058         ret = devm_add_action_or_reset(dev, devm_nvmem_unregister, nvmem);
1059         if (ret)
1060                 return ERR_PTR(ret);
1061
1062         return nvmem;
1063 }
1064 EXPORT_SYMBOL_GPL(devm_nvmem_register);
1065
1066 static struct nvmem_device *__nvmem_device_get(void *data,
1067                         int (*match)(struct device *dev, const void *data))
1068 {
1069         struct nvmem_device *nvmem = NULL;
1070         struct device *dev;
1071
1072         mutex_lock(&nvmem_mutex);
1073         dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
1074         if (dev)
1075                 nvmem = to_nvmem_device(dev);
1076         mutex_unlock(&nvmem_mutex);
1077         if (!nvmem)
1078                 return ERR_PTR(-EPROBE_DEFER);
1079
1080         if (!try_module_get(nvmem->owner)) {
1081                 dev_err(&nvmem->dev,
1082                         "could not increase module refcount for cell %s\n",
1083                         nvmem_dev_name(nvmem));
1084
1085                 put_device(&nvmem->dev);
1086                 return ERR_PTR(-EINVAL);
1087         }
1088
1089         kref_get(&nvmem->refcnt);
1090
1091         return nvmem;
1092 }
1093
1094 static void __nvmem_device_put(struct nvmem_device *nvmem)
1095 {
1096         put_device(&nvmem->dev);
1097         module_put(nvmem->owner);
1098         kref_put(&nvmem->refcnt, nvmem_device_release);
1099 }
1100
1101 #if IS_ENABLED(CONFIG_OF)
1102 /**
1103  * of_nvmem_device_get() - Get nvmem device from a given id
1104  *
1105  * @np: Device tree node that uses the nvmem device.
1106  * @id: nvmem name from nvmem-names property.
1107  *
1108  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1109  * on success.
1110  */
1111 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
1112 {
1113
1114         struct device_node *nvmem_np;
1115         struct nvmem_device *nvmem;
1116         int index = 0;
1117
1118         if (id)
1119                 index = of_property_match_string(np, "nvmem-names", id);
1120
1121         nvmem_np = of_parse_phandle(np, "nvmem", index);
1122         if (!nvmem_np)
1123                 return ERR_PTR(-ENOENT);
1124
1125         nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1126         of_node_put(nvmem_np);
1127         return nvmem;
1128 }
1129 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
1130 #endif
1131
1132 /**
1133  * nvmem_device_get() - Get nvmem device from a given id
1134  *
1135  * @dev: Device that uses the nvmem device.
1136  * @dev_name: name of the requested nvmem device.
1137  *
1138  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1139  * on success.
1140  */
1141 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
1142 {
1143         if (dev->of_node) { /* try dt first */
1144                 struct nvmem_device *nvmem;
1145
1146                 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
1147
1148                 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
1149                         return nvmem;
1150
1151         }
1152
1153         return __nvmem_device_get((void *)dev_name, device_match_name);
1154 }
1155 EXPORT_SYMBOL_GPL(nvmem_device_get);
1156
1157 /**
1158  * nvmem_device_find() - Find nvmem device with matching function
1159  *
1160  * @data: Data to pass to match function
1161  * @match: Callback function to check device
1162  *
1163  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1164  * on success.
1165  */
1166 struct nvmem_device *nvmem_device_find(void *data,
1167                         int (*match)(struct device *dev, const void *data))
1168 {
1169         return __nvmem_device_get(data, match);
1170 }
1171 EXPORT_SYMBOL_GPL(nvmem_device_find);
1172
1173 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
1174 {
1175         struct nvmem_device **nvmem = res;
1176
1177         if (WARN_ON(!nvmem || !*nvmem))
1178                 return 0;
1179
1180         return *nvmem == data;
1181 }
1182
1183 static void devm_nvmem_device_release(struct device *dev, void *res)
1184 {
1185         nvmem_device_put(*(struct nvmem_device **)res);
1186 }
1187
1188 /**
1189  * devm_nvmem_device_put() - put alredy got nvmem device
1190  *
1191  * @dev: Device that uses the nvmem device.
1192  * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
1193  * that needs to be released.
1194  */
1195 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
1196 {
1197         int ret;
1198
1199         ret = devres_release(dev, devm_nvmem_device_release,
1200                              devm_nvmem_device_match, nvmem);
1201
1202         WARN_ON(ret);
1203 }
1204 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
1205
1206 /**
1207  * nvmem_device_put() - put alredy got nvmem device
1208  *
1209  * @nvmem: pointer to nvmem device that needs to be released.
1210  */
1211 void nvmem_device_put(struct nvmem_device *nvmem)
1212 {
1213         __nvmem_device_put(nvmem);
1214 }
1215 EXPORT_SYMBOL_GPL(nvmem_device_put);
1216
1217 /**
1218  * devm_nvmem_device_get() - Get nvmem cell of device form a given id
1219  *
1220  * @dev: Device that requests the nvmem device.
1221  * @id: name id for the requested nvmem device.
1222  *
1223  * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
1224  * on success.  The nvmem_cell will be freed by the automatically once the
1225  * device is freed.
1226  */
1227 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
1228 {
1229         struct nvmem_device **ptr, *nvmem;
1230
1231         ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
1232         if (!ptr)
1233                 return ERR_PTR(-ENOMEM);
1234
1235         nvmem = nvmem_device_get(dev, id);
1236         if (!IS_ERR(nvmem)) {
1237                 *ptr = nvmem;
1238                 devres_add(dev, ptr);
1239         } else {
1240                 devres_free(ptr);
1241         }
1242
1243         return nvmem;
1244 }
1245 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
1246
1247 static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
1248                                             const char *id, int index)
1249 {
1250         struct nvmem_cell *cell;
1251         const char *name = NULL;
1252
1253         cell = kzalloc(sizeof(*cell), GFP_KERNEL);
1254         if (!cell)
1255                 return ERR_PTR(-ENOMEM);
1256
1257         if (id) {
1258                 name = kstrdup_const(id, GFP_KERNEL);
1259                 if (!name) {
1260                         kfree(cell);
1261                         return ERR_PTR(-ENOMEM);
1262                 }
1263         }
1264
1265         cell->id = name;
1266         cell->entry = entry;
1267         cell->index = index;
1268
1269         return cell;
1270 }
1271
1272 static struct nvmem_cell *
1273 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
1274 {
1275         struct nvmem_cell_entry *cell_entry;
1276         struct nvmem_cell *cell = ERR_PTR(-ENOENT);
1277         struct nvmem_cell_lookup *lookup;
1278         struct nvmem_device *nvmem;
1279         const char *dev_id;
1280
1281         if (!dev)
1282                 return ERR_PTR(-EINVAL);
1283
1284         dev_id = dev_name(dev);
1285
1286         mutex_lock(&nvmem_lookup_mutex);
1287
1288         list_for_each_entry(lookup, &nvmem_lookup_list, node) {
1289                 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
1290                     (strcmp(lookup->con_id, con_id) == 0)) {
1291                         /* This is the right entry. */
1292                         nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1293                                                    device_match_name);
1294                         if (IS_ERR(nvmem)) {
1295                                 /* Provider may not be registered yet. */
1296                                 cell = ERR_CAST(nvmem);
1297                                 break;
1298                         }
1299
1300                         cell_entry = nvmem_find_cell_entry_by_name(nvmem,
1301                                                                    lookup->cell_name);
1302                         if (!cell_entry) {
1303                                 __nvmem_device_put(nvmem);
1304                                 cell = ERR_PTR(-ENOENT);
1305                         } else {
1306                                 cell = nvmem_create_cell(cell_entry, con_id, 0);
1307                                 if (IS_ERR(cell))
1308                                         __nvmem_device_put(nvmem);
1309                         }
1310                         break;
1311                 }
1312         }
1313
1314         mutex_unlock(&nvmem_lookup_mutex);
1315         return cell;
1316 }
1317
1318 #if IS_ENABLED(CONFIG_OF)
1319 static struct nvmem_cell_entry *
1320 nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
1321 {
1322         struct nvmem_cell_entry *iter, *cell = NULL;
1323
1324         mutex_lock(&nvmem_mutex);
1325         list_for_each_entry(iter, &nvmem->cells, node) {
1326                 if (np == iter->np) {
1327                         cell = iter;
1328                         break;
1329                 }
1330         }
1331         mutex_unlock(&nvmem_mutex);
1332
1333         return cell;
1334 }
1335
1336 /**
1337  * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1338  *
1339  * @np: Device tree node that uses the nvmem cell.
1340  * @id: nvmem cell name from nvmem-cell-names property, or NULL
1341  *      for the cell at index 0 (the lone cell with no accompanying
1342  *      nvmem-cell-names property).
1343  *
1344  * Return: Will be an ERR_PTR() on error or a valid pointer
1345  * to a struct nvmem_cell.  The nvmem_cell will be freed by the
1346  * nvmem_cell_put().
1347  */
1348 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1349 {
1350         struct device_node *cell_np, *nvmem_np;
1351         struct nvmem_device *nvmem;
1352         struct nvmem_cell_entry *cell_entry;
1353         struct nvmem_cell *cell;
1354         struct of_phandle_args cell_spec;
1355         int index = 0;
1356         int cell_index = 0;
1357         int ret;
1358
1359         /* if cell name exists, find index to the name */
1360         if (id)
1361                 index = of_property_match_string(np, "nvmem-cell-names", id);
1362
1363         ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
1364                                                   "#nvmem-cell-cells",
1365                                                   index, &cell_spec);
1366         if (ret)
1367                 return ERR_PTR(-ENOENT);
1368
1369         if (cell_spec.args_count > 1)
1370                 return ERR_PTR(-EINVAL);
1371
1372         cell_np = cell_spec.np;
1373         if (cell_spec.args_count)
1374                 cell_index = cell_spec.args[0];
1375
1376         nvmem_np = of_get_parent(cell_np);
1377         if (!nvmem_np) {
1378                 of_node_put(cell_np);
1379                 return ERR_PTR(-EINVAL);
1380         }
1381
1382         /* nvmem layouts produce cells within the nvmem-layout container */
1383         if (of_node_name_eq(nvmem_np, "nvmem-layout")) {
1384                 nvmem_np = of_get_next_parent(nvmem_np);
1385                 if (!nvmem_np) {
1386                         of_node_put(cell_np);
1387                         return ERR_PTR(-EINVAL);
1388                 }
1389         }
1390
1391         nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1392         of_node_put(nvmem_np);
1393         if (IS_ERR(nvmem)) {
1394                 of_node_put(cell_np);
1395                 return ERR_CAST(nvmem);
1396         }
1397
1398         cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
1399         of_node_put(cell_np);
1400         if (!cell_entry) {
1401                 __nvmem_device_put(nvmem);
1402                 return ERR_PTR(-ENOENT);
1403         }
1404
1405         cell = nvmem_create_cell(cell_entry, id, cell_index);
1406         if (IS_ERR(cell))
1407                 __nvmem_device_put(nvmem);
1408
1409         return cell;
1410 }
1411 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1412 #endif
1413
1414 /**
1415  * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1416  *
1417  * @dev: Device that requests the nvmem cell.
1418  * @id: nvmem cell name to get (this corresponds with the name from the
1419  *      nvmem-cell-names property for DT systems and with the con_id from
1420  *      the lookup entry for non-DT systems).
1421  *
1422  * Return: Will be an ERR_PTR() on error or a valid pointer
1423  * to a struct nvmem_cell.  The nvmem_cell will be freed by the
1424  * nvmem_cell_put().
1425  */
1426 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1427 {
1428         struct nvmem_cell *cell;
1429
1430         if (dev->of_node) { /* try dt first */
1431                 cell = of_nvmem_cell_get(dev->of_node, id);
1432                 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1433                         return cell;
1434         }
1435
1436         /* NULL cell id only allowed for device tree; invalid otherwise */
1437         if (!id)
1438                 return ERR_PTR(-EINVAL);
1439
1440         return nvmem_cell_get_from_lookup(dev, id);
1441 }
1442 EXPORT_SYMBOL_GPL(nvmem_cell_get);
1443
1444 static void devm_nvmem_cell_release(struct device *dev, void *res)
1445 {
1446         nvmem_cell_put(*(struct nvmem_cell **)res);
1447 }
1448
1449 /**
1450  * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1451  *
1452  * @dev: Device that requests the nvmem cell.
1453  * @id: nvmem cell name id to get.
1454  *
1455  * Return: Will be an ERR_PTR() on error or a valid pointer
1456  * to a struct nvmem_cell.  The nvmem_cell will be freed by the
1457  * automatically once the device is freed.
1458  */
1459 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1460 {
1461         struct nvmem_cell **ptr, *cell;
1462
1463         ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1464         if (!ptr)
1465                 return ERR_PTR(-ENOMEM);
1466
1467         cell = nvmem_cell_get(dev, id);
1468         if (!IS_ERR(cell)) {
1469                 *ptr = cell;
1470                 devres_add(dev, ptr);
1471         } else {
1472                 devres_free(ptr);
1473         }
1474
1475         return cell;
1476 }
1477 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1478
1479 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1480 {
1481         struct nvmem_cell **c = res;
1482
1483         if (WARN_ON(!c || !*c))
1484                 return 0;
1485
1486         return *c == data;
1487 }
1488
1489 /**
1490  * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1491  * from devm_nvmem_cell_get.
1492  *
1493  * @dev: Device that requests the nvmem cell.
1494  * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1495  */
1496 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1497 {
1498         int ret;
1499
1500         ret = devres_release(dev, devm_nvmem_cell_release,
1501                                 devm_nvmem_cell_match, cell);
1502
1503         WARN_ON(ret);
1504 }
1505 EXPORT_SYMBOL(devm_nvmem_cell_put);
1506
1507 /**
1508  * nvmem_cell_put() - Release previously allocated nvmem cell.
1509  *
1510  * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1511  */
1512 void nvmem_cell_put(struct nvmem_cell *cell)
1513 {
1514         struct nvmem_device *nvmem = cell->entry->nvmem;
1515
1516         if (cell->id)
1517                 kfree_const(cell->id);
1518
1519         kfree(cell);
1520         __nvmem_device_put(nvmem);
1521 }
1522 EXPORT_SYMBOL_GPL(nvmem_cell_put);
1523
1524 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
1525 {
1526         u8 *p, *b;
1527         int i, extra, bit_offset = cell->bit_offset;
1528
1529         p = b = buf;
1530         if (bit_offset) {
1531                 /* First shift */
1532                 *b++ >>= bit_offset;
1533
1534                 /* setup rest of the bytes if any */
1535                 for (i = 1; i < cell->bytes; i++) {
1536                         /* Get bits from next byte and shift them towards msb */
1537                         *p |= *b << (BITS_PER_BYTE - bit_offset);
1538
1539                         p = b;
1540                         *b++ >>= bit_offset;
1541                 }
1542         } else {
1543                 /* point to the msb */
1544                 p += cell->bytes - 1;
1545         }
1546
1547         /* result fits in less bytes */
1548         extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1549         while (--extra >= 0)
1550                 *p-- = 0;
1551
1552         /* clear msb bits if any leftover in the last byte */
1553         if (cell->nbits % BITS_PER_BYTE)
1554                 *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
1555 }
1556
1557 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1558                              struct nvmem_cell_entry *cell,
1559                              void *buf, size_t *len, const char *id, int index)
1560 {
1561         int rc;
1562
1563         rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
1564
1565         if (rc)
1566                 return rc;
1567
1568         /* shift bits in-place */
1569         if (cell->bit_offset || cell->nbits)
1570                 nvmem_shift_read_buffer_in_place(cell, buf);
1571
1572         if (cell->read_post_process) {
1573                 rc = cell->read_post_process(cell->priv, id, index,
1574                                              cell->offset, buf, cell->bytes);
1575                 if (rc)
1576                         return rc;
1577         }
1578
1579         if (len)
1580                 *len = cell->bytes;
1581
1582         return 0;
1583 }
1584
1585 /**
1586  * nvmem_cell_read() - Read a given nvmem cell
1587  *
1588  * @cell: nvmem cell to be read.
1589  * @len: pointer to length of cell which will be populated on successful read;
1590  *       can be NULL.
1591  *
1592  * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1593  * buffer should be freed by the consumer with a kfree().
1594  */
1595 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1596 {
1597         struct nvmem_device *nvmem = cell->entry->nvmem;
1598         u8 *buf;
1599         int rc;
1600
1601         if (!nvmem)
1602                 return ERR_PTR(-EINVAL);
1603
1604         buf = kzalloc(cell->entry->bytes, GFP_KERNEL);
1605         if (!buf)
1606                 return ERR_PTR(-ENOMEM);
1607
1608         rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index);
1609         if (rc) {
1610                 kfree(buf);
1611                 return ERR_PTR(rc);
1612         }
1613
1614         return buf;
1615 }
1616 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1617
1618 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
1619                                              u8 *_buf, int len)
1620 {
1621         struct nvmem_device *nvmem = cell->nvmem;
1622         int i, rc, nbits, bit_offset = cell->bit_offset;
1623         u8 v, *p, *buf, *b, pbyte, pbits;
1624
1625         nbits = cell->nbits;
1626         buf = kzalloc(cell->bytes, GFP_KERNEL);
1627         if (!buf)
1628                 return ERR_PTR(-ENOMEM);
1629
1630         memcpy(buf, _buf, len);
1631         p = b = buf;
1632
1633         if (bit_offset) {
1634                 pbyte = *b;
1635                 *b <<= bit_offset;
1636
1637                 /* setup the first byte with lsb bits from nvmem */
1638                 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1639                 if (rc)
1640                         goto err;
1641                 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1642
1643                 /* setup rest of the byte if any */
1644                 for (i = 1; i < cell->bytes; i++) {
1645                         /* Get last byte bits and shift them towards lsb */
1646                         pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1647                         pbyte = *b;
1648                         p = b;
1649                         *b <<= bit_offset;
1650                         *b++ |= pbits;
1651                 }
1652         }
1653
1654         /* if it's not end on byte boundary */
1655         if ((nbits + bit_offset) % BITS_PER_BYTE) {
1656                 /* setup the last byte with msb bits from nvmem */
1657                 rc = nvmem_reg_read(nvmem,
1658                                     cell->offset + cell->bytes - 1, &v, 1);
1659                 if (rc)
1660                         goto err;
1661                 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1662
1663         }
1664
1665         return buf;
1666 err:
1667         kfree(buf);
1668         return ERR_PTR(rc);
1669 }
1670
1671 static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
1672 {
1673         struct nvmem_device *nvmem = cell->nvmem;
1674         int rc;
1675
1676         if (!nvmem || nvmem->read_only ||
1677             (cell->bit_offset == 0 && len != cell->bytes))
1678                 return -EINVAL;
1679
1680         /*
1681          * Any cells which have a read_post_process hook are read-only because
1682          * we cannot reverse the operation and it might affect other cells,
1683          * too.
1684          */
1685         if (cell->read_post_process)
1686                 return -EINVAL;
1687
1688         if (cell->bit_offset || cell->nbits) {
1689                 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1690                 if (IS_ERR(buf))
1691                         return PTR_ERR(buf);
1692         }
1693
1694         rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1695
1696         /* free the tmp buffer */
1697         if (cell->bit_offset || cell->nbits)
1698                 kfree(buf);
1699
1700         if (rc)
1701                 return rc;
1702
1703         return len;
1704 }
1705
1706 /**
1707  * nvmem_cell_write() - Write to a given nvmem cell
1708  *
1709  * @cell: nvmem cell to be written.
1710  * @buf: Buffer to be written.
1711  * @len: length of buffer to be written to nvmem cell.
1712  *
1713  * Return: length of bytes written or negative on failure.
1714  */
1715 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1716 {
1717         return __nvmem_cell_entry_write(cell->entry, buf, len);
1718 }
1719
1720 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1721
1722 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1723                                   void *val, size_t count)
1724 {
1725         struct nvmem_cell *cell;
1726         void *buf;
1727         size_t len;
1728
1729         cell = nvmem_cell_get(dev, cell_id);
1730         if (IS_ERR(cell))
1731                 return PTR_ERR(cell);
1732
1733         buf = nvmem_cell_read(cell, &len);
1734         if (IS_ERR(buf)) {
1735                 nvmem_cell_put(cell);
1736                 return PTR_ERR(buf);
1737         }
1738         if (len != count) {
1739                 kfree(buf);
1740                 nvmem_cell_put(cell);
1741                 return -EINVAL;
1742         }
1743         memcpy(val, buf, count);
1744         kfree(buf);
1745         nvmem_cell_put(cell);
1746
1747         return 0;
1748 }
1749
1750 /**
1751  * nvmem_cell_read_u8() - Read a cell value as a u8
1752  *
1753  * @dev: Device that requests the nvmem cell.
1754  * @cell_id: Name of nvmem cell to read.
1755  * @val: pointer to output value.
1756  *
1757  * Return: 0 on success or negative errno.
1758  */
1759 int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1760 {
1761         return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1762 }
1763 EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1764
1765 /**
1766  * nvmem_cell_read_u16() - Read a cell value as a u16
1767  *
1768  * @dev: Device that requests the nvmem cell.
1769  * @cell_id: Name of nvmem cell to read.
1770  * @val: pointer to output value.
1771  *
1772  * Return: 0 on success or negative errno.
1773  */
1774 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1775 {
1776         return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1777 }
1778 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1779
1780 /**
1781  * nvmem_cell_read_u32() - Read a cell value as a u32
1782  *
1783  * @dev: Device that requests the nvmem cell.
1784  * @cell_id: Name of nvmem cell to read.
1785  * @val: pointer to output value.
1786  *
1787  * Return: 0 on success or negative errno.
1788  */
1789 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1790 {
1791         return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1792 }
1793 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1794
1795 /**
1796  * nvmem_cell_read_u64() - Read a cell value as a u64
1797  *
1798  * @dev: Device that requests the nvmem cell.
1799  * @cell_id: Name of nvmem cell to read.
1800  * @val: pointer to output value.
1801  *
1802  * Return: 0 on success or negative errno.
1803  */
1804 int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1805 {
1806         return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1807 }
1808 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1809
1810 static const void *nvmem_cell_read_variable_common(struct device *dev,
1811                                                    const char *cell_id,
1812                                                    size_t max_len, size_t *len)
1813 {
1814         struct nvmem_cell *cell;
1815         int nbits;
1816         void *buf;
1817
1818         cell = nvmem_cell_get(dev, cell_id);
1819         if (IS_ERR(cell))
1820                 return cell;
1821
1822         nbits = cell->entry->nbits;
1823         buf = nvmem_cell_read(cell, len);
1824         nvmem_cell_put(cell);
1825         if (IS_ERR(buf))
1826                 return buf;
1827
1828         /*
1829          * If nbits is set then nvmem_cell_read() can significantly exaggerate
1830          * the length of the real data. Throw away the extra junk.
1831          */
1832         if (nbits)
1833                 *len = DIV_ROUND_UP(nbits, 8);
1834
1835         if (*len > max_len) {
1836                 kfree(buf);
1837                 return ERR_PTR(-ERANGE);
1838         }
1839
1840         return buf;
1841 }
1842
1843 /**
1844  * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
1845  *
1846  * @dev: Device that requests the nvmem cell.
1847  * @cell_id: Name of nvmem cell to read.
1848  * @val: pointer to output value.
1849  *
1850  * Return: 0 on success or negative errno.
1851  */
1852 int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
1853                                     u32 *val)
1854 {
1855         size_t len;
1856         const u8 *buf;
1857         int i;
1858
1859         buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1860         if (IS_ERR(buf))
1861                 return PTR_ERR(buf);
1862
1863         /* Copy w/ implicit endian conversion */
1864         *val = 0;
1865         for (i = 0; i < len; i++)
1866                 *val |= buf[i] << (8 * i);
1867
1868         kfree(buf);
1869
1870         return 0;
1871 }
1872 EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
1873
1874 /**
1875  * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
1876  *
1877  * @dev: Device that requests the nvmem cell.
1878  * @cell_id: Name of nvmem cell to read.
1879  * @val: pointer to output value.
1880  *
1881  * Return: 0 on success or negative errno.
1882  */
1883 int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
1884                                     u64 *val)
1885 {
1886         size_t len;
1887         const u8 *buf;
1888         int i;
1889
1890         buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1891         if (IS_ERR(buf))
1892                 return PTR_ERR(buf);
1893
1894         /* Copy w/ implicit endian conversion */
1895         *val = 0;
1896         for (i = 0; i < len; i++)
1897                 *val |= (uint64_t)buf[i] << (8 * i);
1898
1899         kfree(buf);
1900
1901         return 0;
1902 }
1903 EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
1904
1905 /**
1906  * nvmem_device_cell_read() - Read a given nvmem device and cell
1907  *
1908  * @nvmem: nvmem device to read from.
1909  * @info: nvmem cell info to be read.
1910  * @buf: buffer pointer which will be populated on successful read.
1911  *
1912  * Return: length of successful bytes read on success and negative
1913  * error code on error.
1914  */
1915 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1916                            struct nvmem_cell_info *info, void *buf)
1917 {
1918         struct nvmem_cell_entry cell;
1919         int rc;
1920         ssize_t len;
1921
1922         if (!nvmem)
1923                 return -EINVAL;
1924
1925         rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1926         if (rc)
1927                 return rc;
1928
1929         rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0);
1930         if (rc)
1931                 return rc;
1932
1933         return len;
1934 }
1935 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1936
1937 /**
1938  * nvmem_device_cell_write() - Write cell to a given nvmem device
1939  *
1940  * @nvmem: nvmem device to be written to.
1941  * @info: nvmem cell info to be written.
1942  * @buf: buffer to be written to cell.
1943  *
1944  * Return: length of bytes written or negative error code on failure.
1945  */
1946 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1947                             struct nvmem_cell_info *info, void *buf)
1948 {
1949         struct nvmem_cell_entry cell;
1950         int rc;
1951
1952         if (!nvmem)
1953                 return -EINVAL;
1954
1955         rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1956         if (rc)
1957                 return rc;
1958
1959         return __nvmem_cell_entry_write(&cell, buf, cell.bytes);
1960 }
1961 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1962
1963 /**
1964  * nvmem_device_read() - Read from a given nvmem device
1965  *
1966  * @nvmem: nvmem device to read from.
1967  * @offset: offset in nvmem device.
1968  * @bytes: number of bytes to read.
1969  * @buf: buffer pointer which will be populated on successful read.
1970  *
1971  * Return: length of successful bytes read on success and negative
1972  * error code on error.
1973  */
1974 int nvmem_device_read(struct nvmem_device *nvmem,
1975                       unsigned int offset,
1976                       size_t bytes, void *buf)
1977 {
1978         int rc;
1979
1980         if (!nvmem)
1981                 return -EINVAL;
1982
1983         rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1984
1985         if (rc)
1986                 return rc;
1987
1988         return bytes;
1989 }
1990 EXPORT_SYMBOL_GPL(nvmem_device_read);
1991
1992 /**
1993  * nvmem_device_write() - Write cell to a given nvmem device
1994  *
1995  * @nvmem: nvmem device to be written to.
1996  * @offset: offset in nvmem device.
1997  * @bytes: number of bytes to write.
1998  * @buf: buffer to be written.
1999  *
2000  * Return: length of bytes written or negative error code on failure.
2001  */
2002 int nvmem_device_write(struct nvmem_device *nvmem,
2003                        unsigned int offset,
2004                        size_t bytes, void *buf)
2005 {
2006         int rc;
2007
2008         if (!nvmem)
2009                 return -EINVAL;
2010
2011         rc = nvmem_reg_write(nvmem, offset, buf, bytes);
2012
2013         if (rc)
2014                 return rc;
2015
2016
2017         return bytes;
2018 }
2019 EXPORT_SYMBOL_GPL(nvmem_device_write);
2020
2021 /**
2022  * nvmem_add_cell_table() - register a table of cell info entries
2023  *
2024  * @table: table of cell info entries
2025  */
2026 void nvmem_add_cell_table(struct nvmem_cell_table *table)
2027 {
2028         mutex_lock(&nvmem_cell_mutex);
2029         list_add_tail(&table->node, &nvmem_cell_tables);
2030         mutex_unlock(&nvmem_cell_mutex);
2031 }
2032 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
2033
2034 /**
2035  * nvmem_del_cell_table() - remove a previously registered cell info table
2036  *
2037  * @table: table of cell info entries
2038  */
2039 void nvmem_del_cell_table(struct nvmem_cell_table *table)
2040 {
2041         mutex_lock(&nvmem_cell_mutex);
2042         list_del(&table->node);
2043         mutex_unlock(&nvmem_cell_mutex);
2044 }
2045 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
2046
2047 /**
2048  * nvmem_add_cell_lookups() - register a list of cell lookup entries
2049  *
2050  * @entries: array of cell lookup entries
2051  * @nentries: number of cell lookup entries in the array
2052  */
2053 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2054 {
2055         int i;
2056
2057         mutex_lock(&nvmem_lookup_mutex);
2058         for (i = 0; i < nentries; i++)
2059                 list_add_tail(&entries[i].node, &nvmem_lookup_list);
2060         mutex_unlock(&nvmem_lookup_mutex);
2061 }
2062 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
2063
2064 /**
2065  * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
2066  *                            entries
2067  *
2068  * @entries: array of cell lookup entries
2069  * @nentries: number of cell lookup entries in the array
2070  */
2071 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2072 {
2073         int i;
2074
2075         mutex_lock(&nvmem_lookup_mutex);
2076         for (i = 0; i < nentries; i++)
2077                 list_del(&entries[i].node);
2078         mutex_unlock(&nvmem_lookup_mutex);
2079 }
2080 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
2081
2082 /**
2083  * nvmem_dev_name() - Get the name of a given nvmem device.
2084  *
2085  * @nvmem: nvmem device.
2086  *
2087  * Return: name of the nvmem device.
2088  */
2089 const char *nvmem_dev_name(struct nvmem_device *nvmem)
2090 {
2091         return dev_name(&nvmem->dev);
2092 }
2093 EXPORT_SYMBOL_GPL(nvmem_dev_name);
2094
2095 static int __init nvmem_init(void)
2096 {
2097         return bus_register(&nvmem_bus_type);
2098 }
2099
2100 static void __exit nvmem_exit(void)
2101 {
2102         bus_unregister(&nvmem_bus_type);
2103 }
2104
2105 subsys_initcall(nvmem_init);
2106 module_exit(nvmem_exit);
2107
2108 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
2109 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
2110 MODULE_DESCRIPTION("nvmem Driver Core");
2111 MODULE_LICENSE("GPL v2");