Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 | /* |
| 3 | * (C) Copyright 2017 |
| 4 | * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This uclass encapsulates hardware methods to gather information about a |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 9 | * sysinfo or a specific device such as hard-wired GPIOs on GPIO expanders, |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 10 | * read-only data in flash ICs, or similar. |
| 11 | * |
| 12 | * The interface offers functions to read the usual standard data types (bool, |
| 13 | * int, string) from the device, each of which is identified by a static |
| 14 | * numeric ID (which will usually be defined as a enum in a header file). |
| 15 | * |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 16 | * If for example the sysinfo had a read-only serial number flash IC, we could |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 17 | * call |
| 18 | * |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 19 | * ret = sysinfo_detect(dev); |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 20 | * if (ret) { |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 21 | * debug("sysinfo device not found."); |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 22 | * return ret; |
| 23 | * } |
| 24 | * |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 25 | * ret = sysinfo_get_int(dev, ID_SERIAL_NUMBER, &serial); |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 26 | * if (ret) { |
| 27 | * debug("Error when reading serial number from device."); |
| 28 | * return ret; |
| 29 | * } |
| 30 | * |
| 31 | * to read the serial number. |
| 32 | */ |
| 33 | |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 34 | #if CONFIG_IS_ENABLED(SYSINFO) |
| 35 | struct sysinfo_ops { |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 36 | /** |
| 37 | * detect() - Run the hardware info detection procedure for this |
| 38 | * device. |
| 39 | * @dev: The device containing the information |
| 40 | * |
| 41 | * This operation might take a long time (e.g. read from EEPROM, |
| 42 | * check the presence of a device on a bus etc.), hence this is not |
| 43 | * done in the probe() method, but later during operation in this |
| 44 | * dedicated method. |
| 45 | * |
| 46 | * Return: 0 if OK, -ve on error. |
| 47 | */ |
| 48 | int (*detect)(struct udevice *dev); |
| 49 | |
| 50 | /** |
| 51 | * get_bool() - Read a specific bool data value that describes the |
| 52 | * hardware setup. |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 53 | * @dev: The sysinfo instance to gather the data. |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 54 | * @id: A unique identifier for the bool value to be read. |
| 55 | * @val: Pointer to a buffer that receives the value read. |
| 56 | * |
| 57 | * Return: 0 if OK, -ve on error. |
| 58 | */ |
| 59 | int (*get_bool)(struct udevice *dev, int id, bool *val); |
| 60 | |
| 61 | /** |
| 62 | * get_int() - Read a specific int data value that describes the |
| 63 | * hardware setup. |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 64 | * @dev: The sysinfo instance to gather the data. |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 65 | * @id: A unique identifier for the int value to be read. |
| 66 | * @val: Pointer to a buffer that receives the value read. |
| 67 | * |
| 68 | * Return: 0 if OK, -ve on error. |
| 69 | */ |
| 70 | int (*get_int)(struct udevice *dev, int id, int *val); |
| 71 | |
| 72 | /** |
| 73 | * get_str() - Read a specific string data value that describes the |
| 74 | * hardware setup. |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 75 | * @dev: The sysinfo instance to gather the data. |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 76 | * @id: A unique identifier for the string value to be read. |
| 77 | * @size: The size of the buffer to receive the string data. |
| 78 | * @val: Pointer to a buffer that receives the value read. |
| 79 | * |
| 80 | * Return: 0 if OK, -ve on error. |
| 81 | */ |
| 82 | int (*get_str)(struct udevice *dev, int id, size_t size, char *val); |
Jean-Jacques Hiblot | d42730e | 2019-10-22 16:39:19 +0200 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * get_fit_loadable - Get the name of an image to load from FIT |
| 86 | * This function can be used to provide the image names based on runtime |
| 87 | * detection. A classic use-case would when DTBOs are used to describe |
| 88 | * additionnal daughter cards. |
| 89 | * |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 90 | * @dev: The sysinfo instance to gather the data. |
Jean-Jacques Hiblot | d42730e | 2019-10-22 16:39:19 +0200 | [diff] [blame] | 91 | * @index: Index of the image. Starts at 0 and gets incremented |
| 92 | * after each call to this function. |
| 93 | * @type: The type of image. For example, "fdt" for DTBs |
| 94 | * @strp: A pointer to string. Untouched if the function fails |
| 95 | * |
| 96 | * Return: 0 if OK, -ENOENT if no loadable is available else -ve on |
| 97 | * error. |
| 98 | */ |
| 99 | int (*get_fit_loadable)(struct udevice *dev, int index, |
| 100 | const char *type, const char **strp); |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 103 | #define sysinfo_get_ops(dev) ((struct sysinfo_ops *)(dev)->driver->ops) |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 104 | |
| 105 | /** |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 106 | * sysinfo_detect() - Run the hardware info detection procedure for this device. |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 107 | * |
| 108 | * @dev: The device containing the information |
| 109 | * |
| 110 | * Return: 0 if OK, -ve on error. |
| 111 | */ |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 112 | int sysinfo_detect(struct udevice *dev); |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 113 | |
| 114 | /** |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 115 | * sysinfo_get_bool() - Read a specific bool data value that describes the |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 116 | * hardware setup. |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 117 | * @dev: The sysinfo instance to gather the data. |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 118 | * @id: A unique identifier for the bool value to be read. |
| 119 | * @val: Pointer to a buffer that receives the value read. |
| 120 | * |
| 121 | * Return: 0 if OK, -ve on error. |
| 122 | */ |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 123 | int sysinfo_get_bool(struct udevice *dev, int id, bool *val); |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 124 | |
| 125 | /** |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 126 | * sysinfo_get_int() - Read a specific int data value that describes the |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 127 | * hardware setup. |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 128 | * @dev: The sysinfo instance to gather the data. |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 129 | * @id: A unique identifier for the int value to be read. |
| 130 | * @val: Pointer to a buffer that receives the value read. |
| 131 | * |
| 132 | * Return: 0 if OK, -ve on error. |
| 133 | */ |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 134 | int sysinfo_get_int(struct udevice *dev, int id, int *val); |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 135 | |
| 136 | /** |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 137 | * sysinfo_get_str() - Read a specific string data value that describes the |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 138 | * hardware setup. |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 139 | * @dev: The sysinfo instance to gather the data. |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 140 | * @id: A unique identifier for the string value to be read. |
| 141 | * @size: The size of the buffer to receive the string data. |
| 142 | * @val: Pointer to a buffer that receives the value read. |
| 143 | * |
| 144 | * Return: 0 if OK, -ve on error. |
| 145 | */ |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 146 | int sysinfo_get_str(struct udevice *dev, int id, size_t size, char *val); |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 147 | |
| 148 | /** |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 149 | * sysinfo_get() - Return the sysinfo device for the sysinfo in question. |
| 150 | * @devp: Pointer to structure to receive the sysinfo device. |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 151 | * |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 152 | * Since there can only be at most one sysinfo instance, the API can supply a |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 153 | * function that returns the unique device. This is especially useful for use |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 154 | * in sysinfo files. |
Mario Six | 5381c28 | 2018-07-31 11:44:11 +0200 | [diff] [blame] | 155 | * |
| 156 | * Return: 0 if OK, -ve on error. |
| 157 | */ |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 158 | int sysinfo_get(struct udevice **devp); |
Jean-Jacques Hiblot | d42730e | 2019-10-22 16:39:19 +0200 | [diff] [blame] | 159 | |
| 160 | /** |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 161 | * sysinfo_get_fit_loadable - Get the name of an image to load from FIT |
Jean-Jacques Hiblot | d42730e | 2019-10-22 16:39:19 +0200 | [diff] [blame] | 162 | * This function can be used to provide the image names based on runtime |
| 163 | * detection. A classic use-case would when DTBOs are used to describe |
| 164 | * additionnal daughter cards. |
| 165 | * |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 166 | * @dev: The sysinfo instance to gather the data. |
Jean-Jacques Hiblot | d42730e | 2019-10-22 16:39:19 +0200 | [diff] [blame] | 167 | * @index: Index of the image. Starts at 0 and gets incremented |
| 168 | * after each call to this function. |
| 169 | * @type: The type of image. For example, "fdt" for DTBs |
| 170 | * @strp: A pointer to string. Untouched if the function fails |
| 171 | * |
| 172 | * |
| 173 | * Return: 0 if OK, -ENOENT if no loadable is available else -ve on |
| 174 | * error. |
| 175 | */ |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 176 | int sysinfo_get_fit_loadable(struct udevice *dev, int index, const char *type, |
| 177 | const char **strp); |
Jean-Jacques Hiblot | 02806e9 | 2019-10-22 16:39:20 +0200 | [diff] [blame] | 178 | |
| 179 | #else |
| 180 | |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 181 | static inline int sysinfo_detect(struct udevice *dev) |
Jean-Jacques Hiblot | 02806e9 | 2019-10-22 16:39:20 +0200 | [diff] [blame] | 182 | { |
| 183 | return -ENOSYS; |
| 184 | } |
| 185 | |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 186 | static inline int sysinfo_get_bool(struct udevice *dev, int id, bool *val) |
Jean-Jacques Hiblot | 02806e9 | 2019-10-22 16:39:20 +0200 | [diff] [blame] | 187 | { |
| 188 | return -ENOSYS; |
| 189 | } |
| 190 | |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 191 | static inline int sysinfo_get_int(struct udevice *dev, int id, int *val) |
Jean-Jacques Hiblot | 02806e9 | 2019-10-22 16:39:20 +0200 | [diff] [blame] | 192 | { |
| 193 | return -ENOSYS; |
| 194 | } |
| 195 | |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 196 | static inline int sysinfo_get_str(struct udevice *dev, int id, size_t size, |
| 197 | char *val) |
Jean-Jacques Hiblot | 02806e9 | 2019-10-22 16:39:20 +0200 | [diff] [blame] | 198 | { |
| 199 | return -ENOSYS; |
| 200 | } |
| 201 | |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 202 | static inline int sysinfo_get(struct udevice **devp) |
Jean-Jacques Hiblot | 02806e9 | 2019-10-22 16:39:20 +0200 | [diff] [blame] | 203 | { |
| 204 | return -ENOSYS; |
| 205 | } |
| 206 | |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 207 | static inline int sysinfo_get_fit_loadable(struct udevice *dev, int index, |
| 208 | const char *type, const char **strp) |
Jean-Jacques Hiblot | 02806e9 | 2019-10-22 16:39:20 +0200 | [diff] [blame] | 209 | { |
| 210 | return -ENOSYS; |
| 211 | } |
| 212 | |
| 213 | #endif |