sky81452-regulator.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * sky81452-regulator.c SKY81452 regulator driver
  3. *
  4. * Copyright 2014 Skyworks Solutions Inc.
  5. * Author : Gyungoh Yoo <jack.yoo@skyworksinc.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/init.h>
  24. #include <linux/err.h>
  25. #include <linux/of.h>
  26. #include <linux/regulator/driver.h>
  27. #include <linux/regulator/of_regulator.h>
  28. /* registers */
  29. #define SKY81452_REG1 0x01
  30. #define SKY81452_REG3 0x03
  31. /* bit mask */
  32. #define SKY81452_LEN 0x40
  33. #define SKY81452_LOUT 0x1F
  34. static struct regulator_ops sky81452_reg_ops = {
  35. .list_voltage = regulator_list_voltage_linear_range,
  36. .map_voltage = regulator_map_voltage_linear_range,
  37. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  38. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  39. .enable = regulator_enable_regmap,
  40. .disable = regulator_disable_regmap,
  41. .is_enabled = regulator_is_enabled_regmap,
  42. };
  43. static const struct regulator_linear_range sky81452_reg_ranges[] = {
  44. REGULATOR_LINEAR_RANGE(4500000, 0, 14, 250000),
  45. REGULATOR_LINEAR_RANGE(9000000, 15, 31, 1000000),
  46. };
  47. static const struct regulator_desc sky81452_reg = {
  48. .name = "LOUT",
  49. .ops = &sky81452_reg_ops,
  50. .type = REGULATOR_VOLTAGE,
  51. .owner = THIS_MODULE,
  52. .n_voltages = SKY81452_LOUT + 1,
  53. .linear_ranges = sky81452_reg_ranges,
  54. .n_linear_ranges = ARRAY_SIZE(sky81452_reg_ranges),
  55. .vsel_reg = SKY81452_REG3,
  56. .vsel_mask = SKY81452_LOUT,
  57. .enable_reg = SKY81452_REG1,
  58. .enable_mask = SKY81452_LEN,
  59. };
  60. #ifdef CONFIG_OF
  61. static struct regulator_init_data *sky81452_reg_parse_dt(struct device *dev)
  62. {
  63. struct regulator_init_data *init_data;
  64. struct device_node *np;
  65. np = of_get_child_by_name(dev->parent->of_node, "regulator");
  66. if (unlikely(!np)) {
  67. dev_err(dev, "regulator node not found");
  68. return NULL;
  69. }
  70. init_data = of_get_regulator_init_data(dev, np);
  71. of_node_put(np);
  72. return init_data;
  73. }
  74. #else
  75. static struct regulator_init_data *sky81452_reg_parse_dt(struct device *dev)
  76. {
  77. return ERR_PTR(-EINVAL);
  78. }
  79. #endif
  80. static int sky81452_reg_probe(struct platform_device *pdev)
  81. {
  82. struct device *dev = &pdev->dev;
  83. const struct regulator_init_data *init_data = dev_get_platdata(dev);
  84. struct regulator_config config = { };
  85. struct regulator_dev *rdev;
  86. if (!init_data) {
  87. init_data = sky81452_reg_parse_dt(dev);
  88. if (IS_ERR(init_data))
  89. return PTR_ERR(init_data);
  90. }
  91. config.dev = dev;
  92. config.init_data = init_data;
  93. config.of_node = dev->of_node;
  94. config.regmap = dev_get_drvdata(dev->parent);
  95. rdev = devm_regulator_register(dev, &sky81452_reg, &config);
  96. if (IS_ERR(rdev))
  97. return PTR_ERR(rdev);
  98. platform_set_drvdata(pdev, rdev);
  99. return 0;
  100. }
  101. static struct platform_driver sky81452_reg_driver = {
  102. .driver = {
  103. .name = "sky81452-regulator",
  104. },
  105. .probe = sky81452_reg_probe,
  106. };
  107. module_platform_driver(sky81452_reg_driver);
  108. MODULE_DESCRIPTION("Skyworks SKY81452 Regulator driver");
  109. MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@skyworksinc.com>");
  110. MODULE_LICENSE("GPL");
  111. MODULE_VERSION("1.0");