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