arizona-micsupp.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. .owner = THIS_MODULE,
  85. };
  86. static const struct regulator_init_data arizona_micsupp_default = {
  87. .constraints = {
  88. .valid_ops_mask = REGULATOR_CHANGE_STATUS |
  89. REGULATOR_CHANGE_VOLTAGE,
  90. .min_uV = 1700000,
  91. .max_uV = 3300000,
  92. },
  93. .num_consumer_supplies = 1,
  94. };
  95. static __devinit int arizona_micsupp_probe(struct platform_device *pdev)
  96. {
  97. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  98. struct regulator_config config = { };
  99. struct arizona_micsupp *micsupp;
  100. int ret;
  101. micsupp = devm_kzalloc(&pdev->dev, sizeof(*micsupp), GFP_KERNEL);
  102. if (micsupp == NULL) {
  103. dev_err(&pdev->dev, "Unable to allocate private data\n");
  104. return -ENOMEM;
  105. }
  106. micsupp->arizona = arizona;
  107. /*
  108. * Since the chip usually supplies itself we provide some
  109. * default init_data for it. This will be overridden with
  110. * platform data if provided.
  111. */
  112. micsupp->init_data = arizona_micsupp_default;
  113. micsupp->init_data.consumer_supplies = &micsupp->supply;
  114. micsupp->supply.supply = "MICVDD";
  115. micsupp->supply.dev_name = dev_name(arizona->dev);
  116. config.dev = arizona->dev;
  117. config.driver_data = micsupp;
  118. config.regmap = arizona->regmap;
  119. if (arizona->pdata.micvdd)
  120. config.init_data = arizona->pdata.micvdd;
  121. else
  122. config.init_data = &micsupp->init_data;
  123. /* Default to regulated mode until the API supports bypass */
  124. regmap_update_bits(arizona->regmap, ARIZONA_MIC_CHARGE_PUMP_1,
  125. ARIZONA_CPMIC_BYPASS, 0);
  126. micsupp->regulator = regulator_register(&arizona_micsupp, &config);
  127. if (IS_ERR(micsupp->regulator)) {
  128. ret = PTR_ERR(micsupp->regulator);
  129. dev_err(arizona->dev, "Failed to register mic supply: %d\n",
  130. ret);
  131. return ret;
  132. }
  133. platform_set_drvdata(pdev, micsupp);
  134. return 0;
  135. }
  136. static __devexit int arizona_micsupp_remove(struct platform_device *pdev)
  137. {
  138. struct arizona_micsupp *micsupp = platform_get_drvdata(pdev);
  139. regulator_unregister(micsupp->regulator);
  140. return 0;
  141. }
  142. static struct platform_driver arizona_micsupp_driver = {
  143. .probe = arizona_micsupp_probe,
  144. .remove = __devexit_p(arizona_micsupp_remove),
  145. .driver = {
  146. .name = "arizona-micsupp",
  147. .owner = THIS_MODULE,
  148. },
  149. };
  150. module_platform_driver(arizona_micsupp_driver);
  151. /* Module information */
  152. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  153. MODULE_DESCRIPTION("Arizona microphone supply driver");
  154. MODULE_LICENSE("GPL");
  155. MODULE_ALIAS("platform:arizona-micsupp");