summaryrefslogtreecommitdiff
path: root/drivers/power/supply/pm8916_lbc.c
blob: 6d92e98cbecc682768bf8c6048735b4687088186 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2023, Nikita Travkin <nikita@trvn.ru>
 */

#include <linux/errno.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/property.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/extcon-provider.h>
#include <linux/mod_devicetable.h>

/* Two bytes: type + subtype */
#define PM8916_PERPH_TYPE 0x04
#define PM8916_LBC_CHGR_TYPE 0x1502
#define PM8916_LBC_BAT_IF_TYPE 0x1602
#define PM8916_LBC_USB_TYPE 0x1702
#define PM8916_LBC_MISC_TYPE 0x1802

#define PM8916_LBC_CHGR_CHG_OPTION 0x08
#define PM8916_LBC_CHGR_PMIC_CHARGER BIT(7)

#define PM8916_LBC_CHGR_CHG_STATUS 0x09

#define PM8916_INT_RT_STS 0x10

#define PM8916_LBC_USB_USBIN_VALID BIT(1)

#define PM8916_LBC_CHGR_VDD_MAX 0x40
#define PM8916_LBC_CHGR_VDD_SAFE 0x41
#define PM8916_LBC_CHGR_IBAT_MAX 0x44
#define PM8916_LBC_CHGR_IBAT_SAFE 0x45

#define PM8916_LBC_CHGR_TCHG_MAX_EN 0x60
#define PM8916_LBC_CHGR_TCHG_MAX_ENABLED BIT(7)
#define PM8916_LBC_CHGR_TCHG_MAX 0x61

#define PM8916_LBC_CHGR_CHG_CTRL 0x49
#define PM8916_LBC_CHGR_CHG_EN BIT(7)
#define PM8916_LBC_CHGR_PSTG_EN BIT(5)

#define PM8916_LBC_CHGR_MIN_CURRENT 90000
#define PM8916_LBC_CHGR_MAX_CURRENT 1440000

#define PM8916_LBC_CHGR_MIN_VOLTAGE 4000000
#define PM8916_LBC_CHGR_MAX_VOLTAGE 4775000
#define PM8916_LBC_CHGR_VOLTAGE_STEP 25000

#define PM8916_LBC_CHGR_MIN_TIME 4
#define PM8916_LBC_CHGR_MAX_TIME 256

struct pm8916_lbc_charger {
	struct device *dev;
	struct extcon_dev *edev;
	struct power_supply *charger;
	struct power_supply_battery_info *info;
	struct regmap *regmap;
	unsigned int reg[4];
	bool online;
	unsigned int charge_voltage_max;
	unsigned int charge_voltage_safe;
	unsigned int charge_current_max;
	unsigned int charge_current_safe;
};

static const unsigned int pm8916_lbc_charger_cable[] = {
	EXTCON_USB,
	EXTCON_NONE,
};

enum {
	LBC_CHGR = 0,
	LBC_BAT_IF,
	LBC_USB,
	LBC_MISC,
};

static int pm8916_lbc_charger_configure(struct pm8916_lbc_charger *chg)
{
	int ret = 0;
	unsigned int tmp;

	chg->charge_voltage_max = clamp_t(u32, chg->charge_voltage_max,
					  PM8916_LBC_CHGR_MIN_VOLTAGE, chg->charge_voltage_safe);

	tmp = chg->charge_voltage_max - PM8916_LBC_CHGR_MIN_VOLTAGE;
	tmp /= PM8916_LBC_CHGR_VOLTAGE_STEP;
	chg->charge_voltage_max = PM8916_LBC_CHGR_MIN_VOLTAGE + tmp * PM8916_LBC_CHGR_VOLTAGE_STEP;

	ret = regmap_write(chg->regmap, chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_VDD_MAX, tmp);
	if (ret)
		goto error;

	chg->charge_current_max = min(chg->charge_current_max, chg->charge_current_safe);

	tmp = clamp_t(u32, chg->charge_current_max,
		      PM8916_LBC_CHGR_MIN_CURRENT, PM8916_LBC_CHGR_MAX_CURRENT);

	tmp = chg->charge_current_max / PM8916_LBC_CHGR_MIN_CURRENT - 1;
	chg->charge_current_max = (tmp + 1) * PM8916_LBC_CHGR_MIN_CURRENT;

	ret = regmap_write(chg->regmap, chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_IBAT_MAX, tmp);
	if (ret)
		goto error;

	ret = regmap_write(chg->regmap, chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_CHG_CTRL,
			   PM8916_LBC_CHGR_CHG_EN | PM8916_LBC_CHGR_PSTG_EN);
	if (ret)
		goto error;

	return ret;

error:
	dev_err(chg->dev, "Failed to configure charging: %pe\n", ERR_PTR(ret));
	return ret;
}

