Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 1 | /* |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 2 | * GPIO driver for TI TPS65912x PMICs |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 3 | * |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 4 | * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/ |
| 5 | * Andrew F. Davis <afd@ti.com> |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 6 | * |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 10 | * |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 11 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 12 | * kind, whether expressed or implied; without even the implied warranty |
| 13 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License version 2 for more details. |
| 15 | * |
| 16 | * Based on the Arizona GPIO driver and the previous TPS65912 driver by |
| 17 | * Margarita Olaya Cabrera <magi@slimlogic.co.uk> |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 18 | */ |
| 19 | |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 20 | #include <linux/gpio.h> |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 21 | #include <linux/module.h> |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 23 | |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 24 | #include <linux/mfd/tps65912.h> |
| 25 | |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 26 | struct tps65912_gpio { |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 27 | struct gpio_chip gpio_chip; |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 28 | struct tps65912 *tps; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 29 | }; |
| 30 | |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 31 | static int tps65912_gpio_get_direction(struct gpio_chip *gc, |
| 32 | unsigned offset) |
| 33 | { |
| 34 | struct tps65912_gpio *gpio = gpiochip_get_data(gc); |
| 35 | |
| 36 | int ret, val; |
| 37 | |
| 38 | ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val); |
| 39 | if (ret) |
| 40 | return ret; |
| 41 | |
| 42 | if (val & GPIO_CFG_MASK) |
| 43 | return GPIOF_DIR_OUT; |
| 44 | else |
| 45 | return GPIOF_DIR_IN; |
| 46 | } |
| 47 | |
| 48 | static int tps65912_gpio_direction_input(struct gpio_chip *gc, unsigned offset) |
| 49 | { |
| 50 | struct tps65912_gpio *gpio = gpiochip_get_data(gc); |
| 51 | |
| 52 | return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset, |
| 53 | GPIO_CFG_MASK, 0); |
| 54 | } |
| 55 | |
| 56 | static int tps65912_gpio_direction_output(struct gpio_chip *gc, |
| 57 | unsigned offset, int value) |
| 58 | { |
| 59 | struct tps65912_gpio *gpio = gpiochip_get_data(gc); |
| 60 | |
| 61 | /* Set the initial value */ |
| 62 | regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset, |
| 63 | GPIO_SET_MASK, value ? GPIO_SET_MASK : 0); |
| 64 | |
| 65 | return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset, |
| 66 | GPIO_CFG_MASK, GPIO_CFG_MASK); |
| 67 | } |
| 68 | |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 69 | static int tps65912_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 70 | { |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 71 | struct tps65912_gpio *gpio = gpiochip_get_data(gc); |
| 72 | int ret, val; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 73 | |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 74 | ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val); |
| 75 | if (ret) |
| 76 | return ret; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 77 | |
| 78 | if (val & GPIO_STS_MASK) |
| 79 | return 1; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static void tps65912_gpio_set(struct gpio_chip *gc, unsigned offset, |
| 85 | int value) |
| 86 | { |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 87 | struct tps65912_gpio *gpio = gpiochip_get_data(gc); |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 88 | |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 89 | regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset, |
| 90 | GPIO_SET_MASK, value ? GPIO_SET_MASK : 0); |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static struct gpio_chip template_chip = { |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 94 | .label = "tps65912-gpio", |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 95 | .owner = THIS_MODULE, |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 96 | .get_direction = tps65912_gpio_get_direction, |
| 97 | .direction_input = tps65912_gpio_direction_input, |
| 98 | .direction_output = tps65912_gpio_direction_output, |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 99 | .get = tps65912_gpio_get, |
| 100 | .set = tps65912_gpio_set, |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 101 | .base = -1, |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 102 | .ngpio = 5, |
| 103 | .can_sleep = true, |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 104 | }; |
| 105 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 106 | static int tps65912_gpio_probe(struct platform_device *pdev) |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 107 | { |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 108 | struct tps65912 *tps = dev_get_drvdata(pdev->dev.parent); |
| 109 | struct tps65912_gpio *gpio; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 110 | int ret; |
| 111 | |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 112 | gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL); |
| 113 | if (!gpio) |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 114 | return -ENOMEM; |
| 115 | |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 116 | gpio->tps = dev_get_drvdata(pdev->dev.parent); |
| 117 | gpio->gpio_chip = template_chip; |
| 118 | gpio->gpio_chip.parent = tps->dev; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 119 | |
Linus Walleij | 9d93efe | 2016-03-09 22:02:52 +0700 | [diff] [blame] | 120 | ret = devm_gpiochip_add_data(&pdev->dev, &gpio->gpio_chip, |
| 121 | gpio); |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 122 | if (ret < 0) { |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 123 | dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret); |
Axel Lin | 45e253a | 2012-09-01 17:44:27 +0800 | [diff] [blame] | 124 | return ret; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 127 | platform_set_drvdata(pdev, gpio); |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 128 | |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 129 | return 0; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 130 | } |
| 131 | |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 132 | static const struct platform_device_id tps65912_gpio_id_table[] = { |
| 133 | { "tps65912-gpio", }, |
| 134 | { /* sentinel */ } |
| 135 | }; |
| 136 | MODULE_DEVICE_TABLE(platform, tps65912_gpio_id_table); |
| 137 | |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 138 | static struct platform_driver tps65912_gpio_driver = { |
| 139 | .driver = { |
| 140 | .name = "tps65912-gpio", |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 141 | }, |
| 142 | .probe = tps65912_gpio_probe, |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 143 | .id_table = tps65912_gpio_id_table, |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 144 | }; |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 145 | module_platform_driver(tps65912_gpio_driver); |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 146 | |
Andrew F. Davis | ca801a2 | 2016-01-25 09:43:47 -0600 | [diff] [blame] | 147 | MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>"); |
| 148 | MODULE_DESCRIPTION("TPS65912 GPIO driver"); |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 149 | MODULE_LICENSE("GPL v2"); |