Skip to contentSkip to conversation

VIM3: add support for checking 'Function' button state

PATCH Superseded Marek Szyprowski 3 participants 15 messages Dec 15, 2020 – Dec 19, 2020 1 message not in the archive

Subsystems (3)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CGME20201215144242eucas1p1c837ea82b7365808b5384c9eb204b593@eucas1p1.samsung.com

#1 lore raw

v2 cover VIM3: add support for checking 'Function' button state

Hi All,

This patchset adds all building blocks needed for checking the 'Function' button state in the boot script on Amlogic A311D based VIM3 board. This button is connected to the ADC lines of the SoC, so it required to enable meson SARADC, the clocks needed for it and a simple button-adc drivers.

Once applied, one can use following commands in the boot scripts:
-->8---
echo Checking Func button state: \\c
button Function
if test ${button} = on
then

	echo Selected alternative boot
	...

fi
--->8---

Best regards
Marek Szyprowski
Samsung R&D Institute Poland

Changelog:
v2:
- removed Change-Id tags
- split defconfig changes into ADC and button related

v1: https://lists.denx.de/pipermail/u-boot/2020-December/434875.html
- initial submission

Patch summary:

Marek Szyprowski (7):
clk: meson: add minimal driver for g12a-ao clocks
adc: meson-saradc: add G12A variant
adc: meson-saradc: skip hardware init only if ADC is enabled
configs: khadas-vim3: enable ADC device support
button: add a simple ADC-based button driver
cmd: button: store button state in the 'button' env
configs: khadas-vim3: enable Function button support

 cmd/button.c                  |   4 +-
 configs/khadas-vim3_defconfig |   4 ++
 drivers/adc/meson-saradc.c    |   9 ++-
 drivers/button/Kconfig        |   8 +++
 drivers/button/Makefile       |   1 +
 drivers/button/button-adc.c   | 117 ++++++++++++++++++++++++++++++++++
 drivers/clk/meson/Makefile    |   1 +
 drivers/clk/meson/g12a-ao.c   |  83 ++++++++++++++++++++++++
 8 files changed, 224 insertions(+), 3 deletions(-)
 create mode 100644 drivers/button/button-adc.c
 create mode 100644 drivers/clk/meson/g12a-ao.c
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 1/7 clk: meson: add minimal driver for g12a-ao clocks

Add minimal driver AO clocks on meson G12A family. Only ADC related clocks are supported.

---
 drivers/clk/meson/Makefile  |  1 +
 drivers/clk/meson/g12a-ao.c | 83 +++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+)
 create mode 100644 drivers/clk/meson/g12a-ao.c
drivers/clk/meson/Makefile +1 -0
index c873d6976f..7204383e17 100644
@@ -6,4 +6,5 @@
 obj-$(CONFIG_CLK_MESON_GX) += gxbb.o
 obj-$(CONFIG_CLK_MESON_AXG) += axg.o
 obj-$(CONFIG_CLK_MESON_G12A) += g12a.o
+obj-$(CONFIG_CLK_MESON_G12A) += g12a-ao.o
 
drivers/clk/meson/g12a-ao.c new file +83 -0

83 lines not on this page · raw message

Signature · 1 line
2.17.1
#3 lore raw

PATCH v2 2/7 adc: meson-saradc: add G12A variant

In reply to Marek Szyprowski

Add support for the SARADC variant found on the G12A SoCs family.

---
 drivers/adc/meson-saradc.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/adc/meson-saradc.c +2 -0
index 72b0cc4e5b..998cef24d8 100644
@@ -711,6 +711,8 @@ static const struct udevice_id meson_saradc_ids[] = {
 	  .data = (ulong)&gxl_saradc_data },
 	{ .compatible = "amlogic,meson-gxm-saradc",
 	  .data = (ulong)&gxl_saradc_data },
+	{ .compatible = "amlogic,meson-g12a-saradc",
+	  .data = (ulong)&gxl_saradc_data },
 	{ }
 };
 
Signature · 1 line
2.17.1
#4 lore raw

PATCH v2 3/7 adc: meson-saradc: skip hardware init only if ADC is enabled

In reply to Marek Szyprowski

The driver skips hardware initialization if it is already configured by the earlier bootloader stage (BL30). Skip the initialization only if the hardware is really initialized and enabled.

---
 drivers/adc/meson-saradc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/adc/meson-saradc.c +5 -2
