Skip to contentSkip to conversation

Add adc uclass's channel mask and conversion helpers and enhance adc cmd

PATCH New Fabrice Gasnier 3 participants 13 messages Nov 12, 2018 – Nov 20, 2018

lore
Share & tools
lore
#0 lore raw

v2 cover Add adc uclass's channel mask and conversion helpers and enhance adc cmd

This series adds ADC uclass's helpers to
- retrieve the ADC device available channels
- ease conversions to standard unit (µV)
- enhance 'adc' command to print information on available ADC channels
- enhance 'adc' command to print conversion result both as raw and µV
- enhance 'adc' command to scan once several or all channels

Changes in v2:
- Add calls to the new functions in test/dm/adc.c as suggested by Simon

Fabrice Gasnier (4):
dm: adc: add uclass's mask and conversion helpers
cmd: adc: add info on channel mask
cmd: adc: print single conversion also in uV
cmd: adc: add an option to scan some or all available channels

 cmd/adc.c                | 70 +++++++++++++++++++++++++++++++++++++++++++++---
 drivers/adc/adc-uclass.c | 37 +++++++++++++++++++++++++
 include/adc.h            | 21 +++++++++++++++
 test/dm/adc.c            | 36 +++++++++++++++++++++++++
 4 files changed, 160 insertions(+), 4 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH v2 1/4 dm: adc: add uclass's mask and conversion helpers

Add two functions to ADC uclass's:
- adc_raw_to_uV() to ease ADC raw value conversion to microvolts
- adc_channel_mask() to get channels on consumer side

---

Changes in v2:
- Add calls to the new functions in test/dm/adc.c as suggested by Simon

 drivers/adc/adc-uclass.c | 37 +++++++++++++++++++++++++++++++++++++
 include/adc.h            | 21 +++++++++++++++++++++
 test/dm/adc.c            | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+)
drivers/adc/adc-uclass.c +37 -0
index 738c1ea..0a492eb 100644
@@ -6,6 +6,7 @@
 
 #include <common.h>
 #include <errno.h>
+#include <div64.h>
 #include <dm.h>
 #include <dm/lists.h>
 #include <dm/device-internal.h>
@@ -77,6 +78,18 @@ int adc_data_mask(struct udevice *dev, unsigned int *data_mask)
 	return 0;
 }
 
+int adc_channel_mask(struct udevice *dev, unsigned int *channel_mask)
+{
+	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
+
+	if (!uc_pdata)
+		return -ENOSYS;
+
+	*channel_mask = uc_pdata->channel_mask;
+
+	return 0;
+}
+
 int adc_stop(struct udevice *dev)
 {
 	const struct adc_ops *ops = dev_get_driver_ops(dev);
@@ -329,6 +342,30 @@ int adc_vss_value(struct udevice *dev, int *uV)
 	return 0;
 }
 
