1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * tusb1210.c - TUSB1210 USB ULPI PHY driver
4  *
5  * Copyright (C) 2015 Intel Corporation
6  *
7  * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
8  */
9 #include <linux/module.h>
10 #include <linux/bitfield.h>
11 #include <linux/delay.h>
12 #include <linux/ulpi/driver.h>
13 #include <linux/ulpi/regs.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/phy/ulpi_phy.h>
16 #include <linux/power_supply.h>
17 #include <linux/workqueue.h>
18 
19 #define TUSB1211_POWER_CONTROL				0x3d
20 #define TUSB1211_POWER_CONTROL_SET			0x3e
21 #define TUSB1211_POWER_CONTROL_CLEAR			0x3f
22 #define TUSB1211_POWER_CONTROL_SW_CONTROL		BIT(0)
23 #define TUSB1211_POWER_CONTROL_DET_COMP			BIT(1)
24 #define TUSB1211_POWER_CONTROL_DP_VSRC_EN		BIT(6)
25 
26 #define TUSB1210_VENDOR_SPECIFIC2			0x80
27 #define TUSB1210_VENDOR_SPECIFIC2_IHSTX_MASK		GENMASK(3, 0)
28 #define TUSB1210_VENDOR_SPECIFIC2_ZHSDRV_MASK		GENMASK(5, 4)
29 #define TUSB1210_VENDOR_SPECIFIC2_DP_MASK		BIT(6)
30 
31 #define TUSB1211_VENDOR_SPECIFIC3			0x85
32 #define TUSB1211_VENDOR_SPECIFIC3_SET			0x86
33 #define TUSB1211_VENDOR_SPECIFIC3_CLEAR			0x87
34 #define TUSB1211_VENDOR_SPECIFIC3_SW_USB_DET		BIT(4)
35 #define TUSB1211_VENDOR_SPECIFIC3_CHGD_IDP_SRC_EN	BIT(6)
36 
37 #define TUSB1210_RESET_TIME_MS				50
38 
39 #define TUSB1210_CHG_DET_MAX_RETRIES			5
40 
41 /* TUSB1210 charger detection work states */
42 enum tusb1210_chg_det_state {
43 	TUSB1210_CHG_DET_CONNECTING,
44 	TUSB1210_CHG_DET_START_DET,
45 	TUSB1210_CHG_DET_READ_DET,
46 	TUSB1210_CHG_DET_FINISH_DET,
47 	TUSB1210_CHG_DET_CONNECTED,
48 	TUSB1210_CHG_DET_DISCONNECTING,
49 	TUSB1210_CHG_DET_DISCONNECTING_DONE,
50 	TUSB1210_CHG_DET_DISCONNECTED,
51 };
52 
53 struct tusb1210 {
54 	struct ulpi *ulpi;
55 	struct phy *phy;
56 	struct gpio_desc *gpio_reset;
57 	struct gpio_desc *gpio_cs;
58 	u8 otg_ctrl;
59 	u8 vendor_specific2;
60 #ifdef CONFIG_POWER_SUPPLY
61 	enum power_supply_usb_type chg_type;
62 	enum tusb1210_chg_det_state chg_det_state;
63 	int chg_det_retries;
64 	struct delayed_work chg_det_work;
65 	struct notifier_block psy_nb;
66 	struct power_supply *psy;
67 	struct power_supply *charger;
68 #endif
69 };
70 
tusb1210_ulpi_write(struct tusb1210 * tusb,u8 reg,u8 val)71 static int tusb1210_ulpi_write(struct tusb1210 *tusb, u8 reg, u8 val)
72 {
73 	int ret;
74 
75 	ret = ulpi_write(tusb->ulpi, reg, val);
76 	if (ret)
77 		dev_err(&tusb->ulpi->dev, "error %d writing val 0x%02x to reg 0x%02x\n",
78 			ret, val, reg);
79 
80 	return ret;
81 }
82 
tusb1210_ulpi_read(struct tusb1210 * tusb,u8 reg,u8 * val)83 static int tusb1210_ulpi_read(struct tusb1210 *tusb, u8 reg, u8 *val)
84 {
85 	int ret;
86 
87 	ret = ulpi_read(tusb->ulpi, reg);
88 	if (ret >= 0) {
89 		*val = ret;
90 		ret = 0;
91 	} else {
92 		dev_err(&tusb->ulpi->dev, "error %d reading reg 0x%02x\n", ret, reg);
93 	}
94 
95 	return ret;
96 }
97 
tusb1210_power_on(struct phy * phy)98 static int tusb1210_power_on(struct phy *phy)
99 {
100 	struct tusb1210 *tusb = phy_get_drvdata(phy);
101 
102 	gpiod_set_value_cansleep(tusb->gpio_reset, 1);
103 	gpiod_set_value_cansleep(tusb->gpio_cs, 1);
104 
105 	msleep(TUSB1210_RESET_TIME_MS);
106 
107 	/* Restore the optional eye diagram optimization value */
108 	tusb1210_ulpi_write(tusb, TUSB1210_VENDOR_SPECIFIC2, tusb->vendor_specific2);
109 
110 	return 0;
111 }
112 
tusb1210_power_off(struct phy * phy)113 static int tusb1210_power_off(struct phy *phy)
114 {
115 	struct tusb1210 *tusb = phy_get_drvdata(phy);
116 
117 	gpiod_set_value_cansleep(tusb->gpio_reset, 0);
118 	gpiod_set_value_cansleep(tusb->gpio_cs, 0);
119 
120 	return 0;
121 }
122 
tusb1210_set_mode(struct phy * phy,enum phy_mode mode,int submode)123 static int tusb1210_set_mode(struct phy *phy, enum phy_mode mode, int submode)
124 {
125 	struct tusb1210 *tusb = phy_get_drvdata(phy);
126 	int ret;
127 	u8 reg;
128 
129 	ret = tusb1210_ulpi_read(tusb, ULPI_OTG_CTRL, &reg);
130 	if (ret < 0)
131 		return ret;
132 
133 	switch (mode) {
134 	case PHY_MODE_USB_HOST:
135 		reg |= (ULPI_OTG_CTRL_DRVVBUS_EXT
136 			| ULPI_OTG_CTRL_ID_PULLUP
137 			| ULPI_OTG_CTRL_DP_PULLDOWN
138 			| ULPI_OTG_CTRL_DM_PULLDOWN);
139 		tusb1210_ulpi_write(tusb, ULPI_OTG_CTRL, reg);
140 		reg |= ULPI_OTG_CTRL_DRVVBUS;
141 		break;
142 	case PHY_MODE_USB_DEVICE:
143 		reg &= ~(ULPI_OTG_CTRL_DRVVBUS
144 			 | ULPI_OTG_CTRL_DP_PULLDOWN
145 			 | ULPI_OTG_CTRL_DM_PULLDOWN);
146 		tusb1210_ulpi_write(tusb, ULPI_OTG_CTRL, reg);
147 		reg &= ~ULPI_OTG_CTRL_DRVVBUS_EXT;
148 		break;
149 	default:
150 		/* nothing */
151 		return 0;
152 	}
153 
154 	tusb->otg_ctrl = reg;
155 	return tusb1210_ulpi_write(tusb, ULPI_OTG_CTRL, reg);
156 }
157 
158 #ifdef CONFIG_POWER_SUPPLY
159 static const char * const tusb1210_chg_det_states[] = {
160 	"CHG_DET_CONNECTING",
161 	"CHG_DET_START_DET",
162 	"CHG_DET_READ_DET",
163 	"CHG_DET_FINISH_DET",
164 	"CHG_DET_CONNECTED",
165 	"CHG_DET_DISCONNECTING",
166 	"CHG_DET_DISCONNECTING_DONE",
167 	"CHG_DET_DISCONNECTED",
168 };
169 
tusb1210_reset(struct tusb1210 * tusb)170 static void tusb1210_reset(struct tusb1210 *tusb)
171 {
172 	gpiod_set_value_cansleep(tusb->gpio_reset, 0);
173 	usleep_range(200, 500);
174 	gpiod_set_value_cansleep(tusb->gpio_reset, 1);
175 }
176 
tusb1210_chg_det_set_type(struct tusb1210 * tusb,enum power_supply_usb_type type)177 static void tusb1210_chg_det_set_type(struct tusb1210 *tusb,
178 				      enum power_supply_usb_type type)
179 {
180 	dev_dbg(&tusb->ulpi->dev, "charger type: %d\n", type);
181 	tusb->chg_type = type;
182 	tusb->chg_det_retries = 0;
183 	power_supply_changed(tusb->psy);
184 }
185 
tusb1210_chg_det_set_state(struct tusb1210 * tusb,enum tusb1210_chg_det_state new_state,int delay_ms)186 static void tusb1210_chg_det_set_state(struct tusb1210 *tusb,
187 				       enum tusb1210_chg_det_state new_state,
188 				       int delay_ms)
189 {
190 	if (delay_ms)
191 		dev_dbg(&tusb->ulpi->dev, "chg_det new state %s in %d ms\n",
192 			tusb1210_chg_det_states[new_state], delay_ms);
193 
194 	tusb->chg_det_state = new_state;
195 	mod_delayed_work(system_long_wq, &tusb->chg_det_work,
196 			 msecs_to_jiffies(delay_ms));
197 }
198 
tusb1210_chg_det_handle_ulpi_error(struct tusb1210 * tusb)199 static void tusb1210_chg_det_handle_ulpi_error(struct tusb1210 *tusb)
200 {
201 	tusb1210_reset(tusb);
202 	if (tusb->chg_det_retries < TUSB1210_CHG_DET_MAX_RETRIES) {
203 		tusb->chg_det_retries++;
204 		tusb1210_chg_det_set_state(tusb, TUSB1210_CHG_DET_START_DET,
205 					   TUSB1210_RESET_TIME_MS);
206 	} else {
207 		tusb1210_chg_det_set_state(tusb, TUSB1210_CHG_DET_FINISH_DET,
208 					   TUSB1210_RESET_TIME_MS);
209 	}
210 }
211 
212 /*
213  * Boards using a TUSB121x for charger-detection have 3 power_supply class devs:
214  *
215  * tusb1211-charger-detect(1) -> charger -> fuel-gauge
216  *
217  * To determine if an USB charger is connected to the board, the online prop of
218  * the charger psy needs to be read. Since the tusb1211-charger-detect psy is
219  * the start of the supplier -> supplied-to chain, power_supply_am_i_supplied()
220  * cannot be used here.
221  *
222  * Instead, below is a list of the power_supply names of known chargers for
223  * these boards and the charger psy is looked up by name from this list.
224  *
225  * (1) modelling the external USB charger
226  */
227 static const char * const tusb1210_chargers[] = {
228 	"bq24190-charger",
229 };
230 
tusb1210_get_online(struct tusb1210 * tusb)231 static bool tusb1210_get_online(struct tusb1210 *tusb)
232 {
233 	union power_supply_propval val;
234 	int i;
235 
236 	for (i = 0; i < ARRAY_SIZE(tusb1210_chargers) && !tusb->charger; i++)
237 		tusb->charger = power_supply_get_by_name(tusb1210_chargers[i]);
238 
239 	if (!tusb->charger)
240 		return false;
241 
242 	if (power_supply_get_property(tusb->charger, POWER_SUPPLY_PROP_ONLINE, &val))
243 		return false;
244 
245 	return val.intval;
246 }
247 
tusb1210_chg_det_work(struct work_struct * work)248 static void tusb1210_chg_det_work(struct work_struct *work)
249 {
250 	struct tusb1210 *tusb = container_of(work, struct tusb1210, chg_det_work.work);
251 	bool vbus_present = tusb1210_get_online(tusb);
252 	int ret;
253 	u8 val;
254 
255 	dev_dbg(&tusb->ulpi->dev, "chg_det state %s vbus_present %d\n",
256 		tusb1210_chg_det_states[tusb->chg_det_state], vbus_present);
257 
258 	switch (tusb->chg_det_state) {
259 	case TUSB1210_CHG_DET_CONNECTING:
260 		tusb->chg_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
261 		tusb->chg_det_retries = 0;
262 		/* Power on USB controller for ulpi_read()/_write() */
263 		ret = pm_runtime_resume_and_get(tusb->ulpi->dev.parent);
264 		if (ret < 0) {
265 			dev_err(&tusb->ulpi->dev, "error %d runtime-resuming\n", ret);
266 			/* Should never happen, skip charger detection */
267 			tusb1210_chg_det_set_state(tusb, TUSB1210_CHG_DET_CONNECTED, 0);
268 			return;
269 		}
270 		tusb1210_chg_det_set_state(tusb, TUSB1210_CHG_DET_START_DET, 0);
271 		break;
272 	case TUSB1210_CHG_DET_START_DET:
273 		/*
274 		 * Use the builtin charger detection FSM to keep things simple.
275 		 * This only detects DCP / SDP. This is good enough for the few
276 		 * boards which actually rely on the phy for charger detection.
277 		 */
278 		mutex_lock(&tusb->phy->mutex);
279 		ret = tusb1210_ulpi_write(tusb, TUSB1211_VENDOR_SPECIFIC3_SET,
280 					  TUSB1211_VENDOR_SPECIFIC3_SW_USB_DET);
281 		mutex_unlock(&tusb->phy->mutex);
282 		if (ret) {
283 			tusb1210_chg_det_handle_ulpi_error(tusb);
284 			break;
285 		}
286 
287 		/* Wait 400 ms for the charger detection FSM to finish */
288 		tusb1210_chg_det_set_state(tusb, TUSB1210_CHG_DET_READ_DET, 400);
289 		break;
290 	case TUSB1210_CHG_DET_READ_DET:
291 		mutex_lock(&tusb->phy->mutex);
292 		ret = tusb1210_ulpi_read(tusb, TUSB1211_POWER_CONTROL, &val);
293 		mutex_unlock(&tusb->phy->mutex);
294 		if (ret) {
295 			tusb1210_chg_det_handle_ulpi_error(tusb);
296 			break;
297 		}
298 
299 		if (val & TUSB1211_POWER_CONTROL_DET_COMP)
300 			tusb1210_chg_det_set_type(tusb, POWER_SUPPLY_USB_TYPE_DCP);
301 		else
302 			tusb1210_chg_det_set_type(tusb, POWER_SUPPLY_USB_TYPE_SDP);
303 
304 		tusb1210_chg_det_set_state(tusb, TUSB1210_CHG_DET_FINISH_DET, 0);
305 		break;
306 	case TUSB1210_CHG_DET_FINISH_DET:
307 		mutex_lock(&tusb->phy->mutex);
308 
309 		/* Set SW_CONTROL to stop the charger-det FSM */
310 		ret = tusb1210_ulpi_write(tusb, TUSB1211_POWER_CONTROL_SET,
311 					  TUSB1211_POWER_CONTROL_SW_CONTROL);
312 
313 		/* Clear DP_VSRC_EN which may have been enabled by the charger-det FSM */
314 		ret |= tusb1210_ulpi_write(tusb, TUSB1211_POWER_CONTROL_CLEAR,
315 					   TUSB1211_POWER_CONTROL_DP_VSRC_EN);
316 
317 		/* Clear CHGD_IDP_SRC_EN (may have been enabled by the charger-det FSM) */
318 		ret |= tusb1210_ulpi_write(tusb, TUSB1211_VENDOR_SPECIFIC3_CLEAR,
319 					   TUSB1211_VENDOR_SPECIFIC3_CHGD_IDP_SRC_EN);
320 
321 		/* If any of the above fails reset the phy */
322 		if (ret) {
323 			tusb1210_reset(tusb);
324 			msleep(TUSB1210_RESET_TIME_MS);
325 		}
326 
327 		/* Restore phy-parameters and OTG_CTRL register */
328 		tusb1210_ulpi_write(tusb, ULPI_OTG_CTRL, tusb->otg_ctrl);
329 		tusb1210_ulpi_write(tusb, TUSB1210_VENDOR_SPECIFIC2,
330 				    tusb->vendor_specific2);
331 
332 		mutex_unlock(&tusb->phy->mutex);
333 
334 		pm_runtime_put(tusb->ulpi->dev.parent);
335 		tusb1210_chg_det_set_state(tusb, TUSB1210_CHG_DET_CONNECTED, 0);
336 		break;
337 	case TUSB1210_CHG_DET_CONNECTED:
338 		if (!vbus_present)
339 			tusb1210_chg_det_set_state(tusb, TUSB1210_CHG_DET_DISCONNECTING, 0);
340 		break;
341 	case TUSB1210_CHG_DET_DISCONNECTING:
342 		/*
343 		 * The phy seems to take approx. 600ms longer then the charger
344 		 * chip (which is used to get vbus_present) to determine Vbus
345 		 * session end. Wait 800ms to ensure the phy has detected and
346 		 * signalled Vbus session end.
347 		 */
348 		tusb1210_chg_det_set_state(tusb, TUSB1210_CHG_DET_DISCONNECTING_DONE, 800);
349 		break;
350 	case TUSB1210_CHG_DET_DISCONNECTING_DONE:
351 		/*
352 		 * The phy often stops reacting to ulpi_read()/_write requests
353 		 * after a Vbus-session end. Reset it to work around this.
354 		 */
355 		tusb1210_reset(tusb);
356 		tusb1210_chg_det_set_type(tusb, POWER_SUPPLY_USB_TYPE_UNKNOWN);
357 		tusb1210_chg_det_set_state(tusb, TUSB1210_CHG_DET_DISCONNECTED, 0);
358 		break;
359 	case TUSB1210_CHG_DET_DISCONNECTED:
360 		if (vbus_present)
361 			tusb1210_chg_det_set_state(tusb, TUSB1210_CHG_DET_CONNECTING, 0);
362 		break;
363 	}
364 }
365 
tusb1210_psy_notifier(struct notifier_block * nb,unsigned long event,void * ptr)366 static int tusb1210_psy_notifier(struct notifier_block *nb,
367 	unsigned long event, void *ptr)
368 {
369 	struct tusb1210 *tusb = container_of(nb, struct tusb1210, psy_nb);
370 	struct power_supply *psy = ptr;
371 
372 	if (psy != tusb->psy && psy->desc->type == POWER_SUPPLY_TYPE_USB)
373 		queue_delayed_work(system_long_wq, &tusb->chg_det_work, 0);
374 
375 	return NOTIFY_OK;
376 }
377 
tusb1210_psy_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)378 static int tusb1210_psy_get_prop(struct power_supply *psy,
379 				 enum power_supply_property psp,
380 				 union power_supply_propval *val)
381 {
382 	struct tusb1210 *tusb = power_supply_get_drvdata(psy);
383 
384 	switch (psp) {
385 	case POWER_SUPPLY_PROP_ONLINE:
386 		val->intval = tusb1210_get_online(tusb);
387 		break;
388 	case POWER_SUPPLY_PROP_USB_TYPE:
389 		val->intval = tusb->chg_type;
390 		break;
391 	case POWER_SUPPLY_PROP_CURRENT_MAX:
392 		if (tusb->chg_type == POWER_SUPPLY_USB_TYPE_DCP)
393 			val->intval = 2000000;
394 		else
395 			val->intval = 500000;
396 		break;
397 	default:
398 		return -EINVAL;
399 	}
400 
401 	return 0;
402 }
403 
404 static const enum power_supply_usb_type tusb1210_psy_usb_types[] = {
405 	POWER_SUPPLY_USB_TYPE_SDP,
406 	POWER_SUPPLY_USB_TYPE_DCP,
407 	POWER_SUPPLY_USB_TYPE_UNKNOWN,
408 };
409 
410 static const enum power_supply_property tusb1210_psy_props[] = {
411 	POWER_SUPPLY_PROP_ONLINE,
412 	POWER_SUPPLY_PROP_USB_TYPE,
413 	POWER_SUPPLY_PROP_CURRENT_MAX,
414 };
415 
416 static const struct power_supply_desc tusb1210_psy_desc = {
417 	.name = "tusb1211-charger-detect",
418 	.type = POWER_SUPPLY_TYPE_USB,
419 	.usb_types = tusb1210_psy_usb_types,
420 	.num_usb_types = ARRAY_SIZE(tusb1210_psy_usb_types),
421 	.properties = tusb1210_psy_props,
422 	.num_properties = ARRAY_SIZE(tusb1210_psy_props),
423 	.get_property = tusb1210_psy_get_prop,
424 };
425 
426 /* Setup charger detection if requested, on errors continue without chg-det */
tusb1210_probe_charger_detect(struct tusb1210 * tusb)427 static void tusb1210_probe_charger_detect(struct tusb1210 *tusb)
428 {
429 	struct power_supply_config psy_cfg = { .drv_data = tusb };
430 	struct device *dev = &tusb->ulpi->dev;
431 	int ret;
432 
433 	if (!device_property_read_bool(dev->parent, "linux,phy_charger_detect"))
434 		return;
435 
436 	if (tusb->ulpi->id.product != 0x1508) {
437 		dev_err(dev, "error charger detection is only supported on the TUSB1211\n");
438 		return;
439 	}
440 
441 	ret = tusb1210_ulpi_read(tusb, ULPI_OTG_CTRL, &tusb->otg_ctrl);
442 	if (ret)
443 		return;
444 
445 	tusb->psy = power_supply_register(dev, &tusb1210_psy_desc, &psy_cfg);
446 	if (IS_ERR(tusb->psy))
447 		return;
448 
449 	/*
450 	 * Delay initial run by 2 seconds to allow the charger driver,
451 	 * which is used to determine vbus_present, to load.
452 	 */
453 	tusb->chg_det_state = TUSB1210_CHG_DET_DISCONNECTED;
454 	INIT_DELAYED_WORK(&tusb->chg_det_work, tusb1210_chg_det_work);
455 	queue_delayed_work(system_long_wq, &tusb->chg_det_work, 2 * HZ);
456 
457 	tusb->psy_nb.notifier_call = tusb1210_psy_notifier;
458 	power_supply_reg_notifier(&tusb->psy_nb);
459 }
460 
tusb1210_remove_charger_detect(struct tusb1210 * tusb)461 static void tusb1210_remove_charger_detect(struct tusb1210 *tusb)
462 {
463 
464 	if (!IS_ERR_OR_NULL(tusb->psy)) {
465 		power_supply_unreg_notifier(&tusb->psy_nb);
466 		cancel_delayed_work_sync(&tusb->chg_det_work);
467 		power_supply_unregister(tusb->psy);
468 	}
469 
470 	if (tusb->charger)
471 		power_supply_put(tusb->charger);
472 }
473 #else
tusb1210_probe_charger_detect(struct tusb1210 * tusb)474 static void tusb1210_probe_charger_detect(struct tusb1210 *tusb) { }
tusb1210_remove_charger_detect(struct tusb1210 * tusb)475 static void tusb1210_remove_charger_detect(struct tusb1210 *tusb) { }
476 #endif
477 
478 static const struct phy_ops phy_ops = {
479 	.power_on = tusb1210_power_on,
480 	.power_off = tusb1210_power_off,
481 	.set_mode = tusb1210_set_mode,
482 	.owner = THIS_MODULE,
483 };
484 
tusb1210_probe(struct ulpi * ulpi)485 static int tusb1210_probe(struct ulpi *ulpi)
486 {
487 	struct tusb1210 *tusb;
488 	u8 val, reg;
489 	int ret;
490 
491 	tusb = devm_kzalloc(&ulpi->dev, sizeof(*tusb), GFP_KERNEL);
492 	if (!tusb)
493 		return -ENOMEM;
494 
495 	tusb->ulpi = ulpi;
496 
497 	tusb->gpio_reset = devm_gpiod_get_optional(&ulpi->dev, "reset",
498 						   GPIOD_OUT_LOW);
499 	if (IS_ERR(tusb->gpio_reset))
500 		return PTR_ERR(tusb->gpio_reset);
501 
502 	gpiod_set_value_cansleep(tusb->gpio_reset, 1);
503 
504 	tusb->gpio_cs = devm_gpiod_get_optional(&ulpi->dev, "cs",
505 						GPIOD_OUT_LOW);
506 	if (IS_ERR(tusb->gpio_cs))
507 		return PTR_ERR(tusb->gpio_cs);
508 
509 	gpiod_set_value_cansleep(tusb->gpio_cs, 1);
510 
511 	/*
512 	 * VENDOR_SPECIFIC2 register in TUSB1210 can be used for configuring eye
513 	 * diagram optimization and DP/DM swap.
514 	 */
515 
516 	ret = tusb1210_ulpi_read(tusb, TUSB1210_VENDOR_SPECIFIC2, &reg);
517 	if (ret)
518 		return ret;
519 
520 	/* High speed output drive strength configuration */
521 	if (!device_property_read_u8(&ulpi->dev, "ihstx", &val))
522 		u8p_replace_bits(&reg, val, (u8)TUSB1210_VENDOR_SPECIFIC2_IHSTX_MASK);
523 
524 	/* High speed output impedance configuration */
525 	if (!device_property_read_u8(&ulpi->dev, "zhsdrv", &val))
526 		u8p_replace_bits(&reg, val, (u8)TUSB1210_VENDOR_SPECIFIC2_ZHSDRV_MASK);
527 
528 	/* DP/DM swap control */
529 	if (!device_property_read_u8(&ulpi->dev, "datapolarity", &val))
530 		u8p_replace_bits(&reg, val, (u8)TUSB1210_VENDOR_SPECIFIC2_DP_MASK);
531 
532 	ret = tusb1210_ulpi_write(tusb, TUSB1210_VENDOR_SPECIFIC2, reg);
533 	if (ret)
534 		return ret;
535 
536 	tusb->vendor_specific2 = reg;
537 
538 	tusb1210_probe_charger_detect(tusb);
539 
540 	tusb->phy = ulpi_phy_create(ulpi, &phy_ops);
541 	if (IS_ERR(tusb->phy)) {
542 		ret = PTR_ERR(tusb->phy);
543 		goto err_remove_charger;
544 	}
545 
546 	phy_set_drvdata(tusb->phy, tusb);
547 	ulpi_set_drvdata(ulpi, tusb);
548 	return 0;
549 
550 err_remove_charger:
551 	tusb1210_remove_charger_detect(tusb);
552 	return ret;
553 }
554 
tusb1210_remove(struct ulpi * ulpi)555 static void tusb1210_remove(struct ulpi *ulpi)
556 {
557 	struct tusb1210 *tusb = ulpi_get_drvdata(ulpi);
558 
559 	ulpi_phy_destroy(ulpi, tusb->phy);
560 	tusb1210_remove_charger_detect(tusb);
561 }
562 
563 #define TI_VENDOR_ID 0x0451
564 
565 static const struct ulpi_device_id tusb1210_ulpi_id[] = {
566 	{ TI_VENDOR_ID, 0x1507, },  /* TUSB1210 */
567 	{ TI_VENDOR_ID, 0x1508, },  /* TUSB1211 */
568 	{ },
569 };
570 MODULE_DEVICE_TABLE(ulpi, tusb1210_ulpi_id);
571 
572 static struct ulpi_driver tusb1210_driver = {
573 	.id_table = tusb1210_ulpi_id,
574 	.probe = tusb1210_probe,
575 	.remove = tusb1210_remove,
576 	.driver = {
577 		.name = "tusb1210",
578 		.owner = THIS_MODULE,
579 	},
580 };
581 
582 module_ulpi_driver(tusb1210_driver);
583 
584 MODULE_AUTHOR("Intel Corporation");
585 MODULE_LICENSE("GPL v2");
586 MODULE_DESCRIPTION("TUSB1210 ULPI PHY driver");
587