mmio.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. { /* sentinel */ }
  27. };
  28. MODULE_DEVICE_TABLE(of, mux_mmio_dt_ids);
  29. static int mux_mmio_probe(struct platform_device *pdev)
  30. {
  31. struct device *dev = &pdev->dev;
  32. struct device_node *np = dev->of_node;
  33. struct regmap_field **fields;
  34. struct mux_chip *mux_chip;
  35. struct regmap *regmap;
  36. int num_fields;
  37. int ret;
  38. int i;
  39. regmap = syscon_node_to_regmap(np->parent);
  40. if (IS_ERR(regmap)) {
  41. ret = PTR_ERR(regmap);
  42. dev_err(dev, "failed to get regmap: %d\n", ret);
  43. return ret;
  44. }
  45. ret = of_property_count_u32_elems(np, "mux-reg-masks");
  46. if (ret == 0 || ret % 2)
  47. ret = -EINVAL;
  48. if (ret < 0) {
  49. dev_err(dev, "mux-reg-masks property missing or invalid: %d\n",
  50. ret);
  51. return ret;
  52. }
  53. num_fields = ret / 2;
  54. mux_chip = devm_mux_chip_alloc(dev, num_fields, num_fields *
  55. sizeof(*fields));
  56. if (IS_ERR(mux_chip))
  57. return PTR_ERR(mux_chip);
  58. fields = mux_chip_priv(mux_chip);
  59. for (i = 0; i < num_fields; i++) {
  60. struct mux_control *mux = &mux_chip->mux[i];
  61. struct reg_field field;
  62. s32 idle_state = MUX_IDLE_AS_IS;
  63. u32 reg, mask;
  64. int bits;
  65. ret = of_property_read_u32_index(np, "mux-reg-masks",
  66. 2 * i, &reg);
  67. if (!ret)
  68. ret = of_property_read_u32_index(np, "mux-reg-masks",
  69. 2 * i + 1, &mask);
  70. if (ret < 0) {
  71. dev_err(dev, "bitfield %d: failed to read mux-reg-masks property: %d\n",
  72. i, ret);
  73. return ret;
  74. }
  75. field.reg = reg;
  76. field.msb = fls(mask) - 1;
  77. field.lsb = ffs(mask) - 1;
  78. if (mask != GENMASK(field.msb, field.lsb)) {
  79. dev_err(dev, "bitfield %d: invalid mask 0x%x\n",
  80. i, mask);
  81. return -EINVAL;
  82. }
  83. fields[i] = devm_regmap_field_alloc(dev, regmap, field);
  84. if (IS_ERR(fields[i])) {
  85. ret = PTR_ERR(fields[i]);
  86. dev_err(dev, "bitfield %d: failed allocate: %d\n",
  87. i, ret);
  88. return ret;
  89. }
  90. bits = 1 + field.msb - field.lsb;
  91. mux->states = 1 << bits;
  92. of_property_read_u32_index(np, "idle-states", i,
  93. (u32 *)&idle_state);
  94. if (idle_state != MUX_IDLE_AS_IS) {
  95. if (idle_state < 0 || idle_state >= mux->states) {
  96. dev_err(dev, "bitfield: %d: out of range idle state %d\n",
  97. i, idle_state);
  98. return -EINVAL;
  99. }
  100. mux->idle_state = idle_state;
  101. }
  102. }
  103. mux_chip->ops = &mux_mmio_ops;
  104. return devm_mux_chip_register(dev, mux_chip);
  105. }
  106. static struct platform_driver mux_mmio_driver = {
  107. .driver = {
  108. .name = "mmio-mux",
  109. .of_match_table = of_match_ptr(mux_mmio_dt_ids),
  110. },
  111. .probe = mux_mmio_probe,
  112. };
  113. module_platform_driver(mux_mmio_driver);
  114. MODULE_DESCRIPTION("MMIO register bitfield-controlled multiplexer driver");
  115. MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
  116. MODULE_LICENSE("GPL v2");