pbias-regulator.c 5.6 KB

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