blob: c0c18c80abbd856cdca81fc2bf94afa9c5a67a55 [file] [log] [blame]
Linus Walleijb4f53ed2018-09-03 09:55:50 +02001// SPDX-License-Identifier: GPL-2.0+
Peter Ujfalusi70ffd692012-08-16 15:13:15 +03002/*
3 * Access to GPOs on TWL6040 chip
4 *
5 * Copyright (C) 2012 Texas Instruments, Inc.
6 *
7 * Authors:
8 * Sergio Aguirre <saaguirre@ti.com>
9 * Peter Ujfalusi <peter.ujfalusi@ti.com>
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030010 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/kthread.h>
15#include <linux/irq.h>
Linus Walleijfc4f8f32018-09-03 09:54:55 +020016#include <linux/gpio/driver.h>
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030017#include <linux/platform_device.h>
18#include <linux/of.h>
19
20#include <linux/mfd/twl6040.h>
21
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030022static int twl6040gpo_get(struct gpio_chip *chip, unsigned offset)
23{
Linus Walleij58383c782015-11-04 09:56:26 +010024 struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030025 int ret = 0;
26
27 ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
28 if (ret < 0)
29 return ret;
30
31 return (ret >> offset) & 1;
32}
33
34static int twl6040gpo_direction_out(struct gpio_chip *chip, unsigned offset,
35 int value)
36{
37 /* This only drives GPOs, and can't change direction */
38 return 0;
39}
40
41static void twl6040gpo_set(struct gpio_chip *chip, unsigned offset, int value)
42{
Linus Walleij58383c782015-11-04 09:56:26 +010043 struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030044 int ret;
45 u8 gpoctl;
46
47 ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
48 if (ret < 0)
49 return;
50
51 if (value)
52 gpoctl = ret | (1 << offset);
53 else
54 gpoctl = ret & ~(1 << offset);
55
56 twl6040_reg_write(twl6040, TWL6040_REG_GPOCTL, gpoctl);
57}
58
59static struct gpio_chip twl6040gpo_chip = {
60 .label = "twl6040",
61 .owner = THIS_MODULE,
62 .get = twl6040gpo_get,
63 .direction_output = twl6040gpo_direction_out,
64 .set = twl6040gpo_set,
Linus Walleij9fb1f392013-12-04 14:42:46 +010065 .can_sleep = true,
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030066};
67
68/*----------------------------------------------------------------------*/
69
Bill Pemberton38363092012-11-19 13:22:34 -050070static int gpo_twl6040_probe(struct platform_device *pdev)
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030071{
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030072 struct device *twl6040_core_dev = pdev->dev.parent;
73 struct twl6040 *twl6040 = dev_get_drvdata(twl6040_core_dev);
74 int ret;
75
Peter Ujfalusia5d28d72013-07-12 13:30:43 +020076 twl6040gpo_chip.base = -1;
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030077
78 if (twl6040_get_revid(twl6040) < TWL6041_REV_ES2_0)
79 twl6040gpo_chip.ngpio = 3; /* twl6040 have 3 GPO */
80 else
81 twl6040gpo_chip.ngpio = 1; /* twl6041 have 1 GPO */
82
Linus Walleij58383c782015-11-04 09:56:26 +010083 twl6040gpo_chip.parent = &pdev->dev;
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030084#ifdef CONFIG_OF_GPIO
85 twl6040gpo_chip.of_node = twl6040_core_dev->of_node;
86#endif
87
Laxman Dewanganfc0292a2016-02-22 17:43:28 +053088 ret = devm_gpiochip_add_data(&pdev->dev, &twl6040gpo_chip, NULL);
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030089 if (ret < 0) {
90 dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
91 twl6040gpo_chip.ngpio = 0;
92 }
93
94 return ret;
95}
96
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030097/* Note: this hardware lives inside an I2C-based multi-function device. */
98MODULE_ALIAS("platform:twl6040-gpo");
99
100static struct platform_driver gpo_twl6040_driver = {
101 .driver = {
102 .name = "twl6040-gpo",
Peter Ujfalusi70ffd692012-08-16 15:13:15 +0300103 },
104 .probe = gpo_twl6040_probe,
Peter Ujfalusi70ffd692012-08-16 15:13:15 +0300105};
106
107module_platform_driver(gpo_twl6040_driver);
108
109MODULE_AUTHOR("Texas Instruments, Inc.");
110MODULE_DESCRIPTION("GPO interface for TWL6040");
111MODULE_LICENSE("GPL");