gpio-f7188x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * GPIO driver for Fintek Super-I/O F71869, F71869A, F71882 and F71889
  3. *
  4. * Copyright (C) 2010-2013 LaCie
  5. *
  6. * Author: Simon Guinot <simon.guinot@sequanux.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/io.h>
  17. #include <linux/gpio.h>
  18. #define DRVNAME "gpio-f7188x"
  19. /*
  20. * Super-I/O registers
  21. */
  22. #define SIO_LDSEL 0x07 /* Logical device select */
  23. #define SIO_DEVID 0x20 /* Device ID (2 bytes) */
  24. #define SIO_DEVREV 0x22 /* Device revision */
  25. #define SIO_MANID 0x23 /* Fintek ID (2 bytes) */
  26. #define SIO_LD_GPIO 0x06 /* GPIO logical device */
  27. #define SIO_UNLOCK_KEY 0x87 /* Key to enable Super-I/O */
  28. #define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */
  29. #define SIO_FINTEK_ID 0x1934 /* Manufacturer ID */
  30. #define SIO_F71869_ID 0x0814 /* F71869 chipset ID */
  31. #define SIO_F71869A_ID 0x1007 /* F71869A chipset ID */
  32. #define SIO_F71882_ID 0x0541 /* F71882 chipset ID */
  33. #define SIO_F71889_ID 0x0909 /* F71889 chipset ID */
  34. enum chips { f71869, f71869a, f71882fg, f71889f };
  35. static const char * const f7188x_names[] = {
  36. "f71869",
  37. "f71869a",
  38. "f71882fg",
  39. "f71889f",
  40. };
  41. struct f7188x_sio {
  42. int addr;
  43. enum chips type;
  44. };
  45. struct f7188x_gpio_bank {
  46. struct gpio_chip chip;
  47. unsigned int regbase;
  48. struct f7188x_gpio_data *data;
  49. };
  50. struct f7188x_gpio_data {
  51. struct f7188x_sio *sio;
  52. int nr_bank;
  53. struct f7188x_gpio_bank *bank;
  54. };
  55. /*
  56. * Super-I/O functions.
  57. */
  58. static inline int superio_inb(int base, int reg)
  59. {
  60. outb(reg, base);
  61. return inb(base + 1);
  62. }
  63. static int superio_inw(int base, int reg)
  64. {
  65. int val;
  66. outb(reg++, base);
  67. val = inb(base + 1) << 8;
  68. outb(reg, base);
  69. val |= inb(base + 1);
  70. return val;
  71. }
  72. static inline void superio_outb(int base, int reg, int val)
  73. {
  74. outb(reg, base);
  75. outb(val, base + 1);
  76. }
  77. static inline int superio_enter(int base)
  78. {
  79. /* Don't step on other drivers' I/O space by accident. */
  80. if (!request_muxed_region(base, 2, DRVNAME)) {
  81. pr_err(DRVNAME "I/O address 0x%04x already in use\n", base);
  82. return -EBUSY;
  83. }
  84. /* According to the datasheet the key must be send twice. */
  85. outb(SIO_UNLOCK_KEY, base);
  86. outb(SIO_UNLOCK_KEY, base);
  87. return 0;
  88. }
  89. static inline void superio_select(int base, int ld)
  90. {
  91. outb(SIO_LDSEL, base);
  92. outb(ld, base + 1);
  93. }
  94. static inline void superio_exit(int base)
  95. {
  96. outb(SIO_LOCK_KEY, base);
  97. release_region(base, 2);
  98. }
  99. /*
  100. * GPIO chip.
  101. */
  102. static int f7188x_gpio_direction_in(struct gpio_chip *chip, unsigned offset);
  103. static int f7188x_gpio_get(struct gpio_chip *chip, unsigned offset);
  104. static int f7188x_gpio_direction_out(struct gpio_chip *chip,
  105. unsigned offset, int value);
  106. static void f7188x_gpio_set(struct gpio_chip *chip, unsigned offset, int value);
  107. #define F7188X_GPIO_BANK(_base, _ngpio, _regbase) \
  108. { \
  109. .chip = { \
  110. .label = DRVNAME, \
  111. .owner = THIS_MODULE, \
  112. .direction_input = f7188x_gpio_direction_in, \
  113. .get = f7188x_gpio_get, \
  114. .direction_output = f7188x_gpio_direction_out, \
  115. .set = f7188x_gpio_set, \
  116. .base = _base, \
  117. .ngpio = _ngpio, \
  118. .can_sleep = true, \
  119. }, \
  120. .regbase = _regbase, \
  121. }
  122. #define gpio_dir(base) (base + 0)
  123. #define gpio_data_out(base) (base + 1)
  124. #define gpio_data_in(base) (base + 2)
  125. /* Output mode register (0:open drain 1:push-pull). */
  126. #define gpio_out_mode(base) (base + 3)
  127. static struct f7188x_gpio_bank f71869_gpio_bank[] = {
  128. F7188X_GPIO_BANK(0, 6, 0xF0),
  129. F7188X_GPIO_BANK(10, 8, 0xE0),
  130. F7188X_GPIO_BANK(20, 8, 0xD0),
  131. F7188X_GPIO_BANK(30, 8, 0xC0),
  132. F7188X_GPIO_BANK(40, 8, 0xB0),
  133. F7188X_GPIO_BANK(50, 5, 0xA0),
  134. F7188X_GPIO_BANK(60, 6, 0x90),
  135. };
  136. static struct f7188x_gpio_bank f71869a_gpio_bank[] = {
  137. F7188X_GPIO_BANK(0, 6, 0xF0),
  138. F7188X_GPIO_BANK(10, 8, 0xE0),
  139. F7188X_GPIO_BANK(20, 8, 0xD0),
  140. F7188X_GPIO_BANK(30, 8, 0xC0),
  141. F7188X_GPIO_BANK(40, 8, 0xB0),
  142. F7188X_GPIO_BANK(50, 5, 0xA0),
  143. F7188X_GPIO_BANK(60, 8, 0x90),
  144. F7188X_GPIO_BANK(70, 8, 0x80),
  145. };
  146. static struct f7188x_gpio_bank f71882_gpio_bank[] = {
  147. F7188X_GPIO_BANK(0, 8, 0xF0),
  148. F7188X_GPIO_BANK(10, 8, 0xE0),
  149. F7188X_GPIO_BANK(20, 8, 0xD0),
  150. F7188X_GPIO_BANK(30, 4, 0xC0),
  151. F7188X_GPIO_BANK(40, 4, 0xB0),
  152. };
  153. static struct f7188x_gpio_bank f71889_gpio_bank[] = {
  154. F7188X_GPIO_BANK(0, 7, 0xF0),
  155. F7188X_GPIO_BANK(10, 7, 0xE0),
  156. F7188X_GPIO_BANK(20, 8, 0xD0),
  157. F7188X_GPIO_BANK(30, 8, 0xC0),
  158. F7188X_GPIO_BANK(40, 8, 0xB0),
  159. F7188X_GPIO_BANK(50, 5, 0xA0),
  160. F7188X_GPIO_BANK(60, 8, 0x90),
  161. F7188X_GPIO_BANK(70, 8, 0x80),
  162. };
  163. static int f7188x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  164. {
  165. int err;
  166. struct f7188x_gpio_bank *bank = gpiochip_get_data(chip);
  167. struct f7188x_sio *sio = bank->data->sio;
  168. u8 dir;
  169. err = superio_enter(sio->addr);
  170. if (err)
  171. return err;
  172. superio_select(sio->addr, SIO_LD_GPIO);
  173. dir = superio_inb(sio->addr, gpio_dir(bank->regbase));
  174. dir &= ~(1 << offset);
  175. superio_outb(sio->addr, gpio_dir(bank->regbase), dir);
  176. superio_exit(sio->addr);
  177. return 0;
  178. }
  179. static int f7188x_gpio_get(struct gpio_chip *chip, unsigned offset)
  180. {
  181. int err;
  182. struct f7188x_gpio_bank *bank = gpiochip_get_data(chip);
  183. struct f7188x_sio *sio = bank->data->sio;
  184. u8 dir, data;
  185. err = superio_enter(sio->addr);
  186. if (err)
  187. return err;
  188. superio_select(sio->addr, SIO_LD_GPIO);
  189. dir = superio_inb(sio->addr, gpio_dir(bank->regbase));
  190. dir = !!(dir & (1 << offset));
  191. if (dir)
  192. data = superio_inb(sio->addr, gpio_data_out(bank->regbase));
  193. else
  194. data = superio_inb(sio->addr, gpio_data_in(bank->regbase));
  195. superio_exit(sio->addr);
  196. return !!(data & 1 << offset);
  197. }
  198. static int f7188x_gpio_direction_out(struct gpio_chip *chip,
  199. unsigned offset, int value)
  200. {
  201. int err;
  202. struct f7188x_gpio_bank *bank = gpiochip_get_data(chip);
  203. struct f7188x_sio *sio = bank->data->sio;
  204. u8 dir, data_out;
  205. err = superio_enter(sio->addr);
  206. if (err)
  207. return err;
  208. superio_select(sio->addr, SIO_LD_GPIO);
  209. data_out = superio_inb(sio->addr, gpio_data_out(bank->regbase));
  210. if (value)
  211. data_out |= (1 << offset);
  212. else
  213. data_out &= ~(1 << offset);
  214. superio_outb(sio->addr, gpio_data_out(bank->regbase), data_out);
  215. dir = superio_inb(sio->addr, gpio_dir(bank->regbase));
  216. dir |= (1 << offset);
  217. superio_outb(sio->addr, gpio_dir(bank->regbase), dir);
  218. superio_exit(sio->addr);
  219. return 0;
  220. }
  221. static void f7188x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  222. {
  223. int err;
  224. struct f7188x_gpio_bank *bank = gpiochip_get_data(chip);
  225. struct f7188x_sio *sio = bank->data->sio;
  226. u8 data_out;
  227. err = superio_enter(sio->addr);
  228. if (err)
  229. return;
  230. superio_select(sio->addr, SIO_LD_GPIO);
  231. data_out = superio_inb(sio->addr, gpio_data_out(bank->regbase));
  232. if (value)
  233. data_out |= (1 << offset);
  234. else
  235. data_out &= ~(1 << offset);
  236. superio_outb(sio->addr, gpio_data_out(bank->regbase), data_out);
  237. superio_exit(sio->addr);
  238. }
  239. /*
  240. * Platform device and driver.
  241. */
  242. static int f7188x_gpio_probe(struct platform_device *pdev)
  243. {
  244. int err;
  245. int i;
  246. struct f7188x_sio *sio = dev_get_platdata(&pdev->dev);
  247. struct f7188x_gpio_data *data;
  248. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  249. if (!data)
  250. return -ENOMEM;
  251. switch (sio->type) {
  252. case f71869:
  253. data->nr_bank = ARRAY_SIZE(f71869_gpio_bank);
  254. data->bank = f71869_gpio_bank;
  255. break;
  256. case f71869a:
  257. data->nr_bank = ARRAY_SIZE(f71869a_gpio_bank);
  258. data->bank = f71869a_gpio_bank;
  259. break;
  260. case f71882fg:
  261. data->nr_bank = ARRAY_SIZE(f71882_gpio_bank);
  262. data->bank = f71882_gpio_bank;
  263. break;
  264. case f71889f:
  265. data->nr_bank = ARRAY_SIZE(f71889_gpio_bank);
  266. data->bank = f71889_gpio_bank;
  267. break;
  268. default:
  269. return -ENODEV;
  270. }
  271. data->sio = sio;
  272. platform_set_drvdata(pdev, data);
  273. /* For each GPIO bank, register a GPIO chip. */
  274. for (i = 0; i < data->nr_bank; i++) {
  275. struct f7188x_gpio_bank *bank = &data->bank[i];
  276. bank->chip.parent = &pdev->dev;
  277. bank->data = data;
  278. err = gpiochip_add_data(&bank->chip, bank);
  279. if (err) {
  280. dev_err(&pdev->dev,
  281. "Failed to register gpiochip %d: %d\n",
  282. i, err);
  283. goto err_gpiochip;
  284. }
  285. }
  286. return 0;
  287. err_gpiochip:
  288. for (i = i - 1; i >= 0; i--) {
  289. struct f7188x_gpio_bank *bank = &data->bank[i];
  290. gpiochip_remove(&bank->chip);
  291. }
  292. return err;
  293. }
  294. static int f7188x_gpio_remove(struct platform_device *pdev)
  295. {
  296. int i;
  297. struct f7188x_gpio_data *data = platform_get_drvdata(pdev);
  298. for (i = 0; i < data->nr_bank; i++) {
  299. struct f7188x_gpio_bank *bank = &data->bank[i];
  300. gpiochip_remove(&bank->chip);
  301. }
  302. return 0;
  303. }
  304. static int __init f7188x_find(int addr, struct f7188x_sio *sio)
  305. {
  306. int err;
  307. u16 devid;
  308. err = superio_enter(addr);
  309. if (err)
  310. return err;
  311. err = -ENODEV;
  312. devid = superio_inw(addr, SIO_MANID);
  313. if (devid != SIO_FINTEK_ID) {
  314. pr_debug(DRVNAME ": Not a Fintek device at 0x%08x\n", addr);
  315. goto err;
  316. }
  317. devid = superio_inw(addr, SIO_DEVID);
  318. switch (devid) {
  319. case SIO_F71869_ID:
  320. sio->type = f71869;
  321. break;
  322. case SIO_F71869A_ID:
  323. sio->type = f71869a;
  324. break;
  325. case SIO_F71882_ID:
  326. sio->type = f71882fg;
  327. break;
  328. case SIO_F71889_ID:
  329. sio->type = f71889f;
  330. break;
  331. default:
  332. pr_info(DRVNAME ": Unsupported Fintek device 0x%04x\n", devid);
  333. goto err;
  334. }
  335. sio->addr = addr;
  336. err = 0;
  337. pr_info(DRVNAME ": Found %s at %#x, revision %d\n",
  338. f7188x_names[sio->type],
  339. (unsigned int) addr,
  340. (int) superio_inb(addr, SIO_DEVREV));
  341. err:
  342. superio_exit(addr);
  343. return err;
  344. }
  345. static struct platform_device *f7188x_gpio_pdev;
  346. static int __init
  347. f7188x_gpio_device_add(const struct f7188x_sio *sio)
  348. {
  349. int err;
  350. f7188x_gpio_pdev = platform_device_alloc(DRVNAME, -1);
  351. if (!f7188x_gpio_pdev)
  352. return -ENOMEM;
  353. err = platform_device_add_data(f7188x_gpio_pdev,
  354. sio, sizeof(*sio));
  355. if (err) {
  356. pr_err(DRVNAME "Platform data allocation failed\n");
  357. goto err;
  358. }
  359. err = platform_device_add(f7188x_gpio_pdev);
  360. if (err) {
  361. pr_err(DRVNAME "Device addition failed\n");
  362. goto err;
  363. }
  364. return 0;
  365. err:
  366. platform_device_put(f7188x_gpio_pdev);
  367. return err;
  368. }
  369. /*
  370. * Try to match a supported Fintek device by reading the (hard-wired)
  371. * configuration I/O ports. If available, then register both the platform
  372. * device and driver to support the GPIOs.
  373. */
  374. static struct platform_driver f7188x_gpio_driver = {
  375. .driver = {
  376. .name = DRVNAME,
  377. },
  378. .probe = f7188x_gpio_probe,
  379. .remove = f7188x_gpio_remove,
  380. };
  381. static int __init f7188x_gpio_init(void)
  382. {
  383. int err;
  384. struct f7188x_sio sio;
  385. if (f7188x_find(0x2e, &sio) &&
  386. f7188x_find(0x4e, &sio))
  387. return -ENODEV;
  388. err = platform_driver_register(&f7188x_gpio_driver);
  389. if (!err) {
  390. err = f7188x_gpio_device_add(&sio);
  391. if (err)
  392. platform_driver_unregister(&f7188x_gpio_driver);
  393. }
  394. return err;
  395. }
  396. subsys_initcall(f7188x_gpio_init);
  397. static void __exit f7188x_gpio_exit(void)
  398. {
  399. platform_device_unregister(f7188x_gpio_pdev);
  400. platform_driver_unregister(&f7188x_gpio_driver);
  401. }
  402. module_exit(f7188x_gpio_exit);
  403. MODULE_DESCRIPTION("GPIO driver for Super-I/O chips F71869, F71869A, F71882FG and F71889F");
  404. MODULE_AUTHOR("Simon Guinot <simon.guinot@sequanux.org>");
  405. MODULE_LICENSE("GPL");