1 /*
2  * Copyright (C) 2012,2013 Infineon Technologies
3  *
4  * Authors:
5  * Peter Huewe <peter.huewe@infineon.com>
6  *
7  * Device driver for TCG/TCPA TPM (trusted platform module).
8  * Specifications at www.trustedcomputinggroup.org
9  *
10  * This device driver implements the TPM interface as defined in
11  * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
12  * Infineon I2C Protocol Stack Specification v0.20.
13  *
14  * It is based on the original tpm_tis device driver from Leendert van
15  * Dorn and Kyleen Hall.
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License as
19  * published by the Free Software Foundation, version 2 of the
20  * License.
21  *
22  *
23  */
24 #include <linux/i2c.h>
25 #include <linux/module.h>
26 #include <linux/wait.h>
27 #include "tpm.h"
28 
29 #define TPM_I2C_INFINEON_BUFSIZE 1260
30 
31 /* max. number of iterations after I2C NAK */
32 #define MAX_COUNT 3
33 
34 #define SLEEP_DURATION_LOW 55
35 #define SLEEP_DURATION_HI 65
36 
37 /* max. number of iterations after I2C NAK for 'long' commands
38  * we need this especially for sending TPM_READY, since the cleanup after the
39  * transtion to the ready state may take some time, but it is unpredictable
40  * how long it will take.
41  */
42 #define MAX_COUNT_LONG 50
43 
44 #define SLEEP_DURATION_LONG_LOW 200
45 #define SLEEP_DURATION_LONG_HI 220
46 
47 /* After sending TPM_READY to 'reset' the TPM we have to sleep even longer */
48 #define SLEEP_DURATION_RESET_LOW 2400
49 #define SLEEP_DURATION_RESET_HI 2600
50 
51 /* we want to use usleep_range instead of msleep for the 5ms TPM_TIMEOUT */
52 #define TPM_TIMEOUT_US_LOW (TPM_TIMEOUT * 1000)
53 #define TPM_TIMEOUT_US_HI  (TPM_TIMEOUT_US_LOW + 2000)
54 
55 /* expected value for DIDVID register */
56 #define TPM_TIS_I2C_DID_VID_9635 0xd1150b00L
57 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
58 
59 enum i2c_chip_type {
60 	SLB9635,
61 	SLB9645,
62 	UNKNOWN,
63 };
64 
65 struct tpm_inf_dev {
66 	struct i2c_client *client;
67 	int locality;
68 	/* In addition to the data itself, the buffer must fit the 7-bit I2C
69 	 * address and the direction bit.
70 	 */
71 	u8 buf[TPM_I2C_INFINEON_BUFSIZE + 1];
72 	struct tpm_chip *chip;
73 	enum i2c_chip_type chip_type;
74 	unsigned int adapterlimit;
75 };
76 
77 static struct tpm_inf_dev tpm_dev;
78 
79 /*
80  * iic_tpm_read() - read from TPM register
81  * @addr: register address to read from
82  * @buffer: provided by caller
83  * @len: number of bytes to read
84  *
85  * Read len bytes from TPM register and put them into
86  * buffer (little-endian format, i.e. first byte is put into buffer[0]).
87  *
88  * NOTE: TPM is big-endian for multi-byte values. Multi-byte
89  * values have to be swapped.
90  *
91  * NOTE: We can't unfortunately use the combined read/write functions
92  * provided by the i2c core as the TPM currently does not support the
93  * repeated start condition and due to it's special requirements.
94  * The i2c_smbus* functions do not work for this chip.
95  *
96  * Return -EIO on error, 0 on success.
97  */
98 static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
99 {
100 
101 	struct i2c_msg msg1 = {
102 		.addr = tpm_dev.client->addr,
103 		.len = 1,
104 		.buf = &addr
105 	};
106 	struct i2c_msg msg2 = {
107 		.addr = tpm_dev.client->addr,
108 		.flags = I2C_M_RD,
109 		.len = len,
110 		.buf = buffer
111 	};
112 	struct i2c_msg msgs[] = {msg1, msg2};
113 
114 	int rc = 0;
115 	int count;
116 	unsigned int msglen = len;
117 
118 	/* Lock the adapter for the duration of the whole sequence. */
119 	if (!tpm_dev.client->adapter->algo->master_xfer)
120 		return -EOPNOTSUPP;
121 	i2c_lock_bus(tpm_dev.client->adapter, I2C_LOCK_SEGMENT);
122 
123 	if (tpm_dev.chip_type == SLB9645) {
124 		/* use a combined read for newer chips
125 		 * unfortunately the smbus functions are not suitable due to
126 		 * the 32 byte limit of the smbus.
127 		 * retries should usually not be needed, but are kept just to
128 		 * be on the safe side.
129 		 */
130 		for (count = 0; count < MAX_COUNT; count++) {
131 			rc = __i2c_transfer(tpm_dev.client->adapter, msgs, 2);
132 			if (rc > 0)
133 				break;	/* break here to skip sleep */
134 			usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
135 		}
136 	} else {
137 		/* Expect to send one command message and one data message, but
138 		 * support looping over each or both if necessary.
139 		 */
140 		while (len > 0) {
141 			/* slb9635 protocol should work in all cases */
142 			for (count = 0; count < MAX_COUNT; count++) {
143 				rc = __i2c_transfer(tpm_dev.client->adapter,
144 						    &msg1, 1);
145 				if (rc > 0)
146 					break;	/* break here to skip sleep */
147 
148 				usleep_range(SLEEP_DURATION_LOW,
149 					     SLEEP_DURATION_HI);
150 			}
151 
152 			if (rc <= 0)
153 				goto out;
154 
155 			/* After the TPM has successfully received the register
156 			 * address it needs some time, thus we're sleeping here
157 			 * again, before retrieving the data
158 			 */
159 			for (count = 0; count < MAX_COUNT; count++) {
160 				if (tpm_dev.adapterlimit) {
161 					msglen = min_t(unsigned int,
162 						       tpm_dev.adapterlimit,
163 						       len);
164 					msg2.len = msglen;
165 				}
166 				usleep_range(SLEEP_DURATION_LOW,
167 					     SLEEP_DURATION_HI);
168 				rc = __i2c_transfer(tpm_dev.client->adapter,
169 						    &msg2, 1);
170 				if (rc > 0) {
171 					/* Since len is unsigned, make doubly
172 					 * sure we do not underflow it.
173 					 */
174 					if (msglen > len)
175 						len = 0;
176 					else
177 						len -= msglen;
178 					msg2.buf += msglen;
179 					break;
180 				}
181 				/* If the I2C adapter rejected the request (e.g
182 				 * when the quirk read_max_len < len) fall back
183 				 * to a sane minimum value and try again.
184 				 */
185 				if (rc == -EOPNOTSUPP)
186 					tpm_dev.adapterlimit =
187 							I2C_SMBUS_BLOCK_MAX;
188 			}
189 
190 			if (rc <= 0)
191 				goto out;
192 		}
193 	}
194 
195 out:
196 	i2c_unlock_bus(tpm_dev.client->adapter, I2C_LOCK_SEGMENT);
197 	/* take care of 'guard time' */
198 	usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
199 
200 	/* __i2c_transfer returns the number of successfully transferred
201 	 * messages.
202 	 * So rc should be greater than 0 here otherwise we have an error.
203 	 */
204 	if (rc <= 0)
205 		return -EIO;
206 
207 	return 0;
208 }
209 
210 static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
211 				 unsigned int sleep_low,
212 				 unsigned int sleep_hi, u8 max_count)
213 {
214 	int rc = -EIO;
215 	int count;
216 
217 	struct i2c_msg msg1 = {
218 		.addr = tpm_dev.client->addr,
219 		.len = len + 1,
220 		.buf = tpm_dev.buf
221 	};
222 
223 	if (len > TPM_I2C_INFINEON_BUFSIZE)
224 		return -EINVAL;
225 
226 	if (!tpm_dev.client->adapter->algo->master_xfer)
227 		return -EOPNOTSUPP;
228 	i2c_lock_bus(tpm_dev.client->adapter, I2C_LOCK_SEGMENT);
229 
230 	/* prepend the 'register address' to the buffer */
231 	tpm_dev.buf[0] = addr;
232 	memcpy(&(tpm_dev.buf[1]), buffer, len);
233 
234 	/*
235 	 * NOTE: We have to use these special mechanisms here and unfortunately
236 	 * cannot rely on the standard behavior of i2c_transfer.
237 	 * Even for newer chips the smbus functions are not
238 	 * suitable due to the 32 byte limit of the smbus.
239 	 */
240 	for (count = 0; count < max_count; count++) {
241 		rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1);
242 		if (rc > 0)
243 			break;
244 		usleep_range(sleep_low, sleep_hi);
245 	}
246 
247 	i2c_unlock_bus(tpm_dev.client->adapter, I2C_LOCK_SEGMENT);
248 	/* take care of 'guard time' */
249 	usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
250 
251 	/* __i2c_transfer returns the number of successfully transferred
252 	 * messages.
253 	 * So rc should be greater than 0 here otherwise we have an error.
254 	 */
255 	if (rc <= 0)
256 		return -EIO;
257 
258 	return 0;
259 }
260 
261 /*
262  * iic_tpm_write() - write to TPM register
263  * @addr: register address to write to
264  * @buffer: containing data to be written
265  * @len: number of bytes to write
266  *
267  * Write len bytes from provided buffer to TPM register (little
268  * endian format, i.e. buffer[0] is written as first byte).
269  *
270  * NOTE: TPM is big-endian for multi-byte values. Multi-byte
271  * values have to be swapped.
272  *
273  * NOTE: use this function instead of the iic_tpm_write_generic function.
274  *
275  * Return -EIO on error, 0 on success
276  */
277 static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
278 {
279 	return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LOW,
280 				     SLEEP_DURATION_HI, MAX_COUNT);
281 }
282 
283 /*
284  * This function is needed especially for the cleanup situation after
285  * sending TPM_READY
286  * */
287 static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
288 {
289 	return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG_LOW,
290 				     SLEEP_DURATION_LONG_HI, MAX_COUNT_LONG);
291 }
292 
293 enum tis_access {
294 	TPM_ACCESS_VALID = 0x80,
295 	TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
296 	TPM_ACCESS_REQUEST_PENDING = 0x04,
297 	TPM_ACCESS_REQUEST_USE = 0x02,
298 };
299 
300 enum tis_status {
301 	TPM_STS_VALID = 0x80,
302 	TPM_STS_COMMAND_READY = 0x40,
303 	TPM_STS_GO = 0x20,
304 	TPM_STS_DATA_AVAIL = 0x10,
305 	TPM_STS_DATA_EXPECT = 0x08,
306 };
307 
308 enum tis_defaults {
309 	TIS_SHORT_TIMEOUT = 750,	/* ms */
310 	TIS_LONG_TIMEOUT = 2000,	/* 2 sec */
311 };
312 
313 #define	TPM_ACCESS(l)			(0x0000 | ((l) << 4))
314 #define	TPM_STS(l)			(0x0001 | ((l) << 4))
315 #define	TPM_DATA_FIFO(l)		(0x0005 | ((l) << 4))
316 #define	TPM_DID_VID(l)			(0x0006 | ((l) << 4))
317 
318 static bool check_locality(struct tpm_chip *chip, int loc)
319 {
320 	u8 buf;
321 	int rc;
322 
323 	rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
324 	if (rc < 0)
325 		return false;
326 
327 	if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
328 	    (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
329 		tpm_dev.locality = loc;
330 		return true;
331 	}
332 
333 	return false;
334 }
335 
336 /* implementation similar to tpm_tis */
337 static void release_locality(struct tpm_chip *chip, int loc, int force)
338 {
339 	u8 buf;
340 	if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
341 		return;
342 
343 	if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
344 	    (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) {
345 		buf = TPM_ACCESS_ACTIVE_LOCALITY;
346 		iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
347 	}
348 }
349 
350 static int request_locality(struct tpm_chip *chip, int loc)
351 {
352 	unsigned long stop;
353 	u8 buf = TPM_ACCESS_REQUEST_USE;
354 
355 	if (check_locality(chip, loc))
356 		return loc;
357 
358 	iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
359 
360 	/* wait for burstcount */
361 	stop = jiffies + chip->timeout_a;
362 	do {
363 		if (check_locality(chip, loc))
364 			return loc;
365 		usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
366 	} while (time_before(jiffies, stop));
367 
368 	return -ETIME;
369 }
370 
371 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
372 {
373 	/* NOTE: since I2C read may fail, return 0 in this case --> time-out */
374 	u8 buf = 0xFF;
375 	u8 i = 0;
376 
377 	do {
378 		if (iic_tpm_read(TPM_STS(tpm_dev.locality), &buf, 1) < 0)
379 			return 0;
380 
381 		i++;
382 	/* if locallity is set STS should not be 0xFF */
383 	} while ((buf == 0xFF) && i < 10);
384 
385 	return buf;
386 }
387 
388 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
389 {
390 	/* this causes the current command to be aborted */
391 	u8 buf = TPM_STS_COMMAND_READY;
392 	iic_tpm_write_long(TPM_STS(tpm_dev.locality), &buf, 1);
393 }
394 
395 static ssize_t get_burstcount(struct tpm_chip *chip)
396 {
397 	unsigned long stop;
398 	ssize_t burstcnt;
399 	u8 buf[3];
400 
401 	/* wait for burstcount */
402 	/* which timeout value, spec has 2 answers (c & d) */
403 	stop = jiffies + chip->timeout_d;
404 	do {
405 		/* Note: STS is little endian */
406 		if (iic_tpm_read(TPM_STS(tpm_dev.locality)+1, buf, 3) < 0)
407 			burstcnt = 0;
408 		else
409 			burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
410 
411 		if (burstcnt)
412 			return burstcnt;
413 
414 		usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
415 	} while (time_before(jiffies, stop));
416 	return -EBUSY;
417 }
418 
419 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
420 			 int *status)
421 {
422 	unsigned long stop;
423 
424 	/* check current status */
425 	*status = tpm_tis_i2c_status(chip);
426 	if ((*status != 0xFF) && (*status & mask) == mask)
427 		return 0;
428 
429 	stop = jiffies + timeout;
430 	do {
431 		/* since we just checked the status, give the TPM some time */
432 		usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
433 		*status = tpm_tis_i2c_status(chip);
434 		if ((*status & mask) == mask)
435 			return 0;
436 
437 	} while (time_before(jiffies, stop));
438 
439 	return -ETIME;
440 }
441 
442 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
443 {
444 	size_t size = 0;
445 	ssize_t burstcnt;
446 	u8 retries = 0;
447 	int rc;
448 
449 	while (size < count) {
450 		burstcnt = get_burstcount(chip);
451 
452 		/* burstcnt < 0 = TPM is busy */
453 		if (burstcnt < 0)
454 			return burstcnt;
455 
456 		/* limit received data to max. left */
457 		if (burstcnt > (count - size))
458 			burstcnt = count - size;
459 
460 		rc = iic_tpm_read(TPM_DATA_FIFO(tpm_dev.locality),
461 				  &(buf[size]), burstcnt);
462 		if (rc == 0)
463 			size += burstcnt;
464 		else if (rc < 0)
465 			retries++;
466 
467 		/* avoid endless loop in case of broken HW */
468 		if (retries > MAX_COUNT_LONG)
469 			return -EIO;
470 	}
471 	return size;
472 }
473 
474 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
475 {
476 	int size = 0;
477 	int status;
478 	u32 expected;
479 
480 	if (count < TPM_HEADER_SIZE) {
481 		size = -EIO;
482 		goto out;
483 	}
484 
485 	/* read first 10 bytes, including tag, paramsize, and result */
486 	size = recv_data(chip, buf, TPM_HEADER_SIZE);
487 	if (size < TPM_HEADER_SIZE) {
488 		dev_err(&chip->dev, "Unable to read header\n");
489 		goto out;
490 	}
491 
492 	expected = be32_to_cpu(*(__be32 *)(buf + 2));
493 	if (((size_t) expected > count) || (expected < TPM_HEADER_SIZE)) {
494 		size = -EIO;
495 		goto out;
496 	}
497 
498 	size += recv_data(chip, &buf[TPM_HEADER_SIZE],
499 			  expected - TPM_HEADER_SIZE);
500 	if (size < expected) {
501 		dev_err(&chip->dev, "Unable to read remainder of result\n");
502 		size = -ETIME;
503 		goto out;
504 	}
505 
506 	wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c, &status);
507 	if (status & TPM_STS_DATA_AVAIL) {	/* retry? */
508 		dev_err(&chip->dev, "Error left over data\n");
509 		size = -EIO;
510 		goto out;
511 	}
512 
513 out:
514 	tpm_tis_i2c_ready(chip);
515 	/* The TPM needs some time to clean up here,
516 	 * so we sleep rather than keeping the bus busy
517 	 */
518 	usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI);
519 	release_locality(chip, tpm_dev.locality, 0);
520 	return size;
521 }
522 
523 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
524 {
525 	int rc, status;
526 	ssize_t burstcnt;
527 	size_t count = 0;
528 	u8 retries = 0;
529 	u8 sts = TPM_STS_GO;
530 
531 	if (len > TPM_I2C_INFINEON_BUFSIZE)
532 		return -E2BIG;
533 
534 	if (request_locality(chip, 0) < 0)
535 		return -EBUSY;
536 
537 	status = tpm_tis_i2c_status(chip);
538 	if ((status & TPM_STS_COMMAND_READY) == 0) {
539 		tpm_tis_i2c_ready(chip);
540 		if (wait_for_stat
541 		    (chip, TPM_STS_COMMAND_READY,
542 		     chip->timeout_b, &status) < 0) {
543 			rc = -ETIME;
544 			goto out_err;
545 		}
546 	}
547 
548 	while (count < len - 1) {
549 		burstcnt = get_burstcount(chip);
550 
551 		/* burstcnt < 0 = TPM is busy */
552 		if (burstcnt < 0)
553 			return burstcnt;
554 
555 		if (burstcnt > (len - 1 - count))
556 			burstcnt = len - 1 - count;
557 
558 		rc = iic_tpm_write(TPM_DATA_FIFO(tpm_dev.locality),
559 				   &(buf[count]), burstcnt);
560 		if (rc == 0)
561 			count += burstcnt;
562 		else if (rc < 0)
563 			retries++;
564 
565 		/* avoid endless loop in case of broken HW */
566 		if (retries > MAX_COUNT_LONG) {
567 			rc = -EIO;
568 			goto out_err;
569 		}
570 
571 		wait_for_stat(chip, TPM_STS_VALID,
572 			      chip->timeout_c, &status);
573 
574 		if ((status & TPM_STS_DATA_EXPECT) == 0) {
575 			rc = -EIO;
576 			goto out_err;
577 		}
578 	}
579 
580 	/* write last byte */
581 	iic_tpm_write(TPM_DATA_FIFO(tpm_dev.locality), &(buf[count]), 1);
582 	wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c, &status);
583 	if ((status & TPM_STS_DATA_EXPECT) != 0) {
584 		rc = -EIO;
585 		goto out_err;
586 	}
587 
588 	/* go and do it */
589 	iic_tpm_write(TPM_STS(tpm_dev.locality), &sts, 1);
590 
591 	return 0;
592 out_err:
593 	tpm_tis_i2c_ready(chip);
594 	/* The TPM needs some time to clean up here,
595 	 * so we sleep rather than keeping the bus busy
596 	 */
597 	usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI);
598 	release_locality(chip, tpm_dev.locality, 0);
599 	return rc;
600 }
601 
602 static bool tpm_tis_i2c_req_canceled(struct tpm_chip *chip, u8 status)
603 {
604 	return (status == TPM_STS_COMMAND_READY);
605 }
606 
607 static const struct tpm_class_ops tpm_tis_i2c = {
608 	.flags = TPM_OPS_AUTO_STARTUP,
609 	.status = tpm_tis_i2c_status,
610 	.recv = tpm_tis_i2c_recv,
611 	.send = tpm_tis_i2c_send,
612 	.cancel = tpm_tis_i2c_ready,
613 	.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
614 	.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
615 	.req_canceled = tpm_tis_i2c_req_canceled,
616 };
617 
618 static int tpm_tis_i2c_init(struct device *dev)
619 {
620 	u32 vendor;
621 	int rc = 0;
622 	struct tpm_chip *chip;
623 
624 	chip = tpmm_chip_alloc(dev, &tpm_tis_i2c);
625 	if (IS_ERR(chip))
626 		return PTR_ERR(chip);
627 
628 	/* Default timeouts */
629 	chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
630 	chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
631 	chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
632 	chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
633 
634 	if (request_locality(chip, 0) != 0) {
635 		dev_err(dev, "could not request locality\n");
636 		rc = -ENODEV;
637 		goto out_err;
638 	}
639 
640 	/* read four bytes from DID_VID register */
641 	if (iic_tpm_read(TPM_DID_VID(0), (u8 *)&vendor, 4) < 0) {
642 		dev_err(dev, "could not read vendor id\n");
643 		rc = -EIO;
644 		goto out_release;
645 	}
646 
647 	if (vendor == TPM_TIS_I2C_DID_VID_9645) {
648 		tpm_dev.chip_type = SLB9645;
649 	} else if (vendor == TPM_TIS_I2C_DID_VID_9635) {
650 		tpm_dev.chip_type = SLB9635;
651 	} else {
652 		dev_err(dev, "vendor id did not match! ID was %08x\n", vendor);
653 		rc = -ENODEV;
654 		goto out_release;
655 	}
656 
657 	dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16);
658 
659 	tpm_dev.chip = chip;
660 
661 	return tpm_chip_register(chip);
662 out_release:
663 	release_locality(chip, tpm_dev.locality, 1);
664 	tpm_dev.client = NULL;
665 out_err:
666 	return rc;
667 }
668 
669 static const struct i2c_device_id tpm_tis_i2c_table[] = {
670 	{"tpm_i2c_infineon"},
671 	{"slb9635tt"},
672 	{"slb9645tt"},
673 	{},
674 };
675 
676 MODULE_DEVICE_TABLE(i2c, tpm_tis_i2c_table);
677 
678 #ifdef CONFIG_OF
679 static const struct of_device_id tpm_tis_i2c_of_match[] = {
680 	{.compatible = "infineon,tpm_i2c_infineon"},
681 	{.compatible = "infineon,slb9635tt"},
682 	{.compatible = "infineon,slb9645tt"},
683 	{},
684 };
685 MODULE_DEVICE_TABLE(of, tpm_tis_i2c_of_match);
686 #endif
687 
688 static SIMPLE_DEV_PM_OPS(tpm_tis_i2c_ops, tpm_pm_suspend, tpm_pm_resume);
689 
690 static int tpm_tis_i2c_probe(struct i2c_client *client,
691 			     const struct i2c_device_id *id)
692 {
693 	int rc;
694 	struct device *dev = &(client->dev);
695 
696 	if (tpm_dev.client != NULL) {
697 		dev_err(dev, "This driver only supports one client at a time\n");
698 		return -EBUSY;	/* We only support one client */
699 	}
700 
701 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
702 		dev_err(dev, "no algorithms associated to the i2c bus\n");
703 		return -ENODEV;
704 	}
705 
706 	tpm_dev.client = client;
707 	rc = tpm_tis_i2c_init(&client->dev);
708 	if (rc != 0) {
709 		tpm_dev.client = NULL;
710 		rc = -ENODEV;
711 	}
712 	return rc;
713 }
714 
715 static int tpm_tis_i2c_remove(struct i2c_client *client)
716 {
717 	struct tpm_chip *chip = tpm_dev.chip;
718 
719 	tpm_chip_unregister(chip);
720 	release_locality(chip, tpm_dev.locality, 1);
721 	tpm_dev.client = NULL;
722 
723 	return 0;
724 }
725 
726 static struct i2c_driver tpm_tis_i2c_driver = {
727 	.id_table = tpm_tis_i2c_table,
728 	.probe = tpm_tis_i2c_probe,
729 	.remove = tpm_tis_i2c_remove,
730 	.driver = {
731 		   .name = "tpm_i2c_infineon",
732 		   .pm = &tpm_tis_i2c_ops,
733 		   .of_match_table = of_match_ptr(tpm_tis_i2c_of_match),
734 		   },
735 };
736 
737 module_i2c_driver(tpm_tis_i2c_driver);
738 MODULE_AUTHOR("Peter Huewe <peter.huewe@infineon.com>");
739 MODULE_DESCRIPTION("TPM TIS I2C Infineon Driver");
740 MODULE_VERSION("2.2.0");
741 MODULE_LICENSE("GPL");
742