i2c-mux-mule.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Theobroma Systems Mule I2C device multiplexer
  4. *
  5. * Copyright (C) 2024 Theobroma Systems Design und Consulting GmbH
  6. */
  7. #include <linux/i2c-mux.h>
  8. #include <linux/i2c.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/property.h>
  13. #include <linux/regmap.h>
  14. #define MULE_I2C_MUX_CONFIG_REG 0xff
  15. #define MULE_I2C_MUX_DEFAULT_DEV 0x0
  16. struct mule_i2c_reg_mux {
  17. struct regmap *regmap;
  18. };
  19. static int mule_i2c_mux_select(struct i2c_mux_core *muxc, u32 dev)
  20. {
  21. struct mule_i2c_reg_mux *mux = muxc->priv;
  22. return regmap_write(mux->regmap, MULE_I2C_MUX_CONFIG_REG, dev);
  23. }
  24. static int mule_i2c_mux_deselect(struct i2c_mux_core *muxc, u32 dev)
  25. {
  26. return mule_i2c_mux_select(muxc, MULE_I2C_MUX_DEFAULT_DEV);
  27. }
  28. static void mule_i2c_mux_remove(void *data)
  29. {
  30. struct i2c_mux_core *muxc = data;
  31. i2c_mux_del_adapters(muxc);
  32. mule_i2c_mux_deselect(muxc, MULE_I2C_MUX_DEFAULT_DEV);
  33. }
  34. static int mule_i2c_mux_probe(struct platform_device *pdev)
  35. {
  36. struct device *mux_dev = &pdev->dev;
  37. struct mule_i2c_reg_mux *priv;
  38. struct i2c_client *client;
  39. struct i2c_mux_core *muxc;
  40. unsigned int readback;
  41. int ndev, ret;
  42. bool old_fw;
  43. /* Count devices on the mux */
  44. ndev = of_get_child_count(mux_dev->of_node);
  45. dev_dbg(mux_dev, "%d devices on the mux\n", ndev);
  46. client = to_i2c_client(mux_dev->parent);
  47. muxc = i2c_mux_alloc(client->adapter, mux_dev, ndev, sizeof(*priv),
  48. I2C_MUX_LOCKED, mule_i2c_mux_select, mule_i2c_mux_deselect);
  49. if (!muxc)
  50. return -ENOMEM;
  51. priv = i2c_mux_priv(muxc);
  52. priv->regmap = dev_get_regmap(mux_dev->parent, NULL);
  53. if (!priv->regmap)
  54. return dev_err_probe(mux_dev, -ENODEV,
  55. "No parent i2c register map\n");
  56. platform_set_drvdata(pdev, muxc);
  57. /*
  58. * MULE_I2C_MUX_DEFAULT_DEV is guaranteed to exist on all old and new
  59. * mule fw. Mule fw without mux support will accept write ops to the
  60. * config register, but readback returns 0xff (register not updated).
  61. */
  62. ret = mule_i2c_mux_select(muxc, MULE_I2C_MUX_DEFAULT_DEV);
  63. if (ret)
  64. return dev_err_probe(mux_dev, ret,
  65. "Failed to write config register\n");
  66. ret = regmap_read(priv->regmap, MULE_I2C_MUX_CONFIG_REG, &readback);
  67. if (ret)
  68. return dev_err_probe(mux_dev, ret,
  69. "Failed to read config register\n");
  70. old_fw = (readback != MULE_I2C_MUX_DEFAULT_DEV);
  71. ret = devm_add_action_or_reset(mux_dev, mule_i2c_mux_remove, muxc);
  72. if (ret)
  73. return dev_err_probe(mux_dev, ret,
  74. "Failed to register mux remove\n");
  75. /* Create device adapters */
  76. for_each_child_of_node_scoped(mux_dev->of_node, dev) {
  77. u32 reg;
  78. ret = of_property_read_u32(dev, "reg", &reg);
  79. if (ret)
  80. return dev_err_probe(mux_dev, ret,
  81. "No reg property found for %s\n",
  82. of_node_full_name(dev));
  83. if (old_fw && reg != 0) {
  84. dev_warn(mux_dev,
  85. "Mux is not supported, please update Mule FW\n");
  86. continue;
  87. }
  88. ret = mule_i2c_mux_select(muxc, reg);
  89. if (ret) {
  90. dev_warn(mux_dev,
  91. "Device %d not supported, please update Mule FW\n", reg);
  92. continue;
  93. }
  94. ret = i2c_mux_add_adapter(muxc, 0, reg);
  95. if (ret)
  96. return ret;
  97. }
  98. mule_i2c_mux_deselect(muxc, MULE_I2C_MUX_DEFAULT_DEV);
  99. return 0;
  100. }
  101. static const struct of_device_id mule_i2c_mux_of_match[] = {
  102. { .compatible = "tsd,mule-i2c-mux", },
  103. {},
  104. };
  105. MODULE_DEVICE_TABLE(of, mule_i2c_mux_of_match);
  106. static struct platform_driver mule_i2c_mux_driver = {
  107. .driver = {
  108. .name = "mule-i2c-mux",
  109. .of_match_table = mule_i2c_mux_of_match,
  110. },
  111. .probe = mule_i2c_mux_probe,
  112. };
  113. module_platform_driver(mule_i2c_mux_driver);
  114. MODULE_AUTHOR("Farouk Bouabid <farouk.bouabid@cherry.de>");
  115. MODULE_DESCRIPTION("I2C mux driver for Theobroma Systems Mule");
  116. MODULE_LICENSE("GPL");