gpio.c revision 6e54d8d252ed09ae148af6565971974af9a96e10
1/* 2 * SuperH Pin Function Controller GPIO driver. 3 * 4 * Copyright (C) 2008 Magnus Damm 5 * Copyright (C) 2009 - 2012 Paul Mundt 6 * 7 * This file is subject to the terms and conditions of the GNU General Public 8 * License. See the file "COPYING" in the main directory of this archive 9 * for more details. 10 */ 11 12#define pr_fmt(fmt) KBUILD_MODNAME " gpio: " fmt 13 14#include <linux/device.h> 15#include <linux/gpio.h> 16#include <linux/init.h> 17#include <linux/module.h> 18#include <linux/pinctrl/consumer.h> 19#include <linux/sh_pfc.h> 20#include <linux/slab.h> 21#include <linux/spinlock.h> 22 23#include "core.h" 24 25struct sh_pfc_chip { 26 struct sh_pfc *pfc; 27 struct gpio_chip gpio_chip; 28}; 29 30static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc) 31{ 32 return container_of(gc, struct sh_pfc_chip, gpio_chip); 33} 34 35static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc) 36{ 37 return gpio_to_pfc_chip(gc)->pfc; 38} 39 40static int sh_gpio_request(struct gpio_chip *gc, unsigned offset) 41{ 42 return pinctrl_request_gpio(offset); 43} 44 45static void sh_gpio_free(struct gpio_chip *gc, unsigned offset) 46{ 47 pinctrl_free_gpio(offset); 48} 49 50static void sh_gpio_set_value(struct sh_pfc *pfc, unsigned gpio, int value) 51{ 52 struct pinmux_data_reg *dr = NULL; 53 int bit = 0; 54 55 if (sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0) 56 BUG(); 57 else 58 sh_pfc_write_bit(dr, bit, value); 59} 60 61static int sh_gpio_get_value(struct sh_pfc *pfc, unsigned gpio) 62{ 63 struct pinmux_data_reg *dr = NULL; 64 int bit = 0; 65 66 if (sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0) 67 return -EINVAL; 68 69 return sh_pfc_read_bit(dr, bit); 70} 71 72static int sh_gpio_direction_input(struct gpio_chip *gc, unsigned offset) 73{ 74 return pinctrl_gpio_direction_input(offset); 75} 76 77static int sh_gpio_direction_output(struct gpio_chip *gc, unsigned offset, 78 int value) 79{ 80 sh_gpio_set_value(gpio_to_pfc(gc), offset, value); 81 82 return pinctrl_gpio_direction_output(offset); 83} 84 85static int sh_gpio_get(struct gpio_chip *gc, unsigned offset) 86{ 87 return sh_gpio_get_value(gpio_to_pfc(gc), offset); 88} 89 90static void sh_gpio_set(struct gpio_chip *gc, unsigned offset, int value) 91{ 92 sh_gpio_set_value(gpio_to_pfc(gc), offset, value); 93} 94 95static int sh_gpio_to_irq(struct gpio_chip *gc, unsigned offset) 96{ 97 struct sh_pfc *pfc = gpio_to_pfc(gc); 98 pinmux_enum_t enum_id; 99 pinmux_enum_t *enum_ids; 100 int i, k, pos; 101 102 pos = 0; 103 enum_id = 0; 104 while (1) { 105 pos = sh_pfc_gpio_to_enum(pfc, offset, pos, &enum_id); 106 if (pos <= 0 || !enum_id) 107 break; 108 109 for (i = 0; i < pfc->pdata->gpio_irq_size; i++) { 110 enum_ids = pfc->pdata->gpio_irq[i].enum_ids; 111 for (k = 0; enum_ids[k]; k++) { 112 if (enum_ids[k] == enum_id) 113 return pfc->pdata->gpio_irq[i].irq; 114 } 115 } 116 } 117 118 return -ENOSYS; 119} 120 121static void sh_pfc_gpio_setup(struct sh_pfc_chip *chip) 122{ 123 struct sh_pfc *pfc = chip->pfc; 124 struct gpio_chip *gc = &chip->gpio_chip; 125 126 gc->request = sh_gpio_request; 127 gc->free = sh_gpio_free; 128 gc->direction_input = sh_gpio_direction_input; 129 gc->get = sh_gpio_get; 130 gc->direction_output = sh_gpio_direction_output; 131 gc->set = sh_gpio_set; 132 gc->to_irq = sh_gpio_to_irq; 133 134 WARN_ON(pfc->pdata->first_gpio != 0); /* needs testing */ 135 136 gc->label = pfc->pdata->name; 137 gc->owner = THIS_MODULE; 138 gc->base = pfc->pdata->first_gpio; 139 gc->ngpio = (pfc->pdata->last_gpio - pfc->pdata->first_gpio) + 1; 140} 141 142int sh_pfc_register_gpiochip(struct sh_pfc *pfc) 143{ 144 struct sh_pfc_chip *chip; 145 int ret; 146 147 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL); 148 if (unlikely(!chip)) 149 return -ENOMEM; 150 151 chip->pfc = pfc; 152 153 sh_pfc_gpio_setup(chip); 154 155 ret = gpiochip_add(&chip->gpio_chip); 156 if (unlikely(ret < 0)) 157 return ret; 158 159 pfc->gpio = chip; 160 161 pr_info("%s handling gpio %d -> %d\n", 162 pfc->pdata->name, pfc->pdata->first_gpio, 163 pfc->pdata->last_gpio); 164 165 return 0; 166} 167 168int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc) 169{ 170 struct sh_pfc_chip *chip = pfc->gpio; 171 int ret; 172 173 ret = gpiochip_remove(&chip->gpio_chip); 174 if (unlikely(ret < 0)) 175 return ret; 176 177 pfc->gpio = NULL; 178 return 0; 179} 180