summaryrefslogtreecommitdiff
path: root/drivers/power/supply/ltc3651-charger.c
blob: eea63ff211c4b69618bd9b68c60c3ace57c1f21c (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
/*
 *  Copyright (C) 2017, Topic Embedded Products
 *  Driver for LTC3651 charger IC.
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under  the terms of the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the License, or (at your
 *  option) any later version.
 */

#include <linux/device.h>
#include <linux/gpio/consumer.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/slab.h>
#include <linux/of.h>

struct ltc3651_charger {
	struct power_supply *charger;
	struct power_supply_desc charger_desc;
	struct gpio_desc *acpr_gpio;
	struct gpio_desc *fault_gpio;
	struct gpio_desc *chrg_gpio;
};

static irqreturn_t ltc3651_charger_irq(int irq, void *devid)
{
	struct power_supply *charger = devid;

	power_supply_changed(charger);

	return IRQ_HANDLED;
}

static inline struct ltc3651_charger *psy_to_ltc3651_charger(
	struct power_supply *psy)
{
	return power_supply_get_drvdata(psy);
}

static int ltc3651_charger_get_property(struct power_supply *psy,
		enum power_supply_property psp, union power_supply_propval *val)
{
	struct ltc3651_charger *ltc3651_charger = psy_to_ltc3651_charger(psy);

	switch (psp) {
	case POWER_SUPPLY_PROP_STATUS:
		if (!ltc3651_charger->chrg_gpio) {
			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
			break;
		}
		if (gpiod_get_value(ltc3651_charger->chrg_gpio))
			val->intval = POWER_SUPPLY_STATUS_CHARGING;
		else
			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
		break;
	case POWER_SUPPLY_PROP_ONLINE:
		val->intval = gpiod_get_value(ltc3651_charger->acpr_gpio);
		break;
	case POWER_SUPPLY_PROP_HEALTH:
		if (!ltc3651_charger->fault_gpio) {
			val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
			break;
		}
		if (!gpiod_get_value(ltc3651_charger->fault_gpio)) {
			val->intval = POWER_SUPPLY_HEALTH_GOOD;
			break;
		}
		/*
		 * If the fault pin is active, the chrg pin explains the type
		 * of failure.
		 */
		if (!ltc3651_charger->chrg_gpio) {
			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
			break;
		}
		val->intval = gpiod_get_value(ltc3651_charger->chrg_gpio) ?
				POWER_SUPPLY_HEALTH_OVERHEAT :
				POWER_SUPPLY_HEALTH_DEAD;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static enum power_supply_property ltc3651_charger_properties[] = {
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_ONLINE,
	POWER_SUPPLY_PROP_HEALTH,
};

static int ltc3651_charger_probe(struct platform_device *pdev)
{
	struct power_supply_config psy_cfg = {};
	struct ltc3651_charger *ltc3651_charger;
	struct power_supply_desc *charger_desc;
	int ret;

	ltc3651_charger = devm_kzalloc(&pdev->dev, sizeof(*ltc3651_charger),
					GFP_KERNEL);
	if (!ltc3651_charger)
		return -ENOMEM;

	ltc3651_charger->acpr_gpio = devm_gpiod_get(&pdev->dev,
					"lltc,acpr", GPIOD_IN);
	if (IS_ERR(ltc3651_charger->acpr_gpio)) {
		ret = PTR_ERR(ltc3651_charger->acpr_gpio);
		dev_err(&pdev->dev, "Failed to acquire acpr GPIO: %d\n", ret);
		return ret;
	}
	ltc3651_charger->fault_gpio = devm_gpiod_get_optional(&pdev->dev,
					"lltc,fault", GPIOD_IN);
	if (IS_ERR(ltc3651_charger->fault_gpio)) {
		ret = PTR_ERR(ltc3651_charger->fault_gpio);
		dev_err(&pdev->dev, "Failed to acquire fault GPIO: %d\n", ret);
		return ret;
	}
	ltc3651_charger->chrg_gpio = devm_gpiod_get_optional(&pdev->dev,
					"lltc,chrg", GPIOD_IN);
	if (IS_ERR(ltc3651_charger->chrg_gpio)) {
		ret = PTR_ERR(ltc3651_charger->chrg_gpio);
		dev_err(&pdev->dev, "Failed to acquire chrg GPIO: %d\n", ret);
		return ret;
	}

	charger_desc = &ltc3651_charger->charger_desc;
	charger_desc->name = pdev->dev.of_node->name;
	charger_desc->type = POWER_SUPPLY_TYPE_MAINS;
	charger_desc->properties = ltc3651_charger_properties;
	charger_desc->num_properties = ARRAY_SIZE(ltc3651_charger_properties);
	charger_desc->get_property = ltc3651_charger_get_property;
	psy_cfg.of_node = pdev->dev.of_node;
	psy_cfg.drv_data = ltc3651_charger;

	ltc3651_charger->charger = devm_power_supply_register(&pdev->dev,
						      charger_desc, &psy_cfg);
	if (IS_ERR(ltc3651_charger->charger)) {
		ret = PTR_ERR(ltc3651_charger->charger);
		dev_err(&pdev->dev, "Failed to register power supply: %d\n",
			ret);
		return ret;
	}

	/*
	 * Acquire IRQs for the GPIO pins if possible. If the system does not
	 * support IRQs on these pins, userspace will have to poll the sysfs
	 * files manually.
	 */
	if (ltc3651_charger->acpr_gpio) {
		ret = gpiod_to_irq(ltc3651_charger->acpr_gpio);
		if (ret >= 0)
			ret = devm_request_any_context_irq(&pdev->dev, ret,
				ltc3651_charger_irq,
				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
				dev_name(&pdev->dev), ltc3651_charger->charger);
		if (ret < 0)
			dev_warn(&pdev->dev, "Failed to request acpr irq\n");
	}
	if (ltc3651_charger->fault_gpio) {
		ret = gpiod_to_irq(ltc3651_charger->fault_gpio);
		if (ret >= 0)
			ret = devm_request_any_context_irq(&pdev->dev, ret,
				ltc3651_charger_irq,
				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
				dev_name(&pdev->dev), ltc3651_charger->charger);
		if (ret < 0)
			dev_warn(&pdev->dev, "Failed to request fault irq\n");
	}
	if (ltc3651_charger->chrg_gpio) {
		ret = gpiod_to_irq(ltc3651_charger->chrg_gpio);
		if (ret >= 0)
			ret = devm_request_any_context_irq(&pdev->dev, ret,
				ltc3651_charger_irq,
				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
				dev_name(&pdev->dev), ltc3651_charger->charger);
		if (ret < 0)
			dev_warn(&pdev->dev, "Failed to request chrg irq\n");
	}

	platform_set_drvdata(pdev, ltc3651_charger);

	return 0;
}

static const struct of_device_id ltc3651_charger_match[] = {
	{ .compatible = "lltc,ltc3651-charger" },
	{ }
};
MODULE_DEVICE_TABLE(of, ltc3651_charger_match);

static struct platform_driver ltc3651_charger_driver = {
	.probe = ltc3651_charger_probe,
	.driver = {
		.name = "ltc3651-charger",
		.of_match_table = ltc3651_charger_match,
	},
};

module_platform_driver(ltc3651_charger_driver);

MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
MODULE_DESCRIPTION("Driver for LTC3651 charger");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:ltc3651-charger");