GNU Linux-libre 4.9.308-gnu1
[releases.git] / drivers / pinctrl / qcom / pinctrl-spmi-gpio.c
1 /*
2  * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
3  *
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.
7  *
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.
12  */
13
14 #include <linux/gpio.h>
15 #include <linux/module.h>
16 #include <linux/of.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>
25
26 #include <dt-bindings/pinctrl/qcom,pmic-gpio.h>
27
28 #include "../core.h"
29 #include "../pinctrl-utils.h"
30
31 #define PMIC_GPIO_ADDRESS_RANGE                 0x100
32
33 /* type and subtype registers base address offsets */
34 #define PMIC_GPIO_REG_TYPE                      0x4
35 #define PMIC_GPIO_REG_SUBTYPE                   0x5
36
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
43
44 #define PMIC_MPP_REG_RT_STS                     0x10
45 #define PMIC_MPP_REG_RT_STS_VAL_MASK            0x1
46
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
53
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
60
61 /* PMIC_GPIO_REG_DIG_VIN_CTL */
62 #define PMIC_GPIO_REG_VIN_SHIFT                 0
63 #define PMIC_GPIO_REG_VIN_MASK                  0x7
64
65 /* PMIC_GPIO_REG_DIG_PULL_CTL */
66 #define PMIC_GPIO_REG_PULL_SHIFT                0
67 #define PMIC_GPIO_REG_PULL_MASK                 0x7
68
69 #define PMIC_GPIO_PULL_DOWN                     4
70 #define PMIC_GPIO_PULL_DISABLE                  5
71
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
77
78 /*
79  * Output type - indicates pin should be configured as push-pull,
80  * open drain or open source.
81  */
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
85
86 /* PMIC_GPIO_REG_EN_CTL */
87 #define PMIC_GPIO_REG_MASTER_EN_SHIFT           7
88
89 #define PMIC_GPIO_PHYSICAL_OFFSET               1
90
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)
94
95 /**
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[]
111  */
112 struct pmic_gpio_pad {
113         u16             base;
114         int             irq;
115         bool            is_enabled;
116         bool            out_value;
117         bool            have_buffer;
118         bool            output_enabled;
119         bool            input_enabled;
120         unsigned int    num_sources;
121         unsigned int    power_source;
122         unsigned int    buffer_type;
123         unsigned int    pullup;
124         unsigned int    strength;
125         unsigned int    function;
126 };
127
128 struct pmic_gpio_state {
129         struct device   *dev;
130         struct regmap   *map;
131         struct pinctrl_dev *ctrl;
132         struct gpio_chip chip;
133 };
134
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},
138 };
139
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),
144 };
145 #endif
146
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",
153 };
154
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,
160 };
161
162 static int pmic_gpio_read(struct pmic_gpio_state *state,
163                           struct pmic_gpio_pad *pad, unsigned int addr)
164 {
165         unsigned int val;
166         int ret;
167
168         ret = regmap_read(state->map, pad->base + addr, &val);
169         if (ret < 0)
170                 dev_err(state->dev, "read 0x%x failed\n", addr);
171         else
172                 ret = val;
173
174         return ret;
175 }
176
177 static int pmic_gpio_write(struct pmic_gpio_state *state,
178                            struct pmic_gpio_pad *pad, unsigned int addr,
179                            unsigned int val)
180 {
181         int ret;
182
183         ret = regmap_write(state->map, pad->base + addr, val);
184         if (ret < 0)
185                 dev_err(state->dev, "write 0x%x failed\n", addr);
186
187         return ret;
188 }
189
190 static int pmic_gpio_get_groups_count(struct pinctrl_dev *pctldev)
191 {
192         /* Every PIN is a group */
193         return pctldev->desc->npins;
194 }
195
196 static const char *pmic_gpio_get_group_name(struct pinctrl_dev *pctldev,
197                                             unsigned pin)
198 {
199         return pctldev->desc->pins[pin].name;
200 }
201
202 static int pmic_gpio_get_group_pins(struct pinctrl_dev *pctldev, unsigned pin,
203                                     const unsigned **pins, unsigned *num_pins)
204 {
205         *pins = &pctldev->desc->pins[pin].number;
206         *num_pins = 1;
207         return 0;
208 }
209
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_free_map,
216 };
217
218 static int pmic_gpio_get_functions_count(struct pinctrl_dev *pctldev)
219 {
220         return ARRAY_SIZE(pmic_gpio_functions);
221 }
222
223 static const char *pmic_gpio_get_function_name(struct pinctrl_dev *pctldev,
224                                                unsigned function)
225 {
226         return pmic_gpio_functions[function];
227 }
228
229 static int pmic_gpio_get_function_groups(struct pinctrl_dev *pctldev,
230                                          unsigned function,
231                                          const char *const **groups,
232                                          unsigned *const num_qgroups)
233 {
234         *groups = pmic_gpio_groups;
235         *num_qgroups = pctldev->desc->npins;
236         return 0;
237 }
238
239 static int pmic_gpio_set_mux(struct pinctrl_dev *pctldev, unsigned function,
240                                 unsigned pin)
241 {
242         struct pmic_gpio_state *state = pinctrl_dev_get_drvdata(pctldev);
243         struct pmic_gpio_pad *pad;
244         unsigned int val;
245         int ret;
246
247         pad = pctldev->desc->pins[pin].drv_data;
248
249         pad->function = function;
250
251         val = 0;
252         if (pad->output_enabled) {
253                 if (pad->input_enabled)
254                         val = 2;
255                 else
256                         val = 1;
257         }
258
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;
262
263         ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_MODE_CTL, val);
264         if (ret < 0)
265                 return ret;
266
267         val = pad->is_enabled << PMIC_GPIO_REG_MASTER_EN_SHIFT;
268
269         return pmic_gpio_write(state, pad, PMIC_GPIO_REG_EN_CTL, val);
270 }
271
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,
277 };
278
279 static int pmic_gpio_config_get(struct pinctrl_dev *pctldev,
280                                 unsigned int pin, unsigned long *config)
281 {
282         unsigned param = pinconf_to_config_param(*config);
283         struct pmic_gpio_pad *pad;
284         unsigned arg;
285
286         pad = pctldev->desc->pins[pin].drv_data;
287
288         switch (param) {
289         case PIN_CONFIG_DRIVE_PUSH_PULL:
290                 if (pad->buffer_type != PMIC_GPIO_OUT_BUF_CMOS)
291                         return -EINVAL;
292                 arg = 1;
293                 break;
294         case PIN_CONFIG_DRIVE_OPEN_DRAIN:
295                 if (pad->buffer_type != PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS)
296                         return -EINVAL;
297                 arg = 1;
298                 break;
299         case PIN_CONFIG_DRIVE_OPEN_SOURCE:
300                 if (pad->buffer_type != PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS)
301                         return -EINVAL;
302                 arg = 1;
303                 break;
304         case PIN_CONFIG_BIAS_PULL_DOWN:
305                 if (pad->pullup != PMIC_GPIO_PULL_DOWN)
306                         return -EINVAL;
307                 arg = 1;
308                 break;
309         case PIN_CONFIG_BIAS_DISABLE:
310                 if (pad->pullup != PMIC_GPIO_PULL_DISABLE)
311                         return -EINVAL;
312                 arg = 1;
313                 break;
314         case PIN_CONFIG_BIAS_PULL_UP:
315                 if (pad->pullup != PMIC_GPIO_PULL_UP_30)
316                         return -EINVAL;
317                 arg = 1;
318                 break;
319         case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
320                 if (pad->is_enabled)
321                         return -EINVAL;
322                 arg = 1;
323                 break;
324         case PIN_CONFIG_POWER_SOURCE:
325                 arg = pad->power_source;
326                 break;
327         case PIN_CONFIG_INPUT_ENABLE:
328                 if (!pad->input_enabled)
329                         return -EINVAL;
330                 arg = 1;
331                 break;
332         case PIN_CONFIG_OUTPUT:
333                 arg = pad->out_value;
334                 break;
335         case PMIC_GPIO_CONF_PULL_UP:
336                 arg = pad->pullup;
337                 break;
338         case PMIC_GPIO_CONF_STRENGTH:
339                 arg = pad->strength;
340                 break;
341         default:
342                 return -EINVAL;
343         }
344
345         *config = pinconf_to_config_packed(param, arg);
346         return 0;
347 }
348
349 static int pmic_gpio_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
350                                 unsigned long *configs, unsigned nconfs)
351 {
352         struct pmic_gpio_state *state = pinctrl_dev_get_drvdata(pctldev);
353         struct pmic_gpio_pad *pad;
354         unsigned param, arg;
355         unsigned int val;
356         int i, ret;
357
358         pad = pctldev->desc->pins[pin].drv_data;
359
360         for (i = 0; i < nconfs; i++) {
361                 param = pinconf_to_config_param(configs[i]);
362                 arg = pinconf_to_config_argument(configs[i]);
363
364                 switch (param) {
365                 case PIN_CONFIG_DRIVE_PUSH_PULL:
366                         pad->buffer_type = PMIC_GPIO_OUT_BUF_CMOS;
367                         break;
368                 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
369                         if (!pad->have_buffer)
370                                 return -EINVAL;
371                         pad->buffer_type = PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS;
372                         break;
373                 case PIN_CONFIG_DRIVE_OPEN_SOURCE:
374                         if (!pad->have_buffer)
375                                 return -EINVAL;
376                         pad->buffer_type = PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS;
377                         break;
378                 case PIN_CONFIG_BIAS_DISABLE:
379                         pad->pullup = PMIC_GPIO_PULL_DISABLE;
380                         break;
381                 case PIN_CONFIG_BIAS_PULL_UP:
382                         pad->pullup = PMIC_GPIO_PULL_UP_30;
383                         break;
384                 case PIN_CONFIG_BIAS_PULL_DOWN:
385                         if (arg)
386                                 pad->pullup = PMIC_GPIO_PULL_DOWN;
387                         else
388                                 pad->pullup = PMIC_GPIO_PULL_DISABLE;
389                         break;
390                 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
391                         pad->is_enabled = false;
392                         break;
393                 case PIN_CONFIG_POWER_SOURCE:
394                         if (arg > pad->num_sources)
395                                 return -EINVAL;
396                         pad->power_source = arg;
397                         break;
398                 case PIN_CONFIG_INPUT_ENABLE:
399                         pad->input_enabled = arg ? true : false;
400                         break;
401                 case PIN_CONFIG_OUTPUT:
402                         pad->output_enabled = true;
403                         pad->out_value = arg;
404                         break;
405                 case PMIC_GPIO_CONF_PULL_UP:
406                         if (arg > PMIC_GPIO_PULL_UP_1P5_30)
407                                 return -EINVAL;
408                         pad->pullup = arg;
409                         break;
410                 case PMIC_GPIO_CONF_STRENGTH:
411                         if (arg > PMIC_GPIO_STRENGTH_LOW)
412                                 return -EINVAL;
413                         pad->strength = arg;
414                         break;
415                 default:
416                         return -EINVAL;
417                 }
418         }
419
420         val = pad->power_source << PMIC_GPIO_REG_VIN_SHIFT;
421
422         ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_VIN_CTL, val);
423         if (ret < 0)
424                 return ret;
425
426         val = pad->pullup << PMIC_GPIO_REG_PULL_SHIFT;
427
428         ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_PULL_CTL, val);
429         if (ret < 0)
430                 return ret;
431
432         val = pad->buffer_type << PMIC_GPIO_REG_OUT_TYPE_SHIFT;
433         val |= pad->strength << PMIC_GPIO_REG_OUT_STRENGTH_SHIFT;
434
435         ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_OUT_CTL, val);
436         if (ret < 0)
437                 return ret;
438
439         val = 0;
440         if (pad->output_enabled) {
441                 if (pad->input_enabled)
442                         val = 2;
443                 else
444                         val = 1;
445         }
446
447         val = val << PMIC_GPIO_REG_MODE_DIR_SHIFT;
448         val |= pad->function << PMIC_GPIO_REG_MODE_FUNCTION_SHIFT;
449         val |= pad->out_value & PMIC_GPIO_REG_MODE_VALUE_SHIFT;
450
451         return pmic_gpio_write(state, pad, PMIC_GPIO_REG_MODE_CTL, val);
452 }
453
454 static void pmic_gpio_config_dbg_show(struct pinctrl_dev *pctldev,
455                                       struct seq_file *s, unsigned pin)
456 {
457         struct pmic_gpio_state *state = pinctrl_dev_get_drvdata(pctldev);
458         struct pmic_gpio_pad *pad;
459         int ret, val;
460
461         static const char *const biases[] = {
462                 "pull-up 30uA", "pull-up 1.5uA", "pull-up 31.5uA",
463                 "pull-up 1.5uA + 30uA boost", "pull-down 10uA", "no pull"
464         };
465         static const char *const buffer_types[] = {
466                 "push-pull", "open-drain", "open-source"
467         };
468         static const char *const strengths[] = {
469                 "no", "high", "medium", "low"
470         };
471
472         pad = pctldev->desc->pins[pin].drv_data;
473
474         seq_printf(s, " gpio%-2d:", pin + PMIC_GPIO_PHYSICAL_OFFSET);
475
476         val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_EN_CTL);
477
478         if (val < 0 || !(val >> PMIC_GPIO_REG_MASTER_EN_SHIFT)) {
479                 seq_puts(s, " ---");
480         } else {
481
482                 if (pad->input_enabled) {
483                         ret = pmic_gpio_read(state, pad, PMIC_MPP_REG_RT_STS);
484                         if (ret < 0)
485                                 return;
486
487                         ret &= PMIC_MPP_REG_RT_STS_VAL_MASK;
488                         pad->out_value = ret;
489                 }
490
491                 seq_printf(s, " %-4s", pad->output_enabled ? "out" : "in");
492                 seq_printf(s, " %-7s", pmic_gpio_functions[pad->function]);
493                 seq_printf(s, " vin-%d", pad->power_source);
494                 seq_printf(s, " %-27s", biases[pad->pullup]);
495                 seq_printf(s, " %-10s", buffer_types[pad->buffer_type]);
496                 seq_printf(s, " %-4s", pad->out_value ? "high" : "low");
497                 seq_printf(s, " %-7s", strengths[pad->strength]);
498         }
499 }
500
501 static const struct pinconf_ops pmic_gpio_pinconf_ops = {
502         .is_generic                     = true,
503         .pin_config_group_get           = pmic_gpio_config_get,
504         .pin_config_group_set           = pmic_gpio_config_set,
505         .pin_config_group_dbg_show      = pmic_gpio_config_dbg_show,
506 };
507
508 static int pmic_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
509 {
510         struct pmic_gpio_state *state = gpiochip_get_data(chip);
511         unsigned long config;
512
513         config = pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE, 1);
514
515         return pmic_gpio_config_set(state->ctrl, pin, &config, 1);
516 }
517
518 static int pmic_gpio_direction_output(struct gpio_chip *chip,
519                                       unsigned pin, int val)
520 {
521         struct pmic_gpio_state *state = gpiochip_get_data(chip);
522         unsigned long config;
523
524         config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, val);
525
526         return pmic_gpio_config_set(state->ctrl, pin, &config, 1);
527 }
528
529 static int pmic_gpio_get(struct gpio_chip *chip, unsigned pin)
530 {
531         struct pmic_gpio_state *state = gpiochip_get_data(chip);
532         struct pmic_gpio_pad *pad;
533         int ret;
534
535         pad = state->ctrl->desc->pins[pin].drv_data;
536
537         if (!pad->is_enabled)
538                 return -EINVAL;
539
540         if (pad->input_enabled) {
541                 ret = pmic_gpio_read(state, pad, PMIC_MPP_REG_RT_STS);
542                 if (ret < 0)
543                         return ret;
544
545                 pad->out_value = ret & PMIC_MPP_REG_RT_STS_VAL_MASK;
546         }
547
548         return !!pad->out_value;
549 }
550
551 static void pmic_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
552 {
553         struct pmic_gpio_state *state = gpiochip_get_data(chip);
554         unsigned long config;
555
556         config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, value);
557
558         pmic_gpio_config_set(state->ctrl, pin, &config, 1);
559 }
560
561 static int pmic_gpio_of_xlate(struct gpio_chip *chip,
562                               const struct of_phandle_args *gpio_desc,
563                               u32 *flags)
564 {
565         if (chip->of_gpio_n_cells < 2)
566                 return -EINVAL;
567
568         if (flags)
569                 *flags = gpio_desc->args[1];
570
571         return gpio_desc->args[0] - PMIC_GPIO_PHYSICAL_OFFSET;
572 }
573
574 static int pmic_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
575 {
576         struct pmic_gpio_state *state = gpiochip_get_data(chip);
577         struct pmic_gpio_pad *pad;
578
579         pad = state->ctrl->desc->pins[pin].drv_data;
580
581         return pad->irq;
582 }
583
584 static void pmic_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
585 {
586         struct pmic_gpio_state *state = gpiochip_get_data(chip);
587         unsigned i;
588
589         for (i = 0; i < chip->ngpio; i++) {
590                 pmic_gpio_config_dbg_show(state->ctrl, s, i);
591                 seq_puts(s, "\n");
592         }
593 }
594
595 static const struct gpio_chip pmic_gpio_gpio_template = {
596         .direction_input        = pmic_gpio_direction_input,
597         .direction_output       = pmic_gpio_direction_output,
598         .get                    = pmic_gpio_get,
599         .set                    = pmic_gpio_set,
600         .request                = gpiochip_generic_request,
601         .free                   = gpiochip_generic_free,
602         .of_xlate               = pmic_gpio_of_xlate,
603         .to_irq                 = pmic_gpio_to_irq,
604         .dbg_show               = pmic_gpio_dbg_show,
605 };
606
607 static int pmic_gpio_populate(struct pmic_gpio_state *state,
608                               struct pmic_gpio_pad *pad)
609 {
610         int type, subtype, val, dir;
611
612         type = pmic_gpio_read(state, pad, PMIC_GPIO_REG_TYPE);
613         if (type < 0)
614                 return type;
615
616         if (type != PMIC_GPIO_TYPE) {
617                 dev_err(state->dev, "incorrect block type 0x%x at 0x%x\n",
618                         type, pad->base);
619                 return -ENODEV;
620         }
621
622         subtype = pmic_gpio_read(state, pad, PMIC_GPIO_REG_SUBTYPE);
623         if (subtype < 0)
624                 return subtype;
625
626         switch (subtype) {
627         case PMIC_GPIO_SUBTYPE_GPIO_4CH:
628                 pad->have_buffer = true;
629         case PMIC_GPIO_SUBTYPE_GPIOC_4CH:
630                 pad->num_sources = 4;
631                 break;
632         case PMIC_GPIO_SUBTYPE_GPIO_8CH:
633                 pad->have_buffer = true;
634         case PMIC_GPIO_SUBTYPE_GPIOC_8CH:
635                 pad->num_sources = 8;
636                 break;
637         default:
638                 dev_err(state->dev, "unknown GPIO type 0x%x\n", subtype);
639                 return -ENODEV;
640         }
641
642         val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_MODE_CTL);
643         if (val < 0)
644                 return val;
645
646         pad->out_value = val & PMIC_GPIO_REG_MODE_VALUE_SHIFT;
647
648         dir = val >> PMIC_GPIO_REG_MODE_DIR_SHIFT;
649         dir &= PMIC_GPIO_REG_MODE_DIR_MASK;
650         switch (dir) {
651         case 0:
652                 pad->input_enabled = true;
653                 pad->output_enabled = false;
654                 break;
655         case 1:
656                 pad->input_enabled = false;
657                 pad->output_enabled = true;
658                 break;
659         case 2:
660                 pad->input_enabled = true;
661                 pad->output_enabled = true;
662                 break;
663         default:
664                 dev_err(state->dev, "unknown GPIO direction\n");
665                 return -ENODEV;
666         }
667
668         pad->function = val >> PMIC_GPIO_REG_MODE_FUNCTION_SHIFT;
669         pad->function &= PMIC_GPIO_REG_MODE_FUNCTION_MASK;
670
671         val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_VIN_CTL);
672         if (val < 0)
673                 return val;
674
675         pad->power_source = val >> PMIC_GPIO_REG_VIN_SHIFT;
676         pad->power_source &= PMIC_GPIO_REG_VIN_MASK;
677
678         val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_PULL_CTL);
679         if (val < 0)
680                 return val;
681
682         pad->pullup = val >> PMIC_GPIO_REG_PULL_SHIFT;
683         pad->pullup &= PMIC_GPIO_REG_PULL_MASK;
684
685         val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_OUT_CTL);
686         if (val < 0)
687                 return val;
688
689         pad->strength = val >> PMIC_GPIO_REG_OUT_STRENGTH_SHIFT;
690         pad->strength &= PMIC_GPIO_REG_OUT_STRENGTH_MASK;
691
692         pad->buffer_type = val >> PMIC_GPIO_REG_OUT_TYPE_SHIFT;
693         pad->buffer_type &= PMIC_GPIO_REG_OUT_TYPE_MASK;
694
695         /* Pin could be disabled with PIN_CONFIG_BIAS_HIGH_IMPEDANCE */
696         pad->is_enabled = true;
697         return 0;
698 }
699
700 static int pmic_gpio_probe(struct platform_device *pdev)
701 {
702         struct device *dev = &pdev->dev;
703         struct pinctrl_pin_desc *pindesc;
704         struct pinctrl_desc *pctrldesc;
705         struct pmic_gpio_pad *pad, *pads;
706         struct pmic_gpio_state *state;
707         int ret, npins, i;
708         u32 reg;
709
710         ret = of_property_read_u32(dev->of_node, "reg", &reg);
711         if (ret < 0) {
712                 dev_err(dev, "missing base address");
713                 return ret;
714         }
715
716         npins = platform_irq_count(pdev);
717         if (!npins)
718                 return -EINVAL;
719         if (npins < 0)
720                 return npins;
721
722         BUG_ON(npins > ARRAY_SIZE(pmic_gpio_groups));
723
724         state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
725         if (!state)
726                 return -ENOMEM;
727
728         platform_set_drvdata(pdev, state);
729
730         state->dev = &pdev->dev;
731         state->map = dev_get_regmap(dev->parent, NULL);
732
733         pindesc = devm_kcalloc(dev, npins, sizeof(*pindesc), GFP_KERNEL);
734         if (!pindesc)
735                 return -ENOMEM;
736
737         pads = devm_kcalloc(dev, npins, sizeof(*pads), GFP_KERNEL);
738         if (!pads)
739                 return -ENOMEM;
740
741         pctrldesc = devm_kzalloc(dev, sizeof(*pctrldesc), GFP_KERNEL);
742         if (!pctrldesc)
743                 return -ENOMEM;
744
745         pctrldesc->pctlops = &pmic_gpio_pinctrl_ops;
746         pctrldesc->pmxops = &pmic_gpio_pinmux_ops;
747         pctrldesc->confops = &pmic_gpio_pinconf_ops;
748         pctrldesc->owner = THIS_MODULE;
749         pctrldesc->name = dev_name(dev);
750         pctrldesc->pins = pindesc;
751         pctrldesc->npins = npins;
752         pctrldesc->num_custom_params = ARRAY_SIZE(pmic_gpio_bindings);
753         pctrldesc->custom_params = pmic_gpio_bindings;
754 #ifdef CONFIG_DEBUG_FS
755         pctrldesc->custom_conf_items = pmic_conf_items;
756 #endif
757
758         for (i = 0; i < npins; i++, pindesc++) {
759                 pad = &pads[i];
760                 pindesc->drv_data = pad;
761                 pindesc->number = i;
762                 pindesc->name = pmic_gpio_groups[i];
763
764                 pad->irq = platform_get_irq(pdev, i);
765                 if (pad->irq < 0)
766                         return pad->irq;
767
768                 pad->base = reg + i * PMIC_GPIO_ADDRESS_RANGE;
769
770                 ret = pmic_gpio_populate(state, pad);
771                 if (ret < 0)
772                         return ret;
773         }
774
775         state->chip = pmic_gpio_gpio_template;
776         state->chip.parent = dev;
777         state->chip.base = -1;
778         state->chip.ngpio = npins;
779         state->chip.label = dev_name(dev);
780         state->chip.of_gpio_n_cells = 2;
781         state->chip.can_sleep = false;
782
783         state->ctrl = devm_pinctrl_register(dev, pctrldesc, state);
784         if (IS_ERR(state->ctrl))
785                 return PTR_ERR(state->ctrl);
786
787         ret = gpiochip_add_data(&state->chip, state);
788         if (ret) {
789                 dev_err(state->dev, "can't add gpio chip\n");
790                 return ret;
791         }
792
793         /*
794          * For DeviceTree-supported systems, the gpio core checks the
795          * pinctrl's device node for the "gpio-ranges" property.
796          * If it is present, it takes care of adding the pin ranges
797          * for the driver. In this case the driver can skip ahead.
798          *
799          * In order to remain compatible with older, existing DeviceTree
800          * files which don't set the "gpio-ranges" property or systems that
801          * utilize ACPI the driver has to call gpiochip_add_pin_range().
802          */
803         if (!of_property_read_bool(dev->of_node, "gpio-ranges")) {
804                 ret = gpiochip_add_pin_range(&state->chip, dev_name(dev), 0, 0,
805                                              npins);
806                 if (ret) {
807                         dev_err(dev, "failed to add pin range\n");
808                         goto err_range;
809                 }
810         }
811
812         return 0;
813
814 err_range:
815         gpiochip_remove(&state->chip);
816         return ret;
817 }
818
819 static int pmic_gpio_remove(struct platform_device *pdev)
820 {
821         struct pmic_gpio_state *state = platform_get_drvdata(pdev);
822
823         gpiochip_remove(&state->chip);
824         return 0;
825 }
826
827 static const struct of_device_id pmic_gpio_of_match[] = {
828         { .compatible = "qcom,pm8916-gpio" },   /* 4 GPIO's */
829         { .compatible = "qcom,pm8941-gpio" },   /* 36 GPIO's */
830         { .compatible = "qcom,pm8994-gpio" },   /* 22 GPIO's */
831         { .compatible = "qcom,pma8084-gpio" },  /* 22 GPIO's */
832         { .compatible = "qcom,spmi-gpio" }, /* Generic */
833         { },
834 };
835
836 MODULE_DEVICE_TABLE(of, pmic_gpio_of_match);
837
838 static struct platform_driver pmic_gpio_driver = {
839         .driver = {
840                    .name = "qcom-spmi-gpio",
841                    .of_match_table = pmic_gpio_of_match,
842         },
843         .probe  = pmic_gpio_probe,
844         .remove = pmic_gpio_remove,
845 };
846
847 module_platform_driver(pmic_gpio_driver);
848
849 MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");
850 MODULE_DESCRIPTION("Qualcomm SPMI PMIC GPIO pin control driver");
851 MODULE_ALIAS("platform:qcom-spmi-gpio");
852 MODULE_LICENSE("GPL v2");