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 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/pwm.h> |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 11 | #include <linux/radix-tree.h> |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 12 | #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 Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 17 | #include <linux/debugfs.h> |
| 18 | #include <linux/seq_file.h> |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 19 | |
Laurent Pinchart | 208be76 | 2013-07-18 00:54:22 +0200 | [diff] [blame] | 20 | #include <dt-bindings/pwm/pwm.h> |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 21 | |
Laurent Pinchart | 208be76 | 2013-07-18 00:54:22 +0200 | [diff] [blame] | 22 | #define MAX_PWMS 1024 |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 23 | |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 24 | static DEFINE_MUTEX(pwm_lookup_lock); |
| 25 | static LIST_HEAD(pwm_lookup_list); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 26 | static DEFINE_MUTEX(pwm_lock); |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 27 | static LIST_HEAD(pwm_chips); |
| 28 | static DECLARE_BITMAP(allocated_pwms, MAX_PWMS); |
| 29 | static RADIX_TREE(pwm_tree, GFP_KERNEL); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 30 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 31 | static struct pwm_device *pwm_to_device(unsigned int pwm) |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 32 | { |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 33 | return radix_tree_lookup(&pwm_tree, pwm); |
| 34 | } |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 35 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 36 | static 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 | |
| 59 | static 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 Reding | 83a9886 | 2016-05-02 12:05:55 +0200 | [diff] [blame] | 65 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 66 | radix_tree_delete(&pwm_tree, pwm->pwm); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 69 | bitmap_clear(allocated_pwms, chip->base, chip->npwm); |
| 70 | |
| 71 | kfree(chip->pwms); |
| 72 | chip->pwms = NULL; |
| 73 | } |
| 74 | |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 75 | static 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 Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 98 | static 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, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 122 | struct pwm_device * |
| 123 | of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args) |
| 124 | { |
| 125 | struct pwm_device *pwm; |
| 126 | |
Lothar Wassmann | 42883cb | 2017-01-29 22:54:13 +0100 | [diff] [blame] | 127 | /* check, whether the driver supports a third cell for flags */ |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 128 | if (pc->of_pwm_n_cells < 3) |
| 129 | return ERR_PTR(-EINVAL); |
| 130 | |
Lothar Wassmann | 42883cb | 2017-01-29 22:54:13 +0100 | [diff] [blame] | 131 | /* flags in the third cell are optional */ |
| 132 | if (args->args_count < 2) |
| 133 | return ERR_PTR(-EINVAL); |
| 134 | |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 135 | 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 Brezillon | e39c0df | 2016-04-14 21:17:21 +0200 | [diff] [blame] | 142 | pwm->args.period = args->args[1]; |
Lothar Wassmann | 42883cb | 2017-01-29 22:54:13 +0100 | [diff] [blame] | 143 | pwm->args.polarity = PWM_POLARITY_NORMAL; |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 144 | |
Lothar Wassmann | 42883cb | 2017-01-29 22:54:13 +0100 | [diff] [blame] | 145 | if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) |
Boris Brezillon | e39c0df | 2016-04-14 21:17:21 +0200 | [diff] [blame] | 146 | pwm->args.polarity = PWM_POLARITY_INVERSED; |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 147 | |
| 148 | return pwm; |
| 149 | } |
Thierry Reding | 417328c | 2012-11-28 15:12:07 +0100 | [diff] [blame] | 150 | EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags); |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 151 | |
Sachin Kamat | e50d352 | 2012-08-10 16:41:13 +0530 | [diff] [blame] | 152 | static struct pwm_device * |
| 153 | of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args) |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 154 | { |
| 155 | struct pwm_device *pwm; |
| 156 | |
Lothar Wassmann | 42883cb | 2017-01-29 22:54:13 +0100 | [diff] [blame] | 157 | /* sanity check driver support */ |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 158 | if (pc->of_pwm_n_cells < 2) |
| 159 | return ERR_PTR(-EINVAL); |
| 160 | |
Lothar Wassmann | 42883cb | 2017-01-29 22:54:13 +0100 | [diff] [blame] | 161 | /* all cells are required */ |
| 162 | if (args->args_count != pc->of_pwm_n_cells) |
| 163 | return ERR_PTR(-EINVAL); |
| 164 | |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 165 | 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 Brezillon | e39c0df | 2016-04-14 21:17:21 +0200 | [diff] [blame] | 172 | pwm->args.period = args->args[1]; |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 173 | |
| 174 | return pwm; |
| 175 | } |
| 176 | |
Sachin Kamat | dfeb86e | 2012-08-02 12:32:42 +0530 | [diff] [blame] | 177 | static void of_pwmchip_add(struct pwm_chip *chip) |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 178 | { |
| 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 Kamat | dfeb86e | 2012-08-02 12:32:42 +0530 | [diff] [blame] | 190 | static void of_pwmchip_remove(struct pwm_chip *chip) |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 191 | { |
Markus Elfring | 8d6cc07 | 2015-02-03 11:54:28 +0100 | [diff] [blame] | 192 | if (chip->dev) |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 193 | of_node_put(chip->dev->of_node); |
| 194 | } |
| 195 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 196 | /** |
| 197 | * pwm_set_chip_data() - set private chip data for a PWM |
| 198 | * @pwm: PWM device |
| 199 | * @data: pointer to chip-specific data |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 200 | * |
| 201 | * Returns: 0 on success or a negative error code on failure. |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 202 | */ |
| 203 | int 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 Reding | 928c447 | 2013-01-30 09:22:24 +0100 | [diff] [blame] | 212 | EXPORT_SYMBOL_GPL(pwm_set_chip_data); |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 213 | |
| 214 | /** |
| 215 | * pwm_get_chip_data() - get private chip data for a PWM |
| 216 | * @pwm: PWM device |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 217 | * |
| 218 | * Returns: A pointer to the chip-private data for the PWM device. |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 219 | */ |
| 220 | void *pwm_get_chip_data(struct pwm_device *pwm) |
| 221 | { |
| 222 | return pwm ? pwm->chip_data : NULL; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 223 | } |
Thierry Reding | 928c447 | 2013-01-30 09:22:24 +0100 | [diff] [blame] | 224 | EXPORT_SYMBOL_GPL(pwm_get_chip_data); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 225 | |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 226 | static 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 Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 239 | /** |
Tim Kryger | b6a00fa | 2015-05-26 13:08:16 -0700 | [diff] [blame] | 240 | * pwmchip_add_with_polarity() - register a new PWM chip |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 241 | * @chip: the PWM chip to add |
Tim Kryger | b6a00fa | 2015-05-26 13:08:16 -0700 | [diff] [blame] | 242 | * @polarity: initial polarity of PWM channels |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 243 | * |
| 244 | * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base |
Tim Kryger | b6a00fa | 2015-05-26 13:08:16 -0700 | [diff] [blame] | 245 | * will be used. The initial polarity for all channels is specified by the |
| 246 | * @polarity parameter. |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 247 | * |
| 248 | * Returns: 0 on success or a negative error code on failure. |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 249 | */ |
Tim Kryger | b6a00fa | 2015-05-26 13:08:16 -0700 | [diff] [blame] | 250 | int pwmchip_add_with_polarity(struct pwm_chip *chip, |
| 251 | enum pwm_polarity polarity) |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 252 | { |
| 253 | struct pwm_device *pwm; |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 254 | unsigned int i; |
| 255 | int ret; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 256 | |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 257 | if (!chip || !chip->dev || !chip->ops || !chip->npwm) |
| 258 | return -EINVAL; |
| 259 | |
| 260 | if (!pwm_ops_check(chip->ops)) |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 261 | return -EINVAL; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 262 | |
| 263 | mutex_lock(&pwm_lock); |
| 264 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 265 | ret = alloc_pwms(chip->base, chip->npwm); |
| 266 | if (ret < 0) |
| 267 | goto out; |
| 268 | |
Thierry Reding | 2907f8a | 2016-05-02 12:07:34 +0200 | [diff] [blame] | 269 | chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL); |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 270 | if (!chip->pwms) { |
| 271 | ret = -ENOMEM; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 272 | goto out; |
| 273 | } |
| 274 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 275 | 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 Brezillon | 43a276b | 2016-04-14 21:17:38 +0200 | [diff] [blame] | 283 | pwm->state.polarity = polarity; |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 284 | |
Boris Brezillon | 15fa8a43 | 2016-04-14 21:17:40 +0200 | [diff] [blame] | 285 | if (chip->ops->get_state) |
| 286 | chip->ops->get_state(chip, pwm, &pwm->state); |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 287 | |
| 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 Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 298 | if (IS_ENABLED(CONFIG_OF)) |
| 299 | of_pwmchip_add(chip); |
| 300 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 301 | out: |
| 302 | mutex_unlock(&pwm_lock); |
Phong Hoang | 347ab94 | 2019-03-19 19:40:08 +0900 | [diff] [blame] | 303 | |
| 304 | if (!ret) |
| 305 | pwmchip_sysfs_export(chip); |
| 306 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 307 | return ret; |
| 308 | } |
Tim Kryger | b6a00fa | 2015-05-26 13:08:16 -0700 | [diff] [blame] | 309 | EXPORT_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 Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 317 | * |
| 318 | * Returns: 0 on success or a negative error code on failure. |
Tim Kryger | b6a00fa | 2015-05-26 13:08:16 -0700 | [diff] [blame] | 319 | */ |
| 320 | int pwmchip_add(struct pwm_chip *chip) |
| 321 | { |
| 322 | return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL); |
| 323 | } |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 324 | EXPORT_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 Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 332 | * |
| 333 | * Returns: 0 on success or a negative error code on failure. |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 334 | */ |
| 335 | int pwmchip_remove(struct pwm_chip *chip) |
| 336 | { |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 337 | unsigned int i; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 338 | int ret = 0; |
| 339 | |
Phong Hoang | 347ab94 | 2019-03-19 19:40:08 +0900 | [diff] [blame] | 340 | pwmchip_sysfs_unexport(chip); |
David Hsu | 0733424 | 2016-08-09 14:57:46 -0700 | [diff] [blame] | 341 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 342 | mutex_lock(&pwm_lock); |
| 343 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 344 | 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 Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 353 | list_del_init(&chip->list); |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 354 | |
| 355 | if (IS_ENABLED(CONFIG_OF)) |
| 356 | of_pwmchip_remove(chip); |
| 357 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 358 | free_pwms(chip); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 359 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 360 | out: |
| 361 | mutex_unlock(&pwm_lock); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 362 | return ret; |
| 363 | } |
| 364 | EXPORT_SYMBOL_GPL(pwmchip_remove); |
| 365 | |
| 366 | /** |
| 367 | * pwm_request() - request a PWM device |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 368 | * @pwm: global PWM device index |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 369 | * @label: PWM device label |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 370 | * |
| 371 | * This function is deprecated, use pwm_get() instead. |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 372 | * |
| 373 | * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on |
| 374 | * failure. |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 375 | */ |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 376 | struct pwm_device *pwm_request(int pwm, const char *label) |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 377 | { |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 378 | struct pwm_device *dev; |
| 379 | int err; |
| 380 | |
| 381 | if (pwm < 0 || pwm >= MAX_PWMS) |
| 382 | return ERR_PTR(-EINVAL); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 383 | |
| 384 | mutex_lock(&pwm_lock); |
| 385 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 386 | dev = pwm_to_device(pwm); |
| 387 | if (!dev) { |
| 388 | dev = ERR_PTR(-EPROBE_DEFER); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 389 | goto out; |
| 390 | } |
| 391 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 392 | err = pwm_device_request(dev, label); |
| 393 | if (err < 0) |
| 394 | dev = ERR_PTR(err); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 395 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 396 | out: |
| 397 | mutex_unlock(&pwm_lock); |
| 398 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 399 | return dev; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 400 | } |
| 401 | EXPORT_SYMBOL_GPL(pwm_request); |
| 402 | |
| 403 | /** |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 404 | * 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 Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 409 | * 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 Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 412 | */ |
| 413 | struct 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 | } |
| 433 | EXPORT_SYMBOL_GPL(pwm_request_from_chip); |
| 434 | |
| 435 | /** |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 436 | * pwm_free() - free a PWM device |
| 437 | * @pwm: PWM device |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 438 | * |
| 439 | * This function is deprecated, use pwm_put() instead. |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 440 | */ |
| 441 | void pwm_free(struct pwm_device *pwm) |
| 442 | { |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 443 | pwm_put(pwm); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 444 | } |
| 445 | EXPORT_SYMBOL_GPL(pwm_free); |
| 446 | |
| 447 | /** |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 448 | * pwm_apply_state() - atomically apply a new state to a PWM device |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 449 | * @pwm: PWM device |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 450 | * @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 Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 453 | */ |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 454 | int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state) |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 455 | { |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 456 | int err; |
| 457 | |
Brian Norris | ef2bf49 | 2016-05-27 09:45:49 -0700 | [diff] [blame] | 458 | if (!pwm || !state || !state->period || |
| 459 | state->duty_cycle > state->period) |
Jonathan Richardson | d1cd214 | 2015-10-16 17:40:58 -0700 | [diff] [blame] | 460 | return -EINVAL; |
| 461 | |
Uwe Kleine-König | 309b32f | 2019-01-07 20:49:37 +0100 | [diff] [blame] | 462 | 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 Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 466 | return 0; |
Jonathan Richardson | d1cd214 | 2015-10-16 17:40:58 -0700 | [diff] [blame] | 467 | |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 468 | if (pwm->chip->ops->apply) { |
| 469 | err = pwm->chip->ops->apply(pwm->chip, pwm, state); |
Jonathan Richardson | d1cd214 | 2015-10-16 17:40:58 -0700 | [diff] [blame] | 470 | if (err) |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 471 | 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 Richardson | d1cd214 | 2015-10-16 17:40:58 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 525 | return 0; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 526 | } |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 527 | EXPORT_SYMBOL_GPL(pwm_apply_state); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 528 | |
| 529 | /** |
Lee Jones | 3a3d1a4 | 2016-06-08 10:21:23 +0100 | [diff] [blame] | 530 | * 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 | */ |
| 537 | int 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 | } |
| 554 | EXPORT_SYMBOL_GPL(pwm_capture); |
| 555 | |
| 556 | /** |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 557 | * pwm_adjust_config() - adjust the current PWM config to the PWM arguments |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 558 | * @pwm: PWM device |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 559 | * |
| 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 Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 563 | */ |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 564 | int pwm_adjust_config(struct pwm_device *pwm) |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 565 | { |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 566 | struct pwm_state state; |
| 567 | struct pwm_args pargs; |
Boris Brezillon | 09a7e4a | 2016-04-14 21:17:39 +0200 | [diff] [blame] | 568 | |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 569 | 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 Brezillon | 09a7e4a | 2016-04-14 21:17:39 +0200 | [diff] [blame] | 586 | } |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 587 | |
| 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 Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 609 | } |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 610 | EXPORT_SYMBOL_GPL(pwm_adjust_config); |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 611 | |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 612 | static 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 Ujfalusi | 8eb9612 | 2012-12-21 01:43:58 -0800 | [diff] [blame] | 630 | * of_pwm_get() - request a PWM via the PWM framework |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 631 | * @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 Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 644 | * |
| 645 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 646 | * error code on failure. |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 647 | */ |
Peter Ujfalusi | 8eb9612 | 2012-12-21 01:43:58 -0800 | [diff] [blame] | 648 | struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id) |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 649 | { |
| 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 Wassmann | f2dafc0 | 2017-01-29 22:54:05 +0100 | [diff] [blame] | 665 | pr_err("%s(): can't parse \"pwms\" property\n", __func__); |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 666 | return ERR_PTR(err); |
| 667 | } |
| 668 | |
| 669 | pc = of_node_to_pwmchip(args.np); |
| 670 | if (IS_ERR(pc)) { |
Jerome Brunet | 93c292e | 2017-05-23 18:05:03 +0200 | [diff] [blame] | 671 | if (PTR_ERR(pc) != -EPROBE_DEFER) |
| 672 | pr_err("%s(): PWM chip not found\n", __func__); |
| 673 | |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 674 | pwm = ERR_CAST(pc); |
| 675 | goto put; |
| 676 | } |
| 677 | |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 678 | 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 | |
| 696 | put: |
| 697 | of_node_put(args.np); |
| 698 | |
| 699 | return pwm; |
| 700 | } |
Peter Ujfalusi | 8eb9612 | 2012-12-21 01:43:58 -0800 | [diff] [blame] | 701 | EXPORT_SYMBOL_GPL(of_pwm_get); |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 702 | |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 703 | /** |
| 704 | * pwm_add_table() - register PWM device consumers |
| 705 | * @table: array of consumers to register |
| 706 | * @num: number of consumers in table |
| 707 | */ |
Shobhit Kumar | c264f11 | 2015-03-12 22:01:31 +0530 | [diff] [blame] | 708 | void pwm_add_table(struct pwm_lookup *table, size_t num) |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 709 | { |
| 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 Kumar | efb0de5 | 2015-05-05 15:04:18 +0530 | [diff] [blame] | 721 | * pwm_remove_table() - unregister PWM device consumers |
| 722 | * @table: array of consumers to unregister |
| 723 | * @num: number of consumers in table |
| 724 | */ |
| 725 | void 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 Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 738 | * pwm_get() - look up and request a PWM device |
| 739 | * @dev: device for PWM consumer |
| 740 | * @con_id: consumer name |
| 741 | * |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 742 | * 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 Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 745 | * |
| 746 | * Once a PWM chip has been found the specified PWM device will be requested |
| 747 | * and is ready to be used. |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 748 | * |
| 749 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 750 | * error code on failure. |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 751 | */ |
| 752 | struct pwm_device *pwm_get(struct device *dev, const char *con_id) |
| 753 | { |
Sachin Kamat | e50d352 | 2012-08-10 16:41:13 +0530 | [diff] [blame] | 754 | const char *dev_id = dev ? dev_name(dev) : NULL; |
Hans de Goede | 69efb34 | 2017-01-22 17:14:07 +0100 | [diff] [blame] | 755 | struct pwm_device *pwm; |
| 756 | struct pwm_chip *chip; |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 757 | unsigned int best = 0; |
Geert Uytterhoeven | 70145f8 | 2014-08-28 11:03:14 +0200 | [diff] [blame] | 758 | struct pwm_lookup *p, *chosen = NULL; |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 759 | unsigned int match; |
Hans de Goede | b526a31 | 2017-01-22 17:14:08 +0100 | [diff] [blame] | 760 | int err; |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 761 | |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 762 | /* look up via DT first */ |
| 763 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) |
Peter Ujfalusi | 8eb9612 | 2012-12-21 01:43:58 -0800 | [diff] [blame] | 764 | return of_pwm_get(dev->of_node, con_id); |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 765 | |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 766 | /* |
| 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 Uytterhoeven | 70145f8 | 2014-08-28 11:03:14 +0200 | [diff] [blame] | 806 | chosen = p; |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 807 | |
| 808 | if (match != 3) |
| 809 | best = match; |
| 810 | else |
| 811 | break; |
| 812 | } |
| 813 | } |
| 814 | |
Hans de Goede | 69efb34 | 2017-01-22 17:14:07 +0100 | [diff] [blame] | 815 | mutex_unlock(&pwm_lookup_lock); |
| 816 | |
| 817 | if (!chosen) |
| 818 | return ERR_PTR(-ENODEV); |
Alexandre Belloni | 3796ce1 | 2014-05-19 22:42:32 +0200 | [diff] [blame] | 819 | |
Geert Uytterhoeven | 70145f8 | 2014-08-28 11:03:14 +0200 | [diff] [blame] | 820 | chip = pwmchip_find_by_name(chosen->provider); |
Hans de Goede | b526a31 | 2017-01-22 17:14:08 +0100 | [diff] [blame] | 821 | |
| 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 Uytterhoeven | 70145f8 | 2014-08-28 11:03:14 +0200 | [diff] [blame] | 834 | if (!chip) |
Hans de Goede | 69efb34 | 2017-01-22 17:14:07 +0100 | [diff] [blame] | 835 | return ERR_PTR(-EPROBE_DEFER); |
Geert Uytterhoeven | 70145f8 | 2014-08-28 11:03:14 +0200 | [diff] [blame] | 836 | |
| 837 | pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); |
Alexandre Belloni | 3796ce1 | 2014-05-19 22:42:32 +0200 | [diff] [blame] | 838 | if (IS_ERR(pwm)) |
Hans de Goede | 69efb34 | 2017-01-22 17:14:07 +0100 | [diff] [blame] | 839 | return pwm; |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 840 | |
Boris Brezillon | fbd45a1 | 2016-05-17 14:27:25 +0200 | [diff] [blame] | 841 | pwm->args.period = chosen->period; |
| 842 | pwm->args.polarity = chosen->polarity; |
| 843 | |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 844 | return pwm; |
| 845 | } |
| 846 | EXPORT_SYMBOL_GPL(pwm_get); |
| 847 | |
| 848 | /** |
| 849 | * pwm_put() - release a PWM device |
| 850 | * @pwm: PWM device |
| 851 | */ |
| 852 | void 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 Kamat | e50d352 | 2012-08-10 16:41:13 +0530 | [diff] [blame] | 860 | pr_warn("PWM device already freed\n"); |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 861 | goto out; |
| 862 | } |
| 863 | |
| 864 | if (pwm->chip->ops->free) |
| 865 | pwm->chip->ops->free(pwm->chip, pwm); |
| 866 | |
Uwe Kleine-König | e926b12 | 2019-03-25 10:49:33 +0100 | [diff] [blame] | 867 | pwm_set_chip_data(pwm, NULL); |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 868 | pwm->label = NULL; |
| 869 | |
| 870 | module_put(pwm->chip->ops->owner); |
| 871 | out: |
| 872 | mutex_unlock(&pwm_lock); |
| 873 | } |
| 874 | EXPORT_SYMBOL_GPL(pwm_put); |
| 875 | |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 876 | static 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 Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 888 | * |
| 889 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 890 | * error code on failure. |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 891 | */ |
| 892 | struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id) |
| 893 | { |
| 894 | struct pwm_device **ptr, *pwm; |
| 895 | |
Wolfram Sang | 77f0b9d | 2013-06-03 22:27:17 +0200 | [diff] [blame] | 896 | ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL); |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 897 | 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 | } |
| 910 | EXPORT_SYMBOL_GPL(devm_pwm_get); |
| 911 | |
Peter Ujfalusi | 261a5ed | 2012-12-21 01:43:59 -0800 | [diff] [blame] | 912 | /** |
| 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 Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 920 | * |
| 921 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 922 | * error code on failure. |
Peter Ujfalusi | 261a5ed | 2012-12-21 01:43:59 -0800 | [diff] [blame] | 923 | */ |
| 924 | struct 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 Sang | 77f0b9d | 2013-06-03 22:27:17 +0200 | [diff] [blame] | 929 | ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL); |
Peter Ujfalusi | 261a5ed | 2012-12-21 01:43:59 -0800 | [diff] [blame] | 930 | 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 | } |
| 943 | EXPORT_SYMBOL_GPL(devm_of_pwm_get); |
| 944 | |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 945 | static 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 | */ |
| 964 | void 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 | } |
| 968 | EXPORT_SYMBOL_GPL(devm_pwm_put); |
| 969 | |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 970 | #ifdef CONFIG_DEBUG_FS |
| 971 | static 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 Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 977 | struct pwm_state state; |
| 978 | |
| 979 | pwm_get_state(pwm, &state); |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 980 | |
| 981 | seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); |
| 982 | |
| 983 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) |
Jingoo Han | adcba1e | 2013-12-19 13:31:24 +0900 | [diff] [blame] | 984 | seq_puts(s, " requested"); |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 985 | |
Boris Brezillon | 39100ce | 2016-04-14 21:17:43 +0200 | [diff] [blame] | 986 | if (state.enabled) |
Jingoo Han | adcba1e | 2013-12-19 13:31:24 +0900 | [diff] [blame] | 987 | seq_puts(s, " enabled"); |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 988 | |
Heiko Stübner | 23e3523 | 2016-04-14 21:17:44 +0200 | [diff] [blame] | 989 | 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 Han | adcba1e | 2013-12-19 13:31:24 +0900 | [diff] [blame] | 994 | seq_puts(s, "\n"); |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | |
| 998 | static 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 | |
| 1006 | static 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 | |
| 1013 | static void pwm_seq_stop(struct seq_file *s, void *v) |
| 1014 | { |
| 1015 | mutex_unlock(&pwm_lock); |
| 1016 | } |
| 1017 | |
| 1018 | static 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önig | cc2d224 | 2019-01-07 20:49:39 +0100 | [diff] [blame] | 1027 | pwm_dbg_show(chip, s); |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 1028 | |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | static 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 | |
| 1039 | static int pwm_seq_open(struct inode *inode, struct file *file) |
| 1040 | { |
| 1041 | return seq_open(file, &pwm_seq_ops); |
| 1042 | } |
| 1043 | |
| 1044 | static 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 | |
| 1052 | static 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 Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 1059 | subsys_initcall(pwm_debugfs_init); |
| 1060 | #endif /* CONFIG_DEBUG_FS */ |