tpm_i2c_stm_st33.c 22.6 KB
Newer Older
1 2
/*
 * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24
3
 * Copyright (C) 2009, 2010, 2014  STMicroelectronics
4 5 6 7 8 9 10 11 12 13 14
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
15 16
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * STMicroelectronics version 1.2.1, Copyright (C) 2014
19 20 21 22 23 24 25 26 27 28
 * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
 * This is free software, and you are welcome to redistribute it
 * under certain conditions.
 *
 * @Author: Christophe RICARD tpmsupport@st.com
 *
 * @File: tpm_stm_st33_i2c.c
 *
 * @Synopsis:
 *	09/15/2010:	First shot driver tpm_tis driver for
29
 *			 lpc is used as model.
30 31
 */

K
Kent Yoder 已提交
32 33 34 35 36 37 38 39 40
#include <linux/pci.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/wait.h>
41
#include <linux/freezer.h>
K
Kent Yoder 已提交
42 43 44 45 46 47 48 49
#include <linux/string.h>
#include <linux/interrupt.h>
#include <linux/sysfs.h>
#include <linux/gpio.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/slab.h>
50 51
#include <linux/of_irq.h>
#include <linux/of_gpio.h>
K
Kent Yoder 已提交
52

53
#include <linux/platform_data/tpm_stm_st33.h>
K
Kent Yoder 已提交
54
#include "tpm.h"
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

#define TPM_ACCESS			0x0
#define TPM_STS				0x18
#define TPM_HASH_END			0x20
#define TPM_DATA_FIFO			0x24
#define TPM_HASH_DATA			0x24
#define TPM_HASH_START			0x28
#define TPM_INTF_CAPABILITY		0x14
#define TPM_INT_STATUS			0x10
#define TPM_INT_ENABLE			0x08

#define TPM_DUMMY_BYTE			0xAA
#define TPM_WRITE_DIRECTION		0x80
#define TPM_HEADER_SIZE			10
#define TPM_BUFSIZE			2048

#define LOCALITY0		0
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

enum stm33zp24_access {
	TPM_ACCESS_VALID = 0x80,
	TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
	TPM_ACCESS_REQUEST_PENDING = 0x04,
	TPM_ACCESS_REQUEST_USE = 0x02,
};

enum stm33zp24_status {
	TPM_STS_VALID = 0x80,
	TPM_STS_COMMAND_READY = 0x40,
	TPM_STS_GO = 0x20,
	TPM_STS_DATA_AVAIL = 0x10,
	TPM_STS_DATA_EXPECT = 0x08,
};

enum stm33zp24_int_flags {
	TPM_GLOBAL_INT_ENABLE = 0x80,
	TPM_INTF_CMD_READY_INT = 0x080,
	TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
	TPM_INTF_WAKE_UP_READY_INT = 0x020,
	TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
	TPM_INTF_STS_VALID_INT = 0x002,
	TPM_INTF_DATA_AVAIL_INT = 0x001,
};

enum tis_defaults {
	TIS_SHORT_TIMEOUT = 750,
	TIS_LONG_TIMEOUT = 2000,
};

104 105 106 107
struct tpm_stm_dev {
	struct i2c_client *client;
	struct tpm_chip *chip;
	u8 buf[TPM_BUFSIZE + 1];
108
	u32 intrs;
109 110 111
	int io_lpcpd;
};

112 113 114 115 116 117 118 119
/*
 * write8_reg
 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
 * @param: tpm_register, the tpm tis register where the data should be written
 * @param: tpm_data, the tpm_data to write inside the tpm_register
 * @param: tpm_size, The length of the data
 * @return: Returns negative errno, or else the number of bytes written.
 */
120
static int write8_reg(struct tpm_stm_dev *tpm_dev, u8 tpm_register,
121 122
		      u8 *tpm_data, u16 tpm_size)
{
123 124 125
	tpm_dev->buf[0] = tpm_register;
	memcpy(tpm_dev->buf + 1, tpm_data, tpm_size);
	return i2c_master_send(tpm_dev->client, tpm_dev->buf, tpm_size + 1);
126 127 128 129 130 131 132 133 134 135
} /* write8_reg() */

/*
 * read8_reg
 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
 * @param: tpm_register, the tpm tis register where the data should be read
 * @param: tpm_data, the TPM response
 * @param: tpm_size, tpm TPM response size to read.
 * @return: number of byte read successfully: should be one if success.
 */
136
static int read8_reg(struct tpm_stm_dev *tpm_dev, u8 tpm_register,
137 138 139 140 141 142
		    u8 *tpm_data, int tpm_size)
{
	u8 status = 0;
	u8 data;

	data = TPM_DUMMY_BYTE;
143
	status = write8_reg(tpm_dev, tpm_register, &data, 1);
144
	if (status == 2)
145
		status = i2c_master_recv(tpm_dev->client, tpm_data, tpm_size);
146 147 148 149 150 151
	return status;
} /* read8_reg() */

/*
 * I2C_WRITE_DATA
 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
152
 * @param: tpm_dev, the chip description
153 154 155 156 157
 * @param: tpm_register, the tpm tis register where the data should be written
 * @param: tpm_data, the tpm_data to write inside the tpm_register
 * @param: tpm_size, The length of the data
 * @return: number of byte written successfully: should be one if success.
 */
158 159
#define I2C_WRITE_DATA(tpm_dev, tpm_register, tpm_data, tpm_size) \
	(write8_reg(tpm_dev, tpm_register | \
160 161 162 163 164
	TPM_WRITE_DIRECTION, tpm_data, tpm_size))

/*
 * I2C_READ_DATA
 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
165
 * @param: tpm_dev, the chip description
166 167 168 169 170
 * @param: tpm_register, the tpm tis register where the data should be read
 * @param: tpm_data, the TPM response
 * @param: tpm_size, tpm TPM response size to read.
 * @return: number of byte read successfully: should be one if success.
 */
171 172
#define I2C_READ_DATA(tpm_dev, tpm_register, tpm_data, tpm_size) \
	(read8_reg(tpm_dev, tpm_register, tpm_data, tpm_size))
173 174 175 176 177

/*
 * clear_interruption
 * clear the TPM interrupt register.
 * @param: tpm, the chip description
178
 * @return: the TPM_INT_STATUS value
179
 */
180
static u8 clear_interruption(struct tpm_stm_dev *tpm_dev)
181 182
{
	u8 interrupt;
183

184 185
	I2C_READ_DATA(tpm_dev, TPM_INT_STATUS, &interrupt, 1);
	I2C_WRITE_DATA(tpm_dev, TPM_INT_STATUS, &interrupt, 1);
186
	return interrupt;
187 188 189 190 191 192 193 194
} /* clear_interruption() */

/*
 * tpm_stm_i2c_cancel, cancel is not implemented.
 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
 */
static void tpm_stm_i2c_cancel(struct tpm_chip *chip)
{
195
	struct tpm_stm_dev *tpm_dev;
196 197
	u8 data;

198
	tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
199 200

	data = TPM_STS_COMMAND_READY;
201
	I2C_WRITE_DATA(tpm_dev, TPM_STS, &data, 1);
202
} /* tpm_stm_i2c_cancel() */
203 204 205 206 207 208 209 210

/*
 * tpm_stm_spi_status return the TPM_STS register
 * @param: chip, the tpm chip description
 * @return: the TPM_STS register value.
 */
static u8 tpm_stm_i2c_status(struct tpm_chip *chip)
{
211
	struct tpm_stm_dev *tpm_dev;
212
	u8 data;
213

214
	tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
215

216
	I2C_READ_DATA(tpm_dev, TPM_STS, &data, 1);
217
	return data;
218
} /* tpm_stm_i2c_status() */
219 220 221 222 223 224 225 226 227


/*
 * check_locality if the locality is active
 * @param: chip, the tpm chip description
 * @return: the active locality or -EACCESS.
 */
static int check_locality(struct tpm_chip *chip)
{
228
	struct tpm_stm_dev *tpm_dev;
229 230 231
	u8 data;
	u8 status;

232
	tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
233

234
	status = I2C_READ_DATA(tpm_dev, TPM_ACCESS, &data, 1);
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	if (status && (data &
		(TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
		(TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
		return chip->vendor.locality;

	return -EACCES;
} /* check_locality() */

/*
 * request_locality request the TPM locality
 * @param: chip, the chip description
 * @return: the active locality or EACCESS.
 */
static int request_locality(struct tpm_chip *chip)
{
	unsigned long stop;
251
	long ret;
252
	struct tpm_stm_dev *tpm_dev;
253 254 255 256 257
	u8 data;

	if (check_locality(chip) == chip->vendor.locality)
		return chip->vendor.locality;

258 259
	tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);

260
	data = TPM_ACCESS_REQUEST_USE;
261 262
	ret = I2C_WRITE_DATA(tpm_dev, TPM_ACCESS, &data, 1);
	if (ret < 0)
263 264
		goto end;

265 266 267 268 269
	stop = jiffies + chip->vendor.timeout_a;

	/* Request locality is usually effective after the request */
	do {
		if (check_locality(chip) >= 0)
270
			return chip->vendor.locality;
271 272
		msleep(TPM_TIMEOUT);
	} while (time_before(jiffies, stop));
273
	ret = -EACCES;
274
end:
275
	return ret;
276 277 278 279 280 281 282 283
} /* request_locality() */

/*
 * release_locality release the active locality
 * @param: chip, the tpm chip description.
 */
static void release_locality(struct tpm_chip *chip)
{
284
	struct tpm_stm_dev *tpm_dev;
285 286
	u8 data;

287
	tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
288 289
	data = TPM_ACCESS_ACTIVE_LOCALITY;

290
	I2C_WRITE_DATA(tpm_dev, TPM_ACCESS, &data, 1);
291 292 293 294 295 296 297 298 299 300 301 302
}

/*
 * get_burstcount return the burstcount address 0x19 0x1A
 * @param: chip, the chip description
 * return: the burstcount.
 */
static int get_burstcount(struct tpm_chip *chip)
{
	unsigned long stop;
	int burstcnt, status;
	u8 tpm_reg, temp;
303
	struct tpm_stm_dev *tpm_dev;
304

305
	tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
306 307 308 309

	stop = jiffies + chip->vendor.timeout_d;
	do {
		tpm_reg = TPM_STS + 1;
310
		status = I2C_READ_DATA(tpm_dev, tpm_reg, &temp, 1);
311 312 313 314 315
		if (status < 0)
			goto end;

		tpm_reg = tpm_reg + 1;
		burstcnt = temp;
316
		status = I2C_READ_DATA(tpm_dev, tpm_reg, &temp, 1);
317 318 319 320 321 322 323 324 325 326 327 328 329
		if (status < 0)
			goto end;

		burstcnt |= temp << 8;
		if (burstcnt)
			return burstcnt;
		msleep(TPM_TIMEOUT);
	} while (time_before(jiffies, stop));

end:
	return -EBUSY;
} /* get_burstcount() */

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
				bool check_cancel, bool *canceled)
{
	u8 status = chip->ops->status(chip);

	*canceled = false;
	if ((status & mask) == mask)
		return true;
	if (check_cancel && chip->ops->req_canceled(chip, status)) {
		*canceled = true;
		return true;
	}
	return false;
}

/*
 * interrupt_to_status
 * @param: irq_mask, the irq mask value to wait
 * @return: the corresponding tpm_sts value
 */
static u8 interrupt_to_status(u8 irq_mask)
{
	u8 status = 0;

354
	if ((irq_mask & TPM_INTF_STS_VALID_INT) == TPM_INTF_STS_VALID_INT)
355 356 357 358 359 360 361 362 363
		status |= TPM_STS_VALID;
	if ((irq_mask & TPM_INTF_DATA_AVAIL_INT) == TPM_INTF_DATA_AVAIL_INT)
		status |= TPM_STS_DATA_AVAIL;
	if ((irq_mask & TPM_INTF_CMD_READY_INT) == TPM_INTF_CMD_READY_INT)
		status |= TPM_STS_COMMAND_READY;

	return status;
} /* status_to_interrupt() */

364 365 366 367 368 369
/*
 * wait_for_stat wait for a TPM_STS value
 * @param: chip, the tpm chip description
 * @param: mask, the value mask to wait
 * @param: timeout, the timeout
 * @param: queue, the wait queue.
370
 * @param: check_cancel, does the command can be cancelled ?
371 372 373
 * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
 */
static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
374
			wait_queue_head_t *queue, bool check_cancel)
375 376
{
	unsigned long stop;
377
	int ret;
378 379 380 381 382 383 384 385 386 387 388 389 390 391
	bool canceled = false;
	bool condition;
	u32 cur_intrs;
	u8 interrupt, status;
	struct tpm_stm_dev *tpm_dev;

	tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);

	/* check current status */
	status = tpm_stm_i2c_status(chip);
	if ((status & mask) == mask)
		return 0;

	stop = jiffies + timeout;
392

393
	if (chip->vendor.irq) {
394 395 396 397 398 399 400 401 402
		cur_intrs = tpm_dev->intrs;
		interrupt = clear_interruption(tpm_dev);
		enable_irq(chip->vendor.irq);

again:
		timeout = stop - jiffies;
		if ((long) timeout <= 0)
			return -1;

403
		ret = wait_event_interruptible_timeout(*queue,
404 405 406 407 408 409 410
					cur_intrs != tpm_dev->intrs, timeout);

		interrupt |= clear_interruption(tpm_dev);
		status = interrupt_to_status(interrupt);
		condition = wait_for_tpm_stat_cond(chip, mask,
						   check_cancel, &canceled);

411
		if (ret >= 0 && condition) {
412 413
			if (canceled)
				return -ECANCELED;
414
			return 0;
415
		}
416
		if (ret == -ERESTARTSYS && freezing(current)) {
417 418 419 420 421
			clear_thread_flag(TIF_SIGPENDING);
			goto again;
		}
		disable_irq_nosync(chip->vendor.irq);

422
	} else {
423 424
		do {
			msleep(TPM_TIMEOUT);
425
			status = chip->ops->status(chip);
426 427 428 429
			if ((status & mask) == mask)
				return 0;
		} while (time_before(jiffies, stop));
	}
430

431 432 433 434 435 436 437 438 439 440 441 442
	return -ETIME;
} /* wait_for_stat() */

/*
 * recv_data receive data
 * @param: chip, the tpm chip description
 * @param: buf, the buffer where the data are received
 * @param: count, the number of data to receive
 * @return: the number of bytes read from TPM FIFO.
 */
static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
{
443
	int size = 0, burstcnt, len, ret;
444
	struct tpm_stm_dev *tpm_dev;
445

446
	tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
447 448 449 450 451

	while (size < count &&
	       wait_for_stat(chip,
			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
			     chip->vendor.timeout_c,
452
			     &chip->vendor.read_queue, true) == 0) {
453
		burstcnt = get_burstcount(chip);
454 455
		if (burstcnt < 0)
			return burstcnt;
456
		len = min_t(int, burstcnt, count - size);
457 458 459 460
		ret = I2C_READ_DATA(tpm_dev, TPM_DATA_FIFO, buf + size, len);
		if (ret < 0)
			return ret;

461 462 463 464 465 466 467 468 469 470 471 472 473 474
		size += len;
	}
	return size;
}

/*
 * tpm_ioserirq_handler the serirq irq handler
 * @param: irq, the tpm chip description
 * @param: dev_id, the description of the chip
 * @return: the status of the handler.
 */
static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
{
	struct tpm_chip *chip = dev_id;
475
	struct tpm_stm_dev *tpm_dev;
476

477
	tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
478

479 480 481 482
	tpm_dev->intrs++;
	wake_up_interruptible(&chip->vendor.read_queue);
	disable_irq_nosync(chip->vendor.irq);

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	return IRQ_HANDLED;
} /* tpm_ioserirq_handler() */


/*
 * tpm_stm_i2c_send send TPM commands through the I2C bus.
 *
 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
 * @param: buf,	the buffer to send.
 * @param: count, the number of bytes to send.
 * @return: In case of success the number of bytes sent.
 *			In other case, a < 0 value describing the issue.
 */
static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,
			    size_t len)
{
499 500
	u32 status, i, size;
	int burstcnt = 0;
K
Kent Yoder 已提交
501
	int ret;
502 503
	u8 data;
	struct i2c_client *client;
504
	struct tpm_stm_dev *tpm_dev;
505

506
	if (!chip)
507 508 509 510
		return -EBUSY;
	if (len < TPM_HEADER_SIZE)
		return -EBUSY;

511 512
	tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
	client = tpm_dev->client;
513 514 515 516 517 518 519 520 521 522 523 524

	client->flags = 0;

	ret = request_locality(chip);
	if (ret < 0)
		return ret;

	status = tpm_stm_i2c_status(chip);
	if ((status & TPM_STS_COMMAND_READY) == 0) {
		tpm_stm_i2c_cancel(chip);
		if (wait_for_stat
		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
525
		     &chip->vendor.read_queue, false) < 0) {
526 527 528 529 530
			ret = -ETIME;
			goto out_err;
		}
	}

531
	for (i = 0; i < len - 1;) {
532
		burstcnt = get_burstcount(chip);
533 534
		if (burstcnt < 0)
			return burstcnt;
535
		size = min_t(int, len - i - 1, burstcnt);
536
		ret = I2C_WRITE_DATA(tpm_dev, TPM_DATA_FIFO, buf + i, size);
537 538 539 540 541 542 543 544 545 546 547 548
		if (ret < 0)
			goto out_err;

		i += size;
	}

	status = tpm_stm_i2c_status(chip);
	if ((status & TPM_STS_DATA_EXPECT) == 0) {
		ret = -EIO;
		goto out_err;
	}

549
	ret = I2C_WRITE_DATA(tpm_dev, TPM_DATA_FIFO, buf + len - 1, 1);
550 551 552 553 554 555 556 557 558 559
	if (ret < 0)
		goto out_err;

	status = tpm_stm_i2c_status(chip);
	if ((status & TPM_STS_DATA_EXPECT) != 0) {
		ret = -EIO;
		goto out_err;
	}

	data = TPM_STS_GO;
560
	I2C_WRITE_DATA(tpm_dev, TPM_STS, &data, 1);
561 562 563 564 565 566 567 568 569 570 571 572 573 574

	return len;
out_err:
	tpm_stm_i2c_cancel(chip);
	release_locality(chip);
	return ret;
}

/*
 * tpm_stm_i2c_recv received TPM response through the I2C bus.
 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
 * @param: buf,	the buffer to store datas.
 * @param: count, the number of bytes to send.
 * @return: In case of success the number of bytes received.
575
 *	    In other case, a < 0 value describing the issue.
576 577 578 579 580 581 582
 */
static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,
			    size_t count)
{
	int size = 0;
	int expected;

583
	if (!chip)
584 585 586 587 588 589 590 591 592
		return -EBUSY;

	if (count < TPM_HEADER_SIZE) {
		size = -EIO;
		goto out;
	}

	size = recv_data(chip, buf, TPM_HEADER_SIZE);
	if (size < TPM_HEADER_SIZE) {
593
		dev_err(chip->pdev, "Unable to read header\n");
594 595 596
		goto out;
	}

597
	expected = be32_to_cpu(*(__be32 *)(buf + 2));
598 599 600 601 602 603
	if (expected > count) {
		size = -EIO;
		goto out;
	}

	size += recv_data(chip, &buf[TPM_HEADER_SIZE],
604
			expected - TPM_HEADER_SIZE);
605
	if (size < expected) {
606
		dev_err(chip->pdev, "Unable to read remainder of result\n");
607 608 609 610 611
		size = -ETIME;
		goto out;
	}

out:
612
	chip->ops->cancel(chip);
613 614 615 616
	release_locality(chip);
	return size;
}

617
static bool tpm_stm_i2c_req_canceled(struct tpm_chip *chip, u8 status)
618
{
619
	return (status == TPM_STS_COMMAND_READY);
620 621
}

622
static const struct tpm_class_ops st_i2c_tpm = {
623 624 625 626 627 628
	.send = tpm_stm_i2c_send,
	.recv = tpm_stm_i2c_recv,
	.cancel = tpm_stm_i2c_cancel,
	.status = tpm_stm_i2c_status,
	.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
	.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
629
	.req_canceled = tpm_stm_i2c_req_canceled,
630 631
};

632 633 634 635 636 637 638 639 640 641 642
#ifdef CONFIG_OF
static int tpm_stm_i2c_of_request_resources(struct tpm_chip *chip)
{
	struct device_node *pp;
	struct tpm_stm_dev *tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
	struct i2c_client *client = tpm_dev->client;
	int gpio;
	int ret;

	pp = client->dev.of_node;
	if (!pp) {
643
		dev_err(chip->pdev, "No platform data\n");
644 645 646 647 648 649
		return -ENODEV;
	}

	/* Get GPIO from device tree */
	gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0);
	if (gpio < 0) {
650
		dev_err(chip->pdev, "Failed to retrieve lpcpd-gpios from dts.\n");
651 652 653 654 655 656 657 658 659 660 661 662
		tpm_dev->io_lpcpd = -1;
		/*
		 * lpcpd pin is not specified. This is not an issue as
		 * power management can be also managed by TPM specific
		 * commands. So leave with a success status code.
		 */
		return 0;
	}
	/* GPIO request and configuration */
	ret = devm_gpio_request_one(&client->dev, gpio,
			GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD");
	if (ret) {
663
		dev_err(chip->pdev, "Failed to request lpcpd pin\n");
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
		return -ENODEV;
	}
	tpm_dev->io_lpcpd = gpio;

	return 0;
}
#else
static int tpm_stm_i2c_of_request_resources(struct tpm_chip *chip)
{
	return -ENODEV;
}
#endif

static int tpm_stm_i2c_request_resources(struct i2c_client *client,
					 struct tpm_chip *chip)
{
	struct st33zp24_platform_data *pdata;
	struct tpm_stm_dev *tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
	int ret;

	pdata = client->dev.platform_data;
685
	if (!pdata) {
686
		dev_err(chip->pdev, "No platform data\n");
687
		return -ENODEV;
688
	}
689

690 691 692 693 694 695 696 697
	/* store for late use */
	tpm_dev->io_lpcpd = pdata->io_lpcpd;

	if (gpio_is_valid(pdata->io_lpcpd)) {
		ret = devm_gpio_request_one(&client->dev,
				pdata->io_lpcpd, GPIOF_OUT_INIT_HIGH,
				"TPM IO_LPCPD");
		if (ret) {
698
			dev_err(chip->pdev, "%s : reset gpio_request failed\n",
699 700 701 702 703 704 705
				__FILE__);
			return ret;
		}
	}

