blob: f8eedd8a676fc68ad21f45b8bfa4ddec55add723 [file] [log] [blame]
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001/*
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
Mika Westerberg96facd22012-05-07 11:26:37 +030014#include <linux/err.h>
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +000015#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>
Mika Westerberg34298d42012-04-16 11:48:41 +030024#include <linux/regmap.h>
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +000025
26/*
27 * Configuration registers. These are mirrored to volatile RAM and can be
28 * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be
29 * reloaded from non-volatile registers after POR.
30 */
31#define CFG_CHARGE_CURRENT 0x00
32#define CFG_CHARGE_CURRENT_FCC_MASK 0xe0
33#define CFG_CHARGE_CURRENT_FCC_SHIFT 5
34#define CFG_CHARGE_CURRENT_PCC_MASK 0x18
35#define CFG_CHARGE_CURRENT_PCC_SHIFT 3
36#define CFG_CHARGE_CURRENT_TC_MASK 0x07
37#define CFG_CURRENT_LIMIT 0x01
38#define CFG_CURRENT_LIMIT_DC_MASK 0xf0
39#define CFG_CURRENT_LIMIT_DC_SHIFT 4
40#define CFG_CURRENT_LIMIT_USB_MASK 0x0f
41#define CFG_FLOAT_VOLTAGE 0x03
Mika Westerberg34298d42012-04-16 11:48:41 +030042#define CFG_FLOAT_VOLTAGE_FLOAT_MASK 0x3f
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +000043#define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0
44#define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6
45#define CFG_STAT 0x05
46#define CFG_STAT_DISABLED BIT(5)
47#define CFG_STAT_ACTIVE_HIGH BIT(7)
48#define CFG_PIN 0x06
49#define CFG_PIN_EN_CTRL_MASK 0x60
50#define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40
51#define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60
52#define CFG_PIN_EN_APSD_IRQ BIT(1)
53#define CFG_PIN_EN_CHARGER_ERROR BIT(2)
54#define CFG_THERM 0x07
55#define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03
56#define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0
57#define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c
58#define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2
59#define CFG_THERM_MONITOR_DISABLED BIT(4)
60#define CFG_SYSOK 0x08
61#define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2)
62#define CFG_OTHER 0x09
63#define CFG_OTHER_RID_MASK 0xc0
64#define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0
65#define CFG_OTG 0x0a
66#define CFG_OTG_TEMP_THRESHOLD_MASK 0x30
67#define CFG_OTG_TEMP_THRESHOLD_SHIFT 4
68#define CFG_OTG_CC_COMPENSATION_MASK 0xc0
69#define CFG_OTG_CC_COMPENSATION_SHIFT 6
70#define CFG_TEMP_LIMIT 0x0b
71#define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03
72#define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0
73#define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c
74#define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2
75#define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30
76#define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4
77#define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0
78#define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6
79#define CFG_FAULT_IRQ 0x0c
80#define CFG_FAULT_IRQ_DCIN_UV BIT(2)
81#define CFG_STATUS_IRQ 0x0d
82#define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4)
83#define CFG_ADDRESS 0x0e
84
85/* Command registers */
86#define CMD_A 0x30
87#define CMD_A_CHG_ENABLED BIT(1)
88#define CMD_A_SUSPEND_ENABLED BIT(2)
89#define CMD_A_ALLOW_WRITE BIT(7)
90#define CMD_B 0x31
91#define CMD_C 0x33
92
93/* Interrupt Status registers */
94#define IRQSTAT_A 0x35
95#define IRQSTAT_C 0x37
96#define IRQSTAT_C_TERMINATION_STAT BIT(0)
97#define IRQSTAT_C_TERMINATION_IRQ BIT(1)
98#define IRQSTAT_C_TAPER_IRQ BIT(3)
99#define IRQSTAT_E 0x39
100#define IRQSTAT_E_USBIN_UV_STAT BIT(0)
101#define IRQSTAT_E_USBIN_UV_IRQ BIT(1)
102#define IRQSTAT_E_DCIN_UV_STAT BIT(4)
103#define IRQSTAT_E_DCIN_UV_IRQ BIT(5)
104#define IRQSTAT_F 0x3a
105
106/* Status registers */
107#define STAT_A 0x3b
108#define STAT_A_FLOAT_VOLTAGE_MASK 0x3f
109#define STAT_B 0x3c
110#define STAT_C 0x3d
111#define STAT_C_CHG_ENABLED BIT(0)
112#define STAT_C_CHG_MASK 0x06
113#define STAT_C_CHG_SHIFT 1
114#define STAT_C_CHARGER_ERROR BIT(6)
115#define STAT_E 0x3f
116
Mika Westerberg34298d42012-04-16 11:48:41 +0300117#define SMB347_MAX_REGISTER 0x3f
118
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000119/**
120 * struct smb347_charger - smb347 charger instance
121 * @lock: protects concurrent access to online variables
Mika Westerberg34298d42012-04-16 11:48:41 +0300122 * @dev: pointer to device
123 * @regmap: pointer to driver regmap
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000124 * @mains: power_supply instance for AC/DC power
125 * @usb: power_supply instance for USB power
126 * @battery: power_supply instance for battery
127 * @mains_online: is AC/DC input connected
128 * @usb_online: is USB input connected
129 * @charging_enabled: is charging enabled
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000130 * @pdata: pointer to platform data
131 */
132struct smb347_charger {
133 struct mutex lock;
Mika Westerberg34298d42012-04-16 11:48:41 +0300134 struct device *dev;
135 struct regmap *regmap;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000136 struct power_supply mains;
137 struct power_supply usb;
138 struct power_supply battery;
139 bool mains_online;
140 bool usb_online;
141 bool charging_enabled;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000142 const struct smb347_charger_platform_data *pdata;
143};
144
145/* Fast charge current in uA */
146static const unsigned int fcc_tbl[] = {
147 700000,
148 900000,
149 1200000,
150 1500000,
151 1800000,
152 2000000,
153 2200000,
154 2500000,
155};
156
157/* Pre-charge current in uA */
158static const unsigned int pcc_tbl[] = {
159 100000,
160 150000,
161 200000,
162 250000,
163};
164
165/* Termination current in uA */
166static const unsigned int tc_tbl[] = {
167 37500,
168 50000,
169 100000,
170 150000,
171 200000,
172 250000,
173 500000,
174 600000,
175};
176
177/* Input current limit in uA */
178static const unsigned int icl_tbl[] = {
179 300000,
180 500000,
181 700000,
182 900000,
183 1200000,
184 1500000,
185 1800000,
186 2000000,
187 2200000,
188 2500000,
189};
190
191/* Charge current compensation in uA */
192static const unsigned int ccc_tbl[] = {
193 250000,
194 700000,
195 900000,
196 1200000,
197};
198
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000199/* Convert current to register value using lookup table */
200static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
201{
202 size_t i;
203
204 for (i = 0; i < size; i++)
205 if (val < tbl[i])
206 break;
207 return i > 0 ? i - 1 : -EINVAL;
208}
209
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000210/**
Mika Westerberg055d7f02012-04-16 11:48:39 +0300211 * smb347_update_ps_status - refreshes the power source status
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000212 * @smb: pointer to smb347 charger instance
213 *
Mika Westerberg055d7f02012-04-16 11:48:39 +0300214 * Function checks whether any power source is connected to the charger and
215 * updates internal state accordingly. If there is a change to previous state
216 * function returns %1, otherwise %0 and negative errno in case of errror.
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000217 */
Mika Westerberg055d7f02012-04-16 11:48:39 +0300218static int smb347_update_ps_status(struct smb347_charger *smb)
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000219{
220 bool usb = false;
221 bool dc = false;
Mika Westerberg34298d42012-04-16 11:48:41 +0300222 unsigned int val;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000223 int ret;
224
Mika Westerberg34298d42012-04-16 11:48:41 +0300225 ret = regmap_read(smb->regmap, IRQSTAT_E, &val);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000226 if (ret < 0)
227 return ret;
228
229 /*
230 * Dc and usb are set depending on whether they are enabled in
231 * platform data _and_ whether corresponding undervoltage is set.
232 */
233 if (smb->pdata->use_mains)
Mika Westerberg34298d42012-04-16 11:48:41 +0300234 dc = !(val & IRQSTAT_E_DCIN_UV_STAT);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000235 if (smb->pdata->use_usb)
Mika Westerberg34298d42012-04-16 11:48:41 +0300236 usb = !(val & IRQSTAT_E_USBIN_UV_STAT);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000237
238 mutex_lock(&smb->lock);
239 ret = smb->mains_online != dc || smb->usb_online != usb;
240 smb->mains_online = dc;
241 smb->usb_online = usb;
242 mutex_unlock(&smb->lock);
243
244 return ret;
245}
246
247/*
Mika Westerberg055d7f02012-04-16 11:48:39 +0300248 * smb347_is_ps_online - returns whether input power source is connected
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000249 * @smb: pointer to smb347 charger instance
250 *
251 * Returns %true if input power source is connected. Note that this is
252 * dependent on what platform has configured for usable power sources. For
Mika Westerberg055d7f02012-04-16 11:48:39 +0300253 * example if USB is disabled, this will return %false even if the USB cable
254 * is connected.
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000255 */
Mika Westerberg055d7f02012-04-16 11:48:39 +0300256static bool smb347_is_ps_online(struct smb347_charger *smb)
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000257{
258 bool ret;
259
260 mutex_lock(&smb->lock);
261 ret = smb->usb_online || smb->mains_online;
262 mutex_unlock(&smb->lock);
263
264 return ret;
265}
266
267/**
268 * smb347_charging_status - returns status of charging
269 * @smb: pointer to smb347 charger instance
270 *
271 * Function returns charging status. %0 means no charging is in progress,
272 * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
273 */
274static int smb347_charging_status(struct smb347_charger *smb)
275{
Mika Westerberg34298d42012-04-16 11:48:41 +0300276 unsigned int val;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000277 int ret;
278
Mika Westerberg055d7f02012-04-16 11:48:39 +0300279 if (!smb347_is_ps_online(smb))
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000280 return 0;
281
Mika Westerberg34298d42012-04-16 11:48:41 +0300282 ret = regmap_read(smb->regmap, STAT_C, &val);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000283 if (ret < 0)
284 return 0;
285
Mika Westerberg34298d42012-04-16 11:48:41 +0300286 return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000287}
288
289static int smb347_charging_set(struct smb347_charger *smb, bool enable)
290{
291 int ret = 0;
292
293 if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) {
Mika Westerberg34298d42012-04-16 11:48:41 +0300294 dev_dbg(smb->dev, "charging enable/disable in SW disabled\n");
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000295 return 0;
296 }
297
298 mutex_lock(&smb->lock);
299 if (smb->charging_enabled != enable) {
Mika Westerberg34298d42012-04-16 11:48:41 +0300300 ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED,
301 enable ? CMD_A_CHG_ENABLED : 0);
302 if (!ret)
303 smb->charging_enabled = enable;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000304 }
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000305 mutex_unlock(&smb->lock);
306 return ret;
307}
308
309static inline int smb347_charging_enable(struct smb347_charger *smb)
310{
311 return smb347_charging_set(smb, true);
312}
313
314static inline int smb347_charging_disable(struct smb347_charger *smb)
315{
316 return smb347_charging_set(smb, false);
317}
318
Mika Westerberg055d7f02012-04-16 11:48:39 +0300319static int smb347_start_stop_charging(struct smb347_charger *smb)
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000320{
321 int ret;
322
323 /*
324 * Depending on whether valid power source is connected or not, we
325 * disable or enable the charging. We do it manually because it
326 * depends on how the platform has configured the valid inputs.
327 */
Mika Westerberg055d7f02012-04-16 11:48:39 +0300328 if (smb347_is_ps_online(smb)) {
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000329 ret = smb347_charging_enable(smb);
330 if (ret < 0)
Mika Westerberg34298d42012-04-16 11:48:41 +0300331 dev_err(smb->dev, "failed to enable charging\n");
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000332 } else {
333 ret = smb347_charging_disable(smb);
334 if (ret < 0)
Mika Westerberg34298d42012-04-16 11:48:41 +0300335 dev_err(smb->dev, "failed to disable charging\n");
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000336 }
337
338 return ret;
339}
340
341static int smb347_set_charge_current(struct smb347_charger *smb)
342{
Mika Westerberg34298d42012-04-16 11:48:41 +0300343 int ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000344
345 if (smb->pdata->max_charge_current) {
Mika Westerberg34298d42012-04-16 11:48:41 +0300346 ret = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl),
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000347 smb->pdata->max_charge_current);
Mika Westerberg34298d42012-04-16 11:48:41 +0300348 if (ret < 0)
349 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000350
Mika Westerberg34298d42012-04-16 11:48:41 +0300351 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
352 CFG_CHARGE_CURRENT_FCC_MASK,
353 ret << CFG_CHARGE_CURRENT_FCC_SHIFT);
354 if (ret < 0)
355 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000356 }
357
358 if (smb->pdata->pre_charge_current) {
Mika Westerberg34298d42012-04-16 11:48:41 +0300359 ret = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl),
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000360 smb->pdata->pre_charge_current);
Mika Westerberg34298d42012-04-16 11:48:41 +0300361 if (ret < 0)
362 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000363
Mika Westerberg34298d42012-04-16 11:48:41 +0300364 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
365 CFG_CHARGE_CURRENT_PCC_MASK,
366 ret << CFG_CHARGE_CURRENT_PCC_SHIFT);
367 if (ret < 0)
368 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000369 }
370
371 if (smb->pdata->termination_current) {
Mika Westerberg34298d42012-04-16 11:48:41 +0300372 ret = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl),
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000373 smb->pdata->termination_current);
Mika Westerberg34298d42012-04-16 11:48:41 +0300374 if (ret < 0)
375 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000376
Mika Westerberg34298d42012-04-16 11:48:41 +0300377 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
378 CFG_CHARGE_CURRENT_TC_MASK, ret);
379 if (ret < 0)
380 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000381 }
382
Mika Westerberg34298d42012-04-16 11:48:41 +0300383 return 0;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000384}
385
386static int smb347_set_current_limits(struct smb347_charger *smb)
387{
Mika Westerberg34298d42012-04-16 11:48:41 +0300388 int ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000389
390 if (smb->pdata->mains_current_limit) {
Mika Westerberg34298d42012-04-16 11:48:41 +0300391 ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000392 smb->pdata->mains_current_limit);
Mika Westerberg34298d42012-04-16 11:48:41 +0300393 if (ret < 0)
394 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000395
Mika Westerberg34298d42012-04-16 11:48:41 +0300396 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
397 CFG_CURRENT_LIMIT_DC_MASK,
398 ret << CFG_CURRENT_LIMIT_DC_SHIFT);
399 if (ret < 0)
400 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000401 }
402
403 if (smb->pdata->usb_hc_current_limit) {
Mika Westerberg34298d42012-04-16 11:48:41 +0300404 ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000405 smb->pdata->usb_hc_current_limit);
Mika Westerberg34298d42012-04-16 11:48:41 +0300406 if (ret < 0)
407 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000408
Mika Westerberg34298d42012-04-16 11:48:41 +0300409 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
410 CFG_CURRENT_LIMIT_USB_MASK, ret);
411 if (ret < 0)
412 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000413 }
414
Mika Westerberg34298d42012-04-16 11:48:41 +0300415 return 0;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000416}
417
418static int smb347_set_voltage_limits(struct smb347_charger *smb)
419{
Mika Westerberg34298d42012-04-16 11:48:41 +0300420 int ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000421
422 if (smb->pdata->pre_to_fast_voltage) {
Mika Westerberg34298d42012-04-16 11:48:41 +0300423 ret = smb->pdata->pre_to_fast_voltage;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000424
425 /* uV */
Mika Westerberg34298d42012-04-16 11:48:41 +0300426 ret = clamp_val(ret, 2400000, 3000000) - 2400000;
427 ret /= 200000;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000428
Mika Westerberg34298d42012-04-16 11:48:41 +0300429 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
430 CFG_FLOAT_VOLTAGE_THRESHOLD_MASK,
431 ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT);
432 if (ret < 0)
433 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000434 }
435
436 if (smb->pdata->max_charge_voltage) {
Mika Westerberg34298d42012-04-16 11:48:41 +0300437 ret = smb->pdata->max_charge_voltage;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000438
439 /* uV */
Mika Westerberg34298d42012-04-16 11:48:41 +0300440 ret = clamp_val(ret, 3500000, 4500000) - 3500000;
441 ret /= 20000;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000442
Mika Westerberg34298d42012-04-16 11:48:41 +0300443 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
444 CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret);
445 if (ret < 0)
446 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000447 }
448
Mika Westerberg34298d42012-04-16 11:48:41 +0300449 return 0;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000450}
451
452static int smb347_set_temp_limits(struct smb347_charger *smb)
453{
454 bool enable_therm_monitor = false;
Mika Westerberg34298d42012-04-16 11:48:41 +0300455 int ret = 0;
456 int val;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000457
458 if (smb->pdata->chip_temp_threshold) {
459 val = smb->pdata->chip_temp_threshold;
460
461 /* degree C */
462 val = clamp_val(val, 100, 130) - 100;
463 val /= 10;
464
Mika Westerberg34298d42012-04-16 11:48:41 +0300465 ret = regmap_update_bits(smb->regmap, CFG_OTG,
466 CFG_OTG_TEMP_THRESHOLD_MASK,
467 val << CFG_OTG_TEMP_THRESHOLD_SHIFT);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000468 if (ret < 0)
469 return ret;
470 }
471
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000472 if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
473 val = smb->pdata->soft_cold_temp_limit;
474
475 val = clamp_val(val, 0, 15);
476 val /= 5;
477 /* this goes from higher to lower so invert the value */
478 val = ~val & 0x3;
479
Mika Westerberg34298d42012-04-16 11:48:41 +0300480 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
481 CFG_TEMP_LIMIT_SOFT_COLD_MASK,
482 val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT);
483 if (ret < 0)
484 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000485
486 enable_therm_monitor = true;
487 }
488
489 if (smb->pdata->soft_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
490 val = smb->pdata->soft_hot_temp_limit;
491
492 val = clamp_val(val, 40, 55) - 40;
493 val /= 5;
494
Mika Westerberg34298d42012-04-16 11:48:41 +0300495 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
496 CFG_TEMP_LIMIT_SOFT_HOT_MASK,
497 val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT);
498 if (ret < 0)
499 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000500
501 enable_therm_monitor = true;
502 }
503
504 if (smb->pdata->hard_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
505 val = smb->pdata->hard_cold_temp_limit;
506
507 val = clamp_val(val, -5, 10) + 5;
508 val /= 5;
509 /* this goes from higher to lower so invert the value */
510 val = ~val & 0x3;
511
Mika Westerberg34298d42012-04-16 11:48:41 +0300512 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
513 CFG_TEMP_LIMIT_HARD_COLD_MASK,
514 val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT);
515 if (ret < 0)
516 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000517
518 enable_therm_monitor = true;
519 }
520
521 if (smb->pdata->hard_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
522 val = smb->pdata->hard_hot_temp_limit;
523
524 val = clamp_val(val, 50, 65) - 50;
525 val /= 5;
526
Mika Westerberg34298d42012-04-16 11:48:41 +0300527 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
528 CFG_TEMP_LIMIT_HARD_HOT_MASK,
529 val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT);
530 if (ret < 0)
531 return ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000532
533 enable_therm_monitor = true;
534 }
535
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000536 /*
537 * If any of the temperature limits are set, we also enable the
538 * thermistor monitoring.
539 *
540 * When soft limits are hit, the device will start to compensate
541 * current and/or voltage depending on the configuration.
542 *
543 * When hard limit is hit, the device will suspend charging
544 * depending on the configuration.
545 */
546 if (enable_therm_monitor) {
Mika Westerberg34298d42012-04-16 11:48:41 +0300547 ret = regmap_update_bits(smb->regmap, CFG_THERM,
548 CFG_THERM_MONITOR_DISABLED, 0);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000549 if (ret < 0)
550 return ret;
551 }
552
553 if (smb->pdata->suspend_on_hard_temp_limit) {
Mika Westerberg34298d42012-04-16 11:48:41 +0300554 ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
555 CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000556 if (ret < 0)
557 return ret;
558 }
559
560 if (smb->pdata->soft_temp_limit_compensation !=
561 SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) {
562 val = smb->pdata->soft_temp_limit_compensation & 0x3;
563
Mika Westerberg34298d42012-04-16 11:48:41 +0300564 ret = regmap_update_bits(smb->regmap, CFG_THERM,
565 CFG_THERM_SOFT_HOT_COMPENSATION_MASK,
566 val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000567 if (ret < 0)
568 return ret;
569
Mika Westerberg34298d42012-04-16 11:48:41 +0300570 ret = regmap_update_bits(smb->regmap, CFG_THERM,
571 CFG_THERM_SOFT_COLD_COMPENSATION_MASK,
572 val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000573 if (ret < 0)
574 return ret;
575 }
576
577 if (smb->pdata->charge_current_compensation) {
578 val = current_to_hw(ccc_tbl, ARRAY_SIZE(ccc_tbl),
579 smb->pdata->charge_current_compensation);
580 if (val < 0)
581 return val;
582
Mika Westerberg34298d42012-04-16 11:48:41 +0300583 ret = regmap_update_bits(smb->regmap, CFG_OTG,
584 CFG_OTG_CC_COMPENSATION_MASK,
585 (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000586 if (ret < 0)
587 return ret;
588 }
589
590 return ret;
591}
592
593/*
594 * smb347_set_writable - enables/disables writing to non-volatile registers
595 * @smb: pointer to smb347 charger instance
596 *
597 * You can enable/disable writing to the non-volatile configuration
598 * registers by calling this function.
599 *
600 * Returns %0 on success and negative errno in case of failure.
601 */
602static int smb347_set_writable(struct smb347_charger *smb, bool writable)
603{
Mika Westerberg34298d42012-04-16 11:48:41 +0300604 return regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE,
605 writable ? CMD_A_ALLOW_WRITE : 0);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000606}
607
608static int smb347_hw_init(struct smb347_charger *smb)
609{
Mika Westerberg34298d42012-04-16 11:48:41 +0300610 unsigned int val;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000611 int ret;
612
613 ret = smb347_set_writable(smb, true);
614 if (ret < 0)
615 return ret;
616
617 /*
618 * Program the platform specific configuration values to the device
619 * first.
620 */
621 ret = smb347_set_charge_current(smb);
622 if (ret < 0)
623 goto fail;
624
625 ret = smb347_set_current_limits(smb);
626 if (ret < 0)
627 goto fail;
628
629 ret = smb347_set_voltage_limits(smb);
630 if (ret < 0)
631 goto fail;
632
633 ret = smb347_set_temp_limits(smb);
634 if (ret < 0)
635 goto fail;
636
637 /* If USB charging is disabled we put the USB in suspend mode */
638 if (!smb->pdata->use_usb) {
Mika Westerberg34298d42012-04-16 11:48:41 +0300639 ret = regmap_update_bits(smb->regmap, CMD_A,
640 CMD_A_SUSPEND_ENABLED,
641 CMD_A_SUSPEND_ENABLED);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000642 if (ret < 0)
643 goto fail;
644 }
645
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000646 /*
647 * If configured by platform data, we enable hardware Auto-OTG
648 * support for driving VBUS. Otherwise we disable it.
649 */
Mika Westerberg34298d42012-04-16 11:48:41 +0300650 ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK,
651 smb->pdata->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000652 if (ret < 0)
653 goto fail;
654
655 /*
656 * Make the charging functionality controllable by a write to the
657 * command register unless pin control is specified in the platform
658 * data.
659 */
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000660 switch (smb->pdata->enable_control) {
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000661 case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW:
Mika Westerberg34298d42012-04-16 11:48:41 +0300662 val = CFG_PIN_EN_CTRL_ACTIVE_LOW;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000663 break;
664 case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH:
Mika Westerberg34298d42012-04-16 11:48:41 +0300665 val = CFG_PIN_EN_CTRL_ACTIVE_HIGH;
666 break;
667 default:
668 val = 0;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000669 break;
670 }
671
Mika Westerberg34298d42012-04-16 11:48:41 +0300672 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK,
673 val);
674 if (ret < 0)
675 goto fail;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000676
Mika Westerberg34298d42012-04-16 11:48:41 +0300677 /* Disable Automatic Power Source Detection (APSD) interrupt. */
678 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000679 if (ret < 0)
680 goto fail;
681
Mika Westerberg055d7f02012-04-16 11:48:39 +0300682 ret = smb347_update_ps_status(smb);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000683 if (ret < 0)
684 goto fail;
685
Mika Westerberg055d7f02012-04-16 11:48:39 +0300686 ret = smb347_start_stop_charging(smb);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000687
688fail:
689 smb347_set_writable(smb, false);
690 return ret;
691}
692
693static irqreturn_t smb347_interrupt(int irq, void *data)
694{
695 struct smb347_charger *smb = data;
Mika Westerberg34298d42012-04-16 11:48:41 +0300696 unsigned int stat_c, irqstat_e, irqstat_c;
697 bool handled = false;
698 int ret;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000699
Mika Westerberg34298d42012-04-16 11:48:41 +0300700 ret = regmap_read(smb->regmap, STAT_C, &stat_c);
701 if (ret < 0) {
702 dev_warn(smb->dev, "reading STAT_C failed\n");
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000703 return IRQ_NONE;
704 }
705
Mika Westerberg34298d42012-04-16 11:48:41 +0300706 ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c);
707 if (ret < 0) {
708 dev_warn(smb->dev, "reading IRQSTAT_C failed\n");
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000709 return IRQ_NONE;
710 }
711
Mika Westerberg34298d42012-04-16 11:48:41 +0300712 ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e);
713 if (ret < 0) {
714 dev_warn(smb->dev, "reading IRQSTAT_E failed\n");
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000715 return IRQ_NONE;
716 }
717
718 /*
719 * If we get charger error we report the error back to user and
720 * disable charging.
721 */
722 if (stat_c & STAT_C_CHARGER_ERROR) {
Mika Westerberg34298d42012-04-16 11:48:41 +0300723 dev_err(smb->dev, "error in charger, disabling charging\n");
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000724
725 smb347_charging_disable(smb);
726 power_supply_changed(&smb->battery);
Mika Westerberg34298d42012-04-16 11:48:41 +0300727 handled = true;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000728 }
729
730 /*
731 * If we reached the termination current the battery is charged and
732 * we can update the status now. Charging is automatically
733 * disabled by the hardware.
734 */
735 if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) {
736 if (irqstat_c & IRQSTAT_C_TERMINATION_STAT)
737 power_supply_changed(&smb->battery);
Mika Westerberg34298d42012-04-16 11:48:41 +0300738 handled = true;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000739 }
740
741 /*
742 * If we got an under voltage interrupt it means that AC/USB input
743 * was connected or disconnected.
744 */
745 if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) {
Mika Westerberg055d7f02012-04-16 11:48:39 +0300746 if (smb347_update_ps_status(smb) > 0) {
747 smb347_start_stop_charging(smb);
Ramakrishna Pallalae6fe3592012-04-19 10:00:18 +0530748 if (smb->pdata->use_mains)
749 power_supply_changed(&smb->mains);
750 if (smb->pdata->use_usb)
751 power_supply_changed(&smb->usb);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000752 }
Mika Westerberg34298d42012-04-16 11:48:41 +0300753 handled = true;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000754 }
755
Mika Westerberg34298d42012-04-16 11:48:41 +0300756 return handled ? IRQ_HANDLED : IRQ_NONE;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000757}
758
759static int smb347_irq_set(struct smb347_charger *smb, bool enable)
760{
761 int ret;
762
763 ret = smb347_set_writable(smb, true);
764 if (ret < 0)
765 return ret;
766
767 /*
768 * Enable/disable interrupts for:
769 * - under voltage
770 * - termination current reached
771 * - charger error
772 */
Mika Westerberg34298d42012-04-16 11:48:41 +0300773 ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff,
774 enable ? CFG_FAULT_IRQ_DCIN_UV : 0);
775 if (ret < 0)
776 goto fail;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000777
Mika Westerberg34298d42012-04-16 11:48:41 +0300778 ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff,
779 enable ? CFG_STATUS_IRQ_TERMINATION_OR_TAPER : 0);
780 if (ret < 0)
781 goto fail;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000782
Mika Westerberg34298d42012-04-16 11:48:41 +0300783 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR,
784 enable ? CFG_PIN_EN_CHARGER_ERROR : 0);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000785fail:
786 smb347_set_writable(smb, false);
787 return ret;
788}
789
790static inline int smb347_irq_enable(struct smb347_charger *smb)
791{
792 return smb347_irq_set(smb, true);
793}
794
795static inline int smb347_irq_disable(struct smb347_charger *smb)
796{
797 return smb347_irq_set(smb, false);
798}
799
Mika Westerberg34298d42012-04-16 11:48:41 +0300800static int smb347_irq_init(struct smb347_charger *smb,
801 struct i2c_client *client)
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000802{
803 const struct smb347_charger_platform_data *pdata = smb->pdata;
804 int ret, irq = gpio_to_irq(pdata->irq_gpio);
805
Mika Westerberg34298d42012-04-16 11:48:41 +0300806 ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000807 if (ret < 0)
808 goto fail;
809
810 ret = request_threaded_irq(irq, NULL, smb347_interrupt,
Mika Westerberg34298d42012-04-16 11:48:41 +0300811 IRQF_TRIGGER_FALLING, client->name, smb);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000812 if (ret < 0)
813 goto fail_gpio;
814
815 ret = smb347_set_writable(smb, true);
816 if (ret < 0)
817 goto fail_irq;
818
819 /*
820 * Configure the STAT output to be suitable for interrupts: disable
821 * all other output (except interrupts) and make it active low.
822 */
Mika Westerberg34298d42012-04-16 11:48:41 +0300823 ret = regmap_update_bits(smb->regmap, CFG_STAT,
824 CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED,
825 CFG_STAT_DISABLED);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000826 if (ret < 0)
827 goto fail_readonly;
828
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000829 smb347_set_writable(smb, false);
Mika Westerberg34298d42012-04-16 11:48:41 +0300830 client->irq = irq;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000831 return 0;
832
833fail_readonly:
834 smb347_set_writable(smb, false);
835fail_irq:
836 free_irq(irq, smb);
837fail_gpio:
838 gpio_free(pdata->irq_gpio);
839fail:
Mika Westerberg34298d42012-04-16 11:48:41 +0300840 client->irq = 0;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000841 return ret;
842}
843
844static int smb347_mains_get_property(struct power_supply *psy,
845 enum power_supply_property prop,
846 union power_supply_propval *val)
847{
848 struct smb347_charger *smb =
849 container_of(psy, struct smb347_charger, mains);
850
851 if (prop == POWER_SUPPLY_PROP_ONLINE) {
852 val->intval = smb->mains_online;
853 return 0;
854 }
855 return -EINVAL;
856}
857
858static enum power_supply_property smb347_mains_properties[] = {
859 POWER_SUPPLY_PROP_ONLINE,
860};
861
862static int smb347_usb_get_property(struct power_supply *psy,
863 enum power_supply_property prop,
864 union power_supply_propval *val)
865{
866 struct smb347_charger *smb =
867 container_of(psy, struct smb347_charger, usb);
868
869 if (prop == POWER_SUPPLY_PROP_ONLINE) {
870 val->intval = smb->usb_online;
871 return 0;
872 }
873 return -EINVAL;
874}
875
876static enum power_supply_property smb347_usb_properties[] = {
877 POWER_SUPPLY_PROP_ONLINE,
878};
879
880static int smb347_battery_get_property(struct power_supply *psy,
881 enum power_supply_property prop,
882 union power_supply_propval *val)
883{
884 struct smb347_charger *smb =
885 container_of(psy, struct smb347_charger, battery);
886 const struct smb347_charger_platform_data *pdata = smb->pdata;
887 int ret;
888
Mika Westerberg055d7f02012-04-16 11:48:39 +0300889 ret = smb347_update_ps_status(smb);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000890 if (ret < 0)
891 return ret;
892
893 switch (prop) {
894 case POWER_SUPPLY_PROP_STATUS:
Mika Westerberg055d7f02012-04-16 11:48:39 +0300895 if (!smb347_is_ps_online(smb)) {
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000896 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
897 break;
898 }
899 if (smb347_charging_status(smb))
900 val->intval = POWER_SUPPLY_STATUS_CHARGING;
901 else
902 val->intval = POWER_SUPPLY_STATUS_FULL;
903 break;
904
905 case POWER_SUPPLY_PROP_CHARGE_TYPE:
Mika Westerberg055d7f02012-04-16 11:48:39 +0300906 if (!smb347_is_ps_online(smb))
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000907 return -ENODATA;
908
909 /*
910 * We handle trickle and pre-charging the same, and taper
911 * and none the same.
912 */
913 switch (smb347_charging_status(smb)) {
914 case 1:
915 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
916 break;
917 case 2:
918 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
919 break;
920 default:
921 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
922 break;
923 }
924 break;
925
926 case POWER_SUPPLY_PROP_TECHNOLOGY:
927 val->intval = pdata->battery_info.technology;
928 break;
929
930 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
931 val->intval = pdata->battery_info.voltage_min_design;
932 break;
933
934 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
935 val->intval = pdata->battery_info.voltage_max_design;
936 break;
937
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000938 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
939 val->intval = pdata->battery_info.charge_full_design;
940 break;
941
942 case POWER_SUPPLY_PROP_MODEL_NAME:
943 val->strval = pdata->battery_info.name;
944 break;
945
946 default:
947 return -EINVAL;
948 }
949
950 return 0;
951}
952
953static enum power_supply_property smb347_battery_properties[] = {
954 POWER_SUPPLY_PROP_STATUS,
955 POWER_SUPPLY_PROP_CHARGE_TYPE,
956 POWER_SUPPLY_PROP_TECHNOLOGY,
957 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
958 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000959 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
960 POWER_SUPPLY_PROP_MODEL_NAME,
961};
962
Mika Westerberg34298d42012-04-16 11:48:41 +0300963static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000964{
Mika Westerberg34298d42012-04-16 11:48:41 +0300965 switch (reg) {
966 case IRQSTAT_A:
967 case IRQSTAT_C:
968 case IRQSTAT_E:
969 case IRQSTAT_F:
970 case STAT_A:
971 case STAT_B:
972 case STAT_C:
973 case STAT_E:
974 return true;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000975 }
976
Mika Westerberg34298d42012-04-16 11:48:41 +0300977 return false;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000978}
979
Mika Westerberg34298d42012-04-16 11:48:41 +0300980static bool smb347_readable_reg(struct device *dev, unsigned int reg)
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +0000981{
Mika Westerberg34298d42012-04-16 11:48:41 +0300982 switch (reg) {
983 case CFG_CHARGE_CURRENT:
984 case CFG_CURRENT_LIMIT:
985 case CFG_FLOAT_VOLTAGE:
986 case CFG_STAT:
987 case CFG_PIN:
988 case CFG_THERM:
989 case CFG_SYSOK:
990 case CFG_OTHER:
991 case CFG_OTG:
992 case CFG_TEMP_LIMIT:
993 case CFG_FAULT_IRQ:
994 case CFG_STATUS_IRQ:
995 case CFG_ADDRESS:
996 case CMD_A:
997 case CMD_B:
998 case CMD_C:
999 return true;
1000 }
1001
1002 return smb347_volatile_reg(dev, reg);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001003}
1004
Mika Westerberg34298d42012-04-16 11:48:41 +03001005static const struct regmap_config smb347_regmap = {
1006 .reg_bits = 8,
1007 .val_bits = 8,
1008 .max_register = SMB347_MAX_REGISTER,
1009 .volatile_reg = smb347_volatile_reg,
1010 .readable_reg = smb347_readable_reg,
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001011};
1012
1013static int smb347_probe(struct i2c_client *client,
1014 const struct i2c_device_id *id)
1015{
1016 static char *battery[] = { "smb347-battery" };
1017 const struct smb347_charger_platform_data *pdata;
1018 struct device *dev = &client->dev;
1019 struct smb347_charger *smb;
1020 int ret;
1021
1022 pdata = dev->platform_data;
1023 if (!pdata)
1024 return -EINVAL;
1025
1026 if (!pdata->use_mains && !pdata->use_usb)
1027 return -EINVAL;
1028
1029 smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
1030 if (!smb)
1031 return -ENOMEM;
1032
1033 i2c_set_clientdata(client, smb);
1034
1035 mutex_init(&smb->lock);
Mika Westerberg34298d42012-04-16 11:48:41 +03001036 smb->dev = &client->dev;
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001037 smb->pdata = pdata;
1038
Mika Westerberg34298d42012-04-16 11:48:41 +03001039 smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
1040 if (IS_ERR(smb->regmap))
1041 return PTR_ERR(smb->regmap);
1042
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001043 ret = smb347_hw_init(smb);
1044 if (ret < 0)
1045 return ret;
1046
Ramakrishna Pallalae6fe3592012-04-19 10:00:18 +05301047 if (smb->pdata->use_mains) {
1048 smb->mains.name = "smb347-mains";
1049 smb->mains.type = POWER_SUPPLY_TYPE_MAINS;
1050 smb->mains.get_property = smb347_mains_get_property;
1051 smb->mains.properties = smb347_mains_properties;
1052 smb->mains.num_properties = ARRAY_SIZE(smb347_mains_properties);
1053 smb->mains.supplied_to = battery;
1054 smb->mains.num_supplicants = ARRAY_SIZE(battery);
1055 ret = power_supply_register(dev, &smb->mains);
1056 if (ret < 0)
1057 return ret;
1058 }
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001059
Ramakrishna Pallalae6fe3592012-04-19 10:00:18 +05301060 if (smb->pdata->use_usb) {
1061 smb->usb.name = "smb347-usb";
1062 smb->usb.type = POWER_SUPPLY_TYPE_USB;
1063 smb->usb.get_property = smb347_usb_get_property;
1064 smb->usb.properties = smb347_usb_properties;
1065 smb->usb.num_properties = ARRAY_SIZE(smb347_usb_properties);
1066 smb->usb.supplied_to = battery;
1067 smb->usb.num_supplicants = ARRAY_SIZE(battery);
1068 ret = power_supply_register(dev, &smb->usb);
1069 if (ret < 0) {
1070 if (smb->pdata->use_mains)
1071 power_supply_unregister(&smb->mains);
1072 return ret;
1073 }
1074 }
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001075
1076 smb->battery.name = "smb347-battery";
1077 smb->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1078 smb->battery.get_property = smb347_battery_get_property;
1079 smb->battery.properties = smb347_battery_properties;
1080 smb->battery.num_properties = ARRAY_SIZE(smb347_battery_properties);
1081
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001082
1083 ret = power_supply_register(dev, &smb->battery);
1084 if (ret < 0) {
Ramakrishna Pallalae6fe3592012-04-19 10:00:18 +05301085 if (smb->pdata->use_usb)
1086 power_supply_unregister(&smb->usb);
1087 if (smb->pdata->use_mains)
1088 power_supply_unregister(&smb->mains);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001089 return ret;
1090 }
1091
1092 /*
1093 * Interrupt pin is optional. If it is connected, we setup the
1094 * interrupt support here.
1095 */
1096 if (pdata->irq_gpio >= 0) {
Mika Westerberg34298d42012-04-16 11:48:41 +03001097 ret = smb347_irq_init(smb, client);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001098 if (ret < 0) {
1099 dev_warn(dev, "failed to initialize IRQ: %d\n", ret);
1100 dev_warn(dev, "disabling IRQ support\n");
Mika Westerbergd72bade2012-04-16 11:48:40 +03001101 } else {
1102 smb347_irq_enable(smb);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001103 }
1104 }
1105
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001106 return 0;
1107}
1108
1109static int smb347_remove(struct i2c_client *client)
1110{
1111 struct smb347_charger *smb = i2c_get_clientdata(client);
1112
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001113 if (client->irq) {
1114 smb347_irq_disable(smb);
1115 free_irq(client->irq, smb);
1116 gpio_free(smb->pdata->irq_gpio);
1117 }
1118
1119 power_supply_unregister(&smb->battery);
Ramakrishna Pallalae6fe3592012-04-19 10:00:18 +05301120 if (smb->pdata->use_usb)
1121 power_supply_unregister(&smb->usb);
1122 if (smb->pdata->use_mains)
1123 power_supply_unregister(&smb->mains);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001124 return 0;
1125}
1126
1127static const struct i2c_device_id smb347_id[] = {
1128 { "smb347", 0 },
1129 { }
1130};
1131MODULE_DEVICE_TABLE(i2c, smb347_id);
1132
1133static struct i2c_driver smb347_driver = {
1134 .driver = {
1135 .name = "smb347",
1136 },
1137 .probe = smb347_probe,
1138 .remove = __devexit_p(smb347_remove),
1139 .id_table = smb347_id,
1140};
1141
Mika Westerbergb75ef1d2012-04-16 11:48:38 +03001142module_i2c_driver(smb347_driver);
Bruce E. Robertsoned1a2302012-02-06 15:59:01 +00001143
1144MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1145MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1146MODULE_DESCRIPTION("SMB347 battery charger driver");
1147MODULE_LICENSE("GPL");
1148MODULE_ALIAS("i2c:smb347");