18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// Regulator Driver for Freescale MC13783 PMIC
48c2ecf20Sopenharmony_ci//
58c2ecf20Sopenharmony_ci// Copyright 2010 Yong Shen <yong.shen@linaro.org>
68c2ecf20Sopenharmony_ci// Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
78c2ecf20Sopenharmony_ci// Copyright 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/mfd/mc13783.h>
108c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h>
118c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
128c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/slab.h>
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/err.h>
178c2ecf20Sopenharmony_ci#include <linux/module.h>
188c2ecf20Sopenharmony_ci#include "mc13xxx.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS0			24
218c2ecf20Sopenharmony_ci/* Enable does not exist for SW1A */
228c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS0_SW1AEN			0
238c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS0_SW1AVSEL			0
248c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS0_SW1AVSEL_M		(63 << 0)
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS1			25
278c2ecf20Sopenharmony_ci/* Enable does not exist for SW1B */
288c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS1_SW1BEN			0
298c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS1_SW1BVSEL			0
308c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS1_SW1BVSEL_M		(63 << 0)
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS2			26
338c2ecf20Sopenharmony_ci/* Enable does not exist for SW2A */
348c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS2_SW2AEN			0
358c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS2_SW2AVSEL			0
368c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS2_SW2AVSEL_M		(63 << 0)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS3			27
398c2ecf20Sopenharmony_ci/* Enable does not exist for SW2B */
408c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS3_SW2BEN			0
418c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS3_SW2BVSEL			0
428c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS3_SW2BVSEL_M		(63 << 0)
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS5			29
458c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS5_SW3EN			(1 << 20)
468c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS5_SW3VSEL			18
478c2ecf20Sopenharmony_ci#define MC13783_REG_SWITCHERS5_SW3VSEL_M		(3 << 18)
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0		30
508c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VIOLOVSEL		2
518c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VDIGVSEL		4
528c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VGENVSEL		6
538c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VRFDIGVSEL	9
548c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VRFREFVSEL	11
558c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VRFCPVSEL		13
568c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VSIMVSEL		14
578c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VESIMVSEL		15
588c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VCAMVSEL		16
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VIOLOVSEL_M	(3 << 2)
618c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VDIGVSEL_M	(3 << 4)
628c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VGENVSEL_M	(7 << 6)
638c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VRFDIGVSEL_M	(3 << 9)
648c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VRFREFVSEL_M	(3 << 11)
658c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VRFCPVSEL_M	(1 << 13)
668c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VSIMVSEL_M	(1 << 14)
678c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VESIMVSEL_M	(1 << 15)
688c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING0_VCAMVSEL_M	(7 << 16)
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1		31
718c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VVIBVSEL		0
728c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VRF1VSEL		2
738c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VRF2VSEL		4
748c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VMMC1VSEL		6
758c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VMMC2VSEL		9
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VVIBVSEL_M	(3 << 0)
788c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VRF1VSEL_M	(3 << 2)
798c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VRF2VSEL_M	(3 << 4)
808c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VMMC1VSEL_M	(7 << 6)
818c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORSETTING1_VMMC2VSEL_M	(7 << 9)
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE0		32
848c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VAUDIOEN		(1 << 0)
858c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VIOHIEN		(1 << 3)
868c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VIOLOEN		(1 << 6)
878c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VDIGEN		(1 << 9)
888c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VGENEN		(1 << 12)
898c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VRFDIGEN		(1 << 15)
908c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VRFREFEN		(1 << 18)
918c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE0_VRFCPEN		(1 << 21)
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE1		33
948c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VSIMEN		(1 << 0)
958c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VESIMEN		(1 << 3)
968c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VCAMEN		(1 << 6)
978c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VRFBGEN		(1 << 9)
988c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VVIBEN		(1 << 11)
998c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VRF1EN		(1 << 12)
1008c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VRF2EN		(1 << 15)
1018c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VMMC1EN		(1 << 18)
1028c2ecf20Sopenharmony_ci#define MC13783_REG_REGULATORMODE1_VMMC2EN		(1 << 21)
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci#define MC13783_REG_POWERMISC			34
1058c2ecf20Sopenharmony_ci#define MC13783_REG_POWERMISC_GPO1EN			(1 << 6)
1068c2ecf20Sopenharmony_ci#define MC13783_REG_POWERMISC_GPO2EN			(1 << 8)
1078c2ecf20Sopenharmony_ci#define MC13783_REG_POWERMISC_GPO3EN			(1 << 10)
1088c2ecf20Sopenharmony_ci#define MC13783_REG_POWERMISC_GPO4EN			(1 << 12)
1098c2ecf20Sopenharmony_ci#define MC13783_REG_POWERMISC_PWGT1SPIEN		(1 << 15)
1108c2ecf20Sopenharmony_ci#define MC13783_REG_POWERMISC_PWGT2SPIEN		(1 << 16)
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#define MC13783_REG_POWERMISC_PWGTSPI_M			(3 << 15)
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/* Voltage Values */
1168c2ecf20Sopenharmony_cistatic const int mc13783_sw1x_val[] = {
1178c2ecf20Sopenharmony_ci	900000, 925000, 950000, 975000,
1188c2ecf20Sopenharmony_ci	1000000, 1025000, 1050000, 1075000,
1198c2ecf20Sopenharmony_ci	1100000, 1125000, 1150000, 1175000,
1208c2ecf20Sopenharmony_ci	1200000, 1225000, 1250000, 1275000,
1218c2ecf20Sopenharmony_ci	1300000, 1325000, 1350000, 1375000,
1228c2ecf20Sopenharmony_ci	1400000, 1425000, 1450000, 1475000,
1238c2ecf20Sopenharmony_ci	1500000, 1525000, 1550000, 1575000,
1248c2ecf20Sopenharmony_ci	1600000, 1625000, 1650000, 1675000,
1258c2ecf20Sopenharmony_ci	1700000, 1700000, 1700000, 1700000,
1268c2ecf20Sopenharmony_ci	1800000, 1800000, 1800000, 1800000,
1278c2ecf20Sopenharmony_ci	1850000, 1850000, 1850000, 1850000,
1288c2ecf20Sopenharmony_ci	2000000, 2000000, 2000000, 2000000,
1298c2ecf20Sopenharmony_ci	2100000, 2100000, 2100000, 2100000,
1308c2ecf20Sopenharmony_ci	2200000, 2200000, 2200000, 2200000,
1318c2ecf20Sopenharmony_ci	2200000, 2200000, 2200000, 2200000,
1328c2ecf20Sopenharmony_ci	2200000, 2200000, 2200000, 2200000,
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic const int mc13783_sw2x_val[] = {
1368c2ecf20Sopenharmony_ci	900000, 925000, 950000, 975000,
1378c2ecf20Sopenharmony_ci	1000000, 1025000, 1050000, 1075000,
1388c2ecf20Sopenharmony_ci	1100000, 1125000, 1150000, 1175000,
1398c2ecf20Sopenharmony_ci	1200000, 1225000, 1250000, 1275000,
1408c2ecf20Sopenharmony_ci	1300000, 1325000, 1350000, 1375000,
1418c2ecf20Sopenharmony_ci	1400000, 1425000, 1450000, 1475000,
1428c2ecf20Sopenharmony_ci	1500000, 1525000, 1550000, 1575000,
1438c2ecf20Sopenharmony_ci	1600000, 1625000, 1650000, 1675000,
1448c2ecf20Sopenharmony_ci	1700000, 1700000, 1700000, 1700000,
1458c2ecf20Sopenharmony_ci	1800000, 1800000, 1800000, 1800000,
1468c2ecf20Sopenharmony_ci	1900000, 1900000, 1900000, 1900000,
1478c2ecf20Sopenharmony_ci	2000000, 2000000, 2000000, 2000000,
1488c2ecf20Sopenharmony_ci	2100000, 2100000, 2100000, 2100000,
1498c2ecf20Sopenharmony_ci	2200000, 2200000, 2200000, 2200000,
1508c2ecf20Sopenharmony_ci	2200000, 2200000, 2200000, 2200000,
1518c2ecf20Sopenharmony_ci	2200000, 2200000, 2200000, 2200000,
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic const unsigned int mc13783_sw3_val[] = {
1558c2ecf20Sopenharmony_ci	5000000, 5000000, 5000000, 5500000,
1568c2ecf20Sopenharmony_ci};
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic const unsigned int mc13783_vaudio_val[] = {
1598c2ecf20Sopenharmony_ci	2775000,
1608c2ecf20Sopenharmony_ci};
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic const unsigned int mc13783_viohi_val[] = {
1638c2ecf20Sopenharmony_ci	2775000,
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic const unsigned int mc13783_violo_val[] = {
1678c2ecf20Sopenharmony_ci	1200000, 1300000, 1500000, 1800000,
1688c2ecf20Sopenharmony_ci};
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic const unsigned int mc13783_vdig_val[] = {
1718c2ecf20Sopenharmony_ci	1200000, 1300000, 1500000, 1800000,
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic const unsigned int mc13783_vgen_val[] = {
1758c2ecf20Sopenharmony_ci	1200000, 1300000, 1500000, 1800000,
1768c2ecf20Sopenharmony_ci	1100000, 2000000, 2775000, 2400000,
1778c2ecf20Sopenharmony_ci};
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic const unsigned int mc13783_vrfdig_val[] = {
1808c2ecf20Sopenharmony_ci	1200000, 1500000, 1800000, 1875000,
1818c2ecf20Sopenharmony_ci};
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic const unsigned int mc13783_vrfref_val[] = {
1848c2ecf20Sopenharmony_ci	2475000, 2600000, 2700000, 2775000,
1858c2ecf20Sopenharmony_ci};
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic const unsigned int mc13783_vrfcp_val[] = {
1888c2ecf20Sopenharmony_ci	2700000, 2775000,
1898c2ecf20Sopenharmony_ci};
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic const unsigned int mc13783_vsim_val[] = {
1928c2ecf20Sopenharmony_ci	1800000, 2900000, 3000000,
1938c2ecf20Sopenharmony_ci};
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cistatic const unsigned int mc13783_vesim_val[] = {
1968c2ecf20Sopenharmony_ci	1800000, 2900000,
1978c2ecf20Sopenharmony_ci};
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic const unsigned int mc13783_vcam_val[] = {
2008c2ecf20Sopenharmony_ci	1500000, 1800000, 2500000, 2550000,
2018c2ecf20Sopenharmony_ci	2600000, 2750000, 2800000, 3000000,
2028c2ecf20Sopenharmony_ci};
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic const unsigned int mc13783_vrfbg_val[] = {
2058c2ecf20Sopenharmony_ci	1250000,
2068c2ecf20Sopenharmony_ci};
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic const unsigned int mc13783_vvib_val[] = {
2098c2ecf20Sopenharmony_ci	1300000, 1800000, 2000000, 3000000,
2108c2ecf20Sopenharmony_ci};
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic const unsigned int mc13783_vmmc_val[] = {
2138c2ecf20Sopenharmony_ci	1600000, 1800000, 2000000, 2600000,
2148c2ecf20Sopenharmony_ci	2700000, 2800000, 2900000, 3000000,
2158c2ecf20Sopenharmony_ci};
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cistatic const unsigned int mc13783_vrf_val[] = {
2188c2ecf20Sopenharmony_ci	1500000, 1875000, 2700000, 2775000,
2198c2ecf20Sopenharmony_ci};
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cistatic const unsigned int mc13783_gpo_val[] = {
2228c2ecf20Sopenharmony_ci	3100000,
2238c2ecf20Sopenharmony_ci};
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic const unsigned int mc13783_pwgtdrv_val[] = {
2268c2ecf20Sopenharmony_ci	5500000,
2278c2ecf20Sopenharmony_ci};
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cistatic const struct regulator_ops mc13783_gpo_regulator_ops;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci#define MC13783_DEFINE(prefix, name, node, reg, vsel_reg, voltages)	\
2328c2ecf20Sopenharmony_ci	MC13xxx_DEFINE(MC13783_REG_, name, node, reg, vsel_reg, voltages, \
2338c2ecf20Sopenharmony_ci			mc13xxx_regulator_ops)
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci#define MC13783_FIXED_DEFINE(prefix, name, node, reg, voltages)		\
2368c2ecf20Sopenharmony_ci	MC13xxx_FIXED_DEFINE(MC13783_REG_, name, node, reg, voltages,	\
2378c2ecf20Sopenharmony_ci			mc13xxx_fixed_regulator_ops)
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci#define MC13783_GPO_DEFINE(prefix, name, node, reg, voltages)		\
2408c2ecf20Sopenharmony_ci	MC13xxx_GPO_DEFINE(MC13783_REG_, name, node, reg, voltages,	\
2418c2ecf20Sopenharmony_ci			mc13783_gpo_regulator_ops)
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci#define MC13783_DEFINE_SW(_name, _node, _reg, _vsel_reg, _voltages)	\
2448c2ecf20Sopenharmony_ci	MC13783_DEFINE(REG, _name, _node, _reg, _vsel_reg, _voltages)
2458c2ecf20Sopenharmony_ci#define MC13783_DEFINE_REGU(_name, _node, _reg, _vsel_reg, _voltages)	\
2468c2ecf20Sopenharmony_ci	MC13783_DEFINE(REG, _name, _node, _reg, _vsel_reg, _voltages)
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic struct mc13xxx_regulator mc13783_regulators[] = {
2498c2ecf20Sopenharmony_ci	MC13783_DEFINE_SW(SW1A, sw1a, SWITCHERS0, SWITCHERS0, mc13783_sw1x_val),
2508c2ecf20Sopenharmony_ci	MC13783_DEFINE_SW(SW1B, sw1b, SWITCHERS1, SWITCHERS1, mc13783_sw1x_val),
2518c2ecf20Sopenharmony_ci	MC13783_DEFINE_SW(SW2A, sw2a, SWITCHERS2, SWITCHERS2, mc13783_sw2x_val),
2528c2ecf20Sopenharmony_ci	MC13783_DEFINE_SW(SW2B, sw2b, SWITCHERS3, SWITCHERS3, mc13783_sw2x_val),
2538c2ecf20Sopenharmony_ci	MC13783_DEFINE_SW(SW3, sw3, SWITCHERS5, SWITCHERS5, mc13783_sw3_val),
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	MC13783_FIXED_DEFINE(REG, VAUDIO, vaudio, REGULATORMODE0, mc13783_vaudio_val),
2568c2ecf20Sopenharmony_ci	MC13783_FIXED_DEFINE(REG, VIOHI, viohi, REGULATORMODE0, mc13783_viohi_val),
2578c2ecf20Sopenharmony_ci	MC13783_DEFINE_REGU(VIOLO, violo, REGULATORMODE0, REGULATORSETTING0,
2588c2ecf20Sopenharmony_ci			    mc13783_violo_val),
2598c2ecf20Sopenharmony_ci	MC13783_DEFINE_REGU(VDIG, vdig, REGULATORMODE0, REGULATORSETTING0,
2608c2ecf20Sopenharmony_ci			    mc13783_vdig_val),
2618c2ecf20Sopenharmony_ci	MC13783_DEFINE_REGU(VGEN, vgen, REGULATORMODE0, REGULATORSETTING0,
2628c2ecf20Sopenharmony_ci			    mc13783_vgen_val),
2638c2ecf20Sopenharmony_ci	MC13783_DEFINE_REGU(VRFDIG, vrfdig, REGULATORMODE0, REGULATORSETTING0,
2648c2ecf20Sopenharmony_ci			    mc13783_vrfdig_val),
2658c2ecf20Sopenharmony_ci	MC13783_DEFINE_REGU(VRFREF, vrfref, REGULATORMODE0, REGULATORSETTING0,
2668c2ecf20Sopenharmony_ci			    mc13783_vrfref_val),
2678c2ecf20Sopenharmony_ci	MC13783_DEFINE_REGU(VRFCP, vrfcp, REGULATORMODE0, REGULATORSETTING0,
2688c2ecf20Sopenharmony_ci			    mc13783_vrfcp_val),
2698c2ecf20Sopenharmony_ci	MC13783_DEFINE_REGU(VSIM, vsim, REGULATORMODE1, REGULATORSETTING0,
2708c2ecf20Sopenharmony_ci			    mc13783_vsim_val),
2718c2ecf20Sopenharmony_ci	MC13783_DEFINE_REGU(VESIM, vesim, REGULATORMODE1, REGULATORSETTING0,
2728c2ecf20Sopenharmony_ci			    mc13783_vesim_val),
2738c2ecf20Sopenharmony_ci	MC13783_DEFINE_REGU(VCAM, vcam, REGULATORMODE1, REGULATORSETTING0,
2748c2ecf20Sopenharmony_ci			    mc13783_vcam_val),
2758c2ecf20Sopenharmony_ci	MC13783_FIXED_DEFINE(REG, VRFBG, vrfbg, REGULATORMODE1, mc13783_vrfbg_val),
2768c2ecf20Sopenharmony_ci	MC13783_DEFINE_REGU(VVIB, vvib, REGULATORMODE1, REGULATORSETTING1,
2778c2ecf20Sopenharmony_ci			    mc13783_vvib_val),
2788c2ecf20Sopenharmony_ci	MC13783_DEFINE_REGU(VRF1, vrf1, REGULATORMODE1, REGULATORSETTING1,
2798c2ecf20Sopenharmony_ci			    mc13783_vrf_val),
2808c2ecf20Sopenharmony_ci	MC13783_DEFINE_REGU(VRF2, vrf2, REGULATORMODE1, REGULATORSETTING1,
2818c2ecf20Sopenharmony_ci			    mc13783_vrf_val),
2828c2ecf20Sopenharmony_ci	MC13783_DEFINE_REGU(VMMC1, vmmc1, REGULATORMODE1, REGULATORSETTING1,
2838c2ecf20Sopenharmony_ci			    mc13783_vmmc_val),
2848c2ecf20Sopenharmony_ci	MC13783_DEFINE_REGU(VMMC2, vmmc2, REGULATORMODE1, REGULATORSETTING1,
2858c2ecf20Sopenharmony_ci			    mc13783_vmmc_val),
2868c2ecf20Sopenharmony_ci	MC13783_GPO_DEFINE(REG, GPO1, gpo1, POWERMISC, mc13783_gpo_val),
2878c2ecf20Sopenharmony_ci	MC13783_GPO_DEFINE(REG, GPO2, gpo1, POWERMISC, mc13783_gpo_val),
2888c2ecf20Sopenharmony_ci	MC13783_GPO_DEFINE(REG, GPO3, gpo1, POWERMISC, mc13783_gpo_val),
2898c2ecf20Sopenharmony_ci	MC13783_GPO_DEFINE(REG, GPO4, gpo1, POWERMISC, mc13783_gpo_val),
2908c2ecf20Sopenharmony_ci	MC13783_GPO_DEFINE(REG, PWGT1SPI, pwgt1spi, POWERMISC, mc13783_pwgtdrv_val),
2918c2ecf20Sopenharmony_ci	MC13783_GPO_DEFINE(REG, PWGT2SPI, pwgt2spi, POWERMISC, mc13783_pwgtdrv_val),
2928c2ecf20Sopenharmony_ci};
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_cistatic int mc13783_powermisc_rmw(struct mc13xxx_regulator_priv *priv, u32 mask,
2958c2ecf20Sopenharmony_ci		u32 val)
2968c2ecf20Sopenharmony_ci{
2978c2ecf20Sopenharmony_ci	struct mc13xxx *mc13783 = priv->mc13xxx;
2988c2ecf20Sopenharmony_ci	int ret;
2998c2ecf20Sopenharmony_ci	u32 valread;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	BUG_ON(val & ~mask);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	mc13xxx_lock(priv->mc13xxx);
3048c2ecf20Sopenharmony_ci	ret = mc13xxx_reg_read(mc13783, MC13783_REG_POWERMISC, &valread);
3058c2ecf20Sopenharmony_ci	if (ret)
3068c2ecf20Sopenharmony_ci		goto out;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	/* Update the stored state for Power Gates. */
3098c2ecf20Sopenharmony_ci	priv->powermisc_pwgt_state =
3108c2ecf20Sopenharmony_ci				(priv->powermisc_pwgt_state & ~mask) | val;
3118c2ecf20Sopenharmony_ci	priv->powermisc_pwgt_state &= MC13783_REG_POWERMISC_PWGTSPI_M;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	/* Construct the new register value */
3148c2ecf20Sopenharmony_ci	valread = (valread & ~mask) | val;
3158c2ecf20Sopenharmony_ci	/* Overwrite the PWGTxEN with the stored version */
3168c2ecf20Sopenharmony_ci	valread = (valread & ~MC13783_REG_POWERMISC_PWGTSPI_M) |
3178c2ecf20Sopenharmony_ci						priv->powermisc_pwgt_state;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	ret = mc13xxx_reg_write(mc13783, MC13783_REG_POWERMISC, valread);
3208c2ecf20Sopenharmony_ciout:
3218c2ecf20Sopenharmony_ci	mc13xxx_unlock(priv->mc13xxx);
3228c2ecf20Sopenharmony_ci	return ret;
3238c2ecf20Sopenharmony_ci}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_cistatic int mc13783_gpo_regulator_enable(struct regulator_dev *rdev)
3268c2ecf20Sopenharmony_ci{
3278c2ecf20Sopenharmony_ci	struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
3288c2ecf20Sopenharmony_ci	struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
3298c2ecf20Sopenharmony_ci	int id = rdev_get_id(rdev);
3308c2ecf20Sopenharmony_ci	u32 en_val = mc13xxx_regulators[id].enable_bit;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	/* Power Gate enable value is 0 */
3358c2ecf20Sopenharmony_ci	if (id == MC13783_REG_PWGT1SPI ||
3368c2ecf20Sopenharmony_ci	    id == MC13783_REG_PWGT2SPI)
3378c2ecf20Sopenharmony_ci		en_val = 0;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	return mc13783_powermisc_rmw(priv, mc13xxx_regulators[id].enable_bit,
3408c2ecf20Sopenharmony_ci					en_val);
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistatic int mc13783_gpo_regulator_disable(struct regulator_dev *rdev)
3448c2ecf20Sopenharmony_ci{
3458c2ecf20Sopenharmony_ci	struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
3468c2ecf20Sopenharmony_ci	struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
3478c2ecf20Sopenharmony_ci	int id = rdev_get_id(rdev);
3488c2ecf20Sopenharmony_ci	u32 dis_val = 0;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	/* Power Gate disable value is 1 */
3538c2ecf20Sopenharmony_ci	if (id == MC13783_REG_PWGT1SPI ||
3548c2ecf20Sopenharmony_ci	    id == MC13783_REG_PWGT2SPI)
3558c2ecf20Sopenharmony_ci		dis_val = mc13xxx_regulators[id].enable_bit;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	return mc13783_powermisc_rmw(priv, mc13xxx_regulators[id].enable_bit,
3588c2ecf20Sopenharmony_ci					dis_val);
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_cistatic int mc13783_gpo_regulator_is_enabled(struct regulator_dev *rdev)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
3648c2ecf20Sopenharmony_ci	struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
3658c2ecf20Sopenharmony_ci	int ret, id = rdev_get_id(rdev);
3668c2ecf20Sopenharmony_ci	unsigned int val;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	mc13xxx_lock(priv->mc13xxx);
3698c2ecf20Sopenharmony_ci	ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val);
3708c2ecf20Sopenharmony_ci	mc13xxx_unlock(priv->mc13xxx);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	if (ret)
3738c2ecf20Sopenharmony_ci		return ret;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	/* Power Gates state is stored in powermisc_pwgt_state
3768c2ecf20Sopenharmony_ci	 * where the meaning of bits is negated */
3778c2ecf20Sopenharmony_ci	val = (val & ~MC13783_REG_POWERMISC_PWGTSPI_M) |
3788c2ecf20Sopenharmony_ci	      (priv->powermisc_pwgt_state ^ MC13783_REG_POWERMISC_PWGTSPI_M);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	return (val & mc13xxx_regulators[id].enable_bit) != 0;
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_cistatic const struct regulator_ops mc13783_gpo_regulator_ops = {
3848c2ecf20Sopenharmony_ci	.enable = mc13783_gpo_regulator_enable,
3858c2ecf20Sopenharmony_ci	.disable = mc13783_gpo_regulator_disable,
3868c2ecf20Sopenharmony_ci	.is_enabled = mc13783_gpo_regulator_is_enabled,
3878c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_table,
3888c2ecf20Sopenharmony_ci	.set_voltage = mc13xxx_fixed_regulator_set_voltage,
3898c2ecf20Sopenharmony_ci};
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic int mc13783_regulator_probe(struct platform_device *pdev)
3928c2ecf20Sopenharmony_ci{
3938c2ecf20Sopenharmony_ci	struct mc13xxx_regulator_priv *priv;
3948c2ecf20Sopenharmony_ci	struct mc13xxx *mc13783 = dev_get_drvdata(pdev->dev.parent);
3958c2ecf20Sopenharmony_ci	struct mc13xxx_regulator_platform_data *pdata =
3968c2ecf20Sopenharmony_ci		dev_get_platdata(&pdev->dev);
3978c2ecf20Sopenharmony_ci	struct mc13xxx_regulator_init_data *mc13xxx_data;
3988c2ecf20Sopenharmony_ci	struct regulator_config config = { };
3998c2ecf20Sopenharmony_ci	int i, num_regulators;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	num_regulators = mc13xxx_get_num_regulators_dt(pdev);
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	if (num_regulators <= 0 && pdata)
4048c2ecf20Sopenharmony_ci		num_regulators = pdata->num_regulators;
4058c2ecf20Sopenharmony_ci	if (num_regulators <= 0)
4068c2ecf20Sopenharmony_ci		return -EINVAL;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	priv = devm_kzalloc(&pdev->dev,
4098c2ecf20Sopenharmony_ci			    struct_size(priv, regulators, num_regulators),
4108c2ecf20Sopenharmony_ci			    GFP_KERNEL);
4118c2ecf20Sopenharmony_ci	if (!priv)
4128c2ecf20Sopenharmony_ci		return -ENOMEM;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	priv->num_regulators = num_regulators;
4158c2ecf20Sopenharmony_ci	priv->mc13xxx_regulators = mc13783_regulators;
4168c2ecf20Sopenharmony_ci	priv->mc13xxx = mc13783;
4178c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, priv);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	mc13xxx_data = mc13xxx_parse_regulators_dt(pdev, mc13783_regulators,
4208c2ecf20Sopenharmony_ci					ARRAY_SIZE(mc13783_regulators));
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	for (i = 0; i < priv->num_regulators; i++) {
4238c2ecf20Sopenharmony_ci		struct regulator_init_data *init_data;
4248c2ecf20Sopenharmony_ci		struct regulator_desc *desc;
4258c2ecf20Sopenharmony_ci		struct device_node *node = NULL;
4268c2ecf20Sopenharmony_ci		int id;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci		if (mc13xxx_data) {
4298c2ecf20Sopenharmony_ci			id = mc13xxx_data[i].id;
4308c2ecf20Sopenharmony_ci			init_data = mc13xxx_data[i].init_data;
4318c2ecf20Sopenharmony_ci			node = mc13xxx_data[i].node;
4328c2ecf20Sopenharmony_ci		} else {
4338c2ecf20Sopenharmony_ci			id = pdata->regulators[i].id;
4348c2ecf20Sopenharmony_ci			init_data = pdata->regulators[i].init_data;
4358c2ecf20Sopenharmony_ci		}
4368c2ecf20Sopenharmony_ci		desc = &mc13783_regulators[id].desc;
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci		config.dev = &pdev->dev;
4398c2ecf20Sopenharmony_ci		config.init_data = init_data;
4408c2ecf20Sopenharmony_ci		config.driver_data = priv;
4418c2ecf20Sopenharmony_ci		config.of_node = node;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci		priv->regulators[i] = devm_regulator_register(&pdev->dev, desc,
4448c2ecf20Sopenharmony_ci							      &config);
4458c2ecf20Sopenharmony_ci		if (IS_ERR(priv->regulators[i])) {
4468c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "failed to register regulator %s\n",
4478c2ecf20Sopenharmony_ci				mc13783_regulators[i].desc.name);
4488c2ecf20Sopenharmony_ci			return PTR_ERR(priv->regulators[i]);
4498c2ecf20Sopenharmony_ci		}
4508c2ecf20Sopenharmony_ci	}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	return 0;
4538c2ecf20Sopenharmony_ci}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_cistatic struct platform_driver mc13783_regulator_driver = {
4568c2ecf20Sopenharmony_ci	.driver	= {
4578c2ecf20Sopenharmony_ci		.name	= "mc13783-regulator",
4588c2ecf20Sopenharmony_ci	},
4598c2ecf20Sopenharmony_ci	.probe		= mc13783_regulator_probe,
4608c2ecf20Sopenharmony_ci};
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_cistatic int __init mc13783_regulator_init(void)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	return platform_driver_register(&mc13783_regulator_driver);
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_cisubsys_initcall(mc13783_regulator_init);
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_cistatic void __exit mc13783_regulator_exit(void)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	platform_driver_unregister(&mc13783_regulator_driver);
4718c2ecf20Sopenharmony_ci}
4728c2ecf20Sopenharmony_cimodule_exit(mc13783_regulator_exit);
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
4758c2ecf20Sopenharmony_ciMODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
4768c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Regulator Driver for Freescale MC13783 PMIC");
4778c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:mc13783-regulator");
478