Merge branch 'pm-opp'
[cascardo/linux.git] / drivers / phy / phy-tusb1210.c
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
23 struct 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
31 static 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
46 static 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
56 static const struct phy_ops phy_ops = {
57         .power_on = tusb1210_power_on,
58         .power_off = tusb1210_power_off,
59         .owner = THIS_MODULE,
60 };
61
62 static int tusb1210_probe(struct ulpi *ulpi)
63 {
64         struct gpio_desc *gpio;
65         struct tusb1210 *tusb;
66         u8 val, reg;
67         int ret;
68
69         tusb = devm_kzalloc(&ulpi->dev, sizeof(*tusb), GFP_KERNEL);
70         if (!tusb)
71                 return -ENOMEM;
72
73         gpio = devm_gpiod_get(&ulpi->dev, "reset");
74         if (!IS_ERR(gpio)) {
75                 ret = gpiod_direction_output(gpio, 0);
76                 if (ret)
77                         return ret;
78                 gpiod_set_value_cansleep(gpio, 1);
79                 tusb->gpio_reset = gpio;
80         }
81
82         gpio = devm_gpiod_get(&ulpi->dev, "cs");
83         if (!IS_ERR(gpio)) {
84                 ret = gpiod_direction_output(gpio, 0);
85                 if (ret)
86                         return ret;
87                 gpiod_set_value_cansleep(gpio, 1);
88                 tusb->gpio_cs = gpio;
89         }
90
91         /*
92          * VENDOR_SPECIFIC2 register in TUSB1210 can be used for configuring eye
93          * diagram optimization and DP/DM swap.
94          */
95
96         /* High speed output drive strength configuration */
97         device_property_read_u8(&ulpi->dev, "ihstx", &val);
98         reg = val << TUSB1210_VENDOR_SPECIFIC2_IHSTX_SHIFT;
99
100         /* High speed output impedance configuration */
101         device_property_read_u8(&ulpi->dev, "zhsdrv", &val);
102         reg |= val << TUSB1210_VENDOR_SPECIFIC2_ZHSDRV_SHIFT;
103
104         /* DP/DM swap control */
105         device_property_read_u8(&ulpi->dev, "datapolarity", &val);
106         reg |= val << TUSB1210_VENDOR_SPECIFIC2_DP_SHIFT;
107
108         if (reg) {
109                 ulpi_write(ulpi, TUSB1210_VENDOR_SPECIFIC2, reg);
110                 tusb->vendor_specific2 = reg;
111         }
112
113         tusb->phy = ulpi_phy_create(ulpi, &phy_ops);
114         if (IS_ERR(tusb->phy))
115                 return PTR_ERR(tusb->phy);
116
117         tusb->ulpi = ulpi;
118
119         phy_set_drvdata(tusb->phy, tusb);
120         ulpi_set_drvdata(ulpi, tusb);
121         return 0;
122 }
123
124 static void tusb1210_remove(struct ulpi *ulpi)
125 {
126         struct tusb1210 *tusb = ulpi_get_drvdata(ulpi);
127
128         ulpi_phy_destroy(ulpi, tusb->phy);
129 }
130
131 #define TI_VENDOR_ID 0x0451
132
133 static const struct ulpi_device_id tusb1210_ulpi_id[] = {
134         { TI_VENDOR_ID, 0x1507, },
135         { },
136 };
137 MODULE_DEVICE_TABLE(ulpi, tusb1210_ulpi_id);
138
139 static struct ulpi_driver tusb1210_driver = {
140         .id_table = tusb1210_ulpi_id,
141         .probe = tusb1210_probe,
142         .remove = tusb1210_remove,
143         .driver = {
144                 .name = "tusb1210",
145                 .owner = THIS_MODULE,
146         },
147 };
148
149 module_ulpi_driver(tusb1210_driver);
150
151 MODULE_AUTHOR("Intel Corporation");
152 MODULE_LICENSE("GPL v2");
153 MODULE_DESCRIPTION("TUSB1210 ULPI PHY driver");