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