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