i2c: designware-pci: Add a note about struct dw_scl_sda_cfg usage
[linux-2.6-microblaze.git] / drivers / i2c / busses / i2c-designware-pcidrv.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Synopsys DesignWare I2C adapter driver (master only).
4  *
5  * Based on the TI DAVINCI I2C adapter driver.
6  *
7  * Copyright (C) 2006 Texas Instruments.
8  * Copyright (C) 2007 MontaVista Software Inc.
9  * Copyright (C) 2009 Provigent Ltd.
10  * Copyright (C) 2011, 2015, 2016 Intel Corporation.
11  */
12 #include <linux/acpi.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/errno.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25
26 #include "i2c-designware-core.h"
27
28 #define DRIVER_NAME "i2c-designware-pci"
29 #define AMD_CLK_RATE_HZ 100000
30
31 enum dw_pci_ctl_id_t {
32         medfield,
33         merrifield,
34         baytrail,
35         cherrytrail,
36         haswell,
37         elkhartlake,
38         navi_amd,
39 };
40
41 /*
42  * This is a legacy structure to describe the hardware counters
43  * to configure signal timings on the bus. For Device Tree platforms
44  * one should use the respective properties and for ACPI there is
45  * a set of ACPI methods that provide these counters. No new
46  * platform should use this structure.
47  */
48 struct dw_scl_sda_cfg {
49         u16 ss_hcnt;
50         u16 fs_hcnt;
51         u16 ss_lcnt;
52         u16 fs_lcnt;
53         u32 sda_hold;
54 };
55
56 struct dw_pci_controller {
57         u32 bus_num;
58         u32 flags;
59         struct dw_scl_sda_cfg *scl_sda_cfg;
60         int (*setup)(struct pci_dev *pdev, struct dw_pci_controller *c);
61         u32 (*get_clk_rate_khz)(struct dw_i2c_dev *dev);
62 };
63
64 /* Merrifield HCNT/LCNT/SDA hold time */
65 static struct dw_scl_sda_cfg mrfld_config = {
66         .ss_hcnt = 0x2f8,
67         .fs_hcnt = 0x87,
68         .ss_lcnt = 0x37b,
69         .fs_lcnt = 0x10a,
70 };
71
72 /* BayTrail HCNT/LCNT/SDA hold time */
73 static struct dw_scl_sda_cfg byt_config = {
74         .ss_hcnt = 0x200,
75         .fs_hcnt = 0x55,
76         .ss_lcnt = 0x200,
77         .fs_lcnt = 0x99,
78         .sda_hold = 0x6,
79 };
80
81 /* Haswell HCNT/LCNT/SDA hold time */
82 static struct dw_scl_sda_cfg hsw_config = {
83         .ss_hcnt = 0x01b0,
84         .fs_hcnt = 0x48,
85         .ss_lcnt = 0x01fb,
86         .fs_lcnt = 0xa0,
87         .sda_hold = 0x9,
88 };
89
90 /* NAVI-AMD HCNT/LCNT/SDA hold time */
91 static struct dw_scl_sda_cfg navi_amd_config = {
92         .ss_hcnt = 0x1ae,
93         .ss_lcnt = 0x23a,
94         .sda_hold = 0x9,
95 };
96
97 static u32 mfld_get_clk_rate_khz(struct dw_i2c_dev *dev)
98 {
99         return 25000;
100 }
101
102 static u32 navi_amd_get_clk_rate_khz(struct dw_i2c_dev *dev)
103 {
104         return AMD_CLK_RATE_HZ;
105 }
106
107 static int mfld_setup(struct pci_dev *pdev, struct dw_pci_controller *c)
108 {
109         struct dw_i2c_dev *dev = dev_get_drvdata(&pdev->dev);
110
111         switch (pdev->device) {
112         case 0x0817:
113                 dev->timings.bus_freq_hz = I2C_MAX_STANDARD_MODE_FREQ;
114                 fallthrough;
115         case 0x0818:
116         case 0x0819:
117                 c->bus_num = pdev->device - 0x817 + 3;
118                 return 0;
119         case 0x082C:
120         case 0x082D:
121         case 0x082E:
122                 c->bus_num = pdev->device - 0x82C + 0;
123                 return 0;
124         }
125         return -ENODEV;
126 }
127
128  /*
129   * TODO find a better way how to deduplicate instantiation
130   * of USB PD slave device from nVidia GPU driver.
131   */
132 static int navi_amd_register_client(struct dw_i2c_dev *dev)
133 {
134         struct i2c_board_info   info;
135
136         memset(&info, 0, sizeof(struct i2c_board_info));
137         strscpy(info.type, "ccgx-ucsi", I2C_NAME_SIZE);
138         info.addr = 0x08;
139         info.irq = dev->irq;
140
141         dev->slave = i2c_new_client_device(&dev->adapter, &info);
142         if (IS_ERR(dev->slave))
143                 return PTR_ERR(dev->slave);
144
145         return 0;
146 }
147
148 static int navi_amd_setup(struct pci_dev *pdev, struct dw_pci_controller *c)
149 {
150         struct dw_i2c_dev *dev = dev_get_drvdata(&pdev->dev);
151
152         dev->flags |= MODEL_AMD_NAVI_GPU;
153         dev->timings.bus_freq_hz = I2C_MAX_STANDARD_MODE_FREQ;
154         return 0;
155 }
156
157 static int mrfld_setup(struct pci_dev *pdev, struct dw_pci_controller *c)
158 {
159         /*
160          * On Intel Merrifield the user visible i2c buses are enumerated
161          * [1..7]. So, we add 1 to shift the default range. Besides that the
162          * first PCI slot provides 4 functions, that's why we have to add 0 to
163          * the first slot and 4 to the next one.
164          */
165         switch (PCI_SLOT(pdev->devfn)) {
166         case 8:
167                 c->bus_num = PCI_FUNC(pdev->devfn) + 0 + 1;
168                 return 0;
169         case 9:
170                 c->bus_num = PCI_FUNC(pdev->devfn) + 4 + 1;
171                 return 0;
172         }
173         return -ENODEV;
174 }
175
176 static u32 ehl_get_clk_rate_khz(struct dw_i2c_dev *dev)
177 {
178         return 100000;
179 }
180
181 static struct dw_pci_controller dw_pci_controllers[] = {
182         [medfield] = {
183                 .bus_num = -1,
184                 .setup = mfld_setup,
185                 .get_clk_rate_khz = mfld_get_clk_rate_khz,
186         },
187         [merrifield] = {
188                 .bus_num = -1,
189                 .scl_sda_cfg = &mrfld_config,
190                 .setup = mrfld_setup,
191         },
192         [baytrail] = {
193                 .bus_num = -1,
194                 .scl_sda_cfg = &byt_config,
195         },
196         [haswell] = {
197                 .bus_num = -1,
198                 .scl_sda_cfg = &hsw_config,
199         },
200         [cherrytrail] = {
201                 .bus_num = -1,
202                 .scl_sda_cfg = &byt_config,
203         },
204         [elkhartlake] = {
205                 .bus_num = -1,
206                 .get_clk_rate_khz = ehl_get_clk_rate_khz,
207         },
208         [navi_amd] = {
209                 .bus_num = -1,
210                 .scl_sda_cfg = &navi_amd_config,
211                 .setup =  navi_amd_setup,
212                 .get_clk_rate_khz = navi_amd_get_clk_rate_khz,
213         },
214 };
215
216 #ifdef CONFIG_PM
217 static int i2c_dw_pci_suspend(struct device *dev)
218 {
219         struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
220
221         i_dev->suspended = true;
222         i_dev->disable(i_dev);
223
224         return 0;
225 }
226
227 static int i2c_dw_pci_resume(struct device *dev)
228 {
229         struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
230         int ret;
231
232         ret = i_dev->init(i_dev);
233         i_dev->suspended = false;
234
235         return ret;
236 }
237 #endif
238
239 static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops, i2c_dw_pci_suspend,
240                             i2c_dw_pci_resume, NULL);
241
242 static int i2c_dw_pci_probe(struct pci_dev *pdev,
243                             const struct pci_device_id *id)
244 {
245         struct dw_i2c_dev *dev;
246         struct i2c_adapter *adap;
247         int r;
248         struct dw_pci_controller *controller;
249         struct dw_scl_sda_cfg *cfg;
250
251         if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers)) {
252                 dev_err(&pdev->dev, "%s: invalid driver data %ld\n", __func__,
253                         id->driver_data);
254                 return -EINVAL;
255         }
256
257         controller = &dw_pci_controllers[id->driver_data];
258
259         r = pcim_enable_device(pdev);
260         if (r) {
261                 dev_err(&pdev->dev, "Failed to enable I2C PCI device (%d)\n",
262                         r);
263                 return r;
264         }
265
266         pci_set_master(pdev);
267
268         r = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
269         if (r) {
270                 dev_err(&pdev->dev, "I/O memory remapping failed\n");
271                 return r;
272         }
273
274         dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
275         if (!dev)
276                 return -ENOMEM;
277
278         r = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
279         if (r < 0)
280                 return r;
281
282         dev->get_clk_rate_khz = controller->get_clk_rate_khz;
283         dev->timings.bus_freq_hz = I2C_MAX_FAST_MODE_FREQ;
284         dev->base = pcim_iomap_table(pdev)[0];
285         dev->dev = &pdev->dev;
286         dev->irq = pci_irq_vector(pdev, 0);
287         dev->flags |= controller->flags;
288
289         pci_set_drvdata(pdev, dev);
290
291         if (controller->setup) {
292                 r = controller->setup(pdev, controller);
293                 if (r) {
294                         pci_free_irq_vectors(pdev);
295                         return r;
296                 }
297         }
298
299         i2c_dw_adjust_bus_speed(dev);
300
301         if (has_acpi_companion(&pdev->dev))
302                 i2c_dw_acpi_configure(&pdev->dev);
303
304         r = i2c_dw_validate_speed(dev);
305         if (r) {
306                 pci_free_irq_vectors(pdev);
307                 return r;
308         }
309
310         i2c_dw_configure(dev);
311
312         if (controller->scl_sda_cfg) {
313                 cfg = controller->scl_sda_cfg;
314                 dev->ss_hcnt = cfg->ss_hcnt;
315                 dev->fs_hcnt = cfg->fs_hcnt;
316                 dev->ss_lcnt = cfg->ss_lcnt;
317                 dev->fs_lcnt = cfg->fs_lcnt;
318                 dev->sda_hold_time = cfg->sda_hold;
319         }
320
321         adap = &dev->adapter;
322         adap->owner = THIS_MODULE;
323         adap->class = 0;
324         ACPI_COMPANION_SET(&adap->dev, ACPI_COMPANION(&pdev->dev));
325         adap->nr = controller->bus_num;
326
327         r = i2c_dw_probe(dev);
328         if (r) {
329                 pci_free_irq_vectors(pdev);
330                 return r;
331         }
332
333         if ((dev->flags & MODEL_MASK) == MODEL_AMD_NAVI_GPU) {
334                 r = navi_amd_register_client(dev);
335                 if (r) {
336                         dev_err(dev->dev, "register client failed with %d\n", r);
337                         return r;
338                 }
339         }
340
341         pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
342         pm_runtime_use_autosuspend(&pdev->dev);
343         pm_runtime_put_autosuspend(&pdev->dev);
344         pm_runtime_allow(&pdev->dev);
345
346         return 0;
347 }
348
349 static void i2c_dw_pci_remove(struct pci_dev *pdev)
350 {
351         struct dw_i2c_dev *dev = pci_get_drvdata(pdev);
352
353         dev->disable(dev);
354         pm_runtime_forbid(&pdev->dev);
355         pm_runtime_get_noresume(&pdev->dev);
356
357         i2c_del_adapter(&dev->adapter);
358         devm_free_irq(&pdev->dev, dev->irq, dev);
359         pci_free_irq_vectors(pdev);
360 }
361
362 /* work with hotplug and coldplug */
363 MODULE_ALIAS("i2c_designware-pci");
364
365 static const struct pci_device_id i2_designware_pci_ids[] = {
366         /* Medfield */
367         { PCI_VDEVICE(INTEL, 0x0817), medfield },
368         { PCI_VDEVICE(INTEL, 0x0818), medfield },
369         { PCI_VDEVICE(INTEL, 0x0819), medfield },
370         { PCI_VDEVICE(INTEL, 0x082C), medfield },
371         { PCI_VDEVICE(INTEL, 0x082D), medfield },
372         { PCI_VDEVICE(INTEL, 0x082E), medfield },
373         /* Merrifield */
374         { PCI_VDEVICE(INTEL, 0x1195), merrifield },
375         { PCI_VDEVICE(INTEL, 0x1196), merrifield },
376         /* Baytrail */
377         { PCI_VDEVICE(INTEL, 0x0F41), baytrail },
378         { PCI_VDEVICE(INTEL, 0x0F42), baytrail },
379         { PCI_VDEVICE(INTEL, 0x0F43), baytrail },
380         { PCI_VDEVICE(INTEL, 0x0F44), baytrail },
381         { PCI_VDEVICE(INTEL, 0x0F45), baytrail },
382         { PCI_VDEVICE(INTEL, 0x0F46), baytrail },
383         { PCI_VDEVICE(INTEL, 0x0F47), baytrail },
384         /* Haswell */
385         { PCI_VDEVICE(INTEL, 0x9c61), haswell },
386         { PCI_VDEVICE(INTEL, 0x9c62), haswell },
387         /* Braswell / Cherrytrail */
388         { PCI_VDEVICE(INTEL, 0x22C1), cherrytrail },
389         { PCI_VDEVICE(INTEL, 0x22C2), cherrytrail },
390         { PCI_VDEVICE(INTEL, 0x22C3), cherrytrail },
391         { PCI_VDEVICE(INTEL, 0x22C4), cherrytrail },
392         { PCI_VDEVICE(INTEL, 0x22C5), cherrytrail },
393         { PCI_VDEVICE(INTEL, 0x22C6), cherrytrail },
394         { PCI_VDEVICE(INTEL, 0x22C7), cherrytrail },
395         /* Elkhart Lake (PSE I2C) */
396         { PCI_VDEVICE(INTEL, 0x4bb9), elkhartlake },
397         { PCI_VDEVICE(INTEL, 0x4bba), elkhartlake },
398         { PCI_VDEVICE(INTEL, 0x4bbb), elkhartlake },
399         { PCI_VDEVICE(INTEL, 0x4bbc), elkhartlake },
400         { PCI_VDEVICE(INTEL, 0x4bbd), elkhartlake },
401         { PCI_VDEVICE(INTEL, 0x4bbe), elkhartlake },
402         { PCI_VDEVICE(INTEL, 0x4bbf), elkhartlake },
403         { PCI_VDEVICE(INTEL, 0x4bc0), elkhartlake },
404         { PCI_VDEVICE(ATI,  0x7314), navi_amd },
405         { PCI_VDEVICE(ATI,  0x73a4), navi_amd },
406         { PCI_VDEVICE(ATI,  0x73e4), navi_amd },
407         { PCI_VDEVICE(ATI,  0x73c4), navi_amd },
408         { 0,}
409 };
410 MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids);
411
412 static struct pci_driver dw_i2c_driver = {
413         .name           = DRIVER_NAME,
414         .id_table       = i2_designware_pci_ids,
415         .probe          = i2c_dw_pci_probe,
416         .remove         = i2c_dw_pci_remove,
417         .driver         = {
418                 .pm     = &i2c_dw_pm_ops,
419         },
420 };
421
422 module_pci_driver(dw_i2c_driver);
423
424 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
425 MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
426 MODULE_LICENSE("GPL");