tpm_i2c_infineon.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. * Copyright (C) 2012,2013 Infineon Technologies
  3. *
  4. * Authors:
  5. * Peter Huewe <peter.huewe@infineon.com>
  6. *
  7. * Device driver for TCG/TCPA TPM (trusted platform module).
  8. * Specifications at www.trustedcomputinggroup.org
  9. *
  10. * This device driver implements the TPM interface as defined in
  11. * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
  12. * Infineon I2C Protocol Stack Specification v0.20.
  13. *
  14. * It is based on the original tpm_tis device driver from Leendert van
  15. * Dorn and Kyleen Hall.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation, version 2 of the
  20. * License.
  21. *
  22. *
  23. */
  24. #include <linux/i2c.h>
  25. #include <linux/module.h>
  26. #include <linux/wait.h>
  27. #include "tpm.h"
  28. /* max. buffer size supported by our TPM */
  29. #define TPM_BUFSIZE 1260
  30. /* max. number of iterations after I2C NAK */
  31. #define MAX_COUNT 3
  32. #define SLEEP_DURATION_LOW 55
  33. #define SLEEP_DURATION_HI 65
  34. /* max. number of iterations after I2C NAK for 'long' commands
  35. * we need this especially for sending TPM_READY, since the cleanup after the
  36. * transtion to the ready state may take some time, but it is unpredictable
  37. * how long it will take.
  38. */
  39. #define MAX_COUNT_LONG 50
  40. #define SLEEP_DURATION_LONG_LOW 200
  41. #define SLEEP_DURATION_LONG_HI 220
  42. /* After sending TPM_READY to 'reset' the TPM we have to sleep even longer */
  43. #define SLEEP_DURATION_RESET_LOW 2400
  44. #define SLEEP_DURATION_RESET_HI 2600
  45. /* we want to use usleep_range instead of msleep for the 5ms TPM_TIMEOUT */
  46. #define TPM_TIMEOUT_US_LOW (TPM_TIMEOUT * 1000)
  47. #define TPM_TIMEOUT_US_HI (TPM_TIMEOUT_US_LOW + 2000)
  48. /* expected value for DIDVID register */
  49. #define TPM_TIS_I2C_DID_VID_9635 0xd1150b00L
  50. #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
  51. enum i2c_chip_type {
  52. SLB9635,
  53. SLB9645,
  54. UNKNOWN,
  55. };
  56. /* Structure to store I2C TPM specific stuff */
  57. struct tpm_inf_dev {
  58. struct i2c_client *client;
  59. u8 buf[TPM_BUFSIZE + sizeof(u8)]; /* max. buffer size + addr */
  60. struct tpm_chip *chip;
  61. enum i2c_chip_type chip_type;
  62. };
  63. static struct tpm_inf_dev tpm_dev;
  64. /*
  65. * iic_tpm_read() - read from TPM register
  66. * @addr: register address to read from
  67. * @buffer: provided by caller
  68. * @len: number of bytes to read
  69. *
  70. * Read len bytes from TPM register and put them into
  71. * buffer (little-endian format, i.e. first byte is put into buffer[0]).
  72. *
  73. * NOTE: TPM is big-endian for multi-byte values. Multi-byte
  74. * values have to be swapped.
  75. *
  76. * NOTE: We can't unfortunately use the combined read/write functions
  77. * provided by the i2c core as the TPM currently does not support the
  78. * repeated start condition and due to it's special requirements.
  79. * The i2c_smbus* functions do not work for this chip.
  80. *
  81. * Return -EIO on error, 0 on success.
  82. */
  83. static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
  84. {
  85. struct i2c_msg msg1 = {
  86. .addr = tpm_dev.client->addr,
  87. .len = 1,
  88. .buf = &addr
  89. };
  90. struct i2c_msg msg2 = {
  91. .addr = tpm_dev.client->addr,
  92. .flags = I2C_M_RD,
  93. .len = len,
  94. .buf = buffer
  95. };
  96. struct i2c_msg msgs[] = {msg1, msg2};
  97. int rc = 0;
  98. int count;
  99. /* Lock the adapter for the duration of the whole sequence. */
  100. if (!tpm_dev.client->adapter->algo->master_xfer)
  101. return -EOPNOTSUPP;
  102. i2c_lock_adapter(tpm_dev.client->adapter);
  103. if (tpm_dev.chip_type == SLB9645) {
  104. /* use a combined read for newer chips
  105. * unfortunately the smbus functions are not suitable due to
  106. * the 32 byte limit of the smbus.
  107. * retries should usually not be needed, but are kept just to
  108. * be on the safe side.
  109. */
  110. for (count = 0; count < MAX_COUNT; count++) {
  111. rc = __i2c_transfer(tpm_dev.client->adapter, msgs, 2);
  112. if (rc > 0)
  113. break; /* break here to skip sleep */
  114. usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
  115. }
  116. } else {
  117. /* slb9635 protocol should work in all cases */
  118. for (count = 0; count < MAX_COUNT; count++) {
  119. rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1);
  120. if (rc > 0)
  121. break; /* break here to skip sleep */
  122. usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
  123. }
  124. if (rc <= 0)
  125. goto out;
  126. /* After the TPM has successfully received the register address
  127. * it needs some time, thus we're sleeping here again, before
  128. * retrieving the data
  129. */
  130. for (count = 0; count < MAX_COUNT; count++) {
  131. usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
  132. rc = __i2c_transfer(tpm_dev.client->adapter, &msg2, 1);
  133. if (rc > 0)
  134. break;
  135. }
  136. }
  137. out:
  138. i2c_unlock_adapter(tpm_dev.client->adapter);
  139. /* take care of 'guard time' */
  140. usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
  141. /* __i2c_transfer returns the number of successfully transferred
  142. * messages.
  143. * So rc should be greater than 0 here otherwise we have an error.
  144. */
  145. if (rc <= 0)
  146. return -EIO;
  147. return 0;
  148. }
  149. static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
  150. unsigned int sleep_low,
  151. unsigned int sleep_hi, u8 max_count)
  152. {
  153. int rc = -EIO;
  154. int count;
  155. struct i2c_msg msg1 = {
  156. .addr = tpm_dev.client->addr,
  157. .len = len + 1,
  158. .buf = tpm_dev.buf
  159. };
  160. if (len > TPM_BUFSIZE)
  161. return -EINVAL;
  162. if (!tpm_dev.client->adapter->algo->master_xfer)
  163. return -EOPNOTSUPP;
  164. i2c_lock_adapter(tpm_dev.client->adapter);
  165. /* prepend the 'register address' to the buffer */
  166. tpm_dev.buf[0] = addr;
  167. memcpy(&(tpm_dev.buf[1]), buffer, len);
  168. /*
  169. * NOTE: We have to use these special mechanisms here and unfortunately
  170. * cannot rely on the standard behavior of i2c_transfer.
  171. * Even for newer chips the smbus functions are not
  172. * suitable due to the 32 byte limit of the smbus.
  173. */
  174. for (count = 0; count < max_count; count++) {
  175. rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1);
  176. if (rc > 0)
  177. break;
  178. usleep_range(sleep_low, sleep_hi);
  179. }
  180. i2c_unlock_adapter(tpm_dev.client->adapter);
  181. /* take care of 'guard time' */
  182. usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
  183. /* __i2c_transfer returns the number of successfully transferred
  184. * messages.
  185. * So rc should be greater than 0 here otherwise we have an error.
  186. */
  187. if (rc <= 0)
  188. return -EIO;
  189. return 0;
  190. }
  191. /*
  192. * iic_tpm_write() - write to TPM register
  193. * @addr: register address to write to
  194. * @buffer: containing data to be written
  195. * @len: number of bytes to write
  196. *
  197. * Write len bytes from provided buffer to TPM register (little
  198. * endian format, i.e. buffer[0] is written as first byte).
  199. *
  200. * NOTE: TPM is big-endian for multi-byte values. Multi-byte
  201. * values have to be swapped.
  202. *
  203. * NOTE: use this function instead of the iic_tpm_write_generic function.
  204. *
  205. * Return -EIO on error, 0 on success
  206. */
  207. static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
  208. {
  209. return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LOW,
  210. SLEEP_DURATION_HI, MAX_COUNT);
  211. }
  212. /*
  213. * This function is needed especially for the cleanup situation after
  214. * sending TPM_READY
  215. * */
  216. static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
  217. {
  218. return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG_LOW,
  219. SLEEP_DURATION_LONG_HI, MAX_COUNT_LONG);
  220. }
  221. enum tis_access {
  222. TPM_ACCESS_VALID = 0x80,
  223. TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
  224. TPM_ACCESS_REQUEST_PENDING = 0x04,
  225. TPM_ACCESS_REQUEST_USE = 0x02,
  226. };
  227. enum tis_status {
  228. TPM_STS_VALID = 0x80,
  229. TPM_STS_COMMAND_READY = 0x40,
  230. TPM_STS_GO = 0x20,
  231. TPM_STS_DATA_AVAIL = 0x10,
  232. TPM_STS_DATA_EXPECT = 0x08,
  233. };
  234. enum tis_defaults {
  235. TIS_SHORT_TIMEOUT = 750, /* ms */
  236. TIS_LONG_TIMEOUT = 2000, /* 2 sec */
  237. };
  238. #define TPM_ACCESS(l) (0x0000 | ((l) << 4))
  239. #define TPM_STS(l) (0x0001 | ((l) << 4))
  240. #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
  241. #define TPM_DID_VID(l) (0x0006 | ((l) << 4))
  242. static int check_locality(struct tpm_chip *chip, int loc)
  243. {
  244. u8 buf;
  245. int rc;
  246. rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
  247. if (rc < 0)
  248. return rc;
  249. if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
  250. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
  251. chip->vendor.locality = loc;
  252. return loc;
  253. }
  254. return -EIO;
  255. }
  256. /* implementation similar to tpm_tis */
  257. static void release_locality(struct tpm_chip *chip, int loc, int force)
  258. {
  259. u8 buf;
  260. if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
  261. return;
  262. if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
  263. (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) {
  264. buf = TPM_ACCESS_ACTIVE_LOCALITY;
  265. iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
  266. }
  267. }
  268. static int request_locality(struct tpm_chip *chip, int loc)
  269. {
  270. unsigned long stop;
  271. u8 buf = TPM_ACCESS_REQUEST_USE;
  272. if (check_locality(chip, loc) >= 0)
  273. return loc;
  274. iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
  275. /* wait for burstcount */
  276. stop = jiffies + chip->vendor.timeout_a;
  277. do {
  278. if (check_locality(chip, loc) >= 0)
  279. return loc;
  280. usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
  281. } while (time_before(jiffies, stop));
  282. return -ETIME;
  283. }
  284. static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
  285. {
  286. /* NOTE: since I2C read may fail, return 0 in this case --> time-out */
  287. u8 buf = 0xFF;
  288. u8 i = 0;
  289. do {
  290. if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
  291. return 0;
  292. i++;
  293. /* if locallity is set STS should not be 0xFF */
  294. } while ((buf == 0xFF) && i < 10);
  295. return buf;
  296. }
  297. static void tpm_tis_i2c_ready(struct tpm_chip *chip)
  298. {
  299. /* this causes the current command to be aborted */
  300. u8 buf = TPM_STS_COMMAND_READY;
  301. iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
  302. }
  303. static ssize_t get_burstcount(struct tpm_chip *chip)
  304. {
  305. unsigned long stop;
  306. ssize_t burstcnt;
  307. u8 buf[3];
  308. /* wait for burstcount */
  309. /* which timeout value, spec has 2 answers (c & d) */
  310. stop = jiffies + chip->vendor.timeout_d;
  311. do {
  312. /* Note: STS is little endian */
  313. if (iic_tpm_read(TPM_STS(chip->vendor.locality)+1, buf, 3) < 0)
  314. burstcnt = 0;
  315. else
  316. burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
  317. if (burstcnt)
  318. return burstcnt;
  319. usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
  320. } while (time_before(jiffies, stop));
  321. return -EBUSY;
  322. }
  323. static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  324. int *status)
  325. {
  326. unsigned long stop;
  327. /* check current status */
  328. *status = tpm_tis_i2c_status(chip);
  329. if ((*status != 0xFF) && (*status & mask) == mask)
  330. return 0;
  331. stop = jiffies + timeout;
  332. do {
  333. /* since we just checked the status, give the TPM some time */
  334. usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
  335. *status = tpm_tis_i2c_status(chip);
  336. if ((*status & mask) == mask)
  337. return 0;
  338. } while (time_before(jiffies, stop));
  339. return -ETIME;
  340. }
  341. static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
  342. {
  343. size_t size = 0;
  344. ssize_t burstcnt;
  345. u8 retries = 0;
  346. int rc;
  347. while (size < count) {
  348. burstcnt = get_burstcount(chip);
  349. /* burstcnt < 0 = TPM is busy */
  350. if (burstcnt < 0)
  351. return burstcnt;
  352. /* limit received data to max. left */
  353. if (burstcnt > (count - size))
  354. burstcnt = count - size;
  355. rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
  356. &(buf[size]), burstcnt);
  357. if (rc == 0)
  358. size += burstcnt;
  359. else if (rc < 0)
  360. retries++;
  361. /* avoid endless loop in case of broken HW */
  362. if (retries > MAX_COUNT_LONG)
  363. return -EIO;
  364. }
  365. return size;
  366. }
  367. static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  368. {
  369. int size = 0;
  370. int expected, status;
  371. if (count < TPM_HEADER_SIZE) {
  372. size = -EIO;
  373. goto out;
  374. }
  375. /* read first 10 bytes, including tag, paramsize, and result */
  376. size = recv_data(chip, buf, TPM_HEADER_SIZE);
  377. if (size < TPM_HEADER_SIZE) {
  378. dev_err(chip->pdev, "Unable to read header\n");
  379. goto out;
  380. }
  381. expected = be32_to_cpu(*(__be32 *)(buf + 2));
  382. if ((size_t) expected > count) {
  383. size = -EIO;
  384. goto out;
  385. }
  386. size += recv_data(chip, &buf[TPM_HEADER_SIZE],
  387. expected - TPM_HEADER_SIZE);
  388. if (size < expected) {
  389. dev_err(chip->pdev, "Unable to read remainder of result\n");
  390. size = -ETIME;
  391. goto out;
  392. }
  393. wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
  394. if (status & TPM_STS_DATA_AVAIL) { /* retry? */
  395. dev_err(chip->pdev, "Error left over data\n");
  396. size = -EIO;
  397. goto out;
  398. }
  399. out:
  400. tpm_tis_i2c_ready(chip);
  401. /* The TPM needs some time to clean up here,
  402. * so we sleep rather than keeping the bus busy
  403. */
  404. usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI);
  405. release_locality(chip, chip->vendor.locality, 0);
  406. return size;
  407. }
  408. static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
  409. {
  410. int rc, status;
  411. ssize_t burstcnt;
  412. size_t count = 0;
  413. u8 retries = 0;
  414. u8 sts = TPM_STS_GO;
  415. if (len > TPM_BUFSIZE)
  416. return -E2BIG; /* command is too long for our tpm, sorry */
  417. if (request_locality(chip, 0) < 0)
  418. return -EBUSY;
  419. status = tpm_tis_i2c_status(chip);
  420. if ((status & TPM_STS_COMMAND_READY) == 0) {
  421. tpm_tis_i2c_ready(chip);
  422. if (wait_for_stat
  423. (chip, TPM_STS_COMMAND_READY,
  424. chip->vendor.timeout_b, &status) < 0) {
  425. rc = -ETIME;
  426. goto out_err;
  427. }
  428. }
  429. while (count < len - 1) {
  430. burstcnt = get_burstcount(chip);
  431. /* burstcnt < 0 = TPM is busy */
  432. if (burstcnt < 0)
  433. return burstcnt;
  434. if (burstcnt > (len - 1 - count))
  435. burstcnt = len - 1 - count;
  436. rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
  437. &(buf[count]), burstcnt);
  438. if (rc == 0)
  439. count += burstcnt;
  440. else if (rc < 0)
  441. retries++;
  442. /* avoid endless loop in case of broken HW */
  443. if (retries > MAX_COUNT_LONG) {
  444. rc = -EIO;
  445. goto out_err;
  446. }
  447. wait_for_stat(chip, TPM_STS_VALID,
  448. chip->vendor.timeout_c, &status);
  449. if ((status & TPM_STS_DATA_EXPECT) == 0) {
  450. rc = -EIO;
  451. goto out_err;
  452. }
  453. }
  454. /* write last byte */
  455. iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), &(buf[count]), 1);
  456. wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
  457. if ((status & TPM_STS_DATA_EXPECT) != 0) {
  458. rc = -EIO;
  459. goto out_err;
  460. }
  461. /* go and do it */
  462. iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
  463. return len;
  464. out_err:
  465. tpm_tis_i2c_ready(chip);
  466. /* The TPM needs some time to clean up here,
  467. * so we sleep rather than keeping the bus busy
  468. */
  469. usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI);
  470. release_locality(chip, chip->vendor.locality, 0);
  471. return rc;
  472. }
  473. static bool tpm_tis_i2c_req_canceled(struct tpm_chip *chip, u8 status)
  474. {
  475. return (status == TPM_STS_COMMAND_READY);
  476. }
  477. static const struct tpm_class_ops tpm_tis_i2c = {
  478. .status = tpm_tis_i2c_status,
  479. .recv = tpm_tis_i2c_recv,
  480. .send = tpm_tis_i2c_send,
  481. .cancel = tpm_tis_i2c_ready,
  482. .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  483. .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  484. .req_canceled = tpm_tis_i2c_req_canceled,
  485. };
  486. static int tpm_tis_i2c_init(struct device *dev)
  487. {
  488. u32 vendor;
  489. int rc = 0;
  490. struct tpm_chip *chip;
  491. chip = tpmm_chip_alloc(dev, &tpm_tis_i2c);
  492. if (IS_ERR(chip))
  493. return PTR_ERR(chip);
  494. /* Disable interrupts */
  495. chip->vendor.irq = 0;
  496. /* Default timeouts */
  497. chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  498. chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
  499. chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  500. chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  501. if (request_locality(chip, 0) != 0) {
  502. dev_err(dev, "could not request locality\n");
  503. rc = -ENODEV;
  504. goto out_err;
  505. }
  506. /* read four bytes from DID_VID register */
  507. if (iic_tpm_read(TPM_DID_VID(0), (u8 *)&vendor, 4) < 0) {
  508. dev_err(dev, "could not read vendor id\n");
  509. rc = -EIO;
  510. goto out_release;
  511. }
  512. if (vendor == TPM_TIS_I2C_DID_VID_9645) {
  513. tpm_dev.chip_type = SLB9645;
  514. } else if (vendor == TPM_TIS_I2C_DID_VID_9635) {
  515. tpm_dev.chip_type = SLB9635;
  516. } else {
  517. dev_err(dev, "vendor id did not match! ID was %08x\n", vendor);
  518. rc = -ENODEV;
  519. goto out_release;
  520. }
  521. dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16);
  522. INIT_LIST_HEAD(&chip->vendor.list);
  523. tpm_dev.chip = chip;
  524. tpm_get_timeouts(chip);
  525. tpm_do_selftest(chip);
  526. return tpm_chip_register(chip);
  527. out_release:
  528. release_locality(chip, chip->vendor.locality, 1);
  529. tpm_dev.client = NULL;
  530. out_err:
  531. return rc;
  532. }
  533. static const struct i2c_device_id tpm_tis_i2c_table[] = {
  534. {"tpm_i2c_infineon", 0},
  535. {"slb9635tt", 0},
  536. {"slb9645tt", 1},
  537. {},
  538. };
  539. MODULE_DEVICE_TABLE(i2c, tpm_tis_i2c_table);
  540. #ifdef CONFIG_OF
  541. static const struct of_device_id tpm_tis_i2c_of_match[] = {
  542. {
  543. .name = "tpm_i2c_infineon",
  544. .type = "tpm",
  545. .compatible = "infineon,tpm_i2c_infineon",
  546. .data = (void *)0
  547. },
  548. {
  549. .name = "slb9635tt",
  550. .type = "tpm",
  551. .compatible = "infineon,slb9635tt",
  552. .data = (void *)0
  553. },
  554. {
  555. .name = "slb9645tt",
  556. .type = "tpm",
  557. .compatible = "infineon,slb9645tt",
  558. .data = (void *)1
  559. },
  560. {},
  561. };
  562. MODULE_DEVICE_TABLE(of, tpm_tis_i2c_of_match);
  563. #endif
  564. static SIMPLE_DEV_PM_OPS(tpm_tis_i2c_ops, tpm_pm_suspend, tpm_pm_resume);
  565. static int tpm_tis_i2c_probe(struct i2c_client *client,
  566. const struct i2c_device_id *id)
  567. {
  568. int rc;
  569. struct device *dev = &(client->dev);
  570. if (tpm_dev.client != NULL) {
  571. dev_err(dev, "This driver only supports one client at a time\n");
  572. return -EBUSY; /* We only support one client */
  573. }
  574. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  575. dev_err(dev, "no algorithms associated to the i2c bus\n");
  576. return -ENODEV;
  577. }
  578. tpm_dev.client = client;
  579. rc = tpm_tis_i2c_init(&client->dev);
  580. if (rc != 0) {
  581. tpm_dev.client = NULL;
  582. rc = -ENODEV;
  583. }
  584. return rc;
  585. }
  586. static int tpm_tis_i2c_remove(struct i2c_client *client)
  587. {
  588. struct tpm_chip *chip = tpm_dev.chip;
  589. tpm_chip_unregister(chip);
  590. release_locality(chip, chip->vendor.locality, 1);
  591. tpm_dev.client = NULL;
  592. return 0;
  593. }
  594. static struct i2c_driver tpm_tis_i2c_driver = {
  595. .id_table = tpm_tis_i2c_table,
  596. .probe = tpm_tis_i2c_probe,
  597. .remove = tpm_tis_i2c_remove,
  598. .driver = {
  599. .name = "tpm_i2c_infineon",
  600. .owner = THIS_MODULE,
  601. .pm = &tpm_tis_i2c_ops,
  602. .of_match_table = of_match_ptr(tpm_tis_i2c_of_match),
  603. },
  604. };
  605. module_i2c_driver(tpm_tis_i2c_driver);
  606. MODULE_AUTHOR("Peter Huewe <peter.huewe@infineon.com>");
  607. MODULE_DESCRIPTION("TPM TIS I2C Infineon Driver");
  608. MODULE_VERSION("2.2.0");
  609. MODULE_LICENSE("GPL");