diff options
author | Mylène Josserand <mylene.josserand@free-electrons.com> | 2016-05-03 12:54:33 +0300 |
---|---|---|
committer | Alexandre Belloni <alexandre.belloni@free-electrons.com> | 2016-05-21 18:04:11 +0300 |
commit | e6e380821236b628a1379db97d777da3e36b8240 (patch) | |
tree | 515fdbb16fbc097783932a60f552d31df5a4e59d /drivers | |
parent | 4e7f1a6051d7d5a62021fde0c9ed7005a1a82307 (diff) | |
download | linux-e6e380821236b628a1379db97d777da3e36b8240.tar.xz |
rtc: rv3029: convert to use regmap
To add support of rv3049, the current driver is converted to use regmap.
Signed-off-by: Mylène Josserand <mylene.josserand@free-electrons.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/rtc/rtc-rv3029c2.c | 275 |
1 files changed, 142 insertions, 133 deletions
diff --git a/drivers/rtc/rtc-rv3029c2.c b/drivers/rtc/rtc-rv3029c2.c index 091be483e3e9..96dd1665220c 100644 --- a/drivers/rtc/rtc-rv3029c2.c +++ b/drivers/rtc/rtc-rv3029c2.c @@ -20,7 +20,7 @@ #include <linux/of.h> #include <linux/hwmon.h> #include <linux/hwmon-sysfs.h> - +#include <linux/regmap.h> /* Register map */ /* control section */ @@ -116,81 +116,84 @@ #define RV3029_USR2_RAM_PAGE 0x3C #define RV3029_USR2_SECTION_LEN 0x04 -static int rv3029_read_regs(struct i2c_client *client, u8 reg, u8 *buf, +struct rv3029_data { + struct device *dev; + struct rtc_device *rtc; + struct regmap *regmap; + int irq; +}; + +static int rv3029_read_regs(struct device *dev, u8 reg, u8 *buf, unsigned len) { - int ret; + struct rv3029_data *rv3029 = dev_get_drvdata(dev); if ((reg > RV3029_USR1_RAM_PAGE + 7) || (reg + len > RV3029_USR1_RAM_PAGE + 8)) 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; + return regmap_bulk_read(rv3029->regmap, reg, buf, len); } -static int rv3029_write_regs(struct i2c_client *client, u8 reg, u8 const buf[], +static int rv3029_write_regs(struct device *dev, u8 reg, u8 const buf[], unsigned len) { + struct rv3029_data *rv3029 = dev_get_drvdata(dev); + if ((reg > RV3029_USR1_RAM_PAGE + 7) || (reg + len > RV3029_USR1_RAM_PAGE + 8)) return -EINVAL; - return i2c_smbus_write_i2c_block_data(client, reg, len, buf); + return regmap_bulk_write(rv3029->regmap, reg, buf, len); } -static int rv3029_update_bits(struct i2c_client *client, u8 reg, u8 mask, - u8 set) +static int rv3029_update_bits(struct device *dev, u8 reg, u8 mask, u8 set) { u8 buf; int ret; - ret = rv3029_read_regs(client, reg, &buf, 1); + ret = rv3029_read_regs(dev, reg, &buf, 1); if (ret < 0) return ret; buf &= ~mask; buf |= set & mask; - ret = rv3029_write_regs(client, reg, &buf, 1); + ret = rv3029_write_regs(dev, reg, &buf, 1); if (ret < 0) return ret; return 0; } -static int rv3029_get_sr(struct i2c_client *client, u8 *buf) +static int rv3029_get_sr(struct device *dev, u8 *buf) { - int ret = rv3029_read_regs(client, RV3029_STATUS, buf, 1); + int ret = rv3029_read_regs(dev, RV3029_STATUS, buf, 1); if (ret < 0) return -EIO; - dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]); + dev_dbg(dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]); return 0; } -static int rv3029_set_sr(struct i2c_client *client, u8 val) +static int rv3029_set_sr(struct device *dev, u8 val) { u8 buf[1]; int sr; buf[0] = val; - sr = rv3029_write_regs(client, RV3029_STATUS, buf, 1); - dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]); + sr = rv3029_write_regs(dev, RV3029_STATUS, buf, 1); + dev_dbg(dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]); if (sr < 0) return -EIO; return 0; } -static int rv3029_eeprom_busywait(struct i2c_client *client) +static int rv3029_eeprom_busywait(struct device *dev) { int i, ret; u8 sr; for (i = 100; i > 0; i--) { - ret = rv3029_get_sr(client, &sr); + ret = rv3029_get_sr(dev, &sr); if (ret < 0) break; if (!(sr & RV3029_STATUS_EEBUSY)) @@ -198,28 +201,28 @@ static int rv3029_eeprom_busywait(struct i2c_client *client) usleep_range(1000, 10000); } if (i <= 0) { - dev_err(&client->dev, "EEPROM busy wait timeout.\n"); + dev_err(dev, "EEPROM busy wait timeout.\n"); return -ETIMEDOUT; } return ret; } -static int rv3029_eeprom_exit(struct i2c_client *client) +static int rv3029_eeprom_exit(struct device *dev) { /* Re-enable eeprom refresh */ - return rv3029_update_bits(client, RV3029_ONOFF_CTRL, + return rv3029_update_bits(dev, RV3029_ONOFF_CTRL, RV3029_ONOFF_CTRL_EERE, RV3029_ONOFF_CTRL_EERE); } -static int rv3029_eeprom_enter(struct i2c_client *client) +static int rv3029_eeprom_enter(struct device *dev) { int ret; u8 sr; /* Check whether we are in the allowed voltage range. */ - ret = rv3029_get_sr(client, &sr); + ret = rv3029_get_sr(dev, &sr); if (ret < 0) return ret; if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) { @@ -228,119 +231,118 @@ static int rv3029_eeprom_enter(struct i2c_client *client) */ sr &= ~RV3029_STATUS_VLOW1; sr &= ~RV3029_STATUS_VLOW2; - ret = rv3029_set_sr(client, sr); + ret = rv3029_set_sr(dev, sr); if (ret < 0) return ret; usleep_range(1000, 10000); - ret = rv3029_get_sr(client, &sr); + ret = rv3029_get_sr(dev, &sr); if (ret < 0) return ret; if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) { - dev_err(&client->dev, + dev_err(dev, "Supply voltage is too low to safely access the EEPROM.\n"); return -ENODEV; } } /* Disable eeprom refresh. */ - ret = rv3029_update_bits(client, RV3029_ONOFF_CTRL, - RV3029_ONOFF_CTRL_EERE, 0); + ret = rv3029_update_bits(dev, 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); + ret = rv3029_eeprom_busywait(dev); if (ret < 0) - rv3029_eeprom_exit(client); + rv3029_eeprom_exit(dev); return ret; } -static int rv3029_eeprom_read(struct i2c_client *client, u8 reg, +static int rv3029_eeprom_read(struct device *dev, u8 reg, u8 buf[], size_t len) { int ret, err; - err = rv3029_eeprom_enter(client); + err = rv3029_eeprom_enter(dev); if (err < 0) return err; - ret = rv3029_read_regs(client, reg, buf, len); + ret = rv3029_read_regs(dev, reg, buf, len); - err = rv3029_eeprom_exit(client); + err = rv3029_eeprom_exit(dev); if (err < 0) return err; return ret; } -static int rv3029_eeprom_write(struct i2c_client *client, u8 reg, +static int rv3029_eeprom_write(struct device *dev, u8 reg, u8 const buf[], size_t len) { int ret, err; size_t i; u8 tmp; - err = rv3029_eeprom_enter(client); + err = rv3029_eeprom_enter(dev); if (err < 0) return err; for (i = 0; i < len; i++, reg++) { - ret = rv3029_read_regs(client, reg, &tmp, 1); + ret = rv3029_read_regs(dev, reg, &tmp, 1); if (ret < 0) break; if (tmp != buf[i]) { - ret = rv3029_write_regs(client, reg, &buf[i], 1); + ret = rv3029_write_regs(dev, reg, &buf[i], 1); if (ret < 0) break; } - ret = rv3029_eeprom_busywait(client); + ret = rv3029_eeprom_busywait(dev); if (ret < 0) break; } - err = rv3029_eeprom_exit(client); + err = rv3029_eeprom_exit(dev); if (err < 0) return err; return ret; } -static int rv3029_eeprom_update_bits(struct i2c_client *client, +static int rv3029_eeprom_update_bits(struct device *dev, u8 reg, u8 mask, u8 set) { u8 buf; int ret; - ret = rv3029_eeprom_read(client, reg, &buf, 1); + ret = rv3029_eeprom_read(dev, reg, &buf, 1); if (ret < 0) return ret; buf &= ~mask; buf |= set & mask; - ret = rv3029_eeprom_write(client, reg, &buf, 1); + ret = rv3029_eeprom_write(dev, reg, &buf, 1); if (ret < 0) return ret; return 0; } -static int rv3029_read_time(struct i2c_client *client, struct rtc_time *tm) +static int rv3029_read_time(struct device *dev, struct rtc_time *tm) { u8 buf[1]; int ret; u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, }; - ret = rv3029_get_sr(client, buf); + ret = rv3029_get_sr(dev, buf); if (ret < 0) { - dev_err(&client->dev, "%s: reading SR failed\n", __func__); + dev_err(dev, "%s: reading SR failed\n", __func__); return -EIO; } - ret = rv3029_read_regs(client, RV3029_W_SEC, regs, + ret = rv3029_read_regs(dev, RV3029_W_SEC, regs, RV3029_WATCH_SECTION_LEN); if (ret < 0) { - dev_err(&client->dev, "%s: reading RTC section failed\n", - __func__); + dev_err(dev, "%s: reading RTC section failed\n", __func__); return ret; } @@ -368,30 +370,23 @@ static int rv3029_read_time(struct i2c_client *client, struct rtc_time *tm) return 0; } -static int rv3029_rtc_read_time(struct device *dev, struct rtc_time *tm) -{ - return rv3029_read_time(to_i2c_client(dev), tm); -} - -static int rv3029_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) +static int rv3029_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) { - struct i2c_client *client = to_i2c_client(dev); struct rtc_time *const tm = &alarm->time; int ret; u8 regs[8]; - ret = rv3029_get_sr(client, regs); + ret = rv3029_get_sr(dev, regs); if (ret < 0) { - dev_err(&client->dev, "%s: reading SR failed\n", __func__); + dev_err(dev, "%s: reading SR failed\n", __func__); return -EIO; } - ret = rv3029_read_regs(client, RV3029_A_SC, regs, + ret = rv3029_read_regs(dev, RV3029_A_SC, regs, RV3029_ALARM_SECTION_LEN); if (ret < 0) { - dev_err(&client->dev, "%s: reading alarm section failed\n", - __func__); + dev_err(dev, "%s: reading alarm section failed\n", __func__); return ret; } @@ -406,25 +401,23 @@ static int rv3029_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) return 0; } -static int rv3029_rtc_alarm_set_irq(struct i2c_client *client, int enable) +static int rv3029_rtc_alarm_set_irq(struct device *dev, int enable) { int ret; /* enable/disable AIE irq */ - ret = rv3029_update_bits(client, RV3029_IRQ_CTRL, - RV3029_IRQ_CTRL_AIE, + ret = rv3029_update_bits(dev, RV3029_IRQ_CTRL, RV3029_IRQ_CTRL_AIE, (enable ? RV3029_IRQ_CTRL_AIE : 0)); if (ret < 0) { - dev_err(&client->dev, "can't update INT reg\n"); + dev_err(dev, "can't update INT reg\n"); return ret; } return 0; } -static int rv3029_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) +static int rv3029_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) { - struct i2c_client *client = to_i2c_client(dev); struct rtc_time *const tm = &alarm->time; int ret; u8 regs[8]; @@ -437,9 +430,9 @@ static int rv3029_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) if (tm->tm_year < 100) return -EINVAL; - ret = rv3029_get_sr(client, regs); + ret = rv3029_get_sr(dev, regs); if (ret < 0) { - dev_err(&client->dev, "%s: reading SR failed\n", __func__); + dev_err(dev, "%s: reading SR failed\n", __func__); return -EIO; } regs[RV3029_A_SC-RV3029_A_SC] = bin2bcd(tm->tm_sec & 0x7f); @@ -450,38 +443,38 @@ static int rv3029_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) 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_write_regs(client, RV3029_A_SC, regs, + ret = rv3029_write_regs(dev, RV3029_A_SC, regs, RV3029_ALARM_SECTION_LEN); if (ret < 0) return ret; if (alarm->enabled) { /* clear AF flag */ - ret = rv3029_update_bits(client, RV3029_IRQ_FLAGS, + ret = rv3029_update_bits(dev, RV3029_IRQ_FLAGS, RV3029_IRQ_FLAGS_AF, 0); if (ret < 0) { - dev_err(&client->dev, "can't clear alarm flag\n"); + dev_err(dev, "can't clear alarm flag\n"); return ret; } /* enable AIE irq */ - ret = rv3029_rtc_alarm_set_irq(client, 1); + ret = rv3029_rtc_alarm_set_irq(dev, 1); if (ret) return ret; - dev_dbg(&client->dev, "alarm IRQ armed\n"); + dev_dbg(dev, "alarm IRQ armed\n"); } else { /* disable AIE irq */ - ret = rv3029_rtc_alarm_set_irq(client, 0); + ret = rv3029_rtc_alarm_set_irq(dev, 0); if (ret) return ret; - dev_dbg(&client->dev, "alarm IRQ disabled\n"); + dev_dbg(dev, "alarm IRQ disabled\n"); } return 0; } -static int rv3029_set_time(struct i2c_client *client, struct rtc_time const *tm) +static int rv3029_set_time(struct device *dev, struct rtc_time *tm) { u8 regs[8]; int ret; @@ -502,31 +495,25 @@ static int rv3029_set_time(struct i2c_client *client, struct rtc_time const *tm) 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); - ret = rv3029_write_regs(client, RV3029_W_SEC, regs, + ret = rv3029_write_regs(dev, RV3029_W_SEC, regs, RV3029_WATCH_SECTION_LEN); if (ret < 0) return ret; - ret = rv3029_get_sr(client, regs); + ret = rv3029_get_sr(dev, regs); if (ret < 0) { - dev_err(&client->dev, "%s: reading SR failed\n", __func__); + dev_err(dev, "%s: reading SR failed\n", __func__); return ret; } /* clear PON bit */ - ret = rv3029_set_sr(client, (regs[0] & ~RV3029_STATUS_PON)); + ret = rv3029_set_sr(dev, (regs[0] & ~RV3029_STATUS_PON)); if (ret < 0) { - dev_err(&client->dev, "%s: reading SR failed\n", __func__); + dev_err(dev, "%s: reading SR failed\n", __func__); return ret; } return 0; } - -static int rv3029_rtc_set_time(struct device *dev, struct rtc_time *tm) -{ - return rv3029_set_time(to_i2c_client(dev), tm); -} - static const struct rv3029_trickle_tab_elem { u32 r; /* resistance in ohms */ u8 conf; /* trickle config bits */ @@ -584,9 +571,9 @@ static const struct rv3029_trickle_tab_elem { }, }; -static void rv3029_trickle_config(struct i2c_client *client) +static void rv3029_trickle_config(struct device *dev) { - struct device_node *of_node = client->dev.of_node; + struct device_node *of_node = dev->of_node; const struct rv3029_trickle_tab_elem *elem; int i, err; u32 ohms; @@ -608,27 +595,26 @@ static void rv3029_trickle_config(struct i2c_client *client) break; } trickle_set_bits = elem->conf; - dev_info(&client->dev, + dev_info(dev, "Trickle charger enabled at %d ohms resistance.\n", elem->r); } - err = rv3029_eeprom_update_bits(client, RV3029_CONTROL_E2P_EECTRL, + err = rv3029_eeprom_update_bits(dev, RV3029_CONTROL_E2P_EECTRL, RV3029_TRICKLE_MASK, trickle_set_bits); if (err < 0) { - dev_err(&client->dev, - "Failed to update trickle charger config\n"); + dev_err(dev, "Failed to update trickle charger config\n"); } } #ifdef CONFIG_RTC_DRV_RV3029_HWMON -static int rv3029_read_temp(struct i2c_client *client, int *temp_mC) +static int rv3029_read_temp(struct device *dev, int *temp_mC) { int ret; u8 temp; - ret = rv3029_read_regs(client, RV3029_TEMP_PAGE, &temp, 1); + ret = rv3029_read_regs(dev, RV3029_TEMP_PAGE, &temp, 1); if (ret < 0) return ret; @@ -641,10 +627,9 @@ static ssize_t rv3029_hwmon_show_temp(struct device *dev, struct device_attribute *attr, char *buf) { - struct i2c_client *client = dev_get_drvdata(dev); int ret, temp_mC; - ret = rv3029_read_temp(client, &temp_mC); + ret = rv3029_read_temp(dev, &temp_mC); if (ret < 0) return ret; @@ -656,7 +641,6 @@ static ssize_t rv3029_hwmon_set_update_interval(struct device *dev, const char *buf, size_t count) { - struct i2c_client *client = dev_get_drvdata(dev); unsigned long interval_ms; int ret; u8 th_set_bits = 0; @@ -670,7 +654,7 @@ static ssize_t rv3029_hwmon_set_update_interval(struct device *dev, if (interval_ms >= 16000) th_set_bits |= RV3029_EECTRL_THP; } - ret = rv3029_eeprom_update_bits(client, RV3029_CONTROL_E2P_EECTRL, + ret = rv3029_eeprom_update_bits(dev, RV3029_CONTROL_E2P_EECTRL, RV3029_EECTRL_THE | RV3029_EECTRL_THP, th_set_bits); if (ret < 0) @@ -683,11 +667,10 @@ static ssize_t rv3029_hwmon_show_update_interval(struct device *dev, struct device_attribute *attr, char *buf) { - struct i2c_client *client = dev_get_drvdata(dev); int ret, interval_ms; u8 eectrl; - ret = rv3029_eeprom_read(client, RV3029_CONTROL_E2P_EECTRL, + ret = rv3029_eeprom_read(dev, RV3029_CONTROL_E2P_EECTRL, &eectrl, 1); if (ret < 0) return ret; @@ -717,32 +700,32 @@ static struct attribute *rv3029_hwmon_attrs[] = { }; ATTRIBUTE_GROUPS(rv3029_hwmon); -static void rv3029_hwmon_register(struct i2c_client *client) +static void rv3029_hwmon_register(struct device *dev, const char *name) { + struct rv3029_data *rv3029 = dev_get_drvdata(dev); struct device *hwmon_dev; - hwmon_dev = devm_hwmon_device_register_with_groups( - &client->dev, client->name, client, rv3029_hwmon_groups); + hwmon_dev = devm_hwmon_device_register_with_groups(dev, name, rv3029, + rv3029_hwmon_groups); if (IS_ERR(hwmon_dev)) { - dev_warn(&client->dev, - "unable to register hwmon device %ld\n", + dev_warn(dev, "unable to register hwmon device %ld\n", PTR_ERR(hwmon_dev)); } } #else /* CONFIG_RTC_DRV_RV3029_HWMON */ -static void rv3029_hwmon_register(struct i2c_client *client) +static void rv3029_hwmon_register(struct device *dev, const char *name) { } #endif /* CONFIG_RTC_DRV_RV3029_HWMON */ 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, + .read_time = rv3029_read_time, + .set_time = rv3029_set_time, + .read_alarm = rv3029_read_alarm, + .set_alarm = rv3029_set_alarm, }; static struct i2c_device_id rv3029_id[] = { @@ -752,41 +735,67 @@ static struct i2c_device_id rv3029_id[] = { }; MODULE_DEVICE_TABLE(i2c, rv3029_id); -static int rv3029_probe(struct i2c_client *client, - const struct i2c_device_id *id) +static int rv3029_probe(struct device *dev, struct regmap *regmap, int irq, + const char *name) { - struct rtc_device *rtc; + struct rv3029_data *rv3029; int rc = 0; u8 buf[1]; - if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_EMUL)) - return -ENODEV; + rv3029 = devm_kzalloc(dev, sizeof(*rv3029), GFP_KERNEL); + if (!rv3029) + return -ENOMEM; + + rv3029->regmap = regmap; + rv3029->irq = irq; + rv3029->dev = dev; + dev_set_drvdata(dev, rv3029); - rc = rv3029_get_sr(client, buf); + rc = rv3029_get_sr(dev, buf); if (rc < 0) { - dev_err(&client->dev, "reading status failed\n"); + dev_err(dev, "reading status failed\n"); return rc; } - rv3029_trickle_config(client); - rv3029_hwmon_register(client); + rv3029_trickle_config(dev); + rv3029_hwmon_register(dev, name); + + rv3029->rtc = devm_rtc_device_register(dev, name, &rv3029_rtc_ops, + THIS_MODULE); - rtc = devm_rtc_device_register(&client->dev, client->name, - &rv3029_rtc_ops, THIS_MODULE); + return PTR_ERR_OR_ZERO(rv3029->rtc); +} - if (IS_ERR(rtc)) - return PTR_ERR(rtc); +static int rv3029_i2c_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct regmap *regmap; + static const struct regmap_config config = { + .reg_bits = 8, + .val_bits = 8, + }; + + if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK | + I2C_FUNC_SMBUS_BYTE)) { + dev_err(&client->dev, "Adapter does not support SMBUS_I2C_BLOCK or SMBUS_I2C_BYTE\n"); + return -ENODEV; + } - i2c_set_clientdata(client, rtc); + regmap = devm_regmap_init_i2c(client, &config); + if (IS_ERR(regmap)) { + dev_err(&client->dev, "%s: regmap allocation failed: %ld\n", + __func__, PTR_ERR(regmap)); + return PTR_ERR(regmap); + } - return 0; + return rv3029_probe(&client->dev, regmap, client->irq, client->name); } static struct i2c_driver rv3029_driver = { .driver = { .name = "rtc-rv3029c2", }, - .probe = rv3029_probe, + .probe = rv3029_i2c_probe, .id_table = rv3029_id, }; |