i2c: thunderx: Add i2c driver for ThunderX SOC
[cascardo/linux.git] / drivers / i2c / busses / i2c-thunderx-pcidrv.c
1 /*
2  * Cavium ThunderX i2c driver.
3  *
4  * Copyright (C) 2015,2016 Cavium Inc.
5  * Authors: Fred Martin <fmartin@caviumnetworks.com>
6  *          Jan Glauber <jglauber@cavium.com>
7  *
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.
11  */
12
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>
21
22 #include "i2c-octeon-core.h"
23
24 #define DRV_NAME "i2c-thunderx"
25
26 #define PCI_DEVICE_ID_THUNDER_TWSI      0xa012
27
28 #define SYS_FREQ_DEFAULT                700000000
29
30 #define TWSI_INT_ENA_W1C                0x1028
31 #define TWSI_INT_ENA_W1S                0x1030
32
33 /*
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.
37  */
38 static void thunder_i2c_int_enable(struct octeon_i2c *i2c)
39 {
40         octeon_i2c_writeq_flush(TWSI_INT_CORE_INT,
41                                 i2c->twsi_base + TWSI_INT_ENA_W1S);
42 }
43
44 /*
45  * Disable the CORE interrupt.
46  */
47 static void thunder_i2c_int_disable(struct octeon_i2c *i2c)
48 {
49         octeon_i2c_writeq_flush(TWSI_INT_CORE_INT,
50                                 i2c->twsi_base + TWSI_INT_ENA_W1C);
51 }
52
53 static void thunder_i2c_hlc_int_enable(struct octeon_i2c *i2c)
54 {
55         octeon_i2c_writeq_flush(TWSI_INT_ST_INT | TWSI_INT_TS_INT,
56                                 i2c->twsi_base + TWSI_INT_ENA_W1S);
57 }
58
59 static void thunder_i2c_hlc_int_disable(struct octeon_i2c *i2c)
60 {
61         octeon_i2c_writeq_flush(TWSI_INT_ST_INT | TWSI_INT_TS_INT,
62                                 i2c->twsi_base + TWSI_INT_ENA_W1C);
63 }
64
65 static u32 thunderx_i2c_functionality(struct i2c_adapter *adap)
66 {
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;
69 }
70
71 static const struct i2c_algorithm thunderx_i2c_algo = {
72         .master_xfer = octeon_i2c_xfer,
73         .functionality = thunderx_i2c_functionality,
74 };
75
76 static struct i2c_adapter thunderx_i2c_ops = {
77         .owner  = THIS_MODULE,
78         .name   = "ThunderX adapter",
79         .algo   = &thunderx_i2c_algo,
80 };
81
82 static void thunder_i2c_clock_enable(struct device *dev, struct octeon_i2c *i2c)
83 {
84         int ret;
85
86         i2c->clk = clk_get(dev, NULL);
87         if (IS_ERR(i2c->clk)) {
88                 i2c->clk = NULL;
89                 goto skip;
90         }
91
92         ret = clk_prepare_enable(i2c->clk);
93         if (ret)
94                 goto skip;
95         i2c->sys_freq = clk_get_rate(i2c->clk);
96
97 skip:
98         if (!i2c->sys_freq)
99                 i2c->sys_freq = SYS_FREQ_DEFAULT;
100 }
101
102 static void thunder_i2c_clock_disable(struct device *dev, struct clk *clk)
103 {
104         if (!clk)
105                 return;
106         clk_disable_unprepare(clk);
107         clk_put(clk);
108 }
109
110 static int thunder_i2c_probe_pci(struct pci_dev *pdev,
111                                  const struct pci_device_id *ent)
112 {
113         struct device *dev = &pdev->dev;
114         struct octeon_i2c *i2c;
115         int ret;
116
117         i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL);
118         if (!i2c)
119                 return -ENOMEM;
120
121         i2c->dev = dev;
122         pci_set_drvdata(pdev, i2c);
123         ret = pcim_enable_device(pdev);
124         if (ret)
125                 return ret;
126
127         ret = pci_request_regions(pdev, DRV_NAME);
128         if (ret)
129                 return ret;
130
131         i2c->twsi_base = pcim_iomap(pdev, 0, pci_resource_len(pdev, 0));
132         if (!i2c->twsi_base)
133                 return -EINVAL;
134
135         thunder_i2c_clock_enable(dev, i2c);
136         ret = device_property_read_u32(dev, "clock-frequency", &i2c->twsi_freq);
137         if (ret)
138                 i2c->twsi_freq = 100000;
139
140         init_waitqueue_head(&i2c->queue);
141
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;
146
147         ret = pci_enable_msix(pdev, &i2c->i2c_msix, 1);
148         if (ret)
149                 goto error;
150
151         ret = devm_request_irq(dev, i2c->i2c_msix.vector, octeon_i2c_isr, 0,
152                                DRV_NAME, i2c);
153         if (ret)
154                 goto error;
155
156         ret = octeon_i2c_init_lowlevel(i2c);
157         if (ret)
158                 goto error;
159
160         octeon_i2c_set_clock(i2c);
161
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);
170
171         ret = i2c_add_adapter(&i2c->adap);
172         if (ret)
173                 goto error;
174
175         dev_info(i2c->dev, "Probed. Set system clock to %u\n", i2c->sys_freq);
176         return 0;
177
178 error:
179         thunder_i2c_clock_disable(dev, i2c->clk);
180         return ret;
181 }
182
183 static void thunder_i2c_remove_pci(struct pci_dev *pdev)
184 {
185         struct octeon_i2c *i2c = pci_get_drvdata(pdev);
186
187         thunder_i2c_clock_disable(&pdev->dev, i2c->clk);
188         i2c_del_adapter(&i2c->adap);
189 }
190
191 static const struct pci_device_id thunder_i2c_pci_id_table[] = {
192         { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_TWSI) },
193         { 0, }
194 };
195
196 MODULE_DEVICE_TABLE(pci, thunder_i2c_pci_id_table);
197
198 static struct pci_driver thunder_i2c_pci_driver = {
199         .name           = DRV_NAME,
200         .id_table       = thunder_i2c_pci_id_table,
201         .probe          = thunder_i2c_probe_pci,
202         .remove         = thunder_i2c_remove_pci,
203 };
204
205 module_pci_driver(thunder_i2c_pci_driver);
206
207 MODULE_LICENSE("GPL");
208 MODULE_AUTHOR("Fred Martin <fmartin@caviumnetworks.com>");
209 MODULE_DESCRIPTION("I2C-Bus adapter for Cavium ThunderX SOC");