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