--- initial
+++ final
@@ -1,141 +1,135 @@
 static int at91_adc_probe(struct platform_device *pdev) {
   unsigned int prsc, mstrclk, ticks, adc_clk, shtim;
   int ret;
   struct iio_dev *idev;
   struct at91_adc_state *st;
   struct resource *res;
   u32 reg;
-  idev = iio_device_alloc(sizeof(struct at91_adc_state));
-  if (idev == NULL) {
-    ret = -ENOMEM;
-    goto error_ret;
-  }
+  idev = devm_iio_device_alloc(dev, sizeof(struct at91_adc_state));
+  if (idev == NULL) return -ENOMEM;
   st = iio_priv(idev);
   if (pdev->dev.of_node)
     ret = at91_adc_probe_dt(st, pdev);
   else
     ret = at91_adc_probe_pdata(st, pdev);
   if (ret) {
     dev_err(&pdev->dev, "No platform data available.\n");
     ret = -EINVAL;
-    goto error_free_device;
+    return ret;
   }
   platform_set_drvdata(pdev, idev);
   idev->dev.parent = &pdev->dev;
   idev->name = dev_name(&pdev->dev);
   idev->modes = INDIO_DIRECT_MODE;
   idev->info = &at91_adc_info;
   st->irq = platform_get_irq(pdev, 0);
   if (st->irq < 0) {
     dev_err(&pdev->dev, "No IRQ ID is designated\n");
     ret = -ENODEV;
-    goto error_free_device;
+    return ret;
   }
   res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
   st->reg_base = devm_ioremap_resource(&pdev->dev, res);
   if (IS_ERR(st->reg_base)) {
     ret = PTR_ERR(st->reg_base);
-    goto error_free_device;
+    return ret;
   }
   /*
    * Disable all IRQs before setting up the handler
    */
   at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
   at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
   ret = request_irq(st->irq, at91_adc_eoc_trigger, 0, pdev->dev.driver->name, idev);
   if (ret) {
     dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
-    goto error_free_device;
+    return ret;
   }
   st->clk = devm_clk_get(&pdev->dev, "adc_clk");
   if (IS_ERR(st->clk)) {
     dev_err(&pdev->dev, "Failed to get the clock.\n");
     ret = PTR_ERR(st->clk);
     goto error_free_irq;
   }
   ret = clk_prepare_enable(st->clk);
   if (ret) {
     dev_err(&pdev->dev, "Could not prepare or enable the clock.\n");
     goto error_free_irq;
   }
   st->adc_clk = devm_clk_get(&pdev->dev, "adc_op_clk");
   if (IS_ERR(st->adc_clk)) {
     dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
     ret = PTR_ERR(st->adc_clk);
     goto error_disable_clk;
   }
   ret = clk_prepare_enable(st->adc_clk);
   if (ret) {
     dev_err(&pdev->dev, "Could not prepare or enable the ADC clock.\n");
     goto error_disable_clk;
   }
   /*
    * Prescaler rate computation using the formula from the Atmel's
    * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
    * specified by the electrical characteristics of the board.
    */
   mstrclk = clk_get_rate(st->clk);
   adc_clk = clk_get_rate(st->adc_clk);
   prsc = (mstrclk / (2 * adc_clk)) - 1;
   if (!st->startup_time) {
     dev_err(&pdev->dev, "No startup time available.\n");
     ret = -EINVAL;
     goto error_disable_adc_clk;
   }
   /*
    * Number of ticks needed to cover the startup time of the ADC as
    * defined in the electrical characteristics of the board, divided by 8.
    * The formula thus is : Startup Time = (ticks + 1) * 8 / ADC Clock
    */
   ticks = round_up((st->startup_time * adc_clk / 1000000) - 1, 8) / 8;
   /*
    * a minimal Sample and Hold Time is necessary for the ADC to guarantee
    * the best converted final value between two channels selection
    * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock
    */
   shtim = round_up((st->sample_hold_time * adc_clk / 1000000) - 1, 1);
   reg = AT91_ADC_PRESCAL_(prsc) & AT91_ADC_PRESCAL;
   reg |= AT91_ADC_STARTUP_(ticks) & AT91_ADC_STARTUP;
   if (st->low_res) reg |= AT91_ADC_LOWRES;
   if (st->sleep_mode) reg |= AT91_ADC_SLEEP;
   reg |= AT91_ADC_SHTIM_(shtim) & AT91_ADC_SHTIM;
   at91_adc_writel(st, AT91_ADC_MR, reg);
   /* Setup the ADC channels available on the board */
   ret = at91_adc_channel_init(idev);
   if (ret < 0) {
     dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
     goto error_disable_adc_clk;
   }
   init_waitqueue_head(&st->wq_data_avail);
   mutex_init(&st->lock);
   ret = at91_adc_buffer_init(idev);
   if (ret < 0) {
     dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
     goto error_disable_adc_clk;
   }
   ret = at91_adc_trigger_init(idev);
   if (ret < 0) {
     dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
     goto error_unregister_buffer;
   }
   ret = iio_device_register(idev);
   if (ret < 0) {
     dev_err(&pdev->dev, "Couldn't register the device.\n");
     goto error_remove_triggers;
   }
   return 0;
 error_remove_triggers:
   at91_adc_trigger_remove(idev);
 error_unregister_buffer:
   at91_adc_buffer_remove(idev);
 error_disable_adc_clk:
   clk_disable_unprepare(st->adc_clk);
 error_disable_clk:
   clk_disable_unprepare(st->clk);
 error_free_irq:
   free_irq(st->irq, idev);
-error_free_device:
-  iio_device_free(idev);
-error_ret:
   return ret;
 }<sep>@@
expression e,e1,e2,ret;
identifier l1,l2,x,f;
type T;
@@
f(T x) {
	... when != l2
(
-       e = iio_device_alloc(e1);
+       e = devm_iio_device_alloc(dev, e1);
        if (e == NULL)
(
-	{
-               ret = e2;
-               goto l2;
+               return e2;
-       }
|
	{
		...
-               ret = e2;
		...
-               goto l2;
+               return e2;
       }
)
<...
-	goto l1;
+	return ret;
...>
&
e = iio_device_alloc(e1);
... when exists
-l1:
-       iio_device_free(e);
-l2:
        return ret;
)
}