GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / soc / actions / owl-sps-helper.c
1 /*
2  * Actions Semi Owl Smart Power System (SPS) shared helpers
3  *
4  * Copyright 2012 Actions Semi Inc.
5  * Author: Actions Semi, Inc.
6  *
7  * Copyright (c) 2017 Andreas Färber
8  *
9  * This program is free software; you can redistribute  it and/or modify it
10  * under  the terms of  the GNU General  Public License as published by the
11  * Free Software Foundation;  either version 2 of the  License, or (at your
12  * option) any later version.
13  */
14
15 #include <linux/delay.h>
16 #include <linux/io.h>
17
18 #define OWL_SPS_PG_CTL  0x0
19
20 int owl_sps_set_pg(void __iomem *base, u32 pwr_mask, u32 ack_mask, bool enable)
21 {
22         u32 val;
23         bool ack;
24         int timeout;
25
26         val = readl(base + OWL_SPS_PG_CTL);
27         ack = val & ack_mask;
28         if (ack == enable)
29                 return 0;
30
31         if (enable)
32                 val |= pwr_mask;
33         else
34                 val &= ~pwr_mask;
35
36         writel(val, base + OWL_SPS_PG_CTL);
37
38         for (timeout = 5000; timeout > 0; timeout -= 50) {
39                 val = readl(base + OWL_SPS_PG_CTL);
40                 if ((val & ack_mask) == (enable ? ack_mask : 0))
41                         break;
42                 udelay(50);
43         }
44         if (timeout <= 0)
45                 return -ETIMEDOUT;
46
47         udelay(10);
48
49         return 0;
50 }
51 EXPORT_SYMBOL_GPL(owl_sps_set_pg);