index 998cef24d8..ce7ae990ad 100644
@@ -512,8 +512,11 @@ static int meson_saradc_init(struct meson_saradc_priv *priv)
 	 * reading the temperature sensor.
 	 */
 	regmap_read(priv->regmap, MESON_SAR_ADC_REG3, &regval);
-	if (regval & MESON_SAR_ADC_REG3_BL30_INITIALIZED)
-		return 0;
+	if (regval & MESON_SAR_ADC_REG3_BL30_INITIALIZED) {
+		regmap_read(priv->regmap, MESON_SAR_ADC_REG3, &regval);
+		if (regval & MESON_SAR_ADC_REG3_ADC_EN)
+			return 0;
+	}
 
 	meson_saradc_stop_sample_engine(priv);
 
Signature · 1 line
2.17.1
#5 lore raw

PATCH v2 4/7 configs: khadas-vim3: enable ADC device support

In reply to Marek Szyprowski

ADC device (Meson SARADC) will be used for probing 'Function' button state.

---
 configs/khadas-vim3_defconfig | 2 ++
 1 file changed, 2 insertions(+)
configs/khadas-vim3_defconfig +2 -0
index 9d7ba72d44..5d16652fd6 100644
@@ -29,6 +29,8 @@ CONFIG_CMD_REGULATOR=y
 CONFIG_OF_CONTROL=y
 CONFIG_SYS_RELOC_GD_ENV_ADDR=y
 CONFIG_NET_RANDOM_ETHADDR=y
+CONFIG_ADC=y
+CONFIG_SARADC_MESON=y
 CONFIG_DM_I2C=y
 CONFIG_SYS_I2C_MESON=y
 CONFIG_DM_MMC=y
Signature · 1 line
2.17.1
#6 lore raw

PATCH v2 5/7 button: add a simple ADC-based button driver

In reply to Marek Szyprowski

Add a simple ADC-based button driver. This driver binds to the 'adc-keys' device tree node.

---
 drivers/button/Kconfig      |   8 +++
 drivers/button/Makefile     |   1 +
 drivers/button/button-adc.c | 117 ++++++++++++++++++++++++++++++++++++
 3 files changed, 126 insertions(+)
 create mode 100644 drivers/button/button-adc.c
drivers/button/Kconfig +8 -0
index 6b3ec7e55d..283367f2bd 100644
@@ -9,6 +9,14 @@ config BUTTON
 	  can provide access to board-specific buttons. Use of the device tree
 	  for configuration is encouraged.
 
+config BUTTON_ADC
+	bool "Button adc"
+	depends on BUTTON
+	help
+	  Enable support for buttons which are connected to ADC lines. The ADC
+	  driver must use driver model. Buttons are configured using the device
+	  tree.
+
 config BUTTON_GPIO
 	bool "Button gpio"
 	depends on BUTTON
drivers/button/Makefile +1 -0
index fcc10ebe8d..bbd18af149 100644
@@ -3,4 +3,5 @@
 # Copyright (C) 2020 Philippe Reynes <[email protected]>
 
 obj-$(CONFIG_BUTTON) += button-uclass.o
+obj-$(CONFIG_BUTTON_ADC) += button-adc.o
 obj-$(CONFIG_BUTTON_GPIO) += button-gpio.o
drivers/button/button-adc.c new file +117 -0

117 lines not on this page · raw message

Signature · 1 line
2.17.1
#7 lore raw

PATCH v2 5/7 button: add a simple ADC-based button driver

Quoted 16 lines · Marek Szyprowski

Add a simple ADC-based button driver. This driver binds to the 'adc-keys' device tree node.

---
  drivers/button/Kconfig      |   8 +++
  drivers/button/Makefile     |   1 +
  drivers/button/button-adc.c | 117 ++++++++++++++++++++++++++++++++++++
  3 files changed, 126 insertions(+)

create mode 100644 drivers/button/button-adc.c

drivers/button/Kconfig +0 -0
index 6b3ec7e55d..283367f2bd 100644

drivers/button/Kconfig config BUTTON · line 16

Quoted 8 lines
drivers/button/Kconfig +5 -0
@@ -9,6 +9,14 @@ config BUTTON
  	  can provide access to board-specific buttons. Use of the device tree
  	  for configuration is encouraged.
 
+config BUTTON_ADC
+	bool "Button adc"
+	depends on BUTTON
+	help
+	  Enable support for buttons which are connected to ADC lines. The ADC

ADC might stand for "Analog to Digital Converter", or "Automated Data Collection", or "Automatic Distance Control, or something else. Please, replace the abbreviation by the full expression.

