blob: 2a1a2d566e39194f365715602a9f7ebb0e4bf879 [file] [log] [blame]
Sascha Hauer0c2498f2011-01-28 09:40:40 +01001/*
2 * Generic pwmlib implementation
3 *
4 * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
Thierry Redingf051c462011-12-14 11:12:23 +01005 * Copyright (C) 2011-2012 Avionic Design GmbH
Sascha Hauer0c2498f2011-01-28 09:40:40 +01006 *
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
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
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
19 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/module.h>
23#include <linux/pwm.h>
Thierry Redingf051c462011-12-14 11:12:23 +010024#include <linux/radix-tree.h>
Sascha Hauer0c2498f2011-01-28 09:40:40 +010025#include <linux/list.h>
26#include <linux/mutex.h>
27#include <linux/err.h>
28#include <linux/slab.h>
29#include <linux/device.h>
Thierry Reding62099abf2012-03-26 09:31:48 +020030#include <linux/debugfs.h>
31#include <linux/seq_file.h>
Sascha Hauer0c2498f2011-01-28 09:40:40 +010032
Laurent Pinchart208be762013-07-18 00:54:22 +020033#include <dt-bindings/pwm/pwm.h>
Sascha Hauer0c2498f2011-01-28 09:40:40 +010034
Laurent Pinchart208be762013-07-18 00:54:22 +020035#define MAX_PWMS 1024
Philip, Avinash83af2402012-11-21 13:10:44 +053036
Thierry Reding8138d2d2012-03-26 08:42:48 +020037static DEFINE_MUTEX(pwm_lookup_lock);
38static LIST_HEAD(pwm_lookup_list);
Sascha Hauer0c2498f2011-01-28 09:40:40 +010039static DEFINE_MUTEX(pwm_lock);
Thierry Redingf051c462011-12-14 11:12:23 +010040static LIST_HEAD(pwm_chips);
41static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
42static RADIX_TREE(pwm_tree, GFP_KERNEL);
Sascha Hauer0c2498f2011-01-28 09:40:40 +010043
Thierry Redingf051c462011-12-14 11:12:23 +010044static struct pwm_device *pwm_to_device(unsigned int pwm)
Sascha Hauer0c2498f2011-01-28 09:40:40 +010045{
Thierry Redingf051c462011-12-14 11:12:23 +010046 return radix_tree_lookup(&pwm_tree, pwm);
47}
Sascha Hauer0c2498f2011-01-28 09:40:40 +010048
Thierry Redingf051c462011-12-14 11:12:23 +010049static int alloc_pwms(int pwm, unsigned int count)
50{
51 unsigned int from = 0;
52 unsigned int start;
53
54 if (pwm >= MAX_PWMS)
55 return -EINVAL;
56
57 if (pwm >= 0)
58 from = pwm;
59
60 start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from,
61 count, 0);
62
63 if (pwm >= 0 && start != pwm)
64 return -EEXIST;
65
66 if (start + count > MAX_PWMS)
67 return -ENOSPC;
68
69 return start;
70}
71
72static void free_pwms(struct pwm_chip *chip)
73{
74 unsigned int i;
75
76 for (i = 0; i < chip->npwm; i++) {
77 struct pwm_device *pwm = &chip->pwms[i];
Thierry Reding83a98862016-05-02 12:05:55 +020078
Thierry Redingf051c462011-12-14 11:12:23 +010079 radix_tree_delete(&pwm_tree, pwm->pwm);
Sascha Hauer0c2498f2011-01-28 09:40:40 +010080 }
81
Thierry Redingf051c462011-12-14 11:12:23 +010082 bitmap_clear(allocated_pwms, chip->base, chip->npwm);
83
84 kfree(chip->pwms);
85 chip->pwms = NULL;
86}
87
Thierry Reding8138d2d2012-03-26 08:42:48 +020088static struct pwm_chip *pwmchip_find_by_name(const char *name)
89{
90 struct pwm_chip *chip;
91
92 if (!name)
93 return NULL;
94
95 mutex_lock(&pwm_lock);
96
97 list_for_each_entry(chip, &pwm_chips, list) {
98 const char *chip_name = dev_name(chip->dev);
99
100 if (chip_name && strcmp(chip_name, name) == 0) {
101 mutex_unlock(&pwm_lock);
102 return chip;
103 }
104 }
105
106 mutex_unlock(&pwm_lock);
107
108 return NULL;
109}
110
Thierry Redingf051c462011-12-14 11:12:23 +0100111static int pwm_device_request(struct pwm_device *pwm, const char *label)
112{
113 int err;
114
115 if (test_bit(PWMF_REQUESTED, &pwm->flags))
116 return -EBUSY;
117
118 if (!try_module_get(pwm->chip->ops->owner))
119 return -ENODEV;
120
121 if (pwm->chip->ops->request) {
122 err = pwm->chip->ops->request(pwm->chip, pwm);
123 if (err) {
124 module_put(pwm->chip->ops->owner);
125 return err;
126 }
127 }
128
129 set_bit(PWMF_REQUESTED, &pwm->flags);
130 pwm->label = label;
131
132 return 0;
133}
134
Philip, Avinash83af2402012-11-21 13:10:44 +0530135struct pwm_device *
136of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
137{
138 struct pwm_device *pwm;
139
140 if (pc->of_pwm_n_cells < 3)
141 return ERR_PTR(-EINVAL);
142
143 if (args->args[0] >= pc->npwm)
144 return ERR_PTR(-EINVAL);
145
146 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
147 if (IS_ERR(pwm))
148 return pwm;
149
150 pwm_set_period(pwm, args->args[1]);
151
Laurent Pinchart208be762013-07-18 00:54:22 +0200152 if (args->args[2] & PWM_POLARITY_INVERTED)
Philip, Avinash83af2402012-11-21 13:10:44 +0530153 pwm_set_polarity(pwm, PWM_POLARITY_INVERSED);
154 else
155 pwm_set_polarity(pwm, PWM_POLARITY_NORMAL);
156
157 return pwm;
158}
Thierry Reding417328c2012-11-28 15:12:07 +0100159EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
Philip, Avinash83af2402012-11-21 13:10:44 +0530160
Sachin Kamate50d3522012-08-10 16:41:13 +0530161static struct pwm_device *
162of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
Thierry Reding7299ab72011-12-14 11:10:32 +0100163{
164 struct pwm_device *pwm;
165
166 if (pc->of_pwm_n_cells < 2)
167 return ERR_PTR(-EINVAL);
168
169 if (args->args[0] >= pc->npwm)
170 return ERR_PTR(-EINVAL);
171
172 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
173 if (IS_ERR(pwm))
174 return pwm;
175
176 pwm_set_period(pwm, args->args[1]);
177
178 return pwm;
179}
180
Sachin Kamatdfeb86e2012-08-02 12:32:42 +0530181static void of_pwmchip_add(struct pwm_chip *chip)
Thierry Reding7299ab72011-12-14 11:10:32 +0100182{
183 if (!chip->dev || !chip->dev->of_node)
184 return;
185
186 if (!chip->of_xlate) {
187 chip->of_xlate = of_pwm_simple_xlate;
188 chip->of_pwm_n_cells = 2;
189 }
190
191 of_node_get(chip->dev->of_node);
192}
193
Sachin Kamatdfeb86e2012-08-02 12:32:42 +0530194static void of_pwmchip_remove(struct pwm_chip *chip)
Thierry Reding7299ab72011-12-14 11:10:32 +0100195{
Markus Elfring8d6cc072015-02-03 11:54:28 +0100196 if (chip->dev)
Thierry Reding7299ab72011-12-14 11:10:32 +0100197 of_node_put(chip->dev->of_node);
198}
199
Thierry Redingf051c462011-12-14 11:12:23 +0100200/**
201 * pwm_set_chip_data() - set private chip data for a PWM
202 * @pwm: PWM device
203 * @data: pointer to chip-specific data
Thierry Reding04883802015-07-27 11:58:32 +0200204 *
205 * Returns: 0 on success or a negative error code on failure.
Thierry Redingf051c462011-12-14 11:12:23 +0100206 */
207int pwm_set_chip_data(struct pwm_device *pwm, void *data)
208{
209 if (!pwm)
210 return -EINVAL;
211
212 pwm->chip_data = data;
213
214 return 0;
215}
Thierry Reding928c4472013-01-30 09:22:24 +0100216EXPORT_SYMBOL_GPL(pwm_set_chip_data);
Thierry Redingf051c462011-12-14 11:12:23 +0100217
218/**
219 * pwm_get_chip_data() - get private chip data for a PWM
220 * @pwm: PWM device
Thierry Reding04883802015-07-27 11:58:32 +0200221 *
222 * Returns: A pointer to the chip-private data for the PWM device.
Thierry Redingf051c462011-12-14 11:12:23 +0100223 */
224void *pwm_get_chip_data(struct pwm_device *pwm)
225{
226 return pwm ? pwm->chip_data : NULL;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100227}
Thierry Reding928c4472013-01-30 09:22:24 +0100228EXPORT_SYMBOL_GPL(pwm_get_chip_data);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100229
230/**
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700231 * pwmchip_add_with_polarity() - register a new PWM chip
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100232 * @chip: the PWM chip to add
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700233 * @polarity: initial polarity of PWM channels
Thierry Redingf051c462011-12-14 11:12:23 +0100234 *
235 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700236 * will be used. The initial polarity for all channels is specified by the
237 * @polarity parameter.
Thierry Reding04883802015-07-27 11:58:32 +0200238 *
239 * Returns: 0 on success or a negative error code on failure.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100240 */
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700241int pwmchip_add_with_polarity(struct pwm_chip *chip,
242 enum pwm_polarity polarity)
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100243{
244 struct pwm_device *pwm;
Thierry Redingf051c462011-12-14 11:12:23 +0100245 unsigned int i;
246 int ret;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100247
Thierry Redingf051c462011-12-14 11:12:23 +0100248 if (!chip || !chip->dev || !chip->ops || !chip->ops->config ||
Xiubo Li533acc02014-08-18 17:08:44 +0800249 !chip->ops->enable || !chip->ops->disable || !chip->npwm)
Thierry Redingf051c462011-12-14 11:12:23 +0100250 return -EINVAL;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100251
252 mutex_lock(&pwm_lock);
253
Thierry Redingf051c462011-12-14 11:12:23 +0100254 ret = alloc_pwms(chip->base, chip->npwm);
255 if (ret < 0)
256 goto out;
257
Thierry Reding2907f8a2016-05-02 12:07:34 +0200258 chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL);
Thierry Redingf051c462011-12-14 11:12:23 +0100259 if (!chip->pwms) {
260 ret = -ENOMEM;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100261 goto out;
262 }
263
Thierry Redingf051c462011-12-14 11:12:23 +0100264 chip->base = ret;
265
266 for (i = 0; i < chip->npwm; i++) {
267 pwm = &chip->pwms[i];
268
269 pwm->chip = chip;
270 pwm->pwm = chip->base + i;
271 pwm->hwpwm = i;
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700272 pwm->polarity = polarity;
Jonathan Richardsond1cd2142015-10-16 17:40:58 -0700273 mutex_init(&pwm->lock);
Thierry Redingf051c462011-12-14 11:12:23 +0100274
275 radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
276 }
277
278 bitmap_set(allocated_pwms, chip->base, chip->npwm);
279
280 INIT_LIST_HEAD(&chip->list);
281 list_add(&chip->list, &pwm_chips);
282
283 ret = 0;
284
Thierry Reding7299ab72011-12-14 11:10:32 +0100285 if (IS_ENABLED(CONFIG_OF))
286 of_pwmchip_add(chip);
287
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700288 pwmchip_sysfs_export(chip);
289
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100290out:
291 mutex_unlock(&pwm_lock);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100292 return ret;
293}
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700294EXPORT_SYMBOL_GPL(pwmchip_add_with_polarity);
295
296/**
297 * pwmchip_add() - register a new PWM chip
298 * @chip: the PWM chip to add
299 *
300 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
301 * will be used. The initial polarity for all channels is normal.
Thierry Reding04883802015-07-27 11:58:32 +0200302 *
303 * Returns: 0 on success or a negative error code on failure.
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700304 */
305int pwmchip_add(struct pwm_chip *chip)
306{
307 return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL);
308}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100309EXPORT_SYMBOL_GPL(pwmchip_add);
310
311/**
312 * pwmchip_remove() - remove a PWM chip
313 * @chip: the PWM chip to remove
314 *
315 * Removes a PWM chip. This function may return busy if the PWM chip provides
316 * a PWM device that is still requested.
Thierry Reding04883802015-07-27 11:58:32 +0200317 *
318 * Returns: 0 on success or a negative error code on failure.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100319 */
320int pwmchip_remove(struct pwm_chip *chip)
321{
Thierry Redingf051c462011-12-14 11:12:23 +0100322 unsigned int i;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100323 int ret = 0;
324
325 mutex_lock(&pwm_lock);
326
Thierry Redingf051c462011-12-14 11:12:23 +0100327 for (i = 0; i < chip->npwm; i++) {
328 struct pwm_device *pwm = &chip->pwms[i];
329
330 if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
331 ret = -EBUSY;
332 goto out;
333 }
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100334 }
335
Thierry Redingf051c462011-12-14 11:12:23 +0100336 list_del_init(&chip->list);
Thierry Reding7299ab72011-12-14 11:10:32 +0100337
338 if (IS_ENABLED(CONFIG_OF))
339 of_pwmchip_remove(chip);
340
Thierry Redingf051c462011-12-14 11:12:23 +0100341 free_pwms(chip);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100342
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700343 pwmchip_sysfs_unexport(chip);
344
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100345out:
346 mutex_unlock(&pwm_lock);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100347 return ret;
348}
349EXPORT_SYMBOL_GPL(pwmchip_remove);
350
351/**
352 * pwm_request() - request a PWM device
Thierry Reding04883802015-07-27 11:58:32 +0200353 * @pwm: global PWM device index
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100354 * @label: PWM device label
Thierry Reding8138d2d2012-03-26 08:42:48 +0200355 *
356 * This function is deprecated, use pwm_get() instead.
Thierry Reding04883802015-07-27 11:58:32 +0200357 *
358 * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
359 * failure.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100360 */
Thierry Redingf051c462011-12-14 11:12:23 +0100361struct pwm_device *pwm_request(int pwm, const char *label)
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100362{
Thierry Redingf051c462011-12-14 11:12:23 +0100363 struct pwm_device *dev;
364 int err;
365
366 if (pwm < 0 || pwm >= MAX_PWMS)
367 return ERR_PTR(-EINVAL);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100368
369 mutex_lock(&pwm_lock);
370
Thierry Redingf051c462011-12-14 11:12:23 +0100371 dev = pwm_to_device(pwm);
372 if (!dev) {
373 dev = ERR_PTR(-EPROBE_DEFER);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100374 goto out;
375 }
376
Thierry Redingf051c462011-12-14 11:12:23 +0100377 err = pwm_device_request(dev, label);
378 if (err < 0)
379 dev = ERR_PTR(err);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100380
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100381out:
382 mutex_unlock(&pwm_lock);
383
Thierry Redingf051c462011-12-14 11:12:23 +0100384 return dev;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100385}
386EXPORT_SYMBOL_GPL(pwm_request);
387
388/**
Thierry Redingf051c462011-12-14 11:12:23 +0100389 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
390 * @chip: PWM chip
391 * @index: per-chip index of the PWM to request
392 * @label: a literal description string of this PWM
393 *
Thierry Reding04883802015-07-27 11:58:32 +0200394 * Returns: A pointer to the PWM device at the given index of the given PWM
395 * chip. A negative error code is returned if the index is not valid for the
396 * specified PWM chip or if the PWM device cannot be requested.
Thierry Redingf051c462011-12-14 11:12:23 +0100397 */
398struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
399 unsigned int index,
400 const char *label)
401{
402 struct pwm_device *pwm;
403 int err;
404
405 if (!chip || index >= chip->npwm)
406 return ERR_PTR(-EINVAL);
407
408 mutex_lock(&pwm_lock);
409 pwm = &chip->pwms[index];
410
411 err = pwm_device_request(pwm, label);
412 if (err < 0)
413 pwm = ERR_PTR(err);
414
415 mutex_unlock(&pwm_lock);
416 return pwm;
417}
418EXPORT_SYMBOL_GPL(pwm_request_from_chip);
419
420/**
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100421 * pwm_free() - free a PWM device
422 * @pwm: PWM device
Thierry Reding8138d2d2012-03-26 08:42:48 +0200423 *
424 * This function is deprecated, use pwm_put() instead.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100425 */
426void pwm_free(struct pwm_device *pwm)
427{
Thierry Reding8138d2d2012-03-26 08:42:48 +0200428 pwm_put(pwm);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100429}
430EXPORT_SYMBOL_GPL(pwm_free);
431
432/**
433 * pwm_config() - change a PWM device configuration
434 * @pwm: PWM device
435 * @duty_ns: "on" time (in nanoseconds)
436 * @period_ns: duration (in nanoseconds) of one cycle
Thierry Reding04883802015-07-27 11:58:32 +0200437 *
438 * Returns: 0 on success or a negative error code on failure.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100439 */
440int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
441{
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700442 int err;
443
Thierry Redingc2d476a2012-09-02 22:13:40 +0200444 if (!pwm || duty_ns < 0 || period_ns <= 0 || duty_ns > period_ns)
Thierry Redingf051c462011-12-14 11:12:23 +0100445 return -EINVAL;
446
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700447 err = pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns);
448 if (err)
449 return err;
450
451 pwm->duty_cycle = duty_ns;
452 pwm->period = period_ns;
453
454 return 0;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100455}
456EXPORT_SYMBOL_GPL(pwm_config);
457
458/**
Philip, Avinash0aa0869c2012-07-24 19:35:32 +0530459 * pwm_set_polarity() - configure the polarity of a PWM signal
460 * @pwm: PWM device
461 * @polarity: new polarity of the PWM signal
462 *
Thierry Reding04883802015-07-27 11:58:32 +0200463 * Note that the polarity cannot be configured while the PWM device is
464 * enabled.
465 *
466 * Returns: 0 on success or a negative error code on failure.
Philip, Avinash0aa0869c2012-07-24 19:35:32 +0530467 */
468int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity)
469{
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700470 int err;
471
Philip, Avinash0aa0869c2012-07-24 19:35:32 +0530472 if (!pwm || !pwm->chip->ops)
473 return -EINVAL;
474
475 if (!pwm->chip->ops->set_polarity)
476 return -ENOSYS;
477
Jonathan Richardsond1cd2142015-10-16 17:40:58 -0700478 mutex_lock(&pwm->lock);
479
480 if (pwm_is_enabled(pwm)) {
481 err = -EBUSY;
482 goto unlock;
483 }
Philip, Avinash0aa0869c2012-07-24 19:35:32 +0530484
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700485 err = pwm->chip->ops->set_polarity(pwm->chip, pwm, polarity);
486 if (err)
Jonathan Richardsond1cd2142015-10-16 17:40:58 -0700487 goto unlock;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700488
489 pwm->polarity = polarity;
490
Jonathan Richardsond1cd2142015-10-16 17:40:58 -0700491unlock:
492 mutex_unlock(&pwm->lock);
493 return err;
Philip, Avinash0aa0869c2012-07-24 19:35:32 +0530494}
495EXPORT_SYMBOL_GPL(pwm_set_polarity);
496
497/**
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100498 * pwm_enable() - start a PWM output toggling
499 * @pwm: PWM device
Thierry Reding04883802015-07-27 11:58:32 +0200500 *
501 * Returns: 0 on success or a negative error code on failure.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100502 */
503int pwm_enable(struct pwm_device *pwm)
504{
Jonathan Richardsond1cd2142015-10-16 17:40:58 -0700505 int err = 0;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100506
Jonathan Richardsond1cd2142015-10-16 17:40:58 -0700507 if (!pwm)
508 return -EINVAL;
509
510 mutex_lock(&pwm->lock);
511
512 if (!test_and_set_bit(PWMF_ENABLED, &pwm->flags)) {
513 err = pwm->chip->ops->enable(pwm->chip, pwm);
514 if (err)
515 clear_bit(PWMF_ENABLED, &pwm->flags);
516 }
517
518 mutex_unlock(&pwm->lock);
519
520 return err;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100521}
522EXPORT_SYMBOL_GPL(pwm_enable);
523
524/**
525 * pwm_disable() - stop a PWM output toggling
526 * @pwm: PWM device
527 */
528void pwm_disable(struct pwm_device *pwm)
529{
Thierry Redingf051c462011-12-14 11:12:23 +0100530 if (pwm && test_and_clear_bit(PWMF_ENABLED, &pwm->flags))
531 pwm->chip->ops->disable(pwm->chip, pwm);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100532}
533EXPORT_SYMBOL_GPL(pwm_disable);
Thierry Reding62099abf2012-03-26 09:31:48 +0200534
Thierry Reding7299ab72011-12-14 11:10:32 +0100535static struct pwm_chip *of_node_to_pwmchip(struct device_node *np)
536{
537 struct pwm_chip *chip;
538
539 mutex_lock(&pwm_lock);
540
541 list_for_each_entry(chip, &pwm_chips, list)
542 if (chip->dev && chip->dev->of_node == np) {
543 mutex_unlock(&pwm_lock);
544 return chip;
545 }
546
547 mutex_unlock(&pwm_lock);
548
549 return ERR_PTR(-EPROBE_DEFER);
550}
551
552/**
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800553 * of_pwm_get() - request a PWM via the PWM framework
Thierry Reding7299ab72011-12-14 11:10:32 +0100554 * @np: device node to get the PWM from
555 * @con_id: consumer name
556 *
557 * Returns the PWM device parsed from the phandle and index specified in the
558 * "pwms" property of a device tree node or a negative error-code on failure.
559 * Values parsed from the device tree are stored in the returned PWM device
560 * object.
561 *
562 * If con_id is NULL, the first PWM device listed in the "pwms" property will
563 * be requested. Otherwise the "pwm-names" property is used to do a reverse
564 * lookup of the PWM index. This also means that the "pwm-names" property
565 * becomes mandatory for devices that look up the PWM device via the con_id
566 * parameter.
Thierry Reding04883802015-07-27 11:58:32 +0200567 *
568 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
569 * error code on failure.
Thierry Reding7299ab72011-12-14 11:10:32 +0100570 */
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800571struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id)
Thierry Reding7299ab72011-12-14 11:10:32 +0100572{
573 struct pwm_device *pwm = NULL;
574 struct of_phandle_args args;
575 struct pwm_chip *pc;
576 int index = 0;
577 int err;
578
579 if (con_id) {
580 index = of_property_match_string(np, "pwm-names", con_id);
581 if (index < 0)
582 return ERR_PTR(index);
583 }
584
585 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
586 &args);
587 if (err) {
588 pr_debug("%s(): can't parse \"pwms\" property\n", __func__);
589 return ERR_PTR(err);
590 }
591
592 pc = of_node_to_pwmchip(args.np);
593 if (IS_ERR(pc)) {
594 pr_debug("%s(): PWM chip not found\n", __func__);
595 pwm = ERR_CAST(pc);
596 goto put;
597 }
598
599 if (args.args_count != pc->of_pwm_n_cells) {
600 pr_debug("%s: wrong #pwm-cells for %s\n", np->full_name,
601 args.np->full_name);
602 pwm = ERR_PTR(-EINVAL);
603 goto put;
604 }
605
606 pwm = pc->of_xlate(pc, &args);
607 if (IS_ERR(pwm))
608 goto put;
609
610 /*
611 * If a consumer name was not given, try to look it up from the
612 * "pwm-names" property if it exists. Otherwise use the name of
613 * the user device node.
614 */
615 if (!con_id) {
616 err = of_property_read_string_index(np, "pwm-names", index,
617 &con_id);
618 if (err < 0)
619 con_id = np->name;
620 }
621
622 pwm->label = con_id;
623
624put:
625 of_node_put(args.np);
626
627 return pwm;
628}
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800629EXPORT_SYMBOL_GPL(of_pwm_get);
Thierry Reding7299ab72011-12-14 11:10:32 +0100630
Thierry Reding8138d2d2012-03-26 08:42:48 +0200631/**
632 * pwm_add_table() - register PWM device consumers
633 * @table: array of consumers to register
634 * @num: number of consumers in table
635 */
Shobhit Kumarc264f112015-03-12 22:01:31 +0530636void pwm_add_table(struct pwm_lookup *table, size_t num)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200637{
638 mutex_lock(&pwm_lookup_lock);
639
640 while (num--) {
641 list_add_tail(&table->list, &pwm_lookup_list);
642 table++;
643 }
644
645 mutex_unlock(&pwm_lookup_lock);
646}
647
648/**
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530649 * pwm_remove_table() - unregister PWM device consumers
650 * @table: array of consumers to unregister
651 * @num: number of consumers in table
652 */
653void pwm_remove_table(struct pwm_lookup *table, size_t num)
654{
655 mutex_lock(&pwm_lookup_lock);
656
657 while (num--) {
658 list_del(&table->list);
659 table++;
660 }
661
662 mutex_unlock(&pwm_lookup_lock);
663}
664
665/**
Thierry Reding8138d2d2012-03-26 08:42:48 +0200666 * pwm_get() - look up and request a PWM device
667 * @dev: device for PWM consumer
668 * @con_id: consumer name
669 *
Thierry Reding7299ab72011-12-14 11:10:32 +0100670 * Lookup is first attempted using DT. If the device was not instantiated from
671 * a device tree, a PWM chip and a relative index is looked up via a table
672 * supplied by board setup code (see pwm_add_table()).
Thierry Reding8138d2d2012-03-26 08:42:48 +0200673 *
674 * Once a PWM chip has been found the specified PWM device will be requested
675 * and is ready to be used.
Thierry Reding04883802015-07-27 11:58:32 +0200676 *
677 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
678 * error code on failure.
Thierry Reding8138d2d2012-03-26 08:42:48 +0200679 */
680struct pwm_device *pwm_get(struct device *dev, const char *con_id)
681{
682 struct pwm_device *pwm = ERR_PTR(-EPROBE_DEFER);
Sachin Kamate50d3522012-08-10 16:41:13 +0530683 const char *dev_id = dev ? dev_name(dev) : NULL;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200684 struct pwm_chip *chip = NULL;
685 unsigned int best = 0;
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200686 struct pwm_lookup *p, *chosen = NULL;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200687 unsigned int match;
688
Thierry Reding7299ab72011-12-14 11:10:32 +0100689 /* look up via DT first */
690 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800691 return of_pwm_get(dev->of_node, con_id);
Thierry Reding7299ab72011-12-14 11:10:32 +0100692
Thierry Reding8138d2d2012-03-26 08:42:48 +0200693 /*
694 * We look up the provider in the static table typically provided by
695 * board setup code. We first try to lookup the consumer device by
696 * name. If the consumer device was passed in as NULL or if no match
697 * was found, we try to find the consumer by directly looking it up
698 * by name.
699 *
700 * If a match is found, the provider PWM chip is looked up by name
701 * and a PWM device is requested using the PWM device per-chip index.
702 *
703 * The lookup algorithm was shamelessly taken from the clock
704 * framework:
705 *
706 * We do slightly fuzzy matching here:
707 * An entry with a NULL ID is assumed to be a wildcard.
708 * If an entry has a device ID, it must match
709 * If an entry has a connection ID, it must match
710 * Then we take the most specific entry - with the following order
711 * of precedence: dev+con > dev only > con only.
712 */
713 mutex_lock(&pwm_lookup_lock);
714
715 list_for_each_entry(p, &pwm_lookup_list, list) {
716 match = 0;
717
718 if (p->dev_id) {
719 if (!dev_id || strcmp(p->dev_id, dev_id))
720 continue;
721
722 match += 2;
723 }
724
725 if (p->con_id) {
726 if (!con_id || strcmp(p->con_id, con_id))
727 continue;
728
729 match += 1;
730 }
731
732 if (match > best) {
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200733 chosen = p;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200734
735 if (match != 3)
736 best = match;
737 else
738 break;
739 }
740 }
741
Thierry Reding655a0352015-10-05 14:38:32 +0200742 if (!chosen) {
743 pwm = ERR_PTR(-ENODEV);
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200744 goto out;
Thierry Reding655a0352015-10-05 14:38:32 +0200745 }
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200746
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200747 chip = pwmchip_find_by_name(chosen->provider);
748 if (!chip)
749 goto out;
750
751 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200752 if (IS_ERR(pwm))
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200753 goto out;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200754
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200755 pwm_set_period(pwm, chosen->period);
756 pwm_set_polarity(pwm, chosen->polarity);
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200757
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200758out:
759 mutex_unlock(&pwm_lookup_lock);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200760 return pwm;
761}
762EXPORT_SYMBOL_GPL(pwm_get);
763
764/**
765 * pwm_put() - release a PWM device
766 * @pwm: PWM device
767 */
768void pwm_put(struct pwm_device *pwm)
769{
770 if (!pwm)
771 return;
772
773 mutex_lock(&pwm_lock);
774
775 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
Sachin Kamate50d3522012-08-10 16:41:13 +0530776 pr_warn("PWM device already freed\n");
Thierry Reding8138d2d2012-03-26 08:42:48 +0200777 goto out;
778 }
779
780 if (pwm->chip->ops->free)
781 pwm->chip->ops->free(pwm->chip, pwm);
782
783 pwm->label = NULL;
784
785 module_put(pwm->chip->ops->owner);
786out:
787 mutex_unlock(&pwm_lock);
788}
789EXPORT_SYMBOL_GPL(pwm_put);
790
Alexandre Courbot63543162012-08-01 19:20:58 +0900791static void devm_pwm_release(struct device *dev, void *res)
792{
793 pwm_put(*(struct pwm_device **)res);
794}
795
796/**
797 * devm_pwm_get() - resource managed pwm_get()
798 * @dev: device for PWM consumer
799 * @con_id: consumer name
800 *
801 * This function performs like pwm_get() but the acquired PWM device will
802 * automatically be released on driver detach.
Thierry Reding04883802015-07-27 11:58:32 +0200803 *
804 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
805 * error code on failure.
Alexandre Courbot63543162012-08-01 19:20:58 +0900806 */
807struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
808{
809 struct pwm_device **ptr, *pwm;
810
Wolfram Sang77f0b9d2013-06-03 22:27:17 +0200811 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
Alexandre Courbot63543162012-08-01 19:20:58 +0900812 if (!ptr)
813 return ERR_PTR(-ENOMEM);
814
815 pwm = pwm_get(dev, con_id);
816 if (!IS_ERR(pwm)) {
817 *ptr = pwm;
818 devres_add(dev, ptr);
819 } else {
820 devres_free(ptr);
821 }
822
823 return pwm;
824}
825EXPORT_SYMBOL_GPL(devm_pwm_get);
826
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800827/**
828 * devm_of_pwm_get() - resource managed of_pwm_get()
829 * @dev: device for PWM consumer
830 * @np: device node to get the PWM from
831 * @con_id: consumer name
832 *
833 * This function performs like of_pwm_get() but the acquired PWM device will
834 * automatically be released on driver detach.
Thierry Reding04883802015-07-27 11:58:32 +0200835 *
836 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
837 * error code on failure.
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800838 */
839struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
840 const char *con_id)
841{
842 struct pwm_device **ptr, *pwm;
843
Wolfram Sang77f0b9d2013-06-03 22:27:17 +0200844 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800845 if (!ptr)
846 return ERR_PTR(-ENOMEM);
847
848 pwm = of_pwm_get(np, con_id);
849 if (!IS_ERR(pwm)) {
850 *ptr = pwm;
851 devres_add(dev, ptr);
852 } else {
853 devres_free(ptr);
854 }
855
856 return pwm;
857}
858EXPORT_SYMBOL_GPL(devm_of_pwm_get);
859
Alexandre Courbot63543162012-08-01 19:20:58 +0900860static int devm_pwm_match(struct device *dev, void *res, void *data)
861{
862 struct pwm_device **p = res;
863
864 if (WARN_ON(!p || !*p))
865 return 0;
866
867 return *p == data;
868}
869
870/**
871 * devm_pwm_put() - resource managed pwm_put()
872 * @dev: device for PWM consumer
873 * @pwm: PWM device
874 *
875 * Release a PWM previously allocated using devm_pwm_get(). Calling this
876 * function is usually not needed because devm-allocated resources are
877 * automatically released on driver detach.
878 */
879void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
880{
881 WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm));
882}
883EXPORT_SYMBOL_GPL(devm_pwm_put);
884
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100885/**
886 * pwm_can_sleep() - report whether PWM access will sleep
887 * @pwm: PWM device
888 *
Thierry Reding04883802015-07-27 11:58:32 +0200889 * Returns: True if accessing the PWM can sleep, false otherwise.
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100890 */
891bool pwm_can_sleep(struct pwm_device *pwm)
892{
Thierry Redingff01c942016-01-21 15:04:59 +0100893 return true;
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100894}
895EXPORT_SYMBOL_GPL(pwm_can_sleep);
896
Thierry Reding62099abf2012-03-26 09:31:48 +0200897#ifdef CONFIG_DEBUG_FS
898static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
899{
900 unsigned int i;
901
902 for (i = 0; i < chip->npwm; i++) {
903 struct pwm_device *pwm = &chip->pwms[i];
904
905 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
906
907 if (test_bit(PWMF_REQUESTED, &pwm->flags))
Jingoo Hanadcba1e2013-12-19 13:31:24 +0900908 seq_puts(s, " requested");
Thierry Reding62099abf2012-03-26 09:31:48 +0200909
Boris Brezillon5c312522015-07-01 10:21:47 +0200910 if (pwm_is_enabled(pwm))
Jingoo Hanadcba1e2013-12-19 13:31:24 +0900911 seq_puts(s, " enabled");
Thierry Reding62099abf2012-03-26 09:31:48 +0200912
Jingoo Hanadcba1e2013-12-19 13:31:24 +0900913 seq_puts(s, "\n");
Thierry Reding62099abf2012-03-26 09:31:48 +0200914 }
915}
916
917static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
918{
919 mutex_lock(&pwm_lock);
920 s->private = "";
921
922 return seq_list_start(&pwm_chips, *pos);
923}
924
925static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
926{
927 s->private = "\n";
928
929 return seq_list_next(v, &pwm_chips, pos);
930}
931
932static void pwm_seq_stop(struct seq_file *s, void *v)
933{
934 mutex_unlock(&pwm_lock);
935}
936
937static int pwm_seq_show(struct seq_file *s, void *v)
938{
939 struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
940
941 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
942 chip->dev->bus ? chip->dev->bus->name : "no-bus",
943 dev_name(chip->dev), chip->npwm,
944 (chip->npwm != 1) ? "s" : "");
945
946 if (chip->ops->dbg_show)
947 chip->ops->dbg_show(chip, s);
948 else
949 pwm_dbg_show(chip, s);
950
951 return 0;
952}
953
954static const struct seq_operations pwm_seq_ops = {
955 .start = pwm_seq_start,
956 .next = pwm_seq_next,
957 .stop = pwm_seq_stop,
958 .show = pwm_seq_show,
959};
960
961static int pwm_seq_open(struct inode *inode, struct file *file)
962{
963 return seq_open(file, &pwm_seq_ops);
964}
965
966static const struct file_operations pwm_debugfs_ops = {
967 .owner = THIS_MODULE,
968 .open = pwm_seq_open,
969 .read = seq_read,
970 .llseek = seq_lseek,
971 .release = seq_release,
972};
973
974static int __init pwm_debugfs_init(void)
975{
976 debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
977 &pwm_debugfs_ops);
978
979 return 0;
980}
Thierry Reding62099abf2012-03-26 09:31:48 +0200981subsys_initcall(pwm_debugfs_init);
982#endif /* CONFIG_DEBUG_FS */