1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 23c2b9075SWolfgang Grandegger /* 33c2b9075SWolfgang Grandegger * Driver for Epson's RTC module RX-8025 SA/NB 43c2b9075SWolfgang Grandegger * 53c2b9075SWolfgang Grandegger * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com> 63c2b9075SWolfgang Grandegger * 73c2b9075SWolfgang Grandegger * Copyright (C) 2005 by Digi International Inc. 83c2b9075SWolfgang Grandegger * All rights reserved. 93c2b9075SWolfgang Grandegger * 103c2b9075SWolfgang Grandegger * Modified by fengjh at rising.com.cn 11d00cd819SLeslie Lau * <lm-sensors@lm-sensors.org> 123c2b9075SWolfgang Grandegger * 2006.11 133c2b9075SWolfgang Grandegger * 143c2b9075SWolfgang Grandegger * Code cleanup by Sergei Poselenov, <sposelenov@emcraft.com> 153c2b9075SWolfgang Grandegger * Converted to new style by Wolfgang Grandegger <wg@grandegger.com> 163c2b9075SWolfgang Grandegger * Alarm and periodic interrupt added by Dmitry Rakhchev <rda@emcraft.com> 173c2b9075SWolfgang Grandegger */ 183c2b9075SWolfgang Grandegger #include <linux/bcd.h> 192ddd1869SAlexandre Belloni #include <linux/bitops.h> 203c2b9075SWolfgang Grandegger #include <linux/i2c.h> 2115d3bdc2SAlexandre Belloni #include <linux/kernel.h> 2215d3bdc2SAlexandre Belloni #include <linux/module.h> 233c2b9075SWolfgang Grandegger #include <linux/rtc.h> 243c2b9075SWolfgang Grandegger 253c2b9075SWolfgang Grandegger /* Register definitions */ 263c2b9075SWolfgang Grandegger #define RX8025_REG_SEC 0x00 273c2b9075SWolfgang Grandegger #define RX8025_REG_MIN 0x01 283c2b9075SWolfgang Grandegger #define RX8025_REG_HOUR 0x02 293c2b9075SWolfgang Grandegger #define RX8025_REG_WDAY 0x03 303c2b9075SWolfgang Grandegger #define RX8025_REG_MDAY 0x04 313c2b9075SWolfgang Grandegger #define RX8025_REG_MONTH 0x05 323c2b9075SWolfgang Grandegger #define RX8025_REG_YEAR 0x06 333c2b9075SWolfgang Grandegger #define RX8025_REG_DIGOFF 0x07 343c2b9075SWolfgang Grandegger #define RX8025_REG_ALWMIN 0x08 353c2b9075SWolfgang Grandegger #define RX8025_REG_ALWHOUR 0x09 363c2b9075SWolfgang Grandegger #define RX8025_REG_ALWWDAY 0x0a 373c2b9075SWolfgang Grandegger #define RX8025_REG_ALDMIN 0x0b 383c2b9075SWolfgang Grandegger #define RX8025_REG_ALDHOUR 0x0c 393c2b9075SWolfgang Grandegger /* 0x0d is reserved */ 403c2b9075SWolfgang Grandegger #define RX8025_REG_CTRL1 0x0e 413c2b9075SWolfgang Grandegger #define RX8025_REG_CTRL2 0x0f 423c2b9075SWolfgang Grandegger 433c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL1_CT (7 << 0) 443c2b9075SWolfgang Grandegger /* 1 Hz periodic level irq */ 453c2b9075SWolfgang Grandegger #define RX8025_BIT_CTRL1_CT_1HZ 4 462ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL1_TEST BIT(3) 472ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL1_1224 BIT(5) 482ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL1_DALE BIT(6) 492ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL1_WALE BIT(7) 503c2b9075SWolfgang Grandegger 512ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_DAFG BIT(0) 522ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_WAFG BIT(1) 532ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_CTFG BIT(2) 542ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_PON BIT(4) 552ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_XST BIT(5) 562ddd1869SAlexandre Belloni #define RX8025_BIT_CTRL2_VDET BIT(6) 573c2b9075SWolfgang Grandegger 583c2b9075SWolfgang Grandegger /* Clock precision adjustment */ 593c2b9075SWolfgang Grandegger #define RX8025_ADJ_RESOLUTION 3050 /* in ppb */ 603c2b9075SWolfgang Grandegger #define RX8025_ADJ_DATA_MAX 62 613c2b9075SWolfgang Grandegger #define RX8025_ADJ_DATA_MIN -62 623c2b9075SWolfgang Grandegger 633c2b9075SWolfgang Grandegger static const struct i2c_device_id rx8025_id[] = { 643c2b9075SWolfgang Grandegger { "rx8025", 0 }, 653c2b9075SWolfgang Grandegger { } 663c2b9075SWolfgang Grandegger }; 673c2b9075SWolfgang Grandegger MODULE_DEVICE_TABLE(i2c, rx8025_id); 683c2b9075SWolfgang Grandegger 693c2b9075SWolfgang Grandegger struct rx8025_data { 703c2b9075SWolfgang Grandegger struct i2c_client *client; 713c2b9075SWolfgang Grandegger struct rtc_device *rtc; 723c2b9075SWolfgang Grandegger u8 ctrl1; 733c2b9075SWolfgang Grandegger }; 743c2b9075SWolfgang Grandegger 75fd9061fbSAlexandre Belloni static s32 rx8025_read_reg(const struct i2c_client *client, u8 number) 763c2b9075SWolfgang Grandegger { 77fd9061fbSAlexandre Belloni return i2c_smbus_read_byte_data(client, number << 4); 783c2b9075SWolfgang Grandegger } 793c2b9075SWolfgang Grandegger 80fd9061fbSAlexandre Belloni static int rx8025_read_regs(const struct i2c_client *client, 81fd9061fbSAlexandre Belloni u8 number, u8 length, u8 *values) 823c2b9075SWolfgang Grandegger { 83fd9061fbSAlexandre Belloni int ret = i2c_smbus_read_i2c_block_data(client, number << 4, length, 84fd9061fbSAlexandre Belloni values); 85fd9061fbSAlexandre Belloni if (ret != length) 863c2b9075SWolfgang Grandegger return ret < 0 ? ret : -EIO; 873c2b9075SWolfgang Grandegger 883c2b9075SWolfgang Grandegger return 0; 893c2b9075SWolfgang Grandegger } 903c2b9075SWolfgang Grandegger 91fd9061fbSAlexandre Belloni static s32 rx8025_write_reg(const struct i2c_client *client, u8 number, 92fd9061fbSAlexandre Belloni u8 value) 933c2b9075SWolfgang Grandegger { 94fd9061fbSAlexandre Belloni return i2c_smbus_write_byte_data(client, number << 4, value); 953c2b9075SWolfgang Grandegger } 963c2b9075SWolfgang Grandegger 97fd9061fbSAlexandre Belloni static s32 rx8025_write_regs(const struct i2c_client *client, 98fd9061fbSAlexandre Belloni u8 number, u8 length, const u8 *values) 993c2b9075SWolfgang Grandegger { 100fd9061fbSAlexandre Belloni return i2c_smbus_write_i2c_block_data(client, number << 4, 1013c2b9075SWolfgang Grandegger length, values); 1023c2b9075SWolfgang Grandegger } 1033c2b9075SWolfgang Grandegger 104efbbb4fdSAlexandre Belloni static int rx8025_check_validity(struct device *dev) 105efbbb4fdSAlexandre Belloni { 106efbbb4fdSAlexandre Belloni struct rx8025_data *rx8025 = dev_get_drvdata(dev); 107efbbb4fdSAlexandre Belloni int ctrl2; 108efbbb4fdSAlexandre Belloni 109efbbb4fdSAlexandre Belloni ctrl2 = rx8025_read_reg(rx8025->client, RX8025_REG_CTRL2); 110efbbb4fdSAlexandre Belloni if (ctrl2 < 0) 111efbbb4fdSAlexandre Belloni return ctrl2; 112efbbb4fdSAlexandre Belloni 113efbbb4fdSAlexandre Belloni if (ctrl2 & RX8025_BIT_CTRL2_VDET) 114efbbb4fdSAlexandre Belloni dev_warn(dev, "power voltage drop detected\n"); 115efbbb4fdSAlexandre Belloni 116efbbb4fdSAlexandre Belloni if (ctrl2 & RX8025_BIT_CTRL2_PON) { 117efbbb4fdSAlexandre Belloni dev_warn(dev, "power-on reset detected, date is invalid\n"); 118efbbb4fdSAlexandre Belloni return -EINVAL; 119efbbb4fdSAlexandre Belloni } 120efbbb4fdSAlexandre Belloni 121efbbb4fdSAlexandre Belloni if (!(ctrl2 & RX8025_BIT_CTRL2_XST)) { 122efbbb4fdSAlexandre Belloni dev_warn(dev, "crystal stopped, date is invalid\n"); 123efbbb4fdSAlexandre Belloni return -EINVAL; 124efbbb4fdSAlexandre Belloni } 125efbbb4fdSAlexandre Belloni 126efbbb4fdSAlexandre Belloni return 0; 127efbbb4fdSAlexandre Belloni } 128efbbb4fdSAlexandre Belloni 1298c4a4467SAlexandre Belloni static int rx8025_reset_validity(struct i2c_client *client) 1308c4a4467SAlexandre Belloni { 1318c4a4467SAlexandre Belloni int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2); 1328c4a4467SAlexandre Belloni 1338c4a4467SAlexandre Belloni if (ctrl2 < 0) 1348c4a4467SAlexandre Belloni return ctrl2; 1358c4a4467SAlexandre Belloni 1368c4a4467SAlexandre Belloni ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET); 1378c4a4467SAlexandre Belloni 1388c4a4467SAlexandre Belloni return rx8025_write_reg(client, RX8025_REG_CTRL2, 1398c4a4467SAlexandre Belloni ctrl2 | RX8025_BIT_CTRL2_XST); 1408c4a4467SAlexandre Belloni } 1418c4a4467SAlexandre Belloni 142b6a57c95SAlexandre Belloni static irqreturn_t rx8025_handle_irq(int irq, void *dev_id) 1433c2b9075SWolfgang Grandegger { 1443c2b9075SWolfgang Grandegger struct i2c_client *client = dev_id; 1453c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = i2c_get_clientdata(client); 1469dbe3852SAkinobu Mita struct mutex *lock = &rx8025->rtc->ops_lock; 147fd9061fbSAlexandre Belloni int status; 1483c2b9075SWolfgang Grandegger 1499dbe3852SAkinobu Mita mutex_lock(lock); 150fd9061fbSAlexandre Belloni status = rx8025_read_reg(client, RX8025_REG_CTRL2); 151fd9061fbSAlexandre Belloni if (status < 0) 1523c2b9075SWolfgang Grandegger goto out; 1533c2b9075SWolfgang Grandegger 1543c2b9075SWolfgang Grandegger if (!(status & RX8025_BIT_CTRL2_XST)) 1553c2b9075SWolfgang Grandegger dev_warn(&client->dev, "Oscillation stop was detected," 1563c2b9075SWolfgang Grandegger "you may have to readjust the clock\n"); 1573c2b9075SWolfgang Grandegger 1583c2b9075SWolfgang Grandegger if (status & RX8025_BIT_CTRL2_CTFG) { 1593c2b9075SWolfgang Grandegger /* periodic */ 1603c2b9075SWolfgang Grandegger status &= ~RX8025_BIT_CTRL2_CTFG; 1613c2b9075SWolfgang Grandegger rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF); 1623c2b9075SWolfgang Grandegger } 1633c2b9075SWolfgang Grandegger 1643c2b9075SWolfgang Grandegger if (status & RX8025_BIT_CTRL2_DAFG) { 1653c2b9075SWolfgang Grandegger /* alarm */ 1663c2b9075SWolfgang Grandegger status &= RX8025_BIT_CTRL2_DAFG; 1673c2b9075SWolfgang Grandegger if (rx8025_write_reg(client, RX8025_REG_CTRL1, 1683c2b9075SWolfgang Grandegger rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE)) 1693c2b9075SWolfgang Grandegger goto out; 1703c2b9075SWolfgang Grandegger rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF); 1713c2b9075SWolfgang Grandegger } 1723c2b9075SWolfgang Grandegger 1733c2b9075SWolfgang Grandegger out: 1749dbe3852SAkinobu Mita mutex_unlock(lock); 1759dbe3852SAkinobu Mita 176b6a57c95SAlexandre Belloni return IRQ_HANDLED; 1773c2b9075SWolfgang Grandegger } 1783c2b9075SWolfgang Grandegger 1793c2b9075SWolfgang Grandegger static int rx8025_get_time(struct device *dev, struct rtc_time *dt) 1803c2b9075SWolfgang Grandegger { 1813c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev); 182fd9061fbSAlexandre Belloni u8 date[7]; 183efbbb4fdSAlexandre Belloni int err; 1843c2b9075SWolfgang Grandegger 185efbbb4fdSAlexandre Belloni err = rx8025_check_validity(dev); 186efbbb4fdSAlexandre Belloni if (err) 187efbbb4fdSAlexandre Belloni return err; 1886f0a8cfeSAlexandre Belloni 1893c2b9075SWolfgang Grandegger err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date); 1903c2b9075SWolfgang Grandegger if (err) 1913c2b9075SWolfgang Grandegger return err; 1923c2b9075SWolfgang Grandegger 1931921cab1SAndy Shevchenko dev_dbg(dev, "%s: read %7ph\n", __func__, date); 1943c2b9075SWolfgang Grandegger 1953c2b9075SWolfgang Grandegger dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f); 1963c2b9075SWolfgang Grandegger dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f); 1973c2b9075SWolfgang Grandegger if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) 1983c2b9075SWolfgang Grandegger dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f); 1993c2b9075SWolfgang Grandegger else 2003c2b9075SWolfgang Grandegger dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12 2013c2b9075SWolfgang Grandegger + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0); 2023c2b9075SWolfgang Grandegger 2033c2b9075SWolfgang Grandegger dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f); 2043c2b9075SWolfgang Grandegger dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1; 20532672c55SAlexandre Belloni dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100; 2063c2b9075SWolfgang Grandegger 2071921cab1SAndy Shevchenko dev_dbg(dev, "%s: date %ptRr\n", __func__, dt); 2083c2b9075SWolfgang Grandegger 20922652ba7SAlexandre Belloni return 0; 2103c2b9075SWolfgang Grandegger } 2113c2b9075SWolfgang Grandegger 2123c2b9075SWolfgang Grandegger static int rx8025_set_time(struct device *dev, struct rtc_time *dt) 2133c2b9075SWolfgang Grandegger { 2143c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev); 2153c2b9075SWolfgang Grandegger u8 date[7]; 2168c4a4467SAlexandre Belloni int ret; 2173c2b9075SWolfgang Grandegger 21832672c55SAlexandre Belloni if ((dt->tm_year < 100) || (dt->tm_year > 199)) 21932672c55SAlexandre Belloni return -EINVAL; 2203c2b9075SWolfgang Grandegger 2213c2b9075SWolfgang Grandegger /* 2223c2b9075SWolfgang Grandegger * Here the read-only bits are written as "0". I'm not sure if that 2233c2b9075SWolfgang Grandegger * is sound. 2243c2b9075SWolfgang Grandegger */ 2253c2b9075SWolfgang Grandegger date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec); 2263c2b9075SWolfgang Grandegger date[RX8025_REG_MIN] = bin2bcd(dt->tm_min); 2273c2b9075SWolfgang Grandegger if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) 2283c2b9075SWolfgang Grandegger date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour); 2293c2b9075SWolfgang Grandegger else 2303c2b9075SWolfgang Grandegger date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0) 2313c2b9075SWolfgang Grandegger | bin2bcd((dt->tm_hour + 11) % 12 + 1); 2323c2b9075SWolfgang Grandegger 2333c2b9075SWolfgang Grandegger date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday); 2343c2b9075SWolfgang Grandegger date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday); 2353c2b9075SWolfgang Grandegger date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1); 23632672c55SAlexandre Belloni date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100); 2373c2b9075SWolfgang Grandegger 2381921cab1SAndy Shevchenko dev_dbg(dev, "%s: write %7ph\n", __func__, date); 2393c2b9075SWolfgang Grandegger 2408c4a4467SAlexandre Belloni ret = rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date); 2418c4a4467SAlexandre Belloni if (ret < 0) 2428c4a4467SAlexandre Belloni return ret; 2438c4a4467SAlexandre Belloni 2448c4a4467SAlexandre Belloni return rx8025_reset_validity(rx8025->client); 2453c2b9075SWolfgang Grandegger } 2463c2b9075SWolfgang Grandegger 2476f0a8cfeSAlexandre Belloni static int rx8025_init_client(struct i2c_client *client) 2483c2b9075SWolfgang Grandegger { 2493c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = i2c_get_clientdata(client); 2503c2b9075SWolfgang Grandegger u8 ctrl[2], ctrl2; 2513c2b9075SWolfgang Grandegger int need_clear = 0; 2523c2b9075SWolfgang Grandegger int err; 2533c2b9075SWolfgang Grandegger 2543c2b9075SWolfgang Grandegger err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl); 2553c2b9075SWolfgang Grandegger if (err) 2563c2b9075SWolfgang Grandegger goto out; 2573c2b9075SWolfgang Grandegger 2583c2b9075SWolfgang Grandegger /* Keep test bit zero ! */ 2593c2b9075SWolfgang Grandegger rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST; 2603c2b9075SWolfgang Grandegger 2613c2b9075SWolfgang Grandegger if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) { 2623c2b9075SWolfgang Grandegger dev_warn(&client->dev, "Alarm was detected\n"); 2633c2b9075SWolfgang Grandegger need_clear = 1; 2643c2b9075SWolfgang Grandegger } 2653c2b9075SWolfgang Grandegger 2665c66e1e0SAlexandre Belloni if (ctrl[1] & RX8025_BIT_CTRL2_CTFG) 2673c2b9075SWolfgang Grandegger need_clear = 1; 2683c2b9075SWolfgang Grandegger 2696f0a8cfeSAlexandre Belloni if (need_clear) { 270a27c7bf6SAlexandre Belloni ctrl2 = ctrl[1]; 2718c4a4467SAlexandre Belloni ctrl2 &= ~(RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG | 2723c2b9075SWolfgang Grandegger RX8025_BIT_CTRL2_DAFG); 2733c2b9075SWolfgang Grandegger 2743c2b9075SWolfgang Grandegger err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2); 2753c2b9075SWolfgang Grandegger } 2763c2b9075SWolfgang Grandegger out: 2773c2b9075SWolfgang Grandegger return err; 2783c2b9075SWolfgang Grandegger } 2793c2b9075SWolfgang Grandegger 2803c2b9075SWolfgang Grandegger /* Alarm support */ 2813c2b9075SWolfgang Grandegger static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t) 2823c2b9075SWolfgang Grandegger { 2833c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev); 2843c2b9075SWolfgang Grandegger struct i2c_client *client = rx8025->client; 285fd9061fbSAlexandre Belloni u8 ald[2]; 286fd9061fbSAlexandre Belloni int ctrl2, err; 2873c2b9075SWolfgang Grandegger 2883c2b9075SWolfgang Grandegger if (client->irq <= 0) 2893c2b9075SWolfgang Grandegger return -EINVAL; 2903c2b9075SWolfgang Grandegger 2913c2b9075SWolfgang Grandegger err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald); 2923c2b9075SWolfgang Grandegger if (err) 2933c2b9075SWolfgang Grandegger return err; 2943c2b9075SWolfgang Grandegger 295fd9061fbSAlexandre Belloni ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2); 296fd9061fbSAlexandre Belloni if (ctrl2 < 0) 297fd9061fbSAlexandre Belloni return ctrl2; 2983c2b9075SWolfgang Grandegger 2993c2b9075SWolfgang Grandegger dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n", 3003c2b9075SWolfgang Grandegger __func__, ald[0], ald[1], ctrl2); 3013c2b9075SWolfgang Grandegger 3023c2b9075SWolfgang Grandegger /* Hardware alarms precision is 1 minute! */ 3033c2b9075SWolfgang Grandegger t->time.tm_sec = 0; 3043c2b9075SWolfgang Grandegger t->time.tm_min = bcd2bin(ald[0] & 0x7f); 3053c2b9075SWolfgang Grandegger if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) 3063c2b9075SWolfgang Grandegger t->time.tm_hour = bcd2bin(ald[1] & 0x3f); 3073c2b9075SWolfgang Grandegger else 3083c2b9075SWolfgang Grandegger t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12 3093c2b9075SWolfgang Grandegger + (ald[1] & 0x20 ? 12 : 0); 3103c2b9075SWolfgang Grandegger 311862cac15SAndy Shevchenko dev_dbg(dev, "%s: date: %ptRr\n", __func__, &t->time); 3123c2b9075SWolfgang Grandegger t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE); 3133c2b9075SWolfgang Grandegger t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled; 3143c2b9075SWolfgang Grandegger 3153c2b9075SWolfgang Grandegger return err; 3163c2b9075SWolfgang Grandegger } 3173c2b9075SWolfgang Grandegger 3183c2b9075SWolfgang Grandegger static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t) 3193c2b9075SWolfgang Grandegger { 3203c2b9075SWolfgang Grandegger struct i2c_client *client = to_i2c_client(dev); 3213c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev); 3223c2b9075SWolfgang Grandegger u8 ald[2]; 3233c2b9075SWolfgang Grandegger int err; 3243c2b9075SWolfgang Grandegger 3253c2b9075SWolfgang Grandegger if (client->irq <= 0) 3263c2b9075SWolfgang Grandegger return -EINVAL; 3273c2b9075SWolfgang Grandegger 328302c5608SAkinobu Mita /* 329302c5608SAkinobu Mita * Hardware alarm precision is 1 minute! 330302c5608SAkinobu Mita * round up to nearest minute 331302c5608SAkinobu Mita */ 332302c5608SAkinobu Mita if (t->time.tm_sec) { 333302c5608SAkinobu Mita time64_t alarm_time = rtc_tm_to_time64(&t->time); 334302c5608SAkinobu Mita 335302c5608SAkinobu Mita alarm_time += 60 - t->time.tm_sec; 336302c5608SAkinobu Mita rtc_time64_to_tm(alarm_time, &t->time); 337302c5608SAkinobu Mita } 338302c5608SAkinobu Mita 3393c2b9075SWolfgang Grandegger ald[0] = bin2bcd(t->time.tm_min); 3403c2b9075SWolfgang Grandegger if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224) 3413c2b9075SWolfgang Grandegger ald[1] = bin2bcd(t->time.tm_hour); 3423c2b9075SWolfgang Grandegger else 3433c2b9075SWolfgang Grandegger ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0) 3443c2b9075SWolfgang Grandegger | bin2bcd((t->time.tm_hour + 11) % 12 + 1); 3453c2b9075SWolfgang Grandegger 3463c2b9075SWolfgang Grandegger dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]); 3473c2b9075SWolfgang Grandegger 3483c2b9075SWolfgang Grandegger if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) { 3493c2b9075SWolfgang Grandegger rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE; 3503c2b9075SWolfgang Grandegger err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, 3513c2b9075SWolfgang Grandegger rx8025->ctrl1); 3523c2b9075SWolfgang Grandegger if (err) 3533c2b9075SWolfgang Grandegger return err; 3543c2b9075SWolfgang Grandegger } 3553c2b9075SWolfgang Grandegger err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald); 3563c2b9075SWolfgang Grandegger if (err) 3573c2b9075SWolfgang Grandegger return err; 3583c2b9075SWolfgang Grandegger 3593c2b9075SWolfgang Grandegger if (t->enabled) { 3603c2b9075SWolfgang Grandegger rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE; 3613c2b9075SWolfgang Grandegger err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, 3623c2b9075SWolfgang Grandegger rx8025->ctrl1); 3633c2b9075SWolfgang Grandegger if (err) 3643c2b9075SWolfgang Grandegger return err; 3653c2b9075SWolfgang Grandegger } 3663c2b9075SWolfgang Grandegger 3673c2b9075SWolfgang Grandegger return 0; 3683c2b9075SWolfgang Grandegger } 3693c2b9075SWolfgang Grandegger 3703c2b9075SWolfgang Grandegger static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled) 3713c2b9075SWolfgang Grandegger { 3723c2b9075SWolfgang Grandegger struct rx8025_data *rx8025 = dev_get_drvdata(dev); 3733c2b9075SWolfgang Grandegger u8 ctrl1; 3743c2b9075SWolfgang Grandegger int err; 3753c2b9075SWolfgang Grandegger 3763c2b9075SWolfgang Grandegger ctrl1 = rx8025->ctrl1; 3773c2b9075SWolfgang Grandegger if (enabled) 3783c2b9075SWolfgang Grandegger ctrl1 |= RX8025_BIT_CTRL1_DALE; 3793c2b9075SWolfgang Grandegger else 3803c2b9075SWolfgang Grandegger ctrl1 &= ~RX8025_BIT_CTRL1_DALE; 3813c2b9075SWolfgang Grandegger 3823c2b9075SWolfgang Grandegger if (ctrl1 != rx8025->ctrl1) { 3833c2b9075SWolfgang Grandegger rx8025->ctrl1 = ctrl1; 3843c2b9075SWolfgang Grandegger err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, 3853c2b9075SWolfgang Grandegger rx8025->ctrl1); 3863c2b9075SWolfgang Grandegger if (err) 3873c2b9075SWolfgang Grandegger return err; 3883c2b9075SWolfgang Grandegger } 3893c2b9075SWolfgang Grandegger return 0; 3903c2b9075SWolfgang Grandegger } 3913c2b9075SWolfgang Grandegger 39234c7b3acSJulia Lawall static const struct rtc_class_ops rx8025_rtc_ops = { 3933c2b9075SWolfgang Grandegger .read_time = rx8025_get_time, 3943c2b9075SWolfgang Grandegger .set_time = rx8025_set_time, 3953c2b9075SWolfgang Grandegger .read_alarm = rx8025_read_alarm, 3963c2b9075SWolfgang Grandegger .set_alarm = rx8025_set_alarm, 3973c2b9075SWolfgang Grandegger .alarm_irq_enable = rx8025_alarm_irq_enable, 3983c2b9075SWolfgang Grandegger }; 3993c2b9075SWolfgang Grandegger 4003c2b9075SWolfgang Grandegger /* 4013c2b9075SWolfgang Grandegger * Clock precision adjustment support 4023c2b9075SWolfgang Grandegger * 4033c2b9075SWolfgang Grandegger * According to the RX8025 SA/NB application manual the frequency and 404b770ffd4SThomas Weber * temperature characteristics can be approximated using the following 4053c2b9075SWolfgang Grandegger * equation: 4063c2b9075SWolfgang Grandegger * 4073c2b9075SWolfgang Grandegger * df = a * (ut - t)**2 4083c2b9075SWolfgang Grandegger * 4093c2b9075SWolfgang Grandegger * df: Frequency deviation in any temperature 4103c2b9075SWolfgang Grandegger * a : Coefficient = (-35 +-5) * 10**-9 4113c2b9075SWolfgang Grandegger * ut: Ultimate temperature in degree = +25 +-5 degree 4123c2b9075SWolfgang Grandegger * t : Any temperature in degree 4133c2b9075SWolfgang Grandegger * 4143c2b9075SWolfgang Grandegger * Note that the clock adjustment in ppb must be entered (which is 4153c2b9075SWolfgang Grandegger * the negative value of the deviation). 4163c2b9075SWolfgang Grandegger */ 4173c2b9075SWolfgang Grandegger static int rx8025_get_clock_adjust(struct device *dev, int *adj) 4183c2b9075SWolfgang Grandegger { 4193c2b9075SWolfgang Grandegger struct i2c_client *client = to_i2c_client(dev); 420fd9061fbSAlexandre Belloni int digoff; 4213c2b9075SWolfgang Grandegger 422fd9061fbSAlexandre Belloni digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF); 423fd9061fbSAlexandre Belloni if (digoff < 0) 424fd9061fbSAlexandre Belloni return digoff; 4253c2b9075SWolfgang Grandegger 4263c2b9075SWolfgang Grandegger *adj = digoff >= 64 ? digoff - 128 : digoff; 4273c2b9075SWolfgang Grandegger if (*adj > 0) 4283c2b9075SWolfgang Grandegger (*adj)--; 4293c2b9075SWolfgang Grandegger *adj *= -RX8025_ADJ_RESOLUTION; 4303c2b9075SWolfgang Grandegger 4313c2b9075SWolfgang Grandegger return 0; 4323c2b9075SWolfgang Grandegger } 4333c2b9075SWolfgang Grandegger 4343c2b9075SWolfgang Grandegger static int rx8025_set_clock_adjust(struct device *dev, int adj) 4353c2b9075SWolfgang Grandegger { 4363c2b9075SWolfgang Grandegger struct i2c_client *client = to_i2c_client(dev); 4373c2b9075SWolfgang Grandegger u8 digoff; 4383c2b9075SWolfgang Grandegger int err; 4393c2b9075SWolfgang Grandegger 4403c2b9075SWolfgang Grandegger adj /= -RX8025_ADJ_RESOLUTION; 4413c2b9075SWolfgang Grandegger if (adj > RX8025_ADJ_DATA_MAX) 4423c2b9075SWolfgang Grandegger adj = RX8025_ADJ_DATA_MAX; 4433c2b9075SWolfgang Grandegger else if (adj < RX8025_ADJ_DATA_MIN) 4443c2b9075SWolfgang Grandegger adj = RX8025_ADJ_DATA_MIN; 4453c2b9075SWolfgang Grandegger else if (adj > 0) 4463c2b9075SWolfgang Grandegger adj++; 4473c2b9075SWolfgang Grandegger else if (adj < 0) 4483c2b9075SWolfgang Grandegger adj += 128; 4493c2b9075SWolfgang Grandegger digoff = adj; 4503c2b9075SWolfgang Grandegger 4513c2b9075SWolfgang Grandegger err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff); 4523c2b9075SWolfgang Grandegger if (err) 4533c2b9075SWolfgang Grandegger return err; 4543c2b9075SWolfgang Grandegger 4553c2b9075SWolfgang Grandegger dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff); 4563c2b9075SWolfgang Grandegger 4573c2b9075SWolfgang Grandegger return 0; 4583c2b9075SWolfgang Grandegger } 4593c2b9075SWolfgang Grandegger 4603c2b9075SWolfgang Grandegger static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev, 4613c2b9075SWolfgang Grandegger struct device_attribute *attr, 4623c2b9075SWolfgang Grandegger char *buf) 4633c2b9075SWolfgang Grandegger { 4643c2b9075SWolfgang Grandegger int err, adj; 4653c2b9075SWolfgang Grandegger 4663c2b9075SWolfgang Grandegger err = rx8025_get_clock_adjust(dev, &adj); 4673c2b9075SWolfgang Grandegger if (err) 4683c2b9075SWolfgang Grandegger return err; 4693c2b9075SWolfgang Grandegger 4703c2b9075SWolfgang Grandegger return sprintf(buf, "%d\n", adj); 4713c2b9075SWolfgang Grandegger } 4723c2b9075SWolfgang Grandegger 4733c2b9075SWolfgang Grandegger static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev, 4743c2b9075SWolfgang Grandegger struct device_attribute *attr, 4753c2b9075SWolfgang Grandegger const char *buf, size_t count) 4763c2b9075SWolfgang Grandegger { 4773c2b9075SWolfgang Grandegger int adj, err; 4783c2b9075SWolfgang Grandegger 4793c2b9075SWolfgang Grandegger if (sscanf(buf, "%i", &adj) != 1) 4803c2b9075SWolfgang Grandegger return -EINVAL; 4813c2b9075SWolfgang Grandegger 4823c2b9075SWolfgang Grandegger err = rx8025_set_clock_adjust(dev, adj); 4833c2b9075SWolfgang Grandegger 4843c2b9075SWolfgang Grandegger return err ? err : count; 4853c2b9075SWolfgang Grandegger } 4863c2b9075SWolfgang Grandegger 4873c2b9075SWolfgang Grandegger static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR, 4883c2b9075SWolfgang Grandegger rx8025_sysfs_show_clock_adjust, 4893c2b9075SWolfgang Grandegger rx8025_sysfs_store_clock_adjust); 4903c2b9075SWolfgang Grandegger 4913c2b9075SWolfgang Grandegger static int rx8025_sysfs_register(struct device *dev) 4923c2b9075SWolfgang Grandegger { 4933c2b9075SWolfgang Grandegger return device_create_file(dev, &dev_attr_clock_adjust_ppb); 4943c2b9075SWolfgang Grandegger } 4953c2b9075SWolfgang Grandegger 4963c2b9075SWolfgang Grandegger static void rx8025_sysfs_unregister(struct device *dev) 4973c2b9075SWolfgang Grandegger { 4983c2b9075SWolfgang Grandegger device_remove_file(dev, &dev_attr_clock_adjust_ppb); 4993c2b9075SWolfgang Grandegger } 5003c2b9075SWolfgang Grandegger 5015a167f45SGreg Kroah-Hartman static int rx8025_probe(struct i2c_client *client, 5023c2b9075SWolfgang Grandegger const struct i2c_device_id *id) 5033c2b9075SWolfgang Grandegger { 5043c2b9075SWolfgang Grandegger struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); 5053c2b9075SWolfgang Grandegger struct rx8025_data *rx8025; 5066f0a8cfeSAlexandre Belloni int err = 0; 5073c2b9075SWolfgang Grandegger 5083c2b9075SWolfgang Grandegger if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA 5093c2b9075SWolfgang Grandegger | I2C_FUNC_SMBUS_I2C_BLOCK)) { 5103c2b9075SWolfgang Grandegger dev_err(&adapter->dev, 5113c2b9075SWolfgang Grandegger "doesn't support required functionality\n"); 512dbcce7cfSAlexandre Belloni return -EIO; 5133c2b9075SWolfgang Grandegger } 5143c2b9075SWolfgang Grandegger 515fac42b41SJingoo Han rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL); 516b1f9d790SAlexandre Belloni if (!rx8025) 517dbcce7cfSAlexandre Belloni return -ENOMEM; 5183c2b9075SWolfgang Grandegger 5193c2b9075SWolfgang Grandegger rx8025->client = client; 5203c2b9075SWolfgang Grandegger i2c_set_clientdata(client, rx8025); 5213c2b9075SWolfgang Grandegger 5226f0a8cfeSAlexandre Belloni err = rx8025_init_client(client); 5233c2b9075SWolfgang Grandegger if (err) 524dbcce7cfSAlexandre Belloni return err; 5253c2b9075SWolfgang Grandegger 526fac42b41SJingoo Han rx8025->rtc = devm_rtc_device_register(&client->dev, client->name, 5273c2b9075SWolfgang Grandegger &rx8025_rtc_ops, THIS_MODULE); 5283c2b9075SWolfgang Grandegger if (IS_ERR(rx8025->rtc)) { 5293c2b9075SWolfgang Grandegger dev_err(&client->dev, "unable to register the class device\n"); 530dbcce7cfSAlexandre Belloni return PTR_ERR(rx8025->rtc); 5313c2b9075SWolfgang Grandegger } 5323c2b9075SWolfgang Grandegger 5333c2b9075SWolfgang Grandegger if (client->irq > 0) { 5343c2b9075SWolfgang Grandegger dev_info(&client->dev, "IRQ %d supplied\n", client->irq); 535f0b63a1dSAlexandre Belloni err = devm_request_threaded_irq(&client->dev, client->irq, NULL, 5360a966c07SAkinobu Mita rx8025_handle_irq, 5370a966c07SAkinobu Mita IRQF_ONESHOT, 5380a966c07SAkinobu Mita "rx8025", client); 5393c2b9075SWolfgang Grandegger if (err) { 5408a06513dSAlexandre Belloni dev_err(&client->dev, "unable to request IRQ, alarms disabled\n"); 5418a06513dSAlexandre Belloni client->irq = 0; 5423c2b9075SWolfgang Grandegger } 5433c2b9075SWolfgang Grandegger } 5443c2b9075SWolfgang Grandegger 5453c2b9075SWolfgang Grandegger rx8025->rtc->max_user_freq = 1; 5463c2b9075SWolfgang Grandegger 547ef5f4a9eSAkinobu Mita /* the rx8025 alarm only supports a minute accuracy */ 548ef5f4a9eSAkinobu Mita rx8025->rtc->uie_unsupported = 1; 549ef5f4a9eSAkinobu Mita 5503c2b9075SWolfgang Grandegger err = rx8025_sysfs_register(&client->dev); 5513c2b9075SWolfgang Grandegger return err; 5523c2b9075SWolfgang Grandegger } 5533c2b9075SWolfgang Grandegger 5545a167f45SGreg Kroah-Hartman static int rx8025_remove(struct i2c_client *client) 5553c2b9075SWolfgang Grandegger { 5563c2b9075SWolfgang Grandegger rx8025_sysfs_unregister(&client->dev); 5573c2b9075SWolfgang Grandegger return 0; 5583c2b9075SWolfgang Grandegger } 5593c2b9075SWolfgang Grandegger 5603c2b9075SWolfgang Grandegger static struct i2c_driver rx8025_driver = { 5613c2b9075SWolfgang Grandegger .driver = { 5623c2b9075SWolfgang Grandegger .name = "rtc-rx8025", 5633c2b9075SWolfgang Grandegger }, 5643c2b9075SWolfgang Grandegger .probe = rx8025_probe, 5655a167f45SGreg Kroah-Hartman .remove = rx8025_remove, 5663c2b9075SWolfgang Grandegger .id_table = rx8025_id, 5673c2b9075SWolfgang Grandegger }; 5683c2b9075SWolfgang Grandegger 5690abc9201SAxel Lin module_i2c_driver(rx8025_driver); 5703c2b9075SWolfgang Grandegger 5713c2b9075SWolfgang Grandegger MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>"); 5723c2b9075SWolfgang Grandegger MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver"); 5733c2b9075SWolfgang Grandegger MODULE_LICENSE("GPL"); 574