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/i2c.h>
12 #include <linux/mfd/lp3943.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/pwm.h>
16 #include <linux/slab.h>
18 #define LP3943_MAX_DUTY 255
19 #define LP3943_MIN_PERIOD 6250
20 #define LP3943_MAX_PERIOD 1600000
24 struct lp3943 *lp3943;
25 struct lp3943_platform_data *pdata;
28 static inline struct lp3943_pwm *to_lp3943_pwm(struct pwm_chip *_chip)
30 return container_of(_chip, struct lp3943_pwm, chip);
33 static struct lp3943_pwm_map *
34 lp3943_pwm_request_map(struct lp3943_pwm *lp3943_pwm, int hwpwm)
36 struct lp3943_platform_data *pdata = lp3943_pwm->pdata;
37 struct lp3943 *lp3943 = lp3943_pwm->lp3943;
38 struct lp3943_pwm_map *pwm_map;
41 pwm_map = kzalloc(sizeof(*pwm_map), GFP_KERNEL);
43 return ERR_PTR(-ENOMEM);
45 pwm_map->output = pdata->pwms[hwpwm]->output;
46 pwm_map->num_outputs = pdata->pwms[hwpwm]->num_outputs;
48 for (i = 0; i < pwm_map->num_outputs; i++) {
49 offset = pwm_map->output[i];
51 /* Return an error if the pin is already assigned */
52 if (test_and_set_bit(offset, &lp3943->pin_used)) {
54 return ERR_PTR(-EBUSY);
61 static int lp3943_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
63 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
64 struct lp3943_pwm_map *pwm_map;
66 pwm_map = lp3943_pwm_request_map(lp3943_pwm, pwm->hwpwm);
68 return PTR_ERR(pwm_map);
70 return pwm_set_chip_data(pwm, pwm_map);
73 static void lp3943_pwm_free_map(struct lp3943_pwm *lp3943_pwm,
74 struct lp3943_pwm_map *pwm_map)
76 struct lp3943 *lp3943 = lp3943_pwm->lp3943;
79 for (i = 0; i < pwm_map->num_outputs; i++) {
80 offset = pwm_map->output[i];
81 clear_bit(offset, &lp3943->pin_used);
87 static void lp3943_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
89 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
90 struct lp3943_pwm_map *pwm_map = pwm_get_chip_data(pwm);
92 lp3943_pwm_free_map(lp3943_pwm, pwm_map);
95 static int lp3943_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
96 u64 duty_ns, u64 period_ns)
98 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
99 struct lp3943 *lp3943 = lp3943_pwm->lp3943;
100 u8 val, reg_duty, reg_prescale;
104 * How to configure the LP3943 PWMs
106 * 1) Period = 6250 ~ 1600000
107 * 2) Prescale = period / 6250 -1
108 * 3) Duty = input duty
110 * Prescale and duty are register values
113 if (pwm->hwpwm == 0) {
114 reg_prescale = LP3943_REG_PRESCALE0;
115 reg_duty = LP3943_REG_PWM0;
117 reg_prescale = LP3943_REG_PRESCALE1;
118 reg_duty = LP3943_REG_PWM1;
122 * Note that after this clamping, period_ns fits into an int. This is
123 * helpful because we can resort to integer division below instead of
124 * the (more expensive) 64 bit division.
126 period_ns = clamp(period_ns, (u64)LP3943_MIN_PERIOD, (u64)LP3943_MAX_PERIOD);
127 val = (u8)((int)period_ns / LP3943_MIN_PERIOD - 1);
129 err = lp3943_write_byte(lp3943, reg_prescale, val);
133 duty_ns = min(duty_ns, period_ns);
134 val = (u8)((int)duty_ns * LP3943_MAX_DUTY / (int)period_ns);
136 return lp3943_write_byte(lp3943, reg_duty, val);
139 static int lp3943_pwm_set_mode(struct lp3943_pwm *lp3943_pwm,
140 struct lp3943_pwm_map *pwm_map,
143 struct lp3943 *lp3943 = lp3943_pwm->lp3943;
144 const struct lp3943_reg_cfg *mux = lp3943->mux_cfg;
147 for (i = 0; i < pwm_map->num_outputs; i++) {
148 index = pwm_map->output[i];
149 err = lp3943_update_bits(lp3943, mux[index].reg,
151 val << mux[index].shift);
159 static int lp3943_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
161 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
162 struct lp3943_pwm_map *pwm_map = pwm_get_chip_data(pwm);
166 val = LP3943_DIM_PWM0;
168 val = LP3943_DIM_PWM1;
171 * Each PWM generator is set to control any of outputs of LP3943.
172 * To enable/disable the PWM, these output pins should be configured.
175 return lp3943_pwm_set_mode(lp3943_pwm, pwm_map, val);
178 static void lp3943_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
180 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
181 struct lp3943_pwm_map *pwm_map = pwm_get_chip_data(pwm);
184 * LP3943 outputs are open-drain, so the pin should be configured
185 * when the PWM is disabled.
188 lp3943_pwm_set_mode(lp3943_pwm, pwm_map, LP3943_GPIO_OUT_HIGH);
191 static int lp3943_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
192 const struct pwm_state *state)
196 if (state->polarity != PWM_POLARITY_NORMAL)
199 if (!state->enabled) {
200 if (pwm->state.enabled)
201 lp3943_pwm_disable(chip, pwm);
205 err = lp3943_pwm_config(chip, pwm, state->duty_cycle, state->period);
209 if (!pwm->state.enabled)
210 err = lp3943_pwm_enable(chip, pwm);
215 static const struct pwm_ops lp3943_pwm_ops = {
216 .request = lp3943_pwm_request,
217 .free = lp3943_pwm_free,
218 .apply = lp3943_pwm_apply,
219 .owner = THIS_MODULE,
222 static int lp3943_pwm_parse_dt(struct device *dev,
223 struct lp3943_pwm *lp3943_pwm)
225 static const char * const name[] = { "ti,pwm0", "ti,pwm1", };
226 struct device_node *node = dev->of_node;
227 struct lp3943_platform_data *pdata;
228 struct lp3943_pwm_map *pwm_map;
229 enum lp3943_pwm_output *output;
230 int i, err, proplen, count = 0;
236 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
241 * Read the output map configuration from the device tree.
242 * Each of the two PWM generators can drive zero or more outputs.
245 for (i = 0; i < LP3943_NUM_PWMS; i++) {
246 if (!of_get_property(node, name[i], &proplen))
249 num_outputs = proplen / sizeof(u32);
250 if (num_outputs == 0)
253 output = devm_kcalloc(dev, num_outputs, sizeof(*output),
258 err = of_property_read_u32_array(node, name[i], output,
263 pwm_map = devm_kzalloc(dev, sizeof(*pwm_map), GFP_KERNEL);
267 pwm_map->output = output;
268 pwm_map->num_outputs = num_outputs;
269 pdata->pwms[i] = pwm_map;
277 lp3943_pwm->pdata = pdata;
281 static int lp3943_pwm_probe(struct platform_device *pdev)
283 struct lp3943 *lp3943 = dev_get_drvdata(pdev->dev.parent);
284 struct lp3943_pwm *lp3943_pwm;
287 lp3943_pwm = devm_kzalloc(&pdev->dev, sizeof(*lp3943_pwm), GFP_KERNEL);
291 lp3943_pwm->pdata = lp3943->pdata;
292 if (!lp3943_pwm->pdata) {
293 if (IS_ENABLED(CONFIG_OF))
294 ret = lp3943_pwm_parse_dt(&pdev->dev, lp3943_pwm);
302 lp3943_pwm->lp3943 = lp3943;
303 lp3943_pwm->chip.dev = &pdev->dev;
304 lp3943_pwm->chip.ops = &lp3943_pwm_ops;
305 lp3943_pwm->chip.npwm = LP3943_NUM_PWMS;
307 return devm_pwmchip_add(&pdev->dev, &lp3943_pwm->chip);
311 static const struct of_device_id lp3943_pwm_of_match[] = {
312 { .compatible = "ti,lp3943-pwm", },
315 MODULE_DEVICE_TABLE(of, lp3943_pwm_of_match);
318 static struct platform_driver lp3943_pwm_driver = {
319 .probe = lp3943_pwm_probe,
321 .name = "lp3943-pwm",
322 .of_match_table = of_match_ptr(lp3943_pwm_of_match),
325 module_platform_driver(lp3943_pwm_driver);
327 MODULE_DESCRIPTION("LP3943 PWM driver");
328 MODULE_ALIAS("platform:lp3943-pwm");
329 MODULE_AUTHOR("Milo Kim");
330 MODULE_LICENSE("GPL");