Linux Audio

Check our new training course

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