max1600.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MAX1600 PCMCIA power switch library
  4. *
  5. * Copyright (C) 2016 Russell King
  6. */
  7. #include <linux/device.h>
  8. #include <linux/module.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/slab.h>
  11. #include "max1600.h"
  12. static const char *max1600_gpio_name[2][MAX1600_GPIO_MAX] = {
  13. { "a0vcc", "a1vcc", "a0vpp", "a1vpp" },
  14. { "b0vcc", "b1vcc", "b0vpp", "b1vpp" },
  15. };
  16. int max1600_init(struct device *dev, struct max1600 **ptr,
  17. unsigned int channel, unsigned int code)
  18. {
  19. struct max1600 *m;
  20. int chan;
  21. int i;
  22. switch (channel) {
  23. case MAX1600_CHAN_A:
  24. chan = 0;
  25. break;
  26. case MAX1600_CHAN_B:
  27. chan = 1;
  28. break;
  29. default:
  30. return -EINVAL;
  31. }
  32. if (code != MAX1600_CODE_LOW && code != MAX1600_CODE_HIGH)
  33. return -EINVAL;
  34. m = devm_kzalloc(dev, sizeof(*m), GFP_KERNEL);
  35. if (!m)
  36. return -ENOMEM;
  37. m->dev = dev;
  38. m->code = code;
  39. for (i = 0; i < MAX1600_GPIO_MAX; i++) {
  40. const char *name;
  41. name = max1600_gpio_name[chan][i];
  42. if (i != MAX1600_GPIO_0VPP) {
  43. m->gpio[i] = devm_gpiod_get(dev, name, GPIOD_OUT_LOW);
  44. } else {
  45. m->gpio[i] = devm_gpiod_get_optional(dev, name,
  46. GPIOD_OUT_LOW);
  47. if (!m->gpio[i])
  48. break;
  49. }
  50. if (IS_ERR(m->gpio[i]))
  51. return PTR_ERR(m->gpio[i]);
  52. }
  53. *ptr = m;
  54. return 0;
  55. }
  56. EXPORT_SYMBOL_GPL(max1600_init);
  57. int max1600_configure(struct max1600 *m, unsigned int vcc, unsigned int vpp)
  58. {
  59. DECLARE_BITMAP(values, MAX1600_GPIO_MAX) = { 0, };
  60. int n = MAX1600_GPIO_0VPP;
  61. if (m->gpio[MAX1600_GPIO_0VPP]) {
  62. if (vpp == 0) {
  63. __assign_bit(MAX1600_GPIO_0VPP, values, 0);
  64. __assign_bit(MAX1600_GPIO_1VPP, values, 0);
  65. } else if (vpp == 120) {
  66. __assign_bit(MAX1600_GPIO_0VPP, values, 0);
  67. __assign_bit(MAX1600_GPIO_1VPP, values, 1);
  68. } else if (vpp == vcc) {
  69. __assign_bit(MAX1600_GPIO_0VPP, values, 1);
  70. __assign_bit(MAX1600_GPIO_1VPP, values, 0);
  71. } else {
  72. dev_err(m->dev, "unrecognised Vpp %u.%uV\n",
  73. vpp / 10, vpp % 10);
  74. return -EINVAL;
  75. }
  76. n = MAX1600_GPIO_MAX;
  77. } else if (vpp != vcc && vpp != 0) {
  78. dev_err(m->dev, "no VPP control\n");
  79. return -EINVAL;
  80. }
  81. if (vcc == 0) {
  82. __assign_bit(MAX1600_GPIO_0VCC, values, 0);
  83. __assign_bit(MAX1600_GPIO_1VCC, values, 0);
  84. } else if (vcc == 33) { /* VY */
  85. __assign_bit(MAX1600_GPIO_0VCC, values, 1);
  86. __assign_bit(MAX1600_GPIO_1VCC, values, 0);
  87. } else if (vcc == 50) { /* VX */
  88. __assign_bit(MAX1600_GPIO_0VCC, values, 0);
  89. __assign_bit(MAX1600_GPIO_1VCC, values, 1);
  90. } else {
  91. dev_err(m->dev, "unrecognised Vcc %u.%uV\n",
  92. vcc / 10, vcc % 10);
  93. return -EINVAL;
  94. }
  95. if (m->code == MAX1600_CODE_HIGH) {
  96. /*
  97. * Cirrus mode appears to be the same as Intel mode,
  98. * except the VCC pins are inverted.
  99. */
  100. __change_bit(MAX1600_GPIO_0VCC, values);
  101. __change_bit(MAX1600_GPIO_1VCC, values);
  102. }
  103. return gpiod_set_array_value_cansleep(n, m->gpio, NULL, values);
  104. }
  105. EXPORT_SYMBOL_GPL(max1600_configure);
  106. MODULE_LICENSE("GPL v2");