]>
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 */ | |
70 | if (pos > nvmem->size) | |
71 | return 0; | |
72 | ||
73 | if (pos + count > nvmem->size) | |
74 | count = nvmem->size - pos; | |
75 | ||
76 | count = round_down(count, nvmem->word_size); | |
77 | ||
78 | rc = regmap_raw_read(nvmem->regmap, pos, buf, count); | |
79 | ||
80 | if (IS_ERR_VALUE(rc)) | |
81 | return rc; | |
82 | ||
83 | return count; | |
84 | } | |
85 | ||
86 | static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj, | |
87 | struct bin_attribute *attr, | |
88 | char *buf, loff_t pos, size_t count) | |
89 | { | |
90 | struct device *dev = container_of(kobj, struct device, kobj); | |
91 | struct nvmem_device *nvmem = to_nvmem_device(dev); | |
92 | int rc; | |
93 | ||
94 | /* Stop the user from writing */ | |
95 | if (pos > nvmem->size) | |
96 | return 0; | |
97 | ||
98 | if (pos + count > nvmem->size) | |
99 | count = nvmem->size - pos; | |
100 | ||
101 | count = round_down(count, nvmem->word_size); | |
102 | ||
103 | rc = regmap_raw_write(nvmem->regmap, pos, buf, count); | |
104 | ||
105 | if (IS_ERR_VALUE(rc)) | |
106 | return rc; | |
107 | ||
108 | return count; | |
109 | } | |
110 | ||
111 | /* default read/write permissions */ | |
112 | static struct bin_attribute bin_attr_rw_nvmem = { | |
113 | .attr = { | |
114 | .name = "nvmem", | |
115 | .mode = S_IWUSR | S_IRUGO, | |
116 | }, | |
117 | .read = bin_attr_nvmem_read, | |
118 | .write = bin_attr_nvmem_write, | |
119 | }; | |
120 | ||
121 | static struct bin_attribute *nvmem_bin_rw_attributes[] = { | |
122 | &bin_attr_rw_nvmem, | |
123 | NULL, | |
124 | }; | |
125 | ||
126 | static const struct attribute_group nvmem_bin_rw_group = { | |
127 | .bin_attrs = nvmem_bin_rw_attributes, | |
128 | }; | |
129 | ||
130 | static const struct attribute_group *nvmem_rw_dev_groups[] = { | |
131 | &nvmem_bin_rw_group, | |
132 | NULL, | |
133 | }; | |
134 | ||
135 | /* read only permission */ | |
136 | static struct bin_attribute bin_attr_ro_nvmem = { | |
137 | .attr = { | |
138 | .name = "nvmem", | |
139 | .mode = S_IRUGO, | |
140 | }, | |
141 | .read = bin_attr_nvmem_read, | |
142 | }; | |
143 | ||
144 | static struct bin_attribute *nvmem_bin_ro_attributes[] = { | |
145 | &bin_attr_ro_nvmem, | |
146 | NULL, | |
147 | }; | |
148 | ||
149 | static const struct attribute_group nvmem_bin_ro_group = { | |
150 | .bin_attrs = nvmem_bin_ro_attributes, | |
151 | }; | |
152 | ||
153 | static const struct attribute_group *nvmem_ro_dev_groups[] = { | |
154 | &nvmem_bin_ro_group, | |
155 | NULL, | |
156 | }; | |
157 | ||
158 | static void nvmem_release(struct device *dev) | |
159 | { | |
160 | struct nvmem_device *nvmem = to_nvmem_device(dev); | |
161 | ||
162 | ida_simple_remove(&nvmem_ida, nvmem->id); | |
163 | kfree(nvmem); | |
164 | } | |
165 | ||
166 | static const struct device_type nvmem_provider_type = { | |
167 | .release = nvmem_release, | |
168 | }; | |
169 | ||
170 | static struct bus_type nvmem_bus_type = { | |
171 | .name = "nvmem", | |
172 | }; | |
173 | ||
174 | static int of_nvmem_match(struct device *dev, void *nvmem_np) | |
175 | { | |
176 | return dev->of_node == nvmem_np; | |
177 | } | |
178 | ||
179 | static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np) | |
180 | { | |
181 | struct device *d; | |
182 | ||
183 | if (!nvmem_np) | |
184 | return NULL; | |
185 | ||
186 | d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match); | |
187 | ||
188 | if (!d) | |
189 | return NULL; | |
190 | ||
191 | return to_nvmem_device(d); | |
192 | } | |
193 | ||
194 | static struct nvmem_cell *nvmem_find_cell(const char *cell_id) | |
195 | { | |
196 | struct nvmem_cell *p; | |
197 | ||
198 | list_for_each_entry(p, &nvmem_cells, node) | |
199 | if (p && !strcmp(p->name, cell_id)) | |
200 | return p; | |
201 | ||
202 | return NULL; | |
203 | } | |
204 | ||
205 | static void nvmem_cell_drop(struct nvmem_cell *cell) | |
206 | { | |
207 | mutex_lock(&nvmem_cells_mutex); | |
208 | list_del(&cell->node); | |
209 | mutex_unlock(&nvmem_cells_mutex); | |
210 | kfree(cell); | |
211 | } | |
212 | ||
213 | static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem) | |
214 | { | |
215 | struct nvmem_cell *cell; | |
216 | struct list_head *p, *n; | |
217 | ||
218 | list_for_each_safe(p, n, &nvmem_cells) { | |
219 | cell = list_entry(p, struct nvmem_cell, node); | |
220 | if (cell->nvmem == nvmem) | |
221 | nvmem_cell_drop(cell); | |
222 | } | |
223 | } | |
224 | ||
225 | static void nvmem_cell_add(struct nvmem_cell *cell) | |
226 | { | |
227 | mutex_lock(&nvmem_cells_mutex); | |
228 | list_add_tail(&cell->node, &nvmem_cells); | |
229 | mutex_unlock(&nvmem_cells_mutex); | |
230 | } | |
231 | ||
232 | static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, | |
233 | const struct nvmem_cell_info *info, | |
234 | struct nvmem_cell *cell) | |
235 | { | |
236 | cell->nvmem = nvmem; | |
237 | cell->offset = info->offset; | |
238 | cell->bytes = info->bytes; | |
239 | cell->name = info->name; | |
240 | ||
241 | cell->bit_offset = info->bit_offset; | |
242 | cell->nbits = info->nbits; | |
243 | ||
244 | if (cell->nbits) | |
245 | cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, | |
246 | BITS_PER_BYTE); | |
247 | ||
248 | if (!IS_ALIGNED(cell->offset, nvmem->stride)) { | |
249 | dev_err(&nvmem->dev, | |
250 | "cell %s unaligned to nvmem stride %d\n", | |
251 | cell->name, nvmem->stride); | |
252 | return -EINVAL; | |
253 | } | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
258 | static int nvmem_add_cells(struct nvmem_device *nvmem, | |
259 | const struct nvmem_config *cfg) | |
260 | { | |
261 | struct nvmem_cell **cells; | |
262 | const struct nvmem_cell_info *info = cfg->cells; | |
263 | int i, rval; | |
264 | ||
265 | cells = kcalloc(cfg->ncells, sizeof(*cells), GFP_KERNEL); | |
266 | if (!cells) | |
267 | return -ENOMEM; | |
268 | ||
269 | for (i = 0; i < cfg->ncells; i++) { | |
270 | cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL); | |
271 | if (!cells[i]) { | |
272 | rval = -ENOMEM; | |
273 | goto err; | |
274 | } | |
275 | ||
276 | rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]); | |
277 | if (IS_ERR_VALUE(rval)) { | |
278 | kfree(cells[i]); | |
279 | goto err; | |
280 | } | |
281 | ||
282 | nvmem_cell_add(cells[i]); | |
283 | } | |
284 | ||
285 | nvmem->ncells = cfg->ncells; | |
286 | /* remove tmp array */ | |
287 | kfree(cells); | |
288 | ||
289 | return 0; | |
290 | err: | |
291 | while (--i) | |
292 | nvmem_cell_drop(cells[i]); | |
293 | ||
294 | return rval; | |
295 | } | |
296 | ||
297 | /** | |
298 | * nvmem_register() - Register a nvmem device for given nvmem_config. | |
299 | * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem | |
300 | * | |
301 | * @config: nvmem device configuration with which nvmem device is created. | |
302 | * | |
303 | * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device | |
304 | * on success. | |
305 | */ | |
306 | ||
307 | struct nvmem_device *nvmem_register(const struct nvmem_config *config) | |
308 | { | |
309 | struct nvmem_device *nvmem; | |
310 | struct device_node *np; | |
311 | struct regmap *rm; | |
312 | int rval; | |
313 | ||
314 | if (!config->dev) | |
315 | return ERR_PTR(-EINVAL); | |
316 | ||
317 | rm = dev_get_regmap(config->dev, NULL); | |
318 | if (!rm) { | |
319 | dev_err(config->dev, "Regmap not found\n"); | |
320 | return ERR_PTR(-EINVAL); | |
321 | } | |
322 | ||
323 | nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL); | |
324 | if (!nvmem) | |
325 | return ERR_PTR(-ENOMEM); | |
326 | ||
327 | rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL); | |
328 | if (rval < 0) { | |
329 | kfree(nvmem); | |
330 | return ERR_PTR(rval); | |
331 | } | |
332 | ||
333 | nvmem->id = rval; | |
334 | nvmem->regmap = rm; | |
335 | nvmem->owner = config->owner; | |
336 | nvmem->stride = regmap_get_reg_stride(rm); | |
337 | nvmem->word_size = regmap_get_val_bytes(rm); | |
338 | nvmem->size = regmap_get_max_register(rm) + nvmem->stride; | |
339 | nvmem->dev.type = &nvmem_provider_type; | |
340 | nvmem->dev.bus = &nvmem_bus_type; | |
341 | nvmem->dev.parent = config->dev; | |
342 | np = config->dev->of_node; | |
343 | nvmem->dev.of_node = np; | |
344 | dev_set_name(&nvmem->dev, "%s%d", | |
345 | config->name ? : "nvmem", config->id); | |
346 | ||
347 | nvmem->read_only = of_property_read_bool(np, "read-only") | | |
348 | config->read_only; | |
349 | ||
350 | nvmem->dev.groups = nvmem->read_only ? nvmem_ro_dev_groups : | |
351 | nvmem_rw_dev_groups; | |
352 | ||
353 | device_initialize(&nvmem->dev); | |
354 | ||
355 | dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); | |
356 | ||
357 | rval = device_add(&nvmem->dev); | |
358 | if (rval) { | |
359 | ida_simple_remove(&nvmem_ida, nvmem->id); | |
360 | kfree(nvmem); | |
361 | return ERR_PTR(rval); | |
362 | } | |
363 | ||
364 | if (config->cells) | |
365 | nvmem_add_cells(nvmem, config); | |
366 | ||
367 | return nvmem; | |
368 | } | |
369 | EXPORT_SYMBOL_GPL(nvmem_register); | |
370 | ||
371 | /** | |
372 | * nvmem_unregister() - Unregister previously registered nvmem device | |
373 | * | |
374 | * @nvmem: Pointer to previously registered nvmem device. | |
375 | * | |
376 | * Return: Will be an negative on error or a zero on success. | |
377 | */ | |
378 | int nvmem_unregister(struct nvmem_device *nvmem) | |
379 | { | |
69aba794 SK |
380 | mutex_lock(&nvmem_mutex); |
381 | if (nvmem->users) { | |
382 | mutex_unlock(&nvmem_mutex); | |
eace75cf | 383 | return -EBUSY; |
69aba794 SK |
384 | } |
385 | mutex_unlock(&nvmem_mutex); | |
eace75cf SK |
386 | |
387 | nvmem_device_remove_all_cells(nvmem); | |
388 | device_del(&nvmem->dev); | |
389 | ||
390 | return 0; | |
391 | } | |
392 | EXPORT_SYMBOL_GPL(nvmem_unregister); | |
393 | ||
69aba794 SK |
394 | static struct nvmem_device *__nvmem_device_get(struct device_node *np, |
395 | struct nvmem_cell **cellp, | |
396 | const char *cell_id) | |
397 | { | |
398 | struct nvmem_device *nvmem = NULL; | |
399 | ||
400 | mutex_lock(&nvmem_mutex); | |
401 | ||
402 | if (np) { | |
403 | nvmem = of_nvmem_find(np); | |
404 | if (!nvmem) { | |
405 | mutex_unlock(&nvmem_mutex); | |
406 | return ERR_PTR(-EPROBE_DEFER); | |
407 | } | |
408 | } else { | |
409 | struct nvmem_cell *cell = nvmem_find_cell(cell_id); | |
410 | ||
411 | if (cell) { | |
412 | nvmem = cell->nvmem; | |
413 | *cellp = cell; | |
414 | } | |
415 | ||
416 | if (!nvmem) { | |
417 | mutex_unlock(&nvmem_mutex); | |
418 | return ERR_PTR(-ENOENT); | |
419 | } | |
420 | } | |
421 | ||
422 | nvmem->users++; | |
423 | mutex_unlock(&nvmem_mutex); | |
424 | ||
425 | if (!try_module_get(nvmem->owner)) { | |
426 | dev_err(&nvmem->dev, | |
427 | "could not increase module refcount for cell %s\n", | |
428 | nvmem->name); | |
429 | ||
430 | mutex_lock(&nvmem_mutex); | |
431 | nvmem->users--; | |
432 | mutex_unlock(&nvmem_mutex); | |
433 | ||
434 | return ERR_PTR(-EINVAL); | |
435 | } | |
436 | ||
437 | return nvmem; | |
438 | } | |
439 | ||
440 | static void __nvmem_device_put(struct nvmem_device *nvmem) | |
441 | { | |
442 | module_put(nvmem->owner); | |
443 | mutex_lock(&nvmem_mutex); | |
444 | nvmem->users--; | |
445 | mutex_unlock(&nvmem_mutex); | |
446 | } | |
447 | ||
448 | static struct nvmem_cell *nvmem_cell_get_from_list(const char *cell_id) | |
449 | { | |
450 | struct nvmem_cell *cell = NULL; | |
451 | struct nvmem_device *nvmem; | |
452 | ||
453 | nvmem = __nvmem_device_get(NULL, &cell, cell_id); | |
454 | if (IS_ERR(nvmem)) | |
455 | return ERR_CAST(nvmem); | |
456 | ||
457 | return cell; | |
458 | } | |
459 | ||
460 | #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF) | |
461 | /** | |
462 | * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id | |
463 | * | |
464 | * @dev node: Device tree node that uses the nvmem cell | |
465 | * @id: nvmem cell name from nvmem-cell-names property. | |
466 | * | |
467 | * Return: Will be an ERR_PTR() on error or a valid pointer | |
468 | * to a struct nvmem_cell. The nvmem_cell will be freed by the | |
469 | * nvmem_cell_put(). | |
470 | */ | |
471 | struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, | |
472 | const char *name) | |
473 | { | |
474 | struct device_node *cell_np, *nvmem_np; | |
475 | struct nvmem_cell *cell; | |
476 | struct nvmem_device *nvmem; | |
477 | const __be32 *addr; | |
478 | int rval, len, index; | |
479 | ||
480 | index = of_property_match_string(np, "nvmem-cell-names", name); | |
481 | ||
482 | cell_np = of_parse_phandle(np, "nvmem-cells", index); | |
483 | if (!cell_np) | |
484 | return ERR_PTR(-EINVAL); | |
485 | ||
486 | nvmem_np = of_get_next_parent(cell_np); | |
487 | if (!nvmem_np) | |
488 | return ERR_PTR(-EINVAL); | |
489 | ||
490 | nvmem = __nvmem_device_get(nvmem_np, NULL, NULL); | |
491 | if (IS_ERR(nvmem)) | |
492 | return ERR_CAST(nvmem); | |
493 | ||
494 | addr = of_get_property(cell_np, "reg", &len); | |
495 | if (!addr || (len < 2 * sizeof(u32))) { | |
496 | dev_err(&nvmem->dev, "nvmem: invalid reg on %s\n", | |
497 | cell_np->full_name); | |
498 | rval = -EINVAL; | |
499 | goto err_mem; | |
500 | } | |
501 | ||
502 | cell = kzalloc(sizeof(*cell), GFP_KERNEL); | |
503 | if (!cell) { | |
504 | rval = -ENOMEM; | |
505 | goto err_mem; | |
506 | } | |
507 | ||
508 | cell->nvmem = nvmem; | |
509 | cell->offset = be32_to_cpup(addr++); | |
510 | cell->bytes = be32_to_cpup(addr); | |
511 | cell->name = cell_np->name; | |
512 | ||
513 | addr = of_get_property(cell_np, "bits", &len); | |
514 | if (addr && len == (2 * sizeof(u32))) { | |
515 | cell->bit_offset = be32_to_cpup(addr++); | |
516 | cell->nbits = be32_to_cpup(addr); | |
517 | } | |
518 | ||
519 | if (cell->nbits) | |
520 | cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, | |
521 | BITS_PER_BYTE); | |
522 | ||
523 | if (!IS_ALIGNED(cell->offset, nvmem->stride)) { | |
524 | dev_err(&nvmem->dev, | |
525 | "cell %s unaligned to nvmem stride %d\n", | |
526 | cell->name, nvmem->stride); | |
527 | rval = -EINVAL; | |
528 | goto err_sanity; | |
529 | } | |
530 | ||
531 | nvmem_cell_add(cell); | |
532 | ||
533 | return cell; | |
534 | ||
535 | err_sanity: | |
536 | kfree(cell); | |
537 | ||
538 | err_mem: | |
539 | __nvmem_device_put(nvmem); | |
540 | ||
541 | return ERR_PTR(rval); | |
542 | } | |
543 | EXPORT_SYMBOL_GPL(of_nvmem_cell_get); | |
544 | #endif | |
545 | ||
546 | /** | |
547 | * nvmem_cell_get() - Get nvmem cell of device form a given cell name | |
548 | * | |
549 | * @dev node: Device tree node that uses the nvmem cell | |
550 | * @id: nvmem cell name to get. | |
551 | * | |
552 | * Return: Will be an ERR_PTR() on error or a valid pointer | |
553 | * to a struct nvmem_cell. The nvmem_cell will be freed by the | |
554 | * nvmem_cell_put(). | |
555 | */ | |
556 | struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *cell_id) | |
557 | { | |
558 | struct nvmem_cell *cell; | |
559 | ||
560 | if (dev->of_node) { /* try dt first */ | |
561 | cell = of_nvmem_cell_get(dev->of_node, cell_id); | |
562 | if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER) | |
563 | return cell; | |
564 | } | |
565 | ||
566 | return nvmem_cell_get_from_list(cell_id); | |
567 | } | |
568 | EXPORT_SYMBOL_GPL(nvmem_cell_get); | |
569 | ||
570 | static void devm_nvmem_cell_release(struct device *dev, void *res) | |
571 | { | |
572 | nvmem_cell_put(*(struct nvmem_cell **)res); | |
573 | } | |
574 | ||
575 | /** | |
576 | * devm_nvmem_cell_get() - Get nvmem cell of device form a given id | |
577 | * | |
578 | * @dev node: Device tree node that uses the nvmem cell | |
579 | * @id: nvmem id in nvmem-names property. | |
580 | * | |
581 | * Return: Will be an ERR_PTR() on error or a valid pointer | |
582 | * to a struct nvmem_cell. The nvmem_cell will be freed by the | |
583 | * automatically once the device is freed. | |
584 | */ | |
585 | struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id) | |
586 | { | |
587 | struct nvmem_cell **ptr, *cell; | |
588 | ||
589 | ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL); | |
590 | if (!ptr) | |
591 | return ERR_PTR(-ENOMEM); | |
592 | ||
593 | cell = nvmem_cell_get(dev, id); | |
594 | if (!IS_ERR(cell)) { | |
595 | *ptr = cell; | |
596 | devres_add(dev, ptr); | |
597 | } else { | |
598 | devres_free(ptr); | |
599 | } | |
600 | ||
601 | return cell; | |
602 | } | |
603 | EXPORT_SYMBOL_GPL(devm_nvmem_cell_get); | |
604 | ||
605 | static int devm_nvmem_cell_match(struct device *dev, void *res, void *data) | |
606 | { | |
607 | struct nvmem_cell **c = res; | |
608 | ||
609 | if (WARN_ON(!c || !*c)) | |
610 | return 0; | |
611 | ||
612 | return *c == data; | |
613 | } | |
614 | ||
615 | /** | |
616 | * devm_nvmem_cell_put() - Release previously allocated nvmem cell | |
617 | * from devm_nvmem_cell_get. | |
618 | * | |
619 | * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get() | |
620 | */ | |
621 | void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell) | |
622 | { | |
623 | int ret; | |
624 | ||
625 | ret = devres_release(dev, devm_nvmem_cell_release, | |
626 | devm_nvmem_cell_match, cell); | |
627 | ||
628 | WARN_ON(ret); | |
629 | } | |
630 | EXPORT_SYMBOL(devm_nvmem_cell_put); | |
631 | ||
632 | /** | |
633 | * nvmem_cell_put() - Release previously allocated nvmem cell. | |
634 | * | |
635 | * @cell: Previously allocated nvmem cell by nvmem_cell_get() | |
636 | */ | |
637 | void nvmem_cell_put(struct nvmem_cell *cell) | |
638 | { | |
639 | struct nvmem_device *nvmem = cell->nvmem; | |
640 | ||
641 | __nvmem_device_put(nvmem); | |
642 | nvmem_cell_drop(cell); | |
643 | } | |
644 | EXPORT_SYMBOL_GPL(nvmem_cell_put); | |
645 | ||
646 | static inline void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, | |
647 | void *buf) | |
648 | { | |
649 | u8 *p, *b; | |
650 | int i, bit_offset = cell->bit_offset; | |
651 | ||
652 | p = b = buf; | |
653 | if (bit_offset) { | |
654 | /* First shift */ | |
655 | *b++ >>= bit_offset; | |
656 | ||
657 | /* setup rest of the bytes if any */ | |
658 | for (i = 1; i < cell->bytes; i++) { | |
659 | /* Get bits from next byte and shift them towards msb */ | |
660 | *p |= *b << (BITS_PER_BYTE - bit_offset); | |
661 | ||
662 | p = b; | |
663 | *b++ >>= bit_offset; | |
664 | } | |
665 | ||
666 | /* result fits in less bytes */ | |
667 | if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE)) | |
668 | *p-- = 0; | |
669 | } | |
670 | /* clear msb bits if any leftover in the last byte */ | |
671 | *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0); | |
672 | } | |
673 | ||
674 | static int __nvmem_cell_read(struct nvmem_device *nvmem, | |
675 | struct nvmem_cell *cell, | |
676 | void *buf, size_t *len) | |
677 | { | |
678 | int rc; | |
679 | ||
680 | rc = regmap_raw_read(nvmem->regmap, cell->offset, buf, cell->bytes); | |
681 | ||
682 | if (IS_ERR_VALUE(rc)) | |
683 | return rc; | |
684 | ||
685 | /* shift bits in-place */ | |
686 | if (cell->bit_offset || cell->bit_offset) | |
687 | nvmem_shift_read_buffer_in_place(cell, buf); | |
688 | ||
689 | *len = cell->bytes; | |
690 | ||
691 | return 0; | |
692 | } | |
693 | ||
694 | /** | |
695 | * nvmem_cell_read() - Read a given nvmem cell | |
696 | * | |
697 | * @cell: nvmem cell to be read. | |
698 | * @len: pointer to length of cell which will be populated on successful read. | |
699 | * | |
700 | * Return: ERR_PTR() on error or a valid pointer to a char * buffer on success. | |
701 | * The buffer should be freed by the consumer with a kfree(). | |
702 | */ | |
703 | void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len) | |
704 | { | |
705 | struct nvmem_device *nvmem = cell->nvmem; | |
706 | u8 *buf; | |
707 | int rc; | |
708 | ||
709 | if (!nvmem || !nvmem->regmap) | |
710 | return ERR_PTR(-EINVAL); | |
711 | ||
712 | buf = kzalloc(cell->bytes, GFP_KERNEL); | |
713 | if (!buf) | |
714 | return ERR_PTR(-ENOMEM); | |
715 | ||
716 | rc = __nvmem_cell_read(nvmem, cell, buf, len); | |
717 | if (IS_ERR_VALUE(rc)) { | |
718 | kfree(buf); | |
719 | return ERR_PTR(rc); | |
720 | } | |
721 | ||
722 | return buf; | |
723 | } | |
724 | EXPORT_SYMBOL_GPL(nvmem_cell_read); | |
725 | ||
726 | static inline void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell, | |
727 | u8 *_buf, int len) | |
728 | { | |
729 | struct nvmem_device *nvmem = cell->nvmem; | |
730 | int i, rc, nbits, bit_offset = cell->bit_offset; | |
731 | u8 v, *p, *buf, *b, pbyte, pbits; | |
732 | ||
733 | nbits = cell->nbits; | |
734 | buf = kzalloc(cell->bytes, GFP_KERNEL); | |
735 | if (!buf) | |
736 | return ERR_PTR(-ENOMEM); | |
737 | ||
738 | memcpy(buf, _buf, len); | |
739 | p = b = buf; | |
740 | ||
741 | if (bit_offset) { | |
742 | pbyte = *b; | |
743 | *b <<= bit_offset; | |
744 | ||
745 | /* setup the first byte with lsb bits from nvmem */ | |
746 | rc = regmap_raw_read(nvmem->regmap, cell->offset, &v, 1); | |
747 | *b++ |= GENMASK(bit_offset - 1, 0) & v; | |
748 | ||
749 | /* setup rest of the byte if any */ | |
750 | for (i = 1; i < cell->bytes; i++) { | |
751 | /* Get last byte bits and shift them towards lsb */ | |
752 | pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset); | |
753 | pbyte = *b; | |
754 | p = b; | |
755 | *b <<= bit_offset; | |
756 | *b++ |= pbits; | |
757 | } | |
758 | } | |
759 | ||
760 | /* if it's not end on byte boundary */ | |
761 | if ((nbits + bit_offset) % BITS_PER_BYTE) { | |
762 | /* setup the last byte with msb bits from nvmem */ | |
763 | rc = regmap_raw_read(nvmem->regmap, | |
764 | cell->offset + cell->bytes - 1, &v, 1); | |
765 | *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v; | |
766 | ||
767 | } | |
768 | ||
769 | return buf; | |
770 | } | |
771 | ||
772 | /** | |
773 | * nvmem_cell_write() - Write to a given nvmem cell | |
774 | * | |
775 | * @cell: nvmem cell to be written. | |
776 | * @buf: Buffer to be written. | |
777 | * @len: length of buffer to be written to nvmem cell. | |
778 | * | |
779 | * Return: length of bytes written or negative on failure. | |
780 | */ | |
781 | int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len) | |
782 | { | |
783 | struct nvmem_device *nvmem = cell->nvmem; | |
784 | int rc; | |
785 | ||
786 | if (!nvmem || !nvmem->regmap || nvmem->read_only || | |
787 | (cell->bit_offset == 0 && len != cell->bytes)) | |
788 | return -EINVAL; | |
789 | ||
790 | if (cell->bit_offset || cell->nbits) { | |
791 | buf = nvmem_cell_prepare_write_buffer(cell, buf, len); | |
792 | if (IS_ERR(buf)) | |
793 | return PTR_ERR(buf); | |
794 | } | |
795 | ||
796 | rc = regmap_raw_write(nvmem->regmap, cell->offset, buf, cell->bytes); | |
797 | ||
798 | /* free the tmp buffer */ | |
799 | if (cell->bit_offset) | |
800 | kfree(buf); | |
801 | ||
802 | if (IS_ERR_VALUE(rc)) | |
803 | return rc; | |
804 | ||
805 | return len; | |
806 | } | |
807 | EXPORT_SYMBOL_GPL(nvmem_cell_write); | |
808 | ||
eace75cf SK |
809 | static int __init nvmem_init(void) |
810 | { | |
811 | return bus_register(&nvmem_bus_type); | |
812 | } | |
813 | ||
814 | static void __exit nvmem_exit(void) | |
815 | { | |
816 | bus_unregister(&nvmem_bus_type); | |
817 | } | |
818 | ||
819 | subsys_initcall(nvmem_init); | |
820 | module_exit(nvmem_exit); | |
821 | ||
822 | MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org"); | |
823 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com"); | |
824 | MODULE_DESCRIPTION("nvmem Driver Core"); | |
825 | MODULE_LICENSE("GPL v2"); |