1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Chrager driver for Sgm4154x
4 *
5 * Copyright (c) 2022 Rockchip Electronics Co., Ltd.
6 *
7 * Author: Xu Shengfei <xsf@rock-chips.com>
8 */
9
10 #include <linux/delay.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/power_supply.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/of_regulator.h>
18 #include <linux/regulator/machine.h>
19 #include <linux/types.h>
20
21 static int dbg_enable;
22
23 module_param_named(dbg_level, dbg_enable, int, 0644);
24
25 #define DBG(args...) \
26 do { \
27 if (dbg_enable) { \
28 pr_info(args); \
29 } \
30 } while (0)
31
32 #define SGM4154x_MANUFACTURER "SGMICRO"
33 #define SGM4154x_NAME "sgm41542"
34 #define SGM4154x_PN_ID (BIT(6) | BIT(5) | BIT(3))
35
36 /* define register */
37 #define SGM4154x_CHRG_CTRL_0 0x00
38 #define SGM4154x_CHRG_CTRL_1 0x01
39 #define SGM4154x_CHRG_CTRL_2 0x02
40 #define SGM4154x_CHRG_CTRL_3 0x03
41 #define SGM4154x_CHRG_CTRL_4 0x04
42 #define SGM4154x_CHRG_CTRL_5 0x05
43 #define SGM4154x_CHRG_CTRL_6 0x06
44 #define SGM4154x_CHRG_CTRL_7 0x07
45 #define SGM4154x_CHRG_STAT 0x08
46 #define SGM4154x_CHRG_FAULT 0x09
47 #define SGM4154x_CHRG_CTRL_a 0x0a
48 #define SGM4154x_CHRG_CTRL_b 0x0b
49 #define SGM4154x_CHRG_CTRL_c 0x0c
50 #define SGM4154x_CHRG_CTRL_d 0x0d
51 #define SGM4154x_INPUT_DET 0x0e
52 #define SGM4154x_CHRG_CTRL_f 0x0f
53
54 /* charge status flags */
55 #define SGM4154x_CHRG_EN BIT(4)
56 #define SGM4154x_HIZ_EN BIT(7)
57 #define SGM4154x_TERM_EN BIT(7)
58 #define SGM4154x_VAC_OVP_MASK GENMASK(7, 6)
59 #define SGM4154x_DPDM_ONGOING BIT(7)
60 #define SGM4154x_VBUS_GOOD BIT(7)
61
62 #define SGM4154x_BOOSTV GENMASK(5, 4)
63 #define SGM4154x_BOOST_LIM BIT(7)
64 #define SGM4154x_OTG_EN BIT(5)
65
66 /* Part ID */
67 #define SGM4154x_PN_MASK GENMASK(6, 3)
68
69 /* WDT TIMER SET */
70 #define SGM4154x_WDT_TIMER_MASK GENMASK(5, 4)
71 #define SGM4154x_WDT_TIMER_DISABLE 0
72 #define SGM4154x_WDT_TIMER_40S BIT(4)
73 #define SGM4154x_WDT_TIMER_80S BIT(5)
74 #define SGM4154x_WDT_TIMER_160S (BIT(4) | BIT(5))
75
76 #define SGM4154x_WDT_RST_MASK BIT(6)
77 #define SGM4154x_WDT_RST BIT(6)
78
79 /* SAFETY TIMER SET */
80 #define SGM4154x_SAFETY_TIMER_MASK GENMASK(3, 3)
81 #define SGM4154x_SAFETY_TIMER_DISABLE 0
82 #define SGM4154x_SAFETY_TIMER_EN BIT(3)
83 #define SGM4154x_SAFETY_TIMER_5H 0
84 #define SGM4154x_SAFETY_TIMER_10H BIT(2)
85
86 /* recharge voltage */
87 #define SGM4154x_VRECHARGE BIT(0)
88 #define SGM4154x_VRECHRG_STEP_mV 100
89 #define SGM4154x_VRECHRG_OFFSET_mV 100
90
91 /* charge status */
92 #define SGM4154x_VSYS_STAT BIT(0)
93 #define SGM4154x_THERM_STAT BIT(1)
94 #define SGM4154x_PG_STAT BIT(2)
95 #define SGM4154x_CHG_STAT_MASK GENMASK(4, 3)
96 #define SGM4154x_PRECHRG BIT(3)
97 #define SGM4154x_FAST_CHRG BIT(4)
98 #define SGM4154x_TERM_CHRG (BIT(3) | BIT(4))
99
100 /* charge type */
101 #define SGM4154x_VBUS_STAT_MASK GENMASK(7, 5)
102 #define SGM4154x_NOT_CHRGING 0
103 #define SGM4154x_USB_SDP BIT(5)
104 #define SGM4154x_USB_CDP BIT(6)
105 #define SGM4154x_USB_DCP (BIT(5) | BIT(6))
106 #define SGM4154x_UNKNOWN (BIT(7) | BIT(5))
107 #define SGM4154x_NON_STANDARD (BIT(7) | BIT(6))
108 #define SGM4154x_OTG_MODE (BIT(7) | BIT(6) | BIT(5))
109
110 /* TEMP Status */
111 #define SGM4154x_TEMP_MASK GENMASK(2, 0)
112 #define SGM4154x_TEMP_NORMAL BIT(0)
113 #define SGM4154x_TEMP_WARM BIT(1)
114 #define SGM4154x_TEMP_COOL (BIT(0) | BIT(1))
115 #define SGM4154x_TEMP_COLD (BIT(0) | BIT(3))
116 #define SGM4154x_TEMP_HOT (BIT(2) | BIT(3))
117
118 /* precharge current */
119 #define SGM4154x_PRECHRG_CUR_MASK GENMASK(7, 4)
120 #define SGM4154x_PRECHRG_CURRENT_STEP_uA 60000
121 #define SGM4154x_PRECHRG_I_MIN_uA 60000
122 #define SGM4154x_PRECHRG_I_MAX_uA 780000
123 #define SGM4154x_PRECHRG_I_DEF_uA 180000
124
125 /* termination current */
126 #define SGM4154x_TERMCHRG_CUR_MASK GENMASK(3, 0)
127 #define SGM4154x_TERMCHRG_CURRENT_STEP_uA 60000
128 #define SGM4154x_TERMCHRG_I_MIN_uA 60000
129 #define SGM4154x_TERMCHRG_I_MAX_uA 960000
130 #define SGM4154x_TERMCHRG_I_DEF_uA 180000
131
132 /* charge current */
133 #define SGM4154x_ICHRG_CUR_MASK GENMASK(5, 0)
134 #define SGM4154x_ICHRG_I_STEP_uA 60000
135 #define SGM4154x_ICHRG_I_MIN_uA 0
136 #define SGM4154x_ICHRG_I_MAX_uA 3000000
137 #define SGM4154x_ICHRG_I_DEF_uA 2040000
138
139 /* charge voltage */
140 #define SGM4154x_VREG_V_MASK GENMASK(7, 3)
141 #define SGM4154x_VREG_V_MAX_uV 4624000
142 #define SGM4154x_VREG_V_MIN_uV 3856000
143 #define SGM4154x_VREG_V_DEF_uV 4208000
144 #define SGM4154x_VREG_V_STEP_uV 32000
145
146 /* VREG Fine Tuning */
147 #define SGM4154x_VREG_FT_MASK GENMASK(7, 6)
148 #define SGM4154x_VREG_FT_UP_8mV BIT(6)
149 #define SGM4154x_VREG_FT_DN_8mV BIT(7)
150 #define SGM4154x_VREG_FT_DN_16mV (BIT(7) | BIT(6))
151
152 /* iindpm current */
153 #define SGM4154x_IINDPM_I_MASK GENMASK(4, 0)
154 #define SGM4154x_IINDPM_I_MIN_uA 100000
155 #define SGM4154x_IINDPM_I_MAX_uA 3800000
156 #define SGM4154x_IINDPM_STEP_uA 100000
157 #define SGM4154x_IINDPM_DEF_uA 2400000
158
159 #define SGM4154x_VINDPM_INT_MASK BIT(1)
160 #define SGM4154x_VINDPM_INT_DIS BIT(1)
161 #define SGM4154x_IINDPM_INT_MASK BIT(0)
162 #define SGM4154x_IINDPM_INT_DIS BIT(0)
163
164 /* vindpm voltage */
165 #define SGM4154x_VINDPM_V_MASK GENMASK(3, 0)
166 #define SGM4154x_VINDPM_V_MIN_uV 3900000
167 #define SGM4154x_VINDPM_V_MAX_uV 12000000
168 #define SGM4154x_VINDPM_STEP_uV 100000
169 #define SGM4154x_VINDPM_DEF_uV 4500000
170 #define SGM4154x_VINDPM_OS_MASK GENMASK(1, 0)
171
172 /* DP DM SEL */
173 #define SGM4154x_DP_VSEL_MASK GENMASK(4, 3)
174 #define SGM4154x_DM_VSEL_MASK GENMASK(2, 1)
175
176 /* PUMPX SET */
177 #define SGM4154x_EN_PUMPX BIT(7)
178 #define SGM4154x_PUMPX_UP BIT(6)
179 #define SGM4154x_PUMPX_DN BIT(5)
180
181 #define SGM4154x_BATFET_DIS_MSK BIT(5)
182 #define SGM4154x_BATFET_DIS_DIS BIT(5)
183 #define SGM4154x_BATFET_DLY_MSK BIT(3)
184 #define SGM4154x_BATFET_DLY_EN BIT(3)
185
186 #define DEFAULT_INPUT_CURRENT (500 * 1000)
187
188 struct sgm4154x_init_data {
189 int ichg; /* charge current */
190 int ilim; /* input current */
191 int vreg; /* regulation voltage */
192 int iterm; /* termination current */
193 int iprechg; /* precharge current */
194 int vlim; /* minimum system voltage limit */
195 int max_ichg;
196 int max_vreg;
197 };
198
199 struct sgm4154x_state {
200 bool vsys_stat;
201 bool therm_stat;
202 bool online;
203 u8 chrg_stat;
204 u8 vbus_status;
205
206 bool chrg_en;
207 bool hiz_en;
208 bool term_en;
209 bool vbus_gd;
210 u8 chrg_type;
211 u8 health;
212 u8 chrg_fault;
213 u8 ntc_fault;
214 };
215
216 struct sgm4154x_device {
217 struct i2c_client *client;
218 struct device *dev;
219 struct power_supply *charger;
220 struct mutex lock;
221 struct mutex i2c_rw_lock;
222 struct regmap *regmap;
223
224 char model_name[I2C_NAME_SIZE];
225 int device_id;
226
227 struct sgm4154x_init_data init_data;
228 struct sgm4154x_state state;
229 struct regulator_dev *otg_rdev;
230 struct notifier_block pm_nb;
231 int input_current;
232 bool sgm4154x_suspend_flag;
233 bool watchdog_enable;
234 struct workqueue_struct *sgm_monitor_wq;
235 struct delayed_work sgm_delay_work;
236 };
237
238 /* SGM4154x REG06 BOOST_LIM[5:4], uV */
239 static const unsigned int BOOST_VOLT_LIMIT[] = {
240 4850000, 5000000, 5150000, 5300000
241 };
242
243 static const unsigned int BOOST_CURRENT_LIMIT[] = {
244 1200000, 2000000
245 };
246
247 enum SGM4154x_VINDPM_OS {
248 VINDPM_OS_3900mV,
249 VINDPM_OS_5900mV,
250 VINDPM_OS_7500mV,
251 VINDPM_OS_10500mV,
252 };
253
sgm4154x_set_term_curr(struct sgm4154x_device * sgm,int uA)254 static int sgm4154x_set_term_curr(struct sgm4154x_device *sgm, int uA)
255 {
256 int reg_val;
257 int ret;
258
259 if (uA < SGM4154x_TERMCHRG_I_MIN_uA)
260 uA = SGM4154x_TERMCHRG_I_MIN_uA;
261 else if (uA > SGM4154x_TERMCHRG_I_MAX_uA)
262 uA = SGM4154x_TERMCHRG_I_MAX_uA;
263
264 reg_val = (uA - SGM4154x_TERMCHRG_I_MIN_uA) / SGM4154x_TERMCHRG_CURRENT_STEP_uA;
265
266 ret = regmap_update_bits(sgm->regmap,
267 SGM4154x_CHRG_CTRL_3,
268 SGM4154x_TERMCHRG_CUR_MASK,
269 reg_val);
270 if (ret)
271 dev_err(sgm->dev, "set term current error!\n");
272
273 return ret;
274 }
275
sgm4154x_set_prechrg_curr(struct sgm4154x_device * sgm,int uA)276 static int sgm4154x_set_prechrg_curr(struct sgm4154x_device *sgm, int uA)
277 {
278 int reg_val;
279 int ret;
280
281 if (uA < SGM4154x_PRECHRG_I_MIN_uA)
282 uA = SGM4154x_PRECHRG_I_MIN_uA;
283 else if (uA > SGM4154x_PRECHRG_I_MAX_uA)
284 uA = SGM4154x_PRECHRG_I_MAX_uA;
285
286 reg_val = (uA - SGM4154x_PRECHRG_I_MIN_uA) / SGM4154x_PRECHRG_CURRENT_STEP_uA;
287
288 reg_val = reg_val << 4;
289 ret = regmap_update_bits(sgm->regmap,
290 SGM4154x_CHRG_CTRL_3,
291 SGM4154x_PRECHRG_CUR_MASK,
292 reg_val);
293 if (ret)
294 dev_err(sgm->dev, "set precharge current error!\n");
295
296 return ret;
297 }
298
sgm4154x_set_ichrg_curr(struct sgm4154x_device * sgm,int uA)299 static int sgm4154x_set_ichrg_curr(struct sgm4154x_device *sgm, int uA)
300 {
301 int reg_val;
302 int ret;
303
304 if (uA < SGM4154x_ICHRG_I_MIN_uA)
305 uA = SGM4154x_ICHRG_I_MIN_uA;
306 else {
307 if ((sgm->init_data.max_ichg > 0) && (uA > sgm->init_data.max_ichg))
308 uA = sgm->init_data.max_ichg;
309 uA = min(uA, SGM4154x_ICHRG_I_MAX_uA);
310 }
311
312 reg_val = uA / SGM4154x_ICHRG_I_STEP_uA;
313
314 ret = regmap_update_bits(sgm->regmap,
315 SGM4154x_CHRG_CTRL_2,
316 SGM4154x_ICHRG_CUR_MASK,
317 reg_val);
318 if (ret)
319 dev_err(sgm->dev, "set icharge current error!\n");
320
321 return ret;
322 }
323
sgm4154x_set_chrg_volt(struct sgm4154x_device * sgm,int chrg_volt)324 static int sgm4154x_set_chrg_volt(struct sgm4154x_device *sgm, int chrg_volt)
325 {
326 int reg_val;
327 int ret;
328
329 if (chrg_volt < SGM4154x_VREG_V_MIN_uV)
330 chrg_volt = SGM4154x_VREG_V_MIN_uV;
331 else {
332 if ((sgm->init_data.max_vreg > 0) && (chrg_volt > sgm->init_data.max_vreg))
333 chrg_volt = sgm->init_data.max_vreg;
334 chrg_volt = min(chrg_volt, SGM4154x_VREG_V_MAX_uV);
335 }
336
337 reg_val = (chrg_volt - SGM4154x_VREG_V_MIN_uV) / SGM4154x_VREG_V_STEP_uV;
338 if (chrg_volt == 4200 * 1000)
339 reg_val = 0x0b;
340 reg_val = reg_val << 3;
341 ret = regmap_update_bits(sgm->regmap,
342 SGM4154x_CHRG_CTRL_4,
343 SGM4154x_VREG_V_MASK,
344 reg_val);
345 if (ret)
346 dev_err(sgm->dev, "set charge voltage error!\n");
347
348 return ret;
349 }
350
sgm4154x_set_vindpm_offset_os(struct sgm4154x_device * sgm,enum SGM4154x_VINDPM_OS offset_os)351 static int sgm4154x_set_vindpm_offset_os(struct sgm4154x_device *sgm,
352 enum SGM4154x_VINDPM_OS offset_os)
353 {
354 int ret;
355
356 ret = regmap_update_bits(sgm->regmap,
357 SGM4154x_CHRG_CTRL_f,
358 SGM4154x_VINDPM_OS_MASK,
359 offset_os);
360
361 if (ret)
362 dev_err(sgm->dev, "set vindpm offset os error!\n");
363
364 return ret;
365 }
366
sgm4154x_set_input_volt_lim(struct sgm4154x_device * sgm,unsigned int vindpm)367 static int sgm4154x_set_input_volt_lim(struct sgm4154x_device *sgm,
368 unsigned int vindpm)
369 {
370 enum SGM4154x_VINDPM_OS os_val;
371 unsigned int offset;
372 u8 reg_val;
373 int ret;
374
375
376 if (vindpm < SGM4154x_VINDPM_V_MIN_uV ||
377 vindpm > SGM4154x_VINDPM_V_MAX_uV)
378 return -EINVAL;
379
380 if (vindpm < 5900000) {
381 os_val = VINDPM_OS_3900mV;
382 offset = 3900000;
383 } else if (vindpm >= 5900000 && vindpm < 7500000) {
384 os_val = VINDPM_OS_5900mV;
385 offset = 5900000;
386 } else if (vindpm >= 7500000 && vindpm < 10500000) {
387 os_val = VINDPM_OS_7500mV;
388 offset = 7500000;
389 } else {
390 os_val = VINDPM_OS_10500mV;
391 offset = 10500000;
392 }
393
394 ret = sgm4154x_set_vindpm_offset_os(sgm, os_val);
395 if (ret) {
396 dev_err(sgm->dev, "set vin dpm error!\n");
397 return ret;
398 }
399
400 reg_val = (vindpm - offset) / SGM4154x_VINDPM_STEP_uV;
401
402 ret = regmap_update_bits(sgm->regmap, SGM4154x_CHRG_CTRL_6,
403 SGM4154x_VINDPM_V_MASK, reg_val);
404 if (ret) {
405 dev_err(sgm->dev, "input voltage error!\n");
406 return ret;
407 }
408
409 return ret;
410 }
411
sgm4154x_set_input_curr_lim(struct sgm4154x_device * sgm,int iindpm)412 static int sgm4154x_set_input_curr_lim(struct sgm4154x_device *sgm, int iindpm)
413 {
414 int reg_val;
415 int ret;
416
417 if (iindpm < SGM4154x_IINDPM_I_MIN_uA)
418 return -EINVAL;
419
420 if ((iindpm > SGM4154x_IINDPM_I_MAX_uA) || (iindpm > sgm->init_data.ilim))
421 iindpm = min(SGM4154x_IINDPM_I_MAX_uA, sgm->init_data.ilim);
422
423 sgm->input_current = iindpm;
424 reg_val = (iindpm-SGM4154x_IINDPM_I_MIN_uA) / SGM4154x_IINDPM_STEP_uA;
425
426 ret = regmap_update_bits(sgm->regmap,
427 SGM4154x_CHRG_CTRL_0,
428 SGM4154x_IINDPM_I_MASK,
429 reg_val);
430 if (ret)
431 dev_err(sgm->dev, "set input current limit error!\n");
432
433 return ret;
434 }
435
sgm4154x_get_input_curr_lim(struct sgm4154x_device * sgm)436 static int sgm4154x_get_input_curr_lim(struct sgm4154x_device *sgm)
437 {
438 int ret;
439 int ilim;
440
441 ret = regmap_read(sgm->regmap, SGM4154x_CHRG_CTRL_0, &ilim);
442 if (ret) {
443 dev_err(sgm->dev, "get input current limit error!\n");
444 return ret;
445 }
446 if (SGM4154x_IINDPM_I_MASK == (ilim & SGM4154x_IINDPM_I_MASK))
447 return SGM4154x_IINDPM_I_MAX_uA;
448
449 ilim = (ilim & SGM4154x_IINDPM_I_MASK) * SGM4154x_IINDPM_STEP_uA + SGM4154x_IINDPM_I_MIN_uA;
450
451 return ilim;
452 }
453
sgm4154x_watchdog_timer_reset(struct sgm4154x_device * sgm)454 static int sgm4154x_watchdog_timer_reset(struct sgm4154x_device *sgm)
455 {
456 int ret;
457
458 ret = regmap_update_bits(sgm->regmap,
459 SGM4154x_CHRG_CTRL_1,
460 SGM4154x_WDT_RST_MASK,
461 SGM4154x_WDT_RST);
462
463 if (ret)
464 dev_err(sgm->dev, "set watchdog timer error!\n");
465
466 return ret;
467 }
468
sgm4154x_set_watchdog_timer(struct sgm4154x_device * sgm,int time)469 static int sgm4154x_set_watchdog_timer(struct sgm4154x_device *sgm, int time)
470 {
471 u8 reg_val;
472 int ret;
473
474 if (time == 0)
475 reg_val = SGM4154x_WDT_TIMER_DISABLE;
476 else if (time == 40)
477 reg_val = SGM4154x_WDT_TIMER_40S;
478 else if (time == 80)
479 reg_val = SGM4154x_WDT_TIMER_80S;
480 else
481 reg_val = SGM4154x_WDT_TIMER_160S;
482
483 ret = regmap_update_bits(sgm->regmap,
484 SGM4154x_CHRG_CTRL_5,
485 SGM4154x_WDT_TIMER_MASK,
486 reg_val);
487
488 if (ret) {
489 dev_err(sgm->dev, "set watchdog timer error!\n");
490 return ret;
491 }
492
493 if (time) {
494 DBG("sgm41542: enable watchdog\n");
495 if (!sgm->watchdog_enable)
496 queue_delayed_work(sgm->sgm_monitor_wq,
497 &sgm->sgm_delay_work,
498 msecs_to_jiffies(1000 * 5));
499 sgm->watchdog_enable = true;
500 } else {
501 DBG("sgm41542: disable watchdog\n");
502 sgm->watchdog_enable = false;
503 sgm4154x_watchdog_timer_reset(sgm);
504 }
505
506 return ret;
507 }
508
sgm4154x_enable_charger(struct sgm4154x_device * sgm)509 static int sgm4154x_enable_charger(struct sgm4154x_device *sgm)
510 {
511 int ret;
512
513 ret = regmap_update_bits(sgm->regmap,
514 SGM4154x_CHRG_CTRL_1,
515 SGM4154x_CHRG_EN,
516 SGM4154x_CHRG_EN);
517 if (ret)
518 dev_err(sgm->dev, "enable charger error!\n");
519
520 return ret;
521 }
522
sgm4154x_disable_charger(struct sgm4154x_device * sgm)523 static int sgm4154x_disable_charger(struct sgm4154x_device *sgm)
524 {
525 int ret;
526
527 ret = regmap_update_bits(sgm->regmap,
528 SGM4154x_CHRG_CTRL_1,
529 SGM4154x_CHRG_EN,
530 0);
531 if (ret)
532 dev_err(sgm->dev, "disable charger error!\n");
533
534 return ret;
535 }
536
sgm4154x_set_vac_ovp(struct sgm4154x_device * sgm)537 static int sgm4154x_set_vac_ovp(struct sgm4154x_device *sgm)
538 {
539 int reg_val;
540 int ret;
541
542 reg_val = 0xFF & SGM4154x_VAC_OVP_MASK;
543
544 ret = regmap_update_bits(sgm->regmap,
545 SGM4154x_CHRG_CTRL_6,
546 SGM4154x_VAC_OVP_MASK,
547 reg_val);
548 if (ret)
549 dev_err(sgm->dev, "set vac ovp error!\n");
550
551 return ret;
552 }
553
sgm4154x_set_recharge_volt(struct sgm4154x_device * sgm,int recharge_volt)554 static int sgm4154x_set_recharge_volt(struct sgm4154x_device *sgm, int recharge_volt)
555 {
556 int reg_val;
557 int ret;
558
559 reg_val = (recharge_volt - SGM4154x_VRECHRG_OFFSET_mV) / SGM4154x_VRECHRG_STEP_mV;
560
561 ret = regmap_update_bits(sgm->regmap,
562 SGM4154x_CHRG_CTRL_4,
563 SGM4154x_VRECHARGE,
564 reg_val);
565 if (ret)
566 dev_err(sgm->dev, "set recharger error!\n");
567
568 return ret;
569 }
570
sgm4154x_get_state(struct sgm4154x_device * sgm,struct sgm4154x_state * state)571 static int sgm4154x_get_state(struct sgm4154x_device *sgm,
572 struct sgm4154x_state *state)
573 {
574 int chrg_param_0, chrg_param_1, chrg_param_2;
575 int chrg_stat;
576 int fault;
577 int ret;
578
579 ret = regmap_read(sgm->regmap, SGM4154x_CHRG_STAT, &chrg_stat);
580 if (ret) {
581 pr_err("read SGM4154x_CHRG_STAT fail\n");
582 return ret;
583 }
584
585 DBG("SGM4154x_CHRG_STAT[0x%x]: 0x%x\n", SGM4154x_CHRG_STAT, chrg_stat);
586 state->chrg_type = chrg_stat & SGM4154x_VBUS_STAT_MASK;
587 state->chrg_stat = chrg_stat & SGM4154x_CHG_STAT_MASK;
588 state->online = !!(chrg_stat & SGM4154x_PG_STAT);
589 state->therm_stat = !!(chrg_stat & SGM4154x_THERM_STAT);
590 state->vsys_stat = !!(chrg_stat & SGM4154x_VSYS_STAT);
591
592 ret = regmap_read(sgm->regmap, SGM4154x_CHRG_FAULT, &fault);
593 if (ret) {
594 pr_err("read SGM4154x_CHRG_FAULT fail\n");
595 return ret;
596 }
597 DBG("SGM4154x_CHRG_FAULT[0x%x]: 0x%x\n", SGM4154x_CHRG_FAULT, fault);
598
599 state->chrg_fault = fault;
600 state->ntc_fault = fault & SGM4154x_TEMP_MASK;
601 state->health = state->ntc_fault;
602 ret = regmap_read(sgm->regmap, SGM4154x_CHRG_CTRL_0, &chrg_param_0);
603 if (ret) {
604 pr_err("read SGM4154x_CHRG_CTRL_0 fail\n");
605 return ret;
606 }
607 state->hiz_en = !!(chrg_param_0 & SGM4154x_HIZ_EN);
608 DBG("SGM4154x_CHRG_CTRL_0[0x%x]: 0x%x\n", SGM4154x_CHRG_CTRL_0, chrg_param_0);
609
610 ret = regmap_read(sgm->regmap, SGM4154x_CHRG_CTRL_5, &chrg_param_1);
611 if (ret) {
612 pr_err("read SGM4154x_CHRG_CTRL_5 fail\n");
613 return ret;
614 }
615 state->term_en = !!(chrg_param_1 & SGM4154x_TERM_EN);
616 DBG("SGM4154x_CHRG_CTRL_5[0x%x]: 0x%x\n", SGM4154x_CHRG_CTRL_5, chrg_param_1);
617
618 ret = regmap_read(sgm->regmap, SGM4154x_CHRG_CTRL_a, &chrg_param_2);
619 if (ret) {
620 pr_err("read SGM4154x_CHRG_CTRL_a fail\n");
621 return ret;
622 }
623 state->vbus_gd = !!(chrg_param_2 & SGM4154x_VBUS_GOOD);
624 DBG("SGM4154x_CHRG_CTRL_a[0x%x]: 0x%x\n", SGM4154x_CHRG_CTRL_a, chrg_param_2);
625
626 DBG("chrg_type: 0x%x\n", state->chrg_type);
627 DBG("chrg_stat: 0x%x\n", state->chrg_stat);
628 DBG("online: 0x%x\n", state->online);
629 DBG("therm_stat: 0x%x\n", state->therm_stat);
630 DBG("vsys_stat: 0x%x\n", state->vsys_stat);
631 DBG("chrg_fault: 0x%x\n", state->chrg_fault);
632 DBG("ntc_fault: 0x%x\n", state->ntc_fault);
633 DBG("health: 0x%x\n", state->health);
634 DBG("hiz_en: 0x%x\n", state->hiz_en);
635 DBG("term_en: 0x%x\n", state->term_en);
636 DBG("vbus_gd: 0x%x\n", state->vbus_gd);
637
638 return ret;
639 }
640
sgm4154x_property_is_writeable(struct power_supply * psy,enum power_supply_property prop)641 static int sgm4154x_property_is_writeable(struct power_supply *psy,
642 enum power_supply_property prop)
643 {
644 switch (prop) {
645 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
646 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
647 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
648 case POWER_SUPPLY_PROP_ONLINE:
649 return true;
650 default:
651 return false;
652 }
653 }
654
sgm4154x_charger_set_property(struct power_supply * psy,enum power_supply_property prop,const union power_supply_propval * val)655 static int sgm4154x_charger_set_property(struct power_supply *psy,
656 enum power_supply_property prop,
657 const union power_supply_propval *val)
658 {
659 struct sgm4154x_device *sgm = power_supply_get_drvdata(psy);
660 int ret = -EINVAL;
661
662 switch (prop) {
663 case POWER_SUPPLY_PROP_ONLINE:
664 if (val->intval) {
665 ret = sgm4154x_enable_charger(sgm);
666 sgm4154x_set_watchdog_timer(sgm, SGM4154x_WDT_TIMER_40S);
667 } else {
668 sgm4154x_set_watchdog_timer(sgm, 0);
669 ret = sgm4154x_disable_charger(sgm);
670 }
671 break;
672
673 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
674 ret = sgm4154x_set_input_curr_lim(sgm, val->intval);
675 break;
676 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
677 ret = sgm4154x_set_ichrg_curr(sgm, val->intval);
678 break;
679 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
680 ret = sgm4154x_set_chrg_volt(sgm, val->intval);
681 break;
682
683 default:
684 return -EINVAL;
685 }
686
687 return ret;
688 }
689
sgm4154x_charger_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)690 static int sgm4154x_charger_get_property(struct power_supply *psy,
691 enum power_supply_property psp,
692 union power_supply_propval *val)
693 {
694 struct sgm4154x_device *sgm = power_supply_get_drvdata(psy);
695 struct sgm4154x_state state;
696 int ret = 0;
697
698 mutex_lock(&sgm->lock);
699 ret = sgm4154x_get_state(sgm, &state);
700 if (ret) {
701 dev_err(sgm->dev, "get state error!\n");
702 mutex_unlock(&sgm->lock);
703 return ret;
704 }
705 sgm->state = state;
706 mutex_unlock(&sgm->lock);
707
708 switch (psp) {
709 case POWER_SUPPLY_PROP_STATUS:
710 if (!state.chrg_type || (state.chrg_type == SGM4154x_OTG_MODE))
711 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
712 else if (!state.chrg_stat)
713 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
714 else if (state.chrg_stat == SGM4154x_TERM_CHRG)
715 val->intval = POWER_SUPPLY_STATUS_FULL;
716 else
717 val->intval = POWER_SUPPLY_STATUS_CHARGING;
718 break;
719 case POWER_SUPPLY_PROP_CHARGE_TYPE:
720 switch (state.chrg_stat) {
721 case SGM4154x_PRECHRG:
722 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
723 break;
724 case SGM4154x_FAST_CHRG:
725 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
726 break;
727 case SGM4154x_TERM_CHRG:
728 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
729 break;
730 case SGM4154x_NOT_CHRGING:
731 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
732 break;
733 default:
734 val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
735 }
736 break;
737 case POWER_SUPPLY_PROP_MANUFACTURER:
738 val->strval = SGM4154x_MANUFACTURER;
739 break;
740
741 case POWER_SUPPLY_PROP_MODEL_NAME:
742 val->strval = SGM4154x_NAME;
743 break;
744
745 case POWER_SUPPLY_PROP_ONLINE:
746 val->intval = state.online;
747 break;
748 case POWER_SUPPLY_PROP_PRESENT:
749 val->intval = state.vbus_gd;
750 break;
751 case POWER_SUPPLY_PROP_TYPE:
752 val->intval = POWER_SUPPLY_TYPE_USB;
753 break;
754 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
755 val->intval = sgm->init_data.max_vreg;
756 break;
757 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
758 val->intval = SGM4154x_ICHRG_I_MAX_uA;
759 break;
760 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
761 val->intval = 12 * 1000 * 1000;
762 break;
763
764 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
765 val->intval = sgm4154x_get_input_curr_lim(sgm);
766 if (val->intval < 0)
767 return -EINVAL;
768 break;
769 default:
770 return -EINVAL;
771 }
772
773 return ret;
774 }
775
registers_show(struct device * dev,struct device_attribute * attr,char * buf)776 static ssize_t registers_show(struct device *dev,
777 struct device_attribute *attr,
778 char *buf)
779 {
780 struct sgm4154x_device *sgm4154x = dev_get_drvdata(dev);
781 u8 tmpbuf[30];
782 int idx = 0;
783 u8 addr;
784 int val;
785 int len;
786 int ret;
787
788 for (addr = 0x0; addr <= SGM4154x_CHRG_CTRL_f; addr++) {
789 ret = regmap_read(sgm4154x->regmap, addr, &val);
790 if (ret == 0) {
791 len = snprintf(tmpbuf, 30,
792 "Reg[%.2X] = 0x%.2x\n",
793 addr,
794 val);
795 memcpy(&buf[idx], tmpbuf, len);
796 idx += len;
797 }
798 }
799
800 return idx;
801 }
802
registers_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)803 static ssize_t registers_store(struct device *dev,
804 struct device_attribute *attr,
805 const char *buf, size_t count)
806 {
807 struct sgm4154x_device *sgm4154x = dev_get_drvdata(dev);
808 unsigned int reg;
809 int ret;
810 int val;
811
812 ret = sscanf(buf, "%x %x", ®, &val);
813 if (ret == 2 && reg <= SGM4154x_CHRG_CTRL_f)
814 regmap_write(sgm4154x->regmap, (unsigned char)reg, val);
815
816 return count;
817 }
818 static DEVICE_ATTR_RW(registers);
819
sgm4154x_create_device_node(struct device * dev)820 static void sgm4154x_create_device_node(struct device *dev)
821 {
822 device_create_file(dev, &dev_attr_registers);
823 }
824
sgm4154x_irq_handler_thread(int irq,void * private)825 static irqreturn_t sgm4154x_irq_handler_thread(int irq, void *private)
826 {
827 struct sgm4154x_device *sgm4154x = private;
828 struct sgm4154x_state state;
829 int ret;
830 u8 addr;
831 int val;
832
833 for (addr = 0x0; addr <= SGM4154x_CHRG_CTRL_f; addr++) {
834 ret = regmap_read(sgm4154x->regmap, addr, &val);
835 if (ret)
836 dev_err(sgm4154x->dev, "read addr[0x%x] error!\n", addr);
837 DBG("[0x%x]: 0x%x\n", addr, val);
838 }
839 ret = sgm4154x_get_state(sgm4154x, &state);
840 if (ret) {
841 dev_err(sgm4154x->dev, "get state error!\n");
842 return IRQ_NONE;
843 }
844 sgm4154x->state = state;
845 if (state.vbus_gd) {
846 if (sgm4154x->input_current >= DEFAULT_INPUT_CURRENT) {
847 ret = sgm4154x_set_input_curr_lim(sgm4154x, sgm4154x->input_current);
848 if (ret) {
849 dev_err(sgm4154x->dev, "set input current error!\n");
850 return IRQ_NONE;
851 }
852 }
853 }
854 power_supply_changed(sgm4154x->charger);
855
856 return IRQ_HANDLED;
857 }
858
859 static enum power_supply_property sgm4154x_power_supply_props[] = {
860 POWER_SUPPLY_PROP_TYPE,
861 POWER_SUPPLY_PROP_MANUFACTURER,
862 POWER_SUPPLY_PROP_MODEL_NAME,
863 POWER_SUPPLY_PROP_STATUS,
864 POWER_SUPPLY_PROP_ONLINE,
865 POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT,
866 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
867 POWER_SUPPLY_PROP_CHARGE_TYPE,
868 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
869 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
870 POWER_SUPPLY_PROP_PRESENT
871 };
872
873 static char *sgm4154x_charger_supplied_to[] = {
874 "usb",
875 };
876
877 static struct power_supply_desc sgm4154x_power_supply_desc = {
878 .name = "sgm4154x-charger",
879 .type = POWER_SUPPLY_TYPE_USB,
880 .properties = sgm4154x_power_supply_props,
881 .num_properties = ARRAY_SIZE(sgm4154x_power_supply_props),
882 .get_property = sgm4154x_charger_get_property,
883 .set_property = sgm4154x_charger_set_property,
884 .property_is_writeable = sgm4154x_property_is_writeable,
885 };
886
sgm4154x_is_volatile_reg(struct device * dev,unsigned int reg)887 static bool sgm4154x_is_volatile_reg(struct device *dev, unsigned int reg)
888 {
889 switch (reg) {
890 case SGM4154x_CHRG_CTRL_0 ... SGM4154x_CHRG_CTRL_f:
891 return true;
892 default:
893 return false;
894 }
895 }
896
897 static const struct regmap_config sgm4154x_regmap_config = {
898 .reg_bits = 8,
899 .val_bits = 8,
900
901 .max_register = SGM4154x_CHRG_CTRL_f,
902
903 .cache_type = REGCACHE_RBTREE,
904 .volatile_reg = sgm4154x_is_volatile_reg,
905 };
906
sgm4154x_power_supply_init(struct sgm4154x_device * sgm,struct device * dev)907 static int sgm4154x_power_supply_init(struct sgm4154x_device *sgm,
908 struct device *dev)
909 {
910 struct power_supply_config psy_cfg = { .drv_data = sgm,
911 .of_node = dev->of_node, };
912
913 psy_cfg.supplied_to = sgm4154x_charger_supplied_to;
914 psy_cfg.num_supplicants = ARRAY_SIZE(sgm4154x_charger_supplied_to);
915 psy_cfg.of_node = dev->of_node;
916 sgm->charger = devm_power_supply_register(sgm->dev,
917 &sgm4154x_power_supply_desc,
918 &psy_cfg);
919 if (IS_ERR(sgm->charger))
920 return -EINVAL;
921
922 return 0;
923 }
924
sgm4154x_hw_init(struct sgm4154x_device * sgm)925 static int sgm4154x_hw_init(struct sgm4154x_device *sgm)
926 {
927 struct power_supply_battery_info bat_info = { };
928 int chrg_stat, ret = 0;
929
930 ret = power_supply_get_battery_info(sgm->charger, &bat_info);
931 if (ret) {
932 pr_info("sgm4154x: no battery information is supplied\n");
933 /*
934 * If no battery information is supplied, we should set
935 * default charge termination current to 120 mA, and default
936 * charge termination voltage to 4.35V.
937 */
938 bat_info.constant_charge_current_max_ua =
939 SGM4154x_ICHRG_I_DEF_uA;
940 bat_info.constant_charge_voltage_max_uv =
941 SGM4154x_VREG_V_DEF_uV;
942 bat_info.precharge_current_ua =
943 SGM4154x_PRECHRG_I_DEF_uA;
944 bat_info.charge_term_current_ua =
945 SGM4154x_TERMCHRG_I_DEF_uA;
946 sgm->init_data.max_ichg =
947 SGM4154x_ICHRG_I_MAX_uA;
948 sgm->init_data.max_vreg = SGM4154x_VREG_V_DEF_uV;
949 }
950 if (!bat_info.constant_charge_current_max_ua)
951 bat_info.constant_charge_current_max_ua =
952 SGM4154x_ICHRG_I_MAX_uA;
953 if (!bat_info.constant_charge_voltage_max_uv)
954 bat_info.constant_charge_voltage_max_uv =
955 SGM4154x_VREG_V_DEF_uV;
956 if (!bat_info.precharge_current_ua)
957 bat_info.precharge_current_ua =
958 SGM4154x_PRECHRG_I_DEF_uA;
959 if (!bat_info.charge_term_current_ua)
960 bat_info.charge_term_current_ua =
961 SGM4154x_TERMCHRG_I_DEF_uA;
962 if (!sgm->init_data.max_ichg)
963 sgm->init_data.max_ichg =
964 SGM4154x_ICHRG_I_MAX_uA;
965
966 if (bat_info.constant_charge_voltage_max_uv)
967 sgm->init_data.max_vreg = bat_info.constant_charge_voltage_max_uv;
968
969 ret = sgm4154x_set_watchdog_timer(sgm, 0);
970 if (ret)
971 goto err_out;
972
973
974 ret = sgm4154x_set_prechrg_curr(sgm, bat_info.precharge_current_ua);
975 if (ret)
976 goto err_out;
977
978 ret = sgm4154x_set_chrg_volt(sgm,
979 sgm->init_data.max_vreg);
980 if (ret)
981 goto err_out;
982
983 ret = sgm4154x_set_term_curr(sgm,
984 bat_info.charge_term_current_ua);
985 if (ret)
986 goto err_out;
987
988 ret = sgm4154x_set_input_volt_lim(sgm, sgm->init_data.vlim);
989 if (ret)
990 goto err_out;
991
992 ret = regmap_read(sgm->regmap, SGM4154x_CHRG_STAT, &chrg_stat);
993 if (ret) {
994 pr_err("read SGM4154x_CHRG_STAT fail\n");
995 goto err_out;
996 }
997
998 if (!(chrg_stat & SGM4154x_PG_STAT)) {
999 ret = sgm4154x_set_input_curr_lim(sgm, DEFAULT_INPUT_CURRENT);
1000 if (ret)
1001 goto err_out;
1002 ret = sgm4154x_set_ichrg_curr(sgm,
1003 bat_info.constant_charge_current_max_ua);
1004 if (ret)
1005 goto err_out;
1006
1007 ret = sgm4154x_disable_charger(sgm);
1008 if (ret)
1009 goto err_out;
1010 }
1011 ret = sgm4154x_set_vac_ovp(sgm);
1012 if (ret)
1013 goto err_out;
1014
1015 regmap_update_bits(sgm->regmap,
1016 SGM4154x_CHRG_CTRL_d,
1017 0x01,
1018 0x00);
1019
1020 regmap_update_bits(sgm->regmap,
1021 SGM4154x_CHRG_CTRL_a,
1022 SGM4154x_IINDPM_INT_MASK,
1023 SGM4154x_IINDPM_INT_DIS);
1024
1025 regmap_update_bits(sgm->regmap,
1026 SGM4154x_CHRG_CTRL_a,
1027 SGM4154x_VINDPM_INT_MASK,
1028 SGM4154x_VINDPM_INT_DIS);
1029
1030
1031 ret = sgm4154x_set_recharge_volt(sgm, 200);
1032 if (ret)
1033 goto err_out;
1034
1035 DBG("ichrg_curr:%d\n"
1036 "prechrg_curr:%d\n"
1037 "chrg_vol:%d\n"
1038 "term_curr:%d\n"
1039 "input_curr_lim:%d\n",
1040 bat_info.constant_charge_current_max_ua,
1041 bat_info.precharge_current_ua,
1042 bat_info.constant_charge_voltage_max_uv,
1043 bat_info.charge_term_current_ua,
1044 sgm->init_data.ilim);
1045
1046 return 0;
1047
1048 err_out:
1049 return ret;
1050 }
1051
sgm4154x_parse_dt(struct sgm4154x_device * sgm)1052 static int sgm4154x_parse_dt(struct sgm4154x_device *sgm)
1053 {
1054 int ret;
1055
1056 ret = device_property_read_u32(sgm->dev,
1057 "input-voltage-limit-microvolt",
1058 &sgm->init_data.vlim);
1059 if (ret)
1060 sgm->init_data.vlim = SGM4154x_VINDPM_DEF_uV;
1061
1062 if (sgm->init_data.vlim > SGM4154x_VINDPM_V_MAX_uV ||
1063 sgm->init_data.vlim < SGM4154x_VINDPM_V_MIN_uV)
1064 return -EINVAL;
1065
1066 ret = device_property_read_u32(sgm->dev,
1067 "input-current-limit-microamp",
1068 &sgm->init_data.ilim);
1069 if (ret)
1070 sgm->init_data.ilim = SGM4154x_IINDPM_DEF_uA;
1071
1072 if (sgm->init_data.ilim > SGM4154x_IINDPM_I_MAX_uA ||
1073 sgm->init_data.ilim < SGM4154x_IINDPM_I_MIN_uA)
1074 return -EINVAL;
1075
1076 return 0;
1077 }
1078
sgm4154x_set_otg_voltage(struct sgm4154x_device * sgm,int uv)1079 static int sgm4154x_set_otg_voltage(struct sgm4154x_device *sgm, int uv)
1080 {
1081 int ret = 0;
1082 int reg_val = -1;
1083 int i = 0;
1084
1085 while (i < 4) {
1086 if (uv == BOOST_VOLT_LIMIT[i]) {
1087 reg_val = i;
1088 break;
1089 }
1090 i++;
1091 }
1092 if (reg_val < 0)
1093 return reg_val;
1094 reg_val = reg_val << 4;
1095 ret = regmap_update_bits(sgm->regmap,
1096 SGM4154x_CHRG_CTRL_6,
1097 SGM4154x_BOOSTV,
1098 reg_val);
1099 if (ret) {
1100 dev_err(sgm->dev, "set otg voltage error!\n");
1101 return ret;
1102 }
1103
1104 return ret;
1105 }
1106
sgm4154x_set_otg_current(struct sgm4154x_device * sgm,int ua)1107 static int sgm4154x_set_otg_current(struct sgm4154x_device *sgm, int ua)
1108 {
1109 int ret = 0;
1110
1111 if (ua == BOOST_CURRENT_LIMIT[0]) {
1112 ret = regmap_update_bits(sgm->regmap,
1113 SGM4154x_CHRG_CTRL_2,
1114 SGM4154x_BOOST_LIM,
1115 0);
1116 if (ret) {
1117 dev_err(sgm->dev, "set boost current limit error!\n");
1118 return ret;
1119 }
1120 } else if (ua == BOOST_CURRENT_LIMIT[1]) {
1121 ret = regmap_update_bits(sgm->regmap,
1122 SGM4154x_CHRG_CTRL_2,
1123 SGM4154x_BOOST_LIM,
1124 BIT(7));
1125 if (ret) {
1126 dev_err(sgm->dev, "set boost current limit error!\n");
1127 return ret;
1128 }
1129 }
1130
1131 return ret;
1132 }
1133
sgm4154x_enable_vbus(struct regulator_dev * rdev)1134 static int sgm4154x_enable_vbus(struct regulator_dev *rdev)
1135 {
1136 struct sgm4154x_device *sgm = rdev_get_drvdata(rdev);
1137 int ret = 0;
1138
1139 ret = regmap_update_bits(sgm->regmap,
1140 SGM4154x_CHRG_CTRL_1,
1141 SGM4154x_OTG_EN,
1142 SGM4154x_OTG_EN);
1143 if (ret) {
1144 dev_err(sgm->dev, "set OTG enable error!\n");
1145 return ret;
1146 }
1147
1148 return ret;
1149 }
1150
sgm4154x_disable_vbus(struct regulator_dev * rdev)1151 static int sgm4154x_disable_vbus(struct regulator_dev *rdev)
1152 {
1153 struct sgm4154x_device *sgm = rdev_get_drvdata(rdev);
1154 int ret = 0;
1155
1156 ret = regmap_update_bits(sgm->regmap,
1157 SGM4154x_CHRG_CTRL_1,
1158 SGM4154x_OTG_EN,
1159 0);
1160 if (ret) {
1161 dev_err(sgm->dev, "set OTG disable error!\n");
1162 return ret;
1163 }
1164
1165 return ret;
1166 }
1167
sgm4154x_is_enabled_vbus(struct regulator_dev * rdev)1168 static int sgm4154x_is_enabled_vbus(struct regulator_dev *rdev)
1169 {
1170 struct sgm4154x_device *sgm = rdev_get_drvdata(rdev);
1171 int temp = 0;
1172 int ret = 0;
1173
1174 ret = regmap_read(sgm->regmap, SGM4154x_CHRG_CTRL_1, &temp);
1175 if (ret) {
1176 dev_err(sgm->dev, "get vbus status error!\n");
1177 return ret;
1178 }
1179
1180 return (temp & SGM4154x_OTG_EN) ? 1 : 0;
1181 }
1182
1183 static const struct regulator_ops sgm4154x_vbus_ops = {
1184 .enable = sgm4154x_enable_vbus,
1185 .disable = sgm4154x_disable_vbus,
1186 .is_enabled = sgm4154x_is_enabled_vbus,
1187 };
1188
1189 static struct regulator_desc sgm4154x_otg_rdesc = {
1190 .of_match = "otg-vbus",
1191 .name = "otg-vbus",
1192 .regulators_node = of_match_ptr("regulators"),
1193 .ops = &sgm4154x_vbus_ops,
1194 .owner = THIS_MODULE,
1195 .type = REGULATOR_VOLTAGE,
1196 .fixed_uV = 5000000,
1197 .n_voltages = 1,
1198 };
1199
sgm4154x_vbus_regulator_register(struct sgm4154x_device * sgm)1200 static int sgm4154x_vbus_regulator_register(struct sgm4154x_device *sgm)
1201 {
1202 struct device_node *np;
1203 struct regulator_config config = {};
1204 int ret = 0;
1205
1206 np = of_get_child_by_name(sgm->dev->of_node, "regulators");
1207 if (!np) {
1208 dev_warn(sgm->dev, "cannot find regulators node\n");
1209 return -ENXIO;
1210 }
1211
1212 /* otg regulator */
1213 config.dev = sgm->dev;
1214 config.driver_data = sgm;
1215 sgm->otg_rdev = devm_regulator_register(sgm->dev,
1216 &sgm4154x_otg_rdesc,
1217 &config);
1218 if (IS_ERR(sgm->otg_rdev))
1219 ret = PTR_ERR(sgm->otg_rdev);
1220
1221 return ret;
1222 }
1223
sgm4154x_suspend_notifier(struct notifier_block * nb,unsigned long event,void * dummy)1224 static int sgm4154x_suspend_notifier(struct notifier_block *nb,
1225 unsigned long event,
1226 void *dummy)
1227 {
1228 struct sgm4154x_device *sgm = container_of(nb, struct sgm4154x_device, pm_nb);
1229
1230 switch (event) {
1231
1232 case PM_SUSPEND_PREPARE:
1233 sgm->sgm4154x_suspend_flag = 1;
1234 return NOTIFY_OK;
1235
1236 case PM_POST_SUSPEND:
1237 sgm->sgm4154x_suspend_flag = 0;
1238 return NOTIFY_OK;
1239
1240 default:
1241 return NOTIFY_DONE;
1242 }
1243 }
1244
sgm4154x_hw_chipid_detect(struct sgm4154x_device * sgm)1245 static int sgm4154x_hw_chipid_detect(struct sgm4154x_device *sgm)
1246 {
1247 int ret = 0;
1248 int val = 0;
1249
1250 ret = regmap_read(sgm->regmap, SGM4154x_CHRG_CTRL_b, &val);
1251 if (ret < 0)
1252 return ret;
1253
1254 return val;
1255 }
1256
sgm_charger_work(struct work_struct * work)1257 static void sgm_charger_work(struct work_struct *work)
1258 {
1259 struct sgm4154x_device *sgm =
1260 container_of(work,
1261 struct sgm4154x_device,
1262 sgm_delay_work.work);
1263
1264 sgm4154x_watchdog_timer_reset(sgm);
1265 if (sgm->watchdog_enable)
1266 queue_delayed_work(sgm->sgm_monitor_wq,
1267 &sgm->sgm_delay_work,
1268 msecs_to_jiffies(1000 * 5));
1269 }
1270
sgm4154x_probe(struct i2c_client * client,const struct i2c_device_id * id)1271 static int sgm4154x_probe(struct i2c_client *client,
1272 const struct i2c_device_id *id)
1273 {
1274 struct device *dev = &client->dev;
1275 struct sgm4154x_device *sgm;
1276 int ret;
1277
1278 sgm = devm_kzalloc(dev, sizeof(*sgm), GFP_KERNEL);
1279 if (!sgm)
1280 return -ENOMEM;
1281
1282 sgm->client = client;
1283 sgm->dev = dev;
1284
1285 mutex_init(&sgm->lock);
1286
1287 strncpy(sgm->model_name, id->name, I2C_NAME_SIZE);
1288
1289 sgm->regmap = devm_regmap_init_i2c(client, &sgm4154x_regmap_config);
1290 if (IS_ERR(sgm->regmap)) {
1291 dev_err(dev, "Failed to allocate register map\n");
1292 return PTR_ERR(sgm->regmap);
1293 }
1294
1295 i2c_set_clientdata(client, sgm);
1296
1297 ret = sgm4154x_parse_dt(sgm);
1298 if (ret) {
1299 dev_err(dev, "Failed to read device tree properties%d\n", ret);
1300 return ret;
1301 }
1302
1303 ret = sgm4154x_hw_chipid_detect(sgm);
1304 if ((ret & SGM4154x_PN_MASK) != SGM4154x_PN_ID) {
1305 pr_info("[%s] device not found !\n", __func__);
1306 return ret;
1307 }
1308
1309 device_init_wakeup(dev, 1);
1310
1311 if (client->irq) {
1312 ret = devm_request_threaded_irq(dev, client->irq, NULL,
1313 sgm4154x_irq_handler_thread,
1314 IRQF_TRIGGER_FALLING |
1315 IRQF_ONESHOT,
1316 "sgm41542-irq", sgm);
1317 if (ret)
1318 goto error_out;
1319 enable_irq_wake(client->irq);
1320 }
1321
1322 sgm->pm_nb.notifier_call = sgm4154x_suspend_notifier;
1323 register_pm_notifier(&sgm->pm_nb);
1324
1325 ret = sgm4154x_power_supply_init(sgm, dev);
1326 if (ret) {
1327 dev_err(dev, "Failed to register power supply\n");
1328 goto error_out;
1329 }
1330
1331 ret = sgm4154x_hw_init(sgm);
1332 if (ret) {
1333 dev_err(dev, "Cannot initialize the chip.\n");
1334 goto error_out;
1335 }
1336
1337 /* OTG setting 5V/1.2A */
1338 ret = sgm4154x_set_otg_voltage(sgm, 5000000);
1339 if (ret) {
1340 dev_err(sgm->dev, "set OTG voltage error!\n");
1341 return ret;
1342 }
1343
1344 ret = sgm4154x_set_otg_current(sgm, 1200000);
1345 if (ret) {
1346 dev_err(sgm->dev, "set OTG current error!\n");
1347 return ret;
1348 }
1349
1350 sgm->sgm_monitor_wq = alloc_ordered_workqueue("%s",
1351 WQ_MEM_RECLAIM | WQ_FREEZABLE, "sgm-monitor-wq");
1352 INIT_DELAYED_WORK(&sgm->sgm_delay_work, sgm_charger_work);
1353
1354 sgm4154x_vbus_regulator_register(sgm);
1355 sgm4154x_create_device_node(sgm->dev);
1356 return ret;
1357 error_out:
1358
1359 return ret;
1360 }
1361
sgm4154x_charger_remove(struct i2c_client * client)1362 static int sgm4154x_charger_remove(struct i2c_client *client)
1363 {
1364 struct sgm4154x_device *sgm = i2c_get_clientdata(client);
1365
1366 destroy_workqueue(sgm->sgm_monitor_wq);
1367 regulator_unregister(sgm->otg_rdev);
1368 power_supply_unregister(sgm->charger);
1369 mutex_destroy(&sgm->lock);
1370
1371 return 0;
1372 }
1373
sgm4154x_charger_shutdown(struct i2c_client * client)1374 static void sgm4154x_charger_shutdown(struct i2c_client *client)
1375 {
1376 struct sgm4154x_device *sgm = i2c_get_clientdata(client);
1377 int ret = 0;
1378
1379 sgm4154x_set_prechrg_curr(sgm, SGM4154x_PRECHRG_I_DEF_uA);
1380 ret = sgm4154x_disable_charger(sgm);
1381 if (ret)
1382 pr_err("Failed to disable charger, ret = %d\n", ret);
1383 }
1384
1385 static const struct i2c_device_id sgm4154x_i2c_ids[] = {
1386 { "sgm41542", 0 },
1387 {},
1388 };
1389 MODULE_DEVICE_TABLE(i2c, sgm4154x_i2c_ids);
1390
1391 static const struct of_device_id sgm4154x_of_match[] = {
1392 { .compatible = "sgm,sgm41542", },
1393 { },
1394 };
1395 MODULE_DEVICE_TABLE(of, sgm4154x_of_match);
1396
1397 static struct i2c_driver sgm4154x_driver = {
1398 .driver = {
1399 .name = "sgm4154x-charger",
1400 .of_match_table = sgm4154x_of_match,
1401 },
1402 .probe = sgm4154x_probe,
1403 .remove = sgm4154x_charger_remove,
1404 .shutdown = sgm4154x_charger_shutdown,
1405 .id_table = sgm4154x_i2c_ids,
1406 };
1407 module_i2c_driver(sgm4154x_driver);
1408
1409 MODULE_AUTHOR("Xu Shengfei <xsf@rock-chips.com>");
1410 MODULE_DESCRIPTION("sgm4154x charger driver");
1411 MODULE_LICENSE("GPL");
1412