blob: fb56ec755ce898212f9d355e3ff76614cf200686 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * Summit Microelectronics SMB347 Battery Charger Driver
3 *
4 * Copyright (C) 2011, Intel Corporation
5 *
6 * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/debugfs.h>
15#include <linux/gpio.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/i2c.h>
21#include <linux/mutex.h>
22#include <linux/power_supply.h>
23#include <linux/power/smb347-charger.h>
24#include <linux/seq_file.h>
25#include <linux/delay.h>
26
27/*
28 * Configuration registers. These are mirrored to volatile RAM and can be
29 * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be
30 * reloaded from non-volatile registers after POR.
31 */
32#define CFG_CHARGE_CURRENT 0x00
33#define CFG_CHARGE_CURRENT_FCC_MASK 0xe0
34#define CFG_CHARGE_CURRENT_FCC_SHIFT 5
35#define CFG_CHARGE_CURRENT_PCC_MASK 0x18
36#define CFG_CHARGE_CURRENT_PCC_SHIFT 3
37#define CFG_CHARGE_CURRENT_TC_MASK 0x07
38#define CFG_CURRENT_LIMIT 0x01
39#define CFG_CURRENT_LIMIT_DC_MASK 0xf0
40#define CFG_CURRENT_LIMIT_DC_SHIFT 4
41#define CFG_CURRENT_LIMIT_USB_MASK 0x0f
42#define CFG_VARIOUS_FUNCTION 0x02
43#define CFG_INPUT_SOURCE_PRIORITY BIT(2)
44#define CFG_FLOAT_VOLTAGE 0x03
45#define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0
46#define CFG_FLOAT_VOLTAGE_MASK 0x3F
47#define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6
48#define CFG_CHARGE_CONTROL 0x04
49#define CFG_AUTOMATIC_RECHARGE_DISABLE BIT(7)
50#define CFG_STAT 0x05
51#define CFG_STAT_DISABLED BIT(5)
52#define CFG_STAT_ACTIVE_HIGH BIT(7)
53#define CFG_PIN 0x06
54#define CFG_PIN_EN_CTRL_MASK 0x60
55#define CFG_PIN_USB_MODE_CTRL BIT(4)
56#define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40
57#define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60
58#define CFG_PIN_EN_APSD_IRQ BIT(1)
59#define CFG_PIN_EN_CHARGER_ERROR BIT(2)
60#define CFG_THERM 0x07
61#define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03
62#define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0
63#define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c
64#define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2
65#define CFG_THERM_MONITOR_DISABLED BIT(4)
66#define CFG_SYSOK 0x08
67#define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2)
68#define CFG_OTHER 0x09
69#define CFG_OTHER_RID_MASK 0xc0
70#define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0
71#define CFG_OTG 0x0a
72#define CFG_OTG_TEMP_THRESHOLD_MASK 0x30
73#define CFG_OTG_TEMP_THRESHOLD_SHIFT 4
74#define CFG_OTG_CC_COMPENSATION_MASK 0xc0
75#define CFG_OTG_CC_COMPENSATION_SHIFT 6
76#define CFG_TEMP_LIMIT 0x0b
77#define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03
78#define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0
79#define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c
80#define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2
81#define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30
82#define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4
83#define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0
84#define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6
85#define CFG_FAULT_IRQ 0x0c
86#define CFG_FAULT_IRQ_DCIN_UV BIT(2)
87#define CFG_STATUS_IRQ 0x0d
88#define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4)
89#define CFG_ADDRESS 0x0e
90
91/* Command registers */
92#define CMD_A 0x30
93#define CMD_A_CHG_ENABLED BIT(1)
94#define CMD_A_SUSPEND_ENABLED BIT(2)
95#define CMD_A_OTG_ENABLE BIT(4)
96#define CMD_A_ALLOW_WRITE BIT(7)
97#define CMD_B 0x31
98#define CMD_B_POR BIT(7)
99#define CMD_B_USB59_MODE BIT(1)
100#define CMD_B_HC_MODE BIT(0)
101#define CMD_C 0x33
102
103/* Interrupt Status registers */
104#define IRQSTAT_A 0x35
105#define IRQSTAT_C 0x37
106#define IRQSTAT_C_TERMINATION_STAT BIT(0)
107#define IRQSTAT_C_TERMINATION_IRQ BIT(1)
108#define IRQSTAT_C_TAPER_IRQ BIT(3)
109#define IRQSTAT_E 0x39
110#define IRQSTAT_E_USBIN_UV_STAT BIT(0)
111#define IRQSTAT_E_USBIN_UV_IRQ BIT(1)
112#define IRQSTAT_E_DCIN_UV_STAT BIT(4)
113#define IRQSTAT_E_DCIN_UV_IRQ BIT(5)
114#define IRQSTAT_F 0x3a
115
116/* Status registers */
117#define STAT_A 0x3b
118#define STAT_A_FLOAT_VOLTAGE_MASK 0x3f
119#define STAT_B 0x3c
120#define STAT_C 0x3d
121#define STAT_C_CHG_ENABLED BIT(0)
122#define STAT_C_CHG_STATUS BIT(5)
123#define STAT_C_CHG_MASK 0x06
124#define STAT_C_CHG_SHIFT 1
125#define STAT_C_CHARGER_ERROR BIT(6)
126#define STAT_E 0x3f
127
128/**
129 * struct smb347_charger - smb347 charger instance
130 * @lock: protects concurrent access to online variables
131 * @client: pointer to i2c client
132 * @mains: power_supply instance for AC/DC power
133 * @usb: power_supply instance for USB power
134 * @battery: power_supply instance for battery
135 * @mains_online: is AC/DC input connected
136 * @usb_online: is USB input connected
137 * @charging_enabled: is charging enabled
138 * @dentry: for debugfs
139 * @pdata: pointer to platform data
140 */
141struct smb347_charger {
142 struct mutex lock;
143 struct i2c_client *client;
144 struct power_supply mains;
145 struct power_supply usb;
146 struct power_supply battery;
147 bool mains_online;
148 bool usb_online;
149 bool charging_enabled;
150 unsigned int mains_current_limit;
151 bool usb_hc_mode;
152 bool usb_otg_enabled;
153 bool is_fully_charged;
154 int en_gpio;
155 struct dentry *dentry;
156 const struct smb347_charger_platform_data *pdata;
157};
158
159/* Fast charge current in uA */
160static const unsigned int fcc_tbl[] = {
161 700000,
162 900000,
163 1200000,
164 1500000,
165 1800000,
166 2000000,
167 2200000,
168 2500000,
169};
170
171/* Pre-charge current in uA */
172static const unsigned int pcc_tbl[] = {
173 100000,
174 150000,
175 200000,
176 250000,
177};
178
179/* Termination current in uA */
180static const unsigned int tc_tbl[] = {
181 37500,
182 50000,
183 100000,
184 150000,
185 200000,
186 250000,
187 500000,
188 600000,
189};
190
191/* Input current limit in uA */
192static const unsigned int icl_tbl[] = {
193 300000,
194 500000,
195 700000,
196 900000,
197 1200000,
198 1500000,
199 1800000,
200 2000000,
201 2200000,
202 2500000,
203};
204
205/* Charge current compensation in uA */
206static const unsigned int ccc_tbl[] = {
207 250000,
208 700000,
209 900000,
210 1200000,
211};
212
213/* Convert register value to current using lookup table */
214static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val)
215{
216 if (val >= size)
217 return -EINVAL;
218 return tbl[val];
219}
220
221/* Convert current to register value using lookup table */
222static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
223{
224 size_t i;
225
226 for (i = 0; i < size; i++)
227 if (val < tbl[i])
228 break;
229 return i > 0 ? i - 1 : -EINVAL;
230}
231
232static int smb347_read(struct smb347_charger *smb, u8 reg)
233{
234 int ret;
235
236 ret = i2c_smbus_read_byte_data(smb->client, reg);
237 if (ret < 0)
238 dev_warn(&smb->client->dev, "failed to read reg 0x%x: %d\n",
239 reg, ret);
240 return ret;
241}
242
243static int smb347_write(struct smb347_charger *smb, u8 reg, u8 val)
244{
245 int ret;
246
247 ret = i2c_smbus_write_byte_data(smb->client, reg, val);
248 if (ret < 0)
249 dev_warn(&smb->client->dev, "failed to write reg 0x%x: %d\n",
250 reg, ret);
251 return ret;
252}
253
254/**
255 * smb347_update_status - updates the charging status
256 * @smb: pointer to smb347 charger instance
257 *
258 * Function checks status of the charging and updates internal state
259 * accordingly. Returns %0 if there is no change in status, %1 if the
260 * status has changed and negative errno in case of failure.
261 */
262static int smb347_update_status(struct smb347_charger *smb)
263{
264 bool usb = false;
265 bool dc = false;
266 int ret;
267
268 ret = smb347_read(smb, IRQSTAT_E);
269 if (ret < 0)
270 return ret;
271
272 /*
273 * Dc and usb are set depending on whether they are enabled in
274 * platform data _and_ whether corresponding undervoltage is set.
275 */
276 if (smb->pdata->use_mains)
277 dc = !(ret & IRQSTAT_E_DCIN_UV_STAT);
278 if (smb->pdata->use_usb)
279 usb = !(ret & IRQSTAT_E_USBIN_UV_STAT);
280
281 mutex_lock(&smb->lock);
282 ret = smb->mains_online != dc || smb->usb_online != usb;
283 smb->mains_online = dc;
284 smb->usb_online = usb;
285 mutex_unlock(&smb->lock);
286
287 return ret;
288}
289
290/*
291 * smb347_is_online - returns whether input power source is connected
292 * @smb: pointer to smb347 charger instance
293 *
294 * Returns %true if input power source is connected. Note that this is
295 * dependent on what platform has configured for usable power sources. For
296 * example if USB is disabled, this will return %false even if the USB
297 * cable is connected.
298 */
299static bool smb347_is_online(struct smb347_charger *smb)
300{
301 bool ret;
302
303 mutex_lock(&smb->lock);
304 ret = smb->usb_online || smb->mains_online;
305 mutex_unlock(&smb->lock);
306
307 return ret;
308}
309
310/**
311 * smb347_charging_status - returns status of charging
312 * @smb: pointer to smb347 charger instance
313 *
314 * Function returns charging status. %0 means no charging is in progress,
315 * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
316 */
317static int smb347_charging_status(struct smb347_charger *smb)
318{
319 int ret;
320
321 if (!smb347_is_online(smb))
322 return 0;
323
324 ret = smb347_read(smb, STAT_C);
325 if (ret < 0)
326 return 0;
327
328 return (ret & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT;
329}
330
331static int smb347_charging_set(struct smb347_charger *smb, bool enable)
332{
333 int ret = 0;
334
335 if (enable && !smb->charging_enabled)
336 smb->is_fully_charged = false;
337
338 if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) {
339 smb->charging_enabled = enable;
340
341 if (smb->en_gpio)
342 gpio_set_value(
343 smb->en_gpio,
344 (smb->pdata->enable_control ==
345 SMB347_CHG_ENABLE_PIN_ACTIVE_LOW) ^ enable);
346 return 0;
347 }
348
349 mutex_lock(&smb->lock);
350 if (smb->charging_enabled != enable) {
351 ret = smb347_read(smb, CMD_A);
352 if (ret < 0)
353 goto out;
354
355 smb->charging_enabled = enable;
356
357 if (enable)
358 ret |= CMD_A_CHG_ENABLED;
359 else
360 ret &= ~CMD_A_CHG_ENABLED;
361
362 ret = smb347_write(smb, CMD_A, ret);
363 }
364out:
365 mutex_unlock(&smb->lock);
366 return ret;
367}
368
369static inline int smb347_charging_enable(struct smb347_charger *smb)
370{
371 return smb347_charging_set(smb, true);
372}
373
374static inline int smb347_charging_disable(struct smb347_charger *smb)
375{
376 return smb347_charging_set(smb, false);
377}
378
379static int smb347_update_online(struct smb347_charger *smb)
380{
381 int ret;
382
383 /*
384 * Depending on whether valid power source is connected or not, we
385 * disable or enable the charging. We do it manually because it
386 * depends on how the platform has configured the valid inputs.
387 */
388 if (smb347_is_online(smb)) {
389 ret = smb347_charging_enable(smb);
390 if (ret < 0)
391 dev_err(&smb->client->dev,
392 "failed to enable charging\n");
393 } else {
394 ret = smb347_charging_disable(smb);
395 if (ret < 0)
396 dev_err(&smb->client->dev,
397 "failed to disable charging\n");
398 }
399
400 return ret;
401}
402
403static int smb347_set_charge_current(struct smb347_charger *smb)
404{
405 int ret, val;
406
407 ret = smb347_read(smb, CFG_CHARGE_CURRENT);
408 if (ret < 0)
409 return ret;
410
411 if (smb->pdata->max_charge_current) {
412 val = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl),
413 smb->pdata->max_charge_current);
414 if (val < 0)
415 return val;
416
417 ret &= ~CFG_CHARGE_CURRENT_FCC_MASK;
418 ret |= val << CFG_CHARGE_CURRENT_FCC_SHIFT;
419 }
420
421 if (smb->pdata->pre_charge_current) {
422 val = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl),
423 smb->pdata->pre_charge_current);
424 if (val < 0)
425 return val;
426
427 ret &= ~CFG_CHARGE_CURRENT_PCC_MASK;
428 ret |= val << CFG_CHARGE_CURRENT_PCC_SHIFT;
429 }
430
431 if (smb->pdata->termination_current) {
432 val = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl),
433 smb->pdata->termination_current);
434 if (val < 0)
435 return val;
436
437 ret &= ~CFG_CHARGE_CURRENT_TC_MASK;
438 ret |= val;
439 }
440
441 return smb347_write(smb, CFG_CHARGE_CURRENT, ret);
442}
443
444static int smb347_set_current_limits(struct smb347_charger *smb)
445{
446 int ret, val;
447
448 ret = smb347_read(smb, CFG_CURRENT_LIMIT);
449 if (ret < 0)
450 return ret;
451
452 if (smb->mains_current_limit) {
453 val = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
454 smb->mains_current_limit);
455 if (val < 0)
456 return val;
457
458 ret &= ~CFG_CURRENT_LIMIT_DC_MASK;
459 ret |= val << CFG_CURRENT_LIMIT_DC_SHIFT;
460 }
461
462 if (smb->pdata->usb_hc_current_limit) {
463 val = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
464 smb->pdata->usb_hc_current_limit);
465 if (val < 0)
466 return val;
467
468 ret &= ~CFG_CURRENT_LIMIT_USB_MASK;
469 ret |= val;
470 }
471
472 return smb347_write(smb, CFG_CURRENT_LIMIT, ret);
473}
474
475static int smb347_set_voltage_limits(struct smb347_charger *smb)
476{
477 int ret, val;
478
479 ret = smb347_read(smb, CFG_FLOAT_VOLTAGE);
480 if (ret < 0)
481 return ret;
482
483 if (smb->pdata->pre_to_fast_voltage) {
484 val = smb->pdata->pre_to_fast_voltage;
485
486 /* uV */
487 val = clamp_val(val, 2400000, 3000000) - 2400000;
488 val /= 200000;
489
490 ret &= ~CFG_FLOAT_VOLTAGE_THRESHOLD_MASK;
491 ret |= val << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT;
492 }
493
494 if (smb->pdata->max_charge_voltage) {
495 val = smb->pdata->max_charge_voltage;
496
497 /* uV */
498 val = clamp_val(val, 3500000, 4500000) - 3500000;
499 val /= 20000;
500
501 ret &= ~CFG_FLOAT_VOLTAGE_MASK;
502 ret |= val;
503 }
504
505 return smb347_write(smb, CFG_FLOAT_VOLTAGE, ret);
506}
507
508static int smb347_set_temp_limits(struct smb347_charger *smb)
509{
510 bool enable_therm_monitor = false;
511 int ret, val;
512
513 if (smb->pdata->chip_temp_threshold) {
514 val = smb->pdata->chip_temp_threshold;
515
516 /* degree C */
517 val = clamp_val(val, 100, 130) - 100;
518 val /= 10;
519
520 ret = smb347_read(smb, CFG_OTG);
521 if (ret < 0)
522 return ret;
523
524 ret &= ~CFG_OTG_TEMP_THRESHOLD_MASK;
525 ret |= val << CFG_OTG_TEMP_THRESHOLD_SHIFT;
526
527 ret = smb347_write(smb, CFG_OTG, ret);
528 if (ret < 0)
529 return ret;
530 }
531
532 ret = smb347_read(smb, CFG_TEMP_LIMIT);
533 if (ret < 0)
534 return ret;
535
536 if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
537 val = smb->pdata->soft_cold_temp_limit;
538
539 val = clamp_val(val, 0, 15);
540 val /= 5;
541 /* this goes from higher to lower so invert the value */
542 val = ~val & 0x3;
543
544 ret &= ~CFG_TEMP_LIMIT_SOFT_COLD_MASK;
545 ret |= val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT;
546
547 enable_therm_monitor = true;
548 }
549
550 if (smb->pdata->soft_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
551 val = smb->pdata->soft_hot_temp_limit;
552
553 val = clamp_val(val, 40, 55) - 40;
554 val /= 5;
555
556 ret &= ~CFG_TEMP_LIMIT_SOFT_HOT_MASK;
557 ret |= val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT;
558
559 enable_therm_monitor = true;
560 }
561
562 if (smb->pdata->hard_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
563 val = smb->pdata->hard_cold_temp_limit;
564
565 val = clamp_val(val, -5, 10) + 5;
566 val /= 5;
567 /* this goes from higher to lower so invert the value */
568 val = ~val & 0x3;
569
570 ret &= ~CFG_TEMP_LIMIT_HARD_COLD_MASK;
571 ret |= val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT;
572
573 enable_therm_monitor = true;
574 }
575
576 if (smb->pdata->hard_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
577 val = smb->pdata->hard_hot_temp_limit;
578
579 val = clamp_val(val, 50, 65) - 50;
580 val /= 5;
581
582 ret &= ~CFG_TEMP_LIMIT_HARD_HOT_MASK;
583 ret |= val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT;
584
585 enable_therm_monitor = true;
586 }
587
588 ret = smb347_write(smb, CFG_TEMP_LIMIT, ret);
589 if (ret < 0)
590 return ret;
591
592 /*
593 * If any of the temperature limits are set, we also enable the
594 * thermistor monitoring.
595 *
596 * When soft limits are hit, the device will start to compensate
597 * current and/or voltage depending on the configuration.
598 *
599 * When hard limit is hit, the device will suspend charging
600 * depending on the configuration.
601 */
602 if (enable_therm_monitor) {
603 ret = smb347_read(smb, CFG_THERM);
604 if (ret < 0)
605 return ret;
606
607 ret &= ~CFG_THERM_MONITOR_DISABLED;
608
609 ret = smb347_write(smb, CFG_THERM, ret);
610 if (ret < 0)
611 return ret;
612 }
613
614 if (smb->pdata->suspend_on_hard_temp_limit) {
615 ret = smb347_read(smb, CFG_SYSOK);
616 if (ret < 0)
617 return ret;
618
619 ret &= ~CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED;
620
621 ret = smb347_write(smb, CFG_SYSOK, ret);
622 if (ret < 0)
623 return ret;
624 }
625
626 if (smb->pdata->soft_temp_limit_compensation !=
627 SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) {
628 val = smb->pdata->soft_temp_limit_compensation & 0x3;
629
630 ret = smb347_read(smb, CFG_THERM);
631 if (ret < 0)
632 return ret;
633
634 ret &= ~CFG_THERM_SOFT_HOT_COMPENSATION_MASK;
635 ret |= val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT;
636
637 ret &= ~CFG_THERM_SOFT_COLD_COMPENSATION_MASK;
638 ret |= val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT;
639
640 ret = smb347_write(smb, CFG_THERM, ret);
641 if (ret < 0)
642 return ret;
643 }
644
645 if (smb->pdata->charge_current_compensation) {
646 val = current_to_hw(ccc_tbl, ARRAY_SIZE(ccc_tbl),
647 smb->pdata->charge_current_compensation);
648 if (val < 0)
649 return val;
650
651 ret = smb347_read(smb, CFG_OTG);
652 if (ret < 0)
653 return ret;
654
655 ret &= ~CFG_OTG_CC_COMPENSATION_MASK;
656 ret |= (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT;
657
658 ret = smb347_write(smb, CFG_OTG, ret);
659 if (ret < 0)
660 return ret;
661 }
662
663 return ret;
664}
665
666/*
667 * smb347_set_writable - enables/disables writing to non-volatile registers
668 * @smb: pointer to smb347 charger instance
669 *
670 * You can enable/disable writing to the non-volatile configuration
671 * registers by calling this function.
672 *
673 * Returns %0 on success and negative errno in case of failure.
674 */
675static int smb347_set_writable(struct smb347_charger *smb, bool writable)
676{
677 int ret;
678
679 ret = smb347_read(smb, CMD_A);
680 if (ret < 0)
681 return ret;
682
683 if (writable)
684 ret |= CMD_A_ALLOW_WRITE;
685 else
686 ret &= ~CMD_A_ALLOW_WRITE;
687
688 return smb347_write(smb, CMD_A, ret);
689}
690
691static int smb347_irq_set(struct smb347_charger *smb, bool enable)
692{
693 int ret;
694
695 ret = smb347_set_writable(smb, true);
696 if (ret < 0)
697 return ret;
698
699 /*
700 * Enable/disable interrupts for:
701 * - under voltage
702 * - termination current reached
703 * - charger error
704 */
705 if (enable) {
706 ret = smb347_write(smb, CFG_FAULT_IRQ, CFG_FAULT_IRQ_DCIN_UV);
707 if (ret < 0)
708 goto fail;
709
710 ret = smb347_write(smb, CFG_STATUS_IRQ,
711 CFG_STATUS_IRQ_TERMINATION_OR_TAPER);
712 if (ret < 0)
713 goto fail;
714
715 ret = smb347_read(smb, CFG_PIN);
716 if (ret < 0)
717 goto fail;
718
719 ret |= CFG_PIN_EN_CHARGER_ERROR;
720
721 ret = smb347_write(smb, CFG_PIN, ret);
722 } else {
723 ret = smb347_write(smb, CFG_FAULT_IRQ, 0);
724 if (ret < 0)
725 goto fail;
726
727 ret = smb347_write(smb, CFG_STATUS_IRQ, 0);
728 if (ret < 0)
729 goto fail;
730
731 ret = smb347_read(smb, CFG_PIN);
732 if (ret < 0)
733 goto fail;
734
735 ret &= ~CFG_PIN_EN_CHARGER_ERROR;
736
737 ret = smb347_write(smb, CFG_PIN, ret);
738 }
739
740fail:
741 smb347_set_writable(smb, false);
742 return ret;
743}
744
745static inline int smb347_irq_enable(struct smb347_charger *smb)
746{
747 return smb347_irq_set(smb, true);
748}
749
750static inline int smb347_irq_disable(struct smb347_charger *smb)
751{
752 return smb347_irq_set(smb, false);
753}
754
755static irqreturn_t smb347_interrupt(int irq, void *data)
756{
757 struct smb347_charger *smb = data;
758 int stat_c, t;
759 u8 irqstat[6];
760 irqreturn_t ret = IRQ_NONE;
761
762 t = i2c_smbus_read_i2c_block_data(smb->client, IRQSTAT_A, 6, irqstat);
763 if (t < 0) {
764 dev_warn(&smb->client->dev,
765 "reading IRQSTAT registers failed\n");
766 return IRQ_NONE;
767 }
768
769 stat_c = smb347_read(smb, STAT_C);
770 if (stat_c < 0) {
771 dev_warn(&smb->client->dev, "reading STAT_C failed\n");
772 return IRQ_NONE;
773 }
774
775 pr_debug("%s: stat c=%x irq a=%x b=%x c=%x d=%x e=%x f=%x\n",
776 __func__, stat_c, irqstat[0], irqstat[1], irqstat[2],
777 irqstat[3], irqstat[4], irqstat[5]);
778
779 /*
780 * If we get charger error we report the error back to user and
781 * disable charging.
782 */
783 if (stat_c & STAT_C_CHARGER_ERROR) {
784 dev_err(&smb->client->dev,
785 "error in charger, disabling charging\n");
786
787 smb347_charging_disable(smb);
788 power_supply_changed(&smb->battery);
789
790 ret = IRQ_HANDLED;
791 } else if (((stat_c & STAT_C_CHG_STATUS) ||
792 (irqstat[2] & (IRQSTAT_C_TERMINATION_IRQ |
793 IRQSTAT_C_TERMINATION_STAT))) &&
794 !smb->is_fully_charged) {
795 dev_info(&smb->client->dev, "charge terminated\n");
796 smb->is_fully_charged = true;
797 smb347_charging_disable(smb);
798 power_supply_changed(&smb->battery);
799 ret = IRQ_HANDLED;
800 }
801
802 if (irqstat[2] & IRQSTAT_C_TAPER_IRQ)
803 ret = IRQ_HANDLED;
804
805 /*
806 * If we got an under voltage interrupt it means that AC/USB input
807 * was disconnected.
808 */
809 if (irqstat[4] & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ))
810 ret = IRQ_HANDLED;
811
812 if (smb347_update_status(smb) > 0) {
813 smb347_update_online(smb);
814 power_supply_changed(&smb->mains);
815 power_supply_changed(&smb->usb);
816 ret = IRQ_HANDLED;
817 }
818
819 return ret;
820}
821
822static int smb347_irq_init(struct smb347_charger *smb)
823{
824 const struct smb347_charger_platform_data *pdata = smb->pdata;
825 int ret, irq = gpio_to_irq(pdata->irq_gpio);
826
827 ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, smb->client->name);
828 if (ret < 0)
829 goto fail;
830
831 ret = request_threaded_irq(irq, NULL, smb347_interrupt,
832 pdata->disable_stat_interrupts ?
833 IRQF_TRIGGER_RISING | IRQF_ONESHOT :
834 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
835 smb->client->name, smb);
836 if (ret < 0)
837 goto fail_gpio;
838
839 ret = enable_irq_wake(irq);
840 if (ret)
841 pr_err("%s: failed to enable wake on irq %d\n", __func__, irq);
842
843 smb->client->irq = irq;
844 return 0;
845
846fail_gpio:
847 gpio_free(pdata->irq_gpio);
848fail:
849 smb->client->irq = 0;
850 return ret;
851}
852
853static int smb347_hw_init(struct smb347_charger *smb)
854{
855 int ret;
856
857 ret = smb347_set_writable(smb, true);
858 if (ret < 0)
859 return ret;
860
861 /*
862 * Program the platform specific configuration values to the device
863 * first.
864 */
865 ret = smb347_set_charge_current(smb);
866 if (ret < 0)
867 goto fail;
868
869 ret = smb347_set_current_limits(smb);
870 if (ret < 0)
871 goto fail;
872
873 ret = smb347_set_voltage_limits(smb);
874 if (ret < 0)
875 goto fail;
876
877// HACK for Manta pre-alpha 0.2, TH_BATTERY not connected properly
878#if 0 // HACK
879 ret = smb347_set_temp_limits(smb);
880 if (ret < 0)
881 goto fail;
882#endif // HACK
883
884 /* If USB charging is disabled we put the USB in suspend mode */
885 if (!smb->pdata->use_usb) {
886 ret = smb347_read(smb, CMD_A);
887 if (ret < 0)
888 goto fail;
889
890 ret |= CMD_A_SUSPEND_ENABLED;
891
892 ret = smb347_write(smb, CMD_A, ret);
893 if (ret < 0)
894 goto fail;
895 }
896
897 ret = smb347_read(smb, CFG_OTHER);
898 if (ret < 0)
899 goto fail;
900
901 /*
902 * If configured by platform data, we enable hardware Auto-OTG
903 * support for driving VBUS. Otherwise we disable it.
904 */
905 ret &= ~CFG_OTHER_RID_MASK;
906 if (smb->pdata->use_usb_otg)
907 ret |= CFG_OTHER_RID_ENABLED_AUTO_OTG;
908
909 ret = smb347_write(smb, CFG_OTHER, ret);
910 if (ret < 0)
911 goto fail;
912
913 /* If configured by platform data, disable AUTOMATIC RECHARGE */
914 if (smb->pdata->disable_automatic_recharge) {
915 ret = smb347_read(smb, CFG_CHARGE_CONTROL);
916 if (ret < 0)
917 goto fail;
918
919 ret |= CFG_AUTOMATIC_RECHARGE_DISABLE;
920
921 ret = smb347_write(smb, CFG_CHARGE_CONTROL, ret);
922 if (ret < 0)
923 goto fail;
924 }
925
926 ret = smb347_read(smb, CFG_PIN);
927 if (ret < 0)
928 goto fail;
929
930 /*
931 * Make the charging functionality controllable by a write to the
932 * command register unless pin control is specified in the platform
933 * data.
934 */
935 ret &= ~(CFG_PIN_EN_CTRL_MASK | CFG_PIN_USB_MODE_CTRL);
936
937 switch (smb->pdata->enable_control) {
938 case SMB347_CHG_ENABLE_SW:
939 /* Do nothing, 0 means i2c control */
940 break;
941 case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW:
942 ret |= CFG_PIN_EN_CTRL_ACTIVE_LOW;
943 break;
944 case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH:
945 ret |= CFG_PIN_EN_CTRL_ACTIVE_HIGH;
946 break;
947 }
948
949 if (smb->pdata->usb_mode_pin_ctrl)
950 ret |= CFG_PIN_USB_MODE_CTRL;
951
952 /* Disable Automatic Power Source Detection (APSD) interrupt. */
953 ret &= ~CFG_PIN_EN_APSD_IRQ;
954
955 ret = smb347_write(smb, CFG_PIN, ret);
956 if (ret < 0)
957 goto fail;
958
959 ret = smb347_update_status(smb);
960 if (ret < 0)
961 goto fail;
962
963 ret = smb347_update_online(smb);
964
965 if ((smb->pdata->irq_gpio >= 0) &&
966 !smb->pdata->disable_stat_interrupts) {
967 /*
968 * Configure the STAT output to be suitable for interrupts:
969 * disable all other output (except interrupts) and make it
970 * active low.
971 */
972 ret = smb347_read(smb, CFG_STAT);
973 if (ret < 0)
974 goto fail;
975
976 ret &= ~CFG_STAT_ACTIVE_HIGH;
977 ret |= CFG_STAT_DISABLED;
978
979 ret = smb347_write(smb, CFG_STAT, ret);
980 if (ret < 0)
981 goto fail;
982
983 ret = smb347_irq_enable(smb);
984 if (ret < 0)
985 goto fail;
986 }
987
988fail:
989 smb347_set_writable(smb, false);
990 return ret;
991}
992
993static int smb347_mains_get_property(struct power_supply *psy,
994 enum power_supply_property prop,
995 union power_supply_propval *val)
996{
997 struct smb347_charger *smb =
998 container_of(psy, struct smb347_charger, mains);
999
1000 switch (prop) {
1001 case POWER_SUPPLY_PROP_ONLINE:
1002 val->intval = smb->mains_online;
1003 return 0;
1004
1005 case POWER_SUPPLY_PROP_CURRENT_MAX:
1006 val->intval = smb->mains_current_limit;
1007 return 0;
1008
1009 default:
1010 return -EINVAL;
1011 }
1012 return -EINVAL;
1013}
1014
1015static int smb347_mains_set_property(struct power_supply *psy,
1016 enum power_supply_property prop,
1017 const union power_supply_propval *val)
1018{
1019 struct smb347_charger *smb =
1020 container_of(psy, struct smb347_charger, mains);
1021 int ret;
1022 bool oldval;
1023
1024 switch (prop) {
1025 case POWER_SUPPLY_PROP_ONLINE:
1026 oldval = smb->mains_online;
1027
1028 smb->mains_online = val->intval;
1029
1030 smb347_set_writable(smb, true);
1031
1032 ret = smb347_read(smb, CMD_A);
1033 if (ret < 0)
1034 return -EINVAL;
1035
1036 ret &= ~CMD_A_SUSPEND_ENABLED;
1037 if (val->intval)
1038 ret |= CMD_A_SUSPEND_ENABLED;
1039
1040 ret = smb347_write(smb, CMD_A, ret);
1041
1042 smb347_hw_init(smb);
1043
1044 smb347_set_writable(smb, false);
1045
1046 if (smb->mains_online != oldval)
1047 power_supply_changed(psy);
1048 return 0;
1049 case POWER_SUPPLY_PROP_CURRENT_MAX:
1050 smb->mains_current_limit = val->intval;
1051 smb347_hw_init(smb);
1052 return 0;
1053
1054 default:
1055 return -EINVAL;
1056 }
1057
1058 return -EINVAL;
1059}
1060
1061static int smb347_mains_property_is_writeable(struct power_supply *psy,
1062 enum power_supply_property prop)
1063{
1064 switch (prop) {
1065 case POWER_SUPPLY_PROP_CURRENT_MAX:
1066 return 1;
1067 default:
1068 break;
1069 }
1070
1071 return 0;
1072}
1073
1074static enum power_supply_property smb347_mains_properties[] = {
1075 POWER_SUPPLY_PROP_ONLINE,
1076 POWER_SUPPLY_PROP_CURRENT_MAX,
1077};
1078
1079static int smb347_usb_get_property(struct power_supply *psy,
1080 enum power_supply_property prop,
1081 union power_supply_propval *val)
1082{
1083 struct smb347_charger *smb =
1084 container_of(psy, struct smb347_charger, usb);
1085
1086 switch (prop) {
1087 case POWER_SUPPLY_PROP_ONLINE:
1088 val->intval = smb->usb_online;
1089 return 0;
1090
1091 case POWER_SUPPLY_PROP_USB_HC:
1092 val->intval = smb->usb_hc_mode;
1093 return 0;
1094
1095 case POWER_SUPPLY_PROP_USB_OTG:
1096 val->intval = smb->usb_otg_enabled;
1097 return 0;
1098
1099 default:
1100 break;
1101 }
1102 return -EINVAL;
1103}
1104
1105static int smb347_usb_set_property(struct power_supply *psy,
1106 enum power_supply_property prop,
1107 const union power_supply_propval *val)
1108{
1109 int ret = -EINVAL;
1110 struct smb347_charger *smb =
1111 container_of(psy, struct smb347_charger, usb);
1112 bool oldval;
1113
1114 switch (prop) {
1115 case POWER_SUPPLY_PROP_ONLINE:
1116 oldval = smb->usb_online;
1117 smb->usb_online = val->intval;
1118
1119 if (smb->usb_online != oldval)
1120 power_supply_changed(psy);
1121 ret = 0;
1122 break;
1123 case POWER_SUPPLY_PROP_USB_HC:
1124 smb347_set_writable(smb, true);
1125 ret = smb347_write(smb, CMD_B, val->intval ?
1126 CMD_B_HC_MODE : CMD_B_USB59_MODE);
1127 smb347_set_writable(smb, false);
1128 smb->usb_hc_mode = val->intval;
1129 break;
1130
1131 case POWER_SUPPLY_PROP_USB_OTG:
1132 ret = smb347_read(smb, CMD_A);
1133
1134 if (ret < 0)
1135 return ret;
1136
1137 if (val->intval)
1138 ret |= CMD_A_OTG_ENABLE;
1139 else
1140 ret &= ~CMD_A_OTG_ENABLE;
1141
1142 ret = smb347_write(smb, CMD_A, ret);
1143
1144 if (ret >= 0)
1145 smb->usb_otg_enabled = val->intval;
1146
1147 break;
1148
1149 default:
1150 break;
1151 }
1152
1153 return ret;
1154}
1155
1156static int smb347_usb_property_is_writeable(struct power_supply *psy,
1157 enum power_supply_property prop)
1158{
1159 switch (prop) {
1160 case POWER_SUPPLY_PROP_USB_HC:
1161 case POWER_SUPPLY_PROP_USB_OTG:
1162 return 1;
1163 default:
1164 break;
1165 }
1166
1167 return 0;
1168}
1169
1170static enum power_supply_property smb347_usb_properties[] = {
1171 POWER_SUPPLY_PROP_ONLINE,
1172 POWER_SUPPLY_PROP_USB_HC,
1173 POWER_SUPPLY_PROP_USB_OTG,
1174};
1175
1176static int smb347_battery_get_property(struct power_supply *psy,
1177 enum power_supply_property prop,
1178 union power_supply_propval *val)
1179{
1180 struct smb347_charger *smb =
1181 container_of(psy, struct smb347_charger, battery);
1182 const struct smb347_charger_platform_data *pdata = smb->pdata;
1183 int ret;
1184
1185 ret = smb347_update_status(smb);
1186 if (ret < 0)
1187 return ret;
1188
1189 if (ret > 0) {
1190 smb347_update_online(smb);
1191 power_supply_changed(&smb->mains);
1192 power_supply_changed(&smb->usb);
1193 }
1194
1195 switch (prop) {
1196 case POWER_SUPPLY_PROP_STATUS:
1197 if (!smb347_is_online(smb)) {
1198 smb->is_fully_charged = false;
1199 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1200 break;
1201 }
1202 if (smb347_charging_status(smb))
1203 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1204 else
1205 val->intval = smb->is_fully_charged ?
1206 POWER_SUPPLY_STATUS_FULL :
1207 POWER_SUPPLY_STATUS_NOT_CHARGING;
1208 break;
1209
1210 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1211 if (!smb347_is_online(smb))
1212 return -ENODATA;
1213
1214 /*
1215 * We handle trickle and pre-charging the same, and taper
1216 * and none the same.
1217 */
1218 switch (smb347_charging_status(smb)) {
1219 case 1:
1220 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1221 break;
1222 case 2:
1223 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
1224 break;
1225 default:
1226 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
1227 break;
1228 }
1229 break;
1230
1231 case POWER_SUPPLY_PROP_TECHNOLOGY:
1232 val->intval = pdata->battery_info.technology;
1233 break;
1234
1235 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
1236 val->intval = pdata->battery_info.voltage_min_design;
1237 break;
1238
1239 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
1240 val->intval = pdata->battery_info.voltage_max_design;
1241 break;
1242
1243 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1244 if (!smb347_is_online(smb))
1245 return -ENODATA;
1246 ret = smb347_read(smb, STAT_A);
1247 if (ret < 0)
1248 return ret;
1249
1250 ret &= STAT_A_FLOAT_VOLTAGE_MASK;
1251 if (ret > 0x3d)
1252 ret = 0x3d;
1253
1254 val->intval = 3500000 + ret * 20000;
1255 break;
1256
1257 case POWER_SUPPLY_PROP_CURRENT_NOW:
1258 if (!smb347_is_online(smb))
1259 return -ENODATA;
1260
1261 ret = smb347_read(smb, STAT_B);
1262 if (ret < 0)
1263 return ret;
1264
1265 /*
1266 * The current value is composition of FCC and PCC values
1267 * and we can detect which table to use from bit 5.
1268 */
1269 if (ret & 0x20) {
1270 val->intval = hw_to_current(fcc_tbl,
1271 ARRAY_SIZE(fcc_tbl),
1272 ret & 7);
1273 } else {
1274 ret >>= 3;
1275 val->intval = hw_to_current(pcc_tbl,
1276 ARRAY_SIZE(pcc_tbl),
1277 ret & 7);
1278 }
1279 break;
1280
1281 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
1282 val->intval = pdata->battery_info.charge_full_design;
1283 break;
1284
1285 case POWER_SUPPLY_PROP_CHARGE_ENABLED:
1286 val->intval = smb->charging_enabled;
1287 break;
1288
1289 case POWER_SUPPLY_PROP_MODEL_NAME:
1290 val->strval = pdata->battery_info.name;
1291 break;
1292
1293 default:
1294 return -EINVAL;
1295 }
1296
1297 return 0;
1298}
1299
1300static int smb347_battery_set_property(struct power_supply *psy,
1301 enum power_supply_property prop,
1302 const union power_supply_propval *val)
1303{
1304 int ret = -EINVAL;
1305 struct smb347_charger *smb =
1306 container_of(psy, struct smb347_charger, battery);
1307
1308 switch (prop) {
1309 case POWER_SUPPLY_PROP_CHARGE_ENABLED:
1310 ret = smb347_charging_set(smb, val->intval);
1311 break;
1312
1313 default:
1314 break;
1315 }
1316
1317 return ret;
1318}
1319
1320static int smb347_battery_property_is_writeable(struct power_supply *psy,
1321 enum power_supply_property prop)
1322{
1323 switch (prop) {
1324 case POWER_SUPPLY_PROP_CHARGE_ENABLED:
1325 return 1;
1326 default:
1327 break;
1328 }
1329
1330 return 0;
1331}
1332
1333static enum power_supply_property smb347_battery_properties[] = {
1334 POWER_SUPPLY_PROP_STATUS,
1335 POWER_SUPPLY_PROP_CHARGE_TYPE,
1336 POWER_SUPPLY_PROP_TECHNOLOGY,
1337 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
1338 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
1339 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1340 POWER_SUPPLY_PROP_CURRENT_NOW,
1341 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
1342 POWER_SUPPLY_PROP_CHARGE_ENABLED,
1343 POWER_SUPPLY_PROP_MODEL_NAME,
1344};
1345
1346static int smb347_debugfs_show(struct seq_file *s, void *data)
1347{
1348 struct smb347_charger *smb = s->private;
1349 int ret;
1350 u8 reg;
1351
1352 seq_printf(s, "Control registers:\n");
1353 seq_printf(s, "==================\n");
1354 for (reg = CFG_CHARGE_CURRENT; reg <= CFG_ADDRESS; reg++) {
1355 ret = smb347_read(smb, reg);
1356 seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret);
1357 }
1358 seq_printf(s, "\n");
1359
1360 seq_printf(s, "Command registers:\n");
1361 seq_printf(s, "==================\n");
1362 ret = smb347_read(smb, CMD_A);
1363 seq_printf(s, "0x%02x:\t0x%02x\n", CMD_A, ret);
1364 ret = smb347_read(smb, CMD_B);
1365 seq_printf(s, "0x%02x:\t0x%02x\n", CMD_B, ret);
1366 ret = smb347_read(smb, CMD_C);
1367 seq_printf(s, "0x%02x:\t0x%02x\n", CMD_C, ret);
1368 seq_printf(s, "\n");
1369
1370 seq_printf(s, "Interrupt status registers:\n");
1371 seq_printf(s, "===========================\n");
1372 for (reg = IRQSTAT_A; reg <= IRQSTAT_F; reg++) {
1373 ret = smb347_read(smb, reg);
1374 seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret);
1375 }
1376 seq_printf(s, "\n");
1377
1378 seq_printf(s, "Status registers:\n");
1379 seq_printf(s, "=================\n");
1380 for (reg = STAT_A; reg <= STAT_E; reg++) {
1381 ret = smb347_read(smb, reg);
1382 seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret);
1383 }
1384
1385 return 0;
1386}
1387
1388static int smb347_debugfs_open(struct inode *inode, struct file *file)
1389{
1390 return single_open(file, smb347_debugfs_show, inode->i_private);
1391}
1392
1393static const struct file_operations smb347_debugfs_fops = {
1394 .open = smb347_debugfs_open,
1395 .read = seq_read,
1396 .llseek = seq_lseek,
1397 .release = single_release,
1398};
1399
1400static int smb347_probe(struct i2c_client *client,
1401 const struct i2c_device_id *id)
1402{
1403 static char *battery[] = { "smb347-battery" };
1404 const struct smb347_charger_platform_data *pdata;
1405 struct device *dev = &client->dev;
1406 struct smb347_charger *smb;
1407 int ret;
1408
1409 pdata = dev->platform_data;
1410 if (!pdata)
1411 return -EINVAL;
1412
1413 if (!pdata->use_mains && !pdata->use_usb)
1414 return -EINVAL;
1415
1416 smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
1417 if (!smb)
1418 return -ENOMEM;
1419
1420 i2c_set_clientdata(client, smb);
1421
1422 mutex_init(&smb->lock);
1423 smb->client = client;
1424 smb->pdata = pdata;
1425
1426 smb->mains_current_limit = smb->pdata->mains_current_limit;
1427
1428 if (pdata->en_gpio) {
1429 ret = gpio_request_one(
1430 pdata->en_gpio,
1431 smb->pdata->enable_control ==
1432 SMB347_CHG_ENABLE_PIN_ACTIVE_LOW ?
1433 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
1434 smb->client->name);
1435 if (ret < 0)
1436 dev_warn(dev, "failed to claim EN GPIO: %d\n", ret);
1437 else
1438 smb->en_gpio = pdata->en_gpio;
1439 }
1440
1441 ret = smb347_write(smb, CMD_B, CMD_B_POR);
1442 if (ret < 0)
1443 return ret;
1444
1445 msleep(20);
1446
1447 ret = smb347_read(smb, CMD_B);
1448 if (ret < 0) {
1449 dev_err(dev, "failed read after reset\n");
1450 return ret;
1451 }
1452
1453 ret = smb347_hw_init(smb);
1454 if (ret < 0)
1455 return ret;
1456
1457 smb->mains.name = "smb347-mains";
1458 smb->mains.type = POWER_SUPPLY_TYPE_MAINS;
1459 smb->mains.get_property = smb347_mains_get_property;
1460 smb->mains.set_property = smb347_mains_set_property;
1461 smb->mains.property_is_writeable = smb347_mains_property_is_writeable;
1462 smb->mains.properties = smb347_mains_properties;
1463 smb->mains.num_properties = ARRAY_SIZE(smb347_mains_properties);
1464 smb->mains.supplied_to = battery;
1465 smb->mains.num_supplicants = ARRAY_SIZE(battery);
1466
1467 smb->usb.name = "smb347-usb";
1468 smb->usb.type = POWER_SUPPLY_TYPE_USB;
1469 smb->usb.get_property = smb347_usb_get_property;
1470 smb->usb.set_property = smb347_usb_set_property;
1471 smb->usb.property_is_writeable = smb347_usb_property_is_writeable;
1472 smb->usb.properties = smb347_usb_properties;
1473 smb->usb.num_properties = ARRAY_SIZE(smb347_usb_properties);
1474 smb->usb.supplied_to = battery;
1475 smb->usb.num_supplicants = ARRAY_SIZE(battery);
1476
1477 smb->battery.name = "smb347-battery";
1478 smb->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1479 smb->battery.get_property = smb347_battery_get_property;
1480 smb->battery.set_property = smb347_battery_set_property;
1481 smb->battery.property_is_writeable = smb347_battery_property_is_writeable;
1482 smb->battery.properties = smb347_battery_properties;
1483 smb->battery.num_properties = ARRAY_SIZE(smb347_battery_properties);
1484
1485 if (smb->pdata->supplied_to) {
1486 smb->battery.supplied_to = smb->pdata->supplied_to;
1487 smb->battery.num_supplicants = smb->pdata->num_supplicants;
1488 smb->battery.external_power_changed = power_supply_changed;
1489 }
1490
1491 ret = power_supply_register(dev, &smb->mains);
1492 if (ret < 0)
1493 return ret;
1494
1495 ret = power_supply_register(dev, &smb->usb);
1496 if (ret < 0) {
1497 power_supply_unregister(&smb->mains);
1498 return ret;
1499 }
1500
1501 ret = power_supply_register(dev, &smb->battery);
1502 if (ret < 0) {
1503 power_supply_unregister(&smb->usb);
1504 power_supply_unregister(&smb->mains);
1505 return ret;
1506 }
1507
1508 /*
1509 * Interrupt pin is optional. If it is connected, we setup the
1510 * interrupt support here.
1511 */
1512 if (pdata->irq_gpio >= 0) {
1513 ret = smb347_irq_init(smb);
1514 if (ret < 0) {
1515 dev_warn(dev, "failed to initialize IRQ: %d\n", ret);
1516 dev_warn(dev, "disabling IRQ support\n");
1517 }
1518 }
1519
1520 smb->dentry = debugfs_create_file("smb347-regs", S_IRUSR, NULL, smb,
1521 &smb347_debugfs_fops);
1522 return 0;
1523}
1524
1525static int smb347_remove(struct i2c_client *client)
1526{
1527 struct smb347_charger *smb = i2c_get_clientdata(client);
1528
1529 if (!IS_ERR_OR_NULL(smb->dentry))
1530 debugfs_remove(smb->dentry);
1531
1532 if (client->irq) {
1533 smb347_irq_disable(smb);
1534 disable_irq_wake(client->irq);
1535 free_irq(client->irq, smb);
1536 gpio_free(smb->pdata->irq_gpio);
1537 }
1538
1539 power_supply_unregister(&smb->battery);
1540 power_supply_unregister(&smb->usb);
1541 power_supply_unregister(&smb->mains);
1542 return 0;
1543}
1544
1545static int smb347_suspend(struct device *dev)
1546{
1547 struct i2c_client *client = to_i2c_client(dev);
1548
1549 if (client->irq)
1550 disable_irq(client->irq);
1551 return 0;
1552}
1553
1554static int smb347_resume(struct device *dev)
1555{
1556 struct i2c_client *client = to_i2c_client(dev);
1557
1558 if (client->irq)
1559 enable_irq(client->irq);
1560 return 0;
1561}
1562
1563static const struct dev_pm_ops smb347_pm_ops = {
1564 .suspend = smb347_suspend,
1565 .resume = smb347_resume,
1566};
1567
1568static const struct i2c_device_id smb347_id[] = {
1569 { "smb347", 0 },
1570 { }
1571};
1572MODULE_DEVICE_TABLE(i2c, smb347_id);
1573
1574static struct i2c_driver smb347_driver = {
1575 .driver = {
1576 .name = "smb347",
1577 .pm = &smb347_pm_ops,
1578 },
1579 .probe = smb347_probe,
1580 .remove = __devexit_p(smb347_remove),
1581 .id_table = smb347_id,
1582};
1583
1584static int __init smb347_init(void)
1585{
1586 return i2c_add_driver(&smb347_driver);
1587}
1588module_init(smb347_init);
1589
1590static void __exit smb347_exit(void)
1591{
1592 i2c_del_driver(&smb347_driver);
1593}
1594module_exit(smb347_exit);
1595
1596MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1597MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1598MODULE_DESCRIPTION("SMB347 battery charger driver");
1599MODULE_LICENSE("GPL");
1600MODULE_ALIAS("i2c:smb347");