Commit f8b23bbd authored by Krzysztof Kozlowski's avatar Krzysztof Kozlowski Committed by Linus Torvalds

rtc: s5m: support different register layout

Prepare for adding support for S2MPS14 RTC device to the rtc-s5m driver:

1. Add a map of registers used by the driver which differ between
   the chipsets (S5M876X and S2MPS14).

2. Move code of checking for alarm pending to separate function.
Signed-off-by: default avatarKrzysztof Kozlowski <k.kozlowski@samsung.com>
Cc: Kyungmin Park <kyungmin.park@samsung.com>
Cc: Lee Jones <lee.jones@linaro.org>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Sangbeom Kim <sbkim73@samsung.com>
Cc: Samuel Ortiz <sameo@linux.intel.com>
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 0c5f5d9a
/* /*
* Copyright (c) 2013 Samsung Electronics Co., Ltd * Copyright (c) 2013-2014 Samsung Electronics Co., Ltd
* http://www.samsung.com * http://www.samsung.com
* *
* Copyright (C) 2013 Google, Inc * Copyright (C) 2013 Google, Inc
...@@ -38,6 +38,42 @@ ...@@ -38,6 +38,42 @@
*/ */
#define UDR_READ_RETRY_CNT 5 #define UDR_READ_RETRY_CNT 5
/* Registers used by the driver which are different between chipsets. */
struct s5m_rtc_reg_config {
/* Number of registers used for setting time/alarm0/alarm1 */
unsigned int regs_count;
/* First register for time, seconds */
unsigned int time;
/* RTC control register */
unsigned int ctrl;
/* First register for alarm 0, seconds */
unsigned int alarm0;
/* First register for alarm 1, seconds */
unsigned int alarm1;
/* SMPL/WTSR register */
unsigned int smpl_wtsr;
/*
* Register for update flag (UDR). Typically setting UDR field to 1
* will enable update of time or alarm register. Then it will be
* auto-cleared after successful update.
*/
unsigned int rtc_udr_update;
/* Mask for UDR field in 'rtc_udr_update' register */
unsigned int rtc_udr_mask;
};
/* Register map for S5M8763 and S5M8767 */
static const struct s5m_rtc_reg_config s5m_rtc_regs = {
.regs_count = 8,
.time = S5M_RTC_SEC,
.ctrl = S5M_ALARM1_CONF,
.alarm0 = S5M_ALARM0_SEC,
.alarm1 = S5M_ALARM1_SEC,
.smpl_wtsr = S5M_WTSR_SMPL_CNTL,
.rtc_udr_update = S5M_RTC_UDR_CON,
.rtc_udr_mask = S5M_RTC_UDR_MASK,
};
struct s5m_rtc_info { struct s5m_rtc_info {
struct device *dev; struct device *dev;
struct i2c_client *i2c; struct i2c_client *i2c;
...@@ -48,6 +84,7 @@ struct s5m_rtc_info { ...@@ -48,6 +84,7 @@ struct s5m_rtc_info {
int device_type; int device_type;
int rtc_24hr_mode; int rtc_24hr_mode;
bool wtsr_smpl; bool wtsr_smpl;
const struct s5m_rtc_reg_config *regs;
}; };
static const struct regmap_config s5m_rtc_regmap_config = { static const struct regmap_config s5m_rtc_regmap_config = {
...@@ -119,8 +156,9 @@ static inline int s5m8767_wait_for_udr_update(struct s5m_rtc_info *info) ...@@ -119,8 +156,9 @@ static inline int s5m8767_wait_for_udr_update(struct s5m_rtc_info *info)
unsigned int data; unsigned int data;
do { do {
ret = regmap_read(info->regmap, S5M_RTC_UDR_CON, &data); ret = regmap_read(info->regmap, info->regs->rtc_udr_update,
} while (--retry && (data & S5M_RTC_UDR_MASK) && !ret); &data);
} while (--retry && (data & info->regs->rtc_udr_mask) && !ret);
if (!retry) if (!retry)
dev_err(info->dev, "waiting for UDR update, reached max number of retries\n"); dev_err(info->dev, "waiting for UDR update, reached max number of retries\n");
...@@ -128,21 +166,47 @@ static inline int s5m8767_wait_for_udr_update(struct s5m_rtc_info *info) ...@@ -128,21 +166,47 @@ static inline int s5m8767_wait_for_udr_update(struct s5m_rtc_info *info)
return ret; return ret;
} }
static inline int s5m_check_peding_alarm_interrupt(struct s5m_rtc_info *info,
struct rtc_wkalrm *alarm)
{
int ret;
unsigned int val;
switch (info->device_type) {
case S5M8767X:
case S5M8763X:
ret = regmap_read(info->regmap, S5M_RTC_STATUS, &val);
val &= S5M_ALARM0_STATUS;
break;
default:
return -EINVAL;
}
if (ret < 0)
return ret;
if (val)
alarm->pending = 1;
else
alarm->pending = 0;
return 0;
}
static inline int s5m8767_rtc_set_time_reg(struct s5m_rtc_info *info) static inline int s5m8767_rtc_set_time_reg(struct s5m_rtc_info *info)
{ {
int ret; int ret;
unsigned int data; unsigned int data;
ret = regmap_read(info->regmap, S5M_RTC_UDR_CON, &data); ret = regmap_read(info->regmap, info->regs->rtc_udr_update, &data);
if (ret < 0) { if (ret < 0) {
dev_err(info->dev, "failed to read update reg(%d)\n", ret); dev_err(info->dev, "failed to read update reg(%d)\n", ret);
return ret; return ret;
} }
data |= S5M_RTC_TIME_EN_MASK; data |= S5M_RTC_TIME_EN_MASK;
data |= S5M_RTC_UDR_MASK; data |= info->regs->rtc_udr_mask;
ret = regmap_write(info->regmap, S5M_RTC_UDR_CON, data); ret = regmap_write(info->regmap, info->regs->rtc_udr_update, data);
if (ret < 0) { if (ret < 0) {
dev_err(info->dev, "failed to write update reg(%d)\n", ret); dev_err(info->dev, "failed to write update reg(%d)\n", ret);
return ret; return ret;
...@@ -158,7 +222,7 @@ static inline int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info) ...@@ -158,7 +222,7 @@ static inline int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info)
int ret; int ret;
unsigned int data; unsigned int data;
ret = regmap_read(info->regmap, S5M_RTC_UDR_CON, &data); ret = regmap_read(info->regmap, info->regs->rtc_udr_update, &data);
if (ret < 0) { if (ret < 0) {
dev_err(info->dev, "%s: fail to read update reg(%d)\n", dev_err(info->dev, "%s: fail to read update reg(%d)\n",
__func__, ret); __func__, ret);
...@@ -166,9 +230,9 @@ static inline int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info) ...@@ -166,9 +230,9 @@ static inline int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info)
} }
data &= ~S5M_RTC_TIME_EN_MASK; data &= ~S5M_RTC_TIME_EN_MASK;
data |= S5M_RTC_UDR_MASK; data |= info->regs->rtc_udr_mask;
ret = regmap_write(info->regmap, S5M_RTC_UDR_CON, data); ret = regmap_write(info->regmap, info->regs->rtc_udr_update, data);
if (ret < 0) { if (ret < 0) {
dev_err(info->dev, "%s: fail to write update reg(%d)\n", dev_err(info->dev, "%s: fail to write update reg(%d)\n",
__func__, ret); __func__, ret);
...@@ -215,10 +279,11 @@ static void s5m8763_tm_to_data(struct rtc_time *tm, u8 *data) ...@@ -215,10 +279,11 @@ static void s5m8763_tm_to_data(struct rtc_time *tm, u8 *data)
static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm) static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm)
{ {
struct s5m_rtc_info *info = dev_get_drvdata(dev); struct s5m_rtc_info *info = dev_get_drvdata(dev);
u8 data[8]; u8 data[info->regs->regs_count];
int ret; int ret;
ret = regmap_bulk_read(info->regmap, S5M_RTC_SEC, data, 8); ret = regmap_bulk_read(info->regmap, info->regs->time, data,
info->regs->regs_count);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -245,7 +310,7 @@ static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm) ...@@ -245,7 +310,7 @@ static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm)
static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm) static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm)
{ {
struct s5m_rtc_info *info = dev_get_drvdata(dev); struct s5m_rtc_info *info = dev_get_drvdata(dev);
u8 data[8]; u8 data[info->regs->regs_count];
int ret = 0; int ret = 0;
switch (info->device_type) { switch (info->device_type) {
...@@ -266,7 +331,8 @@ static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm) ...@@ -266,7 +331,8 @@ static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm)
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday); tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
ret = regmap_raw_write(info->regmap, S5M_RTC_SEC, data, 8); ret = regmap_raw_write(info->regmap, info->regs->time, data,
info->regs->regs_count);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -278,11 +344,12 @@ static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm) ...@@ -278,11 +344,12 @@ static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm)
static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{ {
struct s5m_rtc_info *info = dev_get_drvdata(dev); struct s5m_rtc_info *info = dev_get_drvdata(dev);
u8 data[8]; u8 data[info->regs->regs_count];
unsigned int val; unsigned int val;
int ret, i; int ret, i;
ret = regmap_bulk_read(info->regmap, S5M_ALARM0_SEC, data, 8); ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
info->regs->regs_count);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -294,54 +361,42 @@ static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) ...@@ -294,54 +361,42 @@ static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
return ret; return ret;
alrm->enabled = !!val; alrm->enabled = !!val;
ret = regmap_read(info->regmap, S5M_RTC_STATUS, &val);
if (ret < 0)
return ret;
break; break;
case S5M8767X: case S5M8767X:
s5m8767_data_to_tm(data, &alrm->time, info->rtc_24hr_mode); s5m8767_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon,
alrm->time.tm_mday, alrm->time.tm_hour,
alrm->time.tm_min, alrm->time.tm_sec,
alrm->time.tm_wday);
alrm->enabled = 0; alrm->enabled = 0;
for (i = 0; i < 7; i++) { for (i = 0; i < info->regs->regs_count; i++) {
if (data[i] & ALARM_ENABLE_MASK) { if (data[i] & ALARM_ENABLE_MASK) {
alrm->enabled = 1; alrm->enabled = 1;
break; break;
} }
} }
alrm->pending = 0;
ret = regmap_read(info->regmap, S5M_RTC_STATUS, &val);
if (ret < 0)
return ret;
break; break;
default: default:
return -EINVAL; return -EINVAL;
} }
if (val & S5M_ALARM0_STATUS) dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
alrm->pending = 1; 1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon,
else alrm->time.tm_mday, alrm->time.tm_hour,
alrm->pending = 0; alrm->time.tm_min, alrm->time.tm_sec,
alrm->time.tm_wday);
ret = s5m_check_peding_alarm_interrupt(info, alrm);
return 0; return 0;
} }
static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info) static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info)
{ {
u8 data[8]; u8 data[info->regs->regs_count];
int ret, i; int ret, i;
struct rtc_time tm; struct rtc_time tm;
ret = regmap_bulk_read(info->regmap, S5M_ALARM0_SEC, data, 8); ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
info->regs->regs_count);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -356,10 +411,11 @@ static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info) ...@@ -356,10 +411,11 @@ static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info)
break; break;
case S5M8767X: case S5M8767X:
for (i = 0; i < 7; i++) for (i = 0; i < info->regs->regs_count; i++)
data[i] &= ~ALARM_ENABLE_MASK; data[i] &= ~ALARM_ENABLE_MASK;
ret = regmap_raw_write(info->regmap, S5M_ALARM0_SEC, data, 8); ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
info->regs->regs_count);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -377,11 +433,12 @@ static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info) ...@@ -377,11 +433,12 @@ static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info)
static int s5m_rtc_start_alarm(struct s5m_rtc_info *info) static int s5m_rtc_start_alarm(struct s5m_rtc_info *info)
{ {
int ret; int ret;
u8 data[8]; u8 data[info->regs->regs_count];
u8 alarm0_conf; u8 alarm0_conf;
struct rtc_time tm; struct rtc_time tm;
ret = regmap_bulk_read(info->regmap, S5M_ALARM0_SEC, data, 8); ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
info->regs->regs_count);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -408,7 +465,8 @@ static int s5m_rtc_start_alarm(struct s5m_rtc_info *info) ...@@ -408,7 +465,8 @@ static int s5m_rtc_start_alarm(struct s5m_rtc_info *info)
if (data[RTC_YEAR1] & 0x7f) if (data[RTC_YEAR1] & 0x7f)
data[RTC_YEAR1] |= ALARM_ENABLE_MASK; data[RTC_YEAR1] |= ALARM_ENABLE_MASK;
ret = regmap_raw_write(info->regmap, S5M_ALARM0_SEC, data, 8); ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
info->regs->regs_count);
if (ret < 0) if (ret < 0)
return ret; return ret;
ret = s5m8767_rtc_set_alarm_reg(info); ret = s5m8767_rtc_set_alarm_reg(info);
...@@ -425,7 +483,7 @@ static int s5m_rtc_start_alarm(struct s5m_rtc_info *info) ...@@ -425,7 +483,7 @@ static int s5m_rtc_start_alarm(struct s5m_rtc_info *info)
static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{ {
struct s5m_rtc_info *info = dev_get_drvdata(dev); struct s5m_rtc_info *info = dev_get_drvdata(dev);
u8 data[8]; u8 data[info->regs->regs_count];
int ret; int ret;
switch (info->device_type) { switch (info->device_type) {
...@@ -450,7 +508,8 @@ static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) ...@@ -450,7 +508,8 @@ static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
if (ret < 0) if (ret < 0)
return ret; return ret;
ret = regmap_raw_write(info->regmap, S5M_ALARM0_SEC, data, 8); ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
info->regs->regs_count);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -495,7 +554,7 @@ static const struct rtc_class_ops s5m_rtc_ops = { ...@@ -495,7 +554,7 @@ static const struct rtc_class_ops s5m_rtc_ops = {
static void s5m_rtc_enable_wtsr(struct s5m_rtc_info *info, bool enable) static void s5m_rtc_enable_wtsr(struct s5m_rtc_info *info, bool enable)
{ {
int ret; int ret;
ret = regmap_update_bits(info->regmap, S5M_WTSR_SMPL_CNTL, ret = regmap_update_bits(info->regmap, info->regs->smpl_wtsr,
WTSR_ENABLE_MASK, WTSR_ENABLE_MASK,
enable ? WTSR_ENABLE_MASK : 0); enable ? WTSR_ENABLE_MASK : 0);
if (ret < 0) if (ret < 0)
...@@ -506,7 +565,7 @@ static void s5m_rtc_enable_wtsr(struct s5m_rtc_info *info, bool enable) ...@@ -506,7 +565,7 @@ static void s5m_rtc_enable_wtsr(struct s5m_rtc_info *info, bool enable)
static void s5m_rtc_enable_smpl(struct s5m_rtc_info *info, bool enable) static void s5m_rtc_enable_smpl(struct s5m_rtc_info *info, bool enable)
{ {
int ret; int ret;
ret = regmap_update_bits(info->regmap, S5M_WTSR_SMPL_CNTL, ret = regmap_update_bits(info->regmap, info->regs->smpl_wtsr,
SMPL_ENABLE_MASK, SMPL_ENABLE_MASK,
enable ? SMPL_ENABLE_MASK : 0); enable ? SMPL_ENABLE_MASK : 0);
if (ret < 0) if (ret < 0)
...@@ -564,9 +623,11 @@ static int s5m_rtc_probe(struct platform_device *pdev) ...@@ -564,9 +623,11 @@ static int s5m_rtc_probe(struct platform_device *pdev)
break; break;
case S5M8763X: case S5M8763X:
regmap_cfg = &s5m_rtc_regmap_config; regmap_cfg = &s5m_rtc_regmap_config;
info->regs = &s5m_rtc_regs;
break; break;
case S5M8767X: case S5M8767X:
regmap_cfg = &s5m_rtc_regmap_config; regmap_cfg = &s5m_rtc_regmap_config;
info->regs = &s5m_rtc_regs;
break; break;
default: default:
dev_err(&pdev->dev, "Device type is not supported by RTC driver\n"); dev_err(&pdev->dev, "Device type is not supported by RTC driver\n");
...@@ -653,7 +714,7 @@ static void s5m_rtc_shutdown(struct platform_device *pdev) ...@@ -653,7 +714,7 @@ static void s5m_rtc_shutdown(struct platform_device *pdev)
if (info->wtsr_smpl) { if (info->wtsr_smpl) {
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
s5m_rtc_enable_wtsr(info, false); s5m_rtc_enable_wtsr(info, false);
regmap_read(info->regmap, S5M_WTSR_SMPL_CNTL, &val); regmap_read(info->regmap, info->regs->smpl_wtsr, &val);
pr_debug("%s: WTSR_SMPL reg(0x%02x)\n", __func__, val); pr_debug("%s: WTSR_SMPL reg(0x%02x)\n", __func__, val);
if (val & WTSR_ENABLE_MASK) if (val & WTSR_ENABLE_MASK)
pr_emerg("%s: fail to disable WTSR\n", pr_emerg("%s: fail to disable WTSR\n",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment