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