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