Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +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 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 7 | #include <common.h> |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 9 | #include <sysinfo.h> |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 10 | #include <i2c.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 12 | #include <asm/gpio.h> |
| 13 | |
| 14 | #include "gazerbeam.h" |
| 15 | |
| 16 | /* Sequence number of I2C bus that holds the GPIO expanders */ |
| 17 | static const int I2C_BUS_SEQ_NO = 1; |
| 18 | |
| 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; |
| 23 | |
| 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; |
| 28 | |
| 29 | /** |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 30 | * struct sysinfo_gazerbeam_priv - Private data structure for the gazerbeam |
| 31 | * sysinfo driver |
| 32 | * @reset_gpios: GPIOs for the sysinfo's reset GPIOs. |
| 33 | * @var_gpios: GPIOs for the sysinfo's hardware variant GPIOs |
| 34 | * @ver_gpios: GPIOs for the sysinfo's hardware version GPIOs |
| 35 | * @variant: Container for the sysinfo's hardware variant (CON/CPU) |
| 36 | * @multichannel: Container for the sysinfo's multichannel variant (MC4/MC2/SC) |
| 37 | * @hwversion: Container for the sysinfo's hardware version |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 38 | */ |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 39 | struct sysinfo_gazerbeam_priv { |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 40 | struct gpio_desc reset_gpios[2]; |
| 41 | struct gpio_desc var_gpios[2]; |
| 42 | struct gpio_desc ver_gpios[4]; |
| 43 | int variant; |
| 44 | int multichannel; |
| 45 | int hwversion; |
| 46 | }; |
| 47 | |
| 48 | /** |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 49 | * _read_sysinfo_variant_data() - Read variant information from the hardware. |
| 50 | * @dev: The sysinfo device for which to determine the multichannel and device |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 51 | * type information. |
| 52 | * |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 53 | * The data read from the sysinfo's hardware (mostly hard-wired GPIOs) is stored |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 54 | * in the private data structure of the driver to be used by other driver |
| 55 | * methods. |
| 56 | * |
| 57 | * Return: 0 if OK, -ve on error. |
| 58 | */ |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 59 | static int _read_sysinfo_variant_data(struct udevice *dev) |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 60 | { |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 61 | struct sysinfo_gazerbeam_priv *priv = dev_get_priv(dev); |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 62 | struct udevice *i2c_bus; |
| 63 | struct udevice *dummy; |
| 64 | char *listname; |
Mario Six | 581b681 | 2019-03-29 10:18:16 +0100 | [diff] [blame] | 65 | int mc4, mc2, sc, mc2_sc, con; |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 66 | int gpio_num; |
| 67 | int res; |
| 68 | |
| 69 | res = uclass_get_device_by_seq(UCLASS_I2C, I2C_BUS_SEQ_NO, &i2c_bus); |
| 70 | if (res) { |
| 71 | debug("%s: Could not get I2C bus %d (err = %d)\n", |
| 72 | dev->name, I2C_BUS_SEQ_NO, res); |
| 73 | return res; |
| 74 | } |
| 75 | |
| 76 | if (!i2c_bus) { |
| 77 | debug("%s: Could not get I2C bus %d\n", |
| 78 | dev->name, I2C_BUS_SEQ_NO); |
| 79 | return -EIO; |
| 80 | } |
| 81 | |
Mario Six | 581b681 | 2019-03-29 10:18:16 +0100 | [diff] [blame] | 82 | mc2_sc = !dm_i2c_probe(i2c_bus, MC2_EXPANDER_ADDR, 0, &dummy); |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 83 | mc4 = !dm_i2c_probe(i2c_bus, MC4_EXPANDER_ADDR, 0, &dummy); |
| 84 | |
Mario Six | 581b681 | 2019-03-29 10:18:16 +0100 | [diff] [blame] | 85 | if (mc2_sc && mc4) { |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 86 | debug("%s: Board hardware configuration inconsistent.\n", |
| 87 | dev->name); |
| 88 | return -EINVAL; |
| 89 | } |
| 90 | |
Mario Six | 581b681 | 2019-03-29 10:18:16 +0100 | [diff] [blame] | 91 | listname = mc2_sc ? "var-gpios-mc2" : "var-gpios-mc4"; |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 92 | |
| 93 | gpio_num = gpio_request_list_by_name(dev, listname, priv->var_gpios, |
| 94 | ARRAY_SIZE(priv->var_gpios), |
| 95 | GPIOD_IS_IN); |
| 96 | if (gpio_num < 0) { |
| 97 | debug("%s: Requesting gpio list %s failed (err = %d).\n", |
| 98 | dev->name, listname, gpio_num); |
| 99 | return gpio_num; |
| 100 | } |
| 101 | |
| 102 | sc = dm_gpio_get_value(&priv->var_gpios[SC_GPIO_NO]); |
| 103 | if (sc < 0) { |
| 104 | debug("%s: Error while reading 'sc' GPIO (err = %d)", |
| 105 | dev->name, sc); |
| 106 | return sc; |
| 107 | } |
| 108 | |
Mario Six | 581b681 | 2019-03-29 10:18:16 +0100 | [diff] [blame] | 109 | mc2 = mc2_sc ? (sc ? 0 : 1) : 0; |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 110 | |
| 111 | if ((sc && mc2) || (sc && mc4) || (!sc && !mc2 && !mc4)) { |
| 112 | debug("%s: Board hardware configuration inconsistent.\n", |
| 113 | dev->name); |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | |
Mario Six | 581b681 | 2019-03-29 10:18:16 +0100 | [diff] [blame] | 117 | con = dm_gpio_get_value(&priv->var_gpios[CON_GPIO_NO]); |
| 118 | if (con < 0) { |
| 119 | debug("%s: Error while reading 'con' GPIO (err = %d)", |
| 120 | dev->name, con); |
| 121 | return con; |
| 122 | } |
| 123 | |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 124 | priv->variant = con ? VAR_CON : VAR_CPU; |
| 125 | |
| 126 | priv->multichannel = mc4 ? 4 : (mc2 ? 2 : (sc ? 1 : 0)); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /** |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 132 | * _read_hwversion() - Read the hardware version from the sysinfo. |
| 133 | * @dev: The sysinfo device for which to read the hardware version. |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 134 | * |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 135 | * The hardware version read from the sysinfo (from hard-wired GPIOs) is stored |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 136 | * in the private data structure of the driver to be used by other driver |
| 137 | * methods. |
| 138 | * |
| 139 | * Return: 0 if OK, -ve on error. |
| 140 | */ |
| 141 | static int _read_hwversion(struct udevice *dev) |
| 142 | { |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 143 | struct sysinfo_gazerbeam_priv *priv = dev_get_priv(dev); |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 144 | int res; |
| 145 | |
| 146 | res = gpio_request_list_by_name(dev, "ver-gpios", priv->ver_gpios, |
| 147 | ARRAY_SIZE(priv->ver_gpios), |
| 148 | GPIOD_IS_IN); |
| 149 | if (res < 0) { |
| 150 | debug("%s: Error getting GPIO list 'ver-gpios' (err = %d)\n", |
| 151 | dev->name, res); |
| 152 | return -ENODEV; |
| 153 | } |
| 154 | |
| 155 | res = dm_gpio_get_values_as_int(priv->ver_gpios, |
| 156 | ARRAY_SIZE(priv->ver_gpios)); |
| 157 | if (res < 0) { |
| 158 | debug("%s: Error reading HW version from expander (err = %d)\n", |
| 159 | dev->name, res); |
| 160 | return res; |
| 161 | } |
| 162 | |
| 163 | priv->hwversion = res; |
| 164 | |
| 165 | res = gpio_free_list(dev, priv->ver_gpios, ARRAY_SIZE(priv->ver_gpios)); |
| 166 | if (res < 0) { |
| 167 | debug("%s: Error freeing HW version GPIO list (err = %d)\n", |
| 168 | dev->name, res); |
| 169 | return res; |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 175 | static int sysinfo_gazerbeam_detect(struct udevice *dev) |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 176 | { |
| 177 | int res; |
| 178 | |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 179 | res = _read_sysinfo_variant_data(dev); |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 180 | if (res) { |
| 181 | debug("%s: Error reading multichannel variant (err = %d)\n", |
| 182 | dev->name, res); |
| 183 | return res; |
| 184 | } |
| 185 | |
| 186 | res = _read_hwversion(dev); |
| 187 | if (res) { |
| 188 | debug("%s: Error reading hardware version (err = %d)\n", |
| 189 | dev->name, res); |
| 190 | return res; |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 196 | static int sysinfo_gazerbeam_get_int(struct udevice *dev, int id, int *val) |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 197 | { |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 198 | struct sysinfo_gazerbeam_priv *priv = dev_get_priv(dev); |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 199 | |
| 200 | switch (id) { |
| 201 | case BOARD_MULTICHANNEL: |
| 202 | *val = priv->multichannel; |
| 203 | break; |
| 204 | case BOARD_VARIANT: |
| 205 | *val = priv->variant; |
| 206 | break; |
| 207 | case BOARD_HWVERSION: |
| 208 | *val = priv->hwversion; |
| 209 | break; |
| 210 | default: |
| 211 | debug("%s: Integer value %d unknown\n", dev->name, id); |
| 212 | return -EINVAL; |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 218 | static const struct udevice_id sysinfo_gazerbeam_ids[] = { |
| 219 | { .compatible = "gdsys,sysinfo-gazerbeam" }, |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 220 | { /* sentinel */ } |
| 221 | }; |
| 222 | |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 223 | static const struct sysinfo_ops sysinfo_gazerbeam_ops = { |
| 224 | .detect = sysinfo_gazerbeam_detect, |
| 225 | .get_int = sysinfo_gazerbeam_get_int, |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 226 | }; |
| 227 | |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 228 | static int sysinfo_gazerbeam_probe(struct udevice *dev) |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 229 | { |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 230 | struct sysinfo_gazerbeam_priv *priv = dev_get_priv(dev); |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 231 | int gpio_num, i; |
| 232 | |
| 233 | gpio_num = gpio_request_list_by_name(dev, "reset-gpios", |
| 234 | priv->reset_gpios, |
| 235 | ARRAY_SIZE(priv->reset_gpios), |
| 236 | GPIOD_IS_OUT); |
| 237 | |
| 238 | if (gpio_num < 0) { |
| 239 | debug("%s: Error getting GPIO list 'reset-gpios' (err = %d)\n", |
| 240 | dev->name, gpio_num); |
| 241 | return gpio_num; |
| 242 | } |
| 243 | |
| 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); |
| 247 | |
| 248 | if (res) { |
| 249 | debug("%s: Error while setting GPIO %d (err = %d)\n", |
| 250 | dev->name, i, res); |
| 251 | return res; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 258 | U_BOOT_DRIVER(sysinfo_gazerbeam) = { |
| 259 | .name = "sysinfo_gazerbeam", |
| 260 | .id = UCLASS_SYSINFO, |
| 261 | .of_match = sysinfo_gazerbeam_ids, |
| 262 | .ops = &sysinfo_gazerbeam_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 263 | .priv_auto = sizeof(struct sysinfo_gazerbeam_priv), |
Simon Glass | 458b66a | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 264 | .probe = sysinfo_gazerbeam_probe, |
Mario Six | a7dfbd0 | 2018-07-31 11:44:12 +0200 | [diff] [blame] | 265 | }; |