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/slab.h>
16 #include <linux/sysfs.h>
19 * Deprecated ABI compatibility, this should be removed at some point
22 static const char nvram_warning[] = "Deprecated ABI, please use nvmem";
25 rtc_nvram_read(struct file *filp, struct kobject *kobj,
26 struct bin_attribute *attr,
27 char *buf, loff_t off, size_t count)
29 dev_warn_once(kobj_to_dev(kobj), nvram_warning);
31 return nvmem_device_read(attr->private, off, count, buf);
35 rtc_nvram_write(struct file *filp, struct kobject *kobj,
36 struct bin_attribute *attr,
37 char *buf, loff_t off, size_t count)
39 dev_warn_once(kobj_to_dev(kobj), nvram_warning);
41 return nvmem_device_write(attr->private, off, count, buf);
44 static int rtc_nvram_register(struct rtc_device *rtc,
45 struct nvmem_device *nvmem, size_t size)
49 rtc->nvram = kzalloc(sizeof(struct bin_attribute), GFP_KERNEL);
53 rtc->nvram->attr.name = "nvram";
54 rtc->nvram->attr.mode = 0644;
55 rtc->nvram->private = nvmem;
57 sysfs_bin_attr_init(rtc->nvram);
59 rtc->nvram->read = rtc_nvram_read;
60 rtc->nvram->write = rtc_nvram_write;
61 rtc->nvram->size = size;
63 err = sysfs_create_bin_file(&rtc->dev.parent->kobj,
73 static void rtc_nvram_unregister(struct rtc_device *rtc)
75 sysfs_remove_bin_file(&rtc->dev.parent->kobj, rtc->nvram);
83 int rtc_nvmem_register(struct rtc_device *rtc,
84 struct nvmem_config *nvmem_config)
86 struct nvmem_device *nvmem;
91 nvmem_config->dev = rtc->dev.parent;
92 nvmem_config->owner = rtc->owner;
93 nvmem = devm_nvmem_register(rtc->dev.parent, nvmem_config);
95 return PTR_ERR(nvmem);
97 /* Register the old ABI */
98 if (rtc->nvram_old_abi)
99 rtc_nvram_register(rtc, nvmem, nvmem_config->size);
103 EXPORT_SYMBOL_GPL(rtc_nvmem_register);
105 void rtc_nvmem_unregister(struct rtc_device *rtc)
107 /* unregister the old ABI */
109 rtc_nvram_unregister(rtc);