aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc/rtc-rpi.c
blob: 006012333e789125f4a173f43f5a243f16322f66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/**
 * rtc-rpi.c
 *
 * RTC driver using firmware mailbox
 * Supports battery backed RTC and wake alarms
 *
 * Based on rtc-meson-vrtc by Neil Armstrong
 *
 * Copyright (c) 2023, Raspberry Pi Ltd.
 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/rtc.h>
#include <linux/of.h>
#include <soc/bcm2835/raspberrypi-firmware.h>

struct rpi_rtc_data {
	struct rtc_device *rtc;
	struct rpi_firmware *fw;
	u32 bbat_vchg_microvolts;
};

#define RPI_FIRMWARE_GET_RTC_REG 0x00030087
#define RPI_FIRMWARE_SET_RTC_REG 0x00038087

enum {
	RTC_TIME,
	RTC_ALARM,
	RTC_ALARM_PENDING,
	RTC_ALARM_ENABLE,
	RTC_BBAT_CHG_VOLTS,
	RTC_BBAT_CHG_VOLTS_MIN,
	RTC_BBAT_CHG_VOLTS_MAX,
	RTC_BBAT_VOLTS
};

static int rpi_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
	struct rpi_rtc_data *vrtc = dev_get_drvdata(dev);
	u32 data[2] = {RTC_TIME};
	int err;

	err = rpi_firmware_property(vrtc->fw, RPI_FIRMWARE_GET_RTC_REG,
				    &data, sizeof(data));
	rtc_time64_to_tm(data[1], tm);
	return err;
}

static int rpi_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
	struct rpi_rtc_data *vrtc = dev_get_drvdata(dev);
	u32 data[2] = {RTC_TIME, rtc_tm_to_time64(tm)};

	return rpi_firmware_property(vrtc->fw, RPI_FIRMWARE_SET_RTC_REG,
				     &data, sizeof(data));
}

static int rpi_rtc_alarm_irq_is_enabled(struct device *dev, unsigned char *enabled)
{
	struct rpi_rtc_data *vrtc = dev_get_drvdata(dev);
	u32 data[2] = {RTC_ALARM_ENABLE};
	s32 err = 0;

	err = rpi_firmware_property(vrtc->fw, RPI_FIRMWARE_GET_RTC_REG,
				    &data, sizeof(data));
	*enabled = data[1] & 0x1;
	return err;
}

static int rpi_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
{
	struct rpi_rtc_data *vrtc = dev_get_drvdata(dev);
	u32 data[2] = {RTC_ALARM_ENABLE, enabled};

	return rpi_firmware_property(vrtc->fw, RPI_FIRMWARE_SET_RTC_REG,
				     &data, sizeof(data));
}

static int rpi_rtc_alarm_clear_pending(struct device *dev)
{
	struct rpi_rtc_data *vrtc = dev_get_drvdata(dev);
	u32 data[2] = {RTC_ALARM_PENDING, 1};

	return rpi_firmware_property(vrtc->fw, RPI_FIRMWARE_SET_RTC_REG,
				     &data, sizeof(data));
}

static int rpi_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
{
	struct rpi_rtc_data *vrtc = dev_get_drvdata(dev);
	u32 data[2] = {RTC_ALARM};
	s32 err = 0;

	err = rpi_rtc_alarm_irq_is_enabled(dev, &alarm->enabled);
	if (!err)
		err = rpi_firmware_property(vrtc->fw, RPI_FIRMWARE_GET_RTC_REG,
					    &data, sizeof(data));
	rtc_time64_to_tm(data[1], &alarm->time);

	return err;
}

static int rpi_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
{
	struct rpi_rtc_data *vrtc = dev_get_drvdata(dev);
	u32 data[2] = {RTC_ALARM, rtc_tm_to_time64(&alarm->time)};
	int err;

	err = rpi_firmware_property(vrtc->fw, RPI_FIRMWARE_SET_RTC_REG,
				    &data, sizeof(data));

	if (err == 0)
		err = rpi_rtc_alarm_irq_enable(dev, alarm->enabled);

	return err;
}

static const struct rtc_class_ops rpi_rtc_ops = {
	.read_time = rpi_rtc_read_time,
	.set_time = rpi_rtc_set_time,
	.read_alarm = rpi_rtc_read_alarm,
	.set_alarm = rpi_rtc_set_alarm,
	.alarm_irq_enable = rpi_rtc_alarm_irq_enable,
};

static int rpi_rtc_set_charge_voltage(struct device *dev)
{
	struct rpi_rtc_data *vrtc = dev_get_drvdata(dev);
	u32 data[2] = {RTC_BBAT_CHG_VOLTS, vrtc->bbat_vchg_microvolts};
	int err;

	err = rpi_firmware_property(vrtc->fw, RPI_FIRMWARE_SET_RTC_REG,
				    &data, sizeof(data));

	if (err)
		dev_err(dev, "failed to set trickle charge voltage to %uuV: %d\n",
			vrtc->bbat_vchg_microvolts, err);
	else if (vrtc->bbat_vchg_microvolts)
		dev_info(dev, "trickle charging enabled at %uuV\n",
			 vrtc->bbat_vchg_microvolts);

	return err;
}

static ssize_t rpi_rtc_print_uint_reg(struct device *dev, char *buf, u32 reg)
{
	struct rpi_rtc_data *vrtc = dev_get_drvdata(dev->parent);
	u32 data[2] = {reg, 0};
	int ret = 0;

	ret = rpi_firmware_property(vrtc->fw, RPI_FIRMWARE_GET_RTC_REG,
				    &data, sizeof(data));
	if (ret < 0)
		return ret;

	return sprintf(buf, "%u\n", data[1]);
}

static ssize_t charging_voltage_show(struct device *dev,
				     struct device_attribute *attr,
				     char *buf)
{
	return rpi_rtc_print_uint_reg(dev, buf, RTC_BBAT_CHG_VOLTS);
}
static DEVICE_ATTR_RO(charging_voltage);

static ssize_t charging_voltage_min_show(struct device *dev,
					 struct device_attribute *attr,
					 char *buf)
{
	return rpi_rtc_print_uint_reg(dev, buf, RTC_BBAT_CHG_VOLTS_MIN);
}
static DEVICE_ATTR_RO(charging_voltage_min);

static ssize_t charging_voltage_max_show(struct device *dev,
					 struct device_attribute *attr,
					 char *buf)
{
	return rpi_rtc_print_uint_reg(dev, buf, RTC_BBAT_CHG_VOLTS_MAX);
}
static DEVICE_ATTR_RO(charging_voltage_max);

static ssize_t battery_voltage_show(struct device *dev,
				    struct device_attribute *attr,
				    char *buf)
{
	return rpi_rtc_print_uint_reg(dev, buf, RTC_BBAT_VOLTS);
}
static DEVICE_ATTR_RO(battery_voltage);

static struct attribute *rpi_rtc_attrs[] = {
	&dev_attr_charging_voltage.attr,
	&dev_attr_charging_voltage_min.attr,
	&dev_attr_charging_voltage_max.attr,
	&dev_attr_battery_voltage.attr,
	NULL
};

static const struct attribute_group rpi_rtc_sysfs_files = {
	.attrs = rpi_rtc_attrs,
};

static int rpi_rtc_probe(struct platform_device *pdev)
{
	struct rpi_rtc_data *vrtc;
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
	struct device_node *fw_node;
	struct rpi_firmware *fw;
	int ret;

	fw_node = of_parse_phandle(np, "firmware", 0);
	if (!fw_node) {
		dev_err(dev, "Missing firmware node\n");
		return -ENOENT;
	}

	fw = rpi_firmware_get(fw_node);
	if (!fw)
		return -EPROBE_DEFER;

	vrtc = devm_kzalloc(&pdev->dev, sizeof(*vrtc), GFP_KERNEL);
	if (!vrtc)
		return -ENOMEM;

	vrtc->fw = fw;

	device_init_wakeup(&pdev->dev, 1);

	platform_set_drvdata(pdev, vrtc);

	vrtc->rtc = devm_rtc_allocate_device(&pdev->dev);
	if (IS_ERR(vrtc->rtc))
		return PTR_ERR(vrtc->rtc);

	set_bit(RTC_FEATURE_ALARM_WAKEUP_ONLY, vrtc->rtc->features);
	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, vrtc->rtc->features);

	vrtc->rtc->ops = &rpi_rtc_ops;
	ret = rtc_add_group(vrtc->rtc, &rpi_rtc_sysfs_files);
	if (ret)
		return ret;

	rpi_rtc_alarm_clear_pending(dev);

	/*
	 * Optionally enable trickle charging - if the property isn't
	 * present (or set to zero), trickle charging is disabled.
	 */
	of_property_read_u32(np, "trickle-charge-microvolt",
			     &vrtc->bbat_vchg_microvolts);

	rpi_rtc_set_charge_voltage(dev);

	return devm_rtc_register_device(vrtc->rtc);
}

static const struct of_device_id rpi_rtc_dt_match[] = {
	{ .compatible = "raspberrypi,rpi-rtc"},
	{},
};
MODULE_DEVICE_TABLE(of, rpi_rtc_dt_match);

static struct platform_driver rpi_rtc_driver = {
	.probe = rpi_rtc_probe,
	.driver = {
		.name = "rpi-rtc",
		.of_match_table = rpi_rtc_dt_match,
	},
};

module_platform_driver(rpi_rtc_driver);

MODULE_DESCRIPTION("Raspberry Pi RTC driver");
MODULE_LICENSE("GPL");