blob: 27f67dfa649d08373de5d898cffb19f8e851ea93 [file] [log] [blame]
Bartosz Golaszewskib1c1db92018-09-21 06:40:20 -07001// SPDX-License-Identifier: GPL-2.0
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +01002/*
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>
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +01007 */
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>
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -070014#include <linux/kref.h>
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010015#include <linux/module.h>
16#include <linux/nvmem-consumer.h>
17#include <linux/nvmem-provider.h>
18#include <linux/of.h>
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010019#include <linux/slab.h>
20
21struct nvmem_device {
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010022 struct module *owner;
23 struct device dev;
24 int stride;
25 int word_size;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010026 int id;
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -070027 struct kref refcnt;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010028 size_t size;
29 bool read_only;
Andrew Lunnb6c217a2016-02-26 20:59:19 +010030 int flags;
31 struct bin_attribute eeprom;
32 struct device *base_dev;
Bartosz Golaszewskic7235ee2018-09-21 06:40:14 -070033 struct list_head cells;
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +010034 nvmem_reg_read_t reg_read;
35 nvmem_reg_write_t reg_write;
36 void *priv;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010037};
38
Andrew Lunnb6c217a2016-02-26 20:59:19 +010039#define FLAG_COMPAT BIT(0)
40
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010041struct nvmem_cell {
42 const char *name;
43 int offset;
44 int bytes;
45 int bit_offset;
46 int nbits;
Srinivas Kandagatla0749aa22018-11-06 15:41:41 +000047 struct device_node *np;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010048 struct nvmem_device *nvmem;
49 struct list_head node;
50};
51
52static DEFINE_MUTEX(nvmem_mutex);
53static DEFINE_IDA(nvmem_ida);
54
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -070055static DEFINE_MUTEX(nvmem_cell_mutex);
56static LIST_HEAD(nvmem_cell_tables);
57
Bartosz Golaszewski506157b2018-09-21 06:40:17 -070058static DEFINE_MUTEX(nvmem_lookup_mutex);
59static LIST_HEAD(nvmem_lookup_list);
60
Bartosz Golaszewskibee11382018-09-21 06:40:19 -070061static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
62
Andrew Lunnb6c217a2016-02-26 20:59:19 +010063#ifdef CONFIG_DEBUG_LOCK_ALLOC
64static struct lock_class_key eeprom_lock_key;
65#endif
66
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010067#define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +010068static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
69 void *val, size_t bytes)
70{
71 if (nvmem->reg_read)
72 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
73
74 return -EINVAL;
75}
76
77static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
78 void *val, size_t bytes)
79{
80 if (nvmem->reg_write)
81 return nvmem->reg_write(nvmem->priv, offset, val, bytes);
82
83 return -EINVAL;
84}
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010085
86static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
87 struct bin_attribute *attr,
88 char *buf, loff_t pos, size_t count)
89{
Andrew Lunnb6c217a2016-02-26 20:59:19 +010090 struct device *dev;
91 struct nvmem_device *nvmem;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010092 int rc;
93
Andrew Lunnb6c217a2016-02-26 20:59:19 +010094 if (attr->private)
95 dev = attr->private;
96 else
97 dev = container_of(kobj, struct device, kobj);
98 nvmem = to_nvmem_device(dev);
99
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100100 /* Stop the user from reading */
ZhengShunQian7c806882015-09-30 13:33:56 +0100101 if (pos >= nvmem->size)
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100102 return 0;
103
Srinivas Kandagatla313a72f2015-11-17 09:12:41 +0000104 if (count < nvmem->word_size)
105 return -EINVAL;
106
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100107 if (pos + count > nvmem->size)
108 count = nvmem->size - pos;
109
110 count = round_down(count, nvmem->word_size);
111
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +0100112 rc = nvmem_reg_read(nvmem, pos, buf, count);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100113
Arnd Bergmann287980e2016-05-27 23:23:25 +0200114 if (rc)
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100115 return rc;
116
117 return count;
118}
119
120static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
121 struct bin_attribute *attr,
122 char *buf, loff_t pos, size_t count)
123{
Andrew Lunnb6c217a2016-02-26 20:59:19 +0100124 struct device *dev;
125 struct nvmem_device *nvmem;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100126 int rc;
127
Andrew Lunnb6c217a2016-02-26 20:59:19 +0100128 if (attr->private)
129 dev = attr->private;
130 else
131 dev = container_of(kobj, struct device, kobj);
132 nvmem = to_nvmem_device(dev);
133
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100134 /* Stop the user from writing */
ZhengShunQian7c806882015-09-30 13:33:56 +0100135 if (pos >= nvmem->size)
Guy Shapiro38b07742017-09-11 11:00:11 +0200136 return -EFBIG;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100137
Srinivas Kandagatla313a72f2015-11-17 09:12:41 +0000138 if (count < nvmem->word_size)
139 return -EINVAL;
140
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100141 if (pos + count > nvmem->size)
142 count = nvmem->size - pos;
143
144 count = round_down(count, nvmem->word_size);
145
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +0100146 rc = nvmem_reg_write(nvmem, pos, buf, count);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100147
Arnd Bergmann287980e2016-05-27 23:23:25 +0200148 if (rc)
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100149 return rc;
150
151 return count;
152}
153
154/* default read/write permissions */
155static struct bin_attribute bin_attr_rw_nvmem = {
156 .attr = {
157 .name = "nvmem",
Bartosz Golaszewskie7e07f42018-09-21 06:40:24 -0700158 .mode = 0644,
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100159 },
160 .read = bin_attr_nvmem_read,
161 .write = bin_attr_nvmem_write,
162};
163
164static struct bin_attribute *nvmem_bin_rw_attributes[] = {
165 &bin_attr_rw_nvmem,
166 NULL,
167};
168
169static const struct attribute_group nvmem_bin_rw_group = {
170 .bin_attrs = nvmem_bin_rw_attributes,
171};
172
173static const struct attribute_group *nvmem_rw_dev_groups[] = {
174 &nvmem_bin_rw_group,
175 NULL,
176};
177
178/* read only permission */
179static struct bin_attribute bin_attr_ro_nvmem = {
180 .attr = {
181 .name = "nvmem",
Bartosz Golaszewskie7e07f42018-09-21 06:40:24 -0700182 .mode = 0444,
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100183 },
184 .read = bin_attr_nvmem_read,
185};
186
187static struct bin_attribute *nvmem_bin_ro_attributes[] = {
188 &bin_attr_ro_nvmem,
189 NULL,
190};
191
192static const struct attribute_group nvmem_bin_ro_group = {
193 .bin_attrs = nvmem_bin_ro_attributes,
194};
195
196static const struct attribute_group *nvmem_ro_dev_groups[] = {
197 &nvmem_bin_ro_group,
198 NULL,
199};
200
Andrew Lunn811b0d62016-02-26 20:59:18 +0100201/* default read/write permissions, root only */
202static struct bin_attribute bin_attr_rw_root_nvmem = {
203 .attr = {
204 .name = "nvmem",
Bartosz Golaszewskie7e07f42018-09-21 06:40:24 -0700205 .mode = 0600,
Andrew Lunn811b0d62016-02-26 20:59:18 +0100206 },
207 .read = bin_attr_nvmem_read,
208 .write = bin_attr_nvmem_write,
209};
210
211static struct bin_attribute *nvmem_bin_rw_root_attributes[] = {
212 &bin_attr_rw_root_nvmem,
213 NULL,
214};
215
216static const struct attribute_group nvmem_bin_rw_root_group = {
217 .bin_attrs = nvmem_bin_rw_root_attributes,
218};
219
220static const struct attribute_group *nvmem_rw_root_dev_groups[] = {
221 &nvmem_bin_rw_root_group,
222 NULL,
223};
224
225/* read only permission, root only */
226static struct bin_attribute bin_attr_ro_root_nvmem = {
227 .attr = {
228 .name = "nvmem",
Bartosz Golaszewskie7e07f42018-09-21 06:40:24 -0700229 .mode = 0400,
Andrew Lunn811b0d62016-02-26 20:59:18 +0100230 },
231 .read = bin_attr_nvmem_read,
232};
233
234static struct bin_attribute *nvmem_bin_ro_root_attributes[] = {
235 &bin_attr_ro_root_nvmem,
236 NULL,
237};
238
239static const struct attribute_group nvmem_bin_ro_root_group = {
240 .bin_attrs = nvmem_bin_ro_root_attributes,
241};
242
243static const struct attribute_group *nvmem_ro_root_dev_groups[] = {
244 &nvmem_bin_ro_root_group,
245 NULL,
246};
247
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100248static void nvmem_release(struct device *dev)
249{
250 struct nvmem_device *nvmem = to_nvmem_device(dev);
251
252 ida_simple_remove(&nvmem_ida, nvmem->id);
253 kfree(nvmem);
254}
255
256static const struct device_type nvmem_provider_type = {
257 .release = nvmem_release,
258};
259
260static struct bus_type nvmem_bus_type = {
261 .name = "nvmem",
262};
263
264static int of_nvmem_match(struct device *dev, void *nvmem_np)
265{
266 return dev->of_node == nvmem_np;
267}
268
269static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np)
270{
271 struct device *d;
272
273 if (!nvmem_np)
274 return NULL;
275
276 d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match);
277
278 if (!d)
279 return NULL;
280
281 return to_nvmem_device(d);
282}
283
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700284static struct nvmem_device *nvmem_find(const char *name)
285{
286 struct device *d;
287
288 d = bus_find_device_by_name(&nvmem_bus_type, NULL, name);
289
290 if (!d)
291 return NULL;
292
293 return to_nvmem_device(d);
294}
295
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100296static void nvmem_cell_drop(struct nvmem_cell *cell)
297{
Bartosz Golaszewskibee11382018-09-21 06:40:19 -0700298 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
Bartosz Golaszewskic7235ee2018-09-21 06:40:14 -0700299 mutex_lock(&nvmem_mutex);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100300 list_del(&cell->node);
Bartosz Golaszewskic7235ee2018-09-21 06:40:14 -0700301 mutex_unlock(&nvmem_mutex);
Srinivas Kandagatla0749aa22018-11-06 15:41:41 +0000302 of_node_put(cell->np);
Rob Herringbadcdff2018-10-03 18:47:04 +0100303 kfree(cell->name);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100304 kfree(cell);
305}
306
307static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
308{
Bartosz Golaszewski18521832018-09-21 06:40:05 -0700309 struct nvmem_cell *cell, *p;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100310
Bartosz Golaszewskic7235ee2018-09-21 06:40:14 -0700311 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
312 nvmem_cell_drop(cell);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100313}
314
315static void nvmem_cell_add(struct nvmem_cell *cell)
316{
Bartosz Golaszewskic7235ee2018-09-21 06:40:14 -0700317 mutex_lock(&nvmem_mutex);
318 list_add_tail(&cell->node, &cell->nvmem->cells);
319 mutex_unlock(&nvmem_mutex);
Bartosz Golaszewskibee11382018-09-21 06:40:19 -0700320 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100321}
322
323static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
324 const struct nvmem_cell_info *info,
325 struct nvmem_cell *cell)
326{
327 cell->nvmem = nvmem;
328 cell->offset = info->offset;
329 cell->bytes = info->bytes;
330 cell->name = info->name;
331
332 cell->bit_offset = info->bit_offset;
333 cell->nbits = info->nbits;
334
335 if (cell->nbits)
336 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
337 BITS_PER_BYTE);
338
339 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
340 dev_err(&nvmem->dev,
341 "cell %s unaligned to nvmem stride %d\n",
342 cell->name, nvmem->stride);
343 return -EINVAL;
344 }
345
346 return 0;
347}
348
Andrew Lunnb3db17e2018-05-11 12:06:56 +0100349/**
350 * nvmem_add_cells() - Add cell information to an nvmem device
351 *
352 * @nvmem: nvmem device to add cells to.
353 * @info: nvmem cell info to add to the device
354 * @ncells: number of cells in info
355 *
356 * Return: 0 or negative error code on failure.
357 */
Srinivas Kandagatlaef92ab32018-09-21 06:40:26 -0700358static int nvmem_add_cells(struct nvmem_device *nvmem,
Andrew Lunnb3db17e2018-05-11 12:06:56 +0100359 const struct nvmem_cell_info *info,
360 int ncells)
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100361{
362 struct nvmem_cell **cells;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100363 int i, rval;
364
Andrew Lunnb3db17e2018-05-11 12:06:56 +0100365 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100366 if (!cells)
367 return -ENOMEM;
368
Andrew Lunnb3db17e2018-05-11 12:06:56 +0100369 for (i = 0; i < ncells; i++) {
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100370 cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
371 if (!cells[i]) {
372 rval = -ENOMEM;
373 goto err;
374 }
375
376 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
Arnd Bergmann287980e2016-05-27 23:23:25 +0200377 if (rval) {
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100378 kfree(cells[i]);
379 goto err;
380 }
381
382 nvmem_cell_add(cells[i]);
383 }
384
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100385 /* remove tmp array */
386 kfree(cells);
387
388 return 0;
389err:
Rasmus Villemoesdfdf1412016-02-08 22:04:29 +0100390 while (i--)
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100391 nvmem_cell_drop(cells[i]);
392
Rasmus Villemoesdfdf1412016-02-08 22:04:29 +0100393 kfree(cells);
394
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100395 return rval;
396}
397
Andrew Lunnb6c217a2016-02-26 20:59:19 +0100398/*
399 * nvmem_setup_compat() - Create an additional binary entry in
400 * drivers sys directory, to be backwards compatible with the older
401 * drivers/misc/eeprom drivers.
402 */
403static int nvmem_setup_compat(struct nvmem_device *nvmem,
404 const struct nvmem_config *config)
405{
406 int rval;
407
408 if (!config->base_dev)
409 return -EINVAL;
410
411 if (nvmem->read_only)
412 nvmem->eeprom = bin_attr_ro_root_nvmem;
413 else
414 nvmem->eeprom = bin_attr_rw_root_nvmem;
415 nvmem->eeprom.attr.name = "eeprom";
416 nvmem->eeprom.size = nvmem->size;
417#ifdef CONFIG_DEBUG_LOCK_ALLOC
418 nvmem->eeprom.attr.key = &eeprom_lock_key;
419#endif
420 nvmem->eeprom.private = &nvmem->dev;
421 nvmem->base_dev = config->base_dev;
422
423 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
424 if (rval) {
425 dev_err(&nvmem->dev,
426 "Failed to create eeprom binary file %d\n", rval);
427 return rval;
428 }
429
430 nvmem->flags |= FLAG_COMPAT;
431
432 return 0;
433}
434
Bartosz Golaszewskibee11382018-09-21 06:40:19 -0700435/**
436 * nvmem_register_notifier() - Register a notifier block for nvmem events.
437 *
438 * @nb: notifier block to be called on nvmem events.
439 *
440 * Return: 0 on success, negative error number on failure.
441 */
442int nvmem_register_notifier(struct notifier_block *nb)
443{
444 return blocking_notifier_chain_register(&nvmem_notifier, nb);
445}
446EXPORT_SYMBOL_GPL(nvmem_register_notifier);
447
448/**
449 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
450 *
451 * @nb: notifier block to be unregistered.
452 *
453 * Return: 0 on success, negative error number on failure.
454 */
455int nvmem_unregister_notifier(struct notifier_block *nb)
456{
457 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
458}
459EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
460
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -0700461static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
462{
463 const struct nvmem_cell_info *info;
464 struct nvmem_cell_table *table;
465 struct nvmem_cell *cell;
466 int rval = 0, i;
467
468 mutex_lock(&nvmem_cell_mutex);
469 list_for_each_entry(table, &nvmem_cell_tables, node) {
470 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
471 for (i = 0; i < table->ncells; i++) {
472 info = &table->cells[i];
473
474 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
475 if (!cell) {
476 rval = -ENOMEM;
477 goto out;
478 }
479
480 rval = nvmem_cell_info_to_nvmem_cell(nvmem,
481 info,
482 cell);
483 if (rval) {
484 kfree(cell);
485 goto out;
486 }
487
488 nvmem_cell_add(cell);
489 }
490 }
491 }
492
493out:
494 mutex_unlock(&nvmem_cell_mutex);
495 return rval;
496}
497
Bartosz Golaszewskie888d442018-09-21 06:40:16 -0700498static struct nvmem_cell *
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700499nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
500{
501 struct nvmem_cell *cell = NULL;
502
503 mutex_lock(&nvmem_mutex);
504 list_for_each_entry(cell, &nvmem->cells, node) {
505 if (strcmp(cell_id, cell->name) == 0)
506 break;
507 }
508 mutex_unlock(&nvmem_mutex);
509
510 return cell;
511}
512
Bartosz Golaszewskie888d442018-09-21 06:40:16 -0700513static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
514{
515 struct device_node *parent, *child;
516 struct device *dev = &nvmem->dev;
517 struct nvmem_cell *cell;
518 const __be32 *addr;
519 int len;
520
521 parent = dev->of_node;
522
523 for_each_child_of_node(parent, child) {
524 addr = of_get_property(child, "reg", &len);
525 if (!addr || (len < 2 * sizeof(u32))) {
526 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
527 return -EINVAL;
528 }
529
530 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
531 if (!cell)
532 return -ENOMEM;
533
534 cell->nvmem = nvmem;
Srinivas Kandagatla0749aa22018-11-06 15:41:41 +0000535 cell->np = of_node_get(child);
Bartosz Golaszewskie888d442018-09-21 06:40:16 -0700536 cell->offset = be32_to_cpup(addr++);
537 cell->bytes = be32_to_cpup(addr);
Rob Herringbadcdff2018-10-03 18:47:04 +0100538 cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
Bartosz Golaszewskie888d442018-09-21 06:40:16 -0700539
540 addr = of_get_property(child, "bits", &len);
541 if (addr && len == (2 * sizeof(u32))) {
542 cell->bit_offset = be32_to_cpup(addr++);
543 cell->nbits = be32_to_cpup(addr);
544 }
545
546 if (cell->nbits)
547 cell->bytes = DIV_ROUND_UP(
548 cell->nbits + cell->bit_offset,
549 BITS_PER_BYTE);
550
551 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
552 dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
553 cell->name, nvmem->stride);
554 /* Cells already added will be freed later. */
Rob Herringbadcdff2018-10-03 18:47:04 +0100555 kfree(cell->name);
Bartosz Golaszewskie888d442018-09-21 06:40:16 -0700556 kfree(cell);
557 return -EINVAL;
558 }
559
560 nvmem_cell_add(cell);
561 }
562
563 return 0;
564}
565
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100566/**
567 * nvmem_register() - Register a nvmem device for given nvmem_config.
568 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
569 *
570 * @config: nvmem device configuration with which nvmem device is created.
571 *
572 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
573 * on success.
574 */
575
576struct nvmem_device *nvmem_register(const struct nvmem_config *config)
577{
578 struct nvmem_device *nvmem;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100579 int rval;
580
581 if (!config->dev)
582 return ERR_PTR(-EINVAL);
583
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100584 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
585 if (!nvmem)
586 return ERR_PTR(-ENOMEM);
587
588 rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
589 if (rval < 0) {
590 kfree(nvmem);
591 return ERR_PTR(rval);
592 }
593
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -0700594 kref_init(&nvmem->refcnt);
Bartosz Golaszewskic7235ee2018-09-21 06:40:14 -0700595 INIT_LIST_HEAD(&nvmem->cells);
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -0700596
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100597 nvmem->id = rval;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100598 nvmem->owner = config->owner;
Masahiro Yamada17eb18d2017-10-21 01:57:42 +0900599 if (!nvmem->owner && config->dev->driver)
600 nvmem->owner = config->dev->driver->owner;
Heiner Kallweit99897ef2017-12-15 14:06:05 +0000601 nvmem->stride = config->stride ?: 1;
602 nvmem->word_size = config->word_size ?: 1;
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +0100603 nvmem->size = config->size;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100604 nvmem->dev.type = &nvmem_provider_type;
605 nvmem->dev.bus = &nvmem_bus_type;
606 nvmem->dev.parent = config->dev;
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +0100607 nvmem->priv = config->priv;
608 nvmem->reg_read = config->reg_read;
609 nvmem->reg_write = config->reg_write;
Heiner Kallweitfc2f9972017-12-15 14:06:06 +0000610 nvmem->dev.of_node = config->dev->of_node;
Andrey Smirnovfd0f4902018-03-09 14:46:56 +0000611
612 if (config->id == -1 && config->name) {
613 dev_set_name(&nvmem->dev, "%s", config->name);
614 } else {
615 dev_set_name(&nvmem->dev, "%s%d",
616 config->name ? : "nvmem",
617 config->name ? config->id : nvmem->id);
618 }
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100619
Heiner Kallweitfc2f9972017-12-15 14:06:06 +0000620 nvmem->read_only = device_property_present(config->dev, "read-only") |
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100621 config->read_only;
622
Andrew Lunn811b0d62016-02-26 20:59:18 +0100623 if (config->root_only)
624 nvmem->dev.groups = nvmem->read_only ?
625 nvmem_ro_root_dev_groups :
626 nvmem_rw_root_dev_groups;
627 else
628 nvmem->dev.groups = nvmem->read_only ?
629 nvmem_ro_dev_groups :
630 nvmem_rw_dev_groups;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100631
632 device_initialize(&nvmem->dev);
633
634 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
635
636 rval = device_add(&nvmem->dev);
Andrew Lunnb6c217a2016-02-26 20:59:19 +0100637 if (rval)
Johan Hovold3360acd2017-06-09 10:59:07 +0100638 goto err_put_device;
Andrew Lunnb6c217a2016-02-26 20:59:19 +0100639
640 if (config->compat) {
641 rval = nvmem_setup_compat(nvmem, config);
642 if (rval)
Johan Hovold3360acd2017-06-09 10:59:07 +0100643 goto err_device_del;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100644 }
645
Bartosz Golaszewskifa72d842018-09-21 06:40:07 -0700646 if (config->cells) {
647 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
648 if (rval)
649 goto err_teardown_compat;
650 }
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100651
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -0700652 rval = nvmem_add_cells_from_table(nvmem);
653 if (rval)
654 goto err_remove_cells;
655
Bartosz Golaszewskie888d442018-09-21 06:40:16 -0700656 rval = nvmem_add_cells_from_of(nvmem);
657 if (rval)
658 goto err_remove_cells;
659
Bartosz Golaszewskibee11382018-09-21 06:40:19 -0700660 rval = blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
661 if (rval)
662 goto err_remove_cells;
663
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100664 return nvmem;
Johan Hovold3360acd2017-06-09 10:59:07 +0100665
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -0700666err_remove_cells:
667 nvmem_device_remove_all_cells(nvmem);
Bartosz Golaszewskifa72d842018-09-21 06:40:07 -0700668err_teardown_compat:
669 if (config->compat)
670 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
Johan Hovold3360acd2017-06-09 10:59:07 +0100671err_device_del:
672 device_del(&nvmem->dev);
673err_put_device:
674 put_device(&nvmem->dev);
675
Andrew Lunnb6c217a2016-02-26 20:59:19 +0100676 return ERR_PTR(rval);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100677}
678EXPORT_SYMBOL_GPL(nvmem_register);
679
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -0700680static void nvmem_device_release(struct kref *kref)
681{
682 struct nvmem_device *nvmem;
683
684 nvmem = container_of(kref, struct nvmem_device, refcnt);
685
Bartosz Golaszewskibee11382018-09-21 06:40:19 -0700686 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
687
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -0700688 if (nvmem->flags & FLAG_COMPAT)
689 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
690
691 nvmem_device_remove_all_cells(nvmem);
692 device_del(&nvmem->dev);
693 put_device(&nvmem->dev);
694}
695
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100696/**
697 * nvmem_unregister() - Unregister previously registered nvmem device
698 *
699 * @nvmem: Pointer to previously registered nvmem device.
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100700 */
Bartosz Golaszewskibf58e882018-09-21 06:40:13 -0700701void nvmem_unregister(struct nvmem_device *nvmem)
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100702{
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -0700703 kref_put(&nvmem->refcnt, nvmem_device_release);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100704}
705EXPORT_SYMBOL_GPL(nvmem_unregister);
706
Andrey Smirnovf1f50ec2018-03-09 14:46:57 +0000707static void devm_nvmem_release(struct device *dev, void *res)
708{
Bartosz Golaszewskibf58e882018-09-21 06:40:13 -0700709 nvmem_unregister(*(struct nvmem_device **)res);
Andrey Smirnovf1f50ec2018-03-09 14:46:57 +0000710}
711
712/**
713 * devm_nvmem_register() - Register a managed nvmem device for given
714 * nvmem_config.
715 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
716 *
Srinivas Kandagatlab378c772018-05-11 12:07:02 +0100717 * @dev: Device that uses the nvmem device.
Andrey Smirnovf1f50ec2018-03-09 14:46:57 +0000718 * @config: nvmem device configuration with which nvmem device is created.
719 *
720 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
721 * on success.
722 */
723struct nvmem_device *devm_nvmem_register(struct device *dev,
724 const struct nvmem_config *config)
725{
726 struct nvmem_device **ptr, *nvmem;
727
728 ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
729 if (!ptr)
730 return ERR_PTR(-ENOMEM);
731
732 nvmem = nvmem_register(config);
733
734 if (!IS_ERR(nvmem)) {
735 *ptr = nvmem;
736 devres_add(dev, ptr);
737 } else {
738 devres_free(ptr);
739 }
740
741 return nvmem;
742}
743EXPORT_SYMBOL_GPL(devm_nvmem_register);
744
745static int devm_nvmem_match(struct device *dev, void *res, void *data)
746{
747 struct nvmem_device **r = res;
748
749 return *r == data;
750}
751
752/**
753 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
754 * device.
755 *
Srinivas Kandagatlab378c772018-05-11 12:07:02 +0100756 * @dev: Device that uses the nvmem device.
Andrey Smirnovf1f50ec2018-03-09 14:46:57 +0000757 * @nvmem: Pointer to previously registered nvmem device.
758 *
759 * Return: Will be an negative on error or a zero on success.
760 */
761int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
762{
763 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
764}
765EXPORT_SYMBOL(devm_nvmem_unregister);
766
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100767static struct nvmem_device *__nvmem_device_get(struct device_node *np,
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700768 const char *nvmem_name)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100769{
770 struct nvmem_device *nvmem = NULL;
771
772 mutex_lock(&nvmem_mutex);
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700773 nvmem = np ? of_nvmem_find(np) : nvmem_find(nvmem_name);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100774 mutex_unlock(&nvmem_mutex);
Bartosz Golaszewskic7235ee2018-09-21 06:40:14 -0700775 if (!nvmem)
776 return ERR_PTR(-EPROBE_DEFER);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100777
778 if (!try_module_get(nvmem->owner)) {
779 dev_err(&nvmem->dev,
780 "could not increase module refcount for cell %s\n",
Bartosz Golaszewski5db652c2018-09-21 06:40:04 -0700781 nvmem_dev_name(nvmem));
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100782
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100783 return ERR_PTR(-EINVAL);
784 }
785
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -0700786 kref_get(&nvmem->refcnt);
787
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100788 return nvmem;
789}
790
791static void __nvmem_device_put(struct nvmem_device *nvmem)
792{
793 module_put(nvmem->owner);
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -0700794 kref_put(&nvmem->refcnt, nvmem_device_release);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100795}
796
Masahiro Yamadae701c672017-09-11 11:00:14 +0200797#if IS_ENABLED(CONFIG_OF)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +0100798/**
799 * of_nvmem_device_get() - Get nvmem device from a given id
800 *
Vivek Gautam29143262017-01-22 23:02:39 +0000801 * @np: Device tree node that uses the nvmem device.
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +0100802 * @id: nvmem name from nvmem-names property.
803 *
804 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
805 * on success.
806 */
807struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
808{
809
810 struct device_node *nvmem_np;
811 int index;
812
813 index = of_property_match_string(np, "nvmem-names", id);
814
815 nvmem_np = of_parse_phandle(np, "nvmem", index);
816 if (!nvmem_np)
817 return ERR_PTR(-EINVAL);
818
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700819 return __nvmem_device_get(nvmem_np, NULL);
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +0100820}
821EXPORT_SYMBOL_GPL(of_nvmem_device_get);
822#endif
823
824/**
825 * nvmem_device_get() - Get nvmem device from a given id
826 *
Vivek Gautam29143262017-01-22 23:02:39 +0000827 * @dev: Device that uses the nvmem device.
828 * @dev_name: name of the requested nvmem device.
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +0100829 *
830 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
831 * on success.
832 */
833struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
834{
835 if (dev->of_node) { /* try dt first */
836 struct nvmem_device *nvmem;
837
838 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
839
840 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
841 return nvmem;
842
843 }
844
845 return nvmem_find(dev_name);
846}
847EXPORT_SYMBOL_GPL(nvmem_device_get);
848
849static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
850{
851 struct nvmem_device **nvmem = res;
852
853 if (WARN_ON(!nvmem || !*nvmem))
854 return 0;
855
856 return *nvmem == data;
857}
858
859static void devm_nvmem_device_release(struct device *dev, void *res)
860{
861 nvmem_device_put(*(struct nvmem_device **)res);
862}
863
864/**
865 * devm_nvmem_device_put() - put alredy got nvmem device
866 *
Vivek Gautam29143262017-01-22 23:02:39 +0000867 * @dev: Device that uses the nvmem device.
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +0100868 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
869 * that needs to be released.
870 */
871void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
872{
873 int ret;
874
875 ret = devres_release(dev, devm_nvmem_device_release,
876 devm_nvmem_device_match, nvmem);
877
878 WARN_ON(ret);
879}
880EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
881
882/**
883 * nvmem_device_put() - put alredy got nvmem device
884 *
885 * @nvmem: pointer to nvmem device that needs to be released.
886 */
887void nvmem_device_put(struct nvmem_device *nvmem)
888{
889 __nvmem_device_put(nvmem);
890}
891EXPORT_SYMBOL_GPL(nvmem_device_put);
892
893/**
894 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
895 *
Vivek Gautam29143262017-01-22 23:02:39 +0000896 * @dev: Device that requests the nvmem device.
897 * @id: name id for the requested nvmem device.
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +0100898 *
899 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
900 * on success. The nvmem_cell will be freed by the automatically once the
901 * device is freed.
902 */
903struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
904{
905 struct nvmem_device **ptr, *nvmem;
906
907 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
908 if (!ptr)
909 return ERR_PTR(-ENOMEM);
910
911 nvmem = nvmem_device_get(dev, id);
912 if (!IS_ERR(nvmem)) {
913 *ptr = nvmem;
914 devres_add(dev, ptr);
915 } else {
916 devres_free(ptr);
917 }
918
919 return nvmem;
920}
921EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
922
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700923static struct nvmem_cell *
924nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100925{
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700926 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
927 struct nvmem_cell_lookup *lookup;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100928 struct nvmem_device *nvmem;
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700929 const char *dev_id;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100930
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700931 if (!dev)
932 return ERR_PTR(-EINVAL);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100933
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700934 dev_id = dev_name(dev);
935
936 mutex_lock(&nvmem_lookup_mutex);
937
938 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
939 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
940 (strcmp(lookup->con_id, con_id) == 0)) {
941 /* This is the right entry. */
942 nvmem = __nvmem_device_get(NULL, lookup->nvmem_name);
Bartosz Golaszewskicccb3b12018-10-03 09:31:11 +0200943 if (IS_ERR(nvmem)) {
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700944 /* Provider may not be registered yet. */
Bartosz Golaszewskicccb3b12018-10-03 09:31:11 +0200945 cell = ERR_CAST(nvmem);
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700946 goto out;
947 }
948
949 cell = nvmem_find_cell_by_name(nvmem,
950 lookup->cell_name);
951 if (!cell) {
952 __nvmem_device_put(nvmem);
Bartosz Golaszewskicccb3b12018-10-03 09:31:11 +0200953 cell = ERR_PTR(-ENOENT);
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700954 goto out;
955 }
956 }
957 }
958
959out:
960 mutex_unlock(&nvmem_lookup_mutex);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100961 return cell;
962}
963
Masahiro Yamadae701c672017-09-11 11:00:14 +0200964#if IS_ENABLED(CONFIG_OF)
Arnd Bergmann3c53e232018-10-02 23:11:12 +0200965static struct nvmem_cell *
Srinivas Kandagatla0749aa22018-11-06 15:41:41 +0000966nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
Arnd Bergmann3c53e232018-10-02 23:11:12 +0200967{
968 struct nvmem_cell *cell = NULL;
Arnd Bergmann3c53e232018-10-02 23:11:12 +0200969
970 mutex_lock(&nvmem_mutex);
971 list_for_each_entry(cell, &nvmem->cells, node) {
Srinivas Kandagatla0749aa22018-11-06 15:41:41 +0000972 if (np == cell->np)
Arnd Bergmann3c53e232018-10-02 23:11:12 +0200973 break;
974 }
975 mutex_unlock(&nvmem_mutex);
976
977 return cell;
978}
979
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100980/**
981 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
982 *
Vivek Gautam29143262017-01-22 23:02:39 +0000983 * @np: Device tree node that uses the nvmem cell.
Bartosz Golaszewski165589f2018-09-21 06:40:21 -0700984 * @id: nvmem cell name from nvmem-cell-names property, or NULL
985 * for the cell at index 0 (the lone cell with no accompanying
986 * nvmem-cell-names property).
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100987 *
988 * Return: Will be an ERR_PTR() on error or a valid pointer
989 * to a struct nvmem_cell. The nvmem_cell will be freed by the
990 * nvmem_cell_put().
991 */
Bartosz Golaszewski165589f2018-09-21 06:40:21 -0700992struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100993{
994 struct device_node *cell_np, *nvmem_np;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100995 struct nvmem_device *nvmem;
Bartosz Golaszewskie888d442018-09-21 06:40:16 -0700996 struct nvmem_cell *cell;
Vivek Gautamfd0c4782017-01-22 23:02:40 +0000997 int index = 0;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100998
Vivek Gautamfd0c4782017-01-22 23:02:40 +0000999 /* if cell name exists, find index to the name */
Bartosz Golaszewski165589f2018-09-21 06:40:21 -07001000 if (id)
1001 index = of_property_match_string(np, "nvmem-cell-names", id);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001002
1003 cell_np = of_parse_phandle(np, "nvmem-cells", index);
1004 if (!cell_np)
1005 return ERR_PTR(-EINVAL);
1006
1007 nvmem_np = of_get_next_parent(cell_np);
1008 if (!nvmem_np)
1009 return ERR_PTR(-EINVAL);
1010
Bartosz Golaszewski506157b2018-09-21 06:40:17 -07001011 nvmem = __nvmem_device_get(nvmem_np, NULL);
Masahiro Yamadaaad8d092017-09-11 11:00:12 +02001012 of_node_put(nvmem_np);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001013 if (IS_ERR(nvmem))
1014 return ERR_CAST(nvmem);
1015
Srinivas Kandagatla0749aa22018-11-06 15:41:41 +00001016 cell = nvmem_find_cell_by_node(nvmem, cell_np);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001017 if (!cell) {
Bartosz Golaszewskie888d442018-09-21 06:40:16 -07001018 __nvmem_device_put(nvmem);
1019 return ERR_PTR(-ENOENT);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001020 }
1021
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001022 return cell;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001023}
1024EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1025#endif
1026
1027/**
1028 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1029 *
Vivek Gautam29143262017-01-22 23:02:39 +00001030 * @dev: Device that requests the nvmem cell.
Bartosz Golaszewski165589f2018-09-21 06:40:21 -07001031 * @id: nvmem cell name to get (this corresponds with the name from the
1032 * nvmem-cell-names property for DT systems and with the con_id from
1033 * the lookup entry for non-DT systems).
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001034 *
1035 * Return: Will be an ERR_PTR() on error or a valid pointer
1036 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1037 * nvmem_cell_put().
1038 */
Bartosz Golaszewski165589f2018-09-21 06:40:21 -07001039struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001040{
1041 struct nvmem_cell *cell;
1042
1043 if (dev->of_node) { /* try dt first */
Bartosz Golaszewski165589f2018-09-21 06:40:21 -07001044 cell = of_nvmem_cell_get(dev->of_node, id);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001045 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1046 return cell;
1047 }
1048
Bartosz Golaszewski165589f2018-09-21 06:40:21 -07001049 /* NULL cell id only allowed for device tree; invalid otherwise */
1050 if (!id)
Douglas Anderson87ed1402018-06-18 18:30:43 +01001051 return ERR_PTR(-EINVAL);
1052
Bartosz Golaszewski165589f2018-09-21 06:40:21 -07001053 return nvmem_cell_get_from_lookup(dev, id);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001054}
1055EXPORT_SYMBOL_GPL(nvmem_cell_get);
1056
1057static void devm_nvmem_cell_release(struct device *dev, void *res)
1058{
1059 nvmem_cell_put(*(struct nvmem_cell **)res);
1060}
1061
1062/**
1063 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1064 *
Vivek Gautam29143262017-01-22 23:02:39 +00001065 * @dev: Device that requests the nvmem cell.
1066 * @id: nvmem cell name id to get.
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001067 *
1068 * Return: Will be an ERR_PTR() on error or a valid pointer
1069 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1070 * automatically once the device is freed.
1071 */
1072struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1073{
1074 struct nvmem_cell **ptr, *cell;
1075
1076 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1077 if (!ptr)
1078 return ERR_PTR(-ENOMEM);
1079
1080 cell = nvmem_cell_get(dev, id);
1081 if (!IS_ERR(cell)) {
1082 *ptr = cell;
1083 devres_add(dev, ptr);
1084 } else {
1085 devres_free(ptr);
1086 }
1087
1088 return cell;
1089}
1090EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1091
1092static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1093{
1094 struct nvmem_cell **c = res;
1095
1096 if (WARN_ON(!c || !*c))
1097 return 0;
1098
1099 return *c == data;
1100}
1101
1102/**
1103 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1104 * from devm_nvmem_cell_get.
1105 *
Vivek Gautam29143262017-01-22 23:02:39 +00001106 * @dev: Device that requests the nvmem cell.
1107 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001108 */
1109void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1110{
1111 int ret;
1112
1113 ret = devres_release(dev, devm_nvmem_cell_release,
1114 devm_nvmem_cell_match, cell);
1115
1116 WARN_ON(ret);
1117}
1118EXPORT_SYMBOL(devm_nvmem_cell_put);
1119
1120/**
1121 * nvmem_cell_put() - Release previously allocated nvmem cell.
1122 *
Vivek Gautam29143262017-01-22 23:02:39 +00001123 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001124 */
1125void nvmem_cell_put(struct nvmem_cell *cell)
1126{
1127 struct nvmem_device *nvmem = cell->nvmem;
1128
1129 __nvmem_device_put(nvmem);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001130}
1131EXPORT_SYMBOL_GPL(nvmem_cell_put);
1132
Masahiro Yamadaf7c04f12017-09-11 11:00:13 +02001133static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001134{
1135 u8 *p, *b;
1136 int i, bit_offset = cell->bit_offset;
1137
1138 p = b = buf;
1139 if (bit_offset) {
1140 /* First shift */
1141 *b++ >>= bit_offset;
1142
1143 /* setup rest of the bytes if any */
1144 for (i = 1; i < cell->bytes; i++) {
1145 /* Get bits from next byte and shift them towards msb */
1146 *p |= *b << (BITS_PER_BYTE - bit_offset);
1147
1148 p = b;
1149 *b++ >>= bit_offset;
1150 }
1151
1152 /* result fits in less bytes */
1153 if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE))
1154 *p-- = 0;
1155 }
1156 /* clear msb bits if any leftover in the last byte */
1157 *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
1158}
1159
1160static int __nvmem_cell_read(struct nvmem_device *nvmem,
1161 struct nvmem_cell *cell,
1162 void *buf, size_t *len)
1163{
1164 int rc;
1165
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001166 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001167
Arnd Bergmann287980e2016-05-27 23:23:25 +02001168 if (rc)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001169 return rc;
1170
1171 /* shift bits in-place */
Axel Lincbf854a2015-09-30 13:35:15 +01001172 if (cell->bit_offset || cell->nbits)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001173 nvmem_shift_read_buffer_in_place(cell, buf);
1174
Vivek Gautam3b4a6872017-01-22 23:02:38 +00001175 if (len)
1176 *len = cell->bytes;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001177
1178 return 0;
1179}
1180
1181/**
1182 * nvmem_cell_read() - Read a given nvmem cell
1183 *
1184 * @cell: nvmem cell to be read.
Vivek Gautam3b4a6872017-01-22 23:02:38 +00001185 * @len: pointer to length of cell which will be populated on successful read;
1186 * can be NULL.
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001187 *
Brian Norrisb577faf2017-01-04 16:18:11 +00001188 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1189 * buffer should be freed by the consumer with a kfree().
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001190 */
1191void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1192{
1193 struct nvmem_device *nvmem = cell->nvmem;
1194 u8 *buf;
1195 int rc;
1196
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001197 if (!nvmem)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001198 return ERR_PTR(-EINVAL);
1199
1200 buf = kzalloc(cell->bytes, GFP_KERNEL);
1201 if (!buf)
1202 return ERR_PTR(-ENOMEM);
1203
1204 rc = __nvmem_cell_read(nvmem, cell, buf, len);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001205 if (rc) {
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001206 kfree(buf);
1207 return ERR_PTR(rc);
1208 }
1209
1210 return buf;
1211}
1212EXPORT_SYMBOL_GPL(nvmem_cell_read);
1213
Masahiro Yamadaf7c04f12017-09-11 11:00:13 +02001214static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
1215 u8 *_buf, int len)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001216{
1217 struct nvmem_device *nvmem = cell->nvmem;
1218 int i, rc, nbits, bit_offset = cell->bit_offset;
1219 u8 v, *p, *buf, *b, pbyte, pbits;
1220
1221 nbits = cell->nbits;
1222 buf = kzalloc(cell->bytes, GFP_KERNEL);
1223 if (!buf)
1224 return ERR_PTR(-ENOMEM);
1225
1226 memcpy(buf, _buf, len);
1227 p = b = buf;
1228
1229 if (bit_offset) {
1230 pbyte = *b;
1231 *b <<= bit_offset;
1232
1233 /* setup the first byte with lsb bits from nvmem */
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001234 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
Mathieu Malaterre50808bf2018-05-11 12:07:03 +01001235 if (rc)
1236 goto err;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001237 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1238
1239 /* setup rest of the byte if any */
1240 for (i = 1; i < cell->bytes; i++) {
1241 /* Get last byte bits and shift them towards lsb */
1242 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1243 pbyte = *b;
1244 p = b;
1245 *b <<= bit_offset;
1246 *b++ |= pbits;
1247 }
1248 }
1249
1250 /* if it's not end on byte boundary */
1251 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1252 /* setup the last byte with msb bits from nvmem */
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001253 rc = nvmem_reg_read(nvmem,
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001254 cell->offset + cell->bytes - 1, &v, 1);
Mathieu Malaterre50808bf2018-05-11 12:07:03 +01001255 if (rc)
1256 goto err;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001257 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1258
1259 }
1260
1261 return buf;
Mathieu Malaterre50808bf2018-05-11 12:07:03 +01001262err:
1263 kfree(buf);
1264 return ERR_PTR(rc);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001265}
1266
1267/**
1268 * nvmem_cell_write() - Write to a given nvmem cell
1269 *
1270 * @cell: nvmem cell to be written.
1271 * @buf: Buffer to be written.
1272 * @len: length of buffer to be written to nvmem cell.
1273 *
1274 * Return: length of bytes written or negative on failure.
1275 */
1276int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1277{
1278 struct nvmem_device *nvmem = cell->nvmem;
1279 int rc;
1280
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001281 if (!nvmem || nvmem->read_only ||
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001282 (cell->bit_offset == 0 && len != cell->bytes))
1283 return -EINVAL;
1284
1285 if (cell->bit_offset || cell->nbits) {
1286 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1287 if (IS_ERR(buf))
1288 return PTR_ERR(buf);
1289 }
1290
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001291 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001292
1293 /* free the tmp buffer */
Axel Linace22172015-09-30 13:36:10 +01001294 if (cell->bit_offset || cell->nbits)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001295 kfree(buf);
1296
Arnd Bergmann287980e2016-05-27 23:23:25 +02001297 if (rc)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001298 return rc;
1299
1300 return len;
1301}
1302EXPORT_SYMBOL_GPL(nvmem_cell_write);
1303
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001304/**
Leonard Crestezd026d702017-07-26 11:34:46 +02001305 * nvmem_cell_read_u32() - Read a cell value as an u32
1306 *
1307 * @dev: Device that requests the nvmem cell.
1308 * @cell_id: Name of nvmem cell to read.
1309 * @val: pointer to output value.
1310 *
1311 * Return: 0 on success or negative errno.
1312 */
1313int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1314{
1315 struct nvmem_cell *cell;
1316 void *buf;
1317 size_t len;
1318
1319 cell = nvmem_cell_get(dev, cell_id);
1320 if (IS_ERR(cell))
1321 return PTR_ERR(cell);
1322
1323 buf = nvmem_cell_read(cell, &len);
1324 if (IS_ERR(buf)) {
1325 nvmem_cell_put(cell);
1326 return PTR_ERR(buf);
1327 }
1328 if (len != sizeof(*val)) {
1329 kfree(buf);
1330 nvmem_cell_put(cell);
1331 return -EINVAL;
1332 }
1333 memcpy(val, buf, sizeof(*val));
1334
1335 kfree(buf);
1336 nvmem_cell_put(cell);
1337 return 0;
1338}
1339EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1340
1341/**
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001342 * nvmem_device_cell_read() - Read a given nvmem device and cell
1343 *
1344 * @nvmem: nvmem device to read from.
1345 * @info: nvmem cell info to be read.
1346 * @buf: buffer pointer which will be populated on successful read.
1347 *
1348 * Return: length of successful bytes read on success and negative
1349 * error code on error.
1350 */
1351ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1352 struct nvmem_cell_info *info, void *buf)
1353{
1354 struct nvmem_cell cell;
1355 int rc;
1356 ssize_t len;
1357
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001358 if (!nvmem)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001359 return -EINVAL;
1360
1361 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001362 if (rc)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001363 return rc;
1364
1365 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001366 if (rc)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001367 return rc;
1368
1369 return len;
1370}
1371EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1372
1373/**
1374 * nvmem_device_cell_write() - Write cell to a given nvmem device
1375 *
1376 * @nvmem: nvmem device to be written to.
Vivek Gautam29143262017-01-22 23:02:39 +00001377 * @info: nvmem cell info to be written.
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001378 * @buf: buffer to be written to cell.
1379 *
1380 * Return: length of bytes written or negative error code on failure.
Bartosz Golaszewski48f63a22018-09-21 06:40:23 -07001381 */
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001382int nvmem_device_cell_write(struct nvmem_device *nvmem,
1383 struct nvmem_cell_info *info, void *buf)
1384{
1385 struct nvmem_cell cell;
1386 int rc;
1387
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001388 if (!nvmem)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001389 return -EINVAL;
1390
1391 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001392 if (rc)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001393 return rc;
1394
1395 return nvmem_cell_write(&cell, buf, cell.bytes);
1396}
1397EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1398
1399/**
1400 * nvmem_device_read() - Read from a given nvmem device
1401 *
1402 * @nvmem: nvmem device to read from.
1403 * @offset: offset in nvmem device.
1404 * @bytes: number of bytes to read.
1405 * @buf: buffer pointer which will be populated on successful read.
1406 *
1407 * Return: length of successful bytes read on success and negative
1408 * error code on error.
1409 */
1410int nvmem_device_read(struct nvmem_device *nvmem,
1411 unsigned int offset,
1412 size_t bytes, void *buf)
1413{
1414 int rc;
1415
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001416 if (!nvmem)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001417 return -EINVAL;
1418
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001419 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001420
Arnd Bergmann287980e2016-05-27 23:23:25 +02001421 if (rc)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001422 return rc;
1423
1424 return bytes;
1425}
1426EXPORT_SYMBOL_GPL(nvmem_device_read);
1427
1428/**
1429 * nvmem_device_write() - Write cell to a given nvmem device
1430 *
1431 * @nvmem: nvmem device to be written to.
1432 * @offset: offset in nvmem device.
1433 * @bytes: number of bytes to write.
1434 * @buf: buffer to be written.
1435 *
1436 * Return: length of bytes written or negative error code on failure.
Bartosz Golaszewski48f63a22018-09-21 06:40:23 -07001437 */
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001438int nvmem_device_write(struct nvmem_device *nvmem,
1439 unsigned int offset,
1440 size_t bytes, void *buf)
1441{
1442 int rc;
1443
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001444 if (!nvmem)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001445 return -EINVAL;
1446
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001447 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001448
Arnd Bergmann287980e2016-05-27 23:23:25 +02001449 if (rc)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001450 return rc;
1451
1452
1453 return bytes;
1454}
1455EXPORT_SYMBOL_GPL(nvmem_device_write);
1456
Bartosz Golaszewskid7b9fd12018-09-21 06:40:03 -07001457/**
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -07001458 * nvmem_add_cell_table() - register a table of cell info entries
1459 *
1460 * @table: table of cell info entries
1461 */
1462void nvmem_add_cell_table(struct nvmem_cell_table *table)
1463{
1464 mutex_lock(&nvmem_cell_mutex);
1465 list_add_tail(&table->node, &nvmem_cell_tables);
1466 mutex_unlock(&nvmem_cell_mutex);
1467}
1468EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1469
1470/**
1471 * nvmem_del_cell_table() - remove a previously registered cell info table
1472 *
1473 * @table: table of cell info entries
1474 */
1475void nvmem_del_cell_table(struct nvmem_cell_table *table)
1476{
1477 mutex_lock(&nvmem_cell_mutex);
1478 list_del(&table->node);
1479 mutex_unlock(&nvmem_cell_mutex);
1480}
1481EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1482
1483/**
Bartosz Golaszewski506157b2018-09-21 06:40:17 -07001484 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1485 *
1486 * @entries: array of cell lookup entries
1487 * @nentries: number of cell lookup entries in the array
1488 */
1489void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1490{
1491 int i;
1492
1493 mutex_lock(&nvmem_lookup_mutex);
1494 for (i = 0; i < nentries; i++)
1495 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1496 mutex_unlock(&nvmem_lookup_mutex);
1497}
1498EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1499
1500/**
1501 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1502 * entries
1503 *
1504 * @entries: array of cell lookup entries
1505 * @nentries: number of cell lookup entries in the array
1506 */
1507void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1508{
1509 int i;
1510
1511 mutex_lock(&nvmem_lookup_mutex);
1512 for (i = 0; i < nentries; i++)
1513 list_del(&entries[i].node);
1514 mutex_unlock(&nvmem_lookup_mutex);
1515}
1516EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1517
1518/**
Bartosz Golaszewskid7b9fd12018-09-21 06:40:03 -07001519 * nvmem_dev_name() - Get the name of a given nvmem device.
1520 *
1521 * @nvmem: nvmem device.
1522 *
1523 * Return: name of the nvmem device.
1524 */
1525const char *nvmem_dev_name(struct nvmem_device *nvmem)
1526{
1527 return dev_name(&nvmem->dev);
1528}
1529EXPORT_SYMBOL_GPL(nvmem_dev_name);
1530
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +01001531static int __init nvmem_init(void)
1532{
1533 return bus_register(&nvmem_bus_type);
1534}
1535
1536static void __exit nvmem_exit(void)
1537{
1538 bus_unregister(&nvmem_bus_type);
1539}
1540
1541subsys_initcall(nvmem_init);
1542module_exit(nvmem_exit);
1543
1544MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1545MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1546MODULE_DESCRIPTION("nvmem Driver Core");
1547MODULE_LICENSE("GPL v2");