gpio-it87.c 9.7 KB

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