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> |
| 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 | /** |
| 29 | * struct board_gazerbeam_priv - Private data structure for the gazerbeam board |
| 30 | * driver. |
| 31 | * @reset_gpios: GPIOs for the board's reset GPIOs. |
| 32 | * @var_gpios: GPIOs for the board's hardware variant GPIOs |
| 33 | * @ver_gpios: GPIOs for the board's hardware version GPIOs |
| 34 | * @variant: Container for the board's hardware variant (CON/CPU) |
| 35 | * @multichannel: Container for the board's multichannel variant (MC4/MC2/SC) |
| 36 | * @hwversion: Container for the board's hardware version |
| 37 | */ |
| 38 | struct board_gazerbeam_priv { |
| 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 | /** |
| 48 | * _read_board_variant_data() - Read variant information from the hardware. |
| 49 | * @dev: The board device for which to determine the multichannel and device |
| 50 | * type information. |
| 51 | * |
| 52 | * The data read from the board's hardware (mostly hard-wired GPIOs) is stored |
| 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 | */ |
| 58 | static int _read_board_variant_data(struct udevice *dev) |
| 59 | { |
| 60 | struct board_gazerbeam_priv *priv = dev_get_priv(dev); |
| 61 | struct udevice *i2c_bus; |
| 62 | struct udevice *dummy; |
| 63 | char *listname; |
| 64 | int mc4, mc2, sc, con; |
| 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 | |
| 81 | mc2 = !dm_i2c_probe(i2c_bus, MC2_EXPANDER_ADDR, 0, &dummy); |
| 82 | mc4 = !dm_i2c_probe(i2c_bus, MC4_EXPANDER_ADDR, 0, &dummy); |
| 83 | |
| 84 | if (mc2 && mc4) { |
| 85 | debug("%s: Board hardware configuration inconsistent.\n", |
| 86 | dev->name); |
| 87 | return -EINVAL; |
| 88 | } |
| 89 | |
| 90 | listname = mc2 ? "var-gpios-mc2" : "var-gpios-mc4"; |
| 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 | |
| 108 | con = dm_gpio_get_value(&priv->var_gpios[CON_GPIO_NO]); |
| 109 | if (con < 0) { |
| 110 | debug("%s: Error while reading 'con' GPIO (err = %d)", |
| 111 | dev->name, con); |
| 112 | return con; |
| 113 | } |
| 114 | |
| 115 | if ((sc && mc2) || (sc && mc4) || (!sc && !mc2 && !mc4)) { |
| 116 | debug("%s: Board hardware configuration inconsistent.\n", |
| 117 | dev->name); |
| 118 | return -EINVAL; |
| 119 | } |
| 120 | |
| 121 | priv->variant = con ? VAR_CON : VAR_CPU; |
| 122 | |
| 123 | priv->multichannel = mc4 ? 4 : (mc2 ? 2 : (sc ? 1 : 0)); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * _read_hwversion() - Read the hardware version from the board. |
| 130 | * @dev: The board device for which to read the hardware version. |
| 131 | * |
| 132 | * The hardware version read from the board (from hard-wired GPIOs) is stored |
| 133 | * in the private data structure of the driver to be used by other driver |
| 134 | * methods. |
| 135 | * |
| 136 | * Return: 0 if OK, -ve on error. |
| 137 | */ |
| 138 | static int _read_hwversion(struct udevice *dev) |
| 139 | { |
| 140 | struct board_gazerbeam_priv *priv = dev_get_priv(dev); |
| 141 | int res; |
| 142 | |
| 143 | res = gpio_request_list_by_name(dev, "ver-gpios", priv->ver_gpios, |
| 144 | ARRAY_SIZE(priv->ver_gpios), |
| 145 | GPIOD_IS_IN); |
| 146 | if (res < 0) { |
| 147 | debug("%s: Error getting GPIO list 'ver-gpios' (err = %d)\n", |
| 148 | dev->name, res); |
| 149 | return -ENODEV; |
| 150 | } |
| 151 | |
| 152 | res = dm_gpio_get_values_as_int(priv->ver_gpios, |
| 153 | ARRAY_SIZE(priv->ver_gpios)); |
| 154 | if (res < 0) { |
| 155 | debug("%s: Error reading HW version from expander (err = %d)\n", |
| 156 | dev->name, res); |
| 157 | return res; |
| 158 | } |
| 159 | |
| 160 | priv->hwversion = res; |
| 161 | |
| 162 | res = gpio_free_list(dev, priv->ver_gpios, ARRAY_SIZE(priv->ver_gpios)); |
| 163 | if (res < 0) { |
| 164 | debug("%s: Error freeing HW version GPIO list (err = %d)\n", |
| 165 | dev->name, res); |
| 166 | return res; |
| 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static int board_gazerbeam_detect(struct udevice *dev) |
| 173 | { |
| 174 | int res; |
| 175 | |
| 176 | res = _read_board_variant_data(dev); |
| 177 | if (res) { |
| 178 | debug("%s: Error reading multichannel variant (err = %d)\n", |
| 179 | dev->name, res); |
| 180 | return res; |
| 181 | } |
| 182 | |
| 183 | res = _read_hwversion(dev); |
| 184 | if (res) { |
| 185 | debug("%s: Error reading hardware version (err = %d)\n", |
| 186 | dev->name, res); |
| 187 | return res; |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int board_gazerbeam_get_int(struct udevice *dev, int id, int *val) |
| 194 | { |
| 195 | struct board_gazerbeam_priv *priv = dev_get_priv(dev); |
| 196 | |
| 197 | switch (id) { |
| 198 | case BOARD_MULTICHANNEL: |
| 199 | *val = priv->multichannel; |
| 200 | break; |
| 201 | case BOARD_VARIANT: |
| 202 | *val = priv->variant; |
| 203 | break; |
| 204 | case BOARD_HWVERSION: |
| 205 | *val = priv->hwversion; |
| 206 | break; |
| 207 | default: |
| 208 | debug("%s: Integer value %d unknown\n", dev->name, id); |
| 209 | return -EINVAL; |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static const struct udevice_id board_gazerbeam_ids[] = { |
| 216 | { .compatible = "gdsys,board_gazerbeam" }, |
| 217 | { /* sentinel */ } |
| 218 | }; |
| 219 | |
| 220 | static const struct board_ops board_gazerbeam_ops = { |
| 221 | .detect = board_gazerbeam_detect, |
| 222 | .get_int = board_gazerbeam_get_int, |
| 223 | }; |
| 224 | |
| 225 | static int board_gazerbeam_probe(struct udevice *dev) |
| 226 | { |
| 227 | struct board_gazerbeam_priv *priv = dev_get_priv(dev); |
| 228 | int gpio_num, i; |
| 229 | |
| 230 | gpio_num = gpio_request_list_by_name(dev, "reset-gpios", |
| 231 | priv->reset_gpios, |
| 232 | ARRAY_SIZE(priv->reset_gpios), |
| 233 | GPIOD_IS_OUT); |
| 234 | |
| 235 | if (gpio_num < 0) { |
| 236 | debug("%s: Error getting GPIO list 'reset-gpios' (err = %d)\n", |
| 237 | dev->name, gpio_num); |
| 238 | return gpio_num; |
| 239 | } |
| 240 | |
| 241 | /* Set startup-finished GPIOs */ |
| 242 | for (i = 0; i < ARRAY_SIZE(priv->reset_gpios); i++) { |
| 243 | int res = dm_gpio_set_value(&priv->reset_gpios[i], 0); |
| 244 | |
| 245 | if (res) { |
| 246 | debug("%s: Error while setting GPIO %d (err = %d)\n", |
| 247 | dev->name, i, res); |
| 248 | return res; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | U_BOOT_DRIVER(board_gazerbeam) = { |
| 256 | .name = "board_gazerbeam", |
| 257 | .id = UCLASS_BOARD, |
| 258 | .of_match = board_gazerbeam_ids, |
| 259 | .ops = &board_gazerbeam_ops, |
| 260 | .priv_auto_alloc_size = sizeof(struct board_gazerbeam_priv), |
| 261 | .probe = board_gazerbeam_probe, |
| 262 | }; |