	return 0;
}
706 707

/*
708
 * tpm_stm_i2c_probe initialize the TPM device
709 710 711 712 713 714
 * @param: client, the i2c_client drescription (TPM I2C description).
 * @param: id, the i2c_device_id struct.
 * @return: 0 in case of success.
 *	 -1 in other case.
 */
static int
715
tpm_stm_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
716
{
717
	int ret;
718
	u8 intmask = 0;
719 720
	struct tpm_chip *chip;
	struct st33zp24_platform_data *platform_data;
721
	struct tpm_stm_dev *tpm_dev;
722

723
	if (!client) {
724
		pr_info("%s: i2c client is NULL. Device not accessible.\n",
725
			__func__);
726
		return -ENODEV;
727 728 729 730
	}

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
		dev_info(&client->dev, "client not i2c capable\n");
731
		return -ENODEV;
732 733
	}

734 735
	tpm_dev = devm_kzalloc(&client->dev, sizeof(struct tpm_stm_dev),
			       GFP_KERNEL);
736 737
	if (!tpm_dev)
		return -ENOMEM;
738

739 740 741
	chip = tpmm_chip_alloc(&client->dev, &st_i2c_tpm);
	if (IS_ERR(chip))
		return PTR_ERR(chip);
742

743 744
	TPM_VPRIV(chip) = tpm_dev;
	tpm_dev->client = client;
745

746 747 748 749 750 751 752 753 754 755 756
	platform_data = client->dev.platform_data;
	if (!platform_data && client->dev.of_node) {
		ret = tpm_stm_i2c_of_request_resources(chip);
		if (ret)
			goto _tpm_clean_answer;
	} else if (platform_data) {
		ret = tpm_stm_i2c_request_resources(client, chip);
		if (ret)
			goto _tpm_clean_answer;
	}

757 758 759 760 761 762 763
	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);

	chip->vendor.locality = LOCALITY0;

764
	if (client->irq) {
765 766 767 768
		/* INTERRUPT Setup */
		init_waitqueue_head(&chip->vendor.read_queue);
		tpm_dev->intrs = 0;

769
		if (request_locality(chip) != LOCALITY0) {
770
			ret = -ENODEV;
771
			goto _tpm_clean_answer;
772 773
		}

774
		clear_interruption(tpm_dev);
775 776
		ret = devm_request_irq(&client->dev, client->irq,
				tpm_ioserirq_handler,
777 778
				IRQF_TRIGGER_HIGH,
				"TPM SERIRQ management", chip);
779
		if (ret < 0) {
780
			dev_err(chip->pdev, "TPM SERIRQ signals %d not available\n",
781
				client->irq);
782
			goto _tpm_clean_answer;
783 784 785 786 787 788
		}

		intmask |= TPM_INTF_CMD_READY_INT
			|  TPM_INTF_STS_VALID_INT
			|  TPM_INTF_DATA_AVAIL_INT;

789 790
		ret = I2C_WRITE_DATA(tpm_dev, TPM_INT_ENABLE, &intmask, 1);
		if (ret < 0)
791
			goto _tpm_clean_answer;
792 793

		intmask = TPM_GLOBAL_INT_ENABLE;
794
		ret = I2C_WRITE_DATA(tpm_dev, (TPM_INT_ENABLE + 3),
795
				     &intmask, 1);
796
		if (ret < 0)
797
			goto _tpm_clean_answer;
798

799
		chip->vendor.irq = client->irq;
800

801 802
		disable_irq_nosync(chip->vendor.irq);

803 804 805 806
		tpm_gen_interrupt(chip);
	}

	tpm_get_timeouts(chip);
807
	tpm_do_selftest(chip);
808

809
	return tpm_chip_register(chip);
810
_tpm_clean_answer:
811
	dev_info(chip->pdev, "TPM I2C initialisation fail\n");
812
	return ret;
813 814 815
}

