fpc1020_regulator.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* FPC1020 Touch sensor driver
  2. *
  3. * Copyright (c) 2013,2014 Fingerprint Cards AB <tech@fingerprints.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License Version 2
  7. * as published by the Free Software Foundation.
  8. */
  9. #define DEBUG
  10. #include <linux/regulator/consumer.h>
  11. #ifndef CONFIG_OF
  12. #include <linux/spi/fpc1020.h>
  13. #include <linux/spi/fpc1020_common.h>
  14. #include <linux/spi/fpc1020_regulator.h>
  15. #else
  16. #include <linux/of.h>
  17. #include "fpc1020.h"
  18. #include "fpc1020_common.h"
  19. #include "fpc1020_regulator.h"
  20. #endif
  21. /* -------------------------------------------------------------------- */
  22. int fpc1020_regulator_configure(fpc1020_data_t *fpc1020)
  23. {
  24. int error = 0;
  25. dev_dbg(&fpc1020->spi->dev, "%s\n", __func__);
  26. fpc1020->vdd_tx = regulator_get(&fpc1020->spi->dev, "vdd_tx");
  27. if (IS_ERR(fpc1020->vdd_tx)) {
  28. error = PTR_ERR(fpc1020->vdd_tx);
  29. dev_err(&fpc1020->spi->dev,
  30. "Regulator get failed, vdd_tx, error=%d\n", error);
  31. goto supply_err;
  32. }
  33. if (regulator_count_voltages(fpc1020->vdd_tx) > 0) {
  34. error = regulator_set_voltage(fpc1020->vdd_tx,
  35. SUPPLY_TX_MIN, SUPPLY_TX_MAX);
  36. if (error) {
  37. dev_err(&fpc1020->spi->dev,
  38. "regulator set(tx) failed, error=%d\n", error);
  39. goto supply_err;
  40. }
  41. }
  42. return 0;
  43. supply_err:
  44. fpc1020_regulator_release(fpc1020);
  45. return error;
  46. }
  47. /* -------------------------------------------------------------------- */
  48. int fpc1020_regulator_release(fpc1020_data_t *fpc1020)
  49. {
  50. if (fpc1020->vdd_tx != NULL) {
  51. regulator_put(fpc1020->vdd_tx);
  52. fpc1020->vdd_tx = NULL;
  53. }
  54. fpc1020->power_enabled = false;
  55. return 0;
  56. }
  57. /* -------------------------------------------------------------------- */
  58. int fpc1020_regulator_set(fpc1020_data_t *fpc1020, bool enable)
  59. {
  60. int error = 0;
  61. if (fpc1020->vdd_tx == NULL) {
  62. dev_err(&fpc1020->spi->dev,
  63. "Regulators not set\n");
  64. return -EINVAL;
  65. }
  66. if (enable) {
  67. dev_dbg(&fpc1020->spi->dev, "%s on\n", __func__);
  68. /******
  69. Do we really need to set current?
  70. How would it affect the vibrator that shares this regulator?
  71. regulator_set_optimum_mode(fpc1020->vcc_spi,
  72. SUPPLY_SPI_REQ_CURRENT);
  73. */
  74. error = (regulator_is_enabled(fpc1020->vdd_tx) == 0) ?
  75. regulator_enable(fpc1020->vdd_tx) : 0;
  76. if (error) {
  77. dev_err(&fpc1020->spi->dev,
  78. "Regulator vdd_tx enable failed, error=%d\n",
  79. error);
  80. goto out_err;
  81. }
  82. } else {
  83. dev_dbg(&fpc1020->spi->dev, "%s off\n", __func__);
  84. error = (fpc1020->power_enabled &&
  85. regulator_is_enabled(fpc1020->vdd_tx) > 0) ?
  86. regulator_disable(fpc1020->vdd_tx) : 0;
  87. if (error) {
  88. dev_err(&fpc1020->spi->dev,
  89. "Regulator vdd_tx disable failed, error=%d\n",
  90. error);
  91. goto out_err;
  92. }
  93. }
  94. fpc1020->power_enabled = enable;
  95. return 0;
  96. out_err:
  97. fpc1020_regulator_release(fpc1020);
  98. return error;
  99. }