Skip to content

Commit 3b26bf1

Browse files
Samuel OrtizSamuel Ortiz
authored andcommitted
mfd: New asic3 gpio configuration code
The ASIC3 GPIO configuration code is a bit obscure and hardly readable. This patch changes it so that it is now more readable and understandable, by being more explicit. Signed-off-by: Samuel Ortiz <sameo@openedhand.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 1effe5b commit 3b26bf1

2 files changed

Lines changed: 67 additions & 66 deletions

File tree

‎drivers/mfd/asic3.c‎

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -465,69 +465,54 @@ static void asic3_gpio_set(struct gpio_chip *chip,
465465
return;
466466
}
467467

468-
static inline u32 asic3_get_gpio(struct asic3 *asic, unsigned int base,
469-
unsigned int function)
468+
static int asic3_gpio_probe(struct platform_device *pdev,
469+
u16 *gpio_config, int num)
470470
{
471-
return asic3_read_register(asic, base + function);
472-
}
473-
474-
static void asic3_set_gpio(struct asic3 *asic, unsigned int base,
475-
unsigned int function, u32 bits, u32 val)
476-
{
477-
unsigned long flags;
478-
479-
spin_lock_irqsave(&asic->lock, flags);
480-
val |= (asic3_read_register(asic, base + function) & ~bits);
481-
482-
asic3_write_register(asic, base + function, val);
483-
spin_unlock_irqrestore(&asic->lock, flags);
484-
}
485-
486-
#define asic3_set_gpio_a(asic, fn, bits, val) \
487-
asic3_set_gpio(asic, ASIC3_GPIO_A_Base, ASIC3_GPIO_##fn, bits, val)
488-
#define asic3_set_gpio_b(asic, fn, bits, val) \
489-
asic3_set_gpio(asic, ASIC3_GPIO_B_Base, ASIC3_GPIO_##fn, bits, val)
490-
#define asic3_set_gpio_c(asic, fn, bits, val) \
491-
asic3_set_gpio(asic, ASIC3_GPIO_C_Base, ASIC3_GPIO_##fn, bits, val)
492-
#define asic3_set_gpio_d(asic, fn, bits, val) \
493-
asic3_set_gpio(asic, ASIC3_GPIO_D_Base, ASIC3_GPIO_##fn, bits, val)
494-
495-
#define asic3_set_gpio_banks(asic, fn, bits, pdata, field) \
496-
do { \
497-
asic3_set_gpio_a((asic), fn, (bits), (pdata)->gpio_a.field); \
498-
asic3_set_gpio_b((asic), fn, (bits), (pdata)->gpio_b.field); \
499-
asic3_set_gpio_c((asic), fn, (bits), (pdata)->gpio_c.field); \
500-
asic3_set_gpio_d((asic), fn, (bits), (pdata)->gpio_d.field); \
501-
} while (0)
502-
503-
504-
static int asic3_gpio_probe(struct platform_device *pdev)
505-
{
506-
struct asic3_platform_data *pdata = pdev->dev.platform_data;
507471
struct asic3 *asic = platform_get_drvdata(pdev);
472+
u16 alt_reg[ASIC3_NUM_GPIO_BANKS];
473+
u16 out_reg[ASIC3_NUM_GPIO_BANKS];
474+
u16 dir_reg[ASIC3_NUM_GPIO_BANKS];
475+
int i;
508476

477+
memset(alt_reg, 0, ASIC3_NUM_GPIO_BANKS);
478+
memset(out_reg, 0, ASIC3_NUM_GPIO_BANKS);
479+
memset(dir_reg, 0, ASIC3_NUM_GPIO_BANKS);
480+
481+
/* Enable all GPIOs */
509482
asic3_write_register(asic, ASIC3_GPIO_OFFSET(A, Mask), 0xffff);
510483
asic3_write_register(asic, ASIC3_GPIO_OFFSET(B, Mask), 0xffff);
511484
asic3_write_register(asic, ASIC3_GPIO_OFFSET(C, Mask), 0xffff);
512485
asic3_write_register(asic, ASIC3_GPIO_OFFSET(D, Mask), 0xffff);
513486

514-
asic3_set_gpio_a(asic, SleepMask, 0xffff, 0xffff);
515-
asic3_set_gpio_b(asic, SleepMask, 0xffff, 0xffff);
516-
asic3_set_gpio_c(asic, SleepMask, 0xffff, 0xffff);
517-
asic3_set_gpio_d(asic, SleepMask, 0xffff, 0xffff);
518-
519-
if (pdata) {
520-
asic3_set_gpio_banks(asic, Out, 0xffff, pdata, init);
521-
asic3_set_gpio_banks(asic, Direction, 0xffff, pdata, dir);
522-
asic3_set_gpio_banks(asic, SleepMask, 0xffff, pdata,
523-
sleep_mask);
524-
asic3_set_gpio_banks(asic, SleepOut, 0xffff, pdata, sleep_out);
525-
asic3_set_gpio_banks(asic, BattFaultOut, 0xffff, pdata,
526-
batt_fault_out);
527-
asic3_set_gpio_banks(asic, SleepConf, 0xffff, pdata,
528-
sleep_conf);
529-
asic3_set_gpio_banks(asic, AltFunction, 0xffff, pdata,
530-
alt_function);
487+
for (i = 0; i < num; i++) {
488+
u8 alt, pin, dir, init, bank_num, bit_num;
489+
u16 config = gpio_config[i];
490+
491+
pin = ASIC3_CONFIG_GPIO_PIN(config);
492+
alt = ASIC3_CONFIG_GPIO_ALT(config);
493+
dir = ASIC3_CONFIG_GPIO_DIR(config);
494+
init = ASIC3_CONFIG_GPIO_INIT(config);
495+
496+
bank_num = ASIC3_GPIO_TO_BANK(pin);
497+
bit_num = ASIC3_GPIO_TO_BIT(pin);
498+
499+
alt_reg[bank_num] |= (alt << bit_num);
500+
out_reg[bank_num] |= (init << bit_num);
501+
dir_reg[bank_num] |= (dir << bit_num);
502+
}
503+
504+
for (i = 0; i < ASIC3_NUM_GPIO_BANKS; i++) {
505+
asic3_write_register(asic,
506+
ASIC3_BANK_TO_BASE(i) +
507+
ASIC3_GPIO_Direction,
508+
dir_reg[i]);
509+
asic3_write_register(asic,
510+
ASIC3_BANK_TO_BASE(i) + ASIC3_GPIO_Out,
511+
out_reg[i]);
512+
asic3_write_register(asic,
513+
ASIC3_BANK_TO_BASE(i) +
514+
ASIC3_GPIO_AltFunction,
515+
alt_reg[i]);
531516
}
532517

533518
return gpiochip_add(&asic->gpio);
@@ -598,7 +583,9 @@ static int asic3_probe(struct platform_device *pdev)
598583
asic->gpio.direction_input = asic3_gpio_direction_input;
599584
asic->gpio.direction_output = asic3_gpio_direction_output;
600585

601-
ret = asic3_gpio_probe(pdev);
586+
ret = asic3_gpio_probe(pdev,
587+
pdata->gpio_config,
588+
pdata->gpio_config_num);
602589
if (ret < 0) {
603590
printk(KERN_ERR "GPIO probe failed\n");
604591
goto out_irq;

‎include/linux/mfd/asic3.h‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* published by the Free Software Foundation.
99
*
1010
* Copyright 2001 Compaq Computer Corporation.
11-
* Copyright 2007 OpendHand.
11+
* Copyright 2007-2008 OpenedHand Ltd.
1212
*/
1313

1414
#ifndef __ASIC3_H__
@@ -17,15 +17,8 @@
1717
#include <linux/types.h>
1818

1919
struct asic3_platform_data {
20-
struct {
21-
u32 dir;
22-
u32 init;
23-
u32 sleep_mask;
24-
u32 sleep_out;
25-
u32 batt_fault_out;
26-
u32 sleep_conf;
27-
u32 alt_function;
28-
} gpio_a, gpio_b, gpio_c, gpio_d;
20+
u16 *gpio_config;
21+
unsigned int gpio_config_num;
2922

3023
unsigned int bus_shift;
3124

@@ -86,6 +79,27 @@ struct asic3_platform_data {
8679
*/
8780
#define ASIC3_GPIO_Status 0x30 /* R Pin status */
8881

82+
/*
83+
* ASIC3 GPIO config
84+
*
85+
* Bits 0..6 gpio number
86+
* Bits 7..13 Alternate function
87+
* Bit 14 Direction
88+
* Bit 15 Initial value
89+
*
90+
*/
91+
#define ASIC3_CONFIG_GPIO_PIN(config) ((config) & 0x7f)
92+
#define ASIC3_CONFIG_GPIO_ALT(config) (((config) & (0x7f << 7)) >> 7)
93+
#define ASIC3_CONFIG_GPIO_DIR(config) ((config & (1 << 14)) >> 14)
94+
#define ASIC3_CONFIG_GPIO_INIT(config) ((config & (1 << 15)) >> 15)
95+
#define ASIC3_CONFIG_GPIO(gpio, alt, dir, init) (((gpio) & 0x7f) \
96+
| (((alt) & 0x7f) << 7) | (((dir) & 0x1) << 14) \
97+
| (((init) & 0x1) << 15))
98+
#define ASIC3_CONFIG_GPIO_DEFAULT(gpio, dir, init) \
99+
ASIC3_CONFIG_GPIO((gpio), 0, (dir), (init))
100+
#define ASIC3_CONFIG_GPIO_DEFAULT_OUT(gpio, init) \
101+
ASIC3_CONFIG_GPIO((gpio), 0, 1, (init))
102+
89103
#define ASIC3_SPI_Base 0x0400
90104
#define ASIC3_SPI_Control 0x0000
91105
#define ASIC3_SPI_TxData 0x0004

0 commit comments

Comments
 (0)