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) /* drivers/rtc/rtc-max6902.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2006 8D Technologies inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2004 Compulab Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Driver for MAX6902 spi RTC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/init.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/spi/spi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/bcd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define MAX6902_REG_SECONDS		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define MAX6902_REG_MINUTES		0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define MAX6902_REG_HOURS		0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define MAX6902_REG_DATE		0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define MAX6902_REG_MONTH		0x09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define MAX6902_REG_DAY			0x0B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define MAX6902_REG_YEAR		0x0D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define MAX6902_REG_CONTROL		0x0F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define MAX6902_REG_CENTURY		0x13
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static int max6902_set_reg(struct device *dev, unsigned char address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 				unsigned char data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct spi_device *spi = to_spi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	unsigned char buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	/* MSB must be '0' to write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	buf[0] = address & 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	buf[1] = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	return spi_write_then_read(spi, buf, 2, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static int max6902_get_reg(struct device *dev, unsigned char address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 				unsigned char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct spi_device *spi = to_spi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	/* Set MSB to indicate read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	*data = address | 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	return spi_write_then_read(spi, data, 1, data, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static int max6902_read_time(struct device *dev, struct rtc_time *dt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	int err, century;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct spi_device *spi = to_spi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	unsigned char buf[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	buf[0] = 0xbf;	/* Burst read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	err = spi_write_then_read(spi, buf, 1, buf, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	/* The chip sends data in this order:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	 * Seconds, Minutes, Hours, Date, Month, Day, Year */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	dt->tm_sec	= bcd2bin(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	dt->tm_min	= bcd2bin(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	dt->tm_hour	= bcd2bin(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	dt->tm_mday	= bcd2bin(buf[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	dt->tm_mon	= bcd2bin(buf[4]) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	dt->tm_wday	= bcd2bin(buf[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	dt->tm_year	= bcd2bin(buf[6]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	/* Read century */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	century = bcd2bin(buf[0]) * 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	dt->tm_year += century;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	dt->tm_year -= 1900;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static int max6902_set_time(struct device *dev, struct rtc_time *dt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	dt->tm_year = dt->tm_year + 1900;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	/* Remove write protection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	max6902_set_reg(dev, MAX6902_REG_CONTROL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	max6902_set_reg(dev, MAX6902_REG_SECONDS, bin2bcd(dt->tm_sec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	max6902_set_reg(dev, MAX6902_REG_MINUTES, bin2bcd(dt->tm_min));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	max6902_set_reg(dev, MAX6902_REG_HOURS, bin2bcd(dt->tm_hour));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	max6902_set_reg(dev, MAX6902_REG_DATE, bin2bcd(dt->tm_mday));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	max6902_set_reg(dev, MAX6902_REG_MONTH, bin2bcd(dt->tm_mon + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	max6902_set_reg(dev, MAX6902_REG_DAY, bin2bcd(dt->tm_wday));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	max6902_set_reg(dev, MAX6902_REG_YEAR, bin2bcd(dt->tm_year % 100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	max6902_set_reg(dev, MAX6902_REG_CENTURY, bin2bcd(dt->tm_year / 100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	/* Compulab used a delay here. However, the datasheet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 * does not mention a delay being required anywhere... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	/* delay(2000); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	/* Write protect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	max6902_set_reg(dev, MAX6902_REG_CONTROL, 0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static const struct rtc_class_ops max6902_rtc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	.read_time	= max6902_read_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	.set_time	= max6902_set_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int max6902_probe(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	struct rtc_device *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	unsigned char tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	spi->mode = SPI_MODE_3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	spi->bits_per_word = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	spi_setup(spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (res != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	rtc = devm_rtc_device_register(&spi->dev, "max6902",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 				&max6902_rtc_ops, THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (IS_ERR(rtc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		return PTR_ERR(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	spi_set_drvdata(spi, rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return 0;
^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 struct spi_driver max6902_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		.name	= "rtc-max6902",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	.probe	= max6902_probe,
^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_spi_driver(max6902_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) MODULE_DESCRIPTION("max6902 spi RTC driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) MODULE_AUTHOR("Raphael Assenat");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) MODULE_ALIAS("spi:rtc-max6902");