gpio-mmio.c (664b0bae0b87f69bc9deb098f5e0158b9cf18e04) gpio-mmio.c (d799a4de0a250f1bdd99765bb8e55a5e2f469a1f)
1/*
2 * Generic driver for memory-mapped GPIO controllers.
3 *
4 * Copyright 2008 MontaVista Software, Inc.
5 * Copyright 2008,2010 Anton Vorontsov <cbouatmailru@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the

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

131 if (gc->be_bits)
132 return BIT(gc->bgpio_bits - 1 - line);
133 return BIT(line);
134}
135
136static int bgpio_get_set(struct gpio_chip *gc, unsigned int gpio)
137{
138 unsigned long pinmask = bgpio_line2mask(gc, gpio);
1/*
2 * Generic driver for memory-mapped GPIO controllers.
3 *
4 * Copyright 2008 MontaVista Software, Inc.
5 * Copyright 2008,2010 Anton Vorontsov <cbouatmailru@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the

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

131 if (gc->be_bits)
132 return BIT(gc->bgpio_bits - 1 - line);
133 return BIT(line);
134}
135
136static int bgpio_get_set(struct gpio_chip *gc, unsigned int gpio)
137{
138 unsigned long pinmask = bgpio_line2mask(gc, gpio);
139 bool dir = !!(gc->bgpio_dir & pinmask);
139
140
140 if (gc->bgpio_dir & pinmask)
141 /*
142 * If the direction is OUT we read the value from the SET
143 * register, and if the direction is IN we read the value
144 * from the DAT register.
145 *
146 * If the direction bits are inverted, naturally this gets
147 * inverted too.
148 */
149 if (gc->bgpio_dir_inverted)
150 dir = !dir;
151
152 if (dir)
141 return !!(gc->read_reg(gc->reg_set) & pinmask);
142 else
143 return !!(gc->read_reg(gc->reg_dat) & pinmask);
144}
145
146/*
147 * This assumes that the bits in the GPIO register are in native endianness.
148 * We only assign the function pointer if we have that.
149 */
150static int bgpio_get_set_multiple(struct gpio_chip *gc, unsigned long *mask,
151 unsigned long *bits)
152{
153 unsigned long get_mask = 0;
154 unsigned long set_mask = 0;
155
156 /* Make sure we first clear any bits that are zero when we read the register */
157 *bits &= ~*mask;
158
159 /* Exploit the fact that we know which directions are set */
153 return !!(gc->read_reg(gc->reg_set) & pinmask);
154 else
155 return !!(gc->read_reg(gc->reg_dat) & pinmask);
156}
157
158/*
159 * This assumes that the bits in the GPIO register are in native endianness.
160 * We only assign the function pointer if we have that.
161 */
162static int bgpio_get_set_multiple(struct gpio_chip *gc, unsigned long *mask,
163 unsigned long *bits)
164{
165 unsigned long get_mask = 0;
166 unsigned long set_mask = 0;
167
168 /* Make sure we first clear any bits that are zero when we read the register */
169 *bits &= ~*mask;
170
171 /* Exploit the fact that we know which directions are set */
160 set_mask = *mask & gc->bgpio_dir;
161 get_mask = *mask & ~gc->bgpio_dir;
172 if (gc->bgpio_dir_inverted) {
173 set_mask = *mask & ~gc->bgpio_dir;
174 get_mask = *mask & gc->bgpio_dir;
175 } else {
176 set_mask = *mask & gc->bgpio_dir;
177 get_mask = *mask & ~gc->bgpio_dir;
178 }
162
163 if (set_mask)
164 *bits |= gc->read_reg(gc->reg_set) & set_mask;
165 if (get_mask)
166 *bits |= gc->read_reg(gc->reg_dat) & get_mask;
167
168 return 0;
169}

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

354}
355
356static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
357{
358 unsigned long flags;
359
360 spin_lock_irqsave(&gc->bgpio_lock, flags);
361
179
180 if (set_mask)
181 *bits |= gc->read_reg(gc->reg_set) & set_mask;
182 if (get_mask)
183 *bits |= gc->read_reg(gc->reg_dat) & get_mask;
184
185 return 0;
186}

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

