aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/leds
diff options
context:
space:
mode:
authorAlexander Stein <alexander.stein@systec-electronic.com>2012-05-29 18:07:30 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-05-29 19:22:32 -0400
commite7e11d8ba807d451857b5c68abe249c7fc2b980f (patch)
treed93a84ccd11589da51e93b76924d30d2f8eec626 /drivers/leds
parent44e1e9f8e70506728b02a18e6d03599a6485d67f (diff)
drivers/leds/leds-pca955x.c: fix race condition while setting brightness on several LEDs
When issuing the following command: for I in 0 1 2 3 4 5 6 7; do echo 0 > /sys/class/leds/pca955x\:${I}/brightness; done It is possible that all the pca955x_read_ls calls are done sequentially before any pca955x_write_ls call is done. This updates the LS only to the last LED update in its set. Fix this by using a global lock for the pca995x device during pca955x_led_work. Also used a struct for shared data betreen all LEDs. [akpm@linux-foundation.org: revert unintentional rename of pca955x_ledsel()] Signed-off-by: Alexander Stein <alexander.stein@systec-electronic.com> Cc: Richard Purdie <rpurdie@rpsys.net> Cc: Bryan Wu <bryan.wu@canonical.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/leds')
-rw-r--r--drivers/leds/leds-pca955x.c95
1 files changed, 59 insertions, 36 deletions
diff --git a/drivers/leds/leds-pca955x.c b/drivers/leds/leds-pca955x.c
index dcc3bc3d38db..5f462dbf0dbb 100644
--- a/drivers/leds/leds-pca955x.c
+++ b/drivers/leds/leds-pca955x.c
@@ -101,11 +101,16 @@ static const struct i2c_device_id pca955x_id[] = {
101}; 101};
102MODULE_DEVICE_TABLE(i2c, pca955x_id); 102MODULE_DEVICE_TABLE(i2c, pca955x_id);
103 103
104struct pca955x_led { 104struct pca955x {
105 struct mutex lock;
106 struct pca955x_led *leds;
105 struct pca955x_chipdef *chipdef; 107 struct pca955x_chipdef *chipdef;
106 struct i2c_client *client; 108 struct i2c_client *client;
109};
110
111struct pca955x_led {
112 struct pca955x *pca955x;
107 struct work_struct work; 113 struct work_struct work;
108 spinlock_t lock;
109 enum led_brightness brightness; 114 enum led_brightness brightness;
110 struct led_classdev led_cdev; 115 struct led_classdev led_cdev;
111 int led_num; /* 0 .. 15 potentially */ 116 int led_num; /* 0 .. 15 potentially */
@@ -140,7 +145,7 @@ static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state)
140 */ 145 */
141static void pca955x_write_psc(struct i2c_client *client, int n, u8 val) 146static void pca955x_write_psc(struct i2c_client *client, int n, u8 val)
142{ 147{
143 struct pca955x_led *pca955x = i2c_get_clientdata(client); 148 struct pca955x *pca955x = i2c_get_clientdata(client);
144 149
145 i2c_smbus_write_byte_data(client, 150 i2c_smbus_write_byte_data(client,
146 pca95xx_num_input_regs(pca955x->chipdef->bits) + 2*n, 151 pca95xx_num_input_regs(pca955x->chipdef->bits) + 2*n,
@@ -156,7 +161,7 @@ static void pca955x_write_psc(struct i2c_client *client, int n, u8 val)
156 */ 161 */
157static void pca955x_write_pwm(struct i2c_client *client, int n, u8 val) 162static void pca955x_write_pwm(struct i2c_client *client, int n, u8 val)
158{ 163{
159 struct pca955x_led *pca955x = i2c_get_clientdata(client); 164 struct pca955x *pca955x = i2c_get_clientdata(client);
160 165
161 i2c_smbus_write_byte_data(client, 166 i2c_smbus_write_byte_data(client,
162 pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + 2*n, 167 pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + 2*n,
@@ -169,7 +174,7 @@ static void pca955x_write_pwm(struct i2c_client *client, int n, u8 val)
169 */ 174 */
170static void pca955x_write_ls(struct i2c_client *client, int n, u8 val) 175static void pca955x_write_ls(struct i2c_client *client, int n, u8 val)
171{ 176{
172 struct pca955x_led *pca955x = i2c_get_clientdata(client); 177 struct pca955x *pca955x = i2c_get_clientdata(client);
173 178
174 i2c_smbus_write_byte_data(client, 179 i2c_smbus_write_byte_data(client,
175 pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n, 180 pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n,
@@ -182,7 +187,7 @@ static void pca955x_write_ls(struct i2c_client *client, int n, u8 val)
182 */ 187 */
183static u8 pca955x_read_ls(struct i2c_client *client, int n) 188static u8 pca955x_read_ls(struct i2c_client *client, int n)
184{ 189{
185 struct pca955x_led *pca955x = i2c_get_clientdata(client); 190 struct pca955x *pca955x = i2c_get_clientdata(client);
186 191
187 return (u8) i2c_smbus_read_byte_data(client, 192 return (u8) i2c_smbus_read_byte_data(client,
188 pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n); 193 pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n);
@@ -190,18 +195,23 @@ static u8 pca955x_read_ls(struct i2c_client *client, int n)
190 195
191static void pca955x_led_work(struct work_struct *work) 196static void pca955x_led_work(struct work_struct *work)
192{ 197{
193 struct pca955x_led *pca955x; 198 struct pca955x_led *pca955x_led;
199 struct pca955x *pca955x;
194 u8 ls; 200 u8 ls;
195 int chip_ls; /* which LSx to use (0-3 potentially) */ 201 int chip_ls; /* which LSx to use (0-3 potentially) */
196 int ls_led; /* which set of bits within LSx to use (0-3) */ 202 int ls_led; /* which set of bits within LSx to use (0-3) */
197 203
198 pca955x = container_of(work, struct pca955x_led, work); 204 pca955x_led = container_of(work, struct pca955x_led, work);
199 chip_ls = pca955x->led_num / 4; 205 pca955x = pca955x_led->pca955x;
200 ls_led = pca955x->led_num % 4; 206
207 chip_ls = pca955x_led->led_num / 4;
208 ls_led = pca955x_led->led_num % 4;
209
210 mutex_lock(&pca955x->lock);
201 211
202 ls = pca955x_read_ls(pca955x->client, chip_ls); 212 ls = pca955x_read_ls(pca955x->client, chip_ls);
203 213
204 switch (pca955x->brightness) { 214 switch (pca955x_led->brightness) {
205 case LED_FULL: 215 case LED_FULL:
206 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON); 216 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON);
207 break; 217 break;
@@ -219,12 +229,15 @@ static void pca955x_led_work(struct work_struct *work)
219 * OFF, HALF, or FULL. But, this is probably better than 229 * OFF, HALF, or FULL. But, this is probably better than
220 * just turning off for all other values. 230 * just turning off for all other values.
221 */ 231 */
222 pca955x_write_pwm(pca955x->client, 1, 255-pca955x->brightness); 232 pca955x_write_pwm(pca955x->client, 1,
233 255 - pca955x_led->brightness);
223 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1); 234 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1);
224 break; 235 break;
225 } 236 }
226 237
227 pca955x_write_ls(pca955x->client, chip_ls, ls); 238 pca955x_write_ls(pca955x->client, chip_ls, ls);
239
240 mutex_unlock(&pca955x->lock);
228} 241}
229 242
230static void pca955x_led_set(struct led_classdev *led_cdev, enum led_brightness value) 243static void pca955x_led_set(struct led_classdev *led_cdev, enum led_brightness value)
@@ -233,7 +246,6 @@ static void pca955x_led_set(struct led_classdev *led_cdev, enum led_brightness v
233 246
234 pca955x = container_of(led_cdev, struct pca955x_led, led_cdev); 247 pca955x = container_of(led_cdev, struct pca955x_led, led_cdev);
235 248
236 spin_lock(&pca955x->lock);
237 pca955x->brightness = value; 249 pca955x->brightness = value;
238 250
239 /* 251 /*
@@ -241,14 +253,13 @@ static void pca955x_led_set(struct led_classdev *led_cdev, enum led_brightness v
241 * can sleep. 253 * can sleep.
242 */ 254 */
243 schedule_work(&pca955x->work); 255 schedule_work(&pca955x->work);
244
245 spin_unlock(&pca955x->lock);
246} 256}
247 257
248static int __devinit pca955x_probe(struct i2c_client *client, 258static int __devinit pca955x_probe(struct i2c_client *client,
249 const struct i2c_device_id *id) 259 const struct i2c_device_id *id)
250{ 260{
251 struct pca955x_led *pca955x; 261 struct pca955x *pca955x;
262 struct pca955x_led *pca955x_led;
252 struct pca955x_chipdef *chip; 263 struct pca955x_chipdef *chip;
253 struct i2c_adapter *adapter; 264 struct i2c_adapter *adapter;
254 struct led_platform_data *pdata; 265 struct led_platform_data *pdata;
@@ -282,39 +293,48 @@ static int __devinit pca955x_probe(struct i2c_client *client,
282 } 293 }
283 } 294 }
284 295
285 pca955x = kzalloc(sizeof(*pca955x) * chip->bits, GFP_KERNEL); 296 pca955x = kzalloc(sizeof(*pca955x), GFP_KERNEL);
286 if (!pca955x) 297 if (!pca955x)
287 return -ENOMEM; 298 return -ENOMEM;
288 299
300 pca955x->leds = kzalloc(sizeof(*pca955x_led) * chip->bits, GFP_KERNEL);
301 if (!pca955x->leds) {
302 err = -ENOMEM;
303 goto exit_nomem;
304 }
305
289 i2c_set_clientdata(client, pca955x); 306 i2c_set_clientdata(client, pca955x);
290 307
308 mutex_init(&pca955x->lock);
309 pca955x->client = client;
310 pca955x->chipdef = chip;
311
291 for (i = 0; i < chip->bits; i++) { 312 for (i = 0; i < chip->bits; i++) {
292 pca955x[i].chipdef = chip; 313 pca955x_led = &pca955x->leds[i];
293 pca955x[i].client = client; 314 pca955x_led->led_num = i;
294 pca955x[i].led_num = i; 315 pca955x_led->pca955x = pca955x;
295 316
296 /* Platform data can specify LED names and default triggers */ 317 /* Platform data can specify LED names and default triggers */
297 if (pdata) { 318 if (pdata) {
298 if (pdata->leds[i].name) 319 if (pdata->leds[i].name)
299 snprintf(pca955x[i].name, 320 snprintf(pca955x_led->name,
300 sizeof(pca955x[i].name), "pca955x:%s", 321 sizeof(pca955x_led->name), "pca955x:%s",
301 pdata->leds[i].name); 322 pdata->leds[i].name);
302 if (pdata->leds[i].default_trigger) 323 if (pdata->leds[i].default_trigger)
303 pca955x[i].led_cdev.default_trigger = 324 pca955x_led->led_cdev.default_trigger =
304 pdata->leds[i].default_trigger; 325 pdata->leds[i].default_trigger;
305 } else { 326 } else {
306 snprintf(pca955x[i].name, sizeof(pca955x[i].name), 327 snprintf(pca955x_led->name, sizeof(pca955x_led->name),
307 "pca955x:%d", i); 328 "pca955x:%d", i);
308 } 329 }
309 330
310 spin_lock_init(&pca955x[i].lock); 331 pca955x_led->led_cdev.name = pca955x_led->name;
311 332 pca955x_led->led_cdev.brightness_set = pca955x_led_set;
312 pca955x[i].led_cdev.name = pca955x[i].name;
313 pca955x[i].led_cdev.brightness_set = pca955x_led_set;
314 333
315 INIT_WORK(&pca955x[i].work, pca955x_led_work); 334 INIT_WORK(&pca955x_led->work, pca955x_led_work);
316 335
317 err = led_classdev_register(&client->dev, &pca955x[i].led_cdev); 336 err = led_classdev_register(&client->dev,
337 &pca955x_led->led_cdev);
318 if (err < 0) 338 if (err < 0)
319 goto exit; 339 goto exit;
320 } 340 }
@@ -337,10 +357,12 @@ static int __devinit pca955x_probe(struct i2c_client *client,
337 357
338exit: 358exit:
339 while (i--) { 359 while (i--) {
340 led_classdev_unregister(&pca955x[i].led_cdev); 360 led_classdev_unregister(&pca955x->leds[i].led_cdev);
341 cancel_work_sync(&pca955x[i].work); 361 cancel_work_sync(&pca955x->leds[i].work);
342 } 362 }
343 363
364 kfree(pca955x->leds);
365exit_nomem:
344 kfree(pca955x); 366 kfree(pca955x);
345 367
346 return err; 368 return err;
@@ -348,14 +370,15 @@ exit:
348 370
349static int __devexit pca955x_remove(struct i2c_client *client) 371static int __devexit pca955x_remove(struct i2c_client *client)
350{ 372{
351 struct pca955x_led *pca955x = i2c_get_clientdata(client); 373 struct pca955x *pca955x = i2c_get_clientdata(client);
352 int i; 374 int i;
353 375
354 for (i = 0; i < pca955x->chipdef->bits; i++) { 376 for (i = 0; i < pca955x->chipdef->bits; i++) {
355 led_classdev_unregister(&pca955x[i].led_cdev); 377 led_classdev_unregister(&pca955x->leds[i].led_cdev);
356 cancel_work_sync(&pca955x[i].work); 378 cancel_work_sync(&pca955x->leds[i].work);
357 } 379 }
358 380
381 kfree(pca955x->leds);
359 kfree(pca955x); 382 kfree(pca955x);
360 383
361 return 0; 384 return 0;