Skip to contentSkip to conversation

platform/chrome: Add basic support for Wilco EC

PATCH Superseded Nick Crews 4 participants 13 messages Feb 8, 2019

Subsystems (3)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Nick Crews

author
#0 lore raw

v6 cover platform/chrome: Add basic support for Wilco EC

There is a new chromebook that contains a different Embedded Controller (codename Wilco) than the rest of the chromebook series. Thus the kernel requires a different driver than the already existing and generalized cros_ec_* drivers. The core of the communication with the EC is implemented in wilco_ec/mailbox.c, using a simple byte-level protocol with a checksum, transmitted over an eSPI bus.

In this patch series I only introduce a very barebones
driver with a debugfs interface for sending/receiving raw byte sequences to the EC, as well as a standard RTC driver with read/write functionality. Later, I will hopefully include more specialized drivers such as a keyboard backlight driver, an EC event notification node, telemetry data query node, etc.

The entry point of the driver is wilco_ec/core.c,
which is responsible for several tasks:
- Initialize the register interface that is used by wilco_ec_mailbox()
- Create a platform device which is picked up by the debugfs driver
- Create a platform device which is picked up by the RTC driver

A full thread of the development of these patches can be found at
https://chromium-review.googlesource.com/c/1356082. This thread contains comments and revisions that could be helpful in understanding how the driver arrived at the state it is in now. The thread also contains some ChromeOS specific patches that actually enable the driver. If you want to test the patch yourself, you would have to install the ChromeOS SDK and cherry pick in these patches, and even then you would need a Sarien device to actually run it.

Thank you for your comments!

Changes in v6:
- Re-added WILCO_EC_FLAG_EXTENDED_DATA and went back to always reading either EC_MAILBOX_DATA_SIZE or EC_MAILBOX_DATA_SIZE_EXTENDED bytes, since it turns out the EC always expects you to calculate the checksum over all of them.
- Split WILCO_EC_MSG_TELEMETRY into WILCO_EC_MSG_TELEMETRY_SHORT and WILCO_EC_MSG_TELEMETRY_LONG, since there will be two different commands.
- A couple comment and err message polishes
- s/4.19/5.1/ for kernel version in documentation, since that is the version this patch should land in.
- Instead of requiring at least 3 bytes for msg type and command, now just require two for msg type. We can skip the command.
- Fixed error checking in probe() so that errors are hidden, without causing more errors or unextpected behavior.
- Some comment polishing.
- In the core, actually unregister the debugfs child platform_device
- In the core, actually unregister the RTC child platform_device.

Changes in v5:
- move checking of NO_RESPONSE flag before timeout check, so now timeout doesn't always happen when EC isn't supposed to respond.
- rm WILCO_EC_FLAG_EXTENDED_DATA, that is already obvious from wilco_ec_message.response_size
- core now always continues regardless of debugfs failure
- mv documentation to file header
- Check for OOM
- rm unneeded check if debug_info is allocqated
- rm bogus comment
- add space around "+"
- rm WILCO_EC_FLAG_EXTENDED_DATA, that is already obvious from wilco_ec_message.response_size

Changes in v4:
- add #define DRV_NAME
- remove redundant Kconfig nesting
- changed my email to @chromium.org
- Add better explanation of what core.c does
- Change debugfs driver to be a separate module
- Change me email to @chromium.org from @google.com
- Change CONFIG_WILCO_EC_SYSFS_RAW to CONFIG_WILCO_EC_DEBUGFS
- Change me email to @chromium.org from @google.com
- Move "Add RTC driver" before "Add sysfs attributes" so that it could get accepted earlier, since it is less contentious

Changes in v3:
- Change <= to >= in mec_in_range()
- Add " - EC_HOST_CMD_REGION0" to offset arg for io_bytes_mec()
- remove unused ret in probe()
- Add newline spacer in probe()
- rm unnecessary res in get_resource()
- s/8bit/8-bit
- rm first sleep when sending command to EC
- Move the attribute to the debugfs system
- Move the implementation to debugfs.c
- Improve the raw hex parsing
- Encapsulate global variables in one object
- Add safety check when passing less than 3 bytes
- Move documentation to debugfs-wilco-ec
- 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:
- Fixed kernel-doc comments
- Fixed include of linux/mfd/cros_ec_lpc_mec.h
- cros_ec_lpc_mec_in_range() returns -EINVAL on error
- Added parens around macro variables
- Remove COMPILE_TEST from Kconfig because inb()/outb() won't work on anything but X86
- Add myself as module author
- Tweak mailbox()
- Add sysfs documentation
- rm duplicate EC_MAILBOX_DATA_SIZE defs
- Make docstrings follow kernel style
- Fix tags in commit msg
- Move Kconfig to subdirectory
- Reading raw now includes ASCII translation
- 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

Nick Crews (4):
cros_ec: Remove cros_ec dependency in lpc_mec platform/chrome: Add new driver for Wilco EC platform/chrome: Add support for raw commands in debugfs platform/chrome: rtc: Add RTC driver

 Documentation/ABI/testing/debugfs-wilco-ec |  23 ++
 drivers/platform/chrome/Kconfig            |   4 +-
 drivers/platform/chrome/Makefile           |   2 +
 drivers/platform/chrome/cros_ec_lpc_mec.c  |  52 ++++-
 drivers/platform/chrome/cros_ec_lpc_mec.h  |  43 ++--
 drivers/platform/chrome/cros_ec_lpc_reg.c  |  47 ++--
 drivers/platform/chrome/wilco_ec/Kconfig   |  21 ++
 drivers/platform/chrome/wilco_ec/Makefile  |   6 +
 drivers/platform/chrome/wilco_ec/core.c    | 136 ++++++++++++
 drivers/platform/chrome/wilco_ec/debugfs.c | 238 +++++++++++++++++++++
 drivers/platform/chrome/wilco_ec/mailbox.c | 236 ++++++++++++++++++++
 drivers/rtc/Kconfig                        |  11 +
 drivers/rtc/Makefile                       |   1 +
 drivers/rtc/rtc-wilco-ec.c                 | 177 +++++++++++++++
 include/linux/platform_data/wilco-ec.h     | 144 +++++++++++++
 15 files changed, 1081 insertions(+), 60 deletions(-)
 create mode 100644 Documentation/ABI/testing/debugfs-wilco-ec
 create mode 100644 drivers/platform/chrome/wilco_ec/Kconfig
 create mode 100644 drivers/platform/chrome/wilco_ec/Makefile
 create mode 100644 drivers/platform/chrome/wilco_ec/core.c
 create mode 100644 drivers/platform/chrome/wilco_ec/debugfs.c
 create mode 100644 drivers/platform/chrome/wilco_ec/mailbox.c
 create mode 100644 drivers/rtc/rtc-wilco-ec.c
 create mode 100644 include/linux/platform_data/wilco-ec.h
Signature · 1 line
2.20.1.611.gfbb209baf1-goog
author
#1 lore raw

PATCH v6 1/4 cros_ec: Remove cros_ec dependency in lpc_mec

In order to allow this code to be re-used, remove the dependency on the rest of the cros_ec code from the cros_ec_lpc_mec functions.

Instead of using a hardcoded register base address of 0x800 have this be passed in to cros_ec_lpc_mec_init(). The existing cros_ec use case now passes in the 0x800 base address this way.

There are some error checks that happen in cros_ec_lpc_mec_in_range() that probably shouldn't be there, as they are checking kernel-space callers and not user-space input. However, we'll just do the refactor in this patch, and in a future patch might remove this error checking and fix all the instances of code that calls this.

There's a similar problem in cros_ec_lpc_read_bytes(), where we return a checksum, but on error just return 0. This should probably be changed so that it returns int, but we don't want to have to mess with all the calling code for this fix. Maybe we'll come back through later and fix this.

---

Changes in v6: None
Changes in v5: None
Changes in v4: None
Changes in v3:
- Change <= to >= in mec_in_range()
- Add " - EC_HOST_CMD_REGION0" to offset arg for io_bytes_mec()

Changes in v2:
- Fixed kernel-doc comments
- Fixed include of linux/mfd/cros_ec_lpc_mec.h
- cros_ec_lpc_mec_in_range() returns -EINVAL on error
- Added parens around macro variables

 drivers/platform/chrome/cros_ec_lpc_mec.c | 52 +++++++++++++++++++----
 drivers/platform/chrome/cros_ec_lpc_mec.h | 43 ++++++++++---------
 drivers/platform/chrome/cros_ec_lpc_reg.c | 47 +++++++-------------
 3 files changed, 83 insertions(+), 59 deletions(-)
drivers/platform/chrome/cros_ec_lpc_mec.c +43 -9
index c4edfa83e493..782e238a8d4e 100644
@@ -23,7 +23,6 @@
 
 #include <linux/delay.h>
 #include <linux/io.h>
-#include <linux/mfd/cros_ec_commands.h>
 #include <linux/mutex.h>
 #include <linux/types.h>
 
@@ -34,6 +33,7 @@
  * EC mutex because memmap data may be accessed without it being held.
  */
 static struct mutex io_mutex;
+static u16 mec_emi_base, mec_emi_end;
 
 /*
  * cros_ec_lpc_mec_emi_write_address
@@ -46,10 +46,37 @@ static struct mutex io_mutex;
 static void cros_ec_lpc_mec_emi_write_address(u16 addr,
 			enum cros_ec_lpc_mec_emi_access_mode access_type)
 {
-	/* Address relative to start of EMI range */
-	addr -= MEC_EMI_RANGE_START;
-	outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0);
-	outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1);
+	outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0(mec_emi_base));
+	outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1(mec_emi_base));
+}
+
+/**
+ * cros_ec_lpc_mec_in_range() - Determine if addresses are in MEC EMI range.
+ *
+ * @offset: Address offset
+ * @length: Number of bytes to check
+ *
+ * Return: 1 if in range, 0 if not, and -EINVAL on failure
+ *         such as the mec range not being initialized
+ */
+int cros_ec_lpc_mec_in_range(unsigned int offset, unsigned int length)
+{
+	if (length == 0)
+		return -EINVAL;
+
+	if (WARN_ON(mec_emi_base == 0 || mec_emi_end == 0))
+		return -EINVAL;
+
+	if (offset >= mec_emi_base && offset < mec_emi_end) {
+		if (WARN_ON(offset + length - 1 >= mec_emi_end))
+			return -EINVAL;
+		return 1;
+	}
+
+	if (WARN_ON(offset + length > mec_emi_base && offset < mec_emi_end))
+		return -EINVAL;
+
+	return 0;
 }
 
 /*
@@ -71,6 +98,11 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
 	u8 sum = 0;
 	enum cros_ec_lpc_mec_emi_access_mode access, new_access;
 
+	/* Return checksum of 0 if window is not initialized */
+	WARN_ON(mec_emi_base == 0 || mec_emi_end == 0);
+	if (mec_emi_base == 0 || mec_emi_end == 0)
+		return 0;
+
 	/*
 	 * Long access cannot be used on misaligned data since reading B0 loads
 	 * the data register and writing B3 flushes.
@@ -86,9 +118,9 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
 	cros_ec_lpc_mec_emi_write_address(offset, access);
 
 	/* Skip bytes in case of misaligned offset */
