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