13c2b9075SWolfgang Grandegger /* 23c2b9075SWolfgang Grandegger * Driver for Epson's RTC module RX-8025 SA/NB 33c2b9075SWolfgang Grandegger * 43c2b9075SWolfgang Grandegger * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com> 53c2b9075SWolfgang Grandegger * 63c2b9075SWolfgang Grandegger * Copyright (C) 2005 by Digi International Inc. 73c2b9075SWolfgang Grandegger * All rights reserved. 83c2b9075SWolfgang Grandegger * 93c2b9075SWolfgang Grandegger * Modified by fengjh at rising.com.cn 103c2b9075SWolfgang Grandegger * <http://lists.lm-sensors.org/mailman/listinfo/lm-sensors> 113c2b9075SWolfgang Grandegger * 2006.11 123c2b9075SWolfgang Grandegger * 133c2b9075SWolfgang Grandegger * Code cleanup by Sergei Poselenov, <sposelenov@emcraft.com> 143c2b9075SWolfgang Grandegger * Converted to new style by Wolfgang Grandegger <wg@grandegger.com> 153c2b9075SWolfgang Grandegger * Alarm and periodic interrupt added by Dmitry Rakhchev <rda@emcraft.com> 163c2b9075SWolfgang Grandegger * 173c2b9075SWolfgang Grandegger * This program is free software; you can redistribute it and/or 183c2b9075SWolfgang Grandegger * modify it under the terms of the GNU General Public License 193c2b9075SWolfgang Grandegger * version 2 as published by the Free Software Foundation. 203c2b9075SWolfgang Grandegger */ 213c2b9075SWolfgang Grandegger #include <linux/bcd.h> 22*2ddd1869SAlexandre Belloni #include <linux/bitops.h> 233c2b9075SWolfgang Grandegger #include <linux/i2c.h> 2415d3bdc2SAlexandre Belloni #include <linux/kernel.h> 2515d3bdc2SAlexandre Belloni #include <linux/module.h> 263c2b9075SWolfgang Grandegger #include <linux/rtc.h> 273c2b9075SWolfgang Grandegger 283c2b9075SWolfgang Grandegger /* Register definitions */ 293c2b9075SWolfgang Grandegger #define RX8025_REG_SEC 0x00 303c2b9075SWolfgang Grandegger #define RX8025_REG_MIN 0x01 313c2b9075SWolfgang Grandegger #define RX8025_REG_HOUR 0x02 323c2b9075SWolfgang Grandegger #define RX8025_REG_WDAY 0x03 333c2b9075SWolfgang Grandegger #define RX8025_REG_MDAY 0x04 343c2b9075SWolfgang Grandegger #define RX8025_REG_MONTH 0x05 353c2b9075SWolfgang Grandegger #define RX8025_REG_YEAR 0x06 363c2b9075SWolfgang Grandegger #define RX8025_REG_DIGOFF 0x07 373c2b9075SWolfgang Grandegger #define RX8025_REG_ALWMIN 0x08 383c2b9075SWolfgang Grandegger #define RX8025_REG_ALWHOUR 0x09 393c2b9075SWolfgang Grandegger #define RX8025_REG_ALWWDAY 0x0a 403c2b9075SWolfgang Grandegger #define RX8025_REG_ALDMIN 0x0b 413c2b9075SWolfgang Grandegger #define RX8025_REG_ALDHOUR 0x0c 423c2b9075SWolfgang Grandegger /* 0x0d is reserved */ 433c2b9075SWolfgang Grandegger #define RX8025_REG_CTRL1 0x0e 443c2b9075SWolfgang Grandegger #define RX8025_REG_CTRL2 0x0f 453c2b9075SWolfgang Grandegger 463c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL1_CT (7 << 0) 473c2b9075SWolfgang Grandegger /* 1 Hz periodic level irq */ 483c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL1_CT_1HZ 4 49*2ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL1_TEST BIT(3) 50*2ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL1_1224 BIT(5) 51*2ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL1_DALE BIT(6) 52*2ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL1_WALE BIT(7) 533c2b9075SWolfgang Grandegger 54*2ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_DAFG BIT(0) 55*2ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_WAFG BIT(1) 56*2ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_CTFG BIT(2) 57*2ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_PON BIT(4) 58*2ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_XST BIT(5) 59*2ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_VDET BIT(6) 603c2b9075SWolfgang Grandegger 613c2b9075SWolfgang Grandegger /* Clock precision adjustment */ 623c2b9075SWolfgang Grandegger #define RX8025_ADJ_RESOLUTION 3050 /* in ppb */ 633c2b9075SWolfgang Grandegger #define RX8025_ADJ_DATA_MAX 62 643c2b9075SWolfgang Grandegger #define RX8025_ADJ_DATA_MIN -62 653c2b9075SWolfgang Grandegger 663c2b9075SWolfgang Grandegger static const struct i2c_device_id rx8025_id[] = { 673c2b9075SWolfgang Grandegger { "rx8025", 0 }, 683c2b9075SWolfgang Grandegger { } 693c2b9075SWolfgang Grandegger }; 703c2b9075SWolfgang Grandegger MODULE_DEVICE_TABLE(i2c, rx8025_id); 713c2b9075SWolfgang Grandegger 723c2b9075SWolfgang Grandegger struct rx8025_data { 733c2b9075SWolfgang Grandegger struct i2c_client *client; 743c2b9075SWolfgang Grandegger struct rtc_device *rtc; 753c2b9075SWolfgang Grandegger u8 ctrl1; 763c2b9075SWolfgang Grandegger }; 773c2b9075SWolfgang Grandegger 783c2b9075SWolfgang Grandegger static int rx8025_read_reg(struct i2c_client *client, int number, u8 *value) 793c2b9075SWolfgang Grandegger { 803c2b9075SWolfgang Grandegger int ret = i2c_smbus_read_byte_data(client, (number << 4) | 0x08); 813c2b9075SWolfgang Grandegger 823c2b9075SWolfgang Grandegger if (ret < 0) { 833c2b9075SWolfgang Grandegger dev_err(&client->dev, "Unable to read register #%d\n", number); 843c2b9075SWolfgang Grandegger return ret; 853c2b9075SWolfgang Grandegger } 863c2b9075SWolfgang Grandegger 873c2b9075SWolfgang Grandegger *value = ret; 883c2b9075SWolfgang Grandegger return 0; 893c2b9075SWolfgang Grandegger } 903c2b9075SWolfgang Grandegger 913c2b9075SWolfgang Grandegger static int rx8025_read_regs(struct i2c_client *client, 923c2b9075SWolfgang Grandegger int number, u8 length, u8 *values) 933c2b9075SWolfgang Grandegger { 943c2b9075SWolfgang Grandegger int ret = i2c_smbus_read_i2c_block_data(client, (number << 4) | 0x08, 953c2b9075SWolfgang Grandegger length, values); 963c2b9075SWolfgang Grandegger 973c2b9075SWolfgang Grandegger if (ret != length) { 983c2b9075SWolfgang Grandegger dev_err(&client->dev, "Unable to read registers #%d..#%d\n", 993c2b9075SWolfgang Grandegger number, number + length - 1); 1003c2b9075SWolfgang Grandegger return ret < 0 ? ret : -EIO; 1013c2b9075SWolfgang Grandegger } 1023c2b9075SWolfgang Grandegger 1033c2b9075SWolfgang Grandegger return 0; 1043c2b9075SWolfgang Grandegger } 1053c2b9075SWolfgang Grandegger 1063c2b9075SWolfgang Grandegger static int rx8025_write_reg(struct i2c_client *client, int number, u8 value) 1073c2b9075SWolfgang Grandegger { 1083c2b9075SWolfgang Grandegger int ret = i2c_smbus_write_byte_data(client, number << 4, value); 1093c2b9075SWolfgang Grandegger 1103c2b9075SWolfgang Grandegger if (ret) 1113c2b9075SWolfgang Grandegger dev_err(&client->dev, "Unable to write register #%d\n", 1123c2b9075SWolfgang Grandegger number); 1133c2b9075SWolfgang Grandegger 1143c2b9075SWolfgang Grandegger return ret; 1153c2b9075SWolfgang Grandegger } 1163c2b9075SWolfgang Grandegger 1173c2b9075SWolfgang Grandegger static int rx8025_write_regs(struct i2c_client *client, 1183c2b9075SWolfgang Grandegger int number, u8 length, u8 *values) 1193c2b9075SWolfgang Grandegger { 1203c2b9075SWolfgang Grandegger int ret = i2c_smbus_write_i2c_block_data(client, (number << 4) | 0x08, 1213c2b9075SWolfgang Grandegger length, values); 1223c2b9075SWolfgang Grandegger 1233c2b9075SWolfgang Grandegger if (ret) 1243c2b9075SWolfgang Grandegger dev_err(&client->dev, "Unable to write registers #%d..#%d\n", 1253c2b9075SWolfgang Grandegger number, number + length - 1); 1263c2b9075SWolfgang Grandegger 1273c2b9075SWolfgang Grandegger return ret; 1283c2b9075SWolfgang Grandegger } 1293c2b9075SWolfgang Grandegger 130b6a57c95SAlexandre Belloni static irqreturn_t rx8025_handle_irq(int irq, void *dev_id) 1313c2b9075SWolfgang Grandegger { 1323c2b9075SWolfgang Grandegger struct i2c_client *client = dev_id; 1333c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = i2c_get_clientdata(client); 1343c2b9075SWolfgang Grandegger u8 status; 1353c2b9075SWolfgang Grandegger 1363c2b9075SWolfgang Grandegger if (rx8025_read_reg(client, RX8025_REG_CTRL2, &status)) 1373c2b9075SWolfgang Grandegger goto out; 1383c2b9075SWolfgang Grandegger 1393c2b9075SWolfgang Grandegger if (!(status & RX8025_BIT_CTRL2_XST)) 1403c2b9075SWolfgang Grandegger dev_warn(&client->dev, "Oscillation stop was detected," 1413c2b9075SWolfgang Grandegger "you may have to readjust the clock\n"); 1423c2b9075SWolfgang Grandegger 1433c2b9075SWolfgang Grandegger if (status & RX8025_BIT_CTRL2_CTFG) { 1443c2b9075SWolfgang Grandegger /* periodic */ 1453c2b9075SWolfgang Grandegger status &= ~RX8025_BIT_CTRL2_CTFG; 1463c2b9075SWolfgang Grandegger rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF); 1473c2b9075SWolfgang Grandegger } 1483c2b9075SWolfgang Grandegger 1493c2b9075SWolfgang Grandegger if (status & RX8025_BIT_CTRL2_DAFG) { 1503c2b9075SWolfgang Grandegger /* alarm */ 1513c2b9075SWolfgang Grandegger status &= RX8025_BIT_CTRL2_DAFG; 1523c2b9075SWolfgang Grandegger if (rx8025_write_reg(client, RX8025_REG_CTRL1, 1533c2b9075SWolfgang Grandegger rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE)) 1543c2b9075SWolfgang Grandegger goto out; 1553c2b9075SWolfgang Grandegger rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF); 1563c2b9075SWolfgang Grandegger } 1573c2b9075SWolfgang Grandegger 1583c2b9075SWolfgang Grandegger /* acknowledge IRQ */ 1593c2b9075SWolfgang Grandegger rx8025_write_reg(client, RX8025_REG_CTRL2, 1603c2b9075SWolfgang Grandegger status | RX8025_BIT_CTRL2_XST); 1613c2b9075SWolfgang Grandegger 1623c2b9075SWolfgang Grandegger out: 163b6a57c95SAlexandre Belloni return IRQ_HANDLED; 1643c2b9075SWolfgang Grandegger } 1653c2b9075SWolfgang Grandegger 1663c2b9075SWolfgang Grandegger static int rx8025_get_time(struct device *dev, struct rtc_time *dt) 1673c2b9075SWolfgang Grandegger { 1683c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev); 1693c2b9075SWolfgang Grandegger u8 date[7]; 1703c2b9075SWolfgang Grandegger int err; 1713c2b9075SWolfgang Grandegger 1723c2b9075SWolfgang Grandegger err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date); 1733c2b9075SWolfgang Grandegger if (err) 1743c2b9075SWolfgang Grandegger return err; 1753c2b9075SWolfgang Grandegger 1763c2b9075SWolfgang Grandegger dev_dbg(dev, "%s: read 0x%02x 0x%02x " 1773c2b9075SWolfgang Grandegger "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__, 1783c2b9075SWolfgang Grandegger date[0], date[1], date[2], date[3], date[4], 1793c2b9075SWolfgang Grandegger date[5], date[6]); 1803c2b9075SWolfgang Grandegger 1813c2b9075SWolfgang Grandegger dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f); 1823c2b9075SWolfgang Grandegger dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f); 1833c2b9075SWolfgang Grandegger if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) 1843c2b9075SWolfgang Grandegger dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f); 1853c2b9075SWolfgang Grandegger else 1863c2b9075SWolfgang Grandegger dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12 1873c2b9075SWolfgang Grandegger + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0); 1883c2b9075SWolfgang Grandegger 1893c2b9075SWolfgang Grandegger dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f); 1903c2b9075SWolfgang Grandegger dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1; 1913c2b9075SWolfgang Grandegger dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]); 1923c2b9075SWolfgang Grandegger 1933c2b9075SWolfgang Grandegger if (dt->tm_year < 70) 1943c2b9075SWolfgang Grandegger dt->tm_year += 100; 1953c2b9075SWolfgang Grandegger 1963c2b9075SWolfgang Grandegger dev_dbg(dev, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__, 1973c2b9075SWolfgang Grandegger dt->tm_sec, dt->tm_min, dt->tm_hour, 1983c2b9075SWolfgang Grandegger dt->tm_mday, dt->tm_mon, dt->tm_year); 1993c2b9075SWolfgang Grandegger 2003c2b9075SWolfgang Grandegger return rtc_valid_tm(dt); 2013c2b9075SWolfgang Grandegger } 2023c2b9075SWolfgang Grandegger 2033c2b9075SWolfgang Grandegger static int rx8025_set_time(struct device *dev, struct rtc_time *dt) 2043c2b9075SWolfgang Grandegger { 2053c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev); 2063c2b9075SWolfgang Grandegger u8 date[7]; 2073c2b9075SWolfgang Grandegger 2083c2b9075SWolfgang Grandegger /* 2093c2b9075SWolfgang Grandegger * BUG: The HW assumes every year that is a multiple of 4 to be a leap 2103c2b9075SWolfgang Grandegger * year. Next time this is wrong is 2100, which will not be a leap 2113c2b9075SWolfgang Grandegger * year. 2123c2b9075SWolfgang Grandegger */ 2133c2b9075SWolfgang Grandegger 2143c2b9075SWolfgang Grandegger /* 2153c2b9075SWolfgang Grandegger * Here the read-only bits are written as "0". I'm not sure if that 2163c2b9075SWolfgang Grandegger * is sound. 2173c2b9075SWolfgang Grandegger */ 2183c2b9075SWolfgang Grandegger date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec); 2193c2b9075SWolfgang Grandegger date[RX8025_REG_MIN] = bin2bcd(dt->tm_min); 2203c2b9075SWolfgang Grandegger if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) 2213c2b9075SWolfgang Grandegger date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour); 2223c2b9075SWolfgang Grandegger else 2233c2b9075SWolfgang Grandegger date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0) 2243c2b9075SWolfgang Grandegger | bin2bcd((dt->tm_hour + 11) % 12 + 1); 2253c2b9075SWolfgang Grandegger 2263c2b9075SWolfgang Grandegger date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday); 2273c2b9075SWolfgang Grandegger date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday); 2283c2b9075SWolfgang Grandegger date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1); 2293c2b9075SWolfgang Grandegger date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year % 100); 2303c2b9075SWolfgang Grandegger 2313c2b9075SWolfgang Grandegger dev_dbg(dev, 2323c2b9075SWolfgang Grandegger "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", 2333c2b9075SWolfgang Grandegger __func__, 2343c2b9075SWolfgang Grandegger date[0], date[1], date[2], date[3], date[4], date[5], date[6]); 2353c2b9075SWolfgang Grandegger 2363c2b9075SWolfgang Grandegger return rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date); 2373c2b9075SWolfgang Grandegger } 2383c2b9075SWolfgang Grandegger 2393c2b9075SWolfgang Grandegger static int rx8025_init_client(struct i2c_client *client, int *need_reset) 2403c2b9075SWolfgang Grandegger { 2413c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = i2c_get_clientdata(client); 2423c2b9075SWolfgang Grandegger u8 ctrl[2], ctrl2; 2433c2b9075SWolfgang Grandegger int need_clear = 0; 2443c2b9075SWolfgang Grandegger int err; 2453c2b9075SWolfgang Grandegger 2463c2b9075SWolfgang Grandegger err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl); 2473c2b9075SWolfgang Grandegger if (err) 2483c2b9075SWolfgang Grandegger goto out; 2493c2b9075SWolfgang Grandegger 2503c2b9075SWolfgang Grandegger /* Keep test bit zero ! */ 2513c2b9075SWolfgang Grandegger rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST; 2523c2b9075SWolfgang Grandegger 2533c2b9075SWolfgang Grandegger if (ctrl[1] & RX8025_BIT_CTRL2_PON) { 2543c2b9075SWolfgang Grandegger dev_warn(&client->dev, "power-on reset was detected, " 2553c2b9075SWolfgang Grandegger "you may have to readjust the clock\n"); 2563c2b9075SWolfgang Grandegger *need_reset = 1; 2573c2b9075SWolfgang Grandegger } 2583c2b9075SWolfgang Grandegger 2593c2b9075SWolfgang Grandegger if (ctrl[1] & RX8025_BIT_CTRL2_VDET) { 2603c2b9075SWolfgang Grandegger dev_warn(&client->dev, "a power voltage drop was detected, " 2613c2b9075SWolfgang Grandegger "you may have to readjust the clock\n"); 2623c2b9075SWolfgang Grandegger *need_reset = 1; 2633c2b9075SWolfgang Grandegger } 2643c2b9075SWolfgang Grandegger 2653c2b9075SWolfgang Grandegger if (!(ctrl[1] & RX8025_BIT_CTRL2_XST)) { 2663c2b9075SWolfgang Grandegger dev_warn(&client->dev, "Oscillation stop was detected," 2673c2b9075SWolfgang Grandegger "you may have to readjust the clock\n"); 2683c2b9075SWolfgang Grandegger *need_reset = 1; 2693c2b9075SWolfgang Grandegger } 2703c2b9075SWolfgang Grandegger 2713c2b9075SWolfgang Grandegger if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) { 2723c2b9075SWolfgang Grandegger dev_warn(&client->dev, "Alarm was detected\n"); 2733c2b9075SWolfgang Grandegger need_clear = 1; 2743c2b9075SWolfgang Grandegger } 2753c2b9075SWolfgang Grandegger 2763c2b9075SWolfgang Grandegger if (!(ctrl[1] & RX8025_BIT_CTRL2_CTFG)) 2773c2b9075SWolfgang Grandegger need_clear = 1; 2783c2b9075SWolfgang Grandegger 2793c2b9075SWolfgang Grandegger if (*need_reset || need_clear) { 2803c2b9075SWolfgang Grandegger ctrl2 = ctrl[0]; 2813c2b9075SWolfgang Grandegger ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET | 2823c2b9075SWolfgang Grandegger RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG | 2833c2b9075SWolfgang Grandegger RX8025_BIT_CTRL2_DAFG); 2843c2b9075SWolfgang Grandegger ctrl2 |= RX8025_BIT_CTRL2_XST; 2853c2b9075SWolfgang Grandegger 2863c2b9075SWolfgang Grandegger err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2); 2873c2b9075SWolfgang Grandegger } 2883c2b9075SWolfgang Grandegger out: 2893c2b9075SWolfgang Grandegger return err; 2903c2b9075SWolfgang Grandegger } 2913c2b9075SWolfgang Grandegger 2923c2b9075SWolfgang Grandegger /* Alarm support */ 2933c2b9075SWolfgang Grandegger static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t) 2943c2b9075SWolfgang Grandegger { 2953c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev); 2963c2b9075SWolfgang Grandegger struct i2c_client *client = rx8025->client; 2973c2b9075SWolfgang Grandegger u8 ctrl2, ald[2]; 2983c2b9075SWolfgang Grandegger int err; 2993c2b9075SWolfgang Grandegger 3003c2b9075SWolfgang Grandegger if (client->irq <= 0) 3013c2b9075SWolfgang Grandegger return -EINVAL; 3023c2b9075SWolfgang Grandegger 3033c2b9075SWolfgang Grandegger err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald); 3043c2b9075SWolfgang Grandegger if (err) 3053c2b9075SWolfgang Grandegger return err; 3063c2b9075SWolfgang Grandegger 3073c2b9075SWolfgang Grandegger err = rx8025_read_reg(client, RX8025_REG_CTRL2, &ctrl2); 3083c2b9075SWolfgang Grandegger if (err) 3093c2b9075SWolfgang Grandegger return err; 3103c2b9075SWolfgang Grandegger 3113c2b9075SWolfgang Grandegger dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n", 3123c2b9075SWolfgang Grandegger __func__, ald[0], ald[1], ctrl2); 3133c2b9075SWolfgang Grandegger 3143c2b9075SWolfgang Grandegger /* Hardware alarms precision is 1 minute! */ 3153c2b9075SWolfgang Grandegger t->time.tm_sec = 0; 3163c2b9075SWolfgang Grandegger t->time.tm_min = bcd2bin(ald[0] & 0x7f); 3173c2b9075SWolfgang Grandegger if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) 3183c2b9075SWolfgang Grandegger t->time.tm_hour = bcd2bin(ald[1] & 0x3f); 3193c2b9075SWolfgang Grandegger else 3203c2b9075SWolfgang Grandegger t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12 3213c2b9075SWolfgang Grandegger + (ald[1] & 0x20 ? 12 : 0); 3223c2b9075SWolfgang Grandegger 3233c2b9075SWolfgang Grandegger t->time.tm_wday = -1; 3243c2b9075SWolfgang Grandegger t->time.tm_mday = -1; 3253c2b9075SWolfgang Grandegger t->time.tm_mon = -1; 3263c2b9075SWolfgang Grandegger t->time.tm_year = -1; 3273c2b9075SWolfgang Grandegger 3283c2b9075SWolfgang Grandegger dev_dbg(dev, "%s: date: %ds %dm %dh %dmd %dm %dy\n", 3293c2b9075SWolfgang Grandegger __func__, 3303c2b9075SWolfgang Grandegger t->time.tm_sec, t->time.tm_min, t->time.tm_hour, 3313c2b9075SWolfgang Grandegger t->time.tm_mday, t->time.tm_mon, t->time.tm_year); 3323c2b9075SWolfgang Grandegger t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE); 3333c2b9075SWolfgang Grandegger t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled; 3343c2b9075SWolfgang Grandegger 3353c2b9075SWolfgang Grandegger return err; 3363c2b9075SWolfgang Grandegger } 3373c2b9075SWolfgang Grandegger 3383c2b9075SWolfgang Grandegger static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t) 3393c2b9075SWolfgang Grandegger { 3403c2b9075SWolfgang Grandegger struct i2c_client *client = to_i2c_client(dev); 3413c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev); 3423c2b9075SWolfgang Grandegger u8 ald[2]; 3433c2b9075SWolfgang Grandegger int err; 3443c2b9075SWolfgang Grandegger 3453c2b9075SWolfgang Grandegger if (client->irq <= 0) 3463c2b9075SWolfgang Grandegger return -EINVAL; 3473c2b9075SWolfgang Grandegger 3483c2b9075SWolfgang Grandegger /* Hardware alarm precision is 1 minute! */ 3493c2b9075SWolfgang Grandegger ald[0] = bin2bcd(t->time.tm_min); 3503c2b9075SWolfgang Grandegger if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) 3513c2b9075SWolfgang Grandegger ald[1] = bin2bcd(t->time.tm_hour); 3523c2b9075SWolfgang Grandegger else 3533c2b9075SWolfgang Grandegger ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0) 3543c2b9075SWolfgang Grandegger | bin2bcd((t->time.tm_hour + 11) % 12 + 1); 3553c2b9075SWolfgang Grandegger 3563c2b9075SWolfgang Grandegger dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]); 3573c2b9075SWolfgang Grandegger 3583c2b9075SWolfgang Grandegger if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) { 3593c2b9075SWolfgang Grandegger rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE; 3603c2b9075SWolfgang Grandegger err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, 3613c2b9075SWolfgang Grandegger rx8025->ctrl1); 3623c2b9075SWolfgang Grandegger if (err) 3633c2b9075SWolfgang Grandegger return err; 3643c2b9075SWolfgang Grandegger } 3653c2b9075SWolfgang Grandegger err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald); 3663c2b9075SWolfgang Grandegger if (err) 3673c2b9075SWolfgang Grandegger return err; 3683c2b9075SWolfgang Grandegger 3693c2b9075SWolfgang Grandegger if (t->enabled) { 3703c2b9075SWolfgang Grandegger rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE; 3713c2b9075SWolfgang Grandegger err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, 3723c2b9075SWolfgang Grandegger rx8025->ctrl1); 3733c2b9075SWolfgang Grandegger if (err) 3743c2b9075SWolfgang Grandegger return err; 3753c2b9075SWolfgang Grandegger } 3763c2b9075SWolfgang Grandegger 3773c2b9075SWolfgang Grandegger return 0; 3783c2b9075SWolfgang Grandegger } 3793c2b9075SWolfgang Grandegger 3803c2b9075SWolfgang Grandegger static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled) 3813c2b9075SWolfgang Grandegger { 3823c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev); 3833c2b9075SWolfgang Grandegger u8 ctrl1; 3843c2b9075SWolfgang Grandegger int err; 3853c2b9075SWolfgang Grandegger 3863c2b9075SWolfgang Grandegger ctrl1 = rx8025->ctrl1; 3873c2b9075SWolfgang Grandegger if (enabled) 3883c2b9075SWolfgang Grandegger ctrl1 |= RX8025_BIT_CTRL1_DALE; 3893c2b9075SWolfgang Grandegger else 3903c2b9075SWolfgang Grandegger ctrl1 &= ~RX8025_BIT_CTRL1_DALE; 3913c2b9075SWolfgang Grandegger 3923c2b9075SWolfgang Grandegger if (ctrl1 != rx8025->ctrl1) { 3933c2b9075SWolfgang Grandegger rx8025->ctrl1 = ctrl1; 3943c2b9075SWolfgang Grandegger err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, 3953c2b9075SWolfgang Grandegger rx8025->ctrl1); 3963c2b9075SWolfgang Grandegger if (err) 3973c2b9075SWolfgang Grandegger return err; 3983c2b9075SWolfgang Grandegger } 3993c2b9075SWolfgang Grandegger return 0; 4003c2b9075SWolfgang Grandegger } 4013c2b9075SWolfgang Grandegger 4023c2b9075SWolfgang Grandegger static struct rtc_class_ops rx8025_rtc_ops = { 4033c2b9075SWolfgang Grandegger .read_time = rx8025_get_time, 4043c2b9075SWolfgang Grandegger .set_time = rx8025_set_time, 4053c2b9075SWolfgang Grandegger .read_alarm = rx8025_read_alarm, 4063c2b9075SWolfgang Grandegger .set_alarm = rx8025_set_alarm, 4073c2b9075SWolfgang Grandegger .alarm_irq_enable = rx8025_alarm_irq_enable, 4083c2b9075SWolfgang Grandegger }; 4093c2b9075SWolfgang Grandegger 4103c2b9075SWolfgang Grandegger /* 4113c2b9075SWolfgang Grandegger * Clock precision adjustment support 4123c2b9075SWolfgang Grandegger * 4133c2b9075SWolfgang Grandegger * According to the RX8025 SA/NB application manual the frequency and 414b770ffd4SThomas Weber * temperature characteristics can be approximated using the following 4153c2b9075SWolfgang Grandegger * equation: 4163c2b9075SWolfgang Grandegger * 4173c2b9075SWolfgang Grandegger * df = a * (ut - t)**2 4183c2b9075SWolfgang Grandegger * 4193c2b9075SWolfgang Grandegger * df: Frequency deviation in any temperature 4203c2b9075SWolfgang Grandegger * a : Coefficient = (-35 +-5) * 10**-9 4213c2b9075SWolfgang Grandegger * ut: Ultimate temperature in degree = +25 +-5 degree 4223c2b9075SWolfgang Grandegger * t : Any temperature in degree 4233c2b9075SWolfgang Grandegger * 4243c2b9075SWolfgang Grandegger * Note that the clock adjustment in ppb must be entered (which is 4253c2b9075SWolfgang Grandegger * the negative value of the deviation). 4263c2b9075SWolfgang Grandegger */ 4273c2b9075SWolfgang Grandegger static int rx8025_get_clock_adjust(struct device *dev, int *adj) 4283c2b9075SWolfgang Grandegger { 4293c2b9075SWolfgang Grandegger struct i2c_client *client = to_i2c_client(dev); 4303c2b9075SWolfgang Grandegger u8 digoff; 4313c2b9075SWolfgang Grandegger int err; 4323c2b9075SWolfgang Grandegger 4333c2b9075SWolfgang Grandegger err = rx8025_read_reg(client, RX8025_REG_DIGOFF, &digoff); 4343c2b9075SWolfgang Grandegger if (err) 4353c2b9075SWolfgang Grandegger return err; 4363c2b9075SWolfgang Grandegger 4373c2b9075SWolfgang Grandegger *adj = digoff >= 64 ? digoff - 128 : digoff; 4383c2b9075SWolfgang Grandegger if (*adj > 0) 4393c2b9075SWolfgang Grandegger (*adj)--; 4403c2b9075SWolfgang Grandegger *adj *= -RX8025_ADJ_RESOLUTION; 4413c2b9075SWolfgang Grandegger 4423c2b9075SWolfgang Grandegger return 0; 4433c2b9075SWolfgang Grandegger } 4443c2b9075SWolfgang Grandegger 4453c2b9075SWolfgang Grandegger static int rx8025_set_clock_adjust(struct device *dev, int adj) 4463c2b9075SWolfgang Grandegger { 4473c2b9075SWolfgang Grandegger struct i2c_client *client = to_i2c_client(dev); 4483c2b9075SWolfgang Grandegger u8 digoff; 4493c2b9075SWolfgang Grandegger int err; 4503c2b9075SWolfgang Grandegger 4513c2b9075SWolfgang Grandegger adj /= -RX8025_ADJ_RESOLUTION; 4523c2b9075SWolfgang Grandegger if (adj > RX8025_ADJ_DATA_MAX) 4533c2b9075SWolfgang Grandegger adj = RX8025_ADJ_DATA_MAX; 4543c2b9075SWolfgang Grandegger else if (adj < RX8025_ADJ_DATA_MIN) 4553c2b9075SWolfgang Grandegger adj = RX8025_ADJ_DATA_MIN; 4563c2b9075SWolfgang Grandegger else if (adj > 0) 4573c2b9075SWolfgang Grandegger adj++; 4583c2b9075SWolfgang Grandegger else if (adj < 0) 4593c2b9075SWolfgang Grandegger adj += 128; 4603c2b9075SWolfgang Grandegger digoff = adj; 4613c2b9075SWolfgang Grandegger 4623c2b9075SWolfgang Grandegger err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff); 4633c2b9075SWolfgang Grandegger if (err) 4643c2b9075SWolfgang Grandegger return err; 4653c2b9075SWolfgang Grandegger 4663c2b9075SWolfgang Grandegger dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff); 4673c2b9075SWolfgang Grandegger 4683c2b9075SWolfgang Grandegger return 0; 4693c2b9075SWolfgang Grandegger } 4703c2b9075SWolfgang Grandegger 4713c2b9075SWolfgang Grandegger static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev, 4723c2b9075SWolfgang Grandegger struct device_attribute *attr, 4733c2b9075SWolfgang Grandegger char *buf) 4743c2b9075SWolfgang Grandegger { 4753c2b9075SWolfgang Grandegger int err, adj; 4763c2b9075SWolfgang Grandegger 4773c2b9075SWolfgang Grandegger err = rx8025_get_clock_adjust(dev, &adj); 4783c2b9075SWolfgang Grandegger if (err) 4793c2b9075SWolfgang Grandegger return err; 4803c2b9075SWolfgang Grandegger 4813c2b9075SWolfgang Grandegger return sprintf(buf, "%d\n", adj); 4823c2b9075SWolfgang Grandegger } 4833c2b9075SWolfgang Grandegger 4843c2b9075SWolfgang Grandegger static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev, 4853c2b9075SWolfgang Grandegger struct device_attribute *attr, 4863c2b9075SWolfgang Grandegger const char *buf, size_t count) 4873c2b9075SWolfgang Grandegger { 4883c2b9075SWolfgang Grandegger int adj, err; 4893c2b9075SWolfgang Grandegger 4903c2b9075SWolfgang Grandegger if (sscanf(buf, "%i", &adj) != 1) 4913c2b9075SWolfgang Grandegger return -EINVAL; 4923c2b9075SWolfgang Grandegger 4933c2b9075SWolfgang Grandegger err = rx8025_set_clock_adjust(dev, adj); 4943c2b9075SWolfgang Grandegger 4953c2b9075SWolfgang Grandegger return err ? err : count; 4963c2b9075SWolfgang Grandegger } 4973c2b9075SWolfgang Grandegger 4983c2b9075SWolfgang Grandegger static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR, 4993c2b9075SWolfgang Grandegger rx8025_sysfs_show_clock_adjust, 5003c2b9075SWolfgang Grandegger rx8025_sysfs_store_clock_adjust); 5013c2b9075SWolfgang Grandegger 5023c2b9075SWolfgang Grandegger static int rx8025_sysfs_register(struct device *dev) 5033c2b9075SWolfgang Grandegger { 5043c2b9075SWolfgang Grandegger return device_create_file(dev, &dev_attr_clock_adjust_ppb); 5053c2b9075SWolfgang Grandegger } 5063c2b9075SWolfgang Grandegger 5073c2b9075SWolfgang Grandegger static void rx8025_sysfs_unregister(struct device *dev) 5083c2b9075SWolfgang Grandegger { 5093c2b9075SWolfgang Grandegger device_remove_file(dev, &dev_attr_clock_adjust_ppb); 5103c2b9075SWolfgang Grandegger } 5113c2b9075SWolfgang Grandegger 5125a167f45SGreg Kroah-Hartman static int rx8025_probe(struct i2c_client *client, 5133c2b9075SWolfgang Grandegger const struct i2c_device_id *id) 5143c2b9075SWolfgang Grandegger { 5153c2b9075SWolfgang Grandegger struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); 5163c2b9075SWolfgang Grandegger struct rx8025_data *rx8025; 517dbcce7cfSAlexandre Belloni int err = 0, need_reset = 0; 5183c2b9075SWolfgang Grandegger 5193c2b9075SWolfgang Grandegger if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA 5203c2b9075SWolfgang Grandegger | I2C_FUNC_SMBUS_I2C_BLOCK)) { 5213c2b9075SWolfgang Grandegger dev_err(&adapter->dev, 5223c2b9075SWolfgang Grandegger "doesn't support required functionality\n"); 523dbcce7cfSAlexandre Belloni return -EIO; 5243c2b9075SWolfgang Grandegger } 5253c2b9075SWolfgang Grandegger 526fac42b41SJingoo Han rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL); 5273c2b9075SWolfgang Grandegger if (!rx8025) { 528dbcce7cfSAlexandre Belloni return -ENOMEM; 5293c2b9075SWolfgang Grandegger } 5303c2b9075SWolfgang Grandegger 5313c2b9075SWolfgang Grandegger rx8025->client = client; 5323c2b9075SWolfgang Grandegger i2c_set_clientdata(client, rx8025); 5333c2b9075SWolfgang Grandegger 5343c2b9075SWolfgang Grandegger err = rx8025_init_client(client, &need_reset); 5353c2b9075SWolfgang Grandegger if (err) 536dbcce7cfSAlexandre Belloni return err; 5373c2b9075SWolfgang Grandegger 5383c2b9075SWolfgang Grandegger if (need_reset) { 5393c2b9075SWolfgang Grandegger struct rtc_time tm; 5403c2b9075SWolfgang Grandegger dev_info(&client->dev, 5413c2b9075SWolfgang Grandegger "bad conditions detected, resetting date\n"); 5423c2b9075SWolfgang Grandegger rtc_time_to_tm(0, &tm); /* 1970/1/1 */ 5433c2b9075SWolfgang Grandegger rx8025_set_time(&client->dev, &tm); 5443c2b9075SWolfgang Grandegger } 5453c2b9075SWolfgang Grandegger 546fac42b41SJingoo Han rx8025->rtc = devm_rtc_device_register(&client->dev, client->name, 5473c2b9075SWolfgang Grandegger &rx8025_rtc_ops, THIS_MODULE); 5483c2b9075SWolfgang Grandegger if (IS_ERR(rx8025->rtc)) { 5493c2b9075SWolfgang Grandegger dev_err(&client->dev, "unable to register the class device\n"); 550dbcce7cfSAlexandre Belloni return PTR_ERR(rx8025->rtc); 5513c2b9075SWolfgang Grandegger } 5523c2b9075SWolfgang Grandegger 5533c2b9075SWolfgang Grandegger if (client->irq > 0) { 5543c2b9075SWolfgang Grandegger dev_info(&client->dev, "IRQ %d supplied\n", client->irq); 555f0b63a1dSAlexandre Belloni err = devm_request_threaded_irq(&client->dev, client->irq, NULL, 556f0b63a1dSAlexandre Belloni rx8025_handle_irq, 0, "rx8025", 557f0b63a1dSAlexandre Belloni client); 5583c2b9075SWolfgang Grandegger if (err) { 5593c2b9075SWolfgang Grandegger dev_err(&client->dev, "unable to request IRQ\n"); 560dbcce7cfSAlexandre Belloni return err; 5613c2b9075SWolfgang Grandegger } 5623c2b9075SWolfgang Grandegger } 5633c2b9075SWolfgang Grandegger 5643c2b9075SWolfgang Grandegger rx8025->rtc->irq_freq = 1; 5653c2b9075SWolfgang Grandegger rx8025->rtc->max_user_freq = 1; 5663c2b9075SWolfgang Grandegger 5673c2b9075SWolfgang Grandegger err = rx8025_sysfs_register(&client->dev); 5683c2b9075SWolfgang Grandegger return err; 5693c2b9075SWolfgang Grandegger } 5703c2b9075SWolfgang Grandegger 5715a167f45SGreg Kroah-Hartman static int rx8025_remove(struct i2c_client *client) 5723c2b9075SWolfgang Grandegger { 5733c2b9075SWolfgang Grandegger rx8025_sysfs_unregister(&client->dev); 5743c2b9075SWolfgang Grandegger return 0; 5753c2b9075SWolfgang Grandegger } 5763c2b9075SWolfgang Grandegger 5773c2b9075SWolfgang Grandegger static struct i2c_driver rx8025_driver = { 5783c2b9075SWolfgang Grandegger .driver = { 5793c2b9075SWolfgang Grandegger .name = "rtc-rx8025", 5803c2b9075SWolfgang Grandegger }, 5813c2b9075SWolfgang Grandegger .probe = rx8025_probe, 5825a167f45SGreg Kroah-Hartman .remove = rx8025_remove, 5833c2b9075SWolfgang Grandegger .id_table = rx8025_id, 5843c2b9075SWolfgang Grandegger }; 5853c2b9075SWolfgang Grandegger 5860abc9201SAxel Lin module_i2c_driver(rx8025_driver); 5873c2b9075SWolfgang Grandegger 5883c2b9075SWolfgang Grandegger MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>"); 5893c2b9075SWolfgang Grandegger MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver"); 5903c2b9075SWolfgang Grandegger MODULE_LICENSE("GPL"); 591