Skip to contentSkip to conversation

mfd: Add support for Khadas Microcontroller

PATCH Superseded Neil Armstrong 4 participants 18 messages May 12, 2020 – Jun 2, 2020

Subsystems (6)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover mfd: Add support for Khadas Microcontroller

The new Khadas VIM2, VIM3 and Edge boards embeds an on-board microcontroller connected via I2C.

This Microcontroller is present on the Khadas VIM1, VIM2, VIM3 and Edge boards.

It has multiple boot control features like password check, power-on options, power-off control and system FAN control on recent boards.

Thie serie adds :
- the bindings
- the MFD driver
- the HWMON cell driver
- the NVMEM cell driver
- updates MAINTAINERS
- add support into the Khadas VIM3/VIM3L DT

Patch 6 depends on [1].

Changes since RFC v1 at [2]:
- moved hwmon driver to thermal-only
- moved the SM1 thermal nodes in a separate serie
- added the bindings review tag from rob

[1] http://lore.kernel.org/r/[email protected]
[2] http://lore.kernel.org/r/[email protected]

Neil Armstrong (6):
dt-bindings: mfd: add Khadas Microcontroller bindings
mfd: add support for the Khadas System control Microcontroller
thermal: add support for the MCU controlled FAN on Khadas boards
nvmem: add support for the Khadas MCU Programmable User Memory
MAINTAINERS: add myself as maintainer for Khadas MCU drivers
arm64: dts: meson-khadas-vim3: add Khadas MCU nodes

 .../devicetree/bindings/mfd/khadas,mcu.yaml   |  44 +++++
 MAINTAINERS                                   |  10 +
 .../boot/dts/amlogic/meson-khadas-vim3.dtsi   |  23 +++
 drivers/mfd/Kconfig                           |  14 ++
 drivers/mfd/Makefile                          |   1 +
 drivers/mfd/khadas-mcu.c                      | 143 ++++++++++++++
 drivers/nvmem/Kconfig                         |   8 +
 drivers/nvmem/Makefile                        |   2 +
 drivers/nvmem/khadas-mcu-user-mem.c           | 128 +++++++++++++
 drivers/thermal/Kconfig                       |  10 +
 drivers/thermal/Makefile                      |   1 +
 drivers/thermal/khadas_mcu_fan.c              | 174 ++++++++++++++++++
 include/linux/mfd/khadas-mcu.h                |  91 +++++++++
 13 files changed, 649 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mfd/khadas,mcu.yaml
 create mode 100644 drivers/mfd/khadas-mcu.c
 create mode 100644 drivers/nvmem/khadas-mcu-user-mem.c
 create mode 100644 drivers/thermal/khadas_mcu_fan.c
 create mode 100644 include/linux/mfd/khadas-mcu.h
Signature · 1 line
2.22.0
#7 lore raw

PATCH v2 3/6 thermal: add support for the MCU controlled FAN on Khadas boards

The new Khadas VIM2 and VIM3 boards controls the cooling fan via the on-board microcontroller.

This implements the FAN control as thermal devices and as cell of the Khadas MCU MFD driver.

---
 drivers/thermal/Kconfig          |  10 ++
 drivers/thermal/Makefile         |   1 +
 drivers/thermal/khadas_mcu_fan.c | 174 +++++++++++++++++++++++++++++++
 3 files changed, 185 insertions(+)
 create mode 100644 drivers/thermal/khadas_mcu_fan.c
drivers/thermal/Kconfig +10 -0
index 91af271e9bb0..72b3960cc5ac 100644
@@ -490,4 +490,14 @@ config SPRD_THERMAL
 	help
 	  Support for the Spreadtrum thermal sensor driver in the Linux thermal
 	  framework.
+
+config KHADAS_MCU_FAN_THERMAL
+	tristate "Khadas MCU controller FAN cooling support"
+	depends on OF || COMPILE_TEST
+	select MFD_CORE
+	select REGMAP
+	help
+	  If you say yes here you get support for the FAN controlled
+	  by the Microcontroller found on the Khadas VIM boards.
+
 endif
drivers/thermal/Makefile +1 -0
index 8c8ed7b79915..460428c2122c 100644
@@ -60,3 +60,4 @@ obj-$(CONFIG_ZX2967_THERMAL)	+= zx2967_thermal.o
 obj-$(CONFIG_UNIPHIER_THERMAL)	+= uniphier_thermal.o
 obj-$(CONFIG_AMLOGIC_THERMAL)     += amlogic_thermal.o
 obj-$(CONFIG_SPRD_THERMAL)	+= sprd_thermal.o
+obj-$(CONFIG_KHADAS_MCU_FAN_THERMAL)	+= khadas_mcu_fan.o
drivers/thermal/khadas_mcu_fan.c new file +174 -0

174 lines not on this page · raw message

Signature · 1 line
2.22.0
#1 lore raw

PATCH v2 1/6 dt-bindings: mfd: add Khadas Microcontroller bindings

In reply to Neil Armstrong

This Microcontroller is present on the Khadas VIM1, VIM2, VIM3 and Edge boards.

It has multiple boot control features like password check, power-on options, power-off control and system FAN control on recent boards.

---
 .../devicetree/bindings/mfd/khadas,mcu.yaml   | 44 +++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mfd/khadas,mcu.yaml
Documentation/devicetree/bindings/mfd/khadas,mcu.yaml new file +44 -0
new file mode 100644
index 000000000000..a3b976f101e8
@@ -0,0 +1,44 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mfd/khadas,mcu.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Khadas on-board Microcontroller Device Tree Bindings
+
+maintainers:
+  - Neil Armstrong <[email protected]>
+
+description: |
+  Khadas embeds a microcontroller on their VIM and Edge boards adding some
+  system feature as PWM Fan control (for VIM2 rev14 or VIM3), User memory
+  storage, IR/Key resume control, system power LED control and more.
+
+properties:
+  compatible:
+    enum:
+      - khadas,mcu # MCU revision is discoverable
+
+  "#cooling-cells": # Only needed for boards having FAN control feature
+    const: 2
+
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+      #address-cells = <1>;
+      #size-cells = <0>;
+      khadas_mcu: system-controller@18 {
+        compatible = "khadas,mcu";
+        reg = <0x18>;
+        #cooling-cells = <2>;
+      };
+    };
Signature · 1 line
2.22.0
#3 lore raw

PATCH v2 2/6 mfd: add support for the Khadas System control Microcontroller

In reply to Neil Armstrong

This Microcontroller is present on the Khadas VIM1, VIM2, VIM3 and Edge boards.

It has multiple boot control features like password check, power-on options, power-off control and system FAN control on recent boards.

This implements a very basic MFD driver with the fan control and User NVMEM cells.

---
 drivers/mfd/Kconfig            |  14 ++++
 drivers/mfd/Makefile           |   1 +
 drivers/mfd/khadas-mcu.c       | 143 +++++++++++++++++++++++++++++++++
 include/linux/mfd/khadas-mcu.h |  91 +++++++++++++++++++++
 4 files changed, 249 insertions(+)
 create mode 100644 drivers/mfd/khadas-mcu.c
 create mode 100644 include/linux/mfd/khadas-mcu.h
drivers/mfd/Kconfig +14 -0
index 0a59249198d3..b95091397052 100644
@@ -2003,6 +2003,20 @@ config MFD_WCD934X
 	  This driver provides common support WCD934x audio codec and its
 	  associated Pin Controller, Soundwire Controller and Audio codec.
 
+config MFD_KHADAS_MCU
+	tristate "Support for Khadas System control Microcontroller"
+	depends on I2C
+	depends on OF || COMPILE_TEST
+	select MFD_CORE
+	select REGMAP_I2C
+	help
+	  Support for the Khadas System control Microcontroller interface present
+	  on their VIM and Edge boards.
+
+	  This driver provides common support for accessing the device,
+	  additional drivers must be enabled in order to use the functionality
+	  of the device.
+
 menu "Multimedia Capabilities Port drivers"
 	depends on ARCH_SA1100
 
drivers/mfd/Makefile +1 -0
index f935d10cbf0f..0f1633b096bb 100644
@@ -257,5 +257,6 @@ obj-$(CONFIG_MFD_ROHM_BD70528)	+= rohm-bd70528.o
 obj-$(CONFIG_MFD_ROHM_BD71828)	+= rohm-bd71828.o
 obj-$(CONFIG_MFD_ROHM_BD718XX)	+= rohm-bd718x7.o
 obj-$(CONFIG_MFD_STMFX) 	+= stmfx.o
+obj-$(CONFIG_MFD_KHADAS_MCU) 	+= khadas-mcu.o
 
 obj-$(CONFIG_SGI_MFD_IOC3)	+= ioc3.o
drivers/mfd/khadas-mcu.c new file +143 -0

143 lines not on this page · raw message

include/linux/mfd/khadas-mcu.h new file +91 -0

91 lines not on this page · raw message

Signature · 1 line
2.22.0
#11 lore raw

PATCH v2 4/6 nvmem: add support for the Khadas MCU Programmable User Memory

In reply to Neil Armstrong

The new Khadas VIM2, VIM3 and Edge boards embeds an on-board microcontroller offering a 56bytes User Programmable NVMEM array.

This array needs a password to be writable, thus a password sysfs file has been added on the device node to unlock the NVMEM.

The default 6bytes password id: "Khadas"

This implements the user NVMEM devices as cell of the Khadas MCU MFD driver.

---
 drivers/nvmem/Kconfig               |   8 ++
 drivers/nvmem/Makefile              |   2 +
 drivers/nvmem/khadas-mcu-user-mem.c | 128 ++++++++++++++++++++++++++++
 3 files changed, 138 insertions(+)
 create mode 100644 drivers/nvmem/khadas-mcu-user-mem.c
drivers/nvmem/Kconfig +8 -0
index d7b7f6d688e7..92cd4f6aa931 100644
@@ -67,6 +67,14 @@ config JZ4780_EFUSE
 	  To compile this driver as a module, choose M here: the module
 	  will be called nvmem_jz4780_efuse.
 
+config NVMEM_KHADAS_MCU_USER_MEM
+	tristate "Khadas MCU User programmable memory support"
+	depends on MFD_KHADAS_MCU
+	depends on REGMAP
+	help
+	  This is a driver for the MCU User programmable memory
+	  available on the Khadas VIM and Edge boards.
+
 config NVMEM_LPC18XX_EEPROM
 	tristate "NXP LPC18XX EEPROM Memory Support"
 	depends on ARCH_LPC18XX || COMPILE_TEST
drivers/nvmem/Makefile +2 -0
index a7c377218341..0516a309542d 100644
@@ -17,6 +17,8 @@ obj-$(CONFIG_NVMEM_IMX_OCOTP_SCU)	+= nvmem-imx-ocotp-scu.o
 nvmem-imx-ocotp-scu-y		:= imx-ocotp-scu.o
 obj-$(CONFIG_JZ4780_EFUSE)		+= nvmem_jz4780_efuse.o
 nvmem_jz4780_efuse-y		:= jz4780-efuse.o
+obj-$(CONFIG_NVMEM_KHADAS_MCU_USER_MEM)	+= nvmem-khadas-mcu-user-mem.o
+nvmem-khadas-mcu-user-mem-y	:= khadas-mcu-user-mem.o
 obj-$(CONFIG_NVMEM_LPC18XX_EEPROM)	+= nvmem_lpc18xx_eeprom.o
 nvmem_lpc18xx_eeprom-y	:= lpc18xx_eeprom.o
 obj-$(CONFIG_NVMEM_LPC18XX_OTP)	+= nvmem_lpc18xx_otp.o
drivers/nvmem/khadas-mcu-user-mem.c new file +128 -0

128 lines not on this page · raw message

Signature · 1 line
2.22.0
#17 lore raw

PATCH v2 6/6 arm64: dts: meson-khadas-vim3: add Khadas MCU nodes

In reply to Neil Armstrong

Add the Khadas MCU node with active FAN thermal nodes for all the Khadas VIM3 variants.

---
 .../boot/dts/amlogic/meson-khadas-vim3.dtsi   | 23 +++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/arm64/boot/dts/amlogic/meson-khadas-vim3.dtsi +23 -0
index 094ecf2222bb..3325e54ea690 100644
@@ -183,6 +183,23 @@
 	hdmi-phandle = <&hdmi_tx>;
 };
 
