summaryrefslogtreecommitdiff
path: root/io-module/mtdc_gpiob.c
blob: c207483f165760451f64bb57d5cfb62a9804b966 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

struct gpio_pin *dc_gpio_pin_by_attr_name(const char *name) {
	struct gpio_pin *pin;
	char *pin_attr_name;

	if (!strcmp(name, "led1")) {
		pin_attr_name = "dc-gpio1";
	} else if (!strcmp(name, "led2")) {
		pin_attr_name = "dc-gpio2";
	} else if (!strcmp(name, "dout-enable")) {
		pin_attr_name = "dc-gpio3";
	} else {
		log_error("daughter card attribute %s not available", name);
		return NULL;
	}

	for (pin = gpio_pins; *pin->name; pin++) {
		if (!strcmp(pin->pin.label, pin_attr_name)) {
			return pin;
		}
	}

	log_error("pin with attr name %s not found", name);

	return NULL;
}


static ssize_t mts_attr_show_dc_gpio_pin(struct device *dev,
			struct device_attribute *attr,
			char *buf)
{
	int value;
	struct gpio_pin *pin = dc_gpio_pin_by_attr_name(attr->attr.name);

	if (!pin) {
		return -ENODEV;
	}

	mutex_lock(&mts_io_mutex);

	value = gpio_get_value(pin->pin.gpio);

	mutex_unlock(&mts_io_mutex);

	if (value < 0) {
		return value;
	}

	if (pin->active_low) {
		value = !value;
	}

	return sprintf(buf, "%d\n", value);
}

static ssize_t mts_attr_store_dc_gpio_pin(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	int value;
	struct gpio_pin *pin = dc_gpio_pin_by_attr_name(attr->attr.name);

	if (!pin) {
		return -ENODEV;
	}

	if (sscanf(buf, "%i", &value) != 1) {
		return -EINVAL;
	}

	if (pin->active_low) {
		value = !value;
	}

	mutex_lock(&mts_io_mutex);

	gpio_set_value(pin->pin.gpio, value);

	mutex_unlock(&mts_io_mutex);

	return count;
}

static ssize_t mts_attr_show_dc_din(struct device *dev, struct device_attribute *attr, char *buf)
{
	int tmp;
	u8 bit;
	u8 byte;

	if (!spi_dc_din_dev) {
		log_error("dc din device not present");
		return -ENODEV;
	}

	if (!strcmp(attr->attr.name, "din0")) {
		bit = BIT(0);
	} else if (!strcmp(attr->attr.name, "din1")) {
		bit = BIT(1);
	} else if (!strcmp(attr->attr.name, "din2")) {
		bit = BIT(2);
	} else if (!strcmp(attr->attr.name, "din3")) {
		bit = BIT(3);
	} else {
		log_error("dc din attr does not exist");
		return -ENOENT;
	}

	tmp = spi_readn(spi_dc_din_dev, &byte, 1);
	if (tmp) {
		log_error("spi_read failed %d", tmp);
		return tmp;
	}

	tmp = byte & bit ? 1 : 0;

	return sprintf(buf, "%d\n", tmp);
}

static ssize_t mts_attr_store_dc_dout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
	int value;
	u8 bit;

	if (!spi_dc_dout_dev) {
		log_error("dc dout device not present");
		return -ENODEV;
	}

	if (!strcmp(attr->attr.name, "dout0")) {
		bit = BIT(0);
	} else if (!strcmp(attr->attr.name, "dout1")) {
		bit = BIT(1);
	} else if (!strcmp(attr->attr.name, "dout2")) {
		bit = BIT(2);
	} else if (!strcmp(attr->attr.name, "dout3")) {
		bit = BIT(3);
	} else {
		log_error("dc dout attr does not exist");
		return -ENOENT;
	}

	if (sscanf(buf, "%i", &value) != 1) {
		log_error("dc dout attr invalid argument");
		return -EINVAL;
	}

	mutex_lock(&spi_dc_dout_mutex);

	if (value) {
		spi_dc_dout_value &= ~bit;
	} else {
		spi_dc_dout_value |= bit;
	}

	spi_writen(spi_dc_dout_dev, &spi_dc_dout_value, 1);

	mutex_unlock(&spi_dc_dout_mutex);

	return count;
}

