Alexandre Belloni | cdf7545 | 2019-03-13 23:02:48 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 2 | /* |
| 3 | * RTC subsystem, nvmem interface |
| 4 | * |
| 5 | * Copyright (C) 2017 Alexandre Belloni |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/err.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/nvmem-consumer.h> |
| 11 | #include <linux/rtc.h> |
Alexandre Belloni | bba3d2d | 2018-12-31 00:49:36 +0100 | [diff] [blame] | 12 | #include <linux/slab.h> |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 13 | #include <linux/sysfs.h> |
| 14 | |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 15 | /* |
| 16 | * Deprecated ABI compatibility, this should be removed at some point |
| 17 | */ |
| 18 | |
| 19 | static const char nvram_warning[] = "Deprecated ABI, please use nvmem"; |
| 20 | |
| 21 | static ssize_t |
| 22 | rtc_nvram_read(struct file *filp, struct kobject *kobj, |
| 23 | struct bin_attribute *attr, |
| 24 | char *buf, loff_t off, size_t count) |
| 25 | { |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 26 | dev_warn_once(kobj_to_dev(kobj), nvram_warning); |
| 27 | |
Alexandre Belloni | 41c9e13 | 2018-11-10 21:29:03 +0100 | [diff] [blame] | 28 | return nvmem_device_read(attr->private, off, count, buf); |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static ssize_t |
| 32 | rtc_nvram_write(struct file *filp, struct kobject *kobj, |
| 33 | struct bin_attribute *attr, |
| 34 | char *buf, loff_t off, size_t count) |
| 35 | { |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 36 | dev_warn_once(kobj_to_dev(kobj), nvram_warning); |
| 37 | |
Alexandre Belloni | 41c9e13 | 2018-11-10 21:29:03 +0100 | [diff] [blame] | 38 | return nvmem_device_write(attr->private, off, count, buf); |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Alexandre Belloni | 41c9e13 | 2018-11-10 21:29:03 +0100 | [diff] [blame] | 41 | static int rtc_nvram_register(struct rtc_device *rtc, |
| 42 | struct nvmem_device *nvmem, size_t size) |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 43 | { |
| 44 | int err; |
| 45 | |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 46 | rtc->nvram = kzalloc(sizeof(*rtc->nvram), GFP_KERNEL); |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 47 | if (!rtc->nvram) |
| 48 | return -ENOMEM; |
| 49 | |
| 50 | rtc->nvram->attr.name = "nvram"; |
| 51 | rtc->nvram->attr.mode = 0644; |
Alexandre Belloni | 41c9e13 | 2018-11-10 21:29:03 +0100 | [diff] [blame] | 52 | rtc->nvram->private = nvmem; |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 53 | |
| 54 | sysfs_bin_attr_init(rtc->nvram); |
| 55 | |
| 56 | rtc->nvram->read = rtc_nvram_read; |
| 57 | rtc->nvram->write = rtc_nvram_write; |
Alexandre Belloni | 4cce9d3 | 2018-02-12 23:47:16 +0100 | [diff] [blame] | 58 | rtc->nvram->size = size; |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 59 | |
| 60 | err = sysfs_create_bin_file(&rtc->dev.parent->kobj, |
| 61 | rtc->nvram); |
| 62 | if (err) { |
Alexandre Belloni | bba3d2d | 2018-12-31 00:49:36 +0100 | [diff] [blame] | 63 | kfree(rtc->nvram); |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 64 | rtc->nvram = NULL; |
| 65 | } |
| 66 | |
| 67 | return err; |
| 68 | } |
| 69 | |
| 70 | static void rtc_nvram_unregister(struct rtc_device *rtc) |
| 71 | { |
| 72 | sysfs_remove_bin_file(&rtc->dev.parent->kobj, rtc->nvram); |
Alexandre Belloni | bba3d2d | 2018-12-31 00:49:36 +0100 | [diff] [blame] | 73 | kfree(rtc->nvram); |
| 74 | rtc->nvram = NULL; |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* |
| 78 | * New ABI, uses nvmem |
| 79 | */ |
Alexandre Belloni | 2cc8212 | 2018-02-12 23:47:17 +0100 | [diff] [blame] | 80 | int rtc_nvmem_register(struct rtc_device *rtc, |
| 81 | struct nvmem_config *nvmem_config) |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 82 | { |
Alexandre Belloni | 41c9e13 | 2018-11-10 21:29:03 +0100 | [diff] [blame] | 83 | struct nvmem_device *nvmem; |
Alexandre Belloni | ab3ea36 | 2018-02-12 23:47:18 +0100 | [diff] [blame] | 84 | |
Alexandre Belloni | 4cce9d3 | 2018-02-12 23:47:16 +0100 | [diff] [blame] | 85 | if (!nvmem_config) |
Alexandre Belloni | 2cc8212 | 2018-02-12 23:47:17 +0100 | [diff] [blame] | 86 | return -ENODEV; |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 87 | |
Alexandre Belloni | ac75779 | 2018-02-28 21:58:02 +0100 | [diff] [blame] | 88 | nvmem_config->dev = rtc->dev.parent; |
Alexandre Belloni | 4cce9d3 | 2018-02-12 23:47:16 +0100 | [diff] [blame] | 89 | nvmem_config->owner = rtc->owner; |
Alexandre Belloni | 41c9e13 | 2018-11-10 21:29:03 +0100 | [diff] [blame] | 90 | nvmem = devm_nvmem_register(rtc->dev.parent, nvmem_config); |
| 91 | if (IS_ERR(nvmem)) |
| 92 | return PTR_ERR(nvmem); |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 93 | |
| 94 | /* Register the old ABI */ |
| 95 | if (rtc->nvram_old_abi) |
Alexandre Belloni | 41c9e13 | 2018-11-10 21:29:03 +0100 | [diff] [blame] | 96 | rtc_nvram_register(rtc, nvmem, nvmem_config->size); |
Alexandre Belloni | 2cc8212 | 2018-02-12 23:47:17 +0100 | [diff] [blame] | 97 | |
| 98 | return 0; |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 99 | } |
Alexandre Belloni | fd5cd21 | 2018-02-12 23:47:19 +0100 | [diff] [blame] | 100 | EXPORT_SYMBOL_GPL(rtc_nvmem_register); |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 101 | |
| 102 | void rtc_nvmem_unregister(struct rtc_device *rtc) |
| 103 | { |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 104 | /* unregister the old ABI */ |
| 105 | if (rtc->nvram) |
| 106 | rtc_nvram_unregister(rtc); |
Alexandre Belloni | 697e5a4 | 2017-07-06 11:42:02 +0200 | [diff] [blame] | 107 | } |