mt6311-regulator.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. .cache_type = REGCACHE_RBTREE,
  32. };
  33. /* Default limits measured in millivolts and milliamps */
  34. #define MT6311_MIN_UV 600000
  35. #define MT6311_MAX_UV 1393750
  36. #define MT6311_STEP_UV 6250
  37. static const struct regulator_linear_range buck_volt_range[] = {
  38. REGULATOR_LINEAR_RANGE(MT6311_MIN_UV, 0, 0x7f, MT6311_STEP_UV),
  39. };
  40. static const struct regulator_ops mt6311_buck_ops = {
  41. .list_voltage = regulator_list_voltage_linear_range,
  42. .map_voltage = regulator_map_voltage_linear_range,
  43. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  44. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  45. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  46. .enable = regulator_enable_regmap,
  47. .disable = regulator_disable_regmap,
  48. .is_enabled = regulator_is_enabled_regmap,
  49. };
  50. static const struct regulator_ops mt6311_ldo_ops = {
  51. .enable = regulator_enable_regmap,
  52. .disable = regulator_disable_regmap,
  53. .is_enabled = regulator_is_enabled_regmap,
  54. };
  55. #define MT6311_BUCK(_id) \
  56. {\
  57. .name = #_id,\
  58. .ops = &mt6311_buck_ops,\
  59. .of_match = of_match_ptr(#_id),\
  60. .regulators_node = of_match_ptr("regulators"),\
  61. .type = REGULATOR_VOLTAGE,\
  62. .id = MT6311_ID_##_id,\
  63. .n_voltages = (MT6311_MAX_UV - MT6311_MIN_UV) / MT6311_STEP_UV + 1,\
  64. .min_uV = MT6311_MIN_UV,\
  65. .uV_step = MT6311_STEP_UV,\
  66. .owner = THIS_MODULE,\
  67. .linear_ranges = buck_volt_range, \
  68. .n_linear_ranges = ARRAY_SIZE(buck_volt_range), \
  69. .enable_reg = MT6311_VDVFS11_CON9,\
  70. .enable_mask = MT6311_PMIC_VDVFS11_EN_MASK,\
  71. .vsel_reg = MT6311_VDVFS11_CON12,\
  72. .vsel_mask = MT6311_PMIC_VDVFS11_VOSEL_MASK,\
  73. }
  74. #define MT6311_LDO(_id) \
  75. {\
  76. .name = #_id,\
  77. .ops = &mt6311_ldo_ops,\
  78. .of_match = of_match_ptr(#_id),\
  79. .regulators_node = of_match_ptr("regulators"),\
  80. .type = REGULATOR_VOLTAGE,\
  81. .id = MT6311_ID_##_id,\
  82. .owner = THIS_MODULE,\
  83. .enable_reg = MT6311_LDO_CON3,\
  84. .enable_mask = MT6311_PMIC_RG_VBIASN_EN_MASK,\
  85. }
  86. static const struct regulator_desc mt6311_regulators[] = {
  87. MT6311_BUCK(VDVFS),
  88. MT6311_LDO(VBIASN),
  89. };
  90. /*
  91. * I2C driver interface functions
  92. */
  93. static int mt6311_i2c_probe(struct i2c_client *i2c,
  94. const struct i2c_device_id *id)
  95. {
  96. struct regulator_config config = { };
  97. struct regulator_dev *rdev;
  98. struct regmap *regmap;
  99. int i, ret;
  100. unsigned int data;
  101. regmap = devm_regmap_init_i2c(i2c, &mt6311_regmap_config);
  102. if (IS_ERR(regmap)) {
  103. ret = PTR_ERR(regmap);
  104. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  105. ret);
  106. return ret;
  107. }
  108. ret = regmap_read(regmap, MT6311_SWCID, &data);
  109. if (ret < 0) {
  110. dev_err(&i2c->dev, "Failed to read DEVICE_ID reg: %d\n", ret);
  111. return ret;
  112. }
  113. switch (data) {
  114. case MT6311_E1_CID_CODE:
  115. case MT6311_E2_CID_CODE:
  116. case MT6311_E3_CID_CODE:
  117. break;
  118. default:
  119. dev_err(&i2c->dev, "Unsupported device id = 0x%x.\n", data);
  120. return -ENODEV;
  121. }
  122. for (i = 0; i < MT6311_MAX_REGULATORS; i++) {
  123. config.dev = &i2c->dev;
  124. config.regmap = regmap;
  125. rdev = devm_regulator_register(&i2c->dev,
  126. &mt6311_regulators[i], &config);
  127. if (IS_ERR(rdev)) {
  128. dev_err(&i2c->dev,
  129. "Failed to register MT6311 regulator\n");
  130. return PTR_ERR(rdev);
  131. }
  132. }
  133. return 0;
  134. }
  135. static const struct i2c_device_id mt6311_i2c_id[] = {
  136. {"mt6311", 0},
  137. {},
  138. };
  139. MODULE_DEVICE_TABLE(i2c, mt6311_i2c_id);
  140. #ifdef CONFIG_OF
  141. static const struct of_device_id mt6311_dt_ids[] = {
  142. { .compatible = "mediatek,mt6311-regulator",
  143. .data = &mt6311_i2c_id[0] },
  144. {},
  145. };
  146. MODULE_DEVICE_TABLE(of, mt6311_dt_ids);
  147. #endif
  148. static struct i2c_driver mt6311_regulator_driver = {
  149. .driver = {
  150. .name = "mt6311",
  151. .of_match_table = of_match_ptr(mt6311_dt_ids),
  152. },
  153. .probe = mt6311_i2c_probe,
  154. .id_table = mt6311_i2c_id,
  155. };
  156. module_i2c_driver(mt6311_regulator_driver);
  157. MODULE_AUTHOR("Henry Chen <henryc.chen@mediatek.com>");
  158. MODULE_DESCRIPTION("Regulator device driver for Mediatek MT6311");
  159. MODULE_LICENSE("GPL v2");