i2c.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24
  3. * Copyright (C) 2009 - 2015 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
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/gpio.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/tpm.h>
  24. #include <linux/platform_data/st33zp24.h>
  25. #include "st33zp24.h"
  26. #define TPM_DUMMY_BYTE 0xAA
  27. struct st33zp24_i2c_phy {
  28. struct i2c_client *client;
  29. u8 buf[TPM_BUFSIZE + 1];
  30. int io_lpcpd;
  31. };
  32. /*
  33. * write8_reg
  34. * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
  35. * @param: tpm_register, the tpm tis register where the data should be written
  36. * @param: tpm_data, the tpm_data to write inside the tpm_register
  37. * @param: tpm_size, The length of the data
  38. * @return: Returns negative errno, or else the number of bytes written.
  39. */
  40. static int write8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
  41. {
  42. struct st33zp24_i2c_phy *phy = phy_id;
  43. phy->buf[0] = tpm_register;
  44. memcpy(phy->buf + 1, tpm_data, tpm_size);
  45. return i2c_master_send(phy->client, phy->buf, tpm_size + 1);
  46. } /* write8_reg() */
  47. /*
  48. * read8_reg
  49. * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
  50. * @param: tpm_register, the tpm tis register where the data should be read
  51. * @param: tpm_data, the TPM response
  52. * @param: tpm_size, tpm TPM response size to read.
  53. * @return: number of byte read successfully: should be one if success.
  54. */
  55. static int read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
  56. {
  57. struct st33zp24_i2c_phy *phy = phy_id;
  58. u8 status = 0;
  59. u8 data;
  60. data = TPM_DUMMY_BYTE;
  61. status = write8_reg(phy, tpm_register, &data, 1);
  62. if (status == 2)
  63. status = i2c_master_recv(phy->client, tpm_data, tpm_size);
  64. return status;
  65. } /* read8_reg() */
  66. /*
  67. * st33zp24_i2c_send
  68. * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
  69. * @param: phy_id, the phy description
  70. * @param: tpm_register, the tpm tis register where the data should be written
  71. * @param: tpm_data, the tpm_data to write inside the tpm_register
  72. * @param: tpm_size, the length of the data
  73. * @return: number of byte written successfully: should be one if success.
  74. */
  75. static int st33zp24_i2c_send(void *phy_id, u8 tpm_register, u8 *tpm_data,
  76. int tpm_size)
  77. {
  78. return write8_reg(phy_id, tpm_register | TPM_WRITE_DIRECTION, tpm_data,
  79. tpm_size);
  80. }
  81. /*
  82. * st33zp24_i2c_recv
  83. * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
  84. * @param: phy_id, the phy description
  85. * @param: tpm_register, the tpm tis register where the data should be read
  86. * @param: tpm_data, the TPM response
  87. * @param: tpm_size, tpm TPM response size to read.
  88. * @return: number of byte read successfully: should be one if success.
  89. */
  90. static int st33zp24_i2c_recv(void *phy_id, u8 tpm_register, u8 *tpm_data,
  91. int tpm_size)
  92. {
  93. return read8_reg(phy_id, tpm_register, tpm_data, tpm_size);
  94. }
  95. static const struct st33zp24_phy_ops i2c_phy_ops = {
  96. .send = st33zp24_i2c_send,
  97. .recv = st33zp24_i2c_recv,
  98. };
  99. #ifdef CONFIG_OF
  100. static int st33zp24_i2c_of_request_resources(struct st33zp24_i2c_phy *phy)
  101. {
  102. struct device_node *pp;
  103. struct i2c_client *client = phy->client;
  104. int gpio;
  105. int ret;
  106. pp = client->dev.of_node;
  107. if (!pp) {
  108. dev_err(&client->dev, "No platform data\n");
  109. return -ENODEV;
  110. }
  111. /* Get GPIO from device tree */
  112. gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0);
  113. if (gpio < 0) {
  114. dev_err(&client->dev,
  115. "Failed to retrieve lpcpd-gpios from dts.\n");
  116. phy->io_lpcpd = -1;
  117. /*
  118. * lpcpd pin is not specified. This is not an issue as
  119. * power management can be also managed by TPM specific
  120. * commands. So leave with a success status code.
  121. */
  122. return 0;
  123. }
  124. /* GPIO request and configuration */
  125. ret = devm_gpio_request_one(&client->dev, gpio,
  126. GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD");
  127. if (ret) {
  128. dev_err(&client->dev, "Failed to request lpcpd pin\n");
  129. return -ENODEV;
  130. }
  131. phy->io_lpcpd = gpio;
  132. return 0;
  133. }
  134. #else
  135. static int st33zp24_i2c_of_request_resources(struct st33zp24_i2c_phy *phy)
  136. {
  137. return -ENODEV;
  138. }
  139. #endif
  140. static int st33zp24_i2c_request_resources(struct i2c_client *client,
  141. struct st33zp24_i2c_phy *phy)
  142. {
  143. struct st33zp24_platform_data *pdata;
  144. int ret;
  145. pdata = client->dev.platform_data;
  146. if (!pdata) {
  147. dev_err(&client->dev, "No platform data\n");
  148. return -ENODEV;
  149. }
  150. /* store for late use */
  151. phy->io_lpcpd = pdata->io_lpcpd;
  152. if (gpio_is_valid(pdata->io_lpcpd)) {
  153. ret = devm_gpio_request_one(&client->dev,
  154. pdata->io_lpcpd, GPIOF_OUT_INIT_HIGH,
  155. "TPM IO_LPCPD");
  156. if (ret) {
  157. dev_err(&client->dev, "Failed to request lpcpd pin\n");
  158. return ret;
  159. }
  160. }
  161. return 0;
  162. }
  163. /*
  164. * st33zp24_i2c_probe initialize the TPM device
  165. * @param: client, the i2c_client drescription (TPM I2C description).
  166. * @param: id, the i2c_device_id struct.
  167. * @return: 0 in case of success.
  168. * -1 in other case.
  169. */
  170. static int st33zp24_i2c_probe(struct i2c_client *client,
  171. const struct i2c_device_id *id)
  172. {
  173. int ret;
  174. struct st33zp24_platform_data *pdata;
  175. struct st33zp24_i2c_phy *phy;
  176. if (!client) {
  177. pr_info("%s: i2c client is NULL. Device not accessible.\n",
  178. __func__);
  179. return -ENODEV;
  180. }
  181. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  182. dev_info(&client->dev, "client not i2c capable\n");
  183. return -ENODEV;
  184. }
  185. phy = devm_kzalloc(&client->dev, sizeof(struct st33zp24_i2c_phy),
  186. GFP_KERNEL);
  187. if (!phy)
  188. return -ENOMEM;
  189. phy->client = client;
  190. pdata = client->dev.platform_data;
  191. if (!pdata && client->dev.of_node) {
  192. ret = st33zp24_i2c_of_request_resources(phy);
  193. if (ret)
  194. return ret;
  195. } else if (pdata) {
  196. ret = st33zp24_i2c_request_resources(client, phy);
  197. if (ret)
  198. return ret;
  199. }
  200. return st33zp24_probe(phy, &i2c_phy_ops, &client->dev, client->irq,
  201. phy->io_lpcpd);
  202. }
  203. /*
  204. * st33zp24_i2c_remove remove the TPM device
  205. * @param: client, the i2c_client description (TPM I2C description).
  206. * @return: 0 in case of success.
  207. */
  208. static int st33zp24_i2c_remove(struct i2c_client *client)
  209. {
  210. struct tpm_chip *chip = i2c_get_clientdata(client);
  211. return st33zp24_remove(chip);
  212. }
  213. static const struct i2c_device_id st33zp24_i2c_id[] = {
  214. {TPM_ST33_I2C, 0},
  215. {}
  216. };
  217. MODULE_DEVICE_TABLE(i2c, st33zp24_i2c_id);
  218. #ifdef CONFIG_OF
  219. static const struct of_device_id of_st33zp24_i2c_match[] = {
  220. { .compatible = "st,st33zp24-i2c", },
  221. {}
  222. };
  223. MODULE_DEVICE_TABLE(of, of_st33zp24_i2c_match);
  224. #endif
  225. static SIMPLE_DEV_PM_OPS(st33zp24_i2c_ops, st33zp24_pm_suspend,
  226. st33zp24_pm_resume);
  227. static struct i2c_driver st33zp24_i2c_driver = {
  228. .driver = {
  229. .name = TPM_ST33_I2C,
  230. .pm = &st33zp24_i2c_ops,
  231. .of_match_table = of_match_ptr(of_st33zp24_i2c_match),
  232. },
  233. .probe = st33zp24_i2c_probe,
  234. .remove = st33zp24_i2c_remove,
  235. .id_table = st33zp24_i2c_id
  236. };
  237. module_i2c_driver(st33zp24_i2c_driver);
  238. MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
  239. MODULE_DESCRIPTION("STM TPM 1.2 I2C ST33 Driver");
  240. MODULE_VERSION("1.3.0");
  241. MODULE_LICENSE("GPL");