summaryrefslogtreecommitdiff
path: root/drivers/power/supply/google/google-smblite-hvdcp.c
blob: 74611be524e36d31ca5170cff5d4c57569f839c3 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright 2023 Google LLC */

/*
Controller state transitions illustrated below in graphviz format:

	digraph {
		OFF -> USBIN_SUSPEND_WAIT [label="HVDCP request"]

		USBIN_SUSPEND_WAIT -> ENTER_IDLE_WAIT [label="usbin suspended"]

		ENTER_IDLE_WAIT -> NEGOTIATION [label="Charger in QC3.0 idle mode"]

		NEGOTIATION -> NEGOTIATION [label="Voltage adjusted"]
		NEGOTIATION -> SETTLE_MONITOR [label="Voltage within target"]

		SETTLE_MONITOR -> NEGOTIATION [label="Voltage changed"]
		SETTLE_MONITOR -> USBIN_RESUME_WAIT [label="Voltage remained consistent"]

		USBIN_RESUME_WAIT -> DONE [label="usbin resumed"]
		DONE -> OFF [label="unplug"]
	}
*/

#include <linux/alarmtimer.h>
#include <linux/delay.h>
#include <linux/jiffies.h>
#include <linux/ktime.h>
#include <linux/limits.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/notifier.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/workqueue.h>
#include "smblite-shim.h"
#include "smblite-reg.h"
#include "misc/gvotable.h"

#define GOOGLE_HVDCP_VOTER "GOOGLE_HVDCP_VOTER"
#define USBIN_SUSPENDED_MAX_UA 10000
#define HVDCP_PULSE_DELTA_UV 200000
#define NUM_VOLTAGE_READS 15
#define PMIC_USBIN_SUSPENDED_ICL 0
#define VBUS_MEAS_ERROR_UV 30000
#define SUSPEND_WAIT_MS 50
#define VOLTAGE_CHECK_RETRY_MS 50
#define WAIT_DISABLED U32_MAX

#define STATES \
	X(OFF) \
	X(USBIN_SUSPEND_WAIT) \
	X(ENTER_IDLE_WAIT) \
	X(NEGOTIATION) \
	X(SETTLE_MONITOR) \
	X(USBIN_RESUME_WAIT) \
	X(DONE)

#define CMDS \
	X(IDLE) \
	X(FORCE_5V) \
	X(INCREMENT) \
	X(DECREMENT)

#define X(cmd) CMD_##cmd,
enum cmd {
	CMDS
	NUM_CMDS
};
#undef X

#define X(state) HVDCP_##state,
enum hvdcp_state {
	STATES
	NUM_HVDCP_STATES
};
#undef X

#define X(state) #state,
static const char *state_str[] = {
	STATES
};
#undef X

#define X(cmd) #cmd,
static const char *cmd_str[] = {
	CMDS
};
#undef X

struct state_data {
	unsigned int elapsed_ms;
	ktime_t last_elapsed_check_kt;

	unsigned int wait_ms;
	ktime_t last_state_work_kt;
};

struct session_data {
	/*
	 * Slow waits are enabled if a charger voltage change occurs while
	 * in SETTLE_MONITOR state. Spacing out voltage increment / decrement
	 * commands seems to help in avoiding extra voltage changes after the
	 * command is sent.
	 */
	bool slow_waits;
};

struct cmd_wait_info {
	u16 typ;

	/* Used when session_data slow_waits == true */
	u16 slow;
};

struct hvdcp {
	struct mutex lock;
	struct notifier_block hvdcp_req_nb;
	struct notifier_block plugin_nb;
	struct work_struct controller_work;
	struct alarm controller_alarm;
	enum hvdcp_state state;
	struct state_data state_data;
	struct session_data sesh_data;
	struct cmd_wait_info cmd_waits[NUM_CMDS];
	u16 renegotiation_wait_ms;
	u16 cmd_retry_wait_ms;
	u16 settle_monitor_ms;
	u32 voltage_ceiling;
	struct device *dev;
	struct power_supply *shim_psy;
	struct smblite_shim *smblite_shim;
	struct gvotable_election *usb_icl_votable;
	struct gvotable_election *fake_psy_online_votable;
};

