2 * RTC subsystem, nvmem interface
4 * Copyright (C) 2017 Alexandre Belloni
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.
11 #include <linux/err.h>
12 #include <linux/types.h>
13 #include <linux/nvmem-consumer.h>
14 #include <linux/rtc.h>
15 #include <linux/sysfs.h>
20 * Deprecated ABI compatibility, this should be removed at some point
23 static const char nvram_warning[] = "Deprecated ABI, please use nvmem";
26 rtc_nvram_read(struct file *filp, struct kobject *kobj,
27 struct bin_attribute *attr,
28 char *buf, loff_t off, size_t count)
30 struct rtc_device *rtc = attr->private;
32 dev_warn_once(kobj_to_dev(kobj), nvram_warning);
34 return nvmem_device_read(rtc->nvmem, off, count, buf);
38 rtc_nvram_write(struct file *filp, struct kobject *kobj,
39 struct bin_attribute *attr,
40 char *buf, loff_t off, size_t count)
42 struct rtc_device *rtc = attr->private;
44 dev_warn_once(kobj_to_dev(kobj), nvram_warning);
46 return nvmem_device_write(rtc->nvmem, off, count, buf);
49 static int rtc_nvram_register(struct rtc_device *rtc)
53 rtc->nvram = devm_kzalloc(rtc->dev.parent,
54 sizeof(struct bin_attribute),
59 rtc->nvram->attr.name = "nvram";
60 rtc->nvram->attr.mode = 0644;
61 rtc->nvram->private = rtc;
63 sysfs_bin_attr_init(rtc->nvram);
65 rtc->nvram->read = rtc_nvram_read;
66 rtc->nvram->write = rtc_nvram_write;
67 rtc->nvram->size = rtc->nvmem_config->size;
69 err = sysfs_create_bin_file(&rtc->dev.parent->kobj,
72 devm_kfree(rtc->dev.parent, rtc->nvram);
79 static void rtc_nvram_unregister(struct rtc_device *rtc)
81 sysfs_remove_bin_file(&rtc->dev.parent->kobj, rtc->nvram);
87 void rtc_nvmem_register(struct rtc_device *rtc)
89 if (!rtc->nvmem_config)
92 rtc->nvmem_config->dev = &rtc->dev;
93 rtc->nvmem_config->owner = rtc->owner;
94 rtc->nvmem = nvmem_register(rtc->nvmem_config);
95 if (IS_ERR_OR_NULL(rtc->nvmem))
98 /* Register the old ABI */
99 if (rtc->nvram_old_abi)
100 rtc_nvram_register(rtc);
103 void rtc_nvmem_unregister(struct rtc_device *rtc)
105 if (IS_ERR_OR_NULL(rtc->nvmem))
108 /* unregister the old ABI */
110 rtc_nvram_unregister(rtc);
112 nvmem_unregister(rtc->nvmem);