gpio-it87.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * GPIO interface for IT87xx Super I/O chips
  3. *
  4. * Author: Diego Elio Pettenò <flameeyes@flameeyes.eu>
  5. *
  6. * Based on it87_wdt.c by Oliver Schuster
  7. * gpio-it8761e.c by Denis Turischev
  8. * gpio-stmpe.c by Rabin Vincent
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License 2 as published
  12. * by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; see the file COPYING. If not, write to
  21. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/io.h>
  28. #include <linux/errno.h>
  29. #include <linux/ioport.h>
  30. #include <linux/slab.h>
  31. #include <linux/gpio.h>
  32. /* Chip Id numbers */
  33. #define NO_DEV_ID 0xffff
  34. #define IT8728_ID 0x8728
  35. #define IT8732_ID 0x8732
  36. #define IT8761_ID 0x8761
  37. /* IO Ports */
  38. #define REG 0x2e
  39. #define VAL 0x2f
  40. /* Logical device Numbers LDN */
  41. #define GPIO 0x07
  42. /* Configuration Registers and Functions */
  43. #define LDNREG 0x07
  44. #define CHIPID 0x20
  45. #define CHIPREV 0x22
  46. /**
  47. * struct it87_gpio - it87-specific GPIO chip
  48. * @chip the underlying gpio_chip structure
  49. * @lock a lock to avoid races between operations
  50. * @io_base base address for gpio ports
  51. * @io_size size of the port rage starting from io_base.
  52. * @output_base Super I/O register address for Output Enable register
  53. * @simple_base Super I/O 'Simple I/O' Enable register
  54. * @simple_size Super IO 'Simple I/O' Enable register size; this is
  55. * required because IT87xx chips might only provide Simple I/O
  56. * switches on a subset of lines, whereas the others keep the
  57. * same status all time.
  58. */
  59. struct it87_gpio {
  60. struct gpio_chip chip;
  61. spinlock_t lock;
  62. u16 io_base;
  63. u16 io_size;
  64. u8 output_base;
  65. u8 simple_base;
  66. u8 simple_size;
  67. };
  68. static struct it87_gpio it87_gpio_chip = {
  69. .lock = __SPIN_LOCK_UNLOCKED(it87_gpio_chip.lock),
  70. };
  71. static inline struct it87_gpio *to_it87_gpio(struct gpio_chip *chip)
  72. {
  73. return container_of(chip, struct it87_gpio, chip);
  74. }
  75. /* Superio chip access functions; copied from wdt_it87 */
  76. static inline int superio_enter(void)
  77. {
  78. /*
  79. * Try to reserve REG and REG + 1 for exclusive access.
  80. */
  81. if (!request_muxed_region(REG, 2, KBUILD_MODNAME))
  82. return -EBUSY;
  83. outb(0x87, REG);
  84. outb(0x01, REG);
  85. outb(0x55, REG);
  86. outb(0x55, REG);
  87. return 0;
  88. }
  89. static inline void superio_exit(void)
  90. {
  91. outb(0x02, REG);
  92. outb(0x02, VAL);
  93. release_region(REG, 2);
  94. }
  95. static inline void superio_select(int ldn)
  96. {
  97. outb(LDNREG, REG);
  98. outb(ldn, VAL);
  99. }
  100. static inline int superio_inb(int reg)
  101. {
  102. outb(reg, REG);
  103. return inb(VAL);
  104. }
  105. static inline void superio_outb(int val, int reg)
  106. {
  107. outb(reg, REG);
  108. outb(val, VAL);
  109. }
  110. static inline int superio_inw(int reg)
  111. {
  112. int val;
  113. outb(reg++, REG);
  114. val = inb(VAL) << 8;
  115. outb(reg, REG);
  116. val |= inb(VAL);
  117. return val;
  118. }
  119. static inline void superio_outw(int val, int reg)
  120. {
  121. outb(reg++, REG);
  122. outb(val >> 8, VAL);
  123. outb(reg, REG);
  124. outb(val, VAL);
  125. }
  126. static inline void superio_set_mask(int mask, int reg)
  127. {
  128. u8 curr_val = superio_inb(reg);
  129. u8 new_val = curr_val | mask;
  130. if (curr_val != new_val)
  131. superio_outb(new_val, reg);
  132. }
  133. static inline void superio_clear_mask(int mask, int reg)
  134. {
  135. u8 curr_val = superio_inb(reg);
  136. u8 new_val = curr_val & ~mask;
  137. if (curr_val != new_val)
  138. superio_outb(new_val, reg);
  139. }
  140. static int it87_gpio_request(struct gpio_chip *chip, unsigned gpio_num)
  141. {
  142. u8 mask, group;
  143. int rc = 0;
  144. struct it87_gpio *it87_gpio = to_it87_gpio(chip);
  145. mask = 1 << (gpio_num % 8);
  146. group = (gpio_num / 8);
  147. spin_lock(&it87_gpio->lock);
  148. rc = superio_enter();
  149. if (rc)
  150. goto exit;
  151. /* not all the IT87xx chips support Simple I/O and not all of
  152. * them allow all the lines to be set/unset to Simple I/O.
  153. */
  154. if (group < it87_gpio->simple_size)
  155. superio_set_mask(mask, group + it87_gpio->simple_base);
  156. /* clear output enable, setting the pin to input, as all the
  157. * newly-exported GPIO interfaces are set to input.
  158. */
  159. superio_clear_mask(mask, group + it87_gpio->output_base);
  160. superio_exit();
  161. exit:
  162. spin_unlock(&it87_gpio->lock);
  163. return rc;
  164. }
  165. static int it87_gpio_get(struct gpio_chip *chip, unsigned gpio_num)
  166. {
  167. u16 reg;
  168. u8 mask;
  169. struct it87_gpio *it87_gpio = to_it87_gpio(chip);
  170. mask = 1 << (gpio_num % 8);
  171. reg = (gpio_num / 8) + it87_gpio->io_base;
  172. return !!(inb(reg) & mask);
  173. }
  174. static int it87_gpio_direction_in(struct gpio_chip *chip, unsigned gpio_num)
  175. {
  176. u8 mask, group;
  177. int rc = 0;
  178. struct it87_gpio *it87_gpio = to_it87_gpio(chip);
  179. mask = 1 << (gpio_num % 8);
  180. group = (gpio_num / 8);
  181. spin_lock(&it87_gpio->lock);
  182. rc = superio_enter();
  183. if (rc)
  184. goto exit;
  185. /* clear the output enable bit */
  186. superio_clear_mask(mask, group + it87_gpio->output_base);
  187. superio_exit();
  188. exit:
  189. spin_unlock(&it87_gpio->lock);
  190. return rc;
  191. }
  192. static void it87_gpio_set(struct gpio_chip *chip,
  193. unsigned gpio_num, int val)
  194. {
  195. u8 mask, curr_vals;
  196. u16 reg;
  197. struct it87_gpio *it87_gpio = to_it87_gpio(chip);
  198. mask = 1 << (gpio_num % 8);
  199. reg = (gpio_num / 8) + it87_gpio->io_base;
  200. curr_vals = inb(reg);
  201. if (val)
  202. outb(curr_vals | mask, reg);
  203. else
  204. outb(curr_vals & ~mask, reg);
  205. }
  206. static int it87_gpio_direction_out(struct gpio_chip *chip,
  207. unsigned gpio_num, int val)
  208. {
  209. u8 mask, group;
  210. int rc = 0;
  211. struct it87_gpio *it87_gpio = to_it87_gpio(chip);
  212. mask = 1 << (gpio_num % 8);
  213. group = (gpio_num / 8);
  214. spin_lock(&it87_gpio->lock);
  215. rc = superio_enter();
  216. if (rc)
  217. goto exit;
  218. /* set the output enable bit */
  219. superio_set_mask(mask, group + it87_gpio->output_base);
  220. it87_gpio_set(chip, gpio_num, val);
  221. superio_exit();
  222. exit:
  223. spin_unlock(&it87_gpio->lock);
  224. return rc;
  225. }
  226. static struct gpio_chip it87_template_chip = {
  227. .label = KBUILD_MODNAME,
  228. .owner = THIS_MODULE,
  229. .request = it87_gpio_request,
  230. .get = it87_gpio_get,
  231. .direction_input = it87_gpio_direction_in,
  232. .set = it87_gpio_set,
  233. .direction_output = it87_gpio_direction_out,
  234. .base = -1
  235. };
  236. static int __init it87_gpio_init(void)
  237. {
  238. int rc = 0, i;
  239. u16 chip_type;
  240. u8 chip_rev, gpio_ba_reg;
  241. char *labels, **labels_table;
  242. struct it87_gpio *it87_gpio = &it87_gpio_chip;
  243. rc = superio_enter();
  244. if (rc)
  245. return rc;
  246. chip_type = superio_inw(CHIPID);
  247. chip_rev = superio_inb(CHIPREV) & 0x0f;
  248. superio_exit();
  249. it87_gpio->chip = it87_template_chip;
  250. switch (chip_type) {
  251. case IT8728_ID:
  252. case IT8732_ID:
  253. gpio_ba_reg = 0x62;
  254. it87_gpio->io_size = 8;
  255. it87_gpio->output_base = 0xc8;
  256. it87_gpio->simple_base = 0xc0;
  257. it87_gpio->simple_size = 5;
  258. it87_gpio->chip.ngpio = 64;
  259. break;
  260. case IT8761_ID:
  261. gpio_ba_reg = 0x60;
  262. it87_gpio->io_size = 4;
  263. it87_gpio->output_base = 0xf0;
  264. it87_gpio->simple_size = 0;
  265. it87_gpio->chip.ngpio = 16;
  266. break;
  267. case NO_DEV_ID:
  268. pr_err("no device\n");
  269. return -ENODEV;
  270. default:
  271. pr_err("Unknown Chip found, Chip %04x Revision %x\n",
  272. chip_type, chip_rev);
  273. return -ENODEV;
  274. }
  275. rc = superio_enter();
  276. if (rc)
  277. return rc;
  278. superio_select(GPIO);
  279. /* fetch GPIO base address */
  280. it87_gpio->io_base = superio_inw(gpio_ba_reg);
  281. superio_exit();
  282. pr_info("Found Chip IT%04x rev %x. %u GPIO lines starting at %04xh\n",
  283. chip_type, chip_rev, it87_gpio->chip.ngpio,
  284. it87_gpio->io_base);
  285. if (!request_region(it87_gpio->io_base, it87_gpio->io_size,
  286. KBUILD_MODNAME))
  287. return -EBUSY;
  288. /* Set up aliases for the GPIO connection.
  289. *
  290. * ITE documentation for recent chips such as the IT8728F
  291. * refers to the GPIO lines as GPxy, with a coordinates system
  292. * where x is the GPIO group (starting from 1) and y is the
  293. * bit within the group.
  294. *
  295. * By creating these aliases, we make it easier to understand
  296. * to which GPIO pin we're referring to.
  297. */
  298. labels = kcalloc(it87_gpio->chip.ngpio, sizeof("it87_gpXY"),
  299. GFP_KERNEL);
  300. labels_table = kcalloc(it87_gpio->chip.ngpio, sizeof(const char *),
  301. GFP_KERNEL);
  302. if (!labels || !labels_table) {
  303. rc = -ENOMEM;
  304. goto labels_free;
  305. }
  306. for (i = 0; i < it87_gpio->chip.ngpio; i++) {
  307. char *label = &labels[i * sizeof("it87_gpXY")];
  308. sprintf(label, "it87_gp%u%u", 1+(i/8), i%8);
  309. labels_table[i] = label;
  310. }
  311. it87_gpio->chip.names = (const char *const*)labels_table;
  312. rc = gpiochip_add(&it87_gpio->chip);
  313. if (rc)
  314. goto labels_free;
  315. return 0;
  316. labels_free:
  317. kfree(labels_table);
  318. kfree(labels);
  319. release_region(it87_gpio->io_base, it87_gpio->io_size);
  320. return rc;
  321. }
  322. static void __exit it87_gpio_exit(void)
  323. {
  324. struct it87_gpio *it87_gpio = &it87_gpio_chip;
  325. gpiochip_remove(&it87_gpio->chip);
  326. release_region(it87_gpio->io_base, it87_gpio->io_size);
  327. kfree(it87_gpio->chip.names[0]);
  328. kfree(it87_gpio->chip.names);
  329. }
  330. module_init(it87_gpio_init);
  331. module_exit(it87_gpio_exit);
  332. MODULE_AUTHOR("Diego Elio Pettenò <flameeyes@flameeyes.eu>");
  333. MODULE_DESCRIPTION("GPIO interface for IT87xx Super I/O chips");
  334. MODULE_LICENSE("GPL");