gpio-exar.c (4076cf08ac7673aca7d4dd9ddf18045d08dbc292) gpio-exar.c (380b1e2f3a2f32bfe9c0aa85a68629eb99b043c0)
1/*
2 * GPIO driver for Exar XR17V35X chip
3 *
4 * Copyright (C) 2015 Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.

--- 17 unchanged lines hidden (view full) ---

26static DEFINE_IDA(ida_index);
27
28struct exar_gpio_chip {
29 struct gpio_chip gpio_chip;
30 struct mutex lock;
31 int index;
32 void __iomem *regs;
33 char name[20];
1/*
2 * GPIO driver for Exar XR17V35X chip
3 *
4 * Copyright (C) 2015 Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.

--- 17 unchanged lines hidden (view full) ---

26static DEFINE_IDA(ida_index);
27
28struct exar_gpio_chip {
29 struct gpio_chip gpio_chip;
30 struct mutex lock;
31 int index;
32 void __iomem *regs;
33 char name[20];
34 unsigned int first_pin;
34};
35
36static void exar_update(struct gpio_chip *chip, unsigned int reg, int val,
37 unsigned int offset)
38{
39 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
40 int temp;
41

--- 4 unchanged lines hidden (view full) ---

46 temp |= BIT(offset);
47 writeb(temp, exar_gpio->regs + reg);
48 mutex_unlock(&exar_gpio->lock);
49}
50
51static int exar_set_direction(struct gpio_chip *chip, int direction,
52 unsigned int offset)
53{
35};
36
37static void exar_update(struct gpio_chip *chip, unsigned int reg, int val,
38 unsigned int offset)
39{
40 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
41 int temp;
42

--- 4 unchanged lines hidden (view full) ---

47 temp |= BIT(offset);
48 writeb(temp, exar_gpio->regs + reg);
49 mutex_unlock(&exar_gpio->lock);
50}
51
52static int exar_set_direction(struct gpio_chip *chip, int direction,
53 unsigned int offset)
54{
54 unsigned int bank = offset / 8;
55 unsigned int addr;
55 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
56 unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
57 EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
58 unsigned int bit = (offset + exar_gpio->first_pin) % 8;
56
59
57 addr = bank ? EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
58 exar_update(chip, addr, direction, offset % 8);
60 exar_update(chip, addr, direction, bit);
59 return 0;
60}
61
62static int exar_get(struct gpio_chip *chip, unsigned int reg)
63{
64 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
65 int value;
66
67 mutex_lock(&exar_gpio->lock);
68 value = readb(exar_gpio->regs + reg);
69 mutex_unlock(&exar_gpio->lock);
70
71 return value;
72}
73
74static int exar_get_direction(struct gpio_chip *chip, unsigned int offset)
75{
61 return 0;
62}
63
64static int exar_get(struct gpio_chip *chip, unsigned int reg)
65{
66 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
67 int value;
68
69 mutex_lock(&exar_gpio->lock);
70 value = readb(exar_gpio->regs + reg);
71 mutex_unlock(&exar_gpio->lock);
72
73 return value;
74}
75
76static int exar_get_direction(struct gpio_chip *chip, unsigned int offset)
77{
76 unsigned int bank = offset / 8;
77 unsigned int addr;
78 int val;
78 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
79 unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
80 EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
81 unsigned int bit = (offset + exar_gpio->first_pin) % 8;
79
82
80 addr = bank ? EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
81 val = exar_get(chip, addr) & BIT(offset % 8);
82
83 return !!val;
83 return !!(exar_get(chip, addr) & BIT(bit));
84}
85
86static int exar_get_value(struct gpio_chip *chip, unsigned int offset)
87{
84}
85
86static int exar_get_value(struct gpio_chip *chip, unsigned int offset)
87{
88 unsigned int bank = offset / 8;
89 unsigned int addr;
90 int val;
88 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
89 unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
90 EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
91 unsigned int bit = (offset + exar_gpio->first_pin) % 8;
91
92
92 addr = bank ? EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
93 val = exar_get(chip, addr) & BIT(offset % 8);
94
95 return !!val;
93 return !!(exar_get(chip, addr) & BIT(bit));
96}
97
98static void exar_set_value(struct gpio_chip *chip, unsigned int offset,
99 int value)
100{
94}
95
96static void exar_set_value(struct gpio_chip *chip, unsigned int offset,
97 int value)
98{
101 unsigned int bank = offset / 8;
102 unsigned int addr;
99 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
100 unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
101 EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
102 unsigned int bit = (offset + exar_gpio->first_pin) % 8;
103
103
104 addr = bank ? EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
105 exar_update(chip, addr, value, offset % 8);
104 exar_update(chip, addr, value, bit);
106}
107
108static int exar_direction_output(struct gpio_chip *chip, unsigned int offset,
109 int value)
110{
111 exar_set_value(chip, offset, value);
112 return exar_set_direction(chip, 0, offset);
113}
114
115static int exar_direction_input(struct gpio_chip *chip, unsigned int offset)
116{
117 return exar_set_direction(chip, 1, offset);
118}
119
120static int gpio_exar_probe(struct platform_device *pdev)
121{
122 struct pci_dev *pcidev = to_pci_dev(pdev->dev.parent);
123 struct exar_gpio_chip *exar_gpio;
105}
106
107static int exar_direction_output(struct gpio_chip *chip, unsigned int offset,
108 int value)
109{
110 exar_set_value(chip, offset, value);
111 return exar_set_direction(chip, 0, offset);
112}
113
114static int exar_direction_input(struct gpio_chip *chip, unsigned int offset)
115{
116 return exar_set_direction(chip, 1, offset);
117}
118
119static int gpio_exar_probe(struct platform_device *pdev)
120{
121 struct pci_dev *pcidev = to_pci_dev(pdev->dev.parent);
122 struct exar_gpio_chip *exar_gpio;
123 u32 first_pin, ngpios;
124 void __iomem *p;
125 int index, ret;
126
127 /*
128 * The UART driver must have mapped region 0 prior to registering this
129 * device - use it.
130 */
131 p = pcim_iomap_table(pcidev)[0];
132 if (!p)
133 return -ENOMEM;
134
124 void __iomem *p;
125 int index, ret;
126
127 /*
128 * The UART driver must have mapped region 0 prior to registering this
129 * device - use it.
130 */
131 p = pcim_iomap_table(pcidev)[0];
132 if (!p)
133 return -ENOMEM;
134
135 ret = device_property_read_u32(&pdev->dev, "linux,first-pin",
136 &first_pin);
137 if (ret)
138 return ret;
139
140 ret = device_property_read_u32(&pdev->dev, "ngpios", &ngpios);
141 if (ret)
142 return ret;
143
135 exar_gpio = devm_kzalloc(&pdev->dev, sizeof(*exar_gpio), GFP_KERNEL);
136 if (!exar_gpio)
137 return -ENOMEM;
138
139 mutex_init(&exar_gpio->lock);
140
141 index = ida_simple_get(&ida_index, 0, 0, GFP_KERNEL);
142
143 sprintf(exar_gpio->name, "exar_gpio%d", index);
144 exar_gpio->gpio_chip.label = exar_gpio->name;
145 exar_gpio->gpio_chip.parent = &pdev->dev;
146 exar_gpio->gpio_chip.direction_output = exar_direction_output;
147 exar_gpio->gpio_chip.direction_input = exar_direction_input;
148 exar_gpio->gpio_chip.get_direction = exar_get_direction;
149 exar_gpio->gpio_chip.get = exar_get_value;
150 exar_gpio->gpio_chip.set = exar_set_value;
151 exar_gpio->gpio_chip.base = -1;
144 exar_gpio = devm_kzalloc(&pdev->dev, sizeof(*exar_gpio), GFP_KERNEL);
145 if (!exar_gpio)
146 return -ENOMEM;
147
148 mutex_init(&exar_gpio->lock);
149
150 index = ida_simple_get(&ida_index, 0, 0, GFP_KERNEL);
151
152 sprintf(exar_gpio->name, "exar_gpio%d", index);
153 exar_gpio->gpio_chip.label = exar_gpio->name;
154 exar_gpio->gpio_chip.parent = &pdev->dev;
155 exar_gpio->gpio_chip.direction_output = exar_direction_output;
156 exar_gpio->gpio_chip.direction_input = exar_direction_input;
157 exar_gpio->gpio_chip.get_direction = exar_get_direction;
158 exar_gpio->gpio_chip.get = exar_get_value;
159 exar_gpio->gpio_chip.set = exar_set_value;
160 exar_gpio->gpio_chip.base = -1;
152 exar_gpio->gpio_chip.ngpio = 16;
161 exar_gpio->gpio_chip.ngpio = ngpios;
153 exar_gpio->regs = p;
154 exar_gpio->index = index;
162 exar_gpio->regs = p;
163 exar_gpio->index = index;
164 exar_gpio->first_pin = first_pin;
155
156 ret = devm_gpiochip_add_data(&pdev->dev,
157 &exar_gpio->gpio_chip, exar_gpio);
158 if (ret)
159 goto err_destroy;
160
161 platform_set_drvdata(pdev, exar_gpio);
162

--- 32 unchanged lines hidden ---
165
166 ret = devm_gpiochip_add_data(&pdev->dev,
167 &exar_gpio->gpio_chip, exar_gpio);
168 if (ret)
169 goto err_destroy;
170
171 platform_set_drvdata(pdev, exar_gpio);
172

--- 32 unchanged lines hidden ---