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