-	io_addr = MEC_EMI_EC_DATA_B0 + (offset & 0x3);
+	io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base) + (offset & 0x3);
 	while (i < length) {
-		while (io_addr <= MEC_EMI_EC_DATA_B3) {
+		while (io_addr <= MEC_EMI_EC_DATA_B3(mec_emi_base)) {
 			if (io_type == MEC_IO_READ)
 				buf[i] = inb(io_addr++);
 			else
@@ -118,7 +150,7 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
 		}
 
 		/* Access [B0, B3] on each loop pass */
-		io_addr = MEC_EMI_EC_DATA_B0;
+		io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base);
 	}
 
 done:
@@ -128,9 +160,11 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
 }
 EXPORT_SYMBOL(cros_ec_lpc_io_bytes_mec);
 
-void cros_ec_lpc_mec_init(void)
+void cros_ec_lpc_mec_init(unsigned int base, unsigned int end)
 {
 	mutex_init(&io_mutex);
+	mec_emi_base = base;
+	mec_emi_end = end;
 }
 EXPORT_SYMBOL(cros_ec_lpc_mec_init);
 
drivers/platform/chrome/cros_ec_lpc_mec.h +24 -19

65 lines not on this page · raw message

drivers/platform/chrome/cros_ec_lpc_reg.c +16 -31

67 lines not on this page · raw message

Signature · 1 line
2.20.1.611.gfbb209baf1-goog
author
#2 lore raw

PATCH v6 2/4 platform/chrome: Add new driver for Wilco EC

In reply to Nick Crews

This EC is an incompatible variant of the typical Chrome OS embedded controller. It uses the same low-level communication and a similar protocol with some significant differences. The EC firmware does not support the same mailbox commands so it is not registered as a cros_ec device type. This commit exports the wilco_ec_mailbox() function so that other modules can use it to communicate with the EC.

---

Changes in v6:
- Re-added WILCO_EC_FLAG_EXTENDED_DATA and went back to always reading either EC_MAILBOX_DATA_SIZE or EC_MAILBOX_DATA_SIZE_EXTENDED bytes, since it turns out the EC always expects you to calculate the checksum over all of them.
- Split WILCO_EC_MSG_TELEMETRY into WILCO_EC_MSG_TELEMETRY_SHORT and WILCO_EC_MSG_TELEMETRY_LONG, since there will be two different commands.
- A couple comment and err message polishes

Changes in v5:
- move checking of NO_RESPONSE flag before timeout check, so now timeout doesn't always happen when EC isn't supposed to respond.
- rm WILCO_EC_FLAG_EXTENDED_DATA, that is already obvious from wilco_ec_message.response_size

Changes in v4:
- add #define DRV_NAME
- remove redundant Kconfig nesting
- changed my email to @chromium.org
- Add better explanation of what core.c does

Changes in v3:
- remove unused ret in probe()
- Add newline spacer in probe()
- rm unnecessary res in get_resource()
- s/8bit/8-bit
- rm first sleep when sending command to EC

Changes in v2:
- Remove COMPILE_TEST from Kconfig because inb()/outb() won't work on anything but X86
- Add myself as module author
- Tweak mailbox()

 drivers/platform/chrome/Kconfig            |   4 +-
 drivers/platform/chrome/Makefile           |   2 +
 drivers/platform/chrome/wilco_ec/Kconfig   |  11 +
 drivers/platform/chrome/wilco_ec/Makefile  |   4 +
 drivers/platform/chrome/wilco_ec/core.c    | 104 +++++++++
 drivers/platform/chrome/wilco_ec/mailbox.c | 236 +++++++++++++++++++++
 include/linux/platform_data/wilco-ec.h     | 140 ++++++++++++
 7 files changed, 500 insertions(+), 1 deletion(-)
 create mode 100644 drivers/platform/chrome/wilco_ec/Kconfig
 create mode 100644 drivers/platform/chrome/wilco_ec/Makefile
 create mode 100644 drivers/platform/chrome/wilco_ec/core.c
 create mode 100644 drivers/platform/chrome/wilco_ec/mailbox.c
 create mode 100644 include/linux/platform_data/wilco-ec.h
drivers/platform/chrome/Kconfig +3 -1
index 16b1615958aa..bf889adfd4ef 100644
@@ -49,6 +49,8 @@ config CHROMEOS_TBMC
 	  To compile this driver as a module, choose M here: the
 	  module will be called chromeos_tbmc.
 
+source "drivers/platform/chrome/wilco_ec/Kconfig"
+
 config CROS_EC_CTL
         tristate
 
@@ -86,7 +88,7 @@ config CROS_EC_LPC
 
 config CROS_EC_LPC_MEC
 	bool "ChromeOS Embedded Controller LPC Microchip EC (MEC) variant"
-	depends on CROS_EC_LPC
+	depends on CROS_EC_LPC || WILCO_EC
 	default n
 	help
 	  If you say Y here, a variant LPC protocol for the Microchip EC
drivers/platform/chrome/Makefile +2 -0
index cd591bf872bb..7242ee2b13c5 100644
@@ -13,3 +13,5 @@ cros_ec_lpcs-$(CONFIG_CROS_EC_LPC_MEC)	+= cros_ec_lpc_mec.o
 obj-$(CONFIG_CROS_EC_LPC)		+= cros_ec_lpcs.o
 obj-$(CONFIG_CROS_EC_PROTO)		+= cros_ec_proto.o
 obj-$(CONFIG_CROS_KBD_LED_BACKLIGHT)	+= cros_kbd_led_backlight.o
+
+obj-$(CONFIG_WILCO_EC)			+= wilco_ec/
drivers/platform/chrome/wilco_ec/Kconfig new file +11 -0
new file mode 100644
index 000000000000..20945a301ec6
@@ -0,0 +1,11 @@
+config WILCO_EC
+	tristate "ChromeOS Wilco Embedded Controller"
+	depends on ACPI && X86
+	select CROS_EC_LPC_MEC
+	help
+	  If you say Y here, you get support for talking to the ChromeOS
+	  Wilco EC over an eSPI bus. This uses a simple byte-level protocol
+	  with a checksum.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called wilco_ec.
drivers/platform/chrome/wilco_ec/Makefile new file +4 -0
new file mode 100644
index 000000000000..03b32301dc61
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0
+
+wilco_ec-objs				:= core.o mailbox.o
+obj-$(CONFIG_WILCO_EC)			+= wilco_ec.o
drivers/platform/chrome/wilco_ec/core.c new file +104 -0

104 lines not on this page · raw message

drivers/platform/chrome/wilco_ec/mailbox.c new file +236 -0

236 lines not on this page · raw message

include/linux/platform_data/wilco-ec.h new file +140 -0

140 lines not on this page · raw message

Signature · 1 line
2.20.1.611.gfbb209baf1-goog
author
#3 lore raw

PATCH v6 3/4 platform/chrome: Add support for raw commands in debugfs

In reply to Nick Crews

Add a debugfs attribute that allows sending raw commands to the EC. This is useful for development and debug but should not be enabled in a production environment.

To test:
Get the EC firmware build date
First send the request command

Quoted 1 line

echo 00 f0 38 00 03 00 > raw

Then read the result. "12/21/18" is in the middle of the response

Quoted 1 line

cat raw

00 31 32 2f 32 31 2f 31 38 00 00 0f 01 00 01 00 .12/21/18.......

Get the EC firmware build date
First send the request command

Quoted 1 line

echo 00 f0 38 00 03 00 > raw

Then read the result. "12/21/18" is in the middle of the response

Quoted 1 line

cat raw

00 31 32 2f 32 31 2f 31 38 00 00 0f 01 00 01 00 .12/21/18.......

---

Changes in v6:
- s/4.19/5.1/ for kernel version in documentation, since that is the version this patch should land in.
- Instead of requiring at least 3 bytes for msg type and command, now just require two for msg type. We can skip the command.
- Fixed error checking in probe() so that errors are hidden, without causing more errors or unextpected behavior.
- Some comment polishing.
- In the core, actually unregister the debugfs child platform_device

Changes in v5:
- core now always continues regardless of debugfs failure
- mv documentation to file header
- Check for OOM
- rm unneeded check if debug_info is allocqated
- rm bogus comment
- add space around "+"
- rm WILCO_EC_FLAG_EXTENDED_DATA, that is already obvious from wilco_ec_message.response_size

Changes in v4:
- Change debugfs driver to be a separate module
- Change me email to @chromium.org from @google.com
- Change CONFIG_WILCO_EC_SYSFS_RAW to CONFIG_WILCO_EC_DEBUGFS

Changes in v3:
- Move the attribute to the debugfs system
- Move the implementation to debugfs.c
- Improve the raw hex parsing
- Encapsulate global variables in one object
- Add safety check when passing less than 3 bytes
- Move documentation to debugfs-wilco-ec

Changes in v2:
- Add sysfs documentation
- rm duplicate EC_MAILBOX_DATA_SIZE defs
- Make docstrings follow kernel style
- Fix tags in commit msg
- Move Kconfig to subdirectory
- Reading raw now includes ASCII translation

 Documentation/ABI/testing/debugfs-wilco-ec |  23 ++
 drivers/platform/chrome/wilco_ec/Kconfig   |  10 +
 drivers/platform/chrome/wilco_ec/Makefile  |   2 +
 drivers/platform/chrome/wilco_ec/core.c    |  14 ++
 drivers/platform/chrome/wilco_ec/debugfs.c | 238 +++++++++++++++++++++
 include/linux/platform_data/wilco-ec.h     |   2 +
 6 files changed, 289 insertions(+)
 create mode 100644 Documentation/ABI/testing/debugfs-wilco-ec
 create mode 100644 drivers/platform/chrome/wilco_ec/debugfs.c
Documentation/ABI/testing/debugfs-wilco-ec new file +23 -0
new file mode 100644
index 000000000000..f814f112e213
@@ -0,0 +1,23 @@
+What:		/sys/kernel/debug/wilco_ec/raw
+Date:		January 2019
+KernelVersion:	5.1
+Description:
+		Write and read raw mailbox commands to the EC.
+
+		For writing:
+		Bytes 0-1 indicate the message type:
+			00 F0 = Execute Legacy Command
+			00 F2 = Read/Write NVRAM Property
+		Byte 2 provides the command code
+		Bytes 3+ consist of the data passed in the request
+
+		At least three bytes are required, for the msg type and command,
+		with additional bytes optional for additional data.
+
+		Example:
+		// Request EC info type 3 (EC firmware build date)
+		$ echo 00 f0 38 00 03 00 > raw
+		// View the result. The decoded ASCII result "12/21/18" is
+		// included after the raw hex.
+		$ cat raw
+		00 31 32 2f 32 31 2f 31 38 00 38 00 01 00 2f 00  .12/21/18.8...
drivers/platform/chrome/wilco_ec/Kconfig +10 -0
index 20945a301ec6..fb6d9ff33e41 100644
@@ -9,3 +9,13 @@ config WILCO_EC
 
 	  To compile this driver as a module, choose M here: the
 	  module will be called wilco_ec.
