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