static int pm8916_lbc_charger_get_property(struct power_supply *psy,
					   enum power_supply_property psp,
					   union power_supply_propval *val)
{
	struct pm8916_lbc_charger *chg = power_supply_get_drvdata(psy);

	switch (psp) {
	case POWER_SUPPLY_PROP_ONLINE:
		val->intval = chg->online;
		return 0;

	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
		val->intval = chg->charge_voltage_max;
		return 0;

	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
		val->intval = chg->charge_current_max;
		return 0;

	default:
		return -EINVAL;
	};
}

static int pm8916_lbc_charger_set_property(struct power_supply *psy,
					   enum power_supply_property prop,
					   const union power_supply_propval *val)
{
	struct pm8916_lbc_charger *chg = power_supply_get_drvdata(psy);

	switch (prop) {
	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
		chg->charge_current_max = val->intval;
		return pm8916_lbc_charger_configure(chg);
	default:
		return -EINVAL;
	}
}

static int pm8916_lbc_charger_property_is_writeable(struct power_supply *psy,
						    enum power_supply_property psp)
{
	switch (psp) {
	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
		return true;
	default:
		return false;
	}
}

static enum power_supply_property pm8916_lbc_charger_properties[] = {
	POWER_SUPPLY_PROP_ONLINE,
	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
};

static irqreturn_t pm8916_lbc_charger_state_changed_irq(int irq, void *data)
{
	struct pm8916_lbc_charger *chg = data;
	unsigned int tmp;
	int ret;

	ret = regmap_read(chg->regmap, chg->reg[LBC_USB] + PM8916_INT_RT_STS, &tmp);
	if (ret)
		return IRQ_HANDLED;

	chg->online = !!(tmp & PM8916_LBC_USB_USBIN_VALID);
	extcon_set_state_sync(chg->edev, EXTCON_USB, chg->online);

	power_supply_changed(chg->charger);

	return IRQ_HANDLED;
}

static int pm8916_lbc_charger_probe_dt(struct pm8916_lbc_charger *chg)
{
	struct device *dev = chg->dev;
	int ret = 0;
	unsigned int tmp;

	ret = device_property_read_u32(dev, "qcom,fast-charge-safe-voltage", &chg->charge_voltage_safe);
	if (ret)
		return ret;
	if (chg->charge_voltage_safe < PM8916_LBC_CHGR_MIN_VOLTAGE)
		return -EINVAL;

	chg->charge_voltage_safe = clamp_t(u32, chg->charge_voltage_safe,
					PM8916_LBC_CHGR_MIN_VOLTAGE, PM8916_LBC_CHGR_MAX_VOLTAGE);

	tmp = chg->charge_voltage_safe - PM8916_LBC_CHGR_MIN_VOLTAGE;
	tmp /= PM8916_LBC_CHGR_VOLTAGE_STEP;
	ret = regmap_write(chg->regmap, chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_VDD_SAFE, tmp);
	if (ret)
		return ret;

	ret = device_property_read_u32(dev, "qcom,fast-charge-safe-current", &chg->charge_current_safe);
	if (ret)
		return ret;
	if (chg->charge_current_safe < PM8916_LBC_CHGR_MIN_CURRENT)
		return -EINVAL;

	chg->charge_current_safe = clamp_t(u32, chg->charge_current_safe,
					PM8916_LBC_CHGR_MIN_CURRENT, PM8916_LBC_CHGR_MAX_CURRENT);

	chg->charge_current_max = chg->charge_current_safe;

	tmp = chg->charge_current_safe / PM8916_LBC_CHGR_MIN_CURRENT - 1;
	ret = regmap_write(chg->regmap, chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_IBAT_SAFE, tmp);
	if (ret)
		return ret;

	/* Disable charger timeout. */
	ret = regmap_write(chg->regmap, chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_TCHG_MAX_EN, 0x00);
	if (ret)
		return ret;

	return ret;
}

static const struct power_supply_desc pm8916_lbc_charger_psy_desc = {
	.name = "pm8916-lbc-chgr",
	.type = POWER_SUPPLY_TYPE_USB,
	.properties = pm8916_lbc_charger_properties,
	.num_properties = ARRAY_SIZE(pm8916_lbc_charger_properties),
	.get_property = pm8916_lbc_charger_get_property,
	.set_property = pm8916_lbc_charger_set_property,
	.property_is_writeable = pm8916_lbc_charger_property_is_writeable,
};

