mt6311-regulator.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. {
  81. struct regulator_config config = { };
  82. struct regulator_dev *rdev;
  83. struct regmap *regmap;
  84. int i, ret;
  85. unsigned int data;
  86. regmap = devm_regmap_init_i2c(i2c, &mt6311_regmap_config);
  87. if (IS_ERR(regmap)) {
  88. ret = PTR_ERR(regmap);
  89. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  90. ret);
  91. return ret;
  92. }
  93. ret = regmap_read(regmap, MT6311_SWCID, &data);
  94. if (ret < 0) {
  95. dev_err(&i2c->dev, "Failed to read DEVICE_ID reg: %d\n", ret);
  96. return ret;
  97. }
  98. switch (data) {
  99. case MT6311_E1_CID_CODE:
  100. case MT6311_E2_CID_CODE:
  101. case MT6311_E3_CID_CODE:
  102. break;
  103. default:
  104. dev_err(&i2c->dev, "Unsupported device id = 0x%x.\n", data);
  105. return -ENODEV;
  106. }
  107. for (i = 0; i < MT6311_MAX_REGULATORS; i++) {
  108. config.dev = &i2c->dev;
  109. config.regmap = regmap;
  110. rdev = devm_regulator_register(&i2c->dev,
  111. &mt6311_regulators[i], &config);
  112. if (IS_ERR(rdev)) {
  113. dev_err(&i2c->dev,
  114. "Failed to register MT6311 regulator\n");
  115. return PTR_ERR(rdev);
  116. }
  117. }
  118. return 0;
  119. }
  120. static const struct i2c_device_id mt6311_i2c_id[] = {
  121. {"mt6311", 0},
  122. {},
  123. };
  124. MODULE_DEVICE_TABLE(i2c, mt6311_i2c_id);
  125. #ifdef CONFIG_OF
  126. static const struct of_device_id mt6311_dt_ids[] = {
  127. { .compatible = "mediatek,mt6311-regulator",
  128. .data = &mt6311_i2c_id[0] },
  129. {},
  130. };
  131. MODULE_DEVICE_TABLE(of, mt6311_dt_ids);
  132. #endif
  133. static struct i2c_driver mt6311_regulator_driver = {
  134. .driver = {
  135. .name = "mt6311",
  136. .of_match_table = of_match_ptr(mt6311_dt_ids),
  137. },
  138. .probe_new = mt6311_i2c_probe,
  139. .id_table = mt6311_i2c_id,
  140. };
  141. module_i2c_driver(mt6311_regulator_driver);
  142. MODULE_AUTHOR("Henry Chen <henryc.chen@mediatek.com>");
  143. MODULE_DESCRIPTION("Regulator device driver for Mediatek MT6311");
  144. MODULE_LICENSE("GPL v2");