10b2f6228SArnaud Ebalard /* 20b2f6228SArnaud Ebalard * rtc-ab-b5ze-s3 - Driver for Abracon AB-RTCMC-32.768Khz-B5ZE-S3 30b2f6228SArnaud Ebalard * I2C RTC / Alarm chip 40b2f6228SArnaud Ebalard * 50b2f6228SArnaud Ebalard * Copyright (C) 2014, Arnaud EBALARD <arno@natisbad.org> 60b2f6228SArnaud Ebalard * 70b2f6228SArnaud Ebalard * Detailed datasheet of the chip is available here: 80b2f6228SArnaud Ebalard * 90b2f6228SArnaud Ebalard * http://www.abracon.com/realtimeclock/AB-RTCMC-32.768kHz-B5ZE-S3-Application-Manual.pdf 100b2f6228SArnaud Ebalard * 110b2f6228SArnaud Ebalard * This work is based on ISL12057 driver (drivers/rtc/rtc-isl12057.c). 120b2f6228SArnaud Ebalard * 130b2f6228SArnaud Ebalard * This program is free software; you can redistribute it and/or modify 140b2f6228SArnaud Ebalard * it under the terms of the GNU General Public License as published by 150b2f6228SArnaud Ebalard * the Free Software Foundation; either version 2 of the License, or 160b2f6228SArnaud Ebalard * (at your option) any later version. 170b2f6228SArnaud Ebalard * 180b2f6228SArnaud Ebalard * This program is distributed in the hope that it will be useful, 190b2f6228SArnaud Ebalard * but WITHOUT ANY WARRANTY; without even the implied warranty of 200b2f6228SArnaud Ebalard * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 210b2f6228SArnaud Ebalard * GNU General Public License for more details. 220b2f6228SArnaud Ebalard */ 230b2f6228SArnaud Ebalard 240b2f6228SArnaud Ebalard #include <linux/module.h> 250b2f6228SArnaud Ebalard #include <linux/mutex.h> 260b2f6228SArnaud Ebalard #include <linux/rtc.h> 270b2f6228SArnaud Ebalard #include <linux/i2c.h> 280b2f6228SArnaud Ebalard #include <linux/bcd.h> 290b2f6228SArnaud Ebalard #include <linux/of.h> 300b2f6228SArnaud Ebalard #include <linux/regmap.h> 310b2f6228SArnaud Ebalard #include <linux/interrupt.h> 320b2f6228SArnaud Ebalard 330b2f6228SArnaud Ebalard #define DRV_NAME "rtc-ab-b5ze-s3" 340b2f6228SArnaud Ebalard 350b2f6228SArnaud Ebalard /* Control section */ 360b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1 0x00 /* Control 1 register */ 370b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1_CIE BIT(0) /* Pulse interrupt enable */ 380b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1_AIE BIT(1) /* Alarm interrupt enable */ 390b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1_SIE BIT(2) /* Second interrupt enable */ 400b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1_PM BIT(3) /* 24h/12h mode */ 410b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1_SR BIT(4) /* Software reset */ 420b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1_STOP BIT(5) /* RTC circuit enable */ 430b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL1_CAP BIT(7) 440b2f6228SArnaud Ebalard 450b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2 0x01 /* Control 2 register */ 460b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_CTBIE BIT(0) /* Countdown timer B int. enable */ 470b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_CTAIE BIT(1) /* Countdown timer A int. enable */ 480b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_WTAIE BIT(2) /* Watchdog timer A int. enable */ 490b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_AF BIT(3) /* Alarm interrupt status */ 500b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_SF BIT(4) /* Second interrupt status */ 510b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_CTBF BIT(5) /* Countdown timer B int. status */ 520b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_CTAF BIT(6) /* Countdown timer A int. status */ 530b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL2_WTAF BIT(7) /* Watchdog timer A int. status */ 540b2f6228SArnaud Ebalard 550b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3 0x02 /* Control 3 register */ 560b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3_PM2 BIT(7) /* Power Management bit 2 */ 570b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3_PM1 BIT(6) /* Power Management bit 1 */ 580b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3_PM0 BIT(5) /* Power Management bit 0 */ 590b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3_BSF BIT(3) /* Battery switchover int. status */ 600b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3_BLF BIT(2) /* Battery low int. status */ 610b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3_BSIE BIT(1) /* Battery switchover int. enable */ 620b2f6228SArnaud Ebalard #define ABB5ZES3_REG_CTRL3_BLIE BIT(0) /* Battery low int. enable */ 630b2f6228SArnaud Ebalard 640b2f6228SArnaud Ebalard #define ABB5ZES3_CTRL_SEC_LEN 3 650b2f6228SArnaud Ebalard 660b2f6228SArnaud Ebalard /* RTC section */ 670b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_SC 0x03 /* RTC Seconds register */ 680b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_SC_OSC BIT(7) /* Clock integrity status */ 690b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_MN 0x04 /* RTC Minutes register */ 700b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_HR 0x05 /* RTC Hours register */ 710b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_HR_PM BIT(5) /* RTC Hours PM bit */ 720b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_DT 0x06 /* RTC Date register */ 730b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_DW 0x07 /* RTC Day of the week register */ 740b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_MO 0x08 /* RTC Month register */ 750b2f6228SArnaud Ebalard #define ABB5ZES3_REG_RTC_YR 0x09 /* RTC Year register */ 760b2f6228SArnaud Ebalard 770b2f6228SArnaud Ebalard #define ABB5ZES3_RTC_SEC_LEN 7 780b2f6228SArnaud Ebalard 790b2f6228SArnaud Ebalard /* Alarm section (enable bits are all active low) */ 800b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_MN 0x0A /* Alarm - minute register */ 810b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_MN_AE BIT(7) /* Minute enable */ 820b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_HR 0x0B /* Alarm - hours register */ 830b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_HR_AE BIT(7) /* Hour enable */ 840b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_DT 0x0C /* Alarm - date register */ 850b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_DT_AE BIT(7) /* Date (day of the month) enable */ 860b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_DW 0x0D /* Alarm - day of the week reg. */ 870b2f6228SArnaud Ebalard #define ABB5ZES3_REG_ALRM_DW_AE BIT(7) /* Day of the week enable */ 880b2f6228SArnaud Ebalard 890b2f6228SArnaud Ebalard #define ABB5ZES3_ALRM_SEC_LEN 4 900b2f6228SArnaud Ebalard 910b2f6228SArnaud Ebalard /* Frequency offset section */ 920b2f6228SArnaud Ebalard #define ABB5ZES3_REG_FREQ_OF 0x0E /* Frequency offset register */ 930b2f6228SArnaud Ebalard #define ABB5ZES3_REG_FREQ_OF_MODE 0x0E /* Offset mode: 2 hours / minute */ 940b2f6228SArnaud Ebalard 950b2f6228SArnaud Ebalard /* CLOCKOUT section */ 960b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK 0x0F /* Timer & Clockout register */ 970b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_TAM BIT(7) /* Permanent/pulsed timer A/int. 2 */ 980b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_TBM BIT(6) /* Permanent/pulsed timer B */ 990b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_COF2 BIT(5) /* Clkout Freq bit 2 */ 1000b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_COF1 BIT(4) /* Clkout Freq bit 1 */ 1010b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_COF0 BIT(3) /* Clkout Freq bit 0 */ 1020b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_TAC1 BIT(2) /* Timer A: - 01 : countdown */ 1030b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_TAC0 BIT(1) /* - 10 : timer */ 1040b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIM_CLK_TBC BIT(0) /* Timer B enable */ 1050b2f6228SArnaud Ebalard 1060b2f6228SArnaud Ebalard /* Timer A Section */ 1070b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMA_CLK 0x10 /* Timer A clock register */ 1080b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMA_CLK_TAQ2 BIT(2) /* Freq bit 2 */ 1090b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMA_CLK_TAQ1 BIT(1) /* Freq bit 1 */ 1100b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMA_CLK_TAQ0 BIT(0) /* Freq bit 0 */ 1110b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMA 0x11 /* Timer A register */ 1120b2f6228SArnaud Ebalard 1130b2f6228SArnaud Ebalard #define ABB5ZES3_TIMA_SEC_LEN 2 1140b2f6228SArnaud Ebalard 1150b2f6228SArnaud Ebalard /* Timer B Section */ 1160b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB_CLK 0x12 /* Timer B clock register */ 1170b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB_CLK_TBW2 BIT(6) 1180b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB_CLK_TBW1 BIT(5) 1190b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB_CLK_TBW0 BIT(4) 1200b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB_CLK_TAQ2 BIT(2) 1210b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB_CLK_TAQ1 BIT(1) 1220b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB_CLK_TAQ0 BIT(0) 1230b2f6228SArnaud Ebalard #define ABB5ZES3_REG_TIMB 0x13 /* Timer B register */ 1240b2f6228SArnaud Ebalard #define ABB5ZES3_TIMB_SEC_LEN 2 1250b2f6228SArnaud Ebalard 1260b2f6228SArnaud Ebalard #define ABB5ZES3_MEM_MAP_LEN 0x14 1270b2f6228SArnaud Ebalard 1280b2f6228SArnaud Ebalard struct abb5zes3_rtc_data { 1290b2f6228SArnaud Ebalard struct rtc_device *rtc; 1300b2f6228SArnaud Ebalard struct regmap *regmap; 1310b2f6228SArnaud Ebalard struct mutex lock; 1320b2f6228SArnaud Ebalard 1330b2f6228SArnaud Ebalard int irq; 1340b2f6228SArnaud Ebalard 1350b2f6228SArnaud Ebalard bool battery_low; 136c8a1d8a5SArnaud Ebalard bool timer_alarm; /* current alarm is via timer A */ 1370b2f6228SArnaud Ebalard }; 1380b2f6228SArnaud Ebalard 1390b2f6228SArnaud Ebalard /* 1400b2f6228SArnaud Ebalard * Try and match register bits w/ fixed null values to see whether we 1410b2f6228SArnaud Ebalard * are dealing with an ABB5ZES3. Note: this function is called early 1420b2f6228SArnaud Ebalard * during init and hence does need mutex protection. 1430b2f6228SArnaud Ebalard */ 1440b2f6228SArnaud Ebalard static int abb5zes3_i2c_validate_chip(struct regmap *regmap) 1450b2f6228SArnaud Ebalard { 1460b2f6228SArnaud Ebalard u8 regs[ABB5ZES3_MEM_MAP_LEN]; 1470b2f6228SArnaud Ebalard static const u8 mask[ABB5ZES3_MEM_MAP_LEN] = { 0x00, 0x00, 0x10, 0x00, 1480b2f6228SArnaud Ebalard 0x80, 0xc0, 0xc0, 0xf8, 1490b2f6228SArnaud Ebalard 0xe0, 0x00, 0x00, 0x40, 1500b2f6228SArnaud Ebalard 0x40, 0x78, 0x00, 0x00, 1510b2f6228SArnaud Ebalard 0xf8, 0x00, 0x88, 0x00 }; 1520b2f6228SArnaud Ebalard int ret, i; 1530b2f6228SArnaud Ebalard 1540b2f6228SArnaud Ebalard ret = regmap_bulk_read(regmap, 0, regs, ABB5ZES3_MEM_MAP_LEN); 1550b2f6228SArnaud Ebalard if (ret) 1560b2f6228SArnaud Ebalard return ret; 1570b2f6228SArnaud Ebalard 1580b2f6228SArnaud Ebalard for (i = 0; i < ABB5ZES3_MEM_MAP_LEN; ++i) { 1590b2f6228SArnaud Ebalard if (regs[i] & mask[i]) /* check if bits are cleared */ 1600b2f6228SArnaud Ebalard return -ENODEV; 1610b2f6228SArnaud Ebalard } 1620b2f6228SArnaud Ebalard 1630b2f6228SArnaud Ebalard return 0; 1640b2f6228SArnaud Ebalard } 1650b2f6228SArnaud Ebalard 1660b2f6228SArnaud Ebalard /* Clear alarm status bit. */ 1670b2f6228SArnaud Ebalard static int _abb5zes3_rtc_clear_alarm(struct device *dev) 1680b2f6228SArnaud Ebalard { 1690b2f6228SArnaud Ebalard struct abb5zes3_rtc_data *data = dev_get_drvdata(dev); 1700b2f6228SArnaud Ebalard int ret; 1710b2f6228SArnaud Ebalard 1720b2f6228SArnaud Ebalard ret = regmap_update_bits(data->regmap, ABB5ZES3_REG_CTRL2, 1730b2f6228SArnaud Ebalard ABB5ZES3_REG_CTRL2_AF, 0); 1740b2f6228SArnaud Ebalard if (ret) 1750b2f6228SArnaud Ebalard dev_err(dev, "%s: clearing alarm failed (%d)\n", __func__, ret); 1760b2f6228SArnaud Ebalard 1770b2f6228SArnaud Ebalard return ret; 1780b2f6228SArnaud Ebalard } 1790b2f6228SArnaud Ebalard 1800b2f6228SArnaud Ebalard /* Enable or disable alarm (i.e. alarm interrupt generation) */ 1810b2f6228SArnaud Ebalard static int _abb5zes3_rtc_update_alarm(struct device *dev, bool enable) 1820b2f6228SArnaud Ebalard { 1830b2f6228SArnaud Ebalard struct abb5zes3_rtc_data *data = dev_get_drvdata(dev); 1840b2f6228SArnaud Ebalard int ret; 1850b2f6228SArnaud Ebalard 1860b2f6228SArnaud Ebalard ret = regmap_update_bits(data->regmap, ABB5ZES3_REG_CTRL1, 1870b2f6228SArnaud Ebalard ABB5ZES3_REG_CTRL1_AIE, 1880b2f6228SArnaud Ebalard enable ? ABB5ZES3_REG_CTRL1_AIE : 0); 1890b2f6228SArnaud Ebalard if (ret) 1900b2f6228SArnaud Ebalard dev_err(dev, "%s: writing alarm INT failed (%d)\n", 1910b2f6228SArnaud Ebalard __func__, ret); 1920b2f6228SArnaud Ebalard 1930b2f6228SArnaud Ebalard return ret; 1940b2f6228SArnaud Ebalard } 1950b2f6228SArnaud Ebalard 196c8a1d8a5SArnaud Ebalard /* Enable or disable timer (watchdog timer A interrupt generation) */ 197c8a1d8a5SArnaud Ebalard static int _abb5zes3_rtc_update_timer(struct device *dev, bool enable) 198c8a1d8a5SArnaud Ebalard { 199c8a1d8a5SArnaud Ebalard struct abb5zes3_rtc_data *data = dev_get_drvdata(dev); 200c8a1d8a5SArnaud Ebalard int ret; 201c8a1d8a5SArnaud Ebalard 202c8a1d8a5SArnaud Ebalard ret = regmap_update_bits(data->regmap, ABB5ZES3_REG_CTRL2, 203c8a1d8a5SArnaud Ebalard ABB5ZES3_REG_CTRL2_WTAIE, 204c8a1d8a5SArnaud Ebalard enable ? ABB5ZES3_REG_CTRL2_WTAIE : 0); 205c8a1d8a5SArnaud Ebalard if (ret) 206c8a1d8a5SArnaud Ebalard dev_err(dev, "%s: writing timer INT failed (%d)\n", 207c8a1d8a5SArnaud Ebalard __func__, ret); 208c8a1d8a5SArnaud Ebalard 209c8a1d8a5SArnaud Ebalard return ret; 210c8a1d8a5SArnaud Ebalard } 211c8a1d8a5SArnaud Ebalard 2120b2f6228SArnaud Ebalard /* 2130b2f6228SArnaud Ebalard * Note: we only read, so regmap inner lock protection is sufficient, i.e. 2140b2f6228SArnaud Ebalard * we do not need driver's main lock protection. 2150b2f6228SArnaud Ebalard */ 2160b2f6228SArnaud Ebalard static int _abb5zes3_rtc_read_time(struct device *dev, struct rtc_time *tm) 2170b2f6228SArnaud Ebalard { 2180b2f6228SArnaud Ebalard struct abb5zes3_rtc_data *data = dev_get_drvdata(dev); 2190b2f6228SArnaud Ebalard u8 regs[ABB5ZES3_REG_RTC_SC + ABB5ZES3_RTC_SEC_LEN]; 220ce2e5a76SAlexandre Belloni int ret = 0; 2210b2f6228SArnaud Ebalard 2220b2f6228SArnaud Ebalard /* 2230b2f6228SArnaud Ebalard * As we need to read CTRL1 register anyway to access 24/12h 2240b2f6228SArnaud Ebalard * mode bit, we do a single bulk read of both control and RTC 2250b2f6228SArnaud Ebalard * sections (they are consecutive). This also ease indexing 2260b2f6228SArnaud Ebalard * of register values after bulk read. 2270b2f6228SArnaud Ebalard */ 2280b2f6228SArnaud Ebalard ret = regmap_bulk_read(data->regmap, ABB5ZES3_REG_CTRL1, regs, 2290b2f6228SArnaud Ebalard sizeof(regs)); 2300b2f6228SArnaud Ebalard if (ret) { 2310b2f6228SArnaud Ebalard dev_err(dev, "%s: reading RTC time failed (%d)\n", 2320b2f6228SArnaud Ebalard __func__, ret); 2330b2f6228SArnaud Ebalard goto err; 2340b2f6228SArnaud Ebalard } 2350b2f6228SArnaud Ebalard 2360b2f6228SArnaud Ebalard /* If clock integrity is not guaranteed, do not return a time value */ 2370b2f6228SArnaud Ebalard if (regs[ABB5ZES3_REG_RTC_SC] & ABB5ZES3_REG_RTC_SC_OSC) { 2380b2f6228SArnaud Ebalard ret = -ENODATA; 2390b2f6228SArnaud Ebalard goto err; 2400b2f6228SArnaud Ebalard } 2410b2f6228SArnaud Ebalard 2420b2f6228SArnaud Ebalard tm->tm_sec = bcd2bin(regs[ABB5ZES3_REG_RTC_SC] & 0x7F); 2430b2f6228SArnaud Ebalard tm->tm_min = bcd2bin(regs[ABB5ZES3_REG_RTC_MN]); 2440b2f6228SArnaud Ebalard 2450b2f6228SArnaud Ebalard if (regs[ABB5ZES3_REG_CTRL1] & ABB5ZES3_REG_CTRL1_PM) { /* 12hr mode */ 2460b2f6228SArnaud Ebalard tm->tm_hour = bcd2bin(regs[ABB5ZES3_REG_RTC_HR] & 0x1f); 2470b2f6228SArnaud Ebalard if (regs[ABB5ZES3_REG_RTC_HR] & ABB5ZES3_REG_RTC_HR_PM) /* PM */ 2480b2f6228SArnaud Ebalard tm->tm_hour += 12; 2490b2f6228SArnaud Ebalard } else { /* 24hr mode */ 2500b2f6228SArnaud Ebalard tm->tm_hour = bcd2bin(regs[ABB5ZES3_REG_RTC_HR]); 2510b2f6228SArnaud Ebalard } 2520b2f6228SArnaud Ebalard 2530b2f6228SArnaud Ebalard tm->tm_mday = bcd2bin(regs[ABB5ZES3_REG_RTC_DT]); 2540b2f6228SArnaud Ebalard tm->tm_wday = bcd2bin(regs[ABB5ZES3_REG_RTC_DW]); 2550b2f6228SArnaud Ebalard tm->tm_mon = bcd2bin(regs[ABB5ZES3_REG_RTC_MO]) - 1; /* starts at 1 */ 2560b2f6228SArnaud Ebalard tm->tm_year = bcd2bin(regs[ABB5ZES3_REG_RTC_YR]) + 100; 2570b2f6228SArnaud Ebalard 2580b2f6228SArnaud Ebalard err: 2590b2f6228SArnaud Ebalard return ret; 2600b2f6228SArnaud Ebalard } 2610b2f6228SArnaud Ebalard 2620b2f6228SArnaud Ebalard static int abb5zes3_rtc_set_time(struct device *dev, struct rtc_time *tm) 2630b2f6228SArnaud Ebalard { 2640b2f6228SArnaud Ebalard struct abb5zes3_rtc_data *data = dev_get_drvdata(dev); 2650b2f6228SArnaud Ebalard u8 regs[ABB5ZES3_REG_RTC_SC + ABB5ZES3_RTC_SEC_LEN]; 2660b2f6228SArnaud Ebalard int ret; 2670b2f6228SArnaud Ebalard 2680b2f6228SArnaud Ebalard /* 2690b2f6228SArnaud Ebalard * Year register is 8-bit wide and bcd-coded, i.e records values 2700b2f6228SArnaud Ebalard * between 0 and 99. tm_year is an offset from 1900 and we are 2710b2f6228SArnaud Ebalard * interested in the 2000-2099 range, so any value less than 100 2720b2f6228SArnaud Ebalard * is invalid. 2730b2f6228SArnaud Ebalard */ 2740b2f6228SArnaud Ebalard if (tm->tm_year < 100) 2750b2f6228SArnaud Ebalard return -EINVAL; 2760b2f6228SArnaud Ebalard 2770b2f6228SArnaud Ebalard regs[ABB5ZES3_REG_RTC_SC] = bin2bcd(tm->tm_sec); /* MSB=0 clears OSC */ 2780b2f6228SArnaud Ebalard regs[ABB5ZES3_REG_RTC_MN] = bin2bcd(tm->tm_min); 2790b2f6228SArnaud Ebalard regs[ABB5ZES3_REG_RTC_HR] = bin2bcd(tm->tm_hour); /* 24-hour format */ 2800b2f6228SArnaud Ebalard regs[ABB5ZES3_REG_RTC_DT] = bin2bcd(tm->tm_mday); 2810b2f6228SArnaud Ebalard regs[ABB5ZES3_REG_RTC_DW] = bin2bcd(tm->tm_wday); 2820b2f6228SArnaud Ebalard regs[ABB5ZES3_REG_RTC_MO] = bin2bcd(tm->tm_mon + 1); 2830b2f6228SArnaud Ebalard regs[ABB5ZES3_REG_RTC_YR] = bin2bcd(tm->tm_year - 100); 2840b2f6228SArnaud Ebalard 2850b2f6228SArnaud Ebalard mutex_lock(&data->lock); 2860b2f6228SArnaud Ebalard ret = regmap_bulk_write(data->regmap, ABB5ZES3_REG_RTC_SC, 2870b2f6228SArnaud Ebalard regs + ABB5ZES3_REG_RTC_SC, 2880b2f6228SArnaud Ebalard ABB5ZES3_RTC_SEC_LEN); 2890b2f6228SArnaud Ebalard mutex_unlock(&data->lock); 2900b2f6228SArnaud Ebalard 2910b2f6228SArnaud Ebalard 2920b2f6228SArnaud Ebalard return ret; 2930b2f6228SArnaud Ebalard } 2940b2f6228SArnaud Ebalard 295c8a1d8a5SArnaud Ebalard /* 296c8a1d8a5SArnaud Ebalard * Set provided TAQ and Timer A registers (TIMA_CLK and TIMA) based on 297c8a1d8a5SArnaud Ebalard * given number of seconds. 298c8a1d8a5SArnaud Ebalard */ 299c8a1d8a5SArnaud Ebalard static inline void sec_to_timer_a(u8 secs, u8 *taq, u8 *timer_a) 300c8a1d8a5SArnaud Ebalard { 301c8a1d8a5SArnaud Ebalard *taq = ABB5ZES3_REG_TIMA_CLK_TAQ1; /* 1Hz */ 302c8a1d8a5SArnaud Ebalard *timer_a = secs; 303c8a1d8a5SArnaud Ebalard } 304c8a1d8a5SArnaud Ebalard 305c8a1d8a5SArnaud Ebalard /* 306c8a1d8a5SArnaud Ebalard * Return current number of seconds in Timer A. As we only use 307c8a1d8a5SArnaud Ebalard * timer A with a 1Hz freq, this is what we expect to have. 308c8a1d8a5SArnaud Ebalard */ 309c8a1d8a5SArnaud Ebalard static inline int sec_from_timer_a(u8 *secs, u8 taq, u8 timer_a) 310c8a1d8a5SArnaud Ebalard { 311c8a1d8a5SArnaud Ebalard if (taq != ABB5ZES3_REG_TIMA_CLK_TAQ1) /* 1Hz */ 312c8a1d8a5SArnaud Ebalard return -EINVAL; 313c8a1d8a5SArnaud Ebalard 314c8a1d8a5SArnaud Ebalard *secs = timer_a; 315c8a1d8a5SArnaud Ebalard 316c8a1d8a5SArnaud Ebalard return 0; 317c8a1d8a5SArnaud Ebalard } 318c8a1d8a5SArnaud Ebalard 319c8a1d8a5SArnaud Ebalard /* 320c8a1d8a5SArnaud Ebalard * Read alarm currently configured via a watchdog timer using timer A. This 321c8a1d8a5SArnaud Ebalard * is done by reading current RTC time and adding remaining timer time. 322c8a1d8a5SArnaud Ebalard */ 323c8a1d8a5SArnaud Ebalard static int _abb5zes3_rtc_read_timer(struct device *dev, 324c8a1d8a5SArnaud Ebalard struct rtc_wkalrm *alarm) 325c8a1d8a5SArnaud Ebalard { 326c8a1d8a5SArnaud Ebalard struct abb5zes3_rtc_data *data = dev_get_drvdata(dev); 327c8a1d8a5SArnaud Ebalard struct rtc_time rtc_tm, *alarm_tm = &alarm->time; 328c8a1d8a5SArnaud Ebalard u8 regs[ABB5ZES3_TIMA_SEC_LEN + 1]; 329c8a1d8a5SArnaud Ebalard unsigned long rtc_secs; 330c8a1d8a5SArnaud Ebalard unsigned int reg; 331c8a1d8a5SArnaud Ebalard u8 timer_secs; 332c8a1d8a5SArnaud Ebalard int ret; 333c8a1d8a5SArnaud Ebalard 334c8a1d8a5SArnaud Ebalard /* 335c8a1d8a5SArnaud Ebalard * Instead of doing two separate calls, because they are consecutive, 336c8a1d8a5SArnaud Ebalard * we grab both clockout register and Timer A section. The latter is 337c8a1d8a5SArnaud Ebalard * used to decide if timer A is enabled (as a watchdog timer). 338c8a1d8a5SArnaud Ebalard */ 339c8a1d8a5SArnaud Ebalard ret = regmap_bulk_read(data->regmap, ABB5ZES3_REG_TIM_CLK, regs, 340c8a1d8a5SArnaud Ebalard ABB5ZES3_TIMA_SEC_LEN + 1); 341c8a1d8a5SArnaud Ebalard if (ret) { 342c8a1d8a5SArnaud Ebalard dev_err(dev, "%s: reading Timer A section failed (%d)\n", 343c8a1d8a5SArnaud Ebalard __func__, ret); 344c8a1d8a5SArnaud Ebalard goto err; 345c8a1d8a5SArnaud Ebalard } 346c8a1d8a5SArnaud Ebalard 347c8a1d8a5SArnaud Ebalard /* get current time ... */ 348c8a1d8a5SArnaud Ebalard ret = _abb5zes3_rtc_read_time(dev, &rtc_tm); 349c8a1d8a5SArnaud Ebalard if (ret) 350c8a1d8a5SArnaud Ebalard goto err; 351c8a1d8a5SArnaud Ebalard 352c8a1d8a5SArnaud Ebalard /* ... convert to seconds ... */ 353c8a1d8a5SArnaud Ebalard ret = rtc_tm_to_time(&rtc_tm, &rtc_secs); 354c8a1d8a5SArnaud Ebalard if (ret) 355c8a1d8a5SArnaud Ebalard goto err; 356c8a1d8a5SArnaud Ebalard 357c8a1d8a5SArnaud Ebalard /* ... add remaining timer A time ... */ 358c8a1d8a5SArnaud Ebalard ret = sec_from_timer_a(&timer_secs, regs[1], regs[2]); 359c8a1d8a5SArnaud Ebalard if (ret) 360c8a1d8a5SArnaud Ebalard goto err; 361c8a1d8a5SArnaud Ebalard 362c8a1d8a5SArnaud Ebalard /* ... and convert back. */ 363c8a1d8a5SArnaud Ebalard rtc_time_to_tm(rtc_secs + timer_secs, alarm_tm); 364c8a1d8a5SArnaud Ebalard 365c8a1d8a5SArnaud Ebalard ret = regmap_read(data->regmap, ABB5ZES3_REG_CTRL2, ®); 366c8a1d8a5SArnaud Ebalard if (ret) { 367c8a1d8a5SArnaud Ebalard dev_err(dev, "%s: reading ctrl reg failed (%d)\n", 368c8a1d8a5SArnaud Ebalard __func__, ret); 369c8a1d8a5SArnaud Ebalard goto err; 370c8a1d8a5SArnaud Ebalard } 371c8a1d8a5SArnaud Ebalard 372c8a1d8a5SArnaud Ebalard alarm->enabled = !!(reg & ABB5ZES3_REG_CTRL2_WTAIE); 373c8a1d8a5SArnaud Ebalard 374c8a1d8a5SArnaud Ebalard err: 375c8a1d8a5SArnaud Ebalard return ret; 376c8a1d8a5SArnaud Ebalard } 377c8a1d8a5SArnaud Ebalard 378c8a1d8a5SArnaud Ebalard /* Read alarm currently configured via a RTC alarm registers. */ 379c8a1d8a5SArnaud Ebalard static int _abb5zes3_rtc_read_alarm(struct device *dev, 380c8a1d8a5SArnaud Ebalard struct rtc_wkalrm *alarm) 3810b2f6228SArnaud Ebalard { 3820b2f6228SArnaud Ebalard struct abb5zes3_rtc_data *data = dev_get_drvdata(dev); 3830b2f6228SArnaud Ebalard struct rtc_time rtc_tm, *alarm_tm = &alarm->time; 3840b2f6228SArnaud Ebalard unsigned long rtc_secs, alarm_secs; 3850b2f6228SArnaud Ebalard u8 regs[ABB5ZES3_ALRM_SEC_LEN]; 3860b2f6228SArnaud Ebalard unsigned int reg; 3870b2f6228SArnaud Ebalard int ret; 3880b2f6228SArnaud Ebalard 3890b2f6228SArnaud Ebalard ret = regmap_bulk_read(data->regmap, ABB5ZES3_REG_ALRM_MN, regs, 3900b2f6228SArnaud Ebalard ABB5ZES3_ALRM_SEC_LEN); 3910b2f6228SArnaud Ebalard if (ret) { 3920b2f6228SArnaud Ebalard dev_err(dev, "%s: reading alarm section failed (%d)\n", 3930b2f6228SArnaud Ebalard __func__, ret); 3940b2f6228SArnaud Ebalard goto err; 3950b2f6228SArnaud Ebalard } 3960b2f6228SArnaud Ebalard 3970b2f6228SArnaud Ebalard alarm_tm->tm_sec = 0; 3980b2f6228SArnaud Ebalard alarm_tm->tm_min = bcd2bin(regs[0] & 0x7f); 3990b2f6228SArnaud Ebalard alarm_tm->tm_hour = bcd2bin(regs[1] & 0x3f); 4000b2f6228SArnaud Ebalard alarm_tm->tm_mday = bcd2bin(regs[2] & 0x3f); 4010b2f6228SArnaud Ebalard alarm_tm->tm_wday = -1; 4020b2f6228SArnaud Ebalard 4030b2f6228SArnaud Ebalard /* 4040b2f6228SArnaud Ebalard * The alarm section does not store year/month. We use the ones in rtc 4050b2f6228SArnaud Ebalard * section as a basis and increment month and then year if needed to get 4060b2f6228SArnaud Ebalard * alarm after current time. 4070b2f6228SArnaud Ebalard */ 4080b2f6228SArnaud Ebalard ret = _abb5zes3_rtc_read_time(dev, &rtc_tm); 4090b2f6228SArnaud Ebalard if (ret) 4100b2f6228SArnaud Ebalard goto err; 4110b2f6228SArnaud Ebalard 4120b2f6228SArnaud Ebalard alarm_tm->tm_year = rtc_tm.tm_year; 4130b2f6228SArnaud Ebalard alarm_tm->tm_mon = rtc_tm.tm_mon; 4140b2f6228SArnaud Ebalard 4150b2f6228SArnaud Ebalard ret = rtc_tm_to_time(&rtc_tm, &rtc_secs); 4160b2f6228SArnaud Ebalard if (ret) 4170b2f6228SArnaud Ebalard goto err; 4180b2f6228SArnaud Ebalard 4190b2f6228SArnaud Ebalard ret = rtc_tm_to_time(alarm_tm, &alarm_secs); 4200b2f6228SArnaud Ebalard if (ret) 4210b2f6228SArnaud Ebalard goto err; 4220b2f6228SArnaud Ebalard 4230b2f6228SArnaud Ebalard if (alarm_secs < rtc_secs) { 4240b2f6228SArnaud Ebalard if (alarm_tm->tm_mon == 11) { 4250b2f6228SArnaud Ebalard alarm_tm->tm_mon = 0; 4260b2f6228SArnaud Ebalard alarm_tm->tm_year += 1; 4270b2f6228SArnaud Ebalard } else { 4280b2f6228SArnaud Ebalard alarm_tm->tm_mon += 1; 4290b2f6228SArnaud Ebalard } 4300b2f6228SArnaud Ebalard } 4310b2f6228SArnaud Ebalard 4320b2f6228SArnaud Ebalard ret = regmap_read(data->regmap, ABB5ZES3_REG_CTRL1, ®); 4330b2f6228SArnaud Ebalard if (ret) { 4340b2f6228SArnaud Ebalard dev_err(dev, "%s: reading ctrl reg failed (%d)\n", 4350b2f6228SArnaud Ebalard __func__, ret); 4360b2f6228SArnaud Ebalard goto err; 4370b2f6228SArnaud Ebalard } 4380b2f6228SArnaud Ebalard 4390b2f6228SArnaud Ebalard alarm->enabled = !!(reg & ABB5ZES3_REG_CTRL1_AIE); 4400b2f6228SArnaud Ebalard 4410b2f6228SArnaud Ebalard err: 442c8a1d8a5SArnaud Ebalard return ret; 443c8a1d8a5SArnaud Ebalard } 444c8a1d8a5SArnaud Ebalard 445c8a1d8a5SArnaud Ebalard /* 446c8a1d8a5SArnaud Ebalard * As the Alarm mechanism supported by the chip is only accurate to the 447c8a1d8a5SArnaud Ebalard * minute, we use the watchdog timer mechanism provided by timer A 448c8a1d8a5SArnaud Ebalard * (up to 256 seconds w/ a second accuracy) for low alarm values (below 449c8a1d8a5SArnaud Ebalard * 4 minutes). Otherwise, we use the common alarm mechanism provided 450c8a1d8a5SArnaud Ebalard * by the chip. In order for that to work, we keep track of currently 451c8a1d8a5SArnaud Ebalard * configured timer type via 'timer_alarm' flag in our private data 452c8a1d8a5SArnaud Ebalard * structure. 453c8a1d8a5SArnaud Ebalard */ 454c8a1d8a5SArnaud Ebalard static int abb5zes3_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) 455c8a1d8a5SArnaud Ebalard { 456c8a1d8a5SArnaud Ebalard struct abb5zes3_rtc_data *data = dev_get_drvdata(dev); 457c8a1d8a5SArnaud Ebalard int ret; 458c8a1d8a5SArnaud Ebalard 459c8a1d8a5SArnaud Ebalard mutex_lock(&data->lock); 460c8a1d8a5SArnaud Ebalard if (data->timer_alarm) 461c8a1d8a5SArnaud Ebalard ret = _abb5zes3_rtc_read_timer(dev, alarm); 462c8a1d8a5SArnaud Ebalard else 463c8a1d8a5SArnaud Ebalard ret = _abb5zes3_rtc_read_alarm(dev, alarm); 4640b2f6228SArnaud Ebalard mutex_unlock(&data->lock); 4650b2f6228SArnaud Ebalard 4660b2f6228SArnaud Ebalard return ret; 4670b2f6228SArnaud Ebalard } 4680b2f6228SArnaud Ebalard 469c8a1d8a5SArnaud Ebalard /* 470c8a1d8a5SArnaud Ebalard * Set alarm using chip alarm mechanism. It is only accurate to the 471c8a1d8a5SArnaud Ebalard * minute (not the second). The function expects alarm interrupt to 472c8a1d8a5SArnaud Ebalard * be disabled. 473c8a1d8a5SArnaud Ebalard */ 474c8a1d8a5SArnaud Ebalard static int _abb5zes3_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) 4750b2f6228SArnaud Ebalard { 4760b2f6228SArnaud Ebalard struct abb5zes3_rtc_data *data = dev_get_drvdata(dev); 4770b2f6228SArnaud Ebalard struct rtc_time *alarm_tm = &alarm->time; 4780b2f6228SArnaud Ebalard unsigned long rtc_secs, alarm_secs; 4790b2f6228SArnaud Ebalard u8 regs[ABB5ZES3_ALRM_SEC_LEN]; 4800b2f6228SArnaud Ebalard struct rtc_time rtc_tm; 4810b2f6228SArnaud Ebalard int ret, enable = 1; 4820b2f6228SArnaud Ebalard 4830b2f6228SArnaud Ebalard ret = _abb5zes3_rtc_read_time(dev, &rtc_tm); 4840b2f6228SArnaud Ebalard if (ret) 4850b2f6228SArnaud Ebalard goto err; 4860b2f6228SArnaud Ebalard 4870b2f6228SArnaud Ebalard ret = rtc_tm_to_time(&rtc_tm, &rtc_secs); 4880b2f6228SArnaud Ebalard if (ret) 4890b2f6228SArnaud Ebalard goto err; 4900b2f6228SArnaud Ebalard 4910b2f6228SArnaud Ebalard ret = rtc_tm_to_time(alarm_tm, &alarm_secs); 4920b2f6228SArnaud Ebalard if (ret) 4930b2f6228SArnaud Ebalard goto err; 4940b2f6228SArnaud Ebalard 4950b2f6228SArnaud Ebalard /* If alarm time is before current time, disable the alarm */ 4960b2f6228SArnaud Ebalard if (!alarm->enabled || alarm_secs <= rtc_secs) { 4970b2f6228SArnaud Ebalard enable = 0; 4980b2f6228SArnaud Ebalard } else { 4990b2f6228SArnaud Ebalard /* 5000b2f6228SArnaud Ebalard * Chip only support alarms up to one month in the future. Let's 5010b2f6228SArnaud Ebalard * return an error if we get something after that limit. 5020b2f6228SArnaud Ebalard * Comparison is done by incrementing rtc_tm month field by one 5030b2f6228SArnaud Ebalard * and checking alarm value is still below. 5040b2f6228SArnaud Ebalard */ 5050b2f6228SArnaud Ebalard if (rtc_tm.tm_mon == 11) { /* handle year wrapping */ 5060b2f6228SArnaud Ebalard rtc_tm.tm_mon = 0; 5070b2f6228SArnaud Ebalard rtc_tm.tm_year += 1; 5080b2f6228SArnaud Ebalard } else { 5090b2f6228SArnaud Ebalard rtc_tm.tm_mon += 1; 5100b2f6228SArnaud Ebalard } 5110b2f6228SArnaud Ebalard 5120b2f6228SArnaud Ebalard ret = rtc_tm_to_time(&rtc_tm, &rtc_secs); 5130b2f6228SArnaud Ebalard if (ret) 5140b2f6228SArnaud Ebalard goto err; 5150b2f6228SArnaud Ebalard 5160b2f6228SArnaud Ebalard if (alarm_secs > rtc_secs) { 5170b2f6228SArnaud Ebalard dev_err(dev, "%s: alarm maximum is one month in the " 5180b2f6228SArnaud Ebalard "future (%d)\n", __func__, ret); 5190b2f6228SArnaud Ebalard ret = -EINVAL; 5200b2f6228SArnaud Ebalard goto err; 5210b2f6228SArnaud Ebalard } 5220b2f6228SArnaud Ebalard } 5230b2f6228SArnaud Ebalard 524c8a1d8a5SArnaud Ebalard /* 525c8a1d8a5SArnaud Ebalard * Program all alarm registers but DW one. For each register, setting 526c8a1d8a5SArnaud Ebalard * MSB to 0 enables associated alarm. 527c8a1d8a5SArnaud Ebalard */ 528c8a1d8a5SArnaud Ebalard regs[0] = bin2bcd(alarm_tm->tm_min) & 0x7f; 529c8a1d8a5SArnaud Ebalard regs[1] = bin2bcd(alarm_tm->tm_hour) & 0x3f; 530c8a1d8a5SArnaud Ebalard regs[2] = bin2bcd(alarm_tm->tm_mday) & 0x3f; 5310b2f6228SArnaud Ebalard regs[3] = ABB5ZES3_REG_ALRM_DW_AE; /* do not match day of the week */ 5320b2f6228SArnaud Ebalard 5330b2f6228SArnaud Ebalard ret = regmap_bulk_write(data->regmap, ABB5ZES3_REG_ALRM_MN, regs, 5340b2f6228SArnaud Ebalard ABB5ZES3_ALRM_SEC_LEN); 5350b2f6228SArnaud Ebalard if (ret < 0) { 5360b2f6228SArnaud Ebalard dev_err(dev, "%s: writing ALARM section failed (%d)\n", 5370b2f6228SArnaud Ebalard __func__, ret); 5380b2f6228SArnaud Ebalard goto err; 5390b2f6228SArnaud Ebalard } 5400b2f6228SArnaud Ebalard 541c8a1d8a5SArnaud Ebalard /* Record currently configured alarm is not a timer */ 542c8a1d8a5SArnaud Ebalard data->timer_alarm = 0; 543c8a1d8a5SArnaud Ebalard 544c8a1d8a5SArnaud Ebalard /* Enable or disable alarm interrupt generation */ 5450b2f6228SArnaud Ebalard ret = _abb5zes3_rtc_update_alarm(dev, enable); 5460b2f6228SArnaud Ebalard 5470b2f6228SArnaud Ebalard err: 548c8a1d8a5SArnaud Ebalard return ret; 549c8a1d8a5SArnaud Ebalard } 550c8a1d8a5SArnaud Ebalard 551c8a1d8a5SArnaud Ebalard /* 552c8a1d8a5SArnaud Ebalard * Set alarm using timer watchdog (via timer A) mechanism. The function expects 553c8a1d8a5SArnaud Ebalard * timer A interrupt to be disabled. 554c8a1d8a5SArnaud Ebalard */ 555c8a1d8a5SArnaud Ebalard static int _abb5zes3_rtc_set_timer(struct device *dev, struct rtc_wkalrm *alarm, 556c8a1d8a5SArnaud Ebalard u8 secs) 557c8a1d8a5SArnaud Ebalard { 558c8a1d8a5SArnaud Ebalard struct abb5zes3_rtc_data *data = dev_get_drvdata(dev); 559c8a1d8a5SArnaud Ebalard u8 regs[ABB5ZES3_TIMA_SEC_LEN]; 560c8a1d8a5SArnaud Ebalard u8 mask = ABB5ZES3_REG_TIM_CLK_TAC0 | ABB5ZES3_REG_TIM_CLK_TAC1; 561c8a1d8a5SArnaud Ebalard int ret = 0; 562c8a1d8a5SArnaud Ebalard 563c8a1d8a5SArnaud Ebalard /* Program given number of seconds to Timer A registers */ 564c8a1d8a5SArnaud Ebalard sec_to_timer_a(secs, ®s[0], ®s[1]); 565c8a1d8a5SArnaud Ebalard ret = regmap_bulk_write(data->regmap, ABB5ZES3_REG_TIMA_CLK, regs, 566c8a1d8a5SArnaud Ebalard ABB5ZES3_TIMA_SEC_LEN); 567c8a1d8a5SArnaud Ebalard if (ret < 0) { 568c8a1d8a5SArnaud Ebalard dev_err(dev, "%s: writing timer section failed\n", __func__); 569c8a1d8a5SArnaud Ebalard goto err; 570c8a1d8a5SArnaud Ebalard } 571c8a1d8a5SArnaud Ebalard 572c8a1d8a5SArnaud Ebalard /* Configure Timer A as a watchdog timer */ 573c8a1d8a5SArnaud Ebalard ret = regmap_update_bits(data->regmap, ABB5ZES3_REG_TIM_CLK, 574c8a1d8a5SArnaud Ebalard mask, ABB5ZES3_REG_TIM_CLK_TAC1); 575c8a1d8a5SArnaud Ebalard if (ret) 576c8a1d8a5SArnaud Ebalard dev_err(dev, "%s: failed to update timer\n", __func__); 577c8a1d8a5SArnaud Ebalard 578c8a1d8a5SArnaud Ebalard /* Record currently configured alarm is a timer */ 579c8a1d8a5SArnaud Ebalard data->timer_alarm = 1; 580c8a1d8a5SArnaud Ebalard 581c8a1d8a5SArnaud Ebalard /* Enable or disable timer interrupt generation */ 582c8a1d8a5SArnaud Ebalard ret = _abb5zes3_rtc_update_timer(dev, alarm->enabled); 583c8a1d8a5SArnaud Ebalard 584c8a1d8a5SArnaud Ebalard err: 585c8a1d8a5SArnaud Ebalard return ret; 586c8a1d8a5SArnaud Ebalard } 587c8a1d8a5SArnaud Ebalard 588c8a1d8a5SArnaud Ebalard /* 589c8a1d8a5SArnaud Ebalard * The chip has an alarm which is only accurate to the minute. In order to 590c8a1d8a5SArnaud Ebalard * handle alarms below that limit, we use the watchdog timer function of 591c8a1d8a5SArnaud Ebalard * timer A. More precisely, the timer method is used for alarms below 240 592c8a1d8a5SArnaud Ebalard * seconds. 593c8a1d8a5SArnaud Ebalard */ 594c8a1d8a5SArnaud Ebalard static int abb5zes3_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) 595c8a1d8a5SArnaud Ebalard { 596c8a1d8a5SArnaud Ebalard struct abb5zes3_rtc_data *data = dev_get_drvdata(dev); 597c8a1d8a5SArnaud Ebalard struct rtc_time *alarm_tm = &alarm->time; 598c8a1d8a5SArnaud Ebalard unsigned long rtc_secs, alarm_secs; 599c8a1d8a5SArnaud Ebalard struct rtc_time rtc_tm; 600c8a1d8a5SArnaud Ebalard int ret; 601c8a1d8a5SArnaud Ebalard 602c8a1d8a5SArnaud Ebalard mutex_lock(&data->lock); 603c8a1d8a5SArnaud Ebalard ret = _abb5zes3_rtc_read_time(dev, &rtc_tm); 604c8a1d8a5SArnaud Ebalard if (ret) 605c8a1d8a5SArnaud Ebalard goto err; 606c8a1d8a5SArnaud Ebalard 607c8a1d8a5SArnaud Ebalard ret = rtc_tm_to_time(&rtc_tm, &rtc_secs); 608c8a1d8a5SArnaud Ebalard if (ret) 609c8a1d8a5SArnaud Ebalard goto err; 610c8a1d8a5SArnaud Ebalard 611c8a1d8a5SArnaud Ebalard ret = rtc_tm_to_time(alarm_tm, &alarm_secs); 612c8a1d8a5SArnaud Ebalard if (ret) 613c8a1d8a5SArnaud Ebalard goto err; 614c8a1d8a5SArnaud Ebalard 615c8a1d8a5SArnaud Ebalard /* Let's first disable both the alarm and the timer interrupts */ 616c8a1d8a5SArnaud Ebalard ret = _abb5zes3_rtc_update_alarm(dev, false); 617c8a1d8a5SArnaud Ebalard if (ret < 0) { 618c8a1d8a5SArnaud Ebalard dev_err(dev, "%s: unable to disable alarm (%d)\n", __func__, 619c8a1d8a5SArnaud Ebalard ret); 620c8a1d8a5SArnaud Ebalard goto err; 621c8a1d8a5SArnaud Ebalard } 622c8a1d8a5SArnaud Ebalard ret = _abb5zes3_rtc_update_timer(dev, false); 623c8a1d8a5SArnaud Ebalard if (ret < 0) { 624c8a1d8a5SArnaud Ebalard dev_err(dev, "%s: unable to disable timer (%d)\n", __func__, 625c8a1d8a5SArnaud Ebalard ret); 626c8a1d8a5SArnaud Ebalard goto err; 627c8a1d8a5SArnaud Ebalard } 628c8a1d8a5SArnaud Ebalard 629c8a1d8a5SArnaud Ebalard data->timer_alarm = 0; 630c8a1d8a5SArnaud Ebalard 631c8a1d8a5SArnaud Ebalard /* 632c8a1d8a5SArnaud Ebalard * Let's now configure the alarm; if we are expected to ring in 633c8a1d8a5SArnaud Ebalard * more than 240s, then we setup an alarm. Otherwise, a timer. 634c8a1d8a5SArnaud Ebalard */ 635c8a1d8a5SArnaud Ebalard if ((alarm_secs > rtc_secs) && ((alarm_secs - rtc_secs) <= 240)) 636c8a1d8a5SArnaud Ebalard ret = _abb5zes3_rtc_set_timer(dev, alarm, 637c8a1d8a5SArnaud Ebalard alarm_secs - rtc_secs); 638c8a1d8a5SArnaud Ebalard else 639c8a1d8a5SArnaud Ebalard ret = _abb5zes3_rtc_set_alarm(dev, alarm); 640c8a1d8a5SArnaud Ebalard 641c8a1d8a5SArnaud Ebalard err: 6420b2f6228SArnaud Ebalard mutex_unlock(&data->lock); 6430b2f6228SArnaud Ebalard 644c8a1d8a5SArnaud Ebalard if (ret) 645c8a1d8a5SArnaud Ebalard dev_err(dev, "%s: unable to configure alarm (%d)\n", __func__, 646c8a1d8a5SArnaud Ebalard ret); 647c8a1d8a5SArnaud Ebalard 6480b2f6228SArnaud Ebalard return ret; 6490b2f6228SArnaud Ebalard } 6500b2f6228SArnaud Ebalard 6510b2f6228SArnaud Ebalard /* Enable or disable battery low irq generation */ 6520b2f6228SArnaud Ebalard static inline int _abb5zes3_rtc_battery_low_irq_enable(struct regmap *regmap, 6530b2f6228SArnaud Ebalard bool enable) 6540b2f6228SArnaud Ebalard { 6550b2f6228SArnaud Ebalard return regmap_update_bits(regmap, ABB5ZES3_REG_CTRL3, 6560b2f6228SArnaud Ebalard ABB5ZES3_REG_CTRL3_BLIE, 6570b2f6228SArnaud Ebalard enable ? ABB5ZES3_REG_CTRL3_BLIE : 0); 6580b2f6228SArnaud Ebalard } 6590b2f6228SArnaud Ebalard 6600b2f6228SArnaud Ebalard /* 6610b2f6228SArnaud Ebalard * Check current RTC status and enable/disable what needs to be. Return 0 if 6620b2f6228SArnaud Ebalard * everything went ok and a negative value upon error. Note: this function 6630b2f6228SArnaud Ebalard * is called early during init and hence does need mutex protection. 6640b2f6228SArnaud Ebalard */ 6650b2f6228SArnaud Ebalard static int abb5zes3_rtc_check_setup(struct device *dev) 6660b2f6228SArnaud Ebalard { 6670b2f6228SArnaud Ebalard struct abb5zes3_rtc_data *data = dev_get_drvdata(dev); 6680b2f6228SArnaud Ebalard struct regmap *regmap = data->regmap; 6690b2f6228SArnaud Ebalard unsigned int reg; 6700b2f6228SArnaud Ebalard int ret; 6710b2f6228SArnaud Ebalard u8 mask; 6720b2f6228SArnaud Ebalard 6730b2f6228SArnaud Ebalard /* 6740b2f6228SArnaud Ebalard * By default, the devices generates a 32.768KHz signal on IRQ#1 pin. It 6750b2f6228SArnaud Ebalard * is disabled here to prevent polluting the interrupt line and 6760b2f6228SArnaud Ebalard * uselessly triggering the IRQ handler we install for alarm and battery 6770b2f6228SArnaud Ebalard * low events. Note: this is done before clearing int. status below 6780b2f6228SArnaud Ebalard * in this function. 6790b2f6228SArnaud Ebalard * We also disable all timers and set timer interrupt to permanent (not 6800b2f6228SArnaud Ebalard * pulsed). 6810b2f6228SArnaud Ebalard */ 6820b2f6228SArnaud Ebalard mask = (ABB5ZES3_REG_TIM_CLK_TBC | ABB5ZES3_REG_TIM_CLK_TAC0 | 6830b2f6228SArnaud Ebalard ABB5ZES3_REG_TIM_CLK_TAC1 | ABB5ZES3_REG_TIM_CLK_COF0 | 6840b2f6228SArnaud Ebalard ABB5ZES3_REG_TIM_CLK_COF1 | ABB5ZES3_REG_TIM_CLK_COF2 | 6850b2f6228SArnaud Ebalard ABB5ZES3_REG_TIM_CLK_TBM | ABB5ZES3_REG_TIM_CLK_TAM); 6860b2f6228SArnaud Ebalard ret = regmap_update_bits(regmap, ABB5ZES3_REG_TIM_CLK, mask, 6870b2f6228SArnaud Ebalard ABB5ZES3_REG_TIM_CLK_COF0 | ABB5ZES3_REG_TIM_CLK_COF1 | 6880b2f6228SArnaud Ebalard ABB5ZES3_REG_TIM_CLK_COF2); 6890b2f6228SArnaud Ebalard if (ret < 0) { 6900b2f6228SArnaud Ebalard dev_err(dev, "%s: unable to initialize clkout register (%d)\n", 6910b2f6228SArnaud Ebalard __func__, ret); 6920b2f6228SArnaud Ebalard return ret; 6930b2f6228SArnaud Ebalard } 6940b2f6228SArnaud Ebalard 6950b2f6228SArnaud Ebalard /* 6960b2f6228SArnaud Ebalard * Each component of the alarm (MN, HR, DT, DW) can be enabled/disabled 6970b2f6228SArnaud Ebalard * individually by clearing/setting MSB of each associated register. So, 6980b2f6228SArnaud Ebalard * we set all alarm enable bits to disable current alarm setting. 6990b2f6228SArnaud Ebalard */ 7000b2f6228SArnaud Ebalard mask = (ABB5ZES3_REG_ALRM_MN_AE | ABB5ZES3_REG_ALRM_HR_AE | 7010b2f6228SArnaud Ebalard ABB5ZES3_REG_ALRM_DT_AE | ABB5ZES3_REG_ALRM_DW_AE); 7020b2f6228SArnaud Ebalard ret = regmap_update_bits(regmap, ABB5ZES3_REG_CTRL2, mask, mask); 7030b2f6228SArnaud Ebalard if (ret < 0) { 7040b2f6228SArnaud Ebalard dev_err(dev, "%s: unable to disable alarm setting (%d)\n", 7050b2f6228SArnaud Ebalard __func__, ret); 7060b2f6228SArnaud Ebalard return ret; 7070b2f6228SArnaud Ebalard } 7080b2f6228SArnaud Ebalard 7090b2f6228SArnaud Ebalard /* Set Control 1 register (RTC enabled, 24hr mode, all int. disabled) */ 7100b2f6228SArnaud Ebalard mask = (ABB5ZES3_REG_CTRL1_CIE | ABB5ZES3_REG_CTRL1_AIE | 7110b2f6228SArnaud Ebalard ABB5ZES3_REG_CTRL1_SIE | ABB5ZES3_REG_CTRL1_PM | 7120b2f6228SArnaud Ebalard ABB5ZES3_REG_CTRL1_CAP | ABB5ZES3_REG_CTRL1_STOP); 7130b2f6228SArnaud Ebalard ret = regmap_update_bits(regmap, ABB5ZES3_REG_CTRL1, mask, 0); 7140b2f6228SArnaud Ebalard if (ret < 0) { 7150b2f6228SArnaud Ebalard dev_err(dev, "%s: unable to initialize CTRL1 register (%d)\n", 7160b2f6228SArnaud Ebalard __func__, ret); 7170b2f6228SArnaud Ebalard return ret; 7180b2f6228SArnaud Ebalard } 7190b2f6228SArnaud Ebalard 7200b2f6228SArnaud Ebalard /* 7210b2f6228SArnaud Ebalard * Set Control 2 register (timer int. disabled, alarm status cleared). 7220b2f6228SArnaud Ebalard * WTAF is read-only and cleared automatically by reading the register. 7230b2f6228SArnaud Ebalard */ 7240b2f6228SArnaud Ebalard mask = (ABB5ZES3_REG_CTRL2_CTBIE | ABB5ZES3_REG_CTRL2_CTAIE | 7250b2f6228SArnaud Ebalard ABB5ZES3_REG_CTRL2_WTAIE | ABB5ZES3_REG_CTRL2_AF | 7260b2f6228SArnaud Ebalard ABB5ZES3_REG_CTRL2_SF | ABB5ZES3_REG_CTRL2_CTBF | 7270b2f6228SArnaud Ebalard ABB5ZES3_REG_CTRL2_CTAF); 7280b2f6228SArnaud Ebalard ret = regmap_update_bits(regmap, ABB5ZES3_REG_CTRL2, mask, 0); 7290b2f6228SArnaud Ebalard if (ret < 0) { 7300b2f6228SArnaud Ebalard dev_err(dev, "%s: unable to initialize CTRL2 register (%d)\n", 7310b2f6228SArnaud Ebalard __func__, ret); 7320b2f6228SArnaud Ebalard return ret; 7330b2f6228SArnaud Ebalard } 7340b2f6228SArnaud Ebalard 7350b2f6228SArnaud Ebalard /* 7360b2f6228SArnaud Ebalard * Enable battery low detection function and battery switchover function 7370b2f6228SArnaud Ebalard * (standard mode). Disable associated interrupts. Clear battery 7380b2f6228SArnaud Ebalard * switchover flag but not battery low flag. The latter is checked 7390b2f6228SArnaud Ebalard * later below. 7400b2f6228SArnaud Ebalard */ 7410b2f6228SArnaud Ebalard mask = (ABB5ZES3_REG_CTRL3_PM0 | ABB5ZES3_REG_CTRL3_PM1 | 7420b2f6228SArnaud Ebalard ABB5ZES3_REG_CTRL3_PM2 | ABB5ZES3_REG_CTRL3_BLIE | 7430b2f6228SArnaud Ebalard ABB5ZES3_REG_CTRL3_BSIE| ABB5ZES3_REG_CTRL3_BSF); 7440b2f6228SArnaud Ebalard ret = regmap_update_bits(regmap, ABB5ZES3_REG_CTRL3, mask, 0); 7450b2f6228SArnaud Ebalard if (ret < 0) { 7460b2f6228SArnaud Ebalard dev_err(dev, "%s: unable to initialize CTRL3 register (%d)\n", 7470b2f6228SArnaud Ebalard __func__, ret); 7480b2f6228SArnaud Ebalard return ret; 7490b2f6228SArnaud Ebalard } 7500b2f6228SArnaud Ebalard 7510b2f6228SArnaud Ebalard /* Check oscillator integrity flag */ 7520b2f6228SArnaud Ebalard ret = regmap_read(regmap, ABB5ZES3_REG_RTC_SC, ®); 7530b2f6228SArnaud Ebalard if (ret < 0) { 7540b2f6228SArnaud Ebalard dev_err(dev, "%s: unable to read osc. integrity flag (%d)\n", 7550b2f6228SArnaud Ebalard __func__, ret); 7560b2f6228SArnaud Ebalard return ret; 7570b2f6228SArnaud Ebalard } 7580b2f6228SArnaud Ebalard 7590b2f6228SArnaud Ebalard if (reg & ABB5ZES3_REG_RTC_SC_OSC) { 7600b2f6228SArnaud Ebalard dev_err(dev, "clock integrity not guaranteed. Osc. has stopped " 7610b2f6228SArnaud Ebalard "or has been interrupted.\n"); 7620b2f6228SArnaud Ebalard dev_err(dev, "change battery (if not already done) and " 7630b2f6228SArnaud Ebalard "then set time to reset osc. failure flag.\n"); 7640b2f6228SArnaud Ebalard } 7650b2f6228SArnaud Ebalard 7660b2f6228SArnaud Ebalard /* 7670b2f6228SArnaud Ebalard * Check battery low flag at startup: this allows reporting battery 7680b2f6228SArnaud Ebalard * is low at startup when IRQ line is not connected. Note: we record 7690b2f6228SArnaud Ebalard * current status to avoid reenabling this interrupt later in probe 7700b2f6228SArnaud Ebalard * function if battery is low. 7710b2f6228SArnaud Ebalard */ 7720b2f6228SArnaud Ebalard ret = regmap_read(regmap, ABB5ZES3_REG_CTRL3, ®); 7730b2f6228SArnaud Ebalard if (ret < 0) { 7740b2f6228SArnaud Ebalard dev_err(dev, "%s: unable to read battery low flag (%d)\n", 7750b2f6228SArnaud Ebalard __func__, ret); 7760b2f6228SArnaud Ebalard return ret; 7770b2f6228SArnaud Ebalard } 7780b2f6228SArnaud Ebalard 7790b2f6228SArnaud Ebalard data->battery_low = reg & ABB5ZES3_REG_CTRL3_BLF; 7800b2f6228SArnaud Ebalard if (data->battery_low) { 7810b2f6228SArnaud Ebalard dev_err(dev, "RTC battery is low; please, consider " 7820b2f6228SArnaud Ebalard "changing it!\n"); 7830b2f6228SArnaud Ebalard 7840b2f6228SArnaud Ebalard ret = _abb5zes3_rtc_battery_low_irq_enable(regmap, false); 7850b2f6228SArnaud Ebalard if (ret) 7860b2f6228SArnaud Ebalard dev_err(dev, "%s: disabling battery low interrupt " 7870b2f6228SArnaud Ebalard "generation failed (%d)\n", __func__, ret); 7880b2f6228SArnaud Ebalard } 7890b2f6228SArnaud Ebalard 7900b2f6228SArnaud Ebalard return ret; 7910b2f6228SArnaud Ebalard } 7920b2f6228SArnaud Ebalard 7930b2f6228SArnaud Ebalard static int abb5zes3_rtc_alarm_irq_enable(struct device *dev, 7940b2f6228SArnaud Ebalard unsigned int enable) 7950b2f6228SArnaud Ebalard { 7960b2f6228SArnaud Ebalard struct abb5zes3_rtc_data *rtc_data = dev_get_drvdata(dev); 7970b2f6228SArnaud Ebalard int ret = 0; 7980b2f6228SArnaud Ebalard 7990b2f6228SArnaud Ebalard if (rtc_data->irq) { 8000b2f6228SArnaud Ebalard mutex_lock(&rtc_data->lock); 801c8a1d8a5SArnaud Ebalard if (rtc_data->timer_alarm) 802c8a1d8a5SArnaud Ebalard ret = _abb5zes3_rtc_update_timer(dev, enable); 803c8a1d8a5SArnaud Ebalard else 8040b2f6228SArnaud Ebalard ret = _abb5zes3_rtc_update_alarm(dev, enable); 8050b2f6228SArnaud Ebalard mutex_unlock(&rtc_data->lock); 8060b2f6228SArnaud Ebalard } 8070b2f6228SArnaud Ebalard 8080b2f6228SArnaud Ebalard return ret; 8090b2f6228SArnaud Ebalard } 8100b2f6228SArnaud Ebalard 8110b2f6228SArnaud Ebalard static irqreturn_t _abb5zes3_rtc_interrupt(int irq, void *data) 8120b2f6228SArnaud Ebalard { 8130b2f6228SArnaud Ebalard struct i2c_client *client = data; 8140b2f6228SArnaud Ebalard struct device *dev = &client->dev; 8150b2f6228SArnaud Ebalard struct abb5zes3_rtc_data *rtc_data = dev_get_drvdata(dev); 8160b2f6228SArnaud Ebalard struct rtc_device *rtc = rtc_data->rtc; 8170b2f6228SArnaud Ebalard u8 regs[ABB5ZES3_CTRL_SEC_LEN]; 8180b2f6228SArnaud Ebalard int ret, handled = IRQ_NONE; 8190b2f6228SArnaud Ebalard 8200b2f6228SArnaud Ebalard ret = regmap_bulk_read(rtc_data->regmap, 0, regs, 8210b2f6228SArnaud Ebalard ABB5ZES3_CTRL_SEC_LEN); 8220b2f6228SArnaud Ebalard if (ret) { 8230b2f6228SArnaud Ebalard dev_err(dev, "%s: unable to read control section (%d)!\n", 8240b2f6228SArnaud Ebalard __func__, ret); 8250b2f6228SArnaud Ebalard return handled; 8260b2f6228SArnaud Ebalard } 8270b2f6228SArnaud Ebalard 8280b2f6228SArnaud Ebalard /* 8290b2f6228SArnaud Ebalard * Check battery low detection flag and disable battery low interrupt 8300b2f6228SArnaud Ebalard * generation if flag is set (interrupt can only be cleared when 8310b2f6228SArnaud Ebalard * battery is replaced). 8320b2f6228SArnaud Ebalard */ 8330b2f6228SArnaud Ebalard if (regs[ABB5ZES3_REG_CTRL3] & ABB5ZES3_REG_CTRL3_BLF) { 8340b2f6228SArnaud Ebalard dev_err(dev, "RTC battery is low; please change it!\n"); 8350b2f6228SArnaud Ebalard 8360b2f6228SArnaud Ebalard _abb5zes3_rtc_battery_low_irq_enable(rtc_data->regmap, false); 8370b2f6228SArnaud Ebalard 8380b2f6228SArnaud Ebalard handled = IRQ_HANDLED; 8390b2f6228SArnaud Ebalard } 8400b2f6228SArnaud Ebalard 8410b2f6228SArnaud Ebalard /* Check alarm flag */ 8420b2f6228SArnaud Ebalard if (regs[ABB5ZES3_REG_CTRL2] & ABB5ZES3_REG_CTRL2_AF) { 8430b2f6228SArnaud Ebalard dev_dbg(dev, "RTC alarm!\n"); 8440b2f6228SArnaud Ebalard 8450b2f6228SArnaud Ebalard rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF); 8460b2f6228SArnaud Ebalard 8470b2f6228SArnaud Ebalard /* Acknowledge and disable the alarm */ 8480b2f6228SArnaud Ebalard _abb5zes3_rtc_clear_alarm(dev); 8490b2f6228SArnaud Ebalard _abb5zes3_rtc_update_alarm(dev, 0); 8500b2f6228SArnaud Ebalard 8510b2f6228SArnaud Ebalard handled = IRQ_HANDLED; 8520b2f6228SArnaud Ebalard } 8530b2f6228SArnaud Ebalard 854c8a1d8a5SArnaud Ebalard /* Check watchdog Timer A flag */ 855c8a1d8a5SArnaud Ebalard if (regs[ABB5ZES3_REG_CTRL2] & ABB5ZES3_REG_CTRL2_WTAF) { 856c8a1d8a5SArnaud Ebalard dev_dbg(dev, "RTC timer!\n"); 857c8a1d8a5SArnaud Ebalard 858c8a1d8a5SArnaud Ebalard rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF); 859c8a1d8a5SArnaud Ebalard 860c8a1d8a5SArnaud Ebalard /* 861c8a1d8a5SArnaud Ebalard * Acknowledge and disable the alarm. Note: WTAF 862c8a1d8a5SArnaud Ebalard * flag had been cleared when reading CTRL2 863c8a1d8a5SArnaud Ebalard */ 864c8a1d8a5SArnaud Ebalard _abb5zes3_rtc_update_timer(dev, 0); 865c8a1d8a5SArnaud Ebalard 866c8a1d8a5SArnaud Ebalard rtc_data->timer_alarm = 0; 867c8a1d8a5SArnaud Ebalard 868c8a1d8a5SArnaud Ebalard handled = IRQ_HANDLED; 869c8a1d8a5SArnaud Ebalard } 870c8a1d8a5SArnaud Ebalard 8710b2f6228SArnaud Ebalard return handled; 8720b2f6228SArnaud Ebalard } 8730b2f6228SArnaud Ebalard 8740b2f6228SArnaud Ebalard static const struct rtc_class_ops rtc_ops = { 8750b2f6228SArnaud Ebalard .read_time = _abb5zes3_rtc_read_time, 8760b2f6228SArnaud Ebalard .set_time = abb5zes3_rtc_set_time, 8770b2f6228SArnaud Ebalard .read_alarm = abb5zes3_rtc_read_alarm, 8780b2f6228SArnaud Ebalard .set_alarm = abb5zes3_rtc_set_alarm, 8790b2f6228SArnaud Ebalard .alarm_irq_enable = abb5zes3_rtc_alarm_irq_enable, 8800b2f6228SArnaud Ebalard }; 8810b2f6228SArnaud Ebalard 882ac2a2726SKrzysztof Kozlowski static const struct regmap_config abb5zes3_rtc_regmap_config = { 8830b2f6228SArnaud Ebalard .reg_bits = 8, 8840b2f6228SArnaud Ebalard .val_bits = 8, 8850b2f6228SArnaud Ebalard }; 8860b2f6228SArnaud Ebalard 8870b2f6228SArnaud Ebalard static int abb5zes3_probe(struct i2c_client *client, 8880b2f6228SArnaud Ebalard const struct i2c_device_id *id) 8890b2f6228SArnaud Ebalard { 8900b2f6228SArnaud Ebalard struct abb5zes3_rtc_data *data = NULL; 8910b2f6228SArnaud Ebalard struct device *dev = &client->dev; 8920b2f6228SArnaud Ebalard struct regmap *regmap; 8930b2f6228SArnaud Ebalard int ret; 8940b2f6228SArnaud Ebalard 8950b2f6228SArnaud Ebalard if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C | 8960b2f6228SArnaud Ebalard I2C_FUNC_SMBUS_BYTE_DATA | 8970b2f6228SArnaud Ebalard I2C_FUNC_SMBUS_I2C_BLOCK)) { 8980b2f6228SArnaud Ebalard ret = -ENODEV; 8990b2f6228SArnaud Ebalard goto err; 9000b2f6228SArnaud Ebalard } 9010b2f6228SArnaud Ebalard 9020b2f6228SArnaud Ebalard regmap = devm_regmap_init_i2c(client, &abb5zes3_rtc_regmap_config); 9030b2f6228SArnaud Ebalard if (IS_ERR(regmap)) { 9040b2f6228SArnaud Ebalard ret = PTR_ERR(regmap); 9050b2f6228SArnaud Ebalard dev_err(dev, "%s: regmap allocation failed: %d\n", 9060b2f6228SArnaud Ebalard __func__, ret); 9070b2f6228SArnaud Ebalard goto err; 9080b2f6228SArnaud Ebalard } 9090b2f6228SArnaud Ebalard 9100b2f6228SArnaud Ebalard ret = abb5zes3_i2c_validate_chip(regmap); 9110b2f6228SArnaud Ebalard if (ret) 9120b2f6228SArnaud Ebalard goto err; 9130b2f6228SArnaud Ebalard 9140b2f6228SArnaud Ebalard data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 9150b2f6228SArnaud Ebalard if (!data) { 9160b2f6228SArnaud Ebalard ret = -ENOMEM; 9170b2f6228SArnaud Ebalard goto err; 9180b2f6228SArnaud Ebalard } 9190b2f6228SArnaud Ebalard 9200b2f6228SArnaud Ebalard mutex_init(&data->lock); 9210b2f6228SArnaud Ebalard data->regmap = regmap; 9220b2f6228SArnaud Ebalard dev_set_drvdata(dev, data); 9230b2f6228SArnaud Ebalard 9240b2f6228SArnaud Ebalard ret = abb5zes3_rtc_check_setup(dev); 9250b2f6228SArnaud Ebalard if (ret) 9260b2f6228SArnaud Ebalard goto err; 9270b2f6228SArnaud Ebalard 928*8bde032bSAlexandre Belloni data->rtc = devm_rtc_allocate_device(dev); 929*8bde032bSAlexandre Belloni ret = PTR_ERR_OR_ZERO(data->rtc); 930*8bde032bSAlexandre Belloni if (ret) { 931*8bde032bSAlexandre Belloni dev_err(dev, "%s: unable to allocate RTC device (%d)\n", 932*8bde032bSAlexandre Belloni __func__, ret); 933*8bde032bSAlexandre Belloni goto err; 934*8bde032bSAlexandre Belloni } 935*8bde032bSAlexandre Belloni 9360b2f6228SArnaud Ebalard if (client->irq > 0) { 9370b2f6228SArnaud Ebalard ret = devm_request_threaded_irq(dev, client->irq, NULL, 9380b2f6228SArnaud Ebalard _abb5zes3_rtc_interrupt, 9390b2f6228SArnaud Ebalard IRQF_SHARED|IRQF_ONESHOT, 9400b2f6228SArnaud Ebalard DRV_NAME, client); 9410b2f6228SArnaud Ebalard if (!ret) { 9420b2f6228SArnaud Ebalard device_init_wakeup(dev, true); 9430b2f6228SArnaud Ebalard data->irq = client->irq; 9440b2f6228SArnaud Ebalard dev_dbg(dev, "%s: irq %d used by RTC\n", __func__, 9450b2f6228SArnaud Ebalard client->irq); 9460b2f6228SArnaud Ebalard } else { 9470b2f6228SArnaud Ebalard dev_err(dev, "%s: irq %d unavailable (%d)\n", 9480b2f6228SArnaud Ebalard __func__, client->irq, ret); 9490b2f6228SArnaud Ebalard goto err; 9500b2f6228SArnaud Ebalard } 9510b2f6228SArnaud Ebalard } 9520b2f6228SArnaud Ebalard 953*8bde032bSAlexandre Belloni data->rtc->ops = &rtc_ops; 9540b2f6228SArnaud Ebalard 9550b2f6228SArnaud Ebalard /* Enable battery low detection interrupt if battery not already low */ 9560b2f6228SArnaud Ebalard if (!data->battery_low && data->irq) { 9570b2f6228SArnaud Ebalard ret = _abb5zes3_rtc_battery_low_irq_enable(regmap, true); 9580b2f6228SArnaud Ebalard if (ret) { 9590b2f6228SArnaud Ebalard dev_err(dev, "%s: enabling battery low interrupt " 9600b2f6228SArnaud Ebalard "generation failed (%d)\n", __func__, ret); 9610b2f6228SArnaud Ebalard goto err; 9620b2f6228SArnaud Ebalard } 9630b2f6228SArnaud Ebalard } 9640b2f6228SArnaud Ebalard 965*8bde032bSAlexandre Belloni ret = rtc_register_device(data->rtc); 966*8bde032bSAlexandre Belloni 9670b2f6228SArnaud Ebalard err: 9680b2f6228SArnaud Ebalard if (ret && data && data->irq) 9690b2f6228SArnaud Ebalard device_init_wakeup(dev, false); 9700b2f6228SArnaud Ebalard return ret; 9710b2f6228SArnaud Ebalard } 9720b2f6228SArnaud Ebalard 9730b2f6228SArnaud Ebalard static int abb5zes3_remove(struct i2c_client *client) 9740b2f6228SArnaud Ebalard { 9750b2f6228SArnaud Ebalard struct abb5zes3_rtc_data *rtc_data = dev_get_drvdata(&client->dev); 9760b2f6228SArnaud Ebalard 9770b2f6228SArnaud Ebalard if (rtc_data->irq > 0) 9780b2f6228SArnaud Ebalard device_init_wakeup(&client->dev, false); 9790b2f6228SArnaud Ebalard 9800b2f6228SArnaud Ebalard return 0; 9810b2f6228SArnaud Ebalard } 9820b2f6228SArnaud Ebalard 9830b2f6228SArnaud Ebalard #ifdef CONFIG_PM_SLEEP 9840b2f6228SArnaud Ebalard static int abb5zes3_rtc_suspend(struct device *dev) 9850b2f6228SArnaud Ebalard { 9860b2f6228SArnaud Ebalard struct abb5zes3_rtc_data *rtc_data = dev_get_drvdata(dev); 9870b2f6228SArnaud Ebalard 9880b2f6228SArnaud Ebalard if (device_may_wakeup(dev)) 9890b2f6228SArnaud Ebalard return enable_irq_wake(rtc_data->irq); 9900b2f6228SArnaud Ebalard 9910b2f6228SArnaud Ebalard return 0; 9920b2f6228SArnaud Ebalard } 9930b2f6228SArnaud Ebalard 9940b2f6228SArnaud Ebalard static int abb5zes3_rtc_resume(struct device *dev) 9950b2f6228SArnaud Ebalard { 9960b2f6228SArnaud Ebalard struct abb5zes3_rtc_data *rtc_data = dev_get_drvdata(dev); 9970b2f6228SArnaud Ebalard 9980b2f6228SArnaud Ebalard if (device_may_wakeup(dev)) 9990b2f6228SArnaud Ebalard return disable_irq_wake(rtc_data->irq); 10000b2f6228SArnaud Ebalard 10010b2f6228SArnaud Ebalard return 0; 10020b2f6228SArnaud Ebalard } 10030b2f6228SArnaud Ebalard #endif 10040b2f6228SArnaud Ebalard 10050b2f6228SArnaud Ebalard static SIMPLE_DEV_PM_OPS(abb5zes3_rtc_pm_ops, abb5zes3_rtc_suspend, 10060b2f6228SArnaud Ebalard abb5zes3_rtc_resume); 10070b2f6228SArnaud Ebalard 10080b2f6228SArnaud Ebalard #ifdef CONFIG_OF 10090b2f6228SArnaud Ebalard static const struct of_device_id abb5zes3_dt_match[] = { 10100b2f6228SArnaud Ebalard { .compatible = "abracon,abb5zes3" }, 10110b2f6228SArnaud Ebalard { }, 10120b2f6228SArnaud Ebalard }; 10131c4fc295SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, abb5zes3_dt_match); 10140b2f6228SArnaud Ebalard #endif 10150b2f6228SArnaud Ebalard 10160b2f6228SArnaud Ebalard static const struct i2c_device_id abb5zes3_id[] = { 10170b2f6228SArnaud Ebalard { "abb5zes3", 0 }, 10180b2f6228SArnaud Ebalard { } 10190b2f6228SArnaud Ebalard }; 10200b2f6228SArnaud Ebalard MODULE_DEVICE_TABLE(i2c, abb5zes3_id); 10210b2f6228SArnaud Ebalard 10220b2f6228SArnaud Ebalard static struct i2c_driver abb5zes3_driver = { 10230b2f6228SArnaud Ebalard .driver = { 10240b2f6228SArnaud Ebalard .name = DRV_NAME, 10250b2f6228SArnaud Ebalard .pm = &abb5zes3_rtc_pm_ops, 10260b2f6228SArnaud Ebalard .of_match_table = of_match_ptr(abb5zes3_dt_match), 10270b2f6228SArnaud Ebalard }, 10280b2f6228SArnaud Ebalard .probe = abb5zes3_probe, 10290b2f6228SArnaud Ebalard .remove = abb5zes3_remove, 10300b2f6228SArnaud Ebalard .id_table = abb5zes3_id, 10310b2f6228SArnaud Ebalard }; 10320b2f6228SArnaud Ebalard module_i2c_driver(abb5zes3_driver); 10330b2f6228SArnaud Ebalard 10340b2f6228SArnaud Ebalard MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>"); 10350b2f6228SArnaud Ebalard MODULE_DESCRIPTION("Abracon AB-RTCMC-32.768kHz-B5ZE-S3 RTC/Alarm driver"); 10360b2f6228SArnaud Ebalard MODULE_LICENSE("GPL"); 1037