gpio-it87.c 9.2 KB

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