summaryrefslogtreecommitdiff
path: root/snd-alpx/alpx_gpio.c
blob: 47340570f202b8888db974b8ac3f546b00f4898e (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
*  Support for Digigram AlpX PCI-e boards
*
*  Copyright (c) 2024 Digigram Digital (info@digigram.com)
*/


#include "alpx_gpio.h"

#include <linux/io.h>

static inline bool alpx_gpio_is_an_input (struct alpx_device *alpx_dev, unsigned offset)
{
	return offset < alpx_dev->variant->gpios.inputs_qty;
}


int alpx_gpio_get(struct gpio_chip *chip, unsigned offset)
{
	struct alpx_device *alpx_dev = gpiochip_get_data(chip);
	u32 value;
	
	if (alpx_gpio_is_an_input(alpx_dev, offset)) {
		value = readl(alpx_dev->base + alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.inputs_reg_offset);
		
		dev_dbg( alpx_dev->dev,"[0x%08x] =>[0x%08x]\n", alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.inputs_reg_offset,  value);
	}
	else {
		value = readl(alpx_dev->base + alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.outputs_reg_offset);
		
		dev_dbg( alpx_dev->dev,"[0x%08x] =>[0x%08x]\n", alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.outputs_reg_offset,  value);
		//translate offset -> bit rank
		offset -= alpx_dev->variant->gpios.inputs_qty;
	}
	
	return ALPX_GPIO_VALUE(offset, value);
}

void alpx_gpio_set(struct gpio_chip *chip, unsigned offset,
			int gpio_value)
{
	struct alpx_device *alpx_dev = gpiochip_get_data(chip);
	unsigned int value;
	
		
	if (alpx_gpio_is_an_input(alpx_dev, offset))
		return;

	//translate Offset to bit rank
	offset -= alpx_dev->variant->gpios.inputs_qty;

	value = readl(alpx_dev->base + alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.outputs_reg_offset );
	
	dev_dbg( alpx_dev->dev,"read [0x%08x] =>0x%08x\n",alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.outputs_reg_offset,  value);


	value &= ~ALPX_GPIO_MASK(offset);
	value |= ALPX_GPIO_SEL(offset, gpio_value);
	
	dev_dbg( alpx_dev->dev,"write 0x%08x =>[0x%08x]\n", value , alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.outputs_reg_offset);
	
	writel(value, alpx_dev->base + alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.outputs_reg_offset);
}

int alpx_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
{
	struct alpx_device *alpx_dev = gpiochip_get_data(chip);
	
	if (offset >= chip->ngpio)
		return -EINVAL;
	
	dev_dbg( alpx_dev->dev,"Offset %d is %s\n", 
			 offset, alpx_gpio_is_an_input(alpx_dev, offset) ? "INPUT" : "OUTPUT");
	
	return alpx_gpio_is_an_input(alpx_dev, offset) ? GPIOF_DIR_IN : GPIOF_DIR_OUT;
}

int alpx_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
					int gpio_value)
{
	struct alpx_device *alpx_dev = gpiochip_get_data(chip);
	if (alpx_gpio_is_an_input(alpx_dev, offset))
		return -EINVAL;

	alpx_gpio_set(chip, offset, gpio_value);

	return 0;
}

int alpx_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
{
	/* All ports handle INPUTs operations */
	return 0;
}

int alpx_gpio_register(struct alpx_device *alpx_dev, const char* card_name)
{
	struct gpio_chip *chip = &alpx_dev->gpio_chip;

	chip->parent = alpx_dev->dev;
	chip->owner = THIS_MODULE;
	chip->label = card_name;
	chip->base = -1;

	chip->ngpio = alpx_dev->variant->gpios.inputs_qty + alpx_dev->variant->gpios.outputs_qty;
	chip->get = alpx_gpio_get;
	chip->set = alpx_gpio_set;
	chip->get_direction = alpx_gpio_get_direction;
	chip->direction_output = alpx_gpio_direction_output;
	chip->direction_input = alpx_gpio_direction_input;
	
	dev_dbg( alpx_dev->dev,"%s(): creating GP(%d)I(%d)O for %s.\n", __func__, 
			alpx_dev->variant->gpios.inputs_qty,
			alpx_dev->variant->gpios.outputs_qty,
			chip->label);

	return gpiochip_add_data(chip, alpx_dev);
}

void alpx_gpio_unregister(struct alpx_device *alpx_dev)
{
	struct gpio_chip* const chip = &alpx_dev->gpio_chip;	
	if (chip->parent == alpx_dev->dev)
		gpiochip_remove(chip);
}