1/**
2 * tusb1210.c - TUSB1210 USB ULPI PHY driver
3 *
4 * Copyright (C) 2015 Intel Corporation
5 *
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12#include <linux/module.h>
13#include <linux/ulpi/driver.h>
14#include <linux/gpio/consumer.h>
15
16#include "ulpi_phy.h"
17
18#define TUSB1210_VENDOR_SPECIFIC2		0x80
19#define TUSB1210_VENDOR_SPECIFIC2_IHSTX_SHIFT	0
20#define TUSB1210_VENDOR_SPECIFIC2_ZHSDRV_SHIFT	4
21#define TUSB1210_VENDOR_SPECIFIC2_DP_SHIFT	6
22
23struct tusb1210 {
24	struct ulpi *ulpi;
25	struct phy *phy;
26	struct gpio_desc *gpio_reset;
27	struct gpio_desc *gpio_cs;
28	u8 vendor_specific2;
29};
30
31static int tusb1210_power_on(struct phy *phy)
32{
33	struct tusb1210 *tusb = phy_get_drvdata(phy);
34
35	gpiod_set_value_cansleep(tusb->gpio_reset, 1);
36	gpiod_set_value_cansleep(tusb->gpio_cs, 1);
37
38	/* Restore the optional eye diagram optimization value */
39	if (tusb->vendor_specific2)
40		ulpi_write(tusb->ulpi, TUSB1210_VENDOR_SPECIFIC2,
41			   tusb->vendor_specific2);
42
43	return 0;
44}
45
46static int tusb1210_power_off(struct phy *phy)
47{
48	struct tusb1210 *tusb = phy_get_drvdata(phy);
49
50	gpiod_set_value_cansleep(tusb->gpio_reset, 0);
51	gpiod_set_value_cansleep(tusb->gpio_cs, 0);
52
53	return 0;
54}
55
56static const struct phy_ops phy_ops = {
57	.power_on = tusb1210_power_on,
58	.power_off = tusb1210_power_off,
59	.owner = THIS_MODULE,
60};
61
62static int tusb1210_probe(struct ulpi *ulpi)
63{
64	struct tusb1210 *tusb;
65	u8 val, reg;
66
67	tusb = devm_kzalloc(&ulpi->dev, sizeof(*tusb), GFP_KERNEL);
68	if (!tusb)
69		return -ENOMEM;
70
71	tusb->gpio_reset = devm_gpiod_get_optional(&ulpi->dev, "reset",
72						   GPIOD_OUT_LOW);
73	if (IS_ERR(tusb->gpio_reset))
74		return PTR_ERR(tusb->gpio_reset);
75
76	gpiod_set_value_cansleep(tusb->gpio_reset, 1);
77
78	tusb->gpio_cs = devm_gpiod_get_optional(&ulpi->dev, "cs",
79						GPIOD_OUT_LOW);
80	if (IS_ERR(tusb->gpio_cs))
81		return PTR_ERR(tusb->gpio_cs);
82
83	gpiod_set_value_cansleep(tusb->gpio_cs, 1);
84
85	/*
86	 * VENDOR_SPECIFIC2 register in TUSB1210 can be used for configuring eye
87	 * diagram optimization and DP/DM swap.
88	 */
89
90	/* High speed output drive strength configuration */
91	device_property_read_u8(&ulpi->dev, "ihstx", &val);
92	reg = val << TUSB1210_VENDOR_SPECIFIC2_IHSTX_SHIFT;
93
94	/* High speed output impedance configuration */
95	device_property_read_u8(&ulpi->dev, "zhsdrv", &val);
96	reg |= val << TUSB1210_VENDOR_SPECIFIC2_ZHSDRV_SHIFT;
97
98	/* DP/DM swap control */
99	device_property_read_u8(&ulpi->dev, "datapolarity", &val);
100	reg |= val << TUSB1210_VENDOR_SPECIFIC2_DP_SHIFT;
101
102	if (reg) {
103		ulpi_write(ulpi, TUSB1210_VENDOR_SPECIFIC2, reg);
104		tusb->vendor_specific2 = reg;
105	}
106
107	tusb->phy = ulpi_phy_create(ulpi, &phy_ops);
108	if (IS_ERR(tusb->phy))
109		return PTR_ERR(tusb->phy);
110
111	tusb->ulpi = ulpi;
112
113	phy_set_drvdata(tusb->phy, tusb);
114	ulpi_set_drvdata(ulpi, tusb);
115	return 0;
116}
117
118static void tusb1210_remove(struct ulpi *ulpi)
119{
120	struct tusb1210 *tusb = ulpi_get_drvdata(ulpi);
121
122	ulpi_phy_destroy(ulpi, tusb->phy);
123}
124
125#define TI_VENDOR_ID 0x0451
126
127static const struct ulpi_device_id tusb1210_ulpi_id[] = {
128	{ TI_VENDOR_ID, 0x1507, },
129	{ },
130};
131MODULE_DEVICE_TABLE(ulpi, tusb1210_ulpi_id);
132
133static struct ulpi_driver tusb1210_driver = {
134	.id_table = tusb1210_ulpi_id,
135	.probe = tusb1210_probe,
136	.remove = tusb1210_remove,
137	.driver = {
138		.name = "tusb1210",
139		.owner = THIS_MODULE,
140	},
141};
142
143module_ulpi_driver(tusb1210_driver);
144
145MODULE_AUTHOR("Intel Corporation");
146MODULE_LICENSE("GPL v2");
147MODULE_DESCRIPTION("TUSB1210 ULPI PHY driver");
148