Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
devres.c
Go to the documentation of this file.
1 /*
2  * drivers/gpio/devres.c - managed gpio resources
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
6  * as published by the Free Software Foundation.
7  *
8  * You should have received a copy of the GNU General Public License
9  * along with this program; if not, write to the Free Software
10  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
11  *
12  * This file is based on kernel/irq/devres.c
13  *
14  * Copyright (c) 2011 John Crispin <[email protected]>
15  */
16 
17 #include <linux/module.h>
18 #include <linux/gpio.h>
19 #include <linux/device.h>
20 #include <linux/gfp.h>
21 
22 static void devm_gpio_release(struct device *dev, void *res)
23 {
24  unsigned *gpio = res;
25 
26  gpio_free(*gpio);
27 }
28 
29 static int devm_gpio_match(struct device *dev, void *res, void *data)
30 {
31  unsigned *this = res, *gpio = data;
32 
33  return *this == *gpio;
34 }
35 
51 int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
52 {
53  unsigned *dr;
54  int rc;
55 
56  dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
57  if (!dr)
58  return -ENOMEM;
59 
60  rc = gpio_request(gpio, label);
61  if (rc) {
62  devres_free(dr);
63  return rc;
64  }
65 
66  *dr = gpio;
67  devres_add(dev, dr);
68 
69  return 0;
70 }
72 
80 int devm_gpio_request_one(struct device *dev, unsigned gpio,
81  unsigned long flags, const char *label)
82 {
83  unsigned *dr;
84  int rc;
85 
86  dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
87  if (!dr)
88  return -ENOMEM;
89 
90  rc = gpio_request_one(gpio, flags, label);
91  if (rc) {
92  devres_free(dr);
93  return rc;
94  }
95 
96  *dr = gpio;
97  devres_add(dev, dr);
98 
99  return 0;
100 }
102 
113 void devm_gpio_free(struct device *dev, unsigned int gpio)
114 {
115 
116  WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match,
117  &gpio));
118 }