drm/tidss: Fix typos
[drm/drm-misc.git] / drivers / pinctrl / qcom / pinctrl-lpass-lpi.c
blob7366aba5a199b46bab7d722856ee30f5dc7ff72b
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 */
7 #include <linux/bitfield.h>
8 #include <linux/clk.h>
9 #include <linux/gpio/driver.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/seq_file.h>
15 #include <linux/pinctrl/pinconf-generic.h>
16 #include <linux/pinctrl/pinconf.h>
17 #include <linux/pinctrl/pinmux.h>
19 #include "../pinctrl-utils.h"
21 #include "pinctrl-lpass-lpi.h"
23 #define MAX_NR_GPIO 32
24 #define GPIO_FUNC 0
25 #define MAX_LPI_NUM_CLKS 2
27 struct lpi_pinctrl {
28 struct device *dev;
29 struct pinctrl_dev *ctrl;
30 struct gpio_chip chip;
31 struct pinctrl_desc desc;
32 char __iomem *tlmm_base;
33 char __iomem *slew_base;
34 struct clk_bulk_data clks[MAX_LPI_NUM_CLKS];
35 /* Protects from concurrent register updates */
36 struct mutex lock;
37 DECLARE_BITMAP(ever_gpio, MAX_NR_GPIO);
38 const struct lpi_pinctrl_variant_data *data;
41 static int lpi_gpio_read(struct lpi_pinctrl *state, unsigned int pin,
42 unsigned int addr)
44 return ioread32(state->tlmm_base + LPI_TLMM_REG_OFFSET * pin + addr);
47 static int lpi_gpio_write(struct lpi_pinctrl *state, unsigned int pin,
48 unsigned int addr, unsigned int val)
50 iowrite32(val, state->tlmm_base + LPI_TLMM_REG_OFFSET * pin + addr);
52 return 0;
55 static const struct pinctrl_ops lpi_gpio_pinctrl_ops = {
56 .get_groups_count = pinctrl_generic_get_group_count,
57 .get_group_name = pinctrl_generic_get_group_name,
58 .get_group_pins = pinctrl_generic_get_group_pins,
59 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
60 .dt_free_map = pinctrl_utils_free_map,
63 static int lpi_gpio_get_functions_count(struct pinctrl_dev *pctldev)
65 struct lpi_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
67 return pctrl->data->nfunctions;
70 static const char *lpi_gpio_get_function_name(struct pinctrl_dev *pctldev,
71 unsigned int function)
73 struct lpi_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
75 return pctrl->data->functions[function].name;
78 static int lpi_gpio_get_function_groups(struct pinctrl_dev *pctldev,
79 unsigned int function,
80 const char *const **groups,
81 unsigned *const num_qgroups)
83 struct lpi_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
85 *groups = pctrl->data->functions[function].groups;
86 *num_qgroups = pctrl->data->functions[function].ngroups;
88 return 0;
91 static int lpi_gpio_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
92 unsigned int group)
94 struct lpi_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
95 const struct lpi_pingroup *g = &pctrl->data->groups[group];
96 u32 val;
97 int i, pin = g->pin;
99 for (i = 0; i < g->nfuncs; i++) {
100 if (g->funcs[i] == function)
101 break;
104 if (WARN_ON(i == g->nfuncs))
105 return -EINVAL;
107 mutex_lock(&pctrl->lock);
108 val = lpi_gpio_read(pctrl, pin, LPI_GPIO_CFG_REG);
111 * If this is the first time muxing to GPIO and the direction is
112 * output, make sure that we're not going to be glitching the pin
113 * by reading the current state of the pin and setting it as the
114 * output.
116 if (i == GPIO_FUNC && (val & LPI_GPIO_OE_MASK) &&
117 !test_and_set_bit(group, pctrl->ever_gpio)) {
118 u32 io_val = lpi_gpio_read(pctrl, group, LPI_GPIO_VALUE_REG);
120 if (io_val & LPI_GPIO_VALUE_IN_MASK) {
121 if (!(io_val & LPI_GPIO_VALUE_OUT_MASK))
122 lpi_gpio_write(pctrl, group, LPI_GPIO_VALUE_REG,
123 io_val | LPI_GPIO_VALUE_OUT_MASK);
124 } else {
125 if (io_val & LPI_GPIO_VALUE_OUT_MASK)
126 lpi_gpio_write(pctrl, group, LPI_GPIO_VALUE_REG,
127 io_val & ~LPI_GPIO_VALUE_OUT_MASK);
131 u32p_replace_bits(&val, i, LPI_GPIO_FUNCTION_MASK);
132 lpi_gpio_write(pctrl, pin, LPI_GPIO_CFG_REG, val);
133 mutex_unlock(&pctrl->lock);
135 return 0;
138 static const struct pinmux_ops lpi_gpio_pinmux_ops = {
139 .get_functions_count = lpi_gpio_get_functions_count,
140 .get_function_name = lpi_gpio_get_function_name,
141 .get_function_groups = lpi_gpio_get_function_groups,
142 .set_mux = lpi_gpio_set_mux,
145 static int lpi_config_get(struct pinctrl_dev *pctldev,
146 unsigned int pin, unsigned long *config)
148 unsigned int param = pinconf_to_config_param(*config);
149 struct lpi_pinctrl *state = dev_get_drvdata(pctldev->dev);
150 unsigned int arg = 0;
151 int is_out;
152 int pull;
153 u32 ctl_reg;
155 ctl_reg = lpi_gpio_read(state, pin, LPI_GPIO_CFG_REG);
156 is_out = ctl_reg & LPI_GPIO_OE_MASK;
157 pull = FIELD_GET(LPI_GPIO_PULL_MASK, ctl_reg);
159 switch (param) {
160 case PIN_CONFIG_BIAS_DISABLE:
161 if (pull == LPI_GPIO_BIAS_DISABLE)
162 arg = 1;
163 break;
164 case PIN_CONFIG_BIAS_PULL_DOWN:
165 if (pull == LPI_GPIO_PULL_DOWN)
166 arg = 1;
167 break;
168 case PIN_CONFIG_BIAS_BUS_HOLD:
169 if (pull == LPI_GPIO_KEEPER)
170 arg = 1;
171 break;
172 case PIN_CONFIG_BIAS_PULL_UP:
173 if (pull == LPI_GPIO_PULL_UP)
174 arg = 1;
175 break;
176 case PIN_CONFIG_INPUT_ENABLE:
177 case PIN_CONFIG_OUTPUT:
178 if (is_out)
179 arg = 1;
180 break;
181 default:
182 return -EINVAL;
185 *config = pinconf_to_config_packed(param, arg);
186 return 0;
189 static int lpi_config_set_slew_rate(struct lpi_pinctrl *pctrl,
190 const struct lpi_pingroup *g,
191 unsigned int group, unsigned int slew)
193 unsigned long sval;
194 void __iomem *reg;
195 int slew_offset;
197 if (slew > LPI_SLEW_RATE_MAX) {
198 dev_err(pctrl->dev, "invalid slew rate %u for pin: %d\n",
199 slew, group);
200 return -EINVAL;
203 slew_offset = g->slew_offset;
204 if (slew_offset == LPI_NO_SLEW)
205 return 0;
207 if (pctrl->data->flags & LPI_FLAG_SLEW_RATE_SAME_REG)
208 reg = pctrl->tlmm_base + LPI_TLMM_REG_OFFSET * group + LPI_GPIO_CFG_REG;
209 else
210 reg = pctrl->slew_base + LPI_SLEW_RATE_CTL_REG;
212 mutex_lock(&pctrl->lock);
214 sval = ioread32(reg);
215 sval &= ~(LPI_SLEW_RATE_MASK << slew_offset);
216 sval |= slew << slew_offset;
217 iowrite32(sval, reg);
219 mutex_unlock(&pctrl->lock);
221 return 0;
224 static int lpi_config_set(struct pinctrl_dev *pctldev, unsigned int group,
225 unsigned long *configs, unsigned int nconfs)
227 struct lpi_pinctrl *pctrl = dev_get_drvdata(pctldev->dev);
228 unsigned int param, arg, pullup = LPI_GPIO_BIAS_DISABLE, strength = 2;
229 bool value, output_enabled = false;
230 const struct lpi_pingroup *g;
231 int i, ret;
232 u32 val;
234 g = &pctrl->data->groups[group];
235 for (i = 0; i < nconfs; i++) {
236 param = pinconf_to_config_param(configs[i]);
237 arg = pinconf_to_config_argument(configs[i]);
239 switch (param) {
240 case PIN_CONFIG_BIAS_DISABLE:
241 pullup = LPI_GPIO_BIAS_DISABLE;
242 break;
243 case PIN_CONFIG_BIAS_PULL_DOWN:
244 pullup = LPI_GPIO_PULL_DOWN;
245 break;
246 case PIN_CONFIG_BIAS_BUS_HOLD:
247 pullup = LPI_GPIO_KEEPER;
248 break;
249 case PIN_CONFIG_BIAS_PULL_UP:
250 pullup = LPI_GPIO_PULL_UP;
251 break;
252 case PIN_CONFIG_INPUT_ENABLE:
253 output_enabled = false;
254 break;
255 case PIN_CONFIG_OUTPUT:
256 output_enabled = true;
257 value = arg;
258 break;
259 case PIN_CONFIG_DRIVE_STRENGTH:
260 strength = arg;
261 break;
262 case PIN_CONFIG_SLEW_RATE:
263 ret = lpi_config_set_slew_rate(pctrl, g, group, arg);
264 if (ret)
265 return ret;
266 break;
267 default:
268 return -EINVAL;
273 * As per Hardware Programming Guide, when configuring pin as output,
274 * set the pin value before setting output-enable (OE).
276 if (output_enabled) {
277 val = u32_encode_bits(value ? 1 : 0, LPI_GPIO_VALUE_OUT_MASK);
278 lpi_gpio_write(pctrl, group, LPI_GPIO_VALUE_REG, val);
281 mutex_lock(&pctrl->lock);
282 val = lpi_gpio_read(pctrl, group, LPI_GPIO_CFG_REG);
284 u32p_replace_bits(&val, pullup, LPI_GPIO_PULL_MASK);
285 u32p_replace_bits(&val, LPI_GPIO_DS_TO_VAL(strength),
286 LPI_GPIO_OUT_STRENGTH_MASK);
287 u32p_replace_bits(&val, output_enabled, LPI_GPIO_OE_MASK);
289 lpi_gpio_write(pctrl, group, LPI_GPIO_CFG_REG, val);
290 mutex_unlock(&pctrl->lock);
292 return 0;
295 static const struct pinconf_ops lpi_gpio_pinconf_ops = {
296 .is_generic = true,
297 .pin_config_group_get = lpi_config_get,
298 .pin_config_group_set = lpi_config_set,
301 static int lpi_gpio_direction_input(struct gpio_chip *chip, unsigned int pin)
303 struct lpi_pinctrl *state = gpiochip_get_data(chip);
304 unsigned long config;
306 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE, 1);
308 return lpi_config_set(state->ctrl, pin, &config, 1);
311 static int lpi_gpio_direction_output(struct gpio_chip *chip,
312 unsigned int pin, int val)
314 struct lpi_pinctrl *state = gpiochip_get_data(chip);
315 unsigned long config;
317 config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, val);
319 return lpi_config_set(state->ctrl, pin, &config, 1);
322 static int lpi_gpio_get(struct gpio_chip *chip, unsigned int pin)
324 struct lpi_pinctrl *state = gpiochip_get_data(chip);
326 return lpi_gpio_read(state, pin, LPI_GPIO_VALUE_REG) &
327 LPI_GPIO_VALUE_IN_MASK;
330 static void lpi_gpio_set(struct gpio_chip *chip, unsigned int pin, int value)
332 struct lpi_pinctrl *state = gpiochip_get_data(chip);
333 unsigned long config;
335 config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, value);
337 lpi_config_set(state->ctrl, pin, &config, 1);
340 #ifdef CONFIG_DEBUG_FS
342 static unsigned int lpi_regval_to_drive(u32 val)
344 return (val + 1) * 2;
347 static void lpi_gpio_dbg_show_one(struct seq_file *s,
348 struct pinctrl_dev *pctldev,
349 struct gpio_chip *chip,
350 unsigned int offset,
351 unsigned int gpio)
353 struct lpi_pinctrl *state = gpiochip_get_data(chip);
354 struct pinctrl_pin_desc pindesc;
355 unsigned int func;
356 int is_out;
357 int drive;
358 int pull;
359 u32 ctl_reg;
361 static const char * const pulls[] = {
362 "no pull",
363 "pull down",
364 "keeper",
365 "pull up"
368 pctldev = pctldev ? : state->ctrl;
369 pindesc = pctldev->desc->pins[offset];
370 ctl_reg = lpi_gpio_read(state, offset, LPI_GPIO_CFG_REG);
371 is_out = ctl_reg & LPI_GPIO_OE_MASK;
373 func = FIELD_GET(LPI_GPIO_FUNCTION_MASK, ctl_reg);
374 drive = FIELD_GET(LPI_GPIO_OUT_STRENGTH_MASK, ctl_reg);
375 pull = FIELD_GET(LPI_GPIO_PULL_MASK, ctl_reg);
377 seq_printf(s, " %-8s: %-3s %d", pindesc.name, is_out ? "out" : "in", func);
378 seq_printf(s, " %dmA", lpi_regval_to_drive(drive));
379 seq_printf(s, " %s", pulls[pull]);
382 static void lpi_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
384 unsigned int gpio = chip->base;
385 unsigned int i;
387 for (i = 0; i < chip->ngpio; i++, gpio++) {
388 lpi_gpio_dbg_show_one(s, NULL, chip, i, gpio);
389 seq_puts(s, "\n");
393 #else
394 #define lpi_gpio_dbg_show NULL
395 #endif
397 static const struct gpio_chip lpi_gpio_template = {
398 .direction_input = lpi_gpio_direction_input,
399 .direction_output = lpi_gpio_direction_output,
400 .get = lpi_gpio_get,
401 .set = lpi_gpio_set,
402 .request = gpiochip_generic_request,
403 .free = gpiochip_generic_free,
404 .dbg_show = lpi_gpio_dbg_show,
407 static int lpi_build_pin_desc_groups(struct lpi_pinctrl *pctrl)
409 int i, ret;
411 for (i = 0; i < pctrl->data->npins; i++) {
412 const struct pinctrl_pin_desc *pin_info = pctrl->desc.pins + i;
414 ret = pinctrl_generic_add_group(pctrl->ctrl, pin_info->name,
415 (int *)&pin_info->number, 1, NULL);
416 if (ret < 0)
417 goto err_pinctrl;
420 return 0;
422 err_pinctrl:
423 for (; i > 0; i--)
424 pinctrl_generic_remove_group(pctrl->ctrl, i - 1);
426 return ret;
429 int lpi_pinctrl_probe(struct platform_device *pdev)
431 const struct lpi_pinctrl_variant_data *data;
432 struct device *dev = &pdev->dev;
433 struct lpi_pinctrl *pctrl;
434 int ret;
436 pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
437 if (!pctrl)
438 return -ENOMEM;
440 platform_set_drvdata(pdev, pctrl);
442 data = of_device_get_match_data(dev);
443 if (!data)
444 return -EINVAL;
446 if (WARN_ON(data->npins > MAX_NR_GPIO))
447 return -EINVAL;
449 pctrl->data = data;
450 pctrl->dev = &pdev->dev;
452 pctrl->clks[0].id = "core";
453 pctrl->clks[1].id = "audio";
455 pctrl->tlmm_base = devm_platform_ioremap_resource(pdev, 0);
456 if (IS_ERR(pctrl->tlmm_base))
457 return dev_err_probe(dev, PTR_ERR(pctrl->tlmm_base),
458 "TLMM resource not provided\n");
460 if (!(data->flags & LPI_FLAG_SLEW_RATE_SAME_REG)) {
461 pctrl->slew_base = devm_platform_ioremap_resource(pdev, 1);
462 if (IS_ERR(pctrl->slew_base))
463 return dev_err_probe(dev, PTR_ERR(pctrl->slew_base),
464 "Slew resource not provided\n");
467 ret = devm_clk_bulk_get_optional(dev, MAX_LPI_NUM_CLKS, pctrl->clks);
468 if (ret)
469 return ret;
471 ret = clk_bulk_prepare_enable(MAX_LPI_NUM_CLKS, pctrl->clks);
472 if (ret)
473 return dev_err_probe(dev, ret, "Can't enable clocks\n");
475 pctrl->desc.pctlops = &lpi_gpio_pinctrl_ops;
476 pctrl->desc.pmxops = &lpi_gpio_pinmux_ops;
477 pctrl->desc.confops = &lpi_gpio_pinconf_ops;
478 pctrl->desc.owner = THIS_MODULE;
479 pctrl->desc.name = dev_name(dev);
480 pctrl->desc.pins = data->pins;
481 pctrl->desc.npins = data->npins;
482 pctrl->chip = lpi_gpio_template;
483 pctrl->chip.parent = dev;
484 pctrl->chip.base = -1;
485 pctrl->chip.ngpio = data->npins;
486 pctrl->chip.label = dev_name(dev);
487 pctrl->chip.can_sleep = false;
489 mutex_init(&pctrl->lock);
491 pctrl->ctrl = devm_pinctrl_register(dev, &pctrl->desc, pctrl);
492 if (IS_ERR(pctrl->ctrl)) {
493 ret = PTR_ERR(pctrl->ctrl);
494 dev_err(dev, "failed to add pin controller\n");
495 goto err_pinctrl;
498 ret = lpi_build_pin_desc_groups(pctrl);
499 if (ret)
500 goto err_pinctrl;
502 ret = devm_gpiochip_add_data(dev, &pctrl->chip, pctrl);
503 if (ret) {
504 dev_err(pctrl->dev, "can't add gpio chip\n");
505 goto err_pinctrl;
508 return 0;
510 err_pinctrl:
511 mutex_destroy(&pctrl->lock);
512 clk_bulk_disable_unprepare(MAX_LPI_NUM_CLKS, pctrl->clks);
514 return ret;
516 EXPORT_SYMBOL_GPL(lpi_pinctrl_probe);
518 void lpi_pinctrl_remove(struct platform_device *pdev)
520 struct lpi_pinctrl *pctrl = platform_get_drvdata(pdev);
521 int i;
523 mutex_destroy(&pctrl->lock);
524 clk_bulk_disable_unprepare(MAX_LPI_NUM_CLKS, pctrl->clks);
526 for (i = 0; i < pctrl->data->npins; i++)
527 pinctrl_generic_remove_group(pctrl->ctrl, i);
529 EXPORT_SYMBOL_GPL(lpi_pinctrl_remove);
531 MODULE_DESCRIPTION("QTI LPI GPIO pin control driver");
532 MODULE_LICENSE("GPL");