Merge tag 'reset-for-v5.3' of git://git.pengutronix.de/git/pza/linux into arm/drivers
[sfrench/cifs-2.6.git] / drivers / regulator / ab8500-ext.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) ST-Ericsson SA 2010
4  *
5  * Authors: Bengt Jonsson <bengt.g.jonsson@stericsson.com>
6  *
7  * This file is based on drivers/regulator/ab8500.c
8  *
9  * AB8500 external regulators
10  *
11  * ab8500-ext supports the following regulators:
12  * - VextSupply3
13  */
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/err.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/of_regulator.h>
23 #include <linux/mfd/abx500.h>
24 #include <linux/mfd/abx500/ab8500.h>
25 #include <linux/regulator/ab8500.h>
26
27 static struct regulator_consumer_supply ab8500_vaux1_consumers[] = {
28         /* Main display, u8500 R3 uib */
29         REGULATOR_SUPPLY("vddi", "mcde_disp_sony_acx424akp.0"),
30         /* Main display, u8500 uib and ST uib */
31         REGULATOR_SUPPLY("vdd1", "samsung_s6d16d0.0"),
32         /* Secondary display, ST uib */
33         REGULATOR_SUPPLY("vdd1", "samsung_s6d16d0.1"),
34         /* SFH7741 proximity sensor */
35         REGULATOR_SUPPLY("vcc", "gpio-keys.0"),
36         /* BH1780GLS ambient light sensor */
37         REGULATOR_SUPPLY("vcc", "2-0029"),
38         /* lsm303dlh accelerometer */
39         REGULATOR_SUPPLY("vdd", "2-0018"),
40         /* lsm303dlhc accelerometer */
41         REGULATOR_SUPPLY("vdd", "2-0019"),
42         /* lsm303dlh magnetometer */
43         REGULATOR_SUPPLY("vdd", "2-001e"),
44         /* Rohm BU21013 Touchscreen devices */
45         REGULATOR_SUPPLY("avdd", "3-005c"),
46         REGULATOR_SUPPLY("avdd", "3-005d"),
47         /* Synaptics RMI4 Touchscreen device */
48         REGULATOR_SUPPLY("vdd", "3-004b"),
49         /* L3G4200D Gyroscope device */
50         REGULATOR_SUPPLY("vdd", "2-0068"),
51         /* Ambient light sensor device */
52         REGULATOR_SUPPLY("vdd", "3-0029"),
53         /* Pressure sensor device */
54         REGULATOR_SUPPLY("vdd", "2-005c"),
55         /* Cypress TrueTouch Touchscreen device */
56         REGULATOR_SUPPLY("vcpin", "spi8.0"),
57         /* Camera device */
58         REGULATOR_SUPPLY("vaux12v5", "mmio_camera"),
59 };
60
61 static struct regulator_consumer_supply ab8500_vaux2_consumers[] = {
62         /* On-board eMMC power */
63         REGULATOR_SUPPLY("vmmc", "sdi4"),
64         /* AB8500 audio codec */
65         REGULATOR_SUPPLY("vcc-N2158", "ab8500-codec.0"),
66         /* AB8500 accessory detect 1 */
67         REGULATOR_SUPPLY("vcc-N2158", "ab8500-acc-det.0"),
68         /* AB8500 Tv-out device */
69         REGULATOR_SUPPLY("vcc-N2158", "mcde_tv_ab8500.4"),
70         /* AV8100 HDMI device */
71         REGULATOR_SUPPLY("vcc-N2158", "av8100_hdmi.3"),
72 };
73
74 static struct regulator_consumer_supply ab8500_vaux3_consumers[] = {
75         REGULATOR_SUPPLY("v-SD-STM", "stm"),
76         /* External MMC slot power */
77         REGULATOR_SUPPLY("vmmc", "sdi0"),
78 };
79
80 static struct regulator_consumer_supply ab8500_vtvout_consumers[] = {
81         /* TV-out DENC supply */
82         REGULATOR_SUPPLY("vtvout", "ab8500-denc.0"),
83         /* Internal general-purpose ADC */
84         REGULATOR_SUPPLY("vddadc", "ab8500-gpadc.0"),
85         /* ADC for charger */
86         REGULATOR_SUPPLY("vddadc", "ab8500-charger.0"),
87         /* AB8500 Tv-out device */
88         REGULATOR_SUPPLY("vtvout", "mcde_tv_ab8500.4"),
89 };
90
91 static struct regulator_consumer_supply ab8500_vaud_consumers[] = {
92         /* AB8500 audio-codec main supply */
93         REGULATOR_SUPPLY("vaud", "ab8500-codec.0"),
94 };
95
96 static struct regulator_consumer_supply ab8500_vamic1_consumers[] = {
97         /* AB8500 audio-codec Mic1 supply */
98         REGULATOR_SUPPLY("vamic1", "ab8500-codec.0"),
99 };
100
101 static struct regulator_consumer_supply ab8500_vamic2_consumers[] = {
102         /* AB8500 audio-codec Mic2 supply */
103         REGULATOR_SUPPLY("vamic2", "ab8500-codec.0"),
104 };
105
106 static struct regulator_consumer_supply ab8500_vdmic_consumers[] = {
107         /* AB8500 audio-codec DMic supply */
108         REGULATOR_SUPPLY("vdmic", "ab8500-codec.0"),
109 };
110
111 static struct regulator_consumer_supply ab8500_vintcore_consumers[] = {
112         /* SoC core supply, no device */
113         REGULATOR_SUPPLY("v-intcore", NULL),
114         /* USB Transceiver */
115         REGULATOR_SUPPLY("vddulpivio18", "ab8500-usb.0"),
116         /* Handled by abx500 clk driver */
117         REGULATOR_SUPPLY("v-intcore", "abx500-clk.0"),
118 };
119
120 static struct regulator_consumer_supply ab8500_vana_consumers[] = {
121         /* DB8500 DSI */
122         REGULATOR_SUPPLY("vdddsi1v2", "mcde"),
123         REGULATOR_SUPPLY("vdddsi1v2", "b2r2_core"),
124         REGULATOR_SUPPLY("vdddsi1v2", "b2r2_1_core"),
125         REGULATOR_SUPPLY("vdddsi1v2", "dsilink.0"),
126         REGULATOR_SUPPLY("vdddsi1v2", "dsilink.1"),
127         REGULATOR_SUPPLY("vdddsi1v2", "dsilink.2"),
128         /* DB8500 CSI */
129         REGULATOR_SUPPLY("vddcsi1v2", "mmio_camera"),
130 };
131
132 /* ab8500 regulator register initialization */
133 static struct ab8500_regulator_reg_init ab8500_reg_init[] = {
134         /*
135          * VanaRequestCtrl          = HP/LP depending on VxRequest
136          * VextSupply1RequestCtrl   = HP/LP depending on VxRequest
137          */
138         INIT_REGULATOR_REGISTER(AB8500_REGUREQUESTCTRL2,       0xf0, 0x00),
139         /*
140          * VextSupply2RequestCtrl   = HP/LP depending on VxRequest
141          * VextSupply3RequestCtrl   = HP/LP depending on VxRequest
142          * Vaux1RequestCtrl         = HP/LP depending on VxRequest
143          * Vaux2RequestCtrl         = HP/LP depending on VxRequest
144          */
145         INIT_REGULATOR_REGISTER(AB8500_REGUREQUESTCTRL3,       0xff, 0x00),
146         /*
147          * Vaux3RequestCtrl         = HP/LP depending on VxRequest
148          * SwHPReq                  = Control through SWValid disabled
149          */
150         INIT_REGULATOR_REGISTER(AB8500_REGUREQUESTCTRL4,       0x07, 0x00),
151         /*
152          * VanaSysClkReq1HPValid    = disabled
153          * Vaux1SysClkReq1HPValid   = disabled
154          * Vaux2SysClkReq1HPValid   = disabled
155          * Vaux3SysClkReq1HPValid   = disabled
156          */
157         INIT_REGULATOR_REGISTER(AB8500_REGUSYSCLKREQ1HPVALID1, 0xe8, 0x00),
158         /*
159          * VextSupply1SysClkReq1HPValid = disabled
160          * VextSupply2SysClkReq1HPValid = disabled
161          * VextSupply3SysClkReq1HPValid = SysClkReq1 controlled
162          */
163         INIT_REGULATOR_REGISTER(AB8500_REGUSYSCLKREQ1HPVALID2, 0x70, 0x40),
164         /*
165          * VanaHwHPReq1Valid        = disabled
166          * Vaux1HwHPreq1Valid       = disabled
167          * Vaux2HwHPReq1Valid       = disabled
168          * Vaux3HwHPReqValid        = disabled
169          */
170         INIT_REGULATOR_REGISTER(AB8500_REGUHWHPREQ1VALID1,     0xe8, 0x00),
171         /*
172          * VextSupply1HwHPReq1Valid = disabled
173          * VextSupply2HwHPReq1Valid = disabled
174          * VextSupply3HwHPReq1Valid = disabled
175          */
176         INIT_REGULATOR_REGISTER(AB8500_REGUHWHPREQ1VALID2,     0x07, 0x00),
177         /*
178          * VanaHwHPReq2Valid        = disabled
179          * Vaux1HwHPReq2Valid       = disabled
180          * Vaux2HwHPReq2Valid       = disabled
181          * Vaux3HwHPReq2Valid       = disabled
182          */
183         INIT_REGULATOR_REGISTER(AB8500_REGUHWHPREQ2VALID1,     0xe8, 0x00),
184         /*
185          * VextSupply1HwHPReq2Valid = disabled
186          * VextSupply2HwHPReq2Valid = disabled
187          * VextSupply3HwHPReq2Valid = HWReq2 controlled
188          */
189         INIT_REGULATOR_REGISTER(AB8500_REGUHWHPREQ2VALID2,     0x07, 0x04),
190         /*
191          * VanaSwHPReqValid         = disabled
192          * Vaux1SwHPReqValid        = disabled
193          */
194         INIT_REGULATOR_REGISTER(AB8500_REGUSWHPREQVALID1,      0xa0, 0x00),
195         /*
196          * Vaux2SwHPReqValid        = disabled
197          * Vaux3SwHPReqValid        = disabled
198          * VextSupply1SwHPReqValid  = disabled
199          * VextSupply2SwHPReqValid  = disabled
200          * VextSupply3SwHPReqValid  = disabled
201          */
202         INIT_REGULATOR_REGISTER(AB8500_REGUSWHPREQVALID2,      0x1f, 0x00),
203         /*
204          * SysClkReq2Valid1         = SysClkReq2 controlled
205          * SysClkReq3Valid1         = disabled
206          * SysClkReq4Valid1         = SysClkReq4 controlled
207          * SysClkReq5Valid1         = disabled
208          * SysClkReq6Valid1         = SysClkReq6 controlled
209          * SysClkReq7Valid1         = disabled
210          * SysClkReq8Valid1         = disabled
211          */
212         INIT_REGULATOR_REGISTER(AB8500_REGUSYSCLKREQVALID1,    0xfe, 0x2a),
213         /*
214          * SysClkReq2Valid2         = disabled
215          * SysClkReq3Valid2         = disabled
216          * SysClkReq4Valid2         = disabled
217          * SysClkReq5Valid2         = disabled
218          * SysClkReq6Valid2         = SysClkReq6 controlled
219          * SysClkReq7Valid2         = disabled
220          * SysClkReq8Valid2         = disabled
221          */
222         INIT_REGULATOR_REGISTER(AB8500_REGUSYSCLKREQVALID2,    0xfe, 0x20),
223         /*
224          * VTVoutEna                = disabled
225          * Vintcore12Ena            = disabled
226          * Vintcore12Sel            = 1.25 V
227          * Vintcore12LP             = inactive (HP)
228          * VTVoutLP                 = inactive (HP)
229          */
230         INIT_REGULATOR_REGISTER(AB8500_REGUMISC1,              0xfe, 0x10),
231         /*
232          * VaudioEna                = disabled
233          * VdmicEna                 = disabled
234          * Vamic1Ena                = disabled
235          * Vamic2Ena                = disabled
236          */
237         INIT_REGULATOR_REGISTER(AB8500_VAUDIOSUPPLY,           0x1e, 0x00),
238         /*
239          * Vamic1_dzout             = high-Z when Vamic1 is disabled
240          * Vamic2_dzout             = high-Z when Vamic2 is disabled
241          */
242         INIT_REGULATOR_REGISTER(AB8500_REGUCTRL1VAMIC,         0x03, 0x00),
243         /*
244          * VPll                     = Hw controlled (NOTE! PRCMU bits)
245          * VanaRegu                 = force off
246          */
247         INIT_REGULATOR_REGISTER(AB8500_VPLLVANAREGU,           0x0f, 0x02),
248         /*
249          * VrefDDREna               = disabled
250          * VrefDDRSleepMode         = inactive (no pulldown)
251          */
252         INIT_REGULATOR_REGISTER(AB8500_VREFDDR,                0x03, 0x00),
253         /*
254          * VextSupply1Regu          = force LP
255          * VextSupply2Regu          = force OFF
256          * VextSupply3Regu          = force HP (-> STBB2=LP and TPS=LP)
257          * ExtSupply2Bypass         = ExtSupply12LPn ball is 0 when Ena is 0
258          * ExtSupply3Bypass         = ExtSupply3LPn ball is 0 when Ena is 0
259          */
260         INIT_REGULATOR_REGISTER(AB8500_EXTSUPPLYREGU,          0xff, 0x13),
261         /*
262          * Vaux1Regu                = force HP
263          * Vaux2Regu                = force off
264          */
265         INIT_REGULATOR_REGISTER(AB8500_VAUX12REGU,             0x0f, 0x01),
266         /*
267          * Vaux3Regu                = force off
268          */
269         INIT_REGULATOR_REGISTER(AB8500_VRF1VAUX3REGU,          0x03, 0x00),
270         /*
271          * Vaux1Sel                 = 2.8 V
272          */
273         INIT_REGULATOR_REGISTER(AB8500_VAUX1SEL,               0x0f, 0x0C),
274         /*
275          * Vaux2Sel                 = 2.9 V
276          */
277         INIT_REGULATOR_REGISTER(AB8500_VAUX2SEL,               0x0f, 0x0d),
278         /*
279          * Vaux3Sel                 = 2.91 V
280          */
281         INIT_REGULATOR_REGISTER(AB8500_VRF1VAUX3SEL,           0x07, 0x07),
282         /*
283          * VextSupply12LP           = disabled (no LP)
284          */
285         INIT_REGULATOR_REGISTER(AB8500_REGUCTRL2SPARE,         0x01, 0x00),
286         /*
287          * Vaux1Disch               = short discharge time
288          * Vaux2Disch               = short discharge time
289          * Vaux3Disch               = short discharge time
290          * Vintcore12Disch          = short discharge time
291          * VTVoutDisch              = short discharge time
292          * VaudioDisch              = short discharge time
293          */
294         INIT_REGULATOR_REGISTER(AB8500_REGUCTRLDISCH,          0xfc, 0x00),
295         /*
296          * VanaDisch                = short discharge time
297          * VdmicPullDownEna         = pulldown disabled when Vdmic is disabled
298          * VdmicDisch               = short discharge time
299          */
300         INIT_REGULATOR_REGISTER(AB8500_REGUCTRLDISCH2,         0x16, 0x00),
301 };
302
303 /* AB8500 regulators */
304 static struct regulator_init_data ab8500_regulators[AB8500_NUM_REGULATORS] = {
305         /* supplies to the display/camera */
306         [AB8500_LDO_AUX1] = {
307                 .supply_regulator = "ab8500-ext-supply3",
308                 .constraints = {
309                         .name = "V-DISPLAY",
310                         .min_uV = 2800000,
311                         .max_uV = 3300000,
312                         .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE |
313                                           REGULATOR_CHANGE_STATUS,
314                         .boot_on = 1, /* display is on at boot */
315                 },
316                 .num_consumer_supplies = ARRAY_SIZE(ab8500_vaux1_consumers),
317                 .consumer_supplies = ab8500_vaux1_consumers,
318         },
319         /* supplies to the on-board eMMC */
320         [AB8500_LDO_AUX2] = {
321                 .supply_regulator = "ab8500-ext-supply3",
322                 .constraints = {
323                         .name = "V-eMMC1",
324                         .min_uV = 1100000,
325                         .max_uV = 3300000,
326                         .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE |
327                                           REGULATOR_CHANGE_STATUS |
328                                           REGULATOR_CHANGE_MODE,
329                         .valid_modes_mask = REGULATOR_MODE_NORMAL |
330                                             REGULATOR_MODE_IDLE,
331                 },
332                 .num_consumer_supplies = ARRAY_SIZE(ab8500_vaux2_consumers),
333                 .consumer_supplies = ab8500_vaux2_consumers,
334         },
335         /* supply for VAUX3, supplies to SDcard slots */
336         [AB8500_LDO_AUX3] = {
337                 .supply_regulator = "ab8500-ext-supply3",
338                 .constraints = {
339                         .name = "V-MMC-SD",
340                         .min_uV = 1100000,
341                         .max_uV = 3300000,
342                         .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE |
343                                           REGULATOR_CHANGE_STATUS |
344                                           REGULATOR_CHANGE_MODE,
345                         .valid_modes_mask = REGULATOR_MODE_NORMAL |
346                                             REGULATOR_MODE_IDLE,
347                 },
348                 .num_consumer_supplies = ARRAY_SIZE(ab8500_vaux3_consumers),
349                 .consumer_supplies = ab8500_vaux3_consumers,
350         },
351         /* supply for tvout, gpadc, TVOUT LDO */
352         [AB8500_LDO_TVOUT] = {
353                 .constraints = {
354                         .name = "V-TVOUT",
355                         .valid_ops_mask = REGULATOR_CHANGE_STATUS,
356                 },
357                 .num_consumer_supplies = ARRAY_SIZE(ab8500_vtvout_consumers),
358                 .consumer_supplies = ab8500_vtvout_consumers,
359         },
360         /* supply for ab8500-vaudio, VAUDIO LDO */
361         [AB8500_LDO_AUDIO] = {
362                 .constraints = {
363                         .name = "V-AUD",
364                         .valid_ops_mask = REGULATOR_CHANGE_STATUS,
365                 },
366                 .num_consumer_supplies = ARRAY_SIZE(ab8500_vaud_consumers),
367                 .consumer_supplies = ab8500_vaud_consumers,
368         },
369         /* supply for v-anamic1 VAMic1-LDO */
370         [AB8500_LDO_ANAMIC1] = {
371                 .constraints = {
372                         .name = "V-AMIC1",
373                         .valid_ops_mask = REGULATOR_CHANGE_STATUS,
374                 },
375                 .num_consumer_supplies = ARRAY_SIZE(ab8500_vamic1_consumers),
376                 .consumer_supplies = ab8500_vamic1_consumers,
377         },
378         /* supply for v-amic2, VAMIC2 LDO, reuse constants for AMIC1 */
379         [AB8500_LDO_ANAMIC2] = {
380                 .constraints = {
381                         .name = "V-AMIC2",
382                         .valid_ops_mask = REGULATOR_CHANGE_STATUS,
383                 },
384                 .num_consumer_supplies = ARRAY_SIZE(ab8500_vamic2_consumers),
385                 .consumer_supplies = ab8500_vamic2_consumers,
386         },
387         /* supply for v-dmic, VDMIC LDO */
388         [AB8500_LDO_DMIC] = {
389                 .constraints = {
390                         .name = "V-DMIC",
391                         .valid_ops_mask = REGULATOR_CHANGE_STATUS,
392                 },
393                 .num_consumer_supplies = ARRAY_SIZE(ab8500_vdmic_consumers),
394                 .consumer_supplies = ab8500_vdmic_consumers,
395         },
396         /* supply for v-intcore12, VINTCORE12 LDO */
397         [AB8500_LDO_INTCORE] = {
398                 .constraints = {
399                         .name = "V-INTCORE",
400                         .min_uV = 1250000,
401                         .max_uV = 1350000,
402                         .input_uV = 1800000,
403                         .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE |
404                                           REGULATOR_CHANGE_STATUS |
405                                           REGULATOR_CHANGE_MODE |
406                                           REGULATOR_CHANGE_DRMS,
407                         .valid_modes_mask = REGULATOR_MODE_NORMAL |
408                                             REGULATOR_MODE_IDLE,
409                 },
410                 .num_consumer_supplies = ARRAY_SIZE(ab8500_vintcore_consumers),
411                 .consumer_supplies = ab8500_vintcore_consumers,
412         },
413         /* supply for U8500 CSI-DSI, VANA LDO */
414         [AB8500_LDO_ANA] = {
415                 .constraints = {
416                         .name = "V-CSI-DSI",
417                         .valid_ops_mask = REGULATOR_CHANGE_STATUS,
418                 },
419                 .num_consumer_supplies = ARRAY_SIZE(ab8500_vana_consumers),
420                 .consumer_supplies = ab8500_vana_consumers,
421         },
422 };
423
424 /* supply for VextSupply3 */
425 static struct regulator_consumer_supply ab8500_ext_supply3_consumers[] = {
426         /* SIM supply for 3 V SIM cards */
427         REGULATOR_SUPPLY("vinvsim", "sim-detect.0"),
428 };
429
430 /*
431  * AB8500 external regulators
432  */
433 static struct regulator_init_data ab8500_ext_regulators[] = {
434         /* fixed Vbat supplies VSMPS1_EXT_1V8 */
435         [AB8500_EXT_SUPPLY1] = {
436                 .constraints = {
437                         .name = "ab8500-ext-supply1",
438                         .min_uV = 1800000,
439                         .max_uV = 1800000,
440                         .initial_mode = REGULATOR_MODE_IDLE,
441                         .boot_on = 1,
442                         .always_on = 1,
443                 },
444         },
445         /* fixed Vbat supplies VSMPS2_EXT_1V36 and VSMPS5_EXT_1V15 */
446         [AB8500_EXT_SUPPLY2] = {
447                 .constraints = {
448                         .name = "ab8500-ext-supply2",
449                         .min_uV = 1360000,
450                         .max_uV = 1360000,
451                 },
452         },
453         /* fixed Vbat supplies VSMPS3_EXT_3V4 and VSMPS4_EXT_3V4 */
454         [AB8500_EXT_SUPPLY3] = {
455                 .constraints = {
456                         .name = "ab8500-ext-supply3",
457                         .min_uV = 3400000,
458                         .max_uV = 3400000,
459                         .valid_ops_mask = REGULATOR_CHANGE_STATUS,
460                         .boot_on = 1,
461                 },
462                 .num_consumer_supplies =
463                         ARRAY_SIZE(ab8500_ext_supply3_consumers),
464                 .consumer_supplies = ab8500_ext_supply3_consumers,
465         },
466 };
467
468 static struct ab8500_regulator_platform_data ab8500_regulator_plat_data = {
469         .reg_init               = ab8500_reg_init,
470         .num_reg_init           = ARRAY_SIZE(ab8500_reg_init),
471         .regulator              = ab8500_regulators,
472         .num_regulator          = ARRAY_SIZE(ab8500_regulators),
473         .ext_regulator          = ab8500_ext_regulators,
474         .num_ext_regulator      = ARRAY_SIZE(ab8500_ext_regulators),
475 };
476
477 /**
478  * struct ab8500_ext_regulator_info - ab8500 regulator information
479  * @dev: device pointer
480  * @desc: regulator description
481  * @cfg: regulator configuration (extension of regulator FW configuration)
482  * @update_bank: bank to control on/off
483  * @update_reg: register to control on/off
484  * @update_mask: mask to enable/disable and set mode of regulator
485  * @update_val: bits holding the regulator current mode
486  * @update_val_hp: bits to set EN pin active (LPn pin deactive)
487  *                 normally this means high power mode
488  * @update_val_lp: bits to set EN pin active and LPn pin active
489  *                 normally this means low power mode
490  * @update_val_hw: bits to set regulator pins in HW control
491  *                 SysClkReq pins and logic will choose mode
492  */
493 struct ab8500_ext_regulator_info {
494         struct device *dev;
495         struct regulator_desc desc;
496         struct ab8500_ext_regulator_cfg *cfg;
497         u8 update_bank;
498         u8 update_reg;
499         u8 update_mask;
500         u8 update_val;
501         u8 update_val_hp;
502         u8 update_val_lp;
503         u8 update_val_hw;
504 };
505
506 static int ab8500_ext_regulator_enable(struct regulator_dev *rdev)
507 {
508         int ret;
509         struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
510         u8 regval;
511
512         if (info == NULL) {
513                 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
514                 return -EINVAL;
515         }
516
517         /*
518          * To satisfy both HW high power request and SW request, the regulator
519          * must be on in high power.
520          */
521         if (info->cfg && info->cfg->hwreq)
522                 regval = info->update_val_hp;
523         else
524                 regval = info->update_val;
525
526         ret = abx500_mask_and_set_register_interruptible(info->dev,
527                 info->update_bank, info->update_reg,
528                 info->update_mask, regval);
529         if (ret < 0) {
530                 dev_err(rdev_get_dev(rdev),
531                         "couldn't set enable bits for regulator\n");
532                 return ret;
533         }
534
535         dev_dbg(rdev_get_dev(rdev),
536                 "%s-enable (bank, reg, mask, value): 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
537                 info->desc.name, info->update_bank, info->update_reg,
538                 info->update_mask, regval);
539
540         return 0;
541 }
542
543 static int ab8500_ext_regulator_disable(struct regulator_dev *rdev)
544 {
545         int ret;
546         struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
547         u8 regval;
548
549         if (info == NULL) {
550                 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
551                 return -EINVAL;
552         }
553
554         /*
555          * Set the regulator in HW request mode if configured
556          */
557         if (info->cfg && info->cfg->hwreq)
558                 regval = info->update_val_hw;
559         else
560                 regval = 0;
561
562         ret = abx500_mask_and_set_register_interruptible(info->dev,
563                 info->update_bank, info->update_reg,
564                 info->update_mask, regval);
565         if (ret < 0) {
566                 dev_err(rdev_get_dev(rdev),
567                         "couldn't set disable bits for regulator\n");
568                 return ret;
569         }
570
571         dev_dbg(rdev_get_dev(rdev), "%s-disable (bank, reg, mask, value):"
572                 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
573                 info->desc.name, info->update_bank, info->update_reg,
574                 info->update_mask, regval);
575
576         return 0;
577 }
578
579 static int ab8500_ext_regulator_is_enabled(struct regulator_dev *rdev)
580 {
581         int ret;
582         struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
583         u8 regval;
584
585         if (info == NULL) {
586                 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
587                 return -EINVAL;
588         }
589
590         ret = abx500_get_register_interruptible(info->dev,
591                 info->update_bank, info->update_reg, &regval);
592         if (ret < 0) {
593                 dev_err(rdev_get_dev(rdev),
594                         "couldn't read 0x%x register\n", info->update_reg);
595                 return ret;
596         }
597
598         dev_dbg(rdev_get_dev(rdev), "%s-is_enabled (bank, reg, mask, value):"
599                 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
600                 info->desc.name, info->update_bank, info->update_reg,
601                 info->update_mask, regval);
602
603         if (((regval & info->update_mask) == info->update_val_lp) ||
604             ((regval & info->update_mask) == info->update_val_hp))
605                 return 1;
606         else
607                 return 0;
608 }
609
610 static int ab8500_ext_regulator_set_mode(struct regulator_dev *rdev,
611                                          unsigned int mode)
612 {
613         int ret = 0;
614         struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
615         u8 regval;
616
617         if (info == NULL) {
618                 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
619                 return -EINVAL;
620         }
621
622         switch (mode) {
623         case REGULATOR_MODE_NORMAL:
624                 regval = info->update_val_hp;
625                 break;
626         case REGULATOR_MODE_IDLE:
627                 regval = info->update_val_lp;
628                 break;
629
630         default:
631                 return -EINVAL;
632         }
633
634         /* If regulator is enabled and info->cfg->hwreq is set, the regulator
635            must be on in high power, so we don't need to write the register with
636            the same value.
637          */
638         if (ab8500_ext_regulator_is_enabled(rdev) &&
639             !(info->cfg && info->cfg->hwreq)) {
640                 ret = abx500_mask_and_set_register_interruptible(info->dev,
641                                         info->update_bank, info->update_reg,
642                                         info->update_mask, regval);
643                 if (ret < 0) {
644                         dev_err(rdev_get_dev(rdev),
645                                 "Could not set regulator mode.\n");
646                         return ret;
647                 }
648
649                 dev_dbg(rdev_get_dev(rdev),
650                         "%s-set_mode (bank, reg, mask, value): "
651                         "0x%x, 0x%x, 0x%x, 0x%x\n",
652                         info->desc.name, info->update_bank, info->update_reg,
653                         info->update_mask, regval);
654         }
655
656         info->update_val = regval;
657
658         return 0;
659 }
660
661 static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev)
662 {
663         struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
664         int ret;
665
666         if (info == NULL) {
667                 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
668                 return -EINVAL;
669         }
670
671         if (info->update_val == info->update_val_hp)
672                 ret = REGULATOR_MODE_NORMAL;
673         else if (info->update_val == info->update_val_lp)
674                 ret = REGULATOR_MODE_IDLE;
675         else
676                 ret = -EINVAL;
677
678         return ret;
679 }
680
681 static int ab8500_ext_set_voltage(struct regulator_dev *rdev, int min_uV,
682                                   int max_uV, unsigned *selector)
683 {
684         struct regulation_constraints *regu_constraints = rdev->constraints;
685
686         if (!regu_constraints) {
687                 dev_err(rdev_get_dev(rdev), "No regulator constraints\n");
688                 return -EINVAL;
689         }
690
691         if (regu_constraints->min_uV == min_uV &&
692             regu_constraints->max_uV == max_uV)
693                 return 0;
694
695         dev_err(rdev_get_dev(rdev),
696                 "Requested min %duV max %duV != constrained min %duV max %duV\n",
697                 min_uV, max_uV,
698                 regu_constraints->min_uV, regu_constraints->max_uV);
699
700         return -EINVAL;
701 }
702
703 static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
704                                    unsigned selector)
705 {
706         struct regulation_constraints *regu_constraints = rdev->constraints;
707
708         if (regu_constraints == NULL) {
709                 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
710                 return -EINVAL;
711         }
712         /* return the uV for the fixed regulators */
713         if (regu_constraints->min_uV && regu_constraints->max_uV) {
714                 if (regu_constraints->min_uV == regu_constraints->max_uV)
715                         return regu_constraints->min_uV;
716         }
717         return -EINVAL;
718 }
719
720 static const struct regulator_ops ab8500_ext_regulator_ops = {
721         .enable                 = ab8500_ext_regulator_enable,
722         .disable                = ab8500_ext_regulator_disable,
723         .is_enabled             = ab8500_ext_regulator_is_enabled,
724         .set_mode               = ab8500_ext_regulator_set_mode,
725         .get_mode               = ab8500_ext_regulator_get_mode,
726         .set_voltage            = ab8500_ext_set_voltage,
727         .list_voltage           = ab8500_ext_list_voltage,
728 };
729
730 static struct ab8500_ext_regulator_info
731                 ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
732         [AB8500_EXT_SUPPLY1] = {
733                 .desc = {
734                         .name           = "VEXTSUPPLY1",
735                         .of_match       = of_match_ptr("ab8500_ext1"),
736                         .ops            = &ab8500_ext_regulator_ops,
737                         .type           = REGULATOR_VOLTAGE,
738                         .id             = AB8500_EXT_SUPPLY1,
739                         .owner          = THIS_MODULE,
740                         .n_voltages     = 1,
741                 },
742                 .update_bank            = 0x04,
743                 .update_reg             = 0x08,
744                 .update_mask            = 0x03,
745                 .update_val             = 0x01,
746                 .update_val_hp          = 0x01,
747                 .update_val_lp          = 0x03,
748                 .update_val_hw          = 0x02,
749         },
750         [AB8500_EXT_SUPPLY2] = {
751                 .desc = {
752                         .name           = "VEXTSUPPLY2",
753                         .of_match       = of_match_ptr("ab8500_ext2"),
754                         .ops            = &ab8500_ext_regulator_ops,
755                         .type           = REGULATOR_VOLTAGE,
756                         .id             = AB8500_EXT_SUPPLY2,
757                         .owner          = THIS_MODULE,
758                         .n_voltages     = 1,
759                 },
760                 .update_bank            = 0x04,
761                 .update_reg             = 0x08,
762                 .update_mask            = 0x0c,
763                 .update_val             = 0x04,
764                 .update_val_hp          = 0x04,
765                 .update_val_lp          = 0x0c,
766                 .update_val_hw          = 0x08,
767         },
768         [AB8500_EXT_SUPPLY3] = {
769                 .desc = {
770                         .name           = "VEXTSUPPLY3",
771                         .of_match       = of_match_ptr("ab8500_ext3"),
772                         .ops            = &ab8500_ext_regulator_ops,
773                         .type           = REGULATOR_VOLTAGE,
774                         .id             = AB8500_EXT_SUPPLY3,
775                         .owner          = THIS_MODULE,
776                         .n_voltages     = 1,
777                 },
778                 .update_bank            = 0x04,
779                 .update_reg             = 0x08,
780                 .update_mask            = 0x30,
781                 .update_val             = 0x10,
782                 .update_val_hp          = 0x10,
783                 .update_val_lp          = 0x30,
784                 .update_val_hw          = 0x20,
785         },
786 };
787
788 static int ab8500_ext_regulator_probe(struct platform_device *pdev)
789 {
790         struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
791         struct ab8500_regulator_platform_data *pdata = &ab8500_regulator_plat_data;
792         struct regulator_config config = { };
793         struct regulator_dev *rdev;
794         int i;
795
796         if (!ab8500) {
797                 dev_err(&pdev->dev, "null mfd parent\n");
798                 return -EINVAL;
799         }
800
801         /* make sure the platform data has the correct size */
802         if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) {
803                 dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
804                 return -EINVAL;
805         }
806
807         /* check for AB8500 2.x */
808         if (is_ab8500_2p0_or_earlier(ab8500)) {
809                 struct ab8500_ext_regulator_info *info;
810
811                 /* VextSupply3LPn is inverted on AB8500 2.x */
812                 info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
813                 info->update_val = 0x30;
814                 info->update_val_hp = 0x30;
815                 info->update_val_lp = 0x10;
816         }
817
818         /* register all regulators */
819         for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
820                 struct ab8500_ext_regulator_info *info = NULL;
821
822                 /* assign per-regulator data */
823                 info = &ab8500_ext_regulator_info[i];
824                 info->dev = &pdev->dev;
825                 info->cfg = (struct ab8500_ext_regulator_cfg *)
826                         pdata->ext_regulator[i].driver_data;
827
828                 config.dev = &pdev->dev;
829                 config.driver_data = info;
830                 config.init_data = &pdata->ext_regulator[i];
831
832                 /* register regulator with framework */
833                 rdev = devm_regulator_register(&pdev->dev, &info->desc,
834                                                &config);
835                 if (IS_ERR(rdev)) {
836                         dev_err(&pdev->dev, "failed to register regulator %s\n",
837                                         info->desc.name);
838                         return PTR_ERR(rdev);
839                 }
840
841                 dev_dbg(&pdev->dev, "%s-probed\n", info->desc.name);
842         }
843
844         return 0;
845 }
846
847 static struct platform_driver ab8500_ext_regulator_driver = {
848         .probe = ab8500_ext_regulator_probe,
849         .driver         = {
850                 .name   = "ab8500-ext-regulator",
851         },
852 };
853
854 static int __init ab8500_ext_regulator_init(void)
855 {
856         int ret;
857
858         ret = platform_driver_register(&ab8500_ext_regulator_driver);
859         if (ret)
860                 pr_err("Failed to register ab8500 ext regulator: %d\n", ret);
861
862         return ret;
863 }
864 subsys_initcall(ab8500_ext_regulator_init);
865
866 static void __exit ab8500_ext_regulator_exit(void)
867 {
868         platform_driver_unregister(&ab8500_ext_regulator_driver);
869 }
870 module_exit(ab8500_ext_regulator_exit);
871
872 MODULE_LICENSE("GPL v2");
873 MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
874 MODULE_DESCRIPTION("AB8500 external regulator driver");
875 MODULE_ALIAS("platform:ab8500-ext-regulator");