Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mfd: da9150: Add support for Fuel-Gauge

Signed-off-by: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
Signed-off-by: Lee Jones <lee.jones@linaro.org>

authored by

Adam Thomson and committed by
Lee Jones
1ac710e0 1f93e4a9

+167 -8
+149 -7
drivers/mfd/da9150-core.c
··· 23 23 #include <linux/mfd/da9150/core.h> 24 24 #include <linux/mfd/da9150/registers.h> 25 25 26 + /* Raw device access, used for QIF */ 27 + static int da9150_i2c_read_device(struct i2c_client *client, u8 addr, int count, 28 + u8 *buf) 29 + { 30 + struct i2c_msg xfer; 31 + int ret; 32 + 33 + /* 34 + * Read is split into two transfers as device expects STOP/START rather 35 + * than repeated start to carry out this kind of access. 36 + */ 37 + 38 + /* Write address */ 39 + xfer.addr = client->addr; 40 + xfer.flags = 0; 41 + xfer.len = 1; 42 + xfer.buf = &addr; 43 + 44 + ret = i2c_transfer(client->adapter, &xfer, 1); 45 + if (ret != 1) { 46 + if (ret < 0) 47 + return ret; 48 + else 49 + return -EIO; 50 + } 51 + 52 + /* Read data */ 53 + xfer.addr = client->addr; 54 + xfer.flags = I2C_M_RD; 55 + xfer.len = count; 56 + xfer.buf = buf; 57 + 58 + ret = i2c_transfer(client->adapter, &xfer, 1); 59 + if (ret == 1) 60 + return 0; 61 + else if (ret < 0) 62 + return ret; 63 + else 64 + return -EIO; 65 + } 66 + 67 + static int da9150_i2c_write_device(struct i2c_client *client, u8 addr, 68 + int count, const u8 *buf) 69 + { 70 + struct i2c_msg xfer; 71 + u8 *reg_data; 72 + int ret; 73 + 74 + reg_data = kzalloc(1 + count, GFP_KERNEL); 75 + if (!reg_data) 76 + return -ENOMEM; 77 + 78 + reg_data[0] = addr; 79 + memcpy(&reg_data[1], buf, count); 80 + 81 + /* Write address & data */ 82 + xfer.addr = client->addr; 83 + xfer.flags = 0; 84 + xfer.len = 1 + count; 85 + xfer.buf = reg_data; 86 + 87 + ret = i2c_transfer(client->adapter, &xfer, 1); 88 + kfree(reg_data); 89 + if (ret == 1) 90 + return 0; 91 + else if (ret < 0) 92 + return ret; 93 + else 94 + return -EIO; 95 + } 96 + 26 97 static bool da9150_volatile_reg(struct device *dev, unsigned int reg) 27 98 { 28 99 switch (reg) { ··· 177 106 178 107 .volatile_reg = da9150_volatile_reg, 179 108 }; 109 + 110 + void da9150_read_qif(struct da9150 *da9150, u8 addr, int count, u8 *buf) 111 + { 112 + int ret; 113 + 114 + ret = da9150_i2c_read_device(da9150->core_qif, addr, count, buf); 115 + if (ret < 0) 116 + dev_err(da9150->dev, "Failed to read from QIF 0x%x: %d\n", 117 + addr, ret); 118 + } 119 + EXPORT_SYMBOL_GPL(da9150_read_qif); 120 + 121 + void da9150_write_qif(struct da9150 *da9150, u8 addr, int count, const u8 *buf) 122 + { 123 + int ret; 124 + 125 + ret = da9150_i2c_write_device(da9150->core_qif, addr, count, buf); 126 + if (ret < 0) 127 + dev_err(da9150->dev, "Failed to write to QIF 0x%x: %d\n", 128 + addr, ret); 129 + } 130 + EXPORT_SYMBOL_GPL(da9150_write_qif); 180 131 181 132 u8 da9150_reg_read(struct da9150 *da9150, u16 reg) 182 133 { ··· 390 297 }, 391 298 }; 392 299 300 + static struct resource da9150_fg_resources[] = { 301 + DEFINE_RES_IRQ_NAMED(DA9150_IRQ_FG, "FG"), 302 + }; 303 + 304 + enum da9150_dev_idx { 305 + DA9150_GPADC_IDX = 0, 306 + DA9150_CHARGER_IDX, 307 + DA9150_FG_IDX, 308 + }; 309 + 393 310 static struct mfd_cell da9150_devs[] = { 394 - { 311 + [DA9150_GPADC_IDX] = { 395 312 .name = "da9150-gpadc", 396 313 .of_compatible = "dlg,da9150-gpadc", 397 314 .resources = da9150_gpadc_resources, 398 315 .num_resources = ARRAY_SIZE(da9150_gpadc_resources), 399 316 }, 400 - { 317 + [DA9150_CHARGER_IDX] = { 401 318 .name = "da9150-charger", 402 319 .of_compatible = "dlg,da9150-charger", 403 320 .resources = da9150_charger_resources, 404 321 .num_resources = ARRAY_SIZE(da9150_charger_resources), 322 + }, 323 + [DA9150_FG_IDX] = { 324 + .name = "da9150-fuel-gauge", 325 + .of_compatible = "dlg,da9150-fuel-gauge", 326 + .resources = da9150_fg_resources, 327 + .num_resources = ARRAY_SIZE(da9150_fg_resources), 405 328 }, 406 329 }; 407 330 ··· 426 317 { 427 318 struct da9150 *da9150; 428 319 struct da9150_pdata *pdata = dev_get_platdata(&client->dev); 320 + int qif_addr; 429 321 int ret; 430 322 431 323 da9150 = devm_kzalloc(&client->dev, sizeof(*da9150), GFP_KERNEL); ··· 445 335 return ret; 446 336 } 447 337 448 - da9150->irq_base = pdata ? pdata->irq_base : -1; 338 + /* Setup secondary I2C interface for QIF access */ 339 + qif_addr = da9150_reg_read(da9150, DA9150_CORE2WIRE_CTRL_A); 340 + qif_addr = (qif_addr & DA9150_CORE_BASE_ADDR_MASK) >> 1; 341 + qif_addr |= DA9150_QIF_I2C_ADDR_LSB; 342 + da9150->core_qif = i2c_new_dummy(client->adapter, qif_addr); 343 + if (!da9150->core_qif) { 344 + dev_err(da9150->dev, "Failed to attach QIF client\n"); 345 + return -ENODEV; 346 + } 347 + 348 + i2c_set_clientdata(da9150->core_qif, da9150); 349 + 350 + if (pdata) { 351 + da9150->irq_base = pdata->irq_base; 352 + 353 + da9150_devs[DA9150_FG_IDX].platform_data = pdata->fg_pdata; 354 + da9150_devs[DA9150_FG_IDX].pdata_size = 355 + sizeof(struct da9150_fg_pdata); 356 + } else { 357 + da9150->irq_base = -1; 358 + } 449 359 450 360 ret = regmap_add_irq_chip(da9150->regmap, da9150->irq, 451 361 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 452 362 da9150->irq_base, &da9150_regmap_irq_chip, 453 363 &da9150->regmap_irq_data); 454 - if (ret) 455 - return ret; 364 + if (ret) { 365 + dev_err(da9150->dev, "Failed to add regmap irq chip: %d\n", 366 + ret); 367 + goto regmap_irq_fail; 368 + } 369 + 456 370 457 371 da9150->irq_base = regmap_irq_chip_get_base(da9150->regmap_irq_data); 372 + 458 373 enable_irq_wake(da9150->irq); 459 374 460 375 ret = mfd_add_devices(da9150->dev, -1, da9150_devs, ··· 487 352 da9150->irq_base, NULL); 488 353 if (ret) { 489 354 dev_err(da9150->dev, "Failed to add child devices: %d\n", ret); 490 - regmap_del_irq_chip(da9150->irq, da9150->regmap_irq_data); 491 - return ret; 355 + goto mfd_fail; 492 356 } 493 357 494 358 return 0; 359 + 360 + mfd_fail: 361 + regmap_del_irq_chip(da9150->irq, da9150->regmap_irq_data); 362 + regmap_irq_fail: 363 + i2c_unregister_device(da9150->core_qif); 364 + 365 + return ret; 495 366 } 496 367 497 368 static int da9150_remove(struct i2c_client *client) ··· 506 365 507 366 regmap_del_irq_chip(da9150->irq, da9150->regmap_irq_data); 508 367 mfd_remove_devices(da9150->dev); 368 + i2c_unregister_device(da9150->core_qif); 509 369 510 370 return 0; 511 371 }
+18 -1
include/linux/mfd/da9150/core.h
··· 15 15 #define __DA9150_CORE_H 16 16 17 17 #include <linux/device.h> 18 + #include <linux/i2c.h> 18 19 #include <linux/interrupt.h> 19 20 #include <linux/regmap.h> 20 21 ··· 47 46 #define DA9150_IRQ_GPADC 19 48 47 #define DA9150_IRQ_WKUP 20 49 48 49 + /* I2C sub-device address */ 50 + #define DA9150_QIF_I2C_ADDR_LSB 0x5 51 + 52 + struct da9150_fg_pdata { 53 + u32 update_interval; /* msecs */ 54 + u8 warn_soc_lvl; /* % value */ 55 + u8 crit_soc_lvl; /* % value */ 56 + }; 57 + 50 58 struct da9150_pdata { 51 59 int irq_base; 60 + struct da9150_fg_pdata *fg_pdata; 52 61 }; 53 62 54 63 struct da9150 { 55 64 struct device *dev; 56 65 struct regmap *regmap; 66 + struct i2c_client *core_qif; 67 + 57 68 struct regmap_irq_chip_data *regmap_irq_data; 58 69 int irq; 59 70 int irq_base; 60 71 }; 61 72 62 - /* Device I/O */ 73 + /* Device I/O - Query Interface for FG and standard register access */ 74 + void da9150_read_qif(struct da9150 *da9150, u8 addr, int count, u8 *buf); 75 + void da9150_write_qif(struct da9150 *da9150, u8 addr, int count, const u8 *buf); 76 + 63 77 u8 da9150_reg_read(struct da9150 *da9150, u16 reg); 64 78 void da9150_reg_write(struct da9150 *da9150, u16 reg, u8 val); 65 79 void da9150_set_bits(struct da9150 *da9150, u16 reg, u8 mask, u8 val); 66 80 67 81 void da9150_bulk_read(struct da9150 *da9150, u16 reg, int count, u8 *buf); 68 82 void da9150_bulk_write(struct da9150 *da9150, u16 reg, int count, const u8 *buf); 83 + 69 84 #endif /* __DA9150_CORE_H */