static inline uint16_t get_cmd_wait(struct hvdcp *hvdcp, enum cmd cmd)
{
	return hvdcp->sesh_data.slow_waits
		? hvdcp->cmd_waits[cmd].slow : hvdcp->cmd_waits[cmd].typ;
}

static int run_cmd(struct hvdcp *hvdcp, enum cmd cmd, unsigned int *wait_ms_out)
{
	int ret;

	*wait_ms_out = get_cmd_wait(hvdcp, cmd);

	dev_info(hvdcp->dev, "Running cmd %s\n", cmd_str[cmd]);

	if (smblite_lib_is_boost_en(hvdcp->smblite_shim->chg)) {
		dev_err(hvdcp->dev, "Boost mode en (pre-cmd)\n");
		return -EAGAIN;
	}

	switch(cmd) {
	case CMD_IDLE:
		ret = smblite_lib_force_vbus_voltage(hvdcp->smblite_shim->chg,
						IDLE_BIT);
		break;
	case CMD_FORCE_5V:
		ret = smblite_lib_force_vbus_voltage(hvdcp->smblite_shim->chg,
						FORCE_5V_BIT);
		break;
	case CMD_INCREMENT:
		ret = smblite_lib_dp_pulse(hvdcp->smblite_shim->chg);
		break;
	case CMD_DECREMENT:
		ret = smblite_lib_dm_pulse(hvdcp->smblite_shim->chg);
		break;
	default:
		break;
	};

	/*
	 * Try and catch cases where boost occurs at the same time as command.
	 * If boost is active, the PMIC does not toggle data lines for HVDCP
	 * commands
	 */
	if (smblite_lib_is_boost_en(hvdcp->smblite_shim->chg)) {
		dev_err(hvdcp->dev, "Boost mode en (post-cmd)\n");
		return -EAGAIN;
	}

	return ret;
}

static void set_voltage_max(int no_load_voltage)
{
	struct gvotable_election *election =
		gvotable_election_get_handle("SHIM_VMAX");
	int max = DIV_ROUND_CLOSEST(no_load_voltage, 100000) * 100000;

	if (!election)
		return;

	gvotable_cast_int_vote(election, GOOGLE_HVDCP_VOTER, max, (max != 0));
}

static int suspend_usbin(struct hvdcp *hvdcp, bool suspend)
{
	int ret = gvotable_cast_bool_vote(hvdcp->fake_psy_online_votable,
					  GOOGLE_HVDCP_VOTER, suspend);
	if (ret != 0)
		return ret;

	return gvotable_cast_int_vote(hvdcp->usb_icl_votable,
				GOOGLE_HVDCP_VOTER,
				PMIC_USBIN_SUSPENDED_ICL, suspend);
}

static bool is_usbin_suspended(struct hvdcp *hvdcp)
{
	struct smblite_shim *shim = hvdcp->smblite_shim;
	int pmic_icl;
	union power_supply_propval curr_now;
	int ret;

	ret = smblite_lib_get_charge_param(shim->chg,
					&shim->chg->param.icl_stat,
					&pmic_icl);
	if (ret != 0)
		pmic_icl = PMIC_USBIN_SUSPENDED_ICL + 1;

	ret = smblite_lib_get_prop_usbin_current(shim->chg, &curr_now);
	if (ret != 0)
		curr_now.intval = USBIN_SUSPENDED_MAX_UA + 1;

	return (pmic_icl == PMIC_USBIN_SUSPENDED_ICL)
		&& (curr_now.intval < USBIN_SUSPENDED_MAX_UA);
}

