xref: /openbmc/linux/drivers/rtc/rtc-rx8025.c (revision 6562c9ac)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for Epson's RTC module RX-8025 SA/NB
4  *
5  * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com>
6  *
7  * Copyright (C) 2005 by Digi International Inc.
8  * All rights reserved.
9  *
10  * Modified by fengjh at rising.com.cn
11  * <lm-sensors@lm-sensors.org>
12  * 2006.11
13  *
14  * Code cleanup by Sergei Poselenov, <sposelenov@emcraft.com>
15  * Converted to new style by Wolfgang Grandegger <wg@grandegger.com>
16  * Alarm and periodic interrupt added by Dmitry Rakhchev <rda@emcraft.com>
17  */
18 #include <linux/bcd.h>
19 #include <linux/bitops.h>
20 #include <linux/i2c.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/rtc.h>
24 
25 /* Register definitions */
26 #define RX8025_REG_SEC		0x00
27 #define RX8025_REG_MIN		0x01
28 #define RX8025_REG_HOUR		0x02
29 #define RX8025_REG_WDAY		0x03
30 #define RX8025_REG_MDAY		0x04
31 #define RX8025_REG_MONTH	0x05
32 #define RX8025_REG_YEAR		0x06
33 #define RX8025_REG_DIGOFF	0x07
34 #define RX8025_REG_ALWMIN	0x08
35 #define RX8025_REG_ALWHOUR	0x09
36 #define RX8025_REG_ALWWDAY	0x0a
37 #define RX8025_REG_ALDMIN	0x0b
38 #define RX8025_REG_ALDHOUR	0x0c
39 /* 0x0d is reserved */
40 #define RX8025_REG_CTRL1	0x0e
41 #define RX8025_REG_CTRL2	0x0f
42 
43 #define RX8025_BIT_CTRL1_CT	(7 << 0)
44 /* 1 Hz periodic level irq */
45 #define RX8025_BIT_CTRL1_CT_1HZ	4
46 #define RX8025_BIT_CTRL1_TEST	BIT(3)
47 #define RX8025_BIT_CTRL1_1224	BIT(5)
48 #define RX8025_BIT_CTRL1_DALE	BIT(6)
49 #define RX8025_BIT_CTRL1_WALE	BIT(7)
50 
51 #define RX8025_BIT_CTRL2_DAFG	BIT(0)
52 #define RX8025_BIT_CTRL2_WAFG	BIT(1)
53 #define RX8025_BIT_CTRL2_CTFG	BIT(2)
54 #define RX8025_BIT_CTRL2_PON	BIT(4)
55 #define RX8025_BIT_CTRL2_XST	BIT(5)
56 #define RX8025_BIT_CTRL2_VDET	BIT(6)
57 
58 #define RX8035_BIT_HOUR_1224	BIT(7)
59 
60 /* Clock precision adjustment */
61 #define RX8025_ADJ_RESOLUTION	3050 /* in ppb */
62 #define RX8025_ADJ_DATA_MAX	62
63 #define RX8025_ADJ_DATA_MIN	-62
64 
65 enum rx_model {
66 	model_rx_unknown,
67 	model_rx_8025,
68 	model_rx_8035,
69 	model_last
70 };
71 
72 static const struct i2c_device_id rx8025_id[] = {
73 	{ "rx8025", model_rx_8025 },
74 	{ "rx8035", model_rx_8035 },
75 	{ }
76 };
77 MODULE_DEVICE_TABLE(i2c, rx8025_id);
78 
79 struct rx8025_data {
80 	struct rtc_device *rtc;
81 	enum rx_model model;
82 	u8 ctrl1;
83 	int is_24;
84 };
85 
86 static s32 rx8025_read_reg(const struct i2c_client *client, u8 number)
87 {
88 	return i2c_smbus_read_byte_data(client, number << 4);
89 }
90 
91 static int rx8025_read_regs(const struct i2c_client *client,
92 			    u8 number, u8 length, u8 *values)
93 {
94 	int ret = i2c_smbus_read_i2c_block_data(client, number << 4, length,
95 						values);
96 	if (ret != length)
97 		return ret < 0 ? ret : -EIO;
98 
99 	return 0;
100 }
101 
102 static s32 rx8025_write_reg(const struct i2c_client *client, u8 number,
103 			    u8 value)
104 {
105 	return i2c_smbus_write_byte_data(client, number << 4, value);
106 }
107 
108 static s32 rx8025_write_regs(const struct i2c_client *client,
109 			     u8 number, u8 length, const u8 *values)
110 {
111 	return i2c_smbus_write_i2c_block_data(client, number << 4,
112 					      length, values);
113 }
114 
115 static int rx8025_is_osc_stopped(enum rx_model model, int ctrl2)
116 {
117 	int xstp = ctrl2 & RX8025_BIT_CTRL2_XST;
118 	/* XSTP bit has different polarity on RX-8025 vs RX-8035.
119 	 * RX-8025: 0 == oscillator stopped
120 	 * RX-8035: 1 == oscillator stopped
121 	 */
122 
123 	if (model == model_rx_8025)
124 		xstp = !xstp;
125 
126 	return xstp;
127 }
128 
129 static int rx8025_check_validity(struct device *dev)
130 {
131 	struct i2c_client *client = to_i2c_client(dev);
132 	struct rx8025_data *drvdata = dev_get_drvdata(dev);
133 	int ctrl2;
134 	int xstp;
135 
136 	ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
137 	if (ctrl2 < 0)
138 		return ctrl2;
139 
140 	if (ctrl2 & RX8025_BIT_CTRL2_VDET)
141 		dev_warn(dev, "power voltage drop detected\n");
142 
143 	if (ctrl2 & RX8025_BIT_CTRL2_PON) {
144 		dev_warn(dev, "power-on reset detected, date is invalid\n");
145 		return -EINVAL;
146 	}
147 
148 	xstp = rx8025_is_osc_stopped(drvdata->model, ctrl2);
149 	if (xstp) {
150 		dev_warn(dev, "crystal stopped, date is invalid\n");
151 		return -EINVAL;
152 	}
153 
154 	return 0;
155 }
156 
157 static int rx8025_reset_validity(struct i2c_client *client)
158 {
159 	struct rx8025_data *drvdata = i2c_get_clientdata(client);
160 	int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
161 
162 	if (ctrl2 < 0)
163 		return ctrl2;
164 
165 	ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET);
166 
167 	if (drvdata->model == model_rx_8025)
168 		ctrl2 |= RX8025_BIT_CTRL2_XST;
169 	else
170 		ctrl2 &= ~(RX8025_BIT_CTRL2_XST);
171 
172 	return rx8025_write_reg(client, RX8025_REG_CTRL2,
173 				ctrl2);
174 }
175 
176 static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
177 {
178 	struct i2c_client *client = dev_id;
179 	struct rx8025_data *rx8025 = i2c_get_clientdata(client);
180 	int status, xstp;
181 
182 	rtc_lock(rx8025->rtc);
183 	status = rx8025_read_reg(client, RX8025_REG_CTRL2);
184 	if (status < 0)
185 		goto out;
186 
187 	xstp = rx8025_is_osc_stopped(rx8025->model, status);
188 	if (xstp)
189 		dev_warn(&client->dev, "Oscillation stop was detected,"
190 			 "you may have to readjust the clock\n");
191 
192 	if (status & RX8025_BIT_CTRL2_CTFG) {
193 		/* periodic */
194 		status &= ~RX8025_BIT_CTRL2_CTFG;
195 		rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
196 	}
197 
198 	if (status & RX8025_BIT_CTRL2_DAFG) {
199 		/* alarm */
200 		status &= RX8025_BIT_CTRL2_DAFG;
201 		if (rx8025_write_reg(client, RX8025_REG_CTRL1,
202 				     rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
203 			goto out;
204 		rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
205 	}
206 
207 out:
208 	rtc_unlock(rx8025->rtc);
209 
210 	return IRQ_HANDLED;
211 }
212 
213 static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
214 {
215 	struct i2c_client *client = to_i2c_client(dev);
216 	struct rx8025_data *rx8025 = dev_get_drvdata(dev);
217 	u8 date[7];
218 	int err;
219 
220 	err = rx8025_check_validity(dev);
221 	if (err)
222 		return err;
223 
224 	err = rx8025_read_regs(client, RX8025_REG_SEC, 7, date);
225 	if (err)
226 		return err;
227 
228 	dev_dbg(dev, "%s: read %7ph\n", __func__, date);
229 
230 	dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f);
231 	dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f);
232 	if (rx8025->is_24)
233 		dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
234 	else
235 		dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12
236 			+ (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0);
237 
238 	dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
239 	dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1;
240 	dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100;
241 
242 	dev_dbg(dev, "%s: date %ptRr\n", __func__, dt);
243 
244 	return 0;
245 }
246 
247 static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
248 {
249 	struct i2c_client *client = to_i2c_client(dev);
250 	struct rx8025_data *rx8025 = dev_get_drvdata(dev);
251 	u8 date[7];
252 	int ret;
253 
254 	/*
255 	 * Here the read-only bits are written as "0".  I'm not sure if that
256 	 * is sound.
257 	 */
258 	date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
259 	date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
260 	if (rx8025->is_24)
261 		date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
262 	else
263 		date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
264 			| bin2bcd((dt->tm_hour + 11) % 12 + 1);
265 
266 	date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
267 	date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
268 	date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
269 	date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100);
270 
271 	dev_dbg(dev, "%s: write %7ph\n", __func__, date);
272 
273 	ret = rx8025_write_regs(client, RX8025_REG_SEC, 7, date);
274 	if (ret < 0)
275 		return ret;
276 
277 	return rx8025_reset_validity(client);
278 }
279 
280 static int rx8025_init_client(struct i2c_client *client)
281 {
282 	struct rx8025_data *rx8025 = i2c_get_clientdata(client);
283 	u8 ctrl[2], ctrl2;
284 	int need_clear = 0;
285 	int hour_reg;
286 	int err;
287 
288 	err = rx8025_read_regs(client, RX8025_REG_CTRL1, 2, ctrl);
289 	if (err)
290 		goto out;
291 
292 	/* Keep test bit zero ! */
293 	rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
294 
295 	if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
296 		dev_warn(&client->dev, "Alarm was detected\n");
297 		need_clear = 1;
298 	}
299 
300 	if (ctrl[1] & RX8025_BIT_CTRL2_CTFG)
301 		need_clear = 1;
302 
303 	if (need_clear) {
304 		ctrl2 = ctrl[1];
305 		ctrl2 &= ~(RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
306 			   RX8025_BIT_CTRL2_DAFG);
307 
308 		err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
309 	}
310 
311 	if (rx8025->model == model_rx_8035) {
312 		/* In RX-8035, 12/24 flag is in the hour register */
313 		hour_reg = rx8025_read_reg(client, RX8025_REG_HOUR);
314 		if (hour_reg < 0)
315 			return hour_reg;
316 		rx8025->is_24 = (hour_reg & RX8035_BIT_HOUR_1224);
317 	} else {
318 		rx8025->is_24 = (ctrl[1] & RX8025_BIT_CTRL1_1224);
319 	}
320 out:
321 	return err;
322 }
323 
324 /* Alarm support */
325 static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
326 {
327 	struct i2c_client *client = to_i2c_client(dev);
328 	struct rx8025_data *rx8025 = dev_get_drvdata(dev);
329 	u8 ald[2];
330 	int ctrl2, err;
331 
332 	err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
333 	if (err)
334 		return err;
335 
336 	ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
337 	if (ctrl2 < 0)
338 		return ctrl2;
339 
340 	dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
341 		__func__, ald[0], ald[1], ctrl2);
342 
343 	/* Hardware alarms precision is 1 minute! */
344 	t->time.tm_sec = 0;
345 	t->time.tm_min = bcd2bin(ald[0] & 0x7f);
346 	if (rx8025->is_24)
347 		t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
348 	else
349 		t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
350 			+ (ald[1] & 0x20 ? 12 : 0);
351 
352 	dev_dbg(dev, "%s: date: %ptRr\n", __func__, &t->time);
353 	t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
354 	t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
355 
356 	return err;
357 }
358 
359 static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
360 {
361 	struct i2c_client *client = to_i2c_client(dev);
362 	struct rx8025_data *rx8025 = dev_get_drvdata(dev);
363 	u8 ald[2];
364 	int err;
365 
366 	ald[0] = bin2bcd(t->time.tm_min);
367 	if (rx8025->is_24)
368 		ald[1] = bin2bcd(t->time.tm_hour);
369 	else
370 		ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
371 			| bin2bcd((t->time.tm_hour + 11) % 12 + 1);
372 
373 	dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
374 
375 	if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
376 		rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
377 		err = rx8025_write_reg(client, RX8025_REG_CTRL1,
378 				       rx8025->ctrl1);
379 		if (err)
380 			return err;
381 	}
382 	err = rx8025_write_regs(client, RX8025_REG_ALDMIN, 2, ald);
383 	if (err)
384 		return err;
385 
386 	if (t->enabled) {
387 		rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
388 		err = rx8025_write_reg(client, RX8025_REG_CTRL1,
389 				       rx8025->ctrl1);
390 		if (err)
391 			return err;
392 	}
393 
394 	return 0;
395 }
396 
397 static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
398 {
399 	struct i2c_client *client = to_i2c_client(dev);
400 	struct rx8025_data *rx8025 = dev_get_drvdata(dev);
401 	u8 ctrl1;
402 	int err;
403 
404 	ctrl1 = rx8025->ctrl1;
405 	if (enabled)
406 		ctrl1 |= RX8025_BIT_CTRL1_DALE;
407 	else
408 		ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
409 
410 	if (ctrl1 != rx8025->ctrl1) {
411 		rx8025->ctrl1 = ctrl1;
412 		err = rx8025_write_reg(client, RX8025_REG_CTRL1,
413 				       rx8025->ctrl1);
414 		if (err)
415 			return err;
416 	}
417 	return 0;
418 }
419 
420 /*
421  * According to the RX8025 SA/NB application manual the frequency and
422  * temperature characteristics can be approximated using the following
423  * equation:
424  *
425  *   df = a * (ut - t)**2
426  *
427  *   df: Frequency deviation in any temperature
428  *   a : Coefficient = (-35 +-5) * 10**-9
429  *   ut: Ultimate temperature in degree = +25 +-5 degree
430  *   t : Any temperature in degree
431  */
432 static int rx8025_read_offset(struct device *dev, long *offset)
433 {
434 	struct i2c_client *client = to_i2c_client(dev);
435 	int digoff;
436 
437 	digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF);
438 	if (digoff < 0)
439 		return digoff;
440 
441 	*offset = digoff >= 64 ? digoff - 128 : digoff;
442 	if (*offset > 0)
443 		(*offset)--;
444 	*offset *= RX8025_ADJ_RESOLUTION;
445 
446 	return 0;
447 }
448 
449 static int rx8025_set_offset(struct device *dev, long offset)
450 {
451 	struct i2c_client *client = to_i2c_client(dev);
452 	u8 digoff;
453 
454 	offset /= RX8025_ADJ_RESOLUTION;
455 	if (offset > RX8025_ADJ_DATA_MAX)
456 		offset = RX8025_ADJ_DATA_MAX;
457 	else if (offset < RX8025_ADJ_DATA_MIN)
458 		offset = RX8025_ADJ_DATA_MIN;
459 	else if (offset > 0)
460 		offset++;
461 	else if (offset < 0)
462 		offset += 128;
463 	digoff = offset;
464 
465 	return rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
466 }
467 
468 static const struct rtc_class_ops rx8025_rtc_ops = {
469 	.read_time = rx8025_get_time,
470 	.set_time = rx8025_set_time,
471 	.read_alarm = rx8025_read_alarm,
472 	.set_alarm = rx8025_set_alarm,
473 	.alarm_irq_enable = rx8025_alarm_irq_enable,
474 	.read_offset = rx8025_read_offset,
475 	.set_offset = rx8025_set_offset,
476 };
477 
478 static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
479 					      struct device_attribute *attr,
480 					      char *buf)
481 {
482 	long adj;
483 	int err;
484 
485 	dev_warn_once(dev, "clock_adjust_ppb is deprecated, use offset\n");
486 	err = rx8025_read_offset(dev, &adj);
487 	if (err)
488 		return err;
489 
490 	return sprintf(buf, "%ld\n", -adj);
491 }
492 
493 static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
494 					       struct device_attribute *attr,
495 					       const char *buf, size_t count)
496 {
497 	long adj;
498 	int err;
499 
500 	dev_warn_once(dev, "clock_adjust_ppb is deprecated, use offset\n");
501 	if (kstrtol(buf, 10, &adj) != 0)
502 		return -EINVAL;
503 
504 	err = rx8025_set_offset(dev, -adj);
505 
506 	return err ? err : count;
507 }
508 
509 static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
510 		   rx8025_sysfs_show_clock_adjust,
511 		   rx8025_sysfs_store_clock_adjust);
512 
513 static struct attribute *rx8025_attrs[] = {
514 	&dev_attr_clock_adjust_ppb.attr,
515 	NULL
516 };
517 
518 static const struct attribute_group rx8025_attr_group = {
519 	.attrs	= rx8025_attrs,
520 };
521 
522 static int rx8025_probe(struct i2c_client *client,
523 			const struct i2c_device_id *id)
524 {
525 	struct i2c_adapter *adapter = client->adapter;
526 	struct rx8025_data *rx8025;
527 	int err = 0;
528 
529 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
530 				     | I2C_FUNC_SMBUS_I2C_BLOCK)) {
531 		dev_err(&adapter->dev,
532 			"doesn't support required functionality\n");
533 		return -EIO;
534 	}
535 
536 	rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL);
537 	if (!rx8025)
538 		return -ENOMEM;
539 
540 	i2c_set_clientdata(client, rx8025);
541 
542 	if (id)
543 		rx8025->model = id->driver_data;
544 
545 	err = rx8025_init_client(client);
546 	if (err)
547 		return err;
548 
549 	rx8025->rtc = devm_rtc_allocate_device(&client->dev);
550 	if (IS_ERR(rx8025->rtc))
551 		return PTR_ERR(rx8025->rtc);
552 
553 	rx8025->rtc->ops = &rx8025_rtc_ops;
554 	rx8025->rtc->range_min = RTC_TIMESTAMP_BEGIN_1900;
555 	rx8025->rtc->range_max = RTC_TIMESTAMP_END_2099;
556 
557 	if (client->irq > 0) {
558 		dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
559 		err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
560 						rx8025_handle_irq,
561 						IRQF_ONESHOT,
562 						"rx8025", client);
563 		if (err)
564 			clear_bit(RTC_FEATURE_ALARM, rx8025->rtc->features);
565 	}
566 
567 	rx8025->rtc->max_user_freq = 1;
568 
569 	set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rx8025->rtc->features);
570 	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rx8025->rtc->features);
571 
572 	err = rtc_add_group(rx8025->rtc, &rx8025_attr_group);
573 	if (err)
574 		return err;
575 
576 	return devm_rtc_register_device(rx8025->rtc);
577 }
578 
579 static struct i2c_driver rx8025_driver = {
580 	.driver = {
581 		.name = "rtc-rx8025",
582 	},
583 	.probe		= rx8025_probe,
584 	.id_table	= rx8025_id,
585 };
586 
587 module_i2c_driver(rx8025_driver);
588 
589 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
590 MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
591 MODULE_LICENSE("GPL");
592