mt6311-regulator.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2015 MediaTek Inc.
  4. // Author: Henry Chen <henryc.chen@mediatek.com>
  5. #include <linux/err.h>
  6. #include <linux/gpio.h>
  7. #include <linux/i2c.h>
  8. #include <linux/init.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/module.h>
  11. #include <linux/regmap.h>
  12. #include <linux/regulator/driver.h>
  13. #include <linux/regulator/machine.h>
  14. #include <linux/regulator/of_regulator.h>
  15. #include <linux/regulator/mt6311.h>
  16. #include <linux/slab.h>
  17. #include "mt6311-regulator.h"
  18. static const struct regmap_config mt6311_regmap_config = {
  19. .reg_bits = 8,
  20. .val_bits = 8,
  21. .max_register = MT6311_FQMTR_CON4,
  22. .cache_type = REGCACHE_RBTREE,
  23. };
  24. /* Default limits measured in millivolts and milliamps */
  25. #define MT6311_MIN_UV 600000
  26. #define MT6311_MAX_UV 1393750
  27. #define MT6311_STEP_UV 6250
  28. static const struct regulator_ops mt6311_buck_ops = {
  29. .list_voltage = regulator_list_voltage_linear,
  30. .map_voltage = regulator_map_voltage_linear,
  31. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  32. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  33. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  34. .enable = regulator_enable_regmap,
  35. .disable = regulator_disable_regmap,
  36. .is_enabled = regulator_is_enabled_regmap,
  37. };
  38. static const struct regulator_ops mt6311_ldo_ops = {
  39. .enable = regulator_enable_regmap,
  40. .disable = regulator_disable_regmap,
  41. .is_enabled = regulator_is_enabled_regmap,
  42. };
  43. #define MT6311_BUCK(_id) \
  44. {\
  45. .name = #_id,\
  46. .ops = &mt6311_buck_ops,\
  47. .of_match = of_match_ptr(#_id),\
  48. .regulators_node = of_match_ptr("regulators"),\
  49. .type = REGULATOR_VOLTAGE,\
  50. .id = MT6311_ID_##_id,\
  51. .n_voltages = (MT6311_MAX_UV - MT6311_MIN_UV) / MT6311_STEP_UV + 1,\
  52. .min_uV = MT6311_MIN_UV,\
  53. .uV_step = MT6311_STEP_UV,\
  54. .owner = THIS_MODULE,\
  55. .enable_reg = MT6311_VDVFS11_CON9,\
  56. .enable_mask = MT6311_PMIC_VDVFS11_EN_MASK,\
  57. .vsel_reg = MT6311_VDVFS11_CON12,\
  58. .vsel_mask = MT6311_PMIC_VDVFS11_VOSEL_MASK,\
  59. }
  60. #define MT6311_LDO(_id) \
  61. {\
  62. .name = #_id,\
  63. .ops = &mt6311_ldo_ops,\
  64. .of_match = of_match_ptr(#_id),\
  65. .regulators_node = of_match_ptr("regulators"),\
  66. .type = REGULATOR_VOLTAGE,\
  67. .id = MT6311_ID_##_id,\
  68. .owner = THIS_MODULE,\
  69. .enable_reg = MT6311_LDO_CON3,\
  70. .enable_mask = MT6311_PMIC_RG_VBIASN_EN_MASK,\
  71. }
  72. static const struct regulator_desc mt6311_regulators[] = {
  73. MT6311_BUCK(VDVFS),
  74. MT6311_LDO(VBIASN),
  75. };
  76. /*
  77. * I2C driver interface functions
  78. */
  79. static int mt6311_i2c_probe(struct i2c_client *i2c,
  80. const struct i2c_device_id *id)
  81. {
  82. struct regulator_config config = { };
  83. struct regulator_dev *rdev;
  84. struct regmap *regmap;
  85. int i, ret;
  86. unsigned int data;
  87. regmap = devm_regmap_init_i2c(i2c, &mt6311_regmap_config);
  88. if (IS_ERR(regmap)) {
  89. ret = PTR_ERR(regmap);
  90. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  91. ret);
  92. return ret;
  93. }
  94. ret = regmap_read(regmap, MT6311_SWCID, &data);
  95. if (ret < 0) {
  96. dev_err(&i2c->dev, "Failed to read DEVICE_ID reg: %d\n", ret);
  97. return ret;
  98. }
  99. switch (data) {
  100. case MT6311_E1_CID_CODE:
  101. case MT6311_E2_CID_CODE:
  102. case MT6311_E3_CID_CODE:
  103. break;
  104. default:
  105. dev_err(&i2c->dev, "Unsupported device id = 0x%x.\n", data);
  106. return -ENODEV;
  107. }
  108. for (i = 0; i < MT6311_MAX_REGULATORS; i++) {
  109. config.dev = &i2c->dev;
  110. config.regmap = regmap;
  111. rdev = devm_regulator_register(&i2c->dev,
  112. &mt6311_regulators[i], &config);
  113. if (IS_ERR(rdev)) {
  114. dev_err(&i2c->dev,
  115. "Failed to register MT6311 regulator\n");
  116. return PTR_ERR(rdev);
  117. }
  118. }
  119. return 0;
  120. }
  121. static const struct i2c_device_id mt6311_i2c_id[] = {
  122. {"mt6311", 0},
  123. {},
  124. };
  125. MODULE_DEVICE_TABLE(i2c, mt6311_i2c_id);
  126. #ifdef CONFIG_OF
  127. static const struct of_device_id mt6311_dt_ids[] = {
  128. { .compatible = "mediatek,mt6311-regulator",
  129. .data = &mt6311_i2c_id[0] },
  130. {},
  131. };
  132. MODULE_DEVICE_TABLE(of, mt6311_dt_ids);
  133. #endif
  134. static struct i2c_driver mt6311_regulator_driver = {
  135. .driver = {
  136. .name = "mt6311",
  137. .of_match_table = of_match_ptr(mt6311_dt_ids),
  138. },
  139. .probe = mt6311_i2c_probe,
  140. .id_table = mt6311_i2c_id,
  141. };
  142. module_i2c_driver(mt6311_regulator_driver);
  143. MODULE_AUTHOR("Henry Chen <henryc.chen@mediatek.com>");
  144. MODULE_DESCRIPTION("Regulator device driver for Mediatek MT6311");
  145. MODULE_LICENSE("GPL v2");