1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) ST-Ericsson SA 2012
4 *
5 * Charger driver for AB8500
6 *
7 * Author:
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
11 */
12
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/component.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/notifier.h>
20 #include <linux/slab.h>
21 #include <linux/platform_device.h>
22 #include <linux/power_supply.h>
23 #include <linux/completion.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/err.h>
26 #include <linux/workqueue.h>
27 #include <linux/kobject.h>
28 #include <linux/of.h>
29 #include <linux/mfd/core.h>
30 #include <linux/mfd/abx500/ab8500.h>
31 #include <linux/mfd/abx500.h>
32 #include <linux/usb/otg.h>
33 #include <linux/mutex.h>
34 #include <linux/iio/consumer.h>
35
36 #include "ab8500-bm.h"
37 #include "ab8500-chargalg.h"
38
39 /* Charger constants */
40 #define NO_PW_CONN 0
41 #define AC_PW_CONN 1
42 #define USB_PW_CONN 2
43
44 #define MAIN_WDOG_ENA 0x01
45 #define MAIN_WDOG_KICK 0x02
46 #define MAIN_WDOG_DIS 0x00
47 #define CHARG_WD_KICK 0x01
48 #define MAIN_CH_ENA 0x01
49 #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
50 #define USB_CH_ENA 0x01
51 #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
52 #define MAIN_CH_DET 0x01
53 #define MAIN_CH_CV_ON 0x04
54 #define USB_CH_CV_ON 0x08
55 #define VBUS_DET_DBNC100 0x02
56 #define VBUS_DET_DBNC1 0x01
57 #define OTP_ENABLE_WD 0x01
58 #define DROP_COUNT_RESET 0x01
59 #define USB_CH_DET 0x01
60
61 #define MAIN_CH_INPUT_CURR_SHIFT 4
62 #define VBUS_IN_CURR_LIM_SHIFT 4
63 #define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
64 #define VBUS_IN_CURR_LIM_RETRY_SET_TIME 30 /* seconds */
65
66 #define LED_INDICATOR_PWM_ENA 0x01
67 #define LED_INDICATOR_PWM_DIS 0x00
68 #define LED_IND_CUR_5MA 0x04
69 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
70
71 /* HW failure constants */
72 #define MAIN_CH_TH_PROT 0x02
73 #define VBUS_CH_NOK 0x08
74 #define USB_CH_TH_PROT 0x02
75 #define VBUS_OVV_TH 0x01
76 #define MAIN_CH_NOK 0x01
77 #define VBUS_DET 0x80
78
79 #define MAIN_CH_STATUS2_MAINCHGDROP 0x80
80 #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
81 #define USB_CH_VBUSDROP 0x40
82 #define USB_CH_VBUSDETDBNC 0x01
83
84 /* UsbLineStatus register bit masks */
85 #define AB8500_USB_LINK_STATUS 0x78
86 #define AB8505_USB_LINK_STATUS 0xF8
87 #define AB8500_STD_HOST_SUSP 0x18
88 #define USB_LINK_STATUS_SHIFT 3
89
90 /* Watchdog timeout constant */
91 #define WD_TIMER 0x30 /* 4min */
92 #define WD_KICK_INTERVAL (60 * HZ)
93
94 /* Lowest charger voltage is 3.39V -> 0x4E */
95 #define LOW_VOLT_REG 0x4E
96
97 /* Step up/down delay in us */
98 #define STEP_UDELAY 1000
99
100 #define CHARGER_STATUS_POLL 10 /* in ms */
101
102 #define CHG_WD_INTERVAL (60 * HZ)
103
104 #define AB8500_SW_CONTROL_FALLBACK 0x03
105 /* Wait for enumeration before charing in us */
106 #define WAIT_ACA_RID_ENUMERATION (5 * 1000)
107 /*External charger control*/
108 #define AB8500_SYS_CHARGER_CONTROL_REG 0x52
109 #define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
110 #define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
111
112 /* UsbLineStatus register - usb types */
113 enum ab8500_charger_link_status {
114 USB_STAT_NOT_CONFIGURED,
115 USB_STAT_STD_HOST_NC,
116 USB_STAT_STD_HOST_C_NS,
117 USB_STAT_STD_HOST_C_S,
118 USB_STAT_HOST_CHG_NM,
119 USB_STAT_HOST_CHG_HS,
120 USB_STAT_HOST_CHG_HS_CHIRP,
121 USB_STAT_DEDICATED_CHG,
122 USB_STAT_ACA_RID_A,
123 USB_STAT_ACA_RID_B,
124 USB_STAT_ACA_RID_C_NM,
125 USB_STAT_ACA_RID_C_HS,
126 USB_STAT_ACA_RID_C_HS_CHIRP,
127 USB_STAT_HM_IDGND,
128 USB_STAT_RESERVED,
129 USB_STAT_NOT_VALID_LINK,
130 USB_STAT_PHY_EN,
131 USB_STAT_SUP_NO_IDGND_VBUS,
132 USB_STAT_SUP_IDGND_VBUS,
133 USB_STAT_CHARGER_LINE_1,
134 USB_STAT_CARKIT_1,
135 USB_STAT_CARKIT_2,
136 USB_STAT_ACA_DOCK_CHARGER,
137 };
138
139 enum ab8500_usb_state {
140 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
141 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
142 AB8500_BM_USB_STATE_CONFIGURED,
143 AB8500_BM_USB_STATE_SUSPEND,
144 AB8500_BM_USB_STATE_RESUME,
145 AB8500_BM_USB_STATE_MAX,
146 };
147
148 /* VBUS input current limits supported in AB8500 in uA */
149 #define USB_CH_IP_CUR_LVL_0P05 50000
150 #define USB_CH_IP_CUR_LVL_0P09 98000
151 #define USB_CH_IP_CUR_LVL_0P19 193000
152 #define USB_CH_IP_CUR_LVL_0P29 290000
153 #define USB_CH_IP_CUR_LVL_0P38 380000
154 #define USB_CH_IP_CUR_LVL_0P45 450000
155 #define USB_CH_IP_CUR_LVL_0P5 500000
156 #define USB_CH_IP_CUR_LVL_0P6 600000
157 #define USB_CH_IP_CUR_LVL_0P7 700000
158 #define USB_CH_IP_CUR_LVL_0P8 800000
159 #define USB_CH_IP_CUR_LVL_0P9 900000
160 #define USB_CH_IP_CUR_LVL_1P0 1000000
161 #define USB_CH_IP_CUR_LVL_1P1 1100000
162 #define USB_CH_IP_CUR_LVL_1P3 1300000
163 #define USB_CH_IP_CUR_LVL_1P4 1400000
164 #define USB_CH_IP_CUR_LVL_1P5 1500000
165
166 #define VBAT_TRESH_IP_CUR_RED 3800000
167
168 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
169 struct ab8500_charger, usb_chg)
170 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
171 struct ab8500_charger, ac_chg)
172
173 /**
174 * struct ab8500_charger_interrupts - ab8500 interrupts
175 * @name: name of the interrupt
176 * @isr function pointer to the isr
177 */
178 struct ab8500_charger_interrupts {
179 char *name;
180 irqreturn_t (*isr)(int irq, void *data);
181 };
182
183 struct ab8500_charger_info {
184 int charger_connected;
185 int charger_online;
186 int charger_voltage_uv;
187 int cv_active;
188 bool wd_expired;
189 int charger_current_ua;
190 };
191
192 struct ab8500_charger_event_flags {
193 bool mainextchnotok;
194 bool main_thermal_prot;
195 bool usb_thermal_prot;
196 bool vbus_ovv;
197 bool usbchargernotok;
198 bool chgwdexp;
199 bool vbus_collapse;
200 bool vbus_drop_end;
201 };
202
203 struct ab8500_charger_usb_state {
204 int usb_current_ua;
205 int usb_current_tmp_ua;
206 enum ab8500_usb_state state;
207 enum ab8500_usb_state state_tmp;
208 spinlock_t usb_lock;
209 };
210
211 struct ab8500_charger_max_usb_in_curr {
212 int usb_type_max_ua;
213 int set_max_ua;
214 int calculated_max_ua;
215 };
216
217 /**
218 * struct ab8500_charger - ab8500 Charger device information
219 * @dev: Pointer to the structure device
220 * @vbus_detected: VBUS detected
221 * @vbus_detected_start:
222 * VBUS detected during startup
223 * @ac_conn: This will be true when the AC charger has been plugged
224 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
225 * charger is enabled
226 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
227 * charger is enabled
228 * @vbat Battery voltage
229 * @old_vbat Previously measured battery voltage
230 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
231 * @autopower Indicate if we should have automatic pwron after pwrloss
232 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
233 * @invalid_charger_detect_state State when forcing AB to use invalid charger
234 * @is_aca_rid: Incicate if accessory is ACA type
235 * @current_stepping_sessions:
236 * Counter for current stepping sessions
237 * @parent: Pointer to the struct ab8500
238 * @adc_main_charger_v ADC channel for main charger voltage
239 * @adc_main_charger_c ADC channel for main charger current
240 * @adc_vbus_v ADC channel for USB charger voltage
241 * @adc_usb_charger_c ADC channel for USB charger current
242 * @bm: Platform specific battery management information
243 * @flags: Structure for information about events triggered
244 * @usb_state: Structure for usb stack information
245 * @max_usb_in_curr: Max USB charger input current
246 * @ac_chg: AC charger power supply
247 * @usb_chg: USB charger power supply
248 * @ac: Structure that holds the AC charger properties
249 * @usb: Structure that holds the USB charger properties
250 * @regu: Pointer to the struct regulator
251 * @charger_wq: Work queue for the IRQs and checking HW state
252 * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
253 * @pm_lock: Lock to prevent system to suspend
254 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
255 * @check_hw_failure_work: Work for checking HW state
256 * @check_usbchgnotok_work: Work for checking USB charger not ok status
257 * @kick_wd_work: Work for kicking the charger watchdog in case
258 * of ABB rev 1.* due to the watchog logic bug
259 * @ac_charger_attached_work: Work for checking if AC charger is still
260 * connected
261 * @usb_charger_attached_work: Work for checking if USB charger is still
262 * connected
263 * @ac_work: Work for checking AC charger connection
264 * @detect_usb_type_work: Work for detecting the USB type connected
265 * @usb_link_status_work: Work for checking the new USB link status
266 * @usb_state_changed_work: Work for checking USB state
267 * @attach_work: Work for detecting USB type
268 * @vbus_drop_end_work: Work for detecting VBUS drop end
269 * @check_main_thermal_prot_work:
270 * Work for checking Main thermal status
271 * @check_usb_thermal_prot_work:
272 * Work for checking USB thermal status
273 * @charger_attached_mutex: For controlling the wakelock
274 */
275 struct ab8500_charger {
276 struct device *dev;
277 bool vbus_detected;
278 bool vbus_detected_start;
279 bool ac_conn;
280 bool vddadc_en_ac;
281 bool vddadc_en_usb;
282 int vbat;
283 int old_vbat;
284 bool usb_device_is_unrecognised;
285 bool autopower;
286 bool autopower_cfg;
287 int invalid_charger_detect_state;
288 int is_aca_rid;
289 atomic_t current_stepping_sessions;
290 struct ab8500 *parent;
291 struct iio_channel *adc_main_charger_v;
292 struct iio_channel *adc_main_charger_c;
293 struct iio_channel *adc_vbus_v;
294 struct iio_channel *adc_usb_charger_c;
295 struct ab8500_bm_data *bm;
296 struct ab8500_charger_event_flags flags;
297 struct ab8500_charger_usb_state usb_state;
298 struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
299 struct ux500_charger ac_chg;
300 struct ux500_charger usb_chg;
301 struct ab8500_charger_info ac;
302 struct ab8500_charger_info usb;
303 struct regulator *regu;
304 struct workqueue_struct *charger_wq;
305 struct mutex usb_ipt_crnt_lock;
306 struct delayed_work check_vbat_work;
307 struct delayed_work check_hw_failure_work;
308 struct delayed_work check_usbchgnotok_work;
309 struct delayed_work kick_wd_work;
310 struct delayed_work usb_state_changed_work;
311 struct delayed_work attach_work;
312 struct delayed_work ac_charger_attached_work;
313 struct delayed_work usb_charger_attached_work;
314 struct delayed_work vbus_drop_end_work;
315 struct work_struct ac_work;
316 struct work_struct detect_usb_type_work;
317 struct work_struct usb_link_status_work;
318 struct work_struct check_main_thermal_prot_work;
319 struct work_struct check_usb_thermal_prot_work;
320 struct usb_phy *usb_phy;
321 struct notifier_block nb;
322 struct mutex charger_attached_mutex;
323 };
324
325 /* AC properties */
326 static enum power_supply_property ab8500_charger_ac_props[] = {
327 POWER_SUPPLY_PROP_HEALTH,
328 POWER_SUPPLY_PROP_PRESENT,
329 POWER_SUPPLY_PROP_ONLINE,
330 POWER_SUPPLY_PROP_VOLTAGE_NOW,
331 POWER_SUPPLY_PROP_VOLTAGE_AVG,
332 POWER_SUPPLY_PROP_CURRENT_NOW,
333 };
334
335 /* USB properties */
336 static enum power_supply_property ab8500_charger_usb_props[] = {
337 POWER_SUPPLY_PROP_HEALTH,
338 POWER_SUPPLY_PROP_CURRENT_AVG,
339 POWER_SUPPLY_PROP_PRESENT,
340 POWER_SUPPLY_PROP_ONLINE,
341 POWER_SUPPLY_PROP_VOLTAGE_NOW,
342 POWER_SUPPLY_PROP_VOLTAGE_AVG,
343 POWER_SUPPLY_PROP_CURRENT_NOW,
344 };
345
346 /*
347 * Function for enabling and disabling sw fallback mode
348 * should always be disabled when no charger is connected.
349 */
ab8500_enable_disable_sw_fallback(struct ab8500_charger * di,bool fallback)350 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
351 bool fallback)
352 {
353 u8 val;
354 u8 reg;
355 u8 bank;
356 u8 bit;
357 int ret;
358
359 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
360
361 if (is_ab8500(di->parent)) {
362 bank = 0x15;
363 reg = 0x0;
364 bit = 3;
365 } else {
366 bank = AB8500_SYS_CTRL1_BLOCK;
367 reg = AB8500_SW_CONTROL_FALLBACK;
368 bit = 0;
369 }
370
371 /* read the register containing fallback bit */
372 ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
373 if (ret < 0) {
374 dev_err(di->dev, "%d read failed\n", __LINE__);
375 return;
376 }
377
378 if (is_ab8500(di->parent)) {
379 /* enable the OPT emulation registers */
380 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
381 if (ret) {
382 dev_err(di->dev, "%d write failed\n", __LINE__);
383 goto disable_otp;
384 }
385 }
386
387 if (fallback)
388 val |= (1 << bit);
389 else
390 val &= ~(1 << bit);
391
392 /* write back the changed fallback bit value to register */
393 ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
394 if (ret) {
395 dev_err(di->dev, "%d write failed\n", __LINE__);
396 }
397
398 disable_otp:
399 if (is_ab8500(di->parent)) {
400 /* disable the set OTP registers again */
401 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
402 if (ret) {
403 dev_err(di->dev, "%d write failed\n", __LINE__);
404 }
405 }
406 }
407
408 /**
409 * ab8500_power_supply_changed - a wrapper with local extensions for
410 * power_supply_changed
411 * @di: pointer to the ab8500_charger structure
412 * @psy: pointer to power_supply_that have changed.
413 *
414 */
ab8500_power_supply_changed(struct ab8500_charger * di,struct power_supply * psy)415 static void ab8500_power_supply_changed(struct ab8500_charger *di,
416 struct power_supply *psy)
417 {
418 /*
419 * This happens if we get notifications or interrupts and
420 * the platform has been configured not to support one or
421 * other type of charging.
422 */
423 if (!psy)
424 return;
425
426 if (di->autopower_cfg) {
427 if (!di->usb.charger_connected &&
428 !di->ac.charger_connected &&
429 di->autopower) {
430 di->autopower = false;
431 ab8500_enable_disable_sw_fallback(di, false);
432 } else if (!di->autopower &&
433 (di->ac.charger_connected ||
434 di->usb.charger_connected)) {
435 di->autopower = true;
436 ab8500_enable_disable_sw_fallback(di, true);
437 }
438 }
439 power_supply_changed(psy);
440 }
441
ab8500_charger_set_usb_connected(struct ab8500_charger * di,bool connected)442 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
443 bool connected)
444 {
445 if (connected != di->usb.charger_connected) {
446 dev_dbg(di->dev, "USB connected:%i\n", connected);
447 di->usb.charger_connected = connected;
448
449 if (!connected)
450 di->flags.vbus_drop_end = false;
451
452 /*
453 * Sometimes the platform is configured not to support
454 * USB charging and no psy has been created, but we still
455 * will get these notifications.
456 */
457 if (di->usb_chg.psy) {
458 sysfs_notify(&di->usb_chg.psy->dev.kobj, NULL,
459 "present");
460 }
461
462 if (connected) {
463 mutex_lock(&di->charger_attached_mutex);
464 mutex_unlock(&di->charger_attached_mutex);
465
466 if (is_ab8500(di->parent))
467 queue_delayed_work(di->charger_wq,
468 &di->usb_charger_attached_work,
469 HZ);
470 } else {
471 cancel_delayed_work_sync(&di->usb_charger_attached_work);
472 mutex_lock(&di->charger_attached_mutex);
473 mutex_unlock(&di->charger_attached_mutex);
474 }
475 }
476 }
477
478 /**
479 * ab8500_charger_get_ac_voltage() - get ac charger voltage
480 * @di: pointer to the ab8500_charger structure
481 *
482 * Returns ac charger voltage in microvolt (on success)
483 */
ab8500_charger_get_ac_voltage(struct ab8500_charger * di)484 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
485 {
486 int vch, ret;
487
488 /* Only measure voltage if the charger is connected */
489 if (di->ac.charger_connected) {
490 /* Convert to microvolt, IIO returns millivolt */
491 ret = iio_read_channel_processed_scale(di->adc_main_charger_v,
492 &vch, 1000);
493 if (ret < 0) {
494 dev_err(di->dev, "%s ADC conv failed\n", __func__);
495 return ret;
496 }
497 } else {
498 vch = 0;
499 }
500 return vch;
501 }
502
503 /**
504 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
505 * @di: pointer to the ab8500_charger structure
506 *
507 * Returns ac charger CV mode (on success) else error code
508 */
ab8500_charger_ac_cv(struct ab8500_charger * di)509 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
510 {
511 u8 val;
512 int ret = 0;
513
514 /* Only check CV mode if the charger is online */
515 if (di->ac.charger_online) {
516 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
517 AB8500_CH_STATUS1_REG, &val);
518 if (ret < 0) {
519 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
520 return 0;
521 }
522
523 if (val & MAIN_CH_CV_ON)
524 ret = 1;
525 else
526 ret = 0;
527 }
528
529 return ret;
530 }
531
532 /**
533 * ab8500_charger_get_vbus_voltage() - get vbus voltage
534 * @di: pointer to the ab8500_charger structure
535 *
536 * This function returns the vbus voltage.
537 * Returns vbus voltage in microvolt (on success)
538 */
ab8500_charger_get_vbus_voltage(struct ab8500_charger * di)539 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
540 {
541 int vch, ret;
542
543 /* Only measure voltage if the charger is connected */
544 if (di->usb.charger_connected) {
545 /* Convert to microvolt, IIO returns millivolt */
546 ret = iio_read_channel_processed_scale(di->adc_vbus_v,
547 &vch, 1000);
548 if (ret < 0) {
549 dev_err(di->dev, "%s ADC conv failed\n", __func__);
550 return ret;
551 }
552 } else {
553 vch = 0;
554 }
555 return vch;
556 }
557
558 /**
559 * ab8500_charger_get_usb_current() - get usb charger current
560 * @di: pointer to the ab8500_charger structure
561 *
562 * This function returns the usb charger current.
563 * Returns usb current in microamperes (on success) and error code on failure
564 */
ab8500_charger_get_usb_current(struct ab8500_charger * di)565 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
566 {
567 int ich, ret;
568
569 /* Only measure current if the charger is online */
570 if (di->usb.charger_online) {
571 /* Return microamperes */
572 ret = iio_read_channel_processed_scale(di->adc_usb_charger_c,
573 &ich, 1000);
574 if (ret < 0) {
575 dev_err(di->dev, "%s ADC conv failed\n", __func__);
576 return ret;
577 }
578 } else {
579 ich = 0;
580 }
581 return ich;
582 }
583
584 /**
585 * ab8500_charger_get_ac_current() - get ac charger current
586 * @di: pointer to the ab8500_charger structure
587 *
588 * This function returns the ac charger current.
589 * Returns ac current in microamperes (on success) and error code on failure.
590 */
ab8500_charger_get_ac_current(struct ab8500_charger * di)591 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
592 {
593 int ich, ret;
594
595 /* Only measure current if the charger is online */
596 if (di->ac.charger_online) {
597 /* Return microamperes */
598 ret = iio_read_channel_processed_scale(di->adc_main_charger_c,
599 &ich, 1000);
600 if (ret < 0) {
601 dev_err(di->dev, "%s ADC conv failed\n", __func__);
602 return ret;
603 }
604 } else {
605 ich = 0;
606 }
607 return ich;
608 }
609
610 /**
611 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
612 * @di: pointer to the ab8500_charger structure
613 *
614 * Returns ac charger CV mode (on success) else error code
615 */
ab8500_charger_usb_cv(struct ab8500_charger * di)616 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
617 {
618 int ret;
619 u8 val;
620
621 /* Only check CV mode if the charger is online */
622 if (di->usb.charger_online) {
623 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
624 AB8500_CH_USBCH_STAT1_REG, &val);
625 if (ret < 0) {
626 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
627 return 0;
628 }
629
630 if (val & USB_CH_CV_ON)
631 ret = 1;
632 else
633 ret = 0;
634 } else {
635 ret = 0;
636 }
637
638 return ret;
639 }
640
641 /**
642 * ab8500_charger_detect_chargers() - Detect the connected chargers
643 * @di: pointer to the ab8500_charger structure
644 * @probe: if probe, don't delay and wait for HW
645 *
646 * Returns the type of charger connected.
647 * For USB it will not mean we can actually charge from it
648 * but that there is a USB cable connected that we have to
649 * identify. This is used during startup when we don't get
650 * interrupts of the charger detection
651 *
652 * Returns an integer value, that means,
653 * NO_PW_CONN no power supply is connected
654 * AC_PW_CONN if the AC power supply is connected
655 * USB_PW_CONN if the USB power supply is connected
656 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
657 */
ab8500_charger_detect_chargers(struct ab8500_charger * di,bool probe)658 static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
659 {
660 int result = NO_PW_CONN;
661 int ret;
662 u8 val;
663
664 /* Check for AC charger */
665 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
666 AB8500_CH_STATUS1_REG, &val);
667 if (ret < 0) {
668 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
669 return ret;
670 }
671
672 if (val & MAIN_CH_DET)
673 result = AC_PW_CONN;
674
675 /* Check for USB charger */
676
677 if (!probe) {
678 /*
679 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
680 * when disconnecting ACA even though no
681 * charger was connected. Try waiting a little
682 * longer than the 100 ms of VBUS_DET_DBNC100...
683 */
684 msleep(110);
685 }
686 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
687 AB8500_CH_USBCH_STAT1_REG, &val);
688 if (ret < 0) {
689 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
690 return ret;
691 }
692 dev_dbg(di->dev,
693 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
694 val);
695 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
696 result |= USB_PW_CONN;
697
698 return result;
699 }
700
701 /**
702 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
703 * @di: pointer to the ab8500_charger structure
704 * @link_status: the identified USB type
705 *
706 * Get the maximum current that is allowed to be drawn from the host
707 * based on the USB type.
708 * Returns error code in case of failure else 0 on success
709 */
ab8500_charger_max_usb_curr(struct ab8500_charger * di,enum ab8500_charger_link_status link_status)710 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
711 enum ab8500_charger_link_status link_status)
712 {
713 int ret = 0;
714
715 di->usb_device_is_unrecognised = false;
716
717 /*
718 * Platform only supports USB 2.0.
719 * This means that charging current from USB source
720 * is maximum 500 mA. Every occurrence of USB_STAT_*_HOST_*
721 * should set USB_CH_IP_CUR_LVL_0P5.
722 */
723
724 switch (link_status) {
725 case USB_STAT_STD_HOST_NC:
726 case USB_STAT_STD_HOST_C_NS:
727 case USB_STAT_STD_HOST_C_S:
728 dev_dbg(di->dev, "USB Type - Standard host is "
729 "detected through USB driver\n");
730 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
731 di->is_aca_rid = 0;
732 break;
733 case USB_STAT_HOST_CHG_HS_CHIRP:
734 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
735 di->is_aca_rid = 0;
736 break;
737 case USB_STAT_HOST_CHG_HS:
738 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
739 di->is_aca_rid = 0;
740 break;
741 case USB_STAT_ACA_RID_C_HS:
742 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P9;
743 di->is_aca_rid = 0;
744 break;
745 case USB_STAT_ACA_RID_A:
746 /*
747 * Dedicated charger level minus maximum current accessory
748 * can consume (900mA). Closest level is 500mA
749 */
750 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
751 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
752 di->is_aca_rid = 1;
753 break;
754 case USB_STAT_ACA_RID_B:
755 /*
756 * Dedicated charger level minus 120mA (20mA for ACA and
757 * 100mA for potential accessory). Closest level is 1300mA
758 */
759 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_1P3;
760 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
761 di->max_usb_in_curr.usb_type_max_ua);
762 di->is_aca_rid = 1;
763 break;
764 case USB_STAT_HOST_CHG_NM:
765 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
766 di->is_aca_rid = 0;
767 break;
768 case USB_STAT_DEDICATED_CHG:
769 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_1P5;
770 di->is_aca_rid = 0;
771 break;
772 case USB_STAT_ACA_RID_C_HS_CHIRP:
773 case USB_STAT_ACA_RID_C_NM:
774 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_1P5;
775 di->is_aca_rid = 1;
776 break;
777 case USB_STAT_NOT_CONFIGURED:
778 if (di->vbus_detected) {
779 di->usb_device_is_unrecognised = true;
780 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
781 di->max_usb_in_curr.usb_type_max_ua =
782 USB_CH_IP_CUR_LVL_1P5;
783 break;
784 }
785 fallthrough;
786 case USB_STAT_HM_IDGND:
787 dev_err(di->dev, "USB Type - Charging not allowed\n");
788 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P05;
789 ret = -ENXIO;
790 break;
791 case USB_STAT_RESERVED:
792 if (is_ab8500(di->parent)) {
793 di->flags.vbus_collapse = true;
794 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
795 "VBUS has collapsed\n");
796 ret = -ENXIO;
797 break;
798 } else {
799 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
800 di->max_usb_in_curr.usb_type_max_ua =
801 USB_CH_IP_CUR_LVL_0P05;
802 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
803 link_status,
804 di->max_usb_in_curr.usb_type_max_ua);
805 ret = -ENXIO;
806 break;
807 }
808 case USB_STAT_CARKIT_1:
809 case USB_STAT_CARKIT_2:
810 case USB_STAT_ACA_DOCK_CHARGER:
811 case USB_STAT_CHARGER_LINE_1:
812 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
813 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
814 di->max_usb_in_curr.usb_type_max_ua);
815 break;
816 case USB_STAT_NOT_VALID_LINK:
817 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
818 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
819 break;
820
821 default:
822 dev_err(di->dev, "USB Type - Unknown\n");
823 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P05;
824 ret = -ENXIO;
825 break;
826 }
827
828 di->max_usb_in_curr.set_max_ua = di->max_usb_in_curr.usb_type_max_ua;
829 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
830 link_status, di->max_usb_in_curr.set_max_ua);
831
832 return ret;
833 }
834
835 /**
836 * ab8500_charger_read_usb_type() - read the type of usb connected
837 * @di: pointer to the ab8500_charger structure
838 *
839 * Detect the type of the plugged USB
840 * Returns error code in case of failure else 0 on success
841 */
ab8500_charger_read_usb_type(struct ab8500_charger * di)842 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
843 {
844 int ret;
845 u8 val;
846
847 ret = abx500_get_register_interruptible(di->dev,
848 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
849 if (ret < 0) {
850 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
851 return ret;
852 }
853 if (is_ab8500(di->parent))
854 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
855 AB8500_USB_LINE_STAT_REG, &val);
856 else
857 ret = abx500_get_register_interruptible(di->dev,
858 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
859 if (ret < 0) {
860 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
861 return ret;
862 }
863
864 /* get the USB type */
865 if (is_ab8500(di->parent))
866 val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
867 else
868 val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
869 ret = ab8500_charger_max_usb_curr(di,
870 (enum ab8500_charger_link_status) val);
871
872 return ret;
873 }
874
875 /**
876 * ab8500_charger_detect_usb_type() - get the type of usb connected
877 * @di: pointer to the ab8500_charger structure
878 *
879 * Detect the type of the plugged USB
880 * Returns error code in case of failure else 0 on success
881 */
ab8500_charger_detect_usb_type(struct ab8500_charger * di)882 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
883 {
884 int i, ret;
885 u8 val;
886
887 /*
888 * On getting the VBUS rising edge detect interrupt there
889 * is a 250ms delay after which the register UsbLineStatus
890 * is filled with valid data.
891 */
892 for (i = 0; i < 10; i++) {
893 msleep(250);
894 ret = abx500_get_register_interruptible(di->dev,
895 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
896 &val);
897 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
898 __func__, val);
899 if (ret < 0) {
900 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
901 return ret;
902 }
903
904 if (is_ab8500(di->parent))
905 ret = abx500_get_register_interruptible(di->dev,
906 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
907 else
908 ret = abx500_get_register_interruptible(di->dev,
909 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
910 if (ret < 0) {
911 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
912 return ret;
913 }
914 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
915 val);
916 /*
917 * Until the IT source register is read the UsbLineStatus
918 * register is not updated, hence doing the same
919 * Revisit this:
920 */
921
922 /* get the USB type */
923 if (is_ab8500(di->parent))
924 val = (val & AB8500_USB_LINK_STATUS) >>
925 USB_LINK_STATUS_SHIFT;
926 else
927 val = (val & AB8505_USB_LINK_STATUS) >>
928 USB_LINK_STATUS_SHIFT;
929 if (val)
930 break;
931 }
932 ret = ab8500_charger_max_usb_curr(di,
933 (enum ab8500_charger_link_status) val);
934
935 return ret;
936 }
937
938 /*
939 * This array maps the raw hex value to charger voltage used by the AB8500
940 * Values taken from the UM0836, in microvolt.
941 */
942 static int ab8500_charger_voltage_map[] = {
943 3500000,
944 3525000,
945 3550000,
946 3575000,
947 3600000,
948 3625000,
949 3650000,
950 3675000,
951 3700000,
952 3725000,
953 3750000,
954 3775000,
955 3800000,
956 3825000,
957 3850000,
958 3875000,
959 3900000,
960 3925000,
961 3950000,
962 3975000,
963 4000000,
964 4025000,
965 4050000,
966 4060000,
967 4070000,
968 4080000,
969 4090000,
970 4100000,
971 4110000,
972 4120000,
973 4130000,
974 4140000,
975 4150000,
976 4160000,
977 4170000,
978 4180000,
979 4190000,
980 4200000,
981 4210000,
982 4220000,
983 4230000,
984 4240000,
985 4250000,
986 4260000,
987 4270000,
988 4280000,
989 4290000,
990 4300000,
991 4310000,
992 4320000,
993 4330000,
994 4340000,
995 4350000,
996 4360000,
997 4370000,
998 4380000,
999 4390000,
1000 4400000,
1001 4410000,
1002 4420000,
1003 4430000,
1004 4440000,
1005 4450000,
1006 4460000,
1007 4470000,
1008 4480000,
1009 4490000,
1010 4500000,
1011 4510000,
1012 4520000,
1013 4530000,
1014 4540000,
1015 4550000,
1016 4560000,
1017 4570000,
1018 4580000,
1019 4590000,
1020 4600000,
1021 };
1022
ab8500_voltage_to_regval(int voltage_uv)1023 static int ab8500_voltage_to_regval(int voltage_uv)
1024 {
1025 int i;
1026
1027 /* Special case for voltage below 3.5V */
1028 if (voltage_uv < ab8500_charger_voltage_map[0])
1029 return LOW_VOLT_REG;
1030
1031 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
1032 if (voltage_uv < ab8500_charger_voltage_map[i])
1033 return i - 1;
1034 }
1035
1036 /* If not last element, return error */
1037 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
1038 if (voltage_uv == ab8500_charger_voltage_map[i])
1039 return i;
1040 else
1041 return -1;
1042 }
1043
1044 /* This array maps the raw register value to charger input current */
1045 static int ab8500_charge_input_curr_map[] = {
1046 50000, 98000, 193000, 290000, 380000, 450000, 500000, 600000,
1047 700000, 800000, 900000, 1000000, 1100000, 1300000, 1400000, 1500000,
1048 };
1049
1050 /* This array maps the raw register value to charger output current */
1051 static int ab8500_charge_output_curr_map[] = {
1052 100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000,
1053 900000, 1000000, 1100000, 1200000, 1300000, 1400000, 1500000, 1500000,
1054 };
1055
ab8500_current_to_regval(struct ab8500_charger * di,int curr_ua)1056 static int ab8500_current_to_regval(struct ab8500_charger *di, int curr_ua)
1057 {
1058 int i;
1059
1060 if (curr_ua < ab8500_charge_output_curr_map[0])
1061 return 0;
1062
1063 for (i = 0; i < ARRAY_SIZE(ab8500_charge_output_curr_map); i++) {
1064 if (curr_ua < ab8500_charge_output_curr_map[i])
1065 return i - 1;
1066 }
1067
1068 /* If not last element, return error */
1069 i = ARRAY_SIZE(ab8500_charge_output_curr_map) - 1;
1070 if (curr_ua == ab8500_charge_output_curr_map[i])
1071 return i;
1072 else
1073 return -1;
1074 }
1075
ab8500_vbus_in_curr_to_regval(struct ab8500_charger * di,int curr_ua)1076 static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr_ua)
1077 {
1078 int i;
1079
1080 if (curr_ua < ab8500_charge_input_curr_map[0])
1081 return 0;
1082
1083 for (i = 0; i < ARRAY_SIZE(ab8500_charge_input_curr_map); i++) {
1084 if (curr_ua < ab8500_charge_input_curr_map[i])
1085 return i - 1;
1086 }
1087
1088 /* If not last element, return error */
1089 i = ARRAY_SIZE(ab8500_charge_input_curr_map) - 1;
1090 if (curr_ua == ab8500_charge_input_curr_map[i])
1091 return i;
1092 else
1093 return -1;
1094 }
1095
1096 /**
1097 * ab8500_charger_get_usb_cur() - get usb current
1098 * @di: pointer to the ab8500_charger structure
1099 *
1100 * The usb stack provides the maximum current that can be drawn from
1101 * the standard usb host. This will be in uA.
1102 * This function converts current in uA to a value that can be written
1103 * to the register. Returns -1 if charging is not allowed
1104 */
ab8500_charger_get_usb_cur(struct ab8500_charger * di)1105 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1106 {
1107 int ret = 0;
1108 switch (di->usb_state.usb_current_ua) {
1109 case 100000:
1110 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P09;
1111 break;
1112 case 200000:
1113 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P19;
1114 break;
1115 case 300000:
1116 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P29;
1117 break;
1118 case 400000:
1119 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P38;
1120 break;
1121 case 500000:
1122 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
1123 break;
1124 default:
1125 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P05;
1126 ret = -EPERM;
1127 break;
1128 }
1129 di->max_usb_in_curr.set_max_ua = di->max_usb_in_curr.usb_type_max_ua;
1130 return ret;
1131 }
1132
1133 /**
1134 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1135 * @di: pointer to the ab8500_charger structure
1136 * @reg: select what charger register to check
1137 *
1138 * Check if current stepping should be allowed to continue.
1139 * Checks if charger source has not collapsed. If it has, further stepping
1140 * is not allowed.
1141 */
ab8500_charger_check_continue_stepping(struct ab8500_charger * di,int reg)1142 static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1143 int reg)
1144 {
1145 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1146 return !di->flags.vbus_drop_end;
1147 else
1148 return true;
1149 }
1150
1151 /**
1152 * ab8500_charger_set_current() - set charger current
1153 * @di: pointer to the ab8500_charger structure
1154 * @ich_ua: charger current, in uA
1155 * @reg: select what charger register to set
1156 *
1157 * Set charger current.
1158 * There is no state machine in the AB to step up/down the charger
1159 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1160 * charging is started. Instead we need to implement
1161 * this charger current step-up/down here.
1162 * Returns error code in case of failure else 0(on success)
1163 */
ab8500_charger_set_current(struct ab8500_charger * di,int ich_ua,int reg)1164 static int ab8500_charger_set_current(struct ab8500_charger *di,
1165 int ich_ua, int reg)
1166 {
1167 int ret = 0;
1168 int curr_index, prev_curr_index, shift_value, i;
1169 u8 reg_value;
1170 u32 step_udelay;
1171 bool no_stepping = false;
1172
1173 atomic_inc(&di->current_stepping_sessions);
1174
1175 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1176 reg, ®_value);
1177 if (ret < 0) {
1178 dev_err(di->dev, "%s read failed\n", __func__);
1179 goto exit_set_current;
1180 }
1181
1182 switch (reg) {
1183 case AB8500_MCH_IPT_CURLVL_REG:
1184 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1185 prev_curr_index = (reg_value >> shift_value);
1186 curr_index = ab8500_current_to_regval(di, ich_ua);
1187 step_udelay = STEP_UDELAY;
1188 if (!di->ac.charger_connected)
1189 no_stepping = true;
1190 break;
1191 case AB8500_USBCH_IPT_CRNTLVL_REG:
1192 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1193 prev_curr_index = (reg_value >> shift_value);
1194 curr_index = ab8500_vbus_in_curr_to_regval(di, ich_ua);
1195 step_udelay = STEP_UDELAY * 100;
1196
1197 if (!di->usb.charger_connected)
1198 no_stepping = true;
1199 break;
1200 case AB8500_CH_OPT_CRNTLVL_REG:
1201 shift_value = 0;
1202 prev_curr_index = (reg_value >> shift_value);
1203 curr_index = ab8500_current_to_regval(di, ich_ua);
1204 step_udelay = STEP_UDELAY;
1205 if (curr_index && (curr_index - prev_curr_index) > 1)
1206 step_udelay *= 100;
1207
1208 if (!di->usb.charger_connected && !di->ac.charger_connected)
1209 no_stepping = true;
1210
1211 break;
1212 default:
1213 dev_err(di->dev, "%s current register not valid\n", __func__);
1214 ret = -ENXIO;
1215 goto exit_set_current;
1216 }
1217
1218 if (curr_index < 0) {
1219 dev_err(di->dev, "requested current limit out-of-range\n");
1220 ret = -ENXIO;
1221 goto exit_set_current;
1222 }
1223
1224 /* only update current if it's been changed */
1225 if (prev_curr_index == curr_index) {
1226 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1227 __func__, reg);
1228 ret = 0;
1229 goto exit_set_current;
1230 }
1231
1232 dev_dbg(di->dev, "%s set charger current: %d uA for reg: 0x%02x\n",
1233 __func__, ich_ua, reg);
1234
1235 if (no_stepping) {
1236 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1237 reg, (u8)curr_index << shift_value);
1238 if (ret)
1239 dev_err(di->dev, "%s write failed\n", __func__);
1240 } else if (prev_curr_index > curr_index) {
1241 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1242 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1243 (u8) i << shift_value, reg);
1244 ret = abx500_set_register_interruptible(di->dev,
1245 AB8500_CHARGER, reg, (u8)i << shift_value);
1246 if (ret) {
1247 dev_err(di->dev, "%s write failed\n", __func__);
1248 goto exit_set_current;
1249 }
1250 if (i != curr_index)
1251 usleep_range(step_udelay, step_udelay * 2);
1252 }
1253 } else {
1254 bool allow = true;
1255 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
1256 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1257 (u8)i << shift_value, reg);
1258 ret = abx500_set_register_interruptible(di->dev,
1259 AB8500_CHARGER, reg, (u8)i << shift_value);
1260 if (ret) {
1261 dev_err(di->dev, "%s write failed\n", __func__);
1262 goto exit_set_current;
1263 }
1264 if (i != curr_index)
1265 usleep_range(step_udelay, step_udelay * 2);
1266
1267 allow = ab8500_charger_check_continue_stepping(di, reg);
1268 }
1269 }
1270
1271 exit_set_current:
1272 atomic_dec(&di->current_stepping_sessions);
1273
1274 return ret;
1275 }
1276
1277 /**
1278 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1279 * @di: pointer to the ab8500_charger structure
1280 * @ich_in_ua: charger input current limit in microampere
1281 *
1282 * Sets the current that can be drawn from the USB host
1283 * Returns error code in case of failure else 0(on success)
1284 */
ab8500_charger_set_vbus_in_curr(struct ab8500_charger * di,int ich_in_ua)1285 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1286 int ich_in_ua)
1287 {
1288 int min_value;
1289 int ret;
1290
1291 /* We should always use to lowest current limit */
1292 min_value = min(di->bm->chg_params->usb_curr_max_ua, ich_in_ua);
1293 if (di->max_usb_in_curr.set_max_ua > 0)
1294 min_value = min(di->max_usb_in_curr.set_max_ua, min_value);
1295
1296 if (di->usb_state.usb_current_ua >= 0)
1297 min_value = min(di->usb_state.usb_current_ua, min_value);
1298
1299 switch (min_value) {
1300 case 100000:
1301 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1302 min_value = USB_CH_IP_CUR_LVL_0P05;
1303 break;
1304 case 500000:
1305 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1306 min_value = USB_CH_IP_CUR_LVL_0P45;
1307 break;
1308 default:
1309 break;
1310 }
1311
1312 dev_info(di->dev, "VBUS input current limit set to %d uA\n", min_value);
1313
1314 mutex_lock(&di->usb_ipt_crnt_lock);
1315 ret = ab8500_charger_set_current(di, min_value,
1316 AB8500_USBCH_IPT_CRNTLVL_REG);
1317 mutex_unlock(&di->usb_ipt_crnt_lock);
1318
1319 return ret;
1320 }
1321
1322 /**
1323 * ab8500_charger_set_main_in_curr() - set main charger input current
1324 * @di: pointer to the ab8500_charger structure
1325 * @ich_in_ua: input charger current, in uA
1326 *
1327 * Set main charger input current.
1328 * Returns error code in case of failure else 0(on success)
1329 */
ab8500_charger_set_main_in_curr(struct ab8500_charger * di,int ich_in_ua)1330 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1331 int ich_in_ua)
1332 {
1333 return ab8500_charger_set_current(di, ich_in_ua,
1334 AB8500_MCH_IPT_CURLVL_REG);
1335 }
1336
1337 /**
1338 * ab8500_charger_set_output_curr() - set charger output current
1339 * @di: pointer to the ab8500_charger structure
1340 * @ich_out_ua: output charger current, in uA
1341 *
1342 * Set charger output current.
1343 * Returns error code in case of failure else 0(on success)
1344 */
ab8500_charger_set_output_curr(struct ab8500_charger * di,int ich_out_ua)1345 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1346 int ich_out_ua)
1347 {
1348 return ab8500_charger_set_current(di, ich_out_ua,
1349 AB8500_CH_OPT_CRNTLVL_REG);
1350 }
1351
1352 /**
1353 * ab8500_charger_led_en() - turn on/off chargign led
1354 * @di: pointer to the ab8500_charger structure
1355 * @on: flag to turn on/off the chargign led
1356 *
1357 * Power ON/OFF charging LED indication
1358 * Returns error code in case of failure else 0(on success)
1359 */
ab8500_charger_led_en(struct ab8500_charger * di,int on)1360 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1361 {
1362 int ret;
1363
1364 if (on) {
1365 /* Power ON charging LED indicator, set LED current to 5mA */
1366 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1367 AB8500_LED_INDICATOR_PWM_CTRL,
1368 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1369 if (ret) {
1370 dev_err(di->dev, "Power ON LED failed\n");
1371 return ret;
1372 }
1373 /* LED indicator PWM duty cycle 252/256 */
1374 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1375 AB8500_LED_INDICATOR_PWM_DUTY,
1376 LED_INDICATOR_PWM_DUTY_252_256);
1377 if (ret) {
1378 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1379 return ret;
1380 }
1381 } else {
1382 /* Power off charging LED indicator */
1383 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1384 AB8500_LED_INDICATOR_PWM_CTRL,
1385 LED_INDICATOR_PWM_DIS);
1386 if (ret) {
1387 dev_err(di->dev, "Power-off LED failed\n");
1388 return ret;
1389 }
1390 }
1391
1392 return ret;
1393 }
1394
1395 /**
1396 * ab8500_charger_ac_en() - enable or disable ac charging
1397 * @di: pointer to the ab8500_charger structure
1398 * @enable: enable/disable flag
1399 * @vset_uv: charging voltage in microvolt
1400 * @iset_ua: charging current in microampere
1401 *
1402 * Enable/Disable AC/Mains charging and turns on/off the charging led
1403 * respectively.
1404 **/
ab8500_charger_ac_en(struct ux500_charger * charger,int enable,int vset_uv,int iset_ua)1405 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1406 int enable, int vset_uv, int iset_ua)
1407 {
1408 int ret;
1409 int volt_index;
1410 int curr_index;
1411 int input_curr_index;
1412 u8 overshoot = 0;
1413
1414 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1415
1416 if (enable) {
1417 /* Check if AC is connected */
1418 if (!di->ac.charger_connected) {
1419 dev_err(di->dev, "AC charger not connected\n");
1420 return -ENXIO;
1421 }
1422
1423 /* Enable AC charging */
1424 dev_dbg(di->dev, "Enable AC: %duV %duA\n", vset_uv, iset_ua);
1425
1426 /*
1427 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1428 * will be triggered every time we enable the VDD ADC supply.
1429 * This will turn off charging for a short while.
1430 * It can be avoided by having the supply on when
1431 * there is a charger enabled. Normally the VDD ADC supply
1432 * is enabled every time a GPADC conversion is triggered.
1433 * We will force it to be enabled from this driver to have
1434 * the GPADC module independent of the AB8500 chargers
1435 */
1436 if (!di->vddadc_en_ac) {
1437 ret = regulator_enable(di->regu);
1438 if (ret)
1439 dev_warn(di->dev,
1440 "Failed to enable regulator\n");
1441 else
1442 di->vddadc_en_ac = true;
1443 }
1444
1445 /* Check if the requested voltage or current is valid */
1446 volt_index = ab8500_voltage_to_regval(vset_uv);
1447 curr_index = ab8500_current_to_regval(di, iset_ua);
1448 input_curr_index = ab8500_current_to_regval(di,
1449 di->bm->chg_params->ac_curr_max_ua);
1450 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1451 dev_err(di->dev,
1452 "Charger voltage or current too high, "
1453 "charging not started\n");
1454 return -ENXIO;
1455 }
1456
1457 /* ChVoltLevel: maximum battery charging voltage */
1458 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1459 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1460 if (ret) {
1461 dev_err(di->dev, "%s write failed\n", __func__);
1462 return ret;
1463 }
1464 /* MainChInputCurr: current that can be drawn from the charger*/
1465 ret = ab8500_charger_set_main_in_curr(di,
1466 di->bm->chg_params->ac_curr_max_ua);
1467 if (ret) {
1468 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1469 __func__);
1470 return ret;
1471 }
1472 /* ChOutputCurentLevel: protected output current */
1473 ret = ab8500_charger_set_output_curr(di, iset_ua);
1474 if (ret) {
1475 dev_err(di->dev, "%s "
1476 "Failed to set ChOutputCurentLevel\n",
1477 __func__);
1478 return ret;
1479 }
1480
1481 /* Check if VBAT overshoot control should be enabled */
1482 if (!di->bm->enable_overshoot)
1483 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1484
1485 /* Enable Main Charger */
1486 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1487 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1488 if (ret) {
1489 dev_err(di->dev, "%s write failed\n", __func__);
1490 return ret;
1491 }
1492
1493 /* Power on charging LED indication */
1494 ret = ab8500_charger_led_en(di, true);
1495 if (ret < 0)
1496 dev_err(di->dev, "failed to enable LED\n");
1497
1498 di->ac.charger_online = 1;
1499 } else {
1500 /* Disable AC charging */
1501 if (is_ab8500_1p1_or_earlier(di->parent)) {
1502 /*
1503 * For ABB revision 1.0 and 1.1 there is a bug in the
1504 * watchdog logic. That means we have to continuously
1505 * kick the charger watchdog even when no charger is
1506 * connected. This is only valid once the AC charger
1507 * has been enabled. This is a bug that is not handled
1508 * by the algorithm and the watchdog have to be kicked
1509 * by the charger driver when the AC charger
1510 * is disabled
1511 */
1512 if (di->ac_conn) {
1513 queue_delayed_work(di->charger_wq,
1514 &di->kick_wd_work,
1515 round_jiffies(WD_KICK_INTERVAL));
1516 }
1517
1518 /*
1519 * We can't turn off charging completely
1520 * due to a bug in AB8500 cut1.
1521 * If we do, charging will not start again.
1522 * That is why we set the lowest voltage
1523 * and current possible
1524 */
1525 ret = abx500_set_register_interruptible(di->dev,
1526 AB8500_CHARGER,
1527 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1528 if (ret) {
1529 dev_err(di->dev,
1530 "%s write failed\n", __func__);
1531 return ret;
1532 }
1533
1534 ret = ab8500_charger_set_output_curr(di, 0);
1535 if (ret) {
1536 dev_err(di->dev, "%s "
1537 "Failed to set ChOutputCurentLevel\n",
1538 __func__);
1539 return ret;
1540 }
1541 } else {
1542 ret = abx500_set_register_interruptible(di->dev,
1543 AB8500_CHARGER,
1544 AB8500_MCH_CTRL1, 0);
1545 if (ret) {
1546 dev_err(di->dev,
1547 "%s write failed\n", __func__);
1548 return ret;
1549 }
1550 }
1551
1552 ret = ab8500_charger_led_en(di, false);
1553 if (ret < 0)
1554 dev_err(di->dev, "failed to disable LED\n");
1555
1556 di->ac.charger_online = 0;
1557 di->ac.wd_expired = false;
1558
1559 /* Disable regulator if enabled */
1560 if (di->vddadc_en_ac) {
1561 regulator_disable(di->regu);
1562 di->vddadc_en_ac = false;
1563 }
1564
1565 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1566 }
1567 ab8500_power_supply_changed(di, di->ac_chg.psy);
1568
1569 return ret;
1570 }
1571
1572 /**
1573 * ab8500_charger_usb_en() - enable usb charging
1574 * @di: pointer to the ab8500_charger structure
1575 * @enable: enable/disable flag
1576 * @vset_uv: charging voltage in microvolt
1577 * @ich_out_ua: charger output current in microampere
1578 *
1579 * Enable/Disable USB charging and turns on/off the charging led respectively.
1580 * Returns error code in case of failure else 0(on success)
1581 */
ab8500_charger_usb_en(struct ux500_charger * charger,int enable,int vset_uv,int ich_out_ua)1582 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1583 int enable, int vset_uv, int ich_out_ua)
1584 {
1585 int ret;
1586 int volt_index;
1587 int curr_index;
1588 u8 overshoot = 0;
1589
1590 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1591
1592 if (enable) {
1593 /* Check if USB is connected */
1594 if (!di->usb.charger_connected) {
1595 dev_err(di->dev, "USB charger not connected\n");
1596 return -ENXIO;
1597 }
1598
1599 /*
1600 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1601 * will be triggered every time we enable the VDD ADC supply.
1602 * This will turn off charging for a short while.
1603 * It can be avoided by having the supply on when
1604 * there is a charger enabled. Normally the VDD ADC supply
1605 * is enabled every time a GPADC conversion is triggered.
1606 * We will force it to be enabled from this driver to have
1607 * the GPADC module independent of the AB8500 chargers
1608 */
1609 if (!di->vddadc_en_usb) {
1610 ret = regulator_enable(di->regu);
1611 if (ret)
1612 dev_warn(di->dev,
1613 "Failed to enable regulator\n");
1614 else
1615 di->vddadc_en_usb = true;
1616 }
1617
1618 /* Enable USB charging */
1619 dev_dbg(di->dev, "Enable USB: %d uV %d uA\n", vset_uv, ich_out_ua);
1620
1621 /* Check if the requested voltage or current is valid */
1622 volt_index = ab8500_voltage_to_regval(vset_uv);
1623 curr_index = ab8500_current_to_regval(di, ich_out_ua);
1624 if (volt_index < 0 || curr_index < 0) {
1625 dev_err(di->dev,
1626 "Charger voltage or current too high, "
1627 "charging not started\n");
1628 return -ENXIO;
1629 }
1630
1631 /*
1632 * ChVoltLevel: max voltage up to which battery can be
1633 * charged
1634 */
1635 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1636 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1637 if (ret) {
1638 dev_err(di->dev, "%s write failed\n", __func__);
1639 return ret;
1640 }
1641 /* Check if VBAT overshoot control should be enabled */
1642 if (!di->bm->enable_overshoot)
1643 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1644
1645 /* Enable USB Charger */
1646 dev_dbg(di->dev,
1647 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
1648 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1649 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1650 if (ret) {
1651 dev_err(di->dev, "%s write failed\n", __func__);
1652 return ret;
1653 }
1654
1655 /* If success power on charging LED indication */
1656 ret = ab8500_charger_led_en(di, true);
1657 if (ret < 0)
1658 dev_err(di->dev, "failed to enable LED\n");
1659
1660 di->usb.charger_online = 1;
1661
1662 /* USBChInputCurr: current that can be drawn from the usb */
1663 ret = ab8500_charger_set_vbus_in_curr(di,
1664 di->max_usb_in_curr.usb_type_max_ua);
1665 if (ret) {
1666 dev_err(di->dev, "setting USBChInputCurr failed\n");
1667 return ret;
1668 }
1669
1670 /* ChOutputCurentLevel: protected output current */
1671 ret = ab8500_charger_set_output_curr(di, ich_out_ua);
1672 if (ret) {
1673 dev_err(di->dev, "%s "
1674 "Failed to set ChOutputCurentLevel\n",
1675 __func__);
1676 return ret;
1677 }
1678
1679 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1680
1681 } else {
1682 /* Disable USB charging */
1683 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1684 ret = abx500_set_register_interruptible(di->dev,
1685 AB8500_CHARGER,
1686 AB8500_USBCH_CTRL1_REG, 0);
1687 if (ret) {
1688 dev_err(di->dev,
1689 "%s write failed\n", __func__);
1690 return ret;
1691 }
1692
1693 ret = ab8500_charger_led_en(di, false);
1694 if (ret < 0)
1695 dev_err(di->dev, "failed to disable LED\n");
1696 /* USBChInputCurr: current that can be drawn from the usb */
1697 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1698 if (ret) {
1699 dev_err(di->dev, "setting USBChInputCurr failed\n");
1700 return ret;
1701 }
1702
1703 /* ChOutputCurentLevel: protected output current */
1704 ret = ab8500_charger_set_output_curr(di, 0);
1705 if (ret) {
1706 dev_err(di->dev, "%s "
1707 "Failed to reset ChOutputCurentLevel\n",
1708 __func__);
1709 return ret;
1710 }
1711 di->usb.charger_online = 0;
1712 di->usb.wd_expired = false;
1713
1714 /* Disable regulator if enabled */
1715 if (di->vddadc_en_usb) {
1716 regulator_disable(di->regu);
1717 di->vddadc_en_usb = false;
1718 }
1719
1720 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1721
1722 /* Cancel any pending Vbat check work */
1723 cancel_delayed_work(&di->check_vbat_work);
1724
1725 }
1726 ab8500_power_supply_changed(di, di->usb_chg.psy);
1727
1728 return ret;
1729 }
1730
1731 /**
1732 * ab8500_charger_usb_check_enable() - enable usb charging
1733 * @charger: pointer to the ux500_charger structure
1734 * @vset_uv: charging voltage in microvolt
1735 * @iset_ua: charger output current in microampere
1736 *
1737 * Check if the VBUS charger has been disconnected and reconnected without
1738 * AB8500 rising an interrupt. Returns 0 on success.
1739 */
ab8500_charger_usb_check_enable(struct ux500_charger * charger,int vset_uv,int iset_ua)1740 static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1741 int vset_uv, int iset_ua)
1742 {
1743 u8 usbch_ctrl1 = 0;
1744 int ret = 0;
1745
1746 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1747
1748 if (!di->usb.charger_connected)
1749 return ret;
1750
1751 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1752 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1753 if (ret < 0) {
1754 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1755 return ret;
1756 }
1757 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1758
1759 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1760 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1761
1762 ret = abx500_mask_and_set_register_interruptible(di->dev,
1763 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1764 DROP_COUNT_RESET, DROP_COUNT_RESET);
1765 if (ret < 0) {
1766 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1767 return ret;
1768 }
1769
1770 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset_uv, iset_ua);
1771 if (ret < 0) {
1772 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1773 __LINE__);
1774 return ret;
1775 }
1776 }
1777 return ret;
1778 }
1779
1780 /**
1781 * ab8500_charger_ac_check_enable() - enable usb charging
1782 * @charger: pointer to the ux500_charger structure
1783 * @vset_uv: charging voltage in microvolt
1784 * @iset_ua: charger output current in micrompere
1785 *
1786 * Check if the AC charger has been disconnected and reconnected without
1787 * AB8500 rising an interrupt. Returns 0 on success.
1788 */
ab8500_charger_ac_check_enable(struct ux500_charger * charger,int vset_uv,int iset_ua)1789 static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1790 int vset_uv, int iset_ua)
1791 {
1792 u8 mainch_ctrl1 = 0;
1793 int ret = 0;
1794
1795 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1796
1797 if (!di->ac.charger_connected)
1798 return ret;
1799
1800 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1801 AB8500_MCH_CTRL1, &mainch_ctrl1);
1802 if (ret < 0) {
1803 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1804 return ret;
1805 }
1806 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1807
1808 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1809 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1810
1811 ret = abx500_mask_and_set_register_interruptible(di->dev,
1812 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1813 DROP_COUNT_RESET, DROP_COUNT_RESET);
1814
1815 if (ret < 0) {
1816 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1817 return ret;
1818 }
1819
1820 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset_uv, iset_ua);
1821 if (ret < 0) {
1822 dev_err(di->dev, "failed to enable AC charger %d\n",
1823 __LINE__);
1824 return ret;
1825 }
1826 }
1827 return ret;
1828 }
1829
1830 /**
1831 * ab8500_charger_watchdog_kick() - kick charger watchdog
1832 * @di: pointer to the ab8500_charger structure
1833 *
1834 * Kick charger watchdog
1835 * Returns error code in case of failure else 0(on success)
1836 */
ab8500_charger_watchdog_kick(struct ux500_charger * charger)1837 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1838 {
1839 int ret;
1840 struct ab8500_charger *di;
1841
1842 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1843 di = to_ab8500_charger_ac_device_info(charger);
1844 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1845 di = to_ab8500_charger_usb_device_info(charger);
1846 else
1847 return -ENXIO;
1848
1849 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1850 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1851 if (ret)
1852 dev_err(di->dev, "Failed to kick WD!\n");
1853
1854 return ret;
1855 }
1856
1857 /**
1858 * ab8500_charger_update_charger_current() - update charger current
1859 * @charger: pointer to the ab8500_charger structure
1860 * @ich_out_ua: desired output current in microampere
1861 *
1862 * Update the charger output current for the specified charger
1863 * Returns error code in case of failure else 0(on success)
1864 */
ab8500_charger_update_charger_current(struct ux500_charger * charger,int ich_out_ua)1865 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1866 int ich_out_ua)
1867 {
1868 int ret;
1869 struct ab8500_charger *di;
1870
1871 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1872 di = to_ab8500_charger_ac_device_info(charger);
1873 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1874 di = to_ab8500_charger_usb_device_info(charger);
1875 else
1876 return -ENXIO;
1877
1878 ret = ab8500_charger_set_output_curr(di, ich_out_ua);
1879 if (ret) {
1880 dev_err(di->dev, "%s "
1881 "Failed to set ChOutputCurentLevel\n",
1882 __func__);
1883 return ret;
1884 }
1885
1886 /* Reset the main and usb drop input current measurement counter */
1887 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1888 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
1889 if (ret) {
1890 dev_err(di->dev, "%s write failed\n", __func__);
1891 return ret;
1892 }
1893
1894 return ret;
1895 }
1896
ab8500_charger_get_ext_psy_data(struct device * dev,void * data)1897 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1898 {
1899 struct power_supply *psy;
1900 struct power_supply *ext = dev_get_drvdata(dev);
1901 const char **supplicants = (const char **)ext->supplied_to;
1902 struct ab8500_charger *di;
1903 union power_supply_propval ret;
1904 int j;
1905 struct ux500_charger *usb_chg;
1906
1907 usb_chg = (struct ux500_charger *)data;
1908 psy = usb_chg->psy;
1909
1910 di = to_ab8500_charger_usb_device_info(usb_chg);
1911
1912 /*
1913 * For all psy where the driver name appears in any supplied_to
1914 * in practice what we will find will always be "ab8500_fg" as
1915 * the fuel gauge is responsible of keeping track of VBAT.
1916 */
1917 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
1918 if (j < 0)
1919 return 0;
1920
1921 /* Go through all properties for the psy */
1922 for (j = 0; j < ext->desc->num_properties; j++) {
1923 enum power_supply_property prop;
1924 prop = ext->desc->properties[j];
1925
1926 if (power_supply_get_property(ext, prop, &ret))
1927 continue;
1928
1929 switch (prop) {
1930 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1931 switch (ext->desc->type) {
1932 case POWER_SUPPLY_TYPE_BATTERY:
1933 /* This will always be "ab8500_fg" */
1934 dev_dbg(di->dev, "get VBAT from %s\n",
1935 dev_name(&ext->dev));
1936 di->vbat = ret.intval;
1937 break;
1938 default:
1939 break;
1940 }
1941 break;
1942 default:
1943 break;
1944 }
1945 }
1946 return 0;
1947 }
1948
1949 /**
1950 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1951 * @work pointer to the work_struct structure
1952 *
1953 * Due to a asic bug it is necessary to lower the input current to the vbus
1954 * charger when charging with at some specific levels. This issue is only valid
1955 * for below a certain battery voltage. This function makes sure that
1956 * the allowed current limit isn't exceeded.
1957 */
ab8500_charger_check_vbat_work(struct work_struct * work)1958 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1959 {
1960 int t = 10;
1961 struct ab8500_charger *di = container_of(work,
1962 struct ab8500_charger, check_vbat_work.work);
1963
1964 power_supply_for_each_device(&di->usb_chg, ab8500_charger_get_ext_psy_data);
1965
1966 /* First run old_vbat is 0. */
1967 if (di->old_vbat == 0)
1968 di->old_vbat = di->vbat;
1969
1970 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1971 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1972 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1973 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1974
1975 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1976 " old: %d\n", di->max_usb_in_curr.usb_type_max_ua,
1977 di->vbat, di->old_vbat);
1978 ab8500_charger_set_vbus_in_curr(di,
1979 di->max_usb_in_curr.usb_type_max_ua);
1980 power_supply_changed(di->usb_chg.psy);
1981 }
1982
1983 di->old_vbat = di->vbat;
1984
1985 /*
1986 * No need to check the battery voltage every second when not close to
1987 * the threshold.
1988 */
1989 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100000) &&
1990 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100000)))
1991 t = 1;
1992
1993 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1994 }
1995
1996 /**
1997 * ab8500_charger_check_hw_failure_work() - check main charger failure
1998 * @work: pointer to the work_struct structure
1999 *
2000 * Work queue function for checking the main charger status
2001 */
ab8500_charger_check_hw_failure_work(struct work_struct * work)2002 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
2003 {
2004 int ret;
2005 u8 reg_value;
2006
2007 struct ab8500_charger *di = container_of(work,
2008 struct ab8500_charger, check_hw_failure_work.work);
2009
2010 /* Check if the status bits for HW failure is still active */
2011 if (di->flags.mainextchnotok) {
2012 ret = abx500_get_register_interruptible(di->dev,
2013 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2014 if (ret < 0) {
2015 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2016 return;
2017 }
2018 if (!(reg_value & MAIN_CH_NOK)) {
2019 di->flags.mainextchnotok = false;
2020 ab8500_power_supply_changed(di, di->ac_chg.psy);
2021 }
2022 }
2023 if (di->flags.vbus_ovv) {
2024 ret = abx500_get_register_interruptible(di->dev,
2025 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
2026 ®_value);
2027 if (ret < 0) {
2028 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2029 return;
2030 }
2031 if (!(reg_value & VBUS_OVV_TH)) {
2032 di->flags.vbus_ovv = false;
2033 ab8500_power_supply_changed(di, di->usb_chg.psy);
2034 }
2035 }
2036 /* If we still have a failure, schedule a new check */
2037 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2038 queue_delayed_work(di->charger_wq,
2039 &di->check_hw_failure_work, round_jiffies(HZ));
2040 }
2041 }
2042
2043 /**
2044 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2045 * @work: pointer to the work_struct structure
2046 *
2047 * Work queue function for kicking the charger watchdog.
2048 *
2049 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2050 * logic. That means we have to continuously kick the charger
2051 * watchdog even when no charger is connected. This is only
2052 * valid once the AC charger has been enabled. This is
2053 * a bug that is not handled by the algorithm and the
2054 * watchdog have to be kicked by the charger driver
2055 * when the AC charger is disabled
2056 */
ab8500_charger_kick_watchdog_work(struct work_struct * work)2057 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2058 {
2059 int ret;
2060
2061 struct ab8500_charger *di = container_of(work,
2062 struct ab8500_charger, kick_wd_work.work);
2063
2064 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2065 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2066 if (ret)
2067 dev_err(di->dev, "Failed to kick WD!\n");
2068
2069 /* Schedule a new watchdog kick */
2070 queue_delayed_work(di->charger_wq,
2071 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2072 }
2073
2074 /**
2075 * ab8500_charger_ac_work() - work to get and set main charger status
2076 * @work: pointer to the work_struct structure
2077 *
2078 * Work queue function for checking the main charger status
2079 */
ab8500_charger_ac_work(struct work_struct * work)2080 static void ab8500_charger_ac_work(struct work_struct *work)
2081 {
2082 int ret;
2083
2084 struct ab8500_charger *di = container_of(work,
2085 struct ab8500_charger, ac_work);
2086
2087 /*
2088 * Since we can't be sure that the events are received
2089 * synchronously, we have the check if the main charger is
2090 * connected by reading the status register
2091 */
2092 ret = ab8500_charger_detect_chargers(di, false);
2093 if (ret < 0)
2094 return;
2095
2096 if (ret & AC_PW_CONN) {
2097 di->ac.charger_connected = 1;
2098 di->ac_conn = true;
2099 } else {
2100 di->ac.charger_connected = 0;
2101 }
2102
2103 ab8500_power_supply_changed(di, di->ac_chg.psy);
2104 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
2105 }
2106
ab8500_charger_usb_attached_work(struct work_struct * work)2107 static void ab8500_charger_usb_attached_work(struct work_struct *work)
2108 {
2109 struct ab8500_charger *di = container_of(work,
2110 struct ab8500_charger,
2111 usb_charger_attached_work.work);
2112 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2113 int ret, i;
2114 u8 statval;
2115
2116 for (i = 0; i < 10; i++) {
2117 ret = abx500_get_register_interruptible(di->dev,
2118 AB8500_CHARGER,
2119 AB8500_CH_USBCH_STAT1_REG,
2120 &statval);
2121 if (ret < 0) {
2122 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2123 goto reschedule;
2124 }
2125 if ((statval & usbch) != usbch)
2126 goto reschedule;
2127
2128 msleep(CHARGER_STATUS_POLL);
2129 }
2130
2131 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2132
2133 mutex_lock(&di->charger_attached_mutex);
2134 mutex_unlock(&di->charger_attached_mutex);
2135
2136 return;
2137
2138 reschedule:
2139 queue_delayed_work(di->charger_wq,
2140 &di->usb_charger_attached_work,
2141 HZ);
2142 }
2143
ab8500_charger_ac_attached_work(struct work_struct * work)2144 static void ab8500_charger_ac_attached_work(struct work_struct *work)
2145 {
2146
2147 struct ab8500_charger *di = container_of(work,
2148 struct ab8500_charger,
2149 ac_charger_attached_work.work);
2150 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2151 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2152 int ret, i;
2153 u8 statval;
2154
2155 for (i = 0; i < 10; i++) {
2156 ret = abx500_get_register_interruptible(di->dev,
2157 AB8500_CHARGER,
2158 AB8500_CH_STATUS2_REG,
2159 &statval);
2160 if (ret < 0) {
2161 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2162 goto reschedule;
2163 }
2164
2165 if ((statval & mainch) != mainch)
2166 goto reschedule;
2167
2168 msleep(CHARGER_STATUS_POLL);
2169 }
2170
2171 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2172 queue_work(di->charger_wq, &di->ac_work);
2173
2174 mutex_lock(&di->charger_attached_mutex);
2175 mutex_unlock(&di->charger_attached_mutex);
2176
2177 return;
2178
2179 reschedule:
2180 queue_delayed_work(di->charger_wq,
2181 &di->ac_charger_attached_work,
2182 HZ);
2183 }
2184
2185 /**
2186 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2187 * @work: Pointer to the work_struct structure
2188 *
2189 * Detect the type of USB plugged
2190 */
ab8500_charger_detect_usb_type_work(struct work_struct * work)2191 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
2192 {
2193 int ret;
2194
2195 struct ab8500_charger *di = container_of(work,
2196 struct ab8500_charger, detect_usb_type_work);
2197
2198 /*
2199 * Since we can't be sure that the events are received
2200 * synchronously, we have the check if is
2201 * connected by reading the status register
2202 */
2203 ret = ab8500_charger_detect_chargers(di, false);
2204 if (ret < 0)
2205 return;
2206
2207 if (!(ret & USB_PW_CONN)) {
2208 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2209 di->vbus_detected = false;
2210 ab8500_charger_set_usb_connected(di, false);
2211 ab8500_power_supply_changed(di, di->usb_chg.psy);
2212 } else {
2213 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2214 di->vbus_detected = true;
2215
2216 if (is_ab8500_1p1_or_earlier(di->parent)) {
2217 ret = ab8500_charger_detect_usb_type(di);
2218 if (!ret) {
2219 ab8500_charger_set_usb_connected(di, true);
2220 ab8500_power_supply_changed(di,
2221 di->usb_chg.psy);
2222 }
2223 } else {
2224 /*
2225 * For ABB cut2.0 and onwards we have an IRQ,
2226 * USB_LINK_STATUS that will be triggered when the USB
2227 * link status changes. The exception is USB connected
2228 * during startup. Then we don't get a
2229 * USB_LINK_STATUS IRQ
2230 */
2231 if (di->vbus_detected_start) {
2232 di->vbus_detected_start = false;
2233 ret = ab8500_charger_detect_usb_type(di);
2234 if (!ret) {
2235 ab8500_charger_set_usb_connected(di,
2236 true);
2237 ab8500_power_supply_changed(di,
2238 di->usb_chg.psy);
2239 }
2240 }
2241 }
2242 }
2243 }
2244
2245 /**
2246 * ab8500_charger_usb_link_attach_work() - work to detect USB type
2247 * @work: pointer to the work_struct structure
2248 *
2249 * Detect the type of USB plugged
2250 */
ab8500_charger_usb_link_attach_work(struct work_struct * work)2251 static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2252 {
2253 struct ab8500_charger *di =
2254 container_of(work, struct ab8500_charger, attach_work.work);
2255 int ret;
2256
2257 /* Update maximum input current if USB enumeration is not detected */
2258 if (!di->usb.charger_online) {
2259 ret = ab8500_charger_set_vbus_in_curr(di,
2260 di->max_usb_in_curr.usb_type_max_ua);
2261 if (ret)
2262 return;
2263 }
2264
2265 ab8500_charger_set_usb_connected(di, true);
2266 ab8500_power_supply_changed(di, di->usb_chg.psy);
2267 }
2268
2269 /**
2270 * ab8500_charger_usb_link_status_work() - work to detect USB type
2271 * @work: pointer to the work_struct structure
2272 *
2273 * Detect the type of USB plugged
2274 */
ab8500_charger_usb_link_status_work(struct work_struct * work)2275 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2276 {
2277 int detected_chargers;
2278 int ret;
2279 u8 val;
2280 u8 link_status;
2281
2282 struct ab8500_charger *di = container_of(work,
2283 struct ab8500_charger, usb_link_status_work);
2284
2285 /*
2286 * Since we can't be sure that the events are received
2287 * synchronously, we have the check if is
2288 * connected by reading the status register
2289 */
2290 detected_chargers = ab8500_charger_detect_chargers(di, false);
2291 if (detected_chargers < 0)
2292 return;
2293
2294 /*
2295 * Some chargers that breaks the USB spec is
2296 * identified as invalid by AB8500 and it refuse
2297 * to start the charging process. but by jumping
2298 * through a few hoops it can be forced to start.
2299 */
2300 if (is_ab8500(di->parent))
2301 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2302 AB8500_USB_LINE_STAT_REG, &val);
2303 else
2304 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2305 AB8500_USB_LINK1_STAT_REG, &val);
2306
2307 if (ret >= 0)
2308 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2309 else
2310 dev_dbg(di->dev, "Error reading USB link status\n");
2311
2312 if (is_ab8500(di->parent))
2313 link_status = AB8500_USB_LINK_STATUS;
2314 else
2315 link_status = AB8505_USB_LINK_STATUS;
2316
2317 if (detected_chargers & USB_PW_CONN) {
2318 if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
2319 USB_STAT_NOT_VALID_LINK &&
2320 di->invalid_charger_detect_state == 0) {
2321 dev_dbg(di->dev,
2322 "Invalid charger detected, state= 0\n");
2323 /*Enable charger*/
2324 abx500_mask_and_set_register_interruptible(di->dev,
2325 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
2326 USB_CH_ENA, USB_CH_ENA);
2327 /*Enable charger detection*/
2328 abx500_mask_and_set_register_interruptible(di->dev,
2329 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2330 USB_CH_DET, USB_CH_DET);
2331 di->invalid_charger_detect_state = 1;
2332 /*exit and wait for new link status interrupt.*/
2333 return;
2334
2335 }
2336 if (di->invalid_charger_detect_state == 1) {
2337 dev_dbg(di->dev,
2338 "Invalid charger detected, state= 1\n");
2339 /*Stop charger detection*/
2340 abx500_mask_and_set_register_interruptible(di->dev,
2341 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2342 USB_CH_DET, 0x00);
2343 /*Check link status*/
2344 if (is_ab8500(di->parent))
2345 ret = abx500_get_register_interruptible(di->dev,
2346 AB8500_USB, AB8500_USB_LINE_STAT_REG,
2347 &val);
2348 else
2349 ret = abx500_get_register_interruptible(di->dev,
2350 AB8500_USB, AB8500_USB_LINK1_STAT_REG,
2351 &val);
2352
2353 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2354 (val & link_status) >> USB_LINK_STATUS_SHIFT);
2355 di->invalid_charger_detect_state = 2;
2356 }
2357 } else {
2358 di->invalid_charger_detect_state = 0;
2359 }
2360
2361 if (!(detected_chargers & USB_PW_CONN)) {
2362 di->vbus_detected = false;
2363 ab8500_charger_set_usb_connected(di, false);
2364 ab8500_power_supply_changed(di, di->usb_chg.psy);
2365 return;
2366 }
2367
2368 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2369 di->vbus_detected = true;
2370 ret = ab8500_charger_read_usb_type(di);
2371 if (ret) {
2372 if (ret == -ENXIO) {
2373 /* No valid charger type detected */
2374 ab8500_charger_set_usb_connected(di, false);
2375 ab8500_power_supply_changed(di, di->usb_chg.psy);
2376 }
2377 return;
2378 }
2379
2380 if (di->usb_device_is_unrecognised) {
2381 dev_dbg(di->dev,
2382 "Potential Legacy Charger device. "
2383 "Delay work for %d msec for USB enum "
2384 "to finish",
2385 WAIT_ACA_RID_ENUMERATION);
2386 queue_delayed_work(di->charger_wq,
2387 &di->attach_work,
2388 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2389 } else if (di->is_aca_rid == 1) {
2390 /* Only wait once */
2391 di->is_aca_rid++;
2392 dev_dbg(di->dev,
2393 "%s Wait %d msec for USB enum to finish",
2394 __func__, WAIT_ACA_RID_ENUMERATION);
2395 queue_delayed_work(di->charger_wq,
2396 &di->attach_work,
2397 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2398 } else {
2399 queue_delayed_work(di->charger_wq,
2400 &di->attach_work,
2401 0);
2402 }
2403 }
2404
ab8500_charger_usb_state_changed_work(struct work_struct * work)2405 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2406 {
2407 int ret;
2408 unsigned long flags;
2409
2410 struct ab8500_charger *di = container_of(work,
2411 struct ab8500_charger, usb_state_changed_work.work);
2412
2413 if (!di->vbus_detected) {
2414 dev_dbg(di->dev,
2415 "%s !di->vbus_detected\n",
2416 __func__);
2417 return;
2418 }
2419
2420 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2421 di->usb_state.state = di->usb_state.state_tmp;
2422 di->usb_state.usb_current_ua = di->usb_state.usb_current_tmp_ua;
2423 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2424
2425 dev_dbg(di->dev, "%s USB state: 0x%02x uA: %d\n",
2426 __func__, di->usb_state.state, di->usb_state.usb_current_ua);
2427
2428 switch (di->usb_state.state) {
2429 case AB8500_BM_USB_STATE_RESET_HS:
2430 case AB8500_BM_USB_STATE_RESET_FS:
2431 case AB8500_BM_USB_STATE_SUSPEND:
2432 case AB8500_BM_USB_STATE_MAX:
2433 ab8500_charger_set_usb_connected(di, false);
2434 ab8500_power_supply_changed(di, di->usb_chg.psy);
2435 break;
2436
2437 case AB8500_BM_USB_STATE_RESUME:
2438 /*
2439 * when suspend->resume there should be delay
2440 * of 1sec for enabling charging
2441 */
2442 msleep(1000);
2443 fallthrough;
2444 case AB8500_BM_USB_STATE_CONFIGURED:
2445 /*
2446 * USB is configured, enable charging with the charging
2447 * input current obtained from USB driver
2448 */
2449 if (!ab8500_charger_get_usb_cur(di)) {
2450 /* Update maximum input current */
2451 ret = ab8500_charger_set_vbus_in_curr(di,
2452 di->max_usb_in_curr.usb_type_max_ua);
2453 if (ret)
2454 return;
2455
2456 ab8500_charger_set_usb_connected(di, true);
2457 ab8500_power_supply_changed(di, di->usb_chg.psy);
2458 }
2459 break;
2460
2461 default:
2462 break;
2463 }
2464 }
2465
2466 /**
2467 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2468 * @work: pointer to the work_struct structure
2469 *
2470 * Work queue function for checking the USB charger Not OK status
2471 */
ab8500_charger_check_usbchargernotok_work(struct work_struct * work)2472 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2473 {
2474 int ret;
2475 u8 reg_value;
2476 bool prev_status;
2477
2478 struct ab8500_charger *di = container_of(work,
2479 struct ab8500_charger, check_usbchgnotok_work.work);
2480
2481 /* Check if the status bit for usbchargernotok is still active */
2482 ret = abx500_get_register_interruptible(di->dev,
2483 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2484 if (ret < 0) {
2485 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2486 return;
2487 }
2488 prev_status = di->flags.usbchargernotok;
2489
2490 if (reg_value & VBUS_CH_NOK) {
2491 di->flags.usbchargernotok = true;
2492 /* Check again in 1sec */
2493 queue_delayed_work(di->charger_wq,
2494 &di->check_usbchgnotok_work, HZ);
2495 } else {
2496 di->flags.usbchargernotok = false;
2497 di->flags.vbus_collapse = false;
2498 }
2499
2500 if (prev_status != di->flags.usbchargernotok)
2501 ab8500_power_supply_changed(di, di->usb_chg.psy);
2502 }
2503
2504 /**
2505 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2506 * @work: pointer to the work_struct structure
2507 *
2508 * Work queue function for checking the Main thermal prot status
2509 */
ab8500_charger_check_main_thermal_prot_work(struct work_struct * work)2510 static void ab8500_charger_check_main_thermal_prot_work(
2511 struct work_struct *work)
2512 {
2513 int ret;
2514 u8 reg_value;
2515
2516 struct ab8500_charger *di = container_of(work,
2517 struct ab8500_charger, check_main_thermal_prot_work);
2518
2519 /* Check if the status bit for main_thermal_prot is still active */
2520 ret = abx500_get_register_interruptible(di->dev,
2521 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2522 if (ret < 0) {
2523 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2524 return;
2525 }
2526 if (reg_value & MAIN_CH_TH_PROT)
2527 di->flags.main_thermal_prot = true;
2528 else
2529 di->flags.main_thermal_prot = false;
2530
2531 ab8500_power_supply_changed(di, di->ac_chg.psy);
2532 }
2533
2534 /**
2535 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2536 * @work: pointer to the work_struct structure
2537 *
2538 * Work queue function for checking the USB thermal prot status
2539 */
ab8500_charger_check_usb_thermal_prot_work(struct work_struct * work)2540 static void ab8500_charger_check_usb_thermal_prot_work(
2541 struct work_struct *work)
2542 {
2543 int ret;
2544 u8 reg_value;
2545
2546 struct ab8500_charger *di = container_of(work,
2547 struct ab8500_charger, check_usb_thermal_prot_work);
2548
2549 /* Check if the status bit for usb_thermal_prot is still active */
2550 ret = abx500_get_register_interruptible(di->dev,
2551 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2552 if (ret < 0) {
2553 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2554 return;
2555 }
2556 if (reg_value & USB_CH_TH_PROT)
2557 di->flags.usb_thermal_prot = true;
2558 else
2559 di->flags.usb_thermal_prot = false;
2560
2561 ab8500_power_supply_changed(di, di->usb_chg.psy);
2562 }
2563
2564 /**
2565 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2566 * @irq: interrupt number
2567 * @_di: pointer to the ab8500_charger structure
2568 *
2569 * Returns IRQ status(IRQ_HANDLED)
2570 */
ab8500_charger_mainchunplugdet_handler(int irq,void * _di)2571 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2572 {
2573 struct ab8500_charger *di = _di;
2574
2575 dev_dbg(di->dev, "Main charger unplugged\n");
2576 queue_work(di->charger_wq, &di->ac_work);
2577
2578 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2579 mutex_lock(&di->charger_attached_mutex);
2580 mutex_unlock(&di->charger_attached_mutex);
2581
2582 return IRQ_HANDLED;
2583 }
2584
2585 /**
2586 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2587 * @irq: interrupt number
2588 * @_di: pointer to the ab8500_charger structure
2589 *
2590 * Returns IRQ status(IRQ_HANDLED)
2591 */
ab8500_charger_mainchplugdet_handler(int irq,void * _di)2592 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2593 {
2594 struct ab8500_charger *di = _di;
2595
2596 dev_dbg(di->dev, "Main charger plugged\n");
2597 queue_work(di->charger_wq, &di->ac_work);
2598
2599 mutex_lock(&di->charger_attached_mutex);
2600 mutex_unlock(&di->charger_attached_mutex);
2601
2602 if (is_ab8500(di->parent))
2603 queue_delayed_work(di->charger_wq,
2604 &di->ac_charger_attached_work,
2605 HZ);
2606 return IRQ_HANDLED;
2607 }
2608
2609 /**
2610 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2611 * @irq: interrupt number
2612 * @_di: pointer to the ab8500_charger structure
2613 *
2614 * Returns IRQ status(IRQ_HANDLED)
2615 */
ab8500_charger_mainextchnotok_handler(int irq,void * _di)2616 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2617 {
2618 struct ab8500_charger *di = _di;
2619
2620 dev_dbg(di->dev, "Main charger not ok\n");
2621 di->flags.mainextchnotok = true;
2622 ab8500_power_supply_changed(di, di->ac_chg.psy);
2623
2624 /* Schedule a new HW failure check */
2625 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2626
2627 return IRQ_HANDLED;
2628 }
2629
2630 /**
2631 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2632 * thermal protection threshold
2633 * @irq: interrupt number
2634 * @_di: pointer to the ab8500_charger structure
2635 *
2636 * Returns IRQ status(IRQ_HANDLED)
2637 */
ab8500_charger_mainchthprotr_handler(int irq,void * _di)2638 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2639 {
2640 struct ab8500_charger *di = _di;
2641
2642 dev_dbg(di->dev,
2643 "Die temp above Main charger thermal protection threshold\n");
2644 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2645
2646 return IRQ_HANDLED;
2647 }
2648
2649 /**
2650 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2651 * thermal protection threshold
2652 * @irq: interrupt number
2653 * @_di: pointer to the ab8500_charger structure
2654 *
2655 * Returns IRQ status(IRQ_HANDLED)
2656 */
ab8500_charger_mainchthprotf_handler(int irq,void * _di)2657 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2658 {
2659 struct ab8500_charger *di = _di;
2660
2661 dev_dbg(di->dev,
2662 "Die temp ok for Main charger thermal protection threshold\n");
2663 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2664
2665 return IRQ_HANDLED;
2666 }
2667
ab8500_charger_vbus_drop_end_work(struct work_struct * work)2668 static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2669 {
2670 struct ab8500_charger *di = container_of(work,
2671 struct ab8500_charger, vbus_drop_end_work.work);
2672 int ret, curr_ua;
2673 u8 reg_value;
2674
2675 di->flags.vbus_drop_end = false;
2676
2677 /* Reset the drop counter */
2678 abx500_set_register_interruptible(di->dev,
2679 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
2680
2681 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2682 AB8500_CH_USBCH_STAT2_REG, ®_value);
2683 if (ret < 0) {
2684 dev_err(di->dev, "%s read failed\n", __func__);
2685 return;
2686 }
2687
2688 curr_ua = ab8500_charge_input_curr_map[
2689 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
2690
2691 if (di->max_usb_in_curr.calculated_max_ua != curr_ua) {
2692 /* USB source is collapsing */
2693 di->max_usb_in_curr.calculated_max_ua = curr_ua;
2694 dev_dbg(di->dev,
2695 "VBUS input current limiting to %d uA\n",
2696 di->max_usb_in_curr.calculated_max_ua);
2697 } else {
2698 /*
2699 * USB source can not give more than this amount.
2700 * Taking more will collapse the source.
2701 */
2702 di->max_usb_in_curr.set_max_ua =
2703 di->max_usb_in_curr.calculated_max_ua;
2704 dev_dbg(di->dev,
2705 "VBUS input current limited to %d uA\n",
2706 di->max_usb_in_curr.set_max_ua);
2707 }
2708
2709 if (di->usb.charger_connected)
2710 ab8500_charger_set_vbus_in_curr(di,
2711 di->max_usb_in_curr.usb_type_max_ua);
2712 }
2713
2714 /**
2715 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2716 * @irq: interrupt number
2717 * @_di: pointer to the ab8500_charger structure
2718 *
2719 * Returns IRQ status(IRQ_HANDLED)
2720 */
ab8500_charger_vbusdetf_handler(int irq,void * _di)2721 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2722 {
2723 struct ab8500_charger *di = _di;
2724
2725 di->vbus_detected = false;
2726 dev_dbg(di->dev, "VBUS falling detected\n");
2727 queue_work(di->charger_wq, &di->detect_usb_type_work);
2728
2729 return IRQ_HANDLED;
2730 }
2731
2732 /**
2733 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2734 * @irq: interrupt number
2735 * @_di: pointer to the ab8500_charger structure
2736 *
2737 * Returns IRQ status(IRQ_HANDLED)
2738 */
ab8500_charger_vbusdetr_handler(int irq,void * _di)2739 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2740 {
2741 struct ab8500_charger *di = _di;
2742
2743 di->vbus_detected = true;
2744 dev_dbg(di->dev, "VBUS rising detected\n");
2745
2746 queue_work(di->charger_wq, &di->detect_usb_type_work);
2747
2748 return IRQ_HANDLED;
2749 }
2750
2751 /**
2752 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2753 * @irq: interrupt number
2754 * @_di: pointer to the ab8500_charger structure
2755 *
2756 * Returns IRQ status(IRQ_HANDLED)
2757 */
ab8500_charger_usblinkstatus_handler(int irq,void * _di)2758 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2759 {
2760 struct ab8500_charger *di = _di;
2761
2762 dev_dbg(di->dev, "USB link status changed\n");
2763
2764 queue_work(di->charger_wq, &di->usb_link_status_work);
2765
2766 return IRQ_HANDLED;
2767 }
2768
2769 /**
2770 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2771 * thermal protection threshold
2772 * @irq: interrupt number
2773 * @_di: pointer to the ab8500_charger structure
2774 *
2775 * Returns IRQ status(IRQ_HANDLED)
2776 */
ab8500_charger_usbchthprotr_handler(int irq,void * _di)2777 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2778 {
2779 struct ab8500_charger *di = _di;
2780
2781 dev_dbg(di->dev,
2782 "Die temp above USB charger thermal protection threshold\n");
2783 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2784
2785 return IRQ_HANDLED;
2786 }
2787
2788 /**
2789 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2790 * thermal protection threshold
2791 * @irq: interrupt number
2792 * @_di: pointer to the ab8500_charger structure
2793 *
2794 * Returns IRQ status(IRQ_HANDLED)
2795 */
ab8500_charger_usbchthprotf_handler(int irq,void * _di)2796 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2797 {
2798 struct ab8500_charger *di = _di;
2799
2800 dev_dbg(di->dev,
2801 "Die temp ok for USB charger thermal protection threshold\n");
2802 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2803
2804 return IRQ_HANDLED;
2805 }
2806
2807 /**
2808 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2809 * @irq: interrupt number
2810 * @_di: pointer to the ab8500_charger structure
2811 *
2812 * Returns IRQ status(IRQ_HANDLED)
2813 */
ab8500_charger_usbchargernotokr_handler(int irq,void * _di)2814 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2815 {
2816 struct ab8500_charger *di = _di;
2817
2818 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2819 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2820
2821 return IRQ_HANDLED;
2822 }
2823
2824 /**
2825 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2826 * @irq: interrupt number
2827 * @_di: pointer to the ab8500_charger structure
2828 *
2829 * Returns IRQ status(IRQ_HANDLED)
2830 */
ab8500_charger_chwdexp_handler(int irq,void * _di)2831 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2832 {
2833 struct ab8500_charger *di = _di;
2834
2835 dev_dbg(di->dev, "Charger watchdog expired\n");
2836
2837 /*
2838 * The charger that was online when the watchdog expired
2839 * needs to be restarted for charging to start again
2840 */
2841 if (di->ac.charger_online) {
2842 di->ac.wd_expired = true;
2843 ab8500_power_supply_changed(di, di->ac_chg.psy);
2844 }
2845 if (di->usb.charger_online) {
2846 di->usb.wd_expired = true;
2847 ab8500_power_supply_changed(di, di->usb_chg.psy);
2848 }
2849
2850 return IRQ_HANDLED;
2851 }
2852
2853 /**
2854 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2855 * @irq: interrupt number
2856 * @_di: pointer to the ab8500_charger structure
2857 *
2858 * Returns IRQ status(IRQ_HANDLED)
2859 */
ab8500_charger_vbuschdropend_handler(int irq,void * _di)2860 static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2861 {
2862 struct ab8500_charger *di = _di;
2863
2864 dev_dbg(di->dev, "VBUS charger drop ended\n");
2865 di->flags.vbus_drop_end = true;
2866
2867 /*
2868 * VBUS might have dropped due to bad connection.
2869 * Schedule a new input limit set to the value SW requests.
2870 */
2871 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
2872 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
2873
2874 return IRQ_HANDLED;
2875 }
2876
2877 /**
2878 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2879 * @irq: interrupt number
2880 * @_di: pointer to the ab8500_charger structure
2881 *
2882 * Returns IRQ status(IRQ_HANDLED)
2883 */
ab8500_charger_vbusovv_handler(int irq,void * _di)2884 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2885 {
2886 struct ab8500_charger *di = _di;
2887
2888 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2889 di->flags.vbus_ovv = true;
2890 ab8500_power_supply_changed(di, di->usb_chg.psy);
2891
2892 /* Schedule a new HW failure check */
2893 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2894
2895 return IRQ_HANDLED;
2896 }
2897
2898 /**
2899 * ab8500_charger_ac_get_property() - get the ac/mains properties
2900 * @psy: pointer to the power_supply structure
2901 * @psp: pointer to the power_supply_property structure
2902 * @val: pointer to the power_supply_propval union
2903 *
2904 * This function gets called when an application tries to get the ac/mains
2905 * properties by reading the sysfs files.
2906 * AC/Mains properties are online, present and voltage.
2907 * online: ac/mains charging is in progress or not
2908 * present: presence of the ac/mains
2909 * voltage: AC/Mains voltage
2910 * Returns error code in case of failure else 0(on success)
2911 */
ab8500_charger_ac_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)2912 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2913 enum power_supply_property psp,
2914 union power_supply_propval *val)
2915 {
2916 struct ab8500_charger *di;
2917 int ret;
2918
2919 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2920
2921 switch (psp) {
2922 case POWER_SUPPLY_PROP_HEALTH:
2923 if (di->flags.mainextchnotok)
2924 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2925 else if (di->ac.wd_expired || di->usb.wd_expired)
2926 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2927 else if (di->flags.main_thermal_prot)
2928 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2929 else
2930 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2931 break;
2932 case POWER_SUPPLY_PROP_ONLINE:
2933 val->intval = di->ac.charger_online;
2934 break;
2935 case POWER_SUPPLY_PROP_PRESENT:
2936 val->intval = di->ac.charger_connected;
2937 break;
2938 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2939 ret = ab8500_charger_get_ac_voltage(di);
2940 if (ret >= 0)
2941 di->ac.charger_voltage_uv = ret;
2942 /* On error, use previous value */
2943 val->intval = di->ac.charger_voltage_uv;
2944 break;
2945 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2946 /*
2947 * This property is used to indicate when CV mode is entered
2948 * for the AC charger
2949 */
2950 di->ac.cv_active = ab8500_charger_ac_cv(di);
2951 val->intval = di->ac.cv_active;
2952 break;
2953 case POWER_SUPPLY_PROP_CURRENT_NOW:
2954 ret = ab8500_charger_get_ac_current(di);
2955 if (ret >= 0)
2956 di->ac.charger_current_ua = ret;
2957 val->intval = di->ac.charger_current_ua;
2958 break;
2959 default:
2960 return -EINVAL;
2961 }
2962 return 0;
2963 }
2964
2965 /**
2966 * ab8500_charger_usb_get_property() - get the usb properties
2967 * @psy: pointer to the power_supply structure
2968 * @psp: pointer to the power_supply_property structure
2969 * @val: pointer to the power_supply_propval union
2970 *
2971 * This function gets called when an application tries to get the usb
2972 * properties by reading the sysfs files.
2973 * USB properties are online, present and voltage.
2974 * online: usb charging is in progress or not
2975 * present: presence of the usb
2976 * voltage: vbus voltage
2977 * Returns error code in case of failure else 0(on success)
2978 */
ab8500_charger_usb_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)2979 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2980 enum power_supply_property psp,
2981 union power_supply_propval *val)
2982 {
2983 struct ab8500_charger *di;
2984 int ret;
2985
2986 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2987
2988 switch (psp) {
2989 case POWER_SUPPLY_PROP_HEALTH:
2990 if (di->flags.usbchargernotok)
2991 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2992 else if (di->ac.wd_expired || di->usb.wd_expired)
2993 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2994 else if (di->flags.usb_thermal_prot)
2995 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2996 else if (di->flags.vbus_ovv)
2997 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2998 else
2999 val->intval = POWER_SUPPLY_HEALTH_GOOD;
3000 break;
3001 case POWER_SUPPLY_PROP_ONLINE:
3002 val->intval = di->usb.charger_online;
3003 break;
3004 case POWER_SUPPLY_PROP_PRESENT:
3005 val->intval = di->usb.charger_connected;
3006 break;
3007 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
3008 ret = ab8500_charger_get_vbus_voltage(di);
3009 if (ret >= 0)
3010 di->usb.charger_voltage_uv = ret;
3011 val->intval = di->usb.charger_voltage_uv;
3012 break;
3013 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
3014 /*
3015 * This property is used to indicate when CV mode is entered
3016 * for the USB charger
3017 */
3018 di->usb.cv_active = ab8500_charger_usb_cv(di);
3019 val->intval = di->usb.cv_active;
3020 break;
3021 case POWER_SUPPLY_PROP_CURRENT_NOW:
3022 ret = ab8500_charger_get_usb_current(di);
3023 if (ret >= 0)
3024 di->usb.charger_current_ua = ret;
3025 val->intval = di->usb.charger_current_ua;
3026 break;
3027 case POWER_SUPPLY_PROP_CURRENT_AVG:
3028 /*
3029 * This property is used to indicate when VBUS has collapsed
3030 * due to too high output current from the USB charger
3031 */
3032 if (di->flags.vbus_collapse)
3033 val->intval = 1;
3034 else
3035 val->intval = 0;
3036 break;
3037 default:
3038 return -EINVAL;
3039 }
3040 return 0;
3041 }
3042
3043 /**
3044 * ab8500_charger_init_hw_registers() - Set up charger related registers
3045 * @di: pointer to the ab8500_charger structure
3046 *
3047 * Set up charger OVV, watchdog and maximum voltage registers as well as
3048 * charging of the backup battery
3049 */
ab8500_charger_init_hw_registers(struct ab8500_charger * di)3050 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3051 {
3052 int ret = 0;
3053
3054 /* Setup maximum charger current and voltage for ABB cut2.0 */
3055 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3056 ret = abx500_set_register_interruptible(di->dev,
3057 AB8500_CHARGER,
3058 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3059 if (ret) {
3060 dev_err(di->dev,
3061 "failed to set CH_VOLT_LVL_MAX_REG\n");
3062 goto out;
3063 }
3064
3065 ret = abx500_set_register_interruptible(di->dev,
3066 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3067 CH_OP_CUR_LVL_1P6);
3068 if (ret) {
3069 dev_err(di->dev,
3070 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3071 goto out;
3072 }
3073 }
3074
3075 if (is_ab8505_2p0(di->parent))
3076 ret = abx500_mask_and_set_register_interruptible(di->dev,
3077 AB8500_CHARGER,
3078 AB8500_USBCH_CTRL2_REG,
3079 VBUS_AUTO_IN_CURR_LIM_ENA,
3080 VBUS_AUTO_IN_CURR_LIM_ENA);
3081 else
3082 /*
3083 * VBUS OVV set to 6.3V and enable automatic current limitation
3084 */
3085 ret = abx500_set_register_interruptible(di->dev,
3086 AB8500_CHARGER,
3087 AB8500_USBCH_CTRL2_REG,
3088 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
3089 if (ret) {
3090 dev_err(di->dev,
3091 "failed to set automatic current limitation\n");
3092 goto out;
3093 }
3094
3095 /* Enable main watchdog in OTP */
3096 ret = abx500_set_register_interruptible(di->dev,
3097 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3098 if (ret) {
3099 dev_err(di->dev, "failed to enable main WD in OTP\n");
3100 goto out;
3101 }
3102
3103 /* Enable main watchdog */
3104 ret = abx500_set_register_interruptible(di->dev,
3105 AB8500_SYS_CTRL2_BLOCK,
3106 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3107 if (ret) {
3108 dev_err(di->dev, "failed to enable main watchdog\n");
3109 goto out;
3110 }
3111
3112 /*
3113 * Due to internal synchronisation, Enable and Kick watchdog bits
3114 * cannot be enabled in a single write.
3115 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3116 * between writing Enable then Kick bits.
3117 */
3118 udelay(63);
3119
3120 /* Kick main watchdog */
3121 ret = abx500_set_register_interruptible(di->dev,
3122 AB8500_SYS_CTRL2_BLOCK,
3123 AB8500_MAIN_WDOG_CTRL_REG,
3124 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3125 if (ret) {
3126 dev_err(di->dev, "failed to kick main watchdog\n");
3127 goto out;
3128 }
3129
3130 /* Disable main watchdog */
3131 ret = abx500_set_register_interruptible(di->dev,
3132 AB8500_SYS_CTRL2_BLOCK,
3133 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3134 if (ret) {
3135 dev_err(di->dev, "failed to disable main watchdog\n");
3136 goto out;
3137 }
3138
3139 /* Set watchdog timeout */
3140 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3141 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3142 if (ret) {
3143 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3144 goto out;
3145 }
3146
3147 ret = ab8500_charger_led_en(di, false);
3148 if (ret < 0) {
3149 dev_err(di->dev, "failed to disable LED\n");
3150 goto out;
3151 }
3152
3153 ret = abx500_set_register_interruptible(di->dev,
3154 AB8500_RTC,
3155 AB8500_RTC_BACKUP_CHG_REG,
3156 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
3157 if (ret) {
3158 dev_err(di->dev, "failed to setup backup battery charging\n");
3159 goto out;
3160 }
3161
3162 /* Enable backup battery charging */
3163 ret = abx500_mask_and_set_register_interruptible(di->dev,
3164 AB8500_RTC, AB8500_RTC_CTRL_REG,
3165 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3166 if (ret < 0) {
3167 dev_err(di->dev, "%s mask and set failed\n", __func__);
3168 goto out;
3169 }
3170
3171 out:
3172 return ret;
3173 }
3174
3175 /*
3176 * ab8500 charger driver interrupts and their respective isr
3177 */
3178 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3179 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3180 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3181 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3182 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3183 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3184 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3185 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3186 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3187 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3188 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3189 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3190 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3191 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
3192 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
3193 };
3194
ab8500_charger_usb_notifier_call(struct notifier_block * nb,unsigned long event,void * power)3195 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3196 unsigned long event, void *power)
3197 {
3198 struct ab8500_charger *di =
3199 container_of(nb, struct ab8500_charger, nb);
3200 enum ab8500_usb_state bm_usb_state;
3201 /*
3202 * FIXME: it appears the AB8500 PHY never sends what it should here.
3203 * Fix the PHY driver to properly notify the desired current.
3204 * Also broadcast microampere and not milliampere.
3205 */
3206 unsigned mA = *((unsigned *)power);
3207
3208 if (event != USB_EVENT_VBUS) {
3209 dev_dbg(di->dev, "not a standard host, returning\n");
3210 return NOTIFY_DONE;
3211 }
3212
3213 /* TODO: State is fabricate here. See if charger really needs USB
3214 * state or if mA is enough
3215 */
3216 if ((di->usb_state.usb_current_ua == 2000) && (mA > 2))
3217 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3218 else if (mA == 0)
3219 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3220 else if (mA == 2)
3221 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3222 else if (mA >= 8) /* 8, 100, 500 */
3223 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3224 else /* Should never occur */
3225 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3226
3227 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3228 __func__, bm_usb_state, mA);
3229
3230 spin_lock(&di->usb_state.usb_lock);
3231 di->usb_state.state_tmp = bm_usb_state;
3232 /* FIXME: broadcast ua instead, see above */
3233 di->usb_state.usb_current_tmp_ua = mA * 1000;
3234 spin_unlock(&di->usb_state.usb_lock);
3235
3236 /*
3237 * wait for some time until you get updates from the usb stack
3238 * and negotiations are completed
3239 */
3240 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
3241
3242 return NOTIFY_OK;
3243 }
3244
ab8500_charger_resume(struct device * dev)3245 static int __maybe_unused ab8500_charger_resume(struct device *dev)
3246 {
3247 int ret;
3248 struct ab8500_charger *di = dev_get_drvdata(dev);
3249
3250 /*
3251 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3252 * logic. That means we have to continuously kick the charger
3253 * watchdog even when no charger is connected. This is only
3254 * valid once the AC charger has been enabled. This is
3255 * a bug that is not handled by the algorithm and the
3256 * watchdog have to be kicked by the charger driver
3257 * when the AC charger is disabled
3258 */
3259 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3260 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3261 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3262 if (ret)
3263 dev_err(di->dev, "Failed to kick WD!\n");
3264
3265 /* If not already pending start a new timer */
3266 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3267 round_jiffies(WD_KICK_INTERVAL));
3268 }
3269
3270 /* If we still have a HW failure, schedule a new check */
3271 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3272 queue_delayed_work(di->charger_wq,
3273 &di->check_hw_failure_work, 0);
3274 }
3275
3276 if (di->flags.vbus_drop_end)
3277 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3278
3279 return 0;
3280 }
3281
ab8500_charger_suspend(struct device * dev)3282 static int __maybe_unused ab8500_charger_suspend(struct device *dev)
3283 {
3284 struct ab8500_charger *di = dev_get_drvdata(dev);
3285
3286 /* Cancel any pending jobs */
3287 cancel_delayed_work(&di->check_hw_failure_work);
3288 cancel_delayed_work(&di->vbus_drop_end_work);
3289
3290 flush_delayed_work(&di->attach_work);
3291 flush_delayed_work(&di->usb_charger_attached_work);
3292 flush_delayed_work(&di->ac_charger_attached_work);
3293 flush_delayed_work(&di->check_usbchgnotok_work);
3294 flush_delayed_work(&di->check_vbat_work);
3295 flush_delayed_work(&di->kick_wd_work);
3296
3297 flush_work(&di->usb_link_status_work);
3298 flush_work(&di->ac_work);
3299 flush_work(&di->detect_usb_type_work);
3300
3301 if (atomic_read(&di->current_stepping_sessions))
3302 return -EAGAIN;
3303
3304 return 0;
3305 }
3306
3307 static char *supply_interface[] = {
3308 "ab8500_chargalg",
3309 "ab8500_fg",
3310 "ab8500_btemp",
3311 };
3312
3313 static const struct power_supply_desc ab8500_ac_chg_desc = {
3314 .name = "ab8500_ac",
3315 .type = POWER_SUPPLY_TYPE_MAINS,
3316 .properties = ab8500_charger_ac_props,
3317 .num_properties = ARRAY_SIZE(ab8500_charger_ac_props),
3318 .get_property = ab8500_charger_ac_get_property,
3319 };
3320
3321 static const struct power_supply_desc ab8500_usb_chg_desc = {
3322 .name = "ab8500_usb",
3323 .type = POWER_SUPPLY_TYPE_USB,
3324 .properties = ab8500_charger_usb_props,
3325 .num_properties = ARRAY_SIZE(ab8500_charger_usb_props),
3326 .get_property = ab8500_charger_usb_get_property,
3327 };
3328
ab8500_charger_bind(struct device * dev)3329 static int ab8500_charger_bind(struct device *dev)
3330 {
3331 struct ab8500_charger *di = dev_get_drvdata(dev);
3332 int ch_stat;
3333 int ret;
3334
3335 /* Create a work queue for the charger */
3336 di->charger_wq = alloc_ordered_workqueue("ab8500_charger_wq",
3337 WQ_MEM_RECLAIM);
3338 if (di->charger_wq == NULL) {
3339 dev_err(dev, "failed to create work queue\n");
3340 return -ENOMEM;
3341 }
3342
3343 ch_stat = ab8500_charger_detect_chargers(di, false);
3344
3345 if (ch_stat & AC_PW_CONN) {
3346 if (is_ab8500(di->parent))
3347 queue_delayed_work(di->charger_wq,
3348 &di->ac_charger_attached_work,
3349 HZ);
3350 }
3351 if (ch_stat & USB_PW_CONN) {
3352 if (is_ab8500(di->parent))
3353 queue_delayed_work(di->charger_wq,
3354 &di->usb_charger_attached_work,
3355 HZ);
3356 di->vbus_detected = true;
3357 di->vbus_detected_start = true;
3358 queue_work(di->charger_wq,
3359 &di->detect_usb_type_work);
3360 }
3361
3362 ret = component_bind_all(dev, di);
3363 if (ret) {
3364 dev_err(dev, "can't bind component devices\n");
3365 destroy_workqueue(di->charger_wq);
3366 return ret;
3367 }
3368
3369 return 0;
3370 }
3371
ab8500_charger_unbind(struct device * dev)3372 static void ab8500_charger_unbind(struct device *dev)
3373 {
3374 struct ab8500_charger *di = dev_get_drvdata(dev);
3375 int ret;
3376
3377 /* Disable AC charging */
3378 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3379
3380 /* Disable USB charging */
3381 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3382
3383 /* Backup battery voltage and current disable */
3384 ret = abx500_mask_and_set_register_interruptible(di->dev,
3385 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3386 if (ret < 0)
3387 dev_err(di->dev, "%s mask and set failed\n", __func__);
3388
3389 /* Delete the work queue */
3390 destroy_workqueue(di->charger_wq);
3391
3392 /* Unbind fg, btemp, algorithm */
3393 component_unbind_all(dev, di);
3394 }
3395
3396 static const struct component_master_ops ab8500_charger_comp_ops = {
3397 .bind = ab8500_charger_bind,
3398 .unbind = ab8500_charger_unbind,
3399 };
3400
3401 static struct platform_driver *const ab8500_charger_component_drivers[] = {
3402 &ab8500_fg_driver,
3403 &ab8500_btemp_driver,
3404 &ab8500_chargalg_driver,
3405 };
3406
ab8500_charger_probe(struct platform_device * pdev)3407 static int ab8500_charger_probe(struct platform_device *pdev)
3408 {
3409 struct device *dev = &pdev->dev;
3410 struct device_node *np = dev->of_node;
3411 struct component_match *match = NULL;
3412 struct power_supply_config ac_psy_cfg = {}, usb_psy_cfg = {};
3413 struct ab8500_charger *di;
3414 int charger_status;
3415 int i, irq;
3416 int ret;
3417
3418 di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
3419 if (!di)
3420 return -ENOMEM;
3421
3422 di->bm = &ab8500_bm_data;
3423
3424 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3425
3426 /* get parent data */
3427 di->dev = dev;
3428 di->parent = dev_get_drvdata(pdev->dev.parent);
3429
3430 /* Get ADC channels */
3431 if (!is_ab8505(di->parent)) {
3432 di->adc_main_charger_v = devm_iio_channel_get(dev, "main_charger_v");
3433 if (IS_ERR(di->adc_main_charger_v)) {
3434 ret = dev_err_probe(dev, PTR_ERR(di->adc_main_charger_v),
3435 "failed to get ADC main charger voltage\n");
3436 return ret;
3437 }
3438 di->adc_main_charger_c = devm_iio_channel_get(dev, "main_charger_c");
3439 if (IS_ERR(di->adc_main_charger_c)) {
3440 ret = dev_err_probe(dev, PTR_ERR(di->adc_main_charger_c),
3441 "failed to get ADC main charger current\n");
3442 return ret;
3443 }
3444 }
3445 di->adc_vbus_v = devm_iio_channel_get(dev, "vbus_v");
3446 if (IS_ERR(di->adc_vbus_v)) {
3447 ret = dev_err_probe(dev, PTR_ERR(di->adc_vbus_v),
3448 "failed to get ADC USB charger voltage\n");
3449 return ret;
3450 }
3451 di->adc_usb_charger_c = devm_iio_channel_get(dev, "usb_charger_c");
3452 if (IS_ERR(di->adc_usb_charger_c)) {
3453 ret = dev_err_probe(dev, PTR_ERR(di->adc_usb_charger_c),
3454 "failed to get ADC USB charger current\n");
3455 return ret;
3456 }
3457
3458 /*
3459 * VDD ADC supply needs to be enabled from this driver when there
3460 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3461 * interrupts during charging
3462 */
3463 di->regu = devm_regulator_get(dev, "vddadc");
3464 if (IS_ERR(di->regu)) {
3465 ret = PTR_ERR(di->regu);
3466 dev_err(dev, "failed to get vddadc regulator\n");
3467 return ret;
3468 }
3469
3470 /* Request interrupts */
3471 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3472 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3473 if (irq < 0)
3474 return irq;
3475
3476 ret = devm_request_threaded_irq(dev,
3477 irq, NULL, ab8500_charger_irq[i].isr,
3478 IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
3479 ab8500_charger_irq[i].name, di);
3480
3481 if (ret != 0) {
3482 dev_err(dev, "failed to request %s IRQ %d: %d\n"
3483 , ab8500_charger_irq[i].name, irq, ret);
3484 return ret;
3485 }
3486 dev_dbg(dev, "Requested %s IRQ %d: %d\n",
3487 ab8500_charger_irq[i].name, irq, ret);
3488 }
3489
3490 /* initialize lock */
3491 spin_lock_init(&di->usb_state.usb_lock);
3492 mutex_init(&di->usb_ipt_crnt_lock);
3493
3494 di->autopower = false;
3495 di->invalid_charger_detect_state = 0;
3496
3497 /* AC and USB supply config */
3498 ac_psy_cfg.of_node = np;
3499 ac_psy_cfg.supplied_to = supply_interface;
3500 ac_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3501 ac_psy_cfg.drv_data = &di->ac_chg;
3502 usb_psy_cfg.of_node = np;
3503 usb_psy_cfg.supplied_to = supply_interface;
3504 usb_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3505 usb_psy_cfg.drv_data = &di->usb_chg;
3506
3507 /* AC supply */
3508 /* ux500_charger sub-class */
3509 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
3510 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
3511 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3512 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3513 di->ac_chg.max_out_volt_uv = ab8500_charger_voltage_map[
3514 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3515 di->ac_chg.max_out_curr_ua =
3516 ab8500_charge_output_curr_map[ARRAY_SIZE(ab8500_charge_output_curr_map) - 1];
3517 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
3518 /*
3519 * The AB8505 only supports USB charging. If we are not the
3520 * AB8505, register an AC charger.
3521 *
3522 * TODO: if this should be opt-in, add DT properties for this.
3523 */
3524 if (!is_ab8505(di->parent))
3525 di->ac_chg.enabled = true;
3526
3527 /* USB supply */
3528 /* ux500_charger sub-class */
3529 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
3530 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
3531 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3532 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3533 di->usb_chg.max_out_volt_uv = ab8500_charger_voltage_map[
3534 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3535 di->usb_chg.max_out_curr_ua =
3536 ab8500_charge_output_curr_map[ARRAY_SIZE(ab8500_charge_output_curr_map) - 1];
3537 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
3538 di->usb_state.usb_current_ua = -1;
3539
3540 mutex_init(&di->charger_attached_mutex);
3541
3542 /* Init work for HW failure check */
3543 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
3544 ab8500_charger_check_hw_failure_work);
3545 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
3546 ab8500_charger_check_usbchargernotok_work);
3547
3548 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3549 ab8500_charger_ac_attached_work);
3550 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3551 ab8500_charger_usb_attached_work);
3552
3553 /*
3554 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3555 * logic. That means we have to continuously kick the charger
3556 * watchdog even when no charger is connected. This is only
3557 * valid once the AC charger has been enabled. This is
3558 * a bug that is not handled by the algorithm and the
3559 * watchdog have to be kicked by the charger driver
3560 * when the AC charger is disabled
3561 */
3562 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
3563 ab8500_charger_kick_watchdog_work);
3564
3565 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
3566 ab8500_charger_check_vbat_work);
3567
3568 INIT_DELAYED_WORK(&di->attach_work,
3569 ab8500_charger_usb_link_attach_work);
3570
3571 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3572 ab8500_charger_usb_state_changed_work);
3573
3574 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3575 ab8500_charger_vbus_drop_end_work);
3576
3577 /* Init work for charger detection */
3578 INIT_WORK(&di->usb_link_status_work,
3579 ab8500_charger_usb_link_status_work);
3580 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3581 INIT_WORK(&di->detect_usb_type_work,
3582 ab8500_charger_detect_usb_type_work);
3583
3584 /* Init work for checking HW status */
3585 INIT_WORK(&di->check_main_thermal_prot_work,
3586 ab8500_charger_check_main_thermal_prot_work);
3587 INIT_WORK(&di->check_usb_thermal_prot_work,
3588 ab8500_charger_check_usb_thermal_prot_work);
3589
3590
3591 /* Initialize OVV, and other registers */
3592 ret = ab8500_charger_init_hw_registers(di);
3593 if (ret) {
3594 dev_err(dev, "failed to initialize ABB registers\n");
3595 return ret;
3596 }
3597
3598 /* Register AC charger class */
3599 if (di->ac_chg.enabled) {
3600 di->ac_chg.psy = devm_power_supply_register(dev,
3601 &ab8500_ac_chg_desc,
3602 &ac_psy_cfg);
3603 if (IS_ERR(di->ac_chg.psy)) {
3604 dev_err(dev, "failed to register AC charger\n");
3605 return PTR_ERR(di->ac_chg.psy);
3606 }
3607 }
3608
3609 /* Register USB charger class */
3610 di->usb_chg.psy = devm_power_supply_register(dev,
3611 &ab8500_usb_chg_desc,
3612 &usb_psy_cfg);
3613 if (IS_ERR(di->usb_chg.psy)) {
3614 dev_err(dev, "failed to register USB charger\n");
3615 return PTR_ERR(di->usb_chg.psy);
3616 }
3617
3618 /*
3619 * Check what battery we have, since we always have the USB
3620 * psy, use that as a handle.
3621 */
3622 ret = ab8500_bm_of_probe(di->usb_chg.psy, di->bm);
3623 if (ret)
3624 return dev_err_probe(dev, ret,
3625 "failed to get battery information\n");
3626
3627 /* Identify the connected charger types during startup */
3628 charger_status = ab8500_charger_detect_chargers(di, true);
3629 if (charger_status & AC_PW_CONN) {
3630 di->ac.charger_connected = 1;
3631 di->ac_conn = true;
3632 ab8500_power_supply_changed(di, di->ac_chg.psy);
3633 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
3634 }
3635
3636 platform_set_drvdata(pdev, di);
3637
3638 /* Create something that will match the subdrivers when we bind */
3639 for (i = 0; i < ARRAY_SIZE(ab8500_charger_component_drivers); i++) {
3640 struct device_driver *drv = &ab8500_charger_component_drivers[i]->driver;
3641 struct device *p = NULL, *d;
3642
3643 while ((d = platform_find_device_by_driver(p, drv))) {
3644 put_device(p);
3645 component_match_add(dev, &match, component_compare_dev, d);
3646 p = d;
3647 }
3648 put_device(p);
3649 }
3650 if (!match) {
3651 dev_err(dev, "no matching components\n");
3652 ret = -ENODEV;
3653 goto remove_ab8500_bm;
3654 }
3655 if (IS_ERR(match)) {
3656 dev_err(dev, "could not create component match\n");
3657 ret = PTR_ERR(match);
3658 goto remove_ab8500_bm;
3659 }
3660
3661 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3662 if (IS_ERR_OR_NULL(di->usb_phy)) {
3663 dev_err(dev, "failed to get usb transceiver\n");
3664 ret = -EINVAL;
3665 goto remove_ab8500_bm;
3666 }
3667 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3668 ret = usb_register_notifier(di->usb_phy, &di->nb);
3669 if (ret) {
3670 dev_err(dev, "failed to register usb notifier\n");
3671 goto put_usb_phy;
3672 }
3673
3674 ret = component_master_add_with_match(&pdev->dev,
3675 &ab8500_charger_comp_ops,
3676 match);
3677 if (ret) {
3678 dev_err(dev, "failed to add component master\n");
3679 goto free_notifier;
3680 }
3681
3682 return 0;
3683
3684 free_notifier:
3685 usb_unregister_notifier(di->usb_phy, &di->nb);
3686 put_usb_phy:
3687 usb_put_phy(di->usb_phy);
3688 remove_ab8500_bm:
3689 ab8500_bm_of_remove(di->usb_chg.psy, di->bm);
3690 return ret;
3691 }
3692
ab8500_charger_remove(struct platform_device * pdev)3693 static void ab8500_charger_remove(struct platform_device *pdev)
3694 {
3695 struct ab8500_charger *di = platform_get_drvdata(pdev);
3696
3697 component_master_del(&pdev->dev, &ab8500_charger_comp_ops);
3698
3699 usb_unregister_notifier(di->usb_phy, &di->nb);
3700 ab8500_bm_of_remove(di->usb_chg.psy, di->bm);
3701 usb_put_phy(di->usb_phy);
3702 }
3703
3704 static SIMPLE_DEV_PM_OPS(ab8500_charger_pm_ops, ab8500_charger_suspend, ab8500_charger_resume);
3705
3706 static const struct of_device_id ab8500_charger_match[] = {
3707 { .compatible = "stericsson,ab8500-charger", },
3708 { },
3709 };
3710 MODULE_DEVICE_TABLE(of, ab8500_charger_match);
3711
3712 static struct platform_driver ab8500_charger_driver = {
3713 .probe = ab8500_charger_probe,
3714 .remove_new = ab8500_charger_remove,
3715 .driver = {
3716 .name = "ab8500-charger",
3717 .of_match_table = ab8500_charger_match,
3718 .pm = &ab8500_charger_pm_ops,
3719 },
3720 };
3721
ab8500_charger_init(void)3722 static int __init ab8500_charger_init(void)
3723 {
3724 int ret;
3725
3726 ret = platform_register_drivers(ab8500_charger_component_drivers,
3727 ARRAY_SIZE(ab8500_charger_component_drivers));
3728 if (ret)
3729 return ret;
3730
3731 ret = platform_driver_register(&ab8500_charger_driver);
3732 if (ret) {
3733 platform_unregister_drivers(ab8500_charger_component_drivers,
3734 ARRAY_SIZE(ab8500_charger_component_drivers));
3735 return ret;
3736 }
3737
3738 return 0;
3739 }
3740
ab8500_charger_exit(void)3741 static void __exit ab8500_charger_exit(void)
3742 {
3743 platform_unregister_drivers(ab8500_charger_component_drivers,
3744 ARRAY_SIZE(ab8500_charger_component_drivers));
3745 platform_driver_unregister(&ab8500_charger_driver);
3746 }
3747
3748 module_init(ab8500_charger_init);
3749 module_exit(ab8500_charger_exit);
3750
3751 MODULE_LICENSE("GPL v2");
3752 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3753 MODULE_ALIAS("platform:ab8500-charger");
3754 MODULE_DESCRIPTION("AB8500 charger management driver");
3755