blob: c1fae6ccf2a63e4578203c037f29dfb37c33a897 [file] [log] [blame]
Mario Six6238ae42018-07-31 11:44:12 +02001// 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>
Simon Glass3a8ee3d2020-11-05 06:32:05 -07009#include <sysinfo.h>
Mario Six6238ae42018-07-31 11:44:12 +020010#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Mario Six6238ae42018-07-31 11:44:12 +020012#include <asm/gpio.h>
13
14#include "gazerbeam.h"
15
16/* Sequence number of I2C bus that holds the GPIO expanders */
17static const int I2C_BUS_SEQ_NO = 1;
18
19/* I2C address of SC/MC2 expander */
20static const int MC2_EXPANDER_ADDR = 0x20;
21/* I2C address of MC4 expander */
22static const int MC4_EXPANDER_ADDR = 0x22;
23
24/* Number of the GPIO to read the SC data from */
25static const int SC_GPIO_NO;
26/* Number of the GPIO to read the CON data from */
27static const int CON_GPIO_NO = 1;
28
29/**
Simon Glass3a8ee3d2020-11-05 06:32:05 -070030 * 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 Six6238ae42018-07-31 11:44:12 +020038 */
Simon Glass3a8ee3d2020-11-05 06:32:05 -070039struct sysinfo_gazerbeam_priv {
Mario Six6238ae42018-07-31 11:44:12 +020040 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 Glass3a8ee3d2020-11-05 06:32:05 -070049 * _read_sysinfo_variant_data() - Read variant information from the hardware.
50 * @dev: The sysinfo device for which to determine the multichannel and device
Mario Six6238ae42018-07-31 11:44:12 +020051 * type information.
52 *
Simon Glass3a8ee3d2020-11-05 06:32:05 -070053 * The data read from the sysinfo's hardware (mostly hard-wired GPIOs) is stored
Mario Six6238ae42018-07-31 11:44:12 +020054 * 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 Glass3a8ee3d2020-11-05 06:32:05 -070059static int _read_sysinfo_variant_data(struct udevice *dev)
Mario Six6238ae42018-07-31 11:44:12 +020060{
Simon Glass3a8ee3d2020-11-05 06:32:05 -070061 struct sysinfo_gazerbeam_priv *priv = dev_get_priv(dev);
Mario Six6238ae42018-07-31 11:44:12 +020062 struct udevice *i2c_bus;
63 struct udevice *dummy;
64 char *listname;
Mario Six96388792019-03-29 10:18:16 +010065 int mc4, mc2, sc, mc2_sc, con;
Mario Six6238ae42018-07-31 11:44:12 +020066 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 Six96388792019-03-29 10:18:16 +010082 mc2_sc = !dm_i2c_probe(i2c_bus, MC2_EXPANDER_ADDR, 0, &dummy);
Mario Six6238ae42018-07-31 11:44:12 +020083 mc4 = !dm_i2c_probe(i2c_bus, MC4_EXPANDER_ADDR, 0, &dummy);
84
Mario Six96388792019-03-29 10:18:16 +010085 if (mc2_sc && mc4) {
Mario Six6238ae42018-07-31 11:44:12 +020086 debug("%s: Board hardware configuration inconsistent.\n",
87 dev->name);
88 return -EINVAL;
89 }
90
Mario Six96388792019-03-29 10:18:16 +010091 listname = mc2_sc ? "var-gpios-mc2" : "var-gpios-mc4";
Mario Six6238ae42018-07-31 11:44:12 +020092
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 Six96388792019-03-29 10:18:16 +0100109 mc2 = mc2_sc ? (sc ? 0 : 1) : 0;
Mario Six6238ae42018-07-31 11:44:12 +0200110
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 Six96388792019-03-29 10:18:16 +0100117 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 Six6238ae42018-07-31 11:44:12 +0200124 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 Glass3a8ee3d2020-11-05 06:32:05 -0700132 * _read_hwversion() - Read the hardware version from the sysinfo.
133 * @dev: The sysinfo device for which to read the hardware version.
Mario Six6238ae42018-07-31 11:44:12 +0200134 *
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700135 * The hardware version read from the sysinfo (from hard-wired GPIOs) is stored
Mario Six6238ae42018-07-31 11:44:12 +0200136 * 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 */
141static int _read_hwversion(struct udevice *dev)
142{
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700143 struct sysinfo_gazerbeam_priv *priv = dev_get_priv(dev);
Mario Six6238ae42018-07-31 11:44:12 +0200144 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 Glass3a8ee3d2020-11-05 06:32:05 -0700175static int sysinfo_gazerbeam_detect(struct udevice *dev)
Mario Six6238ae42018-07-31 11:44:12 +0200176{
177 int res;
178
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700179 res = _read_sysinfo_variant_data(dev);
Mario Six6238ae42018-07-31 11:44:12 +0200180 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 Glass3a8ee3d2020-11-05 06:32:05 -0700196static int sysinfo_gazerbeam_get_int(struct udevice *dev, int id, int *val)
Mario Six6238ae42018-07-31 11:44:12 +0200197{
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700198 struct sysinfo_gazerbeam_priv *priv = dev_get_priv(dev);
Mario Six6238ae42018-07-31 11:44:12 +0200199
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 Glass3a8ee3d2020-11-05 06:32:05 -0700218static const struct udevice_id sysinfo_gazerbeam_ids[] = {
219 { .compatible = "gdsys,sysinfo-gazerbeam" },
Mario Six6238ae42018-07-31 11:44:12 +0200220 { /* sentinel */ }
221};
222
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700223static const struct sysinfo_ops sysinfo_gazerbeam_ops = {
224 .detect = sysinfo_gazerbeam_detect,
225 .get_int = sysinfo_gazerbeam_get_int,
Mario Six6238ae42018-07-31 11:44:12 +0200226};
227
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700228static int sysinfo_gazerbeam_probe(struct udevice *dev)
Mario Six6238ae42018-07-31 11:44:12 +0200229{
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700230 struct sysinfo_gazerbeam_priv *priv = dev_get_priv(dev);
Mario Six6238ae42018-07-31 11:44:12 +0200231 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 Glass3a8ee3d2020-11-05 06:32:05 -0700258U_BOOT_DRIVER(sysinfo_gazerbeam) = {
259 .name = "sysinfo_gazerbeam",
260 .id = UCLASS_SYSINFO,
261 .of_match = sysinfo_gazerbeam_ids,
262 .ops = &sysinfo_gazerbeam_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700263 .priv_auto = sizeof(struct sysinfo_gazerbeam_priv),
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700264 .probe = sysinfo_gazerbeam_probe,
Mario Six6238ae42018-07-31 11:44:12 +0200265};