Nicolas Saenz Julienne | c366c76 | 2016-01-30 17:06:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Verifone Int. |
| 3 | * |
| 4 | * Author: Nicolas Saenz Julienne <nicolassaenzj@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify i t |
| 7 | * under the terms of the GNU General Public License as published by th e |
| 8 | * Free Software Foundation; either version 2 of the License, or (at you r |
| 9 | * option) any later version. |
| 10 | * |
| 11 | * This driver is based on the gpio-tps65912 implementation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/gpio/driver.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/mfd/tps65218.h> |
Nicolas Saenz Julienne | c366c76 | 2016-01-30 17:06:44 +0000 | [diff] [blame] | 20 | |
| 21 | struct tps65218_gpio { |
| 22 | struct tps65218 *tps65218; |
| 23 | struct gpio_chip gpio_chip; |
| 24 | }; |
| 25 | |
| 26 | static int tps65218_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 27 | { |
| 28 | struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc); |
| 29 | struct tps65218 *tps65218 = tps65218_gpio->tps65218; |
| 30 | unsigned int val; |
| 31 | int ret; |
| 32 | |
| 33 | ret = tps65218_reg_read(tps65218, TPS65218_REG_ENABLE2, &val); |
| 34 | if (ret) |
| 35 | return ret; |
| 36 | |
| 37 | return !!(val & (TPS65218_ENABLE2_GPIO1 << offset)); |
| 38 | } |
| 39 | |
| 40 | static void tps65218_gpio_set(struct gpio_chip *gc, unsigned offset, |
| 41 | int value) |
| 42 | { |
| 43 | struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc); |
| 44 | struct tps65218 *tps65218 = tps65218_gpio->tps65218; |
| 45 | |
| 46 | if (value) |
| 47 | tps65218_set_bits(tps65218, TPS65218_REG_ENABLE2, |
| 48 | TPS65218_ENABLE2_GPIO1 << offset, |
| 49 | TPS65218_ENABLE2_GPIO1 << offset, |
| 50 | TPS65218_PROTECT_L1); |
| 51 | else |
| 52 | tps65218_clear_bits(tps65218, TPS65218_REG_ENABLE2, |
| 53 | TPS65218_ENABLE2_GPIO1 << offset, |
| 54 | TPS65218_PROTECT_L1); |
| 55 | } |
| 56 | |
| 57 | static int tps65218_gpio_output(struct gpio_chip *gc, unsigned offset, |
| 58 | int value) |
| 59 | { |
| 60 | /* Only drives GPOs */ |
Axel Lin | 818cc6a | 2016-02-15 20:10:40 +0800 | [diff] [blame] | 61 | tps65218_gpio_set(gc, offset, value); |
Nicolas Saenz Julienne | c366c76 | 2016-01-30 17:06:44 +0000 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int tps65218_gpio_input(struct gpio_chip *gc, unsigned offset) |
| 66 | { |
| 67 | return -EPERM; |
| 68 | } |
| 69 | |
| 70 | static int tps65218_gpio_request(struct gpio_chip *gc, unsigned offset) |
| 71 | { |
| 72 | struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc); |
| 73 | struct tps65218 *tps65218 = tps65218_gpio->tps65218; |
Nicolas Saenz Julienne | c366c76 | 2016-01-30 17:06:44 +0000 | [diff] [blame] | 74 | int ret; |
| 75 | |
Linus Walleij | 143b65d | 2016-02-16 15:41:42 +0100 | [diff] [blame] | 76 | if (gpiochip_line_is_open_source(gc, offset)) { |
Nicolas Saenz Julienne | c366c76 | 2016-01-30 17:06:44 +0000 | [diff] [blame] | 77 | dev_err(gc->parent, "can't work as open source\n"); |
| 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | switch (offset) { |
| 82 | case 0: |
Linus Walleij | 143b65d | 2016-02-16 15:41:42 +0100 | [diff] [blame] | 83 | if (!gpiochip_line_is_open_drain(gc, offset)) { |
Nicolas Saenz Julienne | c366c76 | 2016-01-30 17:06:44 +0000 | [diff] [blame] | 84 | dev_err(gc->parent, "GPO1 works only as open drain\n"); |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | |
| 88 | /* Disable sequencer for GPO1 */ |
| 89 | ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7, |
| 90 | TPS65218_SEQ7_GPO1_SEQ_MASK, |
| 91 | TPS65218_PROTECT_L1); |
| 92 | if (ret) |
| 93 | return ret; |
| 94 | |
| 95 | /* Setup GPO1 */ |
| 96 | ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1, |
| 97 | TPS65218_CONFIG1_IO1_SEL, |
| 98 | TPS65218_PROTECT_L1); |
| 99 | if (ret) |
| 100 | return ret; |
| 101 | |
| 102 | break; |
| 103 | case 1: |
Nicolas Saenz Julienne | c366c76 | 2016-01-30 17:06:44 +0000 | [diff] [blame] | 104 | /* Setup GPO2 */ |
| 105 | ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1, |
| 106 | TPS65218_CONFIG1_IO1_SEL, |
| 107 | TPS65218_PROTECT_L1); |
| 108 | if (ret) |
| 109 | return ret; |
| 110 | |
| 111 | break; |
| 112 | |
| 113 | case 2: |
Linus Walleij | 143b65d | 2016-02-16 15:41:42 +0100 | [diff] [blame] | 114 | if (!gpiochip_line_is_open_drain(gc, offset)) { |
Nicolas Saenz Julienne | c366c76 | 2016-01-30 17:06:44 +0000 | [diff] [blame] | 115 | dev_err(gc->parent, "GPO3 works only as open drain\n"); |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | |
| 119 | /* Disable sequencer for GPO3 */ |
| 120 | ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7, |
| 121 | TPS65218_SEQ7_GPO3_SEQ_MASK, |
| 122 | TPS65218_PROTECT_L1); |
| 123 | if (ret) |
| 124 | return ret; |
| 125 | |
| 126 | /* Setup GPO3 */ |
| 127 | ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG2, |
| 128 | TPS65218_CONFIG2_DC12_RST, |
| 129 | TPS65218_PROTECT_L1); |
| 130 | if (ret) |
| 131 | return ret; |
| 132 | |
| 133 | break; |
| 134 | default: |
| 135 | return -EINVAL; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Linus Walleij | f30e49f | 2016-04-08 14:13:53 +0200 | [diff] [blame] | 141 | static int tps65218_gpio_set_single_ended(struct gpio_chip *gc, |
| 142 | unsigned offset, |
| 143 | enum single_ended_mode mode) |
| 144 | { |
| 145 | struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc); |
| 146 | struct tps65218 *tps65218 = tps65218_gpio->tps65218; |
| 147 | |
| 148 | switch (offset) { |
| 149 | case 0: |
| 150 | case 2: |
| 151 | /* GPO1 is hardwired to be open drain */ |
| 152 | if (mode == LINE_MODE_OPEN_DRAIN) |
| 153 | return 0; |
| 154 | return -ENOTSUPP; |
| 155 | case 1: |
| 156 | /* GPO2 is push-pull by default, can be set as open drain. */ |
| 157 | if (mode == LINE_MODE_OPEN_DRAIN) |
| 158 | return tps65218_clear_bits(tps65218, |
| 159 | TPS65218_REG_CONFIG1, |
| 160 | TPS65218_CONFIG1_GPO2_BUF, |
| 161 | TPS65218_PROTECT_L1); |
| 162 | if (mode == LINE_MODE_PUSH_PULL) |
| 163 | return tps65218_set_bits(tps65218, |
| 164 | TPS65218_REG_CONFIG1, |
| 165 | TPS65218_CONFIG1_GPO2_BUF, |
| 166 | TPS65218_CONFIG1_GPO2_BUF, |
| 167 | TPS65218_PROTECT_L1); |
| 168 | return -ENOTSUPP; |
| 169 | default: |
| 170 | break; |
| 171 | } |
| 172 | return -ENOTSUPP; |
| 173 | } |
| 174 | |
Julia Lawall | e35b5ab | 2016-09-11 14:14:37 +0200 | [diff] [blame] | 175 | static const struct gpio_chip template_chip = { |
Nicolas Saenz Julienne | c366c76 | 2016-01-30 17:06:44 +0000 | [diff] [blame] | 176 | .label = "gpio-tps65218", |
| 177 | .owner = THIS_MODULE, |
| 178 | .request = tps65218_gpio_request, |
| 179 | .direction_output = tps65218_gpio_output, |
| 180 | .direction_input = tps65218_gpio_input, |
| 181 | .get = tps65218_gpio_get, |
| 182 | .set = tps65218_gpio_set, |
Linus Walleij | f30e49f | 2016-04-08 14:13:53 +0200 | [diff] [blame] | 183 | .set_single_ended = tps65218_gpio_set_single_ended, |
Nicolas Saenz Julienne | c366c76 | 2016-01-30 17:06:44 +0000 | [diff] [blame] | 184 | .can_sleep = true, |
| 185 | .ngpio = 3, |
| 186 | .base = -1, |
| 187 | }; |
| 188 | |
| 189 | static int tps65218_gpio_probe(struct platform_device *pdev) |
| 190 | { |
| 191 | struct tps65218 *tps65218 = dev_get_drvdata(pdev->dev.parent); |
| 192 | struct tps65218_gpio *tps65218_gpio; |
| 193 | int ret; |
| 194 | |
| 195 | tps65218_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps65218_gpio), |
| 196 | GFP_KERNEL); |
| 197 | if (!tps65218_gpio) |
| 198 | return -ENOMEM; |
| 199 | |
| 200 | tps65218_gpio->tps65218 = tps65218; |
| 201 | tps65218_gpio->gpio_chip = template_chip; |
| 202 | tps65218_gpio->gpio_chip.parent = &pdev->dev; |
| 203 | #ifdef CONFIG_OF_GPIO |
| 204 | tps65218_gpio->gpio_chip.of_node = pdev->dev.of_node; |
| 205 | #endif |
| 206 | |
Wei Yongjun | 8df759c | 2016-09-16 01:51:10 +0000 | [diff] [blame^] | 207 | ret = devm_gpiochip_add_data(&pdev->dev, &tps65218_gpio->gpio_chip, |
| 208 | tps65218_gpio); |
Nicolas Saenz Julienne | c366c76 | 2016-01-30 17:06:44 +0000 | [diff] [blame] | 209 | if (ret < 0) { |
| 210 | dev_err(&pdev->dev, "Failed to register gpiochip, %d\n", ret); |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | platform_set_drvdata(pdev, tps65218_gpio); |
| 215 | |
| 216 | return ret; |
| 217 | } |
| 218 | |
Nicolas Saenz Julienne | c366c76 | 2016-01-30 17:06:44 +0000 | [diff] [blame] | 219 | static const struct of_device_id tps65218_dt_match[] = { |
| 220 | { .compatible = "ti,tps65218-gpio" }, |
| 221 | { } |
| 222 | }; |
| 223 | MODULE_DEVICE_TABLE(of, tps65218_dt_match); |
| 224 | |
Keerthy | a944a89 | 2016-06-28 18:00:52 +0530 | [diff] [blame] | 225 | static const struct platform_device_id tps65218_gpio_id_table[] = { |
| 226 | { "tps65218-gpio", }, |
| 227 | { /* sentinel */ } |
| 228 | }; |
| 229 | MODULE_DEVICE_TABLE(platform, tps65218_gpio_id_table); |
| 230 | |
Nicolas Saenz Julienne | c366c76 | 2016-01-30 17:06:44 +0000 | [diff] [blame] | 231 | static struct platform_driver tps65218_gpio_driver = { |
| 232 | .driver = { |
| 233 | .name = "tps65218-gpio", |
| 234 | .of_match_table = of_match_ptr(tps65218_dt_match) |
| 235 | }, |
| 236 | .probe = tps65218_gpio_probe, |
Keerthy | a944a89 | 2016-06-28 18:00:52 +0530 | [diff] [blame] | 237 | .id_table = tps65218_gpio_id_table, |
Nicolas Saenz Julienne | c366c76 | 2016-01-30 17:06:44 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | module_platform_driver(tps65218_gpio_driver); |
| 241 | |
| 242 | MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>"); |
| 243 | MODULE_DESCRIPTION("GPO interface for TPS65218 PMICs"); |
| 244 | MODULE_LICENSE("GPL v2"); |
| 245 | MODULE_ALIAS("platform:tps65218-gpio"); |