arizona-micsupp.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * arizona-micsupp.c -- Microphone supply for Arizona devices
  3. *
  4. * Copyright 2012 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/bitops.h>
  17. #include <linux/err.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/regulator/machine.h>
  21. #include <linux/gpio.h>
  22. #include <linux/slab.h>
  23. #include <linux/mfd/arizona/core.h>
  24. #include <linux/mfd/arizona/pdata.h>
  25. #include <linux/mfd/arizona/registers.h>
  26. #define ARIZONA_MICSUPP_MAX_SELECTOR 0x1f
  27. struct arizona_micsupp {
  28. struct regulator_dev *regulator;
  29. struct arizona *arizona;
  30. struct regulator_consumer_supply supply;
  31. struct regulator_init_data init_data;
  32. };
  33. static int arizona_micsupp_list_voltage(struct regulator_dev *rdev,
  34. unsigned int selector)
  35. {
  36. if (selector > ARIZONA_MICSUPP_MAX_SELECTOR)
  37. return -EINVAL;
  38. if (selector == ARIZONA_MICSUPP_MAX_SELECTOR)
  39. return 3300000;
  40. else
  41. return (selector * 50000) + 1700000;
  42. }
  43. static int arizona_micsupp_map_voltage(struct regulator_dev *rdev,
  44. int min_uV, int max_uV)
  45. {
  46. unsigned int voltage;
  47. int selector;
  48. if (min_uV < 1700000)
  49. min_uV = 1700000;
  50. if (min_uV > 3200000)
  51. selector = ARIZONA_MICSUPP_MAX_SELECTOR;
  52. else
  53. selector = DIV_ROUND_UP(min_uV - 1700000, 50000);
  54. if (selector < 0)
  55. return -EINVAL;
  56. voltage = arizona_micsupp_list_voltage(rdev, selector);
  57. if (voltage < min_uV || voltage > max_uV)
  58. return -EINVAL;
  59. return selector;
  60. }
  61. static struct regulator_ops arizona_micsupp_ops = {
  62. .enable = regulator_enable_regmap,
  63. .disable = regulator_disable_regmap,
  64. .is_enabled = regulator_is_enabled_regmap,
  65. .list_voltage = arizona_micsupp_list_voltage,
  66. .map_voltage = arizona_micsupp_map_voltage,
  67. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  68. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  69. .get_bypass = regulator_get_bypass_regmap,
  70. .set_bypass = regulator_set_bypass_regmap,
  71. };
  72. static const struct regulator_desc arizona_micsupp = {
  73. .name = "MICVDD",
  74. .supply_name = "CPVDD",
  75. .type = REGULATOR_VOLTAGE,
  76. .n_voltages = ARIZONA_MICSUPP_MAX_SELECTOR + 1,
  77. .ops = &arizona_micsupp_ops,
  78. .vsel_reg = ARIZONA_LDO2_CONTROL_1,
  79. .vsel_mask = ARIZONA_LDO2_VSEL_MASK,
  80. .enable_reg = ARIZONA_MIC_CHARGE_PUMP_1,
  81. .enable_mask = ARIZONA_CPMIC_ENA,
  82. .bypass_reg = ARIZONA_MIC_CHARGE_PUMP_1,
  83. .bypass_mask = ARIZONA_CPMIC_BYPASS,
  84. .enable_time = 3000,
  85. .owner = THIS_MODULE,
  86. };
  87. static const struct regulator_init_data arizona_micsupp_default = {
  88. .constraints = {
  89. .valid_ops_mask = REGULATOR_CHANGE_STATUS |
  90. REGULATOR_CHANGE_VOLTAGE,
  91. .min_uV = 1700000,
  92. .max_uV = 3300000,
  93. },
  94. .num_consumer_supplies = 1,
  95. };
  96. static int arizona_micsupp_probe(struct platform_device *pdev)
  97. {
  98. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  99. struct regulator_config config = { };
  100. struct arizona_micsupp *micsupp;
  101. int ret;
  102. micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
  103. if (micsupp == NULL) {
  104. dev_err(&pdev->dev, "Unable to allocate private data\n");
  105. return -ENOMEM;
  106. }
  107. micsupp->arizona = arizona;
  108. /*
  109. * Since the chip usually supplies itself we provide some
  110. * default init_data for it. This will be overridden with
  111. * platform data if provided.
  112. */
  113. micsupp->init_data = arizona_micsupp_default;
  114. micsupp->init_data.consumer_supplies = &micsupp->supply;
  115. micsupp->supply.supply = "MICVDD";
  116. micsupp->supply.dev_name = dev_name(arizona->dev);
  117. config.dev = arizona->dev;
  118. config.driver_data = micsupp;
  119. config.regmap = arizona->regmap;
  120. if (arizona->pdata.micvdd)
  121. config.init_data = arizona->pdata.micvdd;
  122. else
  123. config.init_data = &micsupp->init_data;
  124. /* Default to regulated mode until the API supports bypass */
  125. regmap_update_bits(arizona->regmap, ARIZONA_MIC_CHARGE_PUMP_1,
  126. ARIZONA_CPMIC_BYPASS, 0);
  127. micsupp->regulator = regulator_register(&arizona_micsupp, &config);
  128. if (IS_ERR(micsupp->regulator)) {
  129. ret = PTR_ERR(micsupp->regulator);
  130. dev_err(arizona->dev, "Failed to register mic supply: %d\n",
  131. ret);
  132. return ret;
  133. }
  134. platform_set_drvdata(pdev, micsupp);
  135. return 0;
  136. }
  137. static int arizona_micsupp_remove(struct platform_device *pdev)
  138. {
  139. struct arizona_micsupp *micsupp = platform_get_drvdata(pdev);
  140. regulator_unregister(micsupp->regulator);
  141. return 0;
  142. }
  143. static struct platform_driver arizona_micsupp_driver = {
  144. .probe = arizona_micsupp_probe,
  145. .remove = arizona_micsupp_remove,
  146. .driver = {
  147. .name = "arizona-micsupp",
  148. .owner = THIS_MODULE,
  149. },
  150. };
  151. module_platform_driver(arizona_micsupp_driver);
  152. /* Module information */
  153. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  154. MODULE_DESCRIPTION("Arizona microphone supply driver");
  155. MODULE_LICENSE("GPL");
  156. MODULE_ALIAS("platform:arizona-micsupp");