• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
4  * Copyright (c) 2020 Linaro Ltd.
5  */
6 
7 #include <linux/clk.h>
8 #include <linux/gpio/driver.h>
9 #include <linux/module.h>
10 #include <linux/of_device.h>
11 #include <linux/pinctrl/pinconf-generic.h>
12 #include <linux/pinctrl/pinconf.h>
13 #include <linux/pinctrl/pinmux.h>
14 #include "../pinctrl-utils.h"
15 #include "pinctrl-lpass-lpi.h"
16 
17 #define MAX_LPI_NUM_CLKS	2
18 
19 struct lpi_pinctrl {
20 	struct device *dev;
21 	struct pinctrl_dev *ctrl;
22 	struct gpio_chip chip;
23 	struct pinctrl_desc desc;
24 	char __iomem *tlmm_base;
25 	char __iomem *slew_base;
26 	struct clk_bulk_data clks[MAX_LPI_NUM_CLKS];
27 	/* Protects from concurrent register updates */
28 	struct mutex lock;
29 	const struct lpi_pinctrl_variant_data *data;
30 };
31 
lpi_gpio_read(struct lpi_pinctrl * state,unsigned int pin,unsigned int addr)32 static int lpi_gpio_read(struct lpi_pinctrl *state, unsigned int pin,
33 			 unsigned int addr)
34 {
35 	return ioread32(state->tlmm_base + LPI_TLMM_REG_OFFSET * pin + addr);
36 }
37 
lpi_gpio_write(struct lpi_pinctrl * state,unsigned int pin,unsigned int addr,unsigned int val)38 static int lpi_gpio_write(struct lpi_pinctrl *state, unsigned int pin,
39 			  unsigned int addr, unsigned int val)
40 {
41 	iowrite32(val, state->tlmm_base + LPI_TLMM_REG_OFFSET * pin + addr);
42 
43 	return 0;
44 }
45 
46 static const struct pinctrl_ops lpi_gpio_pinctrl_ops = {
47 	.get_groups_count	= pinctrl_generic_get_group_count,
48 	.get_group_name		= pinctrl_generic_get_group_name,
49 	.get_group_pins		= pinctrl_generic_get_group_pins,
50 	.dt_node_to_map		= pinconf_generic_dt_node_to_map_group,
51 	.dt_free_map		= pinctrl_utils_free_map,
52 };
53 
lpi_gpio_get_functions_count(struct pinctrl_dev * pctldev)54 static int lpi_gpio_get_functions_count(struct pinctrl_dev *pctldev)
55 {
56 	struct lpi_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
57 
58 	return pctrl->data->nfunctions;
59 }
60 
lpi_gpio_get_function_name(struct pinctrl_dev * pctldev,unsigned int function)61 static const char *lpi_gpio_get_function_name(struct pinctrl_dev *pctldev,
62 					      unsigned int function)
63 {
64 	struct lpi_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
65 
66 	return pctrl->data->functions[function].name;
67 }
68 
lpi_gpio_get_function_groups(struct pinctrl_dev * pctldev,unsigned int function,const char * const ** groups,unsigned * const num_qgroups)69 static int lpi_gpio_get_function_groups(struct pinctrl_dev *pctldev,
70 					unsigned int function,
71 					const char *const **groups,
72 					unsigned *const num_qgroups)
73 {
74 	struct lpi_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
75 
76 	*groups = pctrl->data->functions[function].groups;
77 	*num_qgroups = pctrl->data->functions[function].ngroups;
78 
79 	return 0;
80 }
81 
lpi_gpio_set_mux(struct pinctrl_dev * pctldev,unsigned int function,unsigned int group_num)82 static int lpi_gpio_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
83 			    unsigned int group_num)
84 {
85 	struct lpi_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
86 	const struct lpi_pingroup *g = &pctrl->data->groups[group_num];
87 	u32 val;
88 	int i, pin = g->pin;
89 
90 	for (i = 0; i < g->nfuncs; i++) {
91 		if (g->funcs[i] == function)
92 			break;
93 	}
94 
95 	if (WARN_ON(i == g->nfuncs))
96 		return -EINVAL;
97 
98 	mutex_lock(&pctrl->lock);
99 	val = lpi_gpio_read(pctrl, pin, LPI_GPIO_CFG_REG);
100 	u32p_replace_bits(&val, i, LPI_GPIO_FUNCTION_MASK);
101 	lpi_gpio_write(pctrl, pin, LPI_GPIO_CFG_REG, val);
102 	mutex_unlock(&pctrl->lock);
103 
104 	return 0;
105 }
106 
107 static const struct pinmux_ops lpi_gpio_pinmux_ops = {
108 	.get_functions_count	= lpi_gpio_get_functions_count,
109 	.get_function_name	= lpi_gpio_get_function_name,
110 	.get_function_groups	= lpi_gpio_get_function_groups,
111 	.set_mux		= lpi_gpio_set_mux,
112 };
113 
lpi_config_get(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * config)114 static int lpi_config_get(struct pinctrl_dev *pctldev,
115 			  unsigned int pin, unsigned long *config)
116 {
117 	unsigned int param = pinconf_to_config_param(*config);
118 	struct lpi_pinctrl *state = dev_get_drvdata(pctldev->dev);
119 	unsigned int arg = 0;
120 	int is_out;
121 	int pull;
122 	u32 ctl_reg;
123 
124 	ctl_reg = lpi_gpio_read(state, pin, LPI_GPIO_CFG_REG);
125 	is_out = ctl_reg & LPI_GPIO_OE_MASK;
126 	pull = FIELD_GET(LPI_GPIO_PULL_MASK, ctl_reg);
127 
128 	switch (param) {
129 	case PIN_CONFIG_BIAS_DISABLE:
130 		if (pull == LPI_GPIO_BIAS_DISABLE)
131 			arg = 1;
132 		break;
133 	case PIN_CONFIG_BIAS_PULL_DOWN:
134 		if (pull == LPI_GPIO_PULL_DOWN)
135 			arg = 1;
136 		break;
137 	case PIN_CONFIG_BIAS_BUS_HOLD:
138 		if (pull == LPI_GPIO_KEEPER)
139 			arg = 1;
140 		break;
141 	case PIN_CONFIG_BIAS_PULL_UP:
142 		if (pull == LPI_GPIO_PULL_UP)
143 			arg = 1;
144 		break;
145 	case PIN_CONFIG_INPUT_ENABLE:
146 	case PIN_CONFIG_OUTPUT:
147 		if (is_out)
148 			arg = 1;
149 		break;
150 	default:
151 		return -EINVAL;
152 	}
153 
154 	*config = pinconf_to_config_packed(param, arg);
155 	return 0;
156 }
157 
lpi_config_set(struct pinctrl_dev * pctldev,unsigned int group,unsigned long * configs,unsigned int nconfs)158 static int lpi_config_set(struct pinctrl_dev *pctldev, unsigned int group,
159 			  unsigned long *configs, unsigned int nconfs)
160 {
161 	struct lpi_pinctrl *pctrl = dev_get_drvdata(pctldev->dev);
162 	unsigned int param, arg, pullup = LPI_GPIO_BIAS_DISABLE, strength = 2;
163 	bool value, output_enabled = false;
164 	const struct lpi_pingroup *g;
165 	unsigned long sval;
166 	int i, slew_offset;
167 	u32 val;
168 
169 	g = &pctrl->data->groups[group];
170 	for (i = 0; i < nconfs; i++) {
171 		param = pinconf_to_config_param(configs[i]);
172 		arg = pinconf_to_config_argument(configs[i]);
173 
174 		switch (param) {
175 		case PIN_CONFIG_BIAS_DISABLE:
176 			pullup = LPI_GPIO_BIAS_DISABLE;
177 			break;
178 		case PIN_CONFIG_BIAS_PULL_DOWN:
179 			pullup = LPI_GPIO_PULL_DOWN;
180 			break;
181 		case PIN_CONFIG_BIAS_BUS_HOLD:
182 			pullup = LPI_GPIO_KEEPER;
183 			break;
184 		case PIN_CONFIG_BIAS_PULL_UP:
185 			pullup = LPI_GPIO_PULL_UP;
186 			break;
187 		case PIN_CONFIG_INPUT_ENABLE:
188 			output_enabled = false;
189 			break;
190 		case PIN_CONFIG_OUTPUT:
191 			output_enabled = true;
192 			value = arg;
193 			break;
194 		case PIN_CONFIG_DRIVE_STRENGTH:
195 			strength = arg;
196 			break;
197 		case PIN_CONFIG_SLEW_RATE:
198 			if (arg > LPI_SLEW_RATE_MAX) {
199 				dev_err(pctldev->dev, "invalid slew rate %u for pin: %d\n",
200 					arg, group);
201 				return -EINVAL;
202 			}
203 
204 			slew_offset = g->slew_offset;
205 			if (slew_offset == LPI_NO_SLEW)
206 				break;
207 
208 			mutex_lock(&pctrl->lock);
209 
210 			sval = ioread32(pctrl->slew_base + LPI_SLEW_RATE_CTL_REG);
211 			sval &= ~(LPI_SLEW_RATE_MASK << slew_offset);
212 			sval |= arg << slew_offset;
213 			iowrite32(sval, pctrl->slew_base + LPI_SLEW_RATE_CTL_REG);
214 
215 			mutex_unlock(&pctrl->lock);
216 			break;
217 		default:
218 			return -EINVAL;
219 		}
220 	}
221 
222 	/*
223 	 * As per Hardware Programming Guide, when configuring pin as output,
224 	 * set the pin value before setting output-enable (OE).
225 	 */
226 	if (output_enabled) {
227 		val = u32_encode_bits(value ? 1 : 0, LPI_GPIO_VALUE_OUT_MASK);
228 		lpi_gpio_write(pctrl, group, LPI_GPIO_VALUE_REG, val);
229 	}
230 
231 	mutex_lock(&pctrl->lock);
232 	val = lpi_gpio_read(pctrl, group, LPI_GPIO_CFG_REG);
233 
234 	u32p_replace_bits(&val, pullup, LPI_GPIO_PULL_MASK);
235 	u32p_replace_bits(&val, LPI_GPIO_DS_TO_VAL(strength),
236 			  LPI_GPIO_OUT_STRENGTH_MASK);
237 	u32p_replace_bits(&val, output_enabled, LPI_GPIO_OE_MASK);
238 
239 	lpi_gpio_write(pctrl, group, LPI_GPIO_CFG_REG, val);
240 	mutex_unlock(&pctrl->lock);
241 
242 	return 0;
243 }
244 
245 static const struct pinconf_ops lpi_gpio_pinconf_ops = {
246 	.is_generic			= true,
247 	.pin_config_group_get		= lpi_config_get,
248 	.pin_config_group_set		= lpi_config_set,
249 };
250 
lpi_gpio_direction_input(struct gpio_chip * chip,unsigned int pin)251 static int lpi_gpio_direction_input(struct gpio_chip *chip, unsigned int pin)
252 {
253 	struct lpi_pinctrl *state = gpiochip_get_data(chip);
254 	unsigned long config;
255 
256 	config = pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE, 1);
257 
258 	return lpi_config_set(state->ctrl, pin, &config, 1);
259 }
260 
lpi_gpio_direction_output(struct gpio_chip * chip,unsigned int pin,int val)261 static int lpi_gpio_direction_output(struct gpio_chip *chip,
262 				     unsigned int pin, int val)
263 {
264 	struct lpi_pinctrl *state = gpiochip_get_data(chip);
265 	unsigned long config;
266 
267 	config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, val);
268 
269 	return lpi_config_set(state->ctrl, pin, &config, 1);
270 }
271 
lpi_gpio_get(struct gpio_chip * chip,unsigned int pin)272 static int lpi_gpio_get(struct gpio_chip *chip, unsigned int pin)
273 {
274 	struct lpi_pinctrl *state = gpiochip_get_data(chip);
275 
276 	return lpi_gpio_read(state, pin, LPI_GPIO_VALUE_REG) &
277 		LPI_GPIO_VALUE_IN_MASK;
278 }
279 
lpi_gpio_set(struct gpio_chip * chip,unsigned int pin,int value)280 static void lpi_gpio_set(struct gpio_chip *chip, unsigned int pin, int value)
281 {
282 	struct lpi_pinctrl *state = gpiochip_get_data(chip);
283 	unsigned long config;
284 
285 	config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, value);
286 
287 	lpi_config_set(state->ctrl, pin, &config, 1);
288 }
289 
290 #ifdef CONFIG_DEBUG_FS
291 #include <linux/seq_file.h>
292 
lpi_regval_to_drive(u32 val)293 static unsigned int lpi_regval_to_drive(u32 val)
294 {
295 	return (val + 1) * 2;
296 }
297 
lpi_gpio_dbg_show_one(struct seq_file * s,struct pinctrl_dev * pctldev,struct gpio_chip * chip,unsigned int offset,unsigned int gpio)298 static void lpi_gpio_dbg_show_one(struct seq_file *s,
299 				  struct pinctrl_dev *pctldev,
300 				  struct gpio_chip *chip,
301 				  unsigned int offset,
302 				  unsigned int gpio)
303 {
304 	struct lpi_pinctrl *state = gpiochip_get_data(chip);
305 	struct pinctrl_pin_desc pindesc;
306 	unsigned int func;
307 	int is_out;
308 	int drive;
309 	int pull;
310 	u32 ctl_reg;
311 
312 	static const char * const pulls[] = {
313 		"no pull",
314 		"pull down",
315 		"keeper",
316 		"pull up"
317 	};
318 
319 	pctldev = pctldev ? : state->ctrl;
320 	pindesc = pctldev->desc->pins[offset];
321 	ctl_reg = lpi_gpio_read(state, offset, LPI_GPIO_CFG_REG);
322 	is_out = ctl_reg & LPI_GPIO_OE_MASK;
323 
324 	func = FIELD_GET(LPI_GPIO_FUNCTION_MASK, ctl_reg);
325 	drive = FIELD_GET(LPI_GPIO_OUT_STRENGTH_MASK, ctl_reg);
326 	pull = FIELD_GET(LPI_GPIO_PULL_MASK, ctl_reg);
327 
328 	seq_printf(s, " %-8s: %-3s %d", pindesc.name, is_out ? "out" : "in", func);
329 	seq_printf(s, " %dmA", lpi_regval_to_drive(drive));
330 	seq_printf(s, " %s", pulls[pull]);
331 }
332 
lpi_gpio_dbg_show(struct seq_file * s,struct gpio_chip * chip)333 static void lpi_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
334 {
335 	unsigned int gpio = chip->base;
336 	unsigned int i;
337 
338 	for (i = 0; i < chip->ngpio; i++, gpio++) {
339 		lpi_gpio_dbg_show_one(s, NULL, chip, i, gpio);
340 		seq_puts(s, "\n");
341 	}
342 }
343 
344 #else
345 #define lpi_gpio_dbg_show NULL
346 #endif
347 
348 static const struct gpio_chip lpi_gpio_template = {
349 	.direction_input	= lpi_gpio_direction_input,
350 	.direction_output	= lpi_gpio_direction_output,
351 	.get			= lpi_gpio_get,
352 	.set			= lpi_gpio_set,
353 	.request		= gpiochip_generic_request,
354 	.free			= gpiochip_generic_free,
355 	.dbg_show		= lpi_gpio_dbg_show,
356 };
357 
lpi_build_pin_desc_groups(struct lpi_pinctrl * pctrl)358 static int lpi_build_pin_desc_groups(struct lpi_pinctrl *pctrl)
359 {
360 	int i, ret;
361 
362 	for (i = 0; i < pctrl->data->npins; i++) {
363 		const struct pinctrl_pin_desc *pin_info = pctrl->desc.pins + i;
364 
365 		ret = pinctrl_generic_add_group(pctrl->ctrl, pin_info->name,
366 						  (int *)&pin_info->number, 1, NULL);
367 		if (ret < 0)
368 			goto err_pinctrl;
369 	}
370 
371 	return 0;
372 
373 err_pinctrl:
374 	for (; i > 0; i--)
375 		pinctrl_generic_remove_group(pctrl->ctrl, i - 1);
376 
377 	return ret;
378 }
379 
lpi_pinctrl_probe(struct platform_device * pdev)380 int lpi_pinctrl_probe(struct platform_device *pdev)
381 {
382 	const struct lpi_pinctrl_variant_data *data;
383 	struct device *dev = &pdev->dev;
384 	struct lpi_pinctrl *pctrl;
385 	int ret;
386 
387 	pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
388 	if (!pctrl)
389 		return -ENOMEM;
390 
391 	platform_set_drvdata(pdev, pctrl);
392 
393 	data = of_device_get_match_data(dev);
394 	if (!data)
395 		return -EINVAL;
396 
397 	pctrl->data = data;
398 	pctrl->dev = &pdev->dev;
399 
400 	pctrl->clks[0].id = "core";
401 	pctrl->clks[1].id = "audio";
402 
403 	pctrl->tlmm_base = devm_platform_ioremap_resource(pdev, 0);
404 	if (IS_ERR(pctrl->tlmm_base))
405 		return dev_err_probe(dev, PTR_ERR(pctrl->tlmm_base),
406 				     "TLMM resource not provided\n");
407 
408 	pctrl->slew_base = devm_platform_ioremap_resource(pdev, 1);
409 	if (IS_ERR(pctrl->slew_base))
410 		return dev_err_probe(dev, PTR_ERR(pctrl->slew_base),
411 				     "Slew resource not provided\n");
412 
413 	if (of_property_read_bool(dev->of_node, "qcom,adsp-bypass-mode"))
414 		ret = devm_clk_bulk_get_optional(dev, MAX_LPI_NUM_CLKS, pctrl->clks);
415 	else
416 		ret = devm_clk_bulk_get(dev, MAX_LPI_NUM_CLKS, pctrl->clks);
417 
418 	if (ret)
419 		return ret;
420 
421 	ret = clk_bulk_prepare_enable(MAX_LPI_NUM_CLKS, pctrl->clks);
422 	if (ret)
423 		return dev_err_probe(dev, ret, "Can't enable clocks\n");
424 
425 	pctrl->desc.pctlops = &lpi_gpio_pinctrl_ops;
426 	pctrl->desc.pmxops = &lpi_gpio_pinmux_ops;
427 	pctrl->desc.confops = &lpi_gpio_pinconf_ops;
428 	pctrl->desc.owner = THIS_MODULE;
429 	pctrl->desc.name = dev_name(dev);
430 	pctrl->desc.pins = data->pins;
431 	pctrl->desc.npins = data->npins;
432 	pctrl->chip = lpi_gpio_template;
433 	pctrl->chip.parent = dev;
434 	pctrl->chip.base = -1;
435 	pctrl->chip.ngpio = data->npins;
436 	pctrl->chip.label = dev_name(dev);
437 	pctrl->chip.of_gpio_n_cells = 2;
438 	pctrl->chip.can_sleep = false;
439 
440 	mutex_init(&pctrl->lock);
441 
442 	pctrl->ctrl = devm_pinctrl_register(dev, &pctrl->desc, pctrl);
443 	if (IS_ERR(pctrl->ctrl)) {
444 		ret = PTR_ERR(pctrl->ctrl);
445 		dev_err(dev, "failed to add pin controller\n");
446 		goto err_pinctrl;
447 	}
448 
449 	ret = lpi_build_pin_desc_groups(pctrl);
450 	if (ret)
451 		goto err_pinctrl;
452 
453 	ret = devm_gpiochip_add_data(dev, &pctrl->chip, pctrl);
454 	if (ret) {
455 		dev_err(pctrl->dev, "can't add gpio chip\n");
456 		goto err_pinctrl;
457 	}
458 
459 	return 0;
460 
461 err_pinctrl:
462 	mutex_destroy(&pctrl->lock);
463 	clk_bulk_disable_unprepare(MAX_LPI_NUM_CLKS, pctrl->clks);
464 
465 	return ret;
466 }
467 EXPORT_SYMBOL_GPL(lpi_pinctrl_probe);
468 
lpi_pinctrl_remove(struct platform_device * pdev)469 int lpi_pinctrl_remove(struct platform_device *pdev)
470 {
471 	struct lpi_pinctrl *pctrl = platform_get_drvdata(pdev);
472 	int i;
473 
474 	mutex_destroy(&pctrl->lock);
475 	clk_bulk_disable_unprepare(MAX_LPI_NUM_CLKS, pctrl->clks);
476 
477 	for (i = 0; i < pctrl->data->npins; i++)
478 		pinctrl_generic_remove_group(pctrl->ctrl, i);
479 
480 	return 0;
481 }
482 EXPORT_SYMBOL_GPL(lpi_pinctrl_remove);
483 
484 MODULE_DESCRIPTION("QTI LPI GPIO pin control driver");
485 MODULE_LICENSE("GPL");
486