Linux Audio

Check our new training course

Loading...
v4.17
 
  1/*
  2 * Driver for TWL4030/6030 Pulse Width Modulator used as LED driver
  3 *
  4 * Copyright (C) 2012 Texas Instruments
  5 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  6 *
  7 * This driver is a complete rewrite of the former pwm-twl6030.c authorded by:
  8 * Hemanth V <hemanthv@ti.com>
  9 *
 10 * This program is free software; you can redistribute it and/or modify it
 11 * under the terms of the GNU General Public License version 2 as published by
 12 * the Free Software Foundation.
 13 *
 14 * This program is distributed in the hope that it will be useful, but WITHOUT
 15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 16 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 17 * more details.
 18 *
 19 * You should have received a copy of the GNU General Public License along with
 20 * this program.  If not, see <http://www.gnu.org/licenses/>.
 21 */
 22
 23#include <linux/module.h>
 24#include <linux/of.h>
 25#include <linux/platform_device.h>
 26#include <linux/pwm.h>
 27#include <linux/mfd/twl.h>
 28#include <linux/slab.h>
 29
 30/*
 31 * This driver handles the PWM driven LED terminals of TWL4030 and TWL6030.
 32 * To generate the signal on TWL4030:
 33 *  - LEDA uses PWMA
 34 *  - LEDB uses PWMB
 35 * TWL6030 has one LED pin with dedicated LEDPWM
 36 */
 37
 38#define TWL4030_LED_MAX		0x7f
 39#define TWL6030_LED_MAX		0xff
 40
 41/* Registers, bits and macro for TWL4030 */
 42#define TWL4030_LEDEN_REG	0x00
 43#define TWL4030_PWMA_REG	0x01
 44
 45#define TWL4030_LEDXON		(1 << 0)
 46#define TWL4030_LEDXPWM		(1 << 4)
 47#define TWL4030_LED_PINS	(TWL4030_LEDXON | TWL4030_LEDXPWM)
 48#define TWL4030_LED_TOGGLE(led, x)	((x) << (led))
 49
 50/* Register, bits and macro for TWL6030 */
 51#define TWL6030_LED_PWM_CTRL1	0xf4
 52#define TWL6030_LED_PWM_CTRL2	0xf5
 53
 54#define TWL6040_LED_MODE_HW	0x00
 55#define TWL6040_LED_MODE_ON	0x01
 56#define TWL6040_LED_MODE_OFF	0x02
 57#define TWL6040_LED_MODE_MASK	0x03
 58
 59struct twl_pwmled_chip {
 60	struct pwm_chip chip;
 61	struct mutex mutex;
 62};
 63
 64static inline struct twl_pwmled_chip *to_twl(struct pwm_chip *chip)
 65{
 66	return container_of(chip, struct twl_pwmled_chip, chip);
 67}
 68
 69static int twl4030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
 70			      int duty_ns, int period_ns)
 71{
 72	int duty_cycle = DIV_ROUND_UP(duty_ns * TWL4030_LED_MAX, period_ns) + 1;
 73	u8 pwm_config[2] = { 1, 0 };
 74	int base, ret;
 75
 76	/*
 77	 * To configure the duty period:
 78	 * On-cycle is set to 1 (the minimum allowed value)
 79	 * The off time of 0 is not configurable, so the mapping is:
 80	 * 0 -> off cycle = 2,
 81	 * 1 -> off cycle = 2,
 82	 * 2 -> off cycle = 3,
 83	 * 126 - > off cycle 127,
 84	 * 127 - > off cycle 1
 85	 * When on cycle == off cycle the PWM will be always on
 86	 */
 87	if (duty_cycle == 1)
 88		duty_cycle = 2;
 89	else if (duty_cycle > TWL4030_LED_MAX)
 90		duty_cycle = 1;
 91
 92	base = pwm->hwpwm * 2 + TWL4030_PWMA_REG;
 93
 94	pwm_config[1] = duty_cycle;
 95
 96	ret = twl_i2c_write(TWL4030_MODULE_LED, pwm_config, base, 2);
 97	if (ret < 0)
 98		dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
 99
100	return ret;
101}
102
103static int twl4030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
104{
105	struct twl_pwmled_chip *twl = to_twl(chip);
106	int ret;
107	u8 val;
108
109	mutex_lock(&twl->mutex);
110	ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
111	if (ret < 0) {
112		dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
113		goto out;
114	}
115
116	val |= TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
117
118	ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
119	if (ret < 0)
120		dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
121
122out:
123	mutex_unlock(&twl->mutex);
124	return ret;
125}
126
127static void twl4030_pwmled_disable(struct pwm_chip *chip,
128				   struct pwm_device *pwm)
129{
130	struct twl_pwmled_chip *twl = to_twl(chip);
131	int ret;
132	u8 val;
133
134	mutex_lock(&twl->mutex);
135	ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
136	if (ret < 0) {
137		dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
138		goto out;
139	}
140
141	val &= ~TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
142
143	ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
144	if (ret < 0)
145		dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
146
147out:
148	mutex_unlock(&twl->mutex);
149}
150
151static int twl6030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
152			      int duty_ns, int period_ns)
153{
154	int duty_cycle = (duty_ns * TWL6030_LED_MAX) / period_ns;
155	u8 on_time;
156	int ret;
157
158	on_time = duty_cycle & 0xff;
159
160	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, on_time,
161			       TWL6030_LED_PWM_CTRL1);
162	if (ret < 0)
163		dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
164
165	return ret;
166}
167
168static int twl6030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
169{
170	struct twl_pwmled_chip *twl = to_twl(chip);
171	int ret;
172	u8 val;
173
174	mutex_lock(&twl->mutex);
175	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
176	if (ret < 0) {
177		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
178			pwm->label);
179		goto out;
180	}
181
182	val &= ~TWL6040_LED_MODE_MASK;
183	val |= TWL6040_LED_MODE_ON;
184
185	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
186	if (ret < 0)
187		dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
188
189out:
190	mutex_unlock(&twl->mutex);
191	return ret;
192}
193
194static void twl6030_pwmled_disable(struct pwm_chip *chip,
195				   struct pwm_device *pwm)
196{
197	struct twl_pwmled_chip *twl = to_twl(chip);
198	int ret;
199	u8 val;
200
201	mutex_lock(&twl->mutex);
202	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
203	if (ret < 0) {
204		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
205			pwm->label);
206		goto out;
207	}
208
209	val &= ~TWL6040_LED_MODE_MASK;
210	val |= TWL6040_LED_MODE_OFF;
211
212	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
213	if (ret < 0)
214		dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
215
216out:
217	mutex_unlock(&twl->mutex);
218}
219
220static int twl6030_pwmled_request(struct pwm_chip *chip, struct pwm_device *pwm)
221{
222	struct twl_pwmled_chip *twl = to_twl(chip);
223	int ret;
224	u8 val;
225
226	mutex_lock(&twl->mutex);
227	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
228	if (ret < 0) {
229		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
230			pwm->label);
231		goto out;
232	}
233
234	val &= ~TWL6040_LED_MODE_MASK;
235	val |= TWL6040_LED_MODE_OFF;
236
237	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
238	if (ret < 0)
239		dev_err(chip->dev, "%s: Failed to request PWM\n", pwm->label);
240
241out:
242	mutex_unlock(&twl->mutex);
243	return ret;
244}
245
246static void twl6030_pwmled_free(struct pwm_chip *chip, struct pwm_device *pwm)
247{
248	struct twl_pwmled_chip *twl = to_twl(chip);
249	int ret;
250	u8 val;
251
252	mutex_lock(&twl->mutex);
253	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
254	if (ret < 0) {
255		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
256			pwm->label);
257		goto out;
258	}
259
260	val &= ~TWL6040_LED_MODE_MASK;
261	val |= TWL6040_LED_MODE_HW;
262
263	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
264	if (ret < 0)
265		dev_err(chip->dev, "%s: Failed to free PWM\n", pwm->label);
266
267out:
268	mutex_unlock(&twl->mutex);
269}
270
271static const struct pwm_ops twl4030_pwmled_ops = {
272	.enable = twl4030_pwmled_enable,
273	.disable = twl4030_pwmled_disable,
274	.config = twl4030_pwmled_config,
275	.owner = THIS_MODULE,
276};
277
278static const struct pwm_ops twl6030_pwmled_ops = {
279	.enable = twl6030_pwmled_enable,
280	.disable = twl6030_pwmled_disable,
281	.config = twl6030_pwmled_config,
282	.request = twl6030_pwmled_request,
283	.free = twl6030_pwmled_free,
284	.owner = THIS_MODULE,
285};
286
287static int twl_pwmled_probe(struct platform_device *pdev)
288{
289	struct twl_pwmled_chip *twl;
290	int ret;
291
292	twl = devm_kzalloc(&pdev->dev, sizeof(*twl), GFP_KERNEL);
293	if (!twl)
294		return -ENOMEM;
295
296	if (twl_class_is_4030()) {
297		twl->chip.ops = &twl4030_pwmled_ops;
298		twl->chip.npwm = 2;
299	} else {
300		twl->chip.ops = &twl6030_pwmled_ops;
301		twl->chip.npwm = 1;
302	}
303
304	twl->chip.dev = &pdev->dev;
305	twl->chip.base = -1;
306
307	mutex_init(&twl->mutex);
308
309	ret = pwmchip_add(&twl->chip);
310	if (ret < 0)
311		return ret;
312
313	platform_set_drvdata(pdev, twl);
314
315	return 0;
316}
317
318static int twl_pwmled_remove(struct platform_device *pdev)
319{
320	struct twl_pwmled_chip *twl = platform_get_drvdata(pdev);
321
322	return pwmchip_remove(&twl->chip);
323}
324
325#ifdef CONFIG_OF
326static const struct of_device_id twl_pwmled_of_match[] = {
327	{ .compatible = "ti,twl4030-pwmled" },
328	{ .compatible = "ti,twl6030-pwmled" },
329	{ },
330};
331MODULE_DEVICE_TABLE(of, twl_pwmled_of_match);
332#endif
333
334static struct platform_driver twl_pwmled_driver = {
335	.driver = {
336		.name = "twl-pwmled",
337		.of_match_table = of_match_ptr(twl_pwmled_of_match),
338	},
339	.probe = twl_pwmled_probe,
340	.remove = twl_pwmled_remove,
341};
342module_platform_driver(twl_pwmled_driver);
343
344MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
345MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030 LED outputs");
346MODULE_ALIAS("platform:twl-pwmled");
347MODULE_LICENSE("GPL");
v5.9
  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
 12#include <linux/module.h>
 13#include <linux/of.h>
 14#include <linux/platform_device.h>
 15#include <linux/pwm.h>
 16#include <linux/mfd/twl.h>
 17#include <linux/slab.h>
 18
 19/*
 20 * This driver handles the PWM driven LED terminals of TWL4030 and TWL6030.
 21 * To generate the signal on TWL4030:
 22 *  - LEDA uses PWMA
 23 *  - LEDB uses PWMB
 24 * TWL6030 has one LED pin with dedicated LEDPWM
 25 */
 26
 27#define TWL4030_LED_MAX		0x7f
 28#define TWL6030_LED_MAX		0xff
 29
 30/* Registers, bits and macro for TWL4030 */
 31#define TWL4030_LEDEN_REG	0x00
 32#define TWL4030_PWMA_REG	0x01
 33
 34#define TWL4030_LEDXON		(1 << 0)
 35#define TWL4030_LEDXPWM		(1 << 4)
 36#define TWL4030_LED_PINS	(TWL4030_LEDXON | TWL4030_LEDXPWM)
 37#define TWL4030_LED_TOGGLE(led, x)	((x) << (led))
 38
 39/* Register, bits and macro for TWL6030 */
 40#define TWL6030_LED_PWM_CTRL1	0xf4
 41#define TWL6030_LED_PWM_CTRL2	0xf5
 42
 43#define TWL6040_LED_MODE_HW	0x00
 44#define TWL6040_LED_MODE_ON	0x01
 45#define TWL6040_LED_MODE_OFF	0x02
 46#define TWL6040_LED_MODE_MASK	0x03
 47
 48struct twl_pwmled_chip {
 49	struct pwm_chip chip;
 50	struct mutex mutex;
 51};
 52
 53static inline struct twl_pwmled_chip *to_twl(struct pwm_chip *chip)
 54{
 55	return container_of(chip, struct twl_pwmled_chip, chip);
 56}
 57
 58static int twl4030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
 59			      int duty_ns, int period_ns)
 60{
 61	int duty_cycle = DIV_ROUND_UP(duty_ns * TWL4030_LED_MAX, period_ns) + 1;
 62	u8 pwm_config[2] = { 1, 0 };
 63	int base, ret;
 64
 65	/*
 66	 * To configure the duty period:
 67	 * On-cycle is set to 1 (the minimum allowed value)
 68	 * The off time of 0 is not configurable, so the mapping is:
 69	 * 0 -> off cycle = 2,
 70	 * 1 -> off cycle = 2,
 71	 * 2 -> off cycle = 3,
 72	 * 126 - > off cycle 127,
 73	 * 127 - > off cycle 1
 74	 * When on cycle == off cycle the PWM will be always on
 75	 */
 76	if (duty_cycle == 1)
 77		duty_cycle = 2;
 78	else if (duty_cycle > TWL4030_LED_MAX)
 79		duty_cycle = 1;
 80
 81	base = pwm->hwpwm * 2 + TWL4030_PWMA_REG;
 82
 83	pwm_config[1] = duty_cycle;
 84
 85	ret = twl_i2c_write(TWL4030_MODULE_LED, pwm_config, base, 2);
 86	if (ret < 0)
 87		dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
 88
 89	return ret;
 90}
 91
 92static int twl4030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
 93{
 94	struct twl_pwmled_chip *twl = to_twl(chip);
 95	int ret;
 96	u8 val;
 97
 98	mutex_lock(&twl->mutex);
 99	ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
100	if (ret < 0) {
101		dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
102		goto out;
103	}
104
105	val |= TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
106
107	ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
108	if (ret < 0)
109		dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
110
111out:
112	mutex_unlock(&twl->mutex);
113	return ret;
114}
115
116static void twl4030_pwmled_disable(struct pwm_chip *chip,
117				   struct pwm_device *pwm)
118{
119	struct twl_pwmled_chip *twl = to_twl(chip);
120	int ret;
121	u8 val;
122
123	mutex_lock(&twl->mutex);
124	ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
125	if (ret < 0) {
126		dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
127		goto out;
128	}
129
130	val &= ~TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
131
132	ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
133	if (ret < 0)
134		dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
135
136out:
137	mutex_unlock(&twl->mutex);
138}
139
140static int twl6030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
141			      int duty_ns, int period_ns)
142{
143	int duty_cycle = (duty_ns * TWL6030_LED_MAX) / period_ns;
144	u8 on_time;
145	int ret;
146
147	on_time = duty_cycle & 0xff;
148
149	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, on_time,
150			       TWL6030_LED_PWM_CTRL1);
151	if (ret < 0)
152		dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
153
154	return ret;
155}
156
157static int twl6030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
158{
159	struct twl_pwmled_chip *twl = to_twl(chip);
160	int ret;
161	u8 val;
162
163	mutex_lock(&twl->mutex);
164	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
165	if (ret < 0) {
166		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
167			pwm->label);
168		goto out;
169	}
170
171	val &= ~TWL6040_LED_MODE_MASK;
172	val |= TWL6040_LED_MODE_ON;
173
174	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
175	if (ret < 0)
176		dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
177
178out:
179	mutex_unlock(&twl->mutex);
180	return ret;
181}
182
183static void twl6030_pwmled_disable(struct pwm_chip *chip,
184				   struct pwm_device *pwm)
185{
186	struct twl_pwmled_chip *twl = to_twl(chip);
187	int ret;
188	u8 val;
189
190	mutex_lock(&twl->mutex);
191	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
192	if (ret < 0) {
193		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
194			pwm->label);
195		goto out;
196	}
197
198	val &= ~TWL6040_LED_MODE_MASK;
199	val |= TWL6040_LED_MODE_OFF;
200
201	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
202	if (ret < 0)
203		dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
204
205out:
206	mutex_unlock(&twl->mutex);
207}
208
209static int twl6030_pwmled_request(struct pwm_chip *chip, struct pwm_device *pwm)
210{
211	struct twl_pwmled_chip *twl = to_twl(chip);
212	int ret;
213	u8 val;
214
215	mutex_lock(&twl->mutex);
216	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
217	if (ret < 0) {
218		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
219			pwm->label);
220		goto out;
221	}
222
223	val &= ~TWL6040_LED_MODE_MASK;
224	val |= TWL6040_LED_MODE_OFF;
225
226	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
227	if (ret < 0)
228		dev_err(chip->dev, "%s: Failed to request PWM\n", pwm->label);
229
230out:
231	mutex_unlock(&twl->mutex);
232	return ret;
233}
234
235static void twl6030_pwmled_free(struct pwm_chip *chip, struct pwm_device *pwm)
236{
237	struct twl_pwmled_chip *twl = to_twl(chip);
238	int ret;
239	u8 val;
240
241	mutex_lock(&twl->mutex);
242	ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
243	if (ret < 0) {
244		dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
245			pwm->label);
246		goto out;
247	}
248
249	val &= ~TWL6040_LED_MODE_MASK;
250	val |= TWL6040_LED_MODE_HW;
251
252	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
253	if (ret < 0)
254		dev_err(chip->dev, "%s: Failed to free PWM\n", pwm->label);
255
256out:
257	mutex_unlock(&twl->mutex);
258}
259
260static const struct pwm_ops twl4030_pwmled_ops = {
261	.enable = twl4030_pwmled_enable,
262	.disable = twl4030_pwmled_disable,
263	.config = twl4030_pwmled_config,
264	.owner = THIS_MODULE,
265};
266
267static const struct pwm_ops twl6030_pwmled_ops = {
268	.enable = twl6030_pwmled_enable,
269	.disable = twl6030_pwmled_disable,
270	.config = twl6030_pwmled_config,
271	.request = twl6030_pwmled_request,
272	.free = twl6030_pwmled_free,
273	.owner = THIS_MODULE,
274};
275
276static int twl_pwmled_probe(struct platform_device *pdev)
277{
278	struct twl_pwmled_chip *twl;
279	int ret;
280
281	twl = devm_kzalloc(&pdev->dev, sizeof(*twl), GFP_KERNEL);
282	if (!twl)
283		return -ENOMEM;
284
285	if (twl_class_is_4030()) {
286		twl->chip.ops = &twl4030_pwmled_ops;
287		twl->chip.npwm = 2;
288	} else {
289		twl->chip.ops = &twl6030_pwmled_ops;
290		twl->chip.npwm = 1;
291	}
292
293	twl->chip.dev = &pdev->dev;
294	twl->chip.base = -1;
295
296	mutex_init(&twl->mutex);
297
298	ret = pwmchip_add(&twl->chip);
299	if (ret < 0)
300		return ret;
301
302	platform_set_drvdata(pdev, twl);
303
304	return 0;
305}
306
307static int twl_pwmled_remove(struct platform_device *pdev)
308{
309	struct twl_pwmled_chip *twl = platform_get_drvdata(pdev);
310
311	return pwmchip_remove(&twl->chip);
312}
313
314#ifdef CONFIG_OF
315static const struct of_device_id twl_pwmled_of_match[] = {
316	{ .compatible = "ti,twl4030-pwmled" },
317	{ .compatible = "ti,twl6030-pwmled" },
318	{ },
319};
320MODULE_DEVICE_TABLE(of, twl_pwmled_of_match);
321#endif
322
323static struct platform_driver twl_pwmled_driver = {
324	.driver = {
325		.name = "twl-pwmled",
326		.of_match_table = of_match_ptr(twl_pwmled_of_match),
327	},
328	.probe = twl_pwmled_probe,
329	.remove = twl_pwmled_remove,
330};
331module_platform_driver(twl_pwmled_driver);
332
333MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
334MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030 LED outputs");
335MODULE_ALIAS("platform:twl-pwmled");
336MODULE_LICENSE("GPL");