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