Commit | Line | Data |
---|---|---|
eace75cf SK |
1 | /* |
2 | * nvmem framework core. | |
3 | * | |
4 | * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org> | |
5 | * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 and | |
9 | * only version 2 as published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | */ | |
16 | ||
17 | #include <linux/device.h> | |
18 | #include <linux/export.h> | |
19 | #include <linux/fs.h> | |
20 | #include <linux/idr.h> | |
21 | #include <linux/init.h> | |
22 | #include <linux/module.h> | |
23 | #include <linux/nvmem-consumer.h> | |
24 | #include <linux/nvmem-provider.h> | |
25 | #include <linux/of.h> | |
26 | #include <linux/regmap.h> | |
27 | #include <linux/slab.h> | |
28 | ||
29 | struct nvmem_device { | |
30 | const char *name; | |
31 | struct regmap *regmap; | |
32 | struct module *owner; | |
33 | struct device dev; | |
34 | int stride; | |
35 | int word_size; | |
36 | int ncells; | |
37 | int id; | |
38 | int users; | |
39 | size_t size; | |
40 | bool read_only; | |
41 | }; | |
42 | ||
43 | struct nvmem_cell { | |
44 | const char *name; | |
45 | int offset; | |
46 | int bytes; | |
47 | int bit_offset; | |
48 | int nbits; | |
49 | struct nvmem_device *nvmem; | |
50 | struct list_head node; | |
51 | }; | |
52 | ||
53 | static DEFINE_MUTEX(nvmem_mutex); | |
54 | static DEFINE_IDA(nvmem_ida); | |
55 | ||
56 | static LIST_HEAD(nvmem_cells); | |
57 | static DEFINE_MUTEX(nvmem_cells_mutex); | |
58 | ||
59 | #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev) | |
60 | ||
61 | static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj, | |
62 | struct bin_attribute *attr, | |
63 | char *buf, loff_t pos, size_t count) | |
64 | { | |
65 | struct device *dev = container_of(kobj, struct device, kobj); | |
66 | struct nvmem_device *nvmem = to_nvmem_device(dev); | |
67 | int rc; | |
68 | ||
69 | /* Stop the user from reading */ | |
7c806883 | 70 | if (pos >= nvmem->size) |
eace75cf SK |
71 | return 0; |
72 | ||
313a72ff SK |
73 | if (count < nvmem->word_size) |
74 | return -EINVAL; | |
75 | ||
eace75cf SK |
76 | if (pos + count > nvmem->size) |
77 | count = nvmem->size - pos; | |
78 | ||
79 | count = round_down(count, nvmem->word_size); | |
80 | ||
81 | rc = regmap_raw_read(nvmem->regmap, pos, buf, count); | |
82 | ||
83 | if (IS_ERR_VALUE(rc)) | |
84 | return rc; | |
85 | ||
86 | return count; | |
87 | } | |
88 | ||
89 | static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj, | |
90 | struct bin_attribute *attr, | |
91 | char *buf, loff_t pos, size_t count) | |
92 | { | |
93 | struct device *dev = container_of(kobj, struct device, kobj); | |
94 | struct nvmem_device *nvmem = to_nvmem_device(dev); | |
95 | int rc; | |
96 | ||
97 | /* Stop the user from writing */ | |
7c806883 | 98 | if (pos >= nvmem->size) |
eace75cf SK |
99 | return 0; |
100 | ||
313a72ff SK |
101 | if (count < nvmem->word_size) |
102 | return -EINVAL; | |
103 | ||
eace75cf SK |
104 | if (pos + count > nvmem->size) |
105 | count = nvmem->size - pos; | |
106 | ||
107 | count = round_down(count, nvmem->word_size); | |
108 | ||
109 | rc = regmap_raw_write(nvmem->regmap, pos, buf, count); | |
110 | ||
111 | if (IS_ERR_VALUE(rc)) | |
112 | return rc; | |
113 | ||
114 | return count; | |
115 | } | |
116 | ||
117 | /* default read/write permissions */ | |
118 | static struct bin_attribute bin_attr_rw_nvmem = { | |
119 | .attr = { | |
120 | .name = "nvmem", | |
121 | .mode = S_IWUSR | S_IRUGO, | |
122 | }, | |
123 | .read = bin_attr_nvmem_read, | |
124 | .write = bin_attr_nvmem_write, | |
125 | }; | |
126 | ||
127 | static struct bin_attribute *nvmem_bin_rw_attributes[] = { | |
128 | &bin_attr_rw_nvmem, | |
129 | NULL, | |
130 | }; | |
131 | ||
132 | static const struct attribute_group nvmem_bin_rw_group = { | |
133 | .bin_attrs = nvmem_bin_rw_attributes, | |
134 | }; | |
135 | ||
136 | static const struct attribute_group *nvmem_rw_dev_groups[] = { | |
137 | &nvmem_bin_rw_group, | |
138 | NULL, | |
139 | }; | |
140 | ||
141 | /* read only permission */ | |
142 | static struct bin_attribute bin_attr_ro_nvmem = { | |
143 | .attr = { | |
144 | .name = "nvmem", | |
145 | .mode = S_IRUGO, | |
146 | }, | |
147 | .read = bin_attr_nvmem_read, | |
148 | }; | |
149 | ||
150 | static struct bin_attribute *nvmem_bin_ro_attributes[] = { | |
151 | &bin_attr_ro_nvmem, | |
152 | NULL, | |
153 | }; | |
154 | ||
155 | static const struct attribute_group nvmem_bin_ro_group = { | |
156 | .bin_attrs = nvmem_bin_ro_attributes, | |
157 | }; | |
158 | ||
159 | static const struct attribute_group *nvmem_ro_dev_groups[] = { | |
160 | &nvmem_bin_ro_group, | |
161 | NULL, | |
162 | }; | |
163 | ||
811b0d65 AL |
164 | /* default read/write permissions, root only */ |
165 | static struct bin_attribute bin_attr_rw_root_nvmem = { | |
166 | .attr = { | |
167 | .name = "nvmem", | |
168 | .mode = S_IWUSR | S_IRUSR, | |
169 | }, | |
170 | .read = bin_attr_nvmem_read, | |
171 | .write = bin_attr_nvmem_write, | |
172 | }; | |
173 | ||
174 | static struct bin_attribute *nvmem_bin_rw_root_attributes[] = { | |
175 | &bin_attr_rw_root_nvmem, | |
176 | NULL, | |
177 | }; | |
178 | ||
179 | static const struct attribute_group nvmem_bin_rw_root_group = { | |
180 | .bin_attrs = nvmem_bin_rw_root_attributes, | |
181 | }; | |
182 | ||
183 | static const struct attribute_group *nvmem_rw_root_dev_groups[] = { | |
184 | &nvmem_bin_rw_root_group, | |
185 | NULL, | |
186 | }; | |
187 | ||
188 | /* read only permission, root only */ | |
189 | static struct bin_attribute bin_attr_ro_root_nvmem = { | |
190 | .attr = { | |
191 | .name = "nvmem", | |
192 | .mode = S_IRUSR, | |
193 | }, | |
194 | .read = bin_attr_nvmem_read, | |
195 | }; | |
196 | ||
197 | static struct bin_attribute *nvmem_bin_ro_root_attributes[] = { | |
198 | &bin_attr_ro_root_nvmem, | |
199 | NULL, | |
200 | }; | |
201 | ||
202 | static const struct attribute_group nvmem_bin_ro_root_group = { | |
203 | .bin_attrs = nvmem_bin_ro_root_attributes, | |
204 | }; | |
205 | ||
206 | static const struct attribute_group *nvmem_ro_root_dev_groups[] = { | |
207 | &nvmem_bin_ro_root_group, | |
208 | NULL, | |
209 | }; | |
210 | ||
eace75cf SK |
211 | static void nvmem_release(struct device *dev) |
212 | { | |
213 | struct nvmem_device *nvmem = to_nvmem_device(dev); | |
214 | ||
215 | ida_simple_remove(&nvmem_ida, nvmem->id); | |
216 | kfree(nvmem); | |
217 | } | |
218 | ||
219 | static const struct device_type nvmem_provider_type = { | |
220 | .release = nvmem_release, | |
221 | }; | |
222 | ||
223 | static struct bus_type nvmem_bus_type = { | |
224 | .name = "nvmem", | |
225 | }; | |
226 | ||
227 | static int of_nvmem_match(struct device *dev, void *nvmem_np) | |
228 | { | |
229 | return dev->of_node == nvmem_np; | |
230 | } | |
231 | ||
232 | static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np) | |
233 | { | |
234 | struct device *d; | |
235 | ||
236 | if (!nvmem_np) | |
237 | return NULL; | |
238 | ||
239 | d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match); | |
240 | ||
241 | if (!d) | |
242 | return NULL; | |
243 | ||
244 | return to_nvmem_device(d); | |
245 | } | |
246 | ||
247 | static struct nvmem_cell *nvmem_find_cell(const char *cell_id) | |
248 | { | |
249 | struct nvmem_cell *p; | |
250 | ||
251 | list_for_each_entry(p, &nvmem_cells, node) | |
252 | if (p && !strcmp(p->name, cell_id)) | |
253 | return p; | |
254 | ||
255 | return NULL; | |
256 | } | |
257 | ||
258 | static void nvmem_cell_drop(struct nvmem_cell *cell) | |
259 | { | |
260 | mutex_lock(&nvmem_cells_mutex); | |
261 | list_del(&cell->node); | |
262 | mutex_unlock(&nvmem_cells_mutex); | |
263 | kfree(cell); | |
264 | } | |
265 | ||
266 | static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem) | |
267 | { | |
268 | struct nvmem_cell *cell; | |
269 | struct list_head *p, *n; | |
270 | ||
271 | list_for_each_safe(p, n, &nvmem_cells) { | |
272 | cell = list_entry(p, struct nvmem_cell, node); | |
273 | if (cell->nvmem == nvmem) | |
274 | nvmem_cell_drop(cell); | |
275 | } | |
276 | } | |
277 | ||
278 | static void nvmem_cell_add(struct nvmem_cell *cell) | |
279 | { | |
280 | mutex_lock(&nvmem_cells_mutex); | |
281 | list_add_tail(&cell->node, &nvmem_cells); | |
282 | mutex_unlock(&nvmem_cells_mutex); | |
283 | } | |
284 | ||
285 | static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, | |
286 | const struct nvmem_cell_info *info, | |
287 | struct nvmem_cell *cell) | |
288 | { | |
289 | cell->nvmem = nvmem; | |
290 | cell->offset = info->offset; | |
291 | cell->bytes = info->bytes; | |
292 | cell->name = info->name; | |
293 | ||
294 | cell->bit_offset = info->bit_offset; | |
295 | cell->nbits = info->nbits; | |
296 | ||
297 | if (cell->nbits) | |
298 | cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, | |
299 | BITS_PER_BYTE); | |
300 | ||
301 | if (!IS_ALIGNED(cell->offset, nvmem->stride)) { | |
302 | dev_err(&nvmem->dev, | |
303 | "cell %s unaligned to nvmem stride %d\n", | |
304 | cell->name, nvmem->stride); | |
305 | return -EINVAL; | |
306 | } | |
307 | ||
308 | return 0; | |
309 | } | |
310 | ||
311 | static int nvmem_add_cells(struct nvmem_device *nvmem, | |
312 | const struct nvmem_config *cfg) | |
313 | { | |
314 | struct nvmem_cell **cells; | |
315 | const struct nvmem_cell_info *info = cfg->cells; | |
316 | int i, rval; | |
317 | ||
318 | cells = kcalloc(cfg->ncells, sizeof(*cells), GFP_KERNEL); | |
319 | if (!cells) | |
320 | return -ENOMEM; | |
321 | ||
322 | for (i = 0; i < cfg->ncells; i++) { | |
323 | cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL); | |
324 | if (!cells[i]) { | |
325 | rval = -ENOMEM; | |
326 | goto err; | |
327 | } | |
328 | ||
329 | rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]); | |
330 | if (IS_ERR_VALUE(rval)) { | |
331 | kfree(cells[i]); | |
332 | goto err; | |
333 | } | |
334 | ||
335 | nvmem_cell_add(cells[i]); | |
336 | } | |
337 | ||
338 | nvmem->ncells = cfg->ncells; | |
339 | /* remove tmp array */ | |
340 | kfree(cells); | |
341 | ||
342 | return 0; | |
343 | err: | |
dfdf1414 | 344 | while (i--) |
eace75cf SK |
345 | nvmem_cell_drop(cells[i]); |
346 | ||
dfdf1414 RV |
347 | kfree(cells); |
348 | ||
eace75cf SK |
349 | return rval; |
350 | } | |
351 | ||
352 | /** | |
353 | * nvmem_register() - Register a nvmem device for given nvmem_config. | |
354 | * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem | |
355 | * | |
356 | * @config: nvmem device configuration with which nvmem device is created. | |
357 | * | |
358 | * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device | |
359 | * on success. | |
360 | */ | |
361 | ||
362 | struct nvmem_device *nvmem_register(const struct nvmem_config *config) | |
363 | { | |
364 | struct nvmem_device *nvmem; | |
365 | struct device_node *np; | |
366 | struct regmap *rm; | |
367 | int rval; | |
368 | ||
369 | if (!config->dev) | |
370 | return ERR_PTR(-EINVAL); | |
371 | ||
372 | rm = dev_get_regmap(config->dev, NULL); | |
373 | if (!rm) { | |
374 | dev_err(config->dev, "Regmap not found\n"); | |
375 | return ERR_PTR(-EINVAL); | |
376 | } | |
377 | ||
378 | nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL); | |
379 | if (!nvmem) | |
380 | return ERR_PTR(-ENOMEM); | |
381 | ||
382 | rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL); | |
383 | if (rval < 0) { | |
384 | kfree(nvmem); | |
385 | return ERR_PTR(rval); | |
386 | } | |
387 | ||
388 | nvmem->id = rval; | |
389 | nvmem->regmap = rm; | |
390 | nvmem->owner = config->owner; | |
391 | nvmem->stride = regmap_get_reg_stride(rm); | |
392 | nvmem->word_size = regmap_get_val_bytes(rm); | |
393 | nvmem->size = regmap_get_max_register(rm) + nvmem->stride; | |
394 | nvmem->dev.type = &nvmem_provider_type; | |
395 | nvmem->dev.bus = &nvmem_bus_type; | |
396 | nvmem->dev.parent = config->dev; | |
397 | np = config->dev->of_node; | |
398 | nvmem->dev.of_node = np; | |
399 | dev_set_name(&nvmem->dev, "%s%d", | |
400 | config->name ? : "nvmem", config->id); | |
401 | ||
402 | nvmem->read_only = of_property_read_bool(np, "read-only") | | |
403 | config->read_only; | |
404 | ||
811b0d65 AL |
405 | if (config->root_only) |
406 | nvmem->dev.groups = nvmem->read_only ? | |
407 | nvmem_ro_root_dev_groups : | |
408 | nvmem_rw_root_dev_groups; | |
409 | else | |
410 | nvmem->dev.groups = nvmem->read_only ? | |
411 | nvmem_ro_dev_groups : | |
412 | nvmem_rw_dev_groups; | |
eace75cf SK |
413 | |
414 | device_initialize(&nvmem->dev); | |
415 | ||
416 | dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); | |
417 | ||
418 | rval = device_add(&nvmem->dev); | |
419 | if (rval) { | |
420 | ida_simple_remove(&nvmem_ida, nvmem->id); | |
421 | kfree(nvmem); | |
422 | return ERR_PTR(rval); | |
423 | } | |
424 | ||
425 | if (config->cells) | |
426 | nvmem_add_cells(nvmem, config); | |
427 | ||
428 | return nvmem; | |
429 | } | |
430 | EXPORT_SYMBOL_GPL(nvmem_register); | |
431 | ||
432 | /** | |
433 | * nvmem_unregister() - Unregister previously registered nvmem device | |
434 | * | |
435 | * @nvmem: Pointer to previously registered nvmem device. | |
436 | * | |
437 | * Return: Will be an negative on error or a zero on success. | |
438 | */ | |
439 | int nvmem_unregister(struct nvmem_device *nvmem) | |
440 | { | |
69aba794 SK |
441 | mutex_lock(&nvmem_mutex); |
442 | if (nvmem->users) { | |
443 | mutex_unlock(&nvmem_mutex); | |
eace75cf | 444 | return -EBUSY; |
69aba794 SK |
445 | } |
446 | mutex_unlock(&nvmem_mutex); | |
eace75cf SK |
447 | |
448 | nvmem_device_remove_all_cells(nvmem); | |
449 | device_del(&nvmem->dev); | |
450 | ||
451 | return 0; | |
452 | } | |
453 | EXPORT_SYMBOL_GPL(nvmem_unregister); | |
454 | ||
69aba794 SK |
455 | static struct nvmem_device *__nvmem_device_get(struct device_node *np, |
456 | struct nvmem_cell **cellp, | |
457 | const char *cell_id) | |
458 | { | |
459 | struct nvmem_device *nvmem = NULL; | |
460 | ||
461 | mutex_lock(&nvmem_mutex); | |
462 | ||
463 | if (np) { | |
464 | nvmem = of_nvmem_find(np); | |
465 | if (!nvmem) { | |
466 | mutex_unlock(&nvmem_mutex); | |
467 | return ERR_PTR(-EPROBE_DEFER); | |
468 | } | |
469 | } else { | |
470 | struct nvmem_cell *cell = nvmem_find_cell(cell_id); | |
471 | ||
472 | if (cell) { | |
473 | nvmem = cell->nvmem; | |
474 | *cellp = cell; | |
475 | } | |
476 | ||
477 | if (!nvmem) { | |
478 | mutex_unlock(&nvmem_mutex); | |
479 | return ERR_PTR(-ENOENT); | |
480 | } | |
481 | } | |
482 | ||
483 | nvmem->users++; | |
484 | mutex_unlock(&nvmem_mutex); | |
485 | ||
486 | if (!try_module_get(nvmem->owner)) { | |
487 | dev_err(&nvmem->dev, | |
488 | "could not increase module refcount for cell %s\n", | |
489 | nvmem->name); | |
490 | ||
491 | mutex_lock(&nvmem_mutex); | |
492 | nvmem->users--; | |
493 | mutex_unlock(&nvmem_mutex); | |
494 | ||
495 | return ERR_PTR(-EINVAL); | |
496 | } | |
497 | ||
498 | return nvmem; | |
499 | } | |
500 | ||
501 | static void __nvmem_device_put(struct nvmem_device *nvmem) | |
502 | { | |
503 | module_put(nvmem->owner); | |
504 | mutex_lock(&nvmem_mutex); | |
505 | nvmem->users--; | |
506 | mutex_unlock(&nvmem_mutex); | |
507 | } | |
508 | ||
e2a5402e SK |
509 | static int nvmem_match(struct device *dev, void *data) |
510 | { | |
511 | return !strcmp(dev_name(dev), data); | |
512 | } | |
513 | ||
514 | static struct nvmem_device *nvmem_find(const char *name) | |
515 | { | |
516 | struct device *d; | |
517 | ||
518 | d = bus_find_device(&nvmem_bus_type, NULL, (void *)name, nvmem_match); | |
519 | ||
520 | if (!d) | |
521 | return NULL; | |
522 | ||
523 | return to_nvmem_device(d); | |
524 | } | |
525 | ||
526 | #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF) | |
527 | /** | |
528 | * of_nvmem_device_get() - Get nvmem device from a given id | |
529 | * | |
530 | * @dev node: Device tree node that uses the nvmem device | |
531 | * @id: nvmem name from nvmem-names property. | |
532 | * | |
533 | * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device | |
534 | * on success. | |
535 | */ | |
536 | struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id) | |
537 | { | |
538 | ||
539 | struct device_node *nvmem_np; | |
540 | int index; | |
541 | ||
542 | index = of_property_match_string(np, "nvmem-names", id); | |
543 | ||
544 | nvmem_np = of_parse_phandle(np, "nvmem", index); | |
545 | if (!nvmem_np) | |
546 | return ERR_PTR(-EINVAL); | |
547 | ||
548 | return __nvmem_device_get(nvmem_np, NULL, NULL); | |
549 | } | |
550 | EXPORT_SYMBOL_GPL(of_nvmem_device_get); | |
551 | #endif | |
552 | ||
553 | /** | |
554 | * nvmem_device_get() - Get nvmem device from a given id | |
555 | * | |
556 | * @dev : Device that uses the nvmem device | |
557 | * @id: nvmem name from nvmem-names property. | |
558 | * | |
559 | * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device | |
560 | * on success. | |
561 | */ | |
562 | struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name) | |
563 | { | |
564 | if (dev->of_node) { /* try dt first */ | |
565 | struct nvmem_device *nvmem; | |
566 | ||
567 | nvmem = of_nvmem_device_get(dev->of_node, dev_name); | |
568 | ||
569 | if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER) | |
570 | return nvmem; | |
571 | ||
572 | } | |
573 | ||
574 | return nvmem_find(dev_name); | |
575 | } | |
576 | EXPORT_SYMBOL_GPL(nvmem_device_get); | |
577 | ||
578 | static int devm_nvmem_device_match(struct device *dev, void *res, void *data) | |
579 | { | |
580 | struct nvmem_device **nvmem = res; | |
581 | ||
582 | if (WARN_ON(!nvmem || !*nvmem)) | |
583 | return 0; | |
584 | ||
585 | return *nvmem == data; | |
586 | } | |
587 | ||
588 | static void devm_nvmem_device_release(struct device *dev, void *res) | |
589 | { | |
590 | nvmem_device_put(*(struct nvmem_device **)res); | |
591 | } | |
592 | ||
593 | /** | |
594 | * devm_nvmem_device_put() - put alredy got nvmem device | |
595 | * | |
596 | * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(), | |
597 | * that needs to be released. | |
598 | */ | |
599 | void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem) | |
600 | { | |
601 | int ret; | |
602 | ||
603 | ret = devres_release(dev, devm_nvmem_device_release, | |
604 | devm_nvmem_device_match, nvmem); | |
605 | ||
606 | WARN_ON(ret); | |
607 | } | |
608 | EXPORT_SYMBOL_GPL(devm_nvmem_device_put); | |
609 | ||
610 | /** | |
611 | * nvmem_device_put() - put alredy got nvmem device | |
612 | * | |
613 | * @nvmem: pointer to nvmem device that needs to be released. | |
614 | */ | |
615 | void nvmem_device_put(struct nvmem_device *nvmem) | |
616 | { | |
617 | __nvmem_device_put(nvmem); | |
618 | } | |
619 | EXPORT_SYMBOL_GPL(nvmem_device_put); | |
620 | ||
621 | /** | |
622 | * devm_nvmem_device_get() - Get nvmem cell of device form a given id | |
623 | * | |
624 | * @dev node: Device tree node that uses the nvmem cell | |
625 | * @id: nvmem name in nvmems property. | |
626 | * | |
627 | * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell | |
628 | * on success. The nvmem_cell will be freed by the automatically once the | |
629 | * device is freed. | |
630 | */ | |
631 | struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id) | |
632 | { | |
633 | struct nvmem_device **ptr, *nvmem; | |
634 | ||
635 | ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL); | |
636 | if (!ptr) | |
637 | return ERR_PTR(-ENOMEM); | |
638 | ||
639 | nvmem = nvmem_device_get(dev, id); | |
640 | if (!IS_ERR(nvmem)) { | |
641 | *ptr = nvmem; | |
642 | devres_add(dev, ptr); | |
643 | } else { | |
644 | devres_free(ptr); | |
645 | } | |
646 | ||
647 | return nvmem; | |
648 | } | |
649 | EXPORT_SYMBOL_GPL(devm_nvmem_device_get); | |
650 | ||
69aba794 SK |
651 | static struct nvmem_cell *nvmem_cell_get_from_list(const char *cell_id) |
652 | { | |
653 | struct nvmem_cell *cell = NULL; | |
654 | struct nvmem_device *nvmem; | |
655 | ||
656 | nvmem = __nvmem_device_get(NULL, &cell, cell_id); | |
657 | if (IS_ERR(nvmem)) | |
658 | return ERR_CAST(nvmem); | |
659 | ||
660 | return cell; | |
661 | } | |
662 | ||
663 | #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF) | |
664 | /** | |
665 | * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id | |
666 | * | |
667 | * @dev node: Device tree node that uses the nvmem cell | |
668 | * @id: nvmem cell name from nvmem-cell-names property. | |
669 | * | |
670 | * Return: Will be an ERR_PTR() on error or a valid pointer | |
671 | * to a struct nvmem_cell. The nvmem_cell will be freed by the | |
672 | * nvmem_cell_put(). | |
673 | */ | |
674 | struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, | |
675 | const char *name) | |
676 | { | |
677 | struct device_node *cell_np, *nvmem_np; | |
678 | struct nvmem_cell *cell; | |
679 | struct nvmem_device *nvmem; | |
680 | const __be32 *addr; | |
681 | int rval, len, index; | |
682 | ||
683 | index = of_property_match_string(np, "nvmem-cell-names", name); | |
684 | ||
685 | cell_np = of_parse_phandle(np, "nvmem-cells", index); | |
686 | if (!cell_np) | |
687 | return ERR_PTR(-EINVAL); | |
688 | ||
689 | nvmem_np = of_get_next_parent(cell_np); | |
690 | if (!nvmem_np) | |
691 | return ERR_PTR(-EINVAL); | |
692 | ||
693 | nvmem = __nvmem_device_get(nvmem_np, NULL, NULL); | |
694 | if (IS_ERR(nvmem)) | |
695 | return ERR_CAST(nvmem); | |
696 | ||
697 | addr = of_get_property(cell_np, "reg", &len); | |
698 | if (!addr || (len < 2 * sizeof(u32))) { | |
699 | dev_err(&nvmem->dev, "nvmem: invalid reg on %s\n", | |
700 | cell_np->full_name); | |
701 | rval = -EINVAL; | |
702 | goto err_mem; | |
703 | } | |
704 | ||
705 | cell = kzalloc(sizeof(*cell), GFP_KERNEL); | |
706 | if (!cell) { | |
707 | rval = -ENOMEM; | |
708 | goto err_mem; | |
709 | } | |
710 | ||
711 | cell->nvmem = nvmem; | |
712 | cell->offset = be32_to_cpup(addr++); | |
713 | cell->bytes = be32_to_cpup(addr); | |
714 | cell->name = cell_np->name; | |
715 | ||
716 | addr = of_get_property(cell_np, "bits", &len); | |
717 | if (addr && len == (2 * sizeof(u32))) { | |
718 | cell->bit_offset = be32_to_cpup(addr++); | |
719 | cell->nbits = be32_to_cpup(addr); | |
720 | } | |
721 | ||
722 | if (cell->nbits) | |
723 | cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, | |
724 | BITS_PER_BYTE); | |
725 | ||
726 | if (!IS_ALIGNED(cell->offset, nvmem->stride)) { | |
727 | dev_err(&nvmem->dev, | |
728 | "cell %s unaligned to nvmem stride %d\n", | |
729 | cell->name, nvmem->stride); | |
730 | rval = -EINVAL; | |
731 | goto err_sanity; | |
732 | } | |
733 | ||
734 | nvmem_cell_add(cell); | |
735 | ||
736 | return cell; | |
737 | ||
738 | err_sanity: | |
739 | kfree(cell); | |
740 | ||
741 | err_mem: | |
742 | __nvmem_device_put(nvmem); | |
743 | ||
744 | return ERR_PTR(rval); | |
745 | } | |
746 | EXPORT_SYMBOL_GPL(of_nvmem_cell_get); | |
747 | #endif | |
748 | ||
749 | /** | |
750 | * nvmem_cell_get() - Get nvmem cell of device form a given cell name | |
751 | * | |
752 | * @dev node: Device tree node that uses the nvmem cell | |
753 | * @id: nvmem cell name to get. | |
754 | * | |
755 | * Return: Will be an ERR_PTR() on error or a valid pointer | |
756 | * to a struct nvmem_cell. The nvmem_cell will be freed by the | |
757 | * nvmem_cell_put(). | |
758 | */ | |
759 | struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *cell_id) | |
760 | { | |
761 | struct nvmem_cell *cell; | |
762 | ||
763 | if (dev->of_node) { /* try dt first */ | |
764 | cell = of_nvmem_cell_get(dev->of_node, cell_id); | |
765 | if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER) | |
766 | return cell; | |
767 | } | |
768 | ||
769 | return nvmem_cell_get_from_list(cell_id); | |
770 | } | |
771 | EXPORT_SYMBOL_GPL(nvmem_cell_get); | |
772 | ||
773 | static void devm_nvmem_cell_release(struct device *dev, void *res) | |
774 | { | |
775 | nvmem_cell_put(*(struct nvmem_cell **)res); | |
776 | } | |
777 | ||
778 | /** | |
779 | * devm_nvmem_cell_get() - Get nvmem cell of device form a given id | |
780 | * | |
781 | * @dev node: Device tree node that uses the nvmem cell | |
782 | * @id: nvmem id in nvmem-names property. | |
783 | * | |
784 | * Return: Will be an ERR_PTR() on error or a valid pointer | |
785 | * to a struct nvmem_cell. The nvmem_cell will be freed by the | |
786 | * automatically once the device is freed. | |
787 | */ | |
788 | struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id) | |
789 | { | |
790 | struct nvmem_cell **ptr, *cell; | |
791 | ||
792 | ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL); | |
793 | if (!ptr) | |
794 | return ERR_PTR(-ENOMEM); | |
795 | ||
796 | cell = nvmem_cell_get(dev, id); | |
797 | if (!IS_ERR(cell)) { | |
798 | *ptr = cell; | |
799 | devres_add(dev, ptr); | |
800 | } else { | |
801 | devres_free(ptr); | |
802 | } | |
803 | ||
804 | return cell; | |
805 | } | |
806 | EXPORT_SYMBOL_GPL(devm_nvmem_cell_get); | |
807 | ||
808 | static int devm_nvmem_cell_match(struct device *dev, void *res, void *data) | |
809 | { | |
810 | struct nvmem_cell **c = res; | |
811 | ||
812 | if (WARN_ON(!c || !*c)) | |
813 | return 0; | |
814 | ||
815 | return *c == data; | |
816 | } | |
817 | ||
818 | /** | |
819 | * devm_nvmem_cell_put() - Release previously allocated nvmem cell | |
820 | * from devm_nvmem_cell_get. | |
821 | * | |
822 | * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get() | |
823 | */ | |
824 | void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell) | |
825 | { | |
826 | int ret; | |
827 | ||
828 | ret = devres_release(dev, devm_nvmem_cell_release, | |
829 | devm_nvmem_cell_match, cell); | |
830 | ||
831 | WARN_ON(ret); | |
832 | } | |
833 | EXPORT_SYMBOL(devm_nvmem_cell_put); | |
834 | ||
835 | /** | |
836 | * nvmem_cell_put() - Release previously allocated nvmem cell. | |
837 | * | |
838 | * @cell: Previously allocated nvmem cell by nvmem_cell_get() | |
839 | */ | |
840 | void nvmem_cell_put(struct nvmem_cell *cell) | |
841 | { | |
842 | struct nvmem_device *nvmem = cell->nvmem; | |
843 | ||
844 | __nvmem_device_put(nvmem); | |
845 | nvmem_cell_drop(cell); | |
846 | } | |
847 | EXPORT_SYMBOL_GPL(nvmem_cell_put); | |
848 | ||
849 | static inline void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, | |
850 | void *buf) | |
851 | { | |
852 | u8 *p, *b; | |
853 | int i, bit_offset = cell->bit_offset; | |
854 | ||
855 | p = b = buf; | |
856 | if (bit_offset) { | |
857 | /* First shift */ | |
858 | *b++ >>= bit_offset; | |
859 | ||
860 | /* setup rest of the bytes if any */ | |
861 | for (i = 1; i < cell->bytes; i++) { | |
862 | /* Get bits from next byte and shift them towards msb */ | |
863 | *p |= *b << (BITS_PER_BYTE - bit_offset); | |
864 | ||
865 | p = b; | |
866 | *b++ >>= bit_offset; | |
867 | } | |
868 | ||
869 | /* result fits in less bytes */ | |
870 | if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE)) | |
871 | *p-- = 0; | |
872 | } | |
873 | /* clear msb bits if any leftover in the last byte */ | |
874 | *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0); | |
875 | } | |
876 | ||
877 | static int __nvmem_cell_read(struct nvmem_device *nvmem, | |
878 | struct nvmem_cell *cell, | |
879 | void *buf, size_t *len) | |
880 | { | |
881 | int rc; | |
882 | ||
883 | rc = regmap_raw_read(nvmem->regmap, cell->offset, buf, cell->bytes); | |
884 | ||
885 | if (IS_ERR_VALUE(rc)) | |
886 | return rc; | |
887 | ||
888 | /* shift bits in-place */ | |
cbf854ab | 889 | if (cell->bit_offset || cell->nbits) |
69aba794 SK |
890 | nvmem_shift_read_buffer_in_place(cell, buf); |
891 | ||
892 | *len = cell->bytes; | |
893 | ||
894 | return 0; | |
895 | } | |
896 | ||
897 | /** | |
898 | * nvmem_cell_read() - Read a given nvmem cell | |
899 | * | |
900 | * @cell: nvmem cell to be read. | |
901 | * @len: pointer to length of cell which will be populated on successful read. | |
902 | * | |
903 | * Return: ERR_PTR() on error or a valid pointer to a char * buffer on success. | |
904 | * The buffer should be freed by the consumer with a kfree(). | |
905 | */ | |
906 | void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len) | |
907 | { | |
908 | struct nvmem_device *nvmem = cell->nvmem; | |
909 | u8 *buf; | |
910 | int rc; | |
911 | ||
912 | if (!nvmem || !nvmem->regmap) | |
913 | return ERR_PTR(-EINVAL); | |
914 | ||
915 | buf = kzalloc(cell->bytes, GFP_KERNEL); | |
916 | if (!buf) | |
917 | return ERR_PTR(-ENOMEM); | |
918 | ||
919 | rc = __nvmem_cell_read(nvmem, cell, buf, len); | |
920 | if (IS_ERR_VALUE(rc)) { | |
921 | kfree(buf); | |
922 | return ERR_PTR(rc); | |
923 | } | |
924 | ||
925 | return buf; | |
926 | } | |
927 | EXPORT_SYMBOL_GPL(nvmem_cell_read); | |
928 | ||
929 | static inline void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell, | |
930 | u8 *_buf, int len) | |
931 | { | |
932 | struct nvmem_device *nvmem = cell->nvmem; | |
933 | int i, rc, nbits, bit_offset = cell->bit_offset; | |
934 | u8 v, *p, *buf, *b, pbyte, pbits; | |
935 | ||
936 | nbits = cell->nbits; | |
937 | buf = kzalloc(cell->bytes, GFP_KERNEL); | |
938 | if (!buf) | |
939 | return ERR_PTR(-ENOMEM); | |
940 | ||
941 | memcpy(buf, _buf, len); | |
942 | p = b = buf; | |
943 | ||
944 | if (bit_offset) { | |
945 | pbyte = *b; | |
946 | *b <<= bit_offset; | |
947 | ||
948 | /* setup the first byte with lsb bits from nvmem */ | |
949 | rc = regmap_raw_read(nvmem->regmap, cell->offset, &v, 1); | |
950 | *b++ |= GENMASK(bit_offset - 1, 0) & v; | |
951 | ||
952 | /* setup rest of the byte if any */ | |
953 | for (i = 1; i < cell->bytes; i++) { | |
954 | /* Get last byte bits and shift them towards lsb */ | |
955 | pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset); | |
956 | pbyte = *b; | |
957 | p = b; | |
958 | *b <<= bit_offset; | |
959 | *b++ |= pbits; | |
960 | } | |
961 | } | |
962 | ||
963 | /* if it's not end on byte boundary */ | |
964 | if ((nbits + bit_offset) % BITS_PER_BYTE) { | |
965 | /* setup the last byte with msb bits from nvmem */ | |
966 | rc = regmap_raw_read(nvmem->regmap, | |
967 | cell->offset + cell->bytes - 1, &v, 1); | |
968 | *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v; | |
969 | ||
970 | } | |
971 | ||
972 | return buf; | |
973 | } | |
974 | ||
975 | /** | |
976 | * nvmem_cell_write() - Write to a given nvmem cell | |
977 | * | |
978 | * @cell: nvmem cell to be written. | |
979 | * @buf: Buffer to be written. | |
980 | * @len: length of buffer to be written to nvmem cell. | |
981 | * | |
982 | * Return: length of bytes written or negative on failure. | |
983 | */ | |
984 | int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len) | |
985 | { | |
986 | struct nvmem_device *nvmem = cell->nvmem; | |
987 | int rc; | |
988 | ||
989 | if (!nvmem || !nvmem->regmap || nvmem->read_only || | |
990 | (cell->bit_offset == 0 && len != cell->bytes)) | |
991 | return -EINVAL; | |
992 | ||
993 | if (cell->bit_offset || cell->nbits) { | |
994 | buf = nvmem_cell_prepare_write_buffer(cell, buf, len); | |
995 | if (IS_ERR(buf)) | |
996 | return PTR_ERR(buf); | |
997 | } | |
998 | ||
999 | rc = regmap_raw_write(nvmem->regmap, cell->offset, buf, cell->bytes); | |
1000 | ||
1001 | /* free the tmp buffer */ | |
ace22170 | 1002 | if (cell->bit_offset || cell->nbits) |
69aba794 SK |
1003 | kfree(buf); |
1004 | ||
1005 | if (IS_ERR_VALUE(rc)) | |
1006 | return rc; | |
1007 | ||
1008 | return len; | |
1009 | } | |
1010 | EXPORT_SYMBOL_GPL(nvmem_cell_write); | |
1011 | ||
e2a5402e SK |
1012 | /** |
1013 | * nvmem_device_cell_read() - Read a given nvmem device and cell | |
1014 | * | |
1015 | * @nvmem: nvmem device to read from. | |
1016 | * @info: nvmem cell info to be read. | |
1017 | * @buf: buffer pointer which will be populated on successful read. | |
1018 | * | |
1019 | * Return: length of successful bytes read on success and negative | |
1020 | * error code on error. | |
1021 | */ | |
1022 | ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem, | |
1023 | struct nvmem_cell_info *info, void *buf) | |
1024 | { | |
1025 | struct nvmem_cell cell; | |
1026 | int rc; | |
1027 | ssize_t len; | |
1028 | ||
1029 | if (!nvmem || !nvmem->regmap) | |
1030 | return -EINVAL; | |
1031 | ||
1032 | rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); | |
1033 | if (IS_ERR_VALUE(rc)) | |
1034 | return rc; | |
1035 | ||
1036 | rc = __nvmem_cell_read(nvmem, &cell, buf, &len); | |
1037 | if (IS_ERR_VALUE(rc)) | |
1038 | return rc; | |
1039 | ||
1040 | return len; | |
1041 | } | |
1042 | EXPORT_SYMBOL_GPL(nvmem_device_cell_read); | |
1043 | ||
1044 | /** | |
1045 | * nvmem_device_cell_write() - Write cell to a given nvmem device | |
1046 | * | |
1047 | * @nvmem: nvmem device to be written to. | |
1048 | * @info: nvmem cell info to be written | |
1049 | * @buf: buffer to be written to cell. | |
1050 | * | |
1051 | * Return: length of bytes written or negative error code on failure. | |
1052 | * */ | |
1053 | int nvmem_device_cell_write(struct nvmem_device *nvmem, | |
1054 | struct nvmem_cell_info *info, void *buf) | |
1055 | { | |
1056 | struct nvmem_cell cell; | |
1057 | int rc; | |
1058 | ||
1059 | if (!nvmem || !nvmem->regmap) | |
1060 | return -EINVAL; | |
1061 | ||
1062 | rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); | |
1063 | if (IS_ERR_VALUE(rc)) | |
1064 | return rc; | |
1065 | ||
1066 | return nvmem_cell_write(&cell, buf, cell.bytes); | |
1067 | } | |
1068 | EXPORT_SYMBOL_GPL(nvmem_device_cell_write); | |
1069 | ||
1070 | /** | |
1071 | * nvmem_device_read() - Read from a given nvmem device | |
1072 | * | |
1073 | * @nvmem: nvmem device to read from. | |
1074 | * @offset: offset in nvmem device. | |
1075 | * @bytes: number of bytes to read. | |
1076 | * @buf: buffer pointer which will be populated on successful read. | |
1077 | * | |
1078 | * Return: length of successful bytes read on success and negative | |
1079 | * error code on error. | |
1080 | */ | |
1081 | int nvmem_device_read(struct nvmem_device *nvmem, | |
1082 | unsigned int offset, | |
1083 | size_t bytes, void *buf) | |
1084 | { | |
1085 | int rc; | |
1086 | ||
1087 | if (!nvmem || !nvmem->regmap) | |
1088 | return -EINVAL; | |
1089 | ||
1090 | rc = regmap_raw_read(nvmem->regmap, offset, buf, bytes); | |
1091 | ||
1092 | if (IS_ERR_VALUE(rc)) | |
1093 | return rc; | |
1094 | ||
1095 | return bytes; | |
1096 | } | |
1097 | EXPORT_SYMBOL_GPL(nvmem_device_read); | |
1098 | ||
1099 | /** | |
1100 | * nvmem_device_write() - Write cell to a given nvmem device | |
1101 | * | |
1102 | * @nvmem: nvmem device to be written to. | |
1103 | * @offset: offset in nvmem device. | |
1104 | * @bytes: number of bytes to write. | |
1105 | * @buf: buffer to be written. | |
1106 | * | |
1107 | * Return: length of bytes written or negative error code on failure. | |
1108 | * */ | |
1109 | int nvmem_device_write(struct nvmem_device *nvmem, | |
1110 | unsigned int offset, | |
1111 | size_t bytes, void *buf) | |
1112 | { | |
1113 | int rc; | |
1114 | ||
1115 | if (!nvmem || !nvmem->regmap) | |
1116 | return -EINVAL; | |
1117 | ||
1118 | rc = regmap_raw_write(nvmem->regmap, offset, buf, bytes); | |
1119 | ||
1120 | if (IS_ERR_VALUE(rc)) | |
1121 | return rc; | |
1122 | ||
1123 | ||
1124 | return bytes; | |
1125 | } | |
1126 | EXPORT_SYMBOL_GPL(nvmem_device_write); | |
1127 | ||
eace75cf SK |
1128 | static int __init nvmem_init(void) |
1129 | { | |
1130 | return bus_register(&nvmem_bus_type); | |
1131 | } | |
1132 | ||
1133 | static void __exit nvmem_exit(void) | |
1134 | { | |
1135 | bus_unregister(&nvmem_bus_type); | |
1136 | } | |
1137 | ||
1138 | subsys_initcall(nvmem_init); | |
1139 | module_exit(nvmem_exit); | |
1140 | ||
1141 | MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org"); | |
1142 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com"); | |
1143 | MODULE_DESCRIPTION("nvmem Driver Core"); | |
1144 | MODULE_LICENSE("GPL v2"); |