1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // max8998_charger.c - Power supply consumer driver for the Maxim 8998/LP3974
4 //
5 //  Copyright (C) 2009-2010 Samsung Electronics
6 //  MyungJoo Ham <myungjoo.ham@samsung.com>
7 
8 #include <linux/err.h>
9 #include <linux/module.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/slab.h>
12 #include <linux/platform_device.h>
13 #include <linux/power_supply.h>
14 #include <linux/mfd/max8998.h>
15 #include <linux/mfd/max8998-private.h>
16 
17 struct max8998_battery_data {
18 	struct device *dev;
19 	struct max8998_dev *iodev;
20 	struct power_supply *battery;
21 };
22 
23 static enum power_supply_property max8998_battery_props[] = {
24 	POWER_SUPPLY_PROP_PRESENT, /* the presence of battery */
25 	POWER_SUPPLY_PROP_ONLINE, /* charger is active or not */
26 };
27 
28 /* Note that the charger control is done by a current regulator "CHARGER" */
max8998_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)29 static int max8998_battery_get_property(struct power_supply *psy,
30 		enum power_supply_property psp,
31 		union power_supply_propval *val)
32 {
33 	struct max8998_battery_data *max8998 = power_supply_get_drvdata(psy);
34 	struct i2c_client *i2c = max8998->iodev->i2c;
35 	int ret;
36 	u8 reg;
37 
38 	switch (psp) {
39 	case POWER_SUPPLY_PROP_PRESENT:
40 		ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
41 		if (ret)
42 			return ret;
43 		if (reg & (1 << 4))
44 			val->intval = 0;
45 		else
46 			val->intval = 1;
47 		break;
48 	case POWER_SUPPLY_PROP_ONLINE:
49 		ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
50 		if (ret)
51 			return ret;
52 		if (reg & (1 << 3))
53 			val->intval = 0;
54 		else
55 			val->intval = 1;
56 		break;
57 	default:
58 		return -EINVAL;
59 	}
60 
61 	return 0;
62 }
63 
64 static const struct power_supply_desc max8998_battery_desc = {
65 	.name		= "max8998_pmic",
66 	.type		= POWER_SUPPLY_TYPE_BATTERY,
67 	.get_property	= max8998_battery_get_property,
68 	.properties	= max8998_battery_props,
69 	.num_properties	= ARRAY_SIZE(max8998_battery_props),
70 };
71 
max8998_battery_probe(struct platform_device * pdev)72 static int max8998_battery_probe(struct platform_device *pdev)
73 {
74 	struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
75 	struct max8998_platform_data *pdata = iodev->pdata;
76 	struct power_supply_config psy_cfg = {};
77 	struct max8998_battery_data *max8998;
78 	struct i2c_client *i2c;
79 	int ret = 0;
80 
81 	if (!pdata) {
82 		dev_err(pdev->dev.parent, "No platform init data supplied\n");
83 		return -ENODEV;
84 	}
85 
86 	max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_battery_data),
87 				GFP_KERNEL);
88 	if (!max8998)
89 		return -ENOMEM;
90 
91 	max8998->dev = &pdev->dev;
92 	max8998->iodev = iodev;
93 	platform_set_drvdata(pdev, max8998);
94 	i2c = max8998->iodev->i2c;
95 
96 	/* Setup "End of Charge" */
97 	/* If EOC value equals 0,
98 	 * remain value set from bootloader or default value */
99 	if (pdata->eoc >= 10 && pdata->eoc <= 45) {
100 		max8998_update_reg(i2c, MAX8998_REG_CHGR1,
101 				(pdata->eoc / 5 - 2) << 5, 0x7 << 5);
102 	} else if (pdata->eoc == 0) {
103 		dev_dbg(max8998->dev,
104 			"EOC value not set: leave it unchanged.\n");
105 	} else {
106 		dev_err(max8998->dev, "Invalid EOC value\n");
107 		return -EINVAL;
108 	}
109 
110 	/* Setup Charge Restart Level */
111 	switch (pdata->restart) {
112 	case 100:
113 		max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x1 << 3, 0x3 << 3);
114 		break;
115 	case 150:
116 		max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x0 << 3, 0x3 << 3);
117 		break;
118 	case 200:
119 		max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x2 << 3, 0x3 << 3);
120 		break;
121 	case -1:
122 		max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x3 << 3, 0x3 << 3);
123 		break;
124 	case 0:
125 		dev_dbg(max8998->dev,
126 			"Restart Level not set: leave it unchanged.\n");
127 		break;
128 	default:
129 		dev_err(max8998->dev, "Invalid Restart Level\n");
130 		return -EINVAL;
131 	}
132 
133 	/* Setup Charge Full Timeout */
134 	switch (pdata->timeout) {
135 	case 5:
136 		max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x0 << 4, 0x3 << 4);
137 		break;
138 	case 6:
139 		max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x1 << 4, 0x3 << 4);
140 		break;
141 	case 7:
142 		max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x2 << 4, 0x3 << 4);
143 		break;
144 	case -1:
145 		max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x3 << 4, 0x3 << 4);
146 		break;
147 	case 0:
148 		dev_dbg(max8998->dev,
149 			"Full Timeout not set: leave it unchanged.\n");
150 		break;
151 	default:
152 		dev_err(max8998->dev, "Invalid Full Timeout value\n");
153 		return -EINVAL;
154 	}
155 
156 	psy_cfg.drv_data = max8998;
157 
158 	max8998->battery = devm_power_supply_register(max8998->dev,
159 						      &max8998_battery_desc,
160 						      &psy_cfg);
161 	if (IS_ERR(max8998->battery)) {
162 		ret = PTR_ERR(max8998->battery);
163 		dev_err(max8998->dev, "failed: power supply register: %d\n",
164 			ret);
165 		return ret;
166 	}
167 
168 	return 0;
169 }
170 
171 static const struct platform_device_id max8998_battery_id[] = {
172 	{ "max8998-battery", TYPE_MAX8998 },
173 	{ }
174 };
175 
176 static struct platform_driver max8998_battery_driver = {
177 	.driver = {
178 		.name = "max8998-battery",
179 	},
180 	.probe = max8998_battery_probe,
181 	.id_table = max8998_battery_id,
182 };
183 
184 module_platform_driver(max8998_battery_driver);
185 
186 MODULE_DESCRIPTION("MAXIM 8998 battery control driver");
187 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
188 MODULE_LICENSE("GPL");
189 MODULE_ALIAS("platform:max8998-battery");
190