pbias-regulator.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * pbias-regulator.c
  3. *
  4. * Copyright (C) 2014 Texas Instruments Incorporated - http://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 vmode;
  32. unsigned int enable_time;
  33. char *name;
  34. };
  35. struct pbias_regulator_data {
  36. struct regulator_desc desc;
  37. void __iomem *pbias_addr;
  38. struct regulator_dev *dev;
  39. struct regmap *syscon;
  40. const struct pbias_reg_info *info;
  41. int voltage;
  42. };
  43. static const unsigned int pbias_volt_table[] = {
  44. 1800000,
  45. 3000000
  46. };
  47. static struct regulator_ops pbias_regulator_voltage_ops = {
  48. .list_voltage = regulator_list_voltage_table,
  49. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  50. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  51. .enable = regulator_enable_regmap,
  52. .disable = regulator_disable_regmap,
  53. .is_enabled = regulator_is_enabled_regmap,
  54. };
  55. static const struct pbias_reg_info pbias_mmc_omap2430 = {
  56. .enable = BIT(1),
  57. .enable_mask = BIT(1),
  58. .vmode = BIT(0),
  59. .enable_time = 100,
  60. .name = "pbias_mmc_omap2430"
  61. };
  62. static const struct pbias_reg_info pbias_sim_omap3 = {
  63. .enable = BIT(9),
  64. .enable_mask = BIT(9),
  65. .vmode = BIT(8),
  66. .enable_time = 100,
  67. .name = "pbias_sim_omap3"
  68. };
  69. static const struct pbias_reg_info pbias_mmc_omap4 = {
  70. .enable = BIT(26) | BIT(22),
  71. .enable_mask = BIT(26) | BIT(25) | BIT(22),
  72. .vmode = BIT(21),
  73. .enable_time = 100,
  74. .name = "pbias_mmc_omap4"
  75. };
  76. static const struct pbias_reg_info pbias_mmc_omap5 = {
  77. .enable = BIT(27) | BIT(26),
  78. .enable_mask = BIT(27) | BIT(25) | BIT(26),
  79. .vmode = BIT(21),
  80. .enable_time = 100,
  81. .name = "pbias_mmc_omap5"
  82. };
  83. static struct of_regulator_match pbias_matches[] = {
  84. { .name = "pbias_mmc_omap2430", .driver_data = (void *)&pbias_mmc_omap2430},
  85. { .name = "pbias_sim_omap3", .driver_data = (void *)&pbias_sim_omap3},
  86. { .name = "pbias_mmc_omap4", .driver_data = (void *)&pbias_mmc_omap4},
  87. { .name = "pbias_mmc_omap5", .driver_data = (void *)&pbias_mmc_omap5},
  88. };
  89. #define PBIAS_NUM_REGS ARRAY_SIZE(pbias_matches)
  90. static const struct of_device_id pbias_of_match[] = {
  91. { .compatible = "ti,pbias-omap", },
  92. {},
  93. };
  94. MODULE_DEVICE_TABLE(of, pbias_of_match);
  95. static int pbias_regulator_probe(struct platform_device *pdev)
  96. {
  97. struct device_node *np = pdev->dev.of_node;
  98. struct pbias_regulator_data *drvdata;
  99. struct resource *res;
  100. struct regulator_config cfg = { };
  101. struct regmap *syscon;
  102. const struct pbias_reg_info *info;
  103. int ret = 0;
  104. int count, idx, data_idx = 0;
  105. count = of_regulator_match(&pdev->dev, np, pbias_matches,
  106. PBIAS_NUM_REGS);
  107. if (count < 0)
  108. return count;
  109. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct pbias_regulator_data)
  110. * count, GFP_KERNEL);
  111. if (!drvdata)
  112. return -ENOMEM;
  113. syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
  114. if (IS_ERR(syscon))
  115. return PTR_ERR(syscon);
  116. cfg.regmap = syscon;
  117. cfg.dev = &pdev->dev;
  118. for (idx = 0; idx < PBIAS_NUM_REGS && data_idx < count; idx++) {
  119. if (!pbias_matches[idx].init_data ||
  120. !pbias_matches[idx].of_node)
  121. continue;
  122. info = pbias_matches[idx].driver_data;
  123. if (!info)
  124. return -ENODEV;
  125. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  126. if (!res)
  127. return -EINVAL;
  128. drvdata[data_idx].syscon = syscon;
  129. drvdata[data_idx].info = info;
  130. drvdata[data_idx].desc.name = info->name;
  131. drvdata[data_idx].desc.owner = THIS_MODULE;
  132. drvdata[data_idx].desc.type = REGULATOR_VOLTAGE;
  133. drvdata[data_idx].desc.ops = &pbias_regulator_voltage_ops;
  134. drvdata[data_idx].desc.volt_table = pbias_volt_table;
  135. drvdata[data_idx].desc.n_voltages = 2;
  136. drvdata[data_idx].desc.enable_time = info->enable_time;
  137. drvdata[data_idx].desc.vsel_reg = res->start;
  138. drvdata[data_idx].desc.vsel_mask = info->vmode;
  139. drvdata[data_idx].desc.enable_reg = res->start;
  140. drvdata[data_idx].desc.enable_mask = info->enable_mask;
  141. drvdata[data_idx].desc.enable_val = info->enable;
  142. cfg.init_data = pbias_matches[idx].init_data;
  143. cfg.driver_data = &drvdata[data_idx];
  144. cfg.of_node = pbias_matches[idx].of_node;
  145. drvdata[data_idx].dev = devm_regulator_register(&pdev->dev,
  146. &drvdata[data_idx].desc, &cfg);
  147. if (IS_ERR(drvdata[data_idx].dev)) {
  148. ret = PTR_ERR(drvdata[data_idx].dev);
  149. dev_err(&pdev->dev,
  150. "Failed to register regulator: %d\n", ret);
  151. goto err_regulator;
  152. }
  153. data_idx++;
  154. }
  155. platform_set_drvdata(pdev, drvdata);
  156. err_regulator:
  157. return ret;
  158. }
  159. static struct platform_driver pbias_regulator_driver = {
  160. .probe = pbias_regulator_probe,
  161. .driver = {
  162. .name = "pbias-regulator",
  163. .owner = THIS_MODULE,
  164. .of_match_table = of_match_ptr(pbias_of_match),
  165. },
  166. };
  167. module_platform_driver(pbias_regulator_driver);
  168. MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
  169. MODULE_DESCRIPTION("pbias voltage regulator");
  170. MODULE_LICENSE("GPL");
  171. MODULE_ALIAS("platform:pbias-regulator");