Best regards

Heinrich

Quoted 140 lines
drivers/button/Kconfig +3 -0
+	  driver must use driver model. Buttons are configured using the device
+	  tree.
+
  config BUTTON_GPIO
  	bool "Button gpio"
  	depends on BUTTON
drivers/button/Makefile +1 -0
index fcc10ebe8d..bbd18af149 100644
@@ -3,4 +3,5 @@
  # Copyright (C) 2020 Philippe Reynes <[email protected]>
 
  obj-$(CONFIG_BUTTON) += button-uclass.o
+obj-$(CONFIG_BUTTON_ADC) += button-adc.o
  obj-$(CONFIG_BUTTON_GPIO) += button-gpio.o
drivers/button/button-adc.c new file +117 -0
new file mode 100644
index 0000000000..086c676c02
@@ -0,0 +1,117 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020 Samsung Electronics Co., Ltd.
+ *		http://www.samsung.com
+ * Author: Marek Szyprowski <[email protected]>
+ */
+
+#include <common.h>
+#include <button.h>
+#include <dm.h>
+#include <dm/lists.h>
+#include <dm/uclass-internal.h>
+#include <log.h>
+#include <adc.h>
+
+struct button_adc_priv {
+	struct udevice *adc;
+	int channel;
+};
+
+static enum button_state_t button_adc_get_state(struct udevice *dev)
+{
+	struct button_adc_priv *priv = dev_get_priv(dev);
+	unsigned int val, mask;
+	int ret;
+
+	ret = adc_start_channel(priv->adc, priv->channel);
+	if (ret)
+		return ret;
+
+	ret = adc_channel_data(priv->adc, priv->channel, &val);
+	if (ret)
+		return ret;
+
+	ret = adc_data_mask(priv->adc, &mask);
+	if (ret)
+		return ret;
+
+	/* getting state is simplified a bit */
+	if (ret == 0)
+		return (val < mask / 2) ? BUTTON_ON : BUTTON_OFF;
+
+	return ret;
+}
+
+static int button_adc_probe(struct udevice *dev)
+{
+	struct button_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
+	struct button_adc_priv *priv = dev_get_priv(dev);
+	struct ofnode_phandle_args args;
+	int ret;
+
+	/* Ignore the top-level button node */
+	if (!uc_plat->label)
+		return 0;
+
+	ret = dev_read_phandle_with_args(dev->parent, "io-channels",
+					 "#io-channel-cells", 0, 0, &args);
+	if (ret)
+		return ret;
+
+	ret = uclass_get_device_by_name(UCLASS_ADC, ofnode_get_name(args.node),
+					&priv->adc);
+	if (ret)
+		return ret;
+
+	priv->channel = args.args[0];
+
+	return ret;
+}
+
+static int button_adc_bind(struct udevice *parent)
+{
+	struct udevice *dev;
+	ofnode node;
+	int ret;
+
+	dev_for_each_subnode(node, parent) {
+		struct button_uc_plat *uc_plat;
+		const char *label;
+
+		label = ofnode_read_string(node, "label");
+		if (!label) {
+			debug("%s: node %s has no label\n", __func__,
+			      ofnode_get_name(node));
+			return -EINVAL;
+		}
+		ret = device_bind_driver_to_node(parent, "button_adc",
+						 ofnode_get_name(node),
+						 node, &dev);
+		if (ret)
+			return ret;
+		uc_plat = dev_get_uclass_platdata(dev);
+		uc_plat->label = label;
+	}
+
+	return 0;
+}
+
+static const struct button_ops button_adc_ops = {
+	.get_state	= button_adc_get_state,
+};
+
+static const struct udevice_id button_adc_ids[] = {
+	{ .compatible = "adc-keys" },
+	{ }
+};
+
+U_BOOT_DRIVER(button_adc) = {
+	.name		= "button_adc",
+	.id		= UCLASS_BUTTON,
+	.of_match	= button_adc_ids,
+	.ops		= &button_adc_ops,
+	.priv_auto_alloc_size = sizeof(struct button_adc_priv),
+	.bind		= button_adc_bind,
+	.probe		= button_adc_probe,
+};
#8 lore raw

PATCH v2 6/7 cmd: button: store button state in the 'button' env

In reply to Marek Szyprowski

Save examined button state in 'button' environment variable to enable checking button state in the scripts.

Change-Id I78b539e1516573fcfea4401f75469291844daae4
---
 cmd/button.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
