xref: /linux/drivers/usb/phy/phy-isp1301.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0
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>
15 #include <linux/mutex.h>
16 #include <linux/i2c.h>
17 #include <linux/usb/phy.h>
18 #include <linux/usb/isp1301.h>
19 
20 #define DRV_NAME		"isp1301"
21 
22 struct isp1301 {
23 	struct usb_phy		phy;
24 	struct mutex		mutex;
25 
26 	struct i2c_client	*client;
27 };
28 
29 #define phy_to_isp(p)		(container_of((p), struct isp1301, phy))
30 
31 static const struct i2c_device_id isp1301_id[] = {
32 	{ "isp1301", 0 },
33 	{ }
34 };
35 MODULE_DEVICE_TABLE(i2c, isp1301_id);
36 
37 static const struct of_device_id isp1301_of_match[] = {
38 	{.compatible = "nxp,isp1301" },
39 	{ },
40 };
41 MODULE_DEVICE_TABLE(of, isp1301_of_match);
42 
43 static struct i2c_client *isp1301_i2c_client;
44 
45 static 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 
50 static int isp1301_write(struct isp1301 *isp, u8 reg, u8 value)
51 {
52 	return __isp1301_write(isp, reg, value, 0);
53 }
54 
55 static 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 
60 static 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 
87 static 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 
99 static int isp1301_probe(struct i2c_client *client,
100 			 const struct i2c_device_id *i2c_id)
101 {
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;
113 	phy->dev = &client->dev;
114 	phy->label = DRV_NAME;
115 	phy->init = isp1301_phy_init;
116 	phy->set_vbus = isp1301_phy_set_vbus;
117 	phy->type = USB_PHY_TYPE_USB2;
118 
119 	i2c_set_clientdata(client, isp);
120 	usb_add_phy_dev(phy);
121 
122 	isp1301_i2c_client = client;
123 
124 	return 0;
125 }
126 
127 static int isp1301_remove(struct i2c_client *client)
128 {
129 	struct isp1301 *isp = i2c_get_clientdata(client);
130 
131 	usb_remove_phy(&isp->phy);
132 	isp1301_i2c_client = NULL;
133 
134 	return 0;
135 }
136 
137 static struct i2c_driver isp1301_driver = {
138 	.driver = {
139 		.name = DRV_NAME,
140 		.of_match_table = isp1301_of_match,
141 	},
142 	.probe = isp1301_probe,
143 	.remove = isp1301_remove,
144 	.id_table = isp1301_id,
145 };
146 
147 module_i2c_driver(isp1301_driver);
148 
149 static 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 
156 struct 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 }
168 EXPORT_SYMBOL_GPL(isp1301_get_client);
169 
170 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
171 MODULE_DESCRIPTION("NXP ISP1301 USB transceiver driver");
172 MODULE_LICENSE("GPL");
173