Advertisement
alivenets

patch-gpio-new

Apr 19th, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.49 KB | None | 0 0
  1. CC: Grant Likely <grant.likely@secretlab.ca>
  2. CC: Linus Walleij <linus.walleij@stericsson.com>
  3. Signed-off-by: Diego Elio Pettenò <flameeyes@flameeyes.eu>
  4.  
  5. MAINTAINERS | 5 +
  6. drivers/gpio/Kconfig | 11 ++
  7. drivers/gpio/Makefile | 1 +
  8. drivers/gpio/gpio-it87.c | 397 ++++++++++++++++++++++++++++++++++++++++++++++
  9. 4 files changed, 414 insertions(+), 0 deletions(-)
  10. create mode 100644 drivers/gpio/gpio-it87.c
  11.  
  12. diff --git a/MAINTAINERS b/MAINTAINERS
  13. index 553ac10..a196f14 100644
  14. a/MAINTAINERS
  15. +++ b/MAINTAINERS
  16. @@ -3692,6 +3692,11 @@ S: Maintained
  17.  F: Documentation/hwmon/it87
  18.  F: drivers/hwmon/it87.c
  19.  
  20. +IT87xx GPIO DRIVER
  21. +M: Diego Elio Pettenò <flameeyes@flameeyes.eu>
  22. +S: Maintained
  23. +F: drivers/gpio/gpio-it87.c
  24. +
  25.  IVTV VIDEO4LINUX DRIVER
  26.  M: Andy Walls <awalls@md.metrocast.net>
  27.  L: ivtv-devel@ivtvdriver.org (moderated for non-subscribers)
  28. diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
  29. index 3359f1e..8dc26b7 100644
  30. a/drivers/gpio/Kconfig
  31. +++ b/drivers/gpio/Kconfig
  32. @@ -91,6 +91,17 @@ config GPIO_IT8761E
  33.  help
  34.  Say yes here to support GPIO functionality of IT8761E super I/O chip.
  35.  
  36. +config GPIO_IT87
  37. + tristate "IT87xx GPIO support"
  38. + depends on X86 # unconditional access to IO space.
  39. + help
  40. + Say yes here to support GPIO functionality of IT87xx Super I/O chips.
  41. +
  42. + This driver currently supports ITE IT8728 Super I/O chips.
  43. +
  44. + To compile this driver as a module, choose M here: the module will
  45. + be called gpio_it87
  46. +
  47.  config GPIO_EP93XX
  48.  def_bool y
  49.  depends on ARCH_EP93XX
  50. diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
  51. index 41fe67f..affe510 100644
  52. a/drivers/gpio/Makefile
  53. +++ b/drivers/gpio/Makefile
  54. @@ -18,6 +18,7 @@ obj-$(CONFIG_ARCH_DAVINCI) += gpio-davinci.o
  55.  obj-$(CONFIG_GPIO_EP93XX) += gpio-ep93xx.o
  56.  obj-$(CONFIG_GPIO_ICH) += gpio-ich.o
  57.  obj-$(CONFIG_GPIO_IT8761E) += gpio-it8761e.o
  58. +obj-$(CONFIG_GPIO_IT87) += gpio-it87.o
  59.  obj-$(CONFIG_GPIO_JANZ_TTL) += gpio-janz-ttl.o
  60.  obj-$(CONFIG_ARCH_KS8695) += gpio-ks8695.o
  61.  obj-$(CONFIG_GPIO_LANGWELL) += gpio-langwell.o
  62. diff --git a/drivers/gpio/gpio-it87.c b/drivers/gpio/gpio-it87.c
  63. new file mode 100644
  64. index 0000000..958c126
  65. /dev/null
  66. +++ b/drivers/gpio/gpio-it87.c
  67. @@ -0,0 +1,397 @@
  68. +/*
  69. + * GPIO interface for IT87xx Super I/O chips
  70. + *
  71. + * Author: Diego Elio Pettenò <flameeyes@flameeyes.eu>
  72. + *
  73. + * Based on it87_wdt.c by Oliver Schuster
  74. + * gpio-it8761e.c by Denis Turischev
  75. + *
  76. + * This program is free software; you can redistribute it and/or modify
  77. + * it under the terms of the GNU General Public License 2 as published
  78. + * by the Free Software Foundation.
  79. + *
  80. + * This program is distributed in the hope that it will be useful,
  81. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  82. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  83. + * GNU General Public License for more details.
  84. + *
  85. + * You should have received a copy of the GNU General Public License
  86. + * along with this program; see the file COPYING. If not, write to
  87. + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  88. + */
  89. +
  90. +#include <linux/init.h>
  91. +#include <linux/kernel.h>
  92. +#include <linux/module.h>
  93. +#include <linux/io.h>
  94. +#include <linux/errno.h>
  95. +#include <linux/ioport.h>
  96. +
  97. +#include <linux/gpio.h>
  98. +
  99. +#define GPIO_NAME "it87-gpio"
  100. +#define PFX GPIO_NAME ": "
  101. +
  102. +/* Chip Id numbers */
  103. +#define NO_DEV_ID 0xffff
  104. +#define IT8728_ID 0x8728
  105. +
  106. +/* IO Ports */
  107. +#define REG 0x2e
  108. +#define VAL 0x2f
  109. +
  110. +/* Logical device Numbers LDN */
  111. +#define GPIO 0x07
  112. +
  113. +/* Configuration Registers and Functions */
  114. +#define LDNREG 0x07
  115. +#define CHIPID 0x20
  116. +#define CHIPREV 0x22
  117. +
  118. +/* GPIO Simple I/O Base Address registers */
  119. +#define GPIO_BASE 0x62
  120. +#define GPIO_IOSIZE 8
  121. +
  122. +/* GPIO polarity inverting registers base */
  123. +#define GPIO_PS_BASE 0xb0 /* to 0xb4 */
  124. +/* Simple I/O Enable registers base */
  125. +#define GPIO_SE_BASE 0xc0 /* to 0xc4 */
  126. +#define GPIO_SE_SIZE 5
  127. +/* Output Enable registers base */
  128. +#define GPIO_OE_BASE 0xc8 /* to 0xcf */
  129. +
  130. +/* Superio Chip */
  131. +
  132. +static inline int superio_enter(void)
  133. +{
  134. + /*
  135. + * Try to reserve REG and REG + 1 for exclusive access.
  136. + */
  137. + if (!request_muxed_region(REG, 2, GPIO_NAME))
  138. + return -EBUSY;
  139. +
  140. + outb(0x87, REG);
  141. + outb(0x01, REG);
  142. + outb(0x55, REG);
  143. + outb(0x55, REG);
  144. + return 0;
  145. +}
  146. +
  147. +static inline void superio_exit(void)
  148. +{
  149. + outb(0x02, REG);
  150. + outb(0x02, VAL);
  151. + release_region(REG, 2);
  152. +}
  153. +
  154. +static inline void superio_select(int ldn)
  155. +{
  156. + outb(LDNREG, REG);
  157. + outb(ldn, VAL);
  158. +}
  159. +
  160. +static inline int superio_inb(int reg)
  161. +{
  162. + outb(reg, REG);
  163. + return inb(VAL);
  164. +}
  165. +
  166. +static inline void superio_outb(int val, int reg)
  167. +{
  168. + outb(reg, REG);
  169. + outb(val, VAL);
  170. +}
  171. +
  172. +static inline int superio_inw(int reg)
  173. +{
  174. + int val;
  175. + outb(reg++, REG);
  176. + val = inb(VAL) << 8;
  177. + outb(reg, REG);
  178. + val |= inb(VAL);
  179. + return val;
  180. +}
  181. +
  182. +static inline void superio_outw(int val, int reg)
  183. +{
  184. + outb(reg++, REG);
  185. + outb(val >> 8, VAL);
  186. + outb(reg, REG);
  187. + outb(val, VAL);
  188. +}
  189. +
  190. +static inline void superio_set_bit(int bit, int reg)
  191. +{
  192. + u8 curr_val = superio_inb(reg);
  193. + u8 new_val = curr_val | 1 << bit;
  194. +
  195. + if (curr_val != new_val)
  196. + superio_outb(new_val, reg);
  197. +}
  198. +
  199. +static inline void superio_clear_bit(int bit, int reg)
  200. +{
  201. + u8 curr_val = superio_inb(reg);
  202. + u8 new_val = curr_val & ~(1 << bit);
  203. +
  204. + if (curr_val != new_val)
  205. + superio_outb(new_val, reg);
  206. +}
  207. +
  208. +static u16 gpio_ba;
  209. +
  210. +static int it87_gpio_request(struct gpio_chip *gc, unsigned gpio_num)
  211. +{
  212. + u8 bit, group;
  213. + int rc;
  214. +
  215. + bit = gpio_num % 8;
  216. + group = (gpio_num / 8);
  217. +
  218. + if ((rc = superio_enter()))
  219. + return rc;
  220. +
  221. + /* enable Simple I/O on the GPIO pin, removing alternate
  222. + * function; only the first five groups are programmable as
  223. + * either Simple I/O or alternate function, the final free are
  224. + * always set to Simple I/O.
  225. + *
  226. + * This might differ depending on chip type so it could have
  227. + * to be made configurable.
  228. + */
  229. + if (group >= GPIO_SE_SIZE)
  230. + superio_set_bit(bit, group + GPIO_SE_BASE);
  231. +
  232. + /* clear output enable, setting the pin to input, as all the
  233. + * newly-exported GPIO interfaces are set to input.
  234. + */
  235. + superio_clear_bit(bit, group + GPIO_OE_BASE);
  236. +
  237. + superio_exit();
  238. +
  239. + return 0;
  240. +}
  241. +
  242. +static int it87_gpio_get(struct gpio_chip *gc, unsigned gpio_num)
  243. +{
  244. + u16 reg;
  245. + u8 bit;
  246. +
  247. + bit = gpio_num % 8;
  248. + reg = (gpio_num / 8) + gpio_ba;
  249. +
  250. + return !!(inb(reg) & (1 << bit));
  251. +}
  252. +
  253. +static int it87_gpio_direction_in(struct gpio_chip *gc, unsigned gpio_num)
  254. +{
  255. + u8 bit, group;
  256. + int rc;
  257. +
  258. + bit = gpio_num % 8;
  259. + group = (gpio_num / 8);
  260. +
  261. + if ((rc = superio_enter()))
  262. + return rc;
  263. +
  264. + /* clear the output enable bit */
  265. + superio_clear_bit(bit, group + GPIO_OE_BASE);
  266. +
  267. + superio_exit();
  268. +
  269. + return 0;
  270. +}
  271. +
  272. +static void it87_gpio_set(struct gpio_chip *gc,
  273. + unsigned gpio_num, int val)
  274. +{
  275. + unsigned long flags;
  276. + u8 curr_vals, bit;
  277. + u16 reg;
  278. +
  279. + bit = gpio_num % 8;
  280. + reg = (gpio_num / 8) + gpio_ba;
  281. +
  282. + curr_vals = inb(reg);
  283. + if (val)
  284. + outb(curr_vals | (1 << bit) , reg);
  285. + else
  286. + outb(curr_vals & ~(1 << bit), reg);
  287. +}
  288. +
  289. +static int it87_gpio_direction_out(struct gpio_chip *gc,
  290. + unsigned gpio_num, int val)
  291. +{
  292. + u8 bit, group;
  293. + int rc;
  294. +
  295. + bit = gpio_num % 8;
  296. + group = (gpio_num / 8);
  297. +
  298. + if ((rc = superio_enter()))
  299. + return rc;
  300. +
  301. + /* set the output enable bit */
  302. + superio_set_bit(bit, group + GPIO_OE_BASE);
  303. +
  304. + it87_gpio_set(gc, gpio_num, val);
  305. +
  306. + superio_exit();
  307. +
  308. + return 0;
  309. +}
  310. +
  311. +/* ITE documentation refers to the GPIO registers as coordinates of
  312. + * group/bit; we alias them as otherwise it becomes hard to find a
  313. + * correlation between the chip's offsets and the names in the
  314. + * documentation.
  315. + */
  316. +static const char *const it87_gpio_aliases[] = {
  317. + "it87_gp10",
  318. + "it87_gp11",
  319. + "it87_gp12",
  320. + "it87_gp13",
  321. + "it87_gp14",
  322. + "it87_gp15",
  323. + "it87_gp16",
  324. + "it87_gp17",
  325. + "it87_gp20",
  326. + "it87_gp21",
  327. + "it87_gp22",
  328. + "it87_gp23",
  329. + "it87_gp24",
  330. + "it87_gp25",
  331. + "it87_gp26",
  332. + "it87_gp27",
  333. + "it87_gp30",
  334. + "it87_gp31",
  335. + "it87_gp32",
  336. + "it87_gp33",
  337. + "it87_gp34",
  338. + "it87_gp35",
  339. + "it87_gp36",
  340. + "it87_gp37",
  341. + "it87_gp40",
  342. + "it87_gp41",
  343. + "it87_gp42",
  344. + "it87_gp43",
  345. + "it87_gp44",
  346. + "it87_gp45",
  347. + "it87_gp46",
  348. + "it87_gp47",
  349. + "it87_gp50",
  350. + "it87_gp51",
  351. + "it87_gp52",
  352. + "it87_gp53",
  353. + "it87_gp54",
  354. + "it87_gp55",
  355. + "it87_gp56",
  356. + "it87_gp57",
  357. + "it87_gp60",
  358. + "it87_gp61",
  359. + "it87_gp62",
  360. + "it87_gp63",
  361. + "it87_gp64",
  362. + "it87_gp65",
  363. + "it87_gp66",
  364. + "it87_gp67",
  365. + "it87_gp70",
  366. + "it87_gp71",
  367. + "it87_gp72",
  368. + "it87_gp73",
  369. + "it87_gp74",
  370. + "it87_gp75",
  371. + "it87_gp76",
  372. + "it87_gp77",
  373. + "it87_gp80",
  374. + "it87_gp81",
  375. + "it87_gp82",
  376. + "it87_gp83",
  377. + "it87_gp84",
  378. + "it87_gp85",
  379. + "it87_gp86",
  380. + "it87_gp87"
  381. +};
  382. +
  383. +static struct gpio_chip it87_gpio_chip = {
  384. + .label = GPIO_NAME,
  385. + .owner = THIS_MODULE,
  386. + .request = it87_gpio_request,
  387. + .get = it87_gpio_get,
  388. + .direction_input = it87_gpio_direction_in,
  389. + .set = it87_gpio_set,
  390. + .direction_output = it87_gpio_direction_out,
  391. + .names = it87_gpio_aliases
  392. +};
  393. +
  394. +static int __init it87_gpio_init(void)
  395. +{
  396. + int rc = 0;
  397. + u8 chip_rev;
  398. + u16 chip_type;
  399. +
  400. + if ((rc = superio_enter()))
  401. + return rc;
  402. +
  403. + chip_type = superio_inw(CHIPID);
  404. + chip_rev = superio_inb(CHIPREV) & 0x0f;
  405. + superio_exit();
  406. +
  407. + switch(chip_type) {
  408. + case IT8728_ID:
  409. + break;
  410. + case NO_DEV_ID:
  411. + printk(KERN_ERR PFX "no device");
  412. + return -ENODEV;
  413. + default:
  414. + printk(KERN_ERR PFX
  415. + "Unknown Chip found, Chip %04x Revision %x",
  416. + chip_type, chip_rev);
  417. + return -ENODEV;
  418. + }
  419. +
  420. + if ((rc = superio_enter()))
  421. + return rc;
  422. +
  423. + superio_select(GPIO);
  424. +
  425. + /* fetch GPIO base address */
  426. + gpio_ba = superio_inw(GPIO_BASE);
  427. +
  428. + superio_exit();
  429. +
  430. + if (!request_region(gpio_ba, GPIO_IOSIZE, GPIO_NAME))
  431. + return -EBUSY;
  432. +
  433. + it87_gpio_chip.base = -1;
  434. + it87_gpio_chip.ngpio = 64;
  435. +
  436. + if ((rc = gpiochip_add(&it87_gpio_chip)) < 0)
  437. + goto gpiochip_add_err;
  438. +
  439. + return 0;
  440. +
  441. +gpiochip_add_err:
  442. + release_region(gpio_ba, GPIO_IOSIZE);
  443. + gpio_ba = 0;
  444. + return rc;
  445. +}
  446. +
  447. +static void __exit it87_gpio_exit(void)
  448. +{
  449. + if (gpio_ba) {
  450. + int ret = gpiochip_remove(&it87_gpio_chip);
  451. +
  452. + WARN(ret, "%s(): gpiochip_remove() failed, ret=%d",
  453. + __func__, ret);
  454. +
  455. + release_region(gpio_ba, GPIO_IOSIZE);
  456. + gpio_ba = 0;
  457. + }
  458. +}
  459. +module_init(it87_gpio_init);
  460. +module_exit(it87_gpio_exit);
  461. +
  462. +MODULE_AUTHOR("Diego Elio Pettenò <flameeyes@flameeyes.eu>");
  463. +MODULE_DESCRIPTION("GPIO interface for IT87xx Super I/O chips");
  464. +MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement