Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Qualcomm PMIC8XXX GPIO driver based on RPC |
| 3 | * |
| 4 | * Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 and |
| 8 | * only version 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 17 | |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/gpio.h> |
| 20 | #include <linux/gpio-pm8xxx-rpc.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/seq_file.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <mach/pmic.h> |
| 26 | |
| 27 | struct pm8xxx_gpio_rpc_chip { |
| 28 | struct list_head link; |
| 29 | struct gpio_chip gpio_chip; |
| 30 | }; |
| 31 | |
| 32 | static LIST_HEAD(pm8xxx_gpio_rpc_chips); |
| 33 | static DEFINE_MUTEX(pm8xxx_gpio_chips_lock); |
| 34 | |
| 35 | static int pm8xxx_gpio_rpc_get(struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip, |
| 36 | unsigned gpio) |
| 37 | { |
| 38 | int rc; |
| 39 | |
| 40 | if (gpio >= pm8xxx_gpio_chip->gpio_chip.ngpio |
| 41 | || pm8xxx_gpio_chip == NULL) |
| 42 | return -EINVAL; |
| 43 | |
| 44 | rc = pmic_gpio_get_value(gpio); |
| 45 | |
| 46 | return rc; |
| 47 | } |
| 48 | |
| 49 | static int pm8xxx_gpio_rpc_set(struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip, |
| 50 | unsigned gpio, int value) |
| 51 | { |
| 52 | int rc; |
| 53 | |
| 54 | if (gpio >= pm8xxx_gpio_chip->gpio_chip.ngpio || |
| 55 | pm8xxx_gpio_chip == NULL) |
| 56 | return -EINVAL; |
| 57 | |
| 58 | rc = pmic_gpio_set_value(gpio, value); |
| 59 | |
| 60 | return rc; |
| 61 | } |
| 62 | |
| 63 | static int pm8xxx_gpio_rpc_set_direction(struct pm8xxx_gpio_rpc_chip |
| 64 | *pm8xxx_gpio_chip, unsigned gpio, int direction) |
| 65 | { |
| 66 | int rc = 0; |
| 67 | |
| 68 | if (!direction || pm8xxx_gpio_chip == NULL) |
| 69 | return -EINVAL; |
| 70 | |
| 71 | if (direction == PM_GPIO_DIR_IN) |
| 72 | rc = pmic_gpio_direction_input(gpio); |
| 73 | else if (direction == PM_GPIO_DIR_OUT) |
| 74 | rc = pmic_gpio_direction_output(gpio); |
| 75 | |
| 76 | return rc; |
| 77 | } |
| 78 | |
| 79 | static int pm8xxx_gpio_rpc_read(struct gpio_chip *gpio_chip, unsigned offset) |
| 80 | { |
| 81 | struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip = |
| 82 | dev_get_drvdata(gpio_chip->dev); |
| 83 | |
| 84 | return pm8xxx_gpio_rpc_get(pm8xxx_gpio_chip, offset); |
| 85 | } |
| 86 | |
| 87 | static void pm8xxx_gpio_rpc_write(struct gpio_chip *gpio_chip, |
| 88 | unsigned offset, int val) |
| 89 | { |
| 90 | struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip = |
| 91 | dev_get_drvdata(gpio_chip->dev); |
| 92 | |
| 93 | pm8xxx_gpio_rpc_set(pm8xxx_gpio_chip, offset, !!val); |
| 94 | } |
| 95 | |
| 96 | static int pm8xxx_gpio_rpc_direction_input(struct gpio_chip *gpio_chip, |
| 97 | unsigned offset) |
| 98 | { |
| 99 | struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip = |
| 100 | dev_get_drvdata(gpio_chip->dev); |
| 101 | |
| 102 | return pm8xxx_gpio_rpc_set_direction(pm8xxx_gpio_chip, offset, |
| 103 | PM_GPIO_DIR_IN); |
| 104 | } |
| 105 | |
| 106 | static int pm8xxx_gpio_rpc_direction_output(struct gpio_chip *gpio_chip, |
| 107 | unsigned offset, int val) |
| 108 | { |
| 109 | int ret = 0; |
| 110 | |
| 111 | struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip = |
| 112 | dev_get_drvdata(gpio_chip->dev); |
| 113 | |
| 114 | ret = pm8xxx_gpio_rpc_set_direction(pm8xxx_gpio_chip, offset, |
| 115 | PM_GPIO_DIR_OUT); |
| 116 | if (!ret) |
| 117 | ret = pm8xxx_gpio_rpc_set(pm8xxx_gpio_chip, offset, !!val); |
| 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | static void pm8xxx_gpio_rpc_dbg_show(struct seq_file *s, struct gpio_chip |
| 123 | *gpio_chip) |
| 124 | { |
| 125 | struct pm8xxx_gpio_rpc_chip *pmxx_gpio_chip = |
| 126 | dev_get_drvdata(gpio_chip->dev); |
| 127 | u8 state, mode; |
| 128 | const char *label; |
| 129 | int i; |
| 130 | |
| 131 | for (i = 0; i < gpio_chip->ngpio; i++) { |
| 132 | label = gpiochip_is_requested(gpio_chip, i); |
| 133 | state = pm8xxx_gpio_rpc_get(pmxx_gpio_chip, i); |
| 134 | mode = pmic_gpio_get_direction(i); |
| 135 | seq_printf(s, "gpio-%-3d (%-12.12s) %s %s", |
| 136 | gpio_chip->base + i, |
| 137 | label ? label : " ", mode ? "out" : "in", |
| 138 | state ? "hi" : "lo"); |
| 139 | seq_printf(s, "\n"); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static int __devinit pm8xxx_gpio_rpc_probe(struct platform_device *pdev) |
| 144 | { |
| 145 | int ret; |
| 146 | struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip; |
| 147 | const struct pm8xxx_gpio_rpc_platform_data *pdata = |
| 148 | pdev->dev.platform_data; |
| 149 | |
| 150 | if (!pdata) { |
| 151 | pr_err("missing platform data\n"); |
| 152 | return -EINVAL; |
| 153 | } |
| 154 | |
| 155 | pm8xxx_gpio_chip = kzalloc(sizeof(struct pm8xxx_gpio_rpc_chip), |
| 156 | GFP_KERNEL); |
| 157 | if (!pm8xxx_gpio_chip) { |
| 158 | pr_err("Cannot allocate pm8xxx_gpio_chip\n"); |
| 159 | return -ENOMEM; |
| 160 | } |
| 161 | |
| 162 | pm8xxx_gpio_chip->gpio_chip.label = "pm8xxx-gpio-rpc"; |
| 163 | pm8xxx_gpio_chip->gpio_chip.direction_input = |
| 164 | pm8xxx_gpio_rpc_direction_input; |
| 165 | pm8xxx_gpio_chip->gpio_chip.direction_output = |
| 166 | pm8xxx_gpio_rpc_direction_output; |
| 167 | pm8xxx_gpio_chip->gpio_chip.get = pm8xxx_gpio_rpc_read; |
| 168 | pm8xxx_gpio_chip->gpio_chip.set = pm8xxx_gpio_rpc_write; |
| 169 | pm8xxx_gpio_chip->gpio_chip.dbg_show = pm8xxx_gpio_rpc_dbg_show; |
| 170 | pm8xxx_gpio_chip->gpio_chip.ngpio = pdata->ngpios; |
| 171 | pm8xxx_gpio_chip->gpio_chip.can_sleep = 1; |
| 172 | pm8xxx_gpio_chip->gpio_chip.dev = &pdev->dev; |
| 173 | pm8xxx_gpio_chip->gpio_chip.base = pdata->gpio_base; |
| 174 | |
| 175 | mutex_lock(&pm8xxx_gpio_chips_lock); |
| 176 | list_add(&pm8xxx_gpio_chip->link, &pm8xxx_gpio_rpc_chips); |
| 177 | mutex_unlock(&pm8xxx_gpio_chips_lock); |
| 178 | platform_set_drvdata(pdev, pm8xxx_gpio_chip); |
| 179 | |
| 180 | ret = gpiochip_add(&pm8xxx_gpio_chip->gpio_chip); |
| 181 | if (ret) { |
| 182 | pr_err("gpiochip_add failed ret = %d\n", ret); |
| 183 | goto reset_drvdata; |
| 184 | } |
| 185 | |
| 186 | pr_info("OK: base=%d, ngpio=%d\n", pm8xxx_gpio_chip->gpio_chip.base, |
| 187 | pm8xxx_gpio_chip->gpio_chip.ngpio); |
| 188 | |
| 189 | return 0; |
| 190 | |
| 191 | reset_drvdata: |
| 192 | mutex_lock(&pm8xxx_gpio_chips_lock); |
| 193 | list_del(&pm8xxx_gpio_chip->link); |
| 194 | mutex_unlock(&pm8xxx_gpio_chips_lock); |
| 195 | platform_set_drvdata(pdev, NULL); |
| 196 | kfree(pm8xxx_gpio_chip); |
| 197 | mutex_destroy(&pm8xxx_gpio_chips_lock); |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static int __devexit pm8xxx_gpio_rpc_remove(struct platform_device *pdev) |
| 202 | { |
| 203 | struct pm8xxx_gpio_rpc_chip *pm8xxx_gpio_chip = |
| 204 | platform_get_drvdata(pdev); |
| 205 | |
| 206 | mutex_lock(&pm8xxx_gpio_chips_lock); |
| 207 | list_del(&pm8xxx_gpio_chip->link); |
| 208 | mutex_unlock(&pm8xxx_gpio_chips_lock); |
| 209 | platform_set_drvdata(pdev, NULL); |
| 210 | if (gpiochip_remove(&pm8xxx_gpio_chip->gpio_chip)) |
| 211 | pr_err("failed to remove gpio chip\n"); |
| 212 | kfree(pm8xxx_gpio_chip); |
| 213 | mutex_destroy(&pm8xxx_gpio_chips_lock); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static struct platform_driver pm8xxx_gpio_rpc_driver = { |
| 218 | .probe = pm8xxx_gpio_rpc_probe, |
| 219 | .remove = __devexit_p(pm8xxx_gpio_rpc_remove), |
| 220 | .driver = { |
| 221 | .name = PM8XXX_GPIO_DEV_NAME, |
| 222 | .owner = THIS_MODULE, |
| 223 | }, |
| 224 | }; |
| 225 | |
| 226 | static int __init pm8xxx_gpio_rpc_init(void) |
| 227 | { |
| 228 | return platform_driver_register(&pm8xxx_gpio_rpc_driver); |
| 229 | } |
| 230 | postcore_initcall(pm8xxx_gpio_rpc_init); |
| 231 | |
| 232 | static void __exit pm8xxx_gpio_rpc_exit(void) |
| 233 | { |
| 234 | platform_driver_unregister(&pm8xxx_gpio_rpc_driver); |
| 235 | } |
| 236 | module_exit(pm8xxx_gpio_rpc_exit); |
| 237 | |
| 238 | MODULE_LICENSE("GPL v2"); |
| 239 | MODULE_DESCRIPTION("PMIC GPIO driver based on RPC"); |
| 240 | MODULE_VERSION("1.0"); |
| 241 | MODULE_ALIAS("platform:" PM8XXX_GPIO_DEV_NAME); |