162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci//
362306a36Sopenharmony_ci// Regulator Driver for Freescale MC13783 PMIC
462306a36Sopenharmony_ci//
562306a36Sopenharmony_ci// Copyright 2010 Yong Shen <yong.shen@linaro.org>
662306a36Sopenharmony_ci// Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
762306a36Sopenharmony_ci// Copyright 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/mfd/mc13783.h>
1062306a36Sopenharmony_ci#include <linux/regulator/machine.h>
1162306a36Sopenharmony_ci#include <linux/regulator/driver.h>
1262306a36Sopenharmony_ci#include <linux/platform_device.h>
1362306a36Sopenharmony_ci#include <linux/kernel.h>
1462306a36Sopenharmony_ci#include <linux/slab.h>
1562306a36Sopenharmony_ci#include <linux/init.h>
1662306a36Sopenharmony_ci#include <linux/err.h>
1762306a36Sopenharmony_ci#include <linux/module.h>
1862306a36Sopenharmony_ci#include "mc13xxx.h"
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS0			24
2162306a36Sopenharmony_ci/* Enable does not exist for SW1A */
2262306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS0_SW1AEN			0
2362306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS0_SW1AVSEL			0
2462306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS0_SW1AVSEL_M		(63 << 0)
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS1			25
2762306a36Sopenharmony_ci/* Enable does not exist for SW1B */
2862306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS1_SW1BEN			0
2962306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS1_SW1BVSEL			0
3062306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS1_SW1BVSEL_M		(63 << 0)
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS2			26
3362306a36Sopenharmony_ci/* Enable does not exist for SW2A */
3462306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS2_SW2AEN			0
3562306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS2_SW2AVSEL			0
3662306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS2_SW2AVSEL_M		(63 << 0)
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS3			27
3962306a36Sopenharmony_ci/* Enable does not exist for SW2B */
4062306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS3_SW2BEN			0
4162306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS3_SW2BVSEL			0
4262306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS3_SW2BVSEL_M		(63 << 0)
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS5			29
4562306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS5_SW3EN			(1 << 20)
4662306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS5_SW3VSEL			18
4762306a36Sopenharmony_ci#define MC13783_REG_SWITCHERS5_SW3VSEL_M		(3 << 18)
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0		30
5062306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VIOLOVSEL		2
5162306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VDIGVSEL		4
5262306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VGENVSEL		6
5362306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VRFDIGVSEL	9
5462306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VRFREFVSEL	11
5562306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VRFCPVSEL		13
5662306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VSIMVSEL		14
5762306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VESIMVSEL		15
5862306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VCAMVSEL		16
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VIOLOVSEL_M	(3 << 2)
6162306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VDIGVSEL_M	(3 << 4)
6262306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VGENVSEL_M	(7 << 6)
6362306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VRFDIGVSEL_M	(3 << 9)
6462306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VRFREFVSEL_M	(3 << 11)
6562306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VRFCPVSEL_M	(1 << 13)
6662306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VSIMVSEL_M	(1 << 14)
6762306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VESIMVSEL_M	(1 << 15)
6862306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VCAMVSEL_M	(7 << 16)
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1		31
7162306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VVIBVSEL		0
7262306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VRF1VSEL		2
7362306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VRF2VSEL		4
7462306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VMMC1VSEL		6
7562306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VMMC2VSEL		9
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VVIBVSEL_M	(3 << 0)
7862306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VRF1VSEL_M	(3 << 2)
7962306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VRF2VSEL_M	(3 << 4)
8062306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VMMC1VSEL_M	(7 << 6)
8162306a36Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VMMC2VSEL_M	(7 << 9)
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE0		32
8462306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VAUDIOEN		(1 << 0)
8562306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VIOHIEN		(1 << 3)
8662306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VIOLOEN		(1 << 6)
8762306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VDIGEN		(1 << 9)
8862306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VGENEN		(1 << 12)
8962306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VRFDIGEN		(1 << 15)
9062306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VRFREFEN		(1 << 18)
9162306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VRFCPEN		(1 << 21)
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE1		33
9462306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VSIMEN		(1 << 0)
9562306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VESIMEN		(1 << 3)
9662306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VCAMEN		(1 << 6)
9762306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VRFBGEN		(1 << 9)
9862306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VVIBEN		(1 << 11)
9962306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VRF1EN		(1 << 12)
10062306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VRF2EN		(1 << 15)
10162306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VMMC1EN		(1 << 18)
10262306a36Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VMMC2EN		(1 << 21)
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci#define MC13783_REG_POWERMISC			34
10562306a36Sopenharmony_ci#define MC13783_REG_POWERMISC_GPO1EN			(1 << 6)
10662306a36Sopenharmony_ci#define MC13783_REG_POWERMISC_GPO2EN			(1 << 8)
10762306a36Sopenharmony_ci#define MC13783_REG_POWERMISC_GPO3EN			(1 << 10)
10862306a36Sopenharmony_ci#define MC13783_REG_POWERMISC_GPO4EN			(1 << 12)
10962306a36Sopenharmony_ci#define MC13783_REG_POWERMISC_PWGT1SPIEN		(1 << 15)
11062306a36Sopenharmony_ci#define MC13783_REG_POWERMISC_PWGT2SPIEN		(1 << 16)
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci#define MC13783_REG_POWERMISC_PWGTSPI_M			(3 << 15)
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci/* Voltage Values */
11662306a36Sopenharmony_cistatic const int mc13783_sw1x_val[] = {
11762306a36Sopenharmony_ci	900000, 925000, 950000, 975000,
11862306a36Sopenharmony_ci	1000000, 1025000, 1050000, 1075000,
11962306a36Sopenharmony_ci	1100000, 1125000, 1150000, 1175000,
12062306a36Sopenharmony_ci	1200000, 1225000, 1250000, 1275000,
12162306a36Sopenharmony_ci	1300000, 1325000, 1350000, 1375000,
12262306a36Sopenharmony_ci	1400000, 1425000, 1450000, 1475000,
12362306a36Sopenharmony_ci	1500000, 1525000, 1550000, 1575000,
12462306a36Sopenharmony_ci	1600000, 1625000, 1650000, 1675000,
12562306a36Sopenharmony_ci	1700000, 1700000, 1700000, 1700000,
12662306a36Sopenharmony_ci	1800000, 1800000, 1800000, 1800000,
12762306a36Sopenharmony_ci	1850000, 1850000, 1850000, 1850000,
12862306a36Sopenharmony_ci	2000000, 2000000, 2000000, 2000000,
12962306a36Sopenharmony_ci	2100000, 2100000, 2100000, 2100000,
13062306a36Sopenharmony_ci	2200000, 2200000, 2200000, 2200000,
13162306a36Sopenharmony_ci	2200000, 2200000, 2200000, 2200000,
13262306a36Sopenharmony_ci	2200000, 2200000, 2200000, 2200000,
13362306a36Sopenharmony_ci};
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cistatic const int mc13783_sw2x_val[] = {
13662306a36Sopenharmony_ci	900000, 925000, 950000, 975000,
13762306a36Sopenharmony_ci	1000000, 1025000, 1050000, 1075000,
13862306a36Sopenharmony_ci	1100000, 1125000, 1150000, 1175000,
13962306a36Sopenharmony_ci	1200000, 1225000, 1250000, 1275000,
14062306a36Sopenharmony_ci	1300000, 1325000, 1350000, 1375000,
14162306a36Sopenharmony_ci	1400000, 1425000, 1450000, 1475000,
14262306a36Sopenharmony_ci	1500000, 1525000, 1550000, 1575000,
14362306a36Sopenharmony_ci	1600000, 1625000, 1650000, 1675000,
14462306a36Sopenharmony_ci	1700000, 1700000, 1700000, 1700000,
14562306a36Sopenharmony_ci	1800000, 1800000, 1800000, 1800000,
14662306a36Sopenharmony_ci	1900000, 1900000, 1900000, 1900000,
14762306a36Sopenharmony_ci	2000000, 2000000, 2000000, 2000000,
14862306a36Sopenharmony_ci	2100000, 2100000, 2100000, 2100000,
14962306a36Sopenharmony_ci	2200000, 2200000, 2200000, 2200000,
15062306a36Sopenharmony_ci	2200000, 2200000, 2200000, 2200000,
15162306a36Sopenharmony_ci	2200000, 2200000, 2200000, 2200000,
15262306a36Sopenharmony_ci};
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_cistatic const unsigned int mc13783_sw3_val[] = {
15562306a36Sopenharmony_ci	5000000, 5000000, 5000000, 5500000,
15662306a36Sopenharmony_ci};
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_cistatic const unsigned int mc13783_vaudio_val[] = {
15962306a36Sopenharmony_ci	2775000,
16062306a36Sopenharmony_ci};
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_cistatic const unsigned int mc13783_viohi_val[] = {
16362306a36Sopenharmony_ci	2775000,
16462306a36Sopenharmony_ci};
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_cistatic const unsigned int mc13783_violo_val[] = {
16762306a36Sopenharmony_ci	1200000, 1300000, 1500000, 1800000,
16862306a36Sopenharmony_ci};
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_cistatic const unsigned int mc13783_vdig_val[] = {
17162306a36Sopenharmony_ci	1200000, 1300000, 1500000, 1800000,
17262306a36Sopenharmony_ci};
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_cistatic const unsigned int mc13783_vgen_val[] = {
17562306a36Sopenharmony_ci	1200000, 1300000, 1500000, 1800000,
17662306a36Sopenharmony_ci	1100000, 2000000, 2775000, 2400000,
17762306a36Sopenharmony_ci};
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_cistatic const unsigned int mc13783_vrfdig_val[] = {
18062306a36Sopenharmony_ci	1200000, 1500000, 1800000, 1875000,
18162306a36Sopenharmony_ci};
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_cistatic const unsigned int mc13783_vrfref_val[] = {
18462306a36Sopenharmony_ci	2475000, 2600000, 2700000, 2775000,
18562306a36Sopenharmony_ci};
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_cistatic const unsigned int mc13783_vrfcp_val[] = {
18862306a36Sopenharmony_ci	2700000, 2775000,
18962306a36Sopenharmony_ci};
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_cistatic const unsigned int mc13783_vsim_val[] = {
19262306a36Sopenharmony_ci	1800000, 2900000, 3000000,
19362306a36Sopenharmony_ci};
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_cistatic const unsigned int mc13783_vesim_val[] = {
19662306a36Sopenharmony_ci	1800000, 2900000,
19762306a36Sopenharmony_ci};
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_cistatic const unsigned int mc13783_vcam_val[] = {
20062306a36Sopenharmony_ci	1500000, 1800000, 2500000, 2550000,
20162306a36Sopenharmony_ci	2600000, 2750000, 2800000, 3000000,
20262306a36Sopenharmony_ci};
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_cistatic const unsigned int mc13783_vrfbg_val[] = {
20562306a36Sopenharmony_ci	1250000,
20662306a36Sopenharmony_ci};
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_cistatic const unsigned int mc13783_vvib_val[] = {
20962306a36Sopenharmony_ci	1300000, 1800000, 2000000, 3000000,
21062306a36Sopenharmony_ci};
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_cistatic const unsigned int mc13783_vmmc_val[] = {
21362306a36Sopenharmony_ci	1600000, 1800000, 2000000, 2600000,
21462306a36Sopenharmony_ci	2700000, 2800000, 2900000, 3000000,
21562306a36Sopenharmony_ci};
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_cistatic const unsigned int mc13783_vrf_val[] = {
21862306a36Sopenharmony_ci	1500000, 1875000, 2700000, 2775000,
21962306a36Sopenharmony_ci};
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_cistatic const unsigned int mc13783_gpo_val[] = {
22262306a36Sopenharmony_ci	3100000,
22362306a36Sopenharmony_ci};
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_cistatic const unsigned int mc13783_pwgtdrv_val[] = {
22662306a36Sopenharmony_ci	5500000,
22762306a36Sopenharmony_ci};
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_cistatic const struct regulator_ops mc13783_gpo_regulator_ops;
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci#define MC13783_DEFINE(prefix, name, node, reg, vsel_reg, voltages)	\
23262306a36Sopenharmony_ci	MC13xxx_DEFINE(MC13783_REG_, name, node, reg, vsel_reg, voltages, \
23362306a36Sopenharmony_ci			mc13xxx_regulator_ops)
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci#define MC13783_FIXED_DEFINE(prefix, name, node, reg, voltages)		\
23662306a36Sopenharmony_ci	MC13xxx_FIXED_DEFINE(MC13783_REG_, name, node, reg, voltages,	\
23762306a36Sopenharmony_ci			mc13xxx_fixed_regulator_ops)
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci#define MC13783_GPO_DEFINE(prefix, name, node, reg, voltages)		\
24062306a36Sopenharmony_ci	MC13xxx_GPO_DEFINE(MC13783_REG_, name, node, reg, voltages,	\
24162306a36Sopenharmony_ci			mc13783_gpo_regulator_ops)
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci#define MC13783_DEFINE_SW(_name, _node, _reg, _vsel_reg, _voltages)	\
24462306a36Sopenharmony_ci	MC13783_DEFINE(REG, _name, _node, _reg, _vsel_reg, _voltages)
24562306a36Sopenharmony_ci#define MC13783_DEFINE_REGU(_name, _node, _reg, _vsel_reg, _voltages)	\
24662306a36Sopenharmony_ci	MC13783_DEFINE(REG, _name, _node, _reg, _vsel_reg, _voltages)
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_cistatic struct mc13xxx_regulator mc13783_regulators[] = {
24962306a36Sopenharmony_ci	MC13783_DEFINE_SW(SW1A, sw1a, SWITCHERS0, SWITCHERS0, mc13783_sw1x_val),
25062306a36Sopenharmony_ci	MC13783_DEFINE_SW(SW1B, sw1b, SWITCHERS1, SWITCHERS1, mc13783_sw1x_val),
25162306a36Sopenharmony_ci	MC13783_DEFINE_SW(SW2A, sw2a, SWITCHERS2, SWITCHERS2, mc13783_sw2x_val),
25262306a36Sopenharmony_ci	MC13783_DEFINE_SW(SW2B, sw2b, SWITCHERS3, SWITCHERS3, mc13783_sw2x_val),
25362306a36Sopenharmony_ci	MC13783_DEFINE_SW(SW3, sw3, SWITCHERS5, SWITCHERS5, mc13783_sw3_val),
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci	MC13783_FIXED_DEFINE(REG, VAUDIO, vaudio, REGULATORMODE0, mc13783_vaudio_val),
25662306a36Sopenharmony_ci	MC13783_FIXED_DEFINE(REG, VIOHI, viohi, REGULATORMODE0, mc13783_viohi_val),
25762306a36Sopenharmony_ci	MC13783_DEFINE_REGU(VIOLO, violo, REGULATORMODE0, REGULATORSETTING0,
25862306a36Sopenharmony_ci			    mc13783_violo_val),
25962306a36Sopenharmony_ci	MC13783_DEFINE_REGU(VDIG, vdig, REGULATORMODE0, REGULATORSETTING0,
26062306a36Sopenharmony_ci			    mc13783_vdig_val),
26162306a36Sopenharmony_ci	MC13783_DEFINE_REGU(VGEN, vgen, REGULATORMODE0, REGULATORSETTING0,
26262306a36Sopenharmony_ci			    mc13783_vgen_val),
26362306a36Sopenharmony_ci	MC13783_DEFINE_REGU(VRFDIG, vrfdig, REGULATORMODE0, REGULATORSETTING0,
26462306a36Sopenharmony_ci			    mc13783_vrfdig_val),
26562306a36Sopenharmony_ci	MC13783_DEFINE_REGU(VRFREF, vrfref, REGULATORMODE0, REGULATORSETTING0,
26662306a36Sopenharmony_ci			    mc13783_vrfref_val),
26762306a36Sopenharmony_ci	MC13783_DEFINE_REGU(VRFCP, vrfcp, REGULATORMODE0, REGULATORSETTING0,
26862306a36Sopenharmony_ci			    mc13783_vrfcp_val),
26962306a36Sopenharmony_ci	MC13783_DEFINE_REGU(VSIM, vsim, REGULATORMODE1, REGULATORSETTING0,
27062306a36Sopenharmony_ci			    mc13783_vsim_val),
27162306a36Sopenharmony_ci	MC13783_DEFINE_REGU(VESIM, vesim, REGULATORMODE1, REGULATORSETTING0,
27262306a36Sopenharmony_ci			    mc13783_vesim_val),
27362306a36Sopenharmony_ci	MC13783_DEFINE_REGU(VCAM, vcam, REGULATORMODE1, REGULATORSETTING0,
27462306a36Sopenharmony_ci			    mc13783_vcam_val),
27562306a36Sopenharmony_ci	MC13783_FIXED_DEFINE(REG, VRFBG, vrfbg, REGULATORMODE1, mc13783_vrfbg_val),
27662306a36Sopenharmony_ci	MC13783_DEFINE_REGU(VVIB, vvib, REGULATORMODE1, REGULATORSETTING1,
27762306a36Sopenharmony_ci			    mc13783_vvib_val),
27862306a36Sopenharmony_ci	MC13783_DEFINE_REGU(VRF1, vrf1, REGULATORMODE1, REGULATORSETTING1,
27962306a36Sopenharmony_ci			    mc13783_vrf_val),
28062306a36Sopenharmony_ci	MC13783_DEFINE_REGU(VRF2, vrf2, REGULATORMODE1, REGULATORSETTING1,
28162306a36Sopenharmony_ci			    mc13783_vrf_val),
28262306a36Sopenharmony_ci	MC13783_DEFINE_REGU(VMMC1, vmmc1, REGULATORMODE1, REGULATORSETTING1,
28362306a36Sopenharmony_ci			    mc13783_vmmc_val),
28462306a36Sopenharmony_ci	MC13783_DEFINE_REGU(VMMC2, vmmc2, REGULATORMODE1, REGULATORSETTING1,
28562306a36Sopenharmony_ci			    mc13783_vmmc_val),
28662306a36Sopenharmony_ci	MC13783_GPO_DEFINE(REG, GPO1, gpo1, POWERMISC, mc13783_gpo_val),
28762306a36Sopenharmony_ci	MC13783_GPO_DEFINE(REG, GPO2, gpo1, POWERMISC, mc13783_gpo_val),
28862306a36Sopenharmony_ci	MC13783_GPO_DEFINE(REG, GPO3, gpo1, POWERMISC, mc13783_gpo_val),
28962306a36Sopenharmony_ci	MC13783_GPO_DEFINE(REG, GPO4, gpo1, POWERMISC, mc13783_gpo_val),
29062306a36Sopenharmony_ci	MC13783_GPO_DEFINE(REG, PWGT1SPI, pwgt1spi, POWERMISC, mc13783_pwgtdrv_val),
29162306a36Sopenharmony_ci	MC13783_GPO_DEFINE(REG, PWGT2SPI, pwgt2spi, POWERMISC, mc13783_pwgtdrv_val),
29262306a36Sopenharmony_ci};
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_cistatic int mc13783_powermisc_rmw(struct mc13xxx_regulator_priv *priv, u32 mask,
29562306a36Sopenharmony_ci		u32 val)
29662306a36Sopenharmony_ci{
29762306a36Sopenharmony_ci	struct mc13xxx *mc13783 = priv->mc13xxx;
29862306a36Sopenharmony_ci	int ret;
29962306a36Sopenharmony_ci	u32 valread;
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci	BUG_ON(val & ~mask);
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci	mc13xxx_lock(priv->mc13xxx);
30462306a36Sopenharmony_ci	ret = mc13xxx_reg_read(mc13783, MC13783_REG_POWERMISC, &valread);
30562306a36Sopenharmony_ci	if (ret)
30662306a36Sopenharmony_ci		goto out;
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci	/* Update the stored state for Power Gates. */
30962306a36Sopenharmony_ci	priv->powermisc_pwgt_state =
31062306a36Sopenharmony_ci				(priv->powermisc_pwgt_state & ~mask) | val;
31162306a36Sopenharmony_ci	priv->powermisc_pwgt_state &= MC13783_REG_POWERMISC_PWGTSPI_M;
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci	/* Construct the new register value */
31462306a36Sopenharmony_ci	valread = (valread & ~mask) | val;
31562306a36Sopenharmony_ci	/* Overwrite the PWGTxEN with the stored version */
31662306a36Sopenharmony_ci	valread = (valread & ~MC13783_REG_POWERMISC_PWGTSPI_M) |
31762306a36Sopenharmony_ci						priv->powermisc_pwgt_state;
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	ret = mc13xxx_reg_write(mc13783, MC13783_REG_POWERMISC, valread);
32062306a36Sopenharmony_ciout:
32162306a36Sopenharmony_ci	mc13xxx_unlock(priv->mc13xxx);
32262306a36Sopenharmony_ci	return ret;
32362306a36Sopenharmony_ci}
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_cistatic int mc13783_gpo_regulator_enable(struct regulator_dev *rdev)
32662306a36Sopenharmony_ci{
32762306a36Sopenharmony_ci	struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
32862306a36Sopenharmony_ci	struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
32962306a36Sopenharmony_ci	int id = rdev_get_id(rdev);
33062306a36Sopenharmony_ci	u32 en_val = mc13xxx_regulators[id].enable_bit;
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci	/* Power Gate enable value is 0 */
33562306a36Sopenharmony_ci	if (id == MC13783_REG_PWGT1SPI ||
33662306a36Sopenharmony_ci	    id == MC13783_REG_PWGT2SPI)
33762306a36Sopenharmony_ci		en_val = 0;
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci	return mc13783_powermisc_rmw(priv, mc13xxx_regulators[id].enable_bit,
34062306a36Sopenharmony_ci					en_val);
34162306a36Sopenharmony_ci}
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_cistatic int mc13783_gpo_regulator_disable(struct regulator_dev *rdev)
34462306a36Sopenharmony_ci{
34562306a36Sopenharmony_ci	struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
34662306a36Sopenharmony_ci	struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
34762306a36Sopenharmony_ci	int id = rdev_get_id(rdev);
34862306a36Sopenharmony_ci	u32 dis_val = 0;
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci	/* Power Gate disable value is 1 */
35362306a36Sopenharmony_ci	if (id == MC13783_REG_PWGT1SPI ||
35462306a36Sopenharmony_ci	    id == MC13783_REG_PWGT2SPI)
35562306a36Sopenharmony_ci		dis_val = mc13xxx_regulators[id].enable_bit;
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci	return mc13783_powermisc_rmw(priv, mc13xxx_regulators[id].enable_bit,
35862306a36Sopenharmony_ci					dis_val);
35962306a36Sopenharmony_ci}
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_cistatic int mc13783_gpo_regulator_is_enabled(struct regulator_dev *rdev)
36262306a36Sopenharmony_ci{
36362306a36Sopenharmony_ci	struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
36462306a36Sopenharmony_ci	struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
36562306a36Sopenharmony_ci	int ret, id = rdev_get_id(rdev);
36662306a36Sopenharmony_ci	unsigned int val;
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ci	mc13xxx_lock(priv->mc13xxx);
36962306a36Sopenharmony_ci	ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val);
37062306a36Sopenharmony_ci	mc13xxx_unlock(priv->mc13xxx);
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_ci	if (ret)
37362306a36Sopenharmony_ci		return ret;
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci	/* Power Gates state is stored in powermisc_pwgt_state
37662306a36Sopenharmony_ci	 * where the meaning of bits is negated */
37762306a36Sopenharmony_ci	val = (val & ~MC13783_REG_POWERMISC_PWGTSPI_M) |
37862306a36Sopenharmony_ci	      (priv->powermisc_pwgt_state ^ MC13783_REG_POWERMISC_PWGTSPI_M);
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci	return (val & mc13xxx_regulators[id].enable_bit) != 0;
38162306a36Sopenharmony_ci}
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_cistatic const struct regulator_ops mc13783_gpo_regulator_ops = {
38462306a36Sopenharmony_ci	.enable = mc13783_gpo_regulator_enable,
38562306a36Sopenharmony_ci	.disable = mc13783_gpo_regulator_disable,
38662306a36Sopenharmony_ci	.is_enabled = mc13783_gpo_regulator_is_enabled,
38762306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_table,
38862306a36Sopenharmony_ci	.set_voltage = mc13xxx_fixed_regulator_set_voltage,
38962306a36Sopenharmony_ci};
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_cistatic int mc13783_regulator_probe(struct platform_device *pdev)
39262306a36Sopenharmony_ci{
39362306a36Sopenharmony_ci	struct mc13xxx_regulator_priv *priv;
39462306a36Sopenharmony_ci	struct mc13xxx *mc13783 = dev_get_drvdata(pdev->dev.parent);
39562306a36Sopenharmony_ci	struct mc13xxx_regulator_platform_data *pdata =
39662306a36Sopenharmony_ci		dev_get_platdata(&pdev->dev);
39762306a36Sopenharmony_ci	struct mc13xxx_regulator_init_data *mc13xxx_data;
39862306a36Sopenharmony_ci	struct regulator_config config = { };
39962306a36Sopenharmony_ci	int i, num_regulators;
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci	num_regulators = mc13xxx_get_num_regulators_dt(pdev);
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci	if (num_regulators <= 0 && pdata)
40462306a36Sopenharmony_ci		num_regulators = pdata->num_regulators;
40562306a36Sopenharmony_ci	if (num_regulators <= 0)
40662306a36Sopenharmony_ci		return -EINVAL;
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci	priv = devm_kzalloc(&pdev->dev,
40962306a36Sopenharmony_ci			    struct_size(priv, regulators, num_regulators),
41062306a36Sopenharmony_ci			    GFP_KERNEL);
41162306a36Sopenharmony_ci	if (!priv)
41262306a36Sopenharmony_ci		return -ENOMEM;
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci	priv->num_regulators = num_regulators;
41562306a36Sopenharmony_ci	priv->mc13xxx_regulators = mc13783_regulators;
41662306a36Sopenharmony_ci	priv->mc13xxx = mc13783;
41762306a36Sopenharmony_ci	platform_set_drvdata(pdev, priv);
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ci	mc13xxx_data = mc13xxx_parse_regulators_dt(pdev, mc13783_regulators,
42062306a36Sopenharmony_ci					ARRAY_SIZE(mc13783_regulators));
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci	for (i = 0; i < priv->num_regulators; i++) {
42362306a36Sopenharmony_ci		struct regulator_init_data *init_data;
42462306a36Sopenharmony_ci		struct regulator_desc *desc;
42562306a36Sopenharmony_ci		struct device_node *node = NULL;
42662306a36Sopenharmony_ci		int id;
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci		if (mc13xxx_data) {
42962306a36Sopenharmony_ci			id = mc13xxx_data[i].id;
43062306a36Sopenharmony_ci			init_data = mc13xxx_data[i].init_data;
43162306a36Sopenharmony_ci			node = mc13xxx_data[i].node;
43262306a36Sopenharmony_ci		} else {
43362306a36Sopenharmony_ci			id = pdata->regulators[i].id;
43462306a36Sopenharmony_ci			init_data = pdata->regulators[i].init_data;
43562306a36Sopenharmony_ci		}
43662306a36Sopenharmony_ci		desc = &mc13783_regulators[id].desc;
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci		config.dev = &pdev->dev;
43962306a36Sopenharmony_ci		config.init_data = init_data;
44062306a36Sopenharmony_ci		config.driver_data = priv;
44162306a36Sopenharmony_ci		config.of_node = node;
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci		priv->regulators[i] = devm_regulator_register(&pdev->dev, desc,
44462306a36Sopenharmony_ci							      &config);
44562306a36Sopenharmony_ci		if (IS_ERR(priv->regulators[i])) {
44662306a36Sopenharmony_ci			dev_err(&pdev->dev, "failed to register regulator %s\n",
44762306a36Sopenharmony_ci				mc13783_regulators[i].desc.name);
44862306a36Sopenharmony_ci			return PTR_ERR(priv->regulators[i]);
44962306a36Sopenharmony_ci		}
45062306a36Sopenharmony_ci	}
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ci	return 0;
45362306a36Sopenharmony_ci}
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_cistatic struct platform_driver mc13783_regulator_driver = {
45662306a36Sopenharmony_ci	.driver	= {
45762306a36Sopenharmony_ci		.name	= "mc13783-regulator",
45862306a36Sopenharmony_ci		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
45962306a36Sopenharmony_ci	},
46062306a36Sopenharmony_ci	.probe		= mc13783_regulator_probe,
46162306a36Sopenharmony_ci};
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_cistatic int __init mc13783_regulator_init(void)
46462306a36Sopenharmony_ci{
46562306a36Sopenharmony_ci	return platform_driver_register(&mc13783_regulator_driver);
46662306a36Sopenharmony_ci}
46762306a36Sopenharmony_cisubsys_initcall(mc13783_regulator_init);
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_cistatic void __exit mc13783_regulator_exit(void)
47062306a36Sopenharmony_ci{
47162306a36Sopenharmony_ci	platform_driver_unregister(&mc13783_regulator_driver);
47262306a36Sopenharmony_ci}
47362306a36Sopenharmony_cimodule_exit(mc13783_regulator_exit);
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
47662306a36Sopenharmony_ciMODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
47762306a36Sopenharmony_ciMODULE_DESCRIPTION("Regulator Driver for Freescale MC13783 PMIC");
47862306a36Sopenharmony_ciMODULE_ALIAS("platform:mc13783-regulator");
479