Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7493446imu; Tue, 22 Jan 2019 06:57:51 -0800 (PST) X-Google-Smtp-Source: ALg8bN7nqLkS/rp9tJ3WfXjver/ilLxYgg0NDKchku7w9epYOAjGqvEg+DYaQsnbOVZMxSq1Mkem X-Received: by 2002:a17:902:8a8a:: with SMTP id p10mr34994551plo.50.1548169071453; Tue, 22 Jan 2019 06:57:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548169071; cv=none; d=google.com; s=arc-20160816; b=GXOfLGro8+ETzktDK8zSKYXia0ko1WLZ++biR/Qq4VPUGFNlRUWR5vrWpDIzg84uxA a8XMk8Wyhik6V5CY67csPi7Ci4xtdimy5LpWMLUtoNkl10z3eWApUS0hbdqLQxckzDod ItLJY9QjcBcBq1DWkGP4llYlYanEM+g5ha6S0gr5z4D6C6xIQGjm/gMzhzHOtBbw2g9V L+L6NZJm5VgmNc3Ke5oIafbaoJ7FjAGH1YPmuhWe9V4Gtz+OSqI+iM3wIWaHXHb/3ExH wkOoBcgCGFVxh200n8wMGQQMlGZ39JzBHZIOwTSIMrw5PteENPUH135irOvi2AyqBztd A7qQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=K+2zfFSibbg190l5BiqRyileh2/rBR4aXcPonbfilMU=; b=h8GO84tySllVkdQVoqq+VhsojKZvHo0UqzTko3UM/uRX9nQO7Tw+NROjxc9So8GCrO Hh+HBczEvDabO9s0gpEAbdvYnDHLgScxKyg5jAF7n2gH6GB1IqYdDMKyuH9AbOmeR6Js CuY5xHOO0tu5IMF7g9qq6MeboyaWOQ0yCZtLeyhWuG3VGpUHa6SmyIKTt5qqtWd4pvKG z3/7Em8+f1VTmsXOguMFU9pvD74yQLeCMmDf0HP789QUQMro/4bhLevrgiKvg+q8LflJ 4mqpXP2S/0VETgcv/RyRa5F6CPjD025u1bqjAvzs+pxpibI7CMNjYoLg8l5vA4G/YIFe 58/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=C82kitik; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c2si16132840plb.152.2019.01.22.06.57.35; Tue, 22 Jan 2019 06:57:51 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=C82kitik; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729041AbfAVO4J (ORCPT + 99 others); Tue, 22 Jan 2019 09:56:09 -0500 Received: from mail-qt1-f195.google.com ([209.85.160.195]:43630 "EHLO mail-qt1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728836AbfAVO4J (ORCPT ); Tue, 22 Jan 2019 09:56:09 -0500 Received: by mail-qt1-f195.google.com with SMTP id i7so27806347qtj.10; Tue, 22 Jan 2019 06:56:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=K+2zfFSibbg190l5BiqRyileh2/rBR4aXcPonbfilMU=; b=C82kitik7fUqGHHOUPGruIWahZy3kw97dMhkOBRMkVnBYaSn1avhaJrMHP8lNxLGtW pget1rtHcEcAZGiVWjJcZ53TJaGHPExeJx3oeyPVwVoAyT6OSRxDzntrqyI10TafTVwl xBMD9vWx1MqknlTpl+hFt6qd4AtVSwvJ/EDQek++ri7UN+nYGL8kqS/9gpTPnKnGOIUP nuf/hA+drfXADQlFpYWGpWq2Nr/kC9nHXS8Q+JkzPy/7SOmo8R65QhJHKO797J+JNDZ1 eXuNt9hcJ6SRgQ6nw3uZ/xRSHCuLrkt9qZjQdv27wHsz0BPXiXG9ZLpDlZCUU/J6ZDs5 60FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=K+2zfFSibbg190l5BiqRyileh2/rBR4aXcPonbfilMU=; b=PeGfGbGesIFCu3XQnS0Pzl9NKR76XLLyyjzGZ4v4Z8rweZ9msZk/i42Qi0UWTiqc+p /vro0XuEchYcTnZoIUve3z0C/PRfLYYqn4AJbCU+w+zBggWHqo8dGr2l1DrFR1TjFAXa k0HjbE2FKLd0dN1vK/LlGis2HFmAw5q2+MXSnFdV3MK86ith975NBGRNWnQIXkJMPK7I /1dPl9McqOpa35tyy5wAZdKLMvTVelps0ecC04TwgYZ1iS9H3DOpaRh1XdkeRK8p4+Tf oWFHLZv8RhDCMN2F3zWdnDyVma63TAVhHWteLsmdWtVU6YGgN9DQbEW9dSFFeEJvkrTu B7Ew== X-Gm-Message-State: AJcUuke8pYDT5z04dBCbdoy/Ptm1cFROsqLdnA5u8AA3O0EWCdfMxnUp krUC959epV3O8LvH7nR8O6o0CKYijlz3nCXqfaE= X-Received: by 2002:ac8:2585:: with SMTP id e5mr31667294qte.233.1548168967318; Tue, 22 Jan 2019 06:56:07 -0800 (PST) MIME-Version: 1.0 References: <20190119001422.48186-1-ncrews@chromium.org> <20190119001422.48186-6-ncrews@chromium.org> In-Reply-To: <20190119001422.48186-6-ncrews@chromium.org> From: Enric Balletbo Serra Date: Tue, 22 Jan 2019 15:55:55 +0100 Message-ID: Subject: Re: [PATCH v3 5/9] platform/chrome: rtc: Add RTC driver To: Nick Crews Cc: linux-kernel , Guenter Roeck , Simon Glass , Daniel Kurtz , dlaurie@chromium.org, Duncan Laurie , linux-rtc@vger.kernel.org, Enric Balletbo i Serra , Alessandro Zummo , Benson Leung , Nick Crews , Alexandre Belloni Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Nick, Missatge de Nick Crews del dia ds., 19 de gen. 2019 a les 1:17: > > From: Duncan Laurie > > This Embedded Controller has an internal RTC that is exposed > as a standard RTC class driver with read/write functionality. > > The driver is added to the drivers/rtc/ so that the maintainer of that > directory will be able to comment on this change, as that maintainer is > the expert on this system. In addition, the driver code is called > indirectly after a corresponding device is registered from core.c, > as opposed to core.c registering the driver callbacks directly. > > > hwclock --show --rtc /dev/rtc1 > 2007-12-31 16:01:20.460959-08:00 > > hwclock --systohc --rtc /dev/rtc1 > > hwclock --show --rtc /dev/rtc1 > 2018-11-29 17:08:00.780793-08:00 > > Signed-off-by: Duncan Laurie > Signed-off-by: Nick Crews > --- > > Changes in v3: > - rm #define for driver name > - Don't compute weekday when reading from RTC. > Still set weekday when writing, as RTC needs this > to control advanced power scheduling > - rm check for invalid month data > - Set range_min and range_max on rtc_device > > Changes in v2: > - rm license boiler plate > - rm "wilco_ec_rtc -" prefix in docstring > - Make rtc driver its own module within the drivers/rtc/ directory > - Register a rtc device from core.c that is picked up by this driver > > drivers/platform/chrome/wilco_ec/core.c | 17 +++ Acked-by: Enric Balletbo i Serra > drivers/rtc/Kconfig | 11 ++ > drivers/rtc/Makefile | 1 + > drivers/rtc/rtc-wilco-ec.c | 177 ++++++++++++++++++++++++ > 4 files changed, 206 insertions(+) > create mode 100644 drivers/rtc/rtc-wilco-ec.c > > diff --git a/drivers/platform/chrome/wilco_ec/core.c b/drivers/platform/chrome/wilco_ec/core.c > index 9b4a25e63b56..dd8b896b3833 100644 > --- a/drivers/platform/chrome/wilco_ec/core.c > +++ b/drivers/platform/chrome/wilco_ec/core.c > @@ -34,6 +34,7 @@ static int wilco_ec_probe(struct platform_device *pdev) > { > struct device *dev = &pdev->dev; > struct wilco_ec_device *ec; > + struct platform_device *child_pdev; > int ret; > > ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL); > @@ -76,8 +77,24 @@ static int wilco_ec_probe(struct platform_device *pdev) > goto rm_debugfs; > } > > + /* > + * Register a RTC platform device that will get picked up by the RTC > + * subsystem. This platform device will get freed when its parent device > + * dev is unregistered. > + */ > + child_pdev = platform_device_register_data(dev, "rtc-wilco-ec", > + PLATFORM_DEVID_AUTO, > + NULL, 0); > + if (IS_ERR(child_pdev)) { > + dev_err(dev, "Failed to create RTC platform device\n"); > + ret = PTR_ERR(child_pdev); > + goto remove_sysfs; > + } > + > return 0; > > +remove_sysfs: > + wilco_ec_sysfs_remove(ec); > rm_debugfs: > wilco_ec_debugfs_remove(ec); > destroy_mec: > diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig > index 225b0b8516f3..d5063c791515 100644 > --- a/drivers/rtc/Kconfig > +++ b/drivers/rtc/Kconfig > @@ -1814,4 +1814,15 @@ config RTC_DRV_GOLDFISH > Goldfish is a code name for the virtual platform developed by Google > for Android emulation. > > +config RTC_DRV_WILCO_EC > + tristate "Wilco EC RTC" > + depends on WILCO_EC > + default m > + help > + If you say yes here, you get read/write support for the Real Time > + Clock on the Wilco Embedded Controller (Wilco is a kind of Chromebook) > + > + This can also be built as a module. If so, the module will > + be named "rtc_wilco_ec". > + > endif # RTC_CLASS > diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile > index df022d820bee..6255ea78da25 100644 > --- a/drivers/rtc/Makefile > +++ b/drivers/rtc/Makefile > @@ -172,6 +172,7 @@ obj-$(CONFIG_RTC_DRV_V3020) += rtc-v3020.o > obj-$(CONFIG_RTC_DRV_VR41XX) += rtc-vr41xx.o > obj-$(CONFIG_RTC_DRV_VRTC) += rtc-mrst.o > obj-$(CONFIG_RTC_DRV_VT8500) += rtc-vt8500.o > +obj-$(CONFIG_RTC_DRV_WILCO_EC) += rtc-wilco-ec.o > obj-$(CONFIG_RTC_DRV_WM831X) += rtc-wm831x.o > obj-$(CONFIG_RTC_DRV_WM8350) += rtc-wm8350.o > obj-$(CONFIG_RTC_DRV_X1205) += rtc-x1205.o > diff --git a/drivers/rtc/rtc-wilco-ec.c b/drivers/rtc/rtc-wilco-ec.c > new file mode 100644 > index 000000000000..b721cc3f17f0 > --- /dev/null > +++ b/drivers/rtc/rtc-wilco-ec.c > @@ -0,0 +1,177 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * RTC interface for Wilco Embedded Controller with R/W abilities > + * > + * Copyright 2018 Google LLC > + * > + * The corresponding platform device is typically registered in > + * drivers/platform/chrome/wilco_ec/core.c > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#define EC_COMMAND_CMOS 0x7c > +#define EC_CMOS_TOD_WRITE 0x02 > +#define EC_CMOS_TOD_READ 0x08 > + > +/** > + * struct ec_rtc_read - Format of RTC returned by EC. > + * @second: Second value (0..59) > + * @minute: Minute value (0..59) > + * @hour: Hour value (0..23) > + * @day: Day value (1..31) > + * @month: Month value (1..12) > + * @year: Year value (full year % 100) > + * @century: Century value (full year / 100) > + * > + * All values are presented in binary (not BCD). > + */ > +struct ec_rtc_read { > + u8 second; > + u8 minute; > + u8 hour; > + u8 day; > + u8 month; > + u8 year; > + u8 century; > +} __packed; > + > +/** > + * struct ec_rtc_write - Format of RTC sent to the EC. > + * @param: EC_CMOS_TOD_WRITE > + * @century: Century value (full year / 100) > + * @year: Year value (full year % 100) > + * @month: Month value (1..12) > + * @day: Day value (1..31) > + * @hour: Hour value (0..23) > + * @minute: Minute value (0..59) > + * @second: Second value (0..59) > + * @weekday: Day of the week (0=Saturday) > + * > + * All values are presented in BCD. > + */ > +struct ec_rtc_write { > + u8 param; > + u8 century; > + u8 year; > + u8 month; > + u8 day; > + u8 hour; > + u8 minute; > + u8 second; > + u8 weekday; > +} __packed; > + > +int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm) > +{ > + struct wilco_ec_device *ec = dev_get_drvdata(dev->parent); > + u8 param = EC_CMOS_TOD_READ; > + struct ec_rtc_read rtc; > + struct wilco_ec_message msg = { > + .type = WILCO_EC_MSG_LEGACY, > + .flags = WILCO_EC_FLAG_RAW_RESPONSE, > + .command = EC_COMMAND_CMOS, > + .request_data = ¶m, > + .request_size = sizeof(param), > + .response_data = &rtc, > + .response_size = sizeof(rtc), > + }; > + int ret; > + > + ret = wilco_ec_mailbox(ec, &msg); > + if (ret < 0) > + return ret; > + > + tm->tm_sec = rtc.second; > + tm->tm_min = rtc.minute; > + tm->tm_hour = rtc.hour; > + tm->tm_mday = rtc.day; > + tm->tm_mon = rtc.month - 1; > + tm->tm_year = rtc.year + (rtc.century * 100) - 1900; > + tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year); > + > + /* Don't compute day of week, we don't need it. */ > + tm->tm_wday = -1; > + > + return 0; > +} > + > +int wilco_ec_rtc_write(struct device *dev, struct rtc_time *tm) > +{ > + struct wilco_ec_device *ec = dev_get_drvdata(dev->parent); > + struct ec_rtc_write rtc; > + struct wilco_ec_message msg = { > + .type = WILCO_EC_MSG_LEGACY, > + .flags = WILCO_EC_FLAG_RAW_RESPONSE, > + .command = EC_COMMAND_CMOS, > + .request_data = &rtc, > + .request_size = sizeof(rtc), > + }; > + int year = tm->tm_year + 1900; > + /* > + * Convert from 0=Sunday to 0=Saturday for the EC > + * We DO need to set weekday because the EC controls battery charging > + * schedules that depend on the day of the week. > + */ > + int wday = tm->tm_wday == 6 ? 0 : tm->tm_wday + 1; > + int ret; > + > + rtc.param = EC_CMOS_TOD_WRITE; > + rtc.century = bin2bcd(year / 100); > + rtc.year = bin2bcd(year % 100); > + rtc.month = bin2bcd(tm->tm_mon + 1); > + rtc.day = bin2bcd(tm->tm_mday); > + rtc.hour = bin2bcd(tm->tm_hour); > + rtc.minute = bin2bcd(tm->tm_min); > + rtc.second = bin2bcd(tm->tm_sec); > + rtc.weekday = bin2bcd(wday); > + > + ret = wilco_ec_mailbox(ec, &msg); > + if (ret < 0) > + return ret; > + > + return 0; > +} > + > +static const struct rtc_class_ops wilco_ec_rtc_ops = { > + .read_time = wilco_ec_rtc_read, > + .set_time = wilco_ec_rtc_write, > +}; > + > +static int wilco_ec_rtc_probe(struct platform_device *pdev) > +{ > + struct rtc_device *rtc; > + > + rtc = devm_rtc_allocate_device(&pdev->dev); > + if (IS_ERR(rtc)) > + return PTR_ERR(rtc); > + > + rtc->ops = &wilco_ec_rtc_ops; > + /* EC only supports this century */ > + rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; > + rtc->range_max = RTC_TIMESTAMP_END_2099; > + rtc->owner = THIS_MODULE; > + > + return rtc_register_device(rtc); > +} > + > +static struct platform_driver wilco_ec_rtc_driver = { > + .driver = { > + .name = "rtc-wilco-ec", > + }, > + .probe = wilco_ec_rtc_probe, > +}; > + > +module_platform_driver(wilco_ec_rtc_driver); > + > +MODULE_ALIAS("platform:rtc-wilco-ec"); > +MODULE_AUTHOR("Nick Crews "); > +MODULE_LICENSE("GPL v2"); > +MODULE_DESCRIPTION("Wilco EC RTC driver"); > -- > 2.20.1.321.g9e740568ce-goog >