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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Driver for the SGS-Thomson M48T35 Timekeeper RAM chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2000 Silicon Graphics, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Written by Ulf Carlsson (ulfc@engr.sgi.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Copyright (C) 2008 Thomas Bogendoerfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Based on code written by Paul Gortmaker.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/bcd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) struct m48t35_rtc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	u8	pad[0x7ff8];    /* starts at 0x7ff8 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #ifdef CONFIG_SGI_IP27
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	u8	hour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	u8	min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	u8	sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	u8	control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	u8	year;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	u8	month;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	u8	date;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	u8	day;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	u8	control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	u8	sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	u8	min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	u8	hour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	u8	day;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	u8	date;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	u8	month;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	u8	year;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define M48T35_RTC_SET		0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define M48T35_RTC_READ		0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) struct m48t35_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct rtc_device *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct m48t35_rtc __iomem *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	unsigned long baseaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static int m48t35_read_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct m48t35_priv *priv = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	u8 control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	 * Only the values that we read from the RTC are set. We leave
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	 * tm_wday, tm_yday and tm_isdst untouched. Even though the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	 * by the RTC when initially set to a non-zero value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	spin_lock_irq(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	control = readb(&priv->reg->control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	writeb(control | M48T35_RTC_READ, &priv->reg->control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	tm->tm_sec = readb(&priv->reg->sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	tm->tm_min = readb(&priv->reg->min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	tm->tm_hour = readb(&priv->reg->hour);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	tm->tm_mday = readb(&priv->reg->date);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	tm->tm_mon = readb(&priv->reg->month);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	tm->tm_year = readb(&priv->reg->year);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	writeb(control, &priv->reg->control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	spin_unlock_irq(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	tm->tm_sec = bcd2bin(tm->tm_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	tm->tm_min = bcd2bin(tm->tm_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	tm->tm_hour = bcd2bin(tm->tm_hour);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	tm->tm_mday = bcd2bin(tm->tm_mday);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	tm->tm_mon = bcd2bin(tm->tm_mon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	tm->tm_year = bcd2bin(tm->tm_year);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	 * Account for differences between how the RTC uses the values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	 * and how they are defined in a struct rtc_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	tm->tm_year += 70;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	if (tm->tm_year <= 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		tm->tm_year += 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	tm->tm_mon--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) static int m48t35_set_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct m48t35_priv *priv = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	unsigned char mon, day, hrs, min, sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	unsigned int yrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	u8 control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	yrs = tm->tm_year + 1900;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	mon = tm->tm_mon + 1;   /* tm_mon starts at zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	day = tm->tm_mday;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	hrs = tm->tm_hour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	min = tm->tm_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	sec = tm->tm_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (yrs < 1970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	yrs -= 1970;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (yrs > 255)    /* They are unsigned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (yrs > 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (yrs >= 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		yrs -= 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	sec = bin2bcd(sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	min = bin2bcd(min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	hrs = bin2bcd(hrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	day = bin2bcd(day);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	mon = bin2bcd(mon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	yrs = bin2bcd(yrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	spin_lock_irq(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	control = readb(&priv->reg->control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	writeb(control | M48T35_RTC_SET, &priv->reg->control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	writeb(yrs, &priv->reg->year);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	writeb(mon, &priv->reg->month);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	writeb(day, &priv->reg->date);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	writeb(hrs, &priv->reg->hour);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	writeb(min, &priv->reg->min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	writeb(sec, &priv->reg->sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	writeb(control, &priv->reg->control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	spin_unlock_irq(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static const struct rtc_class_ops m48t35_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	.read_time	= m48t35_read_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	.set_time	= m48t35_set_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static int m48t35_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	struct m48t35_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	priv = devm_kzalloc(&pdev->dev, sizeof(struct m48t35_priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	priv->size = resource_size(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (!devm_request_mem_region(&pdev->dev, res->start, priv->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				     pdev->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	priv->baseaddr = res->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	priv->reg = devm_ioremap(&pdev->dev, priv->baseaddr, priv->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (!priv->reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	spin_lock_init(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	platform_set_drvdata(pdev, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	priv->rtc = devm_rtc_device_register(&pdev->dev, "m48t35",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 				  &m48t35_ops, THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	return PTR_ERR_OR_ZERO(priv->rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static struct platform_driver m48t35_platform_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		.name	= "rtc-m48t35",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	.probe		= m48t35_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) module_platform_driver(m48t35_platform_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) MODULE_DESCRIPTION("M48T35 RTC driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) MODULE_ALIAS("platform:rtc-m48t35");