Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Texas Instruments Inc. |
| 3 | * |
| 4 | * Author: Margarita Olaya <magi@slimlogic.co.uk> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2 of the License, or (at your |
| 9 | * option) any later version. |
| 10 | * |
| 11 | * This driver is based on wm8350 implementation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/gpio.h> |
| 18 | #include <linux/mfd/core.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/seq_file.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/mfd/tps65912.h> |
| 23 | |
| 24 | struct tps65912_gpio_data { |
| 25 | struct tps65912 *tps65912; |
| 26 | struct gpio_chip gpio_chip; |
| 27 | }; |
| 28 | |
| 29 | static int tps65912_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 30 | { |
Linus Walleij | ad8dd23 | 2015-12-07 14:50:42 +0100 | [diff] [blame] | 31 | struct tps65912_gpio_data *tps65912_gpio = gpiochip_get_data(gc); |
Nicolas Saenz Julienne | 2f97c20 | 2015-02-19 01:52:25 +0000 | [diff] [blame] | 32 | struct tps65912 *tps65912 = tps65912_gpio->tps65912; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 33 | int val; |
| 34 | |
| 35 | val = tps65912_reg_read(tps65912, TPS65912_GPIO1 + offset); |
| 36 | |
| 37 | if (val & GPIO_STS_MASK) |
| 38 | return 1; |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static void tps65912_gpio_set(struct gpio_chip *gc, unsigned offset, |
| 44 | int value) |
| 45 | { |
Linus Walleij | ad8dd23 | 2015-12-07 14:50:42 +0100 | [diff] [blame] | 46 | struct tps65912_gpio_data *tps65912_gpio = gpiochip_get_data(gc); |
Nicolas Saenz Julienne | 2f97c20 | 2015-02-19 01:52:25 +0000 | [diff] [blame] | 47 | struct tps65912 *tps65912 = tps65912_gpio->tps65912; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 48 | |
| 49 | if (value) |
| 50 | tps65912_set_bits(tps65912, TPS65912_GPIO1 + offset, |
| 51 | GPIO_SET_MASK); |
| 52 | else |
| 53 | tps65912_clear_bits(tps65912, TPS65912_GPIO1 + offset, |
| 54 | GPIO_SET_MASK); |
| 55 | } |
| 56 | |
| 57 | static int tps65912_gpio_output(struct gpio_chip *gc, unsigned offset, |
| 58 | int value) |
| 59 | { |
Linus Walleij | ad8dd23 | 2015-12-07 14:50:42 +0100 | [diff] [blame] | 60 | struct tps65912_gpio_data *tps65912_gpio = gpiochip_get_data(gc); |
Nicolas Saenz Julienne | 2f97c20 | 2015-02-19 01:52:25 +0000 | [diff] [blame] | 61 | struct tps65912 *tps65912 = tps65912_gpio->tps65912; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 62 | |
| 63 | /* Set the initial value */ |
| 64 | tps65912_gpio_set(gc, offset, value); |
| 65 | |
| 66 | return tps65912_set_bits(tps65912, TPS65912_GPIO1 + offset, |
| 67 | GPIO_CFG_MASK); |
| 68 | } |
| 69 | |
| 70 | static int tps65912_gpio_input(struct gpio_chip *gc, unsigned offset) |
| 71 | { |
Linus Walleij | ad8dd23 | 2015-12-07 14:50:42 +0100 | [diff] [blame] | 72 | struct tps65912_gpio_data *tps65912_gpio = gpiochip_get_data(gc); |
Nicolas Saenz Julienne | 2f97c20 | 2015-02-19 01:52:25 +0000 | [diff] [blame] | 73 | struct tps65912 *tps65912 = tps65912_gpio->tps65912; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 74 | |
| 75 | return tps65912_clear_bits(tps65912, TPS65912_GPIO1 + offset, |
| 76 | GPIO_CFG_MASK); |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static struct gpio_chip template_chip = { |
| 80 | .label = "tps65912", |
| 81 | .owner = THIS_MODULE, |
| 82 | .direction_input = tps65912_gpio_input, |
| 83 | .direction_output = tps65912_gpio_output, |
| 84 | .get = tps65912_gpio_get, |
| 85 | .set = tps65912_gpio_set, |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 86 | .can_sleep = true, |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 87 | .ngpio = 5, |
| 88 | .base = -1, |
| 89 | }; |
| 90 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 91 | static int tps65912_gpio_probe(struct platform_device *pdev) |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 92 | { |
| 93 | struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 94 | struct tps65912_board *pdata = dev_get_platdata(tps65912->dev); |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 95 | struct tps65912_gpio_data *tps65912_gpio; |
| 96 | int ret; |
| 97 | |
Axel Lin | 45e253a | 2012-09-01 17:44:27 +0800 | [diff] [blame] | 98 | tps65912_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps65912_gpio), |
| 99 | GFP_KERNEL); |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 100 | if (tps65912_gpio == NULL) |
| 101 | return -ENOMEM; |
| 102 | |
| 103 | tps65912_gpio->tps65912 = tps65912; |
| 104 | tps65912_gpio->gpio_chip = template_chip; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 105 | tps65912_gpio->gpio_chip.parent = &pdev->dev; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 106 | if (pdata && pdata->gpio_base) |
| 107 | tps65912_gpio->gpio_chip.base = pdata->gpio_base; |
| 108 | |
Linus Walleij | ad8dd23 | 2015-12-07 14:50:42 +0100 | [diff] [blame] | 109 | ret = gpiochip_add_data(&tps65912_gpio->gpio_chip, tps65912_gpio); |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 110 | if (ret < 0) { |
| 111 | dev_err(&pdev->dev, "Failed to register gpiochip, %d\n", ret); |
Axel Lin | 45e253a | 2012-09-01 17:44:27 +0800 | [diff] [blame] | 112 | return ret; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | platform_set_drvdata(pdev, tps65912_gpio); |
| 116 | |
| 117 | return ret; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Bill Pemberton | 206210c | 2012-11-19 13:25:50 -0500 | [diff] [blame] | 120 | static int tps65912_gpio_remove(struct platform_device *pdev) |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 121 | { |
| 122 | struct tps65912_gpio_data *tps65912_gpio = platform_get_drvdata(pdev); |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 123 | |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 124 | gpiochip_remove(&tps65912_gpio->gpio_chip); |
| 125 | return 0; |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static struct platform_driver tps65912_gpio_driver = { |
| 129 | .driver = { |
| 130 | .name = "tps65912-gpio", |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 131 | }, |
| 132 | .probe = tps65912_gpio_probe, |
Bill Pemberton | 8283c4f | 2012-11-19 13:20:08 -0500 | [diff] [blame] | 133 | .remove = tps65912_gpio_remove, |
Margarita Olaya | 668a6cc | 2011-06-09 14:50:19 -0500 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | static int __init tps65912_gpio_init(void) |
| 137 | { |
| 138 | return platform_driver_register(&tps65912_gpio_driver); |
| 139 | } |
| 140 | subsys_initcall(tps65912_gpio_init); |
| 141 | |
| 142 | static void __exit tps65912_gpio_exit(void) |
| 143 | { |
| 144 | platform_driver_unregister(&tps65912_gpio_driver); |
| 145 | } |
| 146 | module_exit(tps65912_gpio_exit); |
| 147 | |
| 148 | MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>"); |
| 149 | MODULE_DESCRIPTION("GPIO interface for TPS65912 PMICs"); |
| 150 | MODULE_LICENSE("GPL v2"); |
| 151 | MODULE_ALIAS("platform:tps65912-gpio"); |