+&cpu_thermal {
+	trips {
+		cpu_active: cpu-active {
+			temperature = <80000>; /* millicelsius */
+			hysteresis = <2000>; /* millicelsius */
+			type = "active";
+		};
+	};
+
+	cooling-maps {
+		map {
+			trip = <&cpu_active>;
+			cooling-device = <&khadas_mcu THERMAL_NO_LIMIT THERMAL_NO_LIMIT>;
+		};
+	};
+};
+
 &ext_mdio {
 	external_phy: ethernet-phy@0 {
 		/* Realtek RTL8211F (0x001cc916) */
@@ -222,6 +239,12 @@
 	pinctrl-0 = <&i2c_ao_sck_pins>, <&i2c_ao_sda_pins>;
 	pinctrl-names = "default";
 
+	khadas_mcu: system-controller@18 {
+		compatible = "khadas,mcu";
+		reg = <0x18>;
+		#cooling-cells = <2>;
+	};
+
 	gpio_expander: gpio-controller@20 {
 		compatible = "ti,tca6408";
 		reg = <0x20>;
Signature · 1 line
2.22.0
#16 lore raw

PATCH v2 5/6 MAINTAINERS: add myself as maintainer for Khadas MCU drivers

In reply to Neil Armstrong

Add the HWMON and NVMEM drivers along the MFD drivers and header as Maintained by myself.

---
 MAINTAINERS | 10 ++++++++++
 1 file changed, 10 insertions(+)
MAINTAINERS +10 -0
index b816a453b10e..609baa78d810 100644
@@ -9431,6 +9431,16 @@ F:	include/linux/kdb.h
 F:	include/linux/kgdb.h
 F:	kernel/debug/
 
+KHADAS MCU MFD DRIVER
+M:	Neil Armstrong <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/mfd/khadas,mcu.yaml
+F:	drivers/mfd/khadas-mcu.c
+F:	include/linux/mfd/khadas-mcu.h
+F:	drivers/hwmon/khadas-mcu-fan.c
+F:	drivers/nvmem/khadas-mcu-user-mem.c
+
 KMEMLEAK
 M:	Catalin Marinas <[email protected]>
 S:	Maintained
Signature · 1 line
2.22.0
#12 lore raw

Re: [PATCH v2 4/6] nvmem: add support for the Khadas MCU Programmable User Memory

In reply to Neil Armstrong
Quoted 5 lines · Neil Armstrong

The new Khadas VIM2, VIM3 and Edge boards embeds an on-board microcontroller offering a 56bytes User Programmable NVMEM array.

This array needs a password to be writable, thus a password sysfs file has been added on the device node to unlock the NVMEM.

Is this one time programmable? Or does it need to be unlocked for every read/write?

How can you make sure that the memory is unlocked before making attempt to read/write?

Quoted 51 lines

The default 6bytes password id: "Khadas"

This implements the user NVMEM devices as cell of the Khadas MCU MFD driver.

---
  drivers/nvmem/Kconfig               |   8 ++
  drivers/nvmem/Makefile              |   2 +
  drivers/nvmem/khadas-mcu-user-mem.c | 128 ++++++++++++++++++++++++++++
  3 files changed, 138 insertions(+)

create mode 100644 drivers/nvmem/khadas-mcu-user-mem.c

drivers/nvmem/Kconfig +8 -0
index d7b7f6d688e7..92cd4f6aa931 100644
@@ -67,6 +67,14 @@ config JZ4780_EFUSE
  	  To compile this driver as a module, choose M here: the module
  	  will be called nvmem_jz4780_efuse.
 
+config NVMEM_KHADAS_MCU_USER_MEM
+	tristate "Khadas MCU User programmable memory support"
+	depends on MFD_KHADAS_MCU
+	depends on REGMAP
+	help
+	  This is a driver for the MCU User programmable memory
+	  available on the Khadas VIM and Edge boards.
+
  config NVMEM_LPC18XX_EEPROM
  	tristate "NXP LPC18XX EEPROM Memory Support"
  	depends on ARCH_LPC18XX || COMPILE_TEST
drivers/nvmem/Makefile +2 -0
index a7c377218341..0516a309542d 100644
@@ -17,6 +17,8 @@ obj-$(CONFIG_NVMEM_IMX_OCOTP_SCU)	+= nvmem-imx-ocotp-scu.o
  nvmem-imx-ocotp-scu-y		:= imx-ocotp-scu.o
  obj-$(CONFIG_JZ4780_EFUSE)		+= nvmem_jz4780_efuse.o
  nvmem_jz4780_efuse-y		:= jz4780-efuse.o
+obj-$(CONFIG_NVMEM_KHADAS_MCU_USER_MEM)	+= nvmem-khadas-mcu-user-mem.o
+nvmem-khadas-mcu-user-mem-y	:= khadas-mcu-user-mem.o
  obj-$(CONFIG_NVMEM_LPC18XX_EEPROM)	+= nvmem_lpc18xx_eeprom.o
  nvmem_lpc18xx_eeprom-y	:= lpc18xx_eeprom.o
  obj-$(CONFIG_NVMEM_LPC18XX_OTP)	+= nvmem_lpc18xx_otp.o
drivers/nvmem/khadas-mcu-user-mem.c new file +0 -0
new file mode 100644
index 000000000000..a1d5ae9a030c

drivers/nvmem/khadas-mcu-user-mem.c line 9

Quoted 9 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +9 -0
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Khadas MCU User programmable Memory
+ *
+ * Copyright (C) 2020 BayLibre SAS
+ * Author(s): Neil Armstrong <[email protected]>
+ */
+
+#include <linux/clk.h>

Why do we need this header?

Quoted 25 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +25 -0
+#include <linux/module.h>
+#include <linux/nvmem-provider.h>
+#include <linux/mfd/khadas-mcu.h>
+#include <linux/regmap.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+
+static int khadas_mcu_user_mem_read(void *context, unsigned int offset,
+			    void *val, size_t bytes)
+{
+	struct khadas_mcu *khadas_mcu = context;
+
+	return regmap_bulk_read(khadas_mcu->map,
+				KHADAS_MCU_USER_DATA_0_REG + offset,
+				val, bytes);
+}
+
+static int khadas_mcu_user_mem_write(void *context, unsigned int offset,
+			     void *val, size_t bytes)
+{
+	struct khadas_mcu *khadas_mcu = context;
+
+	return regmap_bulk_write(khadas_mcu->map,
+				KHADAS_MCU_USER_DATA_0_REG + offset,
+				val, bytes);

drivers/nvmem/khadas-mcu-user-mem.c line 44

Quoted 10 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +10 -0
+}
+
+static ssize_t password_store(struct device *dev, struct device_attribute *attr,
+			     const char *buf, size_t count)
+{
+	struct khadas_mcu *khadas_mcu = dev_get_drvdata(dev);
+	int i, ret;
+
+	if (count < 6)
+		return -EINVAL;

Possibly this magic 6 value needs proper define. An additional check here for > 6 would be better as well.

Quoted 28 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +28 -0
+
+	ret = regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 1);
+	if (ret)
+		return ret;
+
+	for (i = 0 ; i < 6 ; ++i) {
+		ret = regmap_write(khadas_mcu->map,
+				   KHADAS_MCU_CHECK_USER_PASSWD_REG,
+				   buf[i]);
+		if (ret)
+			goto out;
+	}
+
+	ret = regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 0);
+	if (ret)
+		return ret;
+
+	return count;
+out:
+	regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 0);
+
+	return ret;
+}
+
+static DEVICE_ATTR_WO(password);
+
+static struct attribute *khadas_mcu_user_mem_sysfs_attributes[] = {
+	&dev_attr_password.attr,

drivers/nvmem/khadas-mcu-user-mem.c line 82

Quoted 10 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +10 -0
+	NULL,
+};
+
+static const struct attribute_group khadas_mcu_user_mem_sysfs_attr_group = {
+	.attrs = khadas_mcu_user_mem_sysfs_attributes,
+};
+
+static int khadas_mcu_user_mem_probe(struct platform_device *pdev)
+{
+	struct khadas_mcu *khadas_mcu = dev_get_drvdata(pdev->dev.parent);

You could probably get away with dependency of this structure and the header itself by directly getting hold of regmap from parent device.

Quoted 5 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +5 -0
+	struct device *dev = &pdev->dev;
+	struct nvmem_device *nvmem;
+	struct nvmem_config *econfig;
+
+	econfig = devm_kzalloc(dev, sizeof(*econfig), GFP_KERNEL);

drivers/nvmem/khadas-mcu-user-mem.c line 97

Quoted 10 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +10 -0
+	if (!econfig)
+		return -ENOMEM;
+
+	econfig->dev = pdev->dev.parent;
+	econfig->name = dev_name(pdev->dev.parent);
+	econfig->stride = 1;
+	econfig->word_size = 1;
+	econfig->reg_read = khadas_mcu_user_mem_read;
+	econfig->reg_write = khadas_mcu_user_mem_write;
+	econfig->size = 56;

define 56 to make it more readable!

Quoted 32 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +31 -0
+	econfig->priv = khadas_mcu;
+
+	platform_set_drvdata(pdev, khadas_mcu);
+
+	nvmem = devm_nvmem_register(&pdev->dev, econfig);
+	if (IS_ERR(nvmem))
+		return PTR_ERR(nvmem);
+
+	return sysfs_create_group(&pdev->dev.kobj,
+				  &khadas_mcu_user_mem_sysfs_attr_group);
+}
+
+static const struct platform_device_id khadas_mcu_user_mem_id_table[] = {
+	{ .name = "khadas-mcu-user-mem", },
+	{},
+};
+MODULE_DEVICE_TABLE(platform, khadas_mcu_user_mem_id_table);
+
+static struct platform_driver khadas_mcu_user_mem_driver = {
+	.probe = khadas_mcu_user_mem_probe,
+	.driver = {
+		.name = "khadas-mcu-user-mem",
+	},
+	.id_table = khadas_mcu_user_mem_id_table,
+};
+
+module_platform_driver(khadas_mcu_user_mem_driver);
+
+MODULE_AUTHOR("Neil Armstrong <[email protected]>");
+MODULE_DESCRIPTION("Khadas MCU User MEM driver");
+MODULE_LICENSE("GPL v2");
#13 lore raw

Re: [PATCH v2 4/6] nvmem: add support for the Khadas MCU Programmable User Memory

Quoted 10 lines · Srinivas Kandagatla
Quoted 5 lines · Neil Armstrong

The new Khadas VIM2, VIM3 and Edge boards embeds an on-board microcontroller offering a 56bytes User Programmable NVMEM array.

This array needs a password to be writable, thus a password sysfs file has been added on the device node to unlock the NVMEM.

Is this one time programmable? Or does it need to be unlocked for every read/write?

It can be rewritten, and needs the password to read & write.

Quoted 2 lines

How can you make sure that the memory is unlocked before making attempt to read/write?

The only way to know it's unlock is reading back the password when unlocked.

Quoted 54 lines
Quoted 54 lines

The default 6bytes password id: "Khadas"

This implements the user NVMEM devices as cell of the Khadas MCU MFD driver.

---
  drivers/nvmem/Kconfig               |   8 ++ drivers/nvmem/Makefile              |   2 + drivers/nvmem/khadas-mcu-user-mem.c | 128 ++++++++++++++++++++++++++++

  3 files changed, 138 insertions(+)

  create mode 100644 drivers/nvmem/khadas-mcu-user-mem.c

drivers/nvmem/Kconfig +0 -0
index d7b7f6d688e7..92cd4f6aa931 100644
@@ -67,6 +67,14 @@ config JZ4780_EFUSE

        To compile this driver as a module, choose M here: the module will be called nvmem_jz4780_efuse.
  +config NVMEM_KHADAS_MCU_USER_MEM
+    tristate "Khadas MCU User programmable memory support" +    depends on MFD_KHADAS_MCU
+    depends on REGMAP
+    help
+      This is a driver for the MCU User programmable memory +      available on the Khadas VIM and Edge boards.
+
  config NVMEM_LPC18XX_EEPROM
      tristate "NXP LPC18XX EEPROM Memory Support"
      depends on ARCH_LPC18XX || COMPILE_TEST

drivers/nvmem/Makefile +0 -0
index a7c377218341..0516a309542d 100644
@@ -17,6 +17,8 @@ obj-$(CONFIG_NVMEM_IMX_OCOTP_SCU)    += nvmem-imx-ocotp-scu.o

  nvmem-imx-ocotp-scu-y        := imx-ocotp-scu.o obj-$(CONFIG_JZ4780_EFUSE)        += nvmem_jz4780_efuse.o nvmem_jz4780_efuse-y        := jz4780-efuse.o

drivers/nvmem/Makefile +2 -0
+obj-$(CONFIG_NVMEM_KHADAS_MCU_USER_MEM)    += nvmem-khadas-mcu-user-mem.o
+nvmem-khadas-mcu-user-mem-y    := khadas-mcu-user-mem.o

  obj-$(CONFIG_NVMEM_LPC18XX_EEPROM)    += nvmem_lpc18xx_eeprom.o nvmem_lpc18xx_eeprom-y    := lpc18xx_eeprom.o obj-$(CONFIG_NVMEM_LPC18XX_OTP)    += nvmem_lpc18xx_otp.o

drivers/nvmem/khadas-mcu-user-mem.c new file +3 -0
new file mode 100644
index 000000000000..a1d5ae9a030c
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Khadas MCU User programmable Memory
Quoted 8 lines

drivers/nvmem/khadas-mcu-user-mem.c line 9

Quoted 6 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +6 -0
+ *
+ * Copyright (C) 2020 BayLibre SAS
+ * Author(s): Neil Armstrong <[email protected]>
+ */
+
+#include <linux/clk.h>

Why do we need this header?

Will drop

Quoted 30 lines
Quoted 30 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +29 -0
+#include <linux/module.h>
+#include <linux/nvmem-provider.h>
+#include <linux/mfd/khadas-mcu.h>
+#include <linux/regmap.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+
+static int khadas_mcu_user_mem_read(void *context, unsigned int offset,
+                void *val, size_t bytes)
+{
+    struct khadas_mcu *khadas_mcu = context;
+
+    return regmap_bulk_read(khadas_mcu->map,
+                KHADAS_MCU_USER_DATA_0_REG + offset,
+                val, bytes);
+}
+
+static int khadas_mcu_user_mem_write(void *context, unsigned int offset,
+                 void *val, size_t bytes)
+{
+    struct khadas_mcu *khadas_mcu = context;
+
+    return regmap_bulk_write(khadas_mcu->map,
+                KHADAS_MCU_USER_DATA_0_REG + offset,
+                val, bytes);
+}
+
+static ssize_t password_store(struct device *dev, struct device_attribute *attr,
+                 const char *buf, size_t count)
Quoted 8 lines

drivers/nvmem/khadas-mcu-user-mem.c line 44

