2 * Cavium ThunderX i2c driver.
4 * Copyright (C) 2015,2016 Cavium Inc.
5 * Authors: Fred Martin <fmartin@caviumnetworks.com>
6 * Jan Glauber <jglauber@cavium.com>
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
13 #include <linux/acpi.h>
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
22 #include "i2c-octeon-core.h"
24 #define DRV_NAME "i2c-thunderx"
26 #define PCI_DEVICE_ID_THUNDER_TWSI 0xa012
28 #define SYS_FREQ_DEFAULT 700000000
30 #define TWSI_INT_ENA_W1C 0x1028
31 #define TWSI_INT_ENA_W1S 0x1030
34 * Enable the CORE interrupt.
35 * The interrupt will be asserted when there is non-STAT_IDLE state in the
36 * SW_TWSI_EOP_TWSI_STAT register.
38 static void thunder_i2c_int_enable(struct octeon_i2c *i2c)
40 octeon_i2c_writeq_flush(TWSI_INT_CORE_INT,
41 i2c->twsi_base + TWSI_INT_ENA_W1S);
45 * Disable the CORE interrupt.
47 static void thunder_i2c_int_disable(struct octeon_i2c *i2c)
49 octeon_i2c_writeq_flush(TWSI_INT_CORE_INT,
50 i2c->twsi_base + TWSI_INT_ENA_W1C);
53 static void thunder_i2c_hlc_int_enable(struct octeon_i2c *i2c)
55 octeon_i2c_writeq_flush(TWSI_INT_ST_INT | TWSI_INT_TS_INT,
56 i2c->twsi_base + TWSI_INT_ENA_W1S);
59 static void thunder_i2c_hlc_int_disable(struct octeon_i2c *i2c)
61 octeon_i2c_writeq_flush(TWSI_INT_ST_INT | TWSI_INT_TS_INT,
62 i2c->twsi_base + TWSI_INT_ENA_W1C);
65 static u32 thunderx_i2c_functionality(struct i2c_adapter *adap)
67 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
68 I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_SMBUS_BLOCK_PROC_CALL;
71 static const struct i2c_algorithm thunderx_i2c_algo = {
72 .master_xfer = octeon_i2c_xfer,
73 .functionality = thunderx_i2c_functionality,
76 static struct i2c_adapter thunderx_i2c_ops = {
78 .name = "ThunderX adapter",
79 .algo = &thunderx_i2c_algo,
82 static void thunder_i2c_clock_enable(struct device *dev, struct octeon_i2c *i2c)
86 i2c->clk = clk_get(dev, NULL);
87 if (IS_ERR(i2c->clk)) {
92 ret = clk_prepare_enable(i2c->clk);
95 i2c->sys_freq = clk_get_rate(i2c->clk);
99 i2c->sys_freq = SYS_FREQ_DEFAULT;
102 static void thunder_i2c_clock_disable(struct device *dev, struct clk *clk)
106 clk_disable_unprepare(clk);
110 static int thunder_i2c_probe_pci(struct pci_dev *pdev,
111 const struct pci_device_id *ent)
113 struct device *dev = &pdev->dev;
114 struct octeon_i2c *i2c;
117 i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL);
122 pci_set_drvdata(pdev, i2c);
123 ret = pcim_enable_device(pdev);
127 ret = pci_request_regions(pdev, DRV_NAME);
131 i2c->twsi_base = pcim_iomap(pdev, 0, pci_resource_len(pdev, 0));
135 thunder_i2c_clock_enable(dev, i2c);
136 ret = device_property_read_u32(dev, "clock-frequency", &i2c->twsi_freq);
138 i2c->twsi_freq = 100000;
140 init_waitqueue_head(&i2c->queue);
142 i2c->int_enable = thunder_i2c_int_enable;
143 i2c->int_disable = thunder_i2c_int_disable;
144 i2c->hlc_int_enable = thunder_i2c_hlc_int_enable;
145 i2c->hlc_int_disable = thunder_i2c_hlc_int_disable;
147 ret = pci_enable_msix(pdev, &i2c->i2c_msix, 1);
151 ret = devm_request_irq(dev, i2c->i2c_msix.vector, octeon_i2c_isr, 0,
156 ret = octeon_i2c_init_lowlevel(i2c);
160 octeon_i2c_set_clock(i2c);
162 i2c->adap = thunderx_i2c_ops;
163 i2c->adap.retries = 5;
164 i2c->adap.bus_recovery_info = &octeon_i2c_recovery_info;
165 i2c->adap.dev.parent = dev;
166 i2c->adap.dev.of_node = pdev->dev.of_node;
167 snprintf(i2c->adap.name, sizeof(i2c->adap.name),
168 "Cavium ThunderX i2c adapter at %s", dev_name(dev));
169 i2c_set_adapdata(&i2c->adap, i2c);
171 ret = i2c_add_adapter(&i2c->adap);
175 dev_info(i2c->dev, "Probed. Set system clock to %u\n", i2c->sys_freq);
179 thunder_i2c_clock_disable(dev, i2c->clk);
183 static void thunder_i2c_remove_pci(struct pci_dev *pdev)
185 struct octeon_i2c *i2c = pci_get_drvdata(pdev);
187 thunder_i2c_clock_disable(&pdev->dev, i2c->clk);
188 i2c_del_adapter(&i2c->adap);
191 static const struct pci_device_id thunder_i2c_pci_id_table[] = {
192 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_TWSI) },
196 MODULE_DEVICE_TABLE(pci, thunder_i2c_pci_id_table);
198 static struct pci_driver thunder_i2c_pci_driver = {
200 .id_table = thunder_i2c_pci_id_table,
201 .probe = thunder_i2c_probe_pci,
202 .remove = thunder_i2c_remove_pci,
205 module_pci_driver(thunder_i2c_pci_driver);
207 MODULE_LICENSE("GPL");
208 MODULE_AUTHOR("Fred Martin <fmartin@caviumnetworks.com>");
209 MODULE_DESCRIPTION("I2C-Bus adapter for Cavium ThunderX SOC");