gpio-it87.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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.h>
  33. /* Chip Id numbers */
  34. #define NO_DEV_ID 0xffff
  35. #define IT8620_ID 0x8620
  36. #define IT8628_ID 0x8628
  37. #define IT8728_ID 0x8728
  38. #define IT8732_ID 0x8732
  39. #define IT8761_ID 0x8761
  40. #define IT8772_ID 0x8772
  41. /* IO Ports */
  42. #define REG 0x2e
  43. #define VAL 0x2f
  44. /* Logical device Numbers LDN */
  45. #define GPIO 0x07
  46. /* Configuration Registers and Functions */
  47. #define LDNREG 0x07
  48. #define CHIPID 0x20
  49. #define CHIPREV 0x22
  50. /**
  51. * struct it87_gpio - it87-specific GPIO chip
  52. * @chip the underlying gpio_chip structure
  53. * @lock a lock to avoid races between operations
  54. * @io_base base address for gpio ports
  55. * @io_size size of the port rage starting from io_base.
  56. * @output_base Super I/O register address for Output Enable register
  57. * @simple_base Super I/O 'Simple I/O' Enable register
  58. * @simple_size Super IO 'Simple I/O' Enable register size; this is
  59. * required because IT87xx chips might only provide Simple I/O
  60. * switches on a subset of lines, whereas the others keep the
  61. * same status all time.
  62. */
  63. struct it87_gpio {
  64. struct gpio_chip chip;
  65. spinlock_t lock;
  66. u16 io_base;
  67. u16 io_size;
  68. u8 output_base;
  69. u8 simple_base;
  70. u8 simple_size;
  71. };
  72. static struct it87_gpio it87_gpio_chip = {
  73. .lock = __SPIN_LOCK_UNLOCKED(it87_gpio_chip.lock),
  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 = gpiochip_get_data(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 = gpiochip_get_data(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 = gpiochip_get_data(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 = gpiochip_get_data(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 = gpiochip_get_data(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 const 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 IT8620_ID:
  252. case IT8628_ID:
  253. gpio_ba_reg = 0x62;
  254. it87_gpio->io_size = 11;
  255. it87_gpio->output_base = 0xc8;
  256. it87_gpio->simple_size = 0;
  257. it87_gpio->chip.ngpio = 64;
  258. break;
  259. case IT8728_ID:
  260. case IT8732_ID:
  261. case IT8772_ID:
  262. gpio_ba_reg = 0x62;
  263. it87_gpio->io_size = 8;
  264. it87_gpio->output_base = 0xc8;
  265. it87_gpio->simple_base = 0xc0;
  266. it87_gpio->simple_size = 5;
  267. it87_gpio->chip.ngpio = 64;
  268. break;
  269. case IT8761_ID:
  270. gpio_ba_reg = 0x60;
  271. it87_gpio->io_size = 4;
  272. it87_gpio->output_base = 0xf0;
  273. it87_gpio->simple_size = 0;
  274. it87_gpio->chip.ngpio = 16;
  275. break;
  276. case NO_DEV_ID:
  277. pr_err("no device\n");
  278. return -ENODEV;
  279. default:
  280. pr_err("Unknown Chip found, Chip %04x Revision %x\n",
  281. chip_type, chip_rev);
  282. return -ENODEV;
  283. }
  284. rc = superio_enter();
  285. if (rc)
  286. return rc;
  287. superio_select(GPIO);
  288. /* fetch GPIO base address */
  289. it87_gpio->io_base = superio_inw(gpio_ba_reg);
  290. superio_exit();
  291. pr_info("Found Chip IT%04x rev %x. %u GPIO lines starting at %04xh\n",
  292. chip_type, chip_rev, it87_gpio->chip.ngpio,
  293. it87_gpio->io_base);
  294. if (!request_region(it87_gpio->io_base, it87_gpio->io_size,
  295. KBUILD_MODNAME))
  296. return -EBUSY;
  297. /* Set up aliases for the GPIO connection.
  298. *
  299. * ITE documentation for recent chips such as the IT8728F
  300. * refers to the GPIO lines as GPxy, with a coordinates system
  301. * where x is the GPIO group (starting from 1) and y is the
  302. * bit within the group.
  303. *
  304. * By creating these aliases, we make it easier to understand
  305. * to which GPIO pin we're referring to.
  306. */
  307. labels = kcalloc(it87_gpio->chip.ngpio, sizeof("it87_gpXY"),
  308. GFP_KERNEL);
  309. labels_table = kcalloc(it87_gpio->chip.ngpio, sizeof(const char *),
  310. GFP_KERNEL);
  311. if (!labels || !labels_table) {
  312. rc = -ENOMEM;
  313. goto labels_free;
  314. }
  315. for (i = 0; i < it87_gpio->chip.ngpio; i++) {
  316. char *label = &labels[i * sizeof("it87_gpXY")];
  317. sprintf(label, "it87_gp%u%u", 1+(i/8), i%8);
  318. labels_table[i] = label;
  319. }
  320. it87_gpio->chip.names = (const char *const*)labels_table;
  321. rc = gpiochip_add_data(&it87_gpio->chip, it87_gpio);
  322. if (rc)
  323. goto labels_free;
  324. return 0;
  325. labels_free:
  326. kfree(labels_table);
  327. kfree(labels);
  328. release_region(it87_gpio->io_base, it87_gpio->io_size);
  329. return rc;
  330. }
  331. static void __exit it87_gpio_exit(void)
  332. {
  333. struct it87_gpio *it87_gpio = &it87_gpio_chip;
  334. gpiochip_remove(&it87_gpio->chip);
  335. release_region(it87_gpio->io_base, it87_gpio->io_size);
  336. kfree(it87_gpio->chip.names[0]);
  337. kfree(it87_gpio->chip.names);
  338. }
  339. module_init(it87_gpio_init);
  340. module_exit(it87_gpio_exit);
  341. MODULE_AUTHOR("Diego Elio Pettenò <flameeyes@flameeyes.eu>");
  342. MODULE_DESCRIPTION("GPIO interface for IT87xx Super I/O chips");
  343. MODULE_LICENSE("GPL");