1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI/National Semiconductor LP3943 PWM driver
5 * Copyright 2013 Texas Instruments
7 * Author: Milo Kim <milo.kim@ti.com>
10 #include <linux/err.h>
11 #include <linux/mfd/lp3943.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/pwm.h>
15 #include <linux/slab.h>
17 #define LP3943_MAX_DUTY 255
18 #define LP3943_MIN_PERIOD 6250
19 #define LP3943_MAX_PERIOD 1600000
23 struct lp3943 *lp3943;
24 struct lp3943_platform_data *pdata;
27 static inline struct lp3943_pwm *to_lp3943_pwm(struct pwm_chip *_chip)
29 return container_of(_chip, struct lp3943_pwm, chip);
32 static struct lp3943_pwm_map *
33 lp3943_pwm_request_map(struct lp3943_pwm *lp3943_pwm, int hwpwm)
35 struct lp3943_platform_data *pdata = lp3943_pwm->pdata;
36 struct lp3943 *lp3943 = lp3943_pwm->lp3943;
37 struct lp3943_pwm_map *pwm_map;
40 pwm_map = kzalloc(sizeof(*pwm_map), GFP_KERNEL);
42 return ERR_PTR(-ENOMEM);
44 pwm_map->output = pdata->pwms[hwpwm]->output;
45 pwm_map->num_outputs = pdata->pwms[hwpwm]->num_outputs;
47 for (i = 0; i < pwm_map->num_outputs; i++) {
48 offset = pwm_map->output[i];
50 /* Return an error if the pin is already assigned */
51 if (test_and_set_bit(offset, &lp3943->pin_used)) {
53 return ERR_PTR(-EBUSY);
60 static int lp3943_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
62 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
63 struct lp3943_pwm_map *pwm_map;
65 pwm_map = lp3943_pwm_request_map(lp3943_pwm, pwm->hwpwm);
67 return PTR_ERR(pwm_map);
69 return pwm_set_chip_data(pwm, pwm_map);
72 static void lp3943_pwm_free_map(struct lp3943_pwm *lp3943_pwm,
73 struct lp3943_pwm_map *pwm_map)
75 struct lp3943 *lp3943 = lp3943_pwm->lp3943;
78 for (i = 0; i < pwm_map->num_outputs; i++) {
79 offset = pwm_map->output[i];
80 clear_bit(offset, &lp3943->pin_used);
86 static void lp3943_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
88 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
89 struct lp3943_pwm_map *pwm_map = pwm_get_chip_data(pwm);
91 lp3943_pwm_free_map(lp3943_pwm, pwm_map);
94 static int lp3943_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
95 u64 duty_ns, u64 period_ns)
97 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
98 struct lp3943 *lp3943 = lp3943_pwm->lp3943;
99 u8 val, reg_duty, reg_prescale;
103 * How to configure the LP3943 PWMs
105 * 1) Period = 6250 ~ 1600000
106 * 2) Prescale = period / 6250 -1
107 * 3) Duty = input duty
109 * Prescale and duty are register values
112 if (pwm->hwpwm == 0) {
113 reg_prescale = LP3943_REG_PRESCALE0;
114 reg_duty = LP3943_REG_PWM0;
116 reg_prescale = LP3943_REG_PRESCALE1;
117 reg_duty = LP3943_REG_PWM1;
121 * Note that after this clamping, period_ns fits into an int. This is
122 * helpful because we can resort to integer division below instead of
123 * the (more expensive) 64 bit division.
125 period_ns = clamp(period_ns, (u64)LP3943_MIN_PERIOD, (u64)LP3943_MAX_PERIOD);
126 val = (u8)((int)period_ns / LP3943_MIN_PERIOD - 1);
128 err = lp3943_write_byte(lp3943, reg_prescale, val);
132 duty_ns = min(duty_ns, period_ns);
133 val = (u8)((int)duty_ns * LP3943_MAX_DUTY / (int)period_ns);
135 return lp3943_write_byte(lp3943, reg_duty, val);
138 static int lp3943_pwm_set_mode(struct lp3943_pwm *lp3943_pwm,
139 struct lp3943_pwm_map *pwm_map,
142 struct lp3943 *lp3943 = lp3943_pwm->lp3943;
143 const struct lp3943_reg_cfg *mux = lp3943->mux_cfg;
146 for (i = 0; i < pwm_map->num_outputs; i++) {
147 index = pwm_map->output[i];
148 err = lp3943_update_bits(lp3943, mux[index].reg,
150 val << mux[index].shift);
158 static int lp3943_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
160 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
161 struct lp3943_pwm_map *pwm_map = pwm_get_chip_data(pwm);
165 val = LP3943_DIM_PWM0;
167 val = LP3943_DIM_PWM1;
170 * Each PWM generator is set to control any of outputs of LP3943.
171 * To enable/disable the PWM, these output pins should be configured.
174 return lp3943_pwm_set_mode(lp3943_pwm, pwm_map, val);
177 static void lp3943_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
179 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
180 struct lp3943_pwm_map *pwm_map = pwm_get_chip_data(pwm);
183 * LP3943 outputs are open-drain, so the pin should be configured
184 * when the PWM is disabled.
187 lp3943_pwm_set_mode(lp3943_pwm, pwm_map, LP3943_GPIO_OUT_HIGH);
190 static int lp3943_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
191 const struct pwm_state *state)
195 if (state->polarity != PWM_POLARITY_NORMAL)
198 if (!state->enabled) {
199 if (pwm->state.enabled)
200 lp3943_pwm_disable(chip, pwm);
204 err = lp3943_pwm_config(chip, pwm, state->duty_cycle, state->period);
208 if (!pwm->state.enabled)
209 err = lp3943_pwm_enable(chip, pwm);
214 static const struct pwm_ops lp3943_pwm_ops = {
215 .request = lp3943_pwm_request,
216 .free = lp3943_pwm_free,
217 .apply = lp3943_pwm_apply,
218 .owner = THIS_MODULE,
221 static int lp3943_pwm_parse_dt(struct device *dev,
222 struct lp3943_pwm *lp3943_pwm)
224 static const char * const name[] = { "ti,pwm0", "ti,pwm1", };
225 struct device_node *node = dev->of_node;
226 struct lp3943_platform_data *pdata;
227 struct lp3943_pwm_map *pwm_map;
228 enum lp3943_pwm_output *output;
229 int i, err, proplen, count = 0;
235 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
240 * Read the output map configuration from the device tree.
241 * Each of the two PWM generators can drive zero or more outputs.
244 for (i = 0; i < LP3943_NUM_PWMS; i++) {
245 if (!of_get_property(node, name[i], &proplen))
248 num_outputs = proplen / sizeof(u32);
249 if (num_outputs == 0)
252 output = devm_kcalloc(dev, num_outputs, sizeof(*output),
257 err = of_property_read_u32_array(node, name[i], output,
262 pwm_map = devm_kzalloc(dev, sizeof(*pwm_map), GFP_KERNEL);
266 pwm_map->output = output;
267 pwm_map->num_outputs = num_outputs;
268 pdata->pwms[i] = pwm_map;
276 lp3943_pwm->pdata = pdata;
280 static int lp3943_pwm_probe(struct platform_device *pdev)
282 struct lp3943 *lp3943 = dev_get_drvdata(pdev->dev.parent);
283 struct lp3943_pwm *lp3943_pwm;
286 lp3943_pwm = devm_kzalloc(&pdev->dev, sizeof(*lp3943_pwm), GFP_KERNEL);
290 lp3943_pwm->pdata = lp3943->pdata;
291 if (!lp3943_pwm->pdata) {
292 if (IS_ENABLED(CONFIG_OF))
293 ret = lp3943_pwm_parse_dt(&pdev->dev, lp3943_pwm);
301 lp3943_pwm->lp3943 = lp3943;
302 lp3943_pwm->chip.dev = &pdev->dev;
303 lp3943_pwm->chip.ops = &lp3943_pwm_ops;
304 lp3943_pwm->chip.npwm = LP3943_NUM_PWMS;
306 return devm_pwmchip_add(&pdev->dev, &lp3943_pwm->chip);
310 static const struct of_device_id lp3943_pwm_of_match[] = {
311 { .compatible = "ti,lp3943-pwm", },
314 MODULE_DEVICE_TABLE(of, lp3943_pwm_of_match);
317 static struct platform_driver lp3943_pwm_driver = {
318 .probe = lp3943_pwm_probe,
320 .name = "lp3943-pwm",
321 .of_match_table = of_match_ptr(lp3943_pwm_of_match),
324 module_platform_driver(lp3943_pwm_driver);
326 MODULE_DESCRIPTION("LP3943 PWM driver");
327 MODULE_ALIAS("platform:lp3943-pwm");
328 MODULE_AUTHOR("Milo Kim");
329 MODULE_LICENSE("GPL");