Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Driver for TWL4030/6030 Pulse Width Modulator used as LED driver
  4 *
  5 * Copyright (C) 2012 Texas Instruments
  6 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  7 *
  8 * This driver is a complete rewrite of the former pwm-twl6030.c authorded by:
  9 * Hemanth V <hemanthv@ti.com>
 10 *
 11 * Reference manual for the twl6030 is available at:
 12 * https://www.ti.com/lit/ds/symlink/twl6030.pdf
 13 *
 14 * Limitations:
 15 * - The twl6030 hardware only supports two period lengths (128 clock ticks and
 16 *   64 clock ticks), the driver only uses 128 ticks
 17 * - The hardware doesn't support ON = 0, so the active part of a period doesn't
 18 *   start at its beginning.
 19 * - The hardware could support inverted polarity (with a similar limitation as
 20 *   for normal: the last clock tick is always inactive).
 21 * - The hardware emits a constant low output when disabled.
 22 * - A request for .duty_cycle = 0 results in an output wave with one active
 23 *   clock tick per period. This should better use the disabled state.
 24 * - The driver only implements setting the relative duty cycle.
 25 * - The driver doesn't implement .get_state().
 26 */
 27
 28#include <linux/module.h>
 29#include <linux/of.h>
 30#include <linux/platform_device.h>
 31#include <linux/pwm.h>
 32#include <linux/mfd/twl.h>
 33#include <linux/slab.h>
 34
 35/*
 36 * This driver handles the PWM driven LED terminals of TWL4030 and TWL6030.
 37 * To generate the signal on TWL4030:
 38 *  - LEDA uses PWMA
 39 *  - LEDB uses PWMB
 40 * TWL6030 has one LED pin with dedicated LEDPWM
 41 */
 42
 43#define TWL4030_LED_MAX		0x7f
 44#define TWL6030_LED_MAX		0xff
 45
 46/* Registers, bits and macro for TWL4030 */
 47#define TWL4030_LEDEN_REG	0x00
 48#define TWL4030_PWMA_REG	0x01
 49
 50#define TWL4030_LEDXON		(1 << 0)
 51#define TWL4030_LEDXPWM		(1 << 4)
 52#define TWL4030_LED_PINS	(TWL4030_LEDXON | TWL4030_LEDXPWM)
 53#define TWL4030_LED_TOGGLE(led, x)	((x) << (led))
 54
 55/* Register, bits and macro for TWL6030 */
 56#define TWL6030_LED_PWM_CTRL1	0xf4
 57#define TWL6030_LED_PWM_CTRL2	0xf5
 58
 59#define TWL6040_LED_MODE_HW	0x00
 60#define TWL6040_LED_MODE_ON	0x01
 61#define TWL6040_LED_MODE_OFF	0x02
 62#define TWL6040_LED_MODE_MASK	0x03
 63
 64struct twl_pwmled_chip {
 65	struct pwm_chip chip;
 66	struct mutex mutex;
 67};
 68
 69static inline struct twl_pwmled_chip *to_twl(struct pwm_chip *chip)
 70{
 71	return container_of(chip, struct twl_pwmled_chip, chip);
 72}
 73
 74static int twl4030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
 75			      int duty_ns, int period_ns)
 76{
 77	int duty_cycle = DIV_ROUND_UP(duty_ns * TWL4030_LED_MAX, period_ns) + 1;
 78	u8 pwm_config[2] = { 1, 0 };
 79	int base, ret;
 80
 81	/*
 82	 * To configure the duty period:
 83	 * On-cycle is set to 1 (the minimum allowed value)
 84	 * The off time of 0 is not configurable, so the mapping is:
 85	 * 0 -> off cycle = 2,
 86	 * 1 -> off cycle = 2,
 87	 * 2 -> off cycle = 3,
 88	 * 126 - > off cycle 127,
 89	 * 127 - > off cycle 1
 90	 * When on cycle == off cycle the PWM will be always on
 91	 */
 92	if (duty_cycle == 1)
 93		duty_cycle = 2;
 94	else if (duty_cycle > TWL4030_LED_MAX)
 95		duty_cycle = 1;
 96
 97	base = pwm->hwpwm * 2 + TWL4030_PWMA_REG;
 98
 99	pwm_config[1] = duty_cycle;
100
101	ret = twl_i2c_write(TWL4030_MODULE_LED, pwm_config, base, 2);
102	if (ret < 0)
103		dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
104
105	return ret;
106}
107
108static int twl4030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
109{
110	struct twl_pwmled_chip *twl = to_twl(chip);
111	int ret;
112	u8 val;
113
114	mutex_lock(&twl->mutex);
115	ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
116	if (ret < 0) {
117		dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
118		goto out;
119	}
120
121	val |= TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
122
123	ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
124	if (ret < 0)
125		dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
126
127out:
128	mutex_unlock(&twl->mutex);
129	return ret;
130}
131
132static void twl4030_pwmled_disable(struct pwm_chip *chip,
133				   struct pwm_device *pwm)
134{
135	struct twl_pwmled_chip *twl = to_twl(chip);
136	int ret;
137	u8 val;
138
139	mutex_lock(&twl->mutex);
140	ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
141	if (ret < 0) {
142		dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
143		goto out;
144	}
145
146	val &= ~TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
147
148	ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
149	if (ret < 0)
150		dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
151
152out:
153	mutex_unlock(&twl->mutex);
154}
155
156static int twl4030_pwmled_apply(struct pwm_chip *chip, struct pwm_device *pwm,
157				const struct pwm_state *state)
158{
159	int ret;
160
161	if (state->polarity != PWM_POLARITY_NORMAL)
162		return -EINVAL;
163
164	if (!state->enabled) {
165		if (pwm->state.enabled)
166			twl4030_pwmled_disable(chip, pwm);
167
168		return 0;
169	}
170
171	/*
172	 * We cannot skip calling ->config even if state->period ==
173	 * pwm->state.period && state->duty_cycle == pwm->state.duty_cycle
174	 * because we might have exited early in the last call to
175	 * pwm_apply_might_sleep because of !state->enabled and so the two values in
176	 * pwm->state might not be configured in hardware.
177	 */
178	ret = twl4030_pwmled_config(chip, pwm,
179				    state->duty_cycle, state->period);
180	if (ret)
181		return ret;
182
183	if (!pwm->state.enabled)
184		ret = twl4030_pwmled_enable(chip, pwm);
185
186	return ret;
187}
188
189
190static const struct pwm_ops twl4030_pwmled_ops = {
191	.apply = twl4030_pwmled_apply,
192};
193
194static int twl6030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
195			      int duty_ns, int period_ns)
196{
197	int duty_cycle = (duty_ns * TWL6030_LED_MAX) / period_ns;
198	u8 on_time;
199	int ret;
200
201	on_time = duty_cycle & 0xff;
202
203	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, on_time,
204			       TWL6030_LED_PWM_CTRL1);
205	if (ret < 0)
206		dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
207
208	return ret;
209}
210
211static int twl6030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
212{
213	struct twl_pwmled_chip *twl = to_twl(chip);
214	int ret;
215	u8 val;
216
217	mutex_lock(&twl->mutex);
218	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
219	if (ret < 0) {
220		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
221			pwm->label);
222		goto out;
223	}
224
225	val &= ~TWL6040_LED_MODE_MASK;
226	val |= TWL6040_LED_MODE_ON;
227
228	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
229	if (ret < 0)
230		dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
231
232out:
233	mutex_unlock(&twl->mutex);
234	return ret;
235}
236
237static void twl6030_pwmled_disable(struct pwm_chip *chip,
238				   struct pwm_device *pwm)
239{
240	struct twl_pwmled_chip *twl = to_twl(chip);
241	int ret;
242	u8 val;
243
244	mutex_lock(&twl->mutex);
245	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
246	if (ret < 0) {
247		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
248			pwm->label);
249		goto out;
250	}
251
252	val &= ~TWL6040_LED_MODE_MASK;
253	val |= TWL6040_LED_MODE_OFF;
254
255	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
256	if (ret < 0)
257		dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
258
259out:
260	mutex_unlock(&twl->mutex);
261}
262
263static int twl6030_pwmled_apply(struct pwm_chip *chip, struct pwm_device *pwm,
264				const struct pwm_state *state)
265{
266	int err;
267
268	if (state->polarity != pwm->state.polarity)
269		return -EINVAL;
270
271	if (!state->enabled) {
272		if (pwm->state.enabled)
273			twl6030_pwmled_disable(chip, pwm);
274
275		return 0;
276	}
277
278	err = twl6030_pwmled_config(chip, pwm,
279				    state->duty_cycle, state->period);
280	if (err)
281		return err;
282
283	if (!pwm->state.enabled)
284		err = twl6030_pwmled_enable(chip, pwm);
285
286	return err;
287}
288
289static int twl6030_pwmled_request(struct pwm_chip *chip, struct pwm_device *pwm)
290{
291	struct twl_pwmled_chip *twl = to_twl(chip);
292	int ret;
293	u8 val;
294
295	mutex_lock(&twl->mutex);
296	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
297	if (ret < 0) {
298		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
299			pwm->label);
300		goto out;
301	}
302
303	val &= ~TWL6040_LED_MODE_MASK;
304	val |= TWL6040_LED_MODE_OFF;
305
306	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
307	if (ret < 0)
308		dev_err(chip->dev, "%s: Failed to request PWM\n", pwm->label);
309
310out:
311	mutex_unlock(&twl->mutex);
312	return ret;
313}
314
315static void twl6030_pwmled_free(struct pwm_chip *chip, struct pwm_device *pwm)
316{
317	struct twl_pwmled_chip *twl = to_twl(chip);
318	int ret;
319	u8 val;
320
321	mutex_lock(&twl->mutex);
322	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
323	if (ret < 0) {
324		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
325			pwm->label);
326		goto out;
327	}
328
329	val &= ~TWL6040_LED_MODE_MASK;
330	val |= TWL6040_LED_MODE_HW;
331
332	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
333	if (ret < 0)
334		dev_err(chip->dev, "%s: Failed to free PWM\n", pwm->label);
335
336out:
337	mutex_unlock(&twl->mutex);
338}
339
340static const struct pwm_ops twl6030_pwmled_ops = {
341	.apply = twl6030_pwmled_apply,
342	.request = twl6030_pwmled_request,
343	.free = twl6030_pwmled_free,
344};
345
346static int twl_pwmled_probe(struct platform_device *pdev)
347{
 
348	struct twl_pwmled_chip *twl;
349
350	twl = devm_kzalloc(&pdev->dev, sizeof(*twl), GFP_KERNEL);
351	if (!twl)
352		return -ENOMEM;
353
354	if (twl_class_is_4030()) {
355		twl->chip.ops = &twl4030_pwmled_ops;
356		twl->chip.npwm = 2;
357	} else {
358		twl->chip.ops = &twl6030_pwmled_ops;
359		twl->chip.npwm = 1;
360	}
361
362	twl->chip.dev = &pdev->dev;
 
 
 
 
 
363
364	mutex_init(&twl->mutex);
365
366	return devm_pwmchip_add(&pdev->dev, &twl->chip);
367}
368
369#ifdef CONFIG_OF
370static const struct of_device_id twl_pwmled_of_match[] = {
371	{ .compatible = "ti,twl4030-pwmled" },
372	{ .compatible = "ti,twl6030-pwmled" },
373	{ },
374};
375MODULE_DEVICE_TABLE(of, twl_pwmled_of_match);
376#endif
377
378static struct platform_driver twl_pwmled_driver = {
379	.driver = {
380		.name = "twl-pwmled",
381		.of_match_table = of_match_ptr(twl_pwmled_of_match),
382	},
383	.probe = twl_pwmled_probe,
384};
385module_platform_driver(twl_pwmled_driver);
386
387MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
388MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030 LED outputs");
389MODULE_ALIAS("platform:twl-pwmled");
390MODULE_LICENSE("GPL");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Driver for TWL4030/6030 Pulse Width Modulator used as LED driver
  4 *
  5 * Copyright (C) 2012 Texas Instruments
  6 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  7 *
  8 * This driver is a complete rewrite of the former pwm-twl6030.c authorded by:
  9 * Hemanth V <hemanthv@ti.com>
 10 *
 11 * Reference manual for the twl6030 is available at:
 12 * https://www.ti.com/lit/ds/symlink/twl6030.pdf
 13 *
 14 * Limitations:
 15 * - The twl6030 hardware only supports two period lengths (128 clock ticks and
 16 *   64 clock ticks), the driver only uses 128 ticks
 17 * - The hardware doesn't support ON = 0, so the active part of a period doesn't
 18 *   start at its beginning.
 19 * - The hardware could support inverted polarity (with a similar limitation as
 20 *   for normal: the last clock tick is always inactive).
 21 * - The hardware emits a constant low output when disabled.
 22 * - A request for .duty_cycle = 0 results in an output wave with one active
 23 *   clock tick per period. This should better use the disabled state.
 24 * - The driver only implements setting the relative duty cycle.
 25 * - The driver doesn't implement .get_state().
 26 */
 27
 28#include <linux/module.h>
 29#include <linux/of.h>
 30#include <linux/platform_device.h>
 31#include <linux/pwm.h>
 32#include <linux/mfd/twl.h>
 33#include <linux/slab.h>
 34
 35/*
 36 * This driver handles the PWM driven LED terminals of TWL4030 and TWL6030.
 37 * To generate the signal on TWL4030:
 38 *  - LEDA uses PWMA
 39 *  - LEDB uses PWMB
 40 * TWL6030 has one LED pin with dedicated LEDPWM
 41 */
 42
 43#define TWL4030_LED_MAX		0x7f
 44#define TWL6030_LED_MAX		0xff
 45
 46/* Registers, bits and macro for TWL4030 */
 47#define TWL4030_LEDEN_REG	0x00
 48#define TWL4030_PWMA_REG	0x01
 49
 50#define TWL4030_LEDXON		(1 << 0)
 51#define TWL4030_LEDXPWM		(1 << 4)
 52#define TWL4030_LED_PINS	(TWL4030_LEDXON | TWL4030_LEDXPWM)
 53#define TWL4030_LED_TOGGLE(led, x)	((x) << (led))
 54
 55/* Register, bits and macro for TWL6030 */
 56#define TWL6030_LED_PWM_CTRL1	0xf4
 57#define TWL6030_LED_PWM_CTRL2	0xf5
 58
 59#define TWL6040_LED_MODE_HW	0x00
 60#define TWL6040_LED_MODE_ON	0x01
 61#define TWL6040_LED_MODE_OFF	0x02
 62#define TWL6040_LED_MODE_MASK	0x03
 63
 64struct twl_pwmled_chip {
 
 65	struct mutex mutex;
 66};
 67
 68static inline struct twl_pwmled_chip *to_twl(struct pwm_chip *chip)
 69{
 70	return pwmchip_get_drvdata(chip);
 71}
 72
 73static int twl4030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
 74			      int duty_ns, int period_ns)
 75{
 76	int duty_cycle = DIV_ROUND_UP(duty_ns * TWL4030_LED_MAX, period_ns) + 1;
 77	u8 pwm_config[2] = { 1, 0 };
 78	int base, ret;
 79
 80	/*
 81	 * To configure the duty period:
 82	 * On-cycle is set to 1 (the minimum allowed value)
 83	 * The off time of 0 is not configurable, so the mapping is:
 84	 * 0 -> off cycle = 2,
 85	 * 1 -> off cycle = 2,
 86	 * 2 -> off cycle = 3,
 87	 * 126 - > off cycle 127,
 88	 * 127 - > off cycle 1
 89	 * When on cycle == off cycle the PWM will be always on
 90	 */
 91	if (duty_cycle == 1)
 92		duty_cycle = 2;
 93	else if (duty_cycle > TWL4030_LED_MAX)
 94		duty_cycle = 1;
 95
 96	base = pwm->hwpwm * 2 + TWL4030_PWMA_REG;
 97
 98	pwm_config[1] = duty_cycle;
 99
100	ret = twl_i2c_write(TWL4030_MODULE_LED, pwm_config, base, 2);
101	if (ret < 0)
102		dev_err(pwmchip_parent(chip), "%s: Failed to configure PWM\n", pwm->label);
103
104	return ret;
105}
106
107static int twl4030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
108{
109	struct twl_pwmled_chip *twl = to_twl(chip);
110	int ret;
111	u8 val;
112
113	mutex_lock(&twl->mutex);
114	ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
115	if (ret < 0) {
116		dev_err(pwmchip_parent(chip), "%s: Failed to read LEDEN\n", pwm->label);
117		goto out;
118	}
119
120	val |= TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
121
122	ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
123	if (ret < 0)
124		dev_err(pwmchip_parent(chip), "%s: Failed to enable PWM\n", pwm->label);
125
126out:
127	mutex_unlock(&twl->mutex);
128	return ret;
129}
130
131static void twl4030_pwmled_disable(struct pwm_chip *chip,
132				   struct pwm_device *pwm)
133{
134	struct twl_pwmled_chip *twl = to_twl(chip);
135	int ret;
136	u8 val;
137
138	mutex_lock(&twl->mutex);
139	ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
140	if (ret < 0) {
141		dev_err(pwmchip_parent(chip), "%s: Failed to read LEDEN\n", pwm->label);
142		goto out;
143	}
144
145	val &= ~TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
146
147	ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
148	if (ret < 0)
149		dev_err(pwmchip_parent(chip), "%s: Failed to disable PWM\n", pwm->label);
150
151out:
152	mutex_unlock(&twl->mutex);
153}
154
155static int twl4030_pwmled_apply(struct pwm_chip *chip, struct pwm_device *pwm,
156				const struct pwm_state *state)
157{
158	int ret;
159
160	if (state->polarity != PWM_POLARITY_NORMAL)
161		return -EINVAL;
162
163	if (!state->enabled) {
164		if (pwm->state.enabled)
165			twl4030_pwmled_disable(chip, pwm);
166
167		return 0;
168	}
169
170	/*
171	 * We cannot skip calling ->config even if state->period ==
172	 * pwm->state.period && state->duty_cycle == pwm->state.duty_cycle
173	 * because we might have exited early in the last call to
174	 * pwm_apply_might_sleep because of !state->enabled and so the two values in
175	 * pwm->state might not be configured in hardware.
176	 */
177	ret = twl4030_pwmled_config(chip, pwm,
178				    state->duty_cycle, state->period);
179	if (ret)
180		return ret;
181
182	if (!pwm->state.enabled)
183		ret = twl4030_pwmled_enable(chip, pwm);
184
185	return ret;
186}
187
188
189static const struct pwm_ops twl4030_pwmled_ops = {
190	.apply = twl4030_pwmled_apply,
191};
192
193static int twl6030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
194			      int duty_ns, int period_ns)
195{
196	int duty_cycle = (duty_ns * TWL6030_LED_MAX) / period_ns;
197	u8 on_time;
198	int ret;
199
200	on_time = duty_cycle & 0xff;
201
202	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, on_time,
203			       TWL6030_LED_PWM_CTRL1);
204	if (ret < 0)
205		dev_err(pwmchip_parent(chip), "%s: Failed to configure PWM\n", pwm->label);
206
207	return ret;
208}
209
210static int twl6030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
211{
212	struct twl_pwmled_chip *twl = to_twl(chip);
213	int ret;
214	u8 val;
215
216	mutex_lock(&twl->mutex);
217	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
218	if (ret < 0) {
219		dev_err(pwmchip_parent(chip), "%s: Failed to read PWM_CTRL2\n",
220			pwm->label);
221		goto out;
222	}
223
224	val &= ~TWL6040_LED_MODE_MASK;
225	val |= TWL6040_LED_MODE_ON;
226
227	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
228	if (ret < 0)
229		dev_err(pwmchip_parent(chip), "%s: Failed to enable PWM\n", pwm->label);
230
231out:
232	mutex_unlock(&twl->mutex);
233	return ret;
234}
235
236static void twl6030_pwmled_disable(struct pwm_chip *chip,
237				   struct pwm_device *pwm)
238{
239	struct twl_pwmled_chip *twl = to_twl(chip);
240	int ret;
241	u8 val;
242
243	mutex_lock(&twl->mutex);
244	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
245	if (ret < 0) {
246		dev_err(pwmchip_parent(chip), "%s: Failed to read PWM_CTRL2\n",
247			pwm->label);
248		goto out;
249	}
250
251	val &= ~TWL6040_LED_MODE_MASK;
252	val |= TWL6040_LED_MODE_OFF;
253
254	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
255	if (ret < 0)
256		dev_err(pwmchip_parent(chip), "%s: Failed to disable PWM\n", pwm->label);
257
258out:
259	mutex_unlock(&twl->mutex);
260}
261
262static int twl6030_pwmled_apply(struct pwm_chip *chip, struct pwm_device *pwm,
263				const struct pwm_state *state)
264{
265	int err;
266
267	if (state->polarity != pwm->state.polarity)
268		return -EINVAL;
269
270	if (!state->enabled) {
271		if (pwm->state.enabled)
272			twl6030_pwmled_disable(chip, pwm);
273
274		return 0;
275	}
276
277	err = twl6030_pwmled_config(chip, pwm,
278				    state->duty_cycle, state->period);
279	if (err)
280		return err;
281
282	if (!pwm->state.enabled)
283		err = twl6030_pwmled_enable(chip, pwm);
284
285	return err;
286}
287
288static int twl6030_pwmled_request(struct pwm_chip *chip, struct pwm_device *pwm)
289{
290	struct twl_pwmled_chip *twl = to_twl(chip);
291	int ret;
292	u8 val;
293
294	mutex_lock(&twl->mutex);
295	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
296	if (ret < 0) {
297		dev_err(pwmchip_parent(chip), "%s: Failed to read PWM_CTRL2\n",
298			pwm->label);
299		goto out;
300	}
301
302	val &= ~TWL6040_LED_MODE_MASK;
303	val |= TWL6040_LED_MODE_OFF;
304
305	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
306	if (ret < 0)
307		dev_err(pwmchip_parent(chip), "%s: Failed to request PWM\n", pwm->label);
308
309out:
310	mutex_unlock(&twl->mutex);
311	return ret;
312}
313
314static void twl6030_pwmled_free(struct pwm_chip *chip, struct pwm_device *pwm)
315{
316	struct twl_pwmled_chip *twl = to_twl(chip);
317	int ret;
318	u8 val;
319
320	mutex_lock(&twl->mutex);
321	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
322	if (ret < 0) {
323		dev_err(pwmchip_parent(chip), "%s: Failed to read PWM_CTRL2\n",
324			pwm->label);
325		goto out;
326	}
327
328	val &= ~TWL6040_LED_MODE_MASK;
329	val |= TWL6040_LED_MODE_HW;
330
331	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
332	if (ret < 0)
333		dev_err(pwmchip_parent(chip), "%s: Failed to free PWM\n", pwm->label);
334
335out:
336	mutex_unlock(&twl->mutex);
337}
338
339static const struct pwm_ops twl6030_pwmled_ops = {
340	.apply = twl6030_pwmled_apply,
341	.request = twl6030_pwmled_request,
342	.free = twl6030_pwmled_free,
343};
344
345static int twl_pwmled_probe(struct platform_device *pdev)
346{
347	struct pwm_chip *chip;
348	struct twl_pwmled_chip *twl;
349	unsigned int npwm;
350	const struct pwm_ops *ops;
 
 
351
352	if (twl_class_is_4030()) {
353		ops = &twl4030_pwmled_ops;
354		npwm = 2;
355	} else {
356		ops = &twl6030_pwmled_ops;
357		npwm = 1;
358	}
359
360	chip = devm_pwmchip_alloc(&pdev->dev, npwm, sizeof(*twl));
361	if (IS_ERR(chip))
362		return PTR_ERR(chip);
363	twl = to_twl(chip);
364
365	chip->ops = ops;
366
367	mutex_init(&twl->mutex);
368
369	return devm_pwmchip_add(&pdev->dev, chip);
370}
371
372#ifdef CONFIG_OF
373static const struct of_device_id twl_pwmled_of_match[] = {
374	{ .compatible = "ti,twl4030-pwmled" },
375	{ .compatible = "ti,twl6030-pwmled" },
376	{ },
377};
378MODULE_DEVICE_TABLE(of, twl_pwmled_of_match);
379#endif
380
381static struct platform_driver twl_pwmled_driver = {
382	.driver = {
383		.name = "twl-pwmled",
384		.of_match_table = of_match_ptr(twl_pwmled_of_match),
385	},
386	.probe = twl_pwmled_probe,
387};
388module_platform_driver(twl_pwmled_driver);
389
390MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
391MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030 LED outputs");
392MODULE_ALIAS("platform:twl-pwmled");
393MODULE_LICENSE("GPL");