i2c-mux-gpio.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * I2C multiplexer using GPIO API
  4. *
  5. * Copyright 2017 NXP
  6. *
  7. * Peng Fan <peng.fan@nxp.com>
  8. */
  9. #include <asm/io.h>
  10. #include <asm-generic/gpio.h>
  11. #include <common.h>
  12. #include <dm.h>
  13. #include <dm/pinctrl.h>
  14. #include <fdtdec.h>
  15. #include <i2c.h>
  16. #include <linux/errno.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /**
  19. * struct i2c_mux_gpio_priv - private data for i2c mux gpio
  20. *
  21. * @values: the reg value of each child node
  22. * @n_values: num of regs
  23. * @gpios: the mux-gpios array
  24. * @n_gpios: num of gpios in mux-gpios
  25. * @idle: the value of idle-state
  26. */
  27. struct i2c_mux_gpio_priv {
  28. u32 *values;
  29. int n_values;
  30. struct gpio_desc *gpios;
  31. int n_gpios;
  32. u32 idle;
  33. };
  34. static int i2c_mux_gpio_select(struct udevice *dev, struct udevice *bus,
  35. uint channel)
  36. {
  37. struct i2c_mux_gpio_priv *priv = dev_get_priv(dev);
  38. int i, ret;
  39. for (i = 0; i < priv->n_gpios; i++) {
  40. ret = dm_gpio_set_value(&priv->gpios[i], (channel >> i) & 1);
  41. if (ret)
  42. return ret;
  43. }
  44. return 0;
  45. }
  46. static int i2c_mux_gpio_deselect(struct udevice *dev, struct udevice *bus,
  47. uint channel)
  48. {
  49. struct i2c_mux_gpio_priv *priv = dev_get_priv(dev);
  50. int i, ret;
  51. for (i = 0; i < priv->n_gpios; i++) {
  52. ret = dm_gpio_set_value(&priv->gpios[i], (priv->idle >> i) & 1);
  53. if (ret)
  54. return ret;
  55. }
  56. return 0;
  57. }
  58. static int i2c_mux_gpio_probe(struct udevice *dev)
  59. {
  60. const void *fdt = gd->fdt_blob;
  61. int node = dev_of_offset(dev);
  62. struct i2c_mux_gpio_priv *mux = dev_get_priv(dev);
  63. struct gpio_desc *gpios;
  64. u32 *values;
  65. int i = 0, subnode, ret;
  66. mux->n_values = fdtdec_get_child_count(fdt, node);
  67. values = devm_kzalloc(dev, sizeof(*mux->values) * mux->n_values,
  68. GFP_KERNEL);
  69. if (!values) {
  70. dev_err(dev, "Cannot alloc values array");
  71. return -ENOMEM;
  72. }
  73. fdt_for_each_subnode(subnode, fdt, node) {
  74. *(values + i) = fdtdec_get_uint(fdt, subnode, "reg", -1);
  75. i++;
  76. }
  77. mux->values = values;
  78. mux->idle = fdtdec_get_uint(fdt, node, "idle-state", -1);
  79. mux->n_gpios = gpio_get_list_count(dev, "mux-gpios");
  80. if (mux->n_gpios < 0) {
  81. dev_err(dev, "Missing mux-gpios property\n");
  82. return -EINVAL;
  83. }
  84. gpios = devm_kzalloc(dev, sizeof(struct gpio_desc) * mux->n_gpios,
  85. GFP_KERNEL);
  86. if (!gpios) {
  87. dev_err(dev, "Cannot allocate gpios array\n");
  88. return -ENOMEM;
  89. }
  90. ret = gpio_request_list_by_name(dev, "mux-gpios", gpios, mux->n_gpios,
  91. GPIOD_IS_OUT_ACTIVE);
  92. if (ret <= 0) {
  93. dev_err(dev, "Failed to request mux-gpios\n");
  94. return ret;
  95. }
  96. mux->gpios = gpios;
  97. return 0;
  98. }
  99. static const struct i2c_mux_ops i2c_mux_gpio_ops = {
  100. .select = i2c_mux_gpio_select,
  101. .deselect = i2c_mux_gpio_deselect,
  102. };
  103. static const struct udevice_id i2c_mux_gpio_ids[] = {
  104. { .compatible = "i2c-mux-gpio", },
  105. {}
  106. };
  107. U_BOOT_DRIVER(i2c_mux_gpio) = {
  108. .name = "i2c_mux_gpio",
  109. .id = UCLASS_I2C_MUX,
  110. .of_match = i2c_mux_gpio_ids,
  111. .ops = &i2c_mux_gpio_ops,
  112. .probe = i2c_mux_gpio_probe,
  113. .priv_auto_alloc_size = sizeof(struct i2c_mux_gpio_priv),
  114. };