gpio.c 11 KB

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