USB: add SPDX identifiers to all remaining files in drivers/usb/
[linux-2.6-block.git] / drivers / usb / phy / phy-isp1301.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
8b7c3b68
RS
2/*
3 * NXP ISP1301 USB transceiver driver
4 *
5 * Copyright (C) 2012 Roland Stigge
6 *
7 * Author: Roland Stigge <stigge@antcom.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
790d3a5a 15#include <linux/mutex.h>
8b7c3b68 16#include <linux/i2c.h>
790d3a5a 17#include <linux/usb/phy.h>
c38a4f3f 18#include <linux/usb/isp1301.h>
8b7c3b68
RS
19
20#define DRV_NAME "isp1301"
21
790d3a5a
FB
22struct isp1301 {
23 struct usb_phy phy;
24 struct mutex mutex;
25
26 struct i2c_client *client;
27};
28
c38a4f3f
FB
29#define phy_to_isp(p) (container_of((p), struct isp1301, phy))
30
8b7c3b68
RS
31static const struct i2c_device_id isp1301_id[] = {
32 { "isp1301", 0 },
33 { }
34};
8fb7ab50 35MODULE_DEVICE_TABLE(i2c, isp1301_id);
8b7c3b68 36
fd567653
JMC
37static const struct of_device_id isp1301_of_match[] = {
38 {.compatible = "nxp,isp1301" },
39 { },
40};
41MODULE_DEVICE_TABLE(of, isp1301_of_match);
42
8b7c3b68
RS
43static struct i2c_client *isp1301_i2c_client;
44
c38a4f3f
FB
45static int __isp1301_write(struct isp1301 *isp, u8 reg, u8 value, u8 clear)
46{
47 return i2c_smbus_write_byte_data(isp->client, reg | clear, value);
48}
49
50static int isp1301_write(struct isp1301 *isp, u8 reg, u8 value)
51{
52 return __isp1301_write(isp, reg, value, 0);
53}
54
55static int isp1301_clear(struct isp1301 *isp, u8 reg, u8 value)
56{
57 return __isp1301_write(isp, reg, value, ISP1301_I2C_REG_CLEAR_ADDR);
58}
59
60static int isp1301_phy_init(struct usb_phy *phy)
61{
62 struct isp1301 *isp = phy_to_isp(phy);
63
64 /* Disable transparent UART mode first */
65 isp1301_clear(isp, ISP1301_I2C_MODE_CONTROL_1, MC1_UART_EN);
66 isp1301_clear(isp, ISP1301_I2C_MODE_CONTROL_1, ~MC1_SPEED_REG);
67 isp1301_write(isp, ISP1301_I2C_MODE_CONTROL_1, MC1_SPEED_REG);
68 isp1301_clear(isp, ISP1301_I2C_MODE_CONTROL_2, ~0);
69 isp1301_write(isp, ISP1301_I2C_MODE_CONTROL_2, (MC2_BI_DI | MC2_PSW_EN
70 | MC2_SPD_SUSP_CTRL));
71
72 isp1301_clear(isp, ISP1301_I2C_OTG_CONTROL_1, ~0);
73 isp1301_write(isp, ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0);
74 isp1301_write(isp, ISP1301_I2C_OTG_CONTROL_1, (OTG1_DM_PULLDOWN
75 | OTG1_DP_PULLDOWN));
76 isp1301_clear(isp, ISP1301_I2C_OTG_CONTROL_1, (OTG1_DM_PULLUP
77 | OTG1_DP_PULLUP));
78
79 /* mask all interrupts */
80 isp1301_clear(isp, ISP1301_I2C_INTERRUPT_LATCH, ~0);
81 isp1301_clear(isp, ISP1301_I2C_INTERRUPT_FALLING, ~0);
82 isp1301_clear(isp, ISP1301_I2C_INTERRUPT_RISING, ~0);
83
84 return 0;
85}
86
87static int isp1301_phy_set_vbus(struct usb_phy *phy, int on)
88{
89 struct isp1301 *isp = phy_to_isp(phy);
90
91 if (on)
92 isp1301_write(isp, ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DRV);
93 else
94 isp1301_clear(isp, ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DRV);
95
96 return 0;
97}
98
8b7c3b68
RS
99static int isp1301_probe(struct i2c_client *client,
100 const struct i2c_device_id *i2c_id)
101{
790d3a5a
FB
102 struct isp1301 *isp;
103 struct usb_phy *phy;
104
105 isp = devm_kzalloc(&client->dev, sizeof(*isp), GFP_KERNEL);
106 if (!isp)
107 return -ENOMEM;
108
109 isp->client = client;
110 mutex_init(&isp->mutex);
111
112 phy = &isp->phy;
1abd8b31 113 phy->dev = &client->dev;
790d3a5a 114 phy->label = DRV_NAME;
c38a4f3f
FB
115 phy->init = isp1301_phy_init;
116 phy->set_vbus = isp1301_phy_set_vbus;
790d3a5a
FB
117 phy->type = USB_PHY_TYPE_USB2;
118
119 i2c_set_clientdata(client, isp);
120 usb_add_phy_dev(phy);
121
8b7c3b68 122 isp1301_i2c_client = client;
790d3a5a 123
8b7c3b68
RS
124 return 0;
125}
126
127static int isp1301_remove(struct i2c_client *client)
128{
790d3a5a
FB
129 struct isp1301 *isp = i2c_get_clientdata(client);
130
131 usb_remove_phy(&isp->phy);
132 isp1301_i2c_client = NULL;
133
8b7c3b68
RS
134 return 0;
135}
136
137static struct i2c_driver isp1301_driver = {
138 .driver = {
139 .name = DRV_NAME,
a7f12a21 140 .of_match_table = isp1301_of_match,
8b7c3b68
RS
141 },
142 .probe = isp1301_probe,
143 .remove = isp1301_remove,
144 .id_table = isp1301_id,
145};
146
147module_i2c_driver(isp1301_driver);
148
149static int match(struct device *dev, void *data)
150{
151 struct device_node *node = (struct device_node *)data;
152 return (dev->of_node == node) &&
153 (dev->driver == &isp1301_driver.driver);
154}
155
156struct i2c_client *isp1301_get_client(struct device_node *node)
157{
158 if (node) { /* reference of ISP1301 I2C node via DT */
159 struct device *dev = bus_find_device(&i2c_bus_type, NULL,
160 node, match);
161 if (!dev)
162 return NULL;
163 return to_i2c_client(dev);
164 } else { /* non-DT: only one ISP1301 chip supported */
165 return isp1301_i2c_client;
166 }
167}
168EXPORT_SYMBOL_GPL(isp1301_get_client);
169
170MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
171MODULE_DESCRIPTION("NXP ISP1301 USB transceiver driver");
172MODULE_LICENSE("GPL");