0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/module.h>
0010 #include <linux/platform_device.h>
0011 #include <linux/gpio/driver.h>
0012
0013 #include <linux/mfd/da9055/core.h>
0014 #include <linux/mfd/da9055/reg.h>
0015 #include <linux/mfd/da9055/pdata.h>
0016
0017 #define DA9055_VDD_IO 0x0
0018 #define DA9055_PUSH_PULL 0x3
0019 #define DA9055_ACT_LOW 0x0
0020 #define DA9055_GPI 0x1
0021 #define DA9055_PORT_MASK 0x3
0022 #define DA9055_PORT_SHIFT(offset) (4 * (offset % 2))
0023
0024 #define DA9055_INPUT DA9055_GPI
0025 #define DA9055_OUTPUT DA9055_PUSH_PULL
0026 #define DA9055_IRQ_GPI0 3
0027
0028 struct da9055_gpio {
0029 struct da9055 *da9055;
0030 struct gpio_chip gp;
0031 };
0032
0033 static int da9055_gpio_get(struct gpio_chip *gc, unsigned offset)
0034 {
0035 struct da9055_gpio *gpio = gpiochip_get_data(gc);
0036 int gpio_direction = 0;
0037 int ret;
0038
0039
0040 ret = da9055_reg_read(gpio->da9055, (offset >> 1) + DA9055_REG_GPIO0_1);
0041 if (ret < 0)
0042 return ret;
0043
0044 gpio_direction = ret & (DA9055_PORT_MASK) << DA9055_PORT_SHIFT(offset);
0045 gpio_direction >>= DA9055_PORT_SHIFT(offset);
0046 switch (gpio_direction) {
0047 case DA9055_INPUT:
0048 ret = da9055_reg_read(gpio->da9055, DA9055_REG_STATUS_B);
0049 if (ret < 0)
0050 return ret;
0051 break;
0052 case DA9055_OUTPUT:
0053 ret = da9055_reg_read(gpio->da9055, DA9055_REG_GPIO_MODE0_2);
0054 if (ret < 0)
0055 return ret;
0056 }
0057
0058 return ret & (1 << offset);
0059
0060 }
0061
0062 static void da9055_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
0063 {
0064 struct da9055_gpio *gpio = gpiochip_get_data(gc);
0065
0066 da9055_reg_update(gpio->da9055,
0067 DA9055_REG_GPIO_MODE0_2,
0068 1 << offset,
0069 value << offset);
0070 }
0071
0072 static int da9055_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
0073 {
0074 struct da9055_gpio *gpio = gpiochip_get_data(gc);
0075 unsigned char reg_byte;
0076
0077 reg_byte = (DA9055_ACT_LOW | DA9055_GPI)
0078 << DA9055_PORT_SHIFT(offset);
0079
0080 return da9055_reg_update(gpio->da9055, (offset >> 1) +
0081 DA9055_REG_GPIO0_1,
0082 DA9055_PORT_MASK <<
0083 DA9055_PORT_SHIFT(offset),
0084 reg_byte);
0085 }
0086
0087 static int da9055_gpio_direction_output(struct gpio_chip *gc,
0088 unsigned offset, int value)
0089 {
0090 struct da9055_gpio *gpio = gpiochip_get_data(gc);
0091 unsigned char reg_byte;
0092 int ret;
0093
0094 reg_byte = (DA9055_VDD_IO | DA9055_PUSH_PULL)
0095 << DA9055_PORT_SHIFT(offset);
0096
0097 ret = da9055_reg_update(gpio->da9055, (offset >> 1) +
0098 DA9055_REG_GPIO0_1,
0099 DA9055_PORT_MASK <<
0100 DA9055_PORT_SHIFT(offset),
0101 reg_byte);
0102 if (ret < 0)
0103 return ret;
0104
0105 da9055_gpio_set(gc, offset, value);
0106
0107 return 0;
0108 }
0109
0110 static int da9055_gpio_to_irq(struct gpio_chip *gc, u32 offset)
0111 {
0112 struct da9055_gpio *gpio = gpiochip_get_data(gc);
0113 struct da9055 *da9055 = gpio->da9055;
0114
0115 return regmap_irq_get_virq(da9055->irq_data,
0116 DA9055_IRQ_GPI0 + offset);
0117 }
0118
0119 static const struct gpio_chip reference_gp = {
0120 .label = "da9055-gpio",
0121 .owner = THIS_MODULE,
0122 .get = da9055_gpio_get,
0123 .set = da9055_gpio_set,
0124 .direction_input = da9055_gpio_direction_input,
0125 .direction_output = da9055_gpio_direction_output,
0126 .to_irq = da9055_gpio_to_irq,
0127 .can_sleep = true,
0128 .ngpio = 3,
0129 .base = -1,
0130 };
0131
0132 static int da9055_gpio_probe(struct platform_device *pdev)
0133 {
0134 struct da9055_gpio *gpio;
0135 struct da9055_pdata *pdata;
0136
0137 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
0138 if (!gpio)
0139 return -ENOMEM;
0140
0141 gpio->da9055 = dev_get_drvdata(pdev->dev.parent);
0142 pdata = dev_get_platdata(gpio->da9055->dev);
0143
0144 gpio->gp = reference_gp;
0145 if (pdata && pdata->gpio_base)
0146 gpio->gp.base = pdata->gpio_base;
0147
0148 return devm_gpiochip_add_data(&pdev->dev, &gpio->gp, gpio);
0149 }
0150
0151 static struct platform_driver da9055_gpio_driver = {
0152 .probe = da9055_gpio_probe,
0153 .driver = {
0154 .name = "da9055-gpio",
0155 },
0156 };
0157
0158 static int __init da9055_gpio_init(void)
0159 {
0160 return platform_driver_register(&da9055_gpio_driver);
0161 }
0162 subsys_initcall(da9055_gpio_init);
0163
0164 static void __exit da9055_gpio_exit(void)
0165 {
0166 platform_driver_unregister(&da9055_gpio_driver);
0167 }
0168 module_exit(da9055_gpio_exit);
0169
0170 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
0171 MODULE_DESCRIPTION("DA9055 GPIO Device Driver");
0172 MODULE_LICENSE("GPL");
0173 MODULE_ALIAS("platform:da9055-gpio");