max8893.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/regmap.h>
  6. #include <linux/regulator/driver.h>
  7. static const struct regulator_ops max8893_ops = {
  8. .is_enabled = regulator_is_enabled_regmap,
  9. .enable = regulator_enable_regmap,
  10. .disable = regulator_disable_regmap,
  11. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  12. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  13. .list_voltage = regulator_list_voltage_linear,
  14. .map_voltage = regulator_map_voltage_linear,
  15. };
  16. static const struct regulator_desc max8893_regulators[] = {
  17. {
  18. .name = "BUCK",
  19. .supply_name = "in-buck",
  20. .of_match = of_match_ptr("buck"),
  21. .regulators_node = of_match_ptr("regulators"),
  22. .n_voltages = 0x11,
  23. .id = 6,
  24. .ops = &max8893_ops,
  25. .type = REGULATOR_VOLTAGE,
  26. .owner = THIS_MODULE,
  27. .min_uV = 800000,
  28. .uV_step = 100000,
  29. .vsel_reg = 0x4,
  30. .vsel_mask = 0x1f,
  31. .enable_reg = 0x0,
  32. .enable_mask = BIT(7),
  33. },
  34. {
  35. .name = "LDO1",
  36. .supply_name = "in-ldo1",
  37. .of_match = of_match_ptr("ldo1"),
  38. .regulators_node = of_match_ptr("regulators"),
  39. .n_voltages = 0x12,
  40. .id = 1,
  41. .ops = &max8893_ops,
  42. .type = REGULATOR_VOLTAGE,
  43. .owner = THIS_MODULE,
  44. .min_uV = 1600000,
  45. .uV_step = 100000,
  46. .vsel_reg = 0x5,
  47. .vsel_mask = 0x1f,
  48. .enable_reg = 0x0,
  49. .enable_mask = BIT(5),
  50. },
  51. {
  52. .name = "LDO2",
  53. .supply_name = "in-ldo2",
  54. .of_match = of_match_ptr("ldo2"),
  55. .regulators_node = of_match_ptr("regulators"),
  56. .n_voltages = 0x16,
  57. .id = 2,
  58. .ops = &max8893_ops,
  59. .type = REGULATOR_VOLTAGE,
  60. .owner = THIS_MODULE,
  61. .min_uV = 1200000,
  62. .uV_step = 100000,
  63. .vsel_reg = 0x6,
  64. .vsel_mask = 0x1f,
  65. .enable_reg = 0x0,
  66. .enable_mask = BIT(4),
  67. },
  68. {
  69. .name = "LDO3",
  70. .supply_name = "in-ldo3",
  71. .of_match = of_match_ptr("ldo3"),
  72. .regulators_node = of_match_ptr("regulators"),
  73. .n_voltages = 0x12,
  74. .id = 3,
  75. .ops = &max8893_ops,
  76. .type = REGULATOR_VOLTAGE,
  77. .owner = THIS_MODULE,
  78. .min_uV = 1600000,
  79. .uV_step = 100000,
  80. .vsel_reg = 0x7,
  81. .vsel_mask = 0x1f,
  82. .enable_reg = 0x0,
  83. .enable_mask = BIT(3),
  84. },
  85. {
  86. .name = "LDO4",
  87. .supply_name = "in-ldo4",
  88. .of_match = of_match_ptr("ldo4"),
  89. .regulators_node = of_match_ptr("regulators"),
  90. .n_voltages = 0x1a,
  91. .id = 4,
  92. .ops = &max8893_ops,
  93. .type = REGULATOR_VOLTAGE,
  94. .owner = THIS_MODULE,
  95. .min_uV = 800000,
  96. .uV_step = 100000,
  97. .vsel_reg = 0x8,
  98. .vsel_mask = 0x1f,
  99. .enable_reg = 0x0,
  100. .enable_mask = BIT(2),
  101. },
  102. {
  103. .name = "LDO5",
  104. .supply_name = "in-ldo5",
  105. .of_match = of_match_ptr("ldo5"),
  106. .regulators_node = of_match_ptr("regulators"),
  107. .n_voltages = 0x1a,
  108. .id = 5,
  109. .ops = &max8893_ops,
  110. .type = REGULATOR_VOLTAGE,
  111. .owner = THIS_MODULE,
  112. .min_uV = 800000,
  113. .uV_step = 100000,
  114. .vsel_reg = 0x9,
  115. .vsel_mask = 0x1f,
  116. .enable_reg = 0x0,
  117. .enable_mask = BIT(1),
  118. }
  119. };
  120. static const struct regmap_config max8893_regmap = {
  121. .reg_bits = 8,
  122. .val_bits = 8,
  123. };
  124. static int max8893_probe_new(struct i2c_client *i2c)
  125. {
  126. int id, ret;
  127. struct regulator_config config = {.dev = &i2c->dev};
  128. struct regmap *regmap = devm_regmap_init_i2c(i2c, &max8893_regmap);
  129. if (IS_ERR(regmap)) {
  130. ret = PTR_ERR(regmap);
  131. dev_err(&i2c->dev, "regmap init failed: %d\n", ret);
  132. return ret;
  133. }
  134. for (id = 0; id < ARRAY_SIZE(max8893_regulators); id++) {
  135. struct regulator_dev *rdev;
  136. rdev = devm_regulator_register(&i2c->dev,
  137. &max8893_regulators[id],
  138. &config);
  139. if (IS_ERR(rdev)) {
  140. ret = PTR_ERR(rdev);
  141. dev_err(&i2c->dev, "failed to register %s: %d\n",
  142. max8893_regulators[id].name, ret);
  143. return ret;
  144. }
  145. }
  146. return 0;
  147. }
  148. #ifdef CONFIG_OF
  149. static const struct of_device_id max8893_dt_match[] = {
  150. { .compatible = "maxim,max8893" },
  151. { /* sentinel */ },
  152. };
  153. MODULE_DEVICE_TABLE(of, max8893_dt_match);
  154. #endif
  155. static const struct i2c_device_id max8893_ids[] = {
  156. { "max8893", 0 },
  157. { },
  158. };
  159. MODULE_DEVICE_TABLE(i2c, max8893_ids);
  160. static struct i2c_driver max8893_driver = {
  161. .probe_new = max8893_probe_new,
  162. .driver = {
  163. .name = "max8893",
  164. .of_match_table = of_match_ptr(max8893_dt_match),
  165. },
  166. .id_table = max8893_ids,
  167. };
  168. module_i2c_driver(max8893_driver);
  169. MODULE_DESCRIPTION("Maxim MAX8893 PMIC driver");
  170. MODULE_AUTHOR("Sergey Larin <[email protected]>");
  171. MODULE_LICENSE("GPL");