2 * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/gpio.h>
15 #include <linux/module.h>
17 #include <linux/of_irq.h>
18 #include <linux/pinctrl/pinconf-generic.h>
19 #include <linux/pinctrl/pinconf.h>
20 #include <linux/pinctrl/pinmux.h>
21 #include <linux/platform_device.h>
22 #include <linux/regmap.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
26 #include <dt-bindings/pinctrl/qcom,pmic-gpio.h>
29 #include "../pinctrl-utils.h"
31 #define PMIC_GPIO_ADDRESS_RANGE 0x100
33 /* type and subtype registers base address offsets */
34 #define PMIC_GPIO_REG_TYPE 0x4
35 #define PMIC_GPIO_REG_SUBTYPE 0x5
37 /* GPIO peripheral type and subtype out_values */
38 #define PMIC_GPIO_TYPE 0x10
39 #define PMIC_GPIO_SUBTYPE_GPIO_4CH 0x1
40 #define PMIC_GPIO_SUBTYPE_GPIOC_4CH 0x5
41 #define PMIC_GPIO_SUBTYPE_GPIO_8CH 0x9
42 #define PMIC_GPIO_SUBTYPE_GPIOC_8CH 0xd
44 #define PMIC_MPP_REG_RT_STS 0x10
45 #define PMIC_MPP_REG_RT_STS_VAL_MASK 0x1
47 /* control register base address offsets */
48 #define PMIC_GPIO_REG_MODE_CTL 0x40
49 #define PMIC_GPIO_REG_DIG_VIN_CTL 0x41
50 #define PMIC_GPIO_REG_DIG_PULL_CTL 0x42
51 #define PMIC_GPIO_REG_DIG_OUT_CTL 0x45
52 #define PMIC_GPIO_REG_EN_CTL 0x46
54 /* PMIC_GPIO_REG_MODE_CTL */
55 #define PMIC_GPIO_REG_MODE_VALUE_SHIFT 0x1
56 #define PMIC_GPIO_REG_MODE_FUNCTION_SHIFT 1
57 #define PMIC_GPIO_REG_MODE_FUNCTION_MASK 0x7
58 #define PMIC_GPIO_REG_MODE_DIR_SHIFT 4
59 #define PMIC_GPIO_REG_MODE_DIR_MASK 0x7
61 /* PMIC_GPIO_REG_DIG_VIN_CTL */
62 #define PMIC_GPIO_REG_VIN_SHIFT 0
63 #define PMIC_GPIO_REG_VIN_MASK 0x7
65 /* PMIC_GPIO_REG_DIG_PULL_CTL */
66 #define PMIC_GPIO_REG_PULL_SHIFT 0
67 #define PMIC_GPIO_REG_PULL_MASK 0x7
69 #define PMIC_GPIO_PULL_DOWN 4
70 #define PMIC_GPIO_PULL_DISABLE 5
72 /* PMIC_GPIO_REG_DIG_OUT_CTL */
73 #define PMIC_GPIO_REG_OUT_STRENGTH_SHIFT 0
74 #define PMIC_GPIO_REG_OUT_STRENGTH_MASK 0x3
75 #define PMIC_GPIO_REG_OUT_TYPE_SHIFT 4
76 #define PMIC_GPIO_REG_OUT_TYPE_MASK 0x3
79 * Output type - indicates pin should be configured as push-pull,
80 * open drain or open source.
82 #define PMIC_GPIO_OUT_BUF_CMOS 0
83 #define PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS 1
84 #define PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS 2
86 /* PMIC_GPIO_REG_EN_CTL */
87 #define PMIC_GPIO_REG_MASTER_EN_SHIFT 7
89 #define PMIC_GPIO_PHYSICAL_OFFSET 1
91 /* Qualcomm specific pin configurations */
92 #define PMIC_GPIO_CONF_PULL_UP (PIN_CONFIG_END + 1)
93 #define PMIC_GPIO_CONF_STRENGTH (PIN_CONFIG_END + 2)
96 * struct pmic_gpio_pad - keep current GPIO settings
97 * @base: Address base in SPMI device.
98 * @irq: IRQ number which this GPIO generate.
99 * @is_enabled: Set to false when GPIO should be put in high Z state.
100 * @out_value: Cached pin output value
101 * @have_buffer: Set to true if GPIO output could be configured in push-pull,
102 * open-drain or open-source mode.
103 * @output_enabled: Set to true if GPIO output logic is enabled.
104 * @input_enabled: Set to true if GPIO input buffer logic is enabled.
105 * @num_sources: Number of power-sources supported by this GPIO.
106 * @power_source: Current power-source used.
107 * @buffer_type: Push-pull, open-drain or open-source.
108 * @pullup: Constant current which flow trough GPIO output buffer.
109 * @strength: No, Low, Medium, High
110 * @function: See pmic_gpio_functions[]
112 struct pmic_gpio_pad
{
120 unsigned int num_sources
;
121 unsigned int power_source
;
122 unsigned int buffer_type
;
124 unsigned int strength
;
125 unsigned int function
;
128 struct pmic_gpio_state
{
131 struct pinctrl_dev
*ctrl
;
132 struct gpio_chip chip
;
135 static const struct pinconf_generic_params pmic_gpio_bindings
[] = {
136 {"qcom,pull-up-strength", PMIC_GPIO_CONF_PULL_UP
, 0},
137 {"qcom,drive-strength", PMIC_GPIO_CONF_STRENGTH
, 0},
140 #ifdef CONFIG_DEBUG_FS
141 static const struct pin_config_item pmic_conf_items
[ARRAY_SIZE(pmic_gpio_bindings
)] = {
142 PCONFDUMP(PMIC_GPIO_CONF_PULL_UP
, "pull up strength", NULL
, true),
143 PCONFDUMP(PMIC_GPIO_CONF_STRENGTH
, "drive-strength", NULL
, true),
147 static const char *const pmic_gpio_groups
[] = {
148 "gpio1", "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7", "gpio8",
149 "gpio9", "gpio10", "gpio11", "gpio12", "gpio13", "gpio14", "gpio15",
150 "gpio16", "gpio17", "gpio18", "gpio19", "gpio20", "gpio21", "gpio22",
151 "gpio23", "gpio24", "gpio25", "gpio26", "gpio27", "gpio28", "gpio29",
152 "gpio30", "gpio31", "gpio32", "gpio33", "gpio34", "gpio35", "gpio36",
155 static const char *const pmic_gpio_functions
[] = {
156 PMIC_GPIO_FUNC_NORMAL
, PMIC_GPIO_FUNC_PAIRED
,
157 PMIC_GPIO_FUNC_FUNC1
, PMIC_GPIO_FUNC_FUNC2
,
158 PMIC_GPIO_FUNC_DTEST1
, PMIC_GPIO_FUNC_DTEST2
,
159 PMIC_GPIO_FUNC_DTEST3
, PMIC_GPIO_FUNC_DTEST4
,
162 static int pmic_gpio_read(struct pmic_gpio_state
*state
,
163 struct pmic_gpio_pad
*pad
, unsigned int addr
)
168 ret
= regmap_read(state
->map
, pad
->base
+ addr
, &val
);
170 dev_err(state
->dev
, "read 0x%x failed\n", addr
);
177 static int pmic_gpio_write(struct pmic_gpio_state
*state
,
178 struct pmic_gpio_pad
*pad
, unsigned int addr
,
183 ret
= regmap_write(state
->map
, pad
->base
+ addr
, val
);
185 dev_err(state
->dev
, "write 0x%x failed\n", addr
);
190 static int pmic_gpio_get_groups_count(struct pinctrl_dev
*pctldev
)
192 /* Every PIN is a group */
193 return pctldev
->desc
->npins
;
196 static const char *pmic_gpio_get_group_name(struct pinctrl_dev
*pctldev
,
199 return pctldev
->desc
->pins
[pin
].name
;
202 static int pmic_gpio_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned pin
,
203 const unsigned **pins
, unsigned *num_pins
)
205 *pins
= &pctldev
->desc
->pins
[pin
].number
;
210 static const struct pinctrl_ops pmic_gpio_pinctrl_ops
= {
211 .get_groups_count
= pmic_gpio_get_groups_count
,
212 .get_group_name
= pmic_gpio_get_group_name
,
213 .get_group_pins
= pmic_gpio_get_group_pins
,
214 .dt_node_to_map
= pinconf_generic_dt_node_to_map_group
,
215 .dt_free_map
= pinctrl_utils_dt_free_map
,
218 static int pmic_gpio_get_functions_count(struct pinctrl_dev
*pctldev
)
220 return ARRAY_SIZE(pmic_gpio_functions
);
223 static const char *pmic_gpio_get_function_name(struct pinctrl_dev
*pctldev
,
226 return pmic_gpio_functions
[function
];
229 static int pmic_gpio_get_function_groups(struct pinctrl_dev
*pctldev
,
231 const char *const **groups
,
232 unsigned *const num_qgroups
)
234 *groups
= pmic_gpio_groups
;
235 *num_qgroups
= pctldev
->desc
->npins
;
239 static int pmic_gpio_set_mux(struct pinctrl_dev
*pctldev
, unsigned function
,
242 struct pmic_gpio_state
*state
= pinctrl_dev_get_drvdata(pctldev
);
243 struct pmic_gpio_pad
*pad
;
247 pad
= pctldev
->desc
->pins
[pin
].drv_data
;
249 pad
->function
= function
;
252 if (pad
->output_enabled
) {
253 if (pad
->input_enabled
)
259 val
= val
<< PMIC_GPIO_REG_MODE_DIR_SHIFT
;
260 val
|= pad
->function
<< PMIC_GPIO_REG_MODE_FUNCTION_SHIFT
;
261 val
|= pad
->out_value
& PMIC_GPIO_REG_MODE_VALUE_SHIFT
;
263 ret
= pmic_gpio_write(state
, pad
, PMIC_GPIO_REG_MODE_CTL
, val
);
267 val
= pad
->is_enabled
<< PMIC_GPIO_REG_MASTER_EN_SHIFT
;
269 return pmic_gpio_write(state
, pad
, PMIC_GPIO_REG_EN_CTL
, val
);
272 static const struct pinmux_ops pmic_gpio_pinmux_ops
= {
273 .get_functions_count
= pmic_gpio_get_functions_count
,
274 .get_function_name
= pmic_gpio_get_function_name
,
275 .get_function_groups
= pmic_gpio_get_function_groups
,
276 .set_mux
= pmic_gpio_set_mux
,
279 static int pmic_gpio_config_get(struct pinctrl_dev
*pctldev
,
280 unsigned int pin
, unsigned long *config
)
282 unsigned param
= pinconf_to_config_param(*config
);
283 struct pmic_gpio_pad
*pad
;
286 pad
= pctldev
->desc
->pins
[pin
].drv_data
;
289 case PIN_CONFIG_DRIVE_PUSH_PULL
:
290 arg
= pad
->buffer_type
== PMIC_GPIO_OUT_BUF_CMOS
;
292 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
293 arg
= pad
->buffer_type
== PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS
;
295 case PIN_CONFIG_DRIVE_OPEN_SOURCE
:
296 arg
= pad
->buffer_type
== PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS
;
298 case PIN_CONFIG_BIAS_PULL_DOWN
:
299 arg
= pad
->pullup
== PMIC_GPIO_PULL_DOWN
;
301 case PIN_CONFIG_BIAS_DISABLE
:
302 arg
= pad
->pullup
= PMIC_GPIO_PULL_DISABLE
;
304 case PIN_CONFIG_BIAS_PULL_UP
:
305 arg
= pad
->pullup
== PMIC_GPIO_PULL_UP_30
;
307 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE
:
308 arg
= !pad
->is_enabled
;
310 case PIN_CONFIG_POWER_SOURCE
:
311 arg
= pad
->power_source
;
313 case PIN_CONFIG_INPUT_ENABLE
:
314 arg
= pad
->input_enabled
;
316 case PIN_CONFIG_OUTPUT
:
317 arg
= pad
->out_value
;
319 case PMIC_GPIO_CONF_PULL_UP
:
322 case PMIC_GPIO_CONF_STRENGTH
:
329 *config
= pinconf_to_config_packed(param
, arg
);
333 static int pmic_gpio_config_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
334 unsigned long *configs
, unsigned nconfs
)
336 struct pmic_gpio_state
*state
= pinctrl_dev_get_drvdata(pctldev
);
337 struct pmic_gpio_pad
*pad
;
342 pad
= pctldev
->desc
->pins
[pin
].drv_data
;
344 for (i
= 0; i
< nconfs
; i
++) {
345 param
= pinconf_to_config_param(configs
[i
]);
346 arg
= pinconf_to_config_argument(configs
[i
]);
349 case PIN_CONFIG_DRIVE_PUSH_PULL
:
350 pad
->buffer_type
= PMIC_GPIO_OUT_BUF_CMOS
;
352 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
353 if (!pad
->have_buffer
)
355 pad
->buffer_type
= PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS
;
357 case PIN_CONFIG_DRIVE_OPEN_SOURCE
:
358 if (!pad
->have_buffer
)
360 pad
->buffer_type
= PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS
;
362 case PIN_CONFIG_BIAS_DISABLE
:
363 pad
->pullup
= PMIC_GPIO_PULL_DISABLE
;
365 case PIN_CONFIG_BIAS_PULL_UP
:
366 pad
->pullup
= PMIC_GPIO_PULL_UP_30
;
368 case PIN_CONFIG_BIAS_PULL_DOWN
:
370 pad
->pullup
= PMIC_GPIO_PULL_DOWN
;
372 pad
->pullup
= PMIC_GPIO_PULL_DISABLE
;
374 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE
:
375 pad
->is_enabled
= false;
377 case PIN_CONFIG_POWER_SOURCE
:
378 if (arg
> pad
->num_sources
)
380 pad
->power_source
= arg
;
382 case PIN_CONFIG_INPUT_ENABLE
:
383 pad
->input_enabled
= arg
? true : false;
385 case PIN_CONFIG_OUTPUT
:
386 pad
->output_enabled
= true;
387 pad
->out_value
= arg
;
389 case PMIC_GPIO_CONF_PULL_UP
:
390 if (arg
> PMIC_GPIO_PULL_UP_1P5_30
)
394 case PMIC_GPIO_CONF_STRENGTH
:
395 if (arg
> PMIC_GPIO_STRENGTH_LOW
)
404 val
= pad
->power_source
<< PMIC_GPIO_REG_VIN_SHIFT
;
406 ret
= pmic_gpio_write(state
, pad
, PMIC_GPIO_REG_DIG_VIN_CTL
, val
);
410 val
= pad
->pullup
<< PMIC_GPIO_REG_PULL_SHIFT
;
412 ret
= pmic_gpio_write(state
, pad
, PMIC_GPIO_REG_DIG_PULL_CTL
, val
);
416 val
= pad
->buffer_type
<< PMIC_GPIO_REG_OUT_TYPE_SHIFT
;
417 val
|= pad
->strength
<< PMIC_GPIO_REG_OUT_STRENGTH_SHIFT
;
419 ret
= pmic_gpio_write(state
, pad
, PMIC_GPIO_REG_DIG_OUT_CTL
, val
);
424 if (pad
->output_enabled
) {
425 if (pad
->input_enabled
)
431 val
= val
<< PMIC_GPIO_REG_MODE_DIR_SHIFT
;
432 val
|= pad
->function
<< PMIC_GPIO_REG_MODE_FUNCTION_SHIFT
;
433 val
|= pad
->out_value
& PMIC_GPIO_REG_MODE_VALUE_SHIFT
;
435 return pmic_gpio_write(state
, pad
, PMIC_GPIO_REG_MODE_CTL
, val
);
438 static void pmic_gpio_config_dbg_show(struct pinctrl_dev
*pctldev
,
439 struct seq_file
*s
, unsigned pin
)
441 struct pmic_gpio_state
*state
= pinctrl_dev_get_drvdata(pctldev
);
442 struct pmic_gpio_pad
*pad
;
445 static const char *const biases
[] = {
446 "pull-up 30uA", "pull-up 1.5uA", "pull-up 31.5uA",
447 "pull-up 1.5uA + 30uA boost", "pull-down 10uA", "no pull"
449 static const char *const buffer_types
[] = {
450 "push-pull", "open-drain", "open-source"
452 static const char *const strengths
[] = {
453 "no", "high", "medium", "low"
456 pad
= pctldev
->desc
->pins
[pin
].drv_data
;
458 seq_printf(s
, " gpio%-2d:", pin
+ PMIC_GPIO_PHYSICAL_OFFSET
);
460 val
= pmic_gpio_read(state
, pad
, PMIC_GPIO_REG_EN_CTL
);
462 if (val
< 0 || !(val
>> PMIC_GPIO_REG_MASTER_EN_SHIFT
)) {
466 if (pad
->input_enabled
) {
467 ret
= pmic_gpio_read(state
, pad
, PMIC_MPP_REG_RT_STS
);
471 ret
&= PMIC_MPP_REG_RT_STS_VAL_MASK
;
472 pad
->out_value
= ret
;
475 seq_printf(s
, " %-4s", pad
->output_enabled
? "out" : "in");
476 seq_printf(s
, " %-7s", pmic_gpio_functions
[pad
->function
]);
477 seq_printf(s
, " vin-%d", pad
->power_source
);
478 seq_printf(s
, " %-27s", biases
[pad
->pullup
]);
479 seq_printf(s
, " %-10s", buffer_types
[pad
->buffer_type
]);
480 seq_printf(s
, " %-4s", pad
->out_value
? "high" : "low");
481 seq_printf(s
, " %-7s", strengths
[pad
->strength
]);
485 static const struct pinconf_ops pmic_gpio_pinconf_ops
= {
487 .pin_config_group_get
= pmic_gpio_config_get
,
488 .pin_config_group_set
= pmic_gpio_config_set
,
489 .pin_config_group_dbg_show
= pmic_gpio_config_dbg_show
,
492 static int pmic_gpio_direction_input(struct gpio_chip
*chip
, unsigned pin
)
494 struct pmic_gpio_state
*state
= gpiochip_get_data(chip
);
495 unsigned long config
;
497 config
= pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE
, 1);
499 return pmic_gpio_config_set(state
->ctrl
, pin
, &config
, 1);
502 static int pmic_gpio_direction_output(struct gpio_chip
*chip
,
503 unsigned pin
, int val
)
505 struct pmic_gpio_state
*state
= gpiochip_get_data(chip
);
506 unsigned long config
;
508 config
= pinconf_to_config_packed(PIN_CONFIG_OUTPUT
, val
);
510 return pmic_gpio_config_set(state
->ctrl
, pin
, &config
, 1);
513 static int pmic_gpio_get(struct gpio_chip
*chip
, unsigned pin
)
515 struct pmic_gpio_state
*state
= gpiochip_get_data(chip
);
516 struct pmic_gpio_pad
*pad
;
519 pad
= state
->ctrl
->desc
->pins
[pin
].drv_data
;
521 if (!pad
->is_enabled
)
524 if (pad
->input_enabled
) {
525 ret
= pmic_gpio_read(state
, pad
, PMIC_MPP_REG_RT_STS
);
529 pad
->out_value
= ret
& PMIC_MPP_REG_RT_STS_VAL_MASK
;
532 return !!pad
->out_value
;
535 static void pmic_gpio_set(struct gpio_chip
*chip
, unsigned pin
, int value
)
537 struct pmic_gpio_state
*state
= gpiochip_get_data(chip
);
538 unsigned long config
;
540 config
= pinconf_to_config_packed(PIN_CONFIG_OUTPUT
, value
);
542 pmic_gpio_config_set(state
->ctrl
, pin
, &config
, 1);
545 static int pmic_gpio_of_xlate(struct gpio_chip
*chip
,
546 const struct of_phandle_args
*gpio_desc
,
549 if (chip
->of_gpio_n_cells
< 2)
553 *flags
= gpio_desc
->args
[1];
555 return gpio_desc
->args
[0] - PMIC_GPIO_PHYSICAL_OFFSET
;
558 static int pmic_gpio_to_irq(struct gpio_chip
*chip
, unsigned pin
)
560 struct pmic_gpio_state
*state
= gpiochip_get_data(chip
);
561 struct pmic_gpio_pad
*pad
;
563 pad
= state
->ctrl
->desc
->pins
[pin
].drv_data
;
568 static void pmic_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
570 struct pmic_gpio_state
*state
= gpiochip_get_data(chip
);
573 for (i
= 0; i
< chip
->ngpio
; i
++) {
574 pmic_gpio_config_dbg_show(state
->ctrl
, s
, i
);
579 static const struct gpio_chip pmic_gpio_gpio_template
= {
580 .direction_input
= pmic_gpio_direction_input
,
581 .direction_output
= pmic_gpio_direction_output
,
582 .get
= pmic_gpio_get
,
583 .set
= pmic_gpio_set
,
584 .request
= gpiochip_generic_request
,
585 .free
= gpiochip_generic_free
,
586 .of_xlate
= pmic_gpio_of_xlate
,
587 .to_irq
= pmic_gpio_to_irq
,
588 .dbg_show
= pmic_gpio_dbg_show
,
591 static int pmic_gpio_populate(struct pmic_gpio_state
*state
,
592 struct pmic_gpio_pad
*pad
)
594 int type
, subtype
, val
, dir
;
596 type
= pmic_gpio_read(state
, pad
, PMIC_GPIO_REG_TYPE
);
600 if (type
!= PMIC_GPIO_TYPE
) {
601 dev_err(state
->dev
, "incorrect block type 0x%x at 0x%x\n",
606 subtype
= pmic_gpio_read(state
, pad
, PMIC_GPIO_REG_SUBTYPE
);
611 case PMIC_GPIO_SUBTYPE_GPIO_4CH
:
612 pad
->have_buffer
= true;
613 case PMIC_GPIO_SUBTYPE_GPIOC_4CH
:
614 pad
->num_sources
= 4;
616 case PMIC_GPIO_SUBTYPE_GPIO_8CH
:
617 pad
->have_buffer
= true;
618 case PMIC_GPIO_SUBTYPE_GPIOC_8CH
:
619 pad
->num_sources
= 8;
622 dev_err(state
->dev
, "unknown GPIO type 0x%x\n", subtype
);
626 val
= pmic_gpio_read(state
, pad
, PMIC_GPIO_REG_MODE_CTL
);
630 pad
->out_value
= val
& PMIC_GPIO_REG_MODE_VALUE_SHIFT
;
632 dir
= val
>> PMIC_GPIO_REG_MODE_DIR_SHIFT
;
633 dir
&= PMIC_GPIO_REG_MODE_DIR_MASK
;
636 pad
->input_enabled
= true;
637 pad
->output_enabled
= false;
640 pad
->input_enabled
= false;
641 pad
->output_enabled
= true;
644 pad
->input_enabled
= true;
645 pad
->output_enabled
= true;
648 dev_err(state
->dev
, "unknown GPIO direction\n");
652 pad
->function
= val
>> PMIC_GPIO_REG_MODE_FUNCTION_SHIFT
;
653 pad
->function
&= PMIC_GPIO_REG_MODE_FUNCTION_MASK
;
655 val
= pmic_gpio_read(state
, pad
, PMIC_GPIO_REG_DIG_VIN_CTL
);
659 pad
->power_source
= val
>> PMIC_GPIO_REG_VIN_SHIFT
;
660 pad
->power_source
&= PMIC_GPIO_REG_VIN_MASK
;
662 val
= pmic_gpio_read(state
, pad
, PMIC_GPIO_REG_DIG_PULL_CTL
);
666 pad
->pullup
= val
>> PMIC_GPIO_REG_PULL_SHIFT
;
667 pad
->pullup
&= PMIC_GPIO_REG_PULL_MASK
;
669 val
= pmic_gpio_read(state
, pad
, PMIC_GPIO_REG_DIG_OUT_CTL
);
673 pad
->strength
= val
>> PMIC_GPIO_REG_OUT_STRENGTH_SHIFT
;
674 pad
->strength
&= PMIC_GPIO_REG_OUT_STRENGTH_MASK
;
676 pad
->buffer_type
= val
>> PMIC_GPIO_REG_OUT_TYPE_SHIFT
;
677 pad
->buffer_type
&= PMIC_GPIO_REG_OUT_TYPE_MASK
;
679 /* Pin could be disabled with PIN_CONFIG_BIAS_HIGH_IMPEDANCE */
680 pad
->is_enabled
= true;
684 static int pmic_gpio_probe(struct platform_device
*pdev
)
686 struct device
*dev
= &pdev
->dev
;
687 struct pinctrl_pin_desc
*pindesc
;
688 struct pinctrl_desc
*pctrldesc
;
689 struct pmic_gpio_pad
*pad
, *pads
;
690 struct pmic_gpio_state
*state
;
694 ret
= of_property_read_u32(dev
->of_node
, "reg", ®
);
696 dev_err(dev
, "missing base address");
700 npins
= platform_irq_count(pdev
);
706 BUG_ON(npins
> ARRAY_SIZE(pmic_gpio_groups
));
708 state
= devm_kzalloc(dev
, sizeof(*state
), GFP_KERNEL
);
712 platform_set_drvdata(pdev
, state
);
714 state
->dev
= &pdev
->dev
;
715 state
->map
= dev_get_regmap(dev
->parent
, NULL
);
717 pindesc
= devm_kcalloc(dev
, npins
, sizeof(*pindesc
), GFP_KERNEL
);
721 pads
= devm_kcalloc(dev
, npins
, sizeof(*pads
), GFP_KERNEL
);
725 pctrldesc
= devm_kzalloc(dev
, sizeof(*pctrldesc
), GFP_KERNEL
);
729 pctrldesc
->pctlops
= &pmic_gpio_pinctrl_ops
;
730 pctrldesc
->pmxops
= &pmic_gpio_pinmux_ops
;
731 pctrldesc
->confops
= &pmic_gpio_pinconf_ops
;
732 pctrldesc
->owner
= THIS_MODULE
;
733 pctrldesc
->name
= dev_name(dev
);
734 pctrldesc
->pins
= pindesc
;
735 pctrldesc
->npins
= npins
;
736 pctrldesc
->num_custom_params
= ARRAY_SIZE(pmic_gpio_bindings
);
737 pctrldesc
->custom_params
= pmic_gpio_bindings
;
738 #ifdef CONFIG_DEBUG_FS
739 pctrldesc
->custom_conf_items
= pmic_conf_items
;
742 for (i
= 0; i
< npins
; i
++, pindesc
++) {
744 pindesc
->drv_data
= pad
;
746 pindesc
->name
= pmic_gpio_groups
[i
];
748 pad
->irq
= platform_get_irq(pdev
, i
);
752 pad
->base
= reg
+ i
* PMIC_GPIO_ADDRESS_RANGE
;
754 ret
= pmic_gpio_populate(state
, pad
);
759 state
->chip
= pmic_gpio_gpio_template
;
760 state
->chip
.parent
= dev
;
761 state
->chip
.base
= -1;
762 state
->chip
.ngpio
= npins
;
763 state
->chip
.label
= dev_name(dev
);
764 state
->chip
.of_gpio_n_cells
= 2;
765 state
->chip
.can_sleep
= false;
767 state
->ctrl
= pinctrl_register(pctrldesc
, dev
, state
);
768 if (IS_ERR(state
->ctrl
))
769 return PTR_ERR(state
->ctrl
);
771 ret
= gpiochip_add_data(&state
->chip
, state
);
773 dev_err(state
->dev
, "can't add gpio chip\n");
777 ret
= gpiochip_add_pin_range(&state
->chip
, dev_name(dev
), 0, 0, npins
);
779 dev_err(dev
, "failed to add pin range\n");
786 gpiochip_remove(&state
->chip
);
788 pinctrl_unregister(state
->ctrl
);
792 static int pmic_gpio_remove(struct platform_device
*pdev
)
794 struct pmic_gpio_state
*state
= platform_get_drvdata(pdev
);
796 gpiochip_remove(&state
->chip
);
797 pinctrl_unregister(state
->ctrl
);
801 static const struct of_device_id pmic_gpio_of_match
[] = {
802 { .compatible
= "qcom,pm8916-gpio" }, /* 4 GPIO's */
803 { .compatible
= "qcom,pm8941-gpio" }, /* 36 GPIO's */
804 { .compatible
= "qcom,pm8994-gpio" }, /* 22 GPIO's */
805 { .compatible
= "qcom,pma8084-gpio" }, /* 22 GPIO's */
809 MODULE_DEVICE_TABLE(of
, pmic_gpio_of_match
);
811 static struct platform_driver pmic_gpio_driver
= {
813 .name
= "qcom-spmi-gpio",
814 .of_match_table
= pmic_gpio_of_match
,
816 .probe
= pmic_gpio_probe
,
817 .remove
= pmic_gpio_remove
,
820 module_platform_driver(pmic_gpio_driver
);
822 MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");
823 MODULE_DESCRIPTION("Qualcomm SPMI PMIC GPIO pin control driver");
824 MODULE_ALIAS("platform:qcom-spmi-gpio");
825 MODULE_LICENSE("GPL v2");