Root/drivers/bcma/driver_gpio.c

1/*
2 * Broadcom specific AMBA
3 * GPIO driver
4 *
5 * Copyright 2011, Broadcom Corporation
6 * Copyright 2012, Hauke Mehrtens <hauke@hauke-m.de>
7 *
8 * Licensed under the GNU/GPL. See COPYING for details.
9 */
10
11#include <linux/gpio.h>
12#include <linux/export.h>
13#include <linux/bcma/bcma.h>
14
15#include "bcma_private.h"
16
17static inline struct bcma_drv_cc *bcma_gpio_get_cc(struct gpio_chip *chip)
18{
19    return container_of(chip, struct bcma_drv_cc, gpio);
20}
21
22static int bcma_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
23{
24    struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
25
26    return !!bcma_chipco_gpio_in(cc, 1 << gpio);
27}
28
29static void bcma_gpio_set_value(struct gpio_chip *chip, unsigned gpio,
30                int value)
31{
32    struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
33
34    bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
35}
36
37static int bcma_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
38{
39    struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
40
41    bcma_chipco_gpio_outen(cc, 1 << gpio, 0);
42    return 0;
43}
44
45static int bcma_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
46                      int value)
47{
48    struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
49
50    bcma_chipco_gpio_outen(cc, 1 << gpio, 1 << gpio);
51    bcma_chipco_gpio_out(cc, 1 << gpio, value ? 1 << gpio : 0);
52    return 0;
53}
54
55static int bcma_gpio_request(struct gpio_chip *chip, unsigned gpio)
56{
57    struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
58
59    bcma_chipco_gpio_control(cc, 1 << gpio, 0);
60    /* clear pulldown */
61    bcma_chipco_gpio_pulldown(cc, 1 << gpio, 0);
62    /* Set pullup */
63    bcma_chipco_gpio_pullup(cc, 1 << gpio, 1 << gpio);
64
65    return 0;
66}
67
68static void bcma_gpio_free(struct gpio_chip *chip, unsigned gpio)
69{
70    struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
71
72    /* clear pullup */
73    bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
74}
75
76static int bcma_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
77{
78    struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
79
80    if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
81        return bcma_core_irq(cc->core);
82    else
83        return -EINVAL;
84}
85
86int bcma_gpio_init(struct bcma_drv_cc *cc)
87{
88    struct gpio_chip *chip = &cc->gpio;
89
90    chip->label = "bcma_gpio";
91    chip->owner = THIS_MODULE;
92    chip->request = bcma_gpio_request;
93    chip->free = bcma_gpio_free;
94    chip->get = bcma_gpio_get_value;
95    chip->set = bcma_gpio_set_value;
96    chip->direction_input = bcma_gpio_direction_input;
97    chip->direction_output = bcma_gpio_direction_output;
98    chip->to_irq = bcma_gpio_to_irq;
99    chip->ngpio = 16;
100    /* There is just one SoC in one device and its GPIO addresses should be
101     * deterministic to address them more easily. The other buses could get
102     * a random base number. */
103    if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
104        chip->base = 0;
105    else
106        chip->base = -1;
107
108    return gpiochip_add(chip);
109}
110
111int bcma_gpio_unregister(struct bcma_drv_cc *cc)
112{
113    return gpiochip_remove(&cc->gpio);
114}
115

Archive Download this file



interactive