2019-06-04 08:11:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2008-11-12 21:27:07 +00:00
|
|
|
/*
|
2009-01-06 22:42:16 +00:00
|
|
|
* rtc-ds1390.c -- driver for the Dallas/Maxim DS1390/93/94 SPI RTC
|
2008-11-12 21:27:07 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Mercury IMC Ltd
|
|
|
|
* Written by Mark Jackson <mpfj@mimc.co.uk>
|
|
|
|
*
|
2009-01-06 22:42:16 +00:00
|
|
|
* NOTE: Currently this driver only supports the bare minimum for read
|
|
|
|
* and write the RTC. The extra features provided by the chip family
|
2008-11-12 21:27:07 +00:00
|
|
|
* (alarms, trickle charger, different control registers) are unavailable.
|
|
|
|
*/
|
|
|
|
|
2009-01-06 22:42:16 +00:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
2008-11-12 21:27:07 +00:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/rtc.h>
|
|
|
|
#include <linux/spi/spi.h>
|
|
|
|
#include <linux/bcd.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 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2015-09-18 15:27:57 +00:00
|
|
|
#include <linux/of.h>
|
2008-11-12 21:27:07 +00:00
|
|
|
|
|
|
|
#define DS1390_REG_100THS 0x00
|
|
|
|
#define DS1390_REG_SECONDS 0x01
|
|
|
|
#define DS1390_REG_MINUTES 0x02
|
|
|
|
#define DS1390_REG_HOURS 0x03
|
|
|
|
#define DS1390_REG_DAY 0x04
|
|
|
|
#define DS1390_REG_DATE 0x05
|
|
|
|
#define DS1390_REG_MONTH_CENT 0x06
|
|
|
|
#define DS1390_REG_YEAR 0x07
|
|
|
|
|
|
|
|
#define DS1390_REG_ALARM_100THS 0x08
|
|
|
|
#define DS1390_REG_ALARM_SECONDS 0x09
|
|
|
|
#define DS1390_REG_ALARM_MINUTES 0x0A
|
|
|
|
#define DS1390_REG_ALARM_HOURS 0x0B
|
|
|
|
#define DS1390_REG_ALARM_DAY_DATE 0x0C
|
|
|
|
|
|
|
|
#define DS1390_REG_CONTROL 0x0D
|
|
|
|
#define DS1390_REG_STATUS 0x0E
|
|
|
|
#define DS1390_REG_TRICKLE 0x0F
|
|
|
|
|
2015-09-18 15:27:57 +00:00
|
|
|
#define DS1390_TRICKLE_CHARGER_ENABLE 0xA0
|
|
|
|
#define DS1390_TRICKLE_CHARGER_250_OHM 0x01
|
|
|
|
#define DS1390_TRICKLE_CHARGER_2K_OHM 0x02
|
|
|
|
#define DS1390_TRICKLE_CHARGER_4K_OHM 0x03
|
|
|
|
#define DS1390_TRICKLE_CHARGER_NO_DIODE 0x04
|
|
|
|
#define DS1390_TRICKLE_CHARGER_DIODE 0x08
|
|
|
|
|
2008-11-12 21:27:07 +00:00
|
|
|
struct ds1390 {
|
|
|
|
struct rtc_device *rtc;
|
|
|
|
u8 txrx_buf[9]; /* cmd + 8 registers */
|
|
|
|
};
|
|
|
|
|
2015-09-18 15:27:57 +00:00
|
|
|
static void ds1390_set_reg(struct device *dev, unsigned char address,
|
|
|
|
unsigned char data)
|
|
|
|
{
|
|
|
|
struct spi_device *spi = to_spi_device(dev);
|
|
|
|
unsigned char buf[2];
|
|
|
|
|
|
|
|
/* MSB must be '1' to write */
|
|
|
|
buf[0] = address | 0x80;
|
|
|
|
buf[1] = data;
|
|
|
|
|
|
|
|
spi_write(spi, buf, 2);
|
|
|
|
}
|
|
|
|
|
2008-11-12 21:27:07 +00:00
|
|
|
static int ds1390_get_reg(struct device *dev, unsigned char address,
|
|
|
|
unsigned char *data)
|
|
|
|
{
|
|
|
|
struct spi_device *spi = to_spi_device(dev);
|
|
|
|
struct ds1390 *chip = dev_get_drvdata(dev);
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Clear MSB to indicate read */
|
|
|
|
chip->txrx_buf[0] = address & 0x7f;
|
|
|
|
/* do the i/o */
|
|
|
|
status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 1);
|
|
|
|
if (status != 0)
|
|
|
|
return status;
|
|
|
|
|
2015-09-18 15:27:56 +00:00
|
|
|
*data = chip->txrx_buf[0];
|
2008-11-12 21:27:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-18 15:27:57 +00:00
|
|
|
static void ds1390_trickle_of_init(struct spi_device *spi)
|
|
|
|
{
|
|
|
|
u32 ohms = 0;
|
|
|
|
u8 value;
|
|
|
|
|
|
|
|
if (of_property_read_u32(spi->dev.of_node, "trickle-resistor-ohms",
|
|
|
|
&ohms))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Enable charger */
|
|
|
|
value = DS1390_TRICKLE_CHARGER_ENABLE;
|
|
|
|
if (of_property_read_bool(spi->dev.of_node, "trickle-diode-disable"))
|
|
|
|
value |= DS1390_TRICKLE_CHARGER_NO_DIODE;
|
|
|
|
else
|
|
|
|
value |= DS1390_TRICKLE_CHARGER_DIODE;
|
|
|
|
|
|
|
|
/* Resistor select */
|
|
|
|
switch (ohms) {
|
|
|
|
case 250:
|
|
|
|
value |= DS1390_TRICKLE_CHARGER_250_OHM;
|
|
|
|
break;
|
|
|
|
case 2000:
|
|
|
|
value |= DS1390_TRICKLE_CHARGER_2K_OHM;
|
|
|
|
break;
|
|
|
|
case 4000:
|
|
|
|
value |= DS1390_TRICKLE_CHARGER_4K_OHM;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_warn(&spi->dev,
|
|
|
|
"Unsupported ohm value %02ux in dt\n", ohms);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ds1390_set_reg(&spi->dev, DS1390_REG_TRICKLE, value);
|
|
|
|
|
|
|
|
out:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-06 22:42:16 +00:00
|
|
|
static int ds1390_read_time(struct device *dev, struct rtc_time *dt)
|
2008-11-12 21:27:07 +00:00
|
|
|
{
|
|
|
|
struct spi_device *spi = to_spi_device(dev);
|
|
|
|
struct ds1390 *chip = dev_get_drvdata(dev);
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* build the message */
|
|
|
|
chip->txrx_buf[0] = DS1390_REG_SECONDS;
|
|
|
|
|
|
|
|
/* do the i/o */
|
|
|
|
status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 8);
|
|
|
|
if (status != 0)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
/* The chip sends data in this order:
|
|
|
|
* Seconds, Minutes, Hours, Day, Date, Month / Century, Year */
|
|
|
|
dt->tm_sec = bcd2bin(chip->txrx_buf[0]);
|
|
|
|
dt->tm_min = bcd2bin(chip->txrx_buf[1]);
|
|
|
|
dt->tm_hour = bcd2bin(chip->txrx_buf[2]);
|
|
|
|
dt->tm_wday = bcd2bin(chip->txrx_buf[3]);
|
|
|
|
dt->tm_mday = bcd2bin(chip->txrx_buf[4]);
|
|
|
|
/* mask off century bit */
|
|
|
|
dt->tm_mon = bcd2bin(chip->txrx_buf[5] & 0x7f) - 1;
|
|
|
|
/* adjust for century bit */
|
|
|
|
dt->tm_year = bcd2bin(chip->txrx_buf[6]) + ((chip->txrx_buf[5] & 0x80) ? 100 : 0);
|
|
|
|
|
2018-02-19 15:23:56 +00:00
|
|
|
return 0;
|
2008-11-12 21:27:07 +00:00
|
|
|
}
|
|
|
|
|
2009-01-06 22:42:16 +00:00
|
|
|
static int ds1390_set_time(struct device *dev, struct rtc_time *dt)
|
2008-11-12 21:27:07 +00:00
|
|
|
{
|
|
|
|
struct spi_device *spi = to_spi_device(dev);
|
|
|
|
struct ds1390 *chip = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
/* build the message */
|
|
|
|
chip->txrx_buf[0] = DS1390_REG_SECONDS | 0x80;
|
|
|
|
chip->txrx_buf[1] = bin2bcd(dt->tm_sec);
|
|
|
|
chip->txrx_buf[2] = bin2bcd(dt->tm_min);
|
|
|
|
chip->txrx_buf[3] = bin2bcd(dt->tm_hour);
|
|
|
|
chip->txrx_buf[4] = bin2bcd(dt->tm_wday);
|
|
|
|
chip->txrx_buf[5] = bin2bcd(dt->tm_mday);
|
|
|
|
chip->txrx_buf[6] = bin2bcd(dt->tm_mon + 1) |
|
|
|
|
((dt->tm_year > 99) ? 0x80 : 0x00);
|
|
|
|
chip->txrx_buf[7] = bin2bcd(dt->tm_year % 100);
|
|
|
|
|
|
|
|
/* do the i/o */
|
|
|
|
return spi_write_then_read(spi, chip->txrx_buf, 8, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rtc_class_ops ds1390_rtc_ops = {
|
|
|
|
.read_time = ds1390_read_time,
|
|
|
|
.set_time = ds1390_set_time,
|
|
|
|
};
|
|
|
|
|
2012-12-21 21:09:38 +00:00
|
|
|
static int ds1390_probe(struct spi_device *spi)
|
2008-11-12 21:27:07 +00:00
|
|
|
{
|
|
|
|
unsigned char tmp;
|
|
|
|
struct ds1390 *chip;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
spi->mode = SPI_MODE_3;
|
|
|
|
spi->bits_per_word = 8;
|
|
|
|
spi_setup(spi);
|
|
|
|
|
2013-04-29 23:20:39 +00:00
|
|
|
chip = devm_kzalloc(&spi->dev, sizeof(*chip), GFP_KERNEL);
|
2014-04-03 21:49:40 +00:00
|
|
|
if (!chip)
|
2008-11-12 21:27:07 +00:00
|
|
|
return -ENOMEM;
|
2014-04-03 21:49:40 +00:00
|
|
|
|
2013-04-29 23:20:20 +00:00
|
|
|
spi_set_drvdata(spi, chip);
|
2008-11-12 21:27:07 +00:00
|
|
|
|
|
|
|
res = ds1390_get_reg(&spi->dev, DS1390_REG_SECONDS, &tmp);
|
2009-01-06 22:42:16 +00:00
|
|
|
if (res != 0) {
|
|
|
|
dev_err(&spi->dev, "unable to read device\n");
|
2008-11-12 21:27:07 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-09-18 15:27:57 +00:00
|
|
|
if (spi->dev.of_node)
|
|
|
|
ds1390_trickle_of_init(spi);
|
|
|
|
|
2013-04-29 23:20:39 +00:00
|
|
|
chip->rtc = devm_rtc_device_register(&spi->dev, "ds1390",
|
|
|
|
&ds1390_rtc_ops, THIS_MODULE);
|
2009-01-06 22:42:16 +00:00
|
|
|
if (IS_ERR(chip->rtc)) {
|
|
|
|
dev_err(&spi->dev, "unable to register device\n");
|
|
|
|
res = PTR_ERR(chip->rtc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2008-11-12 21:27:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 11:09:47 +00:00
|
|
|
static const struct of_device_id ds1390_of_match[] = {
|
|
|
|
{ .compatible = "dallas,ds1390" },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, ds1390_of_match);
|
|
|
|
|
2021-09-23 19:49:21 +00:00
|
|
|
static const struct spi_device_id ds1390_spi_ids[] = {
|
|
|
|
{ .name = "ds1390" },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(spi, ds1390_spi_ids);
|
|
|
|
|
2008-11-12 21:27:07 +00:00
|
|
|
static struct spi_driver ds1390_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "rtc-ds1390",
|
2017-10-27 11:09:47 +00:00
|
|
|
.of_match_table = of_match_ptr(ds1390_of_match),
|
2008-11-12 21:27:07 +00:00
|
|
|
},
|
|
|
|
.probe = ds1390_probe,
|
2021-09-23 19:49:21 +00:00
|
|
|
.id_table = ds1390_spi_ids,
|
2008-11-12 21:27:07 +00:00
|
|
|
};
|
|
|
|
|
2012-03-23 22:02:30 +00:00
|
|
|
module_spi_driver(ds1390_driver);
|
2008-11-12 21:27:07 +00:00
|
|
|
|
2009-01-06 22:42:16 +00:00
|
|
|
MODULE_DESCRIPTION("Dallas/Maxim DS1390/93/94 SPI RTC driver");
|
2008-11-12 21:27:07 +00:00
|
|
|
MODULE_AUTHOR("Mark Jackson <mpfj@mimc.co.uk>");
|
|
|
|
MODULE_LICENSE("GPL");
|
2009-09-22 23:46:08 +00:00
|
|
|
MODULE_ALIAS("spi:rtc-ds1390");
|