1b07682b6SSantosh Shilimkar /* 2ef3b7d0dSBalaji T K * rtc-twl.c -- TWL Real Time Clock interface 3b07682b6SSantosh Shilimkar * 4b07682b6SSantosh Shilimkar * Copyright (C) 2007 MontaVista Software, Inc 5b07682b6SSantosh Shilimkar * Author: Alexandre Rusev <source@mvista.com> 6b07682b6SSantosh Shilimkar * 7b07682b6SSantosh Shilimkar * Based on original TI driver twl4030-rtc.c 8b07682b6SSantosh Shilimkar * Copyright (C) 2006 Texas Instruments, Inc. 9b07682b6SSantosh Shilimkar * 10b07682b6SSantosh Shilimkar * Based on rtc-omap.c 11b07682b6SSantosh Shilimkar * Copyright (C) 2003 MontaVista Software, Inc. 12b07682b6SSantosh Shilimkar * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com> 13b07682b6SSantosh Shilimkar * Copyright (C) 2006 David Brownell 14b07682b6SSantosh Shilimkar * 15b07682b6SSantosh Shilimkar * This program is free software; you can redistribute it and/or 16b07682b6SSantosh Shilimkar * modify it under the terms of the GNU General Public License 17b07682b6SSantosh Shilimkar * as published by the Free Software Foundation; either version 18b07682b6SSantosh Shilimkar * 2 of the License, or (at your option) any later version. 19b07682b6SSantosh Shilimkar */ 20b07682b6SSantosh Shilimkar 21b07682b6SSantosh Shilimkar #include <linux/kernel.h> 22b07682b6SSantosh Shilimkar #include <linux/errno.h> 23b07682b6SSantosh Shilimkar #include <linux/init.h> 24b07682b6SSantosh Shilimkar #include <linux/module.h> 25b07682b6SSantosh Shilimkar #include <linux/types.h> 26b07682b6SSantosh Shilimkar #include <linux/rtc.h> 27b07682b6SSantosh Shilimkar #include <linux/bcd.h> 28b07682b6SSantosh Shilimkar #include <linux/platform_device.h> 29b07682b6SSantosh Shilimkar #include <linux/interrupt.h> 30c8a6046eSSachin Kamat #include <linux/of.h> 31b07682b6SSantosh Shilimkar 32b07682b6SSantosh Shilimkar #include <linux/i2c/twl.h> 33b07682b6SSantosh Shilimkar 34b07682b6SSantosh Shilimkar 35b07682b6SSantosh Shilimkar /* 36b07682b6SSantosh Shilimkar * RTC block register offsets (use TWL_MODULE_RTC) 37b07682b6SSantosh Shilimkar */ 38a6b49ffdSBalaji T K enum { 39a6b49ffdSBalaji T K REG_SECONDS_REG = 0, 40a6b49ffdSBalaji T K REG_MINUTES_REG, 41a6b49ffdSBalaji T K REG_HOURS_REG, 42a6b49ffdSBalaji T K REG_DAYS_REG, 43a6b49ffdSBalaji T K REG_MONTHS_REG, 44a6b49ffdSBalaji T K REG_YEARS_REG, 45a6b49ffdSBalaji T K REG_WEEKS_REG, 46b07682b6SSantosh Shilimkar 47a6b49ffdSBalaji T K REG_ALARM_SECONDS_REG, 48a6b49ffdSBalaji T K REG_ALARM_MINUTES_REG, 49a6b49ffdSBalaji T K REG_ALARM_HOURS_REG, 50a6b49ffdSBalaji T K REG_ALARM_DAYS_REG, 51a6b49ffdSBalaji T K REG_ALARM_MONTHS_REG, 52a6b49ffdSBalaji T K REG_ALARM_YEARS_REG, 53b07682b6SSantosh Shilimkar 54a6b49ffdSBalaji T K REG_RTC_CTRL_REG, 55a6b49ffdSBalaji T K REG_RTC_STATUS_REG, 56a6b49ffdSBalaji T K REG_RTC_INTERRUPTS_REG, 57b07682b6SSantosh Shilimkar 58a6b49ffdSBalaji T K REG_RTC_COMP_LSB_REG, 59a6b49ffdSBalaji T K REG_RTC_COMP_MSB_REG, 60a6b49ffdSBalaji T K }; 612e84067bSTobias Klauser static const u8 twl4030_rtc_reg_map[] = { 62a6b49ffdSBalaji T K [REG_SECONDS_REG] = 0x00, 63a6b49ffdSBalaji T K [REG_MINUTES_REG] = 0x01, 64a6b49ffdSBalaji T K [REG_HOURS_REG] = 0x02, 65a6b49ffdSBalaji T K [REG_DAYS_REG] = 0x03, 66a6b49ffdSBalaji T K [REG_MONTHS_REG] = 0x04, 67a6b49ffdSBalaji T K [REG_YEARS_REG] = 0x05, 68a6b49ffdSBalaji T K [REG_WEEKS_REG] = 0x06, 69a6b49ffdSBalaji T K 70a6b49ffdSBalaji T K [REG_ALARM_SECONDS_REG] = 0x07, 71a6b49ffdSBalaji T K [REG_ALARM_MINUTES_REG] = 0x08, 72a6b49ffdSBalaji T K [REG_ALARM_HOURS_REG] = 0x09, 73a6b49ffdSBalaji T K [REG_ALARM_DAYS_REG] = 0x0A, 74a6b49ffdSBalaji T K [REG_ALARM_MONTHS_REG] = 0x0B, 75a6b49ffdSBalaji T K [REG_ALARM_YEARS_REG] = 0x0C, 76a6b49ffdSBalaji T K 77a6b49ffdSBalaji T K [REG_RTC_CTRL_REG] = 0x0D, 78a6b49ffdSBalaji T K [REG_RTC_STATUS_REG] = 0x0E, 79a6b49ffdSBalaji T K [REG_RTC_INTERRUPTS_REG] = 0x0F, 80a6b49ffdSBalaji T K 81a6b49ffdSBalaji T K [REG_RTC_COMP_LSB_REG] = 0x10, 82a6b49ffdSBalaji T K [REG_RTC_COMP_MSB_REG] = 0x11, 83a6b49ffdSBalaji T K }; 842e84067bSTobias Klauser static const u8 twl6030_rtc_reg_map[] = { 85a6b49ffdSBalaji T K [REG_SECONDS_REG] = 0x00, 86a6b49ffdSBalaji T K [REG_MINUTES_REG] = 0x01, 87a6b49ffdSBalaji T K [REG_HOURS_REG] = 0x02, 88a6b49ffdSBalaji T K [REG_DAYS_REG] = 0x03, 89a6b49ffdSBalaji T K [REG_MONTHS_REG] = 0x04, 90a6b49ffdSBalaji T K [REG_YEARS_REG] = 0x05, 91a6b49ffdSBalaji T K [REG_WEEKS_REG] = 0x06, 92a6b49ffdSBalaji T K 93a6b49ffdSBalaji T K [REG_ALARM_SECONDS_REG] = 0x08, 94a6b49ffdSBalaji T K [REG_ALARM_MINUTES_REG] = 0x09, 95a6b49ffdSBalaji T K [REG_ALARM_HOURS_REG] = 0x0A, 96a6b49ffdSBalaji T K [REG_ALARM_DAYS_REG] = 0x0B, 97a6b49ffdSBalaji T K [REG_ALARM_MONTHS_REG] = 0x0C, 98a6b49ffdSBalaji T K [REG_ALARM_YEARS_REG] = 0x0D, 99a6b49ffdSBalaji T K 100a6b49ffdSBalaji T K [REG_RTC_CTRL_REG] = 0x10, 101a6b49ffdSBalaji T K [REG_RTC_STATUS_REG] = 0x11, 102a6b49ffdSBalaji T K [REG_RTC_INTERRUPTS_REG] = 0x12, 103a6b49ffdSBalaji T K 104a6b49ffdSBalaji T K [REG_RTC_COMP_LSB_REG] = 0x13, 105a6b49ffdSBalaji T K [REG_RTC_COMP_MSB_REG] = 0x14, 106a6b49ffdSBalaji T K }; 107b07682b6SSantosh Shilimkar 108b07682b6SSantosh Shilimkar /* RTC_CTRL_REG bitfields */ 109b07682b6SSantosh Shilimkar #define BIT_RTC_CTRL_REG_STOP_RTC_M 0x01 110b07682b6SSantosh Shilimkar #define BIT_RTC_CTRL_REG_ROUND_30S_M 0x02 111b07682b6SSantosh Shilimkar #define BIT_RTC_CTRL_REG_AUTO_COMP_M 0x04 112b07682b6SSantosh Shilimkar #define BIT_RTC_CTRL_REG_MODE_12_24_M 0x08 113b07682b6SSantosh Shilimkar #define BIT_RTC_CTRL_REG_TEST_MODE_M 0x10 114b07682b6SSantosh Shilimkar #define BIT_RTC_CTRL_REG_SET_32_COUNTER_M 0x20 115b07682b6SSantosh Shilimkar #define BIT_RTC_CTRL_REG_GET_TIME_M 0x40 116f3ec434cSKonstantin Shlyakhovoy #define BIT_RTC_CTRL_REG_RTC_V_OPT 0x80 117b07682b6SSantosh Shilimkar 118b07682b6SSantosh Shilimkar /* RTC_STATUS_REG bitfields */ 119b07682b6SSantosh Shilimkar #define BIT_RTC_STATUS_REG_RUN_M 0x02 120b07682b6SSantosh Shilimkar #define BIT_RTC_STATUS_REG_1S_EVENT_M 0x04 121b07682b6SSantosh Shilimkar #define BIT_RTC_STATUS_REG_1M_EVENT_M 0x08 122b07682b6SSantosh Shilimkar #define BIT_RTC_STATUS_REG_1H_EVENT_M 0x10 123b07682b6SSantosh Shilimkar #define BIT_RTC_STATUS_REG_1D_EVENT_M 0x20 124b07682b6SSantosh Shilimkar #define BIT_RTC_STATUS_REG_ALARM_M 0x40 125b07682b6SSantosh Shilimkar #define BIT_RTC_STATUS_REG_POWER_UP_M 0x80 126b07682b6SSantosh Shilimkar 127b07682b6SSantosh Shilimkar /* RTC_INTERRUPTS_REG bitfields */ 128b07682b6SSantosh Shilimkar #define BIT_RTC_INTERRUPTS_REG_EVERY_M 0x03 129b07682b6SSantosh Shilimkar #define BIT_RTC_INTERRUPTS_REG_IT_TIMER_M 0x04 130b07682b6SSantosh Shilimkar #define BIT_RTC_INTERRUPTS_REG_IT_ALARM_M 0x08 131b07682b6SSantosh Shilimkar 132b07682b6SSantosh Shilimkar 133b07682b6SSantosh Shilimkar /* REG_SECONDS_REG through REG_YEARS_REG is how many registers? */ 134b07682b6SSantosh Shilimkar #define ALL_TIME_REGS 6 135b07682b6SSantosh Shilimkar 136b07682b6SSantosh Shilimkar /*----------------------------------------------------------------------*/ 137a6b49ffdSBalaji T K static u8 *rtc_reg_map; 138b07682b6SSantosh Shilimkar 139b07682b6SSantosh Shilimkar /* 140ef3b7d0dSBalaji T K * Supports 1 byte read from TWL RTC register. 141b07682b6SSantosh Shilimkar */ 142ef3b7d0dSBalaji T K static int twl_rtc_read_u8(u8 *data, u8 reg) 143b07682b6SSantosh Shilimkar { 144b07682b6SSantosh Shilimkar int ret; 145b07682b6SSantosh Shilimkar 146a6b49ffdSBalaji T K ret = twl_i2c_read_u8(TWL_MODULE_RTC, data, (rtc_reg_map[reg])); 147b07682b6SSantosh Shilimkar if (ret < 0) 148ef3b7d0dSBalaji T K pr_err("twl_rtc: Could not read TWL" 149b07682b6SSantosh Shilimkar "register %X - error %d\n", reg, ret); 150b07682b6SSantosh Shilimkar return ret; 151b07682b6SSantosh Shilimkar } 152b07682b6SSantosh Shilimkar 153b07682b6SSantosh Shilimkar /* 154ef3b7d0dSBalaji T K * Supports 1 byte write to TWL RTC registers. 155b07682b6SSantosh Shilimkar */ 156ef3b7d0dSBalaji T K static int twl_rtc_write_u8(u8 data, u8 reg) 157b07682b6SSantosh Shilimkar { 158b07682b6SSantosh Shilimkar int ret; 159b07682b6SSantosh Shilimkar 160a6b49ffdSBalaji T K ret = twl_i2c_write_u8(TWL_MODULE_RTC, data, (rtc_reg_map[reg])); 161b07682b6SSantosh Shilimkar if (ret < 0) 162ef3b7d0dSBalaji T K pr_err("twl_rtc: Could not write TWL" 163b07682b6SSantosh Shilimkar "register %X - error %d\n", reg, ret); 164b07682b6SSantosh Shilimkar return ret; 165b07682b6SSantosh Shilimkar } 166b07682b6SSantosh Shilimkar 167b07682b6SSantosh Shilimkar /* 168b07682b6SSantosh Shilimkar * Cache the value for timer/alarm interrupts register; this is 169b07682b6SSantosh Shilimkar * only changed by callers holding rtc ops lock (or resume). 170b07682b6SSantosh Shilimkar */ 171b07682b6SSantosh Shilimkar static unsigned char rtc_irq_bits; 172b07682b6SSantosh Shilimkar 173b07682b6SSantosh Shilimkar /* 174b07682b6SSantosh Shilimkar * Enable 1/second update and/or alarm interrupts. 175b07682b6SSantosh Shilimkar */ 176b07682b6SSantosh Shilimkar static int set_rtc_irq_bit(unsigned char bit) 177b07682b6SSantosh Shilimkar { 178b07682b6SSantosh Shilimkar unsigned char val; 179b07682b6SSantosh Shilimkar int ret; 180b07682b6SSantosh Shilimkar 181ce9f6506SVenu Byravarasu /* if the bit is set, return from here */ 182ce9f6506SVenu Byravarasu if (rtc_irq_bits & bit) 183ce9f6506SVenu Byravarasu return 0; 184ce9f6506SVenu Byravarasu 185b07682b6SSantosh Shilimkar val = rtc_irq_bits | bit; 186b07682b6SSantosh Shilimkar val &= ~BIT_RTC_INTERRUPTS_REG_EVERY_M; 187ef3b7d0dSBalaji T K ret = twl_rtc_write_u8(val, REG_RTC_INTERRUPTS_REG); 188b07682b6SSantosh Shilimkar if (ret == 0) 189b07682b6SSantosh Shilimkar rtc_irq_bits = val; 190b07682b6SSantosh Shilimkar 191b07682b6SSantosh Shilimkar return ret; 192b07682b6SSantosh Shilimkar } 193b07682b6SSantosh Shilimkar 194b07682b6SSantosh Shilimkar /* 195b07682b6SSantosh Shilimkar * Disable update and/or alarm interrupts. 196b07682b6SSantosh Shilimkar */ 197b07682b6SSantosh Shilimkar static int mask_rtc_irq_bit(unsigned char bit) 198b07682b6SSantosh Shilimkar { 199b07682b6SSantosh Shilimkar unsigned char val; 200b07682b6SSantosh Shilimkar int ret; 201b07682b6SSantosh Shilimkar 202ce9f6506SVenu Byravarasu /* if the bit is clear, return from here */ 203ce9f6506SVenu Byravarasu if (!(rtc_irq_bits & bit)) 204ce9f6506SVenu Byravarasu return 0; 205ce9f6506SVenu Byravarasu 206b07682b6SSantosh Shilimkar val = rtc_irq_bits & ~bit; 207ef3b7d0dSBalaji T K ret = twl_rtc_write_u8(val, REG_RTC_INTERRUPTS_REG); 208b07682b6SSantosh Shilimkar if (ret == 0) 209b07682b6SSantosh Shilimkar rtc_irq_bits = val; 210b07682b6SSantosh Shilimkar 211b07682b6SSantosh Shilimkar return ret; 212b07682b6SSantosh Shilimkar } 213b07682b6SSantosh Shilimkar 214ef3b7d0dSBalaji T K static int twl_rtc_alarm_irq_enable(struct device *dev, unsigned enabled) 215b07682b6SSantosh Shilimkar { 216ae845894SKevin Hilman struct platform_device *pdev = to_platform_device(dev); 217ae845894SKevin Hilman int irq = platform_get_irq(pdev, 0); 218ae845894SKevin Hilman static bool twl_rtc_wake_enabled; 219b07682b6SSantosh Shilimkar int ret; 220b07682b6SSantosh Shilimkar 221ae845894SKevin Hilman if (enabled) { 222b07682b6SSantosh Shilimkar ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M); 223ae845894SKevin Hilman if (device_can_wakeup(dev) && !twl_rtc_wake_enabled) { 224ae845894SKevin Hilman enable_irq_wake(irq); 225ae845894SKevin Hilman twl_rtc_wake_enabled = true; 226ae845894SKevin Hilman } 227ae845894SKevin Hilman } else { 228b07682b6SSantosh Shilimkar ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M); 229ae845894SKevin Hilman if (twl_rtc_wake_enabled) { 230ae845894SKevin Hilman disable_irq_wake(irq); 231ae845894SKevin Hilman twl_rtc_wake_enabled = false; 232ae845894SKevin Hilman } 233ae845894SKevin Hilman } 234b07682b6SSantosh Shilimkar 235b07682b6SSantosh Shilimkar return ret; 236b07682b6SSantosh Shilimkar } 237b07682b6SSantosh Shilimkar 238b07682b6SSantosh Shilimkar /* 239ef3b7d0dSBalaji T K * Gets current TWL RTC time and date parameters. 240b07682b6SSantosh Shilimkar * 241b07682b6SSantosh Shilimkar * The RTC's time/alarm representation is not what gmtime(3) requires 242b07682b6SSantosh Shilimkar * Linux to use: 243b07682b6SSantosh Shilimkar * 244b07682b6SSantosh Shilimkar * - Months are 1..12 vs Linux 0-11 245b07682b6SSantosh Shilimkar * - Years are 0..99 vs Linux 1900..N (we assume 21st century) 246b07682b6SSantosh Shilimkar */ 247ef3b7d0dSBalaji T K static int twl_rtc_read_time(struct device *dev, struct rtc_time *tm) 248b07682b6SSantosh Shilimkar { 24914591d88SPeter Ujfalusi unsigned char rtc_data[ALL_TIME_REGS]; 250b07682b6SSantosh Shilimkar int ret; 251b07682b6SSantosh Shilimkar u8 save_control; 252f3ec434cSKonstantin Shlyakhovoy u8 rtc_control; 253b07682b6SSantosh Shilimkar 254ef3b7d0dSBalaji T K ret = twl_rtc_read_u8(&save_control, REG_RTC_CTRL_REG); 255f3ec434cSKonstantin Shlyakhovoy if (ret < 0) { 256f3ec434cSKonstantin Shlyakhovoy dev_err(dev, "%s: reading CTRL_REG, error %d\n", __func__, ret); 257b07682b6SSantosh Shilimkar return ret; 258f3ec434cSKonstantin Shlyakhovoy } 259f3ec434cSKonstantin Shlyakhovoy /* for twl6030/32 make sure BIT_RTC_CTRL_REG_GET_TIME_M is clear */ 260f3ec434cSKonstantin Shlyakhovoy if (twl_class_is_6030()) { 261f3ec434cSKonstantin Shlyakhovoy if (save_control & BIT_RTC_CTRL_REG_GET_TIME_M) { 262f3ec434cSKonstantin Shlyakhovoy save_control &= ~BIT_RTC_CTRL_REG_GET_TIME_M; 263ef3b7d0dSBalaji T K ret = twl_rtc_write_u8(save_control, REG_RTC_CTRL_REG); 264f3ec434cSKonstantin Shlyakhovoy if (ret < 0) { 265f3ec434cSKonstantin Shlyakhovoy dev_err(dev, "%s clr GET_TIME, error %d\n", 266f3ec434cSKonstantin Shlyakhovoy __func__, ret); 267b07682b6SSantosh Shilimkar return ret; 268f3ec434cSKonstantin Shlyakhovoy } 269f3ec434cSKonstantin Shlyakhovoy } 270f3ec434cSKonstantin Shlyakhovoy } 271f3ec434cSKonstantin Shlyakhovoy 272f3ec434cSKonstantin Shlyakhovoy /* Copy RTC counting registers to static registers or latches */ 273f3ec434cSKonstantin Shlyakhovoy rtc_control = save_control | BIT_RTC_CTRL_REG_GET_TIME_M; 274f3ec434cSKonstantin Shlyakhovoy 275f3ec434cSKonstantin Shlyakhovoy /* for twl6030/32 enable read access to static shadowed registers */ 276f3ec434cSKonstantin Shlyakhovoy if (twl_class_is_6030()) 277f3ec434cSKonstantin Shlyakhovoy rtc_control |= BIT_RTC_CTRL_REG_RTC_V_OPT; 278f3ec434cSKonstantin Shlyakhovoy 279f3ec434cSKonstantin Shlyakhovoy ret = twl_rtc_write_u8(rtc_control, REG_RTC_CTRL_REG); 280f3ec434cSKonstantin Shlyakhovoy if (ret < 0) { 281f3ec434cSKonstantin Shlyakhovoy dev_err(dev, "%s: writing CTRL_REG, error %d\n", __func__, ret); 282f3ec434cSKonstantin Shlyakhovoy return ret; 283f3ec434cSKonstantin Shlyakhovoy } 284b07682b6SSantosh Shilimkar 285ef3b7d0dSBalaji T K ret = twl_i2c_read(TWL_MODULE_RTC, rtc_data, 286a6b49ffdSBalaji T K (rtc_reg_map[REG_SECONDS_REG]), ALL_TIME_REGS); 287b07682b6SSantosh Shilimkar 288b07682b6SSantosh Shilimkar if (ret < 0) { 289f3ec434cSKonstantin Shlyakhovoy dev_err(dev, "%s: reading data, error %d\n", __func__, ret); 290b07682b6SSantosh Shilimkar return ret; 291b07682b6SSantosh Shilimkar } 292b07682b6SSantosh Shilimkar 293f3ec434cSKonstantin Shlyakhovoy /* for twl6030 restore original state of rtc control register */ 294f3ec434cSKonstantin Shlyakhovoy if (twl_class_is_6030()) { 295f3ec434cSKonstantin Shlyakhovoy ret = twl_rtc_write_u8(save_control, REG_RTC_CTRL_REG); 296f3ec434cSKonstantin Shlyakhovoy if (ret < 0) { 297f3ec434cSKonstantin Shlyakhovoy dev_err(dev, "%s: restore CTRL_REG, error %d\n", 298f3ec434cSKonstantin Shlyakhovoy __func__, ret); 299f3ec434cSKonstantin Shlyakhovoy return ret; 300f3ec434cSKonstantin Shlyakhovoy } 301f3ec434cSKonstantin Shlyakhovoy } 302f3ec434cSKonstantin Shlyakhovoy 303b07682b6SSantosh Shilimkar tm->tm_sec = bcd2bin(rtc_data[0]); 304b07682b6SSantosh Shilimkar tm->tm_min = bcd2bin(rtc_data[1]); 305b07682b6SSantosh Shilimkar tm->tm_hour = bcd2bin(rtc_data[2]); 306b07682b6SSantosh Shilimkar tm->tm_mday = bcd2bin(rtc_data[3]); 307b07682b6SSantosh Shilimkar tm->tm_mon = bcd2bin(rtc_data[4]) - 1; 308b07682b6SSantosh Shilimkar tm->tm_year = bcd2bin(rtc_data[5]) + 100; 309b07682b6SSantosh Shilimkar 310b07682b6SSantosh Shilimkar return ret; 311b07682b6SSantosh Shilimkar } 312b07682b6SSantosh Shilimkar 313ef3b7d0dSBalaji T K static int twl_rtc_set_time(struct device *dev, struct rtc_time *tm) 314b07682b6SSantosh Shilimkar { 315b07682b6SSantosh Shilimkar unsigned char save_control; 31614591d88SPeter Ujfalusi unsigned char rtc_data[ALL_TIME_REGS]; 317b07682b6SSantosh Shilimkar int ret; 318b07682b6SSantosh Shilimkar 31914591d88SPeter Ujfalusi rtc_data[0] = bin2bcd(tm->tm_sec); 32014591d88SPeter Ujfalusi rtc_data[1] = bin2bcd(tm->tm_min); 32114591d88SPeter Ujfalusi rtc_data[2] = bin2bcd(tm->tm_hour); 32214591d88SPeter Ujfalusi rtc_data[3] = bin2bcd(tm->tm_mday); 32314591d88SPeter Ujfalusi rtc_data[4] = bin2bcd(tm->tm_mon + 1); 32414591d88SPeter Ujfalusi rtc_data[5] = bin2bcd(tm->tm_year - 100); 325b07682b6SSantosh Shilimkar 326b07682b6SSantosh Shilimkar /* Stop RTC while updating the TC registers */ 327ef3b7d0dSBalaji T K ret = twl_rtc_read_u8(&save_control, REG_RTC_CTRL_REG); 328b07682b6SSantosh Shilimkar if (ret < 0) 329b07682b6SSantosh Shilimkar goto out; 330b07682b6SSantosh Shilimkar 331b07682b6SSantosh Shilimkar save_control &= ~BIT_RTC_CTRL_REG_STOP_RTC_M; 3328f6b0dd3SJesper Juhl ret = twl_rtc_write_u8(save_control, REG_RTC_CTRL_REG); 333b07682b6SSantosh Shilimkar if (ret < 0) 334b07682b6SSantosh Shilimkar goto out; 335b07682b6SSantosh Shilimkar 336b07682b6SSantosh Shilimkar /* update all the time registers in one shot */ 337ef3b7d0dSBalaji T K ret = twl_i2c_write(TWL_MODULE_RTC, rtc_data, 338a6b49ffdSBalaji T K (rtc_reg_map[REG_SECONDS_REG]), ALL_TIME_REGS); 339b07682b6SSantosh Shilimkar if (ret < 0) { 340b07682b6SSantosh Shilimkar dev_err(dev, "rtc_set_time error %d\n", ret); 341b07682b6SSantosh Shilimkar goto out; 342b07682b6SSantosh Shilimkar } 343b07682b6SSantosh Shilimkar 344b07682b6SSantosh Shilimkar /* Start back RTC */ 345b07682b6SSantosh Shilimkar save_control |= BIT_RTC_CTRL_REG_STOP_RTC_M; 346ef3b7d0dSBalaji T K ret = twl_rtc_write_u8(save_control, REG_RTC_CTRL_REG); 347b07682b6SSantosh Shilimkar 348b07682b6SSantosh Shilimkar out: 349b07682b6SSantosh Shilimkar return ret; 350b07682b6SSantosh Shilimkar } 351b07682b6SSantosh Shilimkar 352b07682b6SSantosh Shilimkar /* 353ef3b7d0dSBalaji T K * Gets current TWL RTC alarm time. 354b07682b6SSantosh Shilimkar */ 355ef3b7d0dSBalaji T K static int twl_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) 356b07682b6SSantosh Shilimkar { 35714591d88SPeter Ujfalusi unsigned char rtc_data[ALL_TIME_REGS]; 358b07682b6SSantosh Shilimkar int ret; 359b07682b6SSantosh Shilimkar 360ef3b7d0dSBalaji T K ret = twl_i2c_read(TWL_MODULE_RTC, rtc_data, 361a6b49ffdSBalaji T K (rtc_reg_map[REG_ALARM_SECONDS_REG]), ALL_TIME_REGS); 362b07682b6SSantosh Shilimkar if (ret < 0) { 363b07682b6SSantosh Shilimkar dev_err(dev, "rtc_read_alarm error %d\n", ret); 364b07682b6SSantosh Shilimkar return ret; 365b07682b6SSantosh Shilimkar } 366b07682b6SSantosh Shilimkar 367b07682b6SSantosh Shilimkar /* some of these fields may be wildcard/"match all" */ 368b07682b6SSantosh Shilimkar alm->time.tm_sec = bcd2bin(rtc_data[0]); 369b07682b6SSantosh Shilimkar alm->time.tm_min = bcd2bin(rtc_data[1]); 370b07682b6SSantosh Shilimkar alm->time.tm_hour = bcd2bin(rtc_data[2]); 371b07682b6SSantosh Shilimkar alm->time.tm_mday = bcd2bin(rtc_data[3]); 372b07682b6SSantosh Shilimkar alm->time.tm_mon = bcd2bin(rtc_data[4]) - 1; 373b07682b6SSantosh Shilimkar alm->time.tm_year = bcd2bin(rtc_data[5]) + 100; 374b07682b6SSantosh Shilimkar 375b07682b6SSantosh Shilimkar /* report cached alarm enable state */ 376b07682b6SSantosh Shilimkar if (rtc_irq_bits & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M) 377b07682b6SSantosh Shilimkar alm->enabled = 1; 378b07682b6SSantosh Shilimkar 379b07682b6SSantosh Shilimkar return ret; 380b07682b6SSantosh Shilimkar } 381b07682b6SSantosh Shilimkar 382ef3b7d0dSBalaji T K static int twl_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) 383b07682b6SSantosh Shilimkar { 38414591d88SPeter Ujfalusi unsigned char alarm_data[ALL_TIME_REGS]; 385b07682b6SSantosh Shilimkar int ret; 386b07682b6SSantosh Shilimkar 387ef3b7d0dSBalaji T K ret = twl_rtc_alarm_irq_enable(dev, 0); 388b07682b6SSantosh Shilimkar if (ret) 389b07682b6SSantosh Shilimkar goto out; 390b07682b6SSantosh Shilimkar 39114591d88SPeter Ujfalusi alarm_data[0] = bin2bcd(alm->time.tm_sec); 39214591d88SPeter Ujfalusi alarm_data[1] = bin2bcd(alm->time.tm_min); 39314591d88SPeter Ujfalusi alarm_data[2] = bin2bcd(alm->time.tm_hour); 39414591d88SPeter Ujfalusi alarm_data[3] = bin2bcd(alm->time.tm_mday); 39514591d88SPeter Ujfalusi alarm_data[4] = bin2bcd(alm->time.tm_mon + 1); 39614591d88SPeter Ujfalusi alarm_data[5] = bin2bcd(alm->time.tm_year - 100); 397b07682b6SSantosh Shilimkar 398b07682b6SSantosh Shilimkar /* update all the alarm registers in one shot */ 399ef3b7d0dSBalaji T K ret = twl_i2c_write(TWL_MODULE_RTC, alarm_data, 400a6b49ffdSBalaji T K (rtc_reg_map[REG_ALARM_SECONDS_REG]), ALL_TIME_REGS); 401b07682b6SSantosh Shilimkar if (ret) { 402b07682b6SSantosh Shilimkar dev_err(dev, "rtc_set_alarm error %d\n", ret); 403b07682b6SSantosh Shilimkar goto out; 404b07682b6SSantosh Shilimkar } 405b07682b6SSantosh Shilimkar 406b07682b6SSantosh Shilimkar if (alm->enabled) 407ef3b7d0dSBalaji T K ret = twl_rtc_alarm_irq_enable(dev, 1); 408b07682b6SSantosh Shilimkar out: 409b07682b6SSantosh Shilimkar return ret; 410b07682b6SSantosh Shilimkar } 411b07682b6SSantosh Shilimkar 412ef3b7d0dSBalaji T K static irqreturn_t twl_rtc_interrupt(int irq, void *rtc) 413b07682b6SSantosh Shilimkar { 4142778ebccSVenu Byravarasu unsigned long events; 415b07682b6SSantosh Shilimkar int ret = IRQ_NONE; 416b07682b6SSantosh Shilimkar int res; 417b07682b6SSantosh Shilimkar u8 rd_reg; 418b07682b6SSantosh Shilimkar 419ef3b7d0dSBalaji T K res = twl_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG); 420b07682b6SSantosh Shilimkar if (res) 421b07682b6SSantosh Shilimkar goto out; 422b07682b6SSantosh Shilimkar /* 423b07682b6SSantosh Shilimkar * Figure out source of interrupt: ALARM or TIMER in RTC_STATUS_REG. 424b07682b6SSantosh Shilimkar * only one (ALARM or RTC) interrupt source may be enabled 425b07682b6SSantosh Shilimkar * at time, we also could check our results 426b07682b6SSantosh Shilimkar * by reading RTS_INTERRUPTS_REGISTER[IT_TIMER,IT_ALARM] 427b07682b6SSantosh Shilimkar */ 428b07682b6SSantosh Shilimkar if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M) 4292778ebccSVenu Byravarasu events = RTC_IRQF | RTC_AF; 430b07682b6SSantosh Shilimkar else 4312778ebccSVenu Byravarasu events = RTC_IRQF | RTC_PF; 432b07682b6SSantosh Shilimkar 43394a339d0SVenu Byravarasu res = twl_rtc_write_u8(BIT_RTC_STATUS_REG_ALARM_M, 434b07682b6SSantosh Shilimkar REG_RTC_STATUS_REG); 435b07682b6SSantosh Shilimkar if (res) 436b07682b6SSantosh Shilimkar goto out; 437b07682b6SSantosh Shilimkar 438a6b49ffdSBalaji T K if (twl_class_is_4030()) { 439b07682b6SSantosh Shilimkar /* Clear on Read enabled. RTC_IT bit of TWL4030_INT_PWR_ISR1 440b07682b6SSantosh Shilimkar * needs 2 reads to clear the interrupt. One read is done in 441ef3b7d0dSBalaji T K * do_twl_pwrirq(). Doing the second read, to clear 442b07682b6SSantosh Shilimkar * the bit. 443b07682b6SSantosh Shilimkar * 444b07682b6SSantosh Shilimkar * FIXME the reason PWR_ISR1 needs an extra read is that 445b07682b6SSantosh Shilimkar * RTC_IF retriggered until we cleared REG_ALARM_M above. 446b07682b6SSantosh Shilimkar * But re-reading like this is a bad hack; by doing so we 447b07682b6SSantosh Shilimkar * risk wrongly clearing status for some other IRQ (losing 448b07682b6SSantosh Shilimkar * the interrupt). Be smarter about handling RTC_UF ... 449b07682b6SSantosh Shilimkar */ 450fc7b92fcSBalaji T K res = twl_i2c_read_u8(TWL4030_MODULE_INT, 451b07682b6SSantosh Shilimkar &rd_reg, TWL4030_INT_PWR_ISR1); 452b07682b6SSantosh Shilimkar if (res) 453b07682b6SSantosh Shilimkar goto out; 454a6b49ffdSBalaji T K } 455b07682b6SSantosh Shilimkar 456b07682b6SSantosh Shilimkar /* Notify RTC core on event */ 457b07682b6SSantosh Shilimkar rtc_update_irq(rtc, 1, events); 458b07682b6SSantosh Shilimkar 459b07682b6SSantosh Shilimkar ret = IRQ_HANDLED; 460b07682b6SSantosh Shilimkar out: 461b07682b6SSantosh Shilimkar return ret; 462b07682b6SSantosh Shilimkar } 463b07682b6SSantosh Shilimkar 464ef3b7d0dSBalaji T K static struct rtc_class_ops twl_rtc_ops = { 465ef3b7d0dSBalaji T K .read_time = twl_rtc_read_time, 466ef3b7d0dSBalaji T K .set_time = twl_rtc_set_time, 467ef3b7d0dSBalaji T K .read_alarm = twl_rtc_read_alarm, 468ef3b7d0dSBalaji T K .set_alarm = twl_rtc_set_alarm, 469ef3b7d0dSBalaji T K .alarm_irq_enable = twl_rtc_alarm_irq_enable, 470b07682b6SSantosh Shilimkar }; 471b07682b6SSantosh Shilimkar 472b07682b6SSantosh Shilimkar /*----------------------------------------------------------------------*/ 473b07682b6SSantosh Shilimkar 4745a167f45SGreg Kroah-Hartman static int twl_rtc_probe(struct platform_device *pdev) 475b07682b6SSantosh Shilimkar { 476b07682b6SSantosh Shilimkar struct rtc_device *rtc; 4777e72c686STodd Poynor int ret = -EINVAL; 478b07682b6SSantosh Shilimkar int irq = platform_get_irq(pdev, 0); 479b07682b6SSantosh Shilimkar u8 rd_reg; 480b07682b6SSantosh Shilimkar 481b07682b6SSantosh Shilimkar if (irq <= 0) 482*f53eeb85SJingoo Han return ret; 483b07682b6SSantosh Shilimkar 484d3869ff6SPeter Ujfalusi /* Initialize the register map */ 485d3869ff6SPeter Ujfalusi if (twl_class_is_4030()) 486d3869ff6SPeter Ujfalusi rtc_reg_map = (u8 *)twl4030_rtc_reg_map; 487d3869ff6SPeter Ujfalusi else 488d3869ff6SPeter Ujfalusi rtc_reg_map = (u8 *)twl6030_rtc_reg_map; 489d3869ff6SPeter Ujfalusi 490ef3b7d0dSBalaji T K ret = twl_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG); 491b07682b6SSantosh Shilimkar if (ret < 0) 492*f53eeb85SJingoo Han return ret; 493b07682b6SSantosh Shilimkar 494b07682b6SSantosh Shilimkar if (rd_reg & BIT_RTC_STATUS_REG_POWER_UP_M) 495b07682b6SSantosh Shilimkar dev_warn(&pdev->dev, "Power up reset detected.\n"); 496b07682b6SSantosh Shilimkar 497b07682b6SSantosh Shilimkar if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M) 498b07682b6SSantosh Shilimkar dev_warn(&pdev->dev, "Pending Alarm interrupt detected.\n"); 499b07682b6SSantosh Shilimkar 500b07682b6SSantosh Shilimkar /* Clear RTC Power up reset and pending alarm interrupts */ 501ef3b7d0dSBalaji T K ret = twl_rtc_write_u8(rd_reg, REG_RTC_STATUS_REG); 502b07682b6SSantosh Shilimkar if (ret < 0) 503*f53eeb85SJingoo Han return ret; 504b07682b6SSantosh Shilimkar 505a6b49ffdSBalaji T K if (twl_class_is_6030()) { 506a6b49ffdSBalaji T K twl6030_interrupt_unmask(TWL6030_RTC_INT_MASK, 507a6b49ffdSBalaji T K REG_INT_MSK_LINE_A); 508a6b49ffdSBalaji T K twl6030_interrupt_unmask(TWL6030_RTC_INT_MASK, 509a6b49ffdSBalaji T K REG_INT_MSK_STS_A); 510a6b49ffdSBalaji T K } 511a6b49ffdSBalaji T K 512f7439bcbSVenu Byravarasu dev_info(&pdev->dev, "Enabling TWL-RTC\n"); 513f7439bcbSVenu Byravarasu ret = twl_rtc_write_u8(BIT_RTC_CTRL_REG_STOP_RTC_M, REG_RTC_CTRL_REG); 514b07682b6SSantosh Shilimkar if (ret < 0) 515*f53eeb85SJingoo Han return ret; 516b07682b6SSantosh Shilimkar 5178dcebaa9SKevin Hilman /* ensure interrupts are disabled, bootloaders can be strange */ 5188dcebaa9SKevin Hilman ret = twl_rtc_write_u8(0, REG_RTC_INTERRUPTS_REG); 5198dcebaa9SKevin Hilman if (ret < 0) 5208dcebaa9SKevin Hilman dev_warn(&pdev->dev, "unable to disable interrupt\n"); 5218dcebaa9SKevin Hilman 522b07682b6SSantosh Shilimkar /* init cached IRQ enable bits */ 523ef3b7d0dSBalaji T K ret = twl_rtc_read_u8(&rtc_irq_bits, REG_RTC_INTERRUPTS_REG); 524b07682b6SSantosh Shilimkar if (ret < 0) 525*f53eeb85SJingoo Han return ret; 526b07682b6SSantosh Shilimkar 527b99b94b5SGrygorii Strashko device_init_wakeup(&pdev->dev, 1); 528b99b94b5SGrygorii Strashko 529*f53eeb85SJingoo Han rtc = devm_rtc_device_register(&pdev->dev, pdev->name, 530*f53eeb85SJingoo Han &twl_rtc_ops, THIS_MODULE); 5317e72c686STodd Poynor if (IS_ERR(rtc)) { 5327e72c686STodd Poynor dev_err(&pdev->dev, "can't register RTC device, err %ld\n", 5337e72c686STodd Poynor PTR_ERR(rtc)); 534*f53eeb85SJingoo Han return PTR_ERR(rtc); 5357e72c686STodd Poynor } 5367e72c686STodd Poynor 537*f53eeb85SJingoo Han ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, 538*f53eeb85SJingoo Han twl_rtc_interrupt, 5396b91bf1aSKevin Hilman IRQF_TRIGGER_RISING | IRQF_ONESHOT, 5407e72c686STodd Poynor dev_name(&rtc->dev), rtc); 5417e72c686STodd Poynor if (ret < 0) { 5427e72c686STodd Poynor dev_err(&pdev->dev, "IRQ is not free.\n"); 543*f53eeb85SJingoo Han return ret; 5447e72c686STodd Poynor } 5457e72c686STodd Poynor 5467e72c686STodd Poynor platform_set_drvdata(pdev, rtc); 5477e72c686STodd Poynor return 0; 548b07682b6SSantosh Shilimkar } 549b07682b6SSantosh Shilimkar 550b07682b6SSantosh Shilimkar /* 551ef3b7d0dSBalaji T K * Disable all TWL RTC module interrupts. 552b07682b6SSantosh Shilimkar * Sets status flag to free. 553b07682b6SSantosh Shilimkar */ 5545a167f45SGreg Kroah-Hartman static int twl_rtc_remove(struct platform_device *pdev) 555b07682b6SSantosh Shilimkar { 556b07682b6SSantosh Shilimkar /* leave rtc running, but disable irqs */ 557b07682b6SSantosh Shilimkar mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M); 558b07682b6SSantosh Shilimkar mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M); 559a6b49ffdSBalaji T K if (twl_class_is_6030()) { 560a6b49ffdSBalaji T K twl6030_interrupt_mask(TWL6030_RTC_INT_MASK, 561a6b49ffdSBalaji T K REG_INT_MSK_LINE_A); 562a6b49ffdSBalaji T K twl6030_interrupt_mask(TWL6030_RTC_INT_MASK, 563a6b49ffdSBalaji T K REG_INT_MSK_STS_A); 564a6b49ffdSBalaji T K } 565a6b49ffdSBalaji T K 566b07682b6SSantosh Shilimkar return 0; 567b07682b6SSantosh Shilimkar } 568b07682b6SSantosh Shilimkar 569ef3b7d0dSBalaji T K static void twl_rtc_shutdown(struct platform_device *pdev) 570b07682b6SSantosh Shilimkar { 571b07682b6SSantosh Shilimkar /* mask timer interrupts, but leave alarm interrupts on to enable 572b07682b6SSantosh Shilimkar power-on when alarm is triggered */ 573b07682b6SSantosh Shilimkar mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M); 574b07682b6SSantosh Shilimkar } 575b07682b6SSantosh Shilimkar 576b9d8c460SJingoo Han #ifdef CONFIG_PM_SLEEP 577b07682b6SSantosh Shilimkar static unsigned char irqstat; 578b07682b6SSantosh Shilimkar 579b9d8c460SJingoo Han static int twl_rtc_suspend(struct device *dev) 580b07682b6SSantosh Shilimkar { 581b07682b6SSantosh Shilimkar irqstat = rtc_irq_bits; 582b07682b6SSantosh Shilimkar 583b07682b6SSantosh Shilimkar mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M); 584b07682b6SSantosh Shilimkar return 0; 585b07682b6SSantosh Shilimkar } 586b07682b6SSantosh Shilimkar 587b9d8c460SJingoo Han static int twl_rtc_resume(struct device *dev) 588b07682b6SSantosh Shilimkar { 589b07682b6SSantosh Shilimkar set_rtc_irq_bit(irqstat); 590b07682b6SSantosh Shilimkar return 0; 591b07682b6SSantosh Shilimkar } 592b07682b6SSantosh Shilimkar #endif 593b07682b6SSantosh Shilimkar 594b9d8c460SJingoo Han static SIMPLE_DEV_PM_OPS(twl_rtc_pm_ops, twl_rtc_suspend, twl_rtc_resume); 595b9d8c460SJingoo Han 596c8a6046eSSachin Kamat #ifdef CONFIG_OF 597948170f8SBenoit Cousson static const struct of_device_id twl_rtc_of_match[] = { 598948170f8SBenoit Cousson {.compatible = "ti,twl4030-rtc", }, 599948170f8SBenoit Cousson { }, 600948170f8SBenoit Cousson }; 601948170f8SBenoit Cousson MODULE_DEVICE_TABLE(of, twl_rtc_of_match); 602c8a6046eSSachin Kamat #endif 603c8a6046eSSachin Kamat 604ef3b7d0dSBalaji T K MODULE_ALIAS("platform:twl_rtc"); 605b07682b6SSantosh Shilimkar 606b07682b6SSantosh Shilimkar static struct platform_driver twl4030rtc_driver = { 607ef3b7d0dSBalaji T K .probe = twl_rtc_probe, 6085a167f45SGreg Kroah-Hartman .remove = twl_rtc_remove, 609ef3b7d0dSBalaji T K .shutdown = twl_rtc_shutdown, 610b07682b6SSantosh Shilimkar .driver = { 611b07682b6SSantosh Shilimkar .owner = THIS_MODULE, 612ef3b7d0dSBalaji T K .name = "twl_rtc", 613b9d8c460SJingoo Han .pm = &twl_rtc_pm_ops, 614c8a6046eSSachin Kamat .of_match_table = of_match_ptr(twl_rtc_of_match), 615b07682b6SSantosh Shilimkar }, 616b07682b6SSantosh Shilimkar }; 617b07682b6SSantosh Shilimkar 6185ee67484SPeter Ujfalusi module_platform_driver(twl4030rtc_driver); 619b07682b6SSantosh Shilimkar 620b07682b6SSantosh Shilimkar MODULE_AUTHOR("Texas Instruments, MontaVista Software"); 621b07682b6SSantosh Shilimkar MODULE_LICENSE("GPL"); 622