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 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <board.h> |
| 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 | /** |
| 30 | * struct board_gazerbeam_priv - Private data structure for the gazerbeam board |
| 31 | * driver. |
| 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 |
| 38 | */ |
| 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]; |
| 43 | int variant; |
| 44 | int multichannel; |
| 45 | int hwversion; |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * _read_board_variant_data() - Read variant information from the hardware. |
| 50 | * @dev: The board device for which to determine the multichannel and device |
| 51 | * type information. |
| 52 | * |
| 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 |
| 55 | * methods. |
| 56 | * |
| 57 | * Return: 0 if OK, -ve on error. |
| 58 | */ |
| 59 | static int _read_board_variant_data(struct udevice *dev) |
| 60 | { |
| 61 | struct board_gazerbeam_priv *priv = dev_get_priv(dev); |
| 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 | /** |
| 132 | * _read_hwversion() - Read the hardware version from the board. |
| 133 | * @dev: The board device for which to read the hardware version. |
| 134 | * |
| 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 |
| 137 | * methods. |
| 138 | * |
| 139 | * Return: 0 if OK, -ve on error. |
| 140 | */ |
| 141 | static int _read_hwversion(struct udevice *dev) |
| 142 | { |
| 143 | struct board_gazerbeam_priv *priv = dev_get_priv(dev); |
| 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 | |
| 175 | static int board_gazerbeam_detect(struct udevice *dev) |
| 176 | { |
| 177 | int res; |
| 178 | |
| 179 | res = _read_board_variant_data(dev); |
| 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 | |
| 196 | static int board_gazerbeam_get_int(struct udevice *dev, int id, int *val) |
| 197 | { |
| 198 | struct board_gazerbeam_priv *priv = dev_get_priv(dev); |
| 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 | |
| 218 | static const struct udevice_id board_gazerbeam_ids[] = { |
| 219 | { .compatible = "gdsys,board_gazerbeam" }, |
| 220 | { /* sentinel */ } |
| 221 | }; |
| 222 | |
| 223 | static const struct board_ops board_gazerbeam_ops = { |
| 224 | .detect = board_gazerbeam_detect, |
| 225 | .get_int = board_gazerbeam_get_int, |
| 226 | }; |
| 227 | |
| 228 | static int board_gazerbeam_probe(struct udevice *dev) |
| 229 | { |
| 230 | struct board_gazerbeam_priv *priv = dev_get_priv(dev); |
| 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 | |
| 258 | U_BOOT_DRIVER(board_gazerbeam) = { |
| 259 | .name = "board_gazerbeam", |
| 260 | .id = UCLASS_BOARD, |
| 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, |
| 265 | }; |