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 | */ |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
| 8 | #include <dm.h> |
| 9 | #include <adc.h> |
| 10 | |
| 11 | static int do_adc_list(cmd_tbl_t *cmdtp, int flag, int argc, |
| 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 | |
| 34 | static int do_adc_info(cmd_tbl_t *cmdtp, int flag, int argc, |
| 35 | char *const argv[]) |
| 36 | { |
| 37 | struct udevice *dev; |
Fabrice Gasnier | 1c84d90 | 2018-11-12 14:03:59 +0100 | [diff] [blame^] | 38 | unsigned int data_mask, ch_mask; |
Neil Armstrong | 051ebe3 | 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 | 1c84d90 | 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 | 051ebe3 | 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 | |
| 71 | static int do_adc_single(cmd_tbl_t *cmdtp, int flag, int argc, |
| 72 | char *const argv[]) |
| 73 | { |
| 74 | unsigned int data; |
| 75 | int ret; |
| 76 | |
| 77 | if (argc < 3) |
| 78 | return CMD_RET_USAGE; |
| 79 | |
| 80 | ret = adc_channel_single_shot(argv[1], simple_strtol(argv[2], NULL, 0), |
| 81 | &data); |
| 82 | if (ret) { |
| 83 | printf("Error getting single shot for device %s channel %s\n", |
| 84 | argv[1], argv[2]); |
| 85 | return CMD_RET_FAILURE; |
| 86 | } |
| 87 | |
| 88 | printf("%u\n", data); |
| 89 | |
| 90 | return CMD_RET_SUCCESS; |
| 91 | } |
| 92 | |
| 93 | static cmd_tbl_t cmd_adc_sub[] = { |
| 94 | U_BOOT_CMD_MKENT(list, 1, 1, do_adc_list, "", ""), |
| 95 | U_BOOT_CMD_MKENT(info, 2, 1, do_adc_info, "", ""), |
| 96 | U_BOOT_CMD_MKENT(single, 3, 1, do_adc_single, "", ""), |
| 97 | }; |
| 98 | |
| 99 | static int do_adc(cmd_tbl_t *cmdtp, int flag, int argc, |
| 100 | char *const argv[]) |
| 101 | { |
| 102 | cmd_tbl_t *c; |
| 103 | |
| 104 | if (argc < 2) |
| 105 | return CMD_RET_USAGE; |
| 106 | |
| 107 | /* Strip off leading 'adc' command argument */ |
| 108 | argc--; |
| 109 | argv++; |
| 110 | |
| 111 | c = find_cmd_tbl(argv[0], &cmd_adc_sub[0], ARRAY_SIZE(cmd_adc_sub)); |
| 112 | |
| 113 | if (c) |
| 114 | return c->cmd(cmdtp, flag, argc, argv); |
| 115 | else |
| 116 | return CMD_RET_USAGE; |
| 117 | } |
| 118 | |
| 119 | static char adc_help_text[] = |
| 120 | "list - list ADC devices\n" |
| 121 | "adc info <name> - Get ADC device info\n" |
| 122 | "adc single <name> <channel> - Get Single data of ADC device channel"; |
| 123 | |
| 124 | U_BOOT_CMD(adc, 4, 1, do_adc, "ADC sub-system", adc_help_text); |