@@ -44,7 +44,8 @@ static struct sun4i_ss_alg_template ss_algs[] = {
4444 .cra_blocksize = MD5_HMAC_BLOCK_SIZE ,
4545 .cra_ctxsize = sizeof (struct sun4i_req_ctx ),
4646 .cra_module = THIS_MODULE ,
47- .cra_init = sun4i_hash_crainit
47+ .cra_init = sun4i_hash_crainit ,
48+ .cra_exit = sun4i_hash_craexit ,
4849 }
4950 }
5051 }
@@ -70,7 +71,8 @@ static struct sun4i_ss_alg_template ss_algs[] = {
7071 .cra_blocksize = SHA1_BLOCK_SIZE ,
7172 .cra_ctxsize = sizeof (struct sun4i_req_ctx ),
7273 .cra_module = THIS_MODULE ,
73- .cra_init = sun4i_hash_crainit
74+ .cra_init = sun4i_hash_crainit ,
75+ .cra_exit = sun4i_hash_craexit ,
7476 }
7577 }
7678 }
@@ -223,17 +225,26 @@ static struct sun4i_ss_alg_template ss_algs[] = {
223225#endif
224226};
225227
226- static void sun4i_ss_disable (struct sun4i_ss_ctx * ss )
228+ /*
229+ * Power management strategy: The device is suspended unless a TFM exists for
230+ * one of the algorithms proposed by this driver.
231+ */
232+ static int sun4i_ss_pm_suspend (struct device * dev )
227233{
234+ struct sun4i_ss_ctx * ss = dev_get_drvdata (dev );
235+
228236 if (ss -> reset )
229237 reset_control_assert (ss -> reset );
230238
231239 clk_disable_unprepare (ss -> ssclk );
232240 clk_disable_unprepare (ss -> busclk );
241+ return 0 ;
233242}
234243
235- static int sun4i_ss_enable (struct sun4i_ss_ctx * ss )
244+ static int sun4i_ss_pm_resume (struct device * dev )
236245{
246+ struct sun4i_ss_ctx * ss = dev_get_drvdata (dev );
247+
237248 int err ;
238249
239250 err = clk_prepare_enable (ss -> busclk );
@@ -258,10 +269,38 @@ static int sun4i_ss_enable(struct sun4i_ss_ctx *ss)
258269
259270 return err ;
260271err_enable :
261- sun4i_ss_disable (ss );
272+ sun4i_ss_pm_suspend (dev );
273+ return err ;
274+ }
275+
276+ const struct dev_pm_ops sun4i_ss_pm_ops = {
277+ SET_RUNTIME_PM_OPS (sun4i_ss_pm_suspend , sun4i_ss_pm_resume , NULL )
278+ };
279+
280+ /*
281+ * When power management is enabled, this function enables the PM and set the
282+ * device as suspended
283+ * When power management is disabled, this function just enables the device
284+ */
285+ static int sun4i_ss_pm_init (struct sun4i_ss_ctx * ss )
286+ {
287+ int err ;
288+
289+ pm_runtime_use_autosuspend (ss -> dev );
290+ pm_runtime_set_autosuspend_delay (ss -> dev , 2000 );
291+
292+ err = pm_runtime_set_suspended (ss -> dev );
293+ if (err )
294+ return err ;
295+ pm_runtime_enable (ss -> dev );
262296 return err ;
263297}
264298
299+ static void sun4i_ss_pm_exit (struct sun4i_ss_ctx * ss )
300+ {
301+ pm_runtime_disable (ss -> dev );
302+ }
303+
265304static int sun4i_ss_probe (struct platform_device * pdev )
266305{
267306 u32 v ;
@@ -308,18 +347,14 @@ static int sun4i_ss_probe(struct platform_device *pdev)
308347 ss -> reset = NULL ;
309348 }
310349
311- err = sun4i_ss_enable (ss );
312- if (err )
313- goto error_enable ;
314-
315350 /*
316351 * Check that clock have the correct rates given in the datasheet
317352 * Try to set the clock to the maximum allowed
318353 */
319354 err = clk_set_rate (ss -> ssclk , cr_mod );
320355 if (err ) {
321356 dev_err (& pdev -> dev , "Cannot set clock rate to ssclk\n" );
322- goto error_enable ;
357+ return err ;
323358 }
324359
325360 /*
@@ -347,22 +382,34 @@ static int sun4i_ss_probe(struct platform_device *pdev)
347382 dev_warn (& pdev -> dev , "Clock ss is at %lu (%lu MHz) (must be <= %lu)\n" ,
348383 cr , cr / 1000000 , cr_mod );
349384
385+ ss -> dev = & pdev -> dev ;
386+ platform_set_drvdata (pdev , ss );
387+
388+ spin_lock_init (& ss -> slock );
389+
390+ err = sun4i_ss_pm_init (ss );
391+ if (err )
392+ return err ;
393+
350394 /*
351395 * Datasheet named it "Die Bonding ID"
352396 * I expect to be a sort of Security System Revision number.
353397 * Since the A80 seems to have an other version of SS
354398 * this info could be useful
355399 */
400+
401+ err = pm_runtime_get_sync (ss -> dev );
402+ if (err < 0 )
403+ goto error_pm ;
404+
356405 writel (SS_ENABLED , ss -> base + SS_CTL );
357406 v = readl (ss -> base + SS_CTL );
358407 v >>= 16 ;
359408 v &= 0x07 ;
360409 dev_info (& pdev -> dev , "Die ID %d\n" , v );
361410 writel (0 , ss -> base + SS_CTL );
362411
363- ss -> dev = & pdev -> dev ;
364-
365- spin_lock_init (& ss -> slock );
412+ pm_runtime_put_sync (ss -> dev );
366413
367414 for (i = 0 ; i < ARRAY_SIZE (ss_algs ); i ++ ) {
368415 ss_algs [i ].ss = ss ;
@@ -392,7 +439,6 @@ static int sun4i_ss_probe(struct platform_device *pdev)
392439 break ;
393440 }
394441 }
395- platform_set_drvdata (pdev , ss );
396442 return 0 ;
397443error_alg :
398444 i -- ;
@@ -409,8 +455,8 @@ static int sun4i_ss_probe(struct platform_device *pdev)
409455 break ;
410456 }
411457 }
412- error_enable :
413- sun4i_ss_disable (ss );
458+ error_pm :
459+ sun4i_ss_pm_exit (ss );
414460 return err ;
415461}
416462
@@ -433,8 +479,7 @@ static int sun4i_ss_remove(struct platform_device *pdev)
433479 }
434480 }
435481
436- writel (0 , ss -> base + SS_CTL );
437- sun4i_ss_disable (ss );
482+ sun4i_ss_pm_exit (ss );
438483 return 0 ;
439484}
440485
@@ -449,6 +494,7 @@ static struct platform_driver sun4i_ss_driver = {
449494 .remove = sun4i_ss_remove ,
450495 .driver = {
451496 .name = "sun4i-ss" ,
497+ .pm = & sun4i_ss_pm_ops ,
452498 .of_match_table = a20ss_crypto_of_match_table ,
453499 },
454500};
0 commit comments