aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/power/smb347-charger.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/power/smb347-charger.c')
-rw-r--r--drivers/power/smb347-charger.c712
1 files changed, 283 insertions, 429 deletions
diff --git a/drivers/power/smb347-charger.c b/drivers/power/smb347-charger.c
index ce1694d1a365..f8eedd8a676f 100644
--- a/drivers/power/smb347-charger.c
+++ b/drivers/power/smb347-charger.c
@@ -11,7 +11,7 @@
11 * published by the Free Software Foundation. 11 * published by the Free Software Foundation.
12 */ 12 */
13 13
14#include <linux/debugfs.h> 14#include <linux/err.h>
15#include <linux/gpio.h> 15#include <linux/gpio.h>
16#include <linux/kernel.h> 16#include <linux/kernel.h>
17#include <linux/module.h> 17#include <linux/module.h>
@@ -21,7 +21,7 @@
21#include <linux/mutex.h> 21#include <linux/mutex.h>
22#include <linux/power_supply.h> 22#include <linux/power_supply.h>
23#include <linux/power/smb347-charger.h> 23#include <linux/power/smb347-charger.h>
24#include <linux/seq_file.h> 24#include <linux/regmap.h>
25 25
26/* 26/*
27 * Configuration registers. These are mirrored to volatile RAM and can be 27 * Configuration registers. These are mirrored to volatile RAM and can be
@@ -39,6 +39,7 @@
39#define CFG_CURRENT_LIMIT_DC_SHIFT 4 39#define CFG_CURRENT_LIMIT_DC_SHIFT 4
40#define CFG_CURRENT_LIMIT_USB_MASK 0x0f 40#define CFG_CURRENT_LIMIT_USB_MASK 0x0f
41#define CFG_FLOAT_VOLTAGE 0x03 41#define CFG_FLOAT_VOLTAGE 0x03
42#define CFG_FLOAT_VOLTAGE_FLOAT_MASK 0x3f
42#define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0 43#define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0
43#define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6 44#define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6
44#define CFG_STAT 0x05 45#define CFG_STAT 0x05
@@ -113,29 +114,31 @@
113#define STAT_C_CHARGER_ERROR BIT(6) 114#define STAT_C_CHARGER_ERROR BIT(6)
114#define STAT_E 0x3f 115#define STAT_E 0x3f
115 116
117#define SMB347_MAX_REGISTER 0x3f
118
116/** 119/**
117 * struct smb347_charger - smb347 charger instance 120 * struct smb347_charger - smb347 charger instance
118 * @lock: protects concurrent access to online variables 121 * @lock: protects concurrent access to online variables
119 * @client: pointer to i2c client 122 * @dev: pointer to device
123 * @regmap: pointer to driver regmap
120 * @mains: power_supply instance for AC/DC power 124 * @mains: power_supply instance for AC/DC power
121 * @usb: power_supply instance for USB power 125 * @usb: power_supply instance for USB power
122 * @battery: power_supply instance for battery 126 * @battery: power_supply instance for battery
123 * @mains_online: is AC/DC input connected 127 * @mains_online: is AC/DC input connected
124 * @usb_online: is USB input connected 128 * @usb_online: is USB input connected
125 * @charging_enabled: is charging enabled 129 * @charging_enabled: is charging enabled
126 * @dentry: for debugfs
127 * @pdata: pointer to platform data 130 * @pdata: pointer to platform data
128 */ 131 */
129struct smb347_charger { 132struct smb347_charger {
130 struct mutex lock; 133 struct mutex lock;
131 struct i2c_client *client; 134 struct device *dev;
135 struct regmap *regmap;
132 struct power_supply mains; 136 struct power_supply mains;
133 struct power_supply usb; 137 struct power_supply usb;
134 struct power_supply battery; 138 struct power_supply battery;
135 bool mains_online; 139 bool mains_online;
136 bool usb_online; 140 bool usb_online;
137 bool charging_enabled; 141 bool charging_enabled;
138 struct dentry *dentry;
139 const struct smb347_charger_platform_data *pdata; 142 const struct smb347_charger_platform_data *pdata;
140}; 143};
141 144
@@ -193,14 +196,6 @@ static const unsigned int ccc_tbl[] = {
193 1200000, 196 1200000,
194}; 197};
195 198
196/* Convert register value to current using lookup table */
197static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val)
198{
199 if (val >= size)
200 return -EINVAL;
201 return tbl[val];
202}
203
204/* Convert current to register value using lookup table */ 199/* Convert current to register value using lookup table */
205static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val) 200static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
206{ 201{
@@ -212,43 +207,22 @@ static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
212 return i > 0 ? i - 1 : -EINVAL; 207 return i > 0 ? i - 1 : -EINVAL;
213} 208}
214 209
215static int smb347_read(struct smb347_charger *smb, u8 reg)
216{
217 int ret;
218
219 ret = i2c_smbus_read_byte_data(smb->client, reg);
220 if (ret < 0)
221 dev_warn(&smb->client->dev, "failed to read reg 0x%x: %d\n",
222 reg, ret);
223 return ret;
224}
225
226static int smb347_write(struct smb347_charger *smb, u8 reg, u8 val)
227{
228 int ret;
229
230 ret = i2c_smbus_write_byte_data(smb->client, reg, val);
231 if (ret < 0)
232 dev_warn(&smb->client->dev, "failed to write reg 0x%x: %d\n",
233 reg, ret);
234 return ret;
235}
236
237/** 210/**
238 * smb347_update_status - updates the charging status 211 * smb347_update_ps_status - refreshes the power source status
239 * @smb: pointer to smb347 charger instance 212 * @smb: pointer to smb347 charger instance
240 * 213 *
241 * Function checks status of the charging and updates internal state 214 * Function checks whether any power source is connected to the charger and
242 * accordingly. Returns %0 if there is no change in status, %1 if the 215 * updates internal state accordingly. If there is a change to previous state
243 * status has changed and negative errno in case of failure. 216 * function returns %1, otherwise %0 and negative errno in case of errror.
244 */ 217 */
245static int smb347_update_status(struct smb347_charger *smb) 218static int smb347_update_ps_status(struct smb347_charger *smb)
246{ 219{
247 bool usb = false; 220 bool usb = false;
248 bool dc = false; 221 bool dc = false;
222 unsigned int val;
249 int ret; 223 int ret;
250 224
251 ret = smb347_read(smb, IRQSTAT_E); 225 ret = regmap_read(smb->regmap, IRQSTAT_E, &val);
252 if (ret < 0) 226 if (ret < 0)
253 return ret; 227 return ret;
254 228
@@ -257,9 +231,9 @@ static int smb347_update_status(struct smb347_charger *smb)
257 * platform data _and_ whether corresponding undervoltage is set. 231 * platform data _and_ whether corresponding undervoltage is set.
258 */ 232 */
259 if (smb->pdata->use_mains) 233 if (smb->pdata->use_mains)
260 dc = !(ret & IRQSTAT_E_DCIN_UV_STAT); 234 dc = !(val & IRQSTAT_E_DCIN_UV_STAT);
261 if (smb->pdata->use_usb) 235 if (smb->pdata->use_usb)
262 usb = !(ret & IRQSTAT_E_USBIN_UV_STAT); 236 usb = !(val & IRQSTAT_E_USBIN_UV_STAT);
263 237
264 mutex_lock(&smb->lock); 238 mutex_lock(&smb->lock);
265 ret = smb->mains_online != dc || smb->usb_online != usb; 239 ret = smb->mains_online != dc || smb->usb_online != usb;
@@ -271,15 +245,15 @@ static int smb347_update_status(struct smb347_charger *smb)
271} 245}
272 246
273/* 247/*
274 * smb347_is_online - returns whether input power source is connected 248 * smb347_is_ps_online - returns whether input power source is connected
275 * @smb: pointer to smb347 charger instance 249 * @smb: pointer to smb347 charger instance
276 * 250 *
277 * Returns %true if input power source is connected. Note that this is 251 * Returns %true if input power source is connected. Note that this is
278 * dependent on what platform has configured for usable power sources. For 252 * dependent on what platform has configured for usable power sources. For
279 * example if USB is disabled, this will return %false even if the USB 253 * example if USB is disabled, this will return %false even if the USB cable
280 * cable is connected. 254 * is connected.
281 */ 255 */
282static bool smb347_is_online(struct smb347_charger *smb) 256static bool smb347_is_ps_online(struct smb347_charger *smb)
283{ 257{
284 bool ret; 258 bool ret;
285 259
@@ -299,16 +273,17 @@ static bool smb347_is_online(struct smb347_charger *smb)
299 */ 273 */
300static int smb347_charging_status(struct smb347_charger *smb) 274static int smb347_charging_status(struct smb347_charger *smb)
301{ 275{
276 unsigned int val;
302 int ret; 277 int ret;
303 278
304 if (!smb347_is_online(smb)) 279 if (!smb347_is_ps_online(smb))
305 return 0; 280 return 0;
306 281
307 ret = smb347_read(smb, STAT_C); 282 ret = regmap_read(smb->regmap, STAT_C, &val);
308 if (ret < 0) 283 if (ret < 0)
309 return 0; 284 return 0;
310 285
311 return (ret & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT; 286 return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT;
312} 287}
313 288
314static int smb347_charging_set(struct smb347_charger *smb, bool enable) 289static int smb347_charging_set(struct smb347_charger *smb, bool enable)
@@ -316,27 +291,17 @@ static int smb347_charging_set(struct smb347_charger *smb, bool enable)
316 int ret = 0; 291 int ret = 0;
317 292
318 if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) { 293 if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) {
319 dev_dbg(&smb->client->dev, 294 dev_dbg(smb->dev, "charging enable/disable in SW disabled\n");
320 "charging enable/disable in SW disabled\n");
321 return 0; 295 return 0;
322 } 296 }
323 297
324 mutex_lock(&smb->lock); 298 mutex_lock(&smb->lock);
325 if (smb->charging_enabled != enable) { 299 if (smb->charging_enabled != enable) {
326 ret = smb347_read(smb, CMD_A); 300 ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED,
327 if (ret < 0) 301 enable ? CMD_A_CHG_ENABLED : 0);
328 goto out; 302 if (!ret)
329 303 smb->charging_enabled = enable;
330 smb->charging_enabled = enable;
331
332 if (enable)
333 ret |= CMD_A_CHG_ENABLED;
334 else
335 ret &= ~CMD_A_CHG_ENABLED;
336
337 ret = smb347_write(smb, CMD_A, ret);
338 } 304 }
339out:
340 mutex_unlock(&smb->lock); 305 mutex_unlock(&smb->lock);
341 return ret; 306 return ret;
342} 307}
@@ -351,7 +316,7 @@ static inline int smb347_charging_disable(struct smb347_charger *smb)
351 return smb347_charging_set(smb, false); 316 return smb347_charging_set(smb, false);
352} 317}
353 318
354static int smb347_update_online(struct smb347_charger *smb) 319static int smb347_start_stop_charging(struct smb347_charger *smb)
355{ 320{
356 int ret; 321 int ret;
357 322
@@ -360,16 +325,14 @@ static int smb347_update_online(struct smb347_charger *smb)
360 * disable or enable the charging. We do it manually because it 325 * disable or enable the charging. We do it manually because it
361 * depends on how the platform has configured the valid inputs. 326 * depends on how the platform has configured the valid inputs.
362 */ 327 */
363 if (smb347_is_online(smb)) { 328 if (smb347_is_ps_online(smb)) {
364 ret = smb347_charging_enable(smb); 329 ret = smb347_charging_enable(smb);
365 if (ret < 0) 330 if (ret < 0)
366 dev_err(&smb->client->dev, 331 dev_err(smb->dev, "failed to enable charging\n");
367 "failed to enable charging\n");
368 } else { 332 } else {
369 ret = smb347_charging_disable(smb); 333 ret = smb347_charging_disable(smb);
370 if (ret < 0) 334 if (ret < 0)
371 dev_err(&smb->client->dev, 335 dev_err(smb->dev, "failed to disable charging\n");
372 "failed to disable charging\n");
373 } 336 }
374 337
375 return ret; 338 return ret;
@@ -377,112 +340,120 @@ static int smb347_update_online(struct smb347_charger *smb)
377 340
378static int smb347_set_charge_current(struct smb347_charger *smb) 341static int smb347_set_charge_current(struct smb347_charger *smb)
379{ 342{
380 int ret, val; 343 int ret;
381
382 ret = smb347_read(smb, CFG_CHARGE_CURRENT);
383 if (ret < 0)
384 return ret;
385 344
386 if (smb->pdata->max_charge_current) { 345 if (smb->pdata->max_charge_current) {
387 val = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl), 346 ret = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl),
388 smb->pdata->max_charge_current); 347 smb->pdata->max_charge_current);
389 if (val < 0) 348 if (ret < 0)
390 return val; 349 return ret;
391 350
392 ret &= ~CFG_CHARGE_CURRENT_FCC_MASK; 351 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
393 ret |= val << CFG_CHARGE_CURRENT_FCC_SHIFT; 352 CFG_CHARGE_CURRENT_FCC_MASK,
353 ret << CFG_CHARGE_CURRENT_FCC_SHIFT);
354 if (ret < 0)
355 return ret;
394 } 356 }
395 357
396 if (smb->pdata->pre_charge_current) { 358 if (smb->pdata->pre_charge_current) {
397 val = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl), 359 ret = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl),
398 smb->pdata->pre_charge_current); 360 smb->pdata->pre_charge_current);
399 if (val < 0) 361 if (ret < 0)
400 return val; 362 return ret;
401 363
402 ret &= ~CFG_CHARGE_CURRENT_PCC_MASK; 364 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
403 ret |= val << CFG_CHARGE_CURRENT_PCC_SHIFT; 365 CFG_CHARGE_CURRENT_PCC_MASK,
366 ret << CFG_CHARGE_CURRENT_PCC_SHIFT);
367 if (ret < 0)
368 return ret;
404 } 369 }
405 370
406 if (smb->pdata->termination_current) { 371 if (smb->pdata->termination_current) {
407 val = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl), 372 ret = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl),
408 smb->pdata->termination_current); 373 smb->pdata->termination_current);
409 if (val < 0) 374 if (ret < 0)
410 return val; 375 return ret;
411 376
412 ret &= ~CFG_CHARGE_CURRENT_TC_MASK; 377 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
413 ret |= val; 378 CFG_CHARGE_CURRENT_TC_MASK, ret);
379 if (ret < 0)
380 return ret;
414 } 381 }
415 382
416 return smb347_write(smb, CFG_CHARGE_CURRENT, ret); 383 return 0;
417} 384}
418 385
419static int smb347_set_current_limits(struct smb347_charger *smb) 386static int smb347_set_current_limits(struct smb347_charger *smb)
420{ 387{
421 int ret, val; 388 int ret;
422
423 ret = smb347_read(smb, CFG_CURRENT_LIMIT);
424 if (ret < 0)
425 return ret;
426 389
427 if (smb->pdata->mains_current_limit) { 390 if (smb->pdata->mains_current_limit) {
428 val = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl), 391 ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
429 smb->pdata->mains_current_limit); 392 smb->pdata->mains_current_limit);
430 if (val < 0) 393 if (ret < 0)
431 return val; 394 return ret;
432 395
433 ret &= ~CFG_CURRENT_LIMIT_DC_MASK; 396 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
434 ret |= val << CFG_CURRENT_LIMIT_DC_SHIFT; 397 CFG_CURRENT_LIMIT_DC_MASK,
398 ret << CFG_CURRENT_LIMIT_DC_SHIFT);
399 if (ret < 0)
400 return ret;
435 } 401 }
436 402
437 if (smb->pdata->usb_hc_current_limit) { 403 if (smb->pdata->usb_hc_current_limit) {
438 val = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl), 404 ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
439 smb->pdata->usb_hc_current_limit); 405 smb->pdata->usb_hc_current_limit);
440 if (val < 0) 406 if (ret < 0)
441 return val; 407 return ret;
442 408
443 ret &= ~CFG_CURRENT_LIMIT_USB_MASK; 409 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
444 ret |= val; 410 CFG_CURRENT_LIMIT_USB_MASK, ret);
411 if (ret < 0)
412 return ret;
445 } 413 }
446 414
447 return smb347_write(smb, CFG_CURRENT_LIMIT, ret); 415 return 0;
448} 416}
449 417
450static int smb347_set_voltage_limits(struct smb347_charger *smb) 418static int smb347_set_voltage_limits(struct smb347_charger *smb)
451{ 419{
452 int ret, val; 420 int ret;
453
454 ret = smb347_read(smb, CFG_FLOAT_VOLTAGE);
455 if (ret < 0)
456 return ret;
457 421
458 if (smb->pdata->pre_to_fast_voltage) { 422 if (smb->pdata->pre_to_fast_voltage) {
459 val = smb->pdata->pre_to_fast_voltage; 423 ret = smb->pdata->pre_to_fast_voltage;
460 424
461 /* uV */ 425 /* uV */
462 val = clamp_val(val, 2400000, 3000000) - 2400000; 426 ret = clamp_val(ret, 2400000, 3000000) - 2400000;
463 val /= 200000; 427 ret /= 200000;
464 428
465 ret &= ~CFG_FLOAT_VOLTAGE_THRESHOLD_MASK; 429 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
466 ret |= val << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT; 430 CFG_FLOAT_VOLTAGE_THRESHOLD_MASK,
431 ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT);
432 if (ret < 0)
433 return ret;
467 } 434 }
468 435
469 if (smb->pdata->max_charge_voltage) { 436 if (smb->pdata->max_charge_voltage) {
470 val = smb->pdata->max_charge_voltage; 437 ret = smb->pdata->max_charge_voltage;
471 438
472 /* uV */ 439 /* uV */
473 val = clamp_val(val, 3500000, 4500000) - 3500000; 440 ret = clamp_val(ret, 3500000, 4500000) - 3500000;
474 val /= 20000; 441 ret /= 20000;
475 442
476 ret |= val; 443 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
444 CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret);
445 if (ret < 0)
446 return ret;
477 } 447 }
478 448
479 return smb347_write(smb, CFG_FLOAT_VOLTAGE, ret); 449 return 0;
480} 450}
481 451
482static int smb347_set_temp_limits(struct smb347_charger *smb) 452static int smb347_set_temp_limits(struct smb347_charger *smb)
483{ 453{
484 bool enable_therm_monitor = false; 454 bool enable_therm_monitor = false;
485 int ret, val; 455 int ret = 0;
456 int val;
486 457
487 if (smb->pdata->chip_temp_threshold) { 458 if (smb->pdata->chip_temp_threshold) {
488 val = smb->pdata->chip_temp_threshold; 459 val = smb->pdata->chip_temp_threshold;
@@ -491,22 +462,13 @@ static int smb347_set_temp_limits(struct smb347_charger *smb)
491 val = clamp_val(val, 100, 130) - 100; 462 val = clamp_val(val, 100, 130) - 100;
492 val /= 10; 463 val /= 10;
493 464
494 ret = smb347_read(smb, CFG_OTG); 465 ret = regmap_update_bits(smb->regmap, CFG_OTG,
495 if (ret < 0) 466 CFG_OTG_TEMP_THRESHOLD_MASK,
496 return ret; 467 val << CFG_OTG_TEMP_THRESHOLD_SHIFT);
497
498 ret &= ~CFG_OTG_TEMP_THRESHOLD_MASK;
499 ret |= val << CFG_OTG_TEMP_THRESHOLD_SHIFT;
500
501 ret = smb347_write(smb, CFG_OTG, ret);
502 if (ret < 0) 468 if (ret < 0)
503 return ret; 469 return ret;
504 } 470 }
505 471
506 ret = smb347_read(smb, CFG_TEMP_LIMIT);
507 if (ret < 0)
508 return ret;
509
510 if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) { 472 if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
511 val = smb->pdata->soft_cold_temp_limit; 473 val = smb->pdata->soft_cold_temp_limit;
512 474
@@ -515,8 +477,11 @@ static int smb347_set_temp_limits(struct smb347_charger *smb)
515 /* this goes from higher to lower so invert the value */ 477 /* this goes from higher to lower so invert the value */
516 val = ~val & 0x3; 478 val = ~val & 0x3;
517 479
518 ret &= ~CFG_TEMP_LIMIT_SOFT_COLD_MASK; 480 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
519 ret |= val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT; 481 CFG_TEMP_LIMIT_SOFT_COLD_MASK,
482 val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT);
483 if (ret < 0)
484 return ret;
520 485
521 enable_therm_monitor = true; 486 enable_therm_monitor = true;
522 } 487 }
@@ -527,8 +492,11 @@ static int smb347_set_temp_limits(struct smb347_charger *smb)
527 val = clamp_val(val, 40, 55) - 40; 492 val = clamp_val(val, 40, 55) - 40;
528 val /= 5; 493 val /= 5;
529 494
530 ret &= ~CFG_TEMP_LIMIT_SOFT_HOT_MASK; 495 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
531 ret |= val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT; 496 CFG_TEMP_LIMIT_SOFT_HOT_MASK,
497 val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT);
498 if (ret < 0)
499 return ret;
532 500
533 enable_therm_monitor = true; 501 enable_therm_monitor = true;
534 } 502 }
@@ -541,8 +509,11 @@ static int smb347_set_temp_limits(struct smb347_charger *smb)
541 /* this goes from higher to lower so invert the value */ 509 /* this goes from higher to lower so invert the value */
542 val = ~val & 0x3; 510 val = ~val & 0x3;
543 511
544 ret &= ~CFG_TEMP_LIMIT_HARD_COLD_MASK; 512 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
545 ret |= val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT; 513 CFG_TEMP_LIMIT_HARD_COLD_MASK,
514 val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT);
515 if (ret < 0)
516 return ret;
546 517
547 enable_therm_monitor = true; 518 enable_therm_monitor = true;
548 } 519 }
@@ -553,16 +524,15 @@ static int smb347_set_temp_limits(struct smb347_charger *smb)
553 val = clamp_val(val, 50, 65) - 50; 524 val = clamp_val(val, 50, 65) - 50;
554 val /= 5; 525 val /= 5;
555 526
556 ret &= ~CFG_TEMP_LIMIT_HARD_HOT_MASK; 527 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
557 ret |= val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT; 528 CFG_TEMP_LIMIT_HARD_HOT_MASK,
529 val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT);
530 if (ret < 0)
531 return ret;
558 532
559 enable_therm_monitor = true; 533 enable_therm_monitor = true;
560 } 534 }
561 535
562 ret = smb347_write(smb, CFG_TEMP_LIMIT, ret);
563 if (ret < 0)
564 return ret;
565
566 /* 536 /*
567 * If any of the temperature limits are set, we also enable the 537 * If any of the temperature limits are set, we also enable the
568 * thermistor monitoring. 538 * thermistor monitoring.
@@ -574,25 +544,15 @@ static int smb347_set_temp_limits(struct smb347_charger *smb)
574 * depending on the configuration. 544 * depending on the configuration.
575 */ 545 */
576 if (enable_therm_monitor) { 546 if (enable_therm_monitor) {
577 ret = smb347_read(smb, CFG_THERM); 547 ret = regmap_update_bits(smb->regmap, CFG_THERM,
578 if (ret < 0) 548 CFG_THERM_MONITOR_DISABLED, 0);
579 return ret;
580
581 ret &= ~CFG_THERM_MONITOR_DISABLED;
582
583 ret = smb347_write(smb, CFG_THERM, ret);
584 if (ret < 0) 549 if (ret < 0)
585 return ret; 550 return ret;
586 } 551 }
587 552
588 if (smb->pdata->suspend_on_hard_temp_limit) { 553 if (smb->pdata->suspend_on_hard_temp_limit) {
589 ret = smb347_read(smb, CFG_SYSOK); 554 ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
590 if (ret < 0) 555 CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0);
591 return ret;
592
593 ret &= ~CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED;
594
595 ret = smb347_write(smb, CFG_SYSOK, ret);
596 if (ret < 0) 556 if (ret < 0)
597 return ret; 557 return ret;
598 } 558 }
@@ -601,17 +561,15 @@ static int smb347_set_temp_limits(struct smb347_charger *smb)
601 SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) { 561 SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) {
602 val = smb->pdata->soft_temp_limit_compensation & 0x3; 562 val = smb->pdata->soft_temp_limit_compensation & 0x3;
603 563
604 ret = smb347_read(smb, CFG_THERM); 564 ret = regmap_update_bits(smb->regmap, CFG_THERM,
565 CFG_THERM_SOFT_HOT_COMPENSATION_MASK,
566 val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT);
605 if (ret < 0) 567 if (ret < 0)
606 return ret; 568 return ret;
607 569
608 ret &= ~CFG_THERM_SOFT_HOT_COMPENSATION_MASK; 570 ret = regmap_update_bits(smb->regmap, CFG_THERM,
609 ret |= val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT; 571 CFG_THERM_SOFT_COLD_COMPENSATION_MASK,
610 572 val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT);
611 ret &= ~CFG_THERM_SOFT_COLD_COMPENSATION_MASK;
612 ret |= val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT;
613
614 ret = smb347_write(smb, CFG_THERM, ret);
615 if (ret < 0) 573 if (ret < 0)
616 return ret; 574 return ret;
617 } 575 }
@@ -622,14 +580,9 @@ static int smb347_set_temp_limits(struct smb347_charger *smb)
622 if (val < 0) 580 if (val < 0)
623 return val; 581 return val;
624 582
625 ret = smb347_read(smb, CFG_OTG); 583 ret = regmap_update_bits(smb->regmap, CFG_OTG,
626 if (ret < 0) 584 CFG_OTG_CC_COMPENSATION_MASK,
627 return ret; 585 (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT);
628
629 ret &= ~CFG_OTG_CC_COMPENSATION_MASK;
630 ret |= (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT;
631
632 ret = smb347_write(smb, CFG_OTG, ret);
633 if (ret < 0) 586 if (ret < 0)
634 return ret; 587 return ret;
635 } 588 }
@@ -648,22 +601,13 @@ static int smb347_set_temp_limits(struct smb347_charger *smb)
648 */ 601 */
649static int smb347_set_writable(struct smb347_charger *smb, bool writable) 602static int smb347_set_writable(struct smb347_charger *smb, bool writable)
650{ 603{
651 int ret; 604 return regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE,
652 605 writable ? CMD_A_ALLOW_WRITE : 0);
653 ret = smb347_read(smb, CMD_A);
654 if (ret < 0)
655 return ret;
656
657 if (writable)
658 ret |= CMD_A_ALLOW_WRITE;
659 else
660 ret &= ~CMD_A_ALLOW_WRITE;
661
662 return smb347_write(smb, CMD_A, ret);
663} 606}
664 607
665static int smb347_hw_init(struct smb347_charger *smb) 608static int smb347_hw_init(struct smb347_charger *smb)
666{ 609{
610 unsigned int val;
667 int ret; 611 int ret;
668 612
669 ret = smb347_set_writable(smb, true); 613 ret = smb347_set_writable(smb, true);
@@ -692,34 +636,19 @@ static int smb347_hw_init(struct smb347_charger *smb)
692 636
693 /* If USB charging is disabled we put the USB in suspend mode */ 637 /* If USB charging is disabled we put the USB in suspend mode */
694 if (!smb->pdata->use_usb) { 638 if (!smb->pdata->use_usb) {
695 ret = smb347_read(smb, CMD_A); 639 ret = regmap_update_bits(smb->regmap, CMD_A,
696 if (ret < 0) 640 CMD_A_SUSPEND_ENABLED,
697 goto fail; 641 CMD_A_SUSPEND_ENABLED);
698
699 ret |= CMD_A_SUSPEND_ENABLED;
700
701 ret = smb347_write(smb, CMD_A, ret);
702 if (ret < 0) 642 if (ret < 0)
703 goto fail; 643 goto fail;
704 } 644 }
705 645
706 ret = smb347_read(smb, CFG_OTHER);
707 if (ret < 0)
708 goto fail;
709
710 /* 646 /*
711 * If configured by platform data, we enable hardware Auto-OTG 647 * If configured by platform data, we enable hardware Auto-OTG
712 * support for driving VBUS. Otherwise we disable it. 648 * support for driving VBUS. Otherwise we disable it.
713 */ 649 */
714 ret &= ~CFG_OTHER_RID_MASK; 650 ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK,
715 if (smb->pdata->use_usb_otg) 651 smb->pdata->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0);
716 ret |= CFG_OTHER_RID_ENABLED_AUTO_OTG;
717
718 ret = smb347_write(smb, CFG_OTHER, ret);
719 if (ret < 0)
720 goto fail;
721
722 ret = smb347_read(smb, CFG_PIN);
723 if (ret < 0) 652 if (ret < 0)
724 goto fail; 653 goto fail;
725 654
@@ -728,32 +657,33 @@ static int smb347_hw_init(struct smb347_charger *smb)
728 * command register unless pin control is specified in the platform 657 * command register unless pin control is specified in the platform
729 * data. 658 * data.
730 */ 659 */
731 ret &= ~CFG_PIN_EN_CTRL_MASK;
732
733 switch (smb->pdata->enable_control) { 660 switch (smb->pdata->enable_control) {
734 case SMB347_CHG_ENABLE_SW:
735 /* Do nothing, 0 means i2c control */
736 break;
737 case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW: 661 case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW:
738 ret |= CFG_PIN_EN_CTRL_ACTIVE_LOW; 662 val = CFG_PIN_EN_CTRL_ACTIVE_LOW;
739 break; 663 break;
740 case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH: 664 case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH:
741 ret |= CFG_PIN_EN_CTRL_ACTIVE_HIGH; 665 val = CFG_PIN_EN_CTRL_ACTIVE_HIGH;
666 break;
667 default:
668 val = 0;
742 break; 669 break;
743 } 670 }
744 671
745 /* Disable Automatic Power Source Detection (APSD) interrupt. */ 672 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK,
746 ret &= ~CFG_PIN_EN_APSD_IRQ; 673 val);
674 if (ret < 0)
675 goto fail;
747 676
748 ret = smb347_write(smb, CFG_PIN, ret); 677 /* Disable Automatic Power Source Detection (APSD) interrupt. */
678 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0);
749 if (ret < 0) 679 if (ret < 0)
750 goto fail; 680 goto fail;
751 681
752 ret = smb347_update_status(smb); 682 ret = smb347_update_ps_status(smb);
753 if (ret < 0) 683 if (ret < 0)
754 goto fail; 684 goto fail;
755 685
756 ret = smb347_update_online(smb); 686 ret = smb347_start_stop_charging(smb);
757 687
758fail: 688fail:
759 smb347_set_writable(smb, false); 689 smb347_set_writable(smb, false);
@@ -763,24 +693,25 @@ fail:
763static irqreturn_t smb347_interrupt(int irq, void *data) 693static irqreturn_t smb347_interrupt(int irq, void *data)
764{ 694{
765 struct smb347_charger *smb = data; 695 struct smb347_charger *smb = data;
766 int stat_c, irqstat_e, irqstat_c; 696 unsigned int stat_c, irqstat_e, irqstat_c;
767 irqreturn_t ret = IRQ_NONE; 697 bool handled = false;
698 int ret;
768 699
769 stat_c = smb347_read(smb, STAT_C); 700 ret = regmap_read(smb->regmap, STAT_C, &stat_c);
770 if (stat_c < 0) { 701 if (ret < 0) {
771 dev_warn(&smb->client->dev, "reading STAT_C failed\n"); 702 dev_warn(smb->dev, "reading STAT_C failed\n");
772 return IRQ_NONE; 703 return IRQ_NONE;
773 } 704 }
774 705
775 irqstat_c = smb347_read(smb, IRQSTAT_C); 706 ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c);
776 if (irqstat_c < 0) { 707 if (ret < 0) {
777 dev_warn(&smb->client->dev, "reading IRQSTAT_C failed\n"); 708 dev_warn(smb->dev, "reading IRQSTAT_C failed\n");
778 return IRQ_NONE; 709 return IRQ_NONE;
779 } 710 }
780 711
781 irqstat_e = smb347_read(smb, IRQSTAT_E); 712 ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e);
782 if (irqstat_e < 0) { 713 if (ret < 0) {
783 dev_warn(&smb->client->dev, "reading IRQSTAT_E failed\n"); 714 dev_warn(smb->dev, "reading IRQSTAT_E failed\n");
784 return IRQ_NONE; 715 return IRQ_NONE;
785 } 716 }
786 717
@@ -789,13 +720,11 @@ static irqreturn_t smb347_interrupt(int irq, void *data)
789 * disable charging. 720 * disable charging.
790 */ 721 */
791 if (stat_c & STAT_C_CHARGER_ERROR) { 722 if (stat_c & STAT_C_CHARGER_ERROR) {
792 dev_err(&smb->client->dev, 723 dev_err(smb->dev, "error in charger, disabling charging\n");
793 "error in charger, disabling charging\n");
794 724
795 smb347_charging_disable(smb); 725 smb347_charging_disable(smb);
796 power_supply_changed(&smb->battery); 726 power_supply_changed(&smb->battery);
797 727 handled = true;
798 ret = IRQ_HANDLED;
799 } 728 }
800 729
801 /* 730 /*
@@ -806,7 +735,7 @@ static irqreturn_t smb347_interrupt(int irq, void *data)
806 if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) { 735 if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) {
807 if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) 736 if (irqstat_c & IRQSTAT_C_TERMINATION_STAT)
808 power_supply_changed(&smb->battery); 737 power_supply_changed(&smb->battery);
809 ret = IRQ_HANDLED; 738 handled = true;
810 } 739 }
811 740
812 /* 741 /*
@@ -814,15 +743,17 @@ static irqreturn_t smb347_interrupt(int irq, void *data)
814 * was connected or disconnected. 743 * was connected or disconnected.
815 */ 744 */
816 if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) { 745 if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) {
817 if (smb347_update_status(smb) > 0) { 746 if (smb347_update_ps_status(smb) > 0) {
818 smb347_update_online(smb); 747 smb347_start_stop_charging(smb);
819 power_supply_changed(&smb->mains); 748 if (smb->pdata->use_mains)
820 power_supply_changed(&smb->usb); 749 power_supply_changed(&smb->mains);
750 if (smb->pdata->use_usb)
751 power_supply_changed(&smb->usb);
821 } 752 }
822 ret = IRQ_HANDLED; 753 handled = true;
823 } 754 }
824 755
825 return ret; 756 return handled ? IRQ_HANDLED : IRQ_NONE;
826} 757}
827 758
828static int smb347_irq_set(struct smb347_charger *smb, bool enable) 759static int smb347_irq_set(struct smb347_charger *smb, bool enable)
@@ -839,41 +770,18 @@ static int smb347_irq_set(struct smb347_charger *smb, bool enable)
839 * - termination current reached 770 * - termination current reached
840 * - charger error 771 * - charger error
841 */ 772 */
842 if (enable) { 773 ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff,
843 ret = smb347_write(smb, CFG_FAULT_IRQ, CFG_FAULT_IRQ_DCIN_UV); 774 enable ? CFG_FAULT_IRQ_DCIN_UV : 0);
844 if (ret < 0) 775 if (ret < 0)
845 goto fail; 776 goto fail;
846
847 ret = smb347_write(smb, CFG_STATUS_IRQ,
848 CFG_STATUS_IRQ_TERMINATION_OR_TAPER);
849 if (ret < 0)
850 goto fail;
851
852 ret = smb347_read(smb, CFG_PIN);
853 if (ret < 0)
854 goto fail;
855
856 ret |= CFG_PIN_EN_CHARGER_ERROR;
857
858 ret = smb347_write(smb, CFG_PIN, ret);
859 } else {
860 ret = smb347_write(smb, CFG_FAULT_IRQ, 0);
861 if (ret < 0)
862 goto fail;
863
864 ret = smb347_write(smb, CFG_STATUS_IRQ, 0);
865 if (ret < 0)
866 goto fail;
867
868 ret = smb347_read(smb, CFG_PIN);
869 if (ret < 0)
870 goto fail;
871
872 ret &= ~CFG_PIN_EN_CHARGER_ERROR;
873 777
874 ret = smb347_write(smb, CFG_PIN, ret); 778 ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff,
875 } 779 enable ? CFG_STATUS_IRQ_TERMINATION_OR_TAPER : 0);
780 if (ret < 0)
781 goto fail;
876 782
783 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR,
784 enable ? CFG_PIN_EN_CHARGER_ERROR : 0);
877fail: 785fail:
878 smb347_set_writable(smb, false); 786 smb347_set_writable(smb, false);
879 return ret; 787 return ret;
@@ -889,18 +797,18 @@ static inline int smb347_irq_disable(struct smb347_charger *smb)
889 return smb347_irq_set(smb, false); 797 return smb347_irq_set(smb, false);
890} 798}
891 799
892static int smb347_irq_init(struct smb347_charger *smb) 800static int smb347_irq_init(struct smb347_charger *smb,
801 struct i2c_client *client)
893{ 802{
894 const struct smb347_charger_platform_data *pdata = smb->pdata; 803 const struct smb347_charger_platform_data *pdata = smb->pdata;
895 int ret, irq = gpio_to_irq(pdata->irq_gpio); 804 int ret, irq = gpio_to_irq(pdata->irq_gpio);
896 805
897 ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, smb->client->name); 806 ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name);
898 if (ret < 0) 807 if (ret < 0)
899 goto fail; 808 goto fail;
900 809
901 ret = request_threaded_irq(irq, NULL, smb347_interrupt, 810 ret = request_threaded_irq(irq, NULL, smb347_interrupt,
902 IRQF_TRIGGER_FALLING, smb->client->name, 811 IRQF_TRIGGER_FALLING, client->name, smb);
903 smb);
904 if (ret < 0) 812 if (ret < 0)
905 goto fail_gpio; 813 goto fail_gpio;
906 814
@@ -912,23 +820,14 @@ static int smb347_irq_init(struct smb347_charger *smb)
912 * Configure the STAT output to be suitable for interrupts: disable 820 * Configure the STAT output to be suitable for interrupts: disable
913 * all other output (except interrupts) and make it active low. 821 * all other output (except interrupts) and make it active low.
914 */ 822 */
915 ret = smb347_read(smb, CFG_STAT); 823 ret = regmap_update_bits(smb->regmap, CFG_STAT,
916 if (ret < 0) 824 CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED,
917 goto fail_readonly; 825 CFG_STAT_DISABLED);
918
919 ret &= ~CFG_STAT_ACTIVE_HIGH;
920 ret |= CFG_STAT_DISABLED;
921
922 ret = smb347_write(smb, CFG_STAT, ret);
923 if (ret < 0)
924 goto fail_readonly;
925
926 ret = smb347_irq_enable(smb);
927 if (ret < 0) 826 if (ret < 0)
928 goto fail_readonly; 827 goto fail_readonly;
929 828
930 smb347_set_writable(smb, false); 829 smb347_set_writable(smb, false);
931 smb->client->irq = irq; 830 client->irq = irq;
932 return 0; 831 return 0;
933 832
934fail_readonly: 833fail_readonly:
@@ -938,7 +837,7 @@ fail_irq:
938fail_gpio: 837fail_gpio:
939 gpio_free(pdata->irq_gpio); 838 gpio_free(pdata->irq_gpio);
940fail: 839fail:
941 smb->client->irq = 0; 840 client->irq = 0;
942 return ret; 841 return ret;
943} 842}
944 843
@@ -987,13 +886,13 @@ static int smb347_battery_get_property(struct power_supply *psy,
987 const struct smb347_charger_platform_data *pdata = smb->pdata; 886 const struct smb347_charger_platform_data *pdata = smb->pdata;
988 int ret; 887 int ret;
989 888
990 ret = smb347_update_status(smb); 889 ret = smb347_update_ps_status(smb);
991 if (ret < 0) 890 if (ret < 0)
992 return ret; 891 return ret;
993 892
994 switch (prop) { 893 switch (prop) {
995 case POWER_SUPPLY_PROP_STATUS: 894 case POWER_SUPPLY_PROP_STATUS:
996 if (!smb347_is_online(smb)) { 895 if (!smb347_is_ps_online(smb)) {
997 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 896 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
998 break; 897 break;
999 } 898 }
@@ -1004,7 +903,7 @@ static int smb347_battery_get_property(struct power_supply *psy,
1004 break; 903 break;
1005 904
1006 case POWER_SUPPLY_PROP_CHARGE_TYPE: 905 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1007 if (!smb347_is_online(smb)) 906 if (!smb347_is_ps_online(smb))
1008 return -ENODATA; 907 return -ENODATA;
1009 908
1010 /* 909 /*
@@ -1036,44 +935,6 @@ static int smb347_battery_get_property(struct power_supply *psy,
1036 val->intval = pdata->battery_info.voltage_max_design; 935 val->intval = pdata->battery_info.voltage_max_design;
1037 break; 936 break;
1038 937
1039 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1040 if (!smb347_is_online(smb))
1041 return -ENODATA;
1042 ret = smb347_read(smb, STAT_A);
1043 if (ret < 0)
1044 return ret;
1045
1046 ret &= STAT_A_FLOAT_VOLTAGE_MASK;
1047 if (ret > 0x3d)
1048 ret = 0x3d;
1049
1050 val->intval = 3500000 + ret * 20000;
1051 break;
1052
1053 case POWER_SUPPLY_PROP_CURRENT_NOW:
1054 if (!smb347_is_online(smb))
1055 return -ENODATA;
1056
1057 ret = smb347_read(smb, STAT_B);
1058 if (ret < 0)
1059 return ret;
1060
1061 /*
1062 * The current value is composition of FCC and PCC values
1063 * and we can detect which table to use from bit 5.
1064 */
1065 if (ret & 0x20) {
1066 val->intval = hw_to_current(fcc_tbl,
1067 ARRAY_SIZE(fcc_tbl),
1068 ret & 7);
1069 } else {
1070 ret >>= 3;
1071 val->intval = hw_to_current(pcc_tbl,
1072 ARRAY_SIZE(pcc_tbl),
1073 ret & 7);
1074 }
1075 break;
1076
1077 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 938 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
1078 val->intval = pdata->battery_info.charge_full_design; 939 val->intval = pdata->battery_info.charge_full_design;
1079 break; 940 break;
@@ -1095,64 +956,58 @@ static enum power_supply_property smb347_battery_properties[] = {
1095 POWER_SUPPLY_PROP_TECHNOLOGY, 956 POWER_SUPPLY_PROP_TECHNOLOGY,
1096 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 957 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
1097 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 958 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
1098 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1099 POWER_SUPPLY_PROP_CURRENT_NOW,
1100 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 959 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
1101 POWER_SUPPLY_PROP_MODEL_NAME, 960 POWER_SUPPLY_PROP_MODEL_NAME,
1102}; 961};
1103 962
1104static int smb347_debugfs_show(struct seq_file *s, void *data) 963static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
1105{ 964{
1106 struct smb347_charger *smb = s->private; 965 switch (reg) {
1107 int ret; 966 case IRQSTAT_A:
1108 u8 reg; 967 case IRQSTAT_C:
1109 968 case IRQSTAT_E:
1110 seq_printf(s, "Control registers:\n"); 969 case IRQSTAT_F:
1111 seq_printf(s, "==================\n"); 970 case STAT_A:
1112 for (reg = CFG_CHARGE_CURRENT; reg <= CFG_ADDRESS; reg++) { 971 case STAT_B:
1113 ret = smb347_read(smb, reg); 972 case STAT_C:
1114 seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret); 973 case STAT_E:
1115 } 974 return true;
1116 seq_printf(s, "\n");
1117
1118 seq_printf(s, "Command registers:\n");
1119 seq_printf(s, "==================\n");
1120 ret = smb347_read(smb, CMD_A);
1121 seq_printf(s, "0x%02x:\t0x%02x\n", CMD_A, ret);
1122 ret = smb347_read(smb, CMD_B);
1123 seq_printf(s, "0x%02x:\t0x%02x\n", CMD_B, ret);
1124 ret = smb347_read(smb, CMD_C);
1125 seq_printf(s, "0x%02x:\t0x%02x\n", CMD_C, ret);
1126 seq_printf(s, "\n");
1127
1128 seq_printf(s, "Interrupt status registers:\n");
1129 seq_printf(s, "===========================\n");
1130 for (reg = IRQSTAT_A; reg <= IRQSTAT_F; reg++) {
1131 ret = smb347_read(smb, reg);
1132 seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret);
1133 }
1134 seq_printf(s, "\n");
1135
1136 seq_printf(s, "Status registers:\n");
1137 seq_printf(s, "=================\n");
1138 for (reg = STAT_A; reg <= STAT_E; reg++) {
1139 ret = smb347_read(smb, reg);
1140 seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret);
1141 } 975 }
1142 976
1143 return 0; 977 return false;
1144} 978}
1145 979
1146static int smb347_debugfs_open(struct inode *inode, struct file *file) 980static bool smb347_readable_reg(struct device *dev, unsigned int reg)
1147{ 981{
1148 return single_open(file, smb347_debugfs_show, inode->i_private); 982 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);
1149} 1003}
1150 1004
1151static const struct file_operations smb347_debugfs_fops = { 1005static const struct regmap_config smb347_regmap = {
1152 .open = smb347_debugfs_open, 1006 .reg_bits = 8,
1153 .read = seq_read, 1007 .val_bits = 8,
1154 .llseek = seq_lseek, 1008 .max_register = SMB347_MAX_REGISTER,
1155 .release = single_release, 1009 .volatile_reg = smb347_volatile_reg,
1010 .readable_reg = smb347_readable_reg,
1156}; 1011};
1157 1012
1158static int smb347_probe(struct i2c_client *client, 1013static int smb347_probe(struct i2c_client *client,
@@ -1178,28 +1033,45 @@ static int smb347_probe(struct i2c_client *client,
1178 i2c_set_clientdata(client, smb); 1033 i2c_set_clientdata(client, smb);
1179 1034
1180 mutex_init(&smb->lock); 1035 mutex_init(&smb->lock);
1181 smb->client = client; 1036 smb->dev = &client->dev;
1182 smb->pdata = pdata; 1037 smb->pdata = pdata;
1183 1038
1039 smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
1040 if (IS_ERR(smb->regmap))
1041 return PTR_ERR(smb->regmap);
1042
1184 ret = smb347_hw_init(smb); 1043 ret = smb347_hw_init(smb);
1185 if (ret < 0) 1044 if (ret < 0)
1186 return ret; 1045 return ret;
1187 1046
1188 smb->mains.name = "smb347-mains"; 1047 if (smb->pdata->use_mains) {
1189 smb->mains.type = POWER_SUPPLY_TYPE_MAINS; 1048 smb->mains.name = "smb347-mains";
1190 smb->mains.get_property = smb347_mains_get_property; 1049 smb->mains.type = POWER_SUPPLY_TYPE_MAINS;
1191 smb->mains.properties = smb347_mains_properties; 1050 smb->mains.get_property = smb347_mains_get_property;
1192 smb->mains.num_properties = ARRAY_SIZE(smb347_mains_properties); 1051 smb->mains.properties = smb347_mains_properties;
1193 smb->mains.supplied_to = battery; 1052 smb->mains.num_properties = ARRAY_SIZE(smb347_mains_properties);
1194 smb->mains.num_supplicants = ARRAY_SIZE(battery); 1053 smb->mains.supplied_to = battery;
1195 1054 smb->mains.num_supplicants = ARRAY_SIZE(battery);
1196 smb->usb.name = "smb347-usb"; 1055 ret = power_supply_register(dev, &smb->mains);
1197 smb->usb.type = POWER_SUPPLY_TYPE_USB; 1056 if (ret < 0)
1198 smb->usb.get_property = smb347_usb_get_property; 1057 return ret;
1199 smb->usb.properties = smb347_usb_properties; 1058 }
1200 smb->usb.num_properties = ARRAY_SIZE(smb347_usb_properties); 1059
1201 smb->usb.supplied_to = battery; 1060 if (smb->pdata->use_usb) {
1202 smb->usb.num_supplicants = ARRAY_SIZE(battery); 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 }
1203 1075
1204 smb->battery.name = "smb347-battery"; 1076 smb->battery.name = "smb347-battery";
1205 smb->battery.type = POWER_SUPPLY_TYPE_BATTERY; 1077 smb->battery.type = POWER_SUPPLY_TYPE_BATTERY;
@@ -1207,20 +1079,13 @@ static int smb347_probe(struct i2c_client *client,
1207 smb->battery.properties = smb347_battery_properties; 1079 smb->battery.properties = smb347_battery_properties;
1208 smb->battery.num_properties = ARRAY_SIZE(smb347_battery_properties); 1080 smb->battery.num_properties = ARRAY_SIZE(smb347_battery_properties);
1209 1081
1210 ret = power_supply_register(dev, &smb->mains);
1211 if (ret < 0)
1212 return ret;
1213
1214 ret = power_supply_register(dev, &smb->usb);
1215 if (ret < 0) {
1216 power_supply_unregister(&smb->mains);
1217 return ret;
1218 }
1219 1082
1220 ret = power_supply_register(dev, &smb->battery); 1083 ret = power_supply_register(dev, &smb->battery);
1221 if (ret < 0) { 1084 if (ret < 0) {
1222 power_supply_unregister(&smb->usb); 1085 if (smb->pdata->use_usb)
1223 power_supply_unregister(&smb->mains); 1086 power_supply_unregister(&smb->usb);
1087 if (smb->pdata->use_mains)
1088 power_supply_unregister(&smb->mains);
1224 return ret; 1089 return ret;
1225 } 1090 }
1226 1091
@@ -1229,15 +1094,15 @@ static int smb347_probe(struct i2c_client *client,
1229 * interrupt support here. 1094 * interrupt support here.
1230 */ 1095 */
1231 if (pdata->irq_gpio >= 0) { 1096 if (pdata->irq_gpio >= 0) {
1232 ret = smb347_irq_init(smb); 1097 ret = smb347_irq_init(smb, client);
1233 if (ret < 0) { 1098 if (ret < 0) {
1234 dev_warn(dev, "failed to initialize IRQ: %d\n", ret); 1099 dev_warn(dev, "failed to initialize IRQ: %d\n", ret);
1235 dev_warn(dev, "disabling IRQ support\n"); 1100 dev_warn(dev, "disabling IRQ support\n");
1101 } else {
1102 smb347_irq_enable(smb);
1236 } 1103 }
1237 } 1104 }
1238 1105
1239 smb->dentry = debugfs_create_file("smb347-regs", S_IRUSR, NULL, smb,
1240 &smb347_debugfs_fops);
1241 return 0; 1106 return 0;
1242} 1107}
1243 1108
@@ -1245,9 +1110,6 @@ static int smb347_remove(struct i2c_client *client)
1245{ 1110{
1246 struct smb347_charger *smb = i2c_get_clientdata(client); 1111 struct smb347_charger *smb = i2c_get_clientdata(client);
1247 1112
1248 if (!IS_ERR_OR_NULL(smb->dentry))
1249 debugfs_remove(smb->dentry);
1250
1251 if (client->irq) { 1113 if (client->irq) {
1252 smb347_irq_disable(smb); 1114 smb347_irq_disable(smb);
1253 free_irq(client->irq, smb); 1115 free_irq(client->irq, smb);
@@ -1255,8 +1117,10 @@ static int smb347_remove(struct i2c_client *client)
1255 } 1117 }
1256 1118
1257 power_supply_unregister(&smb->battery); 1119 power_supply_unregister(&smb->battery);
1258 power_supply_unregister(&smb->usb); 1120 if (smb->pdata->use_usb)
1259 power_supply_unregister(&smb->mains); 1121 power_supply_unregister(&smb->usb);
1122 if (smb->pdata->use_mains)
1123 power_supply_unregister(&smb->mains);
1260 return 0; 1124 return 0;
1261} 1125}
1262 1126
@@ -1275,17 +1139,7 @@ static struct i2c_driver smb347_driver = {
1275 .id_table = smb347_id, 1139 .id_table = smb347_id,
1276}; 1140};
1277 1141
1278static int __init smb347_init(void) 1142module_i2c_driver(smb347_driver);
1279{
1280 return i2c_add_driver(&smb347_driver);
1281}
1282module_init(smb347_init);
1283
1284static void __exit smb347_exit(void)
1285{
1286 i2c_del_driver(&smb347_driver);
1287}
1288module_exit(smb347_exit);
1289 1143
1290MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>"); 1144MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1291MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>"); 1145MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");