This source file includes following definitions.
- set_lpn_pin
- clear_lpn_pin
- pm2xxx_reg_read
- pm2xxx_reg_write
- pm2xxx_charging_enable_mngt
- pm2xxx_charging_disable_mngt
- pm2xxx_charger_batt_therm_mngt
- pm2xxx_charger_die_therm_mngt
- pm2xxx_charger_ovv_mngt
- pm2xxx_charger_wd_exp_mngt
- pm2xxx_charger_vbat_lsig_mngt
- pm2xxx_charger_bat_disc_mngt
- pm2xxx_charger_detection
- pm2xxx_charger_itv_pwr_plug_mngt
- pm2xxx_charger_itv_pwr_unplug_mngt
- pm2_int_reg0
- pm2_int_reg1
- pm2_int_reg2
- pm2_int_reg3
- pm2_int_reg4
- pm2_int_reg5
- pm2xxx_irq_int
- pm2xxx_charger_get_ac_cv
- pm2xxx_current_to_regval
- pm2xxx_voltage_to_regval
- pm2xxx_charger_update_charger_current
- pm2xxx_charger_ac_get_property
- pm2xxx_charging_init
- pm2xxx_charger_ac_en
- pm2xxx_charger_watchdog_kick
- pm2xxx_charger_ac_work
- pm2xxx_charger_check_hw_failure_work
- pm2xxx_charger_check_main_thermal_prot_work
- pm2xxx_wall_charger_resume
- pm2xxx_wall_charger_suspend
- pm2xxx_runtime_suspend
- pm2xxx_runtime_resume
- pm2xxx_wall_charger_probe
- pm2xxx_wall_charger_remove
- pm2xxx_charger_init
- pm2xxx_charger_exit
1
2
3
4
5
6
7
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/device.h>
11 #include <linux/interrupt.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <linux/platform_device.h>
15 #include <linux/power_supply.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/err.h>
18 #include <linux/i2c.h>
19 #include <linux/workqueue.h>
20 #include <linux/mfd/abx500/ab8500.h>
21 #include <linux/mfd/abx500/ab8500-bm.h>
22 #include <linux/mfd/abx500/ux500_chargalg.h>
23 #include <linux/pm2301_charger.h>
24 #include <linux/gpio.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/pm.h>
27
28 #include "pm2301_charger.h"
29
30 #define to_pm2xxx_charger_ac_device_info(x) container_of((x), \
31 struct pm2xxx_charger, ac_chg)
32 #define SLEEP_MIN 50
33 #define SLEEP_MAX 100
34 #define PM2XXX_AUTOSUSPEND_DELAY 500
35
36 static int pm2xxx_interrupt_registers[] = {
37 PM2XXX_REG_INT1,
38 PM2XXX_REG_INT2,
39 PM2XXX_REG_INT3,
40 PM2XXX_REG_INT4,
41 PM2XXX_REG_INT5,
42 PM2XXX_REG_INT6,
43 };
44
45 static enum power_supply_property pm2xxx_charger_ac_props[] = {
46 POWER_SUPPLY_PROP_HEALTH,
47 POWER_SUPPLY_PROP_PRESENT,
48 POWER_SUPPLY_PROP_ONLINE,
49 POWER_SUPPLY_PROP_VOLTAGE_AVG,
50 };
51
52 static int pm2xxx_charger_voltage_map[] = {
53 3500,
54 3525,
55 3550,
56 3575,
57 3600,
58 3625,
59 3650,
60 3675,
61 3700,
62 3725,
63 3750,
64 3775,
65 3800,
66 3825,
67 3850,
68 3875,
69 3900,
70 3925,
71 3950,
72 3975,
73 4000,
74 4025,
75 4050,
76 4075,
77 4100,
78 4125,
79 4150,
80 4175,
81 4200,
82 4225,
83 4250,
84 4275,
85 4300,
86 };
87
88 static int pm2xxx_charger_current_map[] = {
89 200,
90 200,
91 400,
92 600,
93 800,
94 1000,
95 1200,
96 1400,
97 1600,
98 1800,
99 2000,
100 2200,
101 2400,
102 2600,
103 2800,
104 3000,
105 };
106
107 static const struct i2c_device_id pm2xxx_ident[] = {
108 { "pm2301", 0 },
109 { }
110 };
111
112 static void set_lpn_pin(struct pm2xxx_charger *pm2)
113 {
114 if (!pm2->ac.charger_connected && gpio_is_valid(pm2->lpn_pin)) {
115 gpio_set_value(pm2->lpn_pin, 1);
116 usleep_range(SLEEP_MIN, SLEEP_MAX);
117 }
118 }
119
120 static void clear_lpn_pin(struct pm2xxx_charger *pm2)
121 {
122 if (!pm2->ac.charger_connected && gpio_is_valid(pm2->lpn_pin))
123 gpio_set_value(pm2->lpn_pin, 0);
124 }
125
126 static int pm2xxx_reg_read(struct pm2xxx_charger *pm2, int reg, u8 *val)
127 {
128 int ret;
129
130
131 pm_runtime_get_sync(pm2->dev);
132
133 ret = i2c_smbus_read_i2c_block_data(pm2->config.pm2xxx_i2c, reg,
134 1, val);
135 if (ret < 0)
136 dev_err(pm2->dev, "Error reading register at 0x%x\n", reg);
137 else
138 ret = 0;
139
140 pm_runtime_put_sync(pm2->dev);
141
142 return ret;
143 }
144
145 static int pm2xxx_reg_write(struct pm2xxx_charger *pm2, int reg, u8 val)
146 {
147 int ret;
148
149
150 pm_runtime_get_sync(pm2->dev);
151
152 ret = i2c_smbus_write_i2c_block_data(pm2->config.pm2xxx_i2c, reg,
153 1, &val);
154 if (ret < 0)
155 dev_err(pm2->dev, "Error writing register at 0x%x\n", reg);
156 else
157 ret = 0;
158
159 pm_runtime_put_sync(pm2->dev);
160
161 return ret;
162 }
163
164 static int pm2xxx_charging_enable_mngt(struct pm2xxx_charger *pm2)
165 {
166 int ret;
167
168
169 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG2,
170 (PM2XXX_CH_AUTO_RESUME_EN | PM2XXX_CHARGER_ENA));
171
172 return ret;
173 }
174
175 static int pm2xxx_charging_disable_mngt(struct pm2xxx_charger *pm2)
176 {
177 int ret;
178
179
180 ret = pm2xxx_reg_write(pm2, PM2XXX_SW_CTRL_REG, PM2XXX_SWCTRL_HW);
181 if (ret < 0) {
182 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
183 return ret;
184 }
185
186
187 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG2,
188 (PM2XXX_CH_AUTO_RESUME_DIS | PM2XXX_CHARGER_DIS));
189 if (ret < 0) {
190 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
191 return ret;
192 }
193
194 return 0;
195 }
196
197 static int pm2xxx_charger_batt_therm_mngt(struct pm2xxx_charger *pm2, int val)
198 {
199 queue_work(pm2->charger_wq, &pm2->check_main_thermal_prot_work);
200
201 return 0;
202 }
203
204
205 static int pm2xxx_charger_die_therm_mngt(struct pm2xxx_charger *pm2, int val)
206 {
207 queue_work(pm2->charger_wq, &pm2->check_main_thermal_prot_work);
208
209 return 0;
210 }
211
212 static int pm2xxx_charger_ovv_mngt(struct pm2xxx_charger *pm2, int val)
213 {
214 dev_err(pm2->dev, "Overvoltage detected\n");
215 pm2->flags.ovv = true;
216 power_supply_changed(pm2->ac_chg.psy);
217
218
219 queue_delayed_work(pm2->charger_wq, &pm2->check_hw_failure_work, 0);
220
221 return 0;
222 }
223
224 static int pm2xxx_charger_wd_exp_mngt(struct pm2xxx_charger *pm2, int val)
225 {
226 dev_dbg(pm2->dev , "20 minutes watchdog expired\n");
227
228 pm2->ac.wd_expired = true;
229 power_supply_changed(pm2->ac_chg.psy);
230
231 return 0;
232 }
233
234 static int pm2xxx_charger_vbat_lsig_mngt(struct pm2xxx_charger *pm2, int val)
235 {
236 int ret;
237
238 switch (val) {
239 case PM2XXX_INT1_ITVBATLOWR:
240 dev_dbg(pm2->dev, "VBAT grows above VBAT_LOW level\n");
241
242 ret = pm2xxx_reg_write(pm2, PM2XXX_SW_CTRL_REG,
243 PM2XXX_SWCTRL_SW);
244 if (ret < 0) {
245 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
246 return ret;
247 }
248 break;
249
250 case PM2XXX_INT1_ITVBATLOWF:
251 dev_dbg(pm2->dev, "VBAT drops below VBAT_LOW level\n");
252
253 ret = pm2xxx_reg_write(pm2, PM2XXX_SW_CTRL_REG,
254 PM2XXX_SWCTRL_HW);
255 if (ret < 0) {
256 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
257 return ret;
258 }
259 break;
260
261 default:
262 dev_err(pm2->dev, "Unknown VBAT level\n");
263 }
264
265 return 0;
266 }
267
268 static int pm2xxx_charger_bat_disc_mngt(struct pm2xxx_charger *pm2, int val)
269 {
270 dev_dbg(pm2->dev, "battery disconnected\n");
271
272 return 0;
273 }
274
275 static int pm2xxx_charger_detection(struct pm2xxx_charger *pm2, u8 *val)
276 {
277 int ret;
278
279 ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT2, val);
280
281 if (ret < 0) {
282 dev_err(pm2->dev, "Charger detection failed\n");
283 goto out;
284 }
285
286 *val &= (PM2XXX_INT2_S_ITVPWR1PLUG | PM2XXX_INT2_S_ITVPWR2PLUG);
287
288 out:
289 return ret;
290 }
291
292 static int pm2xxx_charger_itv_pwr_plug_mngt(struct pm2xxx_charger *pm2, int val)
293 {
294
295 int ret;
296 u8 read_val;
297
298
299
300
301
302
303 ret = pm2xxx_charger_detection(pm2, &read_val);
304
305 if ((ret == 0) && read_val) {
306 pm2->ac.charger_connected = 1;
307 pm2->ac_conn = true;
308 queue_work(pm2->charger_wq, &pm2->ac_work);
309 }
310
311
312 return ret;
313 }
314
315 static int pm2xxx_charger_itv_pwr_unplug_mngt(struct pm2xxx_charger *pm2,
316 int val)
317 {
318 pm2->ac.charger_connected = 0;
319 queue_work(pm2->charger_wq, &pm2->ac_work);
320
321 return 0;
322 }
323
324 static int pm2_int_reg0(void *pm2_data, int val)
325 {
326 struct pm2xxx_charger *pm2 = pm2_data;
327 int ret = 0;
328
329 if (val & PM2XXX_INT1_ITVBATLOWR) {
330 ret = pm2xxx_charger_vbat_lsig_mngt(pm2,
331 PM2XXX_INT1_ITVBATLOWR);
332 if (ret < 0)
333 goto out;
334 }
335
336 if (val & PM2XXX_INT1_ITVBATLOWF) {
337 ret = pm2xxx_charger_vbat_lsig_mngt(pm2,
338 PM2XXX_INT1_ITVBATLOWF);
339 if (ret < 0)
340 goto out;
341 }
342
343 if (val & PM2XXX_INT1_ITVBATDISCONNECT) {
344 ret = pm2xxx_charger_bat_disc_mngt(pm2,
345 PM2XXX_INT1_ITVBATDISCONNECT);
346 if (ret < 0)
347 goto out;
348 }
349 out:
350 return ret;
351 }
352
353 static int pm2_int_reg1(void *pm2_data, int val)
354 {
355 struct pm2xxx_charger *pm2 = pm2_data;
356 int ret = 0;
357
358 if (val & (PM2XXX_INT2_ITVPWR1PLUG | PM2XXX_INT2_ITVPWR2PLUG)) {
359 dev_dbg(pm2->dev , "Main charger plugged\n");
360 ret = pm2xxx_charger_itv_pwr_plug_mngt(pm2, val &
361 (PM2XXX_INT2_ITVPWR1PLUG | PM2XXX_INT2_ITVPWR2PLUG));
362 }
363
364 if (val &
365 (PM2XXX_INT2_ITVPWR1UNPLUG | PM2XXX_INT2_ITVPWR2UNPLUG)) {
366 dev_dbg(pm2->dev , "Main charger unplugged\n");
367 ret = pm2xxx_charger_itv_pwr_unplug_mngt(pm2, val &
368 (PM2XXX_INT2_ITVPWR1UNPLUG |
369 PM2XXX_INT2_ITVPWR2UNPLUG));
370 }
371
372 return ret;
373 }
374
375 static int pm2_int_reg2(void *pm2_data, int val)
376 {
377 struct pm2xxx_charger *pm2 = pm2_data;
378 int ret = 0;
379
380 if (val & PM2XXX_INT3_ITAUTOTIMEOUTWD)
381 ret = pm2xxx_charger_wd_exp_mngt(pm2, val);
382
383 if (val & (PM2XXX_INT3_ITCHPRECHARGEWD |
384 PM2XXX_INT3_ITCHCCWD | PM2XXX_INT3_ITCHCVWD)) {
385 dev_dbg(pm2->dev,
386 "Watchdog occurred for precharge, CC and CV charge\n");
387 }
388
389 return ret;
390 }
391
392 static int pm2_int_reg3(void *pm2_data, int val)
393 {
394 struct pm2xxx_charger *pm2 = pm2_data;
395 int ret = 0;
396
397 if (val & (PM2XXX_INT4_ITCHARGINGON)) {
398 dev_dbg(pm2->dev ,
399 "chargind operation has started\n");
400 }
401
402 if (val & (PM2XXX_INT4_ITVRESUME)) {
403 dev_dbg(pm2->dev,
404 "battery discharged down to VResume threshold\n");
405 }
406
407 if (val & (PM2XXX_INT4_ITBATTFULL)) {
408 dev_dbg(pm2->dev , "battery fully detected\n");
409 }
410
411 if (val & (PM2XXX_INT4_ITCVPHASE)) {
412 dev_dbg(pm2->dev, "CV phase enter with 0.5C charging\n");
413 }
414
415 if (val & (PM2XXX_INT4_ITVPWR2OVV | PM2XXX_INT4_ITVPWR1OVV)) {
416 pm2->failure_case = VPWR_OVV;
417 ret = pm2xxx_charger_ovv_mngt(pm2, val &
418 (PM2XXX_INT4_ITVPWR2OVV | PM2XXX_INT4_ITVPWR1OVV));
419 dev_dbg(pm2->dev, "VPWR/VSYSTEM overvoltage detected\n");
420 }
421
422 if (val & (PM2XXX_INT4_S_ITBATTEMPCOLD |
423 PM2XXX_INT4_S_ITBATTEMPHOT)) {
424 ret = pm2xxx_charger_batt_therm_mngt(pm2, val &
425 (PM2XXX_INT4_S_ITBATTEMPCOLD |
426 PM2XXX_INT4_S_ITBATTEMPHOT));
427 dev_dbg(pm2->dev, "BTEMP is too Low/High\n");
428 }
429
430 return ret;
431 }
432
433 static int pm2_int_reg4(void *pm2_data, int val)
434 {
435 struct pm2xxx_charger *pm2 = pm2_data;
436 int ret = 0;
437
438 if (val & PM2XXX_INT5_ITVSYSTEMOVV) {
439 pm2->failure_case = VSYSTEM_OVV;
440 ret = pm2xxx_charger_ovv_mngt(pm2, val &
441 PM2XXX_INT5_ITVSYSTEMOVV);
442 dev_dbg(pm2->dev, "VSYSTEM overvoltage detected\n");
443 }
444
445 if (val & (PM2XXX_INT5_ITTHERMALWARNINGFALL |
446 PM2XXX_INT5_ITTHERMALWARNINGRISE |
447 PM2XXX_INT5_ITTHERMALSHUTDOWNFALL |
448 PM2XXX_INT5_ITTHERMALSHUTDOWNRISE)) {
449 dev_dbg(pm2->dev, "BTEMP die temperature is too Low/High\n");
450 ret = pm2xxx_charger_die_therm_mngt(pm2, val &
451 (PM2XXX_INT5_ITTHERMALWARNINGFALL |
452 PM2XXX_INT5_ITTHERMALWARNINGRISE |
453 PM2XXX_INT5_ITTHERMALSHUTDOWNFALL |
454 PM2XXX_INT5_ITTHERMALSHUTDOWNRISE));
455 }
456
457 return ret;
458 }
459
460 static int pm2_int_reg5(void *pm2_data, int val)
461 {
462 struct pm2xxx_charger *pm2 = pm2_data;
463 int ret = 0;
464
465 if (val & (PM2XXX_INT6_ITVPWR2DROP | PM2XXX_INT6_ITVPWR1DROP)) {
466 dev_dbg(pm2->dev, "VMPWR drop to VBAT level\n");
467 }
468
469 if (val & (PM2XXX_INT6_ITVPWR2VALIDRISE |
470 PM2XXX_INT6_ITVPWR1VALIDRISE |
471 PM2XXX_INT6_ITVPWR2VALIDFALL |
472 PM2XXX_INT6_ITVPWR1VALIDFALL)) {
473 dev_dbg(pm2->dev, "Falling/Rising edge on WPWR1/2\n");
474 }
475
476 return ret;
477 }
478
479 static irqreturn_t pm2xxx_irq_int(int irq, void *data)
480 {
481 struct pm2xxx_charger *pm2 = data;
482 struct pm2xxx_interrupts *interrupt = pm2->pm2_int;
483 int i;
484
485
486 pm_runtime_get_sync(pm2->dev);
487
488 do {
489 for (i = 0; i < PM2XXX_NUM_INT_REG; i++) {
490 pm2xxx_reg_read(pm2,
491 pm2xxx_interrupt_registers[i],
492 &(interrupt->reg[i]));
493
494 if (interrupt->reg[i] > 0)
495 interrupt->handler[i](pm2, interrupt->reg[i]);
496 }
497 } while (gpio_get_value(pm2->pdata->gpio_irq_number) == 0);
498
499 pm_runtime_mark_last_busy(pm2->dev);
500 pm_runtime_put_autosuspend(pm2->dev);
501
502 return IRQ_HANDLED;
503 }
504
505 static int pm2xxx_charger_get_ac_cv(struct pm2xxx_charger *pm2)
506 {
507 int ret = 0;
508 u8 val;
509
510 if (pm2->ac.charger_connected && pm2->ac.charger_online) {
511
512 ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT4, &val);
513 if (ret < 0) {
514 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
515 goto out;
516 }
517
518 if (val & PM2XXX_INT4_S_ITCVPHASE)
519 ret = PM2XXX_CONST_VOLT;
520 else
521 ret = PM2XXX_CONST_CURR;
522 }
523 out:
524 return ret;
525 }
526
527 static int pm2xxx_current_to_regval(int curr)
528 {
529 int i;
530
531 if (curr < pm2xxx_charger_current_map[0])
532 return 0;
533
534 for (i = 1; i < ARRAY_SIZE(pm2xxx_charger_current_map); i++) {
535 if (curr < pm2xxx_charger_current_map[i])
536 return (i - 1);
537 }
538
539 i = ARRAY_SIZE(pm2xxx_charger_current_map) - 1;
540 if (curr == pm2xxx_charger_current_map[i])
541 return i;
542 else
543 return -EINVAL;
544 }
545
546 static int pm2xxx_voltage_to_regval(int curr)
547 {
548 int i;
549
550 if (curr < pm2xxx_charger_voltage_map[0])
551 return 0;
552
553 for (i = 1; i < ARRAY_SIZE(pm2xxx_charger_voltage_map); i++) {
554 if (curr < pm2xxx_charger_voltage_map[i])
555 return i - 1;
556 }
557
558 i = ARRAY_SIZE(pm2xxx_charger_voltage_map) - 1;
559 if (curr == pm2xxx_charger_voltage_map[i])
560 return i;
561 else
562 return -EINVAL;
563 }
564
565 static int pm2xxx_charger_update_charger_current(struct ux500_charger *charger,
566 int ich_out)
567 {
568 int ret;
569 int curr_index;
570 struct pm2xxx_charger *pm2;
571 u8 val;
572
573 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
574 pm2 = to_pm2xxx_charger_ac_device_info(charger);
575 else
576 return -ENXIO;
577
578 curr_index = pm2xxx_current_to_regval(ich_out);
579 if (curr_index < 0) {
580 dev_err(pm2->dev,
581 "Charger current too high, charging not started\n");
582 return -ENXIO;
583 }
584
585 ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG6, &val);
586 if (ret >= 0) {
587 val &= ~PM2XXX_DIR_CH_CC_CURRENT_MASK;
588 val |= curr_index;
589 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, val);
590 if (ret < 0) {
591 dev_err(pm2->dev,
592 "%s write failed\n", __func__);
593 }
594 }
595 else
596 dev_err(pm2->dev, "%s read failed\n", __func__);
597
598 return ret;
599 }
600
601 static int pm2xxx_charger_ac_get_property(struct power_supply *psy,
602 enum power_supply_property psp,
603 union power_supply_propval *val)
604 {
605 struct pm2xxx_charger *pm2;
606
607 pm2 = to_pm2xxx_charger_ac_device_info(psy_to_ux500_charger(psy));
608
609 switch (psp) {
610 case POWER_SUPPLY_PROP_HEALTH:
611 if (pm2->flags.mainextchnotok)
612 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
613 else if (pm2->ac.wd_expired)
614 val->intval = POWER_SUPPLY_HEALTH_DEAD;
615 else if (pm2->flags.main_thermal_prot)
616 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
617 else if (pm2->flags.ovv)
618 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
619 else
620 val->intval = POWER_SUPPLY_HEALTH_GOOD;
621 break;
622 case POWER_SUPPLY_PROP_ONLINE:
623 val->intval = pm2->ac.charger_online;
624 break;
625 case POWER_SUPPLY_PROP_PRESENT:
626 val->intval = pm2->ac.charger_connected;
627 break;
628 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
629 pm2->ac.cv_active = pm2xxx_charger_get_ac_cv(pm2);
630 val->intval = pm2->ac.cv_active;
631 break;
632 default:
633 return -EINVAL;
634 }
635 return 0;
636 }
637
638 static int pm2xxx_charging_init(struct pm2xxx_charger *pm2)
639 {
640 int ret = 0;
641
642
643 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG3,
644 (PM2XXX_CH_WD_CV_PHASE_60MIN | PM2XXX_CH_WD_CC_PHASE_60MIN));
645 if( ret < 0)
646 return ret;
647
648
649 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG4,
650 PM2XXX_CH_WD_PRECH_PHASE_60MIN);
651
652
653 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG5,
654 PM2XXX_CH_WD_AUTO_TIMEOUT_20MIN);
655
656
657
658
659
660
661 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6,
662 (PM2XXX_DIR_CH_CC_CURRENT_1000MA |
663 PM2XXX_CH_PRECH_CURRENT_100MA |
664 PM2XXX_CH_EOC_CURRENT_100MA));
665
666
667
668
669
670 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG7,
671 (PM2XXX_CH_PRECH_VOL_2_9 | PM2XXX_CH_VRESUME_VOL_3_8));
672
673
674 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG8,
675 PM2XXX_CH_VOLT_4_2);
676
677
678 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG9,
679 (PM2XXX_CH_150MV_DROP_300MV | PM2XXX_CHARCHING_INFO_DIS |
680 PM2XXX_CH_CC_REDUCED_CURRENT_IDENT |
681 PM2XXX_CH_CC_MODEDROP_DIS));
682
683
684 ret = pm2xxx_reg_write(pm2, PM2XXX_INP_VOLT_VPWR2,
685 PM2XXX_VPWR2_OVV_10);
686 ret = pm2xxx_reg_write(pm2, PM2XXX_INP_VOLT_VPWR1,
687 PM2XXX_VPWR1_OVV_10);
688
689
690 ret = pm2xxx_reg_write(pm2, PM2XXX_INP_DROP_VPWR2,
691 (PM2XXX_VPWR2_HW_OPT_DIS | PM2XXX_VPWR2_VALID_DIS |
692 PM2XXX_VPWR2_DROP_DIS));
693 ret = pm2xxx_reg_write(pm2, PM2XXX_INP_DROP_VPWR1,
694 (PM2XXX_VPWR1_HW_OPT_DIS | PM2XXX_VPWR1_VALID_DIS |
695 PM2XXX_VPWR1_DROP_DIS));
696
697
698 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_LOW_LEV_COMP_REG,
699 PM2XXX_VBAT_LOW_MONITORING_ENA);
700
701 return ret;
702 }
703
704 static int pm2xxx_charger_ac_en(struct ux500_charger *charger,
705 int enable, int vset, int iset)
706 {
707 int ret;
708 int volt_index;
709 int curr_index;
710 u8 val;
711
712 struct pm2xxx_charger *pm2 = to_pm2xxx_charger_ac_device_info(charger);
713
714 if (enable) {
715 if (!pm2->ac.charger_connected) {
716 dev_dbg(pm2->dev, "AC charger not connected\n");
717 return -ENXIO;
718 }
719
720 dev_dbg(pm2->dev, "Enable AC: %dmV %dmA\n", vset, iset);
721 if (!pm2->vddadc_en_ac) {
722 ret = regulator_enable(pm2->regu);
723 if (ret)
724 dev_warn(pm2->dev,
725 "Failed to enable vddadc regulator\n");
726 else
727 pm2->vddadc_en_ac = true;
728 }
729
730 ret = pm2xxx_charging_init(pm2);
731 if (ret < 0) {
732 dev_err(pm2->dev, "%s charging init failed\n",
733 __func__);
734 goto error_occured;
735 }
736
737 volt_index = pm2xxx_voltage_to_regval(vset);
738 curr_index = pm2xxx_current_to_regval(iset);
739
740 if (volt_index < 0 || curr_index < 0) {
741 dev_err(pm2->dev,
742 "Charger voltage or current too high, "
743 "charging not started\n");
744 return -ENXIO;
745 }
746
747 ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG8, &val);
748 if (ret < 0) {
749 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
750 goto error_occured;
751 }
752 val &= ~PM2XXX_CH_VOLT_MASK;
753 val |= volt_index;
754 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG8, val);
755 if (ret < 0) {
756 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
757 goto error_occured;
758 }
759
760 ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG6, &val);
761 if (ret < 0) {
762 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
763 goto error_occured;
764 }
765 val &= ~PM2XXX_DIR_CH_CC_CURRENT_MASK;
766 val |= curr_index;
767 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, val);
768 if (ret < 0) {
769 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
770 goto error_occured;
771 }
772
773 if (!pm2->bat->enable_overshoot) {
774 ret = pm2xxx_reg_read(pm2, PM2XXX_LED_CTRL_REG, &val);
775 if (ret < 0) {
776 dev_err(pm2->dev, "%s pm2xxx read failed\n",
777 __func__);
778 goto error_occured;
779 }
780 val |= PM2XXX_ANTI_OVERSHOOT_EN;
781 ret = pm2xxx_reg_write(pm2, PM2XXX_LED_CTRL_REG, val);
782 if (ret < 0) {
783 dev_err(pm2->dev, "%s pm2xxx write failed\n",
784 __func__);
785 goto error_occured;
786 }
787 }
788
789 ret = pm2xxx_charging_enable_mngt(pm2);
790 if (ret < 0) {
791 dev_err(pm2->dev, "Failed to enable"
792 "pm2xxx ac charger\n");
793 goto error_occured;
794 }
795
796 pm2->ac.charger_online = 1;
797 } else {
798 pm2->ac.charger_online = 0;
799 pm2->ac.wd_expired = false;
800
801
802 if (pm2->vddadc_en_ac) {
803 regulator_disable(pm2->regu);
804 pm2->vddadc_en_ac = false;
805 }
806
807 ret = pm2xxx_charging_disable_mngt(pm2);
808 if (ret < 0) {
809 dev_err(pm2->dev, "failed to disable"
810 "pm2xxx ac charger\n");
811 goto error_occured;
812 }
813
814 dev_dbg(pm2->dev, "PM2301: " "Disabled AC charging\n");
815 }
816 power_supply_changed(pm2->ac_chg.psy);
817
818 error_occured:
819 return ret;
820 }
821
822 static int pm2xxx_charger_watchdog_kick(struct ux500_charger *charger)
823 {
824 int ret;
825 struct pm2xxx_charger *pm2;
826
827 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
828 pm2 = to_pm2xxx_charger_ac_device_info(charger);
829 else
830 return -ENXIO;
831
832 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_WD_KICK, WD_TIMER);
833 if (ret)
834 dev_err(pm2->dev, "Failed to kick WD!\n");
835
836 return ret;
837 }
838
839 static void pm2xxx_charger_ac_work(struct work_struct *work)
840 {
841 struct pm2xxx_charger *pm2 = container_of(work,
842 struct pm2xxx_charger, ac_work);
843
844
845 power_supply_changed(pm2->ac_chg.psy);
846 sysfs_notify(&pm2->ac_chg.psy->dev.kobj, NULL, "present");
847 };
848
849 static void pm2xxx_charger_check_hw_failure_work(struct work_struct *work)
850 {
851 u8 reg_value;
852
853 struct pm2xxx_charger *pm2 = container_of(work,
854 struct pm2xxx_charger, check_hw_failure_work.work);
855
856 if (pm2->flags.ovv) {
857 pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT4, ®_value);
858
859 if (!(reg_value & (PM2XXX_INT4_S_ITVPWR1OVV |
860 PM2XXX_INT4_S_ITVPWR2OVV))) {
861 pm2->flags.ovv = false;
862 power_supply_changed(pm2->ac_chg.psy);
863 }
864 }
865
866
867 if (pm2->flags.ovv) {
868 queue_delayed_work(pm2->charger_wq,
869 &pm2->check_hw_failure_work, round_jiffies(HZ));
870 }
871 }
872
873 static void pm2xxx_charger_check_main_thermal_prot_work(
874 struct work_struct *work)
875 {
876 int ret;
877 u8 val;
878
879 struct pm2xxx_charger *pm2 = container_of(work, struct pm2xxx_charger,
880 check_main_thermal_prot_work);
881
882
883 ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT5, &val);
884 if (ret < 0) {
885 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
886 return;
887 }
888 if (val & (PM2XXX_INT5_S_ITTHERMALWARNINGRISE
889 | PM2XXX_INT5_S_ITTHERMALSHUTDOWNRISE))
890 pm2->flags.main_thermal_prot = true;
891 else if (val & (PM2XXX_INT5_S_ITTHERMALWARNINGFALL
892 | PM2XXX_INT5_S_ITTHERMALSHUTDOWNFALL))
893 pm2->flags.main_thermal_prot = false;
894
895 power_supply_changed(pm2->ac_chg.psy);
896 }
897
898 static struct pm2xxx_interrupts pm2xxx_int = {
899 .handler[0] = pm2_int_reg0,
900 .handler[1] = pm2_int_reg1,
901 .handler[2] = pm2_int_reg2,
902 .handler[3] = pm2_int_reg3,
903 .handler[4] = pm2_int_reg4,
904 .handler[5] = pm2_int_reg5,
905 };
906
907 static struct pm2xxx_irq pm2xxx_charger_irq[] = {
908 {"PM2XXX_IRQ_INT", pm2xxx_irq_int},
909 };
910
911 static int __maybe_unused pm2xxx_wall_charger_resume(struct device *dev)
912 {
913 struct i2c_client *i2c_client = to_i2c_client(dev);
914 struct pm2xxx_charger *pm2;
915
916 pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(i2c_client);
917 set_lpn_pin(pm2);
918
919
920 if (pm2->flags.ovv)
921 queue_delayed_work(pm2->charger_wq,
922 &pm2->check_hw_failure_work, 0);
923
924 return 0;
925 }
926
927 static int __maybe_unused pm2xxx_wall_charger_suspend(struct device *dev)
928 {
929 struct i2c_client *i2c_client = to_i2c_client(dev);
930 struct pm2xxx_charger *pm2;
931
932 pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(i2c_client);
933 clear_lpn_pin(pm2);
934
935
936 if (delayed_work_pending(&pm2->check_hw_failure_work))
937 cancel_delayed_work(&pm2->check_hw_failure_work);
938
939 flush_work(&pm2->ac_work);
940 flush_work(&pm2->check_main_thermal_prot_work);
941
942 return 0;
943 }
944
945 static int __maybe_unused pm2xxx_runtime_suspend(struct device *dev)
946 {
947 struct i2c_client *pm2xxx_i2c_client = to_i2c_client(dev);
948 struct pm2xxx_charger *pm2;
949
950 pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(pm2xxx_i2c_client);
951 clear_lpn_pin(pm2);
952
953 return 0;
954 }
955
956 static int __maybe_unused pm2xxx_runtime_resume(struct device *dev)
957 {
958 struct i2c_client *pm2xxx_i2c_client = to_i2c_client(dev);
959 struct pm2xxx_charger *pm2;
960
961 pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(pm2xxx_i2c_client);
962
963 if (gpio_is_valid(pm2->lpn_pin) && gpio_get_value(pm2->lpn_pin) == 0)
964 set_lpn_pin(pm2);
965
966 return 0;
967 }
968
969 static const struct dev_pm_ops pm2xxx_pm_ops __maybe_unused = {
970 SET_SYSTEM_SLEEP_PM_OPS(pm2xxx_wall_charger_suspend,
971 pm2xxx_wall_charger_resume)
972 SET_RUNTIME_PM_OPS(pm2xxx_runtime_suspend, pm2xxx_runtime_resume, NULL)
973 };
974
975 static int pm2xxx_wall_charger_probe(struct i2c_client *i2c_client,
976 const struct i2c_device_id *id)
977 {
978 struct pm2xxx_platform_data *pl_data = i2c_client->dev.platform_data;
979 struct power_supply_config psy_cfg = {};
980 struct pm2xxx_charger *pm2;
981 int ret = 0;
982 u8 val;
983 int i;
984
985 if (!pl_data) {
986 dev_err(&i2c_client->dev, "No platform data supplied\n");
987 return -EINVAL;
988 }
989
990 pm2 = kzalloc(sizeof(struct pm2xxx_charger), GFP_KERNEL);
991 if (!pm2) {
992 dev_err(&i2c_client->dev, "pm2xxx_charger allocation failed\n");
993 return -ENOMEM;
994 }
995
996
997 pm2->dev = &i2c_client->dev;
998
999 pm2->pm2_int = &pm2xxx_int;
1000
1001
1002 if (!pl_data->wall_charger) {
1003 dev_err(pm2->dev, "no charger platform data supplied\n");
1004 ret = -EINVAL;
1005 goto free_device_info;
1006 }
1007
1008 pm2->pdata = pl_data->wall_charger;
1009
1010
1011 if (!pl_data->battery) {
1012 dev_err(pm2->dev, "no battery platform data supplied\n");
1013 ret = -EINVAL;
1014 goto free_device_info;
1015 }
1016
1017 pm2->bat = pl_data->battery;
1018
1019 if (!i2c_check_functionality(i2c_client->adapter,
1020 I2C_FUNC_SMBUS_BYTE_DATA |
1021 I2C_FUNC_SMBUS_READ_WORD_DATA)) {
1022 ret = -ENODEV;
1023 dev_info(pm2->dev, "pm2301 i2c_check_functionality failed\n");
1024 goto free_device_info;
1025 }
1026
1027 pm2->config.pm2xxx_i2c = i2c_client;
1028 pm2->config.pm2xxx_id = (struct i2c_device_id *) id;
1029 i2c_set_clientdata(i2c_client, pm2);
1030
1031
1032
1033 pm2->ac_chg_desc.name = pm2->pdata->label;
1034 pm2->ac_chg_desc.type = POWER_SUPPLY_TYPE_MAINS;
1035 pm2->ac_chg_desc.properties = pm2xxx_charger_ac_props;
1036 pm2->ac_chg_desc.num_properties = ARRAY_SIZE(pm2xxx_charger_ac_props);
1037 pm2->ac_chg_desc.get_property = pm2xxx_charger_ac_get_property;
1038
1039 psy_cfg.supplied_to = pm2->pdata->supplied_to;
1040 psy_cfg.num_supplicants = pm2->pdata->num_supplicants;
1041
1042 pm2->ac_chg.ops.enable = &pm2xxx_charger_ac_en;
1043 pm2->ac_chg.ops.kick_wd = &pm2xxx_charger_watchdog_kick;
1044 pm2->ac_chg.ops.update_curr = &pm2xxx_charger_update_charger_current;
1045 pm2->ac_chg.max_out_volt = pm2xxx_charger_voltage_map[
1046 ARRAY_SIZE(pm2xxx_charger_voltage_map) - 1];
1047 pm2->ac_chg.max_out_curr = pm2xxx_charger_current_map[
1048 ARRAY_SIZE(pm2xxx_charger_current_map) - 1];
1049 pm2->ac_chg.wdt_refresh = WD_KICK_INTERVAL;
1050 pm2->ac_chg.enabled = true;
1051 pm2->ac_chg.external = true;
1052
1053
1054 pm2->charger_wq = alloc_ordered_workqueue("pm2xxx_charger_wq",
1055 WQ_MEM_RECLAIM);
1056 if (pm2->charger_wq == NULL) {
1057 ret = -ENOMEM;
1058 dev_err(pm2->dev, "failed to create work queue\n");
1059 goto free_device_info;
1060 }
1061
1062
1063 INIT_WORK(&pm2->ac_work, pm2xxx_charger_ac_work);
1064
1065
1066 INIT_WORK(&pm2->check_main_thermal_prot_work,
1067 pm2xxx_charger_check_main_thermal_prot_work);
1068
1069
1070 INIT_DEFERRABLE_WORK(&pm2->check_hw_failure_work,
1071 pm2xxx_charger_check_hw_failure_work);
1072
1073
1074
1075
1076
1077
1078 pm2->regu = regulator_get(pm2->dev, "vddadc");
1079 if (IS_ERR(pm2->regu)) {
1080 ret = PTR_ERR(pm2->regu);
1081 dev_err(pm2->dev, "failed to get vddadc regulator\n");
1082 goto free_charger_wq;
1083 }
1084
1085
1086 pm2->ac_chg.psy = power_supply_register(pm2->dev, &pm2->ac_chg_desc,
1087 &psy_cfg);
1088 if (IS_ERR(pm2->ac_chg.psy)) {
1089 dev_err(pm2->dev, "failed to register AC charger\n");
1090 ret = PTR_ERR(pm2->ac_chg.psy);
1091 goto free_regulator;
1092 }
1093
1094
1095 ret = request_threaded_irq(gpio_to_irq(pm2->pdata->gpio_irq_number),
1096 NULL,
1097 pm2xxx_charger_irq[0].isr,
1098 pm2->pdata->irq_type,
1099 pm2xxx_charger_irq[0].name, pm2);
1100
1101 if (ret != 0) {
1102 dev_err(pm2->dev, "failed to request %s IRQ %d: %d\n",
1103 pm2xxx_charger_irq[0].name,
1104 gpio_to_irq(pm2->pdata->gpio_irq_number), ret);
1105 goto unregister_pm2xxx_charger;
1106 }
1107
1108 ret = pm_runtime_set_active(pm2->dev);
1109 if (ret)
1110 dev_err(pm2->dev, "set active Error\n");
1111
1112 pm_runtime_enable(pm2->dev);
1113 pm_runtime_set_autosuspend_delay(pm2->dev, PM2XXX_AUTOSUSPEND_DELAY);
1114 pm_runtime_use_autosuspend(pm2->dev);
1115 pm_runtime_resume(pm2->dev);
1116
1117
1118 ret = enable_irq_wake(gpio_to_irq(pm2->pdata->gpio_irq_number));
1119 if (ret) {
1120 dev_err(pm2->dev, "failed to set irq wake\n");
1121 goto unregister_pm2xxx_interrupt;
1122 }
1123
1124 mutex_init(&pm2->lock);
1125
1126 if (gpio_is_valid(pm2->pdata->lpn_gpio)) {
1127
1128 pm2->lpn_pin = pm2->pdata->lpn_gpio;
1129
1130
1131
1132
1133
1134
1135 ret = gpio_request(pm2->lpn_pin, "pm2301_lpm_gpio");
1136
1137 if (ret < 0) {
1138 dev_err(pm2->dev, "pm2301_lpm_gpio request failed\n");
1139 goto disable_pm2_irq_wake;
1140 }
1141 ret = gpio_direction_output(pm2->lpn_pin, 0);
1142 if (ret < 0) {
1143 dev_err(pm2->dev, "pm2301_lpm_gpio direction failed\n");
1144 goto free_gpio;
1145 }
1146 set_lpn_pin(pm2);
1147 }
1148
1149
1150 for (i = 0; i < PM2XXX_NUM_INT_REG; i++)
1151 pm2xxx_reg_read(pm2,
1152 pm2xxx_interrupt_registers[i],
1153 &val);
1154
1155 ret = pm2xxx_charger_detection(pm2, &val);
1156
1157 if ((ret == 0) && val) {
1158 pm2->ac.charger_connected = 1;
1159 ab8500_override_turn_on_stat(~AB8500_POW_KEY_1_ON,
1160 AB8500_MAIN_CH_DET);
1161 pm2->ac_conn = true;
1162 power_supply_changed(pm2->ac_chg.psy);
1163 sysfs_notify(&pm2->ac_chg.psy->dev.kobj, NULL, "present");
1164 }
1165
1166 return 0;
1167
1168 free_gpio:
1169 if (gpio_is_valid(pm2->lpn_pin))
1170 gpio_free(pm2->lpn_pin);
1171 disable_pm2_irq_wake:
1172 disable_irq_wake(gpio_to_irq(pm2->pdata->gpio_irq_number));
1173 unregister_pm2xxx_interrupt:
1174
1175 free_irq(gpio_to_irq(pm2->pdata->gpio_irq_number), pm2);
1176 unregister_pm2xxx_charger:
1177
1178 power_supply_unregister(pm2->ac_chg.psy);
1179 free_regulator:
1180
1181 regulator_put(pm2->regu);
1182 free_charger_wq:
1183 destroy_workqueue(pm2->charger_wq);
1184 free_device_info:
1185 kfree(pm2);
1186
1187 return ret;
1188 }
1189
1190 static int pm2xxx_wall_charger_remove(struct i2c_client *i2c_client)
1191 {
1192 struct pm2xxx_charger *pm2 = i2c_get_clientdata(i2c_client);
1193
1194
1195 pm_runtime_disable(pm2->dev);
1196
1197 pm2xxx_charger_ac_en(&pm2->ac_chg, false, 0, 0);
1198
1199
1200 disable_irq_wake(gpio_to_irq(pm2->pdata->gpio_irq_number));
1201
1202
1203 free_irq(gpio_to_irq(pm2->pdata->gpio_irq_number), pm2);
1204
1205
1206 destroy_workqueue(pm2->charger_wq);
1207
1208 flush_scheduled_work();
1209
1210
1211 regulator_put(pm2->regu);
1212
1213 power_supply_unregister(pm2->ac_chg.psy);
1214
1215 if (gpio_is_valid(pm2->lpn_pin))
1216 gpio_free(pm2->lpn_pin);
1217
1218 kfree(pm2);
1219
1220 return 0;
1221 }
1222
1223 static const struct i2c_device_id pm2xxx_id[] = {
1224 { "pm2301", 0 },
1225 { }
1226 };
1227
1228 MODULE_DEVICE_TABLE(i2c, pm2xxx_id);
1229
1230 static struct i2c_driver pm2xxx_charger_driver = {
1231 .probe = pm2xxx_wall_charger_probe,
1232 .remove = pm2xxx_wall_charger_remove,
1233 .driver = {
1234 .name = "pm2xxx-wall_charger",
1235 .pm = IS_ENABLED(CONFIG_PM) ? &pm2xxx_pm_ops : NULL,
1236 },
1237 .id_table = pm2xxx_id,
1238 };
1239
1240 static int __init pm2xxx_charger_init(void)
1241 {
1242 return i2c_add_driver(&pm2xxx_charger_driver);
1243 }
1244
1245 static void __exit pm2xxx_charger_exit(void)
1246 {
1247 i2c_del_driver(&pm2xxx_charger_driver);
1248 }
1249
1250 device_initcall_sync(pm2xxx_charger_init);
1251 module_exit(pm2xxx_charger_exit);
1252
1253 MODULE_LICENSE("GPL v2");
1254 MODULE_AUTHOR("Rajkumar kasirajan, Olivier Launay");
1255 MODULE_DESCRIPTION("PM2xxx charger management driver");