static int pm8916_lbc_charger_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct pm8916_lbc_charger *chg;
	struct power_supply_config psy_cfg = {};
	int ret, len, irq;
	unsigned int tmp;

	chg = devm_kzalloc(dev, sizeof(*chg), GFP_KERNEL);
	if (!chg)
		return -ENOMEM;

	chg->dev = dev;

	chg->regmap = dev_get_regmap(pdev->dev.parent, NULL);
	if (!chg->regmap)
		return -ENODEV;

	len = device_property_count_u32(dev, "reg");
	if (len < 0)
		return len;
	if (len != 4)
		return dev_err_probe(dev, -EINVAL,
				     "Wrong amount of reg values: %d (4 expected)\n", len);

	irq = platform_get_irq_byname(pdev, "usb_vbus");
	if (irq < 0)
		return irq;

	ret = devm_request_threaded_irq(dev, irq, NULL, pm8916_lbc_charger_state_changed_irq,
					IRQF_ONESHOT, "pm8916_lbc", chg);
	if (ret)
		return ret;

	ret = device_property_read_u32_array(dev, "reg", chg->reg, len);
	if (ret)
		return ret;

	ret = regmap_bulk_read(chg->regmap, chg->reg[LBC_CHGR] + PM8916_PERPH_TYPE, &tmp, 2);
	if (ret)
		goto comm_error;
	if (tmp != PM8916_LBC_CHGR_TYPE)
		goto type_error;

	ret = regmap_bulk_read(chg->regmap, chg->reg[LBC_BAT_IF] + PM8916_PERPH_TYPE, &tmp, 2);
	if (ret)
		goto comm_error;
	if (tmp != PM8916_LBC_BAT_IF_TYPE)
		goto type_error;

	ret = regmap_bulk_read(chg->regmap, chg->reg[LBC_USB] + PM8916_PERPH_TYPE, &tmp, 2);
	if (ret)
		goto comm_error;
	if (tmp != PM8916_LBC_USB_TYPE)
		goto type_error;

	ret = regmap_bulk_read(chg->regmap, chg->reg[LBC_MISC] + PM8916_PERPH_TYPE, &tmp, 2);
	if (ret)
		goto comm_error;
	if (tmp != PM8916_LBC_MISC_TYPE)
		goto type_error;

	ret = regmap_read(chg->regmap, chg->reg[LBC_CHGR] + PM8916_LBC_CHGR_CHG_OPTION, &tmp);
	if (ret)
		goto comm_error;
	if (tmp != PM8916_LBC_CHGR_PMIC_CHARGER)
		dev_err_probe(dev, -ENODEV, "The system is using an external charger\n");

	ret = pm8916_lbc_charger_probe_dt(chg);
	if (ret)
		dev_err_probe(dev, ret, "Error while parsing device tree\n");

	psy_cfg.drv_data = chg;
	psy_cfg.of_node = dev->of_node;

	chg->charger = devm_power_supply_register(dev, &pm8916_lbc_charger_psy_desc, &psy_cfg);
	if (IS_ERR(chg->charger))
		return dev_err_probe(dev, PTR_ERR(chg->charger), "Unable to register charger\n");

	ret = power_supply_get_battery_info(chg->charger, &chg->info);
	if (ret)
		return dev_err_probe(dev, ret, "Unable to get battery info\n");

	chg->edev = devm_extcon_dev_allocate(dev, pm8916_lbc_charger_cable);
	if (IS_ERR(chg->edev))
		return PTR_ERR(chg->edev);

	ret = devm_extcon_dev_register(dev, chg->edev);
	if (ret < 0)
		return dev_err_probe(dev, ret, "failed to register extcon device\n");

	ret = regmap_read(chg->regmap, chg->reg[LBC_USB] + PM8916_INT_RT_STS, &tmp);
	if (ret)
		goto comm_error;

	chg->online = !!(tmp & PM8916_LBC_USB_USBIN_VALID);
	extcon_set_state_sync(chg->edev, EXTCON_USB, chg->online);

	chg->charge_voltage_max = chg->info->voltage_max_design_uv;
	ret = pm8916_lbc_charger_configure(chg);
	if (ret)
		return ret;

	return 0;

comm_error:
	return dev_err_probe(dev, ret, "Unable to communicate with device\n");

type_error:
	return dev_err_probe(dev, -ENODEV, "Device reported wrong type: 0x%X\n", tmp);
}

static const struct of_device_id pm8916_lbc_charger_of_match[] = {
	{ .compatible = "qcom,pm8916-lbc", },
	{}
};
MODULE_DEVICE_TABLE(of, pm8916_lbc_charger_of_match);

static struct platform_driver pm8916_lbc_charger_driver = {
	.driver = {
		.name = "pm8916-lbc",
		.of_match_table = pm8916_lbc_charger_of_match,
	},
	.probe = pm8916_lbc_charger_probe,
};
module_platform_driver(pm8916_lbc_charger_driver);

MODULE_DESCRIPTION("pm8916 LBC driver");
MODULE_AUTHOR("Nikita Travkin <nikita@trvn.ru>");
MODULE_LICENSE("GPL");