26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
38 #define STATUS_POR_BIT (1 << 1)
39 #define STATUS_BST_BIT (1 << 3)
40 #define STATUS_VMN_BIT (1 << 8)
41 #define STATUS_TMN_BIT (1 << 9)
42 #define STATUS_SMN_BIT (1 << 10)
43 #define STATUS_BI_BIT (1 << 11)
44 #define STATUS_VMX_BIT (1 << 12)
45 #define STATUS_TMX_BIT (1 << 13)
46 #define STATUS_SMX_BIT (1 << 14)
47 #define STATUS_BR_BIT (1 << 15)
50 #define CONFIG_ALRT_BIT_ENBL (1 << 2)
51 #define STATUS_INTR_SOCMIN_BIT (1 << 10)
52 #define STATUS_INTR_SOCMAX_BIT (1 << 14)
54 #define VFSOC0_LOCK 0x0000
55 #define VFSOC0_UNLOCK 0x0080
56 #define MODEL_UNLOCK1 0X0059
57 #define MODEL_UNLOCK2 0X00C4
58 #define MODEL_LOCK1 0X0000
59 #define MODEL_LOCK2 0X0000
61 #define dQ_ACC_DIV 0x4
62 #define dP_ACC_100 0x1900
63 #define dP_ACC_200 0x3200
65 #define MAX17042_IC_VERSION 0x0092
66 #define MAX17047_IC_VERSION 0x00AC
82 dev_err(&client->
dev,
"%s: err %d\n", __func__, ret);
92 dev_err(&client->
dev,
"%s: err %d\n", __func__, ret);
97 static void max17042_set_reg(
struct i2c_client *client,
102 for (i = 0; i <
size; i++)
103 max17042_write_reg(client, data[i].
addr, data[i].data);
122 static int max17042_get_property(
struct power_supply *psy,
175 val->
intval = ret * 625 / 8;
182 val->
intval = ret * 625 / 8;
189 val->
intval = ret * 625 / 8;
203 val->
intval = ret * 1000 / 2;
210 val->
intval = ret * 1000 / 2;
219 if (val->
intval & 0x8000) {
228 if (chip->
pdata->enable_current_sense) {
234 if (val->
intval & 0x8000) {
246 if (chip->
pdata->enable_current_sense) {
247 ret = max17042_read_reg(chip->
client,
253 if (val->
intval & 0x8000) {
270 static int max17042_write_verify_reg(
struct i2c_client *client,
279 read_value = max17042_read_reg(client, reg);
280 if (read_value != value) {
284 }
while (retries && read_value != value);
287 dev_err(&client->
dev,
"%s: err %d\n", __func__, ret);
292 static inline void max17042_override_por(
296 max17042_write_reg(client, reg, value);
299 static inline void max10742_unlock_model(
struct max17042_chip *chip)
306 static inline void max10742_lock_model(
struct max17042_chip *chip)
313 static inline void max17042_write_model_data(
struct max17042_chip *chip,
318 for (i = 0; i <
size; i++)
319 max17042_write_reg(client, addr + i,
320 chip->
pdata->config_data->cell_char_tbl[i]);
323 static inline void max17042_read_model_data(
struct max17042_chip *chip,
324 u8 addr,
u16 *data,
int size)
329 for (i = 0; i <
size; i++)
330 data[i] = max17042_read_reg(client, addr + i);
333 static inline int max17042_model_data_compare(
struct max17042_chip *chip,
338 if (
memcmp(data1, data2, size)) {
339 dev_err(&chip->
client->dev,
"%s compare failed\n", __func__);
340 for (i = 0; i <
size; i++)
355 temp_data = kcalloc(table_size,
sizeof(*temp_data),
GFP_KERNEL);
359 max10742_unlock_model(chip);
365 ret = max17042_model_data_compare(
367 chip->
pdata->config_data->cell_char_tbl,
371 max10742_lock_model(chip);
377 static int max17042_verify_model_lock(
struct max17042_chip *chip)
384 temp_data = kcalloc(table_size,
sizeof(*temp_data),
GFP_KERNEL);
390 for (i = 0; i < table_size; i++)
398 static void max17042_write_config_regs(
struct max17042_chip *chip)
412 static void max17042_write_custom_regs(
struct max17042_chip *chip)
439 static void max17042_update_capacity_regs(
struct max17042_chip *chip)
451 static void max17042_reset_vfsoc0_reg(
struct max17042_chip *chip)
461 static void max17042_load_new_capacity_params(
struct max17042_chip *chip)
463 u16 full_cap0, rep_cap, dq_acc, vfSoc;
475 rem_cap = ((vfSoc >> 8) * full_cap0) / 100;
478 rep_cap = (
u16)rem_cap;
501 static inline void max17042_override_por_values(
struct max17042_chip *chip)
559 max17042_override_por_values(chip);
566 max17042_write_config_regs(chip);
569 ret = max17042_init_model(chip);
575 max17042_verify_model_lock(chip);
582 max17042_write_custom_regs(chip);
585 max17042_update_capacity_regs(chip);
593 max17042_reset_vfsoc0_reg(chip);
596 max17042_load_new_capacity_params(chip);
613 soc_tr = (soc + off) << 8;
614 soc_tr |= (soc - off);
627 max17042_set_soc_threshold(chip, 1);
641 if (chip->
pdata->enable_por_init && chip->
pdata->config_data) {
642 ret = max17042_init_chip(chip);
652 max17042_get_pdata(
struct device *dev)
669 if (of_property_read_u32(np,
"maxim,rsns-microohm", &prop) == 0) {
678 max17042_get_pdata(
struct device *dev)
700 chip->
pdata = max17042_get_pdata(&client->
dev);
702 dev_err(&client->
dev,
"no platform data provided\n");
706 i2c_set_clientdata(client, chip);
710 dev_dbg(&client->
dev,
"chip type max17042 detected\n");
713 dev_dbg(&client->
dev,
"chip type max17047/50 detected\n");
716 dev_err(&client->
dev,
"device version mismatch: %x\n", ret);
720 chip->
battery.name =
"max170xx_battery";
722 chip->
battery.get_property = max17042_get_property;
723 chip->
battery.properties = max17042_battery_props;
728 if (!chip->
pdata->enable_current_sense)
729 chip->
battery.num_properties -= 2;
731 if (chip->
pdata->r_sns == 0)
734 if (chip->
pdata->init_data)
735 max17042_set_reg(client, chip->
pdata->init_data,
736 chip->
pdata->num_init_data);
738 if (!chip->
pdata->enable_current_sense) {
746 dev_err(&client->
dev,
"failed: power supply register\n");
752 max17042_thread_handler,
759 max17042_set_soc_threshold(chip, 1);
762 dev_err(&client->
dev,
"%s(): cannot get IRQ\n",
789 static int max17042_suspend(
struct device *dev)
799 enable_irq_wake(chip->
client->irq);
805 static int max17042_resume(
struct device *dev)
810 disable_irq_wake(chip->
client->irq);
813 max17042_set_soc_threshold(chip, 1);
819 static const struct dev_pm_ops max17042_pm_ops = {
821 .resume = max17042_resume,
824 #define MAX17042_PM_OPS (&max17042_pm_ops)
826 #define MAX17042_PM_OPS NULL
830 static const struct of_device_id max17042_dt_match[] = {
832 { .compatible =
"maxim,max17047" },
833 { .compatible =
"maxim,max17050" },
847 static struct i2c_driver max17042_i2c_driver = {
853 .probe = max17042_probe,
855 .id_table = max17042_id,