static int read_voltage(struct hvdcp *hvdcp, int *voltage_out)
{
	struct smb_charger *chg = hvdcp->smblite_shim->chg;
	union power_supply_propval val;
	int ret;
	int x;
	int avg;
	int minimum = INT_MAX;
	int maximum = 0;

	if (!voltage_out)
		return -EINVAL;

	avg = 0;
	for (x = 0; x < NUM_VOLTAGE_READS; x++) {
		ret = smblite_lib_get_prop_usb_voltage_now(chg, &val);
		if (ret != 0)
			break;
		avg += val.intval;
		minimum = min(minimum, val.intval);
		maximum = max(maximum, val.intval);

		/* Sleep for about a millisecond between samples */
		usleep_range(1000, 1111);
	}

	avg = (avg / NUM_VOLTAGE_READS);
	*voltage_out = (avg + VBUS_MEAS_ERROR_UV);

	if (ret == 0)
		dev_info(hvdcp->dev, "Voltage: avg:%d min:%d max:%d " \
			"returned (w/ %d meas err):%d (uV)\n",
			avg, minimum, maximum, VBUS_MEAS_ERROR_UV,
			*voltage_out);
	else
		dev_err(hvdcp->dev, "Voltage read error: %d\n", ret);

	return ret;
}

static void clear_state_data_locked(struct state_data *data)
{
	data->elapsed_ms = 0;
	data->last_elapsed_check_kt = ktime_get_boottime();

	data->wait_ms = 0;
	data->last_state_work_kt = 0;
}

static void update_elapsed_time_locked(struct state_data *data)
{
	ktime_t now = ktime_get_boottime();
	data->elapsed_ms += ktime_ms_delta(now, data->last_elapsed_check_kt);
	data->last_elapsed_check_kt = now;
}

static void update_state_locked(struct hvdcp *hvdcp,
				enum hvdcp_state new_state)
{
	struct state_data *state_data = &hvdcp->state_data;

	update_elapsed_time_locked(state_data);
	hvdcp->state_data.last_state_work_kt = ktime_get_boottime();

	if (new_state != hvdcp->state) {
		dev_info(hvdcp->dev, "State change: %s -> %s\n",
			state_str[hvdcp->state],
			state_str[new_state]);
		hvdcp->state = new_state;
		clear_state_data_locked(state_data);
	}
}

static void reset_state_locked(struct hvdcp *hvdcp)
{
	update_state_locked(hvdcp, HVDCP_OFF);
	alarm_try_to_cancel(&hvdcp->controller_alarm);
	suspend_usbin(hvdcp, false);
	hvdcp->sesh_data.slow_waits = false;
	clear_state_data_locked(&hvdcp->state_data);
	set_voltage_max(0);
}

static void controller_work(struct work_struct *work)
{
	struct hvdcp *hvdcp = container_of(work, struct hvdcp, controller_work);
	int voltage;
	int delta;
	int ret;
	enum hvdcp_state new_state;
	enum cmd cmd;
	union power_supply_propval plugged;
	struct session_data *sesh_data = &hvdcp->sesh_data;
	struct state_data *state_data = &hvdcp->state_data;
	unsigned int wait_ms = WAIT_DISABLED;
	unsigned int waited_dur_ms;
	ktime_t now;

	dev_dbg(hvdcp->dev, "entry\n");

	mutex_lock(&hvdcp->lock);
	update_elapsed_time_locked(state_data);

	ret = smblite_lib_get_prop_usb_present(hvdcp->smblite_shim->chg,
					&plugged);
	if ((ret != 0) || !plugged.intval) {
		reset_state_locked(hvdcp);
		mutex_unlock(&hvdcp->lock);
		return;
	}

	new_state = hvdcp->state;

	now = ktime_get_boottime();
	waited_dur_ms = ktime_ms_delta(now, state_data->last_state_work_kt);
	if (waited_dur_ms < state_data->wait_ms) {
		int corrected_wait_ms = (state_data->wait_ms - waited_dur_ms);
		dev_dbg(hvdcp->dev, "Need to wait %u ms before handling %s",
			corrected_wait_ms, state_str[hvdcp->state]);
		wait_ms = corrected_wait_ms;
		goto out;
	}

	switch (hvdcp->state) {
	case HVDCP_OFF:
		/*
		 * Suspend the charging path so that:
		 *   - We can read an accurate charger voltage without drops due
		 *     to cable resistance.
		 *   - If the charger boosts to over the voltage ceiling, the
		 *     charging puck won't shut down the eletrical path.
		 *     If the puck's overvoltage threshold is reached, it will
		 *     clamp the voltage and excess power will be dissipated
		 *     in the form of heat. If a significant amount of power is
		 *     dissipated, the generated heat will trip the thermal
		 *     protection and the puck will temporarily shut down.
		 */
		ret = suspend_usbin(hvdcp, true);
		if (ret == 0)
			new_state = HVDCP_USBIN_SUSPEND_WAIT;
		wait_ms = SUSPEND_WAIT_MS;
		break;
	case HVDCP_USBIN_SUSPEND_WAIT:
		if (!is_usbin_suspended(hvdcp)) {
			wait_ms = HVDCP_USBIN_SUSPEND_WAIT;
			break;
		}
		new_state = HVDCP_ENTER_IDLE_WAIT;
		wait_ms = 0;
		break;
	case HVDCP_ENTER_IDLE_WAIT:
		ret = run_cmd(hvdcp, CMD_IDLE, &wait_ms);
		if (ret != 0) {
			wait_ms = hvdcp->cmd_retry_wait_ms;
			break;
		}
		new_state = HVDCP_NEGOTIATION;
		break;
	case HVDCP_NEGOTIATION:
		ret = read_voltage(hvdcp, &voltage);
		if (ret != 0) {
			wait_ms = VOLTAGE_CHECK_RETRY_MS;
			break;
		}
		delta = (hvdcp->voltage_ceiling - voltage);
		if ((delta >= 0) && (delta < HVDCP_PULSE_DELTA_UV)) {
			new_state = HVDCP_SETTLE_MONITOR;
			wait_ms = VOLTAGE_CHECK_RETRY_MS;
			break;
		}
		cmd = (delta > 0) ? CMD_INCREMENT : CMD_DECREMENT;
		ret = run_cmd(hvdcp, cmd, &wait_ms);
		if (ret != 0) {
			wait_ms = hvdcp->cmd_retry_wait_ms;
			break;
		}
		break;
	case HVDCP_SETTLE_MONITOR:
		ret = read_voltage(hvdcp, &voltage);
		if (ret != 0) {
			wait_ms = VOLTAGE_CHECK_RETRY_MS;
			break;
		}
		if (voltage > hvdcp->voltage_ceiling) {
			dev_dbg(hvdcp->dev, "%u uV over %u uV ceiling!\n",
				voltage, hvdcp->voltage_ceiling);
			new_state = HVDCP_NEGOTIATION;
			sesh_data->slow_waits = true;
			wait_ms = hvdcp->renegotiation_wait_ms;
			break;
		}

		delta = (hvdcp->voltage_ceiling - voltage);
		if (delta > (HVDCP_PULSE_DELTA_UV + 20000)) {
			dev_dbg(hvdcp->dev, "Delta %d uV larger than pulse!\n",
				delta);
			new_state = HVDCP_NEGOTIATION;
			sesh_data->slow_waits = true;
			wait_ms = hvdcp->renegotiation_wait_ms;
			break;
		}

		if (state_data->elapsed_ms < hvdcp->settle_monitor_ms) {
			wait_ms = VOLTAGE_CHECK_RETRY_MS;
			break;
		}

		set_voltage_max(voltage);
		new_state = HVDCP_USBIN_RESUME_WAIT;
		wait_ms = 0;
		break;
	case HVDCP_USBIN_RESUME_WAIT:
		ret = suspend_usbin(hvdcp, false);
		if (ret != 0) {
			wait_ms = SUSPEND_WAIT_MS;
			break;
		}
		new_state = HVDCP_DONE;
	default:
		break;
	}

out:
	update_state_locked(hvdcp, new_state);
	mutex_unlock(&hvdcp->lock);

	if (wait_ms != WAIT_DISABLED) {
		dev_dbg(hvdcp->dev, "Next controller run in %d ms\n",
			wait_ms);
		state_data->wait_ms = wait_ms;
		alarm_try_to_cancel(&hvdcp->controller_alarm);
		alarm_start_relative(&hvdcp->controller_alarm,
				ms_to_ktime(wait_ms));
	}
}