371}
372
373static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
374{
375 unsigned long flags;
376
377 spin_lock_irqsave(&gc->bgpio_lock, flags);
378
362 gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio);
379 if (gc->bgpio_dir_inverted)
380 gc->bgpio_dir |= bgpio_line2mask(gc, gpio);
381 else
382 gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio);
363 gc->write_reg(gc->reg_dir, gc->bgpio_dir);
364
365 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
366
367 return 0;
368}
369
370static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio)
371{
372 /* Return 0 if output, 1 of input */
383 gc->write_reg(gc->reg_dir, gc->bgpio_dir);
384
385 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
386
387 return 0;
388}
389
390static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio)
391{
392 /* Return 0 if output, 1 of input */
373 return !(gc->read_reg(gc->reg_dir) & bgpio_line2mask(gc, gpio));
393 if (gc->bgpio_dir_inverted)
394 return !!(gc->read_reg(gc->reg_dir) & bgpio_line2mask(gc, gpio));
395 else
396 return !(gc->read_reg(gc->reg_dir) & bgpio_line2mask(gc, gpio));
374}
375
376static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
377{
378 unsigned long flags;
379
380 gc->set(gc, gpio, val);
381
382 spin_lock_irqsave(&gc->bgpio_lock, flags);
383
397}
398
399static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
400{
401 unsigned long flags;
402
403 gc->set(gc, gpio, val);
404
405 spin_lock_irqsave(&gc->bgpio_lock, flags);
406
384 gc->bgpio_dir |= bgpio_line2mask(gc, gpio);
407 if (gc->bgpio_dir_inverted)
408 gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio);
409 else
410 gc->bgpio_dir |= bgpio_line2mask(gc, gpio);
385 gc->write_reg(gc->reg_dir, gc->bgpio_dir);
386
387 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
388
389 return 0;
390}
391
411 gc->write_reg(gc->reg_dir, gc->bgpio_dir);
412
413 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
414
415 return 0;
416}
417
392static int bgpio_dir_in_inv(struct gpio_chip *gc, unsigned int gpio)
393{
394 unsigned long flags;
395
396 spin_lock_irqsave(&gc->bgpio_lock, flags);
397
398 gc->bgpio_dir |= bgpio_line2mask(gc, gpio);
399 gc->write_reg(gc->reg_dir, gc->bgpio_dir);
400
401 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
402
403 return 0;
404}
405
406static int bgpio_dir_out_inv(struct gpio_chip *gc, unsigned int gpio, int val)
407{
408 unsigned long flags;
409
410 gc->set(gc, gpio, val);
411
412 spin_lock_irqsave(&gc->bgpio_lock, flags);
413
414 gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio);
415 gc->write_reg(gc->reg_dir, gc->bgpio_dir);
416
417 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
418
419 return 0;
420}
421
422static int bgpio_get_dir_inv(struct gpio_chip *gc, unsigned int gpio)
423{
424 /* Return 0 if output, 1 if input */
425 return !!(gc->read_reg(gc->reg_dir) & bgpio_line2mask(gc, gpio));
426}
427
428static int bgpio_setup_accessors(struct device *dev,
429 struct gpio_chip *gc,
430 bool byte_be)
431{
432
433 switch (gc->bgpio_bits) {
434 case 8:
435 gc->read_reg = bgpio_read8;

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

555 return -EINVAL;
556 } else if (dirout) {
557 gc->reg_dir = dirout;
558 gc->direction_output = bgpio_dir_out;
559 gc->direction_input = bgpio_dir_in;
560 gc->get_direction = bgpio_get_dir;
561 } else if (dirin) {
562 gc->reg_dir = dirin;
418static int bgpio_setup_accessors(struct device *dev,
419 struct gpio_chip *gc,
420 bool byte_be)
421{
422
423 switch (gc->bgpio_bits) {
424 case 8:
425 gc->read_reg = bgpio_read8;

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

545 return -EINVAL;
546 } else if (dirout) {
547 gc->reg_dir = dirout;
548 gc->direction_output = bgpio_dir_out;
549 gc->direction_input = bgpio_dir_in;
550 gc->get_direction = bgpio_get_dir;
551 } else if (dirin) {
552 gc->reg_dir = dirin;
563 gc->direction_output = bgpio_dir_out_inv;
564 gc->direction_input = bgpio_dir_in_inv;
565 gc->get_direction = bgpio_get_dir_inv;
553 gc->direction_output = bgpio_dir_out;
554 gc->direction_input = bgpio_dir_in;
555 gc->get_direction = bgpio_get_dir;
556 gc->bgpio_dir_inverted = true;
566 } else {
567 if (flags & BGPIOF_NO_OUTPUT)
568 gc->direction_output = bgpio_dir_out_err;
569 else
570 gc->direction_output = bgpio_simple_dir_out;
571 gc->direction_input = bgpio_simple_dir_in;
572 }
573
574 return 0;
575}
576
577static int bgpio_request(struct gpio_chip *chip, unsigned gpio_pin)
578{
579 if (gpio_pin < chip->ngpio)
580 return 0;
581
582 return -EINVAL;
583}
584
557 } else {
558 if (flags & BGPIOF_NO_OUTPUT)
559 gc->direction_output = bgpio_dir_out_err;
560 else
561 gc->direction_output = bgpio_simple_dir_out;
562 gc->direction_input = bgpio_simple_dir_in;
563 }
564
565 return 0;
566}
567
568static int bgpio_request(struct gpio_chip *chip, unsigned gpio_pin)
569{
570 if (gpio_pin < chip->ngpio)
571 return 0;
572
573 return -EINVAL;
574}
575
576/**
577 * bgpio_init() - Initialize generic GPIO accessor functions
578 * @gc: the GPIO chip to set up
579 * @dev: the parent device of the new GPIO chip (compulsory)
580 * @sz: the size (width) of the MMIO registers in bytes, typically 1, 2 or 4
581 * @dat: MMIO address for the register to READ the value of the GPIO lines, it
582 * is expected that a 1 in the corresponding bit in this register means the
583 * line is asserted
584 * @set: MMIO address for the register to SET the value of the GPIO lines, it is
585 * expected that we write the line with 1 in this register to drive the GPIO line
586 * high.
587 * @clr: MMIO address for the register to CLEAR the value of the GPIO lines, it is
588 * expected that we write the line with 1 in this register to drive the GPIO line
589 * low. It is allowed to leave this address as NULL, in that case the SET register
590 * will be assumed to also clear the GPIO lines, by actively writing the line
591 * with 0.
592 * @dirout: MMIO address for the register to set the line as OUTPUT. It is assumed
593 * that setting a line to 1 in this register will turn that line into an
594 * output line. Conversely, setting the line to 0 will turn that line into
595 * an input. Either this or @dirin can be defined, but never both.
596 * @dirin: MMIO address for the register to set this line as INPUT. It is assumed
597 * that setting a line to 1 in this register will turn that line into an
598 * input line. Conversely, setting the line to 0 will turn that line into
599 * an output. Either this or @dirout can be defined, but never both.
600 * @flags: Different flags that will affect the behaviour of the device, such as
601 * endianness etc.
602 */
585int bgpio_init(struct gpio_chip *gc, struct device *dev,
586 unsigned long sz, void __iomem *dat, void __iomem *set,
587 void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
588 unsigned long flags)
589{
590 int ret;
591
592 if (!is_power_of_2(sz))

--- 196 unchanged lines hidden ---
603int bgpio_init(struct gpio_chip *gc, struct device *dev,
604 unsigned long sz, void __iomem *dat, void __iomem *set,
605 void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
606 unsigned long flags)
607{
608 int ret;
609
610 if (!is_power_of_2(sz))

--- 196 unchanged lines hidden ---