Linux 6.11-rc7
[linux.git] / drivers / pwm / pwm-lp3943.c
blob61189cea10467089801349c001d0286efca621dc
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * TI/National Semiconductor LP3943 PWM driver
5 * Copyright 2013 Texas Instruments
7 * Author: Milo Kim <milo.kim@ti.com>
8 */
10 #include <linux/err.h>
11 #include <linux/mfd/lp3943.h>
12 #include <linux/module.h>
13 #include <linux/of.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
22 struct lp3943_pwm {
23 struct lp3943 *lp3943;
24 struct lp3943_platform_data *pdata;
25 struct lp3943_pwm_map pwm_map[LP3943_NUM_PWMS];
28 static inline struct lp3943_pwm *to_lp3943_pwm(struct pwm_chip *chip)
30 return pwmchip_get_drvdata(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 = &lp3943_pwm->pwm_map[hwpwm];
39 int i, offset;
41 pwm_map->output = pdata->pwms[hwpwm]->output;
42 pwm_map->num_outputs = pdata->pwms[hwpwm]->num_outputs;
44 for (i = 0; i < pwm_map->num_outputs; i++) {
45 offset = pwm_map->output[i];
47 /* Return an error if the pin is already assigned */
48 if (test_and_set_bit(offset, &lp3943->pin_used))
49 return ERR_PTR(-EBUSY);
52 return pwm_map;
55 static int lp3943_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
57 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
58 struct lp3943_pwm_map *pwm_map;
60 pwm_map = lp3943_pwm_request_map(lp3943_pwm, pwm->hwpwm);
61 if (IS_ERR(pwm_map))
62 return PTR_ERR(pwm_map);
64 return 0;
67 static void lp3943_pwm_free_map(struct lp3943_pwm *lp3943_pwm,
68 struct lp3943_pwm_map *pwm_map)
70 struct lp3943 *lp3943 = lp3943_pwm->lp3943;
71 int i, offset;
73 for (i = 0; i < pwm_map->num_outputs; i++) {
74 offset = pwm_map->output[i];
75 clear_bit(offset, &lp3943->pin_used);
79 static void lp3943_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
81 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
82 struct lp3943_pwm_map *pwm_map = &lp3943_pwm->pwm_map[pwm->hwpwm];
84 lp3943_pwm_free_map(lp3943_pwm, pwm_map);
87 static int lp3943_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
88 u64 duty_ns, u64 period_ns)
90 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
91 struct lp3943 *lp3943 = lp3943_pwm->lp3943;
92 u8 val, reg_duty, reg_prescale;
93 int err;
96 * How to configure the LP3943 PWMs
98 * 1) Period = 6250 ~ 1600000
99 * 2) Prescale = period / 6250 -1
100 * 3) Duty = input duty
102 * Prescale and duty are register values
105 if (pwm->hwpwm == 0) {
106 reg_prescale = LP3943_REG_PRESCALE0;
107 reg_duty = LP3943_REG_PWM0;
108 } else {
109 reg_prescale = LP3943_REG_PRESCALE1;
110 reg_duty = LP3943_REG_PWM1;
114 * Note that after this clamping, period_ns fits into an int. This is
115 * helpful because we can resort to integer division below instead of
116 * the (more expensive) 64 bit division.
118 period_ns = clamp(period_ns, (u64)LP3943_MIN_PERIOD, (u64)LP3943_MAX_PERIOD);
119 val = (u8)((int)period_ns / LP3943_MIN_PERIOD - 1);
121 err = lp3943_write_byte(lp3943, reg_prescale, val);
122 if (err)
123 return err;
125 duty_ns = min(duty_ns, period_ns);
126 val = (u8)((int)duty_ns * LP3943_MAX_DUTY / (int)period_ns);
128 return lp3943_write_byte(lp3943, reg_duty, val);
131 static int lp3943_pwm_set_mode(struct lp3943_pwm *lp3943_pwm,
132 struct lp3943_pwm_map *pwm_map,
133 u8 val)
135 struct lp3943 *lp3943 = lp3943_pwm->lp3943;
136 const struct lp3943_reg_cfg *mux = lp3943->mux_cfg;
137 int i, index, err;
139 for (i = 0; i < pwm_map->num_outputs; i++) {
140 index = pwm_map->output[i];
141 err = lp3943_update_bits(lp3943, mux[index].reg,
142 mux[index].mask,
143 val << mux[index].shift);
144 if (err)
145 return err;
148 return 0;
151 static int lp3943_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
153 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
154 struct lp3943_pwm_map *pwm_map = &lp3943_pwm->pwm_map[pwm->hwpwm];
155 u8 val;
157 if (pwm->hwpwm == 0)
158 val = LP3943_DIM_PWM0;
159 else
160 val = LP3943_DIM_PWM1;
163 * Each PWM generator is set to control any of outputs of LP3943.
164 * To enable/disable the PWM, these output pins should be configured.
167 return lp3943_pwm_set_mode(lp3943_pwm, pwm_map, val);
170 static void lp3943_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
172 struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
173 struct lp3943_pwm_map *pwm_map = &lp3943_pwm->pwm_map[pwm->hwpwm];
176 * LP3943 outputs are open-drain, so the pin should be configured
177 * when the PWM is disabled.
180 lp3943_pwm_set_mode(lp3943_pwm, pwm_map, LP3943_GPIO_OUT_HIGH);
183 static int lp3943_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
184 const struct pwm_state *state)
186 int err;
188 if (state->polarity != PWM_POLARITY_NORMAL)
189 return -EINVAL;
191 if (!state->enabled) {
192 if (pwm->state.enabled)
193 lp3943_pwm_disable(chip, pwm);
194 return 0;
197 err = lp3943_pwm_config(chip, pwm, state->duty_cycle, state->period);
198 if (err)
199 return err;
201 if (!pwm->state.enabled)
202 err = lp3943_pwm_enable(chip, pwm);
204 return err;
207 static const struct pwm_ops lp3943_pwm_ops = {
208 .request = lp3943_pwm_request,
209 .free = lp3943_pwm_free,
210 .apply = lp3943_pwm_apply,
213 static int lp3943_pwm_parse_dt(struct device *dev,
214 struct lp3943_pwm *lp3943_pwm)
216 static const char * const name[] = { "ti,pwm0", "ti,pwm1", };
217 struct device_node *node = dev->of_node;
218 struct lp3943_platform_data *pdata;
219 struct lp3943_pwm_map *pwm_map;
220 enum lp3943_pwm_output *output;
221 int i, err, proplen, count = 0;
222 u32 num_outputs;
224 if (!node)
225 return -EINVAL;
227 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
228 if (!pdata)
229 return -ENOMEM;
232 * Read the output map configuration from the device tree.
233 * Each of the two PWM generators can drive zero or more outputs.
236 for (i = 0; i < LP3943_NUM_PWMS; i++) {
237 if (!of_get_property(node, name[i], &proplen))
238 continue;
240 num_outputs = proplen / sizeof(u32);
241 if (num_outputs == 0)
242 continue;
244 output = devm_kcalloc(dev, num_outputs, sizeof(*output),
245 GFP_KERNEL);
246 if (!output)
247 return -ENOMEM;
249 err = of_property_read_u32_array(node, name[i], output,
250 num_outputs);
251 if (err)
252 return err;
254 pwm_map = devm_kzalloc(dev, sizeof(*pwm_map), GFP_KERNEL);
255 if (!pwm_map)
256 return -ENOMEM;
258 pwm_map->output = output;
259 pwm_map->num_outputs = num_outputs;
260 pdata->pwms[i] = pwm_map;
262 count++;
265 if (count == 0)
266 return -ENODATA;
268 lp3943_pwm->pdata = pdata;
269 return 0;
272 static int lp3943_pwm_probe(struct platform_device *pdev)
274 struct lp3943 *lp3943 = dev_get_drvdata(pdev->dev.parent);
275 struct pwm_chip *chip;
276 struct lp3943_pwm *lp3943_pwm;
277 int ret;
279 chip = devm_pwmchip_alloc(&pdev->dev, LP3943_NUM_PWMS, sizeof(*lp3943_pwm));
280 if (IS_ERR(chip))
281 return PTR_ERR(chip);
282 lp3943_pwm = to_lp3943_pwm(chip);
284 lp3943_pwm->pdata = lp3943->pdata;
285 if (!lp3943_pwm->pdata) {
286 if (IS_ENABLED(CONFIG_OF))
287 ret = lp3943_pwm_parse_dt(&pdev->dev, lp3943_pwm);
288 else
289 ret = -ENODEV;
291 if (ret)
292 return ret;
295 lp3943_pwm->lp3943 = lp3943;
296 chip->ops = &lp3943_pwm_ops;
298 return devm_pwmchip_add(&pdev->dev, chip);
301 #ifdef CONFIG_OF
302 static const struct of_device_id lp3943_pwm_of_match[] = {
303 { .compatible = "ti,lp3943-pwm", },
306 MODULE_DEVICE_TABLE(of, lp3943_pwm_of_match);
307 #endif
309 static struct platform_driver lp3943_pwm_driver = {
310 .probe = lp3943_pwm_probe,
311 .driver = {
312 .name = "lp3943-pwm",
313 .of_match_table = of_match_ptr(lp3943_pwm_of_match),
316 module_platform_driver(lp3943_pwm_driver);
318 MODULE_DESCRIPTION("LP3943 PWM driver");
319 MODULE_ALIAS("platform:lp3943-pwm");
320 MODULE_AUTHOR("Milo Kim");
321 MODULE_LICENSE("GPL");