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>
15 #include <linux/gpio/driver.h>
18 struct device
*master
;
19 struct gpio_chip gpio_chip
;
20 unsigned char lut
[ADP5520_MAXGPIOS
];
24 static int adp5520_gpio_get_value(struct gpio_chip
*chip
, unsigned off
)
26 struct adp5520_gpio
*dev
;
29 dev
= gpiochip_get_data(chip
);
32 * There are dedicated registers for GPIO IN/OUT.
33 * Make sure we return the right value, even when configured as output
36 if (test_bit(off
, &dev
->output
))
37 adp5520_read(dev
->master
, ADP5520_GPIO_OUT
, ®_val
);
39 adp5520_read(dev
->master
, ADP5520_GPIO_IN
, ®_val
);
41 return !!(reg_val
& dev
->lut
[off
]);
44 static void adp5520_gpio_set_value(struct gpio_chip
*chip
,
45 unsigned off
, int val
)
47 struct adp5520_gpio
*dev
;
48 dev
= gpiochip_get_data(chip
);
51 adp5520_set_bits(dev
->master
, ADP5520_GPIO_OUT
, dev
->lut
[off
]);
53 adp5520_clr_bits(dev
->master
, ADP5520_GPIO_OUT
, dev
->lut
[off
]);
56 static int adp5520_gpio_direction_input(struct gpio_chip
*chip
, unsigned off
)
58 struct adp5520_gpio
*dev
;
59 dev
= gpiochip_get_data(chip
);
61 clear_bit(off
, &dev
->output
);
63 return adp5520_clr_bits(dev
->master
, ADP5520_GPIO_CFG_2
,
67 static int adp5520_gpio_direction_output(struct gpio_chip
*chip
,
68 unsigned off
, int val
)
70 struct adp5520_gpio
*dev
;
72 dev
= gpiochip_get_data(chip
);
74 set_bit(off
, &dev
->output
);
77 ret
|= adp5520_set_bits(dev
->master
, ADP5520_GPIO_OUT
,
80 ret
|= adp5520_clr_bits(dev
->master
, ADP5520_GPIO_OUT
,
83 ret
|= adp5520_set_bits(dev
->master
, ADP5520_GPIO_CFG_2
,
89 static int adp5520_gpio_probe(struct platform_device
*pdev
)
91 struct adp5520_gpio_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
92 struct adp5520_gpio
*dev
;
95 unsigned char ctl_mask
= 0;
98 dev_err(&pdev
->dev
, "missing platform data\n");
102 if (pdev
->id
!= ID_ADP5520
) {
103 dev_err(&pdev
->dev
, "only ADP5520 supports GPIO\n");
107 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
111 dev
->master
= pdev
->dev
.parent
;
113 for (gpios
= 0, i
= 0; i
< ADP5520_MAXGPIOS
; i
++)
114 if (pdata
->gpio_en_mask
& (1 << i
))
115 dev
->lut
[gpios
++] = 1 << i
;
122 gc
= &dev
->gpio_chip
;
123 gc
->direction_input
= adp5520_gpio_direction_input
;
124 gc
->direction_output
= adp5520_gpio_direction_output
;
125 gc
->get
= adp5520_gpio_get_value
;
126 gc
->set
= adp5520_gpio_set_value
;
127 gc
->can_sleep
= true;
129 gc
->base
= pdata
->gpio_start
;
131 gc
->label
= pdev
->name
;
132 gc
->owner
= THIS_MODULE
;
134 ret
= adp5520_clr_bits(dev
->master
, ADP5520_GPIO_CFG_1
,
135 pdata
->gpio_en_mask
);
137 if (pdata
->gpio_en_mask
& ADP5520_GPIO_C3
)
138 ctl_mask
|= ADP5520_C3_MODE
;
140 if (pdata
->gpio_en_mask
& ADP5520_GPIO_R3
)
141 ctl_mask
|= ADP5520_R3_MODE
;
144 ret
= adp5520_set_bits(dev
->master
, ADP5520_LED_CONTROL
,
147 ret
|= adp5520_set_bits(dev
->master
, ADP5520_GPIO_PULLUP
,
148 pdata
->gpio_pullup_mask
);
151 dev_err(&pdev
->dev
, "failed to write\n");
155 ret
= devm_gpiochip_add_data(&pdev
->dev
, &dev
->gpio_chip
, dev
);
159 platform_set_drvdata(pdev
, dev
);
166 static struct platform_driver adp5520_gpio_driver
= {
168 .name
= "adp5520-gpio",
170 .probe
= adp5520_gpio_probe
,
173 module_platform_driver(adp5520_gpio_driver
);
175 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
176 MODULE_DESCRIPTION("GPIO ADP5520 Driver");
177 MODULE_LICENSE("GPL");
178 MODULE_ALIAS("platform:adp5520-gpio");