cmd/button.c +3 -1
index 64c5a8fa04..8da911068a 100644
@@ -23,8 +23,10 @@ static int show_button_state(struct udevice *dev)
 	ret = button_get_state(dev);
 	if (ret >= BUTTON_COUNT)
 		ret = -EINVAL;
-	if (ret >= 0)
+	if (ret >= 0) {
 		printf("%s\n", state_label[ret]);
+		env_set("button", state_label[ret]);
+	}
 
 	return ret;
 }
Signature · 1 line
2.17.1
#9 lore raw

PATCH RESEND v2 6/7 cmd: button: store button state in the 'button' env

Save examined button state in 'button' environment variable to enable checking button state in the scripts.

---
Resend reason: get rid of the Change-Id tag, that was still in v2.

---
 cmd/button.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
cmd/button.c +3 -1
index 64c5a8fa04..8da911068a 100644
@@ -23,8 +23,10 @@ static int show_button_state(struct udevice *dev)
 	ret = button_get_state(dev);
 	if (ret >= BUTTON_COUNT)
 		ret = -EINVAL;
-	if (ret >= 0)
+	if (ret >= 0) {
 		printf("%s\n", state_label[ret]);
+		env_set("button", state_label[ret]);
+	}
 
 	return ret;
 }
Signature · 1 line
2.17.1
#10 lore raw

PATCH RESEND v2 6/7 cmd: button: store button state in the 'button' env

Quoted 15 lines · Marek Szyprowski

Save examined button state in 'button' environment variable to enable checking button state in the scripts.

---
Resend reason: get rid of the Change-Id tag, that was still in v2.

---
  cmd/button.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
cmd/button.c +0 -0
index 64c5a8fa04..8da911068a 100644

cmd/button.c static int show_button_state(struct udevice *dev) · line 28

Quoted 7 lines
cmd/button.c +2 -1
@@ -23,8 +23,10 @@ static int show_button_state(struct udevice *dev)
  	ret = button_get_state(dev);
  	if (ret >= BUTTON_COUNT)
  		ret = -EINVAL;
-	if (ret >= 0)
+	if (ret >= 0) {
  		printf("%s\n", state_label[ret]);
+		env_set("button", state_label[ret]);

Using a hard coded environment variable does not make much sense to me. The button command has a return value. So just use

button mybutton; setenv myvar $?

Best regards

Heinrich

Quoted 5 lines
cmd/button.c +1 -0
+	}
 
  	return ret;
  }
#11 lore raw

PATCH RESEND v2 6/7 cmd: button: store button state in the 'button' env

Quoted 27 lines · Heinrich Schuchardt
Quoted 15 lines · Marek Szyprowski

Save examined button state in 'button' environment variable to enable checking button state in the scripts.

---
Resend reason: get rid of the Change-Id tag, that was still in v2.
---
? cmd/button.c | 4 +++-
? 1 file changed, 3 insertions(+), 1 deletion(-)

cmd/button.c +0 -0
index 64c5a8fa04..8da911068a 100644

cmd/button.c static int show_button_state(struct udevice *dev) · line 28

Quoted 7 lines
cmd/button.c +2 -1
@@ -23,8 +23,10 @@ static int show_button_state(struct udevice *dev)
 ????? ret = button_get_state(dev);
 ????? if (ret >= BUTTON_COUNT)
 ????????? ret = -EINVAL;