/*
816
 * tpm_stm_i2c_remove remove the TPM device
817
 * @param: client, the i2c_client description (TPM I2C description).
818 819
 * @return: 0 in case of success.
 */
820
static int tpm_stm_i2c_remove(struct i2c_client *client)
821
{
822 823
	struct tpm_chip *chip =
		(struct tpm_chip *) i2c_get_clientdata(client);
824

825
	if (chip)
826
		tpm_chip_unregister(chip);
827

828 829 830
	return 0;
}

831
#ifdef CONFIG_PM_SLEEP
832
/*
833
 * tpm_stm_i2c_pm_suspend suspend the TPM device
834 835 836 837
 * @param: client, the i2c_client drescription (TPM I2C description).
 * @param: mesg, the power management message.
 * @return: 0 in case of success.
 */
838
static int tpm_stm_i2c_pm_suspend(struct device *dev)
839
{
840
	struct st33zp24_platform_data *pin_infos = dev->platform_data;
841 842
	int ret = 0;

843
	if (gpio_is_valid(pin_infos->io_lpcpd))
844
		gpio_set_value(pin_infos->io_lpcpd, 0);
845
	else
846
		ret = tpm_pm_suspend(dev);
847

848
	return ret;
849
} /* tpm_stm_i2c_suspend() */
850 851

/*
852
 * tpm_stm_i2c_pm_resume resume the TPM device
853 854 855
 * @param: client, the i2c_client drescription (TPM I2C description).
 * @return: 0 in case of success.
 */
856
static int tpm_stm_i2c_pm_resume(struct device *dev)
857
{
858 859
	struct tpm_chip *chip = dev_get_drvdata(dev);
	struct st33zp24_platform_data *pin_infos = dev->platform_data;
860 861 862

	int ret = 0;

863
	if (gpio_is_valid(pin_infos->io_lpcpd)) {
864
		gpio_set_value(pin_infos->io_lpcpd, 1);
865 866 867
		ret = wait_for_stat(chip,
				TPM_STS_VALID, chip->vendor.timeout_b,
				&chip->vendor.read_queue, false);
868 869 870 871
	} else {
		ret = tpm_pm_resume(dev);
		if (!ret)
			tpm_do_selftest(chip);
872 873
	}
	return ret;
874
} /* tpm_stm_i2c_pm_resume() */
875
#endif
876

877
static const struct i2c_device_id tpm_stm_i2c_id[] = {
878 879 880
	{TPM_ST33_I2C, 0},
	{}
};
881
MODULE_DEVICE_TABLE(i2c, tpm_stm_i2c_id);
882 883 884 885 886 887 888 889 890

#ifdef CONFIG_OF
static const struct of_device_id of_st33zp24_i2c_match[] = {
	{ .compatible = "st,st33zp24-i2c", },
	{}
};
MODULE_DEVICE_TABLE(of, of_st33zp24_i2c_match);
#endif

891
static SIMPLE_DEV_PM_OPS(tpm_stm_i2c_ops, tpm_stm_i2c_pm_suspend,
892 893
			 tpm_stm_i2c_pm_resume);

894
static struct i2c_driver tpm_stm_i2c_driver = {
895
	.driver = {
896 897 898 899
		.owner = THIS_MODULE,
		.name = TPM_ST33_I2C,
		.pm = &tpm_stm_i2c_ops,
		.of_match_table = of_match_ptr(of_st33zp24_i2c_match),
900
	},
901 902 903
	.probe = tpm_stm_i2c_probe,
	.remove = tpm_stm_i2c_remove,
	.id_table = tpm_stm_i2c_id
904 905
};

906
module_i2c_driver(tpm_stm_i2c_driver);
907 908 909

MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
MODULE_DESCRIPTION("STM TPM I2C ST33 Driver");
910
MODULE_VERSION("1.2.1");
K
Kent Yoder 已提交
911
MODULE_LICENSE("GPL");