| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * RTC subsystem, nvmem interface |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2017 Alexandre Belloni |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 8 | | - * published by the Free Software Foundation. |
|---|
| 9 | 6 | */ |
|---|
| 10 | 7 | |
|---|
| 11 | 8 | #include <linux/err.h> |
|---|
| 12 | 9 | #include <linux/types.h> |
|---|
| 13 | 10 | #include <linux/nvmem-consumer.h> |
|---|
| 14 | 11 | #include <linux/rtc.h> |
|---|
| 12 | +#include <linux/slab.h> |
|---|
| 15 | 13 | #include <linux/sysfs.h> |
|---|
| 16 | 14 | |
|---|
| 17 | 15 | /* |
|---|
| .. | .. |
|---|
| 25 | 23 | struct bin_attribute *attr, |
|---|
| 26 | 24 | char *buf, loff_t off, size_t count) |
|---|
| 27 | 25 | { |
|---|
| 28 | | - struct rtc_device *rtc = attr->private; |
|---|
| 29 | | - |
|---|
| 30 | 26 | dev_warn_once(kobj_to_dev(kobj), nvram_warning); |
|---|
| 31 | 27 | |
|---|
| 32 | | - return nvmem_device_read(rtc->nvmem, off, count, buf); |
|---|
| 28 | + return nvmem_device_read(attr->private, off, count, buf); |
|---|
| 33 | 29 | } |
|---|
| 34 | 30 | |
|---|
| 35 | 31 | static ssize_t |
|---|
| .. | .. |
|---|
| 37 | 33 | struct bin_attribute *attr, |
|---|
| 38 | 34 | char *buf, loff_t off, size_t count) |
|---|
| 39 | 35 | { |
|---|
| 40 | | - struct rtc_device *rtc = attr->private; |
|---|
| 41 | | - |
|---|
| 42 | 36 | dev_warn_once(kobj_to_dev(kobj), nvram_warning); |
|---|
| 43 | 37 | |
|---|
| 44 | | - return nvmem_device_write(rtc->nvmem, off, count, buf); |
|---|
| 38 | + return nvmem_device_write(attr->private, off, count, buf); |
|---|
| 45 | 39 | } |
|---|
| 46 | 40 | |
|---|
| 47 | | -static int rtc_nvram_register(struct rtc_device *rtc, size_t size) |
|---|
| 41 | +static int rtc_nvram_register(struct rtc_device *rtc, |
|---|
| 42 | + struct nvmem_device *nvmem, size_t size) |
|---|
| 48 | 43 | { |
|---|
| 49 | 44 | int err; |
|---|
| 50 | 45 | |
|---|
| 51 | | - rtc->nvram = devm_kzalloc(rtc->dev.parent, |
|---|
| 52 | | - sizeof(struct bin_attribute), |
|---|
| 53 | | - GFP_KERNEL); |
|---|
| 46 | + rtc->nvram = kzalloc(sizeof(*rtc->nvram), GFP_KERNEL); |
|---|
| 54 | 47 | if (!rtc->nvram) |
|---|
| 55 | 48 | return -ENOMEM; |
|---|
| 56 | 49 | |
|---|
| 57 | 50 | rtc->nvram->attr.name = "nvram"; |
|---|
| 58 | 51 | rtc->nvram->attr.mode = 0644; |
|---|
| 59 | | - rtc->nvram->private = rtc; |
|---|
| 52 | + rtc->nvram->private = nvmem; |
|---|
| 60 | 53 | |
|---|
| 61 | 54 | sysfs_bin_attr_init(rtc->nvram); |
|---|
| 62 | 55 | |
|---|
| .. | .. |
|---|
| 67 | 60 | err = sysfs_create_bin_file(&rtc->dev.parent->kobj, |
|---|
| 68 | 61 | rtc->nvram); |
|---|
| 69 | 62 | if (err) { |
|---|
| 70 | | - devm_kfree(rtc->dev.parent, rtc->nvram); |
|---|
| 63 | + kfree(rtc->nvram); |
|---|
| 71 | 64 | rtc->nvram = NULL; |
|---|
| 72 | 65 | } |
|---|
| 73 | 66 | |
|---|
| .. | .. |
|---|
| 77 | 70 | static void rtc_nvram_unregister(struct rtc_device *rtc) |
|---|
| 78 | 71 | { |
|---|
| 79 | 72 | sysfs_remove_bin_file(&rtc->dev.parent->kobj, rtc->nvram); |
|---|
| 73 | + kfree(rtc->nvram); |
|---|
| 74 | + rtc->nvram = NULL; |
|---|
| 80 | 75 | } |
|---|
| 81 | 76 | |
|---|
| 82 | 77 | /* |
|---|
| .. | .. |
|---|
| 85 | 80 | int rtc_nvmem_register(struct rtc_device *rtc, |
|---|
| 86 | 81 | struct nvmem_config *nvmem_config) |
|---|
| 87 | 82 | { |
|---|
| 88 | | - if (!IS_ERR_OR_NULL(rtc->nvmem)) |
|---|
| 89 | | - return -EBUSY; |
|---|
| 83 | + struct nvmem_device *nvmem; |
|---|
| 90 | 84 | |
|---|
| 91 | 85 | if (!nvmem_config) |
|---|
| 92 | 86 | return -ENODEV; |
|---|
| 93 | 87 | |
|---|
| 94 | 88 | nvmem_config->dev = rtc->dev.parent; |
|---|
| 95 | 89 | nvmem_config->owner = rtc->owner; |
|---|
| 96 | | - rtc->nvmem = nvmem_register(nvmem_config); |
|---|
| 97 | | - if (IS_ERR(rtc->nvmem)) |
|---|
| 98 | | - return PTR_ERR(rtc->nvmem); |
|---|
| 90 | + nvmem = devm_nvmem_register(rtc->dev.parent, nvmem_config); |
|---|
| 91 | + if (IS_ERR(nvmem)) |
|---|
| 92 | + return PTR_ERR(nvmem); |
|---|
| 99 | 93 | |
|---|
| 100 | 94 | /* Register the old ABI */ |
|---|
| 101 | 95 | if (rtc->nvram_old_abi) |
|---|
| 102 | | - rtc_nvram_register(rtc, nvmem_config->size); |
|---|
| 96 | + rtc_nvram_register(rtc, nvmem, nvmem_config->size); |
|---|
| 103 | 97 | |
|---|
| 104 | 98 | return 0; |
|---|
| 105 | 99 | } |
|---|
| .. | .. |
|---|
| 107 | 101 | |
|---|
| 108 | 102 | void rtc_nvmem_unregister(struct rtc_device *rtc) |
|---|
| 109 | 103 | { |
|---|
| 110 | | - if (IS_ERR_OR_NULL(rtc->nvmem)) |
|---|
| 111 | | - return; |
|---|
| 112 | | - |
|---|
| 113 | 104 | /* unregister the old ABI */ |
|---|
| 114 | 105 | if (rtc->nvram) |
|---|
| 115 | 106 | rtc_nvram_unregister(rtc); |
|---|
| 116 | | - |
|---|
| 117 | | - nvmem_unregister(rtc->nvmem); |
|---|
| 118 | 107 | } |
|---|