blob: b0b87b3b52a6516f6fd1ab7a28cb7449589f4370 [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];
78 radix_tree_delete(&pwm_tree, pwm->pwm);
Sascha Hauer0c2498f2011-01-28 09:40:40 +010079 }
80
Thierry Redingf051c462011-12-14 11:12:23 +010081 bitmap_clear(allocated_pwms, chip->base, chip->npwm);
82
83 kfree(chip->pwms);
84 chip->pwms = NULL;
85}
86
Thierry Reding8138d2d2012-03-26 08:42:48 +020087static struct pwm_chip *pwmchip_find_by_name(const char *name)
88{
89 struct pwm_chip *chip;
90
91 if (!name)
92 return NULL;
93
94 mutex_lock(&pwm_lock);
95
96 list_for_each_entry(chip, &pwm_chips, list) {
97 const char *chip_name = dev_name(chip->dev);
98
99 if (chip_name && strcmp(chip_name, name) == 0) {
100 mutex_unlock(&pwm_lock);
101 return chip;
102 }
103 }
104
105 mutex_unlock(&pwm_lock);
106
107 return NULL;
108}
109
Thierry Redingf051c462011-12-14 11:12:23 +0100110static int pwm_device_request(struct pwm_device *pwm, const char *label)
111{
112 int err;
113
114 if (test_bit(PWMF_REQUESTED, &pwm->flags))
115 return -EBUSY;
116
117 if (!try_module_get(pwm->chip->ops->owner))
118 return -ENODEV;
119
120 if (pwm->chip->ops->request) {
121 err = pwm->chip->ops->request(pwm->chip, pwm);
122 if (err) {
123 module_put(pwm->chip->ops->owner);
124 return err;
125 }
126 }
127
128 set_bit(PWMF_REQUESTED, &pwm->flags);
129 pwm->label = label;
130
131 return 0;
132}
133
Philip, Avinash83af2402012-11-21 13:10:44 +0530134struct pwm_device *
135of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
136{
137 struct pwm_device *pwm;
138
139 if (pc->of_pwm_n_cells < 3)
140 return ERR_PTR(-EINVAL);
141
142 if (args->args[0] >= pc->npwm)
143 return ERR_PTR(-EINVAL);
144
145 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
146 if (IS_ERR(pwm))
147 return pwm;
148
Boris Brezillone39c0df2016-04-14 21:17:21 +0200149 pwm->args.period = args->args[1];
Philip, Avinash83af2402012-11-21 13:10:44 +0530150
Laurent Pinchart208be762013-07-18 00:54:22 +0200151 if (args->args[2] & PWM_POLARITY_INVERTED)
Boris Brezillone39c0df2016-04-14 21:17:21 +0200152 pwm->args.polarity = PWM_POLARITY_INVERSED;
Philip, Avinash83af2402012-11-21 13:10:44 +0530153 else
Boris Brezillone39c0df2016-04-14 21:17:21 +0200154 pwm->args.polarity = PWM_POLARITY_NORMAL;
Philip, Avinash83af2402012-11-21 13:10:44 +0530155
156 return pwm;
157}
Thierry Reding417328c2012-11-28 15:12:07 +0100158EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
Philip, Avinash83af2402012-11-21 13:10:44 +0530159
Sachin Kamate50d3522012-08-10 16:41:13 +0530160static struct pwm_device *
161of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
Thierry Reding7299ab72011-12-14 11:10:32 +0100162{
163 struct pwm_device *pwm;
164
165 if (pc->of_pwm_n_cells < 2)
166 return ERR_PTR(-EINVAL);
167
168 if (args->args[0] >= pc->npwm)
169 return ERR_PTR(-EINVAL);
170
171 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
172 if (IS_ERR(pwm))
173 return pwm;
174
Boris Brezillone39c0df2016-04-14 21:17:21 +0200175 pwm->args.period = args->args[1];
Thierry Reding7299ab72011-12-14 11:10:32 +0100176
177 return pwm;
178}
179
Sachin Kamatdfeb86e2012-08-02 12:32:42 +0530180static void of_pwmchip_add(struct pwm_chip *chip)
Thierry Reding7299ab72011-12-14 11:10:32 +0100181{
182 if (!chip->dev || !chip->dev->of_node)
183 return;
184
185 if (!chip->of_xlate) {
186 chip->of_xlate = of_pwm_simple_xlate;
187 chip->of_pwm_n_cells = 2;
188 }
189
190 of_node_get(chip->dev->of_node);
191}
192
Sachin Kamatdfeb86e2012-08-02 12:32:42 +0530193static void of_pwmchip_remove(struct pwm_chip *chip)
Thierry Reding7299ab72011-12-14 11:10:32 +0100194{
Markus Elfring8d6cc072015-02-03 11:54:28 +0100195 if (chip->dev)
Thierry Reding7299ab72011-12-14 11:10:32 +0100196 of_node_put(chip->dev->of_node);
197}
198
Thierry Redingf051c462011-12-14 11:12:23 +0100199/**
200 * pwm_set_chip_data() - set private chip data for a PWM
201 * @pwm: PWM device
202 * @data: pointer to chip-specific data
Thierry Reding04883802015-07-27 11:58:32 +0200203 *
204 * Returns: 0 on success or a negative error code on failure.
Thierry Redingf051c462011-12-14 11:12:23 +0100205 */
206int pwm_set_chip_data(struct pwm_device *pwm, void *data)
207{
208 if (!pwm)
209 return -EINVAL;
210
211 pwm->chip_data = data;
212
213 return 0;
214}
Thierry Reding928c4472013-01-30 09:22:24 +0100215EXPORT_SYMBOL_GPL(pwm_set_chip_data);
Thierry Redingf051c462011-12-14 11:12:23 +0100216
217/**
218 * pwm_get_chip_data() - get private chip data for a PWM
219 * @pwm: PWM device
Thierry Reding04883802015-07-27 11:58:32 +0200220 *
221 * Returns: A pointer to the chip-private data for the PWM device.
Thierry Redingf051c462011-12-14 11:12:23 +0100222 */
223void *pwm_get_chip_data(struct pwm_device *pwm)
224{
225 return pwm ? pwm->chip_data : NULL;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100226}
Thierry Reding928c4472013-01-30 09:22:24 +0100227EXPORT_SYMBOL_GPL(pwm_get_chip_data);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100228
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200229static bool pwm_ops_check(const struct pwm_ops *ops)
230{
231 /* driver supports legacy, non-atomic operation */
232 if (ops->config && ops->enable && ops->disable)
233 return true;
234
235 /* driver supports atomic operation */
236 if (ops->apply)
237 return true;
238
239 return false;
240}
241
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100242/**
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700243 * pwmchip_add_with_polarity() - register a new PWM chip
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100244 * @chip: the PWM chip to add
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700245 * @polarity: initial polarity of PWM channels
Thierry Redingf051c462011-12-14 11:12:23 +0100246 *
247 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700248 * will be used. The initial polarity for all channels is specified by the
249 * @polarity parameter.
Thierry Reding04883802015-07-27 11:58:32 +0200250 *
251 * Returns: 0 on success or a negative error code on failure.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100252 */
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700253int pwmchip_add_with_polarity(struct pwm_chip *chip,
254 enum pwm_polarity polarity)
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100255{
256 struct pwm_device *pwm;
Thierry Redingf051c462011-12-14 11:12:23 +0100257 unsigned int i;
258 int ret;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100259
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200260 if (!chip || !chip->dev || !chip->ops || !chip->npwm)
261 return -EINVAL;
262
263 if (!pwm_ops_check(chip->ops))
Thierry Redingf051c462011-12-14 11:12:23 +0100264 return -EINVAL;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100265
266 mutex_lock(&pwm_lock);
267
Thierry Redingf051c462011-12-14 11:12:23 +0100268 ret = alloc_pwms(chip->base, chip->npwm);
269 if (ret < 0)
270 goto out;
271
272 chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
273 if (!chip->pwms) {
274 ret = -ENOMEM;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100275 goto out;
276 }
277
Thierry Redingf051c462011-12-14 11:12:23 +0100278 chip->base = ret;
279
280 for (i = 0; i < chip->npwm; i++) {
281 pwm = &chip->pwms[i];
282
283 pwm->chip = chip;
284 pwm->pwm = chip->base + i;
285 pwm->hwpwm = i;
Boris Brezillon43a276b2016-04-14 21:17:38 +0200286 pwm->state.polarity = polarity;
Thierry Redingf051c462011-12-14 11:12:23 +0100287
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200288 if (chip->ops->get_state)
289 chip->ops->get_state(chip, pwm, &pwm->state);
290
Thierry Redingf051c462011-12-14 11:12:23 +0100291 radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
292 }
293
294 bitmap_set(allocated_pwms, chip->base, chip->npwm);
295
296 INIT_LIST_HEAD(&chip->list);
297 list_add(&chip->list, &pwm_chips);
298
299 ret = 0;
300
Thierry Reding7299ab72011-12-14 11:10:32 +0100301 if (IS_ENABLED(CONFIG_OF))
302 of_pwmchip_add(chip);
303
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700304 pwmchip_sysfs_export(chip);
305
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100306out:
307 mutex_unlock(&pwm_lock);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100308 return ret;
309}
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700310EXPORT_SYMBOL_GPL(pwmchip_add_with_polarity);
311
312/**
313 * pwmchip_add() - register a new PWM chip
314 * @chip: the PWM chip to add
315 *
316 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
317 * will be used. The initial polarity for all channels is normal.
Thierry Reding04883802015-07-27 11:58:32 +0200318 *
319 * Returns: 0 on success or a negative error code on failure.
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700320 */
321int pwmchip_add(struct pwm_chip *chip)
322{
323 return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL);
324}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100325EXPORT_SYMBOL_GPL(pwmchip_add);
326
327/**
328 * pwmchip_remove() - remove a PWM chip
329 * @chip: the PWM chip to remove
330 *
331 * Removes a PWM chip. This function may return busy if the PWM chip provides
332 * a PWM device that is still requested.
Thierry Reding04883802015-07-27 11:58:32 +0200333 *
334 * Returns: 0 on success or a negative error code on failure.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100335 */
336int pwmchip_remove(struct pwm_chip *chip)
337{
Thierry Redingf051c462011-12-14 11:12:23 +0100338 unsigned int i;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100339 int ret = 0;
340
341 mutex_lock(&pwm_lock);
342
Thierry Redingf051c462011-12-14 11:12:23 +0100343 for (i = 0; i < chip->npwm; i++) {
344 struct pwm_device *pwm = &chip->pwms[i];
345
346 if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
347 ret = -EBUSY;
348 goto out;
349 }
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100350 }
351
Thierry Redingf051c462011-12-14 11:12:23 +0100352 list_del_init(&chip->list);
Thierry Reding7299ab72011-12-14 11:10:32 +0100353
354 if (IS_ENABLED(CONFIG_OF))
355 of_pwmchip_remove(chip);
356
Thierry Redingf051c462011-12-14 11:12:23 +0100357 free_pwms(chip);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100358
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700359 pwmchip_sysfs_unexport(chip);
360
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100361out:
362 mutex_unlock(&pwm_lock);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100363 return ret;
364}
365EXPORT_SYMBOL_GPL(pwmchip_remove);
366
367/**
368 * pwm_request() - request a PWM device
Thierry Reding04883802015-07-27 11:58:32 +0200369 * @pwm: global PWM device index
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100370 * @label: PWM device label
Thierry Reding8138d2d2012-03-26 08:42:48 +0200371 *
372 * This function is deprecated, use pwm_get() instead.
Thierry Reding04883802015-07-27 11:58:32 +0200373 *
374 * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
375 * failure.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100376 */
Thierry Redingf051c462011-12-14 11:12:23 +0100377struct pwm_device *pwm_request(int pwm, const char *label)
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100378{
Thierry Redingf051c462011-12-14 11:12:23 +0100379 struct pwm_device *dev;
380 int err;
381
382 if (pwm < 0 || pwm >= MAX_PWMS)
383 return ERR_PTR(-EINVAL);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100384
385 mutex_lock(&pwm_lock);
386
Thierry Redingf051c462011-12-14 11:12:23 +0100387 dev = pwm_to_device(pwm);
388 if (!dev) {
389 dev = ERR_PTR(-EPROBE_DEFER);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100390 goto out;
391 }
392
Thierry Redingf051c462011-12-14 11:12:23 +0100393 err = pwm_device_request(dev, label);
394 if (err < 0)
395 dev = ERR_PTR(err);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100396
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100397out:
398 mutex_unlock(&pwm_lock);
399
Thierry Redingf051c462011-12-14 11:12:23 +0100400 return dev;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100401}
402EXPORT_SYMBOL_GPL(pwm_request);
403
404/**
Thierry Redingf051c462011-12-14 11:12:23 +0100405 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
406 * @chip: PWM chip
407 * @index: per-chip index of the PWM to request
408 * @label: a literal description string of this PWM
409 *
Thierry Reding04883802015-07-27 11:58:32 +0200410 * Returns: A pointer to the PWM device at the given index of the given PWM
411 * chip. A negative error code is returned if the index is not valid for the
412 * specified PWM chip or if the PWM device cannot be requested.
Thierry Redingf051c462011-12-14 11:12:23 +0100413 */
414struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
415 unsigned int index,
416 const char *label)
417{
418 struct pwm_device *pwm;
419 int err;
420
421 if (!chip || index >= chip->npwm)
422 return ERR_PTR(-EINVAL);
423
424 mutex_lock(&pwm_lock);
425 pwm = &chip->pwms[index];
426
427 err = pwm_device_request(pwm, label);
428 if (err < 0)
429 pwm = ERR_PTR(err);
430
431 mutex_unlock(&pwm_lock);
432 return pwm;
433}
434EXPORT_SYMBOL_GPL(pwm_request_from_chip);
435
436/**
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100437 * pwm_free() - free a PWM device
438 * @pwm: PWM device
Thierry Reding8138d2d2012-03-26 08:42:48 +0200439 *
440 * This function is deprecated, use pwm_put() instead.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100441 */
442void pwm_free(struct pwm_device *pwm)
443{
Thierry Reding8138d2d2012-03-26 08:42:48 +0200444 pwm_put(pwm);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100445}
446EXPORT_SYMBOL_GPL(pwm_free);
447
448/**
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200449 * pwm_apply_state() - atomically apply a new state to a PWM device
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100450 * @pwm: PWM device
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200451 * @state: new state to apply. This can be adjusted by the PWM driver
452 * if the requested config is not achievable, for example,
453 * ->duty_cycle and ->period might be approximated.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100454 */
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200455int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state)
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100456{
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700457 int err;
458
Jonathan Richardsond1cd2142015-10-16 17:40:58 -0700459 if (!pwm)
460 return -EINVAL;
461
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200462 if (!memcmp(state, &pwm->state, sizeof(*state)))
463 return 0;
464
465 if (pwm->chip->ops->apply) {
466 err = pwm->chip->ops->apply(pwm->chip, pwm, state);
467 if (err)
468 return err;
469
470 pwm->state = *state;
471 } else {
472 /*
473 * FIXME: restore the initial state in case of error.
474 */
475 if (state->polarity != pwm->state.polarity) {
476 if (!pwm->chip->ops->set_polarity)
477 return -ENOTSUPP;
478
479 /*
480 * Changing the polarity of a running PWM is
481 * only allowed when the PWM driver implements
482 * ->apply().
483 */
484 if (pwm->state.enabled) {
485 pwm->chip->ops->disable(pwm->chip, pwm);
486 pwm->state.enabled = false;
487 }
488
489 err = pwm->chip->ops->set_polarity(pwm->chip, pwm,
490 state->polarity);
491 if (err)
492 return err;
493
494 pwm->state.polarity = state->polarity;
495 }
496
497 if (state->period != pwm->state.period ||
498 state->duty_cycle != pwm->state.duty_cycle) {
499 err = pwm->chip->ops->config(pwm->chip, pwm,
500 state->duty_cycle,
501 state->period);
502 if (err)
503 return err;
504
505 pwm->state.duty_cycle = state->duty_cycle;
506 pwm->state.period = state->period;
507 }
508
509 if (state->enabled != pwm->state.enabled) {
510 if (state->enabled) {
511 err = pwm->chip->ops->enable(pwm->chip, pwm);
512 if (err)
513 return err;
514 } else {
515 pwm->chip->ops->disable(pwm->chip, pwm);
516 }
517
518 pwm->state.enabled = state->enabled;
519 }
Jonathan Richardsond1cd2142015-10-16 17:40:58 -0700520 }
521
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200522 return 0;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100523}
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200524EXPORT_SYMBOL_GPL(pwm_apply_state);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100525
526/**
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200527 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100528 * @pwm: PWM device
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200529 *
530 * This function will adjust the PWM config to the PWM arguments provided
531 * by the DT or PWM lookup table. This is particularly useful to adapt
532 * the bootloader config to the Linux one.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100533 */
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200534int pwm_adjust_config(struct pwm_device *pwm)
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100535{
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200536 struct pwm_state state;
537 struct pwm_args pargs;
Boris Brezillon09a7e4a2016-04-14 21:17:39 +0200538
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200539 pwm_get_args(pwm, &pargs);
540 pwm_get_state(pwm, &state);
541
542 /*
543 * If the current period is zero it means that either the PWM driver
544 * does not support initial state retrieval or the PWM has not yet
545 * been configured.
546 *
547 * In either case, we setup the new period and polarity, and assign a
548 * duty cycle of 0.
549 */
550 if (!state.period) {
551 state.duty_cycle = 0;
552 state.period = pargs.period;
553 state.polarity = pargs.polarity;
554
555 return pwm_apply_state(pwm, &state);
Boris Brezillon09a7e4a2016-04-14 21:17:39 +0200556 }
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200557
558 /*
559 * Adjust the PWM duty cycle/period based on the period value provided
560 * in PWM args.
561 */
562 if (pargs.period != state.period) {
563 u64 dutycycle = (u64)state.duty_cycle * pargs.period;
564
565 do_div(dutycycle, state.period);
566 state.duty_cycle = dutycycle;
567 state.period = pargs.period;
568 }
569
570 /*
571 * If the polarity changed, we should also change the duty cycle.
572 */
573 if (pargs.polarity != state.polarity) {
574 state.polarity = pargs.polarity;
575 state.duty_cycle = state.period - state.duty_cycle;
576 }
577
578 return pwm_apply_state(pwm, &state);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100579}
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200580EXPORT_SYMBOL_GPL(pwm_adjust_config);
Thierry Reding62099abf2012-03-26 09:31:48 +0200581
Thierry Reding7299ab72011-12-14 11:10:32 +0100582static struct pwm_chip *of_node_to_pwmchip(struct device_node *np)
583{
584 struct pwm_chip *chip;
585
586 mutex_lock(&pwm_lock);
587
588 list_for_each_entry(chip, &pwm_chips, list)
589 if (chip->dev && chip->dev->of_node == np) {
590 mutex_unlock(&pwm_lock);
591 return chip;
592 }
593
594 mutex_unlock(&pwm_lock);
595
596 return ERR_PTR(-EPROBE_DEFER);
597}
598
599/**
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800600 * of_pwm_get() - request a PWM via the PWM framework
Thierry Reding7299ab72011-12-14 11:10:32 +0100601 * @np: device node to get the PWM from
602 * @con_id: consumer name
603 *
604 * Returns the PWM device parsed from the phandle and index specified in the
605 * "pwms" property of a device tree node or a negative error-code on failure.
606 * Values parsed from the device tree are stored in the returned PWM device
607 * object.
608 *
609 * If con_id is NULL, the first PWM device listed in the "pwms" property will
610 * be requested. Otherwise the "pwm-names" property is used to do a reverse
611 * lookup of the PWM index. This also means that the "pwm-names" property
612 * becomes mandatory for devices that look up the PWM device via the con_id
613 * parameter.
Thierry Reding04883802015-07-27 11:58:32 +0200614 *
615 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
616 * error code on failure.
Thierry Reding7299ab72011-12-14 11:10:32 +0100617 */
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800618struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id)
Thierry Reding7299ab72011-12-14 11:10:32 +0100619{
620 struct pwm_device *pwm = NULL;
621 struct of_phandle_args args;
622 struct pwm_chip *pc;
623 int index = 0;
624 int err;
625
626 if (con_id) {
627 index = of_property_match_string(np, "pwm-names", con_id);
628 if (index < 0)
629 return ERR_PTR(index);
630 }
631
632 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
633 &args);
634 if (err) {
635 pr_debug("%s(): can't parse \"pwms\" property\n", __func__);
636 return ERR_PTR(err);
637 }
638
639 pc = of_node_to_pwmchip(args.np);
640 if (IS_ERR(pc)) {
641 pr_debug("%s(): PWM chip not found\n", __func__);
642 pwm = ERR_CAST(pc);
643 goto put;
644 }
645
646 if (args.args_count != pc->of_pwm_n_cells) {
647 pr_debug("%s: wrong #pwm-cells for %s\n", np->full_name,
648 args.np->full_name);
649 pwm = ERR_PTR(-EINVAL);
650 goto put;
651 }
652
653 pwm = pc->of_xlate(pc, &args);
654 if (IS_ERR(pwm))
655 goto put;
656
657 /*
658 * If a consumer name was not given, try to look it up from the
659 * "pwm-names" property if it exists. Otherwise use the name of
660 * the user device node.
661 */
662 if (!con_id) {
663 err = of_property_read_string_index(np, "pwm-names", index,
664 &con_id);
665 if (err < 0)
666 con_id = np->name;
667 }
668
669 pwm->label = con_id;
670
671put:
672 of_node_put(args.np);
673
674 return pwm;
675}
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800676EXPORT_SYMBOL_GPL(of_pwm_get);
Thierry Reding7299ab72011-12-14 11:10:32 +0100677
Thierry Reding8138d2d2012-03-26 08:42:48 +0200678/**
679 * pwm_add_table() - register PWM device consumers
680 * @table: array of consumers to register
681 * @num: number of consumers in table
682 */
Shobhit Kumarc264f112015-03-12 22:01:31 +0530683void pwm_add_table(struct pwm_lookup *table, size_t num)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200684{
685 mutex_lock(&pwm_lookup_lock);
686
687 while (num--) {
688 list_add_tail(&table->list, &pwm_lookup_list);
689 table++;
690 }
691
692 mutex_unlock(&pwm_lookup_lock);
693}
694
695/**
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530696 * pwm_remove_table() - unregister PWM device consumers
697 * @table: array of consumers to unregister
698 * @num: number of consumers in table
699 */
700void pwm_remove_table(struct pwm_lookup *table, size_t num)
701{
702 mutex_lock(&pwm_lookup_lock);
703
704 while (num--) {
705 list_del(&table->list);
706 table++;
707 }
708
709 mutex_unlock(&pwm_lookup_lock);
710}
711
712/**
Thierry Reding8138d2d2012-03-26 08:42:48 +0200713 * pwm_get() - look up and request a PWM device
714 * @dev: device for PWM consumer
715 * @con_id: consumer name
716 *
Thierry Reding7299ab72011-12-14 11:10:32 +0100717 * Lookup is first attempted using DT. If the device was not instantiated from
718 * a device tree, a PWM chip and a relative index is looked up via a table
719 * supplied by board setup code (see pwm_add_table()).
Thierry Reding8138d2d2012-03-26 08:42:48 +0200720 *
721 * Once a PWM chip has been found the specified PWM device will be requested
722 * and is ready to be used.
Thierry Reding04883802015-07-27 11:58:32 +0200723 *
724 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
725 * error code on failure.
Thierry Reding8138d2d2012-03-26 08:42:48 +0200726 */
727struct pwm_device *pwm_get(struct device *dev, const char *con_id)
728{
729 struct pwm_device *pwm = ERR_PTR(-EPROBE_DEFER);
Sachin Kamate50d3522012-08-10 16:41:13 +0530730 const char *dev_id = dev ? dev_name(dev) : NULL;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200731 struct pwm_chip *chip = NULL;
732 unsigned int best = 0;
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200733 struct pwm_lookup *p, *chosen = NULL;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200734 unsigned int match;
735
Thierry Reding7299ab72011-12-14 11:10:32 +0100736 /* look up via DT first */
737 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800738 return of_pwm_get(dev->of_node, con_id);
Thierry Reding7299ab72011-12-14 11:10:32 +0100739
Thierry Reding8138d2d2012-03-26 08:42:48 +0200740 /*
741 * We look up the provider in the static table typically provided by
742 * board setup code. We first try to lookup the consumer device by
743 * name. If the consumer device was passed in as NULL or if no match
744 * was found, we try to find the consumer by directly looking it up
745 * by name.
746 *
747 * If a match is found, the provider PWM chip is looked up by name
748 * and a PWM device is requested using the PWM device per-chip index.
749 *
750 * The lookup algorithm was shamelessly taken from the clock
751 * framework:
752 *
753 * We do slightly fuzzy matching here:
754 * An entry with a NULL ID is assumed to be a wildcard.
755 * If an entry has a device ID, it must match
756 * If an entry has a connection ID, it must match
757 * Then we take the most specific entry - with the following order
758 * of precedence: dev+con > dev only > con only.
759 */
760 mutex_lock(&pwm_lookup_lock);
761
762 list_for_each_entry(p, &pwm_lookup_list, list) {
763 match = 0;
764
765 if (p->dev_id) {
766 if (!dev_id || strcmp(p->dev_id, dev_id))
767 continue;
768
769 match += 2;
770 }
771
772 if (p->con_id) {
773 if (!con_id || strcmp(p->con_id, con_id))
774 continue;
775
776 match += 1;
777 }
778
779 if (match > best) {
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200780 chosen = p;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200781
782 if (match != 3)
783 best = match;
784 else
785 break;
786 }
787 }
788
Thierry Reding655a0352015-10-05 14:38:32 +0200789 if (!chosen) {
790 pwm = ERR_PTR(-ENODEV);
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200791 goto out;
Thierry Reding655a0352015-10-05 14:38:32 +0200792 }
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200793
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200794 chip = pwmchip_find_by_name(chosen->provider);
795 if (!chip)
796 goto out;
797
798 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200799 if (IS_ERR(pwm))
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200800 goto out;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200801
Boris Brezillonfbd45a12016-05-17 14:27:25 +0200802 pwm->args.period = chosen->period;
803 pwm->args.polarity = chosen->polarity;
804
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200805out:
806 mutex_unlock(&pwm_lookup_lock);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200807 return pwm;
808}
809EXPORT_SYMBOL_GPL(pwm_get);
810
811/**
812 * pwm_put() - release a PWM device
813 * @pwm: PWM device
814 */
815void pwm_put(struct pwm_device *pwm)
816{
817 if (!pwm)
818 return;
819
820 mutex_lock(&pwm_lock);
821
822 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
Sachin Kamate50d3522012-08-10 16:41:13 +0530823 pr_warn("PWM device already freed\n");
Thierry Reding8138d2d2012-03-26 08:42:48 +0200824 goto out;
825 }
826
827 if (pwm->chip->ops->free)
828 pwm->chip->ops->free(pwm->chip, pwm);
829
830 pwm->label = NULL;
831
832 module_put(pwm->chip->ops->owner);
833out:
834 mutex_unlock(&pwm_lock);
835}
836EXPORT_SYMBOL_GPL(pwm_put);
837
Alexandre Courbot63543162012-08-01 19:20:58 +0900838static void devm_pwm_release(struct device *dev, void *res)
839{
840 pwm_put(*(struct pwm_device **)res);
841}
842
843/**
844 * devm_pwm_get() - resource managed pwm_get()
845 * @dev: device for PWM consumer
846 * @con_id: consumer name
847 *
848 * This function performs like pwm_get() but the acquired PWM device will
849 * automatically be released on driver detach.
Thierry Reding04883802015-07-27 11:58:32 +0200850 *
851 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
852 * error code on failure.
Alexandre Courbot63543162012-08-01 19:20:58 +0900853 */
854struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
855{
856 struct pwm_device **ptr, *pwm;
857
Wolfram Sang77f0b9d2013-06-03 22:27:17 +0200858 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
Alexandre Courbot63543162012-08-01 19:20:58 +0900859 if (!ptr)
860 return ERR_PTR(-ENOMEM);
861
862 pwm = pwm_get(dev, con_id);
863 if (!IS_ERR(pwm)) {
864 *ptr = pwm;
865 devres_add(dev, ptr);
866 } else {
867 devres_free(ptr);
868 }
869
870 return pwm;
871}
872EXPORT_SYMBOL_GPL(devm_pwm_get);
873
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800874/**
875 * devm_of_pwm_get() - resource managed of_pwm_get()
876 * @dev: device for PWM consumer
877 * @np: device node to get the PWM from
878 * @con_id: consumer name
879 *
880 * This function performs like of_pwm_get() but the acquired PWM device will
881 * automatically be released on driver detach.
Thierry Reding04883802015-07-27 11:58:32 +0200882 *
883 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
884 * error code on failure.
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800885 */
886struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
887 const char *con_id)
888{
889 struct pwm_device **ptr, *pwm;
890
Wolfram Sang77f0b9d2013-06-03 22:27:17 +0200891 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800892 if (!ptr)
893 return ERR_PTR(-ENOMEM);
894
895 pwm = of_pwm_get(np, con_id);
896 if (!IS_ERR(pwm)) {
897 *ptr = pwm;
898 devres_add(dev, ptr);
899 } else {
900 devres_free(ptr);
901 }
902
903 return pwm;
904}
905EXPORT_SYMBOL_GPL(devm_of_pwm_get);
906
Alexandre Courbot63543162012-08-01 19:20:58 +0900907static int devm_pwm_match(struct device *dev, void *res, void *data)
908{
909 struct pwm_device **p = res;
910
911 if (WARN_ON(!p || !*p))
912 return 0;
913
914 return *p == data;
915}
916
917/**
918 * devm_pwm_put() - resource managed pwm_put()
919 * @dev: device for PWM consumer
920 * @pwm: PWM device
921 *
922 * Release a PWM previously allocated using devm_pwm_get(). Calling this
923 * function is usually not needed because devm-allocated resources are
924 * automatically released on driver detach.
925 */
926void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
927{
928 WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm));
929}
930EXPORT_SYMBOL_GPL(devm_pwm_put);
931
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100932/**
933 * pwm_can_sleep() - report whether PWM access will sleep
934 * @pwm: PWM device
935 *
Thierry Reding04883802015-07-27 11:58:32 +0200936 * Returns: True if accessing the PWM can sleep, false otherwise.
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100937 */
938bool pwm_can_sleep(struct pwm_device *pwm)
939{
Thierry Redingff01c942016-01-21 15:04:59 +0100940 return true;
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100941}
942EXPORT_SYMBOL_GPL(pwm_can_sleep);
943
Thierry Reding62099abf2012-03-26 09:31:48 +0200944#ifdef CONFIG_DEBUG_FS
945static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
946{
947 unsigned int i;
948
949 for (i = 0; i < chip->npwm; i++) {
950 struct pwm_device *pwm = &chip->pwms[i];
Boris Brezillon39100ce2016-04-14 21:17:43 +0200951 struct pwm_state state;
952
953 pwm_get_state(pwm, &state);
Thierry Reding62099abf2012-03-26 09:31:48 +0200954
955 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
956
957 if (test_bit(PWMF_REQUESTED, &pwm->flags))
Jingoo Hanadcba1e2013-12-19 13:31:24 +0900958 seq_puts(s, " requested");
Thierry Reding62099abf2012-03-26 09:31:48 +0200959
Boris Brezillon39100ce2016-04-14 21:17:43 +0200960 if (state.enabled)
Jingoo Hanadcba1e2013-12-19 13:31:24 +0900961 seq_puts(s, " enabled");
Thierry Reding62099abf2012-03-26 09:31:48 +0200962
Jingoo Hanadcba1e2013-12-19 13:31:24 +0900963 seq_puts(s, "\n");
Thierry Reding62099abf2012-03-26 09:31:48 +0200964 }
965}
966
967static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
968{
969 mutex_lock(&pwm_lock);
970 s->private = "";
971
972 return seq_list_start(&pwm_chips, *pos);
973}
974
975static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
976{
977 s->private = "\n";
978
979 return seq_list_next(v, &pwm_chips, pos);
980}
981
982static void pwm_seq_stop(struct seq_file *s, void *v)
983{
984 mutex_unlock(&pwm_lock);
985}
986
987static int pwm_seq_show(struct seq_file *s, void *v)
988{
989 struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
990
991 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
992 chip->dev->bus ? chip->dev->bus->name : "no-bus",
993 dev_name(chip->dev), chip->npwm,
994 (chip->npwm != 1) ? "s" : "");
995
996 if (chip->ops->dbg_show)
997 chip->ops->dbg_show(chip, s);
998 else
999 pwm_dbg_show(chip, s);
1000
1001 return 0;
1002}
1003
1004static const struct seq_operations pwm_seq_ops = {
1005 .start = pwm_seq_start,
1006 .next = pwm_seq_next,
1007 .stop = pwm_seq_stop,
1008 .show = pwm_seq_show,
1009};
1010
1011static int pwm_seq_open(struct inode *inode, struct file *file)
1012{
1013 return seq_open(file, &pwm_seq_ops);
1014}
1015
1016static const struct file_operations pwm_debugfs_ops = {
1017 .owner = THIS_MODULE,
1018 .open = pwm_seq_open,
1019 .read = seq_read,
1020 .llseek = seq_lseek,
1021 .release = seq_release,
1022};
1023
1024static int __init pwm_debugfs_init(void)
1025{
1026 debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
1027 &pwm_debugfs_ops);
1028
1029 return 0;
1030}
Thierry Reding62099abf2012-03-26 09:31:48 +02001031subsys_initcall(pwm_debugfs_init);
1032#endif /* CONFIG_DEBUG_FS */