Quoted 6 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +6 -0
+{
+    struct khadas_mcu *khadas_mcu = dev_get_drvdata(dev);
+    int i, ret;
+
+    if (count < 6)
+        return -EINVAL;

Possibly this magic 6 value needs proper define. An additional check here for > 6 would be better as well.

Ok

Quoted 33 lines
Quoted 33 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +32 -0
+
+    ret = regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 1);
+    if (ret)
+        return ret;
+
+    for (i = 0 ; i < 6 ; ++i) {
+        ret = regmap_write(khadas_mcu->map,
+                   KHADAS_MCU_CHECK_USER_PASSWD_REG,
+                   buf[i]);
+        if (ret)
+            goto out;
+    }
+
+    ret = regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 0);
+    if (ret)
+        return ret;
+
+    return count;
+out:
+    regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 0);
+
+    return ret;
+}
+
+static DEVICE_ATTR_WO(password);
+
+static struct attribute *khadas_mcu_user_mem_sysfs_attributes[] = {
+    &dev_attr_password.attr,
+    NULL,
+};
+
+static const struct attribute_group khadas_mcu_user_mem_sysfs_attr_group = {
Quoted 8 lines

drivers/nvmem/khadas-mcu-user-mem.c line 82

Quoted 6 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +6 -0
+    .attrs = khadas_mcu_user_mem_sysfs_attributes,
+};
+
+static int khadas_mcu_user_mem_probe(struct platform_device *pdev)
+{
+    struct khadas_mcu *khadas_mcu = dev_get_drvdata(pdev->dev.parent);

You could probably get away with dependency of this structure and the header itself by directly getting hold of regmap from parent device.

Ok

Quoted 18 lines
Quoted 7 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +5 -0
+    struct device *dev = &pdev->dev;
+    struct nvmem_device *nvmem;
+    struct nvmem_config *econfig;
+
+    econfig = devm_kzalloc(dev, sizeof(*econfig), GFP_KERNEL);

drivers/nvmem/khadas-mcu-user-mem.c line 97

Quoted 10 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +10 -0
+    if (!econfig)
+        return -ENOMEM;
+
+    econfig->dev = pdev->dev.parent;
+    econfig->name = dev_name(pdev->dev.parent);
+    econfig->stride = 1;
+    econfig->word_size = 1;
+    econfig->reg_read = khadas_mcu_user_mem_read;
+    econfig->reg_write = khadas_mcu_user_mem_write;
+    econfig->size = 56;

define 56 to make it more readable!

Ok

Quoted 25 lines
Quoted 25 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +24 -0
+    econfig->priv = khadas_mcu;
+
+    platform_set_drvdata(pdev, khadas_mcu);
+
+    nvmem = devm_nvmem_register(&pdev->dev, econfig);
+    if (IS_ERR(nvmem))
+        return PTR_ERR(nvmem);
+
+    return sysfs_create_group(&pdev->dev.kobj,
+                  &khadas_mcu_user_mem_sysfs_attr_group);
+}
+
+static const struct platform_device_id khadas_mcu_user_mem_id_table[] = {
+    { .name = "khadas-mcu-user-mem", },
+    {},
+};
+MODULE_DEVICE_TABLE(platform, khadas_mcu_user_mem_id_table);
+
+static struct platform_driver khadas_mcu_user_mem_driver = {
+    .probe = khadas_mcu_user_mem_probe,
+    .driver = {
+        .name = "khadas-mcu-user-mem",
+    },
+    .id_table = khadas_mcu_user_mem_id_table,
Quoted 8 lines

drivers/nvmem/khadas-mcu-user-mem.c line 128

Quoted 8 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +7 -0
+};
+
+module_platform_driver(khadas_mcu_user_mem_driver);
+
+MODULE_AUTHOR("Neil Armstrong <[email protected]>");
+MODULE_DESCRIPTION("Khadas MCU User MEM driver");
+MODULE_LICENSE("GPL v2");

Thanks for the review.

Neil

#8 lore raw

Re: [PATCH v2 3/6] thermal: add support for the MCU controlled FAN on Khadas boards

In reply to Neil Armstrong
Quoted 20 lines · Neil Armstrong

The new Khadas VIM2 and VIM3 boards controls the cooling fan via the on-board microcontroller.

This implements the FAN control as thermal devices and as cell of the Khadas MCU MFD driver.

---
 drivers/thermal/Kconfig          |  10 ++
 drivers/thermal/Makefile         |   1 +
 drivers/thermal/khadas_mcu_fan.c | 174 +++++++++++++++++++++++++++++++
 3 files changed, 185 insertions(+)
 create mode 100644 drivers/thermal/khadas_mcu_fan.c
drivers/thermal/Kconfig +0 -0
index 91af271e9bb0..72b3960cc5ac 100644

drivers/thermal/Kconfig config SPRD_THERMAL · line 496

Quoted 7 lines
drivers/thermal/Kconfig +4 -0
@@ -490,4 +490,14 @@ config SPRD_THERMAL
        help
          Support for the Spreadtrum thermal sensor driver in the Linux thermal
          framework.
+
+config KHADAS_MCU_FAN_THERMAL
+       tristate "Khadas MCU controller FAN cooling support"
+       depends on OF || COMPILE_TEST

Could you add a depends on the some board/SoC Kconfig option here so this doesn't show up for non-Amlogic/non-Khadas boards?

Looks OK otherwise.

Quoted 197 lines
drivers/thermal/Kconfig +6 -0
+       select MFD_CORE
+       select REGMAP
+       help
+         If you say yes here you get support for the FAN controlled
+         by the Microcontroller found on the Khadas VIM boards.
+
 endif
drivers/thermal/Makefile +1 -0
index 8c8ed7b79915..460428c2122c 100644
@@ -60,3 +60,4 @@ obj-$(CONFIG_ZX2967_THERMAL)  += zx2967_thermal.o
 obj-$(CONFIG_UNIPHIER_THERMAL) += uniphier_thermal.o
 obj-$(CONFIG_AMLOGIC_THERMAL)     += amlogic_thermal.o
 obj-$(CONFIG_SPRD_THERMAL)     += sprd_thermal.o
+obj-$(CONFIG_KHADAS_MCU_FAN_THERMAL)   += khadas_mcu_fan.o
drivers/thermal/khadas_mcu_fan.c new file +174 -1
new file mode 100644
index 000000000000..044d4aba8be2
@@ -0,0 +1,174 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Khadas MCU Controlled FAN driver
+ *
+ * Copyright (C) 2020 BayLibre SAS
+ * Author(s): Neil Armstrong <[email protected]>
+ */
+
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/mfd/khadas-mcu.h>
+#include <linux/regmap.h>
+#include <linux/sysfs.h>
+#include <linux/thermal.h>
+
+#define MAX_LEVEL 3
+
+struct khadas_mcu_fan_ctx {
+       struct khadas_mcu *mcu;
+       unsigned int level;
+       struct thermal_cooling_device *cdev;
+};
+
+static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
+                                   unsigned int level)
+{
+       int ret;
+
+       ret = regmap_write(ctx->mcu->map, KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG,
+                          level);
+       if (ret)
+               return ret;
+
+       ctx->level = level;
+
+       return 0;
+}
+
+static int khadas_mcu_fan_get_max_state(struct thermal_cooling_device *cdev,
+                                       unsigned long *state)
+{
+       struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
+
+       if (!ctx)
+               return -EINVAL;
+
+       *state = MAX_LEVEL;
+
+       return 0;
+}
+
+static int khadas_mcu_fan_get_cur_state(struct thermal_cooling_device *cdev,
+                                       unsigned long *state)
+{
+       struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
+
+       if (!ctx)
+               return -EINVAL;
+
+       *state = ctx->level;
+
+       return 0;
+}
+
+static int
+khadas_mcu_fan_set_cur_state(struct thermal_cooling_device *cdev,
+                            unsigned long state)
+{
+       struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
+
+       if (!ctx || (state > MAX_LEVEL))
+               return -EINVAL;
+
+       if (state == ctx->level)
+               return 0;
+
+       return khadas_mcu_fan_set_level(ctx, state);
+}
+
+static const struct thermal_cooling_device_ops khadas_mcu_fan_cooling_ops = {
+       .get_max_state = khadas_mcu_fan_get_max_state,
+       .get_cur_state = khadas_mcu_fan_get_cur_state,
+       .set_cur_state = khadas_mcu_fan_set_cur_state,
+};
+
+static int khadas_mcu_fan_probe(struct platform_device *pdev)
+{
+       struct khadas_mcu *mcu = dev_get_drvdata(pdev->dev.parent);
+       struct thermal_cooling_device *cdev;
+       struct device *dev = &pdev->dev;
+       struct khadas_mcu_fan_ctx *ctx;
+       int ret;
+
+       ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
+       if (!ctx)
+               return -ENOMEM;
+       ctx->mcu = mcu;
+       platform_set_drvdata(pdev, ctx);
+
+       cdev = devm_thermal_of_cooling_device_register(dev->parent,
+                       dev->parent->of_node, "khadas-mcu-fan", ctx,
+                       &khadas_mcu_fan_cooling_ops);
+       if (IS_ERR(cdev)) {
+               ret = PTR_ERR(cdev);
+               dev_err(dev,
+                               "Failed to register khadas-mcu-fan as cooling device: %d\n",
+                               ret);
+               return ret;
+       }
+       ctx->cdev = cdev;
+       thermal_cdev_update(cdev);
+
+       return 0;
+}
+
+static int khadas_mcu_fan_disable(struct device *dev)
+{
+       struct khadas_mcu_fan_ctx *ctx = dev_get_drvdata(dev);
+       unsigned int level_save = ctx->level;
+       int ret;
+
+       ret = khadas_mcu_fan_set_level(ctx, 0);
+       if (ret)
+               return ret;
+
+       ctx->level = level_save;
+
+       return 0;
+}
+
+static void khadas_mcu_fan_shutdown(struct platform_device *pdev)
+{
+       khadas_mcu_fan_disable(&pdev->dev);
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int khadas_mcu_fan_suspend(struct device *dev)
+{
+       return khadas_mcu_fan_disable(dev);
+}
+
+static int khadas_mcu_fan_resume(struct device *dev)
+{
+       struct khadas_mcu_fan_ctx *ctx = dev_get_drvdata(dev);
+
+       return khadas_mcu_fan_set_level(ctx, ctx->level);
+}
+#endif
+
+static SIMPLE_DEV_PM_OPS(khadas_mcu_fan_pm, khadas_mcu_fan_suspend,
+                        khadas_mcu_fan_resume);
+
+static const struct platform_device_id khadas_mcu_fan_id_table[] = {
+       { .name = "khadas-mcu-fan-ctrl", },
+       {},
+};
+MODULE_DEVICE_TABLE(platform, khadas_mcu_fan_id_table);
+
+static struct platform_driver khadas_mcu_fan_driver = {
+       .probe          = khadas_mcu_fan_probe,
+       .shutdown       = khadas_mcu_fan_shutdown,
+       .driver = {
+               .name           = "khadas-mcu-fan-ctrl",
+               .pm             = &khadas_mcu_fan_pm,
+       },
+       .id_table       = khadas_mcu_fan_id_table,
+};
+
+module_platform_driver(khadas_mcu_fan_driver);
+
+MODULE_AUTHOR("Neil Armstrong <[email protected]>");
+MODULE_DESCRIPTION("Khadas MCU FAN driver");
+MODULE_LICENSE("GPL");
--
Quoted 2 lines

2.22.0

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

Reviewed-by on v2 1/6
#2 lore raw

Re: [PATCH v2 1/6] dt-bindings: mfd: add Khadas Microcontroller bindings

In reply to Neil Armstrong
Quoted 9 lines · Neil Armstrong

This Microcontroller is present on the Khadas VIM1, VIM2, VIM3 and Edge boards.

It has multiple boot control features like password check, power-on options, power-off control and system FAN control on recent boards.

Quoted 58 lines
---
 .../devicetree/bindings/mfd/khadas,mcu.yaml   | 44 +++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mfd/khadas,mcu.yaml
Documentation/devicetree/bindings/mfd/khadas,mcu.yaml new file +44 -1
new file mode 100644
index 000000000000..a3b976f101e8
@@ -0,0 +1,44 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mfd/khadas,mcu.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Khadas on-board Microcontroller Device Tree Bindings
+
+maintainers:
+  - Neil Armstrong <[email protected]>
+
+description: |
+  Khadas embeds a microcontroller on their VIM and Edge boards adding some
+  system feature as PWM Fan control (for VIM2 rev14 or VIM3), User memory
+  storage, IR/Key resume control, system power LED control and more.
+
+properties:
+  compatible:
+    enum:
+      - khadas,mcu # MCU revision is discoverable
+
+  "#cooling-cells": # Only needed for boards having FAN control feature
+    const: 2
+
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+      #address-cells = <1>;
+      #size-cells = <0>;
+      khadas_mcu: system-controller@18 {
+        compatible = "khadas,mcu";
+        reg = <0x18>;
+        #cooling-cells = <2>;
+      };
+    };
--

2.22.0

#9 lore raw

Re: [PATCH v2 3/6] thermal: add support for the MCU controlled FAN on Khadas boards

In reply to Amit Kucheria
Quoted 16 lines · Amit Kucheria
Quoted 15 lines · Neil Armstrong

The new Khadas VIM2 and VIM3 boards controls the cooling fan via the on-board microcontroller.

This implements the FAN control as thermal devices and as cell of the Khadas MCU MFD driver.

---
 drivers/thermal/Kconfig          |  10 ++
 drivers/thermal/Makefile         |   1 +
 drivers/thermal/khadas_mcu_fan.c | 174 +++++++++++++++++++++++++++++++
 3 files changed, 185 insertions(+)
 create mode 100644 drivers/thermal/khadas_mcu_fan.c
Quoted 15 lines

drivers/thermal/Kconfig config SPRD_THERMAL · line 496

Quoted 12 lines
drivers/thermal/Kconfig +4 -0
index 91af271e9bb0..72b3960cc5ac 100644
@@ -490,4 +490,14 @@ config SPRD_THERMAL
        help
          Support for the Spreadtrum thermal sensor driver in the Linux thermal
          framework.
+
+config KHADAS_MCU_FAN_THERMAL
+       tristate "Khadas MCU controller FAN cooling support"
+       depends on OF || COMPILE_TEST

