2006-06-25 19:48:29 +07:00
|
|
|
/*
|
|
|
|
* An rtc driver for the Dallas DS1742
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
|
|
|
|
*
|
|
|
|
* 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.
|
2006-12-07 11:39:41 +07:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Torsten Ertbjerg Rasmussen <tr@newtec.dk>
|
|
|
|
* - nvram size determined from resource
|
|
|
|
* - this ds1742 driver now supports ds1743.
|
2006-06-25 19:48:29 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/bcd.h>
|
|
|
|
#include <linux/kernel.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 15:04:11 +07:00
|
|
|
#include <linux/gfp.h>
|
2006-06-25 19:48:29 +07:00
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/jiffies.h>
|
|
|
|
#include <linux/rtc.h>
|
2014-01-24 06:55:06 +07:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_device.h>
|
2006-06-25 19:48:29 +07:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/io.h>
|
2011-05-27 20:57:25 +07:00
|
|
|
#include <linux/module.h>
|
2006-06-25 19:48:29 +07:00
|
|
|
|
2009-12-16 07:46:02 +07:00
|
|
|
#define DRV_VERSION "0.4"
|
2006-06-25 19:48:29 +07:00
|
|
|
|
2006-12-07 11:39:41 +07:00
|
|
|
#define RTC_SIZE 8
|
2006-06-25 19:48:29 +07:00
|
|
|
|
2006-12-07 11:39:41 +07:00
|
|
|
#define RTC_CONTROL 0
|
|
|
|
#define RTC_CENTURY 0
|
|
|
|
#define RTC_SECONDS 1
|
|
|
|
#define RTC_MINUTES 2
|
|
|
|
#define RTC_HOURS 3
|
|
|
|
#define RTC_DAY 4
|
|
|
|
#define RTC_DATE 5
|
|
|
|
#define RTC_MONTH 6
|
|
|
|
#define RTC_YEAR 7
|
2006-06-25 19:48:29 +07:00
|
|
|
|
|
|
|
#define RTC_CENTURY_MASK 0x3f
|
|
|
|
#define RTC_SECONDS_MASK 0x7f
|
|
|
|
#define RTC_DAY_MASK 0x07
|
|
|
|
|
|
|
|
/* Bits in the Control/Century register */
|
|
|
|
#define RTC_WRITE 0x80
|
|
|
|
#define RTC_READ 0x40
|
|
|
|
|
|
|
|
/* Bits in the Seconds register */
|
|
|
|
#define RTC_STOP 0x80
|
|
|
|
|
|
|
|
/* Bits in the Day register */
|
|
|
|
#define RTC_BATT_FLAG 0x80
|
|
|
|
|
|
|
|
struct rtc_plat_data {
|
2006-12-07 11:39:41 +07:00
|
|
|
void __iomem *ioaddr_nvram;
|
|
|
|
void __iomem *ioaddr_rtc;
|
|
|
|
size_t size_nvram;
|
2006-06-25 19:48:29 +07:00
|
|
|
unsigned long last_jiffies;
|
2009-06-18 06:26:07 +07:00
|
|
|
struct bin_attribute nvram_attr;
|
2006-06-25 19:48:29 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static int ds1742_rtc_set_time(struct device *dev, struct rtc_time *tm)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
|
2006-12-07 11:39:41 +07:00
|
|
|
void __iomem *ioaddr = pdata->ioaddr_rtc;
|
2006-06-25 19:48:29 +07:00
|
|
|
u8 century;
|
|
|
|
|
2008-10-19 10:28:41 +07:00
|
|
|
century = bin2bcd((tm->tm_year + 1900) / 100);
|
2006-06-25 19:48:29 +07:00
|
|
|
|
|
|
|
writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
|
|
|
|
|
2008-10-19 10:28:41 +07:00
|
|
|
writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
|
|
|
|
writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
|
|
|
|
writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
|
|
|
|
writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
|
|
|
|
writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
|
|
|
|
writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
|
|
|
|
writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
|
2006-06-25 19:48:29 +07:00
|
|
|
|
|
|
|
/* RTC_CENTURY and RTC_CONTROL share same register */
|
|
|
|
writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY);
|
|
|
|
writeb(century & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ds1742_rtc_read_time(struct device *dev, struct rtc_time *tm)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
|
2006-12-07 11:39:41 +07:00
|
|
|
void __iomem *ioaddr = pdata->ioaddr_rtc;
|
2006-06-25 19:48:29 +07:00
|
|
|
unsigned int year, month, day, hour, minute, second, week;
|
|
|
|
unsigned int century;
|
|
|
|
|
|
|
|
/* give enough time to update RTC in case of continuous read */
|
|
|
|
if (pdata->last_jiffies == jiffies)
|
|
|
|
msleep(1);
|
|
|
|
pdata->last_jiffies = jiffies;
|
|
|
|
writeb(RTC_READ, ioaddr + RTC_CONTROL);
|
|
|
|
second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
|
|
|
|
minute = readb(ioaddr + RTC_MINUTES);
|
|
|
|
hour = readb(ioaddr + RTC_HOURS);
|
|
|
|
day = readb(ioaddr + RTC_DATE);
|
|
|
|
week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
|
|
|
|
month = readb(ioaddr + RTC_MONTH);
|
|
|
|
year = readb(ioaddr + RTC_YEAR);
|
|
|
|
century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
|
|
|
|
writeb(0, ioaddr + RTC_CONTROL);
|
2008-10-19 10:28:41 +07:00
|
|
|
tm->tm_sec = bcd2bin(second);
|
|
|
|
tm->tm_min = bcd2bin(minute);
|
|
|
|
tm->tm_hour = bcd2bin(hour);
|
|
|
|
tm->tm_mday = bcd2bin(day);
|
|
|
|
tm->tm_wday = bcd2bin(week);
|
|
|
|
tm->tm_mon = bcd2bin(month) - 1;
|
2006-06-25 19:48:29 +07:00
|
|
|
/* year is 1900 + tm->tm_year */
|
2008-10-19 10:28:41 +07:00
|
|
|
tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
|
2006-06-25 19:48:29 +07:00
|
|
|
|
2013-09-12 04:24:26 +07:00
|
|
|
return rtc_valid_tm(tm);
|
2006-06-25 19:48:29 +07:00
|
|
|
}
|
|
|
|
|
2006-10-01 13:28:17 +07:00
|
|
|
static const struct rtc_class_ops ds1742_rtc_ops = {
|
2006-06-25 19:48:29 +07:00
|
|
|
.read_time = ds1742_rtc_read_time,
|
|
|
|
.set_time = ds1742_rtc_set_time,
|
|
|
|
};
|
|
|
|
|
2010-05-13 08:28:57 +07:00
|
|
|
static ssize_t ds1742_nvram_read(struct file *filp, struct kobject *kobj,
|
2007-06-09 12:57:22 +07:00
|
|
|
struct bin_attribute *bin_attr,
|
|
|
|
char *buf, loff_t pos, size_t size)
|
2006-06-25 19:48:29 +07:00
|
|
|
{
|
2009-12-16 07:46:04 +07:00
|
|
|
struct device *dev = container_of(kobj, struct device, kobj);
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
2006-06-25 19:48:29 +07:00
|
|
|
struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
|
2006-12-07 11:39:41 +07:00
|
|
|
void __iomem *ioaddr = pdata->ioaddr_nvram;
|
2006-06-25 19:48:29 +07:00
|
|
|
ssize_t count;
|
|
|
|
|
2015-07-27 04:48:32 +07:00
|
|
|
for (count = 0; count < size; count++)
|
2006-06-25 19:48:29 +07:00
|
|
|
*buf++ = readb(ioaddr + pos++);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2010-05-13 08:28:57 +07:00
|
|
|
static ssize_t ds1742_nvram_write(struct file *filp, struct kobject *kobj,
|
2007-06-09 12:57:22 +07:00
|
|
|
struct bin_attribute *bin_attr,
|
|
|
|
char *buf, loff_t pos, size_t size)
|
2006-06-25 19:48:29 +07:00
|
|
|
{
|
2009-12-16 07:46:04 +07:00
|
|
|
struct device *dev = container_of(kobj, struct device, kobj);
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
2006-06-25 19:48:29 +07:00
|
|
|
struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
|
2006-12-07 11:39:41 +07:00
|
|
|
void __iomem *ioaddr = pdata->ioaddr_nvram;
|
2006-06-25 19:48:29 +07:00
|
|
|
ssize_t count;
|
|
|
|
|
2015-07-27 04:48:32 +07:00
|
|
|
for (count = 0; count < size; count++)
|
2006-06-25 19:48:29 +07:00
|
|
|
writeb(*buf++, ioaddr + pos++);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-12-22 04:09:38 +07:00
|
|
|
static int ds1742_rtc_probe(struct platform_device *pdev)
|
2006-06-25 19:48:29 +07:00
|
|
|
{
|
|
|
|
struct rtc_device *rtc;
|
|
|
|
struct resource *res;
|
|
|
|
unsigned int cen, sec;
|
2009-12-16 07:46:02 +07:00
|
|
|
struct rtc_plat_data *pdata;
|
|
|
|
void __iomem *ioaddr;
|
2006-06-25 19:48:29 +07:00
|
|
|
int ret = 0;
|
|
|
|
|
2009-12-16 07:46:02 +07:00
|
|
|
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
|
2006-06-25 19:48:29 +07:00
|
|
|
if (!pdata)
|
|
|
|
return -ENOMEM;
|
2013-09-12 04:24:24 +07:00
|
|
|
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
ioaddr = devm_ioremap_resource(&pdev->dev, res);
|
|
|
|
if (IS_ERR(ioaddr))
|
|
|
|
return PTR_ERR(ioaddr);
|
2009-12-16 07:46:02 +07:00
|
|
|
|
2006-12-07 11:39:41 +07:00
|
|
|
pdata->ioaddr_nvram = ioaddr;
|
2013-09-12 04:24:24 +07:00
|
|
|
pdata->size_nvram = resource_size(res) - RTC_SIZE;
|
2006-12-07 11:39:41 +07:00
|
|
|
pdata->ioaddr_rtc = ioaddr + pdata->size_nvram;
|
2006-06-25 19:48:29 +07:00
|
|
|
|
2010-03-15 07:29:41 +07:00
|
|
|
sysfs_bin_attr_init(&pdata->nvram_attr);
|
2009-06-18 06:26:07 +07:00
|
|
|
pdata->nvram_attr.attr.name = "nvram";
|
|
|
|
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;
|
|
|
|
|
2006-06-25 19:48:29 +07:00
|
|
|
/* turn RTC on if it was not on */
|
2006-12-07 11:39:41 +07:00
|
|
|
ioaddr = pdata->ioaddr_rtc;
|
2006-06-25 19:48:29 +07:00
|
|
|
sec = readb(ioaddr + RTC_SECONDS);
|
|
|
|
if (sec & RTC_STOP) {
|
|
|
|
sec &= RTC_SECONDS_MASK;
|
|
|
|
cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
|
|
|
|
writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
|
|
|
|
writeb(sec, ioaddr + RTC_SECONDS);
|
|
|
|
writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
|
|
|
|
}
|
2006-10-01 13:28:18 +07:00
|
|
|
if (!(readb(ioaddr + RTC_DAY) & RTC_BATT_FLAG))
|
2006-06-25 19:48:29 +07:00
|
|
|
dev_warn(&pdev->dev, "voltage-low detected.\n");
|
|
|
|
|
2009-12-16 07:46:02 +07:00
|
|
|
pdata->last_jiffies = jiffies;
|
|
|
|
platform_set_drvdata(pdev, pdata);
|
2013-04-30 06:19:01 +07:00
|
|
|
rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
|
2006-06-25 19:48:29 +07:00
|
|
|
&ds1742_rtc_ops, THIS_MODULE);
|
2009-12-16 07:46:02 +07:00
|
|
|
if (IS_ERR(rtc))
|
|
|
|
return PTR_ERR(rtc);
|
2009-06-18 06:26:07 +07:00
|
|
|
|
|
|
|
ret = sysfs_create_bin_file(&pdev->dev.kobj, &pdata->nvram_attr);
|
2014-04-04 04:49:36 +07:00
|
|
|
if (ret)
|
|
|
|
dev_err(&pdev->dev, "Unable to create sysfs entry: %s\n",
|
|
|
|
pdata->nvram_attr.attr.name);
|
2013-04-30 06:19:01 +07:00
|
|
|
|
2014-04-04 04:49:36 +07:00
|
|
|
return 0;
|
2006-06-25 19:48:29 +07:00
|
|
|
}
|
|
|
|
|
2012-12-22 04:09:38 +07:00
|
|
|
static int ds1742_rtc_remove(struct platform_device *pdev)
|
2006-06-25 19:48:29 +07:00
|
|
|
{
|
|
|
|
struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
|
|
|
|
|
2009-06-18 06:26:07 +07:00
|
|
|
sysfs_remove_bin_file(&pdev->dev.kobj, &pdata->nvram_attr);
|
2006-06-25 19:48:29 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-07 04:35:54 +07:00
|
|
|
static const struct of_device_id __maybe_unused ds1742_rtc_of_match[] = {
|
2014-01-24 06:55:06 +07:00
|
|
|
{ .compatible = "maxim,ds1742", },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, ds1742_rtc_of_match);
|
|
|
|
|
2006-06-25 19:48:29 +07:00
|
|
|
static struct platform_driver ds1742_rtc_driver = {
|
|
|
|
.probe = ds1742_rtc_probe,
|
2012-12-22 04:09:38 +07:00
|
|
|
.remove = ds1742_rtc_remove,
|
2006-06-25 19:48:29 +07:00
|
|
|
.driver = {
|
2007-10-16 15:28:18 +07:00
|
|
|
.name = "rtc-ds1742",
|
2014-08-09 04:20:07 +07:00
|
|
|
.of_match_table = of_match_ptr(ds1742_rtc_of_match),
|
2006-06-25 19:48:29 +07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2012-01-11 06:10:48 +07:00
|
|
|
module_platform_driver(ds1742_rtc_driver);
|
2006-06-25 19:48:29 +07:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
|
|
|
|
MODULE_DESCRIPTION("Dallas DS1742 RTC driver");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_VERSION(DRV_VERSION);
|
2008-04-11 11:29:25 +07:00
|
|
|
MODULE_ALIAS("platform:rtc-ds1742");
|