2019-06-04 11:11:33 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2008-06-13 02:21:55 +04:00
/*
* rtc - fm3130 . c - RTC driver for Ramtron FM3130 I2C chip .
*
* Copyright ( C ) 2008 Sergey Lapin
* Based on ds1307 driver by James Chapman and David Brownell
*/
# include <linux/module.h>
# include <linux/i2c.h>
# include <linux/rtc.h>
# include <linux/bcd.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2008-06-13 02:21:55 +04:00
# define FM3130_RTC_CONTROL (0x0)
# define FM3130_CAL_CONTROL (0x1)
# define FM3130_RTC_SECONDS (0x2)
# define FM3130_RTC_MINUTES (0x3)
# define FM3130_RTC_HOURS (0x4)
# define FM3130_RTC_DAY (0x5)
# define FM3130_RTC_DATE (0x6)
# define FM3130_RTC_MONTHS (0x7)
# define FM3130_RTC_YEARS (0x8)
# define FM3130_ALARM_SECONDS (0x9)
# define FM3130_ALARM_MINUTES (0xa)
# define FM3130_ALARM_HOURS (0xb)
# define FM3130_ALARM_DATE (0xc)
# define FM3130_ALARM_MONTHS (0xd)
# define FM3130_ALARM_WP_CONTROL (0xe)
# define FM3130_CAL_CONTROL_BIT_nOSCEN (1 << 7) /* Osciallator enabled */
# define FM3130_RTC_CONTROL_BIT_LB (1 << 7) /* Low battery */
# define FM3130_RTC_CONTROL_BIT_AF (1 << 6) /* Alarm flag */
# define FM3130_RTC_CONTROL_BIT_CF (1 << 5) /* Century overflow */
# define FM3130_RTC_CONTROL_BIT_POR (1 << 4) /* Power on reset */
# define FM3130_RTC_CONTROL_BIT_AEN (1 << 3) /* Alarm enable */
# define FM3130_RTC_CONTROL_BIT_CAL (1 << 2) /* Calibration mode */
# define FM3130_RTC_CONTROL_BIT_WRITE (1 << 1) /* W=1 -> write mode W=0 normal */
# define FM3130_RTC_CONTROL_BIT_READ (1 << 0) /* R=1 -> read mode R=0 normal */
# define FM3130_CLOCK_REGS 7
# define FM3130_ALARM_REGS 5
struct fm3130 {
u8 reg_addr_time ;
2013-07-04 02:05:51 +04:00
u8 reg_addr_alarm ;
2008-06-13 02:21:55 +04:00
u8 regs [ 15 ] ;
struct i2c_msg msg [ 4 ] ;
struct i2c_client * client ;
struct rtc_device * rtc ;
2010-08-11 05:02:10 +04:00
int alarm_valid ;
2008-06-13 02:21:55 +04:00
int data_valid ;
} ;
static const struct i2c_device_id fm3130_id [ ] = {
2008-07-13 00:47:55 +04:00
{ " fm3130 " , 0 } ,
2008-06-13 02:21:55 +04:00
{ }
} ;
MODULE_DEVICE_TABLE ( i2c , fm3130_id ) ;
# define FM3130_MODE_NORMAL 0
# define FM3130_MODE_WRITE 1
# define FM3130_MODE_READ 2
static void fm3130_rtc_mode ( struct device * dev , int mode )
{
struct fm3130 * fm3130 = dev_get_drvdata ( dev ) ;
fm3130 - > regs [ FM3130_RTC_CONTROL ] =
i2c_smbus_read_byte_data ( fm3130 - > client , FM3130_RTC_CONTROL ) ;
switch ( mode ) {
case FM3130_MODE_NORMAL :
fm3130 - > regs [ FM3130_RTC_CONTROL ] & =
~ ( FM3130_RTC_CONTROL_BIT_WRITE |
FM3130_RTC_CONTROL_BIT_READ ) ;
break ;
case FM3130_MODE_WRITE :
fm3130 - > regs [ FM3130_RTC_CONTROL ] | = FM3130_RTC_CONTROL_BIT_WRITE ;
break ;
case FM3130_MODE_READ :
fm3130 - > regs [ FM3130_RTC_CONTROL ] | = FM3130_RTC_CONTROL_BIT_READ ;
break ;
default :
dev_dbg ( dev , " invalid mode %d \n " , mode ) ;
break ;
}
2010-08-11 05:02:10 +04:00
2008-06-13 02:21:55 +04:00
i2c_smbus_write_byte_data ( fm3130 - > client ,
FM3130_RTC_CONTROL , fm3130 - > regs [ FM3130_RTC_CONTROL ] ) ;
}
static int fm3130_get_time ( struct device * dev , struct rtc_time * t )
{
struct fm3130 * fm3130 = dev_get_drvdata ( dev ) ;
int tmp ;
if ( ! fm3130 - > data_valid ) {
/* We have invalid data in RTC, probably due
to battery faults or other problems . Return EIO
2010-06-11 14:16:56 +04:00
for now , it will allow us to set data later instead
2008-06-13 02:21:55 +04:00
of error during probing which disables device */
return - EIO ;
}
fm3130_rtc_mode ( dev , FM3130_MODE_READ ) ;
/* read the RTC date and time registers all at once */
2019-06-08 13:56:05 +03:00
tmp = i2c_transfer ( fm3130 - > client - > adapter , fm3130 - > msg , 2 ) ;
2008-06-13 02:21:55 +04:00
if ( tmp ! = 2 ) {
dev_err ( dev , " %s error %d \n " , " read " , tmp ) ;
return - EIO ;
}
fm3130_rtc_mode ( dev , FM3130_MODE_NORMAL ) ;
2013-02-22 04:44:22 +04:00
dev_dbg ( dev , " %s: %15ph \n " , " read " , fm3130 - > regs ) ;
2008-06-13 02:21:55 +04:00
2008-10-19 07:28:41 +04:00
t - > tm_sec = bcd2bin ( fm3130 - > regs [ FM3130_RTC_SECONDS ] & 0x7f ) ;
t - > tm_min = bcd2bin ( fm3130 - > regs [ FM3130_RTC_MINUTES ] & 0x7f ) ;
2008-06-13 02:21:55 +04:00
tmp = fm3130 - > regs [ FM3130_RTC_HOURS ] & 0x3f ;
2008-10-19 07:28:41 +04:00
t - > tm_hour = bcd2bin ( tmp ) ;
t - > tm_wday = bcd2bin ( fm3130 - > regs [ FM3130_RTC_DAY ] & 0x07 ) - 1 ;
t - > tm_mday = bcd2bin ( fm3130 - > regs [ FM3130_RTC_DATE ] & 0x3f ) ;
2008-06-13 02:21:55 +04:00
tmp = fm3130 - > regs [ FM3130_RTC_MONTHS ] & 0x1f ;
2008-10-19 07:28:41 +04:00
t - > tm_mon = bcd2bin ( tmp ) - 1 ;
2008-06-13 02:21:55 +04:00
/* assume 20YY not 19YY, and ignore CF bit */
2008-10-19 07:28:41 +04:00
t - > tm_year = bcd2bin ( fm3130 - > regs [ FM3130_RTC_YEARS ] ) + 100 ;
2008-06-13 02:21:55 +04:00
dev_dbg ( dev , " %s secs=%d, mins=%d, "
" hours=%d, mday=%d, mon=%d, year=%d, wday=%d \n " ,
" read " , t - > tm_sec , t - > tm_min ,
t - > tm_hour , t - > tm_mday ,
t - > tm_mon , t - > tm_year , t - > tm_wday ) ;
2018-02-19 18:23:56 +03:00
return 0 ;
2008-06-13 02:21:55 +04:00
}
static int fm3130_set_time ( struct device * dev , struct rtc_time * t )
{
struct fm3130 * fm3130 = dev_get_drvdata ( dev ) ;
int tmp , i ;
u8 * buf = fm3130 - > regs ;
dev_dbg ( dev , " %s secs=%d, mins=%d, "
" hours=%d, mday=%d, mon=%d, year=%d, wday=%d \n " ,
" write " , t - > tm_sec , t - > tm_min ,
t - > tm_hour , t - > tm_mday ,
t - > tm_mon , t - > tm_year , t - > tm_wday ) ;
/* first register addr */
2008-10-19 07:28:41 +04:00
buf [ FM3130_RTC_SECONDS ] = bin2bcd ( t - > tm_sec ) ;
buf [ FM3130_RTC_MINUTES ] = bin2bcd ( t - > tm_min ) ;
buf [ FM3130_RTC_HOURS ] = bin2bcd ( t - > tm_hour ) ;
buf [ FM3130_RTC_DAY ] = bin2bcd ( t - > tm_wday + 1 ) ;
buf [ FM3130_RTC_DATE ] = bin2bcd ( t - > tm_mday ) ;
buf [ FM3130_RTC_MONTHS ] = bin2bcd ( t - > tm_mon + 1 ) ;
2008-06-13 02:21:55 +04:00
/* assume 20YY not 19YY */
tmp = t - > tm_year - 100 ;
2008-10-19 07:28:41 +04:00
buf [ FM3130_RTC_YEARS ] = bin2bcd ( tmp ) ;
2008-06-13 02:21:55 +04:00
2013-02-22 04:44:22 +04:00
dev_dbg ( dev , " %s: %15ph \n " , " write " , buf ) ;
2008-06-13 02:21:55 +04:00
fm3130_rtc_mode ( dev , FM3130_MODE_WRITE ) ;
/* Writing time registers, we don't support multibyte transfers */
for ( i = 0 ; i < FM3130_CLOCK_REGS ; i + + ) {
i2c_smbus_write_byte_data ( fm3130 - > client ,
FM3130_RTC_SECONDS + i ,
fm3130 - > regs [ FM3130_RTC_SECONDS + i ] ) ;
}
fm3130_rtc_mode ( dev , FM3130_MODE_NORMAL ) ;
/* We assume here that data are valid once written */
if ( ! fm3130 - > data_valid )
fm3130 - > data_valid = 1 ;
return 0 ;
}
static int fm3130_read_alarm ( struct device * dev , struct rtc_wkalrm * alrm )
{
struct fm3130 * fm3130 = dev_get_drvdata ( dev ) ;
int tmp ;
struct rtc_time * tm = & alrm - > time ;
2010-08-11 05:02:10 +04:00
if ( ! fm3130 - > alarm_valid ) {
/*
* We have invalid alarm in RTC , probably due to battery faults
* or other problems . Return EIO for now , it will allow us to
* set alarm value later instead of error during probing which
* disables device
*/
return - EIO ;
}
2008-06-13 02:21:55 +04:00
/* read the RTC alarm registers all at once */
2019-06-08 13:56:05 +03:00
tmp = i2c_transfer ( fm3130 - > client - > adapter , & fm3130 - > msg [ 2 ] , 2 ) ;
2008-06-13 02:21:55 +04:00
if ( tmp ! = 2 ) {
dev_err ( dev , " %s error %d \n " , " read " , tmp ) ;
return - EIO ;
}
dev_dbg ( dev , " alarm read %02x %02x %02x %02x %02x \n " ,
fm3130 - > regs [ FM3130_ALARM_SECONDS ] ,
fm3130 - > regs [ FM3130_ALARM_MINUTES ] ,
fm3130 - > regs [ FM3130_ALARM_HOURS ] ,
fm3130 - > regs [ FM3130_ALARM_DATE ] ,
fm3130 - > regs [ FM3130_ALARM_MONTHS ] ) ;
2008-10-19 07:28:41 +04:00
tm - > tm_sec = bcd2bin ( fm3130 - > regs [ FM3130_ALARM_SECONDS ] & 0x7F ) ;
tm - > tm_min = bcd2bin ( fm3130 - > regs [ FM3130_ALARM_MINUTES ] & 0x7F ) ;
tm - > tm_hour = bcd2bin ( fm3130 - > regs [ FM3130_ALARM_HOURS ] & 0x3F ) ;
tm - > tm_mday = bcd2bin ( fm3130 - > regs [ FM3130_ALARM_DATE ] & 0x3F ) ;
tm - > tm_mon = bcd2bin ( fm3130 - > regs [ FM3130_ALARM_MONTHS ] & 0x1F ) ;
2010-08-11 05:02:10 +04:00
2008-06-13 02:21:55 +04:00
if ( tm - > tm_mon > 0 )
tm - > tm_mon - = 1 ; /* RTC is 1-12, tm_mon is 0-11 */
2010-08-11 05:02:10 +04:00
2008-06-13 02:21:55 +04:00
dev_dbg ( dev , " %s secs=%d, mins=%d, "
" hours=%d, mday=%d, mon=%d, year=%d, wday=%d \n " ,
" read alarm " , tm - > tm_sec , tm - > tm_min ,
tm - > tm_hour , tm - > tm_mday ,
tm - > tm_mon , tm - > tm_year , tm - > tm_wday ) ;
2010-08-11 05:02:10 +04:00
/* check if alarm enabled */
fm3130 - > regs [ FM3130_RTC_CONTROL ] =
i2c_smbus_read_byte_data ( fm3130 - > client , FM3130_RTC_CONTROL ) ;
if ( ( fm3130 - > regs [ FM3130_RTC_CONTROL ] & FM3130_RTC_CONTROL_BIT_AEN ) & &
( ~ fm3130 - > regs [ FM3130_RTC_CONTROL ] &
FM3130_RTC_CONTROL_BIT_CAL ) ) {
alrm - > enabled = 1 ;
}
2008-06-13 02:21:55 +04:00
return 0 ;
}
static int fm3130_set_alarm ( struct device * dev , struct rtc_wkalrm * alrm )
{
struct fm3130 * fm3130 = dev_get_drvdata ( dev ) ;
struct rtc_time * tm = & alrm - > time ;
int i ;
dev_dbg ( dev , " %s secs=%d, mins=%d, "
" hours=%d, mday=%d, mon=%d, year=%d, wday=%d \n " ,
" write alarm " , tm - > tm_sec , tm - > tm_min ,
tm - > tm_hour , tm - > tm_mday ,
tm - > tm_mon , tm - > tm_year , tm - > tm_wday ) ;
2010-08-11 05:02:10 +04:00
fm3130 - > regs [ FM3130_ALARM_SECONDS ] =
( tm - > tm_sec ! = - 1 ) ? bin2bcd ( tm - > tm_sec ) : 0x80 ;
2008-06-13 02:21:55 +04:00
2010-08-11 05:02:10 +04:00
fm3130 - > regs [ FM3130_ALARM_MINUTES ] =
( tm - > tm_min ! = - 1 ) ? bin2bcd ( tm - > tm_min ) : 0x80 ;
2008-06-13 02:21:55 +04:00
2010-08-11 05:02:10 +04:00
fm3130 - > regs [ FM3130_ALARM_HOURS ] =
( tm - > tm_hour ! = - 1 ) ? bin2bcd ( tm - > tm_hour ) : 0x80 ;
2008-06-13 02:21:55 +04:00
2010-08-11 05:02:10 +04:00
fm3130 - > regs [ FM3130_ALARM_DATE ] =
( tm - > tm_mday ! = - 1 ) ? bin2bcd ( tm - > tm_mday ) : 0x80 ;
2008-06-13 02:21:55 +04:00
2010-08-11 05:02:10 +04:00
fm3130 - > regs [ FM3130_ALARM_MONTHS ] =
( tm - > tm_mon ! = - 1 ) ? bin2bcd ( tm - > tm_mon + 1 ) : 0x80 ;
2008-06-13 02:21:55 +04:00
dev_dbg ( dev , " alarm write %02x %02x %02x %02x %02x \n " ,
fm3130 - > regs [ FM3130_ALARM_SECONDS ] ,
fm3130 - > regs [ FM3130_ALARM_MINUTES ] ,
fm3130 - > regs [ FM3130_ALARM_HOURS ] ,
fm3130 - > regs [ FM3130_ALARM_DATE ] ,
fm3130 - > regs [ FM3130_ALARM_MONTHS ] ) ;
/* Writing time registers, we don't support multibyte transfers */
for ( i = 0 ; i < FM3130_ALARM_REGS ; i + + ) {
i2c_smbus_write_byte_data ( fm3130 - > client ,
FM3130_ALARM_SECONDS + i ,
fm3130 - > regs [ FM3130_ALARM_SECONDS + i ] ) ;
}
fm3130 - > regs [ FM3130_RTC_CONTROL ] =
i2c_smbus_read_byte_data ( fm3130 - > client , FM3130_RTC_CONTROL ) ;
2010-08-11 05:02:10 +04:00
/* enable or disable alarm */
2008-06-13 02:21:55 +04:00
if ( alrm - > enabled ) {
i2c_smbus_write_byte_data ( fm3130 - > client , FM3130_RTC_CONTROL ,
( fm3130 - > regs [ FM3130_RTC_CONTROL ] &
~ ( FM3130_RTC_CONTROL_BIT_CAL ) ) |
FM3130_RTC_CONTROL_BIT_AEN ) ;
} else {
i2c_smbus_write_byte_data ( fm3130 - > client , FM3130_RTC_CONTROL ,
fm3130 - > regs [ FM3130_RTC_CONTROL ] &
2010-08-11 05:02:10 +04:00
~ ( FM3130_RTC_CONTROL_BIT_CAL ) &
~ ( FM3130_RTC_CONTROL_BIT_AEN ) ) ;
2008-06-13 02:21:55 +04:00
}
2010-08-11 05:02:10 +04:00
/* We assume here that data is valid once written */
if ( ! fm3130 - > alarm_valid )
fm3130 - > alarm_valid = 1 ;
2008-06-13 02:21:55 +04:00
return 0 ;
}
2010-08-11 05:02:10 +04:00
static int fm3130_alarm_irq_enable ( struct device * dev , unsigned int enabled )
{
struct fm3130 * fm3130 = dev_get_drvdata ( dev ) ;
int ret = 0 ;
fm3130 - > regs [ FM3130_RTC_CONTROL ] =
i2c_smbus_read_byte_data ( fm3130 - > client , FM3130_RTC_CONTROL ) ;
dev_dbg ( dev , " alarm_irq_enable: enable=%d, FM3130_RTC_CONTROL=%02x \n " ,
enabled , fm3130 - > regs [ FM3130_RTC_CONTROL ] ) ;
switch ( enabled ) {
case 0 : /* alarm off */
ret = i2c_smbus_write_byte_data ( fm3130 - > client ,
FM3130_RTC_CONTROL , fm3130 - > regs [ FM3130_RTC_CONTROL ] &
~ ( FM3130_RTC_CONTROL_BIT_CAL ) &
~ ( FM3130_RTC_CONTROL_BIT_AEN ) ) ;
break ;
case 1 : /* alarm on */
ret = i2c_smbus_write_byte_data ( fm3130 - > client ,
FM3130_RTC_CONTROL , ( fm3130 - > regs [ FM3130_RTC_CONTROL ] &
~ ( FM3130_RTC_CONTROL_BIT_CAL ) ) |
FM3130_RTC_CONTROL_BIT_AEN ) ;
break ;
default :
ret = - EINVAL ;
break ;
}
return ret ;
}
2008-06-13 02:21:55 +04:00
static const struct rtc_class_ops fm3130_rtc_ops = {
. read_time = fm3130_get_time ,
. set_time = fm3130_set_time ,
. read_alarm = fm3130_read_alarm ,
. set_alarm = fm3130_set_alarm ,
2010-08-11 05:02:10 +04:00
. alarm_irq_enable = fm3130_alarm_irq_enable ,
2008-06-13 02:21:55 +04:00
} ;
static struct i2c_driver fm3130_driver ;
2022-06-10 19:23:43 +03:00
static int fm3130_probe ( struct i2c_client * client )
2008-06-13 02:21:55 +04:00
{
struct fm3130 * fm3130 ;
int err = - ENODEV ;
int tmp ;
2019-06-08 13:56:05 +03:00
struct i2c_adapter * adapter = client - > adapter ;
2008-06-13 02:21:55 +04:00
if ( ! i2c_check_functionality ( adapter ,
I2C_FUNC_I2C | I2C_FUNC_SMBUS_WRITE_BYTE_DATA ) )
return - EIO ;
2013-04-30 03:20:41 +04:00
fm3130 = devm_kzalloc ( & client - > dev , sizeof ( struct fm3130 ) , GFP_KERNEL ) ;
2008-06-13 02:21:55 +04:00
if ( ! fm3130 )
return - ENOMEM ;
fm3130 - > client = client ;
i2c_set_clientdata ( client , fm3130 ) ;
fm3130 - > reg_addr_time = FM3130_RTC_SECONDS ;
fm3130 - > reg_addr_alarm = FM3130_ALARM_SECONDS ;
/* Messages to read time */
fm3130 - > msg [ 0 ] . addr = client - > addr ;
fm3130 - > msg [ 0 ] . flags = 0 ;
fm3130 - > msg [ 0 ] . len = 1 ;
fm3130 - > msg [ 0 ] . buf = & fm3130 - > reg_addr_time ;
fm3130 - > msg [ 1 ] . addr = client - > addr ;
fm3130 - > msg [ 1 ] . flags = I2C_M_RD ;
fm3130 - > msg [ 1 ] . len = FM3130_CLOCK_REGS ;
fm3130 - > msg [ 1 ] . buf = & fm3130 - > regs [ FM3130_RTC_SECONDS ] ;
/* Messages to read alarm */
fm3130 - > msg [ 2 ] . addr = client - > addr ;
fm3130 - > msg [ 2 ] . flags = 0 ;
fm3130 - > msg [ 2 ] . len = 1 ;
fm3130 - > msg [ 2 ] . buf = & fm3130 - > reg_addr_alarm ;
fm3130 - > msg [ 3 ] . addr = client - > addr ;
fm3130 - > msg [ 3 ] . flags = I2C_M_RD ;
fm3130 - > msg [ 3 ] . len = FM3130_ALARM_REGS ;
fm3130 - > msg [ 3 ] . buf = & fm3130 - > regs [ FM3130_ALARM_SECONDS ] ;
2010-08-11 05:02:10 +04:00
fm3130 - > alarm_valid = 0 ;
2008-06-13 02:21:55 +04:00
fm3130 - > data_valid = 0 ;
tmp = i2c_transfer ( adapter , fm3130 - > msg , 4 ) ;
if ( tmp ! = 4 ) {
2013-04-30 03:19:29 +04:00
dev_dbg ( & client - > dev , " read error %d \n " , tmp ) ;
2008-06-13 02:21:55 +04:00
err = - EIO ;
goto exit_free ;
}
fm3130 - > regs [ FM3130_RTC_CONTROL ] =
i2c_smbus_read_byte_data ( client , FM3130_RTC_CONTROL ) ;
fm3130 - > regs [ FM3130_CAL_CONTROL ] =
i2c_smbus_read_byte_data ( client , FM3130_CAL_CONTROL ) ;
/* Disabling calibration mode */
2010-02-03 00:43:59 +03:00
if ( fm3130 - > regs [ FM3130_RTC_CONTROL ] & FM3130_RTC_CONTROL_BIT_CAL ) {
2008-06-13 02:21:55 +04:00
i2c_smbus_write_byte_data ( client , FM3130_RTC_CONTROL ,
fm3130 - > regs [ FM3130_RTC_CONTROL ] &
~ ( FM3130_RTC_CONTROL_BIT_CAL ) ) ;
dev_warn ( & client - > dev , " Disabling calibration mode! \n " ) ;
2010-02-03 00:43:59 +03:00
}
2008-06-13 02:21:55 +04:00
/* Disabling read and write modes */
if ( fm3130 - > regs [ FM3130_RTC_CONTROL ] & FM3130_RTC_CONTROL_BIT_WRITE | |
2010-02-03 00:43:59 +03:00
fm3130 - > regs [ FM3130_RTC_CONTROL ] & FM3130_RTC_CONTROL_BIT_READ ) {
2008-06-13 02:21:55 +04:00
i2c_smbus_write_byte_data ( client , FM3130_RTC_CONTROL ,
fm3130 - > regs [ FM3130_RTC_CONTROL ] &
~ ( FM3130_RTC_CONTROL_BIT_READ |
FM3130_RTC_CONTROL_BIT_WRITE ) ) ;
dev_warn ( & client - > dev , " Disabling READ or WRITE mode! \n " ) ;
2010-02-03 00:43:59 +03:00
}
2008-06-13 02:21:55 +04:00
/* oscillator off? turn it on, so clock can tick. */
if ( fm3130 - > regs [ FM3130_CAL_CONTROL ] & FM3130_CAL_CONTROL_BIT_nOSCEN )
i2c_smbus_write_byte_data ( client , FM3130_CAL_CONTROL ,
fm3130 - > regs [ FM3130_CAL_CONTROL ] &
~ ( FM3130_CAL_CONTROL_BIT_nOSCEN ) ) ;
2010-08-11 05:02:10 +04:00
/* low battery? clear flag, and warn */
if ( fm3130 - > regs [ FM3130_RTC_CONTROL ] & FM3130_RTC_CONTROL_BIT_LB ) {
i2c_smbus_write_byte_data ( client , FM3130_RTC_CONTROL ,
fm3130 - > regs [ FM3130_RTC_CONTROL ] &
~ ( FM3130_RTC_CONTROL_BIT_LB ) ) ;
2008-06-13 02:21:55 +04:00
dev_warn ( & client - > dev , " Low battery! \n " ) ;
2010-08-11 05:02:10 +04:00
}
2008-06-13 02:21:55 +04:00
2010-08-11 05:02:10 +04:00
/* check if Power On Reset bit is set */
2008-06-13 02:21:55 +04:00
if ( fm3130 - > regs [ FM3130_RTC_CONTROL ] & FM3130_RTC_CONTROL_BIT_POR ) {
i2c_smbus_write_byte_data ( client , FM3130_RTC_CONTROL ,
fm3130 - > regs [ FM3130_RTC_CONTROL ] &
~ FM3130_RTC_CONTROL_BIT_POR ) ;
2010-08-11 05:02:10 +04:00
dev_dbg ( & client - > dev , " POR bit is set \n " ) ;
2008-06-13 02:21:55 +04:00
}
/* ACS is controlled by alarm */
i2c_smbus_write_byte_data ( client , FM3130_ALARM_WP_CONTROL , 0x80 ) ;
2010-08-11 05:02:10 +04:00
/* alarm registers sanity check */
tmp = bcd2bin ( fm3130 - > regs [ FM3130_RTC_SECONDS ] & 0x7f ) ;
if ( tmp > 59 )
goto bad_alarm ;
2008-10-19 07:28:41 +04:00
tmp = bcd2bin ( fm3130 - > regs [ FM3130_RTC_MINUTES ] & 0x7f ) ;
2010-08-11 05:02:10 +04:00
if ( tmp > 59 )
goto bad_alarm ;
tmp = bcd2bin ( fm3130 - > regs [ FM3130_RTC_HOURS ] & 0x3f ) ;
if ( tmp > 23 )
goto bad_alarm ;
2008-06-13 02:21:55 +04:00
2008-10-19 07:28:41 +04:00
tmp = bcd2bin ( fm3130 - > regs [ FM3130_RTC_DATE ] & 0x3f ) ;
2008-06-13 02:21:55 +04:00
if ( tmp = = 0 | | tmp > 31 )
2010-08-11 05:02:10 +04:00
goto bad_alarm ;
2008-06-13 02:21:55 +04:00
2008-10-19 07:28:41 +04:00
tmp = bcd2bin ( fm3130 - > regs [ FM3130_RTC_MONTHS ] & 0x1f ) ;
2008-06-13 02:21:55 +04:00
if ( tmp = = 0 | | tmp > 12 )
2010-08-11 05:02:10 +04:00
goto bad_alarm ;
2008-06-13 02:21:55 +04:00
2010-08-11 05:02:10 +04:00
fm3130 - > alarm_valid = 1 ;
bad_alarm :
/* clock registers sanity chek */
tmp = bcd2bin ( fm3130 - > regs [ FM3130_RTC_SECONDS ] & 0x7f ) ;
if ( tmp > 59 )
goto bad_clock ;
tmp = bcd2bin ( fm3130 - > regs [ FM3130_RTC_MINUTES ] & 0x7f ) ;
if ( tmp > 59 )
goto bad_clock ;
tmp = bcd2bin ( fm3130 - > regs [ FM3130_RTC_HOURS ] & 0x3f ) ;
if ( tmp > 23 )
goto bad_clock ;
tmp = bcd2bin ( fm3130 - > regs [ FM3130_RTC_DAY ] & 0x7 ) ;
if ( tmp = = 0 | | tmp > 7 )
goto bad_clock ;
tmp = bcd2bin ( fm3130 - > regs [ FM3130_RTC_DATE ] & 0x3f ) ;
if ( tmp = = 0 | | tmp > 31 )
goto bad_clock ;
tmp = bcd2bin ( fm3130 - > regs [ FM3130_RTC_MONTHS ] & 0x1f ) ;
if ( tmp = = 0 | | tmp > 12 )
goto bad_clock ;
2008-06-13 02:21:55 +04:00
fm3130 - > data_valid = 1 ;
2010-08-11 05:02:10 +04:00
bad_clock :
if ( ! fm3130 - > data_valid | | ! fm3130 - > alarm_valid )
2013-02-22 04:44:22 +04:00
dev_dbg ( & client - > dev , " %s: %15ph \n " , " bogus registers " ,
fm3130 - > regs ) ;
2008-06-13 02:21:55 +04:00
/* We won't bail out here because we just got invalid data.
Time setting from u - boot doesn ' t work anyway */
2013-04-30 03:20:41 +04:00
fm3130 - > rtc = devm_rtc_device_register ( & client - > dev , client - > name ,
2008-06-13 02:21:55 +04:00
& fm3130_rtc_ops , THIS_MODULE ) ;
if ( IS_ERR ( fm3130 - > rtc ) ) {
err = PTR_ERR ( fm3130 - > rtc ) ;
dev_err ( & client - > dev ,
" unable to register the class device \n " ) ;
goto exit_free ;
}
return 0 ;
exit_free :
return err ;
}
static struct i2c_driver fm3130_driver = {
. driver = {
. name = " rtc-fm3130 " ,
} ,
2022-06-10 19:23:43 +03:00
. probe_new = fm3130_probe ,
2008-06-13 02:21:55 +04:00
. id_table = fm3130_id ,
} ;
2012-03-24 02:02:31 +04:00
module_i2c_driver ( fm3130_driver ) ;
2008-06-13 02:21:55 +04:00
MODULE_DESCRIPTION ( " RTC driver for FM3130 " ) ;
MODULE_AUTHOR ( " Sergey Lapin <slapin@ossfans.org> " ) ;
MODULE_LICENSE ( " GPL " ) ;