1313#include <linux/clk.h>
1414#include <linux/interrupt.h>
1515#include <linux/platform_device.h>
16+ #include <linux/pm_runtime.h>
1617
1718#include <media/videobuf2-core.h>
1819#include <media/v4l2-mem2mem.h>
@@ -300,20 +301,26 @@ void hevc_d_hw_irq_active2_irq(struct hevc_d_dev *dev,
300301}
301302
302303/*
303- * Stop the clock for this context
304- * clk_disable_unprepare does ref counting so this will not actually
305- * disable the clock if there are other running contexts
304+ * Power down the h/w
305+ * Called from runtime suspend (or directly if runtime PM is unavailable)
306306 */
307- void hevc_d_hw_stop_clock (struct hevc_d_dev * dev )
307+ void hevc_d_hw_power_off (struct hevc_d_dev * dev )
308308{
309+ dev_dbg (dev -> dev , "Power (clock) off\n" );
309310 clk_disable_unprepare (dev -> clock );
310311}
311312
312- /* Always starts the clock if it isn't already on this ctx */
313- int hevc_d_hw_start_clock (struct hevc_d_dev * dev )
313+ /*
314+ * Power up the h/w and (re)initialise any state that is not set per-job
315+ * Called from runtime resume (or directly if runtime PM is unavailable)
316+ */
317+ int hevc_d_hw_power_on (struct hevc_d_dev * dev )
314318{
319+ u32 irq_stat ;
315320 int rv ;
316321
322+ dev_dbg (dev -> dev , "Power (clock) on\n" );
323+
317324 rv = clk_set_min_rate (dev -> clock , dev -> max_clock_rate );
318325 if (rv ) {
319326 dev_err (dev -> dev , "Failed to set clock rate\n" );
@@ -325,22 +332,47 @@ int hevc_d_hw_start_clock(struct hevc_d_dev *dev)
325332 dev_err (dev -> dev , "Failed to enable clock\n" );
326333 return rv ;
327334 }
335+
336+ /*
337+ * Enable IRQs & reset anything pending
338+ * Whilst this seems the wrong way round the h/w doesn't actually
339+ * set the IRQ status bits till the IRQs are enabled. On probe we
340+ * haven't got the IRQ yet and on resume nothing is in flight so
341+ * this should still be safe.
342+ */
343+ irq_write (dev , ARG_IC_ICTRL ,
344+ ARG_IC_ICTRL_ACTIVE1_EN_SET | ARG_IC_ICTRL_ACTIVE2_EN_SET );
345+ irq_stat = irq_read (dev , ARG_IC_ICTRL );
346+ irq_write (dev , ARG_IC_ICTRL , irq_stat );
347+
328348 return 0 ;
329349}
330350
331- static int hw_setup (struct hevc_d_dev * dev )
351+ /*
352+ * Take a power reference for this context
353+ * Runtime PM does ref counting so the h/w stays powered whilst any
354+ * context holds a reference
355+ */
356+ int hevc_d_hw_pm_get (struct hevc_d_dev * dev )
357+ {
358+ int rv ;
359+
360+ rv = pm_runtime_resume_and_get (dev -> dev );
361+ if (rv )
362+ dev_err (dev -> dev , "Failed to power up: %d\n" , rv );
363+ return rv ;
364+ }
365+
366+ void hevc_d_hw_pm_put (struct hevc_d_dev * dev )
367+ {
368+ pm_runtime_put (dev -> dev );
369+ }
370+
371+ static int get_max_clock_rate (struct hevc_d_dev * dev )
332372{
333373 struct device_node * node ;
334- u32 ver ;
335- u32 irq_stat ;
336374 struct rpi_firmware * firmware ;
337375
338- ver = apb_read (dev , RPI_VERSION );
339- if (ver != 0x202 ) {
340- dev_err (dev -> dev , "Unexpected version %#x only 0x202 supported\n" , ver );
341- return - ENODEV ;
342- }
343-
344376 node = rpi_firmware_find_node ();
345377 if (!node )
346378 return - EINVAL ;
@@ -354,17 +386,19 @@ static int hw_setup(struct hevc_d_dev *dev)
354386 RPI_FIRMWARE_HEVC_CLK_ID );
355387 rpi_firmware_put (firmware );
356388
357- /*
358- * Enable IRQs & reset anything pending
359- * Whilst this seems the wrong way round the h/w doesn't actually
360- * set the IRQ status bits till the IRQs are enabled. As we haven't
361- * got the IRQ yet this should still be safe.
362- */
363- irq_write (dev , ARG_IC_ICTRL ,
364- ARG_IC_ICTRL_ACTIVE1_EN_SET | ARG_IC_ICTRL_ACTIVE2_EN_SET );
365- irq_stat = irq_read (dev , ARG_IC_ICTRL );
366- irq_write (dev , ARG_IC_ICTRL , irq_stat );
389+ return 0 ;
390+ }
391+
392+ /* H/w must be powered */
393+ static int check_version (struct hevc_d_dev * dev )
394+ {
395+ u32 ver ;
367396
397+ ver = apb_read (dev , RPI_VERSION );
398+ if (ver != 0x202 ) {
399+ dev_err (dev -> dev , "Unexpected version %#x only 0x202 supported\n" , ver );
400+ return - ENODEV ;
401+ }
368402 return 0 ;
369403}
370404
@@ -388,34 +422,58 @@ int hevc_d_hw_probe(struct hevc_d_dev *dev)
388422 if (IS_ERR (dev -> clock ))
389423 return PTR_ERR (dev -> clock );
390424
391- ret = clk_prepare_enable (dev -> clock );
425+ ret = get_max_clock_rate (dev );
392426 if (ret )
393427 return ret ;
394- ret = hw_setup (dev );
395- clk_disable_unprepare (dev -> clock );
428+
429+ /*
430+ * Power up manually so this works without runtime PM. Runtime PM is
431+ * told we are active once enabled and the driver core idles us after
432+ * probe completes.
433+ */
434+ ret = hevc_d_hw_power_on (dev );
396435 if (ret )
397436 return ret ;
398437
438+ ret = check_version (dev );
439+ if (ret )
440+ goto fail_power_off ;
441+
399442 irq_dec = platform_get_irq (dev -> pdev , 0 );
400- if (irq_dec < 0 )
401- return irq_dec ;
443+ if (irq_dec < 0 ) {
444+ ret = irq_dec ;
445+ goto fail_power_off ;
446+ }
402447 ret = devm_request_threaded_irq (dev -> dev , irq_dec ,
403448 hevc_d_irq_irq ,
404449 hevc_d_irq_thread ,
405450 0 , dev_name (dev -> dev ), dev );
406- if (ret )
451+ if (ret ) {
407452 dev_err (dev -> dev , "Failed to request IRQ - %d\n" , ret );
453+ goto fail_power_off ;
454+ }
455+
456+ pm_runtime_set_active (dev -> dev );
457+ pm_runtime_enable (dev -> dev );
408458
459+ return 0 ;
460+
461+ fail_power_off :
462+ hevc_d_hw_power_off (dev );
409463 return ret ;
410464}
411465
412466void hevc_d_hw_remove (struct hevc_d_dev * dev )
413467{
468+ pm_runtime_disable (dev -> dev );
469+ if (!pm_runtime_status_suspended (dev -> dev ))
470+ hevc_d_hw_power_off (dev );
471+ pm_runtime_set_suspended (dev -> dev );
472+
414473 /*
415474 * IRQ auto freed on unload so no need to do it here
416475 * ioremap auto freed on unload
417476 */
418477 ictl_uninit (& dev -> ic_active1 );
419478 ictl_uninit (& dev -> ic_active2 );
420479}
421-
0 commit comments