blob: 4312096c773873160c583d2efd9d5b99ac646508 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * RTC subsystem, nvmem interface
4 *
5 * Copyright (C) 2017 Alexandre Belloni
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 */
7
8#include <linux/err.h>
9#include <linux/types.h>
10#include <linux/nvmem-consumer.h>
11#include <linux/rtc.h>
David Brazdil0f672f62019-12-10 10:32:29 +000012#include <linux/slab.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013#include <linux/sysfs.h>
14
15/*
16 * Deprecated ABI compatibility, this should be removed at some point
17 */
18
19static const char nvram_warning[] = "Deprecated ABI, please use nvmem";
20
21static ssize_t
22rtc_nvram_read(struct file *filp, struct kobject *kobj,
23 struct bin_attribute *attr,
24 char *buf, loff_t off, size_t count)
25{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026 dev_warn_once(kobj_to_dev(kobj), nvram_warning);
27
David Brazdil0f672f62019-12-10 10:32:29 +000028 return nvmem_device_read(attr->private, off, count, buf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029}
30
31static ssize_t
32rtc_nvram_write(struct file *filp, struct kobject *kobj,
33 struct bin_attribute *attr,
34 char *buf, loff_t off, size_t count)
35{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036 dev_warn_once(kobj_to_dev(kobj), nvram_warning);
37
David Brazdil0f672f62019-12-10 10:32:29 +000038 return nvmem_device_write(attr->private, off, count, buf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039}
40
David Brazdil0f672f62019-12-10 10:32:29 +000041static int rtc_nvram_register(struct rtc_device *rtc,
42 struct nvmem_device *nvmem, size_t size)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043{
44 int err;
45
David Brazdil0f672f62019-12-10 10:32:29 +000046 rtc->nvram = kzalloc(sizeof(*rtc->nvram), GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000047 if (!rtc->nvram)
48 return -ENOMEM;
49
50 rtc->nvram->attr.name = "nvram";
51 rtc->nvram->attr.mode = 0644;
David Brazdil0f672f62019-12-10 10:32:29 +000052 rtc->nvram->private = nvmem;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053
54 sysfs_bin_attr_init(rtc->nvram);
55
56 rtc->nvram->read = rtc_nvram_read;
57 rtc->nvram->write = rtc_nvram_write;
58 rtc->nvram->size = size;
59
60 err = sysfs_create_bin_file(&rtc->dev.parent->kobj,
61 rtc->nvram);
62 if (err) {
David Brazdil0f672f62019-12-10 10:32:29 +000063 kfree(rtc->nvram);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000064 rtc->nvram = NULL;
65 }
66
67 return err;
68}
69
70static void rtc_nvram_unregister(struct rtc_device *rtc)
71{
72 sysfs_remove_bin_file(&rtc->dev.parent->kobj, rtc->nvram);
David Brazdil0f672f62019-12-10 10:32:29 +000073 kfree(rtc->nvram);
74 rtc->nvram = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075}
76
77/*
78 * New ABI, uses nvmem
79 */
80int rtc_nvmem_register(struct rtc_device *rtc,
81 struct nvmem_config *nvmem_config)
82{
David Brazdil0f672f62019-12-10 10:32:29 +000083 struct nvmem_device *nvmem;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084
85 if (!nvmem_config)
86 return -ENODEV;
87
88 nvmem_config->dev = rtc->dev.parent;
89 nvmem_config->owner = rtc->owner;
David Brazdil0f672f62019-12-10 10:32:29 +000090 nvmem = devm_nvmem_register(rtc->dev.parent, nvmem_config);
91 if (IS_ERR(nvmem))
92 return PTR_ERR(nvmem);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000093
94 /* Register the old ABI */
95 if (rtc->nvram_old_abi)
David Brazdil0f672f62019-12-10 10:32:29 +000096 rtc_nvram_register(rtc, nvmem, nvmem_config->size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000097
98 return 0;
99}
100EXPORT_SYMBOL_GPL(rtc_nvmem_register);
101
102void rtc_nvmem_unregister(struct rtc_device *rtc)
103{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000104 /* unregister the old ABI */
105 if (rtc->nvram)
106 rtc_nvram_unregister(rtc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000107}