Could you add a depends on the some board/SoC Kconfig option here so this doesn't show up for non-Amlogic/non-Khadas boards?

Sure,

Thanks.

Neil

Quoted 202 lines

Looks OK otherwise.

Quoted 199 lines
drivers/thermal/Kconfig +6 -0
+       select MFD_CORE
+       select REGMAP
+       help
+         If you say yes here you get support for the FAN controlled
+         by the Microcontroller found on the Khadas VIM boards.
+
 endif
drivers/thermal/Makefile +1 -0
index 8c8ed7b79915..460428c2122c 100644
@@ -60,3 +60,4 @@ obj-$(CONFIG_ZX2967_THERMAL)  += zx2967_thermal.o
 obj-$(CONFIG_UNIPHIER_THERMAL) += uniphier_thermal.o
 obj-$(CONFIG_AMLOGIC_THERMAL)     += amlogic_thermal.o
 obj-$(CONFIG_SPRD_THERMAL)     += sprd_thermal.o
+obj-$(CONFIG_KHADAS_MCU_FAN_THERMAL)   += khadas_mcu_fan.o
drivers/thermal/khadas_mcu_fan.c new file +174 -1
new file mode 100644
index 000000000000..044d4aba8be2
@@ -0,0 +1,174 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Khadas MCU Controlled FAN driver
+ *
+ * Copyright (C) 2020 BayLibre SAS
+ * Author(s): Neil Armstrong <[email protected]>
+ */
+
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/mfd/khadas-mcu.h>
+#include <linux/regmap.h>
+#include <linux/sysfs.h>
+#include <linux/thermal.h>
+
+#define MAX_LEVEL 3
+
+struct khadas_mcu_fan_ctx {
+       struct khadas_mcu *mcu;
+       unsigned int level;
+       struct thermal_cooling_device *cdev;
+};
+
+static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
+                                   unsigned int level)
+{
+       int ret;
+
+       ret = regmap_write(ctx->mcu->map, KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG,
+                          level);
+       if (ret)
+               return ret;
+
+       ctx->level = level;
+
+       return 0;
+}
+
+static int khadas_mcu_fan_get_max_state(struct thermal_cooling_device *cdev,
+                                       unsigned long *state)
+{
+       struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
+
+       if (!ctx)
+               return -EINVAL;
+
+       *state = MAX_LEVEL;
+
+       return 0;
+}
+
+static int khadas_mcu_fan_get_cur_state(struct thermal_cooling_device *cdev,
+                                       unsigned long *state)
+{
+       struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
+
+       if (!ctx)
+               return -EINVAL;
+
+       *state = ctx->level;
+
+       return 0;
+}
+
+static int
+khadas_mcu_fan_set_cur_state(struct thermal_cooling_device *cdev,
+                            unsigned long state)
+{
+       struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
+
+       if (!ctx || (state > MAX_LEVEL))
+               return -EINVAL;
+
+       if (state == ctx->level)
+               return 0;
+
+       return khadas_mcu_fan_set_level(ctx, state);
+}
+
+static const struct thermal_cooling_device_ops khadas_mcu_fan_cooling_ops = {
+       .get_max_state = khadas_mcu_fan_get_max_state,
+       .get_cur_state = khadas_mcu_fan_get_cur_state,
+       .set_cur_state = khadas_mcu_fan_set_cur_state,
+};
+
+static int khadas_mcu_fan_probe(struct platform_device *pdev)
+{
+       struct khadas_mcu *mcu = dev_get_drvdata(pdev->dev.parent);
+       struct thermal_cooling_device *cdev;
+       struct device *dev = &pdev->dev;
+       struct khadas_mcu_fan_ctx *ctx;
+       int ret;
+
+       ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
+       if (!ctx)
+               return -ENOMEM;
+       ctx->mcu = mcu;
+       platform_set_drvdata(pdev, ctx);
+
+       cdev = devm_thermal_of_cooling_device_register(dev->parent,
+                       dev->parent->of_node, "khadas-mcu-fan", ctx,
+                       &khadas_mcu_fan_cooling_ops);
+       if (IS_ERR(cdev)) {
+               ret = PTR_ERR(cdev);
+               dev_err(dev,
+                               "Failed to register khadas-mcu-fan as cooling device: %d\n",
+                               ret);
+               return ret;
+       }
+       ctx->cdev = cdev;
+       thermal_cdev_update(cdev);
+
+       return 0;
+}
+
+static int khadas_mcu_fan_disable(struct device *dev)
+{
+       struct khadas_mcu_fan_ctx *ctx = dev_get_drvdata(dev);
+       unsigned int level_save = ctx->level;
+       int ret;
+
+       ret = khadas_mcu_fan_set_level(ctx, 0);
+       if (ret)
+               return ret;
+
+       ctx->level = level_save;
+
+       return 0;
+}
+
+static void khadas_mcu_fan_shutdown(struct platform_device *pdev)
+{
+       khadas_mcu_fan_disable(&pdev->dev);
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int khadas_mcu_fan_suspend(struct device *dev)
+{
+       return khadas_mcu_fan_disable(dev);
+}
+
+static int khadas_mcu_fan_resume(struct device *dev)
+{
+       struct khadas_mcu_fan_ctx *ctx = dev_get_drvdata(dev);
+
+       return khadas_mcu_fan_set_level(ctx, ctx->level);
+}
+#endif
+
+static SIMPLE_DEV_PM_OPS(khadas_mcu_fan_pm, khadas_mcu_fan_suspend,
+                        khadas_mcu_fan_resume);
+
+static const struct platform_device_id khadas_mcu_fan_id_table[] = {
+       { .name = "khadas-mcu-fan-ctrl", },
+       {},
+};
+MODULE_DEVICE_TABLE(platform, khadas_mcu_fan_id_table);
+
+static struct platform_driver khadas_mcu_fan_driver = {
+       .probe          = khadas_mcu_fan_probe,
+       .shutdown       = khadas_mcu_fan_shutdown,
+       .driver = {
+               .name           = "khadas-mcu-fan-ctrl",
+               .pm             = &khadas_mcu_fan_pm,
+       },
+       .id_table       = khadas_mcu_fan_id_table,
+};
+
+module_platform_driver(khadas_mcu_fan_driver);
+
+MODULE_AUTHOR("Neil Armstrong <[email protected]>");
+MODULE_DESCRIPTION("Khadas MCU FAN driver");
+MODULE_LICENSE("GPL");
--

2.22.0

#14 lore raw

Re: [PATCH v2 4/6] nvmem: add support for the Khadas MCU Programmable User Memory

In reply to Neil Armstrong
Quoted 10 lines · Neil Armstrong
Quoted 9 lines · Srinivas Kandagatla
Quoted 5 lines · Neil Armstrong

The new Khadas VIM2, VIM3 and Edge boards embeds an on-board microcontroller offering a 56bytes User Programmable NVMEM array.

This array needs a password to be writable, thus a password sysfs file has been added on the device node to unlock the NVMEM.

Quoted 8 lines
Quoted 1 line

Is this one time programmable? Or does it need to be unlocked for every read/write?

It can be rewritten, and needs the password to read & write.

Quoted 2 lines

How can you make sure that the memory is unlocked before making attempt to read/write?

The only way to know it's unlock is reading back the password when unlocked.

Current code has no such checks for every read/write? and looks very disconnected w.r.t to password and read/writes.

If user attempts to read/write he will not be aware that he should program the password first!

Also if the password is static or un-changed then why not just statically program this from the driver rather than providing sysfs file?

I dont see how kernel nvmem read/write interface can make sure that memory is unlocked?

Who is the real consumer for this provider?

--srini

Quoted 210 lines
Quoted 55 lines
Quoted 55 lines

The default 6bytes password id: "Khadas"

This implements the user NVMEM devices as cell of the Khadas MCU MFD driver.

---
   drivers/nvmem/Kconfig               |   8 ++
   drivers/nvmem/Makefile              |   2 +
   drivers/nvmem/khadas-mcu-user-mem.c | 128 ++++++++++++++++++++++++++++
   3 files changed, 138 insertions(+)

  create mode 100644 drivers/nvmem/khadas-mcu-user-mem.c

drivers/nvmem/Kconfig +7 -0
index d7b7f6d688e7..92cd4f6aa931 100644
@@ -67,6 +67,14 @@ config JZ4780_EFUSE
         To compile this driver as a module, choose M here: the module
         will be called nvmem_jz4780_efuse.
   +config NVMEM_KHADAS_MCU_USER_MEM
+    tristate "Khadas MCU User programmable memory support"
+    depends on MFD_KHADAS_MCU
+    depends on REGMAP
+    help
+      This is a driver for the MCU User programmable memory
+      available on the Khadas VIM and Edge boards.
+
   config NVMEM_LPC18XX_EEPROM
       tristate "NXP LPC18XX EEPROM Memory Support"
       depends on ARCH_LPC18XX || COMPILE_TEST
drivers/nvmem/Makefile +2 -0
index a7c377218341..0516a309542d 100644
@@ -17,6 +17,8 @@ obj-$(CONFIG_NVMEM_IMX_OCOTP_SCU)    += nvmem-imx-ocotp-scu.o
   nvmem-imx-ocotp-scu-y        := imx-ocotp-scu.o
   obj-$(CONFIG_JZ4780_EFUSE)        += nvmem_jz4780_efuse.o
   nvmem_jz4780_efuse-y        := jz4780-efuse.o
+obj-$(CONFIG_NVMEM_KHADAS_MCU_USER_MEM)    += nvmem-khadas-mcu-user-mem.o
+nvmem-khadas-mcu-user-mem-y    := khadas-mcu-user-mem.o
   obj-$(CONFIG_NVMEM_LPC18XX_EEPROM)    += nvmem_lpc18xx_eeprom.o
   nvmem_lpc18xx_eeprom-y    := lpc18xx_eeprom.o
   obj-$(CONFIG_NVMEM_LPC18XX_OTP)    += nvmem_lpc18xx_otp.o
drivers/nvmem/khadas-mcu-user-mem.c new file +3 -0
new file mode 100644
index 000000000000..a1d5ae9a030c
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Khadas MCU User programmable Memory
Quoted 8 lines

drivers/nvmem/khadas-mcu-user-mem.c line 9

Quoted 6 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +6 -0
+ *
+ * Copyright (C) 2020 BayLibre SAS
+ * Author(s): Neil Armstrong <[email protected]>
+ */
+
+#include <linux/clk.h>

Why do we need this header?

Will drop

Quoted 30 lines
Quoted 30 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +29 -0
+#include <linux/module.h>
+#include <linux/nvmem-provider.h>
+#include <linux/mfd/khadas-mcu.h>
+#include <linux/regmap.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+
+static int khadas_mcu_user_mem_read(void *context, unsigned int offset,
+                void *val, size_t bytes)
+{
+    struct khadas_mcu *khadas_mcu = context;
+
+    return regmap_bulk_read(khadas_mcu->map,
+                KHADAS_MCU_USER_DATA_0_REG + offset,
+                val, bytes);
+}
+
+static int khadas_mcu_user_mem_write(void *context, unsigned int offset,
+                 void *val, size_t bytes)
+{
+    struct khadas_mcu *khadas_mcu = context;
+
+    return regmap_bulk_write(khadas_mcu->map,
+                KHADAS_MCU_USER_DATA_0_REG + offset,
+                val, bytes);
+}
+
+static ssize_t password_store(struct device *dev, struct device_attribute *attr,
+                 const char *buf, size_t count)
Quoted 8 lines

drivers/nvmem/khadas-mcu-user-mem.c line 44

Quoted 6 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +6 -0
+{
+    struct khadas_mcu *khadas_mcu = dev_get_drvdata(dev);
+    int i, ret;
+
+    if (count < 6)
+        return -EINVAL;

Possibly this magic 6 value needs proper define. An additional check here for > 6 would be better as well.

Ok

Quoted 33 lines
Quoted 33 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +32 -0
+
+    ret = regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 1);
+    if (ret)
+        return ret;
+
+    for (i = 0 ; i < 6 ; ++i) {
+        ret = regmap_write(khadas_mcu->map,
+                   KHADAS_MCU_CHECK_USER_PASSWD_REG,
+                   buf[i]);
+        if (ret)
+            goto out;
+    }
+
+    ret = regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 0);
+    if (ret)
+        return ret;
+
+    return count;
+out:
+    regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 0);
+
+    return ret;
+}
+
+static DEVICE_ATTR_WO(password);
+
+static struct attribute *khadas_mcu_user_mem_sysfs_attributes[] = {
+    &dev_attr_password.attr,
+    NULL,
+};
+
+static const struct attribute_group khadas_mcu_user_mem_sysfs_attr_group = {
Quoted 8 lines

drivers/nvmem/khadas-mcu-user-mem.c line 82

Quoted 6 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +6 -0
+    .attrs = khadas_mcu_user_mem_sysfs_attributes,
+};
+
+static int khadas_mcu_user_mem_probe(struct platform_device *pdev)
+{
+    struct khadas_mcu *khadas_mcu = dev_get_drvdata(pdev->dev.parent);

You could probably get away with dependency of this structure and the header itself by directly getting hold of regmap from parent device.

Ok

Quoted 18 lines
Quoted 7 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +5 -0
+    struct device *dev = &pdev->dev;
+    struct nvmem_device *nvmem;
+    struct nvmem_config *econfig;
+
+    econfig = devm_kzalloc(dev, sizeof(*econfig), GFP_KERNEL);

drivers/nvmem/khadas-mcu-user-mem.c line 97

Quoted 10 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +10 -0
+    if (!econfig)
+        return -ENOMEM;
+
+    econfig->dev = pdev->dev.parent;
+    econfig->name = dev_name(pdev->dev.parent);
+    econfig->stride = 1;
+    econfig->word_size = 1;
+    econfig->reg_read = khadas_mcu_user_mem_read;
+    econfig->reg_write = khadas_mcu_user_mem_write;
+    econfig->size = 56;

define 56 to make it more readable!

Ok

Quoted 25 lines
Quoted 25 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +24 -0
+    econfig->priv = khadas_mcu;
+
+    platform_set_drvdata(pdev, khadas_mcu);
+
+    nvmem = devm_nvmem_register(&pdev->dev, econfig);
+    if (IS_ERR(nvmem))
+        return PTR_ERR(nvmem);
+
+    return sysfs_create_group(&pdev->dev.kobj,
+                  &khadas_mcu_user_mem_sysfs_attr_group);
+}
+
+static const struct platform_device_id khadas_mcu_user_mem_id_table[] = {
+    { .name = "khadas-mcu-user-mem", },
+    {},
+};
+MODULE_DEVICE_TABLE(platform, khadas_mcu_user_mem_id_table);
+
+static struct platform_driver khadas_mcu_user_mem_driver = {
+    .probe = khadas_mcu_user_mem_probe,
+    .driver = {
+        .name = "khadas-mcu-user-mem",
+    },
+    .id_table = khadas_mcu_user_mem_id_table,
Quoted 8 lines

drivers/nvmem/khadas-mcu-user-mem.c line 128

Quoted 8 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +7 -0
+};
+
+module_platform_driver(khadas_mcu_user_mem_driver);
+
+MODULE_AUTHOR("Neil Armstrong <[email protected]>");
+MODULE_DESCRIPTION("Khadas MCU User MEM driver");
+MODULE_LICENSE("GPL v2");

