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