summaryrefslogtreecommitdiff
path: root/drivers/pcmcia/max1600.c
blob: 379875a5e7cd4368585d854c947afdf664bdf10f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// SPDX-License-Identifier: GPL-2.0
/*
 * MAX1600 PCMCIA power switch library
 *
 * Copyright (C) 2016 Russell King
 */
#include <linux/device.h>
#include <linux/module.h>
#include <linux/gpio/consumer.h>
#include <linux/slab.h>
#include "max1600.h"

static const char *max1600_gpio_name[2][MAX1600_GPIO_MAX] = {
	{ "a0vcc", "a1vcc", "a0vpp", "a1vpp" },
	{ "b0vcc", "b1vcc", "b0vpp", "b1vpp" },
};

int max1600_init(struct device *dev, struct max1600 **ptr,
	unsigned int channel, unsigned int code)
{
	struct max1600 *m;
	int chan;
	int i;

	switch (channel) {
	case MAX1600_CHAN_A:
		chan = 0;
		break;
	case MAX1600_CHAN_B:
		chan = 1;
		break;
	default:
		return -EINVAL;
	}

	if (code != MAX1600_CODE_LOW && code != MAX1600_CODE_HIGH)
		return -EINVAL;

	m = devm_kzalloc(dev, sizeof(*m), GFP_KERNEL);
	if (!m)
		return -ENOMEM;

	m->dev = dev;
	m->code = code;

	for (i = 0; i < MAX1600_GPIO_MAX; i++) {
		const char *name;

		name = max1600_gpio_name[chan][i];
		if (i != MAX1600_GPIO_0VPP) {
			m->gpio[i] = devm_gpiod_get(dev, name, GPIOD_OUT_LOW);
		} else {
			m->gpio[i] = devm_gpiod_get_optional(dev, name,
							     GPIOD_OUT_LOW);
			if (!m->gpio[i])
				break;
		}
		if (IS_ERR(m->gpio[i]))
			return PTR_ERR(m->gpio[i]);
	}

	*ptr = m;

	return 0;
}
EXPORT_SYMBOL_GPL(max1600_init);

int max1600_configure(struct max1600 *m, unsigned int vcc, unsigned int vpp)
{
	DECLARE_BITMAP(values, MAX1600_GPIO_MAX) = { 0, };
	int n = MAX1600_GPIO_0VPP;

	if (m->gpio[MAX1600_GPIO_0VPP]) {
		if (vpp == 0) {
			__assign_bit(MAX1600_GPIO_0VPP, values, 0);
			__assign_bit(MAX1600_GPIO_1VPP, values, 0);
		} else if (vpp == 120) {
			__assign_bit(MAX1600_GPIO_0VPP, values, 0);
			__assign_bit(MAX1600_GPIO_1VPP, values, 1);
		} else if (vpp == vcc) {
			__assign_bit(MAX1600_GPIO_0VPP, values, 1);
			__assign_bit(MAX1600_GPIO_1VPP, values, 0);
		} else {
			dev_err(m->dev, "unrecognised Vpp %u.%uV\n",
				vpp / 10, vpp % 10);
			return -EINVAL;
		}
		n = MAX1600_GPIO_MAX;
	} else if (vpp != vcc && vpp != 0) {
		dev_err(m->dev, "no VPP control\n");
		return -EINVAL;
	}

	if (vcc == 0) {
		__assign_bit(MAX1600_GPIO_0VCC, values, 0);
		__assign_bit(MAX1600_GPIO_1VCC, values, 0);
	} else if (vcc == 33) { /* VY */
		__assign_bit(MAX1600_GPIO_0VCC, values, 1);
		__assign_bit(MAX1600_GPIO_1VCC, values, 0);
	} else if (vcc == 50) { /* VX */
		__assign_bit(MAX1600_GPIO_0VCC, values, 0);
		__assign_bit(MAX1600_GPIO_1VCC, values, 1);
	} else {
		dev_err(m->dev, "unrecognised Vcc %u.%uV\n",
			vcc / 10, vcc % 10);
		return -EINVAL;
	}

	if (m->code == MAX1600_CODE_HIGH) {
		/*
		 * Cirrus mode appears to be the same as Intel mode,
		 * except the VCC pins are inverted.
		 */
		__change_bit(MAX1600_GPIO_0VCC, values);
		__change_bit(MAX1600_GPIO_1VCC, values);
	}

	return gpiod_set_array_value_cansleep(n, m->gpio, NULL, values);
}
EXPORT_SYMBOL_GPL(max1600_configure);

MODULE_LICENSE("GPL v2");