From patchwork Wed Apr 17 16:33:35 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Olech X-Patchwork-Id: 237293 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail-wi0-x239.google.com (mail-wi0-x239.google.com [IPv6:2a00:1450:400c:c05::239]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority" (not verified)) by ozlabs.org (Postfix) with ESMTPS id B0BA42C0176 for ; Thu, 18 Apr 2013 02:45:10 +1000 (EST) Received: by mail-wi0-f185.google.com with SMTP id c10sf214902wiw.22 for ; Wed, 17 Apr 2013 09:45:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20120806; h=x-received:mime-version:x-beenthere:x-received:received-spf :message-id:x-authentication-warning:from:date:subject:to:cc:cc:cc :cc:cc:cc:cc:x-original-sender:x-original-authentication-results :reply-to:precedence:mailing-list:list-id:x-google-group-id :list-post:list-help:list-archive:sender:list-subscribe :list-unsubscribe:content-type; bh=Psm7w+FoLVjHMq3zGGFfZHWXG3V8nelgH0ol4IF5InY=; b=qpn60Jc8/WoZMblhOwDX9HBMXmzYrcP1ZzDHcp9fRAeyymRjCiuJfgkpF60GG0N58/ Fl866g6oDAfxMd/BWNt3kiZov6hB/M76MmYDTD55cwHUzva4hCtErnTkoGwC9o0vBh78 QOFq9SC6vzWY7eQ8dXT4sjTJG2G+rhoC7U3qxpfbgqH/jM41FMfzIL5I2agHxiYRjBPl OfAt9COdJI3s7mYhXX4nNBt6cnC79nii9vGYwOJPT+k3tOLm7UJiDVtkZiuh5L+lwVbM 4/5ghrlMTC4JOY7m3wy0iDWRwIRUefXvCL2o27qt5Oxertaxh/E/B77Dd58bZDlzNDjE cukw== X-Received: by 10.180.187.51 with SMTP id fp19mr911937wic.5.1366217106929; Wed, 17 Apr 2013 09:45:06 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: rtc-linux@googlegroups.com Received: by 10.180.211.169 with SMTP id nd9ls1459910wic.1.canary; Wed, 17 Apr 2013 09:45:06 -0700 (PDT) X-Received: by 10.204.228.132 with SMTP id je4mr633070bkb.6.1366217106280; Wed, 17 Apr 2013 09:45:06 -0700 (PDT) Received: from mailrelay1.diasemi.com (mailrelay1.diasemi.com. [82.210.246.133]) by gmr-mx.google.com with ESMTP id r6si599381bkz.0.2013.04.17.09.45.05; Wed, 17 Apr 2013 09:45:06 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of anthony.olech.opensource@diasemi.com designates 82.210.246.133 as permitted sender) client-ip=82.210.246.133; Received: from latitude (Not Verified[10.20.27.23]) by mailrelay1.diasemi.com with ESMTP Gateway id ; Wed, 17 Apr 2013 18:45:05 +0200 Received: from latitude (localhost [127.0.0.1]) by latitude (8.14.4/8.14.4/Debian-2ubuntu2) with ESMTP id r3HGj4kv023879; Wed, 17 Apr 2013 17:45:04 +0100 Received: (from tony@localhost) by latitude (8.14.4/8.14.4/Submit) id r3HGi4TM023877; Wed, 17 Apr 2013 17:44:04 +0100 Message-Id: <201304171644.r3HGi4TM023877@latitude> X-Authentication-Warning: latitude: tony set sender to anthony.olech.opensource@diasemi.com using -f From: Anthony Olech Date: Wed, 17 Apr 2013 17:33:35 +0100 Subject: [rtc-linux] [NEW DRIVER V5 4/7] drivers/rtc: DA9058 RTC driver To: Andrew Morton Cc: Mark Brown Cc: Paul Gortmaker Cc: Samuel Ortiz Cc: Alessandro Zummo Cc: rtc-linux@googlegroups.com Cc: LKML Cc: David Dajun Chen X-Original-Sender: anthony.olech.opensource@diasemi.com X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: best guess record for domain of anthony.olech.opensource@diasemi.com designates 82.210.246.133 as permitted sender) smtp.mail=anthony.olech.opensource@diasemi.com Reply-To: rtc-linux@googlegroups.com Precedence: list Mailing-list: list rtc-linux@googlegroups.com; contact rtc-linux+owners@googlegroups.com List-ID: X-Google-Group-Id: 712029733259 List-Post: , List-Help: , List-Archive: Sender: rtc-linux@googlegroups.com List-Subscribe: , List-Unsubscribe: , This patch is relative to linux next-20130417 This is the RTC component driver of the Dialog DA9058 PMIC. This driver is just one component of the whole DA9058 PMIC driver. It depends on the CORE component driver of the DA9058 MFD. Changes relative to V4 of this patch: - rebased to latest tagged linux-next - previously relative to mainline - changed seq_printf to seq_puts where there are no format effectors Signed-off-by: Anthony Olech Signed-off-by: David Dajun Chen --- drivers/rtc/Kconfig | 10 + drivers/rtc/Makefile | 1 + drivers/rtc/rtc-da9058.c | 458 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 469 insertions(+) create mode 100644 drivers/rtc/rtc-da9058.c diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 0c81915..0fc0ca8 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -155,6 +155,16 @@ config RTC_DRV_88PM80X This driver can also be built as a module. If so, the module will be called rtc-88pm80x. +config RTC_DRV_DA9058 + tristate "Dialog DA9058" + depends on MFD_DA9058 + help + If you say yes here you will get support for the + RTC of the Dialog DA9058 PMIC. + + This driver can also be built as a module. If so, the module + will be called rtc-da9058. + config RTC_DRV_DS1307 tristate "Dallas/Maxim DS1307/37/38/39/40, ST M41T00, EPSON RX-8025" help diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index c33f86f..983028f 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -32,6 +32,7 @@ obj-$(CONFIG_RTC_DRV_COH901331) += rtc-coh901331.o obj-$(CONFIG_RTC_DRV_DA9052) += rtc-da9052.o obj-$(CONFIG_RTC_DRV_DA9055) += rtc-da9055.o obj-$(CONFIG_RTC_DRV_DAVINCI) += rtc-davinci.o +obj-$(CONFIG_RTC_DRV_DA9058) += rtc-da9058.o obj-$(CONFIG_RTC_DRV_DM355EVM) += rtc-dm355evm.o obj-$(CONFIG_RTC_DRV_VRTC) += rtc-mrst.o obj-$(CONFIG_RTC_DRV_DS1216) += rtc-ds1216.o diff --git a/drivers/rtc/rtc-da9058.c b/drivers/rtc/rtc-da9058.c new file mode 100644 index 0000000..a1c3879 --- /dev/null +++ b/drivers/rtc/rtc-da9058.c @@ -0,0 +1,458 @@ +/* + * Copyright (C) 2012 Dialog Semiconductor Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +/* + * Limit values + */ +#define DA9058_RTC_SECONDS_LIMIT 59 +#define DA9058_RTC_MINUTES_LIMIT 59 +#define DA9058_RTC_HOURS_LIMIT 23 +#define DA9058_RTC_DAYS_LIMIT 31 +#define DA9058_RTC_MONTHS_LIMIT 12 +#define DA9058_RTC_YEARS_LIMIT 63 + +struct da9058_rtc { + struct da9058 *da9058; + struct platform_device *pdev; + struct rtc_device *rtc_dev; + int alarm_irq; + int tick_irq; + int alarm_enabled; /* used over suspend/resume */ +}; + +static int da9058_rtc_check_param(struct rtc_time *rtc_tm) +{ + if ((rtc_tm->tm_sec > DA9058_RTC_SECONDS_LIMIT) || (rtc_tm->tm_sec < 0)) + return -EIO; + + if ((rtc_tm->tm_min > DA9058_RTC_MINUTES_LIMIT) || (rtc_tm->tm_min < 0)) + return -EIO; + + if ((rtc_tm->tm_hour > DA9058_RTC_HOURS_LIMIT) || (rtc_tm->tm_hour < 0)) + return -EIO; + + if ((rtc_tm->tm_mday > DA9058_RTC_DAYS_LIMIT) || (rtc_tm->tm_mday <= 0)) + return -EIO; + + if ((rtc_tm->tm_mon > DA9058_RTC_MONTHS_LIMIT) || (rtc_tm->tm_mon <= 0)) + return -EIO; + + if ((rtc_tm->tm_year > DA9058_RTC_YEARS_LIMIT) || (rtc_tm->tm_year < 0)) + return -EIO; + + return 0; +} + +static int da9058_rtc_readtime(struct device *dev, struct rtc_time *tm) +{ + struct da9058_rtc *rtc = dev_get_drvdata(dev); + struct da9058 *da9058 = rtc->da9058; + u8 rtc_time[6]; + int ret; + + ret = da9058_bulk_read(da9058, DA9058_COUNTS_REG, rtc_time, 6); + if (ret) + return ret; + + tm->tm_sec = rtc_time[0] & DA9058_RTC_SECS_MASK; + + tm->tm_min = rtc_time[1] & DA9058_RTC_MINS_MASK; + + tm->tm_hour = rtc_time[2] & DA9058_RTC_HRS_MASK; + + tm->tm_mday = (rtc_time[3] & DA9058_RTC_DAY_MASK); + + tm->tm_mon = (rtc_time[4] & DA9058_RTC_MTH_MASK); + + tm->tm_year = (rtc_time[5] & DA9058_RTC_YRS_MASK); + + ret = da9058_rtc_check_param(tm); + + if (ret) + return ret; + + tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, + tm->tm_year); + tm->tm_year += 100; + tm->tm_mon -= 1; + + return 0; +} + +static int da9058_rtc_settime(struct device *dev, struct rtc_time *tm) +{ + struct da9058_rtc *rtc = dev_get_drvdata(dev); + struct da9058 *da9058 = rtc->da9058; + unsigned int rtc_ctrl, val; + u8 rtc_time[6]; + int ret; + + tm->tm_year -= 100; + tm->tm_mon += 1; + + ret = da9058_rtc_check_param(tm); + if (ret < 0) + return ret; + + ret = da9058_reg_read(da9058, DA9058_COUNTS_REG, &rtc_ctrl); + if (ret) + return ret; + rtc_ctrl &= ~DA9058_RTC_SECS_MASK; + + rtc_time[0] = rtc_ctrl | tm->tm_sec; + rtc_time[1] = tm->tm_min; + rtc_time[2] = tm->tm_hour; + rtc_time[3] = tm->tm_mday; + rtc_time[4] = tm->tm_mon; + rtc_time[5] = tm->tm_year; + + ret = da9058_bulk_write(da9058, DA9058_COUNTS_REG, rtc_time, 6); + if (ret) { + dev_dbg(dev, "failed %d to write to RTC\n", ret); + return ret; + } + ret = da9058_reg_read(da9058, DA9058_COUNTY_REG, &val); + if (ret) + return ret; + + val &= DA9058_COUNTY_MONITOR; + if (val) + return 0; + + ret = da9058_set_bits(da9058, DA9058_COUNTY_REG, DA9058_COUNTY_MONITOR); + + return ret; +} + +static int da9058_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm) +{ + struct da9058_rtc *rtc = dev_get_drvdata(dev); + struct da9058 *da9058 = rtc->da9058; + struct rtc_time *tm = &alrm->time; + unsigned int val; + u8 alm_time[6]; + int ret; + + ret = da9058_bulk_read(da9058, DA9058_ALARMS_REG, alm_time, 6); + if (ret) + return ret; + + tm->tm_min = alm_time[0] & DA9058_RTC_ALMSECS_MASK; + + tm->tm_min = alm_time[1] & DA9058_RTC_ALMMINS_MASK; + + tm->tm_hour = alm_time[2] & DA9058_RTC_ALMHRS_MASK; + + tm->tm_mday = alm_time[3] & DA9058_RTC_ALMDAY_MASK; + + tm->tm_mon = alm_time[4] & DA9058_RTC_ALMMTH_MASK; + + tm->tm_year = alm_time[5] & DA9058_RTC_ALMYRS_MASK; + + ret = da9058_rtc_check_param(tm); + if (ret < 0) + return ret; + + ret = da9058_reg_read(da9058, DA9058_ALARMY_REG, &val); + if (ret) + return ret; + + alrm->enabled = val & DA9058_ALARMY_ALARMON; + + tm->tm_year += 100; + tm->tm_mon -= 1; + + return 0; +} + +static int da9058_rtc_stop_alarm(struct da9058_rtc *rtc) +{ + return da9058_clear_bits(rtc->da9058, DA9058_ALARMY_REG, + DA9058_ALARMY_ALARMON); +} + +static int da9058_rtc_start_alarm(struct da9058_rtc *rtc) +{ + return da9058_set_bits(rtc->da9058, DA9058_ALARMY_REG, + DA9058_ALARMY_ALARMON); +} + +static int da9058_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm) +{ + struct da9058_rtc *rtc = dev_get_drvdata(dev); + struct da9058 *da9058 = rtc->da9058; + struct rtc_time *tm = &alrm->time; + unsigned int rtc_ctrl; + u8 alm_time[6]; + int ret; + + tm->tm_year -= 100; + tm->tm_mon += 1; + + ret = da9058_rtc_check_param(tm); + if (ret < 0) + return ret; + + memset(alm_time, 0, sizeof(alm_time)); + + if (tm->tm_sec != -1) + alm_time[0] |= tm->tm_sec; + else + alm_time[0] |= DA9058_RTC_ALMSECS_MASK; + + ret = da9058_reg_read(da9058, DA9058_ALARMMI_REG, &rtc_ctrl); + if (ret) + return ret; + rtc_ctrl &= ~DA9058_RTC_ALMMINS_MASK; + + if (tm->tm_min != -1) + alm_time[1] = rtc_ctrl | tm->tm_min; + else + alm_time[1] = rtc_ctrl | DA9058_RTC_ALMMINS_MASK; + + if (tm->tm_hour != -1) + alm_time[2] |= tm->tm_hour; + else + alm_time[2] |= DA9058_RTC_ALMHRS_MASK; + + if (tm->tm_mday != -1) + alm_time[3] |= tm->tm_mday; + else + alm_time[3] |= DA9058_RTC_ALMDAY_MASK; + + if (tm->tm_mon != -1) + alm_time[4] |= tm->tm_mon; + else + alm_time[4] |= DA9058_RTC_ALMMTH_MASK; + + ret = da9058_reg_read(da9058, DA9058_ALARMY_REG, &rtc_ctrl); + if (ret) + return ret; + + rtc_ctrl &= ~DA9058_RTC_ALMYRS_MASK; + + if (tm->tm_year != -1) + alm_time[5] = rtc_ctrl | tm->tm_year; + else + alm_time[5] = rtc_ctrl | DA9058_RTC_ALMYRS_MASK; + + ret = da9058_rtc_stop_alarm(rtc); + if (ret < 0) + return ret; + + ret = da9058_bulk_write(da9058, DA9058_ALARMS_REG, alm_time, 6); + if (ret) + return ret; + + if (alrm->enabled) + ret = da9058_rtc_start_alarm(rtc); + + return ret; +} + +static int da9058_rtc_alarm_irq_enable(struct device *dev, + unsigned int enabled) +{ + struct da9058_rtc *rtc = dev_get_drvdata(dev); + + if (enabled) + return da9058_rtc_start_alarm(rtc); + else + return da9058_rtc_stop_alarm(rtc); +} + +static irqreturn_t da9058_rtc_timer_alarm_handler(int irq, void *data) +{ + struct da9058_rtc *rtc = data; + + da9058_rtc_stop_alarm(rtc); + rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF); + + return IRQ_HANDLED; +} + +static irqreturn_t da9058_rtc_tick_alarm_handler(int irq, void *data) +{ + struct da9058_rtc *rtc = data; + + rtc_update_irq(rtc->rtc_dev, 1, RTC_PF | RTC_IRQF); + + return IRQ_HANDLED; +} +static int da9058_rtc_proc(struct device *dev, struct seq_file *seq) +{ + struct da9058_rtc *rtc = dev_get_drvdata(dev); + struct da9058 *da9058 = rtc->da9058; + unsigned int rtc_ctrl; + int ret; + + ret = da9058_reg_read(da9058, DA9058_ALARMY_REG, &rtc_ctrl); + + seq_puts(seq, rtc_ctrl & DA9058_ALARMY_ALARMON ? + "ALRM is running\n" : "ALRM is not running\n"); + + return 0; +} + +static const struct rtc_class_ops da9058_rtc_ops = { + .read_time = da9058_rtc_readtime, + .set_time = da9058_rtc_settime, + .read_alarm = da9058_rtc_readalarm, + .set_alarm = da9058_rtc_setalarm, + .proc = da9058_rtc_proc, + .alarm_irq_enable = da9058_rtc_alarm_irq_enable, +}; + +static int da9058_rtc_probe(struct platform_device *pdev) +{ + struct da9058 *da9058 = dev_get_drvdata(pdev->dev.parent); + const struct mfd_cell *cell = mfd_get_cell(pdev); + struct da9058_rtc_pdata *rtc_pdata; + struct da9058_rtc *rtc; + int ret; + + if (cell == NULL) { + ret = -ENODEV; + goto exit; + } + + rtc_pdata = cell->platform_data; + + if (rtc_pdata == NULL) { + ret = -EINVAL; + goto exit; + } + + rtc = devm_kzalloc(&pdev->dev, sizeof(struct da9058_rtc), GFP_KERNEL); + if (!rtc) { + ret = -ENOMEM; + goto exit; + } + + platform_set_drvdata(pdev, rtc); + + rtc->da9058 = da9058; + rtc->pdev = pdev; + ret = da9058_clear_bits(da9058, DA9058_WAITCONT_REG, + DA9058_WAITCONT_RTCCLOCK); + if (ret) { + dev_err(&pdev->dev, "Failed to set RTC running: %d\n", ret); + goto unable_to_init_device; + } + + ret = da9058_set_bits(da9058, DA9058_COUNTY_REG, DA9058_COUNTY_MONITOR); + if (ret) { + dev_err(&pdev->dev, "Failed No gating RTC: %d\n", ret); + goto unable_to_init_device; + } + + device_init_wakeup(&pdev->dev, 1); + + rtc->rtc_dev = rtc_device_register("da9058", &pdev->dev, + &da9058_rtc_ops, THIS_MODULE); + if (IS_ERR(rtc->rtc_dev)) { + ret = PTR_ERR(rtc->rtc_dev); + dev_err(&pdev->dev, "failed to register RTC: %d\n", ret); + goto unable_to_register_device; + } + + rtc->alarm_irq = platform_get_irq(pdev, 0); + if (rtc->alarm_irq < 0) { + dev_err(&pdev->dev, "cannot get RTC ALARM IRQ error=%d\n", + rtc->alarm_irq); + ret = -ENODEV; + goto failed_to_get_alarm_irq; + } + + ret = request_threaded_irq(da9058_to_virt_irq_num(da9058, + rtc->alarm_irq), + NULL, da9058_rtc_timer_alarm_handler, + IRQF_TRIGGER_RISING | IRQF_ONESHOT, + "DA9058 RTC Timer Alarm", rtc); + + if (ret) { + dev_err(&pdev->dev, + "Failed to get rtc timer alarm IRQ %d: %d\n", + rtc->alarm_irq, ret); + goto unable_to_setup_timer_irq; + } + + rtc->tick_irq = platform_get_irq(pdev, 1); + if (rtc->tick_irq < 0) { + dev_err(&pdev->dev, "cannot get RTC TICK IRQ error=%d\n", + rtc->tick_irq); + ret = -ENODEV; + goto failed_to_get_tick_irq; + } + ret = request_threaded_irq(da9058_to_virt_irq_num(da9058, + rtc->tick_irq), + NULL, da9058_rtc_tick_alarm_handler, + IRQF_TRIGGER_RISING | IRQF_ONESHOT, + "DA9058 RTC Tick Alarm", rtc); + if (ret) { + dev_err(&pdev->dev, + "Failed to get rtc timer alarm IRQ %d: %d\n", + DA9058_IRQ_ETICK, ret); + goto unable_to_setup_alarm_irq; + } + goto exit; + +failed_to_get_tick_irq: +unable_to_setup_alarm_irq: + free_irq(da9058_to_virt_irq_num(da9058, rtc->alarm_irq), rtc); +failed_to_get_alarm_irq: +unable_to_setup_timer_irq: + rtc_device_unregister(rtc->rtc_dev); +unable_to_register_device: +unable_to_init_device: + platform_set_drvdata(pdev, NULL); +exit: + return ret; +} + +static int da9058_rtc_remove(struct platform_device *pdev) +{ + struct da9058_rtc *rtc = platform_get_drvdata(pdev); + struct da9058 *da9058 = rtc->da9058; + + free_irq(da9058_to_virt_irq_num(da9058, rtc->alarm_irq), rtc); + free_irq(da9058_to_virt_irq_num(da9058, rtc->tick_irq), rtc); + + rtc_device_unregister(rtc->rtc_dev); + + return 0; +} + +static struct platform_driver da9058_rtc_driver = { + .probe = da9058_rtc_probe, + .remove = da9058_rtc_remove, + .driver = { + .name = "da9058-rtc", + .owner = THIS_MODULE, + }, +}; + +module_platform_driver(da9058_rtc_driver); + +MODULE_DESCRIPTION("Dialog DA9058 PMIC Real Time Clock Driver"); +MODULE_AUTHOR("Anthony Olech "); +MODULE_LICENSE("GPL v2"); +MODULE_ALIAS("platform:da9058-rtc");