Commit a4e1328a9d20ccf4a9e5a19fce172e6deb2a33e2

Authored by Linus Torvalds

Merge branch 'i2c/for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux

Pull more i2c updates from Wolfram Sang:
 "Included are two bugfixes needing some bigger refactoring (sh_mobile:
  deferred probe with DMA, mv64xxx: fix offload support) and one
  deprecated driver removal I thought would go in via ppc but I
  misunderstood.  It has a proper ack from BenH"

* 'i2c/for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux:
  i2c: sh_mobile: fix uninitialized var when debug is enabled
  macintosh: therm_pm72: delete deprecated driver
  i2c: sh_mobile: I2C_SH_MOBILE should depend on HAS_DMA
  i2c: sh_mobile: rework deferred probing
  i2c: sh_mobile: refactor DMA setup
  i2c: mv64xxx: rework offload support to fix several problems
  i2c: mv64xxx: use BIT() macro for register value definitions

Showing 7 changed files Side-by-side Diff

drivers/i2c/busses/Kconfig
... ... @@ -753,6 +753,7 @@
753 753  
754 754 config I2C_SH_MOBILE
755 755 tristate "SuperH Mobile I2C Controller"
  756 + depends on HAS_DMA
756 757 depends on SUPERH || ARCH_SHMOBILE || COMPILE_TEST
757 758 help
758 759 If you say yes to this option, support will be included for the
drivers/i2c/busses/i2c-mv64xxx.c
... ... @@ -30,12 +30,12 @@
30 30 #define MV64XXX_I2C_BAUD_DIV_N(val) (val & 0x7)
31 31 #define MV64XXX_I2C_BAUD_DIV_M(val) ((val & 0xf) << 3)
32 32  
33   -#define MV64XXX_I2C_REG_CONTROL_ACK 0x00000004
34   -#define MV64XXX_I2C_REG_CONTROL_IFLG 0x00000008
35   -#define MV64XXX_I2C_REG_CONTROL_STOP 0x00000010
36   -#define MV64XXX_I2C_REG_CONTROL_START 0x00000020
37   -#define MV64XXX_I2C_REG_CONTROL_TWSIEN 0x00000040
38   -#define MV64XXX_I2C_REG_CONTROL_INTEN 0x00000080
  33 +#define MV64XXX_I2C_REG_CONTROL_ACK BIT(2)
  34 +#define MV64XXX_I2C_REG_CONTROL_IFLG BIT(3)
  35 +#define MV64XXX_I2C_REG_CONTROL_STOP BIT(4)
  36 +#define MV64XXX_I2C_REG_CONTROL_START BIT(5)
  37 +#define MV64XXX_I2C_REG_CONTROL_TWSIEN BIT(6)
  38 +#define MV64XXX_I2C_REG_CONTROL_INTEN BIT(7)
39 39  
40 40 /* Ctlr status values */
41 41 #define MV64XXX_I2C_STATUS_BUS_ERR 0x00
42 42  
43 43  
44 44  
45 45  
... ... @@ -68,20 +68,18 @@
68 68 #define MV64XXX_I2C_REG_BRIDGE_TIMING 0xe0
69 69  
70 70 /* Bridge Control values */
71   -#define MV64XXX_I2C_BRIDGE_CONTROL_WR 0x00000001
72   -#define MV64XXX_I2C_BRIDGE_CONTROL_RD 0x00000002
  71 +#define MV64XXX_I2C_BRIDGE_CONTROL_WR BIT(0)
  72 +#define MV64XXX_I2C_BRIDGE_CONTROL_RD BIT(1)
73 73 #define MV64XXX_I2C_BRIDGE_CONTROL_ADDR_SHIFT 2
74   -#define MV64XXX_I2C_BRIDGE_CONTROL_ADDR_EXT 0x00001000
  74 +#define MV64XXX_I2C_BRIDGE_CONTROL_ADDR_EXT BIT(12)
75 75 #define MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT 13
76 76 #define MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT 16
77   -#define MV64XXX_I2C_BRIDGE_CONTROL_ENABLE 0x00080000
  77 +#define MV64XXX_I2C_BRIDGE_CONTROL_ENABLE BIT(19)
  78 +#define MV64XXX_I2C_BRIDGE_CONTROL_REPEATED_START BIT(20)
78 79  
79 80 /* Bridge Status values */
80   -#define MV64XXX_I2C_BRIDGE_STATUS_ERROR 0x00000001
81   -#define MV64XXX_I2C_STATUS_OFFLOAD_ERROR 0xf0000001
82   -#define MV64XXX_I2C_STATUS_OFFLOAD_OK 0xf0000000
  81 +#define MV64XXX_I2C_BRIDGE_STATUS_ERROR BIT(0)
83 82  
84   -
85 83 /* Driver states */
86 84 enum {
87 85 MV64XXX_I2C_STATE_INVALID,
88 86  
... ... @@ -99,14 +97,12 @@
99 97 MV64XXX_I2C_ACTION_INVALID,
100 98 MV64XXX_I2C_ACTION_CONTINUE,
101 99 MV64XXX_I2C_ACTION_SEND_RESTART,
102   - MV64XXX_I2C_ACTION_OFFLOAD_RESTART,
103 100 MV64XXX_I2C_ACTION_SEND_ADDR_1,
104 101 MV64XXX_I2C_ACTION_SEND_ADDR_2,
105 102 MV64XXX_I2C_ACTION_SEND_DATA,
106 103 MV64XXX_I2C_ACTION_RCV_DATA,
107 104 MV64XXX_I2C_ACTION_RCV_DATA_STOP,
108 105 MV64XXX_I2C_ACTION_SEND_STOP,
109   - MV64XXX_I2C_ACTION_OFFLOAD_SEND_STOP,
110 106 };
111 107  
112 108 struct mv64xxx_i2c_regs {
... ... @@ -193,75 +189,6 @@
193 189 }
194 190 }
195 191  
196   -static int mv64xxx_i2c_offload_msg(struct mv64xxx_i2c_data *drv_data)
197   -{
198   - unsigned long data_reg_hi = 0;
199   - unsigned long data_reg_lo = 0;
200   - unsigned long ctrl_reg;
201   - struct i2c_msg *msg = drv_data->msgs;
202   -
203   - if (!drv_data->offload_enabled)
204   - return -EOPNOTSUPP;
205   -
206   - /* Only regular transactions can be offloaded */
207   - if ((msg->flags & ~(I2C_M_TEN | I2C_M_RD)) != 0)
208   - return -EINVAL;
209   -
210   - /* Only 1-8 byte transfers can be offloaded */
211   - if (msg->len < 1 || msg->len > 8)
212   - return -EINVAL;
213   -
214   - /* Build transaction */
215   - ctrl_reg = MV64XXX_I2C_BRIDGE_CONTROL_ENABLE |
216   - (msg->addr << MV64XXX_I2C_BRIDGE_CONTROL_ADDR_SHIFT);
217   -
218   - if ((msg->flags & I2C_M_TEN) != 0)
219   - ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_ADDR_EXT;
220   -
221   - if ((msg->flags & I2C_M_RD) == 0) {
222   - u8 local_buf[8] = { 0 };
223   -
224   - memcpy(local_buf, msg->buf, msg->len);
225   - data_reg_lo = cpu_to_le32(*((u32 *)local_buf));
226   - data_reg_hi = cpu_to_le32(*((u32 *)(local_buf+4)));
227   -
228   - ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_WR |
229   - (msg->len - 1) << MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT;
230   -
231   - writel(data_reg_lo,
232   - drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_LO);
233   - writel(data_reg_hi,
234   - drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_HI);
235   -
236   - } else {
237   - ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_RD |
238   - (msg->len - 1) << MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT;
239   - }
240   -
241   - /* Execute transaction */
242   - writel(ctrl_reg, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
243   -
244   - return 0;
245   -}
246   -
247   -static void
248   -mv64xxx_i2c_update_offload_data(struct mv64xxx_i2c_data *drv_data)
249   -{
250   - struct i2c_msg *msg = drv_data->msg;
251   -
252   - if (msg->flags & I2C_M_RD) {
253   - u32 data_reg_lo = readl(drv_data->reg_base +
254   - MV64XXX_I2C_REG_RX_DATA_LO);
255   - u32 data_reg_hi = readl(drv_data->reg_base +
256   - MV64XXX_I2C_REG_RX_DATA_HI);
257   - u8 local_buf[8] = { 0 };
258   -
259   - *((u32 *)local_buf) = le32_to_cpu(data_reg_lo);
260   - *((u32 *)(local_buf+4)) = le32_to_cpu(data_reg_hi);
261   - memcpy(msg->buf, local_buf, msg->len);
262   - }
263   -
264   -}
265 192 /*
266 193 *****************************************************************************
267 194 *
... ... @@ -389,16 +316,6 @@
389 316 drv_data->rc = -ENXIO;
390 317 break;
391 318  
392   - case MV64XXX_I2C_STATUS_OFFLOAD_OK:
393   - if (drv_data->send_stop || drv_data->aborting) {
394   - drv_data->action = MV64XXX_I2C_ACTION_OFFLOAD_SEND_STOP;
395   - drv_data->state = MV64XXX_I2C_STATE_IDLE;
396   - } else {
397   - drv_data->action = MV64XXX_I2C_ACTION_OFFLOAD_RESTART;
398   - drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_RESTART;
399   - }
400   - break;
401   -
402 319 default:
403 320 dev_err(&drv_data->adapter.dev,
404 321 "mv64xxx_i2c_fsm: Ctlr Error -- state: 0x%x, "
405 322  
... ... @@ -419,25 +336,15 @@
419 336 drv_data->aborting = 0;
420 337 drv_data->rc = 0;
421 338  
422   - /* Can we offload this msg ? */
423   - if (mv64xxx_i2c_offload_msg(drv_data) < 0) {
424   - /* No, switch to standard path */
425   - mv64xxx_i2c_prepare_for_io(drv_data, drv_data->msgs);
426   - writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_START,
427   - drv_data->reg_base + drv_data->reg_offsets.control);
428   - }
  339 + mv64xxx_i2c_prepare_for_io(drv_data, drv_data->msgs);
  340 + writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_START,
  341 + drv_data->reg_base + drv_data->reg_offsets.control);
