mmio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MMIO register bitfield-controlled multiplexer driver
  4. *
  5. * Copyright (C) 2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/err.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/module.h>
  11. #include <linux/mux/driver.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/property.h>
  15. #include <linux/regmap.h>
  16. static int mux_mmio_set(struct mux_control *mux, int state)
  17. {
  18. struct regmap_field **fields = mux_chip_priv(mux->chip);
  19. return regmap_field_write(fields[mux_control_get_index(mux)], state);
  20. }
  21. static const struct mux_control_ops mux_mmio_ops = {
  22. .set = mux_mmio_set,
  23. };
  24. static const struct of_device_id mux_mmio_dt_ids[] = {
  25. { .compatible = "mmio-mux", },
  26. { .compatible = "reg-mux", },
  27. { /* sentinel */ }
  28. };
  29. MODULE_DEVICE_TABLE(of, mux_mmio_dt_ids);
  30. static int mux_mmio_probe(struct platform_device *pdev)
  31. {
  32. struct device *dev = &pdev->dev;
  33. struct device_node *np = dev->of_node;
  34. struct regmap_field **fields;
  35. struct mux_chip *mux_chip;
  36. struct regmap *regmap;
  37. int num_fields;
  38. int ret;
  39. int i;
  40. if (of_device_is_compatible(np, "mmio-mux"))
  41. regmap = syscon_node_to_regmap(np->parent);
  42. else
  43. regmap = dev_get_regmap(dev->parent, NULL) ?: ERR_PTR(-ENODEV);
  44. if (IS_ERR(regmap)) {
  45. ret = PTR_ERR(regmap);
  46. dev_err(dev, "failed to get regmap: %d\n", ret);
  47. return ret;
  48. }
  49. ret = of_property_count_u32_elems(np, "mux-reg-masks");
  50. if (ret == 0 || ret % 2)
  51. ret = -EINVAL;
  52. if (ret < 0) {
  53. dev_err(dev, "mux-reg-masks property missing or invalid: %d\n",
  54. ret);
  55. return ret;
  56. }
  57. num_fields = ret / 2;
  58. mux_chip = devm_mux_chip_alloc(dev, num_fields, num_fields *
  59. sizeof(*fields));
  60. if (IS_ERR(mux_chip))
  61. return PTR_ERR(mux_chip);
  62. fields = mux_chip_priv(mux_chip);
  63. for (i = 0; i < num_fields; i++) {
  64. struct mux_control *mux = &mux_chip->mux[i];
  65. struct reg_field field;
  66. s32 idle_state = MUX_IDLE_AS_IS;
  67. u32 reg, mask;
  68. int bits;
  69. ret = of_property_read_u32_index(np, "mux-reg-masks",
  70. 2 * i, &reg);
  71. if (!ret)
  72. ret = of_property_read_u32_index(np, "mux-reg-masks",
  73. 2 * i + 1, &mask);
  74. if (ret < 0) {
  75. dev_err(dev, "bitfield %d: failed to read mux-reg-masks property: %d\n",
  76. i, ret);
  77. return ret;
  78. }
  79. field.reg = reg;
  80. field.msb = fls(mask) - 1;
  81. field.lsb = ffs(mask) - 1;
  82. if (mask != GENMASK(field.msb, field.lsb)) {
  83. dev_err(dev, "bitfield %d: invalid mask 0x%x\n",
  84. i, mask);
  85. return -EINVAL;
  86. }
  87. fields[i] = devm_regmap_field_alloc(dev, regmap, field);
  88. if (IS_ERR(fields[i])) {
  89. ret = PTR_ERR(fields[i]);
  90. dev_err(dev, "bitfield %d: failed allocate: %d\n",
  91. i, ret);
  92. return ret;
  93. }
  94. bits = 1 + field.msb - field.lsb;
  95. mux->states = 1 << bits;
  96. of_property_read_u32_index(np, "idle-states", i,
  97. (u32 *)&idle_state);
  98. if (idle_state != MUX_IDLE_AS_IS) {
  99. if (idle_state < 0 || idle_state >= mux->states) {
  100. dev_err(dev, "bitfield: %d: out of range idle state %d\n",
  101. i, idle_state);
  102. return -EINVAL;
  103. }
  104. mux->idle_state = idle_state;
  105. }
  106. }
  107. mux_chip->ops = &mux_mmio_ops;
  108. return devm_mux_chip_register(dev, mux_chip);
  109. }
  110. static struct platform_driver mux_mmio_driver = {
  111. .driver = {
  112. .name = "mmio-mux",
  113. .of_match_table = of_match_ptr(mux_mmio_dt_ids),
  114. },
  115. .probe = mux_mmio_probe,
  116. };
  117. module_platform_driver(mux_mmio_driver);
  118. MODULE_DESCRIPTION("MMIO register bitfield-controlled multiplexer driver");
  119. MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
  120. MODULE_LICENSE("GPL v2");