diff options
author | Bartosz Golaszewski <bgolaszewski@baylibre.com> | 2017-02-06 07:10:35 -0500 |
---|---|---|
committer | Linus Walleij <linus.walleij@linaro.org> | 2017-02-06 08:20:12 -0500 |
commit | ca4091607847d25778db1b701a6e14dcc87a55ff (patch) | |
tree | e65cd027cd199aba75b6c1f80d66b4e8ffbee26b | |
parent | 02e74fc0401ae3f952423b04bea773195f2372ce (diff) |
gpio: mockup: readability tweaks
The following patch tries to improve the readability of the mockup
driver.
The driver is called gpio-mockup, so add the same prefix to all
functions and structures.
Add some newlines and use a temporary pointer in gpio_mockup_add().
Drop the name of the direction enum and rename the enum values to
better reflect their purpose.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
-rw-r--r-- | drivers/gpio/gpio-mockup.c | 123 |
1 files changed, 66 insertions, 57 deletions
diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c index 82a9efd021db..5f6ed4b24b75 100644 --- a/drivers/gpio/gpio-mockup.c +++ b/drivers/gpio/gpio-mockup.c | |||
@@ -16,12 +16,12 @@ | |||
16 | #include <linux/gpio/driver.h> | 16 | #include <linux/gpio/driver.h> |
17 | #include <linux/platform_device.h> | 17 | #include <linux/platform_device.h> |
18 | 18 | ||
19 | #define GPIO_NAME "gpio-mockup" | 19 | #define GPIO_MOCKUP_NAME "gpio-mockup" |
20 | #define MAX_GC 10 | 20 | #define GPIO_MOCKUP_MAX_GC 10 |
21 | 21 | ||
22 | enum direction { | 22 | enum { |
23 | OUT, | 23 | DIR_IN = 0, |
24 | IN | 24 | DIR_OUT, |
25 | }; | 25 | }; |
26 | 26 | ||
27 | /* | 27 | /* |
@@ -29,98 +29,104 @@ enum direction { | |||
29 | * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out | 29 | * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out |
30 | * @value: Configures status of the gpio as 0(low) or 1(high) | 30 | * @value: Configures status of the gpio as 0(low) or 1(high) |
31 | */ | 31 | */ |
32 | struct gpio_pin_status { | 32 | struct gpio_mockup_line_status { |
33 | enum direction dir; | 33 | int dir; |
34 | bool value; | 34 | bool value; |
35 | }; | 35 | }; |
36 | 36 | ||
37 | struct mockup_gpio_controller { | 37 | struct gpio_mockup_chip { |
38 | struct gpio_chip gc; | 38 | struct gpio_chip gc; |
39 | struct gpio_pin_status *stats; | 39 | struct gpio_mockup_line_status *lines; |
40 | }; | 40 | }; |
41 | 41 | ||
42 | static int gpio_mockup_ranges[MAX_GC << 1]; | 42 | static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC << 1]; |
43 | static int gpio_mockup_params_nr; | 43 | static int gpio_mockup_params_nr; |
44 | module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400); | 44 | module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400); |
45 | 45 | ||
46 | static const char pins_name_start = 'A'; | 46 | static const char gpio_mockup_name_start = 'A'; |
47 | 47 | ||
48 | static int mockup_gpio_get(struct gpio_chip *gc, unsigned int offset) | 48 | static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset) |
49 | { | 49 | { |
50 | struct mockup_gpio_controller *cntr = gpiochip_get_data(gc); | 50 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
51 | 51 | ||
52 | return cntr->stats[offset].value; | 52 | return chip->lines[offset].value; |
53 | } | 53 | } |
54 | 54 | ||
55 | static void mockup_gpio_set(struct gpio_chip *gc, unsigned int offset, | 55 | static void gpio_mockup_set(struct gpio_chip *gc, unsigned int offset, |
56 | int value) | 56 | int value) |
57 | { | 57 | { |
58 | struct mockup_gpio_controller *cntr = gpiochip_get_data(gc); | 58 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
59 | 59 | ||
60 | cntr->stats[offset].value = !!value; | 60 | chip->lines[offset].value = !!value; |
61 | } | 61 | } |
62 | 62 | ||
63 | static int mockup_gpio_dirout(struct gpio_chip *gc, unsigned int offset, | 63 | static int gpio_mockup_dirout(struct gpio_chip *gc, unsigned int offset, |
64 | int value) | 64 | int value) |
65 | { | 65 | { |
66 | struct mockup_gpio_controller *cntr = gpiochip_get_data(gc); | 66 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
67 | |||
68 | gpio_mockup_set(gc, offset, value); | ||
69 | chip->lines[offset].dir = DIR_OUT; | ||
67 | 70 | ||
68 | mockup_gpio_set(gc, offset, value); | ||
69 | cntr->stats[offset].dir = OUT; | ||
70 | return 0; | 71 | return 0; |
71 | } | 72 | } |
72 | 73 | ||
73 | static int mockup_gpio_dirin(struct gpio_chip *gc, unsigned int offset) | 74 | static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset) |
74 | { | 75 | { |
75 | struct mockup_gpio_controller *cntr = gpiochip_get_data(gc); | 76 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
77 | |||
78 | chip->lines[offset].dir = DIR_IN; | ||
76 | 79 | ||
77 | cntr->stats[offset].dir = IN; | ||
78 | return 0; | 80 | return 0; |
79 | } | 81 | } |
80 | 82 | ||
81 | static int mockup_gpio_get_direction(struct gpio_chip *gc, unsigned int offset) | 83 | static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset) |
82 | { | 84 | { |
83 | struct mockup_gpio_controller *cntr = gpiochip_get_data(gc); | 85 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
84 | 86 | ||
85 | return cntr->stats[offset].dir; | 87 | return chip->lines[offset].dir; |
86 | } | 88 | } |
87 | 89 | ||
88 | static int mockup_gpio_add(struct device *dev, | 90 | static int gpio_mockup_add(struct device *dev, |
89 | struct mockup_gpio_controller *cntr, | 91 | struct gpio_mockup_chip *chip, |
90 | const char *name, int base, int ngpio) | 92 | const char *name, int base, int ngpio) |
91 | { | 93 | { |
94 | struct gpio_chip *gc = &chip->gc; | ||
92 | int ret; | 95 | int ret; |
93 | 96 | ||
94 | cntr->gc.base = base; | 97 | gc->base = base; |
95 | cntr->gc.ngpio = ngpio; | 98 | gc->ngpio = ngpio; |
96 | cntr->gc.label = name; | 99 | gc->label = name; |
97 | cntr->gc.owner = THIS_MODULE; | 100 | gc->owner = THIS_MODULE; |
98 | cntr->gc.parent = dev; | 101 | gc->parent = dev; |
99 | cntr->gc.get = mockup_gpio_get; | 102 | gc->get = gpio_mockup_get; |
100 | cntr->gc.set = mockup_gpio_set; | 103 | gc->set = gpio_mockup_set; |
101 | cntr->gc.direction_output = mockup_gpio_dirout; | 104 | gc->direction_output = gpio_mockup_dirout; |
102 | cntr->gc.direction_input = mockup_gpio_dirin; | 105 | gc->direction_input = gpio_mockup_dirin; |
103 | cntr->gc.get_direction = mockup_gpio_get_direction; | 106 | gc->get_direction = gpio_mockup_get_direction; |
104 | cntr->stats = devm_kzalloc(dev, sizeof(*cntr->stats) * cntr->gc.ngpio, | 107 | |
108 | chip->lines = devm_kzalloc(dev, sizeof(*chip->lines) * gc->ngpio, | ||
105 | GFP_KERNEL); | 109 | GFP_KERNEL); |
106 | if (!cntr->stats) { | 110 | if (!chip->lines) { |
107 | ret = -ENOMEM; | 111 | ret = -ENOMEM; |
108 | goto err; | 112 | goto err; |
109 | } | 113 | } |
110 | ret = devm_gpiochip_add_data(dev, &cntr->gc, cntr); | 114 | |
115 | ret = devm_gpiochip_add_data(dev, &chip->gc, chip); | ||
111 | if (ret) | 116 | if (ret) |
112 | goto err; | 117 | goto err; |
113 | 118 | ||
114 | dev_info(dev, "gpio<%d..%d> add successful!", base, base + ngpio); | 119 | dev_info(dev, "gpio<%d..%d> add successful!", base, base + ngpio); |
115 | return 0; | 120 | return 0; |
121 | |||
116 | err: | 122 | err: |
117 | dev_err(dev, "gpio<%d..%d> add failed!", base, base + ngpio); | 123 | dev_err(dev, "gpio<%d..%d> add failed!", base, base + ngpio); |
118 | return ret; | 124 | return ret; |
119 | } | 125 | } |
120 | 126 | ||
121 | static int mockup_gpio_probe(struct platform_device *pdev) | 127 | static int gpio_mockup_probe(struct platform_device *pdev) |
122 | { | 128 | { |
123 | struct mockup_gpio_controller *cntr; | 129 | struct gpio_mockup_chip *chips; |
124 | struct device *dev = &pdev->dev; | 130 | struct device *dev = &pdev->dev; |
125 | int ret, i, base, ngpio; | 131 | int ret, i, base, ngpio; |
126 | char *chip_name; | 132 | char *chip_name; |
@@ -128,15 +134,17 @@ static int mockup_gpio_probe(struct platform_device *pdev) | |||
128 | if (gpio_mockup_params_nr < 2) | 134 | if (gpio_mockup_params_nr < 2) |
129 | return -EINVAL; | 135 | return -EINVAL; |
130 | 136 | ||
131 | cntr = devm_kzalloc(dev, sizeof(*cntr) * (gpio_mockup_params_nr >> 1), | 137 | chips = devm_kzalloc(dev, |
132 | GFP_KERNEL); | 138 | sizeof(*chips) * (gpio_mockup_params_nr >> 1), |
133 | if (!cntr) | 139 | GFP_KERNEL); |
140 | if (!chips) | ||
134 | return -ENOMEM; | 141 | return -ENOMEM; |
135 | 142 | ||
136 | platform_set_drvdata(pdev, cntr); | 143 | platform_set_drvdata(pdev, chips); |
137 | 144 | ||
138 | for (i = 0; i < gpio_mockup_params_nr >> 1; i++) { | 145 | for (i = 0; i < gpio_mockup_params_nr >> 1; i++) { |
139 | base = gpio_mockup_ranges[i * 2]; | 146 | base = gpio_mockup_ranges[i * 2]; |
147 | |||
140 | if (base == -1) | 148 | if (base == -1) |
141 | ngpio = gpio_mockup_ranges[i * 2 + 1]; | 149 | ngpio = gpio_mockup_ranges[i * 2 + 1]; |
142 | else | 150 | else |
@@ -144,16 +152,17 @@ static int mockup_gpio_probe(struct platform_device *pdev) | |||
144 | 152 | ||
145 | if (ngpio >= 0) { | 153 | if (ngpio >= 0) { |
146 | chip_name = devm_kasprintf(dev, GFP_KERNEL, | 154 | chip_name = devm_kasprintf(dev, GFP_KERNEL, |
147 | "%s-%c", GPIO_NAME, | 155 | "%s-%c", GPIO_MOCKUP_NAME, |
148 | pins_name_start + i); | 156 | gpio_mockup_name_start + i); |
149 | if (!chip_name) | 157 | if (!chip_name) |
150 | return -ENOMEM; | 158 | return -ENOMEM; |
151 | 159 | ||
152 | ret = mockup_gpio_add(dev, &cntr[i], | 160 | ret = gpio_mockup_add(dev, &chips[i], |
153 | chip_name, base, ngpio); | 161 | chip_name, base, ngpio); |
154 | } else { | 162 | } else { |
155 | ret = -1; | 163 | ret = -1; |
156 | } | 164 | } |
165 | |||
157 | if (ret) { | 166 | if (ret) { |
158 | if (base < 0) | 167 | if (base < 0) |
159 | dev_err(dev, "gpio<%d..%d> add failed\n", | 168 | dev_err(dev, "gpio<%d..%d> add failed\n", |
@@ -169,11 +178,11 @@ static int mockup_gpio_probe(struct platform_device *pdev) | |||
169 | return 0; | 178 | return 0; |
170 | } | 179 | } |
171 | 180 | ||
172 | static struct platform_driver mockup_gpio_driver = { | 181 | static struct platform_driver gpio_mockup_driver = { |
173 | .driver = { | 182 | .driver = { |
174 | .name = GPIO_NAME, | 183 | .name = GPIO_MOCKUP_NAME, |
175 | }, | 184 | }, |
176 | .probe = mockup_gpio_probe, | 185 | .probe = gpio_mockup_probe, |
177 | }; | 186 | }; |
178 | 187 | ||
179 | static struct platform_device *pdev; | 188 | static struct platform_device *pdev; |
@@ -181,7 +190,7 @@ static int __init mock_device_init(void) | |||
181 | { | 190 | { |
182 | int err; | 191 | int err; |
183 | 192 | ||
184 | pdev = platform_device_alloc(GPIO_NAME, -1); | 193 | pdev = platform_device_alloc(GPIO_MOCKUP_NAME, -1); |
185 | if (!pdev) | 194 | if (!pdev) |
186 | return -ENOMEM; | 195 | return -ENOMEM; |
187 | 196 | ||
@@ -191,7 +200,7 @@ static int __init mock_device_init(void) | |||
191 | return err; | 200 | return err; |
192 | } | 201 | } |
193 | 202 | ||
194 | err = platform_driver_register(&mockup_gpio_driver); | 203 | err = platform_driver_register(&gpio_mockup_driver); |
195 | if (err) { | 204 | if (err) { |
196 | platform_device_unregister(pdev); | 205 | platform_device_unregister(pdev); |
197 | return err; | 206 | return err; |
@@ -202,7 +211,7 @@ static int __init mock_device_init(void) | |||
202 | 211 | ||
203 | static void __exit mock_device_exit(void) | 212 | static void __exit mock_device_exit(void) |
204 | { | 213 | { |
205 | platform_driver_unregister(&mockup_gpio_driver); | 214 | platform_driver_unregister(&gpio_mockup_driver); |
206 | platform_device_unregister(pdev); | 215 | platform_device_unregister(pdev); |
207 | } | 216 | } |
208 | 217 | ||