Skip to content

Commit 3f80190

Browse files
dtorjarkkojs
authored andcommitted
tpm: st33zp24: switch to using gpiod API
Switch the driver from legacy gpio API (that uses flat GPIO numbering) to the newer gpiod API (which used descriptors and respects line polarities specified in ACPI or device tree). Because gpio handling code for SPI and I2C variants duplicates each other it is moved into the core code for the driver. Also, it seems that the driver never assigned tpm_dev->io_lpcpd in the past, so gpio-based power management was most likely not working ever. Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
1 parent 0459302 commit 3f80190

File tree

4 files changed

+39
-205
lines changed

4 files changed

+39
-205
lines changed

drivers/char/tpm/st33zp24/i2c.c

Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
#include <linux/module.h>
88
#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>
9+
#include <linux/of.h>
1310
#include <linux/acpi.h>
1411
#include <linux/tpm.h>
1512

@@ -21,7 +18,6 @@
2118
struct st33zp24_i2c_phy {
2219
struct i2c_client *client;
2320
u8 buf[ST33ZP24_BUFSIZE + 1];
24-
int io_lpcpd;
2521
};
2622

2723
/*
@@ -98,85 +94,6 @@ static const struct st33zp24_phy_ops i2c_phy_ops = {
9894
.recv = st33zp24_i2c_recv,
9995
};
10096

101-
static const struct acpi_gpio_params lpcpd_gpios = { 1, 0, false };
102-
103-
static const struct acpi_gpio_mapping acpi_st33zp24_gpios[] = {
104-
{ "lpcpd-gpios", &lpcpd_gpios, 1 },
105-
{},
106-
};
107-
108-
static int st33zp24_i2c_acpi_request_resources(struct i2c_client *client)
109-
{
110-
struct tpm_chip *chip = i2c_get_clientdata(client);
111-
struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
112-
struct st33zp24_i2c_phy *phy = tpm_dev->phy_id;
113-
struct gpio_desc *gpiod_lpcpd;
114-
struct device *dev = &client->dev;
115-
int ret;
116-
117-
ret = devm_acpi_dev_add_driver_gpios(dev, acpi_st33zp24_gpios);
118-
if (ret)
119-
return ret;
120-
121-
/* Get LPCPD GPIO from ACPI */
122-
gpiod_lpcpd = devm_gpiod_get(dev, "lpcpd", GPIOD_OUT_HIGH);
123-
if (IS_ERR(gpiod_lpcpd)) {
124-
dev_err(&client->dev,
125-
"Failed to retrieve lpcpd-gpios from acpi.\n");
126-
phy->io_lpcpd = -1;
127-
/*
128-
* lpcpd pin is not specified. This is not an issue as
129-
* power management can be also managed by TPM specific
130-
* commands. So leave with a success status code.
131-
*/
132-
return 0;
133-
}
134-
135-
phy->io_lpcpd = desc_to_gpio(gpiod_lpcpd);
136-
137-
return 0;
138-
}
139-
140-
static int st33zp24_i2c_of_request_resources(struct i2c_client *client)
141-
{
142-
struct tpm_chip *chip = i2c_get_clientdata(client);
143-
struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
144-
struct st33zp24_i2c_phy *phy = tpm_dev->phy_id;
145-
struct device_node *pp;
146-
int gpio;
147-
int ret;
148-
149-
pp = client->dev.of_node;
150-
if (!pp) {
151-
dev_err(&client->dev, "No platform data\n");
152-
return -ENODEV;
153-
}
154-
155-
/* Get GPIO from device tree */
156-
gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0);
157-
if (gpio < 0) {
158-
dev_err(&client->dev,
159-
"Failed to retrieve lpcpd-gpios from dts.\n");
160-
phy->io_lpcpd = -1;
161-
/*
162-
* lpcpd pin is not specified. This is not an issue as
163-
* power management can be also managed by TPM specific
164-
* commands. So leave with a success status code.
165-
*/
166-
return 0;
167-
}
168-
/* GPIO request and configuration */
169-
ret = devm_gpio_request_one(&client->dev, gpio,
170-
GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD");
171-
if (ret) {
172-
dev_err(&client->dev, "Failed to request lpcpd pin\n");
173-
return -ENODEV;
174-
}
175-
phy->io_lpcpd = gpio;
176-
177-
return 0;
178-
}
179-
18097
/*
18198
* st33zp24_i2c_probe initialize the TPM device
18299
* @param: client, the i2c_client description (TPM I2C description).
@@ -187,7 +104,6 @@ static int st33zp24_i2c_of_request_resources(struct i2c_client *client)
187104
static int st33zp24_i2c_probe(struct i2c_client *client,
188105
const struct i2c_device_id *id)
189106
{
190-
int ret;
191107
struct st33zp24_i2c_phy *phy;
192108

193109
if (!client) {
@@ -208,20 +124,7 @@ static int st33zp24_i2c_probe(struct i2c_client *client,
208124

209125
phy->client = client;
210126

211-
if (client->dev.of_node) {
212-
ret = st33zp24_i2c_of_request_resources(client);
213-
if (ret)
214-
return ret;
215-
} else if (ACPI_HANDLE(&client->dev)) {
216-
ret = st33zp24_i2c_acpi_request_resources(client);
217-
if (ret)
218-
return ret;
219-
} else {
220-
return -ENODEV;
221-
}
222-
223-
return st33zp24_probe(phy, &i2c_phy_ops, &client->dev, client->irq,
224-
phy->io_lpcpd);
127+
return st33zp24_probe(phy, &i2c_phy_ops, &client->dev, client->irq);
225128
}
226129

227130
/*

drivers/char/tpm/st33zp24/spi.c

Lines changed: 2 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
#include <linux/module.h>
88
#include <linux/spi/spi.h>
9-
#include <linux/gpio.h>
10-
#include <linux/gpio/consumer.h>
11-
#include <linux/of_irq.h>
12-
#include <linux/of_gpio.h>
9+
#include <linux/of.h>
1310
#include <linux/acpi.h>
1411
#include <linux/tpm.h>
1512

@@ -60,7 +57,6 @@ struct st33zp24_spi_phy {
6057
u8 tx_buf[ST33ZP24_SPI_BUFFER_SIZE];
6158
u8 rx_buf[ST33ZP24_SPI_BUFFER_SIZE];
6259

63-
int io_lpcpd;
6460
int latency;
6561
};
6662

@@ -217,84 +213,6 @@ static const struct st33zp24_phy_ops spi_phy_ops = {
217213
.recv = st33zp24_spi_recv,
218214
};
219215

220-
static const struct acpi_gpio_params lpcpd_gpios = { 1, 0, false };
221-
222-
static const struct acpi_gpio_mapping acpi_st33zp24_gpios[] = {
223-
{ "lpcpd-gpios", &lpcpd_gpios, 1 },
224-
{},
225-
};
226-
227-
static int st33zp24_spi_acpi_request_resources(struct spi_device *spi_dev)
228-
{
229-
struct tpm_chip *chip = spi_get_drvdata(spi_dev);
230-
struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
231-
struct st33zp24_spi_phy *phy = tpm_dev->phy_id;
232-
struct gpio_desc *gpiod_lpcpd;
233-
struct device *dev = &spi_dev->dev;
234-
int ret;
235-
236-
ret = devm_acpi_dev_add_driver_gpios(dev, acpi_st33zp24_gpios);
237-
if (ret)
238-
return ret;
239-
240-
/* Get LPCPD GPIO from ACPI */
241-
gpiod_lpcpd = devm_gpiod_get(dev, "lpcpd", GPIOD_OUT_HIGH);
242-
if (IS_ERR(gpiod_lpcpd)) {
243-
dev_err(dev, "Failed to retrieve lpcpd-gpios from acpi.\n");
244-
phy->io_lpcpd = -1;
245-
/*
246-
* lpcpd pin is not specified. This is not an issue as
247-
* power management can be also managed by TPM specific
248-
* commands. So leave with a success status code.
249-
*/
250-
return 0;
251-
}
252-
253-
phy->io_lpcpd = desc_to_gpio(gpiod_lpcpd);
254-
255-
return 0;
256-
}
257-
258-
static int st33zp24_spi_of_request_resources(struct spi_device *spi_dev)
259-
{
260-
struct tpm_chip *chip = spi_get_drvdata(spi_dev);
261-
struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
262-
struct st33zp24_spi_phy *phy = tpm_dev->phy_id;
263-
struct device_node *pp;
264-
int gpio;
265-
int ret;
266-
267-
pp = spi_dev->dev.of_node;
268-
if (!pp) {
269-
dev_err(&spi_dev->dev, "No platform data\n");
270-
return -ENODEV;
271-
}
272-
273-
/* Get GPIO from device tree */
274-
gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0);
275-
if (gpio < 0) {
276-
dev_err(&spi_dev->dev,
277-
"Failed to retrieve lpcpd-gpios from dts.\n");
278-
phy->io_lpcpd = -1;
279-
/*
280-
* lpcpd pin is not specified. This is not an issue as
281-
* power management can be also managed by TPM specific
282-
* commands. So leave with a success status code.
283-
*/
284-
return 0;
285-
}
286-
/* GPIO request and configuration */
287-
ret = devm_gpio_request_one(&spi_dev->dev, gpio,
288-
GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD");
289-
if (ret) {
290-
dev_err(&spi_dev->dev, "Failed to request lpcpd pin\n");
291-
return -ENODEV;
292-
}
293-
phy->io_lpcpd = gpio;
294-
295-
return 0;
296-
}
297-
298216
/*
299217
* st33zp24_spi_probe initialize the TPM device
300218
* @param: dev, the spi_device description (TPM SPI description).
@@ -303,7 +221,6 @@ static int st33zp24_spi_of_request_resources(struct spi_device *spi_dev)
303221
*/
304222
static int st33zp24_spi_probe(struct spi_device *dev)
305223
{
306-
int ret;
307224
struct st33zp24_spi_phy *phy;
308225

309226
/* Check SPI platform functionnalities */
@@ -320,24 +237,11 @@ static int st33zp24_spi_probe(struct spi_device *dev)
320237

321238
phy->spi_device = dev;
322239

323-
if (dev->dev.of_node) {
324-
ret = st33zp24_spi_of_request_resources(dev);
325-
if (ret)
326-
return ret;
327-
} else if (ACPI_HANDLE(&dev->dev)) {
328-
ret = st33zp24_spi_acpi_request_resources(dev);
329-
if (ret)
330-
return ret;
331-
} else {
332-
return -ENODEV;
333-
}
334-
335240
phy->latency = st33zp24_spi_evaluate_latency(phy);
336241
if (phy->latency <= 0)
337242
return -ENODEV;
338243

339-
return st33zp24_probe(phy, &spi_phy_ops, &dev->dev, dev->irq,
340-
phy->io_lpcpd);
244+
return st33zp24_probe(phy, &spi_phy_ops, &dev->dev, dev->irq);
341245
}
342246

