xref: /OK3568_Linux_fs/kernel/drivers/mfd/tps80031.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * tps80031.c -- TI TPS80031/TPS80032 mfd core driver.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * MFD core driver for TI TPS80031/TPS80032 Fully Integrated
5*4882a593Smuzhiyun  * Power Management with Power Path and Battery Charger
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (c) 2012, NVIDIA Corporation.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Author: Laxman Dewangan <ldewangan@nvidia.com>
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or
12*4882a593Smuzhiyun  * modify it under the terms of the GNU General Public License as
13*4882a593Smuzhiyun  * published by the Free Software Foundation version 2.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
16*4882a593Smuzhiyun  * whether express or implied; without even the implied warranty of
17*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18*4882a593Smuzhiyun  * General Public License for more details.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
21*4882a593Smuzhiyun  * along with this program; if not, write to the Free Software
22*4882a593Smuzhiyun  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23*4882a593Smuzhiyun  * 02111-1307, USA
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include <linux/err.h>
27*4882a593Smuzhiyun #include <linux/i2c.h>
28*4882a593Smuzhiyun #include <linux/init.h>
29*4882a593Smuzhiyun #include <linux/interrupt.h>
30*4882a593Smuzhiyun #include <linux/irq.h>
31*4882a593Smuzhiyun #include <linux/mfd/core.h>
32*4882a593Smuzhiyun #include <linux/mfd/tps80031.h>
33*4882a593Smuzhiyun #include <linux/pm.h>
34*4882a593Smuzhiyun #include <linux/regmap.h>
35*4882a593Smuzhiyun #include <linux/slab.h>
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun static struct resource tps80031_rtc_resources[] = {
38*4882a593Smuzhiyun 	{
39*4882a593Smuzhiyun 		.start = TPS80031_INT_RTC_ALARM,
40*4882a593Smuzhiyun 		.end = TPS80031_INT_RTC_ALARM,
41*4882a593Smuzhiyun 		.flags = IORESOURCE_IRQ,
42*4882a593Smuzhiyun 	},
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /* TPS80031 sub mfd devices */
46*4882a593Smuzhiyun static const struct mfd_cell tps80031_cell[] = {
47*4882a593Smuzhiyun 	{
48*4882a593Smuzhiyun 		.name = "tps80031-pmic",
49*4882a593Smuzhiyun 	},
50*4882a593Smuzhiyun 	{
51*4882a593Smuzhiyun 		.name = "tps80031-clock",
52*4882a593Smuzhiyun 	},
53*4882a593Smuzhiyun 	{
54*4882a593Smuzhiyun 		.name = "tps80031-rtc",
55*4882a593Smuzhiyun 		.num_resources = ARRAY_SIZE(tps80031_rtc_resources),
56*4882a593Smuzhiyun 		.resources = tps80031_rtc_resources,
57*4882a593Smuzhiyun 	},
58*4882a593Smuzhiyun 	{
59*4882a593Smuzhiyun 		.name = "tps80031-gpadc",
60*4882a593Smuzhiyun 	},
61*4882a593Smuzhiyun 	{
62*4882a593Smuzhiyun 		.name = "tps80031-fuel-gauge",
63*4882a593Smuzhiyun 	},
64*4882a593Smuzhiyun 	{
65*4882a593Smuzhiyun 		.name = "tps80031-charger",
66*4882a593Smuzhiyun 	},
67*4882a593Smuzhiyun };
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun static int tps80031_slave_address[TPS80031_NUM_SLAVES] = {
70*4882a593Smuzhiyun 	TPS80031_I2C_ID0_ADDR,
71*4882a593Smuzhiyun 	TPS80031_I2C_ID1_ADDR,
72*4882a593Smuzhiyun 	TPS80031_I2C_ID2_ADDR,
73*4882a593Smuzhiyun 	TPS80031_I2C_ID3_ADDR,
74*4882a593Smuzhiyun };
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun struct tps80031_pupd_data {
77*4882a593Smuzhiyun 	u8	reg;
78*4882a593Smuzhiyun 	u8	pullup_bit;
79*4882a593Smuzhiyun 	u8	pulldown_bit;
80*4882a593Smuzhiyun };
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun #define TPS80031_IRQ(_reg, _mask)			\
83*4882a593Smuzhiyun 	{							\
84*4882a593Smuzhiyun 		.reg_offset = (TPS80031_INT_MSK_LINE_##_reg) -	\
85*4882a593Smuzhiyun 				TPS80031_INT_MSK_LINE_A,	\
86*4882a593Smuzhiyun 		.mask = BIT(_mask),				\
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun static const struct regmap_irq tps80031_main_irqs[] = {
90*4882a593Smuzhiyun 	[TPS80031_INT_PWRON]		= TPS80031_IRQ(A, 0),
91*4882a593Smuzhiyun 	[TPS80031_INT_RPWRON]		= TPS80031_IRQ(A, 1),
92*4882a593Smuzhiyun 	[TPS80031_INT_SYS_VLOW]		= TPS80031_IRQ(A, 2),
93*4882a593Smuzhiyun 	[TPS80031_INT_RTC_ALARM]	= TPS80031_IRQ(A, 3),
94*4882a593Smuzhiyun 	[TPS80031_INT_RTC_PERIOD]	= TPS80031_IRQ(A, 4),
95*4882a593Smuzhiyun 	[TPS80031_INT_HOT_DIE]		= TPS80031_IRQ(A, 5),
96*4882a593Smuzhiyun 	[TPS80031_INT_VXX_SHORT]	= TPS80031_IRQ(A, 6),
97*4882a593Smuzhiyun 	[TPS80031_INT_SPDURATION]	= TPS80031_IRQ(A, 7),
98*4882a593Smuzhiyun 	[TPS80031_INT_WATCHDOG]		= TPS80031_IRQ(B, 0),
99*4882a593Smuzhiyun 	[TPS80031_INT_BAT]		= TPS80031_IRQ(B, 1),
100*4882a593Smuzhiyun 	[TPS80031_INT_SIM]		= TPS80031_IRQ(B, 2),
101*4882a593Smuzhiyun 	[TPS80031_INT_MMC]		= TPS80031_IRQ(B, 3),
102*4882a593Smuzhiyun 	[TPS80031_INT_RES]		= TPS80031_IRQ(B, 4),
103*4882a593Smuzhiyun 	[TPS80031_INT_GPADC_RT]		= TPS80031_IRQ(B, 5),
104*4882a593Smuzhiyun 	[TPS80031_INT_GPADC_SW2_EOC]	= TPS80031_IRQ(B, 6),
105*4882a593Smuzhiyun 	[TPS80031_INT_CC_AUTOCAL]	= TPS80031_IRQ(B, 7),
106*4882a593Smuzhiyun 	[TPS80031_INT_ID_WKUP]		= TPS80031_IRQ(C, 0),
107*4882a593Smuzhiyun 	[TPS80031_INT_VBUSS_WKUP]	= TPS80031_IRQ(C, 1),
108*4882a593Smuzhiyun 	[TPS80031_INT_ID]		= TPS80031_IRQ(C, 2),
109*4882a593Smuzhiyun 	[TPS80031_INT_VBUS]		= TPS80031_IRQ(C, 3),
110*4882a593Smuzhiyun 	[TPS80031_INT_CHRG_CTRL]	= TPS80031_IRQ(C, 4),
111*4882a593Smuzhiyun 	[TPS80031_INT_EXT_CHRG]		= TPS80031_IRQ(C, 5),
112*4882a593Smuzhiyun 	[TPS80031_INT_INT_CHRG]		= TPS80031_IRQ(C, 6),
113*4882a593Smuzhiyun 	[TPS80031_INT_RES2]		= TPS80031_IRQ(C, 7),
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun static struct regmap_irq_chip tps80031_irq_chip = {
117*4882a593Smuzhiyun 	.name = "tps80031",
118*4882a593Smuzhiyun 	.irqs = tps80031_main_irqs,
119*4882a593Smuzhiyun 	.num_irqs = ARRAY_SIZE(tps80031_main_irqs),
120*4882a593Smuzhiyun 	.num_regs = 3,
121*4882a593Smuzhiyun 	.status_base = TPS80031_INT_STS_A,
122*4882a593Smuzhiyun 	.mask_base = TPS80031_INT_MSK_LINE_A,
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun #define PUPD_DATA(_reg, _pulldown_bit, _pullup_bit)	\
126*4882a593Smuzhiyun 	{						\
127*4882a593Smuzhiyun 		.reg = TPS80031_CFG_INPUT_PUPD##_reg,	\
128*4882a593Smuzhiyun 		.pulldown_bit = _pulldown_bit,		\
129*4882a593Smuzhiyun 		.pullup_bit = _pullup_bit,		\
130*4882a593Smuzhiyun 	}
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun static const struct tps80031_pupd_data tps80031_pupds[] = {
133*4882a593Smuzhiyun 	[TPS80031_PREQ1]		= PUPD_DATA(1, BIT(0),	BIT(1)),
134*4882a593Smuzhiyun 	[TPS80031_PREQ2A]		= PUPD_DATA(1, BIT(2),	BIT(3)),
135*4882a593Smuzhiyun 	[TPS80031_PREQ2B]		= PUPD_DATA(1, BIT(4),	BIT(5)),
136*4882a593Smuzhiyun 	[TPS80031_PREQ2C]		= PUPD_DATA(1, BIT(6),	BIT(7)),
137*4882a593Smuzhiyun 	[TPS80031_PREQ3]		= PUPD_DATA(2, BIT(0),	BIT(1)),
138*4882a593Smuzhiyun 	[TPS80031_NRES_WARM]		= PUPD_DATA(2, 0,	BIT(2)),
139*4882a593Smuzhiyun 	[TPS80031_PWM_FORCE]		= PUPD_DATA(2, BIT(5),	0),
140*4882a593Smuzhiyun 	[TPS80031_CHRG_EXT_CHRG_STATZ]	= PUPD_DATA(2, 0,	BIT(6)),
141*4882a593Smuzhiyun 	[TPS80031_SIM]			= PUPD_DATA(3, BIT(0),	BIT(1)),
142*4882a593Smuzhiyun 	[TPS80031_MMC]			= PUPD_DATA(3, BIT(2),	BIT(3)),
143*4882a593Smuzhiyun 	[TPS80031_GPADC_START]		= PUPD_DATA(3, BIT(4),	0),
144*4882a593Smuzhiyun 	[TPS80031_DVSI2C_SCL]		= PUPD_DATA(4, 0,	BIT(0)),
145*4882a593Smuzhiyun 	[TPS80031_DVSI2C_SDA]		= PUPD_DATA(4, 0,	BIT(1)),
146*4882a593Smuzhiyun 	[TPS80031_CTLI2C_SCL]		= PUPD_DATA(4, 0,	BIT(2)),
147*4882a593Smuzhiyun 	[TPS80031_CTLI2C_SDA]		= PUPD_DATA(4, 0,	BIT(3)),
148*4882a593Smuzhiyun };
149*4882a593Smuzhiyun static struct tps80031 *tps80031_power_off_dev;
150*4882a593Smuzhiyun 
tps80031_ext_power_req_config(struct device * dev,unsigned long ext_ctrl_flag,int preq_bit,int state_reg_add,int trans_reg_add)151*4882a593Smuzhiyun int tps80031_ext_power_req_config(struct device *dev,
152*4882a593Smuzhiyun 		unsigned long ext_ctrl_flag, int preq_bit,
153*4882a593Smuzhiyun 		int state_reg_add, int trans_reg_add)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun 	u8 res_ass_reg = 0;
156*4882a593Smuzhiyun 	int preq_mask_bit = 0;
157*4882a593Smuzhiyun 	int ret;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	if (!(ext_ctrl_flag & TPS80031_EXT_PWR_REQ))
160*4882a593Smuzhiyun 		return 0;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	if (ext_ctrl_flag & TPS80031_PWR_REQ_INPUT_PREQ1) {
163*4882a593Smuzhiyun 		res_ass_reg = TPS80031_PREQ1_RES_ASS_A + (preq_bit >> 3);
164*4882a593Smuzhiyun 		preq_mask_bit = 5;
165*4882a593Smuzhiyun 	} else if (ext_ctrl_flag & TPS80031_PWR_REQ_INPUT_PREQ2) {
166*4882a593Smuzhiyun 		res_ass_reg = TPS80031_PREQ2_RES_ASS_A + (preq_bit >> 3);
167*4882a593Smuzhiyun 		preq_mask_bit = 6;
168*4882a593Smuzhiyun 	} else if (ext_ctrl_flag & TPS80031_PWR_REQ_INPUT_PREQ3) {
169*4882a593Smuzhiyun 		res_ass_reg = TPS80031_PREQ3_RES_ASS_A + (preq_bit >> 3);
170*4882a593Smuzhiyun 		preq_mask_bit = 7;
171*4882a593Smuzhiyun 	}
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	/* Configure REQ_ASS registers */
174*4882a593Smuzhiyun 	ret = tps80031_set_bits(dev, TPS80031_SLAVE_ID1, res_ass_reg,
175*4882a593Smuzhiyun 					BIT(preq_bit & 0x7));
176*4882a593Smuzhiyun 	if (ret < 0) {
177*4882a593Smuzhiyun 		dev_err(dev, "reg 0x%02x setbit failed, err = %d\n",
178*4882a593Smuzhiyun 				res_ass_reg, ret);
179*4882a593Smuzhiyun 		return ret;
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	/* Unmask the PREQ */
183*4882a593Smuzhiyun 	ret = tps80031_clr_bits(dev, TPS80031_SLAVE_ID1,
184*4882a593Smuzhiyun 			TPS80031_PHOENIX_MSK_TRANSITION, BIT(preq_mask_bit));
185*4882a593Smuzhiyun 	if (ret < 0) {
186*4882a593Smuzhiyun 		dev_err(dev, "reg 0x%02x clrbit failed, err = %d\n",
187*4882a593Smuzhiyun 			TPS80031_PHOENIX_MSK_TRANSITION, ret);
188*4882a593Smuzhiyun 		return ret;
189*4882a593Smuzhiyun 	}
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	/* Switch regulator control to resource now */
192*4882a593Smuzhiyun 	if (ext_ctrl_flag & (TPS80031_PWR_REQ_INPUT_PREQ2 |
193*4882a593Smuzhiyun 					TPS80031_PWR_REQ_INPUT_PREQ3)) {
194*4882a593Smuzhiyun 		ret = tps80031_update(dev, TPS80031_SLAVE_ID1, state_reg_add,
195*4882a593Smuzhiyun 						0x0, TPS80031_STATE_MASK);
196*4882a593Smuzhiyun 		if (ret < 0)
197*4882a593Smuzhiyun 			dev_err(dev, "reg 0x%02x update failed, err = %d\n",
198*4882a593Smuzhiyun 				state_reg_add, ret);
199*4882a593Smuzhiyun 	} else {
200*4882a593Smuzhiyun 		ret = tps80031_update(dev, TPS80031_SLAVE_ID1, trans_reg_add,
201*4882a593Smuzhiyun 				TPS80031_TRANS_SLEEP_OFF,
202*4882a593Smuzhiyun 				TPS80031_TRANS_SLEEP_MASK);
203*4882a593Smuzhiyun 		if (ret < 0)
204*4882a593Smuzhiyun 			dev_err(dev, "reg 0x%02x update failed, err = %d\n",
205*4882a593Smuzhiyun 				trans_reg_add, ret);
206*4882a593Smuzhiyun 	}
207*4882a593Smuzhiyun 	return ret;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tps80031_ext_power_req_config);
210*4882a593Smuzhiyun 
tps80031_power_off(void)211*4882a593Smuzhiyun static void tps80031_power_off(void)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun 	dev_info(tps80031_power_off_dev->dev, "switching off PMU\n");
214*4882a593Smuzhiyun 	tps80031_write(tps80031_power_off_dev->dev, TPS80031_SLAVE_ID1,
215*4882a593Smuzhiyun 				TPS80031_PHOENIX_DEV_ON, TPS80031_DEVOFF);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun 
tps80031_pupd_init(struct tps80031 * tps80031,struct tps80031_platform_data * pdata)218*4882a593Smuzhiyun static void tps80031_pupd_init(struct tps80031 *tps80031,
219*4882a593Smuzhiyun 			       struct tps80031_platform_data *pdata)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun 	struct tps80031_pupd_init_data *pupd_init_data = pdata->pupd_init_data;
222*4882a593Smuzhiyun 	int data_size = pdata->pupd_init_data_size;
223*4882a593Smuzhiyun 	int i;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	for (i = 0; i < data_size; ++i) {
226*4882a593Smuzhiyun 		struct tps80031_pupd_init_data *pupd_init = &pupd_init_data[i];
227*4882a593Smuzhiyun 		const struct tps80031_pupd_data *pupd =
228*4882a593Smuzhiyun 			&tps80031_pupds[pupd_init->input_pin];
229*4882a593Smuzhiyun 		u8 update_value = 0;
230*4882a593Smuzhiyun 		u8 update_mask = pupd->pulldown_bit | pupd->pullup_bit;
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 		if (pupd_init->setting == TPS80031_PUPD_PULLDOWN)
233*4882a593Smuzhiyun 			update_value = pupd->pulldown_bit;
234*4882a593Smuzhiyun 		else if (pupd_init->setting == TPS80031_PUPD_PULLUP)
235*4882a593Smuzhiyun 			update_value = pupd->pullup_bit;
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 		tps80031_update(tps80031->dev, TPS80031_SLAVE_ID1, pupd->reg,
238*4882a593Smuzhiyun 				update_value, update_mask);
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
tps80031_init_ext_control(struct tps80031 * tps80031,struct tps80031_platform_data * pdata)242*4882a593Smuzhiyun static int tps80031_init_ext_control(struct tps80031 *tps80031,
243*4882a593Smuzhiyun 			struct tps80031_platform_data *pdata)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	struct device *dev = tps80031->dev;
246*4882a593Smuzhiyun 	int ret;
247*4882a593Smuzhiyun 	int i;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	/* Clear all external control for this rail */
250*4882a593Smuzhiyun 	for (i = 0; i < 9; ++i) {
251*4882a593Smuzhiyun 		ret = tps80031_write(dev, TPS80031_SLAVE_ID1,
252*4882a593Smuzhiyun 				TPS80031_PREQ1_RES_ASS_A + i, 0);
253*4882a593Smuzhiyun 		if (ret < 0) {
254*4882a593Smuzhiyun 			dev_err(dev, "reg 0x%02x write failed, err = %d\n",
255*4882a593Smuzhiyun 				TPS80031_PREQ1_RES_ASS_A + i, ret);
256*4882a593Smuzhiyun 			return ret;
257*4882a593Smuzhiyun 		}
258*4882a593Smuzhiyun 	}
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	/* Mask the PREQ */
261*4882a593Smuzhiyun 	ret = tps80031_set_bits(dev, TPS80031_SLAVE_ID1,
262*4882a593Smuzhiyun 			TPS80031_PHOENIX_MSK_TRANSITION, 0x7 << 5);
263*4882a593Smuzhiyun 	if (ret < 0) {
264*4882a593Smuzhiyun 		dev_err(dev, "reg 0x%02x set_bits failed, err = %d\n",
265*4882a593Smuzhiyun 			TPS80031_PHOENIX_MSK_TRANSITION, ret);
266*4882a593Smuzhiyun 		return ret;
267*4882a593Smuzhiyun 	}
268*4882a593Smuzhiyun 	return ret;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun 
tps80031_irq_init(struct tps80031 * tps80031,int irq,int irq_base)271*4882a593Smuzhiyun static int tps80031_irq_init(struct tps80031 *tps80031, int irq, int irq_base)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun 	struct device *dev = tps80031->dev;
274*4882a593Smuzhiyun 	int i, ret;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	/*
277*4882a593Smuzhiyun 	 * The MASK register used for updating status register when
278*4882a593Smuzhiyun 	 * interrupt occurs and LINE register used to pass the status
279*4882a593Smuzhiyun 	 * to actual interrupt line.  As per datasheet:
280*4882a593Smuzhiyun 	 * When INT_MSK_LINE [i] is set to 1, the associated interrupt
281*4882a593Smuzhiyun 	 * number i is INT line masked, which means that no interrupt is
282*4882a593Smuzhiyun 	 * generated on the INT line.
283*4882a593Smuzhiyun 	 * When INT_MSK_LINE [i] is set to 0, the associated interrupt
284*4882a593Smuzhiyun 	 * number i is  line enabled: An interrupt is generated on the
285*4882a593Smuzhiyun 	 * INT line.
286*4882a593Smuzhiyun 	 * In any case, the INT_STS [i] status bit may or may not be updated,
287*4882a593Smuzhiyun 	 * only linked to the INT_MSK_STS [i] configuration register bit.
288*4882a593Smuzhiyun 	 *
289*4882a593Smuzhiyun 	 * When INT_MSK_STS [i] is set to 1, the associated interrupt number
290*4882a593Smuzhiyun 	 * i is status masked, which means that no interrupt is stored in
291*4882a593Smuzhiyun 	 * the INT_STS[i] status bit. Note that no interrupt number i is
292*4882a593Smuzhiyun 	 * generated on the INT line, even if the INT_MSK_LINE [i] register
293*4882a593Smuzhiyun 	 * bit is set to 0.
294*4882a593Smuzhiyun 	 * When INT_MSK_STS [i] is set to 0, the associated interrupt number i
295*4882a593Smuzhiyun 	 * is status enabled: An interrupt status is updated in the INT_STS [i]
296*4882a593Smuzhiyun 	 * register. The interrupt may or may not be generated on the INT line,
297*4882a593Smuzhiyun 	 * depending on the INT_MSK_LINE [i] configuration register bit.
298*4882a593Smuzhiyun 	 */
299*4882a593Smuzhiyun 	for (i = 0; i < 3; i++)
300*4882a593Smuzhiyun 		tps80031_write(dev, TPS80031_SLAVE_ID2,
301*4882a593Smuzhiyun 				TPS80031_INT_MSK_STS_A + i, 0x00);
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	ret = regmap_add_irq_chip(tps80031->regmap[TPS80031_SLAVE_ID2], irq,
304*4882a593Smuzhiyun 			IRQF_ONESHOT, irq_base,
305*4882a593Smuzhiyun 			&tps80031_irq_chip, &tps80031->irq_data);
306*4882a593Smuzhiyun 	if (ret < 0) {
307*4882a593Smuzhiyun 		dev_err(dev, "add irq failed, err = %d\n", ret);
308*4882a593Smuzhiyun 		return ret;
309*4882a593Smuzhiyun 	}
310*4882a593Smuzhiyun 	return ret;
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
rd_wr_reg_id0(struct device * dev,unsigned int reg)313*4882a593Smuzhiyun static bool rd_wr_reg_id0(struct device *dev, unsigned int reg)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun 	switch (reg) {
316*4882a593Smuzhiyun 	case TPS80031_SMPS1_CFG_FORCE ... TPS80031_SMPS2_CFG_VOLTAGE:
317*4882a593Smuzhiyun 		return true;
318*4882a593Smuzhiyun 	default:
319*4882a593Smuzhiyun 		return false;
320*4882a593Smuzhiyun 	}
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun 
rd_wr_reg_id1(struct device * dev,unsigned int reg)323*4882a593Smuzhiyun static bool rd_wr_reg_id1(struct device *dev, unsigned int reg)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun 	switch (reg) {
326*4882a593Smuzhiyun 	case TPS80031_SECONDS_REG ... TPS80031_RTC_RESET_STATUS_REG:
327*4882a593Smuzhiyun 	case TPS80031_VALIDITY0 ... TPS80031_VALIDITY7:
328*4882a593Smuzhiyun 	case TPS80031_PHOENIX_START_CONDITION ... TPS80031_KEY_PRESS_DUR_CFG:
329*4882a593Smuzhiyun 	case TPS80031_SMPS4_CFG_TRANS ... TPS80031_SMPS3_CFG_VOLTAGE:
330*4882a593Smuzhiyun 	case TPS80031_BROADCAST_ADDR_ALL ... TPS80031_BROADCAST_ADDR_CLK_RST:
331*4882a593Smuzhiyun 	case TPS80031_VANA_CFG_TRANS ... TPS80031_LDO7_CFG_VOLTAGE:
332*4882a593Smuzhiyun 	case TPS80031_REGEN1_CFG_TRANS ... TPS80031_TMP_CFG_STATE:
333*4882a593Smuzhiyun 	case TPS80031_PREQ1_RES_ASS_A ... TPS80031_PREQ3_RES_ASS_C:
334*4882a593Smuzhiyun 	case TPS80031_SMPS_OFFSET ... TPS80031_BATDEBOUNCING:
335*4882a593Smuzhiyun 	case TPS80031_CFG_INPUT_PUPD1 ... TPS80031_CFG_SMPS_PD:
336*4882a593Smuzhiyun 	case TPS80031_BACKUP_REG:
337*4882a593Smuzhiyun 		return true;
338*4882a593Smuzhiyun 	default:
339*4882a593Smuzhiyun 		return false;
340*4882a593Smuzhiyun 	}
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun 
is_volatile_reg_id1(struct device * dev,unsigned int reg)343*4882a593Smuzhiyun static bool is_volatile_reg_id1(struct device *dev, unsigned int reg)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun 	switch (reg) {
346*4882a593Smuzhiyun 	case TPS80031_SMPS4_CFG_TRANS ... TPS80031_SMPS3_CFG_VOLTAGE:
347*4882a593Smuzhiyun 	case TPS80031_VANA_CFG_TRANS ... TPS80031_LDO7_CFG_VOLTAGE:
348*4882a593Smuzhiyun 	case TPS80031_REGEN1_CFG_TRANS ... TPS80031_TMP_CFG_STATE:
349*4882a593Smuzhiyun 	case TPS80031_PREQ1_RES_ASS_A ... TPS80031_PREQ3_RES_ASS_C:
350*4882a593Smuzhiyun 	case TPS80031_SMPS_OFFSET ... TPS80031_BATDEBOUNCING:
351*4882a593Smuzhiyun 	case TPS80031_CFG_INPUT_PUPD1 ... TPS80031_CFG_SMPS_PD:
352*4882a593Smuzhiyun 		return true;
353*4882a593Smuzhiyun 	default:
354*4882a593Smuzhiyun 		return false;
355*4882a593Smuzhiyun 	}
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
rd_wr_reg_id2(struct device * dev,unsigned int reg)358*4882a593Smuzhiyun static bool rd_wr_reg_id2(struct device *dev, unsigned int reg)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun 	switch (reg) {
361*4882a593Smuzhiyun 	case TPS80031_USB_VENDOR_ID_LSB ... TPS80031_USB_OTG_REVISION:
362*4882a593Smuzhiyun 	case TPS80031_GPADC_CTRL ... TPS80031_CTRL_P1:
363*4882a593Smuzhiyun 	case TPS80031_RTCH0_LSB ... TPS80031_GPCH0_MSB:
364*4882a593Smuzhiyun 	case TPS80031_TOGGLE1 ... TPS80031_VIBMODE:
365*4882a593Smuzhiyun 	case TPS80031_PWM1ON ... TPS80031_PWM2OFF:
366*4882a593Smuzhiyun 	case TPS80031_FG_REG_00 ... TPS80031_FG_REG_11:
367*4882a593Smuzhiyun 	case TPS80031_INT_STS_A ... TPS80031_INT_MSK_STS_C:
368*4882a593Smuzhiyun 	case TPS80031_CONTROLLER_CTRL2 ... TPS80031_LED_PWM_CTRL2:
369*4882a593Smuzhiyun 		return true;
370*4882a593Smuzhiyun 	default:
371*4882a593Smuzhiyun 		return false;
372*4882a593Smuzhiyun 	}
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun 
rd_wr_reg_id3(struct device * dev,unsigned int reg)375*4882a593Smuzhiyun static bool rd_wr_reg_id3(struct device *dev, unsigned int reg)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun 	switch (reg) {
378*4882a593Smuzhiyun 	case TPS80031_GPADC_TRIM0 ... TPS80031_GPADC_TRIM18:
379*4882a593Smuzhiyun 		return true;
380*4882a593Smuzhiyun 	default:
381*4882a593Smuzhiyun 		return false;
382*4882a593Smuzhiyun 	}
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun static const struct regmap_config tps80031_regmap_configs[] = {
386*4882a593Smuzhiyun 	{
387*4882a593Smuzhiyun 		.reg_bits = 8,
388*4882a593Smuzhiyun 		.val_bits = 8,
389*4882a593Smuzhiyun 		.writeable_reg = rd_wr_reg_id0,
390*4882a593Smuzhiyun 		.readable_reg = rd_wr_reg_id0,
391*4882a593Smuzhiyun 		.max_register = TPS80031_MAX_REGISTER,
392*4882a593Smuzhiyun 	},
393*4882a593Smuzhiyun 	{
394*4882a593Smuzhiyun 		.reg_bits = 8,
395*4882a593Smuzhiyun 		.val_bits = 8,
396*4882a593Smuzhiyun 		.writeable_reg = rd_wr_reg_id1,
397*4882a593Smuzhiyun 		.readable_reg = rd_wr_reg_id1,
398*4882a593Smuzhiyun 		.volatile_reg = is_volatile_reg_id1,
399*4882a593Smuzhiyun 		.max_register = TPS80031_MAX_REGISTER,
400*4882a593Smuzhiyun 	},
401*4882a593Smuzhiyun 	{
402*4882a593Smuzhiyun 		.reg_bits = 8,
403*4882a593Smuzhiyun 		.val_bits = 8,
404*4882a593Smuzhiyun 		.writeable_reg = rd_wr_reg_id2,
405*4882a593Smuzhiyun 		.readable_reg = rd_wr_reg_id2,
406*4882a593Smuzhiyun 		.max_register = TPS80031_MAX_REGISTER,
407*4882a593Smuzhiyun 	},
408*4882a593Smuzhiyun 	{
409*4882a593Smuzhiyun 		.reg_bits = 8,
410*4882a593Smuzhiyun 		.val_bits = 8,
411*4882a593Smuzhiyun 		.writeable_reg = rd_wr_reg_id3,
412*4882a593Smuzhiyun 		.readable_reg = rd_wr_reg_id3,
413*4882a593Smuzhiyun 		.max_register = TPS80031_MAX_REGISTER,
414*4882a593Smuzhiyun 	},
415*4882a593Smuzhiyun };
416*4882a593Smuzhiyun 
tps80031_probe(struct i2c_client * client,const struct i2c_device_id * id)417*4882a593Smuzhiyun static int tps80031_probe(struct i2c_client *client,
418*4882a593Smuzhiyun 			  const struct i2c_device_id *id)
419*4882a593Smuzhiyun {
420*4882a593Smuzhiyun 	struct tps80031_platform_data *pdata = dev_get_platdata(&client->dev);
421*4882a593Smuzhiyun 	struct tps80031 *tps80031;
422*4882a593Smuzhiyun 	int ret;
423*4882a593Smuzhiyun 	uint8_t es_version;
424*4882a593Smuzhiyun 	uint8_t ep_ver;
425*4882a593Smuzhiyun 	int i;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	if (!pdata) {
428*4882a593Smuzhiyun 		dev_err(&client->dev, "tps80031 requires platform data\n");
429*4882a593Smuzhiyun 		return -EINVAL;
430*4882a593Smuzhiyun 	}
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	tps80031 = devm_kzalloc(&client->dev, sizeof(*tps80031), GFP_KERNEL);
433*4882a593Smuzhiyun 	if (!tps80031)
434*4882a593Smuzhiyun 		return -ENOMEM;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	for (i = 0; i < TPS80031_NUM_SLAVES; i++) {
437*4882a593Smuzhiyun 		if (tps80031_slave_address[i] == client->addr)
438*4882a593Smuzhiyun 			tps80031->clients[i] = client;
439*4882a593Smuzhiyun 		else
440*4882a593Smuzhiyun 			tps80031->clients[i] = devm_i2c_new_dummy_device(&client->dev,
441*4882a593Smuzhiyun 						client->adapter, tps80031_slave_address[i]);
442*4882a593Smuzhiyun 		if (IS_ERR(tps80031->clients[i])) {
443*4882a593Smuzhiyun 			dev_err(&client->dev, "can't attach client %d\n", i);
444*4882a593Smuzhiyun 			return PTR_ERR(tps80031->clients[i]);
445*4882a593Smuzhiyun 		}
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 		i2c_set_clientdata(tps80031->clients[i], tps80031);
448*4882a593Smuzhiyun 		tps80031->regmap[i] = devm_regmap_init_i2c(tps80031->clients[i],
449*4882a593Smuzhiyun 					&tps80031_regmap_configs[i]);
450*4882a593Smuzhiyun 		if (IS_ERR(tps80031->regmap[i])) {
451*4882a593Smuzhiyun 			ret = PTR_ERR(tps80031->regmap[i]);
452*4882a593Smuzhiyun 			dev_err(&client->dev,
453*4882a593Smuzhiyun 				"regmap %d init failed, err %d\n", i, ret);
454*4882a593Smuzhiyun 			return ret;
455*4882a593Smuzhiyun 		}
456*4882a593Smuzhiyun 	}
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	ret = tps80031_read(&client->dev, TPS80031_SLAVE_ID3,
459*4882a593Smuzhiyun 			TPS80031_JTAGVERNUM, &es_version);
460*4882a593Smuzhiyun 	if (ret < 0) {
461*4882a593Smuzhiyun 		dev_err(&client->dev,
462*4882a593Smuzhiyun 			"Silicon version number read failed: %d\n", ret);
463*4882a593Smuzhiyun 		return ret;
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	ret = tps80031_read(&client->dev, TPS80031_SLAVE_ID3,
467*4882a593Smuzhiyun 			TPS80031_EPROM_REV, &ep_ver);
468*4882a593Smuzhiyun 	if (ret < 0) {
469*4882a593Smuzhiyun 		dev_err(&client->dev,
470*4882a593Smuzhiyun 			"Silicon eeprom version read failed: %d\n", ret);
471*4882a593Smuzhiyun 		return ret;
472*4882a593Smuzhiyun 	}
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	dev_info(&client->dev, "ES version 0x%02x and EPROM version 0x%02x\n",
475*4882a593Smuzhiyun 					es_version, ep_ver);
476*4882a593Smuzhiyun 	tps80031->es_version = es_version;
477*4882a593Smuzhiyun 	tps80031->dev = &client->dev;
478*4882a593Smuzhiyun 	i2c_set_clientdata(client, tps80031);
479*4882a593Smuzhiyun 	tps80031->chip_info = id->driver_data;
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	ret = tps80031_irq_init(tps80031, client->irq, pdata->irq_base);
482*4882a593Smuzhiyun 	if (ret) {
483*4882a593Smuzhiyun 		dev_err(&client->dev, "IRQ init failed: %d\n", ret);
484*4882a593Smuzhiyun 		return ret;
485*4882a593Smuzhiyun 	}
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	tps80031_pupd_init(tps80031, pdata);
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	tps80031_init_ext_control(tps80031, pdata);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	ret = mfd_add_devices(tps80031->dev, -1,
492*4882a593Smuzhiyun 			tps80031_cell, ARRAY_SIZE(tps80031_cell),
493*4882a593Smuzhiyun 			NULL, 0,
494*4882a593Smuzhiyun 			regmap_irq_get_domain(tps80031->irq_data));
495*4882a593Smuzhiyun 	if (ret < 0) {
496*4882a593Smuzhiyun 		dev_err(&client->dev, "mfd_add_devices failed: %d\n", ret);
497*4882a593Smuzhiyun 		goto fail_mfd_add;
498*4882a593Smuzhiyun 	}
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	if (pdata->use_power_off && !pm_power_off) {
501*4882a593Smuzhiyun 		tps80031_power_off_dev = tps80031;
502*4882a593Smuzhiyun 		pm_power_off = tps80031_power_off;
503*4882a593Smuzhiyun 	}
504*4882a593Smuzhiyun 	return 0;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun fail_mfd_add:
507*4882a593Smuzhiyun 	regmap_del_irq_chip(client->irq, tps80031->irq_data);
508*4882a593Smuzhiyun 	return ret;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun static const struct i2c_device_id tps80031_id_table[] = {
512*4882a593Smuzhiyun 	{ "tps80031", TPS80031 },
513*4882a593Smuzhiyun 	{ "tps80032", TPS80032 },
514*4882a593Smuzhiyun 	{ }
515*4882a593Smuzhiyun };
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun static struct i2c_driver tps80031_driver = {
518*4882a593Smuzhiyun 	.driver	= {
519*4882a593Smuzhiyun 		.name			= "tps80031",
520*4882a593Smuzhiyun 		.suppress_bind_attrs	= true,
521*4882a593Smuzhiyun 	},
522*4882a593Smuzhiyun 	.probe		= tps80031_probe,
523*4882a593Smuzhiyun 	.id_table	= tps80031_id_table,
524*4882a593Smuzhiyun };
525*4882a593Smuzhiyun 
tps80031_init(void)526*4882a593Smuzhiyun static int __init tps80031_init(void)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun 	return i2c_add_driver(&tps80031_driver);
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun subsys_initcall(tps80031_init);
531