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