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