summaryrefslogtreecommitdiff
path: root/drivers/usb/misc/apple-mfi-fastcharge.c
blob: ac8695195c13c8752076e4391ac81a9da3780c44 (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Fast-charge control for Apple "MFi" devices
 *
 * Copyright (C) 2019 Bastien Nocera <hadess@hadess.net>
 */

/* Standard include files */
#include <linux/module.h>
#include <linux/power_supply.h>
#include <linux/slab.h>
#include <linux/usb.h>

MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
MODULE_DESCRIPTION("Fast-charge control for Apple \"MFi\" devices");
MODULE_LICENSE("GPL");

#define TRICKLE_CURRENT_MA		0
#define FAST_CURRENT_MA			2500

#define APPLE_VENDOR_ID			0x05ac	/* Apple */

/* The product ID is defined as starting with 0x12nn, as per the
 * "Choosing an Apple Device USB Configuration" section in
 * release R9 (2012) of the "MFi Accessory Hardware Specification"
 *
 * To distinguish an Apple device, a USB host can check the device
 * descriptor of attached USB devices for the following fields:
 * ■ Vendor ID: 0x05AC
 * ■ Product ID: 0x12nn
 *
 * Those checks will be done in .match() and .probe().
 */

static const struct usb_device_id mfi_fc_id_table[] = {
	{ .idVendor = APPLE_VENDOR_ID,
	  .match_flags = USB_DEVICE_ID_MATCH_VENDOR },
	{},
};

MODULE_DEVICE_TABLE(usb, mfi_fc_id_table);

/* Driver-local specific stuff */
struct mfi_device {
	struct usb_device *udev;
	struct power_supply *battery;
	int charge_type;
};

static int apple_mfi_fc_set_charge_type(struct mfi_device *mfi,
					const union power_supply_propval *val)
{
	int current_ma;
	int retval;
	__u8 request_type;

	if (mfi->charge_type == val->intval) {
		dev_dbg(&mfi->udev->dev, "charge type %d already set\n",
				mfi->charge_type);
		return 0;
	}

	switch (val->intval) {
	case POWER_SUPPLY_CHARGE_TYPE_TRICKLE:
		current_ma = TRICKLE_CURRENT_MA;
		break;
	case POWER_SUPPLY_CHARGE_TYPE_FAST:
		current_ma = FAST_CURRENT_MA;
		break;
	default:
		return -EINVAL;
	}

	request_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
	retval = usb_control_msg(mfi->udev, usb_sndctrlpipe(mfi->udev, 0),
				 0x40, /* Vendor‐defined power request */
				 request_type,
				 current_ma, /* wValue, current offset */
				 current_ma, /* wIndex, current offset */
				 NULL, 0, USB_CTRL_GET_TIMEOUT);
	if (retval) {
		dev_dbg(&mfi->udev->dev, "retval = %d\n", retval);
		return retval;
	}

	mfi->charge_type = val->intval;

	return 0;
}

static int apple_mfi_fc_get_property(struct power_supply *psy,
		enum power_supply_property psp,
		union power_supply_propval *val)
{
	struct mfi_device *mfi = power_supply_get_drvdata(psy);

	dev_dbg(&mfi->udev->dev, "prop: %d\n", psp);

	switch (psp) {
	case POWER_SUPPLY_PROP_CHARGE_TYPE:
		val->intval = mfi->charge_type;
		break;
	case POWER_SUPPLY_PROP_SCOPE:
		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
		break;
	default:
		return -ENODATA;
	}

	return 0;
}

static int apple_mfi_fc_set_property(struct power_supply *psy,
		enum power_supply_property psp,
		const union power_supply_propval *val)
{
	struct mfi_device *mfi = power_supply_get_drvdata(psy);
	int ret;

	dev_dbg(&mfi->udev->dev, "prop: %d\n", psp);

	ret = pm_runtime_get_sync(&mfi->udev->dev);
	if (ret < 0) {
		pm_runtime_put_noidle(&mfi->udev->dev);
		return ret;
	}

	switch (psp) {
	case POWER_SUPPLY_PROP_CHARGE_TYPE:
		ret = apple_mfi_fc_set_charge_type(mfi, val);
		break;
	default:
		ret = -EINVAL;
	}

	pm_runtime_mark_last_busy(&mfi->udev->dev);
	pm_runtime_put_autosuspend(&mfi->udev->dev);

	return ret;
}

static int apple_mfi_fc_property_is_writeable(struct power_supply *psy,
					      enum power_supply_property psp)
{
	switch (psp) {
	case POWER_SUPPLY_PROP_CHARGE_TYPE:
		return 1;
	default:
		return 0;
	}
}

static enum power_supply_property apple_mfi_fc_properties[] = {
	POWER_SUPPLY_PROP_CHARGE_TYPE,
	POWER_SUPPLY_PROP_SCOPE
};

static const struct power_supply_desc apple_mfi_fc_desc = {
	.name                   = "apple_mfi_fastcharge",
	.type                   = POWER_SUPPLY_TYPE_BATTERY,
	.properties             = apple_mfi_fc_properties,
	.num_properties         = ARRAY_SIZE(apple_mfi_fc_properties),
	.get_property           = apple_mfi_fc_get_property,
	.set_property           = apple_mfi_fc_set_property,
	.property_is_writeable  = apple_mfi_fc_property_is_writeable
};

static bool mfi_fc_match(struct usb_device *udev)
{
	int idProduct;

	idProduct = le16_to_cpu(udev->descriptor.idProduct);
	/* See comment above mfi_fc_id_table[] */
	return (idProduct >= 0x1200 && idProduct <= 0x12ff);
}

static int mfi_fc_probe(struct usb_device *udev)
{
	struct power_supply_config battery_cfg = {};
	struct mfi_device *mfi = NULL;
	int err;

	if (!mfi_fc_match(udev))
		return -ENODEV;

	mfi = kzalloc(sizeof(struct mfi_device), GFP_KERNEL);
	if (!mfi)
		return -ENOMEM;

	battery_cfg.drv_data = mfi;

	mfi->charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
	mfi->battery = power_supply_register(&udev->dev,
						&apple_mfi_fc_desc,
						&battery_cfg);
	if (IS_ERR(mfi->battery)) {
		dev_err(&udev->dev, "Can't register battery\n");
		err = PTR_ERR(mfi->battery);
		kfree(mfi);
		return err;
	}

	mfi->udev = usb_get_dev(udev);
	dev_set_drvdata(&udev->dev, mfi);

	return 0;
}

static void mfi_fc_disconnect(struct usb_device *udev)
{
	struct mfi_device *mfi;

	mfi = dev_get_drvdata(&udev->dev);
	if (mfi->battery)
		power_supply_unregister(mfi->battery);
	dev_set_drvdata(&udev->dev, NULL);
	usb_put_dev(mfi->udev);
	kfree(mfi);
}

static struct usb_device_driver mfi_fc_driver = {
	.name =		"apple-mfi-fastcharge",
	.probe =	mfi_fc_probe,
	.disconnect =	mfi_fc_disconnect,
	.id_table =	mfi_fc_id_table,
	.match =	mfi_fc_match,
	.generic_subclass = 1,
};

static int __init mfi_fc_driver_init(void)
{
	return usb_register_device_driver(&mfi_fc_driver, THIS_MODULE);
}

static void __exit mfi_fc_driver_exit(void)
{
	usb_deregister_device_driver(&mfi_fc_driver);
}

module_init(mfi_fc_driver_init);
module_exit(mfi_fc_driver_exit);