tpm_i2c_stm_st33.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*
  2. * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24
  3. * Copyright (C) 2009, 2010 STMicroelectronics
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * STMicroelectronics version 1.2.0, Copyright (C) 2010
  20. * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
  21. * This is free software, and you are welcome to redistribute it
  22. * under certain conditions.
  23. *
  24. * @Author: Christophe RICARD tpmsupport@st.com
  25. *
  26. * @File: tpm_stm_st33_i2c.c
  27. *
  28. * @Synopsis:
  29. * 09/15/2010: First shot driver tpm_tis driver for
  30. lpc is used as model.
  31. */
  32. #include <linux/pci.h>
  33. #include <linux/module.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/i2c.h>
  36. #include <linux/fs.h>
  37. #include <linux/miscdevice.h>
  38. #include <linux/kernel.h>
  39. #include <linux/delay.h>
  40. #include <linux/wait.h>
  41. #include <linux/string.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/sysfs.h>
  45. #include <linux/gpio.h>
  46. #include <linux/sched.h>
  47. #include <linux/uaccess.h>
  48. #include <linux/io.h>
  49. #include <linux/slab.h>
  50. #include "tpm.h"
  51. #include "tpm_i2c_stm_st33.h"
  52. enum stm33zp24_access {
  53. TPM_ACCESS_VALID = 0x80,
  54. TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
  55. TPM_ACCESS_REQUEST_PENDING = 0x04,
  56. TPM_ACCESS_REQUEST_USE = 0x02,
  57. };
  58. enum stm33zp24_status {
  59. TPM_STS_VALID = 0x80,
  60. TPM_STS_COMMAND_READY = 0x40,
  61. TPM_STS_GO = 0x20,
  62. TPM_STS_DATA_AVAIL = 0x10,
  63. TPM_STS_DATA_EXPECT = 0x08,
  64. };
  65. enum stm33zp24_int_flags {
  66. TPM_GLOBAL_INT_ENABLE = 0x80,
  67. TPM_INTF_CMD_READY_INT = 0x080,
  68. TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
  69. TPM_INTF_WAKE_UP_READY_INT = 0x020,
  70. TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
  71. TPM_INTF_STS_VALID_INT = 0x002,
  72. TPM_INTF_DATA_AVAIL_INT = 0x001,
  73. };
  74. enum tis_defaults {
  75. TIS_SHORT_TIMEOUT = 750,
  76. TIS_LONG_TIMEOUT = 2000,
  77. };
  78. /*
  79. * write8_reg
  80. * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
  81. * @param: tpm_register, the tpm tis register where the data should be written
  82. * @param: tpm_data, the tpm_data to write inside the tpm_register
  83. * @param: tpm_size, The length of the data
  84. * @return: Returns negative errno, or else the number of bytes written.
  85. */
  86. static int write8_reg(struct i2c_client *client, u8 tpm_register,
  87. u8 *tpm_data, u16 tpm_size)
  88. {
  89. struct st33zp24_platform_data *pin_infos;
  90. pin_infos = client->dev.platform_data;
  91. pin_infos->tpm_i2c_buffer[0][0] = tpm_register;
  92. memcpy(&pin_infos->tpm_i2c_buffer[0][1], tpm_data, tpm_size);
  93. return i2c_master_send(client, pin_infos->tpm_i2c_buffer[0],
  94. tpm_size + 1);
  95. } /* write8_reg() */
  96. /*
  97. * read8_reg
  98. * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
  99. * @param: tpm_register, the tpm tis register where the data should be read
  100. * @param: tpm_data, the TPM response
  101. * @param: tpm_size, tpm TPM response size to read.
  102. * @return: number of byte read successfully: should be one if success.
  103. */
  104. static int read8_reg(struct i2c_client *client, u8 tpm_register,
  105. u8 *tpm_data, int tpm_size)
  106. {
  107. u8 status = 0;
  108. u8 data;
  109. data = TPM_DUMMY_BYTE;
  110. status = write8_reg(client, tpm_register, &data, 1);
  111. if (status == 2)
  112. status = i2c_master_recv(client, tpm_data, tpm_size);
  113. return status;
  114. } /* read8_reg() */
  115. /*
  116. * I2C_WRITE_DATA
  117. * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
  118. * @param: client, the chip description
  119. * @param: tpm_register, the tpm tis register where the data should be written
  120. * @param: tpm_data, the tpm_data to write inside the tpm_register
  121. * @param: tpm_size, The length of the data
  122. * @return: number of byte written successfully: should be one if success.
  123. */
  124. #define I2C_WRITE_DATA(client, tpm_register, tpm_data, tpm_size) \
  125. (write8_reg(client, tpm_register | \
  126. TPM_WRITE_DIRECTION, tpm_data, tpm_size))
  127. /*
  128. * I2C_READ_DATA
  129. * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
  130. * @param: tpm, the chip description
  131. * @param: tpm_register, the tpm tis register where the data should be read
  132. * @param: tpm_data, the TPM response
  133. * @param: tpm_size, tpm TPM response size to read.
  134. * @return: number of byte read successfully: should be one if success.
  135. */
  136. #define I2C_READ_DATA(client, tpm_register, tpm_data, tpm_size) \
  137. (read8_reg(client, tpm_register, tpm_data, tpm_size))
  138. /*
  139. * clear_interruption
  140. * clear the TPM interrupt register.
  141. * @param: tpm, the chip description
  142. */
  143. static void clear_interruption(struct i2c_client *client)
  144. {
  145. u8 interrupt;
  146. I2C_READ_DATA(client, TPM_INT_STATUS, &interrupt, 1);
  147. I2C_WRITE_DATA(client, TPM_INT_STATUS, &interrupt, 1);
  148. I2C_READ_DATA(client, TPM_INT_STATUS, &interrupt, 1);
  149. } /* clear_interruption() */
  150. /*
  151. * _wait_for_interrupt_serirq_timeout
  152. * @param: tpm, the chip description
  153. * @param: timeout, the timeout of the interrupt
  154. * @return: the status of the interruption.
  155. */
  156. static long _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
  157. unsigned long timeout)
  158. {
  159. long status;
  160. struct i2c_client *client;
  161. struct st33zp24_platform_data *pin_infos;
  162. client = (struct i2c_client *)TPM_VPRIV(chip);
  163. pin_infos = client->dev.platform_data;
  164. status = wait_for_completion_interruptible_timeout(
  165. &pin_infos->irq_detection,
  166. timeout);
  167. if (status > 0)
  168. enable_irq(gpio_to_irq(pin_infos->io_serirq));
  169. gpio_direction_input(pin_infos->io_serirq);
  170. return status;
  171. } /* wait_for_interrupt_serirq_timeout() */
  172. static int wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
  173. unsigned long timeout)
  174. {
  175. int status = 2;
  176. struct i2c_client *client;
  177. client = (struct i2c_client *)TPM_VPRIV(chip);
  178. status = _wait_for_interrupt_serirq_timeout(chip, timeout);
  179. if (!status) {
  180. status = -EBUSY;
  181. } else {
  182. clear_interruption(client);
  183. if (condition)
  184. status = 1;
  185. }
  186. return status;
  187. }
  188. /*
  189. * tpm_stm_i2c_cancel, cancel is not implemented.
  190. * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
  191. */
  192. static void tpm_stm_i2c_cancel(struct tpm_chip *chip)
  193. {
  194. struct i2c_client *client;
  195. u8 data;
  196. client = (struct i2c_client *)TPM_VPRIV(chip);
  197. data = TPM_STS_COMMAND_READY;
  198. I2C_WRITE_DATA(client, TPM_STS, &data, 1);
  199. if (chip->vendor.irq)
  200. wait_for_serirq_timeout(chip, 1, chip->vendor.timeout_a);
  201. } /* tpm_stm_i2c_cancel() */
  202. /*
  203. * tpm_stm_spi_status return the TPM_STS register
  204. * @param: chip, the tpm chip description
  205. * @return: the TPM_STS register value.
  206. */
  207. static u8 tpm_stm_i2c_status(struct tpm_chip *chip)
  208. {
  209. struct i2c_client *client;
  210. u8 data;
  211. client = (struct i2c_client *)TPM_VPRIV(chip);
  212. I2C_READ_DATA(client, TPM_STS, &data, 1);
  213. return data;
  214. } /* tpm_stm_i2c_status() */
  215. /*
  216. * check_locality if the locality is active
  217. * @param: chip, the tpm chip description
  218. * @return: the active locality or -EACCESS.
  219. */
  220. static int check_locality(struct tpm_chip *chip)
  221. {
  222. struct i2c_client *client;
  223. u8 data;
  224. u8 status;
  225. client = (struct i2c_client *)TPM_VPRIV(chip);
  226. status = I2C_READ_DATA(client, TPM_ACCESS, &data, 1);
  227. if (status && (data &
  228. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
  229. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
  230. return chip->vendor.locality;
  231. return -EACCES;
  232. } /* check_locality() */
  233. /*
  234. * request_locality request the TPM locality
  235. * @param: chip, the chip description
  236. * @return: the active locality or EACCESS.
  237. */
  238. static int request_locality(struct tpm_chip *chip)
  239. {
  240. unsigned long stop;
  241. long rc;
  242. struct i2c_client *client;
  243. u8 data;
  244. client = (struct i2c_client *)TPM_VPRIV(chip);
  245. if (check_locality(chip) == chip->vendor.locality)
  246. return chip->vendor.locality;
  247. data = TPM_ACCESS_REQUEST_USE;
  248. rc = I2C_WRITE_DATA(client, TPM_ACCESS, &data, 1);
  249. if (rc < 0)
  250. goto end;
  251. if (chip->vendor.irq) {
  252. rc = wait_for_serirq_timeout(chip, (check_locality
  253. (chip) >= 0),
  254. chip->vendor.timeout_a);
  255. if (rc > 0)
  256. return chip->vendor.locality;
  257. } else {
  258. stop = jiffies + chip->vendor.timeout_a;
  259. do {
  260. if (check_locality(chip) >= 0)
  261. return chip->vendor.locality;
  262. msleep(TPM_TIMEOUT);
  263. } while (time_before(jiffies, stop));
  264. }
  265. rc = -EACCES;
  266. end:
  267. return rc;
  268. } /* request_locality() */
  269. /*
  270. * release_locality release the active locality
  271. * @param: chip, the tpm chip description.
  272. */
  273. static void release_locality(struct tpm_chip *chip)
  274. {
  275. struct i2c_client *client;
  276. u8 data;
  277. client = (struct i2c_client *)TPM_VPRIV(chip);
  278. data = TPM_ACCESS_ACTIVE_LOCALITY;
  279. I2C_WRITE_DATA(client, TPM_ACCESS, &data, 1);
  280. }
  281. /*
  282. * get_burstcount return the burstcount address 0x19 0x1A
  283. * @param: chip, the chip description
  284. * return: the burstcount.
  285. */
  286. static int get_burstcount(struct tpm_chip *chip)
  287. {
  288. unsigned long stop;
  289. int burstcnt, status;
  290. u8 tpm_reg, temp;
  291. struct i2c_client *client = (struct i2c_client *)TPM_VPRIV(chip);
  292. stop = jiffies + chip->vendor.timeout_d;
  293. do {
  294. tpm_reg = TPM_STS + 1;
  295. status = I2C_READ_DATA(client, tpm_reg, &temp, 1);
  296. if (status < 0)
  297. goto end;
  298. tpm_reg = tpm_reg + 1;
  299. burstcnt = temp;
  300. status = I2C_READ_DATA(client, tpm_reg, &temp, 1);
  301. if (status < 0)
  302. goto end;
  303. burstcnt |= temp << 8;
  304. if (burstcnt)
  305. return burstcnt;
  306. msleep(TPM_TIMEOUT);
  307. } while (time_before(jiffies, stop));
  308. end:
  309. return -EBUSY;
  310. } /* get_burstcount() */
  311. /*
  312. * wait_for_stat wait for a TPM_STS value
  313. * @param: chip, the tpm chip description
  314. * @param: mask, the value mask to wait
  315. * @param: timeout, the timeout
  316. * @param: queue, the wait queue.
  317. * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
  318. */
  319. static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  320. wait_queue_head_t *queue)
  321. {
  322. unsigned long stop;
  323. long rc;
  324. u8 status;
  325. if (chip->vendor.irq) {
  326. rc = wait_for_serirq_timeout(chip, ((tpm_stm_i2c_status
  327. (chip) & mask) ==
  328. mask), timeout);
  329. if (rc > 0)
  330. return 0;
  331. } else {
  332. stop = jiffies + timeout;
  333. do {
  334. msleep(TPM_TIMEOUT);
  335. status = tpm_stm_i2c_status(chip);
  336. if ((status & mask) == mask)
  337. return 0;
  338. } while (time_before(jiffies, stop));
  339. }
  340. return -ETIME;
  341. } /* wait_for_stat() */
  342. /*
  343. * recv_data receive data
  344. * @param: chip, the tpm chip description
  345. * @param: buf, the buffer where the data are received
  346. * @param: count, the number of data to receive
  347. * @return: the number of bytes read from TPM FIFO.
  348. */
  349. static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
  350. {
  351. int size = 0, burstcnt, len, ret;
  352. struct i2c_client *client;
  353. client = (struct i2c_client *)TPM_VPRIV(chip);
  354. while (size < count &&
  355. wait_for_stat(chip,
  356. TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  357. chip->vendor.timeout_c,
  358. &chip->vendor.read_queue) == 0) {
  359. burstcnt = get_burstcount(chip);
  360. if (burstcnt < 0)
  361. return burstcnt;
  362. len = min_t(int, burstcnt, count - size);
  363. ret = I2C_READ_DATA(client, TPM_DATA_FIFO, buf + size, len);
  364. if (ret < 0)
  365. return ret;
  366. size += len;
  367. }
  368. return size;
  369. }
  370. /*
  371. * tpm_ioserirq_handler the serirq irq handler
  372. * @param: irq, the tpm chip description
  373. * @param: dev_id, the description of the chip
  374. * @return: the status of the handler.
  375. */
  376. static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
  377. {
  378. struct tpm_chip *chip = dev_id;
  379. struct i2c_client *client;
  380. struct st33zp24_platform_data *pin_infos;
  381. disable_irq_nosync(irq);
  382. client = (struct i2c_client *)TPM_VPRIV(chip);
  383. pin_infos = client->dev.platform_data;
  384. complete(&pin_infos->irq_detection);
  385. return IRQ_HANDLED;
  386. } /* tpm_ioserirq_handler() */
  387. /*
  388. * tpm_stm_i2c_send send TPM commands through the I2C bus.
  389. *
  390. * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
  391. * @param: buf, the buffer to send.
  392. * @param: count, the number of bytes to send.
  393. * @return: In case of success the number of bytes sent.
  394. * In other case, a < 0 value describing the issue.
  395. */
  396. static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,
  397. size_t len)
  398. {
  399. u32 status, i, size;
  400. int burstcnt = 0;
  401. int ret;
  402. u8 data;
  403. struct i2c_client *client;
  404. if (chip == NULL)
  405. return -EBUSY;
  406. if (len < TPM_HEADER_SIZE)
  407. return -EBUSY;
  408. client = (struct i2c_client *)TPM_VPRIV(chip);
  409. client->flags = 0;
  410. ret = request_locality(chip);
  411. if (ret < 0)
  412. return ret;
  413. status = tpm_stm_i2c_status(chip);
  414. if ((status & TPM_STS_COMMAND_READY) == 0) {
  415. tpm_stm_i2c_cancel(chip);
  416. if (wait_for_stat
  417. (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
  418. &chip->vendor.int_queue) < 0) {
  419. ret = -ETIME;
  420. goto out_err;
  421. }
  422. }
  423. for (i = 0; i < len - 1;) {
  424. burstcnt = get_burstcount(chip);
  425. if (burstcnt < 0)
  426. return burstcnt;
  427. size = min_t(int, len - i - 1, burstcnt);
  428. ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf + i, size);
  429. if (ret < 0)
  430. goto out_err;
  431. i += size;
  432. }
  433. status = tpm_stm_i2c_status(chip);
  434. if ((status & TPM_STS_DATA_EXPECT) == 0) {
  435. ret = -EIO;
  436. goto out_err;
  437. }
  438. ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf + len - 1, 1);
  439. if (ret < 0)
  440. goto out_err;
  441. status = tpm_stm_i2c_status(chip);
  442. if ((status & TPM_STS_DATA_EXPECT) != 0) {
  443. ret = -EIO;
  444. goto out_err;
  445. }
  446. data = TPM_STS_GO;
  447. I2C_WRITE_DATA(client, TPM_STS, &data, 1);
  448. return len;
  449. out_err:
  450. tpm_stm_i2c_cancel(chip);
  451. release_locality(chip);
  452. return ret;
  453. }
  454. /*
  455. * tpm_stm_i2c_recv received TPM response through the I2C bus.
  456. * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
  457. * @param: buf, the buffer to store datas.
  458. * @param: count, the number of bytes to send.
  459. * @return: In case of success the number of bytes received.
  460. * In other case, a < 0 value describing the issue.
  461. */
  462. static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,
  463. size_t count)
  464. {
  465. int size = 0;
  466. int expected;
  467. if (chip == NULL)
  468. return -EBUSY;
  469. if (count < TPM_HEADER_SIZE) {
  470. size = -EIO;
  471. goto out;
  472. }
  473. size = recv_data(chip, buf, TPM_HEADER_SIZE);
  474. if (size < TPM_HEADER_SIZE) {
  475. dev_err(chip->dev, "Unable to read header\n");
  476. goto out;
  477. }
  478. expected = be32_to_cpu(*(__be32 *)(buf + 2));
  479. if (expected > count) {
  480. size = -EIO;
  481. goto out;
  482. }
  483. size += recv_data(chip, &buf[TPM_HEADER_SIZE],
  484. expected - TPM_HEADER_SIZE);
  485. if (size < expected) {
  486. dev_err(chip->dev, "Unable to read remainder of result\n");
  487. size = -ETIME;
  488. goto out;
  489. }
  490. out:
  491. chip->ops->cancel(chip);
  492. release_locality(chip);
  493. return size;
  494. }
  495. static bool tpm_st33_i2c_req_canceled(struct tpm_chip *chip, u8 status)
  496. {
  497. return (status == TPM_STS_COMMAND_READY);
  498. }
  499. static const struct tpm_class_ops st_i2c_tpm = {
  500. .send = tpm_stm_i2c_send,
  501. .recv = tpm_stm_i2c_recv,
  502. .cancel = tpm_stm_i2c_cancel,
  503. .status = tpm_stm_i2c_status,
  504. .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  505. .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  506. .req_canceled = tpm_st33_i2c_req_canceled,
  507. };
  508. static int interrupts;
  509. module_param(interrupts, int, 0444);
  510. MODULE_PARM_DESC(interrupts, "Enable interrupts");
  511. static int power_mgt = 1;
  512. module_param(power_mgt, int, 0444);
  513. MODULE_PARM_DESC(power_mgt, "Power Management");
  514. /*
  515. * tpm_st33_i2c_probe initialize the TPM device
  516. * @param: client, the i2c_client drescription (TPM I2C description).
  517. * @param: id, the i2c_device_id struct.
  518. * @return: 0 in case of success.
  519. * -1 in other case.
  520. */
  521. static int
  522. tpm_st33_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  523. {
  524. int err;
  525. u8 intmask;
  526. struct tpm_chip *chip;
  527. struct st33zp24_platform_data *platform_data;
  528. if (client == NULL) {
  529. pr_info("%s: i2c client is NULL. Device not accessible.\n",
  530. __func__);
  531. err = -ENODEV;
  532. goto end;
  533. }
  534. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  535. dev_info(&client->dev, "client not i2c capable\n");
  536. err = -ENODEV;
  537. goto end;
  538. }
  539. chip = tpm_register_hardware(&client->dev, &st_i2c_tpm);
  540. if (!chip) {
  541. dev_info(&client->dev, "fail chip\n");
  542. err = -ENODEV;
  543. goto end;
  544. }
  545. platform_data = client->dev.platform_data;
  546. if (!platform_data) {
  547. dev_info(&client->dev, "chip not available\n");
  548. err = -ENODEV;
  549. goto _tpm_clean_answer;
  550. }
  551. platform_data->tpm_i2c_buffer[0] =
  552. kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
  553. if (platform_data->tpm_i2c_buffer[0] == NULL) {
  554. err = -ENOMEM;
  555. goto _tpm_clean_answer;
  556. }
  557. platform_data->tpm_i2c_buffer[1] =
  558. kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
  559. if (platform_data->tpm_i2c_buffer[1] == NULL) {
  560. err = -ENOMEM;
  561. goto _tpm_clean_response1;
  562. }
  563. TPM_VPRIV(chip) = client;
  564. chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  565. chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
  566. chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  567. chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  568. chip->vendor.locality = LOCALITY0;
  569. if (power_mgt) {
  570. err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
  571. if (err)
  572. goto _gpio_init1;
  573. gpio_set_value(platform_data->io_lpcpd, 1);
  574. }
  575. if (interrupts) {
  576. init_completion(&platform_data->irq_detection);
  577. if (request_locality(chip) != LOCALITY0) {
  578. err = -ENODEV;
  579. goto _tpm_clean_response2;
  580. }
  581. err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
  582. if (err)
  583. goto _gpio_init2;
  584. clear_interruption(client);
  585. err = request_irq(gpio_to_irq(platform_data->io_serirq),
  586. &tpm_ioserirq_handler,
  587. IRQF_TRIGGER_HIGH,
  588. "TPM SERIRQ management", chip);
  589. if (err < 0) {
  590. dev_err(chip->dev , "TPM SERIRQ signals %d not available\n",
  591. gpio_to_irq(platform_data->io_serirq));
  592. goto _irq_set;
  593. }
  594. err = I2C_READ_DATA(client, TPM_INT_ENABLE, &intmask, 1);
  595. if (err < 0)
  596. goto _irq_set;
  597. intmask |= TPM_INTF_CMD_READY_INT
  598. | TPM_INTF_FIFO_AVALAIBLE_INT
  599. | TPM_INTF_WAKE_UP_READY_INT
  600. | TPM_INTF_LOCALITY_CHANGE_INT
  601. | TPM_INTF_STS_VALID_INT
  602. | TPM_INTF_DATA_AVAIL_INT;
  603. err = I2C_WRITE_DATA(client, TPM_INT_ENABLE, &intmask, 1);
  604. if (err < 0)
  605. goto _irq_set;
  606. intmask = TPM_GLOBAL_INT_ENABLE;
  607. err = I2C_WRITE_DATA(client, (TPM_INT_ENABLE + 3), &intmask, 1);
  608. if (err < 0)
  609. goto _irq_set;
  610. err = I2C_READ_DATA(client, TPM_INT_STATUS, &intmask, 1);
  611. if (err < 0)
  612. goto _irq_set;
  613. chip->vendor.irq = interrupts;
  614. tpm_gen_interrupt(chip);
  615. }
  616. tpm_get_timeouts(chip);
  617. tpm_do_selftest(chip);
  618. dev_info(chip->dev, "TPM I2C Initialized\n");
  619. return 0;
  620. _irq_set:
  621. free_irq(gpio_to_irq(platform_data->io_serirq), (void *)chip);
  622. _gpio_init2:
  623. if (interrupts)
  624. gpio_free(platform_data->io_serirq);
  625. _gpio_init1:
  626. if (power_mgt)
  627. gpio_free(platform_data->io_lpcpd);
  628. _tpm_clean_response2:
  629. kzfree(platform_data->tpm_i2c_buffer[1]);
  630. platform_data->tpm_i2c_buffer[1] = NULL;
  631. _tpm_clean_response1:
  632. kzfree(platform_data->tpm_i2c_buffer[0]);
  633. platform_data->tpm_i2c_buffer[0] = NULL;
  634. _tpm_clean_answer:
  635. tpm_remove_hardware(chip->dev);
  636. end:
  637. pr_info("TPM I2C initialisation fail\n");
  638. return err;
  639. }
  640. /*
  641. * tpm_st33_i2c_remove remove the TPM device
  642. * @param: client, the i2c_client drescription (TPM I2C description).
  643. clear_bit(0, &chip->is_open);
  644. * @return: 0 in case of success.
  645. */
  646. static int tpm_st33_i2c_remove(struct i2c_client *client)
  647. {
  648. struct tpm_chip *chip = (struct tpm_chip *)i2c_get_clientdata(client);
  649. struct st33zp24_platform_data *pin_infos =
  650. ((struct i2c_client *)TPM_VPRIV(chip))->dev.platform_data;
  651. if (pin_infos != NULL) {
  652. free_irq(pin_infos->io_serirq, chip);
  653. gpio_free(pin_infos->io_serirq);
  654. gpio_free(pin_infos->io_lpcpd);
  655. tpm_remove_hardware(chip->dev);
  656. if (pin_infos->tpm_i2c_buffer[1] != NULL) {
  657. kzfree(pin_infos->tpm_i2c_buffer[1]);
  658. pin_infos->tpm_i2c_buffer[1] = NULL;
  659. }
  660. if (pin_infos->tpm_i2c_buffer[0] != NULL) {
  661. kzfree(pin_infos->tpm_i2c_buffer[0]);
  662. pin_infos->tpm_i2c_buffer[0] = NULL;
  663. }
  664. }
  665. return 0;
  666. }
  667. #ifdef CONFIG_PM_SLEEP
  668. /*
  669. * tpm_st33_i2c_pm_suspend suspend the TPM device
  670. * @param: client, the i2c_client drescription (TPM I2C description).
  671. * @param: mesg, the power management message.
  672. * @return: 0 in case of success.
  673. */
  674. static int tpm_st33_i2c_pm_suspend(struct device *dev)
  675. {
  676. struct st33zp24_platform_data *pin_infos = dev->platform_data;
  677. int ret = 0;
  678. if (power_mgt) {
  679. gpio_set_value(pin_infos->io_lpcpd, 0);
  680. } else {
  681. ret = tpm_pm_suspend(dev);
  682. }
  683. return ret;
  684. } /* tpm_st33_i2c_suspend() */
  685. /*
  686. * tpm_st33_i2c_pm_resume resume the TPM device
  687. * @param: client, the i2c_client drescription (TPM I2C description).
  688. * @return: 0 in case of success.
  689. */
  690. static int tpm_st33_i2c_pm_resume(struct device *dev)
  691. {
  692. struct tpm_chip *chip = dev_get_drvdata(dev);
  693. struct st33zp24_platform_data *pin_infos = dev->platform_data;
  694. int ret = 0;
  695. if (power_mgt) {
  696. gpio_set_value(pin_infos->io_lpcpd, 1);
  697. ret = wait_for_serirq_timeout(chip,
  698. (chip->ops->status(chip) &
  699. TPM_STS_VALID) == TPM_STS_VALID,
  700. chip->vendor.timeout_b);
  701. } else {
  702. ret = tpm_pm_resume(dev);
  703. if (!ret)
  704. tpm_do_selftest(chip);
  705. }
  706. return ret;
  707. } /* tpm_st33_i2c_pm_resume() */
  708. #endif
  709. static const struct i2c_device_id tpm_st33_i2c_id[] = {
  710. {TPM_ST33_I2C, 0},
  711. {}
  712. };
  713. MODULE_DEVICE_TABLE(i2c, tpm_st33_i2c_id);
  714. static SIMPLE_DEV_PM_OPS(tpm_st33_i2c_ops, tpm_st33_i2c_pm_suspend,
  715. tpm_st33_i2c_pm_resume);
  716. static struct i2c_driver tpm_st33_i2c_driver = {
  717. .driver = {
  718. .owner = THIS_MODULE,
  719. .name = TPM_ST33_I2C,
  720. .pm = &tpm_st33_i2c_ops,
  721. },
  722. .probe = tpm_st33_i2c_probe,
  723. .remove = tpm_st33_i2c_remove,
  724. .id_table = tpm_st33_i2c_id
  725. };
  726. module_i2c_driver(tpm_st33_i2c_driver);
  727. MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
  728. MODULE_DESCRIPTION("STM TPM I2C ST33 Driver");
  729. MODULE_VERSION("1.2.0");
  730. MODULE_LICENSE("GPL");