2 * GPIO driver for Analog Devices ADP5520 MFD PMICs
4 * Copyright 2009 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/platform_device.h>
14 #include <linux/mfd/adp5520.h>
16 #include <linux/gpio.h>
19 struct device
*master
;
20 struct gpio_chip gpio_chip
;
21 unsigned char lut
[ADP5520_MAXGPIOS
];
25 static int adp5520_gpio_get_value(struct gpio_chip
*chip
, unsigned off
)
27 struct adp5520_gpio
*dev
;
30 dev
= gpiochip_get_data(chip
);
33 * There are dedicated registers for GPIO IN/OUT.
34 * Make sure we return the right value, even when configured as output
37 if (test_bit(off
, &dev
->output
))
38 adp5520_read(dev
->master
, ADP5520_GPIO_OUT
, ®_val
);
40 adp5520_read(dev
->master
, ADP5520_GPIO_IN
, ®_val
);
42 return !!(reg_val
& dev
->lut
[off
]);
45 static void adp5520_gpio_set_value(struct gpio_chip
*chip
,
46 unsigned off
, int val
)
48 struct adp5520_gpio
*dev
;
49 dev
= gpiochip_get_data(chip
);
52 adp5520_set_bits(dev
->master
, ADP5520_GPIO_OUT
, dev
->lut
[off
]);
54 adp5520_clr_bits(dev
->master
, ADP5520_GPIO_OUT
, dev
->lut
[off
]);
57 static int adp5520_gpio_direction_input(struct gpio_chip
*chip
, unsigned off
)
59 struct adp5520_gpio
*dev
;
60 dev
= gpiochip_get_data(chip
);
62 clear_bit(off
, &dev
->output
);
64 return adp5520_clr_bits(dev
->master
, ADP5520_GPIO_CFG_2
,
68 static int adp5520_gpio_direction_output(struct gpio_chip
*chip
,
69 unsigned off
, int val
)
71 struct adp5520_gpio
*dev
;
73 dev
= gpiochip_get_data(chip
);
75 set_bit(off
, &dev
->output
);
78 ret
|= adp5520_set_bits(dev
->master
, ADP5520_GPIO_OUT
,
81 ret
|= adp5520_clr_bits(dev
->master
, ADP5520_GPIO_OUT
,
84 ret
|= adp5520_set_bits(dev
->master
, ADP5520_GPIO_CFG_2
,
90 static int adp5520_gpio_probe(struct platform_device
*pdev
)
92 struct adp5520_gpio_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
93 struct adp5520_gpio
*dev
;
96 unsigned char ctl_mask
= 0;
99 dev_err(&pdev
->dev
, "missing platform data\n");
103 if (pdev
->id
!= ID_ADP5520
) {
104 dev_err(&pdev
->dev
, "only ADP5520 supports GPIO\n");
108 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
112 dev
->master
= pdev
->dev
.parent
;
114 for (gpios
= 0, i
= 0; i
< ADP5520_MAXGPIOS
; i
++)
115 if (pdata
->gpio_en_mask
& (1 << i
))
116 dev
->lut
[gpios
++] = 1 << i
;
123 gc
= &dev
->gpio_chip
;
124 gc
->direction_input
= adp5520_gpio_direction_input
;
125 gc
->direction_output
= adp5520_gpio_direction_output
;
126 gc
->get
= adp5520_gpio_get_value
;
127 gc
->set
= adp5520_gpio_set_value
;
128 gc
->can_sleep
= true;
130 gc
->base
= pdata
->gpio_start
;
132 gc
->label
= pdev
->name
;
133 gc
->owner
= THIS_MODULE
;
135 ret
= adp5520_clr_bits(dev
->master
, ADP5520_GPIO_CFG_1
,
136 pdata
->gpio_en_mask
);
138 if (pdata
->gpio_en_mask
& ADP5520_GPIO_C3
)
139 ctl_mask
|= ADP5520_C3_MODE
;
141 if (pdata
->gpio_en_mask
& ADP5520_GPIO_R3
)
142 ctl_mask
|= ADP5520_R3_MODE
;
145 ret
= adp5520_set_bits(dev
->master
, ADP5520_LED_CONTROL
,
148 ret
|= adp5520_set_bits(dev
->master
, ADP5520_GPIO_PULLUP
,
149 pdata
->gpio_pullup_mask
);
152 dev_err(&pdev
->dev
, "failed to write\n");
156 ret
= devm_gpiochip_add_data(&pdev
->dev
, &dev
->gpio_chip
, dev
);
160 platform_set_drvdata(pdev
, dev
);
167 static struct platform_driver adp5520_gpio_driver
= {
169 .name
= "adp5520-gpio",
171 .probe
= adp5520_gpio_probe
,
174 module_platform_driver(adp5520_gpio_driver
);
176 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
177 MODULE_DESCRIPTION("GPIO ADP5520 Driver");
178 MODULE_LICENSE("GPL");
179 MODULE_ALIAS("platform:adp5520-gpio");