Linus Walleij | b4f53ed | 2018-09-03 09:55:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Access to GPOs on TWL6040 chip |
| 4 | * |
| 5 | * Copyright (C) 2012 Texas Instruments, Inc. |
| 6 | * |
| 7 | * Authors: |
| 8 | * Sergio Aguirre <saaguirre@ti.com> |
| 9 | * Peter Ujfalusi <peter.ujfalusi@ti.com> |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/kthread.h> |
| 15 | #include <linux/irq.h> |
Linus Walleij | fc4f8f3 | 2018-09-03 09:54:55 +0200 | [diff] [blame] | 16 | #include <linux/gpio/driver.h> |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
Linus Walleij | 4bef8bf | 2018-09-03 09:59:57 +0200 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 19 | #include <linux/of.h> |
| 20 | |
| 21 | #include <linux/mfd/twl6040.h> |
| 22 | |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 23 | static int twl6040gpo_get(struct gpio_chip *chip, unsigned offset) |
| 24 | { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 25 | struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent); |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 26 | int ret = 0; |
| 27 | |
| 28 | ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL); |
| 29 | if (ret < 0) |
| 30 | return ret; |
| 31 | |
Linus Walleij | 4bef8bf | 2018-09-03 09:59:57 +0200 | [diff] [blame] | 32 | return !!(ret & BIT(offset)); |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 33 | } |
| 34 | |
Linus Walleij | ba74bd5 | 2018-09-03 10:04:01 +0200 | [diff] [blame] | 35 | static int twl6040gpo_get_direction(struct gpio_chip *chip, unsigned offset) |
| 36 | { |
Matti Vaittinen | e42615e | 2019-11-06 10:54:12 +0200 | [diff] [blame] | 37 | return GPIO_LINE_DIRECTION_OUT; |
Linus Walleij | ba74bd5 | 2018-09-03 10:04:01 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 40 | static int twl6040gpo_direction_out(struct gpio_chip *chip, unsigned offset, |
| 41 | int value) |
| 42 | { |
| 43 | /* This only drives GPOs, and can't change direction */ |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static void twl6040gpo_set(struct gpio_chip *chip, unsigned offset, int value) |
| 48 | { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 49 | struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent); |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 50 | int ret; |
| 51 | u8 gpoctl; |
| 52 | |
| 53 | ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL); |
| 54 | if (ret < 0) |
| 55 | return; |
| 56 | |
| 57 | if (value) |
Linus Walleij | 4bef8bf | 2018-09-03 09:59:57 +0200 | [diff] [blame] | 58 | gpoctl = ret | BIT(offset); |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 59 | else |
Linus Walleij | 4bef8bf | 2018-09-03 09:59:57 +0200 | [diff] [blame] | 60 | gpoctl = ret & ~BIT(offset); |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 61 | |
| 62 | twl6040_reg_write(twl6040, TWL6040_REG_GPOCTL, gpoctl); |
| 63 | } |
| 64 | |
| 65 | static struct gpio_chip twl6040gpo_chip = { |
| 66 | .label = "twl6040", |
| 67 | .owner = THIS_MODULE, |
| 68 | .get = twl6040gpo_get, |
| 69 | .direction_output = twl6040gpo_direction_out, |
Linus Walleij | ba74bd5 | 2018-09-03 10:04:01 +0200 | [diff] [blame] | 70 | .get_direction = twl6040gpo_get_direction, |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 71 | .set = twl6040gpo_set, |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 72 | .can_sleep = true, |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /*----------------------------------------------------------------------*/ |
| 76 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 77 | static int gpo_twl6040_probe(struct platform_device *pdev) |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 78 | { |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 79 | struct device *twl6040_core_dev = pdev->dev.parent; |
| 80 | struct twl6040 *twl6040 = dev_get_drvdata(twl6040_core_dev); |
| 81 | int ret; |
| 82 | |
Peter Ujfalusi | a5d28d7 | 2013-07-12 13:30:43 +0200 | [diff] [blame] | 83 | twl6040gpo_chip.base = -1; |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 84 | |
| 85 | if (twl6040_get_revid(twl6040) < TWL6041_REV_ES2_0) |
| 86 | twl6040gpo_chip.ngpio = 3; /* twl6040 have 3 GPO */ |
| 87 | else |
| 88 | twl6040gpo_chip.ngpio = 1; /* twl6041 have 1 GPO */ |
| 89 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 90 | twl6040gpo_chip.parent = &pdev->dev; |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 91 | #ifdef CONFIG_OF_GPIO |
| 92 | twl6040gpo_chip.of_node = twl6040_core_dev->of_node; |
| 93 | #endif |
| 94 | |
Laxman Dewangan | fc0292a | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 95 | ret = devm_gpiochip_add_data(&pdev->dev, &twl6040gpo_chip, NULL); |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 96 | if (ret < 0) { |
| 97 | dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret); |
| 98 | twl6040gpo_chip.ngpio = 0; |
| 99 | } |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 104 | /* Note: this hardware lives inside an I2C-based multi-function device. */ |
| 105 | MODULE_ALIAS("platform:twl6040-gpo"); |
| 106 | |
| 107 | static struct platform_driver gpo_twl6040_driver = { |
| 108 | .driver = { |
| 109 | .name = "twl6040-gpo", |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 110 | }, |
| 111 | .probe = gpo_twl6040_probe, |
Peter Ujfalusi | 70ffd69 | 2012-08-16 15:13:15 +0300 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | module_platform_driver(gpo_twl6040_driver); |
| 115 | |
| 116 | MODULE_AUTHOR("Texas Instruments, Inc."); |
| 117 | MODULE_DESCRIPTION("GPO interface for TWL6040"); |
| 118 | MODULE_LICENSE("GPL"); |