Commit 9868bc1c authored by Bartosz Golaszewski's avatar Bartosz Golaszewski Committed by Alexandre Belloni

rtc: rx8010: convert to using regmap

This driver requires SMBUS to work. We can relax this requirement if we
switch to using i2c regmap and let the regmap sub-system figure out how
to talk to the bus.

This also has the advantage of shrinking the code for register updates.
Signed-off-by: default avatarBartosz Golaszewski <bgolaszewski@baylibre.com>
Signed-off-by: default avatarAlexandre Belloni <alexandre.belloni@bootlin.com>
Link: https://lore.kernel.org/r/20200914154601.32245-14-brgl@bgdev.pl
parent cee015d9
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <linux/i2c.h> #include <linux/i2c.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/regmap.h>
#include <linux/rtc.h> #include <linux/rtc.h>
#define RX8010_SEC 0x10 #define RX8010_SEC 0x10
...@@ -61,7 +62,7 @@ static const struct of_device_id rx8010_of_match[] = { ...@@ -61,7 +62,7 @@ static const struct of_device_id rx8010_of_match[] = {
MODULE_DEVICE_TABLE(of, rx8010_of_match); MODULE_DEVICE_TABLE(of, rx8010_of_match);
struct rx8010_data { struct rx8010_data {
struct i2c_client *client; struct regmap *regs;
struct rtc_device *rtc; struct rtc_device *rtc;
u8 ctrlreg; u8 ctrlreg;
}; };
...@@ -70,13 +71,12 @@ static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id) ...@@ -70,13 +71,12 @@ static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id)
{ {
struct i2c_client *client = dev_id; struct i2c_client *client = dev_id;
struct rx8010_data *rx8010 = i2c_get_clientdata(client); struct rx8010_data *rx8010 = i2c_get_clientdata(client);
int flagreg; int flagreg, err;
mutex_lock(&rx8010->rtc->ops_lock); mutex_lock(&rx8010->rtc->ops_lock);
flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG); err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg);
if (err) {
if (flagreg <= 0) {
mutex_unlock(&rx8010->rtc->ops_lock); mutex_unlock(&rx8010->rtc->ops_lock);
return IRQ_NONE; return IRQ_NONE;
} }
...@@ -99,10 +99,9 @@ static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id) ...@@ -99,10 +99,9 @@ static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id)
rtc_update_irq(rx8010->rtc, 1, RTC_UF | RTC_IRQF); rtc_update_irq(rx8010->rtc, 1, RTC_UF | RTC_IRQF);
} }
i2c_smbus_write_byte_data(client, RX8010_FLAG, flagreg); err = regmap_write(rx8010->regs, RX8010_FLAG, flagreg);
mutex_unlock(&rx8010->rtc->ops_lock); mutex_unlock(&rx8010->rtc->ops_lock);
return IRQ_HANDLED; return err ? IRQ_NONE : IRQ_HANDLED;
} }
static int rx8010_get_time(struct device *dev, struct rtc_time *dt) static int rx8010_get_time(struct device *dev, struct rtc_time *dt)
...@@ -111,19 +110,18 @@ static int rx8010_get_time(struct device *dev, struct rtc_time *dt) ...@@ -111,19 +110,18 @@ static int rx8010_get_time(struct device *dev, struct rtc_time *dt)
u8 date[RX8010_YEAR - RX8010_SEC + 1]; u8 date[RX8010_YEAR - RX8010_SEC + 1];
int flagreg, err; int flagreg, err;
flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG); err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg);
if (flagreg < 0) if (err)
return flagreg; return err;
if (flagreg & RX8010_FLAG_VLF) { if (flagreg & RX8010_FLAG_VLF) {
dev_warn(dev, "Frequency stop detected\n"); dev_warn(dev, "Frequency stop detected\n");
return -EINVAL; return -EINVAL;
} }
err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_SEC, err = regmap_bulk_read(rx8010->regs, RX8010_SEC, date, sizeof(date));
sizeof(date), date); if (err)
if (err != sizeof(date)) return err;
return err < 0 ? err : -EIO;
dt->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f); dt->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f);
dt->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f); dt->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f);
...@@ -140,19 +138,14 @@ static int rx8010_set_time(struct device *dev, struct rtc_time *dt) ...@@ -140,19 +138,14 @@ static int rx8010_set_time(struct device *dev, struct rtc_time *dt)
{ {
struct rx8010_data *rx8010 = dev_get_drvdata(dev); struct rx8010_data *rx8010 = dev_get_drvdata(dev);
u8 date[RX8010_YEAR - RX8010_SEC + 1]; u8 date[RX8010_YEAR - RX8010_SEC + 1];
int ctrl, flagreg, err; int err;
if ((dt->tm_year < 100) || (dt->tm_year > 199)) if ((dt->tm_year < 100) || (dt->tm_year > 199))
return -EINVAL; return -EINVAL;
/* set STOP bit before changing clock/calendar */ /* set STOP bit before changing clock/calendar */
ctrl = i2c_smbus_read_byte_data(rx8010->client, RX8010_CTRL); err = regmap_set_bits(rx8010->regs, RX8010_CTRL, RX8010_CTRL_STOP);
if (ctrl < 0) if (err)
return ctrl;
rx8010->ctrlreg = ctrl | RX8010_CTRL_STOP;
err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
rx8010->ctrlreg);
if (err < 0)
return err; return err;
date[RX8010_SEC - RX8010_SEC] = bin2bcd(dt->tm_sec); date[RX8010_SEC - RX8010_SEC] = bin2bcd(dt->tm_sec);
...@@ -163,66 +156,54 @@ static int rx8010_set_time(struct device *dev, struct rtc_time *dt) ...@@ -163,66 +156,54 @@ static int rx8010_set_time(struct device *dev, struct rtc_time *dt)
date[RX8010_YEAR - RX8010_SEC] = bin2bcd(dt->tm_year - 100); date[RX8010_YEAR - RX8010_SEC] = bin2bcd(dt->tm_year - 100);
date[RX8010_WDAY - RX8010_SEC] = bin2bcd(1 << dt->tm_wday); date[RX8010_WDAY - RX8010_SEC] = bin2bcd(1 << dt->tm_wday);
err = i2c_smbus_write_i2c_block_data(rx8010->client, err = regmap_bulk_write(rx8010->regs, RX8010_SEC, date, sizeof(date));
RX8010_SEC, sizeof(date), if (err)
date);
if (err < 0)
return err; return err;
/* clear STOP bit after changing clock/calendar */ /* clear STOP bit after changing clock/calendar */
ctrl = i2c_smbus_read_byte_data(rx8010->client, RX8010_CTRL); err = regmap_clear_bits(rx8010->regs, RX8010_CTRL, RX8010_CTRL_STOP);
if (ctrl < 0) if (err)
return ctrl;
rx8010->ctrlreg = ctrl & ~RX8010_CTRL_STOP;
err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
rx8010->ctrlreg);
if (err < 0)
return err; return err;
flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG); err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_VLF);
if (flagreg < 0) if (err)
return flagreg; return err;
if (flagreg & RX8010_FLAG_VLF)
err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG,
flagreg & ~RX8010_FLAG_VLF);
return 0; return 0;
} }
static int rx8010_init_client(struct i2c_client *client) static int rx8010_init_client(struct device *dev)
{ {
struct rx8010_data *rx8010 = i2c_get_clientdata(client); struct rx8010_data *rx8010 = dev_get_drvdata(dev);
u8 ctrl[2]; u8 ctrl[2];
int need_clear = 0, err; int need_clear = 0, err;
/* Initialize reserved registers as specified in datasheet */ /* Initialize reserved registers as specified in datasheet */
err = i2c_smbus_write_byte_data(client, RX8010_RESV17, 0xD8); err = regmap_write(rx8010->regs, RX8010_RESV17, 0xD8);
if (err < 0) if (err)
return err; return err;
err = i2c_smbus_write_byte_data(client, RX8010_RESV30, 0x00); err = regmap_write(rx8010->regs, RX8010_RESV30, 0x00);
if (err < 0) if (err)
return err; return err;
err = i2c_smbus_write_byte_data(client, RX8010_RESV31, 0x08); err = regmap_write(rx8010->regs, RX8010_RESV31, 0x08);
if (err < 0) if (err)
return err; return err;
err = i2c_smbus_write_byte_data(client, RX8010_IRQ, 0x00); err = regmap_write(rx8010->regs, RX8010_IRQ, 0x00);
if (err < 0) if (err)
return err; return err;
err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_FLAG, err = regmap_bulk_read(rx8010->regs, RX8010_FLAG, ctrl, 2);
2, ctrl); if (err)
if (err != 2) return err;
return err < 0 ? err : -EIO;
if (ctrl[0] & RX8010_FLAG_VLF) if (ctrl[0] & RX8010_FLAG_VLF)
dev_warn(&client->dev, "Frequency stop was detected\n"); dev_warn(dev, "Frequency stop was detected\n");
if (ctrl[0] & RX8010_FLAG_AF) { if (ctrl[0] & RX8010_FLAG_AF) {
dev_warn(&client->dev, "Alarm was detected\n"); dev_warn(dev, "Alarm was detected\n");
need_clear = 1; need_clear = 1;
} }
...@@ -234,8 +215,8 @@ static int rx8010_init_client(struct i2c_client *client) ...@@ -234,8 +215,8 @@ static int rx8010_init_client(struct i2c_client *client)
if (need_clear) { if (need_clear) {
ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF); ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF);
err = i2c_smbus_write_byte_data(client, RX8010_FLAG, ctrl[0]); err = regmap_write(rx8010->regs, RX8010_FLAG, ctrl[0]);
if (err < 0) if (err)
return err; return err;
} }
...@@ -247,17 +228,16 @@ static int rx8010_init_client(struct i2c_client *client) ...@@ -247,17 +228,16 @@ static int rx8010_init_client(struct i2c_client *client)
static int rx8010_read_alarm(struct device *dev, struct rtc_wkalrm *t) static int rx8010_read_alarm(struct device *dev, struct rtc_wkalrm *t)
{ {
struct rx8010_data *rx8010 = dev_get_drvdata(dev); struct rx8010_data *rx8010 = dev_get_drvdata(dev);
struct i2c_client *client = rx8010->client;
u8 alarmvals[3]; u8 alarmvals[3];
int flagreg, err; int flagreg, err;
err = i2c_smbus_read_i2c_block_data(client, RX8010_ALMIN, 3, alarmvals); err = regmap_bulk_read(rx8010->regs, RX8010_ALMIN, alarmvals, 3);
if (err != 3) if (err)
return err < 0 ? err : -EIO; return err;
flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG); err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg);
if (flagreg < 0) if (err)
return flagreg; return err;
t->time.tm_sec = 0; t->time.tm_sec = 0;
t->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); t->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
...@@ -274,52 +254,38 @@ static int rx8010_read_alarm(struct device *dev, struct rtc_wkalrm *t) ...@@ -274,52 +254,38 @@ static int rx8010_read_alarm(struct device *dev, struct rtc_wkalrm *t)
static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t) static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t)
{ {
struct i2c_client *client = to_i2c_client(dev);
struct rx8010_data *rx8010 = dev_get_drvdata(dev); struct rx8010_data *rx8010 = dev_get_drvdata(dev);
u8 alarmvals[3]; u8 alarmvals[3];
int extreg, flagreg, err; int err;
flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
if (flagreg < 0)
return flagreg;
if (rx8010->ctrlreg & (RX8010_CTRL_AIE | RX8010_CTRL_UIE)) { if (rx8010->ctrlreg & (RX8010_CTRL_AIE | RX8010_CTRL_UIE)) {
rx8010->ctrlreg &= ~(RX8010_CTRL_AIE | RX8010_CTRL_UIE); rx8010->ctrlreg &= ~(RX8010_CTRL_AIE | RX8010_CTRL_UIE);
err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL, err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg);
rx8010->ctrlreg); if (err)
if (err < 0)
return err; return err;
} }
flagreg &= ~RX8010_FLAG_AF; err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_AF);
err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG, flagreg); if (err)
if (err < 0)
return err; return err;
alarmvals[0] = bin2bcd(t->time.tm_min); alarmvals[0] = bin2bcd(t->time.tm_min);
alarmvals[1] = bin2bcd(t->time.tm_hour); alarmvals[1] = bin2bcd(t->time.tm_hour);
alarmvals[2] = bin2bcd(t->time.tm_mday); alarmvals[2] = bin2bcd(t->time.tm_mday);
err = i2c_smbus_write_i2c_block_data(rx8010->client, RX8010_ALMIN, err = regmap_bulk_write(rx8010->regs, RX8010_ALMIN, alarmvals, 2);
2, alarmvals); if (err)
if (err < 0)
return err; return err;
extreg = i2c_smbus_read_byte_data(client, RX8010_EXT); err = regmap_clear_bits(rx8010->regs, RX8010_EXT, RX8010_EXT_WADA);
if (extreg < 0) if (err)
return extreg;
extreg |= RX8010_EXT_WADA;
err = i2c_smbus_write_byte_data(rx8010->client, RX8010_EXT, extreg);
if (err < 0)
return err; return err;
if (alarmvals[2] == 0) if (alarmvals[2] == 0)
alarmvals[2] |= RX8010_ALARM_AE; alarmvals[2] |= RX8010_ALARM_AE;
err = i2c_smbus_write_byte_data(rx8010->client, RX8010_ALWDAY, err = regmap_write(rx8010->regs, RX8010_ALWDAY, alarmvals[2]);
alarmvals[2]); if (err)
if (err < 0)
return err; return err;
if (t->enabled) { if (t->enabled) {
...@@ -329,9 +295,8 @@ static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t) ...@@ -329,9 +295,8 @@ static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t)
rx8010->ctrlreg |= rx8010->ctrlreg |=
(RX8010_CTRL_AIE | RX8010_CTRL_UIE); (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL, err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg);
rx8010->ctrlreg); if (err)
if (err < 0)
return err; return err;
} }
...@@ -341,9 +306,8 @@ static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t) ...@@ -341,9 +306,8 @@ static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t)
static int rx8010_alarm_irq_enable(struct device *dev, static int rx8010_alarm_irq_enable(struct device *dev,
unsigned int enabled) unsigned int enabled)
{ {
struct i2c_client *client = to_i2c_client(dev);
struct rx8010_data *rx8010 = dev_get_drvdata(dev); struct rx8010_data *rx8010 = dev_get_drvdata(dev);
int flagreg, err; int err;
u8 ctrl; u8 ctrl;
ctrl = rx8010->ctrlreg; ctrl = rx8010->ctrlreg;
...@@ -360,20 +324,14 @@ static int rx8010_alarm_irq_enable(struct device *dev, ...@@ -360,20 +324,14 @@ static int rx8010_alarm_irq_enable(struct device *dev,
ctrl &= ~RX8010_CTRL_AIE; ctrl &= ~RX8010_CTRL_AIE;
} }
flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG); err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_AF);
if (flagreg < 0) if (err)
return flagreg;
flagreg &= ~RX8010_FLAG_AF;
err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG, flagreg);
if (err < 0)
return err; return err;
if (ctrl != rx8010->ctrlreg) { if (ctrl != rx8010->ctrlreg) {
rx8010->ctrlreg = ctrl; rx8010->ctrlreg = ctrl;
err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL, err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg);
rx8010->ctrlreg); if (err)
if (err < 0)
return err; return err;
} }
...@@ -383,13 +341,13 @@ static int rx8010_alarm_irq_enable(struct device *dev, ...@@ -383,13 +341,13 @@ static int rx8010_alarm_irq_enable(struct device *dev,
static int rx8010_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) static int rx8010_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
{ {
struct rx8010_data *rx8010 = dev_get_drvdata(dev); struct rx8010_data *rx8010 = dev_get_drvdata(dev);
int tmp, flagreg; int tmp, flagreg, err;
switch (cmd) { switch (cmd) {
case RTC_VL_READ: case RTC_VL_READ:
flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG); err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg);
if (flagreg < 0) if (err)
return flagreg; return err;
tmp = flagreg & RX8010_FLAG_VLF ? RTC_VL_DATA_INVALID : 0; tmp = flagreg & RX8010_FLAG_VLF ? RTC_VL_DATA_INVALID : 0;
return put_user(tmp, (unsigned int __user *)arg); return put_user(tmp, (unsigned int __user *)arg);
...@@ -414,27 +372,29 @@ static const struct rtc_class_ops rx8010_rtc_ops_alarm = { ...@@ -414,27 +372,29 @@ static const struct rtc_class_ops rx8010_rtc_ops_alarm = {
.alarm_irq_enable = rx8010_alarm_irq_enable, .alarm_irq_enable = rx8010_alarm_irq_enable,
}; };
static const struct regmap_config rx8010_regmap_config = {
.name = "rx8010-rtc",
.reg_bits = 8,
.val_bits = 8,
};
static int rx8010_probe(struct i2c_client *client) static int rx8010_probe(struct i2c_client *client)
{ {
struct i2c_adapter *adapter = client->adapter;
struct device *dev = &client->dev; struct device *dev = &client->dev;
struct rx8010_data *rx8010; struct rx8010_data *rx8010;
int err = 0; int err = 0;
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_I2C_BLOCK)) {
dev_err(&adapter->dev, "doesn't support required functionality\n");
return -EIO;
}
rx8010 = devm_kzalloc(dev, sizeof(*rx8010), GFP_KERNEL); rx8010 = devm_kzalloc(dev, sizeof(*rx8010), GFP_KERNEL);
if (!rx8010) if (!rx8010)
return -ENOMEM; return -ENOMEM;
rx8010->client = client;
i2c_set_clientdata(client, rx8010); i2c_set_clientdata(client, rx8010);
err = rx8010_init_client(client); rx8010->regs = devm_regmap_init_i2c(client, &rx8010_regmap_config);
if (IS_ERR(rx8010->regs))
return PTR_ERR(rx8010->regs);
err = rx8010_init_client(dev);
if (err) if (err)
return err; return err;
......
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