Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * rtc-dm355evm.c - access battery-backed counter in MSP430 firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2008 by David Brownell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/mfd/dm355evm_msp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * The MSP430 firmware on the DM355 EVM uses a watch crystal to feed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * a 1 Hz counter.  When a backup battery is supplied, that makes a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * reasonable RTC for applications where alarms and non-NTP drift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * compensation aren't important.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * The only real glitch is the inability to read or write all four
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * counter bytes atomically:  the count may increment in the middle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * of an operation, causing trouble when the LSB rolls over.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * This driver was tested with firmware revision A4.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) union evm_time {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	u8	bytes[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	u32	value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static int dm355evm_rtc_read_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	union evm_time	time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	int		status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	int		tries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		 * Read LSB(0) to MSB(3) bytes.  Defend against the counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		 * rolling over by re-reading until the value is stable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		 * and assuming the four reads take at most a few seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		status = dm355evm_msp_read(DM355EVM_MSP_RTC_0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		if (tries && time.bytes[0] == status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		time.bytes[0] = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		status = dm355evm_msp_read(DM355EVM_MSP_RTC_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		if (tries && time.bytes[1] == status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		time.bytes[1] = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		status = dm355evm_msp_read(DM355EVM_MSP_RTC_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		if (tries && time.bytes[2] == status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		time.bytes[2] = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		status = dm355evm_msp_read(DM355EVM_MSP_RTC_3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		if (tries && time.bytes[3] == status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		time.bytes[3] = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	} while (++tries < 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	dev_dbg(dev, "read timestamp %08x\n", time.value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	rtc_time64_to_tm(le32_to_cpu(time.value), tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static int dm355evm_rtc_set_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	union evm_time	time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	unsigned long	value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	int		status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	value = rtc_tm_to_time64(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	time.value = cpu_to_le32(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	dev_dbg(dev, "write timestamp %08x\n", time.value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	 * REVISIT handle non-atomic writes ... maybe just retry until
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	 * byte[1] sticks (no rollover)?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	status = dm355evm_msp_write(time.bytes[0], DM355EVM_MSP_RTC_0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	status = dm355evm_msp_write(time.bytes[1], DM355EVM_MSP_RTC_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	status = dm355evm_msp_write(time.bytes[2], DM355EVM_MSP_RTC_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	status = dm355evm_msp_write(time.bytes[3], DM355EVM_MSP_RTC_3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static const struct rtc_class_ops dm355evm_rtc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	.read_time	= dm355evm_rtc_read_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	.set_time	= dm355evm_rtc_set_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /*----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int dm355evm_rtc_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	struct rtc_device *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	rtc = devm_rtc_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (IS_ERR(rtc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		return PTR_ERR(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	platform_set_drvdata(pdev, rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	rtc->ops = &dm355evm_rtc_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	rtc->range_max = U32_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return rtc_register_device(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  * I2C is used to talk to the MSP430, but this platform device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  * exposed by an MFD driver that manages I2C communications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static struct platform_driver rtc_dm355evm_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	.probe		= dm355evm_rtc_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		.name	= "rtc-dm355evm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) module_platform_driver(rtc_dm355evm_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) MODULE_LICENSE("GPL");