11 #define pr_fmt(fmt) "sh_pfc " KBUILD_MODNAME ": " fmt
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
16 #include <linux/module.h>
19 #include <linux/bitops.h>
20 #include <linux/slab.h>
26 static inline bool sh_pfc_initialized(
void)
31 static void pfc_iounmap(
struct sh_pfc *pfc)
43 static int pfc_ioremap(
struct sh_pfc *pfc)
60 pfc->
window[
k].size = resource_size(res);
88 if (address >= (window->
phys + window->
size))
91 return window->
virt + (address - window->
phys);
100 if (enum_id < r->begin)
103 if (enum_id > r->
end)
109 static unsigned long gpio_read_raw_reg(
void __iomem *mapped_reg,
110 unsigned long reg_width)
125 static void gpio_write_raw_reg(
void __iomem *mapped_reg,
126 unsigned long reg_width,
150 pr_debug(
"read_bit: addr = %lx, pos = %ld, "
164 pr_debug(
"write_bit addr = %lx, value = %d, pos = %ld, "
177 static void config_reg_helper(
struct sh_pfc *pfc,
179 unsigned long in_pos,
181 unsigned long *maskp,
186 *mapped_regp = pfc_phys_to_virt(pfc, crp->
reg);
194 for (k = 0; k <= in_pos; k++)
199 static int read_config_reg(
struct sh_pfc *pfc,
206 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
208 pr_debug(
"read_reg: addr = %lx, field = %ld, "
209 "r_width = %ld, f_width = %ld\n",
212 return (gpio_read_raw_reg(mapped_reg, crp->
reg_width) >> pos) &
mask;
215 static void write_config_reg(
struct sh_pfc *pfc,
217 unsigned long field,
unsigned long value)
222 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
224 pr_debug(
"write_reg addr = %lx, value = %ld, field = %ld, "
225 "r_width = %ld, f_width = %ld\n",
228 mask = ~(mask <<
pos);
229 value = value <<
pos;
231 data = gpio_read_raw_reg(mapped_reg, crp->
reg_width);
236 gpio_write_raw_reg(pfc_phys_to_virt(pfc, pfc->
unlock_reg),
239 gpio_write_raw_reg(mapped_reg, crp->
reg_width, data);
242 static int setup_data_reg(
struct sh_pfc *pfc,
unsigned gpio)
260 for (n = 0; n < data_reg->
reg_width; n++) {
277 static void setup_data_regs(
struct sh_pfc *pfc)
282 for (k = pfc->
first_gpio; k <= pfc->last_gpio; k++)
283 setup_data_reg(pfc, k);
317 int *fieldp,
int *valuep,
318 unsigned long **cntp)
321 unsigned long r_width, f_width, curr_width, ncomb;
322 int k,
m,
n,
pos, bit_pos;
336 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
338 curr_width = f_width;
342 ncomb = 1 << curr_width;
343 for (n = 0; n < ncomb; n++) {
344 if (config_reg->
enum_ids[pos + n] == enum_id) {
348 *cntp = &config_reg->
cnt[
m];
368 if (!enum_in_range(enum_id, &pfc->
data)) {
369 if (!enum_in_range(enum_id, &pfc->
mark)) {
370 pr_err(
"non data/mark enum_id for gpio %d\n", gpio);
376 *enum_idp = data[pos + 1];
381 if (data[k] == enum_id) {
382 *enum_idp = data[k + 1];
387 pr_err(
"cannot locate data/mark enum_id for gpio %d\n", gpio);
401 switch (pinmux_type) {
440 in_range = enum_in_range(enum_id, &pfc->
function);
452 in_range = enum_in_range(enum_id, range);
459 if (in_range && enum_id == range->
force)
477 if (get_config_reg(pfc, enum_id, &cr,
478 &field, &value, &cntp) != 0)
484 (read_config_reg(pfc, cr, field) != value))
489 write_config_reg(pfc, cr, field, value);
518 ret = pfc_ioremap(pfc);
525 setup_data_regs(pfc);
534 ret = (*initroutine)(pfc);
540 pr_err(
"failed to initialize pinctrl bindings\n");
549 ret = (*initroutine)(pfc);
558 pr_notice(
"failed to init GPIO chip, ignoring...\n");