+
+config WILCO_EC_DEBUGFS
+	tristate "Enable raw access to EC via debugfs"
+	depends on WILCO_EC
+	help
+	  If you say Y here, you get support for sending raw commands to
+	  the Wilco EC via debugfs.  These commands do not do any byte
+	  manipulation and allow for testing arbitrary commands.  This
+	  interface is intended for debug only and will not be present
+	  on production devices.
drivers/platform/chrome/wilco_ec/Makefile +2 -0
index 03b32301dc61..063e7fb4ea17 100644
@@ -2,3 +2,5 @@
 
 wilco_ec-objs				:= core.o mailbox.o
 obj-$(CONFIG_WILCO_EC)			+= wilco_ec.o
+wilco_ec_debugfs-objs			:= debugfs.o
+obj-$(CONFIG_WILCO_EC_DEBUGFS)		+= wilco_ec_debugfs.o
drivers/platform/chrome/wilco_ec/core.c +14 -0
index 20ecc580d108..ae86cae216fd 100644
@@ -69,11 +69,25 @@ static int wilco_ec_probe(struct platform_device *pdev)
 	cros_ec_lpc_mec_init(ec->io_packet->start,
 			     ec->io_packet->start + EC_MAILBOX_DATA_SIZE);
 
+	/*
+	 * Register a debugfs platform device that will get picked up by the
+	 * debugfs driver. Ignore failure.
+	 */
+	ec->debugfs_pdev = platform_device_register_data(dev,
+							 "wilco-ec-debugfs",
+							 PLATFORM_DEVID_AUTO,
+							 NULL, 0);
+
 	return 0;
 }
 
 static int wilco_ec_remove(struct platform_device *pdev)
 {
+	struct wilco_ec_device *ec = platform_get_drvdata(pdev);
+
+	if (ec->debugfs_pdev)
+		platform_device_unregister(ec->debugfs_pdev);
+
 	/* Teardown cros_ec interface */
 	cros_ec_lpc_mec_destroy();
 
drivers/platform/chrome/wilco_ec/debugfs.c new file +238 -0

238 lines not on this page · raw message

include/linux/platform_data/wilco-ec.h +2 -0
index 0feb4b520a54..5344975afa1a 100644
@@ -34,6 +34,7 @@
  * @data_buffer: Buffer used for EC communication.  The same buffer
  *               is used to hold the request and the response.
  * @data_size: Size of the data buffer used for EC communication.
+ * @debugfs_pdev: The child platform_device used by the debugfs sub-driver.
  */
 struct wilco_ec_device {
 	struct device *dev;
@@ -43,6 +44,7 @@ struct wilco_ec_device {
 	struct resource *io_packet;
 	void *data_buffer;
 	size_t data_size;
+	struct platform_device *debugfs_pdev;
 };
 
 /**
Signature · 1 line
2.20.1.611.gfbb209baf1-goog
#4 lore raw

Re: [PATCH v6 3/4] platform/chrome: Add support for raw commands in debugfs

Hi Nick,
Missatge de Nick Crews <[email protected]> del dia dv., 8 de febr. 2019 a les 2:24:

Quoted 238 lines

Add a debugfs attribute that allows sending raw commands to the EC. This is useful for development and debug but should not be enabled in a production environment.

To test:
Get the EC firmware build date
First send the request command

Quoted 1 line

echo 00 f0 38 00 03 00 > raw

Then read the result. "12/21/18" is in the middle of the response

Quoted 1 line

cat raw

00 31 32 2f 32 31 2f 31 38 00 00 0f 01 00 01 00 .12/21/18.......

Get the EC firmware build date
First send the request command

Quoted 1 line

echo 00 f0 38 00 03 00 > raw

Then read the result. "12/21/18" is in the middle of the response

Quoted 1 line

cat raw

00 31 32 2f 32 31 2f 31 38 00 00 0f 01 00 01 00 .12/21/18.......

---

Changes in v6:
- s/4.19/5.1/ for kernel version in documentation, since that is the version this patch should land in.
- Instead of requiring at least 3 bytes for msg type and command, now just require two for msg type. We can skip the command.
- Fixed error checking in probe() so that errors are hidden, without causing more errors or unextpected behavior.
- Some comment polishing.
- In the core, actually unregister the debugfs child platform_device

Changes in v5:
- core now always continues regardless of debugfs failure
- mv documentation to file header
- Check for OOM
- rm unneeded check if debug_info is allocqated
- rm bogus comment
- add space around "+"
- rm WILCO_EC_FLAG_EXTENDED_DATA, that is already obvious from wilco_ec_message.response_size

Changes in v4:
- Change debugfs driver to be a separate module
- Change me email to @chromium.org from @google.com
- Change CONFIG_WILCO_EC_SYSFS_RAW to CONFIG_WILCO_EC_DEBUGFS

Changes in v3:
- Move the attribute to the debugfs system
- Move the implementation to debugfs.c
- Improve the raw hex parsing
- Encapsulate global variables in one object
- Add safety check when passing less than 3 bytes
- Move documentation to debugfs-wilco-ec

Changes in v2:
- Add sysfs documentation
- rm duplicate EC_MAILBOX_DATA_SIZE defs
- Make docstrings follow kernel style
- Fix tags in commit msg
- Move Kconfig to subdirectory
- Reading raw now includes ASCII translation

 Documentation/ABI/testing/debugfs-wilco-ec |  23 ++
 drivers/platform/chrome/wilco_ec/Kconfig   |  10 +
 drivers/platform/chrome/wilco_ec/Makefile  |   2 +
 drivers/platform/chrome/wilco_ec/core.c    |  14 ++
 drivers/platform/chrome/wilco_ec/debugfs.c | 238 +++++++++++++++++++++
 include/linux/platform_data/wilco-ec.h     |   2 +
 6 files changed, 289 insertions(+)
 create mode 100644 Documentation/ABI/testing/debugfs-wilco-ec
 create mode 100644 drivers/platform/chrome/wilco_ec/debugfs.c
Documentation/ABI/testing/debugfs-wilco-ec new file +23 -0
new file mode 100644
index 000000000000..f814f112e213
@@ -0,0 +1,23 @@
+What:          /sys/kernel/debug/wilco_ec/raw
+Date:          January 2019
+KernelVersion: 5.1
+Description:
+               Write and read raw mailbox commands to the EC.
+
+               For writing:
+               Bytes 0-1 indicate the message type:
+                       00 F0 = Execute Legacy Command
+                       00 F2 = Read/Write NVRAM Property
+               Byte 2 provides the command code
+               Bytes 3+ consist of the data passed in the request
+
+               At least three bytes are required, for the msg type and command,
+               with additional bytes optional for additional data.
+
+               Example:
+               // Request EC info type 3 (EC firmware build date)
+               $ echo 00 f0 38 00 03 00 > raw
+               // View the result. The decoded ASCII result "12/21/18" is
+               // included after the raw hex.
+               $ cat raw
+               00 31 32 2f 32 31 2f 31 38 00 38 00 01 00 2f 00  .12/21/18.8...
drivers/platform/chrome/wilco_ec/Kconfig +10 -0
index 20945a301ec6..fb6d9ff33e41 100644
@@ -9,3 +9,13 @@ config WILCO_EC
 
          To compile this driver as a module, choose M here: the
          module will be called wilco_ec.
+
+config WILCO_EC_DEBUGFS
+       tristate "Enable raw access to EC via debugfs"
+       depends on WILCO_EC
+       help
+         If you say Y here, you get support for sending raw commands to
+         the Wilco EC via debugfs.  These commands do not do any byte
+         manipulation and allow for testing arbitrary commands.  This
+         interface is intended for debug only and will not be present
+         on production devices.
drivers/platform/chrome/wilco_ec/Makefile +2 -0
index 03b32301dc61..063e7fb4ea17 100644
@@ -2,3 +2,5 @@
 
 wilco_ec-objs                          := core.o mailbox.o
 obj-$(CONFIG_WILCO_EC)                 += wilco_ec.o
+wilco_ec_debugfs-objs                  := debugfs.o
+obj-$(CONFIG_WILCO_EC_DEBUGFS)         += wilco_ec_debugfs.o
drivers/platform/chrome/wilco_ec/core.c +14 -0
index 20ecc580d108..ae86cae216fd 100644
@@ -69,11 +69,25 @@ static int wilco_ec_probe(struct platform_device *pdev)
        cros_ec_lpc_mec_init(ec->io_packet->start,
                             ec->io_packet->start + EC_MAILBOX_DATA_SIZE);
 
+       /*
+        * Register a debugfs platform device that will get picked up by the
+        * debugfs driver. Ignore failure.
+        */
+       ec->debugfs_pdev = platform_device_register_data(dev,
+                                                        "wilco-ec-debugfs",
+                                                        PLATFORM_DEVID_AUTO,
+                                                        NULL, 0);
+
        return 0;
 }
 
 static int wilco_ec_remove(struct platform_device *pdev)
 {
+       struct wilco_ec_device *ec = platform_get_drvdata(pdev);
+
+       if (ec->debugfs_pdev)
+               platform_device_unregister(ec->debugfs_pdev);
+
        /* Teardown cros_ec interface */
        cros_ec_lpc_mec_destroy();
 
drivers/platform/chrome/wilco_ec/debugfs.c new file +69 -0
new file mode 100644
index 000000000000..8b324bfe42cd
@@ -0,0 +1,238 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * debugfs attributes for Wilco EC
+ *
+ * Copyright 2019 Google LLC
+ *
+ * There is only one attribute used for debugging, called raw.
+ * You can write a hexadecimal sentence to raw, and that series of bytes
+ * will be sent to the EC. Then, you can read the bytes of response
+ * by reading from raw.
+ *
+ * For writing:
+ * Bytes 0-1 indicate the message type:
+ *         00 F0 = Execute Legacy Command
+ *         00 F2 = Read/Write NVRAM Property
+ * Byte 2 provides the command code
+ * Bytes 3+ consist of the data passed in the request
+ *
+ * When referencing the EC interface spec, byte 2 corresponds to MBOX[0],
+ * byte 3 corresponds to MBOX[1], etc.
+ *
+ * At least three bytes are required, for the msg type and command,
+ * with additional bytes optional for additional data.
+ *
+ * Example:
+ * // Request EC info type 3 (EC firmware build date)
+ * $ echo 00 f0 38 00 03 00 > raw
+ * // View the result. The decoded ASCII result "12/21/18" is
+ * // included after the raw hex.
+ * $ cat raw
+ * 00 31 32 2f 32 31 2f 31 38 00 38 00 01 00 2f 00  .12/21/18.8...
+ */
+
+#include <linux/ctype.h>
+#include <linux/debugfs.h>
+#include <linux/fs.h>
+#include <linux/module.h>
+#include <linux/platform_data/wilco-ec.h>
+#include <linux/platform_device.h>
+
+#define DRV_NAME "wilco-ec-debugfs"
+
+/* The 256 raw bytes will take up more space when represented as a hex string */
+#define FORMATTED_BUFFER_SIZE (EC_MAILBOX_DATA_SIZE_EXTENDED * 4)
+
+struct wilco_ec_debugfs {
+       struct wilco_ec_device *ec;
+       struct dentry *dir;
+       size_t response_size;
+       u8 raw_data[EC_MAILBOX_DATA_SIZE_EXTENDED];
+       u8 formatted_data[FORMATTED_BUFFER_SIZE];
+};
+static struct wilco_ec_debugfs *debug_info;
+
+/**
+ * parse_hex_sentence() - Convert a ascii hex representation into byte array.
+ * @in: Input buffer of ascii.
+ * @isize: Length of input buffer.
+ * @out: Output buffer.
+ * @osize: Length of output buffer, e.g. max number of bytes to parse.
+ *
+ * An valid input is a series of ascii hexadecimal numbers, separated by spaces.
+ * An example valid input is
+ * "   00 f2 0    000076 6 0  ff"
+ *
+ * If an individual "word" within the hex sentence is longer than MAX_WORD_SIZE,
+ * then the sentence is illegal, and parsing will fail.
+ *
+ * Return: Number of bytes parsed, or negative error code on failure.

drivers/platform/chrome/wilco_ec/debugfs.c line 79

Quoted 10 lines
drivers/platform/chrome/wilco_ec/debugfs.c new file +10 -0
+ */
+static int parse_hex_sentence(const char *in, int isize, u8 *out, int osize)
+{
+       int n_parsed = 0;
+       int word_start = 0;
+       int word_end;
+       int word_len;
+       /* Temp buffer for holding a "word" of chars that represents one byte */
+       const static int MAX_WORD_SIZE = 16;
+       char tmp[MAX_WORD_SIZE + 1];

I got this warning:

CC [M] drivers/platform/chrome/wilco_ec/debugfs.o
drivers/platform/chrome/wilco_ec/debugfs.c: In function ‘parse_hex_sentence’:
drivers/platform/chrome/wilco_ec/debugfs.c:79:2: warning: ISO C90 forbids array ‘tmp’ whose size can’t be evaluated [-Wvla]
char tmp[MAX_WORD_SIZE + 1];
^~~~

Could you kindly fix and resend, please.

Thanks,
Enric

Quoted 182 lines
drivers/platform/chrome/wilco_ec/debugfs.c new file +159 -0
+       u8 byte;
+
+       while (word_start < isize && n_parsed < osize) {
+               /* Find the start of the next word */
+               while (word_start < isize && isspace(in[word_start]))
+                       word_start++;
+                /* reached the end of the input before next word? */
+               if (word_start >= isize)
+                       break;
+
+               /* Find the end of this word */
+               word_end = word_start;
+               while (word_end < isize && !isspace(in[word_end]))
+                       word_end++;
+
+               /* Copy to a tmp NULL terminated string */
+               word_len = word_end - word_start;
+               if (word_len > MAX_WORD_SIZE)
+                       return -EINVAL;
+               memcpy(tmp, in + word_start, word_len);
+               tmp[word_len] = '\0';
+
+               /*
+                * Convert from hex string, place in output. If fails to parse,
+                * just return -EINVAL because specific error code is only
+                * relevant for this one word, returning it would be confusing.
+                */
+               if (kstrtou8(tmp, 16, &byte))
+                       return -EINVAL;
+               out[n_parsed++] = byte;
+
+               word_start = word_end;
+       }
+       return n_parsed;
+}
+
+/* The message type takes up two bytes*/
+#define TYPE_AND_DATA_SIZE ((EC_MAILBOX_DATA_SIZE) + 2)
+
+static ssize_t raw_write(struct file *file, const char __user *user_buf,
+                        size_t count, loff_t *ppos)
+{
+       char *buf = debug_info->formatted_data;
+       struct wilco_ec_message msg;
+       u8 request_data[TYPE_AND_DATA_SIZE];
+       ssize_t kcount;
+       int ret;
+
+       if (count > FORMATTED_BUFFER_SIZE)
+               return -EINVAL;
+
+       kcount = simple_write_to_buffer(buf, FORMATTED_BUFFER_SIZE, ppos,
+                                       user_buf, count);
+       if (kcount < 0)
+               return kcount;
+
+       ret = parse_hex_sentence(buf, kcount, request_data, TYPE_AND_DATA_SIZE);
+       if (ret < 0)
+               return ret;
+       /* Need at least two bytes for message type */
+       if (ret < 2)
+               return -EINVAL;
+
+       /* Clear response data buffer */
+       memset(debug_info->raw_data, '\0', EC_MAILBOX_DATA_SIZE_EXTENDED);
+
+       msg.type = request_data[0] << 8 | request_data[1];
+       msg.flags = WILCO_EC_FLAG_RAW;
+       msg.command = ret > 2 ? request_data[2] : 0;
+       msg.request_data = ret > 3 ? request_data + 3 : 0;
+       msg.request_size = ret - 3;
+       msg.response_data = debug_info->raw_data;
+       msg.response_size = EC_MAILBOX_DATA_SIZE;
+
+       /* Telemetry commands use extended response data */
+       if (msg.type == WILCO_EC_MSG_TELEMETRY_LONG) {
+               msg.flags |= WILCO_EC_FLAG_EXTENDED_DATA;
+               msg.response_size = EC_MAILBOX_DATA_SIZE_EXTENDED;
+       }
+
+       ret = wilco_ec_mailbox(debug_info->ec, &msg);
+       if (ret < 0)
+               return ret;
+       debug_info->response_size = ret;
+
+       return count;
+}
+
+static ssize_t raw_read(struct file *file, char __user *user_buf, size_t count,
+                       loff_t *ppos)
+{
+       int fmt_len = 0;
+
+       if (debug_info->response_size) {
+               fmt_len = hex_dump_to_buffer(debug_info->raw_data,
+                                            debug_info->response_size,
+                                            16, 1, debug_info->formatted_data,
+                                            FORMATTED_BUFFER_SIZE, true);
+               /* Only return response the first time it is read */
+               debug_info->response_size = 0;
+       }
+
+       return simple_read_from_buffer(user_buf, count, ppos,
+                                      debug_info->formatted_data, fmt_len);
+}
+
+static const struct file_operations fops_raw = {
+       .owner = THIS_MODULE,
+       .read = raw_read,
+       .write = raw_write,
+       .llseek = no_llseek,
+};
+
+/**
+ * wilco_ec_debugfs_probe() - Create the debugfs node
+ * @pdev: The platform device, probably created in core.c
+ *
+ * Try to create a debugfs node. If it fails, then we don't want to change
+ * behavior at all, this is for debugging after all. Just fail silently.
+ *
+ * Return: 0 always.
+ */
+static int wilco_ec_debugfs_probe(struct platform_device *pdev)
+{
+       struct wilco_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
+
+       debug_info = devm_kzalloc(&pdev->dev, sizeof(*debug_info), GFP_KERNEL);
+       if (!debug_info)
+               return 0;
+       debug_info->ec = ec;
+       debug_info->dir = debugfs_create_dir("wilco_ec", NULL);
+       if (!debug_info->dir)
+               return 0;
+       debugfs_create_file("raw", 0644, debug_info->dir, NULL, &fops_raw);
+
+       return 0;
+}
+
+static int wilco_ec_debugfs_remove(struct platform_device *pdev)
+{
+       debugfs_remove_recursive(debug_info->dir);
+
+       return 0;
+}
+
+static struct platform_driver wilco_ec_debugfs_driver = {
+       .driver = {
+               .name = DRV_NAME,
+       },
+       .probe = wilco_ec_debugfs_probe,
+       .remove = wilco_ec_debugfs_remove,
+};
+
+module_platform_driver(wilco_ec_debugfs_driver);
+
+MODULE_ALIAS("platform:" DRV_NAME);
+MODULE_AUTHOR("Nick Crews <[email protected]>");
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Wilco EC debugfs driver");
include/linux/platform_data/wilco-ec.h +2 -1
index 0feb4b520a54..5344975afa1a 100644
@@ -34,6 +34,7 @@
  * @data_buffer: Buffer used for EC communication.  The same buffer
  *               is used to hold the request and the response.
  * @data_size: Size of the data buffer used for EC communication.
+ * @debugfs_pdev: The child platform_device used by the debugfs sub-driver.
  */
 struct wilco_ec_device {
        struct device *dev;
@@ -43,6 +44,7 @@ struct wilco_ec_device {
        struct resource *io_packet;
        void *data_buffer;
        size_t data_size;
+       struct platform_device *debugfs_pdev;
 };
 
 /**
--

2.20.1.611.gfbb209baf1-goog

author
#5 lore raw

Re: [PATCH v6 3/4] platform/chrome: Add support for raw commands in debugfs

I just sent out a fixed version. Thanks!

Quoted 449 lines · Enric Balletbo Serra

Hi Nick,
Missatge de Nick Crews <[email protected]> del dia dv., 8 de febr. 2019 a les 2:24:

Quoted 238 lines

Add a debugfs attribute that allows sending raw commands to the EC. This is useful for development and debug but should not be enabled in a production environment.

To test:
Get the EC firmware build date
First send the request command

Quoted 1 line

echo 00 f0 38 00 03 00 > raw

Then read the result. "12/21/18" is in the middle of the response

Quoted 1 line

cat raw

00 31 32 2f 32 31 2f 31 38 00 00 0f 01 00 01 00 .12/21/18.......

Get the EC firmware build date
First send the request command

Quoted 1 line

echo 00 f0 38 00 03 00 > raw

Then read the result. "12/21/18" is in the middle of the response

Quoted 1 line

cat raw

00 31 32 2f 32 31 2f 31 38 00 00 0f 01 00 01 00 .12/21/18.......

---

Changes in v6:
- s/4.19/5.1/ for kernel version in documentation, since that is the version this patch should land in.
- Instead of requiring at least 3 bytes for msg type and command, now just require two for msg type. We can skip the command.
- Fixed error checking in probe() so that errors are hidden, without causing more errors or unextpected behavior.
- Some comment polishing.
- In the core, actually unregister the debugfs child platform_device

Changes in v5:
- core now always continues regardless of debugfs failure
- mv documentation to file header
- Check for OOM
- rm unneeded check if debug_info is allocqated
- rm bogus comment
- add space around "+"
- rm WILCO_EC_FLAG_EXTENDED_DATA, that is already obvious from wilco_ec_message.response_size

Changes in v4:
- Change debugfs driver to be a separate module
- Change me email to @chromium.org from @google.com
- Change CONFIG_WILCO_EC_SYSFS_RAW to CONFIG_WILCO_EC_DEBUGFS

Changes in v3:
- Move the attribute to the debugfs system
- Move the implementation to debugfs.c
- Improve the raw hex parsing
- Encapsulate global variables in one object
- Add safety check when passing less than 3 bytes
- Move documentation to debugfs-wilco-ec

Changes in v2:
- Add sysfs documentation
- rm duplicate EC_MAILBOX_DATA_SIZE defs
- Make docstrings follow kernel style
- Fix tags in commit msg
- Move Kconfig to subdirectory
- Reading raw now includes ASCII translation

 Documentation/ABI/testing/debugfs-wilco-ec |  23 ++
 drivers/platform/chrome/wilco_ec/Kconfig   |  10 +
 drivers/platform/chrome/wilco_ec/Makefile  |   2 +
 drivers/platform/chrome/wilco_ec/core.c    |  14 ++
 drivers/platform/chrome/wilco_ec/debugfs.c | 238 +++++++++++++++++++++
 include/linux/platform_data/wilco-ec.h     |   2 +
 6 files changed, 289 insertions(+)
 create mode 100644 Documentation/ABI/testing/debugfs-wilco-ec
 create mode 100644 drivers/platform/chrome/wilco_ec/debugfs.c
Documentation/ABI/testing/debugfs-wilco-ec new file +23 -0
new file mode 100644
index 000000000000..f814f112e213
@@ -0,0 +1,23 @@
+What:          /sys/kernel/debug/wilco_ec/raw
+Date:          January 2019
+KernelVersion: 5.1
+Description:
+               Write and read raw mailbox commands to the EC.
+
+               For writing:
+               Bytes 0-1 indicate the message type:
+                       00 F0 = Execute Legacy Command
+                       00 F2 = Read/Write NVRAM Property
+               Byte 2 provides the command code
+               Bytes 3+ consist of the data passed in the request
+
+               At least three bytes are required, for the msg type and command,
+               with additional bytes optional for additional data.
+
+               Example:
+               // Request EC info type 3 (EC firmware build date)
+               $ echo 00 f0 38 00 03 00 > raw
+               // View the result. The decoded ASCII result "12/21/18" is
+               // included after the raw hex.
+               $ cat raw
+               00 31 32 2f 32 31 2f 31 38 00 38 00 01 00 2f 00  .12/21/18.8...
drivers/platform/chrome/wilco_ec/Kconfig +10 -0
index 20945a301ec6..fb6d9ff33e41 100644
@@ -9,3 +9,13 @@ config WILCO_EC
 
          To compile this driver as a module, choose M here: the
          module will be called wilco_ec.
+
+config WILCO_EC_DEBUGFS
+       tristate "Enable raw access to EC via debugfs"
+       depends on WILCO_EC
+       help
+         If you say Y here, you get support for sending raw commands to
+         the Wilco EC via debugfs.  These commands do not do any byte
+         manipulation and allow for testing arbitrary commands.  This
+         interface is intended for debug only and will not be present
+         on production devices.
drivers/platform/chrome/wilco_ec/Makefile +2 -0
index 03b32301dc61..063e7fb4ea17 100644
@@ -2,3 +2,5 @@
 
 wilco_ec-objs                          := core.o mailbox.o
 obj-$(CONFIG_WILCO_EC)                 += wilco_ec.o
+wilco_ec_debugfs-objs                  := debugfs.o
+obj-$(CONFIG_WILCO_EC_DEBUGFS)         += wilco_ec_debugfs.o
drivers/platform/chrome/wilco_ec/core.c +14 -0
index 20ecc580d108..ae86cae216fd 100644
@@ -69,11 +69,25 @@ static int wilco_ec_probe(struct platform_device *pdev)
        cros_ec_lpc_mec_init(ec->io_packet->start,
                             ec->io_packet->start + EC_MAILBOX_DATA_SIZE);
 
+       /*
+        * Register a debugfs platform device that will get picked up by the
+        * debugfs driver. Ignore failure.
+        */
+       ec->debugfs_pdev = platform_device_register_data(dev,
+                                                        "wilco-ec-debugfs",
+                                                        PLATFORM_DEVID_AUTO,
+                                                        NULL, 0);
+
        return 0;
 }
 
 static int wilco_ec_remove(struct platform_device *pdev)
 {
+       struct wilco_ec_device *ec = platform_get_drvdata(pdev);
+
+       if (ec->debugfs_pdev)
+               platform_device_unregister(ec->debugfs_pdev);
+
        /* Teardown cros_ec interface */
        cros_ec_lpc_mec_destroy();
 
drivers/platform/chrome/wilco_ec/debugfs.c new file +69 -0
new file mode 100644
index 000000000000..8b324bfe42cd
@@ -0,0 +1,238 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * debugfs attributes for Wilco EC
+ *
+ * Copyright 2019 Google LLC
+ *
+ * There is only one attribute used for debugging, called raw.
+ * You can write a hexadecimal sentence to raw, and that series of bytes
+ * will be sent to the EC. Then, you can read the bytes of response
+ * by reading from raw.
+ *
+ * For writing:
+ * Bytes 0-1 indicate the message type:
+ *         00 F0 = Execute Legacy Command
+ *         00 F2 = Read/Write NVRAM Property
+ * Byte 2 provides the command code
+ * Bytes 3+ consist of the data passed in the request
+ *
+ * When referencing the EC interface spec, byte 2 corresponds to MBOX[0],
+ * byte 3 corresponds to MBOX[1], etc.
+ *
+ * At least three bytes are required, for the msg type and command,
+ * with additional bytes optional for additional data.
+ *
+ * Example:
+ * // Request EC info type 3 (EC firmware build date)
+ * $ echo 00 f0 38 00 03 00 > raw
+ * // View the result. The decoded ASCII result "12/21/18" is
+ * // included after the raw hex.
+ * $ cat raw
+ * 00 31 32 2f 32 31 2f 31 38 00 38 00 01 00 2f 00  .12/21/18.8...
+ */
+
+#include <linux/ctype.h>
+#include <linux/debugfs.h>
+#include <linux/fs.h>
+#include <linux/module.h>
+#include <linux/platform_data/wilco-ec.h>
+#include <linux/platform_device.h>
+
+#define DRV_NAME "wilco-ec-debugfs"
+
+/* The 256 raw bytes will take up more space when represented as a hex string */
+#define FORMATTED_BUFFER_SIZE (EC_MAILBOX_DATA_SIZE_EXTENDED * 4)
+
+struct wilco_ec_debugfs {
+       struct wilco_ec_device *ec;
+       struct dentry *dir;
+       size_t response_size;
+       u8 raw_data[EC_MAILBOX_DATA_SIZE_EXTENDED];
+       u8 formatted_data[FORMATTED_BUFFER_SIZE];
+};
+static struct wilco_ec_debugfs *debug_info;
+
+/**
+ * parse_hex_sentence() - Convert a ascii hex representation into byte array.
+ * @in: Input buffer of ascii.
+ * @isize: Length of input buffer.
+ * @out: Output buffer.
+ * @osize: Length of output buffer, e.g. max number of bytes to parse.
+ *
+ * An valid input is a series of ascii hexadecimal numbers, separated by spaces.
+ * An example valid input is
+ * "   00 f2 0    000076 6 0  ff"
+ *
+ * If an individual "word" within the hex sentence is longer than MAX_WORD_SIZE,
+ * then the sentence is illegal, and parsing will fail.
+ *
+ * Return: Number of bytes parsed, or negative error code on failure.

drivers/platform/chrome/wilco_ec/debugfs.c line 79

Quoted 10 lines
drivers/platform/chrome/wilco_ec/debugfs.c new file +10 -0
+ */
+static int parse_hex_sentence(const char *in, int isize, u8 *out, int osize)
+{
+       int n_parsed = 0;
+       int word_start = 0;
+       int word_end;
+       int word_len;
+       /* Temp buffer for holding a "word" of chars that represents one byte */
+       const static int MAX_WORD_SIZE = 16;
+       char tmp[MAX_WORD_SIZE + 1];

I got this warning:

CC [M] drivers/platform/chrome/wilco_ec/debugfs.o
drivers/platform/chrome/wilco_ec/debugfs.c: In function ‘parse_hex_sentence’:
drivers/platform/chrome/wilco_ec/debugfs.c:79:2: warning: ISO C90 forbids array ‘tmp’ whose size can’t be evaluated [-Wvla]
char tmp[MAX_WORD_SIZE + 1];
^~~~

Could you kindly fix and resend, please.

Thanks,
Enric

Quoted 182 lines
drivers/platform/chrome/wilco_ec/debugfs.c new file +159 -0
+       u8 byte;
+
+       while (word_start < isize && n_parsed < osize) {
+               /* Find the start of the next word */
+               while (word_start < isize && isspace(in[word_start]))
+                       word_start++;
+                /* reached the end of the input before next word? */
+               if (word_start >= isize)
+                       break;
+
+               /* Find the end of this word */
+               word_end = word_start;
+               while (word_end < isize && !isspace(in[word_end]))
+                       word_end++;
+
+               /* Copy to a tmp NULL terminated string */
+               word_len = word_end - word_start;
+               if (word_len > MAX_WORD_SIZE)
+                       return -EINVAL;
+               memcpy(tmp, in + word_start, word_len);
+               tmp[word_len] = '\0';
+
+               /*
+                * Convert from hex string, place in output. If fails to parse,
+                * just return -EINVAL because specific error code is only
+                * relevant for this one word, returning it would be confusing.
+                */
+               if (kstrtou8(tmp, 16, &byte))
+                       return -EINVAL;
+               out[n_parsed++] = byte;
+
+               word_start = word_end;
+       }
+       return n_parsed;
+}
+
+/* The message type takes up two bytes*/
+#define TYPE_AND_DATA_SIZE ((EC_MAILBOX_DATA_SIZE) + 2)
+
+static ssize_t raw_write(struct file *file, const char __user *user_buf,
+                        size_t count, loff_t *ppos)
+{
+       char *buf = debug_info->formatted_data;
+       struct wilco_ec_message msg;
+       u8 request_data[TYPE_AND_DATA_SIZE];
+       ssize_t kcount;
+       int ret;
+
+       if (count > FORMATTED_BUFFER_SIZE)
+               return -EINVAL;
+
+       kcount = simple_write_to_buffer(buf, FORMATTED_BUFFER_SIZE, ppos,
+                                       user_buf, count);
+       if (kcount < 0)
+               return kcount;
+
+       ret = parse_hex_sentence(buf, kcount, request_data, TYPE_AND_DATA_SIZE);
+       if (ret < 0)
+               return ret;
+       /* Need at least two bytes for message type */
+       if (ret < 2)
+               return -EINVAL;
+
+       /* Clear response data buffer */
+       memset(debug_info->raw_data, '\0', EC_MAILBOX_DATA_SIZE_EXTENDED);
+
+       msg.type = request_data[0] << 8 | request_data[1];
+       msg.flags = WILCO_EC_FLAG_RAW;
+       msg.command = ret > 2 ? request_data[2] : 0;
+       msg.request_data = ret > 3 ? request_data + 3 : 0;
+       msg.request_size = ret - 3;
+       msg.response_data = debug_info->raw_data;
+       msg.response_size = EC_MAILBOX_DATA_SIZE;
+
+       /* Telemetry commands use extended response data */
+       if (msg.type == WILCO_EC_MSG_TELEMETRY_LONG) {
+               msg.flags |= WILCO_EC_FLAG_EXTENDED_DATA;
+               msg.response_size = EC_MAILBOX_DATA_SIZE_EXTENDED;
+       }
+
+       ret = wilco_ec_mailbox(debug_info->ec, &msg);
+       if (ret < 0)
+               return ret;
+       debug_info->response_size = ret;
+
+       return count;
+}
+
+static ssize_t raw_read(struct file *file, char __user *user_buf, size_t count,
+                       loff_t *ppos)
+{
+       int fmt_len = 0;
+
+       if (debug_info->response_size) {
+               fmt_len = hex_dump_to_buffer(debug_info->raw_data,
+                                            debug_info->response_size,
+                                            16, 1, debug_info->formatted_data,
+                                            FORMATTED_BUFFER_SIZE, true);
+               /* Only return response the first time it is read */
+               debug_info->response_size = 0;
+       }
+
+       return simple_read_from_buffer(user_buf, count, ppos,
+                                      debug_info->formatted_data, fmt_len);
+}
+
+static const struct file_operations fops_raw = {
+       .owner = THIS_MODULE,
+       .read = raw_read,
+       .write = raw_write,
+       .llseek = no_llseek,
+};
+
+/**
+ * wilco_ec_debugfs_probe() - Create the debugfs node
+ * @pdev: The platform device, probably created in core.c
+ *
+ * Try to create a debugfs node. If it fails, then we don't want to change
+ * behavior at all, this is for debugging after all. Just fail silently.
+ *
+ * Return: 0 always.
+ */
+static int wilco_ec_debugfs_probe(struct platform_device *pdev)
+{
+       struct wilco_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
+
+       debug_info = devm_kzalloc(&pdev->dev, sizeof(*debug_info), GFP_KERNEL);
+       if (!debug_info)
+               return 0;
+       debug_info->ec = ec;
+       debug_info->dir = debugfs_create_dir("wilco_ec", NULL);
+       if (!debug_info->dir)
+               return 0;
+       debugfs_create_file("raw", 0644, debug_info->dir, NULL, &fops_raw);
+
+       return 0;
+}
+
+static int wilco_ec_debugfs_remove(struct platform_device *pdev)
+{
+       debugfs_remove_recursive(debug_info->dir);
+
+       return 0;
+}
+
+static struct platform_driver wilco_ec_debugfs_driver = {
+       .driver = {
+               .name = DRV_NAME,
+       },
+       .probe = wilco_ec_debugfs_probe,
+       .remove = wilco_ec_debugfs_remove,
+};
+
+module_platform_driver(wilco_ec_debugfs_driver);
+
+MODULE_ALIAS("platform:" DRV_NAME);
+MODULE_AUTHOR("Nick Crews <[email protected]>");
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Wilco EC debugfs driver");
include/linux/platform_data/wilco-ec.h +2 -1
index 0feb4b520a54..5344975afa1a 100644
@@ -34,6 +34,7 @@
  * @data_buffer: Buffer used for EC communication.  The same buffer
  *               is used to hold the request and the response.
  * @data_size: Size of the data buffer used for EC communication.
+ * @debugfs_pdev: The child platform_device used by the debugfs sub-driver.
  */
 struct wilco_ec_device {
        struct device *dev;
@@ -43,6 +44,7 @@ struct wilco_ec_device {
        struct resource *io_packet;
        void *data_buffer;
        size_t data_size;
+       struct platform_device *debugfs_pdev;
 };
 
 /**
--

2.20.1.611.gfbb209baf1-goog

author
#6 lore raw

PATCH v6 4/4 platform/chrome: rtc: Add RTC driver

In reply to Nick Crews

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.

To test:

Quoted 1 line

hwclock --show --rtc /dev/rtc1

2007-12-31 16:01:20.460959-08:00
Quoted 2 lines

hwclock --systohc --rtc /dev/rtc1
hwclock --show --rtc /dev/rtc1

2018-11-29 17:08:00.780793-08:00
Quoted 1 line

hwclock --show --rtc /dev/rtc1

2007-12-31 16:01:20.460959-08:00
Quoted 2 lines

hwclock --systohc --rtc /dev/rtc1
hwclock --show --rtc /dev/rtc1

2018-11-29 17:08:00.780793-08:00

---

Changes in v6:
- In the core, actually unregister the RTC child platform_device.

Changes in v5: None
Changes in v4:
- Change me email to @chromium.org from @google.com
- Move "Add RTC driver" before "Add sysfs attributes" so that it could get accepted earlier, since it is less contentious

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 |  22 ++-
 drivers/rtc/Kconfig                     |  11 ++
 drivers/rtc/Makefile                    |   1 +
 drivers/rtc/rtc-wilco-ec.c              | 177 ++++++++++++++++++++++++
 include/linux/platform_data/wilco-ec.h  |   2 +
 5 files changed, 211 insertions(+), 2 deletions(-)
 create mode 100644 drivers/rtc/rtc-wilco-ec.c
drivers/platform/chrome/wilco_ec/core.c +20 -2
index ae86cae216fd..e67385ec5103 100644
@@ -42,6 +42,7 @@ static int wilco_ec_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct wilco_ec_device *ec;
+	int ret;
 
 	ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
 	if (!ec)
@@ -70,21 +71,38 @@ static int wilco_ec_probe(struct platform_device *pdev)
 			     ec->io_packet->start + EC_MAILBOX_DATA_SIZE);
 
 	/*
-	 * Register a debugfs platform device that will get picked up by the
-	 * debugfs driver. Ignore failure.
+	 * Register a child device that will be found by the RTC driver.
+	 * Ignore failure.
 	 */
 	ec->debugfs_pdev = platform_device_register_data(dev,
 							 "wilco-ec-debugfs",
 							 PLATFORM_DEVID_AUTO,
 							 NULL, 0);
 
+	/* Register a child device that will be found by the RTC driver. */
+	ec->rtc_pdev = platform_device_register_data(dev, "rtc-wilco-ec",
+						     PLATFORM_DEVID_AUTO,
+						     NULL, 0);
+	if (IS_ERR(ec->rtc_pdev)) {
+		dev_err(dev, "Failed to create RTC platform device\n");
+		ret = PTR_ERR(ec->rtc_pdev);
+		goto unregister_debugfs;
+	}
+
 	return 0;
+
+unregister_debugfs:
+	if (ec->debugfs_pdev)
+		platform_device_unregister(ec->debugfs_pdev);
+	cros_ec_lpc_mec_destroy();
+	return ret;
 }
 
 static int wilco_ec_remove(struct platform_device *pdev)
 {
 	struct wilco_ec_device *ec = platform_get_drvdata(pdev);
 
+	platform_device_unregister(ec->rtc_pdev);
 	if (ec->debugfs_pdev)
 		platform_device_unregister(ec->debugfs_pdev);
 
drivers/rtc/Kconfig +11 -0
index 225b0b8516f3..d5063c791515 100644
@@ -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
drivers/rtc/Makefile +1 -0
index df022d820bee..6255ea78da25 100644
@@ -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
drivers/rtc/rtc-wilco-ec.c new file +177 -0

177 lines not on this page · raw message

include/linux/platform_data/wilco-ec.h +2 -0
index 5344975afa1a..446473a46b88 100644
@@ -35,6 +35,7 @@
  *               is used to hold the request and the response.
  * @data_size: Size of the data buffer used for EC communication.
  * @debugfs_pdev: The child platform_device used by the debugfs sub-driver.
+ * @rtc_pdev: The child platform_device used by the RTC sub-driver.
  */
 struct wilco_ec_device {
 	struct device *dev;
@@ -45,6 +46,7 @@ struct wilco_ec_device {
 	void *data_buffer;
 	size_t data_size;
 	struct platform_device *debugfs_pdev;
+	struct platform_device *rtc_pdev;
 };
 
 /**
Signature · 1 line
2.20.1.611.gfbb209baf1-goog
Acked-by Acked-by Alexandre Belloni on v6 4/4
#7 lore raw

Re: [PATCH v6 4/4] platform/chrome: rtc: Add RTC driver

Hi,

Quoted 16 lines · Nick Crews

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.

To test:

Quoted 1 line

hwclock --show --rtc /dev/rtc1

2007-12-31 16:01:20.460959-08:00
Quoted 2 lines

hwclock --systohc --rtc /dev/rtc1
hwclock --show --rtc /dev/rtc1

2018-11-29 17:08:00.780793-08:00
Quoted 8 lines
Quoted 1 line

hwclock --show --rtc /dev/rtc1

2007-12-31 16:01:20.460959-08:00
Quoted 2 lines

hwclock --systohc --rtc /dev/rtc1
hwclock --show --rtc /dev/rtc1

2018-11-29 17:08:00.780793-08:00

I think that you missed :)

Quoted 19 lines

---

Changes in v6:
- In the core, actually unregister the RTC child platform_device.

Changes in v5: None
Changes in v4:
- Change me email to @chromium.org from @google.com
- Move "Add RTC driver" before "Add sysfs attributes" so that it could get accepted earlier, since it is less contentious

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

Quoted 7 lines

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 |  22 ++-

Alexandre, this specific patch depends on the patches of the series to apply, and the first patches of the series also conflicts/depends on some patches queued in chrome-platform-5.1 branch [1]. Is it fine with you if I create an immutable branch and all go via your tree? Or what do you think? I am new here and I am not sure how to manage that.

Thanks,
Enric

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/chrome-platform/linux.git/log/?h=chrome-platform-5.1

Quoted 296 lines
 drivers/rtc/Kconfig                     |  11 ++
 drivers/rtc/Makefile                    |   1 +
 drivers/rtc/rtc-wilco-ec.c              | 177 ++++++++++++++++++++++++
 include/linux/platform_data/wilco-ec.h  |   2 +
 5 files changed, 211 insertions(+), 2 deletions(-)
 create mode 100644 drivers/rtc/rtc-wilco-ec.c
drivers/platform/chrome/wilco_ec/core.c +20 -2
index ae86cae216fd..e67385ec5103 100644
@@ -42,6 +42,7 @@ static int wilco_ec_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct wilco_ec_device *ec;
+	int ret;
 
 	ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
 	if (!ec)
@@ -70,21 +71,38 @@ static int wilco_ec_probe(struct platform_device *pdev)
 			     ec->io_packet->start + EC_MAILBOX_DATA_SIZE);
 
 	/*
-	 * Register a debugfs platform device that will get picked up by the
-	 * debugfs driver. Ignore failure.
+	 * Register a child device that will be found by the RTC driver.
+	 * Ignore failure.
 	 */
 	ec->debugfs_pdev = platform_device_register_data(dev,
 							 "wilco-ec-debugfs",
 							 PLATFORM_DEVID_AUTO,
 							 NULL, 0);
 
+	/* Register a child device that will be found by the RTC driver. */
+	ec->rtc_pdev = platform_device_register_data(dev, "rtc-wilco-ec",
+						     PLATFORM_DEVID_AUTO,
+						     NULL, 0);
+	if (IS_ERR(ec->rtc_pdev)) {
+		dev_err(dev, "Failed to create RTC platform device\n");
+		ret = PTR_ERR(ec->rtc_pdev);
+		goto unregister_debugfs;
+	}
+
 	return 0;
+
+unregister_debugfs:
+	if (ec->debugfs_pdev)
+		platform_device_unregister(ec->debugfs_pdev);
+	cros_ec_lpc_mec_destroy();
+	return ret;
 }
 
 static int wilco_ec_remove(struct platform_device *pdev)
 {
 	struct wilco_ec_device *ec = platform_get_drvdata(pdev);
 
+	platform_device_unregister(ec->rtc_pdev);
 	if (ec->debugfs_pdev)
 		platform_device_unregister(ec->debugfs_pdev);
 
drivers/rtc/Kconfig +11 -0
index 225b0b8516f3..d5063c791515 100644
@@ -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
drivers/rtc/Makefile +1 -0
index df022d820bee..6255ea78da25 100644
@@ -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
drivers/rtc/rtc-wilco-ec.c new file +177 -0
new file mode 100644
index 000000000000..35cc56114c1c
@@ -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 <linux/bcd.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/platform_data/wilco-ec.h>
+#include <linux/rtc.h>
+#include <linux/timekeeping.h>
+
+#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 = &param,
+		.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 <[email protected]>");
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Wilco EC RTC driver");
include/linux/platform_data/wilco-ec.h +2 -0
index 5344975afa1a..446473a46b88 100644
@@ -35,6 +35,7 @@
  *               is used to hold the request and the response.
  * @data_size: Size of the data buffer used for EC communication.
  * @debugfs_pdev: The child platform_device used by the debugfs sub-driver.
+ * @rtc_pdev: The child platform_device used by the RTC sub-driver.
  */
 struct wilco_ec_device {
 	struct device *dev;
@@ -45,6 +46,7 @@ struct wilco_ec_device {
 	void *data_buffer;
 	size_t data_size;
 	struct platform_device *debugfs_pdev;
+	struct platform_device *rtc_pdev;
 };
 
 /**
author
#8 lore raw

Re: [PATCH v6 4/4] platform/chrome: rtc: Add RTC driver

Hi Enric and Alexandre,

Quoted 26 lines · Enric Balletbo i Serra

Hi,

Quoted 22 lines · Nick Crews

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.

To test:

Quoted 1 line

hwclock --show --rtc /dev/rtc1

2007-12-31 16:01:20.460959-08:00
Quoted 2 lines

hwclock --systohc --rtc /dev/rtc1
hwclock --show --rtc /dev/rtc1

2018-11-29 17:08:00.780793-08:00
Quoted 1 line

hwclock --show --rtc /dev/rtc1

2007-12-31 16:01:20.460959-08:00
Quoted 2 lines

hwclock --systohc --rtc /dev/rtc1
hwclock --show --rtc /dev/rtc1

2018-11-29 17:08:00.780793-08:00
Quoted 8 lines
Quoted 2 lines

I think that you missed :)

Ah, I removed these because this version is a bit different from the one that you Acked. Do you normally keep those sign-offs even after making changes that you possibly might not like?

Quoted 27 lines
Quoted 19 lines

---

Changes in v6:
- In the core, actually unregister the RTC child platform_device.

Changes in v5: None
Changes in v4:
- Change me email to @chromium.org from @google.com
- Move "Add RTC driver" before "Add sysfs attributes" so that it could get accepted earlier, since it is less contentious

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

Quoted 7 lines

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 |  22 ++-
Quoted 8 lines

Alexandre, this specific patch depends on the patches of the series to apply, and the first patches of the series also conflicts/depends on some patches queued in chrome-platform-5.1 branch [1]. Is it fine with you if I create an immutable branch and all go via your tree? Or what do you think? I am new here and I am not sure how to manage that.

Thanks,
Enric

Let me know if I can help with this. I based this off what I thought was the most current master, SHA 74e96711e3379fc66630f2a1d184947f80cf2c48. Was this not the correct commit to base these off of?

Quoted 300 lines

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/chrome-platform/linux.git/log/?h=chrome-platform-5.1

Quoted 296 lines
 drivers/rtc/Kconfig                     |  11 ++
 drivers/rtc/Makefile                    |   1 +
 drivers/rtc/rtc-wilco-ec.c              | 177 ++++++++++++++++++++++++
 include/linux/platform_data/wilco-ec.h  |   2 +
 5 files changed, 211 insertions(+), 2 deletions(-)
 create mode 100644 drivers/rtc/rtc-wilco-ec.c
drivers/platform/chrome/wilco_ec/core.c +20 -2
index ae86cae216fd..e67385ec5103 100644
@@ -42,6 +42,7 @@ static int wilco_ec_probe(struct platform_device *pdev)
 {
      struct device *dev = &pdev->dev;
      struct wilco_ec_device *ec;
+     int ret;
 
      ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
      if (!ec)
@@ -70,21 +71,38 @@ static int wilco_ec_probe(struct platform_device *pdev)
                           ec->io_packet->start + EC_MAILBOX_DATA_SIZE);
 
      /*
-      * Register a debugfs platform device that will get picked up by the
-      * debugfs driver. Ignore failure.
+      * Register a child device that will be found by the RTC driver.
+      * Ignore failure.
       */
      ec->debugfs_pdev = platform_device_register_data(dev,
                                                       "wilco-ec-debugfs",
                                                       PLATFORM_DEVID_AUTO,
                                                       NULL, 0);
 
+     /* Register a child device that will be found by the RTC driver. */
+     ec->rtc_pdev = platform_device_register_data(dev, "rtc-wilco-ec",
+                                                  PLATFORM_DEVID_AUTO,
+                                                  NULL, 0);
+     if (IS_ERR(ec->rtc_pdev)) {
+             dev_err(dev, "Failed to create RTC platform device\n");
+             ret = PTR_ERR(ec->rtc_pdev);
+             goto unregister_debugfs;
+     }
+
      return 0;
+
+unregister_debugfs:
+     if (ec->debugfs_pdev)
+             platform_device_unregister(ec->debugfs_pdev);
+     cros_ec_lpc_mec_destroy();
+     return ret;
 }
 
 static int wilco_ec_remove(struct platform_device *pdev)
 {
      struct wilco_ec_device *ec = platform_get_drvdata(pdev);
 
+     platform_device_unregister(ec->rtc_pdev);
      if (ec->debugfs_pdev)
              platform_device_unregister(ec->debugfs_pdev);
 
drivers/rtc/Kconfig +11 -0
index 225b0b8516f3..d5063c791515 100644
@@ -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
drivers/rtc/Makefile +1 -0
index df022d820bee..6255ea78da25 100644
@@ -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
drivers/rtc/rtc-wilco-ec.c new file +177 -0
new file mode 100644
index 000000000000..35cc56114c1c
@@ -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 <linux/bcd.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/platform_data/wilco-ec.h>
+#include <linux/rtc.h>
+#include <linux/timekeeping.h>
+
+#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 = &param,
+             .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 <[email protected]>");
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Wilco EC RTC driver");
include/linux/platform_data/wilco-ec.h +2 -0
index 5344975afa1a..446473a46b88 100644
@@ -35,6 +35,7 @@
  *               is used to hold the request and the response.
  * @data_size: Size of the data buffer used for EC communication.
  * @debugfs_pdev: The child platform_device used by the debugfs sub-driver.
+ * @rtc_pdev: The child platform_device used by the RTC sub-driver.
  */
 struct wilco_ec_device {
      struct device *dev;
@@ -45,6 +46,7 @@ struct wilco_ec_device {
      void *data_buffer;
      size_t data_size;
      struct platform_device *debugfs_pdev;
+     struct platform_device *rtc_pdev;
 };
 
 /**
#9 lore raw

Re: [PATCH v6 4/4] platform/chrome: rtc: Add RTC driver

Quoted 35 lines · Nick Crews

Hi Enric and Alexandre,

Quoted 31 lines · Enric Balletbo i Serra

Hi,

Quoted 16 lines · Nick Crews

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.

To test:

Quoted 1 line

hwclock --show --rtc /dev/rtc1

2007-12-31 16:01:20.460959-08:00
Quoted 2 lines

hwclock --systohc --rtc /dev/rtc1
hwclock --show --rtc /dev/rtc1

2018-11-29 17:08:00.780793-08:00
Quoted 8 lines
Quoted 1 line

hwclock --show --rtc /dev/rtc1

2007-12-31 16:01:20.460959-08:00
Quoted 2 lines

hwclock --systohc --rtc /dev/rtc1
hwclock --show --rtc /dev/rtc1

2018-11-29 17:08:00.780793-08:00

I think that you missed :)

Quoted 8 lines
Quoted 3 lines

Ah, I removed these because this version is a bit different from the one that you Acked. Do you normally keep those sign-offs even after making changes that you possibly might not like?

I would think you could have kept mine.

Quoted 340 lines
Quoted 27 lines
Quoted 19 lines

---

Changes in v6:
- In the core, actually unregister the RTC child platform_device.

Changes in v5: None
Changes in v4:
- Change me email to @chromium.org from @google.com
- Move "Add RTC driver" before "Add sysfs attributes" so that it could get accepted earlier, since it is less contentious

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

Quoted 7 lines

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 |  22 ++-
Quoted 8 lines

Alexandre, this specific patch depends on the patches of the series to apply, and the first patches of the series also conflicts/depends on some patches queued in chrome-platform-5.1 branch [1]. Is it fine with you if I create an immutable branch and all go via your tree? Or what do you think? I am new here and I am not sure how to manage that.

Thanks,
Enric

Let me know if I can help with this. I based this off what I thought was the most current master, SHA 74e96711e3379fc66630f2a1d184947f80cf2c48. Was this not the correct commit to base these off of?

Quoted 300 lines

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/chrome-platform/linux.git/log/?h=chrome-platform-5.1

Quoted 296 lines
 drivers/rtc/Kconfig                     |  11 ++
 drivers/rtc/Makefile                    |   1 +
 drivers/rtc/rtc-wilco-ec.c              | 177 ++++++++++++++++++++++++
 include/linux/platform_data/wilco-ec.h  |   2 +
 5 files changed, 211 insertions(+), 2 deletions(-)
 create mode 100644 drivers/rtc/rtc-wilco-ec.c
drivers/platform/chrome/wilco_ec/core.c +20 -2
index ae86cae216fd..e67385ec5103 100644
@@ -42,6 +42,7 @@ static int wilco_ec_probe(struct platform_device *pdev)
 {
      struct device *dev = &pdev->dev;
      struct wilco_ec_device *ec;
+     int ret;
 
      ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
      if (!ec)
@@ -70,21 +71,38 @@ static int wilco_ec_probe(struct platform_device *pdev)
                           ec->io_packet->start + EC_MAILBOX_DATA_SIZE);
 
      /*
-      * Register a debugfs platform device that will get picked up by the
-      * debugfs driver. Ignore failure.
+      * Register a child device that will be found by the RTC driver.
+      * Ignore failure.
       */
      ec->debugfs_pdev = platform_device_register_data(dev,
                                                       "wilco-ec-debugfs",
                                                       PLATFORM_DEVID_AUTO,
                                                       NULL, 0);
 
+     /* Register a child device that will be found by the RTC driver. */
+     ec->rtc_pdev = platform_device_register_data(dev, "rtc-wilco-ec",
+                                                  PLATFORM_DEVID_AUTO,
+                                                  NULL, 0);
+     if (IS_ERR(ec->rtc_pdev)) {
+             dev_err(dev, "Failed to create RTC platform device\n");
+             ret = PTR_ERR(ec->rtc_pdev);
+             goto unregister_debugfs;
+     }
+
      return 0;
+
+unregister_debugfs:
+     if (ec->debugfs_pdev)
+             platform_device_unregister(ec->debugfs_pdev);
+     cros_ec_lpc_mec_destroy();
+     return ret;
 }
 
 static int wilco_ec_remove(struct platform_device *pdev)
 {
      struct wilco_ec_device *ec = platform_get_drvdata(pdev);
 
+     platform_device_unregister(ec->rtc_pdev);
      if (ec->debugfs_pdev)
              platform_device_unregister(ec->debugfs_pdev);
 
drivers/rtc/Kconfig +11 -0
index 225b0b8516f3..d5063c791515 100644
@@ -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
drivers/rtc/Makefile +1 -0
index df022d820bee..6255ea78da25 100644
@@ -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
drivers/rtc/rtc-wilco-ec.c new file +177 -0
new file mode 100644
index 000000000000..35cc56114c1c
@@ -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 <linux/bcd.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/platform_data/wilco-ec.h>
+#include <linux/rtc.h>
+#include <linux/timekeeping.h>
+
+#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 = &param,
+             .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 <[email protected]>");
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Wilco EC RTC driver");
include/linux/platform_data/wilco-ec.h +2 -0
index 5344975afa1a..446473a46b88 100644
@@ -35,6 +35,7 @@
  *               is used to hold the request and the response.
  * @data_size: Size of the data buffer used for EC communication.
  * @debugfs_pdev: The child platform_device used by the debugfs sub-driver.
+ * @rtc_pdev: The child platform_device used by the RTC sub-driver.
  */
 struct wilco_ec_device {
      struct device *dev;
@@ -45,6 +46,7 @@ struct wilco_ec_device {
      void *data_buffer;
      size_t data_size;
      struct platform_device *debugfs_pdev;
+     struct platform_device *rtc_pdev;
 };
 
 /**
Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#10 lore raw

Re: [PATCH v6 4/4] platform/chrome: rtc: Add RTC driver

In reply to Nick Crews

Hi Nick,
Missatge de Nick Crews <[email protected]> del dia dv., 8 de febr. 2019 a les 17:10:

Quoted 36 lines

Hi Enric and Alexandre,

Quoted 31 lines · Enric Balletbo i Serra

Hi,

Quoted 16 lines · Nick Crews

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.

To test:

Quoted 1 line

hwclock --show --rtc /dev/rtc1

2007-12-31 16:01:20.460959-08:00
Quoted 2 lines

hwclock --systohc --rtc /dev/rtc1
hwclock --show --rtc /dev/rtc1

2018-11-29 17:08:00.780793-08:00
Quoted 8 lines
Quoted 1 line

hwclock --show --rtc /dev/rtc1

2007-12-31 16:01:20.460959-08:00
Quoted 2 lines

hwclock --systohc --rtc /dev/rtc1
hwclock --show --rtc /dev/rtc1

2018-11-29 17:08:00.780793-08:00

I think that you missed :)

Quoted 8 lines
Quoted 3 lines

Ah, I removed these because this version is a bit different from the one that you Acked. Do you normally keep those sign-offs even after making changes that you possibly might not like?

Well, it's right that you did a small change in the core, so to be strict maybe you have reason, but I don't see as a big change so I expected you maintain my tag. Anyway it's not a problem at all, just that at least I expected the Alexandre ack :)

[snip]

Quoted 7 lines
Quoted 7 lines

Alexandre, this specific patch depends on the patches of the series to apply, and the first patches of the series also conflicts/depends on some patches queued in chrome-platform-5.1 branch [1]. Is it fine with you if I create an immutable branch and all go via your tree? Or what do you think? I am new here and I am not sure how to manage that.

Quoted 7 lines
Quoted 2 lines

Thanks,
Enric

Let me know if I can help with this. I based this off what I thought was the most current master, SHA 74e96711e3379fc66630f2a1d184947f80cf2c48. Was this not the correct commit to base these off of?

Yeah, it's fine, just that I queued recently some changes and I have trivial conflicts in Kconfig and Makefile, nothing that you need to worry.

[snip]

#11 lore raw

Re: [PATCH v6 4/4] platform/chrome: rtc: Add RTC driver

In reply to Enric Balletbo i Serra
Quoted 23 lines · Enric Balletbo i Serra
Quoted 23 lines

Changes in v6:
- In the core, actually unregister the RTC child platform_device.

Changes in v5: None
Changes in v4:
- Change me email to @chromium.org from @google.com
- Move "Add RTC driver" before "Add sysfs attributes" so that it could get accepted earlier, since it is less contentious

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

Quoted 8 lines
Quoted 1 line
 drivers/platform/chrome/wilco_ec/core.c |  22 ++-

Alexandre, this specific patch depends on the patches of the series to apply, and the first patches of the series also conflicts/depends on some patches queued in chrome-platform-5.1 branch [1]. Is it fine with you if I create an immutable branch and all go via your tree? Or what do you think? I am new here and I am not sure how to manage that.

The series should probably go through the chrome tree. The only possible conflict would be in drivers/rtc/Makefile and it should be easy enough for Linus to solve.

Quoted 302 lines

Thanks,
Enric

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/chrome-platform/linux.git/log/?h=chrome-platform-5.1

Quoted 296 lines
 drivers/rtc/Kconfig                     |  11 ++
 drivers/rtc/Makefile                    |   1 +
 drivers/rtc/rtc-wilco-ec.c              | 177 ++++++++++++++++++++++++
 include/linux/platform_data/wilco-ec.h  |   2 +
 5 files changed, 211 insertions(+), 2 deletions(-)
 create mode 100644 drivers/rtc/rtc-wilco-ec.c
drivers/platform/chrome/wilco_ec/core.c +20 -2
index ae86cae216fd..e67385ec5103 100644
@@ -42,6 +42,7 @@ static int wilco_ec_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct wilco_ec_device *ec;
+	int ret;
 
 	ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
 	if (!ec)
@@ -70,21 +71,38 @@ static int wilco_ec_probe(struct platform_device *pdev)
 			     ec->io_packet->start + EC_MAILBOX_DATA_SIZE);
 
 	/*
-	 * Register a debugfs platform device that will get picked up by the
-	 * debugfs driver. Ignore failure.
+	 * Register a child device that will be found by the RTC driver.
+	 * Ignore failure.
 	 */
 	ec->debugfs_pdev = platform_device_register_data(dev,
 							 "wilco-ec-debugfs",
 							 PLATFORM_DEVID_AUTO,
 							 NULL, 0);
 
+	/* Register a child device that will be found by the RTC driver. */
+	ec->rtc_pdev = platform_device_register_data(dev, "rtc-wilco-ec",
+						     PLATFORM_DEVID_AUTO,
+						     NULL, 0);
+	if (IS_ERR(ec->rtc_pdev)) {
+		dev_err(dev, "Failed to create RTC platform device\n");
+		ret = PTR_ERR(ec->rtc_pdev);
+		goto unregister_debugfs;
+	}
+
 	return 0;
+
+unregister_debugfs:
+	if (ec->debugfs_pdev)
+		platform_device_unregister(ec->debugfs_pdev);
+	cros_ec_lpc_mec_destroy();
+	return ret;
 }
 
 static int wilco_ec_remove(struct platform_device *pdev)
 {
 	struct wilco_ec_device *ec = platform_get_drvdata(pdev);
 
+	platform_device_unregister(ec->rtc_pdev);
 	if (ec->debugfs_pdev)
 		platform_device_unregister(ec->debugfs_pdev);
 
drivers/rtc/Kconfig +11 -0
index 225b0b8516f3..d5063c791515 100644
@@ -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
drivers/rtc/Makefile +1 -0
index df022d820bee..6255ea78da25 100644
@@ -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
drivers/rtc/rtc-wilco-ec.c new file +177 -0
new file mode 100644
index 000000000000..35cc56114c1c
@@ -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 <linux/bcd.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/platform_data/wilco-ec.h>
+#include <linux/rtc.h>
+#include <linux/timekeeping.h>
+
+#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 = &param,
+		.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 <[email protected]>");
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Wilco EC RTC driver");
include/linux/platform_data/wilco-ec.h +2 -0
index 5344975afa1a..446473a46b88 100644
@@ -35,6 +35,7 @@
  *               is used to hold the request and the response.
  * @data_size: Size of the data buffer used for EC communication.
  * @debugfs_pdev: The child platform_device used by the debugfs sub-driver.
+ * @rtc_pdev: The child platform_device used by the RTC sub-driver.
  */
 struct wilco_ec_device {
 	struct device *dev;
@@ -45,6 +46,7 @@ struct wilco_ec_device {
 	void *data_buffer;
 	size_t data_size;
 	struct platform_device *debugfs_pdev;
+	struct platform_device *rtc_pdev;
 };
 
 /**
Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#12 lore raw

Re: [PATCH v6 4/4] platform/chrome: rtc: Add RTC driver

Hi Alexandre,

[snip]

Quoted 5 lines

The series should probably go through the chrome tree. The only possible conflict would be in drivers/rtc/Makefile and it should be easy enough for Linus to solve.

Perfect, I will pick also the RTC patch then.

[snip]