3 * Driver for PHOENIX (TWL6030) Pulse Width Modulator
5 * Copyright (C) 2010 Texas Instruments
6 * Author: Hemanth V <hemanthv@ti.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/i2c/twl.h>
24 #include <linux/slab.h>
26 #define LED_PWM_CTRL1 0xF4
27 #define LED_PWM_CTRL2 0xF5
29 /* Max value for CTRL1 register */
30 #define PWM_CTRL1_MAX 255
32 /* Pull down disable */
33 #define PWM_CTRL2_DIS_PD (1 << 6)
35 /* Current control 2.5 milli Amps */
36 #define PWM_CTRL2_CURR_02 (2 << 4)
38 /* LED supply source */
39 #define PWM_CTRL2_SRC_VAC (1 << 2)
42 #define PWM_CTRL2_MODE_HW (0 << 0)
43 #define PWM_CTRL2_MODE_SW (1 << 0)
44 #define PWM_CTRL2_MODE_DIS (2 << 0)
46 #define PWM_CTRL2_MODE_MASK 0x3
53 int pwm_config(struct pwm_device
*pwm
, int duty_ns
, int period_ns
)
58 if (pwm
== NULL
|| period_ns
== 0 || duty_ns
> period_ns
)
61 duty_cycle
= (duty_ns
* PWM_CTRL1_MAX
) / period_ns
;
63 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, duty_cycle
, LED_PWM_CTRL1
);
66 pr_err("%s: Failed to configure PWM, Error %d\n",
72 EXPORT_SYMBOL(pwm_config
);
74 int pwm_enable(struct pwm_device
*pwm
)
79 ret
= twl_i2c_read_u8(TWL6030_MODULE_ID1
, &val
, LED_PWM_CTRL2
);
81 pr_err("%s: Failed to enable PWM, Error %d\n", pwm
->label
, ret
);
85 /* Change mode to software control */
86 val
&= ~PWM_CTRL2_MODE_MASK
;
87 val
|= PWM_CTRL2_MODE_SW
;
89 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, LED_PWM_CTRL2
);
91 pr_err("%s: Failed to enable PWM, Error %d\n", pwm
->label
, ret
);
95 twl_i2c_read_u8(TWL6030_MODULE_ID1
, &val
, LED_PWM_CTRL2
);
98 EXPORT_SYMBOL(pwm_enable
);
100 void pwm_disable(struct pwm_device
*pwm
)
105 ret
= twl_i2c_read_u8(TWL6030_MODULE_ID1
, &val
, LED_PWM_CTRL2
);
107 pr_err("%s: Failed to disable PWM, Error %d\n",
112 val
&= ~PWM_CTRL2_MODE_MASK
;
113 val
|= PWM_CTRL2_MODE_HW
;
115 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, LED_PWM_CTRL2
);
117 pr_err("%s: Failed to disable PWM, Error %d\n",
123 EXPORT_SYMBOL(pwm_disable
);
125 struct pwm_device
*pwm_request(int pwm_id
, const char *label
)
129 struct pwm_device
*pwm
;
131 pwm
= kzalloc(sizeof(struct pwm_device
), GFP_KERNEL
);
133 pr_err("%s: failed to allocate memory\n", label
);
138 pwm
->pwm_id
= pwm_id
;
141 val
= PWM_CTRL2_DIS_PD
| PWM_CTRL2_CURR_02
| PWM_CTRL2_SRC_VAC
|
144 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, LED_PWM_CTRL2
);
147 pr_err("%s: Failed to configure PWM, Error %d\n",
156 EXPORT_SYMBOL(pwm_request
);
158 void pwm_free(struct pwm_device
*pwm
)
163 EXPORT_SYMBOL(pwm_free
);