6
6
*/
7
7
8
8
#include <linux/backlight.h>
9
+ #include <linux/cleanup.h>
9
10
#include <linux/err.h>
10
11
#include <linux/gpio/driver.h>
11
12
#include <linux/i2c.h>
12
13
#include <linux/init.h>
13
14
#include <linux/interrupt.h>
14
15
#include <linux/module.h>
16
+ #include <linux/mutex.h>
15
17
#include <linux/regmap.h>
16
18
#include <linux/regulator/driver.h>
17
19
#include <linux/regulator/machine.h>
@@ -93,7 +95,7 @@ static int attiny_lcd_power_enable(struct regulator_dev *rdev)
93
95
{
94
96
struct attiny_lcd * state = rdev_get_drvdata (rdev );
95
97
96
- mutex_lock (& state -> lock );
98
+ guard ( mutex ) (& state -> lock );
97
99
98
100
/* Ensure bridge, and tp stay in reset */
99
101
attiny_set_port_state (state , REG_PORTC , 0 );
@@ -114,16 +116,14 @@ static int attiny_lcd_power_enable(struct regulator_dev *rdev)
114
116
115
117
msleep (80 );
116
118
117
- mutex_unlock (& state -> lock );
118
-
119
119
return 0 ;
120
120
}
121
121
122
122
static int attiny_lcd_power_disable (struct regulator_dev * rdev )
123
123
{
124
124
struct attiny_lcd * state = rdev_get_drvdata (rdev );
125
125
126
- mutex_lock (& state -> lock );
126
+ guard ( mutex ) (& state -> lock );
127
127
128
128
regmap_write (rdev -> regmap , REG_PWM , 0 );
129
129
usleep_range (5000 , 10000 );
@@ -135,28 +135,24 @@ static int attiny_lcd_power_disable(struct regulator_dev *rdev)
135
135
attiny_set_port_state (state , REG_PORTC , 0 );
136
136
msleep (30 );
137
137
138
- mutex_unlock (& state -> lock );
139
-
140
138
return 0 ;
141
139
}
142
140
143
141
static int attiny_lcd_power_is_enabled (struct regulator_dev * rdev )
144
142
{
145
143
struct attiny_lcd * state = rdev_get_drvdata (rdev );
146
144
unsigned int data ;
147
- int ret , i ;
148
-
149
- mutex_lock ( & state -> lock );
150
-
151
- for ( i = 0 ; i < 10 ; i ++ ) {
152
- ret = regmap_read ( rdev -> regmap , REG_PORTC , & data );
153
- if (! ret )
154
- break ;
155
- usleep_range ( 10000 , 12000 );
145
+ int ret = 0 , i ;
146
+
147
+ scoped_guard ( mutex , & state -> lock ) {
148
+ for ( i = 0 ; i < 10 ; i ++ ) {
149
+ ret = regmap_read ( rdev -> regmap , REG_PORTC , & data );
150
+ if (! ret )
151
+ break ;
152
+ usleep_range ( 10000 , 12000 ) ;
153
+ }
156
154
}
157
155
158
- mutex_unlock (& state -> lock );
159
-
160
156
if (ret < 0 )
161
157
return ret ;
162
158
@@ -189,16 +185,14 @@ static int attiny_update_status(struct backlight_device *bl)
189
185
int brightness = backlight_get_brightness (bl );
190
186
int ret , i ;
191
187
192
- mutex_lock (& state -> lock );
188
+ guard ( mutex ) (& state -> lock );
193
189
194
190
for (i = 0 ; i < 10 ; i ++ ) {
195
191
ret = regmap_write (regmap , REG_PWM , brightness );
196
192
if (!ret )
197
193
break ;
198
194
}
199
195
200
- mutex_unlock (& state -> lock );
201
-
202
196
return ret ;
203
197
}
204
198
@@ -211,15 +205,12 @@ static int attiny_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
211
205
return GPIO_LINE_DIRECTION_OUT ;
212
206
}
213
207
214
- static void attiny_gpio_set (struct gpio_chip * gc , unsigned int off , int val )
208
+ static int attiny_gpio_set (struct gpio_chip * gc , unsigned int off , int val )
215
209
{
216
210
struct attiny_lcd * state = gpiochip_get_data (gc );
217
211
u8 last_val ;
218
212
219
- if (off >= NUM_GPIO )
220
- return ;
221
-
222
- mutex_lock (& state -> lock );
213
+ guard (mutex )(& state -> lock );
223
214
224
215
last_val = attiny_get_port_state (state , mappings [off ].reg );
225
216
if (val )
@@ -242,7 +233,7 @@ static void attiny_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
242
233
msleep (100 );
243
234
}
244
235
245
- mutex_unlock ( & state -> lock ) ;
236
+ return 0 ;
246
237
}
247
238
248
239
static int attiny_i2c_read (struct i2c_client * client , u8 reg , unsigned int * buf )
@@ -296,21 +287,24 @@ static int attiny_i2c_probe(struct i2c_client *i2c)
296
287
if (!state )
297
288
return - ENOMEM ;
298
289
299
- mutex_init (& state -> lock );
290
+ ret = devm_mutex_init (& i2c -> dev , & state -> lock );
291
+ if (ret )
292
+ return ret ;
293
+
300
294
i2c_set_clientdata (i2c , state );
301
295
302
296
regmap = devm_regmap_init_i2c (i2c , & attiny_regmap_config );
303
297
if (IS_ERR (regmap )) {
304
298
ret = PTR_ERR (regmap );
305
299
dev_err (& i2c -> dev , "Failed to allocate register map: %d\n" ,
306
300
ret );
307
- goto error ;
301
+ return ret ;
308
302
}
309
303
310
304
ret = attiny_i2c_read (i2c , REG_ID , & data );
311
305
if (ret < 0 ) {
312
306
dev_err (& i2c -> dev , "Failed to read REG_ID reg: %d\n" , ret );
313
- goto error ;
307
+ return ret ;
314
308
}
315
309
316
310
switch (data ) {
@@ -319,8 +313,7 @@ static int attiny_i2c_probe(struct i2c_client *i2c)
319
313
break ;
320
314
default :
321
315
dev_err (& i2c -> dev , "Unknown Atmel firmware revision: 0x%02x\n" , data );
322
- ret = - ENODEV ;
323
- goto error ;
316
+ return - ENODEV ;
324
317
}
325
318
326
319
regmap_write (regmap , REG_POWERON , 0 );
@@ -336,8 +329,7 @@ static int attiny_i2c_probe(struct i2c_client *i2c)
336
329
rdev = devm_regulator_register (& i2c -> dev , & attiny_regulator , & config );
337
330
if (IS_ERR (rdev )) {
338
331
dev_err (& i2c -> dev , "Failed to register ATTINY regulator\n" );
339
- ret = PTR_ERR (rdev );
340
- goto error ;
332
+ return PTR_ERR (rdev );
341
333
}
342
334
343
335
props .type = BACKLIGHT_RAW ;
@@ -348,10 +340,8 @@ static int attiny_i2c_probe(struct i2c_client *i2c)
348
340
bl = devm_backlight_device_register (& i2c -> dev , dev_name (& i2c -> dev ),
349
341
& i2c -> dev , state , & attiny_bl ,
350
342
& props );
351
- if (IS_ERR (bl )) {
352
- ret = PTR_ERR (bl );
353
- goto error ;
354
- }
343
+ if (IS_ERR (bl ))
344
+ return PTR_ERR (bl );
355
345
356
346
bl -> props .brightness = 0xff ;
357
347
@@ -361,31 +351,17 @@ static int attiny_i2c_probe(struct i2c_client *i2c)
361
351
state -> gc .base = -1 ;
362
352
state -> gc .ngpio = NUM_GPIO ;
363
353
364
- state -> gc .set = attiny_gpio_set ;
354
+ state -> gc .set_rv = attiny_gpio_set ;
365
355
state -> gc .get_direction = attiny_gpio_get_direction ;
366
356
state -> gc .can_sleep = true;
367
357
368
358
ret = devm_gpiochip_add_data (& i2c -> dev , & state -> gc , state );
369
- if (ret ) {
359
+ if (ret )
370
360
dev_err (& i2c -> dev , "Failed to create gpiochip: %d\n" , ret );
371
- goto error ;
372
- }
373
-
374
- return 0 ;
375
-
376
- error :
377
- mutex_destroy (& state -> lock );
378
361
379
362
return ret ;
380
363
}
381
364
382
- static void attiny_i2c_remove (struct i2c_client * client )
383
- {
384
- struct attiny_lcd * state = i2c_get_clientdata (client );
385
-
386
- mutex_destroy (& state -> lock );
387
- }
388
-
389
365
static const struct of_device_id attiny_dt_ids [] = {
390
366
{ .compatible = "raspberrypi,7inch-touchscreen-panel-regulator" },
391
367
{},
@@ -399,7 +375,6 @@ static struct i2c_driver attiny_regulator_driver = {
399
375
.of_match_table = attiny_dt_ids ,
400
376
},
401
377
.probe = attiny_i2c_probe ,
402
- .remove = attiny_i2c_remove ,
403
378
};
404
379
405
380
module_i2c_driver (attiny_regulator_driver );
0 commit comments