USB: usbsevseg: fix max length
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mfd / twl6030-pwm.c
blobe8fee147678d31e57e4d6c9d7272a998ba95029b
1 /*
2 * twl6030_pwm.c
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
15 * more details.
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)
41 /* LED modes */
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
48 struct pwm_device {
49 const char *label;
50 unsigned int pwm_id;
53 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
55 u8 duty_cycle;
56 int ret;
58 if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
59 return -EINVAL;
61 duty_cycle = (duty_ns * PWM_CTRL1_MAX) / period_ns;
63 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, duty_cycle, LED_PWM_CTRL1);
65 if (ret < 0) {
66 pr_err("%s: Failed to configure PWM, Error %d\n",
67 pwm->label, ret);
68 return ret;
70 return 0;
72 EXPORT_SYMBOL(pwm_config);
74 int pwm_enable(struct pwm_device *pwm)
76 u8 val;
77 int ret;
79 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, LED_PWM_CTRL2);
80 if (ret < 0) {
81 pr_err("%s: Failed to enable PWM, Error %d\n", pwm->label, ret);
82 return 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);
90 if (ret < 0) {
91 pr_err("%s: Failed to enable PWM, Error %d\n", pwm->label, ret);
92 return ret;
95 twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, LED_PWM_CTRL2);
96 return 0;
98 EXPORT_SYMBOL(pwm_enable);
100 void pwm_disable(struct pwm_device *pwm)
102 u8 val;
103 int ret;
105 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, LED_PWM_CTRL2);
106 if (ret < 0) {
107 pr_err("%s: Failed to disable PWM, Error %d\n",
108 pwm->label, ret);
109 return;
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);
116 if (ret < 0) {
117 pr_err("%s: Failed to disable PWM, Error %d\n",
118 pwm->label, ret);
119 return;
121 return;
123 EXPORT_SYMBOL(pwm_disable);
125 struct pwm_device *pwm_request(int pwm_id, const char *label)
127 u8 val;
128 int ret;
129 struct pwm_device *pwm;
131 pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
132 if (pwm == NULL) {
133 pr_err("%s: failed to allocate memory\n", label);
134 return NULL;
137 pwm->label = label;
138 pwm->pwm_id = pwm_id;
140 /* Configure PWM */
141 val = PWM_CTRL2_DIS_PD | PWM_CTRL2_CURR_02 | PWM_CTRL2_SRC_VAC |
142 PWM_CTRL2_MODE_HW;
144 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, LED_PWM_CTRL2);
146 if (ret < 0) {
147 pr_err("%s: Failed to configure PWM, Error %d\n",
148 pwm->label, ret);
150 kfree(pwm);
151 return NULL;
154 return pwm;
156 EXPORT_SYMBOL(pwm_request);
158 void pwm_free(struct pwm_device *pwm)
160 pwm_disable(pwm);
161 kfree(pwm);
163 EXPORT_SYMBOL(pwm_free);
165 MODULE_LICENSE("GPL");