1 // SPDX-License-Identifier: GPL-2.0
3 * RTC subsystem, nvmem interface
5 * Copyright (C) 2017 Alexandre Belloni
9 #include <linux/types.h>
10 #include <linux/nvmem-consumer.h>
11 #include <linux/rtc.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
16 * Deprecated ABI compatibility, this should be removed at some point
19 static const char nvram_warning
[] = "Deprecated ABI, please use nvmem";
22 rtc_nvram_read(struct file
*filp
, struct kobject
*kobj
,
23 struct bin_attribute
*attr
,
24 char *buf
, loff_t off
, size_t count
)
26 dev_warn_once(kobj_to_dev(kobj
), nvram_warning
);
28 return nvmem_device_read(attr
->private, off
, count
, buf
);
32 rtc_nvram_write(struct file
*filp
, struct kobject
*kobj
,
33 struct bin_attribute
*attr
,
34 char *buf
, loff_t off
, size_t count
)
36 dev_warn_once(kobj_to_dev(kobj
), nvram_warning
);
38 return nvmem_device_write(attr
->private, off
, count
, buf
);
41 static int rtc_nvram_register(struct rtc_device
*rtc
,
42 struct nvmem_device
*nvmem
, size_t size
)
46 rtc
->nvram
= kzalloc(sizeof(*rtc
->nvram
), GFP_KERNEL
);
50 rtc
->nvram
->attr
.name
= "nvram";
51 rtc
->nvram
->attr
.mode
= 0644;
52 rtc
->nvram
->private = nvmem
;
54 sysfs_bin_attr_init(rtc
->nvram
);
56 rtc
->nvram
->read
= rtc_nvram_read
;
57 rtc
->nvram
->write
= rtc_nvram_write
;
58 rtc
->nvram
->size
= size
;
60 err
= sysfs_create_bin_file(&rtc
->dev
.parent
->kobj
,
70 static void rtc_nvram_unregister(struct rtc_device
*rtc
)
72 sysfs_remove_bin_file(&rtc
->dev
.parent
->kobj
, rtc
->nvram
);
80 int rtc_nvmem_register(struct rtc_device
*rtc
,
81 struct nvmem_config
*nvmem_config
)
83 struct nvmem_device
*nvmem
;
88 nvmem_config
->dev
= rtc
->dev
.parent
;
89 nvmem_config
->owner
= rtc
->owner
;
90 nvmem
= devm_nvmem_register(rtc
->dev
.parent
, nvmem_config
);
92 return PTR_ERR(nvmem
);
94 /* Register the old ABI */
95 if (rtc
->nvram_old_abi
)
96 rtc_nvram_register(rtc
, nvmem
, nvmem_config
->size
);
100 EXPORT_SYMBOL_GPL(rtc_nvmem_register
);
102 void rtc_nvmem_unregister(struct rtc_device
*rtc
)
104 /* unregister the old ABI */
106 rtc_nvram_unregister(rtc
);