Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Access to GPOs on TWL6040 chip |
| 3 | * |
| 4 | * Copyright (C) 2012 Texas Instruments, Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Sergio Aguirre <saaguirre@ti.com> |
| 8 | * Peter Ujfalusi <peter.ujfalusi@ti.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/kthread.h> |
| 28 | #include <linux/irq.h> |
Linus Walleij | fc4f8f3 | 2018-09-03 09:54:55 +0200 | [diff] [blame^] | 29 | #include <linux/gpio/driver.h> |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/of.h> |
| 32 | |
| 33 | #include <linux/mfd/twl6040.h> |
| 34 | |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 35 | static int twl6040gpo_get(struct gpio_chip *chip, unsigned offset) |
| 36 | { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 37 | struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent); |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 38 | int ret = 0; |
| 39 | |
| 40 | ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL); |
| 41 | if (ret < 0) |
| 42 | return ret; |
| 43 | |
| 44 | return (ret >> offset) & 1; |
| 45 | } |
| 46 | |
| 47 | static int twl6040gpo_direction_out(struct gpio_chip *chip, unsigned offset, |
| 48 | int value) |
| 49 | { |
| 50 | /* This only drives GPOs, and can't change direction */ |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static void twl6040gpo_set(struct gpio_chip *chip, unsigned offset, int value) |
| 55 | { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 56 | struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent); |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 57 | int ret; |
| 58 | u8 gpoctl; |
| 59 | |
| 60 | ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL); |
| 61 | if (ret < 0) |
| 62 | return; |
| 63 | |
| 64 | if (value) |
| 65 | gpoctl = ret | (1 << offset); |
| 66 | else |
| 67 | gpoctl = ret & ~(1 << offset); |
| 68 | |
| 69 | twl6040_reg_write(twl6040, TWL6040_REG_GPOCTL, gpoctl); |
| 70 | } |
| 71 | |
| 72 | static struct gpio_chip twl6040gpo_chip = { |
| 73 | .label = "twl6040", |
| 74 | .owner = THIS_MODULE, |
| 75 | .get = twl6040gpo_get, |
| 76 | .direction_output = twl6040gpo_direction_out, |
| 77 | .set = twl6040gpo_set, |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 78 | .can_sleep = true, |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /*----------------------------------------------------------------------*/ |
| 82 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 83 | static int gpo_twl6040_probe(struct platform_device *pdev) |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 84 | { |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 85 | struct device *twl6040_core_dev = pdev->dev.parent; |
| 86 | struct twl6040 *twl6040 = dev_get_drvdata(twl6040_core_dev); |
| 87 | int ret; |
| 88 | |
Peter Ujfalusi | a5d28d7 | 2013-07-12 13:30:43 +0200 | [diff] [blame] | 89 | twl6040gpo_chip.base = -1; |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 90 | |
| 91 | if (twl6040_get_revid(twl6040) < TWL6041_REV_ES2_0) |
| 92 | twl6040gpo_chip.ngpio = 3; /* twl6040 have 3 GPO */ |
| 93 | else |
| 94 | twl6040gpo_chip.ngpio = 1; /* twl6041 have 1 GPO */ |
| 95 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 96 | twl6040gpo_chip.parent = &pdev->dev; |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 97 | #ifdef CONFIG_OF_GPIO |
| 98 | twl6040gpo_chip.of_node = twl6040_core_dev->of_node; |
| 99 | #endif |
| 100 | |
Laxman Dewangan | fc0292a | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 101 | ret = devm_gpiochip_add_data(&pdev->dev, &twl6040gpo_chip, NULL); |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 102 | if (ret < 0) { |
| 103 | dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret); |
| 104 | twl6040gpo_chip.ngpio = 0; |
| 105 | } |
| 106 | |
| 107 | return ret; |
| 108 | } |
| 109 | |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 110 | /* Note: this hardware lives inside an I2C-based multi-function device. */ |
| 111 | MODULE_ALIAS("platform:twl6040-gpo"); |
| 112 | |
| 113 | static struct platform_driver gpo_twl6040_driver = { |
| 114 | .driver = { |
| 115 | .name = "twl6040-gpo", |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 116 | }, |
| 117 | .probe = gpo_twl6040_probe, |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | module_platform_driver(gpo_twl6040_driver); |
| 121 | |
| 122 | MODULE_AUTHOR("Texas Instruments, Inc."); |
| 123 | MODULE_DESCRIPTION("GPO interface for TWL6040"); |
| 124 | MODULE_LICENSE("GPL"); |