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-mpp.h>
29 #include "../pinctrl-utils.h"
31 #define PMIC_MPP_ADDRESS_RANGE 0x100
34 * Pull Up Values - it indicates whether a pull-up should be
35 * applied for bidirectional mode only. The hardware ignores the
36 * configuration when operating in other modes.
38 #define PMIC_MPP_PULL_UP_0P6KOHM 0
39 #define PMIC_MPP_PULL_UP_10KOHM 1
40 #define PMIC_MPP_PULL_UP_30KOHM 2
41 #define PMIC_MPP_PULL_UP_OPEN 3
43 /* type registers base address bases */
44 #define PMIC_MPP_REG_TYPE 0x4
45 #define PMIC_MPP_REG_SUBTYPE 0x5
47 /* mpp peripheral type and subtype values */
48 #define PMIC_MPP_TYPE 0x11
49 #define PMIC_MPP_SUBTYPE_4CH_NO_ANA_OUT 0x3
50 #define PMIC_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT 0x4
51 #define PMIC_MPP_SUBTYPE_4CH_NO_SINK 0x5
52 #define PMIC_MPP_SUBTYPE_ULT_4CH_NO_SINK 0x6
53 #define PMIC_MPP_SUBTYPE_4CH_FULL_FUNC 0x7
54 #define PMIC_MPP_SUBTYPE_8CH_FULL_FUNC 0xf
56 #define PMIC_MPP_REG_RT_STS 0x10
57 #define PMIC_MPP_REG_RT_STS_VAL_MASK 0x1
59 /* control register base address bases */
60 #define PMIC_MPP_REG_MODE_CTL 0x40
61 #define PMIC_MPP_REG_DIG_VIN_CTL 0x41
62 #define PMIC_MPP_REG_DIG_PULL_CTL 0x42
63 #define PMIC_MPP_REG_DIG_IN_CTL 0x43
64 #define PMIC_MPP_REG_EN_CTL 0x46
65 #define PMIC_MPP_REG_AOUT_CTL 0x48
66 #define PMIC_MPP_REG_AIN_CTL 0x4a
67 #define PMIC_MPP_REG_SINK_CTL 0x4c
69 /* PMIC_MPP_REG_MODE_CTL */
70 #define PMIC_MPP_REG_MODE_VALUE_MASK 0x1
71 #define PMIC_MPP_REG_MODE_FUNCTION_SHIFT 1
72 #define PMIC_MPP_REG_MODE_FUNCTION_MASK 0x7
73 #define PMIC_MPP_REG_MODE_DIR_SHIFT 4
74 #define PMIC_MPP_REG_MODE_DIR_MASK 0x7
76 /* PMIC_MPP_REG_DIG_VIN_CTL */
77 #define PMIC_MPP_REG_VIN_SHIFT 0
78 #define PMIC_MPP_REG_VIN_MASK 0x7
80 /* PMIC_MPP_REG_DIG_PULL_CTL */
81 #define PMIC_MPP_REG_PULL_SHIFT 0
82 #define PMIC_MPP_REG_PULL_MASK 0x7
84 /* PMIC_MPP_REG_EN_CTL */
85 #define PMIC_MPP_REG_MASTER_EN_SHIFT 7
87 /* PMIC_MPP_REG_AIN_CTL */
88 #define PMIC_MPP_REG_AIN_ROUTE_SHIFT 0
89 #define PMIC_MPP_REG_AIN_ROUTE_MASK 0x7
91 #define PMIC_MPP_MODE_DIGITAL_INPUT 0
92 #define PMIC_MPP_MODE_DIGITAL_OUTPUT 1
93 #define PMIC_MPP_MODE_DIGITAL_BIDIR 2
94 #define PMIC_MPP_MODE_ANALOG_BIDIR 3
95 #define PMIC_MPP_MODE_ANALOG_INPUT 4
96 #define PMIC_MPP_MODE_ANALOG_OUTPUT 5
97 #define PMIC_MPP_MODE_CURRENT_SINK 6
99 #define PMIC_MPP_SELECTOR_NORMAL 0
100 #define PMIC_MPP_SELECTOR_PAIRED 1
101 #define PMIC_MPP_SELECTOR_DTEST_FIRST 4
103 #define PMIC_MPP_PHYSICAL_OFFSET 1
105 /* Qualcomm specific pin configurations */
106 #define PMIC_MPP_CONF_AMUX_ROUTE (PIN_CONFIG_END + 1)
107 #define PMIC_MPP_CONF_ANALOG_LEVEL (PIN_CONFIG_END + 2)
108 #define PMIC_MPP_CONF_DTEST_SELECTOR (PIN_CONFIG_END + 3)
109 #define PMIC_MPP_CONF_PAIRED (PIN_CONFIG_END + 4)
112 * struct pmic_mpp_pad - keep current MPP settings
113 * @base: Address base in SPMI device.
114 * @irq: IRQ number which this MPP generate.
115 * @is_enabled: Set to false when MPP should be put in high Z state.
116 * @out_value: Cached pin output value.
117 * @output_enabled: Set to true if MPP output logic is enabled.
118 * @input_enabled: Set to true if MPP input buffer logic is enabled.
119 * @paired: Pin operates in paired mode
120 * @num_sources: Number of power-sources supported by this MPP.
121 * @power_source: Current power-source used.
122 * @amux_input: Set the source for analog input.
123 * @aout_level: Analog output level
124 * @pullup: Pullup resistor value. Valid in Bidirectional mode only.
125 * @function: See pmic_mpp_functions[].
126 * @drive_strength: Amount of current in sink mode
127 * @dtest: DTEST route selector
129 struct pmic_mpp_pad
{
137 unsigned int num_sources
;
138 unsigned int power_source
;
139 unsigned int amux_input
;
140 unsigned int aout_level
;
142 unsigned int function
;
143 unsigned int drive_strength
;
147 struct pmic_mpp_state
{
150 struct pinctrl_dev
*ctrl
;
151 struct gpio_chip chip
;
154 static const struct pinconf_generic_params pmic_mpp_bindings
[] = {
155 {"qcom,amux-route", PMIC_MPP_CONF_AMUX_ROUTE
, 0},
156 {"qcom,analog-level", PMIC_MPP_CONF_ANALOG_LEVEL
, 0},
157 {"qcom,dtest", PMIC_MPP_CONF_DTEST_SELECTOR
, 0},
158 {"qcom,paired", PMIC_MPP_CONF_PAIRED
, 0},
161 #ifdef CONFIG_DEBUG_FS
162 static const struct pin_config_item pmic_conf_items
[] = {
163 PCONFDUMP(PMIC_MPP_CONF_AMUX_ROUTE
, "analog mux", NULL
, true),
164 PCONFDUMP(PMIC_MPP_CONF_ANALOG_LEVEL
, "analog level", NULL
, true),
165 PCONFDUMP(PMIC_MPP_CONF_DTEST_SELECTOR
, "dtest", NULL
, true),
166 PCONFDUMP(PMIC_MPP_CONF_PAIRED
, "paired", NULL
, false),
170 static const char *const pmic_mpp_groups
[] = {
171 "mpp1", "mpp2", "mpp3", "mpp4", "mpp5", "mpp6", "mpp7", "mpp8",
174 #define PMIC_MPP_DIGITAL 0
175 #define PMIC_MPP_ANALOG 1
176 #define PMIC_MPP_SINK 2
178 static const char *const pmic_mpp_functions
[] = {
179 "digital", "analog", "sink"
182 static int pmic_mpp_read(struct pmic_mpp_state
*state
,
183 struct pmic_mpp_pad
*pad
, unsigned int addr
)
188 ret
= regmap_read(state
->map
, pad
->base
+ addr
, &val
);
190 dev_err(state
->dev
, "read 0x%x failed\n", addr
);
197 static int pmic_mpp_write(struct pmic_mpp_state
*state
,
198 struct pmic_mpp_pad
*pad
, unsigned int addr
,
203 ret
= regmap_write(state
->map
, pad
->base
+ addr
, val
);
205 dev_err(state
->dev
, "write 0x%x failed\n", addr
);
210 static int pmic_mpp_get_groups_count(struct pinctrl_dev
*pctldev
)
212 /* Every PIN is a group */
213 return pctldev
->desc
->npins
;
216 static const char *pmic_mpp_get_group_name(struct pinctrl_dev
*pctldev
,
219 return pctldev
->desc
->pins
[pin
].name
;
222 static int pmic_mpp_get_group_pins(struct pinctrl_dev
*pctldev
,
224 const unsigned **pins
, unsigned *num_pins
)
226 *pins
= &pctldev
->desc
->pins
[pin
].number
;
231 static const struct pinctrl_ops pmic_mpp_pinctrl_ops
= {
232 .get_groups_count
= pmic_mpp_get_groups_count
,
233 .get_group_name
= pmic_mpp_get_group_name
,
234 .get_group_pins
= pmic_mpp_get_group_pins
,
235 .dt_node_to_map
= pinconf_generic_dt_node_to_map_group
,
236 .dt_free_map
= pinctrl_utils_dt_free_map
,
239 static int pmic_mpp_get_functions_count(struct pinctrl_dev
*pctldev
)
241 return ARRAY_SIZE(pmic_mpp_functions
);
244 static const char *pmic_mpp_get_function_name(struct pinctrl_dev
*pctldev
,
247 return pmic_mpp_functions
[function
];
250 static int pmic_mpp_get_function_groups(struct pinctrl_dev
*pctldev
,
252 const char *const **groups
,
253 unsigned *const num_qgroups
)
255 *groups
= pmic_mpp_groups
;
256 *num_qgroups
= pctldev
->desc
->npins
;
260 static int pmic_mpp_write_mode_ctl(struct pmic_mpp_state
*state
,
261 struct pmic_mpp_pad
*pad
)
268 switch (pad
->function
) {
269 case PMIC_MPP_ANALOG
:
270 if (pad
->input_enabled
&& pad
->output_enabled
)
271 mode
= PMIC_MPP_MODE_ANALOG_BIDIR
;
272 else if (pad
->input_enabled
)
273 mode
= PMIC_MPP_MODE_ANALOG_INPUT
;
275 mode
= PMIC_MPP_MODE_ANALOG_OUTPUT
;
277 case PMIC_MPP_DIGITAL
:
278 if (pad
->input_enabled
&& pad
->output_enabled
)
279 mode
= PMIC_MPP_MODE_DIGITAL_BIDIR
;
280 else if (pad
->input_enabled
)
281 mode
= PMIC_MPP_MODE_DIGITAL_INPUT
;
283 mode
= PMIC_MPP_MODE_DIGITAL_OUTPUT
;
287 mode
= PMIC_MPP_MODE_CURRENT_SINK
;
292 sel
= PMIC_MPP_SELECTOR_DTEST_FIRST
+ pad
->dtest
- 1;
293 else if (pad
->paired
)
294 sel
= PMIC_MPP_SELECTOR_PAIRED
;
296 sel
= PMIC_MPP_SELECTOR_NORMAL
;
298 en
= !!pad
->out_value
;
300 val
= mode
<< PMIC_MPP_REG_MODE_DIR_SHIFT
|
301 sel
<< PMIC_MPP_REG_MODE_FUNCTION_SHIFT
|
304 return pmic_mpp_write(state
, pad
, PMIC_MPP_REG_MODE_CTL
, val
);
307 static int pmic_mpp_set_mux(struct pinctrl_dev
*pctldev
, unsigned function
,
310 struct pmic_mpp_state
*state
= pinctrl_dev_get_drvdata(pctldev
);
311 struct pmic_mpp_pad
*pad
;
315 pad
= pctldev
->desc
->pins
[pin
].drv_data
;
317 pad
->function
= function
;
319 ret
= pmic_mpp_write_mode_ctl(state
, pad
);
321 val
= pad
->is_enabled
<< PMIC_MPP_REG_MASTER_EN_SHIFT
;
323 return pmic_mpp_write(state
, pad
, PMIC_MPP_REG_EN_CTL
, val
);
326 static const struct pinmux_ops pmic_mpp_pinmux_ops
= {
327 .get_functions_count
= pmic_mpp_get_functions_count
,
328 .get_function_name
= pmic_mpp_get_function_name
,
329 .get_function_groups
= pmic_mpp_get_function_groups
,
330 .set_mux
= pmic_mpp_set_mux
,
333 static int pmic_mpp_config_get(struct pinctrl_dev
*pctldev
,
334 unsigned int pin
, unsigned long *config
)
336 unsigned param
= pinconf_to_config_param(*config
);
337 struct pmic_mpp_pad
*pad
;
340 pad
= pctldev
->desc
->pins
[pin
].drv_data
;
343 case PIN_CONFIG_BIAS_DISABLE
:
344 arg
= pad
->pullup
== PMIC_MPP_PULL_UP_OPEN
;
346 case PIN_CONFIG_BIAS_PULL_UP
:
347 switch (pad
->pullup
) {
348 case PMIC_MPP_PULL_UP_OPEN
:
351 case PMIC_MPP_PULL_UP_0P6KOHM
:
354 case PMIC_MPP_PULL_UP_10KOHM
:
357 case PMIC_MPP_PULL_UP_30KOHM
:
364 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE
:
365 arg
= !pad
->is_enabled
;
367 case PIN_CONFIG_POWER_SOURCE
:
368 arg
= pad
->power_source
;
370 case PIN_CONFIG_INPUT_ENABLE
:
371 arg
= pad
->input_enabled
;
373 case PIN_CONFIG_OUTPUT
:
374 arg
= pad
->out_value
;
376 case PMIC_MPP_CONF_DTEST_SELECTOR
:
379 case PMIC_MPP_CONF_AMUX_ROUTE
:
380 arg
= pad
->amux_input
;
382 case PMIC_MPP_CONF_PAIRED
:
385 case PIN_CONFIG_DRIVE_STRENGTH
:
386 arg
= pad
->drive_strength
;
388 case PMIC_MPP_CONF_ANALOG_LEVEL
:
389 arg
= pad
->aout_level
;
395 /* Convert register value to pinconf value */
396 *config
= pinconf_to_config_packed(param
, arg
);
400 static int pmic_mpp_config_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
401 unsigned long *configs
, unsigned nconfs
)
403 struct pmic_mpp_state
*state
= pinctrl_dev_get_drvdata(pctldev
);
404 struct pmic_mpp_pad
*pad
;
409 pad
= pctldev
->desc
->pins
[pin
].drv_data
;
411 /* Make it possible to enable the pin, by not setting high impedance */
412 pad
->is_enabled
= true;
414 for (i
= 0; i
< nconfs
; i
++) {
415 param
= pinconf_to_config_param(configs
[i
]);
416 arg
= pinconf_to_config_argument(configs
[i
]);
419 case PIN_CONFIG_BIAS_DISABLE
:
420 pad
->pullup
= PMIC_MPP_PULL_UP_OPEN
;
422 case PIN_CONFIG_BIAS_PULL_UP
:
425 pad
->pullup
= PMIC_MPP_PULL_UP_0P6KOHM
;
428 pad
->pullup
= PMIC_MPP_PULL_UP_10KOHM
;
431 pad
->pullup
= PMIC_MPP_PULL_UP_30KOHM
;
437 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE
:
438 pad
->is_enabled
= false;
440 case PIN_CONFIG_POWER_SOURCE
:
441 if (arg
>= pad
->num_sources
)
443 pad
->power_source
= arg
;
445 case PIN_CONFIG_INPUT_ENABLE
:
446 pad
->input_enabled
= arg
? true : false;
448 case PIN_CONFIG_OUTPUT
:
449 pad
->output_enabled
= true;
450 pad
->out_value
= arg
;
452 case PMIC_MPP_CONF_DTEST_SELECTOR
:
455 case PIN_CONFIG_DRIVE_STRENGTH
:
456 arg
= pad
->drive_strength
;
458 case PMIC_MPP_CONF_AMUX_ROUTE
:
459 if (arg
>= PMIC_MPP_AMUX_ROUTE_ABUS4
)
461 pad
->amux_input
= arg
;
463 case PMIC_MPP_CONF_ANALOG_LEVEL
:
464 pad
->aout_level
= arg
;
466 case PMIC_MPP_CONF_PAIRED
:
474 val
= pad
->power_source
<< PMIC_MPP_REG_VIN_SHIFT
;
476 ret
= pmic_mpp_write(state
, pad
, PMIC_MPP_REG_DIG_VIN_CTL
, val
);
480 val
= pad
->pullup
<< PMIC_MPP_REG_PULL_SHIFT
;
482 ret
= pmic_mpp_write(state
, pad
, PMIC_MPP_REG_DIG_PULL_CTL
, val
);
486 val
= pad
->amux_input
& PMIC_MPP_REG_AIN_ROUTE_MASK
;
488 ret
= pmic_mpp_write(state
, pad
, PMIC_MPP_REG_AIN_CTL
, val
);
492 ret
= pmic_mpp_write(state
, pad
, PMIC_MPP_REG_AOUT_CTL
, pad
->aout_level
);
496 ret
= pmic_mpp_write_mode_ctl(state
, pad
);
500 val
= pad
->is_enabled
<< PMIC_MPP_REG_MASTER_EN_SHIFT
;
502 return pmic_mpp_write(state
, pad
, PMIC_MPP_REG_EN_CTL
, val
);
505 static void pmic_mpp_config_dbg_show(struct pinctrl_dev
*pctldev
,
506 struct seq_file
*s
, unsigned pin
)
508 struct pmic_mpp_state
*state
= pinctrl_dev_get_drvdata(pctldev
);
509 struct pmic_mpp_pad
*pad
;
512 static const char *const biases
[] = {
513 "0.6kOhm", "10kOhm", "30kOhm", "Disabled"
516 pad
= pctldev
->desc
->pins
[pin
].drv_data
;
518 seq_printf(s
, " mpp%-2d:", pin
+ PMIC_MPP_PHYSICAL_OFFSET
);
520 if (!pad
->is_enabled
) {
524 if (pad
->input_enabled
) {
525 ret
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_RT_STS
);
529 ret
&= PMIC_MPP_REG_RT_STS_VAL_MASK
;
530 pad
->out_value
= ret
;
533 seq_printf(s
, " %-4s", pad
->output_enabled
? "out" : "in");
534 seq_printf(s
, " %-7s", pmic_mpp_functions
[pad
->function
]);
535 seq_printf(s
, " vin-%d", pad
->power_source
);
536 seq_printf(s
, " %d", pad
->aout_level
);
537 seq_printf(s
, " %-8s", biases
[pad
->pullup
]);
538 seq_printf(s
, " %-4s", pad
->out_value
? "high" : "low");
540 seq_printf(s
, " dtest%d", pad
->dtest
);
542 seq_puts(s
, " paired");
546 static const struct pinconf_ops pmic_mpp_pinconf_ops
= {
548 .pin_config_group_get
= pmic_mpp_config_get
,
549 .pin_config_group_set
= pmic_mpp_config_set
,
550 .pin_config_group_dbg_show
= pmic_mpp_config_dbg_show
,
553 static int pmic_mpp_direction_input(struct gpio_chip
*chip
, unsigned pin
)
555 struct pmic_mpp_state
*state
= gpiochip_get_data(chip
);
556 unsigned long config
;
558 config
= pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE
, 1);
560 return pmic_mpp_config_set(state
->ctrl
, pin
, &config
, 1);
563 static int pmic_mpp_direction_output(struct gpio_chip
*chip
,
564 unsigned pin
, int val
)
566 struct pmic_mpp_state
*state
= gpiochip_get_data(chip
);
567 unsigned long config
;
569 config
= pinconf_to_config_packed(PIN_CONFIG_OUTPUT
, val
);
571 return pmic_mpp_config_set(state
->ctrl
, pin
, &config
, 1);
574 static int pmic_mpp_get(struct gpio_chip
*chip
, unsigned pin
)
576 struct pmic_mpp_state
*state
= gpiochip_get_data(chip
);
577 struct pmic_mpp_pad
*pad
;
580 pad
= state
->ctrl
->desc
->pins
[pin
].drv_data
;
582 if (pad
->input_enabled
) {
583 ret
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_RT_STS
);
587 pad
->out_value
= ret
& PMIC_MPP_REG_RT_STS_VAL_MASK
;
590 return !!pad
->out_value
;
593 static void pmic_mpp_set(struct gpio_chip
*chip
, unsigned pin
, int value
)
595 struct pmic_mpp_state
*state
= gpiochip_get_data(chip
);
596 unsigned long config
;
598 config
= pinconf_to_config_packed(PIN_CONFIG_OUTPUT
, value
);
600 pmic_mpp_config_set(state
->ctrl
, pin
, &config
, 1);
603 static int pmic_mpp_of_xlate(struct gpio_chip
*chip
,
604 const struct of_phandle_args
*gpio_desc
,
607 if (chip
->of_gpio_n_cells
< 2)
611 *flags
= gpio_desc
->args
[1];
613 return gpio_desc
->args
[0] - PMIC_MPP_PHYSICAL_OFFSET
;
616 static int pmic_mpp_to_irq(struct gpio_chip
*chip
, unsigned pin
)
618 struct pmic_mpp_state
*state
= gpiochip_get_data(chip
);
619 struct pmic_mpp_pad
*pad
;
621 pad
= state
->ctrl
->desc
->pins
[pin
].drv_data
;
626 static void pmic_mpp_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
628 struct pmic_mpp_state
*state
= gpiochip_get_data(chip
);
631 for (i
= 0; i
< chip
->ngpio
; i
++) {
632 pmic_mpp_config_dbg_show(state
->ctrl
, s
, i
);
637 static const struct gpio_chip pmic_mpp_gpio_template
= {
638 .direction_input
= pmic_mpp_direction_input
,
639 .direction_output
= pmic_mpp_direction_output
,
642 .request
= gpiochip_generic_request
,
643 .free
= gpiochip_generic_free
,
644 .of_xlate
= pmic_mpp_of_xlate
,
645 .to_irq
= pmic_mpp_to_irq
,
646 .dbg_show
= pmic_mpp_dbg_show
,
649 static int pmic_mpp_populate(struct pmic_mpp_state
*state
,
650 struct pmic_mpp_pad
*pad
)
652 int type
, subtype
, val
, dir
;
655 type
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_TYPE
);
659 if (type
!= PMIC_MPP_TYPE
) {
660 dev_err(state
->dev
, "incorrect block type 0x%x at 0x%x\n",
665 subtype
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_SUBTYPE
);
670 case PMIC_MPP_SUBTYPE_4CH_NO_ANA_OUT
:
671 case PMIC_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT
:
672 case PMIC_MPP_SUBTYPE_4CH_NO_SINK
:
673 case PMIC_MPP_SUBTYPE_ULT_4CH_NO_SINK
:
674 case PMIC_MPP_SUBTYPE_4CH_FULL_FUNC
:
675 pad
->num_sources
= 4;
677 case PMIC_MPP_SUBTYPE_8CH_FULL_FUNC
:
678 pad
->num_sources
= 8;
681 dev_err(state
->dev
, "unknown MPP type 0x%x at 0x%x\n",
686 val
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_MODE_CTL
);
690 pad
->out_value
= val
& PMIC_MPP_REG_MODE_VALUE_MASK
;
692 dir
= val
>> PMIC_MPP_REG_MODE_DIR_SHIFT
;
693 dir
&= PMIC_MPP_REG_MODE_DIR_MASK
;
696 case PMIC_MPP_MODE_DIGITAL_INPUT
:
697 pad
->input_enabled
= true;
698 pad
->output_enabled
= false;
699 pad
->function
= PMIC_MPP_DIGITAL
;
701 case PMIC_MPP_MODE_DIGITAL_OUTPUT
:
702 pad
->input_enabled
= false;
703 pad
->output_enabled
= true;
704 pad
->function
= PMIC_MPP_DIGITAL
;
706 case PMIC_MPP_MODE_DIGITAL_BIDIR
:
707 pad
->input_enabled
= true;
708 pad
->output_enabled
= true;
709 pad
->function
= PMIC_MPP_DIGITAL
;
711 case PMIC_MPP_MODE_ANALOG_BIDIR
:
712 pad
->input_enabled
= true;
713 pad
->output_enabled
= true;
714 pad
->function
= PMIC_MPP_ANALOG
;
716 case PMIC_MPP_MODE_ANALOG_INPUT
:
717 pad
->input_enabled
= true;
718 pad
->output_enabled
= false;
719 pad
->function
= PMIC_MPP_ANALOG
;
721 case PMIC_MPP_MODE_ANALOG_OUTPUT
:
722 pad
->input_enabled
= false;
723 pad
->output_enabled
= true;
724 pad
->function
= PMIC_MPP_ANALOG
;
726 case PMIC_MPP_MODE_CURRENT_SINK
:
727 pad
->input_enabled
= false;
728 pad
->output_enabled
= true;
729 pad
->function
= PMIC_MPP_SINK
;
732 dev_err(state
->dev
, "unknown MPP direction\n");
736 sel
= val
>> PMIC_MPP_REG_MODE_FUNCTION_SHIFT
;
737 sel
&= PMIC_MPP_REG_MODE_FUNCTION_MASK
;
739 if (sel
>= PMIC_MPP_SELECTOR_DTEST_FIRST
)
740 pad
->dtest
= sel
+ 1;
741 else if (sel
== PMIC_MPP_SELECTOR_PAIRED
)
744 val
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_DIG_VIN_CTL
);
748 pad
->power_source
= val
>> PMIC_MPP_REG_VIN_SHIFT
;
749 pad
->power_source
&= PMIC_MPP_REG_VIN_MASK
;
751 val
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_DIG_PULL_CTL
);
755 pad
->pullup
= val
>> PMIC_MPP_REG_PULL_SHIFT
;
756 pad
->pullup
&= PMIC_MPP_REG_PULL_MASK
;
758 val
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_AIN_CTL
);
762 pad
->amux_input
= val
>> PMIC_MPP_REG_AIN_ROUTE_SHIFT
;
763 pad
->amux_input
&= PMIC_MPP_REG_AIN_ROUTE_MASK
;
765 val
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_SINK_CTL
);
769 pad
->drive_strength
= val
;
771 val
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_AOUT_CTL
);
775 pad
->aout_level
= val
;
777 val
= pmic_mpp_read(state
, pad
, PMIC_MPP_REG_EN_CTL
);
781 pad
->is_enabled
= !!val
;
786 static int pmic_mpp_probe(struct platform_device
*pdev
)
788 struct device
*dev
= &pdev
->dev
;
789 struct pinctrl_pin_desc
*pindesc
;
790 struct pinctrl_desc
*pctrldesc
;
791 struct pmic_mpp_pad
*pad
, *pads
;
792 struct pmic_mpp_state
*state
;
796 ret
= of_property_read_u32(dev
->of_node
, "reg", ®
);
798 dev_err(dev
, "missing base address");
802 npins
= platform_irq_count(pdev
);
808 BUG_ON(npins
> ARRAY_SIZE(pmic_mpp_groups
));
810 state
= devm_kzalloc(dev
, sizeof(*state
), GFP_KERNEL
);
814 platform_set_drvdata(pdev
, state
);
816 state
->dev
= &pdev
->dev
;
817 state
->map
= dev_get_regmap(dev
->parent
, NULL
);
819 pindesc
= devm_kcalloc(dev
, npins
, sizeof(*pindesc
), GFP_KERNEL
);
823 pads
= devm_kcalloc(dev
, npins
, sizeof(*pads
), GFP_KERNEL
);
827 pctrldesc
= devm_kzalloc(dev
, sizeof(*pctrldesc
), GFP_KERNEL
);
831 pctrldesc
->pctlops
= &pmic_mpp_pinctrl_ops
;
832 pctrldesc
->pmxops
= &pmic_mpp_pinmux_ops
;
833 pctrldesc
->confops
= &pmic_mpp_pinconf_ops
;
834 pctrldesc
->owner
= THIS_MODULE
;
835 pctrldesc
->name
= dev_name(dev
);
836 pctrldesc
->pins
= pindesc
;
837 pctrldesc
->npins
= npins
;
839 pctrldesc
->num_custom_params
= ARRAY_SIZE(pmic_mpp_bindings
);
840 pctrldesc
->custom_params
= pmic_mpp_bindings
;
841 #ifdef CONFIG_DEBUG_FS
842 pctrldesc
->custom_conf_items
= pmic_conf_items
;
845 for (i
= 0; i
< npins
; i
++, pindesc
++) {
847 pindesc
->drv_data
= pad
;
849 pindesc
->name
= pmic_mpp_groups
[i
];
851 pad
->irq
= platform_get_irq(pdev
, i
);
855 pad
->base
= reg
+ i
* PMIC_MPP_ADDRESS_RANGE
;
857 ret
= pmic_mpp_populate(state
, pad
);
862 state
->chip
= pmic_mpp_gpio_template
;
863 state
->chip
.parent
= dev
;
864 state
->chip
.base
= -1;
865 state
->chip
.ngpio
= npins
;
866 state
->chip
.label
= dev_name(dev
);
867 state
->chip
.of_gpio_n_cells
= 2;
868 state
->chip
.can_sleep
= false;
870 state
->ctrl
= pinctrl_register(pctrldesc
, dev
, state
);
871 if (IS_ERR(state
->ctrl
))
872 return PTR_ERR(state
->ctrl
);
874 ret
= gpiochip_add_data(&state
->chip
, state
);
876 dev_err(state
->dev
, "can't add gpio chip\n");
880 ret
= gpiochip_add_pin_range(&state
->chip
, dev_name(dev
), 0, 0, npins
);
882 dev_err(dev
, "failed to add pin range\n");
889 gpiochip_remove(&state
->chip
);
891 pinctrl_unregister(state
->ctrl
);
895 static int pmic_mpp_remove(struct platform_device
*pdev
)
897 struct pmic_mpp_state
*state
= platform_get_drvdata(pdev
);
899 gpiochip_remove(&state
->chip
);
900 pinctrl_unregister(state
->ctrl
);
904 static const struct of_device_id pmic_mpp_of_match
[] = {
905 { .compatible
= "qcom,pm8841-mpp" }, /* 4 MPP's */
906 { .compatible
= "qcom,pm8916-mpp" }, /* 4 MPP's */
907 { .compatible
= "qcom,pm8941-mpp" }, /* 8 MPP's */
908 { .compatible
= "qcom,pm8994-mpp" }, /* 8 MPP's */
909 { .compatible
= "qcom,pma8084-mpp" }, /* 8 MPP's */
913 MODULE_DEVICE_TABLE(of
, pmic_mpp_of_match
);
915 static struct platform_driver pmic_mpp_driver
= {
917 .name
= "qcom-spmi-mpp",
918 .of_match_table
= pmic_mpp_of_match
,
920 .probe
= pmic_mpp_probe
,
921 .remove
= pmic_mpp_remove
,
924 module_platform_driver(pmic_mpp_driver
);
926 MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");
927 MODULE_DESCRIPTION("Qualcomm SPMI PMIC MPP pin control driver");
928 MODULE_ALIAS("platform:qcom-spmi-mpp");
929 MODULE_LICENSE("GPL v2");