static ssize_t mts_attr_show_dc_dout(struct device *dev, struct device_attribute *attr, char *buf)
{
	int value;
	u8 bit;

	if (!spi_dc_dout_dev) {
		log_error("dc dout device not present");
		return -ENODEV;
	}

	if (!strcmp(attr->attr.name, "dout0")) {
		bit = BIT(0);
	} else if (!strcmp(attr->attr.name, "dout1")) {
		bit = BIT(1);
	} else if (!strcmp(attr->attr.name, "dout2")) {
		bit = BIT(2);
	} else if (!strcmp(attr->attr.name, "dout3")) {
		bit = BIT(3);
	} else {
		log_error("dc dout attr does not exist");
		return -ENOENT;
	}

	mutex_lock(&spi_dc_dout_mutex);

	value = spi_dc_dout_value & bit ? 0 : 1;

	mutex_unlock(&spi_dc_dout_mutex);

	return sprintf(buf, "%d\n", value);
}

static ssize_t mts_attr_show_dc_adc(struct device *dev, struct device_attribute *attr, char *buf)
{
	int tmp;
	int tx_data;
	int rx_data;
	int channel;
	int channel_mask = 0x0180;  /* 0b 0000 0001 1000 0000 */
	int manual_mode = 0x1840;   /* 0b 0001 1000 0100 0000 */
	uint8_t tx[2];
	uint8_t rx[2];

	if (!spi_dc_adc_dev) {
		log_error("dc adc device not present");
		return -ENODEV;
	}

	memset(tx, 0, sizeof(tx));
	memset(rx, 0, sizeof(rx));

	if (!strcmp(attr->attr.name, "adc0")) {
		channel = 0;
	} else if (!strcmp(attr->attr.name, "adc1")) {
		channel = 1;
	} else if (! strcmp(attr->attr.name, "adc2")) {
		channel = 2;
	} else {
		log_error("dc adc attr does not exist");
		return -ENOENT;
	}

	/* 1st transfer to set up (5V reference, channel to read from) */
	tx_data = manual_mode | ((channel << 7) & channel_mask);
	tx[0] = tx_data >> 8;
	tx[1] = tx_data & 0xFF;
	tmp = spi_writen(spi_dc_adc_dev, tx, 2);
	if (tmp) {
		log_error("spi_write failed %d", tmp);
		return tmp;
	}

	/* 2nd transfer to clock chip for ADC conversion
	 * this can be a throw-away read or an empty write,
	 * the ADC just needs the clock running so it can convert */
	tx[0] = 0;
	tx[1] = 0;
	tmp = spi_writen(spi_dc_adc_dev, tx, 2);
	if (tmp) {
		log_error("2nd spi_write failed %d", tmp);
		return tmp;
	}

	/* 3rd transfer to read data */
	tmp = spi_readn(spi_dc_adc_dev, rx, 2);
	if (tmp) {
		log_error("spi_read failed %d", tmp);
		return tmp;
	}
	rx_data = ((rx[0] & 0x0F) << 8) | (rx[1] & 0xFF);

	return sprintf(buf, "%lu\n", (unsigned long) rx_data);
}

/* MTDC-GPIOB */
static DEVICE_ATTR_RO_MTS(dev_attr_dc_din0, "din0", mts_attr_show_dc_din);
static DEVICE_ATTR_RO_MTS(dev_attr_dc_din1, "din1", mts_attr_show_dc_din);
static DEVICE_ATTR_RO_MTS(dev_attr_dc_din2, "din2", mts_attr_show_dc_din);
static DEVICE_ATTR_RO_MTS(dev_attr_dc_din3, "din3", mts_attr_show_dc_din);
static DEVICE_ATTR_MTS(dev_attr_dc_dout0, "dout0", mts_attr_show_dc_dout, mts_attr_store_dc_dout);
static DEVICE_ATTR_MTS(dev_attr_dc_dout1, "dout1", mts_attr_show_dc_dout, mts_attr_store_dc_dout);
static DEVICE_ATTR_MTS(dev_attr_dc_dout2, "dout2", mts_attr_show_dc_dout, mts_attr_store_dc_dout);
static DEVICE_ATTR_MTS(dev_attr_dc_dout3, "dout3", mts_attr_show_dc_dout, mts_attr_store_dc_dout);
static DEVICE_ATTR_RO_MTS(dev_attr_dc_adc0, "adc0", mts_attr_show_dc_adc);
static DEVICE_ATTR_RO_MTS(dev_attr_dc_adc1, "adc1", mts_attr_show_dc_adc);
static DEVICE_ATTR_RO_MTS(dev_attr_dc_adc2, "adc2", mts_attr_show_dc_adc);
static DEVICE_ATTR_MTS(dev_attr_dc_led1, "led1", mts_attr_show_dc_gpio_pin, mts_attr_store_dc_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_dc_led2, "led2", mts_attr_show_dc_gpio_pin, mts_attr_store_dc_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_dc_oe, "dout-enable", mts_attr_show_dc_gpio_pin, mts_attr_store_dc_gpio_pin);