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