Neil Armstrong | 49261c7 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 BayLibre, SAS |
| 4 | * Author: Neil Armstrong <narmstrong@baylibre.com> |
| 5 | */ |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
| 8 | #include <dm.h> |
| 9 | #include <adc.h> |
| 10 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 11 | static int do_adc_list(struct cmd_tbl *cmdtp, int flag, int argc, |
Neil Armstrong | 49261c7 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 12 | char *const argv[]) |
| 13 | { |
| 14 | struct udevice *dev; |
| 15 | int ret; |
| 16 | |
| 17 | ret = uclass_first_device_err(UCLASS_ADC, &dev); |
| 18 | if (ret) { |
| 19 | printf("No available ADC device\n"); |
| 20 | return CMD_RET_FAILURE; |
| 21 | } |
| 22 | |
| 23 | do { |
| 24 | printf("- %s\n", dev->name); |
| 25 | |
| 26 | ret = uclass_next_device(&dev); |
| 27 | if (ret) |
| 28 | return CMD_RET_FAILURE; |
| 29 | } while (dev); |
| 30 | |
| 31 | return CMD_RET_SUCCESS; |
| 32 | } |
| 33 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 34 | static int do_adc_info(struct cmd_tbl *cmdtp, int flag, int argc, |
Neil Armstrong | 49261c7 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 35 | char *const argv[]) |
| 36 | { |
| 37 | struct udevice *dev; |
Fabrice Gasnier | fb40888 | 2018-11-12 14:03:59 +0100 | [diff] [blame] | 38 | unsigned int data_mask, ch_mask; |
Neil Armstrong | 49261c7 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 39 | int ret, vss, vdd; |
| 40 | |
| 41 | if (argc < 2) |
| 42 | return CMD_RET_USAGE; |
| 43 | |
| 44 | ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev); |
| 45 | if (ret) { |
| 46 | printf("Unknown ADC device %s\n", argv[1]); |
| 47 | return CMD_RET_FAILURE; |
| 48 | } |
| 49 | |
| 50 | printf("ADC Device '%s' :\n", argv[1]); |
| 51 | |
Fabrice Gasnier | fb40888 | 2018-11-12 14:03:59 +0100 | [diff] [blame] | 52 | ret = adc_channel_mask(dev, &ch_mask); |
| 53 | if (!ret) |
| 54 | printf("channel mask: %x\n", ch_mask); |
| 55 | |
Neil Armstrong | 49261c7 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 56 | ret = adc_data_mask(dev, &data_mask); |
| 57 | if (!ret) |
| 58 | printf("data mask: %x\n", data_mask); |
| 59 | |
| 60 | ret = adc_vdd_value(dev, &vdd); |
| 61 | if (!ret) |
| 62 | printf("vdd: %duV\n", vdd); |
| 63 | |
| 64 | ret = adc_vss_value(dev, &vss); |
| 65 | if (!ret) |
| 66 | printf("vss: %duV\n", vss); |
| 67 | |
| 68 | return CMD_RET_SUCCESS; |
| 69 | } |
| 70 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 71 | static int do_adc_single(struct cmd_tbl *cmdtp, int flag, int argc, |
Neil Armstrong | 49261c7 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 72 | char *const argv[]) |
| 73 | { |
Fabrice Gasnier | 0555044 | 2018-11-12 14:04:00 +0100 | [diff] [blame] | 74 | struct udevice *dev; |
Neil Armstrong | 49261c7 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 75 | unsigned int data; |
Marek Vasut | beea7be | 2022-04-14 18:52:25 +0200 | [diff] [blame] | 76 | int ret, uV, val; |
Neil Armstrong | 49261c7 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 77 | |
| 78 | if (argc < 3) |
| 79 | return CMD_RET_USAGE; |
| 80 | |
| 81 | ret = adc_channel_single_shot(argv[1], simple_strtol(argv[2], NULL, 0), |
| 82 | &data); |
| 83 | if (ret) { |
Samuel Dionne-Riel | 9c7d35a | 2021-12-20 18:23:45 -0500 | [diff] [blame] | 84 | printf("Error getting single shot for device %s channel %s (ret=%d)\n", |
| 85 | argv[1], argv[2], ret); |
Neil Armstrong | 49261c7 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 86 | return CMD_RET_FAILURE; |
| 87 | } |
| 88 | |
Fabrice Gasnier | 0555044 | 2018-11-12 14:04:00 +0100 | [diff] [blame] | 89 | ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev); |
Marek Vasut | beea7be | 2022-04-14 18:52:25 +0200 | [diff] [blame] | 90 | if (!ret && !adc_raw_to_uV(dev, data, &uV)) { |
| 91 | val = uV; |
Fabrice Gasnier | 0555044 | 2018-11-12 14:04:00 +0100 | [diff] [blame] | 92 | printf("%u, %d uV\n", data, uV); |
Marek Vasut | beea7be | 2022-04-14 18:52:25 +0200 | [diff] [blame] | 93 | } else { |
| 94 | val = data; |
Fabrice Gasnier | 0555044 | 2018-11-12 14:04:00 +0100 | [diff] [blame] | 95 | printf("%u\n", data); |
Marek Vasut | beea7be | 2022-04-14 18:52:25 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Heinrich Schuchardt | d1730cf | 2022-04-25 22:26:45 +0200 | [diff] [blame] | 98 | env_set_ulong(argv[2], val); |
Neil Armstrong | 49261c7 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 99 | |
| 100 | return CMD_RET_SUCCESS; |
| 101 | } |
| 102 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 103 | static int do_adc_scan(struct cmd_tbl *cmdtp, int flag, int argc, |
Fabrice Gasnier | 5b2449c | 2018-11-12 14:04:01 +0100 | [diff] [blame] | 104 | char *const argv[]) |
| 105 | { |
| 106 | struct adc_channel ch[ADC_MAX_CHANNEL]; |
| 107 | struct udevice *dev; |
| 108 | unsigned int ch_mask; |
| 109 | int i, chan, ret, uV; |
| 110 | |
| 111 | if (argc < 2) |
| 112 | return CMD_RET_USAGE; |
| 113 | |
| 114 | ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev); |
| 115 | if (ret) { |
| 116 | pr_err("Can't get the ADC %s: %d\n", argv[1], ret); |
| 117 | return CMD_RET_FAILURE; |
| 118 | } |
| 119 | |
| 120 | switch (argc) { |
| 121 | case 3: |
| 122 | ch_mask = simple_strtoul(argv[2], NULL, 0); |
| 123 | if (ch_mask) |
| 124 | break; |
| 125 | case 2: |
| 126 | ret = adc_channel_mask(dev, &ch_mask); |
| 127 | if (ret) { |
| 128 | pr_err("Can't get mask for %s: %d\n", dev->name, ret); |
| 129 | return CMD_RET_FAILURE; |
| 130 | } |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | ret = adc_channels_single_shot(dev->name, ch_mask, ch); |
| 135 | if (ret) { |
| 136 | pr_err("Can't get single shot for %s (chans mask: 0x%x): %d\n", |
| 137 | dev->name, ch_mask, ret); |
| 138 | return CMD_RET_FAILURE; |
| 139 | } |
| 140 | |
| 141 | for (chan = 0, i = 0; chan < ADC_MAX_CHANNEL; chan++) { |
| 142 | if (!(ch_mask & ADC_CHANNEL(chan))) |
| 143 | continue; |
| 144 | if (!adc_raw_to_uV(dev, ch[i].data, &uV)) |
| 145 | printf("[%02d]: %u, %d uV\n", ch[i].id, ch[i].data, uV); |
| 146 | else |
| 147 | printf("[%02d]: %u\n", ch[i].id, ch[i].data); |
| 148 | i++; |
| 149 | } |
| 150 | |
| 151 | return CMD_RET_SUCCESS; |
| 152 | } |
| 153 | |
Neil Armstrong | 49261c7 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 154 | static char adc_help_text[] = |
| 155 | "list - list ADC devices\n" |
| 156 | "adc info <name> - Get ADC device info\n" |
Marek Vasut | beea7be | 2022-04-14 18:52:25 +0200 | [diff] [blame] | 157 | "adc single <name> <channel> [varname] - Get Single data of ADC device channel\n" |
Fabrice Gasnier | 5b2449c | 2018-11-12 14:04:01 +0100 | [diff] [blame] | 158 | "adc scan <name> [channel mask] - Scan all [or masked] ADC channels"; |
Neil Armstrong | 49261c7 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 159 | |
Boris Brezillon | 878acd7 | 2018-12-03 22:54:23 +0100 | [diff] [blame] | 160 | U_BOOT_CMD_WITH_SUBCMDS(adc, "ADC sub-system", adc_help_text, |
| 161 | U_BOOT_SUBCMD_MKENT(list, 1, 1, do_adc_list), |
| 162 | U_BOOT_SUBCMD_MKENT(info, 2, 1, do_adc_info), |
| 163 | U_BOOT_SUBCMD_MKENT(single, 3, 1, do_adc_single), |
| 164 | U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_adc_scan)); |