+int adc_raw_to_uV(struct udevice *dev, unsigned int raw, int *uV)
+{
+	unsigned int data_mask;
+	int ret, val, vref;
+	u64 raw64 = raw;
+
+	ret = adc_vdd_value(dev, &vref);
+	if (ret)
+		return ret;
+
+	if (!adc_vss_value(dev, &val))
+		vref -= val;
+
+	ret = adc_data_mask(dev, &data_mask);
+	if (ret)
+		return ret;
+
+	raw64 *= vref;
+	do_div(raw64, data_mask);
+	*uV = raw64;
+
+	return 0;
+}
+
 static int adc_vdd_platdata_set(struct udevice *dev)
 {
 	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
include/adc.h +21 -0
index d04c9c4..5841dfb 100644
@@ -219,6 +219,17 @@ int adc_channels_data(struct udevice *dev, unsigned int channel_mask,
 int adc_data_mask(struct udevice *dev, unsigned int *data_mask);
 
 /**
+ * adc_channel_mask() - get channel mask for given ADC device
+ *
+ * This can be used if adc uclass platform data is filled.
+ *
+ * @dev:       ADC device to check
+ * @channel_mask: pointer to the returned channel bitmask
+ * @return: 0 if OK, -ve on error
+ */
+int adc_channel_mask(struct udevice *dev, unsigned int *channel_mask);
+
+/**
  * adc_channel_single_shot() - get output data of conversion for the ADC
  * device's channel. This function searches for the device with the given name,
  * starts the given channel conversion and returns the output data.
@@ -284,4 +295,14 @@ int adc_vss_value(struct udevice *dev, int *uV);
  */
 int adc_stop(struct udevice *dev);
 
+/**
+ * adc_raw_to_uV() - converts raw value to microvolts for given ADC device.
+ *
+ * @dev:     ADC device used from conversion
+ * @raw:     raw value to convert
+ * @uV:	     converted value in microvolts
+ * @return:  0 on success or -ve on error
+ */
+int adc_raw_to_uV(struct udevice *dev, unsigned int raw, int *uV);
+
 #endif
test/dm/adc.c +36 -0
index 13eda3b..904c449 100644
@@ -22,10 +22,14 @@
 static int dm_test_adc_bind(struct unit_test_state *uts)
 {
 	struct udevice *dev;
+	unsigned int channel_mask;
 
 	ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
 	ut_asserteq_str(SANDBOX_ADC_DEVNAME, dev->name);
 
+	ut_assertok(adc_channel_mask(dev, &channel_mask));
+	ut_asserteq((1 << SANDBOX_ADC_CHANNELS) - 1, channel_mask);
+
 	return 0;
 }
 DM_TEST(dm_test_adc_bind, DM_TESTF_SCAN_FDT);
@@ -160,3 +164,35 @@ static int dm_test_adc_multi_channel_shot(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_adc_multi_channel_shot, DM_TESTF_SCAN_FDT);
+
+
+static const int dm_test_adc_uV_data[SANDBOX_ADC_CHANNELS] = {
+	((u64)SANDBOX_ADC_CHANNEL0_DATA * SANDBOX_BUCK2_INITIAL_EXPECTED_UV) /
+		SANDBOX_ADC_DATA_MASK,
+	((u64)SANDBOX_ADC_CHANNEL1_DATA * SANDBOX_BUCK2_INITIAL_EXPECTED_UV) /
+		SANDBOX_ADC_DATA_MASK,
+	((u64)SANDBOX_ADC_CHANNEL2_DATA * SANDBOX_BUCK2_INITIAL_EXPECTED_UV) /
+		SANDBOX_ADC_DATA_MASK,
+	((u64)SANDBOX_ADC_CHANNEL3_DATA * SANDBOX_BUCK2_INITIAL_EXPECTED_UV) /
+		SANDBOX_ADC_DATA_MASK,
+};
+
+static int dm_test_adc_raw_to_uV(struct unit_test_state *uts)
+{
+	struct adc_channel *tdata = adc_channel_test_data;
+	unsigned int i, data;
+	struct udevice *dev;
+	int uV;
+
+	ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
+	/* Test each ADC channel's value in microvolts */
+	for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++) {
+		ut_assertok(adc_start_channel(dev, tdata->id));
+		ut_assertok(adc_channel_data(dev, tdata->id, &data));
+		ut_assertok(adc_raw_to_uV(dev, data, &uV));
+		ut_asserteq(dm_test_adc_uV_data[i], uV);
+	}
+
+	return 0;
+}
+DM_TEST(dm_test_adc_raw_to_uV, DM_TESTF_SCAN_FDT);
Signature · 1 line
1.9.1
#2 lore raw

PATCH v2 1/4 dm: adc: add uclass's mask and conversion helpers

Quoted 7 lines · Fabrice Gasnier

Add two functions to ADC uclass's:
- adc_raw_to_uV() to ease ADC raw value conversion to microvolts
- adc_channel_mask() to get channels on consumer side

---

Quoted 7 lines

Changes in v2:
- Add calls to the new functions in test/dm/adc.c as suggested by Simon

 drivers/adc/adc-uclass.c | 37 +++++++++++++++++++++++++++++++++++++
 include/adc.h            | 21 +++++++++++++++++++++
 test/dm/adc.c            | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+)