Thanks for the review.

Neil

#4 lore raw

Re: [PATCH v2 2/6] mfd: add support for the Khadas System control Microcontroller

In reply to Neil Armstrong
Quoted 30 lines · Neil Armstrong

This Microcontroller is present on the Khadas VIM1, VIM2, VIM3 and Edge boards.

It has multiple boot control features like password check, power-on options, power-off control and system FAN control on recent boards.

This implements a very basic MFD driver with the fan control and User NVMEM cells.

---
 drivers/mfd/Kconfig            |  14 ++++
 drivers/mfd/Makefile           |   1 +
 drivers/mfd/khadas-mcu.c       | 143 +++++++++++++++++++++++++++++++++
 include/linux/mfd/khadas-mcu.h |  91 +++++++++++++++++++++
 4 files changed, 249 insertions(+)
 create mode 100644 drivers/mfd/khadas-mcu.c
 create mode 100644 include/linux/mfd/khadas-mcu.h
drivers/mfd/Kconfig +3 -0
index 0a59249198d3..b95091397052 100644
@@ -2003,6 +2003,20 @@ config MFD_WCD934X
 	  This driver provides common support WCD934x audio codec and its
 	  associated Pin Controller, Soundwire Controller and Audio codec.
 
+config MFD_KHADAS_MCU
+	tristate "Support for Khadas System control Microcontroller"
+	depends on I2C

drivers/mfd/Kconfig config MFD_WCD934X · line 2018

Quoted 10 lines
drivers/mfd/Kconfig +10 -0
+	depends on OF || COMPILE_TEST
+	select MFD_CORE
+	select REGMAP_I2C
+	help
+	  Support for the Khadas System control Microcontroller interface present
+	  on their VIM and Edge boards.
+
+	  This driver provides common support for accessing the device,
+	  additional drivers must be enabled in order to use the functionality
+	  of the device.

It would be good to describe the device here.

Quoted 20 lines
drivers/mfd/Kconfig +0 -0
 menu "Multimedia Capabilities Port drivers"
 	depends on ARCH_SA1100
 
drivers/mfd/Makefile +1 -0
index f935d10cbf0f..0f1633b096bb 100644
@@ -257,5 +257,6 @@ obj-$(CONFIG_MFD_ROHM_BD70528)	+= rohm-bd70528.o
 obj-$(CONFIG_MFD_ROHM_BD71828)	+= rohm-bd71828.o
 obj-$(CONFIG_MFD_ROHM_BD718XX)	+= rohm-bd718x7.o
 obj-$(CONFIG_MFD_STMFX) 	+= stmfx.o
+obj-$(CONFIG_MFD_KHADAS_MCU) 	+= khadas-mcu.o
 
 obj-$(CONFIG_SGI_MFD_IOC3)	+= ioc3.o
drivers/mfd/khadas-mcu.c new file +0 -0
new file mode 100644
index 000000000000..6d08fa2e373a

drivers/mfd/khadas-mcu.c line 5

Quoted 5 lines
drivers/mfd/khadas-mcu.c new file +5 -0
@@ -0,0 +1,143 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Khadas System control Microcontroller
+ *
+ * Copyright (C) 2020 BayLibre SAS

Nit: '\n' here please.

drivers/mfd/khadas-mcu.c line 13

Quoted 8 lines
drivers/mfd/khadas-mcu.c new file +8 -0
+ * Author(s): Neil Armstrong <[email protected]>
+ */
+#include <linux/bitfield.h>
+#include <linux/i2c.h>
+#include <linux/mfd/khadas-mcu.h>
+#include <linux/regmap.h>
+#include <linux/mfd/core.h>
+#include <linux/module.h>

Alphabetical.

Quoted 54 lines
drivers/mfd/khadas-mcu.c new file +54 -0
+static bool khadas_mcu_reg_volatile(struct device *dev, unsigned int reg)
+{
+	if (reg >= KHADAS_MCU_USER_DATA_0_REG &&
+	    reg < KHADAS_MCU_PWR_OFF_CMD_REG)
+		return true;
+
+	switch (reg) {
+	case KHADAS_MCU_PWR_OFF_CMD_REG:
+	case KHADAS_MCU_PASSWD_START_REG:
+	case KHADAS_MCU_CHECK_VEN_PASSWD_REG:
+	case KHADAS_MCU_CHECK_USER_PASSWD_REG:
+	case KHADAS_MCU_WOL_INIT_START_REG:
+	case KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG:
+		return true;
+	default:
+		return false;
+	}
+}
+
+static bool khadas_mcu_reg_writeable(struct device *dev, unsigned int reg)
+{
+	switch (reg) {
+	case KHADAS_MCU_PASSWD_VEN_0_REG:
+	case KHADAS_MCU_PASSWD_VEN_1_REG:
+	case KHADAS_MCU_PASSWD_VEN_2_REG:
+	case KHADAS_MCU_PASSWD_VEN_3_REG:
+	case KHADAS_MCU_PASSWD_VEN_4_REG:
+	case KHADAS_MCU_PASSWD_VEN_5_REG:
+	case KHADAS_MCU_MAC_0_REG:
+	case KHADAS_MCU_MAC_1_REG:
+	case KHADAS_MCU_MAC_2_REG:
+	case KHADAS_MCU_MAC_3_REG:
+	case KHADAS_MCU_MAC_4_REG:
+	case KHADAS_MCU_MAC_5_REG:
+	case KHADAS_MCU_USID_0_REG:
+	case KHADAS_MCU_USID_1_REG:
+	case KHADAS_MCU_USID_2_REG:
+	case KHADAS_MCU_USID_3_REG:
+	case KHADAS_MCU_USID_4_REG:
+	case KHADAS_MCU_USID_5_REG:
+	case KHADAS_MCU_VERSION_0_REG:
+	case KHADAS_MCU_VERSION_1_REG:
+	case KHADAS_MCU_DEVICE_NO_0_REG:
+	case KHADAS_MCU_DEVICE_NO_1_REG:
+	case KHADAS_MCU_FACTORY_TEST_REG:
+	case KHADAS_MCU_SHUTDOWN_NORMAL_STATUS_REG:
+		return false;
+	default:
+		return true;
+	}
+}
+
+static const struct regmap_config khadas_mcu_regmap_config = {
+	.reg_bits	= 8,

drivers/mfd/khadas-mcu.c line 78

Quoted 10 lines
drivers/mfd/khadas-mcu.c new file +10 -0
+	.reg_stride	= 1,
+	.val_bits	= 8,
+	.max_register	= KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG,
+	.volatile_reg	= khadas_mcu_reg_volatile,
+	.writeable_reg	= khadas_mcu_reg_writeable,
+	.cache_type	= REGCACHE_RBTREE,
+};
+
+static struct mfd_cell khadas_mcu_fan_cells[] = {
+	/* Feature supported only on VIM1/2 Rev13+ and VIM3 */

Doesn't read great.

Consider reversing or make the sentence more succinct.

"VIM1/2 Rev13+ and VIM3 only"

drivers/mfd/khadas-mcu.c line 83

Quoted 5 lines
drivers/mfd/khadas-mcu.c new file +5 -0
+	{ .name = "khadas-mcu-fan-ctrl", },
+};
+
+static struct mfd_cell khadas_mcu_cells[] = {
+	/* Features supported on all board revisions */

I think we can omit this.

drivers/mfd/khadas-mcu.c line 91

Quoted 8 lines
drivers/mfd/khadas-mcu.c new file +8 -0
+	{ .name = "khadas-mcu-user-mem", },
+};
+
+static int khadas_mcu_probe(struct i2c_client *client,
+		       const struct i2c_device_id *id)
+{
+	struct device *dev = &client->dev;
+	struct khadas_mcu *khadas_mcu;

Prefer a rename to 'ddata'.

drivers/mfd/khadas-mcu.c line 103

Quoted 12 lines
drivers/mfd/khadas-mcu.c new file +12 -0
+	int ret;
+
+	khadas_mcu = devm_kzalloc(dev, sizeof(*khadas_mcu), GFP_KERNEL);
+	if (!khadas_mcu)
+		return -ENOMEM;
+
+	i2c_set_clientdata(client, khadas_mcu);
+
+	khadas_mcu->dev = dev;
+
+	khadas_mcu->map = devm_regmap_init_i2c(client,
+					       &khadas_mcu_regmap_config);

Prefer a rename to 'regmap'.

Quoted 46 lines
drivers/mfd/khadas-mcu.c new file +40 -0
+	if (IS_ERR(khadas_mcu->map)) {
+		ret = PTR_ERR(khadas_mcu->map);
+		dev_err(dev, "Failed to allocate register map: %d\n", ret);
+		return ret;
+	}
+
+	ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE,
+				   khadas_mcu_cells,
+				   ARRAY_SIZE(khadas_mcu_cells),
+				   NULL, 0, NULL);
+	if (ret)
+		return ret;
+
+	if (of_find_property(dev->of_node, "#cooling-cells", NULL))
+		return devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE,
+					    khadas_mcu_fan_cells,
+					    ARRAY_SIZE(khadas_mcu_fan_cells),
+					    NULL, 0, NULL);
+
+	return 0;
+}
+
+static const struct of_device_id khadas_mcu_of_match[] = {
+	{ .compatible = "khadas,mcu", },
+	{},
+};
+MODULE_DEVICE_TABLE(of, khadas_mcu_of_match);
+
+static struct i2c_driver khadas_mcu_driver = {
+	.driver = {
+		.name = "khadas-mcu-core",
+		.of_match_table = of_match_ptr(khadas_mcu_of_match),
+	},
+	.probe = khadas_mcu_probe,
+};
+module_i2c_driver(khadas_mcu_driver);
+
+MODULE_DESCRIPTION("Khadas MCU core driver");
+MODULE_AUTHOR("Neil Armstrong <[email protected]>");
+MODULE_LICENSE("GPL v2");
include/linux/mfd/khadas-mcu.h new file +0 -0
new file mode 100644
index 000000000000..2e68af21735c

include/linux/mfd/khadas-mcu.h line 5

Quoted 5 lines
include/linux/mfd/khadas-mcu.h new file +5 -0
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Khadas System control Microcontroller Register map
+ *
+ * Copyright (C) 2020 BayLibre SAS

Nit: '\n'

include/linux/mfd/khadas-mcu.h line 12

Quoted 7 lines
include/linux/mfd/khadas-mcu.h new file +7 -0
+ * Author(s): Neil Armstrong <[email protected]>
+ */
+
+#ifndef MFD_KHADAS_MCU_H
+#define MFD_KHADAS_MCU_H
+
+#define KHADAS_MCU_PASSWD_VEN_0_REG		0x0 /* RO */

Nit: Can you pad these please?

