summaryrefslogtreecommitdiff
path: root/drivers/platform/x86/lenovo-yoga-tab2-pro-1380-fastcharger.c
blob: d525bdc8ca9b3f8732e8878d8f9c6cf0050157bd (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Support for the custom fast charging protocol found on the Lenovo Yoga
 * Tablet 2 1380F / 1380L models.
 *
 * Copyright (C) 2024 Hans de Goede <hansg@kernel.org>
 */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/delay.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/extcon.h>
#include <linux/gpio/consumer.h>
#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/pinctrl/consumer.h>
#include <linux/pinctrl/machine.h>
#include <linux/platform_device.h>
#include <linux/serdev.h>
#include <linux/time.h>
#include <linux/types.h>
#include <linux/workqueue.h>
#include "serdev_helpers.h"

#define YT2_1380_FC_PDEV_NAME		"lenovo-yoga-tab2-pro-1380-fastcharger"
#define YT2_1380_FC_SERDEV_CTRL		"serial0"
#define YT2_1380_FC_SERDEV_NAME		"serial0-0"
#define YT2_1380_FC_EXTCON_NAME		"i2c-lc824206xa"

#define YT2_1380_FC_MAX_TRIES		5
#define YT2_1380_FC_PIN_SW_DELAY_US	(10 * USEC_PER_MSEC)
#define YT2_1380_FC_UART_DRAIN_DELAY_US	(50 * USEC_PER_MSEC)
#define YT2_1380_FC_VOLT_SW_DELAY_US	(1000 * USEC_PER_MSEC)

struct yt2_1380_fc {
	struct device *dev;
	struct pinctrl *pinctrl;
	struct pinctrl_state *gpio_state;
	struct pinctrl_state *uart_state;
	struct gpio_desc *uart3_txd;
	struct gpio_desc *uart3_rxd;
	struct extcon_dev *extcon;
	struct notifier_block nb;
	struct work_struct work;
	bool fast_charging;
};

static int yt2_1380_fc_set_gpio_mode(struct yt2_1380_fc *fc, bool enable)
{
	struct pinctrl_state *state = enable ? fc->gpio_state : fc->uart_state;
	int ret;

	ret = pinctrl_select_state(fc->pinctrl, state);
	if (ret) {
		dev_err(fc->dev, "Error %d setting pinctrl state\n", ret);
		return ret;
	}

	fsleep(YT2_1380_FC_PIN_SW_DELAY_US);
	return 0;
}

static bool yt2_1380_fc_dedicated_charger_connected(struct yt2_1380_fc *fc)
{
	return extcon_get_state(fc->extcon, EXTCON_CHG_USB_DCP) > 0;
}

static bool yt2_1380_fc_fast_charger_connected(struct yt2_1380_fc *fc)
{
	return extcon_get_state(fc->extcon, EXTCON_CHG_USB_FAST) > 0;
}

static void yt2_1380_fc_worker(struct work_struct *work)
{
	struct yt2_1380_fc *fc = container_of(work, struct yt2_1380_fc, work);
	int i, ret;

	/* Do nothing if already fast charging */
	if (yt2_1380_fc_fast_charger_connected(fc))
		return;

	for (i = 0; i < YT2_1380_FC_MAX_TRIES; i++) {
		/* Set pins to UART mode (for charger disconnect and retries) */
		ret = yt2_1380_fc_set_gpio_mode(fc, false);
		if (ret)
			return;

		/* Only try 12V charging if a dedicated charger is detected */
		if (!yt2_1380_fc_dedicated_charger_connected(fc))
			return;

		/* Send the command to switch to 12V charging */
		ret = serdev_device_write_buf(to_serdev_device(fc->dev), "SC", strlen("SC"));
		if (ret != strlen("SC")) {
			dev_err(fc->dev, "Error %d writing to uart\n", ret);
			return;
		}

		fsleep(YT2_1380_FC_UART_DRAIN_DELAY_US);

		/* Re-check a charger is still connected */
		if (!yt2_1380_fc_dedicated_charger_connected(fc))
			return;

		/*
		 * Now switch the lines to GPIO (output, high). The charger
		 * expects the lines being driven high after the command.
		 * Presumably this is used to detect the tablet getting
		 * unplugged (to switch back to 5V output on unplug).
		 */
		ret = yt2_1380_fc_set_gpio_mode(fc, true);
		if (ret)
			return;

		fsleep(YT2_1380_FC_VOLT_SW_DELAY_US);

		if (yt2_1380_fc_fast_charger_connected(fc))
			return; /* Success */
	}

	dev_dbg(fc->dev, "Failed to switch to 12V charging (not the original charger?)\n");
	/* Failed to enable 12V fast charging, reset pins to default UART mode */
	yt2_1380_fc_set_gpio_mode(fc, false);
}

static int yt2_1380_fc_extcon_evt(struct notifier_block *nb,
				  unsigned long event, void *param)
{
	struct yt2_1380_fc *fc = container_of(nb, struct yt2_1380_fc, nb);

	schedule_work(&fc->work);
	return NOTIFY_OK;
}

static size_t yt2_1380_fc_receive(struct serdev_device *serdev, const u8 *data, size_t len)
{
	/*
	 * Since the USB data lines are shorted for DCP detection, echos of
	 * the "SC" command send in yt2_1380_fc_worker() will be received.
	 */
	dev_dbg(&serdev->dev, "recv: %*ph\n", (int)len, data);
	return len;
}

static const struct serdev_device_ops yt2_1380_fc_serdev_ops = {
	.receive_buf = yt2_1380_fc_receive,
	.write_wakeup = serdev_device_write_wakeup,
};

static int yt2_1380_fc_serdev_probe(struct serdev_device *serdev)
{
	struct device *dev = &serdev->dev;
	struct yt2_1380_fc *fc;
	int ret;

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

	fc->dev = dev;
	fc->nb.notifier_call = yt2_1380_fc_extcon_evt;
	INIT_WORK(&fc->work, yt2_1380_fc_worker);

	/*
	 * Do this first since it may return -EPROBE_DEFER.
	 * There is no extcon_put(), so there is no need to free this.
	 */
	fc->extcon = extcon_get_extcon_dev(YT2_1380_FC_EXTCON_NAME);
	if (IS_ERR(fc->extcon))
		return dev_err_probe(dev, PTR_ERR(fc->extcon), "getting extcon\n");

	fc->pinctrl = devm_pinctrl_get(dev);
	if (IS_ERR(fc->pinctrl))
		return dev_err_probe(dev, PTR_ERR(fc->pinctrl), "getting pinctrl\n");

	/*
	 * To switch the UART3 pins connected to the USB data lines between
	 * UART and GPIO modes.
	 */
	fc->gpio_state = pinctrl_lookup_state(fc->pinctrl, "uart3_gpio");
	fc->uart_state = pinctrl_lookup_state(fc->pinctrl, "uart3_uart");
	if (IS_ERR(fc->gpio_state) || IS_ERR(fc->uart_state))
		return dev_err_probe(dev, -EINVAL, "getting pinctrl states\n");

	ret = yt2_1380_fc_set_gpio_mode(fc, true);
	if (ret)
		return ret;

	fc->uart3_txd = devm_gpiod_get(dev, "uart3_txd", GPIOD_OUT_HIGH);
	if (IS_ERR(fc->uart3_txd))
		return dev_err_probe(dev, PTR_ERR(fc->uart3_txd), "getting uart3_txd gpio\n");

	fc->uart3_rxd = devm_gpiod_get(dev, "uart3_rxd", GPIOD_OUT_HIGH);
	if (IS_ERR(fc->uart3_rxd))
		return dev_err_probe(dev, PTR_ERR(fc->uart3_rxd), "getting uart3_rxd gpio\n");

	ret = yt2_1380_fc_set_gpio_mode(fc, false);
	if (ret)
		return ret;

	ret = devm_serdev_device_open(dev, serdev);
	if (ret)
		return dev_err_probe(dev, ret, "opening UART device\n");

	serdev_device_set_baudrate(serdev, 600);
	serdev_device_set_flow_control(serdev, false);
	serdev_device_set_drvdata(serdev, fc);
	serdev_device_set_client_ops(serdev, &yt2_1380_fc_serdev_ops);

	ret = devm_extcon_register_notifier_all(dev, fc->extcon, &fc->nb);
	if (ret)
		return dev_err_probe(dev, ret, "registering extcon notifier\n");

	/* In case the extcon already has detected a DCP charger */
	schedule_work(&fc->work);

	return 0;
}

struct serdev_device_driver yt2_1380_fc_serdev_driver = {
	.probe = yt2_1380_fc_serdev_probe,
	.driver = {
		.name = KBUILD_MODNAME,
	},
};

static const struct pinctrl_map yt2_1380_fc_pinctrl_map[] = {
	PIN_MAP_MUX_GROUP(YT2_1380_FC_SERDEV_NAME, "uart3_uart",
			  "INT33FC:00", "uart3_grp", "uart"),
	PIN_MAP_MUX_GROUP(YT2_1380_FC_SERDEV_NAME, "uart3_gpio",
			  "INT33FC:00", "uart3_grp_gpio", "gpio"),
};

static int yt2_1380_fc_pdev_probe(struct platform_device *pdev)
{
	struct serdev_device *serdev;
	struct device *ctrl_dev;
	int ret;

	/* Register pinctrl mappings for setting the UART3 pins mode */
	ret = pinctrl_register_mappings(yt2_1380_fc_pinctrl_map,
					ARRAY_SIZE(yt2_1380_fc_pinctrl_map));
	if (ret)
		return ret;

	/* And create the serdev to talk to the charger over the UART3 pins */
	ctrl_dev = get_serdev_controller("PNP0501", "1", 0, YT2_1380_FC_SERDEV_CTRL);
	if (IS_ERR(ctrl_dev)) {
		ret = PTR_ERR(ctrl_dev);
		goto out_pinctrl_unregister_mappings;
	}

	serdev = serdev_device_alloc(to_serdev_controller(ctrl_dev));
	put_device(ctrl_dev);
	if (!serdev) {
		ret = -ENOMEM;
		goto out_pinctrl_unregister_mappings;
	}

	ret = serdev_device_add(serdev);
	if (ret) {
		dev_err_probe(&pdev->dev, ret, "adding serdev\n");
		serdev_device_put(serdev);
		goto out_pinctrl_unregister_mappings;
	}

	/*
	 * serdev device <-> driver matching relies on OF or ACPI matches and
	 * neither is available here, manually bind the driver.
	 */
	ret = device_driver_attach(&yt2_1380_fc_serdev_driver.driver, &serdev->dev);
	if (ret) {
		/* device_driver_attach() maps EPROBE_DEFER to EAGAIN, map it back */
		ret = (ret == -EAGAIN) ? -EPROBE_DEFER : ret;
		dev_err_probe(&pdev->dev, ret, "attaching serdev driver\n");
		goto out_serdev_device_remove;
	}

	/* So that yt2_1380_fc_pdev_remove() can remove the serdev */
	platform_set_drvdata(pdev, serdev);
	return 0;

out_serdev_device_remove:
	serdev_device_remove(serdev);
out_pinctrl_unregister_mappings:
	pinctrl_unregister_mappings(yt2_1380_fc_pinctrl_map);
	return ret;
}

static void yt2_1380_fc_pdev_remove(struct platform_device *pdev)
{
	struct serdev_device *serdev = platform_get_drvdata(pdev);

	serdev_device_remove(serdev);
	pinctrl_unregister_mappings(yt2_1380_fc_pinctrl_map);
}

static struct platform_driver yt2_1380_fc_pdev_driver = {
	.probe = yt2_1380_fc_pdev_probe,
	.remove_new = yt2_1380_fc_pdev_remove,
	.driver = {
		.name = YT2_1380_FC_PDEV_NAME,
		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
	},
};

static int __init yt2_1380_fc_module_init(void)
{
	int ret;

	/*
	 * serdev driver MUST be registered first because pdev driver calls
	 * device_driver_attach() on the serdev, serdev-driver pair.
	 */
	ret = serdev_device_driver_register(&yt2_1380_fc_serdev_driver);
	if (ret)
		return ret;

	ret = platform_driver_register(&yt2_1380_fc_pdev_driver);
	if (ret)
		serdev_device_driver_unregister(&yt2_1380_fc_serdev_driver);

	return ret;
}
module_init(yt2_1380_fc_module_init);

static void __exit yt2_1380_fc_module_exit(void)
{
	platform_driver_unregister(&yt2_1380_fc_pdev_driver);
	serdev_device_driver_unregister(&yt2_1380_fc_serdev_driver);
}
module_exit(yt2_1380_fc_module_exit);

MODULE_ALIAS("platform:" YT2_1380_FC_PDEV_NAME);
MODULE_DESCRIPTION("Lenovo Yoga Tablet 2 1380 fast charge driver");
MODULE_AUTHOR("Hans de Goede <hansg@kernel.org>");
MODULE_LICENSE("GPL");