static enum alarmtimer_restart
controller_alarm_expired(struct alarm *alarm, ktime_t time)
{
	struct hvdcp *hvdcp = container_of(alarm, struct hvdcp,
					controller_alarm);
	dev_dbg(hvdcp->dev, "entry\n");
	schedule_work(&hvdcp->controller_work);
	return ALARMTIMER_NORESTART;
}

static int hvdcp_req_notify(struct notifier_block *nb,
			unsigned long unused_a, void *unused_b)
{
	struct hvdcp *hvdcp = container_of(nb, struct hvdcp, hvdcp_req_nb);
	dev_dbg(hvdcp->dev, "entry\n");
	mutex_lock(&hvdcp->lock);
	if (hvdcp->state == HVDCP_OFF) {
		reset_state_locked(hvdcp);
		schedule_work(&hvdcp->controller_work);
	}
	mutex_unlock(&hvdcp->lock);
	return NOTIFY_OK;
}

static int plugin_notify(struct notifier_block *nb, unsigned long plugged,
			void *unused)
{
	struct hvdcp *hvdcp = container_of(nb, struct hvdcp, plugin_nb);

	dev_dbg(hvdcp->dev, "%s: %u\n", __func__, plugged);

	/*
	 * Only care about when device is unplugged. For plugin, we rely on
	 * the hvdcp req notifier to start hvdcp negotiation.
	 */
	if (((enum smblite_shim_plug_sts)plugged) == SMBLITE_SHIM_UNPLUGGED) {
		mutex_lock(&hvdcp->lock);
		reset_state_locked(hvdcp);
		mutex_unlock(&hvdcp->lock);
	}

	return NOTIFY_OK;
}

static ssize_t state_show(struct device *dev, struct device_attribute *attr,
			char *buf)
{
	enum hvdcp_state state;
	struct platform_device *pdev =
		container_of(dev, struct platform_device, dev);
	struct hvdcp *hvdcp = platform_get_drvdata(pdev);

	mutex_lock(&hvdcp->lock);
	state = hvdcp->state;
	mutex_unlock(&hvdcp->lock);

	return scnprintf(buf, PAGE_SIZE, "%s\n", state_str[state]);
}
static const DEVICE_ATTR_RO(state);

static int setup_sysfs(struct hvdcp *hvdcp)
{
	struct device *dev = hvdcp->dev;
	int ret;

	ret = device_create_file(hvdcp->dev, &dev_attr_state);
	if (ret < 0) {
		dev_err(dev, "Failed to create %s: %d\n",
			dev_attr_state.attr.name, ret);

		/* Nonfatal */
		ret = 0;
	}

	return ret;
}

static void teardown_sysfs(struct hvdcp *hvdcp)
{
	device_remove_file(hvdcp->dev, &dev_attr_state);
}

static struct power_supply *dt_find_psy(struct device *dev, const char *prop)
{
	int ret;
	struct device_node *node = dev->of_node;
	const char *name;

	ret = of_property_read_string(node, prop, &name);
	if (ret != 0)
		return NULL;

	return power_supply_get_by_name(name);
}

