tpm_i2c_stm_st33.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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;
  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)
  359. == 0) {
  360. burstcnt = get_burstcount(chip);
  361. if (burstcnt < 0)
  362. return burstcnt;
  363. len = min_t(int, burstcnt, count - size);
  364. I2C_READ_DATA(client, TPM_DATA_FIFO, buf + size, len);
  365. size += len;
  366. }
  367. return size;
  368. }
  369. /*
  370. * tpm_ioserirq_handler the serirq irq handler
  371. * @param: irq, the tpm chip description
  372. * @param: dev_id, the description of the chip
  373. * @return: the status of the handler.
  374. */
  375. static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
  376. {
  377. struct tpm_chip *chip = dev_id;
  378. struct i2c_client *client;
  379. struct st33zp24_platform_data *pin_infos;
  380. disable_irq_nosync(irq);
  381. client = (struct i2c_client *)TPM_VPRIV(chip);
  382. pin_infos = client->dev.platform_data;
  383. complete(&pin_infos->irq_detection);
  384. return IRQ_HANDLED;
  385. } /* tpm_ioserirq_handler() */
  386. /*
  387. * tpm_stm_i2c_send send TPM commands through the I2C bus.
  388. *
  389. * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
  390. * @param: buf, the buffer to send.
  391. * @param: count, the number of bytes to send.
  392. * @return: In case of success the number of bytes sent.
  393. * In other case, a < 0 value describing the issue.
  394. */
  395. static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,
  396. size_t len)
  397. {
  398. u32 status, i, size;
  399. int burstcnt = 0;
  400. int ret;
  401. u8 data;
  402. struct i2c_client *client;
  403. if (chip == NULL)
  404. return -EBUSY;
  405. if (len < TPM_HEADER_SIZE)
  406. return -EBUSY;
  407. client = (struct i2c_client *)TPM_VPRIV(chip);
  408. client->flags = 0;
  409. ret = request_locality(chip);
  410. if (ret < 0)
  411. return ret;
  412. status = tpm_stm_i2c_status(chip);
  413. if ((status & TPM_STS_COMMAND_READY) == 0) {
  414. tpm_stm_i2c_cancel(chip);
  415. if (wait_for_stat
  416. (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
  417. &chip->vendor.int_queue) < 0) {
  418. ret = -ETIME;
  419. goto out_err;
  420. }
  421. }
  422. for (i = 0; i < len - 1;) {
  423. burstcnt = get_burstcount(chip);
  424. if (burstcnt < 0)
  425. return burstcnt;
  426. size = min_t(int, len - i - 1, burstcnt);
  427. ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf, size);
  428. if (ret < 0)
  429. goto out_err;
  430. i += size;
  431. }
  432. status = tpm_stm_i2c_status(chip);
  433. if ((status & TPM_STS_DATA_EXPECT) == 0) {
  434. ret = -EIO;
  435. goto out_err;
  436. }
  437. ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf + len - 1, 1);
  438. if (ret < 0)
  439. goto out_err;
  440. status = tpm_stm_i2c_status(chip);
  441. if ((status & TPM_STS_DATA_EXPECT) != 0) {
  442. ret = -EIO;
  443. goto out_err;
  444. }
  445. data = TPM_STS_GO;
  446. I2C_WRITE_DATA(client, TPM_STS, &data, 1);
  447. return len;
  448. out_err:
  449. tpm_stm_i2c_cancel(chip);
  450. release_locality(chip);
  451. return ret;
  452. }
  453. /*
  454. * tpm_stm_i2c_recv received TPM response through the I2C bus.
  455. * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
  456. * @param: buf, the buffer to store datas.
  457. * @param: count, the number of bytes to send.
  458. * @return: In case of success the number of bytes received.
  459. * In other case, a < 0 value describing the issue.
  460. */
  461. static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,
  462. size_t count)
  463. {
  464. int size = 0;
  465. int expected;
  466. if (chip == NULL)
  467. return -EBUSY;
  468. if (count < TPM_HEADER_SIZE) {
  469. size = -EIO;
  470. goto out;
  471. }
  472. size = recv_data(chip, buf, TPM_HEADER_SIZE);
  473. if (size < TPM_HEADER_SIZE) {
  474. dev_err(chip->dev, "Unable to read header\n");
  475. goto out;
  476. }
  477. expected = be32_to_cpu(*(__be32 *)(buf + 2));
  478. if (expected > count) {
  479. size = -EIO;
  480. goto out;
  481. }
  482. size += recv_data(chip, &buf[TPM_HEADER_SIZE],
  483. expected - TPM_HEADER_SIZE);
  484. if (size < expected) {
  485. dev_err(chip->dev, "Unable to read remainder of result\n");
  486. size = -ETIME;
  487. goto out;
  488. }
  489. out:
  490. chip->ops->cancel(chip);
  491. release_locality(chip);
  492. return size;
  493. }
  494. static bool tpm_st33_i2c_req_canceled(struct tpm_chip *chip, u8 status)
  495. {
  496. return (status == TPM_STS_COMMAND_READY);
  497. }
  498. static const struct tpm_class_ops st_i2c_tpm = {
  499. .send = tpm_stm_i2c_send,
  500. .recv = tpm_stm_i2c_recv,
  501. .cancel = tpm_stm_i2c_cancel,
  502. .status = tpm_stm_i2c_status,
  503. .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  504. .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  505. .req_canceled = tpm_st33_i2c_req_canceled,
  506. };
  507. static int interrupts;
  508. module_param(interrupts, int, 0444);
  509. MODULE_PARM_DESC(interrupts, "Enable interrupts");
  510. static int power_mgt = 1;
  511. module_param(power_mgt, int, 0444);
  512. MODULE_PARM_DESC(power_mgt, "Power Management");
  513. /*
  514. * tpm_st33_i2c_probe initialize the TPM device
  515. * @param: client, the i2c_client drescription (TPM I2C description).
  516. * @param: id, the i2c_device_id struct.
  517. * @return: 0 in case of success.
  518. * -1 in other case.
  519. */
  520. static int
  521. tpm_st33_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  522. {
  523. int err;
  524. u8 intmask;
  525. struct tpm_chip *chip;
  526. struct st33zp24_platform_data *platform_data;
  527. if (client == NULL) {
  528. pr_info("%s: i2c client is NULL. Device not accessible.\n",
  529. __func__);
  530. err = -ENODEV;
  531. goto end;
  532. }
  533. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  534. dev_info(&client->dev, "client not i2c capable\n");
  535. err = -ENODEV;
  536. goto end;
  537. }
  538. chip = tpm_register_hardware(&client->dev, &st_i2c_tpm);
  539. if (!chip) {
  540. dev_info(&client->dev, "fail chip\n");
  541. err = -ENODEV;
  542. goto end;
  543. }
  544. platform_data = client->dev.platform_data;
  545. if (!platform_data) {
  546. dev_info(&client->dev, "chip not available\n");
  547. err = -ENODEV;
  548. goto _tpm_clean_answer;
  549. }
  550. platform_data->tpm_i2c_buffer[0] =
  551. kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
  552. if (platform_data->tpm_i2c_buffer[0] == NULL) {
  553. err = -ENOMEM;
  554. goto _tpm_clean_answer;
  555. }
  556. platform_data->tpm_i2c_buffer[1] =
  557. kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
  558. if (platform_data->tpm_i2c_buffer[1] == NULL) {
  559. err = -ENOMEM;
  560. goto _tpm_clean_response1;
  561. }
  562. TPM_VPRIV(chip) = client;
  563. chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  564. chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
  565. chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  566. chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  567. chip->vendor.locality = LOCALITY0;
  568. if (power_mgt) {
  569. err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
  570. if (err)
  571. goto _gpio_init1;
  572. gpio_set_value(platform_data->io_lpcpd, 1);
  573. }
  574. if (interrupts) {
  575. init_completion(&platform_data->irq_detection);
  576. if (request_locality(chip) != LOCALITY0) {
  577. err = -ENODEV;
  578. goto _tpm_clean_response2;
  579. }
  580. err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
  581. if (err)
  582. goto _gpio_init2;
  583. clear_interruption(client);
  584. err = request_irq(gpio_to_irq(platform_data->io_serirq),
  585. &tpm_ioserirq_handler,
  586. IRQF_TRIGGER_HIGH,
  587. "TPM SERIRQ management", chip);
  588. if (err < 0) {
  589. dev_err(chip->dev , "TPM SERIRQ signals %d not available\n",
  590. gpio_to_irq(platform_data->io_serirq));
  591. goto _irq_set;
  592. }
  593. err = I2C_READ_DATA(client, TPM_INT_ENABLE, &intmask, 1);
  594. if (err < 0)
  595. goto _irq_set;
  596. intmask |= TPM_INTF_CMD_READY_INT
  597. | TPM_INTF_FIFO_AVALAIBLE_INT
  598. | TPM_INTF_WAKE_UP_READY_INT
  599. | TPM_INTF_LOCALITY_CHANGE_INT
  600. | TPM_INTF_STS_VALID_INT
  601. | TPM_INTF_DATA_AVAIL_INT;
  602. err = I2C_WRITE_DATA(client, TPM_INT_ENABLE, &intmask, 1);
  603. if (err < 0)
  604. goto _irq_set;
  605. intmask = TPM_GLOBAL_INT_ENABLE;
  606. err = I2C_WRITE_DATA(client, (TPM_INT_ENABLE + 3), &intmask, 1);
  607. if (err < 0)
  608. goto _irq_set;
  609. err = I2C_READ_DATA(client, TPM_INT_STATUS, &intmask, 1);
  610. if (err < 0)
  611. goto _irq_set;
  612. chip->vendor.irq = interrupts;
  613. tpm_gen_interrupt(chip);
  614. }
  615. tpm_get_timeouts(chip);
  616. dev_info(chip->dev, "TPM I2C Initialized\n");
  617. return 0;
  618. _irq_set:
  619. free_irq(gpio_to_irq(platform_data->io_serirq), (void *)chip);
  620. _gpio_init2:
  621. if (interrupts)
  622. gpio_free(platform_data->io_serirq);
  623. _gpio_init1:
  624. if (power_mgt)
  625. gpio_free(platform_data->io_lpcpd);
  626. _tpm_clean_response2:
  627. kzfree(platform_data->tpm_i2c_buffer[1]);
  628. platform_data->tpm_i2c_buffer[1] = NULL;
  629. _tpm_clean_response1:
  630. kzfree(platform_data->tpm_i2c_buffer[0]);
  631. platform_data->tpm_i2c_buffer[0] = NULL;
  632. _tpm_clean_answer:
  633. tpm_remove_hardware(chip->dev);
  634. end:
  635. pr_info("TPM I2C initialisation fail\n");
  636. return err;
  637. }
  638. /*
  639. * tpm_st33_i2c_remove remove the TPM device
  640. * @param: client, the i2c_client drescription (TPM I2C description).
  641. clear_bit(0, &chip->is_open);
  642. * @return: 0 in case of success.
  643. */
  644. static int tpm_st33_i2c_remove(struct i2c_client *client)
  645. {
  646. struct tpm_chip *chip = (struct tpm_chip *)i2c_get_clientdata(client);
  647. struct st33zp24_platform_data *pin_infos =
  648. ((struct i2c_client *)TPM_VPRIV(chip))->dev.platform_data;
  649. if (pin_infos != NULL) {
  650. free_irq(pin_infos->io_serirq, chip);
  651. gpio_free(pin_infos->io_serirq);
  652. gpio_free(pin_infos->io_lpcpd);
  653. tpm_remove_hardware(chip->dev);
  654. if (pin_infos->tpm_i2c_buffer[1] != NULL) {
  655. kzfree(pin_infos->tpm_i2c_buffer[1]);
  656. pin_infos->tpm_i2c_buffer[1] = NULL;
  657. }
  658. if (pin_infos->tpm_i2c_buffer[0] != NULL) {
  659. kzfree(pin_infos->tpm_i2c_buffer[0]);
  660. pin_infos->tpm_i2c_buffer[0] = NULL;
  661. }
  662. }
  663. return 0;
  664. }
  665. #ifdef CONFIG_PM_SLEEP
  666. /*
  667. * tpm_st33_i2c_pm_suspend suspend the TPM device
  668. * @param: client, the i2c_client drescription (TPM I2C description).
  669. * @param: mesg, the power management message.
  670. * @return: 0 in case of success.
  671. */
  672. static int tpm_st33_i2c_pm_suspend(struct device *dev)
  673. {
  674. struct st33zp24_platform_data *pin_infos = dev->platform_data;
  675. int ret = 0;
  676. if (power_mgt) {
  677. gpio_set_value(pin_infos->io_lpcpd, 0);
  678. } else {
  679. ret = tpm_pm_suspend(dev);
  680. }
  681. return ret;
  682. } /* tpm_st33_i2c_suspend() */
  683. /*
  684. * tpm_st33_i2c_pm_resume resume the TPM device
  685. * @param: client, the i2c_client drescription (TPM I2C description).
  686. * @return: 0 in case of success.
  687. */
  688. static int tpm_st33_i2c_pm_resume(struct device *dev)
  689. {
  690. struct tpm_chip *chip = dev_get_drvdata(dev);
  691. struct st33zp24_platform_data *pin_infos = dev->platform_data;
  692. int ret = 0;
  693. if (power_mgt) {
  694. gpio_set_value(pin_infos->io_lpcpd, 1);
  695. ret = wait_for_serirq_timeout(chip,
  696. (chip->ops->status(chip) &
  697. TPM_STS_VALID) == TPM_STS_VALID,
  698. chip->vendor.timeout_b);
  699. } else {
  700. ret = tpm_pm_resume(dev);
  701. if (!ret)
  702. tpm_do_selftest(chip);
  703. }
  704. return ret;
  705. } /* tpm_st33_i2c_pm_resume() */
  706. #endif
  707. static const struct i2c_device_id tpm_st33_i2c_id[] = {
  708. {TPM_ST33_I2C, 0},
  709. {}
  710. };
  711. MODULE_DEVICE_TABLE(i2c, tpm_st33_i2c_id);
  712. static SIMPLE_DEV_PM_OPS(tpm_st33_i2c_ops, tpm_st33_i2c_pm_suspend,
  713. tpm_st33_i2c_pm_resume);
  714. static struct i2c_driver tpm_st33_i2c_driver = {
  715. .driver = {
  716. .owner = THIS_MODULE,
  717. .name = TPM_ST33_I2C,
  718. .pm = &tpm_st33_i2c_ops,
  719. },
  720. .probe = tpm_st33_i2c_probe,
  721. .remove = tpm_st33_i2c_remove,
  722. .id_table = tpm_st33_i2c_id
  723. };
  724. module_i2c_driver(tpm_st33_i2c_driver);
  725. MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
  726. MODULE_DESCRIPTION("STM TPM I2C ST33 Driver");
  727. MODULE_VERSION("1.2.0");
  728. MODULE_LICENSE("GPL");