Thomas Gleixner | c82ee6d | 2019-05-19 15:51:48 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Generic pwmlib implementation |
| 4 | * |
| 5 | * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de> |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 6 | * Copyright (C) 2011-2012 Avionic Design GmbH |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Nikolaus Voss | 4a6ef8e | 2019-06-12 10:36:07 +0200 | [diff] [blame] | 9 | #include <linux/acpi.h> |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/pwm.h> |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 12 | #include <linux/radix-tree.h> |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 13 | #include <linux/list.h> |
| 14 | #include <linux/mutex.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/device.h> |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 18 | #include <linux/debugfs.h> |
| 19 | #include <linux/seq_file.h> |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 20 | |
Laurent Pinchart | 208be76 | 2013-07-18 00:54:22 +0200 | [diff] [blame] | 21 | #include <dt-bindings/pwm/pwm.h> |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 22 | |
Uwe Kleine-König | 1188829 | 2019-10-24 10:08:29 +0200 | [diff] [blame] | 23 | #define CREATE_TRACE_POINTS |
| 24 | #include <trace/events/pwm.h> |
| 25 | |
Laurent Pinchart | 208be76 | 2013-07-18 00:54:22 +0200 | [diff] [blame] | 26 | #define MAX_PWMS 1024 |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 27 | |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 28 | static DEFINE_MUTEX(pwm_lookup_lock); |
| 29 | static LIST_HEAD(pwm_lookup_list); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 30 | static DEFINE_MUTEX(pwm_lock); |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 31 | static LIST_HEAD(pwm_chips); |
| 32 | static DECLARE_BITMAP(allocated_pwms, MAX_PWMS); |
| 33 | static RADIX_TREE(pwm_tree, GFP_KERNEL); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 34 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 35 | static struct pwm_device *pwm_to_device(unsigned int pwm) |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 36 | { |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 37 | return radix_tree_lookup(&pwm_tree, pwm); |
| 38 | } |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 39 | |
Uwe Kleine-König | f9a8ee8 | 2021-03-01 19:57:19 +0100 | [diff] [blame] | 40 | static int alloc_pwms(unsigned int count) |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 41 | { |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 42 | unsigned int start; |
| 43 | |
Uwe Kleine-König | f9a8ee8 | 2021-03-01 19:57:19 +0100 | [diff] [blame] | 44 | start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, 0, |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 45 | count, 0); |
| 46 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 47 | if (start + count > MAX_PWMS) |
| 48 | return -ENOSPC; |
| 49 | |
| 50 | return start; |
| 51 | } |
| 52 | |
| 53 | static void free_pwms(struct pwm_chip *chip) |
| 54 | { |
| 55 | unsigned int i; |
| 56 | |
| 57 | for (i = 0; i < chip->npwm; i++) { |
| 58 | struct pwm_device *pwm = &chip->pwms[i]; |
Thierry Reding | 83a9886 | 2016-05-02 12:05:55 +0200 | [diff] [blame] | 59 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 60 | radix_tree_delete(&pwm_tree, pwm->pwm); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 63 | bitmap_clear(allocated_pwms, chip->base, chip->npwm); |
| 64 | |
| 65 | kfree(chip->pwms); |
| 66 | chip->pwms = NULL; |
| 67 | } |
| 68 | |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 69 | static struct pwm_chip *pwmchip_find_by_name(const char *name) |
| 70 | { |
| 71 | struct pwm_chip *chip; |
| 72 | |
| 73 | if (!name) |
| 74 | return NULL; |
| 75 | |
| 76 | mutex_lock(&pwm_lock); |
| 77 | |
| 78 | list_for_each_entry(chip, &pwm_chips, list) { |
| 79 | const char *chip_name = dev_name(chip->dev); |
| 80 | |
| 81 | if (chip_name && strcmp(chip_name, name) == 0) { |
| 82 | mutex_unlock(&pwm_lock); |
| 83 | return chip; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | mutex_unlock(&pwm_lock); |
| 88 | |
| 89 | return NULL; |
| 90 | } |
| 91 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 92 | static int pwm_device_request(struct pwm_device *pwm, const char *label) |
| 93 | { |
| 94 | int err; |
| 95 | |
| 96 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) |
| 97 | return -EBUSY; |
| 98 | |
| 99 | if (!try_module_get(pwm->chip->ops->owner)) |
| 100 | return -ENODEV; |
| 101 | |
| 102 | if (pwm->chip->ops->request) { |
| 103 | err = pwm->chip->ops->request(pwm->chip, pwm); |
| 104 | if (err) { |
| 105 | module_put(pwm->chip->ops->owner); |
| 106 | return err; |
| 107 | } |
| 108 | } |
| 109 | |
Uwe Kleine-König | 1188829 | 2019-10-24 10:08:29 +0200 | [diff] [blame] | 110 | if (pwm->chip->ops->get_state) { |
Thierry Reding | cfc4c18 | 2019-10-21 12:51:56 +0200 | [diff] [blame] | 111 | pwm->chip->ops->get_state(pwm->chip, pwm, &pwm->state); |
Uwe Kleine-König | 1188829 | 2019-10-24 10:08:29 +0200 | [diff] [blame] | 112 | trace_pwm_get(pwm, &pwm->state); |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 113 | |
Kees Cook | f5641d0 | 2020-06-03 15:40:56 -0700 | [diff] [blame] | 114 | if (IS_ENABLED(CONFIG_PWM_DEBUG)) |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 115 | pwm->last = pwm->state; |
Uwe Kleine-König | 1188829 | 2019-10-24 10:08:29 +0200 | [diff] [blame] | 116 | } |
Thierry Reding | cfc4c18 | 2019-10-21 12:51:56 +0200 | [diff] [blame] | 117 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 118 | set_bit(PWMF_REQUESTED, &pwm->flags); |
| 119 | pwm->label = label; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 124 | struct pwm_device * |
| 125 | of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args) |
| 126 | { |
| 127 | struct pwm_device *pwm; |
| 128 | |
Uwe Kleine-König | cf38c97 | 2021-05-10 16:06:37 +0200 | [diff] [blame] | 129 | if (pc->of_pwm_n_cells < 2) |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 130 | return ERR_PTR(-EINVAL); |
| 131 | |
Lothar Wassmann | 42883cb | 2017-01-29 22:54:13 +0100 | [diff] [blame] | 132 | /* flags in the third cell are optional */ |
| 133 | if (args->args_count < 2) |
| 134 | return ERR_PTR(-EINVAL); |
| 135 | |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 136 | if (args->args[0] >= pc->npwm) |
| 137 | return ERR_PTR(-EINVAL); |
| 138 | |
| 139 | pwm = pwm_request_from_chip(pc, args->args[0], NULL); |
| 140 | if (IS_ERR(pwm)) |
| 141 | return pwm; |
| 142 | |
Boris Brezillon | e39c0df | 2016-04-14 21:17:21 +0200 | [diff] [blame] | 143 | pwm->args.period = args->args[1]; |
Lothar Wassmann | 42883cb | 2017-01-29 22:54:13 +0100 | [diff] [blame] | 144 | pwm->args.polarity = PWM_POLARITY_NORMAL; |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 145 | |
Uwe Kleine-König | cf38c97 | 2021-05-10 16:06:37 +0200 | [diff] [blame] | 146 | if (pc->of_pwm_n_cells >= 3) { |
| 147 | if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) |
| 148 | pwm->args.polarity = PWM_POLARITY_INVERSED; |
| 149 | } |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 150 | |
| 151 | return pwm; |
| 152 | } |
Thierry Reding | 417328c | 2012-11-28 15:12:07 +0100 | [diff] [blame] | 153 | EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags); |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 154 | |
Sachin Kamat | dfeb86e | 2012-08-02 12:32:42 +0530 | [diff] [blame] | 155 | static void of_pwmchip_add(struct pwm_chip *chip) |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 156 | { |
| 157 | if (!chip->dev || !chip->dev->of_node) |
| 158 | return; |
| 159 | |
| 160 | if (!chip->of_xlate) { |
Uwe Kleine-König | 69230cf | 2021-05-10 16:06:39 +0200 | [diff] [blame] | 161 | u32 pwm_cells; |
| 162 | |
| 163 | if (of_property_read_u32(chip->dev->of_node, "#pwm-cells", |
| 164 | &pwm_cells)) |
| 165 | pwm_cells = 2; |
| 166 | |
Uwe Kleine-König | 5447e78 | 2021-05-10 16:06:38 +0200 | [diff] [blame] | 167 | chip->of_xlate = of_pwm_xlate_with_flags; |
Uwe Kleine-König | 69230cf | 2021-05-10 16:06:39 +0200 | [diff] [blame] | 168 | chip->of_pwm_n_cells = pwm_cells; |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | of_node_get(chip->dev->of_node); |
| 172 | } |
| 173 | |
Sachin Kamat | dfeb86e | 2012-08-02 12:32:42 +0530 | [diff] [blame] | 174 | static void of_pwmchip_remove(struct pwm_chip *chip) |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 175 | { |
Markus Elfring | 8d6cc07 | 2015-02-03 11:54:28 +0100 | [diff] [blame] | 176 | if (chip->dev) |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 177 | of_node_put(chip->dev->of_node); |
| 178 | } |
| 179 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 180 | /** |
| 181 | * pwm_set_chip_data() - set private chip data for a PWM |
| 182 | * @pwm: PWM device |
| 183 | * @data: pointer to chip-specific data |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 184 | * |
| 185 | * Returns: 0 on success or a negative error code on failure. |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 186 | */ |
| 187 | int pwm_set_chip_data(struct pwm_device *pwm, void *data) |
| 188 | { |
| 189 | if (!pwm) |
| 190 | return -EINVAL; |
| 191 | |
| 192 | pwm->chip_data = data; |
| 193 | |
| 194 | return 0; |
| 195 | } |
Thierry Reding | 928c447 | 2013-01-30 09:22:24 +0100 | [diff] [blame] | 196 | EXPORT_SYMBOL_GPL(pwm_set_chip_data); |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 197 | |
| 198 | /** |
| 199 | * pwm_get_chip_data() - get private chip data for a PWM |
| 200 | * @pwm: PWM device |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 201 | * |
| 202 | * Returns: A pointer to the chip-private data for the PWM device. |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 203 | */ |
| 204 | void *pwm_get_chip_data(struct pwm_device *pwm) |
| 205 | { |
| 206 | return pwm ? pwm->chip_data : NULL; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 207 | } |
Thierry Reding | 928c447 | 2013-01-30 09:22:24 +0100 | [diff] [blame] | 208 | EXPORT_SYMBOL_GPL(pwm_get_chip_data); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 209 | |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 210 | static bool pwm_ops_check(const struct pwm_chip *chip) |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 211 | { |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 212 | |
| 213 | const struct pwm_ops *ops = chip->ops; |
| 214 | |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 215 | /* driver supports legacy, non-atomic operation */ |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 216 | if (ops->config && ops->enable && ops->disable) { |
| 217 | if (IS_ENABLED(CONFIG_PWM_DEBUG)) |
| 218 | dev_warn(chip->dev, |
| 219 | "Driver needs updating to atomic API\n"); |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 220 | |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 221 | return true; |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 222 | } |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 223 | |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 224 | if (!ops->apply) |
| 225 | return false; |
| 226 | |
| 227 | if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) |
| 228 | dev_warn(chip->dev, |
| 229 | "Please implement the .get_state() callback\n"); |
| 230 | |
| 231 | return true; |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 234 | /** |
Uwe Kleine-König | 9666cec | 2020-12-07 14:45:56 +0100 | [diff] [blame] | 235 | * pwmchip_add() - register a new PWM chip |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 236 | * @chip: the PWM chip to add |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 237 | * |
Uwe Kleine-König | 9666cec | 2020-12-07 14:45:56 +0100 | [diff] [blame] | 238 | * Register a new PWM chip. |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 239 | * |
| 240 | * Returns: 0 on success or a negative error code on failure. |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 241 | */ |
Uwe Kleine-König | 9666cec | 2020-12-07 14:45:56 +0100 | [diff] [blame] | 242 | int pwmchip_add(struct pwm_chip *chip) |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 243 | { |
| 244 | struct pwm_device *pwm; |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 245 | unsigned int i; |
| 246 | int ret; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 247 | |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 248 | if (!chip || !chip->dev || !chip->ops || !chip->npwm) |
| 249 | return -EINVAL; |
| 250 | |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 251 | if (!pwm_ops_check(chip)) |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 252 | return -EINVAL; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 253 | |
| 254 | mutex_lock(&pwm_lock); |
| 255 | |
Uwe Kleine-König | f9a8ee8 | 2021-03-01 19:57:19 +0100 | [diff] [blame] | 256 | ret = alloc_pwms(chip->npwm); |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 257 | if (ret < 0) |
| 258 | goto out; |
| 259 | |
Uwe Kleine-König | f9a8ee8 | 2021-03-01 19:57:19 +0100 | [diff] [blame] | 260 | chip->base = ret; |
| 261 | |
Thierry Reding | 2907f8a | 2016-05-02 12:07:34 +0200 | [diff] [blame] | 262 | chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL); |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 263 | if (!chip->pwms) { |
| 264 | ret = -ENOMEM; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 265 | goto out; |
| 266 | } |
| 267 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 268 | for (i = 0; i < chip->npwm; i++) { |
| 269 | pwm = &chip->pwms[i]; |
| 270 | |
| 271 | pwm->chip = chip; |
| 272 | pwm->pwm = chip->base + i; |
| 273 | pwm->hwpwm = i; |
| 274 | |
| 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 Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 285 | if (IS_ENABLED(CONFIG_OF)) |
| 286 | of_pwmchip_add(chip); |
| 287 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 288 | out: |
| 289 | mutex_unlock(&pwm_lock); |
Phong Hoang | 347ab94 | 2019-03-19 19:40:08 +0900 | [diff] [blame] | 290 | |
| 291 | if (!ret) |
| 292 | pwmchip_sysfs_export(chip); |
| 293 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 294 | return ret; |
| 295 | } |
| 296 | EXPORT_SYMBOL_GPL(pwmchip_add); |
| 297 | |
| 298 | /** |
| 299 | * pwmchip_remove() - remove a PWM chip |
| 300 | * @chip: the PWM chip to remove |
| 301 | * |
| 302 | * Removes a PWM chip. This function may return busy if the PWM chip provides |
| 303 | * a PWM device that is still requested. |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 304 | * |
| 305 | * Returns: 0 on success or a negative error code on failure. |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 306 | */ |
Uwe Kleine-König | 8083f58 | 2021-07-07 18:28:35 +0200 | [diff] [blame] | 307 | void pwmchip_remove(struct pwm_chip *chip) |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 308 | { |
Phong Hoang | 347ab94 | 2019-03-19 19:40:08 +0900 | [diff] [blame] | 309 | pwmchip_sysfs_unexport(chip); |
David Hsu | 0733424 | 2016-08-09 14:57:46 -0700 | [diff] [blame] | 310 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 311 | mutex_lock(&pwm_lock); |
| 312 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 313 | list_del_init(&chip->list); |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 314 | |
| 315 | if (IS_ENABLED(CONFIG_OF)) |
| 316 | of_pwmchip_remove(chip); |
| 317 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 318 | free_pwms(chip); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 319 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 320 | mutex_unlock(&pwm_lock); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 321 | } |
| 322 | EXPORT_SYMBOL_GPL(pwmchip_remove); |
| 323 | |
Uwe Kleine-König | bcda91b | 2021-04-07 10:01:54 +0200 | [diff] [blame] | 324 | static void devm_pwmchip_remove(void *data) |
| 325 | { |
| 326 | struct pwm_chip *chip = data; |
| 327 | |
| 328 | pwmchip_remove(chip); |
| 329 | } |
| 330 | |
| 331 | int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip) |
| 332 | { |
| 333 | int ret; |
| 334 | |
| 335 | ret = pwmchip_add(chip); |
| 336 | if (ret) |
| 337 | return ret; |
| 338 | |
| 339 | return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip); |
| 340 | } |
| 341 | EXPORT_SYMBOL_GPL(devm_pwmchip_add); |
| 342 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 343 | /** |
| 344 | * pwm_request() - request a PWM device |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 345 | * @pwm: global PWM device index |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 346 | * @label: PWM device label |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 347 | * |
| 348 | * This function is deprecated, use pwm_get() instead. |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 349 | * |
| 350 | * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on |
| 351 | * failure. |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 352 | */ |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 353 | struct pwm_device *pwm_request(int pwm, const char *label) |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 354 | { |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 355 | struct pwm_device *dev; |
| 356 | int err; |
| 357 | |
| 358 | if (pwm < 0 || pwm >= MAX_PWMS) |
| 359 | return ERR_PTR(-EINVAL); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 360 | |
| 361 | mutex_lock(&pwm_lock); |
| 362 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 363 | dev = pwm_to_device(pwm); |
| 364 | if (!dev) { |
| 365 | dev = ERR_PTR(-EPROBE_DEFER); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 366 | goto out; |
| 367 | } |
| 368 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 369 | err = pwm_device_request(dev, label); |
| 370 | if (err < 0) |
| 371 | dev = ERR_PTR(err); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 372 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 373 | out: |
| 374 | mutex_unlock(&pwm_lock); |
| 375 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 376 | return dev; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 377 | } |
| 378 | EXPORT_SYMBOL_GPL(pwm_request); |
| 379 | |
| 380 | /** |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 381 | * pwm_request_from_chip() - request a PWM device relative to a PWM chip |
| 382 | * @chip: PWM chip |
| 383 | * @index: per-chip index of the PWM to request |
| 384 | * @label: a literal description string of this PWM |
| 385 | * |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 386 | * Returns: A pointer to the PWM device at the given index of the given PWM |
| 387 | * chip. A negative error code is returned if the index is not valid for the |
| 388 | * specified PWM chip or if the PWM device cannot be requested. |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 389 | */ |
| 390 | struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, |
| 391 | unsigned int index, |
| 392 | const char *label) |
| 393 | { |
| 394 | struct pwm_device *pwm; |
| 395 | int err; |
| 396 | |
| 397 | if (!chip || index >= chip->npwm) |
| 398 | return ERR_PTR(-EINVAL); |
| 399 | |
| 400 | mutex_lock(&pwm_lock); |
| 401 | pwm = &chip->pwms[index]; |
| 402 | |
| 403 | err = pwm_device_request(pwm, label); |
| 404 | if (err < 0) |
| 405 | pwm = ERR_PTR(err); |
| 406 | |
| 407 | mutex_unlock(&pwm_lock); |
| 408 | return pwm; |
| 409 | } |
| 410 | EXPORT_SYMBOL_GPL(pwm_request_from_chip); |
| 411 | |
| 412 | /** |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 413 | * pwm_free() - free a PWM device |
| 414 | * @pwm: PWM device |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 415 | * |
| 416 | * This function is deprecated, use pwm_put() instead. |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 417 | */ |
| 418 | void pwm_free(struct pwm_device *pwm) |
| 419 | { |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 420 | pwm_put(pwm); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 421 | } |
| 422 | EXPORT_SYMBOL_GPL(pwm_free); |
| 423 | |
Jason Yan | 374c110 | 2020-04-02 14:57:18 +0800 | [diff] [blame] | 424 | static void pwm_apply_state_debug(struct pwm_device *pwm, |
| 425 | const struct pwm_state *state) |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 426 | { |
| 427 | struct pwm_state *last = &pwm->last; |
| 428 | struct pwm_chip *chip = pwm->chip; |
| 429 | struct pwm_state s1, s2; |
| 430 | int err; |
| 431 | |
| 432 | if (!IS_ENABLED(CONFIG_PWM_DEBUG)) |
| 433 | return; |
| 434 | |
| 435 | /* No reasonable diagnosis possible without .get_state() */ |
| 436 | if (!chip->ops->get_state) |
| 437 | return; |
| 438 | |
| 439 | /* |
| 440 | * *state was just applied. Read out the hardware state and do some |
| 441 | * checks. |
| 442 | */ |
| 443 | |
| 444 | chip->ops->get_state(chip, pwm, &s1); |
| 445 | trace_pwm_get(pwm, &s1); |
| 446 | |
| 447 | /* |
| 448 | * The lowlevel driver either ignored .polarity (which is a bug) or as |
| 449 | * best effort inverted .polarity and fixed .duty_cycle respectively. |
| 450 | * Undo this inversion and fixup for further tests. |
| 451 | */ |
| 452 | if (s1.enabled && s1.polarity != state->polarity) { |
| 453 | s2.polarity = state->polarity; |
| 454 | s2.duty_cycle = s1.period - s1.duty_cycle; |
| 455 | s2.period = s1.period; |
| 456 | s2.enabled = s1.enabled; |
| 457 | } else { |
| 458 | s2 = s1; |
| 459 | } |
| 460 | |
| 461 | if (s2.polarity != state->polarity && |
| 462 | state->duty_cycle < state->period) |
| 463 | dev_warn(chip->dev, ".apply ignored .polarity\n"); |
| 464 | |
| 465 | if (state->enabled && |
| 466 | last->polarity == state->polarity && |
| 467 | last->period > s2.period && |
| 468 | last->period <= state->period) |
| 469 | dev_warn(chip->dev, |
Guru Das Srinagesh | a9d887d | 2020-06-02 15:31:16 -0700 | [diff] [blame] | 470 | ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n", |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 471 | state->period, s2.period, last->period); |
| 472 | |
| 473 | if (state->enabled && state->period < s2.period) |
| 474 | dev_warn(chip->dev, |
Guru Das Srinagesh | a9d887d | 2020-06-02 15:31:16 -0700 | [diff] [blame] | 475 | ".apply is supposed to round down period (requested: %llu, applied: %llu)\n", |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 476 | state->period, s2.period); |
| 477 | |
| 478 | if (state->enabled && |
| 479 | last->polarity == state->polarity && |
| 480 | last->period == s2.period && |
| 481 | last->duty_cycle > s2.duty_cycle && |
| 482 | last->duty_cycle <= state->duty_cycle) |
| 483 | dev_warn(chip->dev, |
Guru Das Srinagesh | a9d887d | 2020-06-02 15:31:16 -0700 | [diff] [blame] | 484 | ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n", |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 485 | state->duty_cycle, state->period, |
| 486 | s2.duty_cycle, s2.period, |
| 487 | last->duty_cycle, last->period); |
| 488 | |
| 489 | if (state->enabled && state->duty_cycle < s2.duty_cycle) |
| 490 | dev_warn(chip->dev, |
Guru Das Srinagesh | a9d887d | 2020-06-02 15:31:16 -0700 | [diff] [blame] | 491 | ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n", |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 492 | state->duty_cycle, state->period, |
| 493 | s2.duty_cycle, s2.period); |
| 494 | |
| 495 | if (!state->enabled && s2.enabled && s2.duty_cycle > 0) |
| 496 | dev_warn(chip->dev, |
Christophe JAILLET | db539cb | 2020-04-11 17:35:28 +0200 | [diff] [blame] | 497 | "requested disabled, but yielded enabled with duty > 0\n"); |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 498 | |
| 499 | /* reapply the state that the driver reported being configured. */ |
| 500 | err = chip->ops->apply(chip, pwm, &s1); |
| 501 | if (err) { |
| 502 | *last = s1; |
| 503 | dev_err(chip->dev, "failed to reapply current setting\n"); |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | trace_pwm_apply(pwm, &s1); |
| 508 | |
| 509 | chip->ops->get_state(chip, pwm, last); |
| 510 | trace_pwm_get(pwm, last); |
| 511 | |
| 512 | /* reapplication of the current state should give an exact match */ |
| 513 | if (s1.enabled != last->enabled || |
| 514 | s1.polarity != last->polarity || |
| 515 | (s1.enabled && s1.period != last->period) || |
| 516 | (s1.enabled && s1.duty_cycle != last->duty_cycle)) { |
| 517 | dev_err(chip->dev, |
Guru Das Srinagesh | a9d887d | 2020-06-02 15:31:16 -0700 | [diff] [blame] | 518 | ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n", |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 519 | s1.enabled, s1.polarity, s1.duty_cycle, s1.period, |
| 520 | last->enabled, last->polarity, last->duty_cycle, |
| 521 | last->period); |
| 522 | } |
| 523 | } |
| 524 | |
Uwe Kleine-König | 77965c98 | 2021-07-01 09:29:25 +0200 | [diff] [blame^] | 525 | static int pwm_apply_legacy(struct pwm_chip *chip, struct pwm_device *pwm, |
| 526 | const struct pwm_state *state) |
| 527 | { |
| 528 | int err; |
| 529 | |
| 530 | /* |
| 531 | * FIXME: restore the initial state in case of error. |
| 532 | */ |
| 533 | if (state->polarity != pwm->state.polarity) { |
| 534 | if (!chip->ops->set_polarity) |
| 535 | return -EINVAL; |
| 536 | |
| 537 | /* |
| 538 | * Changing the polarity of a running PWM is only allowed when |
| 539 | * the PWM driver implements ->apply(). |
| 540 | */ |
| 541 | if (pwm->state.enabled) { |
| 542 | chip->ops->disable(chip, pwm); |
| 543 | |
| 544 | /* |
| 545 | * Update pwm->state already here in case |
| 546 | * .set_polarity() or another callback depend on that. |
| 547 | */ |
| 548 | pwm->state.enabled = false; |
| 549 | } |
| 550 | |
| 551 | err = chip->ops->set_polarity(chip, pwm, state->polarity); |
| 552 | if (err) |
| 553 | return err; |
| 554 | |
| 555 | pwm->state.polarity = state->polarity; |
| 556 | } |
| 557 | |
| 558 | if (state->period != pwm->state.period || |
| 559 | state->duty_cycle != pwm->state.duty_cycle) { |
| 560 | err = chip->ops->config(pwm->chip, pwm, |
| 561 | state->duty_cycle, |
| 562 | state->period); |
| 563 | if (err) |
| 564 | return err; |
| 565 | |
| 566 | pwm->state.period = state->period; |
| 567 | pwm->state.duty_cycle = state->duty_cycle; |
| 568 | } |
| 569 | |
| 570 | if (state->enabled != pwm->state.enabled) { |
| 571 | if (!pwm->state.enabled) { |
| 572 | err = chip->ops->enable(chip, pwm); |
| 573 | if (err) |
| 574 | return err; |
| 575 | } else { |
| 576 | chip->ops->disable(chip, pwm); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 583 | /** |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 584 | * pwm_apply_state() - atomically apply a new state to a PWM device |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 585 | * @pwm: PWM device |
Uwe Kleine-König | 71523d1 | 2019-08-24 17:37:07 +0200 | [diff] [blame] | 586 | * @state: new state to apply |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 587 | */ |
Uwe Kleine-König | 71523d1 | 2019-08-24 17:37:07 +0200 | [diff] [blame] | 588 | int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state) |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 589 | { |
Uwe Kleine-König | fc3c551 | 2019-08-24 17:37:02 +0200 | [diff] [blame] | 590 | struct pwm_chip *chip; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 591 | int err; |
| 592 | |
Uwe Kleine-König | 4ad91a2 | 2021-09-09 11:48:49 +0200 | [diff] [blame] | 593 | /* |
| 594 | * Some lowlevel driver's implementations of .apply() make use of |
| 595 | * mutexes, also with some drivers only returning when the new |
| 596 | * configuration is active calling pwm_apply_state() from atomic context |
| 597 | * is a bad idea. So make it explicit that calling this function might |
| 598 | * sleep. |
| 599 | */ |
| 600 | might_sleep(); |
| 601 | |
Brian Norris | ef2bf49 | 2016-05-27 09:45:49 -0700 | [diff] [blame] | 602 | if (!pwm || !state || !state->period || |
| 603 | state->duty_cycle > state->period) |
Jonathan Richardson | d1cd214 | 2015-10-16 17:40:58 -0700 | [diff] [blame] | 604 | return -EINVAL; |
| 605 | |
Uwe Kleine-König | fc3c551 | 2019-08-24 17:37:02 +0200 | [diff] [blame] | 606 | chip = pwm->chip; |
| 607 | |
Uwe Kleine-König | 309b32f | 2019-01-07 20:49:37 +0100 | [diff] [blame] | 608 | if (state->period == pwm->state.period && |
| 609 | state->duty_cycle == pwm->state.duty_cycle && |
| 610 | state->polarity == pwm->state.polarity && |
Clemens Gruber | 9e40ee1 | 2021-05-07 15:18:42 +0200 | [diff] [blame] | 611 | state->enabled == pwm->state.enabled && |
| 612 | state->usage_power == pwm->state.usage_power) |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 613 | return 0; |
Jonathan Richardson | d1cd214 | 2015-10-16 17:40:58 -0700 | [diff] [blame] | 614 | |
Uwe Kleine-König | 77965c98 | 2021-07-01 09:29:25 +0200 | [diff] [blame^] | 615 | if (chip->ops->apply) |
Uwe Kleine-König | fc3c551 | 2019-08-24 17:37:02 +0200 | [diff] [blame] | 616 | err = chip->ops->apply(chip, pwm, state); |
Uwe Kleine-König | 77965c98 | 2021-07-01 09:29:25 +0200 | [diff] [blame^] | 617 | else |
| 618 | err = pwm_apply_legacy(chip, pwm, state); |
| 619 | if (err) |
| 620 | return err; |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 621 | |
Uwe Kleine-König | 77965c98 | 2021-07-01 09:29:25 +0200 | [diff] [blame^] | 622 | trace_pwm_apply(pwm, state); |
Uwe Kleine-König | 1188829 | 2019-10-24 10:08:29 +0200 | [diff] [blame] | 623 | |
Uwe Kleine-König | 77965c98 | 2021-07-01 09:29:25 +0200 | [diff] [blame^] | 624 | pwm->state = *state; |
Uwe Kleine-König | 3ad1f3a | 2020-02-10 22:35:18 +0100 | [diff] [blame] | 625 | |
Uwe Kleine-König | 77965c98 | 2021-07-01 09:29:25 +0200 | [diff] [blame^] | 626 | /* |
| 627 | * only do this after pwm->state was applied as some |
| 628 | * implementations of .get_state depend on this |
| 629 | */ |
| 630 | pwm_apply_state_debug(pwm, state); |
Jonathan Richardson | d1cd214 | 2015-10-16 17:40:58 -0700 | [diff] [blame] | 631 | |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 632 | return 0; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 633 | } |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 634 | EXPORT_SYMBOL_GPL(pwm_apply_state); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 635 | |
| 636 | /** |
Lee Jones | 3a3d1a4 | 2016-06-08 10:21:23 +0100 | [diff] [blame] | 637 | * pwm_capture() - capture and report a PWM signal |
| 638 | * @pwm: PWM device |
| 639 | * @result: structure to fill with capture result |
| 640 | * @timeout: time to wait, in milliseconds, before giving up on capture |
| 641 | * |
| 642 | * Returns: 0 on success or a negative error code on failure. |
| 643 | */ |
| 644 | int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, |
| 645 | unsigned long timeout) |
| 646 | { |
| 647 | int err; |
| 648 | |
| 649 | if (!pwm || !pwm->chip->ops) |
| 650 | return -EINVAL; |
| 651 | |
| 652 | if (!pwm->chip->ops->capture) |
| 653 | return -ENOSYS; |
| 654 | |
| 655 | mutex_lock(&pwm_lock); |
| 656 | err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout); |
| 657 | mutex_unlock(&pwm_lock); |
| 658 | |
| 659 | return err; |
| 660 | } |
| 661 | EXPORT_SYMBOL_GPL(pwm_capture); |
| 662 | |
| 663 | /** |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 664 | * pwm_adjust_config() - adjust the current PWM config to the PWM arguments |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 665 | * @pwm: PWM device |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 666 | * |
| 667 | * This function will adjust the PWM config to the PWM arguments provided |
| 668 | * by the DT or PWM lookup table. This is particularly useful to adapt |
| 669 | * the bootloader config to the Linux one. |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 670 | */ |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 671 | int pwm_adjust_config(struct pwm_device *pwm) |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 672 | { |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 673 | struct pwm_state state; |
| 674 | struct pwm_args pargs; |
Boris Brezillon | 09a7e4a | 2016-04-14 21:17:39 +0200 | [diff] [blame] | 675 | |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 676 | pwm_get_args(pwm, &pargs); |
| 677 | pwm_get_state(pwm, &state); |
| 678 | |
| 679 | /* |
| 680 | * If the current period is zero it means that either the PWM driver |
| 681 | * does not support initial state retrieval or the PWM has not yet |
| 682 | * been configured. |
| 683 | * |
| 684 | * In either case, we setup the new period and polarity, and assign a |
| 685 | * duty cycle of 0. |
| 686 | */ |
| 687 | if (!state.period) { |
| 688 | state.duty_cycle = 0; |
| 689 | state.period = pargs.period; |
| 690 | state.polarity = pargs.polarity; |
| 691 | |
| 692 | return pwm_apply_state(pwm, &state); |
Boris Brezillon | 09a7e4a | 2016-04-14 21:17:39 +0200 | [diff] [blame] | 693 | } |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 694 | |
| 695 | /* |
| 696 | * Adjust the PWM duty cycle/period based on the period value provided |
| 697 | * in PWM args. |
| 698 | */ |
| 699 | if (pargs.period != state.period) { |
| 700 | u64 dutycycle = (u64)state.duty_cycle * pargs.period; |
| 701 | |
| 702 | do_div(dutycycle, state.period); |
| 703 | state.duty_cycle = dutycycle; |
| 704 | state.period = pargs.period; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * If the polarity changed, we should also change the duty cycle. |
| 709 | */ |
| 710 | if (pargs.polarity != state.polarity) { |
| 711 | state.polarity = pargs.polarity; |
| 712 | state.duty_cycle = state.period - state.duty_cycle; |
| 713 | } |
| 714 | |
| 715 | return pwm_apply_state(pwm, &state); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 716 | } |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 717 | EXPORT_SYMBOL_GPL(pwm_adjust_config); |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 718 | |
Andy Shevchenko | ca06616 | 2021-06-07 15:24:54 +0300 | [diff] [blame] | 719 | static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode) |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 720 | { |
| 721 | struct pwm_chip *chip; |
| 722 | |
| 723 | mutex_lock(&pwm_lock); |
| 724 | |
| 725 | list_for_each_entry(chip, &pwm_chips, list) |
Andy Shevchenko | ca06616 | 2021-06-07 15:24:54 +0300 | [diff] [blame] | 726 | if (chip->dev && dev_fwnode(chip->dev) == fwnode) { |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 727 | mutex_unlock(&pwm_lock); |
| 728 | return chip; |
| 729 | } |
| 730 | |
| 731 | mutex_unlock(&pwm_lock); |
| 732 | |
| 733 | return ERR_PTR(-EPROBE_DEFER); |
| 734 | } |
| 735 | |
Fabrice Gasnier | b2c200e | 2019-04-18 11:37:47 +0200 | [diff] [blame] | 736 | static struct device_link *pwm_device_link_add(struct device *dev, |
| 737 | struct pwm_device *pwm) |
| 738 | { |
| 739 | struct device_link *dl; |
| 740 | |
| 741 | if (!dev) { |
| 742 | /* |
| 743 | * No device for the PWM consumer has been provided. It may |
| 744 | * impact the PM sequence ordering: the PWM supplier may get |
| 745 | * suspended before the consumer. |
| 746 | */ |
| 747 | dev_warn(pwm->chip->dev, |
| 748 | "No consumer device specified to create a link to\n"); |
| 749 | return NULL; |
| 750 | } |
| 751 | |
| 752 | dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER); |
| 753 | if (!dl) { |
| 754 | dev_err(dev, "failed to create device link to %s\n", |
| 755 | dev_name(pwm->chip->dev)); |
| 756 | return ERR_PTR(-EINVAL); |
| 757 | } |
| 758 | |
| 759 | return dl; |
| 760 | } |
| 761 | |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 762 | /** |
Peter Ujfalusi | 8eb9612 | 2012-12-21 01:43:58 -0800 | [diff] [blame] | 763 | * of_pwm_get() - request a PWM via the PWM framework |
Fabrice Gasnier | b2c200e | 2019-04-18 11:37:47 +0200 | [diff] [blame] | 764 | * @dev: device for PWM consumer |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 765 | * @np: device node to get the PWM from |
| 766 | * @con_id: consumer name |
| 767 | * |
| 768 | * Returns the PWM device parsed from the phandle and index specified in the |
| 769 | * "pwms" property of a device tree node or a negative error-code on failure. |
| 770 | * Values parsed from the device tree are stored in the returned PWM device |
| 771 | * object. |
| 772 | * |
| 773 | * If con_id is NULL, the first PWM device listed in the "pwms" property will |
| 774 | * be requested. Otherwise the "pwm-names" property is used to do a reverse |
| 775 | * lookup of the PWM index. This also means that the "pwm-names" property |
| 776 | * becomes mandatory for devices that look up the PWM device via the con_id |
| 777 | * parameter. |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 778 | * |
| 779 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 780 | * error code on failure. |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 781 | */ |
Fabrice Gasnier | b2c200e | 2019-04-18 11:37:47 +0200 | [diff] [blame] | 782 | struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np, |
| 783 | const char *con_id) |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 784 | { |
| 785 | struct pwm_device *pwm = NULL; |
| 786 | struct of_phandle_args args; |
Fabrice Gasnier | b2c200e | 2019-04-18 11:37:47 +0200 | [diff] [blame] | 787 | struct device_link *dl; |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 788 | struct pwm_chip *pc; |
| 789 | int index = 0; |
| 790 | int err; |
| 791 | |
| 792 | if (con_id) { |
| 793 | index = of_property_match_string(np, "pwm-names", con_id); |
| 794 | if (index < 0) |
| 795 | return ERR_PTR(index); |
| 796 | } |
| 797 | |
| 798 | err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, |
| 799 | &args); |
| 800 | if (err) { |
Lothar Wassmann | f2dafc0 | 2017-01-29 22:54:05 +0100 | [diff] [blame] | 801 | pr_err("%s(): can't parse \"pwms\" property\n", __func__); |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 802 | return ERR_PTR(err); |
| 803 | } |
| 804 | |
Andy Shevchenko | ca06616 | 2021-06-07 15:24:54 +0300 | [diff] [blame] | 805 | pc = fwnode_to_pwmchip(of_fwnode_handle(args.np)); |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 806 | if (IS_ERR(pc)) { |
Jerome Brunet | 93c292e | 2017-05-23 18:05:03 +0200 | [diff] [blame] | 807 | if (PTR_ERR(pc) != -EPROBE_DEFER) |
| 808 | pr_err("%s(): PWM chip not found\n", __func__); |
| 809 | |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 810 | pwm = ERR_CAST(pc); |
| 811 | goto put; |
| 812 | } |
| 813 | |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 814 | pwm = pc->of_xlate(pc, &args); |
| 815 | if (IS_ERR(pwm)) |
| 816 | goto put; |
| 817 | |
Fabrice Gasnier | b2c200e | 2019-04-18 11:37:47 +0200 | [diff] [blame] | 818 | dl = pwm_device_link_add(dev, pwm); |
| 819 | if (IS_ERR(dl)) { |
| 820 | /* of_xlate ended up calling pwm_request_from_chip() */ |
| 821 | pwm_free(pwm); |
| 822 | pwm = ERR_CAST(dl); |
| 823 | goto put; |
| 824 | } |
| 825 | |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 826 | /* |
| 827 | * If a consumer name was not given, try to look it up from the |
| 828 | * "pwm-names" property if it exists. Otherwise use the name of |
| 829 | * the user device node. |
| 830 | */ |
| 831 | if (!con_id) { |
| 832 | err = of_property_read_string_index(np, "pwm-names", index, |
| 833 | &con_id); |
| 834 | if (err < 0) |
| 835 | con_id = np->name; |
| 836 | } |
| 837 | |
| 838 | pwm->label = con_id; |
| 839 | |
| 840 | put: |
| 841 | of_node_put(args.np); |
| 842 | |
| 843 | return pwm; |
| 844 | } |
Peter Ujfalusi | 8eb9612 | 2012-12-21 01:43:58 -0800 | [diff] [blame] | 845 | EXPORT_SYMBOL_GPL(of_pwm_get); |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 846 | |
Nikolaus Voss | 4a6ef8e | 2019-06-12 10:36:07 +0200 | [diff] [blame] | 847 | /** |
| 848 | * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI |
Andy Shevchenko | e625fb7 | 2021-06-07 15:24:56 +0300 | [diff] [blame] | 849 | * @fwnode: firmware node to get the "pwms" property from |
Nikolaus Voss | 4a6ef8e | 2019-06-12 10:36:07 +0200 | [diff] [blame] | 850 | * |
| 851 | * Returns the PWM device parsed from the fwnode and index specified in the |
| 852 | * "pwms" property or a negative error-code on failure. |
| 853 | * Values parsed from the device tree are stored in the returned PWM device |
| 854 | * object. |
| 855 | * |
| 856 | * This is analogous to of_pwm_get() except con_id is not yet supported. |
| 857 | * ACPI entries must look like |
| 858 | * Package () {"pwms", Package () |
| 859 | * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}} |
| 860 | * |
| 861 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 862 | * error code on failure. |
| 863 | */ |
Andy Shevchenko | e625fb7 | 2021-06-07 15:24:56 +0300 | [diff] [blame] | 864 | static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode) |
Nikolaus Voss | 4a6ef8e | 2019-06-12 10:36:07 +0200 | [diff] [blame] | 865 | { |
Colin Ian King | bebedf2 | 2021-07-06 16:11:32 +0100 | [diff] [blame] | 866 | struct pwm_device *pwm; |
Nikolaus Voss | 4a6ef8e | 2019-06-12 10:36:07 +0200 | [diff] [blame] | 867 | struct fwnode_reference_args args; |
Nikolaus Voss | 4a6ef8e | 2019-06-12 10:36:07 +0200 | [diff] [blame] | 868 | struct pwm_chip *chip; |
| 869 | int ret; |
| 870 | |
| 871 | memset(&args, 0, sizeof(args)); |
| 872 | |
| 873 | ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args); |
| 874 | if (ret < 0) |
| 875 | return ERR_PTR(ret); |
| 876 | |
Nikolaus Voss | 4a6ef8e | 2019-06-12 10:36:07 +0200 | [diff] [blame] | 877 | if (args.nargs < 2) |
| 878 | return ERR_PTR(-EPROTO); |
| 879 | |
Andy Shevchenko | e5c38ba | 2021-06-07 15:24:55 +0300 | [diff] [blame] | 880 | chip = fwnode_to_pwmchip(args.fwnode); |
Nikolaus Voss | 4a6ef8e | 2019-06-12 10:36:07 +0200 | [diff] [blame] | 881 | if (IS_ERR(chip)) |
| 882 | return ERR_CAST(chip); |
| 883 | |
| 884 | pwm = pwm_request_from_chip(chip, args.args[0], NULL); |
| 885 | if (IS_ERR(pwm)) |
| 886 | return pwm; |
| 887 | |
| 888 | pwm->args.period = args.args[1]; |
| 889 | pwm->args.polarity = PWM_POLARITY_NORMAL; |
| 890 | |
| 891 | if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED) |
| 892 | pwm->args.polarity = PWM_POLARITY_INVERSED; |
Nikolaus Voss | 4a6ef8e | 2019-06-12 10:36:07 +0200 | [diff] [blame] | 893 | |
| 894 | return pwm; |
| 895 | } |
| 896 | |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 897 | /** |
| 898 | * pwm_add_table() - register PWM device consumers |
| 899 | * @table: array of consumers to register |
| 900 | * @num: number of consumers in table |
| 901 | */ |
Shobhit Kumar | c264f11 | 2015-03-12 22:01:31 +0530 | [diff] [blame] | 902 | void pwm_add_table(struct pwm_lookup *table, size_t num) |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 903 | { |
| 904 | mutex_lock(&pwm_lookup_lock); |
| 905 | |
| 906 | while (num--) { |
| 907 | list_add_tail(&table->list, &pwm_lookup_list); |
| 908 | table++; |
| 909 | } |
| 910 | |
| 911 | mutex_unlock(&pwm_lookup_lock); |
| 912 | } |
| 913 | |
| 914 | /** |
Shobhit Kumar | efb0de5 | 2015-05-05 15:04:18 +0530 | [diff] [blame] | 915 | * pwm_remove_table() - unregister PWM device consumers |
| 916 | * @table: array of consumers to unregister |
| 917 | * @num: number of consumers in table |
| 918 | */ |
| 919 | void pwm_remove_table(struct pwm_lookup *table, size_t num) |
| 920 | { |
| 921 | mutex_lock(&pwm_lookup_lock); |
| 922 | |
| 923 | while (num--) { |
| 924 | list_del(&table->list); |
| 925 | table++; |
| 926 | } |
| 927 | |
| 928 | mutex_unlock(&pwm_lookup_lock); |
| 929 | } |
| 930 | |
| 931 | /** |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 932 | * pwm_get() - look up and request a PWM device |
| 933 | * @dev: device for PWM consumer |
| 934 | * @con_id: consumer name |
| 935 | * |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 936 | * Lookup is first attempted using DT. If the device was not instantiated from |
| 937 | * a device tree, a PWM chip and a relative index is looked up via a table |
| 938 | * supplied by board setup code (see pwm_add_table()). |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 939 | * |
| 940 | * Once a PWM chip has been found the specified PWM device will be requested |
| 941 | * and is ready to be used. |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 942 | * |
| 943 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 944 | * error code on failure. |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 945 | */ |
| 946 | struct pwm_device *pwm_get(struct device *dev, const char *con_id) |
| 947 | { |
Andy Shevchenko | e625fb7 | 2021-06-07 15:24:56 +0300 | [diff] [blame] | 948 | const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL; |
Sachin Kamat | e50d352 | 2012-08-10 16:41:13 +0530 | [diff] [blame] | 949 | const char *dev_id = dev ? dev_name(dev) : NULL; |
Hans de Goede | 69efb34 | 2017-01-22 17:14:07 +0100 | [diff] [blame] | 950 | struct pwm_device *pwm; |
| 951 | struct pwm_chip *chip; |
Fabrice Gasnier | b2c200e | 2019-04-18 11:37:47 +0200 | [diff] [blame] | 952 | struct device_link *dl; |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 953 | unsigned int best = 0; |
Geert Uytterhoeven | 70145f8 | 2014-08-28 11:03:14 +0200 | [diff] [blame] | 954 | struct pwm_lookup *p, *chosen = NULL; |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 955 | unsigned int match; |
Hans de Goede | b526a31 | 2017-01-22 17:14:08 +0100 | [diff] [blame] | 956 | int err; |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 957 | |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 958 | /* look up via DT first */ |
Andy Shevchenko | e625fb7 | 2021-06-07 15:24:56 +0300 | [diff] [blame] | 959 | if (is_of_node(fwnode)) |
| 960 | return of_pwm_get(dev, to_of_node(fwnode), con_id); |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 961 | |
Nikolaus Voss | 4a6ef8e | 2019-06-12 10:36:07 +0200 | [diff] [blame] | 962 | /* then lookup via ACPI */ |
Andy Shevchenko | e625fb7 | 2021-06-07 15:24:56 +0300 | [diff] [blame] | 963 | if (is_acpi_node(fwnode)) { |
| 964 | pwm = acpi_pwm_get(fwnode); |
Hans de Goede | 6cf9481 | 2019-07-30 17:48:48 +0200 | [diff] [blame] | 965 | if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) |
| 966 | return pwm; |
| 967 | } |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 968 | |
| 969 | /* |
| 970 | * We look up the provider in the static table typically provided by |
| 971 | * board setup code. We first try to lookup the consumer device by |
| 972 | * name. If the consumer device was passed in as NULL or if no match |
| 973 | * was found, we try to find the consumer by directly looking it up |
| 974 | * by name. |
| 975 | * |
| 976 | * If a match is found, the provider PWM chip is looked up by name |
| 977 | * and a PWM device is requested using the PWM device per-chip index. |
| 978 | * |
| 979 | * The lookup algorithm was shamelessly taken from the clock |
| 980 | * framework: |
| 981 | * |
| 982 | * We do slightly fuzzy matching here: |
| 983 | * An entry with a NULL ID is assumed to be a wildcard. |
| 984 | * If an entry has a device ID, it must match |
| 985 | * If an entry has a connection ID, it must match |
| 986 | * Then we take the most specific entry - with the following order |
| 987 | * of precedence: dev+con > dev only > con only. |
| 988 | */ |
| 989 | mutex_lock(&pwm_lookup_lock); |
| 990 | |
| 991 | list_for_each_entry(p, &pwm_lookup_list, list) { |
| 992 | match = 0; |
| 993 | |
| 994 | if (p->dev_id) { |
| 995 | if (!dev_id || strcmp(p->dev_id, dev_id)) |
| 996 | continue; |
| 997 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 998 | match += 2; |
| 999 | } |
| 1000 | |
| 1001 | if (p->con_id) { |
| 1002 | if (!con_id || strcmp(p->con_id, con_id)) |
| 1003 | continue; |
| 1004 | |
| 1005 | match += 1; |
| 1006 | } |
| 1007 | |
| 1008 | if (match > best) { |
Geert Uytterhoeven | 70145f8 | 2014-08-28 11:03:14 +0200 | [diff] [blame] | 1009 | chosen = p; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1010 | |
| 1011 | if (match != 3) |
| 1012 | best = match; |
| 1013 | else |
| 1014 | break; |
| 1015 | } |
| 1016 | } |
| 1017 | |
Hans de Goede | 69efb34 | 2017-01-22 17:14:07 +0100 | [diff] [blame] | 1018 | mutex_unlock(&pwm_lookup_lock); |
| 1019 | |
| 1020 | if (!chosen) |
| 1021 | return ERR_PTR(-ENODEV); |
Alexandre Belloni | 3796ce1 | 2014-05-19 22:42:32 +0200 | [diff] [blame] | 1022 | |
Geert Uytterhoeven | 70145f8 | 2014-08-28 11:03:14 +0200 | [diff] [blame] | 1023 | chip = pwmchip_find_by_name(chosen->provider); |
Hans de Goede | b526a31 | 2017-01-22 17:14:08 +0100 | [diff] [blame] | 1024 | |
| 1025 | /* |
| 1026 | * If the lookup entry specifies a module, load the module and retry |
| 1027 | * the PWM chip lookup. This can be used to work around driver load |
| 1028 | * ordering issues if driver's can't be made to properly support the |
| 1029 | * deferred probe mechanism. |
| 1030 | */ |
| 1031 | if (!chip && chosen->module) { |
| 1032 | err = request_module(chosen->module); |
| 1033 | if (err == 0) |
| 1034 | chip = pwmchip_find_by_name(chosen->provider); |
| 1035 | } |
| 1036 | |
Geert Uytterhoeven | 70145f8 | 2014-08-28 11:03:14 +0200 | [diff] [blame] | 1037 | if (!chip) |
Hans de Goede | 69efb34 | 2017-01-22 17:14:07 +0100 | [diff] [blame] | 1038 | return ERR_PTR(-EPROBE_DEFER); |
Geert Uytterhoeven | 70145f8 | 2014-08-28 11:03:14 +0200 | [diff] [blame] | 1039 | |
| 1040 | pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); |
Alexandre Belloni | 3796ce1 | 2014-05-19 22:42:32 +0200 | [diff] [blame] | 1041 | if (IS_ERR(pwm)) |
Hans de Goede | 69efb34 | 2017-01-22 17:14:07 +0100 | [diff] [blame] | 1042 | return pwm; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1043 | |
Fabrice Gasnier | b2c200e | 2019-04-18 11:37:47 +0200 | [diff] [blame] | 1044 | dl = pwm_device_link_add(dev, pwm); |
| 1045 | if (IS_ERR(dl)) { |
| 1046 | pwm_free(pwm); |
| 1047 | return ERR_CAST(dl); |
| 1048 | } |
| 1049 | |
Boris Brezillon | fbd45a1 | 2016-05-17 14:27:25 +0200 | [diff] [blame] | 1050 | pwm->args.period = chosen->period; |
| 1051 | pwm->args.polarity = chosen->polarity; |
| 1052 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1053 | return pwm; |
| 1054 | } |
| 1055 | EXPORT_SYMBOL_GPL(pwm_get); |
| 1056 | |
| 1057 | /** |
| 1058 | * pwm_put() - release a PWM device |
| 1059 | * @pwm: PWM device |
| 1060 | */ |
| 1061 | void pwm_put(struct pwm_device *pwm) |
| 1062 | { |
| 1063 | if (!pwm) |
| 1064 | return; |
| 1065 | |
| 1066 | mutex_lock(&pwm_lock); |
| 1067 | |
| 1068 | if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { |
Sachin Kamat | e50d352 | 2012-08-10 16:41:13 +0530 | [diff] [blame] | 1069 | pr_warn("PWM device already freed\n"); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1070 | goto out; |
| 1071 | } |
| 1072 | |
| 1073 | if (pwm->chip->ops->free) |
| 1074 | pwm->chip->ops->free(pwm->chip, pwm); |
| 1075 | |
Uwe Kleine-König | e926b12 | 2019-03-25 10:49:33 +0100 | [diff] [blame] | 1076 | pwm_set_chip_data(pwm, NULL); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1077 | pwm->label = NULL; |
| 1078 | |
| 1079 | module_put(pwm->chip->ops->owner); |
| 1080 | out: |
| 1081 | mutex_unlock(&pwm_lock); |
| 1082 | } |
| 1083 | EXPORT_SYMBOL_GPL(pwm_put); |
| 1084 | |
Andy Shevchenko | 9ae241d | 2021-06-07 15:24:58 +0300 | [diff] [blame] | 1085 | static void devm_pwm_release(void *pwm) |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 1086 | { |
Andy Shevchenko | 9ae241d | 2021-06-07 15:24:58 +0300 | [diff] [blame] | 1087 | pwm_put(pwm); |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | /** |
| 1091 | * devm_pwm_get() - resource managed pwm_get() |
| 1092 | * @dev: device for PWM consumer |
| 1093 | * @con_id: consumer name |
| 1094 | * |
| 1095 | * This function performs like pwm_get() but the acquired PWM device will |
| 1096 | * automatically be released on driver detach. |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 1097 | * |
| 1098 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 1099 | * error code on failure. |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 1100 | */ |
| 1101 | struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id) |
| 1102 | { |
Andy Shevchenko | 9ae241d | 2021-06-07 15:24:58 +0300 | [diff] [blame] | 1103 | struct pwm_device *pwm; |
| 1104 | int ret; |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 1105 | |
| 1106 | pwm = pwm_get(dev, con_id); |
Andy Shevchenko | 9ae241d | 2021-06-07 15:24:58 +0300 | [diff] [blame] | 1107 | if (IS_ERR(pwm)) |
| 1108 | return pwm; |
| 1109 | |
| 1110 | ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); |
| 1111 | if (ret) |
| 1112 | return ERR_PTR(ret); |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 1113 | |
| 1114 | return pwm; |
| 1115 | } |
| 1116 | EXPORT_SYMBOL_GPL(devm_pwm_get); |
| 1117 | |
Peter Ujfalusi | 261a5ed | 2012-12-21 01:43:59 -0800 | [diff] [blame] | 1118 | /** |
| 1119 | * devm_of_pwm_get() - resource managed of_pwm_get() |
| 1120 | * @dev: device for PWM consumer |
| 1121 | * @np: device node to get the PWM from |
| 1122 | * @con_id: consumer name |
| 1123 | * |
| 1124 | * This function performs like of_pwm_get() but the acquired PWM device will |
| 1125 | * automatically be released on driver detach. |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 1126 | * |
| 1127 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 1128 | * error code on failure. |
Peter Ujfalusi | 261a5ed | 2012-12-21 01:43:59 -0800 | [diff] [blame] | 1129 | */ |
| 1130 | struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, |
| 1131 | const char *con_id) |
| 1132 | { |
Andy Shevchenko | 9ae241d | 2021-06-07 15:24:58 +0300 | [diff] [blame] | 1133 | struct pwm_device *pwm; |
| 1134 | int ret; |
Peter Ujfalusi | 261a5ed | 2012-12-21 01:43:59 -0800 | [diff] [blame] | 1135 | |
Fabrice Gasnier | b2c200e | 2019-04-18 11:37:47 +0200 | [diff] [blame] | 1136 | pwm = of_pwm_get(dev, np, con_id); |
Andy Shevchenko | 9ae241d | 2021-06-07 15:24:58 +0300 | [diff] [blame] | 1137 | if (IS_ERR(pwm)) |
| 1138 | return pwm; |
| 1139 | |
| 1140 | ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); |
| 1141 | if (ret) |
| 1142 | return ERR_PTR(ret); |
Peter Ujfalusi | 261a5ed | 2012-12-21 01:43:59 -0800 | [diff] [blame] | 1143 | |
| 1144 | return pwm; |
| 1145 | } |
| 1146 | EXPORT_SYMBOL_GPL(devm_of_pwm_get); |
| 1147 | |
Nikolaus Voss | 4a6ef8e | 2019-06-12 10:36:07 +0200 | [diff] [blame] | 1148 | /** |
| 1149 | * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node |
| 1150 | * @dev: device for PWM consumer |
| 1151 | * @fwnode: firmware node to get the PWM from |
| 1152 | * @con_id: consumer name |
| 1153 | * |
| 1154 | * Returns the PWM device parsed from the firmware node. See of_pwm_get() and |
| 1155 | * acpi_pwm_get() for a detailed description. |
| 1156 | * |
| 1157 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 1158 | * error code on failure. |
| 1159 | */ |
| 1160 | struct pwm_device *devm_fwnode_pwm_get(struct device *dev, |
| 1161 | struct fwnode_handle *fwnode, |
| 1162 | const char *con_id) |
| 1163 | { |
Andy Shevchenko | 9ae241d | 2021-06-07 15:24:58 +0300 | [diff] [blame] | 1164 | struct pwm_device *pwm = ERR_PTR(-ENODEV); |
| 1165 | int ret; |
Nikolaus Voss | 4a6ef8e | 2019-06-12 10:36:07 +0200 | [diff] [blame] | 1166 | |
| 1167 | if (is_of_node(fwnode)) |
| 1168 | pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); |
| 1169 | else if (is_acpi_node(fwnode)) |
| 1170 | pwm = acpi_pwm_get(fwnode); |
Andy Shevchenko | 9ae241d | 2021-06-07 15:24:58 +0300 | [diff] [blame] | 1171 | if (IS_ERR(pwm)) |
| 1172 | return pwm; |
Nikolaus Voss | 4a6ef8e | 2019-06-12 10:36:07 +0200 | [diff] [blame] | 1173 | |
Andy Shevchenko | 9ae241d | 2021-06-07 15:24:58 +0300 | [diff] [blame] | 1174 | ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); |
| 1175 | if (ret) |
| 1176 | return ERR_PTR(ret); |
Nikolaus Voss | 4a6ef8e | 2019-06-12 10:36:07 +0200 | [diff] [blame] | 1177 | |
| 1178 | return pwm; |
| 1179 | } |
| 1180 | EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get); |
| 1181 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1182 | #ifdef CONFIG_DEBUG_FS |
| 1183 | static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s) |
| 1184 | { |
| 1185 | unsigned int i; |
| 1186 | |
| 1187 | for (i = 0; i < chip->npwm; i++) { |
| 1188 | struct pwm_device *pwm = &chip->pwms[i]; |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 1189 | struct pwm_state state; |
| 1190 | |
| 1191 | pwm_get_state(pwm, &state); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1192 | |
| 1193 | seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); |
| 1194 | |
| 1195 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) |
Jingoo Han | adcba1e | 2013-12-19 13:31:24 +0900 | [diff] [blame] | 1196 | seq_puts(s, " requested"); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1197 | |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 1198 | if (state.enabled) |
Jingoo Han | adcba1e | 2013-12-19 13:31:24 +0900 | [diff] [blame] | 1199 | seq_puts(s, " enabled"); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1200 | |
Guru Das Srinagesh | a9d887d | 2020-06-02 15:31:16 -0700 | [diff] [blame] | 1201 | seq_printf(s, " period: %llu ns", state.period); |
| 1202 | seq_printf(s, " duty: %llu ns", state.duty_cycle); |
Heiko Stübner | 23e3523 | 2016-04-14 21:17:44 +0200 | [diff] [blame] | 1203 | seq_printf(s, " polarity: %s", |
| 1204 | state.polarity ? "inverse" : "normal"); |
| 1205 | |
Clemens Gruber | 9e40ee1 | 2021-05-07 15:18:42 +0200 | [diff] [blame] | 1206 | if (state.usage_power) |
| 1207 | seq_puts(s, " usage_power"); |
| 1208 | |
Jingoo Han | adcba1e | 2013-12-19 13:31:24 +0900 | [diff] [blame] | 1209 | seq_puts(s, "\n"); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | static void *pwm_seq_start(struct seq_file *s, loff_t *pos) |
| 1214 | { |
| 1215 | mutex_lock(&pwm_lock); |
| 1216 | s->private = ""; |
| 1217 | |
| 1218 | return seq_list_start(&pwm_chips, *pos); |
| 1219 | } |
| 1220 | |
| 1221 | static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 1222 | { |
| 1223 | s->private = "\n"; |
| 1224 | |
| 1225 | return seq_list_next(v, &pwm_chips, pos); |
| 1226 | } |
| 1227 | |
| 1228 | static void pwm_seq_stop(struct seq_file *s, void *v) |
| 1229 | { |
| 1230 | mutex_unlock(&pwm_lock); |
| 1231 | } |
| 1232 | |
| 1233 | static int pwm_seq_show(struct seq_file *s, void *v) |
| 1234 | { |
| 1235 | struct pwm_chip *chip = list_entry(v, struct pwm_chip, list); |
| 1236 | |
| 1237 | seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private, |
| 1238 | chip->dev->bus ? chip->dev->bus->name : "no-bus", |
| 1239 | dev_name(chip->dev), chip->npwm, |
| 1240 | (chip->npwm != 1) ? "s" : ""); |
| 1241 | |
Uwe Kleine-König | cc2d224 | 2019-01-07 20:49:39 +0100 | [diff] [blame] | 1242 | pwm_dbg_show(chip, s); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1243 | |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
Liu Shixin | f339e79 | 2020-09-16 10:50:28 +0800 | [diff] [blame] | 1247 | static const struct seq_operations pwm_debugfs_sops = { |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1248 | .start = pwm_seq_start, |
| 1249 | .next = pwm_seq_next, |
| 1250 | .stop = pwm_seq_stop, |
| 1251 | .show = pwm_seq_show, |
| 1252 | }; |
| 1253 | |
Liu Shixin | f339e79 | 2020-09-16 10:50:28 +0800 | [diff] [blame] | 1254 | DEFINE_SEQ_ATTRIBUTE(pwm_debugfs); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1255 | |
| 1256 | static int __init pwm_debugfs_init(void) |
| 1257 | { |
Soham Biswas | 765edf0 | 2020-11-18 20:21:12 +0530 | [diff] [blame] | 1258 | debugfs_create_file("pwm", S_IFREG | 0444, NULL, NULL, |
Liu Shixin | f339e79 | 2020-09-16 10:50:28 +0800 | [diff] [blame] | 1259 | &pwm_debugfs_fops); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1260 | |
| 1261 | return 0; |
| 1262 | } |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 1263 | subsys_initcall(pwm_debugfs_init); |
| 1264 | #endif /* CONFIG_DEBUG_FS */ |