Commit fbd9df28faeda17b1a9d3e9ab976e969be98d379
Committed by
Bryan Wu
1 parent
d67eb8e66c
Exists in
smarc-l5.0.0_1.0.0-ga
and in
5 other branches
leds: tca6507: Use of_match_ptr() macro
This eliminates having an #ifdef returning NULL for the case when OF is disabled. Signed-off-by: Sachin Kamat <sachin.kamat@linaro.org> Signed-off-by: Bryan Wu <cooloney@gmail.com>
Showing 1 changed file with 2 additions and 2 deletions Inline Diff
drivers/leds/leds-tca6507.c
1 | /* | 1 | /* |
2 | * leds-tca6507 | 2 | * leds-tca6507 |
3 | * | 3 | * |
4 | * The TCA6507 is a programmable LED controller that can drive 7 | 4 | * The TCA6507 is a programmable LED controller that can drive 7 |
5 | * separate lines either by holding them low, or by pulsing them | 5 | * separate lines either by holding them low, or by pulsing them |
6 | * with modulated width. | 6 | * with modulated width. |
7 | * The modulation can be varied in a simple pattern to produce a blink or | 7 | * The modulation can be varied in a simple pattern to produce a blink or |
8 | * double-blink. | 8 | * double-blink. |
9 | * | 9 | * |
10 | * This driver can configure each line either as a 'GPIO' which is out-only | 10 | * This driver can configure each line either as a 'GPIO' which is out-only |
11 | * (no pull-up) or as an LED with variable brightness and hardware-assisted | 11 | * (no pull-up) or as an LED with variable brightness and hardware-assisted |
12 | * blinking. | 12 | * blinking. |
13 | * | 13 | * |
14 | * Apart from OFF and ON there are three programmable brightness levels which | 14 | * Apart from OFF and ON there are three programmable brightness levels which |
15 | * can be programmed from 0 to 15 and indicate how many 500usec intervals in | 15 | * can be programmed from 0 to 15 and indicate how many 500usec intervals in |
16 | * each 8msec that the led is 'on'. The levels are named MASTER, BANK0 and | 16 | * each 8msec that the led is 'on'. The levels are named MASTER, BANK0 and |
17 | * BANK1. | 17 | * BANK1. |
18 | * | 18 | * |
19 | * There are two different blink rates that can be programmed, each with | 19 | * There are two different blink rates that can be programmed, each with |
20 | * separate time for rise, on, fall, off and second-off. Thus if 3 or more | 20 | * separate time for rise, on, fall, off and second-off. Thus if 3 or more |
21 | * different non-trivial rates are required, software must be used for the extra | 21 | * different non-trivial rates are required, software must be used for the extra |
22 | * rates. The two different blink rates must align with the two levels BANK0 and | 22 | * rates. The two different blink rates must align with the two levels BANK0 and |
23 | * BANK1. | 23 | * BANK1. |
24 | * This driver does not support double-blink so 'second-off' always matches | 24 | * This driver does not support double-blink so 'second-off' always matches |
25 | * 'off'. | 25 | * 'off'. |
26 | * | 26 | * |
27 | * Only 16 different times can be programmed in a roughly logarithmic scale from | 27 | * Only 16 different times can be programmed in a roughly logarithmic scale from |
28 | * 64ms to 16320ms. To be precise the possible times are: | 28 | * 64ms to 16320ms. To be precise the possible times are: |
29 | * 0, 64, 128, 192, 256, 384, 512, 768, | 29 | * 0, 64, 128, 192, 256, 384, 512, 768, |
30 | * 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320 | 30 | * 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320 |
31 | * | 31 | * |
32 | * Times that cannot be closely matched with these must be | 32 | * Times that cannot be closely matched with these must be |
33 | * handled in software. This driver allows 12.5% error in matching. | 33 | * handled in software. This driver allows 12.5% error in matching. |
34 | * | 34 | * |
35 | * This driver does not allow rise/fall rates to be set explicitly. When trying | 35 | * This driver does not allow rise/fall rates to be set explicitly. When trying |
36 | * to match a given 'on' or 'off' period, an appropriate pair of 'change' and | 36 | * to match a given 'on' or 'off' period, an appropriate pair of 'change' and |
37 | * 'hold' times are chosen to get a close match. If the target delay is even, | 37 | * 'hold' times are chosen to get a close match. If the target delay is even, |
38 | * the 'change' number will be the smaller; if odd, the 'hold' number will be | 38 | * the 'change' number will be the smaller; if odd, the 'hold' number will be |
39 | * the smaller. | 39 | * the smaller. |
40 | 40 | ||
41 | * Choosing pairs of delays with 12.5% errors allows us to match delays in the | 41 | * Choosing pairs of delays with 12.5% errors allows us to match delays in the |
42 | * ranges: 56-72, 112-144, 168-216, 224-27504, 28560-36720. | 42 | * ranges: 56-72, 112-144, 168-216, 224-27504, 28560-36720. |
43 | * 26% of the achievable sums can be matched by multiple pairings. For example | 43 | * 26% of the achievable sums can be matched by multiple pairings. For example |
44 | * 1536 == 1536+0, 1024+512, or 768+768. This driver will always choose the | 44 | * 1536 == 1536+0, 1024+512, or 768+768. This driver will always choose the |
45 | * pairing with the least maximum - 768+768 in this case. Other pairings are | 45 | * pairing with the least maximum - 768+768 in this case. Other pairings are |
46 | * not available. | 46 | * not available. |
47 | * | 47 | * |
48 | * Access to the 3 levels and 2 blinks are on a first-come, first-served basis. | 48 | * Access to the 3 levels and 2 blinks are on a first-come, first-served basis. |
49 | * Access can be shared by multiple leds if they have the same level and | 49 | * Access can be shared by multiple leds if they have the same level and |
50 | * either same blink rates, or some don't blink. | 50 | * either same blink rates, or some don't blink. |
51 | * When a led changes, it relinquishes access and tries again, so it might | 51 | * When a led changes, it relinquishes access and tries again, so it might |
52 | * lose access to hardware blink. | 52 | * lose access to hardware blink. |
53 | * If a blink engine cannot be allocated, software blink is used. | 53 | * If a blink engine cannot be allocated, software blink is used. |
54 | * If the desired brightness cannot be allocated, the closest available non-zero | 54 | * If the desired brightness cannot be allocated, the closest available non-zero |
55 | * brightness is used. As 'full' is always available, the worst case would be | 55 | * brightness is used. As 'full' is always available, the worst case would be |
56 | * to have two different blink rates at '1', with Max at '2', then other leds | 56 | * to have two different blink rates at '1', with Max at '2', then other leds |
57 | * will have to choose between '2' and '16'. Hopefully this is not likely. | 57 | * will have to choose between '2' and '16'. Hopefully this is not likely. |
58 | * | 58 | * |
59 | * Each bank (BANK0 and BANK1) has two usage counts - LEDs using the brightness | 59 | * Each bank (BANK0 and BANK1) has two usage counts - LEDs using the brightness |
60 | * and LEDs using the blink. It can only be reprogrammed when the appropriate | 60 | * and LEDs using the blink. It can only be reprogrammed when the appropriate |
61 | * counter is zero. The MASTER level has a single usage count. | 61 | * counter is zero. The MASTER level has a single usage count. |
62 | * | 62 | * |
63 | * Each Led has programmable 'on' and 'off' time as milliseconds. With each | 63 | * Each Led has programmable 'on' and 'off' time as milliseconds. With each |
64 | * there is a flag saying if it was explicitly requested or defaulted. | 64 | * there is a flag saying if it was explicitly requested or defaulted. |
65 | * Similarly the banks know if each time was explicit or a default. Defaults | 65 | * Similarly the banks know if each time was explicit or a default. Defaults |
66 | * are permitted to be changed freely - they are not recognised when matching. | 66 | * are permitted to be changed freely - they are not recognised when matching. |
67 | * | 67 | * |
68 | * | 68 | * |
69 | * An led-tca6507 device must be provided with platform data. This data | 69 | * An led-tca6507 device must be provided with platform data. This data |
70 | * lists for each output: the name, default trigger, and whether the signal | 70 | * lists for each output: the name, default trigger, and whether the signal |
71 | * is being used as a GPiO rather than an led. 'struct led_plaform_data' | 71 | * is being used as a GPiO rather than an led. 'struct led_plaform_data' |
72 | * is used for this. If 'name' is NULL, the output isn't used. If 'flags' | 72 | * is used for this. If 'name' is NULL, the output isn't used. If 'flags' |
73 | * is TCA6507_MAKE_CPIO, the output is a GPO. | 73 | * is TCA6507_MAKE_CPIO, the output is a GPO. |
74 | * The "struct led_platform_data" can be embedded in a | 74 | * The "struct led_platform_data" can be embedded in a |
75 | * "struct tca6507_platform_data" which adds a 'gpio_base' for the GPiOs, | 75 | * "struct tca6507_platform_data" which adds a 'gpio_base' for the GPiOs, |
76 | * and a 'setup' callback which is called once the GPiOs are available. | 76 | * and a 'setup' callback which is called once the GPiOs are available. |
77 | * | 77 | * |
78 | */ | 78 | */ |
79 | 79 | ||
80 | #include <linux/module.h> | 80 | #include <linux/module.h> |
81 | #include <linux/slab.h> | 81 | #include <linux/slab.h> |
82 | #include <linux/leds.h> | 82 | #include <linux/leds.h> |
83 | #include <linux/err.h> | 83 | #include <linux/err.h> |
84 | #include <linux/i2c.h> | 84 | #include <linux/i2c.h> |
85 | #include <linux/gpio.h> | 85 | #include <linux/gpio.h> |
86 | #include <linux/workqueue.h> | 86 | #include <linux/workqueue.h> |
87 | #include <linux/leds-tca6507.h> | 87 | #include <linux/leds-tca6507.h> |
88 | #include <linux/of.h> | ||
88 | 89 | ||
89 | /* LED select registers determine the source that drives LED outputs */ | 90 | /* LED select registers determine the source that drives LED outputs */ |
90 | #define TCA6507_LS_LED_OFF 0x0 /* Output HI-Z (off) */ | 91 | #define TCA6507_LS_LED_OFF 0x0 /* Output HI-Z (off) */ |
91 | #define TCA6507_LS_LED_OFF1 0x1 /* Output HI-Z (off) - not used */ | 92 | #define TCA6507_LS_LED_OFF1 0x1 /* Output HI-Z (off) - not used */ |
92 | #define TCA6507_LS_LED_PWM0 0x2 /* Output LOW with Bank0 rate */ | 93 | #define TCA6507_LS_LED_PWM0 0x2 /* Output LOW with Bank0 rate */ |
93 | #define TCA6507_LS_LED_PWM1 0x3 /* Output LOW with Bank1 rate */ | 94 | #define TCA6507_LS_LED_PWM1 0x3 /* Output LOW with Bank1 rate */ |
94 | #define TCA6507_LS_LED_ON 0x4 /* Output LOW (on) */ | 95 | #define TCA6507_LS_LED_ON 0x4 /* Output LOW (on) */ |
95 | #define TCA6507_LS_LED_MIR 0x5 /* Output LOW with Master Intensity */ | 96 | #define TCA6507_LS_LED_MIR 0x5 /* Output LOW with Master Intensity */ |
96 | #define TCA6507_LS_BLINK0 0x6 /* Blink at Bank0 rate */ | 97 | #define TCA6507_LS_BLINK0 0x6 /* Blink at Bank0 rate */ |
97 | #define TCA6507_LS_BLINK1 0x7 /* Blink at Bank1 rate */ | 98 | #define TCA6507_LS_BLINK1 0x7 /* Blink at Bank1 rate */ |
98 | 99 | ||
99 | enum { | 100 | enum { |
100 | BANK0, | 101 | BANK0, |
101 | BANK1, | 102 | BANK1, |
102 | MASTER, | 103 | MASTER, |
103 | }; | 104 | }; |
104 | static int bank_source[3] = { | 105 | static int bank_source[3] = { |
105 | TCA6507_LS_LED_PWM0, | 106 | TCA6507_LS_LED_PWM0, |
106 | TCA6507_LS_LED_PWM1, | 107 | TCA6507_LS_LED_PWM1, |
107 | TCA6507_LS_LED_MIR, | 108 | TCA6507_LS_LED_MIR, |
108 | }; | 109 | }; |
109 | static int blink_source[2] = { | 110 | static int blink_source[2] = { |
110 | TCA6507_LS_BLINK0, | 111 | TCA6507_LS_BLINK0, |
111 | TCA6507_LS_BLINK1, | 112 | TCA6507_LS_BLINK1, |
112 | }; | 113 | }; |
113 | 114 | ||
114 | /* PWM registers */ | 115 | /* PWM registers */ |
115 | #define TCA6507_REG_CNT 11 | 116 | #define TCA6507_REG_CNT 11 |
116 | 117 | ||
117 | /* | 118 | /* |
118 | * 0x00, 0x01, 0x02 encode the TCA6507_LS_* values, each output | 119 | * 0x00, 0x01, 0x02 encode the TCA6507_LS_* values, each output |
119 | * owns one bit in each register | 120 | * owns one bit in each register |
120 | */ | 121 | */ |
121 | #define TCA6507_FADE_ON 0x03 | 122 | #define TCA6507_FADE_ON 0x03 |
122 | #define TCA6507_FULL_ON 0x04 | 123 | #define TCA6507_FULL_ON 0x04 |
123 | #define TCA6507_FADE_OFF 0x05 | 124 | #define TCA6507_FADE_OFF 0x05 |
124 | #define TCA6507_FIRST_OFF 0x06 | 125 | #define TCA6507_FIRST_OFF 0x06 |
125 | #define TCA6507_SECOND_OFF 0x07 | 126 | #define TCA6507_SECOND_OFF 0x07 |
126 | #define TCA6507_MAX_INTENSITY 0x08 | 127 | #define TCA6507_MAX_INTENSITY 0x08 |
127 | #define TCA6507_MASTER_INTENSITY 0x09 | 128 | #define TCA6507_MASTER_INTENSITY 0x09 |
128 | #define TCA6507_INITIALIZE 0x0A | 129 | #define TCA6507_INITIALIZE 0x0A |
129 | 130 | ||
130 | #define INIT_CODE 0x8 | 131 | #define INIT_CODE 0x8 |
131 | 132 | ||
132 | #define TIMECODES 16 | 133 | #define TIMECODES 16 |
133 | static int time_codes[TIMECODES] = { | 134 | static int time_codes[TIMECODES] = { |
134 | 0, 64, 128, 192, 256, 384, 512, 768, | 135 | 0, 64, 128, 192, 256, 384, 512, 768, |
135 | 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320 | 136 | 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320 |
136 | }; | 137 | }; |
137 | 138 | ||
138 | /* Convert an led.brightness level (0..255) to a TCA6507 level (0..15) */ | 139 | /* Convert an led.brightness level (0..255) to a TCA6507 level (0..15) */ |
139 | static inline int TO_LEVEL(int brightness) | 140 | static inline int TO_LEVEL(int brightness) |
140 | { | 141 | { |
141 | return brightness >> 4; | 142 | return brightness >> 4; |
142 | } | 143 | } |
143 | 144 | ||
144 | /* ...and convert back */ | 145 | /* ...and convert back */ |
145 | static inline int TO_BRIGHT(int level) | 146 | static inline int TO_BRIGHT(int level) |
146 | { | 147 | { |
147 | if (level) | 148 | if (level) |
148 | return (level << 4) | 0xf; | 149 | return (level << 4) | 0xf; |
149 | return 0; | 150 | return 0; |
150 | } | 151 | } |
151 | 152 | ||
152 | #define NUM_LEDS 7 | 153 | #define NUM_LEDS 7 |
153 | struct tca6507_chip { | 154 | struct tca6507_chip { |
154 | int reg_set; /* One bit per register where | 155 | int reg_set; /* One bit per register where |
155 | * a '1' means the register | 156 | * a '1' means the register |
156 | * should be written */ | 157 | * should be written */ |
157 | u8 reg_file[TCA6507_REG_CNT]; | 158 | u8 reg_file[TCA6507_REG_CNT]; |
158 | /* Bank 2 is Master Intensity and doesn't use times */ | 159 | /* Bank 2 is Master Intensity and doesn't use times */ |
159 | struct bank { | 160 | struct bank { |
160 | int level; | 161 | int level; |
161 | int ontime, offtime; | 162 | int ontime, offtime; |
162 | int on_dflt, off_dflt; | 163 | int on_dflt, off_dflt; |
163 | int time_use, level_use; | 164 | int time_use, level_use; |
164 | } bank[3]; | 165 | } bank[3]; |
165 | struct i2c_client *client; | 166 | struct i2c_client *client; |
166 | struct work_struct work; | 167 | struct work_struct work; |
167 | spinlock_t lock; | 168 | spinlock_t lock; |
168 | 169 | ||
169 | struct tca6507_led { | 170 | struct tca6507_led { |
170 | struct tca6507_chip *chip; | 171 | struct tca6507_chip *chip; |
171 | struct led_classdev led_cdev; | 172 | struct led_classdev led_cdev; |
172 | int num; | 173 | int num; |
173 | int ontime, offtime; | 174 | int ontime, offtime; |
174 | int on_dflt, off_dflt; | 175 | int on_dflt, off_dflt; |
175 | int bank; /* Bank used, or -1 */ | 176 | int bank; /* Bank used, or -1 */ |
176 | int blink; /* Set if hardware-blinking */ | 177 | int blink; /* Set if hardware-blinking */ |
177 | } leds[NUM_LEDS]; | 178 | } leds[NUM_LEDS]; |
178 | #ifdef CONFIG_GPIOLIB | 179 | #ifdef CONFIG_GPIOLIB |
179 | struct gpio_chip gpio; | 180 | struct gpio_chip gpio; |
180 | const char *gpio_name[NUM_LEDS]; | 181 | const char *gpio_name[NUM_LEDS]; |
181 | int gpio_map[NUM_LEDS]; | 182 | int gpio_map[NUM_LEDS]; |
182 | #endif | 183 | #endif |
183 | }; | 184 | }; |
184 | 185 | ||
185 | static const struct i2c_device_id tca6507_id[] = { | 186 | static const struct i2c_device_id tca6507_id[] = { |
186 | { "tca6507" }, | 187 | { "tca6507" }, |
187 | { } | 188 | { } |
188 | }; | 189 | }; |
189 | MODULE_DEVICE_TABLE(i2c, tca6507_id); | 190 | MODULE_DEVICE_TABLE(i2c, tca6507_id); |
190 | 191 | ||
191 | static int choose_times(int msec, int *c1p, int *c2p) | 192 | static int choose_times(int msec, int *c1p, int *c2p) |
192 | { | 193 | { |
193 | /* | 194 | /* |
194 | * Choose two timecodes which add to 'msec' as near as possible. | 195 | * Choose two timecodes which add to 'msec' as near as possible. |
195 | * The first returned is the 'on' or 'off' time. The second is to be | 196 | * The first returned is the 'on' or 'off' time. The second is to be |
196 | * used as a 'fade-on' or 'fade-off' time. If 'msec' is even, | 197 | * used as a 'fade-on' or 'fade-off' time. If 'msec' is even, |
197 | * the first will not be smaller than the second. If 'msec' is odd, | 198 | * the first will not be smaller than the second. If 'msec' is odd, |
198 | * the first will not be larger than the second. | 199 | * the first will not be larger than the second. |
199 | * If we cannot get a sum within 1/8 of 'msec' fail with -EINVAL, | 200 | * If we cannot get a sum within 1/8 of 'msec' fail with -EINVAL, |
200 | * otherwise return the sum that was achieved, plus 1 if the first is | 201 | * otherwise return the sum that was achieved, plus 1 if the first is |
201 | * smaller. | 202 | * smaller. |
202 | * If two possibilities are equally good (e.g. 512+0, 256+256), choose | 203 | * If two possibilities are equally good (e.g. 512+0, 256+256), choose |
203 | * the first pair so there is more change-time visible (i.e. it is | 204 | * the first pair so there is more change-time visible (i.e. it is |
204 | * softer). | 205 | * softer). |
205 | */ | 206 | */ |
206 | int c1, c2; | 207 | int c1, c2; |
207 | int tmax = msec * 9 / 8; | 208 | int tmax = msec * 9 / 8; |
208 | int tmin = msec * 7 / 8; | 209 | int tmin = msec * 7 / 8; |
209 | int diff = 65536; | 210 | int diff = 65536; |
210 | 211 | ||
211 | /* We start at '1' to ensure we never even think of choosing a | 212 | /* We start at '1' to ensure we never even think of choosing a |
212 | * total time of '0'. | 213 | * total time of '0'. |
213 | */ | 214 | */ |
214 | for (c1 = 1; c1 < TIMECODES; c1++) { | 215 | for (c1 = 1; c1 < TIMECODES; c1++) { |
215 | int t = time_codes[c1]; | 216 | int t = time_codes[c1]; |
216 | if (t*2 < tmin) | 217 | if (t*2 < tmin) |
217 | continue; | 218 | continue; |
218 | if (t > tmax) | 219 | if (t > tmax) |
219 | break; | 220 | break; |
220 | for (c2 = 0; c2 <= c1; c2++) { | 221 | for (c2 = 0; c2 <= c1; c2++) { |
221 | int tt = t + time_codes[c2]; | 222 | int tt = t + time_codes[c2]; |
222 | int d; | 223 | int d; |
223 | if (tt < tmin) | 224 | if (tt < tmin) |
224 | continue; | 225 | continue; |
225 | if (tt > tmax) | 226 | if (tt > tmax) |
226 | break; | 227 | break; |
227 | /* This works! */ | 228 | /* This works! */ |
228 | d = abs(msec - tt); | 229 | d = abs(msec - tt); |
229 | if (d >= diff) | 230 | if (d >= diff) |
230 | continue; | 231 | continue; |
231 | /* Best yet */ | 232 | /* Best yet */ |
232 | *c1p = c1; | 233 | *c1p = c1; |
233 | *c2p = c2; | 234 | *c2p = c2; |
234 | diff = d; | 235 | diff = d; |
235 | if (d == 0) | 236 | if (d == 0) |
236 | return msec; | 237 | return msec; |
237 | } | 238 | } |
238 | } | 239 | } |
239 | if (diff < 65536) { | 240 | if (diff < 65536) { |
240 | int actual; | 241 | int actual; |
241 | if (msec & 1) { | 242 | if (msec & 1) { |
242 | c1 = *c2p; | 243 | c1 = *c2p; |
243 | *c2p = *c1p; | 244 | *c2p = *c1p; |
244 | *c1p = c1; | 245 | *c1p = c1; |
245 | } | 246 | } |
246 | actual = time_codes[*c1p] + time_codes[*c2p]; | 247 | actual = time_codes[*c1p] + time_codes[*c2p]; |
247 | if (*c1p < *c2p) | 248 | if (*c1p < *c2p) |
248 | return actual + 1; | 249 | return actual + 1; |
249 | else | 250 | else |
250 | return actual; | 251 | return actual; |
251 | } | 252 | } |
252 | /* No close match */ | 253 | /* No close match */ |
253 | return -EINVAL; | 254 | return -EINVAL; |
254 | } | 255 | } |
255 | 256 | ||
256 | /* | 257 | /* |
257 | * Update the register file with the appropriate 3-bit state for | 258 | * Update the register file with the appropriate 3-bit state for |
258 | * the given led. | 259 | * the given led. |
259 | */ | 260 | */ |
260 | static void set_select(struct tca6507_chip *tca, int led, int val) | 261 | static void set_select(struct tca6507_chip *tca, int led, int val) |
261 | { | 262 | { |
262 | int mask = (1 << led); | 263 | int mask = (1 << led); |
263 | int bit; | 264 | int bit; |
264 | 265 | ||
265 | for (bit = 0; bit < 3; bit++) { | 266 | for (bit = 0; bit < 3; bit++) { |
266 | int n = tca->reg_file[bit] & ~mask; | 267 | int n = tca->reg_file[bit] & ~mask; |
267 | if (val & (1 << bit)) | 268 | if (val & (1 << bit)) |
268 | n |= mask; | 269 | n |= mask; |
269 | if (tca->reg_file[bit] != n) { | 270 | if (tca->reg_file[bit] != n) { |
270 | tca->reg_file[bit] = n; | 271 | tca->reg_file[bit] = n; |
271 | tca->reg_set |= (1 << bit); | 272 | tca->reg_set |= (1 << bit); |
272 | } | 273 | } |
273 | } | 274 | } |
274 | } | 275 | } |
275 | 276 | ||
276 | /* Update the register file with the appropriate 4-bit code for | 277 | /* Update the register file with the appropriate 4-bit code for |
277 | * one bank or other. This can be used for timers, for levels, or | 278 | * one bank or other. This can be used for timers, for levels, or |
278 | * for initialisation. | 279 | * for initialisation. |
279 | */ | 280 | */ |
280 | static void set_code(struct tca6507_chip *tca, int reg, int bank, int new) | 281 | static void set_code(struct tca6507_chip *tca, int reg, int bank, int new) |
281 | { | 282 | { |
282 | int mask = 0xF; | 283 | int mask = 0xF; |
283 | int n; | 284 | int n; |
284 | if (bank) { | 285 | if (bank) { |
285 | mask <<= 4; | 286 | mask <<= 4; |
286 | new <<= 4; | 287 | new <<= 4; |
287 | } | 288 | } |
288 | n = tca->reg_file[reg] & ~mask; | 289 | n = tca->reg_file[reg] & ~mask; |
289 | n |= new; | 290 | n |= new; |
290 | if (tca->reg_file[reg] != n) { | 291 | if (tca->reg_file[reg] != n) { |
291 | tca->reg_file[reg] = n; | 292 | tca->reg_file[reg] = n; |
292 | tca->reg_set |= 1 << reg; | 293 | tca->reg_set |= 1 << reg; |
293 | } | 294 | } |
294 | } | 295 | } |
295 | 296 | ||
296 | /* Update brightness level. */ | 297 | /* Update brightness level. */ |
297 | static void set_level(struct tca6507_chip *tca, int bank, int level) | 298 | static void set_level(struct tca6507_chip *tca, int bank, int level) |
298 | { | 299 | { |
299 | switch (bank) { | 300 | switch (bank) { |
300 | case BANK0: | 301 | case BANK0: |
301 | case BANK1: | 302 | case BANK1: |
302 | set_code(tca, TCA6507_MAX_INTENSITY, bank, level); | 303 | set_code(tca, TCA6507_MAX_INTENSITY, bank, level); |
303 | break; | 304 | break; |
304 | case MASTER: | 305 | case MASTER: |
305 | set_code(tca, TCA6507_MASTER_INTENSITY, 0, level); | 306 | set_code(tca, TCA6507_MASTER_INTENSITY, 0, level); |
306 | break; | 307 | break; |
307 | } | 308 | } |
308 | tca->bank[bank].level = level; | 309 | tca->bank[bank].level = level; |
309 | } | 310 | } |
310 | 311 | ||
311 | /* Record all relevant time code for a given bank */ | 312 | /* Record all relevant time code for a given bank */ |
312 | static void set_times(struct tca6507_chip *tca, int bank) | 313 | static void set_times(struct tca6507_chip *tca, int bank) |
313 | { | 314 | { |
314 | int c1, c2; | 315 | int c1, c2; |
315 | int result; | 316 | int result; |
316 | 317 | ||
317 | result = choose_times(tca->bank[bank].ontime, &c1, &c2); | 318 | result = choose_times(tca->bank[bank].ontime, &c1, &c2); |
318 | dev_dbg(&tca->client->dev, | 319 | dev_dbg(&tca->client->dev, |
319 | "Chose on times %d(%d) %d(%d) for %dms\n", c1, time_codes[c1], | 320 | "Chose on times %d(%d) %d(%d) for %dms\n", c1, time_codes[c1], |
320 | c2, time_codes[c2], tca->bank[bank].ontime); | 321 | c2, time_codes[c2], tca->bank[bank].ontime); |
321 | set_code(tca, TCA6507_FADE_ON, bank, c2); | 322 | set_code(tca, TCA6507_FADE_ON, bank, c2); |
322 | set_code(tca, TCA6507_FULL_ON, bank, c1); | 323 | set_code(tca, TCA6507_FULL_ON, bank, c1); |
323 | tca->bank[bank].ontime = result; | 324 | tca->bank[bank].ontime = result; |
324 | 325 | ||
325 | result = choose_times(tca->bank[bank].offtime, &c1, &c2); | 326 | result = choose_times(tca->bank[bank].offtime, &c1, &c2); |
326 | dev_dbg(&tca->client->dev, | 327 | dev_dbg(&tca->client->dev, |
327 | "Chose off times %d(%d) %d(%d) for %dms\n", c1, time_codes[c1], | 328 | "Chose off times %d(%d) %d(%d) for %dms\n", c1, time_codes[c1], |
328 | c2, time_codes[c2], tca->bank[bank].offtime); | 329 | c2, time_codes[c2], tca->bank[bank].offtime); |
329 | set_code(tca, TCA6507_FADE_OFF, bank, c2); | 330 | set_code(tca, TCA6507_FADE_OFF, bank, c2); |
330 | set_code(tca, TCA6507_FIRST_OFF, bank, c1); | 331 | set_code(tca, TCA6507_FIRST_OFF, bank, c1); |
331 | set_code(tca, TCA6507_SECOND_OFF, bank, c1); | 332 | set_code(tca, TCA6507_SECOND_OFF, bank, c1); |
332 | tca->bank[bank].offtime = result; | 333 | tca->bank[bank].offtime = result; |
333 | 334 | ||
334 | set_code(tca, TCA6507_INITIALIZE, bank, INIT_CODE); | 335 | set_code(tca, TCA6507_INITIALIZE, bank, INIT_CODE); |
335 | } | 336 | } |
336 | 337 | ||
337 | /* Write all needed register of tca6507 */ | 338 | /* Write all needed register of tca6507 */ |
338 | 339 | ||
339 | static void tca6507_work(struct work_struct *work) | 340 | static void tca6507_work(struct work_struct *work) |
340 | { | 341 | { |
341 | struct tca6507_chip *tca = container_of(work, struct tca6507_chip, | 342 | struct tca6507_chip *tca = container_of(work, struct tca6507_chip, |
342 | work); | 343 | work); |
343 | struct i2c_client *cl = tca->client; | 344 | struct i2c_client *cl = tca->client; |
344 | int set; | 345 | int set; |
345 | u8 file[TCA6507_REG_CNT]; | 346 | u8 file[TCA6507_REG_CNT]; |
346 | int r; | 347 | int r; |
347 | 348 | ||
348 | spin_lock_irq(&tca->lock); | 349 | spin_lock_irq(&tca->lock); |
349 | set = tca->reg_set; | 350 | set = tca->reg_set; |
350 | memcpy(file, tca->reg_file, TCA6507_REG_CNT); | 351 | memcpy(file, tca->reg_file, TCA6507_REG_CNT); |
351 | tca->reg_set = 0; | 352 | tca->reg_set = 0; |
352 | spin_unlock_irq(&tca->lock); | 353 | spin_unlock_irq(&tca->lock); |
353 | 354 | ||
354 | for (r = 0; r < TCA6507_REG_CNT; r++) | 355 | for (r = 0; r < TCA6507_REG_CNT; r++) |
355 | if (set & (1<<r)) | 356 | if (set & (1<<r)) |
356 | i2c_smbus_write_byte_data(cl, r, file[r]); | 357 | i2c_smbus_write_byte_data(cl, r, file[r]); |
357 | } | 358 | } |
358 | 359 | ||
359 | static void led_release(struct tca6507_led *led) | 360 | static void led_release(struct tca6507_led *led) |
360 | { | 361 | { |
361 | /* If led owns any resource, release it. */ | 362 | /* If led owns any resource, release it. */ |
362 | struct tca6507_chip *tca = led->chip; | 363 | struct tca6507_chip *tca = led->chip; |
363 | if (led->bank >= 0) { | 364 | if (led->bank >= 0) { |
364 | struct bank *b = tca->bank + led->bank; | 365 | struct bank *b = tca->bank + led->bank; |
365 | if (led->blink) | 366 | if (led->blink) |
366 | b->time_use--; | 367 | b->time_use--; |
367 | b->level_use--; | 368 | b->level_use--; |
368 | } | 369 | } |
369 | led->blink = 0; | 370 | led->blink = 0; |
370 | led->bank = -1; | 371 | led->bank = -1; |
371 | } | 372 | } |
372 | 373 | ||
373 | static int led_prepare(struct tca6507_led *led) | 374 | static int led_prepare(struct tca6507_led *led) |
374 | { | 375 | { |
375 | /* Assign this led to a bank, configuring that bank if necessary. */ | 376 | /* Assign this led to a bank, configuring that bank if necessary. */ |
376 | int level = TO_LEVEL(led->led_cdev.brightness); | 377 | int level = TO_LEVEL(led->led_cdev.brightness); |
377 | struct tca6507_chip *tca = led->chip; | 378 | struct tca6507_chip *tca = led->chip; |
378 | int c1, c2; | 379 | int c1, c2; |
379 | int i; | 380 | int i; |
380 | struct bank *b; | 381 | struct bank *b; |
381 | int need_init = 0; | 382 | int need_init = 0; |
382 | 383 | ||
383 | led->led_cdev.brightness = TO_BRIGHT(level); | 384 | led->led_cdev.brightness = TO_BRIGHT(level); |
384 | if (level == 0) { | 385 | if (level == 0) { |
385 | set_select(tca, led->num, TCA6507_LS_LED_OFF); | 386 | set_select(tca, led->num, TCA6507_LS_LED_OFF); |
386 | return 0; | 387 | return 0; |
387 | } | 388 | } |
388 | 389 | ||
389 | if (led->ontime == 0 || led->offtime == 0) { | 390 | if (led->ontime == 0 || led->offtime == 0) { |
390 | /* | 391 | /* |
391 | * Just set the brightness, choosing first usable bank. | 392 | * Just set the brightness, choosing first usable bank. |
392 | * If none perfect, choose best. | 393 | * If none perfect, choose best. |
393 | * Count backwards so we check MASTER bank first | 394 | * Count backwards so we check MASTER bank first |
394 | * to avoid wasting a timer. | 395 | * to avoid wasting a timer. |
395 | */ | 396 | */ |
396 | int best = -1;/* full-on */ | 397 | int best = -1;/* full-on */ |
397 | int diff = 15-level; | 398 | int diff = 15-level; |
398 | 399 | ||
399 | if (level == 15) { | 400 | if (level == 15) { |
400 | set_select(tca, led->num, TCA6507_LS_LED_ON); | 401 | set_select(tca, led->num, TCA6507_LS_LED_ON); |
401 | return 0; | 402 | return 0; |
402 | } | 403 | } |
403 | 404 | ||
404 | for (i = MASTER; i >= BANK0; i--) { | 405 | for (i = MASTER; i >= BANK0; i--) { |
405 | int d; | 406 | int d; |
406 | if (tca->bank[i].level == level || | 407 | if (tca->bank[i].level == level || |
407 | tca->bank[i].level_use == 0) { | 408 | tca->bank[i].level_use == 0) { |
408 | best = i; | 409 | best = i; |
409 | break; | 410 | break; |
410 | } | 411 | } |
411 | d = abs(level - tca->bank[i].level); | 412 | d = abs(level - tca->bank[i].level); |
412 | if (d < diff) { | 413 | if (d < diff) { |
413 | diff = d; | 414 | diff = d; |
414 | best = i; | 415 | best = i; |
415 | } | 416 | } |
416 | } | 417 | } |
417 | if (best == -1) { | 418 | if (best == -1) { |
418 | /* Best brightness is full-on */ | 419 | /* Best brightness is full-on */ |
419 | set_select(tca, led->num, TCA6507_LS_LED_ON); | 420 | set_select(tca, led->num, TCA6507_LS_LED_ON); |
420 | led->led_cdev.brightness = LED_FULL; | 421 | led->led_cdev.brightness = LED_FULL; |
421 | return 0; | 422 | return 0; |
422 | } | 423 | } |
423 | 424 | ||
424 | if (!tca->bank[best].level_use) | 425 | if (!tca->bank[best].level_use) |
425 | set_level(tca, best, level); | 426 | set_level(tca, best, level); |
426 | 427 | ||
427 | tca->bank[best].level_use++; | 428 | tca->bank[best].level_use++; |
428 | led->bank = best; | 429 | led->bank = best; |
429 | set_select(tca, led->num, bank_source[best]); | 430 | set_select(tca, led->num, bank_source[best]); |
430 | led->led_cdev.brightness = TO_BRIGHT(tca->bank[best].level); | 431 | led->led_cdev.brightness = TO_BRIGHT(tca->bank[best].level); |
431 | return 0; | 432 | return 0; |
432 | } | 433 | } |
433 | 434 | ||
434 | /* | 435 | /* |
435 | * We have on/off time so we need to try to allocate a timing bank. | 436 | * We have on/off time so we need to try to allocate a timing bank. |
436 | * First check if times are compatible with hardware and give up if | 437 | * First check if times are compatible with hardware and give up if |
437 | * not. | 438 | * not. |
438 | */ | 439 | */ |
439 | if (choose_times(led->ontime, &c1, &c2) < 0) | 440 | if (choose_times(led->ontime, &c1, &c2) < 0) |
440 | return -EINVAL; | 441 | return -EINVAL; |
441 | if (choose_times(led->offtime, &c1, &c2) < 0) | 442 | if (choose_times(led->offtime, &c1, &c2) < 0) |
442 | return -EINVAL; | 443 | return -EINVAL; |
443 | 444 | ||
444 | for (i = BANK0; i <= BANK1; i++) { | 445 | for (i = BANK0; i <= BANK1; i++) { |
445 | if (tca->bank[i].level_use == 0) | 446 | if (tca->bank[i].level_use == 0) |
446 | /* not in use - it is ours! */ | 447 | /* not in use - it is ours! */ |
447 | break; | 448 | break; |
448 | if (tca->bank[i].level != level) | 449 | if (tca->bank[i].level != level) |
449 | /* Incompatible level - skip */ | 450 | /* Incompatible level - skip */ |
450 | /* FIX: if timer matches we maybe should consider | 451 | /* FIX: if timer matches we maybe should consider |
451 | * this anyway... | 452 | * this anyway... |
452 | */ | 453 | */ |
453 | continue; | 454 | continue; |
454 | 455 | ||
455 | if (tca->bank[i].time_use == 0) | 456 | if (tca->bank[i].time_use == 0) |
456 | /* Timer not in use, and level matches - use it */ | 457 | /* Timer not in use, and level matches - use it */ |
457 | break; | 458 | break; |
458 | 459 | ||
459 | if (!(tca->bank[i].on_dflt || | 460 | if (!(tca->bank[i].on_dflt || |
460 | led->on_dflt || | 461 | led->on_dflt || |
461 | tca->bank[i].ontime == led->ontime)) | 462 | tca->bank[i].ontime == led->ontime)) |
462 | /* on time is incompatible */ | 463 | /* on time is incompatible */ |
463 | continue; | 464 | continue; |
464 | 465 | ||
465 | if (!(tca->bank[i].off_dflt || | 466 | if (!(tca->bank[i].off_dflt || |
466 | led->off_dflt || | 467 | led->off_dflt || |
467 | tca->bank[i].offtime == led->offtime)) | 468 | tca->bank[i].offtime == led->offtime)) |
468 | /* off time is incompatible */ | 469 | /* off time is incompatible */ |
469 | continue; | 470 | continue; |
470 | 471 | ||
471 | /* looks like a suitable match */ | 472 | /* looks like a suitable match */ |
472 | break; | 473 | break; |
473 | } | 474 | } |
474 | 475 | ||
475 | if (i > BANK1) | 476 | if (i > BANK1) |
476 | /* Nothing matches - how sad */ | 477 | /* Nothing matches - how sad */ |
477 | return -EINVAL; | 478 | return -EINVAL; |
478 | 479 | ||
479 | b = &tca->bank[i]; | 480 | b = &tca->bank[i]; |
480 | if (b->level_use == 0) | 481 | if (b->level_use == 0) |
481 | set_level(tca, i, level); | 482 | set_level(tca, i, level); |
482 | b->level_use++; | 483 | b->level_use++; |
483 | led->bank = i; | 484 | led->bank = i; |
484 | 485 | ||
485 | if (b->on_dflt || | 486 | if (b->on_dflt || |
486 | !led->on_dflt || | 487 | !led->on_dflt || |
487 | b->time_use == 0) { | 488 | b->time_use == 0) { |
488 | b->ontime = led->ontime; | 489 | b->ontime = led->ontime; |
489 | b->on_dflt = led->on_dflt; | 490 | b->on_dflt = led->on_dflt; |
490 | need_init = 1; | 491 | need_init = 1; |
491 | } | 492 | } |
492 | 493 | ||
493 | if (b->off_dflt || | 494 | if (b->off_dflt || |
494 | !led->off_dflt || | 495 | !led->off_dflt || |
495 | b->time_use == 0) { | 496 | b->time_use == 0) { |
496 | b->offtime = led->offtime; | 497 | b->offtime = led->offtime; |
497 | b->off_dflt = led->off_dflt; | 498 | b->off_dflt = led->off_dflt; |
498 | need_init = 1; | 499 | need_init = 1; |
499 | } | 500 | } |
500 | 501 | ||
501 | if (need_init) | 502 | if (need_init) |
502 | set_times(tca, i); | 503 | set_times(tca, i); |
503 | 504 | ||
504 | led->ontime = b->ontime; | 505 | led->ontime = b->ontime; |
505 | led->offtime = b->offtime; | 506 | led->offtime = b->offtime; |
506 | 507 | ||
507 | b->time_use++; | 508 | b->time_use++; |
508 | led->blink = 1; | 509 | led->blink = 1; |
509 | led->led_cdev.brightness = TO_BRIGHT(b->level); | 510 | led->led_cdev.brightness = TO_BRIGHT(b->level); |
510 | set_select(tca, led->num, blink_source[i]); | 511 | set_select(tca, led->num, blink_source[i]); |
511 | return 0; | 512 | return 0; |
512 | } | 513 | } |
513 | 514 | ||
514 | static int led_assign(struct tca6507_led *led) | 515 | static int led_assign(struct tca6507_led *led) |
515 | { | 516 | { |
516 | struct tca6507_chip *tca = led->chip; | 517 | struct tca6507_chip *tca = led->chip; |
517 | int err; | 518 | int err; |
518 | unsigned long flags; | 519 | unsigned long flags; |
519 | 520 | ||
520 | spin_lock_irqsave(&tca->lock, flags); | 521 | spin_lock_irqsave(&tca->lock, flags); |
521 | led_release(led); | 522 | led_release(led); |
522 | err = led_prepare(led); | 523 | err = led_prepare(led); |
523 | if (err) { | 524 | if (err) { |
524 | /* | 525 | /* |
525 | * Can only fail on timer setup. In that case we need to | 526 | * Can only fail on timer setup. In that case we need to |
526 | * re-establish as steady level. | 527 | * re-establish as steady level. |
527 | */ | 528 | */ |
528 | led->ontime = 0; | 529 | led->ontime = 0; |
529 | led->offtime = 0; | 530 | led->offtime = 0; |
530 | led_prepare(led); | 531 | led_prepare(led); |
531 | } | 532 | } |
532 | spin_unlock_irqrestore(&tca->lock, flags); | 533 | spin_unlock_irqrestore(&tca->lock, flags); |
533 | 534 | ||
534 | if (tca->reg_set) | 535 | if (tca->reg_set) |
535 | schedule_work(&tca->work); | 536 | schedule_work(&tca->work); |
536 | return err; | 537 | return err; |
537 | } | 538 | } |
538 | 539 | ||
539 | static void tca6507_brightness_set(struct led_classdev *led_cdev, | 540 | static void tca6507_brightness_set(struct led_classdev *led_cdev, |
540 | enum led_brightness brightness) | 541 | enum led_brightness brightness) |
541 | { | 542 | { |
542 | struct tca6507_led *led = container_of(led_cdev, struct tca6507_led, | 543 | struct tca6507_led *led = container_of(led_cdev, struct tca6507_led, |
543 | led_cdev); | 544 | led_cdev); |
544 | led->led_cdev.brightness = brightness; | 545 | led->led_cdev.brightness = brightness; |
545 | led->ontime = 0; | 546 | led->ontime = 0; |
546 | led->offtime = 0; | 547 | led->offtime = 0; |
547 | led_assign(led); | 548 | led_assign(led); |
548 | } | 549 | } |
549 | 550 | ||
550 | static int tca6507_blink_set(struct led_classdev *led_cdev, | 551 | static int tca6507_blink_set(struct led_classdev *led_cdev, |
551 | unsigned long *delay_on, | 552 | unsigned long *delay_on, |
552 | unsigned long *delay_off) | 553 | unsigned long *delay_off) |
553 | { | 554 | { |
554 | struct tca6507_led *led = container_of(led_cdev, struct tca6507_led, | 555 | struct tca6507_led *led = container_of(led_cdev, struct tca6507_led, |
555 | led_cdev); | 556 | led_cdev); |
556 | 557 | ||
557 | if (*delay_on == 0) | 558 | if (*delay_on == 0) |
558 | led->on_dflt = 1; | 559 | led->on_dflt = 1; |
559 | else if (delay_on != &led_cdev->blink_delay_on) | 560 | else if (delay_on != &led_cdev->blink_delay_on) |
560 | led->on_dflt = 0; | 561 | led->on_dflt = 0; |
561 | led->ontime = *delay_on; | 562 | led->ontime = *delay_on; |
562 | 563 | ||
563 | if (*delay_off == 0) | 564 | if (*delay_off == 0) |
564 | led->off_dflt = 1; | 565 | led->off_dflt = 1; |
565 | else if (delay_off != &led_cdev->blink_delay_off) | 566 | else if (delay_off != &led_cdev->blink_delay_off) |
566 | led->off_dflt = 0; | 567 | led->off_dflt = 0; |
567 | led->offtime = *delay_off; | 568 | led->offtime = *delay_off; |
568 | 569 | ||
569 | if (led->ontime == 0) | 570 | if (led->ontime == 0) |
570 | led->ontime = 512; | 571 | led->ontime = 512; |
571 | if (led->offtime == 0) | 572 | if (led->offtime == 0) |
572 | led->offtime = 512; | 573 | led->offtime = 512; |
573 | 574 | ||
574 | if (led->led_cdev.brightness == LED_OFF) | 575 | if (led->led_cdev.brightness == LED_OFF) |
575 | led->led_cdev.brightness = LED_FULL; | 576 | led->led_cdev.brightness = LED_FULL; |
576 | if (led_assign(led) < 0) { | 577 | if (led_assign(led) < 0) { |
577 | led->ontime = 0; | 578 | led->ontime = 0; |
578 | led->offtime = 0; | 579 | led->offtime = 0; |
579 | led->led_cdev.brightness = LED_OFF; | 580 | led->led_cdev.brightness = LED_OFF; |
580 | return -EINVAL; | 581 | return -EINVAL; |
581 | } | 582 | } |
582 | *delay_on = led->ontime; | 583 | *delay_on = led->ontime; |
583 | *delay_off = led->offtime; | 584 | *delay_off = led->offtime; |
584 | return 0; | 585 | return 0; |
585 | } | 586 | } |
586 | 587 | ||
587 | #ifdef CONFIG_GPIOLIB | 588 | #ifdef CONFIG_GPIOLIB |
588 | static void tca6507_gpio_set_value(struct gpio_chip *gc, | 589 | static void tca6507_gpio_set_value(struct gpio_chip *gc, |
589 | unsigned offset, int val) | 590 | unsigned offset, int val) |
590 | { | 591 | { |
591 | struct tca6507_chip *tca = container_of(gc, struct tca6507_chip, gpio); | 592 | struct tca6507_chip *tca = container_of(gc, struct tca6507_chip, gpio); |
592 | unsigned long flags; | 593 | unsigned long flags; |
593 | 594 | ||
594 | spin_lock_irqsave(&tca->lock, flags); | 595 | spin_lock_irqsave(&tca->lock, flags); |
595 | /* | 596 | /* |
596 | * 'OFF' is floating high, and 'ON' is pulled down, so it has the | 597 | * 'OFF' is floating high, and 'ON' is pulled down, so it has the |
597 | * inverse sense of 'val'. | 598 | * inverse sense of 'val'. |
598 | */ | 599 | */ |
599 | set_select(tca, tca->gpio_map[offset], | 600 | set_select(tca, tca->gpio_map[offset], |
600 | val ? TCA6507_LS_LED_OFF : TCA6507_LS_LED_ON); | 601 | val ? TCA6507_LS_LED_OFF : TCA6507_LS_LED_ON); |
601 | spin_unlock_irqrestore(&tca->lock, flags); | 602 | spin_unlock_irqrestore(&tca->lock, flags); |
602 | if (tca->reg_set) | 603 | if (tca->reg_set) |
603 | schedule_work(&tca->work); | 604 | schedule_work(&tca->work); |
604 | } | 605 | } |
605 | 606 | ||
606 | static int tca6507_gpio_direction_output(struct gpio_chip *gc, | 607 | static int tca6507_gpio_direction_output(struct gpio_chip *gc, |
607 | unsigned offset, int val) | 608 | unsigned offset, int val) |
608 | { | 609 | { |
609 | tca6507_gpio_set_value(gc, offset, val); | 610 | tca6507_gpio_set_value(gc, offset, val); |
610 | return 0; | 611 | return 0; |
611 | } | 612 | } |
612 | 613 | ||
613 | static int tca6507_probe_gpios(struct i2c_client *client, | 614 | static int tca6507_probe_gpios(struct i2c_client *client, |
614 | struct tca6507_chip *tca, | 615 | struct tca6507_chip *tca, |
615 | struct tca6507_platform_data *pdata) | 616 | struct tca6507_platform_data *pdata) |
616 | { | 617 | { |
617 | int err; | 618 | int err; |
618 | int i = 0; | 619 | int i = 0; |
619 | int gpios = 0; | 620 | int gpios = 0; |
620 | 621 | ||
621 | for (i = 0; i < NUM_LEDS; i++) | 622 | for (i = 0; i < NUM_LEDS; i++) |
622 | if (pdata->leds.leds[i].name && pdata->leds.leds[i].flags) { | 623 | if (pdata->leds.leds[i].name && pdata->leds.leds[i].flags) { |
623 | /* Configure as a gpio */ | 624 | /* Configure as a gpio */ |
624 | tca->gpio_name[gpios] = pdata->leds.leds[i].name; | 625 | tca->gpio_name[gpios] = pdata->leds.leds[i].name; |
625 | tca->gpio_map[gpios] = i; | 626 | tca->gpio_map[gpios] = i; |
626 | gpios++; | 627 | gpios++; |
627 | } | 628 | } |
628 | 629 | ||
629 | if (!gpios) | 630 | if (!gpios) |
630 | return 0; | 631 | return 0; |
631 | 632 | ||
632 | tca->gpio.label = "gpio-tca6507"; | 633 | tca->gpio.label = "gpio-tca6507"; |
633 | tca->gpio.names = tca->gpio_name; | 634 | tca->gpio.names = tca->gpio_name; |
634 | tca->gpio.ngpio = gpios; | 635 | tca->gpio.ngpio = gpios; |
635 | tca->gpio.base = pdata->gpio_base; | 636 | tca->gpio.base = pdata->gpio_base; |
636 | tca->gpio.owner = THIS_MODULE; | 637 | tca->gpio.owner = THIS_MODULE; |
637 | tca->gpio.direction_output = tca6507_gpio_direction_output; | 638 | tca->gpio.direction_output = tca6507_gpio_direction_output; |
638 | tca->gpio.set = tca6507_gpio_set_value; | 639 | tca->gpio.set = tca6507_gpio_set_value; |
639 | tca->gpio.dev = &client->dev; | 640 | tca->gpio.dev = &client->dev; |
640 | err = gpiochip_add(&tca->gpio); | 641 | err = gpiochip_add(&tca->gpio); |
641 | if (err) { | 642 | if (err) { |
642 | tca->gpio.ngpio = 0; | 643 | tca->gpio.ngpio = 0; |
643 | return err; | 644 | return err; |
644 | } | 645 | } |
645 | if (pdata->setup) | 646 | if (pdata->setup) |
646 | pdata->setup(tca->gpio.base, tca->gpio.ngpio); | 647 | pdata->setup(tca->gpio.base, tca->gpio.ngpio); |
647 | return 0; | 648 | return 0; |
648 | } | 649 | } |
649 | 650 | ||
650 | static void tca6507_remove_gpio(struct tca6507_chip *tca) | 651 | static void tca6507_remove_gpio(struct tca6507_chip *tca) |
651 | { | 652 | { |
652 | if (tca->gpio.ngpio) { | 653 | if (tca->gpio.ngpio) { |
653 | int err = gpiochip_remove(&tca->gpio); | 654 | int err = gpiochip_remove(&tca->gpio); |
654 | dev_err(&tca->client->dev, "%s failed, %d\n", | 655 | dev_err(&tca->client->dev, "%s failed, %d\n", |
655 | "gpiochip_remove()", err); | 656 | "gpiochip_remove()", err); |
656 | } | 657 | } |
657 | } | 658 | } |
658 | #else /* CONFIG_GPIOLIB */ | 659 | #else /* CONFIG_GPIOLIB */ |
659 | static int tca6507_probe_gpios(struct i2c_client *client, | 660 | static int tca6507_probe_gpios(struct i2c_client *client, |
660 | struct tca6507_chip *tca, | 661 | struct tca6507_chip *tca, |
661 | struct tca6507_platform_data *pdata) | 662 | struct tca6507_platform_data *pdata) |
662 | { | 663 | { |
663 | return 0; | 664 | return 0; |
664 | } | 665 | } |
665 | static void tca6507_remove_gpio(struct tca6507_chip *tca) | 666 | static void tca6507_remove_gpio(struct tca6507_chip *tca) |
666 | { | 667 | { |
667 | } | 668 | } |
668 | #endif /* CONFIG_GPIOLIB */ | 669 | #endif /* CONFIG_GPIOLIB */ |
669 | 670 | ||
670 | #ifdef CONFIG_OF | 671 | #ifdef CONFIG_OF |
671 | static struct tca6507_platform_data * | 672 | static struct tca6507_platform_data * |
672 | tca6507_led_dt_init(struct i2c_client *client) | 673 | tca6507_led_dt_init(struct i2c_client *client) |
673 | { | 674 | { |
674 | struct device_node *np = client->dev.of_node, *child; | 675 | struct device_node *np = client->dev.of_node, *child; |
675 | struct tca6507_platform_data *pdata; | 676 | struct tca6507_platform_data *pdata; |
676 | struct led_info *tca_leds; | 677 | struct led_info *tca_leds; |
677 | int count; | 678 | int count; |
678 | 679 | ||
679 | count = of_get_child_count(np); | 680 | count = of_get_child_count(np); |
680 | if (!count || count > NUM_LEDS) | 681 | if (!count || count > NUM_LEDS) |
681 | return ERR_PTR(-ENODEV); | 682 | return ERR_PTR(-ENODEV); |
682 | 683 | ||
683 | tca_leds = devm_kzalloc(&client->dev, | 684 | tca_leds = devm_kzalloc(&client->dev, |
684 | sizeof(struct led_info) * count, GFP_KERNEL); | 685 | sizeof(struct led_info) * count, GFP_KERNEL); |
685 | if (!tca_leds) | 686 | if (!tca_leds) |
686 | return ERR_PTR(-ENOMEM); | 687 | return ERR_PTR(-ENOMEM); |
687 | 688 | ||
688 | for_each_child_of_node(np, child) { | 689 | for_each_child_of_node(np, child) { |
689 | struct led_info led; | 690 | struct led_info led; |
690 | u32 reg; | 691 | u32 reg; |
691 | int ret; | 692 | int ret; |
692 | 693 | ||
693 | led.name = | 694 | led.name = |
694 | of_get_property(child, "label", NULL) ? : child->name; | 695 | of_get_property(child, "label", NULL) ? : child->name; |
695 | led.default_trigger = | 696 | led.default_trigger = |
696 | of_get_property(child, "linux,default-trigger", NULL); | 697 | of_get_property(child, "linux,default-trigger", NULL); |
697 | 698 | ||
698 | ret = of_property_read_u32(child, "reg", ®); | 699 | ret = of_property_read_u32(child, "reg", ®); |
699 | if (ret != 0) | 700 | if (ret != 0) |
700 | continue; | 701 | continue; |
701 | 702 | ||
702 | tca_leds[reg] = led; | 703 | tca_leds[reg] = led; |
703 | } | 704 | } |
704 | pdata = devm_kzalloc(&client->dev, | 705 | pdata = devm_kzalloc(&client->dev, |
705 | sizeof(struct tca6507_platform_data), GFP_KERNEL); | 706 | sizeof(struct tca6507_platform_data), GFP_KERNEL); |
706 | if (!pdata) | 707 | if (!pdata) |
707 | return ERR_PTR(-ENOMEM); | 708 | return ERR_PTR(-ENOMEM); |
708 | 709 | ||
709 | pdata->leds.leds = tca_leds; | 710 | pdata->leds.leds = tca_leds; |
710 | pdata->leds.num_leds = count; | 711 | pdata->leds.num_leds = count; |
711 | 712 | ||
712 | return pdata; | 713 | return pdata; |
713 | } | 714 | } |
714 | 715 | ||
715 | static const struct of_device_id of_tca6507_leds_match[] = { | 716 | static const struct of_device_id of_tca6507_leds_match[] = { |
716 | { .compatible = "ti,tca6507", }, | 717 | { .compatible = "ti,tca6507", }, |
717 | {}, | 718 | {}, |
718 | }; | 719 | }; |
719 | 720 | ||
720 | #else | 721 | #else |
721 | static struct tca6507_platform_data * | 722 | static struct tca6507_platform_data * |
722 | tca6507_led_dt_init(struct i2c_client *client) | 723 | tca6507_led_dt_init(struct i2c_client *client) |
723 | { | 724 | { |
724 | return ERR_PTR(-ENODEV); | 725 | return ERR_PTR(-ENODEV); |
725 | } | 726 | } |
726 | 727 | ||
727 | #define of_tca6507_leds_match NULL | ||
728 | #endif | 728 | #endif |
729 | 729 | ||
730 | static int tca6507_probe(struct i2c_client *client, | 730 | static int tca6507_probe(struct i2c_client *client, |
731 | const struct i2c_device_id *id) | 731 | const struct i2c_device_id *id) |
732 | { | 732 | { |
733 | struct tca6507_chip *tca; | 733 | struct tca6507_chip *tca; |
734 | struct i2c_adapter *adapter; | 734 | struct i2c_adapter *adapter; |
735 | struct tca6507_platform_data *pdata; | 735 | struct tca6507_platform_data *pdata; |
736 | int err; | 736 | int err; |
737 | int i = 0; | 737 | int i = 0; |
738 | 738 | ||
739 | adapter = to_i2c_adapter(client->dev.parent); | 739 | adapter = to_i2c_adapter(client->dev.parent); |
740 | pdata = client->dev.platform_data; | 740 | pdata = client->dev.platform_data; |
741 | 741 | ||
742 | if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) | 742 | if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) |
743 | return -EIO; | 743 | return -EIO; |
744 | 744 | ||
745 | if (!pdata || pdata->leds.num_leds != NUM_LEDS) { | 745 | if (!pdata || pdata->leds.num_leds != NUM_LEDS) { |
746 | pdata = tca6507_led_dt_init(client); | 746 | pdata = tca6507_led_dt_init(client); |
747 | if (IS_ERR(pdata)) { | 747 | if (IS_ERR(pdata)) { |
748 | dev_err(&client->dev, "Need %d entries in platform-data list\n", | 748 | dev_err(&client->dev, "Need %d entries in platform-data list\n", |
749 | NUM_LEDS); | 749 | NUM_LEDS); |
750 | return PTR_ERR(pdata); | 750 | return PTR_ERR(pdata); |
751 | } | 751 | } |
752 | } | 752 | } |
753 | tca = devm_kzalloc(&client->dev, sizeof(*tca), GFP_KERNEL); | 753 | tca = devm_kzalloc(&client->dev, sizeof(*tca), GFP_KERNEL); |
754 | if (!tca) | 754 | if (!tca) |
755 | return -ENOMEM; | 755 | return -ENOMEM; |
756 | 756 | ||
757 | tca->client = client; | 757 | tca->client = client; |
758 | INIT_WORK(&tca->work, tca6507_work); | 758 | INIT_WORK(&tca->work, tca6507_work); |
759 | spin_lock_init(&tca->lock); | 759 | spin_lock_init(&tca->lock); |
760 | i2c_set_clientdata(client, tca); | 760 | i2c_set_clientdata(client, tca); |
761 | 761 | ||
762 | for (i = 0; i < NUM_LEDS; i++) { | 762 | for (i = 0; i < NUM_LEDS; i++) { |
763 | struct tca6507_led *l = tca->leds + i; | 763 | struct tca6507_led *l = tca->leds + i; |
764 | 764 | ||
765 | l->chip = tca; | 765 | l->chip = tca; |
766 | l->num = i; | 766 | l->num = i; |
767 | if (pdata->leds.leds[i].name && !pdata->leds.leds[i].flags) { | 767 | if (pdata->leds.leds[i].name && !pdata->leds.leds[i].flags) { |
768 | l->led_cdev.name = pdata->leds.leds[i].name; | 768 | l->led_cdev.name = pdata->leds.leds[i].name; |
769 | l->led_cdev.default_trigger | 769 | l->led_cdev.default_trigger |
770 | = pdata->leds.leds[i].default_trigger; | 770 | = pdata->leds.leds[i].default_trigger; |
771 | l->led_cdev.brightness_set = tca6507_brightness_set; | 771 | l->led_cdev.brightness_set = tca6507_brightness_set; |
772 | l->led_cdev.blink_set = tca6507_blink_set; | 772 | l->led_cdev.blink_set = tca6507_blink_set; |
773 | l->bank = -1; | 773 | l->bank = -1; |
774 | err = led_classdev_register(&client->dev, | 774 | err = led_classdev_register(&client->dev, |
775 | &l->led_cdev); | 775 | &l->led_cdev); |
776 | if (err < 0) | 776 | if (err < 0) |
777 | goto exit; | 777 | goto exit; |
778 | } | 778 | } |
779 | } | 779 | } |
780 | err = tca6507_probe_gpios(client, tca, pdata); | 780 | err = tca6507_probe_gpios(client, tca, pdata); |
781 | if (err) | 781 | if (err) |
782 | goto exit; | 782 | goto exit; |
783 | /* set all registers to known state - zero */ | 783 | /* set all registers to known state - zero */ |
784 | tca->reg_set = 0x7f; | 784 | tca->reg_set = 0x7f; |
785 | schedule_work(&tca->work); | 785 | schedule_work(&tca->work); |
786 | 786 | ||
787 | return 0; | 787 | return 0; |
788 | exit: | 788 | exit: |
789 | while (i--) { | 789 | while (i--) { |
790 | if (tca->leds[i].led_cdev.name) | 790 | if (tca->leds[i].led_cdev.name) |
791 | led_classdev_unregister(&tca->leds[i].led_cdev); | 791 | led_classdev_unregister(&tca->leds[i].led_cdev); |
792 | } | 792 | } |
793 | return err; | 793 | return err; |
794 | } | 794 | } |
795 | 795 | ||
796 | static int tca6507_remove(struct i2c_client *client) | 796 | static int tca6507_remove(struct i2c_client *client) |
797 | { | 797 | { |
798 | int i; | 798 | int i; |
799 | struct tca6507_chip *tca = i2c_get_clientdata(client); | 799 | struct tca6507_chip *tca = i2c_get_clientdata(client); |
800 | struct tca6507_led *tca_leds = tca->leds; | 800 | struct tca6507_led *tca_leds = tca->leds; |
801 | 801 | ||
802 | for (i = 0; i < NUM_LEDS; i++) { | 802 | for (i = 0; i < NUM_LEDS; i++) { |
803 | if (tca_leds[i].led_cdev.name) | 803 | if (tca_leds[i].led_cdev.name) |
804 | led_classdev_unregister(&tca_leds[i].led_cdev); | 804 | led_classdev_unregister(&tca_leds[i].led_cdev); |
805 | } | 805 | } |
806 | tca6507_remove_gpio(tca); | 806 | tca6507_remove_gpio(tca); |
807 | cancel_work_sync(&tca->work); | 807 | cancel_work_sync(&tca->work); |
808 | 808 | ||
809 | return 0; | 809 | return 0; |
810 | } | 810 | } |
811 | 811 | ||
812 | static struct i2c_driver tca6507_driver = { | 812 | static struct i2c_driver tca6507_driver = { |
813 | .driver = { | 813 | .driver = { |
814 | .name = "leds-tca6507", | 814 | .name = "leds-tca6507", |
815 | .owner = THIS_MODULE, | 815 | .owner = THIS_MODULE, |
816 | .of_match_table = of_tca6507_leds_match, | 816 | .of_match_table = of_match_ptr(of_tca6507_leds_match), |
817 | }, | 817 | }, |
818 | .probe = tca6507_probe, | 818 | .probe = tca6507_probe, |
819 | .remove = tca6507_remove, | 819 | .remove = tca6507_remove, |
820 | .id_table = tca6507_id, | 820 | .id_table = tca6507_id, |
821 | }; | 821 | }; |
822 | 822 | ||
823 | module_i2c_driver(tca6507_driver); | 823 | module_i2c_driver(tca6507_driver); |
824 | 824 | ||
825 | MODULE_AUTHOR("NeilBrown <neilb@suse.de>"); | 825 | MODULE_AUTHOR("NeilBrown <neilb@suse.de>"); |
826 | MODULE_DESCRIPTION("TCA6507 LED/GPO driver"); | 826 | MODULE_DESCRIPTION("TCA6507 LED/GPO driver"); |
827 | MODULE_LICENSE("GPL v2"); | 827 | MODULE_LICENSE("GPL v2"); |