blob: c845b2ff1f4376082cab35232ed00c09dcf8c4f3 [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>
Linus Walleij4bef8bf2018-09-03 09:59:57 +020018#include <linux/bitops.h>
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030019#include <linux/of.h>
20
21#include <linux/mfd/twl6040.h>
22
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030023static int twl6040gpo_get(struct gpio_chip *chip, unsigned offset)
24{
Linus Walleij58383c782015-11-04 09:56:26 +010025 struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030026 int ret = 0;
27
28 ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
29 if (ret < 0)
30 return ret;
31
Linus Walleij4bef8bf2018-09-03 09:59:57 +020032 return !!(ret & BIT(offset));
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030033}
34
Linus Walleijba74bd52018-09-03 10:04:01 +020035static int twl6040gpo_get_direction(struct gpio_chip *chip, unsigned offset)
36{
37 /* This means "out" */
38 return 0;
39}
40
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030041static int twl6040gpo_direction_out(struct gpio_chip *chip, unsigned offset,
42 int value)
43{
44 /* This only drives GPOs, and can't change direction */
45 return 0;
46}
47
48static void twl6040gpo_set(struct gpio_chip *chip, unsigned offset, int value)
49{
Linus Walleij58383c782015-11-04 09:56:26 +010050 struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030051 int ret;
52 u8 gpoctl;
53
54 ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
55 if (ret < 0)
56 return;
57
58 if (value)
Linus Walleij4bef8bf2018-09-03 09:59:57 +020059 gpoctl = ret | BIT(offset);
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030060 else
Linus Walleij4bef8bf2018-09-03 09:59:57 +020061 gpoctl = ret & ~BIT(offset);
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030062
63 twl6040_reg_write(twl6040, TWL6040_REG_GPOCTL, gpoctl);
64}
65
66static struct gpio_chip twl6040gpo_chip = {
67 .label = "twl6040",
68 .owner = THIS_MODULE,
69 .get = twl6040gpo_get,
70 .direction_output = twl6040gpo_direction_out,
Linus Walleijba74bd52018-09-03 10:04:01 +020071 .get_direction = twl6040gpo_get_direction,
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030072 .set = twl6040gpo_set,
Linus Walleij9fb1f392013-12-04 14:42:46 +010073 .can_sleep = true,
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030074};
75
76/*----------------------------------------------------------------------*/
77
Bill Pemberton38363092012-11-19 13:22:34 -050078static int gpo_twl6040_probe(struct platform_device *pdev)
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030079{
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030080 struct device *twl6040_core_dev = pdev->dev.parent;
81 struct twl6040 *twl6040 = dev_get_drvdata(twl6040_core_dev);
82 int ret;
83
Peter Ujfalusia5d28d72013-07-12 13:30:43 +020084 twl6040gpo_chip.base = -1;
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030085
86 if (twl6040_get_revid(twl6040) < TWL6041_REV_ES2_0)
87 twl6040gpo_chip.ngpio = 3; /* twl6040 have 3 GPO */
88 else
89 twl6040gpo_chip.ngpio = 1; /* twl6041 have 1 GPO */
90
Linus Walleij58383c782015-11-04 09:56:26 +010091 twl6040gpo_chip.parent = &pdev->dev;
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030092#ifdef CONFIG_OF_GPIO
93 twl6040gpo_chip.of_node = twl6040_core_dev->of_node;
94#endif
95
Laxman Dewanganfc0292a2016-02-22 17:43:28 +053096 ret = devm_gpiochip_add_data(&pdev->dev, &twl6040gpo_chip, NULL);
Peter Ujfalusi70ffd692012-08-16 15:13:15 +030097 if (ret < 0) {
98 dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
99 twl6040gpo_chip.ngpio = 0;
100 }
101
102 return ret;
103}
104
Peter Ujfalusi70ffd692012-08-16 15:13:15 +0300105/* Note: this hardware lives inside an I2C-based multi-function device. */
106MODULE_ALIAS("platform:twl6040-gpo");
107
108static struct platform_driver gpo_twl6040_driver = {
109 .driver = {
110 .name = "twl6040-gpo",
Peter Ujfalusi70ffd692012-08-16 15:13:15 +0300111 },
112 .probe = gpo_twl6040_probe,
Peter Ujfalusi70ffd692012-08-16 15:13:15 +0300113};
114
115module_platform_driver(gpo_twl6040_driver);
116
117MODULE_AUTHOR("Texas Instruments, Inc.");
118MODULE_DESCRIPTION("GPO interface for TWL6040");
119MODULE_LICENSE("GPL");