static int hvdcp_probe(struct platform_device *pdev)
{
	struct hvdcp *hvdcp;
	struct gvotable_election *icl_votable;
	struct gvotable_election *fake_psy_online_votable;
	struct power_supply *psy;
	int ret;

	icl_votable = gvotable_election_get_handle("USB_ICL");
	if (!icl_votable)
		return -EPROBE_DEFER;

	fake_psy_online_votable =
		gvotable_election_get_handle("SHIM_FAKE_OLN");

	if (!fake_psy_online_votable) {
		return -EPROBE_DEFER;
	}

	/*
	 * Check that the power supply registered by qpnp-smblite.c exists.
	 * If it does, then it's likely that we have an smblite_shim.
	 */
	psy = dt_find_psy(&pdev->dev, "google,qcom-psy");
	if (!psy) {
		dev_err(&pdev->dev, "Could not find qcom's psy\n");
		return -EPROBE_DEFER;
	}
	power_supply_put(psy);

	psy = dt_find_psy(&pdev->dev, "google,shim-psy");
	if (!psy) {
		dev_err(&pdev->dev, "Could not find shim psy\n");
		return -EPROBE_DEFER;
	}

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

	platform_set_drvdata(pdev, hvdcp);
	hvdcp->dev = &pdev->dev;

	hvdcp->usb_icl_votable = icl_votable;
	hvdcp->fake_psy_online_votable = fake_psy_online_votable;
	hvdcp->shim_psy = psy;
	hvdcp->smblite_shim = power_supply_get_drvdata(hvdcp->shim_psy);

	mutex_init(&hvdcp->lock);

	INIT_WORK(&hvdcp->controller_work, controller_work);
	alarm_init(&hvdcp->controller_alarm, ALARM_BOOTTIME,
		controller_alarm_expired);

	hvdcp->cmd_waits[CMD_IDLE].typ = 100;
	hvdcp->cmd_waits[CMD_IDLE].slow = 2500;

	hvdcp->cmd_waits[CMD_FORCE_5V].typ = 150;
	hvdcp->cmd_waits[CMD_FORCE_5V].slow = 2500;

	hvdcp->cmd_waits[CMD_INCREMENT].typ = 150;
	hvdcp->cmd_waits[CMD_INCREMENT].slow = 2500;

	hvdcp->cmd_waits[CMD_DECREMENT].typ = 150;
	hvdcp->cmd_waits[CMD_DECREMENT].slow = 2500;

	hvdcp->renegotiation_wait_ms = 2500;
	hvdcp->cmd_retry_wait_ms = 2500;
	hvdcp->settle_monitor_ms = 6000;

	hvdcp->voltage_ceiling = 5500000;

	reset_state_locked(hvdcp);

	hvdcp->plugin_nb.notifier_call = plugin_notify;
	ret = smblite_shim_plugin_register_notifier(hvdcp->smblite_shim,
						&hvdcp->plugin_nb);
	if (ret != 0) {
		dev_err(hvdcp->dev, "Couldn't register plugin notifier (%d)\n",
			ret);
		goto fail;
	}

	hvdcp->hvdcp_req_nb.notifier_call = hvdcp_req_notify;
	ret = smblite_shim_hvdcp_req_register_notifier(hvdcp->smblite_shim,
						&hvdcp->hvdcp_req_nb);
	if (ret != 0) {
		dev_err(hvdcp->dev, "Couldn't register hvdcp notifier (%d)\n",
			ret);
		goto fail_hvdcp_nb;
	}

	setup_sysfs(hvdcp);

	return 0;

fail_hvdcp_nb:
	smblite_shim_plugin_unregister_notifier(hvdcp->smblite_shim,
						&hvdcp->plugin_nb);
fail:
	return ret;
}

static int hvdcp_remove(struct platform_device *pdev)
{
	struct hvdcp *hvdcp = platform_get_drvdata(pdev);
	teardown_sysfs(hvdcp);
	smblite_shim_hvdcp_req_unregister_notifier(hvdcp->smblite_shim,
						&hvdcp->hvdcp_req_nb);
	smblite_shim_plugin_unregister_notifier(hvdcp->smblite_shim,
						&hvdcp->plugin_nb);
	alarm_try_to_cancel(&hvdcp->controller_alarm);
	power_supply_put(hvdcp->shim_psy);
	platform_set_drvdata(pdev, NULL);
	return 0;
}

static const struct of_device_id match_table[] = {
	{ .compatible = "google,smblite-hvdcp" },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, match_table);

static struct platform_driver hvdcp_driver = {
	.driver	= {
		.name = "google-smblite-hvdcp",
		.of_match_table	= of_match_ptr(match_table),
		.pm = NULL,
	},
	.probe		= hvdcp_probe,
	.remove		= hvdcp_remove,
};
module_platform_driver(hvdcp_driver);

MODULE_DESCRIPTION("QC 3.0 (HVDCP) controller");
MODULE_LICENSE("GPL v2");