Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / char / tpm / tpm_tis_i2c_cr50.c
blob3b55a7b05c46a91d963530cf2bb9eb5792b35a46
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2020 Google Inc.
5 * Based on Infineon TPM driver by Peter Huewe.
7 * cr50 is a firmware for H1 secure modules that requires special
8 * handling for the I2C interface.
10 * - Use an interrupt for transaction status instead of hardcoded delays.
11 * - Must use write+wait+read read protocol.
12 * - All 4 bytes of status register must be read/written at once.
13 * - Burst count max is 63 bytes, and burst count behaves slightly differently
14 * than other I2C TPMs.
15 * - When reading from FIFO the full burstcnt must be read instead of just
16 * reading header and determining the remainder.
19 #include <linux/acpi.h>
20 #include <linux/bug.h>
21 #include <linux/completion.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/pm.h>
26 #include <linux/slab.h>
27 #include <linux/wait.h>
29 #include "tpm_tis_core.h"
31 #define TPM_CR50_MAX_BUFSIZE 64
32 #define TPM_CR50_TIMEOUT_SHORT_MS 2 /* Short timeout during transactions */
33 #define TPM_CR50_TIMEOUT_NOIRQ_MS 20 /* Timeout for TPM ready without IRQ */
34 #define TPM_CR50_I2C_DID_VID 0x00281ae0L /* Device and vendor ID for Cr50 H1 */
35 #define TPM_TI50_DT_I2C_DID_VID 0x504a6666L /* Device and vendor ID for Ti50 DT */
36 #define TPM_TI50_OT_I2C_DID_VID 0x50666666L /* Device and vendor ID for TI50 OT */
37 #define TPM_CR50_I2C_MAX_RETRIES 3 /* Max retries due to I2C errors */
38 #define TPM_CR50_I2C_RETRY_DELAY_LO 55 /* Min usecs between retries on I2C */
39 #define TPM_CR50_I2C_RETRY_DELAY_HI 65 /* Max usecs between retries on I2C */
40 #define TPM_CR50_I2C_DEFAULT_LOC 0
42 #define TPM_I2C_ACCESS(l) (0x0000 | ((l) << 4))
43 #define TPM_I2C_STS(l) (0x0001 | ((l) << 4))
44 #define TPM_I2C_DATA_FIFO(l) (0x0005 | ((l) << 4))
45 #define TPM_I2C_DID_VID(l) (0x0006 | ((l) << 4))
47 /**
48 * struct tpm_i2c_cr50_priv_data - Driver private data.
49 * @irq: Irq number used for this chip.
50 * If irq <= 0, then a fixed timeout is used instead of waiting for irq.
51 * @tpm_ready: Struct used by irq handler to signal R/W readiness.
52 * @buf: Buffer used for i2c writes, with i2c address prepended to content.
54 * Private driver struct used by kernel threads and interrupt context.
56 struct tpm_i2c_cr50_priv_data {
57 int irq;
58 struct completion tpm_ready;
59 u8 buf[TPM_CR50_MAX_BUFSIZE];
62 /**
63 * tpm_cr50_i2c_int_handler() - cr50 interrupt handler.
64 * @dummy: Unused parameter.
65 * @tpm_info: TPM chip information.
67 * The cr50 interrupt handler signals waiting threads that the
68 * interrupt has been asserted. It does not do any interrupt triggered
69 * processing but is instead used to avoid fixed delays.
71 * Return:
72 * IRQ_HANDLED signifies irq was handled by this device.
74 static irqreturn_t tpm_cr50_i2c_int_handler(int dummy, void *tpm_info)
76 struct tpm_chip *chip = tpm_info;
77 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
79 complete(&priv->tpm_ready);
81 return IRQ_HANDLED;
84 /**
85 * tpm_cr50_i2c_wait_tpm_ready() - Wait for tpm to signal ready.
86 * @chip: A TPM chip.
88 * Wait for completion interrupt if available, otherwise use a fixed
89 * delay for the TPM to be ready.
91 * Return:
92 * - 0: Success.
93 * - -errno: A POSIX error code.
95 static int tpm_cr50_i2c_wait_tpm_ready(struct tpm_chip *chip)
97 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
99 /* Use a safe fixed delay if interrupt is not supported */
100 if (priv->irq <= 0) {
101 msleep(TPM_CR50_TIMEOUT_NOIRQ_MS);
102 return 0;
105 /* Wait for interrupt to indicate TPM is ready to respond */
106 if (!wait_for_completion_timeout(&priv->tpm_ready, chip->timeout_a)) {
107 dev_warn(&chip->dev, "Timeout waiting for TPM ready\n");
108 return -ETIMEDOUT;
111 return 0;
115 * tpm_cr50_i2c_enable_tpm_irq() - Enable TPM irq.
116 * @chip: A TPM chip.
118 static void tpm_cr50_i2c_enable_tpm_irq(struct tpm_chip *chip)
120 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
122 if (priv->irq > 0) {
123 reinit_completion(&priv->tpm_ready);
124 enable_irq(priv->irq);
129 * tpm_cr50_i2c_disable_tpm_irq() - Disable TPM irq.
130 * @chip: A TPM chip.
132 static void tpm_cr50_i2c_disable_tpm_irq(struct tpm_chip *chip)
134 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
136 if (priv->irq > 0)
137 disable_irq(priv->irq);
141 * tpm_cr50_i2c_transfer_message() - Transfer a message over i2c.
142 * @dev: Device information.
143 * @adapter: I2C adapter.
144 * @msg: Message to transfer.
146 * Call unlocked i2c transfer routine with the provided parameters and
147 * retry in case of bus errors.
149 * Return:
150 * - 0: Success.
151 * - -errno: A POSIX error code.
153 static int tpm_cr50_i2c_transfer_message(struct device *dev,
154 struct i2c_adapter *adapter,
155 struct i2c_msg *msg)
157 unsigned int try;
158 int rc;
160 for (try = 0; try < TPM_CR50_I2C_MAX_RETRIES; try++) {
161 rc = __i2c_transfer(adapter, msg, 1);
162 if (rc == 1)
163 return 0; /* Successfully transferred the message */
164 if (try)
165 dev_warn(dev, "i2c transfer failed (attempt %d/%d): %d\n",
166 try + 1, TPM_CR50_I2C_MAX_RETRIES, rc);
167 usleep_range(TPM_CR50_I2C_RETRY_DELAY_LO, TPM_CR50_I2C_RETRY_DELAY_HI);
170 /* No i2c message transferred */
171 return -EIO;
175 * tpm_cr50_i2c_read() - Read from TPM register.
176 * @chip: A TPM chip.
177 * @addr: Register address to read from.
178 * @buffer: Read destination, provided by caller.
179 * @len: Number of bytes to read.
181 * Sends the register address byte to the TPM, then waits until TPM
182 * is ready via interrupt signal or timeout expiration, then 'len'
183 * bytes are read from TPM response into the provided 'buffer'.
185 * Return:
186 * - 0: Success.
187 * - -errno: A POSIX error code.
189 static int tpm_cr50_i2c_read(struct tpm_chip *chip, u8 addr, u8 *buffer, size_t len)
191 struct i2c_client *client = to_i2c_client(chip->dev.parent);
192 struct i2c_msg msg_reg_addr = {
193 .addr = client->addr,
194 .len = 1,
195 .buf = &addr
197 struct i2c_msg msg_response = {
198 .addr = client->addr,
199 .flags = I2C_M_RD,
200 .len = len,
201 .buf = buffer
203 int rc;
205 /* Prepare for completion interrupt */
206 tpm_cr50_i2c_enable_tpm_irq(chip);
208 /* Send the register address byte to the TPM */
209 rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg_reg_addr);
210 if (rc < 0)
211 goto out;
213 /* Wait for TPM to be ready with response data */
214 rc = tpm_cr50_i2c_wait_tpm_ready(chip);
215 if (rc < 0)
216 goto out;
218 /* Read response data from the TPM */
219 rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg_response);
221 out:
222 tpm_cr50_i2c_disable_tpm_irq(chip);
224 if (rc < 0)
225 return rc;
227 return 0;
231 * tpm_cr50_i2c_write()- Write to TPM register.
232 * @chip: A TPM chip.
233 * @addr: Register address to write to.
234 * @buffer: Data to write.
235 * @len: Number of bytes to write.
237 * The provided address is prepended to the data in 'buffer', the
238 * combined address+data is sent to the TPM, then wait for TPM to
239 * indicate it is done writing.
241 * Return:
242 * - 0: Success.
243 * - -errno: A POSIX error code.
245 static int tpm_cr50_i2c_write(struct tpm_chip *chip, u8 addr, u8 *buffer,
246 size_t len)
248 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
249 struct i2c_client *client = to_i2c_client(chip->dev.parent);
250 struct i2c_msg msg = {
251 .addr = client->addr,
252 .len = len + 1,
253 .buf = priv->buf
255 int rc;
257 if (len > TPM_CR50_MAX_BUFSIZE - 1)
258 return -EINVAL;
260 /* Prepend the 'register address' to the buffer */
261 priv->buf[0] = addr;
262 memcpy(priv->buf + 1, buffer, len);
264 /* Prepare for completion interrupt */
265 tpm_cr50_i2c_enable_tpm_irq(chip);
267 /* Send write request buffer with address */
268 rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg);
269 if (rc < 0)
270 goto out;
272 /* Wait for TPM to be ready, ignore timeout */
273 tpm_cr50_i2c_wait_tpm_ready(chip);
275 out:
276 tpm_cr50_i2c_disable_tpm_irq(chip);
278 if (rc < 0)
279 return rc;
281 return 0;
285 * tpm_cr50_check_locality() - Verify if required TPM locality is active.
286 * @chip: A TPM chip.
287 * @loc: Locality to be verified
289 * Return:
290 * - loc: Success.
291 * - -errno: A POSIX error code.
293 static int tpm_cr50_check_locality(struct tpm_chip *chip, int loc)
295 u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
296 u8 buf;
297 int rc;
299 rc = tpm_cr50_i2c_read(chip, TPM_I2C_ACCESS(loc), &buf, sizeof(buf));
300 if (rc < 0)
301 return rc;
303 if ((buf & mask) == mask)
304 return loc;
306 return -EIO;
310 * tpm_cr50_release_locality() - Release TPM locality.
311 * @chip: A TPM chip.
312 * @loc: Locality to be released
314 * Return:
315 * - 0: Success.
316 * - -errno: A POSIX error code.
318 static int tpm_cr50_release_locality(struct tpm_chip *chip, int loc)
320 struct i2c_client *client = to_i2c_client(chip->dev.parent);
321 u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING;
322 u8 addr = TPM_I2C_ACCESS(loc);
323 u8 buf;
324 int rc;
326 rc = tpm_cr50_i2c_read(chip, addr, &buf, sizeof(buf));
327 if (rc < 0)
328 goto unlock_out;
330 if ((buf & mask) == mask) {
331 buf = TPM_ACCESS_ACTIVE_LOCALITY;
332 rc = tpm_cr50_i2c_write(chip, addr, &buf, sizeof(buf));
335 unlock_out:
336 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
337 return rc;
341 * tpm_cr50_request_locality() - Request TPM locality.
342 * @chip: A TPM chip.
343 * @loc: Locality to be requested.
345 * Return:
346 * - loc: Success.
347 * - -errno: A POSIX error code.
349 static int tpm_cr50_request_locality(struct tpm_chip *chip, int loc)
351 struct i2c_client *client = to_i2c_client(chip->dev.parent);
352 u8 buf = TPM_ACCESS_REQUEST_USE;
353 unsigned long stop;
354 int rc;
356 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
358 if (tpm_cr50_check_locality(chip, loc) == loc)
359 return loc;
361 rc = tpm_cr50_i2c_write(chip, TPM_I2C_ACCESS(loc), &buf, sizeof(buf));
362 if (rc < 0)
363 goto unlock_out;
365 stop = jiffies + chip->timeout_a;
366 do {
367 if (tpm_cr50_check_locality(chip, loc) == loc)
368 return loc;
370 msleep(TPM_CR50_TIMEOUT_SHORT_MS);
371 } while (time_before(jiffies, stop));
373 rc = -ETIMEDOUT;
375 unlock_out:
376 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
377 return rc;
381 * tpm_cr50_i2c_tis_status() - Read cr50 tis status.
382 * @chip: A TPM chip.
384 * cr50 requires all 4 bytes of status register to be read.
386 * Return:
387 * TPM status byte.
389 static u8 tpm_cr50_i2c_tis_status(struct tpm_chip *chip)
391 u8 buf[4];
393 if (tpm_cr50_i2c_read(chip, TPM_I2C_STS(chip->locality), buf, sizeof(buf)) < 0)
394 return 0;
396 return buf[0];
400 * tpm_cr50_i2c_tis_set_ready() - Set status register to ready.
401 * @chip: A TPM chip.
403 * cr50 requires all 4 bytes of status register to be written.
405 static void tpm_cr50_i2c_tis_set_ready(struct tpm_chip *chip)
407 u8 buf[4] = { TPM_STS_COMMAND_READY };
409 tpm_cr50_i2c_write(chip, TPM_I2C_STS(chip->locality), buf, sizeof(buf));
410 msleep(TPM_CR50_TIMEOUT_SHORT_MS);
414 * tpm_cr50_i2c_get_burst_and_status() - Get burst count and status.
415 * @chip: A TPM chip.
416 * @mask: Status mask.
417 * @burst: Return value for burst.
418 * @status: Return value for status.
420 * cr50 uses bytes 3:2 of status register for burst count and
421 * all 4 bytes must be read.
423 * Return:
424 * - 0: Success.
425 * - -errno: A POSIX error code.
427 static int tpm_cr50_i2c_get_burst_and_status(struct tpm_chip *chip, u8 mask,
428 size_t *burst, u32 *status)
430 unsigned long stop;
431 u8 buf[4];
433 *status = 0;
435 /* wait for burstcount */
436 stop = jiffies + chip->timeout_b;
438 do {
439 if (tpm_cr50_i2c_read(chip, TPM_I2C_STS(chip->locality), buf, sizeof(buf)) < 0) {
440 msleep(TPM_CR50_TIMEOUT_SHORT_MS);
441 continue;
444 *status = *buf;
445 *burst = le16_to_cpup((__le16 *)(buf + 1));
447 if ((*status & mask) == mask &&
448 *burst > 0 && *burst <= TPM_CR50_MAX_BUFSIZE - 1)
449 return 0;
451 msleep(TPM_CR50_TIMEOUT_SHORT_MS);
452 } while (time_before(jiffies, stop));
454 dev_err(&chip->dev, "Timeout reading burst and status\n");
455 return -ETIMEDOUT;
459 * tpm_cr50_i2c_tis_recv() - TPM reception callback.
460 * @chip: A TPM chip.
461 * @buf: Reception buffer.
462 * @buf_len: Buffer length to read.
464 * Return:
465 * - >= 0: Number of read bytes.
466 * - -errno: A POSIX error code.
468 static int tpm_cr50_i2c_tis_recv(struct tpm_chip *chip, u8 *buf, size_t buf_len)
471 u8 mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
472 size_t burstcnt, cur, len, expected;
473 u8 addr = TPM_I2C_DATA_FIFO(chip->locality);
474 u32 status;
475 int rc;
477 if (buf_len < TPM_HEADER_SIZE)
478 return -EINVAL;
480 rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status);
481 if (rc < 0)
482 goto out_err;
484 if (burstcnt > buf_len || burstcnt < TPM_HEADER_SIZE) {
485 dev_err(&chip->dev,
486 "Unexpected burstcnt: %zu (max=%zu, min=%d)\n",
487 burstcnt, buf_len, TPM_HEADER_SIZE);
488 rc = -EIO;
489 goto out_err;
492 /* Read first chunk of burstcnt bytes */
493 rc = tpm_cr50_i2c_read(chip, addr, buf, burstcnt);
494 if (rc < 0) {
495 dev_err(&chip->dev, "Read of first chunk failed\n");
496 goto out_err;
499 /* Determine expected data in the return buffer */
500 expected = be32_to_cpup((__be32 *)(buf + 2));
501 if (expected > buf_len) {
502 dev_err(&chip->dev, "Buffer too small to receive i2c data\n");
503 rc = -E2BIG;
504 goto out_err;
507 /* Now read the rest of the data */
508 cur = burstcnt;
509 while (cur < expected) {
510 /* Read updated burst count and check status */
511 rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status);
512 if (rc < 0)
513 goto out_err;
515 len = min_t(size_t, burstcnt, expected - cur);
516 rc = tpm_cr50_i2c_read(chip, addr, buf + cur, len);
517 if (rc < 0) {
518 dev_err(&chip->dev, "Read failed\n");
519 goto out_err;
522 cur += len;
525 /* Ensure TPM is done reading data */
526 rc = tpm_cr50_i2c_get_burst_and_status(chip, TPM_STS_VALID, &burstcnt, &status);
527 if (rc < 0)
528 goto out_err;
529 if (status & TPM_STS_DATA_AVAIL) {
530 dev_err(&chip->dev, "Data still available\n");
531 rc = -EIO;
532 goto out_err;
535 return cur;
537 out_err:
538 /* Abort current transaction if still pending */
539 if (tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
540 tpm_cr50_i2c_tis_set_ready(chip);
542 return rc;
546 * tpm_cr50_i2c_tis_send() - TPM transmission callback.
547 * @chip: A TPM chip.
548 * @buf: Buffer to send.
549 * @len: Buffer length.
551 * Return:
552 * - 0: Success.
553 * - -errno: A POSIX error code.
555 static int tpm_cr50_i2c_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
557 size_t burstcnt, limit, sent = 0;
558 u8 tpm_go[4] = { TPM_STS_GO };
559 unsigned long stop;
560 u32 status;
561 int rc;
563 /* Wait until TPM is ready for a command */
564 stop = jiffies + chip->timeout_b;
565 while (!(tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
566 if (time_after(jiffies, stop)) {
567 rc = -ETIMEDOUT;
568 goto out_err;
571 tpm_cr50_i2c_tis_set_ready(chip);
574 while (len > 0) {
575 u8 mask = TPM_STS_VALID;
577 /* Wait for data if this is not the first chunk */
578 if (sent > 0)
579 mask |= TPM_STS_DATA_EXPECT;
581 /* Read burst count and check status */
582 rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status);
583 if (rc < 0)
584 goto out_err;
587 * Use burstcnt - 1 to account for the address byte
588 * that is inserted by tpm_cr50_i2c_write()
590 limit = min_t(size_t, burstcnt - 1, len);
591 rc = tpm_cr50_i2c_write(chip, TPM_I2C_DATA_FIFO(chip->locality),
592 &buf[sent], limit);
593 if (rc < 0) {
594 dev_err(&chip->dev, "Write failed\n");
595 goto out_err;
598 sent += limit;
599 len -= limit;
602 /* Ensure TPM is not expecting more data */
603 rc = tpm_cr50_i2c_get_burst_and_status(chip, TPM_STS_VALID, &burstcnt, &status);
604 if (rc < 0)
605 goto out_err;
606 if (status & TPM_STS_DATA_EXPECT) {
607 dev_err(&chip->dev, "Data still expected\n");
608 rc = -EIO;
609 goto out_err;
612 /* Start the TPM command */
613 rc = tpm_cr50_i2c_write(chip, TPM_I2C_STS(chip->locality), tpm_go,
614 sizeof(tpm_go));
615 if (rc < 0) {
616 dev_err(&chip->dev, "Start command failed\n");
617 goto out_err;
619 return 0;
621 out_err:
622 /* Abort current transaction if still pending */
623 if (tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
624 tpm_cr50_i2c_tis_set_ready(chip);
626 return rc;
630 * tpm_cr50_i2c_req_canceled() - Callback to notify a request cancel.
631 * @chip: A TPM chip.
632 * @status: Status given by the cancel callback.
634 * Return:
635 * True if command is ready, False otherwise.
637 static bool tpm_cr50_i2c_req_canceled(struct tpm_chip *chip, u8 status)
639 return status == TPM_STS_COMMAND_READY;
642 static bool tpm_cr50_i2c_is_firmware_power_managed(struct device *dev)
644 u8 val;
645 int ret;
647 /* This flag should default true when the device property is not present */
648 ret = device_property_read_u8(dev, "firmware-power-managed", &val);
649 if (ret)
650 return true;
652 return val;
655 static const struct tpm_class_ops cr50_i2c = {
656 .flags = TPM_OPS_AUTO_STARTUP,
657 .status = &tpm_cr50_i2c_tis_status,
658 .recv = &tpm_cr50_i2c_tis_recv,
659 .send = &tpm_cr50_i2c_tis_send,
660 .cancel = &tpm_cr50_i2c_tis_set_ready,
661 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
662 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
663 .req_canceled = &tpm_cr50_i2c_req_canceled,
664 .request_locality = &tpm_cr50_request_locality,
665 .relinquish_locality = &tpm_cr50_release_locality,
668 #ifdef CONFIG_ACPI
669 static const struct acpi_device_id cr50_i2c_acpi_id[] = {
670 { "GOOG0005", 0 },
673 MODULE_DEVICE_TABLE(acpi, cr50_i2c_acpi_id);
674 #endif
676 #ifdef CONFIG_OF
677 static const struct of_device_id of_cr50_i2c_match[] = {
678 { .compatible = "google,cr50", },
681 MODULE_DEVICE_TABLE(of, of_cr50_i2c_match);
682 #endif
685 * tpm_cr50_vid_to_name() - Maps VID to name.
686 * @vendor: Vendor identifier to map to name
688 * Return:
689 * A valid string for the vendor or empty string
691 static const char *tpm_cr50_vid_to_name(u32 vendor)
693 switch (vendor) {
694 case TPM_CR50_I2C_DID_VID:
695 return "cr50";
696 case TPM_TI50_DT_I2C_DID_VID:
697 return "ti50 DT";
698 case TPM_TI50_OT_I2C_DID_VID:
699 return "ti50 OT";
700 default:
701 return "unknown";
706 * tpm_cr50_i2c_probe() - Driver probe function.
707 * @client: I2C client information.
709 * Return:
710 * - 0: Success.
711 * - -errno: A POSIX error code.
713 static int tpm_cr50_i2c_probe(struct i2c_client *client)
715 struct tpm_i2c_cr50_priv_data *priv;
716 struct device *dev = &client->dev;
717 struct tpm_chip *chip;
718 u32 vendor;
719 u8 buf[4];
720 int rc;
721 int loc;
723 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
724 return -ENODEV;
726 chip = tpmm_chip_alloc(dev, &cr50_i2c);
727 if (IS_ERR(chip))
728 return PTR_ERR(chip);
730 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
731 if (!priv)
732 return -ENOMEM;
734 /* cr50 is a TPM 2.0 chip */
735 chip->flags |= TPM_CHIP_FLAG_TPM2;
736 if (tpm_cr50_i2c_is_firmware_power_managed(dev))
737 chip->flags |= TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED;
739 /* Default timeouts */
740 chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
741 chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
742 chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
743 chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
745 dev_set_drvdata(&chip->dev, priv);
746 init_completion(&priv->tpm_ready);
748 if (client->irq > 0) {
749 rc = devm_request_irq(dev, client->irq, tpm_cr50_i2c_int_handler,
750 IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
751 IRQF_NO_AUTOEN,
752 dev->driver->name, chip);
753 if (rc < 0) {
754 dev_err(dev, "Failed to probe IRQ %d\n", client->irq);
755 return rc;
758 priv->irq = client->irq;
759 } else {
760 dev_warn(dev, "No IRQ, will use %ums delay for TPM ready\n",
761 TPM_CR50_TIMEOUT_NOIRQ_MS);
764 loc = tpm_cr50_request_locality(chip, TPM_CR50_I2C_DEFAULT_LOC);
765 if (loc < 0) {
766 dev_err(dev, "Could not request locality\n");
767 return loc;
770 /* Read four bytes from DID_VID register */
771 rc = tpm_cr50_i2c_read(chip, TPM_I2C_DID_VID(loc), buf, sizeof(buf));
772 if (rc < 0) {
773 dev_err(dev, "Could not read vendor id\n");
774 if (tpm_cr50_release_locality(chip, loc))
775 dev_err(dev, "Could not release locality\n");
776 return rc;
779 rc = tpm_cr50_release_locality(chip, loc);
780 if (rc) {
781 dev_err(dev, "Could not release locality\n");
782 return rc;
785 vendor = le32_to_cpup((__le32 *)buf);
786 if (vendor != TPM_CR50_I2C_DID_VID &&
787 vendor != TPM_TI50_DT_I2C_DID_VID &&
788 vendor != TPM_TI50_OT_I2C_DID_VID) {
789 dev_err(dev, "Vendor ID did not match! ID was %08x\n", vendor);
790 return -ENODEV;
793 dev_info(dev, "%s TPM 2.0 (i2c 0x%02x irq %d id 0x%x)\n",
794 tpm_cr50_vid_to_name(vendor),
795 client->addr, client->irq, vendor >> 16);
796 return tpm_chip_register(chip);
800 * tpm_cr50_i2c_remove() - Driver remove function.
801 * @client: I2C client information.
803 * Return:
804 * - 0: Success.
805 * - -errno: A POSIX error code.
807 static void tpm_cr50_i2c_remove(struct i2c_client *client)
809 struct tpm_chip *chip = i2c_get_clientdata(client);
810 struct device *dev = &client->dev;
812 if (!chip) {
813 dev_crit(dev, "Could not get client data at remove, memory corruption ahead\n");
814 return;
817 tpm_chip_unregister(chip);
820 static SIMPLE_DEV_PM_OPS(cr50_i2c_pm, tpm_pm_suspend, tpm_pm_resume);
822 static struct i2c_driver cr50_i2c_driver = {
823 .probe = tpm_cr50_i2c_probe,
824 .remove = tpm_cr50_i2c_remove,
825 .driver = {
826 .name = "cr50_i2c",
827 .pm = &cr50_i2c_pm,
828 .acpi_match_table = ACPI_PTR(cr50_i2c_acpi_id),
829 .of_match_table = of_match_ptr(of_cr50_i2c_match),
833 module_i2c_driver(cr50_i2c_driver);
835 MODULE_DESCRIPTION("cr50 TPM I2C Driver");
836 MODULE_LICENSE("GPL");