-??? if (ret >= 0)
+??? if (ret >= 0) {
 ????????? printf("%s\n", state_label[ret]);
+??????? env_set("button", state_label[ret]);

Using a hard coded environment variable does not make much sense to me. The button command has a return value. So just use

Quoted 2 lines

button mybutton; setenv myvar $?

Thanks for the hint, I wasn't aware that uboot supports '$?'. By setting the 'button' env variable I've tried to mimic the behavior of the various network and file related commands, which sets 'filesize' env variable.

I will need to add the return value propagation to the button command anyway to make it usable from the scripts.

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
#12 lore raw

PATCH RESEND v2 6/7 cmd: button: store button state in the 'button' env

Quoted 30 lines · Marek Szyprowski
Quoted 27 lines · Heinrich Schuchardt
Quoted 15 lines · Marek Szyprowski

Save examined button state in 'button' environment variable to enable checking button state in the scripts.

---
Resend reason: get rid of the Change-Id tag, that was still in v2.

---
 ? cmd/button.c | 4 +++-

? 1 file changed, 3 insertions(+), 1 deletion(-)

cmd/button.c +0 -0
index 64c5a8fa04..8da911068a 100644

cmd/button.c static int show_button_state(struct udevice *dev) · line 28

Quoted 7 lines
cmd/button.c +2 -1
@@ -23,8 +23,10 @@ static int show_button_state(struct udevice *dev)
 ????? ret = button_get_state(dev);
 ????? if (ret >= BUTTON_COUNT)
 ????????? ret = -EINVAL;
-??? if (ret >= 0)
+??? if (ret >= 0) {
 ????????? printf("%s\n", state_label[ret]);
+??????? env_set("button", state_label[ret]);

Using a hard coded environment variable does not make much sense to me. The button command has a return value. So just use

Quoted 2 lines

button mybutton; setenv myvar $?

Quoted 7 lines

Thanks for the hint, I wasn't aware that uboot supports '$?'. By setting the 'button' env variable I've tried to mimic the behavior of the various network and file related commands, which sets 'filesize' env variable.

I will need to add the return value propagation to the button command anyway to make it usable from the scripts.

Nothing to be done for the button command. Since

a6bfd71a96201127836d59736abcb54dc2d5e1a5
cmd/button: return button status

the button command returns

0 (true) - pressed, on
1 (false) - not pressed, off

Best regards

Heinrich

#13 lore raw

PATCH RESEND v2 6/7 cmd: button: store button state in the 'button' env

Quoted 41 lines · Heinrich Schuchardt
Quoted 30 lines · Marek Szyprowski
Quoted 27 lines · Heinrich Schuchardt
Quoted 15 lines · Marek Szyprowski

Save examined button state in 'button' environment variable to enable checking button state in the scripts.

---
Resend reason: get rid of the Change-Id tag, that was still in v2.
---
?? cmd/button.c | 4 +++-
?? 1 file changed, 3 insertions(+), 1 deletion(-)

cmd/button.c +0 -0
index 64c5a8fa04..8da911068a 100644

cmd/button.c static int show_button_state(struct udevice *dev) · line 28

Quoted 7 lines
cmd/button.c +2 -1
@@ -23,8 +23,10 @@ static int show_button_state(struct udevice *dev)
 ?????? ret = button_get_state(dev);
 ?????? if (ret >= BUTTON_COUNT)
 ?????????? ret = -EINVAL;
-??? if (ret >= 0)
+??? if (ret >= 0) {
 ?????????? printf("%s\n", state_label[ret]);
+??????? env_set("button", state_label[ret]);

Using a hard coded environment variable does not make much sense to me. The button command has a return value. So just use

Quoted 2 lines

button mybutton; setenv myvar $?

Quoted 7 lines

Thanks for the hint, I wasn't aware that uboot supports '$?'. By setting the 'button' env variable I've tried to mimic the behavior of the various network and file related commands, which sets 'filesize' env variable.

I will need to add the return value propagation to the button command anyway to make it usable from the scripts.

Nothing to be done for the button command. Since

Quoted 8 lines

??? a6bfd71a96201127836d59736abcb54dc2d5e1a5
??? cmd/button: return button status

the button command returns

??? 0 (true) - pressed, on
??? 1 (false) - not pressed, off

Right, I've missed that commit. I've developed my code on top of v2020.10 release.

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
#14 lore raw

PATCH RESEND v2 6/7 cmd: button: store button state in the 'button' env

In reply to Marek Szyprowski

Hi Marek,

Quoted 7 lines · Marek Szyprowski

Save examined button state in 'button' environment variable to enable checking button state in the scripts.

---
Resend reason: get rid of the Change-Id tag, that was still in v2.

If you use patman it does that for you.

Regards,
Simon

#15 lore raw

PATCH v2 7/7 configs: khadas-vim3: enable Function button support

In reply to Marek Szyprowski

Add options needed to check the 'Function' button state.

---
 configs/khadas-vim3_defconfig | 2 ++
 1 file changed, 2 insertions(+)
configs/khadas-vim3_defconfig +2 -0
index 5d16652fd6..bc17430569 100644
@@ -31,6 +31,8 @@ CONFIG_SYS_RELOC_GD_ENV_ADDR=y
 CONFIG_NET_RANDOM_ETHADDR=y
 CONFIG_ADC=y
 CONFIG_SARADC_MESON=y
+CONFIG_BUTTON=y
+CONFIG_BUTTON_ADC=y
 CONFIG_DM_I2C=y
 CONFIG_SYS_I2C_MESON=y
 CONFIG_DM_MMC=y
Signature · 1 line
2.17.1