summaryrefslogtreecommitdiff
path: root/drivers/net/can/spi/mcp25xxfd/mcp25xxfd_gpio.c
blob: 5bda9d8a6e7e899c5a73b77a41e17ba8603ff2bb (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
// SPDX-License-Identifier: GPL-2.0

/* CAN bus driver for Microchip 25XXFD CAN Controller with SPI Interface
 *
 * Copyright 2019 Martin Sperl <kernel@martin.sperl.org>
 *
 * Based on Microchip MCP251x CAN controller driver written by
 * David Vrabel, Copyright 2006 Arcom Control Systems Ltd.
 */

#include <linux/gpio/driver.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>

#include "mcp25xxfd_clock.h"
#include "mcp25xxfd_cmd.h"
#include "mcp25xxfd_gpio.h"
#include "mcp25xxfd_priv.h"

static int mcp25xxfd_gpio_request(struct gpio_chip *chip,
				  unsigned int offset)
{
	struct mcp25xxfd_priv *priv = gpiochip_get_data(chip);
	int clock_requestor = offset ?
		MCP25XXFD_CLK_USER_GPIO1 : MCP25XXFD_CLK_USER_GPIO0;

	/* only handle gpio 0/1 */
	if (offset > 1)
		return -EINVAL;

	/* if we have XSTANDBY enabled then gpio0 is not available either */
	if (priv->config.gpio0_xstandby && offset == 0)
		return -EINVAL;

	mcp25xxfd_clock_start(priv, clock_requestor);

	return 0;
}

static void mcp25xxfd_gpio_free(struct gpio_chip *chip,
				unsigned int offset)
{
	struct mcp25xxfd_priv *priv = gpiochip_get_data(chip);
	int clock_requestor = offset ?
		MCP25XXFD_CLK_USER_GPIO1 : MCP25XXFD_CLK_USER_GPIO0;

	/* only handle gpio 0/1 */
	if (offset > 1)
		return;

	mcp25xxfd_clock_stop(priv, clock_requestor);
}

static int mcp25xxfd_gpio_get(struct gpio_chip *chip, unsigned int offset)
{
	struct mcp25xxfd_priv *priv = gpiochip_get_data(chip);
	u32 mask = (offset) ? MCP25XXFD_IOCON_GPIO1 : MCP25XXFD_IOCON_GPIO0;
	int ret;

	/* only handle gpio 0/1 */
	if (offset > 1)
		return -EINVAL;

	/* read the relevant gpio Latch */
	ret = mcp25xxfd_cmd_read_mask(priv->spi, MCP25XXFD_IOCON,
				      &priv->regs.iocon, mask);
	if (ret)
		return ret;

	/* return the match */
	return priv->regs.iocon & mask;
}

static void mcp25xxfd_gpio_set(struct gpio_chip *chip, unsigned int offset,
			       int value)
{
	struct mcp25xxfd_priv *priv = gpiochip_get_data(chip);
	u32 mask = (offset) ? MCP25XXFD_IOCON_LAT1 : MCP25XXFD_IOCON_LAT0;

	/* only handle gpio 0/1 */
	if (offset > 1)
		return;

	/* update in memory representation with the corresponding value */
	if (value)
		priv->regs.iocon |= mask;
	else
		priv->regs.iocon &= ~mask;

	mcp25xxfd_cmd_write_mask(priv->spi, MCP25XXFD_IOCON,
				 priv->regs.iocon, mask);
}

static int mcp25xxfd_gpio_direction_input(struct gpio_chip *chip,
					  unsigned int offset)
{
	struct mcp25xxfd_priv *priv = gpiochip_get_data(chip);
	u32 mask_tri = (offset) ?
		MCP25XXFD_IOCON_TRIS1 : MCP25XXFD_IOCON_TRIS0;
	u32 mask_stby = (offset) ?
		0 : MCP25XXFD_IOCON_XSTBYEN;
	u32 mask_pm = (offset) ?
		MCP25XXFD_IOCON_PM1 : MCP25XXFD_IOCON_PM0;

	/* only handle gpio 0/1 */
	if (offset > 1)
		return -EINVAL;

	/* set the mask */
	priv->regs.iocon |= mask_tri | mask_pm;

	/* clear stby */
	priv->regs.iocon &= ~mask_stby;

	return mcp25xxfd_cmd_write_mask(priv->spi, MCP25XXFD_IOCON,
					priv->regs.iocon,
					mask_tri | mask_stby | mask_pm);
}

static int mcp25xxfd_gpio_direction_output(struct gpio_chip *chip,
					   unsigned int offset, int value)
{
	struct mcp25xxfd_priv *priv = gpiochip_get_data(chip);
	u32 mask_tri = (offset) ?
		MCP25XXFD_IOCON_TRIS1 : MCP25XXFD_IOCON_TRIS0;
	u32 mask_lat = (offset) ?
		MCP25XXFD_IOCON_LAT1 : MCP25XXFD_IOCON_LAT0;
	u32 mask_pm = (offset) ?
		MCP25XXFD_IOCON_PM1 : MCP25XXFD_IOCON_PM0;
	u32 mask_stby = (offset) ?
		0 : MCP25XXFD_IOCON_XSTBYEN;

	/* only handle gpio 0/1 */
	if (offset > 1)
		return -EINVAL;

	/* clear the tristate bit and also clear stby */
	priv->regs.iocon &= ~(mask_tri | mask_stby);

	/* set GPIO mode */
	priv->regs.iocon |= mask_pm;

	/* set the value */
	if (value)
		priv->regs.iocon |= mask_lat;
	else
		priv->regs.iocon &= ~mask_lat;

	return mcp25xxfd_cmd_write_mask(priv->spi, MCP25XXFD_IOCON,
					priv->regs.iocon,
					mask_tri | mask_lat |
					mask_pm | mask_stby);
}

#ifdef CONFIG_OF_DYNAMIC
static void mcp25xxfd_gpio_read_of(struct mcp25xxfd_priv *priv)
{
	const struct device_node *np = priv->spi->dev.of_node;

	priv->config.gpio_open_drain =
		of_property_read_bool(np, "microchip,gpio-open-drain");
	priv->config.gpio0_xstandby =
		of_property_read_bool(np, "microchip,gpio0-xstandby");
}
#else
static void mcp25xxfd_gpio_read_of(struct mcp25xxfd_priv *priv)
{
	priv->config.gpio_open_drain = false;
	priv->config.gpio0_xstandby = false;
}
#endif

static int mcp25xxfd_gpio_setup_regs(struct mcp25xxfd_priv *priv)
{
	/* handle open-drain */
	if (priv->config.gpio_open_drain)
		priv->regs.iocon |= MCP25XXFD_IOCON_INTOD;
	else
		priv->regs.iocon &= ~MCP25XXFD_IOCON_INTOD;

	/* handle xstandby */
	if (priv->config.gpio0_xstandby) {
		priv->regs.iocon &= ~(MCP25XXFD_IOCON_TRIS0 |
				      MCP25XXFD_IOCON_GPIO0);
		priv->regs.iocon |= MCP25XXFD_IOCON_XSTBYEN;
	} else {
		priv->regs.iocon &= ~(MCP25XXFD_IOCON_XSTBYEN);
	}

	/* handle sof / clko */
	if (priv->config.clock_odiv == 0)
		priv->regs.iocon |= MCP25XXFD_IOCON_SOF;
	else
		priv->regs.iocon &= ~MCP25XXFD_IOCON_SOF;

	/* update the iocon register */
	return mcp25xxfd_cmd_write_regs(priv->spi, MCP25XXFD_IOCON,
					&priv->regs.iocon, sizeof(u32));
}

static int mcp25xxfd_gpio_setup_gpiochip(struct mcp25xxfd_priv *priv)
{
	struct gpio_chip *gpio = &priv->gpio;

	/* gpiochip only handles GPIO0 and GPIO1 */
	gpio->owner                = THIS_MODULE;
	gpio->parent               = &priv->spi->dev;
	gpio->label                = dev_name(&priv->spi->dev);
	gpio->direction_input      = mcp25xxfd_gpio_direction_input;
	gpio->get                  = mcp25xxfd_gpio_get;
	gpio->direction_output     = mcp25xxfd_gpio_direction_output;
	gpio->set                  = mcp25xxfd_gpio_set;
	gpio->request              = mcp25xxfd_gpio_request;
	gpio->free                 = mcp25xxfd_gpio_free;
	gpio->base                 = -1;
	gpio->ngpio                = 2;
	gpio->can_sleep            = 1;

	return gpiochip_add_data(gpio, priv);
}

int mcp25xxfd_gpio_setup(struct mcp25xxfd_priv *priv)
{
	int ret;

	/* setting up defaults */
	priv->config.gpio0_xstandby = false;

	mcp25xxfd_gpio_read_of(priv);
	ret = mcp25xxfd_gpio_setup_regs(priv);
	if (ret)
		return ret;

	return mcp25xxfd_gpio_setup_gpiochip(priv);
}

void mcp25xxfd_gpio_remove(struct mcp25xxfd_priv *priv)
{
	gpiochip_remove(&priv->gpio);
}