Version:  2.0.40 2.2.26 2.4.37 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6

Linux/drivers/regulator/s2mps11.c

  1 /*
  2  * s2mps11.c
  3  *
  4  * Copyright (c) 2012-2014 Samsung Electronics Co., Ltd
  5  *              http://www.samsung.com
  6  *
  7  * This program is free software; you can redistribute  it and/or modify it
  8  * under  the terms of  the GNU General  Public License as published by the
  9  * Free Software Foundation;  either version 2 of the  License, or (at your
 10  * option) any later version.
 11  *
 12  * This program is distributed in the hope that it will be useful,
 13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  * GNU General Public License for more details.
 16  *
 17  */
 18 
 19 #include <linux/bug.h>
 20 #include <linux/err.h>
 21 #include <linux/gpio.h>
 22 #include <linux/slab.h>
 23 #include <linux/module.h>
 24 #include <linux/of.h>
 25 #include <linux/regmap.h>
 26 #include <linux/platform_device.h>
 27 #include <linux/regulator/driver.h>
 28 #include <linux/regulator/machine.h>
 29 #include <linux/regulator/of_regulator.h>
 30 #include <linux/of_gpio.h>
 31 #include <linux/mfd/samsung/core.h>
 32 #include <linux/mfd/samsung/s2mps11.h>
 33 #include <linux/mfd/samsung/s2mps13.h>
 34 #include <linux/mfd/samsung/s2mps14.h>
 35 #include <linux/mfd/samsung/s2mps15.h>
 36 #include <linux/mfd/samsung/s2mpu02.h>
 37 
 38 /* The highest number of possible regulators for supported devices. */
 39 #define S2MPS_REGULATOR_MAX             S2MPS13_REGULATOR_MAX
 40 struct s2mps11_info {
 41         int ramp_delay2;
 42         int ramp_delay34;
 43         int ramp_delay5;
 44         int ramp_delay16;
 45         int ramp_delay7810;
 46         int ramp_delay9;
 47 
 48         enum sec_device_type dev_type;
 49 
 50         /*
 51          * One bit for each S2MPS13/S2MPS14/S2MPU02 regulator whether
 52          * the suspend mode was enabled.
 53          */
 54         DECLARE_BITMAP(suspend_state, S2MPS_REGULATOR_MAX);
 55 
 56         /*
 57          * Array (size: number of regulators) with GPIO-s for external
 58          * sleep control.
 59          */
 60         int *ext_control_gpio;
 61 };
 62 
 63 static int get_ramp_delay(int ramp_delay)
 64 {
 65         unsigned char cnt = 0;
 66 
 67         ramp_delay /= 6250;
 68 
 69         while (true) {
 70                 ramp_delay = ramp_delay >> 1;
 71                 if (ramp_delay == 0)
 72                         break;
 73                 cnt++;
 74         }
 75 
 76         if (cnt > 3)
 77                 cnt = 3;
 78 
 79         return cnt;
 80 }
 81 
 82 static int s2mps11_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
 83                                    unsigned int old_selector,
 84                                    unsigned int new_selector)
 85 {
 86         struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
 87         unsigned int ramp_delay = 0;
 88         int old_volt, new_volt;
 89 
 90         switch (rdev_get_id(rdev)) {
 91         case S2MPS11_BUCK2:
 92                 ramp_delay = s2mps11->ramp_delay2;
 93                 break;
 94         case S2MPS11_BUCK3:
 95         case S2MPS11_BUCK4:
 96                 ramp_delay = s2mps11->ramp_delay34;
 97                 break;
 98         case S2MPS11_BUCK5:
 99                 ramp_delay = s2mps11->ramp_delay5;
100                 break;
101         case S2MPS11_BUCK6:
102         case S2MPS11_BUCK1:
103                 ramp_delay = s2mps11->ramp_delay16;
104                 break;
105         case S2MPS11_BUCK7:
106         case S2MPS11_BUCK8:
107         case S2MPS11_BUCK10:
108                 ramp_delay = s2mps11->ramp_delay7810;
109                 break;
110         case S2MPS11_BUCK9:
111                 ramp_delay = s2mps11->ramp_delay9;
112         }
113 
114         if (ramp_delay == 0)
115                 ramp_delay = rdev->desc->ramp_delay;
116 
117         old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
118         new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
119 
120         return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
121 }
122 
123 static int s2mps11_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
124 {
125         struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
126         unsigned int ramp_val, ramp_shift, ramp_reg = S2MPS11_REG_RAMP_BUCK;
127         unsigned int ramp_enable = 1, enable_shift = 0;
128         int ret;
129 
130         switch (rdev_get_id(rdev)) {
131         case S2MPS11_BUCK1:
132                 if (ramp_delay > s2mps11->ramp_delay16)
133                         s2mps11->ramp_delay16 = ramp_delay;
134                 else
135                         ramp_delay = s2mps11->ramp_delay16;
136 
137                 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
138                 break;
139         case S2MPS11_BUCK2:
140                 enable_shift = S2MPS11_BUCK2_RAMP_EN_SHIFT;
141                 if (!ramp_delay) {
142                         ramp_enable = 0;
143                         break;
144                 }
145 
146                 s2mps11->ramp_delay2 = ramp_delay;
147                 ramp_shift = S2MPS11_BUCK2_RAMP_SHIFT;
148                 ramp_reg = S2MPS11_REG_RAMP;
149                 break;
150         case S2MPS11_BUCK3:
151                 enable_shift = S2MPS11_BUCK3_RAMP_EN_SHIFT;
152                 if (!ramp_delay) {
153                         ramp_enable = 0;
154                         break;
155                 }
156 
157                 if (ramp_delay > s2mps11->ramp_delay34)
158                         s2mps11->ramp_delay34 = ramp_delay;
159                 else
160                         ramp_delay = s2mps11->ramp_delay34;
161 
162                 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
163                 ramp_reg = S2MPS11_REG_RAMP;
164                 break;
165         case S2MPS11_BUCK4:
166                 enable_shift = S2MPS11_BUCK4_RAMP_EN_SHIFT;
167                 if (!ramp_delay) {
168                         ramp_enable = 0;
169                         break;
170                 }
171 
172                 if (ramp_delay > s2mps11->ramp_delay34)
173                         s2mps11->ramp_delay34 = ramp_delay;
174                 else
175                         ramp_delay = s2mps11->ramp_delay34;
176 
177                 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
178                 ramp_reg = S2MPS11_REG_RAMP;
179                 break;
180         case S2MPS11_BUCK5:
181                 s2mps11->ramp_delay5 = ramp_delay;
182                 ramp_shift = S2MPS11_BUCK5_RAMP_SHIFT;
183                 break;
184         case S2MPS11_BUCK6:
185                 enable_shift = S2MPS11_BUCK6_RAMP_EN_SHIFT;
186                 if (!ramp_delay) {
187                         ramp_enable = 0;
188                         break;
189                 }
190 
191                 if (ramp_delay > s2mps11->ramp_delay16)
192                         s2mps11->ramp_delay16 = ramp_delay;
193                 else
194                         ramp_delay = s2mps11->ramp_delay16;
195 
196                 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
197                 break;
198         case S2MPS11_BUCK7:
199         case S2MPS11_BUCK8:
200         case S2MPS11_BUCK10:
201                 if (ramp_delay > s2mps11->ramp_delay7810)
202                         s2mps11->ramp_delay7810 = ramp_delay;
203                 else
204                         ramp_delay = s2mps11->ramp_delay7810;
205 
206                 ramp_shift = S2MPS11_BUCK7810_RAMP_SHIFT;
207                 break;
208         case S2MPS11_BUCK9:
209                 s2mps11->ramp_delay9 = ramp_delay;
210                 ramp_shift = S2MPS11_BUCK9_RAMP_SHIFT;
211                 break;
212         default:
213                 return 0;
214         }
215 
216         if (!ramp_enable)
217                 goto ramp_disable;
218 
219         /* Ramp delay can be enabled/disabled only for buck[2346] */
220         if ((rdev_get_id(rdev) >= S2MPS11_BUCK2 &&
221                         rdev_get_id(rdev) <= S2MPS11_BUCK4) ||
222                         rdev_get_id(rdev) == S2MPS11_BUCK6)  {
223                 ret = regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
224                                          1 << enable_shift, 1 << enable_shift);
225                 if (ret) {
226                         dev_err(&rdev->dev, "failed to enable ramp rate\n");
227                         return ret;
228                 }
229         }
230 
231         ramp_val = get_ramp_delay(ramp_delay);
232 
233         return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
234                                   ramp_val << ramp_shift);
235 
236 ramp_disable:
237         return regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
238                                   1 << enable_shift, 0);
239 }
240 
241 static struct regulator_ops s2mps11_ldo_ops = {
242         .list_voltage           = regulator_list_voltage_linear,
243         .map_voltage            = regulator_map_voltage_linear,
244         .is_enabled             = regulator_is_enabled_regmap,
245         .enable                 = regulator_enable_regmap,
246         .disable                = regulator_disable_regmap,
247         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
248         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
249         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
250 };
251 
252 static struct regulator_ops s2mps11_buck_ops = {
253         .list_voltage           =