blob: c0da4932d4af7dfa95492469d842031511f3a3b5 [file] [log] [blame]
Mario Six5381c282018-07-31 11:44:11 +02001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * (C) Copyright 2017
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5 */
6
Simon Glass401d1c42020-10-30 21:38:53 -06007struct udevice;
8
Mario Six5381c282018-07-31 11:44:11 +02009/*
10 * This uclass encapsulates hardware methods to gather information about a
Simon Glass3a8ee3d2020-11-05 06:32:05 -070011 * sysinfo or a specific device such as hard-wired GPIOs on GPIO expanders,
Mario Six5381c282018-07-31 11:44:11 +020012 * read-only data in flash ICs, or similar.
13 *
14 * The interface offers functions to read the usual standard data types (bool,
15 * int, string) from the device, each of which is identified by a static
16 * numeric ID (which will usually be defined as a enum in a header file).
17 *
Simon Glass3a8ee3d2020-11-05 06:32:05 -070018 * If for example the sysinfo had a read-only serial number flash IC, we could
Mario Six5381c282018-07-31 11:44:11 +020019 * call
20 *
Simon Glass3a8ee3d2020-11-05 06:32:05 -070021 * ret = sysinfo_detect(dev);
Mario Six5381c282018-07-31 11:44:11 +020022 * if (ret) {
Simon Glass3a8ee3d2020-11-05 06:32:05 -070023 * debug("sysinfo device not found.");
Mario Six5381c282018-07-31 11:44:11 +020024 * return ret;
25 * }
26 *
Simon Glass3a8ee3d2020-11-05 06:32:05 -070027 * ret = sysinfo_get_int(dev, ID_SERIAL_NUMBER, &serial);
Mario Six5381c282018-07-31 11:44:11 +020028 * if (ret) {
29 * debug("Error when reading serial number from device.");
30 * return ret;
31 * }
32 *
33 * to read the serial number.
34 */
35
Simon Glass3a8ee3d2020-11-05 06:32:05 -070036#if CONFIG_IS_ENABLED(SYSINFO)
37struct sysinfo_ops {
Mario Six5381c282018-07-31 11:44:11 +020038 /**
39 * detect() - Run the hardware info detection procedure for this
40 * device.
41 * @dev: The device containing the information
42 *
43 * This operation might take a long time (e.g. read from EEPROM,
44 * check the presence of a device on a bus etc.), hence this is not
45 * done in the probe() method, but later during operation in this
46 * dedicated method.
47 *
48 * Return: 0 if OK, -ve on error.
49 */
50 int (*detect)(struct udevice *dev);
51
52 /**
53 * get_bool() - Read a specific bool data value that describes the
54 * hardware setup.
Simon Glass3a8ee3d2020-11-05 06:32:05 -070055 * @dev: The sysinfo instance to gather the data.
Mario Six5381c282018-07-31 11:44:11 +020056 * @id: A unique identifier for the bool value to be read.
57 * @val: Pointer to a buffer that receives the value read.
58 *
59 * Return: 0 if OK, -ve on error.
60 */
61 int (*get_bool)(struct udevice *dev, int id, bool *val);
62
63 /**
64 * get_int() - Read a specific int data value that describes the
65 * hardware setup.
Simon Glass3a8ee3d2020-11-05 06:32:05 -070066 * @dev: The sysinfo instance to gather the data.
Mario Six5381c282018-07-31 11:44:11 +020067 * @id: A unique identifier for the int value to be read.
68 * @val: Pointer to a buffer that receives the value read.
69 *
70 * Return: 0 if OK, -ve on error.
71 */
72 int (*get_int)(struct udevice *dev, int id, int *val);
73
74 /**
75 * get_str() - Read a specific string data value that describes the
76 * hardware setup.
Simon Glass3a8ee3d2020-11-05 06:32:05 -070077 * @dev: The sysinfo instance to gather the data.
Mario Six5381c282018-07-31 11:44:11 +020078 * @id: A unique identifier for the string value to be read.
79 * @size: The size of the buffer to receive the string data.
80 * @val: Pointer to a buffer that receives the value read.
81 *
82 * Return: 0 if OK, -ve on error.
83 */
84 int (*get_str)(struct udevice *dev, int id, size_t size, char *val);
Jean-Jacques Hiblotd42730e2019-10-22 16:39:19 +020085
86 /**
87 * get_fit_loadable - Get the name of an image to load from FIT
88 * This function can be used to provide the image names based on runtime
89 * detection. A classic use-case would when DTBOs are used to describe
90 * additionnal daughter cards.
91 *
Simon Glass3a8ee3d2020-11-05 06:32:05 -070092 * @dev: The sysinfo instance to gather the data.
Jean-Jacques Hiblotd42730e2019-10-22 16:39:19 +020093 * @index: Index of the image. Starts at 0 and gets incremented
94 * after each call to this function.
95 * @type: The type of image. For example, "fdt" for DTBs
96 * @strp: A pointer to string. Untouched if the function fails
97 *
98 * Return: 0 if OK, -ENOENT if no loadable is available else -ve on
99 * error.
100 */
101 int (*get_fit_loadable)(struct udevice *dev, int index,
102 const char *type, const char **strp);
Mario Six5381c282018-07-31 11:44:11 +0200103};
104
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700105#define sysinfo_get_ops(dev) ((struct sysinfo_ops *)(dev)->driver->ops)
Mario Six5381c282018-07-31 11:44:11 +0200106
107/**
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700108 * sysinfo_detect() - Run the hardware info detection procedure for this device.
Mario Six5381c282018-07-31 11:44:11 +0200109 *
110 * @dev: The device containing the information
111 *
112 * Return: 0 if OK, -ve on error.
113 */
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700114int sysinfo_detect(struct udevice *dev);
Mario Six5381c282018-07-31 11:44:11 +0200115
116/**
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700117 * sysinfo_get_bool() - Read a specific bool data value that describes the
Mario Six5381c282018-07-31 11:44:11 +0200118 * hardware setup.
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700119 * @dev: The sysinfo instance to gather the data.
Mario Six5381c282018-07-31 11:44:11 +0200120 * @id: A unique identifier for the bool value to be read.
121 * @val: Pointer to a buffer that receives the value read.
122 *
123 * Return: 0 if OK, -ve on error.
124 */
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700125int sysinfo_get_bool(struct udevice *dev, int id, bool *val);
Mario Six5381c282018-07-31 11:44:11 +0200126
127/**
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700128 * sysinfo_get_int() - Read a specific int data value that describes the
Mario Six5381c282018-07-31 11:44:11 +0200129 * hardware setup.
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700130 * @dev: The sysinfo instance to gather the data.
Mario Six5381c282018-07-31 11:44:11 +0200131 * @id: A unique identifier for the int value to be read.
132 * @val: Pointer to a buffer that receives the value read.
133 *
134 * Return: 0 if OK, -ve on error.
135 */
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700136int sysinfo_get_int(struct udevice *dev, int id, int *val);
Mario Six5381c282018-07-31 11:44:11 +0200137
138/**
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700139 * sysinfo_get_str() - Read a specific string data value that describes the
Mario Six5381c282018-07-31 11:44:11 +0200140 * hardware setup.
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700141 * @dev: The sysinfo instance to gather the data.
Mario Six5381c282018-07-31 11:44:11 +0200142 * @id: A unique identifier for the string value to be read.
143 * @size: The size of the buffer to receive the string data.
144 * @val: Pointer to a buffer that receives the value read.
145 *
146 * Return: 0 if OK, -ve on error.
147 */
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700148int sysinfo_get_str(struct udevice *dev, int id, size_t size, char *val);
Mario Six5381c282018-07-31 11:44:11 +0200149
150/**
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700151 * sysinfo_get() - Return the sysinfo device for the sysinfo in question.
152 * @devp: Pointer to structure to receive the sysinfo device.
Mario Six5381c282018-07-31 11:44:11 +0200153 *
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700154 * Since there can only be at most one sysinfo instance, the API can supply a
Mario Six5381c282018-07-31 11:44:11 +0200155 * function that returns the unique device. This is especially useful for use
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700156 * in sysinfo files.
Mario Six5381c282018-07-31 11:44:11 +0200157 *
158 * Return: 0 if OK, -ve on error.
159 */
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700160int sysinfo_get(struct udevice **devp);
Jean-Jacques Hiblotd42730e2019-10-22 16:39:19 +0200161
162/**
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700163 * sysinfo_get_fit_loadable - Get the name of an image to load from FIT
Jean-Jacques Hiblotd42730e2019-10-22 16:39:19 +0200164 * This function can be used to provide the image names based on runtime
165 * detection. A classic use-case would when DTBOs are used to describe
166 * additionnal daughter cards.
167 *
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700168 * @dev: The sysinfo instance to gather the data.
Jean-Jacques Hiblotd42730e2019-10-22 16:39:19 +0200169 * @index: Index of the image. Starts at 0 and gets incremented
170 * after each call to this function.
171 * @type: The type of image. For example, "fdt" for DTBs
172 * @strp: A pointer to string. Untouched if the function fails
173 *
174 *
175 * Return: 0 if OK, -ENOENT if no loadable is available else -ve on
176 * error.
177 */
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700178int sysinfo_get_fit_loadable(struct udevice *dev, int index, const char *type,
179 const char **strp);
Jean-Jacques Hiblot02806e92019-10-22 16:39:20 +0200180
181#else
182
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700183static inline int sysinfo_detect(struct udevice *dev)
Jean-Jacques Hiblot02806e92019-10-22 16:39:20 +0200184{
185 return -ENOSYS;
186}
187
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700188static inline int sysinfo_get_bool(struct udevice *dev, int id, bool *val)
Jean-Jacques Hiblot02806e92019-10-22 16:39:20 +0200189{
190 return -ENOSYS;
191}
192
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700193static inline int sysinfo_get_int(struct udevice *dev, int id, int *val)
Jean-Jacques Hiblot02806e92019-10-22 16:39:20 +0200194{
195 return -ENOSYS;
196}
197
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700198static inline int sysinfo_get_str(struct udevice *dev, int id, size_t size,
199 char *val)
Jean-Jacques Hiblot02806e92019-10-22 16:39:20 +0200200{
201 return -ENOSYS;
202}
203
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700204static inline int sysinfo_get(struct udevice **devp)
Jean-Jacques Hiblot02806e92019-10-22 16:39:20 +0200205{
206 return -ENOSYS;
207}
208
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700209static inline int sysinfo_get_fit_loadable(struct udevice *dev, int index,
210 const char *type, const char **strp)
Jean-Jacques Hiblot02806e92019-10-22 16:39:20 +0200211{
212 return -ENOSYS;
213}
214
215#endif