Neil Armstrong | 051ebe3 | 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 | */ |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 6 | #include <command.h> |
| 7 | #include <dm.h> |
| 8 | #include <adc.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 9 | #include <linux/printk.h> |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 10 | |
Simon Glass | 0914011 | 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 | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 12 | char *const argv[]) |
| 13 | { |
| 14 | struct udevice *dev; |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 15 | int ret, err; |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 16 | |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 17 | ret = err = uclass_first_device_check(UCLASS_ADC, &dev); |
| 18 | |
| 19 | while (dev) { |
| 20 | printf("- %s status: %i\n", dev->name, ret); |
| 21 | |
| 22 | ret = uclass_next_device_check(&dev); |
| 23 | if (ret) |
| 24 | err = ret; |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 25 | } |
| 26 | |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 27 | return err ? CMD_RET_FAILURE : CMD_RET_SUCCESS; |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 28 | } |
| 29 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 30 | static int do_adc_info(struct cmd_tbl *cmdtp, int flag, int argc, |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 31 | char *const argv[]) |
| 32 | { |
| 33 | struct udevice *dev; |
Fabrice Gasnier | 1c84d90 | 2018-11-12 14:03:59 +0100 | [diff] [blame] | 34 | unsigned int data_mask, ch_mask; |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 35 | int ret, vss, vdd; |
| 36 | |
| 37 | if (argc < 2) |
| 38 | return CMD_RET_USAGE; |
| 39 | |
| 40 | ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev); |
| 41 | if (ret) { |
| 42 | printf("Unknown ADC device %s\n", argv[1]); |
| 43 | return CMD_RET_FAILURE; |
| 44 | } |
| 45 | |
| 46 | printf("ADC Device '%s' :\n", argv[1]); |
| 47 | |
Fabrice Gasnier | 1c84d90 | 2018-11-12 14:03:59 +0100 | [diff] [blame] | 48 | ret = adc_channel_mask(dev, &ch_mask); |
| 49 | if (!ret) |
| 50 | printf("channel mask: %x\n", ch_mask); |
| 51 | |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 52 | ret = adc_data_mask(dev, &data_mask); |
| 53 | if (!ret) |
| 54 | printf("data mask: %x\n", data_mask); |
| 55 | |
| 56 | ret = adc_vdd_value(dev, &vdd); |
| 57 | if (!ret) |
| 58 | printf("vdd: %duV\n", vdd); |
| 59 | |
| 60 | ret = adc_vss_value(dev, &vss); |
| 61 | if (!ret) |
| 62 | printf("vss: %duV\n", vss); |
| 63 | |
| 64 | return CMD_RET_SUCCESS; |
| 65 | } |
| 66 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 67 | static int do_adc_single(struct cmd_tbl *cmdtp, int flag, int argc, |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 68 | char *const argv[]) |
| 69 | { |
Vyacheslav Bocharov | 6ac4774 | 2022-07-03 15:49:09 +0300 | [diff] [blame] | 70 | char *varname = NULL; |
Fabrice Gasnier | c56fc49 | 2018-11-12 14:04:00 +0100 | [diff] [blame] | 71 | struct udevice *dev; |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 72 | unsigned int data; |
Marek Vasut | 9de612a | 2022-04-14 18:52:25 +0200 | [diff] [blame] | 73 | int ret, uV, val; |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 74 | |
| 75 | if (argc < 3) |
| 76 | return CMD_RET_USAGE; |
| 77 | |
Vyacheslav Bocharov | 6ac4774 | 2022-07-03 15:49:09 +0300 | [diff] [blame] | 78 | if (argc >= 4) |
| 79 | varname = argv[3]; |
| 80 | |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 81 | ret = adc_channel_single_shot(argv[1], simple_strtol(argv[2], NULL, 0), |
| 82 | &data); |
| 83 | if (ret) { |
Samuel Dionne-Riel | 4a05497 | 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 | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 86 | return CMD_RET_FAILURE; |
| 87 | } |
| 88 | |
Fabrice Gasnier | c56fc49 | 2018-11-12 14:04:00 +0100 | [diff] [blame] | 89 | ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev); |
Marek Vasut | 9de612a | 2022-04-14 18:52:25 +0200 | [diff] [blame] | 90 | if (!ret && !adc_raw_to_uV(dev, data, &uV)) { |
| 91 | val = uV; |
Fabrice Gasnier | c56fc49 | 2018-11-12 14:04:00 +0100 | [diff] [blame] | 92 | printf("%u, %d uV\n", data, uV); |
Marek Vasut | 9de612a | 2022-04-14 18:52:25 +0200 | [diff] [blame] | 93 | } else { |
| 94 | val = data; |
Fabrice Gasnier | c56fc49 | 2018-11-12 14:04:00 +0100 | [diff] [blame] | 95 | printf("%u\n", data); |
Marek Vasut | 9de612a | 2022-04-14 18:52:25 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Vyacheslav Bocharov | 6ac4774 | 2022-07-03 15:49:09 +0300 | [diff] [blame] | 98 | if (varname) |
| 99 | env_set_ulong(varname, val); |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 100 | |
| 101 | return CMD_RET_SUCCESS; |
| 102 | } |
| 103 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 104 | static int do_adc_scan(struct cmd_tbl *cmdtp, int flag, int argc, |
Fabrice Gasnier | 61927d2 | 2018-11-12 14:04:01 +0100 | [diff] [blame] | 105 | char *const argv[]) |
| 106 | { |
| 107 | struct adc_channel ch[ADC_MAX_CHANNEL]; |
| 108 | struct udevice *dev; |
| 109 | unsigned int ch_mask; |
| 110 | int i, chan, ret, uV; |
| 111 | |
| 112 | if (argc < 2) |
| 113 | return CMD_RET_USAGE; |
| 114 | |
| 115 | ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev); |
| 116 | if (ret) { |
| 117 | pr_err("Can't get the ADC %s: %d\n", argv[1], ret); |
| 118 | return CMD_RET_FAILURE; |
| 119 | } |
| 120 | |
| 121 | switch (argc) { |
| 122 | case 3: |
| 123 | ch_mask = simple_strtoul(argv[2], NULL, 0); |
| 124 | if (ch_mask) |
| 125 | break; |
| 126 | case 2: |
| 127 | ret = adc_channel_mask(dev, &ch_mask); |
| 128 | if (ret) { |
| 129 | pr_err("Can't get mask for %s: %d\n", dev->name, ret); |
| 130 | return CMD_RET_FAILURE; |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | ret = adc_channels_single_shot(dev->name, ch_mask, ch); |
| 136 | if (ret) { |
| 137 | pr_err("Can't get single shot for %s (chans mask: 0x%x): %d\n", |
| 138 | dev->name, ch_mask, ret); |
| 139 | return CMD_RET_FAILURE; |
| 140 | } |
| 141 | |
| 142 | for (chan = 0, i = 0; chan < ADC_MAX_CHANNEL; chan++) { |
| 143 | if (!(ch_mask & ADC_CHANNEL(chan))) |
| 144 | continue; |
| 145 | if (!adc_raw_to_uV(dev, ch[i].data, &uV)) |
| 146 | printf("[%02d]: %u, %d uV\n", ch[i].id, ch[i].data, uV); |
| 147 | else |
| 148 | printf("[%02d]: %u\n", ch[i].id, ch[i].data); |
| 149 | i++; |
| 150 | } |
| 151 | |
| 152 | return CMD_RET_SUCCESS; |
| 153 | } |
| 154 | |
Tom Rini | b85ecb2 | 2024-06-19 10:09:44 -0600 | [diff] [blame] | 155 | U_BOOT_LONGHELP(adc, |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 156 | "list - list ADC devices\n" |
| 157 | "adc info <name> - Get ADC device info\n" |
Marek Vasut | 9de612a | 2022-04-14 18:52:25 +0200 | [diff] [blame] | 158 | "adc single <name> <channel> [varname] - Get Single data of ADC device channel\n" |
Tom Rini | b85ecb2 | 2024-06-19 10:09:44 -0600 | [diff] [blame] | 159 | "adc scan <name> [channel mask] - Scan all [or masked] ADC channels\n"); |
Neil Armstrong | 051ebe3 | 2018-04-27 15:17:57 +0200 | [diff] [blame] | 160 | |
Boris Brezillon | 9652cfd | 2018-12-03 22:54:23 +0100 | [diff] [blame] | 161 | U_BOOT_CMD_WITH_SUBCMDS(adc, "ADC sub-system", adc_help_text, |
| 162 | U_BOOT_SUBCMD_MKENT(list, 1, 1, do_adc_list), |
| 163 | U_BOOT_SUBCMD_MKENT(info, 2, 1, do_adc_info), |
Vyacheslav Bocharov | 6ac4774 | 2022-07-03 15:49:09 +0300 | [diff] [blame] | 164 | U_BOOT_SUBCMD_MKENT(single, 4, 1, do_adc_single), |
Boris Brezillon | 9652cfd | 2018-12-03 22:54:23 +0100 | [diff] [blame] | 165 | U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_adc_scan)); |