blob: 4fae3ebea7909853b5a5fa6fdf1a4a27bf13583b [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Laxman Dewangane9fe32b2012-05-14 12:46:12 +05302/*
3 * GPIO driver for RICOH583 power management chip.
4 *
5 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6 * Author: Laxman dewangan <ldewangan@nvidia.com>
7 *
8 * Based on code
9 * Copyright (C) 2011 RICOH COMPANY,LTD
Laxman Dewangane9fe32b2012-05-14 12:46:12 +053010 */
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/slab.h>
Laxman Dewangane9fe32b2012-05-14 12:46:12 +053014#include <linux/platform_device.h>
15#include <linux/device.h>
Linus Walleij98aef8e2018-05-31 08:06:23 +020016#include <linux/gpio/driver.h>
Laxman Dewangane9fe32b2012-05-14 12:46:12 +053017#include <linux/mfd/rc5t583.h>
18
19struct rc5t583_gpio {
20 struct gpio_chip gpio_chip;
21 struct rc5t583 *rc5t583;
22};
23
Laxman Dewangane9fe32b2012-05-14 12:46:12 +053024static int rc5t583_gpio_get(struct gpio_chip *gc, unsigned int offset)
25{
Linus Walleijd660c682015-12-07 14:08:12 +010026 struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
Laxman Dewangane9fe32b2012-05-14 12:46:12 +053027 struct device *parent = rc5t583_gpio->rc5t583->dev;
28 uint8_t val = 0;
29 int ret;
30
31 ret = rc5t583_read(parent, RC5T583_GPIO_MON_IOIN, &val);
32 if (ret < 0)
33 return ret;
34
35 return !!(val & BIT(offset));
36}
37
38static void rc5t583_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
39{
Linus Walleijd660c682015-12-07 14:08:12 +010040 struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
Laxman Dewangane9fe32b2012-05-14 12:46:12 +053041 struct device *parent = rc5t583_gpio->rc5t583->dev;
42 if (val)
43 rc5t583_set_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
44 else
45 rc5t583_clear_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
46}
47
48static int rc5t583_gpio_dir_input(struct gpio_chip *gc, unsigned int offset)
49{
Linus Walleijd660c682015-12-07 14:08:12 +010050 struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
Laxman Dewangane9fe32b2012-05-14 12:46:12 +053051 struct device *parent = rc5t583_gpio->rc5t583->dev;
52 int ret;
53
54 ret = rc5t583_clear_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
55 if (ret < 0)
56 return ret;
57
58 /* Set pin to gpio mode */
59 return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
60}
61
62static int rc5t583_gpio_dir_output(struct gpio_chip *gc, unsigned offset,
63 int value)
64{
Linus Walleijd660c682015-12-07 14:08:12 +010065 struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
Laxman Dewangane9fe32b2012-05-14 12:46:12 +053066 struct device *parent = rc5t583_gpio->rc5t583->dev;
67 int ret;
68
69 rc5t583_gpio_set(gc, offset, value);
70 ret = rc5t583_set_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
71 if (ret < 0)
72 return ret;
73
74 /* Set pin to gpio mode */
75 return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
76}
77
78static int rc5t583_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
79{
Linus Walleijd660c682015-12-07 14:08:12 +010080 struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
Laxman Dewangane9fe32b2012-05-14 12:46:12 +053081
Alexander Shiyan3bde4d22014-02-15 17:24:07 +040082 if (offset < RC5T583_MAX_GPIO)
Laxman Dewangane9fe32b2012-05-14 12:46:12 +053083 return rc5t583_gpio->rc5t583->irq_base +
84 RC5T583_IRQ_GPIO0 + offset;
85 return -EINVAL;
86}
87
88static void rc5t583_gpio_free(struct gpio_chip *gc, unsigned offset)
89{
Linus Walleijd660c682015-12-07 14:08:12 +010090 struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
Laxman Dewangane9fe32b2012-05-14 12:46:12 +053091 struct device *parent = rc5t583_gpio->rc5t583->dev;
92
93 rc5t583_set_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
94}
95
Bill Pemberton38363092012-11-19 13:22:34 -050096static int rc5t583_gpio_probe(struct platform_device *pdev)
Laxman Dewangane9fe32b2012-05-14 12:46:12 +053097{
98 struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
99 struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev);
100 struct rc5t583_gpio *rc5t583_gpio;
101
102 rc5t583_gpio = devm_kzalloc(&pdev->dev, sizeof(*rc5t583_gpio),
103 GFP_KERNEL);
Jingoo Han4b7dfd72014-04-29 17:39:42 +0900104 if (!rc5t583_gpio)
Laxman Dewangane9fe32b2012-05-14 12:46:12 +0530105 return -ENOMEM;
Laxman Dewangane9fe32b2012-05-14 12:46:12 +0530106
107 rc5t583_gpio->gpio_chip.label = "gpio-rc5t583",
108 rc5t583_gpio->gpio_chip.owner = THIS_MODULE,
109 rc5t583_gpio->gpio_chip.free = rc5t583_gpio_free,
110 rc5t583_gpio->gpio_chip.direction_input = rc5t583_gpio_dir_input,
111 rc5t583_gpio->gpio_chip.direction_output = rc5t583_gpio_dir_output,
112 rc5t583_gpio->gpio_chip.set = rc5t583_gpio_set,
113 rc5t583_gpio->gpio_chip.get = rc5t583_gpio_get,
114 rc5t583_gpio->gpio_chip.to_irq = rc5t583_gpio_to_irq,
115 rc5t583_gpio->gpio_chip.ngpio = RC5T583_MAX_GPIO,
Linus Walleij9fb1f392013-12-04 14:42:46 +0100116 rc5t583_gpio->gpio_chip.can_sleep = true,
Linus Walleij58383c782015-11-04 09:56:26 +0100117 rc5t583_gpio->gpio_chip.parent = &pdev->dev;
Laxman Dewangane9fe32b2012-05-14 12:46:12 +0530118 rc5t583_gpio->gpio_chip.base = -1;
119 rc5t583_gpio->rc5t583 = rc5t583;
120
121 if (pdata && pdata->gpio_base)
122 rc5t583_gpio->gpio_chip.base = pdata->gpio_base;
123
124 platform_set_drvdata(pdev, rc5t583_gpio);
125
Laxman Dewanganbc72f7f2016-02-22 17:43:28 +0530126 return devm_gpiochip_add_data(&pdev->dev, &rc5t583_gpio->gpio_chip,
127 rc5t583_gpio);
Laxman Dewangane9fe32b2012-05-14 12:46:12 +0530128}
129
130static struct platform_driver rc5t583_gpio_driver = {
131 .driver = {
132 .name = "rc5t583-gpio",
Laxman Dewangane9fe32b2012-05-14 12:46:12 +0530133 },
134 .probe = rc5t583_gpio_probe,
Laxman Dewangane9fe32b2012-05-14 12:46:12 +0530135};
136
137static int __init rc5t583_gpio_init(void)
138{
139 return platform_driver_register(&rc5t583_gpio_driver);
140}
141subsys_initcall(rc5t583_gpio_init);