mmio.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/property.h>
  15. #include <linux/regmap.h>
  16. struct mux_mmio {
  17. struct regmap_field **fields;
  18. unsigned int *hardware_states;
  19. };
  20. static int mux_mmio_get(struct mux_control *mux, int *state)
  21. {
  22. struct mux_mmio *mux_mmio = mux_chip_priv(mux->chip);
  23. unsigned int index = mux_control_get_index(mux);
  24. return regmap_field_read(mux_mmio->fields[index], state);
  25. }
  26. static int mux_mmio_set(struct mux_control *mux, int state)
  27. {
  28. struct mux_mmio *mux_mmio = mux_chip_priv(mux->chip);
  29. unsigned int index = mux_control_get_index(mux);
  30. return regmap_field_write(mux_mmio->fields[index], state);
  31. }
  32. static const struct mux_control_ops mux_mmio_ops = {
  33. .set = mux_mmio_set,
  34. };
  35. static const struct of_device_id mux_mmio_dt_ids[] = {
  36. { .compatible = "mmio-mux", },
  37. { .compatible = "reg-mux", },
  38. { /* sentinel */ }
  39. };
  40. MODULE_DEVICE_TABLE(of, mux_mmio_dt_ids);
  41. static const struct regmap_config mux_mmio_regmap_cfg = {
  42. .reg_bits = 32,
  43. .val_bits = 32,
  44. .reg_stride = 4,
  45. };
  46. static int mux_mmio_probe(struct platform_device *pdev)
  47. {
  48. struct device *dev = &pdev->dev;
  49. struct device_node *np = dev->of_node;
  50. struct mux_chip *mux_chip;
  51. struct mux_mmio *mux_mmio;
  52. struct regmap *regmap;
  53. void __iomem *base;
  54. int num_fields;
  55. int ret;
  56. int i;
  57. if (of_device_is_compatible(np, "mmio-mux")) {
  58. regmap = syscon_node_to_regmap(np->parent);
  59. } else {
  60. base = devm_platform_ioremap_resource(pdev, 0);
  61. if (IS_ERR(base))
  62. regmap = ERR_PTR(-ENODEV);
  63. else
  64. regmap = devm_regmap_init_mmio(dev, base, &mux_mmio_regmap_cfg);
  65. /* Fallback to checking the parent node on "real" errors. */
  66. if (IS_ERR(regmap) && regmap != ERR_PTR(-EPROBE_DEFER)) {
  67. regmap = dev_get_regmap(dev->parent, NULL);
  68. if (!regmap)
  69. regmap = ERR_PTR(-ENODEV);
  70. }
  71. }
  72. if (IS_ERR(regmap))
  73. return dev_err_probe(dev, PTR_ERR(regmap),
  74. "failed to get regmap\n");
  75. ret = of_property_count_u32_elems(np, "mux-reg-masks");
  76. if (ret == 0 || ret % 2)
  77. ret = -EINVAL;
  78. if (ret < 0) {
  79. dev_err(dev, "mux-reg-masks property missing or invalid: %d\n",
  80. ret);
  81. return ret;
  82. }
  83. num_fields = ret / 2;
  84. mux_chip = devm_mux_chip_alloc(dev, num_fields, sizeof(struct mux_mmio));
  85. if (IS_ERR(mux_chip))
  86. return PTR_ERR(mux_chip);
  87. mux_mmio = mux_chip_priv(mux_chip);
  88. mux_mmio->fields = devm_kmalloc(dev, num_fields * sizeof(*mux_mmio->fields), GFP_KERNEL);
  89. if (!mux_mmio->fields)
  90. return -ENOMEM;
  91. mux_mmio->hardware_states = devm_kmalloc(dev, num_fields *
  92. sizeof(*mux_mmio->hardware_states), GFP_KERNEL);
  93. if (!mux_mmio->hardware_states)
  94. return -ENOMEM;
  95. for (i = 0; i < num_fields; i++) {
  96. struct mux_control *mux = &mux_chip->mux[i];
  97. struct reg_field field;
  98. s32 idle_state = MUX_IDLE_AS_IS;
  99. u32 reg, mask;
  100. int bits;
  101. ret = of_property_read_u32_index(np, "mux-reg-masks",
  102. 2 * i, &reg);
  103. if (!ret)
  104. ret = of_property_read_u32_index(np, "mux-reg-masks",
  105. 2 * i + 1, &mask);
  106. if (ret < 0) {
  107. dev_err(dev, "bitfield %d: failed to read mux-reg-masks property: %d\n",
  108. i, ret);
  109. return ret;
  110. }
  111. field.reg = reg;
  112. field.msb = fls(mask) - 1;
  113. field.lsb = ffs(mask) - 1;
  114. if (mask != GENMASK(field.msb, field.lsb)) {
  115. dev_err(dev, "bitfield %d: invalid mask 0x%x\n",
  116. i, mask);
  117. return -EINVAL;
  118. }
  119. mux_mmio->fields[i] = devm_regmap_field_alloc(dev, regmap, field);
  120. if (IS_ERR(mux_mmio->fields[i])) {
  121. ret = PTR_ERR(mux_mmio->fields[i]);
  122. dev_err(dev, "bitfield %d: failed to allocate: %d\n",
  123. i, ret);
  124. return ret;
  125. }
  126. bits = 1 + field.msb - field.lsb;
  127. mux->states = 1 << bits;
  128. of_property_read_u32_index(np, "idle-states", i,
  129. (u32 *)&idle_state);
  130. if (idle_state != MUX_IDLE_AS_IS) {
  131. if (idle_state < 0 || idle_state >= mux->states) {
  132. dev_err(dev, "bitfield: %d: out of range idle state %d\n",
  133. i, idle_state);
  134. return -EINVAL;
  135. }
  136. mux->idle_state = idle_state;
  137. }
  138. }
  139. mux_chip->ops = &mux_mmio_ops;
  140. dev_set_drvdata(dev, mux_chip);
  141. return devm_mux_chip_register(dev, mux_chip);
  142. }
  143. static int mux_mmio_suspend_noirq(struct device *dev)
  144. {
  145. struct mux_chip *mux_chip = dev_get_drvdata(dev);
  146. struct mux_mmio *mux_mmio = mux_chip_priv(mux_chip);
  147. unsigned int state;
  148. int ret, i;
  149. for (i = 0; i < mux_chip->controllers; i++) {
  150. ret = mux_mmio_get(&mux_chip->mux[i], &state);
  151. if (ret) {
  152. dev_err(dev, "control %u: error saving mux: %d\n", i, ret);
  153. return ret;
  154. }
  155. mux_mmio->hardware_states[i] = state;
  156. }
  157. return 0;
  158. }
  159. static int mux_mmio_resume_noirq(struct device *dev)
  160. {
  161. struct mux_chip *mux_chip = dev_get_drvdata(dev);
  162. struct mux_mmio *mux_mmio = mux_chip_priv(mux_chip);
  163. int ret, i;
  164. for (i = 0; i < mux_chip->controllers; i++) {
  165. ret = mux_mmio_set(&mux_chip->mux[i], mux_mmio->hardware_states[i]);
  166. if (ret) {
  167. dev_err(dev, "control %u: error restoring mux: %d\n", i, ret);
  168. return ret;
  169. }
  170. }
  171. return 0;
  172. }
  173. static DEFINE_NOIRQ_DEV_PM_OPS(mux_mmio_pm_ops, mux_mmio_suspend_noirq, mux_mmio_resume_noirq);
  174. static struct platform_driver mux_mmio_driver = {
  175. .driver = {
  176. .name = "mmio-mux",
  177. .of_match_table = mux_mmio_dt_ids,
  178. .pm = pm_sleep_ptr(&mux_mmio_pm_ops),
  179. },
  180. .probe = mux_mmio_probe,
  181. };
  182. module_platform_driver(mux_mmio_driver);
  183. MODULE_DESCRIPTION("MMIO register bitfield-controlled multiplexer driver");
  184. MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
  185. MODULE_LICENSE("GPL v2");