pbias-regulator.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. struct pbias_of_data {
  45. unsigned int offset;
  46. };
  47. static const unsigned int pbias_volt_table[] = {
  48. 1800000,
  49. 3000000
  50. };
  51. static const struct regulator_ops pbias_regulator_voltage_ops = {
  52. .list_voltage = regulator_list_voltage_table,
  53. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  54. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  55. .enable = regulator_enable_regmap,
  56. .disable = regulator_disable_regmap,
  57. .is_enabled = regulator_is_enabled_regmap,
  58. };
  59. static const struct pbias_reg_info pbias_mmc_omap2430 = {
  60. .enable = BIT(1),
  61. .enable_mask = BIT(1),
  62. .vmode = BIT(0),
  63. .disable_val = 0,
  64. .enable_time = 100,
  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. .name = "pbias_sim_omap3"
  73. };
  74. static const struct pbias_reg_info pbias_mmc_omap4 = {
  75. .enable = BIT(26) | BIT(22),
  76. .enable_mask = BIT(26) | BIT(25) | BIT(22),
  77. .disable_val = BIT(25),
  78. .vmode = BIT(21),
  79. .enable_time = 100,
  80. .name = "pbias_mmc_omap4"
  81. };
  82. static const struct pbias_reg_info pbias_mmc_omap5 = {
  83. .enable = BIT(27) | BIT(26),
  84. .enable_mask = BIT(27) | BIT(25) | BIT(26),
  85. .disable_val = BIT(25),
  86. .vmode = BIT(21),
  87. .enable_time = 100,
  88. .name = "pbias_mmc_omap5"
  89. };
  90. static struct of_regulator_match pbias_matches[] = {
  91. { .name = "pbias_mmc_omap2430", .driver_data = (void *)&pbias_mmc_omap2430},
  92. { .name = "pbias_sim_omap3", .driver_data = (void *)&pbias_sim_omap3},
  93. { .name = "pbias_mmc_omap4", .driver_data = (void *)&pbias_mmc_omap4},
  94. { .name = "pbias_mmc_omap5", .driver_data = (void *)&pbias_mmc_omap5},
  95. };
  96. #define PBIAS_NUM_REGS ARRAY_SIZE(pbias_matches)
  97. /* Offset from SCM general area (and syscon) base */
  98. static const struct pbias_of_data pbias_of_data_omap2 = {
  99. .offset = 0x230,
  100. };
  101. static const struct pbias_of_data pbias_of_data_omap3 = {
  102. .offset = 0x2b0,
  103. };
  104. static const struct pbias_of_data pbias_of_data_omap4 = {
  105. .offset = 0x60,
  106. };
  107. static const struct pbias_of_data pbias_of_data_omap5 = {
  108. .offset = 0x60,
  109. };
  110. static const struct pbias_of_data pbias_of_data_dra7 = {
  111. .offset = 0xe00,
  112. };
  113. static const struct of_device_id pbias_of_match[] = {
  114. { .compatible = "ti,pbias-omap", },
  115. { .compatible = "ti,pbias-omap2", .data = &pbias_of_data_omap2, },
  116. { .compatible = "ti,pbias-omap3", .data = &pbias_of_data_omap3, },
  117. { .compatible = "ti,pbias-omap4", .data = &pbias_of_data_omap4, },
  118. { .compatible = "ti,pbias-omap5", .data = &pbias_of_data_omap5, },
  119. { .compatible = "ti,pbias-dra7", .data = &pbias_of_data_dra7, },
  120. {},
  121. };
  122. MODULE_DEVICE_TABLE(of, pbias_of_match);
  123. static int pbias_regulator_probe(struct platform_device *pdev)
  124. {
  125. struct device_node *np = pdev->dev.of_node;
  126. struct pbias_regulator_data *drvdata;
  127. struct resource *res;
  128. struct regulator_config cfg = { };
  129. struct regmap *syscon;
  130. const struct pbias_reg_info *info;
  131. int ret = 0;
  132. int count, idx, data_idx = 0;
  133. const struct of_device_id *match;
  134. const struct pbias_of_data *data;
  135. unsigned int offset;
  136. count = of_regulator_match(&pdev->dev, np, pbias_matches,
  137. PBIAS_NUM_REGS);
  138. if (count < 0)
  139. return count;
  140. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct pbias_regulator_data)
  141. * count, GFP_KERNEL);
  142. if (!drvdata)
  143. return -ENOMEM;
  144. syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
  145. if (IS_ERR(syscon))
  146. return PTR_ERR(syscon);
  147. match = of_match_device(of_match_ptr(pbias_of_match), &pdev->dev);
  148. if (match && match->data) {
  149. data = match->data;
  150. offset = data->offset;
  151. } else {
  152. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  153. if (!res)
  154. return -EINVAL;
  155. offset = res->start;
  156. dev_WARN(&pdev->dev,
  157. "using legacy dt data for pbias offset\n");
  158. }
  159. cfg.regmap = syscon;
  160. cfg.dev = &pdev->dev;
  161. for (idx = 0; idx < PBIAS_NUM_REGS && data_idx < count; idx++) {
  162. if (!pbias_matches[idx].init_data ||
  163. !pbias_matches[idx].of_node)
  164. continue;
  165. info = pbias_matches[idx].driver_data;
  166. if (!info)
  167. return -ENODEV;
  168. drvdata[data_idx].syscon = syscon;
  169. drvdata[data_idx].info = info;
  170. drvdata[data_idx].desc.name = info->name;
  171. drvdata[data_idx].desc.owner = THIS_MODULE;
  172. drvdata[data_idx].desc.type = REGULATOR_VOLTAGE;
  173. drvdata[data_idx].desc.ops = &pbias_regulator_voltage_ops;
  174. drvdata[data_idx].desc.volt_table = pbias_volt_table;
  175. drvdata[data_idx].desc.n_voltages = 2;
  176. drvdata[data_idx].desc.enable_time = info->enable_time;
  177. drvdata[data_idx].desc.vsel_reg = offset;
  178. drvdata[data_idx].desc.vsel_mask = info->vmode;
  179. drvdata[data_idx].desc.enable_reg = offset;
  180. drvdata[data_idx].desc.enable_mask = info->enable_mask;
  181. drvdata[data_idx].desc.enable_val = info->enable;
  182. drvdata[data_idx].desc.disable_val = info->disable_val;
  183. cfg.init_data = pbias_matches[idx].init_data;
  184. cfg.driver_data = &drvdata[data_idx];
  185. cfg.of_node = pbias_matches[idx].of_node;
  186. drvdata[data_idx].dev = devm_regulator_register(&pdev->dev,
  187. &drvdata[data_idx].desc, &cfg);
  188. if (IS_ERR(drvdata[data_idx].dev)) {
  189. ret = PTR_ERR(drvdata[data_idx].dev);
  190. dev_err(&pdev->dev,
  191. "Failed to register regulator: %d\n", ret);
  192. goto err_regulator;
  193. }
  194. data_idx++;
  195. }
  196. platform_set_drvdata(pdev, drvdata);
  197. err_regulator:
  198. return ret;
  199. }
  200. static struct platform_driver pbias_regulator_driver = {
  201. .probe = pbias_regulator_probe,
  202. .driver = {
  203. .name = "pbias-regulator",
  204. .of_match_table = of_match_ptr(pbias_of_match),
  205. },
  206. };
  207. module_platform_driver(pbias_regulator_driver);
  208. MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
  209. MODULE_DESCRIPTION("pbias voltage regulator");
  210. MODULE_LICENSE("GPL");
  211. MODULE_ALIAS("platform:pbias-regulator");