Commit d295b129762bf6b2b7541243f496e363580de4a2

Authored by Thierry Reding
1 parent 2132fa8d95

pwm: fix used-uninitialized warning in pwm_get()

Some versions of GCC don't seem no notice that the initialization of the
index variable is tied to that of the chip variable and falsely report
it as potentially being used uninitialized. However, to save anybody
else from tripping over this, we now initialize the index variable
unconditionally.

Originally-by: Stephen Warren <swarren@nvidia.com>
Signed-off-by: Thierry Reding <thierry.reding@avionic-design.de>

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

1 /* 1 /*
2 * Generic pwmlib implementation 2 * Generic pwmlib implementation
3 * 3 *
4 * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de> 4 * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
5 * Copyright (C) 2011-2012 Avionic Design GmbH 5 * Copyright (C) 2011-2012 Avionic Design GmbH
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option) 9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version. 10 * any later version.
11 * 11 *
12 * This program is distributed in the hope that it will be useful, 12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details. 15 * GNU General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to 18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 19 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 */ 20 */
21 21
22 #include <linux/module.h> 22 #include <linux/module.h>
23 #include <linux/pwm.h> 23 #include <linux/pwm.h>
24 #include <linux/radix-tree.h> 24 #include <linux/radix-tree.h>
25 #include <linux/list.h> 25 #include <linux/list.h>
26 #include <linux/mutex.h> 26 #include <linux/mutex.h>
27 #include <linux/err.h> 27 #include <linux/err.h>
28 #include <linux/slab.h> 28 #include <linux/slab.h>
29 #include <linux/device.h> 29 #include <linux/device.h>
30 #include <linux/debugfs.h> 30 #include <linux/debugfs.h>
31 #include <linux/seq_file.h> 31 #include <linux/seq_file.h>
32 32
33 #define MAX_PWMS 1024 33 #define MAX_PWMS 1024
34 34
35 static DEFINE_MUTEX(pwm_lookup_lock); 35 static DEFINE_MUTEX(pwm_lookup_lock);
36 static LIST_HEAD(pwm_lookup_list); 36 static LIST_HEAD(pwm_lookup_list);
37 static DEFINE_MUTEX(pwm_lock); 37 static DEFINE_MUTEX(pwm_lock);
38 static LIST_HEAD(pwm_chips); 38 static LIST_HEAD(pwm_chips);
39 static DECLARE_BITMAP(allocated_pwms, MAX_PWMS); 39 static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
40 static RADIX_TREE(pwm_tree, GFP_KERNEL); 40 static RADIX_TREE(pwm_tree, GFP_KERNEL);
41 41
42 static struct pwm_device *pwm_to_device(unsigned int pwm) 42 static struct pwm_device *pwm_to_device(unsigned int pwm)
43 { 43 {
44 return radix_tree_lookup(&pwm_tree, pwm); 44 return radix_tree_lookup(&pwm_tree, pwm);
45 } 45 }
46 46
47 static int alloc_pwms(int pwm, unsigned int count) 47 static int alloc_pwms(int pwm, unsigned int count)
48 { 48 {
49 unsigned int from = 0; 49 unsigned int from = 0;
50 unsigned int start; 50 unsigned int start;
51 51
52 if (pwm >= MAX_PWMS) 52 if (pwm >= MAX_PWMS)
53 return -EINVAL; 53 return -EINVAL;
54 54
55 if (pwm >= 0) 55 if (pwm >= 0)
56 from = pwm; 56 from = pwm;
57 57
58 start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from, 58 start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from,
59 count, 0); 59 count, 0);
60 60
61 if (pwm >= 0 && start != pwm) 61 if (pwm >= 0 && start != pwm)
62 return -EEXIST; 62 return -EEXIST;
63 63
64 if (start + count > MAX_PWMS) 64 if (start + count > MAX_PWMS)
65 return -ENOSPC; 65 return -ENOSPC;
66 66
67 return start; 67 return start;
68 } 68 }
69 69
70 static void free_pwms(struct pwm_chip *chip) 70 static void free_pwms(struct pwm_chip *chip)
71 { 71 {
72 unsigned int i; 72 unsigned int i;
73 73
74 for (i = 0; i < chip->npwm; i++) { 74 for (i = 0; i < chip->npwm; i++) {
75 struct pwm_device *pwm = &chip->pwms[i]; 75 struct pwm_device *pwm = &chip->pwms[i];
76 radix_tree_delete(&pwm_tree, pwm->pwm); 76 radix_tree_delete(&pwm_tree, pwm->pwm);
77 } 77 }
78 78
79 bitmap_clear(allocated_pwms, chip->base, chip->npwm); 79 bitmap_clear(allocated_pwms, chip->base, chip->npwm);
80 80
81 kfree(chip->pwms); 81 kfree(chip->pwms);
82 chip->pwms = NULL; 82 chip->pwms = NULL;
83 } 83 }
84 84
85 static struct pwm_chip *pwmchip_find_by_name(const char *name) 85 static struct pwm_chip *pwmchip_find_by_name(const char *name)
86 { 86 {
87 struct pwm_chip *chip; 87 struct pwm_chip *chip;
88 88
89 if (!name) 89 if (!name)
90 return NULL; 90 return NULL;
91 91
92 mutex_lock(&pwm_lock); 92 mutex_lock(&pwm_lock);
93 93
94 list_for_each_entry(chip, &pwm_chips, list) { 94 list_for_each_entry(chip, &pwm_chips, list) {
95 const char *chip_name = dev_name(chip->dev); 95 const char *chip_name = dev_name(chip->dev);
96 96
97 if (chip_name && strcmp(chip_name, name) == 0) { 97 if (chip_name && strcmp(chip_name, name) == 0) {
98 mutex_unlock(&pwm_lock); 98 mutex_unlock(&pwm_lock);
99 return chip; 99 return chip;
100 } 100 }
101 } 101 }
102 102
103 mutex_unlock(&pwm_lock); 103 mutex_unlock(&pwm_lock);
104 104
105 return NULL; 105 return NULL;
106 } 106 }
107 107
108 static int pwm_device_request(struct pwm_device *pwm, const char *label) 108 static int pwm_device_request(struct pwm_device *pwm, const char *label)
109 { 109 {
110 int err; 110 int err;
111 111
112 if (test_bit(PWMF_REQUESTED, &pwm->flags)) 112 if (test_bit(PWMF_REQUESTED, &pwm->flags))
113 return -EBUSY; 113 return -EBUSY;
114 114
115 if (!try_module_get(pwm->chip->ops->owner)) 115 if (!try_module_get(pwm->chip->ops->owner))
116 return -ENODEV; 116 return -ENODEV;
117 117
118 if (pwm->chip->ops->request) { 118 if (pwm->chip->ops->request) {
119 err = pwm->chip->ops->request(pwm->chip, pwm); 119 err = pwm->chip->ops->request(pwm->chip, pwm);
120 if (err) { 120 if (err) {
121 module_put(pwm->chip->ops->owner); 121 module_put(pwm->chip->ops->owner);
122 return err; 122 return err;
123 } 123 }
124 } 124 }
125 125
126 set_bit(PWMF_REQUESTED, &pwm->flags); 126 set_bit(PWMF_REQUESTED, &pwm->flags);
127 pwm->label = label; 127 pwm->label = label;
128 128
129 return 0; 129 return 0;
130 } 130 }
131 131
132 static struct pwm_device *of_pwm_simple_xlate(struct pwm_chip *pc, 132 static struct pwm_device *of_pwm_simple_xlate(struct pwm_chip *pc,
133 const struct of_phandle_args *args) 133 const struct of_phandle_args *args)
134 { 134 {
135 struct pwm_device *pwm; 135 struct pwm_device *pwm;
136 136
137 if (pc->of_pwm_n_cells < 2) 137 if (pc->of_pwm_n_cells < 2)
138 return ERR_PTR(-EINVAL); 138 return ERR_PTR(-EINVAL);
139 139
140 if (args->args[0] >= pc->npwm) 140 if (args->args[0] >= pc->npwm)
141 return ERR_PTR(-EINVAL); 141 return ERR_PTR(-EINVAL);
142 142
143 pwm = pwm_request_from_chip(pc, args->args[0], NULL); 143 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
144 if (IS_ERR(pwm)) 144 if (IS_ERR(pwm))
145 return pwm; 145 return pwm;
146 146
147 pwm_set_period(pwm, args->args[1]); 147 pwm_set_period(pwm, args->args[1]);
148 148
149 return pwm; 149 return pwm;
150 } 150 }
151 151
152 void of_pwmchip_add(struct pwm_chip *chip) 152 void of_pwmchip_add(struct pwm_chip *chip)
153 { 153 {
154 if (!chip->dev || !chip->dev->of_node) 154 if (!chip->dev || !chip->dev->of_node)
155 return; 155 return;
156 156
157 if (!chip->of_xlate) { 157 if (!chip->of_xlate) {
158 chip->of_xlate = of_pwm_simple_xlate; 158 chip->of_xlate = of_pwm_simple_xlate;
159 chip->of_pwm_n_cells = 2; 159 chip->of_pwm_n_cells = 2;
160 } 160 }
161 161
162 of_node_get(chip->dev->of_node); 162 of_node_get(chip->dev->of_node);
163 } 163 }
164 164
165 void of_pwmchip_remove(struct pwm_chip *chip) 165 void of_pwmchip_remove(struct pwm_chip *chip)
166 { 166 {
167 if (chip->dev && chip->dev->of_node) 167 if (chip->dev && chip->dev->of_node)
168 of_node_put(chip->dev->of_node); 168 of_node_put(chip->dev->of_node);
169 } 169 }
170 170
171 /** 171 /**
172 * pwm_set_chip_data() - set private chip data for a PWM 172 * pwm_set_chip_data() - set private chip data for a PWM
173 * @pwm: PWM device 173 * @pwm: PWM device
174 * @data: pointer to chip-specific data 174 * @data: pointer to chip-specific data
175 */ 175 */
176 int pwm_set_chip_data(struct pwm_device *pwm, void *data) 176 int pwm_set_chip_data(struct pwm_device *pwm, void *data)
177 { 177 {
178 if (!pwm) 178 if (!pwm)
179 return -EINVAL; 179 return -EINVAL;
180 180
181 pwm->chip_data = data; 181 pwm->chip_data = data;
182 182
183 return 0; 183 return 0;
184 } 184 }
185 185
186 /** 186 /**
187 * pwm_get_chip_data() - get private chip data for a PWM 187 * pwm_get_chip_data() - get private chip data for a PWM
188 * @pwm: PWM device 188 * @pwm: PWM device
189 */ 189 */
190 void *pwm_get_chip_data(struct pwm_device *pwm) 190 void *pwm_get_chip_data(struct pwm_device *pwm)
191 { 191 {
192 return pwm ? pwm->chip_data : NULL; 192 return pwm ? pwm->chip_data : NULL;
193 } 193 }
194 194
195 /** 195 /**
196 * pwmchip_add() - register a new PWM chip 196 * pwmchip_add() - register a new PWM chip
197 * @chip: the PWM chip to add 197 * @chip: the PWM chip to add
198 * 198 *
199 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base 199 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
200 * will be used. 200 * will be used.
201 */ 201 */
202 int pwmchip_add(struct pwm_chip *chip) 202 int pwmchip_add(struct pwm_chip *chip)
203 { 203 {
204 struct pwm_device *pwm; 204 struct pwm_device *pwm;
205 unsigned int i; 205 unsigned int i;
206 int ret; 206 int ret;
207 207
208 if (!chip || !chip->dev || !chip->ops || !chip->ops->config || 208 if (!chip || !chip->dev || !chip->ops || !chip->ops->config ||
209 !chip->ops->enable || !chip->ops->disable) 209 !chip->ops->enable || !chip->ops->disable)
210 return -EINVAL; 210 return -EINVAL;
211 211
212 mutex_lock(&pwm_lock); 212 mutex_lock(&pwm_lock);
213 213
214 ret = alloc_pwms(chip->base, chip->npwm); 214 ret = alloc_pwms(chip->base, chip->npwm);
215 if (ret < 0) 215 if (ret < 0)
216 goto out; 216 goto out;
217 217
218 chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL); 218 chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
219 if (!chip->pwms) { 219 if (!chip->pwms) {
220 ret = -ENOMEM; 220 ret = -ENOMEM;
221 goto out; 221 goto out;
222 } 222 }
223 223
224 chip->base = ret; 224 chip->base = ret;
225 225
226 for (i = 0; i < chip->npwm; i++) { 226 for (i = 0; i < chip->npwm; i++) {
227 pwm = &chip->pwms[i]; 227 pwm = &chip->pwms[i];
228 228
229 pwm->chip = chip; 229 pwm->chip = chip;
230 pwm->pwm = chip->base + i; 230 pwm->pwm = chip->base + i;
231 pwm->hwpwm = i; 231 pwm->hwpwm = i;
232 232
233 radix_tree_insert(&pwm_tree, pwm->pwm, pwm); 233 radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
234 } 234 }
235 235
236 bitmap_set(allocated_pwms, chip->base, chip->npwm); 236 bitmap_set(allocated_pwms, chip->base, chip->npwm);
237 237
238 INIT_LIST_HEAD(&chip->list); 238 INIT_LIST_HEAD(&chip->list);
239 list_add(&chip->list, &pwm_chips); 239 list_add(&chip->list, &pwm_chips);
240 240
241 ret = 0; 241 ret = 0;
242 242
243 if (IS_ENABLED(CONFIG_OF)) 243 if (IS_ENABLED(CONFIG_OF))
244 of_pwmchip_add(chip); 244 of_pwmchip_add(chip);
245 245
246 out: 246 out:
247 mutex_unlock(&pwm_lock); 247 mutex_unlock(&pwm_lock);
248 return ret; 248 return ret;
249 } 249 }
250 EXPORT_SYMBOL_GPL(pwmchip_add); 250 EXPORT_SYMBOL_GPL(pwmchip_add);
251 251
252 /** 252 /**
253 * pwmchip_remove() - remove a PWM chip 253 * pwmchip_remove() - remove a PWM chip
254 * @chip: the PWM chip to remove 254 * @chip: the PWM chip to remove
255 * 255 *
256 * Removes a PWM chip. This function may return busy if the PWM chip provides 256 * Removes a PWM chip. This function may return busy if the PWM chip provides
257 * a PWM device that is still requested. 257 * a PWM device that is still requested.
258 */ 258 */
259 int pwmchip_remove(struct pwm_chip *chip) 259 int pwmchip_remove(struct pwm_chip *chip)
260 { 260 {
261 unsigned int i; 261 unsigned int i;
262 int ret = 0; 262 int ret = 0;
263 263
264 mutex_lock(&pwm_lock); 264 mutex_lock(&pwm_lock);
265 265
266 for (i = 0; i < chip->npwm; i++) { 266 for (i = 0; i < chip->npwm; i++) {
267 struct pwm_device *pwm = &chip->pwms[i]; 267 struct pwm_device *pwm = &chip->pwms[i];
268 268
269 if (test_bit(PWMF_REQUESTED, &pwm->flags)) { 269 if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
270 ret = -EBUSY; 270 ret = -EBUSY;
271 goto out; 271 goto out;
272 } 272 }
273 } 273 }
274 274
275 list_del_init(&chip->list); 275 list_del_init(&chip->list);
276 276
277 if (IS_ENABLED(CONFIG_OF)) 277 if (IS_ENABLED(CONFIG_OF))
278 of_pwmchip_remove(chip); 278 of_pwmchip_remove(chip);
279 279
280 free_pwms(chip); 280 free_pwms(chip);
281 281
282 out: 282 out:
283 mutex_unlock(&pwm_lock); 283 mutex_unlock(&pwm_lock);
284 return ret; 284 return ret;
285 } 285 }
286 EXPORT_SYMBOL_GPL(pwmchip_remove); 286 EXPORT_SYMBOL_GPL(pwmchip_remove);
287 287
288 /** 288 /**
289 * pwm_request() - request a PWM device 289 * pwm_request() - request a PWM device
290 * @pwm_id: global PWM device index 290 * @pwm_id: global PWM device index
291 * @label: PWM device label 291 * @label: PWM device label
292 * 292 *
293 * This function is deprecated, use pwm_get() instead. 293 * This function is deprecated, use pwm_get() instead.
294 */ 294 */
295 struct pwm_device *pwm_request(int pwm, const char *label) 295 struct pwm_device *pwm_request(int pwm, const char *label)
296 { 296 {
297 struct pwm_device *dev; 297 struct pwm_device *dev;
298 int err; 298 int err;
299 299
300 if (pwm < 0 || pwm >= MAX_PWMS) 300 if (pwm < 0 || pwm >= MAX_PWMS)
301 return ERR_PTR(-EINVAL); 301 return ERR_PTR(-EINVAL);
302 302
303 mutex_lock(&pwm_lock); 303 mutex_lock(&pwm_lock);
304 304
305 dev = pwm_to_device(pwm); 305 dev = pwm_to_device(pwm);
306 if (!dev) { 306 if (!dev) {
307 dev = ERR_PTR(-EPROBE_DEFER); 307 dev = ERR_PTR(-EPROBE_DEFER);
308 goto out; 308 goto out;
309 } 309 }
310 310
311 err = pwm_device_request(dev, label); 311 err = pwm_device_request(dev, label);
312 if (err < 0) 312 if (err < 0)
313 dev = ERR_PTR(err); 313 dev = ERR_PTR(err);
314 314
315 out: 315 out:
316 mutex_unlock(&pwm_lock); 316 mutex_unlock(&pwm_lock);
317 317
318 return dev; 318 return dev;
319 } 319 }
320 EXPORT_SYMBOL_GPL(pwm_request); 320 EXPORT_SYMBOL_GPL(pwm_request);
321 321
322 /** 322 /**
323 * pwm_request_from_chip() - request a PWM device relative to a PWM chip 323 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
324 * @chip: PWM chip 324 * @chip: PWM chip
325 * @index: per-chip index of the PWM to request 325 * @index: per-chip index of the PWM to request
326 * @label: a literal description string of this PWM 326 * @label: a literal description string of this PWM
327 * 327 *
328 * Returns the PWM at the given index of the given PWM chip. A negative error 328 * Returns the PWM at the given index of the given PWM chip. A negative error
329 * code is returned if the index is not valid for the specified PWM chip or 329 * code is returned if the index is not valid for the specified PWM chip or
330 * if the PWM device cannot be requested. 330 * if the PWM device cannot be requested.
331 */ 331 */
332 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, 332 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
333 unsigned int index, 333 unsigned int index,
334 const char *label) 334 const char *label)
335 { 335 {
336 struct pwm_device *pwm; 336 struct pwm_device *pwm;
337 int err; 337 int err;
338 338
339 if (!chip || index >= chip->npwm) 339 if (!chip || index >= chip->npwm)
340 return ERR_PTR(-EINVAL); 340 return ERR_PTR(-EINVAL);
341 341
342 mutex_lock(&pwm_lock); 342 mutex_lock(&pwm_lock);
343 pwm = &chip->pwms[index]; 343 pwm = &chip->pwms[index];
344 344
345 err = pwm_device_request(pwm, label); 345 err = pwm_device_request(pwm, label);
346 if (err < 0) 346 if (err < 0)
347 pwm = ERR_PTR(err); 347 pwm = ERR_PTR(err);
348 348
349 mutex_unlock(&pwm_lock); 349 mutex_unlock(&pwm_lock);
350 return pwm; 350 return pwm;
351 } 351 }
352 EXPORT_SYMBOL_GPL(pwm_request_from_chip); 352 EXPORT_SYMBOL_GPL(pwm_request_from_chip);
353 353
354 /** 354 /**
355 * pwm_free() - free a PWM device 355 * pwm_free() - free a PWM device
356 * @pwm: PWM device 356 * @pwm: PWM device
357 * 357 *
358 * This function is deprecated, use pwm_put() instead. 358 * This function is deprecated, use pwm_put() instead.
359 */ 359 */
360 void pwm_free(struct pwm_device *pwm) 360 void pwm_free(struct pwm_device *pwm)
361 { 361 {
362 pwm_put(pwm); 362 pwm_put(pwm);
363 } 363 }
364 EXPORT_SYMBOL_GPL(pwm_free); 364 EXPORT_SYMBOL_GPL(pwm_free);
365 365
366 /** 366 /**
367 * pwm_config() - change a PWM device configuration 367 * pwm_config() - change a PWM device configuration
368 * @pwm: PWM device 368 * @pwm: PWM device
369 * @duty_ns: "on" time (in nanoseconds) 369 * @duty_ns: "on" time (in nanoseconds)
370 * @period_ns: duration (in nanoseconds) of one cycle 370 * @period_ns: duration (in nanoseconds) of one cycle
371 */ 371 */
372 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) 372 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
373 { 373 {
374 if (!pwm || period_ns == 0 || duty_ns > period_ns) 374 if (!pwm || period_ns == 0 || duty_ns > period_ns)
375 return -EINVAL; 375 return -EINVAL;
376 376
377 return pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns); 377 return pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns);
378 } 378 }
379 EXPORT_SYMBOL_GPL(pwm_config); 379 EXPORT_SYMBOL_GPL(pwm_config);
380 380
381 /** 381 /**
382 * pwm_enable() - start a PWM output toggling 382 * pwm_enable() - start a PWM output toggling
383 * @pwm: PWM device 383 * @pwm: PWM device
384 */ 384 */
385 int pwm_enable(struct pwm_device *pwm) 385 int pwm_enable(struct pwm_device *pwm)
386 { 386 {
387 if (pwm && !test_and_set_bit(PWMF_ENABLED, &pwm->flags)) 387 if (pwm && !test_and_set_bit(PWMF_ENABLED, &pwm->flags))
388 return pwm->chip->ops->enable(pwm->chip, pwm); 388 return pwm->chip->ops->enable(pwm->chip, pwm);
389 389
390 return pwm ? 0 : -EINVAL; 390 return pwm ? 0 : -EINVAL;
391 } 391 }
392 EXPORT_SYMBOL_GPL(pwm_enable); 392 EXPORT_SYMBOL_GPL(pwm_enable);
393 393
394 /** 394 /**
395 * pwm_disable() - stop a PWM output toggling 395 * pwm_disable() - stop a PWM output toggling
396 * @pwm: PWM device 396 * @pwm: PWM device
397 */ 397 */
398 void pwm_disable(struct pwm_device *pwm) 398 void pwm_disable(struct pwm_device *pwm)
399 { 399 {
400 if (pwm && test_and_clear_bit(PWMF_ENABLED, &pwm->flags)) 400 if (pwm && test_and_clear_bit(PWMF_ENABLED, &pwm->flags))
401 pwm->chip->ops->disable(pwm->chip, pwm); 401 pwm->chip->ops->disable(pwm->chip, pwm);
402 } 402 }
403 EXPORT_SYMBOL_GPL(pwm_disable); 403 EXPORT_SYMBOL_GPL(pwm_disable);
404 404
405 static struct pwm_chip *of_node_to_pwmchip(struct device_node *np) 405 static struct pwm_chip *of_node_to_pwmchip(struct device_node *np)
406 { 406 {
407 struct pwm_chip *chip; 407 struct pwm_chip *chip;
408 408
409 mutex_lock(&pwm_lock); 409 mutex_lock(&pwm_lock);
410 410
411 list_for_each_entry(chip, &pwm_chips, list) 411 list_for_each_entry(chip, &pwm_chips, list)
412 if (chip->dev && chip->dev->of_node == np) { 412 if (chip->dev && chip->dev->of_node == np) {
413 mutex_unlock(&pwm_lock); 413 mutex_unlock(&pwm_lock);
414 return chip; 414 return chip;
415 } 415 }
416 416
417 mutex_unlock(&pwm_lock); 417 mutex_unlock(&pwm_lock);
418 418
419 return ERR_PTR(-EPROBE_DEFER); 419 return ERR_PTR(-EPROBE_DEFER);
420 } 420 }
421 421
422 /** 422 /**
423 * of_pwm_request() - request a PWM via the PWM framework 423 * of_pwm_request() - request a PWM via the PWM framework
424 * @np: device node to get the PWM from 424 * @np: device node to get the PWM from
425 * @con_id: consumer name 425 * @con_id: consumer name
426 * 426 *
427 * Returns the PWM device parsed from the phandle and index specified in the 427 * Returns the PWM device parsed from the phandle and index specified in the
428 * "pwms" property of a device tree node or a negative error-code on failure. 428 * "pwms" property of a device tree node or a negative error-code on failure.
429 * Values parsed from the device tree are stored in the returned PWM device 429 * Values parsed from the device tree are stored in the returned PWM device
430 * object. 430 * object.
431 * 431 *
432 * If con_id is NULL, the first PWM device listed in the "pwms" property will 432 * If con_id is NULL, the first PWM device listed in the "pwms" property will
433 * be requested. Otherwise the "pwm-names" property is used to do a reverse 433 * be requested. Otherwise the "pwm-names" property is used to do a reverse
434 * lookup of the PWM index. This also means that the "pwm-names" property 434 * lookup of the PWM index. This also means that the "pwm-names" property
435 * becomes mandatory for devices that look up the PWM device via the con_id 435 * becomes mandatory for devices that look up the PWM device via the con_id
436 * parameter. 436 * parameter.
437 */ 437 */
438 static struct pwm_device *of_pwm_request(struct device_node *np, 438 static struct pwm_device *of_pwm_request(struct device_node *np,
439 const char *con_id) 439 const char *con_id)
440 { 440 {
441 struct pwm_device *pwm = NULL; 441 struct pwm_device *pwm = NULL;
442 struct of_phandle_args args; 442 struct of_phandle_args args;
443 struct pwm_chip *pc; 443 struct pwm_chip *pc;
444 int index = 0; 444 int index = 0;
445 int err; 445 int err;
446 446
447 if (con_id) { 447 if (con_id) {
448 index = of_property_match_string(np, "pwm-names", con_id); 448 index = of_property_match_string(np, "pwm-names", con_id);
449 if (index < 0) 449 if (index < 0)
450 return ERR_PTR(index); 450 return ERR_PTR(index);
451 } 451 }
452 452
453 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, 453 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
454 &args); 454 &args);
455 if (err) { 455 if (err) {
456 pr_debug("%s(): can't parse \"pwms\" property\n", __func__); 456 pr_debug("%s(): can't parse \"pwms\" property\n", __func__);
457 return ERR_PTR(err); 457 return ERR_PTR(err);
458 } 458 }
459 459
460 pc = of_node_to_pwmchip(args.np); 460 pc = of_node_to_pwmchip(args.np);
461 if (IS_ERR(pc)) { 461 if (IS_ERR(pc)) {
462 pr_debug("%s(): PWM chip not found\n", __func__); 462 pr_debug("%s(): PWM chip not found\n", __func__);
463 pwm = ERR_CAST(pc); 463 pwm = ERR_CAST(pc);
464 goto put; 464 goto put;
465 } 465 }
466 466
467 if (args.args_count != pc->of_pwm_n_cells) { 467 if (args.args_count != pc->of_pwm_n_cells) {
468 pr_debug("%s: wrong #pwm-cells for %s\n", np->full_name, 468 pr_debug("%s: wrong #pwm-cells for %s\n", np->full_name,
469 args.np->full_name); 469 args.np->full_name);
470 pwm = ERR_PTR(-EINVAL); 470 pwm = ERR_PTR(-EINVAL);
471 goto put; 471 goto put;
472 } 472 }
473 473
474 pwm = pc->of_xlate(pc, &args); 474 pwm = pc->of_xlate(pc, &args);
475 if (IS_ERR(pwm)) 475 if (IS_ERR(pwm))
476 goto put; 476 goto put;
477 477
478 /* 478 /*
479 * If a consumer name was not given, try to look it up from the 479 * If a consumer name was not given, try to look it up from the
480 * "pwm-names" property if it exists. Otherwise use the name of 480 * "pwm-names" property if it exists. Otherwise use the name of
481 * the user device node. 481 * the user device node.
482 */ 482 */
483 if (!con_id) { 483 if (!con_id) {
484 err = of_property_read_string_index(np, "pwm-names", index, 484 err = of_property_read_string_index(np, "pwm-names", index,
485 &con_id); 485 &con_id);
486 if (err < 0) 486 if (err < 0)
487 con_id = np->name; 487 con_id = np->name;
488 } 488 }
489 489
490 pwm->label = con_id; 490 pwm->label = con_id;
491 491
492 put: 492 put:
493 of_node_put(args.np); 493 of_node_put(args.np);
494 494
495 return pwm; 495 return pwm;
496 } 496 }
497 497
498 /** 498 /**
499 * pwm_add_table() - register PWM device consumers 499 * pwm_add_table() - register PWM device consumers
500 * @table: array of consumers to register 500 * @table: array of consumers to register
501 * @num: number of consumers in table 501 * @num: number of consumers in table
502 */ 502 */
503 void __init pwm_add_table(struct pwm_lookup *table, size_t num) 503 void __init pwm_add_table(struct pwm_lookup *table, size_t num)
504 { 504 {
505 mutex_lock(&pwm_lookup_lock); 505 mutex_lock(&pwm_lookup_lock);
506 506
507 while (num--) { 507 while (num--) {
508 list_add_tail(&table->list, &pwm_lookup_list); 508 list_add_tail(&table->list, &pwm_lookup_list);
509 table++; 509 table++;
510 } 510 }
511 511
512 mutex_unlock(&pwm_lookup_lock); 512 mutex_unlock(&pwm_lookup_lock);
513 } 513 }
514 514
515 /** 515 /**
516 * pwm_get() - look up and request a PWM device 516 * pwm_get() - look up and request a PWM device
517 * @dev: device for PWM consumer 517 * @dev: device for PWM consumer
518 * @con_id: consumer name 518 * @con_id: consumer name
519 * 519 *
520 * Lookup is first attempted using DT. If the device was not instantiated from 520 * Lookup is first attempted using DT. If the device was not instantiated from
521 * a device tree, a PWM chip and a relative index is looked up via a table 521 * a device tree, a PWM chip and a relative index is looked up via a table
522 * supplied by board setup code (see pwm_add_table()). 522 * supplied by board setup code (see pwm_add_table()).
523 * 523 *
524 * Once a PWM chip has been found the specified PWM device will be requested 524 * Once a PWM chip has been found the specified PWM device will be requested
525 * and is ready to be used. 525 * and is ready to be used.
526 */ 526 */
527 struct pwm_device *pwm_get(struct device *dev, const char *con_id) 527 struct pwm_device *pwm_get(struct device *dev, const char *con_id)
528 { 528 {
529 struct pwm_device *pwm = ERR_PTR(-EPROBE_DEFER); 529 struct pwm_device *pwm = ERR_PTR(-EPROBE_DEFER);
530 const char *dev_id = dev ? dev_name(dev): NULL; 530 const char *dev_id = dev ? dev_name(dev): NULL;
531 struct pwm_chip *chip = NULL; 531 struct pwm_chip *chip = NULL;
532 unsigned int index = 0;
532 unsigned int best = 0; 533 unsigned int best = 0;
533 struct pwm_lookup *p; 534 struct pwm_lookup *p;
534 unsigned int index;
535 unsigned int match; 535 unsigned int match;
536 536
537 /* look up via DT first */ 537 /* look up via DT first */
538 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) 538 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
539 return of_pwm_request(dev->of_node, con_id); 539 return of_pwm_request(dev->of_node, con_id);
540 540
541 /* 541 /*
542 * We look up the provider in the static table typically provided by 542 * We look up the provider in the static table typically provided by
543 * board setup code. We first try to lookup the consumer device by 543 * board setup code. We first try to lookup the consumer device by
544 * name. If the consumer device was passed in as NULL or if no match 544 * name. If the consumer device was passed in as NULL or if no match
545 * was found, we try to find the consumer by directly looking it up 545 * was found, we try to find the consumer by directly looking it up
546 * by name. 546 * by name.
547 * 547 *
548 * If a match is found, the provider PWM chip is looked up by name 548 * If a match is found, the provider PWM chip is looked up by name
549 * and a PWM device is requested using the PWM device per-chip index. 549 * and a PWM device is requested using the PWM device per-chip index.
550 * 550 *
551 * The lookup algorithm was shamelessly taken from the clock 551 * The lookup algorithm was shamelessly taken from the clock
552 * framework: 552 * framework:
553 * 553 *
554 * We do slightly fuzzy matching here: 554 * We do slightly fuzzy matching here:
555 * An entry with a NULL ID is assumed to be a wildcard. 555 * An entry with a NULL ID is assumed to be a wildcard.
556 * If an entry has a device ID, it must match 556 * If an entry has a device ID, it must match
557 * If an entry has a connection ID, it must match 557 * If an entry has a connection ID, it must match
558 * Then we take the most specific entry - with the following order 558 * Then we take the most specific entry - with the following order
559 * of precedence: dev+con > dev only > con only. 559 * of precedence: dev+con > dev only > con only.
560 */ 560 */
561 mutex_lock(&pwm_lookup_lock); 561 mutex_lock(&pwm_lookup_lock);
562 562
563 list_for_each_entry(p, &pwm_lookup_list, list) { 563 list_for_each_entry(p, &pwm_lookup_list, list) {
564 match = 0; 564 match = 0;
565 565
566 if (p->dev_id) { 566 if (p->dev_id) {
567 if (!dev_id || strcmp(p->dev_id, dev_id)) 567 if (!dev_id || strcmp(p->dev_id, dev_id))
568 continue; 568 continue;
569 569
570 match += 2; 570 match += 2;
571 } 571 }
572 572
573 if (p->con_id) { 573 if (p->con_id) {
574 if (!con_id || strcmp(p->con_id, con_id)) 574 if (!con_id || strcmp(p->con_id, con_id))
575 continue; 575 continue;
576 576
577 match += 1; 577 match += 1;
578 } 578 }
579 579
580 if (match > best) { 580 if (match > best) {
581 chip = pwmchip_find_by_name(p->provider); 581 chip = pwmchip_find_by_name(p->provider);
582 index = p->index; 582 index = p->index;
583 583
584 if (match != 3) 584 if (match != 3)
585 best = match; 585 best = match;
586 else 586 else
587 break; 587 break;
588 } 588 }
589 } 589 }
590 590
591 if (chip) 591 if (chip)
592 pwm = pwm_request_from_chip(chip, index, con_id ?: dev_id); 592 pwm = pwm_request_from_chip(chip, index, con_id ?: dev_id);
593 593
594 mutex_unlock(&pwm_lookup_lock); 594 mutex_unlock(&pwm_lookup_lock);
595 595
596 return pwm; 596 return pwm;
597 } 597 }
598 EXPORT_SYMBOL_GPL(pwm_get); 598 EXPORT_SYMBOL_GPL(pwm_get);
599 599
600 /** 600 /**
601 * pwm_put() - release a PWM device 601 * pwm_put() - release a PWM device
602 * @pwm: PWM device 602 * @pwm: PWM device
603 */ 603 */
604 void pwm_put(struct pwm_device *pwm) 604 void pwm_put(struct pwm_device *pwm)
605 { 605 {
606 if (!pwm) 606 if (!pwm)
607 return; 607 return;
608 608
609 mutex_lock(&pwm_lock); 609 mutex_lock(&pwm_lock);
610 610
611 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { 611 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
612 pr_warning("PWM device already freed\n"); 612 pr_warning("PWM device already freed\n");
613 goto out; 613 goto out;
614 } 614 }
615 615
616 if (pwm->chip->ops->free) 616 if (pwm->chip->ops->free)
617 pwm->chip->ops->free(pwm->chip, pwm); 617 pwm->chip->ops->free(pwm->chip, pwm);
618 618
619 pwm->label = NULL; 619 pwm->label = NULL;
620 620
621 module_put(pwm->chip->ops->owner); 621 module_put(pwm->chip->ops->owner);
622 out: 622 out:
623 mutex_unlock(&pwm_lock); 623 mutex_unlock(&pwm_lock);
624 } 624 }
625 EXPORT_SYMBOL_GPL(pwm_put); 625 EXPORT_SYMBOL_GPL(pwm_put);
626 626
627 #ifdef CONFIG_DEBUG_FS 627 #ifdef CONFIG_DEBUG_FS
628 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s) 628 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
629 { 629 {
630 unsigned int i; 630 unsigned int i;
631 631
632 for (i = 0; i < chip->npwm; i++) { 632 for (i = 0; i < chip->npwm; i++) {
633 struct pwm_device *pwm = &chip->pwms[i]; 633 struct pwm_device *pwm = &chip->pwms[i];
634 634
635 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); 635 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
636 636
637 if (test_bit(PWMF_REQUESTED, &pwm->flags)) 637 if (test_bit(PWMF_REQUESTED, &pwm->flags))
638 seq_printf(s, " requested"); 638 seq_printf(s, " requested");
639 639
640 if (test_bit(PWMF_ENABLED, &pwm->flags)) 640 if (test_bit(PWMF_ENABLED, &pwm->flags))
641 seq_printf(s, " enabled"); 641 seq_printf(s, " enabled");
642 642
643 seq_printf(s, "\n"); 643 seq_printf(s, "\n");
644 } 644 }
645 } 645 }
646 646
647 static void *pwm_seq_start(struct seq_file *s, loff_t *pos) 647 static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
648 { 648 {
649 mutex_lock(&pwm_lock); 649 mutex_lock(&pwm_lock);
650 s->private = ""; 650 s->private = "";
651 651
652 return seq_list_start(&pwm_chips, *pos); 652 return seq_list_start(&pwm_chips, *pos);
653 } 653 }
654 654
655 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos) 655 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
656 { 656 {
657 s->private = "\n"; 657 s->private = "\n";
658 658
659 return seq_list_next(v, &pwm_chips, pos); 659 return seq_list_next(v, &pwm_chips, pos);
660 } 660 }
661 661
662 static void pwm_seq_stop(struct seq_file *s, void *v) 662 static void pwm_seq_stop(struct seq_file *s, void *v)
663 { 663 {
664 mutex_unlock(&pwm_lock); 664 mutex_unlock(&pwm_lock);
665 } 665 }
666 666
667 static int pwm_seq_show(struct seq_file *s, void *v) 667 static int pwm_seq_show(struct seq_file *s, void *v)
668 { 668 {
669 struct pwm_chip *chip = list_entry(v, struct pwm_chip, list); 669 struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
670 670
671 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private, 671 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
672 chip->dev->bus ? chip->dev->bus->name : "no-bus", 672 chip->dev->bus ? chip->dev->bus->name : "no-bus",
673 dev_name(chip->dev), chip->npwm, 673 dev_name(chip->dev), chip->npwm,
674 (chip->npwm != 1) ? "s" : ""); 674 (chip->npwm != 1) ? "s" : "");
675 675
676 if (chip->ops->dbg_show) 676 if (chip->ops->dbg_show)
677 chip->ops->dbg_show(chip, s); 677 chip->ops->dbg_show(chip, s);
678 else 678 else
679 pwm_dbg_show(chip, s); 679 pwm_dbg_show(chip, s);
680 680
681 return 0; 681 return 0;
682 } 682 }
683 683
684 static const struct seq_operations pwm_seq_ops = { 684 static const struct seq_operations pwm_seq_ops = {
685 .start = pwm_seq_start, 685 .start = pwm_seq_start,
686 .next = pwm_seq_next, 686 .next = pwm_seq_next,
687 .stop = pwm_seq_stop, 687 .stop = pwm_seq_stop,
688 .show = pwm_seq_show, 688 .show = pwm_seq_show,
689 }; 689 };
690 690
691 static int pwm_seq_open(struct inode *inode, struct file *file) 691 static int pwm_seq_open(struct inode *inode, struct file *file)
692 { 692 {
693 return seq_open(file, &pwm_seq_ops); 693 return seq_open(file, &pwm_seq_ops);
694 } 694 }
695 695
696 static const struct file_operations pwm_debugfs_ops = { 696 static const struct file_operations pwm_debugfs_ops = {
697 .owner = THIS_MODULE, 697 .owner = THIS_MODULE,
698 .open = pwm_seq_open, 698 .open = pwm_seq_open,
699 .read = seq_read, 699 .read = seq_read,
700 .llseek = seq_lseek, 700 .llseek = seq_lseek,
701 .release = seq_release, 701 .release = seq_release,
702 }; 702 };
703 703
704 static int __init pwm_debugfs_init(void) 704 static int __init pwm_debugfs_init(void)
705 { 705 {
706 debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL, 706 debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
707 &pwm_debugfs_ops); 707 &pwm_debugfs_ops);
708 708
709 return 0; 709 return 0;
710 } 710 }
711 711
712 subsys_initcall(pwm_debugfs_init); 712 subsys_initcall(pwm_debugfs_init);
713 #endif /* CONFIG_DEBUG_FS */ 713 #endif /* CONFIG_DEBUG_FS */