Dallas/Maxim DS1374 is a counter designed to continuously count
time in seconds. It provides an I2C interface to the host to
access RTC clock or Alarm/Watchdog timer.

Add MFD Core driver, supporting the I2C communication to RTC and
Watchdog devices.

Signed-off-by: Johnson Chen <johnsonch.c...@moxa.com>
---
 drivers/mfd/Kconfig  |  11 +++++
 drivers/mfd/Makefile |   2 +
 drivers/mfd/ds1374.c | 101 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+)
 create mode 100644 drivers/mfd/ds1374.c

diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
index 687e9c848053..d16031f4b487 100644
--- a/drivers/mfd/Kconfig
+++ b/drivers/mfd/Kconfig
@@ -1980,6 +1980,17 @@ config MFD_STM32_LPTIMER
          To compile this driver as a module, choose M here: the
          module will be called stm32-lptimer.
 
+config MFD_DS1374
+       tristate "Support for Dallas/Maxim DS1374"
+       depends on I2C
+       select MFD_CORE
+       help
+         Say yes here to add support for Dallas/Maxim DS1374 Semiconductor.
+         This driver provides RTC and watchdog.
+
+         This driver can also be built as a module. If so, module will be
+         called ds1374.
+
 config MFD_STM32_TIMERS
        tristate "Support for STM32 Timers"
        depends on (ARCH_STM32 && OF) || COMPILE_TEST
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
index bea2be419822..a6463dd4aa33 100644
--- a/drivers/mfd/Makefile
+++ b/drivers/mfd/Makefile
@@ -243,6 +243,8 @@ obj-$(CONFIG_INTEL_SOC_PMIC_CHTWC)  += 
intel_soc_pmic_chtwc.o
 obj-$(CONFIG_INTEL_SOC_PMIC_CHTDC_TI)  += intel_soc_pmic_chtdc_ti.o
 mt6397-objs    := mt6397-core.o mt6397-irq.o
 obj-$(CONFIG_MFD_MT6397)       += mt6397.o
+obj-$(CONFIG_MFD_DS1374)       += ds1374.o
+
 obj-$(CONFIG_INTEL_SOC_PMIC_MRFLD)     += intel_soc_pmic_mrfld.o
 
 obj-$(CONFIG_MFD_ALTERA_A10SR) += altera-a10sr.o
diff --git a/drivers/mfd/ds1374.c b/drivers/mfd/ds1374.c
new file mode 100644
index 000000000000..6e9041aba5d2
--- /dev/null
+++ b/drivers/mfd/ds1374.c
@@ -0,0 +1,101 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ *  Mamim/Dallas DS1374 MFD Core Driver.
+ *
+ *  Copyright (C) 2020 Johnson Chen <johnsonch.c...@moxa.com>
+ */
+
+#include <linux/kernel.h>
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/mfd/core.h>
+
+
+static struct mfd_cell ds1374_cell[] = {
+       { .name = "ds1374_rtc", },
+       { .name = "ds1374_wdt", },
+};
+
+static int ds1374_probe(struct i2c_client *client,
+                       const struct i2c_device_id *id)
+{
+       int ret;
+
+       ret = i2c_check_functionality(client->adapter,
+                                     I2C_FUNC_SMBUS_BYTE_DATA |
+                                     I2C_FUNC_SMBUS_WORD_DATA |
+                                     I2C_FUNC_SMBUS_BYTE);
+       if (!ret)
+               return -ENODEV;
+
+       ret = devm_mfd_add_devices(&client->dev, 0, ds1374_cell,
+                                  ARRAY_SIZE(ds1374_cell), NULL, 0, NULL);
+
+       if (ret < 0) {
+               dev_err(&client->dev, "failed to add DS1374 sub-devices\n");
+               return ret;
+       }
+
+       return 0;
+}
+
+static int ds1374_remove(struct i2c_client *client)
+{
+       mfd_remove_devices(&client->dev);
+
+       return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int ds1374_suspend(struct device *dev)
+{
+       struct i2c_client *client = to_i2c_client(dev);
+
+       if (client->irq > 0 && device_may_wakeup(&client->dev))
+               enable_irq_wake(client->irq);
+       return 0;
+}
+
+static int ds1374_resume(struct device *dev)
+{
+       struct i2c_client *client = to_i2c_client(dev);
+
+       if (client->irq > 0 && device_may_wakeup(&client->dev))
+               disable_irq_wake(client->irq);
+       return 0;
+}
+#endif
+
+static const struct i2c_device_id ds1374_id[] = {
+       { "ds1374", 0 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, ds1374_id);
+
+#ifdef CONFIG_OF
+static const struct of_device_id ds1374_of_match[] = {
+       { .compatible = "dallas,ds1374" },
+       { }
+};
+MODULE_DEVICE_TABLE(of, ds1374_of_match);
+#endif
+
+static SIMPLE_DEV_PM_OPS(ds1374_pm, ds1374_suspend, ds1374_resume);
+
+static struct i2c_driver ds1374_driver = {
+       .driver                 = {
+               .name           = "ds1374",
+               .of_match_table = of_match_ptr(ds1374_of_match),
+               .pm             = &ds1374_pm,
+       },
+       .probe          = ds1374_probe,
+       .remove         = ds1374_remove,
+       .id_table       = ds1374_id,
+};
+
+module_i2c_driver(ds1374_driver);
+
+MODULE_DESCRIPTION("Dallas/Maxim DS1374 MFD Core Driver");
+MODULE_AUTHOR("Johnson Chen <johnsonch.c...@moxa.com>");
+MODULE_LICENSE("GPL");
-- 
2.20.1

Reply via email to