1 // SPDX-License-Identifier: GPL-2.0-only
3 * GPIO driver for RICOH583 power management chip.
5 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6 * Author: Laxman dewangan <ldewangan@nvidia.com>
9 * Copyright (C) 2011 RICOH COMPANY,LTD
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/platform_device.h>
15 #include <linux/device.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/mfd/rc5t583.h>
20 struct gpio_chip gpio_chip;
21 struct rc5t583 *rc5t583;
24 static int rc5t583_gpio_get(struct gpio_chip *gc, unsigned int offset)
26 struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
27 struct device *parent = rc5t583_gpio->rc5t583->dev;
31 ret = rc5t583_read(parent, RC5T583_GPIO_MON_IOIN, &val);
35 return !!(val & BIT(offset));
38 static void rc5t583_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
40 struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
41 struct device *parent = rc5t583_gpio->rc5t583->dev;
43 rc5t583_set_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
45 rc5t583_clear_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
48 static int rc5t583_gpio_dir_input(struct gpio_chip *gc, unsigned int offset)
50 struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
51 struct device *parent = rc5t583_gpio->rc5t583->dev;
54 ret = rc5t583_clear_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
58 /* Set pin to gpio mode */
59 return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
62 static int rc5t583_gpio_dir_output(struct gpio_chip *gc, unsigned offset,
65 struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
66 struct device *parent = rc5t583_gpio->rc5t583->dev;
69 rc5t583_gpio_set(gc, offset, value);
70 ret = rc5t583_set_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
74 /* Set pin to gpio mode */
75 return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
78 static int rc5t583_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
80 struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
82 if (offset < RC5T583_MAX_GPIO)
83 return rc5t583_gpio->rc5t583->irq_base +
84 RC5T583_IRQ_GPIO0 + offset;
88 static void rc5t583_gpio_free(struct gpio_chip *gc, unsigned offset)
90 struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
91 struct device *parent = rc5t583_gpio->rc5t583->dev;
93 rc5t583_set_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
96 static int rc5t583_gpio_probe(struct platform_device *pdev)
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;
102 rc5t583_gpio = devm_kzalloc(&pdev->dev, sizeof(*rc5t583_gpio),
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,
116 rc5t583_gpio->gpio_chip.can_sleep = true,
117 rc5t583_gpio->gpio_chip.parent = &pdev->dev;
118 rc5t583_gpio->gpio_chip.base = -1;
119 rc5t583_gpio->rc5t583 = rc5t583;
121 if (pdata && pdata->gpio_base)
122 rc5t583_gpio->gpio_chip.base = pdata->gpio_base;
124 platform_set_drvdata(pdev, rc5t583_gpio);
126 return devm_gpiochip_add_data(&pdev->dev, &rc5t583_gpio->gpio_chip,
130 static struct platform_driver rc5t583_gpio_driver = {
132 .name = "rc5t583-gpio",
134 .probe = rc5t583_gpio_probe,
137 static int __init rc5t583_gpio_init(void)
139 return platform_driver_register(&rc5t583_gpio_driver);
141 subsys_initcall(rc5t583_gpio_init);