tpm_i2c_infineon.c 17 KB

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