Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 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 | |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 33 | #include <linux/pci.h> |
| 34 | #include <linux/module.h> |
| 35 | #include <linux/platform_device.h> |
| 36 | #include <linux/i2c.h> |
| 37 | #include <linux/fs.h> |
| 38 | #include <linux/miscdevice.h> |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 39 | #include <linux/kernel.h> |
| 40 | #include <linux/delay.h> |
| 41 | #include <linux/init.h> |
| 42 | #include <linux/wait.h> |
| 43 | #include <linux/string.h> |
| 44 | #include <linux/interrupt.h> |
| 45 | #include <linux/spinlock.h> |
| 46 | #include <linux/sysfs.h> |
| 47 | #include <linux/gpio.h> |
| 48 | #include <linux/sched.h> |
| 49 | #include <linux/uaccess.h> |
| 50 | #include <linux/io.h> |
| 51 | #include <linux/slab.h> |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 52 | |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 53 | #include "tpm.h" |
Kent Yoder | 9da228e | 2013-01-28 07:52:44 -0600 | [diff] [blame] | 54 | #include "tpm_i2c_stm_st33.h" |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 55 | |
| 56 | enum stm33zp24_access { |
| 57 | TPM_ACCESS_VALID = 0x80, |
| 58 | TPM_ACCESS_ACTIVE_LOCALITY = 0x20, |
| 59 | TPM_ACCESS_REQUEST_PENDING = 0x04, |
| 60 | TPM_ACCESS_REQUEST_USE = 0x02, |
| 61 | }; |
| 62 | |
| 63 | enum stm33zp24_status { |
| 64 | TPM_STS_VALID = 0x80, |
| 65 | TPM_STS_COMMAND_READY = 0x40, |
| 66 | TPM_STS_GO = 0x20, |
| 67 | TPM_STS_DATA_AVAIL = 0x10, |
| 68 | TPM_STS_DATA_EXPECT = 0x08, |
| 69 | }; |
| 70 | |
| 71 | enum stm33zp24_int_flags { |
| 72 | TPM_GLOBAL_INT_ENABLE = 0x80, |
| 73 | TPM_INTF_CMD_READY_INT = 0x080, |
| 74 | TPM_INTF_FIFO_AVALAIBLE_INT = 0x040, |
| 75 | TPM_INTF_WAKE_UP_READY_INT = 0x020, |
| 76 | TPM_INTF_LOCALITY_CHANGE_INT = 0x004, |
| 77 | TPM_INTF_STS_VALID_INT = 0x002, |
| 78 | TPM_INTF_DATA_AVAIL_INT = 0x001, |
| 79 | }; |
| 80 | |
| 81 | enum tis_defaults { |
| 82 | TIS_SHORT_TIMEOUT = 750, |
| 83 | TIS_LONG_TIMEOUT = 2000, |
| 84 | }; |
| 85 | |
| 86 | /* |
| 87 | * write8_reg |
| 88 | * Send byte to the TIS register according to the ST33ZP24 I2C protocol. |
| 89 | * @param: tpm_register, the tpm tis register where the data should be written |
| 90 | * @param: tpm_data, the tpm_data to write inside the tpm_register |
| 91 | * @param: tpm_size, The length of the data |
| 92 | * @return: Returns negative errno, or else the number of bytes written. |
| 93 | */ |
| 94 | static int write8_reg(struct i2c_client *client, u8 tpm_register, |
| 95 | u8 *tpm_data, u16 tpm_size) |
| 96 | { |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 97 | struct st33zp24_platform_data *pin_infos; |
| 98 | |
| 99 | pin_infos = client->dev.platform_data; |
| 100 | |
Peter Huewe | 6429891 | 2013-01-29 22:01:59 +0100 | [diff] [blame] | 101 | pin_infos->tpm_i2c_buffer[0][0] = tpm_register; |
| 102 | memcpy(&pin_infos->tpm_i2c_buffer[0][1], tpm_data, tpm_size); |
Peter Huewe | 7333549 | 2013-01-29 22:02:02 +0100 | [diff] [blame] | 103 | return i2c_master_send(client, pin_infos->tpm_i2c_buffer[0], |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 104 | tpm_size + 1); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 105 | } /* write8_reg() */ |
| 106 | |
| 107 | /* |
| 108 | * read8_reg |
| 109 | * Recv byte from the TIS register according to the ST33ZP24 I2C protocol. |
| 110 | * @param: tpm_register, the tpm tis register where the data should be read |
| 111 | * @param: tpm_data, the TPM response |
| 112 | * @param: tpm_size, tpm TPM response size to read. |
| 113 | * @return: number of byte read successfully: should be one if success. |
| 114 | */ |
| 115 | static int read8_reg(struct i2c_client *client, u8 tpm_register, |
| 116 | u8 *tpm_data, int tpm_size) |
| 117 | { |
| 118 | u8 status = 0; |
| 119 | u8 data; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 120 | |
| 121 | data = TPM_DUMMY_BYTE; |
| 122 | status = write8_reg(client, tpm_register, &data, 1); |
| 123 | if (status == 2) |
| 124 | status = i2c_master_recv(client, tpm_data, tpm_size); |
| 125 | return status; |
| 126 | } /* read8_reg() */ |
| 127 | |
| 128 | /* |
| 129 | * I2C_WRITE_DATA |
| 130 | * Send byte to the TIS register according to the ST33ZP24 I2C protocol. |
| 131 | * @param: client, the chip description |
| 132 | * @param: tpm_register, the tpm tis register where the data should be written |
| 133 | * @param: tpm_data, the tpm_data to write inside the tpm_register |
| 134 | * @param: tpm_size, The length of the data |
| 135 | * @return: number of byte written successfully: should be one if success. |
| 136 | */ |
| 137 | #define I2C_WRITE_DATA(client, tpm_register, tpm_data, tpm_size) \ |
| 138 | (write8_reg(client, tpm_register | \ |
| 139 | TPM_WRITE_DIRECTION, tpm_data, tpm_size)) |
| 140 | |
| 141 | /* |
| 142 | * I2C_READ_DATA |
| 143 | * Recv byte from the TIS register according to the ST33ZP24 I2C protocol. |
| 144 | * @param: tpm, the chip description |
| 145 | * @param: tpm_register, the tpm tis register where the data should be read |
| 146 | * @param: tpm_data, the TPM response |
| 147 | * @param: tpm_size, tpm TPM response size to read. |
| 148 | * @return: number of byte read successfully: should be one if success. |
| 149 | */ |
| 150 | #define I2C_READ_DATA(client, tpm_register, tpm_data, tpm_size) \ |
| 151 | (read8_reg(client, tpm_register, tpm_data, tpm_size)) |
| 152 | |
| 153 | /* |
| 154 | * clear_interruption |
| 155 | * clear the TPM interrupt register. |
| 156 | * @param: tpm, the chip description |
| 157 | */ |
| 158 | static void clear_interruption(struct i2c_client *client) |
| 159 | { |
| 160 | u8 interrupt; |
| 161 | I2C_READ_DATA(client, TPM_INT_STATUS, &interrupt, 1); |
| 162 | I2C_WRITE_DATA(client, TPM_INT_STATUS, &interrupt, 1); |
| 163 | I2C_READ_DATA(client, TPM_INT_STATUS, &interrupt, 1); |
| 164 | } /* clear_interruption() */ |
| 165 | |
| 166 | /* |
| 167 | * _wait_for_interrupt_serirq_timeout |
| 168 | * @param: tpm, the chip description |
| 169 | * @param: timeout, the timeout of the interrupt |
| 170 | * @return: the status of the interruption. |
| 171 | */ |
| 172 | static long _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip, |
| 173 | unsigned long timeout) |
| 174 | { |
| 175 | long status; |
| 176 | struct i2c_client *client; |
| 177 | struct st33zp24_platform_data *pin_infos; |
| 178 | |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 179 | client = (struct i2c_client *)TPM_VPRIV(chip); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 180 | pin_infos = client->dev.platform_data; |
| 181 | |
| 182 | status = wait_for_completion_interruptible_timeout( |
| 183 | &pin_infos->irq_detection, |
| 184 | timeout); |
| 185 | if (status > 0) |
| 186 | enable_irq(gpio_to_irq(pin_infos->io_serirq)); |
| 187 | gpio_direction_input(pin_infos->io_serirq); |
| 188 | |
| 189 | return status; |
| 190 | } /* wait_for_interrupt_serirq_timeout() */ |
| 191 | |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 192 | static int wait_for_serirq_timeout(struct tpm_chip *chip, bool condition, |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 193 | unsigned long timeout) |
| 194 | { |
| 195 | int status = 2; |
| 196 | struct i2c_client *client; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 197 | |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 198 | client = (struct i2c_client *)TPM_VPRIV(chip); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 199 | |
| 200 | status = _wait_for_interrupt_serirq_timeout(chip, timeout); |
| 201 | if (!status) { |
| 202 | status = -EBUSY; |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 203 | } else { |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 204 | clear_interruption(client); |
| 205 | if (condition) |
| 206 | status = 1; |
| 207 | } |
| 208 | return status; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * tpm_stm_i2c_cancel, cancel is not implemented. |
| 213 | * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h |
| 214 | */ |
| 215 | static void tpm_stm_i2c_cancel(struct tpm_chip *chip) |
| 216 | { |
| 217 | struct i2c_client *client; |
| 218 | u8 data; |
| 219 | |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 220 | client = (struct i2c_client *)TPM_VPRIV(chip); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 221 | |
| 222 | data = TPM_STS_COMMAND_READY; |
| 223 | I2C_WRITE_DATA(client, TPM_STS, &data, 1); |
| 224 | if (chip->vendor.irq) |
| 225 | wait_for_serirq_timeout(chip, 1, chip->vendor.timeout_a); |
| 226 | } /* tpm_stm_i2c_cancel() */ |
| 227 | |
| 228 | /* |
| 229 | * tpm_stm_spi_status return the TPM_STS register |
| 230 | * @param: chip, the tpm chip description |
| 231 | * @return: the TPM_STS register value. |
| 232 | */ |
| 233 | static u8 tpm_stm_i2c_status(struct tpm_chip *chip) |
| 234 | { |
| 235 | struct i2c_client *client; |
| 236 | u8 data; |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 237 | client = (struct i2c_client *)TPM_VPRIV(chip); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 238 | |
| 239 | I2C_READ_DATA(client, TPM_STS, &data, 1); |
| 240 | return data; |
| 241 | } /* tpm_stm_i2c_status() */ |
| 242 | |
| 243 | |
| 244 | /* |
| 245 | * check_locality if the locality is active |
| 246 | * @param: chip, the tpm chip description |
| 247 | * @return: the active locality or -EACCESS. |
| 248 | */ |
| 249 | static int check_locality(struct tpm_chip *chip) |
| 250 | { |
| 251 | struct i2c_client *client; |
| 252 | u8 data; |
| 253 | u8 status; |
| 254 | |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 255 | client = (struct i2c_client *)TPM_VPRIV(chip); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 256 | |
| 257 | status = I2C_READ_DATA(client, TPM_ACCESS, &data, 1); |
| 258 | if (status && (data & |
| 259 | (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == |
| 260 | (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) |
| 261 | return chip->vendor.locality; |
| 262 | |
| 263 | return -EACCES; |
| 264 | |
| 265 | } /* check_locality() */ |
| 266 | |
| 267 | /* |
| 268 | * request_locality request the TPM locality |
| 269 | * @param: chip, the chip description |
| 270 | * @return: the active locality or EACCESS. |
| 271 | */ |
| 272 | static int request_locality(struct tpm_chip *chip) |
| 273 | { |
| 274 | unsigned long stop; |
| 275 | long rc; |
| 276 | struct i2c_client *client; |
| 277 | u8 data; |
| 278 | |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 279 | client = (struct i2c_client *)TPM_VPRIV(chip); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 280 | |
| 281 | if (check_locality(chip) == chip->vendor.locality) |
| 282 | return chip->vendor.locality; |
| 283 | |
| 284 | data = TPM_ACCESS_REQUEST_USE; |
| 285 | rc = I2C_WRITE_DATA(client, TPM_ACCESS, &data, 1); |
| 286 | if (rc < 0) |
| 287 | goto end; |
| 288 | |
| 289 | if (chip->vendor.irq) { |
| 290 | rc = wait_for_serirq_timeout(chip, (check_locality |
| 291 | (chip) >= 0), |
| 292 | chip->vendor.timeout_a); |
| 293 | if (rc > 0) |
| 294 | return chip->vendor.locality; |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 295 | } else { |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 296 | stop = jiffies + chip->vendor.timeout_a; |
| 297 | do { |
| 298 | if (check_locality(chip) >= 0) |
| 299 | return chip->vendor.locality; |
| 300 | msleep(TPM_TIMEOUT); |
| 301 | } while (time_before(jiffies, stop)); |
| 302 | } |
| 303 | rc = -EACCES; |
| 304 | end: |
| 305 | return rc; |
| 306 | } /* request_locality() */ |
| 307 | |
| 308 | /* |
| 309 | * release_locality release the active locality |
| 310 | * @param: chip, the tpm chip description. |
| 311 | */ |
| 312 | static void release_locality(struct tpm_chip *chip) |
| 313 | { |
| 314 | struct i2c_client *client; |
| 315 | u8 data; |
| 316 | |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 317 | client = (struct i2c_client *)TPM_VPRIV(chip); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 318 | data = TPM_ACCESS_ACTIVE_LOCALITY; |
| 319 | |
| 320 | I2C_WRITE_DATA(client, TPM_ACCESS, &data, 1); |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * get_burstcount return the burstcount address 0x19 0x1A |
| 325 | * @param: chip, the chip description |
| 326 | * return: the burstcount. |
| 327 | */ |
| 328 | static int get_burstcount(struct tpm_chip *chip) |
| 329 | { |
| 330 | unsigned long stop; |
| 331 | int burstcnt, status; |
| 332 | u8 tpm_reg, temp; |
| 333 | |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 334 | struct i2c_client *client = (struct i2c_client *)TPM_VPRIV(chip); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 335 | |
| 336 | stop = jiffies + chip->vendor.timeout_d; |
| 337 | do { |
| 338 | tpm_reg = TPM_STS + 1; |
| 339 | status = I2C_READ_DATA(client, tpm_reg, &temp, 1); |
| 340 | if (status < 0) |
| 341 | goto end; |
| 342 | |
| 343 | tpm_reg = tpm_reg + 1; |
| 344 | burstcnt = temp; |
| 345 | status = I2C_READ_DATA(client, tpm_reg, &temp, 1); |
| 346 | if (status < 0) |
| 347 | goto end; |
| 348 | |
| 349 | burstcnt |= temp << 8; |
| 350 | if (burstcnt) |
| 351 | return burstcnt; |
| 352 | msleep(TPM_TIMEOUT); |
| 353 | } while (time_before(jiffies, stop)); |
| 354 | |
| 355 | end: |
| 356 | return -EBUSY; |
| 357 | } /* get_burstcount() */ |
| 358 | |
| 359 | /* |
| 360 | * wait_for_stat wait for a TPM_STS value |
| 361 | * @param: chip, the tpm chip description |
| 362 | * @param: mask, the value mask to wait |
| 363 | * @param: timeout, the timeout |
| 364 | * @param: queue, the wait queue. |
| 365 | * @return: the tpm status, 0 if success, -ETIME if timeout is reached. |
| 366 | */ |
| 367 | static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, |
| 368 | wait_queue_head_t *queue) |
| 369 | { |
| 370 | unsigned long stop; |
| 371 | long rc; |
| 372 | u8 status; |
| 373 | |
| 374 | if (chip->vendor.irq) { |
| 375 | rc = wait_for_serirq_timeout(chip, ((tpm_stm_i2c_status |
| 376 | (chip) & mask) == |
| 377 | mask), timeout); |
| 378 | if (rc > 0) |
| 379 | return 0; |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 380 | } else { |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 381 | stop = jiffies + timeout; |
| 382 | do { |
| 383 | msleep(TPM_TIMEOUT); |
| 384 | status = tpm_stm_i2c_status(chip); |
| 385 | if ((status & mask) == mask) |
| 386 | return 0; |
| 387 | } while (time_before(jiffies, stop)); |
| 388 | } |
| 389 | return -ETIME; |
| 390 | } /* wait_for_stat() */ |
| 391 | |
| 392 | /* |
| 393 | * recv_data receive data |
| 394 | * @param: chip, the tpm chip description |
| 395 | * @param: buf, the buffer where the data are received |
| 396 | * @param: count, the number of data to receive |
| 397 | * @return: the number of bytes read from TPM FIFO. |
| 398 | */ |
| 399 | static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) |
| 400 | { |
| 401 | int size = 0, burstcnt, len; |
| 402 | struct i2c_client *client; |
| 403 | |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 404 | client = (struct i2c_client *)TPM_VPRIV(chip); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 405 | |
| 406 | while (size < count && |
| 407 | wait_for_stat(chip, |
| 408 | TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
| 409 | chip->vendor.timeout_c, |
| 410 | &chip->vendor.read_queue) |
| 411 | == 0) { |
| 412 | burstcnt = get_burstcount(chip); |
Peter Huewe | 9bf4d6c | 2013-10-30 00:54:20 +0100 | [diff] [blame] | 413 | if (burstcnt < 0) |
| 414 | return burstcnt; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 415 | len = min_t(int, burstcnt, count - size); |
| 416 | I2C_READ_DATA(client, TPM_DATA_FIFO, buf + size, len); |
| 417 | size += len; |
| 418 | } |
| 419 | return size; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * tpm_ioserirq_handler the serirq irq handler |
| 424 | * @param: irq, the tpm chip description |
| 425 | * @param: dev_id, the description of the chip |
| 426 | * @return: the status of the handler. |
| 427 | */ |
| 428 | static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id) |
| 429 | { |
| 430 | struct tpm_chip *chip = dev_id; |
| 431 | struct i2c_client *client; |
| 432 | struct st33zp24_platform_data *pin_infos; |
| 433 | |
| 434 | disable_irq_nosync(irq); |
| 435 | |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 436 | client = (struct i2c_client *)TPM_VPRIV(chip); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 437 | pin_infos = client->dev.platform_data; |
| 438 | |
| 439 | complete(&pin_infos->irq_detection); |
| 440 | return IRQ_HANDLED; |
| 441 | } /* tpm_ioserirq_handler() */ |
| 442 | |
| 443 | |
| 444 | /* |
| 445 | * tpm_stm_i2c_send send TPM commands through the I2C bus. |
| 446 | * |
| 447 | * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h |
| 448 | * @param: buf, the buffer to send. |
| 449 | * @param: count, the number of bytes to send. |
| 450 | * @return: In case of success the number of bytes sent. |
| 451 | * In other case, a < 0 value describing the issue. |
| 452 | */ |
| 453 | static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, |
| 454 | size_t len) |
| 455 | { |
Peter Huewe | 9bf4d6c | 2013-10-30 00:54:20 +0100 | [diff] [blame] | 456 | u32 status, i, size; |
| 457 | int burstcnt = 0; |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 458 | int ret; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 459 | u8 data; |
| 460 | struct i2c_client *client; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 461 | |
| 462 | if (chip == NULL) |
| 463 | return -EBUSY; |
| 464 | if (len < TPM_HEADER_SIZE) |
| 465 | return -EBUSY; |
| 466 | |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 467 | client = (struct i2c_client *)TPM_VPRIV(chip); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 468 | |
| 469 | client->flags = 0; |
| 470 | |
| 471 | ret = request_locality(chip); |
| 472 | if (ret < 0) |
| 473 | return ret; |
| 474 | |
| 475 | status = tpm_stm_i2c_status(chip); |
| 476 | if ((status & TPM_STS_COMMAND_READY) == 0) { |
| 477 | tpm_stm_i2c_cancel(chip); |
| 478 | if (wait_for_stat |
| 479 | (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b, |
| 480 | &chip->vendor.int_queue) < 0) { |
| 481 | ret = -ETIME; |
| 482 | goto out_err; |
| 483 | } |
| 484 | } |
| 485 | |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 486 | for (i = 0; i < len - 1;) { |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 487 | burstcnt = get_burstcount(chip); |
Peter Huewe | 9bf4d6c | 2013-10-30 00:54:20 +0100 | [diff] [blame] | 488 | if (burstcnt < 0) |
| 489 | return burstcnt; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 490 | size = min_t(int, len - i - 1, burstcnt); |
Christophe Ricard | 6280501 | 2014-12-01 19:32:46 +0100 | [diff] [blame] | 491 | ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf + i, size); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 492 | if (ret < 0) |
| 493 | goto out_err; |
| 494 | |
| 495 | i += size; |
| 496 | } |
| 497 | |
| 498 | status = tpm_stm_i2c_status(chip); |
| 499 | if ((status & TPM_STS_DATA_EXPECT) == 0) { |
| 500 | ret = -EIO; |
| 501 | goto out_err; |
| 502 | } |
| 503 | |
| 504 | ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf + len - 1, 1); |
| 505 | if (ret < 0) |
| 506 | goto out_err; |
| 507 | |
| 508 | status = tpm_stm_i2c_status(chip); |
| 509 | if ((status & TPM_STS_DATA_EXPECT) != 0) { |
| 510 | ret = -EIO; |
| 511 | goto out_err; |
| 512 | } |
| 513 | |
| 514 | data = TPM_STS_GO; |
| 515 | I2C_WRITE_DATA(client, TPM_STS, &data, 1); |
| 516 | |
| 517 | return len; |
| 518 | out_err: |
| 519 | tpm_stm_i2c_cancel(chip); |
| 520 | release_locality(chip); |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * tpm_stm_i2c_recv received TPM response through the I2C bus. |
| 526 | * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h. |
| 527 | * @param: buf, the buffer to store datas. |
| 528 | * @param: count, the number of bytes to send. |
| 529 | * @return: In case of success the number of bytes received. |
| 530 | * In other case, a < 0 value describing the issue. |
| 531 | */ |
| 532 | static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf, |
| 533 | size_t count) |
| 534 | { |
| 535 | int size = 0; |
| 536 | int expected; |
| 537 | |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 538 | if (chip == NULL) |
| 539 | return -EBUSY; |
| 540 | |
| 541 | if (count < TPM_HEADER_SIZE) { |
| 542 | size = -EIO; |
| 543 | goto out; |
| 544 | } |
| 545 | |
| 546 | size = recv_data(chip, buf, TPM_HEADER_SIZE); |
| 547 | if (size < TPM_HEADER_SIZE) { |
| 548 | dev_err(chip->dev, "Unable to read header\n"); |
| 549 | goto out; |
| 550 | } |
| 551 | |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 552 | expected = be32_to_cpu(*(__be32 *)(buf + 2)); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 553 | if (expected > count) { |
| 554 | size = -EIO; |
| 555 | goto out; |
| 556 | } |
| 557 | |
| 558 | size += recv_data(chip, &buf[TPM_HEADER_SIZE], |
| 559 | expected - TPM_HEADER_SIZE); |
| 560 | if (size < expected) { |
| 561 | dev_err(chip->dev, "Unable to read remainder of result\n"); |
| 562 | size = -ETIME; |
| 563 | goto out; |
| 564 | } |
| 565 | |
| 566 | out: |
| 567 | chip->vendor.cancel(chip); |
| 568 | release_locality(chip); |
| 569 | return size; |
| 570 | } |
| 571 | |
Stefan Berger | 1f86605 | 2013-01-22 13:52:35 -0600 | [diff] [blame] | 572 | static bool tpm_st33_i2c_req_canceled(struct tpm_chip *chip, u8 status) |
| 573 | { |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 574 | return (status == TPM_STS_COMMAND_READY); |
Stefan Berger | 1f86605 | 2013-01-22 13:52:35 -0600 | [diff] [blame] | 575 | } |
| 576 | |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 577 | static const struct file_operations tpm_st33_i2c_fops = { |
| 578 | .owner = THIS_MODULE, |
| 579 | .llseek = no_llseek, |
| 580 | .read = tpm_read, |
| 581 | .write = tpm_write, |
| 582 | .open = tpm_open, |
| 583 | .release = tpm_release, |
| 584 | }; |
| 585 | |
| 586 | static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL); |
| 587 | static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL); |
| 588 | static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL); |
| 589 | static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL); |
| 590 | static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL); |
| 591 | static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL); |
| 592 | static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL); |
| 593 | static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel); |
| 594 | |
| 595 | static struct attribute *stm_tpm_attrs[] = { |
| 596 | &dev_attr_pubek.attr, |
| 597 | &dev_attr_pcrs.attr, |
| 598 | &dev_attr_enabled.attr, |
| 599 | &dev_attr_active.attr, |
| 600 | &dev_attr_owned.attr, |
| 601 | &dev_attr_temp_deactivated.attr, |
| 602 | &dev_attr_caps.attr, |
| 603 | &dev_attr_cancel.attr, NULL, |
| 604 | }; |
| 605 | |
| 606 | static struct attribute_group stm_tpm_attr_grp = { |
| 607 | .attrs = stm_tpm_attrs |
| 608 | }; |
| 609 | |
| 610 | static struct tpm_vendor_specific st_i2c_tpm = { |
| 611 | .send = tpm_stm_i2c_send, |
| 612 | .recv = tpm_stm_i2c_recv, |
| 613 | .cancel = tpm_stm_i2c_cancel, |
| 614 | .status = tpm_stm_i2c_status, |
| 615 | .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
| 616 | .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
Stefan Berger | 1f86605 | 2013-01-22 13:52:35 -0600 | [diff] [blame] | 617 | .req_canceled = tpm_st33_i2c_req_canceled, |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 618 | .attr_group = &stm_tpm_attr_grp, |
| 619 | .miscdev = {.fops = &tpm_st33_i2c_fops,}, |
| 620 | }; |
| 621 | |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 622 | static int interrupts; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 623 | module_param(interrupts, int, 0444); |
| 624 | MODULE_PARM_DESC(interrupts, "Enable interrupts"); |
| 625 | |
| 626 | static int power_mgt = 1; |
| 627 | module_param(power_mgt, int, 0444); |
| 628 | MODULE_PARM_DESC(power_mgt, "Power Management"); |
| 629 | |
| 630 | /* |
| 631 | * tpm_st33_i2c_probe initialize the TPM device |
| 632 | * @param: client, the i2c_client drescription (TPM I2C description). |
| 633 | * @param: id, the i2c_device_id struct. |
| 634 | * @return: 0 in case of success. |
| 635 | * -1 in other case. |
| 636 | */ |
| 637 | static int |
| 638 | tpm_st33_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 639 | { |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 640 | int err; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 641 | u8 intmask; |
| 642 | struct tpm_chip *chip; |
| 643 | struct st33zp24_platform_data *platform_data; |
| 644 | |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 645 | if (client == NULL) { |
Kent Yoder | 1fbc5e9 | 2013-01-18 17:42:25 -0600 | [diff] [blame] | 646 | pr_info("%s: i2c client is NULL. Device not accessible.\n", |
| 647 | __func__); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 648 | err = -ENODEV; |
| 649 | goto end; |
| 650 | } |
| 651 | |
| 652 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 653 | dev_info(&client->dev, "client not i2c capable\n"); |
| 654 | err = -ENODEV; |
| 655 | goto end; |
| 656 | } |
| 657 | |
| 658 | chip = tpm_register_hardware(&client->dev, &st_i2c_tpm); |
| 659 | if (!chip) { |
| 660 | dev_info(&client->dev, "fail chip\n"); |
| 661 | err = -ENODEV; |
| 662 | goto end; |
| 663 | } |
| 664 | |
| 665 | platform_data = client->dev.platform_data; |
Kent Yoder | 1fbc5e9 | 2013-01-18 17:42:25 -0600 | [diff] [blame] | 666 | |
| 667 | if (!platform_data) { |
| 668 | dev_info(&client->dev, "chip not available\n"); |
| 669 | err = -ENODEV; |
| 670 | goto _tpm_clean_answer; |
| 671 | } |
| 672 | |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 673 | platform_data->tpm_i2c_buffer[0] = |
| 674 | kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL); |
| 675 | if (platform_data->tpm_i2c_buffer[0] == NULL) { |
| 676 | err = -ENOMEM; |
| 677 | goto _tpm_clean_answer; |
| 678 | } |
| 679 | platform_data->tpm_i2c_buffer[1] = |
| 680 | kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL); |
| 681 | if (platform_data->tpm_i2c_buffer[1] == NULL) { |
| 682 | err = -ENOMEM; |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 683 | goto _tpm_clean_response1; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 684 | } |
| 685 | |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 686 | TPM_VPRIV(chip) = client; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 687 | |
| 688 | chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT); |
| 689 | chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT); |
| 690 | chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT); |
| 691 | chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT); |
| 692 | |
| 693 | chip->vendor.locality = LOCALITY0; |
| 694 | |
| 695 | if (power_mgt) { |
| 696 | err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD"); |
| 697 | if (err) |
| 698 | goto _gpio_init1; |
| 699 | gpio_set_value(platform_data->io_lpcpd, 1); |
| 700 | } |
| 701 | |
| 702 | if (interrupts) { |
| 703 | init_completion(&platform_data->irq_detection); |
| 704 | if (request_locality(chip) != LOCALITY0) { |
| 705 | err = -ENODEV; |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 706 | goto _tpm_clean_response2; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 707 | } |
| 708 | err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ"); |
| 709 | if (err) |
| 710 | goto _gpio_init2; |
| 711 | |
| 712 | clear_interruption(client); |
| 713 | err = request_irq(gpio_to_irq(platform_data->io_serirq), |
| 714 | &tpm_ioserirq_handler, |
| 715 | IRQF_TRIGGER_HIGH, |
| 716 | "TPM SERIRQ management", chip); |
| 717 | if (err < 0) { |
| 718 | dev_err(chip->dev , "TPM SERIRQ signals %d not available\n", |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 719 | gpio_to_irq(platform_data->io_serirq)); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 720 | goto _irq_set; |
| 721 | } |
| 722 | |
| 723 | err = I2C_READ_DATA(client, TPM_INT_ENABLE, &intmask, 1); |
| 724 | if (err < 0) |
| 725 | goto _irq_set; |
| 726 | |
| 727 | intmask |= TPM_INTF_CMD_READY_INT |
| 728 | | TPM_INTF_FIFO_AVALAIBLE_INT |
| 729 | | TPM_INTF_WAKE_UP_READY_INT |
| 730 | | TPM_INTF_LOCALITY_CHANGE_INT |
| 731 | | TPM_INTF_STS_VALID_INT |
| 732 | | TPM_INTF_DATA_AVAIL_INT; |
| 733 | |
| 734 | err = I2C_WRITE_DATA(client, TPM_INT_ENABLE, &intmask, 1); |
| 735 | if (err < 0) |
| 736 | goto _irq_set; |
| 737 | |
| 738 | intmask = TPM_GLOBAL_INT_ENABLE; |
| 739 | err = I2C_WRITE_DATA(client, (TPM_INT_ENABLE + 3), &intmask, 1); |
| 740 | if (err < 0) |
| 741 | goto _irq_set; |
| 742 | |
| 743 | err = I2C_READ_DATA(client, TPM_INT_STATUS, &intmask, 1); |
| 744 | if (err < 0) |
| 745 | goto _irq_set; |
| 746 | |
| 747 | chip->vendor.irq = interrupts; |
| 748 | |
| 749 | tpm_gen_interrupt(chip); |
| 750 | } |
| 751 | |
| 752 | tpm_get_timeouts(chip); |
| 753 | |
| 754 | i2c_set_clientdata(client, chip); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 755 | |
| 756 | dev_info(chip->dev, "TPM I2C Initialized\n"); |
| 757 | return 0; |
| 758 | _irq_set: |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 759 | free_irq(gpio_to_irq(platform_data->io_serirq), (void *)chip); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 760 | _gpio_init2: |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 761 | if (interrupts) |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 762 | gpio_free(platform_data->io_serirq); |
| 763 | _gpio_init1: |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 764 | if (power_mgt) |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 765 | gpio_free(platform_data->io_lpcpd); |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 766 | _tpm_clean_response2: |
| 767 | kzfree(platform_data->tpm_i2c_buffer[1]); |
| 768 | platform_data->tpm_i2c_buffer[1] = NULL; |
| 769 | _tpm_clean_response1: |
| 770 | kzfree(platform_data->tpm_i2c_buffer[0]); |
| 771 | platform_data->tpm_i2c_buffer[0] = NULL; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 772 | _tpm_clean_answer: |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 773 | tpm_remove_hardware(chip->dev); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 774 | end: |
| 775 | pr_info("TPM I2C initialisation fail\n"); |
| 776 | return err; |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * tpm_st33_i2c_remove remove the TPM device |
| 781 | * @param: client, the i2c_client drescription (TPM I2C description). |
| 782 | clear_bit(0, &chip->is_open); |
| 783 | * @return: 0 in case of success. |
| 784 | */ |
Peter Huewe | e02983c | 2013-01-29 22:02:00 +0100 | [diff] [blame] | 785 | static int tpm_st33_i2c_remove(struct i2c_client *client) |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 786 | { |
| 787 | struct tpm_chip *chip = (struct tpm_chip *)i2c_get_clientdata(client); |
| 788 | struct st33zp24_platform_data *pin_infos = |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 789 | ((struct i2c_client *)TPM_VPRIV(chip))->dev.platform_data; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 790 | |
| 791 | if (pin_infos != NULL) { |
| 792 | free_irq(pin_infos->io_serirq, chip); |
| 793 | |
| 794 | gpio_free(pin_infos->io_serirq); |
| 795 | gpio_free(pin_infos->io_lpcpd); |
| 796 | |
Kent Yoder | 1fbc5e9 | 2013-01-18 17:42:25 -0600 | [diff] [blame] | 797 | tpm_remove_hardware(chip->dev); |
| 798 | |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 799 | if (pin_infos->tpm_i2c_buffer[1] != NULL) { |
| 800 | kzfree(pin_infos->tpm_i2c_buffer[1]); |
| 801 | pin_infos->tpm_i2c_buffer[1] = NULL; |
| 802 | } |
| 803 | if (pin_infos->tpm_i2c_buffer[0] != NULL) { |
| 804 | kzfree(pin_infos->tpm_i2c_buffer[0]); |
| 805 | pin_infos->tpm_i2c_buffer[0] = NULL; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
Peter Huewe | d459335 | 2012-12-06 01:20:51 +0100 | [diff] [blame] | 812 | #ifdef CONFIG_PM_SLEEP |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 813 | /* |
| 814 | * tpm_st33_i2c_pm_suspend suspend the TPM device |
| 815 | * Added: Work around when suspend and no tpm application is running, suspend |
| 816 | * may fail because chip->data_buffer is not set (only set in tpm_open in Linux |
| 817 | * TPM core) |
| 818 | * @param: client, the i2c_client drescription (TPM I2C description). |
| 819 | * @param: mesg, the power management message. |
| 820 | * @return: 0 in case of success. |
| 821 | */ |
Peter Huewe | d459335 | 2012-12-06 01:20:51 +0100 | [diff] [blame] | 822 | static int tpm_st33_i2c_pm_suspend(struct device *dev) |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 823 | { |
Peter Huewe | d459335 | 2012-12-06 01:20:51 +0100 | [diff] [blame] | 824 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 825 | struct st33zp24_platform_data *pin_infos = dev->platform_data; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 826 | int ret = 0; |
| 827 | |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 828 | if (power_mgt) { |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 829 | gpio_set_value(pin_infos->io_lpcpd, 0); |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 830 | } else { |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 831 | if (chip->data_buffer == NULL) |
| 832 | chip->data_buffer = pin_infos->tpm_i2c_buffer[0]; |
Peter Huewe | d459335 | 2012-12-06 01:20:51 +0100 | [diff] [blame] | 833 | ret = tpm_pm_suspend(dev); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 834 | } |
| 835 | return ret; |
| 836 | } /* tpm_st33_i2c_suspend() */ |
| 837 | |
| 838 | /* |
| 839 | * tpm_st33_i2c_pm_resume resume the TPM device |
| 840 | * @param: client, the i2c_client drescription (TPM I2C description). |
| 841 | * @return: 0 in case of success. |
| 842 | */ |
Peter Huewe | d459335 | 2012-12-06 01:20:51 +0100 | [diff] [blame] | 843 | static int tpm_st33_i2c_pm_resume(struct device *dev) |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 844 | { |
Peter Huewe | d459335 | 2012-12-06 01:20:51 +0100 | [diff] [blame] | 845 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 846 | struct st33zp24_platform_data *pin_infos = dev->platform_data; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 847 | |
| 848 | int ret = 0; |
| 849 | |
| 850 | if (power_mgt) { |
| 851 | gpio_set_value(pin_infos->io_lpcpd, 1); |
| 852 | ret = wait_for_serirq_timeout(chip, |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 853 | (chip->vendor.status(chip) & |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 854 | TPM_STS_VALID) == TPM_STS_VALID, |
| 855 | chip->vendor.timeout_b); |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 856 | } else { |
| 857 | if (chip->data_buffer == NULL) |
| 858 | chip->data_buffer = pin_infos->tpm_i2c_buffer[0]; |
| 859 | ret = tpm_pm_resume(dev); |
| 860 | if (!ret) |
| 861 | tpm_do_selftest(chip); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 862 | } |
| 863 | return ret; |
| 864 | } /* tpm_st33_i2c_pm_resume() */ |
Peter Huewe | d459335 | 2012-12-06 01:20:51 +0100 | [diff] [blame] | 865 | #endif |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 866 | |
| 867 | static const struct i2c_device_id tpm_st33_i2c_id[] = { |
| 868 | {TPM_ST33_I2C, 0}, |
| 869 | {} |
| 870 | }; |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 871 | MODULE_DEVICE_TABLE(i2c, tpm_st33_i2c_id); |
Peter Huewe | 2d089f8 | 2013-02-28 16:15:48 -0600 | [diff] [blame] | 872 | static SIMPLE_DEV_PM_OPS(tpm_st33_i2c_ops, tpm_st33_i2c_pm_suspend, |
| 873 | tpm_st33_i2c_pm_resume); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 874 | static struct i2c_driver tpm_st33_i2c_driver = { |
| 875 | .driver = { |
| 876 | .owner = THIS_MODULE, |
| 877 | .name = TPM_ST33_I2C, |
Peter Huewe | d459335 | 2012-12-06 01:20:51 +0100 | [diff] [blame] | 878 | .pm = &tpm_st33_i2c_ops, |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 879 | }, |
| 880 | .probe = tpm_st33_i2c_probe, |
| 881 | .remove = tpm_st33_i2c_remove, |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 882 | .id_table = tpm_st33_i2c_id |
| 883 | }; |
| 884 | |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 885 | module_i2c_driver(tpm_st33_i2c_driver); |
Mathias Leblanc | 251a7b0 | 2012-11-28 18:22:24 +0100 | [diff] [blame] | 886 | |
| 887 | MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)"); |
| 888 | MODULE_DESCRIPTION("STM TPM I2C ST33 Driver"); |
| 889 | MODULE_VERSION("1.2.0"); |
Kent Yoder | 3d7a7bd | 2012-12-05 16:52:43 -0600 | [diff] [blame] | 890 | MODULE_LICENSE("GPL"); |