blob: 11fff43657ded6ea97ba9d16436a026ee0ccdb04 [file] [log] [blame]
Amir Samuelov2bd9e4e2013-05-05 16:25:59 +03001/* Copyright (c) 2012-2013 The Linux Foundation. All rights reserved.
Amir Samuelovcaaa8f02012-10-25 11:11:57 +02002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#define pr_fmt(fmt) "%s: " fmt, __func__
15
16#include <linux/i2c.h>
17#include <linux/gpio.h>
18#include <linux/errno.h>
19#include <linux/delay.h>
20#include <linux/module.h>
21#include <linux/debugfs.h>
22#include <linux/workqueue.h>
23#include <linux/interrupt.h>
24#include <linux/slab.h>
25#include <linux/power_supply.h>
26#include <linux/i2c/smb350.h>
27#include <linux/bitops.h>
28#include <linux/of.h>
29#include <linux/of_device.h>
30#include <linux/of_gpio.h>
31#include <linux/printk.h>
32
33/* Register definitions */
34#define CHG_CURRENT_REG 0x00 /* Non-Volatile + mirror */
35#define CHG_OTHER_CURRENT_REG 0x01 /* Non-Volatile + mirror */
36#define VAR_FUNC_REG 0x02 /* Non-Volatile + mirror */
37#define FLOAT_VOLTAGE_REG 0x03 /* Non-Volatile + mirror */
38#define CHG_CTRL_REG 0x04 /* Non-Volatile + mirror */
39#define STAT_TIMER_REG 0x05 /* Non-Volatile + mirror */
40#define PIN_ENABLE_CTRL_REG 0x06 /* Non-Volatile + mirror */
41#define THERM_CTRL_A_REG 0x07 /* Non-Volatile + mirror */
42#define SYSOK_USB3_SELECT_REG 0x08 /* Non-Volatile + mirror */
43#define CTRL_FUNCTIONS_REG 0x09 /* Non-Volatile + mirror */
44#define OTG_TLIM_THERM_CNTRL_REG 0x0A /* Non-Volatile + mirror */
45#define TEMP_MONITOR_REG 0x0B /* Non-Volatile + mirror */
46#define FAULT_IRQ_REG 0x0C /* Non-Volatile */
47#define IRQ_ENABLE_REG 0x0D /* Non-Volatile */
48#define SYSOK_REG 0x0E /* Non-Volatile + mirror */
49
50#define AUTO_INPUT_VOLT_DETECT_REG 0x10 /* Non-Volatile Read-Only */
51#define STATUS_IRQ_REG 0x11 /* Non-Volatile Read-Only */
52#define I2C_SLAVE_ADDR_REG 0x12 /* Non-Volatile Read-Only */
53
54#define CMD_A_REG 0x30 /* Volatile Read-Write */
55#define CMD_B_REG 0x31 /* Volatile Read-Write */
56#define CMD_C_REG 0x33 /* Volatile Read-Write */
57
Amir Samuelov094534d2012-11-12 14:27:39 +020058#define HW_VERSION_REG 0x34 /* Volatile Read-Only */
59
Amir Samuelovcaaa8f02012-10-25 11:11:57 +020060#define IRQ_STATUS_A_REG 0x35 /* Volatile Read-Only */
61#define IRQ_STATUS_B_REG 0x36 /* Volatile Read-Only */
62#define IRQ_STATUS_C_REG 0x37 /* Volatile Read-Only */
63#define IRQ_STATUS_D_REG 0x38 /* Volatile Read-Only */
64#define IRQ_STATUS_E_REG 0x39 /* Volatile Read-Only */
65#define IRQ_STATUS_F_REG 0x3A /* Volatile Read-Only */
66
67#define STATUS_A_REG 0x3B /* Volatile Read-Only */
68#define STATUS_B_REG 0x3D /* Volatile Read-Only */
69/* Note: STATUS_C_REG was removed from SMB349 to SMB350 */
70#define STATUS_D_REG 0x3E /* Volatile Read-Only */
71#define STATUS_E_REG 0x3F /* Volatile Read-Only */
72
73#define IRQ_STATUS_NUM (IRQ_STATUS_F_REG - IRQ_STATUS_A_REG + 1)
74
75/* Status bits and masks */
76#define SMB350_MASK(BITS, POS) ((u8)(((1 << BITS) - 1) << POS))
77#define FAST_CHG_CURRENT_MASK SMB350_MASK(4, 4)
78
79#define SMB350_FAST_CHG_MIN_MA 1000
80#define SMB350_FAST_CHG_STEP_MA 200
81#define SMB350_FAST_CHG_MAX_MA 3600
82
83#define TERM_CURRENT_MASK SMB350_MASK(3, 2)
84
85#define SMB350_TERM_CUR_MIN_MA 200
86#define SMB350_TERM_CUR_STEP_MA 100
87#define SMB350_TERM_CUR_MAX_MA 700
88
89#define CMD_A_VOLATILE_WR_PERM BIT(7)
90#define CHG_CTRL_CURR_TERM_END_CHG BIT(6)
91
92enum smb350_chg_status {
93 SMB_CHG_STATUS_NONE = 0,
94 SMB_CHG_STATUS_PRE_CHARGE = 1,
95 SMB_CHG_STATUS_FAST_CHARGE = 2,
96 SMB_CHG_STATUS_TAPER_CHARGE = 3,
97};
98
99static const char * const smb350_chg_status[] = {
100 "none",
101 "pre-charge",
102 "fast-charge",
103 "taper-charge"
104};
105
106struct smb350_device {
107 /* setup */
108 int chg_current_ma;
109 int term_current_ma;
110 int chg_en_n_gpio;
111 int chg_susp_n_gpio;
112 int stat_gpio;
113 int irq;
114 /* internal */
115 enum smb350_chg_status chg_status;
116 struct i2c_client *client;
117 struct delayed_work irq_work;
118 struct dentry *dent;
119 struct wake_lock chg_wake_lock;
120 struct power_supply dc_psy;
121};
122
123static struct smb350_device *smb350_dev;
124
125struct debug_reg {
126 char *name;
127 u8 reg;
128};
129
130#define SMB350_DEBUG_REG(x) {#x, x##_REG}
131
132static struct debug_reg smb350_debug_regs[] = {
133 SMB350_DEBUG_REG(CHG_CURRENT),
134 SMB350_DEBUG_REG(CHG_OTHER_CURRENT),
135 SMB350_DEBUG_REG(VAR_FUNC),
136 SMB350_DEBUG_REG(FLOAT_VOLTAGE),
137 SMB350_DEBUG_REG(CHG_CTRL),
138 SMB350_DEBUG_REG(STAT_TIMER),
139 SMB350_DEBUG_REG(PIN_ENABLE_CTRL),
140 SMB350_DEBUG_REG(THERM_CTRL_A),
141 SMB350_DEBUG_REG(SYSOK_USB3_SELECT),
142 SMB350_DEBUG_REG(CTRL_FUNCTIONS),
143 SMB350_DEBUG_REG(OTG_TLIM_THERM_CNTRL),
144 SMB350_DEBUG_REG(TEMP_MONITOR),
145 SMB350_DEBUG_REG(FAULT_IRQ),
146 SMB350_DEBUG_REG(IRQ_ENABLE),
147 SMB350_DEBUG_REG(SYSOK),
148 SMB350_DEBUG_REG(AUTO_INPUT_VOLT_DETECT),
149 SMB350_DEBUG_REG(STATUS_IRQ),
150 SMB350_DEBUG_REG(I2C_SLAVE_ADDR),
151 SMB350_DEBUG_REG(CMD_A),
152 SMB350_DEBUG_REG(CMD_B),
153 SMB350_DEBUG_REG(CMD_C),
Amir Samuelov094534d2012-11-12 14:27:39 +0200154 SMB350_DEBUG_REG(HW_VERSION),
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200155 SMB350_DEBUG_REG(IRQ_STATUS_A),
156 SMB350_DEBUG_REG(IRQ_STATUS_B),
157 SMB350_DEBUG_REG(IRQ_STATUS_C),
158 SMB350_DEBUG_REG(IRQ_STATUS_D),
159 SMB350_DEBUG_REG(IRQ_STATUS_E),
160 SMB350_DEBUG_REG(IRQ_STATUS_F),
161 SMB350_DEBUG_REG(STATUS_A),
162 SMB350_DEBUG_REG(STATUS_B),
163 SMB350_DEBUG_REG(STATUS_D),
164 SMB350_DEBUG_REG(STATUS_E),
165};
166
167/*
168 * Read 8-bit register value. return negative value on error.
169 */
170static int smb350_read_reg(struct i2c_client *client, u8 reg)
171{
172 int val;
173
174 val = i2c_smbus_read_byte_data(client, reg);
175 if (val < 0)
176 pr_err("i2c read fail. reg=0x%x.ret=%d.\n", reg, val);
177 else
178 pr_debug("reg=0x%02X.val=0x%02X.\n", reg , val);
179
180 return val;
181}
182
183/*
184 * Write 8-bit register value. return negative value on error.
185 */
186static int smb350_write_reg(struct i2c_client *client, u8 reg, u8 val)
187{
188 int ret;
189
190 ret = i2c_smbus_write_byte_data(client, reg, val);
191 if (ret < 0)
192 pr_err("i2c read fail. reg=0x%x.val=0x%x.ret=%d.\n",
193 reg, val, ret);
194 else
195 pr_debug("reg=0x%02X.val=0x%02X.\n", reg , val);
196
197 return ret;
198}
199
200static int smb350_masked_write(struct i2c_client *client, int reg, u8 mask,
201 u8 val)
202{
203 int ret;
204 int temp;
205 int shift = find_first_bit((unsigned long *) &mask, 8);
206
207 temp = smb350_read_reg(client, reg);
208 if (temp < 0)
209 return temp;
210
211 temp &= ~mask;
212 temp |= (val << shift) & mask;
213 ret = smb350_write_reg(client, reg, temp);
214
215 return ret;
216}
217
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200218static bool smb350_is_dc_present(struct i2c_client *client)
219{
220 u16 irq_status_f = smb350_read_reg(client, IRQ_STATUS_F_REG);
221 bool power_ok = irq_status_f & 0x01;
222
223 /* Power-ok , IRQ_STATUS_F_REG bit#0 */
224 if (power_ok)
225 pr_debug("DC is present.\n");
226 else
227 pr_debug("DC is missing.\n");
228
229 return power_ok;
230}
231
Amir Samuelov1c7efb52012-11-20 10:48:59 +0200232static bool smb350_is_charger_present(struct i2c_client *client)
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200233{
234 int val;
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200235
Amir Samuelov1c7efb52012-11-20 10:48:59 +0200236 /* Normally the device is non-removable and embedded on the board.
237 * Verify that charger is present by getting I2C response.
238 */
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200239 val = smb350_read_reg(client, STATUS_B_REG);
240 if (val < 0)
241 return false;
242
Amir Samuelov1c7efb52012-11-20 10:48:59 +0200243 return true;
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200244}
245
246static int smb350_get_prop_charge_type(struct smb350_device *dev)
247{
248 int status_b;
249 enum smb350_chg_status status;
250 int chg_type = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
251 bool chg_enabled;
252 bool charger_err;
253 struct i2c_client *client = dev->client;
254
255 status_b = smb350_read_reg(client, STATUS_B_REG);
256 if (status_b < 0) {
257 pr_err("failed to read STATUS_B_REG.\n");
258 return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
259 }
260
261 chg_enabled = (bool) (status_b & 0x01);
262 charger_err = (bool) (status_b & (1<<6));
263
264 if (!chg_enabled) {
265 pr_warn("Charging not enabled.\n");
Amir Samuelov2bd9e4e2013-05-05 16:25:59 +0300266 /* release the wake-lock when DC power removed */
267 if (wake_lock_active(&dev->chg_wake_lock))
268 wake_unlock(&dev->chg_wake_lock);
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200269 return POWER_SUPPLY_CHARGE_TYPE_NONE;
270 }
271
272 if (charger_err) {
273 pr_warn("Charger error detected.\n");
274 return POWER_SUPPLY_CHARGE_TYPE_NONE;
275 }
276
277 status = (status_b >> 1) & 0x3;
278
279 if (status == SMB_CHG_STATUS_NONE)
280 chg_type = POWER_SUPPLY_CHARGE_TYPE_NONE;
281 else if (status == SMB_CHG_STATUS_FAST_CHARGE) /* constant current */
282 chg_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
283 else if (status == SMB_CHG_STATUS_TAPER_CHARGE) /* constant voltage */
284 chg_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
285 else if (status == SMB_CHG_STATUS_PRE_CHARGE)
286 chg_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
287
288 pr_debug("smb-chg-status=%d=%s.\n", status, smb350_chg_status[status]);
289
290 if (dev->chg_status != status) { /* Status changed */
291 if (status == SMB_CHG_STATUS_NONE) {
292 pr_debug("Charging stopped.\n");
293 wake_unlock(&dev->chg_wake_lock);
294 } else {
295 pr_debug("Charging started.\n");
296 wake_lock(&dev->chg_wake_lock);
297 }
298 }
299
300 dev->chg_status = status;
301
302 return chg_type;
303}
304
305static void smb350_enable_charging(struct smb350_device *dev, bool enable)
306{
307 int val = !enable; /* active low */
308
309 pr_debug("enable=%d.\n", enable);
310
311 gpio_set_value_cansleep(dev->chg_en_n_gpio, val);
312}
313
314/* When the status bit of a certain condition is read,
315 * the corresponding IRQ signal is cleared.
316 */
317static int smb350_clear_irq(struct i2c_client *client)
318{
319 int ret;
320
321 ret = smb350_read_reg(client, IRQ_STATUS_A_REG);
322 if (ret < 0)
323 return ret;
324 ret = smb350_read_reg(client, IRQ_STATUS_B_REG);
325 if (ret < 0)
326 return ret;
327 ret = smb350_read_reg(client, IRQ_STATUS_C_REG);
328 if (ret < 0)
329 return ret;
330 ret = smb350_read_reg(client, IRQ_STATUS_D_REG);
331 if (ret < 0)
332 return ret;
333 ret = smb350_read_reg(client, IRQ_STATUS_E_REG);
334 if (ret < 0)
335 return ret;
336 ret = smb350_read_reg(client, IRQ_STATUS_F_REG);
337 if (ret < 0)
338 return ret;
339
340 return 0;
341}
342
343/*
344 * Do the IRQ work from a thread context rather than interrupt context.
345 * Read status registers to clear interrupt source.
346 * Notify the power-supply driver about change detected.
347 * Relevant events for start/stop charging:
348 * 1. DC insert/remove
349 * 2. End-Of-Charging
350 * 3. Battery insert/remove
351 * 4. Temperture too hot/cold
352 * 5. Charging timeout expired.
353 */
354static void smb350_irq_worker(struct work_struct *work)
355{
356 int ret = 0;
357 struct smb350_device *dev =
358 container_of(work, struct smb350_device, irq_work.work);
359
360 ret = smb350_clear_irq(dev->client);
361 if (ret == 0) { /* Cleared ok */
362 /* Notify Battery-psy about status changed */
363 pr_debug("Notify power_supply_changed.\n");
364 power_supply_changed(&dev->dc_psy);
365 }
366}
367
368/*
369 * The STAT pin is low when charging and high when not charging.
370 * When the smb350 start/stop charging the STAT pin triggers an interrupt.
371 * Interrupt is triggered on both rising or falling edge.
372 */
373static irqreturn_t smb350_irq(int irq, void *dev_id)
374{
375 struct smb350_device *dev = dev_id;
376
377 pr_debug("\n");
378
379 /* I2C transfers API should not run in interrupt context */
380 schedule_delayed_work(&dev->irq_work, msecs_to_jiffies(100));
381
382 return IRQ_HANDLED;
383}
384
385static enum power_supply_property pm_power_props[] = {
386 /* real time */
387 POWER_SUPPLY_PROP_PRESENT,
388 POWER_SUPPLY_PROP_ONLINE,
389 POWER_SUPPLY_PROP_CHARGE_TYPE,
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200390 /* fixed */
391 POWER_SUPPLY_PROP_MANUFACTURER,
392 POWER_SUPPLY_PROP_MODEL_NAME,
393 POWER_SUPPLY_PROP_CURRENT_MAX,
394};
395
396static char *pm_power_supplied_to[] = {
397 "battery",
398};
399
400static int smb350_get_property(struct power_supply *psy,
401 enum power_supply_property psp,
402 union power_supply_propval *val)
403{
404 int ret = 0;
405 struct smb350_device *dev = container_of(psy,
406 struct smb350_device,
407 dc_psy);
408 struct i2c_client *client = dev->client;
409
410 switch (psp) {
411 case POWER_SUPPLY_PROP_PRESENT:
Amir Samuelov1c7efb52012-11-20 10:48:59 +0200412 val->intval = smb350_is_charger_present(client);
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200413 break;
414 case POWER_SUPPLY_PROP_ONLINE:
Amir Samuelov1c7efb52012-11-20 10:48:59 +0200415 val->intval = smb350_is_dc_present(client);
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200416 break;
417 case POWER_SUPPLY_PROP_CHARGE_TYPE:
418 val->intval = smb350_get_prop_charge_type(dev);
419 break;
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200420 case POWER_SUPPLY_PROP_MODEL_NAME:
421 val->strval = SMB350_NAME;
422 break;
423 case POWER_SUPPLY_PROP_MANUFACTURER:
424 val->strval = "Summit Microelectronics";
425 break;
426 case POWER_SUPPLY_PROP_CURRENT_MAX:
427 val->intval = dev->chg_current_ma;
428 break;
429 default:
430 pr_err("Invalid prop = %d.\n", psp);
431 ret = -EINVAL;
432 break;
433 }
434
435 return ret;
436}
437
438static int smb350_set_property(struct power_supply *psy,
439 enum power_supply_property psp,
440 const union power_supply_propval *val)
441{
442 int ret = 0;
443 struct smb350_device *dev =
444 container_of(psy, struct smb350_device, dc_psy);
445
446 switch (psp) {
447 /*
448 * Allow a smart battery to Start/Stop charging.
449 * i.e. when End-Of-Charging detected.
450 * The SMB350 can be configured to terminate charging
451 * when charge-current reaching Termination-Current.
452 */
453 case POWER_SUPPLY_PROP_ONLINE:
454 smb350_enable_charging(dev, val->intval);
455 break;
456 default:
457 pr_err("Invalid prop = %d.\n", psp);
458 ret = -EINVAL;
459 }
460
461 return ret;
462}
463
464static int smb350_set_chg_current(struct i2c_client *client, int current_ma)
465{
466 int ret;
467 u8 temp;
468
469 if ((current_ma < SMB350_FAST_CHG_MIN_MA) ||
470 (current_ma > SMB350_FAST_CHG_MAX_MA)) {
471 pr_err("invalid current %d mA.\n", current_ma);
472 return -EINVAL;
473 }
474
475 temp = (current_ma - SMB350_FAST_CHG_MIN_MA) / SMB350_FAST_CHG_STEP_MA;
476
477 pr_debug("fast-chg-current=%d mA setting %02x\n", current_ma, temp);
478
479 ret = smb350_masked_write(client, CHG_CURRENT_REG,
480 FAST_CHG_CURRENT_MASK, temp);
481
482 return ret;
483}
484
485static int smb350_set_term_current(struct i2c_client *client, int current_ma)
486{
487 int ret;
488 u8 temp;
489
490 if ((current_ma < SMB350_TERM_CUR_MIN_MA) ||
491 (current_ma > SMB350_TERM_CUR_MAX_MA)) {
492 pr_err("invalid current %d mA to set\n", current_ma);
493 return -EINVAL;
494 }
495
496 temp = (current_ma - SMB350_TERM_CUR_MIN_MA) / SMB350_TERM_CUR_STEP_MA;
497
498 pr_debug("term-current=%d mA setting %02x\n", current_ma, temp);
499
500 ret = smb350_masked_write(client, CHG_OTHER_CURRENT_REG,
501 TERM_CURRENT_MASK, temp);
502
503 return ret;
504}
505
506static int smb350_set_reg(void *data, u64 val)
507{
508 u32 addr = (u32) data;
509 int ret;
510 struct i2c_client *client = smb350_dev->client;
511
512 ret = smb350_write_reg(client, addr, (u8) val);
513
514 return ret;
515}
516
517static int smb350_get_reg(void *data, u64 *val)
518{
519 u32 addr = (u32) data;
520 int ret;
521 struct i2c_client *client = smb350_dev->client;
522
523 ret = smb350_read_reg(client, addr);
524 if (ret < 0)
525 return ret;
526
527 *val = ret;
528
529 return 0;
530}
531
532DEFINE_SIMPLE_ATTRIBUTE(reg_fops, smb350_get_reg, smb350_set_reg, "0x%02llx\n");
533
534static int smb350_create_debugfs_entries(struct smb350_device *dev)
535{
536 int i;
537 dev->dent = debugfs_create_dir(SMB350_NAME, NULL);
538 if (IS_ERR(dev->dent)) {
539 pr_err("smb350 driver couldn't create debugfs dir\n");
540 return -EFAULT;
541 }
542
543 for (i = 0 ; i < ARRAY_SIZE(smb350_debug_regs) ; i++) {
544 char *name = smb350_debug_regs[i].name;
545 u32 reg = smb350_debug_regs[i].reg;
546 struct dentry *file;
547
548 file = debugfs_create_file(name, 0644, dev->dent,
549 (void *) reg, &reg_fops);
550 if (IS_ERR(file)) {
551 pr_err("debugfs_create_file %s failed.\n", name);
552 return -EFAULT;
553 }
554 }
555
556 return 0;
557}
558
559static int smb350_set_volatile_params(struct smb350_device *dev)
560{
561 int ret;
562 struct i2c_client *client = dev->client;
563
564 pr_debug("\n");
565
566 ret = smb350_write_reg(client, CMD_A_REG, CMD_A_VOLATILE_WR_PERM);
567 if (ret) {
568 pr_err("Failed to set VOLATILE_WR_PERM ret=%d\n", ret);
569 return ret;
570 }
571
572 /* Disable SMB350 pulse-IRQ mechanism,
573 * we use interrupts based on charging-status-transition
574 */
575 /* Enable STATUS output (regardless of IRQ-pulses) */
576 smb350_masked_write(client, CMD_A_REG, BIT(0), 0);
577
578 /* Disable LED blinking - avoid periodic irq */
579 smb350_masked_write(client, PIN_ENABLE_CTRL_REG, BIT(7), 0);
580
581 /* Disable Failure SMB-IRQ */
582 ret = smb350_write_reg(client, FAULT_IRQ_REG, 0x00);
583 if (ret) {
584 pr_err("Failed to set FAULT_IRQ_REG ret=%d\n", ret);
585 return ret;
586 }
587
588 /* Disable Event IRQ */
589 ret = smb350_write_reg(client, IRQ_ENABLE_REG, 0x00);
590 if (ret) {
591 pr_err("Failed to set IRQ_ENABLE_REG ret=%d\n", ret);
592 return ret;
593 }
594
595 /* Enable charging/not-charging status output via STAT pin */
596 smb350_masked_write(client, STAT_TIMER_REG, BIT(5), 0);
597
598 /* Disable Automatic Recharge */
599 smb350_masked_write(client, CHG_CTRL_REG, BIT(7), 1);
600
601 /* Set fast-charge current */
602 ret = smb350_set_chg_current(client, dev->chg_current_ma);
603 if (ret) {
604 pr_err("Failed to set FAST_CHG_CURRENT ret=%d\n", ret);
605 return ret;
606 }
607
608 if (dev->term_current_ma > 0) {
609 /* Enable Current Termination */
610 smb350_masked_write(client, CHG_CTRL_REG, BIT(6), 0);
611
612 /* Set Termination current */
613 smb350_set_term_current(client, dev->term_current_ma);
614 } else {
615 /* Disable Current Termination */
616 smb350_masked_write(client, CHG_CTRL_REG, BIT(6), 1);
617 }
618
619 return 0;
620}
621
622static int __devinit smb350_register_psy(struct smb350_device *dev)
623{
624 int ret;
625
626 dev->dc_psy.name = "dc";
627 dev->dc_psy.type = POWER_SUPPLY_TYPE_MAINS;
628 dev->dc_psy.supplied_to = pm_power_supplied_to;
629 dev->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to);
630 dev->dc_psy.properties = pm_power_props;
631 dev->dc_psy.num_properties = ARRAY_SIZE(pm_power_props);
632 dev->dc_psy.get_property = smb350_get_property;
633 dev->dc_psy.set_property = smb350_set_property;
634
635 ret = power_supply_register(&dev->client->dev,
636 &dev->dc_psy);
637 if (ret) {
638 pr_err("failed to register power_supply. ret=%d.\n", ret);
639 return ret;
640 }
641
642 return 0;
643}
644
645static int __devinit smb350_probe(struct i2c_client *client,
646 const struct i2c_device_id *id)
647{
648 int ret = 0;
649 const struct smb350_platform_data *pdata;
650 struct device_node *dev_node = client->dev.of_node;
651 struct smb350_device *dev;
Amir Samuelov094534d2012-11-12 14:27:39 +0200652 u8 version;
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200653
654 /* STAT pin change on start/stop charging */
655 u32 irq_flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
656
657 if (!i2c_check_functionality(client->adapter,
658 I2C_FUNC_SMBUS_BYTE_DATA)) {
659 pr_err("i2c func fail.\n");
660 return -EIO;
661 }
662
663 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
664 if (!dev) {
665 pr_err("alloc fail.\n");
666 return -ENOMEM;
667 }
668
669 smb350_dev = dev;
670 dev->client = client;
671
672 if (dev_node) {
673 dev->chg_en_n_gpio =
674 of_get_named_gpio(dev_node, "summit,chg-en-n-gpio", 0);
675 pr_debug("chg_en_n_gpio = %d.\n", dev->chg_en_n_gpio);
676
677 dev->chg_susp_n_gpio =
678 of_get_named_gpio(dev_node,
679 "summit,chg-susp-n-gpio", 0);
680 pr_debug("chg_susp_n_gpio = %d.\n", dev->chg_susp_n_gpio);
681
682 dev->stat_gpio =
683 of_get_named_gpio(dev_node, "summit,stat-gpio", 0);
684 pr_debug("stat_gpio = %d.\n", dev->stat_gpio);
685
686 ret = of_property_read_u32(dev_node, "summit,chg-current-ma",
687 &(dev->chg_current_ma));
688 pr_debug("chg_current_ma = %d.\n", dev->chg_current_ma);
689 if (ret) {
690 pr_err("Unable to read chg_current.\n");
691 return ret;
692 }
693 ret = of_property_read_u32(dev_node, "summit,term-current-ma",
694 &(dev->term_current_ma));
695 pr_debug("term_current_ma = %d.\n", dev->term_current_ma);
696 if (ret) {
697 pr_err("Unable to read term_current_ma.\n");
698 return ret;
699 }
700 } else {
701 pdata = client->dev.platform_data;
702
703 if (pdata == NULL) {
704 pr_err("no platform data.\n");
705 return -EINVAL;
706 }
707
708 dev->chg_en_n_gpio = pdata->chg_en_n_gpio;
709 dev->chg_susp_n_gpio = pdata->chg_susp_n_gpio;
710 dev->stat_gpio = pdata->stat_gpio;
711
712 dev->chg_current_ma = pdata->chg_current_ma;
713 dev->term_current_ma = pdata->term_current_ma;
714 }
715
716 ret = gpio_request(dev->stat_gpio, "smb350_stat");
717 if (ret) {
718 pr_err("gpio_request failed for %d ret=%d\n",
719 dev->stat_gpio, ret);
720 goto err_stat_gpio;
721 }
722 dev->irq = gpio_to_irq(dev->stat_gpio);
723 pr_debug("irq#=%d.\n", dev->irq);
724
725 ret = gpio_request(dev->chg_susp_n_gpio, "smb350_suspend");
726 if (ret) {
727 pr_err("gpio_request failed for %d ret=%d\n",
728 dev->chg_susp_n_gpio, ret);
729 goto err_susp_gpio;
730 }
731
732 ret = gpio_request(dev->chg_en_n_gpio, "smb350_charger_enable");
733 if (ret) {
734 pr_err("gpio_request failed for %d ret=%d\n",
735 dev->chg_en_n_gpio, ret);
736 goto err_en_gpio;
737 }
738
739 i2c_set_clientdata(client, dev);
740
Amir Samuelovfef02172012-11-01 20:19:22 +0200741 /* Disable battery charging by default on power up.
742 * Battery charging is enabled by BMS or Battery-Gauge
743 * by using the set_property callback.
744 */
745 smb350_enable_charging(dev, false);
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200746 msleep(100);
747 gpio_set_value_cansleep(dev->chg_susp_n_gpio, 1); /* Normal */
748 msleep(100); /* Allow the device to exist shutdown */
749
Amir Samuelovfef02172012-11-01 20:19:22 +0200750 /* I2C transaction allowed only after device exit suspend */
751 ret = smb350_read_reg(client, I2C_SLAVE_ADDR_REG);
752 if ((ret>>1) != client->addr) {
753 pr_err("No device.\n");
754 ret = -ENODEV;
755 goto err_no_dev;
756 }
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200757
Amir Samuelov094534d2012-11-12 14:27:39 +0200758 version = smb350_read_reg(client, HW_VERSION_REG);
759 version &= 0x0F; /* bits 0..3 */
760
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200761 ret = smb350_set_volatile_params(dev);
762 if (ret)
763 goto err_set_params;
764
765 ret = smb350_register_psy(dev);
766 if (ret)
767 goto err_set_params;
768
769 ret = smb350_create_debugfs_entries(dev);
770 if (ret)
771 goto err_debugfs;
772
773 INIT_DELAYED_WORK(&dev->irq_work, smb350_irq_worker);
774 wake_lock_init(&dev->chg_wake_lock,
775 WAKE_LOCK_SUSPEND, SMB350_NAME);
776
777 ret = request_irq(dev->irq, smb350_irq, irq_flags,
778 "smb350_irq", dev);
779 if (ret) {
780 pr_err("request_irq %d failed.ret=%d\n", dev->irq, ret);
781 goto err_irq;
782 }
783
Amir Samuelov094534d2012-11-12 14:27:39 +0200784 pr_info("HW Version = 0x%X.\n", version);
785
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200786 return 0;
787
788err_irq:
789err_debugfs:
790 if (dev->dent)
791 debugfs_remove_recursive(dev->dent);
Amir Samuelovfef02172012-11-01 20:19:22 +0200792err_no_dev:
Amir Samuelovcaaa8f02012-10-25 11:11:57 +0200793err_set_params:
794 gpio_free(dev->chg_en_n_gpio);
795err_en_gpio:
796 gpio_free(dev->chg_susp_n_gpio);
797err_susp_gpio:
798 gpio_free(dev->stat_gpio);
799err_stat_gpio:
800 kfree(smb350_dev);
801 smb350_dev = NULL;
802
803 pr_info("FAIL.\n");
804
805 return ret;
806}
807
808static int __devexit smb350_remove(struct i2c_client *client)
809{
810 struct smb350_device *dev = i2c_get_clientdata(client);
811
812 power_supply_unregister(&dev->dc_psy);
813 gpio_free(dev->chg_en_n_gpio);
814 gpio_free(dev->chg_susp_n_gpio);
815 if (dev->stat_gpio)
816 gpio_free(dev->stat_gpio);
817 if (dev->irq)
818 free_irq(dev->irq, dev);
819 if (dev->dent)
820 debugfs_remove_recursive(dev->dent);
821 kfree(smb350_dev);
822 smb350_dev = NULL;
823
824 return 0;
825}
826
827static const struct i2c_device_id smb350_id[] = {
828 {SMB350_NAME, 0},
829 {},
830};
831MODULE_DEVICE_TABLE(i2c, smb350_id);
832
833static const struct of_device_id smb350_match[] = {
834 { .compatible = "summit,smb350-charger", },
835 { },
836};
837
838static struct i2c_driver smb350_driver = {
839 .driver = {
840 .name = SMB350_NAME,
841 .owner = THIS_MODULE,
842 .of_match_table = of_match_ptr(smb350_match),
843 },
844 .probe = smb350_probe,
845 .remove = __devexit_p(smb350_remove),
846 .id_table = smb350_id,
847};
848
849static int __init smb350_init(void)
850{
851 return i2c_add_driver(&smb350_driver);
852}
853module_init(smb350_init);
854
855static void __exit smb350_exit(void)
856{
857 return i2c_del_driver(&smb350_driver);
858}
859module_exit(smb350_exit);
860
861MODULE_DESCRIPTION("Driver for SMB350 charger chip");
862MODULE_LICENSE("GPL v2");
863MODULE_ALIAS("i2c:" SMB350_NAME);