@@ -188,64 +188,60 @@ void ahci_platform_disable_resources(struct ahci_host_priv *hpriv)
188188}
189189EXPORT_SYMBOL_GPL (ahci_platform_disable_resources );
190190
191- static void ahci_put_clks (struct ahci_host_priv * hpriv )
191+ static void ahci_platform_put_resources (struct device * dev , void * res )
192192{
193+ struct ahci_host_priv * hpriv = res ;
193194 int c ;
194195
195196 for (c = 0 ; c < AHCI_MAX_CLKS && hpriv -> clks [c ]; c ++ )
196197 clk_put (hpriv -> clks [c ]);
197198}
198199
199- static int ahci_probe (struct platform_device * pdev )
200+ /**
201+ * ahci_platform_get_resources - Get platform resources
202+ * @pdev: platform device to get resources for
203+ *
204+ * This function allocates an ahci_host_priv struct, and gets the following
205+ * resources, storing a reference to them inside the returned struct:
206+ *
207+ * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
208+ * 2) regulator for controlling the targets power (optional)
209+ * 3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
210+ * or for non devicetree enabled platforms a single clock
211+ *
212+ * RETURNS:
213+ * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
214+ */
215+ struct ahci_host_priv * ahci_platform_get_resources (
216+ struct platform_device * pdev )
200217{
201218 struct device * dev = & pdev -> dev ;
202- struct ahci_platform_data * pdata = dev_get_platdata (dev );
203- const struct platform_device_id * id = platform_get_device_id (pdev );
204- struct ata_port_info pi = ahci_port_info [id ? id -> driver_data : 0 ];
205- const struct ata_port_info * ppi [] = { & pi , NULL };
206219 struct ahci_host_priv * hpriv ;
207- struct ata_host * host ;
208- struct resource * mem ;
209220 struct clk * clk ;
210- int irq ;
211- int n_ports ;
212- int i ;
213- int rc ;
221+ int i , rc = - ENOMEM ;
214222
215- mem = platform_get_resource (pdev , IORESOURCE_MEM , 0 );
216- if (!mem ) {
217- dev_err (dev , "no mmio space\n" );
218- return - EINVAL ;
219- }
220-
221- irq = platform_get_irq (pdev , 0 );
222- if (irq <= 0 ) {
223- dev_err (dev , "no irq\n" );
224- return - EINVAL ;
225- }
223+ if (!devres_open_group (dev , NULL , GFP_KERNEL ))
224+ return ERR_PTR (- ENOMEM );
226225
227- if (pdata && pdata -> ata_port_info )
228- pi = * pdata -> ata_port_info ;
229-
230- hpriv = devm_kzalloc (dev , sizeof (* hpriv ), GFP_KERNEL );
231- if (!hpriv ) {
232- dev_err (dev , "can't alloc ahci_host_priv\n" );
233- return - ENOMEM ;
234- }
226+ hpriv = devres_alloc (ahci_platform_put_resources , sizeof (* hpriv ),
227+ GFP_KERNEL );
228+ if (!hpriv )
229+ goto err_out ;
235230
236- hpriv -> flags |= ( unsigned long ) pi . private_data ;
231+ devres_add ( dev , hpriv ) ;
237232
238- hpriv -> mmio = devm_ioremap (dev , mem -> start , resource_size (mem ));
233+ hpriv -> mmio = devm_ioremap_resource (dev ,
234+ platform_get_resource (pdev , IORESOURCE_MEM , 0 ));
239235 if (!hpriv -> mmio ) {
240- dev_err (dev , "can't map %pR \n" , mem );
241- return - ENOMEM ;
236+ dev_err (dev , "no mmio space \n" );
237+ goto err_out ;
242238 }
243239
244240 hpriv -> target_pwr = devm_regulator_get_optional (dev , "target" );
245241 if (IS_ERR (hpriv -> target_pwr )) {
246242 rc = PTR_ERR (hpriv -> target_pwr );
247243 if (rc == - EPROBE_DEFER )
248- return - EPROBE_DEFER ;
244+ goto err_out ;
249245 hpriv -> target_pwr = NULL ;
250246 }
251247
@@ -264,33 +260,59 @@ static int ahci_probe(struct platform_device *pdev)
264260 if (IS_ERR (clk )) {
265261 rc = PTR_ERR (clk );
266262 if (rc == - EPROBE_DEFER )
267- goto free_clk ;
263+ goto err_out ;
268264 break ;
269265 }
270266 hpriv -> clks [i ] = clk ;
271267 }
272268
273- rc = ahci_platform_enable_resources (hpriv );
274- if (rc )
275- goto free_clk ;
269+ devres_remove_group (dev , NULL );
270+ return hpriv ;
276271
277- /*
278- * Some platforms might need to prepare for mmio region access,
279- * which could be done in the following init call. So, the mmio
280- * region shouldn't be accessed before init (if provided) has
281- * returned successfully.
282- */
283- if (pdata && pdata -> init ) {
284- rc = pdata -> init (dev , hpriv -> mmio );
285- if (rc )
286- goto disable_resources ;
287- }
272+ err_out :
273+ devres_release_group (dev , NULL );
274+ return ERR_PTR (rc );
275+ }
276+ EXPORT_SYMBOL_GPL (ahci_platform_get_resources );
277+
278+ /**
279+ * ahci_platform_init_host - Bring up an ahci-platform host
280+ * @pdev: platform device pointer for the host
281+ * @hpriv: ahci-host private data for the host
282+ * @pi_template: template for the ata_port_info to use
283+ * @force_port_map: param passed to ahci_save_initial_config
284+ * @mask_port_map: param passed to ahci_save_initial_config
285+ *
286+ * This function does all the usual steps needed to bring up an
287+ * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
288+ * must be initialized / enabled before calling this.
289+ *
290+ * RETURNS:
291+ * 0 on success otherwise a negative error code
292+ */
293+ int ahci_platform_init_host (struct platform_device * pdev ,
294+ struct ahci_host_priv * hpriv ,
295+ const struct ata_port_info * pi_template ,
296+ unsigned int force_port_map ,
297+ unsigned int mask_port_map )
298+ {
299+ struct device * dev = & pdev -> dev ;
300+ struct ata_port_info pi = * pi_template ;
301+ const struct ata_port_info * ppi [] = { & pi , NULL };
302+ struct ata_host * host ;
303+ int i , irq , n_ports , rc ;
288304
289- ahci_save_initial_config (dev , hpriv ,
290- pdata ? pdata -> force_port_map : 0 ,
291- pdata ? pdata -> mask_port_map : 0 );
305+ irq = platform_get_irq (pdev , 0 );
306+ if (irq <= 0 ) {
307+ dev_err (dev , "no irq\n" );
308+ return - EINVAL ;
309+ }
292310
293311 /* prepare host */
312+ hpriv -> flags |= (unsigned long )pi .private_data ;
313+
314+ ahci_save_initial_config (dev , hpriv , force_port_map , mask_port_map );
315+
294316 if (hpriv -> cap & HOST_CAP_NCQ )
295317 pi .flags |= ATA_FLAG_NCQ ;
296318
@@ -307,10 +329,8 @@ static int ahci_probe(struct platform_device *pdev)
307329 n_ports = max (ahci_nr_ports (hpriv -> cap ), fls (hpriv -> port_map ));
308330
309331 host = ata_host_alloc_pinfo (dev , ppi , n_ports );
310- if (!host ) {
311- rc = - ENOMEM ;
312- goto pdata_exit ;
313- }
332+ if (!host )
333+ return - ENOMEM ;
314334
315335 host -> private_data = hpriv ;
316336
@@ -325,7 +345,8 @@ static int ahci_probe(struct platform_device *pdev)
325345 for (i = 0 ; i < host -> n_ports ; i ++ ) {
326346 struct ata_port * ap = host -> ports [i ];
327347
328- ata_port_desc (ap , "mmio %pR" , mem );
348+ ata_port_desc (ap , "mmio %pR" ,
349+ platform_get_resource (pdev , IORESOURCE_MEM , 0 ));
329350 ata_port_desc (ap , "port 0x%x" , 0x100 + ap -> port_no * 0x80 );
330351
331352 /* set enclosure management message type */
@@ -339,13 +360,53 @@ static int ahci_probe(struct platform_device *pdev)
339360
340361 rc = ahci_reset_controller (host );
341362 if (rc )
342- goto pdata_exit ;
363+ return rc ;
343364
344365 ahci_init_controller (host );
345366 ahci_print_info (host , "platform" );
346367
347- rc = ata_host_activate (host , irq , ahci_interrupt , IRQF_SHARED ,
348- & ahci_platform_sht );
368+ return ata_host_activate (host , irq , ahci_interrupt , IRQF_SHARED ,
369+ & ahci_platform_sht );
370+ }
371+ EXPORT_SYMBOL_GPL (ahci_platform_init_host );
372+
373+ static int ahci_probe (struct platform_device * pdev )
374+ {
375+ struct device * dev = & pdev -> dev ;
376+ struct ahci_platform_data * pdata = dev_get_platdata (dev );
377+ const struct platform_device_id * id = platform_get_device_id (pdev );
378+ const struct ata_port_info * pi_template ;
379+ struct ahci_host_priv * hpriv ;
380+ int rc ;
381+
382+ hpriv = ahci_platform_get_resources (pdev );
383+ if (IS_ERR (hpriv ))
384+ return PTR_ERR (hpriv );
385+
386+ rc = ahci_platform_enable_resources (hpriv );
387+ if (rc )
388+ return rc ;
389+
390+ /*
391+ * Some platforms might need to prepare for mmio region access,
392+ * which could be done in the following init call. So, the mmio
393+ * region shouldn't be accessed before init (if provided) has
394+ * returned successfully.
395+ */
396+ if (pdata && pdata -> init ) {
397+ rc = pdata -> init (dev , hpriv -> mmio );
398+ if (rc )
399+ goto disable_resources ;
400+ }
401+
402+ if (pdata && pdata -> ata_port_info )
403+ pi_template = pdata -> ata_port_info ;
404+ else
405+ pi_template = & ahci_port_info [id ? id -> driver_data : 0 ];
406+
407+ rc = ahci_platform_init_host (pdev , hpriv , pi_template ,
408+ pdata ? pdata -> force_port_map : 0 ,
409+ pdata ? pdata -> mask_port_map : 0 );
349410 if (rc )
350411 goto pdata_exit ;
351412
@@ -355,8 +416,6 @@ static int ahci_probe(struct platform_device *pdev)
355416 pdata -> exit (dev );
356417disable_resources :
357418 ahci_platform_disable_resources (hpriv );
358- free_clk :
359- ahci_put_clks (hpriv );
360419 return rc ;
361420}
362421
@@ -370,7 +429,6 @@ static void ahci_host_stop(struct ata_host *host)
370429 pdata -> exit (dev );
371430
372431 ahci_platform_disable_resources (hpriv );
373- ahci_put_clks (hpriv );
374432}
375433
376434#ifdef CONFIG_PM_SLEEP
0 commit comments