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