^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * drivers/rtc/rtc-pcf8583.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2000 Russell King
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2008 Wolfram Sang & Juergen Beisert, Pengutronix
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Driver for PCF8583 RTC & RAM chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Converted to the generic RTC susbsystem by G. Liakhovetski (2006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/i2c.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) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/bcd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct rtc_mem {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned int loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) unsigned char *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct pcf8583 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct rtc_device *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned char ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define CTRL_STOP 0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define CTRL_HOLD 0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define CTRL_32KHZ 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define CTRL_MASK 0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define CTRL_ALARMEN 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define CTRL_ALARM 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define CTRL_TIMER 0x01
^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 struct i2c_driver pcf8583_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define get_ctrl(x) ((struct pcf8583 *)i2c_get_clientdata(x))->ctrl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define set_ctrl(x, v) get_ctrl(x) = v
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define CMOS_YEAR (64 + 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define CMOS_CHECKSUM (63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int pcf8583_get_datetime(struct i2c_client *client, struct rtc_time *dt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned char buf[8], addr[1] = { 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct i2c_msg msgs[2] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .addr = client->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .flags = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) .len = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) .buf = addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) .addr = client->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) .flags = I2C_M_RD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) .len = 6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) .buf = buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) memset(buf, 0, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) ret = i2c_transfer(client->adapter, msgs, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (ret == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) dt->tm_year = buf[4] >> 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) dt->tm_wday = buf[5] >> 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) buf[4] &= 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) buf[5] &= 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) dt->tm_sec = bcd2bin(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) dt->tm_min = bcd2bin(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) dt->tm_hour = bcd2bin(buf[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) dt->tm_mday = bcd2bin(buf[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) dt->tm_mon = bcd2bin(buf[5]) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return ret == 2 ? 0 : -EIO;
^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 pcf8583_set_datetime(struct i2c_client *client, struct rtc_time *dt, int datetoo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) unsigned char buf[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int ret, len = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) buf[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) buf[1] = get_ctrl(client) | 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) buf[2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) buf[3] = bin2bcd(dt->tm_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) buf[4] = bin2bcd(dt->tm_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) buf[5] = bin2bcd(dt->tm_hour);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (datetoo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) len = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) buf[6] = bin2bcd(dt->tm_mday) | (dt->tm_year << 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) buf[7] = bin2bcd(dt->tm_mon + 1) | (dt->tm_wday << 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ret = i2c_master_send(client, (char *)buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (ret != len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) buf[1] = get_ctrl(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ret = i2c_master_send(client, (char *)buf, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return ret == 2 ? 0 : -EIO;
^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 int pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) *ctrl = get_ctrl(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return 0;
^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) static int pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) unsigned char buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) buf[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) buf[1] = *ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) set_ctrl(client, *ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return i2c_master_send(client, (char *)buf, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int pcf8583_read_mem(struct i2c_client *client, struct rtc_mem *mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) unsigned char addr[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct i2c_msg msgs[2] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) .addr = client->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) .flags = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) .len = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) .buf = addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .addr = client->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .flags = I2C_M_RD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .len = mem->nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .buf = mem->data,
^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) if (mem->loc < 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) addr[0] = mem->loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int pcf8583_write_mem(struct i2c_client *client, struct rtc_mem *mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) unsigned char buf[9];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (mem->loc < 8 || mem->nr > 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) buf[0] = mem->loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) memcpy(buf + 1, mem->data, mem->nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ret = i2c_master_send(client, buf, mem->nr + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return ret == mem->nr + 1 ? 0 : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static int pcf8583_rtc_read_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) unsigned char ctrl, year[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct rtc_mem mem = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .loc = CMOS_YEAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .nr = sizeof(year),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .data = year
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int real_year, year_offset, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * Ensure that the RTC is running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) pcf8583_get_ctrl(client, &ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (ctrl & (CTRL_STOP | CTRL_HOLD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) unsigned char new_ctrl = ctrl & ~(CTRL_STOP | CTRL_HOLD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) dev_warn(dev, "resetting control %02x -> %02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) ctrl, new_ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) err = pcf8583_set_ctrl(client, &new_ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (pcf8583_get_datetime(client, tm) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) pcf8583_read_mem(client, &mem))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) real_year = year[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * The RTC year holds the LSB two bits of the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * year, which should reflect the LSB two bits of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * CMOS copy of the year. Any difference indicates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * that we have to correct the CMOS version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) year_offset = tm->tm_year - (real_year & 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (year_offset < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * RTC year wrapped. Adjust it appropriately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) year_offset += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) tm->tm_year = (real_year + year_offset + year[1] * 100) - 1900;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static int pcf8583_rtc_set_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) unsigned char year[2], chk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct rtc_mem cmos_year = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) .loc = CMOS_YEAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) .nr = sizeof(year),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) .data = year
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct rtc_mem cmos_check = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) .loc = CMOS_CHECKSUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) .nr = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) .data = &chk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) unsigned int proper_year = tm->tm_year + 1900;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * The RTC's own 2-bit year must reflect the least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * significant two bits of the CMOS year.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) ret = pcf8583_set_datetime(client, tm, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) ret = pcf8583_read_mem(client, &cmos_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ret = pcf8583_read_mem(client, &cmos_year);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) chk -= year[1] + year[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) year[1] = proper_year / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) year[0] = proper_year % 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) chk += year[1] + year[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) ret = pcf8583_write_mem(client, &cmos_year);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) ret = pcf8583_write_mem(client, &cmos_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static const struct rtc_class_ops pcf8583_rtc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) .read_time = pcf8583_rtc_read_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) .set_time = pcf8583_rtc_set_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static int pcf8583_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct pcf8583 *pcf8583;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) pcf8583 = devm_kzalloc(&client->dev, sizeof(struct pcf8583),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (!pcf8583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) i2c_set_clientdata(client, pcf8583);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) pcf8583->rtc = devm_rtc_device_register(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) pcf8583_driver.driver.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) &pcf8583_rtc_ops, THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return PTR_ERR_OR_ZERO(pcf8583->rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static const struct i2c_device_id pcf8583_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) { "pcf8583", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) MODULE_DEVICE_TABLE(i2c, pcf8583_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static struct i2c_driver pcf8583_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) .name = "pcf8583",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) .probe = pcf8583_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) .id_table = pcf8583_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) module_i2c_driver(pcf8583_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) MODULE_AUTHOR("Russell King");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) MODULE_LICENSE("GPL");