Quoted 50 lines
include/linux/mfd/khadas-mcu.h new file +50 -0
+#define KHADAS_MCU_PASSWD_VEN_1_REG		0x1 /* RO */
+#define KHADAS_MCU_PASSWD_VEN_2_REG		0x2 /* RO */
+#define KHADAS_MCU_PASSWD_VEN_3_REG		0x3 /* RO */
+#define KHADAS_MCU_PASSWD_VEN_4_REG		0x4 /* RO */
+#define KHADAS_MCU_PASSWD_VEN_5_REG		0x5 /* RO */
+#define KHADAS_MCU_MAC_0_REG			0x6 /* RO */
+#define KHADAS_MCU_MAC_1_REG			0x7 /* RO */
+#define KHADAS_MCU_MAC_2_REG			0x8 /* RO */
+#define KHADAS_MCU_MAC_3_REG			0x9 /* RO */
+#define KHADAS_MCU_MAC_4_REG			0xa /* RO */
+#define KHADAS_MCU_MAC_5_REG			0xb /* RO */
+#define KHADAS_MCU_USID_0_REG			0xc /* RO */
+#define KHADAS_MCU_USID_1_REG			0xd /* RO */
+#define KHADAS_MCU_USID_2_REG			0xe /* RO */
+#define KHADAS_MCU_USID_3_REG			0xf /* RO */
+#define KHADAS_MCU_USID_4_REG			0x10 /* RO */
+#define KHADAS_MCU_USID_5_REG			0x11 /* RO */
+#define KHADAS_MCU_VERSION_0_REG		0x12 /* RO */
+#define KHADAS_MCU_VERSION_1_REG		0x13 /* RO */
+#define KHADAS_MCU_DEVICE_NO_0_REG		0x14 /* RO */
+#define KHADAS_MCU_DEVICE_NO_1_REG		0x15 /* RO */
+#define KHADAS_MCU_FACTORY_TEST_REG		0x16 /* R */
+#define KHADAS_MCU_BOOT_MODE_REG		0x20 /* RW */
+#define KHADAS_MCU_BOOT_EN_WOL_REG		0x21 /* RW */
+#define KHADAS_MCU_BOOT_EN_RTC_REG		0x22 /* RW */
+#define KHADAS_MCU_BOOT_EN_EXP_REG		0x23 /* RW */
+#define KHADAS_MCU_BOOT_EN_IR_REG		0x24 /* RW */
+#define KHADAS_MCU_BOOT_EN_DCIN_REG		0x25 /* RW */
+#define KHADAS_MCU_BOOT_EN_KEY_REG		0x26 /* RW */
+#define KHADAS_MCU_KEY_MODE_REG			0x27 /* RW */
+#define KHADAS_MCU_LED_MODE_ON_REG		0x28 /* RW */
+#define KHADAS_MCU_LED_MODE_OFF_REG		0x29 /* RW */
+#define KHADAS_MCU_SHUTDOWN_NORMAL_REG		0x2c /* RW */
+#define KHADAS_MCU_MAC_SWITCH_REG		0x2d /* RW */
+#define KHADAS_MCU_MCU_SLEEP_MODE_REG		0x2e /* RW */
+#define KHADAS_MCU_IR_CODE1_0_REG		0x2f /* RW */
+#define KHADAS_MCU_IR_CODE1_1_REG		0x30 /* RW */
+#define KHADAS_MCU_IR_CODE1_2_REG		0x31 /* RW */
+#define KHADAS_MCU_IR_CODE1_3_REG		0x32 /* RW */
+#define KHADAS_MCU_USB_PCIE_SWITCH_REG		0x33 /* RW */
+#define KHADAS_MCU_IR_CODE2_0_REG		0x34 /* RW */
+#define KHADAS_MCU_IR_CODE2_1_REG		0x35 /* RW */
+#define KHADAS_MCU_IR_CODE2_2_REG		0x36 /* RW */
+#define KHADAS_MCU_IR_CODE2_3_REG		0x37 /* RW */
+#define KHADAS_MCU_PASSWD_USER_0_REG		0x40 /* RW */
+#define KHADAS_MCU_PASSWD_USER_1_REG		0x41 /* RW */
+#define KHADAS_MCU_PASSWD_USER_2_REG		0x42 /* RW */
+#define KHADAS_MCU_PASSWD_USER_3_REG		0x43 /* RW */
+#define KHADAS_MCU_PASSWD_USER_4_REG		0x44 /* RW */
+#define KHADAS_MCU_PASSWD_USER_5_REG		0x45 /* RW */

include/linux/mfd/khadas-mcu.h line 72

Quoted 10 lines
include/linux/mfd/khadas-mcu.h new file +10 -0
+#define KHADAS_MCU_USER_DATA_0_REG		0x46 /* RW 56 bytes */
+#define KHADAS_MCU_PWR_OFF_CMD_REG		0x80 /* WO */
+#define KHADAS_MCU_PASSWD_START_REG		0x81 /* WO */
+#define KHADAS_MCU_CHECK_VEN_PASSWD_REG		0x82 /* WO */
+#define KHADAS_MCU_CHECK_USER_PASSWD_REG	0x83 /* WO */
+#define KHADAS_MCU_SHUTDOWN_NORMAL_STATUS_REG	0x86 /* RO */
+#define KHADAS_MCU_WOL_INIT_START_REG		0x87 /* WO */
+#define KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG	0x88 /* WO */
+
+/* Boards */

I think the names make this superfluous.

include/linux/mfd/khadas-mcu.h line 82

Quoted 10 lines
include/linux/mfd/khadas-mcu.h new file +10 -0
+enum {
+	KHADAS_BOARD_VIM1 = 0x1,
+	KHADAS_BOARD_VIM2,
+	KHADAS_BOARD_VIM3,
+	KHADAS_BOARD_EDGE = 0x11,
+	KHADAS_BOARD_EDGE_V,
+};
+
+/**
+ * struct khadas_mcu_data - Khadas MCU MFD structure

Doesn't match the struct name.

Prefer you drop the 'MFD' part.

Quoted 9 lines
include/linux/mfd/khadas-mcu.h new file +9 -0
+ * @device:		device reference used for logs
+ * @map:		register map
+ */
+struct khadas_mcu {
+	struct device *dev;
+	struct regmap *map;
+};
+
+#endif /* MFD_KHADAS_MCU_H */
Signature · 4 lines
Lee Jones [李琼斯]
Linaro Services Technical Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#5 lore raw

Re: [PATCH v2 2/6] mfd: add support for the Khadas System control Microcontroller

Quoted 29 lines · Lee Jones
Quoted 27 lines · Neil Armstrong

This Microcontroller is present on the Khadas VIM1, VIM2, VIM3 and Edge boards.

It has multiple boot control features like password check, power-on options, power-off control and system FAN control on recent boards.

This implements a very basic MFD driver with the fan control and User NVMEM cells.

---
 drivers/mfd/Kconfig            |  14 ++++
 drivers/mfd/Makefile           |   1 +
 drivers/mfd/khadas-mcu.c       | 143 +++++++++++++++++++++++++++++++++
 include/linux/mfd/khadas-mcu.h |  91 +++++++++++++++++++++
 4 files changed, 249 insertions(+)
 create mode 100644 drivers/mfd/khadas-mcu.c
 create mode 100644 include/linux/mfd/khadas-mcu.h
drivers/mfd/Kconfig +0 -0
index 0a59249198d3..b95091397052 100644
@@ -2003,6 +2003,20 @@ config MFD_WCD934X
 	  This driver provides common support WCD934x audio codec and its
 	  associated Pin Controller, Soundwire Controller and Audio codec.
 
Quoted 15 lines

drivers/mfd/Kconfig config MFD_WCD934X · line 2018

Quoted 13 lines
drivers/mfd/Kconfig +13 -0
+config MFD_KHADAS_MCU
+	tristate "Support for Khadas System control Microcontroller"
+	depends on I2C
+	depends on OF || COMPILE_TEST
+	select MFD_CORE
+	select REGMAP_I2C
+	help
+	  Support for the Khadas System control Microcontroller interface present
+	  on their VIM and Edge boards.
+
+	  This driver provides common support for accessing the device,
+	  additional drivers must be enabled in order to use the functionality
+	  of the device.

It would be good to describe the device here.

Ok

Quoted 14 lines
Quoted 14 lines
drivers/mfd/Kconfig +0 -0
 menu "Multimedia Capabilities Port drivers"
 	depends on ARCH_SA1100
 
drivers/mfd/Makefile +1 -0
index f935d10cbf0f..0f1633b096bb 100644
@@ -257,5 +257,6 @@ obj-$(CONFIG_MFD_ROHM_BD70528)	+= rohm-bd70528.o
 obj-$(CONFIG_MFD_ROHM_BD71828)	+= rohm-bd71828.o
 obj-$(CONFIG_MFD_ROHM_BD718XX)	+= rohm-bd718x7.o
 obj-$(CONFIG_MFD_STMFX) 	+= stmfx.o
+obj-$(CONFIG_MFD_KHADAS_MCU) 	+= khadas-mcu.o
 
Quoted 14 lines

drivers/mfd/khadas-mcu.c line 5

Quoted 12 lines
drivers/mfd/Makefile +0 -0
 obj-$(CONFIG_SGI_MFD_IOC3)	+= ioc3.o
drivers/mfd/khadas-mcu.c new file +5 -0
new file mode 100644
index 000000000000..6d08fa2e373a
@@ -0,0 +1,143 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Khadas System control Microcontroller
+ *
+ * Copyright (C) 2020 BayLibre SAS

Nit: '\n' here please.

Ok

Quoted 11 lines

drivers/mfd/khadas-mcu.c line 13

Quoted 9 lines
drivers/mfd/khadas-mcu.c new file +8 -0
+ * Author(s): Neil Armstrong <[email protected]>
+ */
+#include <linux/bitfield.h>
+#include <linux/i2c.h>
+#include <linux/mfd/khadas-mcu.h>
+#include <linux/regmap.h>
+#include <linux/mfd/core.h>
+#include <linux/module.h>

Alphabetical.

Ok

Quoted 68 lines
Quoted 55 lines
drivers/mfd/khadas-mcu.c new file +54 -0
+static bool khadas_mcu_reg_volatile(struct device *dev, unsigned int reg)
+{
+	if (reg >= KHADAS_MCU_USER_DATA_0_REG &&
+	    reg < KHADAS_MCU_PWR_OFF_CMD_REG)
+		return true;
+
+	switch (reg) {
+	case KHADAS_MCU_PWR_OFF_CMD_REG:
+	case KHADAS_MCU_PASSWD_START_REG:
+	case KHADAS_MCU_CHECK_VEN_PASSWD_REG:
+	case KHADAS_MCU_CHECK_USER_PASSWD_REG:
+	case KHADAS_MCU_WOL_INIT_START_REG:
+	case KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG:
+		return true;
+	default:
+		return false;
+	}
+}
+
+static bool khadas_mcu_reg_writeable(struct device *dev, unsigned int reg)
+{
+	switch (reg) {
+	case KHADAS_MCU_PASSWD_VEN_0_REG:
+	case KHADAS_MCU_PASSWD_VEN_1_REG:
+	case KHADAS_MCU_PASSWD_VEN_2_REG:
+	case KHADAS_MCU_PASSWD_VEN_3_REG:
+	case KHADAS_MCU_PASSWD_VEN_4_REG:
+	case KHADAS_MCU_PASSWD_VEN_5_REG:
+	case KHADAS_MCU_MAC_0_REG:
+	case KHADAS_MCU_MAC_1_REG:
+	case KHADAS_MCU_MAC_2_REG:
+	case KHADAS_MCU_MAC_3_REG:
+	case KHADAS_MCU_MAC_4_REG:
+	case KHADAS_MCU_MAC_5_REG:
+	case KHADAS_MCU_USID_0_REG:
+	case KHADAS_MCU_USID_1_REG:
+	case KHADAS_MCU_USID_2_REG:
+	case KHADAS_MCU_USID_3_REG:
+	case KHADAS_MCU_USID_4_REG:
+	case KHADAS_MCU_USID_5_REG:
+	case KHADAS_MCU_VERSION_0_REG:
+	case KHADAS_MCU_VERSION_1_REG:
+	case KHADAS_MCU_DEVICE_NO_0_REG:
+	case KHADAS_MCU_DEVICE_NO_1_REG:
+	case KHADAS_MCU_FACTORY_TEST_REG:
+	case KHADAS_MCU_SHUTDOWN_NORMAL_STATUS_REG:
+		return false;
+	default:
+		return true;
+	}
+}
+
+static const struct regmap_config khadas_mcu_regmap_config = {
+	.reg_bits	= 8,

drivers/mfd/khadas-mcu.c line 78

Quoted 10 lines
drivers/mfd/khadas-mcu.c new file +10 -0
+	.reg_stride	= 1,
+	.val_bits	= 8,
+	.max_register	= KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG,
+	.volatile_reg	= khadas_mcu_reg_volatile,
+	.writeable_reg	= khadas_mcu_reg_writeable,
+	.cache_type	= REGCACHE_RBTREE,
+};
+
+static struct mfd_cell khadas_mcu_fan_cells[] = {
+	/* Feature supported only on VIM1/2 Rev13+ and VIM3 */

Doesn't read great.

Quoted 3 lines

Consider reversing or make the sentence more succinct.

"VIM1/2 Rev13+ and VIM3 only"

Ok

Quoted 8 lines

drivers/mfd/khadas-mcu.c line 83

Quoted 6 lines
drivers/mfd/khadas-mcu.c new file +5 -0
+	{ .name = "khadas-mcu-fan-ctrl", },
+};
+
+static struct mfd_cell khadas_mcu_cells[] = {
+	/* Features supported on all board revisions */

I think we can omit this.

Ok

Quoted 11 lines

drivers/mfd/khadas-mcu.c line 91

Quoted 9 lines
drivers/mfd/khadas-mcu.c new file +8 -0
+	{ .name = "khadas-mcu-user-mem", },
+};
+
+static int khadas_mcu_probe(struct i2c_client *client,
+		       const struct i2c_device_id *id)
+{
+	struct device *dev = &client->dev;
+	struct khadas_mcu *khadas_mcu;

Prefer a rename to 'ddata'.

Ok

Quoted 14 lines

drivers/mfd/khadas-mcu.c line 103

Quoted 13 lines
drivers/mfd/khadas-mcu.c new file +12 -0
+	int ret;
+
+	khadas_mcu = devm_kzalloc(dev, sizeof(*khadas_mcu), GFP_KERNEL);
+	if (!khadas_mcu)
+		return -ENOMEM;
+
+	i2c_set_clientdata(client, khadas_mcu);
+
+	khadas_mcu->dev = dev;
+
+	khadas_mcu->map = devm_regmap_init_i2c(client,
+					       &khadas_mcu_regmap_config);

Ok

Quoted 47 lines

Prefer a rename to 'regmap'.

Quoted 45 lines
drivers/mfd/khadas-mcu.c new file +40 -0
+	if (IS_ERR(khadas_mcu->map)) {
+		ret = PTR_ERR(khadas_mcu->map);
+		dev_err(dev, "Failed to allocate register map: %d\n", ret);
+		return ret;
+	}
+
+	ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE,
+				   khadas_mcu_cells,
+				   ARRAY_SIZE(khadas_mcu_cells),
+				   NULL, 0, NULL);
+	if (ret)
+		return ret;
+
+	if (of_find_property(dev->of_node, "#cooling-cells", NULL))
+		return devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE,
+					    khadas_mcu_fan_cells,
+					    ARRAY_SIZE(khadas_mcu_fan_cells),
+					    NULL, 0, NULL);
+
+	return 0;
+}
+
+static const struct of_device_id khadas_mcu_of_match[] = {
+	{ .compatible = "khadas,mcu", },
+	{},
+};
+MODULE_DEVICE_TABLE(of, khadas_mcu_of_match);
+
+static struct i2c_driver khadas_mcu_driver = {
+	.driver = {
+		.name = "khadas-mcu-core",
+		.of_match_table = of_match_ptr(khadas_mcu_of_match),
+	},
+	.probe = khadas_mcu_probe,
+};
+module_i2c_driver(khadas_mcu_driver);
+
+MODULE_DESCRIPTION("Khadas MCU core driver");
+MODULE_AUTHOR("Neil Armstrong <[email protected]>");
+MODULE_LICENSE("GPL v2");
include/linux/mfd/khadas-mcu.h new file +0 -0
new file mode 100644
index 000000000000..2e68af21735c
Quoted 8 lines

