blob: 4312096c773873160c583d2efd9d5b99ac646508 [file] [log] [blame]
Alexandre Bellonicdf75452019-03-13 23:02:48 +01001// SPDX-License-Identifier: GPL-2.0
Alexandre Belloni697e5a42017-07-06 11:42:02 +02002/*
3 * RTC subsystem, nvmem interface
4 *
5 * Copyright (C) 2017 Alexandre Belloni
Alexandre Belloni697e5a42017-07-06 11:42:02 +02006 */
7
8#include <linux/err.h>
9#include <linux/types.h>
10#include <linux/nvmem-consumer.h>
11#include <linux/rtc.h>
Alexandre Bellonibba3d2d2018-12-31 00:49:36 +010012#include <linux/slab.h>
Alexandre Belloni697e5a42017-07-06 11:42:02 +020013#include <linux/sysfs.h>
14
Alexandre Belloni697e5a42017-07-06 11:42:02 +020015/*
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{
Alexandre Belloni697e5a42017-07-06 11:42:02 +020026 dev_warn_once(kobj_to_dev(kobj), nvram_warning);
27
Alexandre Belloni41c9e132018-11-10 21:29:03 +010028 return nvmem_device_read(attr->private, off, count, buf);
Alexandre Belloni697e5a42017-07-06 11:42:02 +020029}
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{
Alexandre Belloni697e5a42017-07-06 11:42:02 +020036 dev_warn_once(kobj_to_dev(kobj), nvram_warning);
37
Alexandre Belloni41c9e132018-11-10 21:29:03 +010038 return nvmem_device_write(attr->private, off, count, buf);
Alexandre Belloni697e5a42017-07-06 11:42:02 +020039}
40
Alexandre Belloni41c9e132018-11-10 21:29:03 +010041static int rtc_nvram_register(struct rtc_device *rtc,
42 struct nvmem_device *nvmem, size_t size)
Alexandre Belloni697e5a42017-07-06 11:42:02 +020043{
44 int err;
45
Alexandre Belloni606cc432019-03-20 12:59:09 +010046 rtc->nvram = kzalloc(sizeof(*rtc->nvram), GFP_KERNEL);
Alexandre Belloni697e5a42017-07-06 11:42:02 +020047 if (!rtc->nvram)
48 return -ENOMEM;
49
50 rtc->nvram->attr.name = "nvram";
51 rtc->nvram->attr.mode = 0644;
Alexandre Belloni41c9e132018-11-10 21:29:03 +010052 rtc->nvram->private = nvmem;
Alexandre Belloni697e5a42017-07-06 11:42:02 +020053
54 sysfs_bin_attr_init(rtc->nvram);
55
56 rtc->nvram->read = rtc_nvram_read;
57 rtc->nvram->write = rtc_nvram_write;
Alexandre Belloni4cce9d32018-02-12 23:47:16 +010058 rtc->nvram->size = size;
Alexandre Belloni697e5a42017-07-06 11:42:02 +020059
60 err = sysfs_create_bin_file(&rtc->dev.parent->kobj,
61 rtc->nvram);
62 if (err) {
Alexandre Bellonibba3d2d2018-12-31 00:49:36 +010063 kfree(rtc->nvram);
Alexandre Belloni697e5a42017-07-06 11:42:02 +020064 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);
Alexandre Bellonibba3d2d2018-12-31 00:49:36 +010073 kfree(rtc->nvram);
74 rtc->nvram = NULL;
Alexandre Belloni697e5a42017-07-06 11:42:02 +020075}
76
77/*
78 * New ABI, uses nvmem
79 */
Alexandre Belloni2cc82122018-02-12 23:47:17 +010080int rtc_nvmem_register(struct rtc_device *rtc,
81 struct nvmem_config *nvmem_config)
Alexandre Belloni697e5a42017-07-06 11:42:02 +020082{
Alexandre Belloni41c9e132018-11-10 21:29:03 +010083 struct nvmem_device *nvmem;
Alexandre Belloniab3ea362018-02-12 23:47:18 +010084
Alexandre Belloni4cce9d32018-02-12 23:47:16 +010085 if (!nvmem_config)
Alexandre Belloni2cc82122018-02-12 23:47:17 +010086 return -ENODEV;
Alexandre Belloni697e5a42017-07-06 11:42:02 +020087
Alexandre Belloniac757792018-02-28 21:58:02 +010088 nvmem_config->dev = rtc->dev.parent;
Alexandre Belloni4cce9d32018-02-12 23:47:16 +010089 nvmem_config->owner = rtc->owner;
Alexandre Belloni41c9e132018-11-10 21:29:03 +010090 nvmem = devm_nvmem_register(rtc->dev.parent, nvmem_config);
91 if (IS_ERR(nvmem))
92 return PTR_ERR(nvmem);
Alexandre Belloni697e5a42017-07-06 11:42:02 +020093
94 /* Register the old ABI */
95 if (rtc->nvram_old_abi)
Alexandre Belloni41c9e132018-11-10 21:29:03 +010096 rtc_nvram_register(rtc, nvmem, nvmem_config->size);
Alexandre Belloni2cc82122018-02-12 23:47:17 +010097
98 return 0;
Alexandre Belloni697e5a42017-07-06 11:42:02 +020099}
Alexandre Bellonifd5cd212018-02-12 23:47:19 +0100100EXPORT_SYMBOL_GPL(rtc_nvmem_register);
Alexandre Belloni697e5a42017-07-06 11:42:02 +0200101
102void rtc_nvmem_unregister(struct rtc_device *rtc)
103{
Alexandre Belloni697e5a42017-07-06 11:42:02 +0200104 /* unregister the old ABI */
105 if (rtc->nvram)
106 rtc_nvram_unregister(rtc);
Alexandre Belloni697e5a42017-07-06 11:42:02 +0200107}