tpm_st_i2c.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* Copyright (c) 2010, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/i2c.h>
  14. #include <linux/irq.h>
  15. #include <mach/gpio.h>
  16. #include <mach/tpm_st_i2c.h>
  17. #include <mach/msm_iomap.h>
  18. #include "tpm.h"
  19. #define DEVICE_NAME "tpm_st_i2c"
  20. #define TPM_HEADER_LEN sizeof(struct tpm_input_header)
  21. #define TPM_ST_I2C_BLOCK_MAX 40
  22. struct tpm_st_i2c_dev {
  23. struct i2c_client *client;
  24. struct tpm_st_i2c_platform_data *pd;
  25. struct completion com[2];
  26. };
  27. /* for completion array */
  28. #define ACCEPT_CMD_INDEX 0
  29. #define DATA_AVAIL_INDEX 1
  30. static struct tpm_st_i2c_dev *tpm_st_i2c_dev;
  31. #define TPM_ST_I2C_REQ_COMPLETE_MASK 1
  32. static u8 tpm_st_i2c_status(struct tpm_chip *chip)
  33. {
  34. int gpio = tpm_st_i2c_dev->pd->data_avail_gpio;
  35. return gpio_get_value(gpio);
  36. }
  37. static void tpm_st_i2c_cancel(struct tpm_chip *chip)
  38. {
  39. /* not supported */
  40. return;
  41. }
  42. static int tpm_st_i2c_transfer_buf(struct tpm_chip *chip, u8 *buf, size_t count,
  43. int recv)
  44. {
  45. struct i2c_msg msg = {
  46. .addr = tpm_st_i2c_dev->client->addr,
  47. .flags = 0,
  48. .buf = buf,
  49. .len = TPM_HEADER_LEN, /* must read/write header first */
  50. };
  51. int gpio;
  52. int irq;
  53. struct completion *com;
  54. __be32 *native_size;
  55. int read_header = 0;
  56. int rc = 0;
  57. int len = count;
  58. uint32_t size = count;
  59. int tmp;
  60. if (recv) {
  61. msg.flags |= I2C_M_RD;
  62. read_header = 1;
  63. gpio = tpm_st_i2c_dev->pd->data_avail_gpio;
  64. irq = tpm_st_i2c_dev->pd->data_avail_irq;
  65. com = &tpm_st_i2c_dev->com[DATA_AVAIL_INDEX];
  66. } else {
  67. gpio = tpm_st_i2c_dev->pd->accept_cmd_gpio;
  68. irq = tpm_st_i2c_dev->pd->accept_cmd_irq;
  69. com = &tpm_st_i2c_dev->com[ACCEPT_CMD_INDEX];
  70. }
  71. if (len < TPM_HEADER_LEN) {
  72. dev_dbg(chip->dev, "%s: invalid len\n", __func__);
  73. return -EINVAL;
  74. }
  75. do {
  76. if (!gpio_get_value(gpio)) {
  77. /* reset the completion in case the irq fired
  78. * during the probe
  79. */
  80. init_completion(com);
  81. enable_irq(irq);
  82. tmp = wait_for_completion_interruptible_timeout(
  83. com, HZ/2);
  84. if (!tmp) {
  85. dev_dbg(chip->dev, "%s timeout\n",
  86. __func__);
  87. return -EBUSY;
  88. }
  89. }
  90. rc = i2c_transfer(tpm_st_i2c_dev->client->adapter,
  91. &msg, 1);
  92. if (rc < 0) {
  93. dev_dbg(chip->dev, "Error in I2C transfer\n");
  94. return rc;
  95. }
  96. if (read_header) {
  97. read_header = 0;
  98. native_size = (__force __be32 *) (buf + 2);
  99. size = be32_to_cpu(*native_size);
  100. if (count < size) {
  101. dev_dbg(chip->dev,
  102. "%s: invalid count\n",
  103. __func__);
  104. rc = -EIO;
  105. }
  106. len = size;
  107. }
  108. len -= msg.len;
  109. if (len) {
  110. buf += msg.len;
  111. msg.buf = buf;
  112. if (len > TPM_ST_I2C_BLOCK_MAX)
  113. msg.len = TPM_ST_I2C_BLOCK_MAX;
  114. else
  115. msg.len = len;
  116. }
  117. } while (len > 0);
  118. if (rc >= 0)
  119. return size;
  120. else
  121. return rc;
  122. }
  123. static int tpm_st_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  124. {
  125. return tpm_st_i2c_transfer_buf(chip, buf, count, 1);
  126. }
  127. static int tpm_st_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
  128. {
  129. return tpm_st_i2c_transfer_buf(chip, buf, len, 0);
  130. }
  131. #ifdef CONFIG_PM
  132. static int tpm_st_i2c_suspend(struct i2c_client *client, pm_message_t msg)
  133. {
  134. return tpm_pm_suspend(&client->dev, msg);
  135. }
  136. static int tpm_st_i2c_resume(struct i2c_client *client)
  137. {
  138. return tpm_pm_resume(&client->dev);
  139. }
  140. #endif
  141. static const struct file_operations tpm_st_i2c_fs_ops = {
  142. .owner = THIS_MODULE,
  143. .llseek = no_llseek,
  144. .open = tpm_open,
  145. .read = tpm_read,
  146. .write = tpm_write,
  147. .release = tpm_release,
  148. };
  149. static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
  150. static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
  151. static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
  152. static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
  153. static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
  154. static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
  155. NULL);
  156. static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
  157. static struct attribute *tpm_st_i2c_attrs[] = {
  158. &dev_attr_pubek.attr,
  159. &dev_attr_pcrs.attr,
  160. &dev_attr_enabled.attr,
  161. &dev_attr_active.attr,
  162. &dev_attr_owned.attr,
  163. &dev_attr_temp_deactivated.attr,
  164. &dev_attr_caps.attr,
  165. NULL,
  166. };
  167. static struct attribute_group tpm_st_i2c_attr_grp = {
  168. .attrs = tpm_st_i2c_attrs
  169. };
  170. static struct tpm_vendor_specific tpm_st_i2c_vendor = {
  171. .status = tpm_st_i2c_status,
  172. .recv = tpm_st_i2c_recv,
  173. .send = tpm_st_i2c_send,
  174. .cancel = tpm_st_i2c_cancel,
  175. .req_complete_mask = TPM_ST_I2C_REQ_COMPLETE_MASK,
  176. .req_complete_val = TPM_ST_I2C_REQ_COMPLETE_MASK,
  177. .req_canceled = 0xff, /* not supported */
  178. .attr_group = &tpm_st_i2c_attr_grp,
  179. .miscdev = {
  180. .fops = &tpm_st_i2c_fs_ops,},
  181. };
  182. static irqreturn_t tpm_st_i2c_isr(int irq, void *dev_id)
  183. {
  184. disable_irq_nosync(irq);
  185. if (irq == tpm_st_i2c_dev->pd->accept_cmd_irq)
  186. complete(&tpm_st_i2c_dev->com[ACCEPT_CMD_INDEX]);
  187. else
  188. complete(&tpm_st_i2c_dev->com[DATA_AVAIL_INDEX]);
  189. return IRQ_HANDLED;
  190. }
  191. static int tpm_st_i2c_probe(struct i2c_client *client,
  192. const struct i2c_device_id *id)
  193. {
  194. int rc = 0;
  195. struct tpm_st_i2c_platform_data *pd;
  196. struct tpm_chip *chip;
  197. int high;
  198. dev_dbg(&client->dev, "%s()\n", __func__);
  199. if (!i2c_check_functionality(client->adapter,
  200. I2C_FUNC_SMBUS_BYTE |
  201. I2C_FUNC_SMBUS_I2C_BLOCK |
  202. I2C_FUNC_I2C)) {
  203. dev_err(&client->dev, "incompatible adapter\n");
  204. return -ENODEV;
  205. }
  206. pd = client->dev.platform_data;
  207. if (!pd || !pd->gpio_setup || !pd->gpio_release) {
  208. dev_err(&client->dev, "platform data not setup\n");
  209. rc = -EFAULT;
  210. goto no_platform_data;
  211. }
  212. rc = pd->gpio_setup();
  213. if (rc) {
  214. dev_err(&client->dev, "gpio_setup failed\n");
  215. goto gpio_setup_fail;
  216. }
  217. gpio_direction_input(pd->accept_cmd_gpio);
  218. gpio_direction_input(pd->data_avail_gpio);
  219. tpm_st_i2c_dev = kzalloc(sizeof(struct tpm_st_i2c_dev), GFP_KERNEL);
  220. if (!tpm_st_i2c_dev) {
  221. printk(KERN_ERR "%s Unable to allocate memory for struct\n",
  222. __func__);
  223. rc = -ENOMEM;
  224. goto kzalloc_fail;
  225. }
  226. tpm_st_i2c_dev->client = client;
  227. tpm_st_i2c_dev->pd = pd;
  228. init_completion(&tpm_st_i2c_dev->com[ACCEPT_CMD_INDEX]);
  229. init_completion(&tpm_st_i2c_dev->com[DATA_AVAIL_INDEX]);
  230. /* This logic allows us to setup irq but not have it enabled, in
  231. * case the lines are already active
  232. */
  233. high = gpio_get_value(pd->data_avail_gpio);
  234. rc = request_irq(pd->data_avail_irq, tpm_st_i2c_isr, IRQF_TRIGGER_HIGH,
  235. DEVICE_NAME "-data", NULL);
  236. if (rc) {
  237. dev_err(&client->dev, "request for data irq failed\n");
  238. goto data_irq_fail;
  239. }
  240. if (!high)
  241. disable_irq(pd->data_avail_irq);
  242. high = gpio_get_value(pd->accept_cmd_gpio);
  243. rc = request_irq(pd->accept_cmd_irq, tpm_st_i2c_isr, IRQF_TRIGGER_HIGH,
  244. DEVICE_NAME "-cmd", NULL);
  245. if (rc) {
  246. dev_err(&client->dev, "request for cmd irq failed\n");
  247. goto cmd_irq_fail;
  248. }
  249. if (!high)
  250. disable_irq(pd->accept_cmd_irq);
  251. tpm_st_i2c_vendor.irq = pd->data_avail_irq;
  252. chip = tpm_register_hardware(&client->dev, &tpm_st_i2c_vendor);
  253. if (!chip) {
  254. dev_err(&client->dev, "Could not register tpm hardware\n");
  255. rc = -ENODEV;
  256. goto tpm_reg_fail;
  257. }
  258. dev_info(&client->dev, "added\n");
  259. return 0;
  260. tpm_reg_fail:
  261. free_irq(pd->accept_cmd_irq, NULL);
  262. cmd_irq_fail:
  263. free_irq(pd->data_avail_irq, NULL);
  264. data_irq_fail:
  265. kzalloc_fail:
  266. pd->gpio_release();
  267. gpio_setup_fail:
  268. no_platform_data:
  269. return rc;
  270. }
  271. static int __exit tpm_st_i2c_remove(struct i2c_client *client)
  272. {
  273. free_irq(tpm_st_i2c_dev->pd->accept_cmd_irq, NULL);
  274. free_irq(tpm_st_i2c_dev->pd->data_avail_irq, NULL);
  275. tpm_remove_hardware(&client->dev);
  276. tpm_st_i2c_dev->pd->gpio_release();
  277. kfree(tpm_st_i2c_dev);
  278. return 0;
  279. }
  280. static const struct i2c_device_id tpm_st_i2c_id[] = {
  281. { DEVICE_NAME, 0 },
  282. { }
  283. };
  284. static struct i2c_driver tpm_st_i2c_driver = {
  285. .driver = {
  286. .name = DEVICE_NAME,
  287. .owner = THIS_MODULE,
  288. },
  289. .probe = tpm_st_i2c_probe,
  290. .remove = __exit_p(tpm_st_i2c_remove),
  291. #ifdef CONFIG_PM
  292. .suspend = tpm_st_i2c_suspend,
  293. .resume = tpm_st_i2c_resume,
  294. #endif
  295. .id_table = tpm_st_i2c_id,
  296. };
  297. static int __init tpm_st_i2c_init(void)
  298. {
  299. int ret;
  300. ret = i2c_add_driver(&tpm_st_i2c_driver);
  301. if (ret)
  302. printk(KERN_ERR "%s: failed to add i2c driver\n", __func__);
  303. return ret;
  304. }
  305. static void __exit tpm_st_i2c_exit(void)
  306. {
  307. i2c_del_driver(&tpm_st_i2c_driver);
  308. }
  309. module_init(tpm_st_i2c_init);
  310. module_exit(tpm_st_i2c_exit);
  311. MODULE_LICENSE("GPL v2");
  312. MODULE_VERSION("1.0");
  313. MODULE_AUTHOR("Qualcomm Innovation Center, Inc.");
  314. MODULE_DESCRIPTION("ST19NP18-TPM-I2C driver");