1 // SPDX-License-Identifier: GPL-2.0+
14 #include "gazerbeam.h"
16 /* Sequence number of I2C bus that holds the GPIO expanders */
17 static const int I2C_BUS_SEQ_NO = 1;
19 /* I2C address of SC/MC2 expander */
20 static const int MC2_EXPANDER_ADDR = 0x20;
21 /* I2C address of MC4 expander */
22 static const int MC4_EXPANDER_ADDR = 0x22;
24 /* Number of the GPIO to read the SC data from */
25 static const int SC_GPIO_NO;
26 /* Number of the GPIO to read the CON data from */
27 static const int CON_GPIO_NO = 1;
30 * struct board_gazerbeam_priv - Private data structure for the gazerbeam board
32 * @reset_gpios: GPIOs for the board's reset GPIOs.
33 * @var_gpios: GPIOs for the board's hardware variant GPIOs
34 * @ver_gpios: GPIOs for the board's hardware version GPIOs
35 * @variant: Container for the board's hardware variant (CON/CPU)
36 * @multichannel: Container for the board's multichannel variant (MC4/MC2/SC)
37 * @hwversion: Container for the board's hardware version
39 struct board_gazerbeam_priv {
40 struct gpio_desc reset_gpios[2];
41 struct gpio_desc var_gpios[2];
42 struct gpio_desc ver_gpios[4];
49 * _read_board_variant_data() - Read variant information from the hardware.
50 * @dev: The board device for which to determine the multichannel and device
53 * The data read from the board's hardware (mostly hard-wired GPIOs) is stored
54 * in the private data structure of the driver to be used by other driver
57 * Return: 0 if OK, -ve on error.
59 static int _read_board_variant_data(struct udevice *dev)
61 struct board_gazerbeam_priv *priv = dev_get_priv(dev);
62 struct udevice *i2c_bus;
63 struct udevice *dummy;
65 int mc4, mc2, sc, mc2_sc, con;
69 res = uclass_get_device_by_seq(UCLASS_I2C, I2C_BUS_SEQ_NO, &i2c_bus);
71 debug("%s: Could not get I2C bus %d (err = %d)\n",
72 dev->name, I2C_BUS_SEQ_NO, res);
77 debug("%s: Could not get I2C bus %d\n",
78 dev->name, I2C_BUS_SEQ_NO);
82 mc2_sc = !dm_i2c_probe(i2c_bus, MC2_EXPANDER_ADDR, 0, &dummy);
83 mc4 = !dm_i2c_probe(i2c_bus, MC4_EXPANDER_ADDR, 0, &dummy);
86 debug("%s: Board hardware configuration inconsistent.\n",
91 listname = mc2_sc ? "var-gpios-mc2" : "var-gpios-mc4";
93 gpio_num = gpio_request_list_by_name(dev, listname, priv->var_gpios,
94 ARRAY_SIZE(priv->var_gpios),
97 debug("%s: Requesting gpio list %s failed (err = %d).\n",
98 dev->name, listname, gpio_num);
102 sc = dm_gpio_get_value(&priv->var_gpios[SC_GPIO_NO]);
104 debug("%s: Error while reading 'sc' GPIO (err = %d)",
109 mc2 = mc2_sc ? (sc ? 0 : 1) : 0;
111 if ((sc && mc2) || (sc && mc4) || (!sc && !mc2 && !mc4)) {
112 debug("%s: Board hardware configuration inconsistent.\n",
117 con = dm_gpio_get_value(&priv->var_gpios[CON_GPIO_NO]);
119 debug("%s: Error while reading 'con' GPIO (err = %d)",
124 priv->variant = con ? VAR_CON : VAR_CPU;
126 priv->multichannel = mc4 ? 4 : (mc2 ? 2 : (sc ? 1 : 0));
132 * _read_hwversion() - Read the hardware version from the board.
133 * @dev: The board device for which to read the hardware version.
135 * The hardware version read from the board (from hard-wired GPIOs) is stored
136 * in the private data structure of the driver to be used by other driver
139 * Return: 0 if OK, -ve on error.
141 static int _read_hwversion(struct udevice *dev)
143 struct board_gazerbeam_priv *priv = dev_get_priv(dev);
146 res = gpio_request_list_by_name(dev, "ver-gpios", priv->ver_gpios,
147 ARRAY_SIZE(priv->ver_gpios),
150 debug("%s: Error getting GPIO list 'ver-gpios' (err = %d)\n",
155 res = dm_gpio_get_values_as_int(priv->ver_gpios,
156 ARRAY_SIZE(priv->ver_gpios));
158 debug("%s: Error reading HW version from expander (err = %d)\n",
163 priv->hwversion = res;
165 res = gpio_free_list(dev, priv->ver_gpios, ARRAY_SIZE(priv->ver_gpios));
167 debug("%s: Error freeing HW version GPIO list (err = %d)\n",
175 static int board_gazerbeam_detect(struct udevice *dev)
179 res = _read_board_variant_data(dev);
181 debug("%s: Error reading multichannel variant (err = %d)\n",
186 res = _read_hwversion(dev);
188 debug("%s: Error reading hardware version (err = %d)\n",
196 static int board_gazerbeam_get_int(struct udevice *dev, int id, int *val)
198 struct board_gazerbeam_priv *priv = dev_get_priv(dev);
201 case BOARD_MULTICHANNEL:
202 *val = priv->multichannel;
205 *val = priv->variant;
207 case BOARD_HWVERSION:
208 *val = priv->hwversion;
211 debug("%s: Integer value %d unknown\n", dev->name, id);
218 static const struct udevice_id board_gazerbeam_ids[] = {
219 { .compatible = "gdsys,board_gazerbeam" },
223 static const struct board_ops board_gazerbeam_ops = {
224 .detect = board_gazerbeam_detect,
225 .get_int = board_gazerbeam_get_int,
228 static int board_gazerbeam_probe(struct udevice *dev)
230 struct board_gazerbeam_priv *priv = dev_get_priv(dev);
233 gpio_num = gpio_request_list_by_name(dev, "reset-gpios",
235 ARRAY_SIZE(priv->reset_gpios),
239 debug("%s: Error getting GPIO list 'reset-gpios' (err = %d)\n",
240 dev->name, gpio_num);
244 /* Set startup-finished GPIOs */
245 for (i = 0; i < ARRAY_SIZE(priv->reset_gpios); i++) {
246 int res = dm_gpio_set_value(&priv->reset_gpios[i], 0);
249 debug("%s: Error while setting GPIO %d (err = %d)\n",
258 U_BOOT_DRIVER(board_gazerbeam) = {
259 .name = "board_gazerbeam",
261 .of_match = board_gazerbeam_ids,
262 .ops = &board_gazerbeam_ops,
263 .priv_auto_alloc_size = sizeof(struct board_gazerbeam_priv),
264 .probe = board_gazerbeam_probe,