#3 lore raw

PATCH v2 1/4 dm: adc: add uclass's mask and conversion helpers

In reply to Fabrice Gasnier
Quoted 6 lines · Fabrice Gasnier

Add two functions to ADC uclass's:
- adc_raw_to_uV() to ease ADC raw value conversion to microvolts
- adc_channel_mask() to get channels on consumer side

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20181120/63bcb002/attachment.sig>
#4 lore raw

PATCH v2 2/4 cmd: adc: add info on channel mask

In reply to Fabrice Gasnier

Enhance adc info command to report also the channel mask.

---

Changes in v2: None

 cmd/adc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
cmd/adc.c +5 -1
index c8857ed..39f61c1 100644
@@ -35,7 +35,7 @@ static int do_adc_info(cmd_tbl_t *cmdtp, int flag, int argc,
 		       char *const argv[])
 {
 	struct udevice *dev;
-	unsigned int data_mask;
+	unsigned int data_mask, ch_mask;
 	int ret, vss, vdd;
 
 	if (argc < 2)
@@ -49,6 +49,10 @@ static int do_adc_info(cmd_tbl_t *cmdtp, int flag, int argc,
 
 	printf("ADC Device '%s' :\n", argv[1]);
 
+	ret = adc_channel_mask(dev, &ch_mask);
+	if (!ret)
+		printf("channel mask: %x\n", ch_mask);
+
 	ret = adc_data_mask(dev, &data_mask);
 	if (!ret)
 		printf("data mask: %x\n", data_mask);
Signature · 1 line
1.9.1
#6 lore raw

PATCH v2 2/4 cmd: adc: add info on channel mask

In reply to Fabrice Gasnier
Quoted 4 lines · Fabrice Gasnier

Enhance adc info command to report also the channel mask.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20181120/151ff937/attachment.sig>
#7 lore raw

PATCH v2 3/4 cmd: adc: print single conversion also in uV

In reply to Fabrice Gasnier

Use newly introduced adc_raw_to_uV() API to print conversion result both as raw value and micro-volts by default.

---

Changes in v2: None

 cmd/adc.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
cmd/adc.c +7 -2
index 39f61c1..7360a96 100644
@@ -71,8 +71,9 @@ static int do_adc_info(cmd_tbl_t *cmdtp, int flag, int argc,
 static int do_adc_single(cmd_tbl_t *cmdtp, int flag, int argc,
 			 char *const argv[])
 {
+	struct udevice *dev;
 	unsigned int data;
-	int ret;
+	int ret, uV;
 
 	if (argc < 3)
 		return CMD_RET_USAGE;
@@ -85,7 +86,11 @@ static int do_adc_single(cmd_tbl_t *cmdtp, int flag, int argc,
 		return CMD_RET_FAILURE;
 	}
 
-	printf("%u\n", data);
+	ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
+	if (!ret && !adc_raw_to_uV(dev, data, &uV))
+		printf("%u, %d uV\n", data, uV);
+	else
+		printf("%u\n", data);
 
 	return CMD_RET_SUCCESS;
 }
Signature · 1 line
1.9.1
#8 lore raw

PATCH v2 3/4 cmd: adc: print single conversion also in uV

Quoted 10 lines · Fabrice Gasnier

Use newly introduced adc_raw_to_uV() API to print conversion result both as raw value and micro-volts by default.

---

Changes in v2: None

 cmd/adc.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
#9 lore raw

PATCH v2 3/4 cmd: adc: print single conversion also in uV

In reply to Fabrice Gasnier
Quoted 5 lines · Fabrice Gasnier

Use newly introduced adc_raw_to_uV() API to print conversion result both as raw value and micro-volts by default.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20181120/a6d8462a/attachment.sig>
#10 lore raw

PATCH v2 4/4 cmd: adc: add an option to scan some or all available channels

In reply to Fabrice Gasnier

Add new option to 'adc' command to do a single scan of:
- some channel(s), using mask argument
- all channels available on an ADC device (when optional mask is omitted).

---

Changes in v2: None

 cmd/adc.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 54 insertions(+), 1 deletion(-)
cmd/adc.c +54 -1
index 7360a96..2d635ac 100644
@@ -95,10 +95,62 @@ static int do_adc_single(cmd_tbl_t *cmdtp, int flag, int argc,
 	return CMD_RET_SUCCESS;
 }
 
+static int do_adc_scan(cmd_tbl_t *cmdtp, int flag, int argc,
+		       char *const argv[])
+{
+	struct adc_channel ch[ADC_MAX_CHANNEL];
+	struct udevice *dev;
+	unsigned int ch_mask;
+	int i, chan, ret, uV;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+
+	ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
+	if (ret) {
+		pr_err("Can't get the ADC %s: %d\n", argv[1], ret);
+		return CMD_RET_FAILURE;
+	}
+
+	switch (argc) {
+	case 3:
+		ch_mask = simple_strtoul(argv[2], NULL, 0);
+		if (ch_mask)
+			break;
+	case 2:
+		ret = adc_channel_mask(dev, &ch_mask);
+		if (ret) {
+			pr_err("Can't get mask for %s: %d\n", dev->name, ret);
+			return CMD_RET_FAILURE;
+		}
+		break;
+	}
+
+	ret = adc_channels_single_shot(dev->name, ch_mask, ch);
+	if (ret) {
+		pr_err("Can't get single shot for %s (chans mask: 0x%x): %d\n",
+		       dev->name, ch_mask, ret);
+		return CMD_RET_FAILURE;
+	}
+
+	for (chan = 0, i = 0; chan < ADC_MAX_CHANNEL; chan++) {
+		if (!(ch_mask & ADC_CHANNEL(chan)))
+			continue;
+		if (!adc_raw_to_uV(dev, ch[i].data, &uV))
+			printf("[%02d]: %u, %d uV\n", ch[i].id, ch[i].data, uV);
+		else
+			printf("[%02d]: %u\n", ch[i].id, ch[i].data);
+		i++;
+	}
+
+	return CMD_RET_SUCCESS;
+}
+
 static cmd_tbl_t cmd_adc_sub[] = {
 	U_BOOT_CMD_MKENT(list, 1, 1, do_adc_list, "", ""),
 	U_BOOT_CMD_MKENT(info, 2, 1, do_adc_info, "", ""),
 	U_BOOT_CMD_MKENT(single, 3, 1, do_adc_single, "", ""),
+	U_BOOT_CMD_MKENT(scan, 3, 1, do_adc_scan, "", ""),
 };
 
 static int do_adc(cmd_tbl_t *cmdtp, int flag, int argc,
@@ -124,6 +176,7 @@ static int do_adc(cmd_tbl_t *cmdtp, int flag, int argc,
 static char adc_help_text[] =
 	"list - list ADC devices\n"
 	"adc info <name> - Get ADC device info\n"
-	"adc single <name> <channel> - Get Single data of ADC device channel";
+	"adc single <name> <channel> - Get Single data of ADC device channel\n"
+	"adc scan <name> [channel mask] - Scan all [or masked] ADC channels";
 
 U_BOOT_CMD(adc, 4, 1, do_adc, "ADC sub-system", adc_help_text);
Signature · 1 line
1.9.1
#11 lore raw

PATCH v2 4/4 cmd: adc: add an option to scan some or all available channels

Quoted 11 lines · Fabrice Gasnier

Add new option to 'adc' command to do a single scan of:
- some channel(s), using mask argument
- all channels available on an ADC device (when optional mask is omitted).

---

Changes in v2: None

 cmd/adc.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 54 insertions(+), 1 deletion(-)
#12 lore raw

PATCH v2 4/4 cmd: adc: add an option to scan some or all available channels

In reply to Fabrice Gasnier
Quoted 6 lines · Fabrice Gasnier

Add new option to 'adc' command to do a single scan of:
- some channel(s), using mask argument
- all channels available on an ADC device (when optional mask is omitted).

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20181120/9255bfb5/attachment.sig>