Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpio/powerpc: Eliminate duplication of of_get_named_gpio_flags()

A large chunk of qe_pin_request() is unnecessarily cut-and-paste
directly from of_get_named_gpio_flags(). This patch cuts out the
duplicate code and replaces it with a call to of_get_gpio().

v2: fixed compile error due to missing gpio_to_chip()

Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Kumar Gala <galak@kernel.crashing.org>

+10 -35
+8 -34
arch/powerpc/sysdev/qe_lib/gpio.c
··· 139 139 struct qe_pin *qe_pin_request(struct device_node *np, int index) 140 140 { 141 141 struct qe_pin *qe_pin; 142 - struct device_node *gpio_np; 143 142 struct gpio_chip *gc; 144 143 struct of_mm_gpio_chip *mm_gc; 145 144 struct qe_gpio_chip *qe_gc; 146 145 int err; 147 - int size; 148 - const void *gpio_spec; 149 - const u32 *gpio_cells; 150 146 unsigned long flags; 151 147 152 148 qe_pin = kzalloc(sizeof(*qe_pin), GFP_KERNEL); ··· 151 155 return ERR_PTR(-ENOMEM); 152 156 } 153 157 154 - err = of_parse_phandles_with_args(np, "gpios", "#gpio-cells", index, 155 - &gpio_np, &gpio_spec); 156 - if (err) { 157 - pr_debug("%s: can't parse gpios property\n", __func__); 158 + err = of_get_gpio(np, index); 159 + if (err < 0) 158 160 goto err0; 159 - } 161 + gc = gpio_to_chip(err); 162 + if (WARN_ON(!gc)) 163 + goto err0; 160 164 161 - if (!of_device_is_compatible(gpio_np, "fsl,mpc8323-qe-pario-bank")) { 165 + if (!of_device_is_compatible(gc->of_node, "fsl,mpc8323-qe-pario-bank")) { 162 166 pr_debug("%s: tried to get a non-qe pin\n", __func__); 163 167 err = -EINVAL; 164 - goto err1; 168 + goto err0; 165 169 } 166 - 167 - gc = of_node_to_gpiochip(gpio_np); 168 - if (!gc) { 169 - pr_debug("%s: gpio controller %s isn't registered\n", 170 - np->full_name, gpio_np->full_name); 171 - err = -ENODEV; 172 - goto err1; 173 - } 174 - 175 - gpio_cells = of_get_property(gpio_np, "#gpio-cells", &size); 176 - if (!gpio_cells || size != sizeof(*gpio_cells) || 177 - *gpio_cells != gc->of_gpio_n_cells) { 178 - pr_debug("%s: wrong #gpio-cells for %s\n", 179 - np->full_name, gpio_np->full_name); 180 - err = -EINVAL; 181 - goto err1; 182 - } 183 - 184 - err = gc->of_xlate(gc, np, gpio_spec, NULL); 185 - if (err < 0) 186 - goto err1; 187 170 188 171 mm_gc = to_of_mm_gpio_chip(gc); 189 172 qe_gc = to_qe_gpio_chip(mm_gc); 190 173 191 174 spin_lock_irqsave(&qe_gc->lock, flags); 192 175 176 + err -= gc->base; 193 177 if (test_and_set_bit(QE_PIN_REQUESTED, &qe_gc->pin_flags[err]) == 0) { 194 178 qe_pin->controller = qe_gc; 195 179 qe_pin->num = err; ··· 182 206 183 207 if (!err) 184 208 return qe_pin; 185 - err1: 186 - of_node_put(gpio_np); 187 209 err0: 188 210 kfree(qe_pin); 189 211 pr_debug("%s failed with status %d\n", __func__, err);
+1 -1
drivers/gpio/gpiolib.c
··· 114 114 } 115 115 116 116 /* caller holds gpio_lock *OR* gpio is marked as requested */ 117 - static inline struct gpio_chip *gpio_to_chip(unsigned gpio) 117 + struct gpio_chip *gpio_to_chip(unsigned gpio) 118 118 { 119 119 return gpio_desc[gpio].chip; 120 120 }
+1
include/asm-generic/gpio.h
··· 135 135 136 136 extern const char *gpiochip_is_requested(struct gpio_chip *chip, 137 137 unsigned offset); 138 + extern struct gpio_chip *gpio_to_chip(unsigned gpio); 138 139 extern int __must_check gpiochip_reserve(int start, int ngpio); 139 140 140 141 /* add/remove chips */