Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | /* |
| 13 | * Qualcomm PMIC8058 GPIO driver |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/gpio.h> |
| 19 | #include <linux/mfd/pmic8058.h> |
| 20 | #include <linux/debugfs.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/seq_file.h> |
| 24 | |
| 25 | #ifndef CONFIG_GPIOLIB |
| 26 | #include "gpio_chip.h" |
| 27 | #endif |
| 28 | |
| 29 | /* GPIO registers */ |
| 30 | #define SSBI_REG_ADDR_GPIO_BASE 0x150 |
| 31 | #define SSBI_REG_ADDR_GPIO(n) (SSBI_REG_ADDR_GPIO_BASE + n) |
| 32 | |
| 33 | /* GPIO */ |
| 34 | #define PM8058_GPIO_BANK_MASK 0x70 |
| 35 | #define PM8058_GPIO_BANK_SHIFT 4 |
| 36 | #define PM8058_GPIO_WRITE 0x80 |
| 37 | |
| 38 | /* Bank 0 */ |
| 39 | #define PM8058_GPIO_VIN_MASK 0x0E |
| 40 | #define PM8058_GPIO_VIN_SHIFT 1 |
| 41 | #define PM8058_GPIO_MODE_ENABLE 0x01 |
| 42 | |
| 43 | /* Bank 1 */ |
| 44 | #define PM8058_GPIO_MODE_MASK 0x0C |
| 45 | #define PM8058_GPIO_MODE_SHIFT 2 |
| 46 | #define PM8058_GPIO_OUT_BUFFER 0x02 |
| 47 | #define PM8058_GPIO_OUT_INVERT 0x01 |
| 48 | |
| 49 | #define PM8058_GPIO_MODE_OFF 3 |
| 50 | #define PM8058_GPIO_MODE_OUTPUT 2 |
| 51 | #define PM8058_GPIO_MODE_INPUT 0 |
| 52 | #define PM8058_GPIO_MODE_BOTH 1 |
| 53 | |
| 54 | /* Bank 2 */ |
| 55 | #define PM8058_GPIO_PULL_MASK 0x0E |
| 56 | #define PM8058_GPIO_PULL_SHIFT 1 |
| 57 | |
| 58 | /* Bank 3 */ |
| 59 | #define PM8058_GPIO_OUT_STRENGTH_MASK 0x0C |
| 60 | #define PM8058_GPIO_OUT_STRENGTH_SHIFT 2 |
| 61 | #define PM8058_GPIO_PIN_ENABLE 0x00 |
| 62 | #define PM8058_GPIO_PIN_DISABLE 0x01 |
| 63 | |
| 64 | /* Bank 4 */ |
| 65 | #define PM8058_GPIO_FUNC_MASK 0x0E |
| 66 | #define PM8058_GPIO_FUNC_SHIFT 1 |
| 67 | |
| 68 | /* Bank 5 */ |
| 69 | #define PM8058_GPIO_NON_INT_POL_INV 0x08 |
| 70 | #define PM8058_GPIO_BANKS 6 |
| 71 | |
| 72 | struct pm8058_gpio_chip { |
| 73 | struct gpio_chip gpio_chip; |
| 74 | struct pm8058_chip *pm_chip; |
| 75 | struct mutex pm_lock; |
| 76 | u8 bank1[PM8058_GPIOS]; |
| 77 | }; |
| 78 | |
| 79 | static int pm8058_gpio_get(struct pm8058_gpio_chip *chip, unsigned gpio) |
| 80 | { |
| 81 | struct pm8058_gpio_platform_data *pdata; |
| 82 | int mode; |
| 83 | |
| 84 | if (gpio >= PM8058_GPIOS || chip == NULL) |
| 85 | return -EINVAL; |
| 86 | |
| 87 | pdata = chip->gpio_chip.dev->platform_data; |
| 88 | |
| 89 | /* Get gpio value from config bank 1 if output gpio. |
| 90 | Get gpio value from IRQ RT status register for all other gpio modes. |
| 91 | */ |
| 92 | mode = (chip->bank1[gpio] & PM8058_GPIO_MODE_MASK) >> |
| 93 | PM8058_GPIO_MODE_SHIFT; |
| 94 | if (mode == PM8058_GPIO_MODE_OUTPUT) |
| 95 | return chip->bank1[gpio] & PM8058_GPIO_OUT_INVERT; |
| 96 | else |
| 97 | return pm8058_irq_get_rt_status(chip->pm_chip, |
| 98 | pdata->irq_base + gpio); |
| 99 | } |
| 100 | |
| 101 | static int pm8058_gpio_set(struct pm8058_gpio_chip *chip, |
| 102 | unsigned gpio, int value) |
| 103 | { |
| 104 | int rc; |
| 105 | u8 bank1; |
| 106 | |
| 107 | if (gpio >= PM8058_GPIOS || chip == NULL) |
| 108 | return -EINVAL; |
| 109 | |
| 110 | mutex_lock(&chip->pm_lock); |
| 111 | bank1 = chip->bank1[gpio] & ~PM8058_GPIO_OUT_INVERT; |
| 112 | |
| 113 | if (value) |
| 114 | bank1 |= PM8058_GPIO_OUT_INVERT; |
| 115 | |
| 116 | chip->bank1[gpio] = bank1; |
| 117 | rc = pm8058_write(chip->pm_chip, SSBI_REG_ADDR_GPIO(gpio), &bank1, 1); |
| 118 | mutex_unlock(&chip->pm_lock); |
| 119 | |
| 120 | if (rc) |
| 121 | pr_err("%s: FAIL pm8058_write(): rc=%d. " |
| 122 | "(gpio=%d, value=%d)\n", |
| 123 | __func__, rc, gpio, value); |
| 124 | |
| 125 | return rc; |
| 126 | } |
| 127 | |
| 128 | static int pm8058_gpio_set_direction(struct pm8058_gpio_chip *chip, |
| 129 | unsigned gpio, int direction) |
| 130 | { |
| 131 | int rc; |
| 132 | u8 bank1; |
| 133 | static int dir_map[] = { |
| 134 | PM8058_GPIO_MODE_OFF, |
| 135 | PM8058_GPIO_MODE_OUTPUT, |
| 136 | PM8058_GPIO_MODE_INPUT, |
| 137 | PM8058_GPIO_MODE_BOTH, |
| 138 | }; |
| 139 | |
| 140 | if (!direction || chip == NULL) |
| 141 | return -EINVAL; |
| 142 | |
| 143 | mutex_lock(&chip->pm_lock); |
| 144 | bank1 = chip->bank1[gpio] & ~PM8058_GPIO_MODE_MASK; |
| 145 | |
| 146 | bank1 |= ((dir_map[direction] << PM8058_GPIO_MODE_SHIFT) |
| 147 | & PM8058_GPIO_MODE_MASK); |
| 148 | |
| 149 | chip->bank1[gpio] = bank1; |
| 150 | rc = pm8058_write(chip->pm_chip, SSBI_REG_ADDR_GPIO(gpio), &bank1, 1); |
| 151 | mutex_unlock(&chip->pm_lock); |
| 152 | |
| 153 | if (rc) |
| 154 | pr_err("%s: Failed on pm8058_write(): rc=%d (GPIO config)\n", |
| 155 | __func__, rc); |
| 156 | |
| 157 | return rc; |
| 158 | } |
| 159 | |
| 160 | static int pm8058_gpio_init_bank1(struct pm8058_gpio_chip *chip) |
| 161 | { |
| 162 | int i, rc; |
| 163 | u8 bank; |
| 164 | |
| 165 | for (i = 0; i < PM8058_GPIOS; i++) { |
| 166 | bank = 1 << PM8058_GPIO_BANK_SHIFT; |
| 167 | rc = pm8058_write(chip->pm_chip, |
| 168 | SSBI_REG_ADDR_GPIO(i), |
| 169 | &bank, 1); |
| 170 | if (rc) { |
| 171 | pr_err("%s: error setting bank\n", __func__); |
| 172 | return rc; |
| 173 | } |
| 174 | |
| 175 | rc = pm8058_read(chip->pm_chip, |
| 176 | SSBI_REG_ADDR_GPIO(i), |
| 177 | &chip->bank1[i], 1); |
| 178 | if (rc) { |
| 179 | pr_err("%s: error reading bank 1\n", __func__); |
| 180 | return rc; |
| 181 | } |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | #ifndef CONFIG_GPIOLIB |
| 187 | static int pm8058_gpio_configure(struct gpio_chip *chip, |
| 188 | unsigned int gpio, |
| 189 | unsigned long flags) |
| 190 | { |
| 191 | int rc = 0, direction; |
| 192 | struct pm8058_gpio_chip *gpio_chip; |
| 193 | |
| 194 | gpio -= chip->start; |
| 195 | |
| 196 | if (flags & (GPIOF_INPUT | GPIOF_DRIVE_OUTPUT)) { |
| 197 | direction = 0; |
| 198 | if (flags & GPIOF_INPUT) |
| 199 | direction |= PM_GPIO_DIR_IN; |
| 200 | if (flags & GPIOF_DRIVE_OUTPUT) |
| 201 | direction |= PM_GPIO_DIR_OUT; |
| 202 | |
| 203 | gpio_chip = dev_get_drvdata(chip->dev); |
| 204 | |
| 205 | if (flags & (GPIOF_OUTPUT_LOW | GPIOF_OUTPUT_HIGH)) { |
| 206 | if (flags & GPIOF_OUTPUT_HIGH) |
| 207 | rc = pm8058_gpio_set(gpio_chip, |
| 208 | gpio, 1); |
| 209 | else |
| 210 | rc = pm8058_gpio_set(gpio_chip, |
| 211 | gpio, 0); |
| 212 | |
| 213 | if (rc) { |
| 214 | pr_err("%s: FAIL pm8058_gpio_set(): rc=%d.\n", |
| 215 | __func__, rc); |
| 216 | goto bail_out; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | rc = pm8058_gpio_set_direction(gpio_chip, |
| 221 | gpio, direction); |
| 222 | if (rc) |
| 223 | pr_err("%s: FAIL pm8058_gpio_config(): rc=%d.\n", |
| 224 | __func__, rc); |
| 225 | } |
| 226 | |
| 227 | bail_out: |
| 228 | return rc; |
| 229 | } |
| 230 | |
| 231 | static int pm8058_gpio_get_irq_num(struct gpio_chip *chip, |
| 232 | unsigned int gpio, |
| 233 | unsigned int *irqp, |
| 234 | unsigned long *irqnumflagsp) |
| 235 | { |
| 236 | struct pm8058_gpio_platform_data *pdata; |
| 237 | |
| 238 | pdata = chip->dev->platform_data; |
| 239 | gpio -= chip->start; |
| 240 | *irqp = pdata->irq_base + gpio; |
| 241 | if (irqnumflagsp) |
| 242 | *irqnumflagsp = 0; |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int pm8058_gpio_read(struct gpio_chip *chip, unsigned n) |
| 247 | { |
| 248 | struct pm8058_gpio_chip *gpio_chip; |
| 249 | |
| 250 | n -= chip->start; |
| 251 | gpio_chip = dev_get_drvdata(chip->dev); |
| 252 | return pm8058_gpio_get(gpio_chip, n); |
| 253 | } |
| 254 | |
| 255 | static int pm8058_gpio_write(struct gpio_chip *chip, unsigned n, unsigned on) |
| 256 | { |
| 257 | struct pm8058_gpio_chip *gpio_chip; |
| 258 | |
| 259 | n -= chip->start; |
| 260 | gpio_chip = dev_get_drvdata(chip->dev); |
| 261 | return pm8058_gpio_set(gpio_chip, n, on); |
| 262 | } |
| 263 | |
| 264 | static struct pm8058_gpio_chip pm8058_gpio_chip = { |
| 265 | .gpio_chip = { |
| 266 | .configure = pm8058_gpio_configure, |
| 267 | .get_irq_num = pm8058_gpio_get_irq_num, |
| 268 | .read = pm8058_gpio_read, |
| 269 | .write = pm8058_gpio_write, |
| 270 | }, |
| 271 | }; |
| 272 | |
| 273 | static int __devinit pm8058_gpio_probe(struct platform_device *pdev) |
| 274 | { |
| 275 | int rc = 0; |
| 276 | struct pm8058_gpio_platform_data *pdata = pdev->dev.platform_data; |
| 277 | |
| 278 | mutex_init(&pm8058_gpio_chip.pm_lock); |
| 279 | pm8058_gpio_chip.gpio_chip.dev = &pdev->dev; |
| 280 | pm8058_gpio_chip.gpio_chip.start = pdata->gpio_base; |
| 281 | pm8058_gpio_chip.gpio_chip.end = pdata->gpio_base + |
| 282 | PM8058_GPIOS - 1; |
| 283 | pm8058_gpio_chip.pm_chip = platform_get_drvdata(pdev); |
| 284 | platform_set_drvdata(pdev, &pm8058_gpio_chip); |
| 285 | |
| 286 | rc = register_gpio_chip(&pm8058_gpio_chip.gpio_chip); |
| 287 | if (!rc) |
| 288 | goto bail; |
| 289 | |
| 290 | rc = pm8058_gpio_init_bank1(&pm8058_gpio_chip); |
| 291 | if (rc) |
| 292 | goto bail; |
| 293 | |
| 294 | if (pdata->init) |
| 295 | rc = pdata->init(); |
| 296 | |
| 297 | bail: |
| 298 | if (rc) |
| 299 | platform_set_drvdata(pdev, pm8058_gpio_chip.pm_chip); |
| 300 | |
| 301 | pr_info("%s: register_gpio_chip(): rc=%d\n", __func__, rc); |
| 302 | return rc; |
| 303 | } |
| 304 | |
| 305 | static int __devexit pm8058_gpio_remove(struct platform_device *pdev) |
| 306 | { |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | #else |
| 311 | |
| 312 | static int pm8058_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 313 | { |
| 314 | struct pm8058_gpio_platform_data *pdata; |
| 315 | pdata = chip->dev->platform_data; |
| 316 | return pdata->irq_base + offset; |
| 317 | } |
| 318 | |
| 319 | static int pm8058_gpio_read(struct gpio_chip *chip, unsigned offset) |
| 320 | { |
| 321 | struct pm8058_gpio_chip *gpio_chip; |
| 322 | gpio_chip = dev_get_drvdata(chip->dev); |
| 323 | return pm8058_gpio_get(gpio_chip, offset); |
| 324 | } |
| 325 | |
| 326 | static void pm8058_gpio_write(struct gpio_chip *chip, |
| 327 | unsigned offset, int val) |
| 328 | { |
| 329 | struct pm8058_gpio_chip *gpio_chip; |
| 330 | gpio_chip = dev_get_drvdata(chip->dev); |
| 331 | pm8058_gpio_set(gpio_chip, offset, val); |
| 332 | } |
| 333 | |
| 334 | static int pm8058_gpio_direction_input(struct gpio_chip *chip, |
| 335 | unsigned offset) |
| 336 | { |
| 337 | struct pm8058_gpio_chip *gpio_chip; |
| 338 | gpio_chip = dev_get_drvdata(chip->dev); |
| 339 | return pm8058_gpio_set_direction(gpio_chip, offset, PM_GPIO_DIR_IN); |
| 340 | } |
| 341 | |
| 342 | static int pm8058_gpio_direction_output(struct gpio_chip *chip, |
| 343 | unsigned offset, |
| 344 | int val) |
| 345 | { |
| 346 | struct pm8058_gpio_chip *gpio_chip; |
| 347 | int ret; |
| 348 | |
| 349 | gpio_chip = dev_get_drvdata(chip->dev); |
| 350 | ret = pm8058_gpio_set_direction(gpio_chip, offset, PM_GPIO_DIR_OUT); |
| 351 | if (!ret) |
| 352 | ret = pm8058_gpio_set(gpio_chip, offset, val); |
| 353 | |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | static void pm8058_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 358 | { |
| 359 | static const char *cmode[] = { "in", "in/out", "out", "off" }; |
| 360 | struct pm8058_gpio_chip *gpio_chip = dev_get_drvdata(chip->dev); |
| 361 | u8 mode, state, bank; |
| 362 | const char *label; |
| 363 | int i, j; |
| 364 | |
| 365 | for (i = 0; i < PM8058_GPIOS; i++) { |
| 366 | label = gpiochip_is_requested(chip, i); |
| 367 | mode = (gpio_chip->bank1[i] & PM8058_GPIO_MODE_MASK) >> |
| 368 | PM8058_GPIO_MODE_SHIFT; |
| 369 | state = pm8058_gpio_get(gpio_chip, i); |
| 370 | seq_printf(s, "gpio-%-3d (%-12.12s) %-10.10s" |
| 371 | " %s", |
| 372 | chip->base + i, |
| 373 | label ? label : "--", |
| 374 | cmode[mode], |
| 375 | state ? "hi" : "lo"); |
| 376 | for (j = 0; j < PM8058_GPIO_BANKS; j++) { |
| 377 | bank = j << PM8058_GPIO_BANK_SHIFT; |
| 378 | pm8058_write(gpio_chip->pm_chip, |
| 379 | SSBI_REG_ADDR_GPIO(i), |
| 380 | &bank, 1); |
| 381 | pm8058_read(gpio_chip->pm_chip, |
| 382 | SSBI_REG_ADDR_GPIO(i), |
| 383 | &bank, 1); |
| 384 | seq_printf(s, " 0x%02x", bank); |
| 385 | } |
| 386 | seq_printf(s, "\n"); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | static struct pm8058_gpio_chip pm8058_gpio_chip = { |
| 391 | .gpio_chip = { |
| 392 | .label = "pm8058-gpio", |
| 393 | .direction_input = pm8058_gpio_direction_input, |
| 394 | .direction_output = pm8058_gpio_direction_output, |
| 395 | .to_irq = pm8058_gpio_to_irq, |
| 396 | .get = pm8058_gpio_read, |
| 397 | .set = pm8058_gpio_write, |
| 398 | .dbg_show = pm8058_gpio_dbg_show, |
| 399 | .ngpio = PM8058_GPIOS, |
| 400 | .can_sleep = 1, |
| 401 | }, |
| 402 | }; |
| 403 | |
| 404 | static int __devinit pm8058_gpio_probe(struct platform_device *pdev) |
| 405 | { |
| 406 | int ret; |
| 407 | struct pm8058_gpio_platform_data *pdata = pdev->dev.platform_data; |
| 408 | |
| 409 | mutex_init(&pm8058_gpio_chip.pm_lock); |
| 410 | pm8058_gpio_chip.gpio_chip.dev = &pdev->dev; |
| 411 | pm8058_gpio_chip.gpio_chip.base = pdata->gpio_base; |
| 412 | pm8058_gpio_chip.pm_chip = dev_get_drvdata(pdev->dev.parent); |
| 413 | platform_set_drvdata(pdev, &pm8058_gpio_chip); |
| 414 | |
| 415 | ret = gpiochip_add(&pm8058_gpio_chip.gpio_chip); |
| 416 | if (ret) |
| 417 | goto unset_drvdata; |
| 418 | |
| 419 | ret = pm8058_gpio_init_bank1(&pm8058_gpio_chip); |
| 420 | if (ret) |
| 421 | goto remove_chip; |
| 422 | |
| 423 | if (pdata->init) |
| 424 | ret = pdata->init(); |
| 425 | if (!ret) |
| 426 | goto ok; |
| 427 | |
| 428 | remove_chip: |
| 429 | if (gpiochip_remove(&pm8058_gpio_chip.gpio_chip)) |
| 430 | pr_err("%s: failed to remove gpio chip\n", __func__); |
| 431 | unset_drvdata: |
| 432 | platform_set_drvdata(pdev, pm8058_gpio_chip.pm_chip); |
| 433 | ok: |
| 434 | pr_info("%s: gpiochip_add(): rc=%d\n", __func__, ret); |
| 435 | |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | static int __devexit pm8058_gpio_remove(struct platform_device *pdev) |
| 440 | { |
| 441 | return gpiochip_remove(&pm8058_gpio_chip.gpio_chip); |
| 442 | } |
| 443 | |
| 444 | #endif |
| 445 | |
| 446 | int pm8058_gpio_config(int gpio, struct pm8058_gpio *param) |
| 447 | { |
| 448 | int rc; |
| 449 | u8 bank[8]; |
| 450 | static int dir_map[] = { |
| 451 | PM8058_GPIO_MODE_OFF, |
| 452 | PM8058_GPIO_MODE_OUTPUT, |
| 453 | PM8058_GPIO_MODE_INPUT, |
| 454 | PM8058_GPIO_MODE_BOTH, |
| 455 | }; |
| 456 | |
| 457 | if (param == NULL) |
| 458 | return -EINVAL; |
| 459 | |
| 460 | /* Select banks and configure the gpio */ |
| 461 | bank[0] = PM8058_GPIO_WRITE | |
| 462 | ((param->vin_sel << PM8058_GPIO_VIN_SHIFT) & |
| 463 | PM8058_GPIO_VIN_MASK) | |
| 464 | PM8058_GPIO_MODE_ENABLE; |
| 465 | bank[1] = PM8058_GPIO_WRITE | |
| 466 | ((1 << PM8058_GPIO_BANK_SHIFT) & |
| 467 | PM8058_GPIO_BANK_MASK) | |
| 468 | ((dir_map[param->direction] << |
| 469 | PM8058_GPIO_MODE_SHIFT) & |
| 470 | PM8058_GPIO_MODE_MASK) | |
| 471 | ((param->direction & PM_GPIO_DIR_OUT) ? |
| 472 | ((param->output_buffer & 1) ? |
| 473 | PM8058_GPIO_OUT_BUFFER : 0) : 0) | |
| 474 | ((param->direction & PM_GPIO_DIR_OUT) ? |
| 475 | param->output_value & 0x01 : 0); |
| 476 | bank[2] = PM8058_GPIO_WRITE | |
| 477 | ((2 << PM8058_GPIO_BANK_SHIFT) & |
| 478 | PM8058_GPIO_BANK_MASK) | |
| 479 | ((param->pull << PM8058_GPIO_PULL_SHIFT) & |
| 480 | PM8058_GPIO_PULL_MASK); |
| 481 | bank[3] = PM8058_GPIO_WRITE | |
| 482 | ((3 << PM8058_GPIO_BANK_SHIFT) & |
| 483 | PM8058_GPIO_BANK_MASK) | |
| 484 | ((param->out_strength << |
| 485 | PM8058_GPIO_OUT_STRENGTH_SHIFT) & |
| 486 | PM8058_GPIO_OUT_STRENGTH_MASK) | |
| 487 | (param->disable_pin ? |
| 488 | PM8058_GPIO_PIN_DISABLE : PM8058_GPIO_PIN_ENABLE); |
| 489 | bank[4] = PM8058_GPIO_WRITE | |
| 490 | ((4 << PM8058_GPIO_BANK_SHIFT) & |
| 491 | PM8058_GPIO_BANK_MASK) | |
| 492 | ((param->function << PM8058_GPIO_FUNC_SHIFT) & |
| 493 | PM8058_GPIO_FUNC_MASK); |
| 494 | bank[5] = PM8058_GPIO_WRITE | |
| 495 | ((5 << PM8058_GPIO_BANK_SHIFT) & PM8058_GPIO_BANK_MASK) | |
| 496 | (param->inv_int_pol ? 0 : PM8058_GPIO_NON_INT_POL_INV); |
| 497 | |
| 498 | mutex_lock(&pm8058_gpio_chip.pm_lock); |
| 499 | /* Remember bank1 for later use */ |
| 500 | pm8058_gpio_chip.bank1[gpio] = bank[1]; |
| 501 | rc = pm8058_write(pm8058_gpio_chip.pm_chip, |
| 502 | SSBI_REG_ADDR_GPIO(gpio), bank, 6); |
| 503 | mutex_unlock(&pm8058_gpio_chip.pm_lock); |
| 504 | |
| 505 | if (rc) |
| 506 | pr_err("%s: Failed on pm8058_write(): rc=%d (GPIO config)\n", |
| 507 | __func__, rc); |
| 508 | |
| 509 | return rc; |
| 510 | } |
| 511 | EXPORT_SYMBOL(pm8058_gpio_config); |
| 512 | |
| 513 | static struct platform_driver pm8058_gpio_driver = { |
| 514 | .probe = pm8058_gpio_probe, |
| 515 | .remove = __devexit_p(pm8058_gpio_remove), |
| 516 | .driver = { |
| 517 | .name = "pm8058-gpio", |
| 518 | .owner = THIS_MODULE, |
| 519 | }, |
| 520 | }; |
| 521 | |
| 522 | #if defined(CONFIG_DEBUG_FS) |
| 523 | |
| 524 | #define DEBUG_MAX_RW_BUF 128 |
| 525 | #define DEBUG_MAX_FNAME 8 |
| 526 | |
| 527 | static struct dentry *debug_dent; |
| 528 | |
| 529 | static char debug_read_buf[DEBUG_MAX_RW_BUF]; |
| 530 | static char debug_write_buf[DEBUG_MAX_RW_BUF]; |
| 531 | |
| 532 | static int debug_gpios[PM8058_GPIOS]; |
| 533 | |
| 534 | static int debug_open(struct inode *inode, struct file *file) |
| 535 | { |
| 536 | file->private_data = inode->i_private; |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static int debug_read_gpio_bank(int gpio, int bank, u8 *data) |
| 541 | { |
| 542 | int rc; |
| 543 | |
| 544 | mutex_lock(&pm8058_gpio_chip.pm_lock); |
| 545 | |
| 546 | *data = bank << PM8058_GPIO_BANK_SHIFT; |
| 547 | rc = pm8058_write(pm8058_gpio_chip.pm_chip, |
| 548 | SSBI_REG_ADDR_GPIO(gpio), data, 1); |
| 549 | if (rc) |
| 550 | goto bail_out; |
| 551 | |
| 552 | *data = bank << PM8058_GPIO_BANK_SHIFT; |
| 553 | rc = pm8058_read(pm8058_gpio_chip.pm_chip, |
| 554 | SSBI_REG_ADDR_GPIO(gpio), data, 1); |
| 555 | |
| 556 | bail_out: |
| 557 | mutex_unlock(&pm8058_gpio_chip.pm_lock); |
| 558 | |
| 559 | return rc; |
| 560 | } |
| 561 | |
| 562 | static ssize_t debug_read(struct file *file, char __user *buf, |
| 563 | size_t count, loff_t *ppos) |
| 564 | { |
| 565 | int gpio = *((int *) file->private_data); |
| 566 | int len = 0; |
| 567 | int rc = -EINVAL; |
| 568 | u8 bank[PM8058_GPIO_BANKS]; |
| 569 | int val = -1; |
| 570 | int mode; |
| 571 | int i; |
| 572 | |
| 573 | for (i = 0; i < PM8058_GPIO_BANKS; i++) { |
| 574 | rc = debug_read_gpio_bank(gpio, i, &bank[i]); |
| 575 | if (rc) |
| 576 | pr_err("pmic failed to read bank %d\n", i); |
| 577 | } |
| 578 | |
| 579 | if (rc) { |
| 580 | len = snprintf(debug_read_buf, DEBUG_MAX_RW_BUF - 1, "-1\n"); |
| 581 | goto bail_out; |
| 582 | } |
| 583 | |
| 584 | val = pm8058_gpio_get(&pm8058_gpio_chip, gpio); |
| 585 | |
| 586 | /* print the mode and the value */ |
| 587 | mode = (bank[1] & PM8058_GPIO_MODE_MASK) >> PM8058_GPIO_MODE_SHIFT; |
| 588 | if (mode == PM8058_GPIO_MODE_BOTH) |
| 589 | len = snprintf(debug_read_buf, DEBUG_MAX_RW_BUF - 1, |
| 590 | "BOTH %d ", val); |
| 591 | else if (mode == PM8058_GPIO_MODE_INPUT) |
| 592 | len = snprintf(debug_read_buf, DEBUG_MAX_RW_BUF - 1, |
| 593 | "IN %d ", val); |
| 594 | else if (mode == PM8058_GPIO_MODE_OUTPUT) |
| 595 | len = snprintf(debug_read_buf, DEBUG_MAX_RW_BUF - 1, |
| 596 | "OUT %d ", val); |
| 597 | else |
| 598 | len = snprintf(debug_read_buf, DEBUG_MAX_RW_BUF - 1, |
| 599 | "OFF %d ", val); |
| 600 | |
| 601 | /* print the control register values */ |
| 602 | len += snprintf(debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 603 | "[0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x]\n", |
| 604 | bank[0], bank[1], bank[2], bank[3], bank[4], bank[5]); |
| 605 | |
| 606 | bail_out: |
| 607 | rc = simple_read_from_buffer((void __user *) buf, len, |
| 608 | ppos, (void *) debug_read_buf, len); |
| 609 | |
| 610 | return rc; |
| 611 | } |
| 612 | |
| 613 | static ssize_t debug_write(struct file *file, const char __user *buf, |
| 614 | size_t count, loff_t *ppos) |
| 615 | { |
| 616 | int gpio = *((int *) file->private_data); |
| 617 | unsigned long val; |
| 618 | int mode, rc; |
| 619 | |
| 620 | mode = (pm8058_gpio_chip.bank1[gpio] & PM8058_GPIO_MODE_MASK) >> |
| 621 | PM8058_GPIO_MODE_SHIFT; |
| 622 | if (mode == PM8058_GPIO_MODE_OFF || mode == PM8058_GPIO_MODE_INPUT) |
| 623 | return count; |
| 624 | |
| 625 | if (count > sizeof(debug_write_buf)) |
| 626 | return -EFAULT; |
| 627 | |
| 628 | if (copy_from_user(debug_write_buf, buf, count)) { |
| 629 | pr_err("failed to copy from user\n"); |
| 630 | return -EFAULT; |
| 631 | } |
| 632 | debug_write_buf[count] = '\0'; |
| 633 | |
| 634 | rc = strict_strtoul(debug_write_buf, 10, &val); |
| 635 | if (rc) |
| 636 | return rc; |
| 637 | |
| 638 | if (pm8058_gpio_set(&pm8058_gpio_chip, gpio, val)) { |
| 639 | pr_err("gpio write failed\n"); |
| 640 | return -EINVAL; |
| 641 | } |
| 642 | |
| 643 | return count; |
| 644 | } |
| 645 | |
| 646 | static const struct file_operations debug_ops = { |
| 647 | .open = debug_open, |
| 648 | .read = debug_read, |
| 649 | .write = debug_write, |
| 650 | }; |
| 651 | |
| 652 | static void debug_init(void) |
| 653 | { |
| 654 | int i; |
| 655 | char name[DEBUG_MAX_FNAME]; |
| 656 | |
| 657 | debug_dent = debugfs_create_dir("pm_gpio", NULL); |
| 658 | if (IS_ERR(debug_dent)) { |
| 659 | pr_err("pmic8058 debugfs_create_dir fail, error %ld\n", |
| 660 | PTR_ERR(debug_dent)); |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | for (i = 0; i < PM8058_GPIOS; i++) { |
| 665 | snprintf(name, DEBUG_MAX_FNAME-1, "%d", i+1); |
| 666 | debug_gpios[i] = i; |
| 667 | if (debugfs_create_file(name, 0644, debug_dent, |
| 668 | &debug_gpios[i], &debug_ops) == NULL) { |
| 669 | pr_err("pmic8058 debugfs_create_file %s failed\n", |
| 670 | name); |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | static void debug_exit(void) |
| 676 | { |
| 677 | debugfs_remove_recursive(debug_dent); |
| 678 | } |
| 679 | |
| 680 | #else |
| 681 | static void debug_init(void) { } |
| 682 | static void debug_exit(void) { } |
| 683 | #endif |
| 684 | |
| 685 | static int __init pm8058_gpio_init(void) |
| 686 | { |
| 687 | int rc = platform_driver_register(&pm8058_gpio_driver); |
| 688 | if (!rc) |
| 689 | debug_init(); |
| 690 | return rc; |
| 691 | } |
| 692 | |
| 693 | static void __exit pm8058_gpio_exit(void) |
| 694 | { |
| 695 | platform_driver_unregister(&pm8058_gpio_driver); |
| 696 | debug_exit(); |
| 697 | } |
| 698 | |
| 699 | subsys_initcall(pm8058_gpio_init); |
| 700 | module_exit(pm8058_gpio_exit); |
| 701 | |
| 702 | MODULE_LICENSE("GPL v2"); |
| 703 | MODULE_DESCRIPTION("PMIC8058 GPIO driver"); |
| 704 | MODULE_VERSION("1.0"); |
| 705 | MODULE_ALIAS("platform:pm8058-gpio"); |