pfc.c 12 KB

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