blob: a535075c8b69f0aee1380ebc4fca94810dae5a69 [file] [log] [blame]
Paul Mundtb3c185a2012-06-20 17:29:04 +09001/*
2 * SuperH Pin Function Controller GPIO driver.
3 *
4 * Copyright (C) 2008 Magnus Damm
5 * Copyright (C) 2009 - 2012 Paul Mundt
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
9 * for more details.
10 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +010011
12#define pr_fmt(fmt) KBUILD_MODNAME " gpio: " fmt
Paul Mundtb3c185a2012-06-20 17:29:04 +090013
Laurent Pinchart1724acf2012-12-15 23:50:48 +010014#include <linux/device.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090015#include <linux/gpio.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010016#include <linux/init.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090017#include <linux/module.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090018#include <linux/pinctrl/consumer.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010019#include <linux/slab.h>
20#include <linux/spinlock.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090021
Laurent Pinchartf9165132012-12-15 23:50:44 +010022#include "core.h"
23
Paul Mundtb3c185a2012-06-20 17:29:04 +090024struct sh_pfc_chip {
25 struct sh_pfc *pfc;
26 struct gpio_chip gpio_chip;
27};
28
29static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
30{
31 return container_of(gc, struct sh_pfc_chip, gpio_chip);
32}
33
34static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
35{
36 return gpio_to_pfc_chip(gc)->pfc;
37}
38
39static int sh_gpio_request(struct gpio_chip *gc, unsigned offset)
40{
Paul Mundtca5481c62012-07-10 12:08:14 +090041 return pinctrl_request_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +090042}
43
44static void sh_gpio_free(struct gpio_chip *gc, unsigned offset)
45{
Paul Mundtca5481c62012-07-10 12:08:14 +090046 pinctrl_free_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +090047}
48
49static void sh_gpio_set_value(struct sh_pfc *pfc, unsigned gpio, int value)
50{
51 struct pinmux_data_reg *dr = NULL;
52 int bit = 0;
53
Laurent Pincharta99ebec2012-12-15 23:50:51 +010054 if (sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
Paul Mundtb3c185a2012-06-20 17:29:04 +090055 BUG();
56 else
57 sh_pfc_write_bit(dr, bit, value);
58}
59
Paul Mundtb3c185a2012-06-20 17:29:04 +090060static int sh_gpio_get_value(struct sh_pfc *pfc, unsigned gpio)
61{
62 struct pinmux_data_reg *dr = NULL;
63 int bit = 0;
64
Laurent Pincharta99ebec2012-12-15 23:50:51 +010065 if (sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
Paul Mundtb3c185a2012-06-20 17:29:04 +090066 return -EINVAL;
67
68 return sh_pfc_read_bit(dr, bit);
69}
70
Paul Mundtca5481c62012-07-10 12:08:14 +090071static int sh_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
72{
73 return pinctrl_gpio_direction_input(offset);
74}
75
76static int sh_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
77 int value)
78{
79 sh_gpio_set_value(gpio_to_pfc(gc), offset, value);
80
81 return pinctrl_gpio_direction_output(offset);
82}
83
Paul Mundtb3c185a2012-06-20 17:29:04 +090084static int sh_gpio_get(struct gpio_chip *gc, unsigned offset)
85{
86 return sh_gpio_get_value(gpio_to_pfc(gc), offset);
87}
88
89static void sh_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
90{
91 sh_gpio_set_value(gpio_to_pfc(gc), offset, value);
92}
93
94static int sh_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
95{
96 struct sh_pfc *pfc = gpio_to_pfc(gc);
97 pinmux_enum_t enum_id;
98 pinmux_enum_t *enum_ids;
99 int i, k, pos;
100
101 pos = 0;
102 enum_id = 0;
103 while (1) {
104 pos = sh_pfc_gpio_to_enum(pfc, offset, pos, &enum_id);
105 if (pos <= 0 || !enum_id)
106 break;
107
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100108 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
109 enum_ids = pfc->info->gpio_irq[i].enum_ids;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900110 for (k = 0; enum_ids[k]; k++) {
111 if (enum_ids[k] == enum_id)
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100112 return pfc->info->gpio_irq[i].irq;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900113 }
114 }
115 }
116
117 return -ENOSYS;
118}
119
120static void sh_pfc_gpio_setup(struct sh_pfc_chip *chip)
121{
122 struct sh_pfc *pfc = chip->pfc;
123 struct gpio_chip *gc = &chip->gpio_chip;
124
125 gc->request = sh_gpio_request;
126 gc->free = sh_gpio_free;
127 gc->direction_input = sh_gpio_direction_input;
128 gc->get = sh_gpio_get;
129 gc->direction_output = sh_gpio_direction_output;
130 gc->set = sh_gpio_set;
131 gc->to_irq = sh_gpio_to_irq;
132
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100133 WARN_ON(pfc->info->first_gpio != 0); /* needs testing */
Paul Mundtb3c185a2012-06-20 17:29:04 +0900134
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100135 gc->label = pfc->info->name;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900136 gc->owner = THIS_MODULE;
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100137 gc->base = pfc->info->first_gpio;
138 gc->ngpio = (pfc->info->last_gpio - pfc->info->first_gpio) + 1;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900139}
140
141int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
142{
143 struct sh_pfc_chip *chip;
144 int ret;
145
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100146 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900147 if (unlikely(!chip))
148 return -ENOMEM;
149
150 chip->pfc = pfc;
151
152 sh_pfc_gpio_setup(chip);
153
154 ret = gpiochip_add(&chip->gpio_chip);
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100155 if (unlikely(ret < 0))
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100156 return ret;
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100157
158 pfc->gpio = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900159
160 pr_info("%s handling gpio %d -> %d\n",
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100161 pfc->info->name, pfc->info->first_gpio,
162 pfc->info->last_gpio);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900163
Paul Mundtb3c185a2012-06-20 17:29:04 +0900164 return 0;
165}
166
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100167int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900168{
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100169 struct sh_pfc_chip *chip = pfc->gpio;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900170 int ret;
171
172 ret = gpiochip_remove(&chip->gpio_chip);
173 if (unlikely(ret < 0))
174 return ret;
175
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100176 pfc->gpio = NULL;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900177 return 0;
178}