drivers/rtc/rtc-max6916.c

Source file repositories/reference/linux-study-clean/drivers/rtc/rtc-max6916.c

File Facts

System
Linux kernel
Corpus path
drivers/rtc/rtc-max6916.c
Extension
.c
Size
4063 bytes
Lines
161
Domain
Driver Families
Bucket
drivers/rtc
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0-only
/* rtc-max6916.c
 *
 * Driver for MAXIM  max6916 Low Current, SPI Compatible
 * Real Time Clock
 *
 * Author : Venkat Prashanth B U <venkat.prashanth2498@gmail.com>
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/rtc.h>
#include <linux/spi/spi.h>
#include <linux/bcd.h>

/* Registers in max6916 rtc */

#define MAX6916_SECONDS_REG	0x01
#define MAX6916_MINUTES_REG	0x02
#define MAX6916_HOURS_REG	0x03
#define MAX6916_DATE_REG	0x04
#define MAX6916_MONTH_REG	0x05
#define MAX6916_DAY_REG	0x06
#define MAX6916_YEAR_REG	0x07
#define MAX6916_CONTROL_REG	0x08
#define MAX6916_STATUS_REG	0x0C
#define MAX6916_CLOCK_BURST	0x3F

static int max6916_read_reg(struct device *dev, unsigned char address,
			    unsigned char *data)
{
	struct spi_device *spi = to_spi_device(dev);

	*data = address | 0x80;

	return spi_write_then_read(spi, data, 1, data, 1);
}

static int max6916_write_reg(struct device *dev, unsigned char address,
			     unsigned char data)
{
	struct spi_device *spi = to_spi_device(dev);
	unsigned char buf[2];

	buf[0] = address & 0x7F;
	buf[1] = data;

	return spi_write_then_read(spi, buf, 2, NULL, 0);
}

static int max6916_read_time(struct device *dev, struct rtc_time *dt)
{
	struct spi_device *spi = to_spi_device(dev);
	int err;
	unsigned char buf[8];

	buf[0] = MAX6916_CLOCK_BURST | 0x80;

	err = spi_write_then_read(spi, buf, 1, buf, 8);

	if (err)
		return err;

	dt->tm_sec = bcd2bin(buf[0]);
	dt->tm_min = bcd2bin(buf[1]);
	dt->tm_hour = bcd2bin(buf[2] & 0x3F);
	dt->tm_mday = bcd2bin(buf[3]);
	dt->tm_mon = bcd2bin(buf[4]) - 1;
	dt->tm_wday = bcd2bin(buf[5]) - 1;
	dt->tm_year = bcd2bin(buf[6]) + 100;

	return 0;
}

static int max6916_set_time(struct device *dev, struct rtc_time *dt)
{
	struct spi_device *spi = to_spi_device(dev);
	unsigned char buf[9];

	if (dt->tm_year < 100 || dt->tm_year > 199) {
		dev_err(&spi->dev, "Year must be between 2000 and 2099. It's %d.\n",
			dt->tm_year + 1900);
		return -EINVAL;
	}

	buf[0] = MAX6916_CLOCK_BURST & 0x7F;
	buf[1] = bin2bcd(dt->tm_sec);
	buf[2] = bin2bcd(dt->tm_min);

Annotation

Implementation Notes