429 342 }
430 343  
431 344 static void
432 345 mv64xxx_i2c_do_action(struct mv64xxx_i2c_data *drv_data)
433 346 {
434 347 switch(drv_data->action) {
435   - case MV64XXX_I2C_ACTION_OFFLOAD_RESTART:
436   - mv64xxx_i2c_update_offload_data(drv_data);
437   - writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
438   - writel(0, drv_data->reg_base +
439   - MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
440   - /* FALLTHRU */
441 348 case MV64XXX_I2C_ACTION_SEND_RESTART:
442 349 /* We should only get here if we have further messages */
443 350 BUG_ON(drv_data->num_msgs == 0);
444 351  
445 352  
... ... @@ -518,16 +425,71 @@
518 425 drv_data->block = 0;
519 426 wake_up(&drv_data->waitq);
520 427 break;
  428 + }
  429 +}
521 430  
522   - case MV64XXX_I2C_ACTION_OFFLOAD_SEND_STOP:
523   - mv64xxx_i2c_update_offload_data(drv_data);
524   - writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
525   - writel(0, drv_data->reg_base +
526   - MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
527   - drv_data->block = 0;
528   - wake_up(&drv_data->waitq);
529   - break;
  431 +static void
  432 +mv64xxx_i2c_read_offload_rx_data(struct mv64xxx_i2c_data *drv_data,
  433 + struct i2c_msg *msg)
  434 +{
  435 + u32 buf[2];
  436 +
  437 + buf[0] = readl(drv_data->reg_base + MV64XXX_I2C_REG_RX_DATA_LO);
  438 + buf[1] = readl(drv_data->reg_base + MV64XXX_I2C_REG_RX_DATA_HI);
  439 +
  440 + memcpy(msg->buf, buf, msg->len);
  441 +}
  442 +
  443 +static int
  444 +mv64xxx_i2c_intr_offload(struct mv64xxx_i2c_data *drv_data)
  445 +{
  446 + u32 cause, status;
  447 +
  448 + cause = readl(drv_data->reg_base +
  449 + MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
  450 + if (!cause)
  451 + return IRQ_NONE;
  452 +
  453 + status = readl(drv_data->reg_base +
  454 + MV64XXX_I2C_REG_BRIDGE_STATUS);
  455 +
  456 + if (status & MV64XXX_I2C_BRIDGE_STATUS_ERROR) {
  457 + drv_data->rc = -EIO;
  458 + goto out;
530 459 }
  460 +
  461 + drv_data->rc = 0;
  462 +
  463 + /*
  464 + * Transaction is a one message read transaction, read data
  465 + * for this message.
  466 + */
  467 + if (drv_data->num_msgs == 1 && drv_data->msgs[0].flags & I2C_M_RD) {
  468 + mv64xxx_i2c_read_offload_rx_data(drv_data, drv_data->msgs);
  469 + drv_data->msgs++;
  470 + drv_data->num_msgs--;
  471 + }
  472 + /*
  473 + * Transaction is a two messages write/read transaction, read
  474 + * data for the second (read) message.
  475 + */
  476 + else if (drv_data->num_msgs == 2 &&
  477 + !(drv_data->msgs[0].flags & I2C_M_RD) &&
  478 + drv_data->msgs[1].flags & I2C_M_RD) {
  479 + mv64xxx_i2c_read_offload_rx_data(drv_data, drv_data->msgs + 1);
  480 + drv_data->msgs += 2;
  481 + drv_data->num_msgs -= 2;
  482 + }
  483 +
  484 +out:
  485 + writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
  486 + writel(0, drv_data->reg_base +
  487 + MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
  488 + drv_data->block = 0;
  489 +
  490 + wake_up(&drv_data->waitq);
  491 +
  492 + return IRQ_HANDLED;
531 493 }
532 494  
533 495 static irqreturn_t
... ... @@ -540,20 +502,9 @@
540 502  
541 503 spin_lock_irqsave(&drv_data->lock, flags);
542 504  
543   - if (drv_data->offload_enabled) {
544   - while (readl(drv_data->reg_base +
545   - MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE)) {
546   - int reg_status = readl(drv_data->reg_base +
547   - MV64XXX_I2C_REG_BRIDGE_STATUS);
548   - if (reg_status & MV64XXX_I2C_BRIDGE_STATUS_ERROR)
549   - status = MV64XXX_I2C_STATUS_OFFLOAD_ERROR;
550   - else
551   - status = MV64XXX_I2C_STATUS_OFFLOAD_OK;
552   - mv64xxx_i2c_fsm(drv_data, status);
553   - mv64xxx_i2c_do_action(drv_data);
554   - rc = IRQ_HANDLED;
555   - }
556   - }
  505 + if (drv_data->offload_enabled)
  506 + rc = mv64xxx_i2c_intr_offload(drv_data);
  507 +
557 508 while (readl(drv_data->reg_base + drv_data->reg_offsets.control) &
558 509 MV64XXX_I2C_REG_CONTROL_IFLG) {
559 510 status = readl(drv_data->reg_base + drv_data->reg_offsets.status);
... ... @@ -635,6 +586,117 @@
635 586 return drv_data->rc;
636 587 }
637 588  
  589 +static void
  590 +mv64xxx_i2c_prepare_tx(struct mv64xxx_i2c_data *drv_data)
  591 +{
  592 + struct i2c_msg *msg = drv_data->msgs;
  593 + u32 buf[2];
  594 +
  595 + memcpy(buf, msg->buf, msg->len);
  596 +
  597 + writel(buf[0], drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_LO);
  598 + writel(buf[1], drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_HI);
  599 +}
  600 +
  601 +static int
  602 +mv64xxx_i2c_offload_xfer(struct mv64xxx_i2c_data *drv_data)
  603 +{
  604 + struct i2c_msg *msgs = drv_data->msgs;
  605 + int num = drv_data->num_msgs;
  606 + unsigned long ctrl_reg;
  607 + unsigned long flags;
  608 +
  609 + spin_lock_irqsave(&drv_data->lock, flags);
  610 +
  611 + /* Build transaction */
  612 + ctrl_reg = MV64XXX_I2C_BRIDGE_CONTROL_ENABLE |
  613 + (msgs[0].addr << MV64XXX_I2C_BRIDGE_CONTROL_ADDR_SHIFT);
  614 +
  615 + if (msgs[0].flags & I2C_M_TEN)
  616 + ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_ADDR_EXT;
  617 +
  618 + /* Single write message transaction */
  619 + if (num == 1 && !(msgs[0].flags & I2C_M_RD)) {
  620 + size_t len = msgs[0].len - 1;
  621 +
  622 + ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_WR |
  623 + (len << MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT);
  624 + mv64xxx_i2c_prepare_tx(drv_data);
  625 + }
  626 + /* Single read message transaction */
  627 + else if (num == 1 && msgs[0].flags & I2C_M_RD) {
  628 + size_t len = msgs[0].len - 1;
  629 +
  630 + ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_RD |
  631 + (len << MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT);
  632 + }
  633 + /*
  634 + * Transaction with one write and one read message. This is
  635 + * guaranteed by the mv64xx_i2c_can_offload() checks.
  636 + */
  637 + else if (num == 2) {
  638 + size_t lentx = msgs[0].len - 1;
  639 + size_t lenrx = msgs[1].len - 1;
  640 +
  641 + ctrl_reg |=
  642 + MV64XXX_I2C_BRIDGE_CONTROL_RD |
  643 + MV64XXX_I2C_BRIDGE_CONTROL_WR |
  644 + (lentx << MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT) |
  645 + (lenrx << MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT) |
  646 + MV64XXX_I2C_BRIDGE_CONTROL_REPEATED_START;
  647 + mv64xxx_i2c_prepare_tx(drv_data);
  648 + }
  649 +
  650 + /* Execute transaction */
  651 + drv_data->block = 1;
  652 + writel(ctrl_reg, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
  653 + spin_unlock_irqrestore(&drv_data->lock, flags);
  654 +
  655 + mv64xxx_i2c_wait_for_completion(drv_data);
  656 +
  657 + return drv_data->rc;
  658 +}
  659 +
  660 +static bool
  661 +mv64xxx_i2c_valid_offload_sz(struct i2c_msg *msg)
  662 +{
  663 + return msg->len <= 8 && msg->len >= 1;
  664 +}
  665 +
  666 +static bool
  667 +mv64xxx_i2c_can_offload(struct mv64xxx_i2c_data *drv_data)
  668 +{
  669 + struct i2c_msg *msgs = drv_data->msgs;
  670 + int num = drv_data->num_msgs;
  671 +
  672 + return false;
  673 +
  674 + if (!drv_data->offload_enabled)
  675 + return false;
  676 +
  677 + /*
  678 + * We can offload a transaction consisting of a single
  679 + * message, as long as the message has a length between 1 and
  680 + * 8 bytes.
  681 + */
  682 + if (num == 1 && mv64xxx_i2c_valid_offload_sz(msgs))
  683 + return true;
  684 +
  685 + /*
  686 + * We can offload a transaction consisting of two messages, if
  687 + * the first is a write and a second is a read, and both have
  688 + * a length between 1 and 8 bytes.
  689 + */
  690 + if (num == 2 &&
  691 + mv64xxx_i2c_valid_offload_sz(msgs) &&
  692 + mv64xxx_i2c_valid_offload_sz(msgs + 1) &&
  693 + !(msgs[0].flags & I2C_M_RD) &&
  694 + msgs[1].flags & I2C_M_RD)
  695 + return true;
  696 +
  697 + return false;
  698 +}
  699 +
638 700 /*
639 701 *****************************************************************************
640 702 *
... ... @@ -658,7 +720,11 @@
658 720 drv_data->msgs = msgs;
659 721 drv_data->num_msgs = num;
660 722  
661   - rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[0], num == 1);
  723 + if (mv64xxx_i2c_can_offload(drv_data))
  724 + rc = mv64xxx_i2c_offload_xfer(drv_data);
  725 + else
  726 + rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[0], num == 1);
  727 +
662 728 if (rc < 0)
663 729 ret = rc;
664 730  
drivers/i2c/busses/i2c-sh_mobile.c
... ... @@ -140,6 +140,7 @@
140 140 int sr;
141 141 bool send_stop;
142 142  
  143 + struct resource *res;
143 144 struct dma_chan *dma_tx;
144 145 struct dma_chan *dma_rx;
145 146 struct scatterlist sg;
... ... @@ -539,6 +540,42 @@
539 540 iic_set_clr(pd, ICIC, 0, ICIC_TDMAE | ICIC_RDMAE);
540 541 }
541 542  
  543 +static struct dma_chan *sh_mobile_i2c_request_dma_chan(struct device *dev,
  544 + enum dma_transfer_direction dir, dma_addr_t port_addr)
  545 +{
  546 + struct dma_chan *chan;
  547 + struct dma_slave_config cfg;
  548 + char *chan_name = dir == DMA_MEM_TO_DEV ? "tx" : "rx";
  549 + int ret;
  550 +
  551 + chan = dma_request_slave_channel_reason(dev, chan_name);
  552 + if (IS_ERR(chan)) {
  553 + ret = PTR_ERR(chan);
  554 + dev_dbg(dev, "request_channel failed for %s (%d)\n", chan_name, ret);
  555 + return chan;
  556 + }
  557 +
  558 + memset(&cfg, 0, sizeof(cfg));
  559 + cfg.direction = dir;
  560 + if (dir == DMA_MEM_TO_DEV) {
  561 + cfg.dst_addr = port_addr;
  562 + cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  563 + } else {
  564 + cfg.src_addr = port_addr;
  565 + cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  566 + }
  567 +
  568 + ret = dmaengine_slave_config(chan, &cfg);
  569 + if (ret) {
  570 + dev_dbg(dev, "slave_config failed for %s (%d)\n", chan_name, ret);
  571 + dma_release_channel(chan);
  572 + return ERR_PTR(ret);
  573 + }
  574 +
  575 + dev_dbg(dev, "got DMA channel for %s\n", chan_name);
  576 + return chan;
  577 +}
  578 +
542 579 static void sh_mobile_i2c_xfer_dma(struct sh_mobile_i2c_data *pd)
543 580 {
544 581 bool read = pd->msg->flags & I2C_M_RD;
... ... @@ -548,7 +585,16 @@
548 585 dma_addr_t dma_addr;
549 586 dma_cookie_t cookie;
550 587  
551   - if (!chan)
  588 + if (PTR_ERR(chan) == -EPROBE_DEFER) {
  589 + if (read)
  590 + chan = pd->dma_rx = sh_mobile_i2c_request_dma_chan(pd->dev, DMA_DEV_TO_MEM,
  591 + pd->res->start + ICDR);
  592 + else
  593 + chan = pd->dma_tx = sh_mobile_i2c_request_dma_chan(pd->dev, DMA_MEM_TO_DEV,
  594 + pd->res->start + ICDR);
  595 + }
  596 +
  597 + if (IS_ERR(chan))
552 598 return;
553 599  
554 600 dma_addr = dma_map_single(chan->device->dev, pd->msg->buf, pd->msg->len, dir);
555 601  
556 602  
557 603  
558 604  
... ... @@ -747,56 +793,16 @@
747 793 };
748 794 MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids);
749 795  
750   -static int sh_mobile_i2c_request_dma_chan(struct device *dev, enum dma_transfer_direction dir,
751   - dma_addr_t port_addr, struct dma_chan **chan_ptr)
752   -{
753   - struct dma_chan *chan;
754   - struct dma_slave_config cfg;
755   - char *chan_name = dir == DMA_MEM_TO_DEV ? "tx" : "rx";
756   - int ret;
757   -
758   - *chan_ptr = NULL;
759   -
760   - chan = dma_request_slave_channel_reason(dev, chan_name);
761   - if (IS_ERR(chan)) {
762   - ret = PTR_ERR(chan);
763   - dev_dbg(dev, "request_channel failed for %s (%d)\n", chan_name, ret);
764   - return ret;
765   - }
766   -
767   - memset(&cfg, 0, sizeof(cfg));
768   - cfg.direction = dir;
769   - if (dir == DMA_MEM_TO_DEV) {
770   - cfg.dst_addr = port_addr;
771   - cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
772   - } else {
773   - cfg.src_addr = port_addr;
774   - cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
775   - }
776   -
777   - ret = dmaengine_slave_config(chan, &cfg);
778   - if (ret) {
779   - dev_dbg(dev, "slave_config failed for %s (%d)\n", chan_name, ret);
780   - dma_release_channel(chan);
781   - return ret;
782   - }
783   -
784   - *chan_ptr = chan;
785   -
786   - dev_dbg(dev, "got DMA channel for %s\n", chan_name);
787   - return 0;
788   -}
789   -
790 796 static void sh_mobile_i2c_release_dma(struct sh_mobile_i2c_data *pd)
791 797 {
792   - if (pd->dma_tx) {
  798 + if (!IS_ERR(pd->dma_tx)) {
793 799 dma_release_channel(pd->dma_tx);
794   - pd->dma_tx = NULL;
  800 + pd->dma_tx = ERR_PTR(-EPROBE_DEFER);
795 801 }
796 802  
797   - if (pd->dma_rx) {
  803 + if (!IS_ERR(pd->dma_rx)) {
798 804 dma_release_channel(pd->dma_rx);
799   - pd->dma_rx = NULL;
  805 + pd->dma_rx = ERR_PTR(-EPROBE_DEFER);
800 806 }
801 807 }
802 808  
... ... @@ -849,6 +855,7 @@
849 855  
850 856 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
851 857  
  858 + pd->res = res;
852 859 pd->reg = devm_ioremap_resource(&dev->dev, res);
853 860 if (IS_ERR(pd->reg))
854 861 return PTR_ERR(pd->reg);
855 862  
... ... @@ -889,18 +896,8 @@
889 896 /* Init DMA */
890 897 sg_init_table(&pd->sg, 1);
891 898 pd->dma_direction = DMA_NONE;
892   - ret = sh_mobile_i2c_request_dma_chan(pd->dev, DMA_DEV_TO_MEM,
893   - res->start + ICDR, &pd->dma_rx);
894   - if (ret == -EPROBE_DEFER)
895   - return ret;
  899 + pd->dma_rx = pd->dma_tx = ERR_PTR(-EPROBE_DEFER);
896 900  
897   - ret = sh_mobile_i2c_request_dma_chan(pd->dev, DMA_MEM_TO_DEV,
898   - res->start + ICDR, &pd->dma_tx);
899   - if (ret == -EPROBE_DEFER) {
900   - sh_mobile_i2c_release_dma(pd);
901   - return ret;
902   - }
903   -
904 901 /* Enable Runtime PM for this device.
905 902 *
906 903 * Also tell the Runtime PM core to ignore children
... ... @@ -937,8 +934,7 @@
937 934 return ret;
938 935 }
939 936  
940   - dev_info(&dev->dev, "I2C adapter %d, bus speed %lu Hz, DMA=%c\n",
941   - adap->nr, pd->bus_speed, (pd->dma_rx || pd->dma_tx) ? 'y' : 'n');
  937 + dev_info(&dev->dev, "I2C adapter %d, bus speed %lu Hz\n", adap->nr, pd->bus_speed);
942 938  
943 939 return 0;
944 940 }
drivers/macintosh/Kconfig
... ... @@ -204,16 +204,6 @@
204 204 iBook G4, and the ATI based aluminium PowerBooks, allowing slightly
205 205 better fan behaviour by default, and some manual control.
206 206  
207   -config THERM_PM72
208   - tristate "Support for thermal management on PowerMac G5 (AGP)"
209   - depends on I2C && I2C_POWERMAC && PPC_PMAC64
210   - default n
211   - help
212   - This driver provides thermostat and fan control for the desktop
213   - G5 machines.
214   -
215   - This is deprecated, use windfarm instead.
216   -
217 207 config WINDFARM
218 208 tristate "New PowerMac thermal control infrastructure"
219 209 depends on PPC
drivers/macintosh/Makefile
... ... @@ -25,7 +25,6 @@
25 25 obj-$(CONFIG_ADB_PMU68K) += via-pmu68k.o
26 26 obj-$(CONFIG_ADB_MACIO) += macio-adb.o
27 27  
28   -obj-$(CONFIG_THERM_PM72) += therm_pm72.o
29 28 obj-$(CONFIG_THERM_WINDTUNNEL) += therm_windtunnel.o
30 29 obj-$(CONFIG_THERM_ADT746X) += therm_adt746x.o
31 30 obj-$(CONFIG_WINDFARM) += windfarm_core.o
drivers/macintosh/therm_pm72.c
Changes suppressed. Click to show
1   -/*
2   - * Device driver for the thermostats & fan controller of the
3   - * Apple G5 "PowerMac7,2" desktop machines.
4   - *
5   - * (c) Copyright IBM Corp. 2003-2004
6   - *
7   - * Maintained by: Benjamin Herrenschmidt
8   - * <benh@kernel.crashing.org>
9   - *
10   - *
11   - * The algorithm used is the PID control algorithm, used the same
12   - * way the published Darwin code does, using the same values that
13   - * are present in the Darwin 7.0 snapshot property lists.
14   - *
15   - * As far as the CPUs control loops are concerned, I use the
16   - * calibration & PID constants provided by the EEPROM,
17   - * I do _not_ embed any value from the property lists, as the ones
18   - * provided by Darwin 7.0 seem to always have an older version that
19   - * what I've seen on the actual computers.
20   - * It would be interesting to verify that though. Darwin has a
21   - * version code of 1.0.0d11 for all control loops it seems, while
22   - * so far, the machines EEPROMs contain a dataset versioned 1.0.0f
23   - *
24   - * Darwin doesn't provide source to all parts, some missing
25   - * bits like the AppleFCU driver or the actual scale of some
26   - * of the values returned by sensors had to be "guessed" some
27   - * way... or based on what Open Firmware does.
28   - *
29   - * I didn't yet figure out how to get the slots power consumption
30   - * out of the FCU, so that part has not been implemented yet and
31   - * the slots fan is set to a fixed 50% PWM, hoping this value is
32   - * safe enough ...
33   - *
34   - * Note: I have observed strange oscillations of the CPU control
35   - * loop on a dual G5 here. When idle, the CPU exhaust fan tend to
36   - * oscillates slowly (over several minutes) between the minimum
37   - * of 300RPMs and approx. 1000 RPMs. I don't know what is causing
38   - * this, it could be some incorrect constant or an error in the
39   - * way I ported the algorithm, or it could be just normal. I
40   - * don't have full understanding on the way Apple tweaked the PID
41   - * algorithm for the CPU control, it is definitely not a standard
42   - * implementation...
43   - *
44   - * TODO: - Check MPU structure version/signature
45   - * - Add things like /sbin/overtemp for non-critical
46   - * overtemp conditions so userland can take some policy
47   - * decisions, like slowing down CPUs
48   - * - Deal with fan and i2c failures in a better way
49   - * - Maybe do a generic PID based on params used for
50   - * U3 and Drives ? Definitely need to factor code a bit
51   - * better... also make sensor detection more robust using
52   - * the device-tree to probe for them
53   - * - Figure out how to get the slots consumption and set the
54   - * slots fan accordingly
55   - *
56   - * History:
57   - *
58   - * Nov. 13, 2003 : 0.5
59   - * - First release
60   - *
61   - * Nov. 14, 2003 : 0.6
62   - * - Read fan speed from FCU, low level fan routines now deal
63   - * with errors & check fan status, though higher level don't
64   - * do much.
65   - * - Move a bunch of definitions to .h file
66   - *
67   - * Nov. 18, 2003 : 0.7
68   - * - Fix build on ppc64 kernel
69   - * - Move back statics definitions to .c file
70   - * - Avoid calling schedule_timeout with a negative number
71   - *
72   - * Dec. 18, 2003 : 0.8
73   - * - Fix typo when reading back fan speed on 2 CPU machines
74   - *
75   - * Mar. 11, 2004 : 0.9
76   - * - Rework code accessing the ADC chips, make it more robust and
77   - * closer to the chip spec. Also make sure it is configured properly,
78   - * I've seen yet unexplained cases where on startup, I would have stale
79   - * values in the configuration register
80   - * - Switch back to use of target fan speed for PID, thus lowering
81   - * pressure on i2c
82   - *
83   - * Oct. 20, 2004 : 1.1
84   - * - Add device-tree lookup for fan IDs, should detect liquid cooling
85   - * pumps when present
86   - * - Enable driver for PowerMac7,3 machines
87   - * - Split the U3/Backside cooling on U3 & U3H versions as Darwin does
88   - * - Add new CPU cooling algorithm for machines with liquid cooling
89   - * - Workaround for some PowerMac7,3 with empty "fan" node in the devtree
90   - * - Fix a signed/unsigned compare issue in some PID loops
91   - *
92   - * Mar. 10, 2005 : 1.2
93   - * - Add basic support for Xserve G5
94   - * - Retrieve pumps min/max from EEPROM image in device-tree (broken)
95   - * - Use min/max macros here or there
96   - * - Latest darwin updated U3H min fan speed to 20% PWM
97   - *
98   - * July. 06, 2006 : 1.3
99   - * - Fix setting of RPM fans on Xserve G5 (they were going too fast)
100   - * - Add missing slots fan control loop for Xserve G5
101   - * - Lower fixed slots fan speed from 50% to 40% on desktop G5s. We
102   - * still can't properly implement the control loop for these, so let's
103   - * reduce the noise a little bit, it appears that 40% still gives us
104   - * a pretty good air flow
105   - * - Add code to "tickle" the FCU regulary so it doesn't think that
106   - * we are gone while in fact, the machine just didn't need any fan
107   - * speed change lately
108   - *
109   - */
110   -
111   -#include <linux/types.h>
112   -#include <linux/module.h>
113   -#include <linux/errno.h>
114   -#include <linux/kernel.h>
115   -#include <linux/delay.h>
116   -#include <linux/sched.h>
117   -#include <linux/init.h>
118   -#include <linux/spinlock.h>
119   -#include <linux/wait.h>
120   -#include <linux/reboot.h>
121   -#include <linux/kmod.h>
122   -#include <linux/i2c.h>
123   -#include <linux/kthread.h>
124   -#include <linux/mutex.h>
125   -#include <linux/of_device.h>
126   -#include <linux/of_platform.h>
127   -#include <asm/prom.h>
128   -#include <asm/machdep.h>
129   -#include <asm/io.h>
130   -#include <asm/sections.h>
131   -#include <asm/macio.h>
132   -
133   -#include "therm_pm72.h"
134   -
135   -#define VERSION "1.3"
136   -
137   -#undef DEBUG
138   -
139   -#ifdef DEBUG
140   -#define DBG(args...) printk(args)
141   -#else
142   -#define DBG(args...) do { } while(0)
143   -#endif
144   -
145   -
146   -/*
147   - * Driver statics
148   - */
149   -
150   -static struct platform_device * of_dev;
151   -static struct i2c_adapter * u3_0;
152   -static struct i2c_adapter * u3_1;
153   -static struct i2c_adapter * k2;
154   -static struct i2c_client * fcu;
155   -static struct cpu_pid_state processor_state[2];
156   -static struct basckside_pid_params backside_params;
157   -static struct backside_pid_state backside_state;
158   -static struct drives_pid_state drives_state;
159   -static struct dimm_pid_state dimms_state;
160   -static struct slots_pid_state slots_state;
161   -static int state;
162   -static int cpu_count;
163   -static int cpu_pid_type;
164   -static struct task_struct *ctrl_task;
165   -static struct completion ctrl_complete;
166   -static int critical_state;
167   -static int rackmac;
168   -static s32 dimm_output_clamp;
169   -static int fcu_rpm_shift;
170   -static int fcu_tickle_ticks;
171   -static DEFINE_MUTEX(driver_lock);
172   -
173   -/*
174   - * We have 3 types of CPU PID control. One is "split" old style control
175   - * for intake & exhaust fans, the other is "combined" control for both
176   - * CPUs that also deals with the pumps when present. To be "compatible"
177   - * with OS X at this point, we only use "COMBINED" on the machines that
178   - * are identified as having the pumps (though that identification is at
179   - * least dodgy). Ultimately, we could probably switch completely to this
180   - * algorithm provided we hack it to deal with the UP case
181   - */
182   -#define CPU_PID_TYPE_SPLIT 0
183   -#define CPU_PID_TYPE_COMBINED 1
184   -#define CPU_PID_TYPE_RACKMAC 2
185   -
186   -/*
187   - * This table describes all fans in the FCU. The "id" and "type" values
188   - * are defaults valid for all earlier machines. Newer machines will
189   - * eventually override the table content based on the device-tree
190   - */
191   -struct fcu_fan_table
192   -{
193   - char* loc; /* location code */
194   - int type; /* 0 = rpm, 1 = pwm, 2 = pump */
195   - int id; /* id or -1 */
196   -};
197   -
198   -#define FCU_FAN_RPM 0
199   -#define FCU_FAN_PWM 1
200   -
201   -#define FCU_FAN_ABSENT_ID -1
202   -
203   -#define FCU_FAN_COUNT ARRAY_SIZE(fcu_fans)
204   -
205   -struct fcu_fan_table fcu_fans[] = {
206   - [BACKSIDE_FAN_PWM_INDEX] = {
207   - .loc = "BACKSIDE,SYS CTRLR FAN",
208   - .type = FCU_FAN_PWM,
209   - .id = BACKSIDE_FAN_PWM_DEFAULT_ID,
210   - },
211   - [DRIVES_FAN_RPM_INDEX] = {
212   - .loc = "DRIVE BAY",
213   - .type = FCU_FAN_RPM,
214   - .id = DRIVES_FAN_RPM_DEFAULT_ID,
215   - },
216   - [SLOTS_FAN_PWM_INDEX] = {
217   - .loc = "SLOT,PCI FAN",
218   - .type = FCU_FAN_PWM,
219   - .id = SLOTS_FAN_PWM_DEFAULT_ID,
220   - },
221   - [CPUA_INTAKE_FAN_RPM_INDEX] = {
222   - .loc = "CPU A INTAKE",
223   - .type = FCU_FAN_RPM,
224   - .id = CPUA_INTAKE_FAN_RPM_DEFAULT_ID,
225   - },
226   - [CPUA_EXHAUST_FAN_RPM_INDEX] = {
227   - .loc = "CPU A EXHAUST",
228   - .type = FCU_FAN_RPM,
229   - .id = CPUA_EXHAUST_FAN_RPM_DEFAULT_ID,
230   - },
231   - [CPUB_INTAKE_FAN_RPM_INDEX] = {
232   - .loc = "CPU B INTAKE",
233   - .type = FCU_FAN_RPM,
234   - .id = CPUB_INTAKE_FAN_RPM_DEFAULT_ID,
235   - },
236   - [CPUB_EXHAUST_FAN_RPM_INDEX] = {
237   - .loc = "CPU B EXHAUST",
238   - .type = FCU_FAN_RPM,
239   - .id = CPUB_EXHAUST_FAN_RPM_DEFAULT_ID,
240   - },
241   - /* pumps aren't present by default, have to be looked up in the
242   - * device-tree
243   - */
244   - [CPUA_PUMP_RPM_INDEX] = {
245   - .loc = "CPU A PUMP",
246   - .type = FCU_FAN_RPM,
247   - .id = FCU_FAN_ABSENT_ID,
248   - },
249   - [CPUB_PUMP_RPM_INDEX] = {
250   - .loc = "CPU B PUMP",
251   - .type = FCU_FAN_RPM,
252   - .id = FCU_FAN_ABSENT_ID,
253   - },
254   - /* Xserve fans */
255   - [CPU_A1_FAN_RPM_INDEX] = {
256   - .loc = "CPU A 1",
257   - .type = FCU_FAN_RPM,
258   - .id = FCU_FAN_ABSENT_ID,
259   - },
260   - [CPU_A2_FAN_RPM_INDEX] = {
261   - .loc = "CPU A 2",
262   - .type = FCU_FAN_RPM,
263   - .id = FCU_FAN_ABSENT_ID,
264   - },
265   - [CPU_A3_FAN_RPM_INDEX] = {
266   - .loc = "CPU A 3",
267   - .type = FCU_FAN_RPM,
268   - .id = FCU_FAN_ABSENT_ID,
269   - },
270   - [CPU_B1_FAN_RPM_INDEX] = {
271   - .loc = "CPU B 1",
272   - .type = FCU_FAN_RPM,
273   - .id = FCU_FAN_ABSENT_ID,
274   - },
275   - [CPU_B2_FAN_RPM_INDEX] = {
276   - .loc = "CPU B 2",
277   - .type = FCU_FAN_RPM,
278   - .id = FCU_FAN_ABSENT_ID,
279   - },
280   - [CPU_B3_FAN_RPM_INDEX] = {
281   - .loc = "CPU B 3",
282   - .type = FCU_FAN_RPM,
283   - .id = FCU_FAN_ABSENT_ID,
284   - },
285   -};
286   -
287   -static struct i2c_driver therm_pm72_driver;
288   -
289   -/*
290   - * Utility function to create an i2c_client structure and
291   - * attach it to one of u3 adapters
292   - */
293   -static struct i2c_client *attach_i2c_chip(int id, const char *name)
294   -{
295   - struct i2c_client *clt;
296   - struct i2c_adapter *adap;
297   - struct i2c_board_info info;
298   -
299   - if (id & 0x200)
300   - adap = k2;
301   - else if (id & 0x100)
302   - adap = u3_1;
303   - else
304   - adap = u3_0;
305   - if (adap == NULL)
306   - return NULL;
307   -
308   - memset(&info, 0, sizeof(struct i2c_board_info));
309   - info.addr = (id >> 1) & 0x7f;
310   - strlcpy(info.type, "therm_pm72", I2C_NAME_SIZE);
311   - clt = i2c_new_device(adap, &info);
312   - if (!clt) {
313   - printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
314   - return NULL;
315   - }
316   -
317   - /*
318   - * Let i2c-core delete that device on driver removal.
319   - * This is safe because i2c-core holds the core_lock mutex for us.
320   - */
321   - list_add_tail(&clt->detected, &therm_pm72_driver.clients);
322   - return clt;
323   -}
324   -
325   -/*
326   - * Here are the i2c chip access wrappers
327   - */
328   -
329   -static void initialize_adc(struct cpu_pid_state *state)
330   -{
331   - int rc;
332   - u8 buf[2];
333   -
334   - /* Read ADC the configuration register and cache it. We
335   - * also make sure Config2 contains proper values, I've seen
336   - * cases where we got stale grabage in there, thus preventing
337   - * proper reading of conv. values
338   - */
339   -
340   - /* Clear Config2 */
341   - buf[0] = 5;
342   - buf[1] = 0;
343   - i2c_master_send(state->monitor, buf, 2);
344   -
345   - /* Read & cache Config1 */
346   - buf[0] = 1;
347   - rc = i2c_master_send(state->monitor, buf, 1);
348   - if (rc > 0) {
349   - rc = i2c_master_recv(state->monitor, buf, 1);
350   - if (rc > 0) {
351   - state->adc_config = buf[0];
352   - DBG("ADC config reg: %02x\n", state->adc_config);
353   - /* Disable shutdown mode */
354   - state->adc_config &= 0xfe;
355   - buf[0] = 1;
356   - buf[1] = state->adc_config;
357   - rc = i2c_master_send(state->monitor, buf, 2);
358   - }
359   - }
360   - if (rc <= 0)
361   - printk(KERN_ERR "therm_pm72: Error reading ADC config"
362   - " register !\n");
363   -}
364   -
365   -static int read_smon_adc(struct cpu_pid_state *state, int chan)
366   -{
367   - int rc, data, tries = 0;
368   - u8 buf[2];
369   -
370   - for (;;) {
371   - /* Set channel */
372   - buf[0] = 1;
373   - buf[1] = (state->adc_config & 0x1f) | (chan << 5);
374   - rc = i2c_master_send(state->monitor, buf, 2);
375   - if (rc <= 0)
376   - goto error;
377   - /* Wait for conversion */
378   - msleep(1);
379   - /* Switch to data register */
380   - buf[0] = 4;
381   - rc = i2c_master_send(state->monitor, buf, 1);
382   - if (rc <= 0)
383   - goto error;
384   - /* Read result */
385   - rc = i2c_master_recv(state->monitor, buf, 2);
386   - if (rc < 0)
387   - goto error;
388   - data = ((u16)buf[0]) << 8 | (u16)buf[1];
389   - return data >> 6;
390   - error:
391   - DBG("Error reading ADC, retrying...\n");
392   - if (++tries > 10) {
393   - printk(KERN_ERR "therm_pm72: Error reading ADC !\n");
394   - return -1;
395   - }
396   - msleep(10);
397   - }
398   -}
399   -
400   -static int read_lm87_reg(struct i2c_client * chip, int reg)
401   -{
402   - int rc, tries = 0;
403   - u8 buf;
404   -
405   - for (;;) {
406   - /* Set address */
407   - buf = (u8)reg;
408   - rc = i2c_master_send(chip, &buf, 1);
409   - if (rc <= 0)
410   - goto error;
411   - rc = i2c_master_recv(chip, &buf, 1);
412   - if (rc <= 0)
413   - goto error;
414   - return (int)buf;
415   - error:
416   - DBG("Error reading LM87, retrying...\n");
417   - if (++tries > 10) {
418   - printk(KERN_ERR "therm_pm72: Error reading LM87 !\n");
419   - return -1;
420   - }
421   - msleep(10);
422   - }
423   -}
424   -
425   -static int fan_read_reg(int reg, unsigned char *buf, int nb)
426   -{
427   - int tries, nr, nw;
428   -
429   - buf[0] = reg;
430   - tries = 0;
431   - for (;;) {
432   - nw = i2c_master_send(fcu, buf, 1);
433   - if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
434   - break;
435   - msleep(10);
436   - ++tries;
437   - }
438   - if (nw <= 0) {
439   - printk(KERN_ERR "Failure writing address to FCU: %d", nw);
440   - return -EIO;
441   - }
442   - tries = 0;
443   - for (;;) {
444   - nr = i2c_master_recv(fcu, buf, nb);
445   - if (nr > 0 || (nr < 0 && nr != -ENODEV) || tries >= 100)
446   - break;
447   - msleep(10);
448   - ++tries;
449   - }
450   - if (nr <= 0)
451   - printk(KERN_ERR "Failure reading data from FCU: %d", nw);
452   - return nr;
453   -}
454   -
455   -static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
456   -{
457   - int tries, nw;
458   - unsigned char buf[16];
459   -
460   - buf[0] = reg;
461   - memcpy(buf+1, ptr, nb);
462   - ++nb;
463   - tries = 0;
464   - for (;;) {
465   - nw = i2c_master_send(fcu, buf, nb);
466   - if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
467   - break;
468   - msleep(10);
469   - ++tries;
470   - }
471   - if (nw < 0)
472   - printk(KERN_ERR "Failure writing to FCU: %d", nw);
473   - return nw;
474   -}
475   -
476   -static int start_fcu(void)
477   -{
478   - unsigned char buf = 0xff;
479   - int rc;
480   -
481   - rc = fan_write_reg(0xe, &buf, 1);
482   - if (rc < 0)
483   - return -EIO;
484   - rc = fan_write_reg(0x2e, &buf, 1);
485   - if (rc < 0)
486   - return -EIO;
487   - rc = fan_read_reg(0, &buf, 1);
488   - if (rc < 0)
489   - return -EIO;
490   - fcu_rpm_shift = (buf == 1) ? 2 : 3;
491   - printk(KERN_DEBUG "FCU Initialized, RPM fan shift is %d\n",
492   - fcu_rpm_shift);
493   -
494   - return 0;
495   -}
496   -
497   -static int set_rpm_fan(int fan_index, int rpm)
498   -{
499   - unsigned char buf[2];
500   - int rc, id, min, max;
501   -
502   - if (fcu_fans[fan_index].type != FCU_FAN_RPM)
503   - return -EINVAL;
504   - id = fcu_fans[fan_index].id;
505   - if (id == FCU_FAN_ABSENT_ID)
506   - return -EINVAL;
507   -
508   - min = 2400 >> fcu_rpm_shift;
509   - max = 56000 >> fcu_rpm_shift;
510   -
511   - if (rpm < min)
512   - rpm = min;
513   - else if (rpm > max)
514   - rpm = max;
515   - buf[0] = rpm >> (8 - fcu_rpm_shift);
516   - buf[1] = rpm << fcu_rpm_shift;
517   - rc = fan_write_reg(0x10 + (id * 2), buf, 2);
518   - if (rc < 0)
519   - return -EIO;
520   - return 0;
521   -}
522   -
523   -static int get_rpm_fan(int fan_index, int programmed)
524   -{
525   - unsigned char failure;
526   - unsigned char active;
527   - unsigned char buf[2];
528   - int rc, id, reg_base;
529   -
530   - if (fcu_fans[fan_index].type != FCU_FAN_RPM)
531   - return -EINVAL;
532   - id = fcu_fans[fan_index].id;
533   - if (id == FCU_FAN_ABSENT_ID)
534   - return -EINVAL;
535   -
536   - rc = fan_read_reg(0xb, &failure, 1);
537   - if (rc != 1)
538   - return -EIO;
539   - if ((failure & (1 << id)) != 0)
540   - return -EFAULT;
541   - rc = fan_read_reg(0xd, &active, 1);
542   - if (rc != 1)
543   - return -EIO;
544   - if ((active & (1 << id)) == 0)
545   - return -ENXIO;
546   -
547   - /* Programmed value or real current speed */
548   - reg_base = programmed ? 0x10 : 0x11;
549   - rc = fan_read_reg(reg_base + (id * 2), buf, 2);
550   - if (rc != 2)
551   - return -EIO;
552   -
553   - return (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
554   -}
555   -
556   -static int set_pwm_fan(int fan_index, int pwm)
557   -{
558   - unsigned char buf[2];
559   - int rc, id;
560   -
561   - if (fcu_fans[fan_index].type != FCU_FAN_PWM)
562   - return -EINVAL;
563   - id = fcu_fans[fan_index].id;
564   - if (id == FCU_FAN_ABSENT_ID)
565   - return -EINVAL;
566   -
567   - if (pwm < 10)
568   - pwm = 10;
569   - else if (pwm > 100)
570   - pwm = 100;
571   - pwm = (pwm * 2559) / 1000;
572   - buf[0] = pwm;
573   - rc = fan_write_reg(0x30 + (id * 2), buf, 1);
574   - if (rc < 0)
575   - return rc;
576   - return 0;
577   -}
578   -
579   -static int get_pwm_fan(int fan_index)
580   -{
581   - unsigned char failure;
582   - unsigned char active;
583   - unsigned char buf[2];
584   - int rc, id;
585   -
586   - if (fcu_fans[fan_index].type != FCU_FAN_PWM)
587   - return -EINVAL;
588   - id = fcu_fans[fan_index].id;
589   - if (id == FCU_FAN_ABSENT_ID)
590   - return -EINVAL;
591   -
592   - rc = fan_read_reg(0x2b, &failure, 1);
593   - if (rc != 1)
594   - return -EIO;
595   - if ((failure & (1 << id)) != 0)
596   - return -EFAULT;
597   - rc = fan_read_reg(0x2d, &active, 1);
598   - if (rc != 1)
599   - return -EIO;
600   - if ((active & (1 << id)) == 0)
601   - return -ENXIO;
602   -
603   - /* Programmed value or real current speed */
604   - rc = fan_read_reg(0x30 + (id * 2), buf, 1);
605   - if (rc != 1)
606   - return -EIO;
607   -
608   - return (buf[0] * 1000) / 2559;
609   -}
610   -
611   -static void tickle_fcu(void)
612   -{
613   - int pwm;
614   -
615   - pwm = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
616   -
617   - DBG("FCU Tickle, slots fan is: %d\n", pwm);
618   - if (pwm < 0)
619   - pwm = 100;
620   -
621   - if (!rackmac) {
622   - pwm = SLOTS_FAN_DEFAULT_PWM;
623   - } else if (pwm < SLOTS_PID_OUTPUT_MIN)
624   - pwm = SLOTS_PID_OUTPUT_MIN;
625   -
626   - /* That is hopefully enough to make the FCU happy */
627   - set_pwm_fan(SLOTS_FAN_PWM_INDEX, pwm);
628   -}
629   -
630   -
631   -/*
632   - * Utility routine to read the CPU calibration EEPROM data
633   - * from the device-tree
634   - */
635   -static int read_eeprom(int cpu, struct mpu_data *out)
636   -{
637   - struct device_node *np;
638   - char nodename[64];
639   - const u8 *data;
640   - int len;
641   -
642   - /* prom.c routine for finding a node by path is a bit brain dead
643   - * and requires exact @xxx unit numbers. This is a bit ugly but
644   - * will work for these machines
645   - */
646   - sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
647   - np = of_find_node_by_path(nodename);
648   - if (np == NULL) {
649   - printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid node from device-tree\n");
650   - return -ENODEV;
651   - }
652   - data = of_get_property(np, "cpuid", &len);
653   - if (data == NULL) {
654   - printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid property from device-tree\n");
655   - of_node_put(np);
656   - return -ENODEV;
657   - }
658   - memcpy(out, data, sizeof(struct mpu_data));
659   - of_node_put(np);
660   -
661   - return 0;
662   -}
663   -
664   -static void fetch_cpu_pumps_minmax(void)
665   -{
666   - struct cpu_pid_state *state0 = &processor_state[0];
667   - struct cpu_pid_state *state1 = &processor_state[1];
668   - u16 pump_min = 0, pump_max = 0xffff;
669   - u16 tmp[4];
670   -
671   - /* Try to fetch pumps min/max infos from eeprom */
672   -
673   - memcpy(&tmp, &state0->mpu.processor_part_num, 8);
674   - if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
675   - pump_min = max(pump_min, tmp[0]);
676   - pump_max = min(pump_max, tmp[1]);
677   - }
678   - if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
679   - pump_min = max(pump_min, tmp[2]);
680   - pump_max = min(pump_max, tmp[3]);
681   - }
682   -
683   - /* Double check the values, this _IS_ needed as the EEPROM on
684   - * some dual 2.5Ghz G5s seem, at least, to have both min & max
685   - * same to the same value ... (grrrr)
686   - */
687   - if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
688   - pump_min = CPU_PUMP_OUTPUT_MIN;
689   - pump_max = CPU_PUMP_OUTPUT_MAX;
690   - }
691   -
692   - state0->pump_min = state1->pump_min = pump_min;
693   - state0->pump_max = state1->pump_max = pump_max;
694   -}
695   -
696   -/*
697   - * Now, unfortunately, sysfs doesn't give us a nice void * we could
698   - * pass around to the attribute functions, so we don't really have
699   - * choice but implement a bunch of them...
700   - *
701   - * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
702   - * the input twice... I accept patches :)
703   - */
704   -#define BUILD_SHOW_FUNC_FIX(name, data) \
705   -static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
706   -{ \
707   - ssize_t r; \
708   - mutex_lock(&driver_lock); \
709   - r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \
710   - mutex_unlock(&driver_lock); \
711   - return r; \
712   -}
713   -#define BUILD_SHOW_FUNC_INT(name, data) \
714   -static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
715   -{ \
716   - return sprintf(buf, "%d", data); \
717   -}
718   -
719   -BUILD_SHOW_FUNC_FIX(cpu0_temperature, processor_state[0].last_temp)
720   -BUILD_SHOW_FUNC_FIX(cpu0_voltage, processor_state[0].voltage)
721   -BUILD_SHOW_FUNC_FIX(cpu0_current, processor_state[0].current_a)
722   -BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, processor_state[0].rpm)
723   -BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, processor_state[0].intake_rpm)
724   -
725   -BUILD_SHOW_FUNC_FIX(cpu1_temperature, processor_state[1].last_temp)
726   -BUILD_SHOW_FUNC_FIX(cpu1_voltage, processor_state[1].voltage)
727   -BUILD_SHOW_FUNC_FIX(cpu1_current, processor_state[1].current_a)
728   -BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, processor_state[1].rpm)
729   -BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, processor_state[1].intake_rpm)
730   -
731   -BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
732   -BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
733   -
734   -BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
735   -BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
736   -
737   -BUILD_SHOW_FUNC_FIX(slots_temperature, slots_state.last_temp)
738   -BUILD_SHOW_FUNC_INT(slots_fan_pwm, slots_state.pwm)
739   -
740   -BUILD_SHOW_FUNC_FIX(dimms_temperature, dimms_state.last_temp)
741   -
742   -static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
743   -static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
744   -static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
745   -static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
746   -static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
747   -
748   -static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
749   -static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
750   -static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
751   -static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
752   -static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
753   -
754   -static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
755   -static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
756   -
757   -static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
758   -static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
759   -
760   -static DEVICE_ATTR(slots_temperature,S_IRUGO,show_slots_temperature,NULL);
761   -static DEVICE_ATTR(slots_fan_pwm,S_IRUGO,show_slots_fan_pwm,NULL);
762   -
763   -static DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL);
764   -
765   -/*
766   - * CPUs fans control loop
767   - */
768   -
769   -static int do_read_one_cpu_values(struct cpu_pid_state *state, s32 *temp, s32 *power)
770   -{
771   - s32 ltemp, volts, amps;
772   - int index, rc = 0;
773   -
774   - /* Default (in case of error) */
775   - *temp = state->cur_temp;
776   - *power = state->cur_power;
777   -
778   - if (cpu_pid_type == CPU_PID_TYPE_RACKMAC)
779   - index = (state->index == 0) ?
780   - CPU_A1_FAN_RPM_INDEX : CPU_B1_FAN_RPM_INDEX;
781   - else
782   - index = (state->index == 0) ?
783   - CPUA_EXHAUST_FAN_RPM_INDEX : CPUB_EXHAUST_FAN_RPM_INDEX;
784   -
785   - /* Read current fan status */
786   - rc = get_rpm_fan(index, !RPM_PID_USE_ACTUAL_SPEED);
787   - if (rc < 0) {
788   - /* XXX What do we do now ? Nothing for now, keep old value, but
789   - * return error upstream
790   - */
791   - DBG(" cpu %d, fan reading error !\n", state->index);
792   - } else {
793   - state->rpm = rc;
794   - DBG(" cpu %d, exhaust RPM: %d\n", state->index, state->rpm);
795   - }
796   -
797   - /* Get some sensor readings and scale it */
798   - ltemp = read_smon_adc(state, 1);
799   - if (ltemp == -1) {
800   - /* XXX What do we do now ? */
801   - state->overtemp++;
802   - if (rc == 0)
803   - rc = -EIO;
804   - DBG(" cpu %d, temp reading error !\n", state->index);
805   - } else {
806   - /* Fixup temperature according to diode calibration
807   - */
808   - DBG(" cpu %d, temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
809   - state->index,
810   - ltemp, state->mpu.mdiode, state->mpu.bdiode);
811   - *temp = ((s32)ltemp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2;
812   - state->last_temp = *temp;
813   - DBG(" temp: %d.%03d\n", FIX32TOPRINT((*temp)));
814   - }
815   -
816   - /*
817   - * Read voltage & current and calculate power
818   - */
819   - volts = read_smon_adc(state, 3);
820   - amps = read_smon_adc(state, 4);
821   -
822   - /* Scale voltage and current raw sensor values according to fixed scales
823   - * obtained in Darwin and calculate power from I and V
824   - */
825   - volts *= ADC_CPU_VOLTAGE_SCALE;
826   - amps *= ADC_CPU_CURRENT_SCALE;
827   - *power = (((u64)volts) * ((u64)amps)) >> 16;
828   - state->voltage = volts;
829   - state->current_a = amps;
830   - state->last_power = *power;
831   -
832   - DBG(" cpu %d, current: %d.%03d, voltage: %d.%03d, power: %d.%03d W\n",
833   - state->index, FIX32TOPRINT(state->current_a),
834   - FIX32TOPRINT(state->voltage), FIX32TOPRINT(*power));
835   -
836   - return 0;
837   -}
838   -
839   -static void do_cpu_pid(struct cpu_pid_state *state, s32 temp, s32 power)
840   -{
841   - s32 power_target, integral, derivative, proportional, adj_in_target, sval;
842   - s64 integ_p, deriv_p, prop_p, sum;
843   - int i;
844   -
845   - /* Calculate power target value (could be done once for all)
846   - * and convert to a 16.16 fp number
847   - */
848   - power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16;
849   - DBG(" power target: %d.%03d, error: %d.%03d\n",
850   - FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power));
851   -
852   - /* Store temperature and power in history array */
853   - state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
854   - state->temp_history[state->cur_temp] = temp;
855   - state->cur_power = (state->cur_power + 1) % state->count_power;
856   - state->power_history[state->cur_power] = power;
857   - state->error_history[state->cur_power] = power_target - power;
858   -
859   - /* If first loop, fill the history table */
860   - if (state->first) {
861   - for (i = 0; i < (state->count_power - 1); i++) {
862   - state->cur_power = (state->cur_power + 1) % state->count_power;
863   - state->power_history[state->cur_power] = power;
864   - state->error_history[state->cur_power] = power_target - power;
865   - }
866   - for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) {
867   - state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
868   - state->temp_history[state->cur_temp] = temp;
869   - }
870   - state->first = 0;
871   - }
872   -
873   - /* Calculate the integral term normally based on the "power" values */
874   - sum = 0;
875   - integral = 0;
876   - for (i = 0; i < state->count_power; i++)
877   - integral += state->error_history[i];
878   - integral *= CPU_PID_INTERVAL;
879   - DBG(" integral: %08x\n", integral);
880   -
881   - /* Calculate the adjusted input (sense value).
882   - * G_r is 12.20
883   - * integ is 16.16
884   - * so the result is 28.36
885   - *
886   - * input target is mpu.ttarget, input max is mpu.tmax
887   - */
888   - integ_p = ((s64)state->mpu.pid_gr) * (s64)integral;
889   - DBG(" integ_p: %d\n", (int)(integ_p >> 36));
890   - sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
891   - adj_in_target = (state->mpu.ttarget << 16);
892   - if (adj_in_target > sval)
893   - adj_in_target = sval;
894   - DBG(" adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target),
895   - state->mpu.ttarget);
896   -
897   - /* Calculate the derivative term */
898   - derivative = state->temp_history[state->cur_temp] -
899   - state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1)
900   - % CPU_TEMP_HISTORY_SIZE];
901   - derivative /= CPU_PID_INTERVAL;
902   - deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative;
903   - DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
904   - sum += deriv_p;
905   -
906   - /* Calculate the proportional term */
907   - proportional = temp - adj_in_target;
908   - prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional;
909   - DBG(" prop_p: %d\n", (int)(prop_p >> 36));
910   - sum += prop_p;
911   -
912   - /* Scale sum */
913   - sum >>= 36;
914   -
915   - DBG(" sum: %d\n", (int)sum);
916   - state->rpm += (s32)sum;
917   -}
918   -
919   -static void do_monitor_cpu_combined(void)
920   -{
921   - struct cpu_pid_state *state0 = &processor_state[0];
922   - struct cpu_pid_state *state1 = &processor_state[1];
923   - s32 temp0, power0, temp1, power1;
924   - s32 temp_combi, power_combi;
925   - int rc, intake, pump;
926   -
927   - rc = do_read_one_cpu_values(state0, &temp0, &power0);
928   - if (rc < 0) {
929   - /* XXX What do we do now ? */
930   - }
931   - state1->overtemp = 0;
932   - rc = do_read_one_cpu_values(state1, &temp1, &power1);
933   - if (rc < 0) {
934   - /* XXX What do we do now ? */
935   - }
936   - if (state1->overtemp)
937   - state0->overtemp++;
938   -
939   - temp_combi = max(temp0, temp1);
940   - power_combi = max(power0, power1);
941   -
942   - /* Check tmax, increment overtemp if we are there. At tmax+8, we go
943   - * full blown immediately and try to trigger a shutdown
944   - */
945   - if (temp_combi >= ((state0->mpu.tmax + 8) << 16)) {
946   - printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n",
947   - temp_combi >> 16);
948   - state0->overtemp += CPU_MAX_OVERTEMP / 4;
949   - } else if (temp_combi > (state0->mpu.tmax << 16)) {
950   - state0->overtemp++;
951   - printk(KERN_WARNING "Temperature %d above max %d. overtemp %d\n",
952   - temp_combi >> 16, state0->mpu.tmax, state0->overtemp);
953   - } else {
954   - if (state0->overtemp)
955   - printk(KERN_WARNING "Temperature back down to %d\n",
956   - temp_combi >> 16);
957   - state0->overtemp = 0;
958   - }
959   - if (state0->overtemp >= CPU_MAX_OVERTEMP)
960   - critical_state = 1;
961   - if (state0->overtemp > 0) {
962   - state0->rpm = state0->mpu.rmaxn_exhaust_fan;
963   - state0->intake_rpm = intake = state0->mpu.rmaxn_intake_fan;
964   - pump = state0->pump_max;
965   - goto do_set_fans;
966   - }
967   -
968   - /* Do the PID */
969   - do_cpu_pid(state0, temp_combi, power_combi);
970   -
971   - /* Range check */
972   - state0->rpm = max(state0->rpm, (int)state0->mpu.rminn_exhaust_fan);
973   - state0->rpm = min(state0->rpm, (int)state0->mpu.rmaxn_exhaust_fan);
974   -
975   - /* Calculate intake fan speed */
976   - intake = (state0->rpm * CPU_INTAKE_SCALE) >> 16;
977   - intake = max(intake, (int)state0->mpu.rminn_intake_fan);
978   - intake = min(intake, (int)state0->mpu.rmaxn_intake_fan);
979   - state0->intake_rpm = intake;
980   -
981   - /* Calculate pump speed */
982   - pump = (state0->rpm * state0->pump_max) /
983   - state0->mpu.rmaxn_exhaust_fan;
984   - pump = min(pump, state0->pump_max);
985   - pump = max(pump, state0->pump_min);
986   -
987   - do_set_fans:
988   - /* We copy values from state 0 to state 1 for /sysfs */
989   - state1->rpm = state0->rpm;
990   - state1->intake_rpm = state0->intake_rpm;
991   -
992   - DBG("** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n",
993   - state1->index, (int)state1->rpm, intake, pump, state1->overtemp);
994   -
995   - /* We should check for errors, shouldn't we ? But then, what
996   - * do we do once the error occurs ? For FCU notified fan
997   - * failures (-EFAULT) we probably want to notify userland
998   - * some way...
999   - */
1000   - set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1001   - set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1002   - set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1003   - set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1004   -
1005   - if (fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1006   - set_rpm_fan(CPUA_PUMP_RPM_INDEX, pump);
1007   - if (fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1008   - set_rpm_fan(CPUB_PUMP_RPM_INDEX, pump);
1009   -}
1010   -
1011   -static void do_monitor_cpu_split(struct cpu_pid_state *state)
1012   -{
1013   - s32 temp, power;
1014   - int rc, intake;
1015   -
1016   - /* Read current fan status */
1017   - rc = do_read_one_cpu_values(state, &temp, &power);
1018   - if (rc < 0) {
1019   - /* XXX What do we do now ? */
1020   - }
1021   -
1022   - /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1023   - * full blown immediately and try to trigger a shutdown
1024   - */
1025   - if (temp >= ((state->mpu.tmax + 8) << 16)) {
1026   - printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1027   - " (%d) !\n",
1028   - state->index, temp >> 16);
1029   - state->overtemp += CPU_MAX_OVERTEMP / 4;
1030   - } else if (temp > (state->mpu.tmax << 16)) {
1031   - state->overtemp++;
1032   - printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
1033   - state->index, temp >> 16, state->mpu.tmax, state->overtemp);
1034   - } else {
1035   - if (state->overtemp)
1036   - printk(KERN_WARNING "CPU %d temperature back down to %d\n",
1037   - state->index, temp >> 16);
1038   - state->overtemp = 0;
1039   - }
1040   - if (state->overtemp >= CPU_MAX_OVERTEMP)
1041   - critical_state = 1;
1042   - if (state->overtemp > 0) {
1043   - state->rpm = state->mpu.rmaxn_exhaust_fan;
1044   - state->intake_rpm = intake = state->mpu.rmaxn_intake_fan;
1045   - goto do_set_fans;
1046   - }
1047   -
1048   - /* Do the PID */
1049   - do_cpu_pid(state, temp, power);
1050   -
1051   - /* Range check */
1052   - state->rpm = max(state->rpm, (int)state->mpu.rminn_exhaust_fan);
1053   - state->rpm = min(state->rpm, (int)state->mpu.rmaxn_exhaust_fan);
1054   -
1055   - /* Calculate intake fan */
1056   - intake = (state->rpm * CPU_INTAKE_SCALE) >> 16;
1057   - intake = max(intake, (int)state->mpu.rminn_intake_fan);
1058   - intake = min(intake, (int)state->mpu.rmaxn_intake_fan);
1059   - state->intake_rpm = intake;
1060   -
1061   - do_set_fans:
1062   - DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
1063   - state->index, (int)state->rpm, intake, state->overtemp);
1064   -
1065   - /* We should check for errors, shouldn't we ? But then, what
1066   - * do we do once the error occurs ? For FCU notified fan
1067   - * failures (-EFAULT) we probably want to notify userland
1068   - * some way...
1069   - */
1070   - if (state->index == 0) {
1071   - set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1072   - set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state->rpm);
1073   - } else {
1074   - set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1075   - set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state->rpm);
1076   - }
1077   -}
1078   -
1079   -static void do_monitor_cpu_rack(struct cpu_pid_state *state)
1080   -{
1081   - s32 temp, power, fan_min;
1082   - int rc;
1083   -
1084   - /* Read current fan status */
1085   - rc = do_read_one_cpu_values(state, &temp, &power);
1086   - if (rc < 0) {
1087   - /* XXX What do we do now ? */
1088   - }
1089   -
1090   - /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1091   - * full blown immediately and try to trigger a shutdown
1092   - */
1093   - if (temp >= ((state->mpu.tmax + 8) << 16)) {
1094   - printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1095   - " (%d) !\n",
1096   - state->index, temp >> 16);
1097   - state->overtemp = CPU_MAX_OVERTEMP / 4;
1098   - } else if (temp > (state->mpu.tmax << 16)) {
1099   - state->overtemp++;
1100   - printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
1101   - state->index, temp >> 16, state->mpu.tmax, state->overtemp);
1102   - } else {
1103   - if (state->overtemp)
1104   - printk(KERN_WARNING "CPU %d temperature back down to %d\n",
1105   - state->index, temp >> 16);
1106   - state->overtemp = 0;
1107   - }
1108   - if (state->overtemp >= CPU_MAX_OVERTEMP)
1109   - critical_state = 1;
1110   - if (state->overtemp > 0) {
1111   - state->rpm = state->intake_rpm = state->mpu.rmaxn_intake_fan;
1112   - goto do_set_fans;
1113   - }
1114   -
1115   - /* Do the PID */
1116   - do_cpu_pid(state, temp, power);
1117   -
1118   - /* Check clamp from dimms */
1119   - fan_min = dimm_output_clamp;
1120   - fan_min = max(fan_min, (int)state->mpu.rminn_intake_fan);
1121   -
1122   - DBG(" CPU min mpu = %d, min dimm = %d\n",
1123   - state->mpu.rminn_intake_fan, dimm_output_clamp);
1124   -
1125   - state->rpm = max(state->rpm, (int)fan_min);
1126   - state->rpm = min(state->rpm, (int)state->mpu.rmaxn_intake_fan);
1127   - state->intake_rpm = state->rpm;
1128   -
1129   - do_set_fans:
1130   - DBG("** CPU %d RPM: %d overtemp: %d\n",
1131   - state->index, (int)state->rpm, state->overtemp);
1132   -
1133   - /* We should check for errors, shouldn't we ? But then, what
1134   - * do we do once the error occurs ? For FCU notified fan
1135   - * failures (-EFAULT) we probably want to notify userland
1136   - * some way...
1137   - */
1138   - if (state->index == 0) {
1139   - set_rpm_fan(CPU_A1_FAN_RPM_INDEX, state->rpm);
1140   - set_rpm_fan(CPU_A2_FAN_RPM_INDEX, state->rpm);
1141   - set_rpm_fan(CPU_A3_FAN_RPM_INDEX, state->rpm);
1142   - } else {
1143   - set_rpm_fan(CPU_B1_FAN_RPM_INDEX, state->rpm);
1144   - set_rpm_fan(CPU_B2_FAN_RPM_INDEX, state->rpm);
1145   - set_rpm_fan(CPU_B3_FAN_RPM_INDEX, state->rpm);
1146   - }
1147   -}
1148   -
1149   -/*
1150   - * Initialize the state structure for one CPU control loop
1151   - */
1152   -static int init_processor_state(struct cpu_pid_state *state, int index)
1153   -{
1154   - int err;
1155   -
1156   - state->index = index;
1157   - state->first = 1;
1158   - state->rpm = (cpu_pid_type == CPU_PID_TYPE_RACKMAC) ? 4000 : 1000;
1159   - state->overtemp = 0;
1160   - state->adc_config = 0x00;
1161   -
1162   -
1163   - if (index == 0)
1164   - state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor");
1165   - else if (index == 1)
1166   - state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor");
1167   - if (state->monitor == NULL)
1168   - goto fail;
1169   -
1170   - if (read_eeprom(index, &state->mpu))
1171   - goto fail;
1172   -
1173   - state->count_power = state->mpu.tguardband;
1174   - if (state->count_power > CPU_POWER_HISTORY_SIZE) {
1175   - printk(KERN_WARNING "Warning ! too many power history slots\n");
1176   - state->count_power = CPU_POWER_HISTORY_SIZE;
1177   - }
1178   - DBG("CPU %d Using %d power history entries\n", index, state->count_power);
1179   -
1180   - if (index == 0) {
1181   - err = device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1182   - err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1183   - err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_current);
1184   - err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1185   - err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1186   - } else {
1187   - err = device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1188   - err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1189   - err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_current);
1190   - err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1191   - err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1192   - }
1193   - if (err)
1194   - printk(KERN_WARNING "Failed to create some of the attribute"
1195   - "files for CPU %d\n", index);
1196   -
1197   - return 0;
1198   - fail:
1199   - state->monitor = NULL;
1200   -
1201   - return -ENODEV;
1202   -}
1203   -
1204   -/*
1205   - * Dispose of the state data for one CPU control loop
1206   - */
1207   -static void dispose_processor_state(struct cpu_pid_state *state)
1208   -{
1209   - if (state->monitor == NULL)
1210   - return;
1211   -
1212   - if (state->index == 0) {
1213   - device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1214   - device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1215   - device_remove_file(&of_dev->dev, &dev_attr_cpu0_current);
1216   - device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1217   - device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1218   - } else {
1219   - device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1220   - device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1221   - device_remove_file(&of_dev->dev, &dev_attr_cpu1_current);
1222   - device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1223   - device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1224   - }
1225   -
1226   - state->monitor = NULL;
1227   -}
1228   -
1229   -/*
1230   - * Motherboard backside & U3 heatsink fan control loop
1231   - */
1232   -static void do_monitor_backside(struct backside_pid_state *state)
1233   -{
1234   - s32 temp, integral, derivative, fan_min;
1235   - s64 integ_p, deriv_p, prop_p, sum;
1236   - int i, rc;
1237   -
1238   - if (--state->ticks != 0)
1239   - return;
1240   - state->ticks = backside_params.interval;
1241   -
1242   - DBG("backside:\n");
1243   -
1244   - /* Check fan status */
1245   - rc = get_pwm_fan(BACKSIDE_FAN_PWM_INDEX);
1246   - if (rc < 0) {
1247   - printk(KERN_WARNING "Error %d reading backside fan !\n", rc);
1248   - /* XXX What do we do now ? */
1249   - } else
1250   - state->pwm = rc;
1251   - DBG(" current pwm: %d\n", state->pwm);
1252   -
1253   - /* Get some sensor readings */
1254   - temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16;
1255   - state->last_temp = temp;
1256   - DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1257   - FIX32TOPRINT(backside_params.input_target));
1258   -
1259   - /* Store temperature and error in history array */
1260   - state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE;
1261   - state->sample_history[state->cur_sample] = temp;
1262   - state->error_history[state->cur_sample] = temp - backside_params.input_target;
1263   -
1264   - /* If first loop, fill the history table */
1265   - if (state->first) {
1266   - for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) {
1267   - state->cur_sample = (state->cur_sample + 1) %
1268   - BACKSIDE_PID_HISTORY_SIZE;
1269   - state->sample_history[state->cur_sample] = temp;
1270   - state->error_history[state->cur_sample] =
1271   - temp - backside_params.input_target;
1272   - }
1273   - state->first = 0;
1274   - }
1275   -
1276   - /* Calculate the integral term */
1277   - sum = 0;
1278   - integral = 0;
1279   - for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++)
1280   - integral += state->error_history[i];
1281   - integral *= backside_params.interval;
1282   - DBG(" integral: %08x\n", integral);
1283   - integ_p = ((s64)backside_params.G_r) * (s64)integral;
1284   - DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1285   - sum += integ_p;
1286   -
1287   - /* Calculate the derivative term */
1288   - derivative = state->error_history[state->cur_sample] -
1289   - state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1)
1290   - % BACKSIDE_PID_HISTORY_SIZE];
1291   - derivative /= backside_params.interval;
1292   - deriv_p = ((s64)backside_params.G_d) * (s64)derivative;
1293   - DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1294   - sum += deriv_p;
1295   -
1296   - /* Calculate the proportional term */
1297   - prop_p = ((s64)backside_params.G_p) * (s64)(state->error_history[state->cur_sample]);
1298   - DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1299   - sum += prop_p;
1300   -
1301   - /* Scale sum */
1302   - sum >>= 36;
1303   -
1304   - DBG(" sum: %d\n", (int)sum);
1305   - if (backside_params.additive)
1306   - state->pwm += (s32)sum;
1307   - else
1308   - state->pwm = sum;
1309   -
1310   - /* Check for clamp */
1311   - fan_min = (dimm_output_clamp * 100) / 14000;
1312   - fan_min = max(fan_min, backside_params.output_min);
1313   -
1314   - state->pwm = max(state->pwm, fan_min);
1315   - state->pwm = min(state->pwm, backside_params.output_max);
1316   -
1317   - DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
1318   - set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, state->pwm);
1319   -}
1320   -
1321   -/*
1322   - * Initialize the state structure for the backside fan control loop
1323   - */
1324   -static int init_backside_state(struct backside_pid_state *state)
1325   -{
1326   - struct device_node *u3;
1327   - int u3h = 1; /* conservative by default */
1328   - int err;
1329   -
1330   - /*
1331   - * There are different PID params for machines with U3 and machines
1332   - * with U3H, pick the right ones now
1333   - */
1334   - u3 = of_find_node_by_path("/u3@0,f8000000");
1335   - if (u3 != NULL) {
1336   - const u32 *vers = of_get_property(u3, "device-rev", NULL);
1337   - if (vers)
1338   - if (((*vers) & 0x3f) < 0x34)
1339   - u3h = 0;
1340   - of_node_put(u3);
1341   - }
1342   -
1343   - if (rackmac) {
1344   - backside_params.G_d = BACKSIDE_PID_RACK_G_d;
1345   - backside_params.input_target = BACKSIDE_PID_RACK_INPUT_TARGET;
1346   - backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1347   - backside_params.interval = BACKSIDE_PID_RACK_INTERVAL;
1348   - backside_params.G_p = BACKSIDE_PID_RACK_G_p;
1349   - backside_params.G_r = BACKSIDE_PID_G_r;
1350   - backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1351   - backside_params.additive = 0;
1352   - } else if (u3h) {
1353   - backside_params.G_d = BACKSIDE_PID_U3H_G_d;
1354   - backside_params.input_target = BACKSIDE_PID_U3H_INPUT_TARGET;
1355   - backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1356   - backside_params.interval = BACKSIDE_PID_INTERVAL;
1357   - backside_params.G_p = BACKSIDE_PID_G_p;
1358   - backside_params.G_r = BACKSIDE_PID_G_r;
1359   - backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1360   - backside_params.additive = 1;
1361   - } else {
1362   - backside_params.G_d = BACKSIDE_PID_U3_G_d;
1363   - backside_params.input_target = BACKSIDE_PID_U3_INPUT_TARGET;
1364   - backside_params.output_min = BACKSIDE_PID_U3_OUTPUT_MIN;
1365   - backside_params.interval = BACKSIDE_PID_INTERVAL;
1366   - backside_params.G_p = BACKSIDE_PID_G_p;
1367   - backside_params.G_r = BACKSIDE_PID_G_r;
1368   - backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1369   - backside_params.additive = 1;
1370   - }
1371   -
1372   - state->ticks = 1;
1373   - state->first = 1;
1374   - state->pwm = 50;
1375   -
1376   - state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
1377   - if (state->monitor == NULL)
1378   - return -ENODEV;
1379   -
1380   - err = device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
1381   - err |= device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1382   - if (err)
1383   - printk(KERN_WARNING "Failed to create attribute file(s)"
1384   - " for backside fan\n");
1385   -
1386   - return 0;
1387   -}
1388   -
1389   -/*
1390   - * Dispose of the state data for the backside control loop
1391   - */
1392   -static void dispose_backside_state(struct backside_pid_state *state)
1393   -{
1394   - if (state->monitor == NULL)
1395   - return;
1396   -
1397   - device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
1398   - device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1399   -
1400   - state->monitor = NULL;
1401   -}
1402   -
1403   -/*
1404   - * Drives bay fan control loop
1405   - */
1406   -static void do_monitor_drives(struct drives_pid_state *state)
1407   -{
1408   - s32 temp, integral, derivative;
1409   - s64 integ_p, deriv_p, prop_p, sum;
1410   - int i, rc;
1411   -
1412   - if (--state->ticks != 0)
1413   - return;
1414   - state->ticks = DRIVES_PID_INTERVAL;
1415   -
1416   - DBG("drives:\n");
1417   -
1418   - /* Check fan status */
1419   - rc = get_rpm_fan(DRIVES_FAN_RPM_INDEX, !RPM_PID_USE_ACTUAL_SPEED);
1420   - if (rc < 0) {
1421   - printk(KERN_WARNING "Error %d reading drives fan !\n", rc);
1422   - /* XXX What do we do now ? */
1423   - } else
1424   - state->rpm = rc;
1425   - DBG(" current rpm: %d\n", state->rpm);
1426   -
1427   - /* Get some sensor readings */
1428   - temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1429   - DS1775_TEMP)) << 8;
1430   - state->last_temp = temp;
1431   - DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1432   - FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
1433   -
1434   - /* Store temperature and error in history array */
1435   - state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE;
1436   - state->sample_history[state->cur_sample] = temp;
1437   - state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET;
1438   -
1439   - /* If first loop, fill the history table */
1440   - if (state->first) {
1441   - for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) {
1442   - state->cur_sample = (state->cur_sample + 1) %
1443   - DRIVES_PID_HISTORY_SIZE;
1444   - state->sample_history[state->cur_sample] = temp;
1445   - state->error_history[state->cur_sample] =
1446   - temp - DRIVES_PID_INPUT_TARGET;
1447   - }
1448   - state->first = 0;
1449   - }
1450   -
1451   - /* Calculate the integral term */
1452   - sum = 0;
1453   - integral = 0;
1454   - for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++)
1455   - integral += state->error_history[i];
1456   - integral *= DRIVES_PID_INTERVAL;
1457   - DBG(" integral: %08x\n", integral);
1458   - integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral;
1459   - DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1460   - sum += integ_p;
1461   -
1462   - /* Calculate the derivative term */
1463   - derivative = state->error_history[state->cur_sample] -
1464   - state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1)
1465   - % DRIVES_PID_HISTORY_SIZE];
1466   - derivative /= DRIVES_PID_INTERVAL;
1467   - deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative;
1468   - DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1469   - sum += deriv_p;
1470   -
1471   - /* Calculate the proportional term */
1472   - prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1473   - DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1474   - sum += prop_p;
1475   -
1476   - /* Scale sum */
1477   - sum >>= 36;
1478   -
1479   - DBG(" sum: %d\n", (int)sum);
1480   - state->rpm += (s32)sum;
1481   -
1482   - state->rpm = max(state->rpm, DRIVES_PID_OUTPUT_MIN);
1483   - state->rpm = min(state->rpm, DRIVES_PID_OUTPUT_MAX);
1484   -
1485   - DBG("** DRIVES RPM: %d\n", (int)state->rpm);
1486   - set_rpm_fan(DRIVES_FAN_RPM_INDEX, state->rpm);
1487   -}
1488   -
1489   -/*
1490   - * Initialize the state structure for the drives bay fan control loop
1491   - */
1492   -static int init_drives_state(struct drives_pid_state *state)
1493   -{
1494   - int err;
1495   -
1496   - state->ticks = 1;
1497   - state->first = 1;
1498   - state->rpm = 1000;
1499   -
1500   - state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
1501   - if (state->monitor == NULL)
1502   - return -ENODEV;
1503   -
1504   - err = device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
1505   - err |= device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1506   - if (err)
1507   - printk(KERN_WARNING "Failed to create attribute file(s)"
1508   - " for drives bay fan\n");
1509   -
1510   - return 0;
1511   -}
1512   -
1513   -/*
1514   - * Dispose of the state data for the drives control loop
1515   - */
1516   -static void dispose_drives_state(struct drives_pid_state *state)
1517   -{
1518   - if (state->monitor == NULL)
1519   - return;
1520   -
1521   - device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
1522   - device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1523   -
1524   - state->monitor = NULL;
1525   -}
1526   -
1527   -/*
1528   - * DIMMs temp control loop
1529   - */
1530   -static void do_monitor_dimms(struct dimm_pid_state *state)
1531   -{
1532   - s32 temp, integral, derivative, fan_min;
1533   - s64 integ_p, deriv_p, prop_p, sum;
1534   - int i;
1535   -
1536   - if (--state->ticks != 0)
1537   - return;
1538   - state->ticks = DIMM_PID_INTERVAL;
1539   -
1540   - DBG("DIMM:\n");
1541   -
1542   - DBG(" current value: %d\n", state->output);
1543   -
1544   - temp = read_lm87_reg(state->monitor, LM87_INT_TEMP);
1545   - if (temp < 0)
1546   - return;
1547   - temp <<= 16;
1548   - state->last_temp = temp;
1549   - DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1550   - FIX32TOPRINT(DIMM_PID_INPUT_TARGET));
1551   -
1552   - /* Store temperature and error in history array */
1553   - state->cur_sample = (state->cur_sample + 1) % DIMM_PID_HISTORY_SIZE;
1554   - state->sample_history[state->cur_sample] = temp;
1555   - state->error_history[state->cur_sample] = temp - DIMM_PID_INPUT_TARGET;
1556   -
1557   - /* If first loop, fill the history table */
1558   - if (state->first) {
1559   - for (i = 0; i < (DIMM_PID_HISTORY_SIZE - 1); i++) {
1560   - state->cur_sample = (state->cur_sample + 1) %
1561   - DIMM_PID_HISTORY_SIZE;
1562   - state->sample_history[state->cur_sample] = temp;
1563   - state->error_history[state->cur_sample] =
1564   - temp - DIMM_PID_INPUT_TARGET;
1565   - }
1566   - state->first = 0;
1567   - }
1568   -
1569   - /* Calculate the integral term */
1570   - sum = 0;
1571   - integral = 0;
1572   - for (i = 0; i < DIMM_PID_HISTORY_SIZE; i++)
1573   - integral += state->error_history[i];
1574   - integral *= DIMM_PID_INTERVAL;
1575   - DBG(" integral: %08x\n", integral);
1576   - integ_p = ((s64)DIMM_PID_G_r) * (s64)integral;
1577   - DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1578   - sum += integ_p;
1579   -
1580   - /* Calculate the derivative term */
1581   - derivative = state->error_history[state->cur_sample] -
1582   - state->error_history[(state->cur_sample + DIMM_PID_HISTORY_SIZE - 1)
1583   - % DIMM_PID_HISTORY_SIZE];
1584   - derivative /= DIMM_PID_INTERVAL;
1585   - deriv_p = ((s64)DIMM_PID_G_d) * (s64)derivative;
1586   - DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1587   - sum += deriv_p;
1588   -
1589   - /* Calculate the proportional term */
1590   - prop_p = ((s64)DIMM_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1591   - DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1592   - sum += prop_p;
1593   -
1594   - /* Scale sum */
1595   - sum >>= 36;
1596   -
1597   - DBG(" sum: %d\n", (int)sum);
1598   - state->output = (s32)sum;
1599   - state->output = max(state->output, DIMM_PID_OUTPUT_MIN);
1600   - state->output = min(state->output, DIMM_PID_OUTPUT_MAX);
1601   - dimm_output_clamp = state->output;
1602   -
1603   - DBG("** DIMM clamp value: %d\n", (int)state->output);
1604   -
1605   - /* Backside PID is only every 5 seconds, force backside fan clamping now */
1606   - fan_min = (dimm_output_clamp * 100) / 14000;
1607   - fan_min = max(fan_min, backside_params.output_min);
1608   - if (backside_state.pwm < fan_min) {
1609   - backside_state.pwm = fan_min;
1610   - DBG(" -> applying clamp to backside fan now: %d !\n", fan_min);
1611   - set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, fan_min);
1612   - }
1613   -}
1614   -
1615   -/*
1616   - * Initialize the state structure for the DIMM temp control loop
1617   - */
1618   -static int init_dimms_state(struct dimm_pid_state *state)
1619   -{
1620   - state->ticks = 1;
1621   - state->first = 1;
1622   - state->output = 4000;
1623   -
1624   - state->monitor = attach_i2c_chip(XSERVE_DIMMS_LM87, "dimms_temp");
1625   - if (state->monitor == NULL)
1626   - return -ENODEV;
1627   -
1628   - if (device_create_file(&of_dev->dev, &dev_attr_dimms_temperature))
1629   - printk(KERN_WARNING "Failed to create attribute file"
1630   - " for DIMM temperature\n");
1631   -
1632   - return 0;
1633   -}
1634   -
1635   -/*
1636   - * Dispose of the state data for the DIMM control loop
1637   - */
1638   -static void dispose_dimms_state(struct dimm_pid_state *state)
1639   -{
1640   - if (state->monitor == NULL)
1641   - return;
1642   -
1643   - device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature);
1644   -
1645   - state->monitor = NULL;
1646   -}
1647   -
1648   -/*
1649   - * Slots fan control loop
1650   - */
1651   -static void do_monitor_slots(struct slots_pid_state *state)
1652   -{
1653   - s32 temp, integral, derivative;
1654   - s64 integ_p, deriv_p, prop_p, sum;
1655   - int i, rc;
1656   -
1657   - if (--state->ticks != 0)
1658   - return;
1659   - state->ticks = SLOTS_PID_INTERVAL;
1660   -
1661   - DBG("slots:\n");
1662   -
1663   - /* Check fan status */
1664   - rc = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
1665   - if (rc < 0) {
1666   - printk(KERN_WARNING "Error %d reading slots fan !\n", rc);
1667   - /* XXX What do we do now ? */
1668   - } else
1669   - state->pwm = rc;
1670   - DBG(" current pwm: %d\n", state->pwm);
1671   -
1672   - /* Get some sensor readings */
1673   - temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1674   - DS1775_TEMP)) << 8;
1675   - state->last_temp = temp;
1676   - DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1677   - FIX32TOPRINT(SLOTS_PID_INPUT_TARGET));
1678   -
1679   - /* Store temperature and error in history array */
1680   - state->cur_sample = (state->cur_sample + 1) % SLOTS_PID_HISTORY_SIZE;
1681   - state->sample_history[state->cur_sample] = temp;
1682   - state->error_history[state->cur_sample] = temp - SLOTS_PID_INPUT_TARGET;
1683   -
1684   - /* If first loop, fill the history table */
1685   - if (state->first) {
1686   - for (i = 0; i < (SLOTS_PID_HISTORY_SIZE - 1); i++) {
1687   - state->cur_sample = (state->cur_sample + 1) %
1688   - SLOTS_PID_HISTORY_SIZE;
1689   - state->sample_history[state->cur_sample] = temp;
1690   - state->error_history[state->cur_sample] =
1691   - temp - SLOTS_PID_INPUT_TARGET;
1692   - }
1693   - state->first = 0;
1694   - }
1695   -
1696   - /* Calculate the integral term */
1697   - sum = 0;
1698   - integral = 0;
1699   - for (i = 0; i < SLOTS_PID_HISTORY_SIZE; i++)
1700   - integral += state->error_history[i];
1701   - integral *= SLOTS_PID_INTERVAL;
1702   - DBG(" integral: %08x\n", integral);
1703   - integ_p = ((s64)SLOTS_PID_G_r) * (s64)integral;
1704   - DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1705   - sum += integ_p;
1706   -
1707   - /* Calculate the derivative term */
1708   - derivative = state->error_history[state->cur_sample] -
1709   - state->error_history[(state->cur_sample + SLOTS_PID_HISTORY_SIZE - 1)
1710   - % SLOTS_PID_HISTORY_SIZE];
1711   - derivative /= SLOTS_PID_INTERVAL;
1712   - deriv_p = ((s64)SLOTS_PID_G_d) * (s64)derivative;
1713   - DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1714   - sum += deriv_p;
1715   -
1716   - /* Calculate the proportional term */
1717   - prop_p = ((s64)SLOTS_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1718   - DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1719   - sum += prop_p;
1720   -
1721   - /* Scale sum */
1722   - sum >>= 36;
1723   -
1724   - DBG(" sum: %d\n", (int)sum);
1725   - state->pwm = (s32)sum;
1726   -
1727   - state->pwm = max(state->pwm, SLOTS_PID_OUTPUT_MIN);
1728   - state->pwm = min(state->pwm, SLOTS_PID_OUTPUT_MAX);
1729   -
1730   - DBG("** DRIVES PWM: %d\n", (int)state->pwm);
1731   - set_pwm_fan(SLOTS_FAN_PWM_INDEX, state->pwm);
1732   -}
1733   -
1734   -/*
1735   - * Initialize the state structure for the slots bay fan control loop
1736   - */
1737   -static int init_slots_state(struct slots_pid_state *state)
1738   -{
1739   - int err;
1740   -
1741   - state->ticks = 1;
1742   - state->first = 1;
1743   - state->pwm = 50;
1744   -
1745   - state->monitor = attach_i2c_chip(XSERVE_SLOTS_LM75, "slots_temp");
1746   - if (state->monitor == NULL)
1747   - return -ENODEV;
1748   -
1749   - err = device_create_file(&of_dev->dev, &dev_attr_slots_temperature);
1750   - err |= device_create_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1751   - if (err)
1752   - printk(KERN_WARNING "Failed to create attribute file(s)"
1753   - " for slots bay fan\n");
1754   -
1755   - return 0;
1756   -}
1757   -
1758   -/*
1759   - * Dispose of the state data for the slots control loop
1760   - */
1761   -static void dispose_slots_state(struct slots_pid_state *state)
1762   -{
1763   - if (state->monitor == NULL)
1764   - return;
1765   -
1766   - device_remove_file(&of_dev->dev, &dev_attr_slots_temperature);
1767   - device_remove_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1768   -
1769   - state->monitor = NULL;
1770   -}
1771   -
1772   -
1773   -static int call_critical_overtemp(void)
1774   -{
1775   - char *argv[] = { critical_overtemp_path, NULL };
1776   - static char *envp[] = { "HOME=/",
1777   - "TERM=linux",
1778   - "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
1779   - NULL };
1780   -
1781   - return call_usermodehelper(critical_overtemp_path,
1782   - argv, envp, UMH_WAIT_EXEC);
1783   -}
1784   -
1785   -
1786   -/*
1787   - * Here's the kernel thread that calls the various control loops
1788   - */
1789   -static int main_control_loop(void *x)
1790   -{
1791   - DBG("main_control_loop started\n");
1792   -
1793   - mutex_lock(&driver_lock);
1794   -
1795   - if (start_fcu() < 0) {
1796   - printk(KERN_ERR "kfand: failed to start FCU\n");
1797   - mutex_unlock(&driver_lock);
1798   - goto out;
1799   - }
1800   -
1801   - /* Set the PCI fan once for now on non-RackMac */
1802   - if (!rackmac)
1803   - set_pwm_fan(SLOTS_FAN_PWM_INDEX, SLOTS_FAN_DEFAULT_PWM);
1804   -
1805   - /* Initialize ADCs */
1806   - initialize_adc(&processor_state[0]);
1807   - if (processor_state[1].monitor != NULL)
1808   - initialize_adc(&processor_state[1]);
1809   -
1810   - fcu_tickle_ticks = FCU_TICKLE_TICKS;
1811   -
1812   - mutex_unlock(&driver_lock);
1813   -
1814   - while (state == state_attached) {
1815   - unsigned long elapsed, start;
1816   -
1817   - start = jiffies;
1818   -
1819   - mutex_lock(&driver_lock);
1820   -
1821   - /* Tickle the FCU just in case */
1822   - if (--fcu_tickle_ticks < 0) {
1823   - fcu_tickle_ticks = FCU_TICKLE_TICKS;
1824   - tickle_fcu();
1825   - }
1826   -
1827   - /* First, we always calculate the new DIMMs state on an Xserve */
1828   - if (rackmac)
1829   - do_monitor_dimms(&dimms_state);
1830   -
1831   - /* Then, the CPUs */
1832   - if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1833   - do_monitor_cpu_combined();
1834   - else if (cpu_pid_type == CPU_PID_TYPE_RACKMAC) {
1835   - do_monitor_cpu_rack(&processor_state[0]);
1836   - if (processor_state[1].monitor != NULL)
1837   - do_monitor_cpu_rack(&processor_state[1]);
1838   - // better deal with UP
1839   - } else {
1840   - do_monitor_cpu_split(&processor_state[0]);
1841   - if (processor_state[1].monitor != NULL)
1842   - do_monitor_cpu_split(&processor_state[1]);
1843   - // better deal with UP
1844   - }
1845   - /* Then, the rest */
1846   - do_monitor_backside(&backside_state);
1847   - if (rackmac)
1848   - do_monitor_slots(&slots_state);
1849   - else
1850   - do_monitor_drives(&drives_state);
1851   - mutex_unlock(&driver_lock);
1852   -
1853   - if (critical_state == 1) {
1854   - printk(KERN_WARNING "Temperature control detected a critical condition\n");
1855   - printk(KERN_WARNING "Attempting to shut down...\n");
1856   - if (call_critical_overtemp()) {
1857   - printk(KERN_WARNING "Can't call %s, power off now!\n",
1858   - critical_overtemp_path);
1859   - machine_power_off();
1860   - }
1861   - }
1862   - if (critical_state > 0)
1863   - critical_state++;
1864   - if (critical_state > MAX_CRITICAL_STATE) {
1865   - printk(KERN_WARNING "Shutdown timed out, power off now !\n");
1866   - machine_power_off();
1867   - }
1868   -
1869   - // FIXME: Deal with signals
1870   - elapsed = jiffies - start;
1871   - if (elapsed < HZ)
1872   - schedule_timeout_interruptible(HZ - elapsed);
1873   - }
1874   -
1875   - out:
1876   - DBG("main_control_loop ended\n");
1877   -
1878   - ctrl_task = 0;
1879   - complete_and_exit(&ctrl_complete, 0);
1880   -}
1881   -
1882   -/*
1883   - * Dispose the control loops when tearing down
1884   - */
1885   -static void dispose_control_loops(void)
1886   -{
1887   - dispose_processor_state(&processor_state[0]);
1888   - dispose_processor_state(&processor_state[1]);
1889   - dispose_backside_state(&backside_state);
1890   - dispose_drives_state(&drives_state);
1891   - dispose_slots_state(&slots_state);
1892   - dispose_dimms_state(&dimms_state);
1893   -}
1894   -
1895   -/*
1896   - * Create the control loops. U3-0 i2c bus is up, so we can now
1897   - * get to the various sensors
1898   - */
1899   -static int create_control_loops(void)
1900   -{
1901   - struct device_node *np;
1902   -
1903   - /* Count CPUs from the device-tree, we don't care how many are
1904   - * actually used by Linux
1905   - */
1906   - cpu_count = 0;
1907   - for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));)
1908   - cpu_count++;
1909   -
1910   - DBG("counted %d CPUs in the device-tree\n", cpu_count);
1911   -
1912   - /* Decide the type of PID algorithm to use based on the presence of
1913   - * the pumps, though that may not be the best way, that is good enough
1914   - * for now
1915   - */
1916   - if (rackmac)
1917   - cpu_pid_type = CPU_PID_TYPE_RACKMAC;
1918   - else if (of_machine_is_compatible("PowerMac7,3")
1919   - && (cpu_count > 1)
1920   - && fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID
1921   - && fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) {
1922   - printk(KERN_INFO "Liquid cooling pumps detected, using new algorithm !\n");
1923   - cpu_pid_type = CPU_PID_TYPE_COMBINED;
1924   - } else
1925   - cpu_pid_type = CPU_PID_TYPE_SPLIT;
1926   -
1927   - /* Create control loops for everything. If any fail, everything
1928   - * fails
1929   - */
1930   - if (init_processor_state(&processor_state[0], 0))
1931   - goto fail;
1932   - if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1933   - fetch_cpu_pumps_minmax();
1934   -
1935   - if (cpu_count > 1 && init_processor_state(&processor_state[1], 1))
1936   - goto fail;
1937   - if (init_backside_state(&backside_state))
1938   - goto fail;
1939   - if (rackmac && init_dimms_state(&dimms_state))
1940   - goto fail;
1941   - if (rackmac && init_slots_state(&slots_state))
1942   - goto fail;
1943   - if (!rackmac && init_drives_state(&drives_state))
1944   - goto fail;
1945   -
1946   - DBG("all control loops up !\n");
1947   -
1948   - return 0;
1949   -
1950   - fail:
1951   - DBG("failure creating control loops, disposing\n");
1952   -
1953   - dispose_control_loops();
1954   -
1955   - return -ENODEV;
1956   -}
1957   -
1958   -/*
1959   - * Start the control loops after everything is up, that is create
1960   - * the thread that will make them run
1961   - */
1962   -static void start_control_loops(void)
1963   -{
1964   - init_completion(&ctrl_complete);
1965   -
1966   - ctrl_task = kthread_run(main_control_loop, NULL, "kfand");
1967   -}
1968   -
1969   -/*
1970   - * Stop the control loops when tearing down
1971   - */
1972   -static void stop_control_loops(void)
1973   -{
1974   - if (ctrl_task)
1975   - wait_for_completion(&ctrl_complete);
1976   -}
1977   -
1978   -/*
1979   - * Attach to the i2c FCU after detecting U3-1 bus
1980   - */
1981   -static int attach_fcu(void)
1982   -{
1983   - fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu");
1984   - if (fcu == NULL)
1985   - return -ENODEV;
1986   -
1987   - DBG("FCU attached\n");
1988   -
1989   - return 0;
1990   -}
1991   -
1992   -/*
1993   - * Detach from the i2c FCU when tearing down
1994   - */
1995   -static void detach_fcu(void)
1996   -{
1997   - fcu = NULL;
1998   -}
1999   -
2000   -/*
2001   - * Attach to the i2c controller. We probe the various chips based
2002   - * on the device-tree nodes and build everything for the driver to
2003   - * run, we then kick the driver monitoring thread
2004   - */
2005   -static int therm_pm72_attach(struct i2c_adapter *adapter)
2006   -{
2007   - mutex_lock(&driver_lock);
2008   -
2009   - /* Check state */
2010   - if (state == state_detached)
2011   - state = state_attaching;
2012   - if (state != state_attaching) {
2013   - mutex_unlock(&driver_lock);
2014   - return 0;
2015   - }
2016   -
2017   - /* Check if we are looking for one of these */
2018   - if (u3_0 == NULL && !strcmp(adapter->name, "u3 0")) {
2019   - u3_0 = adapter;
2020   - DBG("found U3-0\n");
2021   - if (k2 || !rackmac)
2022   - if (create_control_loops())
2023   - u3_0 = NULL;
2024   - } else if (u3_1 == NULL && !strcmp(adapter->name, "u3 1")) {
2025   - u3_1 = adapter;
2026   - DBG("found U3-1, attaching FCU\n");
2027   - if (attach_fcu())
2028   - u3_1 = NULL;
2029   - } else if (k2 == NULL && !strcmp(adapter->name, "mac-io 0")) {
2030   - k2 = adapter;
2031   - DBG("Found K2\n");
2032   - if (u3_0 && rackmac)
2033   - if (create_control_loops())
2034   - k2 = NULL;
2035   - }
2036   - /* We got all we need, start control loops */
2037   - if (u3_0 != NULL && u3_1 != NULL && (k2 || !rackmac)) {
2038   - DBG("everything up, starting control loops\n");
2039   - state = state_attached;
2040   - start_control_loops();
2041   - }
2042   - mutex_unlock(&driver_lock);
2043   -
2044   - return 0;
2045   -}
2046   -
2047   -static int therm_pm72_probe(struct i2c_client *client,
2048   - const struct i2c_device_id *id)
2049   -{
2050   - /* Always succeed, the real work was done in therm_pm72_attach() */
2051   - return 0;
2052   -}
2053   -
2054   -/*
2055   - * Called when any of the devices which participates into thermal management
2056   - * is going away.
2057   - */
2058   -static int therm_pm72_remove(struct i2c_client *client)
2059   -{
2060   - struct i2c_adapter *adapter = client->adapter;
2061   -
2062   - mutex_lock(&driver_lock);
2063   -
2064   - if (state != state_detached)
2065   - state = state_detaching;
2066   -
2067   - /* Stop control loops if any */
2068   - DBG("stopping control loops\n");
2069   - mutex_unlock(&driver_lock);
2070   - stop_control_loops();
2071   - mutex_lock(&driver_lock);
2072   -
2073   - if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) {
2074   - DBG("lost U3-0, disposing control loops\n");
2075   - dispose_control_loops();
2076   - u3_0 = NULL;
2077   - }
2078   -
2079   - if (u3_1 != NULL && !strcmp(adapter->name, "u3 1")) {
2080   - DBG("lost U3-1, detaching FCU\n");
2081   - detach_fcu();
2082   - u3_1 = NULL;
2083   - }
2084   - if (u3_0 == NULL && u3_1 == NULL)
2085   - state = state_detached;
2086   -
2087   - mutex_unlock(&driver_lock);
2088   -
2089   - return 0;
2090   -}
2091   -
2092   -/*
2093   - * i2c_driver structure to attach to the host i2c controller
2094   - */
2095   -
2096   -static const struct i2c_device_id therm_pm72_id[] = {
2097   - /*
2098   - * Fake device name, thermal management is done by several
2099   - * chips but we don't need to differentiate between them at
2100   - * this point.
2101   - */
2102   - { "therm_pm72", 0 },
2103   - { }
2104   -};
2105   -
2106   -static struct i2c_driver therm_pm72_driver = {
2107   - .driver = {
2108   - .name = "therm_pm72",
2109   - },
2110   - .attach_adapter = therm_pm72_attach,
2111   - .probe = therm_pm72_probe,
2112   - .remove = therm_pm72_remove,
2113   - .id_table = therm_pm72_id,
2114   -};
2115   -
2116   -static int fan_check_loc_match(const char *loc, int fan)
2117   -{
2118   - char tmp[64];
2119   - char *c, *e;
2120   -
2121   - strlcpy(tmp, fcu_fans[fan].loc, 64);
2122   -
2123   - c = tmp;
2124   - for (;;) {
2125   - e = strchr(c, ',');
2126   - if (e)
2127   - *e = 0;
2128   - if (strcmp(loc, c) == 0)
2129   - return 1;
2130   - if (e == NULL)
2131   - break;
2132   - c = e + 1;
2133   - }
2134   - return 0;
2135   -}
2136   -
2137   -static void fcu_lookup_fans(struct device_node *fcu_node)
2138   -{
2139   - struct device_node *np = NULL;
2140   - int i;
2141   -
2142   - /* The table is filled by default with values that are suitable
2143   - * for the old machines without device-tree informations. We scan
2144   - * the device-tree and override those values with whatever is
2145   - * there
2146   - */
2147   -
2148   - DBG("Looking up FCU controls in device-tree...\n");
2149   -
2150   - while ((np = of_get_next_child(fcu_node, np)) != NULL) {
2151   - int type = -1;
2152   - const char *loc;
2153   - const u32 *reg;
2154   -
2155   - DBG(" control: %s, type: %s\n", np->name, np->type);
2156   -
2157   - /* Detect control type */
2158   - if (!strcmp(np->type, "fan-rpm-control") ||
2159   - !strcmp(np->type, "fan-rpm"))
2160   - type = FCU_FAN_RPM;
2161   - if (!strcmp(np->type, "fan-pwm-control") ||
2162   - !strcmp(np->type, "fan-pwm"))
2163   - type = FCU_FAN_PWM;
2164   - /* Only care about fans for now */
2165   - if (type == -1)
2166   - continue;
2167   -
2168   - /* Lookup for a matching location */
2169   - loc = of_get_property(np, "location", NULL);
2170   - reg = of_get_property(np, "reg", NULL);
2171   - if (loc == NULL || reg == NULL)
2172   - continue;
2173   - DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg);
2174   -
2175   - for (i = 0; i < FCU_FAN_COUNT; i++) {
2176   - int fan_id;
2177   -
2178   - if (!fan_check_loc_match(loc, i))
2179   - continue;
2180   - DBG(" location match, index: %d\n", i);
2181   - fcu_fans[i].id = FCU_FAN_ABSENT_ID;
2182   - if (type != fcu_fans[i].type) {
2183   - printk(KERN_WARNING "therm_pm72: Fan type mismatch "
2184   - "in device-tree for %s\n", np->full_name);
2185   - break;
2186   - }
2187   - if (type == FCU_FAN_RPM)
2188   - fan_id = ((*reg) - 0x10) / 2;
2189   - else
2190   - fan_id = ((*reg) - 0x30) / 2;
2191   - if (fan_id > 7) {
2192   - printk(KERN_WARNING "therm_pm72: Can't parse "
2193   - "fan ID in device-tree for %s\n", np->full_name);
2194   - break;
2195   - }
2196   - DBG(" fan id -> %d, type -> %d\n", fan_id, type);
2197   - fcu_fans[i].id = fan_id;
2198   - }
2199   - }
2200   -
2201   - /* Now dump the array */
2202   - printk(KERN_INFO "Detected fan controls:\n");
2203   - for (i = 0; i < FCU_FAN_COUNT; i++) {
2204   - if (fcu_fans[i].id == FCU_FAN_ABSENT_ID)
2205   - continue;
2206   - printk(KERN_INFO " %d: %s fan, id %d, location: %s\n", i,
2207   - fcu_fans[i].type == FCU_FAN_RPM ? "RPM" : "PWM",
2208   - fcu_fans[i].id, fcu_fans[i].loc);
2209   - }
2210   -}
2211   -
2212   -static int fcu_of_probe(struct platform_device* dev)
2213   -{
2214   - state = state_detached;
2215   - of_dev = dev;
2216   -
2217   - dev_info(&dev->dev, "PowerMac G5 Thermal control driver %s\n", VERSION);
2218   -
2219   - /* Lookup the fans in the device tree */
2220   - fcu_lookup_fans(dev->dev.of_node);
2221   -
2222   - /* Add the driver */
2223   - return i2c_add_driver(&therm_pm72_driver);
2224   -}
2225   -
2226   -static int fcu_of_remove(struct platform_device* dev)
2227   -{
2228   - i2c_del_driver(&therm_pm72_driver);
2229   -
2230   - return 0;
2231   -}
2232   -
2233   -static const struct of_device_id fcu_match[] =
2234   -{
2235   - {
2236   - .type = "fcu",
2237   - },
2238   - {},
2239   -};
2240   -MODULE_DEVICE_TABLE(of, fcu_match);
2241   -
2242   -static struct platform_driver fcu_of_platform_driver =
2243   -{
2244   - .driver = {
2245   - .name = "temperature",
2246   - .of_match_table = fcu_match,
2247   - },
2248   - .probe = fcu_of_probe,
2249   - .remove = fcu_of_remove
2250   -};
2251   -
2252   -/*
2253   - * Check machine type, attach to i2c controller
2254   - */
2255   -static int __init therm_pm72_init(void)
2256   -{
2257   - rackmac = of_machine_is_compatible("RackMac3,1");
2258   -
2259   - if (!of_machine_is_compatible("PowerMac7,2") &&
2260   - !of_machine_is_compatible("PowerMac7,3") &&
2261   - !rackmac)
2262   - return -ENODEV;
2263   -
2264   - return platform_driver_register(&fcu_of_platform_driver);
2265   -}
2266   -
2267   -static void __exit therm_pm72_exit(void)
2268   -{
2269   - platform_driver_unregister(&fcu_of_platform_driver);
2270   -}
2271   -
2272   -module_init(therm_pm72_init);
2273   -module_exit(therm_pm72_exit);
2274   -
2275   -MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
2276   -MODULE_DESCRIPTION("Driver for Apple's PowerMac G5 thermal control");
2277   -MODULE_LICENSE("GPL");
drivers/macintosh/therm_pm72.h
1   -#ifndef __THERM_PMAC_7_2_H__
2   -#define __THERM_PMAC_7_2_H__
3   -
4   -typedef unsigned short fu16;
5   -typedef int fs32;
6   -typedef short fs16;
7   -
8   -struct mpu_data
9   -{
10   - u8 signature; /* 0x00 - EEPROM sig. */
11   - u8 bytes_used; /* 0x01 - Bytes used in eeprom (160 ?) */
12   - u8 size; /* 0x02 - EEPROM size (256 ?) */
13   - u8 version; /* 0x03 - EEPROM version */
14   - u32 data_revision; /* 0x04 - Dataset revision */
15   - u8 processor_bin_code[3]; /* 0x08 - Processor BIN code */
16   - u8 bin_code_expansion; /* 0x0b - ??? (padding ?) */
17   - u8 processor_num; /* 0x0c - Number of CPUs on this MPU */
18   - u8 input_mul_bus_div; /* 0x0d - Clock input multiplier/bus divider */
19   - u8 reserved1[2]; /* 0x0e - */
20   - u32 input_clk_freq_high; /* 0x10 - Input clock frequency high */
21   - u8 cpu_nb_target_cycles; /* 0x14 - ??? */
22   - u8 cpu_statlat; /* 0x15 - ??? */
23   - u8 cpu_snooplat; /* 0x16 - ??? */
24   - u8 cpu_snoopacc; /* 0x17 - ??? */
25   - u8 nb_paamwin; /* 0x18 - ??? */
26   - u8 nb_statlat; /* 0x19 - ??? */
27   - u8 nb_snooplat; /* 0x1a - ??? */
28   - u8 nb_snoopwin; /* 0x1b - ??? */
29   - u8 api_bus_mode; /* 0x1c - ??? */
30   - u8 reserved2[3]; /* 0x1d - */
31   - u32 input_clk_freq_low; /* 0x20 - Input clock frequency low */
32   - u8 processor_card_slot; /* 0x24 - Processor card slot number */
33   - u8 reserved3[2]; /* 0x25 - */
34   - u8 padjmax; /* 0x27 - Max power adjustment (Not in OF!) */
35   - u8 ttarget; /* 0x28 - Target temperature */
36   - u8 tmax; /* 0x29 - Max temperature */
37   - u8 pmaxh; /* 0x2a - Max power */
38   - u8 tguardband; /* 0x2b - Guardband temp ??? Hist. len in OSX */
39   - fs32 pid_gp; /* 0x2c - PID proportional gain */
40   - fs32 pid_gr; /* 0x30 - PID reset gain */
41   - fs32 pid_gd; /* 0x34 - PID derivative gain */
42   - fu16 voph; /* 0x38 - Vop High */
43   - fu16 vopl; /* 0x3a - Vop Low */
44   - fs16 nactual_die; /* 0x3c - nActual Die */
45   - fs16 nactual_heatsink; /* 0x3e - nActual Heatsink */
46   - fs16 nactual_system; /* 0x40 - nActual System */
47   - u16 calibration_flags; /* 0x42 - Calibration flags */
48   - fu16 mdiode; /* 0x44 - Diode M value (scaling factor) */
49   - fs16 bdiode; /* 0x46 - Diode B value (offset) */
50   - fs32 theta_heat_sink; /* 0x48 - Theta heat sink */
51   - u16 rminn_intake_fan; /* 0x4c - Intake fan min RPM */
52   - u16 rmaxn_intake_fan; /* 0x4e - Intake fan max RPM */
53   - u16 rminn_exhaust_fan; /* 0x50 - Exhaust fan min RPM */
54   - u16 rmaxn_exhaust_fan; /* 0x52 - Exhaust fan max RPM */
55   - u8 processor_part_num[8]; /* 0x54 - Processor part number XX pumps min/max */
56   - u32 processor_lot_num; /* 0x5c - Processor lot number */
57   - u8 orig_card_sernum[0x10]; /* 0x60 - Card original serial number */
58   - u8 curr_card_sernum[0x10]; /* 0x70 - Card current serial number */
59   - u8 mlb_sernum[0x18]; /* 0x80 - MLB serial number */
60   - u32 checksum1; /* 0x98 - */
61   - u32 checksum2; /* 0x9c - */
62   -}; /* Total size = 0xa0 */
63   -
64   -/* Display a 16.16 fixed point value */
65   -#define FIX32TOPRINT(f) ((f) >> 16),((((f) & 0xffff) * 1000) >> 16)
66   -
67   -/*
68   - * Maximum number of seconds to be in critical state (after a
69   - * normal shutdown attempt). If the machine isn't down after
70   - * this counter elapses, we force an immediate machine power
71   - * off.
72   - */
73   -#define MAX_CRITICAL_STATE 30
74   -static char * critical_overtemp_path = "/sbin/critical_overtemp";
75   -
76   -/*
77   - * This option is "weird" :) Basically, if you define this to 1
78   - * the control loop for the RPMs fans (not PWMs) will apply the
79   - * correction factor obtained from the PID to the _actual_ RPM
80   - * speed read from the FCU.
81   - * If you define the below constant to 0, then it will be
82   - * applied to the setpoint RPM speed, that is basically the
83   - * speed we proviously "asked" for.
84   - *
85   - * I'm not sure which of these Apple's algorithm is supposed
86   - * to use
87   - */
88   -#define RPM_PID_USE_ACTUAL_SPEED 0
89   -
90   -/*
91   - * i2c IDs. Currently, we hard code those and assume that
92   - * the FCU is on U3 bus 1 while all sensors are on U3 bus
93   - * 0. This appear to be safe enough for this first version
94   - * of the driver, though I would accept any clean patch
95   - * doing a better use of the device-tree without turning the
96   - * while i2c registration mechanism into a racy mess
97   - *
98   - * Note: Xserve changed this. We have some bits on the K2 bus,
99   - * which I arbitrarily set to 0x200. Ultimately, we really want
100   - * too lookup these in the device-tree though
101   - */
102   -#define FAN_CTRLER_ID 0x15e
103   -#define SUPPLY_MONITOR_ID 0x58
104   -#define SUPPLY_MONITORB_ID 0x5a
105   -#define DRIVES_DALLAS_ID 0x94
106   -#define BACKSIDE_MAX_ID 0x98
107   -#define XSERVE_DIMMS_LM87 0x25a
108   -#define XSERVE_SLOTS_LM75 0x290
109   -
110   -/*
111   - * Some MAX6690, DS1775, LM87 register definitions
112   - */
113   -#define MAX6690_INT_TEMP 0
114   -#define MAX6690_EXT_TEMP 1
115   -#define DS1775_TEMP 0
116   -#define LM87_INT_TEMP 0x27
117   -
118   -/*
119   - * Scaling factors for the AD7417 ADC converters (except
120   - * for the CPU diode which is obtained from the EEPROM).
121   - * Those values are obtained from the property list of
122   - * the darwin driver
123   - */
124   -#define ADC_12V_CURRENT_SCALE 0x0320 /* _AD2 */
125   -#define ADC_CPU_VOLTAGE_SCALE 0x00a0 /* _AD3 */
126   -#define ADC_CPU_CURRENT_SCALE 0x1f40 /* _AD4 */
127   -
128   -/*
129   - * PID factors for the U3/Backside fan control loop. We have 2 sets
130   - * of values here, one set for U3 and one set for U3H
131   - */
132   -#define BACKSIDE_FAN_PWM_DEFAULT_ID 1
133   -#define BACKSIDE_FAN_PWM_INDEX 0
134   -#define BACKSIDE_PID_U3_G_d 0x02800000
135   -#define BACKSIDE_PID_U3H_G_d 0x01400000
136   -#define BACKSIDE_PID_RACK_G_d 0x00500000
137   -#define BACKSIDE_PID_G_p 0x00500000
138   -#define BACKSIDE_PID_RACK_G_p 0x0004cccc
139   -#define BACKSIDE_PID_G_r 0x00000000
140   -#define BACKSIDE_PID_U3_INPUT_TARGET 0x00410000
141   -#define BACKSIDE_PID_U3H_INPUT_TARGET 0x004b0000
142   -#define BACKSIDE_PID_RACK_INPUT_TARGET 0x00460000
143   -#define BACKSIDE_PID_INTERVAL 5
144   -#define BACKSIDE_PID_RACK_INTERVAL 1
145   -#define BACKSIDE_PID_OUTPUT_MAX 100
146   -#define BACKSIDE_PID_U3_OUTPUT_MIN 20
147   -#define BACKSIDE_PID_U3H_OUTPUT_MIN 20
148   -#define BACKSIDE_PID_HISTORY_SIZE 2
149   -
150   -struct basckside_pid_params
151   -{
152   - s32 G_d;
153   - s32 G_p;
154   - s32 G_r;
155   - s32 input_target;
156   - s32 output_min;
157   - s32 output_max;
158   - s32 interval;
159   - int additive;
160   -};
161   -
162   -struct backside_pid_state
163   -{
164   - int ticks;
165   - struct i2c_client * monitor;
166   - s32 sample_history[BACKSIDE_PID_HISTORY_SIZE];
167   - s32 error_history[BACKSIDE_PID_HISTORY_SIZE];
168   - int cur_sample;
169   - s32 last_temp;
170   - int pwm;
171   - int first;
172   -};
173   -
174   -/*
175   - * PID factors for the Drive Bay fan control loop
176   - */
177   -#define DRIVES_FAN_RPM_DEFAULT_ID 2
178   -#define DRIVES_FAN_RPM_INDEX 1
179   -#define DRIVES_PID_G_d 0x01e00000
180   -#define DRIVES_PID_G_p 0x00500000
181   -#define DRIVES_PID_G_r 0x00000000
182   -#define DRIVES_PID_INPUT_TARGET 0x00280000
183   -#define DRIVES_PID_INTERVAL 5
184   -#define DRIVES_PID_OUTPUT_MAX 4000
185   -#define DRIVES_PID_OUTPUT_MIN 300
186   -#define DRIVES_PID_HISTORY_SIZE 2
187   -
188   -struct drives_pid_state
189   -{
190   - int ticks;
191   - struct i2c_client * monitor;
192   - s32 sample_history[BACKSIDE_PID_HISTORY_SIZE];
193   - s32 error_history[BACKSIDE_PID_HISTORY_SIZE];
194   - int cur_sample;
195   - s32 last_temp;
196   - int rpm;
197   - int first;
198   -};
199   -
200   -#define SLOTS_FAN_PWM_DEFAULT_ID 2
201   -#define SLOTS_FAN_PWM_INDEX 2
202   -#define SLOTS_FAN_DEFAULT_PWM 40 /* Do better here ! */
203   -
204   -
205   -/*
206   - * PID factors for the Xserve DIMM control loop
207   - */
208   -#define DIMM_PID_G_d 0
209   -#define DIMM_PID_G_p 0
210   -#define DIMM_PID_G_r 0x06553600
211   -#define DIMM_PID_INPUT_TARGET 3276800
212   -#define DIMM_PID_INTERVAL 1
213   -#define DIMM_PID_OUTPUT_MAX 14000
214   -#define DIMM_PID_OUTPUT_MIN 4000
215   -#define DIMM_PID_HISTORY_SIZE 20
216   -
217   -struct dimm_pid_state
218   -{
219   - int ticks;
220   - struct i2c_client * monitor;
221   - s32 sample_history[DIMM_PID_HISTORY_SIZE];
222   - s32 error_history[DIMM_PID_HISTORY_SIZE];
223   - int cur_sample;
224   - s32 last_temp;
225   - int first;
226   - int output;
227   -};
228   -
229   -
230   -/*
231   - * PID factors for the Xserve Slots control loop
232   - */
233   -#define SLOTS_PID_G_d 0
234   -#define SLOTS_PID_G_p 0
235   -#define SLOTS_PID_G_r 0x00100000
236   -#define SLOTS_PID_INPUT_TARGET 3200000
237   -#define SLOTS_PID_INTERVAL 1
238   -#define SLOTS_PID_OUTPUT_MAX 100
239   -#define SLOTS_PID_OUTPUT_MIN 20
240   -#define SLOTS_PID_HISTORY_SIZE 20
241   -
242   -struct slots_pid_state
243   -{
244   - int ticks;
245   - struct i2c_client * monitor;
246   - s32 sample_history[SLOTS_PID_HISTORY_SIZE];
247   - s32 error_history[SLOTS_PID_HISTORY_SIZE];
248   - int cur_sample;
249   - s32 last_temp;
250   - int first;
251   - int pwm;
252   -};
253   -
254   -
255   -
256   -/* Desktops */
257   -
258   -#define CPUA_INTAKE_FAN_RPM_DEFAULT_ID 3
259   -#define CPUA_EXHAUST_FAN_RPM_DEFAULT_ID 4
260   -#define CPUB_INTAKE_FAN_RPM_DEFAULT_ID 5
261   -#define CPUB_EXHAUST_FAN_RPM_DEFAULT_ID 6
262   -
263   -#define CPUA_INTAKE_FAN_RPM_INDEX 3
264   -#define CPUA_EXHAUST_FAN_RPM_INDEX 4
265   -#define CPUB_INTAKE_FAN_RPM_INDEX 5
266   -#define CPUB_EXHAUST_FAN_RPM_INDEX 6
267   -
268   -#define CPU_INTAKE_SCALE 0x0000f852
269   -#define CPU_TEMP_HISTORY_SIZE 2
270   -#define CPU_POWER_HISTORY_SIZE 10
271   -#define CPU_PID_INTERVAL 1
272   -#define CPU_MAX_OVERTEMP 90
273   -
274   -#define CPUA_PUMP_RPM_INDEX 7
275   -#define CPUB_PUMP_RPM_INDEX 8
276   -#define CPU_PUMP_OUTPUT_MAX 3200
277   -#define CPU_PUMP_OUTPUT_MIN 1250
278   -
279   -/* Xserve */
280   -#define CPU_A1_FAN_RPM_INDEX 9
281   -#define CPU_A2_FAN_RPM_INDEX 10
282   -#define CPU_A3_FAN_RPM_INDEX 11
283   -#define CPU_B1_FAN_RPM_INDEX 12
284   -#define CPU_B2_FAN_RPM_INDEX 13
285   -#define CPU_B3_FAN_RPM_INDEX 14
286   -
287   -
288   -struct cpu_pid_state
289   -{
290   - int index;
291   - struct i2c_client * monitor;
292   - struct mpu_data mpu;
293   - int overtemp;
294   - s32 temp_history[CPU_TEMP_HISTORY_SIZE];
295   - int cur_temp;
296   - s32 power_history[CPU_POWER_HISTORY_SIZE];
297   - s32 error_history[CPU_POWER_HISTORY_SIZE];
298   - int cur_power;
299   - int count_power;
300   - int rpm;
301   - int intake_rpm;
302   - s32 voltage;
303   - s32 current_a;
304   - s32 last_temp;
305   - s32 last_power;
306   - int first;
307   - u8 adc_config;
308   - s32 pump_min;
309   - s32 pump_max;
310   -};
311   -
312   -/* Tickle FCU every 10 seconds */
313   -#define FCU_TICKLE_TICKS 10
314   -
315   -/*
316   - * Driver state
317   - */
318   -enum {
319   - state_detached,
320   - state_attaching,
321   - state_attached,
322   - state_detaching,
323   -};
324   -
325   -
326   -#endif /* __THERM_PMAC_7_2_H__ */