xref: /linux/drivers/pwm/pwm-lp3943.c (revision 3652117f854819a148ff0fbe4492587d3520b5e5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TI/National Semiconductor LP3943 PWM driver
4  *
5  * Copyright 2013 Texas Instruments
6  *
7  * Author: Milo Kim <milo.kim@ti.com>
8  */
9 
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>
17 
18 #define LP3943_MAX_DUTY			255
19 #define LP3943_MIN_PERIOD		6250
20 #define LP3943_MAX_PERIOD		1600000
21 
22 struct lp3943_pwm {
23 	struct pwm_chip chip;
24 	struct lp3943 *lp3943;
25 	struct lp3943_platform_data *pdata;
26 	struct lp3943_pwm_map pwm_map[LP3943_NUM_PWMS];
27 };
28 
29 static inline struct lp3943_pwm *to_lp3943_pwm(struct pwm_chip *chip)
30 {
31 	return container_of(chip, struct lp3943_pwm, chip);
32 }
33 
34 static struct lp3943_pwm_map *
35 lp3943_pwm_request_map(struct lp3943_pwm *lp3943_pwm, int hwpwm)
36 {
37 	struct lp3943_platform_data *pdata = lp3943_pwm->pdata;
38 	struct lp3943 *lp3943 = lp3943_pwm->lp3943;
39 	struct lp3943_pwm_map *pwm_map = &lp3943_pwm->pwm_map[hwpwm];
40 	int i, offset;
41 
42 	pwm_map->output = pdata->pwms[hwpwm]->output;
43 	pwm_map->num_outputs = pdata->pwms[hwpwm]->num_outputs;
44 
45 	for (i = 0; i < pwm_map->num_outputs; i++) {
46 		offset = pwm_map->output[i];
47 
48 		/* Return an error if the pin is already assigned */
49 		if (test_and_set_bit(offset, &lp3943->pin_used))
50 			return ERR_PTR(-EBUSY);
51 	}
52 
53 	return pwm_map;
54 }
55 
56 static int lp3943_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
57 {
58 	struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
59 	struct lp3943_pwm_map *pwm_map;
60 
61 	pwm_map = lp3943_pwm_request_map(lp3943_pwm, pwm->hwpwm);
62 	if (IS_ERR(pwm_map))
63 		return PTR_ERR(pwm_map);
64 
65 	return 0;
66 }
67 
68 static void lp3943_pwm_free_map(struct lp3943_pwm *lp3943_pwm,
69 				struct lp3943_pwm_map *pwm_map)
70 {
71 	struct lp3943 *lp3943 = lp3943_pwm->lp3943;
72 	int i, offset;
73 
74 	for (i = 0; i < pwm_map->num_outputs; i++) {
75 		offset = pwm_map->output[i];
76 		clear_bit(offset, &lp3943->pin_used);
77 	}
78 }
79 
80 static void lp3943_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
81 {
82 	struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
83 	struct lp3943_pwm_map *pwm_map = &lp3943_pwm->pwm_map[pwm->hwpwm];
84 
85 	lp3943_pwm_free_map(lp3943_pwm, pwm_map);
86 }
87 
88 static int lp3943_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
89 			     u64 duty_ns, u64 period_ns)
90 {
91 	struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
92 	struct lp3943 *lp3943 = lp3943_pwm->lp3943;
93 	u8 val, reg_duty, reg_prescale;
94 	int err;
95 
96 	/*
97 	 * How to configure the LP3943 PWMs
98 	 *
99 	 * 1) Period = 6250 ~ 1600000
100 	 * 2) Prescale = period / 6250 -1
101 	 * 3) Duty = input duty
102 	 *
103 	 * Prescale and duty are register values
104 	 */
105 
106 	if (pwm->hwpwm == 0) {
107 		reg_prescale = LP3943_REG_PRESCALE0;
108 		reg_duty     = LP3943_REG_PWM0;
109 	} else {
110 		reg_prescale = LP3943_REG_PRESCALE1;
111 		reg_duty     = LP3943_REG_PWM1;
112 	}
113 
114 	/*
115 	 * Note that after this clamping, period_ns fits into an int. This is
116 	 * helpful because we can resort to integer division below instead of
117 	 * the (more expensive) 64 bit division.
118 	 */
119 	period_ns = clamp(period_ns, (u64)LP3943_MIN_PERIOD, (u64)LP3943_MAX_PERIOD);
120 	val       = (u8)((int)period_ns / LP3943_MIN_PERIOD - 1);
121 
122 	err = lp3943_write_byte(lp3943, reg_prescale, val);
123 	if (err)
124 		return err;
125 
126 	duty_ns = min(duty_ns, period_ns);
127 	val = (u8)((int)duty_ns * LP3943_MAX_DUTY / (int)period_ns);
128 
129 	return lp3943_write_byte(lp3943, reg_duty, val);
130 }
131 
132 static int lp3943_pwm_set_mode(struct lp3943_pwm *lp3943_pwm,
133 			       struct lp3943_pwm_map *pwm_map,
134 			       u8 val)
135 {
136 	struct lp3943 *lp3943 = lp3943_pwm->lp3943;
137 	const struct lp3943_reg_cfg *mux = lp3943->mux_cfg;
138 	int i, index, err;
139 
140 	for (i = 0; i < pwm_map->num_outputs; i++) {
141 		index = pwm_map->output[i];
142 		err = lp3943_update_bits(lp3943, mux[index].reg,
143 					 mux[index].mask,
144 					 val << mux[index].shift);
145 		if (err)
146 			return err;
147 	}
148 
149 	return 0;
150 }
151 
152 static int lp3943_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
153 {
154 	struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
155 	struct lp3943_pwm_map *pwm_map = &lp3943_pwm->pwm_map[pwm->hwpwm];
156 	u8 val;
157 
158 	if (pwm->hwpwm == 0)
159 		val = LP3943_DIM_PWM0;
160 	else
161 		val = LP3943_DIM_PWM1;
162 
163 	/*
164 	 * Each PWM generator is set to control any of outputs of LP3943.
165 	 * To enable/disable the PWM, these output pins should be configured.
166 	 */
167 
168 	return lp3943_pwm_set_mode(lp3943_pwm, pwm_map, val);
169 }
170 
171 static void lp3943_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
172 {
173 	struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
174 	struct lp3943_pwm_map *pwm_map = &lp3943_pwm->pwm_map[pwm->hwpwm];
175 
176 	/*
177 	 * LP3943 outputs are open-drain, so the pin should be configured
178 	 * when the PWM is disabled.
179 	 */
180 
181 	lp3943_pwm_set_mode(lp3943_pwm, pwm_map, LP3943_GPIO_OUT_HIGH);
182 }
183 
184 static int lp3943_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
185 			    const struct pwm_state *state)
186 {
187 	int err;
188 
189 	if (state->polarity != PWM_POLARITY_NORMAL)
190 		return -EINVAL;
191 
192 	if (!state->enabled) {
193 		if (pwm->state.enabled)
194 			lp3943_pwm_disable(chip, pwm);
195 		return 0;
196 	}
197 
198 	err = lp3943_pwm_config(chip, pwm, state->duty_cycle, state->period);
199 	if (err)
200 		return err;
201 
202 	if (!pwm->state.enabled)
203 		err = lp3943_pwm_enable(chip, pwm);
204 
205 	return err;
206 }
207 
208 static const struct pwm_ops lp3943_pwm_ops = {
209 	.request	= lp3943_pwm_request,
210 	.free		= lp3943_pwm_free,
211 	.apply		= lp3943_pwm_apply,
212 };
213 
214 static int lp3943_pwm_parse_dt(struct device *dev,
215 			       struct lp3943_pwm *lp3943_pwm)
216 {
217 	static const char * const name[] = { "ti,pwm0", "ti,pwm1", };
218 	struct device_node *node = dev->of_node;
219 	struct lp3943_platform_data *pdata;
220 	struct lp3943_pwm_map *pwm_map;
221 	enum lp3943_pwm_output *output;
222 	int i, err, proplen, count = 0;
223 	u32 num_outputs;
224 
225 	if (!node)
226 		return -EINVAL;
227 
228 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
229 	if (!pdata)
230 		return -ENOMEM;
231 
232 	/*
233 	 * Read the output map configuration from the device tree.
234 	 * Each of the two PWM generators can drive zero or more outputs.
235 	 */
236 
237 	for (i = 0; i < LP3943_NUM_PWMS; i++) {
238 		if (!of_get_property(node, name[i], &proplen))
239 			continue;
240 
241 		num_outputs = proplen / sizeof(u32);
242 		if (num_outputs == 0)
243 			continue;
244 
245 		output = devm_kcalloc(dev, num_outputs, sizeof(*output),
246 				      GFP_KERNEL);
247 		if (!output)
248 			return -ENOMEM;
249 
250 		err = of_property_read_u32_array(node, name[i], output,
251 						 num_outputs);
252 		if (err)
253 			return err;
254 
255 		pwm_map = devm_kzalloc(dev, sizeof(*pwm_map), GFP_KERNEL);
256 		if (!pwm_map)
257 			return -ENOMEM;
258 
259 		pwm_map->output = output;
260 		pwm_map->num_outputs = num_outputs;
261 		pdata->pwms[i] = pwm_map;
262 
263 		count++;
264 	}
265 
266 	if (count == 0)
267 		return -ENODATA;
268 
269 	lp3943_pwm->pdata = pdata;
270 	return 0;
271 }
272 
273 static int lp3943_pwm_probe(struct platform_device *pdev)
274 {
275 	struct lp3943 *lp3943 = dev_get_drvdata(pdev->dev.parent);
276 	struct lp3943_pwm *lp3943_pwm;
277 	int ret;
278 
279 	lp3943_pwm = devm_kzalloc(&pdev->dev, sizeof(*lp3943_pwm), GFP_KERNEL);
280 	if (!lp3943_pwm)
281 		return -ENOMEM;
282 
283 	lp3943_pwm->pdata = lp3943->pdata;
284 	if (!lp3943_pwm->pdata) {
285 		if (IS_ENABLED(CONFIG_OF))
286 			ret = lp3943_pwm_parse_dt(&pdev->dev, lp3943_pwm);
287 		else
288 			ret = -ENODEV;
289 
290 		if (ret)
291 			return ret;
292 	}
293 
294 	lp3943_pwm->lp3943 = lp3943;
295 	lp3943_pwm->chip.dev = &pdev->dev;
296 	lp3943_pwm->chip.ops = &lp3943_pwm_ops;
297 	lp3943_pwm->chip.npwm = LP3943_NUM_PWMS;
298 
299 	return devm_pwmchip_add(&pdev->dev, &lp3943_pwm->chip);
300 }
301 
302 #ifdef CONFIG_OF
303 static const struct of_device_id lp3943_pwm_of_match[] = {
304 	{ .compatible = "ti,lp3943-pwm", },
305 	{ }
306 };
307 MODULE_DEVICE_TABLE(of, lp3943_pwm_of_match);
308 #endif
309 
310 static struct platform_driver lp3943_pwm_driver = {
311 	.probe = lp3943_pwm_probe,
312 	.driver = {
313 		.name = "lp3943-pwm",
314 		.of_match_table = of_match_ptr(lp3943_pwm_of_match),
315 	},
316 };
317 module_platform_driver(lp3943_pwm_driver);
318 
319 MODULE_DESCRIPTION("LP3943 PWM driver");
320 MODULE_ALIAS("platform:lp3943-pwm");
321 MODULE_AUTHOR("Milo Kim");
322 MODULE_LICENSE("GPL");
323