blob: a2ad44104aa223f280fa76c57e2bdfeb24447558 [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;
Alexandre Belloni16688452018-11-30 11:53:20 +000031 enum nvmem_type type;
Andrew Lunnb6c217a2016-02-26 20:59:19 +010032 struct bin_attribute eeprom;
33 struct device *base_dev;
Bartosz Golaszewskic7235ee2018-09-21 06:40:14 -070034 struct list_head cells;
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +010035 nvmem_reg_read_t reg_read;
36 nvmem_reg_write_t reg_write;
37 void *priv;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010038};
39
Andrew Lunnb6c217a2016-02-26 20:59:19 +010040#define FLAG_COMPAT BIT(0)
41
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010042struct nvmem_cell {
43 const char *name;
44 int offset;
45 int bytes;
46 int bit_offset;
47 int nbits;
Srinivas Kandagatla0749aa22018-11-06 15:41:41 +000048 struct device_node *np;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010049 struct nvmem_device *nvmem;
50 struct list_head node;
51};
52
53static DEFINE_MUTEX(nvmem_mutex);
54static DEFINE_IDA(nvmem_ida);
55
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -070056static DEFINE_MUTEX(nvmem_cell_mutex);
57static LIST_HEAD(nvmem_cell_tables);
58
Bartosz Golaszewski506157b2018-09-21 06:40:17 -070059static DEFINE_MUTEX(nvmem_lookup_mutex);
60static LIST_HEAD(nvmem_lookup_list);
61
Bartosz Golaszewskibee11382018-09-21 06:40:19 -070062static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
63
Andy Shevchenkoa8b44d52018-11-30 11:53:24 +000064static const char * const nvmem_type_str[] = {
65 [NVMEM_TYPE_UNKNOWN] = "Unknown",
66 [NVMEM_TYPE_EEPROM] = "EEPROM",
67 [NVMEM_TYPE_OTP] = "OTP",
68 [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
69};
70
Andrew Lunnb6c217a2016-02-26 20:59:19 +010071#ifdef CONFIG_DEBUG_LOCK_ALLOC
72static struct lock_class_key eeprom_lock_key;
73#endif
74
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010075#define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +010076static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
77 void *val, size_t bytes)
78{
79 if (nvmem->reg_read)
80 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
81
82 return -EINVAL;
83}
84
85static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
86 void *val, size_t bytes)
87{
88 if (nvmem->reg_write)
89 return nvmem->reg_write(nvmem->priv, offset, val, bytes);
90
91 return -EINVAL;
92}
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +010093
Alexandre Belloni16688452018-11-30 11:53:20 +000094static ssize_t type_show(struct device *dev,
95 struct device_attribute *attr, char *buf)
96{
97 struct nvmem_device *nvmem = to_nvmem_device(dev);
98
99 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
100}
101
102static DEVICE_ATTR_RO(type);
103
104static struct attribute *nvmem_attrs[] = {
105 &dev_attr_type.attr,
106 NULL,
107};
108
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100109static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
110 struct bin_attribute *attr,
111 char *buf, loff_t pos, size_t count)
112{
Andrew Lunnb6c217a2016-02-26 20:59:19 +0100113 struct device *dev;
114 struct nvmem_device *nvmem;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100115 int rc;
116
Andrew Lunnb6c217a2016-02-26 20:59:19 +0100117 if (attr->private)
118 dev = attr->private;
119 else
120 dev = container_of(kobj, struct device, kobj);
121 nvmem = to_nvmem_device(dev);
122
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100123 /* Stop the user from reading */
ZhengShunQian7c806882015-09-30 13:33:56 +0100124 if (pos >= nvmem->size)
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100125 return 0;
126
Srinivas Kandagatla313a72f2015-11-17 09:12:41 +0000127 if (count < nvmem->word_size)
128 return -EINVAL;
129
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100130 if (pos + count > nvmem->size)
131 count = nvmem->size - pos;
132
133 count = round_down(count, nvmem->word_size);
134
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +0100135 rc = nvmem_reg_read(nvmem, pos, buf, count);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100136
Arnd Bergmann287980e2016-05-27 23:23:25 +0200137 if (rc)
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100138 return rc;
139
140 return count;
141}
142
143static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
144 struct bin_attribute *attr,
145 char *buf, loff_t pos, size_t count)
146{
Andrew Lunnb6c217a2016-02-26 20:59:19 +0100147 struct device *dev;
148 struct nvmem_device *nvmem;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100149 int rc;
150
Andrew Lunnb6c217a2016-02-26 20:59:19 +0100151 if (attr->private)
152 dev = attr->private;
153 else
154 dev = container_of(kobj, struct device, kobj);
155 nvmem = to_nvmem_device(dev);
156
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100157 /* Stop the user from writing */
ZhengShunQian7c806882015-09-30 13:33:56 +0100158 if (pos >= nvmem->size)
Guy Shapiro38b07742017-09-11 11:00:11 +0200159 return -EFBIG;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100160
Srinivas Kandagatla313a72f2015-11-17 09:12:41 +0000161 if (count < nvmem->word_size)
162 return -EINVAL;
163
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100164 if (pos + count > nvmem->size)
165 count = nvmem->size - pos;
166
167 count = round_down(count, nvmem->word_size);
168
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +0100169 rc = nvmem_reg_write(nvmem, pos, buf, count);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100170
Arnd Bergmann287980e2016-05-27 23:23:25 +0200171 if (rc)
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100172 return rc;
173
174 return count;
175}
176
177/* default read/write permissions */
178static struct bin_attribute bin_attr_rw_nvmem = {
179 .attr = {
180 .name = "nvmem",
Bartosz Golaszewskie7e07f42018-09-21 06:40:24 -0700181 .mode = 0644,
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100182 },
183 .read = bin_attr_nvmem_read,
184 .write = bin_attr_nvmem_write,
185};
186
187static struct bin_attribute *nvmem_bin_rw_attributes[] = {
188 &bin_attr_rw_nvmem,
189 NULL,
190};
191
192static const struct attribute_group nvmem_bin_rw_group = {
193 .bin_attrs = nvmem_bin_rw_attributes,
Alexandre Belloni16688452018-11-30 11:53:20 +0000194 .attrs = nvmem_attrs,
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100195};
196
197static const struct attribute_group *nvmem_rw_dev_groups[] = {
198 &nvmem_bin_rw_group,
199 NULL,
200};
201
202/* read only permission */
203static struct bin_attribute bin_attr_ro_nvmem = {
204 .attr = {
205 .name = "nvmem",
Bartosz Golaszewskie7e07f42018-09-21 06:40:24 -0700206 .mode = 0444,
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100207 },
208 .read = bin_attr_nvmem_read,
209};
210
211static struct bin_attribute *nvmem_bin_ro_attributes[] = {
212 &bin_attr_ro_nvmem,
213 NULL,
214};
215
216static const struct attribute_group nvmem_bin_ro_group = {
217 .bin_attrs = nvmem_bin_ro_attributes,
Alexandre Belloni16688452018-11-30 11:53:20 +0000218 .attrs = nvmem_attrs,
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100219};
220
221static const struct attribute_group *nvmem_ro_dev_groups[] = {
222 &nvmem_bin_ro_group,
223 NULL,
224};
225
Andrew Lunn811b0d62016-02-26 20:59:18 +0100226/* default read/write permissions, root only */
227static struct bin_attribute bin_attr_rw_root_nvmem = {
228 .attr = {
229 .name = "nvmem",
Bartosz Golaszewskie7e07f42018-09-21 06:40:24 -0700230 .mode = 0600,
Andrew Lunn811b0d62016-02-26 20:59:18 +0100231 },
232 .read = bin_attr_nvmem_read,
233 .write = bin_attr_nvmem_write,
234};
235
236static struct bin_attribute *nvmem_bin_rw_root_attributes[] = {
237 &bin_attr_rw_root_nvmem,
238 NULL,
239};
240
241static const struct attribute_group nvmem_bin_rw_root_group = {
242 .bin_attrs = nvmem_bin_rw_root_attributes,
Alexandre Belloni16688452018-11-30 11:53:20 +0000243 .attrs = nvmem_attrs,
Andrew Lunn811b0d62016-02-26 20:59:18 +0100244};
245
246static const struct attribute_group *nvmem_rw_root_dev_groups[] = {
247 &nvmem_bin_rw_root_group,
248 NULL,
249};
250
251/* read only permission, root only */
252static struct bin_attribute bin_attr_ro_root_nvmem = {
253 .attr = {
254 .name = "nvmem",
Bartosz Golaszewskie7e07f42018-09-21 06:40:24 -0700255 .mode = 0400,
Andrew Lunn811b0d62016-02-26 20:59:18 +0100256 },
257 .read = bin_attr_nvmem_read,
258};
259
260static struct bin_attribute *nvmem_bin_ro_root_attributes[] = {
261 &bin_attr_ro_root_nvmem,
262 NULL,
263};
264
265static const struct attribute_group nvmem_bin_ro_root_group = {
266 .bin_attrs = nvmem_bin_ro_root_attributes,
Alexandre Belloni16688452018-11-30 11:53:20 +0000267 .attrs = nvmem_attrs,
Andrew Lunn811b0d62016-02-26 20:59:18 +0100268};
269
270static const struct attribute_group *nvmem_ro_root_dev_groups[] = {
271 &nvmem_bin_ro_root_group,
272 NULL,
273};
274
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100275static void nvmem_release(struct device *dev)
276{
277 struct nvmem_device *nvmem = to_nvmem_device(dev);
278
279 ida_simple_remove(&nvmem_ida, nvmem->id);
280 kfree(nvmem);
281}
282
283static const struct device_type nvmem_provider_type = {
284 .release = nvmem_release,
285};
286
287static struct bus_type nvmem_bus_type = {
288 .name = "nvmem",
289};
290
291static int of_nvmem_match(struct device *dev, void *nvmem_np)
292{
293 return dev->of_node == nvmem_np;
294}
295
296static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np)
297{
298 struct device *d;
299
300 if (!nvmem_np)
301 return NULL;
302
303 d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match);
304
305 if (!d)
306 return NULL;
307
308 return to_nvmem_device(d);
309}
310
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700311static struct nvmem_device *nvmem_find(const char *name)
312{
313 struct device *d;
314
315 d = bus_find_device_by_name(&nvmem_bus_type, NULL, name);
316
317 if (!d)
318 return NULL;
319
320 return to_nvmem_device(d);
321}
322
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100323static void nvmem_cell_drop(struct nvmem_cell *cell)
324{
Bartosz Golaszewskibee11382018-09-21 06:40:19 -0700325 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
Bartosz Golaszewskic7235ee2018-09-21 06:40:14 -0700326 mutex_lock(&nvmem_mutex);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100327 list_del(&cell->node);
Bartosz Golaszewskic7235ee2018-09-21 06:40:14 -0700328 mutex_unlock(&nvmem_mutex);
Srinivas Kandagatla0749aa22018-11-06 15:41:41 +0000329 of_node_put(cell->np);
Rob Herringbadcdff2018-10-03 18:47:04 +0100330 kfree(cell->name);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100331 kfree(cell);
332}
333
334static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
335{
Bartosz Golaszewski18521832018-09-21 06:40:05 -0700336 struct nvmem_cell *cell, *p;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100337
Bartosz Golaszewskic7235ee2018-09-21 06:40:14 -0700338 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
339 nvmem_cell_drop(cell);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100340}
341
342static void nvmem_cell_add(struct nvmem_cell *cell)
343{
Bartosz Golaszewskic7235ee2018-09-21 06:40:14 -0700344 mutex_lock(&nvmem_mutex);
345 list_add_tail(&cell->node, &cell->nvmem->cells);
346 mutex_unlock(&nvmem_mutex);
Bartosz Golaszewskibee11382018-09-21 06:40:19 -0700347 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100348}
349
350static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
351 const struct nvmem_cell_info *info,
352 struct nvmem_cell *cell)
353{
354 cell->nvmem = nvmem;
355 cell->offset = info->offset;
356 cell->bytes = info->bytes;
357 cell->name = info->name;
358
359 cell->bit_offset = info->bit_offset;
360 cell->nbits = info->nbits;
361
362 if (cell->nbits)
363 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
364 BITS_PER_BYTE);
365
366 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
367 dev_err(&nvmem->dev,
368 "cell %s unaligned to nvmem stride %d\n",
369 cell->name, nvmem->stride);
370 return -EINVAL;
371 }
372
373 return 0;
374}
375
Andrew Lunnb3db17e2018-05-11 12:06:56 +0100376/**
377 * nvmem_add_cells() - Add cell information to an nvmem device
378 *
379 * @nvmem: nvmem device to add cells to.
380 * @info: nvmem cell info to add to the device
381 * @ncells: number of cells in info
382 *
383 * Return: 0 or negative error code on failure.
384 */
Srinivas Kandagatlaef92ab32018-09-21 06:40:26 -0700385static int nvmem_add_cells(struct nvmem_device *nvmem,
Andrew Lunnb3db17e2018-05-11 12:06:56 +0100386 const struct nvmem_cell_info *info,
387 int ncells)
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100388{
389 struct nvmem_cell **cells;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100390 int i, rval;
391
Andrew Lunnb3db17e2018-05-11 12:06:56 +0100392 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100393 if (!cells)
394 return -ENOMEM;
395
Andrew Lunnb3db17e2018-05-11 12:06:56 +0100396 for (i = 0; i < ncells; i++) {
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100397 cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
398 if (!cells[i]) {
399 rval = -ENOMEM;
400 goto err;
401 }
402
403 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
Arnd Bergmann287980e2016-05-27 23:23:25 +0200404 if (rval) {
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100405 kfree(cells[i]);
406 goto err;
407 }
408
409 nvmem_cell_add(cells[i]);
410 }
411
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100412 /* remove tmp array */
413 kfree(cells);
414
415 return 0;
416err:
Rasmus Villemoesdfdf1412016-02-08 22:04:29 +0100417 while (i--)
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100418 nvmem_cell_drop(cells[i]);
419
Rasmus Villemoesdfdf1412016-02-08 22:04:29 +0100420 kfree(cells);
421
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100422 return rval;
423}
424
Andrew Lunnb6c217a2016-02-26 20:59:19 +0100425/*
426 * nvmem_setup_compat() - Create an additional binary entry in
427 * drivers sys directory, to be backwards compatible with the older
428 * drivers/misc/eeprom drivers.
429 */
430static int nvmem_setup_compat(struct nvmem_device *nvmem,
431 const struct nvmem_config *config)
432{
433 int rval;
434
435 if (!config->base_dev)
436 return -EINVAL;
437
438 if (nvmem->read_only)
439 nvmem->eeprom = bin_attr_ro_root_nvmem;
440 else
441 nvmem->eeprom = bin_attr_rw_root_nvmem;
442 nvmem->eeprom.attr.name = "eeprom";
443 nvmem->eeprom.size = nvmem->size;
444#ifdef CONFIG_DEBUG_LOCK_ALLOC
445 nvmem->eeprom.attr.key = &eeprom_lock_key;
446#endif
447 nvmem->eeprom.private = &nvmem->dev;
448 nvmem->base_dev = config->base_dev;
449
450 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
451 if (rval) {
452 dev_err(&nvmem->dev,
453 "Failed to create eeprom binary file %d\n", rval);
454 return rval;
455 }
456
457 nvmem->flags |= FLAG_COMPAT;
458
459 return 0;
460}
461
Bartosz Golaszewskibee11382018-09-21 06:40:19 -0700462/**
463 * nvmem_register_notifier() - Register a notifier block for nvmem events.
464 *
465 * @nb: notifier block to be called on nvmem events.
466 *
467 * Return: 0 on success, negative error number on failure.
468 */
469int nvmem_register_notifier(struct notifier_block *nb)
470{
471 return blocking_notifier_chain_register(&nvmem_notifier, nb);
472}
473EXPORT_SYMBOL_GPL(nvmem_register_notifier);
474
475/**
476 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
477 *
478 * @nb: notifier block to be unregistered.
479 *
480 * Return: 0 on success, negative error number on failure.
481 */
482int nvmem_unregister_notifier(struct notifier_block *nb)
483{
484 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
485}
486EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
487
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -0700488static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
489{
490 const struct nvmem_cell_info *info;
491 struct nvmem_cell_table *table;
492 struct nvmem_cell *cell;
493 int rval = 0, i;
494
495 mutex_lock(&nvmem_cell_mutex);
496 list_for_each_entry(table, &nvmem_cell_tables, node) {
497 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
498 for (i = 0; i < table->ncells; i++) {
499 info = &table->cells[i];
500
501 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
502 if (!cell) {
503 rval = -ENOMEM;
504 goto out;
505 }
506
507 rval = nvmem_cell_info_to_nvmem_cell(nvmem,
508 info,
509 cell);
510 if (rval) {
511 kfree(cell);
512 goto out;
513 }
514
515 nvmem_cell_add(cell);
516 }
517 }
518 }
519
520out:
521 mutex_unlock(&nvmem_cell_mutex);
522 return rval;
523}
524
Bartosz Golaszewskie888d442018-09-21 06:40:16 -0700525static struct nvmem_cell *
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700526nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
527{
Alban Bedel1c832672019-01-28 15:55:02 +0000528 struct nvmem_cell *iter, *cell = NULL;
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700529
530 mutex_lock(&nvmem_mutex);
Alban Bedel1c832672019-01-28 15:55:02 +0000531 list_for_each_entry(iter, &nvmem->cells, node) {
532 if (strcmp(cell_id, iter->name) == 0) {
533 cell = iter;
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700534 break;
Alban Bedel1c832672019-01-28 15:55:02 +0000535 }
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700536 }
537 mutex_unlock(&nvmem_mutex);
538
539 return cell;
540}
541
Bartosz Golaszewskie888d442018-09-21 06:40:16 -0700542static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
543{
544 struct device_node *parent, *child;
545 struct device *dev = &nvmem->dev;
546 struct nvmem_cell *cell;
547 const __be32 *addr;
548 int len;
549
550 parent = dev->of_node;
551
552 for_each_child_of_node(parent, child) {
553 addr = of_get_property(child, "reg", &len);
554 if (!addr || (len < 2 * sizeof(u32))) {
555 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
556 return -EINVAL;
557 }
558
559 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
560 if (!cell)
561 return -ENOMEM;
562
563 cell->nvmem = nvmem;
Srinivas Kandagatla0749aa22018-11-06 15:41:41 +0000564 cell->np = of_node_get(child);
Bartosz Golaszewskie888d442018-09-21 06:40:16 -0700565 cell->offset = be32_to_cpup(addr++);
566 cell->bytes = be32_to_cpup(addr);
Rob Herringbadcdff2018-10-03 18:47:04 +0100567 cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
Bartosz Golaszewskie888d442018-09-21 06:40:16 -0700568
569 addr = of_get_property(child, "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(
577 cell->nbits + cell->bit_offset,
578 BITS_PER_BYTE);
579
580 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
581 dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
582 cell->name, nvmem->stride);
583 /* Cells already added will be freed later. */
Rob Herringbadcdff2018-10-03 18:47:04 +0100584 kfree(cell->name);
Bartosz Golaszewskie888d442018-09-21 06:40:16 -0700585 kfree(cell);
586 return -EINVAL;
587 }
588
589 nvmem_cell_add(cell);
590 }
591
592 return 0;
593}
594
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100595/**
596 * nvmem_register() - Register a nvmem device for given nvmem_config.
597 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
598 *
599 * @config: nvmem device configuration with which nvmem device is created.
600 *
601 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
602 * on success.
603 */
604
605struct nvmem_device *nvmem_register(const struct nvmem_config *config)
606{
607 struct nvmem_device *nvmem;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100608 int rval;
609
610 if (!config->dev)
611 return ERR_PTR(-EINVAL);
612
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100613 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
614 if (!nvmem)
615 return ERR_PTR(-ENOMEM);
616
617 rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
618 if (rval < 0) {
619 kfree(nvmem);
620 return ERR_PTR(rval);
621 }
622
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -0700623 kref_init(&nvmem->refcnt);
Bartosz Golaszewskic7235ee2018-09-21 06:40:14 -0700624 INIT_LIST_HEAD(&nvmem->cells);
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -0700625
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100626 nvmem->id = rval;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100627 nvmem->owner = config->owner;
Masahiro Yamada17eb18d2017-10-21 01:57:42 +0900628 if (!nvmem->owner && config->dev->driver)
629 nvmem->owner = config->dev->driver->owner;
Heiner Kallweit99897ef2017-12-15 14:06:05 +0000630 nvmem->stride = config->stride ?: 1;
631 nvmem->word_size = config->word_size ?: 1;
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +0100632 nvmem->size = config->size;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100633 nvmem->dev.type = &nvmem_provider_type;
634 nvmem->dev.bus = &nvmem_bus_type;
635 nvmem->dev.parent = config->dev;
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +0100636 nvmem->priv = config->priv;
Alexandre Belloni16688452018-11-30 11:53:20 +0000637 nvmem->type = config->type;
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +0100638 nvmem->reg_read = config->reg_read;
639 nvmem->reg_write = config->reg_write;
Bartosz Golaszewski517f14d2018-11-30 11:53:25 +0000640 if (!config->no_of_node)
641 nvmem->dev.of_node = config->dev->of_node;
Andrey Smirnovfd0f4902018-03-09 14:46:56 +0000642
643 if (config->id == -1 && config->name) {
644 dev_set_name(&nvmem->dev, "%s", config->name);
645 } else {
646 dev_set_name(&nvmem->dev, "%s%d",
647 config->name ? : "nvmem",
648 config->name ? config->id : nvmem->id);
649 }
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100650
Alban Bedel1716cfe2019-01-28 15:55:00 +0000651 nvmem->read_only = device_property_present(config->dev, "read-only") ||
652 config->read_only || !nvmem->reg_write;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100653
Andrew Lunn811b0d62016-02-26 20:59:18 +0100654 if (config->root_only)
655 nvmem->dev.groups = nvmem->read_only ?
656 nvmem_ro_root_dev_groups :
657 nvmem_rw_root_dev_groups;
658 else
659 nvmem->dev.groups = nvmem->read_only ?
660 nvmem_ro_dev_groups :
661 nvmem_rw_dev_groups;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100662
663 device_initialize(&nvmem->dev);
664
665 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
666
667 rval = device_add(&nvmem->dev);
Andrew Lunnb6c217a2016-02-26 20:59:19 +0100668 if (rval)
Johan Hovold3360acd2017-06-09 10:59:07 +0100669 goto err_put_device;
Andrew Lunnb6c217a2016-02-26 20:59:19 +0100670
671 if (config->compat) {
672 rval = nvmem_setup_compat(nvmem, config);
673 if (rval)
Johan Hovold3360acd2017-06-09 10:59:07 +0100674 goto err_device_del;
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100675 }
676
Bartosz Golaszewskifa72d842018-09-21 06:40:07 -0700677 if (config->cells) {
678 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
679 if (rval)
680 goto err_teardown_compat;
681 }
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100682
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -0700683 rval = nvmem_add_cells_from_table(nvmem);
684 if (rval)
685 goto err_remove_cells;
686
Bartosz Golaszewskie888d442018-09-21 06:40:16 -0700687 rval = nvmem_add_cells_from_of(nvmem);
688 if (rval)
689 goto err_remove_cells;
690
Bartosz Golaszewskibee11382018-09-21 06:40:19 -0700691 rval = blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
692 if (rval)
693 goto err_remove_cells;
694
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100695 return nvmem;
Johan Hovold3360acd2017-06-09 10:59:07 +0100696
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -0700697err_remove_cells:
698 nvmem_device_remove_all_cells(nvmem);
Bartosz Golaszewskifa72d842018-09-21 06:40:07 -0700699err_teardown_compat:
700 if (config->compat)
701 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
Johan Hovold3360acd2017-06-09 10:59:07 +0100702err_device_del:
703 device_del(&nvmem->dev);
704err_put_device:
705 put_device(&nvmem->dev);
706
Andrew Lunnb6c217a2016-02-26 20:59:19 +0100707 return ERR_PTR(rval);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100708}
709EXPORT_SYMBOL_GPL(nvmem_register);
710
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -0700711static void nvmem_device_release(struct kref *kref)
712{
713 struct nvmem_device *nvmem;
714
715 nvmem = container_of(kref, struct nvmem_device, refcnt);
716
Bartosz Golaszewskibee11382018-09-21 06:40:19 -0700717 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
718
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -0700719 if (nvmem->flags & FLAG_COMPAT)
720 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
721
722 nvmem_device_remove_all_cells(nvmem);
723 device_del(&nvmem->dev);
724 put_device(&nvmem->dev);
725}
726
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100727/**
728 * nvmem_unregister() - Unregister previously registered nvmem device
729 *
730 * @nvmem: Pointer to previously registered nvmem device.
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100731 */
Bartosz Golaszewskibf58e882018-09-21 06:40:13 -0700732void nvmem_unregister(struct nvmem_device *nvmem)
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100733{
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -0700734 kref_put(&nvmem->refcnt, nvmem_device_release);
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +0100735}
736EXPORT_SYMBOL_GPL(nvmem_unregister);
737
Andrey Smirnovf1f50ec2018-03-09 14:46:57 +0000738static void devm_nvmem_release(struct device *dev, void *res)
739{
Bartosz Golaszewskibf58e882018-09-21 06:40:13 -0700740 nvmem_unregister(*(struct nvmem_device **)res);
Andrey Smirnovf1f50ec2018-03-09 14:46:57 +0000741}
742
743/**
744 * devm_nvmem_register() - Register a managed nvmem device for given
745 * nvmem_config.
746 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
747 *
Srinivas Kandagatlab378c772018-05-11 12:07:02 +0100748 * @dev: Device that uses the nvmem device.
Andrey Smirnovf1f50ec2018-03-09 14:46:57 +0000749 * @config: nvmem device configuration with which nvmem device is created.
750 *
751 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
752 * on success.
753 */
754struct nvmem_device *devm_nvmem_register(struct device *dev,
755 const struct nvmem_config *config)
756{
757 struct nvmem_device **ptr, *nvmem;
758
759 ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
760 if (!ptr)
761 return ERR_PTR(-ENOMEM);
762
763 nvmem = nvmem_register(config);
764
765 if (!IS_ERR(nvmem)) {
766 *ptr = nvmem;
767 devres_add(dev, ptr);
768 } else {
769 devres_free(ptr);
770 }
771
772 return nvmem;
773}
774EXPORT_SYMBOL_GPL(devm_nvmem_register);
775
776static int devm_nvmem_match(struct device *dev, void *res, void *data)
777{
778 struct nvmem_device **r = res;
779
780 return *r == data;
781}
782
783/**
784 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
785 * device.
786 *
Srinivas Kandagatlab378c772018-05-11 12:07:02 +0100787 * @dev: Device that uses the nvmem device.
Andrey Smirnovf1f50ec2018-03-09 14:46:57 +0000788 * @nvmem: Pointer to previously registered nvmem device.
789 *
790 * Return: Will be an negative on error or a zero on success.
791 */
792int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
793{
794 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
795}
796EXPORT_SYMBOL(devm_nvmem_unregister);
797
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100798static struct nvmem_device *__nvmem_device_get(struct device_node *np,
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700799 const char *nvmem_name)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100800{
801 struct nvmem_device *nvmem = NULL;
802
803 mutex_lock(&nvmem_mutex);
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700804 nvmem = np ? of_nvmem_find(np) : nvmem_find(nvmem_name);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100805 mutex_unlock(&nvmem_mutex);
Bartosz Golaszewskic7235ee2018-09-21 06:40:14 -0700806 if (!nvmem)
807 return ERR_PTR(-EPROBE_DEFER);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100808
809 if (!try_module_get(nvmem->owner)) {
810 dev_err(&nvmem->dev,
811 "could not increase module refcount for cell %s\n",
Bartosz Golaszewski5db652c2018-09-21 06:40:04 -0700812 nvmem_dev_name(nvmem));
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100813
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100814 return ERR_PTR(-EINVAL);
815 }
816
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -0700817 kref_get(&nvmem->refcnt);
818
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100819 return nvmem;
820}
821
822static void __nvmem_device_put(struct nvmem_device *nvmem)
823{
824 module_put(nvmem->owner);
Bartosz Golaszewskic1de7f42018-09-21 06:40:08 -0700825 kref_put(&nvmem->refcnt, nvmem_device_release);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100826}
827
Masahiro Yamadae701c672017-09-11 11:00:14 +0200828#if IS_ENABLED(CONFIG_OF)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +0100829/**
830 * of_nvmem_device_get() - Get nvmem device from a given id
831 *
Vivek Gautam29143262017-01-22 23:02:39 +0000832 * @np: Device tree node that uses the nvmem device.
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +0100833 * @id: nvmem name from nvmem-names property.
834 *
835 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
836 * on success.
837 */
838struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
839{
840
841 struct device_node *nvmem_np;
842 int index;
843
844 index = of_property_match_string(np, "nvmem-names", id);
845
846 nvmem_np = of_parse_phandle(np, "nvmem", index);
847 if (!nvmem_np)
848 return ERR_PTR(-EINVAL);
849
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700850 return __nvmem_device_get(nvmem_np, NULL);
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +0100851}
852EXPORT_SYMBOL_GPL(of_nvmem_device_get);
853#endif
854
855/**
856 * nvmem_device_get() - Get nvmem device from a given id
857 *
Vivek Gautam29143262017-01-22 23:02:39 +0000858 * @dev: Device that uses the nvmem device.
859 * @dev_name: name of the requested nvmem device.
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +0100860 *
861 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
862 * on success.
863 */
864struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
865{
866 if (dev->of_node) { /* try dt first */
867 struct nvmem_device *nvmem;
868
869 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
870
871 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
872 return nvmem;
873
874 }
875
876 return nvmem_find(dev_name);
877}
878EXPORT_SYMBOL_GPL(nvmem_device_get);
879
880static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
881{
882 struct nvmem_device **nvmem = res;
883
884 if (WARN_ON(!nvmem || !*nvmem))
885 return 0;
886
887 return *nvmem == data;
888}
889
890static void devm_nvmem_device_release(struct device *dev, void *res)
891{
892 nvmem_device_put(*(struct nvmem_device **)res);
893}
894
895/**
896 * devm_nvmem_device_put() - put alredy got nvmem device
897 *
Vivek Gautam29143262017-01-22 23:02:39 +0000898 * @dev: Device that uses the nvmem device.
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +0100899 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
900 * that needs to be released.
901 */
902void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
903{
904 int ret;
905
906 ret = devres_release(dev, devm_nvmem_device_release,
907 devm_nvmem_device_match, nvmem);
908
909 WARN_ON(ret);
910}
911EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
912
913/**
914 * nvmem_device_put() - put alredy got nvmem device
915 *
916 * @nvmem: pointer to nvmem device that needs to be released.
917 */
918void nvmem_device_put(struct nvmem_device *nvmem)
919{
920 __nvmem_device_put(nvmem);
921}
922EXPORT_SYMBOL_GPL(nvmem_device_put);
923
924/**
925 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
926 *
Vivek Gautam29143262017-01-22 23:02:39 +0000927 * @dev: Device that requests the nvmem device.
928 * @id: name id for the requested nvmem device.
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +0100929 *
930 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
931 * on success. The nvmem_cell will be freed by the automatically once the
932 * device is freed.
933 */
934struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
935{
936 struct nvmem_device **ptr, *nvmem;
937
938 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
939 if (!ptr)
940 return ERR_PTR(-ENOMEM);
941
942 nvmem = nvmem_device_get(dev, id);
943 if (!IS_ERR(nvmem)) {
944 *ptr = nvmem;
945 devres_add(dev, ptr);
946 } else {
947 devres_free(ptr);
948 }
949
950 return nvmem;
951}
952EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
953
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700954static struct nvmem_cell *
955nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100956{
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700957 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
958 struct nvmem_cell_lookup *lookup;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100959 struct nvmem_device *nvmem;
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700960 const char *dev_id;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100961
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700962 if (!dev)
963 return ERR_PTR(-EINVAL);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100964
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700965 dev_id = dev_name(dev);
966
967 mutex_lock(&nvmem_lookup_mutex);
968
969 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
970 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
971 (strcmp(lookup->con_id, con_id) == 0)) {
972 /* This is the right entry. */
973 nvmem = __nvmem_device_get(NULL, lookup->nvmem_name);
Bartosz Golaszewskicccb3b12018-10-03 09:31:11 +0200974 if (IS_ERR(nvmem)) {
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700975 /* Provider may not be registered yet. */
Bartosz Golaszewskicccb3b12018-10-03 09:31:11 +0200976 cell = ERR_CAST(nvmem);
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700977 goto out;
978 }
979
980 cell = nvmem_find_cell_by_name(nvmem,
981 lookup->cell_name);
982 if (!cell) {
983 __nvmem_device_put(nvmem);
Bartosz Golaszewskicccb3b12018-10-03 09:31:11 +0200984 cell = ERR_PTR(-ENOENT);
Bartosz Golaszewski506157b2018-09-21 06:40:17 -0700985 goto out;
986 }
987 }
988 }
989
990out:
991 mutex_unlock(&nvmem_lookup_mutex);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +0100992 return cell;
993}
994
Masahiro Yamadae701c672017-09-11 11:00:14 +0200995#if IS_ENABLED(CONFIG_OF)
Arnd Bergmann3c53e232018-10-02 23:11:12 +0200996static struct nvmem_cell *
Srinivas Kandagatla0749aa22018-11-06 15:41:41 +0000997nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
Arnd Bergmann3c53e232018-10-02 23:11:12 +0200998{
Alban Bedel1c832672019-01-28 15:55:02 +0000999 struct nvmem_cell *iter, *cell = NULL;
Arnd Bergmann3c53e232018-10-02 23:11:12 +02001000
1001 mutex_lock(&nvmem_mutex);
Alban Bedel1c832672019-01-28 15:55:02 +00001002 list_for_each_entry(iter, &nvmem->cells, node) {
1003 if (np == iter->np) {
1004 cell = iter;
Arnd Bergmann3c53e232018-10-02 23:11:12 +02001005 break;
Alban Bedel1c832672019-01-28 15:55:02 +00001006 }
Arnd Bergmann3c53e232018-10-02 23:11:12 +02001007 }
1008 mutex_unlock(&nvmem_mutex);
1009
1010 return cell;
1011}
1012
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001013/**
1014 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1015 *
Vivek Gautam29143262017-01-22 23:02:39 +00001016 * @np: Device tree node that uses the nvmem cell.
Bartosz Golaszewski165589f2018-09-21 06:40:21 -07001017 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1018 * for the cell at index 0 (the lone cell with no accompanying
1019 * nvmem-cell-names property).
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001020 *
1021 * Return: Will be an ERR_PTR() on error or a valid pointer
1022 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1023 * nvmem_cell_put().
1024 */
Bartosz Golaszewski165589f2018-09-21 06:40:21 -07001025struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001026{
1027 struct device_node *cell_np, *nvmem_np;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001028 struct nvmem_device *nvmem;
Bartosz Golaszewskie888d442018-09-21 06:40:16 -07001029 struct nvmem_cell *cell;
Vivek Gautamfd0c4782017-01-22 23:02:40 +00001030 int index = 0;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001031
Vivek Gautamfd0c4782017-01-22 23:02:40 +00001032 /* if cell name exists, find index to the name */
Bartosz Golaszewski165589f2018-09-21 06:40:21 -07001033 if (id)
1034 index = of_property_match_string(np, "nvmem-cell-names", id);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001035
1036 cell_np = of_parse_phandle(np, "nvmem-cells", index);
1037 if (!cell_np)
Alban Bedel5087cc12019-01-28 15:55:01 +00001038 return ERR_PTR(-ENOENT);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001039
1040 nvmem_np = of_get_next_parent(cell_np);
1041 if (!nvmem_np)
1042 return ERR_PTR(-EINVAL);
1043
Bartosz Golaszewski506157b2018-09-21 06:40:17 -07001044 nvmem = __nvmem_device_get(nvmem_np, NULL);
Masahiro Yamadaaad8d092017-09-11 11:00:12 +02001045 of_node_put(nvmem_np);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001046 if (IS_ERR(nvmem))
1047 return ERR_CAST(nvmem);
1048
Srinivas Kandagatla0749aa22018-11-06 15:41:41 +00001049 cell = nvmem_find_cell_by_node(nvmem, cell_np);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001050 if (!cell) {
Bartosz Golaszewskie888d442018-09-21 06:40:16 -07001051 __nvmem_device_put(nvmem);
1052 return ERR_PTR(-ENOENT);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001053 }
1054
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001055 return cell;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001056}
1057EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1058#endif
1059
1060/**
1061 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1062 *
Vivek Gautam29143262017-01-22 23:02:39 +00001063 * @dev: Device that requests the nvmem cell.
Bartosz Golaszewski165589f2018-09-21 06:40:21 -07001064 * @id: nvmem cell name to get (this corresponds with the name from the
1065 * nvmem-cell-names property for DT systems and with the con_id from
1066 * the lookup entry for non-DT systems).
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 * nvmem_cell_put().
1071 */
Bartosz Golaszewski165589f2018-09-21 06:40:21 -07001072struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001073{
1074 struct nvmem_cell *cell;
1075
1076 if (dev->of_node) { /* try dt first */
Bartosz Golaszewski165589f2018-09-21 06:40:21 -07001077 cell = of_nvmem_cell_get(dev->of_node, id);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001078 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1079 return cell;
1080 }
1081
Bartosz Golaszewski165589f2018-09-21 06:40:21 -07001082 /* NULL cell id only allowed for device tree; invalid otherwise */
1083 if (!id)
Douglas Anderson87ed1402018-06-18 18:30:43 +01001084 return ERR_PTR(-EINVAL);
1085
Bartosz Golaszewski165589f2018-09-21 06:40:21 -07001086 return nvmem_cell_get_from_lookup(dev, id);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001087}
1088EXPORT_SYMBOL_GPL(nvmem_cell_get);
1089
1090static void devm_nvmem_cell_release(struct device *dev, void *res)
1091{
1092 nvmem_cell_put(*(struct nvmem_cell **)res);
1093}
1094
1095/**
1096 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1097 *
Vivek Gautam29143262017-01-22 23:02:39 +00001098 * @dev: Device that requests the nvmem cell.
1099 * @id: nvmem cell name id to get.
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001100 *
1101 * Return: Will be an ERR_PTR() on error or a valid pointer
1102 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1103 * automatically once the device is freed.
1104 */
1105struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1106{
1107 struct nvmem_cell **ptr, *cell;
1108
1109 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1110 if (!ptr)
1111 return ERR_PTR(-ENOMEM);
1112
1113 cell = nvmem_cell_get(dev, id);
1114 if (!IS_ERR(cell)) {
1115 *ptr = cell;
1116 devres_add(dev, ptr);
1117 } else {
1118 devres_free(ptr);
1119 }
1120
1121 return cell;
1122}
1123EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1124
1125static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1126{
1127 struct nvmem_cell **c = res;
1128
1129 if (WARN_ON(!c || !*c))
1130 return 0;
1131
1132 return *c == data;
1133}
1134
1135/**
1136 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1137 * from devm_nvmem_cell_get.
1138 *
Vivek Gautam29143262017-01-22 23:02:39 +00001139 * @dev: Device that requests the nvmem cell.
1140 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001141 */
1142void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1143{
1144 int ret;
1145
1146 ret = devres_release(dev, devm_nvmem_cell_release,
1147 devm_nvmem_cell_match, cell);
1148
1149 WARN_ON(ret);
1150}
1151EXPORT_SYMBOL(devm_nvmem_cell_put);
1152
1153/**
1154 * nvmem_cell_put() - Release previously allocated nvmem cell.
1155 *
Vivek Gautam29143262017-01-22 23:02:39 +00001156 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001157 */
1158void nvmem_cell_put(struct nvmem_cell *cell)
1159{
1160 struct nvmem_device *nvmem = cell->nvmem;
1161
1162 __nvmem_device_put(nvmem);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001163}
1164EXPORT_SYMBOL_GPL(nvmem_cell_put);
1165
Masahiro Yamadaf7c04f12017-09-11 11:00:13 +02001166static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001167{
1168 u8 *p, *b;
1169 int i, bit_offset = cell->bit_offset;
1170
1171 p = b = buf;
1172 if (bit_offset) {
1173 /* First shift */
1174 *b++ >>= bit_offset;
1175
1176 /* setup rest of the bytes if any */
1177 for (i = 1; i < cell->bytes; i++) {
1178 /* Get bits from next byte and shift them towards msb */
1179 *p |= *b << (BITS_PER_BYTE - bit_offset);
1180
1181 p = b;
1182 *b++ >>= bit_offset;
1183 }
1184
1185 /* result fits in less bytes */
1186 if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE))
1187 *p-- = 0;
1188 }
1189 /* clear msb bits if any leftover in the last byte */
1190 *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
1191}
1192
1193static int __nvmem_cell_read(struct nvmem_device *nvmem,
1194 struct nvmem_cell *cell,
1195 void *buf, size_t *len)
1196{
1197 int rc;
1198
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001199 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001200
Arnd Bergmann287980e2016-05-27 23:23:25 +02001201 if (rc)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001202 return rc;
1203
1204 /* shift bits in-place */
Axel Lincbf854a2015-09-30 13:35:15 +01001205 if (cell->bit_offset || cell->nbits)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001206 nvmem_shift_read_buffer_in_place(cell, buf);
1207
Vivek Gautam3b4a6872017-01-22 23:02:38 +00001208 if (len)
1209 *len = cell->bytes;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001210
1211 return 0;
1212}
1213
1214/**
1215 * nvmem_cell_read() - Read a given nvmem cell
1216 *
1217 * @cell: nvmem cell to be read.
Vivek Gautam3b4a6872017-01-22 23:02:38 +00001218 * @len: pointer to length of cell which will be populated on successful read;
1219 * can be NULL.
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001220 *
Brian Norrisb577faf2017-01-04 16:18:11 +00001221 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1222 * buffer should be freed by the consumer with a kfree().
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001223 */
1224void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1225{
1226 struct nvmem_device *nvmem = cell->nvmem;
1227 u8 *buf;
1228 int rc;
1229
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001230 if (!nvmem)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001231 return ERR_PTR(-EINVAL);
1232
1233 buf = kzalloc(cell->bytes, GFP_KERNEL);
1234 if (!buf)
1235 return ERR_PTR(-ENOMEM);
1236
1237 rc = __nvmem_cell_read(nvmem, cell, buf, len);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001238 if (rc) {
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001239 kfree(buf);
1240 return ERR_PTR(rc);
1241 }
1242
1243 return buf;
1244}
1245EXPORT_SYMBOL_GPL(nvmem_cell_read);
1246
Masahiro Yamadaf7c04f12017-09-11 11:00:13 +02001247static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
1248 u8 *_buf, int len)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001249{
1250 struct nvmem_device *nvmem = cell->nvmem;
1251 int i, rc, nbits, bit_offset = cell->bit_offset;
1252 u8 v, *p, *buf, *b, pbyte, pbits;
1253
1254 nbits = cell->nbits;
1255 buf = kzalloc(cell->bytes, GFP_KERNEL);
1256 if (!buf)
1257 return ERR_PTR(-ENOMEM);
1258
1259 memcpy(buf, _buf, len);
1260 p = b = buf;
1261
1262 if (bit_offset) {
1263 pbyte = *b;
1264 *b <<= bit_offset;
1265
1266 /* setup the first byte with lsb bits from nvmem */
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001267 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
Mathieu Malaterre50808bf2018-05-11 12:07:03 +01001268 if (rc)
1269 goto err;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001270 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1271
1272 /* setup rest of the byte if any */
1273 for (i = 1; i < cell->bytes; i++) {
1274 /* Get last byte bits and shift them towards lsb */
1275 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1276 pbyte = *b;
1277 p = b;
1278 *b <<= bit_offset;
1279 *b++ |= pbits;
1280 }
1281 }
1282
1283 /* if it's not end on byte boundary */
1284 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1285 /* setup the last byte with msb bits from nvmem */
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001286 rc = nvmem_reg_read(nvmem,
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001287 cell->offset + cell->bytes - 1, &v, 1);
Mathieu Malaterre50808bf2018-05-11 12:07:03 +01001288 if (rc)
1289 goto err;
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001290 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1291
1292 }
1293
1294 return buf;
Mathieu Malaterre50808bf2018-05-11 12:07:03 +01001295err:
1296 kfree(buf);
1297 return ERR_PTR(rc);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001298}
1299
1300/**
1301 * nvmem_cell_write() - Write to a given nvmem cell
1302 *
1303 * @cell: nvmem cell to be written.
1304 * @buf: Buffer to be written.
1305 * @len: length of buffer to be written to nvmem cell.
1306 *
1307 * Return: length of bytes written or negative on failure.
1308 */
1309int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1310{
1311 struct nvmem_device *nvmem = cell->nvmem;
1312 int rc;
1313
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001314 if (!nvmem || nvmem->read_only ||
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001315 (cell->bit_offset == 0 && len != cell->bytes))
1316 return -EINVAL;
1317
1318 if (cell->bit_offset || cell->nbits) {
1319 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1320 if (IS_ERR(buf))
1321 return PTR_ERR(buf);
1322 }
1323
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001324 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001325
1326 /* free the tmp buffer */
Axel Linace22172015-09-30 13:36:10 +01001327 if (cell->bit_offset || cell->nbits)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001328 kfree(buf);
1329
Arnd Bergmann287980e2016-05-27 23:23:25 +02001330 if (rc)
Srinivas Kandagatla69aba792015-07-27 12:13:34 +01001331 return rc;
1332
1333 return len;
1334}
1335EXPORT_SYMBOL_GPL(nvmem_cell_write);
1336
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001337/**
Leonard Crestezd026d702017-07-26 11:34:46 +02001338 * nvmem_cell_read_u32() - Read a cell value as an u32
1339 *
1340 * @dev: Device that requests the nvmem cell.
1341 * @cell_id: Name of nvmem cell to read.
1342 * @val: pointer to output value.
1343 *
1344 * Return: 0 on success or negative errno.
1345 */
1346int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1347{
1348 struct nvmem_cell *cell;
1349 void *buf;
1350 size_t len;
1351
1352 cell = nvmem_cell_get(dev, cell_id);
1353 if (IS_ERR(cell))
1354 return PTR_ERR(cell);
1355
1356 buf = nvmem_cell_read(cell, &len);
1357 if (IS_ERR(buf)) {
1358 nvmem_cell_put(cell);
1359 return PTR_ERR(buf);
1360 }
1361 if (len != sizeof(*val)) {
1362 kfree(buf);
1363 nvmem_cell_put(cell);
1364 return -EINVAL;
1365 }
1366 memcpy(val, buf, sizeof(*val));
1367
1368 kfree(buf);
1369 nvmem_cell_put(cell);
1370 return 0;
1371}
1372EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1373
1374/**
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001375 * nvmem_device_cell_read() - Read a given nvmem device and cell
1376 *
1377 * @nvmem: nvmem device to read from.
1378 * @info: nvmem cell info to be read.
1379 * @buf: buffer pointer which will be populated on successful read.
1380 *
1381 * Return: length of successful bytes read on success and negative
1382 * error code on error.
1383 */
1384ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1385 struct nvmem_cell_info *info, void *buf)
1386{
1387 struct nvmem_cell cell;
1388 int rc;
1389 ssize_t len;
1390
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001391 if (!nvmem)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001392 return -EINVAL;
1393
1394 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001395 if (rc)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001396 return rc;
1397
1398 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001399 if (rc)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001400 return rc;
1401
1402 return len;
1403}
1404EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1405
1406/**
1407 * nvmem_device_cell_write() - Write cell to a given nvmem device
1408 *
1409 * @nvmem: nvmem device to be written to.
Vivek Gautam29143262017-01-22 23:02:39 +00001410 * @info: nvmem cell info to be written.
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001411 * @buf: buffer to be written to cell.
1412 *
1413 * Return: length of bytes written or negative error code on failure.
Bartosz Golaszewski48f63a22018-09-21 06:40:23 -07001414 */
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001415int nvmem_device_cell_write(struct nvmem_device *nvmem,
1416 struct nvmem_cell_info *info, void *buf)
1417{
1418 struct nvmem_cell cell;
1419 int rc;
1420
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001421 if (!nvmem)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001422 return -EINVAL;
1423
1424 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001425 if (rc)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001426 return rc;
1427
1428 return nvmem_cell_write(&cell, buf, cell.bytes);
1429}
1430EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1431
1432/**
1433 * nvmem_device_read() - Read from a given nvmem device
1434 *
1435 * @nvmem: nvmem device to read from.
1436 * @offset: offset in nvmem device.
1437 * @bytes: number of bytes to read.
1438 * @buf: buffer pointer which will be populated on successful read.
1439 *
1440 * Return: length of successful bytes read on success and negative
1441 * error code on error.
1442 */
1443int nvmem_device_read(struct nvmem_device *nvmem,
1444 unsigned int offset,
1445 size_t bytes, void *buf)
1446{
1447 int rc;
1448
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001449 if (!nvmem)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001450 return -EINVAL;
1451
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001452 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001453
Arnd Bergmann287980e2016-05-27 23:23:25 +02001454 if (rc)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001455 return rc;
1456
1457 return bytes;
1458}
1459EXPORT_SYMBOL_GPL(nvmem_device_read);
1460
1461/**
1462 * nvmem_device_write() - Write cell to a given nvmem device
1463 *
1464 * @nvmem: nvmem device to be written to.
1465 * @offset: offset in nvmem device.
1466 * @bytes: number of bytes to write.
1467 * @buf: buffer to be written.
1468 *
1469 * Return: length of bytes written or negative error code on failure.
Bartosz Golaszewski48f63a22018-09-21 06:40:23 -07001470 */
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001471int nvmem_device_write(struct nvmem_device *nvmem,
1472 unsigned int offset,
1473 size_t bytes, void *buf)
1474{
1475 int rc;
1476
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001477 if (!nvmem)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001478 return -EINVAL;
1479
Srinivas Kandagatla795ddd12016-04-24 20:28:05 +01001480 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001481
Arnd Bergmann287980e2016-05-27 23:23:25 +02001482 if (rc)
Srinivas Kandagatlae2a54022015-07-27 12:13:45 +01001483 return rc;
1484
1485
1486 return bytes;
1487}
1488EXPORT_SYMBOL_GPL(nvmem_device_write);
1489
Bartosz Golaszewskid7b9fd12018-09-21 06:40:03 -07001490/**
Bartosz Golaszewskib985f4c2018-09-21 06:40:15 -07001491 * nvmem_add_cell_table() - register a table of cell info entries
1492 *
1493 * @table: table of cell info entries
1494 */
1495void nvmem_add_cell_table(struct nvmem_cell_table *table)
1496{
1497 mutex_lock(&nvmem_cell_mutex);
1498 list_add_tail(&table->node, &nvmem_cell_tables);
1499 mutex_unlock(&nvmem_cell_mutex);
1500}
1501EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1502
1503/**
1504 * nvmem_del_cell_table() - remove a previously registered cell info table
1505 *
1506 * @table: table of cell info entries
1507 */
1508void nvmem_del_cell_table(struct nvmem_cell_table *table)
1509{
1510 mutex_lock(&nvmem_cell_mutex);
1511 list_del(&table->node);
1512 mutex_unlock(&nvmem_cell_mutex);
1513}
1514EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1515
1516/**
Bartosz Golaszewski506157b2018-09-21 06:40:17 -07001517 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1518 *
1519 * @entries: array of cell lookup entries
1520 * @nentries: number of cell lookup entries in the array
1521 */
1522void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1523{
1524 int i;
1525
1526 mutex_lock(&nvmem_lookup_mutex);
1527 for (i = 0; i < nentries; i++)
1528 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1529 mutex_unlock(&nvmem_lookup_mutex);
1530}
1531EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1532
1533/**
1534 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1535 * entries
1536 *
1537 * @entries: array of cell lookup entries
1538 * @nentries: number of cell lookup entries in the array
1539 */
1540void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1541{
1542 int i;
1543
1544 mutex_lock(&nvmem_lookup_mutex);
1545 for (i = 0; i < nentries; i++)
1546 list_del(&entries[i].node);
1547 mutex_unlock(&nvmem_lookup_mutex);
1548}
1549EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1550
1551/**
Bartosz Golaszewskid7b9fd12018-09-21 06:40:03 -07001552 * nvmem_dev_name() - Get the name of a given nvmem device.
1553 *
1554 * @nvmem: nvmem device.
1555 *
1556 * Return: name of the nvmem device.
1557 */
1558const char *nvmem_dev_name(struct nvmem_device *nvmem)
1559{
1560 return dev_name(&nvmem->dev);
1561}
1562EXPORT_SYMBOL_GPL(nvmem_dev_name);
1563
Srinivas Kandagatlaeace75c2015-07-27 12:13:19 +01001564static int __init nvmem_init(void)
1565{
1566 return bus_register(&nvmem_bus_type);
1567}
1568
1569static void __exit nvmem_exit(void)
1570{
1571 bus_unregister(&nvmem_bus_type);
1572}
1573
1574subsys_initcall(nvmem_init);
1575module_exit(nvmem_exit);
1576
1577MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1578MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1579MODULE_DESCRIPTION("nvmem Driver Core");
1580MODULE_LICENSE("GPL v2");