2019-04-01 19:33:49 +03:00
// SPDX-License-Identifier: GPL-2.0
2014-08-09 01:20:22 +04:00
/*
* An I2C driver for the PCF85063 RTC
* Copyright 2014 Rose Technology
*
* Author : Søren Andersen < san @ rosetechnology . dk >
* Maintainers : http : //www.nslu2-linux.org/
*/
# include <linux/i2c.h>
# include <linux/bcd.h>
# include <linux/rtc.h>
# include <linux/module.h>
2019-04-01 19:08:10 +03:00
# include <linux/of_device.h>
# include <linux/regmap.h>
2014-08-09 01:20:22 +04:00
2016-07-13 00:15:46 +03:00
/*
* Information for this driver was pulled from the following datasheets .
*
* http : //www.nxp.com/documents/data_sheet/PCF85063A.pdf
* http : //www.nxp.com/documents/data_sheet/PCF85063TP.pdf
*
* PCF85063A - - Rev . 6 — 18 November 2015
* PCF85063TP - - Rev . 4 — 6 May 2015
*/
2014-08-09 01:20:22 +04:00
# define PCF85063_REG_CTRL1 0x00 /* status */
2019-01-19 12:00:31 +03:00
# define PCF85063_REG_CTRL1_CAP_SEL BIT(0)
2016-02-09 13:57:27 +03:00
# define PCF85063_REG_CTRL1_STOP BIT(5)
2014-08-09 01:20:22 +04:00
# define PCF85063_REG_SC 0x04 /* datetime */
2016-02-09 13:57:26 +03:00
# define PCF85063_REG_SC_OS 0x80
2014-08-09 01:20:22 +04:00
2019-04-01 19:08:11 +03:00
struct pcf85063_config {
struct regmap_config regmap ;
} ;
2019-04-01 19:08:10 +03:00
struct pcf85063 {
struct rtc_device * rtc ;
struct regmap * regmap ;
} ;
2016-07-13 00:15:46 +03:00
2018-02-21 18:09:27 +03:00
static int pcf85063_rtc_read_time ( struct device * dev , struct rtc_time * tm )
2014-08-09 01:20:22 +04:00
{
2019-04-01 19:08:10 +03:00
struct pcf85063 * pcf85063 = dev_get_drvdata ( dev ) ;
2016-02-09 13:57:25 +03:00
int rc ;
u8 regs [ 7 ] ;
/*
* while reading , the time / date registers are blocked and not updated
* anymore until the access is finished . To not lose a second
* event , the access must be finished within one second . So , read all
* time / date registers in one turn .
*/
2019-04-01 19:08:10 +03:00
rc = regmap_bulk_read ( pcf85063 - > regmap , PCF85063_REG_SC , regs ,
sizeof ( regs ) ) ;
if ( rc )
return rc ;
2014-08-09 01:20:22 +04:00
2016-02-09 13:57:26 +03:00
/* if the clock has lost its power it makes no sense to use its time */
if ( regs [ 0 ] & PCF85063_REG_SC_OS ) {
2019-04-01 19:08:10 +03:00
dev_warn ( & pcf85063 - > rtc - > dev , " Power loss detected, invalid time \n " ) ;
2016-02-09 13:57:26 +03:00
return - EINVAL ;
}
2016-02-09 13:57:25 +03:00
tm - > tm_sec = bcd2bin ( regs [ 0 ] & 0x7F ) ;
tm - > tm_min = bcd2bin ( regs [ 1 ] & 0x7F ) ;
tm - > tm_hour = bcd2bin ( regs [ 2 ] & 0x3F ) ; /* rtc hr 0-23 */
tm - > tm_mday = bcd2bin ( regs [ 3 ] & 0x3F ) ;
tm - > tm_wday = regs [ 4 ] & 0x07 ;
tm - > tm_mon = bcd2bin ( regs [ 5 ] & 0x1F ) - 1 ; /* rtc mn 1-12 */
tm - > tm_year = bcd2bin ( regs [ 6 ] ) ;
2016-07-18 12:08:59 +03:00
tm - > tm_year + = 100 ;
2014-08-09 01:20:22 +04:00
2018-02-21 18:07:34 +03:00
return 0 ;
2014-08-09 01:20:22 +04:00
}
2018-02-21 18:09:27 +03:00
static int pcf85063_rtc_set_time ( struct device * dev , struct rtc_time * tm )
2014-08-09 01:20:22 +04:00
{
2019-04-01 19:08:10 +03:00
struct pcf85063 * pcf85063 = dev_get_drvdata ( dev ) ;
2016-02-09 13:57:27 +03:00
int rc ;
2016-07-13 00:15:46 +03:00
u8 regs [ 7 ] ;
2014-08-09 01:20:22 +04:00
2016-02-09 13:57:27 +03:00
/*
* to accurately set the time , reset the divider chain and keep it in
* reset state until all time / date registers are written
*/
2019-04-01 19:08:10 +03:00
rc = regmap_update_bits ( pcf85063 - > regmap , PCF85063_REG_CTRL1 ,
PCF85063_REG_CTRL1_STOP ,
PCF85063_REG_CTRL1_STOP ) ;
if ( rc )
2016-02-09 13:57:27 +03:00
return rc ;
2014-08-09 01:20:22 +04:00
/* hours, minutes and seconds */
2016-02-09 13:57:27 +03:00
regs [ 0 ] = bin2bcd ( tm - > tm_sec ) & 0x7F ; /* clear OS flag */
2014-08-09 01:20:22 +04:00
2016-02-09 13:57:27 +03:00
regs [ 1 ] = bin2bcd ( tm - > tm_min ) ;
regs [ 2 ] = bin2bcd ( tm - > tm_hour ) ;
2014-08-09 01:20:22 +04:00
/* Day of month, 1 - 31 */
2016-02-09 13:57:27 +03:00
regs [ 3 ] = bin2bcd ( tm - > tm_mday ) ;
2014-08-09 01:20:22 +04:00
/* Day, 0 - 6 */
2016-02-09 13:57:27 +03:00
regs [ 4 ] = tm - > tm_wday & 0x07 ;
2014-08-09 01:20:22 +04:00
/* month, 1 - 12 */
2016-02-09 13:57:27 +03:00
regs [ 5 ] = bin2bcd ( tm - > tm_mon + 1 ) ;
2014-08-09 01:20:22 +04:00
/* year and century */
2016-07-18 12:08:59 +03:00
regs [ 6 ] = bin2bcd ( tm - > tm_year - 100 ) ;
2016-02-09 13:57:27 +03:00
/* write all registers at once */
2019-04-01 19:08:10 +03:00
rc = regmap_bulk_write ( pcf85063 - > regmap , PCF85063_REG_SC ,
regs , sizeof ( regs ) ) ;
if ( rc )
2016-02-09 13:57:27 +03:00
return rc ;
2014-08-09 01:20:22 +04:00
2016-07-13 00:15:46 +03:00
/*
* Write the control register as a separate action since the size of
* the register space is different between the PCF85063TP and
* PCF85063A devices . The rollover point can not be used .
*/
2019-04-01 19:08:10 +03:00
return regmap_update_bits ( pcf85063 - > regmap , PCF85063_REG_CTRL1 ,
PCF85063_REG_CTRL1_STOP , 0 ) ;
2014-08-09 01:20:22 +04:00
}
static const struct rtc_class_ops pcf85063_rtc_ops = {
. read_time = pcf85063_rtc_read_time ,
. set_time = pcf85063_rtc_set_time
} ;
2019-04-01 19:08:10 +03:00
static int pcf85063_load_capacitance ( struct pcf85063 * pcf85063 ,
const struct device_node * np )
2019-01-19 12:00:31 +03:00
{
2019-04-01 19:08:10 +03:00
u32 load = 7000 ;
u8 reg = 0 ;
2019-01-19 12:00:31 +03:00
2019-04-01 19:08:10 +03:00
of_property_read_u32 ( np , " quartz-load-femtofarads " , & load ) ;
2019-01-19 12:00:31 +03:00
switch ( load ) {
default :
2019-04-01 19:08:10 +03:00
dev_warn ( & pcf85063 - > rtc - > dev , " Unknown quartz-load-femtofarads value: %d. Assuming 7000 " ,
2019-01-19 12:00:31 +03:00
load ) ;
/* fall through */
case 7000 :
break ;
case 12500 :
2019-04-01 19:08:10 +03:00
reg = PCF85063_REG_CTRL1_CAP_SEL ;
2019-01-19 12:00:31 +03:00
break ;
}
2019-04-01 19:08:10 +03:00
return regmap_update_bits ( pcf85063 - > regmap , PCF85063_REG_CTRL1 ,
PCF85063_REG_CTRL1_CAP_SEL , reg ) ;
2019-01-19 12:00:31 +03:00
}
2019-04-01 19:08:11 +03:00
static const struct pcf85063_config pcf85063a_config = {
. regmap = {
. reg_bits = 8 ,
. val_bits = 8 ,
. max_register = 0x11 ,
} ,
} ;
static const struct pcf85063_config pcf85063tp_config = {
. regmap = {
. reg_bits = 8 ,
. val_bits = 8 ,
. max_register = 0x0a ,
} ,
2019-04-01 19:08:10 +03:00
} ;
2019-04-01 19:08:05 +03:00
static int pcf85063_probe ( struct i2c_client * client )
2014-08-09 01:20:22 +04:00
{
2019-04-01 19:08:10 +03:00
struct pcf85063 * pcf85063 ;
unsigned int tmp ;
2016-10-17 16:53:31 +03:00
int err ;
2019-04-01 19:08:11 +03:00
const struct pcf85063_config * config = & pcf85063tp_config ;
const void * data = of_device_get_match_data ( & client - > dev ) ;
2014-08-09 01:20:22 +04:00
dev_dbg ( & client - > dev , " %s \n " , __func__ ) ;
2019-04-01 19:08:10 +03:00
pcf85063 = devm_kzalloc ( & client - > dev , sizeof ( struct pcf85063 ) ,
GFP_KERNEL ) ;
if ( ! pcf85063 )
return - ENOMEM ;
2019-04-01 19:08:11 +03:00
if ( data )
config = data ;
pcf85063 - > regmap = devm_regmap_init_i2c ( client , & config - > regmap ) ;
2019-04-01 19:08:10 +03:00
if ( IS_ERR ( pcf85063 - > regmap ) )
return PTR_ERR ( pcf85063 - > regmap ) ;
i2c_set_clientdata ( client , pcf85063 ) ;
err = regmap_read ( pcf85063 - > regmap , PCF85063_REG_CTRL1 , & tmp ) ;
if ( err ) {
2016-10-17 16:53:31 +03:00
dev_err ( & client - > dev , " RTC chip is not present \n " ) ;
return err ;
}
2019-04-01 19:08:10 +03:00
pcf85063 - > rtc = devm_rtc_allocate_device ( & client - > dev ) ;
if ( IS_ERR ( pcf85063 - > rtc ) )
return PTR_ERR ( pcf85063 - > rtc ) ;
err = pcf85063_load_capacitance ( pcf85063 , client - > dev . of_node ) ;
2019-01-19 12:00:31 +03:00
if ( err < 0 )
dev_warn ( & client - > dev , " failed to set xtal load capacitance: %d " ,
err ) ;
2019-04-01 19:08:10 +03:00
pcf85063 - > rtc - > ops = & pcf85063_rtc_ops ;
pcf85063 - > rtc - > range_min = RTC_TIMESTAMP_BEGIN_2000 ;
pcf85063 - > rtc - > range_max = RTC_TIMESTAMP_END_2099 ;
2014-08-09 01:20:22 +04:00
2019-04-01 19:08:10 +03:00
return rtc_register_device ( pcf85063 - > rtc ) ;
2014-08-09 01:20:22 +04:00
}
# ifdef CONFIG_OF
static const struct of_device_id pcf85063_of_match [ ] = {
2019-04-01 19:08:11 +03:00
{ . compatible = " nxp,pcf85063 " , . data = & pcf85063tp_config } ,
{ . compatible = " nxp,pcf85063tp " , . data = & pcf85063tp_config } ,
{ . compatible = " nxp,pcf85063a " , . data = & pcf85063a_config } ,
2014-08-09 01:20:22 +04:00
{ }
} ;
MODULE_DEVICE_TABLE ( of , pcf85063_of_match ) ;
# endif
static struct i2c_driver pcf85063_driver = {
. driver = {
. name = " rtc-pcf85063 " ,
. of_match_table = of_match_ptr ( pcf85063_of_match ) ,
} ,
2019-04-01 19:08:05 +03:00
. probe_new = pcf85063_probe ,
2014-08-09 01:20:22 +04:00
} ;
module_i2c_driver ( pcf85063_driver ) ;
MODULE_AUTHOR ( " Søren Andersen <san@rosetechnology.dk> " ) ;
MODULE_DESCRIPTION ( " PCF85063 RTC driver " ) ;
MODULE_LICENSE ( " GPL " ) ;