/src/barebox/drivers/rtc/class.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | /* |
3 | | * RTC barebox subsystem, base class |
4 | | * |
5 | | * Copyright (C) 2014 Antony Pavlov <antonynpavlov@gmail.com> |
6 | | */ |
7 | | |
8 | | #include <linux/err.h> |
9 | | #include <rtc.h> |
10 | | #include <linux/rtc.h> |
11 | | #include <device.h> |
12 | | |
13 | | #define for_each_rtc(rtc) \ |
14 | 0 | class_for_each_container_of_device(&rtc_class, rtc, class_dev) |
15 | | |
16 | | DEFINE_DEV_CLASS(rtc_class, "rtc"); |
17 | | |
18 | | struct rtc_device *rtc_lookup(const char *name) |
19 | 0 | { |
20 | 0 | struct rtc_device *r; |
21 | |
|
22 | 0 | for_each_rtc(r) { |
23 | 0 | if (!name || !strcmp(dev_name(&r->class_dev), name)) |
24 | 0 | return r; |
25 | 0 | } |
26 | | |
27 | 0 | return ERR_PTR(-ENODEV); |
28 | 0 | } |
29 | | EXPORT_SYMBOL(rtc_lookup); |
30 | | |
31 | | int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) |
32 | 0 | { |
33 | 0 | return rtc->ops->read_time(rtc, tm); |
34 | 0 | } |
35 | | EXPORT_SYMBOL(rtc_read_time); |
36 | | |
37 | | int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) |
38 | 0 | { |
39 | 0 | struct rtc_time time; |
40 | 0 | unsigned long secs; |
41 | |
|
42 | 0 | if (rtc_valid_tm(tm)) |
43 | 0 | return -EINVAL; |
44 | | |
45 | 0 | rtc_tm_to_time(tm, &secs); |
46 | 0 | rtc_time_to_tm(secs, &time); |
47 | |
|
48 | 0 | return rtc->ops->set_time(rtc, &time); |
49 | 0 | } |
50 | | EXPORT_SYMBOL(rtc_set_time); |
51 | | |
52 | | int rtc_register(struct rtc_device *rtcdev) |
53 | 0 | { |
54 | 0 | struct device *dev = &rtcdev->class_dev; |
55 | |
|
56 | 0 | if (!rtcdev->ops) |
57 | 0 | return -EINVAL; |
58 | | |
59 | 0 | dev->id = DEVICE_ID_DYNAMIC; |
60 | 0 | dev_set_name(dev, "rtc"); |
61 | 0 | if (rtcdev->dev) |
62 | 0 | dev->parent = rtcdev->dev; |
63 | 0 | platform_device_register(dev); |
64 | |
|
65 | 0 | class_add_device(&rtc_class, &rtcdev->class_dev); |
66 | |
|
67 | 0 | return 0; |
68 | 0 | } |
69 | | EXPORT_SYMBOL(rtc_register); |