pfc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * Pinmuxed GPIO support for SuperH.
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/errno.h>
  12. #include <linux/kernel.h>
  13. #include <linux/list.h>
  14. #include <linux/module.h>
  15. #include <linux/clk.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <linux/irq.h>
  19. #include <linux/bitops.h>
  20. #include <linux/gpio.h>
  21. static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
  22. {
  23. if (enum_id < r->begin)
  24. return 0;
  25. if (enum_id > r->end)
  26. return 0;
  27. return 1;
  28. }
  29. static unsigned long gpio_read_raw_reg(unsigned long reg,
  30. unsigned long reg_width)
  31. {
  32. switch (reg_width) {
  33. case 8:
  34. return __raw_readb(reg);
  35. case 16:
  36. return __raw_readw(reg);
  37. case 32:
  38. return __raw_readl(reg);
  39. }
  40. BUG();
  41. return 0;
  42. }
  43. static void gpio_write_raw_reg(unsigned long reg,
  44. unsigned long reg_width,
  45. unsigned long data)
  46. {
  47. switch (reg_width) {
  48. case 8:
  49. __raw_writeb(data, reg);
  50. return;
  51. case 16:
  52. __raw_writew(data, reg);
  53. return;
  54. case 32:
  55. __raw_writel(data, reg);
  56. return;
  57. }
  58. BUG();
  59. }
  60. static void gpio_write_bit(struct pinmux_data_reg *dr,
  61. unsigned long in_pos, unsigned long value)
  62. {
  63. unsigned long pos;
  64. pos = dr->reg_width - (in_pos + 1);
  65. pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
  66. "r_width = %ld\n",
  67. dr->reg, !!value, pos, dr->reg_width);
  68. if (value)
  69. set_bit(pos, &dr->reg_shadow);
  70. else
  71. clear_bit(pos, &dr->reg_shadow);
  72. gpio_write_raw_reg(dr->reg, dr->reg_width, dr->reg_shadow);
  73. }
  74. static int gpio_read_reg(unsigned long reg, unsigned long reg_width,
  75. unsigned long field_width, unsigned long in_pos)
  76. {
  77. unsigned long data, mask, pos;
  78. data = 0;
  79. mask = (1 << field_width) - 1;
  80. pos = reg_width - ((in_pos + 1) * field_width);
  81. pr_debug("read_reg: addr = %lx, pos = %ld, "
  82. "r_width = %ld, f_width = %ld\n",
  83. reg, pos, reg_width, field_width);
  84. data = gpio_read_raw_reg(reg, reg_width);
  85. return (data >> pos) & mask;
  86. }
  87. static void gpio_write_reg(unsigned long reg, unsigned long reg_width,
  88. unsigned long field_width, unsigned long in_pos,
  89. unsigned long value)
  90. {
  91. unsigned long mask, pos;
  92. mask = (1 << field_width) - 1;
  93. pos = reg_width - ((in_pos + 1) * field_width);
  94. pr_debug("write_reg addr = %lx, value = %ld, pos = %ld, "
  95. "r_width = %ld, f_width = %ld\n",
  96. reg, value, pos, reg_width, field_width);
  97. mask = ~(mask << pos);
  98. value = value << pos;
  99. switch (reg_width) {
  100. case 8:
  101. __raw_writeb((__raw_readb(reg) & mask) | value, reg);
  102. break;
  103. case 16:
  104. __raw_writew((__raw_readw(reg) & mask) | value, reg);
  105. break;
  106. case 32:
  107. __raw_writel((__raw_readl(reg) & mask) | value, reg);
  108. break;
  109. }
  110. }
  111. static int setup_data_reg(struct pinmux_info *gpioc, unsigned gpio)
  112. {
  113. struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
  114. struct pinmux_data_reg *data_reg;
  115. int k, n;
  116. if (!enum_in_range(gpiop->enum_id, &gpioc->data))
  117. return -1;
  118. k = 0;
  119. while (1) {
  120. data_reg = gpioc->data_regs + k;
  121. if (!data_reg->reg_width)
  122. break;
  123. for (n = 0; n < data_reg->reg_width; n++) {
  124. if (data_reg->enum_ids[n] == gpiop->enum_id) {
  125. gpiop->flags &= ~PINMUX_FLAG_DREG;
  126. gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
  127. gpiop->flags &= ~PINMUX_FLAG_DBIT;
  128. gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
  129. return 0;
  130. }
  131. }
  132. k++;
  133. }
  134. BUG();
  135. return -1;
  136. }
  137. static void setup_data_regs(struct pinmux_info *gpioc)
  138. {
  139. struct pinmux_data_reg *drp;
  140. int k;
  141. for (k = gpioc->first_gpio; k <= gpioc->last_gpio; k++)
  142. setup_data_reg(gpioc, k);
  143. k = 0;
  144. while (1) {
  145. drp = gpioc->data_regs + k;
  146. if (!drp->reg_width)
  147. break;
  148. drp->reg_shadow = gpio_read_raw_reg(drp->reg, drp->reg_width);
  149. k++;
  150. }
  151. }
  152. static int get_data_reg(struct pinmux_info *gpioc, unsigned gpio,
  153. struct pinmux_data_reg **drp, int *bitp)
  154. {
  155. struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
  156. int k, n;
  157. if (!enum_in_range(gpiop->enum_id, &gpioc->data))
  158. return -1;
  159. k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
  160. n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
  161. *drp = gpioc->data_regs + k;
  162. *bitp = n;
  163. return 0;
  164. }
  165. static int get_config_reg(struct pinmux_info *gpioc, pinmux_enum_t enum_id,
  166. struct pinmux_cfg_reg **crp, int *indexp,
  167. unsigned long **cntp)
  168. {
  169. struct pinmux_cfg_reg *config_reg;
  170. unsigned long r_width, f_width;
  171. int k, n;
  172. k = 0;
  173. while (1) {
  174. config_reg = gpioc->cfg_regs + k;
  175. r_width = config_reg->reg_width;
  176. f_width = config_reg->field_width;
  177. if (!r_width)
  178. break;
  179. for (n = 0; n < (r_width / f_width) * 1 << f_width; n++) {
  180. if (config_reg->enum_ids[n] == enum_id) {
  181. *crp = config_reg;
  182. *indexp = n;
  183. *cntp = &config_reg->cnt[n / (1 << f_width)];
  184. return 0;
  185. }
  186. }
  187. k++;
  188. }
  189. return -1;
  190. }
  191. static int get_gpio_enum_id(struct pinmux_info *gpioc, unsigned gpio,
  192. int pos, pinmux_enum_t *enum_idp)
  193. {
  194. pinmux_enum_t enum_id = gpioc->gpios[gpio].enum_id;
  195. pinmux_enum_t *data = gpioc->gpio_data;
  196. int k;
  197. if (!enum_in_range(enum_id, &gpioc->data)) {
  198. if (!enum_in_range(enum_id, &gpioc->mark)) {
  199. pr_err("non data/mark enum_id for gpio %d\n", gpio);
  200. return -1;
  201. }
  202. }
  203. if (pos) {
  204. *enum_idp = data[pos + 1];
  205. return pos + 1;
  206. }
  207. for (k = 0; k < gpioc->gpio_data_size; k++) {
  208. if (data[k] == enum_id) {
  209. *enum_idp = data[k + 1];
  210. return k + 1;
  211. }
  212. }
  213. pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio);
  214. return -1;
  215. }
  216. static void write_config_reg(struct pinmux_info *gpioc,
  217. struct pinmux_cfg_reg *crp,
  218. int index)
  219. {
  220. unsigned long ncomb, pos, value;
  221. ncomb = 1 << crp->field_width;
  222. pos = index / ncomb;
  223. value = index % ncomb;
  224. gpio_write_reg(crp->reg, crp->reg_width, crp->field_width, pos, value);
  225. }
  226. static int check_config_reg(struct pinmux_info *gpioc,
  227. struct pinmux_cfg_reg *crp,
  228. int index)
  229. {
  230. unsigned long ncomb, pos, value;
  231. ncomb = 1 << crp->field_width;
  232. pos = index / ncomb;
  233. value = index % ncomb;
  234. if (gpio_read_reg(crp->reg, crp->reg_width,
  235. crp->field_width, pos) == value)
  236. return 0;
  237. return -1;
  238. }
  239. enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE };
  240. static int pinmux_config_gpio(struct pinmux_info *gpioc, unsigned gpio,
  241. int pinmux_type, int cfg_mode)
  242. {
  243. struct pinmux_cfg_reg *cr = NULL;
  244. pinmux_enum_t enum_id;
  245. struct pinmux_range *range;
  246. int in_range, pos, index;
  247. unsigned long *cntp;
  248. switch (pinmux_type) {
  249. case PINMUX_TYPE_FUNCTION:
  250. range = NULL;
  251. break;
  252. case PINMUX_TYPE_OUTPUT:
  253. range = &gpioc->output;
  254. break;
  255. case PINMUX_TYPE_INPUT:
  256. range = &gpioc->input;
  257. break;
  258. case PINMUX_TYPE_INPUT_PULLUP:
  259. range = &gpioc->input_pu;
  260. break;
  261. case PINMUX_TYPE_INPUT_PULLDOWN:
  262. range = &gpioc->input_pd;
  263. break;
  264. default:
  265. goto out_err;
  266. }
  267. pos = 0;
  268. enum_id = 0;
  269. index = 0;
  270. while (1) {
  271. pos = get_gpio_enum_id(gpioc, gpio, pos, &enum_id);
  272. if (pos <= 0)
  273. goto out_err;
  274. if (!enum_id)
  275. break;
  276. /* first check if this is a function enum */
  277. in_range = enum_in_range(enum_id, &gpioc->function);
  278. if (!in_range) {
  279. /* not a function enum */
  280. if (range) {
  281. /*
  282. * other range exists, so this pin is
  283. * a regular GPIO pin that now is being
  284. * bound to a specific direction.
  285. *
  286. * for this case we only allow function enums
  287. * and the enums that match the other range.
  288. */
  289. in_range = enum_in_range(enum_id, range);
  290. /*
  291. * special case pass through for fixed
  292. * input-only or output-only pins without
  293. * function enum register association.
  294. */
  295. if (in_range && enum_id == range->force)
  296. continue;
  297. } else {
  298. /*
  299. * no other range exists, so this pin
  300. * must then be of the function type.
  301. *
  302. * allow function type pins to select
  303. * any combination of function/in/out
  304. * in their MARK lists.
  305. */
  306. in_range = 1;
  307. }
  308. }
  309. if (!in_range)
  310. continue;
  311. if (get_config_reg(gpioc, enum_id, &cr, &index, &cntp) != 0)
  312. goto out_err;
  313. switch (cfg_mode) {
  314. case GPIO_CFG_DRYRUN:
  315. if (!*cntp || !check_config_reg(gpioc, cr, index))
  316. continue;
  317. break;
  318. case GPIO_CFG_REQ:
  319. write_config_reg(gpioc, cr, index);
  320. *cntp = *cntp + 1;
  321. break;
  322. case GPIO_CFG_FREE:
  323. *cntp = *cntp - 1;
  324. break;
  325. }
  326. }
  327. return 0;
  328. out_err:
  329. return -1;
  330. }
  331. static DEFINE_SPINLOCK(gpio_lock);
  332. static struct pinmux_info *chip_to_pinmux(struct gpio_chip *chip)
  333. {
  334. return container_of(chip, struct pinmux_info, chip);
  335. }
  336. static int sh_gpio_request(struct gpio_chip *chip, unsigned offset)
  337. {
  338. struct pinmux_info *gpioc = chip_to_pinmux(chip);
  339. struct pinmux_data_reg *dummy;
  340. unsigned long flags;
  341. int i, ret, pinmux_type;
  342. ret = -EINVAL;
  343. if (!gpioc)
  344. goto err_out;
  345. spin_lock_irqsave(&gpio_lock, flags);
  346. if ((gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE)
  347. goto err_unlock;
  348. /* setup pin function here if no data is associated with pin */
  349. if (get_data_reg(gpioc, offset, &dummy, &i) != 0)
  350. pinmux_type = PINMUX_TYPE_FUNCTION;
  351. else
  352. pinmux_type = PINMUX_TYPE_GPIO;
  353. if (pinmux_type == PINMUX_TYPE_FUNCTION) {
  354. if (pinmux_config_gpio(gpioc, offset,
  355. pinmux_type,
  356. GPIO_CFG_DRYRUN) != 0)
  357. goto err_unlock;
  358. if (pinmux_config_gpio(gpioc, offset,
  359. pinmux_type,
  360. GPIO_CFG_REQ) != 0)
  361. BUG();
  362. }
  363. gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
  364. gpioc->gpios[offset].flags |= pinmux_type;
  365. ret = 0;
  366. err_unlock:
  367. spin_unlock_irqrestore(&gpio_lock, flags);
  368. err_out:
  369. return ret;
  370. }
  371. static void sh_gpio_free(struct gpio_chip *chip, unsigned offset)
  372. {
  373. struct pinmux_info *gpioc = chip_to_pinmux(chip);
  374. unsigned long flags;
  375. int pinmux_type;
  376. if (!gpioc)
  377. return;
  378. spin_lock_irqsave(&gpio_lock, flags);
  379. pinmux_type = gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE;
  380. pinmux_config_gpio(gpioc, offset, pinmux_type, GPIO_CFG_FREE);
  381. gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
  382. gpioc->gpios[offset].flags |= PINMUX_TYPE_NONE;
  383. spin_unlock_irqrestore(&gpio_lock, flags);
  384. }
  385. static int pinmux_direction(struct pinmux_info *gpioc,
  386. unsigned gpio, int new_pinmux_type)
  387. {
  388. int pinmux_type;
  389. int ret = -EINVAL;
  390. if (!gpioc)
  391. goto err_out;
  392. pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
  393. switch (pinmux_type) {
  394. case PINMUX_TYPE_GPIO:
  395. break;
  396. case PINMUX_TYPE_OUTPUT:
  397. case PINMUX_TYPE_INPUT:
  398. case PINMUX_TYPE_INPUT_PULLUP:
  399. case PINMUX_TYPE_INPUT_PULLDOWN:
  400. pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE);
  401. break;
  402. default:
  403. goto err_out;
  404. }
  405. if (pinmux_config_gpio(gpioc, gpio,
  406. new_pinmux_type,
  407. GPIO_CFG_DRYRUN) != 0)
  408. goto err_out;
  409. if (pinmux_config_gpio(gpioc, gpio,
  410. new_pinmux_type,
  411. GPIO_CFG_REQ) != 0)
  412. BUG();
  413. gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
  414. gpioc->gpios[gpio].flags |= new_pinmux_type;
  415. ret = 0;
  416. err_out:
  417. return ret;
  418. }
  419. static int sh_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  420. {
  421. struct pinmux_info *gpioc = chip_to_pinmux(chip);
  422. unsigned long flags;
  423. int ret;
  424. spin_lock_irqsave(&gpio_lock, flags);
  425. ret = pinmux_direction(gpioc, offset, PINMUX_TYPE_INPUT);
  426. spin_unlock_irqrestore(&gpio_lock, flags);
  427. return ret;
  428. }
  429. static void sh_gpio_set_value(struct pinmux_info *gpioc,
  430. unsigned gpio, int value)
  431. {
  432. struct pinmux_data_reg *dr = NULL;
  433. int bit = 0;
  434. if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
  435. BUG();
  436. else
  437. gpio_write_bit(dr, bit, value);
  438. }
  439. static int sh_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  440. int value)
  441. {
  442. struct pinmux_info *gpioc = chip_to_pinmux(chip);
  443. unsigned long flags;
  444. int ret;
  445. sh_gpio_set_value(gpioc, offset, value);
  446. spin_lock_irqsave(&gpio_lock, flags);
  447. ret = pinmux_direction(gpioc, offset, PINMUX_TYPE_OUTPUT);
  448. spin_unlock_irqrestore(&gpio_lock, flags);
  449. return ret;
  450. }
  451. static int sh_gpio_get_value(struct pinmux_info *gpioc, unsigned gpio)
  452. {
  453. struct pinmux_data_reg *dr = NULL;
  454. int bit = 0;
  455. if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
  456. return -EINVAL;
  457. return gpio_read_reg(dr->reg, dr->reg_width, 1, bit);
  458. }
  459. static int sh_gpio_get(struct gpio_chip *chip, unsigned offset)
  460. {
  461. return sh_gpio_get_value(chip_to_pinmux(chip), offset);
  462. }
  463. static void sh_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  464. {
  465. sh_gpio_set_value(chip_to_pinmux(chip), offset, value);
  466. }
  467. int register_pinmux(struct pinmux_info *pip)
  468. {
  469. struct gpio_chip *chip = &pip->chip;
  470. pr_info("%s handling gpio %d -> %d\n",
  471. pip->name, pip->first_gpio, pip->last_gpio);
  472. setup_data_regs(pip);
  473. chip->request = sh_gpio_request;
  474. chip->free = sh_gpio_free;
  475. chip->direction_input = sh_gpio_direction_input;
  476. chip->get = sh_gpio_get;
  477. chip->direction_output = sh_gpio_direction_output;
  478. chip->set = sh_gpio_set;
  479. WARN_ON(pip->first_gpio != 0); /* needs testing */
  480. chip->label = pip->name;
  481. chip->owner = THIS_MODULE;
  482. chip->base = pip->first_gpio;
  483. chip->ngpio = (pip->last_gpio - pip->first_gpio) + 1;
  484. return gpiochip_add(chip);
  485. }
  486. int unregister_pinmux(struct pinmux_info *pip)
  487. {
  488. pr_info("%s deregistering\n", pip->name);
  489. return gpiochip_remove(&pip->chip);
  490. }