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