blob: 275b5f399a1addbf2966de351eae74f17ec9372e [file] [log] [blame]
Thomas Gleixnerc82ee6d2019-05-19 15:51:48 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Sascha Hauer0c2498f2011-01-28 09:40:40 +01002/*
3 * Generic pwmlib implementation
4 *
5 * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
Thierry Redingf051c462011-12-14 11:12:23 +01006 * Copyright (C) 2011-2012 Avionic Design GmbH
Sascha Hauer0c2498f2011-01-28 09:40:40 +01007 */
8
9#include <linux/module.h>
10#include <linux/pwm.h>
Thierry Redingf051c462011-12-14 11:12:23 +010011#include <linux/radix-tree.h>
Sascha Hauer0c2498f2011-01-28 09:40:40 +010012#include <linux/list.h>
13#include <linux/mutex.h>
14#include <linux/err.h>
15#include <linux/slab.h>
16#include <linux/device.h>
Thierry Reding62099ab2012-03-26 09:31:48 +020017#include <linux/debugfs.h>
18#include <linux/seq_file.h>
Sascha Hauer0c2498f2011-01-28 09:40:40 +010019
Laurent Pinchart208be762013-07-18 00:54:22 +020020#include <dt-bindings/pwm/pwm.h>
Sascha Hauer0c2498f2011-01-28 09:40:40 +010021
Laurent Pinchart208be762013-07-18 00:54:22 +020022#define MAX_PWMS 1024
Philip, Avinash83af2402012-11-21 13:10:44 +053023
Thierry Reding8138d2d2012-03-26 08:42:48 +020024static DEFINE_MUTEX(pwm_lookup_lock);
25static LIST_HEAD(pwm_lookup_list);
Sascha Hauer0c2498f2011-01-28 09:40:40 +010026static DEFINE_MUTEX(pwm_lock);
Thierry Redingf051c462011-12-14 11:12:23 +010027static LIST_HEAD(pwm_chips);
28static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
29static RADIX_TREE(pwm_tree, GFP_KERNEL);
Sascha Hauer0c2498f2011-01-28 09:40:40 +010030
Thierry Redingf051c462011-12-14 11:12:23 +010031static struct pwm_device *pwm_to_device(unsigned int pwm)
Sascha Hauer0c2498f2011-01-28 09:40:40 +010032{
Thierry Redingf051c462011-12-14 11:12:23 +010033 return radix_tree_lookup(&pwm_tree, pwm);
34}
Sascha Hauer0c2498f2011-01-28 09:40:40 +010035
Thierry Redingf051c462011-12-14 11:12:23 +010036static int alloc_pwms(int pwm, unsigned int count)
37{
38 unsigned int from = 0;
39 unsigned int start;
40
41 if (pwm >= MAX_PWMS)
42 return -EINVAL;
43
44 if (pwm >= 0)
45 from = pwm;
46
47 start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from,
48 count, 0);
49
50 if (pwm >= 0 && start != pwm)
51 return -EEXIST;
52
53 if (start + count > MAX_PWMS)
54 return -ENOSPC;
55
56 return start;
57}
58
59static void free_pwms(struct pwm_chip *chip)
60{
61 unsigned int i;
62
63 for (i = 0; i < chip->npwm; i++) {
64 struct pwm_device *pwm = &chip->pwms[i];
Thierry Reding83a98862016-05-02 12:05:55 +020065
Thierry Redingf051c462011-12-14 11:12:23 +010066 radix_tree_delete(&pwm_tree, pwm->pwm);
Sascha Hauer0c2498f2011-01-28 09:40:40 +010067 }
68
Thierry Redingf051c462011-12-14 11:12:23 +010069 bitmap_clear(allocated_pwms, chip->base, chip->npwm);
70
71 kfree(chip->pwms);
72 chip->pwms = NULL;
73}
74
Thierry Reding8138d2d2012-03-26 08:42:48 +020075static struct pwm_chip *pwmchip_find_by_name(const char *name)
76{
77 struct pwm_chip *chip;
78
79 if (!name)
80 return NULL;
81
82 mutex_lock(&pwm_lock);
83
84 list_for_each_entry(chip, &pwm_chips, list) {
85 const char *chip_name = dev_name(chip->dev);
86
87 if (chip_name && strcmp(chip_name, name) == 0) {
88 mutex_unlock(&pwm_lock);
89 return chip;
90 }
91 }
92
93 mutex_unlock(&pwm_lock);
94
95 return NULL;
96}
97
Thierry Redingf051c462011-12-14 11:12:23 +010098static int pwm_device_request(struct pwm_device *pwm, const char *label)
99{
100 int err;
101
102 if (test_bit(PWMF_REQUESTED, &pwm->flags))
103 return -EBUSY;
104
105 if (!try_module_get(pwm->chip->ops->owner))
106 return -ENODEV;
107
108 if (pwm->chip->ops->request) {
109 err = pwm->chip->ops->request(pwm->chip, pwm);
110 if (err) {
111 module_put(pwm->chip->ops->owner);
112 return err;
113 }
114 }
115
116 set_bit(PWMF_REQUESTED, &pwm->flags);
117 pwm->label = label;
118
119 return 0;
120}
121
Philip, Avinash83af2402012-11-21 13:10:44 +0530122struct pwm_device *
123of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
124{
125 struct pwm_device *pwm;
126
Lothar Wassmann42883cb2017-01-29 22:54:13 +0100127 /* check, whether the driver supports a third cell for flags */
Philip, Avinash83af2402012-11-21 13:10:44 +0530128 if (pc->of_pwm_n_cells < 3)
129 return ERR_PTR(-EINVAL);
130
Lothar Wassmann42883cb2017-01-29 22:54:13 +0100131 /* flags in the third cell are optional */
132 if (args->args_count < 2)
133 return ERR_PTR(-EINVAL);
134
Philip, Avinash83af2402012-11-21 13:10:44 +0530135 if (args->args[0] >= pc->npwm)
136 return ERR_PTR(-EINVAL);
137
138 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
139 if (IS_ERR(pwm))
140 return pwm;
141
Boris Brezillone39c0df2016-04-14 21:17:21 +0200142 pwm->args.period = args->args[1];
Lothar Wassmann42883cb2017-01-29 22:54:13 +0100143 pwm->args.polarity = PWM_POLARITY_NORMAL;
Philip, Avinash83af2402012-11-21 13:10:44 +0530144
Lothar Wassmann42883cb2017-01-29 22:54:13 +0100145 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
Boris Brezillone39c0df2016-04-14 21:17:21 +0200146 pwm->args.polarity = PWM_POLARITY_INVERSED;
Philip, Avinash83af2402012-11-21 13:10:44 +0530147
148 return pwm;
149}
Thierry Reding417328c2012-11-28 15:12:07 +0100150EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
Philip, Avinash83af2402012-11-21 13:10:44 +0530151
Sachin Kamate50d3522012-08-10 16:41:13 +0530152static struct pwm_device *
153of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
Thierry Reding7299ab72011-12-14 11:10:32 +0100154{
155 struct pwm_device *pwm;
156
Lothar Wassmann42883cb2017-01-29 22:54:13 +0100157 /* sanity check driver support */
Thierry Reding7299ab72011-12-14 11:10:32 +0100158 if (pc->of_pwm_n_cells < 2)
159 return ERR_PTR(-EINVAL);
160
Lothar Wassmann42883cb2017-01-29 22:54:13 +0100161 /* all cells are required */
162 if (args->args_count != pc->of_pwm_n_cells)
163 return ERR_PTR(-EINVAL);
164
Thierry Reding7299ab72011-12-14 11:10:32 +0100165 if (args->args[0] >= pc->npwm)
166 return ERR_PTR(-EINVAL);
167
168 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
169 if (IS_ERR(pwm))
170 return pwm;
171
Boris Brezillone39c0df2016-04-14 21:17:21 +0200172 pwm->args.period = args->args[1];
Thierry Reding7299ab72011-12-14 11:10:32 +0100173
174 return pwm;
175}
176
Sachin Kamatdfeb86e2012-08-02 12:32:42 +0530177static void of_pwmchip_add(struct pwm_chip *chip)
Thierry Reding7299ab72011-12-14 11:10:32 +0100178{
179 if (!chip->dev || !chip->dev->of_node)
180 return;
181
182 if (!chip->of_xlate) {
183 chip->of_xlate = of_pwm_simple_xlate;
184 chip->of_pwm_n_cells = 2;
185 }
186
187 of_node_get(chip->dev->of_node);
188}
189
Sachin Kamatdfeb86e2012-08-02 12:32:42 +0530190static void of_pwmchip_remove(struct pwm_chip *chip)
Thierry Reding7299ab72011-12-14 11:10:32 +0100191{
Markus Elfring8d6cc072015-02-03 11:54:28 +0100192 if (chip->dev)
Thierry Reding7299ab72011-12-14 11:10:32 +0100193 of_node_put(chip->dev->of_node);
194}
195
Thierry Redingf051c462011-12-14 11:12:23 +0100196/**
197 * pwm_set_chip_data() - set private chip data for a PWM
198 * @pwm: PWM device
199 * @data: pointer to chip-specific data
Thierry Reding04883802015-07-27 11:58:32 +0200200 *
201 * Returns: 0 on success or a negative error code on failure.
Thierry Redingf051c462011-12-14 11:12:23 +0100202 */
203int pwm_set_chip_data(struct pwm_device *pwm, void *data)
204{
205 if (!pwm)
206 return -EINVAL;
207
208 pwm->chip_data = data;
209
210 return 0;
211}
Thierry Reding928c4472013-01-30 09:22:24 +0100212EXPORT_SYMBOL_GPL(pwm_set_chip_data);
Thierry Redingf051c462011-12-14 11:12:23 +0100213
214/**
215 * pwm_get_chip_data() - get private chip data for a PWM
216 * @pwm: PWM device
Thierry Reding04883802015-07-27 11:58:32 +0200217 *
218 * Returns: A pointer to the chip-private data for the PWM device.
Thierry Redingf051c462011-12-14 11:12:23 +0100219 */
220void *pwm_get_chip_data(struct pwm_device *pwm)
221{
222 return pwm ? pwm->chip_data : NULL;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100223}
Thierry Reding928c4472013-01-30 09:22:24 +0100224EXPORT_SYMBOL_GPL(pwm_get_chip_data);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100225
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200226static bool pwm_ops_check(const struct pwm_ops *ops)
227{
228 /* driver supports legacy, non-atomic operation */
229 if (ops->config && ops->enable && ops->disable)
230 return true;
231
232 /* driver supports atomic operation */
233 if (ops->apply)
234 return true;
235
236 return false;
237}
238
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100239/**
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700240 * pwmchip_add_with_polarity() - register a new PWM chip
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100241 * @chip: the PWM chip to add
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700242 * @polarity: initial polarity of PWM channels
Thierry Redingf051c462011-12-14 11:12:23 +0100243 *
244 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700245 * will be used. The initial polarity for all channels is specified by the
246 * @polarity parameter.
Thierry Reding04883802015-07-27 11:58:32 +0200247 *
248 * Returns: 0 on success or a negative error code on failure.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100249 */
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700250int pwmchip_add_with_polarity(struct pwm_chip *chip,
251 enum pwm_polarity polarity)
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100252{
253 struct pwm_device *pwm;
Thierry Redingf051c462011-12-14 11:12:23 +0100254 unsigned int i;
255 int ret;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100256
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200257 if (!chip || !chip->dev || !chip->ops || !chip->npwm)
258 return -EINVAL;
259
260 if (!pwm_ops_check(chip->ops))
Thierry Redingf051c462011-12-14 11:12:23 +0100261 return -EINVAL;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100262
263 mutex_lock(&pwm_lock);
264
Thierry Redingf051c462011-12-14 11:12:23 +0100265 ret = alloc_pwms(chip->base, chip->npwm);
266 if (ret < 0)
267 goto out;
268
Thierry Reding2907f8a2016-05-02 12:07:34 +0200269 chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL);
Thierry Redingf051c462011-12-14 11:12:23 +0100270 if (!chip->pwms) {
271 ret = -ENOMEM;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100272 goto out;
273 }
274
Thierry Redingf051c462011-12-14 11:12:23 +0100275 chip->base = ret;
276
277 for (i = 0; i < chip->npwm; i++) {
278 pwm = &chip->pwms[i];
279
280 pwm->chip = chip;
281 pwm->pwm = chip->base + i;
282 pwm->hwpwm = i;
Boris Brezillon43a276b2016-04-14 21:17:38 +0200283 pwm->state.polarity = polarity;
Thierry Redingf051c462011-12-14 11:12:23 +0100284
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200285 if (chip->ops->get_state)
286 chip->ops->get_state(chip, pwm, &pwm->state);
Thierry Redingf051c462011-12-14 11:12:23 +0100287
288 radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
289 }
290
291 bitmap_set(allocated_pwms, chip->base, chip->npwm);
292
293 INIT_LIST_HEAD(&chip->list);
294 list_add(&chip->list, &pwm_chips);
295
296 ret = 0;
297
Thierry Reding7299ab72011-12-14 11:10:32 +0100298 if (IS_ENABLED(CONFIG_OF))
299 of_pwmchip_add(chip);
300
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100301out:
302 mutex_unlock(&pwm_lock);
Phong Hoang347ab942019-03-19 19:40:08 +0900303
304 if (!ret)
305 pwmchip_sysfs_export(chip);
306
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100307 return ret;
308}
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700309EXPORT_SYMBOL_GPL(pwmchip_add_with_polarity);
310
311/**
312 * pwmchip_add() - register a new PWM chip
313 * @chip: the PWM chip to add
314 *
315 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
316 * will be used. The initial polarity for all channels is normal.
Thierry Reding04883802015-07-27 11:58:32 +0200317 *
318 * Returns: 0 on success or a negative error code on failure.
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700319 */
320int pwmchip_add(struct pwm_chip *chip)
321{
322 return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL);
323}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100324EXPORT_SYMBOL_GPL(pwmchip_add);
325
326/**
327 * pwmchip_remove() - remove a PWM chip
328 * @chip: the PWM chip to remove
329 *
330 * Removes a PWM chip. This function may return busy if the PWM chip provides
331 * a PWM device that is still requested.
Thierry Reding04883802015-07-27 11:58:32 +0200332 *
333 * Returns: 0 on success or a negative error code on failure.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100334 */
335int pwmchip_remove(struct pwm_chip *chip)
336{
Thierry Redingf051c462011-12-14 11:12:23 +0100337 unsigned int i;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100338 int ret = 0;
339
Phong Hoang347ab942019-03-19 19:40:08 +0900340 pwmchip_sysfs_unexport(chip);
David Hsu07334242016-08-09 14:57:46 -0700341
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100342 mutex_lock(&pwm_lock);
343
Thierry Redingf051c462011-12-14 11:12:23 +0100344 for (i = 0; i < chip->npwm; i++) {
345 struct pwm_device *pwm = &chip->pwms[i];
346
347 if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
348 ret = -EBUSY;
349 goto out;
350 }
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100351 }
352
Thierry Redingf051c462011-12-14 11:12:23 +0100353 list_del_init(&chip->list);
Thierry Reding7299ab72011-12-14 11:10:32 +0100354
355 if (IS_ENABLED(CONFIG_OF))
356 of_pwmchip_remove(chip);
357
Thierry Redingf051c462011-12-14 11:12:23 +0100358 free_pwms(chip);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100359
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100360out:
361 mutex_unlock(&pwm_lock);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100362 return ret;
363}
364EXPORT_SYMBOL_GPL(pwmchip_remove);
365
366/**
367 * pwm_request() - request a PWM device
Thierry Reding04883802015-07-27 11:58:32 +0200368 * @pwm: global PWM device index
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100369 * @label: PWM device label
Thierry Reding8138d2d2012-03-26 08:42:48 +0200370 *
371 * This function is deprecated, use pwm_get() instead.
Thierry Reding04883802015-07-27 11:58:32 +0200372 *
373 * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
374 * failure.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100375 */
Thierry Redingf051c462011-12-14 11:12:23 +0100376struct pwm_device *pwm_request(int pwm, const char *label)
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100377{
Thierry Redingf051c462011-12-14 11:12:23 +0100378 struct pwm_device *dev;
379 int err;
380
381 if (pwm < 0 || pwm >= MAX_PWMS)
382 return ERR_PTR(-EINVAL);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100383
384 mutex_lock(&pwm_lock);
385
Thierry Redingf051c462011-12-14 11:12:23 +0100386 dev = pwm_to_device(pwm);
387 if (!dev) {
388 dev = ERR_PTR(-EPROBE_DEFER);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100389 goto out;
390 }
391
Thierry Redingf051c462011-12-14 11:12:23 +0100392 err = pwm_device_request(dev, label);
393 if (err < 0)
394 dev = ERR_PTR(err);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100395
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100396out:
397 mutex_unlock(&pwm_lock);
398
Thierry Redingf051c462011-12-14 11:12:23 +0100399 return dev;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100400}
401EXPORT_SYMBOL_GPL(pwm_request);
402
403/**
Thierry Redingf051c462011-12-14 11:12:23 +0100404 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
405 * @chip: PWM chip
406 * @index: per-chip index of the PWM to request
407 * @label: a literal description string of this PWM
408 *
Thierry Reding04883802015-07-27 11:58:32 +0200409 * Returns: A pointer to the PWM device at the given index of the given PWM
410 * chip. A negative error code is returned if the index is not valid for the
411 * specified PWM chip or if the PWM device cannot be requested.
Thierry Redingf051c462011-12-14 11:12:23 +0100412 */
413struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
414 unsigned int index,
415 const char *label)
416{
417 struct pwm_device *pwm;
418 int err;
419
420 if (!chip || index >= chip->npwm)
421 return ERR_PTR(-EINVAL);
422
423 mutex_lock(&pwm_lock);
424 pwm = &chip->pwms[index];
425
426 err = pwm_device_request(pwm, label);
427 if (err < 0)
428 pwm = ERR_PTR(err);
429
430 mutex_unlock(&pwm_lock);
431 return pwm;
432}
433EXPORT_SYMBOL_GPL(pwm_request_from_chip);
434
435/**
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100436 * pwm_free() - free a PWM device
437 * @pwm: PWM device
Thierry Reding8138d2d2012-03-26 08:42:48 +0200438 *
439 * This function is deprecated, use pwm_put() instead.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100440 */
441void pwm_free(struct pwm_device *pwm)
442{
Thierry Reding8138d2d2012-03-26 08:42:48 +0200443 pwm_put(pwm);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100444}
445EXPORT_SYMBOL_GPL(pwm_free);
446
447/**
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200448 * pwm_apply_state() - atomically apply a new state to a PWM device
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100449 * @pwm: PWM device
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200450 * @state: new state to apply. This can be adjusted by the PWM driver
451 * if the requested config is not achievable, for example,
452 * ->duty_cycle and ->period might be approximated.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100453 */
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200454int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state)
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100455{
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700456 int err;
457
Brian Norrisef2bf492016-05-27 09:45:49 -0700458 if (!pwm || !state || !state->period ||
459 state->duty_cycle > state->period)
Jonathan Richardsond1cd2142015-10-16 17:40:58 -0700460 return -EINVAL;
461
Uwe Kleine-König309b32f2019-01-07 20:49:37 +0100462 if (state->period == pwm->state.period &&
463 state->duty_cycle == pwm->state.duty_cycle &&
464 state->polarity == pwm->state.polarity &&
465 state->enabled == pwm->state.enabled)
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200466 return 0;
Jonathan Richardsond1cd2142015-10-16 17:40:58 -0700467
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200468 if (pwm->chip->ops->apply) {
469 err = pwm->chip->ops->apply(pwm->chip, pwm, state);
Jonathan Richardsond1cd2142015-10-16 17:40:58 -0700470 if (err)
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200471 return err;
472
473 pwm->state = *state;
474 } else {
475 /*
476 * FIXME: restore the initial state in case of error.
477 */
478 if (state->polarity != pwm->state.polarity) {
479 if (!pwm->chip->ops->set_polarity)
480 return -ENOTSUPP;
481
482 /*
483 * Changing the polarity of a running PWM is
484 * only allowed when the PWM driver implements
485 * ->apply().
486 */
487 if (pwm->state.enabled) {
488 pwm->chip->ops->disable(pwm->chip, pwm);
489 pwm->state.enabled = false;
490 }
491
492 err = pwm->chip->ops->set_polarity(pwm->chip, pwm,
493 state->polarity);
494 if (err)
495 return err;
496
497 pwm->state.polarity = state->polarity;
498 }
499
500 if (state->period != pwm->state.period ||
501 state->duty_cycle != pwm->state.duty_cycle) {
502 err = pwm->chip->ops->config(pwm->chip, pwm,
503 state->duty_cycle,
504 state->period);
505 if (err)
506 return err;
507
508 pwm->state.duty_cycle = state->duty_cycle;
509 pwm->state.period = state->period;
510 }
511
512 if (state->enabled != pwm->state.enabled) {
513 if (state->enabled) {
514 err = pwm->chip->ops->enable(pwm->chip, pwm);
515 if (err)
516 return err;
517 } else {
518 pwm->chip->ops->disable(pwm->chip, pwm);
519 }
520
521 pwm->state.enabled = state->enabled;
522 }
Jonathan Richardsond1cd2142015-10-16 17:40:58 -0700523 }
524
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200525 return 0;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100526}
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200527EXPORT_SYMBOL_GPL(pwm_apply_state);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100528
529/**
Lee Jones3a3d1a42016-06-08 10:21:23 +0100530 * pwm_capture() - capture and report a PWM signal
531 * @pwm: PWM device
532 * @result: structure to fill with capture result
533 * @timeout: time to wait, in milliseconds, before giving up on capture
534 *
535 * Returns: 0 on success or a negative error code on failure.
536 */
537int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
538 unsigned long timeout)
539{
540 int err;
541
542 if (!pwm || !pwm->chip->ops)
543 return -EINVAL;
544
545 if (!pwm->chip->ops->capture)
546 return -ENOSYS;
547
548 mutex_lock(&pwm_lock);
549 err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
550 mutex_unlock(&pwm_lock);
551
552 return err;
553}
554EXPORT_SYMBOL_GPL(pwm_capture);
555
556/**
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200557 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100558 * @pwm: PWM device
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200559 *
560 * This function will adjust the PWM config to the PWM arguments provided
561 * by the DT or PWM lookup table. This is particularly useful to adapt
562 * the bootloader config to the Linux one.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100563 */
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200564int pwm_adjust_config(struct pwm_device *pwm)
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100565{
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200566 struct pwm_state state;
567 struct pwm_args pargs;
Boris Brezillon09a7e4a2016-04-14 21:17:39 +0200568
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200569 pwm_get_args(pwm, &pargs);
570 pwm_get_state(pwm, &state);
571
572 /*
573 * If the current period is zero it means that either the PWM driver
574 * does not support initial state retrieval or the PWM has not yet
575 * been configured.
576 *
577 * In either case, we setup the new period and polarity, and assign a
578 * duty cycle of 0.
579 */
580 if (!state.period) {
581 state.duty_cycle = 0;
582 state.period = pargs.period;
583 state.polarity = pargs.polarity;
584
585 return pwm_apply_state(pwm, &state);
Boris Brezillon09a7e4a2016-04-14 21:17:39 +0200586 }
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200587
588 /*
589 * Adjust the PWM duty cycle/period based on the period value provided
590 * in PWM args.
591 */
592 if (pargs.period != state.period) {
593 u64 dutycycle = (u64)state.duty_cycle * pargs.period;
594
595 do_div(dutycycle, state.period);
596 state.duty_cycle = dutycycle;
597 state.period = pargs.period;
598 }
599
600 /*
601 * If the polarity changed, we should also change the duty cycle.
602 */
603 if (pargs.polarity != state.polarity) {
604 state.polarity = pargs.polarity;
605 state.duty_cycle = state.period - state.duty_cycle;
606 }
607
608 return pwm_apply_state(pwm, &state);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100609}
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200610EXPORT_SYMBOL_GPL(pwm_adjust_config);
Thierry Reding62099ab2012-03-26 09:31:48 +0200611
Thierry Reding7299ab72011-12-14 11:10:32 +0100612static struct pwm_chip *of_node_to_pwmchip(struct device_node *np)
613{
614 struct pwm_chip *chip;
615
616 mutex_lock(&pwm_lock);
617
618 list_for_each_entry(chip, &pwm_chips, list)
619 if (chip->dev && chip->dev->of_node == np) {
620 mutex_unlock(&pwm_lock);
621 return chip;
622 }
623
624 mutex_unlock(&pwm_lock);
625
626 return ERR_PTR(-EPROBE_DEFER);
627}
628
629/**
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800630 * of_pwm_get() - request a PWM via the PWM framework
Thierry Reding7299ab72011-12-14 11:10:32 +0100631 * @np: device node to get the PWM from
632 * @con_id: consumer name
633 *
634 * Returns the PWM device parsed from the phandle and index specified in the
635 * "pwms" property of a device tree node or a negative error-code on failure.
636 * Values parsed from the device tree are stored in the returned PWM device
637 * object.
638 *
639 * If con_id is NULL, the first PWM device listed in the "pwms" property will
640 * be requested. Otherwise the "pwm-names" property is used to do a reverse
641 * lookup of the PWM index. This also means that the "pwm-names" property
642 * becomes mandatory for devices that look up the PWM device via the con_id
643 * parameter.
Thierry Reding04883802015-07-27 11:58:32 +0200644 *
645 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
646 * error code on failure.
Thierry Reding7299ab72011-12-14 11:10:32 +0100647 */
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800648struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id)
Thierry Reding7299ab72011-12-14 11:10:32 +0100649{
650 struct pwm_device *pwm = NULL;
651 struct of_phandle_args args;
652 struct pwm_chip *pc;
653 int index = 0;
654 int err;
655
656 if (con_id) {
657 index = of_property_match_string(np, "pwm-names", con_id);
658 if (index < 0)
659 return ERR_PTR(index);
660 }
661
662 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
663 &args);
664 if (err) {
Lothar Wassmannf2dafc02017-01-29 22:54:05 +0100665 pr_err("%s(): can't parse \"pwms\" property\n", __func__);
Thierry Reding7299ab72011-12-14 11:10:32 +0100666 return ERR_PTR(err);
667 }
668
669 pc = of_node_to_pwmchip(args.np);
670 if (IS_ERR(pc)) {
Jerome Brunet93c292e2017-05-23 18:05:03 +0200671 if (PTR_ERR(pc) != -EPROBE_DEFER)
672 pr_err("%s(): PWM chip not found\n", __func__);
673
Thierry Reding7299ab72011-12-14 11:10:32 +0100674 pwm = ERR_CAST(pc);
675 goto put;
676 }
677
Thierry Reding7299ab72011-12-14 11:10:32 +0100678 pwm = pc->of_xlate(pc, &args);
679 if (IS_ERR(pwm))
680 goto put;
681
682 /*
683 * If a consumer name was not given, try to look it up from the
684 * "pwm-names" property if it exists. Otherwise use the name of
685 * the user device node.
686 */
687 if (!con_id) {
688 err = of_property_read_string_index(np, "pwm-names", index,
689 &con_id);
690 if (err < 0)
691 con_id = np->name;
692 }
693
694 pwm->label = con_id;
695
696put:
697 of_node_put(args.np);
698
699 return pwm;
700}
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800701EXPORT_SYMBOL_GPL(of_pwm_get);
Thierry Reding7299ab72011-12-14 11:10:32 +0100702
Thierry Reding8138d2d2012-03-26 08:42:48 +0200703/**
704 * pwm_add_table() - register PWM device consumers
705 * @table: array of consumers to register
706 * @num: number of consumers in table
707 */
Shobhit Kumarc264f112015-03-12 22:01:31 +0530708void pwm_add_table(struct pwm_lookup *table, size_t num)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200709{
710 mutex_lock(&pwm_lookup_lock);
711
712 while (num--) {
713 list_add_tail(&table->list, &pwm_lookup_list);
714 table++;
715 }
716
717 mutex_unlock(&pwm_lookup_lock);
718}
719
720/**
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530721 * pwm_remove_table() - unregister PWM device consumers
722 * @table: array of consumers to unregister
723 * @num: number of consumers in table
724 */
725void pwm_remove_table(struct pwm_lookup *table, size_t num)
726{
727 mutex_lock(&pwm_lookup_lock);
728
729 while (num--) {
730 list_del(&table->list);
731 table++;
732 }
733
734 mutex_unlock(&pwm_lookup_lock);
735}
736
737/**
Thierry Reding8138d2d2012-03-26 08:42:48 +0200738 * pwm_get() - look up and request a PWM device
739 * @dev: device for PWM consumer
740 * @con_id: consumer name
741 *
Thierry Reding7299ab72011-12-14 11:10:32 +0100742 * Lookup is first attempted using DT. If the device was not instantiated from
743 * a device tree, a PWM chip and a relative index is looked up via a table
744 * supplied by board setup code (see pwm_add_table()).
Thierry Reding8138d2d2012-03-26 08:42:48 +0200745 *
746 * Once a PWM chip has been found the specified PWM device will be requested
747 * and is ready to be used.
Thierry Reding04883802015-07-27 11:58:32 +0200748 *
749 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
750 * error code on failure.
Thierry Reding8138d2d2012-03-26 08:42:48 +0200751 */
752struct pwm_device *pwm_get(struct device *dev, const char *con_id)
753{
Sachin Kamate50d3522012-08-10 16:41:13 +0530754 const char *dev_id = dev ? dev_name(dev) : NULL;
Hans de Goede69efb342017-01-22 17:14:07 +0100755 struct pwm_device *pwm;
756 struct pwm_chip *chip;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200757 unsigned int best = 0;
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200758 struct pwm_lookup *p, *chosen = NULL;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200759 unsigned int match;
Hans de Goedeb526a312017-01-22 17:14:08 +0100760 int err;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200761
Thierry Reding7299ab72011-12-14 11:10:32 +0100762 /* look up via DT first */
763 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800764 return of_pwm_get(dev->of_node, con_id);
Thierry Reding7299ab72011-12-14 11:10:32 +0100765
Thierry Reding8138d2d2012-03-26 08:42:48 +0200766 /*
767 * We look up the provider in the static table typically provided by
768 * board setup code. We first try to lookup the consumer device by
769 * name. If the consumer device was passed in as NULL or if no match
770 * was found, we try to find the consumer by directly looking it up
771 * by name.
772 *
773 * If a match is found, the provider PWM chip is looked up by name
774 * and a PWM device is requested using the PWM device per-chip index.
775 *
776 * The lookup algorithm was shamelessly taken from the clock
777 * framework:
778 *
779 * We do slightly fuzzy matching here:
780 * An entry with a NULL ID is assumed to be a wildcard.
781 * If an entry has a device ID, it must match
782 * If an entry has a connection ID, it must match
783 * Then we take the most specific entry - with the following order
784 * of precedence: dev+con > dev only > con only.
785 */
786 mutex_lock(&pwm_lookup_lock);
787
788 list_for_each_entry(p, &pwm_lookup_list, list) {
789 match = 0;
790
791 if (p->dev_id) {
792 if (!dev_id || strcmp(p->dev_id, dev_id))
793 continue;
794
795 match += 2;
796 }
797
798 if (p->con_id) {
799 if (!con_id || strcmp(p->con_id, con_id))
800 continue;
801
802 match += 1;
803 }
804
805 if (match > best) {
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200806 chosen = p;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200807
808 if (match != 3)
809 best = match;
810 else
811 break;
812 }
813 }
814
Hans de Goede69efb342017-01-22 17:14:07 +0100815 mutex_unlock(&pwm_lookup_lock);
816
817 if (!chosen)
818 return ERR_PTR(-ENODEV);
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200819
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200820 chip = pwmchip_find_by_name(chosen->provider);
Hans de Goedeb526a312017-01-22 17:14:08 +0100821
822 /*
823 * If the lookup entry specifies a module, load the module and retry
824 * the PWM chip lookup. This can be used to work around driver load
825 * ordering issues if driver's can't be made to properly support the
826 * deferred probe mechanism.
827 */
828 if (!chip && chosen->module) {
829 err = request_module(chosen->module);
830 if (err == 0)
831 chip = pwmchip_find_by_name(chosen->provider);
832 }
833
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200834 if (!chip)
Hans de Goede69efb342017-01-22 17:14:07 +0100835 return ERR_PTR(-EPROBE_DEFER);
Geert Uytterhoeven70145f82014-08-28 11:03:14 +0200836
837 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200838 if (IS_ERR(pwm))
Hans de Goede69efb342017-01-22 17:14:07 +0100839 return pwm;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200840
Boris Brezillonfbd45a12016-05-17 14:27:25 +0200841 pwm->args.period = chosen->period;
842 pwm->args.polarity = chosen->polarity;
843
Thierry Reding8138d2d2012-03-26 08:42:48 +0200844 return pwm;
845}
846EXPORT_SYMBOL_GPL(pwm_get);
847
848/**
849 * pwm_put() - release a PWM device
850 * @pwm: PWM device
851 */
852void pwm_put(struct pwm_device *pwm)
853{
854 if (!pwm)
855 return;
856
857 mutex_lock(&pwm_lock);
858
859 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
Sachin Kamate50d3522012-08-10 16:41:13 +0530860 pr_warn("PWM device already freed\n");
Thierry Reding8138d2d2012-03-26 08:42:48 +0200861 goto out;
862 }
863
864 if (pwm->chip->ops->free)
865 pwm->chip->ops->free(pwm->chip, pwm);
866
Uwe Kleine-Könige926b122019-03-25 10:49:33 +0100867 pwm_set_chip_data(pwm, NULL);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200868 pwm->label = NULL;
869
870 module_put(pwm->chip->ops->owner);
871out:
872 mutex_unlock(&pwm_lock);
873}
874EXPORT_SYMBOL_GPL(pwm_put);
875
Alexandre Courbot63543162012-08-01 19:20:58 +0900876static void devm_pwm_release(struct device *dev, void *res)
877{
878 pwm_put(*(struct pwm_device **)res);
879}
880
881/**
882 * devm_pwm_get() - resource managed pwm_get()
883 * @dev: device for PWM consumer
884 * @con_id: consumer name
885 *
886 * This function performs like pwm_get() but the acquired PWM device will
887 * automatically be released on driver detach.
Thierry Reding04883802015-07-27 11:58:32 +0200888 *
889 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
890 * error code on failure.
Alexandre Courbot63543162012-08-01 19:20:58 +0900891 */
892struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
893{
894 struct pwm_device **ptr, *pwm;
895
Wolfram Sang77f0b9d2013-06-03 22:27:17 +0200896 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
Alexandre Courbot63543162012-08-01 19:20:58 +0900897 if (!ptr)
898 return ERR_PTR(-ENOMEM);
899
900 pwm = pwm_get(dev, con_id);
901 if (!IS_ERR(pwm)) {
902 *ptr = pwm;
903 devres_add(dev, ptr);
904 } else {
905 devres_free(ptr);
906 }
907
908 return pwm;
909}
910EXPORT_SYMBOL_GPL(devm_pwm_get);
911
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800912/**
913 * devm_of_pwm_get() - resource managed of_pwm_get()
914 * @dev: device for PWM consumer
915 * @np: device node to get the PWM from
916 * @con_id: consumer name
917 *
918 * This function performs like of_pwm_get() but the acquired PWM device will
919 * automatically be released on driver detach.
Thierry Reding04883802015-07-27 11:58:32 +0200920 *
921 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
922 * error code on failure.
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800923 */
924struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
925 const char *con_id)
926{
927 struct pwm_device **ptr, *pwm;
928
Wolfram Sang77f0b9d2013-06-03 22:27:17 +0200929 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800930 if (!ptr)
931 return ERR_PTR(-ENOMEM);
932
933 pwm = of_pwm_get(np, con_id);
934 if (!IS_ERR(pwm)) {
935 *ptr = pwm;
936 devres_add(dev, ptr);
937 } else {
938 devres_free(ptr);
939 }
940
941 return pwm;
942}
943EXPORT_SYMBOL_GPL(devm_of_pwm_get);
944
Alexandre Courbot63543162012-08-01 19:20:58 +0900945static int devm_pwm_match(struct device *dev, void *res, void *data)
946{
947 struct pwm_device **p = res;
948
949 if (WARN_ON(!p || !*p))
950 return 0;
951
952 return *p == data;
953}
954
955/**
956 * devm_pwm_put() - resource managed pwm_put()
957 * @dev: device for PWM consumer
958 * @pwm: PWM device
959 *
960 * Release a PWM previously allocated using devm_pwm_get(). Calling this
961 * function is usually not needed because devm-allocated resources are
962 * automatically released on driver detach.
963 */
964void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
965{
966 WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm));
967}
968EXPORT_SYMBOL_GPL(devm_pwm_put);
969
Thierry Reding62099ab2012-03-26 09:31:48 +0200970#ifdef CONFIG_DEBUG_FS
971static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
972{
973 unsigned int i;
974
975 for (i = 0; i < chip->npwm; i++) {
976 struct pwm_device *pwm = &chip->pwms[i];
Boris Brezillon39100ce2016-04-14 21:17:43 +0200977 struct pwm_state state;
978
979 pwm_get_state(pwm, &state);
Thierry Reding62099ab2012-03-26 09:31:48 +0200980
981 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
982
983 if (test_bit(PWMF_REQUESTED, &pwm->flags))
Jingoo Hanadcba1e2013-12-19 13:31:24 +0900984 seq_puts(s, " requested");
Thierry Reding62099ab2012-03-26 09:31:48 +0200985
Boris Brezillon39100ce2016-04-14 21:17:43 +0200986 if (state.enabled)
Jingoo Hanadcba1e2013-12-19 13:31:24 +0900987 seq_puts(s, " enabled");
Thierry Reding62099ab2012-03-26 09:31:48 +0200988
Heiko Stübner23e35232016-04-14 21:17:44 +0200989 seq_printf(s, " period: %u ns", state.period);
990 seq_printf(s, " duty: %u ns", state.duty_cycle);
991 seq_printf(s, " polarity: %s",
992 state.polarity ? "inverse" : "normal");
993
Jingoo Hanadcba1e2013-12-19 13:31:24 +0900994 seq_puts(s, "\n");
Thierry Reding62099ab2012-03-26 09:31:48 +0200995 }
996}
997
998static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
999{
1000 mutex_lock(&pwm_lock);
1001 s->private = "";
1002
1003 return seq_list_start(&pwm_chips, *pos);
1004}
1005
1006static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
1007{
1008 s->private = "\n";
1009
1010 return seq_list_next(v, &pwm_chips, pos);
1011}
1012
1013static void pwm_seq_stop(struct seq_file *s, void *v)
1014{
1015 mutex_unlock(&pwm_lock);
1016}
1017
1018static int pwm_seq_show(struct seq_file *s, void *v)
1019{
1020 struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
1021
1022 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
1023 chip->dev->bus ? chip->dev->bus->name : "no-bus",
1024 dev_name(chip->dev), chip->npwm,
1025 (chip->npwm != 1) ? "s" : "");
1026
Uwe Kleine-Königcc2d2242019-01-07 20:49:39 +01001027 pwm_dbg_show(chip, s);
Thierry Reding62099ab2012-03-26 09:31:48 +02001028
1029 return 0;
1030}
1031
1032static const struct seq_operations pwm_seq_ops = {
1033 .start = pwm_seq_start,
1034 .next = pwm_seq_next,
1035 .stop = pwm_seq_stop,
1036 .show = pwm_seq_show,
1037};
1038
1039static int pwm_seq_open(struct inode *inode, struct file *file)
1040{
1041 return seq_open(file, &pwm_seq_ops);
1042}
1043
1044static const struct file_operations pwm_debugfs_ops = {
1045 .owner = THIS_MODULE,
1046 .open = pwm_seq_open,
1047 .read = seq_read,
1048 .llseek = seq_lseek,
1049 .release = seq_release,
1050};
1051
1052static int __init pwm_debugfs_init(void)
1053{
1054 debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
1055 &pwm_debugfs_ops);
1056
1057 return 0;
1058}
Thierry Reding62099ab2012-03-26 09:31:48 +02001059subsys_initcall(pwm_debugfs_init);
1060#endif /* CONFIG_DEBUG_FS */