mmio.c 3.5 KB

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