Thomas Gleixner | 9952f69 | 2019-05-28 10:10:04 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 2 | /* |
| 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 Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 10 | */ |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/slab.h> |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/device.h> |
Linus Walleij | 98aef8e | 2018-05-31 08:06:23 +0200 | [diff] [blame] | 16 | #include <linux/gpio/driver.h> |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 17 | #include <linux/mfd/rc5t583.h> |
| 18 | |
| 19 | struct rc5t583_gpio { |
| 20 | struct gpio_chip gpio_chip; |
| 21 | struct rc5t583 *rc5t583; |
| 22 | }; |
| 23 | |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 24 | static int rc5t583_gpio_get(struct gpio_chip *gc, unsigned int offset) |
| 25 | { |
Linus Walleij | d660c68 | 2015-12-07 14:08:12 +0100 | [diff] [blame] | 26 | struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc); |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 27 | 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 | |
| 38 | static void rc5t583_gpio_set(struct gpio_chip *gc, unsigned int offset, int val) |
| 39 | { |
Linus Walleij | d660c68 | 2015-12-07 14:08:12 +0100 | [diff] [blame] | 40 | struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc); |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 41 | 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 | |
| 48 | static int rc5t583_gpio_dir_input(struct gpio_chip *gc, unsigned int offset) |
| 49 | { |
Linus Walleij | d660c68 | 2015-12-07 14:08:12 +0100 | [diff] [blame] | 50 | struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc); |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 51 | 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 | |
| 62 | static int rc5t583_gpio_dir_output(struct gpio_chip *gc, unsigned offset, |
| 63 | int value) |
| 64 | { |
Linus Walleij | d660c68 | 2015-12-07 14:08:12 +0100 | [diff] [blame] | 65 | struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc); |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 66 | 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 | |
| 78 | static int rc5t583_gpio_to_irq(struct gpio_chip *gc, unsigned offset) |
| 79 | { |
Linus Walleij | d660c68 | 2015-12-07 14:08:12 +0100 | [diff] [blame] | 80 | struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc); |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 81 | |
Alexander Shiyan | 3bde4d2 | 2014-02-15 17:24:07 +0400 | [diff] [blame] | 82 | if (offset < RC5T583_MAX_GPIO) |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 83 | return rc5t583_gpio->rc5t583->irq_base + |
| 84 | RC5T583_IRQ_GPIO0 + offset; |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | |
| 88 | static void rc5t583_gpio_free(struct gpio_chip *gc, unsigned offset) |
| 89 | { |
Linus Walleij | d660c68 | 2015-12-07 14:08:12 +0100 | [diff] [blame] | 90 | struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc); |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 91 | struct device *parent = rc5t583_gpio->rc5t583->dev; |
| 92 | |
| 93 | rc5t583_set_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset)); |
| 94 | } |
| 95 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 96 | static int rc5t583_gpio_probe(struct platform_device *pdev) |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 97 | { |
| 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 Han | 4b7dfd7 | 2014-04-29 17:39:42 +0900 | [diff] [blame] | 104 | if (!rc5t583_gpio) |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 105 | return -ENOMEM; |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 106 | |
| 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 Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 116 | rc5t583_gpio->gpio_chip.can_sleep = true, |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 117 | rc5t583_gpio->gpio_chip.parent = &pdev->dev; |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 118 | 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 Dewangan | bc72f7f | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 126 | return devm_gpiochip_add_data(&pdev->dev, &rc5t583_gpio->gpio_chip, |
| 127 | rc5t583_gpio); |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static struct platform_driver rc5t583_gpio_driver = { |
| 131 | .driver = { |
| 132 | .name = "rc5t583-gpio", |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 133 | }, |
| 134 | .probe = rc5t583_gpio_probe, |
Laxman Dewangan | e9fe32b | 2012-05-14 12:46:12 +0530 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | static int __init rc5t583_gpio_init(void) |
| 138 | { |
| 139 | return platform_driver_register(&rc5t583_gpio_driver); |
| 140 | } |
| 141 | subsys_initcall(rc5t583_gpio_init); |