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/init.h>
25 #include <linux/i2c.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/wait.h>
29 #include "tpm.h"
30 
31 /* max. buffer size supported by our TPM */
32 #define TPM_BUFSIZE 1260
33 
34 /* max. number of iterations after I2C NAK */
35 #define MAX_COUNT 3
36 
37 #define SLEEP_DURATION_LOW 55
38 #define SLEEP_DURATION_HI 65
39 
40 /* max. number of iterations after I2C NAK for 'long' commands
41  * we need this especially for sending TPM_READY, since the cleanup after the
42  * transtion to the ready state may take some time, but it is unpredictable
43  * how long it will take.
44  */
45 #define MAX_COUNT_LONG 50
46 
47 #define SLEEP_DURATION_LONG_LOW 200
48 #define SLEEP_DURATION_LONG_HI 220
49 
50 /* After sending TPM_READY to 'reset' the TPM we have to sleep even longer */
51 #define SLEEP_DURATION_RESET_LOW 2400
52 #define SLEEP_DURATION_RESET_HI 2600
53 
54 /* we want to use usleep_range instead of msleep for the 5ms TPM_TIMEOUT */
55 #define TPM_TIMEOUT_US_LOW (TPM_TIMEOUT * 1000)
56 #define TPM_TIMEOUT_US_HI  (TPM_TIMEOUT_US_LOW + 2000)
57 
58 /* expected value for DIDVID register */
59 #define TPM_TIS_I2C_DID_VID_9635 0xd1150b00L
60 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
61 
62 enum i2c_chip_type {
63 	SLB9635,
64 	SLB9645,
65 	UNKNOWN,
66 };
67 
68 /* Structure to store I2C TPM specific stuff */
69 struct tpm_inf_dev {
70 	struct i2c_client *client;
71 	u8 buf[TPM_BUFSIZE + sizeof(u8)]; /* max. buffer size + addr */
72 	struct tpm_chip *chip;
73 	enum i2c_chip_type chip_type;
74 };
75 
76 static struct tpm_inf_dev tpm_dev;
77 static struct i2c_driver tpm_tis_i2c_driver;
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 
117 	/* Lock the adapter for the duration of the whole sequence. */
118 	if (!tpm_dev.client->adapter->algo->master_xfer)
119 		return -EOPNOTSUPP;
120 	i2c_lock_adapter(tpm_dev.client->adapter);
121 
122 	if (tpm_dev.chip_type == SLB9645) {
123 		/* use a combined read for newer chips
124 		 * unfortunately the smbus functions are not suitable due to
125 		 * the 32 byte limit of the smbus.
126 		 * retries should usually not be needed, but are kept just to
127 		 * be on the safe side.
128 		 */
129 		for (count = 0; count < MAX_COUNT; count++) {
130 			rc = __i2c_transfer(tpm_dev.client->adapter, msgs, 2);
131 			if (rc > 0)
132 				break;	/* break here to skip sleep */
133 			usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
134 		}
135 	} else {
136 		/* slb9635 protocol should work in all cases */
137 		for (count = 0; count < MAX_COUNT; count++) {
138 			rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1);
139 			if (rc > 0)
140 				break;	/* break here to skip sleep */
141 
142 			usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
143 		}
144 
145 		if (rc <= 0)
146 			goto out;
147 
148 		/* After the TPM has successfully received the register address
149 		 * it needs some time, thus we're sleeping here again, before
150 		 * retrieving the data
151 		 */
152 		for (count = 0; count < MAX_COUNT; count++) {
153 			usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
154 			rc = __i2c_transfer(tpm_dev.client->adapter, &msg2, 1);
155 			if (rc > 0)
156 				break;
157 		}
158 	}
159 
160 out:
161 	i2c_unlock_adapter(tpm_dev.client->adapter);
162 	/* take care of 'guard time' */
163 	usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
164 
165 	/* __i2c_transfer returns the number of successfully transferred
166 	 * messages.
167 	 * So rc should be greater than 0 here otherwise we have an error.
168 	 */
169 	if (rc <= 0)
170 		return -EIO;
171 
172 	return 0;
173 }
174 
175 static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
176 				 unsigned int sleep_low,
177 				 unsigned int sleep_hi, u8 max_count)
178 {
179 	int rc = -EIO;
180 	int count;
181 
182 	struct i2c_msg msg1 = {
183 		.addr = tpm_dev.client->addr,
184 		.len = len + 1,
185 		.buf = tpm_dev.buf
186 	};
187 
188 	if (len > TPM_BUFSIZE)
189 		return -EINVAL;
190 
191 	if (!tpm_dev.client->adapter->algo->master_xfer)
192 		return -EOPNOTSUPP;
193 	i2c_lock_adapter(tpm_dev.client->adapter);
194 
195 	/* prepend the 'register address' to the buffer */
196 	tpm_dev.buf[0] = addr;
197 	memcpy(&(tpm_dev.buf[1]), buffer, len);
198 
199 	/*
200 	 * NOTE: We have to use these special mechanisms here and unfortunately
201 	 * cannot rely on the standard behavior of i2c_transfer.
202 	 * Even for newer chips the smbus functions are not
203 	 * suitable due to the 32 byte limit of the smbus.
204 	 */
205 	for (count = 0; count < max_count; count++) {
206 		rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1);
207 		if (rc > 0)
208 			break;
209 		usleep_range(sleep_low, sleep_hi);
210 	}
211 
212 	i2c_unlock_adapter(tpm_dev.client->adapter);
213 	/* take care of 'guard time' */
214 	usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
215 
216 	/* __i2c_transfer returns the number of successfully transferred
217 	 * messages.
218 	 * So rc should be greater than 0 here otherwise we have an error.
219 	 */
220 	if (rc <= 0)
221 		return -EIO;
222 
223 	return 0;
224 }
225 
226 /*
227  * iic_tpm_write() - write to TPM register
228  * @addr: register address to write to
229  * @buffer: containing data to be written
230  * @len: number of bytes to write
231  *
232  * Write len bytes from provided buffer to TPM register (little
233  * endian format, i.e. buffer[0] is written as first byte).
234  *
235  * NOTE: TPM is big-endian for multi-byte values. Multi-byte
236  * values have to be swapped.
237  *
238  * NOTE: use this function instead of the iic_tpm_write_generic function.
239  *
240  * Return -EIO on error, 0 on success
241  */
242 static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
243 {
244 	return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LOW,
245 				     SLEEP_DURATION_HI, MAX_COUNT);
246 }
247 
248 /*
249  * This function is needed especially for the cleanup situation after
250  * sending TPM_READY
251  * */
252 static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
253 {
254 	return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG_LOW,
255 				     SLEEP_DURATION_LONG_HI, MAX_COUNT_LONG);
256 }
257 
258 enum tis_access {
259 	TPM_ACCESS_VALID = 0x80,
260 	TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
261 	TPM_ACCESS_REQUEST_PENDING = 0x04,
262 	TPM_ACCESS_REQUEST_USE = 0x02,
263 };
264 
265 enum tis_status {
266 	TPM_STS_VALID = 0x80,
267 	TPM_STS_COMMAND_READY = 0x40,
268 	TPM_STS_GO = 0x20,
269 	TPM_STS_DATA_AVAIL = 0x10,
270 	TPM_STS_DATA_EXPECT = 0x08,
271 };
272 
273 enum tis_defaults {
274 	TIS_SHORT_TIMEOUT = 750,	/* ms */
275 	TIS_LONG_TIMEOUT = 2000,	/* 2 sec */
276 };
277 
278 #define	TPM_ACCESS(l)			(0x0000 | ((l) << 4))
279 #define	TPM_STS(l)			(0x0001 | ((l) << 4))
280 #define	TPM_DATA_FIFO(l)		(0x0005 | ((l) << 4))
281 #define	TPM_DID_VID(l)			(0x0006 | ((l) << 4))
282 
283 static int check_locality(struct tpm_chip *chip, int loc)
284 {
285 	u8 buf;
286 	int rc;
287 
288 	rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
289 	if (rc < 0)
290 		return rc;
291 
292 	if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
293 	    (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
294 		chip->vendor.locality = loc;
295 		return loc;
296 	}
297 
298 	return -EIO;
299 }
300 
301 /* implementation similar to tpm_tis */
302 static void release_locality(struct tpm_chip *chip, int loc, int force)
303 {
304 	u8 buf;
305 	if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
306 		return;
307 
308 	if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
309 	    (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) {
310 		buf = TPM_ACCESS_ACTIVE_LOCALITY;
311 		iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
312 	}
313 }
314 
315 static int request_locality(struct tpm_chip *chip, int loc)
316 {
317 	unsigned long stop;
318 	u8 buf = TPM_ACCESS_REQUEST_USE;
319 
320 	if (check_locality(chip, loc) >= 0)
321 		return loc;
322 
323 	iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
324 
325 	/* wait for burstcount */
326 	stop = jiffies + chip->vendor.timeout_a;
327 	do {
328 		if (check_locality(chip, loc) >= 0)
329 			return loc;
330 		usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
331 	} while (time_before(jiffies, stop));
332 
333 	return -ETIME;
334 }
335 
336 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
337 {
338 	/* NOTE: since I2C read may fail, return 0 in this case --> time-out */
339 	u8 buf = 0xFF;
340 	u8 i = 0;
341 
342 	do {
343 		if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
344 			return 0;
345 
346 		i++;
347 	/* if locallity is set STS should not be 0xFF */
348 	} while ((buf == 0xFF) && i < 10);
349 
350 	return buf;
351 }
352 
353 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
354 {
355 	/* this causes the current command to be aborted */
356 	u8 buf = TPM_STS_COMMAND_READY;
357 	iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
358 }
359 
360 static ssize_t get_burstcount(struct tpm_chip *chip)
361 {
362 	unsigned long stop;
363 	ssize_t burstcnt;
364 	u8 buf[3];
365 
366 	/* wait for burstcount */
367 	/* which timeout value, spec has 2 answers (c & d) */
368 	stop = jiffies + chip->vendor.timeout_d;
369 	do {
370 		/* Note: STS is little endian */
371 		if (iic_tpm_read(TPM_STS(chip->vendor.locality)+1, buf, 3) < 0)
372 			burstcnt = 0;
373 		else
374 			burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
375 
376 		if (burstcnt)
377 			return burstcnt;
378 
379 		usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
380 	} while (time_before(jiffies, stop));
381 	return -EBUSY;
382 }
383 
384 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
385 			 int *status)
386 {
387 	unsigned long stop;
388 
389 	/* check current status */
390 	*status = tpm_tis_i2c_status(chip);
391 	if ((*status != 0xFF) && (*status & mask) == mask)
392 		return 0;
393 
394 	stop = jiffies + timeout;
395 	do {
396 		/* since we just checked the status, give the TPM some time */
397 		usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
398 		*status = tpm_tis_i2c_status(chip);
399 		if ((*status & mask) == mask)
400 			return 0;
401 
402 	} while (time_before(jiffies, stop));
403 
404 	return -ETIME;
405 }
406 
407 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
408 {
409 	size_t size = 0;
410 	ssize_t burstcnt;
411 	u8 retries = 0;
412 	int rc;
413 
414 	while (size < count) {
415 		burstcnt = get_burstcount(chip);
416 
417 		/* burstcnt < 0 = TPM is busy */
418 		if (burstcnt < 0)
419 			return burstcnt;
420 
421 		/* limit received data to max. left */
422 		if (burstcnt > (count - size))
423 			burstcnt = count - size;
424 
425 		rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
426 				  &(buf[size]), burstcnt);
427 		if (rc == 0)
428 			size += burstcnt;
429 		else if (rc < 0)
430 			retries++;
431 
432 		/* avoid endless loop in case of broken HW */
433 		if (retries > MAX_COUNT_LONG)
434 			return -EIO;
435 	}
436 	return size;
437 }
438 
439 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
440 {
441 	int size = 0;
442 	int expected, status;
443 
444 	if (count < TPM_HEADER_SIZE) {
445 		size = -EIO;
446 		goto out;
447 	}
448 
449 	/* read first 10 bytes, including tag, paramsize, and result */
450 	size = recv_data(chip, buf, TPM_HEADER_SIZE);
451 	if (size < TPM_HEADER_SIZE) {
452 		dev_err(chip->dev, "Unable to read header\n");
453 		goto out;
454 	}
455 
456 	expected = be32_to_cpu(*(__be32 *)(buf + 2));
457 	if ((size_t) expected > count) {
458 		size = -EIO;
459 		goto out;
460 	}
461 
462 	size += recv_data(chip, &buf[TPM_HEADER_SIZE],
463 			  expected - TPM_HEADER_SIZE);
464 	if (size < expected) {
465 		dev_err(chip->dev, "Unable to read remainder of result\n");
466 		size = -ETIME;
467 		goto out;
468 	}
469 
470 	wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
471 	if (status & TPM_STS_DATA_AVAIL) {	/* retry? */
472 		dev_err(chip->dev, "Error left over data\n");
473 		size = -EIO;
474 		goto out;
475 	}
476 
477 out:
478 	tpm_tis_i2c_ready(chip);
479 	/* The TPM needs some time to clean up here,
480 	 * so we sleep rather than keeping the bus busy
481 	 */
482 	usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI);
483 	release_locality(chip, chip->vendor.locality, 0);
484 	return size;
485 }
486 
487 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
488 {
489 	int rc, status;
490 	ssize_t burstcnt;
491 	size_t count = 0;
492 	u8 retries = 0;
493 	u8 sts = TPM_STS_GO;
494 
495 	if (len > TPM_BUFSIZE)
496 		return -E2BIG;	/* command is too long for our tpm, sorry */
497 
498 	if (request_locality(chip, 0) < 0)
499 		return -EBUSY;
500 
501 	status = tpm_tis_i2c_status(chip);
502 	if ((status & TPM_STS_COMMAND_READY) == 0) {
503 		tpm_tis_i2c_ready(chip);
504 		if (wait_for_stat
505 		    (chip, TPM_STS_COMMAND_READY,
506 		     chip->vendor.timeout_b, &status) < 0) {
507 			rc = -ETIME;
508 			goto out_err;
509 		}
510 	}
511 
512 	while (count < len - 1) {
513 		burstcnt = get_burstcount(chip);
514 
515 		/* burstcnt < 0 = TPM is busy */
516 		if (burstcnt < 0)
517 			return burstcnt;
518 
519 		if (burstcnt > (len - 1 - count))
520 			burstcnt = len - 1 - count;
521 
522 		rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
523 				   &(buf[count]), burstcnt);
524 		if (rc == 0)
525 			count += burstcnt;
526 		else if (rc < 0)
527 			retries++;
528 
529 		/* avoid endless loop in case of broken HW */
530 		if (retries > MAX_COUNT_LONG) {
531 			rc = -EIO;
532 			goto out_err;
533 		}
534 
535 		wait_for_stat(chip, TPM_STS_VALID,
536 			      chip->vendor.timeout_c, &status);
537 
538 		if ((status & TPM_STS_DATA_EXPECT) == 0) {
539 			rc = -EIO;
540 			goto out_err;
541 		}
542 	}
543 
544 	/* write last byte */
545 	iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), &(buf[count]), 1);
546 	wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
547 	if ((status & TPM_STS_DATA_EXPECT) != 0) {
548 		rc = -EIO;
549 		goto out_err;
550 	}
551 
552 	/* go and do it */
553 	iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
554 
555 	return len;
556 out_err:
557 	tpm_tis_i2c_ready(chip);
558 	/* The TPM needs some time to clean up here,
559 	 * so we sleep rather than keeping the bus busy
560 	 */
561 	usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI);
562 	release_locality(chip, chip->vendor.locality, 0);
563 	return rc;
564 }
565 
566 static bool tpm_tis_i2c_req_canceled(struct tpm_chip *chip, u8 status)
567 {
568 	return (status == TPM_STS_COMMAND_READY);
569 }
570 
571 static const struct file_operations tis_ops = {
572 	.owner = THIS_MODULE,
573 	.llseek = no_llseek,
574 	.open = tpm_open,
575 	.read = tpm_read,
576 	.write = tpm_write,
577 	.release = tpm_release,
578 };
579 
580 static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
581 static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
582 static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
583 static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
584 static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
585 static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
586 static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
587 static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
588 static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
589 static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
590 
591 static struct attribute *tis_attrs[] = {
592 	&dev_attr_pubek.attr,
593 	&dev_attr_pcrs.attr,
594 	&dev_attr_enabled.attr,
595 	&dev_attr_active.attr,
596 	&dev_attr_owned.attr,
597 	&dev_attr_temp_deactivated.attr,
598 	&dev_attr_caps.attr,
599 	&dev_attr_cancel.attr,
600 	&dev_attr_durations.attr,
601 	&dev_attr_timeouts.attr,
602 	NULL,
603 };
604 
605 static struct attribute_group tis_attr_grp = {
606 	.attrs = tis_attrs
607 };
608 
609 static struct tpm_vendor_specific tpm_tis_i2c = {
610 	.status = tpm_tis_i2c_status,
611 	.recv = tpm_tis_i2c_recv,
612 	.send = tpm_tis_i2c_send,
613 	.cancel = tpm_tis_i2c_ready,
614 	.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
615 	.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
616 	.req_canceled = tpm_tis_i2c_req_canceled,
617 	.attr_group = &tis_attr_grp,
618 	.miscdev.fops = &tis_ops,
619 };
620 
621 static int tpm_tis_i2c_init(struct device *dev)
622 {
623 	u32 vendor;
624 	int rc = 0;
625 	struct tpm_chip *chip;
626 
627 	chip = tpm_register_hardware(dev, &tpm_tis_i2c);
628 	if (!chip) {
629 		dev_err(dev, "could not register hardware\n");
630 		rc = -ENODEV;
631 		goto out_err;
632 	}
633 
634 	/* Disable interrupts */
635 	chip->vendor.irq = 0;
636 
637 	/* Default timeouts */
638 	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
639 	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
640 	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
641 	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
642 
643 	if (request_locality(chip, 0) != 0) {
644 		dev_err(dev, "could not request locality\n");
645 		rc = -ENODEV;
646 		goto out_vendor;
647 	}
648 
649 	/* read four bytes from DID_VID register */
650 	if (iic_tpm_read(TPM_DID_VID(0), (u8 *)&vendor, 4) < 0) {
651 		dev_err(dev, "could not read vendor id\n");
652 		rc = -EIO;
653 		goto out_release;
654 	}
655 
656 	if (vendor == TPM_TIS_I2C_DID_VID_9645) {
657 		tpm_dev.chip_type = SLB9645;
658 	} else if (vendor == TPM_TIS_I2C_DID_VID_9635) {
659 		tpm_dev.chip_type = SLB9635;
660 	} else {
661 		dev_err(dev, "vendor id did not match! ID was %08x\n", vendor);
662 		rc = -ENODEV;
663 		goto out_release;
664 	}
665 
666 	dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16);
667 
668 	INIT_LIST_HEAD(&chip->vendor.list);
669 	tpm_dev.chip = chip;
670 
671 	tpm_get_timeouts(chip);
672 	tpm_do_selftest(chip);
673 
674 	return 0;
675 
676 out_release:
677 	release_locality(chip, chip->vendor.locality, 1);
678 
679 out_vendor:
680 	/* close file handles */
681 	tpm_dev_vendor_release(chip);
682 
683 	/* remove hardware */
684 	tpm_remove_hardware(chip->dev);
685 
686 	/* reset these pointers, otherwise we oops */
687 	chip->dev->release = NULL;
688 	chip->release = NULL;
689 	tpm_dev.client = NULL;
690 	dev_set_drvdata(chip->dev, chip);
691 out_err:
692 	return rc;
693 }
694 
695 static const struct i2c_device_id tpm_tis_i2c_table[] = {
696 	{"tpm_i2c_infineon", 0},
697 	{"slb9635tt", 0},
698 	{"slb9645tt", 1},
699 	{},
700 };
701 
702 MODULE_DEVICE_TABLE(i2c, tpm_tis_i2c_table);
703 
704 #ifdef CONFIG_OF
705 static const struct of_device_id tpm_tis_i2c_of_match[] = {
706 	{
707 		.name = "tpm_i2c_infineon",
708 		.type = "tpm",
709 		.compatible = "infineon,tpm_i2c_infineon",
710 		.data = (void *)0
711 	},
712 	{
713 		.name = "slb9635tt",
714 		.type = "tpm",
715 		.compatible = "infineon,slb9635tt",
716 		.data = (void *)0
717 	},
718 	{
719 		.name = "slb9645tt",
720 		.type = "tpm",
721 		.compatible = "infineon,slb9645tt",
722 		.data = (void *)1
723 	},
724 	{},
725 };
726 MODULE_DEVICE_TABLE(of, tpm_tis_i2c_of_match);
727 #endif
728 
729 static SIMPLE_DEV_PM_OPS(tpm_tis_i2c_ops, tpm_pm_suspend, tpm_pm_resume);
730 
731 static int tpm_tis_i2c_probe(struct i2c_client *client,
732 			     const struct i2c_device_id *id)
733 {
734 	int rc;
735 	struct device *dev = &(client->dev);
736 
737 	if (tpm_dev.client != NULL) {
738 		dev_err(dev, "This driver only supports one client at a time\n");
739 		return -EBUSY;	/* We only support one client */
740 	}
741 
742 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
743 		dev_err(dev, "no algorithms associated to the i2c bus\n");
744 		return -ENODEV;
745 	}
746 
747 	client->driver = &tpm_tis_i2c_driver;
748 	tpm_dev.client = client;
749 	rc = tpm_tis_i2c_init(&client->dev);
750 	if (rc != 0) {
751 		client->driver = NULL;
752 		tpm_dev.client = NULL;
753 		rc = -ENODEV;
754 	}
755 	return rc;
756 }
757 
758 static int tpm_tis_i2c_remove(struct i2c_client *client)
759 {
760 	struct tpm_chip *chip = tpm_dev.chip;
761 	release_locality(chip, chip->vendor.locality, 1);
762 
763 	/* close file handles */
764 	tpm_dev_vendor_release(chip);
765 
766 	/* remove hardware */
767 	tpm_remove_hardware(chip->dev);
768 
769 	/* reset these pointers, otherwise we oops */
770 	chip->dev->release = NULL;
771 	chip->release = NULL;
772 	tpm_dev.client = NULL;
773 	dev_set_drvdata(chip->dev, chip);
774 
775 	return 0;
776 }
777 
778 static struct i2c_driver tpm_tis_i2c_driver = {
779 	.id_table = tpm_tis_i2c_table,
780 	.probe = tpm_tis_i2c_probe,
781 	.remove = tpm_tis_i2c_remove,
782 	.driver = {
783 		   .name = "tpm_i2c_infineon",
784 		   .owner = THIS_MODULE,
785 		   .pm = &tpm_tis_i2c_ops,
786 		   .of_match_table = of_match_ptr(tpm_tis_i2c_of_match),
787 		   },
788 };
789 
790 module_i2c_driver(tpm_tis_i2c_driver);
791 MODULE_AUTHOR("Peter Huewe <peter.huewe@infineon.com>");
792 MODULE_DESCRIPTION("TPM TIS I2C Infineon Driver");
793 MODULE_VERSION("2.2.0");
794 MODULE_LICENSE("GPL");
795