linux/drivers/char/tpm/st33zp24/i2c.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24
   4 * Copyright (C) 2009 - 2016 STMicroelectronics
   5 */
   6
   7#include <linux/module.h>
   8#include <linux/i2c.h>
   9#include <linux/gpio.h>
  10#include <linux/gpio/consumer.h>
  11#include <linux/of_irq.h>
  12#include <linux/of_gpio.h>
  13#include <linux/acpi.h>
  14#include <linux/tpm.h>
  15#include <linux/platform_data/st33zp24.h>
  16
  17#include "../tpm.h"
  18#include "st33zp24.h"
  19
  20#define TPM_DUMMY_BYTE                  0xAA
  21
  22struct st33zp24_i2c_phy {
  23        struct i2c_client *client;
  24        u8 buf[ST33ZP24_BUFSIZE + 1];
  25        int io_lpcpd;
  26};
  27
  28/*
  29 * write8_reg
  30 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
  31 * @param: tpm_register, the tpm tis register where the data should be written
  32 * @param: tpm_data, the tpm_data to write inside the tpm_register
  33 * @param: tpm_size, The length of the data
  34 * @return: Returns negative errno, or else the number of bytes written.
  35 */
  36static int write8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
  37{
  38        struct st33zp24_i2c_phy *phy = phy_id;
  39
  40        phy->buf[0] = tpm_register;
  41        memcpy(phy->buf + 1, tpm_data, tpm_size);
  42        return i2c_master_send(phy->client, phy->buf, tpm_size + 1);
  43} /* write8_reg() */
  44
  45/*
  46 * read8_reg
  47 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
  48 * @param: tpm_register, the tpm tis register where the data should be read
  49 * @param: tpm_data, the TPM response
  50 * @param: tpm_size, tpm TPM response size to read.
  51 * @return: number of byte read successfully: should be one if success.
  52 */
  53static int read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
  54{
  55        struct st33zp24_i2c_phy *phy = phy_id;
  56        u8 status = 0;
  57        u8 data;
  58
  59        data = TPM_DUMMY_BYTE;
  60        status = write8_reg(phy, tpm_register, &data, 1);
  61        if (status == 2)
  62                status = i2c_master_recv(phy->client, tpm_data, tpm_size);
  63        return status;
  64} /* read8_reg() */
  65
  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 */
  75static 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/*
  83 * st33zp24_i2c_recv
  84 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
  85 * @param: phy_id, the phy description
  86 * @param: tpm_register, the tpm tis register where the data should be read
  87 * @param: tpm_data, the TPM response
  88 * @param: tpm_size, tpm TPM response size to read.
  89 * @return: number of byte read successfully: should be one if success.
  90 */
  91static int st33zp24_i2c_recv(void *phy_id, u8 tpm_register, u8 *tpm_data,
  92                             int tpm_size)
  93{
  94        return read8_reg(phy_id, tpm_register, tpm_data, tpm_size);
  95}
  96
  97static const struct st33zp24_phy_ops i2c_phy_ops = {
  98        .send = st33zp24_i2c_send,
  99        .recv = st33zp24_i2c_recv,
 100};
 101
 102static const struct acpi_gpio_params lpcpd_gpios = { 1, 0, false };
 103
 104static const struct acpi_gpio_mapping acpi_st33zp24_gpios[] = {
 105        { "lpcpd-gpios", &lpcpd_gpios, 1 },
 106        {},
 107};
 108
 109static int st33zp24_i2c_acpi_request_resources(struct i2c_client *client)
 110{
 111        struct tpm_chip *chip = i2c_get_clientdata(client);
 112        struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
 113        struct st33zp24_i2c_phy *phy = tpm_dev->phy_id;
 114        struct gpio_desc *gpiod_lpcpd;
 115        struct device *dev = &client->dev;
 116        int ret;
 117
 118        ret = devm_acpi_dev_add_driver_gpios(dev, acpi_st33zp24_gpios);
 119        if (ret)
 120                return ret;
 121
 122        /* Get LPCPD GPIO from ACPI */
 123        gpiod_lpcpd = devm_gpiod_get(dev, "lpcpd", GPIOD_OUT_HIGH);
 124        if (IS_ERR(gpiod_lpcpd)) {
 125                dev_err(&client->dev,
 126                        "Failed to retrieve lpcpd-gpios from acpi.\n");
 127                phy->io_lpcpd = -1;
 128                /*
 129                 * lpcpd pin is not specified. This is not an issue as
 130                 * power management can be also managed by TPM specific
 131                 * commands. So leave with a success status code.
 132                 */
 133                return 0;
 134        }
 135
 136        phy->io_lpcpd = desc_to_gpio(gpiod_lpcpd);
 137
 138        return 0;
 139}
 140
 141static int st33zp24_i2c_of_request_resources(struct i2c_client *client)
 142{
 143        struct tpm_chip *chip = i2c_get_clientdata(client);
 144        struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
 145        struct st33zp24_i2c_phy *phy = tpm_dev->phy_id;
 146        struct device_node *pp;
 147        int gpio;
 148        int ret;
 149
 150        pp = client->dev.of_node;
 151        if (!pp) {
 152                dev_err(&client->dev, "No platform data\n");
 153                return -ENODEV;
 154        }
 155
 156        /* Get GPIO from device tree */
 157        gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0);
 158        if (gpio < 0) {
 159                dev_err(&client->dev,
 160                        "Failed to retrieve lpcpd-gpios from dts.\n");
 161                phy->io_lpcpd = -1;
 162                /*
 163                 * lpcpd pin is not specified. This is not an issue as
 164                 * power management can be also managed by TPM specific
 165                 * commands. So leave with a success status code.
 166                 */
 167                return 0;
 168        }
 169        /* GPIO request and configuration */
 170        ret = devm_gpio_request_one(&client->dev, gpio,
 171                        GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD");
 172        if (ret) {
 173                dev_err(&client->dev, "Failed to request lpcpd pin\n");
 174                return -ENODEV;
 175        }
 176        phy->io_lpcpd = gpio;
 177
 178        return 0;
 179}
 180
 181static int st33zp24_i2c_request_resources(struct i2c_client *client)
 182{
 183        struct tpm_chip *chip = i2c_get_clientdata(client);
 184        struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
 185        struct st33zp24_i2c_phy *phy = tpm_dev->phy_id;
 186        struct st33zp24_platform_data *pdata;
 187        int ret;
 188
 189        pdata = client->dev.platform_data;
 190        if (!pdata) {
 191                dev_err(&client->dev, "No platform data\n");
 192                return -ENODEV;
 193        }
 194
 195        /* store for late use */
 196        phy->io_lpcpd = pdata->io_lpcpd;
 197
 198        if (gpio_is_valid(pdata->io_lpcpd)) {
 199                ret = devm_gpio_request_one(&client->dev,
 200                                pdata->io_lpcpd, GPIOF_OUT_INIT_HIGH,
 201                                "TPM IO_LPCPD");
 202                if (ret) {
 203                        dev_err(&client->dev, "Failed to request lpcpd pin\n");
 204                        return ret;
 205                }
 206        }
 207
 208        return 0;
 209}
 210
 211/*
 212 * st33zp24_i2c_probe initialize the TPM device
 213 * @param: client, the i2c_client drescription (TPM I2C description).
 214 * @param: id, the i2c_device_id struct.
 215 * @return: 0 in case of success.
 216 *       -1 in other case.
 217 */
 218static int st33zp24_i2c_probe(struct i2c_client *client,
 219                              const struct i2c_device_id *id)
 220{
 221        int ret;
 222        struct st33zp24_platform_data *pdata;
 223        struct st33zp24_i2c_phy *phy;
 224
 225        if (!client) {
 226                pr_info("%s: i2c client is NULL. Device not accessible.\n",
 227                        __func__);
 228                return -ENODEV;
 229        }
 230
 231        if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
 232                dev_info(&client->dev, "client not i2c capable\n");
 233                return -ENODEV;
 234        }
 235
 236        phy = devm_kzalloc(&client->dev, sizeof(struct st33zp24_i2c_phy),
 237                           GFP_KERNEL);
 238        if (!phy)
 239                return -ENOMEM;
 240
 241        phy->client = client;
 242
 243        pdata = client->dev.platform_data;
 244        if (!pdata && client->dev.of_node) {
 245                ret = st33zp24_i2c_of_request_resources(client);
 246                if (ret)
 247                        return ret;
 248        } else if (pdata) {
 249                ret = st33zp24_i2c_request_resources(client);
 250                if (ret)
 251                        return ret;
 252        } else if (ACPI_HANDLE(&client->dev)) {
 253                ret = st33zp24_i2c_acpi_request_resources(client);
 254                if (ret)
 255                        return ret;
 256        }
 257
 258        return st33zp24_probe(phy, &i2c_phy_ops, &client->dev, client->irq,
 259                              phy->io_lpcpd);
 260}
 261
 262/*
 263 * st33zp24_i2c_remove remove the TPM device
 264 * @param: client, the i2c_client description (TPM I2C description).
 265 * @return: 0 in case of success.
 266 */
 267static int st33zp24_i2c_remove(struct i2c_client *client)
 268{
 269        struct tpm_chip *chip = i2c_get_clientdata(client);
 270        int ret;
 271
 272        ret = st33zp24_remove(chip);
 273        if (ret)
 274                return ret;
 275
 276        return 0;
 277}
 278
 279static const struct i2c_device_id st33zp24_i2c_id[] = {
 280        {TPM_ST33_I2C, 0},
 281        {}
 282};
 283MODULE_DEVICE_TABLE(i2c, st33zp24_i2c_id);
 284
 285static const struct of_device_id of_st33zp24_i2c_match[] = {
 286        { .compatible = "st,st33zp24-i2c", },
 287        {}
 288};
 289MODULE_DEVICE_TABLE(of, of_st33zp24_i2c_match);
 290
 291static const struct acpi_device_id st33zp24_i2c_acpi_match[] = {
 292        {"SMO3324"},
 293        {}
 294};
 295MODULE_DEVICE_TABLE(acpi, st33zp24_i2c_acpi_match);
 296
 297static SIMPLE_DEV_PM_OPS(st33zp24_i2c_ops, st33zp24_pm_suspend,
 298                         st33zp24_pm_resume);
 299
 300static struct i2c_driver st33zp24_i2c_driver = {
 301        .driver = {
 302                .name = TPM_ST33_I2C,
 303                .pm = &st33zp24_i2c_ops,
 304                .of_match_table = of_match_ptr(of_st33zp24_i2c_match),
 305                .acpi_match_table = ACPI_PTR(st33zp24_i2c_acpi_match),
 306        },
 307        .probe = st33zp24_i2c_probe,
 308        .remove = st33zp24_i2c_remove,
 309        .id_table = st33zp24_i2c_id
 310};
 311
 312module_i2c_driver(st33zp24_i2c_driver);
 313
 314MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
 315MODULE_DESCRIPTION("STM TPM 1.2 I2C ST33 Driver");
 316MODULE_VERSION("1.3.0");
 317MODULE_LICENSE("GPL");
 318