343247
/*

drivers/char/tpm/st33zp24/st33zp24.c

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Copyright (C) 2009 - 2016 STMicroelectronics
55
*/
66

7+
#include <linux/acpi.h>
78
#include <linux/module.h>
89
#include <linux/fs.h>
910
#include <linux/kernel.h>
@@ -12,7 +13,7 @@
1213
#include <linux/freezer.h>
1314
#include <linux/string.h>
1415
#include <linux/interrupt.h>
15-
#include <linux/gpio.h>
16+
#include <linux/gpio/consumer.h>
1617
#include <linux/sched.h>
1718
#include <linux/uaccess.h>
1819
#include <linux/io.h>
@@ -432,11 +433,18 @@ static const struct tpm_class_ops st33zp24_tpm = {
432433
.req_canceled = st33zp24_req_canceled,
433434
};
434435

436+
static const struct acpi_gpio_params lpcpd_gpios = { 1, 0, false };
437+
438+
static const struct acpi_gpio_mapping acpi_st33zp24_gpios[] = {
439+
{ "lpcpd-gpios", &lpcpd_gpios, 1 },
440+
{ },
441+
};
442+
435443
/*
436444
* initialize the TPM device
437445
*/
438446
int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops,
439-
struct device *dev, int irq, int io_lpcpd)
447+
struct device *dev, int irq)
440448
{
441449
int ret;
442450
u8 intmask = 0;
@@ -463,6 +471,25 @@ int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops,
463471

464472
tpm_dev->locality = LOCALITY0;
465473

474+
if (ACPI_COMPANION(dev)) {
475+
ret = devm_acpi_dev_add_driver_gpios(dev, acpi_st33zp24_gpios);
476+
if (ret)
477+
return ret;
478+
}
479+
480+
/*
481+
* Get LPCPD GPIO. If lpcpd pin is not specified. This is not an
482+
* issue as power management can be also managed by TPM specific
483+
* commands.
484+
*/
485+
tpm_dev->io_lpcpd = devm_gpiod_get_optional(dev, "lpcpd",
486+
GPIOD_OUT_HIGH);
487+
ret = PTR_ERR_OR_ZERO(tpm_dev->io_lpcpd);
488+
if (ret) {
489+
dev_err(dev, "failed to request lpcpd gpio: %d\n", ret);
490+
return ret;
491+
}
492+
466493
if (irq) {
467494
/* INTERRUPT Setup */
468495
init_waitqueue_head(&tpm_dev->read_queue);
@@ -525,8 +552,8 @@ int st33zp24_pm_suspend(struct device *dev)
525552

526553
int ret = 0;
527554

528-
if (gpio_is_valid(tpm_dev->io_lpcpd))
529-
gpio_set_value(tpm_dev->io_lpcpd, 0);
555+
if (tpm_dev->io_lpcpd)
556+
gpiod_set_value_cansleep(tpm_dev->io_lpcpd, 0);
530557
else
531558
ret = tpm_pm_suspend(dev);
532559

@@ -540,8 +567,8 @@ int st33zp24_pm_resume(struct device *dev)
540567
struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
541568
int ret = 0;
542569

543-
if (gpio_is_valid(tpm_dev->io_lpcpd)) {
544-
gpio_set_value(tpm_dev->io_lpcpd, 1);
570+
if (tpm_dev->io_lpcpd) {
571+
gpiod_set_value_cansleep(tpm_dev->io_lpcpd, 1);
545572
ret = wait_for_stat(chip,
546573
TPM_STS_VALID, chip->timeout_b,
547574
&tpm_dev->read_queue, false);

drivers/char/tpm/st33zp24/st33zp24.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct st33zp24_dev {
2020
int locality;
2121
int irq;
2222
u32 intrs;
23-
int io_lpcpd;
23+
struct gpio_desc *io_lpcpd;
2424
wait_queue_head_t read_queue;
2525
};
2626

@@ -36,6 +36,6 @@ int st33zp24_pm_resume(struct device *dev);
3636
#endif
3737

3838
int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops,
39-
struct device *dev, int irq, int io_lpcpd);
39+
struct device *dev, int irq);
4040
void st33zp24_remove(struct tpm_chip *chip);
4141
#endif /* __LOCAL_ST33ZP24_H__ */

0 commit comments

Comments
 (0)