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 { 216b07682b6SSantosh Shilimkar int ret; 217b07682b6SSantosh Shilimkar 218b07682b6SSantosh Shilimkar if (enabled) 219b07682b6SSantosh Shilimkar ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M); 220b07682b6SSantosh Shilimkar else 221b07682b6SSantosh Shilimkar ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M); 222b07682b6SSantosh Shilimkar 223b07682b6SSantosh Shilimkar return ret; 224b07682b6SSantosh Shilimkar } 225b07682b6SSantosh Shilimkar 226b07682b6SSantosh Shilimkar /* 227ef3b7d0dSBalaji T K * Gets current TWL RTC time and date parameters. 228b07682b6SSantosh Shilimkar * 229b07682b6SSantosh Shilimkar * The RTC's time/alarm representation is not what gmtime(3) requires 230b07682b6SSantosh Shilimkar * Linux to use: 231b07682b6SSantosh Shilimkar * 232b07682b6SSantosh Shilimkar * - Months are 1..12 vs Linux 0-11 233b07682b6SSantosh Shilimkar * - Years are 0..99 vs Linux 1900..N (we assume 21st century) 234b07682b6SSantosh Shilimkar */ 235ef3b7d0dSBalaji T K static int twl_rtc_read_time(struct device *dev, struct rtc_time *tm) 236b07682b6SSantosh Shilimkar { 23714591d88SPeter Ujfalusi unsigned char rtc_data[ALL_TIME_REGS]; 238b07682b6SSantosh Shilimkar int ret; 239b07682b6SSantosh Shilimkar u8 save_control; 240f3ec434cSKonstantin Shlyakhovoy u8 rtc_control; 241b07682b6SSantosh Shilimkar 242ef3b7d0dSBalaji T K ret = twl_rtc_read_u8(&save_control, REG_RTC_CTRL_REG); 243f3ec434cSKonstantin Shlyakhovoy if (ret < 0) { 244f3ec434cSKonstantin Shlyakhovoy dev_err(dev, "%s: reading CTRL_REG, error %d\n", __func__, ret); 245b07682b6SSantosh Shilimkar return ret; 246f3ec434cSKonstantin Shlyakhovoy } 247f3ec434cSKonstantin Shlyakhovoy /* for twl6030/32 make sure BIT_RTC_CTRL_REG_GET_TIME_M is clear */ 248f3ec434cSKonstantin Shlyakhovoy if (twl_class_is_6030()) { 249f3ec434cSKonstantin Shlyakhovoy if (save_control & BIT_RTC_CTRL_REG_GET_TIME_M) { 250f3ec434cSKonstantin Shlyakhovoy save_control &= ~BIT_RTC_CTRL_REG_GET_TIME_M; 251ef3b7d0dSBalaji T K ret = twl_rtc_write_u8(save_control, REG_RTC_CTRL_REG); 252f3ec434cSKonstantin Shlyakhovoy if (ret < 0) { 253f3ec434cSKonstantin Shlyakhovoy dev_err(dev, "%s clr GET_TIME, error %d\n", 254f3ec434cSKonstantin Shlyakhovoy __func__, ret); 255b07682b6SSantosh Shilimkar return ret; 256f3ec434cSKonstantin Shlyakhovoy } 257f3ec434cSKonstantin Shlyakhovoy } 258f3ec434cSKonstantin Shlyakhovoy } 259f3ec434cSKonstantin Shlyakhovoy 260f3ec434cSKonstantin Shlyakhovoy /* Copy RTC counting registers to static registers or latches */ 261f3ec434cSKonstantin Shlyakhovoy rtc_control = save_control | BIT_RTC_CTRL_REG_GET_TIME_M; 262f3ec434cSKonstantin Shlyakhovoy 263f3ec434cSKonstantin Shlyakhovoy /* for twl6030/32 enable read access to static shadowed registers */ 264f3ec434cSKonstantin Shlyakhovoy if (twl_class_is_6030()) 265f3ec434cSKonstantin Shlyakhovoy rtc_control |= BIT_RTC_CTRL_REG_RTC_V_OPT; 266f3ec434cSKonstantin Shlyakhovoy 267f3ec434cSKonstantin Shlyakhovoy ret = twl_rtc_write_u8(rtc_control, REG_RTC_CTRL_REG); 268f3ec434cSKonstantin Shlyakhovoy if (ret < 0) { 269f3ec434cSKonstantin Shlyakhovoy dev_err(dev, "%s: writing CTRL_REG, error %d\n", __func__, ret); 270f3ec434cSKonstantin Shlyakhovoy return ret; 271f3ec434cSKonstantin Shlyakhovoy } 272b07682b6SSantosh Shilimkar 273ef3b7d0dSBalaji T K ret = twl_i2c_read(TWL_MODULE_RTC, rtc_data, 274a6b49ffdSBalaji T K (rtc_reg_map[REG_SECONDS_REG]), ALL_TIME_REGS); 275b07682b6SSantosh Shilimkar 276b07682b6SSantosh Shilimkar if (ret < 0) { 277f3ec434cSKonstantin Shlyakhovoy dev_err(dev, "%s: reading data, error %d\n", __func__, ret); 278b07682b6SSantosh Shilimkar return ret; 279b07682b6SSantosh Shilimkar } 280b07682b6SSantosh Shilimkar 281f3ec434cSKonstantin Shlyakhovoy /* for twl6030 restore original state of rtc control register */ 282f3ec434cSKonstantin Shlyakhovoy if (twl_class_is_6030()) { 283f3ec434cSKonstantin Shlyakhovoy ret = twl_rtc_write_u8(save_control, REG_RTC_CTRL_REG); 284f3ec434cSKonstantin Shlyakhovoy if (ret < 0) { 285f3ec434cSKonstantin Shlyakhovoy dev_err(dev, "%s: restore CTRL_REG, error %d\n", 286f3ec434cSKonstantin Shlyakhovoy __func__, ret); 287f3ec434cSKonstantin Shlyakhovoy return ret; 288f3ec434cSKonstantin Shlyakhovoy } 289f3ec434cSKonstantin Shlyakhovoy } 290f3ec434cSKonstantin Shlyakhovoy 291b07682b6SSantosh Shilimkar tm->tm_sec = bcd2bin(rtc_data[0]); 292b07682b6SSantosh Shilimkar tm->tm_min = bcd2bin(rtc_data[1]); 293b07682b6SSantosh Shilimkar tm->tm_hour = bcd2bin(rtc_data[2]); 294b07682b6SSantosh Shilimkar tm->tm_mday = bcd2bin(rtc_data[3]); 295b07682b6SSantosh Shilimkar tm->tm_mon = bcd2bin(rtc_data[4]) - 1; 296b07682b6SSantosh Shilimkar tm->tm_year = bcd2bin(rtc_data[5]) + 100; 297b07682b6SSantosh Shilimkar 298b07682b6SSantosh Shilimkar return ret; 299b07682b6SSantosh Shilimkar } 300b07682b6SSantosh Shilimkar 301ef3b7d0dSBalaji T K static int twl_rtc_set_time(struct device *dev, struct rtc_time *tm) 302b07682b6SSantosh Shilimkar { 303b07682b6SSantosh Shilimkar unsigned char save_control; 30414591d88SPeter Ujfalusi unsigned char rtc_data[ALL_TIME_REGS]; 305b07682b6SSantosh Shilimkar int ret; 306b07682b6SSantosh Shilimkar 30714591d88SPeter Ujfalusi rtc_data[0] = bin2bcd(tm->tm_sec); 30814591d88SPeter Ujfalusi rtc_data[1] = bin2bcd(tm->tm_min); 30914591d88SPeter Ujfalusi rtc_data[2] = bin2bcd(tm->tm_hour); 31014591d88SPeter Ujfalusi rtc_data[3] = bin2bcd(tm->tm_mday); 31114591d88SPeter Ujfalusi rtc_data[4] = bin2bcd(tm->tm_mon + 1); 31214591d88SPeter Ujfalusi rtc_data[5] = bin2bcd(tm->tm_year - 100); 313b07682b6SSantosh Shilimkar 314b07682b6SSantosh Shilimkar /* Stop RTC while updating the TC registers */ 315ef3b7d0dSBalaji T K ret = twl_rtc_read_u8(&save_control, REG_RTC_CTRL_REG); 316b07682b6SSantosh Shilimkar if (ret < 0) 317b07682b6SSantosh Shilimkar goto out; 318b07682b6SSantosh Shilimkar 319b07682b6SSantosh Shilimkar save_control &= ~BIT_RTC_CTRL_REG_STOP_RTC_M; 3208f6b0dd3SJesper Juhl ret = twl_rtc_write_u8(save_control, REG_RTC_CTRL_REG); 321b07682b6SSantosh Shilimkar if (ret < 0) 322b07682b6SSantosh Shilimkar goto out; 323b07682b6SSantosh Shilimkar 324b07682b6SSantosh Shilimkar /* update all the time registers in one shot */ 325ef3b7d0dSBalaji T K ret = twl_i2c_write(TWL_MODULE_RTC, rtc_data, 326a6b49ffdSBalaji T K (rtc_reg_map[REG_SECONDS_REG]), ALL_TIME_REGS); 327b07682b6SSantosh Shilimkar if (ret < 0) { 328b07682b6SSantosh Shilimkar dev_err(dev, "rtc_set_time error %d\n", ret); 329b07682b6SSantosh Shilimkar goto out; 330b07682b6SSantosh Shilimkar } 331b07682b6SSantosh Shilimkar 332b07682b6SSantosh Shilimkar /* Start back RTC */ 333b07682b6SSantosh Shilimkar save_control |= BIT_RTC_CTRL_REG_STOP_RTC_M; 334ef3b7d0dSBalaji T K ret = twl_rtc_write_u8(save_control, REG_RTC_CTRL_REG); 335b07682b6SSantosh Shilimkar 336b07682b6SSantosh Shilimkar out: 337b07682b6SSantosh Shilimkar return ret; 338b07682b6SSantosh Shilimkar } 339b07682b6SSantosh Shilimkar 340b07682b6SSantosh Shilimkar /* 341ef3b7d0dSBalaji T K * Gets current TWL RTC alarm time. 342b07682b6SSantosh Shilimkar */ 343ef3b7d0dSBalaji T K static int twl_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) 344b07682b6SSantosh Shilimkar { 34514591d88SPeter Ujfalusi unsigned char rtc_data[ALL_TIME_REGS]; 346b07682b6SSantosh Shilimkar int ret; 347b07682b6SSantosh Shilimkar 348ef3b7d0dSBalaji T K ret = twl_i2c_read(TWL_MODULE_RTC, rtc_data, 349a6b49ffdSBalaji T K (rtc_reg_map[REG_ALARM_SECONDS_REG]), ALL_TIME_REGS); 350b07682b6SSantosh Shilimkar if (ret < 0) { 351b07682b6SSantosh Shilimkar dev_err(dev, "rtc_read_alarm error %d\n", ret); 352b07682b6SSantosh Shilimkar return ret; 353b07682b6SSantosh Shilimkar } 354b07682b6SSantosh Shilimkar 355b07682b6SSantosh Shilimkar /* some of these fields may be wildcard/"match all" */ 356b07682b6SSantosh Shilimkar alm->time.tm_sec = bcd2bin(rtc_data[0]); 357b07682b6SSantosh Shilimkar alm->time.tm_min = bcd2bin(rtc_data[1]); 358b07682b6SSantosh Shilimkar alm->time.tm_hour = bcd2bin(rtc_data[2]); 359b07682b6SSantosh Shilimkar alm->time.tm_mday = bcd2bin(rtc_data[3]); 360b07682b6SSantosh Shilimkar alm->time.tm_mon = bcd2bin(rtc_data[4]) - 1; 361b07682b6SSantosh Shilimkar alm->time.tm_year = bcd2bin(rtc_data[5]) + 100; 362b07682b6SSantosh Shilimkar 363b07682b6SSantosh Shilimkar /* report cached alarm enable state */ 364b07682b6SSantosh Shilimkar if (rtc_irq_bits & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M) 365b07682b6SSantosh Shilimkar alm->enabled = 1; 366b07682b6SSantosh Shilimkar 367b07682b6SSantosh Shilimkar return ret; 368b07682b6SSantosh Shilimkar } 369b07682b6SSantosh Shilimkar 370ef3b7d0dSBalaji T K static int twl_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) 371b07682b6SSantosh Shilimkar { 37214591d88SPeter Ujfalusi unsigned char alarm_data[ALL_TIME_REGS]; 373b07682b6SSantosh Shilimkar int ret; 374b07682b6SSantosh Shilimkar 375ef3b7d0dSBalaji T K ret = twl_rtc_alarm_irq_enable(dev, 0); 376b07682b6SSantosh Shilimkar if (ret) 377b07682b6SSantosh Shilimkar goto out; 378b07682b6SSantosh Shilimkar 37914591d88SPeter Ujfalusi alarm_data[0] = bin2bcd(alm->time.tm_sec); 38014591d88SPeter Ujfalusi alarm_data[1] = bin2bcd(alm->time.tm_min); 38114591d88SPeter Ujfalusi alarm_data[2] = bin2bcd(alm->time.tm_hour); 38214591d88SPeter Ujfalusi alarm_data[3] = bin2bcd(alm->time.tm_mday); 38314591d88SPeter Ujfalusi alarm_data[4] = bin2bcd(alm->time.tm_mon + 1); 38414591d88SPeter Ujfalusi alarm_data[5] = bin2bcd(alm->time.tm_year - 100); 385b07682b6SSantosh Shilimkar 386b07682b6SSantosh Shilimkar /* update all the alarm registers in one shot */ 387ef3b7d0dSBalaji T K ret = twl_i2c_write(TWL_MODULE_RTC, alarm_data, 388a6b49ffdSBalaji T K (rtc_reg_map[REG_ALARM_SECONDS_REG]), ALL_TIME_REGS); 389b07682b6SSantosh Shilimkar if (ret) { 390b07682b6SSantosh Shilimkar dev_err(dev, "rtc_set_alarm error %d\n", ret); 391b07682b6SSantosh Shilimkar goto out; 392b07682b6SSantosh Shilimkar } 393b07682b6SSantosh Shilimkar 394b07682b6SSantosh Shilimkar if (alm->enabled) 395ef3b7d0dSBalaji T K ret = twl_rtc_alarm_irq_enable(dev, 1); 396b07682b6SSantosh Shilimkar out: 397b07682b6SSantosh Shilimkar return ret; 398b07682b6SSantosh Shilimkar } 399b07682b6SSantosh Shilimkar 400ef3b7d0dSBalaji T K static irqreturn_t twl_rtc_interrupt(int irq, void *rtc) 401b07682b6SSantosh Shilimkar { 4022778ebccSVenu Byravarasu unsigned long events; 403b07682b6SSantosh Shilimkar int ret = IRQ_NONE; 404b07682b6SSantosh Shilimkar int res; 405b07682b6SSantosh Shilimkar u8 rd_reg; 406b07682b6SSantosh Shilimkar 407ef3b7d0dSBalaji T K res = twl_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG); 408b07682b6SSantosh Shilimkar if (res) 409b07682b6SSantosh Shilimkar goto out; 410b07682b6SSantosh Shilimkar /* 411b07682b6SSantosh Shilimkar * Figure out source of interrupt: ALARM or TIMER in RTC_STATUS_REG. 412b07682b6SSantosh Shilimkar * only one (ALARM or RTC) interrupt source may be enabled 413b07682b6SSantosh Shilimkar * at time, we also could check our results 414b07682b6SSantosh Shilimkar * by reading RTS_INTERRUPTS_REGISTER[IT_TIMER,IT_ALARM] 415b07682b6SSantosh Shilimkar */ 416b07682b6SSantosh Shilimkar if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M) 4172778ebccSVenu Byravarasu events = RTC_IRQF | RTC_AF; 418b07682b6SSantosh Shilimkar else 4192778ebccSVenu Byravarasu events = RTC_IRQF | RTC_PF; 420b07682b6SSantosh Shilimkar 42194a339d0SVenu Byravarasu res = twl_rtc_write_u8(BIT_RTC_STATUS_REG_ALARM_M, 422b07682b6SSantosh Shilimkar REG_RTC_STATUS_REG); 423b07682b6SSantosh Shilimkar if (res) 424b07682b6SSantosh Shilimkar goto out; 425b07682b6SSantosh Shilimkar 426a6b49ffdSBalaji T K if (twl_class_is_4030()) { 427b07682b6SSantosh Shilimkar /* Clear on Read enabled. RTC_IT bit of TWL4030_INT_PWR_ISR1 428b07682b6SSantosh Shilimkar * needs 2 reads to clear the interrupt. One read is done in 429ef3b7d0dSBalaji T K * do_twl_pwrirq(). Doing the second read, to clear 430b07682b6SSantosh Shilimkar * the bit. 431b07682b6SSantosh Shilimkar * 432b07682b6SSantosh Shilimkar * FIXME the reason PWR_ISR1 needs an extra read is that 433b07682b6SSantosh Shilimkar * RTC_IF retriggered until we cleared REG_ALARM_M above. 434b07682b6SSantosh Shilimkar * But re-reading like this is a bad hack; by doing so we 435b07682b6SSantosh Shilimkar * risk wrongly clearing status for some other IRQ (losing 436b07682b6SSantosh Shilimkar * the interrupt). Be smarter about handling RTC_UF ... 437b07682b6SSantosh Shilimkar */ 438fc7b92fcSBalaji T K res = twl_i2c_read_u8(TWL4030_MODULE_INT, 439b07682b6SSantosh Shilimkar &rd_reg, TWL4030_INT_PWR_ISR1); 440b07682b6SSantosh Shilimkar if (res) 441b07682b6SSantosh Shilimkar goto out; 442a6b49ffdSBalaji T K } 443b07682b6SSantosh Shilimkar 444b07682b6SSantosh Shilimkar /* Notify RTC core on event */ 445b07682b6SSantosh Shilimkar rtc_update_irq(rtc, 1, events); 446b07682b6SSantosh Shilimkar 447b07682b6SSantosh Shilimkar ret = IRQ_HANDLED; 448b07682b6SSantosh Shilimkar out: 449b07682b6SSantosh Shilimkar return ret; 450b07682b6SSantosh Shilimkar } 451b07682b6SSantosh Shilimkar 452ef3b7d0dSBalaji T K static struct rtc_class_ops twl_rtc_ops = { 453ef3b7d0dSBalaji T K .read_time = twl_rtc_read_time, 454ef3b7d0dSBalaji T K .set_time = twl_rtc_set_time, 455ef3b7d0dSBalaji T K .read_alarm = twl_rtc_read_alarm, 456ef3b7d0dSBalaji T K .set_alarm = twl_rtc_set_alarm, 457ef3b7d0dSBalaji T K .alarm_irq_enable = twl_rtc_alarm_irq_enable, 458b07682b6SSantosh Shilimkar }; 459b07682b6SSantosh Shilimkar 460b07682b6SSantosh Shilimkar /*----------------------------------------------------------------------*/ 461b07682b6SSantosh Shilimkar 4625a167f45SGreg Kroah-Hartman static int twl_rtc_probe(struct platform_device *pdev) 463b07682b6SSantosh Shilimkar { 464b07682b6SSantosh Shilimkar struct rtc_device *rtc; 4657e72c686STodd Poynor int ret = -EINVAL; 466b07682b6SSantosh Shilimkar int irq = platform_get_irq(pdev, 0); 467b07682b6SSantosh Shilimkar u8 rd_reg; 468b07682b6SSantosh Shilimkar 469b07682b6SSantosh Shilimkar if (irq <= 0) 4707e72c686STodd Poynor goto out1; 471b07682b6SSantosh Shilimkar 472ef3b7d0dSBalaji T K ret = twl_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG); 473b07682b6SSantosh Shilimkar if (ret < 0) 474b07682b6SSantosh Shilimkar goto out1; 475b07682b6SSantosh Shilimkar 476b07682b6SSantosh Shilimkar if (rd_reg & BIT_RTC_STATUS_REG_POWER_UP_M) 477b07682b6SSantosh Shilimkar dev_warn(&pdev->dev, "Power up reset detected.\n"); 478b07682b6SSantosh Shilimkar 479b07682b6SSantosh Shilimkar if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M) 480b07682b6SSantosh Shilimkar dev_warn(&pdev->dev, "Pending Alarm interrupt detected.\n"); 481b07682b6SSantosh Shilimkar 482b07682b6SSantosh Shilimkar /* Clear RTC Power up reset and pending alarm interrupts */ 483ef3b7d0dSBalaji T K ret = twl_rtc_write_u8(rd_reg, REG_RTC_STATUS_REG); 484b07682b6SSantosh Shilimkar if (ret < 0) 485b07682b6SSantosh Shilimkar goto out1; 486b07682b6SSantosh Shilimkar 487a6b49ffdSBalaji T K if (twl_class_is_6030()) { 488a6b49ffdSBalaji T K twl6030_interrupt_unmask(TWL6030_RTC_INT_MASK, 489a6b49ffdSBalaji T K REG_INT_MSK_LINE_A); 490a6b49ffdSBalaji T K twl6030_interrupt_unmask(TWL6030_RTC_INT_MASK, 491a6b49ffdSBalaji T K REG_INT_MSK_STS_A); 492a6b49ffdSBalaji T K } 493a6b49ffdSBalaji T K 494f7439bcbSVenu Byravarasu dev_info(&pdev->dev, "Enabling TWL-RTC\n"); 495f7439bcbSVenu Byravarasu ret = twl_rtc_write_u8(BIT_RTC_CTRL_REG_STOP_RTC_M, REG_RTC_CTRL_REG); 496b07682b6SSantosh Shilimkar if (ret < 0) 4977e72c686STodd Poynor goto out1; 498b07682b6SSantosh Shilimkar 4998dcebaa9SKevin Hilman /* ensure interrupts are disabled, bootloaders can be strange */ 5008dcebaa9SKevin Hilman ret = twl_rtc_write_u8(0, REG_RTC_INTERRUPTS_REG); 5018dcebaa9SKevin Hilman if (ret < 0) 5028dcebaa9SKevin Hilman dev_warn(&pdev->dev, "unable to disable interrupt\n"); 5038dcebaa9SKevin Hilman 504b07682b6SSantosh Shilimkar /* init cached IRQ enable bits */ 505ef3b7d0dSBalaji T K ret = twl_rtc_read_u8(&rtc_irq_bits, REG_RTC_INTERRUPTS_REG); 506b07682b6SSantosh Shilimkar if (ret < 0) 5077e72c686STodd Poynor goto out1; 508b07682b6SSantosh Shilimkar 5097e72c686STodd Poynor rtc = rtc_device_register(pdev->name, 5107e72c686STodd Poynor &pdev->dev, &twl_rtc_ops, THIS_MODULE); 5117e72c686STodd Poynor if (IS_ERR(rtc)) { 5127e72c686STodd Poynor ret = PTR_ERR(rtc); 5137e72c686STodd Poynor dev_err(&pdev->dev, "can't register RTC device, err %ld\n", 5147e72c686STodd Poynor PTR_ERR(rtc)); 5157e72c686STodd Poynor goto out1; 5167e72c686STodd Poynor } 5177e72c686STodd Poynor 5187e72c686STodd Poynor ret = request_threaded_irq(irq, NULL, twl_rtc_interrupt, 5196b91bf1aSKevin Hilman IRQF_TRIGGER_RISING | IRQF_ONESHOT, 5207e72c686STodd Poynor dev_name(&rtc->dev), rtc); 5217e72c686STodd Poynor if (ret < 0) { 5227e72c686STodd Poynor dev_err(&pdev->dev, "IRQ is not free.\n"); 5237e72c686STodd Poynor goto out2; 5247e72c686STodd Poynor } 5257e72c686STodd Poynor 5267e72c686STodd Poynor platform_set_drvdata(pdev, rtc); 5277e72c686STodd Poynor return 0; 528b07682b6SSantosh Shilimkar 529b07682b6SSantosh Shilimkar out2: 530b07682b6SSantosh Shilimkar rtc_device_unregister(rtc); 5317e72c686STodd Poynor out1: 532b07682b6SSantosh Shilimkar return ret; 533b07682b6SSantosh Shilimkar } 534b07682b6SSantosh Shilimkar 535b07682b6SSantosh Shilimkar /* 536ef3b7d0dSBalaji T K * Disable all TWL RTC module interrupts. 537b07682b6SSantosh Shilimkar * Sets status flag to free. 538b07682b6SSantosh Shilimkar */ 5395a167f45SGreg Kroah-Hartman static int twl_rtc_remove(struct platform_device *pdev) 540b07682b6SSantosh Shilimkar { 541b07682b6SSantosh Shilimkar /* leave rtc running, but disable irqs */ 542b07682b6SSantosh Shilimkar struct rtc_device *rtc = platform_get_drvdata(pdev); 543b07682b6SSantosh Shilimkar int irq = platform_get_irq(pdev, 0); 544b07682b6SSantosh Shilimkar 545b07682b6SSantosh Shilimkar mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M); 546b07682b6SSantosh Shilimkar mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M); 547a6b49ffdSBalaji T K if (twl_class_is_6030()) { 548a6b49ffdSBalaji T K twl6030_interrupt_mask(TWL6030_RTC_INT_MASK, 549a6b49ffdSBalaji T K REG_INT_MSK_LINE_A); 550a6b49ffdSBalaji T K twl6030_interrupt_mask(TWL6030_RTC_INT_MASK, 551a6b49ffdSBalaji T K REG_INT_MSK_STS_A); 552a6b49ffdSBalaji T K } 553a6b49ffdSBalaji T K 554b07682b6SSantosh Shilimkar 555b07682b6SSantosh Shilimkar free_irq(irq, rtc); 556b07682b6SSantosh Shilimkar 557b07682b6SSantosh Shilimkar rtc_device_unregister(rtc); 558b07682b6SSantosh Shilimkar platform_set_drvdata(pdev, NULL); 559b07682b6SSantosh Shilimkar return 0; 560b07682b6SSantosh Shilimkar } 561b07682b6SSantosh Shilimkar 562ef3b7d0dSBalaji T K static void twl_rtc_shutdown(struct platform_device *pdev) 563b07682b6SSantosh Shilimkar { 564b07682b6SSantosh Shilimkar /* mask timer interrupts, but leave alarm interrupts on to enable 565b07682b6SSantosh Shilimkar power-on when alarm is triggered */ 566b07682b6SSantosh Shilimkar mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M); 567b07682b6SSantosh Shilimkar } 568b07682b6SSantosh Shilimkar 569*b9d8c460SJingoo Han #ifdef CONFIG_PM_SLEEP 570b07682b6SSantosh Shilimkar static unsigned char irqstat; 571b07682b6SSantosh Shilimkar 572*b9d8c460SJingoo Han static int twl_rtc_suspend(struct device *dev) 573b07682b6SSantosh Shilimkar { 574b07682b6SSantosh Shilimkar irqstat = rtc_irq_bits; 575b07682b6SSantosh Shilimkar 576b07682b6SSantosh Shilimkar mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M); 577b07682b6SSantosh Shilimkar return 0; 578b07682b6SSantosh Shilimkar } 579b07682b6SSantosh Shilimkar 580*b9d8c460SJingoo Han static int twl_rtc_resume(struct device *dev) 581b07682b6SSantosh Shilimkar { 582b07682b6SSantosh Shilimkar set_rtc_irq_bit(irqstat); 583b07682b6SSantosh Shilimkar return 0; 584b07682b6SSantosh Shilimkar } 585b07682b6SSantosh Shilimkar #endif 586b07682b6SSantosh Shilimkar 587*b9d8c460SJingoo Han static SIMPLE_DEV_PM_OPS(twl_rtc_pm_ops, twl_rtc_suspend, twl_rtc_resume); 588*b9d8c460SJingoo Han 589c8a6046eSSachin Kamat #ifdef CONFIG_OF 590948170f8SBenoit Cousson static const struct of_device_id twl_rtc_of_match[] = { 591948170f8SBenoit Cousson {.compatible = "ti,twl4030-rtc", }, 592948170f8SBenoit Cousson { }, 593948170f8SBenoit Cousson }; 594948170f8SBenoit Cousson MODULE_DEVICE_TABLE(of, twl_rtc_of_match); 595c8a6046eSSachin Kamat #endif 596c8a6046eSSachin Kamat 597ef3b7d0dSBalaji T K MODULE_ALIAS("platform:twl_rtc"); 598b07682b6SSantosh Shilimkar 599b07682b6SSantosh Shilimkar static struct platform_driver twl4030rtc_driver = { 600ef3b7d0dSBalaji T K .probe = twl_rtc_probe, 6015a167f45SGreg Kroah-Hartman .remove = twl_rtc_remove, 602ef3b7d0dSBalaji T K .shutdown = twl_rtc_shutdown, 603b07682b6SSantosh Shilimkar .driver = { 604b07682b6SSantosh Shilimkar .owner = THIS_MODULE, 605ef3b7d0dSBalaji T K .name = "twl_rtc", 606*b9d8c460SJingoo Han .pm = &twl_rtc_pm_ops, 607c8a6046eSSachin Kamat .of_match_table = of_match_ptr(twl_rtc_of_match), 608b07682b6SSantosh Shilimkar }, 609b07682b6SSantosh Shilimkar }; 610b07682b6SSantosh Shilimkar 611ef3b7d0dSBalaji T K static int __init twl_rtc_init(void) 612b07682b6SSantosh Shilimkar { 613a6b49ffdSBalaji T K if (twl_class_is_4030()) 614a6b49ffdSBalaji T K rtc_reg_map = (u8 *) twl4030_rtc_reg_map; 615a6b49ffdSBalaji T K else 616a6b49ffdSBalaji T K rtc_reg_map = (u8 *) twl6030_rtc_reg_map; 617a6b49ffdSBalaji T K 618b07682b6SSantosh Shilimkar return platform_driver_register(&twl4030rtc_driver); 619b07682b6SSantosh Shilimkar } 620ef3b7d0dSBalaji T K module_init(twl_rtc_init); 621b07682b6SSantosh Shilimkar 622ef3b7d0dSBalaji T K static void __exit twl_rtc_exit(void) 623b07682b6SSantosh Shilimkar { 624b07682b6SSantosh Shilimkar platform_driver_unregister(&twl4030rtc_driver); 625b07682b6SSantosh Shilimkar } 626ef3b7d0dSBalaji T K module_exit(twl_rtc_exit); 627b07682b6SSantosh Shilimkar 628b07682b6SSantosh Shilimkar MODULE_AUTHOR("Texas Instruments, MontaVista Software"); 629b07682b6SSantosh Shilimkar MODULE_LICENSE("GPL"); 630