1 // SPDX-License-Identifier: GPL-2.0-only
3 * tusb1210.c - TUSB1210 USB ULPI PHY driver
5 * Copyright (C) 2015 Intel Corporation
7 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
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/property.h>
18 #include <linux/workqueue.h>
20 #define TI_VENDOR_ID 0x0451
21 #define TI_DEVICE_TUSB1210 0x1507
22 #define TI_DEVICE_TUSB1211 0x1508
24 #define TUSB1211_POWER_CONTROL 0x3d
25 #define TUSB1211_POWER_CONTROL_SET 0x3e
26 #define TUSB1211_POWER_CONTROL_CLEAR 0x3f
27 #define TUSB1211_POWER_CONTROL_SW_CONTROL BIT(0)
28 #define TUSB1211_POWER_CONTROL_DET_COMP BIT(1)
29 #define TUSB1211_POWER_CONTROL_DP_VSRC_EN BIT(6)
31 #define TUSB1210_VENDOR_SPECIFIC2 0x80
32 #define TUSB1210_VENDOR_SPECIFIC2_IHSTX_MASK GENMASK(3, 0)
33 #define TUSB1210_VENDOR_SPECIFIC2_ZHSDRV_MASK GENMASK(5, 4)
34 #define TUSB1210_VENDOR_SPECIFIC2_DP_MASK BIT(6)
36 #define TUSB1211_VENDOR_SPECIFIC3 0x85
37 #define TUSB1211_VENDOR_SPECIFIC3_SET 0x86
38 #define TUSB1211_VENDOR_SPECIFIC3_CLEAR 0x87
39 #define TUSB1211_VENDOR_SPECIFIC3_SW_USB_DET BIT(4)
40 #define TUSB1211_VENDOR_SPECIFIC3_CHGD_IDP_SRC_EN BIT(6)
42 #define TUSB1210_RESET_TIME_MS 50
44 #define TUSB1210_CHG_DET_MAX_RETRIES 5
46 /* TUSB1210 charger detection work states */
47 enum tusb1210_chg_det_state
{
48 TUSB1210_CHG_DET_CONNECTING
,
49 TUSB1210_CHG_DET_START_DET
,
50 TUSB1210_CHG_DET_READ_DET
,
51 TUSB1210_CHG_DET_FINISH_DET
,
52 TUSB1210_CHG_DET_CONNECTED
,
53 TUSB1210_CHG_DET_DISCONNECTING
,
54 TUSB1210_CHG_DET_DISCONNECTING_DONE
,
55 TUSB1210_CHG_DET_DISCONNECTED
,
61 struct gpio_desc
*gpio_reset
;
62 struct gpio_desc
*gpio_cs
;
65 #ifdef CONFIG_POWER_SUPPLY
66 enum power_supply_usb_type chg_type
;
67 enum tusb1210_chg_det_state chg_det_state
;
69 struct delayed_work chg_det_work
;
70 struct notifier_block psy_nb
;
71 struct power_supply
*psy
;
75 static int tusb1210_ulpi_write(struct tusb1210
*tusb
, u8 reg
, u8 val
)
77 struct device
*dev
= tusb
->dev
;
80 ret
= ulpi_write(to_ulpi_dev(dev
), reg
, val
);
82 dev_err(dev
, "error %d writing val 0x%02x to reg 0x%02x\n", ret
, val
, reg
);
87 static int tusb1210_ulpi_read(struct tusb1210
*tusb
, u8 reg
, u8
*val
)
89 struct device
*dev
= tusb
->dev
;
92 ret
= ulpi_read(to_ulpi_dev(dev
), reg
);
97 dev_err(dev
, "error %d reading reg 0x%02x\n", ret
, reg
);
103 static int tusb1210_power_on(struct phy
*phy
)
105 struct tusb1210
*tusb
= phy_get_drvdata(phy
);
107 gpiod_set_value_cansleep(tusb
->gpio_reset
, 1);
108 gpiod_set_value_cansleep(tusb
->gpio_cs
, 1);
110 msleep(TUSB1210_RESET_TIME_MS
);
112 /* Restore the optional eye diagram optimization value */
113 tusb1210_ulpi_write(tusb
, TUSB1210_VENDOR_SPECIFIC2
, tusb
->vendor_specific2
);
118 static int tusb1210_power_off(struct phy
*phy
)
120 struct tusb1210
*tusb
= phy_get_drvdata(phy
);
122 gpiod_set_value_cansleep(tusb
->gpio_reset
, 0);
123 gpiod_set_value_cansleep(tusb
->gpio_cs
, 0);
128 static int tusb1210_set_mode(struct phy
*phy
, enum phy_mode mode
, int submode
)
130 struct tusb1210
*tusb
= phy_get_drvdata(phy
);
134 ret
= tusb1210_ulpi_read(tusb
, ULPI_OTG_CTRL
, ®
);
139 case PHY_MODE_USB_HOST
:
140 reg
|= (ULPI_OTG_CTRL_DRVVBUS_EXT
141 | ULPI_OTG_CTRL_ID_PULLUP
142 | ULPI_OTG_CTRL_DP_PULLDOWN
143 | ULPI_OTG_CTRL_DM_PULLDOWN
);
144 tusb1210_ulpi_write(tusb
, ULPI_OTG_CTRL
, reg
);
145 reg
|= ULPI_OTG_CTRL_DRVVBUS
;
147 case PHY_MODE_USB_DEVICE
:
148 reg
&= ~(ULPI_OTG_CTRL_DRVVBUS
149 | ULPI_OTG_CTRL_DP_PULLDOWN
150 | ULPI_OTG_CTRL_DM_PULLDOWN
);
151 tusb1210_ulpi_write(tusb
, ULPI_OTG_CTRL
, reg
);
152 reg
&= ~ULPI_OTG_CTRL_DRVVBUS_EXT
;
159 tusb
->otg_ctrl
= reg
;
160 return tusb1210_ulpi_write(tusb
, ULPI_OTG_CTRL
, reg
);
163 #ifdef CONFIG_POWER_SUPPLY
164 static const char * const tusb1210_chg_det_states
[] = {
165 "CHG_DET_CONNECTING",
168 "CHG_DET_FINISH_DET",
170 "CHG_DET_DISCONNECTING",
171 "CHG_DET_DISCONNECTING_DONE",
172 "CHG_DET_DISCONNECTED",
175 static void tusb1210_reset(struct tusb1210
*tusb
)
177 gpiod_set_value_cansleep(tusb
->gpio_reset
, 0);
178 usleep_range(200, 500);
179 gpiod_set_value_cansleep(tusb
->gpio_reset
, 1);
182 static void tusb1210_chg_det_set_type(struct tusb1210
*tusb
,
183 enum power_supply_usb_type type
)
185 dev_dbg(tusb
->dev
, "charger type: %d\n", type
);
186 tusb
->chg_type
= type
;
187 tusb
->chg_det_retries
= 0;
188 power_supply_changed(tusb
->psy
);
191 static void tusb1210_chg_det_set_state(struct tusb1210
*tusb
,
192 enum tusb1210_chg_det_state new_state
,
196 dev_dbg(tusb
->dev
, "chg_det new state %s in %d ms\n",
197 tusb1210_chg_det_states
[new_state
], delay_ms
);
199 tusb
->chg_det_state
= new_state
;
200 mod_delayed_work(system_long_wq
, &tusb
->chg_det_work
,
201 msecs_to_jiffies(delay_ms
));
204 static void tusb1210_chg_det_handle_ulpi_error(struct tusb1210
*tusb
)
206 tusb1210_reset(tusb
);
207 if (tusb
->chg_det_retries
< TUSB1210_CHG_DET_MAX_RETRIES
) {
208 tusb
->chg_det_retries
++;
209 tusb1210_chg_det_set_state(tusb
, TUSB1210_CHG_DET_START_DET
,
210 TUSB1210_RESET_TIME_MS
);
212 tusb1210_chg_det_set_state(tusb
, TUSB1210_CHG_DET_FINISH_DET
,
213 TUSB1210_RESET_TIME_MS
);
218 * Boards using a TUSB121x for charger-detection have 3 power_supply class devs:
220 * tusb1211-charger-detect(1) -> charger -> fuel-gauge
222 * To determine if an USB charger is connected to the board, the online prop of
223 * the charger psy needs to be read. Since the tusb1211-charger-detect psy is
224 * the start of the supplier -> supplied-to chain, power_supply_am_i_supplied()
225 * cannot be used here.
227 * Instead, below is a list of the power_supply names of known chargers for
228 * these boards and the charger psy is looked up by name from this list.
230 * (1) modelling the external USB charger
232 static const char * const tusb1210_chargers
[] = {
236 static bool tusb1210_get_online(struct tusb1210
*tusb
)
238 struct power_supply
*charger
= NULL
;
239 union power_supply_propval val
;
243 for (i
= 0; i
< ARRAY_SIZE(tusb1210_chargers
) && !charger
; i
++)
244 charger
= power_supply_get_by_name(tusb1210_chargers
[i
]);
249 ret
= power_supply_get_property(charger
, POWER_SUPPLY_PROP_ONLINE
, &val
);
253 power_supply_put(charger
);
258 static void tusb1210_chg_det_work(struct work_struct
*work
)
260 struct tusb1210
*tusb
= container_of(work
, struct tusb1210
, chg_det_work
.work
);
261 bool vbus_present
= tusb1210_get_online(tusb
);
265 dev_dbg(tusb
->dev
, "chg_det state %s vbus_present %d\n",
266 tusb1210_chg_det_states
[tusb
->chg_det_state
], vbus_present
);
268 switch (tusb
->chg_det_state
) {
269 case TUSB1210_CHG_DET_CONNECTING
:
270 tusb
->chg_type
= POWER_SUPPLY_USB_TYPE_UNKNOWN
;
271 tusb
->chg_det_retries
= 0;
272 /* Power on USB controller for ulpi_read()/_write() */
273 ret
= pm_runtime_resume_and_get(tusb
->dev
->parent
);
275 dev_err(tusb
->dev
, "error %d runtime-resuming\n", ret
);
276 /* Should never happen, skip charger detection */
277 tusb1210_chg_det_set_state(tusb
, TUSB1210_CHG_DET_CONNECTED
, 0);
280 tusb1210_chg_det_set_state(tusb
, TUSB1210_CHG_DET_START_DET
, 0);
282 case TUSB1210_CHG_DET_START_DET
:
284 * Use the builtin charger detection FSM to keep things simple.
285 * This only detects DCP / SDP. This is good enough for the few
286 * boards which actually rely on the phy for charger detection.
288 mutex_lock(&tusb
->phy
->mutex
);
289 ret
= tusb1210_ulpi_write(tusb
, TUSB1211_VENDOR_SPECIFIC3_SET
,
290 TUSB1211_VENDOR_SPECIFIC3_SW_USB_DET
);
291 mutex_unlock(&tusb
->phy
->mutex
);
293 tusb1210_chg_det_handle_ulpi_error(tusb
);
297 /* Wait 400 ms for the charger detection FSM to finish */
298 tusb1210_chg_det_set_state(tusb
, TUSB1210_CHG_DET_READ_DET
, 400);
300 case TUSB1210_CHG_DET_READ_DET
:
301 mutex_lock(&tusb
->phy
->mutex
);
302 ret
= tusb1210_ulpi_read(tusb
, TUSB1211_POWER_CONTROL
, &val
);
303 mutex_unlock(&tusb
->phy
->mutex
);
305 tusb1210_chg_det_handle_ulpi_error(tusb
);
309 if (val
& TUSB1211_POWER_CONTROL_DET_COMP
)
310 tusb1210_chg_det_set_type(tusb
, POWER_SUPPLY_USB_TYPE_DCP
);
312 tusb1210_chg_det_set_type(tusb
, POWER_SUPPLY_USB_TYPE_SDP
);
314 tusb1210_chg_det_set_state(tusb
, TUSB1210_CHG_DET_FINISH_DET
, 0);
316 case TUSB1210_CHG_DET_FINISH_DET
:
317 mutex_lock(&tusb
->phy
->mutex
);
319 /* Set SW_CONTROL to stop the charger-det FSM */
320 ret
= tusb1210_ulpi_write(tusb
, TUSB1211_POWER_CONTROL_SET
,
321 TUSB1211_POWER_CONTROL_SW_CONTROL
);
323 /* Clear DP_VSRC_EN which may have been enabled by the charger-det FSM */
324 ret
|= tusb1210_ulpi_write(tusb
, TUSB1211_POWER_CONTROL_CLEAR
,
325 TUSB1211_POWER_CONTROL_DP_VSRC_EN
);
327 /* Clear CHGD_IDP_SRC_EN (may have been enabled by the charger-det FSM) */
328 ret
|= tusb1210_ulpi_write(tusb
, TUSB1211_VENDOR_SPECIFIC3_CLEAR
,
329 TUSB1211_VENDOR_SPECIFIC3_CHGD_IDP_SRC_EN
);
331 /* If any of the above fails reset the phy */
333 tusb1210_reset(tusb
);
334 msleep(TUSB1210_RESET_TIME_MS
);
337 /* Restore phy-parameters and OTG_CTRL register */
338 tusb1210_ulpi_write(tusb
, ULPI_OTG_CTRL
, tusb
->otg_ctrl
);
339 tusb1210_ulpi_write(tusb
, TUSB1210_VENDOR_SPECIFIC2
,
340 tusb
->vendor_specific2
);
342 mutex_unlock(&tusb
->phy
->mutex
);
344 pm_runtime_put(tusb
->dev
->parent
);
345 tusb1210_chg_det_set_state(tusb
, TUSB1210_CHG_DET_CONNECTED
, 0);
347 case TUSB1210_CHG_DET_CONNECTED
:
349 tusb1210_chg_det_set_state(tusb
, TUSB1210_CHG_DET_DISCONNECTING
, 0);
351 case TUSB1210_CHG_DET_DISCONNECTING
:
353 * The phy seems to take approx. 600ms longer then the charger
354 * chip (which is used to get vbus_present) to determine Vbus
355 * session end. Wait 800ms to ensure the phy has detected and
356 * signalled Vbus session end.
358 tusb1210_chg_det_set_state(tusb
, TUSB1210_CHG_DET_DISCONNECTING_DONE
, 800);
360 case TUSB1210_CHG_DET_DISCONNECTING_DONE
:
362 * The phy often stops reacting to ulpi_read()/_write requests
363 * after a Vbus-session end. Reset it to work around this.
365 tusb1210_reset(tusb
);
366 tusb1210_chg_det_set_type(tusb
, POWER_SUPPLY_USB_TYPE_UNKNOWN
);
367 tusb1210_chg_det_set_state(tusb
, TUSB1210_CHG_DET_DISCONNECTED
, 0);
369 case TUSB1210_CHG_DET_DISCONNECTED
:
371 tusb1210_chg_det_set_state(tusb
, TUSB1210_CHG_DET_CONNECTING
, 0);
376 static int tusb1210_psy_notifier(struct notifier_block
*nb
,
377 unsigned long event
, void *ptr
)
379 struct tusb1210
*tusb
= container_of(nb
, struct tusb1210
, psy_nb
);
380 struct power_supply
*psy
= ptr
;
382 if (psy
!= tusb
->psy
&& psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
)
383 queue_delayed_work(system_long_wq
, &tusb
->chg_det_work
, 0);
388 static int tusb1210_psy_get_prop(struct power_supply
*psy
,
389 enum power_supply_property psp
,
390 union power_supply_propval
*val
)
392 struct tusb1210
*tusb
= power_supply_get_drvdata(psy
);
395 case POWER_SUPPLY_PROP_ONLINE
:
396 val
->intval
= tusb1210_get_online(tusb
);
398 case POWER_SUPPLY_PROP_USB_TYPE
:
399 val
->intval
= tusb
->chg_type
;
401 case POWER_SUPPLY_PROP_CURRENT_MAX
:
402 if (tusb
->chg_type
== POWER_SUPPLY_USB_TYPE_DCP
)
403 val
->intval
= 2000000;
405 val
->intval
= 500000;
414 static const enum power_supply_property tusb1210_psy_props
[] = {
415 POWER_SUPPLY_PROP_ONLINE
,
416 POWER_SUPPLY_PROP_USB_TYPE
,
417 POWER_SUPPLY_PROP_CURRENT_MAX
,
420 static const struct power_supply_desc tusb1210_psy_desc
= {
421 .name
= "tusb1211-charger-detect",
422 .type
= POWER_SUPPLY_TYPE_USB
,
423 .usb_types
= BIT(POWER_SUPPLY_USB_TYPE_SDP
) |
424 BIT(POWER_SUPPLY_USB_TYPE_DCP
) |
425 BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN
),
426 .properties
= tusb1210_psy_props
,
427 .num_properties
= ARRAY_SIZE(tusb1210_psy_props
),
428 .get_property
= tusb1210_psy_get_prop
,
431 /* Setup charger detection if requested, on errors continue without chg-det */
432 static void tusb1210_probe_charger_detect(struct tusb1210
*tusb
)
434 struct power_supply_config psy_cfg
= { .drv_data
= tusb
};
435 struct device
*dev
= tusb
->dev
;
436 struct ulpi
*ulpi
= to_ulpi_dev(dev
);
439 if (!device_property_read_bool(dev
->parent
, "linux,phy_charger_detect"))
442 if (ulpi
->id
.product
!= TI_DEVICE_TUSB1211
) {
443 dev_err(dev
, "error charger detection is only supported on the TUSB1211\n");
447 ret
= tusb1210_ulpi_read(tusb
, ULPI_OTG_CTRL
, &tusb
->otg_ctrl
);
451 tusb
->psy
= power_supply_register(dev
, &tusb1210_psy_desc
, &psy_cfg
);
452 if (IS_ERR(tusb
->psy
))
456 * Delay initial run by 2 seconds to allow the charger driver,
457 * which is used to determine vbus_present, to load.
459 tusb
->chg_det_state
= TUSB1210_CHG_DET_DISCONNECTED
;
460 INIT_DELAYED_WORK(&tusb
->chg_det_work
, tusb1210_chg_det_work
);
461 queue_delayed_work(system_long_wq
, &tusb
->chg_det_work
, 2 * HZ
);
463 tusb
->psy_nb
.notifier_call
= tusb1210_psy_notifier
;
464 power_supply_reg_notifier(&tusb
->psy_nb
);
467 static void tusb1210_remove_charger_detect(struct tusb1210
*tusb
)
470 if (!IS_ERR_OR_NULL(tusb
->psy
)) {
471 power_supply_unreg_notifier(&tusb
->psy_nb
);
472 cancel_delayed_work_sync(&tusb
->chg_det_work
);
473 power_supply_unregister(tusb
->psy
);
477 static void tusb1210_probe_charger_detect(struct tusb1210
*tusb
) { }
478 static void tusb1210_remove_charger_detect(struct tusb1210
*tusb
) { }
481 static const struct phy_ops phy_ops
= {
482 .power_on
= tusb1210_power_on
,
483 .power_off
= tusb1210_power_off
,
484 .set_mode
= tusb1210_set_mode
,
485 .owner
= THIS_MODULE
,
488 static int tusb1210_probe(struct ulpi
*ulpi
)
490 struct device
*dev
= &ulpi
->dev
;
491 struct tusb1210
*tusb
;
495 tusb
= devm_kzalloc(dev
, sizeof(*tusb
), GFP_KERNEL
);
501 tusb
->gpio_reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
502 if (IS_ERR(tusb
->gpio_reset
))
503 return PTR_ERR(tusb
->gpio_reset
);
505 gpiod_set_value_cansleep(tusb
->gpio_reset
, 1);
507 tusb
->gpio_cs
= devm_gpiod_get_optional(dev
, "cs", GPIOD_OUT_LOW
);
508 if (IS_ERR(tusb
->gpio_cs
))
509 return PTR_ERR(tusb
->gpio_cs
);
511 gpiod_set_value_cansleep(tusb
->gpio_cs
, 1);
514 * VENDOR_SPECIFIC2 register in TUSB1210 can be used for configuring eye
515 * diagram optimization and DP/DM swap.
518 ret
= tusb1210_ulpi_read(tusb
, TUSB1210_VENDOR_SPECIFIC2
, ®
);
522 /* High speed output drive strength configuration */
523 if (!device_property_read_u8(dev
, "ihstx", &val
))
524 u8p_replace_bits(®
, val
, (u8
)TUSB1210_VENDOR_SPECIFIC2_IHSTX_MASK
);
526 /* High speed output impedance configuration */
527 if (!device_property_read_u8(dev
, "zhsdrv", &val
))
528 u8p_replace_bits(®
, val
, (u8
)TUSB1210_VENDOR_SPECIFIC2_ZHSDRV_MASK
);
530 /* DP/DM swap control */
531 if (!device_property_read_u8(dev
, "datapolarity", &val
))
532 u8p_replace_bits(®
, val
, (u8
)TUSB1210_VENDOR_SPECIFIC2_DP_MASK
);
534 ret
= tusb1210_ulpi_write(tusb
, TUSB1210_VENDOR_SPECIFIC2
, reg
);
538 tusb
->vendor_specific2
= reg
;
540 tusb1210_probe_charger_detect(tusb
);
542 tusb
->phy
= ulpi_phy_create(ulpi
, &phy_ops
);
543 if (IS_ERR(tusb
->phy
)) {
544 ret
= PTR_ERR(tusb
->phy
);
545 goto err_remove_charger
;
548 phy_set_drvdata(tusb
->phy
, tusb
);
549 ulpi_set_drvdata(ulpi
, tusb
);
553 tusb1210_remove_charger_detect(tusb
);
557 static void tusb1210_remove(struct ulpi
*ulpi
)
559 struct tusb1210
*tusb
= ulpi_get_drvdata(ulpi
);
561 ulpi_phy_destroy(ulpi
, tusb
->phy
);
562 tusb1210_remove_charger_detect(tusb
);
565 static const struct ulpi_device_id tusb1210_ulpi_id
[] = {
566 { TI_VENDOR_ID
, TI_DEVICE_TUSB1210
},
567 { TI_VENDOR_ID
, TI_DEVICE_TUSB1211
},
570 MODULE_DEVICE_TABLE(ulpi
, tusb1210_ulpi_id
);
572 static struct ulpi_driver tusb1210_driver
= {
573 .id_table
= tusb1210_ulpi_id
,
574 .probe
= tusb1210_probe
,
575 .remove
= tusb1210_remove
,
578 .owner
= THIS_MODULE
,
582 module_ulpi_driver(tusb1210_driver
);
584 MODULE_AUTHOR("Intel Corporation");
585 MODULE_LICENSE("GPL v2");
586 MODULE_DESCRIPTION("TUSB1210 ULPI PHY driver");