hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/rtc/nvmem.c
....@@ -1,17 +1,15 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * RTC subsystem, nvmem interface
34 *
45 * 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.
96 */
107
118 #include <linux/err.h>
129 #include <linux/types.h>
1310 #include <linux/nvmem-consumer.h>
1411 #include <linux/rtc.h>
12
+#include <linux/slab.h>
1513 #include <linux/sysfs.h>
1614
1715 /*
....@@ -25,11 +23,9 @@
2523 struct bin_attribute *attr,
2624 char *buf, loff_t off, size_t count)
2725 {
28
- struct rtc_device *rtc = attr->private;
29
-
3026 dev_warn_once(kobj_to_dev(kobj), nvram_warning);
3127
32
- return nvmem_device_read(rtc->nvmem, off, count, buf);
28
+ return nvmem_device_read(attr->private, off, count, buf);
3329 }
3430
3531 static ssize_t
....@@ -37,26 +33,23 @@
3733 struct bin_attribute *attr,
3834 char *buf, loff_t off, size_t count)
3935 {
40
- struct rtc_device *rtc = attr->private;
41
-
4236 dev_warn_once(kobj_to_dev(kobj), nvram_warning);
4337
44
- return nvmem_device_write(rtc->nvmem, off, count, buf);
38
+ return nvmem_device_write(attr->private, off, count, buf);
4539 }
4640
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)
4843 {
4944 int err;
5045
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);
5447 if (!rtc->nvram)
5548 return -ENOMEM;
5649
5750 rtc->nvram->attr.name = "nvram";
5851 rtc->nvram->attr.mode = 0644;
59
- rtc->nvram->private = rtc;
52
+ rtc->nvram->private = nvmem;
6053
6154 sysfs_bin_attr_init(rtc->nvram);
6255
....@@ -67,7 +60,7 @@
6760 err = sysfs_create_bin_file(&rtc->dev.parent->kobj,
6861 rtc->nvram);
6962 if (err) {
70
- devm_kfree(rtc->dev.parent, rtc->nvram);
63
+ kfree(rtc->nvram);
7164 rtc->nvram = NULL;
7265 }
7366
....@@ -77,6 +70,8 @@
7770 static void rtc_nvram_unregister(struct rtc_device *rtc)
7871 {
7972 sysfs_remove_bin_file(&rtc->dev.parent->kobj, rtc->nvram);
73
+ kfree(rtc->nvram);
74
+ rtc->nvram = NULL;
8075 }
8176
8277 /*
....@@ -85,21 +80,20 @@
8580 int rtc_nvmem_register(struct rtc_device *rtc,
8681 struct nvmem_config *nvmem_config)
8782 {
88
- if (!IS_ERR_OR_NULL(rtc->nvmem))
89
- return -EBUSY;
83
+ struct nvmem_device *nvmem;
9084
9185 if (!nvmem_config)
9286 return -ENODEV;
9387
9488 nvmem_config->dev = rtc->dev.parent;
9589 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);
9993
10094 /* Register the old ABI */
10195 if (rtc->nvram_old_abi)
102
- rtc_nvram_register(rtc, nvmem_config->size);
96
+ rtc_nvram_register(rtc, nvmem, nvmem_config->size);
10397
10498 return 0;
10599 }
....@@ -107,12 +101,7 @@
107101
108102 void rtc_nvmem_unregister(struct rtc_device *rtc)
109103 {
110
- if (IS_ERR_OR_NULL(rtc->nvmem))
111
- return;
112
-
113104 /* unregister the old ABI */
114105 if (rtc->nvram)
115106 rtc_nvram_unregister(rtc);
116
-
117
- nvmem_unregister(rtc->nvmem);
118107 }