pbias-regulator.c 7.1 KB

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