xref: /OK3568_Linux_fs/kernel/drivers/power/supply/sc89890_charger.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Chrager driver for Sc89890
4  *
5  * Copyright (c) 2022 Rockchip Electronics Co., Ltd.
6  *
7  * Author: Xu Shengfei <xsf@rock-chips.com>
8  */
9 #include <linux/gpio/consumer.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/power_supply.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/types.h>
18 
19 /* Module parameters. */
20 static int debug;
21 module_param_named(debug, debug, int, 0644);
22 MODULE_PARM_DESC(debug, "Set to one to enable debugging messages.");
23 
24 #define DBG(args...) \
25 	do { \
26 		if (debug) { \
27 			pr_info(args); \
28 		} \
29 	} while (0)
30 
31 #define SC89890_MANUFACTURER		"SOUTHCHIP"
32 #define SC89890_IRQ			"sc89890_irq"
33 #define SC89890_ID			4
34 #define SC89890_DEBUG_BUF_LEN		30
35 enum sc89890_fields {
36 	F_EN_HIZ, F_EN_ILIM, F_IILIM,				     /* Reg00 */
37 	F_BHOT, F_BCOLD, F_VINDPM_OFS,				     /* Reg01 */
38 	F_CONV_START, F_CONV_RATE, F_BOOSTF, F_ICO_EN,
39 	F_HVDCP_EN, F_MAXC_EN, F_FORCE_DPM, F_AUTO_DPDM_EN,	     /* Reg02 */
40 	F_BAT_LOAD_EN, F_WD_RST, F_OTG_CFG, F_CHG_CFG, F_SYSVMIN,
41 	F_MIN_VBAT_SEL,						     /* Reg03 */
42 	F_PUMPX_EN, F_ICHG,					     /* Reg04 */
43 	F_IPRECHG, F_ITERM,					     /* Reg05 */
44 	F_VREG, F_BATLOWV, F_VRECHG,				     /* Reg06 */
45 	F_TERM_EN, F_STAT_DIS, F_WD, F_TMR_EN, F_CHG_TMR,
46 	F_JEITA_ISET,						     /* Reg07 */
47 	F_BATCMP, F_VCLAMP, F_TREG,				     /* Reg08 */
48 	F_FORCE_ICO, F_TMR2X_EN, F_BATFET_DIS, F_JEITA_VSET,
49 	F_BATFET_DLY, F_BATFET_RST_EN, F_PUMPX_UP, F_PUMPX_DN,	     /* Reg09 */
50 	F_BOOSTV, F_PFM_OTG_DIS, F_BOOSTI,			     /* Reg0A */
51 	F_VBUS_STAT, F_CHG_STAT, F_PG_STAT, F_SDP_STAT, F_0B_RSVD,
52 	F_VSYS_STAT,						     /* Reg0B */
53 	F_WD_FAULT, F_BOOST_FAULT, F_CHG_FAULT, F_BAT_FAULT,
54 	F_NTC_FAULT,						     /* Reg0C */
55 	F_FORCE_VINDPM, F_VINDPM,				     /* Reg0D */
56 	F_THERM_STAT, F_BATV,					     /* Reg0E */
57 	F_SYSV,							     /* Reg0F */
58 	F_TSPCT,						     /* Reg10 */
59 	F_VBUS_GD, F_VBUSV,					     /* Reg11 */
60 	F_ICHGR,						     /* Reg12 */
61 	F_VDPM_STAT, F_IDPM_STAT, F_IDPM_LIM,			     /* Reg13 */
62 	F_REG_RST, F_ICO_OPTIMIZED, F_PN, F_TS_PROFILE, F_DEV_REV,   /* Reg14 */
63 
64 	F_MAX_FIELDS
65 };
66 
67 /* initial field values, converted to register values */
68 struct sc89890_init_data {
69 	u8 ichg;	/* charge current		*/
70 	u8 vreg;	/* regulation voltage		*/
71 	u8 iterm;	/* termination current		*/
72 	u8 iprechg;	/* precharge current		*/
73 	u8 sysvmin;	/* minimum system voltage limit */
74 	u8 boostv;	/* boost regulation voltage	*/
75 	u8 boosti;	/* boost current limit		*/
76 	u8 boostf;	/* boost frequency		*/
77 	u8 ilim_en;	/* enable ILIM pin		*/
78 	u8 treg;	/* thermal regulation threshold */
79 	u8 rbatcomp;	/* IBAT sense resistor value    */
80 	u8 vclamp;	/* IBAT compensation voltage limit */
81 };
82 
83 struct sc89890_state {
84 	u8 online;
85 	u8 chrg_status;
86 	u8 chrg_fault;
87 	u8 vsys_status;
88 	u8 boost_fault;
89 	u8 bat_fault;
90 };
91 
92 struct sc89890_device {
93 	struct i2c_client *client;
94 	struct device *dev;
95 	struct power_supply *charger;
96 
97 	struct regulator_dev *otg_vbus_reg;
98 	unsigned long usb_event;
99 
100 	struct regmap *rmap;
101 	struct regmap_field *rmap_fields[F_MAX_FIELDS];
102 
103 	struct sc89890_init_data init_data;
104 	struct sc89890_state state;
105 
106 	struct mutex lock; /* protect state data */
107 };
108 
109 static const struct regmap_range sc89890_readonly_reg_ranges[] = {
110 	regmap_reg_range(0x0b, 0x0c),
111 	regmap_reg_range(0x0e, 0x13),
112 };
113 
114 static const struct regmap_access_table sc89890_writeable_regs = {
115 	.no_ranges = sc89890_readonly_reg_ranges,
116 	.n_no_ranges = ARRAY_SIZE(sc89890_readonly_reg_ranges),
117 };
118 
119 static const struct regmap_range sc89890_volatile_reg_ranges[] = {
120 	regmap_reg_range(0x00, 0x00),
121 	regmap_reg_range(0x02, 0x02),
122 	regmap_reg_range(0x09, 0x09),
123 	regmap_reg_range(0x0b, 0x0b),
124 	regmap_reg_range(0x0c, 0x0c),
125 	regmap_reg_range(0x0d, 0x14),
126 };
127 
128 static const struct regmap_access_table sc89890_volatile_regs = {
129 	.yes_ranges = sc89890_volatile_reg_ranges,
130 	.n_yes_ranges = ARRAY_SIZE(sc89890_volatile_reg_ranges),
131 };
132 
133 static const struct regmap_config sc89890_regmap_config = {
134 	.reg_bits = 8,
135 	.val_bits = 8,
136 
137 	.max_register = 0x14,
138 	.cache_type = REGCACHE_RBTREE,
139 
140 	.wr_table = &sc89890_writeable_regs,
141 	.volatile_table = &sc89890_volatile_regs,
142 };
143 
144 static const struct reg_field sc89890_reg_fields[] = {
145 	/* REG00 */
146 	[F_EN_HIZ]		= REG_FIELD(0x00, 7, 7),
147 	[F_EN_ILIM]		= REG_FIELD(0x00, 6, 6),
148 	[F_IILIM]		= REG_FIELD(0x00, 0, 5),
149 	/* REG01 */
150 	[F_BHOT]		= REG_FIELD(0x01, 6, 7),
151 	[F_BCOLD]		= REG_FIELD(0x01, 5, 5),
152 	[F_VINDPM_OFS]		= REG_FIELD(0x01, 0, 4),
153 	/* REG02 */
154 	[F_CONV_START]		= REG_FIELD(0x02, 7, 7),
155 	[F_CONV_RATE]		= REG_FIELD(0x02, 6, 6),
156 	[F_BOOSTF]		= REG_FIELD(0x02, 5, 5),
157 	[F_ICO_EN]		= REG_FIELD(0x02, 4, 4),
158 	[F_HVDCP_EN]		= REG_FIELD(0x02, 3, 3),
159 	[F_MAXC_EN]		= REG_FIELD(0x02, 2, 2),
160 	[F_FORCE_DPM]		= REG_FIELD(0x02, 1, 1),
161 	[F_AUTO_DPDM_EN]	= REG_FIELD(0x02, 0, 0),
162 	/* REG03 */
163 	[F_BAT_LOAD_EN]		= REG_FIELD(0x03, 7, 7),
164 	[F_WD_RST]		= REG_FIELD(0x03, 6, 6),
165 	[F_OTG_CFG]		= REG_FIELD(0x03, 5, 5),
166 	[F_CHG_CFG]		= REG_FIELD(0x03, 4, 4),
167 	[F_SYSVMIN]		= REG_FIELD(0x03, 1, 3),
168 	[F_MIN_VBAT_SEL]	= REG_FIELD(0x03, 0, 0),
169 	/* REG04 */
170 	[F_PUMPX_EN]		= REG_FIELD(0x04, 7, 7),
171 	[F_ICHG]		= REG_FIELD(0x04, 0, 6),
172 	/* REG05 */
173 	[F_IPRECHG]		= REG_FIELD(0x05, 4, 7),
174 	[F_ITERM]		= REG_FIELD(0x05, 0, 3),
175 	/* REG06 */
176 	[F_VREG]		= REG_FIELD(0x06, 2, 7),
177 	[F_BATLOWV]		= REG_FIELD(0x06, 1, 1),
178 	[F_VRECHG]		= REG_FIELD(0x06, 0, 0),
179 	/* REG07 */
180 	[F_TERM_EN]		= REG_FIELD(0x07, 7, 7),
181 	[F_STAT_DIS]		= REG_FIELD(0x07, 6, 6),
182 	[F_WD]			= REG_FIELD(0x07, 4, 5),
183 	[F_TMR_EN]		= REG_FIELD(0x07, 3, 3),
184 	[F_CHG_TMR]		= REG_FIELD(0x07, 1, 2),
185 	[F_JEITA_ISET]		= REG_FIELD(0x07, 0, 0),
186 	/* REG08 */
187 	[F_BATCMP]		= REG_FIELD(0x08, 5, 7),
188 	[F_VCLAMP]		= REG_FIELD(0x08, 2, 4),
189 	[F_TREG]		= REG_FIELD(0x08, 0, 1),
190 	/* REG09 */
191 	[F_FORCE_ICO]		= REG_FIELD(0x09, 7, 7),
192 	[F_TMR2X_EN]		= REG_FIELD(0x09, 6, 6),
193 	[F_BATFET_DIS]		= REG_FIELD(0x09, 5, 5),
194 	[F_JEITA_VSET]		= REG_FIELD(0x09, 4, 4),
195 	[F_BATFET_DLY]		= REG_FIELD(0x09, 3, 3),
196 	[F_BATFET_RST_EN]	= REG_FIELD(0x09, 2, 2),
197 	[F_PUMPX_UP]		= REG_FIELD(0x09, 1, 1),
198 	[F_PUMPX_DN]		= REG_FIELD(0x09, 0, 0),
199 	/* REG0A */
200 	[F_BOOSTV]		= REG_FIELD(0x0A, 4, 7),
201 	[F_BOOSTI]		= REG_FIELD(0x0A, 0, 2),
202 	[F_PFM_OTG_DIS]		= REG_FIELD(0x0A, 3, 3),
203 	/* REG0B */
204 	[F_VBUS_STAT]		= REG_FIELD(0x0B, 5, 7),
205 	[F_CHG_STAT]		= REG_FIELD(0x0B, 3, 4),
206 	[F_PG_STAT]		= REG_FIELD(0x0B, 2, 2),
207 	[F_SDP_STAT]		= REG_FIELD(0x0B, 1, 1),
208 	[F_VSYS_STAT]		= REG_FIELD(0x0B, 0, 0),
209 	/* REG0C */
210 	[F_WD_FAULT]		= REG_FIELD(0x0C, 7, 7),
211 	[F_BOOST_FAULT]		= REG_FIELD(0x0C, 6, 6),
212 	[F_CHG_FAULT]		= REG_FIELD(0x0C, 4, 5),
213 	[F_BAT_FAULT]		= REG_FIELD(0x0C, 3, 3),
214 	[F_NTC_FAULT]		= REG_FIELD(0x0C, 0, 2),
215 	/* REG0D */
216 	[F_FORCE_VINDPM]	= REG_FIELD(0x0D, 7, 7),
217 	[F_VINDPM]		= REG_FIELD(0x0D, 0, 6),
218 	/* REG0E */
219 	[F_THERM_STAT]		= REG_FIELD(0x0E, 7, 7),
220 	[F_BATV]		= REG_FIELD(0x0E, 0, 6),
221 	/* REG0F */
222 	[F_SYSV]		= REG_FIELD(0x0F, 0, 6),
223 	/* REG10 */
224 	[F_TSPCT]		= REG_FIELD(0x10, 0, 6),
225 	/* REG11 */
226 	[F_VBUS_GD]		= REG_FIELD(0x11, 7, 7),
227 	[F_VBUSV]		= REG_FIELD(0x11, 0, 6),
228 	/* REG12 */
229 	[F_ICHGR]		= REG_FIELD(0x12, 0, 6),
230 	/* REG13 */
231 	[F_VDPM_STAT]		= REG_FIELD(0x13, 7, 7),
232 	[F_IDPM_STAT]		= REG_FIELD(0x13, 6, 6),
233 	[F_IDPM_LIM]		= REG_FIELD(0x13, 0, 5),
234 	/* REG14 */
235 	[F_REG_RST]		= REG_FIELD(0x14, 7, 7),
236 	[F_ICO_OPTIMIZED]	= REG_FIELD(0x14, 6, 6),
237 	[F_PN]			= REG_FIELD(0x14, 3, 5),
238 	[F_TS_PROFILE]		= REG_FIELD(0x14, 2, 2),
239 	[F_DEV_REV]		= REG_FIELD(0x14, 0, 1)
240 };
241 
242 enum sc89890_status {
243 	STATUS_NOT_CHARGING,
244 	STATUS_PRE_CHARGING,
245 	STATUS_FAST_CHARGING,
246 	STATUS_TERMINATION_DONE,
247 };
248 
249 enum sc89890_chrg_fault {
250 	CHRG_FAULT_NORMAL,
251 	CHRG_FAULT_INPUT,
252 	CHRG_FAULT_THERMAL_SHUTDOWN,
253 	CHRG_FAULT_TIMER_EXPIRED,
254 };
255 
256 /*
257  * Most of the val -> idx conversions can be computed, given the minimum,
258  * maximum and the step between values. For the rest of conversions, we use
259  * lookup tables.
260  */
261 enum sc89890_table_ids {
262 	/* range tables */
263 	TBL_ICHG,
264 	TBL_ITERM,
265 	TBL_IILIM,
266 	TBL_VREG,
267 	TBL_BOOSTV,
268 	TBL_SYSVMIN,
269 	TBL_VBATCOMP,
270 	TBL_RBATCOMP,
271 
272 	/* lookup tables */
273 	TBL_TREG,
274 	TBL_BOOSTI,
275 };
276 
277 /* Thermal Regulation Threshold lookup table, in degrees Celsius */
278 static const u32 sc89890_treg_tbl[] = { 60, 80, 100, 120 };
279 
280 #define SC89890_TREG_TBL_SIZE		ARRAY_SIZE(sc89890_treg_tbl)
281 
282 /* Boost mode current limit lookup table, in uA */
283 static const u32 sc89890_boosti_tbl[] = {
284 	500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
285 };
286 
287 #define SC89890_BOOSTI_TBL_SIZE		ARRAY_SIZE(sc89890_boosti_tbl)
288 
289 struct sc89890_range {
290 	u32 min;
291 	u32 max;
292 	u32 step;
293 };
294 
295 struct sc89890_lookup {
296 	const u32 *tbl;
297 	u32 size;
298 };
299 
300 static const union {
301 	struct sc89890_range rt;
302 	struct sc89890_lookup lt;
303 } sc89890_tables[] = {
304 	/* range tables */
305 	[TBL_ICHG] = { .rt = {0, 5056000, 64000} }, /* uA */
306 	[TBL_ITERM] = { .rt = {64000, 1024000, 64000} }, /* uA */
307 	[TBL_IILIM] = { .rt = {100000, 3250000, 50000} }, /* uA */
308 	[TBL_VREG] = { .rt = {3840000, 4608000, 16000} }, /* uV */
309 	[TBL_BOOSTV] = { .rt = {4550000, 5510000, 64000} }, /* uV */
310 	[TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} }, /* uV */
311 	[TBL_VBATCOMP] = { .rt = {0, 224000, 32000} }, /* uV */
312 	[TBL_RBATCOMP] = { .rt = {0, 140000, 20000} }, /* uOhm */
313 
314 	/* lookup tables */
315 	[TBL_TREG] = { .lt = {sc89890_treg_tbl, SC89890_TREG_TBL_SIZE} },
316 	[TBL_BOOSTI] = { .lt = {sc89890_boosti_tbl, SC89890_BOOSTI_TBL_SIZE} }
317 };
318 
sc89890_field_read(struct sc89890_device * sc89890,enum sc89890_fields field_id)319 static int sc89890_field_read(struct sc89890_device *sc89890,
320 			      enum sc89890_fields field_id)
321 {
322 	int ret;
323 	int val;
324 
325 	ret = regmap_field_read(sc89890->rmap_fields[field_id], &val);
326 	if (ret < 0)
327 		return ret;
328 
329 	return val;
330 }
331 
sc89890_field_write(struct sc89890_device * sc89890,enum sc89890_fields field_id,u8 val)332 static int sc89890_field_write(struct sc89890_device *sc89890,
333 			       enum sc89890_fields field_id, u8 val)
334 {
335 	return regmap_field_write(sc89890->rmap_fields[field_id], val);
336 }
337 
sc89890_find_idx(u32 value,enum sc89890_table_ids id)338 static u8 sc89890_find_idx(u32 value, enum sc89890_table_ids id)
339 {
340 	u8 idx;
341 
342 	if (id >= TBL_TREG) {
343 		const u32 *tbl = sc89890_tables[id].lt.tbl;
344 		u32 tbl_size = sc89890_tables[id].lt.size;
345 
346 		for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
347 			;
348 	} else {
349 		const struct sc89890_range *rtbl = &sc89890_tables[id].rt;
350 		u8 rtbl_size;
351 
352 		rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
353 
354 		for (idx = 1;
355 		     idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
356 		     idx++)
357 			;
358 	}
359 
360 	return idx - 1;
361 }
362 
sc89890_find_val(u8 idx,enum sc89890_table_ids id)363 static u32 sc89890_find_val(u8 idx, enum sc89890_table_ids id)
364 {
365 	const struct sc89890_range *rtbl;
366 
367 	/* lookup table? */
368 	if (id >= TBL_TREG)
369 		return sc89890_tables[id].lt.tbl[idx];
370 
371 	/* range table */
372 	rtbl = &sc89890_tables[id].rt;
373 
374 	return (rtbl->min + idx * rtbl->step);
375 }
376 
sc89890_is_adc_property(enum power_supply_property psp)377 static bool sc89890_is_adc_property(enum power_supply_property psp)
378 {
379 	switch (psp) {
380 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
381 	case POWER_SUPPLY_PROP_CURRENT_NOW:
382 		return true;
383 
384 	default:
385 		return false;
386 	}
387 }
388 
sc89890_get_chip_state(struct sc89890_device * sc89890,struct sc89890_state * state)389 static int sc89890_get_chip_state(struct sc89890_device *sc89890,
390 				  struct sc89890_state *state)
391 {
392 	int i, ret;
393 
394 	struct {
395 		enum sc89890_fields id;
396 		u8 *data;
397 	} state_fields[] = {
398 		{F_CHG_STAT, &state->chrg_status},
399 		{F_PG_STAT, &state->online},
400 		{F_VSYS_STAT, &state->vsys_status},
401 		{F_BOOST_FAULT, &state->boost_fault},
402 		{F_BAT_FAULT, &state->bat_fault},
403 		{F_CHG_FAULT, &state->chrg_fault}
404 	};
405 
406 	for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
407 		ret = sc89890_field_read(sc89890, state_fields[i].id);
408 		if (ret < 0)
409 			return ret;
410 
411 		*state_fields[i].data = ret;
412 	}
413 
414 	DBG("SC89890: S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT=%d/%d/%d\n",
415 	    state->chrg_status, state->online, state->vsys_status,
416 	    state->chrg_fault, state->boost_fault, state->bat_fault);
417 
418 	return 0;
419 }
420 
__sc89890_handle_irq(struct sc89890_device * sc89890)421 static irqreturn_t __sc89890_handle_irq(struct sc89890_device *sc89890)
422 {
423 	struct sc89890_state new_state;
424 	int ret;
425 
426 	ret = sc89890_get_chip_state(sc89890, &new_state);
427 	if (ret < 0)
428 		return IRQ_NONE;
429 
430 	if (!memcmp(&sc89890->state, &new_state, sizeof(new_state)))
431 		return IRQ_NONE;
432 
433 	if (!new_state.online && sc89890->state.online) {	/* power removed */
434 		/* disable ADC */
435 		ret = sc89890_field_write(sc89890, F_CONV_START, 0);
436 		if (ret < 0)
437 			goto error;
438 	} else if (new_state.online && !sc89890->state.online) { /* power inserted */
439 
440 		/* enable ADC, to have control of charge current/voltage */
441 		ret = sc89890_field_write(sc89890, F_CONV_START, 1);
442 		if (ret < 0)
443 			goto error;
444 	}
445 
446 	sc89890->state = new_state;
447 	power_supply_changed(sc89890->charger);
448 
449 	return IRQ_HANDLED;
450 error:
451 	dev_err(sc89890->dev, "Error communicating with the chip: %pe\n",
452 		ERR_PTR(ret));
453 	return IRQ_HANDLED;
454 }
455 
sc89890_power_supply_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)456 static int sc89890_power_supply_get_property(struct power_supply *psy,
457 					     enum power_supply_property psp,
458 					     union power_supply_propval *val)
459 {
460 	struct sc89890_device *sc89890 = power_supply_get_drvdata(psy);
461 	struct sc89890_state state;
462 	bool do_adc_conv;
463 	int ret;
464 
465 	mutex_lock(&sc89890->lock);
466 	/* update state in case we lost an interrupt */
467 	__sc89890_handle_irq(sc89890);
468 	state = sc89890->state;
469 	do_adc_conv = !state.online && sc89890_is_adc_property(psp);
470 	if (do_adc_conv)
471 		sc89890_field_write(sc89890, F_CONV_START, 1);
472 	mutex_unlock(&sc89890->lock);
473 
474 	if (do_adc_conv)
475 		regmap_field_read_poll_timeout(sc89890->rmap_fields[F_CONV_START],
476 			ret, !ret, 25000, 1000000);
477 
478 	switch (psp) {
479 	case POWER_SUPPLY_PROP_STATUS:
480 		if (!state.online)
481 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
482 		else if (state.chrg_status == STATUS_NOT_CHARGING)
483 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
484 		else if (state.chrg_status == STATUS_PRE_CHARGING ||
485 			 state.chrg_status == STATUS_FAST_CHARGING)
486 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
487 		else if (state.chrg_status == STATUS_TERMINATION_DONE)
488 			val->intval = POWER_SUPPLY_STATUS_FULL;
489 		else
490 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
491 
492 		break;
493 
494 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
495 		if (!state.online || state.chrg_status == STATUS_NOT_CHARGING ||
496 		    state.chrg_status == STATUS_TERMINATION_DONE)
497 			val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
498 		else if (state.chrg_status == STATUS_PRE_CHARGING)
499 			val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
500 		else if (state.chrg_status == STATUS_FAST_CHARGING)
501 			val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
502 		else /* unreachable */
503 			val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
504 		break;
505 
506 	case POWER_SUPPLY_PROP_MANUFACTURER:
507 		val->strval = SC89890_MANUFACTURER;
508 		break;
509 
510 	case POWER_SUPPLY_PROP_MODEL_NAME:
511 		val->strval = "SC89890";
512 		break;
513 
514 	case POWER_SUPPLY_PROP_ONLINE:
515 		val->intval = !!state.chrg_status;
516 		break;
517 
518 	case POWER_SUPPLY_PROP_HEALTH:
519 		if (!state.chrg_fault && !state.bat_fault && !state.boost_fault)
520 			val->intval = POWER_SUPPLY_HEALTH_GOOD;
521 		else if (state.bat_fault)
522 			val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
523 		else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED)
524 			val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
525 		else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN)
526 			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
527 		else
528 			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
529 		break;
530 
531 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
532 		val->intval = sc89890_find_val(sc89890->init_data.ichg, TBL_ICHG);
533 		break;
534 
535 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
536 		if (!state.online) {
537 			val->intval = 0;
538 			break;
539 		}
540 
541 		ret = sc89890_field_read(sc89890, F_BATV); /* read measured value */
542 		if (ret < 0)
543 			return ret;
544 
545 		/* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
546 		val->intval = 2304000 + ret * 20000;
547 		break;
548 
549 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
550 		val->intval = sc89890_find_val(sc89890->init_data.vreg, TBL_VREG);
551 		break;
552 
553 	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
554 		val->intval = sc89890_find_val(sc89890->init_data.iprechg, TBL_ITERM);
555 		break;
556 
557 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
558 		val->intval = sc89890_find_val(sc89890->init_data.iterm, TBL_ITERM);
559 		break;
560 
561 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
562 		ret = sc89890_field_read(sc89890, F_IILIM);
563 		if (ret < 0)
564 			return ret;
565 
566 		val->intval = sc89890_find_val(ret, TBL_IILIM);
567 		break;
568 	case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
569 		val->intval = 13500000; /* uV */
570 		break;
571 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
572 		ret = sc89890_field_read(sc89890, F_SYSV); /* read measured value */
573 		if (ret < 0)
574 			return ret;
575 
576 		/* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
577 		val->intval = 2304000 + ret * 20000;
578 		break;
579 
580 	case POWER_SUPPLY_PROP_CURRENT_NOW:
581 		ret = sc89890_field_read(sc89890, F_ICHGR); /* read measured value */
582 		if (ret < 0)
583 			return ret;
584 
585 		/* converted_val = ADC_val * 50mA (table 10.3.19) */
586 		val->intval = ret * -50000;
587 		break;
588 
589 	default:
590 		return -EINVAL;
591 	}
592 
593 	return 0;
594 }
595 
sc89890_power_supply_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)596 static int sc89890_power_supply_set_property(struct power_supply *psy,
597 					     enum power_supply_property psp,
598 					     const union power_supply_propval *val)
599 {
600 	struct sc89890_device *sc89890 = power_supply_get_drvdata(psy);
601 	int index, ret;
602 
603 	switch (psp) {
604 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
605 		index = sc89890_find_idx(val->intval, TBL_ICHG);
606 		ret = sc89890_field_write(sc89890, F_ICHG, index);
607 		if (ret < 0)
608 			dev_err(sc89890->dev, "set input voltage limit failed\n");
609 		break;
610 
611 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
612 		index = sc89890_find_idx(val->intval, TBL_IILIM);
613 		ret = sc89890_field_write(sc89890, F_IILIM, index);
614 		if (ret < 0)
615 			dev_err(sc89890->dev, "set input current limit failed\n");
616 		break;
617 
618 	default:
619 		ret = -EINVAL;
620 	}
621 
622 	return ret;
623 }
624 
sc89890_irq_handler_thread(int irq,void * private)625 static irqreturn_t sc89890_irq_handler_thread(int irq, void *private)
626 {
627 	struct sc89890_device *sc89890 = private;
628 	irqreturn_t ret;
629 
630 	mutex_lock(&sc89890->lock);
631 	ret = __sc89890_handle_irq(sc89890);
632 	mutex_unlock(&sc89890->lock);
633 
634 	return ret;
635 }
636 
sc89890_chip_reset(struct sc89890_device * sc89890)637 static int sc89890_chip_reset(struct sc89890_device *sc89890)
638 {
639 	int ret;
640 	int rst_check_counter = 10;
641 
642 	ret = sc89890_field_write(sc89890, F_REG_RST, 1);
643 	if (ret < 0)
644 		return ret;
645 
646 	do {
647 		ret = sc89890_field_read(sc89890, F_REG_RST);
648 		if (ret < 0)
649 			return ret;
650 
651 		usleep_range(5, 10);
652 	} while (ret == 1 && --rst_check_counter);
653 
654 	if (!rst_check_counter)
655 		return -ETIMEDOUT;
656 
657 	return 0;
658 }
659 
sc89890_hw_init(struct sc89890_device * sc89890)660 static int sc89890_hw_init(struct sc89890_device *sc89890)
661 {
662 	int ret;
663 	int i;
664 
665 	const struct {
666 		enum sc89890_fields id;
667 		u32 value;
668 	} init_data[] = {
669 		{F_ICHG, sc89890->init_data.ichg},
670 		{F_VREG, sc89890->init_data.vreg},
671 		{F_ITERM, sc89890->init_data.iterm},
672 		{F_IPRECHG, sc89890->init_data.iprechg},
673 		{F_SYSVMIN, sc89890->init_data.sysvmin},
674 		{F_BOOSTV, sc89890->init_data.boostv},
675 		{F_BOOSTI, sc89890->init_data.boosti},
676 		{F_BOOSTF, sc89890->init_data.boostf},
677 		{F_EN_ILIM, sc89890->init_data.ilim_en},
678 		{F_TREG, sc89890->init_data.treg},
679 		{F_BATCMP, sc89890->init_data.rbatcomp},
680 		{F_VCLAMP, sc89890->init_data.vclamp},
681 	};
682 
683 	ret = sc89890_chip_reset(sc89890);
684 	if (ret < 0) {
685 		dev_dbg(sc89890->dev, "Reset failed %d\n", ret);
686 		return ret;
687 	}
688 
689 	/* disable watchdog */
690 	ret = sc89890_field_write(sc89890, F_WD, 0);
691 	if (ret < 0) {
692 		dev_dbg(sc89890->dev, "Disabling watchdog failed %d\n", ret);
693 		return ret;
694 	}
695 
696 	/* initialize currents/voltages and other parameters */
697 	for (i = 0; i < ARRAY_SIZE(init_data); i++) {
698 		ret = sc89890_field_write(sc89890, init_data[i].id,
699 					  init_data[i].value);
700 		if (ret < 0) {
701 			dev_dbg(sc89890->dev, "Writing init data failed %d\n", ret);
702 			return ret;
703 		}
704 	}
705 
706 	/* Configure ADC for continuous conversions when charging */
707 	ret = sc89890_field_write(sc89890, F_CONV_RATE, !!sc89890->state.online);
708 	if (ret < 0) {
709 		dev_err(sc89890->dev, "Config ADC failed %d\n", ret);
710 		return ret;
711 	}
712 
713 	ret = sc89890_field_write(sc89890, F_AUTO_DPDM_EN, 0);
714 	if (ret < 0) {
715 		dev_err(sc89890->dev, "Config F_AUTO_DPDM_EN failed %d\n", ret);
716 		return ret;
717 	}
718 
719 	ret = sc89890_field_write(sc89890, F_HVDCP_EN, 0);
720 	if (ret < 0) {
721 		dev_err(sc89890->dev, "Config F_HVDCP_EN failed %d\n", ret);
722 		return ret;
723 	}
724 
725 	ret = sc89890_get_chip_state(sc89890, &sc89890->state);
726 	if (ret < 0) {
727 		dev_err(sc89890->dev, "Get state failed %d\n", ret);
728 		return ret;
729 	}
730 
731 	return 0;
732 }
733 
734 static const enum power_supply_property sc89890_power_supply_props[] = {
735 	POWER_SUPPLY_PROP_MANUFACTURER,
736 	POWER_SUPPLY_PROP_MODEL_NAME,
737 	POWER_SUPPLY_PROP_STATUS,
738 	POWER_SUPPLY_PROP_CHARGE_TYPE,
739 	POWER_SUPPLY_PROP_ONLINE,
740 	POWER_SUPPLY_PROP_HEALTH,
741 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
742 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
743 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
744 	POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
745 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
746 	POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT,
747 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
748 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
749 	POWER_SUPPLY_PROP_CURRENT_NOW,
750 };
751 
752 static char *sc89890_charger_supplied_to[] = {
753 	"usb",
754 };
755 
756 static const struct power_supply_desc sc89890_power_supply_desc = {
757 	.name = "sc89890-charger",
758 	.type = POWER_SUPPLY_TYPE_USB,
759 	.properties = sc89890_power_supply_props,
760 	.num_properties = ARRAY_SIZE(sc89890_power_supply_props),
761 	.set_property = sc89890_power_supply_set_property,
762 	.get_property = sc89890_power_supply_get_property,
763 };
764 
sc89890_power_supply_init(struct sc89890_device * sc89890)765 static int sc89890_power_supply_init(struct sc89890_device *sc89890)
766 {
767 	struct power_supply_config psy_cfg = { .drv_data = sc89890, };
768 
769 	psy_cfg.of_node = sc89890->dev->of_node;
770 	psy_cfg.supplied_to = sc89890_charger_supplied_to;
771 	psy_cfg.num_supplicants = ARRAY_SIZE(sc89890_charger_supplied_to);
772 
773 	sc89890->charger = devm_power_supply_register(sc89890->dev,
774 						      &sc89890_power_supply_desc,
775 						      &psy_cfg);
776 
777 	if (PTR_ERR_OR_ZERO(sc89890->charger)) {
778 		dev_err(sc89890->dev, "failed to register power supply\n");
779 		return PTR_ERR(sc89890->charger);
780 	}
781 
782 	return 0;
783 }
784 
sc89890_get_chip_version(struct sc89890_device * sc89890)785 static int sc89890_get_chip_version(struct sc89890_device *sc89890)
786 {
787 	int id;
788 
789 	id = sc89890_field_read(sc89890, F_PN);
790 	if (id < 0) {
791 		dev_err(sc89890->dev, "Cannot read chip ID.\n");
792 		return id;
793 	} else if (id != SC89890_ID) {
794 		dev_err(sc89890->dev, "Unknown chip ID %d\n", id);
795 		return -ENODEV;
796 	}
797 
798 	DBG("charge IC: SC89890\n");
799 
800 	return 0;
801 }
802 
sc89890_set_otg_vbus(struct sc89890_device * sc,bool enable)803 static void sc89890_set_otg_vbus(struct sc89890_device *sc, bool enable)
804 {
805 	sc89890_field_write(sc, F_OTG_CFG, enable);
806 }
807 
sc89890_otg_vbus_enable(struct regulator_dev * dev)808 static int sc89890_otg_vbus_enable(struct regulator_dev *dev)
809 {
810 	struct sc89890_device *sc = rdev_get_drvdata(dev);
811 
812 	sc89890_set_otg_vbus(sc, true);
813 
814 	return 0;
815 }
816 
sc89890_otg_vbus_disable(struct regulator_dev * dev)817 static int sc89890_otg_vbus_disable(struct regulator_dev *dev)
818 {
819 	struct sc89890_device *sc = rdev_get_drvdata(dev);
820 
821 	sc89890_set_otg_vbus(sc, false);
822 
823 	return 0;
824 }
825 
sc89890_otg_vbus_is_enabled(struct regulator_dev * dev)826 static int sc89890_otg_vbus_is_enabled(struct regulator_dev *dev)
827 {
828 	struct sc89890_device *sc = rdev_get_drvdata(dev);
829 	u8 val;
830 
831 	val = sc89890_field_read(sc, F_OTG_CFG);
832 
833 	return val;
834 }
835 
836 static const struct regulator_ops sc89890_otg_vbus_ops = {
837 	.enable = sc89890_otg_vbus_enable,
838 	.disable = sc89890_otg_vbus_disable,
839 	.is_enabled = sc89890_otg_vbus_is_enabled,
840 };
841 
842 static const struct regulator_desc sc89890_otg_vbus_desc = {
843 	.name = "otg-vbus",
844 	.of_match = "otg-vbus",
845 	.regulators_node = of_match_ptr("regulators"),
846 	.owner = THIS_MODULE,
847 	.ops = &sc89890_otg_vbus_ops,
848 	.type = REGULATOR_VOLTAGE,
849 	.fixed_uV = 5000000,
850 	.n_voltages = 1,
851 };
852 
sc89890_register_otg_vbus_regulator(struct sc89890_device * sc)853 static int sc89890_register_otg_vbus_regulator(struct sc89890_device *sc)
854 {
855 	struct regulator_config config = { };
856 	struct device_node *np;
857 
858 	np = of_get_child_by_name(sc->dev->of_node, "regulators");
859 	if (!np) {
860 		dev_warn(sc->dev, "cannot find regulators node\n");
861 		return -ENXIO;
862 	}
863 
864 	config.dev = sc->dev;
865 	config.driver_data = sc;
866 
867 	sc->otg_vbus_reg = devm_regulator_register(sc->dev,
868 						   &sc89890_otg_vbus_desc,
869 						   &config);
870 	if (IS_ERR(sc->otg_vbus_reg))
871 		return PTR_ERR(sc->otg_vbus_reg);
872 
873 	return 0;
874 }
875 
registers_show(struct device * dev,struct device_attribute * attr,char * buf)876 static ssize_t registers_show(struct device *dev,
877 			      struct device_attribute *attr,
878 			      char *buf)
879 {
880 	struct sc89890_device *sc89890 = dev_get_drvdata(dev);
881 	u8 tmpbuf[SC89890_DEBUG_BUF_LEN];
882 	int idx = 0;
883 	u8 addr;
884 	int val;
885 	int len;
886 	int ret;
887 
888 	sc89890_field_write(sc89890, F_CONV_START, 1);
889 
890 	regmap_field_read_poll_timeout(sc89890->rmap_fields[F_CONV_START],
891 		ret, !ret, 25000, 1000000);
892 
893 	for (addr = 0x0; addr <= 0x14; addr++) {
894 		ret = regmap_read(sc89890->rmap, addr, &val);
895 		if (ret == 0) {
896 			len = snprintf(tmpbuf, SC89890_DEBUG_BUF_LEN,
897 					"Reg[%.2X] = 0x%.2x\n", addr, val);
898 			memcpy(&buf[idx], tmpbuf, len);
899 			idx += len;
900 		}
901 	}
902 
903 	val = sc89890_find_val(sc89890->init_data.vreg, TBL_VREG);
904 	pr_info("CHARGE_VOLTAGE_MAX: %d\n", val / 1000);
905 
906 	val = sc89890_find_val(sc89890->init_data.iprechg, TBL_ITERM);
907 	pr_info("PRECHARGE_CURRENT: %d\n", val / 1000);
908 
909 	val = sc89890_find_val(sc89890->init_data.iterm, TBL_ITERM);
910 	pr_info("CHARGE_TERM_CURRENT: %d\n", val / 1000);
911 
912 	ret = sc89890_field_read(sc89890, F_BATV); /* read measured value */
913 	if (ret)
914 		dev_err(dev, "read F_BAT error!\n");
915 	else {
916 		/* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
917 		val = 2304000 + ret * 20000;
918 		pr_info("charge voltage: %d\n", val / 1000);
919 	}
920 
921 	ret = sc89890_field_read(sc89890, F_IILIM);
922 	if (ret)
923 		dev_err(dev, "read F_IILIM error!\n");
924 	else {
925 		val = sc89890_find_val(ret, TBL_IILIM);
926 		pr_info("INPUT_CURRENT_LIMIT: %d\n", val / 1000);
927 	}
928 	ret = sc89890_field_read(sc89890, F_SYSV); /* read measured value */
929 	if (ret)
930 		dev_err(dev, "read F_SYSV error!\n");
931 	else {
932 		/* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
933 		val = 2304000 + ret * 20000;
934 		pr_info("VOLTAGE_NOW: %d\n", val / 1000);
935 	}
936 	ret = sc89890_field_read(sc89890, F_ICHGR); /* read measured value */
937 	if (ret)
938 		dev_err(dev, "read F_ICHRG error!\n");
939 	else {
940 		/* converted_val = ADC_val * 50mA (table 10.3.19) */
941 		val = ret * -50000;
942 		pr_info("CURRENT_NOW: %d\n", val / 1000);
943 	}
944 	return idx;
945 }
946 
registers_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)947 static ssize_t registers_store(struct device *dev,
948 			       struct device_attribute *attr,
949 			       const char *buf, size_t count)
950 {
951 	struct sc89890_device *sc89890 = dev_get_drvdata(dev);
952 	int ret;
953 	unsigned int reg;
954 	int val;
955 
956 	ret = sscanf(buf, "%x %x", &reg, &val);
957 	if (ret == 2 && reg <= 0x14)
958 		regmap_write(sc89890->rmap, (unsigned char)reg, val);
959 
960 	return count;
961 }
962 
963 static DEVICE_ATTR_RW(registers);
964 
sc89890_create_device_node(struct device * dev)965 static void sc89890_create_device_node(struct device *dev)
966 {
967 	device_create_file(dev, &dev_attr_registers);
968 }
969 
sc89890_fw_read_u32_props(struct sc89890_device * sc89890)970 static int sc89890_fw_read_u32_props(struct sc89890_device *sc89890)
971 {
972 	struct sc89890_init_data *init = &sc89890->init_data;
973 	u32 property;
974 	int ret;
975 	int i;
976 	struct {
977 		char *name;
978 		bool optional;
979 		enum sc89890_table_ids tbl_id;
980 		u8 *conv_data; /* holds converted value from given property */
981 	} props[] = {
982 		/* required properties */
983 		{"sc,charge-current", false, TBL_ICHG, &init->ichg},
984 		{"sc,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
985 		{"sc,termination-current", false, TBL_ITERM, &init->iterm},
986 		{"sc,precharge-current", false, TBL_ITERM, &init->iprechg},
987 		{"sc,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
988 		{"sc,boost-voltage", false, TBL_BOOSTV, &init->boostv},
989 		{"sc,boost-max-current", false, TBL_BOOSTI, &init->boosti},
990 
991 		/* optional properties */
992 		{"sc,thermal-regulation-threshold", true, TBL_TREG, &init->treg},
993 		{"sc,ibatcomp-micro-ohms", true, TBL_RBATCOMP, &init->rbatcomp},
994 		{"sc,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP, &init->vclamp},
995 	};
996 
997 	/* initialize data for optional properties */
998 	init->treg = 3; /* 120 degrees Celsius */
999 	init->rbatcomp = 0;
1000 	init->vclamp = 0; /* IBAT compensation disabled */
1001 
1002 	for (i = 0; i < ARRAY_SIZE(props); i++) {
1003 		ret = device_property_read_u32(sc89890->dev,
1004 					       props[i].name,
1005 					       &property);
1006 		if (ret < 0) {
1007 			if (props[i].optional)
1008 				continue;
1009 
1010 			dev_err(sc89890->dev, "Unable to read property %d %s\n", ret,
1011 				props[i].name);
1012 
1013 			return ret;
1014 		}
1015 
1016 		*props[i].conv_data = sc89890_find_idx(property,
1017 						       props[i].tbl_id);
1018 	}
1019 
1020 	return 0;
1021 }
1022 
sc89890_fw_probe(struct sc89890_device * sc89890)1023 static int sc89890_fw_probe(struct sc89890_device *sc89890)
1024 {
1025 	int ret;
1026 	struct sc89890_init_data *init = &sc89890->init_data;
1027 
1028 	ret = sc89890_fw_read_u32_props(sc89890);
1029 	if (ret < 0)
1030 		return ret;
1031 
1032 	init->ilim_en = device_property_read_bool(sc89890->dev, "sc,use-ilim-pin");
1033 	init->boostf = device_property_read_bool(sc89890->dev, "sc,boost-low-freq");
1034 
1035 	return 0;
1036 }
1037 
sc89890_probe(struct i2c_client * client,const struct i2c_device_id * id)1038 static int sc89890_probe(struct i2c_client *client,
1039 			 const struct i2c_device_id *id)
1040 {
1041 	struct device *dev = &client->dev;
1042 	struct sc89890_device *sc89890;
1043 	int ret;
1044 	int i;
1045 
1046 	sc89890 = devm_kzalloc(dev, sizeof(*sc89890), GFP_KERNEL);
1047 	if (!sc89890)
1048 		return -ENOMEM;
1049 
1050 	sc89890->client = client;
1051 	sc89890->dev = dev;
1052 
1053 	mutex_init(&sc89890->lock);
1054 	sc89890->rmap = devm_regmap_init_i2c(client, &sc89890_regmap_config);
1055 	if (IS_ERR(sc89890->rmap)) {
1056 		dev_err(dev, "failed to allocate register map\n");
1057 		return PTR_ERR(sc89890->rmap);
1058 	}
1059 
1060 	for (i = 0; i < ARRAY_SIZE(sc89890_reg_fields); i++) {
1061 		const struct reg_field *reg_fields = sc89890_reg_fields;
1062 
1063 		sc89890->rmap_fields[i] = devm_regmap_field_alloc(dev,
1064 								  sc89890->rmap,
1065 								  reg_fields[i]);
1066 		if (IS_ERR(sc89890->rmap_fields[i])) {
1067 			dev_err(dev, "cannot allocate regmap field\n");
1068 			return PTR_ERR(sc89890->rmap_fields[i]);
1069 		}
1070 	}
1071 
1072 	i2c_set_clientdata(client, sc89890);
1073 
1074 	ret = sc89890_get_chip_version(sc89890);
1075 	if (ret) {
1076 		dev_err(dev, "Cannot read chip ID or unknown chip.\n");
1077 		return ret;
1078 	}
1079 
1080 	ret = sc89890_power_supply_init(sc89890);
1081 	if (ret < 0) {
1082 		dev_err(dev, "Failed to register power supply\n");
1083 		goto irq_fail;
1084 	}
1085 
1086 	if (!dev->platform_data) {
1087 		ret = sc89890_fw_probe(sc89890);
1088 		if (ret < 0) {
1089 			dev_err(dev, "Cannot read device properties.\n");
1090 			return ret;
1091 		}
1092 	} else {
1093 		return -ENODEV;
1094 	}
1095 
1096 	ret = sc89890_hw_init(sc89890);
1097 	if (ret < 0) {
1098 		dev_err(dev, "Cannot initialize the chip.\n");
1099 		return ret;
1100 	}
1101 
1102 
1103 	if (client->irq < 0) {
1104 		dev_err(dev, "No irq resource found.\n");
1105 		return client->irq;
1106 	}
1107 
1108 	ret = devm_request_threaded_irq(dev, client->irq, NULL,
1109 					sc89890_irq_handler_thread,
1110 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1111 					SC89890_IRQ, sc89890);
1112 	if (ret)
1113 		goto irq_fail;
1114 
1115 	sc89890_register_otg_vbus_regulator(sc89890);
1116 	sc89890_create_device_node(sc89890->dev);
1117 
1118 	return 0;
1119 
1120 irq_fail:
1121 
1122 	return ret;
1123 }
1124 
sc89890_remove(struct i2c_client * client)1125 static int sc89890_remove(struct i2c_client *client)
1126 {
1127 	struct sc89890_device *sc89890 = i2c_get_clientdata(client);
1128 
1129 	/* reset all registers to default values */
1130 	sc89890_chip_reset(sc89890);
1131 
1132 	return 0;
1133 }
1134 
1135 #ifdef CONFIG_PM_SLEEP
sc89890_suspend(struct device * dev)1136 static int sc89890_suspend(struct device *dev)
1137 {
1138 	struct sc89890_device *sc89890 = dev_get_drvdata(dev);
1139 
1140 	/*
1141 	 * If charger is removed, while in suspend, make sure ADC is disabled
1142 	 * since it consumes slightly more power.
1143 	 */
1144 	return sc89890_field_write(sc89890, F_CONV_RATE, 0);
1145 }
1146 
sc89890_resume(struct device * dev)1147 static int sc89890_resume(struct device *dev)
1148 {
1149 	int ret;
1150 	struct sc89890_device *sc89890 = dev_get_drvdata(dev);
1151 
1152 	mutex_lock(&sc89890->lock);
1153 
1154 	ret = sc89890_get_chip_state(sc89890, &sc89890->state);
1155 	if (ret < 0)
1156 		goto unlock;
1157 
1158 	/* Re-enable ADC only if charger is plugged in. */
1159 	if (sc89890->state.online) {
1160 		ret = sc89890_field_write(sc89890, F_CONV_RATE, 1);
1161 		if (ret < 0)
1162 			goto unlock;
1163 	}
1164 
1165 	/* signal userspace, maybe state changed while suspended */
1166 	power_supply_changed(sc89890->charger);
1167 
1168 unlock:
1169 	mutex_unlock(&sc89890->lock);
1170 
1171 	return ret;
1172 }
1173 #endif
1174 
1175 static const struct dev_pm_ops sc89890_pm = {
1176 	SET_SYSTEM_SLEEP_PM_OPS(sc89890_suspend, sc89890_resume)
1177 };
1178 
1179 static const struct i2c_device_id sc89890_i2c_ids[] = {
1180 	{ "sc89890", 0 },
1181 	{},
1182 };
1183 MODULE_DEVICE_TABLE(i2c, sc89890_i2c_ids);
1184 
1185 static const struct of_device_id sc89890_of_match[] = {
1186 	{ .compatible = "sc,sc89890", },
1187 	{ },
1188 };
1189 MODULE_DEVICE_TABLE(of, sc89890_of_match);
1190 
1191 #ifdef CONFIG_ACPI
1192 static const struct acpi_device_id sc89890_acpi_match[] = {
1193 	{"SC898900", 0},
1194 	{},
1195 };
1196 MODULE_DEVICE_TABLE(acpi, sc89890_acpi_match);
1197 #endif
1198 
1199 static struct i2c_driver sc89890_driver = {
1200 	.driver = {
1201 		.name = "sc89890-charger",
1202 		.of_match_table = of_match_ptr(sc89890_of_match),
1203 		.acpi_match_table = ACPI_PTR(sc89890_acpi_match),
1204 		.pm = &sc89890_pm,
1205 	},
1206 	.probe = sc89890_probe,
1207 	.remove = sc89890_remove,
1208 	.id_table = sc89890_i2c_ids,
1209 };
1210 module_i2c_driver(sc89890_driver);
1211 
1212 MODULE_AUTHOR("xsf<xsf@rock-chips.com>");
1213 MODULE_DESCRIPTION("sc89890 charger driver");
1214 MODULE_LICENSE("GPL");
1215