1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82#include <linux/interrupt.h>
83#include <linux/module.h>
84#include <linux/pci.h>
85#include <linux/kernel.h>
86#include <linux/stddef.h>
87#include <linux/delay.h>
88#include <linux/ioport.h>
89#include <linux/init.h>
90#include <linux/i2c.h>
91#include <linux/acpi.h>
92#include <linux/io.h>
93#include <linux/dmi.h>
94#include <linux/slab.h>
95#include <linux/wait.h>
96#include <linux/err.h>
97#include <linux/platform_device.h>
98#include <linux/platform_data/itco_wdt.h>
99#include <linux/pm_runtime.h>
100
101#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
102 defined CONFIG_DMI
103#include <linux/gpio.h>
104#include <linux/i2c-mux-gpio.h>
105#endif
106
107
108#define SMBHSTSTS(p) (0 + (p)->smba)
109#define SMBHSTCNT(p) (2 + (p)->smba)
110#define SMBHSTCMD(p) (3 + (p)->smba)
111#define SMBHSTADD(p) (4 + (p)->smba)
112#define SMBHSTDAT0(p) (5 + (p)->smba)
113#define SMBHSTDAT1(p) (6 + (p)->smba)
114#define SMBBLKDAT(p) (7 + (p)->smba)
115#define SMBPEC(p) (8 + (p)->smba)
116#define SMBAUXSTS(p) (12 + (p)->smba)
117#define SMBAUXCTL(p) (13 + (p)->smba)
118
119
120#define SMBBAR 4
121#define SMBPCICTL 0x004
122#define SMBPCISTS 0x006
123#define SMBHSTCFG 0x040
124#define TCOBASE 0x050
125#define TCOCTL 0x054
126
127#define ACPIBASE 0x040
128#define ACPIBASE_SMI_OFF 0x030
129#define ACPICTRL 0x044
130#define ACPICTRL_EN 0x080
131
132#define SBREG_BAR 0x10
133#define SBREG_SMBCTRL 0xc6000c
134
135
136#define SMBPCISTS_INTS 0x08
137
138
139#define SMBPCICTL_INTDIS 0x0400
140
141
142#define SMBHSTCFG_HST_EN 1
143#define SMBHSTCFG_SMB_SMI_EN 2
144#define SMBHSTCFG_I2C_EN 4
145
146
147#define TCOCTL_EN 0x0100
148
149
150#define SMBAUXCTL_CRC 1
151#define SMBAUXCTL_E32B 2
152
153
154#define MAX_RETRIES 400
155
156
157#define I801_QUICK 0x00
158#define I801_BYTE 0x04
159#define I801_BYTE_DATA 0x08
160#define I801_WORD_DATA 0x0C
161#define I801_PROC_CALL 0x10
162#define I801_BLOCK_DATA 0x14
163#define I801_I2C_BLOCK_DATA 0x18
164
165
166#define SMBHSTCNT_INTREN 0x01
167#define SMBHSTCNT_KILL 0x02
168#define SMBHSTCNT_LAST_BYTE 0x20
169#define SMBHSTCNT_START 0x40
170#define SMBHSTCNT_PEC_EN 0x80
171
172
173#define SMBHSTSTS_BYTE_DONE 0x80
174#define SMBHSTSTS_INUSE_STS 0x40
175#define SMBHSTSTS_SMBALERT_STS 0x20
176#define SMBHSTSTS_FAILED 0x10
177#define SMBHSTSTS_BUS_ERR 0x08
178#define SMBHSTSTS_DEV_ERR 0x04
179#define SMBHSTSTS_INTR 0x02
180#define SMBHSTSTS_HOST_BUSY 0x01
181
182#define STATUS_ERROR_FLAGS (SMBHSTSTS_FAILED | SMBHSTSTS_BUS_ERR | \
183 SMBHSTSTS_DEV_ERR)
184
185#define STATUS_FLAGS (SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INTR | \
186 STATUS_ERROR_FLAGS)
187
188
189#define PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS 0x0f12
190#define PCI_DEVICE_ID_INTEL_DNV_SMBUS 0x19df
191#define PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS 0x1c22
192#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS 0x1d22
193
194#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0 0x1d70
195#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1 0x1d71
196#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2 0x1d72
197#define PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS 0x1e22
198#define PCI_DEVICE_ID_INTEL_AVOTON_SMBUS 0x1f3c
199#define PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS 0x2292
200#define PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS 0x2330
201#define PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS 0x23b0
202#define PCI_DEVICE_ID_INTEL_GEMINILAKE_SMBUS 0x31d4
203#define PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS 0x3b30
204#define PCI_DEVICE_ID_INTEL_BROXTON_SMBUS 0x5ad4
205#define PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS 0x8c22
206#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS 0x8d22
207#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0 0x8d7d
208#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1 0x8d7e
209#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2 0x8d7f
210#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22
211#define PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS 0x9ca2
212#define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS 0x9d23
213#define PCI_DEVICE_ID_INTEL_CANNONLAKE_LP_SMBUS 0x9da3
214#define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS 0xa123
215#define PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS 0xa1a3
216#define PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS 0xa223
217#define PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS 0xa2a3
218#define PCI_DEVICE_ID_INTEL_CANNONLAKE_H_SMBUS 0xa323
219
220struct i801_mux_config {
221 char *gpio_chip;
222 unsigned values[3];
223 int n_values;
224 unsigned classes[3];
225 unsigned gpios[2];
226 int n_gpios;
227};
228
229struct i801_priv {
230 struct i2c_adapter adapter;
231 unsigned long smba;
232 unsigned char original_hstcfg;
233 struct pci_dev *pci_dev;
234 unsigned int features;
235
236
237 wait_queue_head_t waitq;
238 u8 status;
239
240
241 u8 cmd;
242 bool is_read;
243 int count;
244 int len;
245 u8 *data;
246
247#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
248 defined CONFIG_DMI
249 const struct i801_mux_config *mux_drvdata;
250 struct platform_device *mux_pdev;
251#endif
252 struct platform_device *tco_pdev;
253
254
255
256
257
258 bool acpi_reserved;
259 struct mutex acpi_lock;
260};
261
262static struct pci_driver i801_driver;
263
264#define FEATURE_SMBUS_PEC (1 << 0)
265#define FEATURE_BLOCK_BUFFER (1 << 1)
266#define FEATURE_BLOCK_PROC (1 << 2)
267#define FEATURE_I2C_BLOCK_READ (1 << 3)
268#define FEATURE_IRQ (1 << 4)
269
270#define FEATURE_IDF (1 << 15)
271#define FEATURE_TCO (1 << 16)
272
273static const char *i801_feature_names[] = {
274 "SMBus PEC",
275 "Block buffer",
276 "Block process call",
277 "I2C block read",
278 "Interrupt",
279};
280
281static unsigned int disable_features;
282module_param(disable_features, uint, S_IRUGO | S_IWUSR);
283MODULE_PARM_DESC(disable_features, "Disable selected driver features:\n"
284 "\t\t 0x01 disable SMBus PEC\n"
285 "\t\t 0x02 disable the block buffer\n"
286 "\t\t 0x08 disable the I2C block read functionality\n"
287 "\t\t 0x10 don't use interrupts ");
288
289
290
291static int i801_check_pre(struct i801_priv *priv)
292{
293 int status;
294
295 status = inb_p(SMBHSTSTS(priv));
296 if (status & SMBHSTSTS_HOST_BUSY) {
297 dev_err(&priv->pci_dev->dev, "SMBus is busy, can't use it!\n");
298 return -EBUSY;
299 }
300
301 status &= STATUS_FLAGS;
302 if (status) {
303 dev_dbg(&priv->pci_dev->dev, "Clearing status flags (%02x)\n",
304 status);
305 outb_p(status, SMBHSTSTS(priv));
306 status = inb_p(SMBHSTSTS(priv)) & STATUS_FLAGS;
307 if (status) {
308 dev_err(&priv->pci_dev->dev,
309 "Failed clearing status flags (%02x)\n",
310 status);
311 return -EBUSY;
312 }
313 }
314
315 return 0;
316}
317
318
319
320
321
322
323static int i801_check_post(struct i801_priv *priv, int status)
324{
325 int result = 0;
326
327
328
329
330
331
332
333 if (unlikely(status < 0)) {
334 dev_err(&priv->pci_dev->dev, "Transaction timeout\n");
335
336 dev_dbg(&priv->pci_dev->dev, "Terminating the current operation\n");
337 outb_p(inb_p(SMBHSTCNT(priv)) | SMBHSTCNT_KILL,
338 SMBHSTCNT(priv));
339 usleep_range(1000, 2000);
340 outb_p(inb_p(SMBHSTCNT(priv)) & (~SMBHSTCNT_KILL),
341 SMBHSTCNT(priv));
342
343
344 status = inb_p(SMBHSTSTS(priv));
345 if ((status & SMBHSTSTS_HOST_BUSY) ||
346 !(status & SMBHSTSTS_FAILED))
347 dev_err(&priv->pci_dev->dev,
348 "Failed terminating the transaction\n");
349 outb_p(STATUS_FLAGS, SMBHSTSTS(priv));
350 return -ETIMEDOUT;
351 }
352
353 if (status & SMBHSTSTS_FAILED) {
354 result = -EIO;
355 dev_err(&priv->pci_dev->dev, "Transaction failed\n");
356 }
357 if (status & SMBHSTSTS_DEV_ERR) {
358 result = -ENXIO;
359 dev_dbg(&priv->pci_dev->dev, "No response\n");
360 }
361 if (status & SMBHSTSTS_BUS_ERR) {
362 result = -EAGAIN;
363 dev_dbg(&priv->pci_dev->dev, "Lost arbitration\n");
364 }
365
366
367 outb_p(status, SMBHSTSTS(priv));
368
369 return result;
370}
371
372
373static int i801_wait_intr(struct i801_priv *priv)
374{
375 int timeout = 0;
376 int status;
377
378
379 do {
380 usleep_range(250, 500);
381 status = inb_p(SMBHSTSTS(priv));
382 } while (((status & SMBHSTSTS_HOST_BUSY) ||
383 !(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR))) &&
384 (timeout++ < MAX_RETRIES));
385
386 if (timeout > MAX_RETRIES) {
387 dev_dbg(&priv->pci_dev->dev, "INTR Timeout!\n");
388 return -ETIMEDOUT;
389 }
390 return status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR);
391}
392
393
394static int i801_wait_byte_done(struct i801_priv *priv)
395{
396 int timeout = 0;
397 int status;
398
399
400 do {
401 usleep_range(250, 500);
402 status = inb_p(SMBHSTSTS(priv));
403 } while (!(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_BYTE_DONE)) &&
404 (timeout++ < MAX_RETRIES));
405
406 if (timeout > MAX_RETRIES) {
407 dev_dbg(&priv->pci_dev->dev, "BYTE_DONE Timeout!\n");
408 return -ETIMEDOUT;
409 }
410 return status & STATUS_ERROR_FLAGS;
411}
412
413static int i801_transaction(struct i801_priv *priv, int xact)
414{
415 int status;
416 int result;
417 const struct i2c_adapter *adap = &priv->adapter;
418
419 result = i801_check_pre(priv);
420 if (result < 0)
421 return result;
422
423 if (priv->features & FEATURE_IRQ) {
424 outb_p(xact | SMBHSTCNT_INTREN | SMBHSTCNT_START,
425 SMBHSTCNT(priv));
426 result = wait_event_timeout(priv->waitq,
427 (status = priv->status),
428 adap->timeout);
429 if (!result) {
430 status = -ETIMEDOUT;
431 dev_warn(&priv->pci_dev->dev,
432 "Timeout waiting for interrupt!\n");
433 }
434 priv->status = 0;
435 return i801_check_post(priv, status);
436 }
437
438
439
440 outb_p(xact | SMBHSTCNT_START, SMBHSTCNT(priv));
441
442 status = i801_wait_intr(priv);
443 return i801_check_post(priv, status);
444}
445
446static int i801_block_transaction_by_block(struct i801_priv *priv,
447 union i2c_smbus_data *data,
448 char read_write, int hwpec)
449{
450 int i, len;
451 int status;
452
453 inb_p(SMBHSTCNT(priv));
454
455
456 if (read_write == I2C_SMBUS_WRITE) {
457 len = data->block[0];
458 outb_p(len, SMBHSTDAT0(priv));
459 for (i = 0; i < len; i++)
460 outb_p(data->block[i+1], SMBBLKDAT(priv));
461 }
462
463 status = i801_transaction(priv, I801_BLOCK_DATA |
464 (hwpec ? SMBHSTCNT_PEC_EN : 0));
465 if (status)
466 return status;
467
468 if (read_write == I2C_SMBUS_READ) {
469 len = inb_p(SMBHSTDAT0(priv));
470 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX)
471 return -EPROTO;
472
473 data->block[0] = len;
474 for (i = 0; i < len; i++)
475 data->block[i + 1] = inb_p(SMBBLKDAT(priv));
476 }
477 return 0;
478}
479
480static void i801_isr_byte_done(struct i801_priv *priv)
481{
482 if (priv->is_read) {
483
484 if (((priv->cmd & 0x1c) == I801_BLOCK_DATA) &&
485 (priv->count == 0)) {
486 priv->len = inb_p(SMBHSTDAT0(priv));
487 if (priv->len < 1 || priv->len > I2C_SMBUS_BLOCK_MAX) {
488 dev_err(&priv->pci_dev->dev,
489 "Illegal SMBus block read size %d\n",
490 priv->len);
491
492 priv->len = I2C_SMBUS_BLOCK_MAX;
493 } else {
494 dev_dbg(&priv->pci_dev->dev,
495 "SMBus block read size is %d\n",
496 priv->len);
497 }
498 priv->data[-1] = priv->len;
499 }
500
501
502 if (priv->count < priv->len)
503 priv->data[priv->count++] = inb(SMBBLKDAT(priv));
504 else
505 dev_dbg(&priv->pci_dev->dev,
506 "Discarding extra byte on block read\n");
507
508
509 if (priv->count == priv->len - 1)
510 outb_p(priv->cmd | SMBHSTCNT_LAST_BYTE,
511 SMBHSTCNT(priv));
512 } else if (priv->count < priv->len - 1) {
513
514 outb_p(priv->data[++priv->count], SMBBLKDAT(priv));
515 }
516
517
518 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
519}
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535static irqreturn_t i801_isr(int irq, void *dev_id)
536{
537 struct i801_priv *priv = dev_id;
538 u16 pcists;
539 u8 status;
540
541
542 pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
543 if (!(pcists & SMBPCISTS_INTS))
544 return IRQ_NONE;
545
546 status = inb_p(SMBHSTSTS(priv));
547 if (status != 0x42)
548 dev_dbg(&priv->pci_dev->dev, "irq: status = %02x\n", status);
549
550 if (status & SMBHSTSTS_BYTE_DONE)
551 i801_isr_byte_done(priv);
552
553
554
555
556
557 status &= SMBHSTSTS_INTR | STATUS_ERROR_FLAGS;
558 if (status) {
559 outb_p(status, SMBHSTSTS(priv));
560 priv->status |= status;
561 wake_up(&priv->waitq);
562 }
563
564 return IRQ_HANDLED;
565}
566
567
568
569
570
571
572static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
573 union i2c_smbus_data *data,
574 char read_write, int command,
575 int hwpec)
576{
577 int i, len;
578 int smbcmd;
579 int status;
580 int result;
581 const struct i2c_adapter *adap = &priv->adapter;
582
583 result = i801_check_pre(priv);
584 if (result < 0)
585 return result;
586
587 len = data->block[0];
588
589 if (read_write == I2C_SMBUS_WRITE) {
590 outb_p(len, SMBHSTDAT0(priv));
591 outb_p(data->block[1], SMBBLKDAT(priv));
592 }
593
594 if (command == I2C_SMBUS_I2C_BLOCK_DATA &&
595 read_write == I2C_SMBUS_READ)
596 smbcmd = I801_I2C_BLOCK_DATA;
597 else
598 smbcmd = I801_BLOCK_DATA;
599
600 if (priv->features & FEATURE_IRQ) {
601 priv->is_read = (read_write == I2C_SMBUS_READ);
602 if (len == 1 && priv->is_read)
603 smbcmd |= SMBHSTCNT_LAST_BYTE;
604 priv->cmd = smbcmd | SMBHSTCNT_INTREN;
605 priv->len = len;
606 priv->count = 0;
607 priv->data = &data->block[1];
608
609 outb_p(priv->cmd | SMBHSTCNT_START, SMBHSTCNT(priv));
610 result = wait_event_timeout(priv->waitq,
611 (status = priv->status),
612 adap->timeout);
613 if (!result) {
614 status = -ETIMEDOUT;
615 dev_warn(&priv->pci_dev->dev,
616 "Timeout waiting for interrupt!\n");
617 }
618 priv->status = 0;
619 return i801_check_post(priv, status);
620 }
621
622 for (i = 1; i <= len; i++) {
623 if (i == len && read_write == I2C_SMBUS_READ)
624 smbcmd |= SMBHSTCNT_LAST_BYTE;
625 outb_p(smbcmd, SMBHSTCNT(priv));
626
627 if (i == 1)
628 outb_p(inb(SMBHSTCNT(priv)) | SMBHSTCNT_START,
629 SMBHSTCNT(priv));
630
631 status = i801_wait_byte_done(priv);
632 if (status)
633 goto exit;
634
635 if (i == 1 && read_write == I2C_SMBUS_READ
636 && command != I2C_SMBUS_I2C_BLOCK_DATA) {
637 len = inb_p(SMBHSTDAT0(priv));
638 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX) {
639 dev_err(&priv->pci_dev->dev,
640 "Illegal SMBus block read size %d\n",
641 len);
642
643 while (inb_p(SMBHSTSTS(priv)) &
644 SMBHSTSTS_HOST_BUSY)
645 outb_p(SMBHSTSTS_BYTE_DONE,
646 SMBHSTSTS(priv));
647 outb_p(SMBHSTSTS_INTR, SMBHSTSTS(priv));
648 return -EPROTO;
649 }
650 data->block[0] = len;
651 }
652
653
654 if (read_write == I2C_SMBUS_READ)
655 data->block[i] = inb_p(SMBBLKDAT(priv));
656 if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
657 outb_p(data->block[i+1], SMBBLKDAT(priv));
658
659
660 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
661 }
662
663 status = i801_wait_intr(priv);
664exit:
665 return i801_check_post(priv, status);
666}
667
668static int i801_set_block_buffer_mode(struct i801_priv *priv)
669{
670 outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_E32B, SMBAUXCTL(priv));
671 if ((inb_p(SMBAUXCTL(priv)) & SMBAUXCTL_E32B) == 0)
672 return -EIO;
673 return 0;
674}
675
676
677static int i801_block_transaction(struct i801_priv *priv,
678 union i2c_smbus_data *data, char read_write,
679 int command, int hwpec)
680{
681 int result = 0;
682 unsigned char hostc;
683
684 if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
685 if (read_write == I2C_SMBUS_WRITE) {
686
687 pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &hostc);
688 pci_write_config_byte(priv->pci_dev, SMBHSTCFG,
689 hostc | SMBHSTCFG_I2C_EN);
690 } else if (!(priv->features & FEATURE_I2C_BLOCK_READ)) {
691 dev_err(&priv->pci_dev->dev,
692 "I2C block read is unsupported!\n");
693 return -EOPNOTSUPP;
694 }
695 }
696
697 if (read_write == I2C_SMBUS_WRITE
698 || command == I2C_SMBUS_I2C_BLOCK_DATA) {
699 if (data->block[0] < 1)
700 data->block[0] = 1;
701 if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
702 data->block[0] = I2C_SMBUS_BLOCK_MAX;
703 } else {
704 data->block[0] = 32;
705 }
706
707
708
709
710 if ((priv->features & FEATURE_BLOCK_BUFFER)
711 && command != I2C_SMBUS_I2C_BLOCK_DATA
712 && i801_set_block_buffer_mode(priv) == 0)
713 result = i801_block_transaction_by_block(priv, data,
714 read_write, hwpec);
715 else
716 result = i801_block_transaction_byte_by_byte(priv, data,
717 read_write,
718 command, hwpec);
719
720 if (command == I2C_SMBUS_I2C_BLOCK_DATA
721 && read_write == I2C_SMBUS_WRITE) {
722
723 pci_write_config_byte(priv->pci_dev, SMBHSTCFG, hostc);
724 }
725 return result;
726}
727
728
729static s32 i801_access(struct i2c_adapter *adap, u16 addr,
730 unsigned short flags, char read_write, u8 command,
731 int size, union i2c_smbus_data *data)
732{
733 int hwpec;
734 int block = 0;
735 int ret = 0, xact = 0;
736 struct i801_priv *priv = i2c_get_adapdata(adap);
737
738 mutex_lock(&priv->acpi_lock);
739 if (priv->acpi_reserved) {
740 mutex_unlock(&priv->acpi_lock);
741 return -EBUSY;
742 }
743
744 pm_runtime_get_sync(&priv->pci_dev->dev);
745
746 hwpec = (priv->features & FEATURE_SMBUS_PEC) && (flags & I2C_CLIENT_PEC)
747 && size != I2C_SMBUS_QUICK
748 && size != I2C_SMBUS_I2C_BLOCK_DATA;
749
750 switch (size) {
751 case I2C_SMBUS_QUICK:
752 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
753 SMBHSTADD(priv));
754 xact = I801_QUICK;
755 break;
756 case I2C_SMBUS_BYTE:
757 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
758 SMBHSTADD(priv));
759 if (read_write == I2C_SMBUS_WRITE)
760 outb_p(command, SMBHSTCMD(priv));
761 xact = I801_BYTE;
762 break;
763 case I2C_SMBUS_BYTE_DATA:
764 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
765 SMBHSTADD(priv));
766 outb_p(command, SMBHSTCMD(priv));
767 if (read_write == I2C_SMBUS_WRITE)
768 outb_p(data->byte, SMBHSTDAT0(priv));
769 xact = I801_BYTE_DATA;
770 break;
771 case I2C_SMBUS_WORD_DATA:
772 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
773 SMBHSTADD(priv));
774 outb_p(command, SMBHSTCMD(priv));
775 if (read_write == I2C_SMBUS_WRITE) {
776 outb_p(data->word & 0xff, SMBHSTDAT0(priv));
777 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1(priv));
778 }
779 xact = I801_WORD_DATA;
780 break;
781 case I2C_SMBUS_BLOCK_DATA:
782 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
783 SMBHSTADD(priv));
784 outb_p(command, SMBHSTCMD(priv));
785 block = 1;
786 break;
787 case I2C_SMBUS_I2C_BLOCK_DATA:
788
789
790 outb_p((addr & 0x7f) << 1, SMBHSTADD(priv));
791 if (read_write == I2C_SMBUS_READ) {
792
793
794 outb_p(command, SMBHSTDAT1(priv));
795 } else
796 outb_p(command, SMBHSTCMD(priv));
797 block = 1;
798 break;
799 default:
800 dev_err(&priv->pci_dev->dev, "Unsupported transaction %d\n",
801 size);
802 ret = -EOPNOTSUPP;
803 goto out;
804 }
805
806 if (hwpec)
807 outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_CRC, SMBAUXCTL(priv));
808 else
809 outb_p(inb_p(SMBAUXCTL(priv)) & (~SMBAUXCTL_CRC),
810 SMBAUXCTL(priv));
811
812 if (block)
813 ret = i801_block_transaction(priv, data, read_write, size,
814 hwpec);
815 else
816 ret = i801_transaction(priv, xact);
817
818
819
820
821 if (hwpec || block)
822 outb_p(inb_p(SMBAUXCTL(priv)) &
823 ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
824
825 if (block)
826 goto out;
827 if (ret)
828 goto out;
829 if ((read_write == I2C_SMBUS_WRITE) || (xact == I801_QUICK))
830 goto out;
831
832 switch (xact & 0x7f) {
833 case I801_BYTE:
834 case I801_BYTE_DATA:
835 data->byte = inb_p(SMBHSTDAT0(priv));
836 break;
837 case I801_WORD_DATA:
838 data->word = inb_p(SMBHSTDAT0(priv)) +
839 (inb_p(SMBHSTDAT1(priv)) << 8);
840 break;
841 }
842
843out:
844 pm_runtime_mark_last_busy(&priv->pci_dev->dev);
845 pm_runtime_put_autosuspend(&priv->pci_dev->dev);
846 mutex_unlock(&priv->acpi_lock);
847 return ret;
848}
849
850
851static u32 i801_func(struct i2c_adapter *adapter)
852{
853 struct i801_priv *priv = i2c_get_adapdata(adapter);
854
855 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
856 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
857 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK |
858 ((priv->features & FEATURE_SMBUS_PEC) ? I2C_FUNC_SMBUS_PEC : 0) |
859 ((priv->features & FEATURE_I2C_BLOCK_READ) ?
860 I2C_FUNC_SMBUS_READ_I2C_BLOCK : 0);
861}
862
863static const struct i2c_algorithm smbus_algorithm = {
864 .smbus_xfer = i801_access,
865 .functionality = i801_func,
866};
867
868static const struct pci_device_id i801_ids[] = {
869 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_3) },
870 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_3) },
871 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_2) },
872 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_3) },
873 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_3) },
874 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_3) },
875 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_4) },
876 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_16) },
877 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_17) },
878 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_17) },
879 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_5) },
880 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_6) },
881 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EP80579_1) },
882 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_4) },
883 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_5) },
884 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS) },
885 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS) },
886 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS) },
887 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0) },
888 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1) },
889 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2) },
890 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS) },
891 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS) },
892 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS) },
893 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS) },
894 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMBUS) },
895 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS) },
896 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0) },
897 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1) },
898 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2) },
899 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS) },
900 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_GEMINILAKE_SMBUS) },
901 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS) },
902 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS) },
903 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS) },
904 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS) },
905 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DNV_SMBUS) },
906 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS) },
907 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS) },
908 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS) },
909 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CANNONLAKE_H_SMBUS) },
910 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CANNONLAKE_LP_SMBUS) },
911 { 0, }
912};
913
914MODULE_DEVICE_TABLE(pci, i801_ids);
915
916#if defined CONFIG_X86 && defined CONFIG_DMI
917static unsigned char apanel_addr;
918
919
920static __init const void __iomem *bios_signature(const void __iomem *bios)
921{
922 ssize_t offset;
923 const unsigned char signature[] = "FJKEYINF";
924
925 for (offset = 0; offset < 0x10000; offset += 0x10) {
926 if (check_signature(bios + offset, signature,
927 sizeof(signature)-1))
928 return bios + offset;
929 }
930 return NULL;
931}
932
933static void __init input_apanel_init(void)
934{
935 void __iomem *bios;
936 const void __iomem *p;
937
938 bios = ioremap(0xF0000, 0x10000);
939 p = bios_signature(bios);
940 if (p) {
941
942 apanel_addr = readb(p + 8 + 3) >> 1;
943 }
944 iounmap(bios);
945}
946
947struct dmi_onboard_device_info {
948 const char *name;
949 u8 type;
950 unsigned short i2c_addr;
951 const char *i2c_type;
952};
953
954static const struct dmi_onboard_device_info dmi_devices[] = {
955 { "Syleus", DMI_DEV_TYPE_OTHER, 0x73, "fscsyl" },
956 { "Hermes", DMI_DEV_TYPE_OTHER, 0x73, "fscher" },
957 { "Hades", DMI_DEV_TYPE_OTHER, 0x73, "fschds" },
958};
959
960static void dmi_check_onboard_device(u8 type, const char *name,
961 struct i2c_adapter *adap)
962{
963 int i;
964 struct i2c_board_info info;
965
966 for (i = 0; i < ARRAY_SIZE(dmi_devices); i++) {
967
968 if ((type & ~0x80) != dmi_devices[i].type)
969 continue;
970 if (strcasecmp(name, dmi_devices[i].name))
971 continue;
972
973 memset(&info, 0, sizeof(struct i2c_board_info));
974 info.addr = dmi_devices[i].i2c_addr;
975 strlcpy(info.type, dmi_devices[i].i2c_type, I2C_NAME_SIZE);
976 i2c_new_device(adap, &info);
977 break;
978 }
979}
980
981
982
983
984static void dmi_check_onboard_devices(const struct dmi_header *dm, void *adap)
985{
986 int i, count;
987
988 if (dm->type != 10)
989 return;
990
991 count = (dm->length - sizeof(struct dmi_header)) / 2;
992 for (i = 0; i < count; i++) {
993 const u8 *d = (char *)(dm + 1) + (i * 2);
994 const char *name = ((char *) dm) + dm->length;
995 u8 type = d[0];
996 u8 s = d[1];
997
998 if (!s)
999 continue;
1000 s--;
1001 while (s > 0 && name[0]) {
1002 name += strlen(name) + 1;
1003 s--;
1004 }
1005 if (name[0] == 0)
1006 continue;
1007
1008 dmi_check_onboard_device(type, name, adap);
1009 }
1010}
1011
1012
1013static void i801_probe_optional_slaves(struct i801_priv *priv)
1014{
1015
1016 if (priv->features & FEATURE_IDF)
1017 return;
1018
1019 if (apanel_addr) {
1020 struct i2c_board_info info;
1021
1022 memset(&info, 0, sizeof(struct i2c_board_info));
1023 info.addr = apanel_addr;
1024 strlcpy(info.type, "fujitsu_apanel", I2C_NAME_SIZE);
1025 i2c_new_device(&priv->adapter, &info);
1026 }
1027
1028 if (dmi_name_in_vendors("FUJITSU"))
1029 dmi_walk(dmi_check_onboard_devices, &priv->adapter);
1030}
1031#else
1032static void __init input_apanel_init(void) {}
1033static void i801_probe_optional_slaves(struct i801_priv *priv) {}
1034#endif
1035
1036#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
1037 defined CONFIG_DMI
1038static struct i801_mux_config i801_mux_config_asus_z8_d12 = {
1039 .gpio_chip = "gpio_ich",
1040 .values = { 0x02, 0x03 },
1041 .n_values = 2,
1042 .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD },
1043 .gpios = { 52, 53 },
1044 .n_gpios = 2,
1045};
1046
1047static struct i801_mux_config i801_mux_config_asus_z8_d18 = {
1048 .gpio_chip = "gpio_ich",
1049 .values = { 0x02, 0x03, 0x01 },
1050 .n_values = 3,
1051 .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD, I2C_CLASS_SPD },
1052 .gpios = { 52, 53 },
1053 .n_gpios = 2,
1054};
1055
1056static const struct dmi_system_id mux_dmi_table[] = {
1057 {
1058 .matches = {
1059 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1060 DMI_MATCH(DMI_BOARD_NAME, "Z8NA-D6(C)"),
1061 },
1062 .driver_data = &i801_mux_config_asus_z8_d12,
1063 },
1064 {
1065 .matches = {
1066 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1067 DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)E-D12(X)"),
1068 },
1069 .driver_data = &i801_mux_config_asus_z8_d12,
1070 },
1071 {
1072 .matches = {
1073 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1074 DMI_MATCH(DMI_BOARD_NAME, "Z8NH-D12"),
1075 },
1076 .driver_data = &i801_mux_config_asus_z8_d12,
1077 },
1078 {
1079 .matches = {
1080 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1081 DMI_MATCH(DMI_BOARD_NAME, "Z8PH-D12/IFB"),
1082 },
1083 .driver_data = &i801_mux_config_asus_z8_d12,
1084 },
1085 {
1086 .matches = {
1087 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1088 DMI_MATCH(DMI_BOARD_NAME, "Z8NR-D12"),
1089 },
1090 .driver_data = &i801_mux_config_asus_z8_d12,
1091 },
1092 {
1093 .matches = {
1094 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1095 DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)H-D12"),
1096 },
1097 .driver_data = &i801_mux_config_asus_z8_d12,
1098 },
1099 {
1100 .matches = {
1101 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1102 DMI_MATCH(DMI_BOARD_NAME, "Z8PG-D18"),
1103 },
1104 .driver_data = &i801_mux_config_asus_z8_d18,
1105 },
1106 {
1107 .matches = {
1108 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1109 DMI_MATCH(DMI_BOARD_NAME, "Z8PE-D18"),
1110 },
1111 .driver_data = &i801_mux_config_asus_z8_d18,
1112 },
1113 {
1114 .matches = {
1115 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1116 DMI_MATCH(DMI_BOARD_NAME, "Z8PS-D12"),
1117 },
1118 .driver_data = &i801_mux_config_asus_z8_d12,
1119 },
1120 { }
1121};
1122
1123
1124static int i801_add_mux(struct i801_priv *priv)
1125{
1126 struct device *dev = &priv->adapter.dev;
1127 const struct i801_mux_config *mux_config;
1128 struct i2c_mux_gpio_platform_data gpio_data;
1129 int err;
1130
1131 if (!priv->mux_drvdata)
1132 return 0;
1133 mux_config = priv->mux_drvdata;
1134
1135
1136 memset(&gpio_data, 0, sizeof(struct i2c_mux_gpio_platform_data));
1137 gpio_data.parent = priv->adapter.nr;
1138 gpio_data.values = mux_config->values;
1139 gpio_data.n_values = mux_config->n_values;
1140 gpio_data.classes = mux_config->classes;
1141 gpio_data.gpio_chip = mux_config->gpio_chip;
1142 gpio_data.gpios = mux_config->gpios;
1143 gpio_data.n_gpios = mux_config->n_gpios;
1144 gpio_data.idle = I2C_MUX_GPIO_NO_IDLE;
1145
1146
1147 priv->mux_pdev = platform_device_register_data(dev, "i2c-mux-gpio",
1148 PLATFORM_DEVID_AUTO, &gpio_data,
1149 sizeof(struct i2c_mux_gpio_platform_data));
1150 if (IS_ERR(priv->mux_pdev)) {
1151 err = PTR_ERR(priv->mux_pdev);
1152 priv->mux_pdev = NULL;
1153 dev_err(dev, "Failed to register i2c-mux-gpio device\n");
1154 return err;
1155 }
1156
1157 return 0;
1158}
1159
1160static void i801_del_mux(struct i801_priv *priv)
1161{
1162 if (priv->mux_pdev)
1163 platform_device_unregister(priv->mux_pdev);
1164}
1165
1166static unsigned int i801_get_adapter_class(struct i801_priv *priv)
1167{
1168 const struct dmi_system_id *id;
1169 const struct i801_mux_config *mux_config;
1170 unsigned int class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
1171 int i;
1172
1173 id = dmi_first_match(mux_dmi_table);
1174 if (id) {
1175
1176 mux_config = id->driver_data;
1177 for (i = 0; i < mux_config->n_values; i++)
1178 class &= ~mux_config->classes[i];
1179
1180
1181 priv->mux_drvdata = mux_config;
1182 }
1183
1184 return class;
1185}
1186#else
1187static inline int i801_add_mux(struct i801_priv *priv) { return 0; }
1188static inline void i801_del_mux(struct i801_priv *priv) { }
1189
1190static inline unsigned int i801_get_adapter_class(struct i801_priv *priv)
1191{
1192 return I2C_CLASS_HWMON | I2C_CLASS_SPD;
1193}
1194#endif
1195
1196static const struct itco_wdt_platform_data tco_platform_data = {
1197 .name = "Intel PCH",
1198 .version = 4,
1199};
1200
1201static DEFINE_SPINLOCK(p2sb_spinlock);
1202
1203static void i801_add_tco(struct i801_priv *priv)
1204{
1205 struct pci_dev *pci_dev = priv->pci_dev;
1206 struct resource tco_res[3], *res;
1207 struct platform_device *pdev;
1208 unsigned int devfn;
1209 u32 tco_base, tco_ctl;
1210 u32 base_addr, ctrl_val;
1211 u64 base64_addr;
1212
1213 if (!(priv->features & FEATURE_TCO))
1214 return;
1215
1216 pci_read_config_dword(pci_dev, TCOBASE, &tco_base);
1217 pci_read_config_dword(pci_dev, TCOCTL, &tco_ctl);
1218 if (!(tco_ctl & TCOCTL_EN))
1219 return;
1220
1221 memset(tco_res, 0, sizeof(tco_res));
1222
1223 res = &tco_res[ICH_RES_IO_TCO];
1224 res->start = tco_base & ~1;
1225 res->end = res->start + 32 - 1;
1226 res->flags = IORESOURCE_IO;
1227
1228
1229
1230
1231 devfn = PCI_DEVFN(PCI_SLOT(pci_dev->devfn), 2);
1232 pci_bus_read_config_dword(pci_dev->bus, devfn, ACPIBASE, &base_addr);
1233
1234 res = &tco_res[ICH_RES_IO_SMI];
1235 res->start = (base_addr & ~1) + ACPIBASE_SMI_OFF;
1236 res->end = res->start + 3;
1237 res->flags = IORESOURCE_IO;
1238
1239
1240
1241
1242 pci_bus_read_config_dword(pci_dev->bus, devfn, ACPICTRL, &ctrl_val);
1243 ctrl_val |= ACPICTRL_EN;
1244 pci_bus_write_config_dword(pci_dev->bus, devfn, ACPICTRL, ctrl_val);
1245
1246
1247
1248
1249
1250
1251
1252 spin_lock(&p2sb_spinlock);
1253
1254 devfn = PCI_DEVFN(PCI_SLOT(pci_dev->devfn), 1);
1255
1256
1257 pci_bus_write_config_byte(pci_dev->bus, devfn, 0xe1, 0x0);
1258
1259 pci_bus_read_config_dword(pci_dev->bus, devfn, SBREG_BAR, &base_addr);
1260 base64_addr = base_addr & 0xfffffff0;
1261
1262 pci_bus_read_config_dword(pci_dev->bus, devfn, SBREG_BAR + 0x4, &base_addr);
1263 base64_addr |= (u64)base_addr << 32;
1264
1265
1266 pci_bus_write_config_byte(pci_dev->bus, devfn, 0xe1, 0x1);
1267 spin_unlock(&p2sb_spinlock);
1268
1269 res = &tco_res[ICH_RES_MEM_OFF];
1270 res->start = (resource_size_t)base64_addr + SBREG_SMBCTRL;
1271 res->end = res->start + 3;
1272 res->flags = IORESOURCE_MEM;
1273
1274 pdev = platform_device_register_resndata(&pci_dev->dev, "iTCO_wdt", -1,
1275 tco_res, 3, &tco_platform_data,
1276 sizeof(tco_platform_data));
1277 if (IS_ERR(pdev)) {
1278 dev_warn(&pci_dev->dev, "failed to create iTCO device\n");
1279 return;
1280 }
1281
1282 priv->tco_pdev = pdev;
1283}
1284
1285#ifdef CONFIG_ACPI
1286static acpi_status
1287i801_acpi_io_handler(u32 function, acpi_physical_address address, u32 bits,
1288 u64 *value, void *handler_context, void *region_context)
1289{
1290 struct i801_priv *priv = handler_context;
1291 struct pci_dev *pdev = priv->pci_dev;
1292 acpi_status status;
1293
1294
1295
1296
1297
1298
1299 mutex_lock(&priv->acpi_lock);
1300
1301 if (!priv->acpi_reserved) {
1302 priv->acpi_reserved = true;
1303
1304 dev_warn(&pdev->dev, "BIOS is accessing SMBus registers\n");
1305 dev_warn(&pdev->dev, "Driver SMBus register access inhibited\n");
1306
1307
1308
1309
1310
1311 pm_runtime_get_sync(&pdev->dev);
1312 }
1313
1314 if ((function & ACPI_IO_MASK) == ACPI_READ)
1315 status = acpi_os_read_port(address, (u32 *)value, bits);
1316 else
1317 status = acpi_os_write_port(address, (u32)*value, bits);
1318
1319 mutex_unlock(&priv->acpi_lock);
1320
1321 return status;
1322}
1323
1324static int i801_acpi_probe(struct i801_priv *priv)
1325{
1326 struct acpi_device *adev;
1327 acpi_status status;
1328
1329 adev = ACPI_COMPANION(&priv->pci_dev->dev);
1330 if (adev) {
1331 status = acpi_install_address_space_handler(adev->handle,
1332 ACPI_ADR_SPACE_SYSTEM_IO, i801_acpi_io_handler,
1333 NULL, priv);
1334 if (ACPI_SUCCESS(status))
1335 return 0;
1336 }
1337
1338 return acpi_check_resource_conflict(&priv->pci_dev->resource[SMBBAR]);
1339}
1340
1341static void i801_acpi_remove(struct i801_priv *priv)
1342{
1343 struct acpi_device *adev;
1344
1345 adev = ACPI_COMPANION(&priv->pci_dev->dev);
1346 if (!adev)
1347 return;
1348
1349 acpi_remove_address_space_handler(adev->handle,
1350 ACPI_ADR_SPACE_SYSTEM_IO, i801_acpi_io_handler);
1351
1352 mutex_lock(&priv->acpi_lock);
1353 if (priv->acpi_reserved)
1354 pm_runtime_put(&priv->pci_dev->dev);
1355 mutex_unlock(&priv->acpi_lock);
1356}
1357#else
1358static inline int i801_acpi_probe(struct i801_priv *priv) { return 0; }
1359static inline void i801_acpi_remove(struct i801_priv *priv) { }
1360#endif
1361
1362static int i801_probe(struct pci_dev *dev, const struct pci_device_id *id)
1363{
1364 unsigned char temp;
1365 int err, i;
1366 struct i801_priv *priv;
1367
1368 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1369 if (!priv)
1370 return -ENOMEM;
1371
1372 i2c_set_adapdata(&priv->adapter, priv);
1373 priv->adapter.owner = THIS_MODULE;
1374 priv->adapter.class = i801_get_adapter_class(priv);
1375 priv->adapter.algo = &smbus_algorithm;
1376 priv->adapter.dev.parent = &dev->dev;
1377 device_rh_alloc(&priv->adapter.dev);
1378 ACPI_COMPANION_SET(&priv->adapter.dev, ACPI_COMPANION(&dev->dev));
1379 priv->adapter.retries = 3;
1380 mutex_init(&priv->acpi_lock);
1381
1382 priv->pci_dev = dev;
1383 switch (dev->device) {
1384 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS:
1385 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS:
1386 case PCI_DEVICE_ID_INTEL_CANNONLAKE_H_SMBUS:
1387 case PCI_DEVICE_ID_INTEL_CANNONLAKE_LP_SMBUS:
1388 case PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS:
1389 case PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS:
1390 case PCI_DEVICE_ID_INTEL_DNV_SMBUS:
1391 case PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS:
1392 priv->features |= FEATURE_I2C_BLOCK_READ;
1393 priv->features |= FEATURE_IRQ;
1394 priv->features |= FEATURE_SMBUS_PEC;
1395 priv->features |= FEATURE_BLOCK_BUFFER;
1396 priv->features |= FEATURE_TCO;
1397 break;
1398
1399 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0:
1400 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1:
1401 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2:
1402 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0:
1403 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1:
1404 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2:
1405 priv->features |= FEATURE_IDF;
1406
1407 default:
1408 priv->features |= FEATURE_I2C_BLOCK_READ;
1409 priv->features |= FEATURE_IRQ;
1410
1411 case PCI_DEVICE_ID_INTEL_82801DB_3:
1412 priv->features |= FEATURE_SMBUS_PEC;
1413 priv->features |= FEATURE_BLOCK_BUFFER;
1414
1415 case PCI_DEVICE_ID_INTEL_82801CA_3:
1416 case PCI_DEVICE_ID_INTEL_82801BA_2:
1417 case PCI_DEVICE_ID_INTEL_82801AB_3:
1418 case PCI_DEVICE_ID_INTEL_82801AA_3:
1419 break;
1420 }
1421
1422
1423 for (i = 0; i < ARRAY_SIZE(i801_feature_names); i++) {
1424 if (priv->features & disable_features & (1 << i))
1425 dev_notice(&dev->dev, "%s disabled by user\n",
1426 i801_feature_names[i]);
1427 }
1428 priv->features &= ~disable_features;
1429
1430 err = pci_enable_device(dev);
1431 if (err) {
1432 dev_err(&dev->dev, "Failed to enable SMBus PCI device (%d)\n",
1433 err);
1434 goto exit;
1435 }
1436
1437
1438 priv->smba = pci_resource_start(dev, SMBBAR);
1439 if (!priv->smba) {
1440 dev_err(&dev->dev, "SMBus base address uninitialized, "
1441 "upgrade BIOS\n");
1442 err = -ENODEV;
1443 goto exit;
1444 }
1445
1446 if (i801_acpi_probe(priv)) {
1447 err = -ENODEV;
1448 goto exit;
1449 }
1450
1451 err = pci_request_region(dev, SMBBAR, i801_driver.name);
1452 if (err) {
1453 dev_err(&dev->dev, "Failed to request SMBus region "
1454 "0x%lx-0x%Lx\n", priv->smba,
1455 (unsigned long long)pci_resource_end(dev, SMBBAR));
1456 i801_acpi_remove(priv);
1457 goto exit;
1458 }
1459
1460 pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &temp);
1461 priv->original_hstcfg = temp;
1462 temp &= ~SMBHSTCFG_I2C_EN;
1463 if (!(temp & SMBHSTCFG_HST_EN)) {
1464 dev_info(&dev->dev, "Enabling SMBus device\n");
1465 temp |= SMBHSTCFG_HST_EN;
1466 }
1467 pci_write_config_byte(priv->pci_dev, SMBHSTCFG, temp);
1468
1469 if (temp & SMBHSTCFG_SMB_SMI_EN) {
1470 dev_dbg(&dev->dev, "SMBus using interrupt SMI#\n");
1471
1472 priv->features &= ~FEATURE_IRQ;
1473 }
1474
1475
1476 if (priv->features & (FEATURE_SMBUS_PEC | FEATURE_BLOCK_BUFFER))
1477 outb_p(inb_p(SMBAUXCTL(priv)) &
1478 ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
1479
1480
1481 priv->adapter.timeout = HZ / 5;
1482
1483 if (priv->features & FEATURE_IRQ) {
1484 u16 pcictl, pcists;
1485
1486
1487 pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
1488 if (pcists & SMBPCISTS_INTS)
1489 dev_warn(&dev->dev, "An interrupt is pending!\n");
1490
1491
1492 pci_read_config_word(priv->pci_dev, SMBPCICTL, &pcictl);
1493 if (pcictl & SMBPCICTL_INTDIS) {
1494 dev_info(&dev->dev, "Interrupts are disabled\n");
1495 priv->features &= ~FEATURE_IRQ;
1496 }
1497 }
1498
1499 if (priv->features & FEATURE_IRQ) {
1500 init_waitqueue_head(&priv->waitq);
1501
1502 err = request_irq(dev->irq, i801_isr, IRQF_SHARED,
1503 i801_driver.name, priv);
1504 if (err) {
1505 dev_err(&dev->dev, "Failed to allocate irq %d: %d\n",
1506 dev->irq, err);
1507 priv->features &= ~FEATURE_IRQ;
1508 }
1509 }
1510 dev_info(&dev->dev, "SMBus using %s\n",
1511 priv->features & FEATURE_IRQ ? "PCI interrupt" : "polling");
1512
1513 i801_add_tco(priv);
1514
1515 snprintf(priv->adapter.name, sizeof(priv->adapter.name),
1516 "SMBus I801 adapter at %04lx", priv->smba);
1517 err = i2c_add_adapter(&priv->adapter);
1518 if (err) {
1519 dev_err(&dev->dev, "Failed to add SMBus adapter\n");
1520 i801_acpi_remove(priv);
1521 goto exit_free_irq;
1522 }
1523
1524 i801_probe_optional_slaves(priv);
1525
1526 i801_add_mux(priv);
1527
1528 pci_set_drvdata(dev, priv);
1529
1530 pm_runtime_set_autosuspend_delay(&dev->dev, 1000);
1531 pm_runtime_use_autosuspend(&dev->dev);
1532 pm_runtime_put_autosuspend(&dev->dev);
1533 pm_runtime_allow(&dev->dev);
1534
1535 return 0;
1536
1537exit_free_irq:
1538 if (priv->features & FEATURE_IRQ)
1539 free_irq(dev->irq, priv);
1540 pci_release_region(dev, SMBBAR);
1541exit:
1542 kfree(priv);
1543 return err;
1544}
1545
1546static void i801_remove(struct pci_dev *dev)
1547{
1548 struct i801_priv *priv = pci_get_drvdata(dev);
1549
1550 pm_runtime_forbid(&dev->dev);
1551 pm_runtime_get_noresume(&dev->dev);
1552
1553 i801_del_mux(priv);
1554 i2c_del_adapter(&priv->adapter);
1555 i801_acpi_remove(priv);
1556 pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
1557
1558 if (priv->features & FEATURE_IRQ)
1559 free_irq(dev->irq, priv);
1560 pci_release_region(dev, SMBBAR);
1561
1562 platform_device_unregister(priv->tco_pdev);
1563
1564 kfree(priv);
1565
1566
1567
1568
1569}
1570
1571#ifdef CONFIG_PM
1572static int i801_suspend(struct pci_dev *dev, pm_message_t mesg)
1573{
1574 struct i801_priv *priv = pci_get_drvdata(dev);
1575
1576 pci_save_state(dev);
1577 pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
1578 pci_set_power_state(dev, pci_choose_state(dev, mesg));
1579 return 0;
1580}
1581
1582static int i801_resume(struct pci_dev *dev)
1583{
1584 pci_set_power_state(dev, PCI_D0);
1585 pci_restore_state(dev);
1586 return pci_enable_device(dev);
1587}
1588#else
1589#define i801_suspend NULL
1590#define i801_resume NULL
1591#endif
1592
1593static struct pci_driver i801_driver = {
1594 .name = "i801_smbus",
1595 .id_table = i801_ids,
1596 .probe = i801_probe,
1597 .remove = i801_remove,
1598 .suspend = i801_suspend,
1599 .resume = i801_resume,
1600};
1601
1602static int __init i2c_i801_init(void)
1603{
1604 if (dmi_name_in_vendors("FUJITSU"))
1605 input_apanel_init();
1606 return pci_register_driver(&i801_driver);
1607}
1608
1609static void __exit i2c_i801_exit(void)
1610{
1611 pci_unregister_driver(&i801_driver);
1612}
1613
1614MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>, "
1615 "Jean Delvare <khali@linux-fr.org>");
1616MODULE_DESCRIPTION("I801 SMBus driver");
1617MODULE_LICENSE("GPL");
1618
1619module_init(i2c_i801_init);
1620module_exit(i2c_i801_exit);
1621