summaryrefslogtreecommitdiff
path: root/drivers/pinctrl/sophgo/pinctrl-sg2042-ops.c
blob: 0526aa3f84384ca1094c993bd6f5ac55946e48bd (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// SPDX-License-Identifier: GPL-2.0
/*
 * Sophgo sg2042 SoCs pinctrl driver.
 *
 * Copyright (C) 2024 Inochi Amaoto <inochiama@outlook.com>
 *
 */

#include <linux/bitfield.h>
#include <linux/bits.h>
#include <linux/cleanup.h>
#include <linux/export.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/seq_file.h>
#include <linux/spinlock.h>

#include <linux/pinctrl/pinconf-generic.h>
#include <linux/pinctrl/pinconf.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/pinmux.h>

#include "../pinctrl-utils.h"
#include "../pinmux.h"

#include "pinctrl-sg2042.h"

#define PIN_IO_PULL_ONE_ENABLE		BIT(0)
#define PIN_IO_PULL_DIR_UP		(BIT(1) | PIN_IO_PULL_ONE_ENABLE)
#define PIN_IO_PULL_DIR_DOWN		(0 | PIN_IO_PULL_ONE_ENABLE)
#define PIN_IO_PULL_ONE_MASK		GENMASK(1, 0)

#define PIN_IO_PULL_UP			BIT(2)
#define PIN_IO_PULL_UP_DONW		BIT(3)
#define PIN_IO_PULL_UP_MASK		GENMASK(3, 2)

#define PIN_IO_MUX			GENMASK(5, 4)
#define PIN_IO_DRIVE			GENMASK(9, 6)
#define PIN_IO_SCHMITT_ENABLE		BIT(10)
#define PIN_IO_OUTPUT_ENABLE		BIT(11)

struct sg2042_priv {
	void __iomem				*regs;
};

static u8 sg2042_dt_get_pin_mux(u32 value)
{
	return value >> 16;
}

static inline u32 sg2042_get_pin_reg(struct sophgo_pinctrl *pctrl,
				     const struct sophgo_pin *sp)
{
	struct sg2042_priv *priv = pctrl->priv_ctrl;
	const struct sg2042_pin *pin = sophgo_to_sg2042_pin(sp);
	void __iomem *reg = priv->regs + pin->offset;

	if (sp->flags & PIN_FLAG_WRITE_HIGH)
		return readl(reg) >> 16;
	else
		return readl(reg) & 0xffff;
}

static int sg2042_set_pin_reg(struct sophgo_pinctrl *pctrl,
			      const struct sophgo_pin *sp,
			      u32 value, u32 mask)
{
	struct sg2042_priv *priv = pctrl->priv_ctrl;
	const struct sg2042_pin *pin = sophgo_to_sg2042_pin(sp);
	void __iomem *reg = priv->regs + pin->offset;
	u32 v = readl(reg);

	if (sp->flags & PIN_FLAG_WRITE_HIGH) {
		v &= ~(mask << 16);
		v |= value << 16;
	} else {
		v &= ~mask;
		v |= value;
	}

	writel(v, reg);

	return 0;
}

static void sg2042_pctrl_dbg_show(struct pinctrl_dev *pctldev,
				  struct seq_file *seq, unsigned int pin_id)
{
	struct sophgo_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
	const struct sophgo_pin *sp = sophgo_get_pin(pctrl, pin_id);
	u32 value, mux;

	value = sg2042_get_pin_reg(pctrl, sp);
	mux = FIELD_GET(PIN_IO_MUX, value);
	seq_printf(seq, "mux:%u reg:0x%04x ", mux, value);
}

const struct pinctrl_ops sg2042_pctrl_ops = {
	.get_groups_count	= pinctrl_generic_get_group_count,
	.get_group_name		= pinctrl_generic_get_group_name,
	.get_group_pins		= pinctrl_generic_get_group_pins,
	.pin_dbg_show		= sg2042_pctrl_dbg_show,
	.dt_node_to_map		= sophgo_pctrl_dt_node_to_map,
	.dt_free_map		= pinctrl_utils_free_map,
};
EXPORT_SYMBOL_GPL(sg2042_pctrl_ops);

static void sg2042_set_pinmux_config(struct sophgo_pinctrl *pctrl,
				     const struct sophgo_pin *sp, u32 config)
{
	u32 mux = sg2042_dt_get_pin_mux(config);

	if (!(sp->flags & PIN_FLAG_NO_PINMUX))
		sg2042_set_pin_reg(pctrl, sp, mux, PIN_IO_MUX);
}

const struct pinmux_ops sg2042_pmx_ops = {
	.get_functions_count	= pinmux_generic_get_function_count,
	.get_function_name	= pinmux_generic_get_function_name,
	.get_function_groups	= pinmux_generic_get_function_groups,
	.set_mux		= sophgo_pmx_set_mux,
	.strict			= true,
};
EXPORT_SYMBOL_GPL(sg2042_pmx_ops);

static int sg2042_pconf_get(struct pinctrl_dev *pctldev,
			    unsigned int pin_id, unsigned long *config)
{
	struct sophgo_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
	int param = pinconf_to_config_param(*config);
	const struct sophgo_pin *sp = sophgo_get_pin(pctrl, pin_id);
	u32 value;
	u32 arg;
	bool enabled;
	int ret;

	if (!sp)
		return -EINVAL;

	value = sg2042_get_pin_reg(pctrl, sp);

	switch (param) {
	case PIN_CONFIG_BIAS_DISABLE:
		if (sp->flags & PIN_FLAG_ONLY_ONE_PULL)
			arg = FIELD_GET(PIN_IO_PULL_ONE_ENABLE, value);
		else
			arg = FIELD_GET(PIN_IO_PULL_UP_MASK, value);
		enabled = arg == 0;
		break;
	case PIN_CONFIG_BIAS_PULL_DOWN:
		if (sp->flags & PIN_FLAG_ONLY_ONE_PULL) {
			arg = FIELD_GET(PIN_IO_PULL_ONE_MASK, value);
			enabled = arg == PIN_IO_PULL_DIR_DOWN;
		} else {
			enabled = FIELD_GET(PIN_IO_PULL_UP_DONW, value) != 0;
		}
		arg = sophgo_pinctrl_typical_pull_down(pctrl, sp, NULL);
		break;
	case PIN_CONFIG_BIAS_PULL_UP:
		if (sp->flags & PIN_FLAG_ONLY_ONE_PULL) {
			arg = FIELD_GET(PIN_IO_PULL_ONE_MASK, value);
			enabled = arg == PIN_IO_PULL_DIR_UP;
		} else {
			enabled = FIELD_GET(PIN_IO_PULL_UP, value) != 0;
		}
		arg = sophgo_pinctrl_typical_pull_up(pctrl, sp, NULL);
		break;
	case PIN_CONFIG_DRIVE_STRENGTH_UA:
		enabled = FIELD_GET(PIN_IO_OUTPUT_ENABLE, value) != 0;
		arg = FIELD_GET(PIN_IO_DRIVE, value);
		ret = sophgo_pinctrl_reg2oc(pctrl, sp, NULL, arg);
		if (ret < 0)
			return ret;
		arg = ret;
		break;
	case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
		arg = FIELD_GET(PIN_IO_SCHMITT_ENABLE, value);
		enabled = arg != 0;
		break;
	default:
		return -ENOTSUPP;
	}

	*config = pinconf_to_config_packed(param, arg);

	return enabled ? 0 : -EINVAL;
}

static int sg2042_pinconf_compute_config(struct sophgo_pinctrl *pctrl,
					 const struct sophgo_pin *sp,
					 unsigned long *configs,
					 unsigned int num_configs,
					 u32 *value, u32 *mask)
{
	int i;
	u16 v = 0, m = 0;
	int ret;

	if (!sp)
		return -EINVAL;

	for (i = 0; i < num_configs; i++) {
		int param = pinconf_to_config_param(configs[i]);
		u32 arg = pinconf_to_config_argument(configs[i]);

		switch (param) {
		case PIN_CONFIG_BIAS_DISABLE:
			if (sp->flags & PIN_FLAG_ONLY_ONE_PULL) {
				v &= ~PIN_IO_PULL_ONE_ENABLE;
				m |= PIN_IO_PULL_ONE_ENABLE;
			} else {
				v &= ~PIN_IO_PULL_UP_MASK;
				m |= PIN_IO_PULL_UP_MASK;
			}
			break;
		case PIN_CONFIG_BIAS_PULL_DOWN:
			if (sp->flags & PIN_FLAG_ONLY_ONE_PULL) {
				v &= ~PIN_IO_PULL_ONE_MASK;
				v |= PIN_IO_PULL_DIR_DOWN;
				m |= PIN_IO_PULL_ONE_MASK;
			} else {
				v |= PIN_IO_PULL_UP_DONW;
				m |= PIN_IO_PULL_UP_DONW;
			}
			break;
		case PIN_CONFIG_BIAS_PULL_UP:
			if (sp->flags & PIN_FLAG_ONLY_ONE_PULL) {
				v &= ~PIN_IO_PULL_ONE_MASK;
				v |= PIN_IO_PULL_DIR_UP;
				m |= PIN_IO_PULL_ONE_MASK;
			} else {
				v |= PIN_IO_PULL_UP;
				m |= PIN_IO_PULL_UP;
			}
			break;
		case PIN_CONFIG_DRIVE_STRENGTH_UA:
			v &= ~(PIN_IO_DRIVE | PIN_IO_OUTPUT_ENABLE);
			if (arg != 0) {
				ret = sophgo_pinctrl_oc2reg(pctrl, sp, NULL, arg);
				if (ret < 0)
					return ret;
				if (!(sp->flags & PIN_FLAG_NO_OEX_EN))
					v |= PIN_IO_OUTPUT_ENABLE;
				v |= FIELD_PREP(PIN_IO_DRIVE, ret);
			}
			m |= PIN_IO_DRIVE | PIN_IO_OUTPUT_ENABLE;
			break;
		case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
			v |= PIN_IO_SCHMITT_ENABLE;
			m |= PIN_IO_SCHMITT_ENABLE;
			break;
		default:
			return -ENOTSUPP;
		}
	}

	*value = v;
	*mask = m;

	return 0;
}

const struct pinconf_ops sg2042_pconf_ops = {
	.pin_config_get			= sg2042_pconf_get,
	.pin_config_set			= sophgo_pconf_set,
	.pin_config_group_set		= sophgo_pconf_group_set,
	.is_generic			= true,
};
EXPORT_SYMBOL_GPL(sg2042_pconf_ops);

static int sophgo_pinctrl_init(struct platform_device *pdev,
			       struct sophgo_pinctrl *pctrl)
{
	struct sg2042_priv *priv;

	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->regs = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(priv->regs))
		return PTR_ERR(priv->regs);

	pctrl->priv_ctrl = priv;

	return 0;
}

const struct sophgo_cfg_ops sg2042_cfg_ops = {
	.pctrl_init = sophgo_pinctrl_init,
	.compute_pinconf_config = sg2042_pinconf_compute_config,
	.set_pinconf_config = sg2042_set_pin_reg,
	.set_pinmux_config = sg2042_set_pinmux_config,
};
EXPORT_SYMBOL_GPL(sg2042_cfg_ops);