1414 * HMC1019A: 0.5 dB LSB GaAs MMIC 5-BIT DIGITAL ATTENUATOR, 0.1 - 30 GHz
1515 * HMC1119 0.25 dB LSB, 7-Bit, Silicon Digital Attenuator
1616 *
17- * Copyright 2012-2021 Analog Devices Inc.
17+ * Copyright 2012-2025 Analog Devices Inc.
1818 */
1919
20+ #include <linux/bitrev.h>
21+ #include <linux/cleanup.h>
2022#include <linux/device.h>
21- #include <linux/kernel.h>
22- #include <linux/slab.h>
23- #include <linux/sysfs.h>
24- #include <linux/spi/spi.h>
25- #include <linux/regulator/consumer.h>
26- #include <linux/gpio/consumer.h>
2723#include <linux/err.h>
28- #include <linux/module.h>
29- #include <linux/bitrev.h>
30-
24+ #include <linux/gpio/consumer.h>
3125#include <linux/iio/iio.h>
3226#include <linux/iio/sysfs.h>
27+ #include <linux/module.h>
28+ #include <linux/mod_devicetable.h>
29+ #include <linux/regulator/consumer.h>
30+ #include <linux/slab.h>
31+ #include <linux/spi/spi.h>
32+ #include <linux/sysfs.h>
3333
3434enum ad8366_type {
3535 ID_AD8366 ,
@@ -52,8 +52,7 @@ struct ad8366_info {
5252
5353struct ad8366_state {
5454 struct spi_device * spi ;
55- struct regulator * reg ;
56- struct mutex lock ; /* protect sensor state */
55+ struct mutex lock ; /* protect sensor state */
5756 struct gpio_desc * reset_gpio ;
5857 struct gpio_desc * enable_gpio ;
5958 unsigned char ch [2 ];
@@ -164,7 +163,8 @@ static int ad8366_read_raw(struct iio_dev *indio_dev,
164163 int ret ;
165164 int code , gain = 0 ;
166165
167- mutex_lock (& st -> lock );
166+ guard (mutex )(& st -> lock );
167+
168168 switch (m ) {
169169 case IIO_CHAN_INFO_HARDWAREGAIN :
170170 code = st -> ch [chan -> channel ];
@@ -210,7 +210,6 @@ static int ad8366_read_raw(struct iio_dev *indio_dev,
210210 default :
211211 ret = - EINVAL ;
212212 }
213- mutex_unlock (& st -> lock );
214213
215214 return ret ;
216215};
@@ -267,7 +266,8 @@ static int ad8366_write_raw(struct iio_dev *indio_dev,
267266 break ;
268267 }
269268
270- mutex_lock (& st -> lock );
269+ guard (mutex )(& st -> lock );
270+
271271 switch (mask ) {
272272 case IIO_CHAN_INFO_HARDWAREGAIN :
273273 st -> ch [chan -> channel ] = code ;
@@ -276,7 +276,6 @@ static int ad8366_write_raw(struct iio_dev *indio_dev,
276276 default :
277277 ret = - EINVAL ;
278278 }
279- mutex_unlock (& st -> lock );
280279
281280 return ret ;
282281}
@@ -323,22 +322,21 @@ static int ad8366_probe(struct spi_device *spi)
323322 int ret ;
324323
325324 indio_dev = devm_iio_device_alloc (& spi -> dev , sizeof (* st ));
326- if (indio_dev == NULL )
325+ if (! indio_dev )
327326 return - ENOMEM ;
328327
329328 st = iio_priv (indio_dev );
330-
331- st -> reg = devm_regulator_get (& spi -> dev , "vcc" );
332- if (!IS_ERR (st -> reg )) {
333- ret = regulator_enable (st -> reg );
334- if (ret )
335- return ret ;
336- }
337-
338- spi_set_drvdata (spi , indio_dev );
339- mutex_init (& st -> lock );
340329 st -> spi = spi ;
341330 st -> type = spi_get_device_id (spi )-> driver_data ;
331+ spi_set_drvdata (spi , indio_dev );
332+
333+ ret = devm_regulator_get_enable (& spi -> dev , "vcc" );
334+ if (ret )
335+ return dev_err_probe (& spi -> dev , ret , "Failed to get regulator\n" );
336+
337+ ret = devm_mutex_init (& spi -> dev , & st -> lock );
338+ if (ret )
339+ return dev_err_probe (& spi -> dev , ret , "failed to initialize mutex: %d\n" , ret );
342340
343341 switch (st -> type ) {
344342 case ID_AD8366 :
@@ -356,21 +354,20 @@ static int ad8366_probe(struct spi_device *spi)
356354 case ID_HMC1018 :
357355 case ID_HMC1019 :
358356 st -> reset_gpio = devm_gpiod_get_optional (& spi -> dev , "reset" , GPIOD_OUT_HIGH );
359- if (IS_ERR (st -> reset_gpio )) {
360- ret = PTR_ERR (st -> reset_gpio );
361- goto error_disable_reg ;
362- }
357+ if (IS_ERR (st -> reset_gpio ))
358+ return dev_err_probe (& spi -> dev , PTR_ERR (st -> reset_gpio ),
359+ "Failed to get reset GPIO\n" );
363360
364- st -> enable_gpio = devm_gpiod_get (& spi -> dev , "enable" ,
365- GPIOD_OUT_HIGH );
361+ st -> enable_gpio = devm_gpiod_get (& spi -> dev , "enable" , GPIOD_OUT_HIGH );
362+ if (IS_ERR (st -> enable_gpio ))
363+ return dev_err_probe (& spi -> dev , PTR_ERR (st -> enable_gpio ),
364+ "Failed to get enable GPIO\n" );
366365
367366 indio_dev -> channels = ada4961_channels ;
368367 indio_dev -> num_channels = ARRAY_SIZE (ada4961_channels );
369368 break ;
370369 default :
371- dev_err (& spi -> dev , "Invalid device ID\n" );
372- ret = - EINVAL ;
373- goto error_disable_reg ;
370+ return dev_err_probe (& spi -> dev , - EINVAL , "Invalid device ID\n" );
374371 }
375372
376373 st -> info = & ad8366_infos [st -> type ];
@@ -380,31 +377,9 @@ static int ad8366_probe(struct spi_device *spi)
380377
381378 ret = ad8366_write (indio_dev , 0 , 0 );
382379 if (ret < 0 )
383- goto error_disable_reg ;
384-
385- ret = iio_device_register (indio_dev );
386- if (ret )
387- goto error_disable_reg ;
388-
389- return 0 ;
390-
391- error_disable_reg :
392- if (!IS_ERR (st -> reg ))
393- regulator_disable (st -> reg );
380+ return dev_err_probe (& spi -> dev , ret , "failed to write initial gain\n" );
394381
395- return ret ;
396- }
397-
398- static void ad8366_remove (struct spi_device * spi )
399- {
400- struct iio_dev * indio_dev = spi_get_drvdata (spi );
401- struct ad8366_state * st = iio_priv (indio_dev );
402- struct regulator * reg = st -> reg ;
403-
404- iio_device_unregister (indio_dev );
405-
406- if (!IS_ERR (reg ))
407- regulator_disable (reg );
382+ return devm_iio_device_register (& spi -> dev , indio_dev );
408383}
409384
410385static const struct spi_device_id ad8366_id [] = {
@@ -423,12 +398,26 @@ static const struct spi_device_id ad8366_id[] = {
423398};
424399MODULE_DEVICE_TABLE (spi , ad8366_id );
425400
401+ static const struct of_device_id ad8366_of_match [] = {
402+ { .compatible = "adi,ad8366" },
403+ { .compatible = "adi,ada4961" },
404+ { .compatible = "adi,adrf5720" },
405+ { .compatible = "adi,adrf5730" },
406+ { .compatible = "adi,adrf5731" },
407+ { .compatible = "adi,adl5240" },
408+ { .compatible = "adi,hmc792a" },
409+ { .compatible = "adi,hmc1018a" },
410+ { .compatible = "adi,hmc1019a" },
411+ { .compatible = "adi,hmc1119" },
412+ { }
413+ };
414+ MODULE_DEVICE_TABLE (of , ad8366_of_match );
415+
426416static struct spi_driver ad8366_driver = {
427417 .driver = {
428418 .name = KBUILD_MODNAME ,
429419 },
430420 .probe = ad8366_probe ,
431- .remove = ad8366_remove ,
432421 .id_table = ad8366_id ,
433422};
434423
0 commit comments