gpio.c 9.8 KB

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