gazerbeam.c 6.6 KB

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