0001
0002 #ifndef __LINUX_PWM_H
0003 #define __LINUX_PWM_H
0004
0005 #include <linux/err.h>
0006 #include <linux/mutex.h>
0007 #include <linux/of.h>
0008
0009 struct pwm_chip;
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 enum pwm_polarity {
0021 PWM_POLARITY_NORMAL,
0022 PWM_POLARITY_INVERSED,
0023 };
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 struct pwm_args {
0039 u64 period;
0040 enum pwm_polarity polarity;
0041 };
0042
0043 enum {
0044 PWMF_REQUESTED = 1 << 0,
0045 PWMF_EXPORTED = 1 << 1,
0046 };
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 struct pwm_state {
0060 u64 period;
0061 u64 duty_cycle;
0062 enum pwm_polarity polarity;
0063 bool enabled;
0064 bool usage_power;
0065 };
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079 struct pwm_device {
0080 const char *label;
0081 unsigned long flags;
0082 unsigned int hwpwm;
0083 unsigned int pwm;
0084 struct pwm_chip *chip;
0085 void *chip_data;
0086
0087 struct pwm_args args;
0088 struct pwm_state state;
0089 struct pwm_state last;
0090 };
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102 static inline void pwm_get_state(const struct pwm_device *pwm,
0103 struct pwm_state *state)
0104 {
0105 *state = pwm->state;
0106 }
0107
0108 static inline bool pwm_is_enabled(const struct pwm_device *pwm)
0109 {
0110 struct pwm_state state;
0111
0112 pwm_get_state(pwm, &state);
0113
0114 return state.enabled;
0115 }
0116
0117 static inline void pwm_set_period(struct pwm_device *pwm, u64 period)
0118 {
0119 if (pwm)
0120 pwm->state.period = period;
0121 }
0122
0123 static inline u64 pwm_get_period(const struct pwm_device *pwm)
0124 {
0125 struct pwm_state state;
0126
0127 pwm_get_state(pwm, &state);
0128
0129 return state.period;
0130 }
0131
0132 static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
0133 {
0134 if (pwm)
0135 pwm->state.duty_cycle = duty;
0136 }
0137
0138 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
0139 {
0140 struct pwm_state state;
0141
0142 pwm_get_state(pwm, &state);
0143
0144 return state.duty_cycle;
0145 }
0146
0147 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
0148 {
0149 struct pwm_state state;
0150
0151 pwm_get_state(pwm, &state);
0152
0153 return state.polarity;
0154 }
0155
0156 static inline void pwm_get_args(const struct pwm_device *pwm,
0157 struct pwm_args *args)
0158 {
0159 *args = pwm->args;
0160 }
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179 static inline void pwm_init_state(const struct pwm_device *pwm,
0180 struct pwm_state *state)
0181 {
0182 struct pwm_args args;
0183
0184
0185 pwm_get_state(pwm, state);
0186
0187
0188 pwm_get_args(pwm, &args);
0189
0190 state->period = args.period;
0191 state->polarity = args.polarity;
0192 state->duty_cycle = 0;
0193 state->usage_power = false;
0194 }
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209 static inline unsigned int
0210 pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
0211 {
0212 if (!state->period)
0213 return 0;
0214
0215 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
0216 state->period);
0217 }
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237 static inline int
0238 pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
0239 unsigned int scale)
0240 {
0241 if (!scale || duty_cycle > scale)
0242 return -EINVAL;
0243
0244 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
0245 state->period,
0246 scale);
0247
0248 return 0;
0249 }
0250
0251
0252
0253
0254
0255
0256 struct pwm_capture {
0257 unsigned int period;
0258 unsigned int duty_cycle;
0259 };
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272 struct pwm_ops {
0273 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
0274 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
0275 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
0276 struct pwm_capture *result, unsigned long timeout);
0277 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
0278 const struct pwm_state *state);
0279 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
0280 struct pwm_state *state);
0281 struct module *owner;
0282 };
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295 struct pwm_chip {
0296 struct device *dev;
0297 const struct pwm_ops *ops;
0298 int base;
0299 unsigned int npwm;
0300
0301 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
0302 const struct of_phandle_args *args);
0303 unsigned int of_pwm_n_cells;
0304
0305
0306 struct list_head list;
0307 struct pwm_device *pwms;
0308 };
0309
0310 #if IS_ENABLED(CONFIG_PWM)
0311
0312 struct pwm_device *pwm_request(int pwm_id, const char *label);
0313 void pwm_free(struct pwm_device *pwm);
0314 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state);
0315 int pwm_adjust_config(struct pwm_device *pwm);
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
0326 int period_ns)
0327 {
0328 struct pwm_state state;
0329
0330 if (!pwm)
0331 return -EINVAL;
0332
0333 if (duty_ns < 0 || period_ns < 0)
0334 return -EINVAL;
0335
0336 pwm_get_state(pwm, &state);
0337 if (state.duty_cycle == duty_ns && state.period == period_ns)
0338 return 0;
0339
0340 state.duty_cycle = duty_ns;
0341 state.period = period_ns;
0342 return pwm_apply_state(pwm, &state);
0343 }
0344
0345
0346
0347
0348
0349
0350
0351 static inline int pwm_enable(struct pwm_device *pwm)
0352 {
0353 struct pwm_state state;
0354
0355 if (!pwm)
0356 return -EINVAL;
0357
0358 pwm_get_state(pwm, &state);
0359 if (state.enabled)
0360 return 0;
0361
0362 state.enabled = true;
0363 return pwm_apply_state(pwm, &state);
0364 }
0365
0366
0367
0368
0369
0370 static inline void pwm_disable(struct pwm_device *pwm)
0371 {
0372 struct pwm_state state;
0373
0374 if (!pwm)
0375 return;
0376
0377 pwm_get_state(pwm, &state);
0378 if (!state.enabled)
0379 return;
0380
0381 state.enabled = false;
0382 pwm_apply_state(pwm, &state);
0383 }
0384
0385
0386 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
0387 unsigned long timeout);
0388 int pwm_set_chip_data(struct pwm_device *pwm, void *data);
0389 void *pwm_get_chip_data(struct pwm_device *pwm);
0390
0391 int pwmchip_add(struct pwm_chip *chip);
0392 void pwmchip_remove(struct pwm_chip *chip);
0393
0394 int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip);
0395
0396 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
0397 unsigned int index,
0398 const char *label);
0399
0400 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
0401 const struct of_phandle_args *args);
0402 struct pwm_device *of_pwm_single_xlate(struct pwm_chip *pc,
0403 const struct of_phandle_args *args);
0404
0405 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
0406 struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
0407 const char *con_id);
0408 void pwm_put(struct pwm_device *pwm);
0409
0410 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
0411 struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
0412 const char *con_id);
0413 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
0414 struct fwnode_handle *fwnode,
0415 const char *con_id);
0416 #else
0417 static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
0418 {
0419 might_sleep();
0420 return ERR_PTR(-ENODEV);
0421 }
0422
0423 static inline void pwm_free(struct pwm_device *pwm)
0424 {
0425 might_sleep();
0426 }
0427
0428 static inline int pwm_apply_state(struct pwm_device *pwm,
0429 const struct pwm_state *state)
0430 {
0431 might_sleep();
0432 return -ENOTSUPP;
0433 }
0434
0435 static inline int pwm_adjust_config(struct pwm_device *pwm)
0436 {
0437 return -ENOTSUPP;
0438 }
0439
0440 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
0441 int period_ns)
0442 {
0443 might_sleep();
0444 return -EINVAL;
0445 }
0446
0447 static inline int pwm_capture(struct pwm_device *pwm,
0448 struct pwm_capture *result,
0449 unsigned long timeout)
0450 {
0451 return -EINVAL;
0452 }
0453
0454 static inline int pwm_enable(struct pwm_device *pwm)
0455 {
0456 might_sleep();
0457 return -EINVAL;
0458 }
0459
0460 static inline void pwm_disable(struct pwm_device *pwm)
0461 {
0462 might_sleep();
0463 }
0464
0465 static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
0466 {
0467 return -EINVAL;
0468 }
0469
0470 static inline void *pwm_get_chip_data(struct pwm_device *pwm)
0471 {
0472 return NULL;
0473 }
0474
0475 static inline int pwmchip_add(struct pwm_chip *chip)
0476 {
0477 return -EINVAL;
0478 }
0479
0480 static inline int pwmchip_remove(struct pwm_chip *chip)
0481 {
0482 return -EINVAL;
0483 }
0484
0485 static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
0486 unsigned int index,
0487 const char *label)
0488 {
0489 might_sleep();
0490 return ERR_PTR(-ENODEV);
0491 }
0492
0493 static inline struct pwm_device *pwm_get(struct device *dev,
0494 const char *consumer)
0495 {
0496 might_sleep();
0497 return ERR_PTR(-ENODEV);
0498 }
0499
0500 static inline struct pwm_device *of_pwm_get(struct device *dev,
0501 struct device_node *np,
0502 const char *con_id)
0503 {
0504 might_sleep();
0505 return ERR_PTR(-ENODEV);
0506 }
0507
0508 static inline void pwm_put(struct pwm_device *pwm)
0509 {
0510 might_sleep();
0511 }
0512
0513 static inline struct pwm_device *devm_pwm_get(struct device *dev,
0514 const char *consumer)
0515 {
0516 might_sleep();
0517 return ERR_PTR(-ENODEV);
0518 }
0519
0520 static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
0521 struct device_node *np,
0522 const char *con_id)
0523 {
0524 might_sleep();
0525 return ERR_PTR(-ENODEV);
0526 }
0527
0528 static inline struct pwm_device *
0529 devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
0530 const char *con_id)
0531 {
0532 might_sleep();
0533 return ERR_PTR(-ENODEV);
0534 }
0535 #endif
0536
0537 static inline void pwm_apply_args(struct pwm_device *pwm)
0538 {
0539 struct pwm_state state = { };
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562 state.enabled = false;
0563 state.polarity = pwm->args.polarity;
0564 state.period = pwm->args.period;
0565 state.usage_power = false;
0566
0567 pwm_apply_state(pwm, &state);
0568 }
0569
0570 struct pwm_lookup {
0571 struct list_head list;
0572 const char *provider;
0573 unsigned int index;
0574 const char *dev_id;
0575 const char *con_id;
0576 unsigned int period;
0577 enum pwm_polarity polarity;
0578 const char *module;
0579 };
0580
0581 #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
0582 _period, _polarity, _module) \
0583 { \
0584 .provider = _provider, \
0585 .index = _index, \
0586 .dev_id = _dev_id, \
0587 .con_id = _con_id, \
0588 .period = _period, \
0589 .polarity = _polarity, \
0590 .module = _module, \
0591 }
0592
0593 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
0594 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
0595 _polarity, NULL)
0596
0597 #if IS_ENABLED(CONFIG_PWM)
0598 void pwm_add_table(struct pwm_lookup *table, size_t num);
0599 void pwm_remove_table(struct pwm_lookup *table, size_t num);
0600 #else
0601 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
0602 {
0603 }
0604
0605 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
0606 {
0607 }
0608 #endif
0609
0610 #ifdef CONFIG_PWM_SYSFS
0611 void pwmchip_sysfs_export(struct pwm_chip *chip);
0612 void pwmchip_sysfs_unexport(struct pwm_chip *chip);
0613 #else
0614 static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
0615 {
0616 }
0617
0618 static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
0619 {
0620 }
0621 #endif
0622
0623 #endif