]>
Commit | Line | Data |
---|---|---|
b4f53ed9 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
70ffd691 PU |
2 | /* |
3 | * Access to GPOs on TWL6040 chip | |
4 | * | |
5 | * Copyright (C) 2012 Texas Instruments, Inc. | |
6 | * | |
7 | * Authors: | |
8 | * Sergio Aguirre <[email protected]> | |
9 | * Peter Ujfalusi <[email protected]> | |
70ffd691 PU |
10 | */ |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/kthread.h> | |
15 | #include <linux/irq.h> | |
fc4f8f32 | 16 | #include <linux/gpio/driver.h> |
70ffd691 | 17 | #include <linux/platform_device.h> |
4bef8bf2 | 18 | #include <linux/bitops.h> |
70ffd691 PU |
19 | #include <linux/of.h> |
20 | ||
21 | #include <linux/mfd/twl6040.h> | |
22 | ||
70ffd691 PU |
23 | static int twl6040gpo_get(struct gpio_chip *chip, unsigned offset) |
24 | { | |
26a4dedc | 25 | struct twl6040 *twl6040 = gpiochip_get_data(chip); |
70ffd691 PU |
26 | int ret = 0; |
27 | ||
28 | ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL); | |
29 | if (ret < 0) | |
30 | return ret; | |
31 | ||
4bef8bf2 | 32 | return !!(ret & BIT(offset)); |
70ffd691 PU |
33 | } |
34 | ||
ba74bd5d LW |
35 | static int twl6040gpo_get_direction(struct gpio_chip *chip, unsigned offset) |
36 | { | |
e42615ec | 37 | return GPIO_LINE_DIRECTION_OUT; |
ba74bd5d LW |
38 | } |
39 | ||
70ffd691 PU |
40 | static int twl6040gpo_direction_out(struct gpio_chip *chip, unsigned offset, |
41 | int value) | |
42 | { | |
43 | /* This only drives GPOs, and can't change direction */ | |
44 | return 0; | |
45 | } | |
46 | ||
47 | static void twl6040gpo_set(struct gpio_chip *chip, unsigned offset, int value) | |
48 | { | |
26a4dedc | 49 | struct twl6040 *twl6040 = gpiochip_get_data(chip); |
70ffd691 PU |
50 | int ret; |
51 | u8 gpoctl; | |
52 | ||
53 | ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL); | |
54 | if (ret < 0) | |
55 | return; | |
56 | ||
57 | if (value) | |
4bef8bf2 | 58 | gpoctl = ret | BIT(offset); |
70ffd691 | 59 | else |
4bef8bf2 | 60 | gpoctl = ret & ~BIT(offset); |
70ffd691 PU |
61 | |
62 | twl6040_reg_write(twl6040, TWL6040_REG_GPOCTL, gpoctl); | |
63 | } | |
64 | ||
65 | static struct gpio_chip twl6040gpo_chip = { | |
66 | .label = "twl6040", | |
67 | .owner = THIS_MODULE, | |
68 | .get = twl6040gpo_get, | |
69 | .direction_output = twl6040gpo_direction_out, | |
ba74bd5d | 70 | .get_direction = twl6040gpo_get_direction, |
70ffd691 | 71 | .set = twl6040gpo_set, |
9fb1f39e | 72 | .can_sleep = true, |
70ffd691 PU |
73 | }; |
74 | ||
75 | /*----------------------------------------------------------------------*/ | |
76 | ||
3836309d | 77 | static int gpo_twl6040_probe(struct platform_device *pdev) |
70ffd691 | 78 | { |
70ffd691 PU |
79 | struct device *twl6040_core_dev = pdev->dev.parent; |
80 | struct twl6040 *twl6040 = dev_get_drvdata(twl6040_core_dev); | |
81 | int ret; | |
82 | ||
6dbe6c07 AS |
83 | device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent)); |
84 | ||
a5d28d79 | 85 | twl6040gpo_chip.base = -1; |
70ffd691 PU |
86 | |
87 | if (twl6040_get_revid(twl6040) < TWL6041_REV_ES2_0) | |
88 | twl6040gpo_chip.ngpio = 3; /* twl6040 have 3 GPO */ | |
89 | else | |
90 | twl6040gpo_chip.ngpio = 1; /* twl6041 have 1 GPO */ | |
91 | ||
58383c78 | 92 | twl6040gpo_chip.parent = &pdev->dev; |
70ffd691 | 93 | |
26a4dedc | 94 | ret = devm_gpiochip_add_data(&pdev->dev, &twl6040gpo_chip, twl6040); |
70ffd691 PU |
95 | if (ret < 0) { |
96 | dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret); | |
97 | twl6040gpo_chip.ngpio = 0; | |
98 | } | |
99 | ||
100 | return ret; | |
101 | } | |
102 | ||
70ffd691 PU |
103 | /* Note: this hardware lives inside an I2C-based multi-function device. */ |
104 | MODULE_ALIAS("platform:twl6040-gpo"); | |
105 | ||
106 | static struct platform_driver gpo_twl6040_driver = { | |
107 | .driver = { | |
108 | .name = "twl6040-gpo", | |
70ffd691 PU |
109 | }, |
110 | .probe = gpo_twl6040_probe, | |
70ffd691 PU |
111 | }; |
112 | ||
113 | module_platform_driver(gpo_twl6040_driver); | |
114 | ||
115 | MODULE_AUTHOR("Texas Instruments, Inc."); | |
116 | MODULE_DESCRIPTION("GPO interface for TWL6040"); | |
117 | MODULE_LICENSE("GPL"); |