include/linux/mfd/khadas-mcu.h line 5

Quoted 6 lines
include/linux/mfd/khadas-mcu.h new file +5 -0
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Khadas System control Microcontroller Register map
+ *
+ * Copyright (C) 2020 BayLibre SAS

Nit: '\n'

Ok

Quoted 10 lines

include/linux/mfd/khadas-mcu.h line 12

Quoted 8 lines
include/linux/mfd/khadas-mcu.h new file +7 -0
+ * Author(s): Neil Armstrong <[email protected]>
+ */
+
+#ifndef MFD_KHADAS_MCU_H
+#define MFD_KHADAS_MCU_H
+
+#define KHADAS_MCU_PASSWD_VEN_0_REG		0x0 /* RO */

Nit: Can you pad these please?

Ok

Quoted 55 lines
Quoted 55 lines
include/linux/mfd/khadas-mcu.h new file +54 -0
+#define KHADAS_MCU_PASSWD_VEN_1_REG		0x1 /* RO */
+#define KHADAS_MCU_PASSWD_VEN_2_REG		0x2 /* RO */
+#define KHADAS_MCU_PASSWD_VEN_3_REG		0x3 /* RO */
+#define KHADAS_MCU_PASSWD_VEN_4_REG		0x4 /* RO */
+#define KHADAS_MCU_PASSWD_VEN_5_REG		0x5 /* RO */
+#define KHADAS_MCU_MAC_0_REG			0x6 /* RO */
+#define KHADAS_MCU_MAC_1_REG			0x7 /* RO */
+#define KHADAS_MCU_MAC_2_REG			0x8 /* RO */
+#define KHADAS_MCU_MAC_3_REG			0x9 /* RO */
+#define KHADAS_MCU_MAC_4_REG			0xa /* RO */
+#define KHADAS_MCU_MAC_5_REG			0xb /* RO */
+#define KHADAS_MCU_USID_0_REG			0xc /* RO */
+#define KHADAS_MCU_USID_1_REG			0xd /* RO */
+#define KHADAS_MCU_USID_2_REG			0xe /* RO */
+#define KHADAS_MCU_USID_3_REG			0xf /* RO */
+#define KHADAS_MCU_USID_4_REG			0x10 /* RO */
+#define KHADAS_MCU_USID_5_REG			0x11 /* RO */
+#define KHADAS_MCU_VERSION_0_REG		0x12 /* RO */
+#define KHADAS_MCU_VERSION_1_REG		0x13 /* RO */
+#define KHADAS_MCU_DEVICE_NO_0_REG		0x14 /* RO */
+#define KHADAS_MCU_DEVICE_NO_1_REG		0x15 /* RO */
+#define KHADAS_MCU_FACTORY_TEST_REG		0x16 /* R */
+#define KHADAS_MCU_BOOT_MODE_REG		0x20 /* RW */
+#define KHADAS_MCU_BOOT_EN_WOL_REG		0x21 /* RW */
+#define KHADAS_MCU_BOOT_EN_RTC_REG		0x22 /* RW */
+#define KHADAS_MCU_BOOT_EN_EXP_REG		0x23 /* RW */
+#define KHADAS_MCU_BOOT_EN_IR_REG		0x24 /* RW */
+#define KHADAS_MCU_BOOT_EN_DCIN_REG		0x25 /* RW */
+#define KHADAS_MCU_BOOT_EN_KEY_REG		0x26 /* RW */
+#define KHADAS_MCU_KEY_MODE_REG			0x27 /* RW */
+#define KHADAS_MCU_LED_MODE_ON_REG		0x28 /* RW */
+#define KHADAS_MCU_LED_MODE_OFF_REG		0x29 /* RW */
+#define KHADAS_MCU_SHUTDOWN_NORMAL_REG		0x2c /* RW */
+#define KHADAS_MCU_MAC_SWITCH_REG		0x2d /* RW */
+#define KHADAS_MCU_MCU_SLEEP_MODE_REG		0x2e /* RW */
+#define KHADAS_MCU_IR_CODE1_0_REG		0x2f /* RW */
+#define KHADAS_MCU_IR_CODE1_1_REG		0x30 /* RW */
+#define KHADAS_MCU_IR_CODE1_2_REG		0x31 /* RW */
+#define KHADAS_MCU_IR_CODE1_3_REG		0x32 /* RW */
+#define KHADAS_MCU_USB_PCIE_SWITCH_REG		0x33 /* RW */
+#define KHADAS_MCU_IR_CODE2_0_REG		0x34 /* RW */
+#define KHADAS_MCU_IR_CODE2_1_REG		0x35 /* RW */
+#define KHADAS_MCU_IR_CODE2_2_REG		0x36 /* RW */
+#define KHADAS_MCU_IR_CODE2_3_REG		0x37 /* RW */
+#define KHADAS_MCU_PASSWD_USER_0_REG		0x40 /* RW */
+#define KHADAS_MCU_PASSWD_USER_1_REG		0x41 /* RW */
+#define KHADAS_MCU_PASSWD_USER_2_REG		0x42 /* RW */
+#define KHADAS_MCU_PASSWD_USER_3_REG		0x43 /* RW */
+#define KHADAS_MCU_PASSWD_USER_4_REG		0x44 /* RW */
+#define KHADAS_MCU_PASSWD_USER_5_REG		0x45 /* RW */
+#define KHADAS_MCU_USER_DATA_0_REG		0x46 /* RW 56 bytes */
+#define KHADAS_MCU_PWR_OFF_CMD_REG		0x80 /* WO */
+#define KHADAS_MCU_PASSWD_START_REG		0x81 /* WO */
+#define KHADAS_MCU_CHECK_VEN_PASSWD_REG		0x82 /* WO */
Quoted 8 lines

include/linux/mfd/khadas-mcu.h line 72

Quoted 6 lines
include/linux/mfd/khadas-mcu.h new file +6 -0
+#define KHADAS_MCU_CHECK_USER_PASSWD_REG	0x83 /* WO */
+#define KHADAS_MCU_SHUTDOWN_NORMAL_STATUS_REG	0x86 /* RO */
+#define KHADAS_MCU_WOL_INIT_START_REG		0x87 /* WO */
+#define KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG	0x88 /* WO */
+
+/* Boards */

I think the names make this superfluous.

Ok

Quoted 14 lines

include/linux/mfd/khadas-mcu.h line 82

Quoted 11 lines
include/linux/mfd/khadas-mcu.h new file +10 -0
+enum {
+	KHADAS_BOARD_VIM1 = 0x1,
+	KHADAS_BOARD_VIM2,
+	KHADAS_BOARD_VIM3,
+	KHADAS_BOARD_EDGE = 0x11,
+	KHADAS_BOARD_EDGE_V,
+};
+
+/**
+ * struct khadas_mcu_data - Khadas MCU MFD structure

Doesn't match the struct name>
Prefer you drop the 'MFD' part.

Ok

Quoted 11 lines

include/linux/mfd/khadas-mcu.h line 91

Quoted 10 lines
include/linux/mfd/khadas-mcu.h new file +9 -0
+ * @device:		device reference used for logs
+ * @map:		register map
+ */
+struct khadas_mcu {
+	struct device *dev;
+	struct regmap *map;
+};
+
+#endif /* MFD_KHADAS_MCU_H */

Thanks for the review.

Neil

#10 lore raw

Re: [PATCH v2 3/6] thermal: add support for the MCU controlled FAN on Khadas boards

In reply to Amit Kucheria
Quoted 16 lines · Amit Kucheria
Quoted 15 lines · Neil Armstrong

The new Khadas VIM2 and VIM3 boards controls the cooling fan via the on-board microcontroller.

This implements the FAN control as thermal devices and as cell of the Khadas MCU MFD driver.

---
 drivers/thermal/Kconfig          |  10 ++
 drivers/thermal/Makefile         |   1 +
 drivers/thermal/khadas_mcu_fan.c | 174 +++++++++++++++++++++++++++++++
 3 files changed, 185 insertions(+)
 create mode 100644 drivers/thermal/khadas_mcu_fan.c
Quoted 15 lines

drivers/thermal/Kconfig config SPRD_THERMAL · line 496

Quoted 12 lines
drivers/thermal/Kconfig +4 -0
index 91af271e9bb0..72b3960cc5ac 100644
@@ -490,4 +490,14 @@ config SPRD_THERMAL
        help
          Support for the Spreadtrum thermal sensor driver in the Linux thermal
          framework.
+
+config KHADAS_MCU_FAN_THERMAL
+       tristate "Khadas MCU controller FAN cooling support"
+       depends on OF || COMPILE_TEST

Could you add a depends on the some board/SoC Kconfig option here so this doesn't show up for non-Amlogic/non-Khadas boards?

Ok

Quoted 202 lines

Looks OK otherwise.

Quoted 199 lines
drivers/thermal/Kconfig +6 -0
+       select MFD_CORE
+       select REGMAP
+       help
+         If you say yes here you get support for the FAN controlled
+         by the Microcontroller found on the Khadas VIM boards.
+
 endif
drivers/thermal/Makefile +1 -0
index 8c8ed7b79915..460428c2122c 100644
@@ -60,3 +60,4 @@ obj-$(CONFIG_ZX2967_THERMAL)  += zx2967_thermal.o
 obj-$(CONFIG_UNIPHIER_THERMAL) += uniphier_thermal.o
 obj-$(CONFIG_AMLOGIC_THERMAL)     += amlogic_thermal.o
 obj-$(CONFIG_SPRD_THERMAL)     += sprd_thermal.o
