2011-05-27 07:25:05 +08:00
|
|
|
/*
|
2016-03-05 05:38:45 +08:00
|
|
|
* Micro Crystal RV-3029 rtc class driver
|
2011-05-27 07:25:05 +08:00
|
|
|
*
|
|
|
|
* Author: Gregory Hermant <gregory.hermant@calao-systems.com>
|
2016-03-05 05:40:30 +08:00
|
|
|
* Michael Buesch <m@bues.ch>
|
2011-05-27 07:25:05 +08:00
|
|
|
*
|
|
|
|
* based on previously existing rtc class drivers
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/bcd.h>
|
|
|
|
#include <linux/rtc.h>
|
2016-03-05 05:40:55 +08:00
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
/* Register map */
|
|
|
|
/* control section */
|
2016-03-05 05:38:45 +08:00
|
|
|
#define RV3029_ONOFF_CTRL 0x00
|
2016-03-05 05:39:49 +08:00
|
|
|
#define RV3029_ONOFF_CTRL_WE BIT(0)
|
|
|
|
#define RV3029_ONOFF_CTRL_TE BIT(1)
|
|
|
|
#define RV3029_ONOFF_CTRL_TAR BIT(2)
|
|
|
|
#define RV3029_ONOFF_CTRL_EERE BIT(3)
|
|
|
|
#define RV3029_ONOFF_CTRL_SRON BIT(4)
|
|
|
|
#define RV3029_ONOFF_CTRL_TD0 BIT(5)
|
|
|
|
#define RV3029_ONOFF_CTRL_TD1 BIT(6)
|
|
|
|
#define RV3029_ONOFF_CTRL_CLKINT BIT(7)
|
2016-03-05 05:38:45 +08:00
|
|
|
#define RV3029_IRQ_CTRL 0x01
|
2016-03-05 05:39:49 +08:00
|
|
|
#define RV3029_IRQ_CTRL_AIE BIT(0)
|
|
|
|
#define RV3029_IRQ_CTRL_TIE BIT(1)
|
|
|
|
#define RV3029_IRQ_CTRL_V1IE BIT(2)
|
|
|
|
#define RV3029_IRQ_CTRL_V2IE BIT(3)
|
|
|
|
#define RV3029_IRQ_CTRL_SRIE BIT(4)
|
2016-03-05 05:38:45 +08:00
|
|
|
#define RV3029_IRQ_FLAGS 0x02
|
2016-03-05 05:39:49 +08:00
|
|
|
#define RV3029_IRQ_FLAGS_AF BIT(0)
|
|
|
|
#define RV3029_IRQ_FLAGS_TF BIT(1)
|
|
|
|
#define RV3029_IRQ_FLAGS_V1IF BIT(2)
|
|
|
|
#define RV3029_IRQ_FLAGS_V2IF BIT(3)
|
|
|
|
#define RV3029_IRQ_FLAGS_SRF BIT(4)
|
2016-03-05 05:38:45 +08:00
|
|
|
#define RV3029_STATUS 0x03
|
2016-03-05 05:39:49 +08:00
|
|
|
#define RV3029_STATUS_VLOW1 BIT(2)
|
|
|
|
#define RV3029_STATUS_VLOW2 BIT(3)
|
|
|
|
#define RV3029_STATUS_SR BIT(4)
|
|
|
|
#define RV3029_STATUS_PON BIT(5)
|
|
|
|
#define RV3029_STATUS_EEBUSY BIT(7)
|
2016-03-05 05:38:45 +08:00
|
|
|
#define RV3029_RST_CTRL 0x04
|
2016-03-05 05:39:49 +08:00
|
|
|
#define RV3029_RST_CTRL_SYSR BIT(4)
|
2016-03-05 05:38:45 +08:00
|
|
|
#define RV3029_CONTROL_SECTION_LEN 0x05
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
/* watch section */
|
2016-03-05 05:38:45 +08:00
|
|
|
#define RV3029_W_SEC 0x08
|
|
|
|
#define RV3029_W_MINUTES 0x09
|
|
|
|
#define RV3029_W_HOURS 0x0A
|
2016-03-05 05:39:49 +08:00
|
|
|
#define RV3029_REG_HR_12_24 BIT(6) /* 24h/12h mode */
|
|
|
|
#define RV3029_REG_HR_PM BIT(5) /* PM/AM bit in 12h mode */
|
2016-03-05 05:38:45 +08:00
|
|
|
#define RV3029_W_DATE 0x0B
|
|
|
|
#define RV3029_W_DAYS 0x0C
|
|
|
|
#define RV3029_W_MONTHS 0x0D
|
|
|
|
#define RV3029_W_YEARS 0x0E
|
|
|
|
#define RV3029_WATCH_SECTION_LEN 0x07
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
/* alarm section */
|
2016-03-05 05:38:45 +08:00
|
|
|
#define RV3029_A_SC 0x10
|
|
|
|
#define RV3029_A_MN 0x11
|
|
|
|
#define RV3029_A_HR 0x12
|
|
|
|
#define RV3029_A_DT 0x13
|
|
|
|
#define RV3029_A_DW 0x14
|
|
|
|
#define RV3029_A_MO 0x15
|
|
|
|
#define RV3029_A_YR 0x16
|
|
|
|
#define RV3029_ALARM_SECTION_LEN 0x07
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
/* timer section */
|
2016-03-05 05:38:45 +08:00
|
|
|
#define RV3029_TIMER_LOW 0x18
|
|
|
|
#define RV3029_TIMER_HIGH 0x19
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
/* temperature section */
|
2016-03-05 05:38:45 +08:00
|
|
|
#define RV3029_TEMP_PAGE 0x20
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
/* eeprom data section */
|
2016-03-05 05:38:45 +08:00
|
|
|
#define RV3029_E2P_EEDATA1 0x28
|
|
|
|
#define RV3029_E2P_EEDATA2 0x29
|
2016-03-05 05:39:49 +08:00
|
|
|
#define RV3029_E2PDATA_SECTION_LEN 0x02
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
/* eeprom control section */
|
2016-03-05 05:38:45 +08:00
|
|
|
#define RV3029_CONTROL_E2P_EECTRL 0x30
|
2016-03-05 05:39:49 +08:00
|
|
|
#define RV3029_EECTRL_THP BIT(0) /* temp scan interval */
|
|
|
|
#define RV3029_EECTRL_THE BIT(1) /* thermometer enable */
|
|
|
|
#define RV3029_EECTRL_FD0 BIT(2) /* CLKOUT */
|
|
|
|
#define RV3029_EECTRL_FD1 BIT(3) /* CLKOUT */
|
|
|
|
#define RV3029_TRICKLE_1K BIT(4) /* 1.5K resistance */
|
|
|
|
#define RV3029_TRICKLE_5K BIT(5) /* 5K resistance */
|
|
|
|
#define RV3029_TRICKLE_20K BIT(6) /* 20K resistance */
|
|
|
|
#define RV3029_TRICKLE_80K BIT(7) /* 80K resistance */
|
|
|
|
#define RV3029_TRICKLE_MASK (RV3029_TRICKLE_1K |\
|
|
|
|
RV3029_TRICKLE_5K |\
|
|
|
|
RV3029_TRICKLE_20K |\
|
|
|
|
RV3029_TRICKLE_80K)
|
|
|
|
#define RV3029_TRICKLE_SHIFT 4
|
|
|
|
#define RV3029_CONTROL_E2P_XOFFS 0x31 /* XTAL offset */
|
|
|
|
#define RV3029_CONTROL_E2P_XOFFS_SIGN BIT(7) /* Sign: 1->pos, 0->neg */
|
|
|
|
#define RV3029_CONTROL_E2P_QCOEF 0x32 /* XTAL temp drift coef */
|
|
|
|
#define RV3029_CONTROL_E2P_TURNOVER 0x33 /* XTAL turnover temp (in *C) */
|
|
|
|
#define RV3029_CONTROL_E2P_TOV_MASK 0x3F /* XTAL turnover temp mask */
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
/* user ram section */
|
2016-03-05 05:38:45 +08:00
|
|
|
#define RV3029_USR1_RAM_PAGE 0x38
|
|
|
|
#define RV3029_USR1_SECTION_LEN 0x04
|
|
|
|
#define RV3029_USR2_RAM_PAGE 0x3C
|
|
|
|
#define RV3029_USR2_SECTION_LEN 0x04
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
static int
|
2016-03-05 05:38:45 +08:00
|
|
|
rv3029_i2c_read_regs(struct i2c_client *client, u8 reg, u8 *buf,
|
|
|
|
unsigned len)
|
2011-05-27 07:25:05 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
if ((reg > RV3029_USR1_RAM_PAGE + 7) ||
|
|
|
|
(reg + len > RV3029_USR1_RAM_PAGE + 8))
|
2011-05-27 07:25:05 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = i2c_smbus_read_i2c_block_data(client, reg, len, buf);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (ret < len)
|
|
|
|
return -EIO;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-03-05 05:38:45 +08:00
|
|
|
rv3029_i2c_write_regs(struct i2c_client *client, u8 reg, u8 const buf[],
|
|
|
|
unsigned len)
|
2011-05-27 07:25:05 +08:00
|
|
|
{
|
2016-03-05 05:38:45 +08:00
|
|
|
if ((reg > RV3029_USR1_RAM_PAGE + 7) ||
|
|
|
|
(reg + len > RV3029_USR1_RAM_PAGE + 8))
|
2011-05-27 07:25:05 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return i2c_smbus_write_i2c_block_data(client, reg, len, buf);
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:40:30 +08:00
|
|
|
static int
|
|
|
|
rv3029_i2c_update_bits(struct i2c_client *client, u8 reg, u8 mask, u8 set)
|
|
|
|
{
|
|
|
|
u8 buf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = rv3029_i2c_read_regs(client, reg, &buf, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
buf &= ~mask;
|
|
|
|
buf |= set & mask;
|
|
|
|
ret = rv3029_i2c_write_regs(client, reg, &buf, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-27 07:25:05 +08:00
|
|
|
static int
|
2016-03-05 05:38:45 +08:00
|
|
|
rv3029_i2c_get_sr(struct i2c_client *client, u8 *buf)
|
2011-05-27 07:25:05 +08:00
|
|
|
{
|
2016-03-05 05:38:45 +08:00
|
|
|
int ret = rv3029_i2c_read_regs(client, RV3029_STATUS, buf, 1);
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return -EIO;
|
|
|
|
dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-03-05 05:38:45 +08:00
|
|
|
rv3029_i2c_set_sr(struct i2c_client *client, u8 val)
|
2011-05-27 07:25:05 +08:00
|
|
|
{
|
|
|
|
u8 buf[1];
|
|
|
|
int sr;
|
|
|
|
|
|
|
|
buf[0] = val;
|
2016-03-05 05:38:45 +08:00
|
|
|
sr = rv3029_i2c_write_regs(client, RV3029_STATUS, buf, 1);
|
2011-05-27 07:25:05 +08:00
|
|
|
dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
|
|
|
|
if (sr < 0)
|
|
|
|
return -EIO;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:40:55 +08:00
|
|
|
static int rv3029_eeprom_busywait(struct i2c_client *client)
|
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
u8 sr;
|
|
|
|
|
|
|
|
for (i = 100; i > 0; i--) {
|
|
|
|
ret = rv3029_i2c_get_sr(client, &sr);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
if (!(sr & RV3029_STATUS_EEBUSY))
|
|
|
|
break;
|
|
|
|
usleep_range(1000, 10000);
|
|
|
|
}
|
|
|
|
if (i <= 0) {
|
|
|
|
dev_err(&client->dev, "EEPROM busy wait timeout.\n");
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rv3029_eeprom_exit(struct i2c_client *client)
|
|
|
|
{
|
|
|
|
/* Re-enable eeprom refresh */
|
|
|
|
return rv3029_i2c_update_bits(client, RV3029_ONOFF_CTRL,
|
|
|
|
RV3029_ONOFF_CTRL_EERE,
|
|
|
|
RV3029_ONOFF_CTRL_EERE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rv3029_eeprom_enter(struct i2c_client *client)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u8 sr;
|
|
|
|
|
|
|
|
/* Check whether we are in the allowed voltage range. */
|
|
|
|
ret = rv3029_i2c_get_sr(client, &sr);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
|
|
|
|
/* We clear the bits and retry once just in case
|
|
|
|
* we had a brown out in early startup.
|
|
|
|
*/
|
|
|
|
sr &= ~RV3029_STATUS_VLOW1;
|
|
|
|
sr &= ~RV3029_STATUS_VLOW2;
|
|
|
|
ret = rv3029_i2c_set_sr(client, sr);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
usleep_range(1000, 10000);
|
|
|
|
ret = rv3029_i2c_get_sr(client, &sr);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
|
|
|
|
dev_err(&client->dev,
|
|
|
|
"Supply voltage is too low to safely access the EEPROM.\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable eeprom refresh. */
|
|
|
|
ret = rv3029_i2c_update_bits(client, RV3029_ONOFF_CTRL,
|
|
|
|
RV3029_ONOFF_CTRL_EERE, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Wait for any previous eeprom accesses to finish. */
|
|
|
|
ret = rv3029_eeprom_busywait(client);
|
|
|
|
if (ret < 0)
|
|
|
|
rv3029_eeprom_exit(client);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rv3029_eeprom_read(struct i2c_client *client, u8 reg,
|
|
|
|
u8 buf[], size_t len)
|
|
|
|
{
|
|
|
|
int ret, err;
|
|
|
|
|
|
|
|
err = rv3029_eeprom_enter(client);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
ret = rv3029_i2c_read_regs(client, reg, buf, len);
|
|
|
|
|
|
|
|
err = rv3029_eeprom_exit(client);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rv3029_eeprom_write(struct i2c_client *client, u8 reg,
|
|
|
|
u8 const buf[], size_t len)
|
|
|
|
{
|
|
|
|
int ret, err;
|
|
|
|
size_t i;
|
|
|
|
u8 tmp;
|
|
|
|
|
|
|
|
err = rv3029_eeprom_enter(client);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++, reg++) {
|
|
|
|
ret = rv3029_i2c_read_regs(client, reg, &tmp, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
if (tmp != buf[i]) {
|
|
|
|
ret = rv3029_i2c_write_regs(client, reg, &buf[i], 1);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = rv3029_eeprom_busywait(client);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rv3029_eeprom_exit(client);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-11 01:34:23 +08:00
|
|
|
static int rv3029_eeprom_update_bits(struct i2c_client *client,
|
|
|
|
u8 reg, u8 mask, u8 set)
|
|
|
|
{
|
|
|
|
u8 buf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = rv3029_eeprom_read(client, reg, &buf, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
buf &= ~mask;
|
|
|
|
buf |= set & mask;
|
|
|
|
ret = rv3029_eeprom_write(client, reg, &buf, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-27 07:25:05 +08:00
|
|
|
static int
|
2016-03-05 05:38:45 +08:00
|
|
|
rv3029_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
|
2011-05-27 07:25:05 +08:00
|
|
|
{
|
|
|
|
u8 buf[1];
|
|
|
|
int ret;
|
2016-03-05 05:38:45 +08:00
|
|
|
u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, };
|
2011-05-27 07:25:05 +08:00
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
ret = rv3029_i2c_get_sr(client, buf);
|
2011-05-27 07:25:05 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&client->dev, "%s: reading SR failed\n", __func__);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
ret = rv3029_i2c_read_regs(client, RV3029_W_SEC, regs,
|
|
|
|
RV3029_WATCH_SECTION_LEN);
|
2011-05-27 07:25:05 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&client->dev, "%s: reading RTC section failed\n",
|
|
|
|
__func__);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
tm->tm_sec = bcd2bin(regs[RV3029_W_SEC-RV3029_W_SEC]);
|
|
|
|
tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES-RV3029_W_SEC]);
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
/* HR field has a more complex interpretation */
|
|
|
|
{
|
2016-03-05 05:38:45 +08:00
|
|
|
const u8 _hr = regs[RV3029_W_HOURS-RV3029_W_SEC];
|
|
|
|
|
|
|
|
if (_hr & RV3029_REG_HR_12_24) {
|
2011-05-27 07:25:05 +08:00
|
|
|
/* 12h format */
|
|
|
|
tm->tm_hour = bcd2bin(_hr & 0x1f);
|
2016-03-05 05:38:45 +08:00
|
|
|
if (_hr & RV3029_REG_HR_PM) /* PM flag set */
|
2011-05-27 07:25:05 +08:00
|
|
|
tm->tm_hour += 12;
|
|
|
|
} else /* 24h format */
|
|
|
|
tm->tm_hour = bcd2bin(_hr & 0x3f);
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
tm->tm_mday = bcd2bin(regs[RV3029_W_DATE-RV3029_W_SEC]);
|
|
|
|
tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS-RV3029_W_SEC]) - 1;
|
|
|
|
tm->tm_year = bcd2bin(regs[RV3029_W_YEARS-RV3029_W_SEC]) + 100;
|
|
|
|
tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS-RV3029_W_SEC]) - 1;
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
static int rv3029_rtc_read_time(struct device *dev, struct rtc_time *tm)
|
2011-05-27 07:25:05 +08:00
|
|
|
{
|
2016-03-05 05:38:45 +08:00
|
|
|
return rv3029_i2c_read_time(to_i2c_client(dev), tm);
|
2011-05-27 07:25:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-03-05 05:38:45 +08:00
|
|
|
rv3029_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
|
2011-05-27 07:25:05 +08:00
|
|
|
{
|
|
|
|
struct rtc_time *const tm = &alarm->time;
|
|
|
|
int ret;
|
|
|
|
u8 regs[8];
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
ret = rv3029_i2c_get_sr(client, regs);
|
2011-05-27 07:25:05 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&client->dev, "%s: reading SR failed\n", __func__);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
ret = rv3029_i2c_read_regs(client, RV3029_A_SC, regs,
|
|
|
|
RV3029_ALARM_SECTION_LEN);
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&client->dev, "%s: reading alarm section failed\n",
|
|
|
|
__func__);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
tm->tm_sec = bcd2bin(regs[RV3029_A_SC-RV3029_A_SC] & 0x7f);
|
|
|
|
tm->tm_min = bcd2bin(regs[RV3029_A_MN-RV3029_A_SC] & 0x7f);
|
|
|
|
tm->tm_hour = bcd2bin(regs[RV3029_A_HR-RV3029_A_SC] & 0x3f);
|
|
|
|
tm->tm_mday = bcd2bin(regs[RV3029_A_DT-RV3029_A_SC] & 0x3f);
|
|
|
|
tm->tm_mon = bcd2bin(regs[RV3029_A_MO-RV3029_A_SC] & 0x1f) - 1;
|
|
|
|
tm->tm_year = bcd2bin(regs[RV3029_A_YR-RV3029_A_SC] & 0x7f) + 100;
|
|
|
|
tm->tm_wday = bcd2bin(regs[RV3029_A_DW-RV3029_A_SC] & 0x07) - 1;
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-03-05 05:38:45 +08:00
|
|
|
rv3029_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
|
2011-05-27 07:25:05 +08:00
|
|
|
{
|
2016-03-05 05:38:45 +08:00
|
|
|
return rv3029_i2c_read_alarm(to_i2c_client(dev), alarm);
|
2011-05-27 07:25:05 +08:00
|
|
|
}
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
static int rv3029_rtc_i2c_alarm_set_irq(struct i2c_client *client,
|
2011-05-27 07:25:05 +08:00
|
|
|
int enable)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-03-05 05:40:30 +08:00
|
|
|
/* enable/disable AIE irq */
|
|
|
|
ret = rv3029_i2c_update_bits(client, RV3029_IRQ_CTRL,
|
|
|
|
RV3029_IRQ_CTRL_AIE,
|
|
|
|
(enable ? RV3029_IRQ_CTRL_AIE : 0));
|
2011-05-27 07:25:05 +08:00
|
|
|
if (ret < 0) {
|
2016-03-05 05:40:30 +08:00
|
|
|
dev_err(&client->dev, "can't update INT reg\n");
|
2011-05-27 07:25:05 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
static int rv3029_rtc_i2c_set_alarm(struct i2c_client *client,
|
|
|
|
struct rtc_wkalrm *alarm)
|
2011-05-27 07:25:05 +08:00
|
|
|
{
|
|
|
|
struct rtc_time *const tm = &alarm->time;
|
|
|
|
int ret;
|
|
|
|
u8 regs[8];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The clock has an 8 bit wide bcd-coded register (they never learn)
|
|
|
|
* for the year. tm_year is an offset from 1900 and we are interested
|
|
|
|
* in the 2000-2099 range, so any value less than 100 is invalid.
|
|
|
|
*/
|
|
|
|
if (tm->tm_year < 100)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
ret = rv3029_i2c_get_sr(client, regs);
|
2011-05-27 07:25:05 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&client->dev, "%s: reading SR failed\n", __func__);
|
|
|
|
return -EIO;
|
|
|
|
}
|
2016-03-05 05:38:45 +08:00
|
|
|
regs[RV3029_A_SC-RV3029_A_SC] = bin2bcd(tm->tm_sec & 0x7f);
|
|
|
|
regs[RV3029_A_MN-RV3029_A_SC] = bin2bcd(tm->tm_min & 0x7f);
|
|
|
|
regs[RV3029_A_HR-RV3029_A_SC] = bin2bcd(tm->tm_hour & 0x3f);
|
|
|
|
regs[RV3029_A_DT-RV3029_A_SC] = bin2bcd(tm->tm_mday & 0x3f);
|
|
|
|
regs[RV3029_A_MO-RV3029_A_SC] = bin2bcd((tm->tm_mon & 0x1f) - 1);
|
|
|
|
regs[RV3029_A_DW-RV3029_A_SC] = bin2bcd((tm->tm_wday & 7) - 1);
|
|
|
|
regs[RV3029_A_YR-RV3029_A_SC] = bin2bcd((tm->tm_year & 0x7f) - 100);
|
|
|
|
|
|
|
|
ret = rv3029_i2c_write_regs(client, RV3029_A_SC, regs,
|
|
|
|
RV3029_ALARM_SECTION_LEN);
|
2011-05-27 07:25:05 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (alarm->enabled) {
|
|
|
|
/* clear AF flag */
|
2016-03-05 05:40:30 +08:00
|
|
|
ret = rv3029_i2c_update_bits(client, RV3029_IRQ_FLAGS,
|
|
|
|
RV3029_IRQ_FLAGS_AF, 0);
|
2011-05-27 07:25:05 +08:00
|
|
|
if (ret < 0) {
|
2016-03-05 05:40:30 +08:00
|
|
|
dev_err(&client->dev, "can't clear alarm flag\n");
|
2011-05-27 07:25:05 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* enable AIE irq */
|
2016-03-05 05:38:45 +08:00
|
|
|
ret = rv3029_rtc_i2c_alarm_set_irq(client, 1);
|
2011-05-27 07:25:05 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
dev_dbg(&client->dev, "alarm IRQ armed\n");
|
|
|
|
} else {
|
|
|
|
/* disable AIE irq */
|
2016-03-05 05:38:45 +08:00
|
|
|
ret = rv3029_rtc_i2c_alarm_set_irq(client, 0);
|
2011-05-27 07:25:05 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
dev_dbg(&client->dev, "alarm IRQ disabled\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
static int rv3029_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
|
2011-05-27 07:25:05 +08:00
|
|
|
{
|
2016-03-05 05:38:45 +08:00
|
|
|
return rv3029_rtc_i2c_set_alarm(to_i2c_client(dev), alarm);
|
2011-05-27 07:25:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-03-05 05:38:45 +08:00
|
|
|
rv3029_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
|
2011-05-27 07:25:05 +08:00
|
|
|
{
|
|
|
|
u8 regs[8];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The clock has an 8 bit wide bcd-coded register (they never learn)
|
|
|
|
* for the year. tm_year is an offset from 1900 and we are interested
|
|
|
|
* in the 2000-2099 range, so any value less than 100 is invalid.
|
|
|
|
*/
|
|
|
|
if (tm->tm_year < 100)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
regs[RV3029_W_SEC-RV3029_W_SEC] = bin2bcd(tm->tm_sec);
|
|
|
|
regs[RV3029_W_MINUTES-RV3029_W_SEC] = bin2bcd(tm->tm_min);
|
|
|
|
regs[RV3029_W_HOURS-RV3029_W_SEC] = bin2bcd(tm->tm_hour);
|
|
|
|
regs[RV3029_W_DATE-RV3029_W_SEC] = bin2bcd(tm->tm_mday);
|
|
|
|
regs[RV3029_W_MONTHS-RV3029_W_SEC] = bin2bcd(tm->tm_mon+1);
|
|
|
|
regs[RV3029_W_DAYS-RV3029_W_SEC] = bin2bcd((tm->tm_wday & 7)+1);
|
|
|
|
regs[RV3029_W_YEARS-RV3029_W_SEC] = bin2bcd(tm->tm_year - 100);
|
2011-05-27 07:25:05 +08:00
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
ret = rv3029_i2c_write_regs(client, RV3029_W_SEC, regs,
|
|
|
|
RV3029_WATCH_SECTION_LEN);
|
2011-05-27 07:25:05 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
ret = rv3029_i2c_get_sr(client, regs);
|
2011-05-27 07:25:05 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&client->dev, "%s: reading SR failed\n", __func__);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* clear PON bit */
|
2016-03-05 05:38:45 +08:00
|
|
|
ret = rv3029_i2c_set_sr(client, (regs[0] & ~RV3029_STATUS_PON));
|
2011-05-27 07:25:05 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&client->dev, "%s: reading SR failed\n", __func__);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
static int rv3029_rtc_set_time(struct device *dev, struct rtc_time *tm)
|
2011-05-27 07:25:05 +08:00
|
|
|
{
|
2016-03-05 05:38:45 +08:00
|
|
|
return rv3029_i2c_set_time(to_i2c_client(dev), tm);
|
2011-05-27 07:25:05 +08:00
|
|
|
}
|
|
|
|
|
2016-03-05 05:41:19 +08:00
|
|
|
static const struct rv3029_trickle_tab_elem {
|
|
|
|
u32 r; /* resistance in ohms */
|
|
|
|
u8 conf; /* trickle config bits */
|
|
|
|
} rv3029_trickle_tab[] = {
|
|
|
|
{
|
|
|
|
.r = 1076,
|
|
|
|
.conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
|
|
|
|
RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
|
|
|
|
}, {
|
|
|
|
.r = 1091,
|
|
|
|
.conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
|
|
|
|
RV3029_TRICKLE_20K,
|
|
|
|
}, {
|
|
|
|
.r = 1137,
|
|
|
|
.conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
|
|
|
|
RV3029_TRICKLE_80K,
|
|
|
|
}, {
|
|
|
|
.r = 1154,
|
|
|
|
.conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K,
|
|
|
|
}, {
|
|
|
|
.r = 1371,
|
|
|
|
.conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K |
|
|
|
|
RV3029_TRICKLE_80K,
|
|
|
|
}, {
|
|
|
|
.r = 1395,
|
|
|
|
.conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K,
|
|
|
|
}, {
|
|
|
|
.r = 1472,
|
|
|
|
.conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K,
|
|
|
|
}, {
|
|
|
|
.r = 1500,
|
|
|
|
.conf = RV3029_TRICKLE_1K,
|
|
|
|
}, {
|
|
|
|
.r = 3810,
|
|
|
|
.conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K |
|
|
|
|
RV3029_TRICKLE_80K,
|
|
|
|
}, {
|
|
|
|
.r = 4000,
|
|
|
|
.conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K,
|
|
|
|
}, {
|
|
|
|
.r = 4706,
|
|
|
|
.conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K,
|
|
|
|
}, {
|
|
|
|
.r = 5000,
|
|
|
|
.conf = RV3029_TRICKLE_5K,
|
|
|
|
}, {
|
|
|
|
.r = 16000,
|
|
|
|
.conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
|
|
|
|
}, {
|
|
|
|
.r = 20000,
|
|
|
|
.conf = RV3029_TRICKLE_20K,
|
|
|
|
}, {
|
|
|
|
.r = 80000,
|
|
|
|
.conf = RV3029_TRICKLE_80K,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void rv3029_trickle_config(struct i2c_client *client)
|
|
|
|
{
|
|
|
|
struct device_node *of_node = client->dev.of_node;
|
|
|
|
const struct rv3029_trickle_tab_elem *elem;
|
|
|
|
int i, err;
|
|
|
|
u32 ohms;
|
2016-03-11 01:34:23 +08:00
|
|
|
u8 trickle_set_bits;
|
2016-03-05 05:41:19 +08:00
|
|
|
|
|
|
|
if (!of_node)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Configure the trickle charger. */
|
|
|
|
err = of_property_read_u32(of_node, "trickle-resistor-ohms", &ohms);
|
|
|
|
if (err) {
|
|
|
|
/* Disable trickle charger. */
|
2016-03-11 01:34:23 +08:00
|
|
|
trickle_set_bits = 0;
|
2016-03-05 05:41:19 +08:00
|
|
|
} else {
|
|
|
|
/* Enable trickle charger. */
|
|
|
|
for (i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++) {
|
|
|
|
elem = &rv3029_trickle_tab[i];
|
|
|
|
if (elem->r >= ohms)
|
|
|
|
break;
|
|
|
|
}
|
2016-03-11 01:34:23 +08:00
|
|
|
trickle_set_bits = elem->conf;
|
2016-03-05 05:41:19 +08:00
|
|
|
dev_info(&client->dev,
|
|
|
|
"Trickle charger enabled at %d ohms resistance.\n",
|
|
|
|
elem->r);
|
|
|
|
}
|
2016-03-11 01:34:23 +08:00
|
|
|
err = rv3029_eeprom_update_bits(client, RV3029_CONTROL_E2P_EECTRL,
|
|
|
|
RV3029_TRICKLE_MASK,
|
|
|
|
trickle_set_bits);
|
2016-03-05 05:41:19 +08:00
|
|
|
if (err < 0) {
|
|
|
|
dev_err(&client->dev,
|
2016-03-11 01:34:23 +08:00
|
|
|
"Failed to update trickle charger config\n");
|
2016-03-05 05:41:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
static const struct rtc_class_ops rv3029_rtc_ops = {
|
|
|
|
.read_time = rv3029_rtc_read_time,
|
|
|
|
.set_time = rv3029_rtc_set_time,
|
|
|
|
.read_alarm = rv3029_rtc_read_alarm,
|
|
|
|
.set_alarm = rv3029_rtc_set_alarm,
|
2011-05-27 07:25:05 +08:00
|
|
|
};
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
static struct i2c_device_id rv3029_id[] = {
|
2016-03-05 05:39:17 +08:00
|
|
|
{ "rv3029", 0 },
|
2011-05-27 07:25:05 +08:00
|
|
|
{ "rv3029c2", 0 },
|
|
|
|
{ }
|
|
|
|
};
|
2016-03-05 05:38:45 +08:00
|
|
|
MODULE_DEVICE_TABLE(i2c, rv3029_id);
|
2011-05-27 07:25:05 +08:00
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
static int rv3029_probe(struct i2c_client *client,
|
|
|
|
const struct i2c_device_id *id)
|
2011-05-27 07:25:05 +08:00
|
|
|
{
|
|
|
|
struct rtc_device *rtc;
|
|
|
|
int rc = 0;
|
|
|
|
u8 buf[1];
|
|
|
|
|
|
|
|
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_EMUL))
|
|
|
|
return -ENODEV;
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
rc = rv3029_i2c_get_sr(client, buf);
|
2014-04-04 05:50:17 +08:00
|
|
|
if (rc < 0) {
|
|
|
|
dev_err(&client->dev, "reading status failed\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:41:19 +08:00
|
|
|
rv3029_trickle_config(client);
|
|
|
|
|
2013-04-30 07:19:48 +08:00
|
|
|
rtc = devm_rtc_device_register(&client->dev, client->name,
|
2016-03-05 05:38:45 +08:00
|
|
|
&rv3029_rtc_ops, THIS_MODULE);
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
if (IS_ERR(rtc))
|
|
|
|
return PTR_ERR(rtc);
|
|
|
|
|
|
|
|
i2c_set_clientdata(client, rtc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
static struct i2c_driver rv3029_driver = {
|
2011-05-27 07:25:05 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "rtc-rv3029c2",
|
|
|
|
},
|
2016-03-05 05:38:45 +08:00
|
|
|
.probe = rv3029_probe,
|
|
|
|
.id_table = rv3029_id,
|
2011-05-27 07:25:05 +08:00
|
|
|
};
|
|
|
|
|
2016-03-05 05:38:45 +08:00
|
|
|
module_i2c_driver(rv3029_driver);
|
2011-05-27 07:25:05 +08:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>");
|
2016-03-05 05:40:30 +08:00
|
|
|
MODULE_AUTHOR("Michael Buesch <m@bues.ch>");
|
2016-03-05 05:38:45 +08:00
|
|
|
MODULE_DESCRIPTION("Micro Crystal RV3029 RTC driver");
|
2011-05-27 07:25:05 +08:00
|
|
|
MODULE_LICENSE("GPL");
|