gpio.c 12 KB

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