Commit 87c78d95 authored by Alexandre Belloni's avatar Alexandre Belloni

rtc: ds1742: use generic nvmem

Instead of adding a binary sysfs attribute from the driver (which suffers
from a race condition as the attribute appears after the device), use the
core to register an nvmem device.
Signed-off-by: default avatarAlexandre Belloni <alexandre.belloni@bootlin.com>
parent 1358e7b2
...@@ -53,9 +53,7 @@ ...@@ -53,9 +53,7 @@
struct rtc_plat_data { struct rtc_plat_data {
void __iomem *ioaddr_nvram; void __iomem *ioaddr_nvram;
void __iomem *ioaddr_rtc; void __iomem *ioaddr_rtc;
size_t size_nvram;
unsigned long last_jiffies; unsigned long last_jiffies;
struct bin_attribute nvram_attr;
}; };
static int ds1742_rtc_set_time(struct device *dev, struct rtc_time *tm) static int ds1742_rtc_set_time(struct device *dev, struct rtc_time *tm)
...@@ -122,34 +120,28 @@ static const struct rtc_class_ops ds1742_rtc_ops = { ...@@ -122,34 +120,28 @@ static const struct rtc_class_ops ds1742_rtc_ops = {
.set_time = ds1742_rtc_set_time, .set_time = ds1742_rtc_set_time,
}; };
static ssize_t ds1742_nvram_read(struct file *filp, struct kobject *kobj, static int ds1742_nvram_read(void *priv, unsigned int pos, void *val,
struct bin_attribute *bin_attr, size_t bytes)
char *buf, loff_t pos, size_t size)
{ {
struct device *dev = container_of(kobj, struct device, kobj); struct rtc_plat_data *pdata = priv;
struct platform_device *pdev = to_platform_device(dev);
struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
void __iomem *ioaddr = pdata->ioaddr_nvram; void __iomem *ioaddr = pdata->ioaddr_nvram;
ssize_t count; u8 *buf = val;
for (count = 0; count < size; count++) for (; bytes; bytes--)
*buf++ = readb(ioaddr + pos++); *buf++ = readb(ioaddr + pos++);
return count; return 0;
} }
static ssize_t ds1742_nvram_write(struct file *filp, struct kobject *kobj, static int ds1742_nvram_write(void *priv, unsigned int pos, void *val,
struct bin_attribute *bin_attr, size_t bytes)
char *buf, loff_t pos, size_t size)
{ {
struct device *dev = container_of(kobj, struct device, kobj); struct rtc_plat_data *pdata = priv;
struct platform_device *pdev = to_platform_device(dev);
struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
void __iomem *ioaddr = pdata->ioaddr_nvram; void __iomem *ioaddr = pdata->ioaddr_nvram;
ssize_t count; u8 *buf = val;
for (count = 0; count < size; count++) for (; bytes; bytes--)
writeb(*buf++, ioaddr + pos++); writeb(*buf++, ioaddr + pos++);
return count; return 0;
} }
static int ds1742_rtc_probe(struct platform_device *pdev) static int ds1742_rtc_probe(struct platform_device *pdev)
...@@ -160,6 +152,14 @@ static int ds1742_rtc_probe(struct platform_device *pdev) ...@@ -160,6 +152,14 @@ static int ds1742_rtc_probe(struct platform_device *pdev)
struct rtc_plat_data *pdata; struct rtc_plat_data *pdata;
void __iomem *ioaddr; void __iomem *ioaddr;
int ret = 0; int ret = 0;
struct nvmem_config nvmem_cfg = {
.name = "ds1742_nvram",
.word_size = 1,
.stride = 1,
.reg_read = ds1742_nvram_read,
.reg_write = ds1742_nvram_write,
};
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata) if (!pdata)
...@@ -171,15 +171,10 @@ static int ds1742_rtc_probe(struct platform_device *pdev) ...@@ -171,15 +171,10 @@ static int ds1742_rtc_probe(struct platform_device *pdev)
return PTR_ERR(ioaddr); return PTR_ERR(ioaddr);
pdata->ioaddr_nvram = ioaddr; pdata->ioaddr_nvram = ioaddr;
pdata->size_nvram = resource_size(res) - RTC_SIZE; pdata->ioaddr_rtc = ioaddr + resource_size(res) - RTC_SIZE;
pdata->ioaddr_rtc = ioaddr + pdata->size_nvram;
sysfs_bin_attr_init(&pdata->nvram_attr); nvmem_cfg.size = resource_size(res) - RTC_SIZE;
pdata->nvram_attr.attr.name = "nvram"; nvmem_cfg.priv = pdata;
pdata->nvram_attr.attr.mode = S_IRUGO | S_IWUSR;
pdata->nvram_attr.read = ds1742_nvram_read;
pdata->nvram_attr.write = ds1742_nvram_write;
pdata->nvram_attr.size = pdata->size_nvram;
/* turn RTC on if it was not on */ /* turn RTC on if it was not on */
ioaddr = pdata->ioaddr_rtc; ioaddr = pdata->ioaddr_rtc;
...@@ -202,24 +197,15 @@ static int ds1742_rtc_probe(struct platform_device *pdev) ...@@ -202,24 +197,15 @@ static int ds1742_rtc_probe(struct platform_device *pdev)
return PTR_ERR(rtc); return PTR_ERR(rtc);
rtc->ops = &ds1742_rtc_ops; rtc->ops = &ds1742_rtc_ops;
rtc->nvram_old_abi = true;
ret = rtc_register_device(rtc); ret = rtc_register_device(rtc);
if (ret) if (ret)
return ret; return ret;
ret = sysfs_create_bin_file(&pdev->dev.kobj, &pdata->nvram_attr); if (rtc_nvmem_register(rtc, &nvmem_cfg))
if (ret) dev_err(&pdev->dev, "Unable to register nvmem\n");
dev_err(&pdev->dev, "Unable to create sysfs entry: %s\n",
pdata->nvram_attr.attr.name);
return 0;
}
static int ds1742_rtc_remove(struct platform_device *pdev)
{
struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
sysfs_remove_bin_file(&pdev->dev.kobj, &pdata->nvram_attr);
return 0; return 0;
} }
...@@ -231,7 +217,6 @@ MODULE_DEVICE_TABLE(of, ds1742_rtc_of_match); ...@@ -231,7 +217,6 @@ MODULE_DEVICE_TABLE(of, ds1742_rtc_of_match);
static struct platform_driver ds1742_rtc_driver = { static struct platform_driver ds1742_rtc_driver = {
.probe = ds1742_rtc_probe, .probe = ds1742_rtc_probe,
.remove = ds1742_rtc_remove,
.driver = { .driver = {
.name = "rtc-ds1742", .name = "rtc-ds1742",
.of_match_table = of_match_ptr(ds1742_rtc_of_match), .of_match_table = of_match_ptr(ds1742_rtc_of_match),
......
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