max17040_battery.c (f4b782af61ae7bbf93008d5809b0e3a8ac2bb88e) max17040_battery.c (814755c48f8b2c3e83b3c11535c48ab416128978)
1// SPDX-License-Identifier: GPL-2.0
2//
3// max17040_battery.c
4// fuel-gauge systems for lithium-ion (Li+) batteries
5//
6// Copyright (C) 2009 Samsung Electronics
7// Minkyu Kang <mk7.kang@samsung.com>
8

--- 4 unchanged lines hidden (view full) ---

13#include <linux/err.h>
14#include <linux/i2c.h>
15#include <linux/delay.h>
16#include <linux/interrupt.h>
17#include <linux/power_supply.h>
18#include <linux/of.h>
19#include <linux/regmap.h>
20#include <linux/slab.h>
1// SPDX-License-Identifier: GPL-2.0
2//
3// max17040_battery.c
4// fuel-gauge systems for lithium-ion (Li+) batteries
5//
6// Copyright (C) 2009 Samsung Electronics
7// Minkyu Kang <mk7.kang@samsung.com>
8

--- 4 unchanged lines hidden (view full) ---

13#include <linux/err.h>
14#include <linux/i2c.h>
15#include <linux/delay.h>
16#include <linux/interrupt.h>
17#include <linux/power_supply.h>
18#include <linux/of.h>
19#include <linux/regmap.h>
20#include <linux/slab.h>
21#include <linux/iio/consumer.h>
21
22#define MAX17040_VCELL 0x02
23#define MAX17040_SOC 0x04
24#define MAX17040_MODE 0x06
25#define MAX17040_VER 0x08
26#define MAX17040_CONFIG 0x0C
27#define MAX17040_STATUS 0x1A
28#define MAX17040_CMD 0xFE

--- 108 unchanged lines hidden (view full) ---

