]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/char/tpm/tpm_i2c_nuvoton.c
tpm: msleep() delays - replace with usleep_range() in i2c nuvoton driver
[mirror_ubuntu-zesty-kernel.git] / drivers / char / tpm / tpm_i2c_nuvoton.c
CommitLineData
82cc1a49
JG
1 /******************************************************************************
2 * Nuvoton TPM I2C Device Driver Interface for WPCT301/NPCT501/NPCT6XX,
4c336e4b
JG
3 * based on the TCG TPM Interface Spec version 1.2.
4 * Specifications at www.trustedcomputinggroup.org
5 *
6 * Copyright (C) 2011, Nuvoton Technology Corporation.
7 * Dan Morav <dan.morav@nuvoton.com>
8 * Copyright (C) 2013, Obsidian Research Corp.
9 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/>.
23 *
24 * Nuvoton contact information: APC.Support@nuvoton.com
25 *****************************************************************************/
26
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/slab.h>
31#include <linux/interrupt.h>
32#include <linux/wait.h>
33#include <linux/i2c.h>
82cc1a49 34#include <linux/of_device.h>
4c336e4b
JG
35#include "tpm.h"
36
37/* I2C interface offsets */
38#define TPM_STS 0x00
39#define TPM_BURST_COUNT 0x01
40#define TPM_DATA_FIFO_W 0x20
41#define TPM_DATA_FIFO_R 0x40
42#define TPM_VID_DID_RID 0x60
43/* TPM command header size */
44#define TPM_HEADER_SIZE 10
45#define TPM_RETRY 5
46/*
47 * I2C bus device maximum buffer size w/o counting I2C address or command
48 * i.e. max size required for I2C write is 34 = addr, command, 32 bytes data
49 */
50#define TPM_I2C_MAX_BUF_SIZE 32
51#define TPM_I2C_RETRY_COUNT 32
9c687c46
MZ
52#define TPM_I2C_BUS_DELAY 1000 /* usec */
53#define TPM_I2C_RETRY_DELAY_SHORT 2 * 1000 /* usec */
54#define TPM_I2C_RETRY_DELAY_LONG 10 * 1000 /* usec */
4c336e4b 55
82cc1a49
JG
56#define OF_IS_TPM2 ((void *)1)
57#define I2C_IS_TPM2 1
4c336e4b
JG
58
59struct priv_data {
570a3609 60 int irq;
4c336e4b 61 unsigned int intrs;
6e599f6f 62 wait_queue_head_t read_queue;
4c336e4b
JG
63};
64
65static s32 i2c_nuvoton_read_buf(struct i2c_client *client, u8 offset, u8 size,
66 u8 *data)
67{
68 s32 status;
69
70 status = i2c_smbus_read_i2c_block_data(client, offset, size, data);
71 dev_dbg(&client->dev,
72 "%s(offset=%u size=%u data=%*ph) -> sts=%d\n", __func__,
73 offset, size, (int)size, data, status);
74 return status;
75}
76
77static s32 i2c_nuvoton_write_buf(struct i2c_client *client, u8 offset, u8 size,
78 u8 *data)
79{
80 s32 status;
81
82 status = i2c_smbus_write_i2c_block_data(client, offset, size, data);
83 dev_dbg(&client->dev,
84 "%s(offset=%u size=%u data=%*ph) -> sts=%d\n", __func__,
85 offset, size, (int)size, data, status);
86 return status;
87}
88
89#define TPM_STS_VALID 0x80
90#define TPM_STS_COMMAND_READY 0x40
91#define TPM_STS_GO 0x20
92#define TPM_STS_DATA_AVAIL 0x10
93#define TPM_STS_EXPECT 0x08
94#define TPM_STS_RESPONSE_RETRY 0x02
95#define TPM_STS_ERR_VAL 0x07 /* bit2...bit0 reads always 0 */
96
97#define TPM_I2C_SHORT_TIMEOUT 750 /* ms */
98#define TPM_I2C_LONG_TIMEOUT 2000 /* 2 sec */
99
100/* read TPM_STS register */
101static u8 i2c_nuvoton_read_status(struct tpm_chip *chip)
102{
8cfffc9d 103 struct i2c_client *client = to_i2c_client(chip->dev.parent);
4c336e4b
JG
104 s32 status;
105 u8 data;
106
107 status = i2c_nuvoton_read_buf(client, TPM_STS, 1, &data);
108 if (status <= 0) {
8cfffc9d 109 dev_err(&chip->dev, "%s() error return %d\n", __func__,
4c336e4b
JG
110 status);
111 data = TPM_STS_ERR_VAL;
112 }
113
114 return data;
115}
116
117/* write byte to TPM_STS register */
118static s32 i2c_nuvoton_write_status(struct i2c_client *client, u8 data)
119{
120 s32 status;
121 int i;
122
123 /* this causes the current command to be aborted */
124 for (i = 0, status = -1; i < TPM_I2C_RETRY_COUNT && status < 0; i++) {
125 status = i2c_nuvoton_write_buf(client, TPM_STS, 1, &data);
9c687c46 126 usleep_range(TPM_I2C_BUS_DELAY, TPM_I2C_BUS_DELAY + 300);
4c336e4b
JG
127 }
128 return status;
129}
130
131/* write commandReady to TPM_STS register */
132static void i2c_nuvoton_ready(struct tpm_chip *chip)
133{
8cfffc9d 134 struct i2c_client *client = to_i2c_client(chip->dev.parent);
4c336e4b
JG
135 s32 status;
136
137 /* this causes the current command to be aborted */
138 status = i2c_nuvoton_write_status(client, TPM_STS_COMMAND_READY);
139 if (status < 0)
8cfffc9d 140 dev_err(&chip->dev,
4c336e4b
JG
141 "%s() fail to write TPM_STS.commandReady\n", __func__);
142}
143
144/* read burstCount field from TPM_STS register
145 * return -1 on fail to read */
146static int i2c_nuvoton_get_burstcount(struct i2c_client *client,
147 struct tpm_chip *chip)
148{
af782f33 149 unsigned long stop = jiffies + chip->timeout_d;
4c336e4b
JG
150 s32 status;
151 int burst_count = -1;
152 u8 data;
153
154 /* wait for burstcount to be non-zero */
155 do {
156 /* in I2C burstCount is 1 byte */
157 status = i2c_nuvoton_read_buf(client, TPM_BURST_COUNT, 1,
158 &data);
159 if (status > 0 && data > 0) {
160 burst_count = min_t(u8, TPM_I2C_MAX_BUF_SIZE, data);
161 break;
162 }
9c687c46 163 usleep_range(TPM_I2C_BUS_DELAY, TPM_I2C_BUS_DELAY + 300);
4c336e4b
JG
164 } while (time_before(jiffies, stop));
165
166 return burst_count;
167}
168
169/*
82cc1a49 170 * WPCT301/NPCT501/NPCT6XX SINT# supports only dataAvail
4c336e4b
JG
171 * any call to this function which is not waiting for dataAvail will
172 * set queue to NULL to avoid waiting for interrupt
173 */
174static bool i2c_nuvoton_check_status(struct tpm_chip *chip, u8 mask, u8 value)
175{
176 u8 status = i2c_nuvoton_read_status(chip);
177 return (status != TPM_STS_ERR_VAL) && ((status & mask) == value);
178}
179
180static int i2c_nuvoton_wait_for_stat(struct tpm_chip *chip, u8 mask, u8 value,
181 u32 timeout, wait_queue_head_t *queue)
182{
570a3609 183 if ((chip->flags & TPM_CHIP_FLAG_IRQ) && queue) {
4c336e4b 184 s32 rc;
9e0d39d8 185 struct priv_data *priv = dev_get_drvdata(&chip->dev);
4c336e4b
JG
186 unsigned int cur_intrs = priv->intrs;
187
570a3609 188 enable_irq(priv->irq);
4c336e4b
JG
189 rc = wait_event_interruptible_timeout(*queue,
190 cur_intrs != priv->intrs,
191 timeout);
192 if (rc > 0)
193 return 0;
194 /* At this point we know that the SINT pin is asserted, so we
195 * do not need to do i2c_nuvoton_check_status */
196 } else {
197 unsigned long ten_msec, stop;
198 bool status_valid;
199
200 /* check current status */
201 status_valid = i2c_nuvoton_check_status(chip, mask, value);
202 if (status_valid)
203 return 0;
204
205 /* use polling to wait for the event */
9c687c46 206 ten_msec = jiffies + usecs_to_jiffies(TPM_I2C_RETRY_DELAY_LONG);
4c336e4b
JG
207 stop = jiffies + timeout;
208 do {
209 if (time_before(jiffies, ten_msec))
9c687c46
MZ
210 usleep_range(TPM_I2C_RETRY_DELAY_SHORT,
211 TPM_I2C_RETRY_DELAY_SHORT + 300);
4c336e4b 212 else
9c687c46
MZ
213 usleep_range(TPM_I2C_RETRY_DELAY_LONG,
214 TPM_I2C_RETRY_DELAY_LONG + 300);
4c336e4b
JG
215 status_valid = i2c_nuvoton_check_status(chip, mask,
216 value);
217 if (status_valid)
218 return 0;
219 } while (time_before(jiffies, stop));
220 }
8cfffc9d 221 dev_err(&chip->dev, "%s(%02x, %02x) -> timeout\n", __func__, mask,
4c336e4b
JG
222 value);
223 return -ETIMEDOUT;
224}
225
226/* wait for dataAvail field to be set in the TPM_STS register */
227static int i2c_nuvoton_wait_for_data_avail(struct tpm_chip *chip, u32 timeout,
228 wait_queue_head_t *queue)
229{
230 return i2c_nuvoton_wait_for_stat(chip,
231 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
232 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
233 timeout, queue);
234}
235
236/* Read @count bytes into @buf from TPM_RD_FIFO register */
237static int i2c_nuvoton_recv_data(struct i2c_client *client,
238 struct tpm_chip *chip, u8 *buf, size_t count)
239{
9e0d39d8 240 struct priv_data *priv = dev_get_drvdata(&chip->dev);
4c336e4b
JG
241 s32 rc;
242 int burst_count, bytes2read, size = 0;
243
244 while (size < count &&
245 i2c_nuvoton_wait_for_data_avail(chip,
af782f33 246 chip->timeout_c,
6e599f6f 247 &priv->read_queue) == 0) {
4c336e4b
JG
248 burst_count = i2c_nuvoton_get_burstcount(client, chip);
249 if (burst_count < 0) {
8cfffc9d 250 dev_err(&chip->dev,
4c336e4b
JG
251 "%s() fail to read burstCount=%d\n", __func__,
252 burst_count);
253 return -EIO;
254 }
255 bytes2read = min_t(size_t, burst_count, count - size);
256 rc = i2c_nuvoton_read_buf(client, TPM_DATA_FIFO_R,
257 bytes2read, &buf[size]);
258 if (rc < 0) {
8cfffc9d 259 dev_err(&chip->dev,
4c336e4b
JG
260 "%s() fail on i2c_nuvoton_read_buf()=%d\n",
261 __func__, rc);
262 return -EIO;
263 }
8cfffc9d 264 dev_dbg(&chip->dev, "%s(%d):", __func__, bytes2read);
4c336e4b
JG
265 size += bytes2read;
266 }
267
268 return size;
269}
270
271/* Read TPM command results */
272static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count)
273{
9e0d39d8 274 struct priv_data *priv = dev_get_drvdata(&chip->dev);
8cfffc9d 275 struct device *dev = chip->dev.parent;
4c336e4b
JG
276 struct i2c_client *client = to_i2c_client(dev);
277 s32 rc;
278 int expected, status, burst_count, retries, size = 0;
279
280 if (count < TPM_HEADER_SIZE) {
281 i2c_nuvoton_ready(chip); /* return to idle */
282 dev_err(dev, "%s() count < header size\n", __func__);
283 return -EIO;
284 }
285 for (retries = 0; retries < TPM_RETRY; retries++) {
286 if (retries > 0) {
287 /* if this is not the first trial, set responseRetry */
288 i2c_nuvoton_write_status(client,
289 TPM_STS_RESPONSE_RETRY);
290 }
291 /*
292 * read first available (> 10 bytes), including:
293 * tag, paramsize, and result
294 */
295 status = i2c_nuvoton_wait_for_data_avail(
af782f33 296 chip, chip->timeout_c, &priv->read_queue);
4c336e4b
JG
297 if (status != 0) {
298 dev_err(dev, "%s() timeout on dataAvail\n", __func__);
299 size = -ETIMEDOUT;
300 continue;
301 }
302 burst_count = i2c_nuvoton_get_burstcount(client, chip);
303 if (burst_count < 0) {
304 dev_err(dev, "%s() fail to get burstCount\n", __func__);
305 size = -EIO;
306 continue;
307 }
308 size = i2c_nuvoton_recv_data(client, chip, buf,
309 burst_count);
310 if (size < TPM_HEADER_SIZE) {
311 dev_err(dev, "%s() fail to read header\n", __func__);
312 size = -EIO;
313 continue;
314 }
315 /*
316 * convert number of expected bytes field from big endian 32 bit
317 * to machine native
318 */
319 expected = be32_to_cpu(*(__be32 *) (buf + 2));
320 if (expected > count) {
321 dev_err(dev, "%s() expected > count\n", __func__);
322 size = -EIO;
323 continue;
324 }
325 rc = i2c_nuvoton_recv_data(client, chip, &buf[size],
326 expected - size);
327 size += rc;
328 if (rc < 0 || size < expected) {
329 dev_err(dev, "%s() fail to read remainder of result\n",
330 __func__);
331 size = -EIO;
332 continue;
333 }
334 if (i2c_nuvoton_wait_for_stat(
335 chip, TPM_STS_VALID | TPM_STS_DATA_AVAIL,
af782f33 336 TPM_STS_VALID, chip->timeout_c,
4c336e4b
JG
337 NULL)) {
338 dev_err(dev, "%s() error left over data\n", __func__);
339 size = -ETIMEDOUT;
340 continue;
341 }
342 break;
343 }
344 i2c_nuvoton_ready(chip);
8cfffc9d 345 dev_dbg(&chip->dev, "%s() -> %d\n", __func__, size);
4c336e4b
JG
346 return size;
347}
348
349/*
350 * Send TPM command.
351 *
352 * If interrupts are used (signaled by an irq set in the vendor structure)
353 * tpm.c can skip polling for the data to be available as the interrupt is
354 * waited for here
355 */
356static int i2c_nuvoton_send(struct tpm_chip *chip, u8 *buf, size_t len)
357{
9e0d39d8 358 struct priv_data *priv = dev_get_drvdata(&chip->dev);
8cfffc9d 359 struct device *dev = chip->dev.parent;
4c336e4b
JG
360 struct i2c_client *client = to_i2c_client(dev);
361 u32 ordinal;
362 size_t count = 0;
363 int burst_count, bytes2write, retries, rc = -EIO;
364
365 for (retries = 0; retries < TPM_RETRY; retries++) {
366 i2c_nuvoton_ready(chip);
367 if (i2c_nuvoton_wait_for_stat(chip, TPM_STS_COMMAND_READY,
368 TPM_STS_COMMAND_READY,
af782f33 369 chip->timeout_b, NULL)) {
4c336e4b
JG
370 dev_err(dev, "%s() timeout on commandReady\n",
371 __func__);
372 rc = -EIO;
373 continue;
374 }
375 rc = 0;
376 while (count < len - 1) {
377 burst_count = i2c_nuvoton_get_burstcount(client,
378 chip);
379 if (burst_count < 0) {
380 dev_err(dev, "%s() fail get burstCount\n",
381 __func__);
382 rc = -EIO;
383 break;
384 }
385 bytes2write = min_t(size_t, burst_count,
386 len - 1 - count);
387 rc = i2c_nuvoton_write_buf(client, TPM_DATA_FIFO_W,
388 bytes2write, &buf[count]);
389 if (rc < 0) {
390 dev_err(dev, "%s() fail i2cWriteBuf\n",
391 __func__);
392 break;
393 }
394 dev_dbg(dev, "%s(%d):", __func__, bytes2write);
395 count += bytes2write;
396 rc = i2c_nuvoton_wait_for_stat(chip,
397 TPM_STS_VALID |
398 TPM_STS_EXPECT,
399 TPM_STS_VALID |
400 TPM_STS_EXPECT,
af782f33 401 chip->timeout_c,
4c336e4b
JG
402 NULL);
403 if (rc < 0) {
404 dev_err(dev, "%s() timeout on Expect\n",
405 __func__);
406 rc = -ETIMEDOUT;
407 break;
408 }
409 }
410 if (rc < 0)
411 continue;
412
413 /* write last byte */
414 rc = i2c_nuvoton_write_buf(client, TPM_DATA_FIFO_W, 1,
415 &buf[count]);
416 if (rc < 0) {
417 dev_err(dev, "%s() fail to write last byte\n",
418 __func__);
419 rc = -EIO;
420 continue;
421 }
422 dev_dbg(dev, "%s(last): %02x", __func__, buf[count]);
423 rc = i2c_nuvoton_wait_for_stat(chip,
424 TPM_STS_VALID | TPM_STS_EXPECT,
425 TPM_STS_VALID,
af782f33 426 chip->timeout_c, NULL);
4c336e4b
JG
427 if (rc) {
428 dev_err(dev, "%s() timeout on Expect to clear\n",
429 __func__);
430 rc = -ETIMEDOUT;
431 continue;
432 }
433 break;
434 }
435 if (rc < 0) {
436 /* retries == TPM_RETRY */
437 i2c_nuvoton_ready(chip);
438 return rc;
439 }
440 /* execute the TPM command */
441 rc = i2c_nuvoton_write_status(client, TPM_STS_GO);
442 if (rc < 0) {
443 dev_err(dev, "%s() fail to write Go\n", __func__);
444 i2c_nuvoton_ready(chip);
445 return rc;
446 }
447 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
448 rc = i2c_nuvoton_wait_for_data_avail(chip,
449 tpm_calc_ordinal_duration(chip,
450 ordinal),
6e599f6f 451 &priv->read_queue);
4c336e4b
JG
452 if (rc) {
453 dev_err(dev, "%s() timeout command duration\n", __func__);
454 i2c_nuvoton_ready(chip);
455 return rc;
456 }
457
458 dev_dbg(dev, "%s() -> %zd\n", __func__, len);
459 return len;
460}
461
462static bool i2c_nuvoton_req_canceled(struct tpm_chip *chip, u8 status)
463{
464 return (status == TPM_STS_COMMAND_READY);
465}
466
01ad1fa7 467static const struct tpm_class_ops tpm_i2c = {
cae8b441 468 .flags = TPM_OPS_AUTO_STARTUP,
4c336e4b
JG
469 .status = i2c_nuvoton_read_status,
470 .recv = i2c_nuvoton_recv,
471 .send = i2c_nuvoton_send,
472 .cancel = i2c_nuvoton_ready,
473 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
474 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
475 .req_canceled = i2c_nuvoton_req_canceled,
4c336e4b
JG
476};
477
478/* The only purpose for the handler is to signal to any waiting threads that
479 * the interrupt is currently being asserted. The driver does not do any
480 * processing triggered by interrupts, and the chip provides no way to mask at
481 * the source (plus that would be slow over I2C). Run the IRQ as a one-shot,
482 * this means it cannot be shared. */
483static irqreturn_t i2c_nuvoton_int_handler(int dummy, void *dev_id)
484{
485 struct tpm_chip *chip = dev_id;
9e0d39d8 486 struct priv_data *priv = dev_get_drvdata(&chip->dev);
4c336e4b
JG
487
488 priv->intrs++;
6e599f6f 489 wake_up(&priv->read_queue);
570a3609 490 disable_irq_nosync(priv->irq);
4c336e4b
JG
491 return IRQ_HANDLED;
492}
493
494static int get_vid(struct i2c_client *client, u32 *res)
495{
496 static const u8 vid_did_rid_value[] = { 0x50, 0x10, 0xfe };
497 u32 temp;
498 s32 rc;
499
500 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
501 return -ENODEV;
502 rc = i2c_nuvoton_read_buf(client, TPM_VID_DID_RID, 4, (u8 *)&temp);
503 if (rc < 0)
504 return rc;
505
506 /* check WPCT301 values - ignore RID */
507 if (memcmp(&temp, vid_did_rid_value, sizeof(vid_did_rid_value))) {
508 /*
509 * f/w rev 2.81 has an issue where the VID_DID_RID is not
510 * reporting the right value. so give it another chance at
511 * offset 0x20 (FIFO_W).
512 */
513 rc = i2c_nuvoton_read_buf(client, TPM_DATA_FIFO_W, 4,
514 (u8 *) (&temp));
515 if (rc < 0)
516 return rc;
517
518 /* check WPCT301 values - ignore RID */
519 if (memcmp(&temp, vid_did_rid_value,
520 sizeof(vid_did_rid_value)))
521 return -ENODEV;
522 }
523
524 *res = temp;
525 return 0;
526}
527
528static int i2c_nuvoton_probe(struct i2c_client *client,
529 const struct i2c_device_id *id)
530{
531 int rc;
532 struct tpm_chip *chip;
533 struct device *dev = &client->dev;
570a3609 534 struct priv_data *priv;
4c336e4b
JG
535 u32 vid = 0;
536
537 rc = get_vid(client, &vid);
538 if (rc)
539 return rc;
540
541 dev_info(dev, "VID: %04X DID: %02X RID: %02X\n", (u16) vid,
542 (u8) (vid >> 16), (u8) (vid >> 24));
543
afb5abc2
JS
544 chip = tpmm_chip_alloc(dev, &tpm_i2c);
545 if (IS_ERR(chip))
546 return PTR_ERR(chip);
4c336e4b 547
570a3609
CR
548 priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
549 if (!priv)
afb5abc2 550 return -ENOMEM;
bb95cd34 551
82cc1a49
JG
552 if (dev->of_node) {
553 const struct of_device_id *of_id;
554
555 of_id = of_match_device(dev->driver->of_match_table, dev);
556 if (of_id && of_id->data == OF_IS_TPM2)
557 chip->flags |= TPM_CHIP_FLAG_TPM2;
558 } else
559 if (id->driver_data == I2C_IS_TPM2)
560 chip->flags |= TPM_CHIP_FLAG_TPM2;
561
6e599f6f 562 init_waitqueue_head(&priv->read_queue);
4c336e4b
JG
563
564 /* Default timeouts */
af782f33
CR
565 chip->timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
566 chip->timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
567 chip->timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
568 chip->timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
4c336e4b 569
9e0d39d8
CR
570 dev_set_drvdata(&chip->dev, priv);
571
4c336e4b
JG
572 /*
573 * I2C intfcaps (interrupt capabilitieis) in the chip are hard coded to:
574 * TPM_INTF_INT_LEVEL_LOW | TPM_INTF_DATA_AVAIL_INT
575 * The IRQ should be set in the i2c_board_info (which is done
576 * automatically in of_i2c_register_devices, for device tree users */
570a3609 577 priv->irq = client->irq;
fa7539b2 578 if (client->irq) {
570a3609
CR
579 dev_dbg(dev, "%s() priv->irq\n", __func__);
580 rc = devm_request_irq(dev, client->irq,
4c336e4b
JG
581 i2c_nuvoton_int_handler,
582 IRQF_TRIGGER_LOW,
3635e2ec 583 dev_name(&chip->dev),
4c336e4b
JG
584 chip);
585 if (rc) {
586 dev_err(dev, "%s() Unable to request irq: %d for use\n",
570a3609 587 __func__, priv->irq);
570a3609 588 priv->irq = 0;
4c336e4b 589 } else {
fa7539b2 590 chip->flags |= TPM_CHIP_FLAG_IRQ;
4c336e4b
JG
591 /* Clear any pending interrupt */
592 i2c_nuvoton_ready(chip);
593 /* - wait for TPM_STS==0xA0 (stsValid, commandReady) */
594 rc = i2c_nuvoton_wait_for_stat(chip,
595 TPM_STS_COMMAND_READY,
596 TPM_STS_COMMAND_READY,
af782f33 597 chip->timeout_b,
4c336e4b
JG
598 NULL);
599 if (rc == 0) {
600 /*
601 * TIS is in ready state
602 * write dummy byte to enter reception state
603 * TPM_DATA_FIFO_W <- rc (0)
604 */
605 rc = i2c_nuvoton_write_buf(client,
606 TPM_DATA_FIFO_W,
607 1, (u8 *) (&rc));
608 if (rc < 0)
afb5abc2 609 return rc;
4c336e4b
JG
610 /* TPM_STS <- 0x40 (commandReady) */
611 i2c_nuvoton_ready(chip);
612 } else {
613 /*
614 * timeout_b reached - command was
615 * aborted. TIS should now be in idle state -
616 * only TPM_STS_VALID should be set
617 */
618 if (i2c_nuvoton_read_status(chip) !=
afb5abc2
JS
619 TPM_STS_VALID)
620 return -EIO;
4c336e4b
JG
621 }
622 }
623 }
624
afb5abc2 625 return tpm_chip_register(chip);
4c336e4b
JG
626}
627
628static int i2c_nuvoton_remove(struct i2c_client *client)
629{
9e0d39d8
CR
630 struct tpm_chip *chip = i2c_get_clientdata(client);
631
afb5abc2 632 tpm_chip_unregister(chip);
4c336e4b
JG
633 return 0;
634}
635
4c336e4b 636static const struct i2c_device_id i2c_nuvoton_id[] = {
82cc1a49
JG
637 {"tpm_i2c_nuvoton"},
638 {"tpm2_i2c_nuvoton", .driver_data = I2C_IS_TPM2},
4c336e4b
JG
639 {}
640};
641MODULE_DEVICE_TABLE(i2c, i2c_nuvoton_id);
642
643#ifdef CONFIG_OF
644static const struct of_device_id i2c_nuvoton_of_match[] = {
645 {.compatible = "nuvoton,npct501"},
646 {.compatible = "winbond,wpct301"},
82cc1a49 647 {.compatible = "nuvoton,npct601", .data = OF_IS_TPM2},
4c336e4b
JG
648 {},
649};
650MODULE_DEVICE_TABLE(of, i2c_nuvoton_of_match);
651#endif
652
653static SIMPLE_DEV_PM_OPS(i2c_nuvoton_pm_ops, tpm_pm_suspend, tpm_pm_resume);
654
655static struct i2c_driver i2c_nuvoton_driver = {
656 .id_table = i2c_nuvoton_id,
657 .probe = i2c_nuvoton_probe,
658 .remove = i2c_nuvoton_remove,
659 .driver = {
82cc1a49 660 .name = "tpm_i2c_nuvoton",
4c336e4b
JG
661 .pm = &i2c_nuvoton_pm_ops,
662 .of_match_table = of_match_ptr(i2c_nuvoton_of_match),
663 },
664};
665
666module_i2c_driver(i2c_nuvoton_driver);
667
668MODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
669MODULE_DESCRIPTION("Nuvoton TPM I2C Driver");
670MODULE_LICENSE("GPL");