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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* rtc-bq4802.c: TI BQ4802 RTC driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^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/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/bcd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) MODULE_DESCRIPTION("TI BQ4802 RTC driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) struct bq4802 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	void __iomem		*regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	unsigned long		ioport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct rtc_device	*rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	spinlock_t		lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct resource		*r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	u8 (*read)(struct bq4802 *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	void (*write)(struct bq4802 *, int, u8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static u8 bq4802_read_io(struct bq4802 *p, int off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	return inb(p->ioport + off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static void bq4802_write_io(struct bq4802 *p, int off, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	outb(val, p->ioport + off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static u8 bq4802_read_mem(struct bq4802 *p, int off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	return readb(p->regs + off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static void bq4802_write_mem(struct bq4802 *p, int off, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	writeb(val, p->regs + off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static int bq4802_read_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct bq4802 *p = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	unsigned int century;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	spin_lock_irqsave(&p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	val = p->read(p, 0x0e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	p->write(p, 0xe, val | 0x08);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	tm->tm_sec = p->read(p, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	tm->tm_min = p->read(p, 0x02);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	tm->tm_hour = p->read(p, 0x04);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	tm->tm_mday = p->read(p, 0x06);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	tm->tm_mon = p->read(p, 0x09);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	tm->tm_year = p->read(p, 0x0a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	tm->tm_wday = p->read(p, 0x08);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	century = p->read(p, 0x0f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	p->write(p, 0x0e, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	spin_unlock_irqrestore(&p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	tm->tm_sec = bcd2bin(tm->tm_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	tm->tm_min = bcd2bin(tm->tm_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	tm->tm_hour = bcd2bin(tm->tm_hour);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	tm->tm_mday = bcd2bin(tm->tm_mday);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	tm->tm_mon = bcd2bin(tm->tm_mon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	tm->tm_year = bcd2bin(tm->tm_year);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	tm->tm_wday = bcd2bin(tm->tm_wday);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	century = bcd2bin(century);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	tm->tm_year += (century * 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	tm->tm_year -= 1900;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	tm->tm_mon--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static int bq4802_set_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct bq4802 *p = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	u8 sec, min, hrs, day, mon, yrs, century, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	unsigned int year;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	year = tm->tm_year + 1900;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	century = year / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	yrs = year % 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	mon = tm->tm_mon + 1;   /* tm_mon starts at zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	day = tm->tm_mday;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	hrs = tm->tm_hour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	min = tm->tm_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	sec = tm->tm_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	sec = bin2bcd(sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	min = bin2bcd(min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	hrs = bin2bcd(hrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	day = bin2bcd(day);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	mon = bin2bcd(mon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	yrs = bin2bcd(yrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	century = bin2bcd(century);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	spin_lock_irqsave(&p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	val = p->read(p, 0x0e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	p->write(p, 0x0e, val | 0x08);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	p->write(p, 0x00, sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	p->write(p, 0x02, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	p->write(p, 0x04, hrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	p->write(p, 0x06, day);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	p->write(p, 0x09, mon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	p->write(p, 0x0a, yrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	p->write(p, 0x0f, century);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	p->write(p, 0x0e, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	spin_unlock_irqrestore(&p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static const struct rtc_class_ops bq4802_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	.read_time	= bq4802_read_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	.set_time	= bq4802_set_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int bq4802_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct bq4802 *p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	spin_lock_init(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	p->r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (!p->r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		p->r = platform_get_resource(pdev, IORESOURCE_IO, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		if (!p->r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (p->r->flags & IORESOURCE_IO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		p->ioport = p->r->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		p->read = bq4802_read_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		p->write = bq4802_write_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	} else if (p->r->flags & IORESOURCE_MEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		p->regs = devm_ioremap(&pdev->dev, p->r->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 					resource_size(p->r));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		if (!p->regs){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		p->read = bq4802_read_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		p->write = bq4802_write_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	platform_set_drvdata(pdev, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	p->rtc = devm_rtc_device_register(&pdev->dev, "bq4802",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 					&bq4802_ops, THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (IS_ERR(p->rtc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		err = PTR_ERR(p->rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /* work with hotplug and coldplug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) MODULE_ALIAS("platform:rtc-bq4802");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static struct platform_driver bq4802_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		.name	= "rtc-bq4802",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	.probe		= bq4802_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) module_platform_driver(bq4802_driver);