137};
138
139struct max17040_chip {
140 struct i2c_client *client;
141 struct regmap *regmap;
142 struct delayed_work work;
143 struct power_supply *battery;
144 struct chip_data data;
22
23#define MAX17040_VCELL 0x02
24#define MAX17040_SOC 0x04
25#define MAX17040_MODE 0x06
26#define MAX17040_VER 0x08
27#define MAX17040_CONFIG 0x0C
28#define MAX17040_STATUS 0x1A
29#define MAX17040_CMD 0xFE

--- 108 unchanged lines hidden (view full) ---

138};
139
140struct max17040_chip {
141 struct i2c_client *client;
142 struct regmap *regmap;
143 struct delayed_work work;
144 struct power_supply *battery;
145 struct chip_data data;
146 struct iio_channel *channel_temp;
145
146 /* battery capacity */
147 int soc;
148 /* Low alert threshold from 32% to 1% of the State of Charge */
149 u32 low_soc_alert;
150 /* some devices return twice the capacity */
151 bool quirk_double_soc;
152 /* higher 8 bits for 17043+, 16 bits for 17040,41 */

--- 246 unchanged lines hidden (view full) ---

399 val->intval = max17040_get_soc(chip);
400 break;
401 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
402 val->intval = chip->low_soc_alert;
403 break;
404 case POWER_SUPPLY_PROP_STATUS:
405 power_supply_get_property_from_supplier(psy, psp, val);
406 break;
147
148 /* battery capacity */
149 int soc;
150 /* Low alert threshold from 32% to 1% of the State of Charge */
151 u32 low_soc_alert;
152 /* some devices return twice the capacity */
153 bool quirk_double_soc;
154 /* higher 8 bits for 17043+, 16 bits for 17040,41 */

--- 246 unchanged lines hidden (view full) ---

401 val->intval = max17040_get_soc(chip);
402 break;
403 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
404 val->intval = chip->low_soc_alert;
405 break;
406 case POWER_SUPPLY_PROP_STATUS:
407 power_supply_get_property_from_supplier(psy, psp, val);
408 break;
409 case POWER_SUPPLY_PROP_TEMP:
410 if (!chip->channel_temp)
411 return -ENODATA;
412
413 iio_read_channel_processed_scale(chip->channel_temp,
414 &val->intval, 10);
415 break;
407 default:
408 return -EINVAL;
409 }
410 return 0;
411}
412
413static const struct regmap_config max17040_regmap = {
414 .reg_bits = 8,

--- 4 unchanged lines hidden (view full) ---

419
420static enum power_supply_property max17040_battery_props[] = {
421 POWER_SUPPLY_PROP_ONLINE,
422 POWER_SUPPLY_PROP_PRESENT,
423 POWER_SUPPLY_PROP_VOLTAGE_NOW,
424 POWER_SUPPLY_PROP_CAPACITY,
425 POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
426 POWER_SUPPLY_PROP_STATUS,
416 default:
417 return -EINVAL;
418 }
419 return 0;
420}
421
422static const struct regmap_config max17040_regmap = {
423 .reg_bits = 8,

--- 4 unchanged lines hidden (view full) ---

428
429static enum power_supply_property max17040_battery_props[] = {
430 POWER_SUPPLY_PROP_ONLINE,
431 POWER_SUPPLY_PROP_PRESENT,
432 POWER_SUPPLY_PROP_VOLTAGE_NOW,
433 POWER_SUPPLY_PROP_CAPACITY,
434 POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
435 POWER_SUPPLY_PROP_STATUS,
436 POWER_SUPPLY_PROP_TEMP,
427};
428
429static const struct power_supply_desc max17040_battery_desc = {
430 .name = "battery",
431 .type = POWER_SUPPLY_TYPE_BATTERY,
432 .get_property = max17040_get_property,
433 .set_property = max17040_set_property,
434 .property_is_writeable = max17040_prop_writeable,

--- 29 unchanged lines hidden (view full) ---

464 return ret;
465 chip_id = (uintptr_t)of_device_get_match_data(&client->dev);
466 }
467 chip->data = max17040_family[chip_id];
468
469 i2c_set_clientdata(client, chip);
470 psy_cfg.drv_data = chip;
471
437};
438
439static const struct power_supply_desc max17040_battery_desc = {
440 .name = "battery",
441 .type = POWER_SUPPLY_TYPE_BATTERY,
442 .get_property = max17040_get_property,
443 .set_property = max17040_set_property,
444 .property_is_writeable = max17040_prop_writeable,

--- 29 unchanged lines hidden (view full) ---

474 return ret;
475 chip_id = (uintptr_t)of_device_get_match_data(&client->dev);
476 }
477 chip->data = max17040_family[chip_id];
478
479 i2c_set_clientdata(client, chip);
480 psy_cfg.drv_data = chip;
481
482 /* Switch to devm_iio_channel_get_optional when available */
483 chip->channel_temp = devm_iio_channel_get(&client->dev, "temp");
484 if (IS_ERR(chip->channel_temp)) {
485 ret = PTR_ERR(chip->channel_temp);
486 if (ret != -ENODEV)
487 return dev_err_probe(&client->dev, PTR_ERR(chip->channel_temp),
488 "failed to get temp\n");
489 else
490 chip->channel_temp = NULL;
491 }
492
472 chip->battery = devm_power_supply_register(&client->dev,
473 &max17040_battery_desc, &psy_cfg);
474 if (IS_ERR(chip->battery)) {
475 dev_err(&client->dev, "failed: power supply register\n");
476 return PTR_ERR(chip->battery);
477 }
478
479 ret = max17040_get_version(chip);

--- 136 unchanged lines hidden ---
493 chip->battery = devm_power_supply_register(&client->dev,
494 &max17040_battery_desc, &psy_cfg);
495 if (IS_ERR(chip->battery)) {
496 dev_err(&client->dev, "failed: power supply register\n");
497 return PTR_ERR(chip->battery);
498 }
499
500 ret = max17040_get_version(chip);

--- 136 unchanged lines hidden ---