66 */
77
88#include <linux/backlight.h>
9+ #include <linux/cleanup.h>
910#include <linux/err.h>
1011#include <linux/gpio/driver.h>
1112#include <linux/i2c.h>
1213#include <linux/init.h>
1314#include <linux/interrupt.h>
1415#include <linux/module.h>
16+ #include <linux/mutex.h>
1517#include <linux/regmap.h>
1618#include <linux/regulator/driver.h>
1719#include <linux/regulator/machine.h>
@@ -93,7 +95,7 @@ static int attiny_lcd_power_enable(struct regulator_dev *rdev)
9395{
9496 struct attiny_lcd * state = rdev_get_drvdata (rdev );
9597
96- mutex_lock (& state -> lock );
98+ guard ( mutex ) (& state -> lock );
9799
98100 /* Ensure bridge, and tp stay in reset */
99101 attiny_set_port_state (state , REG_PORTC , 0 );
@@ -114,16 +116,14 @@ static int attiny_lcd_power_enable(struct regulator_dev *rdev)
114116
115117 msleep (80 );
116118
117- mutex_unlock (& state -> lock );
118-
119119 return 0 ;
120120}
121121
122122static int attiny_lcd_power_disable (struct regulator_dev * rdev )
123123{
124124 struct attiny_lcd * state = rdev_get_drvdata (rdev );
125125
126- mutex_lock (& state -> lock );
126+ guard ( mutex ) (& state -> lock );
127127
128128 regmap_write (rdev -> regmap , REG_PWM , 0 );
129129 usleep_range (5000 , 10000 );
@@ -135,28 +135,24 @@ static int attiny_lcd_power_disable(struct regulator_dev *rdev)
135135 attiny_set_port_state (state , REG_PORTC , 0 );
136136 msleep (30 );
137137
138- mutex_unlock (& state -> lock );
139-
140138 return 0 ;
141139}
142140
143141static int attiny_lcd_power_is_enabled (struct regulator_dev * rdev )
144142{
145143 struct attiny_lcd * state = rdev_get_drvdata (rdev );
146144 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+ }
156154 }
157155
158- mutex_unlock (& state -> lock );
159-
160156 if (ret < 0 )
161157 return ret ;
162158
@@ -189,16 +185,14 @@ static int attiny_update_status(struct backlight_device *bl)
189185 int brightness = backlight_get_brightness (bl );
190186 int ret , i ;
191187
192- mutex_lock (& state -> lock );
188+ guard ( mutex ) (& state -> lock );
193189
194190 for (i = 0 ; i < 10 ; i ++ ) {
195191 ret = regmap_write (regmap , REG_PWM , brightness );
196192 if (!ret )
197193 break ;
198194 }
199195
200- mutex_unlock (& state -> lock );
201-
202196 return ret ;
203197}
204198
@@ -211,15 +205,12 @@ static int attiny_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
211205 return GPIO_LINE_DIRECTION_OUT ;
212206}
213207
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 )
215209{
216210 struct attiny_lcd * state = gpiochip_get_data (gc );
217211 u8 last_val ;
218212
219- if (off >= NUM_GPIO )
220- return ;
221-
222- mutex_lock (& state -> lock );
213+ guard (mutex )(& state -> lock );
223214
224215 last_val = attiny_get_port_state (state , mappings [off ].reg );
225216 if (val )
@@ -242,7 +233,7 @@ static void attiny_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
242233 msleep (100 );
243234 }
244235
245- mutex_unlock ( & state -> lock ) ;
236+ return 0 ;
246237}
247238
248239static 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)
296287 if (!state )
297288 return - ENOMEM ;
298289
299- mutex_init (& state -> lock );
290+ ret = devm_mutex_init (& i2c -> dev , & state -> lock );
291+ if (ret )
292+ return ret ;
293+
300294 i2c_set_clientdata (i2c , state );
301295
302296 regmap = devm_regmap_init_i2c (i2c , & attiny_regmap_config );
303297 if (IS_ERR (regmap )) {
304298 ret = PTR_ERR (regmap );
305299 dev_err (& i2c -> dev , "Failed to allocate register map: %d\n" ,
306300 ret );
307- goto error ;
301+ return ret ;
308302 }
309303
310304 ret = attiny_i2c_read (i2c , REG_ID , & data );
311305 if (ret < 0 ) {
312306 dev_err (& i2c -> dev , "Failed to read REG_ID reg: %d\n" , ret );
313- goto error ;
307+ return ret ;
314308 }
315309
316310 switch (data ) {
@@ -319,8 +313,7 @@ static int attiny_i2c_probe(struct i2c_client *i2c)
319313 break ;
320314 default :
321315 dev_err (& i2c -> dev , "Unknown Atmel firmware revision: 0x%02x\n" , data );
322- ret = - ENODEV ;
323- goto error ;
316+ return - ENODEV ;
324317 }
325318
326319 regmap_write (regmap , REG_POWERON , 0 );
@@ -336,8 +329,7 @@ static int attiny_i2c_probe(struct i2c_client *i2c)
336329 rdev = devm_regulator_register (& i2c -> dev , & attiny_regulator , & config );
337330 if (IS_ERR (rdev )) {
338331 dev_err (& i2c -> dev , "Failed to register ATTINY regulator\n" );
339- ret = PTR_ERR (rdev );
340- goto error ;
332+ return PTR_ERR (rdev );
341333 }
342334
343335 props .type = BACKLIGHT_RAW ;
@@ -348,10 +340,8 @@ static int attiny_i2c_probe(struct i2c_client *i2c)
348340 bl = devm_backlight_device_register (& i2c -> dev , dev_name (& i2c -> dev ),
349341 & i2c -> dev , state , & attiny_bl ,
350342 & 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 );
355345
356346 bl -> props .brightness = 0xff ;
357347
@@ -361,31 +351,17 @@ static int attiny_i2c_probe(struct i2c_client *i2c)
361351 state -> gc .base = -1 ;
362352 state -> gc .ngpio = NUM_GPIO ;
363353
364- state -> gc .set = attiny_gpio_set ;
354+ state -> gc .set_rv = attiny_gpio_set ;
365355 state -> gc .get_direction = attiny_gpio_get_direction ;
366356 state -> gc .can_sleep = true;
367357
368358 ret = devm_gpiochip_add_data (& i2c -> dev , & state -> gc , state );
369- if (ret ) {
359+ if (ret )
370360 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 );
378361
379362 return ret ;
380363}
381364
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-
389365static const struct of_device_id attiny_dt_ids [] = {
390366 { .compatible = "raspberrypi,7inch-touchscreen-panel-regulator" },
391367 {},
@@ -399,7 +375,6 @@ static struct i2c_driver attiny_regulator_driver = {
399375 .of_match_table = attiny_dt_ids ,
400376 },
401377 .probe = attiny_i2c_probe ,
402- .remove = attiny_i2c_remove ,
403378};
404379
405380module_i2c_driver (attiny_regulator_driver );
0 commit comments