+obj-$(CONFIG_KHADAS_MCU_FAN_THERMAL)   += khadas_mcu_fan.o
drivers/thermal/khadas_mcu_fan.c new file +174 -1
new file mode 100644
index 000000000000..044d4aba8be2
@@ -0,0 +1,174 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Khadas MCU Controlled FAN driver
+ *
+ * Copyright (C) 2020 BayLibre SAS
+ * Author(s): Neil Armstrong <[email protected]>
+ */
+
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/mfd/khadas-mcu.h>
+#include <linux/regmap.h>
+#include <linux/sysfs.h>
+#include <linux/thermal.h>
+
+#define MAX_LEVEL 3
+
+struct khadas_mcu_fan_ctx {
+       struct khadas_mcu *mcu;
+       unsigned int level;
+       struct thermal_cooling_device *cdev;
+};
+
+static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
+                                   unsigned int level)
+{
+       int ret;
+
+       ret = regmap_write(ctx->mcu->map, KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG,
+                          level);
+       if (ret)
+               return ret;
+
+       ctx->level = level;
+
+       return 0;
+}
+
+static int khadas_mcu_fan_get_max_state(struct thermal_cooling_device *cdev,
+                                       unsigned long *state)
+{
+       struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
+
+       if (!ctx)
+               return -EINVAL;
+
+       *state = MAX_LEVEL;
+
+       return 0;
+}
+
+static int khadas_mcu_fan_get_cur_state(struct thermal_cooling_device *cdev,
+                                       unsigned long *state)
+{
+       struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
+
+       if (!ctx)
+               return -EINVAL;
+
+       *state = ctx->level;
+
+       return 0;
+}
+
+static int
+khadas_mcu_fan_set_cur_state(struct thermal_cooling_device *cdev,
+                            unsigned long state)
+{
+       struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
+
+       if (!ctx || (state > MAX_LEVEL))
+               return -EINVAL;
+
+       if (state == ctx->level)
+               return 0;
+
+       return khadas_mcu_fan_set_level(ctx, state);
+}
+
+static const struct thermal_cooling_device_ops khadas_mcu_fan_cooling_ops = {
+       .get_max_state = khadas_mcu_fan_get_max_state,
+       .get_cur_state = khadas_mcu_fan_get_cur_state,
+       .set_cur_state = khadas_mcu_fan_set_cur_state,
+};
+
+static int khadas_mcu_fan_probe(struct platform_device *pdev)
+{
+       struct khadas_mcu *mcu = dev_get_drvdata(pdev->dev.parent);
+       struct thermal_cooling_device *cdev;
+       struct device *dev = &pdev->dev;
+       struct khadas_mcu_fan_ctx *ctx;
+       int ret;
+
+       ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
+       if (!ctx)
+               return -ENOMEM;
+       ctx->mcu = mcu;
+       platform_set_drvdata(pdev, ctx);
+
+       cdev = devm_thermal_of_cooling_device_register(dev->parent,
+                       dev->parent->of_node, "khadas-mcu-fan", ctx,
+                       &khadas_mcu_fan_cooling_ops);
+       if (IS_ERR(cdev)) {
+               ret = PTR_ERR(cdev);
+               dev_err(dev,
+                               "Failed to register khadas-mcu-fan as cooling device: %d\n",
+                               ret);
+               return ret;
+       }
+       ctx->cdev = cdev;
+       thermal_cdev_update(cdev);
+
+       return 0;
+}
+
+static int khadas_mcu_fan_disable(struct device *dev)
+{
+       struct khadas_mcu_fan_ctx *ctx = dev_get_drvdata(dev);
+       unsigned int level_save = ctx->level;
+       int ret;
+
+       ret = khadas_mcu_fan_set_level(ctx, 0);
+       if (ret)
+               return ret;
+
+       ctx->level = level_save;
+
+       return 0;
+}
+
+static void khadas_mcu_fan_shutdown(struct platform_device *pdev)
+{
+       khadas_mcu_fan_disable(&pdev->dev);
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int khadas_mcu_fan_suspend(struct device *dev)
+{
+       return khadas_mcu_fan_disable(dev);
+}
+
+static int khadas_mcu_fan_resume(struct device *dev)
+{
+       struct khadas_mcu_fan_ctx *ctx = dev_get_drvdata(dev);
+
+       return khadas_mcu_fan_set_level(ctx, ctx->level);
+}
+#endif
+
+static SIMPLE_DEV_PM_OPS(khadas_mcu_fan_pm, khadas_mcu_fan_suspend,
+                        khadas_mcu_fan_resume);
+
+static const struct platform_device_id khadas_mcu_fan_id_table[] = {
+       { .name = "khadas-mcu-fan-ctrl", },
+       {},
+};
+MODULE_DEVICE_TABLE(platform, khadas_mcu_fan_id_table);
+
+static struct platform_driver khadas_mcu_fan_driver = {
+       .probe          = khadas_mcu_fan_probe,
+       .shutdown       = khadas_mcu_fan_shutdown,
+       .driver = {
+               .name           = "khadas-mcu-fan-ctrl",
+               .pm             = &khadas_mcu_fan_pm,
+       },
+       .id_table       = khadas_mcu_fan_id_table,
+};
+
+module_platform_driver(khadas_mcu_fan_driver);
+
+MODULE_AUTHOR("Neil Armstrong <[email protected]>");
+MODULE_DESCRIPTION("Khadas MCU FAN driver");
+MODULE_LICENSE("GPL");
--

2.22.0

#15 lore raw

Re: [PATCH v2 4/6] nvmem: add support for the Khadas MCU Programmable User Memory

In reply to Srinivas Kandagatla
Quoted 23 lines · Srinivas Kandagatla
Quoted 10 lines · Neil Armstrong
Quoted 9 lines · Srinivas Kandagatla
Quoted 5 lines · Neil Armstrong

The new Khadas VIM2, VIM3 and Edge boards embeds an on-board microcontroller offering a 56bytes User Programmable NVMEM array.

This array needs a password to be writable, thus a password sysfs file has been added on the device node to unlock the NVMEM.

Quoted 8 lines
Quoted 1 line

Is this one time programmable? Or does it need to be unlocked for every read/write?

It can be rewritten, and needs the password to read & write.

Quoted 2 lines

How can you make sure that the memory is unlocked before making attempt to read/write?

The only way to know it's unlock is reading back the password when unlocked.

Quoted 6 lines

Current code has no such checks for every read/write? and looks very disconnected w.r.t to password and read/writes.

If user attempts to read/write he will not be aware that he should program the password first!

Also if the password is static or un-changed then why not just statically program this from the driver rather than providing sysfs file?

The passworsd can be changed, how should this be taken in account ?

Quoted 2 lines

I dont see how kernel nvmem read/write interface can make sure that memory is unlocked?

Not sure how to be sure, if locked all the user memory and password is read as 0xff

Quoted 2 lines

Who is the real consumer for this provider?

well, it's more user-space, indeed without the in-kernel password unlock, kernel won't be able to make great use of the data.

I'll drop for next serie until we have a clearer view.

Neil

Quoted 214 lines

--srini

Quoted 210 lines
Quoted 55 lines
Quoted 55 lines

The default 6bytes password id: "Khadas"

This implements the user NVMEM devices as cell of the Khadas MCU MFD driver.

---
   drivers/nvmem/Kconfig               |   8 ++ drivers/nvmem/Makefile              |   2 + drivers/nvmem/khadas-mcu-user-mem.c | 128 ++++++++++++++++++++++++++++

   3 files changed, 138 insertions(+)

   create mode 100644 drivers/nvmem/khadas-mcu-user-mem.c

drivers/nvmem/Kconfig +0 -0
index d7b7f6d688e7..92cd4f6aa931 100644
@@ -67,6 +67,14 @@ config JZ4780_EFUSE

         To compile this driver as a module, choose M here: the module will be called nvmem_jz4780_efuse.
   +config NVMEM_KHADAS_MCU_USER_MEM
+    tristate "Khadas MCU User programmable memory support" +    depends on MFD_KHADAS_MCU
+    depends on REGMAP
+    help
+      This is a driver for the MCU User programmable memory +      available on the Khadas VIM and Edge boards.
+
   config NVMEM_LPC18XX_EEPROM
       tristate "NXP LPC18XX EEPROM Memory Support"
       depends on ARCH_LPC18XX || COMPILE_TEST

drivers/nvmem/Makefile +0 -0
index a7c377218341..0516a309542d 100644
@@ -17,6 +17,8 @@ obj-$(CONFIG_NVMEM_IMX_OCOTP_SCU)    += nvmem-imx-ocotp-scu.o

   nvmem-imx-ocotp-scu-y        := imx-ocotp-scu.o obj-$(CONFIG_JZ4780_EFUSE)        += nvmem_jz4780_efuse.o nvmem_jz4780_efuse-y        := jz4780-efuse.o

drivers/nvmem/Makefile +2 -0
+obj-$(CONFIG_NVMEM_KHADAS_MCU_USER_MEM)    += nvmem-khadas-mcu-user-mem.o
+nvmem-khadas-mcu-user-mem-y    := khadas-mcu-user-mem.o

   obj-$(CONFIG_NVMEM_LPC18XX_EEPROM)    += nvmem_lpc18xx_eeprom.o nvmem_lpc18xx_eeprom-y    := lpc18xx_eeprom.o obj-$(CONFIG_NVMEM_LPC18XX_OTP)    += nvmem_lpc18xx_otp.o

drivers/nvmem/khadas-mcu-user-mem.c new file +3 -0
new file mode 100644
index 000000000000..a1d5ae9a030c
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Khadas MCU User programmable Memory
Quoted 8 lines

drivers/nvmem/khadas-mcu-user-mem.c line 9

Quoted 6 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +6 -0
+ *
+ * Copyright (C) 2020 BayLibre SAS
+ * Author(s): Neil Armstrong <[email protected]>
+ */
+
+#include <linux/clk.h>

Why do we need this header?

Will drop

Quoted 30 lines
Quoted 30 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +29 -0
+#include <linux/module.h>
+#include <linux/nvmem-provider.h>
+#include <linux/mfd/khadas-mcu.h>
+#include <linux/regmap.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+
+static int khadas_mcu_user_mem_read(void *context, unsigned int offset,
+                void *val, size_t bytes)
+{
+    struct khadas_mcu *khadas_mcu = context;
+
+    return regmap_bulk_read(khadas_mcu->map,
+                KHADAS_MCU_USER_DATA_0_REG + offset,
+                val, bytes);
+}
+
+static int khadas_mcu_user_mem_write(void *context, unsigned int offset,
+                 void *val, size_t bytes)
+{
+    struct khadas_mcu *khadas_mcu = context;
+
+    return regmap_bulk_write(khadas_mcu->map,
+                KHADAS_MCU_USER_DATA_0_REG + offset,
+                val, bytes);
+}
+
+static ssize_t password_store(struct device *dev, struct device_attribute *attr,
+                 const char *buf, size_t count)
Quoted 8 lines

drivers/nvmem/khadas-mcu-user-mem.c line 44

Quoted 6 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +6 -0
+{
+    struct khadas_mcu *khadas_mcu = dev_get_drvdata(dev);
+    int i, ret;
+
+    if (count < 6)
+        return -EINVAL;

Possibly this magic 6 value needs proper define. An additional check here for > 6 would be better as well.

Ok

Quoted 33 lines
Quoted 33 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +32 -0
+
+    ret = regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 1);
+    if (ret)
+        return ret;
+
+    for (i = 0 ; i < 6 ; ++i) {
+        ret = regmap_write(khadas_mcu->map,
+                   KHADAS_MCU_CHECK_USER_PASSWD_REG,
+                   buf[i]);
+        if (ret)
+            goto out;
+    }
+
+    ret = regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 0);
+    if (ret)
+        return ret;
+
+    return count;
+out:
+    regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 0);
+
+    return ret;
+}
+
+static DEVICE_ATTR_WO(password);
+
+static struct attribute *khadas_mcu_user_mem_sysfs_attributes[] = {
+    &dev_attr_password.attr,
+    NULL,
+};
+
+static const struct attribute_group khadas_mcu_user_mem_sysfs_attr_group = {
Quoted 8 lines

drivers/nvmem/khadas-mcu-user-mem.c line 82

Quoted 6 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +6 -0
+    .attrs = khadas_mcu_user_mem_sysfs_attributes,
+};
+
+static int khadas_mcu_user_mem_probe(struct platform_device *pdev)
+{
+    struct khadas_mcu *khadas_mcu = dev_get_drvdata(pdev->dev.parent);

You could probably get away with dependency of this structure and the header itself by directly getting hold of regmap from parent device.

Ok

Quoted 18 lines
Quoted 7 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +5 -0
+    struct device *dev = &pdev->dev;
+    struct nvmem_device *nvmem;
+    struct nvmem_config *econfig;
+
+    econfig = devm_kzalloc(dev, sizeof(*econfig), GFP_KERNEL);

drivers/nvmem/khadas-mcu-user-mem.c line 97

Quoted 10 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +10 -0
+    if (!econfig)
+        return -ENOMEM;
+
+    econfig->dev = pdev->dev.parent;
+    econfig->name = dev_name(pdev->dev.parent);
+    econfig->stride = 1;
+    econfig->word_size = 1;
+    econfig->reg_read = khadas_mcu_user_mem_read;
+    econfig->reg_write = khadas_mcu_user_mem_write;
+    econfig->size = 56;

define 56 to make it more readable!

Ok

Quoted 25 lines
Quoted 25 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +24 -0
+    econfig->priv = khadas_mcu;
+
+    platform_set_drvdata(pdev, khadas_mcu);
+
+    nvmem = devm_nvmem_register(&pdev->dev, econfig);
+    if (IS_ERR(nvmem))
+        return PTR_ERR(nvmem);
+
+    return sysfs_create_group(&pdev->dev.kobj,
+                  &khadas_mcu_user_mem_sysfs_attr_group);
+}
+
+static const struct platform_device_id khadas_mcu_user_mem_id_table[] = {
+    { .name = "khadas-mcu-user-mem", },
+    {},
+};
+MODULE_DEVICE_TABLE(platform, khadas_mcu_user_mem_id_table);
+
+static struct platform_driver khadas_mcu_user_mem_driver = {
+    .probe = khadas_mcu_user_mem_probe,
+    .driver = {
+        .name = "khadas-mcu-user-mem",
+    },
+    .id_table = khadas_mcu_user_mem_id_table,
Quoted 8 lines

drivers/nvmem/khadas-mcu-user-mem.c line 128

Quoted 8 lines
drivers/nvmem/khadas-mcu-user-mem.c new file +7 -0
+};
+
+module_platform_driver(khadas_mcu_user_mem_driver);
+
+MODULE_AUTHOR("Neil Armstrong <[email protected]>");
+MODULE_DESCRIPTION("Khadas MCU User MEM driver");
+MODULE_LICENSE("GPL v2");

Thanks for the review.

Neil

#6 lore raw

Re: [PATCH v2 2/6] mfd: add support for the Khadas System control Microcontroller

In reply to Neil Armstrong
Quoted 46 lines · Neil Armstrong
Quoted 29 lines · Lee Jones
Quoted 27 lines · Neil Armstrong

This Microcontroller is present on the Khadas VIM1, VIM2, VIM3 and Edge boards.

It has multiple boot control features like password check, power-on options, power-off control and system FAN control on recent boards.

This implements a very basic MFD driver with the fan control and User NVMEM cells.

---
 drivers/mfd/Kconfig            |  14 ++++
 drivers/mfd/Makefile           |   1 +
 drivers/mfd/khadas-mcu.c       | 143 +++++++++++++++++++++++++++++++++
 include/linux/mfd/khadas-mcu.h |  91 +++++++++++++++++++++
 4 files changed, 249 insertions(+)
 create mode 100644 drivers/mfd/khadas-mcu.c
 create mode 100644 include/linux/mfd/khadas-mcu.h
drivers/mfd/Kconfig +0 -0
index 0a59249198d3..b95091397052 100644
@@ -2003,6 +2003,20 @@ config MFD_WCD934X
 	  This driver provides common support WCD934x audio codec and its
 	  associated Pin Controller, Soundwire Controller and Audio codec.
 
Quoted 15 lines

drivers/mfd/Kconfig config MFD_WCD934X · line 2018

Quoted 13 lines
drivers/mfd/Kconfig +13 -0
+config MFD_KHADAS_MCU
+	tristate "Support for Khadas System control Microcontroller"
+	depends on I2C
+	depends on OF || COMPILE_TEST
+	select MFD_CORE
+	select REGMAP_I2C
+	help
+	  Support for the Khadas System control Microcontroller interface present
+	  on their VIM and Edge boards.
+
+	  This driver provides common support for accessing the device,
+	  additional drivers must be enabled in order to use the functionality
+	  of the device.

It would be good to describe the device here.

Quoted 1 line

Ok

If you agree with all review comments, there really is no need to reply. It's a waste of your time and anyone else who cares enough to search through looking for replies (as I just did).

Signature · 4 lines
Lee Jones [李琼斯]
Linaro Services Technical Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog