Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 1 | /* |
Grant Likely | c103de2 | 2011-06-04 18:38:28 -0600 | [diff] [blame] | 2 | * TI TPS6591x GPIO driver |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2010 Texas Instruments Inc. |
| 5 | * |
| 6 | * Author: Graeme Gregory <gg@slimlogic.co.uk> |
| 7 | * Author: Jorge Eduardo Candelaria jedu@slimlogic.co.uk> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/gpio.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/mfd/tps65910.h> |
| 22 | |
| 23 | static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 24 | { |
| 25 | struct tps65910 *tps65910 = container_of(gc, struct tps65910, gpio); |
| 26 | uint8_t val; |
| 27 | |
Jorge Eduardo Candelaria | 11ad14f | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 28 | tps65910->read(tps65910, TPS65910_GPIO0 + offset, 1, &val); |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 29 | |
Jorge Eduardo Candelaria | 11ad14f | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 30 | if (val & GPIO_STS_MASK) |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 31 | return 1; |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset, |
| 37 | int value) |
| 38 | { |
| 39 | struct tps65910 *tps65910 = container_of(gc, struct tps65910, gpio); |
| 40 | |
| 41 | if (value) |
Jorge Eduardo Candelaria | 11ad14f | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 42 | tps65910_set_bits(tps65910, TPS65910_GPIO0 + offset, |
| 43 | GPIO_SET_MASK); |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 44 | else |
Jorge Eduardo Candelaria | 11ad14f | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 45 | tps65910_clear_bits(tps65910, TPS65910_GPIO0 + offset, |
| 46 | GPIO_SET_MASK); |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset, |
| 50 | int value) |
| 51 | { |
| 52 | struct tps65910 *tps65910 = container_of(gc, struct tps65910, gpio); |
| 53 | |
| 54 | /* Set the initial value */ |
Laxman Dewangan | 94bd244 | 2012-01-18 20:07:35 +0530 | [diff] [blame] | 55 | tps65910_gpio_set(gc, offset, value); |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 56 | |
Jorge Eduardo Candelaria | 11ad14f | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 57 | return tps65910_set_bits(tps65910, TPS65910_GPIO0 + offset, |
| 58 | GPIO_CFG_MASK); |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset) |
| 62 | { |
| 63 | struct tps65910 *tps65910 = container_of(gc, struct tps65910, gpio); |
| 64 | |
Jorge Eduardo Candelaria | 11ad14f | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 65 | return tps65910_clear_bits(tps65910, TPS65910_GPIO0 + offset, |
| 66 | GPIO_CFG_MASK); |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void tps65910_gpio_init(struct tps65910 *tps65910, int gpio_base) |
| 70 | { |
| 71 | int ret; |
Laxman Dewangan | 9467d29 | 2012-02-01 12:09:04 +0530 | [diff] [blame] | 72 | struct tps65910_board *board_data; |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 73 | |
| 74 | if (!gpio_base) |
| 75 | return; |
| 76 | |
| 77 | tps65910->gpio.owner = THIS_MODULE; |
| 78 | tps65910->gpio.label = tps65910->i2c_client->name; |
| 79 | tps65910->gpio.dev = tps65910->dev; |
| 80 | tps65910->gpio.base = gpio_base; |
Jorge Eduardo Candelaria | 11ad14f | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 81 | |
| 82 | switch(tps65910_chip_id(tps65910)) { |
| 83 | case TPS65910: |
Laxman Dewangan | 9467d29 | 2012-02-01 12:09:04 +0530 | [diff] [blame] | 84 | tps65910->gpio.ngpio = TPS65910_NUM_GPIO; |
Axel Lin | 58956ba | 2011-07-06 10:08:27 +0800 | [diff] [blame] | 85 | break; |
Jorge Eduardo Candelaria | 11ad14f | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 86 | case TPS65911: |
Laxman Dewangan | 9467d29 | 2012-02-01 12:09:04 +0530 | [diff] [blame] | 87 | tps65910->gpio.ngpio = TPS65911_NUM_GPIO; |
Axel Lin | 58956ba | 2011-07-06 10:08:27 +0800 | [diff] [blame] | 88 | break; |
Jorge Eduardo Candelaria | 11ad14f | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 89 | default: |
| 90 | return; |
| 91 | } |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 92 | tps65910->gpio.can_sleep = 1; |
| 93 | |
| 94 | tps65910->gpio.direction_input = tps65910_gpio_input; |
| 95 | tps65910->gpio.direction_output = tps65910_gpio_output; |
| 96 | tps65910->gpio.set = tps65910_gpio_set; |
| 97 | tps65910->gpio.get = tps65910_gpio_get; |
| 98 | |
Laxman Dewangan | 9467d29 | 2012-02-01 12:09:04 +0530 | [diff] [blame] | 99 | /* Configure sleep control for gpios */ |
| 100 | board_data = dev_get_platdata(tps65910->dev); |
| 101 | if (board_data) { |
| 102 | int i; |
| 103 | for (i = 0; i < tps65910->gpio.ngpio; ++i) { |
| 104 | if (board_data->en_gpio_sleep[i]) { |
| 105 | ret = tps65910_set_bits(tps65910, |
| 106 | TPS65910_GPIO0 + i, GPIO_SLEEP_MASK); |
| 107 | if (ret < 0) |
| 108 | dev_warn(tps65910->dev, |
| 109 | "GPIO Sleep setting failed\n"); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 114 | ret = gpiochip_add(&tps65910->gpio); |
| 115 | |
| 116 | if (ret) |
| 117 | dev_warn(tps65910->dev, "GPIO registration failed: %d\n", ret); |
| 118 | } |