88pg86x.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/module.h>
  3. #include <linux/i2c.h>
  4. #include <linux/of.h>
  5. #include <linux/regulator/driver.h>
  6. #include <linux/regmap.h>
  7. static const struct regulator_ops pg86x_ops = {
  8. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  9. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  10. .list_voltage = regulator_list_voltage_linear_range,
  11. };
  12. static const struct regulator_linear_range pg86x_buck1_ranges[] = {
  13. REGULATOR_LINEAR_RANGE( 0, 0, 10, 0),
  14. REGULATOR_LINEAR_RANGE(1000000, 11, 34, 25000),
  15. REGULATOR_LINEAR_RANGE(1600000, 35, 47, 50000),
  16. };
  17. static const struct regulator_linear_range pg86x_buck2_ranges[] = {
  18. REGULATOR_LINEAR_RANGE( 0, 0, 15, 0),
  19. REGULATOR_LINEAR_RANGE(1000000, 16, 39, 25000),
  20. REGULATOR_LINEAR_RANGE(1600000, 40, 52, 50000),
  21. };
  22. static const struct regulator_desc pg86x_regulators[] = {
  23. {
  24. .id = 0,
  25. .type = REGULATOR_VOLTAGE,
  26. .name = "buck1",
  27. .of_match = of_match_ptr("buck1"),
  28. .n_voltages = 11 + 24 + 13,
  29. .linear_ranges = pg86x_buck1_ranges,
  30. .n_linear_ranges = 3,
  31. .vsel_reg = 0x24,
  32. .vsel_mask = 0xff,
  33. .ops = &pg86x_ops,
  34. .owner = THIS_MODULE
  35. },
  36. {
  37. .id = 1,
  38. .type = REGULATOR_VOLTAGE,
  39. .name = "buck2",
  40. .of_match = of_match_ptr("buck2"),
  41. .n_voltages = 16 + 24 + 13,
  42. .linear_ranges = pg86x_buck2_ranges,
  43. .n_linear_ranges = 3,
  44. .vsel_reg = 0x13,
  45. .vsel_mask = 0xff,
  46. .ops = &pg86x_ops,
  47. .owner = THIS_MODULE
  48. },
  49. };
  50. static const struct regmap_config pg86x_regmap = {
  51. .reg_bits = 8,
  52. .val_bits = 8,
  53. };
  54. static int pg86x_i2c_probe(struct i2c_client *i2c)
  55. {
  56. int id, ret;
  57. struct regulator_config config = {.dev = &i2c->dev};
  58. struct regmap *regmap = devm_regmap_init_i2c(i2c, &pg86x_regmap);
  59. if (IS_ERR(regmap)) {
  60. ret = PTR_ERR(regmap);
  61. dev_err(&i2c->dev, "regmap init failed: %d\n", ret);
  62. return ret;
  63. }
  64. for (id = 0; id < ARRAY_SIZE(pg86x_regulators); id++) {
  65. struct regulator_dev *rdev;
  66. rdev = devm_regulator_register(&i2c->dev,
  67. &pg86x_regulators[id],
  68. &config);
  69. if (IS_ERR(rdev)) {
  70. ret = PTR_ERR(rdev);
  71. dev_err(&i2c->dev, "failed to register %s: %d\n",
  72. pg86x_regulators[id].name, ret);
  73. return ret;
  74. }
  75. }
  76. return 0;
  77. }
  78. static const struct of_device_id pg86x_dt_ids [] = {
  79. { .compatible = "marvell,88pg867" },
  80. { .compatible = "marvell,88pg868" },
  81. { }
  82. };
  83. MODULE_DEVICE_TABLE(of, pg86x_dt_ids);
  84. static const struct i2c_device_id pg86x_i2c_id[] = {
  85. { "88pg867", },
  86. { "88pg868", },
  87. { }
  88. };
  89. MODULE_DEVICE_TABLE(i2c, pg86x_i2c_id);
  90. static struct i2c_driver pg86x_regulator_driver = {
  91. .driver = {
  92. .name = "88pg86x",
  93. .of_match_table = of_match_ptr(pg86x_dt_ids),
  94. },
  95. .probe_new = pg86x_i2c_probe,
  96. .id_table = pg86x_i2c_id,
  97. };
  98. module_i2c_driver(pg86x_regulator_driver);
  99. MODULE_DESCRIPTION("Marvell 88PG86X voltage regulator");
  100. MODULE_AUTHOR("Alexander Monakov <amonakov@gmail.com>");
  101. MODULE_LICENSE("GPL");