mt6311-regulator.c 4.7 KB

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