Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: adc: at91-sama5d2: Move claim of direct mode up a level and use guard()

Move iio_device_claim_direct_mode() up one layer in the call stack,
and use guard() for scope based unlocking to simplify error handling
by allowing direct returns.

Reviewed-by: Claudiu Beznea <claudiu.beznea@tuxon.dev>
Link: https://patch.msgid.link/20250217141630.897334-22-jic23@kernel.org
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

+21 -21
+21 -21
drivers/iio/adc/at91-sama5d2_adc.c
··· 9 9 */ 10 10 11 11 #include <linux/bitops.h> 12 + #include <linux/cleanup.h> 12 13 #include <linux/clk.h> 13 14 #include <linux/delay.h> 14 15 #include <linux/dma-mapping.h> ··· 1815 1814 struct iio_chan_spec const *chan, int *val) 1816 1815 { 1817 1816 struct at91_adc_state *st = iio_priv(indio_dev); 1818 - int ret; 1819 1817 1820 - ret = iio_device_claim_direct_mode(indio_dev); 1821 - if (ret) 1822 - return ret; 1818 + guard(mutex)(&st->lock); 1823 1819 1824 - mutex_lock(&st->lock); 1825 - ret = at91_adc_read_info_raw(indio_dev, chan, val); 1826 - mutex_unlock(&st->lock); 1827 - 1828 - iio_device_release_direct_mode(indio_dev); 1829 - 1830 - return ret; 1820 + return at91_adc_read_info_raw(indio_dev, chan, val); 1831 1821 } 1832 1822 1833 1823 static void at91_adc_temp_sensor_configure(struct at91_adc_state *st, ··· 1863 1871 u32 tmp; 1864 1872 int ret, vbg, vtemp; 1865 1873 1866 - ret = iio_device_claim_direct_mode(indio_dev); 1867 - if (ret) 1868 - return ret; 1869 - mutex_lock(&st->lock); 1874 + guard(mutex)(&st->lock); 1870 1875 1871 1876 ret = pm_runtime_resume_and_get(st->dev); 1872 1877 if (ret < 0) 1873 - goto unlock; 1878 + return ret; 1874 1879 1875 1880 at91_adc_temp_sensor_configure(st, true); 1876 1881 ··· 1889 1900 at91_adc_temp_sensor_configure(st, false); 1890 1901 pm_runtime_mark_last_busy(st->dev); 1891 1902 pm_runtime_put_autosuspend(st->dev); 1892 - unlock: 1893 - mutex_unlock(&st->lock); 1894 - iio_device_release_direct_mode(indio_dev); 1895 1903 if (ret < 0) 1896 1904 return ret; 1897 1905 ··· 1910 1924 int *val, int *val2, long mask) 1911 1925 { 1912 1926 struct at91_adc_state *st = iio_priv(indio_dev); 1927 + int ret; 1913 1928 1914 1929 switch (mask) { 1915 1930 case IIO_CHAN_INFO_RAW: 1916 - return at91_adc_read_info_locked(indio_dev, chan, val); 1931 + ret = iio_device_claim_direct_mode(indio_dev); 1932 + if (ret) 1933 + return ret; 1934 + 1935 + ret = at91_adc_read_info_locked(indio_dev, chan, val); 1936 + iio_device_release_direct_mode(indio_dev); 1937 + return ret; 1917 1938 1918 1939 case IIO_CHAN_INFO_SCALE: 1919 1940 *val = st->vref_uv / 1000; ··· 1932 1939 case IIO_CHAN_INFO_PROCESSED: 1933 1940 if (chan->type != IIO_TEMP) 1934 1941 return -EINVAL; 1935 - return at91_adc_read_temp(indio_dev, chan, val); 1942 + ret = iio_device_claim_direct_mode(indio_dev); 1943 + if (ret) 1944 + return ret; 1945 + 1946 + ret = at91_adc_read_temp(indio_dev, chan, val); 1947 + iio_device_release_direct_mode(indio_dev); 1948 + 1949 + return ret; 1936 1950 1937 1951 case IIO_CHAN_INFO_SAMP_FREQ: 1938 1952 *val = at91_adc_get_sample_freq(st);