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/pinctrl/pinconf-generic.h>
18 #include <linux/pinctrl/pinconf.h>
19 #include <linux/pinctrl/pinmux.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
25 #include <dt-bindings/pinctrl/qcom,pmic-mpp.h>
28 #include "../pinctrl-utils.h"
30 #define PMIC_MPP_ADDRESS_RANGE 0x100
33 * Pull Up Values - it indicates whether a pull-up should be
34 * applied for bidirectional mode only. The hardware ignores the
35 * configuration when operating in other modes.
37 #define PMIC_MPP_PULL_UP_0P6KOHM 0
38 #define PMIC_MPP_PULL_UP_10KOHM 1
39 #define PMIC_MPP_PULL_UP_30KOHM 2
40 #define PMIC_MPP_PULL_UP_OPEN 3
42 /* type registers base address bases */
43 #define PMIC_MPP_REG_TYPE 0x4
44 #define PMIC_MPP_REG_SUBTYPE 0x5
46 /* mpp peripheral type and subtype values */
47 #define PMIC_MPP_TYPE 0x11
48 #define PMIC_MPP_SUBTYPE_4CH_NO_ANA_OUT 0x3
49 #define PMIC_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT 0x4
50 #define PMIC_MPP_SUBTYPE_4CH_NO_SINK 0x5
51 #define PMIC_MPP_SUBTYPE_ULT_4CH_NO_SINK 0x6
52 #define PMIC_MPP_SUBTYPE_4CH_FULL_FUNC 0x7
53 #define PMIC_MPP_SUBTYPE_8CH_FULL_FUNC 0xf
55 #define PMIC_MPP_REG_RT_STS 0x10
56 #define PMIC_MPP_REG_RT_STS_VAL_MASK 0x1
58 /* control register base address bases */
59 #define PMIC_MPP_REG_MODE_CTL 0x40
60 #define PMIC_MPP_REG_DIG_VIN_CTL 0x41
61 #define PMIC_MPP_REG_DIG_PULL_CTL 0x42
62 #define PMIC_MPP_REG_DIG_IN_CTL 0x43
63 #define PMIC_MPP_REG_EN_CTL 0x46
64 #define PMIC_MPP_REG_AIN_CTL 0x4a
66 /* PMIC_MPP_REG_MODE_CTL */
67 #define PMIC_MPP_REG_MODE_VALUE_MASK 0x1
68 #define PMIC_MPP_REG_MODE_FUNCTION_SHIFT 1
69 #define PMIC_MPP_REG_MODE_FUNCTION_MASK 0x7
70 #define PMIC_MPP_REG_MODE_DIR_SHIFT 4
71 #define PMIC_MPP_REG_MODE_DIR_MASK 0x7
73 /* PMIC_MPP_REG_DIG_VIN_CTL */
74 #define PMIC_MPP_REG_VIN_SHIFT 0
75 #define PMIC_MPP_REG_VIN_MASK 0x7
77 /* PMIC_MPP_REG_DIG_PULL_CTL */
78 #define PMIC_MPP_REG_PULL_SHIFT 0
79 #define PMIC_MPP_REG_PULL_MASK 0x7
81 /* PMIC_MPP_REG_EN_CTL */
82 #define PMIC_MPP_REG_MASTER_EN_SHIFT 7
84 /* PMIC_MPP_REG_AIN_CTL */
85 #define PMIC_MPP_REG_AIN_ROUTE_SHIFT 0
86 #define PMIC_MPP_REG_AIN_ROUTE_MASK 0x7
88 #define PMIC_MPP_PHYSICAL_OFFSET 1
90 /* Qualcomm specific pin configurations */
91 #define PMIC_MPP_CONF_AMUX_ROUTE (PIN_CONFIG_END + 1)
92 #define PMIC_MPP_CONF_ANALOG_MODE (PIN_CONFIG_END + 2)
95 * struct pmic_mpp_pad - keep current MPP settings
96 * @base: Address base in SPMI device.
97 * @irq: IRQ number which this MPP generate.
98 * @is_enabled: Set to false when MPP should be put in high Z state.
99 * @out_value: Cached pin output value.
100 * @output_enabled: Set to true if MPP output logic is enabled.
101 * @input_enabled: Set to true if MPP input buffer logic is enabled.
102 * @analog_mode: Set to true when MPP should operate in Analog Input, Analog
103 * Output or Bidirectional Analog mode.
104 * @num_sources: Number of power-sources supported by this MPP.
105 * @power_source: Current power-source used.
106 * @amux_input: Set the source for analog input.
107 * @pullup: Pullup resistor value. Valid in Bidirectional mode only.
108 * @function: See pmic_mpp_functions[].
110 struct pmic_mpp_pad
{
118 unsigned int num_sources
;
119 unsigned int power_source
;
120 unsigned int amux_input
;
122 unsigned int function
;
125 struct pmic_mpp_state
{
128 struct pinctrl_dev
*ctrl
;
129 struct gpio_chip chip
;
132 struct pmic_mpp_bindings
{
133 const char *property
;
137 static struct pmic_mpp_bindings pmic_mpp_bindings
[] = {
138 {"qcom,amux-route", PMIC_MPP_CONF_AMUX_ROUTE
},
139 {"qcom,analog-mode", PMIC_MPP_CONF_ANALOG_MODE
},
142 static const char *const pmic_mpp_groups
[] = {
143 "mpp1", "mpp2", "mpp3", "mpp4", "mpp5", "mpp6", "mpp7", "mpp8",
146 static const char *const pmic_mpp_functions
[] = {
147 PMIC_MPP_FUNC_NORMAL
, PMIC_MPP_FUNC_PAIRED
,
148 "reserved1", "reserved2",
149 PMIC_MPP_FUNC_DTEST1
, PMIC_MPP_FUNC_DTEST2
,
150 PMIC_MPP_FUNC_DTEST3
, PMIC_MPP_FUNC_DTEST4
,
153 static inline struct pmic_mpp_state
*to_mpp_state(struct gpio_chip
*chip
)
155 return container_of(chip
, struct pmic_mpp_state
, chip
);
158 static int pmic_mpp_read(struct pmic_mpp_state
*state
,
159 struct pmic_mpp_pad
*pad
, unsigned int addr
)
164 ret
= regmap_read(state
->map
, pad
->base
+ addr
, &val
);
166 dev_err(state
->dev
, "read 0x%x failed\n", addr
);
173 static int pmic_mpp_write(struct pmic_mpp_state
*state
,
174 struct pmic_mpp_pad
*pad
, unsigned int addr
,
179 ret
= regmap_write(state
->map
, pad
->base
+ addr
, val
);
181 dev_err(state
->dev
, "write 0x%x failed\n", addr
);
186 static int pmic_mpp_get_groups_count(struct pinctrl_dev
*pctldev
)
188 /* Every PIN is a group */
189 return pctldev
->desc
->npins
;
192 static const char *pmic_mpp_get_group_name(struct pinctrl_dev
*pctldev
,
195 return pctldev
->desc
->pins
[pin
].name
;
198 static int pmic_mpp_get_group_pins(struct pinctrl_dev
*pctldev
,
200 const unsigned **pins
, unsigned *num_pins
)
202 *pins
= &pctldev
->desc
->pins
[pin
].number
;
207 static int pmic_mpp_parse_dt_config(struct device_node
*np
,
208 struct pinctrl_dev
*pctldev
,
209 unsigned long **configs
,
210 unsigned int *nconfs
)
212 struct pmic_mpp_bindings
*par
;
217 for (i
= 0; i
< ARRAY_SIZE(pmic_mpp_bindings
); i
++) {
218 par
= &pmic_mpp_bindings
[i
];
219 ret
= of_property_read_u32(np
, par
->property
, &val
);
221 /* property not found */
225 /* use zero as default value, when no value is specified */
229 dev_dbg(pctldev
->dev
, "found %s with value %u\n",
232 cfg
= pinconf_to_config_packed(par
->param
, val
);
234 ret
= pinctrl_utils_add_config(pctldev
, configs
, nconfs
, cfg
);
242 static int pmic_mpp_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
243 struct device_node
*np
,
244 struct pinctrl_map
**map
,
245 unsigned *reserv
, unsigned *nmaps
,
246 enum pinctrl_map_type type
)
248 unsigned long *configs
= NULL
;
250 struct property
*prop
;
254 ret
= pmic_mpp_parse_dt_config(np
, pctldev
, &configs
, &nconfs
);
261 ret
= of_property_count_strings(np
, "pins");
265 ret
= pinctrl_utils_reserve_map(pctldev
, map
, reserv
, nmaps
, ret
);
269 of_property_for_each_string(np
, "pins", prop
, group
) {
270 ret
= pinctrl_utils_add_map_configs(pctldev
, map
,
271 reserv
, nmaps
, group
,
272 configs
, nconfs
, type
);
281 static int pmic_mpp_dt_node_to_map(struct pinctrl_dev
*pctldev
,
282 struct device_node
*np_config
,
283 struct pinctrl_map
**map
, unsigned *nmaps
)
285 struct device_node
*np
;
286 enum pinctrl_map_type type
;
294 type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
296 for_each_child_of_node(np_config
, np
) {
297 ret
= pinconf_generic_dt_subnode_to_map(pctldev
, np
, map
,
298 &reserv
, nmaps
, type
);
302 ret
= pmic_mpp_dt_subnode_to_map(pctldev
, np
, map
, &reserv
,
309 pinctrl_utils_dt_free_map(pctldev
, *map
, *nmaps
);
314 static const struct pinctrl_ops pmic_mpp_pinctrl_ops
= {
315 .get_groups_count
= pmic_mpp_get_groups_count
,
316 .get_group_name
= pmic_mpp_get_group_name
,
317 .get_group_pins
= pmic_mpp_get_group_pins
,
318 .dt_node_to_map
= pmic_mpp_dt_node_to_map
,
319 .dt_free_map
= pinctrl_utils_dt_free_map
,
322 static int pmic_mpp_get_functions_count(struct pinctrl_dev
*pctldev
)
324 return ARRAY_SIZE(pmic_mpp_functions
);
327 static const char *pmic_mpp_get_function_name(struct pinctrl_dev
*pctldev
,
330 return pmic_mpp_functions
[function
];
333 static int pmic_mpp_get_function_groups(struct pinctrl_dev
*pctldev
,
335 const char *const **groups
,
336 unsigned *const num_qgroups
)
338 *groups
= pmic_mpp_groups
;
339 *num_qgroups
= pctldev
->desc
->npins
;
343 static int pmic_mpp_set_mux(struct pinctrl_dev
*pctldev
, unsigned function
,
346 struct pmic_mpp_state
*state
= pinctrl_dev_get_drvdata(pctldev
);
347 struct pmic_mpp_pad
*pad
;
351 pad
= pctldev
->desc
->pins
[pin
].drv_data
;
353 pad
->function
= function
;
355 if (!pad
->analog_mode
) {
356 val
= 0; /* just digital input */
357 if (pad
->output_enabled
) {
358 if (pad
->input_enabled
)
359 val
= 2; /* digital input and output */
361 val
= 1; /* just digital output */
364 val
= 4; /* just analog input */
365 if (pad
->output_enabled
) {
366 if (pad
->input_enabled
)
367 val
= 3; /* analog input and output */
369 val
= 5; /* just analog output */
373 val
= val
<< PMIC_MPP_REG_MODE_DIR_SHIFT
;
374 val
|= pad
->function
<< PMIC_MPP_REG_MODE_FUNCTION_SHIFT
;
375 val
|= pad
->out_value
& PMIC_MPP_REG_MODE_VALUE_MASK
;
377 ret
= pmic_mpp_write(state
, pad
, PMIC_MPP_REG_MODE_CTL
, val
);
381 val
= pad
->is_enabled
<< PMIC_MPP_REG_MASTER_EN_SHIFT
;
383 return pmic_mpp_write(state
, pad
, PMIC_MPP_REG_EN_CTL
, val
);
386 static const struct pinmux_ops pmic_mpp_pinmux_ops
= {
387 .get_functions_count
= pmic_mpp_get_functions_count
,
388 .get_function_name
= pmic_mpp_get_function_name
,
389 .get_function_groups
= pmic_mpp_get_function_groups
,
390 .set_mux
= pmic_mpp_set_mux
,
393 static int pmic_mpp_config_get(struct pinctrl_dev
*pctldev
,
394 unsigned int pin
, unsigned long *config
)
396 unsigned param
= pinconf_to_config_param(*config
);
397 struct pmic_mpp_pad
*pad
;
400 pad
= pctldev
->desc
->pins
[pin
].drv_data
;
403 case PIN_CONFIG_BIAS_DISABLE
:
404 arg
= pad
->pullup
== PMIC_MPP_PULL_UP_OPEN
;
406 case PIN_CONFIG_BIAS_PULL_UP
:
407 switch (pad
->pullup
) {
408 case PMIC_MPP_PULL_UP_OPEN
:
411 case PMIC_MPP_PULL_UP_0P6KOHM
:
414 case PMIC_MPP_PULL_UP_10KOHM
:
417 case PMIC_MPP_PULL_UP_30KOHM
:
424 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE
:
425 arg
= !pad
->is_enabled
;
427 case PIN_CONFIG_POWER_SOURCE
:
428 arg
= pad
->power_source
;
430 case PIN_CONFIG_INPUT_ENABLE
:
431 arg
= pad
->input_enabled
;
433 case PIN_CONFIG_OUTPUT
:
434 arg
= pad
->out_value
;
436 case PMIC_MPP_CONF_AMUX_ROUTE
:
437 arg
= pad
->amux_input
;
439 case PMIC_MPP_CONF_ANALOG_MODE
:
440 arg
= pad
->analog_mode
;
446 /* Convert register value to pinconf value */
447 *config
= pinconf_to_config_packed(param
, arg
);
451 static int pmic_mpp_config_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
452 unsigned long *configs
, unsigned nconfs
)
454 struct pmic_mpp_state
*state
= pinctrl_dev_get_drvdata(pctldev
);
455 struct pmic_mpp_pad
*pad
;
460 pad
= pctldev
->desc
->pins
[pin
].drv_data
;
462 for (i
= 0; i
< nconfs
; i
++) {
463 param
= pinconf_to_config_param(configs
[i
]);
464 arg
= pinconf_to_config_argument(configs
[i
]);
467 case PIN_CONFIG_BIAS_DISABLE
:
468 pad
->pullup
= PMIC_MPP_PULL_UP_OPEN
;
470 case PIN_CONFIG_BIAS_PULL_UP
:
473 pad
->pullup
= PMIC_MPP_PULL_UP_0P6KOHM
;
476 pad
->pullup
= PMIC_MPP_PULL_UP_10KOHM
;
479 pad
->pullup
= PMIC_MPP_PULL_UP_30KOHM
;
485 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE
:
486 pad
->is_enabled
= false;
488 case PIN_CONFIG_POWER_SOURCE
:
489 if (arg
>= pad
->num_sources
)
491 pad
->power_source
= arg
;
493 case PIN_CONFIG_INPUT_ENABLE
:
494 pad
->input_enabled
= arg
? true : false;
496 case PIN_CONFIG_OUTPUT
:
497 pad
->output_enabled
= true;
498 pad
->out_value
= arg
;
500 case PMIC_MPP_CONF_AMUX_ROUTE
:
501 if (arg
>= PMIC_MPP_AMUX_ROUTE_ABUS4
)
503 pad
->amux_input
= arg
;
505 case PMIC_MPP_CONF_ANALOG_MODE
:
506 pad
->analog_mode
= true;
513 val
= pad
->power_source
<< PMIC_MPP_REG_VIN_SHIFT
;
515 ret
= pmic_mpp_write(state
, pad
, PMIC_MPP_REG_DIG_VIN_CTL
, val
);
519 val
= pad
->pullup
<< PMIC_MPP_REG_PULL_SHIFT
;
521 ret
= pmic_mpp_write(state
, pad
, PMIC_MPP_REG_DIG_PULL_CTL
, val
);
525 val
= pad
->amux_input
& PMIC_MPP_REG_AIN_ROUTE_MASK
;
527 ret
= pmic_mpp_write(state
, pad
, PMIC_MPP_REG_AIN_CTL
, val
);
531 if (!pad
->analog_mode
) {
532 val
= 0; /* just digital input */
533 if (pad
->output_enabled
) {
534 if (pad
->input_enabled
)
535 val
= 2; /* digital input and output */
537 val
= 1; /* just digital output */
540 val
= 4; /* just analog input */
541 if (pad
->output_enabled
) {
542 if (pad
->input_enabled
)
543 val
= 3; /* analog input and output */
545 val
= 5; /* just analog output */
549 val
= val
<< PMIC_MPP_REG_MODE_DIR_SHIFT
;
550 val
|= pad
->function
<< PMIC_MPP_REG_MODE_FUNCTION_SHIFT
;
551 val
|= pad
->out_value
& PMIC_MPP_REG_MODE_VALUE_MASK
;
553 return pmic_mpp_write(state
, pad
, PMIC_MPP_REG_MODE_CTL
, val
);
556 static void pmic_mpp_config_dbg_show(struct pinctrl_dev
*pctldev
,
557 struct seq_file
*s
, unsigned pin
)
559 struct pmic_mpp_state
*state
= pinctrl_dev_get_drvdata(pctldev
);
560 struct pmic_mpp_pad
*pad
;
563 static const char *const biases
[] = {
564 "0.6kOhm", "10kOhm", "30kOhm", "Disabled"
568 pad
= pctldev
->desc
->pins
[pin
].drv_data
;
570 seq_printf(s
, " mpp%-2d:", pin
+ PMIC_MPP_PHYSICAL_OFFSET
);
572 val
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_EN_CTL
);
574 if (val
< 0 || !(val
>> PMIC_MPP_REG_MASTER_EN_SHIFT
)) {
578 if (pad
->input_enabled
) {
579 ret
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_RT_STS
);
583 ret
&= PMIC_MPP_REG_RT_STS_VAL_MASK
;
584 pad
->out_value
= ret
;
587 seq_printf(s
, " %-4s", pad
->output_enabled
? "out" : "in");
588 seq_printf(s
, " %-4s", pad
->analog_mode
? "ana" : "dig");
589 seq_printf(s
, " %-7s", pmic_mpp_functions
[pad
->function
]);
590 seq_printf(s
, " vin-%d", pad
->power_source
);
591 seq_printf(s
, " %-8s", biases
[pad
->pullup
]);
592 seq_printf(s
, " %-4s", pad
->out_value
? "high" : "low");
596 static const struct pinconf_ops pmic_mpp_pinconf_ops
= {
597 .pin_config_group_get
= pmic_mpp_config_get
,
598 .pin_config_group_set
= pmic_mpp_config_set
,
599 .pin_config_group_dbg_show
= pmic_mpp_config_dbg_show
,
602 static int pmic_mpp_direction_input(struct gpio_chip
*chip
, unsigned pin
)
604 struct pmic_mpp_state
*state
= to_mpp_state(chip
);
605 unsigned long config
;
607 config
= pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE
, 1);
609 return pmic_mpp_config_set(state
->ctrl
, pin
, &config
, 1);
612 static int pmic_mpp_direction_output(struct gpio_chip
*chip
,
613 unsigned pin
, int val
)
615 struct pmic_mpp_state
*state
= to_mpp_state(chip
);
616 unsigned long config
;
618 config
= pinconf_to_config_packed(PIN_CONFIG_OUTPUT
, val
);
620 return pmic_mpp_config_set(state
->ctrl
, pin
, &config
, 1);
623 static int pmic_mpp_get(struct gpio_chip
*chip
, unsigned pin
)
625 struct pmic_mpp_state
*state
= to_mpp_state(chip
);
626 struct pmic_mpp_pad
*pad
;
629 pad
= state
->ctrl
->desc
->pins
[pin
].drv_data
;
631 if (pad
->input_enabled
) {
632 ret
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_RT_STS
);
636 pad
->out_value
= ret
& PMIC_MPP_REG_RT_STS_VAL_MASK
;
639 return pad
->out_value
;
642 static void pmic_mpp_set(struct gpio_chip
*chip
, unsigned pin
, int value
)
644 struct pmic_mpp_state
*state
= to_mpp_state(chip
);
645 unsigned long config
;
647 config
= pinconf_to_config_packed(PIN_CONFIG_OUTPUT
, value
);
649 pmic_mpp_config_set(state
->ctrl
, pin
, &config
, 1);
652 static int pmic_mpp_request(struct gpio_chip
*chip
, unsigned base
)
654 return pinctrl_request_gpio(chip
->base
+ base
);
657 static void pmic_mpp_free(struct gpio_chip
*chip
, unsigned base
)
659 pinctrl_free_gpio(chip
->base
+ base
);
662 static int pmic_mpp_of_xlate(struct gpio_chip
*chip
,
663 const struct of_phandle_args
*gpio_desc
,
666 if (chip
->of_gpio_n_cells
< 2)
670 *flags
= gpio_desc
->args
[1];
672 return gpio_desc
->args
[0] - PMIC_MPP_PHYSICAL_OFFSET
;
675 static int pmic_mpp_to_irq(struct gpio_chip
*chip
, unsigned pin
)
677 struct pmic_mpp_state
*state
= to_mpp_state(chip
);
678 struct pmic_mpp_pad
*pad
;
680 pad
= state
->ctrl
->desc
->pins
[pin
].drv_data
;
685 static void pmic_mpp_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
687 struct pmic_mpp_state
*state
= to_mpp_state(chip
);
690 for (i
= 0; i
< chip
->ngpio
; i
++) {
691 pmic_mpp_config_dbg_show(state
->ctrl
, s
, i
);
696 static const struct gpio_chip pmic_mpp_gpio_template
= {
697 .direction_input
= pmic_mpp_direction_input
,
698 .direction_output
= pmic_mpp_direction_output
,
701 .request
= pmic_mpp_request
,
702 .free
= pmic_mpp_free
,
703 .of_xlate
= pmic_mpp_of_xlate
,
704 .to_irq
= pmic_mpp_to_irq
,
705 .dbg_show
= pmic_mpp_dbg_show
,
708 static int pmic_mpp_populate(struct pmic_mpp_state
*state
,
709 struct pmic_mpp_pad
*pad
)
711 int type
, subtype
, val
, dir
;
713 type
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_TYPE
);
717 if (type
!= PMIC_MPP_TYPE
) {
718 dev_err(state
->dev
, "incorrect block type 0x%x at 0x%x\n",
723 subtype
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_SUBTYPE
);
728 case PMIC_MPP_SUBTYPE_4CH_NO_ANA_OUT
:
729 case PMIC_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT
:
730 case PMIC_MPP_SUBTYPE_4CH_NO_SINK
:
731 case PMIC_MPP_SUBTYPE_ULT_4CH_NO_SINK
:
732 case PMIC_MPP_SUBTYPE_4CH_FULL_FUNC
:
733 pad
->num_sources
= 4;
735 case PMIC_MPP_SUBTYPE_8CH_FULL_FUNC
:
736 pad
->num_sources
= 8;
739 dev_err(state
->dev
, "unknown MPP type 0x%x at 0x%x\n",
744 val
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_MODE_CTL
);
748 pad
->out_value
= val
& PMIC_MPP_REG_MODE_VALUE_MASK
;
750 dir
= val
>> PMIC_MPP_REG_MODE_DIR_SHIFT
;
751 dir
&= PMIC_MPP_REG_MODE_DIR_MASK
;
755 pad
->input_enabled
= true;
756 pad
->output_enabled
= false;
757 pad
->analog_mode
= false;
760 pad
->input_enabled
= false;
761 pad
->output_enabled
= true;
762 pad
->analog_mode
= false;
765 pad
->input_enabled
= true;
766 pad
->output_enabled
= true;
767 pad
->analog_mode
= false;
770 pad
->input_enabled
= true;
771 pad
->output_enabled
= true;
772 pad
->analog_mode
= true;
775 pad
->input_enabled
= true;
776 pad
->output_enabled
= false;
777 pad
->analog_mode
= true;
780 pad
->input_enabled
= false;
781 pad
->output_enabled
= true;
782 pad
->analog_mode
= true;
785 dev_err(state
->dev
, "unknown MPP direction\n");
789 pad
->function
= val
>> PMIC_MPP_REG_MODE_FUNCTION_SHIFT
;
790 pad
->function
&= PMIC_MPP_REG_MODE_FUNCTION_MASK
;
792 val
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_DIG_VIN_CTL
);
796 pad
->power_source
= val
>> PMIC_MPP_REG_VIN_SHIFT
;
797 pad
->power_source
&= PMIC_MPP_REG_VIN_MASK
;
799 val
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_DIG_PULL_CTL
);
803 pad
->pullup
= val
>> PMIC_MPP_REG_PULL_SHIFT
;
804 pad
->pullup
&= PMIC_MPP_REG_PULL_MASK
;
806 val
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_AIN_CTL
);
810 pad
->amux_input
= val
>> PMIC_MPP_REG_AIN_ROUTE_SHIFT
;
811 pad
->amux_input
&= PMIC_MPP_REG_AIN_ROUTE_MASK
;
813 /* Pin could be disabled with PIN_CONFIG_BIAS_HIGH_IMPEDANCE */
814 pad
->is_enabled
= true;
818 static int pmic_mpp_probe(struct platform_device
*pdev
)
820 struct device
*dev
= &pdev
->dev
;
821 struct pinctrl_pin_desc
*pindesc
;
822 struct pinctrl_desc
*pctrldesc
;
823 struct pmic_mpp_pad
*pad
, *pads
;
824 struct pmic_mpp_state
*state
;
828 ret
= of_property_read_u32_array(dev
->of_node
, "reg", res
, 2);
830 dev_err(dev
, "missing base address and/or range");
834 npins
= res
[1] / PMIC_MPP_ADDRESS_RANGE
;
838 BUG_ON(npins
> ARRAY_SIZE(pmic_mpp_groups
));
840 state
= devm_kzalloc(dev
, sizeof(*state
), GFP_KERNEL
);
844 platform_set_drvdata(pdev
, state
);
846 state
->dev
= &pdev
->dev
;
847 state
->map
= dev_get_regmap(dev
->parent
, NULL
);
849 pindesc
= devm_kcalloc(dev
, npins
, sizeof(*pindesc
), GFP_KERNEL
);
853 pads
= devm_kcalloc(dev
, npins
, sizeof(*pads
), GFP_KERNEL
);
857 pctrldesc
= devm_kzalloc(dev
, sizeof(*pctrldesc
), GFP_KERNEL
);
861 pctrldesc
->pctlops
= &pmic_mpp_pinctrl_ops
;
862 pctrldesc
->pmxops
= &pmic_mpp_pinmux_ops
;
863 pctrldesc
->confops
= &pmic_mpp_pinconf_ops
;
864 pctrldesc
->owner
= THIS_MODULE
;
865 pctrldesc
->name
= dev_name(dev
);
866 pctrldesc
->pins
= pindesc
;
867 pctrldesc
->npins
= npins
;
869 for (i
= 0; i
< npins
; i
++, pindesc
++) {
871 pindesc
->drv_data
= pad
;
873 pindesc
->name
= pmic_mpp_groups
[i
];
875 pad
->irq
= platform_get_irq(pdev
, i
);
879 pad
->base
= res
[0] + i
* PMIC_MPP_ADDRESS_RANGE
;
881 ret
= pmic_mpp_populate(state
, pad
);
886 state
->chip
= pmic_mpp_gpio_template
;
887 state
->chip
.dev
= dev
;
888 state
->chip
.base
= -1;
889 state
->chip
.ngpio
= npins
;
890 state
->chip
.label
= dev_name(dev
);
891 state
->chip
.of_gpio_n_cells
= 2;
892 state
->chip
.can_sleep
= false;
894 state
->ctrl
= pinctrl_register(pctrldesc
, dev
, state
);
895 if (IS_ERR(state
->ctrl
))
896 return PTR_ERR(state
->ctrl
);
898 ret
= gpiochip_add(&state
->chip
);
900 dev_err(state
->dev
, "can't add gpio chip\n");
904 ret
= gpiochip_add_pin_range(&state
->chip
, dev_name(dev
), 0, 0, npins
);
906 dev_err(dev
, "failed to add pin range\n");
913 gpiochip_remove(&state
->chip
);
915 pinctrl_unregister(state
->ctrl
);
919 static int pmic_mpp_remove(struct platform_device
*pdev
)
921 struct pmic_mpp_state
*state
= platform_get_drvdata(pdev
);
923 gpiochip_remove(&state
->chip
);
924 pinctrl_unregister(state
->ctrl
);
928 static const struct of_device_id pmic_mpp_of_match
[] = {
929 { .compatible
= "qcom,pm8841-mpp" }, /* 4 MPP's */
930 { .compatible
= "qcom,pm8916-mpp" }, /* 4 MPP's */
931 { .compatible
= "qcom,pm8941-mpp" }, /* 8 MPP's */
932 { .compatible
= "qcom,pma8084-mpp" }, /* 8 MPP's */
936 MODULE_DEVICE_TABLE(of
, pmic_mpp_of_match
);
938 static struct platform_driver pmic_mpp_driver
= {
940 .name
= "qcom-spmi-mpp",
941 .of_match_table
= pmic_mpp_of_match
,
943 .probe
= pmic_mpp_probe
,
944 .remove
= pmic_mpp_remove
,
947 module_platform_driver(pmic_mpp_driver
);
949 MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");
950 MODULE_DESCRIPTION("Qualcomm SPMI PMIC MPP pin control driver");
951 MODULE_ALIAS("platform:qcom-spmi-mpp");
952 MODULE_LICENSE("GPL v2");