]>
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; | |
37 | nvmem_reg_read_t reg_read; | |
38 | nvmem_reg_write_t reg_write; | |
39 | struct gpio_desc *wp_gpio; | |
40 | void *priv; | |
41 | }; | |
42 | ||
43 | #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev) | |
44 | ||
45 | #define FLAG_COMPAT BIT(0) | |
b6c217ab | 46 | |
eace75cf SK |
47 | struct nvmem_cell { |
48 | const char *name; | |
49 | int offset; | |
50 | int bytes; | |
51 | int bit_offset; | |
52 | int nbits; | |
0749aa25 | 53 | struct device_node *np; |
eace75cf SK |
54 | struct nvmem_device *nvmem; |
55 | struct list_head node; | |
56 | }; | |
57 | ||
58 | static DEFINE_MUTEX(nvmem_mutex); | |
59 | static DEFINE_IDA(nvmem_ida); | |
60 | ||
b985f4cb BG |
61 | static DEFINE_MUTEX(nvmem_cell_mutex); |
62 | static LIST_HEAD(nvmem_cell_tables); | |
63 | ||
506157be BG |
64 | static DEFINE_MUTEX(nvmem_lookup_mutex); |
65 | static LIST_HEAD(nvmem_lookup_list); | |
66 | ||
bee1138b BG |
67 | static BLOCKING_NOTIFIER_HEAD(nvmem_notifier); |
68 | ||
b96fc541 MA |
69 | static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, |
70 | void *val, size_t bytes) | |
71 | { | |
72 | if (nvmem->reg_read) | |
73 | return nvmem->reg_read(nvmem->priv, offset, val, bytes); | |
74 | ||
75 | return -EINVAL; | |
76 | } | |
77 | ||
78 | static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, | |
79 | void *val, size_t bytes) | |
80 | { | |
81 | int ret; | |
82 | ||
83 | if (nvmem->reg_write) { | |
84 | gpiod_set_value_cansleep(nvmem->wp_gpio, 0); | |
85 | ret = nvmem->reg_write(nvmem->priv, offset, val, bytes); | |
86 | gpiod_set_value_cansleep(nvmem->wp_gpio, 1); | |
87 | return ret; | |
88 | } | |
89 | ||
90 | return -EINVAL; | |
91 | } | |
92 | ||
84400305 SK |
93 | #ifdef CONFIG_NVMEM_SYSFS |
94 | static const char * const nvmem_type_str[] = { | |
95 | [NVMEM_TYPE_UNKNOWN] = "Unknown", | |
96 | [NVMEM_TYPE_EEPROM] = "EEPROM", | |
97 | [NVMEM_TYPE_OTP] = "OTP", | |
98 | [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed", | |
99 | }; | |
100 | ||
101 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
102 | static struct lock_class_key eeprom_lock_key; | |
103 | #endif | |
104 | ||
105 | static ssize_t type_show(struct device *dev, | |
106 | struct device_attribute *attr, char *buf) | |
107 | { | |
108 | struct nvmem_device *nvmem = to_nvmem_device(dev); | |
109 | ||
110 | return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]); | |
111 | } | |
112 | ||
113 | static DEVICE_ATTR_RO(type); | |
114 | ||
115 | static struct attribute *nvmem_attrs[] = { | |
116 | &dev_attr_type.attr, | |
117 | NULL, | |
118 | }; | |
119 | ||
120 | static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj, | |
121 | struct bin_attribute *attr, char *buf, | |
122 | loff_t pos, size_t count) | |
123 | { | |
124 | struct device *dev; | |
125 | struct nvmem_device *nvmem; | |
126 | int rc; | |
127 | ||
128 | if (attr->private) | |
129 | dev = attr->private; | |
130 | else | |
131 | dev = container_of(kobj, struct device, kobj); | |
132 | nvmem = to_nvmem_device(dev); | |
133 | ||
134 | /* Stop the user from reading */ | |
135 | if (pos >= nvmem->size) | |
136 | return 0; | |
137 | ||
83566715 DA |
138 | if (!IS_ALIGNED(pos, nvmem->stride)) |
139 | return -EINVAL; | |
140 | ||
84400305 SK |
141 | if (count < nvmem->word_size) |
142 | return -EINVAL; | |
143 | ||
144 | if (pos + count > nvmem->size) | |
145 | count = nvmem->size - pos; | |
146 | ||
147 | count = round_down(count, nvmem->word_size); | |
148 | ||
149 | if (!nvmem->reg_read) | |
150 | return -EPERM; | |
151 | ||
b96fc541 | 152 | rc = nvmem_reg_read(nvmem, pos, buf, count); |
84400305 SK |
153 | |
154 | if (rc) | |
155 | return rc; | |
156 | ||
157 | return count; | |
158 | } | |
159 | ||
160 | static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj, | |
161 | struct bin_attribute *attr, char *buf, | |
162 | loff_t pos, size_t count) | |
163 | { | |
164 | struct device *dev; | |
165 | struct nvmem_device *nvmem; | |
166 | int rc; | |
167 | ||
168 | if (attr->private) | |
169 | dev = attr->private; | |
170 | else | |
171 | dev = container_of(kobj, struct device, kobj); | |
172 | nvmem = to_nvmem_device(dev); | |
173 | ||
174 | /* Stop the user from writing */ | |
175 | if (pos >= nvmem->size) | |
176 | return -EFBIG; | |
177 | ||
83566715 DA |
178 | if (!IS_ALIGNED(pos, nvmem->stride)) |
179 | return -EINVAL; | |
180 | ||
84400305 SK |
181 | if (count < nvmem->word_size) |
182 | return -EINVAL; | |
183 | ||
184 | if (pos + count > nvmem->size) | |
185 | count = nvmem->size - pos; | |
186 | ||
187 | count = round_down(count, nvmem->word_size); | |
188 | ||
189 | if (!nvmem->reg_write) | |
190 | return -EPERM; | |
191 | ||
b96fc541 | 192 | rc = nvmem_reg_write(nvmem, pos, buf, count); |
84400305 SK |
193 | |
194 | if (rc) | |
195 | return rc; | |
196 | ||
197 | return count; | |
198 | } | |
199 | ||
2a4542e5 | 200 | static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem) |
84400305 | 201 | { |
84400305 SK |
202 | umode_t mode = 0400; |
203 | ||
204 | if (!nvmem->root_only) | |
205 | mode |= 0044; | |
206 | ||
207 | if (!nvmem->read_only) | |
208 | mode |= 0200; | |
209 | ||
210 | if (!nvmem->reg_write) | |
211 | mode &= ~0200; | |
212 | ||
213 | if (!nvmem->reg_read) | |
214 | mode &= ~0444; | |
215 | ||
216 | return mode; | |
217 | } | |
218 | ||
2a4542e5 SK |
219 | static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj, |
220 | struct bin_attribute *attr, int i) | |
221 | { | |
222 | struct device *dev = container_of(kobj, struct device, kobj); | |
223 | struct nvmem_device *nvmem = to_nvmem_device(dev); | |
224 | ||
225 | return nvmem_bin_attr_get_umode(nvmem); | |
226 | } | |
227 | ||
84400305 SK |
228 | /* default read/write permissions */ |
229 | static struct bin_attribute bin_attr_rw_nvmem = { | |
230 | .attr = { | |
231 | .name = "nvmem", | |
232 | .mode = 0644, | |
233 | }, | |
234 | .read = bin_attr_nvmem_read, | |
235 | .write = bin_attr_nvmem_write, | |
236 | }; | |
237 | ||
238 | static struct bin_attribute *nvmem_bin_attributes[] = { | |
239 | &bin_attr_rw_nvmem, | |
240 | NULL, | |
241 | }; | |
242 | ||
243 | static const struct attribute_group nvmem_bin_group = { | |
244 | .bin_attrs = nvmem_bin_attributes, | |
245 | .attrs = nvmem_attrs, | |
246 | .is_bin_visible = nvmem_bin_attr_is_visible, | |
247 | }; | |
248 | ||
249 | static const struct attribute_group *nvmem_dev_groups[] = { | |
250 | &nvmem_bin_group, | |
251 | NULL, | |
252 | }; | |
253 | ||
2a4542e5 | 254 | static struct bin_attribute bin_attr_nvmem_eeprom_compat = { |
84400305 | 255 | .attr = { |
2a4542e5 | 256 | .name = "eeprom", |
84400305 SK |
257 | }, |
258 | .read = bin_attr_nvmem_read, | |
259 | .write = bin_attr_nvmem_write, | |
260 | }; | |
261 | ||
84400305 SK |
262 | /* |
263 | * nvmem_setup_compat() - Create an additional binary entry in | |
264 | * drivers sys directory, to be backwards compatible with the older | |
265 | * drivers/misc/eeprom drivers. | |
266 | */ | |
267 | static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, | |
268 | const struct nvmem_config *config) | |
269 | { | |
270 | int rval; | |
271 | ||
272 | if (!config->compat) | |
273 | return 0; | |
274 | ||
275 | if (!config->base_dev) | |
276 | return -EINVAL; | |
277 | ||
2a4542e5 SK |
278 | nvmem->eeprom = bin_attr_nvmem_eeprom_compat; |
279 | nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem); | |
84400305 SK |
280 | nvmem->eeprom.size = nvmem->size; |
281 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
282 | nvmem->eeprom.attr.key = &eeprom_lock_key; | |
283 | #endif | |
284 | nvmem->eeprom.private = &nvmem->dev; | |
285 | nvmem->base_dev = config->base_dev; | |
286 | ||
287 | rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom); | |
288 | if (rval) { | |
289 | dev_err(&nvmem->dev, | |
290 | "Failed to create eeprom binary file %d\n", rval); | |
291 | return rval; | |
292 | } | |
293 | ||
294 | nvmem->flags |= FLAG_COMPAT; | |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
299 | static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, | |
300 | const struct nvmem_config *config) | |
301 | { | |
302 | if (config->compat) | |
303 | device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); | |
304 | } | |
305 | ||
306 | #else /* CONFIG_NVMEM_SYSFS */ | |
307 | ||
308 | static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, | |
309 | const struct nvmem_config *config) | |
310 | { | |
311 | return -ENOSYS; | |
312 | } | |
313 | static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, | |
314 | const struct nvmem_config *config) | |
315 | { | |
316 | } | |
317 | ||
318 | #endif /* CONFIG_NVMEM_SYSFS */ | |
b6c217ab | 319 | |
eace75cf SK |
320 | static void nvmem_release(struct device *dev) |
321 | { | |
322 | struct nvmem_device *nvmem = to_nvmem_device(dev); | |
323 | ||
324 | ida_simple_remove(&nvmem_ida, nvmem->id); | |
a9c3766c | 325 | gpiod_put(nvmem->wp_gpio); |
eace75cf SK |
326 | kfree(nvmem); |
327 | } | |
328 | ||
329 | static const struct device_type nvmem_provider_type = { | |
330 | .release = nvmem_release, | |
331 | }; | |
332 | ||
333 | static struct bus_type nvmem_bus_type = { | |
334 | .name = "nvmem", | |
335 | }; | |
336 | ||
eace75cf SK |
337 | static void nvmem_cell_drop(struct nvmem_cell *cell) |
338 | { | |
bee1138b | 339 | blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell); |
c7235ee3 | 340 | mutex_lock(&nvmem_mutex); |
eace75cf | 341 | list_del(&cell->node); |
c7235ee3 | 342 | mutex_unlock(&nvmem_mutex); |
0749aa25 | 343 | of_node_put(cell->np); |
16bb7abc | 344 | kfree_const(cell->name); |
eace75cf SK |
345 | kfree(cell); |
346 | } | |
347 | ||
348 | static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem) | |
349 | { | |
1852183e | 350 | struct nvmem_cell *cell, *p; |
eace75cf | 351 | |
c7235ee3 BG |
352 | list_for_each_entry_safe(cell, p, &nvmem->cells, node) |
353 | nvmem_cell_drop(cell); | |
eace75cf SK |
354 | } |
355 | ||
356 | static void nvmem_cell_add(struct nvmem_cell *cell) | |
357 | { | |
c7235ee3 BG |
358 | mutex_lock(&nvmem_mutex); |
359 | list_add_tail(&cell->node, &cell->nvmem->cells); | |
360 | mutex_unlock(&nvmem_mutex); | |
bee1138b | 361 | blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell); |
eace75cf SK |
362 | } |
363 | ||
364 | static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, | |
365 | const struct nvmem_cell_info *info, | |
366 | struct nvmem_cell *cell) | |
367 | { | |
368 | cell->nvmem = nvmem; | |
369 | cell->offset = info->offset; | |
370 | cell->bytes = info->bytes; | |
16bb7abc BB |
371 | cell->name = kstrdup_const(info->name, GFP_KERNEL); |
372 | if (!cell->name) | |
373 | return -ENOMEM; | |
eace75cf SK |
374 | |
375 | cell->bit_offset = info->bit_offset; | |
376 | cell->nbits = info->nbits; | |
377 | ||
378 | if (cell->nbits) | |
379 | cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, | |
380 | BITS_PER_BYTE); | |
381 | ||
382 | if (!IS_ALIGNED(cell->offset, nvmem->stride)) { | |
383 | dev_err(&nvmem->dev, | |
384 | "cell %s unaligned to nvmem stride %d\n", | |
385 | cell->name, nvmem->stride); | |
386 | return -EINVAL; | |
387 | } | |
388 | ||
389 | return 0; | |
390 | } | |
391 | ||
b3db17e4 AL |
392 | /** |
393 | * nvmem_add_cells() - Add cell information to an nvmem device | |
394 | * | |
395 | * @nvmem: nvmem device to add cells to. | |
396 | * @info: nvmem cell info to add to the device | |
397 | * @ncells: number of cells in info | |
398 | * | |
399 | * Return: 0 or negative error code on failure. | |
400 | */ | |
ef92ab30 | 401 | static int nvmem_add_cells(struct nvmem_device *nvmem, |
b3db17e4 AL |
402 | const struct nvmem_cell_info *info, |
403 | int ncells) | |
eace75cf SK |
404 | { |
405 | struct nvmem_cell **cells; | |
eace75cf SK |
406 | int i, rval; |
407 | ||
b3db17e4 | 408 | cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL); |
eace75cf SK |
409 | if (!cells) |
410 | return -ENOMEM; | |
411 | ||
b3db17e4 | 412 | for (i = 0; i < ncells; i++) { |
eace75cf SK |
413 | cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL); |
414 | if (!cells[i]) { | |
415 | rval = -ENOMEM; | |
416 | goto err; | |
417 | } | |
418 | ||
419 | rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]); | |
287980e4 | 420 | if (rval) { |
eace75cf SK |
421 | kfree(cells[i]); |
422 | goto err; | |
423 | } | |
424 | ||
425 | nvmem_cell_add(cells[i]); | |
426 | } | |
427 | ||
eace75cf SK |
428 | /* remove tmp array */ |
429 | kfree(cells); | |
430 | ||
431 | return 0; | |
432 | err: | |
dfdf1414 | 433 | while (i--) |
eace75cf SK |
434 | nvmem_cell_drop(cells[i]); |
435 | ||
dfdf1414 RV |
436 | kfree(cells); |
437 | ||
eace75cf SK |
438 | return rval; |
439 | } | |
440 | ||
bee1138b BG |
441 | /** |
442 | * nvmem_register_notifier() - Register a notifier block for nvmem events. | |
443 | * | |
444 | * @nb: notifier block to be called on nvmem events. | |
445 | * | |
446 | * Return: 0 on success, negative error number on failure. | |
447 | */ | |
448 | int nvmem_register_notifier(struct notifier_block *nb) | |
449 | { | |
450 | return blocking_notifier_chain_register(&nvmem_notifier, nb); | |
451 | } | |
452 | EXPORT_SYMBOL_GPL(nvmem_register_notifier); | |
453 | ||
454 | /** | |
455 | * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events. | |
456 | * | |
457 | * @nb: notifier block to be unregistered. | |
458 | * | |
459 | * Return: 0 on success, negative error number on failure. | |
460 | */ | |
461 | int nvmem_unregister_notifier(struct notifier_block *nb) | |
462 | { | |
463 | return blocking_notifier_chain_unregister(&nvmem_notifier, nb); | |
464 | } | |
465 | EXPORT_SYMBOL_GPL(nvmem_unregister_notifier); | |
466 | ||
b985f4cb BG |
467 | static int nvmem_add_cells_from_table(struct nvmem_device *nvmem) |
468 | { | |
469 | const struct nvmem_cell_info *info; | |
470 | struct nvmem_cell_table *table; | |
471 | struct nvmem_cell *cell; | |
472 | int rval = 0, i; | |
473 | ||
474 | mutex_lock(&nvmem_cell_mutex); | |
475 | list_for_each_entry(table, &nvmem_cell_tables, node) { | |
476 | if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) { | |
477 | for (i = 0; i < table->ncells; i++) { | |
478 | info = &table->cells[i]; | |
479 | ||
480 | cell = kzalloc(sizeof(*cell), GFP_KERNEL); | |
481 | if (!cell) { | |
482 | rval = -ENOMEM; | |
483 | goto out; | |
484 | } | |
485 | ||
486 | rval = nvmem_cell_info_to_nvmem_cell(nvmem, | |
487 | info, | |
488 | cell); | |
489 | if (rval) { | |
490 | kfree(cell); | |
491 | goto out; | |
492 | } | |
493 | ||
494 | nvmem_cell_add(cell); | |
495 | } | |
496 | } | |
497 | } | |
498 | ||
499 | out: | |
500 | mutex_unlock(&nvmem_cell_mutex); | |
501 | return rval; | |
502 | } | |
503 | ||
506157be BG |
504 | static struct nvmem_cell * |
505 | nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id) | |
506 | { | |
1c832674 | 507 | struct nvmem_cell *iter, *cell = NULL; |
506157be BG |
508 | |
509 | mutex_lock(&nvmem_mutex); | |
1c832674 AB |
510 | list_for_each_entry(iter, &nvmem->cells, node) { |
511 | if (strcmp(cell_id, iter->name) == 0) { | |
512 | cell = iter; | |
506157be | 513 | break; |
1c832674 | 514 | } |
506157be BG |
515 | } |
516 | mutex_unlock(&nvmem_mutex); | |
517 | ||
518 | return cell; | |
519 | } | |
520 | ||
e888d445 BG |
521 | static int nvmem_add_cells_from_of(struct nvmem_device *nvmem) |
522 | { | |
523 | struct device_node *parent, *child; | |
524 | struct device *dev = &nvmem->dev; | |
525 | struct nvmem_cell *cell; | |
526 | const __be32 *addr; | |
527 | int len; | |
528 | ||
529 | parent = dev->of_node; | |
530 | ||
531 | for_each_child_of_node(parent, child) { | |
532 | addr = of_get_property(child, "reg", &len); | |
533 | if (!addr || (len < 2 * sizeof(u32))) { | |
534 | dev_err(dev, "nvmem: invalid reg on %pOF\n", child); | |
535 | return -EINVAL; | |
536 | } | |
537 | ||
538 | cell = kzalloc(sizeof(*cell), GFP_KERNEL); | |
539 | if (!cell) | |
540 | return -ENOMEM; | |
541 | ||
542 | cell->nvmem = nvmem; | |
0749aa25 | 543 | cell->np = of_node_get(child); |
e888d445 BG |
544 | cell->offset = be32_to_cpup(addr++); |
545 | cell->bytes = be32_to_cpup(addr); | |
badcdff1 | 546 | cell->name = kasprintf(GFP_KERNEL, "%pOFn", child); |
e888d445 BG |
547 | |
548 | addr = of_get_property(child, "bits", &len); | |
549 | if (addr && len == (2 * sizeof(u32))) { | |
550 | cell->bit_offset = be32_to_cpup(addr++); | |
551 | cell->nbits = be32_to_cpup(addr); | |
552 | } | |
553 | ||
554 | if (cell->nbits) | |
555 | cell->bytes = DIV_ROUND_UP( | |
556 | cell->nbits + cell->bit_offset, | |
557 | BITS_PER_BYTE); | |
558 | ||
559 | if (!IS_ALIGNED(cell->offset, nvmem->stride)) { | |
560 | dev_err(dev, "cell %s unaligned to nvmem stride %d\n", | |
561 | cell->name, nvmem->stride); | |
562 | /* Cells already added will be freed later. */ | |
16bb7abc | 563 | kfree_const(cell->name); |
e888d445 BG |
564 | kfree(cell); |
565 | return -EINVAL; | |
566 | } | |
567 | ||
568 | nvmem_cell_add(cell); | |
569 | } | |
570 | ||
571 | return 0; | |
572 | } | |
573 | ||
eace75cf SK |
574 | /** |
575 | * nvmem_register() - Register a nvmem device for given nvmem_config. | |
576 | * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem | |
577 | * | |
578 | * @config: nvmem device configuration with which nvmem device is created. | |
579 | * | |
580 | * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device | |
581 | * on success. | |
582 | */ | |
583 | ||
584 | struct nvmem_device *nvmem_register(const struct nvmem_config *config) | |
585 | { | |
586 | struct nvmem_device *nvmem; | |
eace75cf SK |
587 | int rval; |
588 | ||
589 | if (!config->dev) | |
590 | return ERR_PTR(-EINVAL); | |
591 | ||
061a320b SK |
592 | if (!config->reg_read && !config->reg_write) |
593 | return ERR_PTR(-EINVAL); | |
594 | ||
eace75cf SK |
595 | nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL); |
596 | if (!nvmem) | |
597 | return ERR_PTR(-ENOMEM); | |
598 | ||
599 | rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL); | |
600 | if (rval < 0) { | |
601 | kfree(nvmem); | |
602 | return ERR_PTR(rval); | |
603 | } | |
31c6ff51 | 604 | |
2a127da4 KT |
605 | if (config->wp_gpio) |
606 | nvmem->wp_gpio = config->wp_gpio; | |
607 | else | |
608 | nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp", | |
609 | GPIOD_OUT_HIGH); | |
f7d8d7dc BG |
610 | if (IS_ERR(nvmem->wp_gpio)) { |
611 | ida_simple_remove(&nvmem_ida, nvmem->id); | |
612 | rval = PTR_ERR(nvmem->wp_gpio); | |
613 | kfree(nvmem); | |
614 | return ERR_PTR(rval); | |
615 | } | |
2a127da4 | 616 | |
c1de7f43 | 617 | kref_init(&nvmem->refcnt); |
c7235ee3 | 618 | INIT_LIST_HEAD(&nvmem->cells); |
c1de7f43 | 619 | |
eace75cf | 620 | nvmem->id = rval; |
eace75cf | 621 | nvmem->owner = config->owner; |
17eb18d6 MY |
622 | if (!nvmem->owner && config->dev->driver) |
623 | nvmem->owner = config->dev->driver->owner; | |
99897efd HK |
624 | nvmem->stride = config->stride ?: 1; |
625 | nvmem->word_size = config->word_size ?: 1; | |
795ddd18 | 626 | nvmem->size = config->size; |
eace75cf SK |
627 | nvmem->dev.type = &nvmem_provider_type; |
628 | nvmem->dev.bus = &nvmem_bus_type; | |
629 | nvmem->dev.parent = config->dev; | |
e6de179d | 630 | nvmem->root_only = config->root_only; |
795ddd18 | 631 | nvmem->priv = config->priv; |
16688453 | 632 | nvmem->type = config->type; |
795ddd18 SK |
633 | nvmem->reg_read = config->reg_read; |
634 | nvmem->reg_write = config->reg_write; | |
517f14d9 BG |
635 | if (!config->no_of_node) |
636 | nvmem->dev.of_node = config->dev->of_node; | |
fd0f4906 AS |
637 | |
638 | if (config->id == -1 && config->name) { | |
639 | dev_set_name(&nvmem->dev, "%s", config->name); | |
640 | } else { | |
641 | dev_set_name(&nvmem->dev, "%s%d", | |
642 | config->name ? : "nvmem", | |
643 | config->name ? config->id : nvmem->id); | |
644 | } | |
eace75cf | 645 | |
1716cfe8 AB |
646 | nvmem->read_only = device_property_present(config->dev, "read-only") || |
647 | config->read_only || !nvmem->reg_write; | |
eace75cf | 648 | |
84400305 SK |
649 | #ifdef CONFIG_NVMEM_SYSFS |
650 | nvmem->dev.groups = nvmem_dev_groups; | |
651 | #endif | |
eace75cf | 652 | |
eace75cf SK |
653 | dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); |
654 | ||
f60442dd | 655 | rval = device_register(&nvmem->dev); |
b6c217ab | 656 | if (rval) |
3360acdf | 657 | goto err_put_device; |
b6c217ab AL |
658 | |
659 | if (config->compat) { | |
ae0c2d72 | 660 | rval = nvmem_sysfs_setup_compat(nvmem, config); |
b6c217ab | 661 | if (rval) |
3360acdf | 662 | goto err_device_del; |
eace75cf SK |
663 | } |
664 | ||
fa72d847 BG |
665 | if (config->cells) { |
666 | rval = nvmem_add_cells(nvmem, config->cells, config->ncells); | |
667 | if (rval) | |
668 | goto err_teardown_compat; | |
669 | } | |
eace75cf | 670 | |
b985f4cb BG |
671 | rval = nvmem_add_cells_from_table(nvmem); |
672 | if (rval) | |
673 | goto err_remove_cells; | |
674 | ||
e888d445 BG |
675 | rval = nvmem_add_cells_from_of(nvmem); |
676 | if (rval) | |
677 | goto err_remove_cells; | |
678 | ||
f4853e1c | 679 | blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); |
bee1138b | 680 | |
eace75cf | 681 | return nvmem; |
3360acdf | 682 | |
b985f4cb BG |
683 | err_remove_cells: |
684 | nvmem_device_remove_all_cells(nvmem); | |
fa72d847 BG |
685 | err_teardown_compat: |
686 | if (config->compat) | |
ae0c2d72 | 687 | nvmem_sysfs_remove_compat(nvmem, config); |
3360acdf JH |
688 | err_device_del: |
689 | device_del(&nvmem->dev); | |
690 | err_put_device: | |
691 | put_device(&nvmem->dev); | |
692 | ||
b6c217ab | 693 | return ERR_PTR(rval); |
eace75cf SK |
694 | } |
695 | EXPORT_SYMBOL_GPL(nvmem_register); | |
696 | ||
c1de7f43 BG |
697 | static void nvmem_device_release(struct kref *kref) |
698 | { | |
699 | struct nvmem_device *nvmem; | |
700 | ||
701 | nvmem = container_of(kref, struct nvmem_device, refcnt); | |
702 | ||
bee1138b BG |
703 | blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem); |
704 | ||
c1de7f43 BG |
705 | if (nvmem->flags & FLAG_COMPAT) |
706 | device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); | |
707 | ||
708 | nvmem_device_remove_all_cells(nvmem); | |
f60442dd | 709 | device_unregister(&nvmem->dev); |
c1de7f43 BG |
710 | } |
711 | ||
eace75cf SK |
712 | /** |
713 | * nvmem_unregister() - Unregister previously registered nvmem device | |
714 | * | |
715 | * @nvmem: Pointer to previously registered nvmem device. | |
eace75cf | 716 | */ |
bf58e882 | 717 | void nvmem_unregister(struct nvmem_device *nvmem) |
eace75cf | 718 | { |
c1de7f43 | 719 | kref_put(&nvmem->refcnt, nvmem_device_release); |
eace75cf SK |
720 | } |
721 | EXPORT_SYMBOL_GPL(nvmem_unregister); | |
722 | ||
f1f50eca AS |
723 | static void devm_nvmem_release(struct device *dev, void *res) |
724 | { | |
bf58e882 | 725 | nvmem_unregister(*(struct nvmem_device **)res); |
f1f50eca AS |
726 | } |
727 | ||
728 | /** | |
729 | * devm_nvmem_register() - Register a managed nvmem device for given | |
730 | * nvmem_config. | |
731 | * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem | |
732 | * | |
b378c779 | 733 | * @dev: Device that uses the nvmem device. |
f1f50eca AS |
734 | * @config: nvmem device configuration with which nvmem device is created. |
735 | * | |
736 | * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device | |
737 | * on success. | |
738 | */ | |
739 | struct nvmem_device *devm_nvmem_register(struct device *dev, | |
740 | const struct nvmem_config *config) | |
741 | { | |
742 | struct nvmem_device **ptr, *nvmem; | |
743 | ||
744 | ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL); | |
745 | if (!ptr) | |
746 | return ERR_PTR(-ENOMEM); | |
747 | ||
748 | nvmem = nvmem_register(config); | |
749 | ||
750 | if (!IS_ERR(nvmem)) { | |
751 | *ptr = nvmem; | |
752 | devres_add(dev, ptr); | |
753 | } else { | |
754 | devres_free(ptr); | |
755 | } | |
756 | ||
757 | return nvmem; | |
758 | } | |
759 | EXPORT_SYMBOL_GPL(devm_nvmem_register); | |
760 | ||
761 | static int devm_nvmem_match(struct device *dev, void *res, void *data) | |
762 | { | |
763 | struct nvmem_device **r = res; | |
764 | ||
765 | return *r == data; | |
766 | } | |
767 | ||
768 | /** | |
769 | * devm_nvmem_unregister() - Unregister previously registered managed nvmem | |
770 | * device. | |
771 | * | |
b378c779 | 772 | * @dev: Device that uses the nvmem device. |
f1f50eca AS |
773 | * @nvmem: Pointer to previously registered nvmem device. |
774 | * | |
775 | * Return: Will be an negative on error or a zero on success. | |
776 | */ | |
777 | int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem) | |
778 | { | |
779 | return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem); | |
780 | } | |
781 | EXPORT_SYMBOL(devm_nvmem_unregister); | |
782 | ||
8c2a2b8c TB |
783 | static struct nvmem_device *__nvmem_device_get(void *data, |
784 | int (*match)(struct device *dev, const void *data)) | |
69aba794 SK |
785 | { |
786 | struct nvmem_device *nvmem = NULL; | |
8c2a2b8c | 787 | struct device *dev; |
69aba794 | 788 | |
c7235ee3 | 789 | mutex_lock(&nvmem_mutex); |
8c2a2b8c TB |
790 | dev = bus_find_device(&nvmem_bus_type, NULL, data, match); |
791 | if (dev) | |
792 | nvmem = to_nvmem_device(dev); | |
69aba794 | 793 | mutex_unlock(&nvmem_mutex); |
c7235ee3 BG |
794 | if (!nvmem) |
795 | return ERR_PTR(-EPROBE_DEFER); | |
69aba794 SK |
796 | |
797 | if (!try_module_get(nvmem->owner)) { | |
798 | dev_err(&nvmem->dev, | |
799 | "could not increase module refcount for cell %s\n", | |
5db652c9 | 800 | nvmem_dev_name(nvmem)); |
69aba794 | 801 | |
73e9dc4d | 802 | put_device(&nvmem->dev); |
69aba794 SK |
803 | return ERR_PTR(-EINVAL); |
804 | } | |
805 | ||
c1de7f43 BG |
806 | kref_get(&nvmem->refcnt); |
807 | ||
69aba794 SK |
808 | return nvmem; |
809 | } | |
810 | ||
811 | static void __nvmem_device_put(struct nvmem_device *nvmem) | |
812 | { | |
73e9dc4d | 813 | put_device(&nvmem->dev); |
69aba794 | 814 | module_put(nvmem->owner); |
c1de7f43 | 815 | kref_put(&nvmem->refcnt, nvmem_device_release); |
69aba794 SK |
816 | } |
817 | ||
e701c67c | 818 | #if IS_ENABLED(CONFIG_OF) |
e2a5402e SK |
819 | /** |
820 | * of_nvmem_device_get() - Get nvmem device from a given id | |
821 | * | |
29143268 | 822 | * @np: Device tree node that uses the nvmem device. |
e2a5402e SK |
823 | * @id: nvmem name from nvmem-names property. |
824 | * | |
825 | * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device | |
826 | * on success. | |
827 | */ | |
828 | struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id) | |
829 | { | |
830 | ||
831 | struct device_node *nvmem_np; | |
d4e7fef1 | 832 | int index = 0; |
e2a5402e | 833 | |
d4e7fef1 AB |
834 | if (id) |
835 | index = of_property_match_string(np, "nvmem-names", id); | |
e2a5402e SK |
836 | |
837 | nvmem_np = of_parse_phandle(np, "nvmem", index); | |
838 | if (!nvmem_np) | |
d4e7fef1 | 839 | return ERR_PTR(-ENOENT); |
e2a5402e | 840 | |
8c2a2b8c | 841 | return __nvmem_device_get(nvmem_np, device_match_of_node); |
e2a5402e SK |
842 | } |
843 | EXPORT_SYMBOL_GPL(of_nvmem_device_get); | |
844 | #endif | |
845 | ||
846 | /** | |
847 | * nvmem_device_get() - Get nvmem device from a given id | |
848 | * | |
29143268 VG |
849 | * @dev: Device that uses the nvmem device. |
850 | * @dev_name: name of the requested nvmem device. | |
e2a5402e SK |
851 | * |
852 | * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device | |
853 | * on success. | |
854 | */ | |
855 | struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name) | |
856 | { | |
857 | if (dev->of_node) { /* try dt first */ | |
858 | struct nvmem_device *nvmem; | |
859 | ||
860 | nvmem = of_nvmem_device_get(dev->of_node, dev_name); | |
861 | ||
862 | if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER) | |
863 | return nvmem; | |
864 | ||
865 | } | |
866 | ||
8c2a2b8c | 867 | return __nvmem_device_get((void *)dev_name, device_match_name); |
e2a5402e SK |
868 | } |
869 | EXPORT_SYMBOL_GPL(nvmem_device_get); | |
870 | ||
8c2a2b8c TB |
871 | /** |
872 | * nvmem_device_find() - Find nvmem device with matching function | |
873 | * | |
874 | * @data: Data to pass to match function | |
875 | * @match: Callback function to check device | |
876 | * | |
877 | * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device | |
878 | * on success. | |
879 | */ | |
880 | struct nvmem_device *nvmem_device_find(void *data, | |
881 | int (*match)(struct device *dev, const void *data)) | |
882 | { | |
883 | return __nvmem_device_get(data, match); | |
884 | } | |
885 | EXPORT_SYMBOL_GPL(nvmem_device_find); | |
886 | ||
e2a5402e SK |
887 | static int devm_nvmem_device_match(struct device *dev, void *res, void *data) |
888 | { | |
889 | struct nvmem_device **nvmem = res; | |
890 | ||
891 | if (WARN_ON(!nvmem || !*nvmem)) | |
892 | return 0; | |
893 | ||
894 | return *nvmem == data; | |
895 | } | |
896 | ||
897 | static void devm_nvmem_device_release(struct device *dev, void *res) | |
898 | { | |
899 | nvmem_device_put(*(struct nvmem_device **)res); | |
900 | } | |
901 | ||
902 | /** | |
903 | * devm_nvmem_device_put() - put alredy got nvmem device | |
904 | * | |
29143268 | 905 | * @dev: Device that uses the nvmem device. |
e2a5402e SK |
906 | * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(), |
907 | * that needs to be released. | |
908 | */ | |
909 | void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem) | |
910 | { | |
911 | int ret; | |
912 | ||
913 | ret = devres_release(dev, devm_nvmem_device_release, | |
914 | devm_nvmem_device_match, nvmem); | |
915 | ||
916 | WARN_ON(ret); | |
917 | } | |
918 | EXPORT_SYMBOL_GPL(devm_nvmem_device_put); | |
919 | ||
920 | /** | |
921 | * nvmem_device_put() - put alredy got nvmem device | |
922 | * | |
923 | * @nvmem: pointer to nvmem device that needs to be released. | |
924 | */ | |
925 | void nvmem_device_put(struct nvmem_device *nvmem) | |
926 | { | |
927 | __nvmem_device_put(nvmem); | |
928 | } | |
929 | EXPORT_SYMBOL_GPL(nvmem_device_put); | |
930 | ||
931 | /** | |
932 | * devm_nvmem_device_get() - Get nvmem cell of device form a given id | |
933 | * | |
29143268 VG |
934 | * @dev: Device that requests the nvmem device. |
935 | * @id: name id for the requested nvmem device. | |
e2a5402e SK |
936 | * |
937 | * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell | |
938 | * on success. The nvmem_cell will be freed by the automatically once the | |
939 | * device is freed. | |
940 | */ | |
941 | struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id) | |
942 | { | |
943 | struct nvmem_device **ptr, *nvmem; | |
944 | ||
945 | ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL); | |
946 | if (!ptr) | |
947 | return ERR_PTR(-ENOMEM); | |
948 | ||
949 | nvmem = nvmem_device_get(dev, id); | |
950 | if (!IS_ERR(nvmem)) { | |
951 | *ptr = nvmem; | |
952 | devres_add(dev, ptr); | |
953 | } else { | |
954 | devres_free(ptr); | |
955 | } | |
956 | ||
957 | return nvmem; | |
958 | } | |
959 | EXPORT_SYMBOL_GPL(devm_nvmem_device_get); | |
960 | ||
506157be BG |
961 | static struct nvmem_cell * |
962 | nvmem_cell_get_from_lookup(struct device *dev, const char *con_id) | |
69aba794 | 963 | { |
506157be BG |
964 | struct nvmem_cell *cell = ERR_PTR(-ENOENT); |
965 | struct nvmem_cell_lookup *lookup; | |
69aba794 | 966 | struct nvmem_device *nvmem; |
506157be | 967 | const char *dev_id; |
69aba794 | 968 | |
506157be BG |
969 | if (!dev) |
970 | return ERR_PTR(-EINVAL); | |
971 | ||
972 | dev_id = dev_name(dev); | |
973 | ||
974 | mutex_lock(&nvmem_lookup_mutex); | |
975 | ||
976 | list_for_each_entry(lookup, &nvmem_lookup_list, node) { | |
977 | if ((strcmp(lookup->dev_id, dev_id) == 0) && | |
978 | (strcmp(lookup->con_id, con_id) == 0)) { | |
979 | /* This is the right entry. */ | |
8c2a2b8c TB |
980 | nvmem = __nvmem_device_get((void *)lookup->nvmem_name, |
981 | device_match_name); | |
cccb3b19 | 982 | if (IS_ERR(nvmem)) { |
506157be | 983 | /* Provider may not be registered yet. */ |
cccb3b19 | 984 | cell = ERR_CAST(nvmem); |
9bfd8198 | 985 | break; |
506157be BG |
986 | } |
987 | ||
988 | cell = nvmem_find_cell_by_name(nvmem, | |
989 | lookup->cell_name); | |
990 | if (!cell) { | |
991 | __nvmem_device_put(nvmem); | |
cccb3b19 | 992 | cell = ERR_PTR(-ENOENT); |
506157be | 993 | } |
9bfd8198 | 994 | break; |
506157be BG |
995 | } |
996 | } | |
69aba794 | 997 | |
506157be | 998 | mutex_unlock(&nvmem_lookup_mutex); |
69aba794 SK |
999 | return cell; |
1000 | } | |
1001 | ||
e701c67c | 1002 | #if IS_ENABLED(CONFIG_OF) |
3c53e235 | 1003 | static struct nvmem_cell * |
0749aa25 | 1004 | nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np) |
3c53e235 | 1005 | { |
1c832674 | 1006 | struct nvmem_cell *iter, *cell = NULL; |
3c53e235 AB |
1007 | |
1008 | mutex_lock(&nvmem_mutex); | |
1c832674 AB |
1009 | list_for_each_entry(iter, &nvmem->cells, node) { |
1010 | if (np == iter->np) { | |
1011 | cell = iter; | |
3c53e235 | 1012 | break; |
1c832674 | 1013 | } |
3c53e235 AB |
1014 | } |
1015 | mutex_unlock(&nvmem_mutex); | |
1016 | ||
1017 | return cell; | |
1018 | } | |
1019 | ||
69aba794 SK |
1020 | /** |
1021 | * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id | |
1022 | * | |
29143268 | 1023 | * @np: Device tree node that uses the nvmem cell. |
165589f0 BG |
1024 | * @id: nvmem cell name from nvmem-cell-names property, or NULL |
1025 | * for the cell at index 0 (the lone cell with no accompanying | |
1026 | * nvmem-cell-names property). | |
69aba794 SK |
1027 | * |
1028 | * Return: Will be an ERR_PTR() on error or a valid pointer | |
1029 | * to a struct nvmem_cell. The nvmem_cell will be freed by the | |
1030 | * nvmem_cell_put(). | |
1031 | */ | |
165589f0 | 1032 | struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id) |
69aba794 SK |
1033 | { |
1034 | struct device_node *cell_np, *nvmem_np; | |
69aba794 | 1035 | struct nvmem_device *nvmem; |
e888d445 | 1036 | struct nvmem_cell *cell; |
fd0c478c | 1037 | int index = 0; |
69aba794 | 1038 | |
fd0c478c | 1039 | /* if cell name exists, find index to the name */ |
165589f0 BG |
1040 | if (id) |
1041 | index = of_property_match_string(np, "nvmem-cell-names", id); | |
69aba794 SK |
1042 | |
1043 | cell_np = of_parse_phandle(np, "nvmem-cells", index); | |
1044 | if (!cell_np) | |
5087cc19 | 1045 | return ERR_PTR(-ENOENT); |
69aba794 SK |
1046 | |
1047 | nvmem_np = of_get_next_parent(cell_np); | |
1048 | if (!nvmem_np) | |
1049 | return ERR_PTR(-EINVAL); | |
1050 | ||
8c2a2b8c | 1051 | nvmem = __nvmem_device_get(nvmem_np, device_match_of_node); |
aad8d097 | 1052 | of_node_put(nvmem_np); |
69aba794 SK |
1053 | if (IS_ERR(nvmem)) |
1054 | return ERR_CAST(nvmem); | |
1055 | ||
0749aa25 | 1056 | cell = nvmem_find_cell_by_node(nvmem, cell_np); |
69aba794 | 1057 | if (!cell) { |
e888d445 BG |
1058 | __nvmem_device_put(nvmem); |
1059 | return ERR_PTR(-ENOENT); | |
69aba794 SK |
1060 | } |
1061 | ||
69aba794 | 1062 | return cell; |
69aba794 SK |
1063 | } |
1064 | EXPORT_SYMBOL_GPL(of_nvmem_cell_get); | |
1065 | #endif | |
1066 | ||
1067 | /** | |
1068 | * nvmem_cell_get() - Get nvmem cell of device form a given cell name | |
1069 | * | |
29143268 | 1070 | * @dev: Device that requests the nvmem cell. |
165589f0 BG |
1071 | * @id: nvmem cell name to get (this corresponds with the name from the |
1072 | * nvmem-cell-names property for DT systems and with the con_id from | |
1073 | * the lookup entry for non-DT systems). | |
69aba794 SK |
1074 | * |
1075 | * Return: Will be an ERR_PTR() on error or a valid pointer | |
1076 | * to a struct nvmem_cell. The nvmem_cell will be freed by the | |
1077 | * nvmem_cell_put(). | |
1078 | */ | |
165589f0 | 1079 | struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id) |
69aba794 SK |
1080 | { |
1081 | struct nvmem_cell *cell; | |
1082 | ||
1083 | if (dev->of_node) { /* try dt first */ | |
165589f0 | 1084 | cell = of_nvmem_cell_get(dev->of_node, id); |
69aba794 SK |
1085 | if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER) |
1086 | return cell; | |
1087 | } | |
1088 | ||
165589f0 BG |
1089 | /* NULL cell id only allowed for device tree; invalid otherwise */ |
1090 | if (!id) | |
87ed1405 DA |
1091 | return ERR_PTR(-EINVAL); |
1092 | ||
165589f0 | 1093 | return nvmem_cell_get_from_lookup(dev, id); |
69aba794 SK |
1094 | } |
1095 | EXPORT_SYMBOL_GPL(nvmem_cell_get); | |
1096 | ||
1097 | static void devm_nvmem_cell_release(struct device *dev, void *res) | |
1098 | { | |
1099 | nvmem_cell_put(*(struct nvmem_cell **)res); | |
1100 | } | |
1101 | ||
1102 | /** | |
1103 | * devm_nvmem_cell_get() - Get nvmem cell of device form a given id | |
1104 | * | |
29143268 VG |
1105 | * @dev: Device that requests the nvmem cell. |
1106 | * @id: nvmem cell name id to get. | |
69aba794 SK |
1107 | * |
1108 | * Return: Will be an ERR_PTR() on error or a valid pointer | |
1109 | * to a struct nvmem_cell. The nvmem_cell will be freed by the | |
1110 | * automatically once the device is freed. | |
1111 | */ | |
1112 | struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id) | |
1113 | { | |
1114 | struct nvmem_cell **ptr, *cell; | |
1115 | ||
1116 | ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL); | |
1117 | if (!ptr) | |
1118 | return ERR_PTR(-ENOMEM); | |
1119 | ||
1120 | cell = nvmem_cell_get(dev, id); | |
1121 | if (!IS_ERR(cell)) { | |
1122 | *ptr = cell; | |
1123 | devres_add(dev, ptr); | |
1124 | } else { | |
1125 | devres_free(ptr); | |
1126 | } | |
1127 | ||
1128 | return cell; | |
1129 | } | |
1130 | EXPORT_SYMBOL_GPL(devm_nvmem_cell_get); | |
1131 | ||
1132 | static int devm_nvmem_cell_match(struct device *dev, void *res, void *data) | |
1133 | { | |
1134 | struct nvmem_cell **c = res; | |
1135 | ||
1136 | if (WARN_ON(!c || !*c)) | |
1137 | return 0; | |
1138 | ||
1139 | return *c == data; | |
1140 | } | |
1141 | ||
1142 | /** | |
1143 | * devm_nvmem_cell_put() - Release previously allocated nvmem cell | |
1144 | * from devm_nvmem_cell_get. | |
1145 | * | |
29143268 VG |
1146 | * @dev: Device that requests the nvmem cell. |
1147 | * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get(). | |
69aba794 SK |
1148 | */ |
1149 | void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell) | |
1150 | { | |
1151 | int ret; | |
1152 | ||
1153 | ret = devres_release(dev, devm_nvmem_cell_release, | |
1154 | devm_nvmem_cell_match, cell); | |
1155 | ||
1156 | WARN_ON(ret); | |
1157 | } | |
1158 | EXPORT_SYMBOL(devm_nvmem_cell_put); | |
1159 | ||
1160 | /** | |
1161 | * nvmem_cell_put() - Release previously allocated nvmem cell. | |
1162 | * | |
29143268 | 1163 | * @cell: Previously allocated nvmem cell by nvmem_cell_get(). |
69aba794 SK |
1164 | */ |
1165 | void nvmem_cell_put(struct nvmem_cell *cell) | |
1166 | { | |
1167 | struct nvmem_device *nvmem = cell->nvmem; | |
1168 | ||
1169 | __nvmem_device_put(nvmem); | |
69aba794 SK |
1170 | } |
1171 | EXPORT_SYMBOL_GPL(nvmem_cell_put); | |
1172 | ||
f7c04f16 | 1173 | static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf) |
69aba794 SK |
1174 | { |
1175 | u8 *p, *b; | |
2fe518fe | 1176 | int i, extra, bit_offset = cell->bit_offset; |
69aba794 SK |
1177 | |
1178 | p = b = buf; | |
1179 | if (bit_offset) { | |
1180 | /* First shift */ | |
1181 | *b++ >>= bit_offset; | |
1182 | ||
1183 | /* setup rest of the bytes if any */ | |
1184 | for (i = 1; i < cell->bytes; i++) { | |
1185 | /* Get bits from next byte and shift them towards msb */ | |
1186 | *p |= *b << (BITS_PER_BYTE - bit_offset); | |
1187 | ||
1188 | p = b; | |
1189 | *b++ >>= bit_offset; | |
1190 | } | |
2fe518fe JRO |
1191 | } else { |
1192 | /* point to the msb */ | |
1193 | p += cell->bytes - 1; | |
69aba794 | 1194 | } |
2fe518fe JRO |
1195 | |
1196 | /* result fits in less bytes */ | |
1197 | extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE); | |
1198 | while (--extra >= 0) | |
1199 | *p-- = 0; | |
1200 | ||
69aba794 SK |
1201 | /* clear msb bits if any leftover in the last byte */ |
1202 | *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0); | |
1203 | } | |
1204 | ||
1205 | static int __nvmem_cell_read(struct nvmem_device *nvmem, | |
1206 | struct nvmem_cell *cell, | |
1207 | void *buf, size_t *len) | |
1208 | { | |
1209 | int rc; | |
1210 | ||
795ddd18 | 1211 | rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes); |
69aba794 | 1212 | |
287980e4 | 1213 | if (rc) |
69aba794 SK |
1214 | return rc; |
1215 | ||
1216 | /* shift bits in-place */ | |
cbf854ab | 1217 | if (cell->bit_offset || cell->nbits) |
69aba794 SK |
1218 | nvmem_shift_read_buffer_in_place(cell, buf); |
1219 | ||
3b4a6877 VG |
1220 | if (len) |
1221 | *len = cell->bytes; | |
69aba794 SK |
1222 | |
1223 | return 0; | |
1224 | } | |
1225 | ||
1226 | /** | |
1227 | * nvmem_cell_read() - Read a given nvmem cell | |
1228 | * | |
1229 | * @cell: nvmem cell to be read. | |
3b4a6877 VG |
1230 | * @len: pointer to length of cell which will be populated on successful read; |
1231 | * can be NULL. | |
69aba794 | 1232 | * |
b577fafc BN |
1233 | * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The |
1234 | * buffer should be freed by the consumer with a kfree(). | |
69aba794 SK |
1235 | */ |
1236 | void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len) | |
1237 | { | |
1238 | struct nvmem_device *nvmem = cell->nvmem; | |
1239 | u8 *buf; | |
1240 | int rc; | |
1241 | ||
795ddd18 | 1242 | if (!nvmem) |
69aba794 SK |
1243 | return ERR_PTR(-EINVAL); |
1244 | ||
1245 | buf = kzalloc(cell->bytes, GFP_KERNEL); | |
1246 | if (!buf) | |
1247 | return ERR_PTR(-ENOMEM); | |
1248 | ||
1249 | rc = __nvmem_cell_read(nvmem, cell, buf, len); | |
287980e4 | 1250 | if (rc) { |
69aba794 SK |
1251 | kfree(buf); |
1252 | return ERR_PTR(rc); | |
1253 | } | |
1254 | ||
1255 | return buf; | |
1256 | } | |
1257 | EXPORT_SYMBOL_GPL(nvmem_cell_read); | |
1258 | ||
f7c04f16 MY |
1259 | static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell, |
1260 | u8 *_buf, int len) | |
69aba794 SK |
1261 | { |
1262 | struct nvmem_device *nvmem = cell->nvmem; | |
1263 | int i, rc, nbits, bit_offset = cell->bit_offset; | |
1264 | u8 v, *p, *buf, *b, pbyte, pbits; | |
1265 | ||
1266 | nbits = cell->nbits; | |
1267 | buf = kzalloc(cell->bytes, GFP_KERNEL); | |
1268 | if (!buf) | |
1269 | return ERR_PTR(-ENOMEM); | |
1270 | ||
1271 | memcpy(buf, _buf, len); | |
1272 | p = b = buf; | |
1273 | ||
1274 | if (bit_offset) { | |
1275 | pbyte = *b; | |
1276 | *b <<= bit_offset; | |
1277 | ||
1278 | /* setup the first byte with lsb bits from nvmem */ | |
795ddd18 | 1279 | rc = nvmem_reg_read(nvmem, cell->offset, &v, 1); |
50808bfc MM |
1280 | if (rc) |
1281 | goto err; | |
69aba794 SK |
1282 | *b++ |= GENMASK(bit_offset - 1, 0) & v; |
1283 | ||
1284 | /* setup rest of the byte if any */ | |
1285 | for (i = 1; i < cell->bytes; i++) { | |
1286 | /* Get last byte bits and shift them towards lsb */ | |
1287 | pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset); | |
1288 | pbyte = *b; | |
1289 | p = b; | |
1290 | *b <<= bit_offset; | |
1291 | *b++ |= pbits; | |
1292 | } | |
1293 | } | |
1294 | ||
1295 | /* if it's not end on byte boundary */ | |
1296 | if ((nbits + bit_offset) % BITS_PER_BYTE) { | |
1297 | /* setup the last byte with msb bits from nvmem */ | |
795ddd18 | 1298 | rc = nvmem_reg_read(nvmem, |
69aba794 | 1299 | cell->offset + cell->bytes - 1, &v, 1); |
50808bfc MM |
1300 | if (rc) |
1301 | goto err; | |
69aba794 SK |
1302 | *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v; |
1303 | ||
1304 | } | |
1305 | ||
1306 | return buf; | |
50808bfc MM |
1307 | err: |
1308 | kfree(buf); | |
1309 | return ERR_PTR(rc); | |
69aba794 SK |
1310 | } |
1311 | ||
1312 | /** | |
1313 | * nvmem_cell_write() - Write to a given nvmem cell | |
1314 | * | |
1315 | * @cell: nvmem cell to be written. | |
1316 | * @buf: Buffer to be written. | |
1317 | * @len: length of buffer to be written to nvmem cell. | |
1318 | * | |
1319 | * Return: length of bytes written or negative on failure. | |
1320 | */ | |
1321 | int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len) | |
1322 | { | |
1323 | struct nvmem_device *nvmem = cell->nvmem; | |
1324 | int rc; | |
1325 | ||
795ddd18 | 1326 | if (!nvmem || nvmem->read_only || |
69aba794 SK |
1327 | (cell->bit_offset == 0 && len != cell->bytes)) |
1328 | return -EINVAL; | |
1329 | ||
1330 | if (cell->bit_offset || cell->nbits) { | |
1331 | buf = nvmem_cell_prepare_write_buffer(cell, buf, len); | |
1332 | if (IS_ERR(buf)) | |
1333 | return PTR_ERR(buf); | |
1334 | } | |
1335 | ||
795ddd18 | 1336 | rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes); |
69aba794 SK |
1337 | |
1338 | /* free the tmp buffer */ | |
ace22170 | 1339 | if (cell->bit_offset || cell->nbits) |
69aba794 SK |
1340 | kfree(buf); |
1341 | ||
287980e4 | 1342 | if (rc) |
69aba794 SK |
1343 | return rc; |
1344 | ||
1345 | return len; | |
1346 | } | |
1347 | EXPORT_SYMBOL_GPL(nvmem_cell_write); | |
1348 | ||
6bb317ce YL |
1349 | static int nvmem_cell_read_common(struct device *dev, const char *cell_id, |
1350 | void *val, size_t count) | |
0a9b2d1c FG |
1351 | { |
1352 | struct nvmem_cell *cell; | |
1353 | void *buf; | |
1354 | size_t len; | |
1355 | ||
1356 | cell = nvmem_cell_get(dev, cell_id); | |
1357 | if (IS_ERR(cell)) | |
1358 | return PTR_ERR(cell); | |
1359 | ||
1360 | buf = nvmem_cell_read(cell, &len); | |
1361 | if (IS_ERR(buf)) { | |
1362 | nvmem_cell_put(cell); | |
1363 | return PTR_ERR(buf); | |
1364 | } | |
6bb317ce | 1365 | if (len != count) { |
0a9b2d1c FG |
1366 | kfree(buf); |
1367 | nvmem_cell_put(cell); | |
1368 | return -EINVAL; | |
1369 | } | |
6bb317ce | 1370 | memcpy(val, buf, count); |
0a9b2d1c FG |
1371 | kfree(buf); |
1372 | nvmem_cell_put(cell); | |
1373 | ||
1374 | return 0; | |
1375 | } | |
6bb317ce YL |
1376 | |
1377 | /** | |
1378 | * nvmem_cell_read_u16() - Read a cell value as an u16 | |
1379 | * | |
1380 | * @dev: Device that requests the nvmem cell. | |
1381 | * @cell_id: Name of nvmem cell to read. | |
1382 | * @val: pointer to output value. | |
1383 | * | |
1384 | * Return: 0 on success or negative errno. | |
1385 | */ | |
1386 | int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val) | |
1387 | { | |
1388 | return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val)); | |
1389 | } | |
0a9b2d1c FG |
1390 | EXPORT_SYMBOL_GPL(nvmem_cell_read_u16); |
1391 | ||
d026d70a LC |
1392 | /** |
1393 | * nvmem_cell_read_u32() - Read a cell value as an u32 | |
1394 | * | |
1395 | * @dev: Device that requests the nvmem cell. | |
1396 | * @cell_id: Name of nvmem cell to read. | |
1397 | * @val: pointer to output value. | |
1398 | * | |
1399 | * Return: 0 on success or negative errno. | |
1400 | */ | |
1401 | int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val) | |
1402 | { | |
6bb317ce | 1403 | return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val)); |
d026d70a LC |
1404 | } |
1405 | EXPORT_SYMBOL_GPL(nvmem_cell_read_u32); | |
1406 | ||
8b977c54 YL |
1407 | /** |
1408 | * nvmem_cell_read_u64() - Read a cell value as an u64 | |
1409 | * | |
1410 | * @dev: Device that requests the nvmem cell. | |
1411 | * @cell_id: Name of nvmem cell to read. | |
1412 | * @val: pointer to output value. | |
1413 | * | |
1414 | * Return: 0 on success or negative errno. | |
1415 | */ | |
1416 | int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val) | |
1417 | { | |
1418 | return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val)); | |
1419 | } | |
1420 | EXPORT_SYMBOL_GPL(nvmem_cell_read_u64); | |
1421 | ||
e2a5402e SK |
1422 | /** |
1423 | * nvmem_device_cell_read() - Read a given nvmem device and cell | |
1424 | * | |
1425 | * @nvmem: nvmem device to read from. | |
1426 | * @info: nvmem cell info to be read. | |
1427 | * @buf: buffer pointer which will be populated on successful read. | |
1428 | * | |
1429 | * Return: length of successful bytes read on success and negative | |
1430 | * error code on error. | |
1431 | */ | |
1432 | ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem, | |
1433 | struct nvmem_cell_info *info, void *buf) | |
1434 | { | |
1435 | struct nvmem_cell cell; | |
1436 | int rc; | |
1437 | ssize_t len; | |
1438 | ||
795ddd18 | 1439 | if (!nvmem) |
e2a5402e SK |
1440 | return -EINVAL; |
1441 | ||
1442 | rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); | |
287980e4 | 1443 | if (rc) |
e2a5402e SK |
1444 | return rc; |
1445 | ||
1446 | rc = __nvmem_cell_read(nvmem, &cell, buf, &len); | |
287980e4 | 1447 | if (rc) |
e2a5402e SK |
1448 | return rc; |
1449 | ||
1450 | return len; | |
1451 | } | |
1452 | EXPORT_SYMBOL_GPL(nvmem_device_cell_read); | |
1453 | ||
1454 | /** | |
1455 | * nvmem_device_cell_write() - Write cell to a given nvmem device | |
1456 | * | |
1457 | * @nvmem: nvmem device to be written to. | |
29143268 | 1458 | * @info: nvmem cell info to be written. |
e2a5402e SK |
1459 | * @buf: buffer to be written to cell. |
1460 | * | |
1461 | * Return: length of bytes written or negative error code on failure. | |
48f63a2c | 1462 | */ |
e2a5402e SK |
1463 | int nvmem_device_cell_write(struct nvmem_device *nvmem, |
1464 | struct nvmem_cell_info *info, void *buf) | |
1465 | { | |
1466 | struct nvmem_cell cell; | |
1467 | int rc; | |
1468 | ||
795ddd18 | 1469 | if (!nvmem) |
e2a5402e SK |
1470 | return -EINVAL; |
1471 | ||
1472 | rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); | |
287980e4 | 1473 | if (rc) |
e2a5402e SK |
1474 | return rc; |
1475 | ||
1476 | return nvmem_cell_write(&cell, buf, cell.bytes); | |
1477 | } | |
1478 | EXPORT_SYMBOL_GPL(nvmem_device_cell_write); | |
1479 | ||
1480 | /** | |
1481 | * nvmem_device_read() - Read from a given nvmem device | |
1482 | * | |
1483 | * @nvmem: nvmem device to read from. | |
1484 | * @offset: offset in nvmem device. | |
1485 | * @bytes: number of bytes to read. | |
1486 | * @buf: buffer pointer which will be populated on successful read. | |
1487 | * | |
1488 | * Return: length of successful bytes read on success and negative | |
1489 | * error code on error. | |
1490 | */ | |
1491 | int nvmem_device_read(struct nvmem_device *nvmem, | |
1492 | unsigned int offset, | |
1493 | size_t bytes, void *buf) | |
1494 | { | |
1495 | int rc; | |
1496 | ||
795ddd18 | 1497 | if (!nvmem) |
e2a5402e SK |
1498 | return -EINVAL; |
1499 | ||
795ddd18 | 1500 | rc = nvmem_reg_read(nvmem, offset, buf, bytes); |
e2a5402e | 1501 | |
287980e4 | 1502 | if (rc) |
e2a5402e SK |
1503 | return rc; |
1504 | ||
1505 | return bytes; | |
1506 | } | |
1507 | EXPORT_SYMBOL_GPL(nvmem_device_read); | |
1508 | ||
1509 | /** | |
1510 | * nvmem_device_write() - Write cell to a given nvmem device | |
1511 | * | |
1512 | * @nvmem: nvmem device to be written to. | |
1513 | * @offset: offset in nvmem device. | |
1514 | * @bytes: number of bytes to write. | |
1515 | * @buf: buffer to be written. | |
1516 | * | |
1517 | * Return: length of bytes written or negative error code on failure. | |
48f63a2c | 1518 | */ |
e2a5402e SK |
1519 | int nvmem_device_write(struct nvmem_device *nvmem, |
1520 | unsigned int offset, | |
1521 | size_t bytes, void *buf) | |
1522 | { | |
1523 | int rc; | |
1524 | ||
795ddd18 | 1525 | if (!nvmem) |
e2a5402e SK |
1526 | return -EINVAL; |
1527 | ||
795ddd18 | 1528 | rc = nvmem_reg_write(nvmem, offset, buf, bytes); |
e2a5402e | 1529 | |
287980e4 | 1530 | if (rc) |
e2a5402e SK |
1531 | return rc; |
1532 | ||
1533 | ||
1534 | return bytes; | |
1535 | } | |
1536 | EXPORT_SYMBOL_GPL(nvmem_device_write); | |
1537 | ||
b985f4cb BG |
1538 | /** |
1539 | * nvmem_add_cell_table() - register a table of cell info entries | |
1540 | * | |
1541 | * @table: table of cell info entries | |
1542 | */ | |
1543 | void nvmem_add_cell_table(struct nvmem_cell_table *table) | |
1544 | { | |
1545 | mutex_lock(&nvmem_cell_mutex); | |
1546 | list_add_tail(&table->node, &nvmem_cell_tables); | |
1547 | mutex_unlock(&nvmem_cell_mutex); | |
1548 | } | |
1549 | EXPORT_SYMBOL_GPL(nvmem_add_cell_table); | |
1550 | ||
1551 | /** | |
1552 | * nvmem_del_cell_table() - remove a previously registered cell info table | |
1553 | * | |
1554 | * @table: table of cell info entries | |
1555 | */ | |
1556 | void nvmem_del_cell_table(struct nvmem_cell_table *table) | |
1557 | { | |
1558 | mutex_lock(&nvmem_cell_mutex); | |
1559 | list_del(&table->node); | |
1560 | mutex_unlock(&nvmem_cell_mutex); | |
1561 | } | |
1562 | EXPORT_SYMBOL_GPL(nvmem_del_cell_table); | |
1563 | ||
506157be BG |
1564 | /** |
1565 | * nvmem_add_cell_lookups() - register a list of cell lookup entries | |
1566 | * | |
1567 | * @entries: array of cell lookup entries | |
1568 | * @nentries: number of cell lookup entries in the array | |
1569 | */ | |
1570 | void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries) | |
1571 | { | |
1572 | int i; | |
1573 | ||
1574 | mutex_lock(&nvmem_lookup_mutex); | |
1575 | for (i = 0; i < nentries; i++) | |
1576 | list_add_tail(&entries[i].node, &nvmem_lookup_list); | |
1577 | mutex_unlock(&nvmem_lookup_mutex); | |
1578 | } | |
1579 | EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups); | |
1580 | ||
1581 | /** | |
1582 | * nvmem_del_cell_lookups() - remove a list of previously added cell lookup | |
1583 | * entries | |
1584 | * | |
1585 | * @entries: array of cell lookup entries | |
1586 | * @nentries: number of cell lookup entries in the array | |
1587 | */ | |
1588 | void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries) | |
1589 | { | |
1590 | int i; | |
1591 | ||
1592 | mutex_lock(&nvmem_lookup_mutex); | |
1593 | for (i = 0; i < nentries; i++) | |
1594 | list_del(&entries[i].node); | |
1595 | mutex_unlock(&nvmem_lookup_mutex); | |
1596 | } | |
1597 | EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups); | |
1598 | ||
d7b9fd16 BG |
1599 | /** |
1600 | * nvmem_dev_name() - Get the name of a given nvmem device. | |
1601 | * | |
1602 | * @nvmem: nvmem device. | |
1603 | * | |
1604 | * Return: name of the nvmem device. | |
1605 | */ | |
1606 | const char *nvmem_dev_name(struct nvmem_device *nvmem) | |
1607 | { | |
1608 | return dev_name(&nvmem->dev); | |
1609 | } | |
1610 | EXPORT_SYMBOL_GPL(nvmem_dev_name); | |
1611 | ||
eace75cf SK |
1612 | static int __init nvmem_init(void) |
1613 | { | |
1614 | return bus_register(&nvmem_bus_type); | |
1615 | } | |
1616 | ||
1617 | static void __exit nvmem_exit(void) | |
1618 | { | |
1619 | bus_unregister(&nvmem_bus_type); | |
1620 | } | |
1621 | ||
1622 | subsys_initcall(nvmem_init); | |
1623 | module_exit(nvmem_exit); | |
1624 | ||
1625 | MODULE_AUTHOR("Srinivas Kandagatla <[email protected]"); | |
1626 | MODULE_AUTHOR("Maxime Ripard <[email protected]"); | |
1627 | MODULE_DESCRIPTION("nvmem Driver Core"); | |
1628 | MODULE_LICENSE("GPL v2"); |