simple-mfd-i2c.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Simple MFD - I2C
  4. *
  5. * Author(s):
  6. * Michael Walle <[email protected]>
  7. * Lee Jones <[email protected]>
  8. *
  9. * This driver creates a single register map with the intention for it to be
  10. * shared by all sub-devices. Children can use their parent's device structure
  11. * (dev.parent) in order to reference it.
  12. *
  13. * Once the register map has been successfully initialised, any sub-devices
  14. * represented by child nodes in Device Tree or via the MFD cells in this file
  15. * will be subsequently registered.
  16. */
  17. #include <linux/i2c.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mfd/core.h>
  20. #include <linux/module.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/regmap.h>
  23. #include "simple-mfd-i2c.h"
  24. static const struct regmap_config regmap_config_8r_8v = {
  25. .reg_bits = 8,
  26. .val_bits = 8,
  27. };
  28. static int simple_mfd_i2c_probe(struct i2c_client *i2c)
  29. {
  30. const struct simple_mfd_data *simple_mfd_data;
  31. const struct regmap_config *regmap_config;
  32. struct regmap *regmap;
  33. int ret;
  34. simple_mfd_data = device_get_match_data(&i2c->dev);
  35. /* If no regmap_config is specified, use the default 8reg and 8val bits */
  36. if (!simple_mfd_data || !simple_mfd_data->regmap_config)
  37. regmap_config = &regmap_config_8r_8v;
  38. else
  39. regmap_config = simple_mfd_data->regmap_config;
  40. regmap = devm_regmap_init_i2c(i2c, regmap_config);
  41. if (IS_ERR(regmap))
  42. return PTR_ERR(regmap);
  43. /* If no MFD cells are spedified, use register the DT child nodes instead */
  44. if (!simple_mfd_data || !simple_mfd_data->mfd_cell)
  45. return devm_of_platform_populate(&i2c->dev);
  46. ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_AUTO,
  47. simple_mfd_data->mfd_cell,
  48. simple_mfd_data->mfd_cell_size,
  49. NULL, 0, NULL);
  50. if (ret)
  51. dev_err(&i2c->dev, "Failed to add child devices\n");
  52. return ret;
  53. }
  54. static const struct mfd_cell sy7636a_cells[] = {
  55. { .name = "sy7636a-regulator", },
  56. { .name = "sy7636a-temperature", },
  57. };
  58. static const struct simple_mfd_data silergy_sy7636a = {
  59. .mfd_cell = sy7636a_cells,
  60. .mfd_cell_size = ARRAY_SIZE(sy7636a_cells),
  61. };
  62. static const struct of_device_id simple_mfd_i2c_of_match[] = {
  63. { .compatible = "kontron,sl28cpld" },
  64. { .compatible = "silergy,sy7636a", .data = &silergy_sy7636a},
  65. {}
  66. };
  67. MODULE_DEVICE_TABLE(of, simple_mfd_i2c_of_match);
  68. static struct i2c_driver simple_mfd_i2c_driver = {
  69. .probe_new = simple_mfd_i2c_probe,
  70. .driver = {
  71. .name = "simple-mfd-i2c",
  72. .of_match_table = simple_mfd_i2c_of_match,
  73. },
  74. };
  75. module_i2c_driver(simple_mfd_i2c_driver);
  76. MODULE_AUTHOR("Michael Walle <[email protected]>");
  77. MODULE_DESCRIPTION("Simple MFD - I2C driver");
  78. MODULE_LICENSE("GPL v2");