pbias-regulator.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * pbias-regulator.c
  3. *
  4. * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com/
  5. * Author: Balaji T K <balajitk@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation version 2.
  10. *
  11. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regulator/driver.h>
  22. #include <linux/regulator/machine.h>
  23. #include <linux/regulator/of_regulator.h>
  24. #include <linux/regmap.h>
  25. #include <linux/slab.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. struct pbias_reg_info {
  29. u32 enable;
  30. u32 enable_mask;
  31. u32 disable_val;
  32. u32 vmode;
  33. unsigned int enable_time;
  34. char *name;
  35. const unsigned int *pbias_volt_table;
  36. int n_voltages;
  37. };
  38. struct pbias_of_data {
  39. unsigned int offset;
  40. };
  41. static const unsigned int pbias_volt_table_3_0V[] = {
  42. 1800000,
  43. 3000000
  44. };
  45. static const unsigned int pbias_volt_table_3_3V[] = {
  46. 1800000,
  47. 3300000
  48. };
  49. static const struct regulator_ops pbias_regulator_voltage_ops = {
  50. .list_voltage = regulator_list_voltage_table,
  51. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  52. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  53. .enable = regulator_enable_regmap,
  54. .disable = regulator_disable_regmap,
  55. .is_enabled = regulator_is_enabled_regmap,
  56. };
  57. static const struct pbias_reg_info pbias_mmc_omap2430 = {
  58. .enable = BIT(1),
  59. .enable_mask = BIT(1),
  60. .vmode = BIT(0),
  61. .disable_val = 0,
  62. .enable_time = 100,
  63. .pbias_volt_table = pbias_volt_table_3_0V,
  64. .n_voltages = 2,
  65. .name = "pbias_mmc_omap2430"
  66. };
  67. static const struct pbias_reg_info pbias_sim_omap3 = {
  68. .enable = BIT(9),
  69. .enable_mask = BIT(9),
  70. .vmode = BIT(8),
  71. .enable_time = 100,
  72. .pbias_volt_table = pbias_volt_table_3_0V,
  73. .n_voltages = 2,
  74. .name = "pbias_sim_omap3"
  75. };
  76. static const struct pbias_reg_info pbias_mmc_omap4 = {
  77. .enable = BIT(26) | BIT(22),
  78. .enable_mask = BIT(26) | BIT(25) | BIT(22),
  79. .disable_val = BIT(25),
  80. .vmode = BIT(21),
  81. .enable_time = 100,
  82. .pbias_volt_table = pbias_volt_table_3_0V,
  83. .n_voltages = 2,
  84. .name = "pbias_mmc_omap4"
  85. };
  86. static const struct pbias_reg_info pbias_mmc_omap5 = {
  87. .enable = BIT(27) | BIT(26),
  88. .enable_mask = BIT(27) | BIT(25) | BIT(26),
  89. .disable_val = BIT(25),
  90. .vmode = BIT(21),
  91. .enable_time = 100,
  92. .pbias_volt_table = pbias_volt_table_3_3V,
  93. .n_voltages = 2,
  94. .name = "pbias_mmc_omap5"
  95. };
  96. static struct of_regulator_match pbias_matches[] = {
  97. { .name = "pbias_mmc_omap2430", .driver_data = (void *)&pbias_mmc_omap2430},
  98. { .name = "pbias_sim_omap3", .driver_data = (void *)&pbias_sim_omap3},
  99. { .name = "pbias_mmc_omap4", .driver_data = (void *)&pbias_mmc_omap4},
  100. { .name = "pbias_mmc_omap5", .driver_data = (void *)&pbias_mmc_omap5},
  101. };
  102. #define PBIAS_NUM_REGS ARRAY_SIZE(pbias_matches)
  103. /* Offset from SCM general area (and syscon) base */
  104. static const struct pbias_of_data pbias_of_data_omap2 = {
  105. .offset = 0x230,
  106. };
  107. static const struct pbias_of_data pbias_of_data_omap3 = {
  108. .offset = 0x2b0,
  109. };
  110. static const struct pbias_of_data pbias_of_data_omap4 = {
  111. .offset = 0x60,
  112. };
  113. static const struct pbias_of_data pbias_of_data_omap5 = {
  114. .offset = 0x60,
  115. };
  116. static const struct pbias_of_data pbias_of_data_dra7 = {
  117. .offset = 0xe00,
  118. };
  119. static const struct of_device_id pbias_of_match[] = {
  120. { .compatible = "ti,pbias-omap", },
  121. { .compatible = "ti,pbias-omap2", .data = &pbias_of_data_omap2, },
  122. { .compatible = "ti,pbias-omap3", .data = &pbias_of_data_omap3, },
  123. { .compatible = "ti,pbias-omap4", .data = &pbias_of_data_omap4, },
  124. { .compatible = "ti,pbias-omap5", .data = &pbias_of_data_omap5, },
  125. { .compatible = "ti,pbias-dra7", .data = &pbias_of_data_dra7, },
  126. {},
  127. };
  128. MODULE_DEVICE_TABLE(of, pbias_of_match);
  129. static int pbias_regulator_probe(struct platform_device *pdev)
  130. {
  131. struct device_node *np = pdev->dev.of_node;
  132. struct resource *res;
  133. struct regulator_config cfg = { };
  134. struct regulator_desc *desc;
  135. struct regulator_dev *rdev;
  136. struct regmap *syscon;
  137. const struct pbias_reg_info *info;
  138. int ret, count, idx;
  139. const struct pbias_of_data *data;
  140. unsigned int offset;
  141. count = of_regulator_match(&pdev->dev, np, pbias_matches,
  142. PBIAS_NUM_REGS);
  143. if (count < 0)
  144. return count;
  145. desc = devm_kcalloc(&pdev->dev, count, sizeof(*desc), GFP_KERNEL);
  146. if (!desc)
  147. return -ENOMEM;
  148. syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
  149. if (IS_ERR(syscon))
  150. return PTR_ERR(syscon);
  151. data = of_device_get_match_data(&pdev->dev);
  152. if (data) {
  153. offset = data->offset;
  154. } else {
  155. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  156. if (!res)
  157. return -EINVAL;
  158. offset = res->start;
  159. dev_WARN(&pdev->dev,
  160. "using legacy dt data for pbias offset\n");
  161. }
  162. cfg.regmap = syscon;
  163. cfg.dev = &pdev->dev;
  164. for (idx = 0; idx < PBIAS_NUM_REGS && count; idx++) {
  165. if (!pbias_matches[idx].init_data ||
  166. !pbias_matches[idx].of_node)
  167. continue;
  168. info = pbias_matches[idx].driver_data;
  169. if (!info)
  170. return -ENODEV;
  171. desc->name = info->name;
  172. desc->owner = THIS_MODULE;
  173. desc->type = REGULATOR_VOLTAGE;
  174. desc->ops = &pbias_regulator_voltage_ops;
  175. desc->volt_table = info->pbias_volt_table;
  176. desc->n_voltages = info->n_voltages;
  177. desc->enable_time = info->enable_time;
  178. desc->vsel_reg = offset;
  179. desc->vsel_mask = info->vmode;
  180. desc->enable_reg = offset;
  181. desc->enable_mask = info->enable_mask;
  182. desc->enable_val = info->enable;
  183. desc->disable_val = info->disable_val;
  184. cfg.init_data = pbias_matches[idx].init_data;
  185. cfg.of_node = pbias_matches[idx].of_node;
  186. rdev = devm_regulator_register(&pdev->dev, desc, &cfg);
  187. if (IS_ERR(rdev)) {
  188. ret = PTR_ERR(rdev);
  189. dev_err(&pdev->dev,
  190. "Failed to register regulator: %d\n", ret);
  191. return ret;
  192. }
  193. desc++;
  194. count--;
  195. }
  196. return 0;
  197. }
  198. static struct platform_driver pbias_regulator_driver = {
  199. .probe = pbias_regulator_probe,
  200. .driver = {
  201. .name = "pbias-regulator",
  202. .of_match_table = of_match_ptr(pbias_of_match),
  203. },
  204. };
  205. module_platform_driver(pbias_regulator_driver);
  206. MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
  207. MODULE_DESCRIPTION("pbias voltage regulator");
  208. MODULE_LICENSE("GPL");
  209. MODULE_ALIAS("platform:pbias-regulator");